@venizia/dev-configs 0.5.0-0 → 0.5.0-10
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 +18 -13
- package/dist/checks/purity/manifest.js +11 -3
- package/dist/checks/purity/manifest.js.map +1 -1
- package/dist/checks/purity/probe.js +19 -2
- package/dist/checks/purity/probe.js.map +1 -1
- package/dist/checks/split-report.js +2 -1
- package/dist/checks/split-report.js.map +1 -1
- package/dist/checks/test-controls.d.ts +9 -1
- package/dist/checks/test-controls.js +9 -3
- package/dist/checks/test-controls.js.map +1 -1
- package/dist/checks/test-dead-code.d.ts +9 -1
- package/dist/checks/test-dead-code.js +13 -2
- package/dist/checks/test-dead-code.js.map +1 -1
- package/dist/checks/test-mutate.d.ts +9 -1
- package/dist/checks/test-mutate.js +12 -6
- package/dist/checks/test-mutate.js.map +1 -1
- package/dist/cli/check.js +42 -38
- package/dist/cli/check.js.map +1 -1
- package/dist/cli/clean.js +3 -2
- package/dist/cli/clean.js.map +1 -1
- package/dist/cli/codemod/any-aliases.d.ts +1 -1
- package/dist/cli/codemod/any-aliases.js +14 -13
- package/dist/cli/codemod/any-aliases.js.map +1 -1
- package/dist/cli/codemod/async-style.d.ts +1 -1
- package/dist/cli/codemod/async-style.js +18 -17
- package/dist/cli/codemod/async-style.js.map +1 -1
- package/dist/cli/codemod/binding-keys.d.ts +32 -1
- package/dist/cli/codemod/binding-keys.js +251 -68
- package/dist/cli/codemod/binding-keys.js.map +1 -1
- package/dist/cli/codemod/class-renames.d.ts +39 -0
- package/dist/cli/codemod/class-renames.js +297 -0
- package/dist/cli/codemod/class-renames.js.map +1 -0
- package/dist/cli/codemod/connector-imports.d.ts +1 -1
- package/dist/cli/codemod/connector-imports.js +13 -12
- package/dist/cli/codemod/connector-imports.js.map +1 -1
- package/dist/cli/codemod/const-class-sets.d.ts +55 -0
- package/dist/cli/codemod/const-class-sets.js +219 -0
- package/dist/cli/codemod/const-class-sets.js.map +1 -0
- package/dist/cli/codemod/const-classes.d.ts +21 -3
- package/dist/cli/codemod/const-classes.js +151 -36
- package/dist/cli/codemod/const-classes.js.map +1 -1
- package/dist/cli/codemod/drop-redundant-casts.d.ts +6 -1
- package/dist/cli/codemod/drop-redundant-casts.js +27 -19
- package/dist/cli/codemod/drop-redundant-casts.js.map +1 -1
- package/dist/cli/codemod/env-get.d.ts +3 -3
- package/dist/cli/codemod/env-get.js +19 -18
- package/dist/cli/codemod/env-get.js.map +1 -1
- package/dist/cli/codemod/files.d.ts +4 -2
- package/dist/cli/codemod/files.js +7 -8
- package/dist/cli/codemod/files.js.map +1 -1
- package/dist/cli/codemod/function-to-arrow.d.ts +76 -0
- package/dist/cli/codemod/function-to-arrow.js +357 -0
- package/dist/cli/codemod/function-to-arrow.js.map +1 -0
- package/dist/cli/codemod/index.js +25 -4
- package/dist/cli/codemod/index.js.map +1 -1
- package/dist/cli/codemod/namespace-functions/index.d.ts +1 -1
- package/dist/cli/codemod/namespace-functions/index.js +14 -13
- package/dist/cli/codemod/namespace-functions/index.js.map +1 -1
- package/dist/cli/codemod/namespace-functions/mappings/dev-configs.js +3 -3
- package/dist/cli/codemod/namespace-functions/mappings/dev-configs.js.map +1 -1
- package/dist/cli/codemod/namespace-functions/mappings/helpers.js +1 -1
- package/dist/cli/codemod/namespace-functions/mappings/helpers.js.map +1 -1
- package/dist/cli/codemod/typed-route-context.d.ts +58 -0
- package/dist/cli/codemod/typed-route-context.js +918 -0
- package/dist/cli/codemod/typed-route-context.js.map +1 -0
- package/dist/cli/index.js +5 -4
- package/dist/cli/index.js.map +1 -1
- package/dist/common/cli-output.d.ts +16 -0
- package/dist/common/cli-output.js +49 -0
- package/dist/common/cli-output.js.map +1 -0
- package/dist/compilers/bun.js +1 -1
- package/dist/compilers/bun.js.map +1 -1
- package/dist/linters/eslint.js +1 -1
- package/dist/linters/eslint.js.map +1 -1
- package/dist/linters/house/browser-api-patterns.d.ts +43 -0
- package/dist/linters/house/browser-api-patterns.js +87 -0
- package/dist/linters/house/browser-api-patterns.js.map +1 -0
- package/dist/linters/house/common.d.ts +17 -2
- package/dist/linters/house/common.js +18 -2
- package/dist/linters/house/common.js.map +1 -1
- package/dist/linters/house/configs.js +25 -0
- package/dist/linters/house/configs.js.map +1 -1
- package/dist/linters/house/const-class-values.rule.d.ts +2 -2
- package/dist/linters/house/const-class-values.rule.js +14 -5
- package/dist/linters/house/const-class-values.rule.js.map +1 -1
- package/dist/linters/house/effect-cleanup-consistency.rule.d.ts +2 -2
- package/dist/linters/house/effect-cleanup-consistency.rule.js +1 -1
- package/dist/linters/house/effect-cleanup-consistency.rule.js.map +1 -1
- package/dist/linters/house/framework-api-patterns.d.ts +54 -0
- package/dist/linters/house/framework-api-patterns.js +62 -0
- package/dist/linters/house/framework-api-patterns.js.map +1 -0
- package/dist/linters/house/framework-exports.d.ts +62 -0
- package/dist/linters/house/framework-exports.js +325 -0
- package/dist/linters/house/framework-exports.js.map +1 -0
- package/dist/linters/house/import-support.d.ts +56 -0
- package/dist/linters/house/import-support.js +179 -0
- package/dist/linters/house/import-support.js.map +1 -0
- package/dist/linters/house/index.d.ts +12 -0
- package/dist/linters/house/index.js +15 -1
- package/dist/linters/house/index.js.map +1 -1
- package/dist/linters/house/max-params-own.rule.d.ts +2 -2
- package/dist/linters/house/max-params-own.rule.js +1 -1
- package/dist/linters/house/max-params-own.rule.js.map +1 -1
- package/dist/linters/house/no-async-hook-callback.rule.d.ts +2 -2
- package/dist/linters/house/no-async-hook-callback.rule.js +1 -1
- package/dist/linters/house/no-async-hook-callback.rule.js.map +1 -1
- package/dist/linters/house/no-await-new-chain.rule.d.ts +2 -2
- package/dist/linters/house/no-await-new-chain.rule.js +1 -1
- package/dist/linters/house/no-await-new-chain.rule.js.map +1 -1
- package/dist/linters/house/no-await-promise-chain.rule.d.ts +2 -2
- package/dist/linters/house/no-await-promise-chain.rule.js +5 -5
- package/dist/linters/house/no-await-promise-chain.rule.js.map +1 -1
- package/dist/linters/house/no-inline-await.rule.d.ts +2 -2
- package/dist/linters/house/no-inline-await.rule.js +1 -1
- package/dist/linters/house/no-inline-await.rule.js.map +1 -1
- package/dist/linters/house/no-leaked-render.rule.d.ts +2 -2
- package/dist/linters/house/no-leaked-render.rule.js +1 -1
- package/dist/linters/house/no-leaked-render.rule.js.map +1 -1
- package/dist/linters/house/no-new-error.rule.d.ts +2 -2
- package/dist/linters/house/no-new-error.rule.js +2 -2
- package/dist/linters/house/no-new-error.rule.js.map +1 -1
- package/dist/linters/house/no-parent-import.rule.d.ts +45 -0
- package/dist/linters/house/no-parent-import.rule.js +176 -0
- package/dist/linters/house/no-parent-import.rule.js.map +1 -0
- package/dist/linters/house/no-return-await.rule.d.ts +2 -2
- package/dist/linters/house/no-return-await.rule.js +1 -1
- package/dist/linters/house/no-return-await.rule.js.map +1 -1
- package/dist/linters/house/no-shadow-framework-type.rule.d.ts +24 -0
- package/dist/linters/house/no-shadow-framework-type.rule.js +100 -0
- package/dist/linters/house/no-shadow-framework-type.rule.js.map +1 -0
- package/dist/linters/house/no-silent-catch.rule.d.ts +2 -2
- package/dist/linters/house/no-silent-catch.rule.js +3 -3
- package/dist/linters/house/no-silent-catch.rule.js.map +1 -1
- package/dist/linters/house/no-unbraced-case.rule.d.ts +2 -2
- package/dist/linters/house/no-unbraced-case.rule.js +1 -1
- package/dist/linters/house/no-unbraced-case.rule.js.map +1 -1
- package/dist/linters/house/plugin.d.ts +1 -1
- package/dist/linters/house/plugin.js +18 -2
- package/dist/linters/house/plugin.js.map +1 -1
- package/dist/linters/house/prefer-framework-api.rule.d.ts +83 -0
- package/dist/linters/house/prefer-framework-api.rule.js +562 -0
- package/dist/linters/house/prefer-framework-api.rule.js.map +1 -0
- package/dist/linters/house/prefer-http-method.rule.d.ts +22 -0
- package/dist/linters/house/prefer-http-method.rule.js +182 -0
- package/dist/linters/house/prefer-http-method.rule.js.map +1 -0
- package/dist/linters/house/prefer-promise-sleep.rule.d.ts +26 -0
- package/dist/linters/house/prefer-promise-sleep.rule.js +155 -0
- package/dist/linters/house/prefer-promise-sleep.rule.js.map +1 -0
- package/dist/linters/house/prefer-tnullable.rule.d.ts +25 -0
- package/dist/linters/house/prefer-tnullable.rule.js +124 -0
- package/dist/linters/house/prefer-tnullable.rule.js.map +1 -0
- package/dist/linters/house/typed-route-context.rule.d.ts +19 -0
- package/dist/linters/house/typed-route-context.rule.js +334 -0
- package/dist/linters/house/typed-route-context.rule.js.map +1 -0
- package/dist/linters/house/zod-schema-convention.rule.d.ts +82 -0
- package/dist/linters/house/zod-schema-convention.rule.js +477 -0
- package/dist/linters/house/zod-schema-convention.rule.js.map +1 -0
- package/dist/linters/oxlint.js +11 -1
- package/dist/linters/oxlint.js.map +1 -1
- package/dist/linters/react-eslint.js.map +1 -1
- package/dist/linters/rules/naming-convention.rule.d.ts +4 -1
- package/dist/linters/rules/naming-convention.rule.js +30 -1
- package/dist/linters/rules/naming-convention.rule.js.map +1 -1
- package/dist/linters/rules/no-restricted-properties.rule.js +1 -1
- package/dist/linters/rules/no-restricted-properties.rule.js.map +1 -1
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -2
- package/dist/testing/index.js.map +1 -1
- package/dist/testing/{rejection.helper.d.ts → rejection.utility.d.ts} +4 -4
- package/dist/testing/{rejection.helper.js → rejection.utility.js} +7 -7
- package/dist/testing/rejection.utility.js.map +1 -0
- package/dist/testing/{spawn.helper.d.ts → spawn.utility.d.ts} +2 -2
- package/dist/testing/{spawn.helper.js → spawn.utility.js} +6 -6
- package/dist/testing/spawn.utility.js.map +1 -0
- package/package.json +7 -7
- package/dist/testing/rejection.helper.js.map +0 -1
- package/dist/testing/spawn.helper.js.map +0 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type ts from 'typescript';
|
|
2
|
+
type TTypeScript = typeof ts;
|
|
3
|
+
export interface IConstClassSetsResult {
|
|
4
|
+
output: string;
|
|
5
|
+
changed: boolean;
|
|
6
|
+
/** The classes of this file that moved to `extends ConstClass`. */
|
|
7
|
+
classes: Array<{
|
|
8
|
+
name: string;
|
|
9
|
+
line: number;
|
|
10
|
+
}>;
|
|
11
|
+
/** The `SCHEME_SET.has(` and `[...SCHEME_SET]` sites rewritten. */
|
|
12
|
+
calls: number;
|
|
13
|
+
/** A `SCHEME_SET` use of a converted class the codemod cannot rewrite. */
|
|
14
|
+
manual: Array<{
|
|
15
|
+
line: number;
|
|
16
|
+
reason: string;
|
|
17
|
+
}>;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Moves a standard-shape value class onto `ConstClass`, and the two ways callers read its `SCHEME_SET`.
|
|
21
|
+
* Standard shape: no base class, string `static readonly` members, a `SCHEME_SET` that lists exactly those members,
|
|
22
|
+
* either no `isValid` or one that is `return this.SCHEME_SET.has(x)`, and no other static member but methods that do
|
|
23
|
+
* not read the set. Any other member keeps the class as it is. Callers are found through the checker, so only a receiver that is such a class (or one that
|
|
24
|
+
* already extends `ConstClass`, a library class that has moved) is rewritten.
|
|
25
|
+
*/
|
|
26
|
+
export declare class ConstClassSets {
|
|
27
|
+
/** The key a converted class goes by across files: its file and its name. */
|
|
28
|
+
static keyOf(opts: {
|
|
29
|
+
fileName: string;
|
|
30
|
+
name: string;
|
|
31
|
+
}): string;
|
|
32
|
+
/** The classes of a file that have the standard shape. */
|
|
33
|
+
static standardClasses(opts: {
|
|
34
|
+
typescript: TTypeScript;
|
|
35
|
+
sourceFile: ts.SourceFile;
|
|
36
|
+
}): ts.ClassDeclaration[];
|
|
37
|
+
private static isStandard;
|
|
38
|
+
/** The member names of `new Set([this.A, this.B])`; `undefined` for any other expression. */
|
|
39
|
+
private static listedMembers;
|
|
40
|
+
/**
|
|
41
|
+
* One file: its standard classes move onto `ConstClass` (when `convertHere`), and every caller of a class in
|
|
42
|
+
* `converted` is rewritten. `converted` holds the `keyOf` of the classes of the whole run.
|
|
43
|
+
*/
|
|
44
|
+
static rewrite(opts: {
|
|
45
|
+
typescript: TTypeScript;
|
|
46
|
+
source: string;
|
|
47
|
+
sourceFile: ts.SourceFile;
|
|
48
|
+
checker: ts.TypeChecker;
|
|
49
|
+
converted: Set<string>;
|
|
50
|
+
convertHere: boolean;
|
|
51
|
+
importFrom: string;
|
|
52
|
+
}): IConstClassSetsResult;
|
|
53
|
+
}
|
|
54
|
+
export {};
|
|
55
|
+
//# sourceMappingURL=const-class-sets.d.ts.map
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConstClassSets = void 0;
|
|
4
|
+
const const_classes_1 = require("./const-classes");
|
|
5
|
+
/**
|
|
6
|
+
* Moves a standard-shape value class onto `ConstClass`, and the two ways callers read its `SCHEME_SET`.
|
|
7
|
+
* Standard shape: no base class, string `static readonly` members, a `SCHEME_SET` that lists exactly those members,
|
|
8
|
+
* either no `isValid` or one that is `return this.SCHEME_SET.has(x)`, and no other static member but methods that do
|
|
9
|
+
* not read the set. Any other member keeps the class as it is. Callers are found through the checker, so only a receiver that is such a class (or one that
|
|
10
|
+
* already extends `ConstClass`, a library class that has moved) is rewritten.
|
|
11
|
+
*/
|
|
12
|
+
class ConstClassSets {
|
|
13
|
+
/** The key a converted class goes by across files: its file and its name. */
|
|
14
|
+
static keyOf(opts) {
|
|
15
|
+
return `${opts.fileName}#${opts.name}`;
|
|
16
|
+
}
|
|
17
|
+
/** The classes of a file that have the standard shape. */
|
|
18
|
+
static standardClasses(opts) {
|
|
19
|
+
const { typescript, sourceFile } = opts;
|
|
20
|
+
return sourceFile.statements.filter((statement) => typescript.isClassDeclaration(statement) &&
|
|
21
|
+
statement.name !== undefined &&
|
|
22
|
+
statement.heritageClauses === undefined &&
|
|
23
|
+
ConstClassSets.isStandard({ typescript, node: statement }));
|
|
24
|
+
}
|
|
25
|
+
static isStandard(opts) {
|
|
26
|
+
const { typescript, node } = opts;
|
|
27
|
+
const values = [];
|
|
28
|
+
let listed;
|
|
29
|
+
for (const member of node.members) {
|
|
30
|
+
const isStatic = typescript.canHaveModifiers(member) &&
|
|
31
|
+
typescript
|
|
32
|
+
.getModifiers(member)
|
|
33
|
+
?.some((modifier) => modifier.kind === typescript.SyntaxKind.StaticKeyword) === true;
|
|
34
|
+
if (!isStatic || member.name === undefined) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const name = member.name.getText();
|
|
38
|
+
if (typescript.isMethodDeclaration(member) && name === 'isValid') {
|
|
39
|
+
const body = member.body?.getText().replace(/\s+/g, ' ') ?? '';
|
|
40
|
+
if (!/^\{ return this\.SCHEME_SET\.has\(\w+\); \}$/.test(body)) {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
// Another static method is fine while it does not read the set.
|
|
46
|
+
if (typescript.isMethodDeclaration(member) && name !== 'SCHEME_SET') {
|
|
47
|
+
if (member.getText().includes('SCHEME_SET')) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!typescript.isPropertyDeclaration(member) || member.initializer === undefined) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
const { initializer } = member;
|
|
56
|
+
if (name === 'SCHEME_SET') {
|
|
57
|
+
listed = ConstClassSets.listedMembers({ typescript, initializer });
|
|
58
|
+
if (listed === undefined) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (!typescript.isStringLiteral(initializer) &&
|
|
64
|
+
!typescript.isNoSubstitutionTemplateLiteral(initializer)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
values.push(name);
|
|
68
|
+
}
|
|
69
|
+
return (listed !== undefined &&
|
|
70
|
+
values.length > 0 &&
|
|
71
|
+
[...listed].sort().join() === [...values].sort().join());
|
|
72
|
+
}
|
|
73
|
+
/** The member names of `new Set([this.A, this.B])`; `undefined` for any other expression. */
|
|
74
|
+
static listedMembers(opts) {
|
|
75
|
+
const { typescript, initializer } = opts;
|
|
76
|
+
if (!typescript.isNewExpression(initializer) ||
|
|
77
|
+
initializer.expression.getText() !== 'Set' ||
|
|
78
|
+
initializer.arguments?.length !== 1) {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
const list = initializer.arguments[0];
|
|
82
|
+
if (list === undefined || !typescript.isArrayLiteralExpression(list)) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const names = [];
|
|
86
|
+
for (const element of list.elements) {
|
|
87
|
+
if (!typescript.isPropertyAccessExpression(element) ||
|
|
88
|
+
element.expression.kind !== typescript.SyntaxKind.ThisKeyword) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
names.push(element.name.text);
|
|
92
|
+
}
|
|
93
|
+
return names;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* One file: its standard classes move onto `ConstClass` (when `convertHere`), and every caller of a class in
|
|
97
|
+
* `converted` is rewritten. `converted` holds the `keyOf` of the classes of the whole run.
|
|
98
|
+
*/
|
|
99
|
+
static rewrite(opts) {
|
|
100
|
+
const { typescript, source, sourceFile, checker, converted, importFrom } = opts;
|
|
101
|
+
const edits = [];
|
|
102
|
+
const classes = [];
|
|
103
|
+
const manual = [];
|
|
104
|
+
let calls = 0;
|
|
105
|
+
const lineOf = (node) => sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
|
|
106
|
+
if (opts.convertHere) {
|
|
107
|
+
for (const node of ConstClassSets.standardClasses({ typescript, sourceFile })) {
|
|
108
|
+
if (node.name === undefined) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
classes.push({ name: node.name.text, line: lineOf(node) });
|
|
112
|
+
edits.push({ start: node.name.end, end: node.name.end, text: ' extends ConstClass' });
|
|
113
|
+
for (const member of node.members) {
|
|
114
|
+
const name = member.name?.getText();
|
|
115
|
+
if (name === 'SCHEME_SET' || name === 'isValid') {
|
|
116
|
+
// From the end of the previous member, so the member's comment and the blank line above it go with it.
|
|
117
|
+
edits.push({ start: member.getFullStart(), end: member.end, text: '' });
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const resolve = (expression) => {
|
|
123
|
+
const symbol = checker.getSymbolAtLocation(expression);
|
|
124
|
+
return symbol !== undefined && symbol.flags & typescript.SymbolFlags.Alias
|
|
125
|
+
? checker.getAliasedSymbol(symbol)
|
|
126
|
+
: symbol;
|
|
127
|
+
};
|
|
128
|
+
/** The class extends `ConstClass`, directly or through the classes it extends. */
|
|
129
|
+
const extendsConstClass = (declaration, depth = 0) => depth < 8 &&
|
|
130
|
+
(declaration.heritageClauses?.some((clause) => clause.types.some((type) => {
|
|
131
|
+
if (type.expression.getText() === 'ConstClass') {
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
const base = resolve(type.expression)?.declarations?.find(typescript.isClassDeclaration);
|
|
135
|
+
return base !== undefined && extendsConstClass(base, depth + 1);
|
|
136
|
+
})) ??
|
|
137
|
+
false);
|
|
138
|
+
const isConverted = (expression) => {
|
|
139
|
+
const symbol = resolve(expression);
|
|
140
|
+
const declaration = symbol?.declarations?.find(typescript.isClassDeclaration);
|
|
141
|
+
if (declaration?.name === undefined) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
// Converted in this run, or already on ConstClass (a class from a library that has moved).
|
|
145
|
+
return (converted.has(ConstClassSets.keyOf({
|
|
146
|
+
fileName: declaration.getSourceFile().fileName,
|
|
147
|
+
name: declaration.name.text,
|
|
148
|
+
})) || extendsConstClass(declaration));
|
|
149
|
+
};
|
|
150
|
+
const removed = (node) => edits.some((edit) => edit.text === '' && node.getStart(sourceFile) >= edit.start && node.end <= edit.end);
|
|
151
|
+
const walk = (node) => {
|
|
152
|
+
if (typescript.isPropertyAccessExpression(node) &&
|
|
153
|
+
node.name.text === 'SCHEME_SET' &&
|
|
154
|
+
!removed(node) &&
|
|
155
|
+
isConverted(node.expression)) {
|
|
156
|
+
const receiver = node.expression.getText(sourceFile);
|
|
157
|
+
const parent = node.parent;
|
|
158
|
+
if (typescript.isPropertyAccessExpression(parent) &&
|
|
159
|
+
parent.expression === node &&
|
|
160
|
+
parent.name.text === 'has' &&
|
|
161
|
+
typescript.isCallExpression(parent.parent) &&
|
|
162
|
+
parent.parent.expression === parent &&
|
|
163
|
+
parent.parent.arguments.length === 1) {
|
|
164
|
+
edits.push({
|
|
165
|
+
start: parent.getStart(sourceFile),
|
|
166
|
+
end: parent.end,
|
|
167
|
+
text: `${receiver}.isValid`,
|
|
168
|
+
});
|
|
169
|
+
calls += 1;
|
|
170
|
+
}
|
|
171
|
+
else if (typescript.isSpreadElement(parent) &&
|
|
172
|
+
typescript.isArrayLiteralExpression(parent.parent) &&
|
|
173
|
+
parent.parent.elements.length === 1) {
|
|
174
|
+
edits.push({
|
|
175
|
+
start: parent.parent.getStart(sourceFile),
|
|
176
|
+
end: parent.parent.end,
|
|
177
|
+
text: `${receiver}.values()`,
|
|
178
|
+
});
|
|
179
|
+
calls += 1;
|
|
180
|
+
}
|
|
181
|
+
else if (typescript.isSpreadElement(parent)) {
|
|
182
|
+
edits.push({
|
|
183
|
+
start: parent.getStart(sourceFile),
|
|
184
|
+
end: parent.end,
|
|
185
|
+
text: `...${receiver}.values()`,
|
|
186
|
+
});
|
|
187
|
+
calls += 1;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
manual.push({
|
|
191
|
+
line: lineOf(node),
|
|
192
|
+
reason: `${receiver}.SCHEME_SET is gone: use ${receiver}.isValid(value) or ${receiver}.values()`,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
typescript.forEachChild(node, walk);
|
|
198
|
+
};
|
|
199
|
+
walk(sourceFile);
|
|
200
|
+
if (classes.length > 0) {
|
|
201
|
+
const importEdit = const_classes_1.ConstClasses.planImports({
|
|
202
|
+
typescript,
|
|
203
|
+
sourceFile,
|
|
204
|
+
importFrom,
|
|
205
|
+
names: [{ name: 'ConstClass', isType: false }],
|
|
206
|
+
});
|
|
207
|
+
if (importEdit) {
|
|
208
|
+
edits.push(importEdit);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
let output = source;
|
|
212
|
+
for (const edit of edits.sort((a, b) => b.start - a.start || b.end - a.end)) {
|
|
213
|
+
output = output.slice(0, edit.start) + edit.text + output.slice(edit.end);
|
|
214
|
+
}
|
|
215
|
+
return { output, changed: output !== source, classes, calls, manual };
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
exports.ConstClassSets = ConstClassSets;
|
|
219
|
+
//# sourceMappingURL=const-class-sets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"const-class-sets.js","sourceRoot":"","sources":["../../../src/cli/codemod/const-class-sets.ts"],"names":[],"mappings":";;;AACA,mDAA+C;AAqB/C;;;;;;GAMG;AACH,MAAa,cAAc;IACzB,6EAA6E;IAC7E,MAAM,CAAC,KAAK,CAAC,IAAwC;QACnD,OAAO,GAAG,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC;IACzC,CAAC;IAED,0DAA0D;IAC1D,MAAM,CAAC,eAAe,CAAC,IAGtB;QACC,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC;QAExC,OAAO,UAAU,CAAC,UAAU,CAAC,MAAM,CACjC,CAAC,SAAS,EAAoC,EAAE,CAC9C,UAAU,CAAC,kBAAkB,CAAC,SAAS,CAAC;YACxC,SAAS,CAAC,IAAI,KAAK,SAAS;YAC5B,SAAS,CAAC,eAAe,KAAK,SAAS;YACvC,cAAc,CAAC,UAAU,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAC7D,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,UAAU,CAAC,IAA4D;QACpF,MAAM,EAAE,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,GAAG,CAAC,EAAE,CAAC;YAC5E,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC5E,CAAC;QAED,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IACxE,CAAC;CACF;AA3SD,wCA2SC","sourcesContent":["import type ts from 'typescript';\nimport { ConstClasses } from './const-classes';\n\ntype TTypeScript = typeof ts;\n\ninterface IEdit {\n start: number;\n end: number;\n text: string;\n}\n\nexport interface IConstClassSetsResult {\n output: string;\n changed: boolean;\n /** The classes of this file that moved to `extends ConstClass`. */\n classes: Array<{ name: string; line: number }>;\n /** The `SCHEME_SET.has(` and `[...SCHEME_SET]` sites rewritten. */\n calls: number;\n /** A `SCHEME_SET` use of a converted class the codemod cannot rewrite. */\n manual: Array<{ line: number; reason: string }>;\n}\n\n/**\n * Moves a standard-shape value class onto `ConstClass`, and the two ways callers read its `SCHEME_SET`.\n * Standard shape: no base class, string `static readonly` members, a `SCHEME_SET` that lists exactly those members,\n * either no `isValid` or one that is `return this.SCHEME_SET.has(x)`, and no other static member but methods that do\n * not read the set. Any other member keeps the class as it is. Callers are found through the checker, so only a receiver that is such a class (or one that\n * already extends `ConstClass`, a library class that has moved) is rewritten.\n */\nexport class ConstClassSets {\n /** The key a converted class goes by across files: its file and its name. */\n static keyOf(opts: { fileName: string; name: string }): string {\n return `${opts.fileName}#${opts.name}`;\n }\n\n /** The classes of a file that have the standard shape. */\n static standardClasses(opts: {\n typescript: TTypeScript;\n sourceFile: ts.SourceFile;\n }): ts.ClassDeclaration[] {\n const { typescript, sourceFile } = opts;\n\n return sourceFile.statements.filter(\n (statement): statement is ts.ClassDeclaration =>\n typescript.isClassDeclaration(statement) &&\n statement.name !== undefined &&\n statement.heritageClauses === undefined &&\n ConstClassSets.isStandard({ typescript, node: statement }),\n );\n }\n\n private static isStandard(opts: { typescript: TTypeScript; node: ts.ClassDeclaration }): boolean {\n const { typescript, node } = opts;\n const values: string[] = [];\n let listed: string[] | undefined;\n\n for (const member of node.members) {\n const isStatic =\n typescript.canHaveModifiers(member) &&\n typescript\n .getModifiers(member)\n ?.some((modifier) => modifier.kind === typescript.SyntaxKind.StaticKeyword) === true;\n\n if (!isStatic || member.name === undefined) {\n return false;\n }\n\n const name = member.name.getText();\n\n if (typescript.isMethodDeclaration(member) && name === 'isValid') {\n const body = member.body?.getText().replace(/\\s+/g, ' ') ?? '';\n\n if (!/^\\{ return this\\.SCHEME_SET\\.has\\(\\w+\\); \\}$/.test(body)) {\n return false;\n }\n\n continue;\n }\n\n // Another static method is fine while it does not read the set.\n if (typescript.isMethodDeclaration(member) && name !== 'SCHEME_SET') {\n if (member.getText().includes('SCHEME_SET')) {\n return false;\n }\n\n continue;\n }\n\n if (!typescript.isPropertyDeclaration(member) || member.initializer === undefined) {\n return false;\n }\n\n const { initializer } = member;\n\n if (name === 'SCHEME_SET') {\n listed = ConstClassSets.listedMembers({ typescript, initializer });\n\n if (listed === undefined) {\n return false;\n }\n\n continue;\n }\n\n if (\n !typescript.isStringLiteral(initializer) &&\n !typescript.isNoSubstitutionTemplateLiteral(initializer)\n ) {\n return false;\n }\n\n values.push(name);\n }\n\n return (\n listed !== undefined &&\n values.length > 0 &&\n [...listed].sort().join() === [...values].sort().join()\n );\n }\n\n /** The member names of `new Set([this.A, this.B])`; `undefined` for any other expression. */\n private static listedMembers(opts: {\n typescript: TTypeScript;\n initializer: ts.Expression;\n }): string[] | undefined {\n const { typescript, initializer } = opts;\n\n if (\n !typescript.isNewExpression(initializer) ||\n initializer.expression.getText() !== 'Set' ||\n initializer.arguments?.length !== 1\n ) {\n return undefined;\n }\n\n const list = initializer.arguments[0];\n\n if (list === undefined || !typescript.isArrayLiteralExpression(list)) {\n return undefined;\n }\n\n const names: string[] = [];\n\n for (const element of list.elements) {\n if (\n !typescript.isPropertyAccessExpression(element) ||\n element.expression.kind !== typescript.SyntaxKind.ThisKeyword\n ) {\n return undefined;\n }\n\n names.push(element.name.text);\n }\n\n return names;\n }\n\n /**\n * One file: its standard classes move onto `ConstClass` (when `convertHere`), and every caller of a class in\n * `converted` is rewritten. `converted` holds the `keyOf` of the classes of the whole run.\n */\n static rewrite(opts: {\n typescript: TTypeScript;\n source: string;\n sourceFile: ts.SourceFile;\n checker: ts.TypeChecker;\n converted: Set<string>;\n convertHere: boolean;\n importFrom: string;\n }): IConstClassSetsResult {\n const { typescript, source, sourceFile, checker, converted, importFrom } = opts;\n const edits: IEdit[] = [];\n const classes: IConstClassSetsResult['classes'] = [];\n const manual: IConstClassSetsResult['manual'] = [];\n let calls = 0;\n\n const lineOf = (node: ts.Node) =>\n sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;\n\n if (opts.convertHere) {\n for (const node of ConstClassSets.standardClasses({ typescript, sourceFile })) {\n if (node.name === undefined) {\n continue;\n }\n\n classes.push({ name: node.name.text, line: lineOf(node) });\n edits.push({ start: node.name.end, end: node.name.end, text: ' extends ConstClass' });\n\n for (const member of node.members) {\n const name = member.name?.getText();\n\n if (name === 'SCHEME_SET' || name === 'isValid') {\n // From the end of the previous member, so the member's comment and the blank line above it go with it.\n edits.push({ start: member.getFullStart(), end: member.end, text: '' });\n }\n }\n }\n }\n\n const resolve = (expression: ts.Node): ts.Symbol | undefined => {\n const symbol = checker.getSymbolAtLocation(expression);\n\n return symbol !== undefined && symbol.flags & typescript.SymbolFlags.Alias\n ? checker.getAliasedSymbol(symbol)\n : symbol;\n };\n\n /** The class extends `ConstClass`, directly or through the classes it extends. */\n const extendsConstClass = (declaration: ts.ClassDeclaration, depth = 0): boolean =>\n depth < 8 &&\n (declaration.heritageClauses?.some((clause) =>\n clause.types.some((type) => {\n if (type.expression.getText() === 'ConstClass') {\n return true;\n }\n\n const base = resolve(type.expression)?.declarations?.find(typescript.isClassDeclaration);\n\n return base !== undefined && extendsConstClass(base, depth + 1);\n }),\n ) ??\n false);\n\n const isConverted = (expression: ts.Expression): boolean => {\n const symbol = resolve(expression);\n\n const declaration = symbol?.declarations?.find(typescript.isClassDeclaration);\n\n if (declaration?.name === undefined) {\n return false;\n }\n\n // Converted in this run, or already on ConstClass (a class from a library that has moved).\n return (\n converted.has(\n ConstClassSets.keyOf({\n fileName: declaration.getSourceFile().fileName,\n name: declaration.name.text,\n }),\n ) || extendsConstClass(declaration)\n );\n };\n\n const removed = (node: ts.Node) =>\n edits.some(\n (edit) =>\n edit.text === '' && node.getStart(sourceFile) >= edit.start && node.end <= edit.end,\n );\n\n const walk = (node: ts.Node): void => {\n if (\n typescript.isPropertyAccessExpression(node) &&\n node.name.text === 'SCHEME_SET' &&\n !removed(node) &&\n isConverted(node.expression)\n ) {\n const receiver = node.expression.getText(sourceFile);\n const parent = node.parent;\n\n if (\n typescript.isPropertyAccessExpression(parent) &&\n parent.expression === node &&\n parent.name.text === 'has' &&\n typescript.isCallExpression(parent.parent) &&\n parent.parent.expression === parent &&\n parent.parent.arguments.length === 1\n ) {\n edits.push({\n start: parent.getStart(sourceFile),\n end: parent.end,\n text: `${receiver}.isValid`,\n });\n calls += 1;\n } else if (\n typescript.isSpreadElement(parent) &&\n typescript.isArrayLiteralExpression(parent.parent) &&\n parent.parent.elements.length === 1\n ) {\n edits.push({\n start: parent.parent.getStart(sourceFile),\n end: parent.parent.end,\n text: `${receiver}.values()`,\n });\n calls += 1;\n } else if (typescript.isSpreadElement(parent)) {\n edits.push({\n start: parent.getStart(sourceFile),\n end: parent.end,\n text: `...${receiver}.values()`,\n });\n calls += 1;\n } else {\n manual.push({\n line: lineOf(node),\n reason: `${receiver}.SCHEME_SET is gone: use ${receiver}.isValid(value) or ${receiver}.values()`,\n });\n }\n\n return;\n }\n\n typescript.forEachChild(node, walk);\n };\n\n walk(sourceFile);\n\n if (classes.length > 0) {\n const importEdit = ConstClasses.planImports({\n typescript,\n sourceFile,\n importFrom,\n names: [{ name: 'ConstClass', isType: false }],\n });\n\n if (importEdit) {\n edits.push(importEdit);\n }\n }\n\n let output = source;\n\n for (const edit of edits.sort((a, b) => b.start - a.start || b.end - a.end)) {\n output = output.slice(0, edit.start) + edit.text + output.slice(edit.end);\n }\n\n return { output, changed: output !== source, classes, calls, manual };\n }\n}\n"]}
|
|
@@ -37,6 +37,11 @@ export interface IConstClassResult {
|
|
|
37
37
|
proposals: IConstClassProposal[];
|
|
38
38
|
untouched: IConstClassUntouched[];
|
|
39
39
|
}
|
|
40
|
+
interface IEdit {
|
|
41
|
+
start: number;
|
|
42
|
+
end: number;
|
|
43
|
+
text: string;
|
|
44
|
+
}
|
|
40
45
|
/**
|
|
41
46
|
* Turns the string-literal unions of exported declarations into const classes: an exported type
|
|
42
47
|
* alias, and a property of an exported interface or object type. The values stay the same, so every
|
|
@@ -65,16 +70,29 @@ export declare class ConstClasses {
|
|
|
65
70
|
/** Reports every string-literal union of an exported declaration the codemod did not handle: it cannot name those. */
|
|
66
71
|
private static findStrays;
|
|
67
72
|
private static isKeySet;
|
|
68
|
-
/** The edit that makes `
|
|
69
|
-
|
|
73
|
+
/** The edit that makes `names` available: nothing for the ones the file imports, a specifier on an import from the same module, else a new import line. */
|
|
74
|
+
static planImports(opts: {
|
|
75
|
+
typescript: TTypeScript;
|
|
76
|
+
sourceFile: ts.SourceFile;
|
|
77
|
+
importFrom: string;
|
|
78
|
+
names: Array<{
|
|
79
|
+
name: string;
|
|
80
|
+
isType: boolean;
|
|
81
|
+
}>;
|
|
82
|
+
}): IEdit | undefined;
|
|
70
83
|
}
|
|
71
84
|
/** `devz codemod const-classes [paths...] [--write | --check]`. */
|
|
72
85
|
export declare class ConstClassesCommand {
|
|
73
|
-
static readonly USAGE = "Usage: devz codemod const-classes [paths...] [--write | --check] [--map <file>] [--emit-map <file>] [--import-from <module>] [--include-tests]\n\nTurns a string-literal union in an exported declaration into a const class (
|
|
86
|
+
static readonly USAGE = "Usage: devz codemod const-classes [paths...] [--write | --check] [--map <file>] [--emit-map <file>] [--import-from <module>] [--include-tests]\n\nTurns a string-literal union in an exported declaration into a const class (it extends ConstClass) plus\n`TConstValue<typeof X>`, the shape the @venizia/const-class-values lint rule asks for. It handles an\nexported type alias and a property of an exported interface or object type. The values stay the same,\nso every existing literal still type-checks and no call site changes.\n\nIt has to choose names: the class is the type name without its leading T, in the plural; a member is\nthe value in upper snake case. The dry run prints every proposal.\n\n --write write the changed files (without it, nothing is written)\n --check dry run that exits 1 while a union can be converted or is left untouched (for CI)\n --all with --write and no path: rewrite every source file under the working directory (--write needs a path or --all)\n --map <file> a JSON file of overrides, keyed by the id the dry run prints\n --emit-map <file> write the proposals as a mapping file to edit, then pass it with --map\n --import-from <m> where TConstValue is imported from (default @venizia/ignis-helpers/common)\n --include-tests also convert test files (skipped by default)\n --help print this text and exit\n\nA mapping entry: { \"src/a.ts#TMode\": { \"class\": \"Modes\", \"members\": { \"read-only\": \"READ_ONLY\" } } },\nor { \"skip\": true }. A union in a parameter, a return type, a generic or a nested type is reported as\nleft untouched: it has no name, so write that class by hand. Key sets (Pick, Omit, Extract, Exclude,\nRecord, mapped types) are not enumerable values and are never touched.";
|
|
74
87
|
static run(opts: {
|
|
75
88
|
root: string;
|
|
76
89
|
args: string[];
|
|
77
90
|
}): Promise<number>;
|
|
91
|
+
/**
|
|
92
|
+
* Second phase: a value class with a `SCHEME_SET` and a standard `isValid` extends `ConstClass`, and its callers
|
|
93
|
+
* read `isValid` and `values()`. Needs the project's checker, so it reads the program once the first phase is written.
|
|
94
|
+
*/
|
|
95
|
+
private static moveToConstClass;
|
|
78
96
|
}
|
|
79
97
|
export {};
|
|
80
98
|
//# sourceMappingURL=const-classes.d.ts.map
|