Pipeline operator

This commit is contained in:
Gilbert 2017-09-29 00:11:41 -04:00 committed by Justin Ridgewell
parent 3746273eda
commit 81496ab7b1
35 changed files with 302 additions and 9 deletions

View File

@ -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.26",
"babylon": "7.0.0-beta.27",
"browserify": "^13.1.1",
"bundle-collapser": "^1.2.1",
"chai": "^4.1.0",

View File

@ -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.26",
"babylon": "7.0.0-beta.27",
"convert-source-map": "^1.1.0",
"debug": "^3.0.1",
"json5": "^0.5.0",

View File

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

View File

@ -0,0 +1,35 @@
# babel-plugin-syntax-pipeline-operator
Allow parsing of the pipeline operator `|>`. See [the proposal](https://github.com/tc39/proposal-pipeline-operator) for details.
## 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"]
});
```

View File

@ -0,0 +1,11 @@
{
"name": "babel-plugin-syntax-pipeline-operator",
"version": "7.0.0-beta.2",
"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"
]
}

View File

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

View File

@ -0,0 +1,35 @@
# babel-plugin-transform-pipeline-operator
Transform pipeline operator `|>` into call expressions. See [the proposal](https://github.com/tc39/proposal-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"]
});
```

View File

@ -0,0 +1,17 @@
{
"name": "babel-plugin-transform-pipeline-operator",
"version": "7.0.0-beta.2",
"description": "Transform pipeline operator into call expressions",
"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": "7.0.0-beta.2"
},
"devDependencies": {
"babel-helper-plugin-test-runner": "7.0.0-beta.2"
}
}

View File

@ -0,0 +1,52 @@
import syntaxPipelineOperator from "babel-plugin-syntax-pipeline-operator";
export default function({ types: t }) {
return {
inherits: syntaxPipelineOperator,
visitor: {
BinaryExpression(path) {
const { operator, left, right } = path.node;
if (operator !== "|>") return;
if (
t.isArrowFunctionExpression(right) &&
right.params.length === 1 &&
t.isIdentifier(right.params[0]) &&
t.isExpression(right.body)
) {
//
// Optimize away arrow function!
//
// Converts:
// arg |> x => x + x;
// To:
// (_x = arg, _x + _x);
//
const paramName = right.params[0].name;
const placeholder = path.scope.generateDeclaredUidIdentifier(
paramName,
);
path.get("right").scope.rename(paramName, placeholder.name);
path.replaceWith(
t.sequenceExpression([
t.assignmentExpression("=", placeholder, left),
right.body,
]),
);
} else {
//
// Simple invocation.
//
// Converts:
// x |> obj.f;
// To:
// obj.f(x);
//
path.replaceWith(t.callExpression(right, [left]));
}
},
},
};
}

View File

@ -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])

View File

@ -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])

View File

@ -0,0 +1,11 @@
var _2, _3, _sum;
var result = (_2 = [5, 10], (_3 = _2.map(x => x * 2), (_sum = _3.reduce((a, b) => a + b), _sum + 1)));
assert.equal(result, 31);
var inc = x => x + 1;
var double = x => x * 2;
var result2 = [4, 9].map(x => double(inc(x)));
assert.deepEqual(result2, [10, 20]);

View File

@ -0,0 +1,3 @@
var inc = (x) => x + 1
assert.equal(10 |> inc, 11);

View File

@ -0,0 +1,3 @@
var inc = (x) => x + 1
assert.equal(10 |> inc, 11);

View File

@ -0,0 +1,3 @@
var inc = x => x + 1;
assert.equal(inc(10), 11);

View File

@ -0,0 +1,4 @@
var inc = (x) => x + 1;
var double = (x) => x * 2;
assert.equal(10 |> inc |> double, 22);

View File

@ -0,0 +1,4 @@
var inc = (x) => x + 1;
var double = (x) => x * 2;
assert.equal(10 |> inc |> double, 22);

