Skip to content

Commit 1bd79af

Browse files
committed
Reduce duplication of addSyntheticComment.
1 parent 6f114a2 commit 1bd79af

File tree

4 files changed

+28
-53
lines changed

4 files changed

+28
-53
lines changed

src/testRunner/unittests/transform.ts

+20-45
Original file line numberDiff line numberDiff line change
@@ -268,33 +268,34 @@ namespace ts {
268268
return fs.readFileSync("/.src/index.d.ts").toString();
269269
}
270270

271+
function addSyntheticComment(nodeFilter: (node: Node) => boolean) {
272+
return (context: TransformationContext) => {
273+
return (sourceFile: SourceFile): SourceFile => {
274+
return visitNode(sourceFile, rootTransform, isSourceFile);
275+
};
276+
function rootTransform<T extends Node>(node: T): VisitResult<T> {
277+
if (nodeFilter(node)) {
278+
setEmitFlags(node, EmitFlags.NoLeadingComments);
279+
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "comment", pos: -1, end: -1, hasTrailingNewLine: true }]);
280+
}
281+
return visitEachChild(node, rootTransform, context);
282+
}
283+
};
284+
}
285+
271286
// https://github.com/Microsoft/TypeScript/issues/24096
272287
testBaseline("transformAddCommentToArrowReturnValue", () => {
273288
return transpileModule(`const foo = () =>
274289
void 0
275290
`, {
276291
transformers: {
277-
before: [addSyntheticComment],
292+
before: [addSyntheticComment(isVoidExpression)],
278293
},
279294
compilerOptions: {
280295
target: ScriptTarget.ES5,
281296
newLine: NewLineKind.CarriageReturnLineFeed,
282297
}
283298
}).outputText;
284-
285-
function addSyntheticComment(context: TransformationContext) {
286-
return (sourceFile: SourceFile): SourceFile => {
287-
return visitNode(sourceFile, rootTransform, isSourceFile);
288-
};
289-
function rootTransform<T extends Node>(node: T): VisitResult<T> {
290-
if (isVoidExpression(node)) {
291-
setEmitFlags(node, EmitFlags.NoLeadingComments);
292-
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.SingleLineCommentTrivia, text: "// comment!", pos: -1, end: -1, hasTrailingNewLine: true }]);
293-
return node;
294-
}
295-
return visitEachChild(node, rootTransform, context);
296-
}
297-
}
298299
});
299300

300301
// https://github.com/Microsoft/TypeScript/issues/17594
@@ -304,27 +305,13 @@ const exportedSeparately = 2;
304305
export {exportedSeparately};
305306
`, {
306307
transformers: {
307-
before: [addSyntheticComment],
308+
before: [addSyntheticComment(isVariableStatement)],
308309
},
309310
compilerOptions: {
310311
target: ScriptTarget.ES5,
311312
newLine: NewLineKind.CarriageReturnLineFeed,
312313
}
313314
}).outputText;
314-
315-
function addSyntheticComment(context: TransformationContext) {
316-
return (sourceFile: SourceFile): SourceFile => {
317-
return visitNode(sourceFile, rootTransform, isSourceFile);
318-
};
319-
function rootTransform<T extends Node>(node: T): VisitResult<T> {
320-
if (isVariableStatement(node)) {
321-
setEmitFlags(node, EmitFlags.NoLeadingComments);
322-
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "* @type {number} ", pos: -1, end: -1, hasTrailingNewLine: true }]);
323-
return node;
324-
}
325-
return visitEachChild(node, rootTransform, context);
326-
}
327-
}
328315
});
329316

330317
// https://github.com/Microsoft/TypeScript/issues/17594
@@ -339,26 +326,14 @@ export * from 'somewhere';
339326
export {Value};
340327
`, {
341328
transformers: {
342-
before: [addSyntheticComment],
329+
before: [addSyntheticComment(n => isImportDeclaration(n) || isExportDeclaration(n) || isImportSpecifier(n) || isExportSpecifier(n))],
343330
},
344331
compilerOptions: {
345332
target: ScriptTarget.ES5,
346333
newLine: NewLineKind.CarriageReturnLineFeed,
347334
}
348335
}).outputText;
349-
350-
function addSyntheticComment(context: TransformationContext) {
351-
return (sourceFile: SourceFile): SourceFile => {
352-
return visitNode(sourceFile, rootTransform, isSourceFile);
353-
};
354-
function rootTransform<T extends Node>(node: T): VisitResult<T> {
355-
if (isImportDeclaration(node) || isExportDeclaration(node) || isImportSpecifier(node) || isExportSpecifier(node)) {
356-
setEmitFlags(node, EmitFlags.NoLeadingComments);
357-
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: `comment!`, pos: -1, end: -1, hasTrailingNewLine: true }]);
358-
}
359-
return visitEachChild(node, rootTransform, context);
360-
}
361-
}
362-
}); });
336+
});
337+
});
363338
}
364339

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var foo = function () {
2-
//// comment!
2+
/*comment*/
33
return void 0;
44
};
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
/** @type {number} */
3+
/*comment*/
44
exports.exportedDirectly = 1;
5-
/** @type {number} */
5+
/*comment*/
66
var exportedSeparately = 2;
77
exports.exportedSeparately = exportedSeparately;

tests/baselines/reference/transformApi/transformsCorrectly.transformAddCommentToImport.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ function __export(m) {
33
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
44
}
55
Object.defineProperty(exports, "__esModule", { value: true });
6-
/*comment!*/
6+
/*comment*/
77
var somewhere_1 = require("somewhere");
88
exports.Value = somewhere_1.Value;
9-
/*comment!*/
9+
/*comment*/
1010
var somewhere_2 = require("somewhere");
11-
/*comment!*/
11+
/*comment*/
1212
exports.X = somewhere_2.X;
13-
/*comment!*/
13+
/*comment*/
1414
exports.Y = somewhere_2.Y;
15-
/*comment!*/
15+
/*comment*/
1616
__export(require("somewhere"));

0 commit comments

Comments
 (0)