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 7 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 @@ -48,6 +48,12 @@ interface ParseOptions {
*/
debugLevel?: boolean | ('typescript-eslint' | 'eslint' | 'typescript')[];

/**
* Causes the parser to an error if it receives an invalid AST from TypeScript.
* This case only usually occurs when attempting to lint invalid code.
*/
errorOnInvalidAST?: boolean;

/**
* Cause the parser to error if it encounters an unknown AST node type (useful for testing).
* This case only usually occurs when TypeScript releases new features.
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,3 @@
export default {
errorOnInvalidAST: true,
};
@@ -0,0 +1 @@
export class { }
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ClassDeclaration _error_ export-missing-name TSESTree - Error 1`] = `[TSError: A class declaration without the 'default' modifier must have a name.]`;
@@ -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`] = `"Both errored"`;
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ExportAllDeclaration _error_ non-string-source TSESTree - Error 1`] = `[TSError: Module specifier must be a string literal.]`;
exports[`AST Fixtures declaration ExportAllDeclaration _error_ non-string-source TSESTree - Error 1`] = `"NO ERROR"`;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

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

exports[`AST Fixtures declaration ImportDeclaration _error_ non-string-source TSESTree - Error 1`] = `[TSError: Module specifier must be a string literal.]`;
exports[`AST Fixtures declaration ImportDeclaration _error_ non-string-source TSESTree - Error 1`] = `"NO ERROR"`;
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration ImportDeclaration _error_ non-string-source Error Alignment 1`] = `"Both errored"`;
exports[`AST Fixtures declaration ImportDeclaration _error_ non-string-source Error Alignment 1`] = `"Babel errored but TSESTree didn't"`;
@@ -0,0 +1,3 @@
export default {
errorOnInvalidAST: true,
};
@@ -0,0 +1 @@
@decl enum Test {}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSEnumDeclaration _error_ decorator TSESTree - Error 1`] = `[TSError: Decorators are not valid here.]`;
@@ -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`] = `"Both errored"`;
@@ -0,0 +1,3 @@
export default {
errorOnInvalidAST: true,
};
@@ -0,0 +1 @@
@decl interface Test {};
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSInterfaceDeclaration _error_ decorator TSESTree - Error 1`] = `[TSError: Decorators are not valid here.]`;
@@ -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`] = `"Both errored"`;
@@ -0,0 +1,3 @@
export default {
errorOnInvalidAST: true,
};
@@ -0,0 +1 @@
@decl type Test = {};
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration TSTypeAliasDeclaration _error_ decorator TSESTree - Error 1`] = `[TSError: Decorators are not valid here.]`;
@@ -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`] = `"Both errored"`;
@@ -0,0 +1,3 @@
export default {
errorOnInvalidAST: true,
};
@@ -0,0 +1 @@
@decl type Test = {};
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ decorator TSESTree - Error 1`] = `[TSError: Decorators are not valid here.]`;
@@ -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`] = `"Both errored"`;
@@ -0,0 +1,3 @@
export default {
errorOnInvalidAST: true,
};
@@ -0,0 +1 @@
const;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures declaration VariableDeclaration _error_ no-variables TSESTree - Error 1`] = `[TSError: A variable declaration list must have at least one variable declarator.]`;
@@ -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`] = `"Both errored"`;
@@ -0,0 +1,3 @@
export default {
errorOnInvalidAST: true,
};
@@ -0,0 +1,3 @@
{
throw
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`AST Fixtures statement ThrowStatement _error_ missing-argument TSESTree - Error 1`] = `[TSError: A throw statement must throw an expression.]`;
@@ -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`] = `"Both errored"`;
2 changes: 2 additions & 0 deletions packages/ast-spec/tests/fixtures-with-differences-errors.shot
Expand Up @@ -7,10 +7,12 @@ Object {
"declaration/ClassDeclaration/fixtures/_error_/missing-extends-type-param/fixture.ts",
"declaration/ClassDeclaration/fixtures/_error_/missing-type-param/fixture.ts",
"declaration/ExportAllDeclaration/fixtures/_error_/kind-type/fixture.ts",
"declaration/ExportAllDeclaration/fixtures/_error_/non-string-source/fixture.ts",
"declaration/ExportAllDeclaration/fixtures/_error_/type-kind/fixture.ts",
"declaration/ExportNamedDeclaration/fixtures/_error_/anonymous-class/fixture.ts",
"declaration/ExportNamedDeclaration/fixtures/_error_/assertion/fixture.ts",
"declaration/FunctionDeclaration/fixtures/_error_/missing-type-param/fixture.ts",
"declaration/ImportDeclaration/fixtures/_error_/non-string-source/fixture.ts",
"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",
Expand Down
1 change: 1 addition & 0 deletions packages/ast-spec/tests/util/parsers/typescript-estree.ts
Expand Up @@ -13,6 +13,7 @@ export function parseTSESTree(
loc: true,
range: true,
tokens: true,
errorOnInvalidAST: fixture.config.errorOnInvalidAST,
});
const { tokens: _, comments: __, ...program } = result;

Expand Down
2 changes: 2 additions & 0 deletions packages/ast-spec/typings/global.d.ts
Expand Up @@ -11,4 +11,6 @@ interface ASTFixtureConfig {
* The value should be a description of why there isn't support - for example a github issue URL.
*/
readonly expectBabelToNotSupport?: string;

readonly errorOnInvalidAST?: boolean;
}
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, {
errorOnInvalidAST: parseSettings.errorOnInvalidAST || false,
errorOnUnknownASTType: parseSettings.errorOnUnknownASTType || false,
shouldPreserveNodeMaps,
});
Expand Down