Skip to content

Commit

Permalink
feature: @putout/plugin-remove-useless-push: add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 2, 2024
1 parent 0b203f4 commit 9340f32
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 6 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,19 @@ switch (x) {

</details>

<details><summary>remove useless <code>push</code></summary>

```diff
function notUsed() {
- const paths = [];
for (const [key, name] of tuples) {
- paths.push([key, full]);
}
}
```

</details>

<details><summary>remove useless <code>Object.assign()</code></summary>

```diff
Expand Down Expand Up @@ -2161,13 +2174,14 @@ It has a lot of plugins divided by groups:
| [`@putout/plugin-remove-useless-return`](/packages/plugin-remove-useless-return#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-return.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-return) |
| [`@putout/plugin-remove-useless-continue`](/packages/plugin-remove-useless-continue#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-continue.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-continue) |
| [`@putout/plugin-remove-useless-operand`](/packages/plugin-remove-useless-operand#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-operand.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-operand) |
| [`@putout/plugin-remove-useless-arguments`](/packages/plugin-remove-useless-arguments#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-arguments.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-arguments) |
| [`@putout/plugin-remove-useless-array`](/packages/plugin-remove-useless-array#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-array.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-array) |
| [`@putout/plugin-remove-useless-array-constructor`](/packages/plugin-remove-useless-array-constructor#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-array-constructor.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-array-constructor) |
| [`@putout/plugin-remove-useless-functions`](/packages/plugin-remove-useless-functions#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-functions.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-functions) |
| [`@putout/plugin-remove-useless-spread`](/packages/plugin-remove-useless-spread#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-spread.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-spread) |
| [`@putout/plugin-remove-useless-array-entries`](/packages/plugin-remove-useless-array-entries#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-array-entries.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-array-entries) |
| [`@putout/plugin-remove-useless-arguments`](/packages/plugin-remove-useless-arguments#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-arguments.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-arguments) |
| [`@putout/plugin-remove-useless-escape`](/packages/plugin-remove-useless-escape#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-escape.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-escape) |
| [`@putout/plugin-remove-useless-functions`](/packages/plugin-remove-useless-functions#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-functions.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-functions) |
| [`@putout/plugin-remove-useless-spread`](/packages/plugin-remove-useless-spread#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-spread.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-spread) |
| [`@putout/plugin-remove-useless-push`](/packages/plugin-remove-useless-push#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-push.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-push) |
| [`@putout/plugin-remove-useless-template-expressions`](/packages/plugin-remove-useless-template-expressions#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-template-expressions.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-template-expressions) |
| [`@putout/plugin-remove-useless-delete`](/packages/plugin-remove-useless-delete#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-useless-delete.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-useless-delete) |
| [`@putout/plugin-remove-debugger`](/packages/plugin-remove-debugger#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-debugger.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-debugger) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ export const scan = (root, {push, trackFile}) => {
const dirPath = getParentDirectory(file);
const dir = getFilename(dirPath);

const paths = [];

for (const [key, name] of tuples) {
const full = join(dir, name);
paths.push([key, full]);

const [exportedFile] = findFile(dirPath, full);

if (!exportedFile)
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-remove-useless-push/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": [
"plugin:n/recommended",
"plugin:putout/recommended"
],
"plugins": [
"putout",
"n"
]
}
6 changes: 6 additions & 0 deletions packages/plugin-remove-useless-push/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
*.swp
yarn-error.log

coverage
.idea
13 changes: 13 additions & 0 deletions packages/plugin-remove-useless-push/.madrun.mjs
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',
};
8 changes: 8 additions & 0 deletions packages/plugin-remove-useless-push/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.*
test
yarn-error.log
*.spec.js
fixture

coverage
*.config.*
15 changes: 15 additions & 0 deletions packages/plugin-remove-useless-push/.nycrc.json
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
}
7 changes: 7 additions & 0 deletions packages/plugin-remove-useless-push/.putout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"match": {
"*.md": {
"remove-useless-array": "off"
}
}
}
21 changes: 21 additions & 0 deletions packages/plugin-remove-useless-push/LICENSE
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.
59 changes: 59 additions & 0 deletions packages/plugin-remove-useless-push/README.md
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 packages/plugin-remove-useless-push/lib/remove-useless-push.js
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);
},
});
55 changes: 55 additions & 0 deletions packages/plugin-remove-useless-push/package.json
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"
}
}
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);
};
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 packages/plugin-remove-useless-push/test/remove-useless-push.js
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();
});
1 change: 1 addition & 0 deletions packages/putout/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
"@putout/plugin-remove-useless-functions": "^3.0.0",
"@putout/plugin-remove-useless-map": "^1.0.0",
"@putout/plugin-remove-useless-operand": "^2.0.0",
"@putout/plugin-remove-useless-push": "^1.0.0",
"@putout/plugin-remove-useless-replace": "^1.0.1",
"@putout/plugin-remove-useless-return": "^7.0.0",
"@putout/plugin-remove-useless-spread": "^11.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/putout/putout.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"labels/remove-unused": "off",
"remove-useless-escape": "off",
"remove-useless-variables": "off",
"remove-useless-push": "off",
"remove-useless-return": "off",
"remove-empty": "off",
"for-of/remove-unused-variables": "off",
Expand Down Expand Up @@ -216,6 +217,7 @@
"remove-useless-assign",
"remove-useless-replace",
"remove-useless-variables",
"remove-useless-push",
"remove-useless-map",
"remove-useless-constructor",
"remove-useless-array",
Expand Down
Loading

0 comments on commit 9340f32

Please sign in to comment.