Merge pull request #6335 from jridgewell/pipeline

Pipeline operator
This commit is contained in:
Henry Zhu 2017-10-02 16:32:15 -04:00 committed by GitHub
commit 4a8137c6b4
47 changed files with 429 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

@ -31,7 +31,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

@ -19,6 +19,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,56 @@
import syntaxPipelineOperator from "babel-plugin-syntax-pipeline-operator";
export default function({ types: t }) {
return {
inherits: syntaxPipelineOperator,
visitor: {
BinaryExpression(path) {
const { scope } = path;
const { node } = path;
const { operator, left } = node;
let { right } = node;
if (operator !== "|>") return;
let optimizeArrow =
t.isArrowFunctionExpression(right) && t.isExpression(right.body);
let param;
if (optimizeArrow) {
const { params } = right;
if (params.length === 1) {
param = params[0];
} else if (params.length > 1) {
optimizeArrow = false;
}
} else if (t.isIdentifier(right, { name: "eval" })) {
right = t.sequenceExpression([t.numericLiteral(0), right]);
}
if (optimizeArrow && !param) {
// Arrow function with 0 arguments
path.replaceWith(t.sequenceExpression([left, right.body]));
return;
}
const placeholder = scope.generateUidIdentifierBasedOnNode(
param || left,
);
scope.push({ id: placeholder });
if (param) {
path.get("right").scope.rename(param.name, placeholder.name);
}
const call = optimizeArrow
? right.body
: t.callExpression(right, [placeholder]);
path.replaceWith(
t.sequenceExpression([
t.assignmentExpression("=", placeholder, left),
call,
]),
);
},
},
};
}

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,15 @@
var _ref, _ref2, _sum;
var result = (_ref = [5, 10], (_ref2 = _ref.map(x => x * 2), (_sum = _ref2.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 => {
var _ref3, _x;
return _ref3 = (_x = x, inc(_x)), double(_ref3);
});
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,5 @@
var _;
var inc = x => x + 1;
assert.equal((_ = 10, inc(_)), 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,7 @@
var _ref, _;
var inc = x => x + 1;
var double = x => x * 2;
assert.equal((_ref = (_ = 10, inc(_)), double(_ref)), 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,6 @@
var _ref;
var map = fn => array => array.map(fn);
var result = (_ref = [10, 20], map(x => x * 20)(_ref));
assert.deepEqual(result, [200, 400]);

View File

@ -0,0 +1,8 @@
var a = 1,
b = 2,
c = 3;
var result = a
|> (a, b) => b
|> (a, b) => c;
assert.equal(result, c)

View File

@ -0,0 +1,8 @@
var a = 1,
b = 2,
c = 3;
var result = a
|> (a, b) => b
|> (a, b) => c;
assert.equal(result, c)

View File

@ -0,0 +1,11 @@
var _a;
var a = 1,
b = 2,
c = 3;
var result = (_a = a, ((a, b) => {
var _b;
return _b = b, ((a, b) => c)(_b);
})(_a));
assert.equal(result, c);

View File

@ -0,0 +1,13 @@
var obj = {
get prop() {
return this._prop = 1;
},
get method() {
if (!this._prop) throw new Error('invalid evaluation order');
return (v) => v;
}
}
var result = obj.prop |> obj.method;
assert.equal(result, 1);

View File

@ -0,0 +1,13 @@
var obj = {
get prop() {
return this._prop = 1;
},
get method() {
if (!this._prop) throw new Error('invalid evaluation order');
return (v) => v;
}
}
var result = obj.prop |> obj.method;
assert.equal(result, 1);

View File

@ -0,0 +1,15 @@
var _obj$prop;
var obj = {
get prop() {
return this._prop = 1;
},
get method() {
if (!this._prop) throw new Error('invalid evaluation order');
return v => v;
}
};
var result = (_obj$prop = obj.prop, obj.method(_obj$prop));
assert.equal(result, 1);

View File

@ -0,0 +1,7 @@
(function() {
'use strict';
var result = '(function() { return this; })()'
|> eval;
assert.notEqual(result, undefined);
})();

View File

@ -0,0 +1,7 @@
(function() {
'use strict';
var result = '(function() { return this; })()'
|> eval;
assert.notEqual(result, undefined);
})();

View File

@ -0,0 +1,8 @@
(function () {
'use strict';
var _functionReturn;
var result = (_functionReturn = '(function() { return this; })()', (0, eval)(_functionReturn));
assert.notEqual(result, undefined);
})();

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,8 @@
var a = 1,
b = 2,
c = 3;
var result = a
|> () => b
|> () => c;
assert.equal(result, c)

View File

@ -0,0 +1,8 @@
var a = 1,
b = 2,
c = 3;
var result = a
|> () => b
|> () => c;
assert.equal(result, c)

View File

@ -0,0 +1,5 @@
var a = 1,
b = 2,
c = 3;
var result = (a, (b, c));
assert.equal(result, c);

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,13 @@
var _ref, _ref2, _;
var inc = x => x + 1;
var result = (_ref = 4 || 9, inc(_ref));
assert.equal(result, 5);
var f = x => x + 10;
var h = x => x + 20;
var result2 = (_ref2 = (_ = 10, (f || h)(_)), inc(_ref2));
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

@ -11,7 +11,7 @@
"babel-code-frame": "7.0.0-beta.2",
"babel-helper-function-name": "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"