Skip to content

Commit 1c760ef

Browse files
committed
Minor update to source map emit and fix for positions
1 parent 9b7270f commit 1c760ef

12 files changed

Lines changed: 83 additions & 71 deletions

File tree

src/compiler/emitter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3904,7 +3904,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
39043904
write(" = ");
39053905
emit(value);
39063906
});
3907-
emitEnd(nodeForSourceMap, /*stopOverridingSpan*/true);
3907+
emitEnd(nodeForSourceMap);
3908+
sourceMap.stopOverridingSpan();
39083909

39093910
if (exportChanged) {
39103911
write(")");

src/compiler/factory.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ namespace ts {
102102
return clone;
103103
}
104104

105+
/**
106+
* Gets a clone of a node with a unique node ID.
107+
*/
108+
export function getUniqueClone<T extends Node>(node: T): T {
109+
const clone = getSynthesizedClone(node);
110+
clone.id = undefined;
111+
getNodeId(clone);
112+
return clone;
113+
}
114+
105115
// Literals
106116

107117
export function createLiteral(value: string, location?: TextRange): StringLiteral;

src/compiler/printer.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,21 @@ const _super = (function (geti, seti) {
298298
const leadingComments = getLeadingComments(node, getNotEmittedParent);
299299
const trailingComments = getTrailingComments(node, getNotEmittedParent);
300300
emitLeadingComments(node, leadingComments);
301-
emitStart(node);
301+
emitStart(node, shouldEmitSourceMap, shouldEmitNestedSourceMap);
302302
emitWorker(node);
303-
emitEnd(node);
303+
emitEnd(node, shouldEmitSourceMap, shouldEmitNestedSourceMap);
304304
emitTrailingComments(node, trailingComments);
305305
}
306306
}
307307

308+
function shouldEmitSourceMap(node: Node) {
309+
return (getNodeEmitFlags(node) & NodeEmitFlags.NoSourceMap) === 0;
310+
}
311+
312+
function shouldEmitNestedSourceMap(node: Node) {
313+
return (getNodeEmitFlags(node) & NodeEmitFlags.NoNestedSourceMaps) === 0;
314+
}
315+
308316
function emitWorker(node: Node): void {
309317
const kind = node.kind;
310318
switch (kind) {
@@ -1151,10 +1159,8 @@ const _super = (function (geti, seti) {
11511159
}
11521160

11531161
function emitExpressionWithTypeArguments(node: ExpressionWithTypeArguments) {
1154-
emitStart(node);
11551162
emitExpression(node.expression);
11561163
emitTypeArguments(node, node.typeArguments);
1157-
emitEnd(node);
11581164
}
11591165

11601166
function emitAsExpression(node: AsExpression) {
@@ -1601,21 +1607,17 @@ const _super = (function (geti, seti) {
16011607
}
16021608

16031609
function emitImportClause(node: ImportClause) {
1604-
emitStart(node);
16051610
emit(node.name);
16061611
if (node.name && node.namedBindings) {
16071612
write(", ");
16081613
}
16091614
emit(node.namedBindings);
1610-
emitEnd(node);
16111615
write(" from ");
16121616
}
16131617

16141618
function emitNamespaceImport(node: NamespaceImport) {
1615-
emitStart(node);
16161619
write("* as ");
16171620
emit(node.name);
1618-
emitEnd(node);
16191621
}
16201622

16211623
function emitNamedImports(node: NamedImports) {
@@ -1759,12 +1761,10 @@ const _super = (function (geti, seti) {
17591761
}
17601762

17611763
function emitHeritageClause(node: HeritageClause) {
1762-
emitStart(node);
17631764
write(" ");
17641765
writeToken(node.token);
17651766
write(" ");
17661767
emitList(node, node.types, ListFormat.HeritageClauseTypes);
1767-
emitEnd(node);
17681768
}
17691769

17701770
function emitCatchClause(node: CatchClause) {
@@ -2207,9 +2207,9 @@ const _super = (function (geti, seti) {
22072207

22082208
function writeTokenNode(node: Node) {
22092209
if (node) {
2210-
emitStart(node);
2210+
emitStart(node, shouldEmitSourceMap, shouldEmitNestedSourceMap);
22112211
writeTokenText(node.kind);
2212-
emitEnd(node);
2212+
emitEnd(node, shouldEmitSourceMap, shouldEmitNestedSourceMap);
22132213
}
22142214
}
22152215

src/compiler/sourcemap.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ namespace ts {
66
getSourceMapData(): SourceMapData;
77
setSourceFile(sourceFile: SourceFile): void;
88
emitPos(pos: number): void;
9-
emitStart(range: TextRange): void;
10-
emitEnd(range: TextRange): void;
11-
/*@deprecated*/ emitEnd(range: TextRange, stopOverridingSpan: boolean): void;
9+
emitStart(range: TextRange, shouldEmit?: (range: TextRange) => boolean, shouldEmitNested?: (range: TextRange) => boolean): void;
10+
emitEnd(range: TextRange, shouldEmit?: (range: TextRange) => boolean, shouldEmitNested?: (range: TextRange) => boolean): void;
1211
/*@deprecated*/ changeEmitSourcePos(): void;
12+
/*@deprecated*/ stopOverridingSpan(): void;
1313
getText(): string;
1414
getSourceMappingURL(): string;
1515
initialize(filePath: string, sourceMapFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean): void;
@@ -34,9 +34,10 @@ namespace ts {
3434
getSourceMapData(): SourceMapData { return undefined; },
3535
setSourceFile(sourceFile: SourceFile): void { },
3636
emitStart(range: TextRange): void { },
37-
emitEnd(range: TextRange, stopOverridingSpan?: boolean): void { },
37+
emitEnd(range: TextRange, shouldEmit?: Function, shouldEmitNested?: Function): void { },
3838
emitPos(pos: number): void { },
3939
changeEmitSourcePos(): void { },
40+
stopOverridingSpan(): void { },
4041
getText(): string { return undefined; },
4142
getSourceMappingURL(): string { return undefined; },
4243
initialize(filePath: string, sourceMapFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean): void { },
@@ -81,6 +82,7 @@ namespace ts {
8182
emitStart,
8283
emitEnd,
8384
changeEmitSourcePos,
85+
stopOverridingSpan: () => stopOverridingSpan = true,
8486
getText,
8587
getSourceMappingURL,
8688
initialize,
@@ -318,21 +320,26 @@ namespace ts {
318320
return range.pos !== -1 ? skipTrivia(currentSourceFile.text, rangeHasDecorators ? (range as Node).decorators.end : range.pos) : -1;
319321
}
320322

321-
function emitStart(range: TextRange) {
322-
emitPos(getStartPos(range));
323+
function emitStart(range: TextRange, shouldEmit?: (range: TextRange) => boolean, shouldEmitNested?: (range: TextRange) => boolean) {
324+
if (!shouldEmit || shouldEmit(range)) {
325+
emitPos(getStartPos(range));
326+
}
323327

324-
if (range.disableSourceMap) {
328+
if (shouldEmitNested && !shouldEmitNested(range)) {
325329
disable();
326330
}
327331
}
328332

329-
function emitEnd(range: TextRange, stopOverridingEnd?: boolean) {
330-
if (range.disableSourceMap) {
333+
function emitEnd(range: TextRange, shouldEmit?: (range: TextRange) => boolean, shouldEmitNested?: (range: TextRange) => boolean) {
334+
if (shouldEmitNested && !shouldEmitNested(range)) {
331335
enable();
332336
}
333337

334-
emitPos(range.end);
335-
stopOverridingSpan = stopOverridingEnd;
338+
if (!shouldEmit || shouldEmit(range)) {
339+
emitPos(range.end);
340+
}
341+
342+
stopOverridingSpan = false;
336343
}
337344

338345
function changeEmitSourcePos() {

src/compiler/transformers/destructuring.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace ts {
1313
* @param visitor An optional visitor to use to visit expressions.
1414
*/
1515
export function flattenDestructuringAssignment(
16+
context: TransformationContext,
1617
node: BinaryExpression,
1718
needsValue: boolean,
1819
recordTempVariable: (node: Identifier) => void,
@@ -44,7 +45,7 @@ namespace ts {
4445
location = node.right;
4546
}
4647

47-
flattenDestructuring(node, value, location, emitAssignment, emitTempVariableAssignment, visitor);
48+
flattenDestructuring(context, node, value, location, emitAssignment, emitTempVariableAssignment, visitor);
4849

4950
if (needsValue) {
5051
expressions.push(value);
@@ -57,7 +58,7 @@ namespace ts {
5758
function emitAssignment(name: Identifier, value: Expression, location: TextRange) {
5859
const expression = createAssignment(name, value, location);
5960
if (isSimpleExpression(value)) {
60-
expression.disableSourceMap = true;
61+
context.setNodeEmitFlags(expression, NodeEmitFlags.NoNestedSourceMaps);
6162
}
6263

6364
aggregateTransformFlags(expression);
@@ -79,17 +80,21 @@ namespace ts {
7980
* @param value The rhs value for the binding pattern.
8081
* @param visitor An optional visitor to use to visit expressions.
8182
*/
82-
export function flattenParameterDestructuring(node: ParameterDeclaration, value: Expression, visitor?: (node: Node) => VisitResult<Node>) {
83+
export function flattenParameterDestructuring(
84+
context: TransformationContext,
85+
node: ParameterDeclaration,
86+
value: Expression,
87+
visitor?: (node: Node) => VisitResult<Node>) {
8388
const declarations: VariableDeclaration[] = [];
8489

85-
flattenDestructuring(node, value, node, emitAssignment, emitTempVariableAssignment, visitor);
90+
flattenDestructuring(context, node, value, node, emitAssignment, emitTempVariableAssignment, visitor);
8691

8792
return declarations;
8893

8994
function emitAssignment(name: Identifier, value: Expression, location: TextRange) {
9095
const declaration = createVariableDeclaration(name, value, location);
9196
if (isSimpleExpression(value)) {
92-
declaration.disableSourceMap = true;
97+
context.setNodeEmitFlags(declaration, NodeEmitFlags.NoNestedSourceMaps);
9398
}
9499

95100
aggregateTransformFlags(declaration);
@@ -110,10 +115,14 @@ namespace ts {
110115
* @param value An optional rhs value for the binding pattern.
111116
* @param visitor An optional visitor to use to visit expressions.
112117
*/
113-
export function flattenVariableDestructuring(node: VariableDeclaration, value?: Expression, visitor?: (node: Node) => VisitResult<Node>) {
118+
export function flattenVariableDestructuring(
119+
context: TransformationContext,
120+
node: VariableDeclaration,
121+
value?: Expression,
122+
visitor?: (node: Node) => VisitResult<Node>) {
114123
const declarations: VariableDeclaration[] = [];
115124

116-
flattenDestructuring(node, value, node, emitAssignment, emitTempVariableAssignment, visitor);
125+
flattenDestructuring(context, node, value, node, emitAssignment, emitTempVariableAssignment, visitor);
117126

118127
return declarations;
119128

@@ -124,7 +133,7 @@ namespace ts {
124133
}
125134

126135
if (isSimpleExpression(value)) {
127-
declaration.disableSourceMap = true;
136+
context.setNodeEmitFlags(declaration, NodeEmitFlags.NoNestedSourceMaps);
128137
}
129138

130139
declaration.original = original;
@@ -148,14 +157,15 @@ namespace ts {
148157
* @param visitor An optional visitor to use to visit expressions.
149158
*/
150159
export function flattenVariableDestructuringToExpression(
160+
context: TransformationContext,
151161
node: VariableDeclaration,
152162
recordTempVariable: (name: Identifier) => void,
153163
nameSubstitution?: (name: Identifier) => Expression,
154164
visitor?: (node: Node) => VisitResult<Node>) {
155165

156166
const pendingAssignments: Expression[] = [];
157167

158-
flattenDestructuring(node, /*value*/ undefined, node, emitAssignment, emitTempVariableAssignment);
168+
flattenDestructuring(context, node, /*value*/ undefined, node, emitAssignment, emitTempVariableAssignment);
159169

160170
const expression = inlineExpressions(pendingAssignments);
161171
aggregateTransformFlags(expression);
@@ -176,7 +186,7 @@ namespace ts {
176186
function emitPendingAssignment(name: Expression, value: Expression, location: TextRange, original: Node) {
177187
const expression = createAssignment(name, value, location);
178188
if (isSimpleExpression(value)) {
179-
expression.disableSourceMap = true;
189+
context.setNodeEmitFlags(expression, NodeEmitFlags.NoNestedSourceMaps);
180190
}
181191

182192
expression.original = original;
@@ -186,6 +196,7 @@ namespace ts {
186196
}
187197

188198
function flattenDestructuring(
199+
context: TransformationContext,
189200
root: BindingElement | BinaryExpression,
190201
value: Expression,
191202
location: TextRange,

src/compiler/transformers/es6.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ namespace ts {
522522
createVariableStatement(
523523
/*modifiers*/ undefined,
524524
createVariableDeclarationList(
525-
flattenParameterDestructuring(parameter, temp, visitor)
525+
flattenParameterDestructuring(context, parameter, temp, visitor)
526526
)
527527
)
528528
);
@@ -964,7 +964,7 @@ namespace ts {
964964
function visitBinaryExpression(node: BinaryExpression, needsDestructuringValue: boolean): Expression {
965965
// If we are here it is because this is a destructuring assignment.
966966
Debug.assert(isDestructuringAssignment(node));
967-
return flattenDestructuringAssignment(node, needsDestructuringValue, hoistVariableDeclaration, visitor);
967+
return flattenDestructuringAssignment(context, node, needsDestructuringValue, hoistVariableDeclaration, visitor);
968968
}
969969

970970
/**
@@ -1090,7 +1090,7 @@ namespace ts {
10901090
// If we are here it is because the name contains a binding pattern.
10911091
Debug.assert(isBindingPattern(node.name));
10921092

1093-
return flattenVariableDestructuring(node, /*value*/ undefined, visitor);
1093+
return flattenVariableDestructuring(context, node, /*value*/ undefined, visitor);
10941094
}
10951095

10961096
function visitLabeledStatement(node: LabeledStatement) {
@@ -1173,6 +1173,7 @@ namespace ts {
11731173
/*modifiers*/ undefined,
11741174
createVariableDeclarationList(
11751175
flattenVariableDestructuring(
1176+
context,
11761177
firstDeclaration,
11771178
createElementAccess(rhsReference, counter),
11781179
visitor
@@ -1208,6 +1209,7 @@ namespace ts {
12081209
statements.push(
12091210
createStatement(
12101211
flattenDestructuringAssignment(
1212+
context,
12111213
assignment,
12121214
/*needsValue*/ false,
12131215
hoistVariableDeclaration,

src/compiler/transformers/module/module.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ namespace ts {
330330
const statements: Statement[] = [];
331331
if (moduleKind !== ModuleKind.AMD) {
332332
if (hasModifier(node, ModifierFlags.Export)) {
333-
addNode(statements,
333+
statements.push(
334334
createStatement(
335335
createExportAssignment(
336336
node.name,
@@ -341,7 +341,7 @@ namespace ts {
341341
);
342342
}
343343
else {
344-
addNode(statements,
344+
statements.push(
345345
createVariableStatement(
346346
/*modifiers*/ undefined,
347347
createVariableDeclarationList([
@@ -350,14 +350,17 @@ namespace ts {
350350
createRequireCall(node),
351351
/*location*/ node
352352
)
353-
])
353+
],
354+
/*location*/ undefined,
355+
/*flags*/ languageVersion >= ScriptTarget.ES6 ? NodeFlags.Const : NodeFlags.None),
356+
/*location*/ node
354357
)
355358
);
356359
}
357360
}
358361
else {
359362
if (hasModifier(node, ModifierFlags.Export)) {
360-
addNode(statements,
363+
statements.push(
361364
createStatement(
362365
createExportAssignment(node.name, node.name),
363366
/*location*/ node
@@ -544,6 +547,7 @@ namespace ts {
544547
const name = node.name;
545548
if (isBindingPattern(name)) {
546549
return flattenVariableDestructuringToExpression(
550+
context,
547551
node,
548552
hoistVariableDeclaration,
549553
getModuleMemberName,

0 commit comments

Comments
 (0)