/
no-logical-assignment-operators.js
60 lines (57 loc) · 2.15 KB
/
no-logical-assignment-operators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
"use strict"
const utils = require("@eslint-community/eslint-utils")
const { getSourceCode } = require("eslint-compat-utils")
module.exports = {
meta: {
docs: {
description: "disallow logical assignment operators.",
category: "ES2021",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-logical-assignment-operators.html",
},
fixable: "code",
messages: {
forbidden: "ES2021 logical assignment operators are forbidden.",
},
schema: [],
type: "problem",
},
create(context) {
const sourceCode = getSourceCode(context)
return {
"AssignmentExpression[operator=/(?:\\|\\||&&|\\?\\?)=/]"(node) {
const operatorToken = sourceCode.getTokenAfter(node.left)
context.report({
node: operatorToken,
messageId: "forbidden",
fix(fixer) {
if (node.left.type !== "Identifier") {
return null
}
const newOperator = node.operator.slice(-1)
const biOperator = node.operator.slice(0, -1)
const varText = sourceCode.getText(node.left)
const results = [
fixer.replaceText(operatorToken, newOperator),
fixer.insertTextAfter(
operatorToken,
` ${varText} ${biOperator}`,
),
]
if (!utils.isParenthesized(node.right, sourceCode)) {
results.push(
fixer.insertTextBefore(node.right, "("),
fixer.insertTextAfter(node.right, ")"),
)
}
return results
},
})
},
}
},
}