feat: parse and preserve parameter decorators in AST#3023
feat: parse and preserve parameter decorators in AST#3023PaperPrototype wants to merge 2 commits intoAssemblyScript:mainfrom
Conversation
Adds support for decorator syntax on function parameters (e.g. `@decorator value: i32`). Decorators are parsed and stored in `ParameterNode.decorators` and `FunctionTypeNode.explicitThisDecorators`, making them available to transformer plugins without affecting compilation. - Parser: parses leading parameter decorators into AST; emits TS1206 only for misplaced decorators (e.g. `value @Deco: i32`) - AST: new `decorators` field on ParameterNode, `explicitThisDecorators` on FunctionTypeNode, `decoratedFunctionTypes` index on Source - Serialization: inline parameter decorator serialization in extra/ast.ts - Transform API: new `beforeCompile(program)` hook in cli/index.d.ts for transformer-owned validation after program init, before WASM compilation - Tests: parser round-trip, compiler clean-compile, parser error cases, and transform example that reads parameter decorators via afterInitialize Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
I regenerated fixtures as well, there was no difference. |
|
Oh, do I need to add my name to the contributors list? Er, idk how that all works. EDIT: I already did, i just forgot |
|
You could have just forced a merge of the latest main branch into your branch. But if you've already started a new one you should close the previous one |
|
|
Oh, I see what you meant... yeah that would have worked 🤣 |
| /** Called after program initialization, before WASM compilation. Transformers should use | ||
| * this hook to perform custom validation of AST constructs such as parameter decorators | ||
| * and emit their own diagnostics. Decorators remain in the AST unchanged. */ | ||
| beforeCompile?(program: Program): void | Promise<void>; | ||
|
|
There was a problem hiding this comment.
Transformers should use
This can be cited as an example, not as a requirement. Also, the comment is rather wordy.
| /** Called after program initialization, before WASM compilation. Transformers should use | ||
| * this hook to perform custom validation of AST constructs such as parameter decorators | ||
| * and emit their own diagnostics. Decorators remain in the AST unchanged. */ | ||
| beforeCompile?(program: Program): void | Promise<void>; |
There was a problem hiding this comment.
Also, I haven't seen any indication that it's been integrated into the pipeline and well tested. For instance afterParse:
https://github.com/search?q=repo%3AAssemblyScript%2Fassemblyscript+afterParse&type=code
for beforeCompile need to do the same
| /** Called after program initialization, before WASM compilation. Transformers should use | ||
| * this hook to perform custom validation of AST constructs such as parameter decorators | ||
| * and emit their own diagnostics. Decorators remain in the AST unchanged. */ | ||
| beforeCompile?(program: Program): void | Promise<void>; |
There was a problem hiding this comment.
It's even less clear why we need beforeCompile when we have afterInitialize?
validate?(program: Program): Promise<bool> | bool;
Specialized validate would be appropriate more, as I suggested. Because it could optionally interrupt the compilation flow without trigger exception (soft bail)
There was a problem hiding this comment.
It's even less clear why we need
afterInitializewhen we haveafterInitialize?
I'm gonna assume you meant:
It's even less clear why we need
beforeCompilewhen we haveafterInitialize?
I think a preliminary discussion on this might be a good idea before I just jump and and start adding stuff 😅. Maybe a change to the transformer API should be delegated to it's own PR? What do you think? That would allow this PR to focus solely on adding decorators to the AST.
There was a problem hiding this comment.
I'm gonna assume you meant
Yeah, copy-paste typo
Maybe a change to the transformer API should be delegated to it's own PR? What do you think?
If this is not a blocker then that’s actually how it should be.
Made a new PR instead of force pushing (see original #3015)
Claude (with my own modifications):
Adds support for decorator syntax on function parameters (e.g.
@decorator value: i32). Decorators are parsed and stored inParameterNode.decoratorsandFunctionTypeNode.explicitThisDecorators, making them available to transformer plugins without affecting compilation.@decorator variableName: i32)decoratorsfield on ParameterNode,explicitThisDecoratorson FunctionTypeNode,decoratedFunctionTypesindex on SourcebeforeCompile(program)hook in cli/index.d.ts for transformer-owned validation after program init, before WASM compilationMe:
The following syntax is now parsed and included in the AST:
I also added a
beforeCompilehook to the transformer API so transformers can validate decorators if they want to (as per @MaxGraey suggestion)