diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/.npmignore b/packages/babel-plugin-transform-remove-unused-catch-binding/.npmignore new file mode 100644 index 0000000000..f980694583 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/.npmignore @@ -0,0 +1,3 @@ +src +test +*.log diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/README.md b/packages/babel-plugin-transform-remove-unused-catch-binding/README.md new file mode 100644 index 0000000000..8443a20b45 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/README.md @@ -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) diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/package.json b/packages/babel-plugin-transform-remove-unused-catch-binding/package.json new file mode 100644 index 0000000000..0ca650ca96 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/package.json @@ -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" + } +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/src/index.js b/packages/babel-plugin-transform-remove-unused-catch-binding/src/index.js new file mode 100644 index 0000000000..66d901533e --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/src/index.js @@ -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(); + } + }, + }, + }; +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/options.json b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/options.json new file mode 100644 index 0000000000..bfd6f2e628 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["transform-remove-unused-catch-binding"] +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-duplicate-variable-declaration/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-duplicate-variable-declaration/actual.js new file mode 100644 index 0000000000..520eab559f --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-duplicate-variable-declaration/actual.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch (e) { + let e = new TypeError('Duplicate variable declaration; will throw an error.'); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-duplicate-variable-declaration/options.json b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-duplicate-variable-declaration/options.json new file mode 100644 index 0000000000..f451b42655 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-duplicate-variable-declaration/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Duplicate declaration \"e\"" +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-null-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-null-binding/actual.js new file mode 100644 index 0000000000..1bf3a12cd8 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-null-binding/actual.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-null-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-null-binding/expected.js new file mode 100644 index 0000000000..1bf3a12cd8 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-null-binding/expected.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-array-pattern-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-array-pattern-binding/actual.js new file mode 100644 index 0000000000..89bd8f7b85 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-array-pattern-binding/actual.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch ([message]) { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-array-pattern-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-array-pattern-binding/expected.js new file mode 100644 index 0000000000..89bd8f7b85 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-array-pattern-binding/expected.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch ([message]) { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-binding/actual.js new file mode 100644 index 0000000000..0f989f7bdc --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-binding/actual.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch (err) { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-binding/expected.js new file mode 100644 index 0000000000..1bf3a12cd8 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-binding/expected.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-object-pattern-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-object-pattern-binding/actual.js new file mode 100644 index 0000000000..7923919210 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-object-pattern-binding/actual.js @@ -0,0 +1,7 @@ +try { + throw 0; +} catch ({ + message +}) { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-object-pattern-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-object-pattern-binding/expected.js new file mode 100644 index 0000000000..7923919210 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-unused-object-pattern-binding/expected.js @@ -0,0 +1,7 @@ +try { + throw 0; +} catch ({ + message +}) { + console.log("it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-array-pattern-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-array-pattern-binding/actual.js new file mode 100644 index 0000000000..20dea187c0 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-array-pattern-binding/actual.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch ([message]) { + console.log(message); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-array-pattern-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-array-pattern-binding/expected.js new file mode 100644 index 0000000000..20dea187c0 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-array-pattern-binding/expected.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch ([message]) { + console.log(message); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding-variable/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding-variable/actual.js new file mode 100644 index 0000000000..098cfdf7fe --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding-variable/actual.js @@ -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.'); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding-variable/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding-variable/expected.js new file mode 100644 index 0000000000..098cfdf7fe --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding-variable/expected.js @@ -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.'); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding/actual.js new file mode 100644 index 0000000000..d0335caa35 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding/actual.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch (err) { + console.log(err, "it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding/expected.js new file mode 100644 index 0000000000..d0335caa35 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-binding/expected.js @@ -0,0 +1,5 @@ +try { + throw 0; +} catch (err) { + console.log(err, "it failed, but this code executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-object-pattern-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-object-pattern-binding/actual.js new file mode 100644 index 0000000000..5d0ec55ede --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-object-pattern-binding/actual.js @@ -0,0 +1,7 @@ +try { + throw 0; +} catch ({ + message +}) { + console.log(message); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-object-pattern-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-object-pattern-binding/expected.js new file mode 100644 index 0000000000..5d0ec55ede --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-block-used-object-pattern-binding/expected.js @@ -0,0 +1,7 @@ +try { + throw 0; +} catch ({ + message +}) { + console.log(message); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-unused-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-unused-binding/actual.js new file mode 100644 index 0000000000..ba0539adf7 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-unused-binding/actual.js @@ -0,0 +1,7 @@ +try { + throw 0; +} catch (err) { + console.log("it failed, but this code executes"); +} finally { + console.log("this code also executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-unused-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-unused-binding/expected.js new file mode 100644 index 0000000000..31e6ca9ff4 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-unused-binding/expected.js @@ -0,0 +1,7 @@ +try { + throw 0; +} catch { + console.log("it failed, but this code executes"); +} finally { + console.log("this code also executes"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-used-binding/actual.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-used-binding/actual.js new file mode 100644 index 0000000000..fb09c402a5 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-used-binding/actual.js @@ -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"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-used-binding/expected.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-used-binding/expected.js new file mode 100644 index 0000000000..fb09c402a5 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/fixtures/remove-unused-catch-binding/try-catch-finally-used-binding/expected.js @@ -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"); +} diff --git a/packages/babel-plugin-transform-remove-unused-catch-binding/test/index.js b/packages/babel-plugin-transform-remove-unused-catch-binding/test/index.js new file mode 100644 index 0000000000..09cfbc31f5 --- /dev/null +++ b/packages/babel-plugin-transform-remove-unused-catch-binding/test/index.js @@ -0,0 +1,3 @@ +import runner from "babel-helper-plugin-test-runner"; + +runner(__dirname);