wrec 0.29.3 → 0.30.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "wrec",
3
3
  "description": "a small library that greatly simplifies building web components",
4
4
  "author": "R. Mark Volkmann",
5
- "version": "0.29.3",
5
+ "version": "0.30.0",
6
6
  "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
@@ -34,8 +34,9 @@
34
34
  },
35
35
  "files": [
36
36
  "dist",
37
- "scripts/used-by.js",
37
+ "scripts/ast-utils.js",
38
38
  "scripts/lint.js",
39
+ "scripts/used-by.js",
39
40
  "README.md"
40
41
  ],
41
42
  "scripts": {
@@ -0,0 +1,107 @@
1
+ import ts from 'typescript';
2
+
3
+ // Finds all classes in a source file that extend Wrec.
4
+ export function collectWrecClasses(sourceFile) {
5
+ const {names: wrecNames} = getWrecImportInfo(sourceFile);
6
+ const classes = [];
7
+
8
+ function visit(node) {
9
+ if (
10
+ ts.isClassDeclaration(node) &&
11
+ node.name &&
12
+ extendsWrec(node, wrecNames)
13
+ ) {
14
+ classes.push(node);
15
+ }
16
+ ts.forEachChild(node, visit);
17
+ }
18
+
19
+ visit(sourceFile);
20
+ return classes;
21
+ }
22
+
23
+ // Determines whether a class declaration extends one of the known Wrec imports.
24
+ export function extendsWrec(classNode, wrecNames) {
25
+ return Boolean(
26
+ classNode.heritageClauses?.some(
27
+ clause =>
28
+ clause.token === ts.SyntaxKind.ExtendsKeyword &&
29
+ clause.types.some(
30
+ type =>
31
+ ts.isExpressionWithTypeArguments(type) &&
32
+ ts.isIdentifier(type.expression) &&
33
+ wrecNames.has(type.expression.text)
34
+ )
35
+ )
36
+ );
37
+ }
38
+
39
+ // Gets a plain string member name when one can be statically determined.
40
+ export function getMemberName(node) {
41
+ const {name} = node;
42
+ return name ? (getNameText(name) ?? undefined) : undefined;
43
+ }
44
+
45
+ // Gets the text value of an AST name node.
46
+ export function getNameText(name) {
47
+ return ts.isIdentifier(name) ||
48
+ ts.isStringLiteral(name) ||
49
+ ts.isPrivateIdentifier(name)
50
+ ? name.text
51
+ : null;
52
+ }
53
+
54
+ // Collects object-literal property names from property assignments.
55
+ export function getPropertyAssignmentNames(objectLiteral) {
56
+ return objectLiteral.properties
57
+ .filter(ts.isPropertyAssignment)
58
+ .map(property => getMemberName(property))
59
+ .filter(name => name !== undefined);
60
+ }
61
+
62
+ // Collects imported Wrec class names and the quote style used for those imports.
63
+ export function getWrecImportInfo(sourceFile) {
64
+ const names = new Set(['Wrec']);
65
+ let quote = "'";
66
+
67
+ for (const statement of sourceFile.statements) {
68
+ if (
69
+ !ts.isImportDeclaration(statement) ||
70
+ !statement.importClause ||
71
+ !ts.isStringLiteral(statement.moduleSpecifier)
72
+ ) {
73
+ continue;
74
+ }
75
+
76
+ const moduleName = statement.moduleSpecifier.text;
77
+ const isWrecModule =
78
+ moduleName === 'wrec' ||
79
+ moduleName.endsWith('/wrec') ||
80
+ moduleName.endsWith('/wrec.js') ||
81
+ moduleName.endsWith('/wrec.ts');
82
+ if (!isWrecModule) continue;
83
+
84
+ const namedBindings = statement.importClause.namedBindings;
85
+ if (!namedBindings || !ts.isNamedImports(namedBindings)) continue;
86
+
87
+ for (const element of namedBindings.elements) {
88
+ const importedName = element.propertyName?.text ?? element.name.text;
89
+ if (importedName === 'Wrec') {
90
+ names.add(element.name.text);
91
+ const moduleText = statement.moduleSpecifier.getText(sourceFile);
92
+ quote = moduleText[0];
93
+ }
94
+ }
95
+ }
96
+
97
+ return {names, quote};
98
+ }
99
+
100
+ // Determines if an AST node has the static modifier.
101
+ export function hasStaticModifier(node) {
102
+ return ts.canHaveModifiers(node)
103
+ ? ts
104
+ .getModifiers(node)
105
+ ?.some(mod => mod.kind === ts.SyntaxKind.StaticKeyword)
106
+ : false;
107
+ }