Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New:
no-unsupported-features/worker_threads
rule (refs #118)
- Loading branch information
1 parent
65cbc08
commit b02dcef
Showing
5 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Disallow unsupported `worker_threads` APIs on the specified version (no-unsupported-features/worker_threads) | ||
|
||
Node.js community is improving built-in modules continuously. | ||
You can check [Node.js Documentation](https://nodejs.org/api/) to know which Node.js version supports each Node.js API. | ||
|
||
This rule reports unsupported `worker_threads` module's APIs on the configured Node.js version as lint errors. | ||
Editor integrations of ESLint would be useful to know it in real-time. | ||
|
||
## Rule Details | ||
|
||
This rule reports APIs of the `worker_threads` module on the basis of [Node.js v10.6.0 Documentation](https://nodejs.org/docs/v10.6.0/api/worker_threads.html). | ||
|
||
### Configured Node.js version range | ||
|
||
This rule reads the [engines] field of `package.json` to detect which Node.js versions your module is supporting. | ||
|
||
I recommend the use of the [engines] field because it's the official way that indicates which Node.js versions your module is supporting. | ||
For example of `package.json`: | ||
|
||
```json | ||
{ | ||
"name": "your-module", | ||
"version": "1.0.0", | ||
"engines": { | ||
"node": ">=6.0.0" | ||
} | ||
} | ||
``` | ||
|
||
If you omit the [engines] field, this rule chooses `>=6.0.0` as the configured Node.js version since `6` is the minimum version the community is maintaining (see also [Node.js Release Working Group](https://github.com/nodejs/Release#readme)). | ||
|
||
### Options | ||
|
||
```json | ||
{ | ||
"node/no-unsupported-features/worker_threads": ["error", { | ||
"version": ">=6.0.0", | ||
"ignores": [] | ||
}] | ||
} | ||
``` | ||
|
||
#### version | ||
|
||
As mentioned above, this rule reads the [engines] field of `package.json`. | ||
But, you can overwrite the version by `version` option. | ||
|
||
The `version` option accepts [the valid version range of `node-semver`](https://github.com/npm/node-semver#range-grammar). | ||
|
||
#### ignores | ||
|
||
If you are using transpilers, maybe you want to ignore the warnings about some features. | ||
You can use this `ignores` option to ignore the given features. | ||
|
||
The `"ignores"` option accepts an array of the following strings. | ||
|
||
- `"worker_threads"` | ||
|
||
### Known limitations | ||
|
||
This rule cannot find non-static things. | ||
E.g., the use of instance methods. | ||
|
||
[engines]: https://docs.npmjs.com/files/package.json#engines |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const { READ } = require("eslint-utils") | ||
const defineUnsupportedModuleHandlers = require("../../util/define-unsupported-module-handlers") | ||
const enumeratePropertyNames = require("../../util/enumerate-property-names") | ||
|
||
const trackMap = { | ||
modules: { | ||
//eslint-disable-next-line camelcase | ||
worker_threads: { | ||
[READ]: { supported: "10.5.0" }, | ||
}, | ||
}, | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
"disallow unsupported `worker_threads` APIs on the specified version", | ||
category: "Possible Errors", | ||
recommended: true, | ||
url: | ||
"https://github.com/mysticatea/eslint-plugin-node/blob/v6.0.1/docs/rules/no-unsupported-features/worker_threads.md", | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
version: { | ||
type: "string", | ||
}, | ||
ignores: { | ||
type: "array", | ||
items: { | ||
enum: Array.from( | ||
enumeratePropertyNames(trackMap.modules) | ||
), | ||
}, | ||
uniqueItems: true, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unsupported: | ||
"The '{{name}}' is not supported until Node.js {{supported}}. The configured version range is '{{version}}'.", | ||
}, | ||
}, | ||
create(context) { | ||
return defineUnsupportedModuleHandlers(context, trackMap) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @author Toru Nagashima | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const RuleTester = require("eslint").RuleTester | ||
const rule = require("../../../../lib/rules/no-unsupported-features/worker_threads") | ||
|
||
new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2015, | ||
sourceType: "module", | ||
}, | ||
globals: { | ||
require: false, | ||
}, | ||
}).run("no-unsupported-features/worker_threads", rule, { | ||
valid: [ | ||
{ | ||
code: "require('worker_threads')", | ||
options: [{ version: "10.5.0" }], | ||
}, | ||
{ | ||
code: "import worker_threads from 'worker_threads'", | ||
options: [{ version: "10.5.0" }], | ||
}, | ||
{ | ||
code: "require('worker_threads')", | ||
options: [{ version: "10.4.99", ignores: ["worker_threads"] }], | ||
}, | ||
{ | ||
code: "import worker_threads from 'worker_threads'", | ||
options: [{ version: "10.4.99", ignores: ["worker_threads"] }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: "require('worker_threads')", | ||
options: [{ version: "10.4.99" }], | ||
errors: [ | ||
{ | ||
messageId: "unsupported", | ||
data: { | ||
name: "worker_threads", | ||
supported: "10.5.0", | ||
version: "10.4.99", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "import worker_threads from 'worker_threads'", | ||
options: [{ version: "10.4.99" }], | ||
errors: [ | ||
{ | ||
messageId: "unsupported", | ||
data: { | ||
name: "worker_threads", | ||
supported: "10.5.0", | ||
version: "10.4.99", | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "import { Worker } from 'worker_threads'", | ||
options: [{ version: "10.4.99" }], | ||
errors: [ | ||
{ | ||
messageId: "unsupported", | ||
data: { | ||
name: "worker_threads", | ||
supported: "10.5.0", | ||
version: "10.4.99", | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}) |