wrec 0.40.3 → 0.42.1
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 +9 -15
- package/dist/{wrec-DHGadgxK.js → wrec-ClOIAA9J.js} +291 -222
- package/dist/wrec-ssr.es.js +7 -7
- package/dist/wrec.es.js +1 -1
- package/package.json +39 -36
- package/scripts/ast-utils.js +20 -28
- package/scripts/declare.js +41 -62
- package/scripts/lint.js +671 -927
- package/scripts/scaffold.js +11 -13
- package/scripts/used-by.js +76 -119
- package/dist/wrec-ssr.d.ts +0 -96
- package/dist/wrec.d.ts +0 -96
package/dist/wrec-ssr.es.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./wrec-
|
|
1
|
+
import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./wrec-ClOIAA9J.js";
|
|
2
2
|
//#region \0rolldown/runtime.js
|
|
3
3
|
var u = Object.defineProperty, d = Object.getOwnPropertyDescriptor, f = Object.getOwnPropertyNames, p = Object.prototype.hasOwnProperty, m = (e, t) => () => (e && (t = e(e = 0)), t), h = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), g = (e, t) => {
|
|
4
4
|
let n = {};
|
|
@@ -4674,7 +4674,7 @@ var u = Object.defineProperty, d = Object.getOwnPropertyDescriptor, f = Object.g
|
|
|
4674
4674
|
for (var t = 1; t < e.length; t++) e[t][0] += e[t - 1][0] + 1;
|
|
4675
4675
|
return e;
|
|
4676
4676
|
}
|
|
4677
|
-
e.default = new Map(/*
|
|
4677
|
+
e.default = new Map(/* #__PURE__ */ t([
|
|
4678
4678
|
[9, "	"],
|
|
4679
4679
|
[0, "
"],
|
|
4680
4680
|
[22, "!"],
|
|
@@ -5467,11 +5467,11 @@ var u = Object.defineProperty, d = Object.getOwnPropertyDescriptor, f = Object.g
|
|
|
5467
5467
|
}],
|
|
5468
5468
|
[0, {
|
|
5469
5469
|
v: "≪",
|
|
5470
|
-
n: new Map(/*
|
|
5470
|
+
n: new Map(/* #__PURE__ */ t([[824, "≪̸"], [7577, "≪⃒"]]))
|
|
5471
5471
|
}],
|
|
5472
5472
|
[0, {
|
|
5473
5473
|
v: "≫",
|
|
5474
|
-
n: new Map(/*
|
|
5474
|
+
n: new Map(/* #__PURE__ */ t([[824, "≫̸"], [7577, "≫⃒"]]))
|
|
5475
5475
|
}],
|
|
5476
5476
|
[0, "≬"],
|
|
5477
5477
|
[0, "≭"],
|
|
@@ -6233,7 +6233,7 @@ var u = Object.defineProperty, d = Object.getOwnPropertyDescriptor, f = Object.g
|
|
|
6233
6233
|
n: 8421,
|
|
6234
6234
|
o: "⫽⃥"
|
|
6235
6235
|
}],
|
|
6236
|
-
[44343, { n: new Map(/*
|
|
6236
|
+
[44343, { n: new Map(/* #__PURE__ */ t([
|
|
6237
6237
|
[56476, "𝒜"],
|
|
6238
6238
|
[1, "𝒞"],
|
|
6239
6239
|
[0, "𝒟"],
|
|
@@ -7129,7 +7129,7 @@ var u = Object.defineProperty, d = Object.getOwnPropertyDescriptor, f = Object.g
|
|
|
7129
7129
|
})(L ||= {});
|
|
7130
7130
|
}));
|
|
7131
7131
|
//#endregion
|
|
7132
|
-
//#region node_modules/css-what/lib/es/parse.js
|
|
7132
|
+
//#region node_modules/.pnpm/css-what@6.2.2/node_modules/css-what/lib/es/parse.js
|
|
7133
7133
|
function ue(e) {
|
|
7134
7134
|
switch (e.type) {
|
|
7135
7135
|
case I.Adjacent:
|
|
@@ -7352,7 +7352,7 @@ var U, pe, me, he, ge, _e = m((() => {
|
|
|
7352
7352
|
]), ge = new Set(["contains", "icontains"]);
|
|
7353
7353
|
}));
|
|
7354
7354
|
//#endregion
|
|
7355
|
-
//#region node_modules/css-what/lib/es/stringify.js
|
|
7355
|
+
//#region node_modules/.pnpm/css-what@6.2.2/node_modules/css-what/lib/es/stringify.js
|
|
7356
7356
|
function ve(e) {
|
|
7357
7357
|
return e.map((e) => e.map(ye).join("")).join(", ");
|
|
7358
7358
|
}
|
package/dist/wrec.es.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { i as e, n as t, r as n, t as r, u as i } from "./wrec-
|
|
1
|
+
import { i as e, n as t, r as n, t as r, u as i } from "./wrec-ClOIAA9J.js";
|
|
2
2
|
export { r as Wrec, i as WrecState, t as createElement, n as css, e as html };
|
package/package.json
CHANGED
|
@@ -1,21 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wrec",
|
|
3
|
+
"version": "0.42.1",
|
|
3
4
|
"description": "a library that greatly simplifies building web components",
|
|
4
|
-
"
|
|
5
|
-
|
|
5
|
+
"keywords": [
|
|
6
|
+
"component",
|
|
7
|
+
"web"
|
|
8
|
+
],
|
|
6
9
|
"license": "MIT",
|
|
10
|
+
"author": "R. Mark Volkmann",
|
|
7
11
|
"repository": {
|
|
8
12
|
"type": "git",
|
|
9
13
|
"url": "git+https://github.com/mvolkmann/wrec.git"
|
|
10
14
|
},
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
15
|
+
"bin": {
|
|
16
|
+
"wrec-declare": "./scripts/declare.js",
|
|
17
|
+
"wrec-lint": "./scripts/lint.js",
|
|
18
|
+
"wrec-scaffold": "./scripts/scaffold.js",
|
|
19
|
+
"wrec-usedby": "./scripts/used-by.js"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"scripts/ast-utils.js",
|
|
24
|
+
"scripts/declare.js",
|
|
25
|
+
"scripts/lint.js",
|
|
26
|
+
"scripts/scaffold.js",
|
|
27
|
+
"scripts/template.ts",
|
|
28
|
+
"scripts/used-by.js",
|
|
29
|
+
"README.md"
|
|
14
30
|
],
|
|
15
31
|
"type": "module",
|
|
16
32
|
"module": "./dist/wrec.es.js",
|
|
17
33
|
"types": "./dist/wrec.d.ts",
|
|
18
|
-
"moduleResolution": "bundler",
|
|
19
34
|
"exports": {
|
|
20
35
|
".": {
|
|
21
36
|
"types": "./dist/wrec.d.ts",
|
|
@@ -28,44 +43,32 @@
|
|
|
28
43
|
"import": "./dist/wrec-ssr.es.js"
|
|
29
44
|
}
|
|
30
45
|
},
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"wrec-usedby": "./scripts/used-by.js"
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"node-html-parser": "^7.1.0",
|
|
48
|
+
"typescript": "^6.0.3",
|
|
49
|
+
"xss": "^1.0.15"
|
|
36
50
|
},
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@playwright/test": "^1.60.0",
|
|
53
|
+
"@types/node": "^25.9.2",
|
|
54
|
+
"get-port": "^7.2.0",
|
|
55
|
+
"jsdom": "^29.1.1",
|
|
56
|
+
"oxfmt": "^0.54.0",
|
|
57
|
+
"oxlint": "^1.69.0",
|
|
58
|
+
"vite": "^8.0.16",
|
|
59
|
+
"vite-plugin-dts": "^5.0.2",
|
|
60
|
+
"vitest": "^4.1.8"
|
|
61
|
+
},
|
|
62
|
+
"moduleResolution": "bundler",
|
|
47
63
|
"scripts": {
|
|
48
64
|
"dev": "vite",
|
|
49
65
|
"build": "vite build",
|
|
50
66
|
"clean": "rm -rf dist",
|
|
67
|
+
"format": "oxfmt",
|
|
51
68
|
"lint": "oxlint",
|
|
52
69
|
"preview": "vite preview",
|
|
53
70
|
"test": "node port-check.js && playwright test",
|
|
54
71
|
"testui": "playwright test --ui",
|
|
55
72
|
"unittest": "vitest run"
|
|
56
|
-
},
|
|
57
|
-
"devDependencies": {
|
|
58
|
-
"@playwright/test": "^1.59.1",
|
|
59
|
-
"@types/node": "^25.5.2",
|
|
60
|
-
"get-port": "^7.2.0",
|
|
61
|
-
"jsdom": "^29.0.2",
|
|
62
|
-
"oxlint": "^1.58.0",
|
|
63
|
-
"vite": "^8.0.3",
|
|
64
|
-
"vite-plugin-dts": "^4.5.4"
|
|
65
|
-
},
|
|
66
|
-
"dependencies": {
|
|
67
|
-
"node-html-parser": "^7.1.0",
|
|
68
|
-
"typescript": "^6.0.2",
|
|
69
|
-
"xss": "^1.0.15"
|
|
70
73
|
}
|
|
71
|
-
}
|
|
74
|
+
}
|
package/scripts/ast-utils.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import ts from
|
|
1
|
+
import ts from "typescript";
|
|
2
2
|
|
|
3
3
|
// Finds all classes in a source file that extend Wrec.
|
|
4
4
|
export function collectWrecClasses(sourceFile) {
|
|
5
|
-
const {names: wrecNames} = getWrecImportInfo(sourceFile);
|
|
5
|
+
const { names: wrecNames } = getWrecImportInfo(sourceFile);
|
|
6
6
|
const classes = [];
|
|
7
7
|
|
|
8
8
|
function visit(node) {
|
|
9
|
-
if (
|
|
10
|
-
ts.isClassDeclaration(node) &&
|
|
11
|
-
node.name &&
|
|
12
|
-
extendsWrec(node, wrecNames)
|
|
13
|
-
) {
|
|
9
|
+
if (ts.isClassDeclaration(node) && node.name && extendsWrec(node, wrecNames)) {
|
|
14
10
|
classes.push(node);
|
|
15
11
|
}
|
|
16
12
|
ts.forEachChild(node, visit);
|
|
@@ -24,29 +20,27 @@ export function collectWrecClasses(sourceFile) {
|
|
|
24
20
|
export function extendsWrec(classNode, wrecNames) {
|
|
25
21
|
return Boolean(
|
|
26
22
|
classNode.heritageClauses?.some(
|
|
27
|
-
clause =>
|
|
23
|
+
(clause) =>
|
|
28
24
|
clause.token === ts.SyntaxKind.ExtendsKeyword &&
|
|
29
25
|
clause.types.some(
|
|
30
|
-
type =>
|
|
26
|
+
(type) =>
|
|
31
27
|
ts.isExpressionWithTypeArguments(type) &&
|
|
32
28
|
ts.isIdentifier(type.expression) &&
|
|
33
|
-
wrecNames.has(type.expression.text)
|
|
34
|
-
)
|
|
35
|
-
)
|
|
29
|
+
wrecNames.has(type.expression.text),
|
|
30
|
+
),
|
|
31
|
+
),
|
|
36
32
|
);
|
|
37
33
|
}
|
|
38
34
|
|
|
39
35
|
// Gets a plain string member name when one can be statically determined.
|
|
40
36
|
export function getMemberName(node) {
|
|
41
|
-
const {name} = node;
|
|
37
|
+
const { name } = node;
|
|
42
38
|
return name ? (getNameText(name) ?? undefined) : undefined;
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
// Gets the text value of an AST name node.
|
|
46
42
|
export function getNameText(name) {
|
|
47
|
-
return ts.isIdentifier(name) ||
|
|
48
|
-
ts.isStringLiteral(name) ||
|
|
49
|
-
ts.isPrivateIdentifier(name)
|
|
43
|
+
return ts.isIdentifier(name) || ts.isStringLiteral(name) || ts.isPrivateIdentifier(name)
|
|
50
44
|
? name.text
|
|
51
45
|
: null;
|
|
52
46
|
}
|
|
@@ -55,13 +49,13 @@ export function getNameText(name) {
|
|
|
55
49
|
export function getPropertyAssignmentNames(objectLiteral) {
|
|
56
50
|
return objectLiteral.properties
|
|
57
51
|
.filter(ts.isPropertyAssignment)
|
|
58
|
-
.map(property => getMemberName(property))
|
|
59
|
-
.filter(name => name !== undefined);
|
|
52
|
+
.map((property) => getMemberName(property))
|
|
53
|
+
.filter((name) => name !== undefined);
|
|
60
54
|
}
|
|
61
55
|
|
|
62
56
|
// Collects imported Wrec class names and the quote style used for those imports.
|
|
63
57
|
export function getWrecImportInfo(sourceFile) {
|
|
64
|
-
const names = new Set([
|
|
58
|
+
const names = new Set(["Wrec"]);
|
|
65
59
|
let quote = "'";
|
|
66
60
|
|
|
67
61
|
for (const statement of sourceFile.statements) {
|
|
@@ -75,10 +69,10 @@ export function getWrecImportInfo(sourceFile) {
|
|
|
75
69
|
|
|
76
70
|
const moduleName = statement.moduleSpecifier.text;
|
|
77
71
|
const isWrecModule =
|
|
78
|
-
moduleName ===
|
|
79
|
-
moduleName.endsWith(
|
|
80
|
-
moduleName.endsWith(
|
|
81
|
-
moduleName.endsWith(
|
|
72
|
+
moduleName === "wrec" ||
|
|
73
|
+
moduleName.endsWith("/wrec") ||
|
|
74
|
+
moduleName.endsWith("/wrec.js") ||
|
|
75
|
+
moduleName.endsWith("/wrec.ts");
|
|
82
76
|
if (!isWrecModule) continue;
|
|
83
77
|
|
|
84
78
|
const namedBindings = statement.importClause.namedBindings;
|
|
@@ -86,7 +80,7 @@ export function getWrecImportInfo(sourceFile) {
|
|
|
86
80
|
|
|
87
81
|
for (const element of namedBindings.elements) {
|
|
88
82
|
const importedName = element.propertyName?.text ?? element.name.text;
|
|
89
|
-
if (importedName ===
|
|
83
|
+
if (importedName === "Wrec") {
|
|
90
84
|
names.add(element.name.text);
|
|
91
85
|
const moduleText = statement.moduleSpecifier.getText(sourceFile);
|
|
92
86
|
quote = moduleText[0];
|
|
@@ -94,14 +88,12 @@ export function getWrecImportInfo(sourceFile) {
|
|
|
94
88
|
}
|
|
95
89
|
}
|
|
96
90
|
|
|
97
|
-
return {names, quote};
|
|
91
|
+
return { names, quote };
|
|
98
92
|
}
|
|
99
93
|
|
|
100
94
|
// Determines if an AST node has the static modifier.
|
|
101
95
|
export function hasStaticModifier(node) {
|
|
102
96
|
return ts.canHaveModifiers(node)
|
|
103
|
-
? ts
|
|
104
|
-
.getModifiers(node)
|
|
105
|
-
?.some(mod => mod.kind === ts.SyntaxKind.StaticKeyword)
|
|
97
|
+
? ts.getModifiers(node)?.some((mod) => mod.kind === ts.SyntaxKind.StaticKeyword)
|
|
106
98
|
: false;
|
|
107
99
|
}
|
package/scripts/declare.js
CHANGED
|
@@ -9,22 +9,18 @@
|
|
|
9
9
|
// Include the --dry flag to report whether changes are needed
|
|
10
10
|
// without writing the modified source back to the file.
|
|
11
11
|
|
|
12
|
-
import fs from
|
|
13
|
-
import path from
|
|
14
|
-
import ts from
|
|
15
|
-
import {
|
|
16
|
-
collectWrecClasses,
|
|
17
|
-
getMemberName,
|
|
18
|
-
hasStaticModifier
|
|
19
|
-
} from './ast-utils.js';
|
|
12
|
+
import fs from "node:fs";
|
|
13
|
+
import path from "node:path";
|
|
14
|
+
import ts from "typescript";
|
|
15
|
+
import { collectWrecClasses, getMemberName, hasStaticModifier } from "./ast-utils.js";
|
|
20
16
|
|
|
21
17
|
const cwd = process.cwd();
|
|
22
18
|
const DECLARE_TYPE_MAP = new Map([
|
|
23
|
-
[
|
|
24
|
-
[
|
|
25
|
-
[
|
|
26
|
-
[
|
|
27
|
-
[
|
|
19
|
+
["Array", "unknown[]"],
|
|
20
|
+
["Boolean", "boolean"],
|
|
21
|
+
["Number", "number"],
|
|
22
|
+
["Object", "object"],
|
|
23
|
+
["String", "string"],
|
|
28
24
|
]);
|
|
29
25
|
|
|
30
26
|
// Analyzes a parsed source file and returns any proposed `declare` edits.
|
|
@@ -53,46 +49,35 @@ function analyzeSourceFile(sourceFile) {
|
|
|
53
49
|
|
|
54
50
|
const start = propertiesMember.end;
|
|
55
51
|
const end = findDeclareBlockEnd(classNode, propertiesMember, sourceFile);
|
|
56
|
-
const nextText = buildDeclareBlock(
|
|
57
|
-
sourceFile,
|
|
58
|
-
classNode,
|
|
59
|
-
propertiesMember,
|
|
60
|
-
declareLines
|
|
61
|
-
);
|
|
52
|
+
const nextText = buildDeclareBlock(sourceFile, classNode, propertiesMember, declareLines);
|
|
62
53
|
const currentText = sourceFile.text.slice(start, end);
|
|
63
54
|
|
|
64
55
|
if (nextText !== currentText) {
|
|
65
|
-
edits.push({end, start, text: nextText});
|
|
56
|
+
edits.push({ end, start, text: nextText });
|
|
66
57
|
}
|
|
67
58
|
}
|
|
68
59
|
|
|
69
60
|
if (classNodes.length === 0) {
|
|
70
|
-
return {changed: false, foundWrecSubclass: false, text: sourceFile.text};
|
|
61
|
+
return { changed: false, foundWrecSubclass: false, text: sourceFile.text };
|
|
71
62
|
}
|
|
72
63
|
|
|
73
64
|
if (edits.length === 0) {
|
|
74
|
-
return {changed: false, foundWrecSubclass: true, text: sourceFile.text};
|
|
65
|
+
return { changed: false, foundWrecSubclass: true, text: sourceFile.text };
|
|
75
66
|
}
|
|
76
67
|
|
|
77
68
|
let nextSource = sourceFile.text;
|
|
78
69
|
edits.sort((a, b) => b.start - a.start);
|
|
79
70
|
|
|
80
71
|
for (const edit of edits) {
|
|
81
|
-
nextSource =
|
|
82
|
-
nextSource.slice(0, edit.start) + edit.text + nextSource.slice(edit.end);
|
|
72
|
+
nextSource = nextSource.slice(0, edit.start) + edit.text + nextSource.slice(edit.end);
|
|
83
73
|
}
|
|
84
74
|
|
|
85
|
-
return {changed: true, foundWrecSubclass: true, text: nextSource};
|
|
75
|
+
return { changed: true, foundWrecSubclass: true, text: nextSource };
|
|
86
76
|
}
|
|
87
77
|
|
|
88
78
|
// Builds the `declare` block that should appear after `static properties`.
|
|
89
|
-
function buildDeclareBlock(
|
|
90
|
-
sourceFile
|
|
91
|
-
classNode,
|
|
92
|
-
propertiesMember,
|
|
93
|
-
declareLines
|
|
94
|
-
) {
|
|
95
|
-
const {text} = sourceFile;
|
|
79
|
+
function buildDeclareBlock(sourceFile, classNode, propertiesMember, declareLines) {
|
|
80
|
+
const { text } = sourceFile;
|
|
96
81
|
const memberIndent = getIndent(text, propertiesMember.getStart(sourceFile));
|
|
97
82
|
const startIndex = classNode.members.indexOf(propertiesMember) + 1;
|
|
98
83
|
let nextMember = null;
|
|
@@ -105,15 +90,13 @@ function buildDeclareBlock(
|
|
|
105
90
|
break;
|
|
106
91
|
}
|
|
107
92
|
|
|
108
|
-
const nextIndent = nextMember
|
|
109
|
-
? getIndent(text, nextMember.getStart(sourceFile))
|
|
110
|
-
: '';
|
|
93
|
+
const nextIndent = nextMember ? getIndent(text, nextMember.getStart(sourceFile)) : "";
|
|
111
94
|
|
|
112
95
|
if (declareLines.length === 0) {
|
|
113
|
-
return nextMember ? `\n\n${nextIndent}` :
|
|
96
|
+
return nextMember ? `\n\n${nextIndent}` : "\n";
|
|
114
97
|
}
|
|
115
98
|
|
|
116
|
-
const content = declareLines.map(line => `${memberIndent}${line}`).join(
|
|
99
|
+
const content = declareLines.map((line) => `${memberIndent}${line}`).join("\n");
|
|
117
100
|
return nextMember ? `\n${content}\n\n${nextIndent}` : `\n${content}\n`;
|
|
118
101
|
}
|
|
119
102
|
|
|
@@ -124,15 +107,15 @@ function createDeclareLine(propName, declareType) {
|
|
|
124
107
|
|
|
125
108
|
// Determines what changes, if any, should be made in a source file.
|
|
126
109
|
export function evaluateSourceFile(filePath, options = {}) {
|
|
127
|
-
const {dry = false} = options;
|
|
110
|
+
const { dry = false } = options;
|
|
128
111
|
const absFilePath = path.resolve(cwd, filePath);
|
|
129
112
|
validateFile(absFilePath);
|
|
130
113
|
|
|
131
|
-
const text = fs.readFileSync(absFilePath,
|
|
114
|
+
const text = fs.readFileSync(absFilePath, "utf8");
|
|
132
115
|
const result = evaluateSourceText(absFilePath, text);
|
|
133
116
|
|
|
134
117
|
if (!result.foundWrecSubclass) {
|
|
135
|
-
throw new Error(
|
|
118
|
+
throw new Error("No class extending Wrec was found.");
|
|
136
119
|
}
|
|
137
120
|
|
|
138
121
|
if (!dry && result.changed) {
|
|
@@ -149,7 +132,7 @@ export function evaluateSourceText(filePath, text) {
|
|
|
149
132
|
text,
|
|
150
133
|
ts.ScriptTarget.Latest,
|
|
151
134
|
true,
|
|
152
|
-
ts.ScriptKind.TS
|
|
135
|
+
ts.ScriptKind.TS,
|
|
153
136
|
);
|
|
154
137
|
return analyzeSourceFile(sourceFile);
|
|
155
138
|
}
|
|
@@ -174,7 +157,7 @@ function getDeclareType(initializer) {
|
|
|
174
157
|
for (const property of initializer.properties) {
|
|
175
158
|
if (
|
|
176
159
|
!ts.isPropertyAssignment(property) ||
|
|
177
|
-
getMemberName(property) !==
|
|
160
|
+
getMemberName(property) !== "type" ||
|
|
178
161
|
!ts.isIdentifier(property.initializer)
|
|
179
162
|
) {
|
|
180
163
|
continue;
|
|
@@ -188,9 +171,9 @@ function getDeclareType(initializer) {
|
|
|
188
171
|
|
|
189
172
|
// Gets the leading indentation in the line that contains a given position.
|
|
190
173
|
function getIndent(text, pos) {
|
|
191
|
-
const lineStart = text.lastIndexOf(
|
|
174
|
+
const lineStart = text.lastIndexOf("\n", pos - 1) + 1;
|
|
192
175
|
const match = /^[ \t]*/.exec(text.slice(lineStart));
|
|
193
|
-
return match ? match[0] :
|
|
176
|
+
return match ? match[0] : "";
|
|
194
177
|
}
|
|
195
178
|
|
|
196
179
|
// Gets the last `static properties = { ... }` declaration in a class.
|
|
@@ -201,7 +184,7 @@ function getLastPropertiesDeclaration(classNode) {
|
|
|
201
184
|
if (
|
|
202
185
|
ts.isPropertyDeclaration(member) &&
|
|
203
186
|
hasStaticModifier(member) &&
|
|
204
|
-
getMemberName(member) ===
|
|
187
|
+
getMemberName(member) === "properties" &&
|
|
205
188
|
member.initializer &&
|
|
206
189
|
ts.isObjectLiteralExpression(member.initializer)
|
|
207
190
|
) {
|
|
@@ -217,52 +200,48 @@ function isDeclarePropertyDeclaration(member) {
|
|
|
217
200
|
return (
|
|
218
201
|
ts.isPropertyDeclaration(member) &&
|
|
219
202
|
ts.canHaveModifiers(member) &&
|
|
220
|
-
ts
|
|
221
|
-
.getModifiers(member)
|
|
222
|
-
?.some(mod => mod.kind === ts.SyntaxKind.DeclareKeyword) === true
|
|
203
|
+
ts.getModifiers(member)?.some((mod) => mod.kind === ts.SyntaxKind.DeclareKeyword) === true
|
|
223
204
|
);
|
|
224
205
|
}
|
|
225
206
|
|
|
226
207
|
// Handles CLI arguments and runs the script.
|
|
227
208
|
function main() {
|
|
228
209
|
const args = process.argv.slice(2);
|
|
229
|
-
const unknownFlags = args.filter(
|
|
230
|
-
arg => arg.startsWith('--') && arg !== '--dry'
|
|
231
|
-
);
|
|
210
|
+
const unknownFlags = args.filter((arg) => arg.startsWith("--") && arg !== "--dry");
|
|
232
211
|
if (unknownFlags.length > 0) {
|
|
233
212
|
throw new Error(`unknown option: ${unknownFlags[0]}`);
|
|
234
213
|
}
|
|
235
214
|
|
|
236
|
-
const inputPaths = args.filter(arg => !arg.startsWith(
|
|
215
|
+
const inputPaths = args.filter((arg) => !arg.startsWith("--"));
|
|
237
216
|
if (inputPaths.length !== 1) {
|
|
238
|
-
throw new Error(
|
|
217
|
+
throw new Error("Specify a single source file");
|
|
239
218
|
}
|
|
240
219
|
|
|
241
|
-
const dry = args.includes(
|
|
242
|
-
const result = evaluateSourceFile(inputPaths[0], {dry});
|
|
220
|
+
const dry = args.includes("--dry");
|
|
221
|
+
const result = evaluateSourceFile(inputPaths[0], { dry });
|
|
243
222
|
|
|
244
223
|
if (dry) {
|
|
245
224
|
if (result.changed) process.exit(1);
|
|
246
|
-
console.info(
|
|
225
|
+
console.info("no changes needed");
|
|
247
226
|
return;
|
|
248
227
|
}
|
|
249
228
|
|
|
250
229
|
if (result.changed) {
|
|
251
|
-
console.info(
|
|
230
|
+
console.info("updated source file");
|
|
252
231
|
} else {
|
|
253
|
-
console.info(
|
|
232
|
+
console.info("no changes needed");
|
|
254
233
|
}
|
|
255
234
|
}
|
|
256
235
|
|
|
257
236
|
// Validates that a source file exists and has a supported extension.
|
|
258
237
|
function validateFile(absFilePath) {
|
|
259
|
-
if (!fs.existsSync(absFilePath)) throw new Error(
|
|
238
|
+
if (!fs.existsSync(absFilePath)) throw new Error("File not found");
|
|
260
239
|
|
|
261
240
|
const stat = fs.statSync(absFilePath);
|
|
262
|
-
if (!stat.isFile()) throw new Error(
|
|
241
|
+
if (!stat.isFile()) throw new Error("Not a file");
|
|
263
242
|
|
|
264
|
-
if (
|
|
265
|
-
throw new Error(
|
|
243
|
+
if (!absFilePath.endsWith(".ts")) {
|
|
244
|
+
throw new Error("declare statements can only be added in .ts files");
|
|
266
245
|
}
|
|
267
246
|
}
|
|
268
247
|
|