@arnaud-barre/tsl-config 0.2.0 → 0.3.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.
- package/README.md +9 -4
- package/index.d.ts +2 -2
- package/index.js +22 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,16 +6,20 @@ To be used with https://npmx.dev/package/@arnaud-barre/oxlint-config
|
|
|
6
6
|
## Install
|
|
7
7
|
|
|
8
8
|
```sh
|
|
9
|
-
bun add -D tsl @arnaud-barre/tsl-config
|
|
9
|
+
bun add -D tsl @arnaud-barre/tsl-config typescript@6
|
|
10
10
|
```
|
|
11
11
|
|
|
12
12
|
```js
|
|
13
13
|
// tsl.config.ts
|
|
14
|
-
import {
|
|
15
|
-
import { defineConfig } from "tsl";
|
|
14
|
+
import { arnaudBarre } from "@arnaud-barre/tsl-config";
|
|
15
|
+
import { core, defineConfig } from "tsl";
|
|
16
16
|
|
|
17
17
|
export default defineConfig({
|
|
18
|
-
rules: [
|
|
18
|
+
rules: [
|
|
19
|
+
...arnaudBarre.all(),
|
|
20
|
+
// oxlint has issues: https://github.com/oxc-project/oxc/issues/30588
|
|
21
|
+
core.preferOptionalChain(),
|
|
22
|
+
],
|
|
19
23
|
});
|
|
20
24
|
```
|
|
21
25
|
|
|
@@ -42,3 +46,4 @@ export default defineConfig({
|
|
|
42
46
|
- jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
|
|
43
47
|
- jsxKey: Validate JSX has key prop when in array or iterator
|
|
44
48
|
- jsxNoNumberTruthiness: Disallow `list.length && ...` in JSX
|
|
49
|
+
- noComputedPropertiesWithUnionType: Disallow `{ [key]: value }` when `key` is a union, which TS widens to an index signature that's assignable to types the object doesn't satisfy. Use `data[key] = value` instead
|
package/index.d.ts
CHANGED
|
@@ -3,5 +3,5 @@ export declare const arnaudBarre: import("tsl").RulesSet<{
|
|
|
3
3
|
jsxFragmentCheckChildren: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
4
4
|
jsxKey: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
5
5
|
jsxNoNumberTruthiness: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
noComputedPropertiesWithUnionType: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
|
|
7
|
+
}>;
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRulesSet, defineRule } from "tsl";
|
|
2
2
|
import { JsxEmit, SyntaxKind, TypeFlags } from "typescript";
|
|
3
3
|
// src/rules/arrayCallbackReturn.ts
|
|
4
4
|
const base = "Found undefined in the return type of this callback. It could come from a missing return statement.";
|
|
@@ -43,7 +43,7 @@ const arrayCallbackReturn = defineRule(() => ({
|
|
|
43
43
|
} }
|
|
44
44
|
}));
|
|
45
45
|
// src/rules/jsxFragmentCheckChildren.ts
|
|
46
|
-
const message$
|
|
46
|
+
const message$2 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
|
|
47
47
|
const jsxFragmentCheckChildren = defineRule(() => ({
|
|
48
48
|
name: "arnaudBarre/jsxFragmentCheckChildren",
|
|
49
49
|
visitor: { JsxFragment(context, node) {
|
|
@@ -63,13 +63,13 @@ const jsxFragmentCheckChildren = defineRule(() => ({
|
|
|
63
63
|
const type = context.checker.getTypeAtLocation(child.expression);
|
|
64
64
|
if (!context.checker.isTypeAssignableTo(type, reactNodeType)) context.report({
|
|
65
65
|
node: child,
|
|
66
|
-
message: message$
|
|
66
|
+
message: message$2
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
69
|
} }
|
|
70
70
|
}));
|
|
71
71
|
const weekMap = /* @__PURE__ */ new WeakMap();
|
|
72
|
-
|
|
72
|
+
const getReactNodeSymbol = (program) => {
|
|
73
73
|
if (weekMap.has(program)) return weekMap.get(program);
|
|
74
74
|
const reactSourceFile = program.getSourceFiles().find((sf) => sf.fileName.includes("@types/react/index.d.ts"));
|
|
75
75
|
if (!reactSourceFile) return;
|
|
@@ -80,7 +80,7 @@ function getReactNodeSymbol(program) {
|
|
|
80
80
|
if (!symbol) return;
|
|
81
81
|
weekMap.set(program, symbol);
|
|
82
82
|
return symbol;
|
|
83
|
-
}
|
|
83
|
+
};
|
|
84
84
|
// src/rules/jsxKey.ts
|
|
85
85
|
const reactComponentNameRE = /^[A-Z][a-zA-Z0-9]*$/u;
|
|
86
86
|
const messages = {
|
|
@@ -206,7 +206,7 @@ const jsxKey = defineRule(() => {
|
|
|
206
206
|
};
|
|
207
207
|
});
|
|
208
208
|
// src/rules/jsxNoNumberTruthiness.ts
|
|
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";
|
|
209
|
+
const message$1 = "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";
|
|
210
210
|
const jsxNoNumberTruthiness = defineRule(() => ({
|
|
211
211
|
name: "arnaudBarre/jsxNoNumberTruthiness",
|
|
212
212
|
visitor: { BinaryExpression(context, node) {
|
|
@@ -214,6 +214,19 @@ const jsxNoNumberTruthiness = defineRule(() => ({
|
|
|
214
214
|
if (node.parent.kind !== SyntaxKind.JsxExpression) return;
|
|
215
215
|
const type = context.checker.getTypeAtLocation(node.left);
|
|
216
216
|
if (context.utils.typeOrUnionHasFlag(type, TypeFlags.NumberLike)) context.report({
|
|
217
|
+
node,
|
|
218
|
+
message: message$1
|
|
219
|
+
});
|
|
220
|
+
} }
|
|
221
|
+
}));
|
|
222
|
+
// src/rules/noComputedPropertiesWithUnionType.ts
|
|
223
|
+
const message = "Computed properties with union types are unsafe. Do `data[key] = value` instead";
|
|
224
|
+
const noComputedPropertiesWithUnionType = defineRule(() => ({
|
|
225
|
+
name: "arnaudBarre/noComputedPropertiesWithUnionType",
|
|
226
|
+
visitor: { ComputedPropertyName(context, node) {
|
|
227
|
+
if (node.parent.parent.kind === SyntaxKind.TypeLiteral) return;
|
|
228
|
+
const type = context.checker.getTypeAtLocation(node.expression);
|
|
229
|
+
if (context.utils.typeHasFlag(type, TypeFlags.Union)) context.report({
|
|
217
230
|
node,
|
|
218
231
|
message
|
|
219
232
|
});
|
|
@@ -224,37 +237,7 @@ const arnaudBarre = createRulesSet({
|
|
|
224
237
|
arrayCallbackReturn,
|
|
225
238
|
jsxFragmentCheckChildren,
|
|
226
239
|
jsxKey,
|
|
227
|
-
jsxNoNumberTruthiness
|
|
240
|
+
jsxNoNumberTruthiness,
|
|
241
|
+
noComputedPropertiesWithUnionType
|
|
228
242
|
});
|
|
229
|
-
|
|
230
|
-
...core.all(),
|
|
231
|
-
core.preferNullishCoalescing({ ignoreIfStatements: true }),
|
|
232
|
-
core.noMisusedPromises({
|
|
233
|
-
checksConditionals: false,
|
|
234
|
-
checksVoidReturn: {
|
|
235
|
-
arguments: false,
|
|
236
|
-
attributes: false
|
|
237
|
-
}
|
|
238
|
-
}),
|
|
239
|
-
core.noConfusingVoidExpression({ ignoreArrowShorthand: true }),
|
|
240
|
-
core.noUnnecessaryCondition({
|
|
241
|
-
allowConstantLoopConditions: "only-allowed-literals",
|
|
242
|
-
checkTypePredicates: true
|
|
243
|
-
}),
|
|
244
|
-
core.switchExhaustivenessCheck({
|
|
245
|
-
allowDefaultCaseForExhaustiveSwitch: false,
|
|
246
|
-
considerDefaultExhaustiveForUnions: true
|
|
247
|
-
}),
|
|
248
|
-
core.strictBooleanExpressions({
|
|
249
|
-
allowAny: false,
|
|
250
|
-
allowNullableBoolean: true,
|
|
251
|
-
allowNullableEnum: true,
|
|
252
|
-
allowNullableNumber: true,
|
|
253
|
-
allowNullableObject: true,
|
|
254
|
-
allowNullableString: true,
|
|
255
|
-
allowNumber: false,
|
|
256
|
-
allowString: false
|
|
257
|
-
}),
|
|
258
|
-
...arnaudBarre.all()
|
|
259
|
-
];
|
|
260
|
-
export { allRules, arnaudBarre };
|
|
243
|
+
export { 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.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:ArnaudBarre/tsl-config",
|