@next-core/brick-utils 2.30.3 → 2.31.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.31.0](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.30.3...@next-core/brick-utils@2.31.0) (2021-11-26)
7
+
8
+
9
+ ### Features
10
+
11
+ * scan i18n in storyboard functions ([e0f68fb](https://github.com/easyops-cn/next-core/commit/e0f68fb456e12f62b4f68b380d66e3a535aaef56))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [2.30.3](https://github.com/easyops-cn/next-core/compare/@next-core/brick-utils@2.30.2...@next-core/brick-utils@2.30.3) (2021-11-25)
7
18
 
8
19
  **Note:** Version bump only for package @next-core/brick-utils
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/asyncToGenerator'), require('lodash'), require('@babel/runtime/helpers/defineProperty'), require('@babel/runtime/helpers/objectSpread2'), require('@next-core/pipes'), require('@babel/runtime/helpers/objectWithoutProperties'), require('js-yaml')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/asyncToGenerator', 'lodash', '@babel/runtime/helpers/defineProperty', '@babel/runtime/helpers/objectSpread2', '@next-core/pipes', '@babel/runtime/helpers/objectWithoutProperties', 'js-yaml'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BrickUtils = {}, global._asyncToGenerator, global.lodash, global._defineProperty, global._objectSpread, global.pipes, global._objectWithoutProperties, global.yaml));
5
- })(this, (function (exports, _asyncToGenerator, lodash, _defineProperty, _objectSpread, pipes, _objectWithoutProperties, yaml) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/asyncToGenerator'), require('lodash'), require('@babel/runtime/helpers/defineProperty'), require('@babel/runtime/helpers/objectSpread2'), require('@babel/runtime/helpers/objectWithoutProperties'), require('@next-core/pipes'), require('js-yaml')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/asyncToGenerator', 'lodash', '@babel/runtime/helpers/defineProperty', '@babel/runtime/helpers/objectSpread2', '@babel/runtime/helpers/objectWithoutProperties', '@next-core/pipes', 'js-yaml'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.BrickUtils = {}, global._asyncToGenerator, global.lodash, global._defineProperty, global._objectSpread, global._objectWithoutProperties, global.pipes, global.yaml));
5
+ })(this, (function (exports, _asyncToGenerator, lodash, _defineProperty, _objectSpread, _objectWithoutProperties, pipes, yaml) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -18528,7 +18528,7 @@
18528
18528
  Evaluate(n);
18529
18529
  }
18530
18530
  } else if (node) {
18531
- var _hooks$beforeVisit; // `node` maybe `null` in some cases.
18531
+ var _hooks$beforeVisit, _hooks$beforeVisitUnk; // `node` maybe `null` in some cases.
18532
18532
 
18533
18533
 
18534
18534
  (_hooks$beforeVisit = hooks.beforeVisit) === null || _hooks$beforeVisit === void 0 ? void 0 : _hooks$beforeVisit.call(hooks, node);
@@ -18537,11 +18537,10 @@
18537
18537
  switch (node.type) {
18538
18538
  case "Identifier":
18539
18539
  if (!ResolveBinding(node.name)) {
18540
- attemptToVisitGlobals.add(node.name);
18540
+ var _hooks$beforeVisitGlo;
18541
18541
 
18542
- if (visitors && hasOwnProperty(visitors, "__GlobalVariable")) {
18543
- visitors.__GlobalVariable(node);
18544
- }
18542
+ (_hooks$beforeVisitGlo = hooks.beforeVisitGlobal) === null || _hooks$beforeVisitGlo === void 0 ? void 0 : _hooks$beforeVisitGlo.call(hooks, node);
18543
+ attemptToVisitGlobals.add(node.name);
18545
18544
  }
18546
18545
 
18547
18546
  return;
@@ -18808,9 +18807,9 @@
18808
18807
  }
18809
18808
  }
18810
18809
 
18811
- if (visitors && hasOwnProperty(visitors, "__UnknownNode")) {
18812
- visitors.__UnknownNode(node);
18813
- } else {
18810
+ var silent = (_hooks$beforeVisitUnk = hooks.beforeVisitUnknown) === null || _hooks$beforeVisitUnk === void 0 ? void 0 : _hooks$beforeVisitUnk.call(hooks, node);
18811
+
18812
+ if (!silent) {
18814
18813
  // eslint-disable-next-line no-console
18815
18814
  console.warn("Unsupported node type `".concat(node.type, "`"));
18816
18815
  }
@@ -18996,93 +18995,100 @@
18996
18995
  }
18997
18996
  });
