@next-core/cook 2.4.7 → 2.4.8

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 +1 @@
1
- {"version":3,"file":"interfaces.js","names":[],"sources":["../../src/interfaces.ts"],"sourcesContent":["import { parse } from \"@babel/parser\";\nimport {\n Expression,\n FunctionDeclaration,\n LVal,\n Node,\n ObjectExpression,\n ObjectPattern,\n ObjectProperty,\n RestElement,\n SourceLocation,\n SpreadElement,\n VariableDeclaration,\n type ArrowFunctionExpression,\n type FunctionExpression,\n} from \"@babel/types\";\n\nexport type EstreeNode =\n | Node\n | EstreeObjectExpression\n | EstreeObjectPattern\n | EstreeProperty\n | EstreeChainExpression\n | EstreeLiteral;\n\nexport type EstreeLVal = LVal | EstreeObjectPattern;\n\nexport type EstreeObjectExpression = Omit<ObjectExpression, \"properties\"> & {\n properties: (EstreeProperty | SpreadElement)[];\n};\n\nexport type EstreeObjectPattern = Omit<ObjectPattern, \"properties\"> & {\n properties: (EstreeProperty | RestElement)[];\n};\n\nexport type EstreeProperty = Omit<ObjectProperty, \"type\"> & {\n type: \"Property\";\n kind: \"init\" | \"get\" | \"set\";\n method?: boolean;\n};\n\nexport interface EstreeChainExpression {\n type: \"ChainExpression\";\n expression: Expression;\n loc: SourceLocation;\n}\n\nexport interface EstreeLiteral {\n type: \"Literal\";\n value: unknown;\n raw: string;\n regex?: {\n flags: string;\n };\n loc: SourceLocation;\n}\n\nexport type FunctionDefinition = FunctionExpression | ArrowFunctionExpression;\n\nexport type NodeWithBoundNames =\n | LVal\n | VariableDeclaration\n | FunctionDeclaration;\n\nexport type EstreeVisitors = Record<string, EstreeVisitorFn>;\n\nexport type EstreeVisitorFn = (node: any) => void;\n\nexport interface CookRules {\n noVar?: boolean;\n}\n\nexport type ParseResultOfFile = ReturnType<typeof parse>;\n"],"mappings":"","ignoreList":[]}
1
+ {"version":3,"file":"interfaces.js","names":[],"sources":["../../src/interfaces.ts"],"sourcesContent":["import { parse } from \"@babel/parser\";\nimport {\n Expression,\n FunctionDeclaration,\n LVal,\n Node,\n ObjectExpression,\n ObjectPattern,\n ObjectProperty,\n RestElement,\n SourceLocation,\n SpreadElement,\n VariableDeclaration,\n type ArrowFunctionExpression,\n type FunctionExpression,\n} from \"@babel/types\";\n\nexport type EstreeNode =\n | Node\n | EstreeObjectExpression\n | EstreeObjectPattern\n | EstreeProperty\n | EstreeChainExpression\n | EstreeLiteral;\n\nexport type EstreeLVal = LVal | EstreeObjectPattern;\n\nexport type EstreeObjectExpression = Omit<ObjectExpression, \"properties\"> & {\n properties: (EstreeProperty | SpreadElement)[];\n};\n\nexport type EstreeObjectPattern = Omit<ObjectPattern, \"properties\"> & {\n properties: (EstreeProperty | RestElement)[];\n};\n\nexport type EstreeProperty = Omit<ObjectProperty, \"type\"> & {\n type: \"Property\";\n kind: \"init\" | \"get\" | \"set\";\n method?: boolean;\n};\n\nexport interface EstreeChainExpression {\n type: \"ChainExpression\";\n expression: Expression;\n loc: SourceLocation;\n}\n\nexport interface EstreeLiteral {\n type: \"Literal\";\n value: unknown;\n raw: string;\n regex?: {\n pattern: string;\n flags: string;\n };\n loc: SourceLocation;\n}\n\nexport type FunctionDefinition = FunctionExpression | ArrowFunctionExpression;\n\nexport type NodeWithBoundNames =\n | LVal\n | VariableDeclaration\n | FunctionDeclaration;\n\nexport type EstreeVisitors = Record<string, EstreeVisitorFn>;\n\nexport type EstreeVisitorFn = (node: any) => void;\n\nexport interface CookRules {\n noVar?: boolean;\n}\n\nexport type ParseResultOfFile = ReturnType<typeof parse>;\n"],"mappings":"","ignoreList":[]}
@@ -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.js");
8
9
  var _precook = require("./precook.js");
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":["_parse","require","_precook","precookFunction","source","typescript","restOptions","func","parseAsEstree","attemptToVisitGlobals","precook","function"],"sources":["../../src/precookFunction.ts"],"sourcesContent":["import { FunctionDeclaration } from \"@babel/types\";\nimport { parseAsEstree } from \"./parse.js\";\nimport { precook, PrecookOptions } from \"./precook.js\";\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,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAWO,SAASE,eAAeA,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","ignoreList":[]}
1
+ {"version":3,"file":"precookFunction.js","names":["_parse","require","_precook","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.js\";\nimport { precook, PrecookOptions } from \"./precook.js\";\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,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAEA,IAAIE,QAAQ,GAAG,IAAIC,OAAO,CAA8B,CAAC;AAYlD,SAASC,eAAeA,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,qBAAqBA,CAAA,EAAS;EAC5Cf,QAAQ,GAAG,IAAIC,OAAO,CAAC,CAAC;AAC1B","ignoreList":[]}
@@ -3,22 +3,33 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.clearExpressionASTCache = clearExpressionASTCache;
6
7
  exports.isEvaluable = isEvaluable;
7
8
  exports.isTrackAll = isTrackAll;
8
9
  exports.preevaluate = preevaluate;
9
10
  exports.shouldAllowRecursiveEvaluations = shouldAllowRecursiveEvaluations;
10
11
  var _parse = require("./parse.js");
11
12
  var _precook = require("./precook.js");
13
+ const ASTCache = new Map();
12
14
  // `raw` should always be asserted by `isEvaluable`.
13
- function preevaluate(raw, options) {
15
+ function preevaluate(raw, {
16
+ cache,
17
+ ...restOptions
18
+ } = {}) {
14
19
  const fixes = [];
15
20
  const source = raw.replace(/^\s*<%[~=]?\s|\s%>\s*$/g, m => {
16
21
  fixes.push(m);
17
22
  return "";
18
23
  });
19
- const expression = (0, _parse.parseAsEstreeExpression)(source);
24
+ let expression = cache ? ASTCache.get(source) : undefined;
25
+ if (!expression) {
26
+ expression = (0, _parse.parseAsEstreeExpression)(source);
27
+ if (cache) {
28
+ ASTCache.set(source, expression);
29
+ }
30
+ }
20
31
  const attemptToVisitGlobals = (0, _precook.precook)(expression, {
21
- ...options,
32
+ ...restOptions,
22
33
  expressionOnly: true
23
34
  });
24
35
  return {
@@ -38,4 +49,7 @@ function shouldAllowRecursiveEvaluations(raw) {
38
49
  function isTrackAll(raw) {
39
50
  return /^\s*<%=\s/.test(raw) && /\s%>\s*$/.test(raw);
40
51
  }
52
+ function clearExpressionASTCache() {
53
+ ASTCache.clear();
54
+ }
41
55
  //# sourceMappingURL=preevaluate.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"preevaluate.js","names":["_parse","require","_precook","preevaluate","raw","options","fixes","source","replace","m","push","expression","parseAsEstreeExpression","attemptToVisitGlobals","precook","expressionOnly","prefix","suffix","isEvaluable","test","shouldAllowRecursiveEvaluations","isTrackAll"],"sources":["../../src/preevaluate.ts"],"sourcesContent":["import { Expression } from \"@babel/types\";\nimport { parseAsEstreeExpression } from \"./parse.js\";\nimport { precook, PrecookOptions } from \"./precook.js\";\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 isTrackAll(raw: string): boolean {\n return /^\\s*<%=\\s/.test(raw) && /\\s%>\\s*$/.test(raw);\n}\n"],"mappings":";;;;;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAYA;AACO,SAASE,WAAWA,CACzBC,GAAW,EACXC,OAA4B,EACT;EACnB,MAAMC,KAAe,GAAG,EAAE;EAC1B,MAAMC,MAAM,GAAGH,GAAG,CAACI,OAAO,CAAC,yBAAyB,EAAGC,CAAC,IAAK;IAC3DH,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,WAAWA,CAACd,GAAW,EAAW;EAChD,OAAO,eAAe,CAACe,IAAI,CAACf,GAAG,CAAC,IAAI,UAAU,CAACe,IAAI,CAACf,GAAG,CAAC;AAC1D;AAEO,SAASgB,+BAA+BA,CAAChB,GAAW,EAAW;EACpE,OAAO,WAAW,CAACe,IAAI,CAACf,GAAG,CAAC;AAC9B;AAEO,SAASiB,UAAUA,CAACjB,GAAW,EAAW;EAC/C,OAAO,WAAW,CAACe,IAAI,CAACf,GAAG,CAAC,IAAI,UAAU,CAACe,IAAI,CAACf,GAAG,CAAC;AACtD","ignoreList":[]}
1
+ {"version":3,"file":"preevaluate.js","names":["_parse","require","_precook","ASTCache","Map","preevaluate","raw","cache","restOptions","fixes","source","replace","m","push","expression","get","undefined","parseAsEstreeExpression","set","attemptToVisitGlobals","precook","expressionOnly","prefix","suffix","isEvaluable","test","shouldAllowRecursiveEvaluations","isTrackAll","clearExpressionASTCache","clear"],"sources":["../../src/preevaluate.ts"],"sourcesContent":["import type { Expression } from \"@babel/types\";\nimport { parseAsEstreeExpression } from \"./parse.js\";\nimport { precook, PrecookOptions } from \"./precook.js\";\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 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,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,QAAA,GAAAD,OAAA;AAEA,MAAME,QAAQ,GAAG,IAAIC,GAAG,CAAqB,CAAC;AAc9C;AACO,SAASC,WAAWA,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,yBAAyB,EAAGC,CAAC,IAAK;IAC3DH,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,WAAWA,CAAClB,GAAW,EAAW;EAChD,OAAO,eAAe,CAACmB,IAAI,CAACnB,GAAG,CAAC,IAAI,UAAU,CAACmB,IAAI,CAACnB,GAAG,CAAC;AAC1D;AAEO,SAASoB,+BAA+BA,CAACpB,GAAW,EAAW;EACpE,OAAO,WAAW,CAACmB,IAAI,CAACnB,GAAG,CAAC;AAC9B;AAEO,SAASqB,UAAUA,CAACrB,GAAW,EAAW;EAC/C,OAAO,WAAW,CAACmB,IAAI,CAACnB,GAAG,CAAC,IAAI,UAAU,CAACmB,IAAI,CAACnB,GAAG,CAAC;AACtD;AAEO,SAASsB,uBAAuBA,CAAA,EAAS;EAC9CzB,QAAQ,CAAC0B,KAAK,CAAC,CAAC;AAClB","ignoreList":[]}
package/dist/esm/cook.js CHANGED
@@ -151,6 +151,8 @@ export function cook(rootAst, codeSource) {
151
151
  // Currently unicode flag is not fully supported across major browsers.
152
152
  throw new SyntaxError(`Unsupported unicode flag in regular expression: ${node.raw}`);
153
153
  }
154
+ // Always create a new RegExp, because the AST will be reused.
155
+ return NormalCompletion(new RegExp(node.regex.pattern, node.regex.flags));
154
156
  }
155
157
  return NormalCompletion(node.value);
156
158
  }