Skip to content

Support jump of record key #55080

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

Closed
wants to merge 2 commits into from
Closed
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
160 changes: 145 additions & 15 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
__String,
AssignmentDeclarationKind,
AssignmentExpression,
AssignmentOperatorToken,
CallLikeExpression,
canHaveSymbol, concatenate,
canHaveSymbol, CheckFlags, concatenate,
createTextSpan,
createTextSpanFromBounds,
createTextSpanFromNode,
Expand Down Expand Up @@ -61,19 +62,24 @@ import {
isJSDocOverrideTag,
isJsxOpeningLikeElement,
isJumpStatementTarget,
isLiteralTypeNode,
isModuleSpecifierLike,
isNameOfFunctionDeclaration,
isNewExpressionTarget,
isObjectBindingPattern,
isPropertyAccessExpression,
isPropertyName,
isRightSideOfPropertyAccess,
isStaticModifier,
isStringLiteral,
isTransientSymbol,
isTypeAliasDeclaration,
isTypeReferenceNode,
isVariableDeclaration,
last,
map,
mapDefined,
MappedSymbolLinks,
MappedType,
ModifierFlags,
moveRangePastModifiers,
Expand Down Expand Up @@ -556,40 +562,164 @@ function isExpandoDeclaration(node: Declaration): boolean {
}

function getDefinitionFromSymbol(typeChecker: TypeChecker, symbol: Symbol, node: Node, failedAliasResolution?: boolean, excludeDeclaration?: Node): DefinitionInfo[] | undefined {
const filteredDeclarations = filter(symbol.declarations, d => d !== excludeDeclaration);
const withoutExpandos = filter(filteredDeclarations, d => !isExpandoDeclaration(d));
const results = some(withoutExpandos) ? withoutExpandos : filteredDeclarations;
return getConstructSignatureDefinition() || getCallSignatureDefinition() || map(results, declaration => createDefinitionInfo(declaration, typeChecker, symbol, node, /*unverified*/ false, failedAliasResolution));
const filteredDeclarations = filter(
symbol.declarations,
(d) => d !== excludeDeclaration
);
const withoutExpandos = filter(
filteredDeclarations,
(d) => !isExpandoDeclaration(d)
);
const results = some(withoutExpandos)
? withoutExpandos
: filteredDeclarations;
return (
getConstructSignatureDefinition() ||
getCallSignatureDefinition() ||
getRecordKeyDefinition() ||
map(results, (declaration) =>
createDefinitionInfo(
declaration,
typeChecker,
symbol,
node,
/*unverified*/ false,
failedAliasResolution
)
)
);

function getConstructSignatureDefinition(): DefinitionInfo[] | undefined {
// Applicable only if we are in a new expression, or we are on a constructor declaration
// and in either case the symbol has a construct signature definition, i.e. class
if (symbol.flags & SymbolFlags.Class && !(symbol.flags & (SymbolFlags.Function | SymbolFlags.Variable)) && (isNewExpressionTarget(node) || node.kind === SyntaxKind.ConstructorKeyword)) {
const cls = find(filteredDeclarations, isClassLike) || Debug.fail("Expected declaration to have at least one class-like declaration");
return getSignatureDefinition(cls.members, /*selectConstructors*/ true);
if (
symbol.flags & SymbolFlags.Class &&
!(symbol.flags & (SymbolFlags.Function | SymbolFlags.Variable)) &&
(isNewExpressionTarget(node) ||
node.kind === SyntaxKind.ConstructorKeyword)
) {
const cls =
find(filteredDeclarations, isClassLike) ||
Debug.fail(
"Expected declaration to have at least one class-like declaration"
);
return getSignatureDefinition(
cls.members,
/*selectConstructors*/ true
);
}
}

function getCallSignatureDefinition(): DefinitionInfo[] | undefined {
return isCallOrNewExpressionTarget(node) || isNameOfFunctionDeclaration(node)
? getSignatureDefinition(filteredDeclarations, /*selectConstructors*/ false)
return isCallOrNewExpressionTarget(node) ||
isNameOfFunctionDeclaration(node)
? getSignatureDefinition(
filteredDeclarations,
/*selectConstructors*/ false
)
: undefined;
}

function getSignatureDefinition(signatureDeclarations: readonly Declaration[] | undefined, selectConstructors: boolean): DefinitionInfo[] | undefined {
function getSignatureDefinition(
signatureDeclarations: readonly Declaration[] | undefined,
selectConstructors: boolean
): DefinitionInfo[] | undefined {
if (!signatureDeclarations) {
return undefined;
}
const declarations = signatureDeclarations.filter(selectConstructors ? isConstructorDeclaration : isFunctionLike);
const declarationsWithBody = declarations.filter(d => !!(d as FunctionLikeDeclaration).body);
const declarations = signatureDeclarations.filter(
selectConstructors ? isConstructorDeclaration : isFunctionLike
);
const declarationsWithBody = declarations.filter(
(d) => !!(d as FunctionLikeDeclaration).body
);

// declarations defined on the global scope can be defined on multiple files. Get all of them.
return declarations.length
? declarationsWithBody.length !== 0
? declarationsWithBody.map(x => createDefinitionInfo(x, typeChecker, symbol, node))
: [createDefinitionInfo(last(declarations), typeChecker, symbol, node, /*unverified*/ false, failedAliasResolution)]
? declarationsWithBody.map((x) =>
createDefinitionInfo(x, typeChecker, symbol, node)
)
: [
createDefinitionInfo(
last(declarations),
typeChecker,
symbol,
node,
/*unverified*/ false,
failedAliasResolution
),
]
: undefined;
}

function getRecordKeyDefinition(): DefinitionInfo[] | undefined {
if (isPropertyAccessExpression(node.parent)) {
const exp = node.parent.expression;
if (isIdentifier(exp)) {
const sym = getSymbol(exp, typeChecker, /*stopAtAlias*/ true);
if (sym.symbol) {
for (const i of sym.symbol.declarations ?? []) {
if (isVariableDeclaration(i)) {
if (i.type) {
// i.type
if (isTypeReferenceNode(i.type)) {
const arg = i.type.typeArguments?.[0];
if (arg && isLiteralTypeNode(arg) && isStringLiteral(arg.literal)) {

// ref `resolveStructuredTypeMembers` in checker.ts to narrow
// may this work for the mapped type
if (isTransientSymbol(symbol)) {
const links = symbol.links;
if (
links.checkFlags &
CheckFlags.Mapped
) {
const mappedSymbolLinks =
links as MappedSymbolLinks;
const firstTypeArgument =
mappedSymbolLinks.mappedType
.aliasTypeArguments?.[0];
if (firstTypeArgument && firstTypeArgument.isLiteral()) {
if (arg.literal.text === firstTypeArgument.value) {
const sourceFile = arg.getSourceFile();
const textSpan = createTextSpanFromNode(arg, sourceFile);
const def: DefinitionInfo = {
kind: ScriptElementKind.typeElement,
name: "test",
containerKind: ScriptElementKind.typeElement,
containerName: "test-container",
textSpan,
fileName: arg.getSourceFile().fileName
};
return [def];
}
}
// this will lead to the ambient Record declaration
// const haha = definitionFromType(
// mappedSymbolLinks.mappedType,
// typeChecker,
// node,
// undefined
// ).slice();
// return haha;
}
}
}
}
}
else {
// may be as expression
}
}
}
}
return undefined;
}
}

return undefined;
}
}

/**
Expand Down