@arnaud-barre/tsl-config 0.3.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
@@ -46,4 +46,5 @@ export default defineConfig({
46
46
  - jsxFragmentCheckChildren: Validates that JSX shorthand fragment children match ReactNode type to workaround https://github.com/microsoft/TypeScript/issues/62358
47
47
  - jsxKey: Validate JSX has key prop when in array or iterator
48
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
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
@@ -4,4 +4,5 @@ export declare const arnaudBarre: import("tsl").RulesSet<{
4
4
  jsxKey: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
5
5
  jsxNoNumberTruthiness: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
6
6
  noComputedPropertiesWithUnionType: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
7
+ noUselessNullishCoalescing: (options?: "off" | undefined) => import("tsl").Rule<unknown>;
7
8
  }>;
package/index.js CHANGED
@@ -2,7 +2,7 @@ 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
  }
@@ -83,7 +83,7 @@ const getReactNodeSymbol = (program) => {
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
  };
@@ -232,12 +232,56 @@ const noComputedPropertiesWithUnionType = defineRule(() => ({
232
232
  });
233
233
  } }
234
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
+ }));
235
278
  // src/index.ts
236
279
  const arnaudBarre = createRulesSet({
237
280
  arrayCallbackReturn,
238
281
  jsxFragmentCheckChildren,
239
282
  jsxKey,
240
283
  jsxNoNumberTruthiness,
241
- noComputedPropertiesWithUnionType
284
+ noComputedPropertiesWithUnionType,
285
+ noUselessNullishCoalescing
242
286
  });
243
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.3.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",