Skip to content

Comments

docs(ast, ast_macros, ast_tools): better documentation for Ast helper attributes.#4856

Merged
graphite-app[bot] merged 1 commit intomainfrom
08-13-docs_ast_macros_better_documentation_for_ast_helper_attributes
Aug 15, 2024
Merged

docs(ast, ast_macros, ast_tools): better documentation for Ast helper attributes.#4856
graphite-app[bot] merged 1 commit intomainfrom
08-13-docs_ast_macros_better_documentation_for_ast_helper_attributes

Conversation

@rzvxa
Copy link
Contributor

@rzvxa rzvxa commented Aug 12, 2024

No description provided.

Copy link
Contributor Author

rzvxa commented Aug 12, 2024

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @rzvxa and the rest of your teammates on Graphite Graphite

@rzvxa rzvxa changed the title docs(ast_macros): better documentation for Ast helper attributes. docs(ast, ast_macros, ast_tools): better documentation for Ast helper attributes. Aug 12, 2024
@github-actions github-actions bot added the A-ast Area - AST label Aug 12, 2024
@rzvxa rzvxa marked this pull request as ready for review August 12, 2024 21:49
@rzvxa rzvxa requested a review from overlookmotel August 12, 2024 21:49
@graphite-app
Copy link
Contributor

graphite-app bot commented Aug 12, 2024

Your org has enabled the Graphite merge queue for merging into main

Add the label “merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix.

You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 12, 2024

CodSpeed Performance Report

Merging #4856 will not alter performance

Comparing 08-13-docs_ast_macros_better_documentation_for_ast_helper_attributes (47c9552) with main (90d0b2b)

Summary

✅ 29 untouched benchmarks

@rzvxa rzvxa added the A-ast-tools Area - AST tools label Aug 12, 2024
Copy link
Member

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs added here to oxc_ast_macros/src/lib.rs are excellent.

Do you think we should retain some comment at top of oxc_ast/src/ast/js.rs etc pointing readers to the docs in oxc_ast_macros?

People may understand that #[scope] attr etc are probably related to the #[ast] attr, and so will look there for docs. But if you try "jump to definition" on generate_derive, it doesn't take you anywhere, and it's not obvious that generate_derive is related to #[ast].

@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 14, 2024

The docs added here to oxc_ast_macros/src/lib.rs are excellent.

Do you think we should retain some comment at top of oxc_ast/src/ast/js.rs etc pointing readers to the docs in oxc_ast_macros?

People may understand that #[scope] attr etc are probably related to the #[ast] attr, and so will look there for docs. But if you try "jump to definition" on generate_derive, it doesn't take you anywhere, and it's not obvious that generate_derive is related to #[ast].

Oh, I didn't notice outer markers aren't pointing to the macro. My initial reasoning behind removing those was to reduce the surface area of our documentation, It is much easier to maintain the docs in one place instead of having to keep multiple places in sync.

I'll bring back the old comments and look for a way so we can have jump to the definition on those attributes as well.

@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 14, 2024

@overlookmotel I've looked into having jump to definition and documentation on each attribute and found this to work:

image

It works similarly to Rust's built-in macros, If we can have a common crate to put some empty macros in it we should be able to document everything.

/// Hello darkness my old friend!
macro_rules! generate_derive {
    ($($trait:ident)*) => { /* `ast_tools` built-in */ }
}
pub(crate) use generate_derive;

Then in the proc-macro, I do something like this to get the compiler/RA to understand it.

                let (abs_derive, generics) = abs_trait(&derive);
                let generate_derive = // it doesn't have to be one per derive, We just need to do it once!
                    quote::quote_spanned!(attr_ident_span => generate_derive!(derive););
                quote! {{
                    // NOTE: these are wrapped in a scope to avoid the need for unique identifiers.
                    trait AssertionTrait: #abs_derive #generics {}
                    impl<T: #derive #generics> AssertionTrait for T {}
                    #generate_derive
                }}

Do you think it is worth implementing? We can place them in the oxc_ast/src/ast/macros but if we want to use it with other crates we have to rename our crates to this to follow the conventions:

oxc_ast_macros content goes into a fresh crate called oxc_ast_derive
use our current oxc_ast_macros to expose everything including the procedural derives/attributes.

