Adds tests for rule documentation by
1. Compiling doc markdown into HTML, which ensures docs use valid markdown syntax
2. Converts the generated HTML into JSX and parses the results with the parser, ensuring the generated HTML is valid
Has the added benefit of adding a lot of JSX test cases to the parser. I've also fixed all violations for these tests in this PR.
I got tired of seeing useless spreads on ternaries and `arr.reduce()` within my company's internal codebase so I overhauled this rule.
## Changes
- add fixer for object spreads
```js
const before = { a, ...{ b, c }, d }
const after = { a, b, c, d } // fixer does not dedupe spaces before `b`
```
- recursively check for useless clones on complex expressions. This rule now catches and auto-fixes the following cases:
```js
// ternaries when both branches create a new array or object
const obj = { ...(foo ? { a: 1 } : { b: 2 }) }
// recursive, so this can support complex cases
const arr = [ ...(foo ? a.map(fn) : bar ? Array.from(iter) : await Promise.all(bar)) ]
// reduce functions where the initial accumulator creates a new object or array
const obj = { ...(arr.reduce(fn, {}) }
```
> AI-generated description because I'm lazy
### TL;DR
This PR introduces the ability to generate documentation for linter rules and adds new methods and metadata for rule fix capabilities.
To see what this looks like, please check out https://github.com/oxc-project/oxc-project.github.io/pull/165.
## Screenshots
Hyperlinks to rule doc pages in auto-generated rules table
<img width="809" alt="image" src="https://github.com/user-attachments/assets/e09eb47d-e86a-4ed1-b1f9-5034f33c71a2">
Example of a docs page
<img width="1273" alt="image" src="https://github.com/user-attachments/assets/78f7e9e6-f4dd-4cc9-aebc-1cdd64b024ec">
### What changed?
- Added `RuleFixMeta` to indicate rule fix capabilities
- Introduced methods `is_none` and `is_pending` in `RuleFixMeta`
- Modified `render_markdown_table` in `RuleTableSection` to accept an optional link prefix
- Created new modules for rule documentation and HTML rendering
- Updated `print_rules` function to generate markdown for rules and detailed documentation pages
### How to test?
Run the `linter-rules` task with appropriate arguments to generate the markdown table and documentation pages.
Verify the generated files for correctness and that all metadata is correctly displayed.
### Why make this change?
To enhance the project documentation and provide clear rule fix capabilities, thereby improving the developer experience and easing the integration process.
---
Some struct fields are reserved names e.g. `type`. They are written in source as `r#type`. Trim off the `r#` prefix in `FieldDef::name` and add it back when generating output.
Main motivation is to have the unescaped field names in JSON schema.
I noticed that in JSON schema the `docs` property contains e.g. `" The name of the identifier being referenced."` (with an excess space on the start). Trim that off.
Avoid the `insert!` macro in AST codegen. Use doc comments starting with special symbol `@` instead.
* Before: `insert!("// plain comment");`
* After: `///@ plain comment`
* Or: `//!@ plain comment`
Either `///@` or `//!@` is converted to plain `//` in output.
`//!@` is legal in top-of-file position, which allows us to inline `#![allow(...)]` attributes, which in my opinion makes the generators a bit easier to read.
fix: #4651
This commit enhances the `prefer_namespace_keyword` rule in the
TypeScript linter: Add support for detecting and fixing nested module
declarations (e.g., module A.B {})
- Introduce helper functions `is_nest_module`, `is_valid_module`, and
`is_invalid_module` to improve code readability and maintainability
- Refactor the main `run` function to use these new helper functions
- Update test cases to cover nested module scenarios
- Improve error reporting for nested modules
#4773 makes types like `Span` importable from `oxc_ast::ast`, so remove the imports from other crates in generated code.
I'm not sure why clippy's `wildcard_imports` rule was not being triggered for `use crate::ast::*;`, but add `#[allow(clippy::wildcard_imports)]` on these statements just to make sure.
Some AST types are defined in crates outside `oxc_ast` crate. Re-export these types from `oxc_ast` crate for ease - so you don't need to remember where they're defined to use them.
It checks 2 things. 1) The input is a supported derive 2) The given identifier is the same as the fully qualified target trait.
The latter makes sure that the trait for derive is included in the scope.
Part of #4704
Here's an expanded example of how we assert traits:
```rust
const _:() = {
{
trait AssertionTrait: ::oxc_allocator::CloneIn<'static> {}
impl<T: CloneIn<'static>> AssertionTrait for T {}
};
};
```
It makes sure `CloneIn` is the same as `::oxc_allocator::CloneIn` and more importantly requires the user to include the trait if they wish to use it with `generate_derive`.
It also provides LSP jump to definition.
2 files in AST codegen had Windows-style CRLF line breaks. Change them to Unix-style LF. Does not alter contents of the files aside from the line breaks.
follow-up to #4735
I was accidentally using the old code to filter the viable target types to derive. It means before this PR we were still using the `#[ast(visit)]` for this purpose.
Remove `Generator::name` and `Pass::name` methods. All impls for these methods return a string identical to the struct name, so can set return value of `Runner::name` in `define_generator!` and `define_pass!` macros instead.
Simplify `derive_get_span` generator that was introduced in #4735. No change to functionality, just aiming for greater readability.
In particular:
* Move defining idents/tokens which are specific to `GetSpan` / `GetSpanMut` into those specific generators, rather than branching on `MUT` later on.
* Remove `const MUT` param.
* Remove the confusing pairs of closures and functions both called `derive_enum` / `derive_struct`.
* Inline function which generates the impls - prioritizing readability over DRY code.