Skip to content

Commit 273b937

Browse files
feat: add version as a setting (#70)
* feat: add version as a setting * test: check settings * docs * docs: mention shareable configs * style: lintfix
1 parent ed988f9 commit 273b937

File tree

13 files changed

+274
-45
lines changed

13 files changed

+274
-45
lines changed

docs/rules/no-unsupported-features/es-builtins.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ See also [TC39 finished proposals](https://github.com/tc39/proposals/blob/master
1919

2020
### Configured Node.js version range
2121

22-
This rule reads the [engines] field of `package.json` to detect which Node.js versions your module is supporting.
22+
This rule gets the supported Node.js version range from the following, falling back to the next if unspecified:
2323

24-
I recommend the use of the [engines] field because it's the official way that indicates which Node.js versions your module is supporting.
25-
For example of `package.json`:
24+
1. Rule configuration `version`
25+
2. ESLint [shared setting](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings) `node.version`
26+
3. `package.json` [`engines`] field
27+
4. `>=8.0.0`
28+
29+
The default version is `8.0.0` because it's the minimum version the community is maintaining (see also [Node.js Release Working Group](https://github.com/nodejs/Release#readme)).
30+
31+
For Node.js packages, using the [`engines`] field is recommended because it's the official way to indicate support:
2632

2733
```json
2834
{
@@ -34,7 +40,7 @@ For example of `package.json`:
3440
}
3541
```
3642

37-
If you omit the [engines] field, this rule chooses `>=8.0.0` as the configured Node.js version since `8` is the minimum version the community is maintaining (see also [Node.js Release Working Group](https://github.com/nodejs/Release#readme)).
43+
For [Shareable Configs](https://eslint.org/docs/latest/developer-guide/shareable-configs) or packages with a different development environment (e.g. pre-compiled, web package, etc.), you can configure ESLint with `settings.node.version` to specify support.
3844

3945
### Options
4046

@@ -49,7 +55,7 @@ If you omit the [engines] field, this rule chooses `>=8.0.0` as the configured N
4955

5056
#### version
5157

52-
As mentioned above, this rule reads the [engines] field of `package.json`.
58+
As mentioned above, this rule reads the [`engines`] field of `package.json`.
5359
But, you can overwrite the version by `version` option.
5460

5561
The `version` option accepts [the valid version range of `node-semver`](https://github.com/npm/node-semver#range-grammar).
@@ -140,6 +146,30 @@ The `"ignores"` option accepts an array of the following strings.
140146

141147
</details>
142148

149+
### Shared Settings
150+
151+
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
152+
Several rules have the same option, but we can set this option at once.
153+
154+
- `version`
155+
156+
For Example:
157+
158+
```json
159+
{
160+
"settings": {
161+
"node": {
162+
"version": ">=8.0.0",
163+
}
164+
},
165+
"rules": {
166+
"n/no-unsupported-features/es-builtins": ["error", {
167+
"ignores": []
168+
}]
169+
}
170+
}
171+
```
172+
143173
### Known limitations
144174

145175
This rule cannot find non-static things.
@@ -150,7 +180,7 @@ For example:
150180
- New `options` properties of function parameters.
151181
- New events.
152182

153-
[engines]: https://docs.npmjs.com/files/package.json#engines
183+
[`engines`]: https://docs.npmjs.com/files/package.json#engines
154184

155185
## 🔎 Implementation
156186

docs/rules/no-unsupported-features/es-syntax.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ For example, set `2020` to `parserOptions.ecmaVersion`.
2222

2323
### Configured Node.js version range
2424

25-
This rule reads the [engines] field of `package.json` to detect which Node.js versions your module is supporting.
25+
This rule gets the supported Node.js version range from the following, falling back to the next if unspecified:
2626

27-
I recommend the use of the [engines] field because it's the official way that indicates which Node.js versions your module is supporting.
28-
For example of `package.json`:
27+
1. Rule configuration `version`
28+
2. ESLint [shared setting](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings) `node.version`
29+
3. `package.json` [`engines`] field
30+
4. `>=8.0.0`
31+
32+
The default version is `8.0.0` because it's the minimum version the community is maintaining (see also [Node.js Release Working Group](https://github.com/nodejs/Release#readme)).
33+
34+
For Node.js packages, using the [`engines`] field is recommended because it's the official way to indicate support:
2935

3036
```json
3137
{
@@ -37,7 +43,7 @@ For example of `package.json`:
3743
}
3844
```
3945

40-
If you omit the [engines] field, this rule chooses `>=8.0.0` as the configured Node.js version since `8` is the minimum version the community is maintaining (see also [Node.js Release Working Group](https://github.com/nodejs/Release#readme)).
46+
For [Shareable Configs](https://eslint.org/docs/latest/developer-guide/shareable-configs) or packages with a different development environment (e.g. pre-compiled, web package, etc.), you can configure ESLint with `settings.node.version` to specify support.
4147

4248
### Options
4349

@@ -52,7 +58,7 @@ If you omit the [engines] field, this rule chooses `>=8.0.0` as the configured N
5258

5359
#### version
5460

55-
As mentioned above, this rule reads the [engines] field of `package.json`.
61+
As mentioned above, this rule reads the [`engines`] field of `package.json`.
5662
But, you can overwrite the version by `version` option.
5763

5864
The `version` option accepts [the valid version range of `node-semver`](https://github.com/npm/node-semver#range-grammar).
@@ -123,7 +129,31 @@ The `"ignores"` option accepts an array of the following strings.
123129

124130
</details>
125131

126-
[engines]: https://docs.npmjs.com/files/package.json#engines
132+
[`engines`]: https://docs.npmjs.com/files/package.json#engines
133+
134+
### Shared Settings
135+
136+
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
137+
Several rules have the same option, but we can set this option at once.
138+
139+
- `version`
140+
141+
For Example:
142+
143+
```json
144+
{
145+
"settings": {
146+
"node": {
147+
"version": ">=8.0.0",
148+
}
149+
},
150+
"rules": {
151+
"n/no-unsupported-features/es-syntax": ["error", {
152+
"ignores": []
153+
}]
154+
}
155+
}
156+
```
127157

128158
## 🔎 Implementation
129159

docs/rules/no-unsupported-features/node-builtins.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ This rule reports APIs of Node.js built-in APIs on the basis of [Node.js v13.2.0
1616

1717
### Configured Node.js version range
1818

19-
This rule reads the [engines] field of `package.json` to detect which Node.js versions your module is supporting.
19+
This rule gets the supported Node.js version range from the following, falling back to the next if unspecified:
2020

21-
I recommend the use of the [engines] field because it's the official way that indicates which Node.js versions your module is supporting.
22-
For example of `package.json`:
21+
1. Rule configuration `version`
22+
2. ESLint [shared setting](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings) `node.version`
23+
3. `package.json` [`engines`] field
24+
4. `>=8.0.0`
25+
26+
The default version is `8.0.0` because it's the minimum version the community is maintaining (see also [Node.js Release Working Group](https://github.com/nodejs/Release#readme)).
27+
28+
For Node.js packages, using the [`engines`] field is recommended because it's the official way to indicate support:
2329

2430
```json
2531
{
@@ -31,7 +37,7 @@ For example of `package.json`:
3137
}
3238
```
3339

34-
If you omit the [engines] field, this rule chooses `>=8.0.0` as the configured Node.js version since `8` is the minimum version the community is maintaining (see also [Node.js Release Working Group](https://github.com/nodejs/Release#readme)).
40+
For [Shareable Configs](https://eslint.org/docs/latest/developer-guide/shareable-configs) or packages with a different development environment (e.g. pre-compiled, web package, etc.), you can configure ESLint with `settings.node.version` to specify support.
3541

3642
### Options
3743

@@ -46,7 +52,7 @@ If you omit the [engines] field, this rule chooses `>=8.0.0` as the configured N
4652

4753
#### version
4854

49-
As mentioned above, this rule reads the [engines] field of `package.json`.
55+
As mentioned above, this rule reads the [`engines`] field of `package.json`.
5056
But, you can overwrite the version by `version` option.
5157

5258
The `version` option accepts [the valid version range of `node-semver`](https://github.com/npm/node-semver#range-grammar).
@@ -324,6 +330,30 @@ The `"ignores"` option accepts an array of the following strings.
324330

325331
</details>
326332

333+
### Shared Settings
334+
335+
The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
336+
Several rules have the same option, but we can set this option at once.
337+
338+
- `version`
339+
340+
For Example:
341+
342+
```json
343+
{
344+
"settings": {
345+
"node": {
346+
"version": ">=8.0.0",
347+
}
348+
},
349+
"rules": {
350+
"n/no-unsupported-features/node-builtins": ["error", {
351+
"ignores": []
352+
}]
353+
}
354+
}
355+
```
356+
327357
### Known limitations
328358

329359
This rule cannot find non-static things.
@@ -334,7 +364,7 @@ For example:
334364
- New `options` properties of function parameters.
335365
- New events.
336366

337-
[engines]: https://docs.npmjs.com/files/package.json#engines
367+
[`engines`]: https://docs.npmjs.com/files/package.json#engines
338368

339369
## 🔎 Implementation
340370

lib/rules/no-deprecated-api.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,7 @@ function toName(type, path) {
671671
*/
672672
function parseOptions(context) {
673673
const raw = context.options[0] || {}
674-
const filePath = context.getFilename()
675-
const version = getConfiguredNodeVersion(raw.version, filePath)
674+
const version = getConfiguredNodeVersion(context)
676675
const ignoredModuleItems = new Set(raw.ignoreModuleItems || [])
677676
const ignoredGlobalItems = new Set(raw.ignoreGlobalItems || [])
678677

@@ -693,9 +692,7 @@ module.exports = {
693692
{
694693
type: "object",
695694
properties: {
696-
version: {
697-
type: "string",
698-
},
695+
version: getConfiguredNodeVersion.schema,
699696
ignoreModuleItems: {
700697
type: "array",
701698
items: {
@@ -720,8 +717,9 @@ module.exports = {
720717
},
721718
],
722719
messages: {
723-
"deprecated": "{{name}} was deprecated since v{{version}}{{replace}}."
724-
}
720+
deprecated:
721+
"{{name}} was deprecated since v{{version}}{{replace}}.",
722+
},
725723
},
726724
create(context) {
727725
const { ignoredModuleItems, ignoredGlobalItems, version } =

lib/rules/no-unsupported-features/es-builtins.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
messages,
1111
} = require("../../util/check-unsupported-builtins")
1212
const enumeratePropertyNames = require("../../util/enumerate-property-names")
13+
const getConfiguredNodeVersion = require("../../util/get-configured-node-version")
1314

1415
const trackMap = {
1516
globals: {
@@ -152,9 +153,7 @@ module.exports = {
152153
{
153154
type: "object",
154155
properties: {
155-
version: {
156-
type: "string",
157-
},
156+
version: getConfiguredNodeVersion.schema,
158157
ignores: {
159158
type: "array",
160159
items: {

lib/rules/no-unsupported-features/es-syntax.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ const keywords = Object.keys(features)
412412
*/
413413
function parseOptions(context) {
414414
const raw = context.options[0] || {}
415-
const filePath = context.getFilename()
416-
const version = getConfiguredNodeVersion(raw.version, filePath)
415+
const version = getConfiguredNodeVersion(context)
417416
const ignores = new Set(raw.ignores || [])
418417

419418
return Object.freeze({ version, ignores })
@@ -534,9 +533,7 @@ module.exports = {
534533
{
535534
type: "object",
536535
properties: {
537-
version: {
538-
type: "string",
539-
},
536+
version: getConfiguredNodeVersion.schema,
540537
ignores: {
541538
type: "array",
542539
items: {

lib/rules/no-unsupported-features/node-builtins.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
messages,
1111
} = require("../../util/check-unsupported-builtins")
1212
const enumeratePropertyNames = require("../../util/enumerate-property-names")
13+
const getConfiguredNodeVersion = require("../../util/get-configured-node-version")
1314

1415
const trackMap = {
1516
globals: {
@@ -263,7 +264,7 @@ const trackMap = {
263264
},
264265
},
265266
release: { [READ]: { supported: "3.0.0" } },
266-
report: { [READ]: { supported: "14.0.0", experimental: "11.8.0"} },
267+
report: { [READ]: { supported: "14.0.0", experimental: "11.8.0" } },
267268
resourceUsage: { [READ]: { supported: "12.6.0" } },
268269
setegid: { [READ]: { supported: "2.0.0" } },
269270
seteuid: { [READ]: { supported: "2.0.0" } },
@@ -384,9 +385,7 @@ module.exports = {
384385
{
385386
type: "object",
386387
properties: {
387-
version: {
388-
type: "string",
389-
},
388+
version: getConfiguredNodeVersion.schema,
390389
ignores: {
391390
type: "array",
392391
items: {

lib/util/check-unsupported-builtins.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ const getSemverRange = require("./get-semver-range")
2323
*/
2424
function parseOptions(context) {
2525
const raw = context.options[0] || {}
26-
const filePath = context.getFilename()
27-
const version = getConfiguredNodeVersion(raw.version, filePath)
26+
const version = getConfiguredNodeVersion(context)
2827
const ignores = new Set(raw.ignores || [])
2928

3029
return Object.freeze({ version, ignores })

lib/util/get-configured-node-version.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ const { Range } = require("semver") //eslint-disable-line no-unused-vars
88
const getPackageJson = require("./get-package-json")
99
const getSemverRange = require("./get-semver-range")
1010

11+
/**
12+
* Gets `version` property from a given option object.
13+
*
14+
* @param {object|undefined} option - An option object to get.
15+
* @returns {string[]|null} The `allowModules` value, or `null`.
16+
*/
17+
function get(option) {
18+
if (option && option.version) {
19+
return option.version
20+
}
21+
return null
22+
}
23+
1124
/**
1225
* Get the `engines.node` field of package.json.
1326
* @param {string} filename The path to the current linting file.
@@ -30,10 +43,19 @@ function getEnginesNode(filename) {
3043
* This will be used to look package.json up if `version` is not a valid version range.
3144
* @returns {Range} The configured version range.
3245
*/
33-
module.exports = function getConfiguredNodeVersion(version, filename) {
46+
module.exports = function getConfiguredNodeVersion(context) {
47+
const version =
48+
get(context.options && context.options[0]) ||
49+
get(context.settings && (context.settings.n || context.settings.node))
50+
const filePath = context.getFilename()
51+
3452
return (
3553
getSemverRange(version) ||
36-
getEnginesNode(filename) ||
54+
getEnginesNode(filePath) ||
3755
getSemverRange(">=8.0.0")
3856
)
3957
}
58+
59+
module.exports.schema = {
60+
type: "string",
61+
}

0 commit comments

Comments
 (0)