@next-core/brick-utils 2.31.5 → 2.32.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/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.32.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.31.5...@next-core/brick-utils@2.32.0) (2021-12-28)
7
+
8
+
9
+ ### Features
10
+
11
+ * refine storyboard functions and expressions ([6626c61](https://github.com/easyops-cn/next-core/commit/6626c61bd910864daaa81be946f765630cfb8103))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [2.31.5](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.31.4...@next-core/brick-utils@2.31.5) (2021-12-24)
7
18
 
8
19
  **Note:** Version bump only for package @next-core/brick-utils
@@ -19336,6 +19336,59 @@
19336
19336
  /* noop */
19337
19337
  }
19338
19338
 
19339
+ function visitStoryboardFunctions(functions, beforeVisitGlobal) {
19340
+ if (Array.isArray(functions)) {
19341
+ for (var fn of functions) {
19342
+ try {
19343
+ precookFunction(fn.source, {
19344
+ typescript: fn.typescript,
19345
+ withParent: true,
19346
+ hooks: {
19347
+ beforeVisitGlobal
19348
+ }
19349
+ });
19350
+ } catch (error) {
19351
+ // eslint-disable-next-line no-console
19352
+ console.error("Parse storyboard function \"".concat(fn.name, "\" failed:"), error);
19353
+ }
19354
+ }
19355
+ }
19356
+ }
19357
+ function visitStoryboardExpressions(data, beforeVisitGlobal, keyword) {
19358
+ var memo = new WeakSet();
19359
+
19360
+ function visit(value) {
19361
+ if (typeof value === "string") {
19362
+ if (value.includes(keyword) && isEvaluable(value)) {
19363
+ try {
19364
+ preevaluate(value, {
19365
+ withParent: true,
19366
+ hooks: {
19367
+ beforeVisitGlobal
19368
+ }
19369
+ });
19370
+ } catch (error) {
19371
+ // eslint-disable-next-line no-console
19372
+ console.error("Parse storyboard expression failed:", error);
19373
+ }
19374
+ }
19375
+ } else if (isObject(value)) {
19376
+ // Avoid call stack overflow.
19377
+ if (memo.has(value)) {
19378
+ return;
19379
+ }
19380
+
19381
+ memo.add(value);
19382
+
19383
+ for (var item of Array.isArray(value) ? value : Object.values(value)) {
19384
+ visit(item);
19385
+ }
19386
+ }
19387
+ }
19388
+
19389
+ visit(data);
19390
+ }
19391
+
19339
19392
  var PROCESSORS = "PROCESSORS";
19340
19393
  function scanProcessorsInStoryboard(storyboard) {
19341
19394
  var _storyboard$meta;
@@ -19346,50 +19399,21 @@
19346
19399
  function scanProcessorsInAny(data) {
19347
19400
  var isUniq = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
19348
19401
  var collection = [];
19349
- collectProcessors(data, collection);
19402
+ visitStoryboardExpressions(data, beforeVisitProcessorsFactory(collection), PROCESSORS);
19350
19403
  return isUniq ? lodash.uniq(collection) : collection;
19351
19404
  }
19352
19405
 
19353
- function collectProcessors(data, collection) {
19354
- var memo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new WeakSet();
19355
-
19356
- if (typeof data === "string") {
19357
- if (data.includes(PROCESSORS) && isEvaluable(data)) {
19358
- preevaluate(data, {
19359
- withParent: true,
19360
- hooks: {
19361
- beforeVisitGlobal(node, parent) {
19362
- if (node.name === PROCESSORS) {
19363
- var memberParent = parent[parent.length - 1];
19364
- var outerMemberParent = parent[parent.length - 2];
19365
-
19366
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object" && !memberParent.node.computed && memberParent.node.property.type === "Identifier" && (outerMemberParent === null || outerMemberParent === void 0 ? void 0 : outerMemberParent.node.type) === "MemberExpression" && outerMemberParent.key === "object" && !outerMemberParent.node.computed && outerMemberParent.node.property.type === "Identifier") {
19367
- collection.push("".concat(memberParent.node.property.name, ".").concat(outerMemberParent.node.property.name));
19368
- }
19369
- }
19370
- }
19406
+ function beforeVisitProcessorsFactory(collection) {
19407
+ return function beforeVisitProcessors(node, parent) {
19408
+ if (node.name === PROCESSORS) {
19409
+ var memberParent = parent[parent.length - 1];
19410
+ var outerMemberParent = parent[parent.length - 2];
19371
19411
 
19372
- }
19373
- });
19374
- }
19375
- } else if (isObject(data)) {
19376
- // Avoid call stack overflow.
19377
- if (memo.has(data)) {
19378
- return;
19379
- }
19380
-
19381
- memo.add(data);
19382
-
19383
- if (Array.isArray(data)) {
19384
- for (var item of data) {
19385
- collectProcessors(item, collection, memo);
19386
- }
19387
- } else {
19388
- for (var _item of Object.values(data)) {
19389
- collectProcessors(_item, collection, memo);
19412
+ if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object" && !memberParent.node.computed && memberParent.node.property.type === "Identifier" && (outerMemberParent === null || outerMemberParent === void 0 ? void 0 : outerMemberParent.node.type) === "MemberExpression" && outerMemberParent.key === "object" && !outerMemberParent.node.computed && outerMemberParent.node.property.type === "Identifier") {
19413
+ collection.push("".concat(memberParent.node.property.name, ".").concat(outerMemberParent.node.property.name));
19390
19414
  }
19391
19415
  }
19392
- }
19416
+ };
19393
19417
  }
19394
19418
 
19395
19419
  function getDllAndDepsOfStoryboard(storyboard, brickPackages, options) {
@@ -20142,58 +20166,37 @@
20142
20166
  function scanPermissionActionsInStoryboard(storyboard) {
20143
20167
  var _storyboard$meta;
20144
20168
 
20145
- return scanPermissionActionsInAny([storyboard.routes, (_storyboard$meta = storyboard.meta) === null || _storyboard$meta === void 0 ? void 0 : _storyboard$meta.customTemplates]);
20169
+ var collection = new Set();
20170
+ var beforeVisitPermissions = beforeVisitPermissionsFactory(collection);
20171
+ var {
20172
+ customTemplates,
20173
+ functions
20174
+ } = (_storyboard$meta = storyboard.meta) !== null && _storyboard$meta !== void 0 ? _storyboard$meta : {};
20175
+ visitStoryboardExpressions([storyboard.routes, customTemplates], beforeVisitPermissions, PERMISSIONS);
20176
+ visitStoryboardFunctions(functions, beforeVisitPermissions);
20177
+ return Array.from(collection);
20146
20178
  }
20147
20179
  function scanPermissionActionsInAny(data) {
20148
20180
  var collection = new Set();
20149
- collectPermissionActions(data, collection);
20181
+ visitStoryboardExpressions(data, beforeVisitPermissionsFactory(collection), PERMISSIONS);
20150
20182
  return Array.from(collection);
20151
20183
  }
20152
20184
 
20153
- function collectPermissionActions(data, collection) {
20154
- var memo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new WeakSet();
20155
-
20156
- if (typeof data === "string") {
20157
- if (data.includes(PERMISSIONS) && isEvaluable(data)) {
20158
- preevaluate(data, {
20159
- withParent: true,
20160
- hooks: {
20161
- beforeVisitGlobal(node, parent) {
20162
- if (node.name === PERMISSIONS) {
20163
- var memberParent = parent[parent.length - 1];
20164
- var callParent = parent[parent.length - 2];
20165
-
20166
- if ((callParent === null || callParent === void 0 ? void 0 : callParent.node.type) === "CallExpression" && (callParent === null || callParent === void 0 ? void 0 : callParent.key) === "callee" && (memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object" && !memberParent.node.computed && memberParent.node.property.type === "Identifier" && memberParent.node.property.name === check) {
20167
- for (var arg of callParent.node.arguments) {
20168
- if (arg.type === "Literal" && typeof arg.value === "string") {
20169
- collection.add(arg.value);
20170
- }
20171
- }
20172
- }
20173
- }
20174
- }
20185
+ function beforeVisitPermissionsFactory(collection) {
20186
+ return function beforeVisitPermissions(node, parent) {
20187
+ if (node.name === PERMISSIONS) {
20188
+ var memberParent = parent[parent.length - 1];
20189
+ var callParent = parent[parent.length - 2];
20175
20190
 
20191
+ if ((callParent === null || callParent === void 0 ? void 0 : callParent.node.type) === "CallExpression" && (callParent === null || callParent === void 0 ? void 0 : callParent.key) === "callee" && (memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object" && !memberParent.node.computed && memberParent.node.property.type === "Identifier" && memberParent.node.property.name === check) {
20192
+ for (var arg of callParent.node.arguments) {
20193
+ if (arg.type === "Literal" && typeof arg.value === "string") {
20194
+ collection.add(arg.value);
20195
+ }
20176
20196
  }
20177
- });
20178
- }
20179
- } else if (isObject(data)) {
20180
- // Avoid call stack overflow.
20181
- if (memo.has(data)) {
20182
- return;
20183
- }
20184
-
20185
- memo.add(data);
20186
-
20187
- if (Array.isArray(data)) {
20188
- for (var item of data) {
20189
- collectPermissionActions(item, collection, memo);
20190
- }
20191
- } else {
20192
- for (var _item of Object.values(data)) {
20193
- collectPermissionActions(_item, collection, memo);
20194
20197
  }
20195
20198
  }
20196
- }
20199
+ };
20197
20200
  }
20198
20201
 
20199
20202
  function scanRouteAliasInStoryboard(storyboard) {
@@ -20253,69 +20256,25 @@
20253
20256
  var _storyboard$meta;
20254
20257
 
20255
20258
  var collection = new Map();
20256
- var beforeVisitGlobal = beforeVisitGlobalFactory(collection); // Notice: `menus` may contain evaluations of I18N too.
20259
+ var beforeVisitI18n = beforeVisitI18nFactory(collection); // Notice: `menus` may contain evaluations of I18N too.
20257
20260
 
20258
20261
  var {
20259
20262
  customTemplates,
20260
20263
  menus,
20261
20264
  functions
20262
20265
  } = (_storyboard$meta = storyboard.meta) !== null && _storyboard$meta !== void 0 ? _storyboard$meta : {};
20263
- collectI18N([storyboard.routes, customTemplates, menus], beforeVisitGlobal);
20264
-
20265
- if (Array.isArray(functions)) {
20266
- for (var fn of functions) {
20267
- precookFunction(fn.source, {
20268
- typescript: fn.typescript,
20269
- withParent: true,
20270
- hooks: {
20271
- beforeVisitGlobal
20272
- }
20273
- });
20274
- }
20275
- }
20276
-
20266
+ visitStoryboardExpressions([storyboard.routes, customTemplates, menus], beforeVisitI18n, I18N);
20267
+ visitStoryboardFunctions(functions, beforeVisitI18n);
20277
20268
  return collection;
20278
20269
  }
20279
20270
  function scanI18NInAny(data) {
20280
20271
  var collection = new Map();
20281
- collectI18N(data, beforeVisitGlobalFactory(collection));
20272
+ visitStoryboardExpressions(data, beforeVisitI18nFactory(collection), I18N);
20282
20273
  return collection;
20283
20274
  }
20284
20275
 
20285
- function collectI18N(data, beforeVisitGlobal) {
20286
- var memo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new WeakSet();
20287
-
20288
- if (typeof data === "string") {
20289
- if (data.includes(I18N) && isEvaluable(data)) {
20290
- preevaluate(data, {
20291
- withParent: true,
20292
- hooks: {
20293
- beforeVisitGlobal
20294
- }
20295
- });
20296
- }
20297
- } else if (isObject(data)) {
20298
- // Avoid call stack overflow.
20299
- if (memo.has(data)) {
20300
- return;
20301
- }
20302
-
20303
- memo.add(data);
20304
-
20305
- if (Array.isArray(data)) {
20306
- for (var item of data) {
20307
- collectI18N(item, beforeVisitGlobal, memo);
20308
- }
20309
- } else {
20310
- for (var _item of Object.values(data)) {
20311
- collectI18N(_item, beforeVisitGlobal, memo);
20312
- }
20313
- }
20314
- }
20315
- }
20316
-
20317
- function beforeVisitGlobalFactory(collection) {
20318
- return function beforeVisitGlobal(node, parent) {
20276
+ function beforeVisitI18nFactory(collection) {
20277
+ return function beforeVisitI18n(node, parent) {
20319
20278
  if (node.name === I18N) {
20320
20279
  var callParent = parent[parent.length - 1];
20321
20280
 
@@ -20924,6 +20883,7 @@
20924
20883
  convertUnitValueByPrecision: convertValueByPrecision
20925
20884
  } = pipes.utils;
20926
20885
 
20886
+ var CTX$1 = "CTX";
20927
20887
  function resolveContextConcurrently(_x, _x2) {
20928
20888
  return _resolveContextConcurrently.apply(this, arguments);
20929
20889
  }
@@ -20999,9 +20959,7 @@
20999
20959
  };
21000
20960
 
21001
20961
  if (!_contextConf.property) {
21002
- collectContexts(_contextConf.if, stats);
21003
- collectContexts(_contextConf.value, stats);
21004
- collectContexts(_contextConf.resolve, stats);
20962
+ visitStoryboardExpressions([_contextConf.if, _contextConf.value, _contextConf.resolve], beforeVisitContextFactory(stats), CTX$1);
21005
20963
  }
21006
20964
 
21007
20965
  depsMap.set(_contextConf, stats);
@@ -21009,60 +20967,30 @@
21009
20967
 
21010
20968
  return depsMap;
21011
20969
  }
21012
- var CTX$1 = "CTX";
21013
20970
 
21014
- function collectContexts(data, stats) {
21015
- var memo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new WeakSet();
21016
-
21017
- if (typeof data === "string") {
21018
- if (data.includes(CTX$1) && isEvaluable(data)) {
21019
- preevaluate(data, {
21020
- withParent: true,
21021
- hooks: {
21022
- beforeVisitGlobal(node, parent) {
21023
- if (node.name === CTX$1) {
21024
- var memberParent = parent[parent.length - 1];
21025
-
21026
- if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
21027
- var memberNode = memberParent.node;
21028
- var dep;
21029
-
21030
- if (!memberNode.computed && memberNode.property.type === "Identifier") {
21031
- dep = memberNode.property.name;
21032
- } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
21033
- dep = memberNode.property.value;
21034
- } else {
21035
- stats.includesComputed = true;
21036
- }
20971
+ function beforeVisitContextFactory(stats) {
20972
+ return function beforeVisitContext(node, parent) {
20973
+ if (node.name === CTX$1) {
20974
+ var memberParent = parent[parent.length - 1];
21037
20975
 
21038
- if (dep !== undefined && !stats.dependencies.includes(dep)) {
21039
- stats.dependencies.push(dep);
21040
- }
21041
- }
21042
- }
21043
- }
20976
+ if ((memberParent === null || memberParent === void 0 ? void 0 : memberParent.node.type) === "MemberExpression" && memberParent.key === "object") {
20977
+ var memberNode = memberParent.node;
20978
+ var dep;
21044
20979
 
20980
+ if (!memberNode.computed && memberNode.property.type === "Identifier") {
20981
+ dep = memberNode.property.name;
20982
+ } else if (memberNode.computed && memberNode.property.type === "Literal" && typeof memberNode.property.value === "string") {
20983
+ dep = memberNode.property.value;
20984
+ } else {
20985
+ stats.includesComputed = true;
21045
20986
  }
21046
- });
21047
- }
21048
- } else if (isObject(data)) {
21049
- // Avoid call stack overflow.
21050
- if (memo.has(data)) {
21051
- return;
21052
- }
21053
20987
 
21054
- memo.add(data);
21055
-
21056
- if (Array.isArray(data)) {
21057
- for (var item of data) {
21058
- collectContexts(item, stats, memo);
21059
- }
21060
- } else {
21061
- for (var _item of Object.values(data)) {
21062
- collectContexts(_item, stats, memo);
20988
+ if (dep !== undefined && !stats.dependencies.includes(dep)) {
20989
+ stats.dependencies.push(dep);
20990
+ }
21063
20991
  }
21064
20992
  }
21065
- }
20993
+ };
21066
20994
  }
21067
20995
 
21068
20996
  function detectCircularContexts(dependencyMap) {
@@ -21466,6 +21394,8 @@
21466
21394
  exports.trackContext = trackContext;
21467
21395
  exports.transform = transform;
21468
21396
  exports.transformAndInject = transformAndInject;
21397
+ exports.visitStoryboardExpressions = visitStoryboardExpressions;
21398
+ exports.visitStoryboardFunctions = visitStoryboardFunctions;
21469
21399
 
21470
21400
  Object.defineProperty(exports, '__esModule', { value: true });
21471
21401