diff --git a/apps/oxlint/src-js/generated/types.d.ts b/apps/oxlint/src-js/generated/types.d.ts index 595edae84d6af..e9a1c3a817543 100644 --- a/apps/oxlint/src-js/generated/types.d.ts +++ b/apps/oxlint/src-js/generated/types.d.ts @@ -1,14 +1,15 @@ // Auto-generated code, DO NOT EDIT DIRECTLY! // To edit this generated file you have to edit `tasks/ast_tools/src/generators/typescript.rs`. -import { Span } from '../plugins/types.ts'; -export { Span }; +import { Comment, Span } from '../plugins/types.ts'; +export { Comment, Span }; export interface Program extends Span { type: 'Program'; body: Array; sourceType: ModuleKind; hashbang: Hashbang | null; + comments: Comment[]; parent?: null; } diff --git a/apps/oxlint/src-js/plugins/source_code.ts b/apps/oxlint/src-js/plugins/source_code.ts index d306e0a44a6e2..3e27e50725bba 100644 --- a/apps/oxlint/src-js/plugins/source_code.ts +++ b/apps/oxlint/src-js/plugins/source_code.ts @@ -161,8 +161,6 @@ export const SOURCE_CODE = Object.freeze({ getAllComments(): Comment[] { if (ast === null) initAst(); // TODO: Deserializing strings is expensive, make this access lazy - // @ts-expect-error types are generated from `Program` attributes - // which are also twinned with ESTree generation so can't touch it return ast.comments; }, diff --git a/tasks/ast_tools/src/generators/typescript.rs b/tasks/ast_tools/src/generators/typescript.rs index 78bb47e0997a0..f1b94b3473ac6 100644 --- a/tasks/ast_tools/src/generators/typescript.rs +++ b/tasks/ast_tools/src/generators/typescript.rs @@ -453,10 +453,9 @@ fn get_single_field<'s>(struct_def: &'s StructDef, schema: &Schema) -> Option<&' } /// Amend version of types for Oxlint. -/// -/// Remove `export interface Span`, and instead import local version of same interface, -/// which includes non-optional `range` and `loc` fields. fn amend_oxlint_types(code: &str) -> String { + // Remove `export interface Span`, and instead import local version of same interface, + // which includes non-optional `range` and `loc` fields. static SPAN_REGEX: Lazy = lazy_regex!(r"export interface Span \{.+?\}"); struct SpanReplacer; @@ -468,10 +467,16 @@ fn amend_oxlint_types(code: &str) -> String { let mut code = SPAN_REGEX.replace(code, SpanReplacer).into_owned(); + // Add `comments` field to `Program` + #[expect(clippy::items_after_statements)] + const HASHBANG_FIELD: &str = "hashbang: Hashbang | null;"; + let index = code.find(HASHBANG_FIELD).unwrap(); + code.insert_str(index + HASHBANG_FIELD.len(), "comments: Comment[];"); + #[rustfmt::skip] code.insert_str(0, " - import { Span } from '../plugins/types.ts'; - export { Span }; + import { Span, Comment } from '../plugins/types.ts'; + export { Span, Comment }; ");