diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 0847d1d7829..8dee87d29b6 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -240,7 +240,7 @@ namespace ts { // Make an identifier from an external module name by extracting the string after the last "/" and replacing // all non-alphanumeric characters with underscores export function makeIdentifierFromModuleName(moduleName: string): string { - return getBaseFileName(moduleName).replace(/\W/g, "_"); + return getBaseFileName(moduleName).replace(/^(\d)/, "_$1").replace(/\W/g, "_"); } export function isBlockOrCatchScoped(declaration: Declaration) { diff --git a/tests/baselines/reference/commonjsSafeImport.js b/tests/baselines/reference/commonjsSafeImport.js new file mode 100644 index 00000000000..a0ef81cc277 --- /dev/null +++ b/tests/baselines/reference/commonjsSafeImport.js @@ -0,0 +1,23 @@ +//// [tests/cases/compiler/commonjsSafeImport.ts] //// + +//// [10_lib.ts] + +export function Foo() {} + +//// [main.ts] +import { Foo } from './10_lib'; + +Foo(); + + +//// [10_lib.js] +function Foo() { } +exports.Foo = Foo; +//// [main.js] +var _10_lib_1 = require('./10_lib'); +_10_lib_1.Foo(); + + +//// [10_lib.d.ts] +export declare function Foo(): void; +//// [main.d.ts] diff --git a/tests/baselines/reference/commonjsSafeImport.symbols b/tests/baselines/reference/commonjsSafeImport.symbols new file mode 100644 index 00000000000..45e247b46a0 --- /dev/null +++ b/tests/baselines/reference/commonjsSafeImport.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/10_lib.ts === + +export function Foo() {} +>Foo : Symbol(Foo, Decl(10_lib.ts, 0, 0)) + +=== tests/cases/compiler/main.ts === +import { Foo } from './10_lib'; +>Foo : Symbol(Foo, Decl(main.ts, 0, 8)) + +Foo(); +>Foo : Symbol(Foo, Decl(main.ts, 0, 8)) + diff --git a/tests/baselines/reference/commonjsSafeImport.types b/tests/baselines/reference/commonjsSafeImport.types new file mode 100644 index 00000000000..5a4dded5b1f --- /dev/null +++ b/tests/baselines/reference/commonjsSafeImport.types @@ -0,0 +1,13 @@ +=== tests/cases/compiler/10_lib.ts === + +export function Foo() {} +>Foo : () => void + +=== tests/cases/compiler/main.ts === +import { Foo } from './10_lib'; +>Foo : () => void + +Foo(); +>Foo() : void +>Foo : () => void + diff --git a/tests/cases/compiler/commonjsSafeImport.ts b/tests/cases/compiler/commonjsSafeImport.ts new file mode 100644 index 00000000000..ce494ee54ad --- /dev/null +++ b/tests/cases/compiler/commonjsSafeImport.ts @@ -0,0 +1,11 @@ +// @target: ES5 +// @module: commonjs +// @declaration: true +// @filename: 10_lib.ts + +export function Foo() {} + +// @filename: main.ts +import { Foo } from './10_lib'; + +Foo();