@arnaud-barre/tsl-config 0.1.5 → 0.3.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 +12 -103
- package/index.d.ts +7 -14
- package/index.js +43 -97
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,20 +1,25 @@
|
|
|
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
|
|
|
7
8
|
```sh
|
|
8
|
-
bun add -D tsl @arnaud-barre/tsl-config
|
|
9
|
+
bun add -D tsl @arnaud-barre/tsl-config typescript@6
|
|
9
10
|
```
|
|
10
11
|
|
|
11
12
|
```js
|
|
12
13
|
// tsl.config.ts
|
|
13
|
-
import {
|
|
14
|
-
import { defineConfig } from "tsl";
|
|
14
|
+
import { arnaudBarre } from "@arnaud-barre/tsl-config";
|
|
15
|
+
import { core, defineConfig } from "tsl";
|
|
15
16
|
|
|
16
17
|
export default defineConfig({
|
|
17
|
-
rules: [
|
|
18
|
+
rules: [
|
|
19
|
+
...arnaudBarre.all(),
|
|
20
|
+
// oxlint has issues: https://github.com/oxc-project/oxc/issues/30588
|
|
21
|
+
core.preferOptionalChain(),
|
|
22
|
+
],
|
|
18
23
|
});
|
|
19
24
|
```
|
|
20
25
|
|
|
@@ -25,116 +30,20 @@ export default defineConfig({
|
|
|
25
30
|
}
|
|
26
31
|
```
|
|
27
32
|
|
|
28
|
-
## TS config
|
|
29
|
-
|
|
30
|
-
### Web project
|
|
33
|
+
## TS config
|
|
31
34
|
|
|
32
35
|
```json
|
|
33
36
|
{
|
|
34
|
-
"include": ["**/*.ts", "**/*.tsx"],
|
|
35
37
|
"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
38
|
"plugins": [{ "name": "tsl/plugin" }]
|
|
59
39
|
}
|
|
60
40
|
}
|
|
61
41
|
```
|
|
62
42
|
|
|
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
43
|
### Custom rules
|
|
134
44
|
|
|
135
45
|
- 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
46
|
- jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
|
|
138
47
|
- jsxKey: Validate JSX has key prop when in array or iterator
|
|
139
48
|
- jsxNoNumberTruthiness: Disallow `list.length && ...` in JSX
|
|
140
|
-
-
|
|
49
|
+
- noComputedPropertiesWithUnionType: Disallow `{ [key]: value }` when `key` is a union, which TS widens to an index signature that's assignable to types the object doesn't satisfy. Use `data[key] = value` instead
|
package/index.d.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
jsxKey: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
9
|
-
jsxNoNumberTruthiness: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
10
|
-
typedEmptyArray: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
|
|
11
|
-
}>;
|
|
12
|
-
declare const allRules: _$tsl.Rule<unknown>[];
|
|
13
|
-
//#endregion
|
|
14
|
-
export { allRules, arnaudBarre };
|
|
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>;
|
|
6
|
+
noComputedPropertiesWithUnionType: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
7
|
+
}>;
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { 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,26 +42,8 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
42
42
|
}
|
|
43
43
|
} }
|
|
44
44
|
}));
|
|
45
|
-
|
|
46
|
-
|
|
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
|
|
64
|
-
const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
|
|
45
|
+
// src/rules/jsxFragmentCheckChildren.ts
|
|
46
|
+
const message$2 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
|
|
65
47
|
const jsxFragmentCheckChildren = defineRule(() => ({
|
|
66
48
|
name: "arnaudBarre/jsxFragmentCheckChildren",
|
|
67
49
|
visitor: { JsxFragment(context, node) {
|
|
@@ -81,13 +63,13 @@ const jsxFragmentCheckChildren = defineRule(() => ({
|
|
|
81
63
|
const type = context.checker.getTypeAtLocation(child.expression);
|
|
82
64
|
if (!context.checker.isTypeAssignableTo(type, reactNodeType)) context.report({
|
|
83
65
|
node: child,
|
|
84
|
-
message: message$
|
|
66
|
+
message: message$2
|
|
85
67
|
});
|
|
86
68
|
}
|
|
87
69
|
} }
|
|
88
70
|
}));
|
|
89
71
|
const weekMap = /* @__PURE__ */ new WeakMap();
|
|
90
|
-
|
|
72
|
+
const getReactNodeSymbol = (program) => {
|
|
91
73
|
if (weekMap.has(program)) return weekMap.get(program);
|
|
92
74
|
const reactSourceFile = program.getSourceFiles().find((sf) => sf.fileName.includes("@types/react/index.d.ts"));
|
|
93
75
|
if (!reactSourceFile) return;
|
|
@@ -98,9 +80,8 @@ function getReactNodeSymbol(program) {
|
|
|
98
80
|
if (!symbol) return;
|
|
99
81
|
weekMap.set(program, symbol);
|
|
100
82
|
return symbol;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
//#region src/rules/jsxKey.ts
|
|
83
|
+
};
|
|
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,68 +205,39 @@ const jsxKey = defineRule(() => {
|
|
|
230
205
|
}
|
|
231
206
|
};
|
|
232
207
|
});
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
208
|
+
// src/rules/jsxNoNumberTruthiness.ts
|
|
209
|
+
const message$1 = "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";
|
|
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: message$1
|
|
219
|
+
});
|
|
220
|
+
} }
|
|
221
|
+
}));
|
|
222
|
+
// src/rules/noComputedPropertiesWithUnionType.ts
|
|
223
|
+
const message = "Computed properties with union types are unsafe. Do `data[key] = value` instead";
|
|
224
|
+
const noComputedPropertiesWithUnionType = defineRule(() => ({
|
|
225
|
+
name: "arnaudBarre/noComputedPropertiesWithUnionType",
|
|
226
|
+
visitor: { ComputedPropertyName(context, node) {
|
|
227
|
+
if (node.parent.parent.kind === SyntaxKind.TypeLiteral) return;
|
|
228
|
+
const type = context.checker.getTypeAtLocation(node.expression);
|
|
229
|
+
if (context.utils.typeHasFlag(type, TypeFlags.Union)) context.report({
|
|
230
|
+
node,
|
|
231
|
+
message
|
|
232
|
+
});
|
|
233
|
+
} }
|
|
234
|
+
}));
|
|
235
|
+
// src/index.ts
|
|
238
236
|
const arnaudBarre = createRulesSet({
|
|
239
237
|
arrayCallbackReturn,
|
|
240
|
-
arrayNoEmptyVariadic,
|
|
241
238
|
jsxFragmentCheckChildren,
|
|
242
239
|
jsxKey,
|
|
243
|
-
jsxNoNumberTruthiness
|
|
244
|
-
|
|
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
|
-
}))
|
|
240
|
+
jsxNoNumberTruthiness,
|
|
241
|
+
noComputedPropertiesWithUnionType
|
|
264
242
|
});
|
|
265
|
-
|
|
266
|
-
...core.all(),
|
|
267
|
-
core.preferNullishCoalescing({ ignoreIfStatements: true }),
|
|
268
|
-
core.noMisusedPromises({
|
|
269
|
-
checksConditionals: false,
|
|
270
|
-
checksVoidReturn: {
|
|
271
|
-
arguments: false,
|
|
272
|
-
attributes: false
|
|
273
|
-
}
|
|
274
|
-
}),
|
|
275
|
-
core.noConfusingVoidExpression({ ignoreArrowShorthand: true }),
|
|
276
|
-
core.noUnnecessaryCondition({
|
|
277
|
-
allowConstantLoopConditions: "only-allowed-literals",
|
|
278
|
-
checkTypePredicates: true
|
|
279
|
-
}),
|
|
280
|
-
core.switchExhaustivenessCheck({
|
|
281
|
-
allowDefaultCaseForExhaustiveSwitch: false,
|
|
282
|
-
considerDefaultExhaustiveForUnions: true
|
|
283
|
-
}),
|
|
284
|
-
core.strictBooleanExpressions({
|
|
285
|
-
allowAny: false,
|
|
286
|
-
allowNullableBoolean: true,
|
|
287
|
-
allowNullableEnum: true,
|
|
288
|
-
allowNullableNumber: true,
|
|
289
|
-
allowNullableObject: true,
|
|
290
|
-
allowNullableString: true,
|
|
291
|
-
allowNumber: false,
|
|
292
|
-
allowString: false
|
|
293
|
-
}),
|
|
294
|
-
...arnaudBarre.all()
|
|
295
|
-
];
|
|
296
|
-
//#endregion
|
|
297
|
-
export { allRules, arnaudBarre };
|
|
243
|
+
export { 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.3.0",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ArnaudBarre/tsl-config",
|