Skip to content

Commit 2afe940

Browse files
authored
Use Object Spread Syntax (babel#7777)
* Use Object Spread Syntax * Nits
1 parent 037fee8 commit 2afe940

File tree

37 files changed

+263
-70
lines changed

37 files changed

+263
-70
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src
2+
test
3+
*.log
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# @babel/plugin-codemod-object-assign-to-object-spread
2+
3+
Transforms old code that uses `Object.assign` with an Object Literal as
4+
the first param to use Object Spread syntax.
5+
6+
## Examples
7+
8+
```js
9+
const obj = Object.assign({
10+
test1: 1,
11+
}, other, {
12+
test2: 2,
13+
}, other2);
14+
```
15+
16+
Is transformed to:
17+
18+
```js
19+
const obj = {
20+
test1: 1,
21+
...other,
22+
test2: 2,
23+
...other2,
24+
};
25+
```
26+
27+
## Installation
28+
29+
```sh
30+
npm install --save-dev @babel/plugin-codemod-object-assign-to-object-spread
31+
```
32+
33+
## Usage
34+
35+
### Via `.babelrc` (Recommended)
36+
37+
**.babelrc**
38+
39+
```json
40+
{
41+
"plugins": ["@babel/plugin-codemod-object-assign-to-object-spread"]
42+
}
43+
```
44+
45+
### Via CLI
46+
47+
```sh
48+
babel --plugins @babel/plugin-codemod-object-assign-to-object-spread script.js
49+
```
50+
51+
### Via Node API
52+
53+
```javascript
54+
require("@babel/core").transform("code", {
55+
plugins: ["@babel/plugin-codemod-object-assign-to-object-spread"]
56+
});
57+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@babel/plugin-codemod-object-assign-to-object-spread",
3+
"version": "7.0.0-beta.44",
4+
"description": "Transforms Object.assign into object spread syntax",
5+
"repository": "https://github.com/babel/babel/tree/master/codemods/babel-plugin-codemod-object-assign-to-object-spread",
6+
"license": "MIT",
7+
"main": "lib/index.js",
8+
"keywords": [
9+
"@babel/codemod",
10+
"@babel/plugin"
11+
],
12+
"dependencies": {
13+
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.44"
14+
},
15+
"peerDependencies": {
16+
"@babel/core": "7.0.0-beta.44"
17+
},
18+
"devDependencies": {
19+
"@babel/core": "7.0.0-beta.44",
20+
"@babel/helper-plugin-test-runner": "7.0.0-beta.44"
21+
}
22+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread";
2+
3+
export default function({ types: t }) {
4+
return {
5+
inherits: syntaxObjectRestSpread,
6+
7+
visitor: {
8+
CallExpression(path) {
9+
if (!path.get("callee").matchesPattern("Object.assign")) return;
10+
11+
const args = path.get("arguments");
12+
if (args.length === 0) return;
13+
14+
const [objPath] = args;
15+
if (!objPath.isObjectExpression()) return;
16+
17+
const obj = objPath.node;
18+
const { properties } = obj;
19+
20+
for (let i = 1; i < args.length; i++) {
21+
const arg = args[i];
22+
const { node } = arg;
23+
24+
if (arg.isObjectExpression()) {
25+
properties.push(...node.properties);
26+
} else {
27+
properties.push(t.spreadElement(node));
28+
}
29+
}
30+
31+
path.replaceWith(obj);
32+
},
33+
},
34+
};
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Object.assign({test: 1}, {test: 2});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["../../../../lib"]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
({
2+
test: 1,
3+
test: 2
4+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Object.assign({test: 1}, test2, {test: 2}, test3);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["../../../../lib"]
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
({
2+
test: 1,
3+
...test2,
4+
test: 2,
5+
...test3
6+
});

0 commit comments

Comments
 (0)