Skip to content

Commit

Permalink
fix: transform() - ensure comments on nodes with only added synthet…
Browse files Browse the repository at this point in the history
…ic leading comments show up in output

Closes #1273
  • Loading branch information
dsherret committed May 21, 2022
1 parent 163de40 commit abc840d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion deno/ts_morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -3710,7 +3710,7 @@ class Node {
return resultNode;
}
function handleTransformation(oldNode, newNode) {
if (oldNode === newNode)
if (oldNode === newNode && newNode.emitNode == null)
return;
const start = oldNode.getStart(compilerSourceFile, true);
const end = oldNode.end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* could cause the statements to be written multiple times.
* -----------------------------------------------------------
*/
import { Mixin, TsMorphInspector, WrappedNode } from "../inspectors.ts";
import { Mixin, TsMorphInspector, WrappedNode } from "../inspectors/mod.ts";
import { Problem } from "./Problem.ts";

export function ensureMixinNotAppliedMultipleTimes(inspector: TsMorphInspector, addProblem: (problem: Problem) => void) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* --------------------------------------------
*/
import { tsMorph } from "../deps.ts";
import { TsMorphInspector } from "../inspectors.ts";
import { TsMorphInspector } from "../inspectors/mod.ts";
import { Problem } from "./Problem.ts";

export function ensureOrThrowExists(inspector: TsMorphInspector, addProblem: (problem: Problem) => void) {
Expand Down
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 @@ -1529,7 +1529,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
}

function handleTransformation(oldNode: ts.Node, newNode: ts.Node) {
if (oldNode === newNode)
if (oldNode === newNode && (newNode as any).emitNode == null)
return;

const start = oldNode.getStart(compilerSourceFile, true);
Expand Down
19 changes: 19 additions & 0 deletions packages/ts-morph/src/tests/issues/1273tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ts } from "@ts-morph/common";
import { expect } from "chai";
import { Project } from "../../Project";

describe("tests for issue #1227", () => {
it("should find referencing source files", () => {
const project = new Project({ useInMemoryFileSystem: true, compilerOptions: { removeComments: false } });

const file = project.createSourceFile("x.ts", `const x: number = 5 * 9;`);
file.transform(traversal => {
const node = traversal.visitChildren();
if (node.getChildCount() === 0)
return ts.addSyntheticLeadingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, "A", false);
return node;
});

expect(file.getFullText()).to.equal(`const /*A*/ x: /*A*/ number = /*A*/ 5 * /*A*/ 9;\n`);
});
});

0 comments on commit abc840d

Please sign in to comment.