forked from lightscript/babel-plugin-lightscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 5fa8631 Author: William C. Johnson <wcjohnson@oigroup.net> Date: Sun Jul 16 20:42:22 2017 -0400 Allow placeholder to be changed commit ee2f7a7 Author: William C. Johnson <wcjohnson@oigroup.net> Date: Sun Jul 16 20:20:23 2017 -0400 Support rest/spread placeholder args commit cd625f1 Author: William C. Johnson <wcjohnson@oigroup.net> Date: Sun Jul 16 19:41:02 2017 -0400 Initial placeholderArgs implementation + testing
- Loading branch information
Showing
33 changed files
with
311 additions
and
8 deletions.
There are no files selected for viewing
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
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 @@ | ||
// Perform non-semantic AST transformations like ensuring function bodies | ||
// are blocks, etc. | ||
import { isNamedArrowFunction } from './functions' | ||
import { toBlockStatement } from './blocks' | ||
|
||
export fixAst(programPath) -> | ||
programPath.traverse({ | ||
Method(path): void -> | ||
{ node } = path | ||
if isNamedArrowFunction(node): | ||
node.body = toBlockStatement(node.body) | ||
path.replaceWith(node) | ||
}) |
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
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
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,73 @@ | ||
import t from './types' | ||
|
||
registerPlaceholder(functions, path, programPath) -> | ||
fnPath = path.getFunctionParent() | ||
if fnPath == programPath || !fnPath.node || !fnPath.node.params: | ||
throw path.buildCodeFrameError("Placeholders cannot be used outside functions.") | ||
|
||
if fnPath.node.params.length: | ||
throw path.buildCodeFrameError("Placeholders cannot be used in functions with arguments.") | ||
|
||
if path.parentPath?.node?.type == "SpreadElement": | ||
if path.node?.index?: | ||
throw path.buildCodeFrameError("Cannot use indices with spread placeholders.") | ||
|
||
let fnInfo = functions.get(fnPath) | ||
if fnInfo: | ||
fnInfo.placeholders.push(path) | ||
else: | ||
functions.set(fnPath, { functionPath: fnPath, placeholders: [ path ] }) | ||
|
||
liftPlaceholders(fnPath, phPaths) -> | ||
// Collate placeholders | ||
ixPhPaths = phPaths.filter(x -> x.node.index?) | ||
nixPhPaths = phPaths.filter(x-> not x.node.index?) | ||
ixPhPaths.sort((a, b) -> a.node.index - b.node.index) | ||
nixPhPaths.sort((a, b) -> | ||
// SpreadElements are last | ||
if a.parentPath?.node.type == "SpreadElement": | ||
1 | ||
elif b.parentPath?.node.type == "SpreadElement": | ||
-1 | ||
else: | ||
0 | ||
) | ||
sortedPhPaths = ixPhPaths.concat(nixPhPaths) | ||
|
||
// Formulate args | ||
args = [] | ||
let prevIndex = undefined, restArg = undefined | ||
for elem phPath in sortedPhPaths: | ||
{ node } = phPath | ||
if phPath.parentPath?.node.type == "SpreadElement": | ||
if not restArg: | ||
now restArg = fnPath.scope.generateUidIdentifier("arg") | ||
args.push(t.restElement(restArg)) | ||
phPath.replaceWith(restArg) | ||
elif node.index? and node.index == prevIndex: | ||
// Repeated use of an index = same arg. | ||
phPath.replaceWith(args[args.length - 1]) | ||
else: | ||
args.push(fnPath.scope.generateUidIdentifier("arg")) | ||
now prevIndex = node.index | ||
phPath.replaceWith(args[args.length - 1]) | ||
|
||
// Lift args to function level | ||
fnNode = fnPath.node | ||
fnNode.params = args | ||
fnPath.replaceWith(fnNode) | ||
|
||
// Collect all functions with placeholders into a Map, one entry per function | ||
findFunctionsWithPlaceholders(programPath) -> | ||
fns = new Map() | ||
programPath.traverse({ | ||
PlaceholderExpression(path): void -> | ||
registerPlaceholder(fns, path, programPath) | ||
}) | ||
fns | ||
|
||
export transformPlaceholders(programPath) -> | ||
fns = programPath~findFunctionsWithPlaceholders() | ||
fns.forEach( fnInfo -> | ||
fnInfo.functionPath~liftPlaceholders(fnInfo.placeholders) | ||
) |
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,30 @@ | ||
-> _ | ||
|
||
=> _ | ||
|
||
f() -> _ | ||
|
||
g() => _ | ||
|
||
fa() -/> _ | ||
|
||
ga() =/> _ | ||
|
||
fg() -*> _ | ||
|
||
gg() =*> _ | ||
|
||
fga() -*/> _ | ||
|
||
gga() -*/> _ | ||
|
||
{ | ||
h() -> _ | ||
i() => _ | ||
j: -> _ | ||
} | ||
|
||
class X { | ||
k() -> _ | ||
l() => _ | ||
} |
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,60 @@ | ||
var _obj; | ||
|
||
(function (_arg) { | ||
return _arg; | ||
}); | ||
|
||
(_arg2) => _arg2; | ||
|
||
function f(_arg3) { | ||
return _arg3; | ||
} | ||
|
||
const g = (_arg4) => _arg4; | ||
|
||
async function fa(_arg5) { | ||
return _arg5; | ||
} | ||
|
||
const ga = async (_arg6) => _arg6; | ||
|
||
function* fg(_arg7) { | ||
return _arg7; | ||
} | ||
|
||
function* gg(_arg8) { | ||
return _arg8; | ||
} | ||
|
||
gg = gg.bind(this)async function* fga(_arg9) { | ||
return _arg9; | ||
} | ||
|
||
async function* gga(_arg10) { | ||
return _arg10; | ||
} | ||
|
||
_obj = { | ||
h(_arg11) { | ||
return _arg11; | ||
}, | ||
i(_arg12) { | ||
return _arg12; | ||
}, | ||
j: function (_arg13) { | ||
return _arg13; | ||
} | ||
}, _obj.i = _obj.i.bind(_obj), _obj; | ||
|
||
class X { | ||
constructor() { | ||
this.l = this.l.bind(this); | ||
} | ||
|
||
k(_arg14) { | ||
return _arg14; | ||
} | ||
l(_arg15) { | ||
return _arg15; | ||
} | ||
} |
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 @@ | ||
-> _ | ||
|
||
-> _(_) |
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 (_arg) { | ||
return _arg; | ||
}); | ||
|
||
(function (_arg2, _arg3) { | ||
return _arg2(_arg3); | ||
}); |
3 changes: 3 additions & 0 deletions
3
test/fixtures/placeholder-args/change-placeholder-directive/actual.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 @@ | ||
'use @oigroup/lightscript with placeholder: "P"' | ||
|
||
-> P |
5 changes: 5 additions & 0 deletions
5
test/fixtures/placeholder-args/change-placeholder-directive/expected.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 @@ | ||
'use @oigroup/lightscript with placeholder: "P"'; | ||
|
||
(function (_arg) { | ||
return _arg; | ||
}); |
1 change: 1 addition & 0 deletions
1
test/fixtures/placeholder-args/change-placeholder-opts/actual.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 @@ | ||
-> P |
3 changes: 3 additions & 0 deletions
3
test/fixtures/placeholder-args/change-placeholder-opts/expected.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 @@ | ||
(function (_arg) { | ||
return _arg; | ||
}); |
3 changes: 3 additions & 0 deletions
3
test/fixtures/placeholder-args/change-placeholder-opts/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,3 @@ | ||
{ | ||
"plugins": [["lightscript", { "placeholderArgs": true, "placeholder": "P" }]] | ||
} |
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 @@ | ||
x.map(-> _ + _1) | ||
|
||
y.map(=> _ + _1) |
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 @@ | ||
x.map(function (_arg, _arg2) { | ||
return _arg2 + _arg; | ||
}); | ||
|
||
y.map((_arg3, _arg4) => _arg4 + _arg3); |
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 @@ | ||
-> _._1 | ||
|
||
-> _(_._)._1[_2] |
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 @@ | ||
(function (_arg, _arg2) { | ||
return _arg2._arg; | ||
});(function (_arg3, _arg4, _arg5, _arg6, _arg7) { | ||
return _arg5(_arg6._arg7)._arg3[_arg4]; | ||
}); |
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 @@ | ||
-> _.map(-> _ + 1) |
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 @@ | ||
(function (_arg) { | ||
return _arg.map(function (_arg2) { | ||
return _arg2 + 1; | ||
}); | ||
}); |
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 @@ | ||
{ | ||
"plugins": [["lightscript", { "placeholderArgs": true }]] | ||
} |
1 change: 1 addition & 0 deletions
1
test/fixtures/placeholder-args/outside-function-illegal/actual.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 @@ | ||
_ + 1 |
3 changes: 3 additions & 0 deletions
3
test/fixtures/placeholder-args/outside-function-illegal/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,3 @@ | ||
{ | ||
"throws": "Placeholders cannot be used outside functions." | ||
} |
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 @@ | ||
-> _2._1 | ||
|
||
-> _1._1 | ||
|
||
-> _2._2._1._1 | ||
|
||
-> _8(_7) |
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 @@ | ||
(function (_arg, _arg2) { | ||
return _arg2._arg; | ||
});(function (_arg3) { | ||
return _arg3._arg3; | ||
});(function (_arg4, _arg5) { | ||
return _arg5._arg5._arg4._arg4; | ||
});(function (_arg6, _arg7) { | ||
return _arg7(_arg6); | ||
}); |
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,2 @@ | ||
class X: | ||
set y() -> _ |
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 @@ | ||
{ | ||
"throws": "setter should have exactly one param (2:2)" | ||
} |
2 changes: 2 additions & 0 deletions
2
test/fixtures/placeholder-args/spread-index-illegal/actual.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,2 @@ | ||
-> | ||
a = [..._1] |
3 changes: 3 additions & 0 deletions
3
test/fixtures/placeholder-args/spread-index-illegal/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,3 @@ | ||
{ | ||
"throws": "Cannot use indices with spread placeholders." | ||
} |
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 @@ | ||
-> f(_2, _1, _, ..._) | ||
|
||
-> | ||
a = [..._] | ||
f(_2, _1, _, a) | ||
|
||
-> | ||
a = [..._] | ||
b = [..._] |
Oops, something went wrong.