diff --git a/tests/lib/plugin.js b/tests/lib/plugin.js index c889d797..5c992646 100644 --- a/tests/lib/plugin.js +++ b/tests/lib/plugin.js @@ -7,7 +7,7 @@ const assert = require("chai").assert; const execSync = require("child_process").execSync; -const CLIEngine = require("eslint").CLIEngine; +const { CLIEngine, ESLint } = require("eslint"); const path = require("path"); const plugin = require("../.."); @@ -19,9 +19,20 @@ const plugin = require("../.."); * Helper function which creates CLIEngine instance with enabled/disabled autofix feature. * @param {string} fixtureConfigName ESLint JSON config fixture filename. * @param {CLIEngineOptions} [options={}] Whether to enable autofix feature. - * @returns {CLIEngine} CLIEngine instance to execute in tests. + * @returns {ESLint} ESLint instance to execute in tests. */ function initCLI(fixtureConfigName, options = {}) { + if (ESLint) { // ESLint v7+ + return new ESLint({ + cwd: path.resolve(__dirname, "../fixtures/"), + ignore: false, + useEslintrc: false, + overrideConfigFile: path.resolve(__dirname, "../fixtures/", fixtureConfigName), + plugins: { markdown: plugin }, + ...options + }); + } + const cli = new CLIEngine({ cwd: path.resolve(__dirname, "../fixtures/"), ignore: false, @@ -72,13 +83,17 @@ describe("recommended config", () => { }); it("should include the plugin", () => { - const config = cli.getConfigForFile("test.md"); + const config = ESLint + ? cli.calculateConfigForFile("test.md") + : cli.getConfigForFile("test.md"); assert.include(config.plugins, "markdown"); }); it("applies convenience configuration", () => { - const config = cli.getConfigForFile("subdir/test.md/0.js"); + const config = ESLint + ? cli.calculateConfigForFile("subdir/test.md/0.js") + : cli.getConfigForFile("subdir/test.md/0.js"); assert.deepStrictEqual(config.parserOptions, { ecmaFeatures: { @@ -95,7 +110,9 @@ describe("recommended config", () => { }); it("overrides configure processor to parse .md file code blocks", () => { - const report = cli.executeOnText(shortText, "test.md"); + const report = ESLint + ? cli.lintText(shortText, { filePath: "test.md" }) + : cli.executeOnText(shortText, "test.md"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -118,7 +135,9 @@ describe("plugin", () => { }); it("should run on .md files", () => { - const report = cli.executeOnText(shortText, "test.md"); + const report = ESLint + ? cli.lintText(shortText, { filePath: "test.md" }) + : cli.executeOnText(shortText, "test.md"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -138,7 +157,9 @@ describe("plugin", () => { "var foo = blah", "```" ].join("\n"); - const report = cli.executeOnText(code, "test.md"); + const report = ESLint + ? cli.lintText(code, { filePath: "test.md" }) + : cli.executeOnText(code, "test.md"); assert.strictEqual(report.results[0].messages[0].message, "'baz' is not defined."); assert.strictEqual(report.results[0].messages[0].line, 5); @@ -158,7 +179,9 @@ describe("plugin", () => { "console.log('a')", "```" ].join("\n"); - const report = cli.executeOnText(code, "test.md"); + const report = ESLint + ? cli.lintText(code, { filePath: "test.md" }) + : cli.executeOnText(code, "test.md"); assert.strictEqual(report.results[0].messages[0].line, 5); }); @@ -169,7 +192,9 @@ describe("plugin", () => { "!@#$%^&*()", "```" ].join("\n"); - const report = cli.executeOnText(code, "test.md"); + const report = ESLint + ? cli.lintText(code, { filePath: "test.md" }) + : cli.executeOnText(code, "test.md"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -192,7 +217,9 @@ describe("plugin", () => { "var foo = blah", "```" ].join("\n"); - const report = cli.executeOnText(code, "test.md"); + const report = ESLint + ? cli.lintText(code, { filePath: "test.md" }) + : cli.executeOnText(code, "test.md"); assert.strictEqual(report.results[0].messages[0].message, "'baz' is not defined."); assert.strictEqual(report.results[0].messages[0].line, 7); @@ -203,7 +230,9 @@ describe("plugin", () => { }); it("should run on .mkdn files", () => { - const report = cli.executeOnText(shortText, "test.mkdn"); + const report = ESLint + ? cli.lintText(shortText, { filePath: "test.mkdnd" }) + : cli.executeOnText(shortText, "test.mkdn"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -212,7 +241,9 @@ describe("plugin", () => { }); it("should run on .mdown files", () => { - const report = cli.executeOnText(shortText, "test.mdown"); + const report = ESLint + ? cli.lintText(shortText, { filePath: "test.mdown" }) + : cli.executeOnText(shortText, "test.mdown"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -221,7 +252,9 @@ describe("plugin", () => { }); it("should run on .markdown files", () => { - const report = cli.executeOnText(shortText, "test.markdown"); + const report = ESLint + ? cli.lintText(shortText, { filePath: "test.markdown" }) + : cli.executeOnText(shortText, "test.markdown"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -230,7 +263,9 @@ describe("plugin", () => { }); it("should run on files with any custom extension", () => { - const report = cli.executeOnText(shortText, "test.custom"); + const report = ESLint + ? cli.lintText(shortText, { filePath: "test.custom" }) + : cli.executeOnText(shortText, "test.custom"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -239,7 +274,9 @@ describe("plugin", () => { }); it("should extract blocks and remap messages", () => { - const report = cli.executeOnFiles([path.resolve(__dirname, "../fixtures/long.md")]); + const report = ESLint + ? cli.lintFiles([path.resolve(__dirname, "../fixtures/long.md")]) + : cli.executeOnFiles([path.resolve(__dirname, "../fixtures/long.md")]); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 5); @@ -289,7 +326,9 @@ describe("plugin", () => { const recursiveCli = initCLI("eslintrc.json", { extensions: [".js", ".markdown", ".md"] }); - const report = recursiveCli.executeOnText(code, "test.md"); + const report = ESLint + ? recursiveCli.lintText(code, { filePath: "test.md" }) + : recursiveCli.executeOnText(code, "test.md"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 2); @@ -320,7 +359,9 @@ describe("plugin", () => { "console.log(double);", "```" ].join("\n"); - const report = cli.executeOnText(code, "test.md"); + const report = ESLint + ? cli.lintText(code, { filePath: "test.md" }) + : cli.executeOnText(code, "test.md"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 4); @@ -344,7 +385,9 @@ describe("plugin", () => { "\"use strict\";", "```" ].join("\n"); - const report = cli.executeOnText(code, "test.md"); + const report = ESLint + ? cli.lintText(code, { filePath: "test.md" }) + : cli.executeOnText(code, "test.md"); assert.strictEqual(report.results.length, 1); assert.strictEqual(report.results[0].messages.length, 1); @@ -374,7 +417,9 @@ describe("plugin", () => { "console.log(\"Hello, world!\")", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -397,7 +442,9 @@ describe("plugin", () => { "console.log(\"Hello, world!\")", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -426,7 +473,9 @@ describe("plugin", () => { "console.log(\"Hello, world!\")", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -451,7 +500,9 @@ describe("plugin", () => { "}", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -476,7 +527,9 @@ describe("plugin", () => { "}", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -497,7 +550,9 @@ describe("plugin", () => { "\"use strict\"", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -518,7 +573,9 @@ describe("plugin", () => { "console.log(\"Hello, world!\")", "````" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -539,7 +596,9 @@ describe("plugin", () => { "console.log(\"Hello, world!\");", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -560,7 +619,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -591,7 +652,9 @@ describe("plugin", () => { " }", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -618,7 +681,9 @@ describe("plugin", () => { "world!\")", "```" ].join("\r\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -643,7 +708,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -666,7 +733,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -689,7 +758,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -712,7 +783,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -737,7 +810,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -764,7 +839,9 @@ describe("plugin", () => { " world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -792,7 +869,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -819,7 +898,9 @@ describe("plugin", () => { "> world!\")", "> ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -857,7 +938,9 @@ describe("plugin", () => { "> > world!\")", "> > ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -886,7 +969,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\");", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -917,7 +1002,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\");", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -941,7 +1028,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -964,7 +1053,9 @@ describe("plugin", () => { " console.log(\"Hello, world!\")", " ```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected); @@ -1007,7 +1098,9 @@ describe("plugin", () => { "};", "```" ].join("\n"); - const report = cli.executeOnText(input, "test.md"); + const report = ESLint + ? cli.lintText(input, { filePath: "test.md" }) + : cli.executeOnText(input, "test.md"); const actual = report.results[0].output; assert.strictEqual(actual, expected);