-
Notifications
You must be signed in to change notification settings - Fork 362
/
eslint-local-rules.cjs
136 lines (129 loc) · 4.17 KB
/
eslint-local-rules.cjs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const { ESLintUtils } = require("@typescript-eslint/experimental-utils");
const createRule = ESLintUtils.RuleCreator((name) => `https://example.com/rule/${name}`);
module.exports = {
"no-bigint-negation": createRule({
name: "no-bigint-negation",
meta: {
type: "problem",
docs: {
description: "Disallow negation of bigint or bigint | undefined variables",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
bigintNegation: "Negation of bigint is not allowed.",
},
hasSuggestions: true,
fixable: true,
},
defaultOptions: [],
create(context) {
const parserServices = ESLintUtils.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
return {
UnaryExpression(node) {
if (node.operator === "!") {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node.argument);
const type = checker.getTypeAtLocation(tsNode);
if (
checker.typeToString(type) === "bigint" ||
checker.typeToString(type) === "bigint | undefined" ||
checker.typeToString(type) === "bigint | null"
) {
context.report({
node,
messageId: "bigintNegation",
suggest: [
{
desc: "Use === undefined",
fix(fixer) {
return [
fixer.removeRange([node.range[0], node.range[0] + 1]),
fixer.insertTextAfter(node.argument, " === undefined"),
];
},
},
{
desc: "Use === null",
fix(fixer) {
return [
fixer.removeRange([node.range[0], node.range[0] + 1]),
fixer.insertTextAfter(node.argument, " === null"),
];
},
},
{
desc: "Use === 0n",
fix(fixer) {
return [
fixer.removeRange([node.range[0], node.range[0] + 1]),
fixer.insertTextAfter(node.argument, " === 0n"),
];
},
},
],
});
}
}
},
};
},
}),
"no-logical-bigint": createRule({
name: "no-conditional-bigint",
meta: {
type: "problem",
docs: {
description: "Disallow using bigint type in logical expressions",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
logicalBigint: "Using bigint in logical expressions is not allowed.",
},
hasSuggestions: true,
fixable: true,
},
defaultOptions: [],
create(context) {
return {
LogicalExpression(node) {
if (checkBigInt(context, node)) {
context.report({
node,
messageId: "logicalBigint",
});
}
},
IfStatement(node) {
if (checkBigInt(context, node.test)) {
context.report({
node,
messageId: "logicalBigint",
});
}
},
};
},
}),
};
function checkBigInt(context, node) {
if (node.type === "Literal" || node.type === "Identifier" || node.type === "MemberExpression") {
const parserServices = ESLintUtils.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
return (
checker.typeToString(type) === "bigint" ||
checker.typeToString(type) === "bigint | undefined" ||
checker.typeToString(type) === "bigint | null"
);
}
if (node.type === "LogicalExpression") {
if (node.operator !== "&&" && node.operator !== "||") return false;
return checkBigInt(context, node.right) || checkBigInt(context, node.left);
}
return false;
}