Skip to content

Commit

Permalink
Merge pull request #25604 from mprobst/fix-exported-var-comments
Browse files Browse the repository at this point in the history
Retain synthetic comments on exports.
  • Loading branch information
mhegazy committed Jul 25, 2018
2 parents 23eb591 + 7d44014 commit fd2eb49
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 89 deletions.
8 changes: 5 additions & 3 deletions src/compiler/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ namespace ts {
const savedContainerEnd = containerEnd;
const savedDeclarationListContainerEnd = declarationListContainerEnd;

if (!skipLeadingComments) {
if (!skipLeadingComments || (pos >= 0 && (emitFlags & EmitFlags.NoLeadingComments) !== 0)) {
// Advance the container position of comments get emitted or if they've been disabled explicitly using NoLeadingComments.
containerPos = pos;
}

if (!skipTrailingComments) {
if (!skipTrailingComments || (end >= 0 && (emitFlags & EmitFlags.NoTrailingComments) !== 0)) {
// As above.
containerEnd = end;

// To avoid invalid comment emit in a down-level binding pattern, we
Expand Down Expand Up @@ -426,4 +428,4 @@ namespace ts {
return isRecognizedTripleSlashComment(currentText, commentPos, commentEnd);
}
}
}
}
5 changes: 2 additions & 3 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ namespace ts {
}

// Perform the capture.
captureThisForNode(statements, ctor, superCallExpression || createActualThis(), firstStatement);
captureThisForNode(statements, ctor, superCallExpression || createActualThis());

// If we're actually replacing the original statement, we need to signal this to the caller.
if (superCallExpression) {
Expand Down Expand Up @@ -1443,7 +1443,7 @@ namespace ts {
}
}

function captureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined, originalStatement?: Statement): void {
function captureThisForNode(statements: Statement[], node: Node, initializer: Expression | undefined): void {
enableSubstitutionsForCapturedThis();
const captureThisStatement = createVariableStatement(
/*modifiers*/ undefined,
Expand All @@ -1456,7 +1456,6 @@ namespace ts {
])
);
setEmitFlags(captureThisStatement, EmitFlags.NoComments | EmitFlags.CustomPrologue);
setTextRange(captureThisStatement, originalStatement);
setSourceMapRange(captureThisStatement, node);
statements.push(captureThisStatement);
}
Expand Down
142 changes: 79 additions & 63 deletions src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ namespace ts {
if (moduleKind !== ModuleKind.AMD) {
if (!node.importClause) {
// import "mod";
return setTextRange(createExpressionStatement(createRequireCall(node)), node);
return setOriginalNode(setTextRange(createExpressionStatement(createRequireCall(node)), node), node);
}
else {
const variables: VariableDeclaration[] = [];
Expand Down Expand Up @@ -806,15 +806,17 @@ namespace ts {
}

statements = append(statements,
setTextRange(
createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList(
variables,
languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
)
),
/*location*/ node
setOriginalNode(
setTextRange(
createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList(
variables,
languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
)
),
/*location*/ node),
/*original*/ node
)
);
}
Expand All @@ -826,13 +828,15 @@ namespace ts {
/*modifiers*/ undefined,
createVariableDeclarationList(
[
setTextRange(
createVariableDeclaration(
getSynthesizedClone(namespaceDeclaration.name),
/*type*/ undefined,
getGeneratedNameForNode(node)
),
/*location*/ node
setOriginalNode(
setTextRange(
createVariableDeclaration(
getSynthesizedClone(namespaceDeclaration.name),
/*type*/ undefined,
getGeneratedNameForNode(node)
),
/*location*/ node),
/*original*/ node
)
],
languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
Expand Down Expand Up @@ -880,33 +884,37 @@ namespace ts {
if (moduleKind !== ModuleKind.AMD) {
if (hasModifier(node, ModifierFlags.Export)) {
statements = append(statements,
setTextRange(
createExpressionStatement(
createExportExpression(
node.name,
createRequireCall(node)
)
),
setOriginalNode(
setTextRange(
createExpressionStatement(
createExportExpression(
node.name,
createRequireCall(node)
)
),
node),
node
)
);
}
else {
statements = append(statements,
setTextRange(
createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList(
[
createVariableDeclaration(
getSynthesizedClone(node.name),
/*type*/ undefined,
createRequireCall(node)
)
],
/*flags*/ languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
)
),
setOriginalNode(
setTextRange(
createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList(
[
createVariableDeclaration(
getSynthesizedClone(node.name),
/*type*/ undefined,
createRequireCall(node)
)
],
/*flags*/ languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None
)
),
node),
node
)
);
Expand All @@ -915,10 +923,12 @@ namespace ts {
else {
if (hasModifier(node, ModifierFlags.Export)) {
statements = append(statements,
setTextRange(
createExpressionStatement(
createExportExpression(getExportName(node), getLocalName(node))
),
setOriginalNode(
setTextRange(
createExpressionStatement(
createExportExpression(getExportName(node), getLocalName(node))
),
node),
node
)
);
Expand Down Expand Up @@ -956,18 +966,20 @@ namespace ts {
// export { x, y } from "mod";
if (moduleKind !== ModuleKind.AMD) {
statements.push(
setTextRange(
createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList([
createVariableDeclaration(
generatedName,
/*type*/ undefined,
createRequireCall(node)
)
])
),
/*location*/ node
setOriginalNode(
setTextRange(
createVariableStatement(
/*modifiers*/ undefined,
createVariableDeclarationList([
createVariableDeclaration(
generatedName,
/*type*/ undefined,
createRequireCall(node)
)
])
),
/*location*/ node),
/* original */ node
)
);
}
Expand All @@ -977,10 +989,12 @@ namespace ts {
specifier.propertyName || specifier.name
);
statements.push(
setTextRange(
createExpressionStatement(
createExportExpression(getExportName(specifier), exportedValue)
),
setOriginalNode(
setTextRange(
createExpressionStatement(
createExportExpression(getExportName(specifier), exportedValue)
),
specifier),
specifier
)
);
Expand All @@ -990,10 +1004,12 @@ namespace ts {
}
else {
// export * from "mod";
return setTextRange(
createExpressionStatement(
createExportStarHelper(context, moduleKind !== ModuleKind.AMD ? createRequireCall(node) : generatedName)
),
return setOriginalNode(
setTextRange(
createExpressionStatement(
createExportStarHelper(context, moduleKind !== ModuleKind.AMD ? createRequireCall(node) : generatedName)
),
node),
node
);
}
Expand Down Expand Up @@ -1140,7 +1156,7 @@ namespace ts {
}

if (expressions) {
statements = append(statements, setTextRange(createExpressionStatement(inlineExpressions(expressions)), node));
statements = append(statements, setOriginalNode(setTextRange(createExpressionStatement(inlineExpressions(expressions)), node), node));
}
}
else {
Expand Down
24 changes: 19 additions & 5 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,7 @@ namespace ts {
const statement = createExpressionStatement(transformInitializedProperty(property, receiver));
setSourceMapRange(statement, moveRangePastModifiers(property));
setCommentRange(statement, property);
setOriginalNode(statement, property);
statements.push(statement);
}
}
Expand All @@ -1262,6 +1263,7 @@ namespace ts {
startOnNewLine(expression);
setSourceMapRange(expression, moveRangePastModifiers(property));
setCommentRange(expression, property);
setOriginalNode(expression, property);
expressions.push(expression);
}

Expand Down Expand Up @@ -2631,7 +2633,8 @@ namespace ts {
// If needed, we should emit a variable declaration for the enum. If we emit
// a leading variable declaration, we should not emit leading comments for the
// enum body.
if (addVarForEnumOrModuleDeclaration(statements, node)) {
const varAdded = addVarForEnumOrModuleDeclaration(statements, node);
if (varAdded) {
// We should still emit the comments if we are emitting a system module.
if (moduleKind !== ModuleKind.System || currentLexicalScope !== currentSourceFile) {
emitFlags |= EmitFlags.NoLeadingComments;
Expand Down Expand Up @@ -2689,8 +2692,13 @@ namespace ts {
);

setOriginalNode(enumStatement, node);
if (varAdded) {
// If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
setSyntheticLeadingComments(enumStatement, undefined);
setSyntheticTrailingComments(enumStatement, undefined);
}
setTextRange(enumStatement, node);
setEmitFlags(enumStatement, emitFlags);
addEmitFlags(enumStatement, emitFlags);
statements.push(enumStatement);

// Add a DeclarationMarker for the enum to preserve trailing comments and mark
Expand Down Expand Up @@ -2880,7 +2888,7 @@ namespace ts {
// })(m1 || (m1 = {})); // trailing comment module
//
setCommentRange(statement, node);
setEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
addEmitFlags(statement, EmitFlags.NoTrailingComments | EmitFlags.HasEndOfDeclarationMarker);
statements.push(statement);
return true;
}
Expand Down Expand Up @@ -2920,7 +2928,8 @@ namespace ts {
// If needed, we should emit a variable declaration for the module. If we emit
// a leading variable declaration, we should not emit leading comments for the
// module body.
if (addVarForEnumOrModuleDeclaration(statements, node)) {
const varAdded = addVarForEnumOrModuleDeclaration(statements, node);
if (varAdded) {
// We should still emit the comments if we are emitting a system module.
if (moduleKind !== ModuleKind.System || currentLexicalScope !== currentSourceFile) {
emitFlags |= EmitFlags.NoLeadingComments;
Expand Down Expand Up @@ -2977,8 +2986,13 @@ namespace ts {
);

setOriginalNode(moduleStatement, node);
if (varAdded) {
// If a variable was added, synthetic comments are emitted on it, not on the moduleStatement.
setSyntheticLeadingComments(moduleStatement, undefined);
setSyntheticTrailingComments(moduleStatement, undefined);
}
setTextRange(moduleStatement, node);
setEmitFlags(moduleStatement, emitFlags);
addEmitFlags(moduleStatement, emitFlags);
statements.push(moduleStatement);

// Add a DeclarationMarker for the namespace to preserve trailing comments and mark
Expand Down

0 comments on commit fd2eb49

Please sign in to comment.