@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.
@@ -3,20 +3,32 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.clearFunctionASTCache = clearFunctionASTCache;
6
7
  exports.precookFunction = precookFunction;
7
8
  var _parse = require("./parse");
8
9
  var _precook = require("./precook");
10
+ let ASTCache = new WeakMap();
9
11
  function precookFunction(source, {
10
12
  typescript,
13
+ cacheKey,
11
14
  ...restOptions
12
15
  } = {}) {
13
- const func = (0, _parse.parseAsEstree)(source, {
14
- typescript
15
- });
16
+ let func = cacheKey ? ASTCache.get(cacheKey) : undefined;
17
+ if (!func) {
18
+ func = (0, _parse.parseAsEstree)(source, {
19
+ typescript
20
+ });
21
+ if (cacheKey) {
22
+ ASTCache.set(cacheKey, func);
23
+ }
24
+ }
16
25
  const attemptToVisitGlobals = (0, _precook.precook)(func, restOptions);
17
26
  return {
18
27
  function: func,
19
28
  attemptToVisitGlobals
20
29
  };
21
30
  }
31
+ function clearFunctionASTCache() {
32
+ ASTCache = new WeakMap();
33
+ }
22
34
  //# sourceMappingURL=precookFunction.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"precookFunction.js","names":["precookFunction","source","typescript","restOptions","func","parseAsEstree","attemptToVisitGlobals","precook","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;AACA;AAWO,SAASA,eAAe,CAC7BC,MAAc,EACd;EAAEC,UAAU;EAAE,GAAGC;AAAoC,CAAC,GAAG,CAAC,CAAC,EACpC;EACvB,MAAMC,IAAI,GAAG,IAAAC,oBAAa,EAACJ,MAAM,EAAE;IAAEC;EAAW,CAAC,CAAC;EAClD,MAAMI,qBAAqB,GAAG,IAAAC,gBAAO,EAACH,IAAI,EAAED,WAAW,CAAC;EACxD,OAAO;IACLK,QAAQ,EAAEJ,IAAI;IACdE;EACF,CAAC;AACH"}
