diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index a3fb38d32c8..d35a723a093 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3889,7 +3889,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge // We create a synthetic copy of the identifier in order to avoid the rewriting that might // otherwise occur when the identifier is emitted. index = createSynthesizedNode(propName.kind); - (index).text = (propName).text; + // We need to unescape identifier here because when parsing an identifier prefixing with "__" + // the parser need to append "_" in order to escape colliding with magic identifiers such as "__proto__" + // Therefore, in order to correctly emit identifiers that are written in original TypeScript file, + // we will unescapeIdentifier to remove additional underscore (if no underscore is added, the function will return original input string) + (index).text = unescapeIdentifier((propName).text); } return !nameIsComputed && index.kind === SyntaxKind.Identifier diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores1.errors.txt b/tests/baselines/reference/exportsAndImportsWithUnderscores1.errors.txt new file mode 100644 index 00000000000..7a65dd62c2b --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores1.errors.txt @@ -0,0 +1,18 @@ +tests/cases/conformance/es6/modules/m1.ts(6,5): error TS1005: ',' expected. + + +==== tests/cases/conformance/es6/modules/m1.ts (1 errors) ==== + + var R: any + export default R = { + "__": 20, + "_": 10 + "___": 30 + ~~~~~ +!!! error TS1005: ',' expected. + } + +==== tests/cases/conformance/es6/modules/m2.ts (0 errors) ==== + import R from "./m1"; + const { __, _, ___ } = R; + \ No newline at end of file diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores1.js b/tests/baselines/reference/exportsAndImportsWithUnderscores1.js new file mode 100644 index 00000000000..d14eab3c251 --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores1.js @@ -0,0 +1,29 @@ +//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores1.ts] //// + +//// [m1.ts] + +var R: any +export default R = { + "__": 20, + "_": 10 + "___": 30 +} + +//// [m2.ts] +import R from "./m1"; +const { __, _, ___ } = R; + + +//// [m1.js] +"use strict"; +var R; +exports.__esModule = true; +exports["default"] = R = { + "__": 20, + "_": 10, + "___": 30 +}; +//// [m2.js] +"use strict"; +var m1_1 = require("./m1"); +var __ = m1_1["default"].__, _ = m1_1["default"]._, ___ = m1_1["default"].___; diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores2.js b/tests/baselines/reference/exportsAndImportsWithUnderscores2.js new file mode 100644 index 00000000000..5d1dcbb9322 --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores2.js @@ -0,0 +1,27 @@ +//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores2.ts] //// + +//// [m1.ts] + +var R: any +export default R = { + "__esmodule": true, + "__proto__": {} +} + +//// [m2.ts] +import R from "./m1"; +const { __esmodule, __proto__ } = R; + + +//// [m1.js] +"use strict"; +var R; +exports.__esModule = true; +exports["default"] = R = { + "__esmodule": true, + "__proto__": {} +}; +//// [m2.js] +"use strict"; +var m1_1 = require("./m1"); +var __esmodule = m1_1["default"].__esmodule, __proto__ = m1_1["default"].__proto__; diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores2.symbols b/tests/baselines/reference/exportsAndImportsWithUnderscores2.symbols new file mode 100644 index 00000000000..d5f5ef17a62 --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores2.symbols @@ -0,0 +1,21 @@ +=== tests/cases/conformance/es6/modules/m1.ts === + +var R: any +>R : Symbol(R, Decl(m1.ts, 1, 3)) + +export default R = { +>R : Symbol(R, Decl(m1.ts, 1, 3)) + + "__esmodule": true, + "__proto__": {} +} + +=== tests/cases/conformance/es6/modules/m2.ts === +import R from "./m1"; +>R : Symbol(R, Decl(m2.ts, 0, 6)) + +const { __esmodule, __proto__ } = R; +>__esmodule : Symbol(__esmodule, Decl(m2.ts, 1, 7)) +>__proto__ : Symbol(__proto__, Decl(m2.ts, 1, 19)) +>R : Symbol(R, Decl(m2.ts, 0, 6)) + diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores2.types b/tests/baselines/reference/exportsAndImportsWithUnderscores2.types new file mode 100644 index 00000000000..a684440f5bd --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores2.types @@ -0,0 +1,26 @@ +=== tests/cases/conformance/es6/modules/m1.ts === + +var R: any +>R : any + +export default R = { +>R = { "__esmodule": true, "__proto__": {}} : { "__esmodule": boolean; "__proto__": {}; } +>R : any +>{ "__esmodule": true, "__proto__": {}} : { "__esmodule": boolean; "__proto__": {}; } + + "__esmodule": true, +>true : boolean + + "__proto__": {} +>{} : {} +} + +=== tests/cases/conformance/es6/modules/m2.ts === +import R from "./m1"; +>R : { "__esmodule": boolean; "__proto__": {}; } + +const { __esmodule, __proto__ } = R; +>__esmodule : boolean +>__proto__ : {} +>R : { "__esmodule": boolean; "__proto__": {}; } + diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores3.js b/tests/baselines/reference/exportsAndImportsWithUnderscores3.js new file mode 100644 index 00000000000..477f870742d --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores3.js @@ -0,0 +1,29 @@ +//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores3.ts] //// + +//// [m1.ts] + +var R: any +export default R = { + "___": 30, + "___hello": 21, + "_hi": 40, +} + +//// [m2.ts] +import R from "./m1"; +const { ___, ___hello, _hi } = R; + + +//// [m1.js] +"use strict"; +var R; +exports.__esModule = true; +exports["default"] = R = { + "___": 30, + "___hello": 21, + "_hi": 40 +}; +//// [m2.js] +"use strict"; +var m1_1 = require("./m1"); +var ___ = m1_1["default"].___, ___hello = m1_1["default"].___hello, _hi = m1_1["default"]._hi; diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores3.symbols b/tests/baselines/reference/exportsAndImportsWithUnderscores3.symbols new file mode 100644 index 00000000000..cd4ba38e8c9 --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores3.symbols @@ -0,0 +1,23 @@ +=== tests/cases/conformance/es6/modules/m1.ts === + +var R: any +>R : Symbol(R, Decl(m1.ts, 1, 3)) + +export default R = { +>R : Symbol(R, Decl(m1.ts, 1, 3)) + + "___": 30, + "___hello": 21, + "_hi": 40, +} + +=== tests/cases/conformance/es6/modules/m2.ts === +import R from "./m1"; +>R : Symbol(R, Decl(m2.ts, 0, 6)) + +const { ___, ___hello, _hi } = R; +>___ : Symbol(___, Decl(m2.ts, 1, 7)) +>___hello : Symbol(___hello, Decl(m2.ts, 1, 12)) +>_hi : Symbol(_hi, Decl(m2.ts, 1, 22)) +>R : Symbol(R, Decl(m2.ts, 0, 6)) + diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores3.types b/tests/baselines/reference/exportsAndImportsWithUnderscores3.types new file mode 100644 index 00000000000..5addb44172d --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores3.types @@ -0,0 +1,30 @@ +=== tests/cases/conformance/es6/modules/m1.ts === + +var R: any +>R : any + +export default R = { +>R = { "___": 30, "___hello": 21, "_hi": 40,} : { "___": number; "___hello": number; "_hi": number; } +>R : any +>{ "___": 30, "___hello": 21, "_hi": 40,} : { "___": number; "___hello": number; "_hi": number; } + + "___": 30, +>30 : number + + "___hello": 21, +>21 : number + + "_hi": 40, +>40 : number +} + +=== tests/cases/conformance/es6/modules/m2.ts === +import R from "./m1"; +>R : { "___": number; "___hello": number; "_hi": number; } + +const { ___, ___hello, _hi } = R; +>___ : number +>___hello : number +>_hi : number +>R : { "___": number; "___hello": number; "_hi": number; } + diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores4.js b/tests/baselines/reference/exportsAndImportsWithUnderscores4.js new file mode 100644 index 00000000000..22cd7c2e71a --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores4.js @@ -0,0 +1,75 @@ +//// [tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores4.ts] //// + +//// [m1.ts] + +declare var console: any; +export function _() { + console.log("_"); +} +export function __() { + console.log("__"); +} +export function ___() { + console.log("___"); +} +export function _hi() { + console.log("_hi"); +} +export function __proto() { + console.log("__proto"); +} +export function __esmodule() { + console.log("__esmodule"); +} +export function ___hello(){ + console.log("___hello"); +} + +//// [m2.ts] +import {_, __, ___hello, __esmodule, __proto, _hi} from "./m1"; +_(); +__(); +___hello(); +__esmodule(); +__proto(); +_hi(); + +//// [m1.js] +"use strict"; +function _() { + console.log("_"); +} +exports._ = _; +function __() { + console.log("__"); +} +exports.__ = __; +function ___() { + console.log("___"); +} +exports.___ = ___; +function _hi() { + console.log("_hi"); +} +exports._hi = _hi; +function __proto() { + console.log("__proto"); +} +exports.__proto = __proto; +function __esmodule() { + console.log("__esmodule"); +} +exports.__esmodule = __esmodule; +function ___hello() { + console.log("___hello"); +} +exports.___hello = ___hello; +//// [m2.js] +"use strict"; +var m1_1 = require("./m1"); +m1_1._(); +m1_1.__(); +m1_1.___hello(); +m1_1.__esmodule(); +m1_1.__proto(); +m1_1._hi(); diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores4.symbols b/tests/baselines/reference/exportsAndImportsWithUnderscores4.symbols new file mode 100644 index 00000000000..a8fb3f307b5 --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores4.symbols @@ -0,0 +1,75 @@ +=== tests/cases/conformance/es6/modules/m1.ts === + +declare var console: any; +>console : Symbol(console, Decl(m1.ts, 1, 11)) + +export function _() { +>_ : Symbol(_, Decl(m1.ts, 1, 25)) + + console.log("_"); +>console : Symbol(console, Decl(m1.ts, 1, 11)) +} +export function __() { +>__ : Symbol(__, Decl(m1.ts, 4, 1)) + + console.log("__"); +>console : Symbol(console, Decl(m1.ts, 1, 11)) +} +export function ___() { +>___ : Symbol(___, Decl(m1.ts, 7, 1)) + + console.log("___"); +>console : Symbol(console, Decl(m1.ts, 1, 11)) +} +export function _hi() { +>_hi : Symbol(_hi, Decl(m1.ts, 10, 1)) + + console.log("_hi"); +>console : Symbol(console, Decl(m1.ts, 1, 11)) +} +export function __proto() { +>__proto : Symbol(__proto, Decl(m1.ts, 13, 1)) + + console.log("__proto"); +>console : Symbol(console, Decl(m1.ts, 1, 11)) +} +export function __esmodule() { +>__esmodule : Symbol(__esmodule, Decl(m1.ts, 16, 1)) + + console.log("__esmodule"); +>console : Symbol(console, Decl(m1.ts, 1, 11)) +} +export function ___hello(){ +>___hello : Symbol(___hello, Decl(m1.ts, 19, 1)) + + console.log("___hello"); +>console : Symbol(console, Decl(m1.ts, 1, 11)) +} + +=== tests/cases/conformance/es6/modules/m2.ts === +import {_, __, ___hello, __esmodule, __proto, _hi} from "./m1"; +>_ : Symbol(_, Decl(m2.ts, 0, 8)) +>__ : Symbol(__, Decl(m2.ts, 0, 10)) +>___hello : Symbol(___hello, Decl(m2.ts, 0, 14)) +>__esmodule : Symbol(__esmodule, Decl(m2.ts, 0, 24)) +>__proto : Symbol(__proto, Decl(m2.ts, 0, 36)) +>_hi : Symbol(_hi, Decl(m2.ts, 0, 45)) + +_(); +>_ : Symbol(_, Decl(m2.ts, 0, 8)) + +__(); +>__ : Symbol(__, Decl(m2.ts, 0, 10)) + +___hello(); +>___hello : Symbol(___hello, Decl(m2.ts, 0, 14)) + +__esmodule(); +>__esmodule : Symbol(__esmodule, Decl(m2.ts, 0, 24)) + +__proto(); +>__proto : Symbol(__proto, Decl(m2.ts, 0, 36)) + +_hi(); +>_hi : Symbol(_hi, Decl(m2.ts, 0, 45)) + diff --git a/tests/baselines/reference/exportsAndImportsWithUnderscores4.types b/tests/baselines/reference/exportsAndImportsWithUnderscores4.types new file mode 100644 index 00000000000..a9042042285 --- /dev/null +++ b/tests/baselines/reference/exportsAndImportsWithUnderscores4.types @@ -0,0 +1,109 @@ +=== tests/cases/conformance/es6/modules/m1.ts === + +declare var console: any; +>console : any + +export function _() { +>_ : () => void + + console.log("_"); +>console.log("_") : any +>console.log : any +>console : any +>log : any +>"_" : string +} +export function __() { +>__ : () => void + + console.log("__"); +>console.log("__") : any +>console.log : any +>console : any +>log : any +>"__" : string +} +export function ___() { +>___ : () => void + + console.log("___"); +>console.log("___") : any +>console.log : any +>console : any +>log : any +>"___" : string +} +export function _hi() { +>_hi : () => void + + console.log("_hi"); +>console.log("_hi") : any +>console.log : any +>console : any +>log : any +>"_hi" : string +} +export function __proto() { +>__proto : () => void + + console.log("__proto"); +>console.log("__proto") : any +>console.log : any +>console : any +>log : any +>"__proto" : string +} +export function __esmodule() { +>__esmodule : () => void + + console.log("__esmodule"); +>console.log("__esmodule") : any +>console.log : any +>console : any +>log : any +>"__esmodule" : string +} +export function ___hello(){ +>___hello : () => void + + console.log("___hello"); +>console.log("___hello") : any +>console.log : any +>console : any +>log : any +>"___hello" : string +} + +=== tests/cases/conformance/es6/modules/m2.ts === +import {_, __, ___hello, __esmodule, __proto, _hi} from "./m1"; +>_ : () => void +>__ : () => void +>___hello : () => void +>__esmodule : () => void +>__proto : () => void +>_hi : () => void + +_(); +>_() : void +>_ : () => void + +__(); +>__() : void +>__ : () => void + +___hello(); +>___hello() : void +>___hello : () => void + +__esmodule(); +>__esmodule() : void +>__esmodule : () => void + +__proto(); +>__proto() : void +>__proto : () => void + +_hi(); +>_hi() : void +>_hi : () => void + diff --git a/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores1.ts b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores1.ts new file mode 100644 index 00000000000..8e38deaaf13 --- /dev/null +++ b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores1.ts @@ -0,0 +1,14 @@ +//@module: commonjs +//@target: ES3 + +// @filename: m1.ts +var R: any +export default R = { + "__": 20, + "_": 10 + "___": 30 +} + +// @filename: m2.ts +import R from "./m1"; +const { __, _, ___ } = R; diff --git a/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores2.ts b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores2.ts new file mode 100644 index 00000000000..fac9f467549 --- /dev/null +++ b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores2.ts @@ -0,0 +1,13 @@ +//@module: commonjs +//@target: ES3 + +// @filename: m1.ts +var R: any +export default R = { + "__esmodule": true, + "__proto__": {} +} + +// @filename: m2.ts +import R from "./m1"; +const { __esmodule, __proto__ } = R; diff --git a/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores3.ts b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores3.ts new file mode 100644 index 00000000000..cd71808915c --- /dev/null +++ b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores3.ts @@ -0,0 +1,14 @@ +//@module: commonjs +//@target: ES3 + +// @filename: m1.ts +var R: any +export default R = { + "___": 30, + "___hello": 21, + "_hi": 40, +} + +// @filename: m2.ts +import R from "./m1"; +const { ___, ___hello, _hi } = R; diff --git a/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores4.ts b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores4.ts new file mode 100644 index 00000000000..34022597552 --- /dev/null +++ b/tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores4.ts @@ -0,0 +1,35 @@ +//@module: commonjs +//@target: ES3 + +// @filename: m1.ts +declare var console: any; +export function _() { + console.log("_"); +} +export function __() { + console.log("__"); +} +export function ___() { + console.log("___"); +} +export function _hi() { + console.log("_hi"); +} +export function __proto() { + console.log("__proto"); +} +export function __esmodule() { + console.log("__esmodule"); +} +export function ___hello(){ + console.log("___hello"); +} + +// @filename: m2.ts +import {_, __, ___hello, __esmodule, __proto, _hi} from "./m1"; +_(); +__(); +___hello(); +__esmodule(); +__proto(); +_hi(); \ No newline at end of file