-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: @putout/plugin-remove-useless-push: add
- Loading branch information
1 parent
0b203f4
commit 9340f32
Showing
18 changed files
with
328 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": [ | ||
"plugin:n/recommended", | ||
"plugin:putout/recommended" | ||
], | ||
"plugins": [ | ||
"putout", | ||
"n" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
*.swp | ||
yarn-error.log | ||
|
||
coverage | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {run} from 'madrun'; | ||
|
||
export default { | ||
'publishOnly': () => run('lint'), | ||
'test': () => `tape 'test/*.js' lib/**/*.spec.js`, | ||
'watch:test': async () => `nodemon -w lib -w test -x ${await run('test')}`, | ||
'lint': () => `putout .`, | ||
'fresh:lint': () => run('lint', '--fresh'), | ||
'lint:fresh': () => run('lint', '--fresh'), | ||
'fix:lint': () => run('lint', '--fix'), | ||
'coverage': async () => `c8 ${await run('test')}`, | ||
'report': () => 'c8 report --reporter=lcov', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.* | ||
test | ||
yarn-error.log | ||
*.spec.js | ||
fixture | ||
|
||
coverage | ||
*.config.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"check-coverage": true, | ||
"all": true, | ||
"exclude": [ | ||
"**/*.spec.*", | ||
"**/fixture", | ||
"test", | ||
".*.*", | ||
"**/*.config.*" | ||
], | ||
"branches": 100, | ||
"lines": 100, | ||
"functions": 100, | ||
"statements": 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"match": { | ||
"*.md": { | ||
"remove-useless-array": "off" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) coderaiser | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# @putout/plugin-remove-useless-push [![NPM version][NPMIMGURL]][NPMURL] | ||
|
||
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-remove-useless-push.svg?style=flat&longCache=true | ||
[NPMURL]: https://npmjs.org/package/@putout/plugin-remove-useless-push "npm" | ||
|
||
> The `push()` method of `Array` instances adds the specified elements to the end of an array and returns the new length of the array. | ||
> | ||
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) | ||
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to remove useless `push()`. The rule fits good with [`putout/remove-unused-variables`](https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-unused-variables#readme). | ||
|
||
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/edebc3873f4b2ea6d99cfb957814bc63/603eed77455010519c1127490267888e3f6576a8). | ||
|
||
## Install | ||
|
||
``` | ||
npm i @putout/plugin-remove-useless-push | ||
``` | ||
|
||
## Rule | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"remove-useless-push": "on" | ||
} | ||
} | ||
``` | ||
|
||
## ❌ Example of incorrect code | ||
|
||
```js | ||
function get() { | ||
const a = []; | ||
const b = []; | ||
|
||
a.push(1); | ||
b.push(2); | ||
|
||
return b; | ||
} | ||
``` | ||
|
||
## ✅ Example of correct code | ||
|
||
```js | ||
function get() { | ||
const a = []; | ||
const b = []; | ||
|
||
b.push(2); | ||
|
||
return b; | ||
} | ||
``` | ||
|
||
## License | ||
|
||
MIT |
33 changes: 33 additions & 0 deletions
33
packages/plugin-remove-useless-push/lib/remove-useless-push.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
const {operator} = require('putout'); | ||
const {remove, getBinding} = operator; | ||
|
||
module.exports.report = () => `Avoid useless 'push()' to array `; | ||
|
||
module.exports.fix = (path) => { | ||
remove(path); | ||
}; | ||
|
||
module.exports.traverse = ({push}) => ({ | ||
'__a.push(__args)': (path) => { | ||
const __a = path.get('callee.object').node; | ||
const binding = getBinding(path, __a.name); | ||
|
||
if (!binding) | ||
return; | ||
|
||
if (!binding.path.isVariableDeclarator()) | ||
return; | ||
|
||
const {referencePaths} = binding; | ||
|
||
if (referencePaths.length > 1) | ||
return; | ||
|
||
if (binding.path.get('id').isObjectPattern()) | ||
return; | ||
|
||
push(path); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "@putout/plugin-remove-useless-push", | ||
"version": "1.0.3", | ||
"type": "commonjs", | ||
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)", | ||
"description": "🐊Putout plugin adds ability to remove useless 'push()'", | ||
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-useless-push#readme", | ||
"main": "lib/remove-useless-push.js", | ||
"release": false, | ||
"tag": false, | ||
"changelog": false, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/coderaiser/putout.git" | ||
}, | ||
"scripts": { | ||
"publishOnly": "madrun publishOnly", | ||
"test": "madrun test", | ||
"watch:test": "madrun watch:test", | ||
"lint": "madrun lint", | ||
"fresh:lint": "madrun fresh:lint", | ||
"lint:fresh": "madrun lint:fresh", | ||
"fix:lint": "madrun fix:lint", | ||
"coverage": "madrun coverage", | ||
"report": "madrun report" | ||
}, | ||
"dependencies": {}, | ||
"keywords": [ | ||
"putout", | ||
"putout-plugin", | ||
"plugin", | ||
"push", | ||
"putout-plugin-remove", | ||
"putout-plugin-remove-useless", | ||
"remove", | ||
"useless" | ||
], | ||
"devDependencies": { | ||
"@putout/test": "^11.0.0", | ||
"c8": "^10.0.0", | ||
"eslint": "^9.0.0", | ||
"eslint-plugin-n": "^17.0.0", | ||
"madrun": "^10.0.0" | ||
}, | ||
"peerDependencies": { | ||
"putout": ">=36" | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=18" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/plugin-remove-useless-push/test/fixture/remove-useless-push-fix.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function notUsed() { | ||
const a = []; | ||
|
||
const paths = []; | ||
|
||
for (const [key, name] of tuples) {} | ||
} | ||
|
||
function noBinding() { | ||
b.push(2); | ||
} | ||
|
||
function used() { | ||
const a = []; | ||
a.push(3); | ||
|
||
return a; | ||
} | ||
|
||
function destructured(putoutConfig) { | ||
const {processors} = putoutConfig; | ||
processors.push('hello'); | ||
} | ||
|
||
const collect = ({collector}) => { | ||
collector.push(propertyPath); | ||
}; |
31 changes: 31 additions & 0 deletions
31
packages/plugin-remove-useless-push/test/fixture/remove-useless-push.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
function notUsed() { | ||
const a = []; | ||
|
||
a.push(1); | ||
const paths = []; | ||
|
||
for (const [key, name] of tuples) { | ||
paths.push([key, full]); | ||
} | ||
} | ||
|
||
function noBinding() { | ||
b.push(2); | ||
} | ||
|
||
function used() { | ||
const a = []; | ||
a.push(3); | ||
|
||
return a; | ||
} | ||
|
||
function destructured(putoutConfig) { | ||
const {processors} = putoutConfig; | ||
processors.push('hello'); | ||
} | ||
|
||
const collect = ({collector}) => { | ||
collector.push(propertyPath); | ||
}; |
21 changes: 21 additions & 0 deletions
21
packages/plugin-remove-useless-push/test/remove-useless-push.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const {createTest} = require('@putout/test'); | ||
const plugin = require('..'); | ||
|
||
const test = createTest(__dirname, { | ||
printer: 'putout', | ||
plugins: [ | ||
['remove-useless-push', plugin], | ||
], | ||
}); | ||
|
||
test('putout: remove-useless-push: report', (t) => { | ||
t.report('remove-useless-push', `Avoid useless 'push()' to array `); | ||
t.end(); | ||
}); | ||
|
||
test('putout: remove-useless-push: transform', (t) => { | ||
t.transform('remove-useless-push'); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.