-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 || []) { | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -707,3 +681,46 @@ function isEqualArray<T>(a: T[], b: T[]): boolean { | |
} | ||
return true | ||
} | ||
|
||
function declarationName(node: ts.Node): ts.Node | undefined { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted into an independent function to satisfy the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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