@openelement/compiler 0.0.0-stage → 1.0.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 +21 -0
- package/README.md +20 -2
- package/package.json +45 -4
- package/src/compiler.d.ts +8 -0
- package/src/compiler.js +8 -0
- package/src/internal/compiler/plugin.d.ts +95 -0
- package/src/internal/compiler/plugin.js +133 -0
- package/src/internal/compiler/semantic-core/analyze-module.js +479 -0
- package/src/internal/compiler/semantic-core/compile.d.ts +39 -0
- package/src/internal/compiler/semantic-core/compile.js +98 -0
- package/src/internal/compiler/semantic-core/compiler-diagnostics.d.ts +31 -0
- package/src/internal/compiler/semantic-core/compiler-diagnostics.js +50 -0
- package/src/internal/compiler/semantic-core/diagnostics/index.d.ts +54 -0
- package/src/internal/compiler/semantic-core/diagnostics/index.js +97 -0
- package/src/internal/compiler/semantic-core/emit-module.js +259 -0
- package/src/internal/compiler/semantic-core/emit-program.js +121 -0
- package/src/internal/compiler/semantic-core/lower-program.js +815 -0
- package/src/internal/compiler/semantic-core/module-analysis.d.ts +115 -0
- package/src/internal/compiler/semantic-core/module-analysis.js +263 -0
- package/src/internal/compiler/semantic-core/parse-module.js +37 -0
- package/src/internal/compiler/semantic-core/source-map.d.ts +53 -0
- package/src/internal/compiler/semantic-core/source-map.js +93 -0
- package/src/internal/compiler/semantic-core/style-imports.js +87 -0
- package/src/internal/compiler/semantic-core/type-check.d.ts +50 -0
- package/src/internal/compiler/semantic-core/type-check.js +106 -0
- package/src/internal/compiler/style-requests.d.ts +53 -0
- package/src/internal/compiler/style-requests.js +50 -0
- package/src/vite.d.ts +2 -0
- package/src/vite.js +2 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { ModuleVocabularyDescriptor, ModuleVocabularyDescriptor as ModuleVocabularyDescriptor$1, StaticSidecarDescriptor, StaticSidecarDescriptor as StaticSidecarDescriptor$1 } from "@openelement/protocol/module-descriptors";
|
|
3
|
+
//#region src/internal/compiler/semantic-core/module-analysis.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* The bundler-neutral facts one module source yields: its relative imports, the
|
|
6
|
+
* intrinsic bindings it admits ({@link ModuleSemanticFacts.compiledElementDecorator},
|
|
7
|
+
* {@link ModuleSemanticFacts.exportedTagName}, custom-element tags it defines or
|
|
8
|
+
* references) and the interaction-event names its handlers bind. The Vite graph
|
|
9
|
+
* adapter branches on these without re-parsing the AST.
|
|
10
|
+
*/
|
|
11
|
+
export interface ModuleSemanticFacts {
|
|
12
|
+
relativeImports: string[];
|
|
13
|
+
compiledElementDecorator: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Set when a class decorator spells the `element` intrinsic through
|
|
16
|
+
* unsupported or ambiguous provenance (type-only import, namespace access,
|
|
17
|
+
* default import, conflicting duplicate bindings, or a relative-module
|
|
18
|
+
* re-export). Never set for clearly foreign bindings (third-party packages,
|
|
19
|
+
* local declarations, bare/global spellings) — those modules are simply not
|
|
20
|
+
* OpenElement modules. The plugin gate compiles such modules so the
|
|
21
|
+
* compiler boundary fails closed with the OEC9027 provenance diagnostic.
|
|
22
|
+
*/
|
|
23
|
+
unsupportedElementDecorator?: string;
|
|
24
|
+
exportedTagName?: string;
|
|
25
|
+
/**
|
|
26
|
+
* True when the module default-exports a page-definition factory call from
|
|
27
|
+
* the host's injected vocabulary, or is an .mdx route. The default scan
|
|
28
|
+
* (no injected vocabulary) never recognizes another package's factories.
|
|
29
|
+
*/
|
|
30
|
+
definePage: boolean;
|
|
31
|
+
usesExportedTagName: boolean;
|
|
32
|
+
enhancedForm: boolean;
|
|
33
|
+
defaultCompiledTag?: string;
|
|
34
|
+
definedCustomElementTags: string[];
|
|
35
|
+
referencedCustomElementTags: string[];
|
|
36
|
+
compilerInteractionEvents: string[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The canonical intrinsic-binding model (#1209, A10.1): compiler intrinsics
|
|
40
|
+
* are binding identities (module specifier + imported name, aliases
|
|
41
|
+
* followed), never identifier spellings. A bare/global spelling NEVER admits
|
|
42
|
+
* an intrinsic. The built-in set covers `@openelement/element` only — host
|
|
43
|
+
* frameworks extend admission through injected
|
|
44
|
+
* {@link StaticSidecarDescriptor}s, never through compiler-side knowledge of
|
|
45
|
+
* another package.
|
|
46
|
+
*/
|
|
47
|
+
export type IntrinsicName = 'element' | 'property' | 'OpenElement' | 'computed' | 'trustedHtml';
|
|
48
|
+
/** Host-supplied admission extensions for the semantic core. */
|
|
49
|
+
export interface SemanticCoreOptions {
|
|
50
|
+
/**
|
|
51
|
+
* Static-sidecar descriptors the host admits. The default core knows none:
|
|
52
|
+
* a module carrying a sidecar policy statement fails closed until the host
|
|
53
|
+
* injects its descriptor.
|
|
54
|
+
*/
|
|
55
|
+
readonly staticSidecars?: readonly StaticSidecarDescriptor[];
|
|
56
|
+
}
|
|
57
|
+
/** Host-supplied vocabulary extensions for the module scan. */
|
|
58
|
+
export interface ModuleSemanticsOptions {
|
|
59
|
+
/**
|
|
60
|
+
* Vocabulary descriptors the host admits beyond the core's own element
|
|
61
|
+
* bindings. The default scan knows no other package: a route module's
|
|
62
|
+
* page-definition export or registration factory is recognized only when
|
|
63
|
+
* the host injects its descriptor.
|
|
64
|
+
*/
|
|
65
|
+
readonly vocabulary?: readonly ModuleVocabularyDescriptor[];
|
|
66
|
+
}
|
|
67
|
+
/** True when `imported` from `module` is a compile-time-only intrinsic binding. */
|
|
68
|
+
declare function isCompileTimeOnlyImport(module: string, imported: string): boolean;
|
|
69
|
+
export interface IntrinsicResolution {
|
|
70
|
+
/** True only for a runtime named import of the intrinsic from its canonical module. */
|
|
71
|
+
readonly canonical: boolean;
|
|
72
|
+
/** The local identifier at the use site (present only when canonical). */
|
|
73
|
+
readonly localName?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Why the provenance is unsupported/ambiguous (type-only, namespace,
|
|
76
|
+
* default import, conflicting duplicates, relative re-export). Callers must
|
|
77
|
+
* fail closed with this reason; absent for clearly foreign or unbound
|
|
78
|
+
* spellings.
|
|
79
|
+
*/
|
|
80
|
+
readonly unsupported?: string;
|
|
81
|
+
}
|
|
82
|
+
export interface ModuleIntrinsicBindings {
|
|
83
|
+
resolveIntrinsic(expression: ts.Expression, intrinsic: IntrinsicName): IntrinsicResolution;
|
|
84
|
+
/**
|
|
85
|
+
* True only when the expression resolves to a runtime named import matching
|
|
86
|
+
* one of the injected 'static-sidecar' descriptors (aliases followed,
|
|
87
|
+
* canonical specifier). Namespace, default, type-only, conflicting and
|
|
88
|
+
* re-export provenance is never admitted — the caller fails closed.
|
|
89
|
+
*/
|
|
90
|
+
isStaticSidecarCallee(expression: ts.Expression): boolean;
|
|
91
|
+
/** True for a runtime (non-type-only) named import, aliases followed. */
|
|
92
|
+
isRuntimeNamedImport(localName: string, module: string | readonly string[], imported: string): boolean;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Resolve the module-scope import/declaration bindings of one source file
|
|
96
|
+
* once, so decorator, heritage and factory use sites all answer provenance
|
|
97
|
+
* from the same table. The semantic core analyzes a single module and stays
|
|
98
|
+
* bundler-neutral: it never follows re-exports across files. Host-injected
|
|
99
|
+
* static-sidecar descriptors ride the options — the core itself never names
|
|
100
|
+
* another package.
|
|
101
|
+
*/
|
|
102
|
+
declare function createModuleIntrinsicBindings(sourceFile: ts.SourceFile, options?: SemanticCoreOptions): ModuleIntrinsicBindings;
|
|
103
|
+
/**
|
|
104
|
+
* Parse one module source and report the semantic facts the compiler boundary
|
|
105
|
+
* and the Vite graph adapters branch on. Pure: it never resolves imports from
|
|
106
|
+
* disk and never throws on foreign or invalid syntax — an unparsable module
|
|
107
|
+
* simply yields no recognized facts. The scan's factory vocabulary is
|
|
108
|
+
* host-injected: the default knows only the element package's own
|
|
109
|
+
* registration factories, and page-definition/registration facts for other
|
|
110
|
+
* packages require the caller's {@link ModuleSemanticsOptions.vocabulary}
|
|
111
|
+
* descriptors.
|
|
112
|
+
*/
|
|
113
|
+
export declare function analyzeModuleSemantics(source: string, fileName: string, options?: ModuleSemanticsOptions): ModuleSemanticFacts;
|
|
114
|
+
//#endregion
|
|
115
|
+
export type { ModuleVocabularyDescriptor$1 as ModuleVocabularyDescriptor, StaticSidecarDescriptor$1 as StaticSidecarDescriptor };
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
//#region src/internal/compiler/semantic-core/module-analysis.ts
|
|
3
|
+
/** Bundler-neutral module semantics consumed by compiler and Vite graph adapters. */
|
|
4
|
+
const INTRINSIC_MODULES = {
|
|
5
|
+
element: ["@openelement/element"],
|
|
6
|
+
property: ["@openelement/element"],
|
|
7
|
+
OpenElement: ["@openelement/element"],
|
|
8
|
+
computed: ["@openelement/element"],
|
|
9
|
+
trustedHtml: ["@openelement/element"]
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The core's own vocabulary: empty. The element package exposes no
|
|
13
|
+
* defineElement() factory — compiled registration is the @element decorator
|
|
14
|
+
* on an OpenElement subclass, which the scan recognizes directly. Every live
|
|
15
|
+
* registration factory — Router's page definition, the lit page factory —
|
|
16
|
+
* rides host-injected descriptors, and the retired factory names fail closed
|
|
17
|
+
* like any other unrecognized import.
|
|
18
|
+
*/
|
|
19
|
+
const CORE_VOCABULARY = [];
|
|
20
|
+
/**
|
|
21
|
+
* Intrinsics that exist only at compile time: the compiler erases the
|
|
22
|
+
* decorator applications they power, and the runtime packages export no such
|
|
23
|
+
* bindings, so the generated module must not keep importing them.
|
|
24
|
+
*/
|
|
25
|
+
const COMPILE_TIME_ONLY_IMPORTS = { "@openelement/element": ["element", "property"] };
|
|
26
|
+
/** True when `imported` from `module` is a compile-time-only intrinsic binding. */
|
|
27
|
+
function isCompileTimeOnlyImport(module, imported) {
|
|
28
|
+
return COMPILE_TIME_ONLY_IMPORTS[module]?.includes(imported) ?? false;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Resolve the module-scope import/declaration bindings of one source file
|
|
32
|
+
* once, so decorator, heritage and factory use sites all answer provenance
|
|
33
|
+
* from the same table. The semantic core analyzes a single module and stays
|
|
34
|
+
* bundler-neutral: it never follows re-exports across files. Host-injected
|
|
35
|
+
* static-sidecar descriptors ride the options — the core itself never names
|
|
36
|
+
* another package.
|
|
37
|
+
*/
|
|
38
|
+
function createModuleIntrinsicBindings(sourceFile, options = {}) {
|
|
39
|
+
const imports = /* @__PURE__ */ new Map();
|
|
40
|
+
const locals = /* @__PURE__ */ new Set();
|
|
41
|
+
for (const statement of sourceFile.statements) {
|
|
42
|
+
if (ts.isImportDeclaration(statement)) {
|
|
43
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
44
|
+
const module = statement.moduleSpecifier.text;
|
|
45
|
+
const clause = statement.importClause;
|
|
46
|
+
if (!clause) continue;
|
|
47
|
+
const clauseTypeOnly = clause.isTypeOnly ?? false;
|
|
48
|
+
const add = (local, binding) => {
|
|
49
|
+
const list = imports.get(local) ?? [];
|
|
50
|
+
list.push(binding);
|
|
51
|
+
imports.set(local, list);
|
|
52
|
+
};
|
|
53
|
+
if (clause.name) add(clause.name.text, {
|
|
54
|
+
form: "default",
|
|
55
|
+
module,
|
|
56
|
+
imported: "default",
|
|
57
|
+
typeOnly: clauseTypeOnly
|
|
58
|
+
});
|
|
59
|
+
const namedBindings = clause.namedBindings;
|
|
60
|
+
if (namedBindings && ts.isNamedImports(namedBindings)) for (const element of namedBindings.elements) add(element.name.text, {
|
|
61
|
+
form: "named",
|
|
62
|
+
module,
|
|
63
|
+
imported: element.propertyName?.text ?? element.name.text,
|
|
64
|
+
typeOnly: clauseTypeOnly || (element.isTypeOnly ?? false)
|
|
65
|
+
});
|
|
66
|
+
else if (namedBindings && ts.isNamespaceImport(namedBindings)) add(namedBindings.name.text, {
|
|
67
|
+
form: "namespace",
|
|
68
|
+
module,
|
|
69
|
+
imported: "*",
|
|
70
|
+
typeOnly: clauseTypeOnly
|
|
71
|
+
});
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if ((ts.isFunctionDeclaration(statement) || ts.isClassDeclaration(statement) || ts.isEnumDeclaration(statement)) && statement.name) {
|
|
75
|
+
locals.add(statement.name.text);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (ts.isVariableStatement(statement)) {
|
|
79
|
+
for (const declaration of statement.declarationList.declarations) if (ts.isIdentifier(declaration.name)) locals.add(declaration.name.text);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const describe = (binding) => {
|
|
83
|
+
if (binding.form === "namespace") return `namespace import of '${binding.module}'`;
|
|
84
|
+
if (binding.form === "default") return `default import of '${binding.module}'`;
|
|
85
|
+
return `import of ${binding.imported} from '${binding.module}'`;
|
|
86
|
+
};
|
|
87
|
+
const resolveIdentifier = (name, bindingName, modules) => {
|
|
88
|
+
if (locals.has(name)) return { canonical: false };
|
|
89
|
+
const bindings = imports.get(name) ?? [];
|
|
90
|
+
if (bindings.length === 0) return { canonical: false };
|
|
91
|
+
if (bindings.length > 1) return {
|
|
92
|
+
canonical: false,
|
|
93
|
+
unsupported: `conflicting module-scope bindings for "${name}" (${bindings.map(describe).join("; ")}); import ${bindingName} once from its canonical module '${modules.join("' or '")}'`
|
|
94
|
+
};
|
|
95
|
+
const binding = bindings[0];
|
|
96
|
+
if (binding.form !== "named") {
|
|
97
|
+
if (modules.includes(binding.module)) return {
|
|
98
|
+
canonical: false,
|
|
99
|
+
unsupported: `"${name}" is a ${describe(binding)}; ${bindingName} requires a runtime named import`
|
|
100
|
+
};
|
|
101
|
+
return { canonical: false };
|
|
102
|
+
}
|
|
103
|
+
if (modules.includes(binding.module)) {
|
|
104
|
+
if (binding.imported !== bindingName) return { canonical: false };
|
|
105
|
+
if (binding.typeOnly) return {
|
|
106
|
+
canonical: false,
|
|
107
|
+
unsupported: `"${name}" is a type-only import of ${bindingName} from '${binding.module}'; intrinsics are runtime named imports`
|
|
108
|
+
};
|
|
109
|
+
return {
|
|
110
|
+
canonical: true,
|
|
111
|
+
localName: name
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (binding.imported === bindingName && binding.module.startsWith(".")) return {
|
|
115
|
+
canonical: false,
|
|
116
|
+
unsupported: `"${name}" imports ${bindingName} from '${binding.module}'; re-export provenance is not resolved across modules — import ${bindingName} from its canonical module '${modules.join("' or '")}'`
|
|
117
|
+
};
|
|
118
|
+
return { canonical: false };
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
resolveIntrinsic(expression, intrinsic) {
|
|
122
|
+
if (ts.isIdentifier(expression)) return resolveIdentifier(expression.text, intrinsic, INTRINSIC_MODULES[intrinsic]);
|
|
123
|
+
if (ts.isPropertyAccessExpression(expression) && ts.isIdentifier(expression.expression) && expression.name.text === intrinsic) {
|
|
124
|
+
const namespace = expression.expression.text;
|
|
125
|
+
const bindings = imports.get(namespace) ?? [];
|
|
126
|
+
if (!locals.has(namespace) && bindings.length === 1 && bindings[0].form === "namespace" && INTRINSIC_MODULES[intrinsic].includes(bindings[0].module)) return {
|
|
127
|
+
canonical: false,
|
|
128
|
+
unsupported: `namespace-qualified intrinsic "${namespace}.${intrinsic}" is unsupported; import ${intrinsic} from '${bindings[0].module}' by name`
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return { canonical: false };
|
|
132
|
+
},
|
|
133
|
+
isStaticSidecarCallee(expression) {
|
|
134
|
+
if (!ts.isIdentifier(expression)) return false;
|
|
135
|
+
for (const descriptor of options.staticSidecars ?? []) if (resolveIdentifier(expression.text, descriptor.exportName, [descriptor.moduleSpecifier]).canonical) return true;
|
|
136
|
+
return false;
|
|
137
|
+
},
|
|
138
|
+
isRuntimeNamedImport(localName, module, imported) {
|
|
139
|
+
if (locals.has(localName)) return false;
|
|
140
|
+
const modules = typeof module === "string" ? [module] : module;
|
|
141
|
+
const bindings = imports.get(localName) ?? [];
|
|
142
|
+
return bindings.length === 1 && bindings[0].form === "named" && modules.includes(bindings[0].module) && bindings[0].imported === imported && !bindings[0].typeOnly;
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
function isCustomElementTag(value) {
|
|
147
|
+
return /^[a-z][a-z0-9]*(-[a-z0-9]+)+$/.test(value);
|
|
148
|
+
}
|
|
149
|
+
function stringArgument(call, index = 0) {
|
|
150
|
+
const argument = call.arguments[index];
|
|
151
|
+
return argument && (ts.isStringLiteral(argument) || ts.isNoSubstitutionTemplateLiteral(argument)) ? argument.text : void 0;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Parse one module source and report the semantic facts the compiler boundary
|
|
155
|
+
* and the Vite graph adapters branch on. Pure: it never resolves imports from
|
|
156
|
+
* disk and never throws on foreign or invalid syntax — an unparsable module
|
|
157
|
+
* simply yields no recognized facts. The scan's factory vocabulary is
|
|
158
|
+
* host-injected: the default knows only the element package's own
|
|
159
|
+
* registration factories, and page-definition/registration facts for other
|
|
160
|
+
* packages require the caller's {@link ModuleSemanticsOptions.vocabulary}
|
|
161
|
+
* descriptors.
|
|
162
|
+
*/
|
|
163
|
+
function analyzeModuleSemantics(source, fileName, options = {}) {
|
|
164
|
+
const sourceFile = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, fileName.endsWith(".tsx") || fileName.endsWith(".jsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS);
|
|
165
|
+
const imports = createModuleIntrinsicBindings(sourceFile);
|
|
166
|
+
const vocabulary = [...CORE_VOCABULARY, ...options.vocabulary ?? []];
|
|
167
|
+
const pageDefinitionBindings = vocabulary.filter((descriptor) => descriptor.kind === "page-definition");
|
|
168
|
+
const registrationBindings = vocabulary.filter((descriptor) => descriptor.kind === "element-registration");
|
|
169
|
+
const relativeImports = /* @__PURE__ */ new Set();
|
|
170
|
+
for (const statement of sourceFile.statements) if ((ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) && statement.moduleSpecifier && ts.isStringLiteral(statement.moduleSpecifier) && statement.moduleSpecifier.text.startsWith(".")) relativeImports.add(statement.moduleSpecifier.text);
|
|
171
|
+
let exportedTagName;
|
|
172
|
+
let compiledElementDecorator = false;
|
|
173
|
+
let unsupportedElementDecorator;
|
|
174
|
+
let definePage = fileName.endsWith(".mdx");
|
|
175
|
+
let usesExportedTagName = false;
|
|
176
|
+
let enhancedForm = false;
|
|
177
|
+
let defaultCompiledTag;
|
|
178
|
+
const defined = /* @__PURE__ */ new Set();
|
|
179
|
+
const referenced = /* @__PURE__ */ new Set();
|
|
180
|
+
const interactionEvents = /* @__PURE__ */ new Set();
|
|
181
|
+
for (const statement of sourceFile.statements) {
|
|
182
|
+
if (ts.isVariableStatement(statement) && statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
|
|
183
|
+
for (const declaration of statement.declarationList.declarations) if (ts.isIdentifier(declaration.name) && declaration.name.text === "tagName" && declaration.initializer && (ts.isStringLiteral(declaration.initializer) || ts.isNoSubstitutionTemplateLiteral(declaration.initializer))) exportedTagName = declaration.initializer.text;
|
|
184
|
+
}
|
|
185
|
+
if (ts.isExportAssignment(statement) && ts.isCallExpression(statement.expression) && ts.isIdentifier(statement.expression.expression)) {
|
|
186
|
+
const pageFactory = statement.expression.expression.text;
|
|
187
|
+
if (pageDefinitionBindings.some((descriptor) => imports.isRuntimeNamedImport(pageFactory, descriptor.moduleSpecifier, descriptor.exportName))) definePage = true;
|
|
188
|
+
}
|
|
189
|
+
if (!ts.isClassDeclaration(statement)) continue;
|
|
190
|
+
const isDefault = statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword);
|
|
191
|
+
const heritage = statement.heritageClauses?.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword)?.types[0]?.expression;
|
|
192
|
+
const extendsOpenElement = heritage !== void 0 && imports.resolveIntrinsic(heritage, "OpenElement").canonical;
|
|
193
|
+
for (const decorator of ts.getDecorators(statement) ?? []) {
|
|
194
|
+
if (!ts.isCallExpression(decorator.expression)) continue;
|
|
195
|
+
const resolution = imports.resolveIntrinsic(decorator.expression.expression, "element");
|
|
196
|
+
if (!resolution.canonical) {
|
|
197
|
+
if (resolution.unsupported && unsupportedElementDecorator === void 0) unsupportedElementDecorator = resolution.unsupported;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
compiledElementDecorator = true;
|
|
201
|
+
const tag = stringArgument(decorator.expression);
|
|
202
|
+
if (!tag || !isCustomElementTag(tag)) continue;
|
|
203
|
+
defined.add(tag);
|
|
204
|
+
if (isDefault && extendsOpenElement) defaultCompiledTag = tag;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const visit = (node) => {
|
|
208
|
+
if (ts.isCallExpression(node)) {
|
|
209
|
+
const expression = node.expression;
|
|
210
|
+
if (expression.kind === ts.SyntaxKind.ImportKeyword && node.arguments.length === 1 && ts.isStringLiteral(node.arguments[0]) && node.arguments[0].text.startsWith(".")) relativeImports.add(node.arguments[0].text);
|
|
211
|
+
if (ts.isIdentifier(expression)) {
|
|
212
|
+
if (registrationBindings.some((descriptor) => imports.isRuntimeNamedImport(expression.text, descriptor.moduleSpecifier, descriptor.exportName))) {
|
|
213
|
+
const tag = stringArgument(node);
|
|
214
|
+
if (tag && isCustomElementTag(tag)) defined.add(tag);
|
|
215
|
+
if (tag !== void 0 && tag === exportedTagName) usesExportedTagName = true;
|
|
216
|
+
if (node.arguments[0] && ts.isIdentifier(node.arguments[0]) && node.arguments[0].text === "tagName") usesExportedTagName = true;
|
|
217
|
+
}
|
|
218
|
+
if (imports.isRuntimeNamedImport(expression.text, "@openelement/element/jsx-runtime", "jsx") || imports.isRuntimeNamedImport(expression.text, "@openelement/element/jsx-runtime", "jsxs") || imports.isRuntimeNamedImport(expression.text, "@openelement/element/jsx-runtime", "jsxDEV")) {
|
|
219
|
+
const tag = stringArgument(node);
|
|
220
|
+
if (tag && isCustomElementTag(tag)) referenced.add(tag);
|
|
221
|
+
}
|
|
222
|
+
} else if (ts.isPropertyAccessExpression(expression) && expression.expression.getText(sourceFile) === "customElements" && expression.name.text === "define") {
|
|
223
|
+
const tag = stringArgument(node);
|
|
224
|
+
if (tag && isCustomElementTag(tag)) defined.add(tag);
|
|
225
|
+
if (tag !== void 0 && tag === exportedTagName) usesExportedTagName = true;
|
|
226
|
+
if (node.arguments[0] && ts.isIdentifier(node.arguments[0]) && node.arguments[0].text === "tagName") usesExportedTagName = true;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) {
|
|
230
|
+
const tag = node.tagName.getText(sourceFile);
|
|
231
|
+
if (isCustomElementTag(tag)) {
|
|
232
|
+
referenced.add(tag);
|
|
233
|
+
if (tag === exportedTagName) usesExportedTagName = true;
|
|
234
|
+
}
|
|
235
|
+
for (const attribute of node.attributes.properties) {
|
|
236
|
+
if (ts.isJsxSpreadAttribute(attribute)) continue;
|
|
237
|
+
const name = attribute.name.getText(sourceFile);
|
|
238
|
+
if (name === "data-open-enhance") enhancedForm = true;
|
|
239
|
+
if (/^on[A-Z]/.test(name)) interactionEvents.add(name.slice(2).toLowerCase());
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (ts.isNoSubstitutionTemplateLiteral(node) || ts.isTemplateExpression(node)) {
|
|
243
|
+
if ((ts.isNoSubstitutionTemplateLiteral(node) ? [node.text] : [node.head.text, ...node.templateSpans.map((span) => span.literal.text)]).some((text) => /data-open-enhance(?=[\s=>/])/.test(text))) enhancedForm = true;
|
|
244
|
+
}
|
|
245
|
+
ts.forEachChild(node, visit);
|
|
246
|
+
};
|
|
247
|
+
visit(sourceFile);
|
|
248
|
+
return {
|
|
249
|
+
relativeImports: [...relativeImports],
|
|
250
|
+
compiledElementDecorator,
|
|
251
|
+
...unsupportedElementDecorator === void 0 ? {} : { unsupportedElementDecorator },
|
|
252
|
+
...exportedTagName === void 0 ? {} : { exportedTagName },
|
|
253
|
+
definePage,
|
|
254
|
+
usesExportedTagName,
|
|
255
|
+
enhancedForm,
|
|
256
|
+
...defaultCompiledTag === void 0 ? {} : { defaultCompiledTag },
|
|
257
|
+
definedCustomElementTags: [...defined].sort(),
|
|
258
|
+
referencedCustomElementTags: [...referenced].sort(),
|
|
259
|
+
compilerInteractionEvents: [...interactionEvents].sort()
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
//#endregion
|
|
263
|
+
export { analyzeModuleSemantics, createModuleIntrinsicBindings, isCompileTimeOnlyImport };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
//#region src/internal/compiler/semantic-core/parse-module.ts
|
|
3
|
+
/**
|
|
4
|
+
* The compiler's parse seam — the ONE module that depends on the TypeScript
|
|
5
|
+
* JavaScript parsing API (#1473, item 2 of the semantic-core split).
|
|
6
|
+
*
|
|
7
|
+
* `ParserPort` is the explicit boundary every parse backend implements:
|
|
8
|
+
* authored source in, TS AST plus syntax diagnostics out. The rest of the
|
|
9
|
+
* semantic core consumes the resulting AST through read-only type predicates
|
|
10
|
+
* and never constructs or transpiles a parse tree itself, so replacing the
|
|
11
|
+
* backend is a change to this file's single implementation and nowhere else.
|
|
12
|
+
*
|
|
13
|
+
* Retirement conditions (the alpha6 architecture-debt lane, issue #1473): this
|
|
14
|
+
* TypeScript-backed implementation is scheduled to be replaced when either
|
|
15
|
+
* trigger fires, whichever lands first —
|
|
16
|
+
* 1. the TypeScript 6 line ends JavaScript-API maintenance ("JS-API EOL"),
|
|
17
|
+
* making tsgo's native API the supported parse entry, or
|
|
18
|
+
* 2. oxc's standard-decorators support is declared stable.
|
|
19
|
+
* At that point `typescriptParser` swaps for an oxc/tsgo-backed `ParserPort`
|
|
20
|
+
* implementation; the compiler pipeline and its diagnostics contract stay.
|
|
21
|
+
*/
|
|
22
|
+
/** The one TypeScript-backed `ParserPort` implementation. */
|
|
23
|
+
const typescriptParser = { parseModule(source, fileName) {
|
|
24
|
+
return {
|
|
25
|
+
sourceFile: ts.createSourceFile(fileName, source, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX),
|
|
26
|
+
syntaxDiagnostics: ts.transpileModule(source, {
|
|
27
|
+
fileName,
|
|
28
|
+
reportDiagnostics: true,
|
|
29
|
+
compilerOptions: {
|
|
30
|
+
jsx: ts.JsxEmit.ReactJSX,
|
|
31
|
+
target: ts.ScriptTarget.ES2022
|
|
32
|
+
}
|
|
33
|
+
}).diagnostics ?? []
|
|
34
|
+
};
|
|
35
|
+
} };
|
|
36
|
+
//#endregion
|
|
37
|
+
export { typescriptParser };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
//#region src/internal/compiler/semantic-core/source-map.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* @openelement/element — Source Map v3 segment emission (#1210, A10.2).
|
|
4
|
+
*
|
|
5
|
+
* The compiler semantic core owns original source spans AND generated emission
|
|
6
|
+
* provenance: while it emits the compiled module it records one
|
|
7
|
+
* segment per copied or derived line/range, and this module turns those
|
|
8
|
+
* records into a standard Source Map v3 `mappings` string (VLQ, line+column,
|
|
9
|
+
* optional names). No Vite/Rollup import lives here — the Vite shell only
|
|
10
|
+
* composes the finished map with the rest of the transform pipeline.
|
|
11
|
+
*
|
|
12
|
+
* Segment table semantics: generatedLine is 1-based, generatedColumn 0-based,
|
|
13
|
+
* sourceLine 1-based, sourceColumn 0-based. There is exactly one source (the
|
|
14
|
+
* authored TSX module), so the source index is always 0.
|
|
15
|
+
*/
|
|
16
|
+
export interface CompiledElementSourceMap {
|
|
17
|
+
version: 3;
|
|
18
|
+
file: string;
|
|
19
|
+
sources: string[];
|
|
20
|
+
sourcesContent: string[];
|
|
21
|
+
names: string[];
|
|
22
|
+
mappings: string;
|
|
23
|
+
/** Compiler-owned Part Program source records carried as supplementary metadata. */
|
|
24
|
+
x_openElement?: unknown;
|
|
25
|
+
}
|
|
26
|
+
export interface EmissionSegment {
|
|
27
|
+
/** 1-based line in the generated module. */
|
|
28
|
+
generatedLine: number;
|
|
29
|
+
/** 0-based column in the generated module. */
|
|
30
|
+
generatedColumn: number;
|
|
31
|
+
/** 1-based line in the authored source. */
|
|
32
|
+
sourceLine: number;
|
|
33
|
+
/** 0-based column in the authored source. */
|
|
34
|
+
sourceColumn: number;
|
|
35
|
+
/** Authored identifier, when the compiler knows it (property/method names). */
|
|
36
|
+
name?: string;
|
|
37
|
+
}
|
|
38
|
+
/** Standard base64 VLQ encoding of one signed source-map field. */
|
|
39
|
+
declare function encodeVlq(value: number): string;
|
|
40
|
+
/**
|
|
41
|
+
* Collect emission segments and serialize them as a Source Map v3 document.
|
|
42
|
+
* Segments may be added in emission order (the emitter walks top to bottom)
|
|
43
|
+
* but are sorted defensively so the encoded deltas are always well-formed.
|
|
44
|
+
*/
|
|
45
|
+
declare class SourceMapSegmentBuilder {
|
|
46
|
+
private readonly segments;
|
|
47
|
+
private readonly names;
|
|
48
|
+
private readonly nameIndexes;
|
|
49
|
+
add(segment: EmissionSegment): void;
|
|
50
|
+
get size(): number;
|
|
51
|
+
build(file: string, source: string, provenance?: unknown): CompiledElementSourceMap;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { ProgramErrorCode, frameworkError } from "@openelement/protocol/errors";
|
|
2
|
+
//#region src/internal/compiler/semantic-core/source-map.ts
|
|
3
|
+
/**
|
|
4
|
+
* @openelement/element — Source Map v3 segment emission (#1210, A10.2).
|
|
5
|
+
*
|
|
6
|
+
* The compiler semantic core owns original source spans AND generated emission
|
|
7
|
+
* provenance: while it emits the compiled module it records one
|
|
8
|
+
* segment per copied or derived line/range, and this module turns those
|
|
9
|
+
* records into a standard Source Map v3 `mappings` string (VLQ, line+column,
|
|
10
|
+
* optional names). No Vite/Rollup import lives here — the Vite shell only
|
|
11
|
+
* composes the finished map with the rest of the transform pipeline.
|
|
12
|
+
*
|
|
13
|
+
* Segment table semantics: generatedLine is 1-based, generatedColumn 0-based,
|
|
14
|
+
* sourceLine 1-based, sourceColumn 0-based. There is exactly one source (the
|
|
15
|
+
* authored TSX module), so the source index is always 0.
|
|
16
|
+
*/
|
|
17
|
+
const BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
18
|
+
/** Standard base64 VLQ encoding of one signed source-map field. */
|
|
19
|
+
function encodeVlq(value) {
|
|
20
|
+
let current = value < 0 ? -value << 1 | 1 : value << 1;
|
|
21
|
+
let encoded = "";
|
|
22
|
+
do {
|
|
23
|
+
let digit = current & 31;
|
|
24
|
+
current >>>= 5;
|
|
25
|
+
if (current > 0) digit |= 32;
|
|
26
|
+
encoded += BASE64[digit];
|
|
27
|
+
} while (current > 0);
|
|
28
|
+
return encoded;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Collect emission segments and serialize them as a Source Map v3 document.
|
|
32
|
+
* Segments may be added in emission order (the emitter walks top to bottom)
|
|
33
|
+
* but are sorted defensively so the encoded deltas are always well-formed.
|
|
34
|
+
*/
|
|
35
|
+
var SourceMapSegmentBuilder = class {
|
|
36
|
+
segments = [];
|
|
37
|
+
names = [];
|
|
38
|
+
nameIndexes = /* @__PURE__ */ new Map();
|
|
39
|
+
add(segment) {
|
|
40
|
+
if (!Number.isInteger(segment.generatedLine) || segment.generatedLine < 1 || !Number.isInteger(segment.generatedColumn) || segment.generatedColumn < 0 || !Number.isInteger(segment.sourceLine) || segment.sourceLine < 1 || !Number.isInteger(segment.sourceColumn) || segment.sourceColumn < 0) throw frameworkError(ProgramErrorCode.INVALID_SOURCE_SEGMENT, `[compiled-program] invalid source segment ${JSON.stringify(segment)}`, { phase: "build" });
|
|
41
|
+
if (segment.name !== void 0 && !this.nameIndexes.has(segment.name)) {
|
|
42
|
+
this.nameIndexes.set(segment.name, this.names.length);
|
|
43
|
+
this.names.push(segment.name);
|
|
44
|
+
}
|
|
45
|
+
this.segments.push(segment);
|
|
46
|
+
}
|
|
47
|
+
get size() {
|
|
48
|
+
return this.segments.length;
|
|
49
|
+
}
|
|
50
|
+
build(file, source, provenance) {
|
|
51
|
+
const sorted = [...this.segments].sort((left, right) => left.generatedLine - right.generatedLine || left.generatedColumn - right.generatedColumn);
|
|
52
|
+
let mappings = "";
|
|
53
|
+
let generatedLine = 1;
|
|
54
|
+
let generatedColumn = 0;
|
|
55
|
+
let sourceLine = 0;
|
|
56
|
+
let sourceColumn = 0;
|
|
57
|
+
let nameIndex = 0;
|
|
58
|
+
let lineHasSegments = false;
|
|
59
|
+
for (const segment of sorted) {
|
|
60
|
+
while (generatedLine < segment.generatedLine) {
|
|
61
|
+
mappings += ";";
|
|
62
|
+
generatedLine++;
|
|
63
|
+
generatedColumn = 0;
|
|
64
|
+
lineHasSegments = false;
|
|
65
|
+
}
|
|
66
|
+
if (lineHasSegments) mappings += ",";
|
|
67
|
+
mappings += encodeVlq(segment.generatedColumn - generatedColumn);
|
|
68
|
+
generatedColumn = segment.generatedColumn;
|
|
69
|
+
mappings += "A";
|
|
70
|
+
mappings += encodeVlq(segment.sourceLine - 1 - sourceLine);
|
|
71
|
+
sourceLine = segment.sourceLine - 1;
|
|
72
|
+
mappings += encodeVlq(segment.sourceColumn - sourceColumn);
|
|
73
|
+
sourceColumn = segment.sourceColumn;
|
|
74
|
+
if (segment.name !== void 0) {
|
|
75
|
+
const index = this.nameIndexes.get(segment.name);
|
|
76
|
+
mappings += encodeVlq(index - nameIndex);
|
|
77
|
+
nameIndex = index;
|
|
78
|
+
}
|
|
79
|
+
lineHasSegments = true;
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
version: 3,
|
|
83
|
+
file,
|
|
84
|
+
sources: [file],
|
|
85
|
+
sourcesContent: [source],
|
|
86
|
+
names: this.names,
|
|
87
|
+
mappings,
|
|
88
|
+
...provenance === void 0 ? {} : { x_openElement: provenance }
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
//#endregion
|
|
93
|
+
export { SourceMapSegmentBuilder, encodeVlq };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
//#region src/internal/compiler/semantic-core/style-imports.ts
|
|
3
|
+
/**
|
|
4
|
+
* Sheet-import admission (#1558) — the one style authoring form.
|
|
5
|
+
*
|
|
6
|
+
* A compiled module's sheets are real `.css` files imported into the module
|
|
7
|
+
* and arrayed in the class's `static styles`. The generated module copies the
|
|
8
|
+
* authored imports and the initializer verbatim (emit-module), the sheet's
|
|
9
|
+
* bytes stay in exactly one place — the authored file (P6) — and the compile
|
|
10
|
+
* result carries the import edges so the host build's style-asset plugin can
|
|
11
|
+
* intercept them (style-requests.ts). Nothing here reads sheet bytes and
|
|
12
|
+
* nothing here synthesizes sheet text: the compiler polices only the
|
|
13
|
+
* import/initializer shape, and every other sheet spelling fails closed with
|
|
14
|
+
* OEC9029 plus a migration hint — a silent inline path would put stylesheet
|
|
15
|
+
* bytes back into JS chunks and void the zero-inline guarantee (#1553).
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* The OEC9029 diagnostic: the one style authoring form is `.css` file
|
|
19
|
+
* imports arrayed in `static styles`; the message carries the migration hint.
|
|
20
|
+
*/
|
|
21
|
+
function failStyleShape(fail, node, message) {
|
|
22
|
+
fail(node, "OEC9029", `${message} — write the sheet to a .css file, import it (\`import sheet from './<component>.css'\`) and array the binding in static styles; inline sheet strings are retired (#1558)`);
|
|
23
|
+
}
|
|
24
|
+
/** Strip parens and type assertions — the initializer's expression core. */
|
|
25
|
+
function unwrap(expr) {
|
|
26
|
+
let current = expr;
|
|
27
|
+
while (ts.isParenthesizedExpression(current) || ts.isAsExpression(current) || ts.isTypeAssertionExpression(current)) current = current.expression;
|
|
28
|
+
return current;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Collect the module's `.css` imports and police their shape: every sheet
|
|
32
|
+
* import is a relative specifier bound through a default import — the
|
|
33
|
+
* side-effect form has no binding for `static styles` to adopt, and a bare
|
|
34
|
+
* package specifier does not resolve inside the authoring tree (the style
|
|
35
|
+
* edge is per-module: the intercept keys on the importing module's directory
|
|
36
|
+
* plus the file name). Named or type-only forms are refused for the same
|
|
37
|
+
* dangling-binding reason.
|
|
38
|
+
*/
|
|
39
|
+
function collectSheetImports(sf, fail) {
|
|
40
|
+
const imports = /* @__PURE__ */ new Map();
|
|
41
|
+
for (const statement of sf.statements) {
|
|
42
|
+
if (!ts.isImportDeclaration(statement)) continue;
|
|
43
|
+
if (!ts.isStringLiteral(statement.moduleSpecifier)) continue;
|
|
44
|
+
const specifier = statement.moduleSpecifier.text;
|
|
45
|
+
if (!specifier.endsWith(".css")) continue;
|
|
46
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) failStyleShape(fail, statement, `the .css import specifier "${specifier}" must be relative; a bare package specifier does not resolve inside the authoring tree`);
|
|
47
|
+
const clause = statement.importClause;
|
|
48
|
+
if (!clause || clause.isTypeOnly || !clause.name) failStyleShape(fail, statement, clause ? `the .css import of "${specifier}" must bind its sheet as a default import; named and type-only forms have no sheet binding for static styles to adopt` : `the .css import of "${specifier}" must bind its sheet — the side-effect form has no binding for static styles to adopt`);
|
|
49
|
+
const name = clause.name.text;
|
|
50
|
+
if (imports.has(name)) fail(statement, "OEC9029", `duplicate sheet import binding "${name}" — a compiled module's .css imports bind distinct names so static styles can array them individually`);
|
|
51
|
+
imports.set(name, {
|
|
52
|
+
name,
|
|
53
|
+
specifier,
|
|
54
|
+
node: statement
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return imports;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Admit the class's authored `static styles` initializer: an array literal of
|
|
61
|
+
* the module's sheet-import bindings, in authored order. Returns the edge
|
|
62
|
+
* specifiers in that order; the admitted initializer emits verbatim, so the
|
|
63
|
+
* generated module keeps the authored import edges the host build intercepts.
|
|
64
|
+
* Every other shape fails closed with OEC9029, and every collected sheet
|
|
65
|
+
* import must be arrayed — an unadopted edge would dangle in the generated
|
|
66
|
+
* module with no style channel to ride.
|
|
67
|
+
*/
|
|
68
|
+
function admitStylesInitializer(stylesNode, sheetImports, sf, fail) {
|
|
69
|
+
const arrayNode = stylesNode === void 0 ? void 0 : unwrap(stylesNode);
|
|
70
|
+
if (arrayNode !== void 0 && !ts.isArrayLiteralExpression(arrayNode)) failStyleShape(fail, arrayNode, `static styles must array the module's .css file imports; "${arrayNode.getText(sf)}" is not an array of sheet imports`);
|
|
71
|
+
const edges = [];
|
|
72
|
+
const arrayed = /* @__PURE__ */ new Set();
|
|
73
|
+
if (arrayNode !== void 0) for (const element of arrayNode.elements) {
|
|
74
|
+
if (ts.isSpreadElement(element) || !ts.isIdentifier(element)) failStyleShape(fail, element, `static styles must array the module's .css file imports as plain bindings; "${element.getText(sf)}" is not a sheet-import binding`);
|
|
75
|
+
const name = element.text;
|
|
76
|
+
const sheet = sheetImports.get(name);
|
|
77
|
+
if (sheet === void 0) failStyleShape(fail, element, `static styles must array the module's .css file imports; "${name}" is not a .css import binding of this module`);
|
|
78
|
+
if (!arrayed.has(name)) {
|
|
79
|
+
arrayed.add(name);
|
|
80
|
+
edges.push(sheet.specifier);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const sheet of sheetImports.values()) if (!arrayed.has(sheet.name)) fail(sheet.node, "OEC9029", `the .css import "${sheet.name}" ("${sheet.specifier}") is never arrayed in static styles — every sheet import of a compiled module is adopted by its class`);
|
|
84
|
+
return edges;
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
export { admitStylesInitializer, collectSheetImports };
|