Skip to content

Commit

Permalink
Fix checker initialization crash (#46973)
Browse files Browse the repository at this point in the history
* Fix checker initialization crash

* Move checks to a place that makes more sense
  • Loading branch information
andrewbranch committed Dec 3, 2021
1 parent 407edc9 commit 240ba0a
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/compiler/checker.ts
Expand Up @@ -1782,8 +1782,9 @@ namespace ts {
nameNotFoundMessage: DiagnosticMessage | undefined,
nameArg: __String | Identifier | undefined,
isUse: boolean,
excludeGlobals = false): Symbol | undefined {
return resolveNameHelper(location, name, meaning, nameNotFoundMessage, nameArg, isUse, excludeGlobals, getSymbol);
excludeGlobals = false,
getSpellingSuggstions = true): Symbol | undefined {
return resolveNameHelper(location, name, meaning, nameNotFoundMessage, nameArg, isUse, excludeGlobals, getSpellingSuggstions, getSymbol);
}

function resolveNameHelper(
Expand All @@ -1794,6 +1795,7 @@ namespace ts {
nameArg: __String | Identifier | undefined,
isUse: boolean,
excludeGlobals: boolean,
getSpellingSuggestions: boolean,
lookup: typeof getSymbol): Symbol | undefined {
const originalLocation = location; // needed for did-you-mean error reporting, which gathers candidates starting from the original location
let result: Symbol | undefined;
Expand Down Expand Up @@ -2123,7 +2125,7 @@ namespace ts {
}
}
if (!result) {
if (nameNotFoundMessage) {
if (nameNotFoundMessage && produceDiagnostics) {
if (!errorLocation ||
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg!) && // TODO: GH#18217
!checkAndReportErrorForExtendingInterface(errorLocation) &&
Expand All @@ -2133,7 +2135,7 @@ namespace ts {
!checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation, name, meaning) &&
!checkAndReportErrorForUsingValueAsType(errorLocation, name, meaning)) {
let suggestion: Symbol | undefined;
if (suggestionCount < maximumSuggestionCount) {
if (getSpellingSuggestions && suggestionCount < maximumSuggestionCount) {
suggestion = getSuggestedSymbolForNonexistentSymbol(originalLocation, name, meaning);
const isGlobalScopeAugmentationDeclaration = suggestion?.valueDeclaration && isAmbientModule(suggestion.valueDeclaration) && isGlobalScopeAugmentation(suggestion.valueDeclaration);
if (isGlobalScopeAugmentationDeclaration) {
Expand Down Expand Up @@ -2173,7 +2175,7 @@ namespace ts {
}

// Perform extra checks only if error reporting was requested
if (nameNotFoundMessage) {
if (nameNotFoundMessage && produceDiagnostics) {
if (propertyWithInvalidInitializer && !(getEmitScriptTarget(compilerOptions) === ScriptTarget.ESNext && useDefineForClassFields)) {
// We have a match, but the reference occurred within a property initializer and the identifier also binds
// to a local variable in the constructor where the code will be emitted. Note that this is actually allowed
Expand Down Expand Up @@ -13658,7 +13660,7 @@ namespace ts {

function getGlobalSymbol(name: __String, meaning: SymbolFlags, diagnostic: DiagnosticMessage | undefined): Symbol | undefined {
// Don't track references for global symbols anyway, so value if `isReference` is arbitrary
return resolveName(undefined, name, meaning, diagnostic, name, /*isUse*/ false);
return resolveName(undefined, name, meaning, diagnostic, name, /*isUse*/ false, /*excludeGlobals*/ false, /*getSpellingSuggestions*/ false);
}

function getGlobalType(name: __String, arity: 0, reportErrors: true): ObjectType;
Expand Down Expand Up @@ -28720,7 +28722,7 @@ namespace ts {

function getSuggestedSymbolForNonexistentSymbol(location: Node | undefined, outerName: __String, meaning: SymbolFlags): Symbol | undefined {
Debug.assert(outerName !== undefined, "outername should always be defined");
const result = resolveNameHelper(location, outerName, meaning, /*nameNotFoundMessage*/ undefined, outerName, /*isUse*/ false, /*excludeGlobals*/ false, (symbols, name, meaning) => {
const result = resolveNameHelper(location, outerName, meaning, /*nameNotFoundMessage*/ undefined, outerName, /*isUse*/ false, /*excludeGlobals*/ false, /*getSpellingSuggestions*/ true, (symbols, name, meaning) => {
Debug.assertEqual(outerName, name, "name should equal outerName");
const symbol = getSymbol(symbols, name, meaning);
// Sometimes the symbol is found when location is a return type of a function: `typeof x` and `x` is declared in the body of the function
Expand Down

0 comments on commit 240ba0a

Please sign in to comment.