@arnaud-barre/tsl-config 0.1.5 → 0.2.0
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/README.md +3 -99
- package/index.d.ts +6 -13
- package/index.js +22 -59
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# @arnaud-barre/tsl-config [](https://www.npmjs.com/package/@arnaud-barre/tsl-config)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Custom type-aware rules for https://github.com/ArnaudBarre/tsl
|
|
4
|
+
To be used with https://npmx.dev/package/@arnaud-barre/oxlint-config
|
|
4
5
|
|
|
5
6
|
## Install
|
|
6
7
|
|
|
@@ -25,116 +26,19 @@ export default defineConfig({
|
|
|
25
26
|
}
|
|
26
27
|
```
|
|
27
28
|
|
|
28
|
-
## TS config
|
|
29
|
-
|
|
30
|
-
### Web project
|
|
29
|
+
## TS config
|
|
31
30
|
|
|
32
31
|
```json
|
|
33
32
|
{
|
|
34
|
-
"include": ["**/*.ts", "**/*.tsx"],
|
|
35
33
|
"compilerOptions": {
|
|
36
|
-
"target": "ES2022",
|
|
37
|
-
"jsx": "react-jsx",
|
|
38
|
-
"module": "ESNext",
|
|
39
|
-
"moduleDetection": "force",
|
|
40
|
-
"lib": ["ES2022", "DOM"],
|
|
41
|
-
"types": ["vite/client", "tsl/patches"],
|
|
42
|
-
"skipLibCheck": true,
|
|
43
|
-
|
|
44
|
-
/* Bundler mode */
|
|
45
|
-
"moduleResolution": "bundler",
|
|
46
|
-
"allowImportingTsExtensions": true,
|
|
47
|
-
"verbatimModuleSyntax": true,
|
|
48
|
-
"erasableSyntaxOnly": true,
|
|
49
|
-
"noEmit": true,
|
|
50
|
-
|
|
51
|
-
/* Linting */
|
|
52
|
-
"noUnusedLocals": true,
|
|
53
|
-
"noUnusedParameters": true,
|
|
54
|
-
"noFallthroughCasesInSwitch": true,
|
|
55
|
-
"useUnknownInCatchVariables": true,
|
|
56
|
-
"noPropertyAccessFromIndexSignature": true,
|
|
57
|
-
|
|
58
34
|
"plugins": [{ "name": "tsl/plugin" }]
|
|
59
35
|
}
|
|
60
36
|
}
|
|
61
37
|
```
|
|
62
38
|
|
|
63
|
-
### Node project (22)
|
|
64
|
-
|
|
65
|
-
```json
|
|
66
|
-
{
|
|
67
|
-
"include": ["**/*.ts"],
|
|
68
|
-
"compilerOptions": {
|
|
69
|
-
"target": "ES2022",
|
|
70
|
-
"module": "ESNext",
|
|
71
|
-
"moduleDetection": "force",
|
|
72
|
-
"lib": ["es2024", "ESNext.Array", "ESNext.Collection", "ESNext.Iterator"],
|
|
73
|
-
"types": ["node", "tsl/patches"],
|
|
74
|
-
"skipLibCheck": true
|
|
75
|
-
|
|
76
|
-
/* Same as web */
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### Node project (24)
|
|
82
|
-
|
|
83
|
-
```json
|
|
84
|
-
{
|
|
85
|
-
"include": ["**/*.ts"],
|
|
86
|
-
"compilerOptions": {
|
|
87
|
-
"target": "ES2024",
|
|
88
|
-
"module": "ESNext",
|
|
89
|
-
"moduleDetection": "force",
|
|
90
|
-
"lib": [
|
|
91
|
-
"es2024",
|
|
92
|
-
"ESNext.Array",
|
|
93
|
-
"ESNext.Collection",
|
|
94
|
-
"ESNext.Iterator",
|
|
95
|
-
"ESNext.Promise"
|
|
96
|
-
],
|
|
97
|
-
"types": ["node", "tsl/patches"],
|
|
98
|
-
"skipLibCheck": true
|
|
99
|
-
|
|
100
|
-
/* Same as web */
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
## Prettier config (3.6.2)
|
|
106
|
-
|
|
107
|
-
### Web projects
|
|
108
|
-
|
|
109
|
-
```json
|
|
110
|
-
// package.json
|
|
111
|
-
"prettier": {
|
|
112
|
-
"experimentalOperatorPosition": "start",
|
|
113
|
-
"xmlWhitespaceSensitivity": "ignore",
|
|
114
|
-
"plugins": [
|
|
115
|
-
"@arnaud-barre/prettier-plugin-sort-imports",
|
|
116
|
-
"@prettier/plugin-xml"
|
|
117
|
-
]
|
|
118
|
-
}
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### Node projects
|
|
122
|
-
|
|
123
|
-
```json
|
|
124
|
-
// package.json
|
|
125
|
-
"prettier": {
|
|
126
|
-
"experimentalOperatorPosition": "start",
|
|
127
|
-
"plugins": [
|
|
128
|
-
"@arnaud-barre/prettier-plugin-sort-imports"
|
|
129
|
-
]
|
|
130
|
-
}
|
|
131
|
-
```
|
|
132
|
-
|
|
133
39
|
### Custom rules
|
|
134
40
|
|
|
135
41
|
- arrayCallbackReturn: Ensure array callbacks that require a return value (map, filter, find, some, ...) don't have undefined in their return type
|
|
136
|
-
- arrayNoEmptyVariadic: Disallow empty variadic arguments to array methods (push, unshift, concat)
|
|
137
42
|
- jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
|
|
138
43
|
- jsxKey: Validate JSX has key prop when in array or iterator
|
|
139
44
|
- jsxNoNumberTruthiness: Disallow `list.length && ...` in JSX
|
|
140
|
-
- typedEmptyArray: Ensure empty arrays declarations have an explicit type annotation
|
package/index.d.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
arrayNoEmptyVariadic: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
7
|
-
jsxFragmentCheckChildren: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
8
|
-
jsxKey: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
9
|
-
jsxNoNumberTruthiness: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
10
|
-
typedEmptyArray: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
1
|
+
export declare const arnaudBarre: import("tsl").RulesSet<{
|
|
2
|
+
arrayCallbackReturn: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
3
|
+
jsxFragmentCheckChildren: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
4
|
+
jsxKey: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
5
|
+
jsxNoNumberTruthiness: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
11
6
|
}>;
|
|
12
|
-
declare const allRules:
|
|
13
|
-
//#endregion
|
|
14
|
-
export { allRules, arnaudBarre };
|
|
7
|
+
export declare const allRules: import("tsl").Rule<unknown>[];
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { core, createRulesSet, defineRule } from "tsl";
|
|
2
2
|
import { JsxEmit, SyntaxKind, TypeFlags } from "typescript";
|
|
3
|
-
|
|
3
|
+
// src/rules/arrayCallbackReturn.ts
|
|
4
4
|
const base = "Found undefined in the return type of this callback. It could come from a missing return statement.";
|
|
5
5
|
const messages$1 = {
|
|
6
6
|
shouldBeBoolean: base + " Update the code to return a boolean value.",
|
|
@@ -19,11 +19,11 @@ const methodsRecords = {
|
|
|
19
19
|
reduce: "addTypeAnnotation",
|
|
20
20
|
reduceRight: "addTypeAnnotation"
|
|
21
21
|
};
|
|
22
|
-
const methods
|
|
22
|
+
const methods = Object.keys(methodsRecords);
|
|
23
23
|
const arrayCallbackReturn = defineRule(() => ({
|
|
24
24
|
name: "arnaudBarre/arrayCallbackReturn",
|
|
25
25
|
visitor: { PropertyAccessExpression(context, node) {
|
|
26
|
-
if (node.name.kind === SyntaxKind.Identifier && methods
|
|
26
|
+
if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length >= 1) {
|
|
27
27
|
const callback = node.parent.arguments[0];
|
|
28
28
|
if (callback.kind === SyntaxKind.ArrowFunction && callback.body.kind !== SyntaxKind.Block) return;
|
|
29
29
|
const signatures = context.checker.getTypeAtLocation(callback).getCallSignatures();
|
|
@@ -42,25 +42,7 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
42
42
|
}
|
|
43
43
|
} }
|
|
44
44
|
}));
|
|
45
|
-
|
|
46
|
-
//#region src/rules/arrayNoEmptyVariadic.ts
|
|
47
|
-
const methods = [
|
|
48
|
-
"push",
|
|
49
|
-
"unshift",
|
|
50
|
-
"concat"
|
|
51
|
-
];
|
|
52
|
-
const arrayNoEmptyVariadic = defineRule(() => ({
|
|
53
|
-
name: "arnaudBarre/arrayNoEmptyVariadic",
|
|
54
|
-
visitor: { PropertyAccessExpression(context, node) {
|
|
55
|
-
if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length === 0) context.report({
|
|
56
|
-
start: node.name.getStart(),
|
|
57
|
-
end: node.parent.getEnd(),
|
|
58
|
-
message: "This call does nothing"
|
|
59
|
-
});
|
|
60
|
-
} }
|
|
61
|
-
}));
|
|
62
|
-
//#endregion
|
|
63
|
-
//#region src/rules/jsxFragmentCheckChildren.ts
|
|
45
|
+
// src/rules/jsxFragmentCheckChildren.ts
|
|
64
46
|
const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
|
|
65
47
|
const jsxFragmentCheckChildren = defineRule(() => ({
|
|
66
48
|
name: "arnaudBarre/jsxFragmentCheckChildren",
|
|
@@ -99,8 +81,7 @@ function getReactNodeSymbol(program) {
|
|
|
99
81
|
weekMap.set(program, symbol);
|
|
100
82
|
return symbol;
|
|
101
83
|
}
|
|
102
|
-
|
|
103
|
-
//#region src/rules/jsxKey.ts
|
|
84
|
+
// src/rules/jsxKey.ts
|
|
104
85
|
const reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
|
|
105
86
|
const messages = {
|
|
106
87
|
missingKey: "Missing key",
|
|
@@ -163,10 +144,7 @@ const jsxKey = defineRule(() => {
|
|
|
163
144
|
case SyntaxKind.SatisfiesExpression:
|
|
164
145
|
case SyntaxKind.TypeAssertionExpression:
|
|
165
146
|
case SyntaxKind.NonNullExpression:
|
|
166
|
-
case SyntaxKind.AsExpression:
|
|
167
|
-
checkExpression(node.expression, context);
|
|
168
|
-
break;
|
|
169
|
-
default: break;
|
|
147
|
+
case SyntaxKind.AsExpression: checkExpression(node.expression, context);
|
|
170
148
|
}
|
|
171
149
|
};
|
|
172
150
|
const checkStatement = (node, context) => {
|
|
@@ -184,10 +162,7 @@ const jsxKey = defineRule(() => {
|
|
|
184
162
|
checkStatement(node.thenStatement, context);
|
|
185
163
|
if (node.elseStatement) checkStatement(node.elseStatement, context);
|
|
186
164
|
break;
|
|
187
|
-
case SyntaxKind.SwitchStatement:
|
|
188
|
-
for (const caseClause of node.caseBlock.clauses) for (const statement of caseClause.statements) checkStatement(statement, context);
|
|
189
|
-
break;
|
|
190
|
-
default: break;
|
|
165
|
+
case SyntaxKind.SwitchStatement: for (const caseClause of node.caseBlock.clauses) for (const statement of caseClause.statements) checkStatement(statement, context);
|
|
191
166
|
}
|
|
192
167
|
};
|
|
193
168
|
const checkIfReactComponentWithKeyInProps = (id, fnNode, context) => {
|
|
@@ -230,37 +205,26 @@ const jsxKey = defineRule(() => {
|
|
|
230
205
|
}
|
|
231
206
|
};
|
|
232
207
|
});
|
|
233
|
-
|
|
234
|
-
//#region src/rules/jsxNoNumberTruthiness.ts
|
|
208
|
+
// src/rules/jsxNoNumberTruthiness.ts
|
|
235
209
|
const message = "Don't use logical expression on a number inside JSX, you might render the character 0 instead of rendering nothing. Either use a ternary expression or more explicit condition";
|
|
236
|
-
|
|
237
|
-
|
|
210
|
+
const jsxNoNumberTruthiness = defineRule(() => ({
|
|
211
|
+
name: "arnaudBarre/jsxNoNumberTruthiness",
|
|
212
|
+
visitor: { BinaryExpression(context, node) {
|
|
213
|
+
if (node.operatorToken.kind !== SyntaxKind.AmpersandAmpersandToken) return;
|
|
214
|
+
if (node.parent.kind !== SyntaxKind.JsxExpression) return;
|
|
215
|
+
const type = context.checker.getTypeAtLocation(node.left);
|
|
216
|
+
if (context.utils.typeOrUnionHasFlag(type, TypeFlags.NumberLike)) context.report({
|
|
217
|
+
node,
|
|
218
|
+
message
|
|
219
|
+
});
|
|
220
|
+
} }
|
|
221
|
+
}));
|
|
222
|
+
// src/index.ts
|
|
238
223
|
const arnaudBarre = createRulesSet({
|
|
239
224
|
arrayCallbackReturn,
|
|
240
|
-
arrayNoEmptyVariadic,
|
|
241
225
|
jsxFragmentCheckChildren,
|
|
242
226
|
jsxKey,
|
|
243
|
-
jsxNoNumberTruthiness
|
|
244
|
-
name: "arnaudBarre/jsxNoNumberTruthiness",
|
|
245
|
-
visitor: { BinaryExpression(context, node) {
|
|
246
|
-
if (node.operatorToken.kind !== SyntaxKind.AmpersandAmpersandToken) return;
|
|
247
|
-
if (node.parent.kind !== SyntaxKind.JsxExpression) return;
|
|
248
|
-
const type = context.checker.getTypeAtLocation(node.left);
|
|
249
|
-
if (context.utils.typeOrUnionHasFlag(type, TypeFlags.NumberLike)) context.report({
|
|
250
|
-
node,
|
|
251
|
-
message
|
|
252
|
-
});
|
|
253
|
-
} }
|
|
254
|
-
})),
|
|
255
|
-
typedEmptyArray: defineRule(() => ({
|
|
256
|
-
name: "arnaudBarre/typedEmptyArray",
|
|
257
|
-
visitor: { VariableStatement(context, node) {
|
|
258
|
-
for (const declaration of node.declarationList.declarations) if (declaration.initializer && declaration.initializer.kind === SyntaxKind.ArrayLiteralExpression && declaration.initializer.elements.length === 0 && !declaration.type) context.report({
|
|
259
|
-
node: declaration.name,
|
|
260
|
-
message: "Empty array needs explicit type annotation"
|
|
261
|
-
});
|
|
262
|
-
} }
|
|
263
|
-
}))
|
|
227
|
+
jsxNoNumberTruthiness
|
|
264
228
|
});
|
|
265
229
|
const allRules = [
|
|
266
230
|
...core.all(),
|
|
@@ -293,5 +257,4 @@ const allRules = [
|
|
|
293
257
|
}),
|
|
294
258
|
...arnaudBarre.all()
|
|
295
259
|
];
|
|
296
|
-
//#endregion
|
|
297
260
|
export { allRules, arnaudBarre };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arnaud-barre/tsl-config",
|
|
3
3
|
"description": "TS-React focused tsl configuration",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ArnaudBarre/tsl-config",
|