Skip to content
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

feat(typescript-estree): added allowInvalidAST option to not throw on invalid tokens #6247

Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/architecture/TypeScript-ESTree.mdx
Expand Up @@ -31,6 +31,12 @@ Parses the given string of code with the options provided and returns an ESTree-

```ts
interface ParseOptions {
/**
* Prevents the parser from throwing an error if it receives an invalid AST from TypeScript.
* This case only usually occurs when attempting to lint invalid code.
*/
allowInvalidAST?: boolean;
bradzacher marked this conversation as resolved.
Show resolved Hide resolved

/**
* create a top-level comments array containing all comments
*/
Expand Down Expand Up @@ -99,7 +105,6 @@ interface ParseOptions {

const PARSE_DEFAULT_OPTIONS: ParseOptions = {
comment: false,
errorOnUnknownASTType: false,
filePath: 'estree.ts', // or 'estree.tsx', if you pass jsx: true
jsx: false,
loc: false,
Expand Down
@@ -0,0 +1 @@
export class { }
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ export-missing-name TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ export-missing-name Babel - Error 1`] = `[SyntaxError: A class name is required. (1:13)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ export-missing-name Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1 @@
@decl enum Test {}
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSEnumDeclaration _error_ decorator TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSEnumDeclaration _error_ decorator Babel - Error 1`] = `[SyntaxError: Leading decorators must be attached to a class declaration. (1:6)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSEnumDeclaration _error_ decorator Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1 @@
@decl interface Test {}
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSInterfaceDeclaration _error_ decorator TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSInterfaceDeclaration _error_ decorator Babel - Error 1`] = `[SyntaxError: Leading decorators must be attached to a class declaration. (1:6)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSInterfaceDeclaration _error_ decorator Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1 @@
@decl type Test = {};
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSTypeAliasDeclaration _error_ decorator TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSTypeAliasDeclaration _error_ decorator Babel - Error 1`] = `[SyntaxError: Leading decorators must be attached to a class declaration. (1:6)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSTypeAliasDeclaration _error_ decorator Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1 @@
@decl type Test = {};
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ decorator TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ decorator Babel - Error 1`] = `[SyntaxError: Leading decorators must be attached to a class declaration. (1:6)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ decorator Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1 @@
const;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ no-variables TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ no-variables Babel - Error 1`] = `[SyntaxError: Unexpected token (1:5)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ no-variables Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1,3 @@
{
throw
}
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument TSESTree - Error 1`] = `"NO ERROR"`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument Babel - Error 1`] = `[SyntaxError: Illegal newline after throw. (2:9)]`;
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
7 changes: 7 additions & 0 deletions packages/ast-spec/tests/fixtures-with-differences-errors.shot
Expand Up @@ -3,6 +3,7 @@
exports[`AST Fixtures List fixtures with Error differences 1`] = `
Object {
"Babel errored but TSESTree didn't": Set {
"declaration/ClassDeclaration/fixtures/_error_/export-missing-name/fixture.ts",
"declaration/ClassDeclaration/fixtures/_error_/implements-non-identifier/fixture.ts",
"declaration/ClassDeclaration/fixtures/_error_/missing-extends-type-param/fixture.ts",
"declaration/ClassDeclaration/fixtures/_error_/missing-type-param/fixture.ts",
Expand All @@ -14,13 +15,18 @@ Object {
"declaration/TSDeclareFunction/fixtures/_error_/async/fixture.ts",
"declaration/TSDeclareFunction/fixtures/_error_/declare-with-body/fixture.ts",
"declaration/TSDeclareFunction/fixtures/_error_/missing-type-param/fixture.ts",
"declaration/TSEnumDeclaration/fixtures/_error_/decorator/fixture.ts",
"declaration/TSImportEqualsDeclaration/fixtures/_error_/external-module-ref-non-string/fixture.ts",
"declaration/TSImportEqualsDeclaration/fixtures/_error_/import-kind/fixture.ts",
"declaration/TSInterfaceDeclaration/fixtures/_error_/decorator/fixture.ts",
"declaration/TSInterfaceDeclaration/fixtures/_error_/missing-extends/fixture.ts",
"declaration/TSInterfaceDeclaration/fixtures/_error_/missing-type-param/fixture.ts",
"declaration/TSInterfaceDeclaration/fixtures/_error_/non-identifier-extends/fixture.ts",
"declaration/TSTypeAliasDeclaration/fixtures/_error_/decorator/fixture.ts",
"declaration/TSTypeAliasDeclaration/fixtures/_error_/missing-type-parameter/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/decorator/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/missing-id-without-value/fixture.ts",
"declaration/VariableDeclaration/fixtures/_error_/no-variables/fixture.ts",
"element/AccessorProperty/fixtures/_error_/modifier-override-with-no-extends/fixture.ts",
"legacy-fixtures/basics/fixtures/_error_/abstract-class-with-abstract-static-constructor/fixture.ts",
"legacy-fixtures/basics/fixtures/_error_/abstract-class-with-override-property/fixture.ts",
Expand Down Expand Up @@ -80,6 +86,7 @@ Object {
"legacy-fixtures/errorRecovery/fixtures/_error_/solo-const/fixture.ts",
"legacy-fixtures/parameter-decorators/fixtures/_error_/parameter-array-pattern-decorator/fixture.ts",
"legacy-fixtures/parameter-decorators/fixtures/_error_/parameter-rest-element-decorator/fixture.ts",
"statement/ThrowStatement/fixtures/_error_/missing-argument/fixture.ts",
},
"TSESTree errored but Babel didn't": Set {
"declaration/ExportAllDeclaration/fixtures/_error_/named-non-identifier/fixture.ts",
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/tests/util/parsers/typescript-estree.ts
Expand Up @@ -8,6 +8,7 @@ export function parseTSESTree(
): ParserResponse {
try {
const result = parse(contents, {
allowInvalidAST: fixture.config.allowInvalidAST,
comment: false,
jsx: fixture.ext.endsWith('x'),
loc: true,
Expand Down
6 changes: 6 additions & 0 deletions packages/ast-spec/typings/global.d.ts
Expand Up @@ -4,6 +4,12 @@
* This is a convenient property because it saves us from a lot of `../`!
*/
interface ASTFixtureConfig {
/**
* Prevents the parser from throwing an error if it receives an invalid AST from TypeScript.
* This case only usually occurs when attempting to lint invalid code.
*/
readonly allowInvalidAST?: boolean;

/**
* Specifies that we expect that babel doesn't yet support the code in this fixture, so we expect that it will error.
* This should not be used if we expect babel to throw for this feature due to a valid parser error!
Expand Down
2 changes: 0 additions & 2 deletions packages/eslint-plugin/tests/rules/indent/indent.test.ts
Expand Up @@ -758,8 +758,6 @@ const div: JQuery<HTMLElement> = $('<div>')
`,
options: [2, { VariableDeclarator: { const: 3 } }],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/441
'const;',

// https://github.com/typescript-eslint/typescript-eslint/issues/1115
{
Expand Down
2 changes: 0 additions & 2 deletions packages/parser/tests/lib/parser.ts
Expand Up @@ -35,7 +35,6 @@ describe('parser', () => {
// ts-estree specific
filePath: 'isolated-file.src.ts',
project: 'tsconfig.json',
errorOnUnknownASTType: false,
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: 'tests/fixtures/services',
extraFileExtensions: ['.foo'],
Expand Down Expand Up @@ -89,7 +88,6 @@ describe('parser', () => {
// ts-estree specific
filePath: 'isolated-file.src.ts',
project: 'tsconfig.json',
errorOnUnknownASTType: false,
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: 'tests/fixtures/services',
extraFileExtensions: ['.foo'],
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/src/ast-converter.ts
Expand Up @@ -26,6 +26,7 @@ export function astConverter(
* Recursively convert the TypeScript AST into an ESTree-compatible AST
*/
const instance = new Converter(ast, {
allowInvalidAST: parseSettings.allowInvalidAST || false,
errorOnUnknownASTType: parseSettings.errorOnUnknownASTType || false,
shouldPreserveNodeMaps,
});
Expand Down