babel-plugin-transform-array-push

0.1.2 • Public • Published

babel-plugin-transform-array-push

Simple transform arr.push(a) to arr[arr.length] = a, just follow loverajoel/jstips#00 - Adding an element at the end.

const arr = [1, 2, 3]
arr.push(4)
 
const arrLength = arr.push(5);

into

const arr = [1, 2, 3];
arr[arr.length] = 4;
 
const arrLength = (arr[arr.length] = 5, arr.length);

Limitation

  • Only for ArrayExpression, and it's known, no re-assign other type
// Will transform
let arr = [1, 2, 3]
arr.push(4)
 
// Will transform
let arr = [1, 2, 3]
arr = [1, 2, 3, 4]
arr4.push(5)
 
// Will not transform
let arr = [1, 2, 3]
arr = { push: 1 }
arr.push(4)
  • Not transform multi-arg of array.push
// Will not transform
let arr = [1, 2, 3]
arr.push(4, 5, 6)

Installation

npm i --save-dev babel-plugin-transform-array-push

Usage

Via .babelrc (recommended)

.babelrc

{
  "plugins": ["transform-array-push"]
}

Via CLI

babel script.js --plugins transform-array-push

Via Node API

require('babel-core').transform('code', {
  plugins: ['transform-array-push'],
})

License

MIT

Package Sidebar

Install

npm i babel-plugin-transform-array-push

Weekly Downloads

3

Version

0.1.2

License

MIT

Last publish

Collaborators

  • jhen0409