@next-core/cook 1.3.9 → 1.6.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/dist/esm/lint.js CHANGED
@@ -1,4 +1,4 @@
1
- import { parse } from "@babel/parser";
1
+ import { parseForAnalysis } from "./parse";
2
2
  import { precook } from "./precook";
3
3
 
4
4
  /** For next-core internal or devtools usage only. */
@@ -8,17 +8,11 @@ export function lint(source) {
8
8
  rules
9
9
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
10
10
  var errors = [];
11
- var file;
12
-
13
- try {
14
- file = parse(source, {
15
- plugins: ["estree", typescript && "typescript"].filter(Boolean),
16
- strictMode: true,
17
- attachComment: false,
18
- // Allow export/import declarations to make linter handle errors.
19
- sourceType: "unambiguous"
20
- });
21
- } catch (e) {
11
+ var file = typeof source === "string" ? parseForAnalysis(source, {
12
+ typescript
13
+ }) : source;
14
+
15
+ if (!file) {
22
16
  // Return no errors if parse failed.
23
17
  return errors;
24
18
  }
@@ -74,93 +68,100 @@ export function lint(source) {
74
68
  }
75
69
  });
76
70
  } else {
77
- var FunctionVisitor = node => {
78
- if (node.async || node.generator) {
79
- errors.push({
80
- type: "SyntaxError",
81
- message: "".concat(node.async ? "Async" : "Generator", " function is not allowed"),
82
- loc: node.loc
83
- });
84
- }
85
- };
86
-
87
71
  precook(func, {
88
- visitors: {
89
- ArrowFunctionExpression: FunctionVisitor,
90
- FunctionDeclaration: FunctionVisitor,
91
- FunctionExpression: FunctionVisitor,
92
-
93
- Literal(node) {
94
- if (node.regex) {
95
- if (node.value === null) {
96
- errors.push({
97
- type: "SyntaxError",
98
- message: "Invalid regular expression",
99
- loc: node.loc
100
- });
101
- } else if (node.regex.flags.includes("u")) {
102
- errors.push({
103
- type: "SyntaxError",
104
- message: "Unsupported unicode flag in regular expression",
105
- loc: node.loc
106
- });
107
- }
108
- }
109
- },
110
-
111
- ObjectExpression(node) {
112
- for (var prop of node.properties) {
113
- if (prop.type === "Property") {
114
- if (prop.kind !== "init") {
72
+ hooks: {
73
+ beforeVisit(node) {
74
+ switch (node.type) {
75
+ case "ArrowFunctionExpression":
76
+ case "FunctionDeclaration":
77
+ case "FunctionExpression":
78
+ if (node.async || node.generator) {
115
79
  errors.push({
116
80
  type: "SyntaxError",
117
- message: "Unsupported object getter/setter property",
118
- loc: prop.loc
81
+ message: "".concat(node.async ? "Async" : "Generator", " function is not allowed"),
82
+ loc: node.loc
119
83
  });
120
- } else if (!prop.computed && prop.key.type === "Identifier" && prop.key.name === "__proto__") {
84
+ }
85
+
86
+ break;
87
+
88
+ case "Literal":
89
+ if (node.regex) {
90
+ if (node.value === null) {
91
+ errors.push({
92
+ type: "SyntaxError",
93
+ message: "Invalid regular expression",
94
+ loc: node.loc
95
+ });
96
+ } else if (node.regex.flags.includes("u")) {
97
+ errors.push({
98
+ type: "SyntaxError",
99
+ message: "Unsupported unicode flag in regular expression",
100
+ loc: node.loc
101
+ });
102
+ }
103
+ }
104
+
105
+ break;
106
+
107
+ case "ObjectExpression":
108
+ for (var prop of node.properties) {
109
+ if (prop.type === "Property") {
110
+ if (prop.kind !== "init") {
111
+ errors.push({
112
+ type: "SyntaxError",
113
+ message: "Unsupported object getter/setter property",
114
+ loc: prop.loc
115
+ });
116
+ } else if (!prop.computed && prop.key.type === "Identifier" && prop.key.name === "__proto__") {
117
+ errors.push({
118
+ type: "TypeError",
119
+ message: "Setting '__proto__' property is not allowed",
120
+ loc: prop.key.loc
121
+ });
122
+ }
123
+ }
124
+ }
125
+
126
+ break;
127
+
128
+ case "VariableDeclaration":
129
+ if (node.kind === "var" && rules !== null && rules !== void 0 && rules.noVar) {
121
130
  errors.push({
122
- type: "TypeError",
123
- message: "Setting '__proto__' property is not allowed",
124
- loc: prop.key.loc
131
+ type: "SyntaxError",
132
+ message: "Var declaration is not recommended, use `let` or `const` instead",
133
+ loc: {
134
+ start: node.loc.start,
135
+ end: {
136
+ line: node.loc.end.line,
137
+ // Only decorate the "var".
138
+ column: node.loc.start.column + 3
139
+ }
140
+ }
125
141
  });
126
142
  }
127
- }
143
+
144
+ break;
128
145
  }
129
146
  },
130
147
 
131
- VariableDeclaration(node) {
132
- if (node.kind === "var" && rules !== null && rules !== void 0 && rules.noVar) {
148
+ beforeVisitGlobal(node) {
149
+ if (node.name === "arguments") {
133
150
  errors.push({
134
151
  type: "SyntaxError",
135
- message: "Var declaration is not recommended, use `let` or `const` instead",
136
- loc: {
137
- start: node.loc.start,
138
- end: {
139
- line: node.loc.end.line,
140
- // Only decorate the "var".
141
- column: node.loc.start.column + 3
142
- }
143
- }
152
+ message: "Use the rest parameters instead of 'arguments'",
153
+ loc: node.loc
144
154
  });
145
155
  }
146
156
  },
147
157
 
148
- __UnknownNode(node) {
158
+ beforeVisitUnknown(node) {
149
159
  errors.push({
150
160
  type: "SyntaxError",
151
161
  message: "Unsupported syntax: `".concat(node.type, "`"),
152
162
  loc: node.loc
153
163
  });
154
- },
155
-
156
- __GlobalVariable(node) {
157
- if (node.name === "arguments") {
158
- errors.push({
159
- type: "SyntaxError",
160
- message: "Use the rest parameters instead of 'arguments'",
161
- loc: node.loc
162
- });
163
- }
164
+ return true;
164
165
  }
165
166
 
166
167
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/lint.ts"],"names":["parse","precook","lint","source","typescript","rules","errors","file","plugins","filter","Boolean","strictMode","attachComment","sourceType","e","body","program","jsNodes","node","type","startsWith","test","push","message","loc","func","isFunctionDeclaration","unshift","start","line","column","end","FunctionVisitor","async","generator","visitors","ArrowFunctionExpression","FunctionDeclaration","FunctionExpression","Literal","regex","value","flags","includes","ObjectExpression","prop","properties","kind","computed","key","name","VariableDeclaration","noVar","__UnknownNode","__GlobalVariable"],"mappings":"AAAA,SAASA,KAAT,QAAiD,eAAjD;AAkBA,SAASC,OAAT,QAAwB,WAAxB;;AAaA;AACA,OAAO,SAASC,IAAT,CACLC,MADK,EAGQ;AAAA,MADb;AAAEC,IAAAA,UAAF;AAAc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AAAA,mBAAmB,CAACjC,IAAD,EAA4B;AAC7C,cAAIA,IAAI,CAAC6B,IAAL,KAAc,KAAd,IAAuB1C,KAAvB,aAAuBA,KAAvB,eAAuBA,KAAK,CAAE+C,KAAlC,EAAyC;AACvC9C,YAAAA,MAAM,CAACgB,IAAP,CAAY;AACVH,cAAAA,IAAI,EAAE,aADI;AAEVI,cAAAA,OAAO,EACL,kEAHQ;AAIVC,cAAAA,GAAG,EAAE;AACHI,gBAAAA,KAAK,EAAEV,IAAI,CAACM,GAAL,CAASI,KADb;AAEHG,gBAAAA,GAAG,EAAE;AACHF,kBAAAA,IAAI,EAAEX,IAAI,CAACM,GAAL,CAASO,GAAT,CAAaF,IADhB;AAEH;AACAC,kBAAAA,MAAM,EAAEZ,IAAI,CAACM,GAAL,CAASI,KAAT,CAAeE,MAAf,GAAwB;AAH7B;AAFF;AAJK,aAAZ;AAaD;AACF,SA5DO;;AA6DRuB,QAAAA,aAAa,CAACnC,IAAD,EAAmB;AAC9BZ,UAAAA,MAAM,CAACgB,IAAP,CAAY;AACVH,YAAAA,IAAI,EAAE,aADI;AAEVI,YAAAA,OAAO,iCAA2BL,IAAI,CAACC,IAAhC,MAFG;AAGVK,YAAAA,GAAG,EAAEN,IAAI,CAACM;AAHA,WAAZ;AAKD,SAnEO;;AAoER8B,QAAAA,gBAAgB,CAACpC,IAAD,EAAmB;AACjC,cAAIA,IAAI,CAACgC,IAAL,KAAc,WAAlB,EAA+B;AAC7B5C,YAAAA,MAAM,CAACgB,IAAP,CAAY;AACVH,cAAAA,IAAI,EAAE,aADI;AAEVI,cAAAA,OAAO,EAAE,gDAFC;AAGVC,cAAAA,GAAG,EAAEN,IAAI,CAACM;AAHA,aAAZ;AAKD;AACF;;AA5EO;AADE,KAAP,CAAP;AAgFD;;AACD,SAAOlB,MAAP;AACD","sourcesContent":["import { parse, ParseResult, ParserPlugin } from \"@babel/parser\";\nimport {\n ArrowFunctionExpression,\n File,\n FunctionDeclaration,\n FunctionExpression,\n Identifier,\n SourceLocation,\n Statement,\n VariableDeclaration,\n} from \"@babel/types\";\nimport {\n EstreeLiteral,\n CookRules,\n EstreeObjectExpression,\n EstreeVisitorFn,\n EstreeNode,\n} from \"./interfaces\";\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,\n { typescript, rules }: LintOptions = {}\n): LintError[] {\n const errors: LintError[] = [];\n let file: ParseResult<File>;\n try {\n file = 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 linter handle errors.\n sourceType: \"unambiguous\",\n });\n } catch (e) {\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 const FunctionVisitor: EstreeVisitorFn = (\n node: FunctionDeclaration | FunctionExpression | ArrowFunctionExpression\n ) => {\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 };\n precook(func, {\n visitors: {\n ArrowFunctionExpression: FunctionVisitor,\n FunctionDeclaration: FunctionVisitor,\n FunctionExpression: FunctionVisitor,\n Literal(node: EstreeLiteral) {\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 },\n ObjectExpression(node: EstreeObjectExpression) {\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 },\n VariableDeclaration(node: 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.end.line,\n // Only decorate the \"var\".\n column: node.loc.start.column + 3,\n },\n },\n });\n }\n },\n __UnknownNode(node: EstreeNode) {\n errors.push({\n type: \"SyntaxError\",\n message: `Unsupported syntax: \\`${node.type}\\``,\n loc: node.loc,\n });\n },\n __GlobalVariable(node: Identifier) {\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 },\n });\n }\n return errors;\n}\n"],"file":"lint.js"}
1
+ {"version":3,"sources":["../../src/lint.ts"],"names":["parseForAnalysis","precook","lint","source","typescript","rules","errors","file","body","program","jsNodes","node","type","startsWith","test","push","message","loc","func","isFunctionDeclaration","unshift","start","line","column","end","hooks","beforeVisit","async","generator","regex","value","flags","includes","prop","properties","kind","computed","key","name","noVar","beforeVisitGlobal","beforeVisitUnknown"],"mappings":"AAQA,SAASA,gBAAT,QAAiC,SAAjC;AACA,SAASC,OAAT,QAAwB,WAAxB;;AAaA;AACA,OAAO,SAASC,IAAT,CACLC,MADK,EAGQ;AAAA,MADb;AAAEC,IAAAA,UAAF;AAAcC,IAAAA;AAAd,GACa,uEADwB,EACxB;AACb,MAAMC,MAAmB,GAAG,EAA5B;AACA,MAAMC,IAAI,GACR,OAAOJ,MAAP,KAAkB,QAAlB,GACIH,gBAAgB,CAACG,MAAD,EAAS;AAAEC,IAAAA;AAAF,GAAT,CADpB,GAEID,MAHN;;AAIA,MAAI,CAACI,IAAL,EAAW;AACT;AACA,WAAOD,MAAP;AACD;;AACD,MAAME,IAAI,GAAGD,IAAI,CAACE,OAAL,CAAaD,IAA1B;AACA,MAAME,OAAoB,GAAGN,UAAU,GAAG,EAAH,GAAQI,IAA/C;;AACA,MAAIJ,UAAJ,EAAgB;AACd,SAAK,IAAMO,IAAX,IAAmBH,IAAnB,EAAyB;AACvB,UAAIG,IAAI,CAACC,IAAL,CAAUC,UAAV,CAAqB,IAArB,CAAJ,EAAgC;AAC9B,YAAI,qBAAqBC,IAArB,CAA0BH,IAAI,CAACC,IAA/B,CAAJ,EAA0C;AACxCN,UAAAA,MAAM,CAACS,IAAP,CAAY;AACVH,YAAAA,IAAI,EAAE,aADI;AAEVI,YAAAA,OAAO,4CAAsCL,IAAI,CAACC,IAA3C,MAFG;AAGVK,YAAAA,GAAG,EAAEN,IAAI,CAACM;AAHA,WAAZ;AAKD;AACF,OARD,MAQO;AACLP,QAAAA,OAAO,CAACK,IAAR,CAAaJ,IAAb;AACD;AACF;AACF;;AACD,MAAIO,IAAJ;;AACA,OAAK,IAAMP,KAAX,IAAmBD,OAAnB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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.end.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"],"file":"lint.js"}
package/dist/esm/parse.js CHANGED
@@ -41,4 +41,26 @@ export function parseAsEstree(source) {
41
41
 
42
42
  return jsNodes[0];
43
43
  }
44
+
45
+ /** For next-core internal or devtools usage only. */
46
+ export function parseForAnalysis(source) {
47
+ var {
48
+ typescript,
49
+ tokens
50
+ } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
51
+
52
+ try {
53
+ return parse(source, {
54
+ plugins: ["estree", typescript && "typescript"].filter(Boolean),
55
+ strictMode: true,
56
+ attachComment: false,
57
+ // Allow export/import declarations to make analyser handle errors.
58
+ sourceType: "unambiguous",
59
+ tokens
60
+ });
61
+ } catch (e) {
62
+ // Return no errors if parse failed.
63
+ return null;
64
+ }
65
+ }
44
66
  //# sourceMappingURL=parse.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/parse.ts"],"names":["parse","parseExpression","parseAsEstreeExpression","source","plugins","proposal","attachComment","parseAsEstree","typescript","file","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","push","length","map","join"],"mappings":"AAAA,SAASA,KAAT,EAAgBC,eAAhB,QAAqD,eAArD;AAGA,OAAO,SAASC,uBAAT,CAAiCC,MAAjC,EAA6D;AAClE,SAAOF,eAAe,CAACE,MAAD,EAAS;AAC7BC,IAAAA,OAAO,EAAE,CAAC,QAAD,EAAW,CAAC,kBAAD,EAAqB;AAAEC,MAAAA,QAAQ,EAAE;AAAZ,KAArB,CAAX,CADoB;AAE7BC,IAAAA,aAAa,EAAE;AAFc,GAAT,CAAtB;AAID;AAMD,OAAO,SAASC,aAAT,CACLJ,MADK,EAGgB;AAAA,MADrB;AAAEK,IAAAA;AAAF,GACqB,uEADgB,EAChB;AACrB,MAAMC,IAAI,GAAGT,KAAK,CAACG,MAAD,EAAS;AACzBC,IAAAA,OAAO,EAAE,CAAC,QAAD,EAAWI,UAAU,IAAI,YAAzB,EAAuCE,MAAvC,CACPC,OADO,CADgB;AAIzBC,IAAAA,UAAU,EAAE,IAJa;AAKzBN,IAAAA,aAAa,EAAE;AALU,GAAT,CAAlB;AAOA,MAAMO,IAAI,GAAGJ,IAAI,CAACK,OAAL,CAAaD,IAA1B;AACA,MAAME,OAAoB,GAAGP,UAAU,GAAG,EAAH,GAAQK,IAA/C;;AACA,MAAIL,UAAJ,EAAgB;AACd,SAAK,IAAMQ,IAAX,IAAmBH,IAAnB,EAAyB;AACvB,UAAIG,IAAI,CAACC,IAAL,CAAUC,UAAV,CAAqB,IAArB,CAAJ,EAAgC;AAC9B,YAAI,qBAAqBC,IAArB,CAA0BH,IAAI,CAACC,IAA/B,CAAJ,EAA0C;AACxC,gBAAM,IAAIG,WAAJ,0CAAkDJ,IAAI,CAACC,IAAvD,EAAN;AACD;AACF,OAJD,MAIO;AACLF,QAAAA,OAAO,CAACM,IAAR,CAAaL,IAAb;AACD;AACF;AACF;;AACD,MAAID,OAAO,CAACO,MAAR,KAAmB,CAAvB,EAA0B;AACxB,UAAM,IAAIF,WAAJ,CAAgB,gCAAhB,CAAN;AACD;;AACD,MAAIL,OAAO,CAACO,MAAR,GAAiB,CAAjB,IAAsBP,OAAO,CAAC,CAAD,CAAP,CAAWE,IAAX,KAAoB,qBAA9C,EAAqE;AACnE,UAAM,IAAIG,WAAJ,4EACgEL,OAAO,CACxEQ,GADiE,CAC5DP,IAAD,gBAAcA,IAAI,CAACC,IAAnB,OAD6D,EAEjEO,IAFiE,CAE5D,IAF4D,CADhE,EAAN;AAKD;;AACD,SAAOT,OAAO,CAAC,CAAD,CAAd;AACD","sourcesContent":["import { parse, parseExpression, ParserPlugin } from \"@babel/parser\";\nimport { Expression, 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"],"file":"parse.js"}
1
+ {"version":3,"sources":["../../src/parse.ts"],"names":["parse","parseExpression","parseAsEstreeExpression","source","plugins","proposal","attachComment","parseAsEstree","typescript","file","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","push","length","map","join","parseForAnalysis","tokens","sourceType","e"],"mappings":"AAAA,SACEA,KADF,EAEEC,eAFF,QAKO,eALP;AAQA,OAAO,SAASC,uBAAT,CAAiCC,MAAjC,EAA6D;AAClE,SAAOF,eAAe,CAACE,MAAD,EAAS;AAC7BC,IAAAA,OAAO,EAAE,CAAC,QAAD,EAAW,CAAC,kBAAD,EAAqB;AAAEC,MAAAA,QAAQ,EAAE;AAAZ,KAArB,CAAX,CADoB;AAE7BC,IAAAA,aAAa,EAAE;AAFc,GAAT,CAAtB;AAID;AAMD,OAAO,SAASC,aAAT,CACLJ,MADK,EAGgB;AAAA,MADrB;AAAEK,IAAAA;AAAF,GACqB,uEADgB,EAChB;AACrB,MAAMC,IAAI,GAAGT,KAAK,CAACG,MAAD,EAAS;AACzBC,IAAAA,OAAO,EAAE,CAAC,QAAD,EAAWI,UAAU,IAAI,YAAzB,EAAuCE,MAAvC,CACPC,OADO,CADgB;AAIzBC,IAAAA,UAAU,EAAE,IAJa;AAKzBN,IAAAA,aAAa,EAAE;AALU,GAAT,CAAlB;AAOA,MAAMO,IAAI,GAAGJ,IAAI,CAACK,OAAL,CAAaD,IAA1B;AACA,MAAME,OAAoB,GAAGP,UAAU,GAAG,EAAH,GAAQK,IAA/C;;AACA,MAAIL,UAAJ,EAAgB;AACd,S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\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"],"file":"parse.js"}