tsl-dx 0.5.1 → 0.5.3
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/dist/index.d.ts +1 -1
- package/dist/index.js +26 -3
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -42,6 +42,6 @@ declare const noDuplicateImports: (options?: "off" | undefined) => tsl.Rule<unkn
|
|
|
42
42
|
declare const noMultilineTemplateExpressionsWithoutAutoDedent: (options?: "off" | undefined) => tsl.Rule<unknown>;
|
|
43
43
|
//#endregion
|
|
44
44
|
//#region src/rules/nullish.d.ts
|
|
45
|
-
declare const nullish: (options?: "off" | undefined) => tsl.Rule<unknown>;
|
|
45
|
+
declare const nullish: (options?: Record<string, unknown> | "off" | undefined) => tsl.Rule<unknown>;
|
|
46
46
|
//#endregion
|
|
47
47
|
export { noDuplicateExports, noDuplicateImports, noMultilineTemplateExpressionsWithoutAutoDedent, nullish };
|
package/dist/index.js
CHANGED
|
@@ -297,9 +297,31 @@ const messages = {
|
|
|
297
297
|
useLooseNullishComparison: (p) => `Use '${p.op}' for nullish comparison.`
|
|
298
298
|
};
|
|
299
299
|
const suggestions = { replaceWithExpression: (p) => `Replace with '${p.expr}'.` };
|
|
300
|
-
const nullish = defineRule(() => ({
|
|
300
|
+
const nullish = defineRule((options) => ({
|
|
301
301
|
name: "dx/nullish",
|
|
302
|
+
createData(ctx) {
|
|
303
|
+
return { runtimeLibrary: options?.runtimeLibrary ?? "@local/eff" };
|
|
304
|
+
},
|
|
302
305
|
visitor: {
|
|
306
|
+
Identifier(ctx, node) {
|
|
307
|
+
if (node.getSourceFile().isDeclarationFile) return;
|
|
308
|
+
if (node.parent.kind === SyntaxKind.BinaryExpression || node.text !== "undefined") return;
|
|
309
|
+
ctx.report({
|
|
310
|
+
node,
|
|
311
|
+
message: messages.useUnitForUndefined,
|
|
312
|
+
suggestions: [{
|
|
313
|
+
message: suggestions.replaceWithExpression({ expr: "unit" }),
|
|
314
|
+
changes: [{
|
|
315
|
+
node,
|
|
316
|
+
newText: "unit"
|
|
317
|
+
}, {
|
|
318
|
+
start: 0,
|
|
319
|
+
end: 0,
|
|
320
|
+
newText: `import { unit } from '${ctx.data.runtimeLibrary}';\n`
|
|
321
|
+
}]
|
|
322
|
+
}]
|
|
323
|
+
});
|
|
324
|
+
},
|
|
303
325
|
UndefinedKeyword(ctx, node) {
|
|
304
326
|
if (node.getSourceFile().isDeclarationFile) return;
|
|
305
327
|
ctx.report({
|
|
@@ -313,18 +335,19 @@ const nullish = defineRule(() => ({
|
|
|
313
335
|
}, {
|
|
314
336
|
start: 0,
|
|
315
337
|
end: 0,
|
|
316
|
-
newText:
|
|
338
|
+
newText: `import type { unit } from '${ctx.data.runtimeLibrary}';\n`
|
|
317
339
|
}]
|
|
318
340
|
}]
|
|
319
341
|
});
|
|
320
342
|
},
|
|
321
343
|
BinaryExpression(ctx, node) {
|
|
344
|
+
if (node.getSourceFile().isDeclarationFile) return;
|
|
322
345
|
const newOperatorText = match(node.operatorToken.kind).with(SyntaxKind.EqualsEqualsEqualsToken, () => "==").with(SyntaxKind.ExclamationEqualsEqualsToken, () => "!=").otherwise(() => null);
|
|
323
346
|
if (newOperatorText == null) return;
|
|
324
347
|
const offendingChild = [node.left, node.right].find((n) => {
|
|
325
348
|
switch (n.kind) {
|
|
326
349
|
case SyntaxKind.NullKeyword: return true;
|
|
327
|
-
case SyntaxKind.Identifier: return n.
|
|
350
|
+
case SyntaxKind.Identifier: return n.text === "unit" || n.text === "undefined";
|
|
328
351
|
default: return false;
|
|
329
352
|
}
|
|
330
353
|
});
|