Skip to content

Commit e17a427

Browse files
Extract isDefinition helper (sourcegraph#196)
1 parent 1a6776e commit e17a427

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

Diff for: .eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ module.exports = {
1010
ignorePatterns: ['temp', 'scip.ts', 'snapshots'],
1111
rules: {
1212
'no-sync': 'off',
13+
'jsdoc/check-indentation': 'off',
14+
'class-methods-use-this': 'error',
1315
},
1416
}

Diff for: src/FileIndexer.ts

+46-29
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ export class FileIndexer {
100100
private visitSymbolOccurrence(node: ts.Node, sym: ts.Symbol): void {
101101
const range = Range.fromNode(node).toLsif()
102102
let role = 0
103-
const isDefinition = this.declarationName(node.parent) === node
104-
if (isDefinition) {
103+
const isDefinitionNode = isDefinition(node)
104+
if (isDefinitionNode) {
105105
role |= scip.scip.SymbolRole.Definition
106106
}
107107
for (const declaration of sym?.declarations || []) {
@@ -118,7 +118,7 @@ export class FileIndexer {
118118
symbol_roles: role,
119119
})
120120
)
121-
if (isDefinition) {
121+
if (isDefinitionNode) {
122122
this.addSymbolInformation(node, sym, declaration, scipSymbol)
123123
this.handleShorthandPropertyDefinition(declaration, range)
124124
this.handleObjectBindingPattern(node, range)
@@ -290,32 +290,6 @@ export class FileIndexer {
290290
}
291291
return relationships
292292
}
293-
private declarationName(node: ts.Node): ts.Node | undefined {
294-
if (
295-
ts.isBindingElement(node) ||
296-
ts.isEnumDeclaration(node) ||
297-
ts.isEnumMember(node) ||
298-
ts.isVariableDeclaration(node) ||
299-
ts.isPropertyDeclaration(node) ||
300-
ts.isAccessor(node) ||
301-
ts.isMethodSignature(node) ||
302-
ts.isMethodDeclaration(node) ||
303-
ts.isPropertySignature(node) ||
304-
ts.isFunctionDeclaration(node) ||
305-
ts.isModuleDeclaration(node) ||
306-
ts.isPropertyAssignment(node) ||
307-
ts.isShorthandPropertyAssignment(node) ||
308-
ts.isParameter(node) ||
309-
ts.isTypeParameterDeclaration(node) ||
310-
ts.isTypeAliasDeclaration(node) ||
311-
ts.isInterfaceDeclaration(node) ||
312-
ts.isClassDeclaration(node)
313-
) {
314-
return node.name
315-
}
316-
return undefined
317-
}
318-
319293
private scipSymbol(node: ts.Node): ScipSymbol {
320294
const fromCache: ScipSymbol | undefined =
321295
this.globalSymbolTable.get(node) || this.localSymbolTable.get(node)
@@ -711,3 +685,46 @@ function isEqualArray<T>(a: T[], b: T[]): boolean {
711685
}
712686
return true
713687
}
688+
689+
function declarationName(node: ts.Node): ts.Node | undefined {
690+
if (
691+
ts.isBindingElement(node) ||
692+
ts.isEnumDeclaration(node) ||
693+
ts.isEnumMember(node) ||
694+
ts.isVariableDeclaration(node) ||
695+
ts.isPropertyDeclaration(node) ||
696+
ts.isAccessor(node) ||
697+
ts.isMethodSignature(node) ||
698+
ts.isMethodDeclaration(node) ||
699+
ts.isPropertySignature(node) ||
700+
ts.isFunctionDeclaration(node) ||
701+
ts.isModuleDeclaration(node) ||
702+
ts.isPropertyAssignment(node) ||
703+
ts.isShorthandPropertyAssignment(node) ||
704+
ts.isParameter(node) ||
705+
ts.isTypeParameterDeclaration(node) ||
706+
ts.isTypeAliasDeclaration(node) ||
707+
ts.isInterfaceDeclaration(node) ||
708+
ts.isClassDeclaration(node)
709+
) {
710+
return node.name
711+
}
712+
return undefined
713+
}
714+
715+
/**
716+
* For example:
717+
*
718+
* const a = 1
719+
* ^ node
720+
* ^ node.parent.name
721+
* ^^^^^ node.parent
722+
*
723+
* function a(): void {}
724+
* ^ node
725+
* ^ node.parent.name
726+
* ^^^^^^^^^^^^^^^^^^^^^ node.parent
727+
*/
728+
function isDefinition(node: ts.Node): boolean {
729+
return declarationName(node.parent) === node
730+
}

0 commit comments

Comments
 (0)