tsl-dx 0.8.0 → 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.d.ts +1 -1
- package/dist/index.js +28 -184
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -88,6 +88,6 @@ type nullishOptions = {
|
|
|
88
88
|
* if (x == null) { }
|
|
89
89
|
* ```
|
|
90
90
|
*/
|
|
91
|
-
declare const nullish: (options?:
|
|
91
|
+
declare const nullish: (options?: "off" | nullishOptions | undefined) => tsl.Rule<unknown>;
|
|
92
92
|
//#endregion
|
|
93
93
|
export { noDuplicateExports, noDuplicateImports, noMultilineTemplateExpressionWithoutAutoDedent, nullish };
|
package/dist/index.js
CHANGED
|
@@ -66,124 +66,6 @@ function buildSuggestions$1(a, b) {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
//#endregion
|
|
70
|
-
//#region ../../.pkgs/eff/dist/index.js
|
|
71
|
-
/**
|
|
72
|
-
* Creates a function that can be used in a data-last (aka `pipe`able) or
|
|
73
|
-
* data-first style.
|
|
74
|
-
*
|
|
75
|
-
* The first parameter to `dual` is either the arity of the uncurried function
|
|
76
|
-
* or a predicate that determines if the function is being used in a data-first
|
|
77
|
-
* or data-last style.
|
|
78
|
-
*
|
|
79
|
-
* Using the arity is the most common use case, but there are some cases where
|
|
80
|
-
* you may want to use a predicate. For example, if you have a function that
|
|
81
|
-
* takes an optional argument, you can use a predicate to determine if the
|
|
82
|
-
* function is being used in a data-first or data-last style.
|
|
83
|
-
*
|
|
84
|
-
* You can pass either the arity of the uncurried function or a predicate
|
|
85
|
-
* which determines if the function is being used in a data-first or
|
|
86
|
-
* data-last style.
|
|
87
|
-
*
|
|
88
|
-
* **Example** (Using arity to determine data-first or data-last style)
|
|
89
|
-
*
|
|
90
|
-
* ```ts
|
|
91
|
-
* import { dual, pipe } from "effect/Function"
|
|
92
|
-
*
|
|
93
|
-
* const sum = dual<
|
|
94
|
-
* (that: number) => (self: number) => number,
|
|
95
|
-
* (self: number, that: number) => number
|
|
96
|
-
* >(2, (self, that) => self + that)
|
|
97
|
-
*
|
|
98
|
-
* console.log(sum(2, 3)) // 5
|
|
99
|
-
* console.log(pipe(2, sum(3))) // 5
|
|
100
|
-
* ```
|
|
101
|
-
*
|
|
102
|
-
* **Example** (Using call signatures to define the overloads)
|
|
103
|
-
*
|
|
104
|
-
* ```ts
|
|
105
|
-
* import { dual, pipe } from "effect/Function"
|
|
106
|
-
*
|
|
107
|
-
* const sum: {
|
|
108
|
-
* (that: number): (self: number) => number
|
|
109
|
-
* (self: number, that: number): number
|
|
110
|
-
* } = dual(2, (self: number, that: number): number => self + that)
|
|
111
|
-
*
|
|
112
|
-
* console.log(sum(2, 3)) // 5
|
|
113
|
-
* console.log(pipe(2, sum(3))) // 5
|
|
114
|
-
* ```
|
|
115
|
-
*
|
|
116
|
-
* **Example** (Using a predicate to determine data-first or data-last style)
|
|
117
|
-
*
|
|
118
|
-
* ```ts
|
|
119
|
-
* import { dual, pipe } from "effect/Function"
|
|
120
|
-
*
|
|
121
|
-
* const sum = dual<
|
|
122
|
-
* (that: number) => (self: number) => number,
|
|
123
|
-
* (self: number, that: number) => number
|
|
124
|
-
* >(
|
|
125
|
-
* (args) => args.length === 2,
|
|
126
|
-
* (self, that) => self + that
|
|
127
|
-
* )
|
|
128
|
-
*
|
|
129
|
-
* console.log(sum(2, 3)) // 5
|
|
130
|
-
* console.log(pipe(2, sum(3))) // 5
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* @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.
|
|
134
|
-
* @param body - The function to be curried.
|
|
135
|
-
* @since 1.0.0
|
|
136
|
-
*/
|
|
137
|
-
const dual = function(arity, body) {
|
|
138
|
-
if (typeof arity === "function") return function() {
|
|
139
|
-
return arity(arguments) ? body.apply(this, arguments) : ((self) => body(self, ...arguments));
|
|
140
|
-
};
|
|
141
|
-
switch (arity) {
|
|
142
|
-
case 0:
|
|
143
|
-
case 1: throw new RangeError(`Invalid arity ${arity}`);
|
|
144
|
-
case 2: return function(a, b) {
|
|
145
|
-
if (arguments.length >= 2) return body(a, b);
|
|
146
|
-
return function(self) {
|
|
147
|
-
return body(self, a);
|
|
148
|
-
};
|
|
149
|
-
};
|
|
150
|
-
case 3: return function(a, b, c) {
|
|
151
|
-
if (arguments.length >= 3) return body(a, b, c);
|
|
152
|
-
return function(self) {
|
|
153
|
-
return body(self, a, b);
|
|
154
|
-
};
|
|
155
|
-
};
|
|
156
|
-
default: return function() {
|
|
157
|
-
if (arguments.length >= arity) return body.apply(this, arguments);
|
|
158
|
-
const args = arguments;
|
|
159
|
-
return function(self) {
|
|
160
|
-
return body(self, ...args);
|
|
161
|
-
};
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
};
|
|
165
|
-
/**
|
|
166
|
-
* 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`.
|
|
167
|
-
* The result is obtained by first applying the `ab` function to `a` and then applying the `bc` function to the result of `ab`.
|
|
168
|
-
*
|
|
169
|
-
* @param self - The first function to apply (or the composed function in data-last style).
|
|
170
|
-
* @param bc - The second function to apply.
|
|
171
|
-
* @returns A composed function that applies both functions in sequence.
|
|
172
|
-
* @example
|
|
173
|
-
* ```ts
|
|
174
|
-
* import * as assert from "node:assert"
|
|
175
|
-
* import { compose } from "effect/Function"
|
|
176
|
-
*
|
|
177
|
-
* const increment = (n: number) => n + 1;
|
|
178
|
-
* const square = (n: number) => n * n;
|
|
179
|
-
*
|
|
180
|
-
* assert.strictEqual(compose(increment, square)(2), 9);
|
|
181
|
-
* ```
|
|
182
|
-
*
|
|
183
|
-
* @since 1.0.0
|
|
184
|
-
*/
|
|
185
|
-
const compose = dual(2, (ab, bc) => (a) => bc(ab(a)));
|
|
186
|
-
|
|
187
69
|
//#endregion
|
|
188
70
|
//#region src/rules/no-duplicate-imports.ts
|
|
189
71
|
const messages$2 = { default: (p) => `Duplicate import from module ${p.source}.` };
|
|
@@ -220,7 +102,7 @@ const noDuplicateImports = defineRule(() => {
|
|
|
220
102
|
node,
|
|
221
103
|
source: node.moduleSpecifier.getText(),
|
|
222
104
|
kind: importKind,
|
|
223
|
-
defaultImport: node.importClause.name?.getText(),
|
|
105
|
+
defaultImport: node.importClause.name?.getText() ?? null,
|
|
224
106
|
bindings: match(node.importClause.namedBindings).with({ kind: ts.SyntaxKind.NamedImports }, (nb) => ({
|
|
225
107
|
kind: "named",
|
|
226
108
|
imports: nb.elements.map((el) => el.getText())
|
|
@@ -333,9 +215,8 @@ const noMultilineTemplateExpressionWithoutAutoDedent = defineRule((options) => {
|
|
|
333
215
|
//#endregion
|
|
334
216
|
//#region src/rules/nullish.ts
|
|
335
217
|
const messages = {
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
replaceWithExpression: (p) => `Replace with '${p.expr}'.`
|
|
218
|
+
default: (p) => `Use '${p.op}' for nullish comparison.`,
|
|
219
|
+
replace: (p) => `Replace with '${p.expr}'.`
|
|
339
220
|
};
|
|
340
221
|
/**
|
|
341
222
|
* Rule to enforce the use of `unit` instead of `undefined` and loose equality for nullish checks.
|
|
@@ -359,69 +240,32 @@ const nullish = defineRule((options) => ({
|
|
|
359
240
|
createData(ctx) {
|
|
360
241
|
return { runtimeLibrary: options?.runtimeLibrary ?? "@local/eff" };
|
|
361
242
|
},
|
|
362
|
-
visitor: {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
message: messages.useUnitForUndefined,
|
|
385
|
-
suggestions: [{
|
|
386
|
-
message: messages.replaceWithExpression({ expr: "unit" }),
|
|
387
|
-
changes: [{
|
|
388
|
-
start: 0,
|
|
389
|
-
end: 0,
|
|
390
|
-
newText: `import type { unit } from '${ctx.data.runtimeLibrary}';\n`
|
|
391
|
-
}, {
|
|
392
|
-
node,
|
|
393
|
-
newText: "unit"
|
|
394
|
-
}]
|
|
395
|
-
}]
|
|
396
|
-
});
|
|
397
|
-
},
|
|
398
|
-
BinaryExpression(ctx, node) {
|
|
399
|
-
const newOperatorText = match(node.operatorToken.kind).with(SyntaxKind.EqualsEqualsEqualsToken, () => "==").with(SyntaxKind.ExclamationEqualsEqualsToken, () => "!=").otherwise(() => null);
|
|
400
|
-
if (newOperatorText == null) return;
|
|
401
|
-
const offendingChild = [node.left, node.right].find((n) => {
|
|
402
|
-
switch (n.kind) {
|
|
403
|
-
case SyntaxKind.NullKeyword: return true;
|
|
404
|
-
case SyntaxKind.Identifier: return n.text === "unit" || n.text === "undefined";
|
|
405
|
-
default: return false;
|
|
406
|
-
}
|
|
407
|
-
});
|
|
408
|
-
if (offendingChild == null) return;
|
|
409
|
-
ctx.report({
|
|
410
|
-
message: messages.useLooseNullishComparison({ op: newOperatorText }),
|
|
411
|
-
node,
|
|
412
|
-
suggestions: [{
|
|
413
|
-
message: messages.replaceWithExpression({ expr: offendingChild === node.left ? `null ${newOperatorText} ${node.right.getText()}` : `${node.left.getText()} ${newOperatorText} null` }),
|
|
414
|
-
changes: [{
|
|
415
|
-
node: node.operatorToken,
|
|
416
|
-
newText: newOperatorText
|
|
417
|
-
}, {
|
|
418
|
-
node: offendingChild,
|
|
419
|
-
newText: "null"
|
|
420
|
-
}]
|
|
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"
|
|
421
265
|
}]
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
266
|
+
}]
|
|
267
|
+
});
|
|
268
|
+
} }
|
|
425
269
|
}));
|
|
426
270
|
|
|
427
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",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@liautaud/typezod": "^2.0.0",
|
|
28
28
|
"dedent": "^1.7.1",
|
|
29
|
-
"tsdown": "^0.
|
|
29
|
+
"tsdown": "^0.21.0-beta.2",
|
|
30
30
|
"tsl": "^1.0.29",
|
|
31
31
|
"vitest": "^4.0.18",
|
|
32
32
|
"@local/configs": "0.0.0",
|