https://github.com/jhen0409/babel-plugin-transform-array-push
Simple transform `arr.push(a)` to `arr[arr.length] = a`, just follow loverajoel/jstips#00
https://github.com/jhen0409/babel-plugin-transform-array-push
Last synced: 8 months ago
JSON representation
Simple transform `arr.push(a)` to `arr[arr.length] = a`, just follow loverajoel/jstips#00
- Host: GitHub
- URL: https://github.com/jhen0409/babel-plugin-transform-array-push
- Owner: jhen0409
- License: mit
- Created: 2016-05-08T01:38:57.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-05-10T11:38:34.000Z (over 9 years ago)
- Last Synced: 2025-01-24T02:59:06.137Z (9 months ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 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](https://github.com/loverajoel/jstips/blob/gh-pages/_posts/en/2015-12-29-insert-item-inside-an-array.md#adding-an-element-at-the-end).
```js
const arr = [1, 2, 3]
arr.push(4)const arrLength = arr.push(5);
```into
```js
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
```js
// 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`
```js
// Will not transform
let arr = [1, 2, 3]
arr.push(4, 5, 6)
```## Installation
```js
npm i --save-dev babel-plugin-transform-array-push
```## Usage
### Via `.babelrc` (recommended)
**.babelrc**
```json
{
"plugins": ["transform-array-push"]
}
```### Via CLI
```js
babel script.js --plugins transform-array-push
```### Via Node API
```js
require('babel-core').transform('code', {
plugins: ['transform-array-push'],
})
```## License
[MIT](LICENSE.md)