feat: parse parameter decorators for transforms#3001
feat: parse parameter decorators for transforms#3001jtenner wants to merge 8 commits intoAssemblyScript:mainfrom
Conversation
|
Whoops! I forgot to check the validation scripts locally. Everything looks good now. I hope this contribution finds it's way to main because I want to use it to make some very cheeky transforms for my webgpu implementation. |
Extend parameter AST nodes to retain decorators, including explicit-this decorators on function signatures, without forcing a broad constructor API churn. Teach both parameter parsers to accept stacked decorators on regular, rest, explicit-this, constructor-property, function-type, and parenthesized-arrow parameters. Update the AST builder to serialize parameter decorators inline so parser fixtures can round-trip the new syntax faithfully. Add a focused parser fixture covering the preserved syntax surface before deferred validation is introduced.
Add a Program-owned validation pass that walks the final AST and reports TS1206 once per decorated parameter, using the full decorator-list span for the diagnostic range. Invoke that validation from compile after transforms have had their afterInitialize window, so transformers can still remove parameter decorators before any diagnostics are emitted. Add a dedicated compiler rejection fixture covering regular, rest, explicit-this, constructor-property, function-type, function-expression, and arrow-parameter cases.
Add a dedicated transform input containing the same invalid parameter-decorator forms exercised by the compiler rejection fixture. Introduce ESM and CommonJS afterInitialize transforms that walk the AST and strip parameter decorators, including explicit-this decorators on function signatures. Extend the transform test scripts to compile that input with the stripping transforms, proving no TS1206 diagnostics are emitted once transforms remove the decorators in time.
4ec3371 to
4786023
Compare
|
Is this all set? I rebased it on main and had to add the eslint "ignore" file because of the decorator proof. I left a comment to explain why. Please let me know if anything isn't up to standard! |
src/extra/ast.ts
Outdated
| } | ||
| } | ||
|
|
||
| private serializeParameterDecorator(node: DecoratorNode): void { |
There was a problem hiding this comment.
Why serializeParameterDecorator instead visitParameterDecorator?
There was a problem hiding this comment.
I missed this. Sorry.
|
|
||
| type Callback = (@arg value: i32) => void; | ||
| const expression = function(@arg value: i32): void {}; | ||
| const arrow = (@arg value: i32): void => {}; |
There was a problem hiding this comment.
You also need to add sample test cases and handle errors properly. Someting like:
function regularEmpty(@first): void {}
function firstEmpty(@first, value: i32): void {}
function regularWrongPos(value @first: i32): void {}
function regularWrongPos2(value: i32 @first): void {}
function restWrongPos(...values @rest: i32[]): void {}
function selfWrongPos(this: i32 @self): void {}
function noLits(@123 value: i32): void {}
function noExprs(@(a + b) value: i32): void {}There was a problem hiding this comment.
I added another test file. Should be good to go.
|
👀 are you planning on using this for WGSL/GLSL translation? |
Summary
This PR makes parameter decorators parseable and preservable in the AST so transforms can inspect or remove them before compilation-time validation runs.
This is intentionally not a new end-user language feature. Any parameter decorators that survive transform time still produce
TS1206: Decorators are not valid here.What Changed
ParameterNodethisparameter decorators onFunctionTypeNodeProgramvalidation pass that rejects surviving parameter decorators once per decorated parameter using the full decorator-list rangeafterInitialize, so transforms can remove the decorators firstWhy
The parser and transform pipeline can use preserved parameter decorators as an AST-level extension point without committing AssemblyScript to supporting them as regular language syntax.
That gives transform authors a useful hook while keeping the language semantics unchanged:
Impact
TS1206if parameter decorators survive to compilationValidation
npm run buildnpm run test:parser -- parameter-decoratorsnpm run test:compiler -- parameter-decorators --noDiffnpm run test:transform