@arnaud-barre/tsl-config 0.1.0 → 0.1.2
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 +30 -5
- package/index.d.ts +1 -0
- package/index.js +52 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ export default defineConfig({
|
|
|
25
25
|
}
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
## TS config (5.
|
|
28
|
+
## TS config (5.9)
|
|
29
29
|
|
|
30
30
|
### Web project
|
|
31
31
|
|
|
@@ -62,7 +62,7 @@ export default defineConfig({
|
|
|
62
62
|
}
|
|
63
63
|
```
|
|
64
64
|
|
|
65
|
-
### Node project (
|
|
65
|
+
### Node project (22)
|
|
66
66
|
|
|
67
67
|
```json
|
|
68
68
|
{
|
|
@@ -71,7 +71,7 @@ export default defineConfig({
|
|
|
71
71
|
"target": "ES2022",
|
|
72
72
|
"module": "ESNext",
|
|
73
73
|
"moduleDetection": "force",
|
|
74
|
-
"lib": ["
|
|
74
|
+
"lib": ["es2024", "ESNext.Array", "ESNext.Collection", "ESNext.Iterator"],
|
|
75
75
|
"types": ["node", "tsl/patches"],
|
|
76
76
|
"skipLibCheck": true
|
|
77
77
|
|
|
@@ -80,7 +80,31 @@ export default defineConfig({
|
|
|
80
80
|
}
|
|
81
81
|
```
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
### Node project (24)
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"include": ["**/*.ts"],
|
|
88
|
+
"compilerOptions": {
|
|
89
|
+
"target": "ES2024",
|
|
90
|
+
"module": "ESNext",
|
|
91
|
+
"moduleDetection": "force",
|
|
92
|
+
"lib": [
|
|
93
|
+
"es2024",
|
|
94
|
+
"ESNext.Array",
|
|
95
|
+
"ESNext.Collection",
|
|
96
|
+
"ESNext.Iterator",
|
|
97
|
+
"ESNext.Promise"
|
|
98
|
+
],
|
|
99
|
+
"types": ["node", "tsl/patches"],
|
|
100
|
+
"skipLibCheck": true
|
|
101
|
+
|
|
102
|
+
/* Same as web */
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Prettier config (3.6.2)
|
|
84
108
|
|
|
85
109
|
### Web projects
|
|
86
110
|
|
|
@@ -110,6 +134,7 @@ export default defineConfig({
|
|
|
110
134
|
|
|
111
135
|
### Custom rules
|
|
112
136
|
|
|
113
|
-
- jsxKey: Validate JSX has key prop when in array or iterator
|
|
114
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
|
|
115
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
|
@@ -33,7 +33,7 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
33
33
|
if (signatures.length === 0) return;
|
|
34
34
|
for (const signature of signatures) {
|
|
35
35
|
const type = signature.getReturnType();
|
|
36
|
-
if (type.flags & TypeFlags.Undefined || type.isUnion() && type.types.some((t) => t.flags & TypeFlags.Undefined)) {
|
|
36
|
+
if ((type.flags & TypeFlags.Undefined) !== 0 || type.isUnion() && type.types.some((t) => (t.flags & TypeFlags.Undefined) !== 0)) {
|
|
37
37
|
const messageId = methodsRecords[node.name.text];
|
|
38
38
|
if (messageId === "addTypeAnnotation" && node.parent.typeArguments) return;
|
|
39
39
|
context.report({
|
|
@@ -46,6 +46,49 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
46
46
|
} }
|
|
47
47
|
}));
|
|
48
48
|
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/rules/jsxFragmentCheckChildren.ts
|
|
51
|
+
const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
|
|
52
|
+
const jsxFragmentCheckChildren = defineRule(() => ({
|
|
53
|
+
name: "arnaudBarre/jsxFragmentCheckChildren",
|
|
54
|
+
visitor: { JsxFragment(context, node) {
|
|
55
|
+
const reactNodeType = getReactNodeType(context.program);
|
|
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 getReactNodeType(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 checker = program.getTypeChecker();
|
|
85
|
+
const symbol = checker.getSymbolAtLocation(statement.name);
|
|
86
|
+
if (!symbol) return;
|
|
87
|
+
const type = checker.getDeclaredTypeOfSymbol(symbol);
|
|
88
|
+
weekMap.set(program, type);
|
|
89
|
+
return type;
|
|
90
|
+
}
|
|
91
|
+
|
|
49
92
|
//#endregion
|
|
50
93
|
//#region src/rules/jsxKey.ts
|
|
51
94
|
const reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
|
|
@@ -193,6 +236,7 @@ const jsxNoNumberTruthiness = defineRule(() => ({
|
|
|
193
236
|
//#region src/index.ts
|
|
194
237
|
const arnaudBarre = createRulesSet({
|
|
195
238
|
arrayCallbackReturn,
|
|
239
|
+
jsxFragmentCheckChildren,
|
|
196
240
|
jsxKey,
|
|
197
241
|
jsxNoNumberTruthiness
|
|
198
242
|
});
|
|
@@ -216,8 +260,14 @@ const allRules = [
|
|
|
216
260
|
considerDefaultExhaustiveForUnions: true
|
|
217
261
|
}),
|
|
218
262
|
core.strictBooleanExpressions({
|
|
263
|
+
allowAny: false,
|
|
264
|
+
allowNullableBoolean: true,
|
|
265
|
+
allowNullableEnum: true,
|
|
266
|
+
allowNullableNumber: true,
|
|
267
|
+
allowNullableObject: true,
|
|
219
268
|
allowNullableString: true,
|
|
220
|
-
|
|
269
|
+
allowNumber: false,
|
|
270
|
+
allowString: false
|
|
221
271
|
}),
|
|
222
272
|
...arnaudBarre.all()
|
|
223
273
|
];
|
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.2",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ArnaudBarre/tsl-config",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
".": "./index.js"
|
|
11
11
|
},
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"tsl": "^1.0.
|
|
14
|
-
"typescript": "~5.
|
|
13
|
+
"tsl": "^1.0.22",
|
|
14
|
+
"typescript": "~5.9"
|
|
15
15
|
}
|
|
16
16
|
}
|