tsl-dx 0.4.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/LICENSE +21 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.js +309 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Rel1cx
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as tsl0 from "tsl";
|
|
2
|
+
|
|
3
|
+
//#region src/rules/consistent-nullish-comparison.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Rule to enforce the use of `== null` or `!= null` for nullish comparisons.
|
|
6
|
+
*
|
|
7
|
+
* @since 0.0.0
|
|
8
|
+
*/
|
|
9
|
+
declare const consistentNullishComparison: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/rules/no-duplicate-exports.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* Rule to detect and merge duplicate `export from` statements from the same module.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* // Incorrect
|
|
19
|
+
* export { A } from 'module';
|
|
20
|
+
* export { B } from 'module';
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ```ts
|
|
24
|
+
* // Correct
|
|
25
|
+
* export { A, B } from 'module';
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare const noDuplicateExports: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/rules/no-duplicate-imports.d.ts
|
|
31
|
+
/**
|
|
32
|
+
* Rule to detect and merge duplicate `import from` statements from the same module.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* // Incorrect
|
|
38
|
+
* import { A } from 'module';
|
|
39
|
+
* import { B } from 'module';
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* // Correct
|
|
44
|
+
* import { A, B } from 'module';
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare const noDuplicateImports: (options?: "off" | undefined) => tsl0.Rule<unknown>;
|
|
48
|
+
//#endregion
|
|
49
|
+
export { consistentNullishComparison, noDuplicateExports, noDuplicateImports };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { P, match } from "ts-pattern";
|
|
2
|
+
import { defineRule } from "tsl";
|
|
3
|
+
import ts, { SyntaxKind } from "typescript";
|
|
4
|
+
|
|
5
|
+
//#region src/rules/consistent-nullish-comparison.ts
|
|
6
|
+
/**
|
|
7
|
+
* Rule to enforce the use of `== null` or `!= null` for nullish comparisons.
|
|
8
|
+
*
|
|
9
|
+
* @since 0.0.0
|
|
10
|
+
*/
|
|
11
|
+
const consistentNullishComparison = defineRule(() => ({
|
|
12
|
+
name: "local/consistentNullishComparison",
|
|
13
|
+
visitor: { BinaryExpression(context, node) {
|
|
14
|
+
const newOperatorText = match(node.operatorToken.kind).with(SyntaxKind.EqualsEqualsEqualsToken, () => "==").with(SyntaxKind.ExclamationEqualsEqualsToken, () => "!=").otherwise(() => null);
|
|
15
|
+
if (newOperatorText == null) return;
|
|
16
|
+
const offendingChild = [node.left, node.right].find((n) => {
|
|
17
|
+
switch (n.kind) {
|
|
18
|
+
case SyntaxKind.NullKeyword: return true;
|
|
19
|
+
case SyntaxKind.Identifier: return n.escapedText === "undefined";
|
|
20
|
+
default: return false;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
if (offendingChild == null) return;
|
|
24
|
+
context.report({
|
|
25
|
+
message: `Use '${newOperatorText}' for nullish comparison.`,
|
|
26
|
+
node,
|
|
27
|
+
suggestions: [{
|
|
28
|
+
message: offendingChild === node.left ? `Replace with 'null ${newOperatorText} ${node.right.getText()}'.` : `Replace with '${node.left.getText()} ${newOperatorText} null'.`,
|
|
29
|
+
changes: [{
|
|
30
|
+
node: node.operatorToken,
|
|
31
|
+
newText: newOperatorText
|
|
32
|
+
}, {
|
|
33
|
+
node: offendingChild,
|
|
34
|
+
newText: "null"
|
|
35
|
+
}]
|
|
36
|
+
}]
|
|
37
|
+
});
|
|
38
|
+
} }
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/rules/no-duplicate-exports.ts
|
|
43
|
+
const messages$1 = { noDuplicateExports: (p) => `Duplicate export from module ${p.source}.` };
|
|
44
|
+
function isReExportDeclaration(node) {
|
|
45
|
+
return node.exportClause != null && node.moduleSpecifier != null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Rule to detect and merge duplicate `export from` statements from the same module.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* // Incorrect
|
|
54
|
+
* export { A } from 'module';
|
|
55
|
+
* export { B } from 'module';
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* // Correct
|
|
60
|
+
* export { A, B } from 'module';
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
const noDuplicateExports = defineRule(() => {
|
|
64
|
+
return {
|
|
65
|
+
name: "module/no-duplicate-exports",
|
|
66
|
+
createData() {
|
|
67
|
+
return { exports: [] };
|
|
68
|
+
},
|
|
69
|
+
visitor: { ExportDeclaration(ctx, node) {
|
|
70
|
+
if (!isReExportDeclaration(node)) return;
|
|
71
|
+
const source = node.moduleSpecifier.getText();
|
|
72
|
+
const duplicateExport = ctx.data.exports.find((exp) => exp.isTypeOnly === node.isTypeOnly && exp.moduleSpecifier.getText() === source);
|
|
73
|
+
if (duplicateExport != null) {
|
|
74
|
+
ctx.report({
|
|
75
|
+
node,
|
|
76
|
+
message: messages$1.noDuplicateExports({ source }),
|
|
77
|
+
suggestions: buildSuggestions(duplicateExport, node)
|
|
78
|
+
});
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
ctx.data.exports.push(node);
|
|
82
|
+
} }
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
function buildSuggestions(a, b) {
|
|
86
|
+
switch (true) {
|
|
87
|
+
case ts.isNamedExports(a.exportClause) && ts.isNamedExports(b.exportClause): {
|
|
88
|
+
const aElements = a.exportClause.elements.map((el) => el.getText());
|
|
89
|
+
const bElements = b.exportClause.elements.map((el) => el.getText());
|
|
90
|
+
const parts = Array.from(new Set([...aElements, ...bElements]));
|
|
91
|
+
return [{
|
|
92
|
+
message: "Merge duplicate exports",
|
|
93
|
+
changes: [{
|
|
94
|
+
node: a,
|
|
95
|
+
newText: ""
|
|
96
|
+
}, {
|
|
97
|
+
node: b,
|
|
98
|
+
newText: `export ${a.isTypeOnly ? "type " : ""}{ ${parts.join(", ")} } from ${a.moduleSpecifier.getText()};`
|
|
99
|
+
}]
|
|
100
|
+
}];
|
|
101
|
+
}
|
|
102
|
+
default: return [];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region ../../.pkgs/eff/dist/index.js
|
|
108
|
+
/**
|
|
109
|
+
* alias for `undefined`.
|
|
110
|
+
*/
|
|
111
|
+
const unit = void 0;
|
|
112
|
+
/**
|
|
113
|
+
* Creates a function that can be used in a data-last (aka `pipe`able) or
|
|
114
|
+
* data-first style.
|
|
115
|
+
*
|
|
116
|
+
* The first parameter to `dual` is either the arity of the uncurried function
|
|
117
|
+
* or a predicate that determines if the function is being used in a data-first
|
|
118
|
+
* or data-last style.
|
|
119
|
+
*
|
|
120
|
+
* Using the arity is the most common use case, but there are some cases where
|
|
121
|
+
* you may want to use a predicate. For example, if you have a function that
|
|
122
|
+
* takes an optional argument, you can use a predicate to determine if the
|
|
123
|
+
* function is being used in a data-first or data-last style.
|
|
124
|
+
*
|
|
125
|
+
* You can pass either the arity of the uncurried function or a predicate
|
|
126
|
+
* which determines if the function is being used in a data-first or
|
|
127
|
+
* data-last style.
|
|
128
|
+
*
|
|
129
|
+
* **Example** (Using arity to determine data-first or data-last style)
|
|
130
|
+
*
|
|
131
|
+
* ```ts
|
|
132
|
+
* import { dual, pipe } from "effect/Function"
|
|
133
|
+
*
|
|
134
|
+
* const sum = dual<
|
|
135
|
+
* (that: number) => (self: number) => number,
|
|
136
|
+
* (self: number, that: number) => number
|
|
137
|
+
* >(2, (self, that) => self + that)
|
|
138
|
+
*
|
|
139
|
+
* console.log(sum(2, 3)) // 5
|
|
140
|
+
* console.log(pipe(2, sum(3))) // 5
|
|
141
|
+
* ```
|
|
142
|
+
*
|
|
143
|
+
* **Example** (Using call signatures to define the overloads)
|
|
144
|
+
*
|
|
145
|
+
* ```ts
|
|
146
|
+
* import { dual, pipe } from "effect/Function"
|
|
147
|
+
*
|
|
148
|
+
* const sum: {
|
|
149
|
+
* (that: number): (self: number) => number
|
|
150
|
+
* (self: number, that: number): number
|
|
151
|
+
* } = dual(2, (self: number, that: number): number => self + that)
|
|
152
|
+
*
|
|
153
|
+
* console.log(sum(2, 3)) // 5
|
|
154
|
+
* console.log(pipe(2, sum(3))) // 5
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* **Example** (Using a predicate to determine data-first or data-last style)
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { dual, pipe } from "effect/Function"
|
|
161
|
+
*
|
|
162
|
+
* const sum = dual<
|
|
163
|
+
* (that: number) => (self: number) => number,
|
|
164
|
+
* (self: number, that: number) => number
|
|
165
|
+
* >(
|
|
166
|
+
* (args) => args.length === 2,
|
|
167
|
+
* (self, that) => self + that
|
|
168
|
+
* )
|
|
169
|
+
*
|
|
170
|
+
* console.log(sum(2, 3)) // 5
|
|
171
|
+
* console.log(pipe(2, sum(3))) // 5
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* @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.
|
|
175
|
+
* @param body - The function to be curried.
|
|
176
|
+
* @since 1.0.0
|
|
177
|
+
*/
|
|
178
|
+
const dual = function(arity, body) {
|
|
179
|
+
if (typeof arity === "function") return function() {
|
|
180
|
+
return arity(arguments) ? body.apply(this, arguments) : ((self) => body(self, ...arguments));
|
|
181
|
+
};
|
|
182
|
+
switch (arity) {
|
|
183
|
+
case 0:
|
|
184
|
+
case 1: throw new RangeError(`Invalid arity ${arity}`);
|
|
185
|
+
case 2: return function(a, b) {
|
|
186
|
+
if (arguments.length >= 2) return body(a, b);
|
|
187
|
+
return function(self) {
|
|
188
|
+
return body(self, a);
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
case 3: return function(a, b, c) {
|
|
192
|
+
if (arguments.length >= 3) return body(a, b, c);
|
|
193
|
+
return function(self) {
|
|
194
|
+
return body(self, a, b);
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
default: return function() {
|
|
198
|
+
if (arguments.length >= arity) return body.apply(this, arguments);
|
|
199
|
+
const args = arguments;
|
|
200
|
+
return function(self) {
|
|
201
|
+
return body(self, ...args);
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* 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`.
|
|
208
|
+
* The result is obtained by first applying the `ab` function to `a` and then applying the `bc` function to the result of `ab`.
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* ```ts
|
|
212
|
+
* import * as assert from "node:assert"
|
|
213
|
+
* import { compose } from "effect/Function"
|
|
214
|
+
*
|
|
215
|
+
* const increment = (n: number) => n + 1;
|
|
216
|
+
* const square = (n: number) => n * n;
|
|
217
|
+
*
|
|
218
|
+
* assert.strictEqual(compose(increment, square)(2), 9);
|
|
219
|
+
* ```
|
|
220
|
+
*
|
|
221
|
+
* @since 1.0.0
|
|
222
|
+
*/
|
|
223
|
+
const compose = dual(2, (ab, bc) => (a) => bc(ab(a)));
|
|
224
|
+
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/rules/no-duplicate-imports.ts
|
|
227
|
+
const messages = { noDuplicateImports: (p) => `Duplicate import from module ${p.source}.` };
|
|
228
|
+
/**
|
|
229
|
+
* Rule to detect and merge duplicate `import from` statements from the same module.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
*
|
|
233
|
+
* ```ts
|
|
234
|
+
* // Incorrect
|
|
235
|
+
* import { A } from 'module';
|
|
236
|
+
* import { B } from 'module';
|
|
237
|
+
* ```
|
|
238
|
+
*
|
|
239
|
+
* ```ts
|
|
240
|
+
* // Correct
|
|
241
|
+
* import { A, B } from 'module';
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
const noDuplicateImports = defineRule(() => {
|
|
245
|
+
return {
|
|
246
|
+
name: "module/no-duplicate-imports",
|
|
247
|
+
createData() {
|
|
248
|
+
return { imports: [
|
|
249
|
+
[],
|
|
250
|
+
[],
|
|
251
|
+
[]
|
|
252
|
+
] };
|
|
253
|
+
},
|
|
254
|
+
visitor: { ImportDeclaration(ctx, node) {
|
|
255
|
+
if (node.importClause == null) return;
|
|
256
|
+
const importKind = getImportKind(node);
|
|
257
|
+
const importInfo = {
|
|
258
|
+
node,
|
|
259
|
+
source: node.moduleSpecifier.getText(),
|
|
260
|
+
kind: importKind,
|
|
261
|
+
...decodeImportClause(node.importClause)
|
|
262
|
+
};
|
|
263
|
+
const existingImports = ctx.data.imports[importKind];
|
|
264
|
+
const duplicateImport = existingImports.find((imp) => imp.source === importInfo.source);
|
|
265
|
+
if (duplicateImport != null) {
|
|
266
|
+
ctx.report({
|
|
267
|
+
node,
|
|
268
|
+
message: messages.noDuplicateImports({ source: importInfo.source }),
|
|
269
|
+
suggestions: importKind > 1 ? [] : [{
|
|
270
|
+
message: "Merge duplicate imports",
|
|
271
|
+
changes: [{
|
|
272
|
+
node,
|
|
273
|
+
newText: ""
|
|
274
|
+
}, {
|
|
275
|
+
node: duplicateImport.node,
|
|
276
|
+
newText: buildMergedImport(duplicateImport, importInfo)
|
|
277
|
+
}]
|
|
278
|
+
}]
|
|
279
|
+
});
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
existingImports.push(importInfo);
|
|
283
|
+
} }
|
|
284
|
+
};
|
|
285
|
+
});
|
|
286
|
+
function getImportKind(node) {
|
|
287
|
+
return match(node.importClause?.phaseModifier).with(P.nullish, () => 0).with(ts.SyntaxKind.TypeKeyword, () => 1).with(ts.SyntaxKind.DeferKeyword, () => 2).otherwise(() => 0);
|
|
288
|
+
}
|
|
289
|
+
function decodeImportClause(node) {
|
|
290
|
+
const { name, namedBindings } = node;
|
|
291
|
+
return {
|
|
292
|
+
defaultImport: name?.getText(),
|
|
293
|
+
namedImports: namedBindings != null && ts.isNamedImports(namedBindings) ? namedBindings.elements.map((el) => el.getText()) : [],
|
|
294
|
+
namespaceImport: namedBindings != null && ts.isNamespaceImport(namedBindings) ? namedBindings.name.getText() : unit
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
function buildMergedImport(a, b) {
|
|
298
|
+
const parts = [];
|
|
299
|
+
if (a.defaultImport != null) parts.push(a.defaultImport);
|
|
300
|
+
else if (b.defaultImport != null) parts.push(b.defaultImport);
|
|
301
|
+
if (a.namespaceImport != null) parts.push(`* as ${a.namespaceImport}`);
|
|
302
|
+
else if (b.namespaceImport != null) parts.push(`* as ${b.namespaceImport}`);
|
|
303
|
+
const namedImports = Array.from(new Set([...a.namedImports, ...b.namedImports]));
|
|
304
|
+
if (namedImports.length > 0) parts.push(`{ ${namedImports.join(", ")} }`);
|
|
305
|
+
return `${match(a.kind).with(0, () => "import").with(1, () => "import type").with(2, () => "import defer").exhaustive()} ${parts.join(", ")} from ${a.source};`;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
//#endregion
|
|
309
|
+
export { consistentNullishComparison, noDuplicateExports, noDuplicateImports };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tsl-dx",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "A tsl plugin for better JavaScript/TypeScript DX.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"tsl",
|
|
7
|
+
"tsl-dx"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": "Rel1cx<let@ik.me>",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"ts-pattern": "^5.9.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"dedent": "^1.7.1",
|
|
28
|
+
"tsdown": "^0.20.1",
|
|
29
|
+
"tsl": "^1.0.28",
|
|
30
|
+
"vitest": "^4.0.18",
|
|
31
|
+
"@local/configs": "0.0.0",
|
|
32
|
+
"@local/eff": "0.2.9"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"tsl": "^1.0.28",
|
|
36
|
+
"typescript": "*"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsdown --dts-resolve",
|
|
43
|
+
"build:docs": "typedoc",
|
|
44
|
+
"lint:publish": "pnpm publint",
|
|
45
|
+
"lint:ts": "tsl",
|
|
46
|
+
"test": "vitest"
|
|
47
|
+
}
|
|
48
|
+
}
|