-
Notifications
You must be signed in to change notification settings - Fork 53
Plugins Must Declare Annotations #1386
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
base: v1
Are you sure you want to change the base?
Conversation
|
The docs I added follow along with the description from the issue, but I'm thinking it may be easier to deal with annotations that are provided as Types? eg: (perhaps we could update the constructor or create a helper function to create it all at once?) This would mean we wouldn't have to parse the declaration, but instead use it directly... and wouldn't need to have extra error handling on bad parsed values, etc. |
luis-j-soares
left a comment
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.
Mostly off-topic feedback 👼
docs/plugins.md
Outdated
| 'inline()', | ||
| 'log(prefix as string, addLineNumbers = false as boolean)' |
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.
Already itching to use this for our internal plugins once v1 arrives. I do have a couple of questions, maybe both could be reflected in the docs?
- Can a plugin author expect the full type system to be supported, or will it only support the built-in types? (I see
onlyAllowLiterals: true, so maybe that's a "no"? I'm not familiar with the type system yet) - If I have annotation declaration that is implemented by more than one plugin, should a developer assume that the prevalent annotation is based on the order in which the plugins are declared in bsconfig?
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.
Annotation arguments can only be literals - there’s no way of knowing at compile time what the value of a variable is.
That being said, we could support const and enum values as annotation args too. Plan on that in v1.1
so that means, literal strings, integers, floats, literal arrays (with literal values) and literal AA’s can be used as args.
This would be perfectly fine:
@annotation(“test”, {values: [1.1, 4, 7.2], data:{name: “Luis”}}, [{abc: 123}])
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.
I’ll update the docs to reflect that
Co-authored-by: Luis Jacobetty Soares <57358121+luis-j-soares@users.noreply.github.com>
I think the challenge comes when we get into more advanced types for these annotations. Consider an annotation that takes one string, or exactly 2 arguments, or exactly 4 arguments (i know we don't have this in bsc yet, but might someday). In typescript we would represent that as: function someAnnotation(someArg: string | [number, number] | [number, number, number, number]) { }We'd be pushing plugin authors to all start learning a more about the type system than perhaps necessary depending on their annotation argument needs. It feels like declaring annotations as brighterscript code would be more approachable for plugin authors, and might also have a few other benefits:
Maybe I'm making this into a bigger deal than it needs to be? Thoughts? |
|
Perhaps a counter-argument to my argument above would be to provide plugin authors with a helper function to build types from code. Something like this: But it's not too hard to just do this with existing paradigms either... const type = Parser.parse(`
function test(arg as string | [integer, integer] | [integer, integer, integer, integer]): end function
`).ast.findChild(isFunctionStatement).getType(); |
| For example: | ||
| ```typescript | ||
| this.annotations = [ |
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.
It would be really nice to enforce which statement types this annotation could be declared on. For example, rooibos @suite() only works when attached to classes. Adding @suite() to a namespace should be an error. BrighterScript could provide consistent validations for these. I envision configuring it something like this:
this.annotations.push({
restrictTo: ['class'], //this would be a list of all types the annotation maybe declared above
func: new TypedFunctionType(VoidType.instance).setName('inline'),
{
description: 'Add a log message whenever this function is called',
type: new TypedFunctionType(VoidType.instance)
.setName('log')
.addParameter('prefix', StringType.instance)
.addParameter('addLineNumbers', BooleanType.instance, true)
}
Addresses #1348
Plugins must declare their annotations, so that can be validated.
Future PR:
Extra
infologging: