@openelement/compiler 1.0.0-alpha.12 → 1.0.0-alpha.14
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 +2 -2
- package/src/compiler.d.ts +2 -1
- package/src/compiler.js +3 -2
- package/src/internal/compiler/compiled-artifact-style-requests.d.ts +50 -0
- package/src/internal/compiler/compiled-artifact-style-requests.js +129 -0
- package/src/internal/compiler/plugin.js +5 -3
- package/src/internal/compiler/semantic-core/analyze-module.js +3 -2
- package/src/internal/compiler/semantic-core/compile.js +1 -1
- package/src/internal/compiler/semantic-core/emit-module.js +56 -2
- package/src/internal/compiler/semantic-core/lower-program.js +1 -1
- package/src/internal/compiler/semantic-core/module-analysis.d.ts +19 -1
- package/src/internal/compiler/semantic-core/module-analysis.js +33 -3
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openelement/compiler",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/compiler.js",
|
|
6
6
|
"types": "./src/compiler.d.ts",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@openelement/protocol": "1.0.0-alpha.
|
|
47
|
+
"@openelement/protocol": "1.0.0-alpha.14",
|
|
48
48
|
"typescript": "6.0.3"
|
|
49
49
|
}
|
|
50
50
|
}
|
package/src/compiler.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { ModuleSemanticFacts, ModuleSemanticsOptions, ModuleVocabularyDescriptor
|
|
|
2
2
|
import { CompiledElementError, ElementCompilerDiagnostic } from "./internal/compiler/semantic-core/compiler-diagnostics.js";
|
|
3
3
|
import { CompileElementResult, compileElementProgram } from "./internal/compiler/semantic-core/compile.js";
|
|
4
4
|
import { COMPILED_ELEMENT_MARKER, compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, stableModuleId, stripInlineSourceMapComment } from "./internal/compiler/plugin.js";
|
|
5
|
+
import { compiledArtifactStyleRequests } from "./internal/compiler/compiled-artifact-style-requests.js";
|
|
5
6
|
import { RegisteredStyleRequest, clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId } from "./internal/compiler/style-requests.js";
|
|
6
7
|
import { EmittedModuleDiagnostic, EmittedModuleTypeCheckOptions, emittedModuleTypeChecks, typeCheckEmittedModule } from "./internal/compiler/semantic-core/type-check.js";
|
|
7
8
|
import { validatePartProgram } from "@openelement/protocol/part-program";
|
|
8
|
-
export { COMPILED_ELEMENT_MARKER, type CompileElementResult, CompiledElementError, type ElementCompilerDiagnostic, type EmittedModuleDiagnostic, type EmittedModuleTypeCheckOptions, type ModuleSemanticFacts, type ModuleSemanticsOptions, type ModuleVocabularyDescriptor, type RegisteredStyleRequest, type SemanticCoreOptions, type StaticSidecarDescriptor, analyzeModuleSemantics, clearStyleRequests, compileElementModule, compileElementProgram, compiledElementPlugin, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, typeCheckEmittedModule, validatePartProgram };
|
|
9
|
+
export { COMPILED_ELEMENT_MARKER, type CompileElementResult, CompiledElementError, type ElementCompilerDiagnostic, type EmittedModuleDiagnostic, type EmittedModuleTypeCheckOptions, type ModuleSemanticFacts, type ModuleSemanticsOptions, type ModuleVocabularyDescriptor, type RegisteredStyleRequest, type SemanticCoreOptions, type StaticSidecarDescriptor, analyzeModuleSemantics, clearStyleRequests, compileElementModule, compileElementProgram, compiledArtifactStyleRequests, compiledElementPlugin, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, typeCheckEmittedModule, validatePartProgram };
|
package/src/compiler.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { CompiledElementError } from "./internal/compiler/semantic-core/compiler-diagnostics.js";
|
|
2
1
|
import { analyzeModuleSemantics } from "./internal/compiler/semantic-core/module-analysis.js";
|
|
2
|
+
import { CompiledElementError } from "./internal/compiler/semantic-core/compiler-diagnostics.js";
|
|
3
3
|
import { compileElementProgram } from "./internal/compiler/semantic-core/compile.js";
|
|
4
4
|
import { emittedModuleTypeChecks, typeCheckEmittedModule } from "./internal/compiler/semantic-core/type-check.js";
|
|
5
|
+
import { compiledArtifactStyleRequests } from "./internal/compiler/compiled-artifact-style-requests.js";
|
|
5
6
|
import { clearStyleRequests, getStyleRequest, hasStyleImporter, registerStyleRequest, styleEdgesForFile, styleRequestFile, styleRequestModuleId } from "./internal/compiler/style-requests.js";
|
|
6
7
|
import { COMPILED_ELEMENT_MARKER, compileElementModule, compiledElementPlugin, hasElementDecoratorApplication, isCompiledElementModule, stableModuleId, stripInlineSourceMapComment } from "./internal/compiler/plugin.js";
|
|
7
8
|
import { validatePartProgram } from "@openelement/protocol/part-program";
|
|
8
|
-
export { COMPILED_ELEMENT_MARKER, CompiledElementError, analyzeModuleSemantics, clearStyleRequests, compileElementModule, compileElementProgram, compiledElementPlugin, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, typeCheckEmittedModule, validatePartProgram };
|
|
9
|
+
export { COMPILED_ELEMENT_MARKER, CompiledElementError, analyzeModuleSemantics, clearStyleRequests, compileElementModule, compileElementProgram, compiledArtifactStyleRequests, compiledElementPlugin, emittedModuleTypeChecks, getStyleRequest, hasElementDecoratorApplication, hasStyleImporter, isCompiledElementModule, registerStyleRequest, stableModuleId, stripInlineSourceMapComment, styleEdgesForFile, styleRequestFile, styleRequestModuleId, typeCheckEmittedModule, validatePartProgram };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
//#region src/internal/compiler/compiled-artifact-style-requests.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The style edges of an already-compiled artifact module — the second admitted
|
|
4
|
+
* input class of the compiled-element transform (#1558, alpha.13 I1/KR-10).
|
|
5
|
+
*
|
|
6
|
+
* Packages ship the compiler's OWN emitted modules (the release pack pipeline
|
|
7
|
+
* stages `compilePackageElementModules` output),
|
|
8
|
+
* so a consumer build that resolves one has no authored grammar left to
|
|
9
|
+
* compile — but the module's `.css` imports are real edges of the style asset
|
|
10
|
+
* protocol, and the style-asset intercept (router) answers only edges the
|
|
11
|
+
* compiled-element transform registered. Without this recovery the packaged
|
|
12
|
+
* module's sheet imports fall through to the bundler's own CSS channel, whose
|
|
13
|
+
* module has no default export: the island chunk's `static styles` binding
|
|
14
|
+
* then fails the build at MISSING_EXPORT (reproduced with the packed
|
|
15
|
+
* `@openelement/ui`, /tmp/allpkgs app: `"default" is not exported by
|
|
16
|
+
* ".../open-button.css"`), because a workspace build compiled the `.tsx`
|
|
17
|
+
* source and never needed the artifact path.
|
|
18
|
+
*
|
|
19
|
+
* The accepted shape is the ONE style authoring grammar
|
|
20
|
+
* (semantic-core/style-imports.ts): a relative `.css` default import arrayed
|
|
21
|
+
* in the class's `static styles`. The extraction here is deliberately the
|
|
22
|
+
* PERMISSIVE read of that shape: it claims exactly the relative `.css`
|
|
23
|
+
* default imports arrayed as plain bindings, and leaves every other member of
|
|
24
|
+
* a `static styles` array untouched. Artifacts compiled before #1558
|
|
25
|
+
* (legacy inline sheets that referenced a `StyleSheet` built in a plain
|
|
26
|
+
* module) therefore keep riding the old path unchanged — a strict admission
|
|
27
|
+
* here would break every previously-published package for no protocol gain.
|
|
28
|
+
*
|
|
29
|
+
* One writer (P8): this module only recovers edge specifiers; the caller (the
|
|
30
|
+
* compiled-element transform's host binding) registers them, exactly as it
|
|
31
|
+
* registers an authored compile's `styleRequests`.
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* The `.css` style edges of one compiled artifact module, in authored order —
|
|
35
|
+
* or undefined when the module is not a shape this recovery adopts. Never
|
|
36
|
+
* throws and never guesses: a module without the Part Program marker, without
|
|
37
|
+
* a `.css` specifier, without an arrayed `static styles`, or with a sheet
|
|
38
|
+
* import no class arrays is left entirely to the graph's normal channels,
|
|
39
|
+
* exactly as before this recovery existed. Adoption is all-or-nothing per
|
|
40
|
+
* module, so it can never turn a working module into a fail-closed intercept
|
|
41
|
+
* while half its edges stay unregistered.
|
|
42
|
+
*
|
|
43
|
+
* The read tolerates both artifact forms the pack pipeline produces: the
|
|
44
|
+
* compiler's raw emission (TypeScript syntax, e.g. a `type`-annotated styles
|
|
45
|
+
* member) and the packed module (types stripped by the packer). The AST parse
|
|
46
|
+
* is ScriptKind.TSX for exactly that reason — a diagnostics gate on the file
|
|
47
|
+
* extension would refuse the raw form and silently drop its edges.
|
|
48
|
+
*/
|
|
49
|
+
export declare function compiledArtifactStyleRequests(source: string, fileName: string): readonly string[] | undefined;
|
|
50
|
+
//#endregion
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { typescriptParser } from "./semantic-core/parse-module.js";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
//#region src/internal/compiler/compiled-artifact-style-requests.ts
|
|
4
|
+
/**
|
|
5
|
+
* The style edges of an already-compiled artifact module — the second admitted
|
|
6
|
+
* input class of the compiled-element transform (#1558, alpha.13 I1/KR-10).
|
|
7
|
+
*
|
|
8
|
+
* Packages ship the compiler's OWN emitted modules (the release pack pipeline
|
|
9
|
+
* stages `compilePackageElementModules` output),
|
|
10
|
+
* so a consumer build that resolves one has no authored grammar left to
|
|
11
|
+
* compile — but the module's `.css` imports are real edges of the style asset
|
|
12
|
+
* protocol, and the style-asset intercept (router) answers only edges the
|
|
13
|
+
* compiled-element transform registered. Without this recovery the packaged
|
|
14
|
+
* module's sheet imports fall through to the bundler's own CSS channel, whose
|
|
15
|
+
* module has no default export: the island chunk's `static styles` binding
|
|
16
|
+
* then fails the build at MISSING_EXPORT (reproduced with the packed
|
|
17
|
+
* `@openelement/ui`, /tmp/allpkgs app: `"default" is not exported by
|
|
18
|
+
* ".../open-button.css"`), because a workspace build compiled the `.tsx`
|
|
19
|
+
* source and never needed the artifact path.
|
|
20
|
+
*
|
|
21
|
+
* The accepted shape is the ONE style authoring grammar
|
|
22
|
+
* (semantic-core/style-imports.ts): a relative `.css` default import arrayed
|
|
23
|
+
* in the class's `static styles`. The extraction here is deliberately the
|
|
24
|
+
* PERMISSIVE read of that shape: it claims exactly the relative `.css`
|
|
25
|
+
* default imports arrayed as plain bindings, and leaves every other member of
|
|
26
|
+
* a `static styles` array untouched. Artifacts compiled before #1558
|
|
27
|
+
* (legacy inline sheets that referenced a `StyleSheet` built in a plain
|
|
28
|
+
* module) therefore keep riding the old path unchanged — a strict admission
|
|
29
|
+
* here would break every previously-published package for no protocol gain.
|
|
30
|
+
*
|
|
31
|
+
* One writer (P8): this module only recovers edge specifiers; the caller (the
|
|
32
|
+
* compiled-element transform's host binding) registers them, exactly as it
|
|
33
|
+
* registers an authored compile's `styleRequests`.
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* The cheap prefilter every emitted artifact carries: the Part Program's
|
|
37
|
+
* module-local binding name (`const __partProgram = ...` plus the
|
|
38
|
+
* `static __partProgram` member). Modules without it are never parsed, so the
|
|
39
|
+
* recovery costs nothing on ordinary graph nodes.
|
|
40
|
+
*/
|
|
41
|
+
const COMPILED_ARTIFACT_MARKER = "__partProgram";
|
|
42
|
+
/**
|
|
43
|
+
* The second, equally cheap gate: only a module naming a `.css` specifier can
|
|
44
|
+
* carry sheet edges. Modules that merely mention the marker (the element
|
|
45
|
+
* runtime reads `ctor.__partProgram`) stop here instead of paying a parse.
|
|
46
|
+
*/
|
|
47
|
+
const CSS_SPECIFIER_HINT = /\.css['"]/;
|
|
48
|
+
/** The one static member whose initializer arrays the module's sheets. */
|
|
49
|
+
const STYLES_MEMBER_NAME = "styles";
|
|
50
|
+
/** Whether a specifier is the grammar's relative sheet form. */
|
|
51
|
+
function isRelativeSheetSpecifier(specifier) {
|
|
52
|
+
return specifier.endsWith(".css") && (specifier.startsWith("./") || specifier.startsWith("../"));
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The module's relative `.css` default imports, keyed by local binding.
|
|
56
|
+
* Side-effect, named and bare-specifier forms are not sheet edges of the
|
|
57
|
+
* grammar and are left to the bundler.
|
|
58
|
+
*/
|
|
59
|
+
function collectSheetBindings(sf) {
|
|
60
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
61
|
+
for (const statement of sf.statements) {
|
|
62
|
+
if (!ts.isImportDeclaration(statement)) continue;
|
|
63
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
64
|
+
const specifier = statement.moduleSpecifier.text;
|
|
65
|
+
if (!isRelativeSheetSpecifier(specifier)) continue;
|
|
66
|
+
const clause = statement.importClause;
|
|
67
|
+
if (!clause || clause.isTypeOnly || !clause.name) continue;
|
|
68
|
+
bindings.set(clause.name.text, specifier);
|
|
69
|
+
}
|
|
70
|
+
return bindings;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The `.css` style edges of one compiled artifact module, in authored order —
|
|
74
|
+
* or undefined when the module is not a shape this recovery adopts. Never
|
|
75
|
+
* throws and never guesses: a module without the Part Program marker, without
|
|
76
|
+
* a `.css` specifier, without an arrayed `static styles`, or with a sheet
|
|
77
|
+
* import no class arrays is left entirely to the graph's normal channels,
|
|
78
|
+
* exactly as before this recovery existed. Adoption is all-or-nothing per
|
|
79
|
+
* module, so it can never turn a working module into a fail-closed intercept
|
|
80
|
+
* while half its edges stay unregistered.
|
|
81
|
+
*
|
|
82
|
+
* The read tolerates both artifact forms the pack pipeline produces: the
|
|
83
|
+
* compiler's raw emission (TypeScript syntax, e.g. a `type`-annotated styles
|
|
84
|
+
* member) and the packed module (types stripped by the packer). The AST parse
|
|
85
|
+
* is ScriptKind.TSX for exactly that reason — a diagnostics gate on the file
|
|
86
|
+
* extension would refuse the raw form and silently drop its edges.
|
|
87
|
+
*/
|
|
88
|
+
function compiledArtifactStyleRequests(source, fileName) {
|
|
89
|
+
if (!source.includes(COMPILED_ARTIFACT_MARKER)) return void 0;
|
|
90
|
+
if (!CSS_SPECIFIER_HINT.test(source)) return void 0;
|
|
91
|
+
const { sourceFile: sf } = typescriptParser.parseModule(source, fileName);
|
|
92
|
+
const sheetBindings = collectSheetBindings(sf);
|
|
93
|
+
if (sheetBindings.size === 0) return void 0;
|
|
94
|
+
const edges = [];
|
|
95
|
+
const claimed = /* @__PURE__ */ new Set();
|
|
96
|
+
const claim = (element) => {
|
|
97
|
+
if (!ts.isIdentifier(element)) return false;
|
|
98
|
+
const specifier = sheetBindings.get(element.text);
|
|
99
|
+
if (specifier === void 0) return true;
|
|
100
|
+
if (!claimed.has(specifier)) {
|
|
101
|
+
claimed.add(specifier);
|
|
102
|
+
edges.push(specifier);
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
};
|
|
106
|
+
let shapeFollowed = true;
|
|
107
|
+
const visit = (node) => {
|
|
108
|
+
if (ts.isClassDeclaration(node) || ts.isClassExpression(node)) for (const member of node.members) {
|
|
109
|
+
if (!ts.isPropertyDeclaration(member)) continue;
|
|
110
|
+
if (!ts.isIdentifier(member.name) || member.name.text !== STYLES_MEMBER_NAME) continue;
|
|
111
|
+
if (!(ts.getModifiers(member) ?? []).some((modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword)) continue;
|
|
112
|
+
const initializer = member.initializer;
|
|
113
|
+
if (initializer === void 0) continue;
|
|
114
|
+
let expression = initializer;
|
|
115
|
+
while (ts.isParenthesizedExpression(expression) || ts.isAsExpression(expression)) expression = expression.expression;
|
|
116
|
+
if (!ts.isArrayLiteralExpression(expression)) {
|
|
117
|
+
if (sheetBindings.size > 0) shapeFollowed = false;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
for (const element of expression.elements) if (ts.isSpreadElement(element) || !claim(element)) shapeFollowed = false;
|
|
121
|
+
}
|
|
122
|
+
ts.forEachChild(node, visit);
|
|
123
|
+
};
|
|
124
|
+
visit(sf);
|
|
125
|
+
if (!shapeFollowed || claimed.size !== sheetBindings.size) return void 0;
|
|
126
|
+
return edges.length === 0 ? void 0 : edges;
|
|
127
|
+
}
|
|
128
|
+
//#endregion
|
|
129
|
+
export { compiledArtifactStyleRequests };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { analyzeModuleSemantics } from "./semantic-core/module-analysis.js";
|
|
1
2
|
import { diagnosticPluginError } from "./semantic-core/diagnostics/index.js";
|
|
2
3
|
import { CompiledElementError } from "./semantic-core/compiler-diagnostics.js";
|
|
3
|
-
import { analyzeModuleSemantics } from "./semantic-core/module-analysis.js";
|
|
4
4
|
import { compileElementProgram } from "./semantic-core/compile.js";
|
|
5
5
|
import { typeCheckEmittedModule } from "./semantic-core/type-check.js";
|
|
6
|
+
import { compiledArtifactStyleRequests } from "./compiled-artifact-style-requests.js";
|
|
6
7
|
import { registerStyleRequest, styleRequestFile, styleRequestModuleId } from "./style-requests.js";
|
|
7
8
|
//#region src/internal/compiler/plugin.ts
|
|
8
9
|
/** The authored substring every compiled element module contains (`@element(`), used as the cheap prefilter. */
|
|
@@ -101,13 +102,14 @@ function compiledElementPlugin(options = {}) {
|
|
|
101
102
|
try {
|
|
102
103
|
const moduleId = stableModuleId(id, viteRoot, workspaceRoot);
|
|
103
104
|
const compiled = compileElementModule(code, moduleId, { staticSidecars: options.staticSidecars });
|
|
104
|
-
|
|
105
|
-
if (
|
|
105
|
+
const styleRequests = compiled?.styleRequests ?? compiledArtifactStyleRequests(code, id);
|
|
106
|
+
if (styleRequests !== void 0) for (const specifier of styleRequests) registerStyleRequest({
|
|
106
107
|
moduleId: styleRequestModuleId(id, specifier),
|
|
107
108
|
importer: id,
|
|
108
109
|
specifier,
|
|
109
110
|
file: styleRequestFile(id, specifier)
|
|
110
111
|
});
|
|
112
|
+
if (!compiled) return null;
|
|
111
113
|
if (options.typeCheckEmitted) {
|
|
112
114
|
const diagnostics = typeCheckEmittedModule(compiled.code, moduleId, { paths: options.resolutionPaths });
|
|
113
115
|
const first = diagnostics[0];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { resolveCanonicalBaseClass } from "./module-analysis.js";
|
|
1
2
|
import { admitStylesInitializer, collectSheetImports } from "./style-imports.js";
|
|
2
3
|
import ts from "typescript";
|
|
3
4
|
import { camelToKebab, isSafeAttributeName } from "@openelement/protocol/forbidden-sinks";
|
|
@@ -448,8 +449,8 @@ function analyzeCompiledModule(sf, intrinsics, fail) {
|
|
|
448
449
|
}
|
|
449
450
|
const heritage = classNode.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
|
|
450
451
|
const heritageExpression = heritage?.types.length === 1 ? unwrapExpression(heritage.types[0].expression) : void 0;
|
|
451
|
-
const heritageResolution =
|
|
452
|
-
if (!heritageResolution?.canonical) fail(classNode.name ?? classNode, "OEC9003", `compiled classes must extend
|
|
452
|
+
const heritageResolution = resolveCanonicalBaseClass(intrinsics, heritageExpression);
|
|
453
|
+
if (!heritageResolution?.canonical) fail(classNode.name ?? classNode, "OEC9003", `compiled classes must extend a canonical import of OpenElement or ErrorBoundary from '@openelement/element' (found ${heritage?.types[0]?.expression.getText(sf) ?? "no base class"}` + (heritageResolution?.unsupported ? `; ${heritageResolution.unsupported}` : "") + ")");
|
|
453
454
|
const openElementLocalName = heritageResolution.localName;
|
|
454
455
|
if (!classNode.name) fail(classNode, "OEC9003", "compiled classes must be named");
|
|
455
456
|
const className = classNode.name.text;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { createModuleIntrinsicBindings } from "./module-analysis.js";
|
|
1
2
|
import { analyzeCompiledModule } from "./analyze-module.js";
|
|
2
3
|
import { CompiledElementError, failAt, parseDiagnostic } from "./compiler-diagnostics.js";
|
|
3
4
|
import { buildPartProgram } from "./emit-program.js";
|
|
4
5
|
import { Lowering } from "./lower-program.js";
|
|
5
|
-
import { createModuleIntrinsicBindings } from "./module-analysis.js";
|
|
6
6
|
import { emitCompiledModule } from "./emit-module.js";
|
|
7
7
|
import { typescriptParser } from "./parse-module.js";
|
|
8
8
|
import ts from "typescript";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { SYNTHESIZED_COMPUTED_LOCAL } from "./lower-program.js";
|
|
2
1
|
import { isCompileTimeOnlyImport } from "./module-analysis.js";
|
|
2
|
+
import { SYNTHESIZED_COMPUTED_LOCAL } from "./lower-program.js";
|
|
3
3
|
import { SourceMapSegmentBuilder } from "./source-map.js";
|
|
4
4
|
import ts from "typescript";
|
|
5
5
|
import { COMPILED_MODULE_ABI_VERSION, PART_PROGRAM_VERSION } from "@openelement/protocol/part-program";
|
|
@@ -41,6 +41,25 @@ function encodeBase64(value) {
|
|
|
41
41
|
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
42
42
|
return btoa(binary);
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* The platform lifecycle callbacks whose authored bodies the compiler wraps
|
|
46
|
+
* in the error-boundary capture contract. These are the callbacks the platform
|
|
47
|
+
* invokes outside any application stack — a throw inside them would otherwise
|
|
48
|
+
* escape straight to window.onerror with no boundary the way a render throw
|
|
49
|
+
* in React reaches the nearest error boundary. Everything else (event
|
|
50
|
+
* handlers, plain author methods) keeps the verbatim emission: those already
|
|
51
|
+
* run inside caller stacks, and the runtime capture points (the facade
|
|
52
|
+
* property setter, the kernel update sink) own their failure modes.
|
|
53
|
+
*/
|
|
54
|
+
const CAPTURE_WRAPPED_LIFECYCLE_CALLBACKS = /* @__PURE__ */ new Set([
|
|
55
|
+
"connectedCallback",
|
|
56
|
+
"disconnectedCallback",
|
|
57
|
+
"adoptedCallback",
|
|
58
|
+
"attributeChangedCallback",
|
|
59
|
+
"formAssociatedCallback",
|
|
60
|
+
"formResetCallback",
|
|
61
|
+
"formStateRestoreCallback"
|
|
62
|
+
]);
|
|
44
63
|
function generatedHandlerText(handler) {
|
|
45
64
|
const action = handler.action;
|
|
46
65
|
if (action.kind === "method") return ` ${handler.name}(): void { this.${action.name}(); }`;
|
|
@@ -233,7 +252,42 @@ function emitCompiledModule(input) {
|
|
|
233
252
|
const accessibility = field.accessibility ? `${field.accessibility} ` : "";
|
|
234
253
|
pushDerivedLine(` ${accessibility}${field.name}${field.typeText} = `, field.initializerText, ";", field.node.name, 2 + accessibility.length, field.name, field.node.initializer);
|
|
235
254
|
}
|
|
236
|
-
|
|
255
|
+
/**
|
|
256
|
+
* Emit one platform lifecycle callback with the boundary-capture wrapper
|
|
257
|
+
* (P2: the wrapper is a compile-time cost; the runtime delta is one method
|
|
258
|
+
* call on the failure path only). The body bytes and their columns are
|
|
259
|
+
* emitted UNCHANGED from the verbatim copy — body line i keeps the authored
|
|
260
|
+
* text at the authored column, so the continuation mapping stays
|
|
261
|
+
* byte-identical to what `pushVerbatim` produced; the inserted `try {` and
|
|
262
|
+
* `} catch` lines are pure scaffolding and stay unmapped (consumers fall
|
|
263
|
+
* through to the nearest real construct, the method's own first-line
|
|
264
|
+
* mapping). Prior art (P7): React error boundaries capture lifecycle and
|
|
265
|
+
* render throws at runtime; the compiled difference is that this capture
|
|
266
|
+
* point is materialized once, at compile time, around exactly the platform
|
|
267
|
+
* entry points — `_captureError` on the base class then performs the same
|
|
268
|
+
* nearest-boundary climb React's component stack does.
|
|
269
|
+
*/
|
|
270
|
+
const pushLifecycleCapture = (method, name) => {
|
|
271
|
+
const body = method.body;
|
|
272
|
+
const methodText = method.getText(sf);
|
|
273
|
+
const header = methodText.slice(0, body.getStart(sf) - method.getStart(sf));
|
|
274
|
+
const bodyText = methodText.slice(header.length + 1, methodText.length - 1).replace(/\n[ \t]*$/, "");
|
|
275
|
+
const firstGeneratedLine = codeLines.length + 1;
|
|
276
|
+
push(` ${header}{`);
|
|
277
|
+
mapLineAt(firstGeneratedLine, 2, method, name);
|
|
278
|
+
push(` try {${bodyText}`);
|
|
279
|
+
mapContinuationLines(bodyText, firstGeneratedLine + 1, body, 0);
|
|
280
|
+
push(` } catch (error) { if (!this._captureError(error)) throw error; }`);
|
|
281
|
+
push(` }`);
|
|
282
|
+
};
|
|
283
|
+
for (const method of methods) {
|
|
284
|
+
const name = method.name.text;
|
|
285
|
+
if (CAPTURE_WRAPPED_LIFECYCLE_CALLBACKS.has(name)) {
|
|
286
|
+
pushLifecycleCapture(method, name);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
pushVerbatim(method.getText(sf), method, " ", name);
|
|
290
|
+
}
|
|
237
291
|
for (const handler of lowering.generatedHandlers) {
|
|
238
292
|
const handlerLine = codeLines.length + 1;
|
|
239
293
|
push(generatedHandlerText(handler));
|
|
@@ -268,7 +268,7 @@ var Lowering = class {
|
|
|
268
268
|
lowerElement(tagNameNode, attributes, children, path, sourceNode, staticOnly = false) {
|
|
269
269
|
const tag = tagNameNode.getText(this.sf);
|
|
270
270
|
const isCustomHost = /^[a-z][a-z0-9]*(-[a-z0-9]+)+$/.test(tag);
|
|
271
|
-
if (!/^[a-z][a-z0-9]*$/.test(tag) && !isCustomHost) this.fail(tagNameNode, "OEC9010", `component tag <${tag}> is outside the compiler grammar (intrinsic lowercase elements and custom-element hosts only)`);
|
|
271
|
+
if (!/^[a-z][a-z0-9]*$/.test(tag) && !isCustomHost) this.fail(tagNameNode, "OEC9010", `component tag <${tag}> is outside the compiler grammar (intrinsic lowercase elements and custom-element hosts only) — custom elements compose by tag name — use the tag string, not the class value`);
|
|
272
272
|
const tagReason = forbiddenSinkReason("tag", tag);
|
|
273
273
|
if (tagReason !== null) this.fail(tagNameNode, "OEC9010", tagReason);
|
|
274
274
|
const attrs = [];
|
|
@@ -44,7 +44,7 @@ export interface ModuleSemanticFacts {
|
|
|
44
44
|
* {@link StaticSidecarDescriptor}s, never through compiler-side knowledge of
|
|
45
45
|
* another package.
|
|
46
46
|
*/
|
|
47
|
-
export type IntrinsicName = 'element' | 'property' | 'OpenElement' | 'computed' | 'trustedHtml';
|
|
47
|
+
export type IntrinsicName = 'element' | 'property' | 'OpenElement' | 'ErrorBoundary' | 'computed' | 'trustedHtml';
|
|
48
48
|
/** Host-supplied admission extensions for the semantic core. */
|
|
49
49
|
export interface SemanticCoreOptions {
|
|
50
50
|
/**
|
|
@@ -91,6 +91,24 @@ export interface ModuleIntrinsicBindings {
|
|
|
91
91
|
/** True for a runtime (non-type-only) named import, aliases followed. */
|
|
92
92
|
isRuntimeNamedImport(localName: string, module: string | readonly string[], imported: string): boolean;
|
|
93
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* The compiler's admitted base classes, in resolution order (alpha.13 I2,
|
|
96
|
+
* KR-11): the README documents subclassing `ErrorBoundary`, and the compiler
|
|
97
|
+
* admits that authoring form exactly as it admits `OpenElement` — both must
|
|
98
|
+
* be runtime named imports of '@openelement/element', so the binding-source
|
|
99
|
+
* check is unchanged. `ErrorBoundary extends OpenElement`, so everything
|
|
100
|
+
* downstream of admission sees the same class shape.
|
|
101
|
+
*/
|
|
102
|
+
declare const CANONICAL_BASE_CLASS_INTRINSICS: readonly IntrinsicName[];
|
|
103
|
+
/**
|
|
104
|
+
* Resolve one heritage clause against the admitted base classes. Returns the
|
|
105
|
+
* first canonical resolution; when none is canonical, the first resolution
|
|
106
|
+
* that names why the provenance is unsupported (so the caller's fail-closed
|
|
107
|
+
* diagnostic keeps carrying the reason). A plain identifier whose text is one
|
|
108
|
+
* of the admitted names is tried first, so a conflicting-binding diagnostic
|
|
109
|
+
* names the base class the author wrote rather than the other one.
|
|
110
|
+
*/
|
|
111
|
+
declare function resolveCanonicalBaseClass(bindings: ModuleIntrinsicBindings, heritage: ts.Expression | undefined): IntrinsicResolution | undefined;
|
|
94
112
|
/**
|
|
95
113
|
* Resolve the module-scope import/declaration bindings of one source file
|
|
96
114
|
* once, so decorator, heritage and factory use sites all answer provenance
|
|
@@ -5,6 +5,7 @@ const INTRINSIC_MODULES = {
|
|
|
5
5
|
element: ["@openelement/element"],
|
|
6
6
|
property: ["@openelement/element"],
|
|
7
7
|
OpenElement: ["@openelement/element"],
|
|
8
|
+
ErrorBoundary: ["@openelement/element"],
|
|
8
9
|
computed: ["@openelement/element"],
|
|
9
10
|
trustedHtml: ["@openelement/element"]
|
|
10
11
|
};
|
|
@@ -28,6 +29,35 @@ function isCompileTimeOnlyImport(module, imported) {
|
|
|
28
29
|
return COMPILE_TIME_ONLY_IMPORTS[module]?.includes(imported) ?? false;
|
|
29
30
|
}
|
|
30
31
|
/**
|
|
32
|
+
* The compiler's admitted base classes, in resolution order (alpha.13 I2,
|
|
33
|
+
* KR-11): the README documents subclassing `ErrorBoundary`, and the compiler
|
|
34
|
+
* admits that authoring form exactly as it admits `OpenElement` — both must
|
|
35
|
+
* be runtime named imports of '@openelement/element', so the binding-source
|
|
36
|
+
* check is unchanged. `ErrorBoundary extends OpenElement`, so everything
|
|
37
|
+
* downstream of admission sees the same class shape.
|
|
38
|
+
*/
|
|
39
|
+
const CANONICAL_BASE_CLASS_INTRINSICS = ["OpenElement", "ErrorBoundary"];
|
|
40
|
+
/**
|
|
41
|
+
* Resolve one heritage clause against the admitted base classes. Returns the
|
|
42
|
+
* first canonical resolution; when none is canonical, the first resolution
|
|
43
|
+
* that names why the provenance is unsupported (so the caller's fail-closed
|
|
44
|
+
* diagnostic keeps carrying the reason). A plain identifier whose text is one
|
|
45
|
+
* of the admitted names is tried first, so a conflicting-binding diagnostic
|
|
46
|
+
* names the base class the author wrote rather than the other one.
|
|
47
|
+
*/
|
|
48
|
+
function resolveCanonicalBaseClass(bindings, heritage) {
|
|
49
|
+
if (heritage === void 0) return void 0;
|
|
50
|
+
const writtenName = ts.isIdentifier(heritage) ? heritage.text : void 0;
|
|
51
|
+
const candidates = [...CANONICAL_BASE_CLASS_INTRINSICS.filter((name) => name === writtenName), ...CANONICAL_BASE_CLASS_INTRINSICS.filter((name) => name !== writtenName)];
|
|
52
|
+
let unsupported;
|
|
53
|
+
for (const intrinsic of candidates) {
|
|
54
|
+
const resolution = bindings.resolveIntrinsic(heritage, intrinsic);
|
|
55
|
+
if (resolution.canonical) return resolution;
|
|
56
|
+
if (unsupported === void 0 && resolution.unsupported !== void 0) unsupported = resolution;
|
|
57
|
+
}
|
|
58
|
+
return unsupported;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
31
61
|
* Resolve the module-scope import/declaration bindings of one source file
|
|
32
62
|
* once, so decorator, heritage and factory use sites all answer provenance
|
|
33
63
|
* from the same table. The semantic core analyzes a single module and stays
|
|
@@ -189,7 +219,7 @@ function analyzeModuleSemantics(source, fileName, options = {}) {
|
|
|
189
219
|
if (!ts.isClassDeclaration(statement)) continue;
|
|
190
220
|
const isDefault = statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword);
|
|
191
221
|
const heritage = statement.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)?.types[0]?.expression;
|
|
192
|
-
const
|
|
222
|
+
const extendsCanonicalBase = resolveCanonicalBaseClass(imports, heritage)?.canonical === true;
|
|
193
223
|
for (const decorator of ts.getDecorators(statement) ?? []) {
|
|
194
224
|
if (!ts.isCallExpression(decorator.expression)) continue;
|
|
195
225
|
const resolution = imports.resolveIntrinsic(decorator.expression.expression, "element");
|
|
@@ -201,7 +231,7 @@ function analyzeModuleSemantics(source, fileName, options = {}) {
|
|
|
201
231
|
const tag = stringArgument(decorator.expression);
|
|
202
232
|
if (!tag || !isCustomElementTag(tag)) continue;
|
|
203
233
|
defined.add(tag);
|
|
204
|
-
if (isDefault &&
|
|
234
|
+
if (isDefault && extendsCanonicalBase) defaultCompiledTag = tag;
|
|
205
235
|
}
|
|
206
236
|
}
|
|
207
237
|
const visit = (node) => {
|
|
@@ -260,4 +290,4 @@ function analyzeModuleSemantics(source, fileName, options = {}) {
|
|
|
260
290
|
};
|
|
261
291
|
}
|
|
262
292
|
//#endregion
|
|
263
|
-
export { analyzeModuleSemantics, createModuleIntrinsicBindings, isCompileTimeOnlyImport };
|
|
293
|
+
export { CANONICAL_BASE_CLASS_INTRINSICS, analyzeModuleSemantics, createModuleIntrinsicBindings, isCompileTimeOnlyImport, resolveCanonicalBaseClass };
|