@unframework.dev/target-angular 0.0.0-stage → 0.0.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/README.md +426 -2
- package/dist/composable.js +239 -0
- package/dist/composition.js +214 -0
- package/dist/directives.js +82 -0
- package/dist/effects.js +187 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +559 -0
- package/dist/listeners.js +547 -0
- package/dist/members.js +462 -0
- package/dist/models.js +308 -0
- package/dist/modules.js +54 -0
- package/dist/narrowing.js +157 -0
- package/dist/outputs.js +190 -0
- package/dist/plan.js +558 -0
- package/dist/rules.js +332 -0
- package/dist/seeded.js +284 -0
- package/dist/statements.js +213 -0
- package/dist/template.js +243 -0
- package/dist/toolchain/check.js +91 -0
- package/dist/toolchain/client.d.ts +10 -0
- package/dist/toolchain/client.js +79 -0
- package/dist/toolchain/component.js +20 -0
- package/dist/toolchain/index.d.ts +4 -0
- package/dist/toolchain/index.js +15 -0
- package/dist/toolchain/linker.js +57 -0
- package/dist/toolchain/ngtsc-virtual.js +238 -0
- package/dist/toolchain/ngtsc-worker.d.ts +1 -0
- package/dist/toolchain/ngtsc-worker.js +104 -0
- package/dist/toolchain/ngtsc.js +187 -0
- package/dist/toolchain/server.d.ts +8 -0
- package/dist/toolchain/server.js +31 -0
- package/dist/toolchain/tools.js +82 -0
- package/dist/toolchain/vite.js +103 -0
- package/dist/types.js +262 -0
- package/package.json +67 -3
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { resolveInstalled } from "@unframework.dev/codegen/toolchain-node";
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { pathToFileURL } from "node:url";
|
|
6
|
+
const compilers = /* @__PURE__ */ new Map();
|
|
7
|
+
const viteTools = /* @__PURE__ */ new Map();
|
|
8
|
+
/**
|
|
9
|
+
* Loads @angular/compiler-cli and the TypeScript it runs on from `toolchainDir`, once per
|
|
10
|
+
* directory. Rejects with the missing package and the directory when the toolchain is not
|
|
11
|
+
* installed there: a gate that cannot start fails.
|
|
12
|
+
*/
|
|
13
|
+
function loadCompiler(toolchainDir) {
|
|
14
|
+
let loaded = compilers.get(toolchainDir);
|
|
15
|
+
if (!loaded) {
|
|
16
|
+
loaded = importCompiler(toolchainDir);
|
|
17
|
+
loaded.catch(() => compilers.delete(toolchainDir));
|
|
18
|
+
compilers.set(toolchainDir, loaded);
|
|
19
|
+
}
|
|
20
|
+
return loaded;
|
|
21
|
+
}
|
|
22
|
+
/** Loads Analog and @angular/build's JavaScriptTransformer from `toolchainDir`, once. */
|
|
23
|
+
function loadViteTools(toolchainDir) {
|
|
24
|
+
let loaded = viteTools.get(toolchainDir);
|
|
25
|
+
if (!loaded) {
|
|
26
|
+
loaded = importViteTools(toolchainDir);
|
|
27
|
+
loaded.catch(() => viteTools.delete(toolchainDir));
|
|
28
|
+
viteTools.set(toolchainDir, loaded);
|
|
29
|
+
}
|
|
30
|
+
return loaded;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The version of a package installed for `directory`. Used to check that the compiler and the
|
|
34
|
+
* runtime that runs its output are the same Angular release.
|
|
35
|
+
*/
|
|
36
|
+
function packageVersion(directory, name) {
|
|
37
|
+
const manifest = resolveInstalled(directory, `${name}/package.json`);
|
|
38
|
+
if (!manifest) throw new Error(`Cannot resolve ${name} from ${directory}: install it there.`);
|
|
39
|
+
return JSON.parse(readFileSync(manifest, "utf8")).version;
|
|
40
|
+
}
|
|
41
|
+
async function importCompiler(toolchainDir) {
|
|
42
|
+
const entry = resolveTool(toolchainDir, "@angular/compiler-cli");
|
|
43
|
+
const cli = await import(pathToFileURL(entry).href);
|
|
44
|
+
const tsEntry = createRequire(entry).resolve("typescript");
|
|
45
|
+
const ts = (await import(pathToFileURL(tsEntry).href)).default;
|
|
46
|
+
const core = packageVersion(toolchainDir, "@angular/core");
|
|
47
|
+
if (core !== cli.VERSION.full) throw new Error(`The Angular toolchain in ${toolchainDir} mixes @angular/compiler-cli ${cli.VERSION.full} with @angular/core ${core}. Install the same version of both.`);
|
|
48
|
+
const angularScope = join(toolchainDir, "node_modules", "@angular");
|
|
49
|
+
if (!existsSync(join(angularScope, "core", "package.json"))) throw new Error(`The Angular toolchain directory ${toolchainDir} has no node_modules/@angular/core: install @angular/core there as a direct dependency.`);
|
|
50
|
+
return {
|
|
51
|
+
ts,
|
|
52
|
+
cli,
|
|
53
|
+
angularScope,
|
|
54
|
+
declarations: /* @__PURE__ */ new Map()
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async function importViteTools(toolchainDir) {
|
|
58
|
+
const analogEntry = resolveTool(toolchainDir, "@analogjs/vite-plugin-angular");
|
|
59
|
+
const analog = await import(pathToFileURL(analogEntry).href);
|
|
60
|
+
const buildEntry = resolveTool(toolchainDir, "@angular/build/private");
|
|
61
|
+
const build = await import(pathToFileURL(buildEntry).href);
|
|
62
|
+
return {
|
|
63
|
+
analog: (options) => analog.default(options),
|
|
64
|
+
createTransformer: () => new build.JavaScriptTransformer({
|
|
65
|
+
sourcemap: false,
|
|
66
|
+
jit: false,
|
|
67
|
+
advancedOptimizations: false,
|
|
68
|
+
maxConcurrency: 1
|
|
69
|
+
})
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Resolves one of the toolchain's tools, never through NODE_PATH (see `resolveInstalled`), and
|
|
74
|
+
* names what the directory must provide when it fails.
|
|
75
|
+
*/
|
|
76
|
+
function resolveTool(directory, specifier) {
|
|
77
|
+
if (!existsSync(join(directory, "package.json"))) throw new Error(`The Angular toolchain directory ${directory} has no package.json. It must provide @angular/compiler-cli, @angular/build, @analogjs/vite-plugin-angular and TypeScript 6.`);
|
|
78
|
+
const entry = resolveInstalled(directory, specifier);
|
|
79
|
+
if (!entry) throw new Error(`Cannot load ${specifier} from the Angular toolchain directory ${directory}. Install @angular/compiler-cli, @angular/build, @analogjs/vite-plugin-angular and TypeScript 6 there.`);
|
|
80
|
+
return entry;
|
|
81
|
+
}
|
|
82
|
+
export { loadCompiler, loadViteTools, packageVersion };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { loadCompiler, loadViteTools, packageVersion } from "./tools.js";
|
|
2
|
+
import { angularLinker, angularSsrLinker } from "./linker.js";
|
|
3
|
+
import { ngtscVirtual } from "./ngtsc-virtual.js";
|
|
4
|
+
import { mkdirSync, renameSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
/** One instance of each Angular package, wherever its importer lives (the adapters do too). */
|
|
7
|
+
const RUNTIME = [
|
|
8
|
+
"@angular/core",
|
|
9
|
+
"@angular/common",
|
|
10
|
+
"@angular/platform-browser",
|
|
11
|
+
"rxjs"
|
|
12
|
+
];
|
|
13
|
+
/**
|
|
14
|
+
* The plugins and options of an Angular project, merged after the unframework plugin:
|
|
15
|
+
* `ngtscVirtual` compiles every virtual Angular module (AOT, strict templates), and Analog
|
|
16
|
+
* stays in the chain as it would in an Analog app, with `jit: false` (its default under Vitest
|
|
17
|
+
* is JIT). Nothing is JIT-compiled, as in production: browser projects pre-bundle the runtime
|
|
18
|
+
* and link it in the dep optimizer, and SSR projects inline Angular's packages into their
|
|
19
|
+
* module graph and link them there.
|
|
20
|
+
*/
|
|
21
|
+
async function viteConfig(mode, context) {
|
|
22
|
+
const [compiler, tools] = await Promise.all([loadCompiler(context.toolchainDir), loadViteTools(context.toolchainDir)]);
|
|
23
|
+
const runtime = packageVersion(context.root, "@angular/core");
|
|
24
|
+
if (runtime !== compiler.cli.VERSION.full) throw new Error(`The project at ${context.root} runs @angular/core ${runtime}, but the Angular toolchain in ${context.toolchainDir} compiles with ${compiler.cli.VERSION.full}. Use one version.`);
|
|
25
|
+
const plugins = [ngtscVirtual(compiler, context.declarations), ...tools.analog({
|
|
26
|
+
tsconfig: analogTsconfig(context.toolchainDir),
|
|
27
|
+
jit: false
|
|
28
|
+
}).map(sourcemapPluginOnFileNames)];
|
|
29
|
+
if (mode === "browser") return {
|
|
30
|
+
plugins,
|
|
31
|
+
resolve: { dedupe: RUNTIME },
|
|
32
|
+
optimizeDeps: {
|
|
33
|
+
include: [
|
|
34
|
+
"@angular/core",
|
|
35
|
+
"@angular/common",
|
|
36
|
+
"@angular/platform-browser"
|
|
37
|
+
],
|
|
38
|
+
rolldownOptions: { plugins: [angularLinker(tools)] }
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
plugins: [...plugins, angularSsrLinker(tools)],
|
|
43
|
+
resolve: { dedupe: [...RUNTIME, "@angular/platform-server"] },
|
|
44
|
+
ssr: { noExternal: [/(?:^|\/node_modules\/)@angular\//] },
|
|
45
|
+
test: { pool: "forks" }
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** Analog's plugin that compiles, under Vitest, the `.ts` modules Angular did not. */
|
|
49
|
+
const ANALOG_SOURCEMAP_PLUGIN = "@analogjs/vitest-angular-sourcemap-plugin";
|
|
50
|
+
/**
|
|
51
|
+
* Analog's source-map plugin with its `.ts` test applied to the file name only. Analog tests
|
|
52
|
+
* the unanchored `/\.ts/` against the whole module id, so in a checkout whose path has a
|
|
53
|
+
* directory containing `.ts` (`example.ts.net/…`) it compiles every module as TypeScript, and
|
|
54
|
+
* the CSS of Vitest's page fails to parse, so no browser test runs. With the directories
|
|
55
|
+
* removed, the decision is Analog's own on every other path. Remove this once the catalog
|
|
56
|
+
* reaches an Analog whose test is anchored (3.0.0-beta.38 anchors it; 2.9.6 does not).
|
|
57
|
+
*/
|
|
58
|
+
function sourcemapPluginOnFileNames(plugin) {
|
|
59
|
+
const hook = plugin.transform;
|
|
60
|
+
if (plugin.name !== ANALOG_SOURCEMAP_PLUGIN || hook === void 0) return plugin;
|
|
61
|
+
const handler = typeof hook === "function" ? hook : hook.handler;
|
|
62
|
+
const transform = function(code, id, options) {
|
|
63
|
+
if (!/\.ts/.test(withoutDirectories(id))) return;
|
|
64
|
+
return handler.call(this, code, id, options);
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
...plugin,
|
|
68
|
+
transform: typeof hook === "function" ? transform : {
|
|
69
|
+
...hook,
|
|
70
|
+
handler: transform
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** A module id without its directories: the text after the last `/` that comes before any `?`. */
|
|
75
|
+
function withoutDirectories(id) {
|
|
76
|
+
const query = id.indexOf("?");
|
|
77
|
+
const path = query === -1 ? id : id.slice(0, query);
|
|
78
|
+
return id.slice(path.lastIndexOf("/") + 1);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The tsconfig Analog reads: an empty program. Every Angular module of the project is virtual
|
|
82
|
+
* and compiled by `ngtscVirtual` before Analog sees it, so Analog compiles nothing itself and
|
|
83
|
+
* only needs a valid configuration. Written atomically, since several projects start at once.
|
|
84
|
+
*/
|
|
85
|
+
function analogTsconfig(toolchainDir) {
|
|
86
|
+
const file = join(toolchainDir, ".uf-tmp", "tsconfig.analog.json");
|
|
87
|
+
const config = {
|
|
88
|
+
compilerOptions: {
|
|
89
|
+
target: "es2022",
|
|
90
|
+
module: "preserve",
|
|
91
|
+
moduleResolution: "bundler",
|
|
92
|
+
strict: true,
|
|
93
|
+
skipLibCheck: true
|
|
94
|
+
},
|
|
95
|
+
files: []
|
|
96
|
+
};
|
|
97
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
98
|
+
const temporary = `${file}.${process.pid}`;
|
|
99
|
+
writeFileSync(temporary, `${JSON.stringify(config, null, 2)}\n`);
|
|
100
|
+
renameSync(temporary, file);
|
|
101
|
+
return file;
|
|
102
|
+
}
|
|
103
|
+
export { viteConfig };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { parseExpressionSource, parseStatementsSource } from "@unframework.dev/codegen";
|
|
2
|
+
function isTypeNode(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && typeof value.type === "string" && typeof value.start === "number";
|
|
4
|
+
}
|
|
5
|
+
/** The parser has no entry for a type alone: it reads one as the type of an `as` expression. */
|
|
6
|
+
const PREFIX = "0 as ";
|
|
7
|
+
/** A type as written, parsed: offsets in its nodes are relative to `code`. */
|
|
8
|
+
function parseType(code) {
|
|
9
|
+
const { expression } = parseExpressionSource(`${PREFIX}${code}`);
|
|
10
|
+
const type = expression.typeAnnotation;
|
|
11
|
+
if (expression.type !== "TSAsExpression" || !isTypeNode(type)) throw new Error(`Cannot read \`${code}\` as a type.`);
|
|
12
|
+
return type;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The names of the types a parsed type or declaration refers to (`Item` in `Item[]`,
|
|
16
|
+
* `Array<Finish>`, `interface Props extends Base`).
|
|
17
|
+
*/
|
|
18
|
+
function referencedNames(node) {
|
|
19
|
+
const names = /* @__PURE__ */ new Set();
|
|
20
|
+
const visit = (value) => {
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
for (const item of value) visit(item);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!isTypeNode(value)) return;
|
|
26
|
+
if (value.type === "TSTypeReference") {
|
|
27
|
+
let name = value.typeName;
|
|
28
|
+
while (name.type === "TSQualifiedName") name = name.left;
|
|
29
|
+
if (typeof name.name === "string") names.add(name.name);
|
|
30
|
+
}
|
|
31
|
+
if (value.type === "TSInterfaceHeritage") {
|
|
32
|
+
let base = value.expression;
|
|
33
|
+
while (base.type === "MemberExpression") base = base.object;
|
|
34
|
+
if (typeof base.name === "string") names.add(base.name);
|
|
35
|
+
}
|
|
36
|
+
for (const [key, child] of Object.entries(value)) if (key !== "type" && typeof child === "object") visit(child);
|
|
37
|
+
};
|
|
38
|
+
visit(node);
|
|
39
|
+
return names;
|
|
40
|
+
}
|
|
41
|
+
/** A type declaration as written, parsed: offsets in its nodes are relative to its code. */
|
|
42
|
+
function parseDeclaration(code) {
|
|
43
|
+
const [statement] = parseStatementsSource(code).statements;
|
|
44
|
+
if (!isTypeNode(statement) || statement.type !== "TSInterfaceDeclaration" && statement.type !== "TSTypeAliasDeclaration") throw new Error(`Cannot read \`${code}\` as a type declaration.`);
|
|
45
|
+
return statement;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The type a declaration declares, as a type on its own: an interface's body (an object type) or
|
|
49
|
+
* an alias's right-hand side, read from the parsed declaration, since a header's type
|
|
50
|
+
* parameters may hold a `{` or an `=` of their own (`interface List<T extends { id: string }>`,
|
|
51
|
+
* `type P<T = string>`, ADR-0068).
|
|
52
|
+
*/
|
|
53
|
+
function declaredType(declaration) {
|
|
54
|
+
const node = parseDeclaration(declaration.code);
|
|
55
|
+
const type = node.type === "TSInterfaceDeclaration" ? node.body : node.typeAnnotation;
|
|
56
|
+
return declaration.code.slice(type.start, type.end);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The names a whole declaration refers to: its body's or right-hand side's, an interface's bases'
|
|
60
|
+
* and its type parameters' constraints and defaults (`Item` in `<T extends Item>`), but its own
|
|
61
|
+
* type parameters.
|
|
62
|
+
*/
|
|
63
|
+
function declarationReferences(declaration) {
|
|
64
|
+
const node = parseDeclaration(declaration.code);
|
|
65
|
+
const names = referencedNames(node);
|
|
66
|
+
const parameters = node.typeParameters?.params;
|
|
67
|
+
for (const parameter of parameters ?? []) {
|
|
68
|
+
const name = parameter.name?.name;
|
|
69
|
+
if (typeof name === "string") names.delete(name);
|
|
70
|
+
}
|
|
71
|
+
return names;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The type declarations a component's file declares, in source order: every exported one of
|
|
75
|
+
* its closure (`component.types`), which consumers type their props with (ADR-0068), and every
|
|
76
|
+
* other one the output reads (`roots`: an input's or an output's type, the setup's annotations
|
|
77
|
+
* and code) or a declared one refers to. An Angular component types each input by its member's
|
|
78
|
+
* type, so a props interface the source does not export would be declared and never used, which
|
|
79
|
+
* the L5 baseline rejects (`no-unused-vars`), unless an input reads a member from it by indexed
|
|
80
|
+
* access ({@link propType}).
|
|
81
|
+
*/
|
|
82
|
+
function declaredTypes(closure, roots) {
|
|
83
|
+
const byName = new Map(closure.map((declaration) => [declaration.name, declaration]));
|
|
84
|
+
const kept = /* @__PURE__ */ new Set();
|
|
85
|
+
const pending = [...roots, ...closure.filter(({ exported }) => exported).map((declaration) => declaration.name)];
|
|
86
|
+
for (let name = pending.pop(); name !== void 0; name = pending.pop()) {
|
|
87
|
+
const declaration = byName.get(name);
|
|
88
|
+
if (!declaration || kept.has(name)) continue;
|
|
89
|
+
kept.add(name);
|
|
90
|
+
pending.push(...declarationReferences(declaration));
|
|
91
|
+
}
|
|
92
|
+
return closure.filter((declaration) => kept.has(declaration.name));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* A type without `undefined`: the value type of an input with a default, which the input's
|
|
96
|
+
* transform never lets through (ADR-0034). A union's `undefined` members are left out as
|
|
97
|
+
* written; a reference to a local alias that admits `undefined`, or an indexed access, becomes
|
|
98
|
+
* `Exclude<…, undefined>`.
|
|
99
|
+
*/
|
|
100
|
+
function withoutUndefined(code, declarations) {
|
|
101
|
+
return without(code, declarations, false);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* A type without `undefined` and `null`, as `??` and `!` leave it: a union's nullish members are
|
|
105
|
+
* left out as written; a reference to a local alias that admits either, or an indexed access,
|
|
106
|
+
* becomes `NonNullable<…>`.
|
|
107
|
+
*/
|
|
108
|
+
function withoutNullish(code, declarations) {
|
|
109
|
+
return without(code, declarations, true);
|
|
110
|
+
}
|
|
111
|
+
function without(code, declarations, nullish) {
|
|
112
|
+
const left = (type) => type.type === "TSUndefinedKeyword" || nullish && type.type === "TSNullKeyword";
|
|
113
|
+
const aliases = new Map(declarations.filter((declaration) => declaration.code.startsWith("type")).map((declaration) => [declaration.name, declaredType(declaration)]));
|
|
114
|
+
const admits = (type, seen) => {
|
|
115
|
+
if (left(type)) return true;
|
|
116
|
+
switch (type.type) {
|
|
117
|
+
case "TSIndexedAccessType": return true;
|
|
118
|
+
case "TSUnionType": return type.types.some((member) => admits(member, seen));
|
|
119
|
+
case "TSTypeReference": {
|
|
120
|
+
const name = type.typeName.name;
|
|
121
|
+
if (typeof name !== "string" || seen.has(name)) return false;
|
|
122
|
+
const alias = aliases.get(name);
|
|
123
|
+
return alias !== void 0 && admits(parseType(alias), /* @__PURE__ */ new Set([...seen, name]));
|
|
124
|
+
}
|
|
125
|
+
default: return false;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
const type = parseType(code);
|
|
129
|
+
const members = type.type === "TSUnionType" ? type.types : [type];
|
|
130
|
+
const kept = members.filter((member) => !left(member));
|
|
131
|
+
if (!kept.length) return "never";
|
|
132
|
+
const text = (member) => {
|
|
133
|
+
const written = code.slice(member.start - 5, member.end - 5);
|
|
134
|
+
return kept.length > 1 && /=>|\bextends\b|^\s*new\b/.test(written) && !written.startsWith("(") ? `(${written})` : written;
|
|
135
|
+
};
|
|
136
|
+
const written = kept.length === members.length ? code.trim() : kept.map(text).join(" | ");
|
|
137
|
+
if (!kept.some((member) => admits(member, /* @__PURE__ */ new Set()))) return written;
|
|
138
|
+
return nullish ? `NonNullable<${written}>` : `Exclude<${written}, undefined>`;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* The element type of an array type as written (`Item[]`, `readonly Item[]`, `Array<Item>`,
|
|
142
|
+
* `ReadonlyArray<Item>`), or `undefined` for any other type.
|
|
143
|
+
*/
|
|
144
|
+
function arrayElement(code) {
|
|
145
|
+
let type = parseType(code);
|
|
146
|
+
const text = (node) => code.slice(node.start - 5, node.end - 5);
|
|
147
|
+
if (type.type === "TSTypeOperator" && type.operator === "readonly") type = type.typeAnnotation;
|
|
148
|
+
if (type.type === "TSArrayType") return text(type.elementType);
|
|
149
|
+
if (type.type === "TSTypeReference") {
|
|
150
|
+
const name = type.typeName.name;
|
|
151
|
+
const parameters = type.typeArguments?.params ?? [];
|
|
152
|
+
if ((name === "Array" || name === "ReadonlyArray") && parameters.length === 1) return text(parameters[0]);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The type an input is declared with (ADR-0067). A member the props type declares keeps its
|
|
157
|
+
* text as written. A member another declaration declares (an inherited, intersected, picked or
|
|
158
|
+
* imported one, `Prop.declaredBy`) is written in that file's names, which the component's module
|
|
159
|
+
* may not have, so it is read from the props type by indexed access, `ButtonProps["tone"]`: the
|
|
160
|
+
* output imports or declares the props type's names, and the utilities over it still apply.
|
|
161
|
+
*/
|
|
162
|
+
function propType(prop, component) {
|
|
163
|
+
const props = component.propsParameter?.type.code;
|
|
164
|
+
if (!prop.declaredBy || props === void 0) return prop.type.code;
|
|
165
|
+
const type = parseType(props);
|
|
166
|
+
return `${type.type === "TSTypeReference" || type.type === "TSTypeLiteral" ? props.trim() : `(${props.trim()})`}[${JSON.stringify(prop.name)}]`;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The value type of an input with a default, which its transform never lets be `undefined`
|
|
170
|
+
* (ADR-0034): a member's own text without `undefined` ({@link withoutUndefined}), or an indexed
|
|
171
|
+
* access without it, `Exclude<ButtonProps["tone"], undefined>`, since an optional member's
|
|
172
|
+
* indexed access admits `undefined`.
|
|
173
|
+
*/
|
|
174
|
+
function defaultedType(prop, component, declarations) {
|
|
175
|
+
const type = propType(prop, component);
|
|
176
|
+
return prop.declaredBy && type !== prop.type.code ? `Exclude<${type}, undefined>` : withoutUndefined(type, declarations);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* A type with each of a component's type parameters read as its constraint: the type of a scoped
|
|
180
|
+
* slot's context, which a slot directive cannot infer, since ngtsc infers a component's type
|
|
181
|
+
* parameters from its inputs alone (`generic-slot-props`, UF4004, ADR-0068). A parameter without
|
|
182
|
+
* one reads as `any`, as ngtsc types a generic context it cannot infer
|
|
183
|
+
* (`TcbGenericContextBehavior.FallbackToAny`): as `unknown`, a fill could read none of its
|
|
184
|
+
* members. A constraint that names another parameter reads it the same way.
|
|
185
|
+
*/
|
|
186
|
+
function atConstraints(code, parameters) {
|
|
187
|
+
if (!parameters.length) return code;
|
|
188
|
+
const byName = new Map(parameters.map((parameter) => [parameter.name, parameter]));
|
|
189
|
+
const read = (text, seen) => {
|
|
190
|
+
const found = [];
|
|
191
|
+
const visit = (value, shadowed) => {
|
|
192
|
+
if (Array.isArray(value)) {
|
|
193
|
+
for (const item of value) visit(item, shadowed);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if (!isTypeNode(value)) return;
|
|
197
|
+
const name = value.typeName;
|
|
198
|
+
if (value.type === "TSTypeReference" && name?.type === "Identifier" && typeof name.name === "string" && byName.has(name.name) && !shadowed.has(name.name) && !value.typeArguments) {
|
|
199
|
+
found.push({
|
|
200
|
+
start: value.start,
|
|
201
|
+
end: value.end,
|
|
202
|
+
name: name.name
|
|
203
|
+
});
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (value.type === "TSConditionalType") {
|
|
207
|
+
const inferred = /* @__PURE__ */ new Set([...shadowed, ...inferredNames(value.extendsType)]);
|
|
208
|
+
visit(value.checkType, shadowed);
|
|
209
|
+
visit(value.extendsType, inferred);
|
|
210
|
+
visit(value.trueType, inferred);
|
|
211
|
+
visit(value.falseType, shadowed);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const own = [...declaredNames(value)];
|
|
215
|
+
const inner = own.length ? /* @__PURE__ */ new Set([...shadowed, ...own]) : shadowed;
|
|
216
|
+
for (const [key, child] of Object.entries(value)) if (key !== "type" && typeof child === "object") visit(child, inner);
|
|
217
|
+
};
|
|
218
|
+
visit(parseType(text), /* @__PURE__ */ new Set());
|
|
219
|
+
let result = text;
|
|
220
|
+
for (const { start, end, name } of found.toSorted((a, b) => b.start - a.start)) {
|
|
221
|
+
const constraint = byName.get(name).constraint?.code;
|
|
222
|
+
const replaced = constraint === void 0 || seen.has(name) ? "any" : grouped(read(constraint, /* @__PURE__ */ new Set([...seen, name])));
|
|
223
|
+
result = `${result.slice(0, start - 5)}${replaced}${result.slice(end - 5)}`;
|
|
224
|
+
}
|
|
225
|
+
return result;
|
|
226
|
+
};
|
|
227
|
+
return read(code, /* @__PURE__ */ new Set());
|
|
228
|
+
}
|
|
229
|
+
/** The type parameters a node declares for its own subtree: `<T>(x: T) => T`, `[K in …]`. */
|
|
230
|
+
function declaredNames(node) {
|
|
231
|
+
if (node.type === "TSMappedType") {
|
|
232
|
+
const key = node.key;
|
|
233
|
+
return typeof key?.name === "string" ? [key.name] : [];
|
|
234
|
+
}
|
|
235
|
+
const declaration = node.typeParameters;
|
|
236
|
+
if (declaration?.type !== "TSTypeParameterDeclaration") return [];
|
|
237
|
+
return (declaration.params ?? []).map((parameter) => parameter.name?.name).filter((name) => typeof name === "string");
|
|
238
|
+
}
|
|
239
|
+
/** The names a conditional type's extends clause declares with `infer`. */
|
|
240
|
+
function inferredNames(value) {
|
|
241
|
+
if (Array.isArray(value)) return value.flatMap(inferredNames);
|
|
242
|
+
if (!isTypeNode(value)) return [];
|
|
243
|
+
if (value.type === "TSInferType") {
|
|
244
|
+
const name = value.typeParameter?.name?.name;
|
|
245
|
+
return typeof name === "string" ? [name] : [];
|
|
246
|
+
}
|
|
247
|
+
return Object.entries(value).flatMap(([key, child]) => key !== "type" && typeof child === "object" ? inferredNames(child) : []);
|
|
248
|
+
}
|
|
249
|
+
/** A type in parentheses where an operator or a suffix (`[]`, `[K]`) would bind into it. */
|
|
250
|
+
function grouped(type) {
|
|
251
|
+
const text = type.trim();
|
|
252
|
+
return enclosed(text) || !/[|&]|=>|\bextends\b|^(?:new|keyof|readonly|typeof|infer)\b/.test(text) ? text : `(${text})`;
|
|
253
|
+
}
|
|
254
|
+
/** Whether a type's text is one parenthesised type, `("a" | "b")`, which its parser drops. */
|
|
255
|
+
function enclosed(text) {
|
|
256
|
+
if (!text.startsWith("(")) return false;
|
|
257
|
+
let depth = 0;
|
|
258
|
+
for (let index = 0; index < text.length; index += 1) if (text[index] === "(") depth += 1;
|
|
259
|
+
else if (text[index] === ")" && --depth === 0) return index === text.length - 1;
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
export { arrayElement, atConstraints, declaredTypes, defaultedType, propType, withoutNullish, withoutUndefined };
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unframework.dev/target-angular",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "The Angular 22 target of the Unframework compiler: emits idiomatic Angular components from the portable IR, declares their capabilities, and provides the toolchain the tests use.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"angular",
|
|
7
|
+
"compiler",
|
|
8
|
+
"unframework"
|
|
9
|
+
],
|
|
10
|
+
"homepage": "https://github.com/uxfront-com/unframework/tree/main/packages/target-angular#readme",
|
|
11
|
+
"bugs": "https://github.com/uxfront-com/unframework/issues",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/uxfront-com/unframework.git",
|
|
16
|
+
"directory": "packages/target-angular"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./toolchain": {
|
|
29
|
+
"types": "./dist/toolchain/index.d.ts",
|
|
30
|
+
"default": "./dist/toolchain/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./toolchain/client": {
|
|
33
|
+
"types": "./dist/toolchain/client.d.ts",
|
|
34
|
+
"default": "./dist/toolchain/client.js"
|
|
35
|
+
},
|
|
36
|
+
"./toolchain/server": {
|
|
37
|
+
"types": "./dist/toolchain/server.d.ts",
|
|
38
|
+
"default": "./dist/toolchain/server.js"
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@unframework.dev/codegen": "0.0.0",
|
|
47
|
+
"@unframework.dev/ir": "0.0.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@angular/common": "22.2.1",
|
|
51
|
+
"@angular/compiler": "22.2.1",
|
|
52
|
+
"@angular/core": "22.2.1",
|
|
53
|
+
"@angular/platform-browser": "22.2.1",
|
|
54
|
+
"@angular/platform-server": "22.2.1",
|
|
55
|
+
"@types/node": "^24.19.0",
|
|
56
|
+
"@uxfront/typescript-config": "^0.1.0",
|
|
57
|
+
"@vitest/browser-playwright": "5.0.3",
|
|
58
|
+
"playwright": "1.63.0",
|
|
59
|
+
"rxjs": "^7.8.2",
|
|
60
|
+
"tsdown": "^0.23.0",
|
|
61
|
+
"tslib": "^2.8.1",
|
|
62
|
+
"vite": "8.3.1",
|
|
63
|
+
"vitest": "5.0.3"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsdown",
|
|
67
|
+
"check-types": "tsc --noEmit",
|
|
68
|
+
"test": "vitest run"
|
|
69
|
+
}
|
|
6
70
|
}
|