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:
parent
c4696a5bd2
commit
23f98a753a
@ -19,7 +19,7 @@
|
||||
"babel-preset-flow": "7.0.0-alpha.18",
|
||||
"babel-preset-stage-0": "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",
|
||||
"bundle-collapser": "^1.2.1",
|
||||
"chai": "^4.1.0",
|
||||
|
||||
@ -32,7 +32,7 @@
|
||||
"babel-template": "7.0.0-beta.2",
|
||||
"babel-traverse": "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",
|
||||
"debug": "^3.0.1",
|
||||
"json5": "^0.5.0",
|
||||
|
||||
@ -20,6 +20,6 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-helper-fixtures": "7.0.0-beta.2",
|
||||
"babylon": "^7.0.0-beta.25"
|
||||
"babylon": "^7.0.0-beta.26"
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
*.log
|
||||
src
|
||||
42
packages/babel-plugin-syntax-throw-expressions/README.md
Normal file
42
packages/babel-plugin-syntax-throw-expressions/README.md
Normal 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"]
|
||||
});
|
||||
```
|
||||
11
packages/babel-plugin-syntax-throw-expressions/package.json
Normal file
11
packages/babel-plugin-syntax-throw-expressions/package.json
Normal 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"
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
export default function() {
|
||||
return {
|
||||
manipulateOptions(opts, parserOpts) {
|
||||
parserOpts.plugins.push("throwExpressions");
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
47
packages/babel-plugin-transform-throw-expressions/README.md
Normal file
47
packages/babel-plugin-transform-throw-expressions/README.md
Normal 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)
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
@ -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]));
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function* test() {
|
||||
(throw new Error(...arguments));
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
function* test() {
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error(...arguments));
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
async function test() {
|
||||
(throw new Error(await 'test'));
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
async function test() {
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error((await 'test')));
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function test() {
|
||||
(throw new Error('test'));
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
function test() {
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error('test'));
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
(throw new Error('test'));
|
||||
@ -0,0 +1,3 @@
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error('test'));
|
||||
@ -0,0 +1 @@
|
||||
(throw new Error('test'), new Error('2'));
|
||||
@ -0,0 +1,3 @@
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error('test')), new Error('2');
|
||||
@ -0,0 +1 @@
|
||||
true && throw new Error('test');
|
||||
@ -0,0 +1,3 @@
|
||||
true && function (e) {
|
||||
throw e;
|
||||
}(new Error('test'));
|
||||
@ -0,0 +1,2 @@
|
||||
function test(a = throw new Error('test')) {
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function test(a = function (e) {
|
||||
throw e;
|
||||
}(new Error('test'))) {}
|
||||
@ -0,0 +1 @@
|
||||
test((throw new Error('test'), 1));
|
||||
@ -0,0 +1,3 @@
|
||||
test((function (e) {
|
||||
throw e;
|
||||
}(new Error('test')), 1));
|
||||
@ -0,0 +1,3 @@
|
||||
function* test() {
|
||||
(throw new Error(function.sent));
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
function* test() {
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error(function.sent));
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-throw-expressions", "syntax-function-sent"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["transform-throw-expressions"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function* test() {
|
||||
(throw new Error(this));
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
function* test() {
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error(this));
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function test() {
|
||||
throw new Error('test');
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function test() {
|
||||
throw new Error('test');
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
function* test() {
|
||||
(throw new Error(yield 'test'));
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
function* test() {
|
||||
(function (e) {
|
||||
throw e;
|
||||
})(new Error((yield 'test')));
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
import runner from "babel-helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
@ -11,6 +11,7 @@
|
||||
"babel-plugin-transform-export-namespace": "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-throw-expressions": "7.0.0-beta.2",
|
||||
"babel-preset-stage-3": "7.0.0-beta.2"
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ import presetStage3 from "babel-preset-stage-3";
|
||||
import transformFunctionSent from "babel-plugin-transform-function-sent";
|
||||
import transformExportNamespace from "babel-plugin-transform-export-namespace";
|
||||
import transformNumericSeparator from "babel-plugin-transform-numeric-separator";
|
||||
import transformThrowExpressions from "babel-plugin-transform-throw-expressions";
|
||||
|
||||
export default function() {
|
||||
return {
|
||||
@ -11,6 +12,7 @@ export default function() {
|
||||
transformFunctionSent,
|
||||
transformExportNamespace,
|
||||
transformNumericSeparator,
|
||||
transformThrowExpressions,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
"dependencies": {
|
||||
"babel-traverse": "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"
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
"babel-helper-function-name": "7.0.0-beta.2",
|
||||
"babel-messages": "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",
|
||||
"globals": "^10.0.0",
|
||||
"invariant": "^2.2.0",
|
||||
|
||||
@ -14,6 +14,6 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-generator": "7.0.0-beta.2",
|
||||
"babylon": "^7.0.0-beta.25"
|
||||
"babylon": "^7.0.0-beta.26"
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,6 +47,7 @@ export const NUMBER_UNARY_OPERATORS = ["+", "-", "~"];
|
||||
export const STRING_UNARY_OPERATORS = ["typeof"];
|
||||
export const UNARY_OPERATORS = [
|
||||
"void",
|
||||
"throw",
|
||||
...BOOLEAN_UNARY_OPERATORS,
|
||||
...NUMBER_UNARY_OPERATORS,
|
||||
...STRING_UNARY_OPERATORS,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user