diff --git a/docs/developer-guide/nodejs-api.md b/docs/developer-guide/nodejs-api.md index a2959d8e97a..8dbccc7afb1 100644 --- a/docs/developer-guide/nodejs-api.md +++ b/docs/developer-guide/nodejs-api.md @@ -291,7 +291,7 @@ linter.defineParser("my-custom-parser", { const results = linter.verify("// some source text", { parser: "my-custom-parser" }); ``` -### Linter#version +### Linter#version/Linter.version Each instance of `Linter` has a `version` property containing the semantic version number of ESLint that the `Linter` instance is from. @@ -302,6 +302,14 @@ const linter = new Linter(); linter.version; // => '4.5.0' ``` +There is also a `Linter.version` property that you can read without instantiating `Linter`: + +```js +const Linter = require("eslint").Linter; + +Linter.version; // => '4.5.0' +``` + ## linter The `eslint.linter` object (deprecated) is an instance of the `Linter` class as defined [above](#linter). `eslint.linter` exists for backwards compatibility, but we do not recommend using it because any mutations to it are shared among every module that uses `eslint`. Instead, please create your own instance of `eslint.Linter`. diff --git a/lib/linter.js b/lib/linter.js index 8f7bdf9e4c7..b47e6eb1fcf 100644 --- a/lib/linter.js +++ b/lib/linter.js @@ -888,6 +888,15 @@ module.exports = class Linter { this.environments = new Environments(); } + /** + * Getter for package version. + * @static + * @returns {string} The version from package.json. + */ + static get version() { + return pkg.version; + } + /** * Configuration object for the `verify` API. A JS representation of the eslintrc files. * @typedef {Object} ESLintConfig diff --git a/tests/lib/linter.js b/tests/lib/linter.js index dd8e10b951e..5b2f2c31a7c 100644 --- a/tests/lib/linter.js +++ b/tests/lib/linter.js @@ -76,6 +76,14 @@ describe("Linter", () => { sandbox.verifyAndRestore(); }); + describe("Static Members", () => { + describe("version", () => { + it("should return same version as instance property", () => { + assert.strictEqual(Linter.version, linter.version); + }); + }); + }); + describe("when using events", () => { const code = TEST_CODE;