Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section "rename symbol" #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example-transformers/rename-symbol/source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let foo = 1;
foo = 2;
{
const foo = 'abcd';
}
foo = 3;
6 changes: 6 additions & 0 deletions example-transformers/rename-symbol/transformed/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let bar = 1;
bar = 2;
{
const foo = 'abcd';
}
bar = 3;
78 changes: 78 additions & 0 deletions example-transformers/rename-symbol/transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as ts from 'typescript';

// Renames all references to the first identifier with the text "foo"
const identifierText = 'foo';
const identifierMatchIndex = 1;
const newIdentifierText = 'bar';

const transformerProgram = (program: ts.Program) => {
const typeChecker = program.getTypeChecker();

const transformerFactory: ts.TransformerFactory<ts.SourceFile> = context => {
return sourceFile => {
// 1. Find the symbol, if any
let identifierCurrentIndex = -1;
const findSymbolVisitor = (node: ts.Node): ts.Symbol | undefined => {
if (ts.isIdentifier(node)) {
const relatedSymbol = typeChecker.getSymbolAtLocation(node);

console.log(relatedSymbol.escapedName);
if (relatedSymbol.escapedName === identifierText) {
identifierCurrentIndex += 1;
if (identifierCurrentIndex === identifierMatchIndex) {
console.log('found');
return relatedSymbol;
}
}
}

return ts.forEachChild(node, findSymbolVisitor);
};

// 2. Mark the identifiers, if any
const foundIdentifiers = new Array<ts.Identifier>();
const findIdentifiersVisitor = (node: ts.Node): true | undefined => {
if (ts.isIdentifier(node)) {
const relatedSymbol = typeChecker.getSymbolAtLocation(node);

if (relatedSymbol === foundSymbol) {
foundIdentifiers.push(node);
// Stop finding
return true;
}
}

ts.forEachChild(node, findIdentifiersVisitor);
return undefined;
};

// 3. Modify the identifiers
const modifyIdentifiersVisitor = (node: ts.Node): ts.Node => {
if (ts.isIdentifier(node) && foundIdentifiers.includes(node)) {
return ts.updateIdentifier(ts.createIdentifier(newIdentifierText));
}

return ts.visitEachChild(node, modifyIdentifiersVisitor, context);
};

// Execute step 1
const foundSymbol = ts.forEachChild(sourceFile, findSymbolVisitor);

// Execute step 2, if step 1 passed
if (foundSymbol) {
ts.forEachChild(sourceFile, findIdentifiersVisitor);
}

// Execute step 3, if step 2 passed
if (foundIdentifiers.length > 0) {
return ts.visitNode(sourceFile, modifyIdentifiersVisitor);
}

return sourceFile;
};
};

return transformerFactory;
};

export default transformerProgram;
8 changes: 8 additions & 0 deletions example-transformers/rename-symbol/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "transformed",
"plugins": [{ "transform": "./transformer.ts" }]
},
"files": ["source.ts"]
}