Skip to content

update static-eval, closes #34, #35 #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: node_js
node_js:
- "0.8"
- 9
- 8
- 6
- 4
- "0.10"
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,31 @@ module.exports = function parse (modules, opts) {

var xvars = copy(vars);
xvars[node.name] = val;

var res = evaluate(cur, xvars);
if (res === undefined && cur.type === 'CallExpression') {
// static-eval can't safely evaluate code with callbacks, so do it manually in a safe way
var callee = evaluate(cur.callee, xvars);
var args = cur.arguments.map(function (arg) {
// Return a function stub for callbacks so that `static-module` users
// can do `callback.toString()` and get the original source
if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
var fn = function () {
throw new Error('static-module: cannot call callbacks defined inside source code');
};
fn.toString = function () {
return body.slice(arg.start, arg.end);
};
return fn;
}
return evaluate(arg, xvars);
});

if (callee !== undefined) {
res = callee.apply(null, args);
}
}

if (res !== undefined) {
updates.push({
start: cur.start,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"quote-stream": "~1.0.2",
"readable-stream": "~2.3.3",
"shallow-copy": "~0.0.1",
"static-eval": "~0.2.0",
"static-eval": "^2.0.0",
"through2": "~2.0.3"
},
"devDependencies": {
Expand Down