Add throw expressions (#6325)

* Add throw expressions

Stage 2 proposal: https://github.com/tc39/proposal-throw-expressions

* Update babylon

* Add to stage 2
This commit is contained in:
Justin Ridgewell 2017-09-27 16:15:44 -04:00 committed by Henry Zhu
parent c4696a5bd2
commit 23f98a753a
44 changed files with 247 additions and 6 deletions

View File

@ -19,7 +19,7 @@
"babel-preset-flow": "7.0.0-alpha.18", "babel-preset-flow": "7.0.0-alpha.18",
"babel-preset-stage-0": "7.0.0-alpha.18", "babel-preset-stage-0": "7.0.0-alpha.18",
"babel-register": "7.0.0-alpha.18", "babel-register": "7.0.0-alpha.18",
"babylon": "7.0.0-beta.25", "babylon": "7.0.0-beta.26",
"browserify": "^13.1.1", "browserify": "^13.1.1",
"bundle-collapser": "^1.2.1", "bundle-collapser": "^1.2.1",
"chai": "^4.1.0", "chai": "^4.1.0",

View File

@ -32,7 +32,7 @@
"babel-template": "7.0.0-beta.2", "babel-template": "7.0.0-beta.2",
"babel-traverse": "7.0.0-beta.2", "babel-traverse": "7.0.0-beta.2",
"babel-types": "7.0.0-beta.2", "babel-types": "7.0.0-beta.2",
"babylon": "7.0.0-beta.25", "babylon": "7.0.0-beta.26",
"convert-source-map": "^1.1.0", "convert-source-map": "^1.1.0",
"debug": "^3.0.1", "debug": "^3.0.1",
"json5": "^0.5.0", "json5": "^0.5.0",

View File

@ -20,6 +20,6 @@
}, },
"devDependencies": { "devDependencies": {
"babel-helper-fixtures": "7.0.0-beta.2", "babel-helper-fixtures": "7.0.0-beta.2",
"babylon": "^7.0.0-beta.25" "babylon": "^7.0.0-beta.26"
} }
} }

View File

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

View File

@ -0,0 +1,42 @@
# babel-plugin-syntax-throw-expressions
Allow parsing of Throw Expressions:
```js
function test(param = throw new Error('required!')) {
const test = param === true || throw new Error('Falsey!');
}
```
## Installation
```sh
npm install --save-dev babel-plugin-syntax-throw-expressions
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["syntax-throw-expressions"]
}
```
### Via CLI
```sh
babel --plugins syntax-throw-expressions script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["syntax-throw-expressions"]
});
```

View File

@ -0,0 +1,11 @@
{
"name": "babel-plugin-syntax-throw-expressions",
"version": "7.0.0-beta.2",
"description": "Allow parsing of Throw Expressions",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-throw-expressions",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
]
}

View File

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

View File

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

View File

@ -0,0 +1,47 @@
# babel-plugin-transform-throw-expressions
This plugin transforms Throw Expressions into an IIFE.
## Example
```js
function test(param = throw new Error('required!')) {
const test = param === true || throw new Error('Falsey!');
}
```
## Installation
```sh
npm install --save-dev babel-plugin-transform-throw-expressions
```
## Usage
### Via `.babelrc` (Recommended)
**.babelrc**
```json
{
"plugins": ["transform-throw-expressions"]
}
```
### Via CLI
```sh
babel --plugins transform-throw-expressions script.js
```
### Via Node API
```javascript
require("babel-core").transform("code", {
plugins: ["transform-throw-expressions"]
});
```
## References
* [Proposal: Numeric Separators](https://github.com/tc39/proposal-throw-expressions)

View File

@ -0,0 +1,17 @@
{
"name": "babel-plugin-transform-throw-expressions",
"version": "7.0.0-beta.2",
"description": "Wraps Throw Expressions in an IIFE",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-throw-expressions",
"license": "MIT",
"main": "lib/index.js",
"keywords": [
"babel-plugin"
],
"dependencies": {
"babel-plugin-syntax-throw-expressions": "7.0.0-beta.2"
},
"devDependencies": {
"babel-helper-plugin-test-runner": "7.0.0-beta.2"
}
}

View File

@ -0,0 +1,23 @@
import syntaxThrowExpressions from "babel-plugin-syntax-throw-expressions";
export default function({ types: t }) {
return {
inherits: syntaxThrowExpressions,
visitor: {
UnaryExpression(path) {
const { operator, argument } = path.node;
if (operator !== "throw") return;
const arg = t.identifier("e");
const arrow = t.functionExpression(
null,
[arg],
t.blockStatement([t.throwStatement(arg)]),
);
path.replaceWith(t.callExpression(arrow, [argument]));
},
},
};
}

View File

@ -0,0 +1,3 @@
function* test() {
(throw new Error(...arguments));
}

View File

@ -0,0 +1,5 @@
function* test() {
(function (e) {
throw e;
})(new Error(...arguments));
}

View File

@ -0,0 +1,3 @@
async function test() {
(throw new Error(await 'test'));
}

View File

@ -0,0 +1,5 @@
async function test() {
(function (e) {
throw e;
})(new Error((await 'test')));
}

View File

@ -0,0 +1,3 @@
function test() {
(throw new Error('test'));
}

View File

@ -0,0 +1,5 @@
function test() {
(function (e) {
throw e;
})(new Error('test'));
}

View File

@ -0,0 +1,3 @@
(function (e) {
throw e;
})(new Error('test'));

View File

@ -0,0 +1 @@
(throw new Error('test'), new Error('2'));

View File

@ -0,0 +1,3 @@
(function (e) {
throw e;
})(new Error('test')), new Error('2');

View File

@ -0,0 +1 @@
true && throw new Error('test');

View File

@ -0,0 +1,3 @@
true && function (e) {
throw e;
}(new Error('test'));

View File

@ -0,0 +1,2 @@
function test(a = throw new Error('test')) {
}

View File

@ -0,0 +1,3 @@
function test(a = function (e) {
throw e;
}(new Error('test'))) {}

View File

@ -0,0 +1 @@
test((throw new Error('test'), 1));

View File

@ -0,0 +1,3 @@
test((function (e) {
throw e;
}(new Error('test')), 1));

View File

@ -0,0 +1,3 @@
function* test() {
(throw new Error(function.sent));
}

View File

@ -0,0 +1,5 @@
function* test() {
(function (e) {
throw e;
})(new Error(function.sent));
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-throw-expressions", "syntax-function-sent"]
}

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-throw-expressions"]
}

View File

@ -0,0 +1,3 @@
function* test() {
(throw new Error(this));
}

View File

@ -0,0 +1,5 @@
function* test() {
(function (e) {
throw e;
})(new Error(this));
}

View File

@ -0,0 +1,3 @@
function test() {
throw new Error('test');
}

View File

@ -0,0 +1,3 @@
function test() {
throw new Error('test');
}

View File

@ -0,0 +1,3 @@
function* test() {
(throw new Error(yield 'test'));
}

View File

@ -0,0 +1,5 @@
function* test() {
(function (e) {
throw e;
})(new Error((yield 'test')));
}

View File

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

View File

@ -11,6 +11,7 @@
"babel-plugin-transform-export-namespace": "7.0.0-beta.2", "babel-plugin-transform-export-namespace": "7.0.0-beta.2",
"babel-plugin-transform-function-sent": "7.0.0-beta.2", "babel-plugin-transform-function-sent": "7.0.0-beta.2",
"babel-plugin-transform-numeric-separator": "7.0.0-beta.2", "babel-plugin-transform-numeric-separator": "7.0.0-beta.2",
"babel-plugin-transform-throw-expressions": "7.0.0-beta.2",
"babel-preset-stage-3": "7.0.0-beta.2" "babel-preset-stage-3": "7.0.0-beta.2"
} }
} }

View File

@ -3,6 +3,7 @@ import presetStage3 from "babel-preset-stage-3";
import transformFunctionSent from "babel-plugin-transform-function-sent"; import transformFunctionSent from "babel-plugin-transform-function-sent";
import transformExportNamespace from "babel-plugin-transform-export-namespace"; import transformExportNamespace from "babel-plugin-transform-export-namespace";
import transformNumericSeparator from "babel-plugin-transform-numeric-separator"; import transformNumericSeparator from "babel-plugin-transform-numeric-separator";
import transformThrowExpressions from "babel-plugin-transform-throw-expressions";
export default function() { export default function() {
return { return {
@ -11,6 +12,7 @@ export default function() {
transformFunctionSent, transformFunctionSent,
transformExportNamespace, transformExportNamespace,
transformNumericSeparator, transformNumericSeparator,
transformThrowExpressions,
], ],
}; };
} }

View File

@ -10,7 +10,7 @@
"dependencies": { "dependencies": {
"babel-traverse": "7.0.0-beta.2", "babel-traverse": "7.0.0-beta.2",
"babel-types": "7.0.0-beta.2", "babel-types": "7.0.0-beta.2",
"babylon": "7.0.0-beta.25", "babylon": "7.0.0-beta.26",
"lodash": "^4.2.0" "lodash": "^4.2.0"
} }
} }

View File

@ -12,7 +12,7 @@
"babel-helper-function-name": "7.0.0-beta.2", "babel-helper-function-name": "7.0.0-beta.2",
"babel-messages": "7.0.0-beta.2", "babel-messages": "7.0.0-beta.2",
"babel-types": "7.0.0-beta.2", "babel-types": "7.0.0-beta.2",
"babylon": "7.0.0-beta.25", "babylon": "7.0.0-beta.26",
"debug": "^3.0.1", "debug": "^3.0.1",
"globals": "^10.0.0", "globals": "^10.0.0",
"invariant": "^2.2.0", "invariant": "^2.2.0",

View File

@ -14,6 +14,6 @@
}, },
"devDependencies": { "devDependencies": {
"babel-generator": "7.0.0-beta.2", "babel-generator": "7.0.0-beta.2",
"babylon": "^7.0.0-beta.25" "babylon": "^7.0.0-beta.26"
} }
} }

View File

@ -47,6 +47,7 @@ export const NUMBER_UNARY_OPERATORS = ["+", "-", "~"];
export const STRING_UNARY_OPERATORS = ["typeof"]; export const STRING_UNARY_OPERATORS = ["typeof"];
export const UNARY_OPERATORS = [ export const UNARY_OPERATORS = [
"void", "void",
"throw",
...BOOLEAN_UNARY_OPERATORS, ...BOOLEAN_UNARY_OPERATORS,
...NUMBER_UNARY_OPERATORS, ...NUMBER_UNARY_OPERATORS,
...STRING_UNARY_OPERATORS, ...STRING_UNARY_OPERATORS,