-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d153b93
commit 46ed54d
Showing
9 changed files
with
328 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 @@ | ||
# 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) | ||
``` |
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 @@ | ||
# 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) | ||
``` |
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
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,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) | ||
}, | ||
} | ||
}, | ||
} |
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,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) | ||
}, | ||
} | ||
}, | ||
} |
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,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" }], | ||
}, | ||
], | ||
}) |
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,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" }], | ||
}, | ||
], | ||
}) |