question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error while replacing identifier of a PropertyAccessExpression

See original GitHub issue

Hey @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:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
gregjacobscommented, Jul 17, 2018

@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!

1reaction
dsherretcommented, Jul 16, 2018

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 new ThisKeyword as the previous Identifier will be forgotten.

const thatIdentifier = ...;
const thisKeyword = thatIdentifier.replaceWithText("this");

thatIdentifier.getText(); // throws because node was forgotten since the kind changed
thisKeyword.getText();    // ok
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found