Skip to content

Emit source positions for open/close braces #47924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
inlineSourceMap: compilerOptions.inlineSourceMap,
extendedDiagnostics: compilerOptions.extendedDiagnostics,
onlyPrintJsDocStyle: true,
omitBraceSourceMapPositions: true,
writeBundleFileInfo: !!bundleBuildInfo,
recordInternalSection: !!bundleBuildInfo,
relativeToBuildInfo,
Expand Down Expand Up @@ -1391,6 +1392,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
} = handlers;

var extendedDiagnostics = !!printerOptions.extendedDiagnostics;
var omitBraceSourcePositions = !!printerOptions.omitBraceSourceMapPositions;
var newLine = getNewLineCharacter(printerOptions);
var moduleKind = getEmitModuleKind(printerOptions);
var bundledHelpers = new Map<string, boolean>();
Expand Down Expand Up @@ -3578,7 +3580,18 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
decreaseIndent();
}
}
pos = writeTokenText(token, writer, pos);

// We don't emit source positions for most tokens as it tends to be quite noisy, however
// we need to emit source positions for open and close braces so that tools like istanbul
// can map branches for code coverage. However, we still omit brace source positions when
// the output is a declaration file.
if (!omitBraceSourcePositions && (token === SyntaxKind.OpenBraceToken || token === SyntaxKind.CloseBraceToken)) {
pos = writeToken(token, pos, writer, contextNode);
}
else {
pos = writeTokenText(token, writer, pos);
}

