tsl-dx 0.7.2 → 0.9.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/dist/index.js +72 -233
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineRule } from "tsl";
|
|
2
2
|
import ts, { SyntaxKind } from "typescript";
|
|
3
|
-
import {
|
|
3
|
+
import { match } from "ts-pattern";
|
|
4
4
|
|
|
5
5
|
//#region src/rules/no-duplicate-exports.ts
|
|
6
6
|
const messages$3 = { default: (p) => `Duplicate export from module ${p.source}.` };
|
|
@@ -37,7 +37,7 @@ const noDuplicateExports = defineRule(() => {
|
|
|
37
37
|
ctx.report({
|
|
38
38
|
node,
|
|
39
39
|
message: messages$3.default({ source }),
|
|
40
|
-
suggestions: buildSuggestions(duplicateExport, node)
|
|
40
|
+
suggestions: buildSuggestions$1(duplicateExport, node)
|
|
41
41
|
});
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
@@ -45,7 +45,7 @@ const noDuplicateExports = defineRule(() => {
|
|
|
45
45
|
} }
|
|
46
46
|
};
|
|
47
47
|
});
|
|
48
|
-
function buildSuggestions(a, b) {
|
|
48
|
+
function buildSuggestions$1(a, b) {
|
|
49
49
|
switch (true) {
|
|
50
50
|
case ts.isNamedExports(a.exportClause) && ts.isNamedExports(b.exportClause): {
|
|
51
51
|
const aElements = a.exportClause.elements.map((el) => el.getText());
|
|
@@ -66,128 +66,6 @@ function buildSuggestions(a, b) {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
//#endregion
|
|
70
|
-
//#region ../../.pkgs/eff/dist/index.js
|
|
71
|
-
/**
|
|
72
|
-
* alias for `undefined`.
|
|
73
|
-
*/
|
|
74
|
-
const unit = void 0;
|
|
75
|
-
/**
|
|
76
|
-
* Creates a function that can be used in a data-last (aka `pipe`able) or
|
|
77
|
-
* data-first style.
|
|
78
|
-
*
|
|
79
|
-
* The first parameter to `dual` is either the arity of the uncurried function
|
|
80
|
-
* or a predicate that determines if the function is being used in a data-first
|
|
81
|
-
* or data-last style.
|
|
82
|
-
*
|
|
83
|
-
* Using the arity is the most common use case, but there are some cases where
|
|
84
|
-
* you may want to use a predicate. For example, if you have a function that
|
|
85
|
-
* takes an optional argument, you can use a predicate to determine if the
|
|
86
|
-
* function is being used in a data-first or data-last style.
|
|
87
|
-
*
|
|
88
|
-
* You can pass either the arity of the uncurried function or a predicate
|
|
89
|
-
* which determines if the function is being used in a data-first or
|
|
90
|
-
* data-last style.
|
|
91
|
-
*
|
|
92
|
-
* **Example** (Using arity to determine data-first or data-last style)
|
|
93
|
-
*
|
|
94
|
-
* ```ts
|
|
95
|
-
* import { dual, pipe } from "effect/Function"
|
|
96
|
-
*
|
|
97
|
-
* const sum = dual<
|
|
98
|
-
* (that: number) => (self: number) => number,
|
|
99
|
-
* (self: number, that: number) => number
|
|
100
|
-
* >(2, (self, that) => self + that)
|
|
101
|
-
*
|
|
102
|
-
* console.log(sum(2, 3)) // 5
|
|
103
|
-
* console.log(pipe(2, sum(3))) // 5
|
|
104
|
-
* ```
|
|
105
|
-
*
|
|
106
|
-
* **Example** (Using call signatures to define the overloads)
|
|
107
|
-
*
|
|
108
|
-
* ```ts
|
|
109
|
-
* import { dual, pipe } from "effect/Function"
|
|
110
|
-
*
|
|
111
|
-
* const sum: {
|
|
112
|
-
* (that: number): (self: number) => number
|
|
113
|
-
* (self: number, that: number): number
|
|
114
|
-
* } = dual(2, (self: number, that: number): number => self + that)
|
|
115
|
-
*
|
|
116
|
-
* console.log(sum(2, 3)) // 5
|
|
117
|
-
* console.log(pipe(2, sum(3))) // 5
|
|
118
|
-
* ```
|
|
119
|
-
*
|
|
120
|
-
* **Example** (Using a predicate to determine data-first or data-last style)
|
|
121
|
-
*
|
|
122
|
-
* ```ts
|
|
123
|
-
* import { dual, pipe } from "effect/Function"
|
|
124
|
-
*
|
|
125
|
-
* const sum = dual<
|
|
126
|
-
* (that: number) => (self: number) => number,
|
|
127
|
-
* (self: number, that: number) => number
|
|
128
|
-
* >(
|
|
129
|
-
* (args) => args.length === 2,
|
|
130
|
-
* (self, that) => self + that
|
|
131
|
-
* )
|
|
132
|
-
*
|
|
133
|
-
* console.log(sum(2, 3)) // 5
|
|
134
|
-
* console.log(pipe(2, sum(3))) // 5
|
|
135
|
-
* ```
|
|
136
|
-
*
|
|
137
|
-
* @param arity - The arity of the uncurried function or a predicate that determines if the function is being used in a data-first or data-last style.
|
|
138
|
-
* @param body - The function to be curried.
|
|
139
|
-
* @since 1.0.0
|
|
140
|
-
*/
|
|
141
|
-
const dual = function(arity, body) {
|
|
142
|
-
if (typeof arity === "function") return function() {
|
|
143
|
-
return arity(arguments) ? body.apply(this, arguments) : ((self) => body(self, ...arguments));
|
|
144
|
-
};
|
|
145
|
-
switch (arity) {
|
|
146
|
-
case 0:
|
|
147
|
-
case 1: throw new RangeError(`Invalid arity ${arity}`);
|
|
148
|
-
case 2: return function(a, b) {
|
|
149
|
-
if (arguments.length >= 2) return body(a, b);
|
|
150
|
-
return function(self) {
|
|
151
|
-
return body(self, a);
|
|
152
|
-
};
|
|
153
|
-
};
|
|
154
|
-
case 3: return function(a, b, c) {
|
|
155
|
-
if (arguments.length >= 3) return body(a, b, c);
|
|
156
|
-
return function(self) {
|
|
157
|
-
return body(self, a, b);
|
|
158
|
-
};
|
|
159
|
-
};
|
|
160
|
-
default: return function() {
|
|
161
|
-
if (arguments.length >= arity) return body.apply(this, arguments);
|
|
162
|
-
const args = arguments;
|
|
163
|
-
return function(self) {
|
|
164
|
-
return body(self, ...args);
|
|
165
|
-
};
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
/**
|
|
170
|
-
* Composes two functions, `ab` and `bc` into a single function that takes in an argument `a` of type `A` and returns a result of type `C`.
|
|
171
|
-
* The result is obtained by first applying the `ab` function to `a` and then applying the `bc` function to the result of `ab`.
|
|
172
|
-
*
|
|
173
|
-
* @param self - The first function to apply (or the composed function in data-last style).
|
|
174
|
-
* @param bc - The second function to apply.
|
|
175
|
-
* @returns A composed function that applies both functions in sequence.
|
|
176
|
-
* @example
|
|
177
|
-
* ```ts
|
|
178
|
-
* import * as assert from "node:assert"
|
|
179
|
-
* import { compose } from "effect/Function"
|
|
180
|
-
*
|
|
181
|
-
* const increment = (n: number) => n + 1;
|
|
182
|
-
* const square = (n: number) => n * n;
|
|
183
|
-
*
|
|
184
|
-
* assert.strictEqual(compose(increment, square)(2), 9);
|
|
185
|
-
* ```
|
|
186
|
-
*
|
|
187
|
-
* @since 1.0.0
|
|
188
|
-
*/
|
|
189
|
-
const compose = dual(2, (ab, bc) => (a) => bc(ab(a)));
|
|
190
|
-
|
|
191
69
|
//#endregion
|
|
192
70
|
//#region src/rules/no-duplicate-imports.ts
|
|
193
71
|
const messages$2 = { default: (p) => `Duplicate import from module ${p.source}.` };
|
|
@@ -211,64 +89,63 @@ const noDuplicateImports = defineRule(() => {
|
|
|
211
89
|
return {
|
|
212
90
|
name: "dx/no-duplicate-imports",
|
|
213
91
|
createData() {
|
|
214
|
-
return { imports: [
|
|
215
|
-
[],
|
|
216
|
-
[],
|
|
217
|
-
[]
|
|
218
|
-
] };
|
|
92
|
+
return { imports: new Map([
|
|
93
|
+
["value", []],
|
|
94
|
+
["type", []],
|
|
95
|
+
["defer", []]
|
|
96
|
+
]) };
|
|
219
97
|
},
|
|
220
98
|
visitor: { ImportDeclaration(ctx, node) {
|
|
221
99
|
if (node.importClause == null) return;
|
|
222
|
-
const importKind =
|
|
100
|
+
const importKind = match(node.importClause.phaseModifier).with(ts.SyntaxKind.TypeKeyword, () => "type").with(ts.SyntaxKind.DeferKeyword, () => "defer").otherwise(() => "value");
|
|
223
101
|
const importInfo = {
|
|
224
102
|
node,
|
|
225
103
|
source: node.moduleSpecifier.getText(),
|
|
226
104
|
kind: importKind,
|
|
227
|
-
|
|
105
|
+
defaultImport: node.importClause.name?.getText() ?? null,
|
|
106
|
+
bindings: match(node.importClause.namedBindings).with({ kind: ts.SyntaxKind.NamedImports }, (nb) => ({
|
|
107
|
+
kind: "named",
|
|
108
|
+
imports: nb.elements.map((el) => el.getText())
|
|
109
|
+
})).with({ kind: ts.SyntaxKind.NamespaceImport }, (nb) => ({
|
|
110
|
+
kind: "namespace",
|
|
111
|
+
name: nb.name.getText()
|
|
112
|
+
})).otherwise(() => ({
|
|
113
|
+
kind: "named",
|
|
114
|
+
imports: []
|
|
115
|
+
}))
|
|
228
116
|
};
|
|
229
|
-
const existingImports = ctx.data.imports
|
|
117
|
+
const existingImports = ctx.data.imports.get(importKind);
|
|
230
118
|
const duplicateImport = existingImports.find((imp) => imp.source === importInfo.source);
|
|
231
|
-
if (duplicateImport
|
|
232
|
-
|
|
233
|
-
node,
|
|
234
|
-
message: messages$2.default({ source: importInfo.source }),
|
|
235
|
-
suggestions: importKind > 1 ? [] : [{
|
|
236
|
-
message: "Merge duplicate imports",
|
|
237
|
-
changes: [{
|
|
238
|
-
node,
|
|
239
|
-
newText: ""
|
|
240
|
-
}, {
|
|
241
|
-
node: duplicateImport.node,
|
|
242
|
-
newText: buildMergedImport(duplicateImport, importInfo)
|
|
243
|
-
}]
|
|
244
|
-
}]
|
|
245
|
-
});
|
|
119
|
+
if (duplicateImport == null) {
|
|
120
|
+
existingImports.push(importInfo);
|
|
246
121
|
return;
|
|
247
122
|
}
|
|
248
|
-
|
|
123
|
+
ctx.report({
|
|
124
|
+
node,
|
|
125
|
+
message: messages$2.default({ source: importInfo.source }),
|
|
126
|
+
suggestions: buildSuggestions(duplicateImport, importInfo)
|
|
127
|
+
});
|
|
249
128
|
} }
|
|
250
129
|
};
|
|
251
130
|
});
|
|
252
|
-
function
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
function decodeImportClause(node) {
|
|
256
|
-
const { name, namedBindings } = node;
|
|
257
|
-
return {
|
|
258
|
-
defaultImport: name?.getText(),
|
|
259
|
-
namedImports: namedBindings != null && ts.isNamedImports(namedBindings) ? namedBindings.elements.map((el) => el.getText()) : [],
|
|
260
|
-
namespaceImport: namedBindings != null && ts.isNamespaceImport(namedBindings) ? namedBindings.name.getText() : unit
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
function buildMergedImport(a, b) {
|
|
131
|
+
function buildSuggestions(existing, incoming) {
|
|
132
|
+
if (incoming.kind === "defer" || incoming.bindings.kind === "namespace" || existing.bindings.kind === "namespace") return [];
|
|
264
133
|
const parts = [];
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
const
|
|
270
|
-
|
|
271
|
-
|
|
134
|
+
const defaultImport = existing.defaultImport ?? incoming.defaultImport;
|
|
135
|
+
if (defaultImport != null) parts.push(defaultImport);
|
|
136
|
+
const mergedImports = Array.from(new Set([...existing.bindings.imports, ...incoming.bindings.imports]));
|
|
137
|
+
if (mergedImports.length > 0) parts.push(`{ ${mergedImports.join(", ")} }`);
|
|
138
|
+
const importKindPrefix = incoming.kind === "value" ? "import" : "import type";
|
|
139
|
+
return [{
|
|
140
|
+
message: "Merge duplicate imports",
|
|
141
|
+
changes: [{
|
|
142
|
+
node: incoming.node,
|
|
143
|
+
newText: ""
|
|
144
|
+
}, {
|
|
145
|
+
node: existing.node,
|
|
146
|
+
newText: `${importKindPrefix} ${parts.join(", ")} from ${existing.source};`
|
|
147
|
+
}]
|
|
148
|
+
}];
|
|
272
149
|
}
|
|
273
150
|
|
|
274
151
|
//#endregion
|
|
@@ -338,9 +215,8 @@ const noMultilineTemplateExpressionWithoutAutoDedent = defineRule((options) => {
|
|
|
338
215
|
//#endregion
|
|
339
216
|
//#region src/rules/nullish.ts
|
|
340
217
|
const messages = {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
replaceWithExpression: (p) => `Replace with '${p.expr}'.`
|
|
218
|
+
default: (p) => `Use '${p.op}' for nullish comparison.`,
|
|
219
|
+
replace: (p) => `Replace with '${p.expr}'.`
|
|
344
220
|
};
|
|
345
221
|
/**
|
|
346
222
|
* Rule to enforce the use of `unit` instead of `undefined` and loose equality for nullish checks.
|
|
@@ -364,69 +240,32 @@ const nullish = defineRule((options) => ({
|
|
|
364
240
|
createData(ctx) {
|
|
365
241
|
return { runtimeLibrary: options?.runtimeLibrary ?? "@local/eff" };
|
|
366
242
|
},
|
|
367
|
-
visitor: {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
message: messages.useUnitForUndefined,
|
|
390
|
-
suggestions: [{
|
|
391
|
-
message: messages.replaceWithExpression({ expr: "unit" }),
|
|
392
|
-
changes: [{
|
|
393
|
-
start: 0,
|
|
394
|
-
end: 0,
|
|
395
|
-
newText: `import type { unit } from '${ctx.data.runtimeLibrary}';\n`
|
|
396
|
-
}, {
|
|
397
|
-
node,
|
|
398
|
-
newText: "unit"
|
|
399
|
-
}]
|
|
400
|
-
}]
|
|
401
|
-
});
|
|
402
|
-
},
|
|
403
|
-
BinaryExpression(ctx, node) {
|
|
404
|
-
const newOperatorText = match(node.operatorToken.kind).with(SyntaxKind.EqualsEqualsEqualsToken, () => "==").with(SyntaxKind.ExclamationEqualsEqualsToken, () => "!=").otherwise(() => null);
|
|
405
|
-
if (newOperatorText == null) return;
|
|
406
|
-
const offendingChild = [node.left, node.right].find((n) => {
|
|
407
|
-
switch (n.kind) {
|
|
408
|
-
case SyntaxKind.NullKeyword: return true;
|
|
409
|
-
case SyntaxKind.Identifier: return n.text === "unit" || n.text === "undefined";
|
|
410
|
-
default: return false;
|
|
411
|
-
}
|
|
412
|
-
});
|
|
413
|
-
if (offendingChild == null) return;
|
|
414
|
-
ctx.report({
|
|
415
|
-
message: messages.useLooseNullishComparison({ op: newOperatorText }),
|
|
416
|
-
node,
|
|
417
|
-
suggestions: [{
|
|
418
|
-
message: messages.replaceWithExpression({ expr: offendingChild === node.left ? `null ${newOperatorText} ${node.right.getText()}` : `${node.left.getText()} ${newOperatorText} null` }),
|
|
419
|
-
changes: [{
|
|
420
|
-
node: node.operatorToken,
|
|
421
|
-
newText: newOperatorText
|
|
422
|
-
}, {
|
|
423
|
-
node: offendingChild,
|
|
424
|
-
newText: "null"
|
|
425
|
-
}]
|
|
243
|
+
visitor: { BinaryExpression(ctx, node) {
|
|
244
|
+
const newOperatorText = match(node.operatorToken.kind).with(SyntaxKind.EqualsEqualsEqualsToken, () => "==").with(SyntaxKind.ExclamationEqualsEqualsToken, () => "!=").otherwise(() => null);
|
|
245
|
+
if (newOperatorText == null) return;
|
|
246
|
+
const offendingChild = [node.left, node.right].find((n) => {
|
|
247
|
+
switch (n.kind) {
|
|
248
|
+
case SyntaxKind.NullKeyword: return true;
|
|
249
|
+
case SyntaxKind.Identifier: return n.text === "unit" || n.text === "undefined";
|
|
250
|
+
default: return false;
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
if (offendingChild == null) return;
|
|
254
|
+
ctx.report({
|
|
255
|
+
message: messages.default({ op: newOperatorText }),
|
|
256
|
+
node,
|
|
257
|
+
suggestions: [{
|
|
258
|
+
message: messages.replace({ expr: offendingChild === node.left ? `null ${newOperatorText} ${node.right.getText()}` : `${node.left.getText()} ${newOperatorText} null` }),
|
|
259
|
+
changes: [{
|
|
260
|
+
node: node.operatorToken,
|
|
261
|
+
newText: newOperatorText
|
|
262
|
+
}, {
|
|
263
|
+
node: offendingChild,
|
|
264
|
+
newText: "null"
|
|
426
265
|
}]
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
266
|
+
}]
|
|
267
|
+
});
|
|
268
|
+
} }
|
|
430
269
|
}));
|
|
431
270
|
|
|
432
271
|
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tsl-dx",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "A tsl plugin for better JavaScript/TypeScript DX.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tsl",
|
|
@@ -24,8 +24,9 @@
|
|
|
24
24
|
"ts-pattern": "^5.9.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
+
"@liautaud/typezod": "^2.0.0",
|
|
27
28
|
"dedent": "^1.7.1",
|
|
28
|
-
"tsdown": "^0.
|
|
29
|
+
"tsdown": "^0.21.0-beta.2",
|
|
29
30
|
"tsl": "^1.0.29",
|
|
30
31
|
"vitest": "^4.0.18",
|
|
31
32
|
"@local/configs": "0.0.0",
|