Tolerate invalid identifier syntax errors in list parsing
See original GitHub issueSearch Terms
parser variable name list identifiers
Suggestion
Instead of aborting parsing a list when an invalid identifier is reached, have the parser emit a syntax error but otherwise parsing. In other words: it should successfully parse lists such as variable declarations as if the identifiers were valid, with the one difference of emitting a syntax error that doesn’t affect parsing.
Use Cases
Issues such as #11648 and #19352 pop up from situations where the parser attempts to read in a list, such as one of variable declarations, and cannot because an identifier is invalid.
const case = 123;
// ~~~~ 'case' is not allowed as a variable declaration name.(1389)
// ~ Variable declaration expected.(1134)
// ~~~ Variable declaration expected.(1134)
Although #40105 improved the first error message (previously it was also Variable declaration expected.
), the underlying issue remains: the parser aborts reading in the list, resulting in an odd error message and surprising emitted JavaScript.
const ;
123;
Examples
Collecting a few examples from the linked issues…
const data = { in: 1 };
const { in } = data;
for (const case of [1, 2, 3]) console.log(case);
const case = 123;
let delete = true;
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript/JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top GitHub Comments
it depends really on context. If, for example, you saw
function foo()
there, it would likely not be a good idea to reinterpretfunction
as an identifier. Keywords are actually super useful for compilers because the majority case is that they are actually keywords, and thus can be used to resync without needing any special parser logic about if that’s the right thing to do.Of course, as you’ve identified that’s not always the best thing. However, rather than unilaterally reinterpretting as an identifier (Which will likely degrade a bunch of error correction), i might conditionally reinterpret based on what’s around the keyword. in other words, reinterpret if it seems highly likely that it’s an identifier, and that you would have even worse issues treating it as a keyword.
However, one could imagine something unrelated to “list parsing” which is that the parser could reinterpret all unexpected keywords as
Identifier
nodes after issuing an error message on that Node.For example, in the following example, you’ll get two errors:
Whereas we could instead have provided an error like
Now, reinterpreting every unexpected keyword as an Identifier actually does sound appealing, but I don’t know what sort of impact that would have on the rest of the compiler. I can imagine that in the language service, we rely on specific keywords to guide us on what sorts of constructs we might encounter, and we do some other error recovery on specific keywords being present, so there’d be quite a bit of churn there.
Maybe @CyrusNajmabadi has some specific thoughts on this one.