fs = require("fs"),
os = require("os"),
hash = require("../../../lib/cli-engine/hash"),
- { CascadingConfigArrayFactory } = require("@eslint/eslintrc/lib/cascading-config-array-factory"),
+ {
+ Legacy: {
+ CascadingConfigArrayFactory
+ }
+ } = require("@eslint/eslintrc"),
{ unIndent, createCustomTeardown } = require("../../_utils");
const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
originalDir = process.cwd(),
fixtureDir = path.resolve(fs.realpathSync(os.tmpdir()), "eslint/fixtures");
- /** @type {import("../../../lib/cli-engine")["CLIEngine"]} */
+ /** @type {import("../../../lib/cli-engine").CLIEngine} */
let CLIEngine;
- /** @type {import("../../../lib/cli-engine/cli-engine")["getCLIEngineInternalSlots"]} */
+ /** @type {import("../../../lib/cli-engine/cli-engine").getCLIEngineInternalSlots} */
let getCLIEngineInternalSlots;
/**
* @private
*/
function cliEngineWithPlugins(options) {
- const engine = new CLIEngine(options);
-
- // load the mocked plugins
- engine.addPlugin(examplePluginName, examplePlugin);
- engine.addPlugin(examplePluginNameWithNamespace, examplePlugin);
- engine.addPlugin(examplePreprocessorName, require("../../fixtures/processors/custom-processor"));
-
- return engine;
+ return new CLIEngine(options, {
+ preloadedPlugins: {
+ [examplePluginName]: examplePlugin,
+ [examplePluginNameWithNamespace]: examplePlugin,
+ [examplePreprocessorName]: require("../../fixtures/processors/custom-processor")
+ }
+ });
}
// copy into clean area so as not to get "infected" by this project's .eslintrc files
* exceeds the default test timeout, so raise it just for this hook.
* Mocha uses `this` to set timeouts on an individual hook level.
*/
- this.timeout(60 * 1000); // eslint-disable-line no-invalid-this
+ this.timeout(60 * 1000); // eslint-disable-line no-invalid-this -- Mocha API
shell.mkdir("-p", fixtureDir);
shell.cp("-r", "./tests/fixtures/.", fixtureDir);
});
it("should report one fatal message when given a path by --ignore-path that is not a file when ignore is true.", () => {
assert.throws(() => {
- // eslint-disable-next-line no-new
+ // eslint-disable-next-line no-new -- Testing synchronous throwing
new CLIEngine({ ignorePath: fixtureDir });
}, `Cannot read .eslintignore file: ${fixtureDir}\nError: EISDIR: illegal operation on a directory, read`);
});
it("should not modify baseConfig when format is specified", () => {
const customBaseConfig = { root: true };
- new CLIEngine({ baseConfig: customBaseConfig, format: "foo" }); // eslint-disable-line no-new
+ new CLIEngine({ baseConfig: customBaseConfig, format: "foo" }); // eslint-disable-line no-new -- Test side effects
assert.deepStrictEqual(customBaseConfig, { root: true });
});
assert.strictEqual(report.results.length, 1);
assert.strictEqual(report.errorCount, 5);
assert.strictEqual(report.warningCount, 0);
+ assert.strictEqual(report.fatalErrorCount, 0);
assert.strictEqual(report.fixableErrorCount, 3);
assert.strictEqual(report.fixableWarningCount, 0);
assert.strictEqual(report.results[0].messages.length, 5);
messages: [],
errorCount: 0,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var bar = foo;"
],
errorCount: 0,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: []
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: "var bar = foo"
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: []
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var bar = foothis is a syntax error."
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: []
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: "var bar ="
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: []
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: "var bar = foothis is a syntax error.\n return bar;"
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: []
const Module = require("module");
let originalFindPath = null;
- /* eslint-disable no-underscore-dangle */
+ /* eslint-disable no-underscore-dangle -- Private Node API overriding */
before(() => {
originalFindPath = Module._findPath;
Module._findPath = function(id, ...otherArgs) {
after(() => {
Module._findPath = originalFindPath;
});
- /* eslint-enable no-underscore-dangle */
+ /* eslint-enable no-underscore-dangle -- Private Node API overriding */
it("should resolve 'plugins:[\"@scope\"]' to 'node_modules/@scope/eslint-plugin'.", () => {
engine = new CLIEngine({ cwd: getFixturePath("plugin-shorthand/basic") });
describe("executeOnFiles()", () => {
- /** @type {InstanceType<import("../../../lib/cli-engine")["CLIEngine"]>} */
+ /** @type {InstanceType<import("../../../lib/cli-engine").CLIEngine>} */
let engine;
it("should use correct parser when custom parser is specified", () => {
messages: [],
errorCount: 0,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "true ? \"yes\" : \"no\";\n"
messages: [],
errorCount: 0,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0
},
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var msg = \"hi\";\nif (msg == \"hi\") {\n\n}\n"
],
errorCount: 1,
warningCount: 0,
+ fatalErrorCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
output: "var msg = \"hi\" + foo;\n"
useEslintrc: false,
plugins: ["test"],
rules: { "test/example-rule": 1 }
+ }, {
+ preloadedPlugins: {
+ "eslint-plugin-test": {
+ rules: {
+ "example-rule": require("../../fixtures/rules/custom-rule")
+ }
+ }
+ }
});
- engine.addPlugin("eslint-plugin-test", { rules: { "example-rule": require("../../fixtures/rules/custom-rule") } });
-
const report = engine.executeOnFiles([fs.realpathSync(getFixturePath("rules", "test", "test-custom-rule.js"))]);
assert.strictEqual(report.results.length, 1);
},
extensions: ["js", "txt"],
cwd: path.join(fixtureDir, "..")
- });
-
- engine.addPlugin("test-processor", {
- processors: {
- ".txt": {
- preprocess(text) {
- return [text];
- },
- postprocess(messages) {
- return messages[0];
+ }, {
+ preloadedPlugins: {
+ "test-processor": {
+ processors: {
+ ".txt": {
+ preprocess(text) {
+ return [text];
+ },
+ postprocess(messages) {
+ return messages[0];
+ }
+ }
}
}
}
},
extensions: ["js", "txt"],
cwd: path.join(fixtureDir, "..")
- });
-
- engine.addPlugin("test-processor", {
- processors: {
- ".txt": {
- preprocess(text) {
- return [text.replace("a()", "b()")];
- },
- postprocess(messages) {
- messages[0][0].ruleId = "post-processed";
- return messages[0];
+ }, {
+ preloadedPlugins: {
+ "test-processor": {
+ processors: {
+ ".txt": {
+ preprocess(text) {
+ return [text.replace("a()", "b()")];
+ },
+ postprocess(messages) {
+ messages[0][0].ruleId = "post-processed";
+ return messages[0];
+ }
+ }
}
}
}
},
extensions: ["js", "txt"],
ignore: false
- });
-
- engine.addPlugin("test-processor", {
- processors: {
- ".txt": {
- preprocess(text) {
- return [text.replace("a()", "b()")];
- },
- postprocess(messages) {
- messages[0][0].ruleId = "post-processed";
- return messages[0];
+ }, {
+ preloadedPlugins: {
+ "test-processor": {
+ processors: {
+ ".txt": {
+ preprocess(text) {
+ return [text.replace("a()", "b()")];
+ },
+ postprocess(messages) {
+ messages[0][0].ruleId = "post-processed";
+ return messages[0];
+ }
+ }
}
}
}
extensions: ["js", "txt"],
ignore: false,
fix: true
- });
-
- engine.addPlugin("test-processor", {
- processors: {
- ".html": Object.assign({ supportsAutofix: true }, HTML_PROCESSOR)
+ }, {
+ preloadedPlugins: {
+ "test-processor": {
+ processors: {
+ ".html": Object.assign({ supportsAutofix: true }, HTML_PROCESSOR)
+ }
+ }
}
});
extensions: ["js", "txt"],
ignore: false,
fix: true
+ }, {
+ preloadedPlugins: {
+ "test-processor": {
+ processors: {
+ ".html": HTML_PROCESSOR
+ }
+ }
+ }
});
- engine.addPlugin("test-processor", { processors: { ".html": HTML_PROCESSOR } });
-
const report = engine.executeOnText("<script>foo</script>", "foo.html");
assert.strictEqual(report.results[0].messages.length, 1);
},
extensions: ["js", "txt"],
ignore: false
- });
-
- engine.addPlugin("test-processor", {
- processors: {
- ".html": Object.assign({ supportsAutofix: true }, HTML_PROCESSOR)
+ }, {
+ preloadedPlugins: {
+ "test-processor": {
+ processors: {
+ ".html": Object.assign({ supportsAutofix: true }, HTML_PROCESSOR)
+ }
+ }
}
});
assert.throw(() => {
try {
- // eslint-disable-next-line no-new
+ // eslint-disable-next-line no-new -- Check for throwing
new CLIEngine({ cwd });
} catch (error) {
assert.strictEqual(error.messageTemplate, "failed-to-read-json");
const cwd = getFixturePath("ignored-paths", "bad-package-json-ignore");
assert.throws(() => {
- // eslint-disable-next-line no-new
+ // eslint-disable-next-line no-new -- Check for throwing
new CLIEngine({ cwd });
}, "Package.json eslintIgnore property requires an array of paths");
});
const ignorePath = getFixturePath("ignored-paths", "not-a-directory", ".foobaz");
assert.throws(() => {
- // eslint-disable-next-line no-new
+ // eslint-disable-next-line no-new -- Check for throwing
new CLIEngine({ ignorePath, cwd });
}, "Cannot read .eslintignore file");
});
assert.isFunction(formatter);
});
- it("should return null when a customer formatter doesn't exist", () => {
+ it("should return null when a custom formatter doesn't exist", () => {
const engine = new CLIEngine(),
formatterPath = getFixturePath("formatters", "doesntexist.js"),
fullFormatterPath = path.resolve(formatterPath);
}, `There was a problem loading formatter: ${fullFormatterPath}\nError: Cannot find module '${fullFormatterPath}'`);
});
+ it("should throw when a built-in formatter no longer exists", () => {
+ const engine = new CLIEngine();
+
+ assert.throws(() => {
+ engine.getFormatter("table");
+ }, "The table formatter is no longer part of core ESLint. Install it manually with `npm install -D eslint-formatter-table`");
+
+ assert.throws(() => {
+ engine.getFormatter("codeframe");
+ }, "The codeframe formatter is no longer part of core ESLint. Install it manually with `npm install -D eslint-formatter-codeframe`");
+ });
+
it("should throw if the required formatter exists but has an error", () => {
const engine = new CLIEngine(),
formatterPath = getFixturePath("formatters", "broken.js");
assert(engine.getRules().has("node/no-deprecated-api"), "node/no-deprecated-api is present");
});
- it("should expose the rules of the plugin that is added by 'addPlugin'.", () => {
- const engine = new CLIEngine({ plugins: ["foo"] });
-
- engine.addPlugin("foo", require("eslint-plugin-node"));
+ it("should expose the list of rules from a preloaded plugin", () => {
+ const engine = new CLIEngine({
+ plugins: ["foo"]
+ }, {
+ preloadedPlugins: {
+ foo: require("eslint-plugin-node")
+ }
+ });
assert(engine.getRules().has("foo/no-deprecated-api"), "foo/no-deprecated-api is present");
});
const config = {
envs: ["browser"],
ignore: true,
+ useEslintrc: false,
allowInlineConfig: false,
rules: {
"eol-last": 0,
const config = {
envs: ["browser"],
ignore: true,
+ useEslintrc: false,
// allowInlineConfig: true is the default
rules: {
message: "Unused eslint-disable directive (no problems were reported).",
line: 1,
column: 1,
+ fix: {
+ range: [0, 20],
+ text: " "
+ },
severity: 2,
nodeType: null
}
],
errorCount: 1,
warningCount: 0,
- fixableErrorCount: 0,
+ fatalErrorCount: 0,
+ fixableErrorCount: 1,
fixableWarningCount: 0,
source: "/* eslint-disable */"
}
],
errorCount: 1,
warningCount: 0,
- fixableErrorCount: 0,
+ fatalErrorCount: 0,
+ fixableErrorCount: 1,
fixableWarningCount: 0,
usedDeprecatedRules: []
}
}
],
source: "a == b",
- warningCount: 0
+ warningCount: 0,
+ fatalErrorCount: 0
}
]);
});
fixableErrorCount: 0,
fixableWarningCount: 0,
messages: [],
- warningCount: 0
+ warningCount: 0,
+ fatalErrorCount: 0
}
]);
});
fixableErrorCount: 0,
fixableWarningCount: 0,
messages: [],
- warningCount: 0
+ warningCount: 0,
+ fatalErrorCount: 0
}
]);
});
}
],
source: "a == b",
- warningCount: 0
+ warningCount: 0,
+ fatalErrorCount: 0
}
]);
});