Add optionality to catch bindings (#5956)

This commit is contained in:
MarckK 2017-07-25 15:38:48 +01:00 committed by Brian Ng
parent 51a293601b
commit 9fc910d8c0
29 changed files with 360 additions and 9 deletions

View File

@ -32,7 +32,7 @@
"babel-template": "7.0.0-alpha.15",
"babel-traverse": "7.0.0-alpha.15",
"babel-types": "7.0.0-alpha.15",
"babylon": "7.0.0-beta.15",
"babylon": "7.0.0-beta.17",
"convert-source-map": "^1.1.0",
"debug": "^2.1.1",
"json5": "^0.5.0",

View File

@ -20,6 +20,6 @@
},
"devDependencies": {
"babel-helper-fixtures": "7.0.0-alpha.15",
"babylon": "^7.0.0-beta.15"
"babylon": "^7.0.0-beta.17"
}
}

View File

@ -171,10 +171,12 @@ export function TryStatement(node: Object) {
export function CatchClause(node: Object) {
this.word("catch");
this.space();
if (node.param) {
this.token("(");
this.print(node.param, node);
this.token(")");
this.space();
}
this.print(node.body, node);
}

View File

@ -0,0 +1,51 @@
try {} catch (err) {}
try {} catch {}
try {
bar;
} catch (err) {}
try {
bar;
} catch {}
try {
bar;
} catch (err) {
foo();
}
try {
bar;
} catch {
foo();
}
try {
bar;
} catch (err) {
foo();
} finally {
yay();
}
try {
bar;
} catch {
foo();
} finally {
yay();
}
try {
bar;
} catch (err) {
foo();
} finally {}
try {
bar;
} catch {
foo();
} finally {}

View File

@ -0,0 +1,51 @@
try {} catch (err) {}
try {} catch {}
try {
bar;
} catch (err) {}
try {
bar;
} catch {}
try {
bar;
} catch (err) {
foo();
}
try {
bar;
} catch {
foo();
}
try {
bar;
} catch (err) {
foo();
} finally {
yay();
}
try {
bar;
} catch {
foo();
} finally {
yay();
}
try {
bar;
} catch (err) {
foo();
} finally {}
try {
bar;
} catch {
foo();
} finally {}

View File

@ -388,6 +388,7 @@ suites.forEach(function(testSuite) {
"jsx",
"objectRestSpread",
"optionalChaining",
"optionalCatchBinding",
],
strictMode: false,
sourceType: "module",

View File

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

View File

@ -0,0 +1,52 @@
# babel-plugin-syntax-optional-catch-binding
> This plugin allows Babel to parse optional catch bindings.
## Example
**Syntax**
```javascript
try {
throw 0;
} catch {
doSomethingWhichDoesntCareAboutTheValueThrown();
console.log("Yay, code executes!");
}
```
## Installation
```sh
npm install --save-dev babel-plugin-syntax-optional-catch-binding
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["syntax-optional-catch-binding"]
}
```
### Via CLI
```sh
babel --plugins syntax-optional-catch-binding script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["syntax-optional-catch-binding"]
});
```
## References
* [Proposal: Optional Catch Binding for ECMAScript](https://github.com/babel/proposals/issues/7)

View File

@ -0,0 +1,13 @@
{
"name": "babel-plugin-syntax-optional-catch-binding",
"version": "7.0.0-alpha.15",
"description": "Allow parsing of optional catch bindings",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-optional-catch-binding",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {},
"devDependencies": {}
}

View File

@ -0,0 +1,7 @@
export default function() {
return {
manipulateOptions(opts, parserOpts) {
parserOpts.plugins.push("optionalCatchBinding");
},
};
}

View File

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

View File

@ -0,0 +1,60 @@
# babel-plugin-transform-optional-catch-binding
> Optional catch binding enables the catch block to execute whether or not an argument is passed to the catch statement (CatchClause).
## Examples
```js
try {
throw 0;
} catch {
doSomethingWhichDoesntCareAboutTheValueThrown();
}
```
```js
try {
throw 0;
} catch {
doSomethingWhichDoesntCareAboutTheValueThrown();
} finally {
doSomeCleanup();
}
```
## Installation
```sh
npm install --save-dev babel-plugin-transform-optional-catch-binding
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-optional-catch-binding"]
}
```
### Via CLI
```sh
babel --plugins transform-optional-catch-binding script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["transform-optional-catch-binding"]
});
```
## References
- [Proposal: Optional Catch Binding for ECMAScript](https://github.com/babel/proposals/issues/7)

View File

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

View File

@ -0,0 +1,17 @@
import syntaxOptionalCatchBinding from "babel-plugin-syntax-optional-catch-binding";
export default function() {
return {
inherits: syntaxOptionalCatchBinding,
visitor: {
CatchClause(path) {
if (!path.node.param) {
const uid = path.scope.generateUidIdentifier("unused");
const paramPath = path.get("param");
paramPath.replaceWith(uid);
}
},
},
};
}

View File

@ -0,0 +1,19 @@
const test = () => {
try {
throw 0;
}
catch {
return true;
}
}
assert(test());
const test2 = () => {
try {
throw 0;
}
catch (err) {
return true;
}
}
assert(test2());

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 (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,5 @@
try {
throw 0;
} catch (_unused) {
console.log("it failed, but this code executes");
}

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 (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 (_unused) {
console.log("it failed, but this code executes");
} finally {
console.log("this code also executes");
}

View File

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

View File

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

View File

@ -10,7 +10,7 @@
"dependencies": {
"babel-traverse": "7.0.0-alpha.15",
"babel-types": "7.0.0-alpha.15",
"babylon": "7.0.0-beta.13",
"babylon": "7.0.0-beta.17",
"lodash": "^4.2.0"
}
}

View File

@ -12,7 +12,7 @@
"babel-helper-function-name": "7.0.0-alpha.15",
"babel-messages": "7.0.0-alpha.15",
"babel-types": "7.0.0-alpha.15",
"babylon": "7.0.0-beta.15",
"babylon": "7.0.0-beta.17",
"debug": "^2.2.0",
"globals": "^10.0.0",
"invariant": "^2.2.0",

View File

@ -14,6 +14,6 @@
},
"devDependencies": {
"babel-generator": "7.0.0-alpha.15",
"babylon": "^7.0.0-beta.15"
"babylon": "^7.0.0-beta.17"
}
}

View File

@ -143,6 +143,7 @@ defineType("CatchClause", {
fields: {
param: {
validate: assertNodeType("Identifier"),
optional: true,
},
body: {
validate: assertNodeType("BlockStatement"),