@arnaud-barre/tsl-config 0.1.4 → 0.2.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.
Files changed (4) hide show
  1. package/README.md +3 -101
  2. package/index.d.ts +6 -13
  3. package/index.js +19 -63
  4. package/package.json +3 -3
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # @arnaud-barre/tsl-config [![npm](https://img.shields.io/npm/v/@arnaud-barre/tsl-config)](https://www.npmjs.com/package/@arnaud-barre/tsl-config)
2
2
 
3
- Ongoing port of https://github.com/ArnaudBarre/eslint-config for https://github.com/ArnaudBarre/tsl
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
 
@@ -25,118 +26,19 @@ export default defineConfig({
25
26
  }
26
27
  ```
27
28
 
28
- ## TS config (5.9)
29
-
30
- ### Web project
29
+ ## TS config
31
30
 
32
31
  ```json
33
32
  {
34
- "include": ["**/*.ts", "**/*.tsx"],
35
33
  "compilerOptions": {
36
- "target": "ES2022",
37
- "jsx": "react-jsx",
38
- "module": "ESNext",
39
- "moduleDetection": "force",
40
- "lib": ["ES2022", "DOM", "DOM.Iterable"],
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
- "strict": true,
53
- "noUnusedLocals": true,
54
- "noUnusedParameters": true,
55
- "noFallthroughCasesInSwitch": true,
56
- "useUnknownInCatchVariables": true,
57
- "noUncheckedSideEffectImports": true,
58
- "noPropertyAccessFromIndexSignature": true,
59
-
60
34
  "plugins": [{ "name": "tsl/plugin" }]
61
35
  }
62
36
  }
63
37
  ```
64
38
 
65
- ### Node project (22)
66
-
67
- ```json
68
- {
69
- "include": ["**/*.ts"],
70
- "compilerOptions": {
71
- "target": "ES2022",
72
- "module": "ESNext",
73
- "moduleDetection": "force",
74
- "lib": ["es2024", "ESNext.Array", "ESNext.Collection", "ESNext.Iterator"],
75
- "types": ["node", "tsl/patches"],
76
- "skipLibCheck": true
77
-
78
- /* Same as web */
79
- }
80
- }
81
- ```
82
-
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)
108
-
109
- ### Web projects
110
-
111
- ```json
112
- // package.json
113
- "prettier": {
114
- "experimentalOperatorPosition": "start",
115
- "xmlWhitespaceSensitivity": "ignore",
116
- "plugins": [
117
- "@arnaud-barre/prettier-plugin-sort-imports",
118
- "@prettier/plugin-xml"
119
- ]
120
- }
121
- ```
122
-
123
- ### Node projects
124
-
125
- ```json
126
- // package.json
127
- "prettier": {
128
- "experimentalOperatorPosition": "start",
129
- "plugins": [
130
- "@arnaud-barre/prettier-plugin-sort-imports"
131
- ]
132
- }
133
- ```
134
-
135
39
  ### Custom rules
136
40
 
137
41
  - arrayCallbackReturn: Ensure array callbacks that require a return value (map, filter, find, some, ...) don't have undefined in their return type
138
- - arrayNoEmptyVariadic: Disallow empty variadic arguments to array methods (push, unshift, concat)
139
42
  - jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
140
43
  - jsxKey: Validate JSX has key prop when in array or iterator
141
44
  - jsxNoNumberTruthiness: Disallow `list.length && ...` in JSX
142
- - typedEmptyArray: Ensure empty arrays declarations have an explicit type annotation
package/index.d.ts CHANGED
@@ -1,14 +1,7 @@
1
- import * as tsl0 from "tsl";
2
-
3
- //#region src/index.d.ts
4
- declare const arnaudBarre: tsl0.RulesSet<{
5
- arrayCallbackReturn: (options?: "off" | undefined) => tsl0.Rule<unknown>;
6
- arrayNoEmptyVariadic: (options?: "off" | undefined) => tsl0.Rule<unknown>;
7
- jsxFragmentCheckChildren: (options?: "off" | undefined) => tsl0.Rule<unknown>;
8
- jsxKey: (options?: "off" | undefined) => tsl0.Rule<unknown>;
9
- jsxNoNumberTruthiness: (options?: "off" | undefined) => tsl0.Rule<unknown>;
10
- typedEmptyArray: (options?: "off" | undefined) => tsl0.Rule<unknown>;
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>;
11
6
  }>;
12
- declare const allRules: tsl0.Rule<unknown>[];
13
- //#endregion
14
- export { allRules, arnaudBarre };
7
+ export declare const allRules: import("tsl").Rule<unknown>[];
package/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { core, createRulesSet, defineRule } from "tsl";
2
2
  import { JsxEmit, SyntaxKind, TypeFlags } from "typescript";
3
-
4
- //#region src/rules/arrayCallbackReturn.ts
3
+ // 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 = {
7
6
  shouldBeBoolean: base + " Update the code to return a boolean value.",
@@ -20,11 +19,11 @@ const methodsRecords = {
20
19
  reduce: "addTypeAnnotation",
21
20
  reduceRight: "addTypeAnnotation"
22
21
  };
23
- const methods$1 = Object.keys(methodsRecords);
22
+ const methods = 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$1.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length >= 1) {
26
+ if (node.name.kind === SyntaxKind.Identifier && methods.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,27 +42,7 @@ const arrayCallbackReturn = defineRule(() => ({
43
42
  }
44
43
  } }
45
44
  }));
46
-
47
- //#endregion
48
- //#region src/rules/arrayNoEmptyVariadic.ts
49
- const methods = [
50
- "push",
51
- "unshift",
52
- "concat"
53
- ];
54
- const arrayNoEmptyVariadic = defineRule(() => ({
55
- name: "arnaudBarre/arrayNoEmptyVariadic",
56
- visitor: { PropertyAccessExpression(context, node) {
57
- if (node.name.kind === SyntaxKind.Identifier && methods.includes(node.name.text) && node.parent.kind === SyntaxKind.CallExpression && node.parent.arguments.length === 0) context.report({
58
- start: node.name.getStart(),
59
- end: node.parent.getEnd(),
60
- message: "This call does nothing"
61
- });
62
- } }
63
- }));
64
-
65
- //#endregion
66
- //#region src/rules/jsxFragmentCheckChildren.ts
45
+ // src/rules/jsxFragmentCheckChildren.ts
67
46
  const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
68
47
  const jsxFragmentCheckChildren = defineRule(() => ({
69
48
  name: "arnaudBarre/jsxFragmentCheckChildren",
@@ -94,19 +73,15 @@ function getReactNodeSymbol(program) {
94
73
  if (weekMap.has(program)) return weekMap.get(program);
95
74
  const reactSourceFile = program.getSourceFiles().find((sf) => sf.fileName.includes("@types/react/index.d.ts"));
96
75
  if (!reactSourceFile) return;
97
- const namespace = reactSourceFile.statements.find((s) => s.kind === SyntaxKind.ModuleDeclaration);
98
- if (!namespace) return;
99
- if (namespace.body?.kind !== SyntaxKind.ModuleBlock) return;
100
- const statement = namespace.body.statements.find((s) => s.kind === SyntaxKind.TypeAliasDeclaration && s.name.getText() === "ReactNode");
101
- if (!statement) return;
102
- const symbol = program.getTypeChecker().getSymbolAtLocation(statement.name);
76
+ const checker = program.getTypeChecker();
77
+ const modSymbol = checker.getSymbolAtLocation(reactSourceFile);
78
+ if (!modSymbol) return;
79
+ const symbol = checker.getExportsOfModule(modSymbol).find((e) => e.getName() === "ReactNode");
103
80
  if (!symbol) return;
104
81
  weekMap.set(program, symbol);
105
82
  return symbol;
106
83
  }
107
-
108
- //#endregion
109
- //#region src/rules/jsxKey.ts
84
+ // src/rules/jsxKey.ts
110
85
  const reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
111
86
  const messages = {
112
87
  missingKey: "Missing key",
@@ -165,7 +140,11 @@ const jsxKey = defineRule(() => {
165
140
  checkExpression(node.left, context);
166
141
  checkExpression(node.right, context);
167
142
  break;
168
- default: break;
143
+ case SyntaxKind.ParenthesizedExpression:
144
+ case SyntaxKind.SatisfiesExpression:
145
+ case SyntaxKind.TypeAssertionExpression:
146
+ case SyntaxKind.NonNullExpression:
147
+ case SyntaxKind.AsExpression: checkExpression(node.expression, context);
169
148
  }
170
149
  };
171
150
  const checkStatement = (node, context) => {
@@ -183,10 +162,7 @@ const jsxKey = defineRule(() => {
183
162
  checkStatement(node.thenStatement, context);
184
163
  if (node.elseStatement) checkStatement(node.elseStatement, context);
185
164
  break;
186
- case SyntaxKind.SwitchStatement:
187
- for (const caseClause of node.caseBlock.clauses) for (const statement of caseClause.statements) checkStatement(statement, context);
188
- break;
189
- default: break;
165
+ case SyntaxKind.SwitchStatement: for (const caseClause of node.caseBlock.clauses) for (const statement of caseClause.statements) checkStatement(statement, context);
190
166
  }
191
167
  };
192
168
  const checkIfReactComponentWithKeyInProps = (id, fnNode, context) => {
@@ -229,9 +205,7 @@ const jsxKey = defineRule(() => {
229
205
  }
230
206
  };
231
207
  });
232
-
233
- //#endregion
234
- //#region src/rules/jsxNoNumberTruthiness.ts
208
+ // src/rules/jsxNoNumberTruthiness.ts
235
209
  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";
236
210
  const jsxNoNumberTruthiness = defineRule(() => ({
237
211
  name: "arnaudBarre/jsxNoNumberTruthiness",
@@ -245,28 +219,12 @@ const jsxNoNumberTruthiness = defineRule(() => ({
245
219
  });
246
220
  } }
247
221
  }));
248
-
249
- //#endregion
250
- //#region src/rules/typedEmptyArray.ts
251
- const typedEmptyArray = defineRule(() => ({
252
- name: "arnaudBarre/typedEmptyArray",
253
- visitor: { VariableStatement(context, node) {
254
- for (const declaration of node.declarationList.declarations) if (declaration.initializer && declaration.initializer.kind === SyntaxKind.ArrayLiteralExpression && declaration.initializer.elements.length === 0 && !declaration.type) context.report({
255
- node: declaration.name,
256
- message: "Empty array needs explicit type annotation"
257
- });
258
- } }
259
- }));
260
-
261
- //#endregion
262
- //#region src/index.ts
222
+ // src/index.ts
263
223
  const arnaudBarre = createRulesSet({
264
224
  arrayCallbackReturn,
265
- arrayNoEmptyVariadic,
266
225
  jsxFragmentCheckChildren,
267
226
  jsxKey,
268
- jsxNoNumberTruthiness,
269
- typedEmptyArray
227
+ jsxNoNumberTruthiness
270
228
  });
271
229
  const allRules = [
272
230
  ...core.all(),
@@ -299,6 +257,4 @@ const allRules = [
299
257
  }),
300
258
  ...arnaudBarre.all()
301
259
  ];
302
-
303
- //#endregion
304
- export { allRules, arnaudBarre };
260
+ 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.4",
4
+ "version": "0.2.0",
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
  }