question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Tolerate invalid identifier syntax errors in list parsing

See original GitHub issue

Search 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:open
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
CyrusNajmabadicommented, Sep 2, 2020

it depends really on context. If, for example, you saw function foo() there, it would likely not be a good idea to reinterpret function 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.

2reactions
DanielRosenwassercommented, Aug 31, 2020

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:

let x = export + 100;
//      ~~~~~~ Expression expected.
//             ~ Variable declaration expected.

Whereas we could instead have provided an error like

let x = export + 100;
//      ~~~~~~ Unexpected 'export' keyword.

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP parse/syntax errors; and how to solve them
The parser complains about the contained single quoted 'string' , because it usually expects a literal identifier / key there. More precisely it's...
Read more >
Chapter 2. Syntactic Analysis (Parsing) - Esprima
Esprima parser takes a string representing a valid JavaScript program and produces a ... tolerant, Boolean, false, Tolerate a few cases of syntax...
Read more >
Improved Errors on Invalid Variable Names - Goldblog
Improving the first error message emitted for invalid variable identifiers.
Read more >
Don't Panic! Better, Fewer, Syntax Errors for LR Parsers - arXiv
are thus called with a parsing stack and a sequence of remaining input (which we represent as a list of tokens): they can...
Read more >
4 Syntax and basic data types
Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found