@arnaud-barre/tsl-config 0.1.1 → 0.1.3
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 +8 -5
- package/index.d.ts +1 -0
- package/index.js +56 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ export default defineConfig({
|
|
|
38
38
|
"module": "ESNext",
|
|
39
39
|
"moduleDetection": "force",
|
|
40
40
|
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
41
|
-
"types": ["vite/client"],
|
|
41
|
+
"types": ["vite/client", "tsl/patches"],
|
|
42
42
|
"skipLibCheck": true,
|
|
43
43
|
|
|
44
44
|
/* Bundler mode */
|
|
@@ -55,7 +55,9 @@ export default defineConfig({
|
|
|
55
55
|
"noFallthroughCasesInSwitch": true,
|
|
56
56
|
"useUnknownInCatchVariables": true,
|
|
57
57
|
"noUncheckedSideEffectImports": true,
|
|
58
|
-
"noPropertyAccessFromIndexSignature": true
|
|
58
|
+
"noPropertyAccessFromIndexSignature": true,
|
|
59
|
+
|
|
60
|
+
"plugins": [{ "name": "tsl/plugin" }]
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
63
|
```
|
|
@@ -70,7 +72,7 @@ export default defineConfig({
|
|
|
70
72
|
"module": "ESNext",
|
|
71
73
|
"moduleDetection": "force",
|
|
72
74
|
"lib": ["es2024", "ESNext.Array", "ESNext.Collection", "ESNext.Iterator"],
|
|
73
|
-
"types": ["node"],
|
|
75
|
+
"types": ["node", "tsl/patches"],
|
|
74
76
|
"skipLibCheck": true
|
|
75
77
|
|
|
76
78
|
/* Same as web */
|
|
@@ -94,7 +96,7 @@ export default defineConfig({
|
|
|
94
96
|
"ESNext.Iterator",
|
|
95
97
|
"ESNext.Promise"
|
|
96
98
|
],
|
|
97
|
-
"types": ["node"],
|
|
99
|
+
"types": ["node", "tsl/patches"],
|
|
98
100
|
"skipLibCheck": true
|
|
99
101
|
|
|
100
102
|
/* Same as web */
|
|
@@ -132,6 +134,7 @@ export default defineConfig({
|
|
|
132
134
|
|
|
133
135
|
### Custom rules
|
|
134
136
|
|
|
135
|
-
- jsxKey: Validate JSX has key prop when in array or iterator
|
|
136
137
|
- arrayCallbackReturn: Ensure array callbacks that require a return value (map, filter, find, some, ...) don't have undefined in their return type
|
|
138
|
+
- jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
|
|
139
|
+
- jsxKey: Validate JSX has key prop when in array or iterator
|
|
137
140
|
- jsxNoNumberTruthiness: Disallow `list.length && ...` in JSX
|
package/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import * as tsl0 from "tsl";
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
4
|
declare const arnaudBarre: tsl0.RulesSet<{
|
|
5
5
|
arrayCallbackReturn: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
6
|
+
jsxFragmentCheckChildren: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
6
7
|
jsxKey: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
7
8
|
jsxNoNumberTruthiness: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
8
9
|
}>;
|
package/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { core, createRulesSet, defineRule } from "tsl";
|
|
2
|
-
import "
|
|
3
|
-
import { SyntaxKind, TypeFlags } from "typescript";
|
|
2
|
+
import { JsxEmit, SyntaxKind, TypeFlags } from "typescript";
|
|
4
3
|
|
|
5
4
|
//#region src/rules/arrayCallbackReturn.ts
|
|
6
5
|
const base = "Found undefined in the return type of this callback. It could come from a missing return statement.";
|
|
@@ -28,12 +27,11 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
28
27
|
if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length >= 1) {
|
|
29
28
|
const callback = node.parent.arguments[0];
|
|
30
29
|
if (callback.kind === SyntaxKind.ArrowFunction && callback.body.kind !== SyntaxKind.Block) return;
|
|
31
|
-
const
|
|
32
|
-
const signatures = functionType.getCallSignatures();
|
|
30
|
+
const signatures = context.checker.getTypeAtLocation(callback).getCallSignatures();
|
|
33
31
|
if (signatures.length === 0) return;
|
|
34
32
|
for (const signature of signatures) {
|
|
35
33
|
const type = signature.getReturnType();
|
|
36
|
-
if (type.flags & TypeFlags.Undefined || type.isUnion() && type.types.some((t) => t.flags & TypeFlags.Undefined)) {
|
|
34
|
+
if ((type.flags & TypeFlags.Undefined) !== 0 || type.isUnion() && type.types.some((t) => (t.flags & TypeFlags.Undefined) !== 0)) {
|
|
37
35
|
const messageId = methodsRecords[node.name.text];
|
|
38
36
|
if (messageId === "addTypeAnnotation" && node.parent.typeArguments) return;
|
|
39
37
|
context.report({
|
|
@@ -46,6 +44,49 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
46
44
|
} }
|
|
47
45
|
}));
|
|
48
46
|
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/rules/jsxFragmentCheckChildren.ts
|
|
49
|
+
const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
|
|
50
|
+
const jsxFragmentCheckChildren = defineRule(() => ({
|
|
51
|
+
name: "arnaudBarre/jsxFragmentCheckChildren",
|
|
52
|
+
visitor: { JsxFragment(context, node) {
|
|
53
|
+
if (context.compilerOptions.jsx !== JsxEmit.ReactJSX) return;
|
|
54
|
+
const reactNodeSymbol = getReactNodeSymbol(context.program);
|
|
55
|
+
const reactNodeType = reactNodeSymbol ? context.checker.getDeclaredTypeOfSymbol(reactNodeSymbol) : void 0;
|
|
56
|
+
if (!reactNodeType) {
|
|
57
|
+
context.report({
|
|
58
|
+
node,
|
|
59
|
+
message: "Can't resolve ReactNode type. Is @types/react installed?"
|
|
60
|
+
});
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
for (const child of node.children) {
|
|
64
|
+
if (child.kind !== SyntaxKind.JsxExpression) continue;
|
|
65
|
+
if (!child.expression) continue;
|
|
66
|
+
const type = context.checker.getTypeAtLocation(child.expression);
|
|
67
|
+
if (!context.checker.isTypeAssignableTo(type, reactNodeType)) context.report({
|
|
68
|
+
node: child,
|
|
69
|
+
message: message$1
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
} }
|
|
73
|
+
}));
|
|
74
|
+
const weekMap = /* @__PURE__ */ new WeakMap();
|
|
75
|
+
function getReactNodeSymbol(program) {
|
|
76
|
+
if (weekMap.has(program)) return weekMap.get(program);
|
|
77
|
+
const reactSourceFile = program.getSourceFiles().find((sf) => sf.fileName.includes("@types/react/index.d.ts"));
|
|
78
|
+
if (!reactSourceFile) return;
|
|
79
|
+
const namespace = reactSourceFile.statements.find((s) => s.kind === SyntaxKind.ModuleDeclaration);
|
|
80
|
+
if (!namespace) return;
|
|
81
|
+
if (namespace.body?.kind !== SyntaxKind.ModuleBlock) return;
|
|
82
|
+
const statement = namespace.body.statements.find((s) => s.kind === SyntaxKind.TypeAliasDeclaration && s.name.getText() === "ReactNode");
|
|
83
|
+
if (!statement) return;
|
|
84
|
+
const symbol = program.getTypeChecker().getSymbolAtLocation(statement.name);
|
|
85
|
+
if (!symbol) return;
|
|
86
|
+
weekMap.set(program, symbol);
|
|
87
|
+
return symbol;
|
|
88
|
+
}
|
|
89
|
+
|
|
49
90
|
//#endregion
|
|
50
91
|
//#region src/rules/jsxKey.ts
|
|
51
92
|
const reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
|
|
@@ -71,8 +112,7 @@ const jsxKey = defineRule(() => {
|
|
|
71
112
|
node: attr
|
|
72
113
|
};
|
|
73
114
|
if (attr.kind === SyntaxKind.JsxSpreadAttribute) {
|
|
74
|
-
|
|
75
|
-
if (type.getProperty("key")) context.report({
|
|
115
|
+
if (context.checker.getTypeAtLocation(attr.expression).getProperty("key")) context.report({
|
|
76
116
|
message: messages.keySpread,
|
|
77
117
|
node: attr
|
|
78
118
|
});
|
|
@@ -134,8 +174,7 @@ const jsxKey = defineRule(() => {
|
|
|
134
174
|
const checkIfReactComponentWithKeyInProps = (id, fnNode, context) => {
|
|
135
175
|
if (!reactComponentNameRE.test(id.text)) return;
|
|
136
176
|
if (fnNode.parameters.length === 0) return;
|
|
137
|
-
|
|
138
|
-
if (type.getProperty("key")) context.report({
|
|
177
|
+
if (context.checker.getTypeAtLocation(fnNode.parameters[0]).getProperty("key")) context.report({
|
|
139
178
|
message: messages.keyInProps,
|
|
140
179
|
node: id
|
|
141
180
|
});
|
|
@@ -193,6 +232,7 @@ const jsxNoNumberTruthiness = defineRule(() => ({
|
|
|
193
232
|
//#region src/index.ts
|
|
194
233
|
const arnaudBarre = createRulesSet({
|
|
195
234
|
arrayCallbackReturn,
|
|
235
|
+
jsxFragmentCheckChildren,
|
|
196
236
|
jsxKey,
|
|
197
237
|
jsxNoNumberTruthiness
|
|
198
238
|
});
|
|
@@ -216,8 +256,14 @@ const allRules = [
|
|
|
216
256
|
considerDefaultExhaustiveForUnions: true
|
|
217
257
|
}),
|
|
218
258
|
core.strictBooleanExpressions({
|
|
259
|
+
allowAny: false,
|
|
260
|
+
allowNullableBoolean: true,
|
|
261
|
+
allowNullableEnum: true,
|
|
262
|
+
allowNullableNumber: true,
|
|
263
|
+
allowNullableObject: true,
|
|
219
264
|
allowNullableString: true,
|
|
220
|
-
|
|
265
|
+
allowNumber: false,
|
|
266
|
+
allowString: false
|
|
221
267
|
}),
|
|
222
268
|
...arnaudBarre.all()
|
|
223
269
|
];
|
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.1.
|
|
4
|
+
"version": "0.1.3",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ArnaudBarre/tsl-config",
|