eslint-plugin-ziloen 0.1.19 → 0.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +0 -1
- package/dist/index.js +92 -61
- package/package.json +16 -15
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ASTUtils, AST_NODE_TYPES, ESLintUtils } from "@typescript-eslint/utils";
|
|
2
|
+
import { getConstrainedTypeAtLocation } from "@typescript-eslint/type-utils";
|
|
3
3
|
|
|
4
4
|
//#region src/utils.ts
|
|
5
5
|
const createEslintRule = ESLintUtils.RuleCreator((ruleName) => ruleName);
|
|
@@ -85,31 +85,15 @@ const genericSpacing = createEslintRule({
|
|
|
85
85
|
}
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
-
//#endregion
|
|
89
|
-
//#region src/tsutils.ts
|
|
90
|
-
function isUnionType(type) {
|
|
91
|
-
return (type.flags & TypeFlags.Union) !== 0;
|
|
92
|
-
}
|
|
93
|
-
/** Returns all types of a union type or an array containing `type` itself if it's no union type. */
|
|
94
|
-
function unionTypeParts(type) {
|
|
95
|
-
return isUnionType(type) ? type.types : [type];
|
|
96
|
-
}
|
|
97
|
-
function isTypeFlagSet(type, flag) {
|
|
98
|
-
return (type.flags & flag) !== 0;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Resolves the given node's type. Will resolve to the type's generic constraint, if it has one.
|
|
102
|
-
*/
|
|
103
|
-
function getConstrainedTypeAtLocation(checker, node) {
|
|
104
|
-
const nodeType = checker.getTypeAtLocation(node);
|
|
105
|
-
return checker.getBaseConstraintOfType(nodeType) ?? nodeType;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
88
|
//#endregion
|
|
109
89
|
//#region src/rules/jsx-strict-logical-expressions.ts
|
|
110
90
|
/**
|
|
111
91
|
* https://github.com/hluisson/eslint-plugin-jsx-expressions
|
|
112
92
|
*/
|
|
93
|
+
const messages = {
|
|
94
|
+
conditionErrorFalseyString: "Potentially falsy string in logical AND expression. Please use a boolean.",
|
|
95
|
+
conditionErrorFalseyNumber: "Potentially falsy number or bigint in logical AND expression. Please use a boolean."
|
|
96
|
+
};
|
|
113
97
|
const RULE_NAME = "jsx-strict-logical-expressions";
|
|
114
98
|
const jsxStrictLogicalExpressions = createEslintRule({
|
|
115
99
|
name: RULE_NAME,
|
|
@@ -118,9 +102,9 @@ const jsxStrictLogicalExpressions = createEslintRule({
|
|
|
118
102
|
allowNumber: false
|
|
119
103
|
}],
|
|
120
104
|
meta: {
|
|
121
|
-
docs: { description: "Forbid non-boolean falsey values in inline expressions" },
|
|
122
|
-
fixable: "code",
|
|
123
105
|
type: "problem",
|
|
106
|
+
fixable: "code",
|
|
107
|
+
docs: { description: "Forbid non-boolean falsy values in JSX logical AND expressions" },
|
|
124
108
|
schema: [{
|
|
125
109
|
type: "object",
|
|
126
110
|
properties: {
|
|
@@ -129,48 +113,95 @@ const jsxStrictLogicalExpressions = createEslintRule({
|
|
|
129
113
|
},
|
|
130
114
|
additionalProperties: false
|
|
131
115
|
}],
|
|
132
|
-
messages
|
|
133
|
-
conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
|
|
134
|
-
conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
|
|
135
|
-
}
|
|
116
|
+
messages
|
|
136
117
|
},
|
|
137
|
-
create(context, [
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
function
|
|
148
|
-
|
|
149
|
-
if (errorId) context.report({
|
|
150
|
-
node,
|
|
151
|
-
messageId: errorId,
|
|
152
|
-
fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
function determineNode(originalNode) {
|
|
156
|
-
let nodeToEvaluate = originalNode;
|
|
157
|
-
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.ChainExpression) nodeToEvaluate = nodeToEvaluate.expression;
|
|
158
|
-
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== TSESTree.AST_NODE_TYPES.PrivateIdentifier) nodeToEvaluate = nodeToEvaluate.property;
|
|
159
|
-
return nodeToEvaluate;
|
|
118
|
+
create(context, [{ allowString = false, allowNumber = false }]) {
|
|
119
|
+
const services = ESLintUtils.getParserServices(context);
|
|
120
|
+
const checker = services.program.getTypeChecker();
|
|
121
|
+
const stringType = checker.getStringType();
|
|
122
|
+
const numberType = checker.getNumberType();
|
|
123
|
+
const bigintType = checker.getBigIntType();
|
|
124
|
+
/**
|
|
125
|
+
* Returns true when `value` is TypeScript's internal representation
|
|
126
|
+
* of a non-zero bigint literal.
|
|
127
|
+
*/
|
|
128
|
+
function isNonZeroBigIntLiteralValue(value) {
|
|
129
|
+
return typeof value === "object" && value !== null && "base10Value" in value && typeof value.base10Value === "string" && value.base10Value !== "0";
|
|
160
130
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
131
|
+
/**
|
|
132
|
+
* Determines which diagnostic, if any, applies to a condition.
|
|
133
|
+
*/
|
|
134
|
+
function getConditionError(node) {
|
|
135
|
+
const type = getConstrainedTypeAtLocation(services, node);
|
|
136
|
+
const typeParts = type.isUnion() ? type.types : [type];
|
|
137
|
+
let hasPotentiallyFalsyString = false;
|
|
138
|
+
let hasPotentiallyFalsyNumber = false;
|
|
139
|
+
for (const typePart of typeParts) {
|
|
140
|
+
if (typePart.isStringLiteral()) {
|
|
141
|
+
hasPotentiallyFalsyString ||= typePart.value === "";
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (typePart.isNumberLiteral()) {
|
|
145
|
+
hasPotentiallyFalsyNumber ||= typePart.value === 0;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const baseType = checker.getBaseTypeOfLiteralType(typePart);
|
|
149
|
+
if (baseType === stringType) {
|
|
150
|
+
hasPotentiallyFalsyString = true;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (baseType === numberType) {
|
|
154
|
+
hasPotentiallyFalsyNumber = true;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (baseType === bigintType) {
|
|
158
|
+
const value = "value" in typePart ? typePart.value : void 0;
|
|
159
|
+
hasPotentiallyFalsyNumber ||= !isNonZeroBigIntLiteralValue(value);
|
|
160
|
+
}
|
|
168
161
|
}
|
|
162
|
+
if (!allowString && hasPotentiallyFalsyString) return "conditionErrorFalseyString";
|
|
163
|
+
if (!allowNumber && hasPotentiallyFalsyNumber) return "conditionErrorFalseyNumber";
|
|
169
164
|
}
|
|
170
|
-
|
|
171
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Flattens:
|
|
167
|
+
*
|
|
168
|
+
* a && b && <Component />
|
|
169
|
+
*
|
|
170
|
+
* into:
|
|
171
|
+
*
|
|
172
|
+
* [a, b, <Component />]
|
|
173
|
+
*
|
|
174
|
+
* Every operand except the final one acts as a condition.
|
|
175
|
+
*/
|
|
176
|
+
function collectAndOperands(node, operands = []) {
|
|
177
|
+
if (node.type === AST_NODE_TYPES.LogicalExpression && node.operator === "&&") {
|
|
178
|
+
collectAndOperands(node.left, operands);
|
|
179
|
+
collectAndOperands(node.right, operands);
|
|
180
|
+
} else operands.push(node);
|
|
181
|
+
return operands;
|
|
182
|
+
}
|
|
183
|
+
function reportCondition(node) {
|
|
184
|
+
const messageId = getConditionError(node);
|
|
185
|
+
if (!messageId) return;
|
|
186
|
+
context.report({
|
|
187
|
+
node,
|
|
188
|
+
messageId,
|
|
189
|
+
fix: (fixer) => {
|
|
190
|
+
if (node.type === AST_NODE_TYPES.Identifier || node.type === AST_NODE_TYPES.Literal || node.type === AST_NODE_TYPES.MemberExpression || node.type === AST_NODE_TYPES.CallExpression || node.type === AST_NODE_TYPES.ChainExpression) return fixer.insertTextBefore(node, "!!");
|
|
191
|
+
if (ASTUtils.isParenthesized(node, context.sourceCode)) {
|
|
192
|
+
const openingParen = context.sourceCode.getTokenBefore(node);
|
|
193
|
+
if (openingParen?.value === "(") return fixer.insertTextBefore(openingParen, "!!");
|
|
194
|
+
}
|
|
195
|
+
return [fixer.insertTextBefore(node, "!!("), fixer.insertTextAfter(node, ")")];
|
|
196
|
+
}
|
|
197
|
+
});
|
|
172
198
|
}
|
|
173
|
-
return { JSXExpressionContainer
|
|
199
|
+
return { JSXExpressionContainer(node) {
|
|
200
|
+
const expression = node.expression;
|
|
201
|
+
if (expression.type !== AST_NODE_TYPES.LogicalExpression || expression.operator !== "&&") return;
|
|
202
|
+
const operands = collectAndOperands(expression);
|
|
203
|
+
for (let index = 0; index < operands.length - 1; index += 1) reportCondition(operands[index]);
|
|
204
|
+
} };
|
|
174
205
|
}
|
|
175
206
|
});
|
|
176
207
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-ziloen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -8,26 +8,27 @@
|
|
|
8
8
|
"files": [
|
|
9
9
|
"dist"
|
|
10
10
|
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "tsdown",
|
|
13
|
-
"release": "pnpm run build && bumpp --commit --no-push && pnpm publish",
|
|
14
|
-
"test": "vitest"
|
|
15
|
-
},
|
|
16
11
|
"dependencies": {
|
|
17
|
-
"@typescript-eslint/utils": "8.
|
|
12
|
+
"@typescript-eslint/type-utils": "^8.65.0",
|
|
13
|
+
"@typescript-eslint/utils": "8.65.0"
|
|
18
14
|
},
|
|
19
15
|
"devDependencies": {
|
|
20
16
|
"@types/eslint": "^9.6.1",
|
|
21
17
|
"@types/node": "^24.1.0",
|
|
22
|
-
"@typescript-eslint/parser": "8.
|
|
23
|
-
"bumpp": "^
|
|
24
|
-
"eslint": "^10.0
|
|
18
|
+
"@typescript-eslint/parser": "8.65.0",
|
|
19
|
+
"bumpp": "^12.0.0",
|
|
20
|
+
"eslint": "^10.8.0",
|
|
25
21
|
"eslint-vitest-rule-tester": "^3.1.0",
|
|
26
|
-
"tsdown": "^0.
|
|
27
|
-
"typescript": "6.0.
|
|
28
|
-
"vitest": "^4.
|
|
22
|
+
"tsdown": "^0.22.14",
|
|
23
|
+
"typescript": "6.0.2",
|
|
24
|
+
"vitest": "^4.1.10"
|
|
29
25
|
},
|
|
30
26
|
"peerDependencies": {
|
|
31
|
-
"typescript": ">=
|
|
27
|
+
"typescript": ">=6.0.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsdown",
|
|
31
|
+
"release": "pnpm run build && bumpp --commit --no-push && pnpm publish",
|
|
32
|
+
"test": "vitest"
|
|
32
33
|
}
|
|
33
|
-
}
|
|
34
|
+
}
|