-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# babel-plugin-syntax-pipeline-operator | ||
|
||
Allow parsing of do expressions. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install babel-plugin-syntax-pipeline-operator | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
```json | ||
{ | ||
"plugins": ["syntax-pipeline-operator"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins syntax-pipeline-operator script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("babel-core").transform("code", { | ||
plugins: ["syntax-pipeline-operator"] | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "babel-plugin-syntax-pipeline-operator", | ||
"version": "6.3.13", | ||
"description": "Allow parsing of the pipeline operator", | ||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-pipeline-operator", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": { | ||
"babel-runtime": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-helper-plugin-test-runner": "^6.3.13" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function () { | ||
return { | ||
manipulateOptions(opts, parserOpts) { | ||
parserOpts.plugins.push("pipelineOperator"); | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# babel-plugin-transform-pipeline-operator | ||
|
||
Compile pipeline operator `|>` usage to ES5. See [the ES.next proposal](https://github.com/mindeavor/es-pipeline-operator) for details. | ||
|
||
## Installation | ||
|
||
```sh | ||
$ npm install babel-plugin-transform-pipeline-operator | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `.babelrc` (Recommended) | ||
|
||
**.babelrc** | ||
|
||
```json | ||
{ | ||
"plugins": ["transform-pipeline-operator"] | ||
} | ||
``` | ||
|
||
### Via CLI | ||
|
||
```sh | ||
$ babel --plugins transform-pipeline-operator script.js | ||
``` | ||
|
||
### Via Node API | ||
|
||
```javascript | ||
require("babel-core").transform("code", { | ||
plugins: ["transform-pipeline-operator"] | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "babel-plugin-transform-pipeline-operator", | ||
"version": "6.3.13", | ||
"description": "Compile do expressions to ES5", | ||
This comment has been minimized.
Sorry, something went wrong. |
||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-pipeline-operator", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"keywords": [ | ||
"babel-plugin" | ||
], | ||
"dependencies": { | ||
"babel-plugin-syntax-pipeline-operator": "^6.3.13", | ||
"babel-runtime": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-helper-plugin-test-runner": "^6.3.13" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
export default function ({ types: t }) { | ||
return { | ||
inherits: require("babel-plugin-syntax-pipeline-operator"), | ||
|
||
visitor: { | ||
|
||
BinaryExpression(path) { | ||
if (path.node.operator === "|>") { | ||
let right = path.node.right; | ||
|
||
if ( | ||
right.type === "ArrowFunctionExpression" && | ||
right.params.length === 1 && | ||
t.isIdentifier(right.params[0]) && | ||
t.isExpression(right.body) | ||
) { | ||
// | ||
// Optimize away arrow function! | ||
// | ||
// This step converts: | ||
// let result = arg |> x => x + x; | ||
// To: | ||
// let _x = arg; | ||
// let result = arg |> x => x + x; | ||
// | ||
let paramName = right.params[0].name; | ||
let placeholder = path.scope.generateUidIdentifier(paramName); | ||
path.parentPath.insertBefore(t.variableDeclarator(placeholder, path.node.left)) | ||
|
||
// | ||
// This step converts: | ||
// let _x = arg; | ||
// let result = arg |> x => x + x; | ||
// To: | ||
// let _x = arg; | ||
// let result = arg |> _x => _x + _x; | ||
// | ||
let rightPath = path.node.right._paths[0] | ||
rightPath.scope.rename(paramName, placeholder.name) | ||
|
||
// | ||
// This step converts: | ||
// let _x = arg; | ||
// let result = arg |> _x => _x + _x; | ||
// To: | ||
// let _x = arg; | ||
// let result = _x + _x; | ||
// | ||
path.replaceWith(right.body); | ||
} | ||
else { | ||
// | ||
// Simple invocation. | ||
// | ||
// Converts: | ||
// x |> obj.f; | ||
// To: | ||
// obj.f(x); | ||
// | ||
path.replaceWith({ | ||
type: "CallExpression", | ||
callee: path.node.right, | ||
arguments: [ path.node.left ] | ||
}); | ||
} | ||
} | ||
} | ||
|
||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var result = [5,10] | ||
|> _ => _.map(x => x * 2) | ||
|> _ => _.reduce( (a,b) => a + b ) | ||
|> sum => sum + 1 | ||
|
||
assert.equal(result, 31) | ||
|
||
|
||
var inc = (x) => x + 1; | ||
var double = (x) => x * 2; | ||
|
||
var result2 = [4, 9].map( x => x |> inc |> double ) | ||
|
||
assert.deepEqual(result2, [10, 20]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var inc = (x) => x + 1 | ||
|
||
assert.equal(10 |> inc, 11); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
var inc = (x) => x + 1; | ||
var double = (x) => x * 2; | ||
|
||
assert.equal(10 |> inc |> double, 22); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
var map = (fn) => (array) => array.map(fn); | ||
|
||
var result = [10,20] |> map(x => x * 20); | ||
|
||
assert.deepEqual(result, [200, 400]) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var array = [10,20,30]; | ||
|
||
var last = array |> a => a[a.length-1]; | ||
|
||
assert.equal(last, 30); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["transform-pipeline-operator"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require("babel-helper-plugin-test-runner")(__dirname); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ export class TokenType { | |
this.isAssign = !!conf.isAssign; | ||
this.prefix = !!conf.prefix; | ||
this.postfix = !!conf.postfix; | ||
this.binop = conf.binop || null; | ||
this.binop = (conf.binop === 0) ? 0 : (conf.binop || null); | ||
this.updateContext = null; | ||
} | ||
} | ||
|
@@ -82,6 +82,7 @@ export const types = { | |
assign: new TokenType("_=", {beforeExpr: true, isAssign: true}), | ||
incDec: new TokenType("++/--", {prefix: true, postfix: true, startsExpr: true}), | ||
prefix: new TokenType("prefix", {beforeExpr: true, prefix: true, startsExpr: true}), | ||
pipeline: binop("|>", 0), | ||
This comment has been minimized.
Sorry, something went wrong.
hzoo
|
||
logicalOR: binop("||", 1), | ||
logicalAND: binop("&&", 2), | ||
bitwiseOR: binop("|", 3), | ||
|
Did you mean to put
do expressions
here?