-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
右辺がundefinedやnullである厳密等価演算子を等価演算子に自動で修正できる設定を追加 (#1752)
* setup eslint plugin for this repository * add rule no-strict-nullable * separate createRule to a file * add documentation for rule no-strict-nullable * fix rule documentation url * add trailing newline to eslint-plugin/package.json --------- Co-authored-by: Hiroshiba <[email protected]>
- Loading branch information
Showing
8 changed files
with
121 additions
and
27 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,6 @@ | ||
const { ESLintUtils } = require("@typescript-eslint/utils"); | ||
|
||
exports.createRule = ESLintUtils.RuleCreator( | ||
(name) => | ||
`https://github.com/VOICEVOX/voicevox/blob/main/eslint-plugin/${name}.md` | ||
); |
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,14 @@ | ||
// @ts-check | ||
module.exports = { | ||
configs: { | ||
all: { | ||
plugins: ["@voicevox"], | ||
rules: { | ||
"@voicevox/no-strict-nullable": "error", | ||
}, | ||
}, | ||
}, | ||
rules: { | ||
"no-strict-nullable": require("./no-strict-nullable"), | ||
}, | ||
}; |
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,58 @@ | ||
// @ts-check | ||
const { AST_NODE_TYPES } = require("@typescript-eslint/utils"); | ||
const { createRule } = require("./create-rule"); | ||
|
||
/** | ||
* @param {import("@typescript-eslint/types").TSESTree.BinaryExpression["left"]} node | ||
*/ | ||
function isNull(node) { | ||
return node.type === AST_NODE_TYPES.Literal && node.value == null; | ||
} | ||
|
||
/** | ||
* @param {import("@typescript-eslint/types").TSESTree.BinaryExpression["right"]} node | ||
*/ | ||
function isUndefined(node) { | ||
return node.type === AST_NODE_TYPES.Identifier && node.name === "undefined"; | ||
} | ||
|
||
module.exports = createRule({ | ||
create(context) { | ||
return { | ||
BinaryExpression(node) { | ||
if (node.operator !== "===" && node.operator !== "!==") return; | ||
if (!isNull(node.right) && !isUndefined(node.right)) return; | ||
|
||
context.report({ | ||
node, | ||
messageId: "report", | ||
data: { | ||
operator: node.operator.slice(0, 2), | ||
expression: context.getSourceCode().getText(node.right), | ||
}, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[node.left.range[1], node.right.range[0]], | ||
node.operator.slice(0, 2) + " " | ||
); | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
name: "no-strict-nullable", | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "undefinedとnullと比較する際に厳密等価演算子を使わない", | ||
recommended: "error", | ||
}, | ||
messages: { | ||
report: | ||
"'{{ operator }}= {{ expression }}'ではなく'{{ operator }} {{ expression }}'を使用してください。", | ||
}, | ||
schema: [], | ||
fixable: "code", | ||
}, | ||
defaultOptions: [], | ||
}); |
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,18 @@ | ||
# no-strict-nullable | ||
|
||
厳密等価演算子`===`は有用ですが,`undefined`や`null`を右辺に持ってくる時,左辺が`undefined`を取りうるのか,それとも`null`を取りうるのかを考える必要があります. | ||
一方,等価演算子`==`は`undefined`と`null`を区別しないため,このような場合に`==`を使うようにすることで,左辺が取る値を考える必要がなくなります. | ||
|
||
このルールでは,右辺が`null`または`undefined`の場合に,厳密等価演算子`===`を使うことを禁止し,代わりに等価演算子`==`を使うようにします. | ||
|
||
```ts | ||
const a = fuga === undefined; | ||
// ^^^^^^^^^^^^^^^^^^ '=== null'ではなく'== null'を使用してください。 | ||
const button = { text: null }; | ||
const c = button.text !== null; | ||
// ^^^^^^^^^^^^^^^^^^^^ '!== null'ではなく'!= null'を使用してください。 | ||
``` | ||
|
||
## リンク | ||
|
||
https://github.com/VOICEVOX/voicevox/issues/1513 |
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,6 @@ | ||
{ | ||
"name": "@voicevox/eslint-plugin", | ||
"version": "0.0.0", | ||
"description": "eslint plugin for voicevox", | ||
"main": "index.js" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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