Skip to content

Commit

Permalink
feat(typescript-estree): align nodes with estree 2020 (#1389)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and bradzacher committed May 21, 2020
1 parent 0e0010f commit aff5b62
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 94 deletions.
Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/no-inferrable-types.ts
Expand Up @@ -118,7 +118,8 @@ export default util.createRule<Options, MessageIds>({

return (
isFunctionCall(unwrappedInit, 'BigInt') ||
unwrappedInit.type === AST_NODE_TYPES.BigIntLiteral
(unwrappedInit.type === AST_NODE_TYPES.Literal &&
'bigint' in unwrappedInit)
);
}

Expand Down
2 changes: 0 additions & 2 deletions packages/eslint-plugin/src/rules/prefer-optional-chain.ts
Expand Up @@ -306,7 +306,6 @@ export default util.createRule<Options, MessageIds>({
break;

case AST_NODE_TYPES.Literal:
case AST_NODE_TYPES.BigIntLiteral:
case AST_NODE_TYPES.TemplateLiteral:
propertyText = sourceCode.getText(node.property);
break;
Expand Down Expand Up @@ -353,7 +352,6 @@ const ALLOWED_MEMBER_OBJECT_TYPES: ReadonlySet<AST_NODE_TYPES> = new Set([
AST_NODE_TYPES.ThisExpression,
]);
const ALLOWED_COMPUTED_PROP_TYPES: ReadonlySet<AST_NODE_TYPES> = new Set([
AST_NODE_TYPES.BigIntLiteral,
AST_NODE_TYPES.Identifier,
AST_NODE_TYPES.Literal,
AST_NODE_TYPES.MemberExpression,
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/tests/lib/__snapshots__/javascript.ts.snap
Expand Up @@ -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,
Expand Down
@@ -0,0 +1 @@
import('foo', '')
31 changes: 24 additions & 7 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -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<TSESTree.ImportExpression>(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;
Expand Down Expand Up @@ -1915,14 +1929,17 @@ export class Converter {
}

case SyntaxKind.BigIntLiteral: {
const result = this.createNode<TSESTree.BigIntLiteral>(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<TSESTree.BigIntLiteral>(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: {
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/ts-estree/ast-node-types.ts
Expand Up @@ -5,7 +5,6 @@ export enum AST_NODE_TYPES {
AssignmentExpression = 'AssignmentExpression',
AssignmentPattern = 'AssignmentPattern',
AwaitExpression = 'AwaitExpression',
BigIntLiteral = 'BigIntLiteral',
BinaryExpression = 'BinaryExpression',
BlockStatement = 'BlockStatement',
BreakStatement = 'BreakStatement',
Expand Down Expand Up @@ -36,6 +35,7 @@ export enum AST_NODE_TYPES {
Import = 'Import',
ImportDeclaration = 'ImportDeclaration',
ImportDefaultSpecifier = 'ImportDefaultSpecifier',
ImportExpression = 'ImportExpression',
ImportNamespaceSpecifier = 'ImportNamespaceSpecifier',
ImportSpecifier = 'ImportSpecifier',
JSXAttribute = 'JSXAttribute',
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions packages/typescript-estree/src/ts-estree/ts-estree.ts
Expand Up @@ -175,6 +175,7 @@ export type Node =
| Import
| ImportDeclaration
| ImportDefaultSpecifier
| ImportExpression
| ImportNamespaceSpecifier
| ImportSpecifier
| JSXAttribute
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/typescript-estree/src/visitor-keys.ts
Expand Up @@ -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'],
Expand Down Expand Up @@ -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'],
Expand Down
29 changes: 4 additions & 25 deletions packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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');

Expand Down

0 comments on commit aff5b62

Please sign in to comment.