@arnaud-barre/tsl-config 0.2.0 → 0.3.1

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
@@ -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 { allRules } from "@arnaud-barre/tsl-config";
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: [...allRules],
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,5 @@ 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. [Feature request on oxlint](https://github.com/oxc-project/oxc/issues/30593)
50
+ - noUselessNullishCoalescing: Catch `?? null` when the left side can't be `undefined` and `?? undefined` when it can't be `null`. [Feature request on oxlint](https://github.com/oxc-project/oxc/issues/30698)
package/index.d.ts CHANGED
@@ -3,5 +3,6 @@ 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
- export declare const allRules: import("tsl").Rule<unknown>[];
6
+ noComputedPropertiesWithUnionType: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
7
+ noUselessNullishCoalescing: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
8
+ }>;
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { core, createRulesSet, defineRule } from "tsl";
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.";
5
- const messages$1 = {
5
+ const messages$2 = {
6
6
  shouldBeBoolean: base + " Update the code to return a boolean value.",
7
7
  flatMap: base + " If this is intentional, update the code to return an array of a single value.",
8
8
  addTypeAnnotation: base + " If this is intentional, add an explicit type to the function or use a disable comment."
@@ -35,7 +35,7 @@ const arrayCallbackReturn = defineRule(() => ({
35
35
  if (messageId === "addTypeAnnotation" && node.parent.typeArguments) return;
36
36
  context.report({
37
37
  node: node.name,
38
- message: messages$1[messageId]
38
+ message: messages$2[messageId]
39
39
  });
40
40
  }
41
41
  }
@@ -43,7 +43,7 @@ const arrayCallbackReturn = defineRule(() => ({
43
43
  } }
44
44
  }));
45
45
  // src/rules/jsxFragmentCheckChildren.ts
46
- const message$1 = "Children doesn't match ReactNode type. See https://github.com/microsoft/TypeScript/issues/62358";
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$1
66
+ message: message$2
67
67
  });
68
68
  }
69
69
  } }
70
70
  }));
71
71
  const weekMap = /* @__PURE__ */ new WeakMap();
72
- function getReactNodeSymbol(program) {
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,10 +80,10 @@ 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
- const messages = {
86
+ const messages$1 = {
87
87
  missingKey: "Missing key",
88
88
  keyFirst: "Key should be first",
89
89
  keySpread: "Key should not be spread",
@@ -92,7 +92,7 @@ const messages = {
92
92
  const jsxKey = defineRule(() => {
93
93
  const reportFragment = (node, context) => {
94
94
  context.report({
95
- message: messages.missingKey,
95
+ message: messages$1.missingKey,
96
96
  node: node.openingFragment
97
97
  });
98
98
  };
@@ -106,18 +106,18 @@ const jsxKey = defineRule(() => {
106
106
  };
107
107
  if (attr.kind === SyntaxKind.JsxSpreadAttribute) {
108
108
  if (context.checker.getTypeAtLocation(attr.expression).getProperty("key")) context.report({
109
- message: messages.keySpread,
109
+ message: messages$1.keySpread,
110
110
  node: attr
111
111
  });
112
112
  }
113
113
  }
114
114
  if (keyAttr) {
115
115
  if (keyAttr.index !== 0) context.report({
116
- message: messages.keyFirst,
116
+ message: messages$1.keyFirst,
117
117
  node: keyAttr.node
118
118
  });
119
119
  } else context.report({
120
- message: messages.missingKey,
120
+ message: messages$1.missingKey,
121
121
  node: openingElement
122
122
  });
123
123
  };
@@ -169,7 +169,7 @@ const jsxKey = defineRule(() => {
169
169
  if (!reactComponentNameRE.test(id.text)) return;
170
170
  if (fnNode.parameters.length === 0) return;
171
171
  if (context.checker.getTypeAtLocation(fnNode.parameters[0]).getProperty("key")) context.report({
172
- message: messages.keyInProps,
172
+ message: messages$1.keyInProps,
173
173
  node: id
174
174
  });
175
175
  };
@@ -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,47 +214,74 @@ 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
  });
220
233
  } }
221
234
  }));
235
+ // src/rules/noUselessNullishCoalescing.ts
236
+ const messages = {
237
+ uselessNullCoalescing: "Unnecessary coalescing, undefined is not included in the left-hand side of the `??` operator.",
238
+ uselessUndefinedCoalescing: "Unnecessary coalescing, null is not included in the left-hand side of the `??` operator.",
239
+ removeNullishCoalescing: "Remove unnecessary nullish coalescing."
240
+ };
241
+ const ignoredFlags = [
242
+ TypeFlags.Any,
243
+ TypeFlags.Unknown,
244
+ TypeFlags.TypeParameter,
245
+ TypeFlags.TypeVariable,
246
+ TypeFlags.Never
247
+ ];
248
+ const noUselessNullishCoalescing = defineRule(() => ({
249
+ name: "arnaudBarre/noUselessNullishCoalescing",
250
+ visitor: { BinaryExpression(context, node) {
251
+ if (node.operatorToken.kind !== SyntaxKind.QuestionQuestionToken && node.operatorToken.kind !== SyntaxKind.QuestionQuestionEqualsToken) return;
252
+ const isNull = node.right.kind === SyntaxKind.NullKeyword;
253
+ const isUndefined = node.right.kind === SyntaxKind.Identifier && node.right.text === "undefined";
254
+ if (!isNull && !isUndefined) return;
255
+ if (node.left.kind === SyntaxKind.ElementAccessExpression && context.compilerOptions.noUncheckedIndexedAccess !== true) return;
256
+ const leftType = context.utils.getConstrainedTypeAtLocation(node.left);
257
+ const hasFlag = (flag) => context.utils.typeOrUnionHasFlag(leftType, flag);
258
+ if (ignoredFlags.some(hasFlag)) return;
259
+ const hasNull = hasFlag(TypeFlags.Null);
260
+ const hasUndefined = hasFlag(TypeFlags.Undefined) || hasFlag(TypeFlags.Void);
261
+ const message = isNull && hasNull && !hasUndefined ? messages.uselessNullCoalescing : isUndefined && hasUndefined && !hasNull ? messages.uselessUndefinedCoalescing : null;
262
+ if (message === null) return;
263
+ context.report({
264
+ start: node.operatorToken.getStart(),
265
+ end: node.right.getEnd(),
266
+ message,
267
+ suggestions: [{
268
+ message: messages.removeNullishCoalescing,
269
+ changes: [{
270
+ start: node.left.getEnd(),
271
+ end: node.right.getEnd(),
272
+ newText: ""
273
+ }]
274
+ }]
275
+ });
276
+ } }
277
+ }));
222
278
  // src/index.ts
223
279
  const arnaudBarre = createRulesSet({
224
280
  arrayCallbackReturn,
225
281
  jsxFragmentCheckChildren,
226
282
  jsxKey,
227
- jsxNoNumberTruthiness
283
+ jsxNoNumberTruthiness,
284
+ noComputedPropertiesWithUnionType,
285
+ noUselessNullishCoalescing
228
286
  });
229
- const allRules = [
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 };
287
+ 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.2.0",
4
+ "version": "0.3.1",
5
5
  "author": "Arnaud Barré (https://github.com/ArnaudBarre)",
6
6
  "license": "MIT",
7
7
  "repository": "github:ArnaudBarre/tsl-config",