@weapp-vite/ast 6.11.9 → 6.12.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/index.d.mts +9 -9
- package/dist/index.mjs +9 -9
- package/dist/operations/componentProps.d.mts +14 -1
- package/dist/operations/componentProps.mjs +7 -4
- package/dist/operations/featureFlags.d.mts +8 -1
- package/dist/operations/featureFlags.mjs +30 -30
- package/dist/operations/jsxAutoComponents.d.mts +16 -1
- package/dist/operations/jsxAutoComponents.mjs +42 -41
- package/dist/operations/onPageScroll.d.mts +30 -1
- package/dist/operations/onPageScroll.mjs +29 -24
- package/dist/operations/platformApi.d.mts +8 -2
- package/dist/operations/platformApi.mjs +20 -11
- package/dist/operations/require.d.mts +7 -1
- package/dist/operations/require.mjs +29 -21
- package/dist/operations/scriptSetupImports.d.mts +6 -1
- package/dist/operations/scriptSetupImports.mjs +19 -35
- package/dist/operations/setDataPick.d.mts +9 -1
- package/dist/operations/setDataPick.mjs +8 -8
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -2,14 +2,14 @@ import { BABEL_TS_MODULE_PARSER_OPTIONS, BABEL_TS_MODULE_PLUGINS, generate, getV
|
|
|
2
2
|
import { getObjectPropertyByKey, getRenderPropertyFromComponentOptions, resolveRenderExpressionFromComponentOptions, resolveRenderableExpression, toStaticObjectKey, unwrapTypeScriptExpression } from "./babelNodes.mjs";
|
|
3
3
|
import { AstEngineName, AstParserLike, WeappAstConfig } from "./types.mjs";
|
|
4
4
|
import { parseJsLikeWithEngine } from "./engine.mjs";
|
|
5
|
-
import { ComponentPropMap, collectComponentPropsFromCode } from "./operations/componentProps.mjs";
|
|
6
|
-
import { FeatureFlagOptions, collectFeatureFlagsFromCode } from "./operations/featureFlags.mjs";
|
|
7
|
-
import { JsxAutoComponentAnalysisOptions, JsxAutoComponentContext, JsxBabelModuleAnalysisOptions, JsxImportedComponent, collectJsxAutoComponentsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression } from "./operations/jsxAutoComponents.mjs";
|
|
8
|
-
import { collectOnPageScrollPerformanceWarnings } from "./operations/onPageScroll.mjs";
|
|
9
|
-
import { mayContainPlatformApiAccess, platformApiIdentifiers } from "./operations/platformApi.mjs";
|
|
10
|
-
import { RequireToken, collectRequireTokens, mayContainStaticRequireLiteral } from "./operations/require.mjs";
|
|
11
|
-
import { ScriptSetupImport, collectScriptSetupImportsFromCode } from "./operations/scriptSetupImports.mjs";
|
|
12
|
-
import { collectSetDataPickKeysFromTemplateCode } from "./operations/setDataPick.mjs";
|
|
5
|
+
import { ComponentPropMap, collectComponentPropsFromCode, collectComponentPropsWithBabel, collectComponentPropsWithOxc, extractComponentProperties, extractPropertiesObject, getStaticPropertyName, mapConstructorName, mayContainComponentPropsShape, mergeComponentPropTypes, resolveOptionsObjectExpression, resolveOptionsObjectExpressionWithBabel, resolveTypeFromNode } from "./operations/componentProps.mjs";
|
|
6
|
+
import { FeatureFlagOptions, collectFeatureFlagsFromCode, collectFeatureFlagsWithBabel, collectFeatureFlagsWithOxc, consumeNamedFeatureFlag, consumeNamespaceFeatureFlag, mayContainFeatureFlagHints, registerNamedFeatureFlagLocal, registerNamespaceFeatureFlagLocal } from "./operations/featureFlags.mjs";
|
|
7
|
+
import { JsxAutoComponentAnalysisOptions, JsxAutoComponentContext, JsxBabelModuleAnalysisOptions, JsxImportedComponent, collectJsxAutoComponentsFromCode, collectJsxAutoComponentsWithBabel, collectJsxAutoComponentsWithOxc, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectJsxTemplateTagsFromOxc, createJsxImportedComponent, defaultIsDefineComponentSource, defaultResolveBabelComponentExpression, defaultResolveBabelRenderExpression, getJsxImportLocalName, getJsxImportedName, getJsxOxcStaticPropertyName, isJsxDefineComponentImportSpecifier, mayContainJsxAutoComponentEntry, resolveOxcComponentExpression, resolveOxcRenderExpression, unwrapOxcExpression } from "./operations/jsxAutoComponents.mjs";
|
|
8
|
+
import { collectOnPageScrollPerformanceWarnings, collectOnPageScrollWarningsWithBabel, collectOnPageScrollWarningsWithOxc, collectPageScrollInspection, collectPageScrollInspectionWithOxc, createLineStartOffsets, createWarningPrefix, getCallExpressionCalleeName, getLocationFromOffset, getMemberExpressionPropertyName, getOnPageScrollCallbackArgument, getOxcCallExpressionCalleeName, getOxcMemberExpressionPropertyName, getOxcStaticPropertyName, isOnPageScrollCallee, isOxcFunctionLike, isOxcOnPageScrollCallee, isStaticPropertyName } from "./operations/onPageScroll.mjs";
|
|
9
|
+
import { hasPlatformApiMemberExpression, isPlatformApiIdentifier, isPlatformApiMemberExpression, mayContainPlatformApiAccess, mayContainPlatformApiIdentifierByText, platformApiIdentifierList, platformApiIdentifiers } from "./operations/platformApi.mjs";
|
|
10
|
+
import { RequireToken, collectRequireTokens, getRequireAsyncLiteralToken, getStaticRequireLiteralValue, hasStaticRequireCall, isStaticRequireCall, mayContainRequireCallByText, mayContainStaticRequireLiteral } from "./operations/require.mjs";
|
|
11
|
+
import { ScriptSetupImport, collectScriptSetupImportsFromCode, collectScriptSetupImportsWithBabel, collectScriptSetupImportsWithOxc, createScriptSetupImport, getScriptSetupImportedName, mayContainRelevantScriptSetupImports } from "./operations/scriptSetupImports.mjs";
|
|
12
|
+
import { collectIdentifiersFromExpression, collectIdentifiersFromExpressionWithOxc, collectLoopScopeAliases, collectPatternBindingNames, collectSetDataPickKeysFromTemplateCode, collectSetDataPickKeysWithBabel, collectSetDataPickKeysWithOxc, extractTemplateExpressions, hasBindingInScopes } from "./operations/setDataPick.mjs";
|
|
13
13
|
import * as _babel_types0 from "@babel/types";
|
|
14
14
|
import * as oxc_parser0 from "oxc-parser";
|
|
15
15
|
|
|
@@ -31,4 +31,4 @@ declare const oxcAstEngine: {
|
|
|
31
31
|
parseJsLike(code: string, filename?: string): oxc_parser0.Program;
|
|
32
32
|
};
|
|
33
33
|
//#endregion
|
|
34
|
-
export { type AstEngineName, type AstParserLike, BABEL_TS_MODULE_PARSER_OPTIONS, BABEL_TS_MODULE_PLUGINS, type ComponentPropMap, type FeatureFlagOptions, type JsxAutoComponentAnalysisOptions, type JsxAutoComponentContext, type JsxBabelModuleAnalysisOptions, type JsxImportedComponent, type RequireToken, type ScriptSetupImport, type WeappAstConfig, babelAstEngine, collectComponentPropsFromCode, collectFeatureFlagsFromCode, collectJsxAutoComponentsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectOnPageScrollPerformanceWarnings, collectRequireTokens, collectScriptSetupImportsFromCode, collectSetDataPickKeysFromTemplateCode, generate, getObjectPropertyByKey, getRenderPropertyFromComponentOptions, getVisitorKeys, mayContainPlatformApiAccess, mayContainStaticRequireLiteral, oxcAstEngine, parse, parseJsLike, parseJsLikeWithEngine, platformApiIdentifiers, resolveRenderExpressionFromComponentOptions, resolveRenderableExpression, toStaticObjectKey, traverse, unwrapTypeScriptExpression };
|
|
34
|
+
export { type AstEngineName, type AstParserLike, BABEL_TS_MODULE_PARSER_OPTIONS, BABEL_TS_MODULE_PLUGINS, type ComponentPropMap, type FeatureFlagOptions, type JsxAutoComponentAnalysisOptions, type JsxAutoComponentContext, type JsxBabelModuleAnalysisOptions, type JsxImportedComponent, type RequireToken, type ScriptSetupImport, type WeappAstConfig, babelAstEngine, collectComponentPropsFromCode, collectComponentPropsWithBabel, collectComponentPropsWithOxc, collectFeatureFlagsFromCode, collectFeatureFlagsWithBabel, collectFeatureFlagsWithOxc, collectIdentifiersFromExpression, collectIdentifiersFromExpressionWithOxc, collectJsxAutoComponentsFromCode, collectJsxAutoComponentsWithBabel, collectJsxAutoComponentsWithOxc, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectJsxTemplateTagsFromOxc, collectLoopScopeAliases, collectOnPageScrollPerformanceWarnings, collectOnPageScrollWarningsWithBabel, collectOnPageScrollWarningsWithOxc, collectPageScrollInspection, collectPageScrollInspectionWithOxc, collectPatternBindingNames, collectRequireTokens, collectScriptSetupImportsFromCode, collectScriptSetupImportsWithBabel, collectScriptSetupImportsWithOxc, collectSetDataPickKeysFromTemplateCode, collectSetDataPickKeysWithBabel, collectSetDataPickKeysWithOxc, consumeNamedFeatureFlag, consumeNamespaceFeatureFlag, createJsxImportedComponent, createLineStartOffsets, createScriptSetupImport, createWarningPrefix, defaultIsDefineComponentSource, defaultResolveBabelComponentExpression, defaultResolveBabelRenderExpression, extractComponentProperties, extractPropertiesObject, extractTemplateExpressions, generate, getCallExpressionCalleeName, getJsxImportLocalName, getJsxImportedName, getJsxOxcStaticPropertyName, getLocationFromOffset, getMemberExpressionPropertyName, getObjectPropertyByKey, getOnPageScrollCallbackArgument, getOxcCallExpressionCalleeName, getOxcMemberExpressionPropertyName, getOxcStaticPropertyName, getRenderPropertyFromComponentOptions, getRequireAsyncLiteralToken, getScriptSetupImportedName, getStaticPropertyName, getStaticRequireLiteralValue, getVisitorKeys, hasBindingInScopes, hasPlatformApiMemberExpression, hasStaticRequireCall, isJsxDefineComponentImportSpecifier, isOnPageScrollCallee, isOxcFunctionLike, isOxcOnPageScrollCallee, isPlatformApiIdentifier, isPlatformApiMemberExpression, isStaticPropertyName, isStaticRequireCall, mapConstructorName, mayContainComponentPropsShape, mayContainFeatureFlagHints, mayContainJsxAutoComponentEntry, mayContainPlatformApiAccess, mayContainPlatformApiIdentifierByText, mayContainRelevantScriptSetupImports, mayContainRequireCallByText, mayContainStaticRequireLiteral, mergeComponentPropTypes, oxcAstEngine, parse, parseJsLike, parseJsLikeWithEngine, platformApiIdentifierList, platformApiIdentifiers, registerNamedFeatureFlagLocal, registerNamespaceFeatureFlagLocal, resolveOptionsObjectExpression, resolveOptionsObjectExpressionWithBabel, resolveOxcComponentExpression, resolveOxcRenderExpression, resolveRenderExpressionFromComponentOptions, resolveRenderableExpression, resolveTypeFromNode, toStaticObjectKey, traverse, unwrapOxcExpression, unwrapTypeScriptExpression };
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { BABEL_TS_MODULE_PARSER_OPTIONS, BABEL_TS_MODULE_PLUGINS, generate, getVisitorKeys, parse, parseJsLike, traverse } from "./babel.mjs";
|
|
2
2
|
import { getObjectPropertyByKey, getRenderPropertyFromComponentOptions, resolveRenderExpressionFromComponentOptions, resolveRenderableExpression, toStaticObjectKey, unwrapTypeScriptExpression } from "./babelNodes.mjs";
|
|
3
3
|
import { n as oxcAstEngine, r as babelAstEngine, t as parseJsLikeWithEngine } from "./engine-DHqNPCDA.mjs";
|
|
4
|
-
import { collectComponentPropsFromCode } from "./operations/componentProps.mjs";
|
|
5
|
-
import { collectFeatureFlagsFromCode } from "./operations/featureFlags.mjs";
|
|
6
|
-
import { collectJsxAutoComponentsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression } from "./operations/jsxAutoComponents.mjs";
|
|
7
|
-
import { collectOnPageScrollPerformanceWarnings } from "./operations/onPageScroll.mjs";
|
|
8
|
-
import { mayContainPlatformApiAccess, platformApiIdentifiers } from "./operations/platformApi.mjs";
|
|
9
|
-
import { collectRequireTokens, mayContainStaticRequireLiteral } from "./operations/require.mjs";
|
|
10
|
-
import { collectScriptSetupImportsFromCode } from "./operations/scriptSetupImports.mjs";
|
|
11
|
-
import { collectSetDataPickKeysFromTemplateCode } from "./operations/setDataPick.mjs";
|
|
12
|
-
export { BABEL_TS_MODULE_PARSER_OPTIONS, BABEL_TS_MODULE_PLUGINS, babelAstEngine, collectComponentPropsFromCode, collectFeatureFlagsFromCode, collectJsxAutoComponentsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectOnPageScrollPerformanceWarnings, collectRequireTokens, collectScriptSetupImportsFromCode, collectSetDataPickKeysFromTemplateCode, generate, getObjectPropertyByKey, getRenderPropertyFromComponentOptions, getVisitorKeys, mayContainPlatformApiAccess, mayContainStaticRequireLiteral, oxcAstEngine, parse, parseJsLike, parseJsLikeWithEngine, platformApiIdentifiers, resolveRenderExpressionFromComponentOptions, resolveRenderableExpression, toStaticObjectKey, traverse, unwrapTypeScriptExpression };
|
|
4
|
+
import { collectComponentPropsFromCode, collectComponentPropsWithBabel, collectComponentPropsWithOxc, extractComponentProperties, extractPropertiesObject, getStaticPropertyName, mapConstructorName, mayContainComponentPropsShape, mergeComponentPropTypes, resolveOptionsObjectExpression, resolveOptionsObjectExpressionWithBabel, resolveTypeFromNode } from "./operations/componentProps.mjs";
|
|
5
|
+
import { collectFeatureFlagsFromCode, collectFeatureFlagsWithBabel, collectFeatureFlagsWithOxc, consumeNamedFeatureFlag, consumeNamespaceFeatureFlag, mayContainFeatureFlagHints, registerNamedFeatureFlagLocal, registerNamespaceFeatureFlagLocal } from "./operations/featureFlags.mjs";
|
|
6
|
+
import { collectJsxAutoComponentsFromCode, collectJsxAutoComponentsWithBabel, collectJsxAutoComponentsWithOxc, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectJsxTemplateTagsFromOxc, createJsxImportedComponent, defaultIsDefineComponentSource, defaultResolveBabelComponentExpression, defaultResolveBabelRenderExpression, getJsxImportLocalName, getJsxImportedName, getJsxOxcStaticPropertyName, isJsxDefineComponentImportSpecifier, mayContainJsxAutoComponentEntry, resolveOxcComponentExpression, resolveOxcRenderExpression, unwrapOxcExpression } from "./operations/jsxAutoComponents.mjs";
|
|
7
|
+
import { collectOnPageScrollPerformanceWarnings, collectOnPageScrollWarningsWithBabel, collectOnPageScrollWarningsWithOxc, collectPageScrollInspection, collectPageScrollInspectionWithOxc, createLineStartOffsets, createWarningPrefix, getCallExpressionCalleeName, getLocationFromOffset, getMemberExpressionPropertyName, getOnPageScrollCallbackArgument, getOxcCallExpressionCalleeName, getOxcMemberExpressionPropertyName, getOxcStaticPropertyName, isOnPageScrollCallee, isOxcFunctionLike, isOxcOnPageScrollCallee, isStaticPropertyName } from "./operations/onPageScroll.mjs";
|
|
8
|
+
import { hasPlatformApiMemberExpression, isPlatformApiIdentifier, isPlatformApiMemberExpression, mayContainPlatformApiAccess, mayContainPlatformApiIdentifierByText, platformApiIdentifierList, platformApiIdentifiers } from "./operations/platformApi.mjs";
|
|
9
|
+
import { collectRequireTokens, getRequireAsyncLiteralToken, getStaticRequireLiteralValue, hasStaticRequireCall, isStaticRequireCall, mayContainRequireCallByText, mayContainStaticRequireLiteral } from "./operations/require.mjs";
|
|
10
|
+
import { collectScriptSetupImportsFromCode, collectScriptSetupImportsWithBabel, collectScriptSetupImportsWithOxc, createScriptSetupImport, getScriptSetupImportedName, mayContainRelevantScriptSetupImports } from "./operations/scriptSetupImports.mjs";
|
|
11
|
+
import { collectIdentifiersFromExpression, collectIdentifiersFromExpressionWithOxc, collectLoopScopeAliases, collectPatternBindingNames, collectSetDataPickKeysFromTemplateCode, collectSetDataPickKeysWithBabel, collectSetDataPickKeysWithOxc, extractTemplateExpressions, hasBindingInScopes } from "./operations/setDataPick.mjs";
|
|
12
|
+
export { BABEL_TS_MODULE_PARSER_OPTIONS, BABEL_TS_MODULE_PLUGINS, babelAstEngine, collectComponentPropsFromCode, collectComponentPropsWithBabel, collectComponentPropsWithOxc, collectFeatureFlagsFromCode, collectFeatureFlagsWithBabel, collectFeatureFlagsWithOxc, collectIdentifiersFromExpression, collectIdentifiersFromExpressionWithOxc, collectJsxAutoComponentsFromCode, collectJsxAutoComponentsWithBabel, collectJsxAutoComponentsWithOxc, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectJsxTemplateTagsFromOxc, collectLoopScopeAliases, collectOnPageScrollPerformanceWarnings, collectOnPageScrollWarningsWithBabel, collectOnPageScrollWarningsWithOxc, collectPageScrollInspection, collectPageScrollInspectionWithOxc, collectPatternBindingNames, collectRequireTokens, collectScriptSetupImportsFromCode, collectScriptSetupImportsWithBabel, collectScriptSetupImportsWithOxc, collectSetDataPickKeysFromTemplateCode, collectSetDataPickKeysWithBabel, collectSetDataPickKeysWithOxc, consumeNamedFeatureFlag, consumeNamespaceFeatureFlag, createJsxImportedComponent, createLineStartOffsets, createScriptSetupImport, createWarningPrefix, defaultIsDefineComponentSource, defaultResolveBabelComponentExpression, defaultResolveBabelRenderExpression, extractComponentProperties, extractPropertiesObject, extractTemplateExpressions, generate, getCallExpressionCalleeName, getJsxImportLocalName, getJsxImportedName, getJsxOxcStaticPropertyName, getLocationFromOffset, getMemberExpressionPropertyName, getObjectPropertyByKey, getOnPageScrollCallbackArgument, getOxcCallExpressionCalleeName, getOxcMemberExpressionPropertyName, getOxcStaticPropertyName, getRenderPropertyFromComponentOptions, getRequireAsyncLiteralToken, getScriptSetupImportedName, getStaticPropertyName, getStaticRequireLiteralValue, getVisitorKeys, hasBindingInScopes, hasPlatformApiMemberExpression, hasStaticRequireCall, isJsxDefineComponentImportSpecifier, isOnPageScrollCallee, isOxcFunctionLike, isOxcOnPageScrollCallee, isPlatformApiIdentifier, isPlatformApiMemberExpression, isStaticPropertyName, isStaticRequireCall, mapConstructorName, mayContainComponentPropsShape, mayContainFeatureFlagHints, mayContainJsxAutoComponentEntry, mayContainPlatformApiAccess, mayContainPlatformApiIdentifierByText, mayContainRelevantScriptSetupImports, mayContainRequireCallByText, mayContainStaticRequireLiteral, mergeComponentPropTypes, oxcAstEngine, parse, parseJsLike, parseJsLikeWithEngine, platformApiIdentifierList, platformApiIdentifiers, registerNamedFeatureFlagLocal, registerNamespaceFeatureFlagLocal, resolveOptionsObjectExpression, resolveOptionsObjectExpressionWithBabel, resolveOxcComponentExpression, resolveOxcRenderExpression, resolveRenderExpressionFromComponentOptions, resolveRenderableExpression, resolveTypeFromNode, toStaticObjectKey, traverse, unwrapOxcExpression, unwrapTypeScriptExpression };
|
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
import { AstEngineName } from "../types.mjs";
|
|
2
|
+
import * as t from "@babel/types";
|
|
3
|
+
import { NodePath } from "@babel/traverse";
|
|
2
4
|
|
|
3
5
|
//#region src/operations/componentProps.d.ts
|
|
4
6
|
type ComponentPropMap = Map<string, string>;
|
|
7
|
+
declare function mayContainComponentPropsShape(code: string): boolean;
|
|
8
|
+
declare function mapConstructorName(name: string): string;
|
|
9
|
+
declare function resolveTypeFromNode(node: any): string;
|
|
10
|
+
declare function getStaticPropertyName(node: any): any;
|
|
11
|
+
declare function mergeComponentPropTypes(primaryType: string | undefined, optionalTypes: Array<string | undefined>): string;
|
|
12
|
+
declare function extractPropertiesObject(node: any): ComponentPropMap | undefined;
|
|
13
|
+
declare function resolveOptionsObjectExpression(node: any, bindings: Map<string, any>): any;
|
|
14
|
+
declare function resolveOptionsObjectExpressionWithBabel(path: NodePath<t.CallExpression>, node: t.Node | null | undefined): t.ObjectExpression | undefined;
|
|
15
|
+
declare function extractComponentProperties(optionsNode: any): ComponentPropMap;
|
|
16
|
+
declare function collectComponentPropsWithBabel(code: string): ComponentPropMap;
|
|
17
|
+
declare function collectComponentPropsWithOxc(code: string): ComponentPropMap;
|
|
5
18
|
/**
|
|
6
19
|
* 从源码中提取组件属性类型映射。
|
|
7
20
|
*/
|
|
@@ -9,4 +22,4 @@ declare function collectComponentPropsFromCode(code: string, options?: {
|
|
|
9
22
|
astEngine?: AstEngineName;
|
|
10
23
|
}): ComponentPropMap;
|
|
11
24
|
//#endregion
|
|
12
|
-
export { ComponentPropMap, collectComponentPropsFromCode };
|
|
25
|
+
export { ComponentPropMap, collectComponentPropsFromCode, collectComponentPropsWithBabel, collectComponentPropsWithOxc, extractComponentProperties, extractPropertiesObject, getStaticPropertyName, mapConstructorName, mayContainComponentPropsShape, mergeComponentPropTypes, resolveOptionsObjectExpression, resolveOptionsObjectExpressionWithBabel, resolveTypeFromNode };
|
|
@@ -64,6 +64,11 @@ function getStaticPropertyName(node) {
|
|
|
64
64
|
if (node.type === "NumericLiteral") return String(node.value);
|
|
65
65
|
if (node.type === "Literal" && typeof node.value === "number") return String(node.value);
|
|
66
66
|
}
|
|
67
|
+
function mergeComponentPropTypes(primaryType, optionalTypes) {
|
|
68
|
+
const typeCandidates = [primaryType, ...optionalTypes].filter((candidate) => Boolean(candidate && candidate.trim().length > 0));
|
|
69
|
+
const deduped = [...new Set(typeCandidates)];
|
|
70
|
+
return deduped.length > 0 ? deduped.join(" | ") : "any";
|
|
71
|
+
}
|
|
67
72
|
function extractPropertiesObject(node) {
|
|
68
73
|
if (!node || node.type !== "ObjectExpression") return;
|
|
69
74
|
const propMap = /* @__PURE__ */ new Map();
|
|
@@ -85,9 +90,7 @@ function extractPropertiesObject(node) {
|
|
|
85
90
|
optionalTypes.push(resolveTypeFromNode(element));
|
|
86
91
|
}
|
|
87
92
|
}
|
|
88
|
-
|
|
89
|
-
const deduped = [...new Set(typeCandidates)];
|
|
90
|
-
propMap.set(name, deduped.length > 0 ? deduped.join(" | ") : "any");
|
|
93
|
+
propMap.set(name, mergeComponentPropTypes(primaryType, optionalTypes));
|
|
91
94
|
continue;
|
|
92
95
|
}
|
|
93
96
|
propMap.set(name, resolveTypeFromNode(value));
|
|
@@ -172,4 +175,4 @@ function collectComponentPropsFromCode(code, options) {
|
|
|
172
175
|
}
|
|
173
176
|
}
|
|
174
177
|
//#endregion
|
|
175
|
-
export { collectComponentPropsFromCode };
|
|
178
|
+
export { collectComponentPropsFromCode, collectComponentPropsWithBabel, collectComponentPropsWithOxc, extractComponentProperties, extractPropertiesObject, getStaticPropertyName, mapConstructorName, mayContainComponentPropsShape, mergeComponentPropTypes, resolveOptionsObjectExpression, resolveOptionsObjectExpressionWithBabel, resolveTypeFromNode };
|
|
@@ -6,9 +6,16 @@ interface FeatureFlagOptions<TFeature extends string> {
|
|
|
6
6
|
moduleId: string;
|
|
7
7
|
hookToFeature: Record<string, TFeature>;
|
|
8
8
|
}
|
|
9
|
+
declare function mayContainFeatureFlagHints<TFeature extends string>(code: string, moduleId: string, hookToFeature: Record<string, TFeature>): boolean;
|
|
10
|
+
declare function consumeNamedFeatureFlag<TFeature extends string>(enabled: Set<TFeature>, namedHookLocals: Map<string, TFeature>, name: string): void;
|
|
11
|
+
declare function consumeNamespaceFeatureFlag<TFeature extends string>(enabled: Set<TFeature>, namespaceLocals: Set<string>, hookToFeature: Record<string, TFeature>, namespace: string, hookName: string): void;
|
|
12
|
+
declare function registerNamedFeatureFlagLocal<TFeature extends string>(namedHookLocals: Map<string, TFeature>, hookToFeature: Record<string, TFeature>, importedName: string, localName: string): void;
|
|
13
|
+
declare function registerNamespaceFeatureFlagLocal(namespaceLocals: Set<string>, localName: string | undefined): void;
|
|
14
|
+
declare function collectFeatureFlagsWithBabel<TFeature extends string>(code: string, moduleId: string, hookToFeature: Record<string, TFeature>): Set<TFeature>;
|
|
15
|
+
declare function collectFeatureFlagsWithOxc<TFeature extends string>(code: string, moduleId: string, hookToFeature: Record<string, TFeature>): Set<TFeature>;
|
|
9
16
|
/**
|
|
10
17
|
* 根据模块 ID 与 hook 映射表,从源码中收集启用的特性标识。
|
|
11
18
|
*/
|
|
12
19
|
declare function collectFeatureFlagsFromCode<TFeature extends string>(code: string, options: FeatureFlagOptions<TFeature>): Set<TFeature>;
|
|
13
20
|
//#endregion
|
|
14
|
-
export { FeatureFlagOptions, collectFeatureFlagsFromCode };
|
|
21
|
+
export { FeatureFlagOptions, collectFeatureFlagsFromCode, collectFeatureFlagsWithBabel, collectFeatureFlagsWithOxc, consumeNamedFeatureFlag, consumeNamespaceFeatureFlag, mayContainFeatureFlagHints, registerNamedFeatureFlagLocal, registerNamespaceFeatureFlagLocal };
|
|
@@ -7,55 +7,60 @@ function mayContainFeatureFlagHints(code, moduleId, hookToFeature) {
|
|
|
7
7
|
if (!code.includes(moduleId)) return false;
|
|
8
8
|
return Object.keys(hookToFeature).some((hookName) => code.includes(hookName));
|
|
9
9
|
}
|
|
10
|
-
function
|
|
10
|
+
function consumeNamedFeatureFlag(enabled, namedHookLocals, name) {
|
|
11
|
+
const matched = namedHookLocals.get(name);
|
|
12
|
+
if (matched) enabled.add(matched);
|
|
13
|
+
}
|
|
14
|
+
function consumeNamespaceFeatureFlag(enabled, namespaceLocals, hookToFeature, namespace, hookName) {
|
|
15
|
+
if (!namespaceLocals.has(namespace)) return;
|
|
16
|
+
const matched = hookToFeature[hookName];
|
|
17
|
+
if (matched) enabled.add(matched);
|
|
18
|
+
}
|
|
19
|
+
function registerNamedFeatureFlagLocal(namedHookLocals, hookToFeature, importedName, localName) {
|
|
20
|
+
const matched = hookToFeature[importedName];
|
|
21
|
+
if (matched) namedHookLocals.set(localName, matched);
|
|
22
|
+
}
|
|
23
|
+
function registerNamespaceFeatureFlagLocal(namespaceLocals, localName) {
|
|
24
|
+
if (localName) namespaceLocals.add(localName);
|
|
25
|
+
}
|
|
26
|
+
function collectFeatureFlagsWithBabel(code, moduleId, hookToFeature) {
|
|
11
27
|
const ast = parseJsLike(code);
|
|
12
28
|
const namedHookLocals = /* @__PURE__ */ new Map();
|
|
13
29
|
const namespaceLocals = /* @__PURE__ */ new Set();
|
|
14
30
|
for (const stmt of ast.program.body) {
|
|
15
31
|
if (!t.isImportDeclaration(stmt) || stmt.source.value !== moduleId) continue;
|
|
16
|
-
for (const specifier of stmt.specifiers) if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported))
|
|
17
|
-
|
|
18
|
-
if (matched) namedHookLocals.set(specifier.local.name, matched);
|
|
19
|
-
} else if (t.isImportNamespaceSpecifier(specifier)) namespaceLocals.add(specifier.local.name);
|
|
32
|
+
for (const specifier of stmt.specifiers) if (t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported)) registerNamedFeatureFlagLocal(namedHookLocals, hookToFeature, specifier.imported.name, specifier.local.name);
|
|
33
|
+
else if (t.isImportNamespaceSpecifier(specifier)) registerNamespaceFeatureFlagLocal(namespaceLocals, specifier.local.name);
|
|
20
34
|
}
|
|
21
35
|
if (namedHookLocals.size === 0 && namespaceLocals.size === 0) return /* @__PURE__ */ new Set();
|
|
22
36
|
const enabled = /* @__PURE__ */ new Set();
|
|
23
|
-
function consumeHookCallByName(name) {
|
|
24
|
-
const matched = namedHookLocals.get(name);
|
|
25
|
-
if (matched) enabled.add(matched);
|
|
26
|
-
}
|
|
27
|
-
function consumeNamespaceHookCall(namespace, hookName) {
|
|
28
|
-
if (!namespaceLocals.has(namespace)) return;
|
|
29
|
-
const matched = hookToFeature[hookName];
|
|
30
|
-
if (matched) enabled.add(matched);
|
|
31
|
-
}
|
|
32
37
|
traverse(ast, {
|
|
33
38
|
CallExpression(path) {
|
|
34
39
|
const callee = path.node.callee;
|
|
35
40
|
if (t.isIdentifier(callee)) {
|
|
36
|
-
|
|
41
|
+
consumeNamedFeatureFlag(enabled, namedHookLocals, callee.name);
|
|
37
42
|
return;
|
|
38
43
|
}
|
|
39
44
|
if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.object)) {
|
|
40
45
|
const property = callee.property;
|
|
41
|
-
if (t.isIdentifier(property))
|
|
46
|
+
if (t.isIdentifier(property)) consumeNamespaceFeatureFlag(enabled, namespaceLocals, hookToFeature, callee.object.name, property.name);
|
|
42
47
|
}
|
|
43
48
|
},
|
|
44
49
|
OptionalCallExpression(path) {
|
|
45
50
|
const callee = path.node.callee;
|
|
46
51
|
if (t.isIdentifier(callee)) {
|
|
47
|
-
|
|
52
|
+
consumeNamedFeatureFlag(enabled, namedHookLocals, callee.name);
|
|
48
53
|
return;
|
|
49
54
|
}
|
|
50
55
|
if (t.isMemberExpression(callee) && !callee.computed && t.isIdentifier(callee.object)) {
|
|
51
56
|
const property = callee.property;
|
|
52
|
-
if (t.isIdentifier(property))
|
|
57
|
+
if (t.isIdentifier(property)) consumeNamespaceFeatureFlag(enabled, namespaceLocals, hookToFeature, callee.object.name, property.name);
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
});
|
|
56
61
|
return enabled;
|
|
57
62
|
}
|
|
58
|
-
function
|
|
63
|
+
function collectFeatureFlagsWithOxc(code, moduleId, hookToFeature) {
|
|
59
64
|
const ast = parseJsLikeWithEngine(code, {
|
|
60
65
|
engine: "oxc",
|
|
61
66
|
filename: "inline.ts"
|
|
@@ -65,9 +70,8 @@ function collectWithOxc(code, moduleId, hookToFeature) {
|
|
|
65
70
|
for (const statement of ast.body ?? []) {
|
|
66
71
|
if (statement?.type !== "ImportDeclaration" || statement.source?.value !== moduleId) continue;
|
|
67
72
|
for (const specifier of statement.specifiers ?? []) if (specifier.type === "ImportSpecifier" && specifier.imported?.type === "Identifier") {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
} else if (specifier.type === "ImportNamespaceSpecifier" && specifier.local?.type === "Identifier") namespaceLocals.add(specifier.local.name);
|
|
73
|
+
if (specifier.local?.type === "Identifier") registerNamedFeatureFlagLocal(namedHookLocals, hookToFeature, specifier.imported.name, specifier.local.name);
|
|
74
|
+
} else if (specifier.type === "ImportNamespaceSpecifier") registerNamespaceFeatureFlagLocal(namespaceLocals, specifier.local?.type === "Identifier" ? specifier.local.name : void 0);
|
|
71
75
|
}
|
|
72
76
|
if (namedHookLocals.size === 0 && namespaceLocals.size === 0) return /* @__PURE__ */ new Set();
|
|
73
77
|
const enabled = /* @__PURE__ */ new Set();
|
|
@@ -75,14 +79,10 @@ function collectWithOxc(code, moduleId, hookToFeature) {
|
|
|
75
79
|
if (node.type !== "CallExpression") return;
|
|
76
80
|
const callee = node.callee;
|
|
77
81
|
if (callee?.type === "Identifier") {
|
|
78
|
-
|
|
79
|
-
if (matched) enabled.add(matched);
|
|
82
|
+
consumeNamedFeatureFlag(enabled, namedHookLocals, callee.name);
|
|
80
83
|
return;
|
|
81
84
|
}
|
|
82
|
-
if (callee?.type === "MemberExpression" && callee.object?.type === "Identifier" && callee.property?.type === "Identifier"
|
|
83
|
-
const matched = hookToFeature[callee.property.name];
|
|
84
|
-
if (matched) enabled.add(matched);
|
|
85
|
-
}
|
|
85
|
+
if (callee?.type === "MemberExpression" && callee.object?.type === "Identifier" && callee.property?.type === "Identifier") consumeNamespaceFeatureFlag(enabled, namespaceLocals, hookToFeature, callee.object.name, callee.property.name);
|
|
86
86
|
} });
|
|
87
87
|
return enabled;
|
|
88
88
|
}
|
|
@@ -93,10 +93,10 @@ function collectFeatureFlagsFromCode(code, options) {
|
|
|
93
93
|
if (!mayContainFeatureFlagHints(code, options.moduleId, options.hookToFeature)) return /* @__PURE__ */ new Set();
|
|
94
94
|
const engine = options.astEngine ?? "babel";
|
|
95
95
|
try {
|
|
96
|
-
return engine === "oxc" ?
|
|
96
|
+
return engine === "oxc" ? collectFeatureFlagsWithOxc(code, options.moduleId, options.hookToFeature) : collectFeatureFlagsWithBabel(code, options.moduleId, options.hookToFeature);
|
|
97
97
|
} catch {
|
|
98
98
|
return /* @__PURE__ */ new Set();
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
//#endregion
|
|
102
|
-
export { collectFeatureFlagsFromCode };
|
|
102
|
+
export { collectFeatureFlagsFromCode, collectFeatureFlagsWithBabel, collectFeatureFlagsWithOxc, consumeNamedFeatureFlag, consumeNamespaceFeatureFlag, mayContainFeatureFlagHints, registerNamedFeatureFlagLocal, registerNamespaceFeatureFlagLocal };
|
|
@@ -24,14 +24,29 @@ interface JsxBabelModuleAnalysisOptions {
|
|
|
24
24
|
isDefineComponentSource?: (source: string) => boolean;
|
|
25
25
|
resolveBabelComponentExpression?: (declaration: t.Declaration | t.Expression | null, defineComponentDecls: Map<string, ObjectExpression>, defineComponentAliases: Set<string>) => Expression | null;
|
|
26
26
|
}
|
|
27
|
+
declare function mayContainJsxAutoComponentEntry(source: string): boolean;
|
|
28
|
+
declare function defaultIsDefineComponentSource(source: string): source is "vue";
|
|
29
|
+
declare function getJsxImportedName(imported: any): any;
|
|
30
|
+
declare function createJsxImportedComponent(localName: string, importSource: string, kind: JsxImportedComponent['kind'], imported?: any): JsxImportedComponent;
|
|
31
|
+
declare function getJsxImportLocalName(specifier: any): any;
|
|
32
|
+
declare function isJsxDefineComponentImportSpecifier(specifier: any): boolean;
|
|
33
|
+
declare function defaultResolveBabelComponentExpression(declaration: t.Declaration | t.Expression | null, defineComponentDecls: Map<string, ObjectExpression>, defineComponentAliases: Set<string>): Expression | null;
|
|
34
|
+
declare function defaultResolveBabelRenderExpression(componentExpr: Expression): Expression | null;
|
|
27
35
|
declare function collectJsxImportedComponentsAndDefaultExportFromBabelAst(ast: t.File, options?: JsxBabelModuleAnalysisOptions): {
|
|
28
36
|
importedComponents: JsxImportedComponent[];
|
|
29
37
|
exportDefaultExpression: null;
|
|
30
38
|
};
|
|
31
39
|
declare function collectJsxTemplateTagsFromBabelExpression(renderExpression: Expression, isCollectableTag: (tag: string) => boolean): Set<string>;
|
|
40
|
+
declare function unwrapOxcExpression(node: any): any;
|
|
41
|
+
declare function getJsxOxcStaticPropertyName(node: any): any;
|
|
42
|
+
declare function resolveOxcComponentExpression(declaration: any, defineComponentDecls: Map<string, any>, defineComponentAliases: Set<string>): any;
|
|
43
|
+
declare function resolveOxcRenderExpression(componentExpr: any): any;
|
|
44
|
+
declare function collectJsxTemplateTagsFromOxc(renderExpression: any, isCollectableTag: (tag: string) => boolean): Set<string>;
|
|
45
|
+
declare function collectJsxAutoComponentsWithBabel(source: string, options: Required<JsxAutoComponentAnalysisOptions>): JsxAutoComponentContext;
|
|
46
|
+
declare function collectJsxAutoComponentsWithOxc(source: string, options: Required<Pick<JsxAutoComponentAnalysisOptions, 'isCollectableTag' | 'isDefineComponentSource'>>): JsxAutoComponentContext;
|
|
32
47
|
/**
|
|
33
48
|
* 从 JSX 源码中收集自动 usingComponents 所需的导入组件与模板标签。
|
|
34
49
|
*/
|
|
35
50
|
declare function collectJsxAutoComponentsFromCode(source: string, options: JsxAutoComponentAnalysisOptions): JsxAutoComponentContext;
|
|
36
51
|
//#endregion
|
|
37
|
-
export { JsxAutoComponentAnalysisOptions, JsxAutoComponentContext, JsxBabelModuleAnalysisOptions, JsxImportedComponent, collectJsxAutoComponentsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression };
|
|
52
|
+
export { JsxAutoComponentAnalysisOptions, JsxAutoComponentContext, JsxBabelModuleAnalysisOptions, JsxImportedComponent, collectJsxAutoComponentsFromCode, collectJsxAutoComponentsWithBabel, collectJsxAutoComponentsWithOxc, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectJsxTemplateTagsFromOxc, createJsxImportedComponent, defaultIsDefineComponentSource, defaultResolveBabelComponentExpression, defaultResolveBabelRenderExpression, getJsxImportLocalName, getJsxImportedName, getJsxOxcStaticPropertyName, isJsxDefineComponentImportSpecifier, mayContainJsxAutoComponentEntry, resolveOxcComponentExpression, resolveOxcRenderExpression, unwrapOxcExpression };
|
|
@@ -10,6 +10,24 @@ function mayContainJsxAutoComponentEntry(source) {
|
|
|
10
10
|
function defaultIsDefineComponentSource(source) {
|
|
11
11
|
return source === "vue";
|
|
12
12
|
}
|
|
13
|
+
function getJsxImportedName(imported) {
|
|
14
|
+
return t.isIdentifier(imported) ? imported.name : t.isStringLiteral(imported) ? imported.value : imported?.type === "Identifier" ? imported.name : imported?.type === "StringLiteral" ? imported.value : void 0;
|
|
15
|
+
}
|
|
16
|
+
function createJsxImportedComponent(localName, importSource, kind, imported) {
|
|
17
|
+
return {
|
|
18
|
+
localName,
|
|
19
|
+
importSource,
|
|
20
|
+
importedName: kind === "default" ? "default" : getJsxImportedName(imported),
|
|
21
|
+
kind
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function getJsxImportLocalName(specifier) {
|
|
25
|
+
if ("local" in (specifier ?? {}) && t.isIdentifier(specifier.local)) return specifier.local.name;
|
|
26
|
+
return specifier?.local?.type === "Identifier" ? specifier.local.name : void 0;
|
|
27
|
+
}
|
|
28
|
+
function isJsxDefineComponentImportSpecifier(specifier) {
|
|
29
|
+
return t.isImportSpecifier(specifier) ? t.isIdentifier(specifier.imported, { name: "defineComponent" }) : specifier?.type === "ImportSpecifier" && specifier.imported?.type === "Identifier" && specifier.imported.name === "defineComponent";
|
|
30
|
+
}
|
|
13
31
|
function defaultResolveBabelComponentExpression(declaration, defineComponentDecls, defineComponentAliases) {
|
|
14
32
|
if (!declaration) return null;
|
|
15
33
|
if (t.isObjectExpression(declaration)) return declaration;
|
|
@@ -40,35 +58,25 @@ function collectJsxImportedComponentsAndDefaultExportFromBabelAst(ast, options =
|
|
|
40
58
|
traverse(ast, {
|
|
41
59
|
ImportDeclaration(path) {
|
|
42
60
|
const source = path.node.source.value;
|
|
43
|
-
if (resolvedOptions.isDefineComponentSource(source))
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
61
|
+
if (resolvedOptions.isDefineComponentSource(source)) {
|
|
62
|
+
for (const specifier of path.node.specifiers) if (isJsxDefineComponentImportSpecifier(specifier)) {
|
|
63
|
+
const localName = getJsxImportLocalName(specifier);
|
|
64
|
+
if (localName) defineComponentAliases.add(localName);
|
|
65
|
+
}
|
|
47
66
|
}
|
|
48
67
|
if (path.node.importKind === "type") return;
|
|
49
68
|
if (!t.isStringLiteral(path.node.source)) return;
|
|
50
69
|
const importSource = path.node.source.value;
|
|
51
70
|
for (const specifier of path.node.specifiers) {
|
|
52
71
|
if ("importKind" in specifier && specifier.importKind === "type") continue;
|
|
53
|
-
|
|
54
|
-
|
|
72
|
+
const localName = getJsxImportLocalName(specifier);
|
|
73
|
+
if (!localName) continue;
|
|
55
74
|
if (t.isImportDefaultSpecifier(specifier)) {
|
|
56
|
-
imports.set(localName,
|
|
57
|
-
localName,
|
|
58
|
-
importSource,
|
|
59
|
-
importedName: "default",
|
|
60
|
-
kind: "default"
|
|
61
|
-
});
|
|
75
|
+
imports.set(localName, createJsxImportedComponent(localName, importSource, "default"));
|
|
62
76
|
continue;
|
|
63
77
|
}
|
|
64
78
|
if (!t.isImportSpecifier(specifier)) continue;
|
|
65
|
-
|
|
66
|
-
imports.set(localName, {
|
|
67
|
-
localName,
|
|
68
|
-
importSource,
|
|
69
|
-
importedName,
|
|
70
|
-
kind: "named"
|
|
71
|
-
});
|
|
79
|
+
imports.set(localName, createJsxImportedComponent(localName, importSource, "named", specifier.imported));
|
|
72
80
|
}
|
|
73
81
|
},
|
|
74
82
|
VariableDeclarator(path) {
|
|
@@ -150,7 +158,7 @@ function unwrapOxcExpression(node) {
|
|
|
150
158
|
while (current && (current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSNonNullExpression" || current.type === "ParenthesizedExpression")) current = current.expression;
|
|
151
159
|
return current;
|
|
152
160
|
}
|
|
153
|
-
function
|
|
161
|
+
function getJsxOxcStaticPropertyName(node) {
|
|
154
162
|
const current = unwrapOxcExpression(node);
|
|
155
163
|
if (!current) return;
|
|
156
164
|
if (current.type === "Identifier") return current.name;
|
|
@@ -173,7 +181,7 @@ function resolveOxcComponentExpression(declaration, defineComponentDecls, define
|
|
|
173
181
|
function resolveOxcRenderExpression(componentExpr) {
|
|
174
182
|
if (componentExpr?.type !== "ObjectExpression") return null;
|
|
175
183
|
let renderProperty = null;
|
|
176
|
-
for (const property of componentExpr.properties ?? []) if (
|
|
184
|
+
for (const property of componentExpr.properties ?? []) if (getJsxOxcStaticPropertyName(property?.key) === "render") {
|
|
177
185
|
renderProperty = property;
|
|
178
186
|
break;
|
|
179
187
|
}
|
|
@@ -200,7 +208,7 @@ function collectJsxTemplateTagsFromOxc(renderExpression, isCollectableTag) {
|
|
|
200
208
|
} });
|
|
201
209
|
return tags;
|
|
202
210
|
}
|
|
203
|
-
function
|
|
211
|
+
function collectJsxAutoComponentsWithBabel(source, options) {
|
|
204
212
|
const { importedComponents, exportDefaultExpression } = collectJsxImportedComponentsAndDefaultExportFromBabelAst(parse(source, BABEL_TS_MODULE_PARSER_OPTIONS), options);
|
|
205
213
|
if (!exportDefaultExpression) return {
|
|
206
214
|
templateTags: /* @__PURE__ */ new Set(),
|
|
@@ -216,7 +224,7 @@ function collectWithBabel(source, options) {
|
|
|
216
224
|
importedComponents
|
|
217
225
|
};
|
|
218
226
|
}
|
|
219
|
-
function
|
|
227
|
+
function collectJsxAutoComponentsWithOxc(source, options) {
|
|
220
228
|
const ast = parseSync("inline.tsx", source).program;
|
|
221
229
|
const defineComponentAliases = new Set(["defineComponent", "_defineComponent"]);
|
|
222
230
|
const defineComponentDecls = /* @__PURE__ */ new Map();
|
|
@@ -226,29 +234,22 @@ function collectWithOxc(source, options) {
|
|
|
226
234
|
if (statement?.type === "ImportDeclaration") {
|
|
227
235
|
const importSource = statement.source?.value;
|
|
228
236
|
if (typeof importSource === "string" && options.isDefineComponentSource(importSource)) {
|
|
229
|
-
for (const specifier of statement.specifiers ?? []) if (specifier
|
|
237
|
+
for (const specifier of statement.specifiers ?? []) if (isJsxDefineComponentImportSpecifier(specifier)) {
|
|
238
|
+
const localName = getJsxImportLocalName(specifier);
|
|
239
|
+
if (localName) defineComponentAliases.add(localName);
|
|
240
|
+
}
|
|
230
241
|
}
|
|
231
242
|
if (statement.importKind === "type" || typeof importSource !== "string") continue;
|
|
232
243
|
for (const specifier of statement.specifiers ?? []) {
|
|
233
|
-
if (specifier?.importKind === "type"
|
|
234
|
-
const localName = specifier
|
|
244
|
+
if (specifier?.importKind === "type") continue;
|
|
245
|
+
const localName = getJsxImportLocalName(specifier);
|
|
246
|
+
if (!localName) continue;
|
|
235
247
|
if (specifier.type === "ImportDefaultSpecifier") {
|
|
236
|
-
imports.set(localName,
|
|
237
|
-
localName,
|
|
238
|
-
importSource,
|
|
239
|
-
importedName: "default",
|
|
240
|
-
kind: "default"
|
|
241
|
-
});
|
|
248
|
+
imports.set(localName, createJsxImportedComponent(localName, importSource, "default"));
|
|
242
249
|
continue;
|
|
243
250
|
}
|
|
244
251
|
if (specifier.type !== "ImportSpecifier") continue;
|
|
245
|
-
|
|
246
|
-
imports.set(localName, {
|
|
247
|
-
localName,
|
|
248
|
-
importSource,
|
|
249
|
-
importedName,
|
|
250
|
-
kind: "named"
|
|
251
|
-
});
|
|
252
|
+
imports.set(localName, createJsxImportedComponent(localName, importSource, "named", specifier.imported));
|
|
252
253
|
}
|
|
253
254
|
continue;
|
|
254
255
|
}
|
|
@@ -300,7 +301,7 @@ function collectJsxAutoComponentsFromCode(source, options) {
|
|
|
300
301
|
resolveBabelRenderExpression: options.resolveBabelRenderExpression ?? defaultResolveBabelRenderExpression
|
|
301
302
|
};
|
|
302
303
|
try {
|
|
303
|
-
return normalizedOptions.astEngine === "oxc" ?
|
|
304
|
+
return normalizedOptions.astEngine === "oxc" ? collectJsxAutoComponentsWithOxc(source, normalizedOptions) : collectJsxAutoComponentsWithBabel(source, normalizedOptions);
|
|
304
305
|
} catch {
|
|
305
306
|
return {
|
|
306
307
|
templateTags: /* @__PURE__ */ new Set(),
|
|
@@ -309,4 +310,4 @@ function collectJsxAutoComponentsFromCode(source, options) {
|
|
|
309
310
|
}
|
|
310
311
|
}
|
|
311
312
|
//#endregion
|
|
312
|
-
export { collectJsxAutoComponentsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression };
|
|
313
|
+
export { collectJsxAutoComponentsFromCode, collectJsxAutoComponentsWithBabel, collectJsxAutoComponentsWithOxc, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, collectJsxTemplateTagsFromOxc, createJsxImportedComponent, defaultIsDefineComponentSource, defaultResolveBabelComponentExpression, defaultResolveBabelRenderExpression, getJsxImportLocalName, getJsxImportedName, getJsxOxcStaticPropertyName, isJsxDefineComponentImportSpecifier, mayContainJsxAutoComponentEntry, resolveOxcComponentExpression, resolveOxcRenderExpression, unwrapOxcExpression };
|
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
import { AstEngineName } from "../types.mjs";
|
|
2
|
+
import * as t from "@babel/types";
|
|
2
3
|
|
|
3
4
|
//#region src/operations/onPageScroll.d.ts
|
|
5
|
+
interface PageScrollInspection {
|
|
6
|
+
empty: boolean;
|
|
7
|
+
hasSetDataCall: boolean;
|
|
8
|
+
syncApis: Set<string>;
|
|
9
|
+
}
|
|
10
|
+
interface OxcLoc {
|
|
11
|
+
line: number;
|
|
12
|
+
column: number;
|
|
13
|
+
}
|
|
14
|
+
declare function isStaticPropertyName(key: t.Expression | t.Identifier | t.PrivateName): string | undefined;
|
|
15
|
+
declare function getMemberExpressionPropertyName(node: t.MemberExpression | t.OptionalMemberExpression): string | undefined;
|
|
16
|
+
declare function isOnPageScrollCallee(callee: t.Expression | t.V8IntrinsicIdentifier, hookNames: Set<string>, namespaceImports: Set<string>): boolean;
|
|
17
|
+
declare function getCallExpressionCalleeName(callee: t.Expression | t.V8IntrinsicIdentifier): string | undefined;
|
|
18
|
+
declare function getOnPageScrollCallbackArgument(node: {
|
|
19
|
+
arguments?: Array<t.Expression | t.SpreadElement | t.ArgumentPlaceholder | null>;
|
|
20
|
+
}): t.FunctionExpression | t.ArrowFunctionExpression | undefined;
|
|
21
|
+
declare function collectPageScrollInspection(functionPath: any, node: t.ArrowFunctionExpression | t.FunctionExpression | t.ObjectMethod): PageScrollInspection;
|
|
22
|
+
declare function createWarningPrefix(filename: string, line?: number, column?: number): string;
|
|
23
|
+
declare function getOxcStaticPropertyName(node: any): string | undefined;
|
|
24
|
+
declare function getOxcMemberExpressionPropertyName(node: any): string | undefined;
|
|
25
|
+
declare function isOxcFunctionLike(node: any): boolean;
|
|
26
|
+
declare function isOxcOnPageScrollCallee(callee: any, hookNames: Set<string>, namespaceImports: Set<string>): boolean;
|
|
27
|
+
declare function getOxcCallExpressionCalleeName(callee: any): string | undefined;
|
|
28
|
+
declare function createLineStartOffsets(code: string): number[];
|
|
29
|
+
declare function getLocationFromOffset(offset: number | undefined, lineStarts: number[]): OxcLoc | undefined;
|
|
30
|
+
declare function collectPageScrollInspectionWithOxc(node: any): PageScrollInspection;
|
|
31
|
+
declare function collectOnPageScrollWarningsWithOxc(code: string, filename: string): string[];
|
|
32
|
+
declare function collectOnPageScrollWarningsWithBabel(code: string, filename: string): string[];
|
|
4
33
|
/**
|
|
5
34
|
* 静态检测 onPageScroll 中的常见性能风险并返回告警文案。
|
|
6
35
|
*/
|
|
@@ -8,4 +37,4 @@ declare function collectOnPageScrollPerformanceWarnings(code: string, filename:
|
|
|
8
37
|
engine?: AstEngineName;
|
|
9
38
|
}): string[];
|
|
10
39
|
//#endregion
|
|
11
|
-
export { collectOnPageScrollPerformanceWarnings };
|
|
40
|
+
export { collectOnPageScrollPerformanceWarnings, collectOnPageScrollWarningsWithBabel, collectOnPageScrollWarningsWithOxc, collectPageScrollInspection, collectPageScrollInspectionWithOxc, createLineStartOffsets, createWarningPrefix, getCallExpressionCalleeName, getLocationFromOffset, getMemberExpressionPropertyName, getOnPageScrollCallbackArgument, getOxcCallExpressionCalleeName, getOxcMemberExpressionPropertyName, getOxcStaticPropertyName, isOnPageScrollCallee, isOxcFunctionLike, isOxcOnPageScrollCallee, isStaticPropertyName };
|
|
@@ -22,6 +22,11 @@ function getCallExpressionCalleeName(callee) {
|
|
|
22
22
|
if (callee.type === "Identifier") return callee.name;
|
|
23
23
|
if (callee.type === "MemberExpression" || callee.type === "OptionalMemberExpression") return getMemberExpressionPropertyName(callee);
|
|
24
24
|
}
|
|
25
|
+
function getOnPageScrollCallbackArgument(node) {
|
|
26
|
+
const arg0 = node.arguments?.[0];
|
|
27
|
+
if (!arg0 || arg0.type === "SpreadElement") return;
|
|
28
|
+
return arg0.type === "FunctionExpression" || arg0.type === "ArrowFunctionExpression" ? arg0 : void 0;
|
|
29
|
+
}
|
|
25
30
|
function collectPageScrollInspection(functionPath, node) {
|
|
26
31
|
const inspection = {
|
|
27
32
|
empty: node.body.type === "BlockStatement" && node.body.body.length === 0,
|
|
@@ -137,7 +142,7 @@ function collectPageScrollInspectionWithOxc(node) {
|
|
|
137
142
|
if (root) visit(root, true);
|
|
138
143
|
return inspection;
|
|
139
144
|
}
|
|
140
|
-
function
|
|
145
|
+
function collectOnPageScrollWarningsWithOxc(code, filename) {
|
|
141
146
|
const ast = parseJsLikeWithEngine(code, {
|
|
142
147
|
engine: "oxc",
|
|
143
148
|
filename: "inline.ts"
|
|
@@ -187,22 +192,8 @@ function collectWithOxc(code, filename) {
|
|
|
187
192
|
visit(ast);
|
|
188
193
|
return warnings;
|
|
189
194
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
*/
|
|
193
|
-
function collectOnPageScrollPerformanceWarnings(code, filename, options) {
|
|
194
|
-
if (!code.includes("onPageScroll")) return [];
|
|
195
|
-
if (options?.engine === "oxc") try {
|
|
196
|
-
return collectWithOxc(code, filename);
|
|
197
|
-
} catch {
|
|
198
|
-
return [];
|
|
199
|
-
}
|
|
200
|
-
let ast;
|
|
201
|
-
try {
|
|
202
|
-
ast = parseJsLike(code);
|
|
203
|
-
} catch {
|
|
204
|
-
return [];
|
|
205
|
-
}
|
|
195
|
+
function collectOnPageScrollWarningsWithBabel(code, filename) {
|
|
196
|
+
const ast = parseJsLike(code);
|
|
206
197
|
const onPageScrollHookNames = new Set(["onPageScroll"]);
|
|
207
198
|
const namespaceImports = /* @__PURE__ */ new Set();
|
|
208
199
|
for (const statement of ast.program.body) {
|
|
@@ -241,20 +232,34 @@ function collectOnPageScrollPerformanceWarnings(code, filename, options) {
|
|
|
241
232
|
},
|
|
242
233
|
CallExpression(path) {
|
|
243
234
|
if (!isOnPageScrollCallee(path.node.callee, onPageScrollHookNames, namespaceImports)) return;
|
|
244
|
-
const arg0 = path.node
|
|
245
|
-
if (!arg0
|
|
246
|
-
if (arg0.type !== "FunctionExpression" && arg0.type !== "ArrowFunctionExpression") return;
|
|
235
|
+
const arg0 = getOnPageScrollCallbackArgument(path.node);
|
|
236
|
+
if (!arg0) return;
|
|
247
237
|
reportInspection(path.get("arguments.0"), arg0, "onPageScroll(...)");
|
|
248
238
|
},
|
|
249
239
|
OptionalCallExpression(path) {
|
|
250
240
|
if (!isOnPageScrollCallee(path.node.callee, onPageScrollHookNames, namespaceImports)) return;
|
|
251
|
-
const arg0 = path.node
|
|
252
|
-
if (!arg0
|
|
253
|
-
if (arg0.type !== "FunctionExpression" && arg0.type !== "ArrowFunctionExpression") return;
|
|
241
|
+
const arg0 = getOnPageScrollCallbackArgument(path.node);
|
|
242
|
+
if (!arg0) return;
|
|
254
243
|
reportInspection(path.get("arguments.0"), arg0, "onPageScroll(...)");
|
|
255
244
|
}
|
|
256
245
|
});
|
|
257
246
|
return warnings;
|
|
258
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* 静态检测 onPageScroll 中的常见性能风险并返回告警文案。
|
|
250
|
+
*/
|
|
251
|
+
function collectOnPageScrollPerformanceWarnings(code, filename, options) {
|
|
252
|
+
if (!code.includes("onPageScroll")) return [];
|
|
253
|
+
if (options?.engine === "oxc") try {
|
|
254
|
+
return collectOnPageScrollWarningsWithOxc(code, filename);
|
|
255
|
+
} catch {
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
return collectOnPageScrollWarningsWithBabel(code, filename);
|
|
260
|
+
} catch {
|
|
261
|
+
return [];
|
|
262
|
+
}
|
|
263
|
+
}
|
|
259
264
|
//#endregion
|
|
260
|
-
export { collectOnPageScrollPerformanceWarnings };
|
|
265
|
+
export { collectOnPageScrollPerformanceWarnings, collectOnPageScrollWarningsWithBabel, collectOnPageScrollWarningsWithOxc, collectPageScrollInspection, collectPageScrollInspectionWithOxc, createLineStartOffsets, createWarningPrefix, getCallExpressionCalleeName, getLocationFromOffset, getMemberExpressionPropertyName, getOnPageScrollCallbackArgument, getOxcCallExpressionCalleeName, getOxcMemberExpressionPropertyName, getOxcStaticPropertyName, isOnPageScrollCallee, isOxcFunctionLike, isOxcOnPageScrollCallee, isStaticPropertyName };
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { AstEngineName, AstParserLike } from "../types.mjs";
|
|
2
|
+
import { Program } from "@oxc-project/types";
|
|
2
3
|
|
|
3
4
|
//#region src/operations/platformApi.d.ts
|
|
4
|
-
declare const
|
|
5
|
+
declare const platformApiIdentifierList: readonly ["wx", "my", "tt", "swan", "jd", "xhs"];
|
|
6
|
+
declare const platformApiIdentifiers: Set<"wx" | "my" | "tt" | "swan" | "jd" | "xhs">;
|
|
7
|
+
declare function isPlatformApiIdentifier(name: string): boolean;
|
|
8
|
+
declare function mayContainPlatformApiIdentifierByText(code: string): boolean;
|
|
9
|
+
declare function isPlatformApiMemberExpression(node: any): boolean;
|
|
10
|
+
declare function hasPlatformApiMemberExpression(ast: Program): boolean;
|
|
5
11
|
/**
|
|
6
12
|
* 使用统一 AST 入口做平台 API 访问预判。
|
|
7
13
|
*/
|
|
@@ -10,4 +16,4 @@ declare function mayContainPlatformApiAccess(code: string, options?: {
|
|
|
10
16
|
parserLike?: AstParserLike;
|
|
11
17
|
}): boolean;
|
|
12
18
|
//#endregion
|
|
13
|
-
export { mayContainPlatformApiAccess, platformApiIdentifiers };
|
|
19
|
+
export { hasPlatformApiMemberExpression, isPlatformApiIdentifier, isPlatformApiMemberExpression, mayContainPlatformApiAccess, mayContainPlatformApiIdentifierByText, platformApiIdentifierList, platformApiIdentifiers };
|
|
@@ -1,18 +1,33 @@
|
|
|
1
1
|
import { t as parseJsLikeWithEngine } from "../engine-DHqNPCDA.mjs";
|
|
2
2
|
import { walk } from "oxc-walker";
|
|
3
3
|
//#region src/operations/platformApi.ts
|
|
4
|
-
const
|
|
4
|
+
const platformApiIdentifierList = [
|
|
5
5
|
"wx",
|
|
6
6
|
"my",
|
|
7
7
|
"tt",
|
|
8
8
|
"swan",
|
|
9
9
|
"jd",
|
|
10
10
|
"xhs"
|
|
11
|
-
]
|
|
11
|
+
];
|
|
12
|
+
const platformApiIdentifiers = new Set(platformApiIdentifierList);
|
|
13
|
+
function isPlatformApiIdentifier(name) {
|
|
14
|
+
return platformApiIdentifiers.has(name);
|
|
15
|
+
}
|
|
12
16
|
function mayContainPlatformApiIdentifierByText(code) {
|
|
13
17
|
for (const identifier of platformApiIdentifiers) if (code.includes(`${identifier}.`)) return true;
|
|
14
18
|
return false;
|
|
15
19
|
}
|
|
20
|
+
function isPlatformApiMemberExpression(node) {
|
|
21
|
+
return node?.type === "MemberExpression" && node.object?.type === "Identifier" && isPlatformApiIdentifier(node.object.name);
|
|
22
|
+
}
|
|
23
|
+
function hasPlatformApiMemberExpression(ast) {
|
|
24
|
+
let found = false;
|
|
25
|
+
walk(ast, { enter(node) {
|
|
26
|
+
if (found) return;
|
|
27
|
+
if (isPlatformApiMemberExpression(node)) found = true;
|
|
28
|
+
} });
|
|
29
|
+
return found;
|
|
30
|
+
}
|
|
16
31
|
/**
|
|
17
32
|
* 使用统一 AST 入口做平台 API 访问预判。
|
|
18
33
|
*/
|
|
@@ -21,20 +36,14 @@ function mayContainPlatformApiAccess(code, options) {
|
|
|
21
36
|
if (engine !== "oxc") return true;
|
|
22
37
|
if (!mayContainPlatformApiIdentifierByText(code)) return false;
|
|
23
38
|
try {
|
|
24
|
-
|
|
39
|
+
return hasPlatformApiMemberExpression(parseJsLikeWithEngine(code, {
|
|
25
40
|
engine,
|
|
26
41
|
filename: "inline.ts",
|
|
27
42
|
parserLike: options?.parserLike
|
|
28
|
-
});
|
|
29
|
-
let found = false;
|
|
30
|
-
walk(ast, { enter(node) {
|
|
31
|
-
if (found) return;
|
|
32
|
-
if (node.type === "MemberExpression" && node.object.type === "Identifier" && platformApiIdentifiers.has(node.object.name)) found = true;
|
|
33
|
-
} });
|
|
34
|
-
return found;
|
|
43
|
+
}));
|
|
35
44
|
} catch {
|
|
36
45
|
return true;
|
|
37
46
|
}
|
|
38
47
|
}
|
|
39
48
|
//#endregion
|
|
40
|
-
export { mayContainPlatformApiAccess, platformApiIdentifiers };
|
|
49
|
+
export { hasPlatformApiMemberExpression, isPlatformApiIdentifier, isPlatformApiMemberExpression, mayContainPlatformApiAccess, mayContainPlatformApiIdentifierByText, platformApiIdentifierList, platformApiIdentifiers };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AstEngineName, AstParserLike } from "../types.mjs";
|
|
2
|
+
import { Program } from "@oxc-project/types";
|
|
2
3
|
|
|
3
4
|
//#region src/operations/require.d.ts
|
|
4
5
|
interface RequireToken {
|
|
@@ -7,12 +8,17 @@ interface RequireToken {
|
|
|
7
8
|
value: string;
|
|
8
9
|
async?: boolean;
|
|
9
10
|
}
|
|
11
|
+
declare function mayContainRequireCallByText(code: string): boolean;
|
|
12
|
+
declare function getStaticRequireLiteralValue(node: any): any;
|
|
10
13
|
/**
|
|
11
14
|
* 收集 `require.async()` 依赖字面量。
|
|
12
15
|
*/
|
|
16
|
+
declare function getRequireAsyncLiteralToken(node: any): RequireToken | null;
|
|
13
17
|
declare function collectRequireTokens(ast: unknown): {
|
|
14
18
|
requireTokens: RequireToken[];
|
|
15
19
|
};
|
|
20
|
+
declare function isStaticRequireCall(node: any): boolean;
|
|
21
|
+
declare function hasStaticRequireCall(ast: Program): boolean;
|
|
16
22
|
/**
|
|
17
23
|
* 使用统一 AST 入口预判是否存在可静态分析的 `require("...")` / ``require(`...`)``。
|
|
18
24
|
*/
|
|
@@ -21,4 +27,4 @@ declare function mayContainStaticRequireLiteral(code: string, options?: {
|
|
|
21
27
|
parserLike?: AstParserLike;
|
|
22
28
|
}): boolean;
|
|
23
29
|
//#endregion
|
|
24
|
-
export { RequireToken, collectRequireTokens, mayContainStaticRequireLiteral };
|
|
30
|
+
export { RequireToken, collectRequireTokens, getRequireAsyncLiteralToken, getStaticRequireLiteralValue, hasStaticRequireCall, isStaticRequireCall, mayContainRequireCallByText, mayContainStaticRequireLiteral };
|
|
@@ -13,23 +13,38 @@ function getStaticRequireLiteralValue(node) {
|
|
|
13
13
|
/**
|
|
14
14
|
* 收集 `require.async()` 依赖字面量。
|
|
15
15
|
*/
|
|
16
|
+
function getRequireAsyncLiteralToken(node) {
|
|
17
|
+
if (node?.type !== "CallExpression" || node.callee?.type !== "MemberExpression" || node.callee.object?.type !== "Identifier" || node.callee.object.name !== "require" || node.callee.property?.type !== "Identifier" || node.callee.property.name !== "async") return null;
|
|
18
|
+
const argv0 = node.arguments?.[0];
|
|
19
|
+
if (!argv0 || argv0.type !== "Literal" || typeof argv0.value !== "string") return null;
|
|
20
|
+
return {
|
|
21
|
+
start: argv0.start,
|
|
22
|
+
end: argv0.end,
|
|
23
|
+
value: argv0.value,
|
|
24
|
+
async: true
|
|
25
|
+
};
|
|
26
|
+
}
|
|
16
27
|
function collectRequireTokens(ast) {
|
|
17
28
|
const requireTokens = [];
|
|
18
29
|
walk(ast, { enter(node) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const argv0 = node.arguments[0];
|
|
22
|
-
if (argv0 && argv0.type === "Literal" && typeof argv0.value === "string") requireTokens.push({
|
|
23
|
-
start: argv0.start,
|
|
24
|
-
end: argv0.end,
|
|
25
|
-
value: argv0.value,
|
|
26
|
-
async: true
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
+
const token = getRequireAsyncLiteralToken(node);
|
|
31
|
+
if (token) requireTokens.push(token);
|
|
30
32
|
} });
|
|
31
33
|
return { requireTokens };
|
|
32
34
|
}
|
|
35
|
+
function isStaticRequireCall(node) {
|
|
36
|
+
if (node?.type !== "CallExpression") return false;
|
|
37
|
+
if (node.callee?.type !== "Identifier" || node.callee.name !== "require") return false;
|
|
38
|
+
return typeof getStaticRequireLiteralValue(node.arguments?.[0]) === "string";
|
|
39
|
+
}
|
|
40
|
+
function hasStaticRequireCall(ast) {
|
|
41
|
+
let found = false;
|
|
42
|
+
walk(ast, { enter(node) {
|
|
43
|
+
if (found) return;
|
|
44
|
+
if (isStaticRequireCall(node)) found = true;
|
|
45
|
+
} });
|
|
46
|
+
return found;
|
|
47
|
+
}
|
|
33
48
|
/**
|
|
34
49
|
* 使用统一 AST 入口预判是否存在可静态分析的 `require("...")` / ``require(`...`)``。
|
|
35
50
|
*/
|
|
@@ -38,21 +53,14 @@ function mayContainStaticRequireLiteral(code, options) {
|
|
|
38
53
|
if (engine !== "oxc") return true;
|
|
39
54
|
if (!mayContainRequireCallByText(code)) return false;
|
|
40
55
|
try {
|
|
41
|
-
|
|
56
|
+
return hasStaticRequireCall(parseJsLikeWithEngine(code, {
|
|
42
57
|
engine,
|
|
43
58
|
filename: "inline.ts",
|
|
44
59
|
parserLike: options?.parserLike
|
|
45
|
-
});
|
|
46
|
-
let found = false;
|
|
47
|
-
walk(ast, { enter(node) {
|
|
48
|
-
if (found || node.type !== "CallExpression") return;
|
|
49
|
-
if (node.callee.type !== "Identifier" || node.callee.name !== "require") return;
|
|
50
|
-
if (typeof getStaticRequireLiteralValue(node.arguments?.[0]) === "string") found = true;
|
|
51
|
-
} });
|
|
52
|
-
return found;
|
|
60
|
+
}));
|
|
53
61
|
} catch {
|
|
54
62
|
return true;
|
|
55
63
|
}
|
|
56
64
|
}
|
|
57
65
|
//#endregion
|
|
58
|
-
export { collectRequireTokens, mayContainStaticRequireLiteral };
|
|
66
|
+
export { collectRequireTokens, getRequireAsyncLiteralToken, getStaticRequireLiteralValue, hasStaticRequireCall, isStaticRequireCall, mayContainRequireCallByText, mayContainStaticRequireLiteral };
|
|
@@ -7,6 +7,11 @@ interface ScriptSetupImport {
|
|
|
7
7
|
importedName?: string;
|
|
8
8
|
kind: 'default' | 'named';
|
|
9
9
|
}
|
|
10
|
+
declare function mayContainRelevantScriptSetupImports(scriptSetup: string, templateComponentNames: Set<string>): boolean;
|
|
11
|
+
declare function getScriptSetupImportedName(imported: any): any;
|
|
12
|
+
declare function createScriptSetupImport(localName: string, importSource: string, kind: ScriptSetupImport['kind'], imported?: any): ScriptSetupImport;
|
|
13
|
+
declare function collectScriptSetupImportsWithOxc(scriptSetup: string, templateComponentNames: Set<string>): ScriptSetupImport[];
|
|
14
|
+
declare function collectScriptSetupImportsWithBabel(scriptSetup: string, templateComponentNames: Set<string>): ScriptSetupImport[];
|
|
10
15
|
/**
|
|
11
16
|
* 收集 `<script setup>` 中会参与自动 usingComponents 的导入声明。
|
|
12
17
|
*/
|
|
@@ -14,4 +19,4 @@ declare function collectScriptSetupImportsFromCode(scriptSetup: string, template
|
|
|
14
19
|
astEngine?: AstEngineName;
|
|
15
20
|
}): ScriptSetupImport[];
|
|
16
21
|
//#endregion
|
|
17
|
-
export { ScriptSetupImport, collectScriptSetupImportsFromCode };
|
|
22
|
+
export { ScriptSetupImport, collectScriptSetupImportsFromCode, collectScriptSetupImportsWithBabel, collectScriptSetupImportsWithOxc, createScriptSetupImport, getScriptSetupImportedName, mayContainRelevantScriptSetupImports };
|
|
@@ -7,7 +7,18 @@ function mayContainRelevantScriptSetupImports(scriptSetup, templateComponentName
|
|
|
7
7
|
for (const componentName of templateComponentNames) if (scriptSetup.includes(componentName)) return true;
|
|
8
8
|
return false;
|
|
9
9
|
}
|
|
10
|
-
function
|
|
10
|
+
function getScriptSetupImportedName(imported) {
|
|
11
|
+
return imported?.type === "Identifier" ? imported.name : imported?.type === "StringLiteral" ? imported.value : void 0;
|
|
12
|
+
}
|
|
13
|
+
function createScriptSetupImport(localName, importSource, kind, imported) {
|
|
14
|
+
return {
|
|
15
|
+
localName,
|
|
16
|
+
importSource,
|
|
17
|
+
importedName: kind === "default" ? "default" : getScriptSetupImportedName(imported),
|
|
18
|
+
kind
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function collectScriptSetupImportsWithOxc(scriptSetup, templateComponentNames) {
|
|
11
22
|
const results = [];
|
|
12
23
|
const ast = parseJsLikeWithEngine(scriptSetup, {
|
|
13
24
|
engine: "oxc",
|
|
@@ -21,26 +32,13 @@ function collectWithOxc(scriptSetup, templateComponentNames) {
|
|
|
21
32
|
if (specifier?.importKind === "type" || specifier?.local?.type !== "Identifier") continue;
|
|
22
33
|
const localName = specifier.local.name;
|
|
23
34
|
if (!templateComponentNames.has(localName)) continue;
|
|
24
|
-
if (specifier.type === "ImportDefaultSpecifier") results.push(
|
|
25
|
-
|
|
26
|
-
importSource,
|
|
27
|
-
importedName: "default",
|
|
28
|
-
kind: "default"
|
|
29
|
-
});
|
|
30
|
-
else if (specifier.type === "ImportSpecifier") {
|
|
31
|
-
const importedName = specifier.imported?.type === "Identifier" ? specifier.imported.name : specifier.imported?.type === "StringLiteral" ? specifier.imported.value : void 0;
|
|
32
|
-
results.push({
|
|
33
|
-
localName,
|
|
34
|
-
importSource,
|
|
35
|
-
importedName,
|
|
36
|
-
kind: "named"
|
|
37
|
-
});
|
|
38
|
-
}
|
|
35
|
+
if (specifier.type === "ImportDefaultSpecifier") results.push(createScriptSetupImport(localName, importSource, "default"));
|
|
36
|
+
else if (specifier.type === "ImportSpecifier") results.push(createScriptSetupImport(localName, importSource, "named", specifier.imported));
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
39
|
return results;
|
|
42
40
|
}
|
|
43
|
-
function
|
|
41
|
+
function collectScriptSetupImportsWithBabel(scriptSetup, templateComponentNames) {
|
|
44
42
|
const results = [];
|
|
45
43
|
const ast = parse(scriptSetup, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
46
44
|
for (const node of ast.program.body) {
|
|
@@ -51,22 +49,8 @@ function collectWithBabel(scriptSetup, templateComponentNames) {
|
|
|
51
49
|
if (specifier.importKind === "type") continue;
|
|
52
50
|
const localName = specifier.local?.name;
|
|
53
51
|
if (!localName || !templateComponentNames.has(localName)) continue;
|
|
54
|
-
if (specifier.type === "ImportDefaultSpecifier") results.push(
|
|
55
|
-
|
|
56
|
-
importSource,
|
|
57
|
-
importedName: "default",
|
|
58
|
-
kind: "default"
|
|
59
|
-
});
|
|
60
|
-
else if (specifier.type === "ImportSpecifier") {
|
|
61
|
-
const imported = specifier.imported;
|
|
62
|
-
const importedName = imported?.type === "Identifier" ? imported.name : imported?.type === "StringLiteral" ? imported.value : void 0;
|
|
63
|
-
results.push({
|
|
64
|
-
localName,
|
|
65
|
-
importSource,
|
|
66
|
-
importedName,
|
|
67
|
-
kind: "named"
|
|
68
|
-
});
|
|
69
|
-
}
|
|
52
|
+
if (specifier.type === "ImportDefaultSpecifier") results.push(createScriptSetupImport(localName, importSource, "default"));
|
|
53
|
+
else if (specifier.type === "ImportSpecifier") results.push(createScriptSetupImport(localName, importSource, "named", specifier.imported));
|
|
70
54
|
}
|
|
71
55
|
}
|
|
72
56
|
return results;
|
|
@@ -78,10 +62,10 @@ function collectScriptSetupImportsFromCode(scriptSetup, templateComponentNames,
|
|
|
78
62
|
if (!mayContainRelevantScriptSetupImports(scriptSetup, templateComponentNames)) return [];
|
|
79
63
|
const engine = options?.astEngine ?? "babel";
|
|
80
64
|
try {
|
|
81
|
-
return engine === "oxc" ?
|
|
65
|
+
return engine === "oxc" ? collectScriptSetupImportsWithOxc(scriptSetup, templateComponentNames) : collectScriptSetupImportsWithBabel(scriptSetup, templateComponentNames);
|
|
82
66
|
} catch {
|
|
83
67
|
return [];
|
|
84
68
|
}
|
|
85
69
|
}
|
|
86
70
|
//#endregion
|
|
87
|
-
export { collectScriptSetupImportsFromCode };
|
|
71
|
+
export { collectScriptSetupImportsFromCode, collectScriptSetupImportsWithBabel, collectScriptSetupImportsWithOxc, createScriptSetupImport, getScriptSetupImportedName, mayContainRelevantScriptSetupImports };
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { AstEngineName } from "../types.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/operations/setDataPick.d.ts
|
|
4
|
+
declare function collectLoopScopeAliases(template: string): Set<string>;
|
|
5
|
+
declare function extractTemplateExpressions(template: string): string[];
|
|
6
|
+
declare function collectIdentifiersFromExpression(expression: string): Set<string>;
|
|
7
|
+
declare function collectPatternBindingNames(node: any, bindings: Set<string>): void;
|
|
8
|
+
declare function hasBindingInScopes(scopes: Set<string>[], name: string): boolean;
|
|
9
|
+
declare function collectIdentifiersFromExpressionWithOxc(expression: string): Set<string>;
|
|
10
|
+
declare function collectSetDataPickKeysWithBabel(template: string): string[];
|
|
11
|
+
declare function collectSetDataPickKeysWithOxc(template: string): string[];
|
|
4
12
|
/**
|
|
5
13
|
* 从编译后的 WXML 模板提取渲染相关的顶层 key。
|
|
6
14
|
*
|
|
@@ -10,4 +18,4 @@ declare function collectSetDataPickKeysFromTemplateCode(template: string, option
|
|
|
10
18
|
astEngine?: AstEngineName;
|
|
11
19
|
}): string[];
|
|
12
20
|
//#endregion
|
|
13
|
-
export { collectSetDataPickKeysFromTemplateCode };
|
|
21
|
+
export { collectIdentifiersFromExpression, collectIdentifiersFromExpressionWithOxc, collectLoopScopeAliases, collectPatternBindingNames, collectSetDataPickKeysFromTemplateCode, collectSetDataPickKeysWithBabel, collectSetDataPickKeysWithOxc, extractTemplateExpressions, hasBindingInScopes };
|
|
@@ -114,6 +114,9 @@ function collectPatternBindingNames(node, bindings) {
|
|
|
114
114
|
if (node.type === "ObjectPattern") for (const property of node.properties ?? []) if (property?.type === "RestElement") collectPatternBindingNames(property.argument, bindings);
|
|
115
115
|
else collectPatternBindingNames(property?.value, bindings);
|
|
116
116
|
}
|
|
117
|
+
function hasBindingInScopes(scopes, name) {
|
|
118
|
+
return scopes.some((scope) => scope.has(name));
|
|
119
|
+
}
|
|
117
120
|
function collectIdentifiersFromExpressionWithOxc(expression) {
|
|
118
121
|
const collected = /* @__PURE__ */ new Set();
|
|
119
122
|
let ast;
|
|
@@ -126,9 +129,6 @@ function collectIdentifiersFromExpressionWithOxc(expression) {
|
|
|
126
129
|
return collected;
|
|
127
130
|
}
|
|
128
131
|
const scopes = [/* @__PURE__ */ new Set()];
|
|
129
|
-
function hasBinding(name) {
|
|
130
|
-
return scopes.some((scope) => scope.has(name));
|
|
131
|
-
}
|
|
132
132
|
function visit(node) {
|
|
133
133
|
if (!node) return;
|
|
134
134
|
if (node.type === "ReturnStatement") {
|
|
@@ -166,7 +166,7 @@ function collectIdentifiersFromExpressionWithOxc(expression) {
|
|
|
166
166
|
return;
|
|
167
167
|
}
|
|
168
168
|
case "Identifier":
|
|
169
|
-
if (node.name !== "__weappViteExpr" && !
|
|
169
|
+
if (node.name !== "__weappViteExpr" && !hasBindingInScopes(scopes, node.name) && !JS_GLOBAL_IDENTIFIERS.has(node.name)) collected.add(node.name);
|
|
170
170
|
return;
|
|
171
171
|
case "ChainExpression":
|
|
172
172
|
visit(node.expression);
|
|
@@ -248,7 +248,7 @@ function collectIdentifiersFromExpressionWithOxc(expression) {
|
|
|
248
248
|
visit(ast);
|
|
249
249
|
return collected;
|
|
250
250
|
}
|
|
251
|
-
function
|
|
251
|
+
function collectSetDataPickKeysWithBabel(template) {
|
|
252
252
|
const templateExpressions = extractTemplateExpressions(template);
|
|
253
253
|
if (!templateExpressions.length) return [];
|
|
254
254
|
const loopAliases = collectLoopScopeAliases(template);
|
|
@@ -259,7 +259,7 @@ function collectWithBabel(template) {
|
|
|
259
259
|
}
|
|
260
260
|
return [...keys].sort((a, b) => a.localeCompare(b));
|
|
261
261
|
}
|
|
262
|
-
function
|
|
262
|
+
function collectSetDataPickKeysWithOxc(template) {
|
|
263
263
|
const templateExpressions = extractTemplateExpressions(template);
|
|
264
264
|
if (!templateExpressions.length) return [];
|
|
265
265
|
const loopAliases = collectLoopScopeAliases(template);
|
|
@@ -279,10 +279,10 @@ function collectSetDataPickKeysFromTemplateCode(template, options) {
|
|
|
279
279
|
if (!template.includes("{{")) return [];
|
|
280
280
|
const engine = options?.astEngine ?? "babel";
|
|
281
281
|
try {
|
|
282
|
-
return engine === "oxc" ?
|
|
282
|
+
return engine === "oxc" ? collectSetDataPickKeysWithOxc(template) : collectSetDataPickKeysWithBabel(template);
|
|
283
283
|
} catch {
|
|
284
284
|
return [];
|
|
285
285
|
}
|
|
286
286
|
}
|
|
287
287
|
//#endregion
|
|
288
|
-
export { collectSetDataPickKeysFromTemplateCode };
|
|
288
|
+
export { collectIdentifiersFromExpression, collectIdentifiersFromExpressionWithOxc, collectLoopScopeAliases, collectPatternBindingNames, collectSetDataPickKeysFromTemplateCode, collectSetDataPickKeysWithBabel, collectSetDataPickKeysWithOxc, extractTemplateExpressions, hasBindingInScopes };
|