From cabe1d678f793b1f68161ffaba11dbd0ab5a3dfa Mon Sep 17 00:00:00 2001 From: Yui T Date: Sun, 20 Dec 2015 16:39:42 -0800 Subject: [PATCH 1/6] Fix prefix double underscore with extra underscore --- src/compiler/utilities.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index c5a31992a80..3aaae73e4c4 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -230,9 +230,19 @@ namespace ts { return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia); } + function prefixWithUnderscoreUnderscore(identifier: string): boolean { + if (identifier.length <= 2) { + return false; + } + else { + return identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? true : false; + } + } + // Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__' + // TODO(yuisu): comment export function escapeIdentifier(identifier: string): string { - return identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? "_" + identifier : identifier; + return prefixWithUnderscoreUnderscore(identifier)? "_" + identifier : identifier; } // Remove extra underscore from escaped identifier From a034314a9e92af56dbfc99e38dfd8031c51abfe5 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 21 Dec 2015 15:45:58 -0800 Subject: [PATCH 2/6] Add tests --- src/compiler/emitter.ts | 2 +- src/compiler/utilities.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ab775ba9fc6..726fe22aad1 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3848,7 +3848,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi // 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; + (index).text = unescapeIdentifier((propName).text); } return !nameIsComputed && index.kind === SyntaxKind.Identifier diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 3aaae73e4c4..50382b46de6 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -238,11 +238,10 @@ namespace ts { return identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? true : false; } } - + // Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__' - // TODO(yuisu): comment export function escapeIdentifier(identifier: string): string { - return prefixWithUnderscoreUnderscore(identifier)? "_" + identifier : identifier; + return prefixWithUnderscoreUnderscore(identifier) ? "_" + identifier : identifier; } // Remove extra underscore from escaped identifier From 677e080dad9acd8f73baf00e3c1bd9f3425363b2 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 21 Dec 2015 16:23:51 -0800 Subject: [PATCH 3/6] Add tests --- src/compiler/emitter.ts | 2 +- src/compiler/utilities.ts | 5 +- ...portsAndImportsWithUnderscores1.errors.txt | 18 +++ .../exportsAndImportsWithUnderscores1.js | 29 +++++ .../exportsAndImportsWithUnderscores2.js | 27 +++++ .../exportsAndImportsWithUnderscores2.symbols | 21 ++++ .../exportsAndImportsWithUnderscores2.types | 26 +++++ .../exportsAndImportsWithUnderscores3.js | 29 +++++ .../exportsAndImportsWithUnderscores3.symbols | 23 ++++ .../exportsAndImportsWithUnderscores3.types | 30 +++++ .../exportsAndImportsWithUnderscores4.js | 75 ++++++++++++ .../exportsAndImportsWithUnderscores4.symbols | 75 ++++++++++++ .../exportsAndImportsWithUnderscores4.types | 109 ++++++++++++++++++ .../exportsAndImportsWithUnderscores1.ts | 14 +++ .../exportsAndImportsWithUnderscores2.ts | 13 +++ .../exportsAndImportsWithUnderscores3.ts | 14 +++ .../exportsAndImportsWithUnderscores4.ts | 35 ++++++ 17 files changed, 541 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores1.errors.txt create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores1.js create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores2.js create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores2.symbols create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores2.types create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores3.js create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores3.symbols create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores3.types create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores4.js create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores4.symbols create mode 100644 tests/baselines/reference/exportsAndImportsWithUnderscores4.types create mode 100644 tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores1.ts create mode 100644 tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores2.ts create mode 100644 tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores3.ts create mode 100644 tests/cases/conformance/es6/modules/exportsAndImportsWithUnderscores4.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index ab775ba9fc6..726fe22aad1 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3848,7 +3848,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi // 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; + (index).text = unescapeIdentifier((propName).text); } return !nameIsComputed && index.kind === SyntaxKind.Identifier diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 3aaae73e4c4..50382b46de6 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -238,11 +238,10 @@ namespace ts { return identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? true : false; } } - + // Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__' - // TODO(yuisu): comment export function escapeIdentifier(identifier: string): string { - return prefixWithUnderscoreUnderscore(identifier)? "_" + identifier : identifier; + return prefixWithUnderscoreUnderscore(identifier) ? "_" + identifier : identifier; } // Remove extra underscore from escaped 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 From 2fc906ef33d443bdf70c6d15ab98f4346fd51414 Mon Sep 17 00:00:00 2001 From: Yui T Date: Mon, 21 Dec 2015 17:07:18 -0800 Subject: [PATCH 4/6] Address PR --- src/compiler/emitter.ts | 4 ++++ src/compiler/utilities.ts | 12 ++---------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 726fe22aad1..a5ffb92b083 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3848,6 +3848,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi // 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); + // We need to escape 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); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 50382b46de6..6b924c1099e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -230,18 +230,10 @@ namespace ts { return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia); } - function prefixWithUnderscoreUnderscore(identifier: string): boolean { - if (identifier.length <= 2) { - return false; - } - else { - return identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? true : false; - } - } - // Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__' export function escapeIdentifier(identifier: string): string { - return prefixWithUnderscoreUnderscore(identifier) ? "_" + identifier : identifier; + const prefixWithUnderscore = identifier.length > 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._; + return prefixWithUnderscore ? "_" + identifier : identifier; } // Remove extra underscore from escaped identifier From 36a82c70b1ffad912c612c1fb0c9e4272ea18c9a Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Sun, 10 Jan 2016 18:01:40 -0800 Subject: [PATCH 5/6] Address PR --- src/compiler/utilities.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 6b924c1099e..bee0a3eca48 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -232,8 +232,8 @@ namespace ts { // Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__' export function escapeIdentifier(identifier: string): string { - const prefixWithUnderscore = identifier.length > 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._; - return prefixWithUnderscore ? "_" + identifier : identifier; + const prefixedWithUnderscores = identifier.length >= 3 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._; + return prefixedWithUnderscores ? "_" + identifier : identifier; } // Remove extra underscore from escaped identifier From 125d1f448a2502f004cf553acfd1a0bfc4139782 Mon Sep 17 00:00:00 2001 From: Yui T Date: Thu, 21 Jan 2016 16:16:52 -0800 Subject: [PATCH 6/6] Address PR --- src/compiler/emitter.ts | 2 +- src/compiler/utilities.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 0caff291b60..cc4cec5895f 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -3848,7 +3848,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi // 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); - // We need to escape identifier here because when parsing an identifier prefixing with "__" + // 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) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0beabe4b9ea..7bc708c7178 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -232,8 +232,7 @@ namespace ts { // Add an extra underscore to identifiers that start with two underscores to avoid issues with magic names like '__proto__' export function escapeIdentifier(identifier: string): string { - const prefixedWithUnderscores = identifier.length >= 3 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._; - return prefixedWithUnderscores ? "_" + identifier : identifier; + return identifier.length >= 2 && identifier.charCodeAt(0) === CharacterCodes._ && identifier.charCodeAt(1) === CharacterCodes._ ? "_" + identifier : identifier; } // Remove extra underscore from escaped identifier