Skip to content

Commit

Permalink
Use Object Spread Syntax (babel#7777)
Browse files Browse the repository at this point in the history
* Use Object Spread Syntax

* Nits
  • Loading branch information
jridgewell authored Apr 24, 2018
1 parent 037fee8 commit 2afe940
Show file tree
Hide file tree
Showing 37 changed files with 263 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
src
test
*.log
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"]
});
```
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"
}
}
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);
},
},
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign({test: 1}, {test: 2});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../lib"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
({
test: 1,
test: 2
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign({test: 1}, test2, {test: 2}, test3);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../lib"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
({
test: 1,
...test2,
test: 2,
...test3
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(test);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../lib"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign(test);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Object.assign({test: 1});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["../../../../lib"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
({
test: 1
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import runner from "@babel/helper-plugin-test-runner";

runner(__dirname);
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"name": "@babel/plugin-codemod-optional-catch-binding",
"version": "7.0.0-beta.46",
"description": "Remove unused catch bindings",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-remove-unused-catch-binding",
"repository": "https://github.com/babel/babel/tree/master/codemods/babel-plugin-codemod-remove-unused-catch-binding",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"@babel/codemod",
"@babel/plugin"
],
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import syntaxOptionalCatchBinding from "@babel/plugin-syntax-optional-catch-binding";
import { types as t } from "@babel/core";

export default function() {
export default function({ types: t }) {
return {
inherits: syntaxOptionalCatchBinding,

Expand Down
5 changes: 3 additions & 2 deletions packages/babel-cli/src/babel/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ export function log(msg, force) {
}

export function transform(filename, code, opts, callback) {
opts = Object.assign({}, opts, {
opts = {
...opts,
filename,
});
};

babel.transform(code, opts, callback);
}
Expand Down
15 changes: 9 additions & 6 deletions packages/babel-code-frame/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ function getMarkerLines(
source: Array<string>,
opts: Object,
): { start: number, end: number, markerLines: Object } {
const startLoc: Location = Object.assign(
{},
{ column: 0, line: -1 },
loc.start,
);
const endLoc: Location = Object.assign({}, startLoc, loc.end);
const startLoc: Location = {
column: 0,
line: -1,
...loc.start,
};
const endLoc: Location = {
...startLoc,
...loc.end,
};
const { linesAbove = 2, linesBelow = 3 } = opts || {};
const startLine = startLoc.line;
const startColumn = startLoc.column;
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-core/src/config/config-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ function emptyChain(): ConfigChain {
}

function normalizeOptions(opts: ValidatedOptions): ValidatedOptions {
const options = Object.assign({}, opts);
const options = {
...opts,
};
delete options.extends;
delete options.env;
delete options.plugins;
Expand Down
13 changes: 10 additions & 3 deletions packages/babel-core/src/config/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ const loadDescriptor = makeWeakCache(

let item = value;
if (typeof value === "function") {
const api = Object.assign({}, context, makeAPI(cache));
const api = {
...context,
...makeAPI(cache),
};
try {
item = value(api, options, dirname);
} catch (e) {
Expand Down Expand Up @@ -229,9 +232,13 @@ const instantiatePlugin = makeWeakCache(
): Plugin => {
const pluginObj = validatePluginObject(value);

const plugin = Object.assign({}, pluginObj);
const plugin = {
...pluginObj,
};
if (plugin.visitor) {
plugin.visitor = traverse.explode(Object.assign({}, plugin.visitor));
plugin.visitor = traverse.explode({
...plugin.visitor,
});
}

if (plugin.inherits) {
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-core/src/transform-file-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export default function transformFileSync(
if (opts == null) {
options = { filename };
} else if (opts && typeof opts === "object") {
options = Object.assign({}, opts, { filename });
options = {
...opts,
filename,
};
}

const config = loadConfig(options);
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-core/src/transform-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export default ((function transformFile(filename, opts, callback) {
if (opts == null) {
options = { filename };
} else if (opts && typeof opts === "object") {
options = Object.assign({}, opts, { filename });
options = {
...opts,
filename,
};
}

process.nextTick(() => {
Expand Down
60 changes: 31 additions & 29 deletions packages/babel-core/src/transformation/normalize-opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,38 @@ export default function normalizeOptions(config: ResolvedConfig): {} {

const opts = config.options;

const options = Object.assign({}, opts, {
parserOpts: Object.assign(
{
sourceType:
path.extname(filenameRelative) === ".mjs" ? "module" : sourceType,
sourceFileName: filename,
plugins: [],
},
opts.parserOpts,
),
generatorOpts: Object.assign(
{
// General generator flags.
filename,
auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
retainLines: opts.retainLines,
comments,
shouldPrintComment: opts.shouldPrintComment,
compact,
minified: opts.minified,
const options = {
...opts,

// Source-map generation flags.
sourceMaps,
sourceRoot,
sourceFileName,
},
opts.generatorOpts,
),
});
parserOpts: {
sourceType:
path.extname(filenameRelative) === ".mjs" ? "module" : sourceType,

sourceFileName: filename,
plugins: [],
...opts.parserOpts,
},

generatorOpts: {
// General generator flags.
filename,

auxiliaryCommentBefore: opts.auxiliaryCommentBefore,
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
retainLines: opts.retainLines,
comments,
shouldPrintComment: opts.shouldPrintComment,
compact,
minified: opts.minified,

// Source-map generation flags.
sourceMaps,

sourceRoot,
sourceFileName,
...opts.generatorOpts,
},
};

for (const plugins of config.passes) {
for (const plugin of plugins) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,8 @@ const handle = {
// Optionally, a memoize method may be defined on the state, which will be
// called when the member is a self-referential update.
export default function memberExpressionToFunctions(path, visitor, state) {
path.traverse(visitor, Object.assign({}, state, handle));
path.traverse(visitor, {
...state,
...handle,
});
}
4 changes: 3 additions & 1 deletion packages/babel-helper-module-imports/src/import-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ export default class ImportInjector {
optsList.push(importedSource);
}

const newOpts = Object.assign({}, this._defaultOpts);
const newOpts = {
...this._defaultOpts,
};
for (const opts of optsList) {
if (!opts) continue;
Object.keys(newOpts).forEach(key => {
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-helper-plugin-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ function copyApiObject(api) {
}
}

return Object.assign({}, proto, api);
return {
...proto,
...api,
};
}

function has(obj, key) {
Expand Down
4 changes: 3 additions & 1 deletion packages/babel-preset-env/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ export const isPluginRequired = (
};

const getBuiltInTargets = targets => {
const builtInTargets = Object.assign({}, targets);
const builtInTargets = {
...targets,
};
if (builtInTargets.uglify != null) {
delete builtInTargets.uglify;
}
Expand Down
Loading

0 comments on commit 2afe940

Please sign in to comment.