-
Notifications
You must be signed in to change notification settings - Fork 47
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
Formal support and tests for various Variable and name shadowing rules #1089
Merged
+623
−179
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5f9bad7
Changed shadowing logic to only add diagnostics when namespaces match
markwpearce efef4ec
Merge branch 'release-1.0.0' into open_up_shadowing
markwpearce 4a04e34
WIP of adding and refining tests
markwpearce 5bac4b5
Fixed all tests
markwpearce 07fdcdc
use cached callablecontainermap, refactor detectShadowedLocalVar to u…
markwpearce c651b4e
Updated docs
markwpearce 4fa58cd
updated readme
markwpearce fce70cf
Merge branch 'release-1.0.0' of https://github.com/rokucommunity/brig…
TwitchBronBron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,104 @@ | ||
# Variable Shadowing and Name Collisions in BrighterScript | ||
|
||
BrighterScript allows various kinds of [variable name shadowing](https://en.wikipedia.org/wiki/Variable_shadowing). In general, variables and types refer to items defined in the current scope/namespace if available. | ||
|
||
However, due to the limitations of BrightScript, there are some name collisions that lead to unpredictable behavior, and so they cause diagnostic errors. | ||
|
||
## Name Resolution Rules | ||
|
||
**1. Local variables CAN shadow names of global functions.** | ||
|
||
✅ | ||
|
||
```brighterscipt | ||
sub test() | ||
log = "value" ' identifier shadows global function log() - no error | ||
upTime = 0 ' identifier shadows global function Uptime() - no error | ||
end sub | ||
``` | ||
|
||
**2. Local variables CANNOT shadow names of functions or classes defined at the same scope.** | ||
|
||
❌ | ||
|
||
```brighterscipt | ||
sub test() | ||
pi = "Apple" ' identifier shadows function pi() - causes validation error | ||
data = 1234 ' identifier shadows class Data - causes validation error | ||
end sub | ||
|
||
function pi() as float | ||
return 3.14 | ||
function | ||
|
||
class Data | ||
value = {} | ||
end class | ||
``` | ||
|
||
**3. Custom types and definitions (enums, classes, interfaces, functions, consts) CANNOT have the same name if they are in the same namespace.** | ||
|
||
❌ | ||
|
||
```brighterscipt | ||
function someName() | ||
end function | ||
|
||
class SomeName ' class shadows local function - causes validation error | ||
sub foo() | ||
end sub | ||
end class | ||
|
||
namespace alpha | ||
class OtherName ' class in namespace shadows function in same namespace - causes validation error | ||
sub foo() | ||
end sub | ||
end class | ||
|
||
function otherName() | ||
end function | ||
end namespace | ||
``` | ||
|
||
**4. Functions and classes outside of namespaces CANNOT shadow standard global functions (eg. `ParseJson`, `LCase`, etc.)** | ||
|
||
❌ | ||
|
||
```brighterscipt | ||
class log ' class shadows global function - causes validation error | ||
sub foo() | ||
end sub | ||
end class | ||
|
||
|
||
``` | ||
|
||
**5. Definitions inside namespaces CAN shadow standard global functions, or functions at a difference namespace-level. In this way, the outer item is unavailable, and only the item defined at the current scope is accessible.** | ||
|
||
✅ | ||
|
||
```brighterscipt | ||
class SomeName() | ||
end class | ||
|
||
namespace alpha | ||
class SomeName ' class in namespace shadows function in upper scope | ||
sub foo() | ||
print "foo" | ||
end sub | ||
end class | ||
|
||
sub test() | ||
myKlass = new SomeName() ' refers to alpha.SomeName | ||
myKlass.foo() ' will print "foo" | ||
end sub | ||
|
||
sub log(data) ' function defined as alpha.log - this is ok | ||
print "LOG: ";data | ||
end sub | ||
|
||
sub foo() | ||
log("Hello world") ' refers to alpha.log - will print "LOG: Hello world" | ||
end sub | ||
end namespace | ||
``` |
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
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.
This is a great writeup, and really clear. Nice work!