@rzvxa rzvxa force-pushed the 08-13-docs_ast_macros_better_documentation_for_ast_helper_attributes branch from a35f20f to 7da3f8a Compare August 14, 2024 17:08
@rzvxa rzvxa requested a review from overlookmotel August 14, 2024 17:08
@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Aug 15, 2024
@graphite-app
Copy link
Contributor

graphite-app bot commented Aug 15, 2024

Merge activity

  • Aug 15, 7:31 AM EDT: The merge label 'merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Aug 15, 7:31 AM EDT: overlookmotel added this pull request to the Graphite merge queue.
  • Aug 15, 7:35 AM EDT: overlookmotel merged this pull request with the Graphite merge queue.

@overlookmotel overlookmotel force-pushed the 08-13-docs_ast_macros_better_documentation_for_ast_helper_attributes branch from 7da3f8a to 47c9552 Compare August 15, 2024 11:32
@graphite-app graphite-app bot merged commit 47c9552 into main Aug 15, 2024
@graphite-app graphite-app bot deleted the 08-13-docs_ast_macros_better_documentation_for_ast_helper_attributes branch August 15, 2024 11:35
@overlookmotel
Copy link
Member

Do you think it is worth implementing?

I'm not sure. What do you think? I would definitely say yes if we could do it without adding an extra crate, but if it requires an extra crate, maybe it's not worth the complexity.

I am not really au fait with how proc macro spans interface with Rust Analyser, but is there any other way to achieve this? Like e.g.:

  1. In proc macro, set the span of generated_derive to be span of #[ast], so at least jump to definition points you in the right direction.
  2. Do the macro_rules! generate_derive trick you suggested, but put that in oxc_ast/src/macros.rs.

Basically, I like this idea, but think introducing an extra crate just for it is maybe overkill. But if you want to go for it, do it!

@rzvxa
Copy link
Contributor Author

rzvxa commented Aug 15, 2024

I'm not sure. What do you think? I would definitely say yes if we could do it without adding an extra crate, but if it requires an extra crate, maybe it's not worth the complexity.

I agree on the fact that adding it might be a bit of overkill. Let's see if there comes any other situation that such a thing can help with.

1. In proc macro, set the span of `generated_derive` to be span of `#[ast]`, so at least jump to definition points you in the right direction.

This one isn't possible due to the compiler consuming the #[ast] tokens before executing the proc-macro on the stream. Evne if it didn't I'm not sure that RA would understand it correctly.

2. Do the `macro_rules! generate_derive` trick you suggested, but put that in `oxc_ast/src/macros.rs`.

That's exactly what I did in my test. It works great but can only be used within oxc_ast, Since oxc_ast depends on both oxc_syntax and oxc_span so it isn't possible to access these, They form a cyclic dependency graph.

I'll try to think of another way to make it work.
I've already tried generating the dummy macro along with assertions for derive traits however RA doesn't respect it as it doesn't exist before expansion.

@oxc-bot oxc-bot mentioned this pull request Aug 18, 2024
Boshen added a commit that referenced this pull request Aug 18, 2024
## [0.24.3] - 2024-08-18

### Features

