@weapp-vite/ast 6.9.1 → 6.10.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/dist/operations/componentProps.mjs +5 -0
- package/dist/operations/featureFlags.mjs +5 -0
- package/dist/operations/jsxAutoComponents.mjs +7 -0
- package/dist/operations/onPageScroll.mjs +1 -0
- package/dist/operations/platformApi.mjs +5 -0
- package/dist/operations/require.mjs +4 -0
- package/dist/operations/scriptSetupImports.mjs +7 -0
- package/dist/operations/setDataPick.mjs +1 -0
- package/package.json +2 -2
|
@@ -2,6 +2,10 @@ import { BABEL_TS_MODULE_PARSER_OPTIONS, parse, traverse } from "../babel.mjs";
|
|
|
2
2
|
import { t as parseJsLikeWithEngine } from "../engine-DHqNPCDA.mjs";
|
|
3
3
|
import { walk } from "oxc-walker";
|
|
4
4
|
//#region src/operations/componentProps.ts
|
|
5
|
+
const COMPONENT_PROPS_TEXT_HINTS = ["properties", "props"];
|
|
6
|
+
function mayContainComponentPropsShape(code) {
|
|
7
|
+
return COMPONENT_PROPS_TEXT_HINTS.some((hint) => code.includes(hint));
|
|
8
|
+
}
|
|
5
9
|
const CONSTRUCTOR_TYPE_MAP = {
|
|
6
10
|
String: "string",
|
|
7
11
|
StringConstructor: "string",
|
|
@@ -159,6 +163,7 @@ function collectComponentPropsWithOxc(code) {
|
|
|
159
163
|
* 从源码中提取组件属性类型映射。
|
|
160
164
|
*/
|
|
161
165
|
function collectComponentPropsFromCode(code, options) {
|
|
166
|
+
if (!mayContainComponentPropsShape(code)) return /* @__PURE__ */ new Map();
|
|
162
167
|
const engine = options?.astEngine ?? "babel";
|
|
163
168
|
try {
|
|
164
169
|
return engine === "oxc" ? collectComponentPropsWithOxc(code) : collectComponentPropsWithBabel(code);
|
|
@@ -3,6 +3,10 @@ import { t as parseJsLikeWithEngine } from "../engine-DHqNPCDA.mjs";
|
|
|
3
3
|
import * as t from "@babel/types";
|
|
4
4
|
import { walk } from "oxc-walker";
|
|
5
5
|
//#region src/operations/featureFlags.ts
|
|
6
|
+
function mayContainFeatureFlagHints(code, moduleId, hookToFeature) {
|
|
7
|
+
if (!code.includes(moduleId)) return false;
|
|
8
|
+
return Object.keys(hookToFeature).some((hookName) => code.includes(hookName));
|
|
9
|
+
}
|
|
6
10
|
function collectWithBabel(code, moduleId, hookToFeature) {
|
|
7
11
|
const ast = parseJsLike(code);
|
|
8
12
|
const namedHookLocals = /* @__PURE__ */ new Map();
|
|
@@ -86,6 +90,7 @@ function collectWithOxc(code, moduleId, hookToFeature) {
|
|
|
86
90
|
* 根据模块 ID 与 hook 映射表,从源码中收集启用的特性标识。
|
|
87
91
|
*/
|
|
88
92
|
function collectFeatureFlagsFromCode(code, options) {
|
|
93
|
+
if (!mayContainFeatureFlagHints(code, options.moduleId, options.hookToFeature)) return /* @__PURE__ */ new Set();
|
|
89
94
|
const engine = options.astEngine ?? "babel";
|
|
90
95
|
try {
|
|
91
96
|
return engine === "oxc" ? collectWithOxc(code, options.moduleId, options.hookToFeature) : collectWithBabel(code, options.moduleId, options.hookToFeature);
|
|
@@ -4,6 +4,9 @@ import * as t from "@babel/types";
|
|
|
4
4
|
import { parseSync } from "oxc-parser";
|
|
5
5
|
import { walk } from "oxc-walker";
|
|
6
6
|
//#region src/operations/jsxAutoComponents.ts
|
|
7
|
+
function mayContainJsxAutoComponentEntry(source) {
|
|
8
|
+
return source.includes("import") || source.includes("export default");
|
|
9
|
+
}
|
|
7
10
|
function defaultIsDefineComponentSource(source) {
|
|
8
11
|
return source === "vue";
|
|
9
12
|
}
|
|
@@ -285,6 +288,10 @@ function collectWithOxc(source, options) {
|
|
|
285
288
|
* 从 JSX 源码中收集自动 usingComponents 所需的导入组件与模板标签。
|
|
286
289
|
*/
|
|
287
290
|
function collectJsxAutoComponentsFromCode(source, options) {
|
|
291
|
+
if (!mayContainJsxAutoComponentEntry(source)) return {
|
|
292
|
+
templateTags: /* @__PURE__ */ new Set(),
|
|
293
|
+
importedComponents: []
|
|
294
|
+
};
|
|
288
295
|
const normalizedOptions = {
|
|
289
296
|
astEngine: options.astEngine ?? "babel",
|
|
290
297
|
isCollectableTag: options.isCollectableTag,
|
|
@@ -191,6 +191,7 @@ function collectWithOxc(code, filename) {
|
|
|
191
191
|
* 静态检测 onPageScroll 中的常见性能风险并返回告警文案。
|
|
192
192
|
*/
|
|
193
193
|
function collectOnPageScrollPerformanceWarnings(code, filename, options) {
|
|
194
|
+
if (!code.includes("onPageScroll")) return [];
|
|
194
195
|
if (options?.engine === "oxc") try {
|
|
195
196
|
return collectWithOxc(code, filename);
|
|
196
197
|
} catch {
|
|
@@ -9,12 +9,17 @@ const platformApiIdentifiers = new Set([
|
|
|
9
9
|
"jd",
|
|
10
10
|
"xhs"
|
|
11
11
|
]);
|
|
12
|
+
function mayContainPlatformApiIdentifierByText(code) {
|
|
13
|
+
for (const identifier of platformApiIdentifiers) if (code.includes(`${identifier}.`)) return true;
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
12
16
|
/**
|
|
13
17
|
* 使用统一 AST 入口做平台 API 访问预判。
|
|
14
18
|
*/
|
|
15
19
|
function mayContainPlatformApiAccess(code, options) {
|
|
16
20
|
const engine = options?.engine ?? "babel";
|
|
17
21
|
if (engine !== "oxc") return true;
|
|
22
|
+
if (!mayContainPlatformApiIdentifierByText(code)) return false;
|
|
18
23
|
try {
|
|
19
24
|
const ast = parseJsLikeWithEngine(code, {
|
|
20
25
|
engine,
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { t as parseJsLikeWithEngine } from "../engine-DHqNPCDA.mjs";
|
|
2
2
|
import { walk } from "oxc-walker";
|
|
3
3
|
//#region src/operations/require.ts
|
|
4
|
+
function mayContainRequireCallByText(code) {
|
|
5
|
+
return code.includes("require(") || code.includes("require (") || code.includes("require`");
|
|
6
|
+
}
|
|
4
7
|
function getStaticRequireLiteralValue(node) {
|
|
5
8
|
if (!node) return null;
|
|
6
9
|
if (node.type === "Literal" || node.type === "StringLiteral") return typeof node.value === "string" ? node.value : null;
|
|
@@ -33,6 +36,7 @@ function collectRequireTokens(ast) {
|
|
|
33
36
|
function mayContainStaticRequireLiteral(code, options) {
|
|
34
37
|
const engine = options?.engine ?? "babel";
|
|
35
38
|
if (engine !== "oxc") return true;
|
|
39
|
+
if (!mayContainRequireCallByText(code)) return false;
|
|
36
40
|
try {
|
|
37
41
|
const ast = parseJsLikeWithEngine(code, {
|
|
38
42
|
engine,
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { BABEL_TS_MODULE_PARSER_OPTIONS, parse } from "../babel.mjs";
|
|
2
2
|
import { t as parseJsLikeWithEngine } from "../engine-DHqNPCDA.mjs";
|
|
3
3
|
//#region src/operations/scriptSetupImports.ts
|
|
4
|
+
function mayContainRelevantScriptSetupImports(scriptSetup, templateComponentNames) {
|
|
5
|
+
if (templateComponentNames.size === 0) return false;
|
|
6
|
+
if (!scriptSetup.includes("import")) return false;
|
|
7
|
+
for (const componentName of templateComponentNames) if (scriptSetup.includes(componentName)) return true;
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
4
10
|
function collectWithOxc(scriptSetup, templateComponentNames) {
|
|
5
11
|
const results = [];
|
|
6
12
|
const ast = parseJsLikeWithEngine(scriptSetup, {
|
|
@@ -69,6 +75,7 @@ function collectWithBabel(scriptSetup, templateComponentNames) {
|
|
|
69
75
|
* 收集 `<script setup>` 中会参与自动 usingComponents 的导入声明。
|
|
70
76
|
*/
|
|
71
77
|
function collectScriptSetupImportsFromCode(scriptSetup, templateComponentNames, options) {
|
|
78
|
+
if (!mayContainRelevantScriptSetupImports(scriptSetup, templateComponentNames)) return [];
|
|
72
79
|
const engine = options?.astEngine ?? "babel";
|
|
73
80
|
try {
|
|
74
81
|
return engine === "oxc" ? collectWithOxc(scriptSetup, templateComponentNames) : collectWithBabel(scriptSetup, templateComponentNames);
|
|
@@ -276,6 +276,7 @@ function collectWithOxc(template) {
|
|
|
276
276
|
* 根据 AST 引擎从编译后的 WXML 模板提取渲染相关的顶层 key。
|
|
277
277
|
*/
|
|
278
278
|
function collectSetDataPickKeysFromTemplateCode(template, options) {
|
|
279
|
+
if (!template.includes("{{")) return [];
|
|
279
280
|
const engine = options?.astEngine ?? "babel";
|
|
280
281
|
try {
|
|
281
282
|
return engine === "oxc" ? collectWithOxc(template) : collectWithBabel(template);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-vite/ast",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "6.
|
|
4
|
+
"version": "6.10.0",
|
|
5
5
|
"description": "weapp-vite 共享 AST 分析工具包,统一 Babel/Oxc 解析与常用分析操作",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -103,7 +103,7 @@
|
|
|
103
103
|
"dependencies": {
|
|
104
104
|
"@babel/core": "^7.29.0",
|
|
105
105
|
"@babel/generator": "^7.29.1",
|
|
106
|
-
"@babel/parser": "^7.29.
|
|
106
|
+
"@babel/parser": "^7.29.2",
|
|
107
107
|
"@babel/traverse": "^7.29.0",
|
|
108
108
|
"@babel/types": "^7.29.0",
|
|
109
109
|
"@oxc-project/types": "^0.120.0",
|