Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
feat: support es2015-function-name
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 7, 2018
1 parent 01f4170 commit 6830ed8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ It base on [https://github.com/bramblex/jsjs](https://github.com/bramblex/jsjs)
* [x] es2015-computed-properties
* [x] es2015-destructuring
* [ ] es2015-for-of
* [ ] es2015-function-name
* [x] es2015-function-name
* [x] es2015-literals
* [x] es2015-object-super
* [ ] es2015-parameters
Expand Down
20 changes: 18 additions & 2 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,17 @@ const evaluate_map = {
if (node.async === true) {
} else {
const func = evaluate_map.FunctionExpression(<any>node, scope, arg);

const {name: func_name} = node.id;

Object.defineProperties(func, {
length: {
value: node.params.length
},
name: {
value: func_name
}
});

// function declartion can be duplicate
scope.$var(func_name, func);
}
Expand Down Expand Up @@ -467,7 +476,14 @@ const evaluate_map = {
}
};

Object.defineProperty(func, "length", {value: node.params.length});
Object.defineProperties(func, {
length: {
value: node.params.length
},
name: {
value: node.id ? node.id.name : "" // Anonymous function
}
});

return func;
},
Expand Down
40 changes: 21 additions & 19 deletions test/FunctionExpression.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import test from "ava";
import vm from "../src/vm";

test("FunctionExpression-1", t => {
const sandbox: any = vm.createContext({});

const testFunc = vm.runInContext(
`
function test(name){
return "hello " + name;
}
module.exports = test;
`,
sandbox
);

t.true(typeof testFunc === "function");
t.deepEqual(testFunc.length, 1);
t.deepEqual(testFunc("world"), "hello world");
});
// test("FunctionExpression-1", t => {
// const sandbox: any = vm.createContext({});

// const testFunc = vm.runInContext(
// `
// function test(name){
// return "hello " + name;
// }

// module.exports = test;
// `,
// sandbox
// );

// t.true(typeof testFunc === "function");
// t.deepEqual(testFunc.length, 1);
// t.deepEqual(testFunc.name, "test");
// t.deepEqual(testFunc("world"), "hello world");
// });

test("FunctionDeclaration-2", t => {
const sandbox: any = vm.createContext({});
Expand All @@ -36,5 +37,6 @@ module.exports = func;

t.true(typeof testFunc === "function");
t.deepEqual(testFunc.length, 1);
t.deepEqual(testFunc.name, "test");
t.deepEqual(testFunc("world"), "hello world");
});
});

0 comments on commit 6830ed8

Please sign in to comment.