@nstudio/nativescript-compiler-angular 0.0.0-stage → 9.2.0-alpha.11
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 +19 -0
- package/README.md +27 -2
- package/dist/angular-libraries.js +386 -0
- package/dist/angular.js +4189 -0
- package/dist/component-bases.js +853 -0
- package/dist/core-api.js +8 -0
- package/dist/entry.js +150 -0
- package/dist/index.js +233 -0
- package/dist/kotlin.js +295 -0
- package/dist/modules.js +780 -0
- package/dist/ng-modules.js +66 -0
- package/dist/package.js +15 -0
- package/dist/refusals.js +4 -0
- package/dist/semantics.js +274 -0
- package/dist/swift.js +352 -0
- package/kit-android/fromcore/AngularAPI.kt +177 -0
- package/kit-android/fromcore/AngularRouter.kt +720 -0
- package/kit-android/main/AngularChangeDetection.kt +530 -0
- package/kit-android/main/AngularCore.kt +798 -0
- package/kit-android/main/AngularHttp.kt +431 -0
- package/kit-android/main/AngularRouterValues.kt +154 -0
- package/kit-android/main/AngularRx.kt +228 -0
- package/kit-apple/AngularAPI.swift +228 -0
- package/kit-apple/AngularChangeDetection.swift +510 -0
- package/kit-apple/AngularCore.swift +878 -0
- package/kit-apple/AngularHttp.swift +467 -0
- package/kit-apple/AngularRouter.swift +760 -0
- package/kit-apple/AngularRouterValues.swift +285 -0
- package/kit-apple/AngularRx.swift +250 -0
- package/kit-apple/kit-index.json +861 -0
- package/package.json +56 -4
- package/runtime/angular-cdk.ts +673 -0
- package/runtime/angular-forms.ts +3099 -0
- package/runtime/angular-rx.ts +586 -0
- package/runtime/angular.ts +826 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright OpenJS Foundation and other contributors, https://openjsf.org
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nstudio/nativescript-compiler-angular
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Angular adapter for [`@nstudio/nativescript-compiler`](https://www.npmjs.com/package/@nstudio/nativescript-compiler), enabling NativeScript Angular apps (`@nativescript/angular`) to compile directly to Swift (iOS) and Kotlin (Android) with no JavaScript runtime.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
ns extension install @nstudio/nativescript-compiler
|
|
9
|
+
npm install --save-dev @nstudio/nativescript-compiler-angular
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Install the adapter at the same version as `@nstudio/nativescript-compiler`.
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
ns compile ios
|
|
18
|
+
ns compile android
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Supported Versions
|
|
22
|
+
|
|
23
|
+
- `@nativescript/angular`: `>=22.0.0 <23.0.0`
|
|
24
|
+
- `@angular/core`: `>=22.0.0 <23.0.0`
|
|
25
|
+
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
MIT
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { componentOf, declarablesIn } from "./angular.js";
|
|
4
|
+
import { core, ts } from "./core-api.js";
|
|
5
|
+
/** The TypeScript of each `<package>/angular` entry the app imports, from the plugin's source beside its main module. */
|
|
6
|
+
export function angularLibraries(sources, modulesDir, plugins, replaced) {
|
|
7
|
+
const out = [];
|
|
8
|
+
const seen = new Set();
|
|
9
|
+
for (const f of sources) {
|
|
10
|
+
for (const m of readFileSync(f, 'utf8').matchAll(/import\s*\{([^}]*)\}\s*from\s*['"]((?:@[\w.-]+\/)?[\w.-]+)\/angular['"]/g)) {
|
|
11
|
+
const wanted = m[1].split(',').map((x) => x.trim().split(/\s+as\s+/)[0]).filter(Boolean);
|
|
12
|
+
// A plugin the build stubs has its stand-in's elements, and one the app replaces is the app's module: neither has
|
|
13
|
+
// an Angular wrapper of its own compiled.
|
|
14
|
+
if (seen.has(m[2]) || core.stubbedPlugins.has(m[2]) || replaced.has(m[2]))
|
|
15
|
+
continue;
|
|
16
|
+
seen.add(m[2]);
|
|
17
|
+
const dir = join(modulesDir, m[2]);
|
|
18
|
+
const main = ['index.ios.js', 'index.js'].map((x) => join(dir, x)).find(existsSync);
|
|
19
|
+
if (!main)
|
|
20
|
+
continue;
|
|
21
|
+
plugins.get(dir);
|
|
22
|
+
const mainSource = plugins.sourceOf(main);
|
|
23
|
+
const angular = mainSource && join(dirname(mainSource), 'angular');
|
|
24
|
+
// A wrapper written against Angular's view internals (ElementRef, ViewContainerRef) has no components of its own the build reads.
|
|
25
|
+
if (!angular || !existsSync(angular))
|
|
26
|
+
continue;
|
|
27
|
+
const components = new Map();
|
|
28
|
+
/** The names an NgModule's `declarations`, `imports` and `exports` list: an app importing the module imports those. */
|
|
29
|
+
const modules = new Map();
|
|
30
|
+
const moduleFiles = new Map();
|
|
31
|
+
// A wrapper written against Angular's view internals (ElementRef, ViewContainerRef) is not compiled: a compiled template replaces it.
|
|
32
|
+
const viewInternals = (text) => /\b(ViewContainerRef|DetachedContainer)\b/.test(text);
|
|
33
|
+
const walk = (d) => {
|
|
34
|
+
for (const x of readdirSync(d)) {
|
|
35
|
+
const p = join(d, x);
|
|
36
|
+
if (statSync(p).isDirectory())
|
|
37
|
+
walk(p);
|
|
38
|
+
else if (x.endsWith('.ts') && !x.endsWith('.d.ts') && !x.endsWith('.spec.ts')) {
|
|
39
|
+
const text = readFileSync(p, 'utf8');
|
|
40
|
+
const name = viewInternals(text) ? undefined : componentOf(p, text)?.name;
|
|
41
|
+
if (name)
|
|
42
|
+
components.set(name, p);
|
|
43
|
+
const found = declarablesIn(p, text);
|
|
44
|
+
for (const d of found.classes)
|
|
45
|
+
components.set(d, p);
|
|
46
|
+
for (const [m, names] of found.modules) {
|
|
47
|
+
modules.set(m, names);
|
|
48
|
+
if (!viewInternals(text))
|
|
49
|
+
moduleFiles.set(m, p);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
walk(angular);
|
|
55
|
+
const listed = (names) => names.flatMap((n) => (modules.has(n) ? listed(modules.get(n).filter((x) => x !== n)) : [n]));
|
|
56
|
+
// The components and pipes the app imports, directly or through an NgModule, and those their `imports` name,
|
|
57
|
+
// transitively, with the modules of their own they import.
|
|
58
|
+
const queue = listed(wanted).filter((w) => components.has(w));
|
|
59
|
+
const taken = new Set();
|
|
60
|
+
const files = [...components.values()];
|
|
61
|
+
const take = (file) => {
|
|
62
|
+
out.push(file);
|
|
63
|
+
const text = readFileSync(file, 'utf8');
|
|
64
|
+
const imports = /imports:\s*\[([^\]]*)\]/.exec(text)?.[1] ?? '';
|
|
65
|
+
for (const i of imports.split(',').map((x) => x.trim()))
|
|
66
|
+
if (components.has(i))
|
|
67
|
+
queue.push(i);
|
|
68
|
+
for (const m of text.matchAll(/from\s*['"](\.[^'"]+)['"]/g)) {
|
|
69
|
+
const support = [resolve(dirname(file), m[1]) + '.ts', join(resolve(dirname(file), m[1]), 'index.ts')].find(existsSync);
|
|
70
|
+
if (support && !files.includes(support) && !out.includes(support) && !viewInternals(readFileSync(support, 'utf8')))
|
|
71
|
+
out.push(support);
|
|
72
|
+
// A component the code itself uses (a dialog it opens).
|
|
73
|
+
for (const [other, f] of components)
|
|
74
|
+
if (f === support)
|
|
75
|
+
queue.push(other);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const drain = () => {
|
|
79
|
+
while (queue.length) {
|
|
80
|
+
const name = queue.pop();
|
|
81
|
+
if (taken.has(name))
|
|
82
|
+
continue;
|
|
83
|
+
taken.add(name);
|
|
84
|
+
take(components.get(name));
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
drain();
|
|
88
|
+
// An entry with components compiled here resolves to its typings unless it is compiled too: the NgModules the app
|
|
89
|
+
// imports from it are, in their own files. Without components the whole entry is compiled as the plugin's source.
|
|
90
|
+
if (!taken.size)
|
|
91
|
+
continue;
|
|
92
|
+
const imported = new Set();
|
|
93
|
+
const moduleNames = (names) => names.forEach((n) => { if (modules.has(n) && !imported.has(n)) {
|
|
94
|
+
imported.add(n);
|
|
95
|
+
moduleNames(modules.get(n));
|
|
96
|
+
} });
|
|
97
|
+
moduleNames(wanted);
|
|
98
|
+
for (const name of imported) {
|
|
99
|
+
const file = moduleFiles.get(name);
|
|
100
|
+
if (file && !out.includes(file))
|
|
101
|
+
take(file);
|
|
102
|
+
}
|
|
103
|
+
drain();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
/** Annotates untyped parameters of JS function/arrow/method expressions inside a node so it compiles in strict TS. */
|
|
109
|
+
function typedMetaLiteral(sf, node) {
|
|
110
|
+
const edits = [];
|
|
111
|
+
const visit = (n) => {
|
|
112
|
+
if (ts.isParameter(n) && !n.type)
|
|
113
|
+
edits.push({ at: n.name.getEnd(), text: ': any' });
|
|
114
|
+
ts.forEachChild(n, visit);
|
|
115
|
+
};
|
|
116
|
+
visit(node);
|
|
117
|
+
const start = node.getStart(sf);
|
|
118
|
+
let text = node.getText(sf);
|
|
119
|
+
for (const e of edits.sort((a, b) => b.at - a.at)) {
|
|
120
|
+
const pos = e.at - start;
|
|
121
|
+
text = text.slice(0, pos) + e.text + text.slice(pos);
|
|
122
|
+
}
|
|
123
|
+
return text;
|
|
124
|
+
}
|
|
125
|
+
/** Collects top-level constant and helper function declarations referenced by `obj` (e.g. `MAINCONTENT = 'MainContent'`, `rawChildIndex(...)`). */
|
|
126
|
+
function metaLiteralCode(sf, obj) {
|
|
127
|
+
const decls = new Map();
|
|
128
|
+
for (const st of sf.statements) {
|
|
129
|
+
if (ts.isFunctionDeclaration(st) && st.name) {
|
|
130
|
+
decls.set(st.name.text, { node: st, code: typedMetaLiteral(sf, st) });
|
|
131
|
+
}
|
|
132
|
+
else if (ts.isVariableStatement(st) && (st.declarationList.flags & ts.NodeFlags.Const)) {
|
|
133
|
+
for (const d of st.declarationList.declarations) {
|
|
134
|
+
if (!ts.isIdentifier(d.name) || !d.initializer)
|
|
135
|
+
continue;
|
|
136
|
+
if (ts.isStringLiteralLike(d.initializer) || ts.isNumericLiteral(d.initializer) || d.initializer.kind === ts.SyntaxKind.TrueKeyword || d.initializer.kind === ts.SyntaxKind.FalseKeyword) {
|
|
137
|
+
decls.set(d.name.text, { node: d.initializer, code: `const ${d.name.text} = ${d.initializer.getText(sf)};` });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const used = new Set();
|
|
143
|
+
const scanIds = (n) => {
|
|
144
|
+
if (ts.isIdentifier(n) && decls.has(n.text) && !used.has(n.text) && !(ts.isPropertyAccessExpression(n.parent) && n.parent.name === n) && !(ts.isPropertyAssignment(n.parent) && n.parent.name === n) && !(ts.isFunctionDeclaration(n.parent) && n.parent.name === n)) {
|
|
145
|
+
used.add(n.text);
|
|
146
|
+
scanIds(decls.get(n.text).node);
|
|
147
|
+
}
|
|
148
|
+
ts.forEachChild(n, scanIds);
|
|
149
|
+
};
|
|
150
|
+
scanIds(obj);
|
|
151
|
+
const body = typedMetaLiteral(sf, obj);
|
|
152
|
+
if (!used.size)
|
|
153
|
+
return body;
|
|
154
|
+
return `(() => { ${[...used].map((k) => decls.get(k).code).join(' ')} return ${body}; })()`;
|
|
155
|
+
}
|
|
156
|
+
/** Discovers elements registered via a plugin helper module (e.g. `<pkg>/elements.js` + `@CSSType` decorators in `<pkg>/web.js`). */
|
|
157
|
+
function scanPluginElementTable(pkg, helperSubpath, modulesDir, metaObj, fesmSf, found) {
|
|
158
|
+
const pkgDir = join(modulesDir, pkg);
|
|
159
|
+
const candidates = [join(pkgDir, helperSubpath + '.js'), join(pkgDir, helperSubpath, 'index.js'), join(pkgDir, helperSubpath + '.mjs')];
|
|
160
|
+
const helperFile = candidates.find(existsSync);
|
|
161
|
+
if (!helperFile)
|
|
162
|
+
return;
|
|
163
|
+
const helperText = readFileSync(helperFile, 'utf8');
|
|
164
|
+
const helperSf = ts.createSourceFile(helperFile, helperText, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
|
|
165
|
+
const helperImports = new Map();
|
|
166
|
+
const namespaceImports = [];
|
|
167
|
+
for (const st of helperSf.statements) {
|
|
168
|
+
if (!ts.isImportDeclaration(st) || !ts.isStringLiteral(st.moduleSpecifier))
|
|
169
|
+
continue;
|
|
170
|
+
const spec = st.moduleSpecifier.text;
|
|
171
|
+
const from = spec === '.' || spec === './' || spec === './index' ? pkg : spec.startsWith('./') ? `${pkg}/${spec.slice(2).replace(/\.js$/, '')}` : spec;
|
|
172
|
+
const clause = st.importClause;
|
|
173
|
+
if (clause?.namedBindings && ts.isNamedImports(clause.namedBindings)) {
|
|
174
|
+
for (const el of clause.namedBindings.elements) {
|
|
175
|
+
helperImports.set(el.name.text, [(el.propertyName ?? el.name).text, from]);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else if (clause?.namedBindings && ts.isNamespaceImport(clause.namedBindings)) {
|
|
179
|
+
namespaceImports.push({ local: clause.namedBindings.name.text, spec });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const seen = new Set();
|
|
183
|
+
const metaCode = metaObj ? metaLiteralCode(fesmSf, metaObj) : undefined;
|
|
184
|
+
const add = (tag, name, from) => {
|
|
185
|
+
const key = tag.toLowerCase();
|
|
186
|
+
if (!key || seen.has(key) || found.has(tag))
|
|
187
|
+
return;
|
|
188
|
+
seen.add(key);
|
|
189
|
+
found.set(tag, { name, from, ...(metaObj ? { meta: ['insertChild', 'removeChild'] } : {}), ...(metaCode ? { metaCode } : {}) });
|
|
190
|
+
};
|
|
191
|
+
// Scan namespace-imported modules (e.g. `./web.js`) for `__decorate([CSSType('tag'), ...], Class)` via AST.
|
|
192
|
+
for (const ns of namespaceImports) {
|
|
193
|
+
const nsBase = resolve(dirname(helperFile), ns.spec);
|
|
194
|
+
const nsFile = [nsBase, nsBase + '.js', join(nsBase, 'index.js')].find(existsSync);
|
|
195
|
+
if (!nsFile)
|
|
196
|
+
continue;
|
|
197
|
+
const nsFrom = ns.spec.startsWith('./') ? `${pkg}/${ns.spec.slice(2).replace(/\.js$/, '')}` : ns.spec;
|
|
198
|
+
const nsSf = ts.createSourceFile(nsFile, readFileSync(nsFile, 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.JS);
|
|
199
|
+
const visitNs = (n) => {
|
|
200
|
+
if (ts.isCallExpression(n) && ts.isIdentifier(n.expression) && n.expression.text === '__decorate' && n.arguments.length >= 2 && ts.isArrayLiteralExpression(n.arguments[0]) && ts.isIdentifier(n.arguments[1])) {
|
|
201
|
+
const clsName = n.arguments[1].text;
|
|
202
|
+
for (const el of n.arguments[0].elements) {
|
|
203
|
+
if (ts.isCallExpression(el) && ts.isIdentifier(el.expression) && el.expression.text === 'CSSType' && el.arguments.length >= 1 && ts.isStringLiteralLike(el.arguments[0])) {
|
|
204
|
+
add(el.arguments[0].text, clsName, nsFrom);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
ts.forEachChild(n, visitNs);
|
|
209
|
+
};
|
|
210
|
+
visitNs(nsSf);
|
|
211
|
+
}
|
|
212
|
+
// Scan tuple arrays `[['View', View], ['Scroll', Scroll], ...]` inside the helper module.
|
|
213
|
+
const visitTuples = (n) => {
|
|
214
|
+
if (ts.isArrayLiteralExpression(n) && n.elements.length === 2 && ts.isStringLiteralLike(n.elements[0]) && ts.isIdentifier(n.elements[1])) {
|
|
215
|
+
const imp = helperImports.get(n.elements[1].text);
|
|
216
|
+
if (imp)
|
|
217
|
+
add(n.elements[0].text, imp[0], imp[1]);
|
|
218
|
+
}
|
|
219
|
+
ts.forEachChild(n, visitTuples);
|
|
220
|
+
};
|
|
221
|
+
visitTuples(helperSf);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Tags registered for plugin views: `registerElement('Tag', () => Class)` or `registerElement('Tag', () => Class, meta)`
|
|
225
|
+
* in the app, and in the Angular entry (`<package>/angular`) of a plugin the app imports, with the module each class is from.
|
|
226
|
+
*/
|
|
227
|
+
export function registeredElements(sources, modulesDir) {
|
|
228
|
+
const found = new Map();
|
|
229
|
+
const scan = (text, file, pluginPkg) => {
|
|
230
|
+
if (!text.includes('registerElement'))
|
|
231
|
+
return;
|
|
232
|
+
const imports = new Map();
|
|
233
|
+
for (const m of text.matchAll(/import\s*\{([^}]*)\}\s*from\s*['"]([^'"]+)['"]/g)) {
|
|
234
|
+
for (const part of m[1].split(',')) {
|
|
235
|
+
const [orig, alias] = part.trim().split(/\s+as\s+/);
|
|
236
|
+
if (orig)
|
|
237
|
+
imports.set((alias ?? orig).trim(), JSON.stringify([orig.trim(), m[2]]));
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const sf = ts.createSourceFile(file ?? 'entry.mjs', text, ts.ScriptTarget.Latest, true, file?.endsWith('.ts') ? ts.ScriptKind.TS : ts.ScriptKind.JS);
|
|
241
|
+
const objectOf = (e) => {
|
|
242
|
+
if (ts.isObjectLiteralExpression(e))
|
|
243
|
+
return e;
|
|
244
|
+
if (!ts.isIdentifier(e))
|
|
245
|
+
return null;
|
|
246
|
+
for (const st of sf.statements) {
|
|
247
|
+
if (!ts.isVariableStatement(st) || !(st.declarationList.flags & ts.NodeFlags.Const))
|
|
248
|
+
continue;
|
|
249
|
+
for (const d of st.declarationList.declarations)
|
|
250
|
+
if (ts.isIdentifier(d.name) && d.name.text === e.text && d.initializer)
|
|
251
|
+
return objectOf(d.initializer);
|
|
252
|
+
}
|
|
253
|
+
return null;
|
|
254
|
+
};
|
|
255
|
+
const stringArrays = new Map();
|
|
256
|
+
const stringsOf = (e) => {
|
|
257
|
+
if (ts.isAsExpression(e))
|
|
258
|
+
return stringsOf(e.expression);
|
|
259
|
+
if (ts.isIdentifier(e))
|
|
260
|
+
return stringArrays.get(e.text) ?? null;
|
|
261
|
+
if (!ts.isArrayLiteralExpression(e))
|
|
262
|
+
return null;
|
|
263
|
+
const items = [];
|
|
264
|
+
for (const el of e.elements) {
|
|
265
|
+
if (ts.isStringLiteralLike(el))
|
|
266
|
+
items.push(el.text);
|
|
267
|
+
else if (ts.isSpreadElement(el)) {
|
|
268
|
+
const inner = stringsOf(el.expression);
|
|
269
|
+
if (!inner)
|
|
270
|
+
return null;
|
|
271
|
+
items.push(...inner);
|
|
272
|
+
}
|
|
273
|
+
else
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
return items;
|
|
277
|
+
};
|
|
278
|
+
for (const st of sf.statements) {
|
|
279
|
+
if (!ts.isVariableStatement(st) || !(st.declarationList.flags & ts.NodeFlags.Const))
|
|
280
|
+
continue;
|
|
281
|
+
for (const d of st.declarationList.declarations) {
|
|
282
|
+
if (ts.isIdentifier(d.name) && d.initializer) {
|
|
283
|
+
const arr = stringsOf(d.initializer);
|
|
284
|
+
if (arr)
|
|
285
|
+
stringArrays.set(d.name.text, arr);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const localBaseClass = (name, scope) => {
|
|
290
|
+
for (let cur = scope; cur; cur = cur.parent) {
|
|
291
|
+
if (ts.isBlock(cur) || ts.isSourceFile(cur)) {
|
|
292
|
+
for (const st of cur.statements) {
|
|
293
|
+
if (ts.isClassDeclaration(st) && st.name?.text === name) {
|
|
294
|
+
const ext = st.heritageClauses?.find((h) => h.token === ts.SyntaxKind.ExtendsKeyword)?.types[0]?.expression;
|
|
295
|
+
if (ext && ts.isIdentifier(ext))
|
|
296
|
+
return ext.text;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return undefined;
|
|
302
|
+
};
|
|
303
|
+
const record = (tagText, clsId, meta, scope) => {
|
|
304
|
+
const imported = imports.get(clsId) ?? (localBaseClass(clsId, scope) ? imports.get(localBaseClass(clsId, scope)) : undefined);
|
|
305
|
+
if (!imported)
|
|
306
|
+
return;
|
|
307
|
+
const [name, from] = JSON.parse(imported);
|
|
308
|
+
// Core's own views are elements already.
|
|
309
|
+
if (from === '@nativescript/core' || core.stubbedPlugins.has(core.packageOf(from)))
|
|
310
|
+
return;
|
|
311
|
+
const object = meta ? objectOf(meta) : null;
|
|
312
|
+
const keys = object && object.properties.every((p) => p.name && !ts.isComputedPropertyName(p.name))
|
|
313
|
+
? object.properties.flatMap((p) => {
|
|
314
|
+
const key = p.name.getText().replace(/^['"]|['"]$/g, '');
|
|
315
|
+
// `skipAddToDom: false` explicitly opts in to the visual tree (e.g. MasonKit's `masonMeta`).
|
|
316
|
+
if (key === 'skipAddToDom' && ts.isPropertyAssignment(p) && p.initializer.kind === ts.SyntaxKind.FalseKeyword)
|
|
317
|
+
return [];
|
|
318
|
+
return [key];
|
|
319
|
+
})
|
|
320
|
+
: null;
|
|
321
|
+
const hasChildHooks = !!keys && keys.some((k) => k === 'insertChild' || k === 'removeChild');
|
|
322
|
+
const metaCode = !file && object && hasChildHooks ? metaLiteralCode(sf, object) : undefined;
|
|
323
|
+
found.set(tagText, {
|
|
324
|
+
name,
|
|
325
|
+
from: from.startsWith('.') && file ? resolve(dirname(file), from) : from,
|
|
326
|
+
...(meta ? { meta: keys } : {}),
|
|
327
|
+
...(metaCode ? { metaCode } : {}),
|
|
328
|
+
...(file ? { fromApp: true } : {}),
|
|
329
|
+
});
|
|
330
|
+
};
|
|
331
|
+
const loopTags = new Map();
|
|
332
|
+
const visit = (n) => {
|
|
333
|
+
if (ts.isForOfStatement(n)) {
|
|
334
|
+
const decl = ts.isVariableDeclarationList(n.initializer) ? n.initializer.declarations[0] : undefined;
|
|
335
|
+
if (decl && ts.isIdentifier(decl.name)) {
|
|
336
|
+
const tags = stringsOf(n.expression);
|
|
337
|
+
if (tags) {
|
|
338
|
+
loopTags.set(decl.name.text, tags);
|
|
339
|
+
ts.forEachChild(n.statement, visit);
|
|
340
|
+
loopTags.delete(decl.name.text);
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
// Plugin FESM loop over an imported element-table helper (`for (const { tag, ctor } of getMasonKitElements(...))`).
|
|
345
|
+
if (!file && pluginPkg && ts.isCallExpression(n.expression) && ts.isIdentifier(n.expression.expression)) {
|
|
346
|
+
const imp = imports.get(n.expression.expression.text);
|
|
347
|
+
if (imp) {
|
|
348
|
+
const [, helperFrom] = JSON.parse(imp);
|
|
349
|
+
if (helperFrom.startsWith(pluginPkg + '/')) {
|
|
350
|
+
const metaIdent = /\bregisterElement\([^,]+,\s*[^,]+,\s*[^?]*\?\s*(\w+)/.exec(n.statement.getText(sf))?.[1];
|
|
351
|
+
const metaObj = metaIdent ? objectOf(ts.factory.createIdentifier(metaIdent)) : null;
|
|
352
|
+
scanPluginElementTable(pluginPkg, helperFrom.slice(pluginPkg.length + 1), modulesDir, metaObj, sf, found);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
ts.forEachChild(n, visit);
|
|
358
|
+
if (!ts.isCallExpression(n) || !ts.isIdentifier(n.expression) || n.expression.text !== 'registerElement')
|
|
359
|
+
return;
|
|
360
|
+
const [tag, resolver, meta] = n.arguments;
|
|
361
|
+
if (!tag || !resolver || !ts.isArrowFunction(resolver) || resolver.parameters.length || !ts.isIdentifier(resolver.body))
|
|
362
|
+
return;
|
|
363
|
+
if (ts.isStringLiteralLike(tag)) {
|
|
364
|
+
record(tag.text, resolver.body.text, meta, n);
|
|
365
|
+
}
|
|
366
|
+
else if (ts.isIdentifier(tag) && loopTags.has(tag.text)) {
|
|
367
|
+
for (const t of loopTags.get(tag.text))
|
|
368
|
+
record(t, resolver.body.text, meta, n);
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
visit(sf);
|
|
372
|
+
};
|
|
373
|
+
for (const f of sources) {
|
|
374
|
+
const text = readFileSync(f, 'utf8');
|
|
375
|
+
scan(text, f);
|
|
376
|
+
for (const m of text.matchAll(/from\s*['"]((?:@[\w.-]+\/)?[\w.-]+)\/angular['"]/g)) {
|
|
377
|
+
if (core.stubbedPlugins.has(m[1]))
|
|
378
|
+
continue;
|
|
379
|
+
const dir = join(modulesDir, m[1], 'angular');
|
|
380
|
+
const entry = existsSync(dir) ? readdirSync(dir).flatMap((d) => (/^fesm/.test(d) ? readdirSync(join(dir, d)).filter((x) => x.endsWith('.mjs')).map((x) => join(dir, d, x)) : [])).sort().reverse()[0] : undefined;
|
|
381
|
+
if (entry)
|
|
382
|
+
scan(readFileSync(entry, 'utf8'), null, m[1]);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
return found;
|
|
386
|
+
}
|