From 424b805d6166c5095be97d8328ebaf1968ec1028 Mon Sep 17 00:00:00 2001 From: Matt Kantor Date: Wed, 20 Jan 2021 15:23:34 -0800 Subject: [PATCH] Fix "Cannot find name 'global'. Did you mean 'global'?" (#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. --- src/compiler/checker.ts | 4 ++++ .../spellingSuggestionGlobal1.errors.txt | 10 ++++++++++ .../reference/spellingSuggestionGlobal1.js | 10 ++++++++++ .../reference/spellingSuggestionGlobal1.symbols | 8 ++++++++ .../reference/spellingSuggestionGlobal1.types | 11 +++++++++++ .../spellingSuggestionGlobal2.errors.txt | 12 ++++++++++++ .../reference/spellingSuggestionGlobal2.js | 12 ++++++++++++ .../reference/spellingSuggestionGlobal2.symbols | 12 ++++++++++++ .../reference/spellingSuggestionGlobal2.types | 17 +++++++++++++++++ .../spellingSuggestionGlobal3.errors.txt | 10 ++++++++++ .../reference/spellingSuggestionGlobal3.js | 8 ++++++++ .../reference/spellingSuggestionGlobal3.symbols | 7 +++++++ .../reference/spellingSuggestionGlobal3.types | 12 ++++++++++++ .../spellingSuggestionGlobal4.errors.txt | 10 ++++++++++ .../reference/spellingSuggestionGlobal4.js | 10 ++++++++++ .../reference/spellingSuggestionGlobal4.symbols | 8 ++++++++ .../reference/spellingSuggestionGlobal4.types | 11 +++++++++++ .../cases/compiler/spellingSuggestionGlobal1.ts | 3 +++ .../cases/compiler/spellingSuggestionGlobal2.ts | 4 ++++ .../cases/compiler/spellingSuggestionGlobal3.ts | 2 ++ .../cases/compiler/spellingSuggestionGlobal4.ts | 3 +++ 21 files changed, 184 insertions(+) create mode 100644 tests/baselines/reference/spellingSuggestionGlobal1.errors.txt create mode 100644 tests/baselines/reference/spellingSuggestionGlobal1.js create mode 100644 tests/baselines/reference/spellingSuggestionGlobal1.symbols create mode 100644 tests/baselines/reference/spellingSuggestionGlobal1.types create mode 100644 tests/baselines/reference/spellingSuggestionGlobal2.errors.txt create mode 100644 tests/baselines/reference/spellingSuggestionGlobal2.js create mode 100644 tests/baselines/reference/spellingSuggestionGlobal2.symbols create mode 100644 tests/baselines/reference/spellingSuggestionGlobal2.types create mode 100644 tests/baselines/reference/spellingSuggestionGlobal3.errors.txt create mode 100644 tests/baselines/reference/spellingSuggestionGlobal3.js create mode 100644 tests/baselines/reference/spellingSuggestionGlobal3.symbols create mode 100644 tests/baselines/reference/spellingSuggestionGlobal3.types create mode 100644 tests/baselines/reference/spellingSuggestionGlobal4.errors.txt create mode 100644 tests/baselines/reference/spellingSuggestionGlobal4.js create mode 100644 tests/baselines/reference/spellingSuggestionGlobal4.symbols create mode 100644 tests/baselines/reference/spellingSuggestionGlobal4.types create mode 100644 tests/cases/compiler/spellingSuggestionGlobal1.ts create mode 100644 tests/cases/compiler/spellingSuggestionGlobal2.ts create mode 100644 tests/cases/compiler/spellingSuggestionGlobal3.ts create mode 100644 tests/cases/compiler/spellingSuggestionGlobal4.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4e630203b86..e67c9626527 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2028,6 +2028,10 @@ namespace ts { let suggestion: Symbol | undefined; if (suggestedNameNotFoundMessage && suggestionCount < maximumSuggestionCount) { suggestion = getSuggestedSymbolForNonexistentSymbol(originalLocation, name, meaning); + const isGlobalScopeAugmentationDeclaration = suggestion && suggestion.valueDeclaration && isAmbientModule(suggestion.valueDeclaration) && isGlobalScopeAugmentation(suggestion.valueDeclaration); + if (isGlobalScopeAugmentationDeclaration) { + suggestion = undefined; + } if (suggestion) { const suggestionName = symbolToString(suggestion); const diagnostic = error(errorLocation, suggestedNameNotFoundMessage, diagnosticName(nameArg!), suggestionName); diff --git a/tests/baselines/reference/spellingSuggestionGlobal1.errors.txt b/tests/baselines/reference/spellingSuggestionGlobal1.errors.txt new file mode 100644 index 00000000000..491623ea37b --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal1.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/spellingSuggestionGlobal1.ts(3,1): error TS2304: Cannot find name 'global'. + + +==== tests/cases/compiler/spellingSuggestionGlobal1.ts (1 errors) ==== + export {} + declare global { const x: any } + global.x // should not suggest `global` (GH#42209) + ~~~~~~ +!!! error TS2304: Cannot find name 'global'. + \ No newline at end of file diff --git a/tests/baselines/reference/spellingSuggestionGlobal1.js b/tests/baselines/reference/spellingSuggestionGlobal1.js new file mode 100644 index 00000000000..16d5da7ba81 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal1.js @@ -0,0 +1,10 @@ +//// [spellingSuggestionGlobal1.ts] +export {} +declare global { const x: any } +global.x // should not suggest `global` (GH#42209) + + +//// [spellingSuggestionGlobal1.js] +"use strict"; +exports.__esModule = true; +global.x; // should not suggest `global` (GH#42209) diff --git a/tests/baselines/reference/spellingSuggestionGlobal1.symbols b/tests/baselines/reference/spellingSuggestionGlobal1.symbols new file mode 100644 index 00000000000..e9d20fe53d7 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal1.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/spellingSuggestionGlobal1.ts === +export {} +declare global { const x: any } +>global : Symbol(global, Decl(spellingSuggestionGlobal1.ts, 0, 9)) +>x : Symbol(x, Decl(spellingSuggestionGlobal1.ts, 1, 22)) + +global.x // should not suggest `global` (GH#42209) + diff --git a/tests/baselines/reference/spellingSuggestionGlobal1.types b/tests/baselines/reference/spellingSuggestionGlobal1.types new file mode 100644 index 00000000000..1c4cd6a79e2 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal1.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/spellingSuggestionGlobal1.ts === +export {} +declare global { const x: any } +>global : typeof global +>x : any + +global.x // should not suggest `global` (GH#42209) +>global.x : any +>global : any +>x : any + diff --git a/tests/baselines/reference/spellingSuggestionGlobal2.errors.txt b/tests/baselines/reference/spellingSuggestionGlobal2.errors.txt new file mode 100644 index 00000000000..813e64867f0 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal2.errors.txt @@ -0,0 +1,12 @@ +tests/cases/compiler/spellingSuggestionGlobal2.ts(4,1): error TS2552: Cannot find name 'global'. Did you mean 'globals'? + + +==== tests/cases/compiler/spellingSuggestionGlobal2.ts (1 errors) ==== + export {} + declare global { const x: any } + const globals = { x: true } + global.x // should suggest `globals` (GH#42209) + ~~~~~~ +!!! error TS2552: Cannot find name 'global'. Did you mean 'globals'? +!!! related TS2728 tests/cases/compiler/spellingSuggestionGlobal2.ts:3:7: 'globals' is declared here. + \ No newline at end of file diff --git a/tests/baselines/reference/spellingSuggestionGlobal2.js b/tests/baselines/reference/spellingSuggestionGlobal2.js new file mode 100644 index 00000000000..b7da56d7f49 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal2.js @@ -0,0 +1,12 @@ +//// [spellingSuggestionGlobal2.ts] +export {} +declare global { const x: any } +const globals = { x: true } +global.x // should suggest `globals` (GH#42209) + + +//// [spellingSuggestionGlobal2.js] +"use strict"; +exports.__esModule = true; +var globals = { x: true }; +global.x; // should suggest `globals` (GH#42209) diff --git a/tests/baselines/reference/spellingSuggestionGlobal2.symbols b/tests/baselines/reference/spellingSuggestionGlobal2.symbols new file mode 100644 index 00000000000..e7cd40a83f9 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal2.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/spellingSuggestionGlobal2.ts === +export {} +declare global { const x: any } +>global : Symbol(global, Decl(spellingSuggestionGlobal2.ts, 0, 9)) +>x : Symbol(x, Decl(spellingSuggestionGlobal2.ts, 1, 22)) + +const globals = { x: true } +>globals : Symbol(globals, Decl(spellingSuggestionGlobal2.ts, 2, 5)) +>x : Symbol(x, Decl(spellingSuggestionGlobal2.ts, 2, 17)) + +global.x // should suggest `globals` (GH#42209) + diff --git a/tests/baselines/reference/spellingSuggestionGlobal2.types b/tests/baselines/reference/spellingSuggestionGlobal2.types new file mode 100644 index 00000000000..07463a10228 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal2.types @@ -0,0 +1,17 @@ +=== tests/cases/compiler/spellingSuggestionGlobal2.ts === +export {} +declare global { const x: any } +>global : typeof global +>x : any + +const globals = { x: true } +>globals : { x: boolean; } +>{ x: true } : { x: boolean; } +>x : boolean +>true : true + +global.x // should suggest `globals` (GH#42209) +>global.x : any +>global : any +>x : any + diff --git a/tests/baselines/reference/spellingSuggestionGlobal3.errors.txt b/tests/baselines/reference/spellingSuggestionGlobal3.errors.txt new file mode 100644 index 00000000000..a3d49611fbf --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal3.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/spellingSuggestionGlobal3.ts(2,1): error TS2552: Cannot find name 'globals'. Did you mean 'global'? + + +==== tests/cases/compiler/spellingSuggestionGlobal3.ts (1 errors) ==== + const global = { x: true } + globals.x // should suggest `global` (GH#42209) + ~~~~~~~ +!!! error TS2552: Cannot find name 'globals'. Did you mean 'global'? +!!! related TS2728 tests/cases/compiler/spellingSuggestionGlobal3.ts:1:7: 'global' is declared here. + \ No newline at end of file diff --git a/tests/baselines/reference/spellingSuggestionGlobal3.js b/tests/baselines/reference/spellingSuggestionGlobal3.js new file mode 100644 index 00000000000..9ce2c0d5529 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal3.js @@ -0,0 +1,8 @@ +//// [spellingSuggestionGlobal3.ts] +const global = { x: true } +globals.x // should suggest `global` (GH#42209) + + +//// [spellingSuggestionGlobal3.js] +var global = { x: true }; +globals.x; // should suggest `global` (GH#42209) diff --git a/tests/baselines/reference/spellingSuggestionGlobal3.symbols b/tests/baselines/reference/spellingSuggestionGlobal3.symbols new file mode 100644 index 00000000000..44a64fd50f0 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal3.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/spellingSuggestionGlobal3.ts === +const global = { x: true } +>global : Symbol(global, Decl(spellingSuggestionGlobal3.ts, 0, 5)) +>x : Symbol(x, Decl(spellingSuggestionGlobal3.ts, 0, 16)) + +globals.x // should suggest `global` (GH#42209) + diff --git a/tests/baselines/reference/spellingSuggestionGlobal3.types b/tests/baselines/reference/spellingSuggestionGlobal3.types new file mode 100644 index 00000000000..d5708befef7 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal3.types @@ -0,0 +1,12 @@ +=== tests/cases/compiler/spellingSuggestionGlobal3.ts === +const global = { x: true } +>global : { x: boolean; } +>{ x: true } : { x: boolean; } +>x : boolean +>true : true + +globals.x // should suggest `global` (GH#42209) +>globals.x : any +>globals : any +>x : any + diff --git a/tests/baselines/reference/spellingSuggestionGlobal4.errors.txt b/tests/baselines/reference/spellingSuggestionGlobal4.errors.txt new file mode 100644 index 00000000000..0e3a0853a47 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal4.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/spellingSuggestionGlobal4.ts(3,1): error TS2304: Cannot find name 'global'. + + +==== tests/cases/compiler/spellingSuggestionGlobal4.ts (1 errors) ==== + export {} + declare global { var x: any } + global.x // should not suggest `global` (GH#42209) + ~~~~~~ +!!! error TS2304: Cannot find name 'global'. + \ No newline at end of file diff --git a/tests/baselines/reference/spellingSuggestionGlobal4.js b/tests/baselines/reference/spellingSuggestionGlobal4.js new file mode 100644 index 00000000000..d9e6c2b9a7c --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal4.js @@ -0,0 +1,10 @@ +//// [spellingSuggestionGlobal4.ts] +export {} +declare global { var x: any } +global.x // should not suggest `global` (GH#42209) + + +//// [spellingSuggestionGlobal4.js] +"use strict"; +exports.__esModule = true; +global.x; // should not suggest `global` (GH#42209) diff --git a/tests/baselines/reference/spellingSuggestionGlobal4.symbols b/tests/baselines/reference/spellingSuggestionGlobal4.symbols new file mode 100644 index 00000000000..862216dbdb1 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal4.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/spellingSuggestionGlobal4.ts === +export {} +declare global { var x: any } +>global : Symbol(global, Decl(spellingSuggestionGlobal4.ts, 0, 9)) +>x : Symbol(x, Decl(spellingSuggestionGlobal4.ts, 1, 20)) + +global.x // should not suggest `global` (GH#42209) + diff --git a/tests/baselines/reference/spellingSuggestionGlobal4.types b/tests/baselines/reference/spellingSuggestionGlobal4.types new file mode 100644 index 00000000000..9fd8b336716 --- /dev/null +++ b/tests/baselines/reference/spellingSuggestionGlobal4.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/spellingSuggestionGlobal4.ts === +export {} +declare global { var x: any } +>global : typeof global +>x : any + +global.x // should not suggest `global` (GH#42209) +>global.x : any +>global : any +>x : any + diff --git a/tests/cases/compiler/spellingSuggestionGlobal1.ts b/tests/cases/compiler/spellingSuggestionGlobal1.ts new file mode 100644 index 00000000000..74e534da54d --- /dev/null +++ b/tests/cases/compiler/spellingSuggestionGlobal1.ts @@ -0,0 +1,3 @@ +export {} +declare global { const x: any } +global.x // should not suggest `global` (GH#42209) diff --git a/tests/cases/compiler/spellingSuggestionGlobal2.ts b/tests/cases/compiler/spellingSuggestionGlobal2.ts new file mode 100644 index 00000000000..5914f9990b3 --- /dev/null +++ b/tests/cases/compiler/spellingSuggestionGlobal2.ts @@ -0,0 +1,4 @@ +export {} +declare global { const x: any } +const globals = { x: true } +global.x // should suggest `globals` (GH#42209) diff --git a/tests/cases/compiler/spellingSuggestionGlobal3.ts b/tests/cases/compiler/spellingSuggestionGlobal3.ts new file mode 100644 index 00000000000..853290a8d02 --- /dev/null +++ b/tests/cases/compiler/spellingSuggestionGlobal3.ts @@ -0,0 +1,2 @@ +const global = { x: true } +globals.x // should suggest `global` (GH#42209) diff --git a/tests/cases/compiler/spellingSuggestionGlobal4.ts b/tests/cases/compiler/spellingSuggestionGlobal4.ts new file mode 100644 index 00000000000..a100ca4653b --- /dev/null +++ b/tests/cases/compiler/spellingSuggestionGlobal4.ts @@ -0,0 +1,3 @@ +export {} +declare global { var x: any } +global.x // should not suggest `global` (GH#42209)