Last active
June 22, 2018 18:05
-
-
Save keithstric/8d2bc4ebf51679d1eb620a17e7e99c79 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as ts from 'typescript'; | |
let options = { | |
stripInternal: true, | |
target: ts.ScriptTarget.ES5, | |
experimentalDecorators: true, | |
listEmittedFiles: true | |
}; | |
function transform(ctx: ts.TransformationContext): ts.Transformer<ts.SourceFile> { | |
return (rootNode): ts.SourceFile => { | |
const visitChildren: ts.Visitor = (node: ts.Node): ts.VisitResult<ts.Node> => { | |
console.log('Node kind=', ts.SyntaxKind[node.kind]); | |
node = ts.visitEachChild(node, visitChildren, ctx); | |
if (ts.isMethodDeclaration(node)) { | |
let methodDecl: ts.MethodDeclaration = <ts.MethodDeclaration> node; | |
if (methodDecl && methodDecl.name) { | |
let name = <ts.Identifier> methodDecl.name; | |
let text = name.text; | |
if (text === 'getFoo') { | |
let newName: ts.Identifier = ts.createIdentifier('getFooBar'); | |
return ts.updateMethod( | |
methodDecl, | |
methodDecl.decorators, | |
methodDecl.modifiers, | |
methodDecl.asteriskToken, | |
newName, | |
methodDecl.questionToken, | |
methodDecl.typeParameters, | |
methodDecl.parameters, | |
methodDecl.type, | |
methodDecl.body | |
); | |
} | |
} | |
} | |
return node; | |
} | |
return ts.visitNode(rootNode, visitChildren); | |
} | |
} | |
const filePath = '/Users/keithstrickland/RedPill/NPM/ts-compiler-experiment/src/data/sample.ts'; | |
const newFilePath = '/Users/keithstrickland/RedPill/NPM/ts-compiler-experiment/src/data/sample_transformed.ts'; | |
const program = ts.createProgram( | |
[filePath], | |
options | |
); | |
const sourceFile = program.getSourceFile(filePath); | |
const result: ts.TransformationResult<ts.SourceFile> = ts.transform(sourceFile, [transform], options); | |
const transformedSourceFile: ts.SourceFile = result.transformed[0]; | |
const newSource: ts.SourceFile = ts.createSourceFile( | |
newFilePath, | |
transformedSourceFile.getText(), | |
ts.ScriptTarget.ES5, | |
false, | |
ts.ScriptKind.TS); | |
const printer: ts.Printer = ts.createPrinter({newLine: ts.NewLineKind.LineFeed}); | |
printer.printFile(newSource); |
This file contains hidden or 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
export class foo { | |
private _bar: string; | |
get bar() { | |
return this._bar; | |
} | |
set bar(bar) { | |
this._bar = bar; | |
} | |
getFoo() { // Expect this method name to be changed to getFooBar | |
return 'foo'; | |
} | |
getBar() { | |
return 'foo'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment