@next-core/cook 1.9.21 → 1.9.23

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.
@@ -1,20 +1,31 @@
1
1
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
2
- var _excluded = ["typescript"];
2
+ var _excluded = ["typescript", "cacheKey"];
3
3
  import { parseAsEstree } from "./parse";
4
4
  import { precook } from "./precook";
5
+ var ASTCache = new WeakMap();
5
6
  export function precookFunction(source) {
6
7
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
7
8
  {
8
- typescript
9
+ typescript,
10
+ cacheKey
9
11
  } = _ref,
10
12
  restOptions = _objectWithoutProperties(_ref, _excluded);
11
- var func = parseAsEstree(source, {
12
- typescript
13
- });
13
+ var func = cacheKey ? ASTCache.get(cacheKey) : undefined;
14
+ if (!func) {
15
+ func = parseAsEstree(source, {
16
+ typescript
17
+ });
18
+ if (cacheKey) {
19
+ ASTCache.set(cacheKey, func);
20
+ }
21
+ }
14
22
  var attemptToVisitGlobals = precook(func, restOptions);
15
23
  return {
16
24
  function: func,
17
25
  attemptToVisitGlobals
18
26
  };
19
27
  }
28
+ export function clearFunctionASTCache() {
29
+ ASTCache = new WeakMap();
30
+ }
20
31
  //# sourceMappingURL=precookFunction.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"precookFunction.js","names":["parseAsEstree","precook","precookFunction","source","typescript","restOptions","func","attemptToVisitGlobals","function"],"sources":["../../src/precookFunction.ts"],"sourcesContent":["import { FunctionDeclaration } from \"@babel/types\";\nimport { parseAsEstree } from \"./parse\";\nimport { precook, PrecookOptions } from \"./precook\";\n\nexport interface PrecookFunctionOptions extends PrecookOptions {\n typescript?: boolean;\n}\n\nexport interface PrecookFunctionResult {\n function: FunctionDeclaration;\n attemptToVisitGlobals: Set<string>;\n}\n\nexport function precookFunction(\n source: string,\n { typescript, ...restOptions }: PrecookFunctionOptions = {}\n): PrecookFunctionResult {\n const func = parseAsEstree(source, { typescript });\n const attemptToVisitGlobals = precook(func, restOptions);\n return {\n function: func,\n attemptToVisitGlobals,\n };\n}\n"],"mappings":";;AACA,SAASA,aAAa,QAAQ,SAAS;AACvC,SAASC,OAAO,QAAwB,WAAW;AAWnD,OAAO,SAASC,eAAe,CAC7BC,MAAc,EAES;EAAA,+EADkC,CAAC,CAAC;IAA3D;MAAEC;IAAmD,CAAC;IAArCC,WAAW;EAE5B,IAAMC,IAAI,GAAGN,aAAa,CAACG,MAAM,EAAE;IAAEC;EAAW,CAAC,CAAC;EAClD,IAAMG,qBAAqB,GAAGN,OAAO,CAACK,IAAI,EAAED,WAAW,CAAC;EACxD,OAAO;IACLG,QAAQ,EAAEF,IAAI;IACdC;EACF,CAAC;AACH"}
