Skip to content

Fix JSDoc typeof parameter resolution #62053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49700,6 +49700,26 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function getParameterSymbolFromJSDocHost(node: Identifier): Symbol | undefined {
if (!isIdentifier(node)) {
return undefined;
}
const name = node.escapedText;
const decl = getHostSignatureFromJSDoc(node);
if (!decl) {
return undefined;
}
const parameter = find(decl.parameters, p => p.name.kind === SyntaxKind.Identifier && p.name.escapedText === name);
if (parameter && parameter.symbol) {
return parameter.symbol;
}
// If parameter.symbol is not available, try to get it from the function's locals
if (parameter && decl.locals) {
return decl.locals.get(name);
}
return undefined;
}

function getSymbolOfNameOrPropertyAccessExpression(name: EntityName | PrivateIdentifier | PropertyAccessExpression | JSDocMemberName): Symbol | undefined {
if (isDeclarationName(name)) {
return getSymbolOfNode(name.parent);
Expand Down Expand Up @@ -49786,20 +49806,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

const isJSDoc = findAncestor(name, or(isJSDocLinkLike, isJSDocNameReference, isJSDocMemberName));
const meaning = isJSDoc ? SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Value : SymbolFlags.Value;
const isJSDocContext = !!(name.flags & NodeFlags.JSDoc) || isJSDoc;
const meaning = isJSDocContext ? SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Value : SymbolFlags.Value;
if (name.kind === SyntaxKind.Identifier) {
if (isJSXTagName(name) && isJsxIntrinsicTagName(name)) {
const symbol = getIntrinsicTagSymbol(name.parent as JsxOpeningLikeElement);
return symbol === unknownSymbol ? undefined : symbol;
}
// Check for parameter symbol in JSDoc typeof context
if (isJSDocContext && isInTypeQuery(name)) {
const parameterSymbol = getParameterSymbolFromJSDocHost(name);
if (parameterSymbol) {
return parameterSymbol;
}
}
const result = resolveEntityName(name, meaning, /*ignoreErrors*/ true, /*dontResolveAlias*/ true, getHostSignatureFromJSDoc(name));
if (!result && isJSDoc) {
if (!result && isJSDocContext) {
const container = findAncestor(name, or(isClassLike, isInterfaceDeclaration));
if (container) {
return resolveJSDocMemberName(name, /*ignoreErrors*/ true, getSymbolOfDeclaration(container));
}
}
if (result && isJSDoc) {
if (result && isJSDocContext) {
const container = getJSDocHost(name);
if (container && isEnumMember(container) && container === result.valueDeclaration) {
return resolveEntityName(name, meaning, /*ignoreErrors*/ true, /*dontResolveAlias*/ true, getSourceFileOfNode(container)) || result;
Expand Down
26 changes: 26 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11829,6 +11829,13 @@ export function createNameResolver({
return undefined;
}
if (!result) {
// Check for parameter symbol in JSDoc typeof context before failing
if (originalLocation && !isString(nameArg) && (originalLocation.flags & NodeFlags.JSDoc) && isInTypeQuery(originalLocation)) {
const parameterSymbol = getParameterSymbolFromJSDocHost(originalLocation, (nameArg as Identifier).escapedText);
if (parameterSymbol) {
return parameterSymbol;
}
}
onFailedToResolveSymbol(originalLocation, nameArg, meaning, nameNotFoundMessage);
}
else {
Expand All @@ -11839,6 +11846,25 @@ export function createNameResolver({
return result;
}

function getParameterSymbolFromJSDocHost(node: Node, name: __String): Symbol | undefined {
const decl = getHostSignatureFromJSDoc(node);
if (!decl) {
return undefined;
}
const parameter = find(decl.parameters, p => p.name && p.name.kind === SyntaxKind.Identifier && p.name.escapedText === name);
if (parameter && parameter.symbol) {
return parameter.symbol;
}
// If parameter.symbol is not available, try to get it from the function's locals
if (parameter && decl.locals) {
const localSymbol = decl.locals.get(name);
if (localSymbol) {
return localSymbol;
}
}
return undefined;
}

function useOuterVariableScopeInParameter(result: Symbol, location: Node, lastLocation: Node) {
const target = getEmitScriptTarget(compilerOptions);
const functionLocation = location as FunctionLikeDeclaration;
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/jsdocTypeofParameterConsistency.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/jsdocTypeofParameterConsistency.ts] ////

=== jsdocTypeofParameterConsistency.js ===
/**
* @template T
* @param {T} a
* @return {typeof a}
*/
function f(a) {
>f : Symbol(f, Decl(jsdocTypeofParameterConsistency.js, 0, 0))
>a : Symbol(a, Decl(jsdocTypeofParameterConsistency.js, 5, 11))

return a;
>a : Symbol(a, Decl(jsdocTypeofParameterConsistency.js, 5, 11))
}

/**
* @template T
* @param {T} b
* @return {typeof b}
*/
function g(b) {
>g : Symbol(g, Decl(jsdocTypeofParameterConsistency.js, 7, 1))
>b : Symbol(b, Decl(jsdocTypeofParameterConsistency.js, 14, 11))

return b;
>b : Symbol(b, Decl(jsdocTypeofParameterConsistency.js, 14, 11))
}
34 changes: 34 additions & 0 deletions tests/baselines/reference/jsdocTypeofParameterConsistency.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/jsdocTypeofParameterConsistency.ts] ////

=== jsdocTypeofParameterConsistency.js ===
/**
* @template T
* @param {T} a
* @return {typeof a}
*/
function f(a) {
>f : <T>(a: T) => typeof a
> : ^ ^^ ^^ ^^^^^
>a : T
> : ^

return a;
>a : T
> : ^
}

/**
* @template T
* @param {T} b
* @return {typeof b}
*/
function g(b) {
>g : <T>(b: T) => typeof b
> : ^ ^^ ^^ ^^^^^
>b : T
> : ^

return b;
>b : T
> : ^
}
Loading
Loading