Skip to content

Commit 6fcc815

Browse files
Check if the expression had any trailing comments.
1 parent 87e45f9 commit 6fcc815

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/compiler/emitter.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1651,11 +1651,17 @@ namespace ts {
16511651
function emitPropertyAccessExpression(node: PropertyAccessExpression) {
16521652
let indentBeforeDot = false;
16531653
let indentAfterDot = false;
1654+
const dotRangeFirstCommentStart = skipTrivia(
1655+
currentSourceFile!.text,
1656+
node.expression.end,
1657+
/*stopAfterLineBreak*/ false,
1658+
/*stopAtComments*/ true
1659+
);
1660+
const dotRangeStart = skipTrivia(currentSourceFile!.text, dotRangeFirstCommentStart);
1661+
const dotRangeEnd = dotRangeStart + 1;
16541662
if (!(getEmitFlags(node) & EmitFlags.NoIndentation)) {
1655-
const dotRangeStart = node.expression.end;
1656-
const dotRangeEnd = skipTrivia(currentSourceFile!.text, node.expression.end) + 1;
16571663
const dotToken = createToken(SyntaxKind.DotToken);
1658-
dotToken.pos = dotRangeStart;
1664+
dotToken.pos = node.expression.end;
16591665
dotToken.end = dotRangeEnd;
16601666
indentBeforeDot = needsIndentation(node, node.expression, dotToken);
16611667
indentAfterDot = needsIndentation(node, dotToken, node.name);
@@ -1664,7 +1670,8 @@ namespace ts {
16641670
emitExpression(node.expression);
16651671
increaseIndentIf(indentBeforeDot, /*writeSpaceIfNotIndenting*/ false);
16661672

1667-
const shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression);
1673+
const dotHasCommentTrivia = dotRangeFirstCommentStart !== dotRangeStart;
1674+
const shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression, dotHasCommentTrivia);
16681675
if (shouldEmitDotDot) {
16691676
writePunctuation(".");
16701677
}
@@ -1677,13 +1684,15 @@ namespace ts {
16771684

16781685
// 1..toString is a valid property access, emit a dot after the literal
16791686
// Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal
1680-
function needsDotDotForPropertyAccess(expression: Expression) {
1687+
function needsDotDotForPropertyAccess(expression: Expression, dotHasTrivia: boolean) {
16811688
expression = skipPartiallyEmittedExpressions(expression);
16821689
if (isNumericLiteral(expression)) {
16831690
// check if numeric literal is a decimal literal that was originally written with a dot
16841691
const text = getLiteralTextOfNode(<LiteralExpression>expression, /*neverAsciiEscape*/ true);
1685-
return !expression.numericLiteralFlags
1686-
&& !stringContains(text, tokenToString(SyntaxKind.DotToken)!);
1692+
// If he number will be printed verbatim and it doesn't already contain a dot, add one
1693+
// if the expression doesn't have any comments that will be emitted.
1694+
return !expression.numericLiteralFlags && !stringContains(text, tokenToString(SyntaxKind.DotToken)!) &&
1695+
(!dotHasTrivia || printerOptions.removeComments);
16871696
}
16881697
else if (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) {
16891698
// check if constant enum value is integer

0 commit comments

Comments
 (0)