-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from razroo/ZETA-6981-codemod-typescript-const…
…ant-alt [ZETA-6981]: Typescript codemod for adding variable statement
- Loading branch information
Showing
4 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/rz/typescript/add-variable-statement/add-variable-statement.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
src/rz/typescript/add-variable-statement/add-variable-statement.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}] | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters