@next-core/cook 1.0.1

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 (78) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/AnalysisContext.js +42 -0
  4. package/dist/cjs/AnalysisContext.js.map +1 -0
  5. package/dist/cjs/ExecutionContext.js +161 -0
  6. package/dist/cjs/ExecutionContext.js.map +1 -0
  7. package/dist/cjs/context-free.js +262 -0
  8. package/dist/cjs/context-free.js.map +1 -0
  9. package/dist/cjs/cook.js +1587 -0
  10. package/dist/cjs/cook.js.map +1 -0
  11. package/dist/cjs/hasOwnProperty.js +11 -0
  12. package/dist/cjs/hasOwnProperty.js.map +1 -0
  13. package/dist/cjs/index.js +58 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/interfaces.js +6 -0
  16. package/dist/cjs/interfaces.js.map +1 -0
  17. package/dist/cjs/lint.js +176 -0
  18. package/dist/cjs/lint.js.map +1 -0
  19. package/dist/cjs/parse.js +51 -0
  20. package/dist/cjs/parse.js.map +1 -0
  21. package/dist/cjs/precook.js +432 -0
  22. package/dist/cjs/precook.js.map +1 -0
  23. package/dist/cjs/precookFunction.js +25 -0
  24. package/dist/cjs/precookFunction.js.map +1 -0
  25. package/dist/cjs/preevaluate.js +35 -0
  26. package/dist/cjs/preevaluate.js.map +1 -0
  27. package/dist/cjs/sanitize.js +59 -0
  28. package/dist/cjs/sanitize.js.map +1 -0
  29. package/dist/cjs/traverse.js +168 -0
  30. package/dist/cjs/traverse.js.map +1 -0
  31. package/dist/esm/AnalysisContext.js +30 -0
  32. package/dist/esm/AnalysisContext.js.map +1 -0
  33. package/dist/esm/ExecutionContext.js +135 -0
  34. package/dist/esm/ExecutionContext.js.map +1 -0
  35. package/dist/esm/context-free.js +221 -0
  36. package/dist/esm/context-free.js.map +1 -0
  37. package/dist/esm/cook.js +1607 -0
  38. package/dist/esm/cook.js.map +1 -0
  39. package/dist/esm/hasOwnProperty.js +4 -0
  40. package/dist/esm/hasOwnProperty.js.map +1 -0
  41. package/dist/esm/index.js +5 -0
  42. package/dist/esm/index.js.map +1 -0
  43. package/dist/esm/interfaces.js +2 -0
  44. package/dist/esm/interfaces.js.map +1 -0
  45. package/dist/esm/lint.js +168 -0
  46. package/dist/esm/lint.js.map +1 -0
  47. package/dist/esm/parse.js +41 -0
  48. package/dist/esm/parse.js.map +1 -0
  49. package/dist/esm/precook.js +435 -0
  50. package/dist/esm/precook.js.map +1 -0
  51. package/dist/esm/precookFunction.js +20 -0
  52. package/dist/esm/precookFunction.js.map +1 -0
  53. package/dist/esm/preevaluate.js +23 -0
  54. package/dist/esm/preevaluate.js.map +1 -0
  55. package/dist/esm/sanitize.js +48 -0
  56. package/dist/esm/sanitize.js.map +1 -0
  57. package/dist/esm/traverse.js +157 -0
  58. package/dist/esm/traverse.js.map +1 -0
  59. package/dist/types/AnalysisContext.d.ts +17 -0
  60. package/dist/types/ExecutionContext.d.ts +79 -0
  61. package/dist/types/context-free.d.ts +19 -0
  62. package/dist/types/cook.d.ts +12 -0
  63. package/dist/types/cook.spec.d.ts +1 -0
  64. package/dist/types/hasOwnProperty.d.ts +1 -0
  65. package/dist/types/index.d.ts +4 -0
  66. package/dist/types/interfaces.d.ts +33 -0
  67. package/dist/types/lint.d.ts +13 -0
  68. package/dist/types/lint.spec.d.ts +1 -0
  69. package/dist/types/parse.d.ts +6 -0
  70. package/dist/types/precook.d.ts +14 -0
  71. package/dist/types/precook.spec.d.ts +1 -0
  72. package/dist/types/precookFunction.d.ts +10 -0
  73. package/dist/types/precookFunction.spec.d.ts +1 -0
  74. package/dist/types/preevaluate.d.ts +13 -0
  75. package/dist/types/preevaluate.spec.d.ts +1 -0
  76. package/dist/types/sanitize.d.ts +2 -0
  77. package/dist/types/traverse.d.ts +11 -0
  78. package/package.json +40 -0
