Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/oxlint/src-js/generated/types.d.ts
Original file line number Diff line number Diff line change
@@ -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<Directive | Statement>;
sourceType: ModuleKind;
hashbang: Hashbang | null;
comments: Comment[];
parent?: null;
}

Expand Down
2 changes: 0 additions & 2 deletions apps/oxlint/src-js/plugins/source_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},

Expand Down
15 changes: 10 additions & 5 deletions tasks/ast_tools/src/generators/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Regex> = lazy_regex!(r"export interface Span \{.+?\}");

struct SpanReplacer;
Expand All @@ -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 };

");

Expand Down
Loading