Skip to content
Permalink
Browse files

New: add two prefer-global rules

  • Loading branch information...
mysticatea committed Oct 27, 2018
1 parent d153b93 commit 46ed54dc3931bbf78c6b6d0761201867c74c033f
@@ -84,6 +84,8 @@ $ npm install --save-dev eslint eslint-plugin-node
| [node/prefer-global/buffer](./docs/rules/prefer-global/buffer.md) | enforce either `Buffer` or `require("buffer").Buffer` | |
| [node/prefer-global/console](./docs/rules/prefer-global/console.md) | enforce either `console` or `require("console")` | |
| [node/prefer-global/process](./docs/rules/prefer-global/process.md) | enforce either `process` or `require("process")` | |
| [node/prefer-global/text-decoder](./docs/rules/prefer-global/text-decoder.md) | enforce either `TextDecoder` or `require("util").TextDecoder` | |
| [node/prefer-global/text-encoder](./docs/rules/prefer-global/text-encoder.md) | enforce either `TextEncoder` or `require("util").TextEncoder` | |
| [node/prefer-global/url-search-params](./docs/rules/prefer-global/url-search-params.md) | enforce either `URLSearchParams` or `require("url").URLSearchParams` | |
| [node/prefer-global/url](./docs/rules/prefer-global/url.md) | enforce either `URL` or `require("url").URL` | |

@@ -0,0 +1,64 @@
# enforce either `TextDecoder` or `require("util").TextDecoder` (prefer-global/text-decoder)

The `TextDecoder` class of `util` module is defined as a global variable.

```js
console.log(TextDecoder === require("util").TextDecoder) //→ true
```

It will be readable if we use either `TextDecoder` consistently.

## Rule Details

This rule enforces which `TextDecoder` we should use.

### Options

This rule has a string option.

```json
{
"node/prefer-global/text-decoder": ["error", "always" | "never"]
}
```

- `"always"` (default) ... enforces to use the global variable `TextDecoder` rather than `require("util").TextDecoder`.
- `"never"` ... enforces to use `require("util").TextDecoder` rather than the global variable `TextDecoder`.

#### always

Examples of :-1: **incorrect** code for this rule:

```js
/*eslint node/prefer-global/text-decoder: [error]*/
const { TextDecoder } = require("util")
const u = new TextDecoder(s)
```

Examples of :+1: **correct** code for this rule:

```js
/*eslint node/prefer-global/text-decoder: [error]*/
const u = new TextDecoder(s)
```

#### never

Examples of :-1: **incorrect** code for the `"never"` option:

```js
/*eslint node/prefer-global/text-decoder: [error, never]*/
const u = new TextDecoder(s)
```

Examples of :+1: **correct** code for the `"never"` option:

```js
/*eslint node/prefer-global/text-decoder: [error, never]*/
const { TextDecoder } = require("util")
const u = new TextDecoder(s)
```
@@ -0,0 +1,64 @@
# enforce either `TextEncoder` or `require("util").TextEncoder` (prefer-global/text-encoder)

The `TextEncoder` class of `util` module is defined as a global variable.

```js
console.log(TextEncoder === require("util").TextEncoder) //→ true
```

It will be readable if we use either `TextEncoder` consistently.

## Rule Details

This rule enforces which `TextEncoder` we should use.

### Options

This rule has a string option.

```json
{
"node/prefer-global/text-encoder": ["error", "always" | "never"]
}
```

- `"always"` (default) ... enforces to use the global variable `TextEncoder` rather than `require("util").TextEncoder`.
- `"never"` ... enforces to use `require("util").TextEncoder` rather than the global variable `TextEncoder`.

#### always

Examples of :-1: **incorrect** code for this rule:

```js
/*eslint node/prefer-global/text-encoder: [error]*/
const { TextEncoder } = require("util")
const u = new TextEncoder(s)
```

Examples of :+1: **correct** code for this rule:

```js
/*eslint node/prefer-global/text-encoder: [error]*/
const u = new TextEncoder(s)
```

#### never

Examples of :-1: **incorrect** code for the `"never"` option:

```js
/*eslint node/prefer-global/text-encoder: [error, never]*/
const u = new TextEncoder(s)
```

Examples of :+1: **correct** code for the `"never"` option:

