Skip to content

Commit 5238891

Browse files
committed
fix(linter/plugins): add comments field to TS type def for Program (#14626)
#14589 added `comments` field to `Program` in Oxlint JS plugins AST. Add this field to the TS type def for `Program`.
1 parent 84b2605 commit 5238891

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

apps/oxlint/src-js/generated/types.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Auto-generated code, DO NOT EDIT DIRECTLY!
22
// To edit this generated file you have to edit `tasks/ast_tools/src/generators/typescript.rs`.
33

4-
import { Span } from '../plugins/types.ts';
5-
export { Span };
4+
import { Comment, Span } from '../plugins/types.ts';
5+
export { Comment, Span };
66

77
export interface Program extends Span {
88
type: 'Program';
99
body: Array<Directive | Statement>;
1010
sourceType: ModuleKind;
1111
hashbang: Hashbang | null;
12+
comments: Comment[];
1213
parent?: null;
1314
}
1415

apps/oxlint/src-js/plugins/source_code.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ export const SOURCE_CODE = Object.freeze({
161161
getAllComments(): Comment[] {
162162
if (ast === null) initAst();
163163
// TODO: Deserializing strings is expensive, make this access lazy
164-
// @ts-expect-error types are generated from `Program` attributes
165-
// which are also twinned with ESTree generation so can't touch it
166164
return ast.comments;
167165
},
168166

tasks/ast_tools/src/generators/typescript.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,9 @@ fn get_single_field<'s>(struct_def: &'s StructDef, schema: &Schema) -> Option<&'
453453
}
454454

455455
/// Amend version of types for Oxlint.
456-
///
457-
/// Remove `export interface Span`, and instead import local version of same interface,
458-
/// which includes non-optional `range` and `loc` fields.
459456
fn amend_oxlint_types(code: &str) -> String {
457+
// Remove `export interface Span`, and instead import local version of same interface,
458+
// which includes non-optional `range` and `loc` fields.
460459
static SPAN_REGEX: Lazy<Regex> = lazy_regex!(r"export interface Span \{.+?\}");
461460

462461
struct SpanReplacer;
@@ -468,10 +467,16 @@ fn amend_oxlint_types(code: &str) -> String {
468467

469468
let mut code = SPAN_REGEX.replace(code, SpanReplacer).into_owned();
470469

470+
// Add `comments` field to `Program`
471+
#[expect(clippy::items_after_statements)]
472+
const HASHBANG_FIELD: &str = "hashbang: Hashbang | null;";
473+
let index = code.find(HASHBANG_FIELD).unwrap();
474+
code.insert_str(index + HASHBANG_FIELD.len(), "comments: Comment[];");
475+
471476
#[rustfmt::skip]
472477
code.insert_str(0, "
473-
import { Span } from '../plugins/types.ts';
474-
export { Span };
478+
import { Span, Comment } from '../plugins/types.ts';
479+
export { Span, Comment };
475480
476481
");
477482

0 commit comments

Comments
 (0)