Skip to content

Commit 7fd602e

Browse files
committed
Fix microsoft#22866: Condition checking for name collision of generated variable names on emit happening
1 parent 3710218 commit 7fd602e

5 files changed

+303
-5
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21900,7 +21900,7 @@ namespace ts {
2190021900

2190121901
function checkCollisionWithArgumentsInGeneratedCode(node: SignatureDeclaration) {
2190221902
// no rest parameters \ declaration context \ overload - no codegen impact
21903-
if (!hasRestParameter(node) || node.flags & NodeFlags.Ambient || nodeIsMissing((<FunctionLikeDeclaration>node).body)) {
21903+
if (languageVersion >= ScriptTarget.ES2015 || compilerOptions.noEmit || !hasRestParameter(node) || node.flags & NodeFlags.Ambient || nodeIsMissing((<FunctionLikeDeclaration>node).body)) {
2190421904
return;
2190521905
}
2190621906

@@ -21941,13 +21941,13 @@ namespace ts {
2194121941
}
2194221942

2194321943
function checkCollisionWithCapturedThisVariable(node: Node, name: Identifier): void {
21944-
if (needCollisionCheckForIdentifier(node, name, "_this")) {
21944+
if (languageVersion <= ScriptTarget.ES5 && !compilerOptions.noEmit && needCollisionCheckForIdentifier(node, name, "_this")) {
2194521945
potentialThisCollisions.push(node);
2194621946
}
2194721947
}
2194821948

2194921949
function checkCollisionWithCapturedNewTargetVariable(node: Node, name: Identifier): void {
21950-
if (needCollisionCheckForIdentifier(node, name, "_newTarget")) {
21950+
if (languageVersion <= ScriptTarget.ES5 &&!compilerOptions.noEmit && needCollisionCheckForIdentifier(node, name, "_newTarget")) {
2195121951
potentialNewTargetCollisions.push(node);
2195221952
}
2195321953
}
@@ -21984,6 +21984,10 @@ namespace ts {
2198421984
}
2198521985

2198621986
function checkCollisionWithCapturedSuperVariable(node: Node, name: Identifier) {
21987+
if (languageVersion >= ScriptTarget.ES2015 || compilerOptions.noEmit) {
21988+
return;
21989+
}
21990+
2198721991
if (!needCollisionCheckForIdentifier(node, name, "_super")) {
2198821992
return;
2198921993
}
@@ -22008,7 +22012,7 @@ namespace ts {
2200822012

2200922013
function checkCollisionWithRequireExportsInGeneratedCode(node: Node, name: Identifier) {
2201022014
// No need to check for require or exports for ES6 modules and later
22011-
if (modulekind >= ModuleKind.ES2015) {
22015+
if (modulekind >= ModuleKind.ES2015 || compilerOptions.noEmit) {
2201222016
return;
2201322017
}
2201422018

@@ -22031,7 +22035,7 @@ namespace ts {
2203122035
}
2203222036

2203322037
function checkCollisionWithGlobalPromiseInGeneratedCode(node: Node, name: Identifier): void {
22034-
if (languageVersion >= ScriptTarget.ES2017 || !needCollisionCheckForIdentifier(node, name, "Promise")) {
22038+
if (languageVersion >= ScriptTarget.ES2017 || compilerOptions.noEmit || !needCollisionCheckForIdentifier(node, name, "Promise")) {
2203522039
return;
2203622040
}
2203722041

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
tests/cases/compiler/shadowedReservedCompilerDeclarationsWithNoEmit.ts(23,13): error TS1215: Invalid use of 'arguments'. Modules are automatically in strict mode.
2+
3+
4+
==== tests/cases/compiler/shadowedReservedCompilerDeclarationsWithNoEmit.ts (1 errors) ====
5+
// Shadowed captured this and super
6+
class Base { }
7+
class C extends Base {
8+
constructor() {
9+
super();
10+
}
11+
12+
foo() {
13+
let _this = this;
14+
15+
() => {
16+
_this;
17+
};
18+
}
19+
20+
bar() {
21+
let _super = this;
22+
}
23+
}
24+
25+
26+
/// shadowed arguments
27+
function f1(arguments, ...a) {
28+
~~~~~~~~~
29+
!!! error TS1215: Invalid use of 'arguments'. Modules are automatically in strict mode.
30+
}
31+
32+
class C2 extends Base {
33+
constructor() {
34+
super();
35+
var _newTarget = "";
36+
var t = new.target;
37+
var y = _newTarget;
38+
}
39+
}
40+
41+
42+
// Shadowed Promise
43+
var Promise = null;
44+
async function f4() {
45+
return 0;
46+
}
47+
48+
49+
// shadowed require
50+
var require = 0;
51+
52+
// shadowed exports
53+
var exports = 0;
54+
55+
56+
export { };
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
=== tests/cases/compiler/shadowedReservedCompilerDeclarationsWithNoEmit.ts ===
2+
// Shadowed captured this and super
3+
class Base { }
4+
>Base : Symbol(Base, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 0, 0))
5+
6+
class C extends Base {
7+
>C : Symbol(C, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 1, 14))
8+
>Base : Symbol(Base, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 0, 0))
9+
10+
constructor() {
11+
super();
12+
>super : Symbol(Base, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 0, 0))
13+
}
14+
15+
foo() {
16+
>foo : Symbol(C.foo, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 5, 5))
17+
18+
let _this = this;
19+
>_this : Symbol(_this, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 8, 11))
20+
>this : Symbol(C, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 1, 14))
21+
22+
() => {
23+
_this;
24+
>_this : Symbol(_this, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 8, 11))
25+
26+
};
27+
}
28+
29+
bar() {
30+
>bar : Symbol(C.bar, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 13, 5))
31+
32+
let _super = this;
33+
>_super : Symbol(_super, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 16, 11))
34+
>this : Symbol(C, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 1, 14))
35+
}
36+
}
37+
38+
39+
/// shadowed arguments
40+
function f1(arguments, ...a) {
41+
>f1 : Symbol(f1, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 18, 1))
42+
>arguments : Symbol(arguments, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 22, 12))
43+
>a : Symbol(a, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 22, 22))
44+
}
45+
46+
class C2 extends Base {
47+
>C2 : Symbol(C2, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 23, 1))
48+
>Base : Symbol(Base, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 0, 0))
49+
50+
constructor() {
51+
super();
52+
>super : Symbol(Base, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 0, 0))
53+
54+
var _newTarget = "";
55+
>_newTarget : Symbol(_newTarget, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 28, 11))
56+
57+
var t = new.target;
58+
>t : Symbol(t, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 29, 11))
59+
60+
var y = _newTarget;
61+
>y : Symbol(y, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 30, 11))
62+
>_newTarget : Symbol(_newTarget, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 28, 11))
63+
}
64+
}
65+
66+
67+
// Shadowed Promise
68+
var Promise = null;
69+
>Promise : Symbol(Promise, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 36, 3))
70+
71+
async function f4() {
72+
>f4 : Symbol(f4, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 36, 19))
73+
74+
return 0;
75+
}
76+
77+
78+
// shadowed require
79+
var require = 0;
80+
>require : Symbol(require, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 43, 3))
81+
82+
// shadowed exports
83+
var exports = 0;
84+
>exports : Symbol(exports, Decl(shadowedReservedCompilerDeclarationsWithNoEmit.ts, 46, 3))
85+
86+
87+
export { };
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
=== tests/cases/compiler/shadowedReservedCompilerDeclarationsWithNoEmit.ts ===
2+
// Shadowed captured this and super
3+
class Base { }
4+
>Base : Base
5+
6+
class C extends Base {
7+
>C : C
8+
>Base : Base
9+
10+
constructor() {
11+
super();
12+
>super() : void
13+
>super : typeof Base
14+
}
15+
16+
foo() {
17+
>foo : () => void
18+
19+
let _this = this;
20+
>_this : this
21+
>this : this
22+
23+
() => {
24+
>() => { _this; } : () => void
25+
26+
_this;
27+
>_this : this
28+
29+
};
30+
}
31+
32+
bar() {
33+
>bar : () => void
34+
35+
let _super = this;
36+
>_super : this
37+
>this : this
38+
}
39+
}
40+
41+
42+
/// shadowed arguments
43+
function f1(arguments, ...a) {
44+
>f1 : (arguments: any, ...a: any[]) => void
45+
>arguments : any
46+
>a : any[]
47+
}
48+
49+
class C2 extends Base {
50+
>C2 : C2
51+
>Base : Base
52+
53+
constructor() {
54+
super();
55+
>super() : void
56+
>super : typeof Base
57+
58+
var _newTarget = "";
59+
>_newTarget : string
60+
>"" : ""
61+
62+
var t = new.target;
63+
>t : typeof C2
64+
>new.target : typeof C2
65+
>target : any
66+
67+
var y = _newTarget;
68+
>y : string
69+
>_newTarget : string
70+
}
71+
}
72+
73+
74+
// Shadowed Promise
75+
var Promise = null;
76+
>Promise : any
77+
>null : null
78+
79+
async function f4() {
80+
>f4 : () => Promise<number>
81+
82+
return 0;
83+
>0 : 0
84+
}
85+
86+
87+
// shadowed require
88+
var require = 0;
89+
>require : number
90+
>0 : 0
91+
92+
// shadowed exports
93+
var exports = 0;
94+
>exports : number
95+
>0 : 0
96+
97+
98+
export { };
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// @target: es5
2+
// @noemit: true
3+
4+
// Shadowed captured this and super
5+
class Base { }
6+
class C extends Base {
7+
constructor() {
8+
super();
9+
}
10+
11+
foo() {
12+
let _this = this;
13+
14+
() => {
15+
_this;
16+
};
17+
}
18+
19+
bar() {
20+
let _super = this;
21+
}
22+
}
23+
24+
25+
/// shadowed arguments
26+
function f1(arguments, ...a) {
27+
}
28+
29+
class C2 extends Base {
30+
constructor() {
31+
super();
32+
var _newTarget = "";
33+
var t = new.target;
34+
var y = _newTarget;
35+
}
36+
}
37+
38+
39+
// Shadowed Promise
40+
var Promise = null;
41+
async function f4() {
42+
return 0;
43+
}
44+
45+
46+
// shadowed require
47+
var require = 0;
48+
49+
// shadowed exports
50+
var exports = 0;
51+
52+
53+
export { };

0 commit comments

Comments
 (0)