1
+ {"version":3,"file":"precookFunction.js","names":["parseAsEstree","precook","ASTCache","WeakMap","precookFunction","source","typescript","cacheKey","restOptions","func","get","undefined","set","attemptToVisitGlobals","function","clearFunctionASTCache"],"sources":["../../src/precookFunction.ts"],"sourcesContent":["import type { FunctionDeclaration } from \"@babel/types\";\nimport { parseAsEstree } from \"./parse\";\nimport { precook, PrecookOptions } from \"./precook\";\n\nlet ASTCache = new WeakMap<object, FunctionDeclaration>();\n\nexport interface PrecookFunctionOptions extends PrecookOptions {\n cacheKey?: object;\n typescript?: boolean;\n}\n\nexport interface PrecookFunctionResult {\n function: FunctionDeclaration;\n attemptToVisitGlobals: Set<string>;\n}\n\nexport function precookFunction(\n source: string,\n { typescript, cacheKey, ...restOptions }: PrecookFunctionOptions = {}\n): PrecookFunctionResult {\n let func = cacheKey ? ASTCache.get(cacheKey) : undefined;\n if (!func) {\n func = parseAsEstree(source, { typescript });\n if (cacheKey) {\n ASTCache.set(cacheKey, func);\n }\n }\n const attemptToVisitGlobals = precook(func, restOptions);\n return {\n function: func,\n attemptToVisitGlobals,\n };\n}\n\nexport function clearFunctionASTCache(): void {\n ASTCache = new WeakMap();\n}\n"],"mappings":";;AACA,SAASA,aAAa,QAAQ,SAAS;AACvC,SAASC,OAAO,QAAwB,WAAW;AAEnD,IAAIC,QAAQ,GAAG,IAAIC,OAAO,EAA+B;AAYzD,OAAO,SAASC,eAAe,CAC7BC,MAAc,EAES;EAAA,+EAD4C,CAAC,CAAC;IAArE;MAAEC,UAAU;MAAEC;IAAiD,CAAC;IAArCC,WAAW;EAEtC,IAAIC,IAAI,GAAGF,QAAQ,GAAGL,QAAQ,CAACQ,GAAG,CAACH,QAAQ,CAAC,GAAGI,SAAS;EACxD,IAAI,CAACF,IAAI,EAAE;IACTA,IAAI,GAAGT,aAAa,CAACK,MAAM,EAAE;MAAEC;IAAW,CAAC,CAAC;IAC5C,IAAIC,QAAQ,EAAE;MACZL,QAAQ,CAACU,GAAG,CAACL,QAAQ,EAAEE,IAAI,CAAC;IAC9B;EACF;EACA,IAAMI,qBAAqB,GAAGZ,OAAO,CAACQ,IAAI,EAAED,WAAW,CAAC;EACxD,OAAO;IACLM,QAAQ,EAAEL,IAAI;IACdI;EACF,CAAC;AACH;AAEA,OAAO,SAASE,qBAAqB,GAAS;EAC5Cb,QAAQ,GAAG,IAAIC,OAAO,EAAE;AAC1B"}
@@ -1,15 +1,29 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
+ import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
+ var _excluded = ["cache"];
2
4
  import { parseAsEstreeExpression } from "./parse";
3
5
  import { precook } from "./precook";
6
+ var ASTCache = new Map();
4
7
  // `raw` should always be asserted by `isEvaluable`.
