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/enhanced-try' into prerelease/3.1.0
- Loading branch information
Showing
20 changed files
with
203 additions
and
157 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 was deleted.
Oops, something went wrong.
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,116 @@ | ||
import t, { isa } from '../types' | ||
import { transformTails } from '../helpers/tails' | ||
import { toBlockStatement } from '../helpers/blocks' | ||
|
||
import { | ||
getLoc, placeAtLoc as atLoc, placeAtNode as atNode, | ||
getSurroundingLoc, span, traverse, | ||
placeTreeAtLocWhenUnplaced as allAtLoc | ||
} from 'ast-loc-utils' | ||
|
||
import { getMatchInfo, transformMatchCases } from './match' | ||
|
||
normalizeTryStatement(node, path): void -> | ||
// Turn stuff into blocks as needed | ||
if node.block: node.block = node.block~toBlockStatement() | ||
if node.handler?.body: node.handler.body = node.handler.body~toBlockStatement() | ||
if node.finalizer: node.finalizer = node.finalizer~toBlockStatement() | ||
// Paramless handler gets a default param | ||
if node.handler and (not node.handler.param): | ||
node.handler.param = path.scope.generateUidIdentifier("err") | ||
// Generate default handler | ||
if (not node.handler) and (not node.finalizer): | ||
errId = path.scope.generateUidIdentifier("err") | ||
node.handler = t.catchClause( | ||
errId, | ||
t.blockStatement([t.expressionStatement(errId)]) | ||
) | ||
|
||
transformVarDeclTryExpression(path, tryExprPath): void -> | ||
{ node } = tryExprPath | ||
|
||
// Starting from `const x = try ...` | ||
// Add `let _val` | ||
// let _val | ||
// const x = try ... | ||
resRef = path.scope.generateUidIdentifier("val") | ||
path.insertBefore! t.variableDeclaration("let", [t.variableDeclarator(resRef)]) | ||
|
||
// replace `try ...` with `_val` | ||
// let _val | ||
// const x = _val | ||
declaratorPath = path.get("declarations.0") | ||
declaratorPath.node.init = resRef | ||
|
||
// Turn the original node into a try statement and add it before the decl | ||
// let _val | ||
// try ... | ||
// const x = _val | ||
node.type = "TryStatement" | ||
normalizeTryStatement(node, tryExprPath) | ||
path.insertBefore! node | ||
|
||
// Re-tail the `try` statement so it assigns its final value to `_val` | ||
tryPath = path.getPrevSibling() | ||
transformTails(tryPath, false, false, (node) -> | ||
t.assignmentExpression("=", resRef, node)~atNode(node) | ||
) | ||
|
||
transformPessimizedTryExpression(path): void -> | ||
{ node } = path | ||
|
||
// If there are any awaits beneath us, we need async. | ||
let needsAsync = false | ||
path.traverse({ | ||
noScope: true | ||
|
||
Function(path): void -> path.skip() | ||
|
||
AwaitExpression(awaitPath) -> | ||
now needsAsync = true | ||
awaitPath.stop() | ||
|
||
YieldExpression(yieldPath) -> | ||
throw yieldPath.buildCodeFrameError("`yield` is not allowed in `try` expressions.") | ||
|
||
ReturnStatement(returnPath) -> | ||
throw returnPath.buildCodeFrameError("`return` is not allowed in `try` expressions."); | ||
}) | ||
|
||
node.type = "TryStatement" | ||
normalizeTryStatement(node, path) | ||
|
||
iife = t.callExpression( | ||
t.arrowFunctionExpression( | ||
[] | ||
t.blockStatement([node]) | ||
needsAsync | ||
) | ||
[] | ||
) | ||
|
||
path.replaceWith(iife) | ||
|
||
isVarDeclTryExpr(path) -> | ||
path.parent~isa("VariableDeclarator") | ||
and path.parentPath.parent.declarations.length == 1 | ||
and path.parentPath.parentPath.listKey == "body" | ||
|
||
export transformTryExpression(path, isLinter): void -> | ||
if path~isVarDeclTryExpr!: | ||
transformVarDeclTryExpression(path.parentPath.parentPath, path, isLinter) | ||
else: | ||
transformPessimizedTryExpression(path, isLinter) | ||
|
||
export transformTryStatement(path): void -> | ||
{ node } = path | ||
normalizeTryStatement(node, path) | ||
path.replaceWith(node) | ||
|
||
export transformCatchClause(path, isLinter): void -> | ||
{ node } = path | ||
|
||
if node.cases: | ||
nextBody = getMatchInfo(path, path.node.param, isLinter)~transformMatchCases(path.get("cases")) | ||
node.body = nextBody~toBlockStatement() | ||
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
2 changes: 1 addition & 1 deletion
2
...fixtures/catch-expression/await/actual.js → test/fixtures/enhanced-try/await/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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
f() -/> | ||
<- fetchRemote() | ||
try <- fetchRemote() | ||
catch err: false |
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
File renamed without changes.
6 changes: 3 additions & 3 deletions
6
...fixtures/catch-expression/basic/actual.js → test/fixtures/enhanced-try/basic/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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
a = b() | ||
a = try b() | ||
catch err: panic() | ||
|
||
c = d() catch err: | ||
c = try d() catch err: | ||
| SpecificError: specificValue | ||
| Error: generalValue | ||
|
||
e = f() | ||
e = try f() | ||
catch err: | ||
| SpecificError: specificValue | ||
| Error: generalValue |
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 @@ | ||
wrap() -> | ||
f(try g() catch err: return h) |
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": "`return` is not allowed in `try` expressions." | ||
} |
Oops, something went wrong.