Skip to content

Commit 3943635

Browse files
authored
Update: Create Linter.version API (fixes #9271) (#11010)
1 parent a940cf4 commit 3943635

3 files changed

Lines changed: 26 additions & 1 deletion

File tree

docs/developer-guide/nodejs-api.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ linter.defineParser("my-custom-parser", {
291291
const results = linter.verify("// some source text", { parser: "my-custom-parser" });
292292
```
293293

294-
### Linter#version
294+
### Linter#version/Linter.version
295295

296296
Each instance of `Linter` has a `version` property containing the semantic version number of ESLint that the `Linter` instance is from.
297297

@@ -302,6 +302,14 @@ const linter = new Linter();
302302
linter.version; // => '4.5.0'
303303
```
304304

305+
There is also a `Linter.version` property that you can read without instantiating `Linter`:
306+
307+
```js
308+
const Linter = require("eslint").Linter;
309+
310+
Linter.version; // => '4.5.0'
311+
```
312+
305313
## linter
306314

307315
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`.

lib/linter.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,15 @@ module.exports = class Linter {
888888
this.environments = new Environments();
889889
}
890890

891+
/**
892+
* Getter for package version.
893+
* @static
894+
* @returns {string} The version from package.json.
895+
*/
896+
static get version() {
897+
return pkg.version;
898+
}
899+
891900
/**
892901
* Configuration object for the `verify` API. A JS representation of the eslintrc files.
893902
* @typedef {Object} ESLintConfig

tests/lib/linter.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ describe("Linter", () => {
7676
sandbox.verifyAndRestore();
7777
});
7878

79+
describe("Static Members", () => {
80+
describe("version", () => {
81+
it("should return same version as instance property", () => {
82+
assert.strictEqual(Linter.version, linter.version);
83+
});
84+
});
85+
});
86+
7987
describe("when using events", () => {
8088
const code = TEST_CODE;
8189

0 commit comments

Comments
 (0)