Codemod: remove unused catch binding (#6048)

* outline of plugin to remove unused catch binding, test not passing

* plugin to remove unused catch binding

* Edit README.md and package.json

* tests for try catch finally

* Add test to handle case when binding is referenced and given new TypeError (not passing)

* Fix visitor to not remove catch clause param when binding being assigned a new value

* Improve naming of tests and explanations

* add test case for catch param not present and fix test for duplicate variable declaration

* Remove binding.constantViolations filter in visitor as superfluous

* Remove duplicate check that catch clause param present

* Alter visitor so returns out when catch binding is not an Identifier

* Created failing tests for ObjectPattern params and rewrote visitor so now passing
Took out the pass in visitor when param not an Identifier, wrote case to handle when param isObjectPattern, and wrote failing tests for when param isArrayPattern

* Handle case when param isArrayPattern, tests passing

* Update package.json to  v7.0.0-alpha.20

* Revert visitor to only consider transform if param is Identifier
This commit is contained in:
MarckK 2017-09-19 20:38:17 +01:00 committed by Henry Zhu
parent 57584268cd
commit 8dffbf19d0
28 changed files with 234 additions and 0 deletions

View File

@ -0,0 +1,3 @@
src
test
*.log

View File

@ -0,0 +1,60 @@
# babel-plugin-transform-remove-unused-catch-binding
> If the argument bound to the catch block is not referenced in the catch block, that argument and the catch binding is removed.
## Examples
```js
try {
throw 0;
} catch (err) {
console.log("it failed, but this code executes");
}
```
Is transformed to:
```js
try {
throw 0;
} catch {
console.log("it failed, but this code executes");
}
```
## Installation
```sh
npm install --save-dev babel-plugin-transform-remove-unused-catch-binding
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-remove-unused-catch-binding"]
}
```
### Via CLI
```sh
babel --plugins transform-remove-unused-catch-binding script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["transform-remove-unused-catch-binding"]
});
```
## References
This codemod updates your source code in line with the following proposal:
- [Proposal: Optional Catch Binding for ECMAScript](https://github.com/babel/proposals/issues/7)

View File

@ -0,0 +1,17 @@
{
"name": "babel-plugin-transform-remove-unused-catch-binding",
"version": "7.0.0-alpha.20",
"description": "Remove unused catch bindings",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-remove-unused-catch-binding",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"babel-plugin-syntax-optional-catch-binding": "7.0.0-alpha.20"
},
"devDependencies": {
"babel-helper-plugin-test-runner": "7.0.0-alpha.20"
}
}

View File

@ -0,0 +1,24 @@
import syntaxOptionalCatchBinding from "babel-plugin-syntax-optional-catch-binding";
export default function(babel) {
const { types: t } = babel;
return {
inherits: syntaxOptionalCatchBinding,
visitor: {
CatchClause(path) {
if (path.node.param === null || !t.isIdentifier(path.node.param)) {
return;
}
const binding = path.scope.getOwnBinding(path.node.param.name);
if (binding.constantViolations.length > 0) {
return;
}
if (!binding.referenced) {
const paramPath = path.get("param");
paramPath.remove();
}
},
},
};
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-remove-unused-catch-binding"]
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch (e) {
let e = new TypeError('Duplicate variable declaration; will throw an error.');
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch ([message]) {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch ([message]) {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch (err) {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch ({
message
}) {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch ({
message
}) {
console.log("it failed, but this code executes");
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch ([message]) {
console.log(message);
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch ([message]) {
console.log(message);
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch (e) {
e = new TypeError('A new variable is not being declared or initialized; the catch binding is being referenced and cannot be removed.');
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch (e) {
e = new TypeError('A new variable is not being declared or initialized; the catch binding is being referenced and cannot be removed.');
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch (err) {
console.log(err, "it failed, but this code executes");
}

View File

@ -0,0 +1,5 @@
try {
throw 0;
} catch (err) {
console.log(err, "it failed, but this code executes");
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch ({
message
}) {
console.log(message);
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch ({
message
}) {
console.log(message);
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch (err) {
console.log("it failed, but this code executes");
} finally {
console.log("this code also executes");
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch {
console.log("it failed, but this code executes");
} finally {
console.log("this code also executes");
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch (err) {
console.log(err, "it failed, but this code executes");
} finally {
console.log("this code also executes");
}

View File

@ -0,0 +1,7 @@
try {
throw 0;
} catch (err) {
console.log(err, "it failed, but this code executes");
} finally {
console.log("this code also executes");
}

View File

@ -0,0 +1,3 @@
import runner from "babel-helper-plugin-test-runner";
runner(__dirname);