Skip to content

Fix crash on umd and module merge, allow umds to be accessed when merged with a non-UMD symbol #28782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,8 @@ namespace ts {

// If we're in an external module, we can't reference value symbols created from UMD export declarations
if (result && isInExternalModule && (meaning & SymbolFlags.Value) === SymbolFlags.Value && !(originalLocation!.flags & NodeFlags.JSDoc)) {
if (some(result.declarations, d => isNamespaceExportDeclaration(d) || isSourceFile(d) && !!d.symbol.globalExports)) {
const merged = getMergedSymbol(result);
if (length(merged.declarations) && every(merged.declarations, d => isNamespaceExportDeclaration(d) || isSourceFile(d) && !!d.symbol.globalExports)) {
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
}
}
Expand Down Expand Up @@ -5322,12 +5323,11 @@ namespace ts {
return anyType;
}
// Handle export default expressions
if (isSourceFile(declaration)) {
const jsonSourceFile = cast(declaration, isJsonSourceFile);
if (!jsonSourceFile.statements.length) {
if (isSourceFile(declaration) && isJsonSourceFile(declaration)) {
if (!declaration.statements.length) {
return emptyObjectType;
}
const type = getWidenedLiteralType(checkExpression(jsonSourceFile.statements[0].expression));
const type = getWidenedLiteralType(checkExpression(declaration.statements[0].expression));
if (type.flags & TypeFlags.Object) {
return getRegularTypeOfObjectLiteral(type);
}
Expand All @@ -5352,7 +5352,8 @@ namespace ts {
|| isClassDeclaration(declaration)
|| isFunctionDeclaration(declaration)
|| (isMethodDeclaration(declaration) && !isObjectLiteralMethod(declaration))
|| isMethodSignature(declaration)) {
|| isMethodSignature(declaration)
|| isSourceFile(declaration)) {
// Symbol is property of some kind that is merged with something - should use `getTypeOfFuncClassEnumModule` and not `getTypeOfVariableOrParameterOrProperty`
if (symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.Enum | SymbolFlags.ValueModule)) {
return getTypeOfFuncClassEnumModule(symbol);
Expand Down
14 changes: 1 addition & 13 deletions tests/baselines/reference/exportAsNamespace_augment.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/a.d.ts(3,14): error TS2451: Cannot redeclare block-scoped variable 'conflict'.
/b.ts(6,22): error TS2451: Cannot redeclare block-scoped variable 'conflict'.
/b.ts(12,18): error TS2451: Cannot redeclare block-scoped variable 'conflict'.
/b.ts(15,1): error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.
/b.ts(15,7): error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.
/b.ts(15,13): error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.
/b.ts(15,19): error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.


==== /a.d.ts (1 errors) ====
Expand All @@ -16,7 +12,7 @@
!!! related TS6203 /b.ts:6:22: 'conflict' was also declared here.
!!! related TS6204 /b.ts:12:18: and here.

==== /b.ts (6 errors) ====
==== /b.ts (2 errors) ====
import * as a2 from "./a";

declare global {
Expand All @@ -38,13 +34,5 @@
}

a.x + a.y + a.z + a.conflict;
~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't that error correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, no. If there's a global augmentation adding a thing to the global scope, then it should be globally accessible, period.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But aren't the globals accessible from module? They are only accessible from non module file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today, the umd declaration poisons the symbol unconditionally (irrespective of other things the symbol may have merged with). With this change, the UMD declaration only errors on access if every declaration of the symbol is a UMD declaration.

!!! error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.
~
!!! error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.
~
!!! error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.
~
!!! error TS2686: 'a' refers to a UMD global, but the current file is a module. Consider adding an import instead.
a2.x + a2.y + a2.z + a2.conflict;

27 changes: 27 additions & 0 deletions tests/baselines/reference/umdGlobalAugmentationNoCrash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/umdGlobalAugmentationNoCrash.ts] ////

//// [global.d.ts]
declare global {
const React: typeof import("./module");
}
export {};

//// [module.d.ts]
export as namespace React;
export function foo(): string;

//// [some_module.ts]
export {}
React.foo;

//// [emits.ts]
console.log("hello");
React.foo;


//// [some_module.js]
React.foo;
//// [emits.js]
"use strict";
console.log("hello");
React.foo;
34 changes: 34 additions & 0 deletions tests/baselines/reference/umdGlobalAugmentationNoCrash.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
=== tests/cases/compiler/global.d.ts ===
declare global {
>global : Symbol(global, Decl(global.d.ts, 0, 0))

const React: typeof import("./module");
>React : Symbol("tests/cases/compiler/module", Decl(module.d.ts, 0, 0), Decl(global.d.ts, 1, 9))
}
export {};

=== tests/cases/compiler/module.d.ts ===
export as namespace React;
>React : Symbol(React, Decl(module.d.ts, 0, 0))

export function foo(): string;
>foo : Symbol(foo, Decl(module.d.ts, 0, 26))

=== tests/cases/compiler/some_module.ts ===
export {}
React.foo;
>React.foo : Symbol(foo, Decl(module.d.ts, 0, 26))
>React : Symbol("tests/cases/compiler/module", Decl(module.d.ts, 0, 0), Decl(global.d.ts, 1, 9))
>foo : Symbol(foo, Decl(module.d.ts, 0, 26))

=== tests/cases/compiler/emits.ts ===
console.log("hello");
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))

React.foo;
>React.foo : Symbol(foo, Decl(module.d.ts, 0, 26))
>React : Symbol("tests/cases/compiler/module", Decl(module.d.ts, 0, 0), Decl(global.d.ts, 1, 9))
>foo : Symbol(foo, Decl(module.d.ts, 0, 26))

36 changes: 36 additions & 0 deletions tests/baselines/reference/umdGlobalAugmentationNoCrash.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/compiler/global.d.ts ===
declare global {
>global : typeof global

const React: typeof import("./module");
>React : typeof import("tests/cases/compiler/module")
}
export {};

=== tests/cases/compiler/module.d.ts ===
export as namespace React;
>React : typeof import("tests/cases/compiler/module")

export function foo(): string;
>foo : () => string

=== tests/cases/compiler/some_module.ts ===
export {}
React.foo;
>React.foo : () => string
>React : typeof import("tests/cases/compiler/module")
>foo : () => string

=== tests/cases/compiler/emits.ts ===
console.log("hello");
>console.log("hello") : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>"hello" : "hello"

React.foo;
>React.foo : () => string
>React : typeof import("tests/cases/compiler/module")
>foo : () => string

21 changes: 21 additions & 0 deletions tests/cases/compiler/umdGlobalAugmentationNoCrash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// @strict: true
// @module: esnext
// @moduleResolution: node
// @target: es2018
// @filename: global.d.ts
declare global {
const React: typeof import("./module");
}
export {};

// @filename: module.d.ts
export as namespace React;
export function foo(): string;

// @filename: some_module.ts
export {}
React.foo;

// @filename: emits.ts
console.log("hello");
React.foo;