From be6c53d9adff76a5d5d69ca6d8a8296e0dacf54b Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Thu, 20 Jan 2022 18:23:52 +0900 Subject: [PATCH] Update AST typings to es2022 --- src/ast/nodes.ts | 108 +++++++++++++++++++++++++++++++++----- src/script-setup/index.ts | 15 ++++-- 2 files changed, 106 insertions(+), 17 deletions(-) diff --git a/src/ast/nodes.ts b/src/ast/nodes.ts index 3ca9d63b..f8624b82 100644 --- a/src/ast/nodes.ts +++ b/src/ast/nodes.ts @@ -55,8 +55,12 @@ export type ESLintNode = | ESLintPattern | ESLintClassBody | ESLintMethodDefinition + | ESLintPropertyDefinition + | ESLintStaticBlock + | ESLintPrivateIdentifier | ESLintModuleDeclaration | ESLintModuleSpecifier + | ESLintImportExpression | ESLintLegacyRestProperty /** @@ -165,6 +169,7 @@ export interface ESLintForOfStatement extends HasLocation, HasParent { left: ESLintVariableDeclaration | ESLintPattern right: ESLintExpression body: ESLintStatement + await: boolean } export interface ESLintLabeledStatement extends HasLocation, HasParent { @@ -202,7 +207,7 @@ export interface ESLintTryStatement extends HasLocation, HasParent { export interface ESLintCatchClause extends HasLocation, HasParent { type: "CatchClause" - param: ESLintPattern + param: ESLintPattern | null body: ESLintBlockStatement } @@ -251,7 +256,11 @@ export interface ESLintClassDeclaration extends HasLocation, HasParent { export interface ESLintClassBody extends HasLocation, HasParent { type: "ClassBody" - body: ESLintMethodDefinition[] + body: ( + | ESLintMethodDefinition + | ESLintPropertyDefinition + | ESLintStaticBlock + )[] } export interface ESLintMethodDefinition extends HasLocation, HasParent { @@ -259,9 +268,29 @@ export interface ESLintMethodDefinition extends HasLocation, HasParent { kind: "constructor" | "method" | "get" | "set" computed: boolean static: boolean - key: ESLintExpression + key: ESLintExpression | ESLintPrivateIdentifier value: ESLintFunctionExpression } +export interface ESLintPropertyDefinition extends HasLocation, HasParent { + type: "PropertyDefinition" + computed: boolean + static: boolean + key: ESLintExpression | ESLintPrivateIdentifier + value: ESLintExpression | null +} + +export interface ESLintStaticBlock + extends HasLocation, + HasParent, + Omit { + type: "StaticBlock" + body: ESLintStatement[] +} + +export interface ESLintPrivateIdentifier extends HasLocation, HasParent { + type: "PrivateIdentifier" + name: string +} export type ESLintModuleDeclaration = | ESLintImportDeclaration @@ -287,7 +316,7 @@ export interface ESLintImportDeclaration extends HasLocation, HasParent { export interface ESLintImportSpecifier extends HasLocation, HasParent { type: "ImportSpecifier" - imported: ESLintIdentifier + imported: ESLintIdentifier | ESLintStringLiteral local: ESLintIdentifier } @@ -301,6 +330,11 @@ export interface ESLintImportNamespaceSpecifier extends HasLocation, HasParent { local: ESLintIdentifier } +export interface ESLintImportExpression extends HasLocation, HasParent { + type: "ImportExpression" + source: ESLintExpression +} + export interface ESLintExportNamedDeclaration extends HasLocation, HasParent { type: "ExportNamedDeclaration" declaration?: ESLintDeclaration | null @@ -310,8 +344,8 @@ export interface ESLintExportNamedDeclaration extends HasLocation, HasParent { export interface ESLintExportSpecifier extends HasLocation, HasParent { type: "ExportSpecifier" - local: ESLintIdentifier - exported: ESLintIdentifier + local: ESLintIdentifier | ESLintStringLiteral + exported: ESLintIdentifier | ESLintStringLiteral } export interface ESLintExportDefaultDeclaration extends HasLocation, HasParent { @@ -321,6 +355,7 @@ export interface ESLintExportDefaultDeclaration extends HasLocation, HasParent { export interface ESLintExportAllDeclaration extends HasLocation, HasParent { type: "ExportAllDeclaration" + exported: ESLintIdentifier | ESLintStringLiteral | null source: ESLintLiteral } @@ -354,15 +389,55 @@ export interface ESLintIdentifier extends HasLocation, HasParent { type: "Identifier" name: string } - -export interface ESLintLiteral extends HasLocation, HasParent { +interface ESLintLiteralBase extends HasLocation, HasParent { type: "Literal" - value: string | boolean | null | number | RegExp + value: string | boolean | null | number | RegExp | bigint regex?: { pattern: string flags: string } + bigint?: string } +export interface ESLintStringLiteral extends ESLintLiteralBase { + value: string + regex?: undefined + bigint?: undefined +} +export interface ESLintBooleanLiteral extends ESLintLiteralBase { + value: boolean + regex?: undefined + bigint?: undefined +} +export interface ESLintNullLiteral extends ESLintLiteralBase { + value: null + regex?: undefined + bigint?: undefined +} +export interface ESLintNumberLiteral extends ESLintLiteralBase { + value: number + regex?: undefined + bigint?: undefined +} +export interface ESLintRegExpLiteral extends ESLintLiteralBase { + value: null | RegExp + regex: { + pattern: string + flags: string + } + bigint?: undefined +} +export interface ESLintBigIntLiteral extends ESLintLiteralBase { + value: null | bigint + regex?: undefined + bigint: string +} +export type ESLintLiteral = + | ESLintStringLiteral + | ESLintBooleanLiteral + | ESLintNullLiteral + | ESLintNumberLiteral + | ESLintRegExpLiteral + | ESLintBigIntLiteral export interface ESLintThisExpression extends HasLocation, HasParent { type: "ThisExpression" @@ -447,7 +522,7 @@ export interface ESLintBinaryExpression extends HasLocation, HasParent { | "&" | "in" | "instanceof" - left: ESLintExpression + left: ESLintExpression | ESLintPrivateIdentifier right: ESLintExpression } @@ -467,6 +542,9 @@ export interface ESLintAssignmentExpression extends HasLocation, HasParent { | "|=" | "^=" | "&=" + | "||=" + | "&&=" + | "??=" left: ESLintPattern right: ESLintExpression } @@ -480,7 +558,7 @@ export interface ESLintUpdateExpression extends HasLocation, HasParent { export interface ESLintLogicalExpression extends HasLocation, HasParent { type: "LogicalExpression" - operator: "||" | "&&" + operator: "||" | "&&" | "??" left: ESLintExpression right: ESLintExpression } @@ -514,7 +592,7 @@ export interface ESLintMemberExpression extends HasLocation, HasParent { optional: boolean computed: boolean object: ESLintExpression | ESLintSuper - property: ESLintExpression + property: ESLintExpression | ESLintPrivateIdentifier } export interface ESLintYieldExpression extends HasLocation, HasParent { @@ -544,7 +622,7 @@ export interface ESLintTemplateElement extends HasLocation, HasParent { type: "TemplateElement" tail: boolean value: { - cooked: string + cooked: string | null raw: string } } @@ -607,9 +685,11 @@ export interface ESLintAssignmentPattern extends HasLocation, HasParent { right: ESLintExpression } +export type ESLintChainElement = ESLintCallExpression | ESLintMemberExpression + export interface ESLintChainExpression extends HasLocation, HasParent { type: "ChainExpression" - expression: ESLintExpression + expression: ESLintChainElement } /** diff --git a/src/script-setup/index.ts b/src/script-setup/index.ts index afb1fa2f..59d056f1 100644 --- a/src/script-setup/index.ts +++ b/src/script-setup/index.ts @@ -627,12 +627,18 @@ function getScriptSetupCodeBlocks( (t) => t.range[0] === spec.local.range[0], exportTokenIndex, ) - checkToken(tokens[localTokenIndex], spec.local.name) + checkToken( + tokens[localTokenIndex], + (spec.local as ESLintIdentifier).name, + ) const asToken = tokens[localTokenIndex + 1] checkToken(asToken, "as") restoreTokens.push(asToken) const exportedToken = tokens[localTokenIndex + 2] - checkToken(exportedToken, spec.exported.name) + checkToken( + exportedToken, + (spec.exported as ESLintIdentifier).name, + ) restoreTokens.push(exportedToken) processAppend( statementCodeBlocks, @@ -687,7 +693,10 @@ function getScriptSetupCodeBlocks( ) { const spec = body.specifiers[index] const local = locals[index] - if (spec.local.name !== local.name) { + if ( + (spec.local as ESLintIdentifier).name !== + local.name + ) { return null } map.set(spec, local)