Error while replacing identifier of a PropertyAccessExpression
See original GitHub issueHey @dsherret, long time no talk! How’s it going?
I’m back doing ts transforms (yay!), but ran into an issue while trying to replace the object name (expression) in a PropertyAccessExpression.
I’m basically trying to convert that.something
to this.something
, but if the there are 4 chained PropertyAccessExpressions, I get an error. Try running this code:
import Project, { Identifier, PropertyAccessExpression, SyntaxKind, TypeGuards } from 'ts-simple-ast';
const project = new Project();
const sourceFile = project.createSourceFile( 'my-file.ts', `
that.something.something2.something3; // <-- too nested!
` );
const thatDotSomething: PropertyAccessExpression = sourceFile
.getDescendantsOfKind( SyntaxKind.PropertyAccessExpression )
.find( propAccess => TypeGuards.isIdentifier( propAccess.getExpression() ) )!;
const thatIdentifier = thatDotSomething.getExpression() as Identifier;
thatIdentifier.replaceWithText( 'this' );
console.log( sourceFile.getFullText() );
I get the error: Error: Error replacing tree: Should not have more children left over.
Oddly enough, when I remove one property and change that.something.something2.something3
to that.something.something2
, the code works fine.
Can you take a look into this? Thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to use the typescript.createPropertyAccess function in ...
PropertyAccessExpression { // If we are in a namespace or a sourcefile that is a module check if ... createIdentifier(factoryClassName))), // Replace the ......
Read more >entity framework 6 - The properties expression is not valid ...
In my case, changing the following values in the mapper worked. ... problem in any case shown is the same: The "custom identification...
Read more >node_modules/typescript/lib/tsserverlibrary.d.ts - Google Git
Text of identifier, but if the identifier begins with two underscores, ... export interface PropertyAccessExpression extends MemberExpression, ...
Read more >Expressions | Microsoft Learn
However, in many cases it is possible to change an expression's ... In this case, it is an error for the identifier to...
Read more >https://raw.githubusercontent.com/Microsoft/TypeSc...
i.e. if you don't change them, then when // you parse a sub-production, ... Note: any errors at the end of the file...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dsherret Thanks for the quick fix! Definitely good to be back - I really love this stuff and I missed it 😃
Very good to know about the return value from
replaceWithText()
- didn’t realize that it did that. Many thanks, and keep up the great work!Hey @gregjacobs! I’ve been doing well thanks. I hope you have been too. Nice to see you back, but sorry that it’s because of a bug!
Thanks for reporting this. I’ve fixed it now in 12.6.1 (available now). Just use the return value of
replaceWithText
to get the newThisKeyword
as the previousIdentifier
will be forgotten.