Getting full name of property/type
See original GitHub issueI’m currently using the findReferences() method as part of a refactoring tool I’m creating to remove namespaces from a large project.
This method correctly finds all of the project references however it only returns the part being searched and does not include the namespaces to the left so I am unable to rewrite them. Grabbing all the names to the left where they exist has proven to be far more difficult than I expected.
For example, the findReferences() return “something” rather than “foo.bar.something”.
After trying various combinations of getParent, getAncestors and getPreviousSiblings I’ve been unable to get something that reliably pulls everything from the left (leaving any class properties on the right.)
I tried something like this with very limited success.
var node=element.getNode();
if (TypeGuards.isTypeReferenceNode(node) || TypeGuards.isPropertyAccessExpression(node)){
//property with no left namespace, leave alone. broken DOESN'T seem to pick up all single names
console.log("nochange: ", node.getKindName(), node.getText());
} else {
var ancestor = node.getFirstAncestorByKind(ts.SyntaxKind.TypeReference);
if (ancestor){
//appears to work.
ancestor.replaceWithText(getNewQualifiedname(ancestor.getText()))
} else {
var ancestor2 = node.getFirstAncestorByKind(ts.SyntaxKind.PropertyAccessExpression);
if (ancestor2){
//not much success here.
ancestor2.replaceWithText(getNewQualifiedname(ancestor2.getText()))
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (7 by maintainers)
Top GitHub Comments
Nice, I’m glad it’s mostly working out! Let me know what you find about the special character being introduced when you get a chance. I will maybe spend some time on it this weekend to try to reproduce it.
By the way, I created a web-based AST viewer that you can use to help understand the tree. It’s very basic for now, but shows the relationship between the nodes (it took me about 6 hours to do). I’ll work on improving it more in the future: https://dsherret.github.io/ts-ast-viewer/
I don’t believe there’s anything still outstanding in this issue so I’m closing it. Please reopen it if that’s not correct.