@arnaud-barre/tsl-config 0.1.1 → 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 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
@@ -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
- allowNullableBoolean: true
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.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",