@next-core/cook 1.6.78 → 1.6.79

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.
Files changed (55) hide show
  1. package/dist/cjs/AnalysisContext.js +2 -11
  2. package/dist/cjs/AnalysisContext.js.map +1 -1
  3. package/dist/cjs/ExecutionContext.js +11 -37
  4. package/dist/cjs/ExecutionContext.js.map +1 -1
  5. package/dist/cjs/context-free.js +30 -71
  6. package/dist/cjs/context-free.js.map +1 -1
  7. package/dist/cjs/cook.js +147 -386
  8. package/dist/cjs/cook.js.map +1 -1
  9. package/dist/cjs/hasOwnProperty.js +0 -1
  10. package/dist/cjs/hasOwnProperty.js.map +1 -1
  11. package/dist/cjs/index.js +0 -14
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/interfaces.js.map +1 -1
  14. package/dist/cjs/lint.js +0 -21
  15. package/dist/cjs/lint.js.map +1 -1
  16. package/dist/cjs/parse.js +0 -8
  17. package/dist/cjs/parse.js.map +1 -1
  18. package/dist/cjs/precook.js +6 -74
  19. package/dist/cjs/precook.js.map +1 -1
  20. package/dist/cjs/precookFunction.js +0 -3
  21. package/dist/cjs/precookFunction.js.map +1 -1
  22. package/dist/cjs/preevaluate.js +2 -6
  23. package/dist/cjs/preevaluate.js.map +1 -1
  24. package/dist/cjs/sanitize.js +9 -13
  25. package/dist/cjs/sanitize.js.map +1 -1
  26. package/dist/cjs/traverse.js +0 -32
  27. package/dist/cjs/traverse.js.map +1 -1
  28. package/dist/esm/AnalysisContext.js +2 -8
  29. package/dist/esm/AnalysisContext.js.map +1 -1
  30. package/dist/esm/ExecutionContext.js +11 -30
  31. package/dist/esm/ExecutionContext.js.map +1 -1
  32. package/dist/esm/context-free.js +32 -53
  33. package/dist/esm/context-free.js.map +1 -1
  34. package/dist/esm/cook.js +145 -411
  35. package/dist/esm/cook.js.map +1 -1
  36. package/dist/esm/hasOwnProperty.js.map +1 -1
  37. package/dist/esm/index.js.map +1 -1
  38. package/dist/esm/interfaces.js.map +1 -1
  39. package/dist/esm/lint.js +0 -19
  40. package/dist/esm/lint.js.map +1 -1
  41. package/dist/esm/parse.js +0 -6
  42. package/dist/esm/parse.js.map +1 -1
  43. package/dist/esm/precook.js +4 -82
  44. package/dist/esm/precook.js.map +1 -1
  45. package/dist/esm/precookFunction.js +4 -5
  46. package/dist/esm/precookFunction.js.map +1 -1
  47. package/dist/esm/preevaluate.js.map +1 -1
  48. package/dist/esm/sanitize.js +9 -9
  49. package/dist/esm/sanitize.js.map +1 -1
  50. package/dist/esm/traverse.js +0 -29
  51. package/dist/esm/traverse.js.map +1 -1
  52. package/dist/types/interfaces.d.ts +2 -0
  53. package/dist/types/lint.d.ts +3 -4
  54. package/dist/types/parse.d.ts +3 -3
  55. package/package.json +6 -6
package/dist/cjs/index.js CHANGED
@@ -19,9 +19,7 @@ Object.defineProperty(exports, "tokTypes", {
19
19
  return _parser.tokTypes;
20
20
  }
21
21
  });
22
-
23
22
  var _cook = require("./cook");
24
-
25
23
  Object.keys(_cook).forEach(function (key) {
26
24
  if (key === "default" || key === "__esModule") return;
27
25
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -33,9 +31,7 @@ Object.keys(_cook).forEach(function (key) {
33
31
  }
34
32
  });
35
33
  });
36
-
37
34
  var _lint = require("./lint");
38
-
39
35
  Object.keys(_lint).forEach(function (key) {
40
36
  if (key === "default" || key === "__esModule") return;
41
37
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -47,11 +43,8 @@ Object.keys(_lint).forEach(function (key) {
47
43
  }
48
44
  });
49
45
  });
50
-
51
46
  var _parse = require("./parse");
52
-
53
47
  var _precook = require("./precook");
54
-
55
48
  Object.keys(_precook).forEach(function (key) {
56
49
  if (key === "default" || key === "__esModule") return;
57
50
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -63,9 +56,7 @@ Object.keys(_precook).forEach(function (key) {
63
56
  }
64
57
  });
65
58
  });
66
-
67
59
  var _precookFunction = require("./precookFunction");
68
-
69
60
  Object.keys(_precookFunction).forEach(function (key) {
70
61
  if (key === "default" || key === "__esModule") return;
71
62
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -77,9 +68,7 @@ Object.keys(_precookFunction).forEach(function (key) {
77
68
  }
78
69
  });
79
70
  });
80
-
81
71
  var _preevaluate = require("./preevaluate");
82
-
83
72
  Object.keys(_preevaluate).forEach(function (key) {
84
73
  if (key === "default" || key === "__esModule") return;
85
74
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -91,9 +80,7 @@ Object.keys(_preevaluate).forEach(function (key) {
91
80
  }
92
81
  });
93
82
  });
94
-
95
83
  var _interfaces = require("./interfaces");
96
-
97
84
  Object.keys(_interfaces).forEach(function (key) {
98
85
  if (key === "default" || key === "__esModule") return;
99
86
  if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
@@ -105,6 +92,5 @@ Object.keys(_interfaces).forEach(function (key) {
105
92
  }
106
93
  });
107
94
  });
108
-
109
95
  var _parser = require("@babel/parser");
110
96
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["export * from \"./cook\";\nexport * from \"./lint\";\nexport { parseForAnalysis } from \"./parse\";\nexport * from \"./precook\";\nexport * from \"./precookFunction\";\nexport * from \"./preevaluate\";\nexport * from \"./interfaces\";\nexport { tokTypes } from \"@babel/parser\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA;;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;;AACA"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["export * from \"./cook\";\nexport * from \"./lint\";\nexport { parseForAnalysis } from \"./parse\";\nexport * from \"./precook\";\nexport * from \"./precookFunction\";\nexport * from \"./preevaluate\";\nexport * from \"./interfaces\";\nexport { tokTypes } from \"@babel/parser\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AACA;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA;AAAA;EAAA;EAAA;EAAA;EAAA;IAAA;IAAA;MAAA;IAAA;EAAA;AAAA;AACA"}
@@ -1 +1 @@
1
- {"version":3,"file":"interfaces.js","names":[],"sources":["../../src/interfaces.ts"],"sourcesContent":["import {\n Expression,\n FunctionDeclaration,\n LVal,\n Node,\n ObjectExpression,\n ObjectPattern,\n ObjectProperty,\n RestElement,\n SourceLocation,\n SpreadElement,\n VariableDeclaration,\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};\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 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"],"mappings":""}
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} 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};\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 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":""}
package/dist/cjs/lint.js CHANGED
@@ -4,11 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.lint = lint;
7
-
8
7
  var _parse = require("./parse");
9
-
10
8
  var _precook = require("./precook");
11
-
12
9
  /** For next-core internal or devtools usage only. */
13
10
  function lint(source, {
14
11
  typescript,
@@ -18,15 +15,12 @@ function lint(source, {
18
15
  const file = typeof source === "string" ? (0, _parse.parseForAnalysis)(source, {
19
16
  typescript
20
17
  }) : source;
21
-
22
18
  if (!file) {
23
19
  // Return no errors if parse failed.
24
20
  return errors;
25
21
  }
26
-
27
22
  const body = file.program.body;
28
23
  const jsNodes = typescript ? [] : body;
29
-
30
24
  if (typescript) {
31
25
  for (const node of body) {
32
26
  if (node.type.startsWith("TS")) {
@@ -42,12 +36,9 @@ function lint(source, {
42
36
  }
43
37
  }
44
38
  }
45
-
46
39
  let func;
47
-
48
40
  for (const node of jsNodes) {
49
41
  const isFunctionDeclaration = node.type === "FunctionDeclaration";
50
-
51
42
  if (isFunctionDeclaration && !func) {
52
43
  func = node;
53
44
  } else {
@@ -58,7 +49,6 @@ function lint(source, {
58
49
  });
59
50
  }
60
51
  }
61
-
62
52
  if (!func) {
63
53
  errors.unshift({
64
54
  type: "SyntaxError",
@@ -89,9 +79,7 @@ function lint(source, {
89
79
  loc: node.loc
90
80
  });
91
81
  }
92
-
93
82
  break;
94
-
95
83
  case "Literal":
96
84
  if (node.regex) {
97
85
  if (node.value === null) {
@@ -108,9 +96,7 @@ function lint(source, {
108
96
  });
109
97
  }
110
98
  }
111
-
112
99
  break;
113
-
114
100
  case "ObjectExpression":
115
101
  for (const prop of node.properties) {
116
102
  if (prop.type === "Property") {
@@ -129,9 +115,7 @@ function lint(source, {
129
115
  }
130
116
  }
131
117
  }
132
-
133
118
  break;
134
-
135
119
  case "VariableDeclaration":
136
120
  if (node.kind === "var" && rules !== null && rules !== void 0 && rules.noVar) {
137
121
  errors.push({
@@ -147,11 +131,9 @@ function lint(source, {
147
131
  }
148
132
  });
149
133
  }
150
-
151
134
  break;
152
135
  }
153
136
  },
154
-
155
137
  beforeVisitGlobal(node) {
156
138
  if (node.name === "arguments") {
157
139
  errors.push({
@@ -161,7 +143,6 @@ function lint(source, {
161
143
  });
162
144
  }
163
145
  },
164
-
165
146
  beforeVisitUnknown(node) {
166
147
  errors.push({
167
148
  type: "SyntaxError",
@@ -170,11 +151,9 @@ function lint(source, {
170
151
  });
171
152
  return true;
172
153
  }
173
-
174
154
  }
175
155
  });
176
156
  }
177
-
178
157
  return errors;
179
158
  }
180
159
  //# sourceMappingURL=lint.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"lint.js","names":["lint","source","typescript","rules","errors","file","parseForAnalysis","body","program","jsNodes","node","type","startsWith","test","push","message","loc","func","isFunctionDeclaration","unshift","start","line","column","end","precook","hooks","beforeVisit","async","generator","regex","value","flags","includes","prop","properties","kind","computed","key","name","noVar","beforeVisitGlobal","beforeVisitUnknown"],"sources":["../../src/lint.ts"],"sourcesContent":["import { ParseResult } from \"@babel/parser\";\nimport {\n File,\n FunctionDeclaration,\n SourceLocation,\n Statement,\n} from \"@babel/types\";\nimport { CookRules } from \"./interfaces\";\nimport { parseForAnalysis } from \"./parse\";\nimport { precook } from \"./precook\";\n\nexport interface LintOptions {\n typescript?: boolean;\n rules?: CookRules;\n}\n\nexport interface LintError {\n type: \"SyntaxError\" | \"TypeError\";\n message: string;\n loc: SourceLocation;\n}\n\n/** For next-core internal or devtools usage only. */\nexport function lint(\n source: string | ParseResult<File>,\n { typescript, rules }: LintOptions = {}\n): LintError[] {\n const errors: LintError[] = [];\n const file =\n typeof source === \"string\"\n ? parseForAnalysis(source, { typescript })\n : source;\n if (!file) {\n // Return no errors if parse failed.\n return errors;\n }\n const body = file.program.body;\n const jsNodes: Statement[] = typescript ? [] : body;\n if (typescript) {\n for (const node of body) {\n if (node.type.startsWith(\"TS\")) {\n if (/Enum|Import|Export/.test(node.type)) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported TypeScript syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n }\n } else {\n jsNodes.push(node);\n }\n }\n }\n let func: FunctionDeclaration;\n for (const node of jsNodes) {\n const isFunctionDeclaration = node.type === \"FunctionDeclaration\";\n if (isFunctionDeclaration && !func) {\n func = node;\n } else {\n errors.push({\n type: \"SyntaxError\",\n message: isFunctionDeclaration\n ? \"Expect a single function declaration\"\n : `\\`${node.type}\\` is not allowed in top level`,\n loc: node.loc,\n });\n }\n }\n if (!func) {\n errors.unshift({\n type: \"SyntaxError\",\n message: \"Function declaration not found\",\n loc: {\n start: { line: 1, column: 0 },\n end: { line: 1, column: 0 },\n },\n });\n } else {\n precook(func, {\n hooks: {\n beforeVisit(node) {\n switch (node.type) {\n case \"ArrowFunctionExpression\":\n case \"FunctionDeclaration\":\n case \"FunctionExpression\":\n if (node.async || node.generator) {\n errors.push({\n type: \"SyntaxError\",\n message: `${\n node.async ? \"Async\" : \"Generator\"\n } function is not allowed`,\n loc: node.loc,\n });\n }\n break;\n case \"Literal\":\n if (node.regex) {\n if (node.value === null) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Invalid regular expression\",\n loc: node.loc,\n });\n } else if (node.regex.flags.includes(\"u\")) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported unicode flag in regular expression\",\n loc: node.loc,\n });\n }\n }\n break;\n case \"ObjectExpression\":\n for (const prop of node.properties) {\n if (prop.type === \"Property\") {\n if (prop.kind !== \"init\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported object getter/setter property\",\n loc: prop.loc,\n });\n } else if (\n !prop.computed &&\n prop.key.type === \"Identifier\" &&\n prop.key.name === \"__proto__\"\n ) {\n errors.push({\n type: \"TypeError\",\n message: \"Setting '__proto__' property is not allowed\",\n loc: prop.key.loc,\n });\n }\n }\n }\n break;\n case \"VariableDeclaration\":\n if (node.kind === \"var\" && rules?.noVar) {\n errors.push({\n type: \"SyntaxError\",\n message:\n \"Var declaration is not recommended, use `let` or `const` instead\",\n loc: {\n start: node.loc.start,\n end: {\n line: node.loc.start.line,\n // Only decorate the \"var\".\n column: node.loc.start.column + 3,\n },\n },\n });\n }\n break;\n }\n },\n beforeVisitGlobal(node) {\n if (node.name === \"arguments\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Use the rest parameters instead of 'arguments'\",\n loc: node.loc,\n });\n }\n },\n beforeVisitUnknown(node) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n return true;\n },\n },\n });\n }\n return errors;\n}\n"],"mappings":";;;;;;;AAQA;;AACA;;AAaA;AACO,SAASA,IAAT,CACLC,MADK,EAEL;EAAEC,UAAF;EAAcC;AAAd,IAAqC,EAFhC,EAGQ;EACb,MAAMC,MAAmB,GAAG,EAA5B;EACA,MAAMC,IAAI,GACR,OAAOJ,MAAP,KAAkB,QAAlB,GACI,IAAAK,uBAAA,EAAiBL,MAAjB,EAAyB;IAAEC;EAAF,CAAzB,CADJ,GAEID,MAHN;;EAIA,IAAI,CAACI,IAAL,EAAW;IACT;IACA,OAAOD,MAAP;EACD;;EACD,MAAMG,IAAI,GAAGF,IAAI,CAACG,OAAL,CAAaD,IAA1B;EACA,MAAME,OAAoB,GAAGP,UAAU,GAAG,EAAH,GAAQK,IAA/C;;EACA,IAAIL,UAAJ,EAAgB;IACd,KAAK,MAAMQ,IAAX,IAAmBH,IAAnB,EAAyB;MACvB,IAAIG,IAAI,CAACC,IAAL,CAAUC,UAAV,CAAqB,IAArB,C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","file","parseForAnalysis","body","program","jsNodes","node","type","startsWith","test","push","message","loc","func","isFunctionDeclaration","unshift","start","line","column","end","precook","hooks","beforeVisit","async","generator","regex","value","flags","includes","prop","properties","kind","computed","key","name","noVar","beforeVisitGlobal","beforeVisitUnknown"],"sources":["../../src/lint.ts"],"sourcesContent":["import {\n type FunctionDeclaration,\n SourceLocation,\n Statement,\n} from \"@babel/types\";\nimport type { CookRules, ParseResultOfFile } from \"./interfaces\";\nimport { parseForAnalysis } from \"./parse\";\nimport { precook } from \"./precook\";\n\nexport interface LintOptions {\n typescript?: boolean;\n rules?: CookRules;\n}\n\nexport interface LintError {\n type: \"SyntaxError\" | \"TypeError\";\n message: string;\n loc: SourceLocation;\n}\n\n/** For next-core internal or devtools usage only. */\nexport function lint(\n source: string | ParseResultOfFile,\n { typescript, rules }: LintOptions = {}\n): LintError[] {\n const errors: LintError[] = [];\n const file =\n typeof source === \"string\"\n ? parseForAnalysis(source, { typescript })\n : source;\n if (!file) {\n // Return no errors if parse failed.\n return errors;\n }\n const body = file.program.body;\n const jsNodes: Statement[] = typescript ? [] : body;\n if (typescript) {\n for (const node of body) {\n if (node.type.startsWith(\"TS\")) {\n if (/Enum|Import|Export/.test(node.type)) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported TypeScript syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n }\n } else {\n jsNodes.push(node);\n }\n }\n }\n let func: FunctionDeclaration;\n for (const node of jsNodes) {\n const isFunctionDeclaration = node.type === \"FunctionDeclaration\";\n if (isFunctionDeclaration && !func) {\n func = node;\n } else {\n errors.push({\n type: \"SyntaxError\",\n message: isFunctionDeclaration\n ? \"Expect a single function declaration\"\n : `\\`${node.type}\\` is not allowed in top level`,\n loc: node.loc,\n });\n }\n }\n if (!func) {\n errors.unshift({\n type: \"SyntaxError\",\n message: \"Function declaration not found\",\n loc: {\n start: { line: 1, column: 0 },\n end: { line: 1, column: 0 },\n },\n });\n } else {\n precook(func, {\n hooks: {\n beforeVisit(node) {\n switch (node.type) {\n case \"ArrowFunctionExpression\":\n case \"FunctionDeclaration\":\n case \"FunctionExpression\":\n if (node.async || node.generator) {\n errors.push({\n type: \"SyntaxError\",\n message: `${\n node.async ? \"Async\" : \"Generator\"\n } function is not allowed`,\n loc: node.loc,\n });\n }\n break;\n case \"Literal\":\n if (node.regex) {\n if (node.value === null) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Invalid regular expression\",\n loc: node.loc,\n });\n } else if (node.regex.flags.includes(\"u\")) {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported unicode flag in regular expression\",\n loc: node.loc,\n });\n }\n }\n break;\n case \"ObjectExpression\":\n for (const prop of node.properties) {\n if (prop.type === \"Property\") {\n if (prop.kind !== \"init\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Unsupported object getter/setter property\",\n loc: prop.loc,\n });\n } else if (\n !prop.computed &&\n prop.key.type === \"Identifier\" &&\n prop.key.name === \"__proto__\"\n ) {\n errors.push({\n type: \"TypeError\",\n message: \"Setting '__proto__' property is not allowed\",\n loc: prop.key.loc,\n });\n }\n }\n }\n break;\n case \"VariableDeclaration\":\n if (node.kind === \"var\" && rules?.noVar) {\n errors.push({\n type: \"SyntaxError\",\n message:\n \"Var declaration is not recommended, use `let` or `const` instead\",\n loc: {\n start: node.loc.start,\n end: {\n line: node.loc.start.line,\n // Only decorate the \"var\".\n column: node.loc.start.column + 3,\n },\n },\n });\n }\n break;\n }\n },\n beforeVisitGlobal(node) {\n if (node.name === \"arguments\") {\n errors.push({\n type: \"SyntaxError\",\n message: \"Use the rest parameters instead of 'arguments'\",\n loc: node.loc,\n });\n }\n },\n beforeVisitUnknown(node) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n return true;\n },\n },\n });\n }\n return errors;\n}\n"],"mappings":";;;;;;AAMA;AACA;AAaA;AACO,SAASA,IAAI,CAClBC,MAAkC,EAClC;EAAEC,UAAU;E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,IAAI,EAAE;UACtB,IAAIA,IAAI,CAAC4B,IAAI,KAAK,WAAW,EAAE;YAC7BlC,MAAM,CAACU,IAAI,CAAC;cACVH,IAAI,EAAE,aAAa;cACnBI,OAAO,EAAE,gDAAgD;cACzDC,GAAG,EAAEN,IAAI,CAACM;YACZ,CAAC,CAAC;UACJ;QACF,CAAC;QACDyB,kBAAkB,CAAC/B,IAAI,EAAE;UACvBN,MAAM,CAACU,IAAI,CAAC;YACVH,IAAI,EAAE,aAAa;YACnBI,OAAO,EAAG,yBAAwBL,IAAI,CAACC,IAAK,IAAG;YAC/CK,GAAG,EAAEN,IAAI,CAACM;UACZ,CAAC,CAAC;UACF,OAAO,IAAI;QACb;MACF;IACF,CAAC,CAAC;EACJ;EACA,OAAOZ,MAAM;AACf"}
package/dist/cjs/parse.js CHANGED
@@ -6,9 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.parseAsEstree = parseAsEstree;
7
7
  exports.parseAsEstreeExpression = parseAsEstreeExpression;
8
8
  exports.parseForAnalysis = parseForAnalysis;
9
-
10
9
  var _parser = require("@babel/parser");
11
-
12
10
  function parseAsEstreeExpression(source) {
13
11
  return (0, _parser.parseExpression)(source, {
14
12
  plugins: ["estree", ["pipelineOperator", {
@@ -17,7 +15,6 @@ function parseAsEstreeExpression(source) {
17
15
  attachComment: false
18
16
  });
19
17
  }
20
-
21
18
  function parseAsEstree(source, {
22
19
  typescript
23
20
  } = {}) {
@@ -28,7 +25,6 @@ function parseAsEstree(source, {
28
25
  });
29
26
  const body = file.program.body;
30
27
  const jsNodes = typescript ? [] : body;
31
-
32
28
  if (typescript) {
33
29
  for (const node of body) {
34
30
  if (node.type.startsWith("TS")) {
@@ -40,18 +36,14 @@ function parseAsEstree(source, {
40
36
  }
41
37
  }
42
38
  }
43
-
44
39
  if (jsNodes.length === 0) {
45
40
  throw new SyntaxError("Function declaration not found");
46
41
  }
47
-
48
42
  if (jsNodes.length > 1 || jsNodes[0].type !== "FunctionDeclaration") {
49
43
  throw new SyntaxError(`Expect a single function declaration at top level, but received: ${jsNodes.map(node => `"${node.type}"`).join(", ")}`);
50
44
  }
51
-
52
45
  return jsNodes[0];
53
46
  }
54
-
55
47
  /** For next-core internal or devtools usage only. */
56
48
  function parseForAnalysis(source, {
57
49
  typescript,
@@ -1 +1 @@
1
- {"version":3,"file":"parse.js","names":["parseAsEstreeExpression","source","parseExpression","plugins","proposal","attachComment","parseAsEstree","typescript","file","parse","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","push","length","map","join","parseForAnalysis","tokens","sourceType","e"],"sources":["../../src/parse.ts"],"sourcesContent":["import {\n parse,\n parseExpression,\n ParseResult,\n ParserPlugin,\n} from \"@babel/parser\";\nimport { Expression, File, FunctionDeclaration, Statement } from \"@babel/types\";\n\nexport function parseAsEstreeExpression(source: string): Expression {\n return parseExpression(source, {\n plugins: [\"estree\", [\"pipelineOperator\", { proposal: \"minimal\" }]],\n attachComment: false,\n });\n}\n\nexport interface ParseEstreeOptions {\n typescript?: boolean;\n}\n\nexport function parseAsEstree(\n source: string,\n { typescript }: ParseEstreeOptions = {}\n): FunctionDeclaration {\n const file = parse(source, {\n plugins: [\"estree\", typescript && \"typescript\"].filter(\n Boolean\n ) as ParserPlugin[],\n strictMode: true,\n attachComment: false,\n });\n const body = file.program.body;\n const jsNodes: Statement[] = typescript ? [] : body;\n if (typescript) {\n for (const node of body) {\n if (node.type.startsWith(\"TS\")) {\n if (/Enum|Import|Export/.test(node.type)) {\n throw new SyntaxError(`Unsupported TypeScript syntax: ${node.type}`);\n }\n } else {\n jsNodes.push(node);\n }\n }\n }\n if (jsNodes.length === 0) {\n throw new SyntaxError(\"Function declaration not found\");\n }\n if (jsNodes.length > 1 || jsNodes[0].type !== \"FunctionDeclaration\") {\n throw new SyntaxError(\n `Expect a single function declaration at top level, but received: ${jsNodes\n .map((node) => `\"${node.type}\"`)\n .join(\", \")}`\n );\n }\n return jsNodes[0] as FunctionDeclaration;\n}\n\nexport interface AnalysisOptions {\n typescript?: boolean;\n tokens?: boolean;\n}\n\n/** For next-core internal or devtools usage only. */\nexport function parseForAnalysis(\n source: string,\n { typescript, tokens }: AnalysisOptions = {}\n): ParseResult<File> {\n try {\n return parse(source, {\n plugins: [\"estree\", typescript && \"typescript\"].filter(\n Boolean\n ) as ParserPlugin[],\n strictMode: true,\n attachComment: false,\n // Allow export/import declarations to make analyser handle errors.\n sourceType: \"unambiguous\",\n tokens,\n });\n } catch (e) {\n // Return no errors if parse failed.\n return null;\n }\n}\n"],"mappings":";;;;;;;;;AAAA;;AAQO,SAASA,uBAAT,CAAiCC,MAAjC,EAA6D;EAClE,OAAO,IAAAC,uBAAA,EAAgBD,MAAhB,EAAwB;IAC7BE,OAAO,EAAE,CAAC,QAAD,EAAW,CAAC,kBAAD,EAAqB;MAAEC,QAAQ,EAAE;IAAZ,CAArB,CAAX,CADoB;IAE7BC,aAAa,EAAE;EAFc,CAAxB,CAAP;AAID;;AAMM,SAASC,aAAT,CACLL,MADK,EAEL;EAAEM;AAAF,IAAqC,EAFhC,EAGgB;EACrB,MAAMC,IAAI,GAAG,IAAAC,aAAA,EAAMR,MAAN,EAAc;IACzBE,OAAO,EAAE,CAAC,QAAD,EAAWI,UAAU,IAAI,YAAzB,EAAuCG,MAAvC,CACPC,OADO,CADgB;IAIzBC,UAAU,EAAE,IAJa;IAKzBP,aAAa,EAAE;EALU,CAAd,CAAb;EAOA,MAAMQ,IAAI,GAAGL,IAAI,CAACM,OAAL,CAAaD,IAA1B;EACA,MAAME,OAAoB,GAAGR,UAAU,GAAG,EAAH,GAAQM,IAA/C;;EACA,IAAIN,UAAJ,EAAgB;IACd,KAAK,MAAMS,IAAX,IAAmBH,IAAnB,EAAyB;MACvB,IAAIG,IAAI,CAACC,IAAL,CAAUC,UAAV,CAAqB,IAArB,CAAJ,EAAgC;QAC9B,IAAI,qBAAqBC,IAArB,CAA0BH,IAAI,CAACC,IAA/B,CAAJ,EAA0C;UACxC,MAAM,IAAIG,WAAJ,CAAiB,kCAAiCJ,IAAI,CAACC,IAAK,EAA5D,CAAN;QACD;MACF,CAJD,MAIO;QACLF,OAAO,CAACM,IAAR,CAAaL,IAAb;MACD;IACF;EACF;;EACD,IAAID,OAAO,CAACO,MAAR,KAAmB,CAAvB,EAA0B;IACxB,MAAM,IAAIF,WAAJ,CAAgB,gCAAhB,CAAN;EACD;;EACD,IAAIL,OAAO,CAACO,MAAR,GAAiB,CAAjB,IAAsBP,OAAO,CAAC,CAAD,CAAP,CAAWE,IAAX,KAAoB,qBAA9C,EAAqE;IACnE,MAAM,IAAIG,WAAJ,CACH,oEAAmEL,OAAO,CACxEQ,GADiE,CAC5DP,IAAD,IAAW,IAAGA,IAAI,CAACC,IAAK,GADqC,EAEjEO,IAFiE,CAE5D,IAF4D,CAEtD,EAHV,CAAN;EAKD;;EACD,OAAOT,OAAO,CAAC,CAAD,CAAd;AACD;;AAOD;AACO,SAASU,gBAAT,CACLxB,MADK,EAEL;EAAEM,UAAF;EAAcmB;AAAd,IAA0C,EAFrC,EAGc;EACnB,IAAI;IACF,OAAO,IAAAjB,aAAA,EAAMR,MAAN,EAAc;MACnBE,OAAO,EAAE,CAAC,QAAD,EAAWI,UAAU,IAAI,YAAzB,EAAuCG,MAAvC,CACPC,OADO,CADU;MAInBC,UAAU,EAAE,IAJO;MAKnBP,aAAa,EAAE,KALI;MAMnB;MACAsB,UAAU,EAAE,aAPO;MAQnBD;IARmB,CAAd,CAAP;EAUD,CAXD,CAWE,OAAOE,CAAP,EAAU;IACV;IACA,OAAO,IAAP;EACD;AACF"}
1
+ {"version":3,"file":"parse.js","names":["parseAsEstreeExpression","source","parseExpression","plugins","proposal","attachComment","parseAsEstree","typescript","file","parse","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","push","length","map","join","parseForAnalysis","tokens","sourceType","e"],"sources":["../../src/parse.ts"],"sourcesContent":["import { parse, parseExpression, type ParserPlugin } from \"@babel/parser\";\nimport type { Expression, FunctionDeclaration, Statement } from \"@babel/types\";\nimport type { ParseResultOfFile } from \"./interfaces\";\n\nexport function parseAsEstreeExpression(source: string): Expression {\n return parseExpression(source, {\n plugins: [\"estree\", [\"pipelineOperator\", { proposal: \"minimal\" }]],\n attachComment: false,\n });\n}\n\nexport interface ParseEstreeOptions {\n typescript?: boolean;\n}\n\nexport function parseAsEstree(\n source: string,\n { typescript }: ParseEstreeOptions = {}\n): FunctionDeclaration {\n const file = parse(source, {\n plugins: [\"estree\", typescript && \"typescript\"].filter(\n Boolean\n ) as ParserPlugin[],\n strictMode: true,\n attachComment: false,\n });\n const body = file.program.body;\n const jsNodes: Statement[] = typescript ? [] : body;\n if (typescript) {\n for (const node of body) {\n if (node.type.startsWith(\"TS\")) {\n if (/Enum|Import|Export/.test(node.type)) {\n throw new SyntaxError(`Unsupported TypeScript syntax: ${node.type}`);\n }\n } else {\n jsNodes.push(node);\n }\n }\n }\n if (jsNodes.length === 0) {\n throw new SyntaxError(\"Function declaration not found\");\n }\n if (jsNodes.length > 1 || jsNodes[0].type !== \"FunctionDeclaration\") {\n throw new SyntaxError(\n `Expect a single function declaration at top level, but received: ${jsNodes\n .map((node) => `\"${node.type}\"`)\n .join(\", \")}`\n );\n }\n return jsNodes[0] as FunctionDeclaration;\n}\n\nexport interface AnalysisOptions {\n typescript?: boolean;\n tokens?: boolean;\n}\n\n/** For next-core internal or devtools usage only. */\nexport function parseForAnalysis(\n source: string,\n { typescript, tokens }: AnalysisOptions = {}\n): ParseResultOfFile {\n try {\n return parse(source, {\n plugins: [\"estree\", typescript && \"typescript\"].filter(\n Boolean\n ) as ParserPlugin[],\n strictMode: true,\n attachComment: false,\n // Allow export/import declarations to make analyser handle errors.\n sourceType: \"unambiguous\",\n tokens,\n });\n } catch (e) {\n // Return no errors if parse failed.\n return null;\n }\n}\n"],"mappings":";;;;;;;;AAAA;AAIO,SAASA,uBAAuB,CAACC,MAAc,EAAc;EAClE,OAAO,IAAAC,uBAAe,EAACD,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