- d49fb16 oxc_codegen: Support generate range leading comments (#4898)
(IWANABETHATGUY)
- 80d0d1f semantic: Check for invalid interface heritage clauses (#4928)
(DonIsaac)
- 48821c0 semantic,syntax: Add SymbolFlags::ArrowFunction (#4946)
(DonIsaac)
- f1fcdde transformer: Support react fast refresh (#4587) (Dunqing)
- 0d79122 transformer: Support logical-assignment-operators plugin
(#4890) (Dunqing)
- ab1d08c transformer: Support `optional-catch-binding` plugin (#4885)
(Dunqing)
- 69da9fd transformer: Support nullish-coalescing-operator plugin
(#4884) (Dunqing)
- 3a66e58 transformer: Support exponentiation operator plugin (#4876)
(Dunqing)
- f88cbcd transformer: Add `BoundIdentifier::new_uid_in_current_scope`
method (#4903) (overlookmotel)
- 1e6d0fe transformer: Add methods to `BoundIdentifier` (#4897)
(overlookmotel)
- fd34640 traverse: Support `generate_uid_based_on_node` method in
`TraverseCtx` (#4940) (Dunqing)
- 72a37fc traverse: Support `clone_identifier_reference` method in
`TraverseCtx` (#4880) (Dunqing)

### Bug Fixes

- c0b26f4 ast: Do not include `scope_id` fields in JSON AST (#4858)
(overlookmotel)
- bbf9ec0 codegen: Add missing `declare` to `PropertyDefinition` (#4937)
(Boshen)
- f210cf7 codegen: Print `TSSatisfiesExpression` and
`TSInstantiationExpression` (#4936) (Boshen)
- 21f5762 codegen: Minify large numbers (#4889) (Boshen)
- e8de4bd codegen: Fix whitespace issue when minifying `x in new
Error()` (#4886) (Boshen)
- a226962 codegen: Print `TSNonNullExpression` (#4869) (Boshen)
- 3da33d3 codegen: Missing parenthesis for `PrivateInExpression` (#4865)
(Boshen)
- 1808529 codegen: Dedupe pure annotation comments (#4862)
(IWANABETHATGUY)
- d3bbc62 isolated-declarations: Declare modifier of PropertyDefinition
should not be retained (#4941) (Dunqing)
- 8e80f59 isolated_declarations: Class properties should still be lifted
from private constructors (#4934) (michaelm)
- b3ec9e5 isolated_declarations: Always emit module declarations that
perform augmentation (#4919) (michaelm)
- 0fb0b71 isolated_declarations: Always emit module declarations (#4911)
(michaelm)
- 4a16916 isolated_declarations: Support expando functions (#4910)
(michaelm)
- 508644a linter/tree-shaking: Correct the calculation of `>>`, `<<` and
`>>>` (#4932) (mysteryven)
- 46cb1c1 minifier: Handle `Object.definedPropert(exports` for
@babel/types/lib/index.js (#4933) (Boshen)
- 81fd637 minifier: Do not fold `0 && (module.exports = {})` for
`cjs-module-lexer` (#4878) (Boshen)
- 879a271 minifier: Do not join `require` calls for `cjs-module-lexer`
(#4875) (Boshen)
- 1bdde2c parser: Detect @flow in `/** @flow */ comment (#4861) (Boshen)
- 2476dce transformer: Remove an `ast.copy` from
`NullishCoalescingOperator` transform (#4913) (overlookmotel)
- 248a757 transformer/typescript: Typescript syntax within
`SimpleAssignmentTarget` with `MemberExpressions` is not stripped
(#4920) (Dunqing)

### Documentation

- 47c9552 ast, ast_macros, ast_tools: Better documentation for `Ast`
helper attributes. (#4856) (rzvxa)
- 0a01a47 semantic: Improve documentation (#4850) (DonIsaac)
- 9c700ed transformer: Add README including style guide (#4899)
(overlookmotel)

### Refactor

- a6967b3 allocator: Correct code comment (#4904) (overlookmotel)
- 90d0b2b allocator, ast, span, ast_tools: Use `allocator` as var name
for `Allocator` (#4900) (overlookmotel)
- 1eb59d2 ast, isolated_declarations, transformer: Mark
`AstBuilder::copy` as an unsafe function (#4907) (overlookmotel)
- 8e8fcd0 ast_tools: Rename `oxc_ast_codegen` to `oxc_ast_tools`.
(#4846) (rzvxa)
- 786bf07 index: Shorten code and correct comment (#4905)
(overlookmotel)
- ea1e64a semantic: Make SemanticBuilder opaque (#4851) (DonIsaac)
- 5fd1701 sourcemap: Lower the `msrv`. (#4873) (rzvxa)
- 48a1c32 syntax: Inline trivial bitflags methods (#4877)
(overlookmotel)
- 452187a transformer: Rename `BoundIdentifier::new_uid_in_root_scope`
(#4902) (overlookmotel)
- 707a01f transformer: Re-order `BoundIdentifier` methods (#4896)
(overlookmotel)
- 117dff2 transformer: Improve comments for `BoundIdentifier` helper
(#4895) (overlookmotel)

Co-authored-by: Boshen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0-merge Merge with Graphite Merge Queue A-ast Area - AST A-ast-tools Area - AST tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants