scriptable 0.0.5 → 0.1.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/.claude/settings.local.json +10 -0
- package/README.md +139 -0
- package/dist/binary-ops.d.ts +2 -0
- package/dist/binary-ops.js +16 -0
- package/dist/builtin-functions.d.ts +3 -0
- package/dist/builtin-functions.js +151 -0
- package/dist/environment.d.ts +16 -0
- package/dist/environment.js +111 -0
- package/dist/evaluate.d.ts +5 -0
- package/dist/evaluate.js +232 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +26 -0
- package/dist/logic-ops.d.ts +2 -0
- package/dist/logic-ops.js +5 -0
- package/package.json +17 -50
- package/src/__tests__/{break-out.js → break-out.test.ts} +4 -15
- package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
- package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
- package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
- package/src/__tests__/{functions.js → functions.test.ts} +7 -8
- package/src/__tests__/{logic.js → logic.test.ts} +2 -3
- package/src/__tests__/new-features.test.ts +158 -0
- package/src/__tests__/security.test.ts +399 -0
- package/src/__tests__/{variables.js → variables.test.ts} +99 -7
- package/src/binary-ops.ts +18 -0
- package/src/builtin-functions.ts +177 -0
- package/src/environment.ts +132 -0
- package/src/evaluate.ts +275 -0
- package/src/index.ts +42 -0
- package/src/logic-ops.ts +6 -0
- package/tsconfig.json +16 -0
- package/.npmignore +0 -1
- package/src/__tests__/index.js +0 -85
- package/src/binary-ops.js +0 -13
- package/src/builtin-functions.js +0 -58
- package/src/environment.js +0 -60
- package/src/evaluate.js +0 -152
- package/src/index.js +0 -26
- package/src/logic-ops.js +0 -6
- package/yarn.lock +0 -2632
package/src/__tests__/index.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
const runner = require("../");
|
|
2
|
-
|
|
3
|
-
const run = runner();
|
|
4
|
-
|
|
5
|
-
const DEBUG = true;
|
|
6
|
-
|
|
7
|
-
const code = `
|
|
8
|
-
/**
|
|
9
|
-
* @param {String} who say hello to
|
|
10
|
-
*/
|
|
11
|
-
function hello (who) {
|
|
12
|
-
return "hello " + who;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// arrow function
|
|
16
|
-
arrow = (str) => { return str + " =>"; }
|
|
17
|
-
|
|
18
|
-
list = ["me", "myself"];
|
|
19
|
-
push(list, "I");
|
|
20
|
-
|
|
21
|
-
who = 0;
|
|
22
|
-
greet1 = hello(who || "world");
|
|
23
|
-
|
|
24
|
-
who = "Jason";
|
|
25
|
-
greet2 = hello(who || "world");
|
|
26
|
-
|
|
27
|
-
greet3 = arrow(hello(who || "world"));
|
|
28
|
-
|
|
29
|
-
olsenTimezoneBrisbane = "Australia/Brisbane";
|
|
30
|
-
olsenTimezoneLA = "America/Los_Angeles";
|
|
31
|
-
|
|
32
|
-
dateNowUTC = currentDate();
|
|
33
|
-
// Sat Aug 26 2017 22:56:08 GMT+0000
|
|
34
|
-
|
|
35
|
-
dateNowBrisbane = currentDate(olsenTimezoneBrisbane);
|
|
36
|
-
// Sun Aug 27 2017 08:56:08 GMT+1000
|
|
37
|
-
|
|
38
|
-
dateNowLA = currentDate(olsenTimezoneLA);
|
|
39
|
-
// Sat Aug 26 2017 19:52:35 GMT-0700
|
|
40
|
-
|
|
41
|
-
nowFormattedUTC = formatDate(currentDate(), "MMM D YYYY h:mma z");
|
|
42
|
-
// Aug 27 2017 2:56am UTC
|
|
43
|
-
|
|
44
|
-
nowFormattedBrisbane = formatDate(currentDate(), "MMM D YYYY h:mma z", olsenTimezoneBrisbane);
|
|
45
|
-
// Aug 27 2017 12:56pm AEST
|
|
46
|
-
|
|
47
|
-
iso8601 = "2017-08-27T09:00:57.730+10:00";
|
|
48
|
-
|
|
49
|
-
iso8601FormattedUTC = formatDate(iso8601, "MMM D YYYY h:mma z");
|
|
50
|
-
// "Aug 26 2017 11:00pm UTC"
|
|
51
|
-
|
|
52
|
-
iso8601FormattedLA = formatDate(iso8601, "MMM D YYYY h:mma z", olsenTimezoneLA);
|
|
53
|
-
// "Aug 26 2017 4:00pm PDT"
|
|
54
|
-
|
|
55
|
-
Date = (date) => formatDate(date, "MMM D YYYY h:mma z", olsenTimezoneLA);
|
|
56
|
-
|
|
57
|
-
myDate = Date(iso8601);
|
|
58
|
-
// "Aug 26 2017 4:00pm PDT"
|
|
59
|
-
`;
|
|
60
|
-
|
|
61
|
-
describe("docca-script", () => {
|
|
62
|
-
it("runs a script", () => {
|
|
63
|
-
const vars = {};
|
|
64
|
-
const { env } = run(code, vars);
|
|
65
|
-
|
|
66
|
-
expect(env.arrow).toBeInstanceOf(Function);
|
|
67
|
-
expect(env.hello).toBeInstanceOf(Function);
|
|
68
|
-
|
|
69
|
-
expect(env.list).toEqual(["me", "myself", "I"]);
|
|
70
|
-
|
|
71
|
-
expect(env.greet1).toEqual("hello world");
|
|
72
|
-
|
|
73
|
-
expect(env.greet2).toEqual("hello Jason");
|
|
74
|
-
|
|
75
|
-
expect(env.greet3).toEqual("hello Jason =>");
|
|
76
|
-
|
|
77
|
-
expect(/\+0000$/.test(`${env.dateNowUTC}`)).toBeTruthy();
|
|
78
|
-
expect(/\+1000$/.test(`${env.dateNowBrisbane}`)).toBeTruthy();
|
|
79
|
-
expect(/-0700$/.test(`${env.dateNowLA}`)).toBeTruthy();
|
|
80
|
-
|
|
81
|
-
expect(env.iso8601FormattedUTC).toEqual("Aug 26 2017 11:00pm UTC");
|
|
82
|
-
expect(env.iso8601FormattedLA).toEqual("Aug 26 2017 4:00pm PDT");
|
|
83
|
-
expect(env.myDate).toEqual("Aug 26 2017 4:00pm PDT");
|
|
84
|
-
});
|
|
85
|
-
});
|
package/src/binary-ops.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const binaryOps = {
|
|
2
|
-
"+": (l, r) => l + r,
|
|
3
|
-
"-": (l, r) => l - r,
|
|
4
|
-
"*": (l, r) => l * r,
|
|
5
|
-
"/": (l, r) => l / r,
|
|
6
|
-
|
|
7
|
-
">": (l, r) => l > r,
|
|
8
|
-
"<": (l, r) => l < r,
|
|
9
|
-
"==": (l, r) => l === r,
|
|
10
|
-
"!=": (l, r) => l !== r
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
module.exports = binaryOps;
|
package/src/builtin-functions.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
const moment = require("moment-timezone");
|
|
2
|
-
|
|
3
|
-
const lodash = {
|
|
4
|
-
array: require("lodash/array"),
|
|
5
|
-
collection: require("lodash/collection"),
|
|
6
|
-
math: require("lodash/math"),
|
|
7
|
-
number: require("lodash/number"),
|
|
8
|
-
object: require("lodash/object"),
|
|
9
|
-
string: require("lodash/string")
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
function numberFormatter(...args) {
|
|
13
|
-
return new Intl.NumberFormat(...args).format;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const functions = {
|
|
17
|
-
push(...args) {
|
|
18
|
-
const [target, item] = args;
|
|
19
|
-
target.push(item);
|
|
20
|
-
},
|
|
21
|
-
|
|
22
|
-
shift(...args) {
|
|
23
|
-
const [target] = args;
|
|
24
|
-
return target.shift();
|
|
25
|
-
},
|
|
26
|
-
|
|
27
|
-
currentDate(...args) {
|
|
28
|
-
const [tz] = args;
|
|
29
|
-
return moment().tz(tz || "UTC");
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
formatDate(...args) {
|
|
33
|
-
const [dateString, format, timezone] = args;
|
|
34
|
-
const date = moment.tz(
|
|
35
|
-
/^\d+$/.test(dateString) ? +dateString : dateString,
|
|
36
|
-
timezone
|
|
37
|
-
);
|
|
38
|
-
return date.format(format);
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
numberFormatter,
|
|
42
|
-
|
|
43
|
-
formatDollars: numberFormatter("en-us", {
|
|
44
|
-
minimumFractionDigits: 2,
|
|
45
|
-
maximumFractionDigits: 2
|
|
46
|
-
})
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
// add lodash functions..
|
|
50
|
-
lodash.object.forIn(lodash, category => {
|
|
51
|
-
lodash.object.forIn(category, (func, key) => {
|
|
52
|
-
functions[key] = (...args) => {
|
|
53
|
-
return category[key](...args);
|
|
54
|
-
};
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
module.exports = functions;
|
package/src/environment.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const _has = require("lodash/has");
|
|
2
|
-
const _get = require("lodash/get");
|
|
3
|
-
const _set = require("lodash/set");
|
|
4
|
-
|
|
5
|
-
function Environment(parent) {
|
|
6
|
-
this.vars = Object.create(parent ? parent.vars : null);
|
|
7
|
-
this.parent = parent;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
Environment.prototype = {
|
|
11
|
-
extend() {
|
|
12
|
-
return new Environment(this);
|
|
13
|
-
},
|
|
14
|
-
lookup(name) {
|
|
15
|
-
let scope = this;
|
|
16
|
-
while (scope) {
|
|
17
|
-
if (Object.prototype.hasOwnProperty.call(scope.vars, name)) {
|
|
18
|
-
return scope;
|
|
19
|
-
}
|
|
20
|
-
scope = scope.parent;
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
defined(name) {
|
|
24
|
-
if (name in this.vars) {
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
// `has` does not include inherited properties
|
|
28
|
-
// but does handle "deep" properties
|
|
29
|
-
if (_has(this.vars, name)) {
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
get(name, loc) {
|
|
34
|
-
// `in` includes inherited properties
|
|
35
|
-
if (name in this.vars) {
|
|
36
|
-
return this.vars[name];
|
|
37
|
-
}
|
|
38
|
-
// `has` does not include inherited properties
|
|
39
|
-
// but does handle "deep" properties
|
|
40
|
-
if (_has(this.vars, name)) {
|
|
41
|
-
return _get(this.vars, name);
|
|
42
|
-
}
|
|
43
|
-
throw new Error(`"${name}" is undefined (${loc.line}:${loc.column})`);
|
|
44
|
-
},
|
|
45
|
-
set(name, value, loc) {
|
|
46
|
-
const baseName = name.replace(/\..*/, "");
|
|
47
|
-
let scope = this.lookup(baseName);
|
|
48
|
-
if (!scope && this.parent) {
|
|
49
|
-
throw new Error(`"${name}" is undefined (${loc.line}:${loc.column})`);
|
|
50
|
-
}
|
|
51
|
-
_set((scope || this).vars, name, value);
|
|
52
|
-
return value;
|
|
53
|
-
},
|
|
54
|
-
def(name, value) {
|
|
55
|
-
_set(this.vars, name, value);
|
|
56
|
-
return value;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
module.exports = Environment;
|
package/src/evaluate.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
// const set = require("lodash/set");
|
|
2
|
-
const forEach = require("lodash/forEach");
|
|
3
|
-
// const cloneDeep = require("lodash/cloneDeep");
|
|
4
|
-
|
|
5
|
-
const binaryOps = require("./binary-ops");
|
|
6
|
-
const logicOps = require("./logic-ops");
|
|
7
|
-
|
|
8
|
-
const DEBUG = false;
|
|
9
|
-
|
|
10
|
-
function makeFunc(params, body, env, functions) {
|
|
11
|
-
DEBUG && console.log({ params, body });
|
|
12
|
-
// let localEnv = cloneDeep(env);
|
|
13
|
-
const scope = env.extend();
|
|
14
|
-
return (...args) => {
|
|
15
|
-
params.forEach((param, idx) => {
|
|
16
|
-
scope.def(param, idx < args.length ? args[idx] : false);
|
|
17
|
-
});
|
|
18
|
-
return evaluate(body, scope, functions);
|
|
19
|
-
};
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const evals = {
|
|
23
|
-
ArrowFunctionExpression(exp, env, functions) {
|
|
24
|
-
const params = exp.params.map(param => evaluate(param));
|
|
25
|
-
DEBUG && console.log({ exp, env, params: exp.params });
|
|
26
|
-
return makeFunc(params, exp.body, env, functions);
|
|
27
|
-
},
|
|
28
|
-
ArrayExpression(exp, env, functions) {
|
|
29
|
-
return exp.elements.map(el => evaluate(el, env, functions));
|
|
30
|
-
},
|
|
31
|
-
AssignmentExpression(exp, env, functions) {
|
|
32
|
-
const target = evaluate(exp.left);
|
|
33
|
-
return env.set(target, evaluate(exp.right, env, functions), exp.loc.start);
|
|
34
|
-
},
|
|
35
|
-
BinaryExpression(exp, env, functions) {
|
|
36
|
-
DEBUG && console.log({ exp, env });
|
|
37
|
-
const left = evaluate(exp.left, env, functions);
|
|
38
|
-
const right = evaluate(exp.right, env, functions);
|
|
39
|
-
return binaryOps[exp.operator](left, right);
|
|
40
|
-
},
|
|
41
|
-
BlockStatement(exp, env, functions) {
|
|
42
|
-
DEBUG && console.log({ exp, env });
|
|
43
|
-
let result;
|
|
44
|
-
forEach(exp.body, part => {
|
|
45
|
-
result = evaluate(part, env, functions);
|
|
46
|
-
if (part.type === "ReturnStatement") {
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
return result;
|
|
51
|
-
},
|
|
52
|
-
CallExpression(exp, env, functions) {
|
|
53
|
-
const func = evaluate(exp.callee);
|
|
54
|
-
const args = exp.arguments.map(arg => evaluate(arg, env, functions));
|
|
55
|
-
DEBUG && console.log({ exp, env, func, args });
|
|
56
|
-
if (functions[func]) {
|
|
57
|
-
return functions[func](...args);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (env.defined(func)) {
|
|
61
|
-
const customFunc = env.get(func, exp.loc.start);
|
|
62
|
-
if (typeof customFunc === "function") {
|
|
63
|
-
return customFunc(...args);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const loc = exp.loc.start;
|
|
67
|
-
throw new Error(`${func} is not a function (${loc.line}:${loc.column})`);
|
|
68
|
-
},
|
|
69
|
-
ExpressionStatement(exp, env, functions) {
|
|
70
|
-
return evaluate(exp.expression, env, functions);
|
|
71
|
-
},
|
|
72
|
-
File(exp, env, functions) {
|
|
73
|
-
return evaluate(exp.program, env, functions);
|
|
74
|
-
},
|
|
75
|
-
FunctionDeclaration(exp, env, functions) {
|
|
76
|
-
const name = evaluate(exp.id);
|
|
77
|
-
const params = exp.params.map(param => evaluate(param));
|
|
78
|
-
DEBUG && console.log({ exp, env, name, params });
|
|
79
|
-
env.set(name, makeFunc(params, exp.body, env, functions), exp.loc.start);
|
|
80
|
-
},
|
|
81
|
-
Identifier(exp, env, functions) {
|
|
82
|
-
DEBUG && console.log({ exp, env });
|
|
83
|
-
if (env) {
|
|
84
|
-
return env.get(exp.name, exp.loc.start);
|
|
85
|
-
}
|
|
86
|
-
return exp.name;
|
|
87
|
-
},
|
|
88
|
-
IfStatement(exp, env, functions) {
|
|
89
|
-
const test = evaluate(exp.test, env, functions);
|
|
90
|
-
if (test) {
|
|
91
|
-
return evaluate(exp.consequent, env, functions);
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
|
-
Literal(exp) {
|
|
95
|
-
DEBUG && console.log({ exp });
|
|
96
|
-
return exp.value;
|
|
97
|
-
},
|
|
98
|
-
LogicalExpression(exp, env, functions) {
|
|
99
|
-
DEBUG && console.log({ exp, env });
|
|
100
|
-
const left = evaluate(exp.left, env, functions);
|
|
101
|
-
const right = evaluate(exp.right, env, functions);
|
|
102
|
-
return logicOps[exp.operator](left, right);
|
|
103
|
-
},
|
|
104
|
-
MemberExpression(exp, env, functions) {
|
|
105
|
-
if (!env) {
|
|
106
|
-
const obj = evaluate(exp.object);
|
|
107
|
-
const prop = evaluate(exp.property);
|
|
108
|
-
return `${obj}.${prop}`;
|
|
109
|
-
}
|
|
110
|
-
const obj = evaluate(exp.object, env, functions);
|
|
111
|
-
if (exp.computed) {
|
|
112
|
-
const prop = evaluate(exp.property, env, functions);
|
|
113
|
-
return obj[prop];
|
|
114
|
-
}
|
|
115
|
-
return obj[evaluate(exp.property)];
|
|
116
|
-
// const prop = evaluate(exp.property, obj, functions);
|
|
117
|
-
// DEBUG && console.log({ exp, env, obj, prop });
|
|
118
|
-
// if (exp.computed) {
|
|
119
|
-
// return obj[prop];
|
|
120
|
-
// }
|
|
121
|
-
// return prop;
|
|
122
|
-
},
|
|
123
|
-
ObjectExpression(exp, env, functions) {
|
|
124
|
-
DEBUG && console.log({ exp, env });
|
|
125
|
-
return exp.properties.reduce((res, prop) => {
|
|
126
|
-
res[prop.key.name] = evaluate(prop.value, env, functions);
|
|
127
|
-
return res;
|
|
128
|
-
}, {});
|
|
129
|
-
},
|
|
130
|
-
Program(exp, env, functions) {
|
|
131
|
-
DEBUG && console.log({ exp, env });
|
|
132
|
-
// only return the result of the last "line"
|
|
133
|
-
let result;
|
|
134
|
-
exp.body.forEach(exp => {
|
|
135
|
-
result = evaluate(exp, env, functions);
|
|
136
|
-
});
|
|
137
|
-
return result;
|
|
138
|
-
},
|
|
139
|
-
ReturnStatement(exp, env, functions) {
|
|
140
|
-
DEBUG && console.log({ exp, env });
|
|
141
|
-
return evaluate(exp.argument, env, functions);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
function evaluate(exp, env, functions) {
|
|
146
|
-
if (evals[exp.type]) {
|
|
147
|
-
return evals[exp.type](exp, env, functions);
|
|
148
|
-
}
|
|
149
|
-
throw new Error("Cannot evaluate " + exp.type);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
module.exports = evaluate;
|
package/src/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const babylon = require("babylon");
|
|
2
|
-
const evaluate = require("./evaluate");
|
|
3
|
-
const builtinFuncs = require("./builtin-functions");
|
|
4
|
-
const Environment = require("./environment");
|
|
5
|
-
|
|
6
|
-
function parse(code) {
|
|
7
|
-
return babylon.parse(code, {
|
|
8
|
-
sourceType: "script",
|
|
9
|
-
plugins: ["estree"]
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function runner({ functions = builtinFuncs } = {}) {
|
|
14
|
-
return (code, env = {}, debug) => {
|
|
15
|
-
const ast = parse(code);
|
|
16
|
-
debug && console.log(JSON.stringify(ast.program.body, null, 2));
|
|
17
|
-
const globalEnv = new Environment();
|
|
18
|
-
Object.keys(env).forEach(key => {
|
|
19
|
-
globalEnv.def(key, env[key]);
|
|
20
|
-
});
|
|
21
|
-
const result = evaluate(ast, globalEnv, functions);
|
|
22
|
-
return { result, env: globalEnv.vars };
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
module.exports = runner;
|