Skip to content
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

🐛 Set imported binding as an alias of target symbol #1193

Merged
merged 10 commits into from
May 26, 2024

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/core/src/symbol/SymbolUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ type QueryMemberCallback = (this: void, query: SymbolQuery) => unknown
/* istanbul ignore next */
export class SymbolQuery {
readonly category: string
readonly path: readonly string[]
path: readonly string[]
readonly #doc: TextDocument
MulverineX marked this conversation as resolved.
Show resolved Hide resolved
readonly #node: AstNode | undefined
/**
Expand Down Expand Up @@ -1337,6 +1337,8 @@ export class SymbolQuery {
}
this.#symbol = result
this.#map = result.parentMap
this.#parentSymbol = result.parentSymbol
this.path = result.path
misode marked this conversation as resolved.
Show resolved Hide resolved
}
return this
}
Expand Down
41 changes: 30 additions & 11 deletions packages/mcdoc/src/binder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,15 @@ function hoist(node: ModuleNode, ctx: McdocBinderContext): void {
}

// hoistUseStatement associates the AST node with the binding definition in the file symbol table,
// which may get overridden by bindUseStatement in the later stage as an reference to the imported symbol in the global symbol table.
// which will get overridden by bindUseStatement in the later stage as an reference to the imported symbol in the global symbol table.
// This way when the user tries to go to definition on the path in the use statement,
// they will go to the definition in the imported file.

/**
* String representation of the absolute path of the imported symbol.
*/
const importedPathString = pathArrayToString(resolvePath(path, ctx))
MulverineX marked this conversation as resolved.
Show resolved Hide resolved

ctx.symbols
.query(
{ doc: ctx.doc, node },
Expand All @@ -264,6 +269,14 @@ function hoist(node: ModuleNode, ctx: McdocBinderContext): void {
data: {
subcategory: 'use_statement_binding',
visibility: SymbolVisibility.File,
relations: {
aliasOf: importedPathString
? {
category: 'mcdoc',
path: [importedPathString],
}
: undefined,
},
},
usage: { type: 'definition', node: identifier, fullRange: node },
})
Expand Down Expand Up @@ -526,16 +539,22 @@ async function bindPath(
'mcdoc',
pathArrayToString(identifiers),
)
.ifDeclared((_, query) =>
query.enter({
usage: {
type: 'reference',
node: identNode,
fullRange: node,
skipRenaming: LiteralNode.is(identNode),
},
})
)
.ifDeclared((_, query) => {
SPGoding marked this conversation as resolved.
Show resolved Hide resolved
try {
query.resolveAlias().enter({
usage: {
type: 'reference',
node: identNode,
fullRange: node,
skipRenaming: LiteralNode.is(identNode),
},
})
} catch (_ignored) {
MulverineX marked this conversation as resolved.
Show resolved Hide resolved
// Alias target doesn't exist. The error would have been reported
// where the alias symbol was created, so we can ignore the error
// here.
}
})
.else(() => {
if (indexRight === 0) {
ctx.err.report(
Expand Down
Loading