Skip to content

Commit 265069e

Browse files
committed
Merge pull request #6194 from Microsoft/fix4867_transpiling
Fix incorrectly emitting underscore of imported property
2 parents 322126d + bc25990 commit 265069e

16 files changed

+543
-1
lines changed

src/compiler/emitter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3889,7 +3889,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
38893889
// We create a synthetic copy of the identifier in order to avoid the rewriting that might
38903890
// otherwise occur when the identifier is emitted.
38913891
index = <Identifier | LiteralExpression>createSynthesizedNode(propName.kind);
3892-
(<Identifier | LiteralExpression>index).text = (<Identifier | LiteralExpression>propName).text;
3892+
// We need to unescape identifier here because when parsing an identifier prefixing with "__"
3893+
// the parser need to append "_" in order to escape colliding with magic identifiers such as "__proto__"
3894+
// Therefore, in order to correctly emit identifiers that are written in original TypeScript file,
3895+
// we will unescapeIdentifier to remove additional underscore (if no underscore is added, the function will return original input string)
3896+
(<Identifier | LiteralExpression>index).text = unescapeIdentifier((<Identifier | LiteralExpression>propName).text);
38933897
}
38943898

38953899
return !nameIsComputed && index.kind === SyntaxKind.Identifier
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests/cases/conformance/es6/modules/m1.ts(6,5): error TS1005: ',' expected.
2+
3+
4+
==== tests/cases/conformance/es6/modules/m1.ts (1 errors) ====
5+
6+
var R: any
7+
export default R = {
8+
"__": 20,
9+
"_": 10
10+
"___": 30
11+
~~~~~
12+
!!! error TS1005: ',' expected.
13+
}
14+
15+
==== tests/cases/conformance/es6/modules/m2.ts (0 errors) ====
16+
import R from "./m1";
17+
const { __, _, ___ } = R;
18+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores1.ts] ////
2+
3+
//// [m1.ts]
4+
5+
var R: any
6+
export default R = {
7+
"__": 20,
8+
"_": 10
9+
"___": 30
10+
}
11+
12+
//// [m2.ts]
13+
import R from "./m1";
14+
const { __, _, ___ } = R;
15+
16+
17+
//// [m1.js]
18+
"use strict";
19+
var R;
20+
exports.__esModule = true;
21+
exports["default"] = R = {
22+
"__": 20,
23+
"_": 10,
24+
"___": 30
25+
};
26+
//// [m2.js]
27+
"use strict";
28+
var m1_1 = require("./m1");
29+
var __ = m1_1["default"].__, _ = m1_1["default"]._, ___ = m1_1["default"].___;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores2.ts] ////
2+
3+
//// [m1.ts]
4+
5+
var R: any
6+
export default R = {
7+
"__esmodule": true,
8+
"__proto__": {}
9+
}
10+
11+
//// [m2.ts]
12+
import R from "./m1";
13+
const { __esmodule, __proto__ } = R;
14+
15+
16+
//// [m1.js]
17+
"use strict";
18+
var R;
19+
exports.__esModule = true;
20+
exports["default"] = R = {
21+
"__esmodule": true,
22+
"__proto__": {}
23+
};
24+
//// [m2.js]
25+
"use strict";
26+
var m1_1 = require("./m1");
27+
var __esmodule = m1_1["default"].__esmodule, __proto__ = m1_1["default"].__proto__;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/conformance/es6/modules/m1.ts ===
2+
3+
var R: any
4+
>R : Symbol(R, Decl(m1.ts, 1, 3))
5+
6+
export default R = {
7+
>R : Symbol(R, Decl(m1.ts, 1, 3))
8+
9+
"__esmodule": true,
10+
"__proto__": {}
11+
}
12+
13+
=== tests/cases/conformance/es6/modules/m2.ts ===
14+
import R from "./m1";
15+
>R : Symbol(R, Decl(m2.ts, 0, 6))
16+
17+
const { __esmodule, __proto__ } = R;
18+
>__esmodule : Symbol(__esmodule, Decl(m2.ts, 1, 7))
19+
>__proto__ : Symbol(__proto__, Decl(m2.ts, 1, 19))
20+
>R : Symbol(R, Decl(m2.ts, 0, 6))
21+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/conformance/es6/modules/m1.ts ===
2+
3+
var R: any
4+
>R : any
5+
6+
export default R = {
7+
>R = { "__esmodule": true, "__proto__": {}} : { "__esmodule": boolean; "__proto__": {}; }
8+
>R : any
9+
>{ "__esmodule": true, "__proto__": {}} : { "__esmodule": boolean; "__proto__": {}; }
10+
11+
"__esmodule": true,
12+
>true : boolean
13+
14+
"__proto__": {}
15+
>{} : {}
16+
}
17+
18+
=== tests/cases/conformance/es6/modules/m2.ts ===
19+
import R from "./m1";
20+
>R : { "__esmodule": boolean; "__proto__": {}; }
21+
22+
const { __esmodule, __proto__ } = R;
23+
>__esmodule : boolean
24+
>__proto__ : {}
25+
>R : { "__esmodule": boolean; "__proto__": {}; }
26+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores3.ts] ////
2+
3+
//// [m1.ts]
4+
5+
var R: any
6+
export default R = {
7+
"___": 30,
8+
"___hello": 21,
9+
"_hi": 40,
10+
}
11+
12+
//// [m2.ts]
13+
import R from "./m1";
14+
const { ___, ___hello, _hi } = R;
15+
16+
17+
//// [m1.js]
18+
"use strict";
19+
var R;
20+
exports.__esModule = true;
21+
exports["default"] = R = {
22+
"___": 30,
23+
"___hello": 21,
24+
"_hi": 40
25+
};
26+
//// [m2.js]
27+
"use strict";
28+
var m1_1 = require("./m1");
29+
var ___ = m1_1["default"].___, ___hello = m1_1["default"].___hello, _hi = m1_1["default"]._hi;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=== tests/cases/conformance/es6/modules/m1.ts ===
2+
3+
var R: any
4+
>R : Symbol(R, Decl(m1.ts, 1, 3))
5+
6+
export default R = {
7+
>R : Symbol(R, Decl(m1.ts, 1, 3))
8+
9+
"___": 30,
10+
"___hello": 21,
11+
"_hi": 40,
12+
}
13+
14+
=== tests/cases/conformance/es6/modules/m2.ts ===
15+
import R from "./m1";
16+
>R : Symbol(R, Decl(m2.ts, 0, 6))
17+
18+
const { ___, ___hello, _hi } = R;
19+
>___ : Symbol(___, Decl(m2.ts, 1, 7))
20+
>___hello : Symbol(___hello, Decl(m2.ts, 1, 12))
21+
>_hi : Symbol(_hi, Decl(m2.ts, 1, 22))
22+
>R : Symbol(R, Decl(m2.ts, 0, 6))
23+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/conformance/es6/modules/m1.ts ===
2+
3+
var R: any
4+
>R : any
5+
6+
export default R = {
7+
>R = { "___": 30, "___hello": 21, "_hi": 40,} : { "___": number; "___hello": number; "_hi": number; }
8+
>R : any
9+
>{ "___": 30, "___hello": 21, "_hi": 40,} : { "___": number; "___hello": number; "_hi": number; }
10+
11+
"___": 30,
12+
>30 : number
13+
14+
"___hello": 21,
15+
>21 : number
16+
17+
"_hi": 40,
18+
>40 : number
19+
}
20+
21+
=== tests/cases/conformance/es6/modules/m2.ts ===
22+
import R from "./m1";
23+
>R : { "___": number; "___hello": number; "_hi": number; }
24+
25+
const { ___, ___hello, _hi } = R;
26+
>___ : number
27+
>___hello : number
28+
>_hi : number
29+
>R : { "___": number; "___hello": number; "_hi": number; }
30+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores4.ts] ////
2+
3+
//// [m1.ts]
4+
5+
declare var console: any;
6+
export function _() {
7+
console.log("_");
8+
}
9+
export function __() {
10+
console.log("__");
11+
}
12+
export function ___() {
13+
console.log("___");
14+
}
15+
export function _hi() {
16+
console.log("_hi");
17+
}
18+
export function __proto() {
19+
console.log("__proto");
20+
}
21+
export function __esmodule() {
22+
console.log("__esmodule");
23+
}
24+
export function ___hello(){
25+
console.log("___hello");
26+
}
27+
28+
//// [m2.ts]
29+
import {_, __, ___hello, __esmodule, __proto, _hi} from "./m1";
30+
_();
31+
__();
32+
___hello();
33+
__esmodule();
34+
__proto();
35+
_hi();
36+
37+
//// [m1.js]
38+
"use strict";
39+
function _() {
40+
console.log("_");
41+
}
42+
exports._ = _;
43+
function __() {
44+
console.log("__");
45+
}
46+
exports.__ = __;
47+
function ___() {
48+
console.log("___");
49+
}
50+
exports.___ = ___;
51+
function _hi() {
52+
console.log("_hi");
53+
}
54+
exports._hi = _hi;
55+
function __proto() {
56+
console.log("__proto");
57+
}
58+
exports.__proto = __proto;
59+
function __esmodule() {
60+
console.log("__esmodule");
61+
}
62+
exports.__esmodule = __esmodule;
63+
function ___hello() {
64+
console.log("___hello");
65+
}
66+
exports.___hello = ___hello;
67+
//// [m2.js]
68+
"use strict";
69+
var m1_1 = require("./m1");
70+
m1_1._();
71+
m1_1.__();
72+
m1_1.___hello();
73+
m1_1.__esmodule();
74+
m1_1.__proto();
75+
m1_1._hi();

0 commit comments

Comments
 (0)