18998
18997
  } else {
18999
- var FunctionVisitor = node => {
19000
- if (node.async || node.generator) {
19001
- errors.push({
19002
- type: "SyntaxError",
19003
- message: "".concat(node.async ? "Async" : "Generator", " function is not allowed"),
19004
- loc: node.loc
19005
- });
19006
- }
19007
- };
19008
-
19009
18998
  precook(func, {
19010
- visitors: {
19011
- ArrowFunctionExpression: FunctionVisitor,
19012
- FunctionDeclaration: FunctionVisitor,
19013
- FunctionExpression: FunctionVisitor,
19014
-
19015
- Literal(node) {
19016
- if (node.regex) {
19017
- if (node.value === null) {
19018
- errors.push({
19019
- type: "SyntaxError",
19020
- message: "Invalid regular expression",
19021
- loc: node.loc
19022
- });
19023
- } else if (node.regex.flags.includes("u")) {
19024
- errors.push({
19025
- type: "SyntaxError",
19026
- message: "Unsupported unicode flag in regular expression",
19027
- loc: node.loc
19028
- });
19029
- }
19030
- }
19031
- },
19032
-
19033
- ObjectExpression(node) {
19034
- for (var prop of node.properties) {
19035
- if (prop.type === "Property") {
19036
- if (prop.kind !== "init") {
18999
+ hooks: {
19000
+ beforeVisit(node) {
19001
+ switch (node.type) {
19002
+ case "ArrowFunctionExpression":
19003
+ case "FunctionDeclaration":
19004
+ case "FunctionExpression":
19005
+ if (node.async || node.generator) {
19037
19006
  errors.push({
19038
19007
  type: "SyntaxError",
19039
- message: "Unsupported object getter/setter property",
19040
- loc: prop.loc
19008
+ message: "".concat(node.async ? "Async" : "Generator", " function is not allowed"),
19009
+ loc: node.loc
19041
19010
  });
19042
- } else if (!prop.computed && prop.key.type === "Identifier" && prop.key.name === "__proto__") {
19011
+ }
19012
+
19013
+ break;
19014
+
19015
+ case "Literal":
19016
+ if (node.regex) {
19017
+ if (node.value === null) {
19018
+ errors.push({
19019
+ type: "SyntaxError",
19020
+ message: "Invalid regular expression",
19021
+ loc: node.loc
19022
+ });
19023
+ } else if (node.regex.flags.includes("u")) {
19024
+ errors.push({
19025
+ type: "SyntaxError",
19026
+ message: "Unsupported unicode flag in regular expression",
19027
+ loc: node.loc
19028
+ });
19029
+ }
19030
+ }
19031
+
19032
+ break;
19033
+
19034
+ case "ObjectExpression":
19035
+ for (var prop of node.properties) {
19036
+ if (prop.type === "Property") {
19037
+ if (prop.kind !== "init") {
19038
+ errors.push({
19039
+ type: "SyntaxError",
19040
+ message: "Unsupported object getter/setter property",
19041
+ loc: prop.loc
19042
+ });
19043
+ } else if (!prop.computed && prop.key.type === "Identifier" && prop.key.name === "__proto__") {
19044
+ errors.push({
19045
+ type: "TypeError",
19046
+ message: "Setting '__proto__' property is not allowed",
19047
+ loc: prop.key.loc
19048
+ });
19049
+ }
19050
+ }
19051
+ }
19052
+
19053
+ break;
19054
+
19055
+ case "VariableDeclaration":
19056
+ if (node.kind === "var" && rules !== null && rules !== void 0 && rules.noVar) {
19043
19057
  errors.push({
19044
- type: "TypeError",
19045
- message: "Setting '__proto__' property is not allowed",
19046
- loc: prop.key.loc
19058
+ type: "SyntaxError",
19059
+ message: "Var declaration is not recommended, use `let` or `const` instead",
19060
+ loc: {
19061
+ start: node.loc.start,
19062
+ end: {
19063
+ line: node.loc.end.line,
19064
+ // Only decorate the "var".
19065
+ column: node.loc.start.column + 3
19066
+ }
19067
+ }
19047
19068
  });
19048
19069
  }
19049
- }
19070
+
19071
+ break;
19050
19072
  }
19051
19073
  },
19052
19074
 
19053
- VariableDeclaration(node) {
19054
- if (node.kind === "var" && rules !== null && rules !== void 0 && rules.noVar) {
19075
+ beforeVisitGlobal(node) {
19076
+ if (node.name === "arguments") {
19055
19077
  errors.push({
19056
19078
  type: "SyntaxError",
19057
- message: "Var declaration is not recommended, use `let` or `const` instead",
19058
- loc: {
19059
- start: node.loc.start,
19060
- end: {
19061
- line: node.loc.end.line,
19062
- // Only decorate the "var".
19063
- column: node.loc.start.column + 3
19064
- }
19065
- }
19079
+ message: "Use the rest parameters instead of 'arguments'",
19080
+ loc: node.loc
19066
19081
  });
19067
19082
  }
19068
19083
  },
19069
19084
 
19070
- __UnknownNode(node) {
19085
+ beforeVisitUnknown(node) {
19071
19086
  errors.push({
19072
19087
  type: "SyntaxError",
19073
19088
  message: "Unsupported syntax: `".concat(node.type, "`"),
19074
19089
  loc: node.loc
19075
19090
  });
19076
- },
19077
-
19078
- __GlobalVariable(node) {
19079
- if (node.name === "arguments") {
19080
- errors.push({
19081
- type: "SyntaxError",
19082
- message: "Use the rest parameters instead of 'arguments'",
19083
- loc: node.loc
19084
- });
19085
- }
19091
+ return true;
19086
19092
  }
19087
19093
 
19088
19094
  }
@@ -20061,39 +20067,45 @@
20061
20067
 
20062
20068
  var I18N = "I18N";
20063
20069
  function scanI18NInStoryboard(storyboard) {
20064
- // Notice: `menus` may contain evaluations of I18N too.
20065
- return scanI18NInAny([storyboard.routes, storyboard.meta && [storyboard.meta.customTemplates, storyboard.meta.menus]]);
20070
+ var _storyboard$meta;
20071
+
20072
+ var collection = new Map();
20073
+ var beforeVisit = beforeVisitFactory(collection); // Notice: `menus` may contain evaluations of I18N too.
20074
+
20075
+ var {
20076
+ customTemplates,
20077
+ menus,
20078
+ functions
20079
+ } = (_storyboard$meta = storyboard.meta) !== null && _storyboard$meta !== void 0 ? _storyboard$meta : {};
20080
+ collectI18N([storyboard.routes, customTemplates, menus], beforeVisit);
20081
+
20082
+ if (Array.isArray(functions)) {
20083
+ for (var fn of functions) {
20084
+ precookFunction(fn.source, {
20085
+ typescript: fn.typescript,
20086
+ hooks: {
20087
+ beforeVisit
20088
+ }
20089
+ });
20090
+ }
20091
+ }
20092
+
20093
+ return collection;
20066
20094
  }
20067
20095
  function scanI18NInAny(data) {
20068
20096
  var collection = new Map();
20069
- collectI18N(data, collection);
20097
+ collectI18N(data, beforeVisitFactory(collection));
20070
20098
  return collection;
20071
20099
  }
20072
20100
 
20073
- function collectI18N(data, collection) {
20101
+ function collectI18N(data, beforeVisit) {
20074
20102
  var memo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : new WeakSet();
20075
20103
 
20076
20104
  if (typeof data === "string") {
20077
20105
  if (data.includes(I18N) && isEvaluable(data)) {
20078
20106
  preevaluate(data, {
20079
- visitors: {
20080
- CallExpression(node) {
20081
- var [keyNode, defaultNode] = node.arguments;
20082
-
20083
- if (node.callee.type === "Identifier" && node.callee.name === I18N && keyNode && keyNode.type === "Literal" && typeof keyNode.value === "string") {
20084
- var valueSet = collection.get(keyNode.value);
20085
-
20086
- if (!valueSet) {
20087
- valueSet = new Set();
20088
- collection.set(keyNode.value, valueSet);
20089
- }
20090
-
20091
- if (defaultNode && defaultNode.type === "Literal" && typeof defaultNode.value === "string") {
20092
- valueSet.add(defaultNode.value);
20093
- }
20094
- }
20095
- }
20096
-
20107
+ hooks: {
20108
+ beforeVisit
20097
20109
  }
20098
20110
  });
20099
20111
  }
@@ -20107,16 +20119,37 @@
20107
20119
 
20108
20120
  if (Array.isArray(data)) {
20109
20121
  for (var item of data) {
20110
- collectI18N(item, collection, memo);
20122
+ collectI18N(item, beforeVisit, memo);
20111
20123
  }
20112
20124
  } else {
20113
20125
  for (var _item of Object.values(data)) {
20114
- collectI18N(_item, collection, memo);
20126
+ collectI18N(_item, beforeVisit, memo);
20115
20127
  }
20116
20128
  }
20117
20129
  }
20118
20130
  }
20119
20131
 
20132
+ function beforeVisitFactory(collection) {
20133
+ return function beforeVisit(node) {
20134
+ if (node.type === "CallExpression") {
20135
+ var [keyNode, defaultNode] = node.arguments;
20136
+
20137
+ if (node.callee.type === "Identifier" && node.callee.name === I18N && keyNode && keyNode.type === "Literal" && typeof keyNode.value === "string") {
20138
+ var valueSet = collection.get(keyNode.value);
20139
+
20140
+ if (!valueSet) {
20141
+ valueSet = new Set();
20142
+ collection.set(keyNode.value, valueSet);
20143
+ }
20144
+
20145
+ if (defaultNode && defaultNode.type === "Literal" && typeof defaultNode.value === "string") {
20146
+ valueSet.add(defaultNode.value);
20147
+ }
20148
+ }
20149
+ }
20150
+ };
20151
+ }
20152
+
20120
20153
  var LexicalStatus;
20121
20154
 
20122
20155
  (function (LexicalStatus) {