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

fix: allow .tranform to work with Nodes from another parsed source file #1417

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
290 changes: 145 additions & 145 deletions deno/common/ts_morph_common.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -3736,7 +3736,7 @@ class Node {
let lastPos = replaceRange[0];
for (const transform of transformations) {
finalText += fileText.substring(lastPos, transform.start);
finalText += printer.printNode(ts.EmitHint.Unspecified, transform.compilerNode, compilerSourceFile);
finalText += printer.printNode(ts.EmitHint.Unspecified, transform.compilerNode, transform.compilerNode.getSourceFile());
lastPos = transform.end;
}
finalText += fileText.substring(lastPos, replaceRange[1]);
Expand Down
290 changes: 145 additions & 145 deletions packages/common/src/data/libFiles.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/ts-morph/src/compiler/ast/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {

for (const transform of transformations) {
finalText += fileText.substring(lastPos, transform.start);
finalText += printer.printNode(ts.EmitHint.Unspecified, transform.compilerNode, compilerSourceFile);
finalText += printer.printNode(ts.EmitHint.Unspecified, transform.compilerNode, transform.compilerNode.getSourceFile());
dsherret marked this conversation as resolved.
Show resolved Hide resolved
lastPos = transform.end;
}

Expand Down
33 changes: 29 additions & 4 deletions packages/ts-morph/src/tests/compiler/ast/common/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2139,6 +2139,27 @@ class MyClass {
}, `interface A {\n} class B {}`);
});

it("should not forget StringLiterals", () => {
const parseCode = (code: string) =>
new Project()
.createSourceFile("test.ts", code, { overwrite: true })
.getStatement(Node.isVariableStatement)
?.getDeclarationList()
.getDeclarations()[0]
.getInitializer()!;

const replaceWith = parseCode("const y = console.log(\"test\")");

doTest("const y = 1", sourceFile => sourceFile, traversal => {
const node = traversal.visitChildren();

if (ts.isNumericLiteral(node))
return replaceWith.compilerNode;

return node;
}, "const y = console.log(\"test\")");
});

it("should handle comments", () => {
doTest("// test\nclass C {}\n// test2\nclass B {}", sourceFile => sourceFile, traversal => {
const node = traversal.visitChildren();
Expand Down Expand Up @@ -2190,8 +2211,10 @@ class MyClass {
traversal.factory.createBlock([]),
);
});
expect(newNode.getText()).to.equal("function test() { }");
expect(sourceFile.getText()).to.equal("function test() { }");
expect(newNode.getText()).to.equal(`function test() {
}`);
expect(sourceFile.getText()).to.equal(`function test() {
}`);
expect(node.wasForgotten()).to.be.true;
});

Expand All @@ -2210,8 +2233,10 @@ class MyClass {
traversal.factory.createBlock([]),
);
});
expect(newNode.getText()).to.equal("function test() { }");
expect(sourceFile.getText()).to.equal("function test() { }");
expect(newNode.getText()).to.equal(`function test() {
}`);
expect(sourceFile.getText()).to.equal(`function test() {
}`);
expect(node.wasForgotten()).to.be.false;
expect(node === newNode).to.be.true;
});
Expand Down