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.
Merge branch 'feature/catch-expressions' into prerelease/3.0.0
- Loading branch information
Showing
9 changed files
with
151 additions
and
2 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
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,80 @@ | ||
import t, { isa } from '../types' | ||
import { transformTails } from '../helpers/tails' | ||
|
||
import { | ||
getLoc, placeAtLoc as atLoc, placeAtNode as atNode, | ||
getSurroundingLoc, span, traverse, | ||
placeTreeAtLocWhenUnplaced as allAtLoc | ||
} from 'ast-loc-utils' | ||
|
||
import { getMatchInfo, transformMatchCases } from './match' | ||
|
||
transformVarDeclCatchExpression(path, catchExprPath, isLinter): void -> | ||
{ node } = catchExprPath | ||
|
||
resRef = path.scope.generateUidIdentifier("val") | ||
errRef = path.scope.generateUidIdentifier("err")~atLoc(getLoc(node)~span(1)) | ||
catchBody = getMatchInfo(catchExprPath, errRef, isLinter)~transformMatchCases(catchExprPath.get("cases")) | ||
|
||
path.insertBefore! t.variableDeclaration("let", [t.variableDeclarator(resRef)]) | ||
path.insertBefore! t.tryStatement( | ||
// try { _val = expr } | ||
t.blockStatement([ | ||
t.expressionStatement(t.assignmentExpression("=", resRef, node.expression)) | ||
]) | ||
// catch (err) { ... } | ||
t.catchClause( | ||
errRef | ||
t.blockStatement([catchBody]) | ||
) | ||
) | ||
// x = _val | ||
declaratorPath = path.get("declarations.0") | ||
declaratorPath.node.init = resRef | ||
// Transform tails in the catch-clause to assignments | ||
tryPath = path.getPrevSibling() | ||
transformTails(tryPath.get("handler.body"), false, false, (node) -> | ||
t.assignmentExpression("=", resRef, node)~atNode(node) | ||
) | ||
|
||
transformPessimizedCatchExpression(path, isLinter): void -> | ||
{ node } = path | ||
|
||
errRef = path.scope.generateUidIdentifier("err")~atLoc(getLoc(node)~span(1)) | ||
catchBody = getMatchInfo(path, errRef, isLinter)~transformMatchCases(path.get("cases")) | ||
|
||
iife = t.callExpression( | ||
t.arrowFunctionExpression( | ||
[] | ||
t.blockStatement([ | ||
t.tryStatement( | ||
t.blockStatement([ | ||
t.returnStatement(node.expression) | ||
]) | ||
t.catchClause( | ||
errRef | ||
t.blockStatement([catchBody]) | ||
) | ||
) | ||
]) | ||
node.expression~isa("AwaitExpression") // async | ||
) | ||
[] | ||
) | ||
|
||
path.replaceWith(iife) | ||
|
||
isVarDeclCatchExpr(path) -> | ||
path.parent~isa("VariableDeclarator") | ||
and path.parentPath.parent.declarations.length == 1 | ||
and path.parentPath.parentPath.listKey == "body" | ||
|
||
export transformCatchExpression(path, isLinter): void -> | ||
console.log(path.parent) | ||
console.log(path.parentPath.parent) | ||
console.log(path.parentPath.parentPath.parent) | ||
if path~isVarDeclCatchExpr!: | ||
transformVarDeclCatchExpression(path.parentPath.parentPath, path, isLinter) | ||
else: | ||
transformPessimizedCatchExpression(path, isLinter) | ||
|
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,2 @@ | ||
a = b() | ||
catch Error: panic() |
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,12 @@ | ||
import _isMatch from "@oigroup/lightscript-runtime/isMatch"; | ||
let _val; | ||
|
||
try { | ||
_val = b(); | ||
} catch (_err) { | ||
if (_isMatch(Error, _err)) { | ||
_val = panic(); | ||
} | ||
} | ||
|
||
const a = _val; |
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": [ | ||
["lightscript", { | ||
"isLightScript": true, | ||
"catchExpression": true | ||
}] | ||
] | ||
} | ||
|