From 5b2e82074826fb0861d42e01033be5d185c29976 Mon Sep 17 00:00:00 2001 From: Patrick McElhaney Date: Thu, 28 Dec 2017 09:08:58 -0500 Subject: [PATCH] New: CLIEngine#getRules() (refs #6582) --- docs/developer-guide/nodejs-api.md | 21 +++++++++++++++++++++ lib/cli-engine.js | 5 +++++ tests/lib/cli-engine.js | 11 ++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/developer-guide/nodejs-api.md b/docs/developer-guide/nodejs-api.md index 622d8612bacd..009b9874a085 100644 --- a/docs/developer-guide/nodejs-api.md +++ b/docs/developer-guide/nodejs-api.md @@ -678,6 +678,7 @@ var cli = new CLIEngine({ } }); + // lint myfile.js and all files in lib/ var report = cli.executeOnFiles(["myfile.js", "lib/"]); @@ -685,6 +686,26 @@ var report = cli.executeOnFiles(["myfile.js", "lib/"]); CLIEngine.outputFixes(report); ``` +### getRules() + +This method returns a map of all loaded rules. Under the hood, it calls [Linter#getRules](#lintergetrules). + +```js +const CLIEngine = require("eslint").CLIEngine; +const cli = new CLIEngine(); + +cli.getRules(); + +/* +Map { + 'accessor-pairs' => { meta: { docs: [Object], schema: [Array] }, create: [Function: create] }, + 'array-bracket-newline' => { meta: { docs: [Object], schema: [Array] }, create: [Function: create] }, + ... +} +*/ +``` + + ### CLIEngine.version `CLIEngine` has a static `version` property containing the semantic version number of ESLint that it comes from. diff --git a/lib/cli-engine.js b/lib/cli-engine.js index 1abc1fd2c6bc..40a93e46d707 100644 --- a/lib/cli-engine.js +++ b/lib/cli-engine.js @@ -428,6 +428,11 @@ class CLIEngine { this.config = new Config(this.options, this.linter); } + + getRules() { + return this.linter.getRules(); + } + /** * Returns results that only contains errors. * @param {LintResult[]} results The results to filter. diff --git a/tests/lib/cli-engine.js b/tests/lib/cli-engine.js index 419df9eef219..8703bebfb736 100644 --- a/tests/lib/cli-engine.js +++ b/tests/lib/cli-engine.js @@ -138,7 +138,7 @@ describe("CLIEngine", () => { assert.equal(report.results[0].fixableWarningCount, 0); }); - it("should report the toatl and per file warnings when using local cwd .eslintrc", () => { + it("should report the total and per file warnings when using local cwd .eslintrc", () => { engine = new CLIEngine({ rules: { @@ -2811,6 +2811,15 @@ describe("CLIEngine", () => { }); + describe("getRules()", () => { + it("should expose the list of rules", () => { + const engine = new CLIEngine(); + + assert.isTrue(engine.getRules().has("no-eval"), "no-eval is present"); + + }); + }); + describe("resolveFileGlobPatterns", () => { leche.withData([