Skip to content

Commit b6a0988

Browse files
authored
Merge pull request microsoft#30776 from andrewbranch/feature/10178
Add flag to allow access to UMD globals from modules
2 parents 7ccc89b + 786753d commit b6a0988

File tree

20 files changed

+100
-1
lines changed

20 files changed

+100
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1648,7 +1648,7 @@ namespace ts {
16481648
if (result && isInExternalModule && (meaning & SymbolFlags.Value) === SymbolFlags.Value && !(originalLocation!.flags & NodeFlags.JSDoc)) {
16491649
const merged = getMergedSymbol(result);
16501650
if (length(merged.declarations) && every(merged.declarations, d => isNamespaceExportDeclaration(d) || isSourceFile(d) && !!d.symbol.globalExports)) {
1651-
error(errorLocation!, Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, unescapeLeadingUnderscores(name)); // TODO: GH#18217
1651+
errorOrSuggestion(!compilerOptions.allowUmdGlobalAccess, errorLocation!, Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, unescapeLeadingUnderscores(name));
16521652
}
16531653
}
16541654
}

src/compiler/commandLineParser.ts

+7
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,13 @@ namespace ts {
588588
category: Diagnostics.Module_Resolution_Options,
589589
description: Diagnostics.Do_not_resolve_the_real_path_of_symlinks,
590590
},
591+
{
592+
name: "allowUmdGlobalAccess",
593+
type: "boolean",
594+
affectsSemanticDiagnostics: true,
595+
category: Diagnostics.Module_Resolution_Options,
596+
description: Diagnostics.Allow_accessing_UMD_globals_from_modules,
597+
},
591598

592599
// Source Maps
593600
{

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -4946,5 +4946,9 @@
49464946
"Convert parameters to destructured object": {
49474947
"category": "Message",
49484948
"code": 95075
4949+
},
4950+
"Allow accessing UMD globals from modules.": {
4951+
"category": "Message",
4952+
"code": 95076
49494953
}
49504954
}

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4589,6 +4589,7 @@ namespace ts {
45894589
allowJs?: boolean;
45904590
/*@internal*/ allowNonTsExtensions?: boolean;
45914591
allowSyntheticDefaultImports?: boolean;
4592+
allowUmdGlobalAccess?: boolean;
45924593
allowUnreachableCode?: boolean;
45934594
allowUnusedLabels?: boolean;
45944595
alwaysStrict?: boolean; // Always combine with strict property

tests/baselines/reference/api/tsserverlibrary.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,7 @@ declare namespace ts {
24922492
interface CompilerOptions {
24932493
allowJs?: boolean;
24942494
allowSyntheticDefaultImports?: boolean;
2495+
allowUmdGlobalAccess?: boolean;
24952496
allowUnreachableCode?: boolean;
24962497
allowUnusedLabels?: boolean;
24972498
alwaysStrict?: boolean;

tests/baselines/reference/api/typescript.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2492,6 +2492,7 @@ declare namespace ts {
24922492
interface CompilerOptions {
24932493
allowJs?: boolean;
24942494
allowSyntheticDefaultImports?: boolean;
2495+
allowUmdGlobalAccess?: boolean;
24952496
allowUnreachableCode?: boolean;
24962497
allowUnusedLabels?: boolean;
24972498
alwaysStrict?: boolean;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"allowUmdGlobalAccess": true
4+
}
5+
}

tests/baselines/reference/tsConfig/Default initialized TSConfig/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with advanced options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with boolean value compiler options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with enum value compiler options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with files options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option value/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with incorrect compiler option/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options with enum value/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/tsConfig/Initialized TSConfig with list compiler options/tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
4949
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
5050
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
51+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
5152

5253
/* Source Map Options */
5354
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */

tests/baselines/reference/umd9.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [tests/cases/conformance/externalModules/umd9.ts] ////
2+
3+
//// [foo.d.ts]
4+
declare class Thing {
5+
foo(): number;
6+
}
7+
export = Thing;
8+
export as namespace Foo;
9+
10+
//// [a.ts]
11+
/// <reference path="foo.d.ts" />
12+
export const x = Foo; // OK in value position because allowUmdGlobalAccess: true
13+
14+
15+
//// [a.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
/// <reference path="foo.d.ts" />
19+
exports.x = Foo; // OK in value position because allowUmdGlobalAccess: true
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/externalModules/a.ts ===
2+
/// <reference path="foo.d.ts" />
3+
export const x = Foo; // OK in value position because allowUmdGlobalAccess: true
4+
>x : Symbol(x, Decl(a.ts, 1, 12))
5+
>Foo : Symbol(Foo, Decl(foo.d.ts, 3, 15))
6+
7+
=== tests/cases/conformance/externalModules/foo.d.ts ===
8+
declare class Thing {
9+
>Thing : Symbol(Thing, Decl(foo.d.ts, 0, 0))
10+
11+
foo(): number;
12+
>foo : Symbol(Thing.foo, Decl(foo.d.ts, 0, 21))
13+
}
14+
export = Thing;
15+
>Thing : Symbol(Thing, Decl(foo.d.ts, 0, 0))
16+
17+
export as namespace Foo;
18+
>Foo : Symbol(Foo, Decl(foo.d.ts, 3, 15))
19+

tests/baselines/reference/umd9.types

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/externalModules/a.ts ===
2+
/// <reference path="foo.d.ts" />
3+
export const x = Foo; // OK in value position because allowUmdGlobalAccess: true
4+
>x : typeof Thing
5+
>Foo : typeof Thing
6+
7+
=== tests/cases/conformance/externalModules/foo.d.ts ===
8+
declare class Thing {
9+
>Thing : Thing
10+
11+
foo(): number;
12+
>foo : () => number
13+
}
14+
export = Thing;
15+
>Thing : Thing
16+
17+
export as namespace Foo;
18+
>Foo : typeof Thing
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @module: commonjs
2+
// @noImplicitReferences: true
3+
// @allowUmdGlobalAccess: true
4+
5+
// @filename: foo.d.ts
6+
declare class Thing {
7+
foo(): number;
8+
}
9+
export = Thing;
10+
export as namespace Foo;
11+
12+
// @filename: a.ts
13+
/// <reference path="foo.d.ts" />
14+
export const x = Foo; // OK in value position because allowUmdGlobalAccess: true

0 commit comments

Comments
 (0)