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

Commit

Permalink
feat: support RestParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 7, 2018
1 parent 41250fb commit 35390fd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,29 @@ It base on [https://github.com/bramblex/jsjs](https://github.com/bramblex/jsjs)

* [x] ECMA5
* [x] es2015
* [x] es2015-let-const
* [x] es2015-modules-commonjs
* [x] check-es2015-constants
* [x] es2015-block-scoped-functions
* [x] es2015-arrow-functions
* [x] es2015-block-scoping
* [x] es2015-classes
* [x] es2015-computed-properties
* [x] es2015-destructuring
* [ ] es2015-for-of
* [x] es2015-function-name
* [x] es2015-literals
* [x] es2015-object-super
* [ ] es2015-parameters
* [x] es2015-shorthand-properties
* [x] es2015-spread
* [x] es2015-template-literals
* [ ] es2015-unicode-regex (Depends on environment)
* [x] let and const
* [x] import and export
* [x] block scoped functions
* [x] arrow functions
* [x] block scoping
* [x] classes
* [x] computed properties
* [x] destructuring
* [ ] for-of
* [x] function name
* [x] literals
* [x] object super
* [x] default and rest-parameters
* [x] shorthand properties
* [x] spread
* [x] template literals
* [ ] unicode-regex (Depends on environment)
* [ ] es2016
* [ ] exponentiation-operator
* [ ] es2017
* [ ] async-to-generator
* [ ] es2018
* [ ] es2018-asynchronous-iteration
* [ ] asynchronous-iteration
* [ ] Promise.prototype.finally() (Depends on environment)
* [ ] s (dotAll) flag for regular expressions (Depends on environment)
* [ ] RegExp named capture groups (Depends on environment)
Expand Down
15 changes: 11 additions & 4 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,19 @@ const evaluate_map = {
}
},
FunctionExpression(node: types.FunctionExpression, scope: Scope, arg) {
const func = function(...args) {
const func = function(..._arguments) {
const newScope = scope.$child("function");
newScope.invasived = true;
for (let i = 0; i < node.params.length; i++) {
const param = node.params[i];
if (types.isIdentifier(param)) {
newScope.$const(param.name, args[i]);
newScope.$const(param.name, _arguments[i]);
} else if (types.isAssignmentPattern(param)) {
// @es2015 default and rest parameters
evaluate(param, newScope, {...arg, value: args[i]});
// @es2015 default parameters
evaluate(param, newScope, {...arg, value: _arguments[i]});
} else if (types.isRestElement(param)) {
// @es2015 rest parameters
evaluate(param, newScope, {...arg, value: _arguments.slice(i)});
}
}
newScope.$const("this", this);
Expand Down Expand Up @@ -916,6 +919,10 @@ const evaluate_map = {
node.left.name,
value === undefined ? evaluate(node.right, scope, arg) : value
);
},
RestElement(node: types.RestElement, scope: Scope, arg) {
const {value} = arg;
scope.$const((<types.Identifier>node.argument).name, value);
}
};

Expand Down
33 changes: 33 additions & 0 deletions test/RestParameter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import test from "ava";

import vm from "../src/vm";

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

const func: any = vm.runInContext(
`
function say(...families){
return "hello " + families.join(",");
};
module.exports = say;
`,
sandbox
);
t.deepEqual(func("a", "b"), "hello a,b");
});

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

const func: any = vm.runInContext(
`
function say(...families){
return families;
};
module.exports = say;
`,
sandbox
);
t.deepEqual(func("a", "b"), ["a", "b"]);
});

0 comments on commit 35390fd

Please sign in to comment.