-
Notifications
You must be signed in to change notification settings - Fork 12.8k
getTokenAtPosition throws "Cannot read property 'text' of undefined" #25505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The language service APIs (like I think we'd take a PR to address your suggestion (since it would be more efficient), but having any expectation that the APIs will work without parent pointers isn't something I'd take a hard dependency on. |
Fair enough, this is basically my first foray into the typescript code base so it's still unclear to me when parent pointers get set / when they can be expected to be set. By "not something you'd take a hard dependency on", do you mean that I should avoid writing a unit test for this case specifically, so as to not codify this practice too strictly? As soon as the Milestone is set to Community (as detailed here), I'll submit that PR. On a related note, I audited some similar function calls, and noticed a lot of the locations below could benefit from a similar treatment. In each instance, a method which accepts an optional sourceFile parameter is being called without a value for that parameters, but there's a sourceFile in scope that would likely work. If someone more familiar with the code base wants to give me a thumbs up, i'll test each change and submit a PR that updates all those that don't break anything. Methods checked:
|
We should always provide |
@Andy-MS Sure thing! I'll submit one PR tonight with the change I originally proposed, and one in the next few days where I've made more sweeping changes based on the places I highlighted above. I'll try to carefully review whether the Should I write a unit test for that first PR? if so, where do those kinds of unit tests belong? Those in |
Like @DanielRosenwasser said, all services should be considered to need parent pointers set. So this change would just be an optimization and not need new tests. |
…#25505) For performance reasons, we should always pass sourceFile to getChildren if available.
@Andy-MS I'd like to submit a bunch of similar changes after identifying other places that could safely benefit from this. Should I submit one PR for each? one large PR that fixes many at once? Should I submit an issue first detailing each of them, before submitting the PR, or just link it to this one again? Trying to be a good citizen here. :) (I've found 30+ places that could adopt a similar change. This decision would be way above my pay grade, so to speak, but given how widespread this is, would it be worth considering putting a sourceFile property on Node to allow this short-circuiting-if-available behavior without threading optional parameters all through the API and having to remember to pass them around explicitly?) |
TypeScript Version: master (Commit 10b174a)
Search Terms: getChildren, getTokenAtPosition
Code
Expected behavior:
node
refers to the NumericLiteral token with text value '1' from position 12 to 14.Actual behavior:
Related Issues:
#23917
#14808
Comments:
As mentioned in #23917 by @Andy-MS, it seems that it is best practice to pass a sourceFile to getChildren and related methods, for two reasons:
parent
nodes, which may not have been set yet, and can lead to the exception aboveparent
nodes are set, it results in wasted CPU timeThe method
getTokenAtPositionWorker
(seen in-part below) has a non-optional sourceFile parameter which it passes to getStart, but does not pass to getChildren.I'm happy to submit a pull request. I've already made and manually tested the changes locally and everything is working fine, including existing tests. However, I could use some guidance on where and how to add a test for this change.
The text was updated successfully, but these errors were encountered: