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

Commit

Permalink
feat: support SpreadElement
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 5, 2018
1 parent c87c170 commit f373172
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.26.0"
"babel-core": "^6.26.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0"
},
"devDependencies": {
"@types/babel-core": "^6.25.3",
"@types/node": "^9.4.6",
"ava": "^0.25.0",
"typescript": "^2.7.2"
}
}
6 changes: 5 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
_classCallCheck,
_createClass,
_possibleConstructorReturn,
_inherits
_inherits,
_extends
} from "./runtime";

const BREAK_SINGAL: {} = {};
Expand Down Expand Up @@ -647,6 +648,9 @@ const evaluate_map = {
},
Super(node: types.Super, scope: Scope) {
return function() {};
},
SpreadElement(node: types.SpreadElement, scope: Scope) {
return evaluate(node.argument, scope);
}
};

Expand Down
16 changes: 15 additions & 1 deletion src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ export function _inherits(subClass, superClass) {
Object.setPrototypeOf
? Object.setPrototypeOf(subClass, superClass)
: (subClass.__proto__ = superClass);
}
}

export const _extends =
Object.assign ||
function(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
return target;
};
4 changes: 3 additions & 1 deletion src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class Vm {
scope.$const("module", $module);
scope.$const("exports", $exports);

const {ast} = transform(code);
const {ast} = transform(code, {
plugins: ["transform-object-rest-spread"]
});

ast && evaluate(ast, scope);

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

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

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

const obj: any = vm.runInContext(
`
const obj = {
isTrue: false
};
module.exports = {...obj};
`,
sandbox
);

t.true(typeof obj.isTrue === "boolean");
t.false(obj.isTrue);
});

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

const obj: any = vm.runInContext(
`
const obj1 = {
1: true,
2: false
};
const obj2 = {
1: false,
2: true,
name: "hello"
};
module.exports = {...obj1, ...obj2};
`,
sandbox
);

t.false(obj[1]);
t.true(obj[2]);
t.deepEqual(obj.name, "hello");
});
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ babel-plugin-syntax-exponentiation-operator@^6.8.0:
version "6.13.0"
resolved "https://registry.npmjs.org/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"

babel-plugin-syntax-object-rest-spread@^6.13.0:
babel-plugin-syntax-object-rest-spread@^6.13.0, babel-plugin-syntax-object-rest-spread@^6.8.0:
version "6.13.0"
resolved "https://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz#fd6536f2bce13836ffa3a5458c4903a597bb3bf5"

Expand Down Expand Up @@ -564,6 +564,13 @@ babel-plugin-transform-exponentiation-operator@^6.8.0:
babel-plugin-syntax-exponentiation-operator "^6.8.0"
babel-runtime "^6.22.0"

babel-plugin-transform-object-rest-spread@^6.26.0:
version "6.26.0"
resolved "https://registry.npmjs.org/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz#0f36692d50fef6b7e2d4b3ac1478137a963b7b06"
dependencies:
babel-plugin-syntax-object-rest-spread "^6.8.0"
babel-runtime "^6.26.0"

babel-plugin-transform-strict-mode@^6.24.1:
version "6.24.1"
resolved "https://registry.npmjs.org/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz#d5faf7aa578a65bbe591cf5edae04a0c67020758"
Expand Down

0 comments on commit f373172

Please sign in to comment.