Reduce duplication of addSyntheticComment.

This commit is contained in:
Martin Probst 2018-07-12 15:56:03 +02:00
parent 6f114a2c9b
commit 1bd79af760
4 changed files with 28 additions and 53 deletions

View File

@ -268,33 +268,34 @@ namespace ts {
return fs.readFileSync("/.src/index.d.ts").toString();
}
function addSyntheticComment(nodeFilter: (node: Node) => boolean) {
return (context: TransformationContext) => {
return (sourceFile: SourceFile): SourceFile => {
return visitNode(sourceFile, rootTransform, isSourceFile);
};
function rootTransform<T extends Node>(node: T): VisitResult<T> {
if (nodeFilter(node)) {
setEmitFlags(node, EmitFlags.NoLeadingComments);
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "comment", pos: -1, end: -1, hasTrailingNewLine: true }]);
}
return visitEachChild(node, rootTransform, context);
}
};
}
// https://github.com/Microsoft/TypeScript/issues/24096
testBaseline("transformAddCommentToArrowReturnValue", () => {
return transpileModule(`const foo = () =>
void 0
`, {
transformers: {
before: [addSyntheticComment],
before: [addSyntheticComment(isVoidExpression)],
},
compilerOptions: {
target: ScriptTarget.ES5,
newLine: NewLineKind.CarriageReturnLineFeed,
}
}).outputText;
function addSyntheticComment(context: TransformationContext) {
return (sourceFile: SourceFile): SourceFile => {
return visitNode(sourceFile, rootTransform, isSourceFile);
};
function rootTransform<T extends Node>(node: T): VisitResult<T> {
if (isVoidExpression(node)) {
setEmitFlags(node, EmitFlags.NoLeadingComments);
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.SingleLineCommentTrivia, text: "// comment!", pos: -1, end: -1, hasTrailingNewLine: true }]);
return node;
}
return visitEachChild(node, rootTransform, context);
}
}
});
// https://github.com/Microsoft/TypeScript/issues/17594
@ -304,27 +305,13 @@ const exportedSeparately = 2;
export {exportedSeparately};
`, {
transformers: {
before: [addSyntheticComment],
before: [addSyntheticComment(isVariableStatement)],
},
compilerOptions: {
target: ScriptTarget.ES5,
newLine: NewLineKind.CarriageReturnLineFeed,
}
}).outputText;
function addSyntheticComment(context: TransformationContext) {
return (sourceFile: SourceFile): SourceFile => {
return visitNode(sourceFile, rootTransform, isSourceFile);
};
function rootTransform<T extends Node>(node: T): VisitResult<T> {
if (isVariableStatement(node)) {
setEmitFlags(node, EmitFlags.NoLeadingComments);
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: "* @type {number} ", pos: -1, end: -1, hasTrailingNewLine: true }]);
return node;
}
return visitEachChild(node, rootTransform, context);
}
}
});
// https://github.com/Microsoft/TypeScript/issues/17594
@ -339,26 +326,14 @@ export * from 'somewhere';
export {Value};
`, {
transformers: {
before: [addSyntheticComment],
before: [addSyntheticComment(n => isImportDeclaration(n) || isExportDeclaration(n) || isImportSpecifier(n) || isExportSpecifier(n))],
},
compilerOptions: {
target: ScriptTarget.ES5,
newLine: NewLineKind.CarriageReturnLineFeed,
}
}).outputText;
function addSyntheticComment(context: TransformationContext) {
return (sourceFile: SourceFile): SourceFile => {
return visitNode(sourceFile, rootTransform, isSourceFile);
};
function rootTransform<T extends Node>(node: T): VisitResult<T> {
if (isImportDeclaration(node) || isExportDeclaration(node) || isImportSpecifier(node) || isExportSpecifier(node)) {
setEmitFlags(node, EmitFlags.NoLeadingComments);
setSyntheticLeadingComments(node, [{ kind: SyntaxKind.MultiLineCommentTrivia, text: `comment!`, pos: -1, end: -1, hasTrailingNewLine: true }]);
}
return visitEachChild(node, rootTransform, context);
}
}
}); });
});
});
}

View File

@ -1,4 +1,4 @@
var foo = function () {
//// comment!
/*comment*/
return void 0;
};

View File

@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/** @type {number} */
/*comment*/
exports.exportedDirectly = 1;
/** @type {number} */
/*comment*/
var exportedSeparately = 2;
exports.exportedSeparately = exportedSeparately;

View File

@ -3,14 +3,14 @@ function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
/*comment!*/
/*comment*/
var somewhere_1 = require("somewhere");
exports.Value = somewhere_1.Value;
/*comment!*/
/*comment*/
var somewhere_2 = require("somewhere");
/*comment!*/
/*comment*/
exports.X = somewhere_2.X;
/*comment!*/
/*comment*/
exports.Y = somewhere_2.Y;
/*comment!*/
/*comment*/
__export(require("somewhere"));