typescript-eslint: Parsing: strictly enforce the produced AST matches the spec and enforce most "error recovery" parsing errors

{
  "rules": {
    "no-throw-literal": "error"
  }
}
throw

Just that in a file somewhere

Expected Result

This should be a parse-error and should not be linted on.

Actual Result

Parser creates a ThrowStatement with argument set to null, which breaks rules downstream, see https://github.com/eslint/eslint/pull/13143 for context

Additional Info

[Error - 1:41:32 PM] TypeError: Cannot read property 'type' of null
Occurred while linting [fileName].ts:132
    at Object.couldBeError (eslint/lib/rules/utils/ast-utils.js:1262:22)
    at ThrowStatement (eslint/lib/rules/no-throw-literal.js:38:31)
    at eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (eslint/lib/linter/node-event-generator.js:254:26)
    at NodeEventGenerator.applySelectors (eslint/lib/linter/node-event-generator.js:283:22)
    at NodeEventGenerator.enterNode (eslint/lib/linter/node-event-generator.js:297:14)
    at CodePathAnalyzer.enterNode (eslint/lib/linter/code-path-analysis/code-path-analyzer.js:634:23)
    at eslint/lib/linter/linter.js:936:32

Versions

package version
@typescript-eslint/parser ^2.13.0
TypeScript ^3.8.2
ESLint 5.16.0
node 12.16.0
npm 6.13.7

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 11
  • Comments: 22 (17 by maintainers)

Commits related to this issue

Most upvoted comments

The typescript parser is very forgiving. It does its best to figure out what you meant and not throw errors unless it really really has to.

It does this for a number of reasons, but mainly so that use cases that need to be failure tolerant are so (eg syntax highlighters).

It emits errors separately as what it calls diagnostics. These diagnostics are not exceptions - they are just data available as part of the parse.

However, in order to get these diagnostics, you need to use the program.

Before v3, we didn’t create a program unless you configured type-aware linting.

So it meant that we had no consistent way to get the syntax errors. So we just didn’t emit any…

Now a days we always have some form of a program. This means we could leverage TS to do some basic syntax validation.

But again - emitting errors is a breaking change for the typescript-estree package - meaning it’s a breaking change for the entire project.

I can’t remember if I’ve mentioned this before, but the thing I’d be testing is what is the performance of program.getSemanticDiagnostics.

That was the API that I implemented the old no-unused-vars-experimental rule with, and we found that it was way to slow for whole-codebase-linting-purposes. In some cases it would take ~50ms per file to return - which quickly adds up across a codebase (50ms * 1000 files = 50 seconds! Awful!!)

Code reference: https://github.com/typescript-eslint/typescript-eslint/blob/v4.33.0/packages/eslint-plugin/src/rules/no-unused-vars-experimental.ts#L296 Reference issue: https://github.com/typescript-eslint/typescript-eslint/issues/1335

I personally don’t mind some logic!

I don’t mind either way if there’s a way to make TS enforce it and we leverage that - the important thing is that our runtime output matches our types.

Our AST structure isn’t exactly 1:1 so we have some invariants we will want to enforce as we merge things and move things around.

we can definitely prep a PR to do this (that’s kind of the point of #2911 - laying the framework to do this easily), and we could land an option that’s default off.

But it wouldn’t get used until we set it on by default (which is breaking for both typescript-estree and parser).

As it stands right now typescript-estree is a bit of an unsafe mess that’s grown and grown over the years. It’s probably worth refactoring it before we start throwing in assertions.

Also worth noting that some of our AST types are inconsistent, so we’d ideally clean those up at the same time.

No problem. These issues mentioned here are actually not easy to reproduce with Prettier. The standalone throw parses only if it’s inside a block and not followed by a semicolon, and I can’t reproduce the issue with standalone import at all. I mean it’s already a SyntaxError.