-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for types declared in comments (#1293)
* Add basic docs * Added a BrsDoc Parser * Remove leading apostrophes in brsdocs * Added a comment on apostrophes * BrsDoc parser can get parsed docs for a node * Support for @retrun and @type tags * Just comments and names * Defined case sensitivity of doc parsing * Adds diagnostics for bad types in doc comment tags * Unifies doc types so it uses a parsed expression * a few tweaks * Diagnostics on bad types in doc comments unerline the type --------- Co-authored-by: Mark Pearce <mark.pearce@redspace.com>
- Loading branch information
1 parent
43cbf8b
commit 8d5f810
Showing
18 changed files
with
1,420 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Typechecking without transpiling | ||
If you want the benefits of BrighterScript's type system but don't want to use the transpiler, you can use comments to declare variable types to get much richer editor experience and type validation. | ||
|
||
## Params | ||
Declare the type of a specific parameter: | ||
|
||
```brighterscript | ||
'@param {roSGNodeRectangle} node | ||
function resize(node, width as integer, height as string) | ||
'yay, we know that `node` is a `roSGNodeRectangle` type | ||
node.width = width | ||
node.height = height | ||
end function | ||
``` | ||
|
||
## Return type | ||
|
||
Declare the return type of a function | ||
|
||
```brighterscript | ||
'@return {roSGNodeRectangle} | ||
function createRectangle() | ||
return createObject("roSGNode", "Rectangle") | ||
end function | ||
function test() | ||
'yay, we know that `rectangle` is a `roSGNodeRectangle` type | ||
rectangle = createRectangle() | ||
end function | ||
``` | ||
|
||
## Inline variable type | ||
You can override the type of a variable in brs files by starting a comment with `@type` . This will cast the variable below with the given type. | ||
|
||
```brighterscript | ||
' @type {integer} | ||
runtime = getRuntimeFromServer() | ||
``` | ||
|
||
## TODO: Function-level variable type | ||
Sometimes you need to declare a variable at more than one location in your code. In this situation, you can declare the variable type in the body of the function before the variable usage, and that variable will then be treated as that type. | ||
|
||
```brighterscript | ||
' @type {integer} runtime | ||
if m.top.hasRuntime | ||
runtime = m.top.runtime | ||
else | ||
runtime = 12345 | ||
end if | ||
``` |
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.