Skip to content

Commit

Permalink
Merge pull request #65 from razroo/ZETA-6981-codemod-typescript-const…
Browse files Browse the repository at this point in the history
…ant-alt

[ZETA-6981]:  Typescript codemod for adding variable statement
  • Loading branch information
CharlieGreenman authored Oct 29, 2023
2 parents cc1e7b9 + e9d14a7 commit 62b01d8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/rz/morph/interfaces/morph.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const NOT_SUPPORTED = 'NOT_SUPPORTED';
export interface EditFile {
nodeType?: 'import' | 'export' | 'classDeclaration' | 'classMethod' | 'addNgModuleImport' | 'addNgModuleImportToSpec' |
'addNgModuleProvider' | 'addNgModuleDeclaration' | 'addNgModuleProviderToSpec' | 'addToVariableObject' | 'editImport' | 'addConstructorMethod' |
'addVariableDeclarationStatement' | 'addClassMethod' | 'addImportsToExisting' | 'addNgModuleExport' | 'addFunction';
'addVariableDeclarationStatement' | 'addClassMethod' | 'addImportsToExisting' | 'addNgModuleExport' | 'addFunction' | 'addVariableStatement';
codeBlock: string | any | any[];
isExported?: boolean;
nodeToInsertInto?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { EditInput, morphCode } from "../../morph";

describe('addVariableStatement', () => {
it('should add a variable statement', () => {
const fileToBeAddedTo = `import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [CommonModule],
})
export class DataGraphqlModule { }`;

const editTypescriptInput: EditInput = {
fileToBeAddedTo: fileToBeAddedTo,
fileName: 'name.component.ts',
fileType: 'ts',
edits: [
{
nodeType: 'addVariableStatement',
name: 'cleanTypenameApolloLink',
codeBlock: 'new ApolloLink(cleanTypenameLink)'
}
]
};

const result = morphCode(editTypescriptInput);
const expected = `const cleanTypenameApolloLink = new ApolloLink(cleanTypenameLink);
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [CommonModule],
})
export class DataGraphqlModule { }
`;
expect(result).toEqual(expected);
});
});
15 changes: 15 additions & 0 deletions src/rz/typescript/add-variable-statement/add-variable-statement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { VariableDeclarationKind } from "ts-morph";
import { EditCodeBlockInput } from "../interfaces/edit-typescript.interface";

// right now assumes all methods will be private
export function addVariableStatement(editCodeBlockInput: EditCodeBlockInput): void {
console.log('add variale calls');
const sourceFile = editCodeBlockInput.sourceFile;
sourceFile.insertVariableStatement(0, {
declarationKind: VariableDeclarationKind.Const,
declarations: [{
name: editCodeBlockInput.name as string,
initializer: editCodeBlockInput.codeBlock
}]
});
}
4 changes: 4 additions & 0 deletions src/rz/typescript/morph-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { addNgModuleImport } from "./angular-typescript/add-ngmodule-import/ngmo
import { addNgModuleItem, addNgModuleItemToSpec } from "./angular-typescript/add-ngmodule-item/add-ngmodule-item";
import { EditCodeBlockInput } from "./interfaces/edit-typescript.interface";
import { addToVariableObject } from "./variable-statement/variable-statement";
import { addVariableStatement } from "./add-variable-statement/add-variable-statement";

// methodToInsertInto e.g. 'ngAfterViewInit'
// codeBeingAdded e.g. 'this.dataSource.paginator = this.paginator;'
Expand Down Expand Up @@ -125,6 +126,9 @@ export function morphTypescript(editTypescriptInput: EditInput): string {
case 'addClassMethod':
addClassMethod(editWithSourceFile)
break;
case 'addVariableStatement':
addVariableStatement(editWithSourceFile)
break;
}
});

Expand Down

0 comments on commit 62b01d8

Please sign in to comment.