Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented babel-plugin-remove-unused-params. #200

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/babel-plugin-remove-unused-params/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# babel-plugin-remove-unused-params

This plugin removes unused trailing function parameters.

## Example

**In**

```javascript
function foo(a, b) {
return a;
}
```

**Out**

```javascript
function foo(a) {
return a;
}
```

## Installation

```sh
$ npm install babel-plugin-remove-unused-params
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["remove-unused-params"]
}
```

### Via CLI

```sh
$ babel --plugins remove-unused-params script.js
```

### Via Node API

```javascript
require("babel-core").transform("code", {
plugins: ["remove-unused-params"]
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
jest.autoMockOff();

const babel = require("babel-core");
const plugin = require("../src/index");

function transform(code) {
return babel.transform(code, {
plugins: [plugin],
}).code;
}

describe("remove-unused-params-plugin", () => {
it("should remove trailing unused params", () => {
const source =
`function foo(a, b, c) {
return a;
}`;
const expected =
`function foo(a) {
return a;
}`;
expect(transform(source)).toBe(expected);
});

it("should preserve unused params before used params", () => {
const source =
`function foo(a, b, c) {
return b;
}`;
const expected =
`function foo(a, b) {
return b;
}`;
expect(transform(source)).toBe(expected);
});

it("should remove all params if all unused", () => {
const source =
`function foo(a, b, c) {
return 42;
}`;
const expected =
`function foo() {
return 42;
}`;
expect(transform(source)).toBe(expected);
});


it("should remove all params if all unused", () => {
const source =
`function foo(a, b, c) {
return 42;
}`;
const expected =
`function foo() {
return 42;
}`;
expect(transform(source)).toBe(expected);
});

it("should not remove params if referenced in nested functions", () => {
const source =
`function foo(a, b, c) {
function bar(c = b()) {
return;
}
return 42;
}`;
const expected =
`function foo(a, b) {
function bar(c = b()) {
return;
}
return 42;
}`;
expect(transform(source)).toBe(expected);
});

it("should not remove nested params if referenced", () => {
const source =
`function foo({ a: aa, b: bb }, [c, d]) {
return aa;
}`;
const expected =
`function foo({ a: aa, b: bb }) {
return aa;
}`;
expect(transform(source)).toBe(expected);
});
});
16 changes: 16 additions & 0 deletions packages/babel-plugin-remove-unused-params/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "babel-plugin-remove-unused-params",
"version": "0.0.1",
"description": "This plugin removes unused trailing function parameters.",
"homepage": "https://github.com/babel/babili#readme",
"repository": "https://github.com/babel/babili/tree/master/packages/babel-plugin-remove-unused-params",
"bugs": "https://github.com/babel/babili/issues",
"author": "shinew",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {},
"devDependencies": {}
}
43 changes: 43 additions & 0 deletions packages/babel-plugin-remove-unused-params/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

function isIdentifierReferenced(id, scope) {
const binding = scope.getBinding(id.node.name);
if (!binding) {
return false;
}
return binding.references > 0;
}

function isLvalReferenced(lval, scope, t) {
if (t.isIdentifier(lval)) {
return isIdentifierReferenced(lval, scope);
}
let isAnyIdReferenced = false;
lval.traverse({
Identifier(path) {
if (isIdentifierReferenced(path, scope)) {
isAnyIdReferenced = true;
}
}
});
return isAnyIdReferenced;
}

module.exports = function({ types: t }) {
return {
name: "remove-unused-params",
visitor: {
Function(path) {
const lvals = path.get("params");
const isReferenced = lvals.map(
(lval) => isLvalReferenced(lval, path.scope, t));
for (let i = lvals.length - 1; i >= 0; i--) {
if (isReferenced[i] === true) {
break;
}
lvals[i].remove();
}
},
},
};
};