```js
/*eslint node/prefer-global/text-encoder: [error, never]*/
const { TextEncoder } = require("util")
const u = new TextEncoder(s)
```
@@ -30,6 +30,8 @@
"node/prefer-global/buffer": "off",
"node/prefer-global/console": "off",
"node/prefer-global/process": "off",
"node/prefer-global/text-decoder": "off",
"node/prefer-global/text-encoder": "off",
"node/prefer-global/url-search-params": "off",
"node/prefer-global/url": "off",
"node/process-exit-as-throw": "error",
@@ -21,6 +21,8 @@ module.exports = {
"prefer-global/buffer": require("./rules/prefer-global/buffer"),
"prefer-global/console": require("./rules/prefer-global/console"),
"prefer-global/process": require("./rules/prefer-global/process"),
"prefer-global/text-decoder": require("./rules/prefer-global/text-decoder"),
"prefer-global/text-encoder": require("./rules/prefer-global/text-encoder"),
"prefer-global/url-search-params": require("./rules/prefer-global/url-search-params"),
"prefer-global/url": require("./rules/prefer-global/url"),
"process-exit-as-throw": require("./rules/process-exit-as-throw"),
@@ -0,0 +1,48 @@
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"

const { READ } = require("eslint-utils")
const checkForPreferGlobal = require("../../util/check-prefer-global")

const trackMap = {
globals: {
TextDecoder: { [READ]: true },
},
modules: {
util: {
TextDecoder: { [READ]: true },
},
},
}

module.exports = {
meta: {
docs: {
description:
'enforce either `TextDecoder` or `require("util").TextDecoder`',
category: "Stylistic Issues",
recommended: false,
url:
"https://github.com/mysticatea/eslint-plugin-node/blob/v7.0.1/docs/rules/prefer-global/text-decoder.md",
},
fixable: null,
schema: [{ enum: ["always", "never"] }],
messages: {
preferGlobal:
"Unexpected use of 'require(\"util\").TextDecoder'. Use the global variable 'TextDecoder' instead.",
preferModule:
"Unexpected use of the global variable 'TextDecoder'. Use 'require(\"util\").TextDecoder' instead.",
},
},

create(context) {
return {
"Program:exit"() {
checkForPreferGlobal(context, trackMap)
},
}
},
}
@@ -0,0 +1,48 @@
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"

const { READ } = require("eslint-utils")
const checkForPreferGlobal = require("../../util/check-prefer-global")

const trackMap = {
globals: {
TextEncoder: { [READ]: true },
},
modules: {
util: {
TextEncoder: { [READ]: true },
},
},
}

module.exports = {
meta: {
docs: {
description:
'enforce either `TextEncoder` or `require("util").TextEncoder`',
category: "Stylistic Issues",
recommended: false,
url:
"https://github.com/mysticatea/eslint-plugin-node/blob/v7.0.1/docs/rules/prefer-global/text-encoder.md",
},
fixable: null,
schema: [{ enum: ["always", "never"] }],
messages: {
preferGlobal:
"Unexpected use of 'require(\"util\").TextEncoder'. Use the global variable 'TextEncoder' instead.",
preferModule:
"Unexpected use of the global variable 'TextEncoder'. Use 'require(\"util\").TextEncoder' instead.",
},
},

create(context) {
return {
"Program:exit"() {
checkForPreferGlobal(context, trackMap)
},
}
},
}
@@ -0,0 +1,49 @@
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"

const RuleTester = require("eslint").RuleTester
const rule = require("../../../../lib/rules/prefer-global/text-decoder")

new RuleTester({
parserOptions: {
ecmaVersion: 2015,
},
globals: {
TextDecoder: false,
require: false,
},
}).run("prefer-global/text-decoder", rule, {
valid: [
"var b = new TextDecoder(s)",
{
code: "var b = new TextDecoder(s)",
options: ["always"],
},
{
code:
"var { TextDecoder } = require('util'); var b = new TextDecoder(s)",
options: ["never"],
},
],
invalid: [
{
code:
"var { TextDecoder } = require('util'); var b = new TextDecoder(s)",
errors: [{ messageId: "preferGlobal" }],
},
{
code:
"var { TextDecoder } = require('util'); var b = new TextDecoder(s)",
options: ["always"],
errors: [{ messageId: "preferGlobal" }],
},
{
code: "var b = new TextDecoder(s)",
options: ["never"],
errors: [{ messageId: "preferModule" }],
},
],
})
@@ -0,0 +1,49 @@
/**
* @author Toru Nagashima
* See LICENSE file in root directory for full license.
*/
"use strict"

const RuleTester = require("eslint").RuleTester
const rule = require("../../../../lib/rules/prefer-global/text-encoder")

new RuleTester({
parserOptions: {
ecmaVersion: 2015,
},
globals: {
TextEncoder: false,
require: false,
},
}).run("prefer-global/text-encoder", rule, {
valid: [
"var b = new TextEncoder(s)",
{
code: "var b = new TextEncoder(s)",
options: ["always"],
},
{
code:
"var { TextEncoder } = require('util'); var b = new TextEncoder(s)",
options: ["never"],
},
],
invalid: [
{
code:
"var { TextEncoder } = require('util'); var b = new TextEncoder(s)",
errors: [{ messageId: "preferGlobal" }],
},
{
code:
"var { TextEncoder } = require('util'); var b = new TextEncoder(s)",
options: ["always"],
errors: [{ messageId: "preferGlobal" }],
},
{
code: "var b = new TextEncoder(s)",
options: ["never"],
errors: [{ messageId: "preferModule" }],
},
],
})

0 comments on commit 46ed54d

Please sign in to comment.
You can’t perform that action at this time.