From 53d1dffaff509283d165f4d08e8ecd97956fc463 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Mon, 30 Dec 2019 14:57:04 +0100 Subject: [PATCH] use AST selector to simplify things --- build/lib/eslint/utils.js | 18 +++++------------- build/lib/eslint/utils.ts | 18 ++++++------------ 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/build/lib/eslint/utils.js b/build/lib/eslint/utils.js index 1cdd1837e89..ec59aef3b7d 100644 --- a/build/lib/eslint/utils.js +++ b/build/lib/eslint/utils.js @@ -4,7 +4,6 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); -const experimental_utils_1 = require("@typescript-eslint/experimental-utils"); function createImportRuleListener(validateImport) { function _checkImport(node) { if (node && node.type === 'Literal' && typeof node.value === 'string') { @@ -16,20 +15,13 @@ function createImportRuleListener(validateImport) { ImportDeclaration: (node) => { _checkImport(node.source); }, - // import('module').then(...) - CallExpression: (node) => { - var _a; - const { callee, arguments: args } = node; - if (callee.type === 'Import' && args.length > 0 && ((_a = args[0]) === null || _a === void 0 ? void 0 : _a.type) === 'Literal') { - _checkImport(args[0]); - } + // import('module').then(...) OR await import('module') + ['CallExpression[callee.type="Import"][arguments.length=1] > Literal']: (node) => { + _checkImport(node); }, // import foo = ... - [experimental_utils_1.AST_NODE_TYPES.TSImportEqualsDeclaration]: (node) => { - const { moduleReference } = node; - if (moduleReference.type === experimental_utils_1.AST_NODE_TYPES.TSExternalModuleReference) { - _checkImport(moduleReference.expression); - } + ['TSImportEqualsDeclaration > TSExternalModuleReference > Literal']: (node) => { + _checkImport(node); }, // export ?? from 'module' ExportAllDeclaration: (node) => { diff --git a/build/lib/eslint/utils.ts b/build/lib/eslint/utils.ts index c48e174b702..428832e9cf9 100644 --- a/build/lib/eslint/utils.ts +++ b/build/lib/eslint/utils.ts @@ -4,7 +4,7 @@ *--------------------------------------------------------------------------------------------*/ import * as eslint from 'eslint'; -import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; +import { TSESTree } from '@typescript-eslint/experimental-utils'; export function createImportRuleListener(validateImport: (node: TSESTree.Literal, value: string) => any): eslint.Rule.RuleListener { @@ -19,19 +19,13 @@ export function createImportRuleListener(validateImport: (node: TSESTree.Literal ImportDeclaration: (node: any) => { _checkImport((node).source); }, - // import('module').then(...) - CallExpression: (node: any) => { - const { callee, arguments: args } = node; - if ((callee.type) === 'Import' && args.length > 0 && args[0]?.type === 'Literal') { - _checkImport(args[0]); - } + // import('module').then(...) OR await import('module') + ['CallExpression[callee.type="Import"][arguments.length=1] > Literal']: (node: any) => { + _checkImport(node); }, // import foo = ... - [AST_NODE_TYPES.TSImportEqualsDeclaration]: (node: any) => { - const { moduleReference } = (node); - if (moduleReference.type === AST_NODE_TYPES.TSExternalModuleReference) { - _checkImport(((moduleReference).expression)); - } + ['TSImportEqualsDeclaration > TSExternalModuleReference > Literal']: (node: any) => { + _checkImport(node); }, // export ?? from 'module' ExportAllDeclaration: (node: any) => {