Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-transform-replace-object-assign

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-transform-replace-object-assign - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0-beta.0

.all-contributorsrc

27

CHANGELOG.md

@@ -5,2 +5,13 @@ # Changelog

## 2.0.0-beta.0
* Via [#3](https://github.com/newoga/babel-plugin-transform-replace-object-assign/pull/3):
* Updated dependencies.
* New plugin implementation for Babel v7.
* Use [@babel/helper-module-imports](https://npm.im/@babel/helper-module-imports) so the output is either ESM or CJS depending if `sourceType` is `module` or `script`, fixing [#1](https://github.com/newoga/babel-plugin-transform-replace-object-assign/issues/1).
* Added [`@babel/core`](https://npm.im/@babel/core) peer dependency, as Babel does for official plugins.
* Disabled and ignored `package-lock.json`.
* Replaced `lodash.assign` in readme examples with `object-assign` to demo defaults.
* New way to register Babel for Mocha tests.
## 1.0.0

@@ -11,9 +22,9 @@

**This will replace `Object.assign` with `object-assign`:**
```json
{
"plugins": [
["transform-replace-object-assign"]
]
"plugins": [["transform-replace-object-assign"]]
}
```
* **Breaking change:** The plugin configuration no longer supports passing a `string` module name. If you want to provide a custom `Object.assign` implementation, you must provide an `object` config with the name of the module specified in the `moduleSpecifier` key.

@@ -25,7 +36,6 @@

{
"plugins": [
["transform-replace-object-assign", "custom-module"]
]
"plugins": [["transform-replace-object-assign", "custom-module"]]
}
```
**Should be changed to this:**

@@ -36,3 +46,6 @@

"plugins": [
["transform-replace-object-assign", { "moduleSpecifier": "custom-module}" }]
[
"transform-replace-object-assign",
{ "moduleSpecifier": "custom-module" }
]
]

@@ -39,0 +52,0 @@ }

@@ -1,2 +0,2 @@

'use strict';
"use strict";

@@ -6,46 +6,21 @@ Object.defineProperty(exports, "__esModule", {

});
exports.default = _default;
exports.default = function (_ref) {
var _helperModuleImports = require("@babel/helper-module-imports");
function _default(_ref) {
var t = _ref.types;
return {
visitor: {
Program: {
enter: function enter(path, _ref2) {
var file = _ref2.file;
CallExpression: function CallExpression(path, _ref2) {
var file = _ref2.file,
opts = _ref2.opts;
file.set(OBJECT_ASSIGN, false);
},
exit: function exit(path, _ref3) {
var file = _ref3.file,
opts = _ref3.opts;
if (typeof opts === 'string') {
throw new Error('Configuring module specifier with a string is no longer supported. Configure with { "moduleSpecifier": "' + opts + '" } instead of "' + opts + '".');
}
var _opts$moduleSpecifier = opts.moduleSpecifier,
moduleSpecifier = _opts$moduleSpecifier === undefined ? 'object-assign' : _opts$moduleSpecifier;
if (!file.get(OBJECT_ASSIGN) && !path.scope.hasBinding(moduleSpecifier)) {
return;
}
var declar = t.importDeclaration([t.importDefaultSpecifier(file.get(OBJECT_ASSIGN))], t.stringLiteral(moduleSpecifier));
path.node.body.unshift(declar);
}
},
CallExpression: function CallExpression(path, _ref4) {
var file = _ref4.file;
if (path.get('callee').matchesPattern('Object.assign')) {
if (!file.get(OBJECT_ASSIGN)) {
file.set(OBJECT_ASSIGN, path.scope.generateUidIdentifier('objectAssign'));
if (this.importCache) path.node.callee = t.cloneNode(this.importCache);else {
var _opts$moduleSpecifier = opts.moduleSpecifier,
moduleSpecifier = _opts$moduleSpecifier === void 0 ? 'object-assign' : _opts$moduleSpecifier;
this.importCache = (0, _helperModuleImports.addDefault)(file.path, moduleSpecifier);
path.node.callee = this.importCache;
}
path.node.callee = file.get(OBJECT_ASSIGN);
}

@@ -55,4 +30,2 @@ }

};
};
var OBJECT_ASSIGN = 'ObjectAssign';
}
{
"name": "babel-plugin-transform-replace-object-assign",
"version": "1.0.0",
"version": "2.0.0-beta.0",
"description": "Allows you to provide custom implementation of Object.assign in babel builds",

@@ -13,2 +13,4 @@ "repository": {

"scripts": {
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate",
"build": "babel src --out-dir lib",

@@ -27,10 +29,17 @@ "bump-changelog": "./bump-changelog",

],
"peerDependencies": {
"@babel/core": "^7.0.0-beta.42"
},
"dependencies": {
"@babel/helper-module-imports": "^7.0.0-beta.42"
},
"devDependencies": {
"babel-cli": "^6.4.5",
"babel-core": "^6.4.5",
"babel-preset-es2015": "^6.3.13",
"all-contributors-cli": "^4.11.1",
"@babel/cli": "^7.0.0-beta.42",
"@babel/core": "^7.0.0-beta.42",
"@babel/preset-es2015": "^7.0.0-beta.42",
"@babel/register": "^7.0.0-beta.42",
"istanbul": "^0.4.2",
"mocha": "^2.4.5",
"simple-assign": "0.1.0"
"mocha": "^5.0.4"
}
}
## babel-plugin-transform-replace-object-assign
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors)
[![Build Status](https://travis-ci.org/newoga/babel-plugin-transform-replace-object-assign.svg?branch=master)](https://travis-ci.org/newoga/babel-plugin-transform-replace-object-assign)

@@ -15,2 +16,3 @@ [![npm version](https://img.shields.io/npm/v/babel-plugin-transform-replace-object-assign.svg?style=flat-square)](https://www.npmjs.com/package/babel-plugin-transform-replace-object-assign)

#### ⚠️ Important note on the use of this project:
Most likely you do not and should not use this plugin! I initially wrote this plugin due to a [bug](https://bugs.chromium.org/p/v8/issues/detail?id=4118) in Chrome where key order was not gaurenteed to be correct for objects mutated with `Object.assign` (the issue is also described at [sindresorhus/object-assign#22](https://github.com/sindresorhus/object-assign/issues/22)).

@@ -33,3 +35,3 @@

# Install an assign implementation
$ npm install lodash.assign
$ npm install object-assign
```

@@ -39,3 +41,3 @@

When you provide the plugin, also specify which package you would like imported and used when replacing `Object.assign`.
When you provide the plugin, use the `moduleSpecifier` option to specify which package you would like imported and used when replacing `Object.assign`.

@@ -47,3 +49,3 @@ **.babelrc**

"plugins": [
["transform-replace-object-assign", { "moduleSpecifier": "lodash.assign" }]
["transform-replace-object-assign", { "moduleSpecifier": "object-assign" }]
]

@@ -53,2 +55,10 @@ }

To use defaults (which is the same as above):
```json
{
"plugins": ["transform-replace-object-assign"]
}
```
#### Result

@@ -65,6 +75,17 @@

```js
import _lodash from 'lodash.assign';
import _objectAssign from 'object-assign';
_lodash(a, b)
_objectAssign(a, b);
```
```
## Contributors
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore -->
| [<img src="https://avatars2.githubusercontent.com/u/344018?v=4" width="100px;"/><br /><sub><b>Neil Gabbadon</b></sub>](https://github.com/newoga)<br />[💻](https://github.com/newoga/babel-plugin-transform-replace-object-assign/commits?author=newoga "Code") [📖](https://github.com/newoga/babel-plugin-transform-replace-object-assign/commits?author=newoga "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/389286?v=4" width="100px;"/><br /><sub><b>Ivan Nikolić</b></sub>](http://ivannikolic.com)<br />[🐛](https://github.com/newoga/babel-plugin-transform-replace-object-assign/issues?q=author%3Aniksy "Bug reports") | [<img src="https://avatars1.githubusercontent.com/u/45469?v=4" width="100px;"/><br /><sub><b>Jordan Harband</b></sub>](https://twitter.com/ljharb)<br />[🤔](#ideas-ljharb "Ideas, Planning, & Feedback") | [<img src="https://avatars0.githubusercontent.com/u/1754873?v=4" width="100px;"/><br /><sub><b>Jayden Seric</b></sub>](https://jaydenseric.com)<br />[💻](https://github.com/newoga/babel-plugin-transform-replace-object-assign/commits?author=jaydenseric "Code") [📖](https://github.com/newoga/babel-plugin-transform-replace-object-assign/commits?author=jaydenseric "Documentation") |
| :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!

@@ -1,38 +0,15 @@

const OBJECT_ASSIGN = 'ObjectAssign';
import { addDefault } from '@babel/helper-module-imports';
export default function ({types: t}) {
export default function({ types: t }) {
return {
visitor: {
Program: {
enter(path, {file}) {
file.set(OBJECT_ASSIGN, false);
},
exit(path, {file, opts}) {
if (typeof opts === 'string') {
throw new Error(`Configuring module specifier with a string is no longer supported. Configure with { "moduleSpecifier": "${opts}" } instead of "${opts}".`);
}
const { moduleSpecifier = 'object-assign' } = opts;
if (!file.get(OBJECT_ASSIGN) && !path.scope.hasBinding(moduleSpecifier)) {
return;
}
const declar = t.importDeclaration([
t.importDefaultSpecifier(file.get(OBJECT_ASSIGN)),
], t.stringLiteral(moduleSpecifier));
path.node.body.unshift(declar);
}
},
CallExpression(path, {file}) {
CallExpression(path, { file, opts }) {
if (path.get('callee').matchesPattern('Object.assign')) {
if (!file.get(OBJECT_ASSIGN)) {
file.set(OBJECT_ASSIGN, path.scope.generateUidIdentifier('objectAssign'));
if (this.importCache)
path.node.callee = t.cloneNode(this.importCache);
else {
const { moduleSpecifier = 'object-assign' } = opts;
this.importCache = addDefault(file.path, moduleSpecifier);
path.node.callee = this.importCache;
}
path.node.callee = file.get(OBJECT_ASSIGN);
}

@@ -39,0 +16,0 @@ }

@@ -1,3 +0,3 @@

import assign from "simple-assign";
import assign from 'simple-assign';
export default Object.assign || assign;

@@ -6,9 +6,10 @@ "use strict";

});
exports.default = void 0;
var _simpleAssign = require("simple-assign");
var _simpleAssign = _interopRequireDefault(require("simple-assign"));
var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = Object.assign || _simpleAssign2.default;
var _default = Object.assign || _simpleAssign.default;
exports.default = _default;

@@ -1,2 +0,3 @@

Object.assign({a: 1}, {b: 2});
Object.assign({c: 3}, {d: 4});
Object.assign({ a: 1 }, { b: 2 });
Object.assign({ c: 3 }, { d: 4 });
Object.assign({ e: 5 }, { f: 6 });
"use strict";
var _simpleAssign = require("simple-assign");
var _simpleAssign = _interopRequireDefault(require("simple-assign"));
var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
(0, _simpleAssign2.default)({ a: 1 }, { b: 2 });
(0, _simpleAssign2.default)({ c: 3 }, { d: 4 });
(0, _simpleAssign.default)({
a: 1
}, {
b: 2
});
(0, _simpleAssign.default)({
c: 3
}, {
d: 4
});
(0, _simpleAssign.default)({
e: 5
}, {
f: 6
});

@@ -1,1 +0,1 @@

Object.assign({'a': 1}, {'b': 2});
Object.assign({ a: 1 }, { b: 2 });

@@ -1,9 +0,11 @@

'use strict';
"use strict";
var _simpleAssign = require('simple-assign');
var _simpleAssign = _interopRequireDefault(require("simple-assign"));
var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
(0, _simpleAssign2.default)({ 'a': 1 }, { 'b': 2 });
(0, _simpleAssign.default)({
a: 1
}, {
b: 2
});

@@ -1,1 +0,1 @@

Object.assign({'a': 1}, {'b': 2});
Object.assign({ a: 1 }, { b: 2 });

@@ -1,9 +0,11 @@

'use strict';
"use strict";
var _objectAssign2 = require('object-assign');
var _objectAssign = _interopRequireDefault(require("object-assign"));
var _objectAssign3 = _interopRequireDefault(_objectAssign2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
(0, _objectAssign3.default)({ 'a': 1 }, { 'b': 2 });
(0, _objectAssign.default)({
a: 1
}, {
b: 2
});

@@ -1,3 +0,3 @@

import assign from "simple-assign";
import assign from 'simple-assign';
Object.assign({a: 1}, {b: 2});
Object.assign({ a: 1 }, { b: 2 });
"use strict";
var _simpleAssign = require("simple-assign");
var _simpleAssign = _interopRequireDefault(require("simple-assign"));
var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
(0, _simpleAssign2.default)({ a: 1 }, { b: 2 });
(0, _simpleAssign.default)({
a: 1
}, {
b: 2
});
import path from 'path';
import fs from 'fs';
import assert from 'assert';
import {transformFileSync} from 'babel-core';
import { transformFileSync } from '@babel/core';
import plugin from '../src/index';

@@ -13,3 +13,3 @@

const fixturesDir = path.join(__dirname, 'fixtures');
fs.readdirSync(fixturesDir).map((caseName) => {
fs.readdirSync(fixturesDir).map(caseName => {
it(`should ${caseName.split('-').join(' ')}`, () => {

@@ -22,7 +22,7 @@ const fixtureDir = path.join(fixturesDir, caseName);

const actual = transformFileSync(path.join(fixtureDir, 'actual.js'), {
plugins: [
pluginConfig
]
plugins: [pluginConfig]
}).code;
const expected = fs.readFileSync(path.join(fixtureDir, 'expected.js')).toString();
const expected = fs
.readFileSync(path.join(fixtureDir, 'expected.js'))
.toString();

@@ -29,0 +29,0 @@ assert.equal(trim(actual), trim(expected));

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc