Skip to content

Commit 424b805

Browse files
authored
Fix "Cannot find name 'global'. Did you mean 'global'?" (microsoft#42262)
* Add tests for "Cannot find name 'global'. Did you mean 'global'?" * Fix "Cannot find name 'global'. Did you mean 'global'?" * Add an additional test case for spelling suggestions of "global". * Name the boolean for suggestions being global scope augmentations.
1 parent 0383b5c commit 424b805

21 files changed

+184
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,10 @@ namespace ts {
20282028
let suggestion: Symbol | undefined;
20292029
if (suggestedNameNotFoundMessage && suggestionCount < maximumSuggestionCount) {
20302030
suggestion = getSuggestedSymbolForNonexistentSymbol(originalLocation, name, meaning);
2031+
const isGlobalScopeAugmentationDeclaration = suggestion && suggestion.valueDeclaration && isAmbientModule(suggestion.valueDeclaration) && isGlobalScopeAugmentation(suggestion.valueDeclaration);
2032+
if (isGlobalScopeAugmentationDeclaration) {
2033+
suggestion = undefined;
2034+
}
20312035
if (suggestion) {
20322036
const suggestionName = symbolToString(suggestion);
20332037
const diagnostic = error(errorLocation, suggestedNameNotFoundMessage, diagnosticName(nameArg!), suggestionName);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/spellingSuggestionGlobal1.ts(3,1): error TS2304: Cannot find name 'global'.
2+
3+
4+
==== tests/cases/compiler/spellingSuggestionGlobal1.ts (1 errors) ====
5+
export {}
6+
declare global { const x: any }
7+
global.x // should not suggest `global` (GH#42209)
8+
~~~~~~
9+
!!! error TS2304: Cannot find name 'global'.
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [spellingSuggestionGlobal1.ts]
2+
export {}
3+
declare global { const x: any }
4+
global.x // should not suggest `global` (GH#42209)
5+
6+
7+
//// [spellingSuggestionGlobal1.js]
8+
"use strict";
9+
exports.__esModule = true;
10+
global.x; // should not suggest `global` (GH#42209)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal1.ts ===
2+
export {}
3+
declare global { const x: any }
4+
>global : Symbol(global, Decl(spellingSuggestionGlobal1.ts, 0, 9))
5+
>x : Symbol(x, Decl(spellingSuggestionGlobal1.ts, 1, 22))
6+
7+
global.x // should not suggest `global` (GH#42209)
8+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal1.ts ===
2+
export {}
3+
declare global { const x: any }
4+
>global : typeof global
5+
>x : any
6+
7+
global.x // should not suggest `global` (GH#42209)
8+
>global.x : any
9+
>global : any
10+
>x : any
11+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/spellingSuggestionGlobal2.ts(4,1): error TS2552: Cannot find name 'global'. Did you mean 'globals'?
2+
3+
4+
==== tests/cases/compiler/spellingSuggestionGlobal2.ts (1 errors) ====
5+
export {}
6+
declare global { const x: any }
7+
const globals = { x: true }
8+
global.x // should suggest `globals` (GH#42209)
9+
~~~~~~
10+
!!! error TS2552: Cannot find name 'global'. Did you mean 'globals'?
11+
!!! related TS2728 tests/cases/compiler/spellingSuggestionGlobal2.ts:3:7: 'globals' is declared here.
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [spellingSuggestionGlobal2.ts]
2+
export {}
3+
declare global { const x: any }
4+
const globals = { x: true }
5+
global.x // should suggest `globals` (GH#42209)
6+
7+
8+
//// [spellingSuggestionGlobal2.js]
9+
"use strict";
10+
exports.__esModule = true;
11+
var globals = { x: true };
12+
global.x; // should suggest `globals` (GH#42209)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal2.ts ===
2+
export {}
3+
declare global { const x: any }
4+
>global : Symbol(global, Decl(spellingSuggestionGlobal2.ts, 0, 9))
5+
>x : Symbol(x, Decl(spellingSuggestionGlobal2.ts, 1, 22))
6+
7+
const globals = { x: true }
8+
>globals : Symbol(globals, Decl(spellingSuggestionGlobal2.ts, 2, 5))
9+
>x : Symbol(x, Decl(spellingSuggestionGlobal2.ts, 2, 17))
10+
11+
global.x // should suggest `globals` (GH#42209)
12+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal2.ts ===
2+
export {}
3+
declare global { const x: any }
4+
>global : typeof global
5+
>x : any
6+
7+
const globals = { x: true }
8+
>globals : { x: boolean; }
9+
>{ x: true } : { x: boolean; }
10+
>x : boolean
11+
>true : true
12+
13+
global.x // should suggest `globals` (GH#42209)
14+
>global.x : any
15+
>global : any
16+
>x : any
17+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/spellingSuggestionGlobal3.ts(2,1): error TS2552: Cannot find name 'globals'. Did you mean 'global'?
2+
3+
4+
==== tests/cases/compiler/spellingSuggestionGlobal3.ts (1 errors) ====
5+
const global = { x: true }
6+
globals.x // should suggest `global` (GH#42209)
7+
~~~~~~~
8+
!!! error TS2552: Cannot find name 'globals'. Did you mean 'global'?
9+
!!! related TS2728 tests/cases/compiler/spellingSuggestionGlobal3.ts:1:7: 'global' is declared here.
10+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [spellingSuggestionGlobal3.ts]
2+
const global = { x: true }
3+
globals.x // should suggest `global` (GH#42209)
4+
5+
6+
//// [spellingSuggestionGlobal3.js]
7+
var global = { x: true };
8+
globals.x; // should suggest `global` (GH#42209)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal3.ts ===
2+
const global = { x: true }
3+
>global : Symbol(global, Decl(spellingSuggestionGlobal3.ts, 0, 5))
4+
>x : Symbol(x, Decl(spellingSuggestionGlobal3.ts, 0, 16))
5+
6+
globals.x // should suggest `global` (GH#42209)
7+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal3.ts ===
2+
const global = { x: true }
3+
>global : { x: boolean; }
4+
>{ x: true } : { x: boolean; }
5+
>x : boolean
6+
>true : true
7+
8+
globals.x // should suggest `global` (GH#42209)
9+
>globals.x : any
10+
>globals : any
11+
>x : any
12+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/spellingSuggestionGlobal4.ts(3,1): error TS2304: Cannot find name 'global'.
2+
3+
4+
==== tests/cases/compiler/spellingSuggestionGlobal4.ts (1 errors) ====
5+
export {}
6+
declare global { var x: any }
7+
global.x // should not suggest `global` (GH#42209)
8+
~~~~~~
9+
!!! error TS2304: Cannot find name 'global'.
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [spellingSuggestionGlobal4.ts]
2+
export {}
3+
declare global { var x: any }
4+
global.x // should not suggest `global` (GH#42209)
5+
6+
7+
//// [spellingSuggestionGlobal4.js]
8+
"use strict";
9+
exports.__esModule = true;
10+
global.x; // should not suggest `global` (GH#42209)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal4.ts ===
2+
export {}
3+
declare global { var x: any }
4+
>global : Symbol(global, Decl(spellingSuggestionGlobal4.ts, 0, 9))
5+
>x : Symbol(x, Decl(spellingSuggestionGlobal4.ts, 1, 20))
6+
7+
global.x // should not suggest `global` (GH#42209)
8+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/spellingSuggestionGlobal4.ts ===
2+
export {}
3+
declare global { var x: any }
4+
>global : typeof global
5+
>x : any
6+
7+
global.x // should not suggest `global` (GH#42209)
8+
>global.x : any
9+
>global : any
10+
>x : any
11+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export {}
2+
declare global { const x: any }
3+
global.x // should not suggest `global` (GH#42209)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export {}
2+
declare global { const x: any }
3+
const globals = { x: true }
4+
global.x // should suggest `globals` (GH#42209)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const global = { x: true }
2+
globals.x // should suggest `global` (GH#42209)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export {}
2+
declare global { var x: any }
3+
global.x // should not suggest `global` (GH#42209)

0 commit comments

Comments
 (0)