import-attributes: find the assert keyword, so the rule fires #4

Merged
sionzee merged 1 commit from fix/import-attributes into main 2026-11-03 21:45:34 +01:00
Owner

What

The import-attributes rule never fired. It checked text(with_clause.span).starts_with("assert"),
but oxc's WithClause.span starts at the { (the parser advances past the keyword before
recording the start; see parse_import_attributes in oxc_parser 0.156), so the text always began
with {.

  • lints.rs: the keyword is now looked up between the end of the module specifier and the {,
    and the diagnostic and quick fix (Use with) cover exactly the assert keyword.
  • The same check now also runs on re-exports (export { x } from "./a.json" assert { … },
    export * from "./a.json" assert { … }), which hit the same problem.

Why the tests didn't catch it

Nothing tested the rule. No fixture or fix test used assert, and the rule page's examples were
plain ```ts blocks (not checked), because scripts/docs-typecheck.mjs compiles every
checked block with tsc and TypeScript rejects assert.

To close that gap:

  • A doc block can now carry a no-tsc word (```ts bad import-attributes no-tsc). docs.rs
    still checks it with safe-ts; docs-typecheck.mjs leaves it out of the tsc compile.
    docs/rules/import-attributes.md now uses checked examples (the import and the re-export as
    bad, with as good), and says what the rule can't see (dynamic
    import(…, { assert: … })).
  • tests/fixes.rs: import_assertions_become_import_attributes checks the rule fires on import,
    export … from and export * with assert, not on with, and checks the exact result of
    the fix.

Both new checks fail without the fix: the fix test fails its assert_eq, and the docs test
reports rules/import-attributes.md:10 expected {"import-attributes"}, got {}.

Tests

  • cargo nextest run: 95/96 pass. The one failure, fixtures_match_annotations on the
    features fixture (main.ts:55, unexpected unhandled-failure for createDecipheriv()),
    also fails on unmodified main (0e01fb3) and is unrelated.
  • npm run test:docs (docs typecheck: 213 examples compile, wasm build, VitePress build): passes.

Not covered

  • Dynamic import("./x.json", { assert: { type: "json" } }). The rule page says so.

🤖 Generated with Claude Code

## What The `import-attributes` rule never fired. It checked `text(with_clause.span).starts_with("assert")`, but oxc's `WithClause.span` starts at the `{` (the parser advances past the keyword before recording the start; see `parse_import_attributes` in oxc_parser 0.156), so the text always began with `{`. - `lints.rs`: the keyword is now looked up between the end of the module specifier and the `{`, and the diagnostic and quick fix (*Use `with`*) cover exactly the `assert` keyword. - The same check now also runs on re-exports (`export { x } from "./a.json" assert { … }`, `export * from "./a.json" assert { … }`), which hit the same problem. ## Why the tests didn't catch it Nothing tested the rule. No fixture or fix test used `assert`, and the rule page's examples were plain ` ```ts ` blocks (not checked), because `scripts/docs-typecheck.mjs` compiles every checked block with tsc and TypeScript rejects `assert`. To close that gap: - A doc block can now carry a `no-tsc` word (` ```ts bad import-attributes no-tsc `). `docs.rs` still checks it with safe-ts; `docs-typecheck.mjs` leaves it out of the tsc compile. `docs/rules/import-attributes.md` now uses checked examples (the import and the re-export as bad, `with` as good), and says what the rule can't see (dynamic `import(…, { assert: … })`). - `tests/fixes.rs`: `import_assertions_become_import_attributes` checks the rule fires on import, `export … from` and `export *` with `assert`, not on `with`, and checks the exact result of the fix. Both new checks fail without the fix: the fix test fails its `assert_eq`, and the docs test reports `rules/import-attributes.md:10 expected {"import-attributes"}, got {}`. ## Tests - `cargo nextest run`: 95/96 pass. The one failure, `fixtures_match_annotations` on the `features` fixture (`main.ts:55`, unexpected `unhandled-failure` for `createDecipheriv()`), also fails on unmodified `main` (0e01fb3) and is unrelated. - `npm run test:docs` (docs typecheck: 213 examples compile, wasm build, VitePress build): passes. ## Not covered - Dynamic `import("./x.json", { assert: { type: "json" } })`. The rule page says so. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oxc's `WithClause.span` starts at the `{`, after the keyword, so checking that
the clause's text starts with "assert" never matched and the rule was dead.
The keyword is now looked up between the module specifier and the brace, and
re-exports (`export … from "./x.json" assert { … }`) are checked too.

Nothing caught it because nothing tested it: no fixture or fix test used
`assert`, and the rule page's examples were plain blocks, since tsc rejects
the syntax. Doc blocks can now say `no-tsc` to stay checked by safe-ts but out
of the tsc compile; the page uses that, and a fix test covers the quick fix.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch fix/import-attributes 2026-11-03 21:45:34 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!4
No description provided.