Skip to content

Commit

Permalink
Merge pull request #29766 from Microsoft/USE-ALL-THE-ES2015-NAMES-WES…
Browse files Browse the repository at this point in the history
…LEY-WHAT-IS-SO-HARD-ABOUT-THAT

Expand "set your lib" suggestions to other commonly used ES2015 declarations
  • Loading branch information
DanielRosenwasser authored Feb 6, 2019
2 parents eed9db5 + e35ab96 commit 4505eea
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1716,7 +1716,7 @@ namespace ts {
}
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false));
if (symbol && !(symbol.flags & SymbolFlags.NamespaceModule)) {
const message = (name === "Promise" || name === "Symbol")
const message = isES2015OrLaterConstructorName(name)
? Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later
: Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here;
error(errorLocation, message, unescapeLeadingUnderscores(name));
Expand All @@ -1726,6 +1726,19 @@ namespace ts {
return false;
}

function isES2015OrLaterConstructorName(n: __String) {
switch (n) {
case "Promise":
case "Symbol":
case "Map":
case "WeakMap":
case "Set":
case "WeakSet":
return true;
}
return false;
}

function checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule & ~SymbolFlags.Type)) {
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.NamespaceModule & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false));
Expand Down
40 changes: 40 additions & 0 deletions tests/baselines/reference/forwardDeclaredCommonTypes01.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
tests/cases/compiler/forwardDeclaredCommonTypes01.ts(9,9): error TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
tests/cases/compiler/forwardDeclaredCommonTypes01.ts(10,9): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
tests/cases/compiler/forwardDeclaredCommonTypes01.ts(10,17): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
tests/cases/compiler/forwardDeclaredCommonTypes01.ts(11,9): error TS2585: 'Map' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
tests/cases/compiler/forwardDeclaredCommonTypes01.ts(12,9): error TS2585: 'WeakMap' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
tests/cases/compiler/forwardDeclaredCommonTypes01.ts(13,9): error TS2585: 'Set' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
tests/cases/compiler/forwardDeclaredCommonTypes01.ts(14,9): error TS2585: 'WeakSet' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.


==== tests/cases/compiler/forwardDeclaredCommonTypes01.ts (7 errors) ====
interface Promise<T> {}
interface Symbol {}
interface Map<K, V> {}
interface WeakMap<K extends object, V> {}
interface Set<T> {}
interface WeakSet<T extends object> {}

(function() {
new Promise;
~~~~~~~
!!! error TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
new Symbol; Symbol();
~~~~~~
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
~~~~~~
!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
new Map;
~~~
!!! error TS2585: 'Map' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
new WeakMap;
~~~~~~~
!!! error TS2585: 'WeakMap' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
new Set;
~~~
!!! error TS2585: 'Set' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
new WeakSet;
~~~~~~~
!!! error TS2585: 'WeakSet' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.
});

28 changes: 28 additions & 0 deletions tests/baselines/reference/forwardDeclaredCommonTypes01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [forwardDeclaredCommonTypes01.ts]
interface Promise<T> {}
interface Symbol {}
interface Map<K, V> {}
interface WeakMap<K extends object, V> {}
interface Set<T> {}
interface WeakSet<T extends object> {}

(function() {
new Promise;
new Symbol; Symbol();
new Map;
new WeakMap;
new Set;
new WeakSet;
});


//// [forwardDeclaredCommonTypes01.js]
(function () {
new Promise;
new Symbol;
Symbol();
new Map;
new WeakMap;
new Set;
new WeakSet;
});
35 changes: 35 additions & 0 deletions tests/baselines/reference/forwardDeclaredCommonTypes01.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
=== tests/cases/compiler/forwardDeclaredCommonTypes01.ts ===
interface Promise<T> {}
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(forwardDeclaredCommonTypes01.ts, 0, 0))
>T : Symbol(T, Decl(lib.es5.d.ts, --, --), Decl(forwardDeclaredCommonTypes01.ts, 0, 18))

interface Symbol {}
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(forwardDeclaredCommonTypes01.ts, 0, 23))

interface Map<K, V> {}
>Map : Symbol(Map, Decl(forwardDeclaredCommonTypes01.ts, 1, 19))
>K : Symbol(K, Decl(forwardDeclaredCommonTypes01.ts, 2, 14))
>V : Symbol(V, Decl(forwardDeclaredCommonTypes01.ts, 2, 16))

interface WeakMap<K extends object, V> {}
>WeakMap : Symbol(WeakMap, Decl(forwardDeclaredCommonTypes01.ts, 2, 22))
>K : Symbol(K, Decl(forwardDeclaredCommonTypes01.ts, 3, 18))
>V : Symbol(V, Decl(forwardDeclaredCommonTypes01.ts, 3, 35))

interface Set<T> {}
>Set : Symbol(Set, Decl(forwardDeclaredCommonTypes01.ts, 3, 41))
>T : Symbol(T, Decl(forwardDeclaredCommonTypes01.ts, 4, 14))

interface WeakSet<T extends object> {}
>WeakSet : Symbol(WeakSet, Decl(forwardDeclaredCommonTypes01.ts, 4, 19))
>T : Symbol(T, Decl(forwardDeclaredCommonTypes01.ts, 5, 18))

(function() {
new Promise;
new Symbol; Symbol();
new Map;
new WeakMap;
new Set;
new WeakSet;
});

40 changes: 40 additions & 0 deletions tests/baselines/reference/forwardDeclaredCommonTypes01.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
=== tests/cases/compiler/forwardDeclaredCommonTypes01.ts ===
interface Promise<T> {}
interface Symbol {}
interface Map<K, V> {}
interface WeakMap<K extends object, V> {}
interface Set<T> {}
interface WeakSet<T extends object> {}

(function() {
>(function() { new Promise; new Symbol; Symbol(); new Map; new WeakMap; new Set; new WeakSet;}) : () => void
>function() { new Promise; new Symbol; Symbol(); new Map; new WeakMap; new Set; new WeakSet;} : () => void

new Promise;
>new Promise : any
>Promise : any

new Symbol; Symbol();
>new Symbol : any
>Symbol : any
>Symbol() : any
>Symbol : any

new Map;
>new Map : any
>Map : any

new WeakMap;
>new WeakMap : any
>WeakMap : any

new Set;
>new Set : any
>Set : any

new WeakSet;
>new WeakSet : any
>WeakSet : any

});

18 changes: 18 additions & 0 deletions tests/cases/compiler/forwardDeclaredCommonTypes01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @lib: es5
// @target: es5

interface Promise<T> {}
interface Symbol {}
interface Map<K, V> {}
interface WeakMap<K extends object, V> {}
interface Set<T> {}
interface WeakSet<T extends object> {}

(function() {
new Promise;
new Symbol; Symbol();
new Map;
new WeakMap;
new Set;
new WeakSet;
});

0 comments on commit 4505eea

Please sign in to comment.