Skip to content

Commit

Permalink
fix: check for same file references
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Feb 21, 2024
1 parent af1bf87 commit 62d37d7
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions graph/decl_deps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
type ClassDeclaration,
type FunctionDeclaration,
type Node,
ReferencedSymbol,
ReferencedSymbolEntry,
type SourceFile,
SyntaxKind,
type VariableDeclaration,
} from "../deps/ts_morph.ts"
import { encodeVSCodeURI } from "./vscode_uri.ts"

export type Declaration =
| VariableDeclaration
Expand Down Expand Up @@ -41,31 +42,28 @@ const getTopDecl = (ref: ReferencedSymbolEntry): Declaration | undefined => {
)
}

const equals = (a: Node, b: Node) => encodeVSCodeURI(a) === encodeVSCodeURI(b)

/**
* Find all references in other files
*
* TODO: also check for same file? (not neccesary for default exports)
* TODO: use isDefinition()?
*/
const getActualReferences =
(file: SourceFile) => (symbol: ReferencedSymbol): ReferencedSymbolEntry[] =>
symbol
.getReferences()
// .filter(ref => ref.getNode() !== symbol.getDefinition().getNode())
.filter((ref) => ref.getSourceFile().getFilePath() !== file.getFilePath())
.filter((ref) =>
ref.getNode().getParent()?.getKindName() !== "ImportClause"
)
const getActualReferences = (
symbol: ReferencedSymbol,
): ReferencedSymbolEntry[] =>
symbol
.getReferences()
.filter((ref) => !equals(ref.getNode(), symbol.getDefinition().getNode()))
.filter((ref) =>
ref.getNode().getParent()?.getKindName() !== "ImportClause"
)

const getReferencedDecls = (node: Declaration): Declaration[] => {
const file = node.getSourceFile()
const varDecls = node.findReferences()
.flatMap(getActualReferences(file))
const getReferencedDecls = (node: Declaration): Declaration[] =>
node.findReferences()
.flatMap(getActualReferences)
.flatMap((x) => getTopDecl(x) ?? [])

return varDecls
}

/**
* Recursively query **direct** variable references into key-value map.
*/
Expand Down

0 comments on commit 62d37d7

Please sign in to comment.