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);
- 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)
npm i --save-dev babel-plugin-transform-array-push
.babelrc
{
"plugins": ["transform-array-push"]
}
babel script.js --plugins transform-array-push
require('babel-core').transform('code', {
plugins: ['transform-array-push'],
})