forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Object Spread Syntax (babel#7777)
* Use Object Spread Syntax * Nits
- Loading branch information
1 parent
037fee8
commit 2afe940
Showing
37 changed files
with
263 additions
and
70 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
codemods/babel-plugin-codemod-object-assign-to-object-spread/.npmignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
src | ||
test | ||
*.log |
57 changes: 57 additions & 0 deletions
57
codemods/babel-plugin-codemod-object-assign-to-object-spread/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# @babel/plugin-codemod-object-assign-to-object-spread | ||
|
||
Transforms old code that uses `Object.assign` with an Object Literal as | ||
the first param to use Object Spread syntax. | ||
|
||
## Examples | ||
|
||
```js | ||
const obj = Object.assign({ | ||
test1: 1, | ||
}, other, { | ||
test2: 2, | ||
}, other2); | ||
``` | ||
|
||
Is transformed to: | ||
|
||
```js | ||
const obj = { | ||
test1: 1, | ||
...other, | ||
test2: 2, | ||
...other2, | ||
}; | ||
``` | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install --save-dev @babel/plugin-codemod-object-assign-to-object-spread | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
```json | ||
{ | ||
"plugins": ["@babel/plugin-codemod-object-assign-to-object-spread"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
babel --plugins @babel/plugin-codemod-object-assign-to-object-spread script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("@babel/core").transform("code", { | ||
plugins: ["@babel/plugin-codemod-object-assign-to-object-spread"] | ||
}); | ||
``` |
22 changes: 22 additions & 0 deletions
22
codemods/babel-plugin-codemod-object-assign-to-object-spread/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@babel/plugin-codemod-object-assign-to-object-spread", | ||
"version": "7.0.0-beta.44", | ||
"description": "Transforms Object.assign into object spread syntax", | ||
"repository": "https://github.com/babel/babel/tree/master/codemods/babel-plugin-codemod-object-assign-to-object-spread", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"@babel/codemod", | ||
"@babel/plugin" | ||
], | ||
"dependencies": { | ||
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.44" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "7.0.0-beta.44" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "7.0.0-beta.44", | ||
"@babel/helper-plugin-test-runner": "7.0.0-beta.44" | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
codemods/babel-plugin-codemod-object-assign-to-object-spread/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import syntaxObjectRestSpread from "@babel/plugin-syntax-object-rest-spread"; | ||
|
||
export default function({ types: t }) { | ||
return { | ||
inherits: syntaxObjectRestSpread, | ||
|
||
visitor: { | ||
CallExpression(path) { | ||
if (!path.get("callee").matchesPattern("Object.assign")) return; | ||
|
||
const args = path.get("arguments"); | ||
if (args.length === 0) return; | ||
|
||
const [objPath] = args; | ||
if (!objPath.isObjectExpression()) return; | ||
|
||
const obj = objPath.node; | ||
const { properties } = obj; | ||
|
||
for (let i = 1; i < args.length; i++) { | ||
const arg = args[i]; | ||
const { node } = arg; | ||
|
||
if (arg.isObjectExpression()) { | ||
properties.push(...node.properties); | ||
} else { | ||
properties.push(t.spreadElement(node)); | ||
} | ||
} | ||
|
||
path.replaceWith(obj); | ||
}, | ||
}, | ||
}; | ||
} |
1 change: 1 addition & 0 deletions
1
...n-to-object-spread/test/fixtures/object-assign-to-object-spread/multiple-objects/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Object.assign({test: 1}, {test: 2}); |
3 changes: 3 additions & 0 deletions
3
...-object-spread/test/fixtures/object-assign-to-object-spread/multiple-objects/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../../lib"] | ||
} |
4 changes: 4 additions & 0 deletions
4
...-to-object-spread/test/fixtures/object-assign-to-object-spread/multiple-objects/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
({ | ||
test: 1, | ||
test: 2 | ||
}); |
1 change: 1 addition & 0 deletions
1
...ct-assign-to-object-spread/test/fixtures/object-assign-to-object-spread/multiple/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Object.assign({test: 1}, test2, {test: 2}, test3); |
3 changes: 3 additions & 0 deletions
3
...ssign-to-object-spread/test/fixtures/object-assign-to-object-spread/multiple/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../../lib"] | ||
} |
6 changes: 6 additions & 0 deletions
6
...t-assign-to-object-spread/test/fixtures/object-assign-to-object-spread/multiple/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
({ | ||
test: 1, | ||
...test2, | ||
test: 2, | ||
...test3 | ||
}); |
1 change: 1 addition & 0 deletions
1
...-to-object-spread/test/fixtures/object-assign-to-object-spread/single-non-object/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Object.assign(test); |
3 changes: 3 additions & 0 deletions
3
...object-spread/test/fixtures/object-assign-to-object-spread/single-non-object/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../../lib"] | ||
} |
1 change: 1 addition & 0 deletions
1
...to-object-spread/test/fixtures/object-assign-to-object-spread/single-non-object/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Object.assign(test); |
1 change: 1 addition & 0 deletions
1
...ject-assign-to-object-spread/test/fixtures/object-assign-to-object-spread/single/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Object.assign({test: 1}); |
3 changes: 3 additions & 0 deletions
3
...-assign-to-object-spread/test/fixtures/object-assign-to-object-spread/single/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["../../../../lib"] | ||
} |
3 changes: 3 additions & 0 deletions
3
...ect-assign-to-object-spread/test/fixtures/object-assign-to-object-spread/single/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
({ | ||
test: 1 | ||
}); |
3 changes: 3 additions & 0 deletions
3
codemods/babel-plugin-codemod-object-assign-to-object-spread/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import runner from "@babel/helper-plugin-test-runner"; | ||
|
||
runner(__dirname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
codemods/babel-plugin-codemod-optional-catch-binding/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.