From ef86f7da5072dac42923b7c8153bd1efbd9a980b Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 8 Jun 2017 14:15:40 -0700 Subject: [PATCH] Adjust source map offsets for variables in downlevel async funcs and generators --- src/compiler/transformer.ts | 2 +- src/compiler/transformers/generators.ts | 20 +++++--- ...rceMapValidationVarInDownLevelGenerator.js | 14 ++++++ ...apValidationVarInDownLevelGenerator.js.map | 2 + ...ationVarInDownLevelGenerator.sourcemap.txt | 47 +++++++++++++++++++ ...pValidationVarInDownLevelGenerator.symbols | 8 ++++ ...MapValidationVarInDownLevelGenerator.types | 9 ++++ ...rceMapValidationVarInDownLevelGenerator.ts | 7 +++ 8 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js create mode 100644 tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js.map create mode 100644 tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.sourcemap.txt create mode 100644 tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.symbols create mode 100644 tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.types create mode 100644 tests/cases/compiler/sourceMapValidationVarInDownLevelGenerator.ts diff --git a/src/compiler/transformer.ts b/src/compiler/transformer.ts index f0c827d8396..bfd3360e885 100644 --- a/src/compiler/transformer.ts +++ b/src/compiler/transformer.ts @@ -239,7 +239,7 @@ namespace ts { function hoistVariableDeclaration(name: Identifier): void { Debug.assert(state > TransformationState.Uninitialized, "Cannot modify the lexical environment during initialization."); Debug.assert(state < TransformationState.Completed, "Cannot modify the lexical environment after transformation has completed."); - const decl = createVariableDeclaration(name); + const decl = setEmitFlags(createVariableDeclaration(name), EmitFlags.NoNestedSourceMaps); if (!lexicalEnvironmentVariableDeclarations) { lexicalEnvironmentVariableDeclarations = [decl]; } diff --git a/src/compiler/transformers/generators.ts b/src/compiler/transformers/generators.ts index 9aadd6bc686..d0049e72f0b 100644 --- a/src/compiler/transformers/generators.ts +++ b/src/compiler/transformers/generators.ts @@ -640,10 +640,13 @@ namespace ts { return undefined; } - return createStatement( - inlineExpressions( - map(variables, transformInitializedVariable) - ) + return setSourceMapRange( + createStatement( + inlineExpressions( + map(variables, transformInitializedVariable) + ) + ), + node ); } } @@ -1281,9 +1284,12 @@ namespace ts { } function transformInitializedVariable(node: VariableDeclaration) { - return createAssignment( - getSynthesizedClone(node.name), - visitNode(node.initializer, visitor, isExpression) + return setSourceMapRange( + createAssignment( + setSourceMapRange(getSynthesizedClone(node.name), node.name), + visitNode(node.initializer, visitor, isExpression) + ), + node ); } diff --git a/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js new file mode 100644 index 00000000000..24a27fb919a --- /dev/null +++ b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js @@ -0,0 +1,14 @@ +//// [sourceMapValidationVarInDownLevelGenerator.ts] +function * f() { + var x = 1, y; +} + +//// [sourceMapValidationVarInDownLevelGenerator.js] +function f() { + var x, y; + return __generator(this, function (_a) { + x = 1; + return [2 /*return*/]; + }); +} +//# sourceMappingURL=sourceMapValidationVarInDownLevelGenerator.js.map \ No newline at end of file diff --git a/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js.map b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js.map new file mode 100644 index 00000000000..293f445a6fd --- /dev/null +++ b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.js.map @@ -0,0 +1,2 @@ +//// [sourceMapValidationVarInDownLevelGenerator.js.map] +{"version":3,"file":"sourceMapValidationVarInDownLevelGenerator.js","sourceRoot":"","sources":["sourceMapValidationVarInDownLevelGenerator.ts"],"names":[],"mappings":"AAAA;;;QACQ,CAAC,GAAG,CAAC,CAAI;;;CAChB"} \ No newline at end of file diff --git a/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.sourcemap.txt b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.sourcemap.txt new file mode 100644 index 00000000000..8fe7af32307 --- /dev/null +++ b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.sourcemap.txt @@ -0,0 +1,47 @@ +=================================================================== +JsFile: sourceMapValidationVarInDownLevelGenerator.js +mapUrl: sourceMapValidationVarInDownLevelGenerator.js.map +sourceRoot: +sources: sourceMapValidationVarInDownLevelGenerator.ts +=================================================================== +------------------------------------------------------------------- +emittedFile:tests/cases/compiler/sourceMapValidationVarInDownLevelGenerator.js +sourceFile:sourceMapValidationVarInDownLevelGenerator.ts +------------------------------------------------------------------- +>>>function f() { +1 > +2 >^^^^^^^^^^^^^^-> +1 > +1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0) +--- +>>> var x, y; +>>> return __generator(this, function (_a) { +>>> x = 1; +1->^^^^^^^^ +2 > ^ +3 > ^^^ +4 > ^ +5 > ^ +6 > ^^^^^^^^^^^^^^^^^-> +1->function * f() { + > var +2 > x +3 > = +4 > 1 +5 > , y; +1->Emitted(4, 9) Source(2, 9) + SourceIndex(0) +2 >Emitted(4, 10) Source(2, 10) + SourceIndex(0) +3 >Emitted(4, 13) Source(2, 13) + SourceIndex(0) +4 >Emitted(4, 14) Source(2, 14) + SourceIndex(0) +5 >Emitted(4, 15) Source(2, 18) + SourceIndex(0) +--- +>>> return [2 /*return*/]; +>>> }); +>>>} +1->^ +2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-> +1-> + >} +1->Emitted(7, 2) Source(3, 2) + SourceIndex(0) +--- +>>>//# sourceMappingURL=sourceMapValidationVarInDownLevelGenerator.js.map \ No newline at end of file diff --git a/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.symbols b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.symbols new file mode 100644 index 00000000000..99c9c4d714a --- /dev/null +++ b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/sourceMapValidationVarInDownLevelGenerator.ts === +function * f() { +>f : Symbol(f, Decl(sourceMapValidationVarInDownLevelGenerator.ts, 0, 0)) + + var x = 1, y; +>x : Symbol(x, Decl(sourceMapValidationVarInDownLevelGenerator.ts, 1, 7)) +>y : Symbol(y, Decl(sourceMapValidationVarInDownLevelGenerator.ts, 1, 14)) +} diff --git a/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.types b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.types new file mode 100644 index 00000000000..cac2f03d8e4 --- /dev/null +++ b/tests/baselines/reference/sourceMapValidationVarInDownLevelGenerator.types @@ -0,0 +1,9 @@ +=== tests/cases/compiler/sourceMapValidationVarInDownLevelGenerator.ts === +function * f() { +>f : () => IterableIterator + + var x = 1, y; +>x : number +>1 : 1 +>y : any +} diff --git a/tests/cases/compiler/sourceMapValidationVarInDownLevelGenerator.ts b/tests/cases/compiler/sourceMapValidationVarInDownLevelGenerator.ts new file mode 100644 index 00000000000..1a1bbd5a806 --- /dev/null +++ b/tests/cases/compiler/sourceMapValidationVarInDownLevelGenerator.ts @@ -0,0 +1,7 @@ +// @sourcemap: true +// @downlevelIteration: true +// @noEmitHelpers: true +// @lib: es2015 +function * f() { + var x = 1, y; +} \ No newline at end of file