fix: cannot read properties of undefined (reading 'line') #16516
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1, Fixes #2
Hey there, I'm using @babel/generator and this error appeared for me:
@babel\generator\lib\printer.js:599
const nodeStartLine = hasLoc ? nodeLoc.start.line : 0;
TypeError: Cannot read properties of undefined (reading 'line')
So I went straight to this node_module file and added this line of code:
const nodeStartLine = hasLoc ? (nodeLoc?.start?.line ? nodeLoc.start.line : 0) : 0;
Giving more context:
I'm using:
node: v20.11.1
npm: v20.2.4
I used the parser with the object { sourceType: 'module' },
to generate the AST and then I performed some functions with traverse, then when running the generator the above error occurs with the file below:
I thought about this solution, because I noticed that sometimes AST doesn't put either start, or start.line.