icon

Core Concepts

Basic Operations

This guide covers the fundamental operations available in Mutates for manipulating TypeScript code.

File Operations

Creating Files

import { createSourceFile } from '@mutates/core';

// Create a new TypeScript file
createSourceFile(
  'src/example.ts',
  `
  export class Example {
    message: string = 'Hello World';
  }
  `,
);

Reading Files

import { readFileSync } from '@mutates/core';

// Read file contents
const content = readFileSync('src/example.ts');

Class Operations

Adding Classes

import { addClasses } from '@mutates/core';

// Add a new class
addClasses('src/models.ts', {
  name: 'User',
  isExported: true,
  properties: [
    {
      name: 'id',
      type: 'number',
    },
    {
      name: 'name',
      type: 'string',
    },
  ],
});

Finding Classes

import { getClasses } from '@mutates/core';

// Find all classes
const allClasses = getClasses();

// Find classes by pattern
const modelClasses = getClasses({
  pattern: 'src/models/**/*.ts',
});

// Find classes by name
const userClass = getClasses({
  name: 'User',
});

Modifying Classes

import { editClasses } from '@mutates/core';

// Modify classes
editClasses(targetClasses, () => ({
  isExported: true,
  implements: ['Serializable'],
}));

Removing Classes

import { removeClasses } from '@mutates/core';

// Remove classes
removeClasses(targetClasses);

Method Operations

Adding Methods

import { addMethods } from '@mutates/core';

// Add methods to a class
addMethods(targetClass, {
  name: 'greet',
  parameters: [
    {
      name: 'name',
      type: 'string',
    },
  ],
  returnType: 'string',
  statements: 'return `Hello, ${name}!`;',
});

Finding Methods

import { getMethods } from '@mutates/core';

// Get all methods from classes
const methods = getMethods(targetClasses);

// Get specific methods
const getters = getMethods(targetClasses, {
  isGetter: true,
});

Modifying Methods

import { editMethods } from '@mutates/core';

// Modify methods
editMethods(methods, () => ({
  isAsync: true,
  returnType: 'Promise<string>',
}));

Property Operations

Adding Properties

import { addProperties } from '@mutates/core';

// Add properties to a class
addProperties(targetClass, [
  {
    name: 'count',
    type: 'number',
    initializer: '0',
  },
  {
    name: 'isActive',
    type: 'boolean',
    hasQuestionToken: true,
  },
]);

Finding Properties

import { getProperties } from '@mutates/core';

// Get all properties
const properties = getProperties(targetClasses);

// Get specific properties
const optionalProps = getProperties(targetClasses, {
  hasQuestionToken: true,
});

Decorator Operations

Adding Decorators

import { addDecorators } from '@mutates/core';

// Add decorators to a class
addDecorators(targetClass, {
  name: 'Component',
  arguments: [
    {
      selector: 'app-root',
      template: '<div>Hello</div>',
    },
  ],
});

Import Operations

Managing Imports

import { addImports } from '@mutates/core';

// Add imports to a file
addImports('src/example.ts', {
  namedImports: ['Component', 'Input'],
  moduleSpecifier: '@angular/core',
});

// Add default import
addImports('src/example.ts', {
  defaultImport: 'React',
  moduleSpecifier: 'react',
});

Export Operations

Managing Exports

import { addExports } from '@mutates/core';

// Add named exports
addExports('src/index.ts', {
  namedExports: ['User', 'Admin'],
  moduleSpecifier: './models',
});

// Add default export
addExports('src/main.ts', {
  defaultExport: 'App',
});

Type Operations

Adding Interfaces

import { addInterfaces } from '@mutates/core';

// Add interface
addInterfaces('src/types.ts', {
  name: 'UserData',
  isExported: true,
  properties: [
    {
      name: 'id',
      type: 'number'
    },
    {
      name: 'profile',
      type: '{
        name: string;
        email: string;
      }'
    }
  ]
});

Adding Type Aliases

import { addTypeAliases } from '@mutates/core';

// Add type alias
addTypeAliases('src/types.ts', {
  name: 'UserId',
  type: 'string | number',
  isExported: true,
});

Best Practices

  1. Group Related Operations
// Group related transformations
const userClass = getClasses({ name: 'User' })[0];
addProperties(userClass /* properties */);
addMethods(userClass /* methods */);
addDecorators(userClass /* decorators */);
  1. Use Pattern Matching Effectively
// Be specific with patterns
const serviceClasses = getClasses({
  pattern: 'src/**/*.service.ts',
});
  1. Handle Errors
try {
  // Your operations
} catch (error) {
  console.error('Failed to transform code:', error);
}

Next Steps

Previous
Understanding AST