View File

@ -0,0 +1,5 @@
var inc = x => x + 1;
var double = x => x * 2;
assert.equal(double(inc(10)), 22);

View File

@ -0,0 +1,6 @@
var map = (fn) => (array) => array.map(fn);
var result = [10,20] |> map(x => x * 20);
assert.deepEqual(result, [200, 400])

View File

@ -0,0 +1,6 @@
var map = (fn) => (array) => array.map(fn);
var result = [10,20] |> map(x => x * 20);
assert.deepEqual(result, [200, 400])

View File

@ -0,0 +1,4 @@
var map = fn => array => array.map(fn);
var result = map(x => x * 20)([10, 20]);
assert.deepEqual(result, [200, 400]);

View File

@ -0,0 +1,5 @@
var array = [10,20,30];
var last = array |> a => a[a.length-1];
assert.equal(last, 30);

View File

@ -0,0 +1,5 @@
var array = [10,20,30];
var last = array |> a => a[a.length-1];
assert.equal(last, 30);

View File

@ -0,0 +1,5 @@
var _a;
var array = [10, 20, 30];
var last = (_a = array, _a[_a.length - 1]);
assert.equal(last, 30);

View File

@ -0,0 +1,3 @@
{
"plugins": ["transform-pipeline-operator"]
}

View File

@ -0,0 +1,12 @@
var inc = (x) => x + 1;
var result = 4 || 9 |> inc;
assert.equal(result, 5);
var f = (x) => x + 10
var h = (x) => x + 20
var result2 = 10 |> f || h |> inc;
assert.equal(result2, 21);

View File

@ -0,0 +1,12 @@
var inc = (x) => x + 1;
var result = 4 || 9 |> inc;
assert.equal(result, 5);
var f = (x) => x + 10
var h = (x) => x + 20
var result2 = 10 |> f || h |> inc;
assert.equal(result2, 21);

View File

@ -0,0 +1,11 @@
var inc = x => x + 1;
var result = inc(4 || 9);
assert.equal(result, 5);
var f = x => x + 10;
var h = x => x + 20;
var result2 = inc((f || h)(10));
assert.equal(result2, 21);

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-decorators": "7.0.0-beta.2",
"babel-plugin-transform-export-default": "7.0.0-beta.2",
"babel-plugin-transform-optional-chaining": "7.0.0-beta.2",
"babel-plugin-transform-pipeline-operator": "7.0.0-beta.2",
"babel-preset-stage-2": "7.0.0-beta.2"
}
}

View File

@ -3,6 +3,7 @@ import presetStage2 from "babel-preset-stage-2";
import transformDecorators from "babel-plugin-transform-decorators";
import transformExportDefault from "babel-plugin-transform-export-default";
import transformOptionalChaining from "babel-plugin-transform-optional-chaining";
import transformPipelineOperator from "babel-plugin-transform-pipeline-operator";
export default function() {
return {
@ -11,6 +12,7 @@ export default function() {
transformDecorators,
transformExportDefault,
transformOptionalChaining,
transformPipelineOperator,
],
};
}

View File

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

View File

@ -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.26",
"babylon": "7.0.0-beta.27",
"debug": "^3.0.1",
"globals": "^10.0.0",
"invariant": "^2.2.0",

View File

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

View File

@ -1017,9 +1017,9 @@ babylon@7.0.0-beta.18:
version "7.0.0-beta.18"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.18.tgz#5c23ee3fdb66358aabf3789779319c5b78a233c7"
babylon@7.0.0-beta.26:
version "7.0.0-beta.26"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.26.tgz#afc2c6b86113d000cc9476fd6f73e2a9223de8f7"
babylon@7.0.0-beta.27:
version "7.0.0-beta.27"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-7.0.0-beta.27.tgz#b6edd30ef30619e2f630eb52585fdda84e6542cd"
babylon@^6.17.4, babylon@^6.18.0:
version "6.18.0"