eslint-plugin-ziloen 0.1.0 → 0.1.1

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.js ADDED
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ default: () => src_default
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/utils.ts
28
+ var import_utils = require("@typescript-eslint/utils");
29
+ var createEslintRule = import_utils.ESLintUtils.RuleCreator((ruleName) => ruleName);
30
+
31
+ // src/rules/generic-spacing.ts
32
+ var RULE_NAME = "generic-spacing";
33
+ var messageId = "genericSpacingMismatch";
34
+ function removeSpaceAround(node, context) {
35
+ const params = node.params;
36
+ if (!params.length)
37
+ return;
38
+ const sourceCode = context.sourceCode;
39
+ const start = node.range[0] + 1;
40
+ const end = node.range[1] - 1;
41
+ const startNode = params[0];
42
+ const endNode = params.at(-1);
43
+ const textBeforeFirst = sourceCode.text.slice(start, startNode.range[0]);
44
+ const textAfterLast = sourceCode.text.slice(endNode.range[1], end);
45
+ if (textBeforeFirst.length && !textBeforeFirst.includes("\n")) {
46
+ context.report({
47
+ node,
48
+ messageId,
49
+ *fix(fixer) {
50
+ yield fixer.replaceTextRange([start, startNode.range[0]], "");
51
+ },
52
+ loc: {
53
+ start: {
54
+ column: node.loc.start.column + 1,
55
+ line: node.loc.start.line
56
+ },
57
+ end: startNode.loc.start
58
+ }
59
+ });
60
+ }
61
+ if (textAfterLast.length && !textAfterLast.includes("\n")) {
62
+ context.report({
63
+ node,
64
+ messageId,
65
+ *fix(fixer) {
66
+ yield fixer.replaceTextRange([endNode.range[1], end], "");
67
+ },
68
+ loc: {
69
+ start: endNode.loc.end,
70
+ end: {
71
+ column: node.loc.end.column - 1,
72
+ line: node.loc.end.line
73
+ }
74
+ }
75
+ });
76
+ }
77
+ }
78
+ var genericSpacing = createEslintRule({
79
+ name: RULE_NAME,
80
+ meta: {
81
+ type: "layout",
82
+ docs: {
83
+ description: "",
84
+ recommended: "stylistic"
85
+ },
86
+ fixable: "code",
87
+ schema: [],
88
+ messages: {
89
+ genericSpacingMismatch: "spacing mismatch"
90
+ }
91
+ },
92
+ defaultOptions: [],
93
+ create(context) {
94
+ const sourceCode = context.sourceCode;
95
+ return {
96
+ // type T<"K, S"> = any
97
+ TSTypeParameterDeclaration: (node) => removeSpaceAround(node, context),
98
+ // type T = Record<"K, S">
99
+ TSTypeParameterInstantiation: (node) => removeSpaceAround(node, context),
100
+ // type T<"K = any", "S = any"> = any
101
+ TSTypeParameter(node) {
102
+ if (!node.default)
103
+ return;
104
+ const startNode = node.constraint ?? node.name;
105
+ const endNode = node.default;
106
+ const start = startNode.range[1];
107
+ const end = endNode.range[0];
108
+ if (sourceCode.text.slice(start, end) !== " = ") {
109
+ context.report({
110
+ node,
111
+ messageId,
112
+ *fix(fixer) {
113
+ yield fixer.replaceTextRange([start, end], " = ");
114
+ },
115
+ loc: {
116
+ start: startNode.loc.end,
117
+ end: endNode.loc.start
118
+ }
119
+ });
120
+ }
121
+ }
122
+ };
123
+ }
124
+ });
125
+ var generic_spacing_default = genericSpacing;
126
+
127
+ // src/rules/jsx-strict-logical-expressions.ts
128
+ var import_utils3 = require("@typescript-eslint/utils");
129
+ var import_typescript2 = require("typescript");
130
+
131
+ // src/tsutils.ts
132
+ var import_typescript = require("typescript");
133
+ function isUnionType(type) {
134
+ return (type.flags & import_typescript.TypeFlags.Union) !== 0;
135
+ }
136
+ function unionTypeParts(type) {
137
+ return isUnionType(type) ? type.types : [type];
138
+ }
139
+ function isFlagSet(obj, flag) {
140
+ return (obj.flags & flag) !== 0;
141
+ }
142
+ var isTypeFlagSet = isFlagSet;
143
+ function getConstrainedTypeAtLocation(checker, node) {
144
+ const nodeType = checker.getTypeAtLocation(node);
145
+ const constrained = checker.getBaseConstraintOfType(nodeType);
146
+ return constrained ?? nodeType;
147
+ }
148
+
149
+ // src/rules/jsx-strict-logical-expressions.ts
150
+ var RULE_NAME2 = "jsx-strict-logical-expressions";
151
+ var jsxStrictLogicalExpressions = createEslintRule({
152
+ name: RULE_NAME2,
153
+ defaultOptions: [{ allowString: false, allowNumber: false }],
154
+ meta: {
155
+ docs: {
156
+ description: "Forbid non-boolean falsey values in inline expressions",
157
+ recommended: "strict"
158
+ },
159
+ fixable: "code",
160
+ type: "problem",
161
+ schema: [
162
+ {
163
+ type: "object",
164
+ properties: {
165
+ allowString: { type: "boolean" },
166
+ allowNumber: { type: "boolean" }
167
+ },
168
+ additionalProperties: false
169
+ }
170
+ ],
171
+ messages: {
172
+ conditionErrorFalseyString: "Potentially falsey string in logical AND expression. Please use boolean.",
173
+ conditionErrorFalseyNumber: "Potentially falsey number in logical AND expression. Please use boolean."
174
+ }
175
+ },
176
+ create(context, [options]) {
177
+ const parserServices = import_utils3.ESLintUtils.getParserServices(context);
178
+ const typeChecker = parserServices.program.getTypeChecker();
179
+ function checkIdentifier(node) {
180
+ const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
181
+ const types = unionTypeParts(
182
+ getConstrainedTypeAtLocation(typeChecker, tsNode)
183
+ );
184
+ const hasPotentiallyFalseyString = types.some(
185
+ (type) => isTypeFlagSet(type, import_typescript2.TypeFlags.StringLike) && (!type.isStringLiteral() || type.value === "")
186
+ );
187
+ if (!options.allowString && hasPotentiallyFalseyString) {
188
+ return "conditionErrorFalseyString";
189
+ }
190
+ const hasPotentiallyFalseyNumber = types.some(
191
+ (type) => isTypeFlagSet(
192
+ type,
193
+ import_typescript2.TypeFlags.NumberLike | import_typescript2.TypeFlags.BigIntLike
194
+ ) && (!type.isNumberLiteral() || type.value === 0)
195
+ );
196
+ if (!options.allowNumber && hasPotentiallyFalseyNumber) {
197
+ return "conditionErrorFalseyNumber";
198
+ }
199
+ return;
200
+ }
201
+ function checkAndReportIdentifier(node, fixNode) {
202
+ const errorId = checkIdentifier(node);
203
+ if (errorId) {
204
+ context.report({
205
+ node,
206
+ messageId: errorId,
207
+ fix: (fixer) => fixer.insertTextBefore(fixNode, "!!")
208
+ });
209
+ }
210
+ }
211
+ function determineNode(originalNode) {
212
+ let nodeToEvaluate = originalNode;
213
+ if (nodeToEvaluate.type === import_utils3.TSESTree.AST_NODE_TYPES.ChainExpression) {
214
+ nodeToEvaluate = nodeToEvaluate.expression;
215
+ }
216
+ if (nodeToEvaluate.type === import_utils3.TSESTree.AST_NODE_TYPES.MemberExpression && nodeToEvaluate.property.type !== import_utils3.TSESTree.AST_NODE_TYPES.PrivateIdentifier) {
217
+ nodeToEvaluate = nodeToEvaluate.property;
218
+ }
219
+ return nodeToEvaluate;
220
+ }
221
+ function checkLogicalExpression(expressionNode, checkRightNode) {
222
+ const leftNode = determineNode(expressionNode.left);
223
+ if (leftNode.type === import_utils3.TSESTree.AST_NODE_TYPES.LogicalExpression) {
224
+ checkLogicalExpression(leftNode, true);
225
+ } else if (leftNode.type === import_utils3.TSESTree.AST_NODE_TYPES.Identifier) {
226
+ checkAndReportIdentifier(leftNode, expressionNode.left);
227
+ }
228
+ if (checkRightNode) {
229
+ const rightNode = determineNode(expressionNode.right);
230
+ if (rightNode.type === import_utils3.TSESTree.AST_NODE_TYPES.Identifier) {
231
+ checkAndReportIdentifier(rightNode, expressionNode.right);
232
+ }
233
+ }
234
+ }
235
+ function checkJSXExpression(node) {
236
+ if (node.expression.type === import_utils3.TSESTree.AST_NODE_TYPES.LogicalExpression && node.expression.operator === "&&") {
237
+ checkLogicalExpression(node.expression, false);
238
+ }
239
+ }
240
+ return {
241
+ JSXExpressionContainer: checkJSXExpression
242
+ };
243
+ }
244
+ });
245
+ var jsx_strict_logical_expressions_default = jsxStrictLogicalExpressions;
246
+
247
+ // src/index.ts
248
+ var ziloen = {
249
+ meta: {
250
+ name: "ziloen"
251
+ },
252
+ rules: {
253
+ "generic-spacing": generic_spacing_default,
254
+ "jsx-strict-logical-expressions": jsx_strict_logical_expressions_default
255
+ }
256
+ };
257
+ var src_default = ziloen;
package/dist/index.mjs ADDED
@@ -0,0 +1,234 @@
1
+ // src/utils.ts
2
+ import { ESLintUtils } from "@typescript-eslint/utils";
3
+ var createEslintRule = ESLintUtils.RuleCreator((ruleName) => ruleName);
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 { TypeFlags as TypeFlags2 } from "typescript";
104
+
105
+ // src/tsutils.ts
106
+ import { TypeFlags } from "typescript";
107
+ function isUnionType(type) {
108
+ return (type.flags & 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, TypeFlags2.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
+ TypeFlags2.NumberLike | TypeFlags2.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
+ };
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "eslint-plugin-ziloen",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "main": "./dist/index.js",
5
+ "files": [
6
+ "dist"
7
+ ],
5
8
  "scripts": {
6
9
  "build": "rimraf dist && tsup src/index.ts --format esm,cjs --clean",
7
10
  "release": "pnpm run build && bumpp --commit --no-push && npm publish"
@@ -1,7 +0,0 @@
1
- {
2
- "prettier.enable": false,
3
- "editor.codeActionsOnSave": {
4
- "source.fixAll.eslint": true,
5
- "source.sortImports": true
6
- }
7
- }
package/build.config.ts DELETED
@@ -1,12 +0,0 @@
1
- import { defineBuildConfig } from "unbuild"
2
-
3
- export default defineBuildConfig({
4
- entries: [
5
- "src/index"
6
- ],
7
- declaration: false,
8
- clean: true,
9
- rollup: {
10
- emitCJS: true
11
- }
12
- })
package/src/index.ts DELETED
@@ -1,14 +0,0 @@
1
- import genericSpacing from './rules/generic-spacing'
2
- import jsxStrictLogicalExpressions from './rules/jsx-strict-logical-expressions'
3
-
4
- const ziloen = {
5
- meta: {
6
- name: "ziloen"
7
- },
8
- rules: {
9
- 'generic-spacing': genericSpacing,
10
- 'jsx-strict-logical-expressions': jsxStrictLogicalExpressions,
11
- }
12
- }
13
-
14
- export default ziloen
@@ -1,40 +0,0 @@
1
- import { RuleTester } from '@typescript-eslint/rule-tester'
2
- import rule, { RULE_NAME, messageId } from './generic-spacing'
3
-
4
- const valid = [
5
- `type Foo<T, K> = T`,
6
- `type Foo<
7
- T,
8
- K,
9
- > = T`,
10
- `type Foo<T, K> = Record<T, K>`,
11
- `type Foo<T, K> = Record<
12
- T,
13
- K
14
- >`
15
- ]
16
-
17
- const invalid = [
18
- [`type Foo< T, K > = T`, `type Foo<T, K> = T`, 2],
19
- [`type Foo<T, K> = Record< T, K >`, `type Foo<T, K> = Record<T, K>`, 2],
20
- [`type Foo< T, K > = Record< T, K >`, `type Foo<T, K> = Record<T, K>`, 4],
21
- [`type Foo<
22
- T, K
23
- > = Record< T, K >`, `type Foo<
24
- T, K
25
- > = Record<T, K>`, 2],
26
- [`type Foo<T=any, K=any> = T`, `type Foo<T = any, K = any> = T`, 2]
27
- ] as const
28
-
29
- const ruleTester: RuleTester = new RuleTester({
30
- parser: require.resolve('@typescript-eslint/parser')
31
- })
32
-
33
- ruleTester.run(RULE_NAME, rule, {
34
- valid,
35
- invalid: invalid.map(i => ({
36
- code: i[0],
37
- output: i[1].trim(),
38
- errors: Array.from({ length: i[2] || 1 }, () => ({ messageId }))
39
- }))
40
- })
@@ -1,118 +0,0 @@
1
- import type { TSESTree } from '@typescript-eslint/utils'
2
- import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'
3
- import { createEslintRule } from '../utils'
4
-
5
-
6
- export const RULE_NAME = 'generic-spacing'
7
- export type MessageIds = 'genericSpacingMismatch'
8
- export type Options = []
9
-
10
- export const messageId = 'genericSpacingMismatch'
11
-
12
- function removeSpaceAround(
13
- node: TSESTree.TSTypeParameterInstantiation | TSESTree.TSTypeParameterDeclaration,
14
- context: Readonly<RuleContext<MessageIds, []>>
15
- ) {
16
- const params = node.params
17
- if (!params.length) return
18
-
19
- const sourceCode = context.sourceCode
20
-
21
- const start = node.range[0] + 1
22
- const end = node.range[1] - 1
23
-
24
- const startNode = params[0]
25
- const endNode = params.at(-1)!
26
-
27
- const textBeforeFirst = sourceCode.text.slice(start, startNode.range[0])
28
- const textAfterLast = sourceCode.text.slice(endNode.range[1], end)
29
-
30
- // if same line, remove spaces
31
- if (textBeforeFirst.length && !textBeforeFirst.includes('\n')) {
32
- context.report({
33
- node,
34
- messageId,
35
- *fix(fixer) {
36
- yield fixer.replaceTextRange([start, startNode.range[0]], '')
37
- },
38
- loc: {
39
- start: {
40
- column: node.loc.start.column + 1,
41
- line: node.loc.start.line
42
- },
43
- end: startNode.loc.start
44
- }
45
- })
46
- }
47
-
48
- // if same line, remove spaces
49
- if (textAfterLast.length && !textAfterLast.includes('\n')) {
50
- context.report({
51
- node,
52
- messageId,
53
- *fix(fixer) {
54
- yield fixer.replaceTextRange([endNode.range[1], end], '')
55
- },
56
- loc: {
57
- start: endNode.loc.end,
58
- end: {
59
- column: node.loc.end.column - 1,
60
- line: node.loc.end.line
61
- }
62
- }
63
- })
64
- }
65
- }
66
-
67
- const genericSpacing = createEslintRule<Options, MessageIds>({
68
- name: RULE_NAME,
69
- meta: {
70
- type: 'layout',
71
- docs: {
72
- description: '',
73
- recommended: 'stylistic',
74
- },
75
- fixable: 'code',
76
- schema: [],
77
- messages: {
78
- genericSpacingMismatch: 'spacing mismatch'
79
- }
80
- },
81
- defaultOptions: [],
82
- create(context) {
83
- const sourceCode = context.sourceCode
84
-
85
- return {
86
- // type T<"K, S"> = any
87
- TSTypeParameterDeclaration: node => removeSpaceAround(node, context),
88
- // type T = Record<"K, S">
89
- TSTypeParameterInstantiation: node => removeSpaceAround(node, context),
90
- // type T<"K = any", "S = any"> = any
91
- TSTypeParameter(node) {
92
- if (!node.default) return
93
- const startNode = node.constraint ?? node.name
94
- const endNode = node.default
95
-
96
- const start = startNode.range[1]
97
- const end = endNode.range[0]
98
-
99
- // replace all content between name and default with " = "
100
- if (sourceCode.text.slice(start, end) !== ' = ') {
101
- context.report({
102
- node,
103
- messageId,
104
- *fix(fixer) {
105
- yield fixer.replaceTextRange([start, end], ' = ')
106
- },
107
- loc: {
108
- start: startNode.loc.end,
109
- end: endNode.loc.start
110
- }
111
- })
112
- }
113
- }
114
- }
115
- }
116
- })
117
-
118
- export default genericSpacing
@@ -1,159 +0,0 @@
1
- /**
2
- * Copied from https://github.com/hluisson/eslint-plugin-jsx-expressions
3
- */
4
-
5
- import { ESLintUtils, TSESTree } from '@typescript-eslint/utils'
6
- import { TypeFlags } from 'typescript'
7
- import { getConstrainedTypeAtLocation, isTypeFlagSet, unionTypeParts } from '../tsutils'
8
- import { createEslintRule } from '../utils'
9
-
10
- type Options = [
11
- {
12
- allowString?: boolean
13
- allowNumber?: boolean
14
- }
15
- ]
16
-
17
- type MessageIds = 'conditionErrorFalseyString' | 'conditionErrorFalseyNumber'
18
-
19
- export const RULE_NAME = 'jsx-strict-logical-expressions'
20
-
21
- //------------------------------------------------------------------------------
22
- // Rule Definition
23
- //------------------------------------------------------------------------------
24
-
25
- const jsxStrictLogicalExpressions = createEslintRule<Options, MessageIds>({
26
- name: RULE_NAME,
27
- defaultOptions: [{ allowString: false, allowNumber: false }],
28
- meta: {
29
- docs: {
30
- description: 'Forbid non-boolean falsey values in inline expressions',
31
- recommended: 'strict',
32
- },
33
- fixable: 'code',
34
- type: 'problem',
35
- schema: [
36
- {
37
- type: 'object',
38
- properties: {
39
- allowString: { type: 'boolean' },
40
- allowNumber: { type: 'boolean' },
41
- },
42
- additionalProperties: false,
43
- },
44
- ],
45
- messages: {
46
- conditionErrorFalseyString:
47
- 'Potentially falsey string in logical AND expression. Please use boolean.',
48
- conditionErrorFalseyNumber:
49
- 'Potentially falsey number in logical AND expression. Please use boolean.',
50
- },
51
- },
52
-
53
- create(context, [options]) {
54
- const parserServices = ESLintUtils.getParserServices(context)
55
- const typeChecker = parserServices.program.getTypeChecker()
56
-
57
- function checkIdentifier(
58
- node: TSESTree.Identifier
59
- ): MessageIds | undefined {
60
- const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node)
61
- const types = unionTypeParts(
62
- getConstrainedTypeAtLocation(typeChecker, tsNode)
63
- )
64
-
65
- const hasPotentiallyFalseyString = types.some(
66
- type =>
67
- isTypeFlagSet(type, TypeFlags.StringLike) &&
68
- (!type.isStringLiteral() || type.value === '')
69
- )
70
-
71
- if (!options.allowString && hasPotentiallyFalseyString) {
72
- return 'conditionErrorFalseyString'
73
- }
74
-
75
- const hasPotentiallyFalseyNumber = types.some(
76
- type =>
77
- isTypeFlagSet(
78
- type,
79
- TypeFlags.NumberLike | TypeFlags.BigIntLike
80
- ) &&
81
- (!type.isNumberLiteral() || type.value === 0)
82
- )
83
-
84
- if (!options.allowNumber && hasPotentiallyFalseyNumber) {
85
- return 'conditionErrorFalseyNumber'
86
- }
87
-
88
- return
89
- }
90
-
91
- function checkAndReportIdentifier(
92
- node: TSESTree.Identifier,
93
- fixNode: TSESTree.Expression
94
- ) {
95
- const errorId = checkIdentifier(node)
96
- if (errorId) {
97
- context.report({
98
- node,
99
- messageId: errorId,
100
- fix: fixer => fixer.insertTextBefore(fixNode, '!!'),
101
- })
102
- }
103
- }
104
-
105
- // Return the core identifier or expression
106
- function determineNode(originalNode: TSESTree.Expression) {
107
- let nodeToEvaluate = originalNode
108
- if (nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.ChainExpression) {
109
- nodeToEvaluate = nodeToEvaluate.expression
110
- }
111
-
112
- if (
113
- nodeToEvaluate.type === TSESTree.AST_NODE_TYPES.MemberExpression &&
114
- nodeToEvaluate.property.type !==
115
- TSESTree.AST_NODE_TYPES.PrivateIdentifier
116
- ) {
117
- nodeToEvaluate = nodeToEvaluate.property
118
- }
119
-
120
- return nodeToEvaluate
121
- }
122
-
123
- function checkLogicalExpression(
124
- expressionNode: TSESTree.LogicalExpression,
125
- checkRightNode: boolean
126
- ) {
127
- const leftNode = determineNode(expressionNode.left)
128
-
129
- if (leftNode.type === TSESTree.AST_NODE_TYPES.LogicalExpression) {
130
- checkLogicalExpression(leftNode, true)
131
- } else if (leftNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
132
- checkAndReportIdentifier(leftNode, expressionNode.left)
133
- }
134
-
135
- if (checkRightNode) {
136
- const rightNode = determineNode(expressionNode.right)
137
-
138
- if (rightNode.type === TSESTree.AST_NODE_TYPES.Identifier) {
139
- checkAndReportIdentifier(rightNode, expressionNode.right)
140
- }
141
- }
142
- }
143
-
144
- function checkJSXExpression(node: TSESTree.JSXExpressionContainer) {
145
- if (
146
- node.expression.type === TSESTree.AST_NODE_TYPES.LogicalExpression &&
147
- node.expression.operator === '&&'
148
- ) {
149
- checkLogicalExpression(node.expression, false)
150
- }
151
- }
152
-
153
- return {
154
- JSXExpressionContainer: checkJSXExpression,
155
- }
156
- },
157
- })
158
-
159
- export default jsxStrictLogicalExpressions
package/src/tsutils.ts DELETED
@@ -1,33 +0,0 @@
1
- import { TypeFlags, type Node, type Type, type TypeChecker, type UnionType } from 'typescript'
2
-
3
-
4
- export function isUnionType(type: Type): type is UnionType {
5
- return (type.flags & TypeFlags.Union) !== 0
6
- }
7
-
8
- /** Returns all types of a union type or an array containing `type` itself if it's no union type. */
9
- export function unionTypeParts(type: Type): Type[] {
10
- return isUnionType(type) ? type.types : [type]
11
- }
12
-
13
-
14
- function isFlagSet(obj: { flags: number }, flag: number) {
15
- return (obj.flags & flag) !== 0
16
- }
17
-
18
- export const isTypeFlagSet: (type: Type, flag: TypeFlags) => boolean = isFlagSet
19
-
20
-
21
-
22
- /**
23
- * Resolves the given node's type. Will resolve to the type's generic constraint, if it has one.
24
- */
25
- export function getConstrainedTypeAtLocation(
26
- checker: TypeChecker,
27
- node: Node
28
- ): Type {
29
- const nodeType = checker.getTypeAtLocation(node)
30
- const constrained = checker.getBaseConstraintOfType(nodeType)
31
-
32
- return constrained ?? nodeType
33
- }
package/src/utils.ts DELETED
@@ -1,3 +0,0 @@
1
- import { ESLintUtils } from '@typescript-eslint/utils'
2
-
3
- export const createEslintRule = ESLintUtils.RuleCreator(ruleName => ruleName)
package/tsconfig.json DELETED
@@ -1,18 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "lib": [
5
- "ESNext"
6
- ],
7
- "module": "ESNext",
8
- "moduleResolution": "Bundler",
9
- "resolveJsonModule": true,
10
- "strict": true,
11
- "esModuleInterop": true,
12
- "skipDefaultLibCheck": true,
13
- "skipLibCheck": true
14
- },
15
- "include": [
16
- "src"
17
- ]
18
- }