New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transform for F# Pipeline #9984
Merged
nicolo-ribaudo
merged 2 commits into
babel:feature-7.5.0/fsharp-pipeline
from
thiagoarrais:pipeline-transform-fsharp
May 25, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/babel-plugin-proposal-pipeline-operator/src/buildOptimizedSequenceExpression.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { types as t } from "@babel/core"; | ||
|
||
// tries to optimize sequence expressions in the format | ||
// (a = b, ((c) => d + e)(a)) | ||
// to | ||
// (a = b, a + e) | ||
const buildOptimizedSequenceExpression = ({ assign, call, path }) => { | ||
const { left: placeholderNode, right: pipelineLeft } = assign; | ||
const { callee: calledExpression } = call; | ||
|
||
let optimizeArrow = | ||
t.isArrowFunctionExpression(calledExpression) && | ||
t.isExpression(calledExpression.body) && | ||
!calledExpression.async && | ||
!calledExpression.generator; | ||
let param; | ||
|
||
if (optimizeArrow) { | ||
const { params } = calledExpression; | ||
if (params.length === 1 && t.isIdentifier(params[0])) { | ||
param = params[0]; | ||
} else if (params.length > 0) { | ||
optimizeArrow = false; | ||
} | ||
} else if (t.isIdentifier(calledExpression, { name: "eval" })) { | ||
const evalSequence = t.sequenceExpression([ | ||
t.numericLiteral(0), | ||
calledExpression, | ||
]); | ||
|
||
call.callee = evalSequence; | ||
|
||
path.scope.push({ id: placeholderNode }); | ||
|
||
return t.sequenceExpression([assign, call]); | ||
} | ||
|
||
if (optimizeArrow && !param) { | ||
// Arrow function with 0 arguments | ||
return t.sequenceExpression([pipelineLeft, calledExpression.body]); | ||
} | ||
|
||
path.scope.push({ id: placeholderNode }); | ||
|
||
if (param) { | ||
path.get("right").scope.rename(param.name, placeholderNode.name); | ||
|
||
return t.sequenceExpression([assign, calledExpression.body]); | ||
} | ||
|
||
return t.sequenceExpression([assign, call]); | ||
}; | ||
|
||
export default buildOptimizedSequenceExpression; |
25 changes: 25 additions & 0 deletions
25
packages/babel-plugin-proposal-pipeline-operator/src/fsharpVisitor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { types as t } from "@babel/core"; | ||
import buildOptimizedSequenceExpression from "./buildOptimizedSequenceExpression"; | ||
|
||
const fsharpVisitor = { | ||
BinaryExpression(path) { | ||
const { scope, node } = path; | ||
const { operator, left, right } = node; | ||
if (operator !== "|>") return; | ||
|
||
const placeholder = scope.generateUidIdentifierBasedOnNode(left); | ||
|
||
const call = | ||
right.type === "AwaitExpression" | ||
? t.awaitExpression(t.cloneNode(placeholder)) | ||
: t.callExpression(right, [t.cloneNode(placeholder)]); | ||
const sequence = buildOptimizedSequenceExpression({ | ||
assign: t.assignmentExpression("=", t.cloneNode(placeholder), left), | ||
call, | ||
path, | ||
}); | ||
path.replaceWith(sequence); | ||
}, | ||
}; | ||
|
||
export default fsharpVisitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 9 additions & 39 deletions
48
packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...-plugin-proposal-pipeline-operator/test/fixtures/fsharp/arrow-functions-parenless/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const y = 2; | ||
|
||
const f = (x) => (x |> (y) => y + 1) | ||
|> (z) => z * y | ||
|
||
const _f = (x) => x | ||
|> (y) => y + 1 | ||
|> (z) => z * y | ||
|
||
const g = (x) => x | ||
|> (y) => (y + 1 |> (z) => z * y) | ||
|
||
const _g = (x) => x | ||
|> (y => (y + 1 |> (z) => z * y)) | ||
|
||
const __g = (x) => x | ||
|> ( | ||
y => { | ||
return (y + 1 |> (z) => z * y); | ||
} | ||
) | ||
|
||
expect( f(1)).toBe(4); | ||
expect( _f(1)).toBe(4); | ||
expect( g(1)).toBe(2); | ||
expect( _g(1)).toBe(2); | ||
expect(__g(1)).toBe(2); |
14 changes: 14 additions & 0 deletions
14
...ages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/arrow-functions/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
expect(result).toBe(31); | ||
|
||
|
||
var inc = (x) => x + 1; | ||
var double = (x) => x * 2; | ||
|
||
var result2 = [4, 9].map( x => x |> inc |> double ) | ||
|
||
expect(result2).toEqual([10, 20]); |
14 changes: 14 additions & 0 deletions
14
...ges/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/arrow-functions/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
expect(result).toBe(31); | ||
|
||
|
||
var inc = (x) => x + 1; | ||
var double = (x) => x * 2; | ||
|
||
var result2 = [4, 9].map( x => x |> inc |> double ) | ||
|
||
expect(result2).toEqual([10, 20]); |
15 changes: 15 additions & 0 deletions
15
...es/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/arrow-functions/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var _ref, _ref2, _ref3; | ||
|
||
var result = (_ref = (_ref2 = (_ref3 = [5, 10], _ref3.map(x => x * 2)), _ref2.reduce((a, b) => a + b)), _ref + 1); | ||
expect(result).toBe(31); | ||
|
||
var inc = x => x + 1; | ||
|
||
var double = x => x * 2; | ||
|
||
var result2 = [4, 9].map(x => { | ||
var _ref4, _x; | ||
|
||
return _ref4 = (_x = x, inc(_x)), double(_ref4); | ||
}); | ||
expect(result2).toEqual([10, 20]); |
13 changes: 13 additions & 0 deletions
13
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/await/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const triple = (x) => x * 3; | ||
|
||
async function myFunction(n) { | ||
return n | ||
|> Math.abs | ||
|> Promise.resolve | ||
|> await | ||
|> triple; | ||
} | ||
|
||
return myFunction(-7).then(result => { | ||
expect(result).toBe(21); | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/await/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
async function myFunction(n) { | ||
return n | ||
|> Math.abs | ||
|> Promise.resolve | ||
|> await; | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/await/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"plugins": [ | ||
["proposal-pipeline-operator", { "proposal": "fsharp" }] | ||
], | ||
"parserOpts": { | ||
"allowReturnOutsideFunction": true | ||
}, | ||
"minNodeVersion": "8.0.0" | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/await/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
async function myFunction(n) { | ||
var _ref, _ref2, _n; | ||
|
||
return _ref = (_ref2 = (_n = n, Math.abs(_n)), Promise.resolve(_ref2)), await _ref; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/basic/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var inc = (x) => x + 1 | ||
|
||
expect(10 |> inc).toBe(11); |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/basic/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var inc = (x) => x + 1 | ||
|
||
expect(10 |> inc).toBe(11); |
5 changes: 5 additions & 0 deletions
5
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/basic/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var _; | ||
|
||
var inc = x => x + 1; | ||
|
||
expect((_ = 10, inc(_))).toBe(11); |
7 changes: 7 additions & 0 deletions
7
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/indirect-eval/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(function() { | ||
'use strict'; | ||
var result = '(function() { return this; })()' | ||
|> eval; | ||
|
||
expect(result).not.toBeUndefined(); | ||
})(); |
7 changes: 7 additions & 0 deletions
7
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/indirect-eval/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
(function() { | ||
'use strict'; | ||
var result = '(function() { return this; })()' | ||
|> eval; | ||
|
||
expect(result).not.toBeUndefined(); | ||
})(); |
8 changes: 8 additions & 0 deletions
8
...ages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/indirect-eval/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(function () { | ||
'use strict'; | ||
|
||
var _functionReturn; | ||
|
||
var result = (_functionReturn = '(function() { return this; })()', (0, eval)(_functionReturn)); | ||
expect(result).not.toBeUndefined(); | ||
})(); |
5 changes: 5 additions & 0 deletions
5
...bel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/multiple-argument-use/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
||
expect(last).toBe(30); |
5 changes: 5 additions & 0 deletions
5
...el-plugin-proposal-pipeline-operator/test/fixtures/fsharp/multiple-argument-use/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var _array; | ||
|
||
var array = [10, 20, 30]; | ||
var last = (_array = array, _array[_array.length - 1]); | ||
expect(last).toBe(30); |
8 changes: 8 additions & 0 deletions
8
...bel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/optimize-0-param-arrow/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var a = 1, | ||
b = 2, | ||
c = 3; | ||
var result = a | ||
|> (() => b) | ||
|> (() => c); | ||
|
||
expect(result).toBe(c); |
8 changes: 8 additions & 0 deletions
8
...el-plugin-proposal-pipeline-operator/test/fixtures/fsharp/optimize-0-param-arrow/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var a = 1, | ||
b = 2, | ||
c = 3; | ||
var result = a | ||
|> (() => b) | ||
|> (() => c); | ||
|
||
expect(result).toBe(c); |
5 changes: 5 additions & 0 deletions
5
...l-plugin-proposal-pipeline-operator/test/fixtures/fsharp/optimize-0-param-arrow/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var a = 1, | ||
b = 2, | ||
c = 3; | ||
var result = ((a, b), c); | ||
expect(result).toBe(c); |
8 changes: 8 additions & 0 deletions
8
packages/babel-plugin-proposal-pipeline-operator/test/fixtures/fsharp/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"plugins": [ | ||
["proposal-pipeline-operator", { "proposal": "fsharp" }] | ||
], | ||
"parserOpts": { | ||
"allowReturnOutsideFunction": true | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...plugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions-parenless/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const y = 2; | ||
const f = (x) => x | ||
|> (y => y + 1) | ||
|> (z => z * y) | ||
|
||
const g = (x) => x | ||
|> (y => | ||
y + 1 | ||
|> (z => z * y) | ||
) | ||
|
||
const h = (x) => x | ||
|> (y => ( | ||
y + 1 | ||
|> (z => z * y) | ||
)) | ||
|
||
expect(f(1)).toBe(4); | ||
expect(g(1)).toBe(2); | ||
expect(h(1)).toBe(2); |
20 changes: 20 additions & 0 deletions
20
...lugin-proposal-pipeline-operator/test/fixtures/minimal/arrow-functions-parenless/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const y = 2; | ||
const f = (x) => x | ||
|> (y => y + 1) | ||
|> (z => z * y) | ||
|
||
const g = (x) => x | ||
|> (y => | ||
y + 1 | ||
|> (z => z * y) | ||
) | ||
|
||
const h = (x) => x | ||
|> (y => ( | ||
y + 1 | ||
|> (z => z * y) | ||
)) | ||
|
||
expect(f(1)).toBe(4); | ||
expect(g(1)).toBe(2); | ||
expect(h(1)).toBe(2); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Note for reviewers: this function isn't new; it has been moved here from https://github.com/babel/babel/blob/b5febcf1f993cc3ec36fda7c7908181048a0069f/packages/babel-plugin-proposal-pipeline-operator/src/minimalVisitor.js)