1
+ {"version":3,"file":"precookFunction.js","names":["ASTCache","WeakMap","precookFunction","source","typescript","cacheKey","restOptions","func","get","undefined","parseAsEstree","set","attemptToVisitGlobals","precook","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;AACA;AAEA,IAAIA,QAAQ,GAAG,IAAIC,OAAO,EAA+B;AAYlD,SAASC,eAAe,CAC7BC,MAAc,EACd;EAAEC,UAAU;EAAEC,QAAQ;EAAE,GAAGC;AAAoC,CAAC,GAAG,CAAC,CAAC,EAC9C;EACvB,IAAIC,IAAI,GAAGF,QAAQ,GAAGL,QAAQ,CAACQ,GAAG,CAACH,QAAQ,CAAC,GAAGI,SAAS;EACxD,IAAI,CAACF,IAAI,EAAE;IACTA,IAAI,GAAG,IAAAG,oBAAa,EAACP,MAAM,EAAE;MAAEC;IAAW,CAAC,CAAC;IAC5C,IAAIC,QAAQ,EAAE;MACZL,QAAQ,CAACW,GAAG,CAACN,QAAQ,EAAEE,IAAI,CAAC;IAC9B;EACF;EACA,MAAMK,qBAAqB,GAAG,IAAAC,gBAAO,EAACN,IAAI,EAAED,WAAW,CAAC;EACxD,OAAO;IACLQ,QAAQ,EAAEP,IAAI;IACdK;EACF,CAAC;AACH;AAEO,SAASG,qBAAqB,GAAS;EAC5Cf,QAAQ,GAAG,IAAIC,OAAO,EAAE;AAC1B"}
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.clearExpressionASTCache = clearExpressionASTCache;
6
7
  exports.isEvaluable = isEvaluable;
7
8
  exports.isSnippetEvaluation = isSnippetEvaluation;
8
9
  exports.isTrackAll = isTrackAll;
@@ -10,16 +11,26 @@ exports.preevaluate = preevaluate;
10
11
  exports.shouldAllowRecursiveEvaluations = shouldAllowRecursiveEvaluations;
11
12
  var _parse = require("./parse");
12
13
  var _precook = require("./precook");
14
+ const ASTCache = new Map();
13
15
  // `raw` should always be asserted by `isEvaluable`.
14
- function preevaluate(raw, options) {
16
+ function preevaluate(raw, {
17
+ cache,
18
+ ...restOptions
19
+ } = {}) {
15
20
  const fixes = [];
16
21
  const source = raw.replace(/^\s*<%[~=!]?\s|\s%>\s*$/g, m => {
17
22
  fixes.push(m);
18
23
  return "";
19
24
  });
20
- const expression = (0, _parse.parseAsEstreeExpression)(source);
25
+ let expression = cache ? ASTCache.get(source) : undefined;
26
+ if (!expression) {
27
+ expression = (0, _parse.parseAsEstreeExpression)(source);
28
+ if (cache) {
29
+ ASTCache.set(source, expression);
30
+ }
31
+ }
21
32
  const attemptToVisitGlobals = (0, _precook.precook)(expression, {
22
- ...options,
33
+ ...restOptions,
23
34
  expressionOnly: true
24
35
  });
25
36
  return {
@@ -42,4 +53,7 @@ function isSnippetEvaluation(raw) {
42
53
  function isTrackAll(raw) {
43
54
  return /^\s*<%=\s/.test(raw) && /\s%>\s*$/.test(raw);
44
55
  }
56
+ function clearExpressionASTCache() {
57
+ ASTCache.clear();
58
+ }
45
59
  //# sourceMappingURL=preevaluate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"preevaluate.js","names":["preevaluate","raw","options","fixes","source","replace","m","push","expression","parseAsEstreeExpression","attemptToVisitGlobals","precook","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;AACA;AAYA;AACO,SAASA,WAAW,CACzBC,GAAW,EACXC,OAA4B,EACT;EACnB,MAAMC,KAAe,GAAG,EAAE;EAC1B,MAAMC,MAAM,GAAGH,GAAG,CAACI,OAAO,CAAC,0BAA0B,EAAGC,CAAC,IAAK;IAC5DH,KAAK,CAACI,IAAI,CAACD,CAAC,CAAC;IACb,OAAO,EAAE;EACX,CAAC,CAAC;EACF,MAAME,UAAU,GAAG,IAAAC,8BAAuB,EAACL,MAAM,CAAC;EAClD,MAAMM,qBAAqB,GAAG,IAAAC,gBAAO,EAACH,UAAU,EAAE;IAChD,GAAGN,OAAO;IACVU,cAAc,EAAE;EAClB,CAAC,CAAC;EACF,OAAO;IACLJ,UAAU;IACVE,qBAAqB;IACrBN,MAAM;IACNS,MAAM,EAAEV,KAAK,CAAC,CAAC,CAAC;IAChBW,MAAM,EAAEX,KAAK,CAAC,CAAC;EACjB,CAAC;AACH;AAEO,SAASY,WAAW,CAACd,GAAW,EAAW;EAChD,OAAO,eAAe,CAACe,IAAI,CAACf,GAAG,CAAC,IAAI,UAAU,CAACe,IAAI,CAACf,GAAG,CAAC;AAC1D;AAEO,SAASgB,+BAA+B,CAAChB,GAAW,EAAW;EACpE,OAAO,WAAW,CAACe,IAAI,CAACf,GAAG,CAAC;AAC9B;AAEO,SAASiB,mBAAmB,CAACjB,GAAW,EAAW;EACxD,OAAO,gBAAgB,CAACe,IAAI,CAACf,GAAG,CAAC,IAAI,UAAU,CAACe,IAAI,CAACf,GAAG,CAAC;AAC3D;AAEO,SAASkB,UAAU,CAAClB,GAAW,EAAW;EAC/C,OAAO,WAAW,CAACe,IAAI,CAACf,GAAG,CAAC,IAAI,UAAU,CAACe,IAAI,CAACf,GAAG,CAAC;AACtD"}
1
+ {"version":3,"file":"preevaluate.js","names":["ASTCache","Map","preevaluate","raw","cache","restOptions","fixes","source","replace","m","push","expression","get","undefined","parseAsEstreeExpression","set","attemptToVisitGlobals","precook","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;AACA;AAEA,MAAMA,QAAQ,GAAG,IAAIC,GAAG,EAAsB;AAc9C;AACO,SAASC,WAAW,CACzBC,GAAW,EACX;EAAEC,KAAK;EAAE,GAAGC;AAAgC,CAAC,GAAG,CAAC,CAAC,EAC/B;EACnB,MAAMC,KAAe,GAAG,EAAE;EAC1B,MAAMC,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,GAAG,IAAAG,8BAAuB,EAACP,MAAM,CAAC;IAC5C,IAAIH,KAAK,EAAE;MACTJ,QAAQ,CAACe,GAAG,CAACR,MAAM,EAAEI,UAAU,CAAC;IAClC;EACF;EACA,MAAMK,qBAAqB,GAAG,IAAAC,gBAAO,EAACN,UAAU,EAAE;IAChD,GAAGN,WAAW;IACda,cAAc,EAAE;EAClB,CAAC,CAAC;EACF,OAAO;IACLP,UAAU;IACVK,qBAAqB;IACrBT,MAAM;IACNY,MAAM,EAAEb,KAAK,CAAC,CAAC,CAAC;IAChBc,MAAM,EAAEd,KAAK,CAAC,CAAC;EACjB,CAAC;AACH;AAEO,SAASe,WAAW,CAAClB,GAAW,EAAW;EAChD,OAAO,eAAe,CAACmB,IAAI,CAACnB,GAAG,CAAC,IAAI,UAAU,CAACmB,IAAI,CAACnB,GAAG,CAAC;AAC1D;AAEO,SAASoB,+BAA+B,CAACpB,GAAW,EAAW;EACpE,OAAO,WAAW,CAACmB,IAAI,CAACnB,GAAG,CAAC;AAC9B;AAEO,SAASqB,mBAAmB,CAACrB,GAAW,EAAW;EACxD,OAAO,gBAAgB,CAACmB,IAAI,CAACnB,GAAG,CAAC,IAAI,UAAU,CAACmB,IAAI,CAACnB,GAAG,CAAC;AAC3D;AAEO,SAASsB,UAAU,CAACtB,GAAW,EAAW;EAC/C,OAAO,WAAW,CAACmB,IAAI,CAACnB,GAAG,CAAC,IAAI,UAAU,CAACmB,IAAI,CAACnB,GAAG,CAAC;AACtD;AAEO,SAASuB,uBAAuB,GAAS;EAC9C1B,QAAQ,CAAC2B,KAAK,EAAE;AAClB"}
package/dist/esm/cook.js CHANGED
@@ -128,6 +128,8 @@ export function cook(rootAst, codeSource) {
128
128
  // Currently unicode flag is not fully supported across major browsers.
129
129
  throw new SyntaxError("Unsupported unicode flag in regular expression: ".concat(node.raw));
130
130
  }
131
+ // Always create a new RegExp, because the AST will be reused.
132
+ return NormalCompletion(new RegExp(node.regex.pattern, node.regex.flags));
131
133
  }
132
134
  return NormalCompletion(node.value);
133
135
  }