@weapp-vite/wxml 0.0.0-stage → 0.1.0-alpha.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 CHANGED
@@ -1,3 +1,85 @@
1
- # Temporary Holding Version
1
+ # @weapp-vite/wxml
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 保留源码的小程序模板 parser 与 serializer。核心在 Node 和浏览器中运行,不依赖 DOM、文件系统或 JavaScript AST parser。
4
+
5
+ 首版预览通过 GitHub Actions OIDC 发布到 `alpha`,安装使用 `npm install @weapp-vite/wxml@alpha`。npm 初始化产生的 `0.0.0-stage` 只是占位版本;正式版继续使用仓库 changeset 发布流程。维护者入口与证据说明见[发布文档](https://github.com/weapp-vite/weapp-vite/blob/main/docs/repoctl-release.md#wxml-首版-alpha-预览)。
6
+
7
+ ```ts
8
+ import { edit, parse, serialize, walk } from '@weapp-vite/wxml'
9
+
10
+ const document = parse('<view title="before">{{ message }}</view>')
11
+ const updated = edit(document, (handle) => {
12
+ if (handle.node.kind === 'element') {
13
+ handle.setAttribute('title', 'after')
14
+ handle.setAttribute('hidden', { expression: '!visible' })
15
+ }
16
+ })
17
+
18
+ console.log(serialize(updated))
19
+ walk(updated, {
20
+ enter(node) {
21
+ console.log(node.kind, node.start)
22
+ },
23
+ })
24
+ ```
25
+
26
+ ## 接口
27
+
28
+ - `scan(source, options)` 返回 tokens 与 diagnostics,保留标签、属性、脚本和文本的源码范围。
29
+ - `parse(source, options)` 返回带有 children、tokens、diagnostics 和原始 source 的文档。
30
+ - `walk(document, visitor)` 同步先序遍历;enter 返回 false 时跳过子节点。
31
+ - `edit(document, visitor)` 在同步编辑会话中修改,返回新文档;不会修改输入文档。
32
+ - `serialize(document, { mode })` 输出模板源码。默认 preserve;canonical 会遍历树并统一标签和属性标点,不处理缩进、折行或压缩。
33
+
34
+ 节点和属性使用只读 TypeScript 接口,AST 不做运行时深冻结。JavaScript 调用方同样应通过 `edit` 修改;直接改写节点不受支持。
35
+
36
+ 通过编辑句柄的 setAttribute、setBooleanAttribute、removeAttribute、rename、replaceText、remove、replaceWith、insertBefore、insertAfter、prepend 和 append 登记修改;会话结束后句柄失效。属性 setter 替换首个同名位置并移除其他同名属性,removeAttribute 删除所有同名项。
37
+
38
+ 普通字符串作为字面量编码;`{ expression: 'value' }` 表示单个完整绑定的表达式,不能提前闭合绑定或插入额外节点,number 和 boolean 输出为有类型的绑定值。会话提交时校验最终值,被覆盖或删除的临时值不参与输出。插入或替换的源码片段会按当前文档选项解析校验。非法模板仍可读取诊断,但不能编辑或序列化为可写结果。
39
+
40
+ 编辑同步校验修改。保持结构的属性、普通标签名和非空文本修改复用原树;返回的新文档提供修改后的 source,children/tokens 在首次读取时重建并缓存,walk 和后续 edit 会自动触发。结构、脚本标签或自定义边界 resolver 涉及的修改会立即重新解析完整文档。
41
+
42
+ ## 平台与原文
43
+
44
+ `platform` 支持 weapp(默认)、alipay、tt、swan、jd、xhs、qq、ks 和 ty。`syntax` 为 legacy 或 xml,默认微信使用 legacy,其他平台使用 xml;显式 syntax 优先。
45
+
46
+ 输入方言与输出平台独立。WXML 编译到 Web 仍按 WXML 方言解析,Web 的组件映射、Lit 代码生成和运行时事件由 `@weapp-vite/web` 负责。本包不会自动转换平台指令或执行 WXS、插值表达式。
47
+
48
+ preserve 模式保留未修改部分,包括 BOM、换行、空白、引号、大小写、重复属性、裸属性、注释、CDATA 和脚本原文。源码范围采用 JavaScript 字符串的 UTF-16 半开区间 `[start, end)`。属性 value 是不含外层引号的词法原文;解码通过 `decodeAttribute(attribute.value, document.syntax, true, attribute.quote)` 显式进行。
49
+
50
+ 第四个参数应传入原属性的 `quote`。legacy 带引号属性统一解开一层宿主转义,包括绑定中的反斜杠和外层引号;返回逻辑绑定源码,不执行 JavaScript 字符串转义。`undefined` 表示无引号属性,保留反斜杠。legacy 实体解码只处理静态片段,绑定中的 `'&amp;'` 保持原文;XML 方言按完整属性值解码实体。输出 quoted legacy 属性时先编码全部反斜杠,再转义外层引号;未转义的外层引号会产生结构诊断。
51
+
52
+ 脚本正文使用独立词法状态识别字符串、正则和注释。少数注释结束边界存在歧义时,调用方可提供 resolveScriptBoundary;语言服务的兼容层可以注入 JS AST 判定,基础入口不会加载 Babel/Oxc。
53
+
54
+ 默认原文标签为 wxs、sjs、ks、filter、script 和 import-sjs,`rawTags` 可替换完整列表。消费者按实际输入平台选择该列表,例如微信自定义 filter 组件与百度 filter 脚本应使用不同配置。边界 resolver 在编辑校验时接收完整最终源码,插入片段另行校验自身的结构平衡。
55
+
56
+ 当插值已经词法确认结束于 `}}`,但内部括号不匹配时,诊断使用 `template/invalid-interpolation`,并提供只读 `recovery: { kind: 'interpolation', start, end }`,范围包括两侧 mustache。消费者可据此实现自己的错误展示或兼容策略;文档仍有错误,edit 和 serialize 继续拒绝。缺少 `}}`,或字符串、正则、注释、标签结构的边界无法确定时,不提供恢复范围。
57
+
58
+ ## 使用方边界
59
+
60
+ 共享核心已经接入仓库的模板处理入口,各使用方保留自己的语义与兼容层:
61
+
62
+ | 使用方 | 负责的能力 |
63
+ | --- | --- |
64
+ | `@weapp-vite/language-service` | XML 词法兼容接口、可变分析结果、平台语义、作用域、格式化及脚本边界的 JS AST 歧义判定 |
65
+ | `weapp-vite` | 构建扫描、受控模板变换、doctor 引用检查及 scoped style 输出处理 |
66
+ | `@weapp-vite/web` | 输入方言选择、模板编译、字符串模板兼容、表达式执行和 DOM 渲染 |
67
+ | `@mpcore/simulator` | 模拟器模板结构、属性、插值和脚本模块消费 |
68
+
69
+ 模拟器在消费层复现实测的引号转义、反斜杠对及其组合;其他转义沿用既有求值行为,尚未确认与真实宿主一致。Web 表达式按标准 JavaScript 执行;核心只处理语法层次与源码范围。
70
+
71
+ 2026-11-03 的验收覆盖微信开发者工具 Stable 2.02.2609240(基础库 3.17.2)和 Web runtime;六平台矩阵验证的是构建。各入口与平台的具体结果见[验收报告](https://github.com/weapp-vite/weapp-vite/blob/main/docs/reports/2026-11-03-wxml-parser/README.md)。
72
+
73
+ ## 验证与性能
74
+
75
+ ```sh
76
+ pnpm --filter @weapp-vite/wxml build
77
+ pnpm --filter @weapp-vite/wxml test
78
+ pnpm --filter @weapp-vite/wxml test:types
79
+ pnpm --filter @weapp-vite/wxml bench
80
+ pnpm --filter @weapp-vite/wxml bench:check
81
+ ```
82
+
83
+ 性能比较分别测量扫描、完整 AST、canonical 序列化及解析编辑输出。默认命令还覆盖构建扫描、Web 和 simulator 的三个正式消费入口。扫描和建树基线使用 htmlparser2,序列化使用 dom-serializer;未修改原文直接输出的快速路径单独报告。
84
+
85
+ 2026-11-03 的正式 v4 基准在 Node 24.22.1 和 headless Chromium 156.0.8078.4 中通过,每个环境的 140 个可比较结果单元全部达标,无错误。`shared` 是双方可直接比较的 354 份真实模板,`real-all` 包含全部 464 份真实模板,不含合成压力用例;两组聚合的候选/基线 P50 比值均要求 ≤1.00,其余分类(含 WXML)要求 ≤1.05。详细结果见[性能报告](https://github.com/weapp-vite/weapp-vite/blob/main/docs/reports/2026-11-03-wxml-parser/README.md),采样和门禁见[benchmark 说明](https://github.com/weapp-vite/weapp-vite/blob/main/scripts/benchmarkWxmlParser/README.md)。
@@ -0,0 +1,139 @@
1
+ interface SourceRange {
2
+ readonly start: number;
3
+ readonly end: number;
4
+ }
5
+ type Platform = 'weapp' | 'alipay' | 'tt' | 'swan' | 'jd' | 'xhs' | 'qq' | 'ks' | 'ty';
6
+ type Syntax = 'legacy' | 'xml';
7
+ /** 仅在结束边界已确定时提供完整插值的 UTF-16 半开范围,属性外层引号不在范围内。 */
8
+ interface InterpolationRecovery extends SourceRange {
9
+ readonly kind: 'interpolation';
10
+ }
11
+ interface Diagnostic extends SourceRange {
12
+ readonly code: string;
13
+ readonly message: string;
14
+ readonly severity: 'error' | 'warning' | 'info';
15
+ readonly recovery?: InterpolationRecovery;
16
+ }
17
+ interface Attribute extends SourceRange {
18
+ readonly name: string;
19
+ readonly nameStart: number;
20
+ readonly nameEnd: number;
21
+ readonly value?: string;
22
+ readonly valueStart?: number;
23
+ readonly valueEnd?: number;
24
+ readonly rawValue?: string;
25
+ readonly quote?: '"' | '\'';
26
+ }
27
+ interface TagToken extends SourceRange {
28
+ readonly kind: 'tag';
29
+ readonly name: string;
30
+ readonly nameStart: number;
31
+ readonly nameEnd: number;
32
+ readonly closing: boolean;
33
+ readonly selfClosing: boolean;
34
+ readonly attributes: readonly Attribute[];
35
+ readonly matching?: number;
36
+ readonly parent?: number;
37
+ }
38
+ interface RawToken extends SourceRange {
39
+ readonly kind: 'text' | 'comment' | 'cdata' | 'declaration' | 'script';
40
+ readonly tagName?: string;
41
+ }
42
+ type Token = TagToken | RawToken;
43
+ interface ScriptBoundaryCandidates {
44
+ readonly closingTag?: SourceRange;
45
+ readonly trailingComment?: number;
46
+ readonly commentClosings?: readonly (SourceRange & {
47
+ readonly block: boolean;
48
+ })[];
49
+ }
50
+ interface ParseOptions {
51
+ readonly platform?: Platform;
52
+ readonly syntax?: Syntax;
53
+ readonly rawTags?: readonly string[];
54
+ readonly resolveScriptBoundary?: (source: string, start: number, tagName: string, candidates: ScriptBoundaryCandidates) => SourceRange | undefined;
55
+ }
56
+ interface ScanResult {
57
+ readonly tokens: readonly Token[];
58
+ readonly diagnostics: readonly Diagnostic[];
59
+ }
60
+ interface ElementNode extends SourceRange {
61
+ readonly kind: 'element';
62
+ readonly name: string;
63
+ readonly attributes: readonly Attribute[];
64
+ readonly children: readonly Node[];
65
+ readonly opening: TagToken;
66
+ readonly closing?: TagToken;
67
+ readonly selfClosing: boolean;
68
+ }
69
+ interface RawNode extends RawToken {
70
+ readonly data: string;
71
+ }
72
+ type Node = ElementNode | RawNode;
73
+ interface Document extends ScanResult {
74
+ readonly source: string;
75
+ readonly platform: Platform;
76
+ readonly syntax: Syntax;
77
+ readonly children: readonly Node[];
78
+ readonly options: ParseOptions;
79
+ }
80
+ interface Visitor {
81
+ enter?: (node: Node, parent: ElementNode | undefined) => void | false;
82
+ leave?: (node: Node, parent: ElementNode | undefined) => void;
83
+ }
84
+ type AttributeValue = string | number | boolean | {
85
+ readonly expression: string;
86
+ };
87
+ interface EditableNode {
88
+ readonly node: Node;
89
+ rename: (name: string) => void;
90
+ setAttribute: (name: string, value: AttributeValue) => void;
91
+ setBooleanAttribute: (name: string) => void;
92
+ removeAttribute: (name: string) => void;
93
+ replaceText: (value: string | {
94
+ readonly expression: string;
95
+ }) => void;
96
+ remove: () => void;
97
+ replaceWith: (source: string) => void;
98
+ insertBefore: (source: string) => void;
99
+ insertAfter: (source: string) => void;
100
+ prepend: (source: string) => void;
101
+ append: (source: string) => void;
102
+ }
103
+ type EditVisitor = (node: EditableNode) => void | false;
104
+ interface SerializeOptions {
105
+ readonly mode?: 'preserve' | 'canonical';
106
+ }
107
+ /** 解码不含外层引号的词法属性值;表达式内部保留 JS 字符串的转义层。 */
108
+ export declare function decodeAttribute(value: string, syntax?: Syntax, decodeEntities?: boolean, quote?: string): string;
109
+ /** 同步编辑会话验证变更词法后发布新文档,原始文档保持不变。 */
110
+ export declare function edit(document: Document, visitor: EditVisitor): Document;
111
+ /** 定位插值末尾;跳过字符串、注释、正则和嵌套括号,保留原文编码。 */
112
+ export declare function expressionEnd(source: string, start: number, mustache?: boolean, syntax?: Syntax, outerQuote?: string): number;
113
+ /** 返回完整插值的半开范围,保留属性层的编码规则。 */
114
+ export declare function interpolations(source: string, syntax?: Syntax, outerQuote?: string): SourceRange[];
115
+ /** 词法错误携带原文偏移,由调用方选择报告或抛出。 */
116
+ export declare class TemplateLexicalError extends Error {
117
+ readonly offset: number;
118
+ readonly recovery?: InterpolationRecovery | undefined;
119
+ constructor(offset: number, message: string, recovery?: InterpolationRecovery | undefined);
120
+ }
121
+ /** 在一次扫描中建立保留完整原文与属性形式的模板文档。 */
122
+ export declare function parse(source: string, options?: ParseOptions): Document;
123
+ /** 扫描保留原文的 tokens 与诊断,不分配 AST 节点。 */
124
+ export declare function scan(source: string, options?: ParseOptions): ScanResult;
125
+ /** 只收集普通模板注释,不建立属性和标签配对,最后一个候选之后停止扫描。 */
126
+ export declare function scanComments(source: string, options?: ParseOptions): ScanResult;
127
+ /** 只收集词法候选,不加载 JS AST;歧义由调用方注入的 resolver 处理。 */
128
+ export declare function scanScriptBoundaryCandidates(source: string, start: number, tagName: string): ScriptBoundaryCandidates;
129
+ /** 返回明确位于脚本代码层的结束标签,不擅自采用注释内的候选。 */
130
+ export declare function findScriptClosingTag(source: string, start: number, tagName: string): SourceRange | undefined;
131
+ /** 查找词法确认的未闭合尾部块注释。 */
132
+ export declare function findTrailingScriptComment(source: string): number | undefined;
133
+ /** 保留模式输出当前文档源码;规范模式逐节点输出并保留所有文本。 */
134
+ export declare function serialize(document: Document, options?: SerializeOptions): string;
135
+ /** 读取标签与属性范围;属性值始终保留未解码的原文。 */
136
+ export declare function readTag(source: string, start: number, syntax?: Syntax, collectAttributes?: boolean): TagToken;
137
+ /** 迭代遍历文档,enter 返回 false 时跳过子树,避免深模板递归溢出。 */
138
+ export declare function walk(document: Document | readonly Node[], visitor: Visitor): void;
139
+ export type { Attribute, AttributeValue, Diagnostic, Document, EditVisitor, EditableNode, ElementNode, InterpolationRecovery, Node, ParseOptions, Platform, RawNode, RawToken, ScanResult, ScriptBoundaryCandidates, SerializeOptions, SourceRange, Syntax, TagToken, Token, Visitor };