Skip to content

Commit

Permalink
Addressed code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirtitian committed Apr 4, 2024
1 parent 4fa810b commit 0c53615
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
16 changes: 12 additions & 4 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
globals,
getSymbolOfDeclaration,
error,
getNodeLinks,
getRequiresScopeChangeCache,
setRequiresScopeChangeCache,
lookup: getSymbol,
onPropertyWithInvalidInitializer: checkAndReportErrorForInvalidInitializer,
onFailedToResolveSymbol,
Expand All @@ -1526,7 +1527,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
globals,
getSymbolOfDeclaration,
error,
getNodeLinks,
getRequiresScopeChangeCache,
setRequiresScopeChangeCache,
lookup: getSuggestionForSymbolNameLookup,
});
// for public members that accept a Node or one of its subtypes, we must guard against
Expand Down Expand Up @@ -3027,6 +3029,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function getRequiresScopeChangeCache(node: FunctionLikeDeclaration) {
return getNodeLinks(node).declarationRequiresScopeChange;
}
function setRequiresScopeChangeCache(node: FunctionLikeDeclaration, value: boolean) {
getNodeLinks(node).declarationRequiresScopeChange = value;
}
// The invalid initializer error is needed in two situation:
// 1. When result is undefined, after checking for a missing "this."
// 2. When result is defined
Expand Down Expand Up @@ -3056,12 +3064,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
meaning: SymbolFlags,
nameNotFoundMessage: DiagnosticMessage,
) {
const name = typeof nameArg === "string" ? nameArg : (nameArg as Identifier).escapedText;
const name = isString(nameArg) ? nameArg : (nameArg as Identifier).escapedText;
addLazyDiagnostic(() => {
if (
!errorLocation ||
errorLocation.parent.kind !== SyntaxKind.JSDocLink &&
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && // TODO: GH#18217
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) &&
!checkAndReportErrorForExtendingInterface(errorLocation) &&
!checkAndReportErrorForUsingTypeAsNamespace(errorLocation, name, meaning) &&
!checkAndReportErrorForExportingPrimitiveType(errorLocation, name) &&
Expand Down
45 changes: 24 additions & 21 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ import {
ResolvedModuleWithFailedLookupLocations,
ResolvedTypeReferenceDirective,
ResolvedTypeReferenceDirectiveWithFailedLookupLocations,
returnFalse,
ReturnStatement,
returnUndefined,
SatisfiesExpression,
ScriptKind,
ScriptTarget,
Expand Down Expand Up @@ -10818,10 +10820,11 @@ export function createNameResolver({
getSymbolOfDeclaration,
globals,
lookup,
getNodeLinks,
onPropertyWithInvalidInitializer,
onFailedToResolveSymbol,
onSuccessfullyResolvedSymbol,
setRequiresScopeChangeCache = returnUndefined,
getRequiresScopeChangeCache = returnUndefined,
onPropertyWithInvalidInitializer = returnFalse,
onFailedToResolveSymbol = returnUndefined,
onSuccessfullyResolvedSymbol = returnUndefined,
}: {
compilerOptions: CompilerOptions;
getSymbolOfDeclaration: (node: Declaration) => Symbol;
Expand All @@ -10830,7 +10833,8 @@ export function createNameResolver({
argumentsSymbol: Symbol;
requireSymbol: Symbol;
lookup: (symbols: SymbolTable, name: __String, meaning: SymbolFlags) => Symbol | undefined;
getNodeLinks: (node: Node) => { declarationRequiresScopeChange?: boolean; };
setRequiresScopeChangeCache: undefined | ((node: FunctionLikeDeclaration, value: boolean) => void);
getRequiresScopeChangeCache: undefined | ((node: FunctionLikeDeclaration) => boolean | undefined);
onPropertyWithInvalidInitializer?: (location: Node | undefined, name: __String, declaration: PropertyDeclaration, result: Symbol | undefined) => boolean;
onFailedToResolveSymbol?: (
location: Node | undefined,
Expand Down Expand Up @@ -10869,7 +10873,7 @@ export function createNameResolver({
let associatedDeclarationForContainingInitializerOrBindingName: ParameterDeclaration | BindingElement | undefined;
let withinDeferredContext = false;
let grandparent: Node;
const name = typeof nameArg === "string" ? nameArg : (nameArg as Identifier).escapedText;
const name = isString(nameArg) ? nameArg : (nameArg as Identifier).escapedText;
loop:
while (location) {
if (name === "const" && isConstAssertion(location)) {
Expand Down Expand Up @@ -11231,18 +11235,16 @@ export function createNameResolver({
}
}

if (nameNotFoundMessage && propertyWithInvalidInitializer && onPropertyWithInvalidInitializer!(originalLocation, name, propertyWithInvalidInitializer, result)) {
return undefined;
}

if (!result) {
if (nameNotFoundMessage) {
onFailedToResolveSymbol!(originalLocation, nameArg, meaning, nameNotFoundMessage);
if (nameNotFoundMessage) {
if (propertyWithInvalidInitializer && onPropertyWithInvalidInitializer(originalLocation, name, propertyWithInvalidInitializer, result)) {
return undefined;
}
if (!result) {
onFailedToResolveSymbol(originalLocation, nameArg, meaning, nameNotFoundMessage);
}
else {
onSuccessfullyResolvedSymbol(originalLocation, result, meaning, lastLocation, associatedDeclarationForContainingInitializerOrBindingName, withinDeferredContext);
}
return undefined;
}
else if (nameNotFoundMessage) {
onSuccessfullyResolvedSymbol!(originalLocation, result, meaning, lastLocation, associatedDeclarationForContainingInitializerOrBindingName, withinDeferredContext);
}

return result;
Expand All @@ -11264,11 +11266,12 @@ export function createNameResolver({
// - nullish coalesce pre-es2020
// - spread assignment in binding pattern pre-es2017
if (target >= ScriptTarget.ES2015) {
const links = getNodeLinks(functionLocation);
if (links.declarationRequiresScopeChange === undefined) {
links.declarationRequiresScopeChange = forEach(functionLocation.parameters, requiresScopeChange) || false;
let declarationRequiresScopeChange = getRequiresScopeChangeCache(functionLocation);
if (declarationRequiresScopeChange === undefined) {
declarationRequiresScopeChange = forEach(functionLocation.parameters, requiresScopeChange) || false;
setRequiresScopeChangeCache(functionLocation, declarationRequiresScopeChange);
}
return !links.declarationRequiresScopeChange;
return !declarationRequiresScopeChange;
}
}
return false;
Expand Down

0 comments on commit 0c53615

Please sign in to comment.