@melt-js/libs-core 2026.11.15-2 → 2026.11.16-1
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 +15 -3
- package/dist/browser.d.mts +3 -2
- package/dist/browser.mjs +1 -60
- package/dist/browser.mjs.gz +0 -0
- package/dist/cdn/browser.umd.cjs +2 -0
- package/dist/cdn/browser.umd.cjs.gz +0 -0
- package/dist/cdn/demo-browser.umd.cjs +2 -0
- package/dist/cdn/demo-browser.umd.cjs.gz +0 -0
- package/dist/cdn/demo.umd.cjs +2 -0
- package/dist/cdn/demo.umd.cjs.gz +0 -0
- package/dist/cdn/example-esm.html +1 -0
- package/dist/cdn/example-umd.html +1 -0
- package/dist/cdn/index.umd.cjs +2 -0
- package/dist/cdn/index.umd.cjs.gz +0 -0
- package/dist/cdn/manifest.json +129 -0
- package/dist/cdn/vendor.umd.cjs +50 -0
- package/dist/cdn/vendor.umd.cjs.gz +0 -0
- package/dist/cdn.d.mts +3 -0
- package/dist/cdn.mjs +1 -0
- package/dist/demo/browser.mjs +1 -0
- package/dist/demo/browser.mjs.gz +0 -0
- package/dist/demo/clipboard.d.mts +6 -0
- package/dist/demo/clipboard.mjs +1 -0
- package/dist/demo/docs.d.mts +28 -28
- package/dist/demo/docs.mjs +1 -58
- package/dist/demo/docs.mjs.gz +0 -0
- package/dist/demo/download.d.mts +38 -35
- package/dist/demo/download.mjs +1 -4
- package/dist/demo/download.mjs.gz +0 -0
- package/dist/demo/fullscreen.d.mts +8 -8
- package/dist/demo/fullscreen.mjs +1 -1
- package/dist/demo/index.d.mts +3 -1
- package/dist/demo/index.mjs +1 -1
- package/dist/demo/model.d.mts +12 -12
- package/dist/demo/model.mjs +1 -1
- package/dist/demo/remote-debug.d.mts +11 -11
- package/dist/demo/remote-debug.mjs +1 -1
- package/dist/demo/runtime.mjs +6 -6
- package/dist/demo/split-pane.d.mts +8 -8
- package/dist/demo/split-pane.mjs +1 -1
- package/dist/demo/standalone.d.mts +9 -0
- package/dist/demo/standalone.mjs +1 -0
- package/dist/demo/tabs.d.mts +15 -15
- package/dist/demo/tabs.mjs +1 -1
- package/dist/loading.mjs +1 -11
- package/dist/remote.d.mts +110 -109
- package/dist/remote.mjs +1 -1
- package/dist/remote.mjs.gz +0 -0
- package/dist/vendor/hel-runtime.mjs +50 -0
- package/dist/vendor/hel-runtime.mjs.gz +0 -0
- package/package.json +25 -7
- package/scripts/browser-formats.mjs +355 -0
- package/scripts/build-browser.mjs +59 -31
- package/scripts/build-protected-esm.mjs +51 -0
- package/scripts/cdn-runtime.cjs +30 -0
- package/scripts/check-tdz.mjs +322 -322
- package/scripts/client-build-security.cjs +48 -0
- package/scripts/component-events.mjs +57 -57
- package/scripts/comps-bundle-obfuscation.cjs +266 -262
- package/scripts/hel-build-paths.cjs +49 -48
- package/scripts/package-artifacts.mjs +32 -6
- package/scripts/package-env.cjs +13 -6
- package/scripts/publish-stage.mjs +33 -33
- package/scripts/remote-address.cjs +30 -30
- package/scripts/vdoc-parser/NOTICE.md +13 -13
- package/scripts/vdoc-parser/constants.mjs +3 -3
- package/scripts/vdoc-parser/index.d.mts +34 -34
- package/scripts/vdoc-parser/index.mjs +2 -2
- package/scripts/vdoc-parser/parsers/index.mjs +14 -14
- package/scripts/vdoc-parser/parsers/parseCode.mjs +42 -42
- package/scripts/vdoc-parser/parsers/parseCommentBlock.mjs +9 -9
- package/scripts/vdoc-parser/parsers/parseComponentOptions.mjs +12 -12
- package/scripts/vdoc-parser/parsers/parseDefaultValue.mjs +57 -57
- package/scripts/vdoc-parser/parsers/parseDefineProps.mjs +33 -33
- package/scripts/vdoc-parser/parsers/parseExport.mjs +93 -93
- package/scripts/vdoc-parser/parsers/parseFile.mjs +22 -22
- package/scripts/vdoc-parser/parsers/parseFileSync.mjs +22 -22
- package/scripts/vdoc-parser/parsers/parsePropType.mjs +32 -32
- package/scripts/vdoc-parser/parsers/parseProps.mjs +90 -90
- package/scripts/vdoc-parser/parsers/parseSetupScript.mjs +14 -14
- package/scripts/vdoc-parser/parsers/parseSfc.mjs +21 -21
- package/scripts/vdoc-parser/parsers/parseTSTypeLiteral.mjs +48 -48
- package/scripts/vdoc-parser/parsers/parseTypes.mjs +53 -53
- package/scripts/vdoc-parser/parsers/parseTypesFile.mjs +22 -22
- package/scripts/vdoc-parser/parsers/parseTypesFileSync.mjs +22 -22
- package/scripts/vdoc-parser/resolvers/index.mjs +2 -2
- package/scripts/vdoc-parser/resolvers/resolveSpreadElements.mjs +51 -51
- package/scripts/vdoc-parser/utils.mjs +52 -52
- package/scripts/webpack-bundle-obfuscation.cjs +123 -121
package/scripts/check-tdz.mjs
CHANGED
|
@@ -1,328 +1,328 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
/*
|
|
3
|
-
|--------------------------------------------------------------------------
|
|
4
|
-
|
|
|
5
|
-
| 产物 TDZ 守卫:扫描构建产物,拦住「先引用、后声明」的 let / const / class。
|
|
6
|
-
|
|
|
7
|
-
| 背景(真实踩过的坑):vite-plugin-externals 在构建期把 import 就地改写成
|
|
8
|
-
| `const X = window.X`,而 ESM 的 import 是提升的(写在哪一行都先于模块体执行)。
|
|
9
|
-
| 一旦依赖产物把 import 写在模块体后面(tsup 输出常见),改写后就变成:
|
|
10
|
-
|
|
|
11
|
-
| __reExport(ns, vueNs); const vueNs = window.Vue // ← TDZ
|
|
12
|
-
|
|
|
13
|
-
| 浏览器加载即抛 `Cannot access 'xxx' before initialization`;混淆后变量名变成
|
|
14
|
-
| `_0x2bee8b` 之类,很难看出真因。这个脚本在 build 阶段直接拦下这类产物。
|
|
15
|
-
|
|
|
16
|
-
| 判定口径:引用位置早于声明位置,且两者之间没有跨函数边界(即加载期就会执行到)。
|
|
17
|
-
| 跨了函数的「延后引用」不算——那是正常写法(函数体在模块求值完成后才调用)。
|
|
18
|
-
|
|
|
19
|
-
| 用法:
|
|
20
|
-
| hel-check-tdz # 默认扫当前目录的 hel_dist/assets
|
|
21
|
-
| hel-check-tdz dist bundle.js # 也可指定目录 / 文件
|
|
22
|
-
| 退出码:发现 immediate TDZ → 1
|
|
23
|
-
|
|
|
24
|
-
|--------------------------------------------------------------------------
|
|
25
|
-
*/
|
|
26
|
-
import fs from 'node:fs'
|
|
27
|
-
import path from 'node:path'
|
|
28
|
-
import { parse } from 'acorn'
|
|
29
|
-
|
|
30
|
-
const FUNCTION_TYPES = new Set([
|
|
31
|
-
'FunctionDeclaration',
|
|
32
|
-
'FunctionExpression',
|
|
33
|
-
'ArrowFunctionExpression',
|
|
34
|
-
])
|
|
35
|
-
const CLASS_TYPES = new Set(['ClassDeclaration', 'ClassExpression'])
|
|
36
|
-
const SKIP_KEYS = new Set(['type', 'start', 'end', 'loc', 'range', '__parent'])
|
|
37
|
-
|
|
38
|
-
const isNode = v => !!v && typeof v === 'object' && typeof v.type === 'string'
|
|
39
|
-
|
|
40
|
-
/** 通用 AST 遍历,顺手挂 __parent,方便判断标识符的语法角色 */
|
|
41
|
-
function walk(node, enter) {
|
|
42
|
-
enter(node)
|
|
43
|
-
for (const key of Object.keys(node)) {
|
|
44
|
-
if (SKIP_KEYS.has(key)) continue
|
|
45
|
-
const value = node[key]
|
|
46
|
-
if (Array.isArray(value)) {
|
|
47
|
-
for (const item of value) {
|
|
48
|
-
if (isNode(item)) {
|
|
49
|
-
item.__parent = node
|
|
50
|
-
walk(item, enter)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
} else if (isNode(value)) {
|
|
54
|
-
value.__parent = node
|
|
55
|
-
walk(value, enter)
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
class Scope {
|
|
61
|
-
constructor(kind, parent, isFunction) {
|
|
62
|
-
this.kind = kind
|
|
63
|
-
this.parent = parent
|
|
64
|
-
this.isFunction = isFunction
|
|
65
|
-
/** name -> { start, kind } */
|
|
66
|
-
this.lexical = new Map()
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function collectPatternNames(node, cb) {
|
|
71
|
-
if (!node) return
|
|
72
|
-
if (node.type === 'Identifier') return cb(node)
|
|
73
|
-
if (node.type === 'AssignmentPattern') return collectPatternNames(node.left, cb)
|
|
74
|
-
if (node.type === 'RestElement') return collectPatternNames(node.argument, cb)
|
|
75
|
-
if (node.type === 'ArrayPattern') {
|
|
76
|
-
for (const el of node.elements) collectPatternNames(el, cb)
|
|
77
|
-
return
|
|
78
|
-
}
|
|
79
|
-
if (node.type === 'ObjectPattern') {
|
|
80
|
-
for (const prop of node.properties) {
|
|
81
|
-
if (prop.type === 'RestElement') collectPatternNames(prop.argument, cb)
|
|
82
|
-
else collectPatternNames(prop.value, cb)
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** 标识符是否处于「声明位置」(绑定名),是则不算引用 */
|
|
88
|
-
function isDeclarationIdentifier(node) {
|
|
89
|
-
const p = node.__parent
|
|
90
|
-
if (!p) return true
|
|
91
|
-
if (p.type === 'VariableDeclarator' && p.id === node) return true
|
|
92
|
-
if (FUNCTION_TYPES.has(p.type) && p.id === node) return true
|
|
93
|
-
if (CLASS_TYPES.has(p.type) && p.id === node) return true
|
|
94
|
-
if (p.type === 'CatchClause' && p.param === node) return true
|
|
95
|
-
if (
|
|
96
|
-
p.type === 'ImportSpecifier' ||
|
|
97
|
-
p.type === 'ImportDefaultSpecifier' ||
|
|
98
|
-
p.type === 'ImportNamespaceSpecifier'
|
|
99
|
-
)
|
|
100
|
-
return true
|
|
101
|
-
if (
|
|
102
|
-
p.type === 'ArrayPattern' ||
|
|
103
|
-
p.type === 'ObjectPattern' ||
|
|
104
|
-
p.type === 'RestElement' ||
|
|
105
|
-
p.type === 'AssignmentPattern' ||
|
|
106
|
-
p.type === 'Property'
|
|
107
|
-
) {
|
|
108
|
-
let cur = node
|
|
109
|
-
let up = cur.__parent
|
|
110
|
-
while (
|
|
111
|
-
up &&
|
|
112
|
-
(up.type === 'ArrayPattern' ||
|
|
113
|
-
up.type === 'ObjectPattern' ||
|
|
114
|
-
up.type === 'RestElement' ||
|
|
115
|
-
up.type === 'AssignmentPattern' ||
|
|
116
|
-
up.type === 'Property')
|
|
117
|
-
) {
|
|
118
|
-
if (up.type === 'AssignmentPattern' && up.left === cur && up.right === node) return false
|
|
119
|
-
cur = up
|
|
120
|
-
up = up.__parent
|
|
121
|
-
}
|
|
122
|
-
return !!(
|
|
123
|
-
up &&
|
|
124
|
-
((up.type === 'VariableDeclarator' && up.id === cur) ||
|
|
125
|
-
FUNCTION_TYPES.has(up.type) ||
|
|
126
|
-
(up.type === 'CatchClause' && up.param === cur))
|
|
127
|
-
)
|
|
128
|
-
}
|
|
129
|
-
return false
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** 引用是否属于「不是变量引用」的语法位置(属性名、label 等) */
|
|
133
|
-
function isNonReferencePosition(node) {
|
|
134
|
-
const p = node.__parent
|
|
135
|
-
if (!p) return true
|
|
136
|
-
if (p.type === 'MemberExpression' && p.property === node && !p.computed) return true
|
|
137
|
-
if (p.type === 'Property' && p.key === node && !p.computed) return true
|
|
138
|
-
if (p.type === 'MethodDefinition' && p.key === node && !p.computed) return true
|
|
139
|
-
if (p.type === 'PropertyDefinition' && p.key === node && !p.computed) return true
|
|
140
|
-
if (p.type === 'LabeledStatement' || p.type === 'BreakStatement' || p.type === 'ContinueStatement')
|
|
141
|
-
return true
|
|
142
|
-
if (p.type === 'ExportSpecifier' || p.type === 'ImportSpecifier') return true
|
|
143
|
-
return false
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function analyzeCode(code, filename) {
|
|
147
|
-
let ast
|
|
148
|
-
try {
|
|
149
|
-
ast = parse(code, {
|
|
150
|
-
ecmaVersion: 'latest',
|
|
151
|
-
sourceType: 'script',
|
|
152
|
-
allowHashBang: true,
|
|
153
|
-
allowReturnOutsideFunction: true,
|
|
154
|
-
})
|
|
155
|
-
} catch (err) {
|
|
156
|
-
// 少数产物是 ESM,按 module 再解析一次
|
|
157
|
-
ast = parse(code, {
|
|
158
|
-
ecmaVersion: 'latest',
|
|
159
|
-
sourceType: 'module',
|
|
160
|
-
allowHashBang: true,
|
|
161
|
-
})
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
walk(ast, () => {}) // 只为建立 __parent
|
|
165
|
-
|
|
166
|
-
const scopeByNode = new Map()
|
|
167
|
-
/** 绑定标识符(函数参数 / catch 参数等)本身,不参与「引用早于声明」判断 */
|
|
168
|
-
const bindingByNode = new Map()
|
|
169
|
-
const root = new Scope('program', null, false)
|
|
170
|
-
|
|
171
|
-
const build = (node, scope) => {
|
|
172
|
-
let current = scope
|
|
173
|
-
if (FUNCTION_TYPES.has(node.type)) {
|
|
174
|
-
current = new Scope('function', scope, true)
|
|
175
|
-
} else if (node.type === 'BlockStatement' || node.type === 'StaticBlock') {
|
|
176
|
-
current = new Scope('block', scope, false)
|
|
177
|
-
} else if (node.type === 'CatchClause') {
|
|
178
|
-
current = new Scope('catch', scope, false)
|
|
179
|
-
} else if (
|
|
180
|
-
// for (let x of/in ...) 的循环变量属于 for 语句自己的作用域,不是外层;
|
|
181
|
-
// 压缩产物里 `for(let e in t)` 很常见,不单独建作用域会把它误判成外层 let
|
|
182
|
-
node.type === 'ForStatement' ||
|
|
183
|
-
node.type === 'ForInStatement' ||
|
|
184
|
-
node.type === 'ForOfStatement' ||
|
|
185
|
-
// switch 的 case 共用同一个块级作用域
|
|
186
|
-
node.type === 'SwitchStatement'
|
|
187
|
-
) {
|
|
188
|
-
current = new Scope('block', scope, false)
|
|
189
|
-
} else if (CLASS_TYPES.has(node.type)) {
|
|
190
|
-
current = new Scope('class', scope, false)
|
|
191
|
-
}
|
|
192
|
-
scopeByNode.set(node, current)
|
|
193
|
-
|
|
194
|
-
const declare = (id, kind) => {
|
|
195
|
-
if (!current.lexical.has(id.name)) current.lexical.set(id.name, { start: id.start, kind })
|
|
196
|
-
// 参数 / 循环变量 / catch 参数也属于「声明位置」,后续引用不应被当成外层同名变量
|
|
197
|
-
if (!bindingByNode.has(id)) bindingByNode.set(id, { scope: current, kind, start: id.start })
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// 函数参数与 catch 参数也是绑定:不登记的话,压缩产物里形参名和模块级 let 同名时会大面积误报
|
|
201
|
-
if (FUNCTION_TYPES.has(node.type)) {
|
|
202
|
-
for (const param of node.params || []) collectPatternNames(param, id => declare(id, 'param'))
|
|
203
|
-
}
|
|
204
|
-
if (node.type === 'CatchClause' && node.param) {
|
|
205
|
-
collectPatternNames(node.param, id => declare(id, 'catch'))
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if (node.type === 'VariableDeclaration' && (node.kind === 'let' || node.kind === 'const')) {
|
|
209
|
-
for (const decl of node.declarations) {
|
|
210
|
-
collectPatternNames(decl.id, id => declare(id, node.kind))
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
if (node.type === 'ClassDeclaration' && node.id) {
|
|
214
|
-
// 类声明:类名在「类自身作用域」和「外层作用域」都可见
|
|
215
|
-
const outer = scopeByNode.get(node.__parent) || scope
|
|
216
|
-
if (!outer.lexical.has(node.id.name))
|
|
217
|
-
outer.lexical.set(node.id.name, { start: node.id.start, kind: 'class' })
|
|
218
|
-
declare(node.id, 'class')
|
|
219
|
-
} else if (node.type === 'ClassExpression' && node.id) {
|
|
220
|
-
// 类表达式(如压缩产物里的 `var Y = class e extends Error {}`)的类名只在本类内部可见,
|
|
221
|
-
// 不能登记到外层作用域,否则会把外层同名引用误判成 TDZ
|
|
222
|
-
declare(node.id, 'class')
|
|
223
|
-
}
|
|
224
|
-
if (node.type === 'ImportDeclaration') {
|
|
225
|
-
for (const spec of node.specifiers) declare(spec.local, 'import')
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
for (const key of Object.keys(node)) {
|
|
229
|
-
if (SKIP_KEYS.has(key)) continue
|
|
230
|
-
const value = node[key]
|
|
2
|
+
/*
|
|
3
|
+
|--------------------------------------------------------------------------
|
|
4
|
+
|
|
|
5
|
+
| 产物 TDZ 守卫:扫描构建产物,拦住「先引用、后声明」的 let / const / class。
|
|
6
|
+
|
|
|
7
|
+
| 背景(真实踩过的坑):vite-plugin-externals 在构建期把 import 就地改写成
|
|
8
|
+
| `const X = window.X`,而 ESM 的 import 是提升的(写在哪一行都先于模块体执行)。
|
|
9
|
+
| 一旦依赖产物把 import 写在模块体后面(tsup 输出常见),改写后就变成:
|
|
10
|
+
|
|
|
11
|
+
| __reExport(ns, vueNs); const vueNs = window.Vue // ← TDZ
|
|
12
|
+
|
|
|
13
|
+
| 浏览器加载即抛 `Cannot access 'xxx' before initialization`;混淆后变量名变成
|
|
14
|
+
| `_0x2bee8b` 之类,很难看出真因。这个脚本在 build 阶段直接拦下这类产物。
|
|
15
|
+
|
|
|
16
|
+
| 判定口径:引用位置早于声明位置,且两者之间没有跨函数边界(即加载期就会执行到)。
|
|
17
|
+
| 跨了函数的「延后引用」不算——那是正常写法(函数体在模块求值完成后才调用)。
|
|
18
|
+
|
|
|
19
|
+
| 用法:
|
|
20
|
+
| hel-check-tdz # 默认扫当前目录的 hel_dist/assets
|
|
21
|
+
| hel-check-tdz dist bundle.js # 也可指定目录 / 文件
|
|
22
|
+
| 退出码:发现 immediate TDZ → 1
|
|
23
|
+
|
|
|
24
|
+
|--------------------------------------------------------------------------
|
|
25
|
+
*/
|
|
26
|
+
import fs from 'node:fs'
|
|
27
|
+
import path from 'node:path'
|
|
28
|
+
import { parse } from 'acorn'
|
|
29
|
+
|
|
30
|
+
const FUNCTION_TYPES = new Set([
|
|
31
|
+
'FunctionDeclaration',
|
|
32
|
+
'FunctionExpression',
|
|
33
|
+
'ArrowFunctionExpression',
|
|
34
|
+
])
|
|
35
|
+
const CLASS_TYPES = new Set(['ClassDeclaration', 'ClassExpression'])
|
|
36
|
+
const SKIP_KEYS = new Set(['type', 'start', 'end', 'loc', 'range', '__parent'])
|
|
37
|
+
|
|
38
|
+
const isNode = v => !!v && typeof v === 'object' && typeof v.type === 'string'
|
|
39
|
+
|
|
40
|
+
/** 通用 AST 遍历,顺手挂 __parent,方便判断标识符的语法角色 */
|
|
41
|
+
function walk(node, enter) {
|
|
42
|
+
enter(node)
|
|
43
|
+
for (const key of Object.keys(node)) {
|
|
44
|
+
if (SKIP_KEYS.has(key)) continue
|
|
45
|
+
const value = node[key]
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
for (const item of value) {
|
|
48
|
+
if (isNode(item)) {
|
|
49
|
+
item.__parent = node
|
|
50
|
+
walk(item, enter)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
} else if (isNode(value)) {
|
|
54
|
+
value.__parent = node
|
|
55
|
+
walk(value, enter)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class Scope {
|
|
61
|
+
constructor(kind, parent, isFunction) {
|
|
62
|
+
this.kind = kind
|
|
63
|
+
this.parent = parent
|
|
64
|
+
this.isFunction = isFunction
|
|
65
|
+
/** name -> { start, kind } */
|
|
66
|
+
this.lexical = new Map()
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function collectPatternNames(node, cb) {
|
|
71
|
+
if (!node) return
|
|
72
|
+
if (node.type === 'Identifier') return cb(node)
|
|
73
|
+
if (node.type === 'AssignmentPattern') return collectPatternNames(node.left, cb)
|
|
74
|
+
if (node.type === 'RestElement') return collectPatternNames(node.argument, cb)
|
|
75
|
+
if (node.type === 'ArrayPattern') {
|
|
76
|
+
for (const el of node.elements) collectPatternNames(el, cb)
|
|
77
|
+
return
|
|
78
|
+
}
|
|
79
|
+
if (node.type === 'ObjectPattern') {
|
|
80
|
+
for (const prop of node.properties) {
|
|
81
|
+
if (prop.type === 'RestElement') collectPatternNames(prop.argument, cb)
|
|
82
|
+
else collectPatternNames(prop.value, cb)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 标识符是否处于「声明位置」(绑定名),是则不算引用 */
|
|
88
|
+
function isDeclarationIdentifier(node) {
|
|
89
|
+
const p = node.__parent
|
|
90
|
+
if (!p) return true
|
|
91
|
+
if (p.type === 'VariableDeclarator' && p.id === node) return true
|
|
92
|
+
if (FUNCTION_TYPES.has(p.type) && p.id === node) return true
|
|
93
|
+
if (CLASS_TYPES.has(p.type) && p.id === node) return true
|
|
94
|
+
if (p.type === 'CatchClause' && p.param === node) return true
|
|
95
|
+
if (
|
|
96
|
+
p.type === 'ImportSpecifier' ||
|
|
97
|
+
p.type === 'ImportDefaultSpecifier' ||
|
|
98
|
+
p.type === 'ImportNamespaceSpecifier'
|
|
99
|
+
)
|
|
100
|
+
return true
|
|
101
|
+
if (
|
|
102
|
+
p.type === 'ArrayPattern' ||
|
|
103
|
+
p.type === 'ObjectPattern' ||
|
|
104
|
+
p.type === 'RestElement' ||
|
|
105
|
+
p.type === 'AssignmentPattern' ||
|
|
106
|
+
p.type === 'Property'
|
|
107
|
+
) {
|
|
108
|
+
let cur = node
|
|
109
|
+
let up = cur.__parent
|
|
110
|
+
while (
|
|
111
|
+
up &&
|
|
112
|
+
(up.type === 'ArrayPattern' ||
|
|
113
|
+
up.type === 'ObjectPattern' ||
|
|
114
|
+
up.type === 'RestElement' ||
|
|
115
|
+
up.type === 'AssignmentPattern' ||
|
|
116
|
+
up.type === 'Property')
|
|
117
|
+
) {
|
|
118
|
+
if (up.type === 'AssignmentPattern' && up.left === cur && up.right === node) return false
|
|
119
|
+
cur = up
|
|
120
|
+
up = up.__parent
|
|
121
|
+
}
|
|
122
|
+
return !!(
|
|
123
|
+
up &&
|
|
124
|
+
((up.type === 'VariableDeclarator' && up.id === cur) ||
|
|
125
|
+
FUNCTION_TYPES.has(up.type) ||
|
|
126
|
+
(up.type === 'CatchClause' && up.param === cur))
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
return false
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** 引用是否属于「不是变量引用」的语法位置(属性名、label 等) */
|
|
133
|
+
function isNonReferencePosition(node) {
|
|
134
|
+
const p = node.__parent
|
|
135
|
+
if (!p) return true
|
|
136
|
+
if (p.type === 'MemberExpression' && p.property === node && !p.computed) return true
|
|
137
|
+
if (p.type === 'Property' && p.key === node && !p.computed) return true
|
|
138
|
+
if (p.type === 'MethodDefinition' && p.key === node && !p.computed) return true
|
|
139
|
+
if (p.type === 'PropertyDefinition' && p.key === node && !p.computed) return true
|
|
140
|
+
if (p.type === 'LabeledStatement' || p.type === 'BreakStatement' || p.type === 'ContinueStatement')
|
|
141
|
+
return true
|
|
142
|
+
if (p.type === 'ExportSpecifier' || p.type === 'ImportSpecifier') return true
|
|
143
|
+
return false
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function analyzeCode(code, filename) {
|
|
147
|
+
let ast
|
|
148
|
+
try {
|
|
149
|
+
ast = parse(code, {
|
|
150
|
+
ecmaVersion: 'latest',
|
|
151
|
+
sourceType: 'script',
|
|
152
|
+
allowHashBang: true,
|
|
153
|
+
allowReturnOutsideFunction: true,
|
|
154
|
+
})
|
|
155
|
+
} catch (err) {
|
|
156
|
+
// 少数产物是 ESM,按 module 再解析一次
|
|
157
|
+
ast = parse(code, {
|
|
158
|
+
ecmaVersion: 'latest',
|
|
159
|
+
sourceType: 'module',
|
|
160
|
+
allowHashBang: true,
|
|
161
|
+
})
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
walk(ast, () => {}) // 只为建立 __parent
|
|
165
|
+
|
|
166
|
+
const scopeByNode = new Map()
|
|
167
|
+
/** 绑定标识符(函数参数 / catch 参数等)本身,不参与「引用早于声明」判断 */
|
|
168
|
+
const bindingByNode = new Map()
|
|
169
|
+
const root = new Scope('program', null, false)
|
|
170
|
+
|
|
171
|
+
const build = (node, scope) => {
|
|
172
|
+
let current = scope
|
|
173
|
+
if (FUNCTION_TYPES.has(node.type)) {
|
|
174
|
+
current = new Scope('function', scope, true)
|
|
175
|
+
} else if (node.type === 'BlockStatement' || node.type === 'StaticBlock') {
|
|
176
|
+
current = new Scope('block', scope, false)
|
|
177
|
+
} else if (node.type === 'CatchClause') {
|
|
178
|
+
current = new Scope('catch', scope, false)
|
|
179
|
+
} else if (
|
|
180
|
+
// for (let x of/in ...) 的循环变量属于 for 语句自己的作用域,不是外层;
|
|
181
|
+
// 压缩产物里 `for(let e in t)` 很常见,不单独建作用域会把它误判成外层 let
|
|
182
|
+
node.type === 'ForStatement' ||
|
|
183
|
+
node.type === 'ForInStatement' ||
|
|
184
|
+
node.type === 'ForOfStatement' ||
|
|
185
|
+
// switch 的 case 共用同一个块级作用域
|
|
186
|
+
node.type === 'SwitchStatement'
|
|
187
|
+
) {
|
|
188
|
+
current = new Scope('block', scope, false)
|
|
189
|
+
} else if (CLASS_TYPES.has(node.type)) {
|
|
190
|
+
current = new Scope('class', scope, false)
|
|
191
|
+
}
|
|
192
|
+
scopeByNode.set(node, current)
|
|
193
|
+
|
|
194
|
+
const declare = (id, kind) => {
|
|
195
|
+
if (!current.lexical.has(id.name)) current.lexical.set(id.name, { start: id.start, kind })
|
|
196
|
+
// 参数 / 循环变量 / catch 参数也属于「声明位置」,后续引用不应被当成外层同名变量
|
|
197
|
+
if (!bindingByNode.has(id)) bindingByNode.set(id, { scope: current, kind, start: id.start })
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// 函数参数与 catch 参数也是绑定:不登记的话,压缩产物里形参名和模块级 let 同名时会大面积误报
|
|
201
|
+
if (FUNCTION_TYPES.has(node.type)) {
|
|
202
|
+
for (const param of node.params || []) collectPatternNames(param, id => declare(id, 'param'))
|
|
203
|
+
}
|
|
204
|
+
if (node.type === 'CatchClause' && node.param) {
|
|
205
|
+
collectPatternNames(node.param, id => declare(id, 'catch'))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (node.type === 'VariableDeclaration' && (node.kind === 'let' || node.kind === 'const')) {
|
|
209
|
+
for (const decl of node.declarations) {
|
|
210
|
+
collectPatternNames(decl.id, id => declare(id, node.kind))
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (node.type === 'ClassDeclaration' && node.id) {
|
|
214
|
+
// 类声明:类名在「类自身作用域」和「外层作用域」都可见
|
|
215
|
+
const outer = scopeByNode.get(node.__parent) || scope
|
|
216
|
+
if (!outer.lexical.has(node.id.name))
|
|
217
|
+
outer.lexical.set(node.id.name, { start: node.id.start, kind: 'class' })
|
|
218
|
+
declare(node.id, 'class')
|
|
219
|
+
} else if (node.type === 'ClassExpression' && node.id) {
|
|
220
|
+
// 类表达式(如压缩产物里的 `var Y = class e extends Error {}`)的类名只在本类内部可见,
|
|
221
|
+
// 不能登记到外层作用域,否则会把外层同名引用误判成 TDZ
|
|
222
|
+
declare(node.id, 'class')
|
|
223
|
+
}
|
|
224
|
+
if (node.type === 'ImportDeclaration') {
|
|
225
|
+
for (const spec of node.specifiers) declare(spec.local, 'import')
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
for (const key of Object.keys(node)) {
|
|
229
|
+
if (SKIP_KEYS.has(key)) continue
|
|
230
|
+
const value = node[key]
|
|
231
231
|
// The discriminant runs before the case block's lexical environment exists.
|
|
232
232
|
// `switch (x) { case 0: let x }` reads the outer x here.
|
|
233
233
|
const childScope = node.type === 'SwitchStatement' && key === 'discriminant' ? scope : current
|
|
234
|
-
if (Array.isArray(value)) {
|
|
234
|
+
if (Array.isArray(value)) {
|
|
235
235
|
for (const item of value) if (isNode(item)) build(item, childScope)
|
|
236
236
|
} else if (isNode(value)) build(value, childScope)
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
build(ast, root)
|
|
240
|
-
|
|
241
|
-
const problems = []
|
|
242
|
-
walk(ast, node => {
|
|
243
|
-
if (node.type !== 'Identifier') return
|
|
244
|
-
if (bindingByNode.has(node)) return
|
|
245
|
-
if (isDeclarationIdentifier(node)) return
|
|
246
|
-
if (isNonReferencePosition(node)) return
|
|
247
|
-
|
|
248
|
-
let scope = scopeByNode.get(node)
|
|
249
|
-
let hit = null
|
|
250
|
-
while (scope) {
|
|
251
|
-
if (scope.lexical.has(node.name)) {
|
|
252
|
-
hit = { scope, info: scope.lexical.get(node.name) }
|
|
253
|
-
break
|
|
254
|
-
}
|
|
255
|
-
scope = scope.parent
|
|
256
|
-
}
|
|
257
|
-
if (!hit) return
|
|
258
|
-
if (node.start > hit.info.start) return // 正常顺序
|
|
259
|
-
|
|
260
|
-
// 引用到声明之间是否跨了函数:跨了说明是延后执行(不是加载期 TDZ)
|
|
261
|
-
let crossings = 0
|
|
262
|
-
let cur = scopeByNode.get(node)
|
|
263
|
-
while (cur && cur !== hit.scope) {
|
|
264
|
-
if (cur.isFunction) crossings++
|
|
265
|
-
cur = cur.parent
|
|
266
|
-
}
|
|
267
|
-
if (crossings > 0) return
|
|
268
|
-
|
|
269
|
-
problems.push({
|
|
270
|
-
name: node.name,
|
|
271
|
-
start: node.start,
|
|
272
|
-
declStart: hit.info.start,
|
|
273
|
-
declKind: hit.info.kind,
|
|
274
|
-
snippet: code.slice(Math.max(0, node.start - 70), node.start + 90).replace(/\s+/g, ' '),
|
|
275
|
-
})
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
return problems.map(p => ({ ...p, file: filename }))
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function collectFiles(target) {
|
|
282
|
-
const stat = fs.statSync(target, { throwIfNoEntry: false })
|
|
283
|
-
if (!stat) return []
|
|
284
|
-
if (stat.isFile()) return [target]
|
|
285
|
-
return fs
|
|
286
|
-
.readdirSync(target, { withFileTypes: true })
|
|
287
|
-
.flatMap(entry => {
|
|
288
|
-
const full = path.join(target, entry.name)
|
|
289
|
-
if (entry.isDirectory()) return collectFiles(full)
|
|
290
|
-
return entry.isFile() && /\.m?js$/.test(entry.name) ? [full] : []
|
|
291
|
-
})
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const targets = process.argv.slice(2)
|
|
295
|
-
const defaultTarget = 'hel_dist/assets'
|
|
296
|
-
const files = (targets.length ? targets : [defaultTarget]).flatMap(collectFiles)
|
|
297
|
-
|
|
298
|
-
if (!files.length) {
|
|
299
|
-
console.warn(`[check-tdz] 没有找到可检查的产物(目标:${targets.join(', ') || defaultTarget})`)
|
|
300
|
-
process.exit(0)
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
let failed = false
|
|
304
|
-
for (const file of files) {
|
|
305
|
-
const code = fs.readFileSync(file, 'utf8')
|
|
306
|
-
const problems = analyzeCode(code, file)
|
|
307
|
-
const size = (code.length / 1024).toFixed(0)
|
|
308
|
-
if (!problems.length) {
|
|
309
|
-
console.log(`[check-tdz] OK ${file} (${size}KB)`)
|
|
310
|
-
continue
|
|
311
|
-
}
|
|
312
|
-
failed = true
|
|
313
|
-
console.error(`[check-tdz] FAIL ${file} (${size}KB) —— 发现 ${problems.length} 处加载期 TDZ 风险`)
|
|
314
|
-
for (const p of problems.slice(0, 10)) {
|
|
315
|
-
console.error(` · ${p.declKind} '${p.name}' 引用@${p.start} 早于声明@${p.declStart}`)
|
|
316
|
-
console.error(` ...${p.snippet}...`)
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (failed) {
|
|
321
|
-
console.error(
|
|
322
|
-
'\n[check-tdz] 产物里有「先引用、后声明」的 let/const/class,浏览器加载即抛 ' +
|
|
323
|
-
"ReferenceError: Cannot access 'xxx' before initialization。\n" +
|
|
324
|
-
'常见原因:构建期用了 vite-plugin-externals(把 import 就地改写成 const,破坏 ESM 提升),' +
|
|
325
|
-
'或打包产物把 import 写在了模块体后面。',
|
|
326
|
-
)
|
|
327
|
-
}
|
|
328
|
-
process.exitCode = failed ? 1 : 0
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
build(ast, root)
|
|
240
|
+
|
|
241
|
+
const problems = []
|
|
242
|
+
walk(ast, node => {
|
|
243
|
+
if (node.type !== 'Identifier') return
|
|
244
|
+
if (bindingByNode.has(node)) return
|
|
245
|
+
if (isDeclarationIdentifier(node)) return
|
|
246
|
+
if (isNonReferencePosition(node)) return
|
|
247
|
+
|
|
248
|
+
let scope = scopeByNode.get(node)
|
|
249
|
+
let hit = null
|
|
250
|
+
while (scope) {
|
|
251
|
+
if (scope.lexical.has(node.name)) {
|
|
252
|
+
hit = { scope, info: scope.lexical.get(node.name) }
|
|
253
|
+
break
|
|
254
|
+
}
|
|
255
|
+
scope = scope.parent
|
|
256
|
+
}
|
|
257
|
+
if (!hit) return
|
|
258
|
+
if (node.start > hit.info.start) return // 正常顺序
|
|
259
|
+
|
|
260
|
+
// 引用到声明之间是否跨了函数:跨了说明是延后执行(不是加载期 TDZ)
|
|
261
|
+
let crossings = 0
|
|
262
|
+
let cur = scopeByNode.get(node)
|
|
263
|
+
while (cur && cur !== hit.scope) {
|
|
264
|
+
if (cur.isFunction) crossings++
|
|
265
|
+
cur = cur.parent
|
|
266
|
+
}
|
|
267
|
+
if (crossings > 0) return
|
|
268
|
+
|
|
269
|
+
problems.push({
|
|
270
|
+
name: node.name,
|
|
271
|
+
start: node.start,
|
|
272
|
+
declStart: hit.info.start,
|
|
273
|
+
declKind: hit.info.kind,
|
|
274
|
+
snippet: code.slice(Math.max(0, node.start - 70), node.start + 90).replace(/\s+/g, ' '),
|
|
275
|
+
})
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
return problems.map(p => ({ ...p, file: filename }))
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function collectFiles(target) {
|
|
282
|
+
const stat = fs.statSync(target, { throwIfNoEntry: false })
|
|
283
|
+
if (!stat) return []
|
|
284
|
+
if (stat.isFile()) return [target]
|
|
285
|
+
return fs
|
|
286
|
+
.readdirSync(target, { withFileTypes: true })
|
|
287
|
+
.flatMap(entry => {
|
|
288
|
+
const full = path.join(target, entry.name)
|
|
289
|
+
if (entry.isDirectory()) return collectFiles(full)
|
|
290
|
+
return entry.isFile() && /\.m?js$/.test(entry.name) ? [full] : []
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const targets = process.argv.slice(2)
|
|
295
|
+
const defaultTarget = 'hel_dist/assets'
|
|
296
|
+
const files = (targets.length ? targets : [defaultTarget]).flatMap(collectFiles)
|
|
297
|
+
|
|
298
|
+
if (!files.length) {
|
|
299
|
+
console.warn(`[check-tdz] 没有找到可检查的产物(目标:${targets.join(', ') || defaultTarget})`)
|
|
300
|
+
process.exit(0)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
let failed = false
|
|
304
|
+
for (const file of files) {
|
|
305
|
+
const code = fs.readFileSync(file, 'utf8')
|
|
306
|
+
const problems = analyzeCode(code, file)
|
|
307
|
+
const size = (code.length / 1024).toFixed(0)
|
|
308
|
+
if (!problems.length) {
|
|
309
|
+
console.log(`[check-tdz] OK ${file} (${size}KB)`)
|
|
310
|
+
continue
|
|
311
|
+
}
|
|
312
|
+
failed = true
|
|
313
|
+
console.error(`[check-tdz] FAIL ${file} (${size}KB) —— 发现 ${problems.length} 处加载期 TDZ 风险`)
|
|
314
|
+
for (const p of problems.slice(0, 10)) {
|
|
315
|
+
console.error(` · ${p.declKind} '${p.name}' 引用@${p.start} 早于声明@${p.declStart}`)
|
|
316
|
+
console.error(` ...${p.snippet}...`)
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (failed) {
|
|
321
|
+
console.error(
|
|
322
|
+
'\n[check-tdz] 产物里有「先引用、后声明」的 let/const/class,浏览器加载即抛 ' +
|
|
323
|
+
"ReferenceError: Cannot access 'xxx' before initialization。\n" +
|
|
324
|
+
'常见原因:构建期用了 vite-plugin-externals(把 import 就地改写成 const,破坏 ESM 提升),' +
|
|
325
|
+
'或打包产物把 import 写在了模块体后面。',
|
|
326
|
+
)
|
|
327
|
+
}
|
|
328
|
+
process.exitCode = failed ? 1 : 0
|