Skip to content

Commit

Permalink
New: no-unsupported-features/worker_threads rule (refs #118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jul 16, 2018
1 parent 65cbc08 commit b02dcef
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -81,6 +81,7 @@ $ npm install --save-dev eslint eslint-plugin-node
| [node/no-unsupported-features/util](./docs/rules/no-unsupported-features/util.md) | disallow unsupported `util` APIs on the specified version | ⭐️ |
| [node/no-unsupported-features/v8](./docs/rules/no-unsupported-features/v8.md) | disallow unsupported `v8` APIs on the specified version | ⭐️ |
| [node/no-unsupported-features/vm](./docs/rules/no-unsupported-features/vm.md) | disallow unsupported `vm` APIs on the specified version | ⭐️ |
| [node/no-unsupported-features/worker_threads](./docs/rules/no-unsupported-features/worker_threads.md) | disallow unsupported `worker_threads` APIs on the specified version | ⭐️ |
| [node/process-exit-as-throw](./docs/rules/process-exit-as-throw.md) | make `process.exit()` expressions the same code path as `throw` | ⭐️ |
| [node/shebang](./docs/rules/shebang.md) | enforce the correct usage of shebang | ⭐️✒️ |

Expand Down
64 changes: 64 additions & 0 deletions docs/rules/no-unsupported-features/worker_threads.md
@@ -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
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -37,6 +37,7 @@ module.exports = {
"no-unsupported-features/util": require("./rules/no-unsupported-features/util"),
"no-unsupported-features/v8": require("./rules/no-unsupported-features/v8"),
"no-unsupported-features/vm": require("./rules/no-unsupported-features/vm"),
"no-unsupported-features/worker_threads": require("./rules/no-unsupported-features/worker_threads"),
"process-exit-as-throw": require("./rules/process-exit-as-throw"),
shebang: require("./rules/shebang"),

Expand Down
59 changes: 59 additions & 0 deletions lib/rules/no-unsupported-features/worker_threads.js
@@ -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)
},
}
81 changes: 81 additions & 0 deletions tests/lib/rules/no-unsupported-features/worker_threads.js
@@ -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",
},
},
],
},
],
})

0 comments on commit b02dcef

Please sign in to comment.