Skip to content

Commit

Permalink
New: no-unsupported-features/v8 rule (refs #118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jul 16, 2018
1 parent 9992b17 commit 222f6c0
Show file tree
Hide file tree
Showing 5 changed files with 524 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -79,6 +79,7 @@ $ npm install --save-dev eslint eslint-plugin-node
| [node/no-unsupported-features/trace_events](./docs/rules/no-unsupported-features/trace_events.md) | disallow unsupported `trace_events` APIs on the specified version | ⭐️ |
| [node/no-unsupported-features/url](./docs/rules/no-unsupported-features/url.md) | disallow unsupported `url` APIs on the specified version | ⭐️ |
| [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/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
72 changes: 72 additions & 0 deletions docs/rules/no-unsupported-features/v8.md
@@ -0,0 +1,72 @@
# Disallow unsupported `v8` APIs on the specified version (no-unsupported-features/v8)

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 `v8` 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 `v8` module on the basis of [Node.js v10.6.0 Documentation](https://nodejs.org/docs/v10.6.0/api/v8.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/v8": ["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.

- `"v8"`
- `"v8.cachedDataVersionTag"`
- `"v8.getHeapSpaceStatistics"`
- `"v8.serialize"`
- `"v8.deserialize"`
- `"v8.Serializer"`
- `"v8.Deserializer"`
- `"v8.DefaultSerializer"`
- `"v8.DefaultDeserializer"`

### 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 @@ -35,6 +35,7 @@ module.exports = {
"no-unsupported-features/trace_events": require("./rules/no-unsupported-features/trace_events"),
"no-unsupported-features/url": require("./rules/no-unsupported-features/url"),
"no-unsupported-features/util": require("./rules/no-unsupported-features/util"),
"no-unsupported-features/v8": require("./rules/no-unsupported-features/v8"),
"process-exit-as-throw": require("./rules/process-exit-as-throw"),
shebang: require("./rules/shebang"),

Expand Down
66 changes: 66 additions & 0 deletions lib/rules/no-unsupported-features/v8.js
@@ -0,0 +1,66 @@
/**
* @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: {
v8: {
[READ]: { supported: "1.0.0" },
cachedDataVersionTag: { [READ]: { supported: "8.0.0" } },
getHeapSpaceStatistics: { [READ]: { supported: "6.0.0" } },
serialize: { [READ]: { supported: "8.0.0" } },
deserialize: { [READ]: { supported: "8.0.0" } },
Serializer: { [READ]: { supported: "8.0.0" } },
Deserializer: { [READ]: { supported: "8.0.0" } },
DefaultSerializer: { [READ]: { supported: "8.0.0" } },
DefaultDeserializer: { [READ]: { supported: "8.0.0" } },
},
},
}

module.exports = {
meta: {
docs: {
description:
"disallow unsupported `v8` 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/v8.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)
},
}

0 comments on commit 222f6c0

Please sign in to comment.