-
Notifications
You must be signed in to change notification settings - Fork 12.9k
fix: correct name length criterion for spelling fixes #49575
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
Conversation
Compiler tests are failing with baselines that seem unrelated to what I did here. I suppose I can't just accept them, right? |
Can you accept the rest of the baselines? |
tests/baselines/reference/doYouNeedToChangeYourTargetLibraryES2016Plus.errors.txt
Outdated
Show resolved
Hide resolved
@DanielRosenwasser Done. Most of the changes are good IMO, except the one I highlighted. I mean the suggested name is similar, but the previous error message was better. Maybe the criteria need some tuning now? |
9471348
to
aab20de
Compare
@@ -1,12 +1,13 @@ | |||
tests/cases/compiler/weird.js(1,1): error TS2304: Cannot find name 'someFunction'. | |||
tests/cases/compiler/weird.js(1,1): error TS2552: Cannot find name 'someFunction'. Did you mean 'Function'? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this is also unfortunate; it's effectively never that someone wants to use Function
. Maybe this is rare enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This happens for prefixes with length 1-4, so aFunction
, myFunction
, ..., someFunction
but not longer prefixes - and for things like aPhunction
.
tests/baselines/reference/doYouNeedToChangeYourTargetLibraryES2016Plus.errors.txt
Outdated
Show resolved
Hide resolved
tests/baselines/reference/constructorParametersInVariableDeclarations.errors.txt
Show resolved
Hide resolved
private c = () => x; | ||
~ | ||
!!! error TS2304: Cannot find name 'x'. | ||
!!! error TS2301: Initializer of instance member variable 'c' cannot reference identifier 'x' declared in the constructor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but not this. Maybe the check for TS2301 should not happen in arrow function assignments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think x
should be in scope for this arrow function; x
in this example is just a constructor parameter.
I'm a little confused as to why all of these say that it's "declared in the constructor"; my brain is telling me that it's something that's assigned to this
in the constructor, when it's just some variable that happens to be in scope for the constructor function, so it's a little weird. I would think that "cannot find name x" is technically the more accurate message, because there's nothing you can do to x
in the constructor signature (not even private x: number
) that would make it accessible. (But, there is that comment that says "it's legal in this one situation", which is complicating.)
I guess I'd have to go back to when this diagnostic was added to see what the intent was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for the error is that pre-ES2021 (2022?), TS defaults to emitting class properties as assignments in the constructor (controllable separately from target
with useDefinedForClassFields
), so private a = x
would emit:
class A {
constructor(x) {
this.a = x
}
}
Then there are two problems:
- The thing you wrote should not find name 'x' -- so you'd expect the old error.
- But it will at runtime, because the emitted code in the constructor -- so you'd expect the new error.
The new error message is correct when useDefineForClassFields: false
, which it is for this file, and also extremely obscure.
Note that you can get the new error without the old one if you add var x = 1
at the beginning of the test. The new error is possibly even more confusing in this case.
So: the new error is correct and also horrible. I don't know if we should fix it (separately) though: the top hit for the error is a pretty good explanation from 2015. And over time fewer people will target non-compliant emit.
Minutes later: The error still shows up when useDefineForClassFields: true, which it shouldn't. I'll file a bug for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #49719
b: typeof x; // error | ||
~ | ||
!!! error TS2304: Cannot find name 'x'. | ||
!!! error TS2301: Initializer of instance member variable 'b' cannot reference identifier 'x' declared in the constructor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this is not an initializer 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just the message that was wrong; I guess this message didn't come up a lot so nobody realized the wording didn't make sense if the error location was inside a type node. It's an easy fix; just make another message that says "Type" instead. I'm pushing that now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, we could just say "instance member variable 'b' cannot reference identifier 'x' declared in the constructor."
That would be agnostic and remove a bit of logic I had to add to distinguish the cases, but not necessarily point to any one part (but, the error range will be there, so it might be fine).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks a lot better now, but I am a little concerned about the initializer messages.
private c = () => x; | ||
~ | ||
!!! error TS2304: Cannot find name 'x'. | ||
!!! error TS2301: Initializer of instance member variable 'c' cannot reference identifier 'x' declared in the constructor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think x
should be in scope for this arrow function; x
in this example is just a constructor parameter.
I'm a little confused as to why all of these say that it's "declared in the constructor"; my brain is telling me that it's something that's assigned to this
in the constructor, when it's just some variable that happens to be in scope for the constructor function, so it's a little weird. I would think that "cannot find name x" is technically the more accurate message, because there's nothing you can do to x
in the constructor signature (not even private x: number
) that would make it accessible. (But, there is that comment that says "it's legal in this one situation", which is complicating.)
I guess I'd have to go back to when this diagnostic was added to see what the intent was.
tests/baselines/reference/classMemberInitializerWithLamdaScoping4.errors.txt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of the concerns are addressed with the latest commit. I'm not sure if this https://github.com/microsoft/TypeScript/pull/49575/files/7effd54c0224207b142997ad98e9456eeb5be776#r908149168 is desired or not, but I'm also not sure how to fix it.
I won't be available for the next 2 weeks, so if anyone from the team wants to pick up and finish this, feel free to.
I just updated your branch to be up to date with |
// to a local variable in the constructor where the code will be emitted. Note that this is actually allowed | ||
// with ESNext+useDefineForClassFields because the scope semantics are different. | ||
error(errorLocation, | ||
errorLocation && propertyWithInvalidInitializer.type && textRangeContainsPositionInclusive(propertyWithInvalidInitializer.type, errorLocation.pos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like what I've done here; I just want to know "is errorLocation in the type of the property declaration". I was going to use findAncestor
, but it needs to stop once the property declaration node is reached, so that makes it a little more than a one liner, and just checking that the node contains the other node by position is fast. This is done in one other place, so it seemed okay.
Using
Math.min
effectively invalidated the name length criterion and prevented some otherwise good candidates from being suggested.Fixes #49557