eslint-plugin-ziloen 0.0.106 → 0.0.108

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 CHANGED
@@ -1,10 +1,11 @@
1
1
  'use strict';
2
2
 
3
3
  const utils = require('@typescript-eslint/utils');
4
+ const typescript = require('typescript');
4
5
 
5
6
  const createEslintRule = utils.ESLintUtils.RuleCreator((ruleName) => ruleName);
6
7
 
7
- const RULE_NAME = "generic-spacing";
8
+ const RULE_NAME$1 = "generic-spacing";
8
9
  const messageId = "genericSpacingMismatch";
9
10
  function spacingAround(node, context) {
10
11
  const params = node.params;
@@ -44,7 +45,7 @@ function spacingAround(node, context) {
44
45
  }
45
46
  }
46
47
  const genericSpacing = createEslintRule({
47
- name: RULE_NAME,
48
+ name: RULE_NAME$1,
48
49
  meta: {
49
50
  type: "layout",
50
51
  docs: {
@@ -88,9 +89,122 @@ const genericSpacing = createEslintRule({
88
89
  }
89
90
  });
90
91
 
92
+ function isUnionType(type) {
93
+ return (type.flags & typescript.TypeFlags.Union) !== 0;
94
+ }
95
+ function unionTypeParts(type) {
96
+ return isUnionType(type) ? type.types : [type];
97
+ }
98
+ function isFlagSet(obj, flag) {
99
+ return (obj.flags & flag) !== 0;
100
+ }
101
+ const isTypeFlagSet = isFlagSet;
102
+ function getConstrainedTypeAtLocation(checker, node) {
103
+ const nodeType = checker.getTypeAtLocation(node);
104
+ const constrained = checker.getBaseConstraintOfType(nodeType);
105
+ return constrained ?? nodeType;
106
+ }
107
+
108
+ const RULE_NAME = "jsx-strict-logical-expressions";
109
+ const jsxStrictLogicalExpressions = createEslintRule({
110
+ name: RULE_NAME,
111
+ defaultOptions: [{ allowString: false, allowNumber: false }],
112
+ meta: {
113
+ docs: {
114
+ description: "Forbid non-boolean falsey values in inline expressions",
115
+ recommended: "strict"
116
+ },
117
+ fixable: "code",
118
+ type: "problem",
119
+ schema: [
120
+ {
121
+ type: "object",
122
+ properties: {
123
+ allowString: { type: "boolean" },
124
+ allowNumber: { type: "boolean" }
125
+ },
126
+ additionalProperties: false
127
+ }
128
+ ],
129
+ messages: {
130
+ conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
131
+ conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
132
+ }
133
+ },
134
+ create(context, [options]) {
135
+ const parserServices = utils.ESLintUtils.getParserServices(context);
136
+ const typeChecker = parserServices.program.getTypeChecker();
137
+ function checkIdentifier(node) {
138
+ const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
139
+ const types = unionTypeParts(
140
+ getConstrainedTypeAtLocation(typeChecker, tsNode)
141
+ );
142
+ const hasPotentiallyFalseyString = types.some(
143
+ (type) => isTypeFlagSet(type, typescript.TypeFlags.StringLike) && (!type.isStringLiteral() || type.value === "")
144
+ );
145
+ if (!options.allowString && hasPotentiallyFalseyString) {
146
+ return "conditionErrorFalseyString";
147
+ }
148
+ const hasPotentiallyFalseyNumber = types.some(
149
+ (type) => isTypeFlagSet(
150
+ type,
151
+ typescript.TypeFlags.NumberLike | typescript.TypeFlags.BigIntLike
152
+ ) && (!type.isNumberLiteral() || type.value === 0)
153
+ );
154
+ if (!options.allowNumber && hasPotentiallyFalseyNumber) {
155
+ return "conditionErrorFalseyNumber";
156
+ }
157
+ return;
158
+ }
159
+ function checkAndReportIdentifier(node, fixNode) {
160
+ const errorId = checkIdentifier(node);
161
+ if (errorId) {
162
+ context.report({
163
+ node,
164
+ messageId: errorId,
165
+ fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
166
+ });
167
+ }
168
+ }
169
+ function determineNode(originalNode) {
170
+ let nodeToEvaluate = originalNode;
171
+ if (nodeToEvaluate.type === utils.TSESTree.AST_NODE_TYPES.ChainExpression) {
172
+ nodeToEvaluate = nodeToEvaluate.expression;
173
+ }
174
+ if (nodeToEvaluate.type === utils.TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== utils.TSESTree.AST_NODE_TYPES.PrivateIdentifier) {
175
+ nodeToEvaluate = nodeToEvaluate.property;
176
+ }
177
+ return nodeToEvaluate;
178
+ }
179
+ function checkLogicalExpression(expressionNode, checkRightNode) {
180
+ const leftNode = determineNode(expressionNode.left);
181
+ if (leftNode.type === utils.TSESTree.AST_NODE_TYPES.LogicalExpression) {
182
+ checkLogicalExpression(leftNode, true);
183
+ } else if (leftNode.type === utils.TSESTree.AST_NODE_TYPES.Identifier) {
184
+ checkAndReportIdentifier(leftNode, expressionNode.left);
185
+ }
186
+ if (checkRightNode) {
187
+ const rightNode = determineNode(expressionNode.right);
188
+ if (rightNode.type === utils.TSESTree.AST_NODE_TYPES.Identifier) {
189
+ checkAndReportIdentifier(rightNode, expressionNode.right);
190
+ }
191
+ }
192
+ }
193
+ function checkJSXExpression(node) {
194
+ if (node.expression.type === utils.TSESTree.AST_NODE_TYPES.LogicalExpression && node.expression.operator === "&&") {
195
+ checkLogicalExpression(node.expression, false);
196
+ }
197
+ }
198
+ return {
199
+ JSXExpressionContainer: checkJSXExpression
200
+ };
201
+ }
202
+ });
203
+
91
204
  const index = {
92
205
  rules: {
93
- "generic-spacing": genericSpacing
206
+ "generic-spacing": genericSpacing,
207
+ "jsx-strict-logical-expressions": jsxStrictLogicalExpressions
94
208
  }
95
209
  };
96
210
 
package/dist/index.mjs CHANGED
@@ -1,8 +1,9 @@
1
- import { ESLintUtils } from '@typescript-eslint/utils';
1
+ import { ESLintUtils, TSESTree } from '@typescript-eslint/utils';
2
+ import { TypeFlags } from 'typescript';
2
3
 
3
4
  const createEslintRule = ESLintUtils.RuleCreator((ruleName) => ruleName);
4
5
 
5
- const RULE_NAME = "generic-spacing";
6
+ const RULE_NAME$1 = "generic-spacing";
6
7
  const messageId = "genericSpacingMismatch";
7
8
  function spacingAround(node, context) {
8
9
  const params = node.params;
@@ -42,7 +43,7 @@ function spacingAround(node, context) {
42
43
  }
43
44
  }
44
45
  const genericSpacing = createEslintRule({
45
- name: RULE_NAME,
46
+ name: RULE_NAME$1,
46
47
  meta: {
47
48
  type: "layout",
48
49
  docs: {
@@ -86,9 +87,122 @@ const genericSpacing = createEslintRule({
86
87
  }
87
88
  });
88
89
 
90
+ function isUnionType(type) {
91
+ return (type.flags & TypeFlags.Union) !== 0;
92
+ }
93
+ function unionTypeParts(type) {
94
+ return isUnionType(type) ? type.types : [type];
95
+ }
96
+ function isFlagSet(obj, flag) {
97
+ return (obj.flags & flag) !== 0;
98
+ }
99
+ const isTypeFlagSet = isFlagSet;
100
+ function getConstrainedTypeAtLocation(checker, node) {
101
+ const nodeType = checker.getTypeAtLocation(node);
102
+ const constrained = checker.getBaseConstraintOfType(nodeType);
103
+ return constrained ?? nodeType;
104
+ }
105
+
106
+ const RULE_NAME = "jsx-strict-logical-expressions";
107
+ const jsxStrictLogicalExpressions = createEslintRule({
108
+ name: RULE_NAME,
109
+ defaultOptions: [{ allowString: false, allowNumber: false }],
110
+ meta: {
111
+ docs: {
112
+ description: "Forbid non-boolean falsey values in inline expressions",
113
+ recommended: "strict"
114
+ },
115
+ fixable: "code",
116
+ type: "problem",
117
+ schema: [
118
+ {
119
+ type: "object",
120
+ properties: {
121
+ allowString: { type: "boolean" },
122
+ allowNumber: { type: "boolean" }
123
+ },
124
+ additionalProperties: false
125
+ }
126
+ ],
127
+ messages: {
128
+ conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
129
+ conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
130
+ }
131
+ },
132
+ create(context, [options]) {
133
+ const parserServices = ESLintUtils.getParserServices(context);
134
+ const typeChecker = parserServices.program.getTypeChecker();
135
+ function checkIdentifier(node) {
136
+ const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
137
+ const types = unionTypeParts(
138
+ getConstrainedTypeAtLocation(typeChecker, tsNode)
139
+ );
140
+ const hasPotentiallyFalseyString = types.some(
141
+ (type) => isTypeFlagSet(type, TypeFlags.StringLike) && (!type.isStringLiteral() || type.value === "")
142
+ );
143
+ if (!options.allowString && hasPotentiallyFalseyString) {
144
+ return "conditionErrorFalseyString";
145
+ }
146
+ const hasPotentiallyFalseyNumber = types.some(
147
+ (type) => isTypeFlagSet(
148
+ type,
149
+ TypeFlags.NumberLike | TypeFlags.BigIntLike
150
+ ) && (!type.isNumberLiteral() || type.value === 0)
151
+ );
152
+ if (!options.allowNumber && hasPotentiallyFalseyNumber) {
153
+ return "conditionErrorFalseyNumber";
154
+ }
155
+ return;
156
+ }
157
+ function checkAndReportIdentifier(node, fixNode) {
158
+ const errorId = checkIdentifier(node);
159
+ if (errorId) {
160
+ context.report({
161
+ node,
162
+ messageId: errorId,
163
+ fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
164
+ });
165
+ }
166
+ }
167
+ function determineNode(originalNode) {
168
+ let nodeToEvaluate = originalNode;
169
+ if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.ChainExpression) {
170
+ nodeToEvaluate = nodeToEvaluate.expression;
171
+ }
172
+ if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== TSESTree.AST_NODE_TYPES.PrivateIdentifier) {
173
+ nodeToEvaluate = nodeToEvaluate.property;
174
+ }
175
+ return nodeToEvaluate;
176
+ }
177
+ function checkLogicalExpression(expressionNode, checkRightNode) {
178
+ const leftNode = determineNode(expressionNode.left);
179
+ if (leftNode.type === TSESTree.AST_NODE_TYPES.LogicalExpression) {
180
+ checkLogicalExpression(leftNode, true);
181
+ } else if (leftNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
182
+ checkAndReportIdentifier(leftNode, expressionNode.left);
183
+ }
184
+ if (checkRightNode) {
185
+ const rightNode = determineNode(expressionNode.right);
186
+ if (rightNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
187
+ checkAndReportIdentifier(rightNode, expressionNode.right);
188
+ }
189
+ }
190
+ }
191
+ function checkJSXExpression(node) {
192
+ if (node.expression.type === TSESTree.AST_NODE_TYPES.LogicalExpression && node.expression.operator === "&&") {
193
+ checkLogicalExpression(node.expression, false);
194
+ }
195
+ }
196
+ return {
197
+ JSXExpressionContainer: checkJSXExpression
198
+ };
199
+ }
200
+ });
201
+
89
202
  const index = {
90
203
  rules: {
91
- "generic-spacing": genericSpacing
204
+ "generic-spacing": genericSpacing,
205
+ "jsx-strict-logical-expressions": jsxStrictLogicalExpressions
92
206
  }
93
207
  };
94
208
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-ziloen",
3
- "version": "0.0.106",
3
+ "version": "0.0.108",
4
4
  "author": "ziloen",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/ziloen/eslint-config",
@@ -10,7 +10,8 @@
10
10
  "dist"
11
11
  ],
12
12
  "dependencies": {
13
- "@typescript-eslint/utils": "^6.7.0"
13
+ "@typescript-eslint/utils": "^6.7.0",
14
+ "typescript": "^5.2.2"
14
15
  },
15
16
  "devDependencies": {
16
17
  "@types/node": "^20.6.0",