@kezh/my-custom-js-converter 0.1.62 → 0.2.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.
@@ -79,11 +79,13 @@ class MapContext {
79
79
  const srcMethodNames = (0, ast_1.getObjectMethodNames)((0, ast_1.getObjectMethodsByEntryAndMethodNames)(srcAst, entry));
80
80
  if (!srcMethodNames.length) {
81
81
  console.log(`not found entry => ${entry}`);
82
+ this.appendModMap(new map_config_1.Mod(inPath, new map_config_1.Dependencies()), root, entry);
82
83
  return;
83
84
  }
84
85
  const requireModPathByName = (0, ast_1.getRequireModPaths)(path_1.default.dirname(inPath), srcAst);
85
86
  const requireModNames = [...requireModPathByName.keys()].map((name) => name);
86
- const requireMethodNamesByMod = (0, ast_1.getRequireMethodNames)(srcAst, requireModNames);
87
+ !root && requireModNames.unshift(entry);
88
+ const requireMethodNamesByMod = (0, ast_1.getRequireMethodNames)(srcAst, entry, requireModNames);
87
89
  const dependencies = new map_config_1.Dependencies();
88
90
  requireModNames.forEach((name) => {
89
91
  const dep = new map_config_1.Dependency(requireMethodNamesByMod.get(name));
@@ -99,37 +101,50 @@ class MapContext {
99
101
  * 过滤self的dependencies中无效方法
100
102
  */
101
103
  filtering() {
102
- const self = "self";
103
- const selfMod = this.getMod(self);
104
- if (selfMod) {
105
- const dependencies = selfMod.dependencies;
104
+ this.getModNames().forEach((modName) => {
105
+ const mod = this.getMod(modName);
106
+ const dependencies = mod.dependencies;
106
107
  Object.keys(dependencies).forEach((depName) => {
107
108
  const srcAst = (0, ast_1.parseSrcAst)(this.getSrcPathByMod(depName));
108
109
  const methodNames = (0, ast_1.getObjectMethodNames)((0, ast_1.getObjectMethodsByEntryAndMethodNames)(srcAst, depName, dependencies[depName].methods));
109
110
  dependencies[depName].methods = methodNames;
110
111
  });
111
- }
112
+ });
112
113
  }
113
114
  /**
114
115
  * 去除dependencies中未使用方法
115
116
  */
116
117
  shaking() {
117
- this.getModNames().forEach((modName) => {
118
- const depNames = this.getDependencyNameByMod(modName);
119
- if (!depNames.length) {
120
- return;
121
- }
122
- const methodNames = this.getMethodNamesByMod(modName);
123
- if (!methodNames.length) {
124
- return;
118
+ const astMap = new Map();
119
+ this.getModNames().forEach((mod) => {
120
+ astMap.set(mod, (0, ast_1.parseSrcAst)(this.getMod(mod).src));
121
+ });
122
+ const useMethodNameMap = new Map();
123
+ this.getModNames().forEach((mod) => {
124
+ useMethodNameMap.set(mod, this.getMethodNamesByMod(mod));
125
+ });
126
+ const dependencies = this.getMod("self").dependencies;
127
+ const filterMethodMap = new Map();
128
+ const readyStack = Object.keys(dependencies).flatMap((name) => dependencies[name].methods.map((method) => [name, method]));
129
+ while (readyStack.length) {
130
+ const method = readyStack.shift();
131
+ let filterMethods = new Set();
132
+ if (filterMethodMap.has(method[0])) {
133
+ filterMethods = filterMethodMap.get(method[0]);
125
134
  }
126
- const srcAst = (0, ast_1.parseSrcAst)(this.getSrcPathByMod(modName));
127
- const methodNamesByDepName = (0, ast_1.getDependentMethodNames)(srcAst, methodNames, depNames);
128
- const mod = this.getMod(modName);
129
- depNames.forEach((depName) => {
130
- mod.dependencies[depName] = new map_config_1.Dependency(methodNamesByDepName.has(depName)
131
- ? methodNamesByDepName.get(depName)
132
- : []);
135
+ filterMethods.add(method[1]);
136
+ filterMethodMap.set(method[0], filterMethods);
137
+ const inlineMethods = (0, ast_1.getInlineMethodsByMethodName)(astMap.get(method[0]), method[0], method[1], useMethodNameMap);
138
+ readyStack.push(...inlineMethods.map((m) => m.split("#")));
139
+ }
140
+ [...filterMethodMap.keys()].forEach((fMod) => {
141
+ const filterMethods = filterMethodMap.get(fMod);
142
+ this.getModNames().forEach((modName) => {
143
+ var _a;
144
+ const dep = (_a = this.getMod(modName)) === null || _a === void 0 ? void 0 : _a.dependencies[fMod];
145
+ if (dep) {
146
+ dep.methods = dep.methods.filter((m) => filterMethods.has(m));
147
+ }
133
148
  });
134
149
  });
135
150
  }
@@ -2,7 +2,7 @@ import { Node } from "@babel/core";
2
2
  import { ObjectMethod } from "@babel/types";
3
3
  import { Mod } from "../config/map_config";
4
4
  import { MapContext } from "../context/map_context";
5
- declare type AstType = Node | Node[] | null | undefined;
5
+ export declare type AstType = Node | Node[] | null | undefined;
6
6
  export declare function parseSrcAst(srcPath?: string): AstType;
7
7
  export declare function outputObjectMethods(outPath: string, methods: ObjectMethod[]): void;
8
8
  /**
@@ -15,12 +15,13 @@ export declare function outputObjectMethods(outPath: string, methods: ObjectMeth
15
15
  */
16
16
  export declare function getRequireModPaths(baseDir: string, srcAst: AstType, ignoreMod?: string[]): Map<string, string>;
17
17
  /**
18
- * 获取源码中引用的第三方模块方法名
18
+ * 获取源码方法中引用的第三方模块方法名和自身模块方法名
19
19
  * @param mod - 依赖模块
20
+ * @param entry - 入口对象
20
21
  * @param srcAst - 源码AST
21
22
  * @returns
22
23
  */
23
- export declare function getRequireMethodNames(srcAst: AstType, mod: string[]): Map<string, string[]>;
24
+ export declare function getRequireMethodNames(srcAst: AstType, entry: string, mod: string[]): Map<string, string[]>;
24
25
  /**
25
26
  * 获取方法名
26
27
  * @param methods
@@ -51,6 +52,13 @@ export declare function getObjectMehtodsByMehtodNamesAndInsideOwnMethods(srcAst:
51
52
  * @param root
52
53
  */
53
54
  export declare function modifyObjectMethods(srcAst: AstType, entry: string, mod: Mod, methods: ObjectMethod[], root?: boolean): void;
55
+ /**
56
+ * 获取调用方法中所依赖第三方模块方法名
57
+ * @param srcAst
58
+ * @param methodNames - 调用方法名
59
+ * @param depNames - 依赖模块名
60
+ * @returns
61
+ */
54
62
  export declare function getDependentMethodNames(srcAst: AstType, methodNames: string[], depNames: string[]): Map<string, string[]>;
55
63
  /**
56
64
  * 生成导入语句
@@ -65,4 +73,11 @@ export declare function importMods(outDir: string, srcAst: AstType, mapContext:
65
73
  * @param srcAst - 源码AST
66
74
  */
67
75
  export declare function deleteModMethods(srcAst: AstType, mapContext: MapContext): void;
68
- export {};
76
+ /**
77
+ * 获取方法内部调用的方法名
78
+ * @param srcAst - 源码AST
79
+ * @param entry - 入口
80
+ * @param methodName - 方法名
81
+ * @param mapContext
82
+ */
83
+ export declare function getInlineMethodsByMethodName(srcAst: AstType, entry: string, methodName: string, useMethodNameMap: Map<string, string[]>): string[];
package/core/util/ast.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.deleteModMethods = exports.importMods = exports.getDependentMethodNames = exports.modifyObjectMethods = exports.getObjectMehtodsByMehtodNamesAndInsideOwnMethods = exports.getObjectMethodsByEntryAndMethodNames = exports.getObjectMethodNames = exports.getRequireMethodNames = exports.getRequireModPaths = exports.outputObjectMethods = exports.parseSrcAst = void 0;
6
+ exports.getInlineMethodsByMethodName = exports.deleteModMethods = exports.importMods = exports.getDependentMethodNames = exports.modifyObjectMethods = exports.getObjectMehtodsByMehtodNamesAndInsideOwnMethods = exports.getObjectMethodsByEntryAndMethodNames = exports.getObjectMethodNames = exports.getRequireMethodNames = exports.getRequireModPaths = exports.outputObjectMethods = exports.parseSrcAst = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const parser_1 = require("@babel/parser");
9
9
  const traverse_1 = __importDefault(require("@babel/traverse"));
@@ -81,25 +81,32 @@ function getRequireModPaths(baseDir, srcAst, ignoreMod) {
81
81
  }
82
82
  exports.getRequireModPaths = getRequireModPaths;
83
83
  /**
84
- * 获取源码中引用的第三方模块方法名
84
+ * 获取源码方法中引用的第三方模块方法名和自身模块方法名
85
85
  * @param mod - 依赖模块
86
+ * @param entry - 入口对象
86
87
  * @param srcAst - 源码AST
87
88
  * @returns
88
89
  */
89
- function getRequireMethodNames(srcAst, mod) {
90
+ function getRequireMethodNames(srcAst, entry, mod) {
90
91
  const methodsByMod = new Map();
91
92
  (0, traverse_1.default)(srcAst, {
92
93
  MemberExpression(path) {
93
- const invokeObj = path.node.object;
94
- if (!core_1.types.isIdentifier(invokeObj)) {
95
- return;
96
- }
97
94
  const invokeMethod = path.node.property;
98
95
  if (!core_1.types.isIdentifier(invokeMethod)) {
99
96
  return;
100
97
  }
101
- const key = invokeObj.name;
102
- if (!mod.includes(key)) {
98
+ const invokeObj = path.node.object;
99
+ let key = null;
100
+ if (core_1.types.isIdentifier(invokeObj)) {
101
+ key = invokeObj.name;
102
+ if (!mod.includes(key)) {
103
+ return;
104
+ }
105
+ }
106
+ else if (core_1.types.isThisExpression(invokeObj)) {
107
+ key = entry;
108
+ }
109
+ if (!key) {
103
110
  return;
104
111
  }
105
112
  let methods = [];
@@ -276,6 +283,13 @@ function modifyObjectMethods(srcAst, entry, mod, methods, root = false) {
276
283
  });
277
284
  }
278
285
  exports.modifyObjectMethods = modifyObjectMethods;
286
+ /**
287
+ * 获取调用方法中所依赖第三方模块方法名
288
+ * @param srcAst
289
+ * @param methodNames - 调用方法名
290
+ * @param depNames - 依赖模块名
291
+ * @returns
292
+ */
279
293
  function getDependentMethodNames(srcAst, methodNames, depNames) {
280
294
  const methodNamesByDepName = new Map();
281
295
  (0, traverse_1.default)(srcAst, {
@@ -373,3 +387,46 @@ function deleteModMethods(srcAst, mapContext) {
373
387
  });
374
388
  }
375
389
  exports.deleteModMethods = deleteModMethods;
390
+ /**
391
+ * 获取方法内部调用的方法名
392
+ * @param srcAst - 源码AST
393
+ * @param entry - 入口
394
+ * @param methodName - 方法名
395
+ * @param mapContext
396
+ */
397
+ function getInlineMethodsByMethodName(srcAst, entry, methodName, useMethodNameMap) {
398
+ const inlineMethods = new Set();
399
+ const selfMethods = useMethodNameMap.get(entry);
400
+ const mods = [...useMethodNameMap.keys()];
401
+ (0, traverse_1.default)(srcAst, {
402
+ MemberExpression(path) {
403
+ const objectMethodPath = path.findParent((path) => core_1.types.isObjectMethod(path));
404
+ if (!objectMethodPath) {
405
+ return;
406
+ }
407
+ if (!core_1.types.isIdentifier(objectMethodPath.node.key)) {
408
+ return;
409
+ }
410
+ if (objectMethodPath.node.key.name !== methodName) {
411
+ return;
412
+ }
413
+ if (core_1.types.isThisExpression(path.node.object) &&
414
+ core_1.types.isIdentifier(path.node.property)) {
415
+ if (selfMethods.includes(path.node.property.name)) {
416
+ inlineMethods.add(`${entry}#${path.node.property.name}`);
417
+ }
418
+ }
419
+ if (core_1.types.isIdentifier(path.node.object) &&
420
+ core_1.types.isIdentifier(path.node.property)) {
421
+ const modName = path.node.object.name;
422
+ const methodName = path.node.property.name;
423
+ if (mods.includes(modName) &&
424
+ useMethodNameMap.get(modName).includes(methodName)) {
425
+ inlineMethods.add(`${modName}#${methodName}`);
426
+ }
427
+ }
428
+ }
429
+ });
430
+ return [...inlineMethods];
431
+ }
432
+ exports.getInlineMethodsByMethodName = getInlineMethodsByMethodName;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kezh/my-custom-js-converter",
3
- "version": "0.1.62",
3
+ "version": "0.2.1",
4
4
  "description": "自定义代码转换器",
5
5
  "main": "index.js",
6
6
  "bin": {