From 644e9993360c82ae5229014faa378721385ac858 Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Thu, 5 Sep 2019 10:30:03 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=92=20fix=20tests=20on=20old=20ESLint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/lib/rules/file-extension-in-import.js | 54 +++++++++++++++------ tests/lib/rules/no-extraneous-import.js | 32 +++++++++--- tests/lib/rules/no-missing-import.js | 32 +++++++++--- tests/lib/rules/no-unpublished-import.js | 32 +++++++++--- 4 files changed, 113 insertions(+), 37 deletions(-) diff --git a/tests/lib/rules/file-extension-in-import.js b/tests/lib/rules/file-extension-in-import.js index a584b31d..33928c81 100644 --- a/tests/lib/rules/file-extension-in-import.js +++ b/tests/lib/rules/file-extension-in-import.js @@ -5,9 +5,23 @@ "use strict" const path = require("path") -const RuleTester = require("eslint").RuleTester +const { Linter, RuleTester } = require("eslint") const rule = require("../../../lib/rules/file-extension-in-import") +const DynamicImportSupported = (() => { + const config = { parserOptions: { ecmaVersion: 2020 } } + const messages = new Linter().verify("import(s)", config) + return messages.length === 0 +})() + +if (!DynamicImportSupported) { + //eslint-disable-next-line no-console + console.warn( + "[%s] Skip tests for 'import()'", + path.basename(__filename, ".js") + ) +} + function fixture(filename) { return path.resolve( __dirname, @@ -240,20 +254,28 @@ new RuleTester({ }, // import() - { - filename: fixture("test.js"), - code: "function f() { import('./a') }", - output: "function f() { import('./a.js') }", - parserOptions: { ecmaVersion: 2020 }, - errors: [{ messageId: "requireExt", data: { ext: ".js" } }], - }, - { - filename: fixture("test.js"), - code: "function f() { import('./a.js') }", - output: "function f() { import('./a') }", - options: ["never"], - parserOptions: { ecmaVersion: 2020 }, - errors: [{ messageId: "forbidExt", data: { ext: ".js" } }], - }, + ...(DynamicImportSupported + ? [ + { + filename: fixture("test.js"), + code: "function f() { import('./a') }", + output: "function f() { import('./a.js') }", + parserOptions: { ecmaVersion: 2020 }, + errors: [ + { messageId: "requireExt", data: { ext: ".js" } }, + ], + }, + { + filename: fixture("test.js"), + code: "function f() { import('./a.js') }", + output: "function f() { import('./a') }", + options: ["never"], + parserOptions: { ecmaVersion: 2020 }, + errors: [ + { messageId: "forbidExt", data: { ext: ".js" } }, + ], + }, + ] + : []), ], }) diff --git a/tests/lib/rules/no-extraneous-import.js b/tests/lib/rules/no-extraneous-import.js index fe399909..03ee9706 100644 --- a/tests/lib/rules/no-extraneous-import.js +++ b/tests/lib/rules/no-extraneous-import.js @@ -5,9 +5,23 @@ "use strict" const path = require("path") -const { RuleTester } = require("eslint") +const { Linter, RuleTester } = require("eslint") const rule = require("../../../lib/rules/no-extraneous-import") +const DynamicImportSupported = (() => { + const config = { parserOptions: { ecmaVersion: 2020 } } + const messages = new Linter().verify("import(s)", config) + return messages.length === 0 +})() + +if (!DynamicImportSupported) { + //eslint-disable-next-line no-console + console.warn( + "[%s] Skip tests for 'import()'", + path.basename(__filename, ".js") + ) +} + /** * Makes a file path to a fixture. * @param {string} name - A name. @@ -87,11 +101,15 @@ ruleTester.run("no-extraneous-import", rule, { }, // import() - { - filename: fixture("dependencies/a.js"), - code: "function f() { import('bbb') }", - parserOptions: { ecmaVersion: 2020 }, - errors: ['"bbb" is extraneous.'], - }, + ...(DynamicImportSupported + ? [ + { + filename: fixture("dependencies/a.js"), + code: "function f() { import('bbb') }", + parserOptions: { ecmaVersion: 2020 }, + errors: ['"bbb" is extraneous.'], + }, + ] + : []), ], }) diff --git a/tests/lib/rules/no-missing-import.js b/tests/lib/rules/no-missing-import.js index b1f19ac7..9ee1271c 100644 --- a/tests/lib/rules/no-missing-import.js +++ b/tests/lib/rules/no-missing-import.js @@ -5,9 +5,23 @@ "use strict" const path = require("path") -const { RuleTester } = require("eslint") +const { Linter, RuleTester } = require("eslint") const rule = require("../../../lib/rules/no-missing-import") +const DynamicImportSupported = (() => { + const config = { parserOptions: { ecmaVersion: 2020 } } + const messages = new Linter().verify("import(s)", config) + return messages.length === 0 +})() + +if (!DynamicImportSupported) { + //eslint-disable-next-line no-console + console.warn( + "[%s] Skip tests for 'import()'", + path.basename(__filename, ".js") + ) +} + /** * Makes a file path to a fixture. * @param {string} name - A name. @@ -212,11 +226,15 @@ ruleTester.run("no-missing-import", rule, { }, // import() - { - filename: fixture("test.js"), - code: "function f() { import('no-exist-package-0') }", - parserOptions: { ecmaVersion: 2020 }, - errors: ['"no-exist-package-0" is not found.'], - }, + ...(DynamicImportSupported + ? [ + { + filename: fixture("test.js"), + code: "function f() { import('no-exist-package-0') }", + parserOptions: { ecmaVersion: 2020 }, + errors: ['"no-exist-package-0" is not found.'], + }, + ] + : []), ], }) diff --git a/tests/lib/rules/no-unpublished-import.js b/tests/lib/rules/no-unpublished-import.js index c79c6fda..41afd663 100644 --- a/tests/lib/rules/no-unpublished-import.js +++ b/tests/lib/rules/no-unpublished-import.js @@ -5,9 +5,23 @@ "use strict" const path = require("path") -const { RuleTester } = require("eslint") +const { Linter, RuleTester } = require("eslint") const rule = require("../../../lib/rules/no-unpublished-import") +const DynamicImportSupported = (() => { + const config = { parserOptions: { ecmaVersion: 2020 } } + const messages = new Linter().verify("import(s)", config) + return messages.length === 0 +})() + +if (!DynamicImportSupported) { + //eslint-disable-next-line no-console + console.warn( + "[%s] Skip tests for 'import()'", + path.basename(__filename, ".js") + ) +} + /** * Makes a file path to a fixture. * @param {string} name - A name. @@ -246,11 +260,15 @@ ruleTester.run("no-unpublished-import", rule, { }, // import() - { - filename: fixture("2/test.js"), - code: "function f() { import('./ignore1.js') }", - parserOptions: { ecmaVersion: 2020 }, - errors: ['"./ignore1.js" is not published.'], - }, + ...(DynamicImportSupported + ? [ + { + filename: fixture("2/test.js"), + code: "function f() { import('./ignore1.js') }", + parserOptions: { ecmaVersion: 2020 }, + errors: ['"./ignore1.js" is not published.'], + }, + ] + : []), ], })