Skip to content

fix(39022): If exported variable is renamed, tsc will generate code with a syntax error #41156

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 1 commit into from
Feb 4, 2021
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
3 changes: 1 addition & 2 deletions src/compiler/transformers/module/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1874,9 +1874,8 @@ namespace ts {
for (const exportName of exportedNames) {
// Mark the node to prevent triggering this rule again.
noSubstitution[getNodeId(expression)] = true;
expression = createExportExpression(exportName, expression);
expression = factory.createParenthesizedExpression(createExportExpression(exportName, expression));
}

return expression;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ exports.buzz = buzz;
exports.buzz = buzz += 3;
var bizz = 8;
exports.bizz = bizz;
exports.bizz = bizz += 1; // compiles to exports.bizz = bizz += 1
exports.bizz = bizz -= 1; // similarly
exports.bizz = ++bizz; // compiles to exports.bizz = ++bizz
(exports.bizz = bizz += 1); // compiles to exports.bizz = bizz += 1
(exports.bizz = bizz -= 1); // similarly
(exports.bizz = ++bizz); // compiles to exports.bizz = ++bizz
39 changes: 39 additions & 0 deletions tests/baselines/reference/moduleExportsUnaryExpression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//// [moduleExportsUnaryExpression.ts]
let x = 1;

export function foo(y: number) {
if (y <= x++) return y <= x++;
if (y <= x--) return y <= x--;
if (y <= ++x) return y <= ++x;
if (y <= --x) return y <= --x;

x++;
x--;
++x;
--x;
}

export { x };


//// [moduleExportsUnaryExpression.js]
"use strict";
exports.__esModule = true;
exports.x = exports.foo = void 0;
var x = 1;
exports.x = x;
function foo(y) {
if (y <= (exports.x = x += 1))
return y <= (exports.x = x += 1);
if (y <= (exports.x = x -= 1))
return y <= (exports.x = x -= 1);
if (y <= (exports.x = ++x))
return y <= (exports.x = ++x);
if (y <= (exports.x = --x))
return y <= (exports.x = --x);
(exports.x = x += 1);
(exports.x = x -= 1);
(exports.x = ++x);
(exports.x = --x);
}
exports.foo = foo;
48 changes: 48 additions & 0 deletions tests/baselines/reference/moduleExportsUnaryExpression.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
=== tests/cases/compiler/moduleExportsUnaryExpression.ts ===
let x = 1;
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

export function foo(y: number) {
>foo : Symbol(foo, Decl(moduleExportsUnaryExpression.ts, 0, 10))
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))

if (y <= x++) return y <= x++;
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

if (y <= x--) return y <= x--;
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

if (y <= ++x) return y <= ++x;
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

if (y <= --x) return y <= --x;
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))
>y : Symbol(y, Decl(moduleExportsUnaryExpression.ts, 2, 20))
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

x++;
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

x--;
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

++x;
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))

--x;
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 0, 3))
}

export { x };
>x : Symbol(x, Decl(moduleExportsUnaryExpression.ts, 14, 8))

69 changes: 69 additions & 0 deletions tests/baselines/reference/moduleExportsUnaryExpression.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
=== tests/cases/compiler/moduleExportsUnaryExpression.ts ===
let x = 1;
>x : number
>1 : 1

export function foo(y: number) {
>foo : (y: number) => boolean
>y : number

if (y <= x++) return y <= x++;
>y <= x++ : boolean
>y : number
>x++ : number
>x : number
>y <= x++ : boolean
>y : number
>x++ : number
>x : number

if (y <= x--) return y <= x--;
>y <= x-- : boolean
>y : number
>x-- : number
>x : number
>y <= x-- : boolean
>y : number
>x-- : number
>x : number

if (y <= ++x) return y <= ++x;
>y <= ++x : boolean
>y : number
>++x : number
>x : number
>y <= ++x : boolean
>y : number
>++x : number
>x : number

if (y <= --x) return y <= --x;
>y <= --x : boolean
>y : number
>--x : number
>x : number
>y <= --x : boolean
>y : number
>--x : number
>x : number

x++;
>x++ : number
>x : number

x--;
>x-- : number
>x : number

++x;
>++x : number
>x : number

--x;
>--x : number
>x : number
}

export { x };
>x : number

16 changes: 16 additions & 0 deletions tests/cases/compiler/moduleExportsUnaryExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @module: commonjs
let x = 1;

export function foo(y: number) {
if (y <= x++) return y <= x++;
if (y <= x--) return y <= x--;
if (y <= ++x) return y <= ++x;
if (y <= --x) return y <= --x;

x++;
x--;
++x;
--x;
}

export { x };