eslint-plugin-ziloen 0.1.5 → 0.1.6
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.cjs +3 -267
- package/dist/index.js +3 -234
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -1,267 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
default: () => src_default
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(src_exports);
|
|
36
|
-
|
|
37
|
-
// src/utils.ts
|
|
38
|
-
var import_utils = require("@typescript-eslint/utils");
|
|
39
|
-
var createEslintRule = import_utils.ESLintUtils.RuleCreator((ruleName) => ruleName);
|
|
40
|
-
|
|
41
|
-
// src/rules/generic-spacing.ts
|
|
42
|
-
var RULE_NAME = "generic-spacing";
|
|
43
|
-
var messageId = "genericSpacingMismatch";
|
|
44
|
-
function removeSpaceAround(node, context) {
|
|
45
|
-
const params = node.params;
|
|
46
|
-
if (!params.length)
|
|
47
|
-
return;
|
|
48
|
-
const sourceCode = context.sourceCode;
|
|
49
|
-
const start = node.range[0] + 1;
|
|
50
|
-
const end = node.range[1] - 1;
|
|
51
|
-
const startNode = params[0];
|
|
52
|
-
const endNode = params.at(-1);
|
|
53
|
-
const textBeforeFirst = sourceCode.text.slice(start, startNode.range[0]);
|
|
54
|
-
const textAfterLast = sourceCode.text.slice(endNode.range[1], end);
|
|
55
|
-
if (textBeforeFirst.length && !textBeforeFirst.includes("\n")) {
|
|
56
|
-
context.report({
|
|
57
|
-
node,
|
|
58
|
-
messageId,
|
|
59
|
-
*fix(fixer) {
|
|
60
|
-
yield fixer.replaceTextRange([start, startNode.range[0]], "");
|
|
61
|
-
},
|
|
62
|
-
loc: {
|
|
63
|
-
start: {
|
|
64
|
-
column: node.loc.start.column + 1,
|
|
65
|
-
line: node.loc.start.line
|
|
66
|
-
},
|
|
67
|
-
end: startNode.loc.start
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
if (textAfterLast.length && !textAfterLast.includes("\n")) {
|
|
72
|
-
context.report({
|
|
73
|
-
node,
|
|
74
|
-
messageId,
|
|
75
|
-
*fix(fixer) {
|
|
76
|
-
yield fixer.replaceTextRange([endNode.range[1], end], "");
|
|
77
|
-
},
|
|
78
|
-
loc: {
|
|
79
|
-
start: endNode.loc.end,
|
|
80
|
-
end: {
|
|
81
|
-
column: node.loc.end.column - 1,
|
|
82
|
-
line: node.loc.end.line
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
var genericSpacing = createEslintRule({
|
|
89
|
-
name: RULE_NAME,
|
|
90
|
-
meta: {
|
|
91
|
-
type: "layout",
|
|
92
|
-
docs: {
|
|
93
|
-
description: "",
|
|
94
|
-
recommended: "stylistic"
|
|
95
|
-
},
|
|
96
|
-
fixable: "code",
|
|
97
|
-
schema: [],
|
|
98
|
-
messages: {
|
|
99
|
-
genericSpacingMismatch: "spacing mismatch"
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
|
-
defaultOptions: [],
|
|
103
|
-
create(context) {
|
|
104
|
-
const sourceCode = context.sourceCode;
|
|
105
|
-
return {
|
|
106
|
-
// type T<"K, S"> = any
|
|
107
|
-
TSTypeParameterDeclaration: (node) => removeSpaceAround(node, context),
|
|
108
|
-
// type T = Record<"K, S">
|
|
109
|
-
TSTypeParameterInstantiation: (node) => removeSpaceAround(node, context),
|
|
110
|
-
// type T<"K = any", "S = any"> = any
|
|
111
|
-
TSTypeParameter(node) {
|
|
112
|
-
if (!node.default)
|
|
113
|
-
return;
|
|
114
|
-
const startNode = node.constraint ?? node.name;
|
|
115
|
-
const endNode = node.default;
|
|
116
|
-
const start = startNode.range[1];
|
|
117
|
-
const end = endNode.range[0];
|
|
118
|
-
if (sourceCode.text.slice(start, end) !== " = ") {
|
|
119
|
-
context.report({
|
|
120
|
-
node,
|
|
121
|
-
messageId,
|
|
122
|
-
*fix(fixer) {
|
|
123
|
-
yield fixer.replaceTextRange([start, end], " = ");
|
|
124
|
-
},
|
|
125
|
-
loc: {
|
|
126
|
-
start: startNode.loc.end,
|
|
127
|
-
end: endNode.loc.start
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
var generic_spacing_default = genericSpacing;
|
|
136
|
-
|
|
137
|
-
// src/rules/jsx-strict-logical-expressions.ts
|
|
138
|
-
var import_utils3 = require("@typescript-eslint/utils");
|
|
139
|
-
var ts2 = __toESM(require("typescript"), 1);
|
|
140
|
-
|
|
141
|
-
// src/tsutils.ts
|
|
142
|
-
var ts = __toESM(require("typescript"), 1);
|
|
143
|
-
function isUnionType(type) {
|
|
144
|
-
return (type.flags & ts.TypeFlags.Union) !== 0;
|
|
145
|
-
}
|
|
146
|
-
function unionTypeParts(type) {
|
|
147
|
-
return isUnionType(type) ? type.types : [type];
|
|
148
|
-
}
|
|
149
|
-
function isFlagSet(obj, flag) {
|
|
150
|
-
return (obj.flags & flag) !== 0;
|
|
151
|
-
}
|
|
152
|
-
var isTypeFlagSet = isFlagSet;
|
|
153
|
-
function getConstrainedTypeAtLocation(checker, node) {
|
|
154
|
-
const nodeType = checker.getTypeAtLocation(node);
|
|
155
|
-
const constrained = checker.getBaseConstraintOfType(nodeType);
|
|
156
|
-
return constrained ?? nodeType;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// src/rules/jsx-strict-logical-expressions.ts
|
|
160
|
-
var RULE_NAME2 = "jsx-strict-logical-expressions";
|
|
161
|
-
var jsxStrictLogicalExpressions = createEslintRule({
|
|
162
|
-
name: RULE_NAME2,
|
|
163
|
-
defaultOptions: [{ allowString: false, allowNumber: false }],
|
|
164
|
-
meta: {
|
|
165
|
-
docs: {
|
|
166
|
-
description: "Forbid non-boolean falsey values in inline expressions",
|
|
167
|
-
recommended: "strict"
|
|
168
|
-
},
|
|
169
|
-
fixable: "code",
|
|
170
|
-
type: "problem",
|
|
171
|
-
schema: [
|
|
172
|
-
{
|
|
173
|
-
type: "object",
|
|
174
|
-
properties: {
|
|
175
|
-
allowString: { type: "boolean" },
|
|
176
|
-
allowNumber: { type: "boolean" }
|
|
177
|
-
},
|
|
178
|
-
additionalProperties: false
|
|
179
|
-
}
|
|
180
|
-
],
|
|
181
|
-
messages: {
|
|
182
|
-
conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
|
|
183
|
-
conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
|
|
184
|
-
}
|
|
185
|
-
},
|
|
186
|
-
create(context, [options]) {
|
|
187
|
-
const parserServices = import_utils3.ESLintUtils.getParserServices(context);
|
|
188
|
-
const typeChecker = parserServices.program.getTypeChecker();
|
|
189
|
-
function checkIdentifier(node) {
|
|
190
|
-
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
191
|
-
const types = unionTypeParts(
|
|
192
|
-
getConstrainedTypeAtLocation(typeChecker, tsNode)
|
|
193
|
-
);
|
|
194
|
-
const hasPotentiallyFalseyString = types.some(
|
|
195
|
-
(type) => isTypeFlagSet(type, ts2.TypeFlags.StringLike) && (!type.isStringLiteral() || type.value === "")
|
|
196
|
-
);
|
|
197
|
-
if (!options.allowString && hasPotentiallyFalseyString) {
|
|
198
|
-
return "conditionErrorFalseyString";
|
|
199
|
-
}
|
|
200
|
-
const hasPotentiallyFalseyNumber = types.some(
|
|
201
|
-
(type) => isTypeFlagSet(
|
|
202
|
-
type,
|
|
203
|
-
ts2.TypeFlags.NumberLike | ts2.TypeFlags.BigIntLike
|
|
204
|
-
) && (!type.isNumberLiteral() || type.value === 0)
|
|
205
|
-
);
|
|
206
|
-
if (!options.allowNumber && hasPotentiallyFalseyNumber) {
|
|
207
|
-
return "conditionErrorFalseyNumber";
|
|
208
|
-
}
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
function checkAndReportIdentifier(node, fixNode) {
|
|
212
|
-
const errorId = checkIdentifier(node);
|
|
213
|
-
if (errorId) {
|
|
214
|
-
context.report({
|
|
215
|
-
node,
|
|
216
|
-
messageId: errorId,
|
|
217
|
-
fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
function determineNode(originalNode) {
|
|
222
|
-
let nodeToEvaluate = originalNode;
|
|
223
|
-
if (nodeToEvaluate.type === import_utils3.TSESTree.AST_NODE_TYPES.ChainExpression) {
|
|
224
|
-
nodeToEvaluate = nodeToEvaluate.expression;
|
|
225
|
-
}
|
|
226
|
-
if (nodeToEvaluate.type === import_utils3.TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== import_utils3.TSESTree.AST_NODE_TYPES.PrivateIdentifier) {
|
|
227
|
-
nodeToEvaluate = nodeToEvaluate.property;
|
|
228
|
-
}
|
|
229
|
-
return nodeToEvaluate;
|
|
230
|
-
}
|
|
231
|
-
function checkLogicalExpression(expressionNode, checkRightNode) {
|
|
232
|
-
const leftNode = determineNode(expressionNode.left);
|
|
233
|
-
if (leftNode.type === import_utils3.TSESTree.AST_NODE_TYPES.LogicalExpression) {
|
|
234
|
-
checkLogicalExpression(leftNode, true);
|
|
235
|
-
} else if (leftNode.type === import_utils3.TSESTree.AST_NODE_TYPES.Identifier) {
|
|
236
|
-
checkAndReportIdentifier(leftNode, expressionNode.left);
|
|
237
|
-
}
|
|
238
|
-
if (checkRightNode) {
|
|
239
|
-
const rightNode = determineNode(expressionNode.right);
|
|
240
|
-
if (rightNode.type === import_utils3.TSESTree.AST_NODE_TYPES.Identifier) {
|
|
241
|
-
checkAndReportIdentifier(rightNode, expressionNode.right);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
function checkJSXExpression(node) {
|
|
246
|
-
if (node.expression.type === import_utils3.TSESTree.AST_NODE_TYPES.LogicalExpression && node.expression.operator === "&&") {
|
|
247
|
-
checkLogicalExpression(node.expression, false);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return {
|
|
251
|
-
JSXExpressionContainer: checkJSXExpression
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
var jsx_strict_logical_expressions_default = jsxStrictLogicalExpressions;
|
|
256
|
-
|
|
257
|
-
// src/index.ts
|
|
258
|
-
var ziloen = {
|
|
259
|
-
// meta: {
|
|
260
|
-
// name: "ziloen"
|
|
261
|
-
// },
|
|
262
|
-
rules: {
|
|
263
|
-
"generic-spacing": generic_spacing_default,
|
|
264
|
-
"jsx-strict-logical-expressions": jsx_strict_logical_expressions_default
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
var src_default = ziloen;
|
|
1
|
+
"use strict";var _=Object.create;var y=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var R=Object.getPrototypeOf,D=Object.prototype.hasOwnProperty;var k=(e,r)=>{for(var t in r)y(e,t,{get:r[t],enumerable:!0})},x=(e,r,t,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of M(r))!D.call(e,s)&&s!==t&&y(e,s,{get:()=>r[s],enumerable:!(i=O(r,s))||i.enumerable});return e};var N=(e,r,t)=>(t=e!=null?_(R(e)):{},x(r||!e||!e.__esModule?y(t,"default",{value:e,enumerable:!0}):t,e)),v=e=>x(y({},"__esModule",{value:!0}),e);var q={};k(q,{default:()=>z});module.exports=v(q);var b=require("@typescript-eslint/utils"),u=b.ESLintUtils.RuleCreator(e=>e);var w="generic-spacing",S="genericSpacingMismatch";function P(e,r){let t=e.params;if(!t.length)return;let i=r.sourceCode,s=e.range[0]+1,p=e.range[1]-1,c=t[0],g=t.at(-1),m=i.text.slice(s,c.range[0]),n=i.text.slice(g.range[1],p);m.length&&!m.includes(`
|
|
2
|
+
`)&&r.report({node:e,messageId:S,*fix(o){yield o.replaceTextRange([s,c.range[0]],"")},loc:{start:{column:e.loc.start.column+1,line:e.loc.start.line},end:c.loc.start}}),n.length&&!n.includes(`
|
|
3
|
+
`)&&r.report({node:e,messageId:S,*fix(o){yield o.replaceTextRange([g.range[1],p],"")},loc:{start:g.loc.end,end:{column:e.loc.end.column-1,line:e.loc.end.line}}})}var U=u({name:w,meta:{type:"layout",docs:{description:"",recommended:"stylistic"},fixable:"code",schema:[],messages:{genericSpacingMismatch:"spacing mismatch"}},defaultOptions:[],create(e){let r=e.sourceCode;return{TSTypeParameterDeclaration:t=>P(t,e),TSTypeParameterInstantiation:t=>P(t,e),TSTypeParameter(t){if(!t.default)return;let i=t.constraint??t.name,s=t.default,p=i.range[1],c=s.range[0];r.text.slice(p,c)!==" = "&&e.report({node:t,messageId:S,*fix(g){yield g.replaceTextRange([p,c]," = ")},loc:{start:i.loc.end,end:s.loc.start}})}}}}),h=U;var l=require("@typescript-eslint/utils"),d=N(require("typescript"),1);var I=N(require("typescript"),1);function Y(e){return(e.flags&I.default.TypeFlags.Union)!==0}function L(e){return Y(e)?e.types:[e]}function j(e,r){return(e.flags&r)!==0}var E=j;function F(e,r){let t=e.getTypeAtLocation(r);return e.getBaseConstraintOfType(t)??t}var B="jsx-strict-logical-expressions",J=u({name:B,defaultOptions:[{allowString:!1,allowNumber:!1}],meta:{docs:{description:"Forbid non-boolean falsey values in inline expressions",recommended:"strict"},fixable:"code",type:"problem",schema:[{type:"object",properties:{allowString:{type:"boolean"},allowNumber:{type:"boolean"}},additionalProperties:!1}],messages:{conditionErrorFalseyString:"Potentially falsey string in logical AND expression. Please use boolean.",conditionErrorFalseyNumber:"Potentially falsey number in logical AND expression. Please use boolean."}},create(e,[r]){let t=l.ESLintUtils.getParserServices(e),i=t.program.getTypeChecker();function s(n){let o=t.esTreeNodeToTSNodeMap.get(n),a=L(F(i,o)),f=a.some(T=>E(T,d.default.TypeFlags.StringLike)&&(!T.isStringLiteral()||T.value===""));if(!r.allowString&&f)return"conditionErrorFalseyString";let C=a.some(T=>E(T,d.default.TypeFlags.NumberLike|d.default.TypeFlags.BigIntLike)&&(!T.isNumberLiteral()||T.value===0));if(!r.allowNumber&&C)return"conditionErrorFalseyNumber"}function p(n,o){let a=s(n);a&&e.report({node:n,messageId:a,fix:f=>f.insertTextBefore(o,"!!")})}function c(n){let o=n;return o.type===l.TSESTree.AST_NODE_TYPES.ChainExpression&&(o=o.expression),o.type===l.TSESTree.AST_NODE_TYPES.MemberExpression&&o.property.type!==l.TSESTree.AST_NODE_TYPES.PrivateIdentifier&&(o=o.property),o}function g(n,o){let a=c(n.left);if(a.type===l.TSESTree.AST_NODE_TYPES.LogicalExpression?g(a,!0):a.type===l.TSESTree.AST_NODE_TYPES.Identifier&&p(a,n.left),o){let f=c(n.right);f.type===l.TSESTree.AST_NODE_TYPES.Identifier&&p(f,n.right)}}function m(n){n.expression.type===l.TSESTree.AST_NODE_TYPES.LogicalExpression&&n.expression.operator==="&&"&&g(n.expression,!1)}return{JSXExpressionContainer:m}}}),A=J;var X={rules:{"generic-spacing":h,"jsx-strict-logical-expressions":A}},z=X;
|
package/dist/index.js
CHANGED
|
@@ -1,234 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
// src/rules/generic-spacing.ts
|
|
6
|
-
var RULE_NAME = "generic-spacing";
|
|
7
|
-
var messageId = "genericSpacingMismatch";
|
|
8
|
-
function removeSpaceAround(node, context) {
|
|
9
|
-
const params = node.params;
|
|
10
|
-
if (!params.length)
|
|
11
|
-
return;
|
|
12
|
-
const sourceCode = context.sourceCode;
|
|
13
|
-
const start = node.range[0] + 1;
|
|
14
|
-
const end = node.range[1] - 1;
|
|
15
|
-
const startNode = params[0];
|
|
16
|
-
const endNode = params.at(-1);
|
|
17
|
-
const textBeforeFirst = sourceCode.text.slice(start, startNode.range[0]);
|
|
18
|
-
const textAfterLast = sourceCode.text.slice(endNode.range[1], end);
|
|
19
|
-
if (textBeforeFirst.length && !textBeforeFirst.includes("\n")) {
|
|
20
|
-
context.report({
|
|
21
|
-
node,
|
|
22
|
-
messageId,
|
|
23
|
-
*fix(fixer) {
|
|
24
|
-
yield fixer.replaceTextRange([start, startNode.range[0]], "");
|
|
25
|
-
},
|
|
26
|
-
loc: {
|
|
27
|
-
start: {
|
|
28
|
-
column: node.loc.start.column + 1,
|
|
29
|
-
line: node.loc.start.line
|
|
30
|
-
},
|
|
31
|
-
end: startNode.loc.start
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
if (textAfterLast.length && !textAfterLast.includes("\n")) {
|
|
36
|
-
context.report({
|
|
37
|
-
node,
|
|
38
|
-
messageId,
|
|
39
|
-
*fix(fixer) {
|
|
40
|
-
yield fixer.replaceTextRange([endNode.range[1], end], "");
|
|
41
|
-
},
|
|
42
|
-
loc: {
|
|
43
|
-
start: endNode.loc.end,
|
|
44
|
-
end: {
|
|
45
|
-
column: node.loc.end.column - 1,
|
|
46
|
-
line: node.loc.end.line
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
var genericSpacing = createEslintRule({
|
|
53
|
-
name: RULE_NAME,
|
|
54
|
-
meta: {
|
|
55
|
-
type: "layout",
|
|
56
|
-
docs: {
|
|
57
|
-
description: "",
|
|
58
|
-
recommended: "stylistic"
|
|
59
|
-
},
|
|
60
|
-
fixable: "code",
|
|
61
|
-
schema: [],
|
|
62
|
-
messages: {
|
|
63
|
-
genericSpacingMismatch: "spacing mismatch"
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
defaultOptions: [],
|
|
67
|
-
create(context) {
|
|
68
|
-
const sourceCode = context.sourceCode;
|
|
69
|
-
return {
|
|
70
|
-
// type T<"K, S"> = any
|
|
71
|
-
TSTypeParameterDeclaration: (node) => removeSpaceAround(node, context),
|
|
72
|
-
// type T = Record<"K, S">
|
|
73
|
-
TSTypeParameterInstantiation: (node) => removeSpaceAround(node, context),
|
|
74
|
-
// type T<"K = any", "S = any"> = any
|
|
75
|
-
TSTypeParameter(node) {
|
|
76
|
-
if (!node.default)
|
|
77
|
-
return;
|
|
78
|
-
const startNode = node.constraint ?? node.name;
|
|
79
|
-
const endNode = node.default;
|
|
80
|
-
const start = startNode.range[1];
|
|
81
|
-
const end = endNode.range[0];
|
|
82
|
-
if (sourceCode.text.slice(start, end) !== " = ") {
|
|
83
|
-
context.report({
|
|
84
|
-
node,
|
|
85
|
-
messageId,
|
|
86
|
-
*fix(fixer) {
|
|
87
|
-
yield fixer.replaceTextRange([start, end], " = ");
|
|
88
|
-
},
|
|
89
|
-
loc: {
|
|
90
|
-
start: startNode.loc.end,
|
|
91
|
-
end: endNode.loc.start
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
var generic_spacing_default = genericSpacing;
|
|
100
|
-
|
|
101
|
-
// src/rules/jsx-strict-logical-expressions.ts
|
|
102
|
-
import { ESLintUtils as ESLintUtils2, TSESTree } from "@typescript-eslint/utils";
|
|
103
|
-
import * as ts2 from "typescript";
|
|
104
|
-
|
|
105
|
-
// src/tsutils.ts
|
|
106
|
-
import * as ts from "typescript";
|
|
107
|
-
function isUnionType(type) {
|
|
108
|
-
return (type.flags & ts.TypeFlags.Union) !== 0;
|
|
109
|
-
}
|
|
110
|
-
function unionTypeParts(type) {
|
|
111
|
-
return isUnionType(type) ? type.types : [type];
|
|
112
|
-
}
|
|
113
|
-
function isFlagSet(obj, flag) {
|
|
114
|
-
return (obj.flags & flag) !== 0;
|
|
115
|
-
}
|
|
116
|
-
var isTypeFlagSet = isFlagSet;
|
|
117
|
-
function getConstrainedTypeAtLocation(checker, node) {
|
|
118
|
-
const nodeType = checker.getTypeAtLocation(node);
|
|
119
|
-
const constrained = checker.getBaseConstraintOfType(nodeType);
|
|
120
|
-
return constrained ?? nodeType;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// src/rules/jsx-strict-logical-expressions.ts
|
|
124
|
-
var RULE_NAME2 = "jsx-strict-logical-expressions";
|
|
125
|
-
var jsxStrictLogicalExpressions = createEslintRule({
|
|
126
|
-
name: RULE_NAME2,
|
|
127
|
-
defaultOptions: [{ allowString: false, allowNumber: false }],
|
|
128
|
-
meta: {
|
|
129
|
-
docs: {
|
|
130
|
-
description: "Forbid non-boolean falsey values in inline expressions",
|
|
131
|
-
recommended: "strict"
|
|
132
|
-
},
|
|
133
|
-
fixable: "code",
|
|
134
|
-
type: "problem",
|
|
135
|
-
schema: [
|
|
136
|
-
{
|
|
137
|
-
type: "object",
|
|
138
|
-
properties: {
|
|
139
|
-
allowString: { type: "boolean" },
|
|
140
|
-
allowNumber: { type: "boolean" }
|
|
141
|
-
},
|
|
142
|
-
additionalProperties: false
|
|
143
|
-
}
|
|
144
|
-
],
|
|
145
|
-
messages: {
|
|
146
|
-
conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
|
|
147
|
-
conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
|
|
148
|
-
}
|
|
149
|
-
},
|
|
150
|
-
create(context, [options]) {
|
|
151
|
-
const parserServices = ESLintUtils2.getParserServices(context);
|
|
152
|
-
const typeChecker = parserServices.program.getTypeChecker();
|
|
153
|
-
function checkIdentifier(node) {
|
|
154
|
-
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
|
|
155
|
-
const types = unionTypeParts(
|
|
156
|
-
getConstrainedTypeAtLocation(typeChecker, tsNode)
|
|
157
|
-
);
|
|
158
|
-
const hasPotentiallyFalseyString = types.some(
|
|
159
|
-
(type) => isTypeFlagSet(type, ts2.TypeFlags.StringLike) && (!type.isStringLiteral() || type.value === "")
|
|
160
|
-
);
|
|
161
|
-
if (!options.allowString && hasPotentiallyFalseyString) {
|
|
162
|
-
return "conditionErrorFalseyString";
|
|
163
|
-
}
|
|
164
|
-
const hasPotentiallyFalseyNumber = types.some(
|
|
165
|
-
(type) => isTypeFlagSet(
|
|
166
|
-
type,
|
|
167
|
-
ts2.TypeFlags.NumberLike | ts2.TypeFlags.BigIntLike
|
|
168
|
-
) && (!type.isNumberLiteral() || type.value === 0)
|
|
169
|
-
);
|
|
170
|
-
if (!options.allowNumber && hasPotentiallyFalseyNumber) {
|
|
171
|
-
return "conditionErrorFalseyNumber";
|
|
172
|
-
}
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
function checkAndReportIdentifier(node, fixNode) {
|
|
176
|
-
const errorId = checkIdentifier(node);
|
|
177
|
-
if (errorId) {
|
|
178
|
-
context.report({
|
|
179
|
-
node,
|
|
180
|
-
messageId: errorId,
|
|
181
|
-
fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
function determineNode(originalNode) {
|
|
186
|
-
let nodeToEvaluate = originalNode;
|
|
187
|
-
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.ChainExpression) {
|
|
188
|
-
nodeToEvaluate = nodeToEvaluate.expression;
|
|
189
|
-
}
|
|
190
|
-
if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== TSESTree.AST_NODE_TYPES.PrivateIdentifier) {
|
|
191
|
-
nodeToEvaluate = nodeToEvaluate.property;
|
|
192
|
-
}
|
|
193
|
-
return nodeToEvaluate;
|
|
194
|
-
}
|
|
195
|
-
function checkLogicalExpression(expressionNode, checkRightNode) {
|
|
196
|
-
const leftNode = determineNode(expressionNode.left);
|
|
197
|
-
if (leftNode.type === TSESTree.AST_NODE_TYPES.LogicalExpression) {
|
|
198
|
-
checkLogicalExpression(leftNode, true);
|
|
199
|
-
} else if (leftNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
|
|
200
|
-
checkAndReportIdentifier(leftNode, expressionNode.left);
|
|
201
|
-
}
|
|
202
|
-
if (checkRightNode) {
|
|
203
|
-
const rightNode = determineNode(expressionNode.right);
|
|
204
|
-
if (rightNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
|
|
205
|
-
checkAndReportIdentifier(rightNode, expressionNode.right);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
function checkJSXExpression(node) {
|
|
210
|
-
if (node.expression.type === TSESTree.AST_NODE_TYPES.LogicalExpression && node.expression.operator === "&&") {
|
|
211
|
-
checkLogicalExpression(node.expression, false);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return {
|
|
215
|
-
JSXExpressionContainer: checkJSXExpression
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
var jsx_strict_logical_expressions_default = jsxStrictLogicalExpressions;
|
|
220
|
-
|
|
221
|
-
// src/index.ts
|
|
222
|
-
var ziloen = {
|
|
223
|
-
// meta: {
|
|
224
|
-
// name: "ziloen"
|
|
225
|
-
// },
|
|
226
|
-
rules: {
|
|
227
|
-
"generic-spacing": generic_spacing_default,
|
|
228
|
-
"jsx-strict-logical-expressions": jsx_strict_logical_expressions_default
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
var src_default = ziloen;
|
|
232
|
-
export {
|
|
233
|
-
src_default as default
|
|
234
|
-
};
|
|
1
|
+
import{ESLintUtils as I}from"@typescript-eslint/utils";var y=I.RuleCreator(e=>e);var L="generic-spacing",u="genericSpacingMismatch";function E(e,o){let t=e.params;if(!t.length)return;let a=o.sourceCode,p=e.range[0]+1,l=e.range[1]-1,i=t[0],c=t.at(-1),m=a.text.slice(p,i.range[0]),r=a.text.slice(c.range[1],l);m.length&&!m.includes(`
|
|
2
|
+
`)&&o.report({node:e,messageId:u,*fix(n){yield n.replaceTextRange([p,i.range[0]],"")},loc:{start:{column:e.loc.start.column+1,line:e.loc.start.line},end:i.loc.start}}),r.length&&!r.includes(`
|
|
3
|
+
`)&&o.report({node:e,messageId:u,*fix(n){yield n.replaceTextRange([c.range[1],l],"")},loc:{start:c.loc.end,end:{column:e.loc.end.column-1,line:e.loc.end.line}}})}var F=y({name:L,meta:{type:"layout",docs:{description:"",recommended:"stylistic"},fixable:"code",schema:[],messages:{genericSpacingMismatch:"spacing mismatch"}},defaultOptions:[],create(e){let o=e.sourceCode;return{TSTypeParameterDeclaration:t=>E(t,e),TSTypeParameterInstantiation:t=>E(t,e),TSTypeParameter(t){if(!t.default)return;let a=t.constraint??t.name,p=t.default,l=a.range[1],i=p.range[0];o.text.slice(l,i)!==" = "&&e.report({node:t,messageId:u,*fix(c){yield c.replaceTextRange([l,i]," = ")},loc:{start:a.loc.end,end:p.loc.start}})}}}}),x=F;import{ESLintUtils as O,TSESTree as T}from"@typescript-eslint/utils";import S from"typescript";import A from"typescript";function C(e){return(e.flags&A.TypeFlags.Union)!==0}function N(e){return C(e)?e.types:[e]}function _(e,o){return(e.flags&o)!==0}var d=_;function b(e,o){let t=e.getTypeAtLocation(o);return e.getBaseConstraintOfType(t)??t}var M="jsx-strict-logical-expressions",R=y({name:M,defaultOptions:[{allowString:!1,allowNumber:!1}],meta:{docs:{description:"Forbid non-boolean falsey values in inline expressions",recommended:"strict"},fixable:"code",type:"problem",schema:[{type:"object",properties:{allowString:{type:"boolean"},allowNumber:{type:"boolean"}},additionalProperties:!1}],messages:{conditionErrorFalseyString:"Potentially falsey string in logical AND expression. Please use boolean.",conditionErrorFalseyNumber:"Potentially falsey number in logical AND expression. Please use boolean."}},create(e,[o]){let t=O.getParserServices(e),a=t.program.getTypeChecker();function p(r){let n=t.esTreeNodeToTSNodeMap.get(r),s=N(b(a,n)),f=s.some(g=>d(g,S.TypeFlags.StringLike)&&(!g.isStringLiteral()||g.value===""));if(!o.allowString&&f)return"conditionErrorFalseyString";let h=s.some(g=>d(g,S.TypeFlags.NumberLike|S.TypeFlags.BigIntLike)&&(!g.isNumberLiteral()||g.value===0));if(!o.allowNumber&&h)return"conditionErrorFalseyNumber"}function l(r,n){let s=p(r);s&&e.report({node:r,messageId:s,fix:f=>f.insertTextBefore(n,"!!")})}function i(r){let n=r;return n.type===T.AST_NODE_TYPES.ChainExpression&&(n=n.expression),n.type===T.AST_NODE_TYPES.MemberExpression&&n.property.type!==T.AST_NODE_TYPES.PrivateIdentifier&&(n=n.property),n}function c(r,n){let s=i(r.left);if(s.type===T.AST_NODE_TYPES.LogicalExpression?c(s,!0):s.type===T.AST_NODE_TYPES.Identifier&&l(s,r.left),n){let f=i(r.right);f.type===T.AST_NODE_TYPES.Identifier&&l(f,r.right)}}function m(r){r.expression.type===T.AST_NODE_TYPES.LogicalExpression&&r.expression.operator==="&&"&&c(r.expression,!1)}return{JSXExpressionContainer:m}}}),P=R;var D={rules:{"generic-spacing":x,"jsx-strict-logical-expressions":P}},K=D;export{K as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-ziloen",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -14,19 +14,19 @@
|
|
|
14
14
|
"dist"
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
|
-
"build": "rimraf dist && tsup src/index.ts --format esm,cjs --clean",
|
|
17
|
+
"build": "rimraf dist && tsup src/index.ts --format esm,cjs --clean --minify",
|
|
18
18
|
"release": "pnpm run build && bumpp --commit --no-push && npm publish"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@types/eslint": "^8.44.
|
|
22
|
-
"@types/node": "^20.
|
|
23
|
-
"@typescript-eslint/rule-tester": "^6.
|
|
24
|
-
"@typescript-eslint/utils": "^6.
|
|
21
|
+
"@types/eslint": "^8.44.7",
|
|
22
|
+
"@types/node": "^20.9.0",
|
|
23
|
+
"@typescript-eslint/rule-tester": "^6.11.0",
|
|
24
|
+
"@typescript-eslint/utils": "^6.11.0",
|
|
25
25
|
"bumpp": "^9.2.0",
|
|
26
|
-
"eslint": "^8.
|
|
26
|
+
"eslint": "^8.53.0",
|
|
27
27
|
"rimraf": "^5.0.5",
|
|
28
28
|
"tsup": "^7.2.0",
|
|
29
29
|
"typescript": "^5.2.2",
|
|
30
30
|
"unbuild": "^2.0.0"
|
|
31
31
|
}
|
|
32
|
-
}
|
|
32
|
+
}
|