@next-core/cook 2.2.16 → 2.2.17
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/cjs/ExecutionContext.js.map +1 -1
- package/dist/cjs/context-free.js.map +1 -1
- package/dist/cjs/cook.js +1 -1
- package/dist/cjs/cook.js.map +1 -1
- package/dist/cjs/lint.js.map +1 -1
- package/dist/cjs/parse.js.map +1 -1
- package/dist/cjs/precook.js.map +1 -1
- package/dist/esm/ExecutionContext.js +12 -12
- package/dist/esm/ExecutionContext.js.map +1 -1
- package/dist/esm/context-free.js +13 -13
- package/dist/esm/context-free.js.map +1 -1
- package/dist/esm/cook.js +367 -366
- package/dist/esm/cook.js.map +1 -1
- package/dist/esm/lint.js +16 -16
- package/dist/esm/lint.js.map +1 -1
- package/dist/esm/parse.js +7 -7
- package/dist/esm/parse.js.map +1 -1
- package/dist/esm/parseForAnalysis.js +1 -1
- package/dist/esm/precook.js +70 -70
- package/dist/esm/precook.js.map +1 -1
- package/dist/esm/precookFunction.js +6 -9
- package/dist/esm/precookFunction.js.map +1 -1
- package/dist/esm/preevaluate.js +6 -6
- package/dist/esm/preevaluate.js.map +1 -1
- package/dist/esm/sanitize.js +2 -2
- package/dist/esm/sanitize.js.map +1 -1
- package/dist/esm/traverse.js +8 -8
- package/dist/esm/traverse.js.map +1 -1
- package/package.json +6 -6
package/dist/esm/lint.js
CHANGED
|
@@ -4,27 +4,27 @@ import { parseForAnalysis } from "./parseForAnalysis.js";
|
|
|
4
4
|
import { precook } from "./precook.js";
|
|
5
5
|
/** For next-core internal or devtools usage only. */
|
|
6
6
|
export function lint(source) {
|
|
7
|
-
|
|
7
|
+
let {
|
|
8
8
|
typescript,
|
|
9
9
|
rules
|
|
10
10
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
const errors = [];
|
|
12
|
+
const file = typeof source === "string" ? parseForAnalysis(source, {
|
|
13
13
|
typescript
|
|
14
14
|
}) : source;
|
|
15
15
|
if (!file) {
|
|
16
16
|
// Return no errors if parse failed.
|
|
17
17
|
return errors;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
const body = file.program.body;
|
|
20
|
+
const jsNodes = typescript ? [] : body;
|
|
21
21
|
if (typescript) {
|
|
22
|
-
for (
|
|
22
|
+
for (const node of body) {
|
|
23
23
|
if (node.type.startsWith("TS")) {
|
|
24
24
|
if (/Enum|Import|Export/.test(node.type)) {
|
|
25
25
|
errors.push({
|
|
26
26
|
type: "SyntaxError",
|
|
27
|
-
message:
|
|
27
|
+
message: `Unsupported TypeScript syntax: \`${node.type}\``,
|
|
28
28
|
loc: node.loc
|
|
29
29
|
});
|
|
30
30
|
}
|
|
@@ -33,16 +33,16 @@ export function lint(source) {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
for (
|
|
38
|
-
|
|
36
|
+
let func;
|
|
37
|
+
for (const node of jsNodes) {
|
|
38
|
+
const isFunctionDeclaration = node.type === "FunctionDeclaration";
|
|
39
39
|
if (isFunctionDeclaration && !func) {
|
|
40
|
-
func =
|
|
40
|
+
func = node;
|
|
41
41
|
} else {
|
|
42
42
|
errors.push({
|
|
43
43
|
type: "SyntaxError",
|
|
44
|
-
message: isFunctionDeclaration ? "Expect a single function declaration" :
|
|
45
|
-
loc:
|
|
44
|
+
message: isFunctionDeclaration ? "Expect a single function declaration" : `\`${node.type}\` is not allowed in top level`,
|
|
45
|
+
loc: node.loc
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -72,7 +72,7 @@ export function lint(source) {
|
|
|
72
72
|
if (node.async || node.generator) {
|
|
73
73
|
errors.push({
|
|
74
74
|
type: "SyntaxError",
|
|
75
|
-
message:
|
|
75
|
+
message: `${node.async ? "Async" : "Generator"} function is not allowed`,
|
|
76
76
|
loc: node.loc
|
|
77
77
|
});
|
|
78
78
|
}
|
|
@@ -95,7 +95,7 @@ export function lint(source) {
|
|
|
95
95
|
}
|
|
96
96
|
break;
|
|
97
97
|
case "ObjectExpression":
|
|
98
|
-
for (
|
|
98
|
+
for (const prop of node.properties) {
|
|
99
99
|
if (prop.type === "Property") {
|
|
100
100
|
if (prop.kind !== "init") {
|
|
101
101
|
errors.push({
|
|
@@ -143,7 +143,7 @@ export function lint(source) {
|
|
|
143
143
|
beforeVisitUnknown(node) {
|
|
144
144
|
errors.push({
|
|
145
145
|
type: "SyntaxError",
|
|
146
|
-
message:
|
|
146
|
+
message: `Unsupported syntax: \`${node.type}\``,
|
|
147
147
|
loc: node.loc
|
|
148
148
|
});
|
|
149
149
|
return true;
|
package/dist/esm/lint.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lint.js","names":["parseForAnalysis","precook","lint","source","typescript","rules","arguments","length","undefined","errors","file","body","program","jsNodes","node","type","startsWith","test","push","message","concat","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"],"sources":["../../src/lint.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-non-null-assertion */\nimport { type FunctionDeclaration, Statement } from \"@babel/types\";\nimport type { CookRules, ParseResultOfFile } from \"./interfaces.js\";\nimport { parseForAnalysis } from \"./parseForAnalysis.js\";\nimport { precook } from \"./precook.js\";\n\nexport interface LintOptions {\n typescript?: boolean;\n rules?: CookRules;\n}\n\nexport interface LintError {\n type: \"SyntaxError\" | \"TypeError\";\n message: string;\n loc: {\n start: {\n line: number;\n column: number;\n };\n end: {\n line: number;\n column: number;\n };\n };\n}\n\n/** For next-core internal or devtools usage only. */\nexport function lint(\n source: string | ParseResultOfFile | null,\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 | undefined;\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":"AAAA;;AAGA,SAASA,gBAAgB,QAAQ,uBAAuB;AACxD,SAASC,OAAO,QAAQ,cAAc;AAsBtC;AACA,OAAO,SAASC,IAAIA,CAClBC,MAAyC,EAE5B;EAAA,IADb;IAAEC,UAAU;IAAEC;EAAmB,CAAC,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAEvC,IAAMG,MAAmB,GAAG,EAAE;EAC9B,IAAMC,IAAI,GACR,OAAOP,MAAM,KAAK,QAAQ,GACtBH,gBAAgB,CAACG,MAAM,EAAE;IAAEC;EAAW,CAAC,CAAC,GACxCD,MAAM;EACZ,IAAI,CAACO,IAAI,EAAE;IACT;IACA,OAAOD,MAAM;EACf;EACA,IAAME,IAAI,GAAGD,IAAI,CAACE,OAAO,CAACD,IAAI;EAC9B,IAAME,OAAoB,GAAGT,UAAU,GAAG,EAAE,GAAGO,IAAI;EACnD,IAAIP,UAAU,EAAE;IACd,KAAK,IAAMU,IAAI,IAAIH,IAAI,EAAE;MACv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AAI;QACb;MACF;IACF,CAAC,CAAC;EACJ;EACA,OAAOZ,MAAM;AACf","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"lint.js","names":["parseForAnalysis","precook","lint","source","typescript","rules","arguments","length","undefined","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"],"sources":["../../src/lint.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-non-null-assertion */\nimport { type FunctionDeclaration, Statement } from \"@babel/types\";\nimport type { CookRules, ParseResultOfFile } from \"./interfaces.js\";\nimport { parseForAnalysis } from \"./parseForAnalysis.js\";\nimport { precook } from \"./precook.js\";\n\nexport interface LintOptions {\n typescript?: boolean;\n rules?: CookRules;\n}\n\nexport interface LintError {\n type: \"SyntaxError\" | \"TypeError\";\n message: string;\n loc: {\n start: {\n line: number;\n column: number;\n };\n end: {\n line: number;\n column: number;\n };\n };\n}\n\n/** For next-core internal or devtools usage only. */\nexport function lint(\n source: string | ParseResultOfFile | null,\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 | undefined;\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":"AAAA;;AAGA,SAASA,gBAAgB,QAAQ,uBAAuB;AACxD,SAASC,OAAO,QAAQ,cAAc;AAsBtC;AACA,OAAO,SAASC,IAAIA,CAClBC,MAAyC,EAE5B;EAAA,IADb;IAAEC,UAAU;IAAEC;EAAmB,CAAC,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAEvC,MAAMG,MAAmB,GAAG,EAAE;EAC9B,MAAMC,IAAI,GACR,OAAOP,MAAM,KAAK,QAAQ,GACtBH,gBAAgB,CAACG,MAAM,EAAE;IAAEC;EAAW,CAAC,CAAC,GACxCD,MAAM;EACZ,IAAI,CAACO,IAAI,EAAE;IACT;IACA,OAAOD,MAAM;EACf;EACA,MAAME,IAAI,GAAGD,IAAI,CAACE,OAAO,CAACD,IAAI;EAC9B,MAAME,OAAoB,GAAGT,UAAU,GAAG,EAAE,GAAGO,IAAI;EACnD,IAAIP,UAAU,EAAE;IACd,KAAK,MAAMU,IAAI,IAAIH,IAAI,EAAE;MACvB,IAAIG,IAAI,CAACC,IAAI,CAACC,UAAU,CAAC,IAAI,CAAC,EAAE;QAC9B,IAAI,oBAAoB,CAACC,IAAI,CAACH,IAAI,CAACC,IAAI,CAAC,EAAE;UACxCN,MAAM,CAACS,IAAI,CAAC;YACVH,IAAI,EAAE,aAAa;YACnBI,OAAO,EAAE,oCAAoCL,IAAI,CAACC,IAAI,IAAI;YAC1DK,GAAG,EAAEN,IAAI,CAACM;UACZ,CAAC,CAAC;QACJ;MACF,CAAC,MAAM;QACLP,OAAO,CAACK,IAAI,CAACJ,IAAI,CAAC;MACpB;IACF;EACF;EACA,IAAIO,IAAqC;EACzC,KAAK,MAAMP,IAA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AE,aAAa;kBACnBI,OAAO,EACL,kEAAkE;kBACpEC,GAAG,EAAE;oBACHI,KAAK,EAAEV,IAAI,CAACM,GAAG,CAAEI,KAAK;oBACtBG,GAAG,EAAE;sBACHF,IAAI,EAAEX,IAAI,CAACM,GAAG,CAAEI,KAAK,CAACC,IAAI;sBAC1B;sBACAC,MAAM,EAAEZ,IAAI,CAACM,GAAG,CAAEI,KAAK,CAACE,MAAM,GAAG;oBACnC;kBACF;gBACF,CAAC,CAAC;cACJ;cACA;UACJ;QACF,CAAC;QACDiB,iBAAiBA,CAAC7B,IAAI,EAAE;UACtB,IAAIA,IAAI,CAAC2B,IAAI,KAAK,WAAW,EAAE;YAC7BhC,MAAM,CAACS,IAAI,CAAC;cACVH,IAAI,EAAE,aAAa;cACnBI,OAAO,EAAE,gDAAgD;cACzDC,GAAG,EAAEN,IAAI,CAACM;YACZ,CAAC,CAAC;UACJ;QACF,CAAC;QACDwB,kBAAkBA,CAAC9B,IAAI,EAAE;UACvBL,MAAM,CAACS,IAAI,CAAC;YACVH,IAAI,EAAE,aAAa;YACnBI,OAAO,EAAE,yBAAyBL,IAAI,CAACC,IAAI,IAAI;YAC/CK,GAAG,EAAEN,IAAI,CAACM;UACZ,CAAC,CAAC;UACF,OAAO,IAAI;QACb;MACF;IACF,CAAC,CAAC;EACJ;EACA,OAAOX,MAAM;AACf","ignoreList":[]}
|
package/dist/esm/parse.js
CHANGED
|
@@ -8,21 +8,21 @@ export function parseAsEstreeExpression(source) {
|
|
|
8
8
|
});
|
|
9
9
|
}
|
|
10
10
|
export function parseAsEstree(source) {
|
|
11
|
-
|
|
11
|
+
let {
|
|
12
12
|
typescript
|
|
13
13
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
14
|
-
|
|
14
|
+
const file = parse(source, {
|
|
15
15
|
plugins: ["estree", typescript && "typescript"].filter(Boolean),
|
|
16
16
|
strictMode: true,
|
|
17
17
|
attachComment: false
|
|
18
18
|
});
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
const body = file.program.body;
|
|
20
|
+
const jsNodes = typescript ? [] : body;
|
|
21
21
|
if (typescript) {
|
|
22
|
-
for (
|
|
22
|
+
for (const node of body) {
|
|
23
23
|
if (node.type.startsWith("TS")) {
|
|
24
24
|
if (/Enum|Import|Export/.test(node.type)) {
|
|
25
|
-
throw new SyntaxError(
|
|
25
|
+
throw new SyntaxError(`Unsupported TypeScript syntax: ${node.type}`);
|
|
26
26
|
}
|
|
27
27
|
} else {
|
|
28
28
|
jsNodes.push(node);
|
|
@@ -33,7 +33,7 @@ export function parseAsEstree(source) {
|
|
|
33
33
|
throw new SyntaxError("Function declaration not found");
|
|
34
34
|
}
|
|
35
35
|
if (jsNodes.length > 1 || jsNodes[0].type !== "FunctionDeclaration") {
|
|
36
|
-
throw new SyntaxError(
|
|
36
|
+
throw new SyntaxError(`Expect a single function declaration at top level, but received: ${jsNodes.map(node => `"${node.type}"`).join(", ")}`);
|
|
37
37
|
}
|
|
38
38
|
return jsNodes[0];
|
|
39
39
|
}
|
package/dist/esm/parse.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","names":["parse","parseExpression","parseAsEstreeExpression","source","plugins","proposal","attachComment","parseAsEstree","typescript","arguments","length","undefined","file","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","
|
|
1
|
+
{"version":3,"file":"parse.js","names":["parse","parseExpression","parseAsEstreeExpression","source","plugins","proposal","attachComment","parseAsEstree","typescript","arguments","length","undefined","file","filter","Boolean","strictMode","body","program","jsNodes","node","type","startsWith","test","SyntaxError","push","map","join"],"sources":["../../src/parse.ts"],"sourcesContent":["import { parse, parseExpression, type ParserPlugin } from \"@babel/parser\";\nimport type { 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"],"mappings":"AAAA,SAASA,KAAK,EAAEC,eAAe,QAA2B,eAAe;AAGzE,OAAO,SAASC,uBAAuBA,CAACC,MAAc,EAAc;EAClE,OAAOF,eAAe,CAACE,MAAM,EAAE;IAC7BC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,kBAAkB,EAAE;MAAEC,QAAQ,EAAE;IAAU,CAAC,CAAC,CAAC;IAClEC,aAAa,EAAE;EACjB,CAAC,CAAC;AACJ;AAMA,OAAO,SAASC,aAAaA,CAC3BJ,MAAc,EAEO;EAAA,IADrB;IAAEK;EAA+B,CAAC,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAEvC,MAAMG,IAAI,GAAGZ,KAAK,CAACG,MAAM,EAAE;IACzBC,OAAO,EAAE,CAAC,QAAQ,EAAEI,UAAU,IAAI,YAAY,CAAC,CAACK,MAAM,CACpDC,OACF,CAAmB;IACnBC,UAAU,EAAE,IAAI;IAChBT,aAAa,EAAE;EACjB,CAAC,CAAC;EACF,MAAMU,IAAI,GAAGJ,IAAI,CAACK,OAAO,CAACD,IAAI;EAC9B,MAAME,OAAoB,GAAGV,UAAU,GAAG,EAAE,GAAGQ,IAAI;EACnD,IAAIR,UAAU,EAAE;IACd,KAAK,MAAMW,IAAI,IAAIH,IAAI,EAAE;MACvB,IAAIG,IAAI,CAACC,IAAI,CAACC,UAAU,CAAC,IAAI,CAAC,EAAE;QAC9B,IAAI,oBAAoB,CAACC,IAAI,CAACH,IAAI,CAACC,IAAI,CAAC,EAAE;UACxC,MAAM,IAAIG,WAAW,CAAC,kCAAkCJ,IAAI,CAACC,IAAI,EAAE,CAAC;QACtE;MACF,CAAC,MAAM;QACLF,OAAO,CAACM,IAAI,CAACL,IAAI,CAAC;MACpB;IACF;EACF;EACA,IAAID,OAAO,CAACR,MAAM,KAAK,CAAC,EAAE;IACxB,MAAM,IAAIa,WAAW,CAAC,gCAAgC,CAAC;EACzD;EACA,IAAIL,OAAO,CAACR,MAAM,GAAG,CAAC,IAAIQ,OAAO,CAAC,CAAC,CAAC,CAACE,IAAI,KAAK,qBAAqB,EAAE;IACnE,MAAM,IAAIG,WAAW,CACnB,oEAAoEL,OAAO,CACxEO,GAAG,CAAEN,IAAI,IAAK,IAAIA,IAAI,CAACC,IAAI,GAAG,CAAC,CAC/BM,IAAI,CAAC,IAAI,CAAC,EACf,CAAC;EACH;EACA,OAAOR,OAAO,CAAC,CAAC,CAAC;AACnB","ignoreList":[]}
|
package/dist/esm/precook.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
|
|
2
1
|
import { hasOwnProperty } from "./hasOwnProperty.js";
|
|
3
2
|
import { AnalysisContext, AnalysisEnvironment } from "./AnalysisContext.js";
|
|
4
3
|
import { collectBoundNames, collectScopedDeclarations, containsExpression } from "./traverse.js";
|
|
@@ -10,16 +9,16 @@ import { collectBoundNames, collectScopedDeclarations, containsExpression } from
|
|
|
10
9
|
* @returns A set of global variables the root AST attempts to access.
|
|
11
10
|
*/
|
|
12
11
|
export function precook(rootAst) {
|
|
13
|
-
|
|
12
|
+
let {
|
|
14
13
|
expressionOnly,
|
|
15
14
|
visitors,
|
|
16
15
|
withParent,
|
|
17
16
|
hooks = {}
|
|
18
17
|
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
const attemptToVisitGlobals = new Set();
|
|
19
|
+
const analysisContextStack = [];
|
|
20
|
+
const rootEnv = new AnalysisEnvironment(null);
|
|
21
|
+
const rootContext = new AnalysisContext();
|
|
23
22
|
rootContext.VariableEnvironment = rootEnv;
|
|
24
23
|
rootContext.LexicalEnvironment = rootEnv;
|
|
25
24
|
analysisContextStack.push(rootContext);
|
|
@@ -32,7 +31,7 @@ export function precook(rootAst) {
|
|
|
32
31
|
}
|
|
33
32
|
}
|
|
34
33
|
function EvaluateChildren(node, keys, parent) {
|
|
35
|
-
for (
|
|
34
|
+
for (const key of keys) {
|
|
36
35
|
Evaluate(node[key], parent === null || parent === void 0 ? void 0 : parent.concat({
|
|
37
36
|
node,
|
|
38
37
|
key
|
|
@@ -42,9 +41,10 @@ export function precook(rootAst) {
|
|
|
42
41
|
function Evaluate(node, parent) {
|
|
43
42
|
if (Array.isArray(node)) {
|
|
44
43
|
node.forEach((n, index) => {
|
|
45
|
-
Evaluate(n, parent ? parent.slice(0, -1).concat(
|
|
44
|
+
Evaluate(n, parent ? parent.slice(0, -1).concat({
|
|
45
|
+
...parent[parent.length - 1],
|
|
46
46
|
index
|
|
47
|
-
})
|
|
47
|
+
}) : parent);
|
|
48
48
|
});
|
|
49
49
|
} else if (node) {
|
|
50
50
|
var _hooks$beforeVisit, _hooks$beforeVisitUnk;
|
|
@@ -66,8 +66,8 @@ export function precook(rootAst) {
|
|
|
66
66
|
return;
|
|
67
67
|
case "ArrowFunctionExpression":
|
|
68
68
|
{
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
const env = getRunningContext().LexicalEnvironment;
|
|
70
|
+
const closure = OrdinaryFunctionCreate(node, env);
|
|
71
71
|
CallFunction(closure, parent);
|
|
72
72
|
return;
|
|
73
73
|
}
|
|
@@ -128,9 +128,9 @@ export function precook(rootAst) {
|
|
|
128
128
|
if (!node.body.length) {
|
|
129
129
|
return;
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
131
|
+
const runningContext = getRunningContext();
|
|
132
|
+
const oldEnv = runningContext.LexicalEnvironment;
|
|
133
|
+
const blockEnv = new AnalysisEnvironment(oldEnv);
|
|
134
134
|
BlockDeclarationInstantiation(node.body, blockEnv);
|
|
135
135
|
runningContext.LexicalEnvironment = blockEnv;
|
|
136
136
|
EvaluateChildren(node, ["body"], parent);
|
|
@@ -143,13 +143,13 @@ export function precook(rootAst) {
|
|
|
143
143
|
return;
|
|
144
144
|
case "CatchClause":
|
|
145
145
|
{
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
146
|
+
const runningContext = getRunningContext();
|
|
147
|
+
const oldEnv = runningContext.LexicalEnvironment;
|
|
148
|
+
const catchEnv = new AnalysisEnvironment(oldEnv);
|
|
149
149
|
BoundNamesInstantiation(node.param, catchEnv);
|
|
150
|
-
|
|
150
|
+
runningContext.LexicalEnvironment = catchEnv;
|
|
151
151
|
EvaluateChildren(node, ["param", "body"], parent);
|
|
152
|
-
|
|
152
|
+
runningContext.LexicalEnvironment = oldEnv;
|
|
153
153
|
return;
|
|
154
154
|
}
|
|
155
155
|
case "DoWhileStatement":
|
|
@@ -163,55 +163,55 @@ export function precook(rootAst) {
|
|
|
163
163
|
case "ForOfStatement":
|
|
164
164
|
{
|
|
165
165
|
// ForIn/OfHeadEvaluation
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
166
|
+
const lexicalBinding = node.left.type === "VariableDeclaration" && node.left.kind !== "var";
|
|
167
|
+
const runningContext = getRunningContext();
|
|
168
|
+
const oldEnv = runningContext.LexicalEnvironment;
|
|
169
169
|
if (lexicalBinding) {
|
|
170
|
-
|
|
170
|
+
const newEnv = new AnalysisEnvironment(oldEnv);
|
|
171
171
|
BoundNamesInstantiation(node.left, newEnv);
|
|
172
|
-
|
|
172
|
+
runningContext.LexicalEnvironment = newEnv;
|
|
173
173
|
}
|
|
174
174
|
EvaluateChildren(node, ["right"], parent);
|
|
175
|
-
|
|
175
|
+
runningContext.LexicalEnvironment = oldEnv;
|
|
176
176
|
|
|
177
177
|
// ForIn/OfBodyEvaluation
|
|
178
178
|
if (lexicalBinding) {
|
|
179
|
-
|
|
179
|
+
const iterationEnv = new AnalysisEnvironment(oldEnv);
|
|
180
180
|
BoundNamesInstantiation(node.left, iterationEnv);
|
|
181
|
-
|
|
181
|
+
runningContext.LexicalEnvironment = iterationEnv;
|
|
182
182
|
}
|
|
183
183
|
EvaluateChildren(node, ["left", "body"], parent);
|
|
184
|
-
|
|
184
|
+
runningContext.LexicalEnvironment = oldEnv;
|
|
185
185
|
return;
|
|
186
186
|
}
|
|
187
187
|
case "ForStatement":
|
|
188
188
|
{
|
|
189
189
|
var _node$init;
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (
|
|
194
|
-
|
|
190
|
+
const lexicalBinding = ((_node$init = node.init) === null || _node$init === void 0 ? void 0 : _node$init.type) === "VariableDeclaration" && node.init.kind !== "var";
|
|
191
|
+
const runningContext = getRunningContext();
|
|
192
|
+
const oldEnv = runningContext.LexicalEnvironment;
|
|
193
|
+
if (lexicalBinding) {
|
|
194
|
+
const loopEnv = new AnalysisEnvironment(oldEnv);
|
|
195
195
|
BoundNamesInstantiation(node.init, loopEnv);
|
|
196
|
-
|
|
196
|
+
runningContext.LexicalEnvironment = loopEnv;
|
|
197
197
|
}
|
|
198
198
|
EvaluateChildren(node, ["init", "test", "body", "update"], parent);
|
|
199
|
-
|
|
199
|
+
runningContext.LexicalEnvironment = oldEnv;
|
|
200
200
|
return;
|
|
201
201
|
}
|
|
202
202
|
case "FunctionDeclaration":
|
|
203
203
|
{
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
204
|
+
const [fn] = collectBoundNames(node);
|
|
205
|
+
const env = getRunningContext().LexicalEnvironment;
|
|
206
|
+
const fo = OrdinaryFunctionCreate(node, env);
|
|
207
|
+
env.CreateBinding(fn);
|
|
208
208
|
CallFunction(fo, parent);
|
|
209
209
|
return;
|
|
210
210
|
}
|
|
211
211
|
case "FunctionExpression":
|
|
212
212
|
{
|
|
213
|
-
|
|
214
|
-
CallFunction(
|
|
213
|
+
const closure = InstantiateOrdinaryFunctionExpression(node);
|
|
214
|
+
CallFunction(closure, parent);
|
|
215
215
|
return;
|
|
216
216
|
}
|
|
217
217
|
case "IfStatement":
|
|
@@ -228,13 +228,13 @@ export function precook(rootAst) {
|
|
|
228
228
|
case "SwitchStatement":
|
|
229
229
|
{
|
|
230
230
|
EvaluateChildren(node, ["discriminant"], parent);
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
BlockDeclarationInstantiation(node.cases,
|
|
235
|
-
|
|
231
|
+
const runningContext = getRunningContext();
|
|
232
|
+
const oldEnv = runningContext.LexicalEnvironment;
|
|
233
|
+
const blockEnv = new AnalysisEnvironment(oldEnv);
|
|
234
|
+
BlockDeclarationInstantiation(node.cases, blockEnv);
|
|
235
|
+
runningContext.LexicalEnvironment = blockEnv;
|
|
236
236
|
EvaluateChildren(node, ["cases"], parent);
|
|
237
|
-
|
|
237
|
+
runningContext.LexicalEnvironment = oldEnv;
|
|
238
238
|
return;
|
|
239
239
|
}
|
|
240
240
|
case "TryStatement":
|
|
@@ -251,27 +251,27 @@ export function precook(rootAst) {
|
|
|
251
251
|
return;
|
|
252
252
|
}
|
|
253
253
|
}
|
|
254
|
-
|
|
254
|
+
const silent = (_hooks$beforeVisitUnk = hooks.beforeVisitUnknown) === null || _hooks$beforeVisitUnk === void 0 ? void 0 : _hooks$beforeVisitUnk.call(hooks, node, parent);
|
|
255
255
|
if (!silent) {
|
|
256
256
|
// eslint-disable-next-line no-console
|
|
257
|
-
console.warn(
|
|
257
|
+
console.warn(`Unsupported node type \`${node.type}\``);
|
|
258
258
|
}
|
|
259
259
|
}
|
|
260
260
|
}
|
|
261
261
|
function BoundNamesInstantiation(declarations, env) {
|
|
262
|
-
for (
|
|
262
|
+
for (const name of collectBoundNames(declarations)) {
|
|
263
263
|
env.CreateBinding(name);
|
|
264
264
|
}
|
|
265
265
|
}
|
|
266
266
|
function ResolveBinding(name) {
|
|
267
|
-
|
|
267
|
+
const env = getRunningContext().LexicalEnvironment;
|
|
268
268
|
return GetIdentifierReference(env, name);
|
|
269
269
|
}
|
|
270
270
|
function GetIdentifierReference(env, name) {
|
|
271
271
|
return !!env && (env.HasBinding(name) || GetIdentifierReference(env.OuterEnv, name));
|
|
272
272
|
}
|
|
273
273
|
function BlockDeclarationInstantiation(code, env) {
|
|
274
|
-
|
|
274
|
+
const declarations = collectScopedDeclarations(code, {
|
|
275
275
|
var: false,
|
|
276
276
|
topLevel: false
|
|
277
277
|
});
|
|
@@ -290,33 +290,33 @@ export function precook(rootAst) {
|
|
|
290
290
|
analysisContextStack.pop();
|
|
291
291
|
}
|
|
292
292
|
function PrepareOrdinaryCall(F) {
|
|
293
|
-
|
|
294
|
-
|
|
293
|
+
const calleeContext = new AnalysisContext();
|
|
294
|
+
const localEnv = new AnalysisEnvironment(F.Environment);
|
|
295
295
|
calleeContext.VariableEnvironment = localEnv;
|
|
296
296
|
calleeContext.LexicalEnvironment = localEnv;
|
|
297
297
|
analysisContextStack.push(calleeContext);
|
|
298
298
|
}
|
|
299
299
|
function FunctionDeclarationInstantiation(func, parent) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
300
|
+
const calleeContext = getRunningContext();
|
|
301
|
+
const code = func.ECMAScriptCode;
|
|
302
|
+
const formals = func.FormalParameters;
|
|
303
|
+
const hasParameterExpressions = containsExpression(formals);
|
|
304
|
+
const varDeclarations = collectScopedDeclarations(code, {
|
|
305
305
|
var: true,
|
|
306
306
|
topLevel: true
|
|
307
307
|
});
|
|
308
|
-
|
|
309
|
-
|
|
308
|
+
const varNames = collectBoundNames(varDeclarations);
|
|
309
|
+
const env = calleeContext.LexicalEnvironment;
|
|
310
310
|
BoundNamesInstantiation(formals, env);
|
|
311
311
|
Evaluate(formals, parent === null || parent === void 0 ? void 0 : parent.concat({
|
|
312
312
|
node: func.Function,
|
|
313
313
|
key: "params"
|
|
314
314
|
}));
|
|
315
|
-
|
|
315
|
+
let varEnv;
|
|
316
316
|
if (!hasParameterExpressions) {
|
|
317
317
|
// NOTE: Only a single Environment Record is needed for the parameters
|
|
318
318
|
// and top-level vars.
|
|
319
|
-
for (
|
|
319
|
+
for (const n of varNames) {
|
|
320
320
|
env.CreateBinding(n);
|
|
321
321
|
}
|
|
322
322
|
varEnv = env;
|
|
@@ -326,25 +326,25 @@ export function precook(rootAst) {
|
|
|
326
326
|
// visibility of declarations in the function body.
|
|
327
327
|
varEnv = new AnalysisEnvironment(env);
|
|
328
328
|
calleeContext.VariableEnvironment = varEnv;
|
|
329
|
-
for (
|
|
330
|
-
varEnv.CreateBinding(
|
|
329
|
+
for (const n of varNames) {
|
|
330
|
+
varEnv.CreateBinding(n);
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
|
-
|
|
333
|
+
const lexEnv = varEnv;
|
|
334
334
|
calleeContext.LexicalEnvironment = lexEnv;
|
|
335
|
-
|
|
335
|
+
const lexDeclarations = collectScopedDeclarations(code, {
|
|
336
336
|
var: false,
|
|
337
337
|
topLevel: true
|
|
338
338
|
});
|
|
339
339
|
BoundNamesInstantiation(lexDeclarations, lexEnv);
|
|
340
340
|
}
|
|
341
341
|
function InstantiateOrdinaryFunctionExpression(functionExpression) {
|
|
342
|
-
|
|
342
|
+
const scope = getRunningContext().LexicalEnvironment;
|
|
343
343
|
if (!functionExpression.id) {
|
|
344
344
|
return OrdinaryFunctionCreate(functionExpression, scope);
|
|
345
345
|
}
|
|
346
|
-
|
|
347
|
-
|
|
346
|
+
const name = functionExpression.id.name;
|
|
347
|
+
const funcEnv = new AnalysisEnvironment(scope);
|
|
348
348
|
funcEnv.CreateBinding(name);
|
|
349
349
|
return OrdinaryFunctionCreate(functionExpression, funcEnv);
|
|
350
350
|
}
|