Skip to content

Extract isDefinition helper #196

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

Merged
merged 1 commit into from
Oct 28, 2022
Merged
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
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ module.exports = {
ignorePatterns: ['temp', 'scip.ts', 'snapshots'],
rules: {
'no-sync': 'off',
'jsdoc/check-indentation': 'off',
'class-methods-use-this': 'error',
Copy link
Member Author

@valerybugakov valerybugakov Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up on this comment by @varungandhi-src. This rule forbids class methods without instance context usage. @olafurpg, let me know what you think about that!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really nice. Big +1 from me

},
}
75 changes: 46 additions & 29 deletions src/FileIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export class FileIndexer {
private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
const range = Range.fromNode(node).toLsif()
let role = 0
const isDefinition = this.declarationName(node.parent) === node
if (isDefinition) {
const isDefinitionNode = isDefinition(node)
if (isDefinitionNode) {
role |= scip.scip.SymbolRole.Definition
}
for (const declaration of sym?.declarations || []) {
Expand All @@ -118,7 +118,7 @@ export class FileIndexer {
symbol_roles: role,
})
)
if (isDefinition) {
if (isDefinitionNode) {
this.addSymbolInformation(node, sym, declaration, scipSymbol)
this.handleShorthandPropertyDefinition(declaration, range)
this.handleObjectBindingPattern(node, range)
Expand Down Expand Up @@ -290,32 +290,6 @@ export class FileIndexer {
}
return relationships
}
private declarationName(node: ts.Node): ts.Node | undefined {
if (
ts.isBindingElement(node) ||
ts.isEnumDeclaration(node) ||
ts.isEnumMember(node) ||
ts.isVariableDeclaration(node) ||
ts.isPropertyDeclaration(node) ||
ts.isAccessor(node) ||
ts.isMethodSignature(node) ||
ts.isMethodDeclaration(node) ||
ts.isPropertySignature(node) ||
ts.isFunctionDeclaration(node) ||
ts.isModuleDeclaration(node) ||
ts.isPropertyAssignment(node) ||
ts.isShorthandPropertyAssignment(node) ||
ts.isParameter(node) ||
ts.isTypeParameterDeclaration(node) ||
ts.isTypeAliasDeclaration(node) ||
ts.isInterfaceDeclaration(node) ||
ts.isClassDeclaration(node)
) {
return node.name
}
return undefined
}

private scipSymbol(node: ts.Node): ScipSymbol {
const fromCache: ScipSymbol | undefined =
this.globalSymbolTable.get(node) || this.localSymbolTable.get(node)
Expand Down Expand Up @@ -707,3 +681,46 @@ function isEqualArray<T>(a: T[], b: T[]): boolean {
}
return true
}

function declarationName(node: ts.Node): ts.Node | undefined {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted into an independent function to satisfy the class-methods-use-this ESLint rule.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a big improvement in readability to have a toplevel function instead of an instance method.

if (
ts.isBindingElement(node) ||
ts.isEnumDeclaration(node) ||
ts.isEnumMember(node) ||
ts.isVariableDeclaration(node) ||
ts.isPropertyDeclaration(node) ||
ts.isAccessor(node) ||
ts.isMethodSignature(node) ||
ts.isMethodDeclaration(node) ||
ts.isPropertySignature(node) ||
ts.isFunctionDeclaration(node) ||
ts.isModuleDeclaration(node) ||
ts.isPropertyAssignment(node) ||
ts.isShorthandPropertyAssignment(node) ||
ts.isParameter(node) ||
ts.isTypeParameterDeclaration(node) ||
ts.isTypeAliasDeclaration(node) ||
ts.isInterfaceDeclaration(node) ||
ts.isClassDeclaration(node)
) {
return node.name
}
return undefined
}

/**
* For example:
*
* const a = 1
* ^ node
* ^ node.parent.name
* ^^^^^ node.parent
*
* function a(): void {}
* ^ node
* ^ node.parent.name
* ^^^^^^^^^^^^^^^^^^^^^ node.parent
*/
function isDefinition(node: ts.Node): boolean {
return declarationName(node.parent) === node
}