@arnaud-barre/tsl-config 0.1.3 → 0.1.5

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
@@ -25,7 +25,7 @@ export default defineConfig({
25
25
  }
26
26
  ```
27
27
 
28
- ## TS config (5.9)
28
+ ## TS config (6.0)
29
29
 
30
30
  ### Web project
31
31
 
@@ -37,7 +37,7 @@ export default defineConfig({
37
37
  "jsx": "react-jsx",
38
38
  "module": "ESNext",
39
39
  "moduleDetection": "force",
40
- "lib": ["ES2022", "DOM", "DOM.Iterable"],
40
+ "lib": ["ES2022", "DOM"],
41
41
  "types": ["vite/client", "tsl/patches"],
42
42
  "skipLibCheck": true,
43
43
 
@@ -49,12 +49,10 @@ export default defineConfig({
49
49
  "noEmit": true,
50
50
 
51
51
  /* Linting */
52
- "strict": true,
53
52
  "noUnusedLocals": true,
54
53
  "noUnusedParameters": true,
55
54
  "noFallthroughCasesInSwitch": true,
56
55
  "useUnknownInCatchVariables": true,
57
- "noUncheckedSideEffectImports": true,
58
56
  "noPropertyAccessFromIndexSignature": true,
59
57
 
60
58
  "plugins": [{ "name": "tsl/plugin" }]
@@ -135,6 +133,8 @@ export default defineConfig({
135
133
  ### Custom rules
136
134
 
137
135
  - 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)
138
137
  - jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
139
138
  - jsxKey: Validate JSX has key prop when in array or iterator
140
139
  - jsxNoNumberTruthiness: Disallow `list.length && ...` in JSX
140
+ - typedEmptyArray: Ensure empty arrays declarations have an explicit type annotation
package/index.d.ts CHANGED
@@ -1,12 +1,14 @@
1
- import * as tsl0 from "tsl";
1
+ import * as _$tsl from "tsl";
2
2
 
3
3
  //#region src/index.d.ts
4
- declare const arnaudBarre: tsl0.RulesSet<{
5
- arrayCallbackReturn: (options?: "off" | undefined) => tsl0.Rule<unknown>;
6
- jsxFragmentCheckChildren: (options?: "off" | undefined) => tsl0.Rule<unknown>;
7
- jsxKey: (options?: "off" | undefined) => tsl0.Rule<unknown>;
8
- jsxNoNumberTruthiness: (options?: "off" | undefined) => tsl0.Rule<unknown>;
4
+ declare const arnaudBarre: _$tsl.RulesSet<{
5
+ arrayCallbackReturn: (options?: "off" | undefined) => _$tsl.Rule<unknown>;
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>;
9
11
  }>;
10
- declare const allRules: tsl0.Rule<unknown>[];
12
+ declare const allRules: _$tsl.Rule<unknown>[];
11
13
  //#endregion
12
14
  export { allRules, arnaudBarre };
package/index.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { core, createRulesSet, defineRule } from "tsl";
2
2
  import { JsxEmit, SyntaxKind, TypeFlags } from "typescript";
3
-
4
3
  //#region src/rules/arrayCallbackReturn.ts
5
4
  const base = "Found undefined in the return type of this callback. It could come from a missing return statement.";
6
5
  const messages$1 = {
@@ -20,11 +19,11 @@ const methodsRecords = {
20
19
  reduce: "addTypeAnnotation",
21
20
  reduceRight: "addTypeAnnotation"
22
21
  };
23
- const methods = Object.keys(methodsRecords);
22
+ const methods$1 = Object.keys(methodsRecords);
24
23
  const arrayCallbackReturn = defineRule(() => ({
25
24
  name: "arnaudBarre/arrayCallbackReturn",
26
25
  visitor: { PropertyAccessExpression(context, node) {
27
- if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length >= 1) {
26
+ if (node.name.kind === SyntaxKind.Identifier && methods$1.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length >= 1) {
28
27
  const callback = node.parent.arguments[0];
29
28
  if (callback.kind === SyntaxKind.ArrowFunction && callback.body.kind !== SyntaxKind.Block) return;
30
29
  const signatures = context.checker.getTypeAtLocation(callback).getCallSignatures();
@@ -43,7 +42,23 @@ const arrayCallbackReturn = defineRule(() => ({
43
42
  }
44
43
  } }
45
44
  }));
46
-
45
+ //#endregion
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
+ }));
47
62
  //#endregion
48
63
  //#region src/rules/jsxFragmentCheckChildren.ts
49
64
  const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
@@ -76,17 +91,14 @@ function getReactNodeSymbol(program) {
76
91
  if (weekMap.has(program)) return weekMap.get(program);
77
92
  const reactSourceFile = program.getSourceFiles().find((sf) => sf.fileName.includes("@types/react/index.d.ts"));
78
93
  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);
94
+ const checker = program.getTypeChecker();
95
+ const modSymbol = checker.getSymbolAtLocation(reactSourceFile);
96
+ if (!modSymbol) return;
97
+ const symbol = checker.getExportsOfModule(modSymbol).find((e) => e.getName() === "ReactNode");
85
98
  if (!symbol) return;
86
99
  weekMap.set(program, symbol);
87
100
  return symbol;
88
101
  }
89
-
90
102
  //#endregion
91
103
  //#region src/rules/jsxKey.ts
92
104
  const reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
@@ -147,6 +159,13 @@ const jsxKey = defineRule(() => {
147
159
  checkExpression(node.left, context);
148
160
  checkExpression(node.right, context);
149
161
  break;
162
+ case SyntaxKind.ParenthesizedExpression:
163
+ case SyntaxKind.SatisfiesExpression:
164
+ case SyntaxKind.TypeAssertionExpression:
165
+ case SyntaxKind.NonNullExpression:
166
+ case SyntaxKind.AsExpression:
167
+ checkExpression(node.expression, context);
168
+ break;
150
169
  default: break;
151
170
  }
152
171
  };
@@ -211,30 +230,37 @@ const jsxKey = defineRule(() => {
211
230
  }
212
231
  };
213
232
  });
214
-
215
233
  //#endregion
216
234
  //#region src/rules/jsxNoNumberTruthiness.ts
217
235
  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";
218
- const jsxNoNumberTruthiness = defineRule(() => ({
219
- name: "arnaudBarre/jsxNoNumberTruthiness",
220
- visitor: { BinaryExpression(context, node) {
221
- if (node.operatorToken.kind !== SyntaxKind.AmpersandAmpersandToken) return;
222
- if (node.parent.kind !== SyntaxKind.JsxExpression) return;
223
- const type = context.checker.getTypeAtLocation(node.left);
224
- if (context.utils.typeOrUnionHasFlag(type, TypeFlags.NumberLike)) context.report({
225
- node,
226
- message
227
- });
228
- } }
229
- }));
230
-
231
236
  //#endregion
232
237
  //#region src/index.ts
233
238
  const arnaudBarre = createRulesSet({
234
239
  arrayCallbackReturn,
240
+ arrayNoEmptyVariadic,
235
241
  jsxFragmentCheckChildren,
236
242
  jsxKey,
237
- jsxNoNumberTruthiness
243
+ jsxNoNumberTruthiness: defineRule(() => ({
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
+ }))
238
264
  });
239
265
  const allRules = [
240
266
  ...core.all(),
@@ -267,6 +293,5 @@ const allRules = [
267
293
  }),
268
294
  ...arnaudBarre.all()
269
295
  ];
270
-
271
296
  //#endregion
272
- export { allRules, arnaudBarre };
297
+ 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.1.3",
4
+ "version": "0.1.5",
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.22",
14
- "typescript": "~5.9"
13
+ "tsl": "^1.0.30",
14
+ "typescript": "~6.0"
15
15
  }
16
16
  }