Skip to content

Commit

Permalink
feat: Add .findReferencesAsNodes() to deprecate .getReferencingNodes(…
Browse files Browse the repository at this point in the history
…) in next major.

Using `findReferencesAsNodes()` so that the method is more discoverable when using `findReferences()`.
  • Loading branch information
dsherret committed Jun 1, 2018
1 parent a4dd7d2 commit a8a731a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion docs/navigation/finding-references.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ If you just went the nodes that reference the named/nameable declaration, then u
following method:

```ts
const nodes = classDeclaration.getReferencingNodes();
const nodes = classDeclaration.findReferencesAsNodes();
```
13 changes: 11 additions & 2 deletions src/compiler/base/name/ReferenceFindableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ export type ReferenceFindableNodeExtensionType = Node<ts.Node & { name?: ts.Prop

export interface ReferenceFindableNode {
/**
* Finds the references of the node.
* Finds the references of the definition of the node.
*/
findReferences(): ReferencedSymbol[];
/**
* Finds the nodes that reference the definition of the node.
*/
findReferencesAsNodes(): Node[];
/**
* Gets the nodes that reference the definition of the node.
* @deprecated Use `findReferencesAsNodes()`
*/
getReferencingNodes(): Node[];
}
Expand All @@ -23,8 +28,12 @@ export function ReferenceFindableNode<T extends Constructor<ReferenceFindableNod
return this.global.languageService.findReferences(getNodeForReferences(this));
}

findReferencesAsNodes() {
return this.global.languageService.findReferencesAsNodes(getNodeForReferences(this));
}

getReferencingNodes() {
return this.global.languageService.getDefinitionReferencingNodes(getNodeForReferences(this));
return this.findReferencesAsNodes();
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/class/ClassDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ export class ClassDeclaration extends ClassDeclarationBase<ts.ClassDeclaration>
if (nameNode == null)
return classes;

for (const node of nameNode.getReferencingNodes()) {
for (const node of nameNode.findReferencesAsNodes()) {
const nodeParent = node.getParentIfKind(SyntaxKind.ExpressionWithTypeArguments);
if (nodeParent == null)
continue;
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/tools/LanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class LanguageService {
* Finds the nodes that reference the definition(s) of the specified node.
* @param node - Node.
*/
getDefinitionReferencingNodes(node: Node) {
findReferencesAsNodes(node: Node) {
const references = this.findReferences(node);
return ArrayUtils.from(getReferencingNodes());

Expand All @@ -205,6 +205,15 @@ export class LanguageService {
}
}

/**
* Finds the nodes that reference the definition(s) of the specified node.
* @param node - Node.
* @deprecated Use `findReferencesAsNodes()`.
*/
getDefinitionReferencingNodes(node: Node) {
return this.findReferencesAsNodes(node);
}

/**
* Finds references based on the specified position.
* @param sourceFile - Source file.
Expand Down
3 changes: 2 additions & 1 deletion src/next-major-deprecations.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Deprecations in the next major

None.
* getDefinitionReferencingNodes(node)
* getReferencingNodes()
6 changes: 3 additions & 3 deletions src/tests/compiler/base/name/nameableNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ describe(nameof(NameableNode), () => {
});
});

describe(nameof<NameableNode>(n => n.getReferencingNodes), () => {
describe(nameof<NameableNode>(n => n.findReferencesAsNodes), () => {
it("should find all the references and exclude the definition when there is a name", () => {
const {firstChild, project} = getInfoFromText<ClassDeclaration>("class MyClass {}\nconst reference = MyClass;");
const secondSourceFile = project.createSourceFile("second.ts", "const reference2 = MyClass;");
const referencingNodes = firstChild.getReferencingNodes();
const referencingNodes = firstChild.findReferencesAsNodes();
expect(referencingNodes.length).to.equal(2);
expect(referencingNodes[0].getParentOrThrow().getText()).to.equal("reference = MyClass");
expect(referencingNodes[1].getParentOrThrow().getText()).to.equal("reference2 = MyClass");
Expand All @@ -118,7 +118,7 @@ describe(nameof(NameableNode), () => {
it("should find all the references and exclude the definition when there isn't a name", () => {
const {firstChild, project} = getInfoFromText<ClassDeclaration>("export default class {}", { filePath: "/MyClass.ts" });
const secondSourceFile = project.createSourceFile("/second.ts", "import MyClass from './MyClass';\nconst reference2 = MyClass;");
const referencingNodes = firstChild.getReferencingNodes();
const referencingNodes = firstChild.findReferencesAsNodes();
expect(referencingNodes.length).to.equal(2);
expect(referencingNodes[0].getParentOrThrow().getParentOrThrow().getText()).to.equal("import MyClass from './MyClass';");
expect(referencingNodes[1].getParentOrThrow().getText()).to.equal("reference2 = MyClass");
Expand Down
4 changes: 2 additions & 2 deletions src/tests/compiler/base/name/namedNodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ describe(nameof(NamedNode), () => {
});
});

describe(nameof<NamedNode>(n => n.getReferencingNodes), () => {
describe(nameof<NamedNode>(n => n.findReferencesAsNodes), () => {
it("should find all the references and exclude the definition", () => {
const {firstChild, sourceFile, project} = getInfoFromText<FunctionDeclaration>("function myFunction() {}\nconst reference = myFunction;");
const secondSourceFile = project.createSourceFile("second.ts", "const reference2 = myFunction;");
const referencingNodes = firstChild.getReferencingNodes();
const referencingNodes = firstChild.findReferencesAsNodes();
expect(referencingNodes.length).to.equal(2);
expect(referencingNodes[0].getParentOrThrow().getText()).to.equal("reference = myFunction");
expect(referencingNodes[1].getParentOrThrow().getText()).to.equal("reference2 = myFunction");
Expand Down
4 changes: 2 additions & 2 deletions src/tests/compiler/common/identifierTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ const t = MyNamespace.MyClass;
});
});

describe(nameof<Identifier>(n => n.getReferencingNodes), () => {
describe(nameof<Identifier>(n => n.findReferencesAsNodes), () => {
it("should find all the references and exclude the definition", () => {
const {firstChild, sourceFile, project} = getInfoFromText<FunctionDeclaration>("function myFunction() {}\nconst reference = myFunction;");
const secondSourceFile = project.createSourceFile("second.ts", "const reference2 = myFunction;");
const referencingNodes = firstChild.getNameNode().getReferencingNodes();
const referencingNodes = firstChild.getNameNode().findReferencesAsNodes();
expect(referencingNodes.length).to.equal(2);
expect(referencingNodes[0].getParentOrThrow().getText()).to.equal("reference = myFunction");
expect(referencingNodes[1].getParentOrThrow().getText()).to.equal("reference2 = myFunction");
Expand Down

0 comments on commit a8a731a

Please sign in to comment.