-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Specific diagnostic suggestions for unexpected keyword or identifier #43005
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
Specific diagnostic suggestions for unexpected keyword or identifier #43005
Conversation
61a4bf3
to
ec81e4d
Compare
ec81e4d
to
0c967f9
Compare
Is there any corpus of syntactically incorrect code that we could use to figure out how this changes errors? For the number of new error messages, there isn't a lot of churn in our existing tests, which makes me think our coverage isn't very good. |
@sandersn I haven't found one. Looking around at Stryker was a fun curiosity but there isn't any prior art to go off of. Is there something you'd like me to do? I can set up some sample code given parameters for what you're interested in, such as size of the test case. |
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.
A couple to take a closer look at. Looks pretty nice overall, though!
tests/baselines/reference/derivedClassSuperCallsInNonConstructorMembers.errors.txt
Outdated
Show resolved
Hide resolved
tests/baselines/reference/derivedClassSuperCallsInNonConstructorMembers.errors.txt
Show resolved
Hide resolved
tests/baselines/reference/interfacesWithPredefinedTypesAsNames.errors.txt
Outdated
Show resolved
Hide resolved
tests/baselines/reference/parserErrorRecovery_IncompleteMemberVariable2.errors.txt
Outdated
Show resolved
Hide resolved
tests/baselines/reference/parserUnterminatedGeneric2.errors.txt
Outdated
Show resolved
Hide resolved
tests/baselines/reference/templateStringInModuleName.errors.txt
Outdated
Show resolved
Hide resolved
tests/baselines/reference/templateStringInModuleName.errors.txt
Outdated
Show resolved
Hide resolved
tests/baselines/reference/parserComputedPropertyName33.errors.txt
Outdated
Show resolved
Hide resolved
@JoshuaKGoldberg I was hoping for a brainstorming session leading to serendipity. Thanks for brainstorming; maybe next time we'll hit on a great idea. I know other parsers have used fuzzers to good effect, so that might be a worthwhile thing for us to do as a standalone project. Edit Second attempt for clarity: I was asking for you (and any onlookers) to brainstorm ideas for finding syntactically incorrect program text, not with a high expectation of success, but hoping for a small chance. Which you did! Your fuzzing idea is better than I hoped for, although it's probably not worth getting it set up just for this one PR. |
declare module `M1` { | ||
~~~~~~~ | ||
!!! error TS2304: Cannot find name 'declare'. | ||
~~~~~~ | ||
!!! error TS1005: ';' expected. | ||
~~~~~~ | ||
!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. |
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 becomes unfortunate because you will now possibly get a semantic error. Ideally, we would just allow no-substitution template strings in these positions and gracefully handle them later in the compiler.
I'm not sure if it's that big of a problem though. I'd hold off on any changes here.
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.
In hindsight, I’m actually surprised this wasn’t one of the places that IllusionMH made work with NoSubstitutionTemplateLiterals. I thought we hit basically everything that didn’t conflict with the ES spec. Why even bother issuing a grammar error? But that’s a discussion for another PR 🙃
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.
A few clarification requests on this thread:
- "possibly get a semantic error" -- is giving a
parseErrorAt
with theModule declaration names may only use ' or " quoted strings
not going to stop that? I'm under the impression this PR only changes which diagnostic is emitted, not the phase. - I'm inferring "no-substitution template strings" to be template strings that don't have any
${...}
s in them? - By "hold off on any changes here" do you mean I should leave the PR as-is, or revert the logic back to the way it was before this PR?
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.
Ping @andrewbranch @DanielRosenwasser 👋 , I think this PR is ready for re-review pending my questions in this thread? 🙏
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’m also not sure what Daniel meant on this one. This shouldn’t change semantic errors since the parse tree isn’t changing, as far as I can tell. My approval on the PR still stands, just want to make sure Daniel is happy with the changes to address his feedback.
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.
Ping, @DanielRosenwasser? 🙏
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 meant leave the PR as-is, sorry!
Module declaration names may only use ' or " quoted strings.
Don't know if I caught this diagnostic - I think that should be sufficient!
Co-authored-by: Daniel Rosenwasser <[email protected]>
I think that function name is my only nit. Otherwise this seems good to go! |
function parseSemicolonAfterPropertyName(name: PropertyName, type: TypeNode | undefined, initializer: Expression | undefined) { | ||
switch (token()) { | ||
case SyntaxKind.AtToken: | ||
parseErrorAtCurrentToken(Diagnostics.Decorators_must_precede_the_name_and_all_keywords_of_property_declarations); |
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.
We should only report an error here if there's no preceding line terminator. This is valid JS:
class C {
a
b
c() {}
}
ASI inserts a ;
between a
and b
, and between b
and c()
. I would expect the same to be true for decorators:
class C {
a
@dec b
c() {}
}
This should be legal since ASI would insert a ;
between a
and @dec
, however now it is an error.
Fixes #25429
I started off by special casing the generic
';' expected
message for just the "parser sees a word instead of a semicolon" case... but that exposed a ton of test cases in the baselines where different unexpected words were equally or even more confusing with the new error message. There are some baselines that aren't strictly better in this new version but for fear of bloating this PR I tried to stay away from parser behavior changes and limit to only:Each of these errors are improvements over previous scenarios that gave a
';' expected
complaint and generally fall into one of three categories:Error messages could definitely use a round of proofreading from someone who knows better than me how to write them...