if (isSimilarNode && contextNode.end !== pos) {
const isJsxExprContext = contextNode.kind === SyntaxKind.JsxExpression;
emitTrailingCommentsOfPosition(pos, /*prefixSpace*/ !isJsxExprContext, /*forceNoNewline*/ isJsxExprContext);
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9458,6 +9458,7 @@ export interface PrinterOptions {
/** @internal */ sourceMap?: boolean;
/** @internal */ inlineSourceMap?: boolean;
/** @internal */ inlineSources?: boolean;
/** @internal*/ omitBraceSourceMapPositions?: boolean;
/** @internal */ extendedDiagnostics?: boolean;
/** @internal */ onlyPrintJsDocStyle?: boolean;
/** @internal */ neverAsciiEscape?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-of1.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions tests/baselines/reference/ES5For-of1.sourcemap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sourceFile:ES5For-of1.ts
14> ^^
15> ^^^^
16> ^^
17> ^
1 >
2 >for (var v of
3 > ['a', 'b', 'c']
Expand All @@ -41,6 +42,7 @@ sourceFile:ES5For-of1.ts
14>
15> ['a', 'b', 'c']
16> )
17> {
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
2 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
3 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
Expand All @@ -57,6 +59,7 @@ sourceFile:ES5For-of1.ts
14>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
15>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
16>Emitted(1, 62) Source(1, 32) + SourceIndex(0)
17>Emitted(1, 63) Source(1, 33) + SourceIndex(0)
---
>>> var v = _a[_i];
1 >^^^^
Expand Down Expand Up @@ -101,10 +104,13 @@ sourceFile:ES5For-of1.ts
8 >Emitted(3, 20) Source(2, 20) + SourceIndex(0)
---
>>>}
1 >^
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >^
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
>}
1 >Emitted(4, 2) Source(3, 2) + SourceIndex(0)
>
2 >}
1 >Emitted(4, 1) Source(3, 1) + SourceIndex(0)
2 >Emitted(4, 2) Source(3, 2) + SourceIndex(0)
---
>>>//# sourceMappingURL=ES5For-of1.js.map
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-of13.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions tests/baselines/reference/ES5For-of13.sourcemap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sourceFile:ES5For-of13.ts
14> ^^
15> ^^^^
16> ^^
17> ^
1 >
2 >for (let v of
3 > ['a', 'b', 'c']
Expand All @@ -41,6 +42,7 @@ sourceFile:ES5For-of13.ts
14>
15> ['a', 'b', 'c']
16> )
17> {
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
2 >Emitted(1, 6) Source(1, 15) + SourceIndex(0)
3 >Emitted(1, 16) Source(1, 30) + SourceIndex(0)
Expand All @@ -57,6 +59,7 @@ sourceFile:ES5For-of13.ts
14>Emitted(1, 56) Source(1, 15) + SourceIndex(0)
15>Emitted(1, 60) Source(1, 30) + SourceIndex(0)
16>Emitted(1, 62) Source(1, 32) + SourceIndex(0)
17>Emitted(1, 63) Source(1, 33) + SourceIndex(0)
---
>>> var v = _a[_i];
1 >^^^^
Expand Down Expand Up @@ -94,10 +97,13 @@ sourceFile:ES5For-of13.ts
6 >Emitted(3, 15) Source(2, 15) + SourceIndex(0)
---
>>>}
1 >^
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >^
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
>}
1 >Emitted(4, 2) Source(3, 2) + SourceIndex(0)
>
2 >}
1 >Emitted(4, 1) Source(3, 1) + SourceIndex(0)
2 >Emitted(4, 2) Source(3, 2) + SourceIndex(0)
---
>>>//# sourceMappingURL=ES5For-of13.js.map
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-of25.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions tests/baselines/reference/ES5For-of25.sourcemap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sourceFile:ES5For-of25.ts
8 > ^^
9 > ^^^^
10> ^^
11> ^
1->
>
2 >for (var v of
Expand All @@ -69,6 +70,7 @@ sourceFile:ES5For-of25.ts
8 >
9 > a
10> )
11> {
1->Emitted(2, 1) Source(2, 1) + SourceIndex(0)
2 >Emitted(2, 6) Source(2, 15) + SourceIndex(0)
3 >Emitted(2, 16) Source(2, 16) + SourceIndex(0)
Expand All @@ -79,6 +81,7 @@ sourceFile:ES5For-of25.ts
8 >Emitted(2, 44) Source(2, 15) + SourceIndex(0)
9 >Emitted(2, 48) Source(2, 16) + SourceIndex(0)
10>Emitted(2, 50) Source(2, 18) + SourceIndex(0)
11>Emitted(2, 51) Source(2, 19) + SourceIndex(0)
---
>>> var v = a_1[_i];
1 >^^^^
Expand Down Expand Up @@ -120,10 +123,13 @@ sourceFile:ES5For-of25.ts
3 >Emitted(5, 7) Source(4, 7) + SourceIndex(0)
---
>>>}
1 >^
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >^
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
>}
1 >Emitted(6, 2) Source(5, 2) + SourceIndex(0)
>
2 >}
1 >Emitted(6, 1) Source(5, 1) + SourceIndex(0)
2 >Emitted(6, 2) Source(5, 2) + SourceIndex(0)
---
>>>//# sourceMappingURL=ES5For-of25.js.map
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-of26.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions tests/baselines/reference/ES5For-of26.sourcemap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ sourceFile:ES5For-of26.ts
12> ^^
13> ^^^^
14> ^^
15> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
15> ^
16> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >for (var [a = 0, b = 1] of
3 > [2, 3]
Expand All @@ -38,6 +39,7 @@ sourceFile:ES5For-of26.ts
12>
13> [2, 3]
14> )
15> {
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
2 >Emitted(1, 6) Source(1, 28) + SourceIndex(0)
3 >Emitted(1, 16) Source(1, 34) + SourceIndex(0)
Expand All @@ -52,6 +54,7 @@ sourceFile:ES5For-of26.ts
12>Emitted(1, 47) Source(1, 28) + SourceIndex(0)
13>Emitted(1, 51) Source(1, 34) + SourceIndex(0)
14>Emitted(1, 53) Source(1, 36) + SourceIndex(0)
15>Emitted(1, 54) Source(1, 37) + SourceIndex(0)
---
>>> var _b = _a[_i], _c = _b[0], a = _c === void 0 ? 0 : _c, _d = _b[1], b = _d === void 0 ? 1 : _d;
1->^^^^
Expand Down Expand Up @@ -132,10 +135,13 @@ sourceFile:ES5For-of26.ts
3 >Emitted(4, 7) Source(3, 7) + SourceIndex(0)
---
>>>}
1 >^
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
2 >^
3 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1 >
>}
1 >Emitted(5, 2) Source(4, 2) + SourceIndex(0)
>
2 >}
1 >Emitted(5, 1) Source(4, 1) + SourceIndex(0)
2 >Emitted(5, 2) Source(4, 2) + SourceIndex(0)
---
>>>//# sourceMappingURL=ES5For-of26.js.map
Loading