code_to_derive = code_or_ast => {
try {
let cell_ast =
typeof code_or_ast === "string"
? parser.parseCell(code_or_ast)
: code_or_ast;
let cell_code =
typeof code_or_ast === "string"
? code_or_ast.trim()
: code_or_ast.input.slice(code_or_ast.start, code_or_ast.end).trim();
let reference_names = (cell_ast.references || []).map(x => x.name);
if (cell_ast.id == null) {
return {
using: reference_names,
derive: new Function(
...reference_names,
`return ` + generate(cell_ast.body)
)
};
} else if (cell_ast.body.type === 'BlockStatement') {
return {
using: reference_names,
derive: new Function(...reference_names, generate(cell_ast.body))
};
} else if (cell_ast.body.type === 'ImportDeclaration') {
throw new Error('Imports not yet supported!');
let import_ast = cell_ast.body;
return import_ast.specifiers.map(specifier => {
return {
define: specifier.local.name,
module: import_ast.source.value
};
});
} else {
return {
using: reference_names,
derive: new Function(
...reference_names,
`return ` + generate(cell_ast.body)
)
};
}
} catch (error) {
error.stack = '';
return {
using: [],
derive: () => error
};
}
}