diff --git a/packages/eslint-plugin/src/rules/class-literal-property-style.ts b/packages/eslint-plugin/src/rules/class-literal-property-style.ts index a4500cdfdbf..0a257427ec1 100644 --- a/packages/eslint-plugin/src/rules/class-literal-property-style.ts +++ b/packages/eslint-plugin/src/rules/class-literal-property-style.ts @@ -23,10 +23,7 @@ const printNodeModifiers = ( const isSupportedLiteral = ( node: TSESTree.Node, ): node is TSESTree.LiteralExpression => { - if ( - node.type === AST_NODE_TYPES.Literal || - node.type === AST_NODE_TYPES.BigIntLiteral - ) { + if (node.type === AST_NODE_TYPES.Literal) { return true; } diff --git a/packages/eslint-plugin/src/rules/no-inferrable-types.ts b/packages/eslint-plugin/src/rules/no-inferrable-types.ts index 898a3294fd7..2b84414dfcd 100644 --- a/packages/eslint-plugin/src/rules/no-inferrable-types.ts +++ b/packages/eslint-plugin/src/rules/no-inferrable-types.ts @@ -118,7 +118,8 @@ export default util.createRule({ return ( isFunctionCall(unwrappedInit, 'BigInt') || - unwrappedInit.type === AST_NODE_TYPES.BigIntLiteral + (unwrappedInit.type === AST_NODE_TYPES.Literal && + 'bigint' in unwrappedInit) ); } diff --git a/packages/eslint-plugin/src/rules/prefer-optional-chain.ts b/packages/eslint-plugin/src/rules/prefer-optional-chain.ts index 6adc3f1d9c8..0d5176b6f10 100644 --- a/packages/eslint-plugin/src/rules/prefer-optional-chain.ts +++ b/packages/eslint-plugin/src/rules/prefer-optional-chain.ts @@ -306,7 +306,6 @@ export default util.createRule({ break; case AST_NODE_TYPES.Literal: - case AST_NODE_TYPES.BigIntLiteral: case AST_NODE_TYPES.TemplateLiteral: propertyText = sourceCode.getText(node.property); break; @@ -353,7 +352,6 @@ const ALLOWED_MEMBER_OBJECT_TYPES: ReadonlySet = new Set([ AST_NODE_TYPES.ThisExpression, ]); const ALLOWED_COMPUTED_PROP_TYPES: ReadonlySet = new Set([ - AST_NODE_TYPES.BigIntLiteral, AST_NODE_TYPES.Identifier, AST_NODE_TYPES.Literal, AST_NODE_TYPES.MemberExpression, diff --git a/packages/parser/tests/lib/__snapshots__/javascript.ts.snap b/packages/parser/tests/lib/__snapshots__/javascript.ts.snap index 495f3b26b05..654b09e21ab 100644 --- a/packages/parser/tests/lib/__snapshots__/javascript.ts.snap +++ b/packages/parser/tests/lib/__snapshots__/javascript.ts.snap @@ -35993,6 +35993,8 @@ Object { } `; +exports[`javascript fixtures/experimentalDynamicImport/error-dynamic-import-params.src 1`] = `"Dynamic import must have one specifier as an argument."`; + exports[`javascript fixtures/experimentalObjectRestSpread/arg-spread.src 1`] = ` Object { "$id": 6, diff --git a/packages/shared-fixtures/fixtures/javascript/experimentalDynamicImport/error-dynamic-import-params.src.js b/packages/shared-fixtures/fixtures/javascript/experimentalDynamicImport/error-dynamic-import-params.src.js new file mode 100644 index 00000000000..c5d6277887f --- /dev/null +++ b/packages/shared-fixtures/fixtures/javascript/experimentalDynamicImport/error-dynamic-import-params.src.js @@ -0,0 +1 @@ +import('foo', '') diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index dc4a1a0091a..c2ce21755af 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -1808,6 +1808,20 @@ export class Converter { } case SyntaxKind.CallExpression: { + if (node.expression.kind === SyntaxKind.ImportKeyword) { + if (node.arguments.length !== 1) { + throw createError( + this.ast, + node.arguments.pos, + 'Dynamic import must have one specifier as an argument.', + ); + } + return this.createNode(node, { + type: AST_NODE_TYPES.ImportExpression, + source: this.convertChild(node.arguments[0]), + }); + } + const callee = this.convertChild(node.expression); const args = node.arguments.map(el => this.convertChild(el)); let result; @@ -1915,14 +1929,17 @@ export class Converter { } case SyntaxKind.BigIntLiteral: { - const result = this.createNode(node, { - type: AST_NODE_TYPES.BigIntLiteral, - raw: '', - value: '', + const range = getRange(node, this.ast); + const rawValue = this.ast.text.slice(range[0], range[1]); + const bigint = rawValue.slice(0, -1); // remove suffix `n` + const value = typeof BigInt !== 'undefined' ? BigInt(bigint) : null; + return this.createNode(node, { + type: AST_NODE_TYPES.Literal, + raw: rawValue, + value: value, + bigint: value === null ? bigint : String(value), + range, }); - result.raw = this.ast.text.slice(result.range[0], result.range[1]); - result.value = result.raw.slice(0, -1); // remove suffix `n` - return result; } case SyntaxKind.RegularExpressionLiteral: { diff --git a/packages/typescript-estree/src/ts-estree/ast-node-types.ts b/packages/typescript-estree/src/ts-estree/ast-node-types.ts index 13f2485819f..d630398ec71 100644 --- a/packages/typescript-estree/src/ts-estree/ast-node-types.ts +++ b/packages/typescript-estree/src/ts-estree/ast-node-types.ts @@ -5,7 +5,6 @@ export enum AST_NODE_TYPES { AssignmentExpression = 'AssignmentExpression', AssignmentPattern = 'AssignmentPattern', AwaitExpression = 'AwaitExpression', - BigIntLiteral = 'BigIntLiteral', BinaryExpression = 'BinaryExpression', BlockStatement = 'BlockStatement', BreakStatement = 'BreakStatement', @@ -36,6 +35,7 @@ export enum AST_NODE_TYPES { Import = 'Import', ImportDeclaration = 'ImportDeclaration', ImportDefaultSpecifier = 'ImportDefaultSpecifier', + ImportExpression = 'ImportExpression', ImportNamespaceSpecifier = 'ImportNamespaceSpecifier', ImportSpecifier = 'ImportSpecifier', JSXAttribute = 'JSXAttribute', diff --git a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts index bb00046c334..5585c290284 100644 --- a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts +++ b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts @@ -16,7 +16,6 @@ export interface EstreeToTsNodeTypes { | ts.BinaryExpression | ts.ParameterDeclaration; [AST_NODE_TYPES.AwaitExpression]: ts.AwaitExpression; - [AST_NODE_TYPES.BigIntLiteral]: ts.BigIntLiteral; [AST_NODE_TYPES.BinaryExpression]: ts.BinaryExpression; [AST_NODE_TYPES.BlockStatement]: ts.Block; [AST_NODE_TYPES.BreakStatement]: ts.BreakStatement; @@ -73,6 +72,7 @@ export interface EstreeToTsNodeTypes { [AST_NODE_TYPES.Import]: ts.ImportExpression; [AST_NODE_TYPES.ImportDeclaration]: ts.ImportDeclaration; [AST_NODE_TYPES.ImportDefaultSpecifier]: ts.ImportClause; + [AST_NODE_TYPES.ImportExpression]: ts.CallExpression; [AST_NODE_TYPES.ImportNamespaceSpecifier]: ts.NamespaceImport; [AST_NODE_TYPES.ImportSpecifier]: ts.ImportSpecifier; [AST_NODE_TYPES.JSXAttribute]: ts.JsxAttribute; @@ -98,7 +98,8 @@ export interface EstreeToTsNodeTypes { | ts.RegularExpressionLiteral | ts.JsxText | ts.NullLiteral - | ts.BooleanLiteral; + | ts.BooleanLiteral + | ts.BigIntLiteral; [AST_NODE_TYPES.LogicalExpression]: ts.BinaryExpression; [AST_NODE_TYPES.MemberExpression]: | ts.PropertyAccessExpression diff --git a/packages/typescript-estree/src/ts-estree/ts-estree.ts b/packages/typescript-estree/src/ts-estree/ts-estree.ts index bb7944b4bae..bd0b4063619 100644 --- a/packages/typescript-estree/src/ts-estree/ts-estree.ts +++ b/packages/typescript-estree/src/ts-estree/ts-estree.ts @@ -175,6 +175,7 @@ export type Node = | Import | ImportDeclaration | ImportDefaultSpecifier + | ImportExpression | ImportNamespaceSpecifier | ImportSpecifier | JSXAttribute @@ -395,12 +396,13 @@ export type LeftHandSideExpression = | TSAsExpression | ArrowFunctionExpression; export type Literal = + | BigIntLiteral | BooleanLiteral | NumberLiteral | NullLiteral | RegExpLiteral | StringLiteral; -export type LiteralExpression = BigIntLiteral | Literal | TemplateLiteral; +export type LiteralExpression = Literal | TemplateLiteral; export type MemberExpression = | MemberExpressionComputedName | MemberExpressionNonComputedName; @@ -624,7 +626,7 @@ interface FunctionSignatureBase extends BaseNode { interface LiteralBase extends BaseNode { raw: string; - value: boolean | number | RegExp | string | null; + value: string | boolean | null | number | RegExp | bigint; regex?: { pattern: string; flags: string; @@ -784,7 +786,9 @@ export interface AwaitExpression extends BaseNode { } export interface BigIntLiteral extends LiteralBase { - type: AST_NODE_TYPES.BigIntLiteral; + type: AST_NODE_TYPES.Literal; + value: bigint | null; + bigint: string; } export interface BinaryExpression extends BinaryExpressionBase { @@ -968,6 +972,11 @@ export interface ImportDefaultSpecifier extends BaseNode { local: Identifier; } +export interface ImportExpression extends BaseNode { + type: AST_NODE_TYPES.ImportExpression; + source: Expression; +} + export interface ImportNamespaceSpecifier extends BaseNode { type: AST_NODE_TYPES.ImportNamespaceSpecifier; local: Identifier; diff --git a/packages/typescript-estree/src/visitor-keys.ts b/packages/typescript-estree/src/visitor-keys.ts index a3622fc4713..bba4a2f378b 100644 --- a/packages/typescript-estree/src/visitor-keys.ts +++ b/packages/typescript-estree/src/visitor-keys.ts @@ -3,6 +3,8 @@ import * as eslintVisitorKeys from 'eslint-visitor-keys'; export const visitorKeys = eslintVisitorKeys.unionWith({ // Additional estree nodes. Import: [], + // ES2020 + ImportExpression: ['source'], // Additional Properties. ArrayPattern: ['decorators', 'elements', 'typeAnnotation'], ArrowFunctionExpression: ['typeParameters', 'params', 'returnType', 'body'], @@ -40,7 +42,6 @@ export const visitorKeys = eslintVisitorKeys.unionWith({ JSXSpreadChild: ['expression'], // Additional Nodes. - BigIntLiteral: [], ClassProperty: ['decorators', 'key', 'typeAnnotation', 'value'], Decorator: ['expression'], OptionalCallExpression: ['callee', 'typeParameters', 'arguments'], diff --git a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts index e454fe6fb6d..eb247042a8f 100644 --- a/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts +++ b/packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts @@ -37,8 +37,8 @@ class FixturesTester { * Utility to generate a FixturePatternConfig object containing the glob pattern for specific subsections of the fixtures/ directory, * including the capability to ignore specific nested patterns. * - * @param {string} fixturesSubPath the sub-path within the fixtures/ directory - * @param {CreateFixturePatternConfig?} config an optional configuration object with optional sub-paths to ignore and/or parse with sourceType: module + * @param fixturesSubPath the sub-path within the fixtures/ directory + * @param config an optional configuration object with optional sub-paths to ignore and/or parse with sourceType: module */ public addFixturePatternConfig( fixturesSubPath: string, @@ -186,19 +186,7 @@ tester.addFixturePatternConfig('javascript/function', { ], }); -tester.addFixturePatternConfig('javascript/bigIntLiterals', { - ignore: [ - /** - * new BigIntLiteral type - * @see https://github.com/estree/estree/blob/master/es2020.md#bigintliteral - * @see https://github.com/typescript-eslint/typescript-eslint/pull/1389 - */ - 'binary', - 'decimal', - 'hex', - 'octal', - ], -}); +tester.addFixturePatternConfig('javascript/bigIntLiterals'); tester.addFixturePatternConfig('javascript/binaryLiterals'); tester.addFixturePatternConfig('javascript/blockBindings'); @@ -229,16 +217,7 @@ tester.addFixturePatternConfig('javascript/destructuring-and-forOf'); tester.addFixturePatternConfig('javascript/destructuring-and-spread'); tester.addFixturePatternConfig('javascript/experimentalAsyncIteration'); -tester.addFixturePatternConfig('javascript/experimentalDynamicImport', { - ignore: [ - /** - * new ImportExpression type - * @see https://github.com/estree/estree/blob/master/es2020.md#importexpression - * @see https://github.com/typescript-eslint/typescript-eslint/pull/1389 - */ - 'dynamic-import', - ], -}); +tester.addFixturePatternConfig('javascript/experimentalDynamicImport'); tester.addFixturePatternConfig('javascript/exponentiationOperators'); tester.addFixturePatternConfig('javascript/experimentalOptionalCatchBinding'); diff --git a/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap index b2814f8b4ba..1ae708c8a40 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/javascript.ts.snap @@ -16491,6 +16491,7 @@ Object { "body": Array [ Object { "expression": Object { + "bigint": "1", "loc": Object { "end": Object { "column": 4, @@ -16506,8 +16507,8 @@ Object { 4, ], "raw": "0b1n", - "type": "BigIntLiteral", - "value": "0b1", + "type": "Literal", + "value": 1n, }, "loc": Object { "end": Object { @@ -16588,6 +16589,7 @@ Object { "body": Array [ Object { "expression": Object { + "bigint": "1", "loc": Object { "end": Object { "column": 2, @@ -16603,8 +16605,8 @@ Object { 2, ], "raw": "1n", - "type": "BigIntLiteral", - "value": "1", + "type": "Literal", + "value": 1n, }, "loc": Object { "end": Object { @@ -16685,6 +16687,7 @@ Object { "body": Array [ Object { "expression": Object { + "bigint": "1", "loc": Object { "end": Object { "column": 4, @@ -16700,8 +16703,8 @@ Object { 4, ], "raw": "0x1n", - "type": "BigIntLiteral", - "value": "0x1", + "type": "Literal", + "value": 1n, }, "loc": Object { "end": Object { @@ -16782,6 +16785,7 @@ Object { "body": Array [ Object { "expression": Object { + "bigint": "1", "loc": Object { "end": Object { "column": 4, @@ -16797,8 +16801,8 @@ Object { 4, ], "raw": "0o1n", - "type": "BigIntLiteral", - "value": "0o1", + "type": "Literal", + "value": 1n, }, "loc": Object { "end": Object { @@ -80423,44 +80427,6 @@ Object { }, }, "object": Object { - "arguments": Array [ - Object { - "loc": Object { - "end": Object { - "column": 12, - "line": 1, - }, - "start": Object { - "column": 7, - "line": 1, - }, - }, - "range": Array [ - 7, - 12, - ], - "raw": "'foo'", - "type": "Literal", - "value": "foo", - }, - ], - "callee": Object { - "loc": Object { - "end": Object { - "column": 6, - "line": 1, - }, - "start": Object { - "column": 0, - "line": 1, - }, - }, - "range": Array [ - 0, - 6, - ], - "type": "Import", - }, "loc": Object { "end": Object { "column": 13, @@ -80471,12 +80437,30 @@ Object { "line": 1, }, }, - "optional": false, "range": Array [ 0, 13, ], - "type": "CallExpression", + "source": Object { + "loc": Object { + "end": Object { + "column": 12, + "line": 1, + }, + "start": Object { + "column": 7, + "line": 1, + }, + }, + "range": Array [ + 7, + 12, + ], + "raw": "'foo'", + "type": "Literal", + "value": "foo", + }, + "type": "ImportExpression", }, "optional": false, "property": Object { @@ -80738,6 +80722,8 @@ Object { } `; +exports[`javascript fixtures/experimentalDynamicImport/error-dynamic-import-params.src 1`] = `"Dynamic import must have one specifier as an argument."`; + exports[`javascript fixtures/experimentalObjectRestSpread/arg-spread.src 1`] = ` Object { "body": Array [ diff --git a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap index 2cff4d0b441..bdc9a9a52dc 100644 --- a/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap +++ b/packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap @@ -585,6 +585,15 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/experimentalDynamicImport/dynamic-import.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; +exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/experimentalDynamicImport/error-dynamic-import-params.src 1`] = ` +Object { + "column": 7, + "index": 7, + "lineNumber": 1, + "message": "Dynamic import must have one specifier as an argument.", +} +`; + exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/experimentalObjectRestSpread/arg-spread.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`; exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/experimentalObjectRestSpread/destructuring-assign-mirror.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;