Building Consistent Angular Applications with ts-morph

Gili Yaniv
3 min readAug 30, 2023

--

Photo by Sivani Bandaru on Unsplash

Developing Angular applications requires not only a solid understanding of the framework but also efficient tools that facilitate code maintenance, readability, and consistency. One such tool that can greatly enhance the development process is ts-morph. In this article, we will explore what ts-morph is and how it can be used to build consistent Angular applications, complete with code examples.

What is ts-morph?

ts-morph is a TypeScript library that provides a simple and intuitive API for interacting with TypeScript's Abstract Syntax Tree (AST). An AST is a hierarchical representation of the source code's structure, enabling developers to programmatically analyze, modify, and generate code.

By using ts-morph, developers can perform complex transformations and analyses on TypeScript code without resorting to regular expressions or string manipulation, which often leads to brittle and error-prone solutions.

Benefits of Using ts-morph in Angular Applications

Integrating ts-morph into Angular applications can yield several benefits:

  1. Code Consistency: Maintaining consistent coding patterns, naming conventions, and code organization becomes more straightforward with ts-morph. You can automate the enforcement of coding guidelines across your project.
  2. Refactoring: Renaming variables, functions, or classes across a large codebase can be a tedious and error-prone task. ts-morph simplifies refactoring by providing a programmatic way to modify code.
  3. Automation: Certain tasks, such as generating boilerplate code or performing bulk changes, can be automated using ts-morph. This reduces human error and saves time.
  4. Code Analysis: ts-morph enables you to perform in-depth code analysis, such as identifying unused variables or detecting potential issues, across your Angular application.
  5. Custom Code Generators: You can create custom code generators that create repetitive code structures automatically, reducing the need for manual coding.

Using ts-morph in an Angular Application

To illustrate the usage of ts-morph in an Angular application, let's consider a scenario where we want to enforce a naming convention for Angular component classes. We'll ensure that all component classes end with the word "Gili842."

Step 1: Installation

Start by installing ts-morph as a development dependency in your Angular project:

npm install ts-morph --save-dev

Step 2: Creating the Script

Next, let's create a script file that uses ts-morph to enforce our naming convention. Create a file named naming-convention.ts in your project root directory.

import { Project } from "ts-morph";

const project = new Project();

// Load your Angular component files
const files = project.addSourceFilesAtPaths("src/app/**/*.component.ts");

// Define the naming convention rule
const namingConvention = /Component$/;

// Check and update the naming convention
files.forEach(file => {
file.getClasses().forEach(classDeclaration => {
if (!namingConvention.test(classDeclaration.getName() || "")) {
classDeclaration.rename(`${classDeclaration.getName()}Gili842`); // <---
}
});
});

// Save changes
project.save();

Step 3: Running the Script

Add a script entry in your package.json file to run the naming-convention.ts script.

"scripts": {
"enforce-naming": "ts-node naming-convention.ts"
}

Run the script:

npm run enforce-naming

Step 4: Check the Results

After running the script, it will enforce the naming convention for all Angular component classes in your project.

For instance, if you had a component class named Home, it will be renamed to HomeGili842.

// Before
export class Home {
// ...
}

// After
export class HomeGili842 {
// ...
}

Conclusion

ts-morph is a powerful tool that can significantly enhance the development process of Angular applications. By utilizing its capabilities, you can ensure code consistency, automate code generation, and simplify complex refactoring tasks. This leads to more maintainable and readable codebases, ultimately benefiting both developers and the projects they work on. Incorporate ts-morph into your Angular workflow, and experience the ease and efficiency it brings to your development journey.

Follow me on Twitter, Medium, and Linkedin to read more!

--

--

Responses (1)