From 4f74fbf3d674ab9104880954af86f40acd88fb8e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 23 Dec 2019 19:20:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20crash=20on=20dynamic=20imp?= =?UTF-8?q?ort=20with=20identifier=20(#201)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/util/visit-import.js | 10 ++++++++++ tests/lib/rules/no-missing-import.js | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/util/visit-import.js b/lib/util/visit-import.js index 7379f852..905e5bda 100644 --- a/lib/util/visit-import.js +++ b/lib/util/visit-import.js @@ -42,6 +42,16 @@ module.exports = function visitImport( "ImportExpression", ]](node) { const sourceNode = node.source + + // skip `import(foo)` + if ( + node.type === "ImportExpression" && + sourceNode && + sourceNode.type !== "Literal" + ) { + return + } + const name = sourceNode && stripImportPathParams(sourceNode.value) if (name && (includeCore || !resolve.isCore(name))) { targets.push(new ImportTarget(sourceNode, name, options)) diff --git a/tests/lib/rules/no-missing-import.js b/tests/lib/rules/no-missing-import.js index 9ee1271c..b8ff1f15 100644 --- a/tests/lib/rules/no-missing-import.js +++ b/tests/lib/rules/no-missing-import.js @@ -166,6 +166,17 @@ ruleTester.run("no-missing-import", rule, { options: [{ resolvePaths: ["tests"] }], env: { node: true }, }, + + // import() + ...(DynamicImportSupported + ? [ + { + filename: fixture("test.js"), + code: "function f() { import(foo) }", + parserOptions: { ecmaVersion: 2020 }, + }, + ] + : []), ], invalid: [ {