Skip to content

Commit

Permalink
Rename exception var when local var has the same name.
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Sep 26, 2024
1 parent 0230758 commit d315065
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
26 changes: 26 additions & 0 deletions docs/exceptions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Exceptions
## Omitting the exception variable
### Standard use case
There are times where you don't need to use the exception variable in a try/catch. BrighterScript allows you to omit the variable. At transpile time, we'll inject a variable so that your output is still valid brightscript

```BrighterScript
Expand All @@ -23,3 +24,28 @@ function init()
end try
end function
```

### Exception variable name collision
By default, bsc will name the omitted variable `e`. However, if you've already got a variable named `e` in your function body, we'll get out of your way and name it `__bsc_error` instead.

```BrighterScript
function init()
try
somethingDangerous()
catch
print "I live on the edge"
end try
end function
```

transpiles to

```BrighterScript
function init()
try
somethingDangerous()
catch __bsc_error
print "I live on the edge"
end try
end function
```
11 changes: 10 additions & 1 deletion docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ enum RemoteButton
end enum
```

## [Exceptions](exceptions.md)
```brighterscript
try
somethingDangerous()
catch 'look, no exception variable!
print "Living on the edge!"
end try
```

## [Namespaces](namespaces.md)
```brighterscript
namespace util
Expand Down Expand Up @@ -133,4 +142,4 @@ end sub
```

## [Variable Shadowing](variable-shadowing.md)
Name resolution rules for various types of shadowing.
Name resolution rules for various types of shadowing.
22 changes: 22 additions & 0 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,28 @@ describe('BrsFile', () => {
end sub
`, undefined, 'source/main.bs');
});

it('uses alternate name when `e` is already a local var', async () => {
await testTranspile(`
sub new()
e = "ello love"
try
print "hello"
catch
print "error"
end try
end sub
`, `
sub new()
e = "ello love"
try
print "hello"
catch __bsc_error
print "error"
end try
end sub
`, undefined, 'source/main.bs');
});
});

describe('namespaces', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/parser/Statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3141,7 +3141,12 @@ export class CatchStatement extends Statement {
return [
state.transpileToken(this.tokens.catch, 'catch'),
' ',
this.exceptionVariableExpression?.transpile(state) ?? ['e'],
this.exceptionVariableExpression?.transpile(state) ?? [
//use the variable named `e` if it doesn't exist in this function body. otherwise use '__bsc_error' just to make sure we're out of the way
this.getSymbolTable()?.hasSymbol('e', SymbolTypeFlag.runtime)
? '__bsc_error'
: 'e'
],
...(this.catchBranch?.transpile(state) ?? [])
];
}
Expand Down

0 comments on commit d315065

Please sign in to comment.