@@ -0,0 +1,157 @@
1
+ export function collectBoundNames(root) {
2
+ var names = new Set();
3
+
4
+ var collect = node => {
5
+ if (Array.isArray(node)) {
6
+ for (var n of node) {
7
+ collect(n);
8
+ }
9
+ } else if (node) {
10
+ // `node` maybe `null` in some cases.
11
+ switch (node.type) {
12
+ case "Identifier":
13
+ names.add(node.name);
14
+ return;
15
+
16
+ case "VariableDeclaration":
17
+ return collect(node.declarations);
18
+
19
+ case "VariableDeclarator":
20
+ return collect(node.id);
21
+
22
+ case "ArrayPattern":
23
+ return collect(node.elements);
24
+
25
+ case "AssignmentPattern":
26
+ return collect(node.left);
27
+
28
+ case "ObjectPattern":
29
+ return collect(node.properties);
30
+
31
+ case "Property":
32
+ return collect(node.value);
33
+
34
+ case "RestElement":
35
+ return collect(node.argument);
36
+
37
+ case "FunctionDeclaration":
38
+ return collect(node.id);
39
+ }
40
+ }
41
+ };
42
+
43
+ collect(root);
44
+ return Array.from(names);
45
+ }
46
+ export function containsExpression(root) {
47
+ var collect = node => {
48
+ if (Array.isArray(node)) {
49
+ return node.some(collect);
50
+ } else if (node) {
51
+ // `node` maybe `null` in some cases.
52
+ switch (node.type) {
53
+ case "ArrayPattern":
54
+ return collect(node.elements);
55
+
56
+ case "AssignmentPattern":
57
+ return true;
58
+
59
+ case "ObjectPattern":
60
+ return collect(node.properties);
61
+
62
+ case "Property":
63
+ return node.computed || collect(node.value);
64
+
65
+ case "RestElement":
66
+ return collect(node.argument);
67
+ }
68
+ }
69
+ };
70
+
71
+ return collect(root);
72
+ }
73
+ export function collectScopedDeclarations(root, options) {
74
+ var declarations = [];
75
+ var nextOptions = {
76
+ var: options.var
77
+ };
78
+
79
+ var collect = (node, options) => {
80
+ if (Array.isArray(node)) {
81
+ for (var n of node) {
82
+ collect(n, options);
83
+ }
84
+ } else if (node) {
85
+ // `node` maybe `null` in some cases.
86
+ switch (node.type) {
87
+ case "FunctionDeclaration":
88
+ // At the top level of a function, or script, function declarations are
89
+ // treated like var declarations rather than like lexical declarations.
90
+ // See https://tc39.es/ecma262/#sec-static-semantics-toplevellexicallydeclarednames
91
+ if (Number(!options.var) ^ Number(options.topLevel)) {
92
+ declarations.push(node);
93
+ }
94
+
95
+ return;
96
+
97
+ case "VariableDeclaration":
98
+ if (Number(!options.var) ^ Number(node.kind === "var")) {
99
+ declarations.push(node);
100
+ }
101
+
102
+ return;
103
+
104
+ case "SwitchCase":
105
+ collect(node.consequent, nextOptions);
106
+ return;
107
+
108
+ case "CatchClause":
109
+ collect(node.body, nextOptions);
110
+ return;
111
+ }
112
+
113
+ if (options.var) {
114
+ switch (node.type) {
115
+ case "BlockStatement":
116
+ collect(node.body, nextOptions);
117
+ return;
118
+
119
+ case "IfStatement":
120
+ collect(node.consequent, nextOptions);
121
+ collect(node.alternate, nextOptions);
122
+ return;
123
+
124
+ case "DoWhileStatement":
125
+ case "WhileStatement":
126
+ collect(node.body, nextOptions);
127
+ return;
128
+
129
+ case "ForStatement":
130
+ collect(node.init, nextOptions);
131
+ collect(node.body, nextOptions);
132
+ return;
133
+
134
+ case "ForInStatement":
135
+ case "ForOfStatement":
136
+ collect(node.left, nextOptions);
137
+ collect(node.body, nextOptions);
138
+ return;
139
+
140
+ case "SwitchStatement":
141
+ collect(node.cases, nextOptions);
142
+ return;
143
+
144
+ case "TryStatement":
145
+ collect(node.block, nextOptions);
146
+ collect(node.handler, nextOptions);
147
+ collect(node.finalizer, nextOptions);
148
+ return;
149
+ }
150
+ }
151
+ }
152
+ };
153
+
154
+ collect(root, options);
155
+ return declarations;
156
+ }
157
+ //# sourceMappingURL=traverse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/traverse.ts"],"names":["collectBoundNames","root","names","Set","collect","node","Array","isArray","n","type","add","name","declarations","id","elements","left","properties","value","argument","from","containsExpression","some","computed","collectScopedDeclarations","options","nextOptions","var","Number","topLevel","push","kind","consequent","body","alternate","init","cases","block","handler","finalizer"],"mappings":"AAYA,OAAO,SAASA,iBAAT,CACLC,IADK,EAEK;AACV,MAAMC,KAAK,GAAG,IAAIC,GAAJ,EAAd;;AACA,MAA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O,CAACC,IAAI,CAACgC,OAAN,EAAeZ,WAAf,CAAP;AACArB,YAAAA,OAAO,CAACC,IAAI,CAACiC,SAAN,EAAiBb,WAAjB,CAAP;AACA;AA5BJ;AA8BD;AACF;AACF,GAhED;;AAiEArB,EAAAA,OAAO,CAACH,IAAD,EAAOuB,OAAP,CAAP;AACA,SAAOZ,YAAP;AACD","sourcesContent":["import { FunctionDeclaration, VariableDeclaration } from \"@babel/types\";\nimport { EstreeNode, NodeWithBoundNames } from \"./interfaces\";\n\ntype InternalCollect<T = void, O = unknown> = (\n node: EstreeNode | EstreeNode[],\n options?: O\n) => T;\ntype InternalCollectWithOptions<T = void, O = unknown> = (\n node: EstreeNode | EstreeNode[],\n options: O\n) => T;\n\nexport function collectBoundNames(\n root: NodeWithBoundNames | NodeWithBoundNames[]\n): string[] {\n const names = new Set<string>();\n const collect: InternalCollect = (node) => {\n if (Array.isArray(node)) {\n for (const n of node) {\n collect(n);\n }\n } else if (node) {\n // `node` maybe `null` in some cases.\n switch (node.type) {\n case \"Identifier\":\n names.add(node.name);\n return;\n case \"VariableDeclaration\":\n return collect(node.declarations);\n case \"VariableDeclarator\":\n return collect(node.id);\n case \"ArrayPattern\":\n return collect(node.elements);\n case \"AssignmentPattern\":\n return collect(node.left);\n case \"ObjectPattern\":\n return collect(node.properties);\n case \"Property\":\n return collect(node.value);\n case \"RestElement\":\n return collect(node.argument);\n case \"FunctionDeclaration\":\n return collect(node.id);\n }\n }\n };\n collect(root);\n return Array.from(names);\n}\n\nexport function containsExpression(root: EstreeNode | EstreeNode[]): boolean {\n const collect: InternalCollect<boolean> = (node) => {\n if (Array.isArray(node)) {\n return node.some(collect);\n } else if (node) {\n // `node` maybe `null` in some cases.\n switch (node.type) {\n case \"ArrayPattern\":\n return collect(node.elements);\n case \"AssignmentPattern\":\n return true;\n case \"ObjectPattern\":\n return collect(node.properties);\n case \"Property\":\n return node.computed || collect(node.value);\n case \"RestElement\":\n return collect(node.argument);\n }\n }\n };\n return collect(root);\n}\n\ninterface ScopedDeclarationOptions {\n var?: boolean;\n topLevel?: boolean;\n}\n\ntype ScopedDeclaration = VariableDeclaration | FunctionDeclaration;\n\nexport function collectScopedDeclarations(\n root: EstreeNode | EstreeNode[],\n options: ScopedDeclarationOptions\n): ScopedDeclaration[] {\n const declarations: ScopedDeclaration[] = [];\n const nextOptions = { var: options.var };\n const collect: InternalCollectWithOptions<void, ScopedDeclarationOptions> = (\n node,\n options\n ): void => {\n if (Array.isArray(node)) {\n for (const n of node) {\n collect(n, options);\n }\n } else if (node) {\n // `node` maybe `null` in some cases.\n switch (node.type) {\n case \"FunctionDeclaration\":\n // At the top level of a function, or script, function declarations are\n // treated like var declarations rather than like lexical declarations.\n // See https://tc39.es/ecma262/#sec-static-semantics-toplevellexicallydeclarednames\n if (Number(!options.var) ^ Number(options.topLevel)) {\n declarations.push(node);\n }\n return;\n case \"VariableDeclaration\":\n if (Number(!options.var) ^ Number(node.kind === \"var\")) {\n declarations.push(node);\n }\n return;\n case \"SwitchCase\":\n collect(node.consequent, nextOptions);\n return;\n case \"CatchClause\":\n collect(node.body, nextOptions);\n return;\n }\n if (options.var) {\n switch (node.type) {\n case \"BlockStatement\":\n collect(node.body, nextOptions);\n return;\n case \"IfStatement\":\n collect(node.consequent, nextOptions);\n collect(node.alternate, nextOptions);\n return;\n case \"DoWhileStatement\":\n case \"WhileStatement\":\n collect(node.body, nextOptions);\n return;\n case \"ForStatement\":\n collect(node.init, nextOptions);\n collect(node.body, nextOptions);\n return;\n case \"ForInStatement\":\n case \"ForOfStatement\":\n collect(node.left, nextOptions);\n collect(node.body, nextOptions);\n return;\n case \"SwitchStatement\":\n collect(node.cases, nextOptions);\n return;\n case \"TryStatement\":\n collect(node.block, nextOptions);\n collect(node.handler, nextOptions);\n collect(node.finalizer, nextOptions);\n return;\n }\n }\n }\n };\n collect(root, options);\n return declarations;\n}\n"],"file":"traverse.js"}
@@ -0,0 +1,17 @@
1
+ import { Expression, FunctionDeclaration, Statement } from "@babel/types";
2
+ export declare class AnalysisContext {
3
+ VariableEnvironment: AnalysisEnvironment;
4
+ LexicalEnvironment: AnalysisEnvironment;
5
+ }
6
+ export declare class AnalysisEnvironment {
7
+ readonly OuterEnv: AnalysisEnvironment;
8
+ private readonly bindingSet;
9
+ constructor(outer: AnalysisEnvironment);
10
+ HasBinding(name: string): boolean;
11
+ CreateBinding(name: string): void;
12
+ }
13
+ export interface AnalysisFunctionObject {
14
+ FormalParameters: FunctionDeclaration["params"];
15
+ ECMAScriptCode: Statement[] | Expression;
16
+ Environment: AnalysisEnvironment;
17
+ }
@@ -0,0 +1,79 @@
1
+ import { Expression, FunctionDeclaration, Statement } from "@babel/types";
2
+ export declare class ExecutionContext {
3
+ VariableEnvironment: EnvironmentRecord;
4
+ LexicalEnvironment: EnvironmentRecord;
5
+ Function: FunctionObject;
6
+ }
7
+ export declare type EnvironmentRecordType = "function" | "declarative";
8
+ export declare class EnvironmentRecord {
9
+ readonly OuterEnv: EnvironmentRecord;
10
+ private readonly bindingMap;
11
+ constructor(outer: EnvironmentRecord);
12
+ HasBinding(name: string): boolean;
13
+ CreateMutableBinding(name: string, deletable: boolean): CompletionRecord;
14
+ /**
15
+ * Create an immutable binding.
16
+ *
17
+ * @param name - The binding name.
18
+ * @param strict - For named function expressions, strict is false, otherwise it's true.
19
+ * @returns CompletionRecord.
20
+ */
21
+ CreateImmutableBinding(name: string, strict: boolean): CompletionRecord;
22
+ InitializeBinding(name: string, value: unknown): CompletionRecord;
23
+ /**
24
+ * Update a mutable binding value, including function declarations.
25
+ *
26
+ * @param name - The binding name.
27
+ * @param value - The binding value.
28
+ * @param strict - For functions, strict is always false, otherwise it depends on strict-mode.
29
+ * @returns
30
+ */
31
+ SetMutableBinding(name: string, value: unknown, strict: boolean): CompletionRecord;
32
+ GetBindingValue(name: string, strict: boolean): unknown;
33
+ }
34
+ export declare class DeclarativeEnvironment extends EnvironmentRecord {
35
+ }
36
+ export declare class FunctionEnvironment extends EnvironmentRecord {
37
+ }
38
+ export interface BindingState {
39
+ initialized?: boolean;
40
+ value?: unknown;
41
+ mutable?: boolean;
42
+ /** This is used for mutable bindings only. */
43
+ deletable?: boolean;
44
+ /**
45
+ * This is used for immutable bindings only.
46
+ * For named function expressions, `strict` is false,
47
+ * otherwise it's true.
48
+ */
49
+ strict?: boolean;
50
+ }
51
+ export declare const FormalParameters: unique symbol;
52
+ export declare const ECMAScriptCode: unique symbol;
53
+ export declare const Environment: unique symbol;
54
+ export declare const IsConstructor: unique symbol;
55
+ export interface FunctionObject {
56
+ (...args: unknown[]): unknown;
57
+ [FormalParameters]: FunctionDeclaration["params"];
58
+ [ECMAScriptCode]: Statement[] | Expression;
59
+ [Environment]: EnvironmentRecord;
60
+ [IsConstructor]: boolean;
61
+ }
62
+ export declare class ReferenceRecord {
63
+ readonly Base?: Record<PropertyKey, unknown> | EnvironmentRecord | "unresolvable";
64
+ readonly ReferenceName?: PropertyKey;
65
+ /** Whether the reference is in strict mode. */
66
+ readonly Strict?: boolean;
67
+ constructor(base: Record<PropertyKey, unknown> | EnvironmentRecord | "unresolvable", referenceName: PropertyKey, strict: boolean);
68
+ }
69
+ export declare class CompletionRecord {
70
+ readonly Type: CompletionRecordType;
71
+ readonly Value: unknown;
72
+ constructor(type: CompletionRecordType, value: unknown);
73
+ }
74
+ export declare type CompletionRecordType = "normal" | "break" | "continue" | "return" | "throw";
75
+ export declare function NormalCompletion(value: unknown): CompletionRecord;
76
+ export declare const Empty: unique symbol;
77
+ export interface OptionalChainRef {
78
+ skipped?: boolean;
79
+ }
@@ -0,0 +1,19 @@
1
+ import { BinaryExpression, UnaryExpression, VariableDeclaration } from "@babel/types";
2
+ import { CompletionRecord, EnvironmentRecord, ReferenceRecord } from "./ExecutionContext";
3
+ export declare function IsPropertyReference(V: ReferenceRecord): boolean;
4
+ export declare function InitializeReferencedBinding(V: ReferenceRecord, W: unknown): CompletionRecord;
5
+ export declare function CopyDataProperties(target: Record<PropertyKey, unknown>, source: unknown, excludedItems: Set<PropertyKey>): Record<PropertyKey, unknown>;
6
+ export declare function ForDeclarationBindingInstantiation(forDeclaration: VariableDeclaration, env: EnvironmentRecord): void;
7
+ export declare function LoopContinues(completion: CompletionRecord): boolean;
8
+ export declare function UpdateEmpty(completion: CompletionRecord, value: unknown): CompletionRecord;
9
+ export declare function GetValue(V: unknown): unknown;
10
+ export declare function ToPropertyKey(arg: unknown): string | symbol;
11
+ export declare function GetV(V: unknown, P: PropertyKey): unknown;
12
+ export declare function PutValue(V: ReferenceRecord, W: unknown): CompletionRecord;
13
+ export declare function CreateListIteratorRecord(args: Iterable<unknown>): Iterator<unknown>;
14
+ export declare function RequireObjectCoercible(arg: unknown): void;
15
+ export declare function GetIdentifierReference(env: EnvironmentRecord, name: string, strict: boolean): ReferenceRecord;
16
+ export declare function ApplyStringOrNumericBinaryOperator(leftValue: number, operator: BinaryExpression["operator"] | "|>", rightValue: number): unknown;
17
+ export declare function ApplyStringOrNumericAssignment(leftValue: string | number, operator: string, rightValue: string | number): unknown;
18
+ export declare function ApplyUnaryOperator(target: unknown, operator: UnaryExpression["operator"]): unknown;
19
+ export declare function isIterable(cooked: unknown): boolean;
@@ -0,0 +1,12 @@
1
+ import { Expression, FunctionDeclaration } from "@babel/types";
2
+ import { CookRules } from "./interfaces";
3
+ export interface CookOptions {
4
+ rules?: CookRules;
5
+ globalVariables?: Record<string, unknown>;
6
+ }
7
+ export interface CookContext {
8
+ source: string;
9
+ attemptToVisitGlobals: Set<string>;
10
+ }
11
+ /** For next-core internal usage only. */
12
+ export declare function cook(rootAst: FunctionDeclaration | Expression, codeSource: string, { rules, globalVariables }?: CookOptions): unknown;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare function hasOwnProperty(object: unknown, property: string | number | symbol): boolean;
@@ -0,0 +1,4 @@
1
+ export * from "./cook";
2
+ export * from "./lint";
3
+ export * from "./precookFunction";
4
+ export * from "./preevaluate";
@@ -0,0 +1,33 @@
1
+ import { Expression, FunctionDeclaration, LVal, Node, ObjectExpression, ObjectPattern, ObjectProperty, RestElement, SourceLocation, SpreadElement, VariableDeclaration } from "@babel/types";
2
+ export declare type EstreeNode = Node | EstreeObjectExpression | EstreeObjectPattern | EstreeProperty | EstreeChainExpression | EstreeLiteral;
3
+ export declare type EstreeLVal = LVal | EstreeObjectPattern;
4
+ export declare type EstreeObjectExpression = Omit<ObjectExpression, "properties"> & {
5
+ properties: (EstreeProperty | SpreadElement)[];
6
+ };
7
+ export declare type EstreeObjectPattern = Omit<ObjectPattern, "properties"> & {
8
+ properties: (EstreeProperty | RestElement)[];
9
+ };
10
+ export declare type EstreeProperty = Omit<ObjectProperty, "type"> & {
11
+ type: "Property";
12
+ kind: "init" | "get" | "set";
13
+ };
14
+ export interface EstreeChainExpression {
15
+ type: "ChainExpression";
16
+ expression: Expression;
17
+ loc: SourceLocation;
18
+ }
19
+ export interface EstreeLiteral {
20
+ type: "Literal";
21
+ value: unknown;
22
+ raw: string;
23
+ regex?: {
24
+ flags: string;
25
+ };
26
+ loc: SourceLocation;
27
+ }
28
+ export declare type NodeWithBoundNames = LVal | VariableDeclaration | FunctionDeclaration;
29
+ export declare type EstreeVisitors = Record<string, EstreeVisitorFn>;
30
+ export declare type EstreeVisitorFn = (node: any) => void;
31
+ export interface CookRules {
32
+ noVar?: boolean;
33
+ }
@@ -0,0 +1,13 @@
1
+ import { SourceLocation } from "@babel/types";
2
+ import { CookRules } from "./interfaces";
3
+ export interface LintOptions {
4
+ typescript?: boolean;
5
+ rules?: CookRules;
6
+ }
7
+ export interface LintError {
8
+ type: "SyntaxError" | "TypeError";
9
+ message: string;
10
+ loc: SourceLocation;
11
+ }
12
+ /** For next-core internal or devtools usage only. */
13
+ export declare function lint(source: string, { typescript, rules }?: LintOptions): LintError[];
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import { Expression, FunctionDeclaration } from "@babel/types";
2
+ export declare function parseAsEstreeExpression(source: string): Expression;
3
+ export interface ParseEstreeOptions {
4
+ typescript?: boolean;
5
+ }
6
+ export declare function parseAsEstree(source: string, { typescript }?: ParseEstreeOptions): FunctionDeclaration;
@@ -0,0 +1,14 @@
1
+ import { Expression, FunctionDeclaration } from "@babel/types";
2
+ import { EstreeVisitors } from "./interfaces";
3
+ export interface PrecookOptions {
4
+ expressionOnly?: boolean;
5
+ visitors?: EstreeVisitors;
6
+ }
7
+ /**
8
+ * Analysis an AST of a storyboard function or an evaluation expression.
9
+ *
10
+ * @param rootAst - The root AST.
11
+ * @param options - Analysis options.
12
+ * @returns A set of global variables the root AST attempts to access.
13
+ */
14
+ export declare function precook(rootAst: Expression | FunctionDeclaration, { expressionOnly, visitors }?: PrecookOptions): Set<string>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { FunctionDeclaration } from "@babel/types";
2
+ import { PrecookOptions } from "./precook";
3
+ export interface PrecookFunctionOptions extends PrecookOptions {
4
+ typescript?: boolean;
5
+ }
6
+ export interface PrecookFunctionResult {
7
+ function: FunctionDeclaration;
8
+ attemptToVisitGlobals: Set<string>;
9
+ }
10
+ export declare function precookFunction(source: string, { typescript, ...restOptions }?: PrecookFunctionOptions): PrecookFunctionResult;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { Expression } from "@babel/types";
2
+ import { EstreeVisitors } from "./interfaces";
3
+ export interface PreevaluateOptions {
4
+ visitors?: EstreeVisitors;
5
+ }
6
+ export interface PreevaluateResult {
7
+ expression: Expression;
8
+ attemptToVisitGlobals: Set<string>;
9
+ source: string;
10
+ }
11
+ export declare function preevaluate(raw: string, options?: PreevaluateOptions): PreevaluateResult;
12
+ export declare function isEvaluable(raw: string): boolean;
13
+ export declare function shouldAllowRecursiveEvaluations(raw: string): boolean;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare function sanitize(cooked: unknown): void;
2
+ export declare function isAllowedConstructor(constructor: unknown): boolean;
@@ -0,0 +1,11 @@
1
+ import { FunctionDeclaration, VariableDeclaration } from "@babel/types";
2
+ import { EstreeNode, NodeWithBoundNames } from "./interfaces";
3
+ export declare function collectBoundNames(root: NodeWithBoundNames | NodeWithBoundNames[]): string[];
4
+ export declare function containsExpression(root: EstreeNode | EstreeNode[]): boolean;
5
+ interface ScopedDeclarationOptions {
6
+ var?: boolean;
7
+ topLevel?: boolean;
8
+ }
9
+ declare type ScopedDeclaration = VariableDeclaration | FunctionDeclaration;
10
+ export declare function collectScopedDeclarations(root: EstreeNode | EstreeNode[], options: ScopedDeclarationOptions): ScopedDeclaration[];
11
+ export {};
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@next-core/cook",
3
+ "version": "1.0.1",
4
+ "description": "Cook expressions and storyboard functions",
5
+ "homepage": "https://github.com/easyops-cn/next-core/tree/master/packages/cook",
6
+ "license": "GPL-3.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git@github.com:easyops-cn/next-core.git"
10
+ },
11
+ "main": "dist/cjs/index.js",
12
+ "module": "dist/esm/index.js",
13
+ "typings": "dist/types/index.d.ts",
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "clean": "rimraf dist",
19
+ "prestart": "npm run clean",
20
+ "start": "concurrently -k -n types,main \"npm run start:types\" \"npm run start:main\"",
21
+ "start:main": "cross-env NODE_ENV=development build-next-libs --watch",
22
+ "start:types": "tsc --watch --emitDeclarationOnly --declaration --declarationDir dist/types",
23
+ "prebuild": "npm run clean",
24
+ "build": "npm run build:types && npm run build:main",
25
+ "build:main": "cross-env NODE_ENV=production build-next-libs",
26
+ "build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
27
+ "test": "cross-env NODE_ENV='test' jest",
28
+ "test:ci": "cross-env NODE_ENV='test' CI=true jest"
29
+ },
30
+ "sideEffects": false,
31
+ "dependencies": {
32
+ "@babel/parser": "^7.15.5",
33
+ "@next-core/brick-types": "^2.39.1"
34
+ },
35
+ "devDependencies": {
36
+ "@next-core/supply": "^1.0.1",
37
+ "lodash": "^4.17.21"
38
+ },
39
+ "gitHead": "41bff386009ac26450dd768b5e1a1d1c9ee17636"
40
+ }