5
- export function preevaluate(raw, options) {
8
+ export function preevaluate(raw) {
9
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
10
+ {
11
+ cache
12
+ } = _ref,
13
+ restOptions = _objectWithoutProperties(_ref, _excluded);
6
14
  var fixes = [];
7
15
  var source = raw.replace(/^\s*<%[~=!]?\s|\s%>\s*$/g, m => {
8
16
  fixes.push(m);
9
17
  return "";
10
18
  });
11
- var expression = parseAsEstreeExpression(source);
12
- var attemptToVisitGlobals = precook(expression, _objectSpread(_objectSpread({}, options), {}, {
19
+ var expression = cache ? ASTCache.get(source) : undefined;
20
+ if (!expression) {
21
+ expression = parseAsEstreeExpression(source);
22
+ if (cache) {
23
+ ASTCache.set(source, expression);
24
+ }
25
+ }
26
+ var attemptToVisitGlobals = precook(expression, _objectSpread(_objectSpread({}, restOptions), {}, {
13
27
  expressionOnly: true
14
28
  }));
15
29
  return {
@@ -32,4 +46,7 @@ export function isSnippetEvaluation(raw) {
32
46
  export function isTrackAll(raw) {
33
47
  return /^\s*<%=\s/.test(raw) && /\s%>\s*$/.test(raw);
34
48
  }
49
+ export function clearExpressionASTCache() {
50
+ ASTCache.clear();
51
+ }
35
52
  //# sourceMappingURL=preevaluate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"preevaluate.js","names":["parseAsEstreeExpression","precook","preevaluate","raw","options","fixes","source","replace","m","push","expression","attemptToVisitGlobals","expressionOnly","prefix","suffix","isEvaluable","test","shouldAllowRecursiveEvaluations","isSnippetEvaluation","isTrackAll"],"sources":["../../src/preevaluate.ts"],"sourcesContent":["import { Expression } from \"@babel/types\";\nimport { parseAsEstreeExpression } from \"./parse\";\nimport { precook, PrecookOptions } from \"./precook\";\n\nexport type PreevaluateOptions = Omit<PrecookOptions, \"expressionOnly\">;\n\nexport interface PreevaluateResult {\n expression: Expression;\n attemptToVisitGlobals: Set<string>;\n source: string;\n prefix: string;\n suffix: string;\n}\n\n// `raw` should always be asserted by `isEvaluable`.\nexport function preevaluate(\n raw: string,\n options?: PreevaluateOptions\n): PreevaluateResult {\n const fixes: string[] = [];\n const source = raw.replace(/^\\s*<%[~=!]?\\s|\\s%>\\s*$/g, (m) => {\n fixes.push(m);\n return \"\";\n });\n const expression = parseAsEstreeExpression(source);\n const attemptToVisitGlobals = precook(expression, {\n ...options,\n expressionOnly: true,\n });\n return {\n expression,\n attemptToVisitGlobals,\n source,\n prefix: fixes[0],\n suffix: fixes[1],\n };\n}\n\nexport function isEvaluable(raw: string): boolean {\n return /^\\s*<%[~=]?\\s/.test(raw) && /\\s%>\\s*$/.test(raw);\n}\n\nexport function shouldAllowRecursiveEvaluations(raw: string): boolean {\n return /^\\s*<%~\\s/.test(raw);\n}\n\nexport function isSnippetEvaluation(raw: string): boolean {\n return /^\\s*<%[!@]=?\\s/.test(raw) && /\\s%>\\s*$/.test(raw);\n}\n\nexport function isTrackAll(raw: string): boolean {\n return /^\\s*<%=\\s/.test(raw) && /\\s%>\\s*$/.test(raw);\n}\n"],"mappings":";AACA,SAASA,uBAAuB,QAAQ,SAAS;AACjD,SAASC,OAAO,QAAwB,WAAW;AAYnD;AACA,OAAO,SAASC,WAAW,CACzBC,GAAW,EACXC,OAA4B,EACT;EACnB,IAAMC,KAAe,GAAG,EAAE;EAC1B,IAAMC,MAAM,GAAGH,GAAG,CAACI,OAAO,CAAC,0BAA0B,EAAGC,CAAC,IAAK;IAC5DH,KAAK,CAACI,IAAI,CAACD,CAAC,CAAC;IACb,OAAO,EAAE;EACX,CAAC,CAAC;EACF,IAAME,UAAU,GAAGV,uBAAuB,CAACM,MAAM,CAAC;EAClD,IAAMK,qBAAqB,GAAGV,OAAO,CAACS,UAAU,kCAC3CN,OAAO;IACVQ,cAAc,EAAE;EAAI,GACpB;EACF,OAAO;IACLF,UAAU;IACVC,qBAAqB;IACrBL,MAAM;IACNO,MAAM,EAAER,KAAK,CAAC,CAAC,CAAC;IAChBS,MAAM,EAAET,KAAK,CAAC,CAAC;EACjB,CAAC;AACH;AAEA,OAAO,SAASU,WAAW,CAACZ,GAAW,EAAW;EAChD,OAAO,eAAe,CAACa,IAAI,CAACb,GAAG,CAAC,IAAI,UAAU,CAACa,IAAI,CAACb,GAAG,CAAC;AAC1D;AAEA,OAAO,SAASc,+BAA+B,CAACd,GAAW,EAAW;EACpE,OAAO,WAAW,CAACa,IAAI,CAACb,GAAG,CAAC;AAC9B;AAEA,OAAO,SAASe,mBAAmB,CAACf,GAAW,EAAW;EACxD,OAAO,gBAAgB,CAACa,IAAI,CAACb,GAAG,CAAC,IAAI,UAAU,CAACa,IAAI,CAACb,GAAG,CAAC;AAC3D;AAEA,OAAO,SAASgB,UAAU,CAAChB,GAAW,EAAW;EAC/C,OAAO,WAAW,CAACa,IAAI,CAACb,GAAG,CAAC,IAAI,UAAU,CAACa,IAAI,CAACb,GAAG,CAAC;AACtD"}
1
+ {"version":3,"file":"preevaluate.js","names":["parseAsEstreeExpression","precook","ASTCache","Map","preevaluate","raw","cache","restOptions","fixes","source","replace","m","push","expression","get","undefined","set","attemptToVisitGlobals","expressionOnly","prefix","suffix","isEvaluable","test","shouldAllowRecursiveEvaluations","isSnippetEvaluation","isTrackAll","clearExpressionASTCache","clear"],"sources":["../../src/preevaluate.ts"],"sourcesContent":["import type { Expression } from \"@babel/types\";\nimport { parseAsEstreeExpression } from \"./parse\";\nimport { precook, PrecookOptions } from \"./precook\";\n\nconst ASTCache = new Map<string, Expression>();\n\nexport type PreevaluateOptions = Omit<PrecookOptions, \"expressionOnly\"> & {\n cache?: boolean;\n};\n\nexport interface PreevaluateResult {\n expression: Expression;\n attemptToVisitGlobals: Set<string>;\n source: string;\n prefix: string;\n suffix: string;\n}\n\n// `raw` should always be asserted by `isEvaluable`.\nexport function preevaluate(\n raw: string,\n { cache, ...restOptions }: PreevaluateOptions = {}\n): PreevaluateResult {\n const fixes: string[] = [];\n const source = raw.replace(/^\\s*<%[~=!]?\\s|\\s%>\\s*$/g, (m) => {\n fixes.push(m);\n return \"\";\n });\n let expression = cache ? ASTCache.get(source) : undefined;\n if (!expression) {\n expression = parseAsEstreeExpression(source);\n if (cache) {\n ASTCache.set(source, expression);\n }\n }\n const attemptToVisitGlobals = precook(expression, {\n ...restOptions,\n expressionOnly: true,\n });\n return {\n expression,\n attemptToVisitGlobals,\n source,\n prefix: fixes[0],\n suffix: fixes[1],\n };\n}\n\nexport function isEvaluable(raw: string): boolean {\n return /^\\s*<%[~=]?\\s/.test(raw) && /\\s%>\\s*$/.test(raw);\n}\n\nexport function shouldAllowRecursiveEvaluations(raw: string): boolean {\n return /^\\s*<%~\\s/.test(raw);\n}\n\nexport function isSnippetEvaluation(raw: string): boolean {\n return /^\\s*<%[!@]=?\\s/.test(raw) && /\\s%>\\s*$/.test(raw);\n}\n\nexport function isTrackAll(raw: string): boolean {\n return /^\\s*<%=\\s/.test(raw) && /\\s%>\\s*$/.test(raw);\n}\n\nexport function clearExpressionASTCache(): void {\n ASTCache.clear();\n}\n"],"mappings":";;;AACA,SAASA,uBAAuB,QAAQ,SAAS;AACjD,SAASC,OAAO,QAAwB,WAAW;AAEnD,IAAMC,QAAQ,GAAG,IAAIC,GAAG,EAAsB;AAc9C;AACA,OAAO,SAASC,WAAW,CACzBC,GAAW,EAEQ;EAAA,+EAD6B,CAAC,CAAC;IAAlD;MAAEC;IAA0C,CAAC;IAAjCC,WAAW;EAEvB,IAAMC,KAAe,GAAG,EAAE;EAC1B,IAAMC,MAAM,GAAGJ,GAAG,CAACK,OAAO,CAAC,0BAA0B,EAAGC,CAAC,IAAK;IAC5DH,KAAK,CAACI,IAAI,CAACD,CAAC,CAAC;IACb,OAAO,EAAE;EACX,CAAC,CAAC;EACF,IAAIE,UAAU,GAAGP,KAAK,GAAGJ,QAAQ,CAACY,GAAG,CAACL,MAAM,CAAC,GAAGM,SAAS;EACzD,IAAI,CAACF,UAAU,EAAE;IACfA,UAAU,GAAGb,uBAAuB,CAACS,MAAM,CAAC;IAC5C,IAAIH,KAAK,EAAE;MACTJ,QAAQ,CAACc,GAAG,CAACP,MAAM,EAAEI,UAAU,CAAC;IAClC;EACF;EACA,IAAMI,qBAAqB,GAAGhB,OAAO,CAACY,UAAU,kCAC3CN,WAAW;IACdW,cAAc,EAAE;EAAI,GACpB;EACF,OAAO;IACLL,UAAU;IACVI,qBAAqB;IACrBR,MAAM;IACNU,MAAM,EAAEX,KAAK,CAAC,CAAC,CAAC;IAChBY,MAAM,EAAEZ,KAAK,CAAC,CAAC;EACjB,CAAC;AACH;AAEA,OAAO,SAASa,WAAW,CAAChB,GAAW,EAAW;EAChD,OAAO,eAAe,CAACiB,IAAI,CAACjB,GAAG,CAAC,IAAI,UAAU,CAACiB,IAAI,CAACjB,GAAG,CAAC;AAC1D;AAEA,OAAO,SAASkB,+BAA+B,CAAClB,GAAW,EAAW;EACpE,OAAO,WAAW,CAACiB,IAAI,CAACjB,GAAG,CAAC;AAC9B;AAEA,OAAO,SAASmB,mBAAmB,CAACnB,GAAW,EAAW;EACxD,OAAO,gBAAgB,CAACiB,IAAI,CAACjB,GAAG,CAAC,IAAI,UAAU,CAACiB,IAAI,CAACjB,GAAG,CAAC;AAC3D;AAEA,OAAO,SAASoB,UAAU,CAACpB,GAAW,EAAW;EAC/C,OAAO,WAAW,CAACiB,IAAI,CAACjB,GAAG,CAAC,IAAI,UAAU,CAACiB,IAAI,CAACjB,GAAG,CAAC;AACtD;AAEA,OAAO,SAASqB,uBAAuB,GAAS;EAC9CxB,QAAQ,CAACyB,KAAK,EAAE;AAClB"}
@@ -22,6 +22,7 @@ export interface EstreeLiteral {
22
22
  value: unknown;
23
23
  raw: string;
24
24
  regex?: {
25
+ pattern: string;
25
26
  flags: string;
26
27
  };
27
28
  loc: SourceLocation;
@@ -1,10 +1,12 @@
1
- import { FunctionDeclaration } from "@babel/types";
1
+ import type { FunctionDeclaration } from "@babel/types";
2
2
  import { PrecookOptions } from "./precook";
3
3
  export interface PrecookFunctionOptions extends PrecookOptions {
4
+ cacheKey?: object;
4
5
  typescript?: boolean;
5
6
  }
6
7
  export interface PrecookFunctionResult {
7
8
  function: FunctionDeclaration;
8
9
  attemptToVisitGlobals: Set<string>;
9
10
  }
10
- export declare function precookFunction(source: string, { typescript, ...restOptions }?: PrecookFunctionOptions): PrecookFunctionResult;
11
+ export declare function precookFunction(source: string, { typescript, cacheKey, ...restOptions }?: PrecookFunctionOptions): PrecookFunctionResult;
12
+ export declare function clearFunctionASTCache(): void;
@@ -1,6 +1,8 @@
1
- import { Expression } from "@babel/types";
1
+ import type { Expression } from "@babel/types";
2
2
  import { PrecookOptions } from "./precook";
3
- export declare type PreevaluateOptions = Omit<PrecookOptions, "expressionOnly">;
3
+ export declare type PreevaluateOptions = Omit<PrecookOptions, "expressionOnly"> & {
4
+ cache?: boolean;
5
+ };
4
6
  export interface PreevaluateResult {
5
7
  expression: Expression;
6
8
  attemptToVisitGlobals: Set<string>;
@@ -8,8 +10,9 @@ export interface PreevaluateResult {
8
10
  prefix: string;
9
11
  suffix: string;
10
12
  }
11
- export declare function preevaluate(raw: string, options?: PreevaluateOptions): PreevaluateResult;
13
+ export declare function preevaluate(raw: string, { cache, ...restOptions }?: PreevaluateOptions): PreevaluateResult;
12
14
  export declare function isEvaluable(raw: string): boolean;
13
15
  export declare function shouldAllowRecursiveEvaluations(raw: string): boolean;
14
16
  export declare function isSnippetEvaluation(raw: string): boolean;
15
17
  export declare function isTrackAll(raw: string): boolean;
18
+ export declare function clearExpressionASTCache(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next-core/cook",
3
- "version": "1.9.21",
3
+ "version": "1.9.23",
4
4
  "description": "Cook expressions and storyboard functions",
5
5
  "homepage": "https://github.com/easyops-cn/next-core/tree/master/packages/cook",
6
6
  "license": "GPL-3.0",
@@ -30,12 +30,12 @@
30
30
  "sideEffects": false,
31
31
  "dependencies": {
32
32
  "@babel/parser": "^7.20.5",
33
- "@next-core/brick-types": "^2.98.0"
33
+ "@next-core/brick-types": "^2.98.1"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@next-core/build-next-libs": "^0.2.16",
37
- "@next-core/supply": "^1.1.21",
37
+ "@next-core/supply": "^1.1.22",
38
38
  "lodash": "^4.17.21"
39
39
  },
40
- "gitHead": "f473f042645d24462e595d961f14b22e15bd67fd"
40
+ "gitHead": "2f3e51e97c53f902790e63e9cfc887afb91d4fc1"
41
41
  }