Skip to content

Commit 5e3b415

Browse files
committed
refactor(linter): duplicate RawTransferMetadata in oxc_linter crate (#12382)
`RawTransferMetadata` is used to store basic info about the data in the buffer which is used on JS side to locate `Program` in the buffer etc. This struct lives in `napi/parser` crate but we're going to need it in `napi/oxlint2` too. So make a 2nd copy of it in `napi/oxlint2` and use `oxc_ast_tools` to enforce that the 2 copies are identical. This is a bit of a hack, but it doesn't seem worthwhile creating a new crate just to hold this one struct, and it doesn't really fit in `oxc_allocator`, or any of our other existing crates.
1 parent b0db2d7 commit 5e3b415

File tree

12 files changed

+366
-268
lines changed

12 files changed

+366
-268
lines changed

.github/generated/ast_changes_watch_list.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ src:
3838
- 'crates/oxc_formatter/src/generated/ast_nodes.rs'
3939
- 'crates/oxc_formatter/src/generated/format.rs'
4040
- 'crates/oxc_formatter/src/generated/format_write.rs'
41+
- 'crates/oxc_linter/src/generated/assert_layouts.rs'
42+
- 'crates/oxc_linter/src/lib.rs'
4143
- 'crates/oxc_regular_expression/src/ast.rs'
4244
- 'crates/oxc_regular_expression/src/generated/assert_layouts.rs'
4345
- 'crates/oxc_regular_expression/src/generated/derive_clone_in.rs'

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/oxc_ast_macros/src/generated/structs.rs

Lines changed: 250 additions & 249 deletions
Large diffs are not rendered by default.

crates/oxc_linter/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ description.workspace = true
1717
default = []
1818
ruledocs = ["oxc_macros/ruledocs"] # Enables the `ruledocs` feature for conditional compilation
1919
language_server = ["oxc_data_structures/rope"] # For the Runtime to support needed information for the language server
20-
oxlint2 = ["tokio/rt-multi-thread"]
20+
oxlint2 = ["dep:oxc_ast_macros", "tokio/rt-multi-thread"]
2121
disable_oxlint2 = []
2222
force_test_reporter = []
2323

@@ -30,6 +30,7 @@ doctest = true
3030
[dependencies]
3131
oxc_allocator = { workspace = true }
3232
oxc_ast = { workspace = true }
33+
oxc_ast_macros = { workspace = true, optional = true }
3334
oxc_ast_visit = { workspace = true }
3435
oxc_cfg = { workspace = true }
3536
oxc_codegen = { workspace = true }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Auto-generated code, DO NOT EDIT DIRECTLY!
2+
// To edit this generated file you have to edit `tasks/ast_tools/src/generators/assert_layouts.rs`.
3+
4+
#![allow(unused_imports)]
5+
6+
use std::mem::{align_of, offset_of, size_of};
7+
8+
use crate::*;
9+
10+
#[cfg(target_pointer_width = "64")]
11+
const _: () = {
12+
// Padding: 3 bytes
13+
assert!(size_of::<RawTransferMetadata2>() == 16);
14+
assert!(align_of::<RawTransferMetadata2>() == 8);
15+
assert!(offset_of!(RawTransferMetadata2, data_offset) == 8);
16+
assert!(offset_of!(RawTransferMetadata2, is_ts) == 12);
17+
assert!(offset_of!(RawTransferMetadata2, _padding) == 0);
18+
};
19+
20+
#[cfg(target_pointer_width = "32")]
21+
const _: () = {
22+
// Padding: 3 bytes
23+
assert!(size_of::<RawTransferMetadata2>() == 16);
24+
assert!(align_of::<RawTransferMetadata2>() == 8);
25+
assert!(offset_of!(RawTransferMetadata2, data_offset) == 8);
26+
assert!(offset_of!(RawTransferMetadata2, is_ts) == 12);
27+
assert!(offset_of!(RawTransferMetadata2, _padding) == 0);
28+
};
29+
30+
#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))]
31+
const _: () = panic!("Platforms with pointer width other than 64 or 32 bit are not supported");

crates/oxc_linter/src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use std::{path::Path, rc::Rc, sync::Arc};
66
use oxc_allocator::Allocator;
77
use oxc_semantic::{AstNode, Semantic};
88

9+
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
10+
use oxc_ast_macros::ast;
11+
912
#[cfg(test)]
1013
mod tester;
1114

@@ -29,6 +32,12 @@ pub mod loader;
2932
pub mod rules;
3033
pub mod table;
3134

35+
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
36+
mod generated {
37+
#[cfg(debug_assertions)]
38+
pub mod assert_layouts;
39+
}
40+
3241
pub use crate::{
3342
config::{
3443
BuiltinLintPlugins, Config, ConfigBuilderError, ConfigStore, ConfigStoreBuilder,
@@ -273,6 +282,33 @@ impl Linter {
273282
}
274283
}
275284

285+
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
286+
/// Metadata written to end of buffer.
287+
///
288+
/// Duplicate of `RawTransferMetadata` in `napi/parser/src/raw_transfer_types.rs`.
289+
/// Any changes made here also need to be made there.
290+
/// `oxc_ast_tools` checks that the 2 copies are identical.
291+
#[ast]
292+
struct RawTransferMetadata2 {
293+
/// Offset of `RawTransferData` within buffer.
294+
pub data_offset: u32,
295+
/// `true` if AST is TypeScript.
296+
pub is_ts: bool,
297+
/// Padding to pad struct to size 16.
298+
pub(crate) _padding: u64,
299+
}
300+
301+
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
302+
use RawTransferMetadata2 as RawTransferMetadata;
303+
304+
#[cfg(all(feature = "oxlint2", not(feature = "disable_oxlint2")))]
305+
#[expect(dead_code)]
306+
impl RawTransferMetadata {
307+
pub fn new(data_offset: u32, is_ts: bool) -> Self {
308+
Self { data_offset, is_ts, _padding: 0 }
309+
}
310+
}
311+
276312
#[cfg(test)]
277313
mod test {
278314
use super::Oxlintrc;

napi/parser/generated/deserialize/js.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5238,16 +5238,16 @@ function deserializeBoxTSExternalModuleReference(pos) {
52385238
return deserializeTSExternalModuleReference(uint32[pos >> 2]);
52395239
}
52405240

5241-
function deserializeOptionNameSpan(pos) {
5242-
if (uint32[(pos + 8) >> 2] === 0 && uint32[(pos + 12) >> 2] === 0) return null;
5243-
return deserializeNameSpan(pos);
5244-
}
5245-
52465241
function deserializeU64(pos) {
52475242
const pos32 = pos >> 2;
52485243
return uint32[pos32] + uint32[pos32 + 1] * 4294967296;
52495244
}
52505245

5246+
function deserializeOptionNameSpan(pos) {
5247+
if (uint32[(pos + 8) >> 2] === 0 && uint32[(pos + 12) >> 2] === 0) return null;
5248+
return deserializeNameSpan(pos);
5249+
}
5250+
52515251
function deserializeOptionU64(pos) {
52525252
if (uint8[pos] === 0) return null;
52535253
return deserializeU64(pos + 8);

napi/parser/generated/deserialize/ts.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5369,16 +5369,16 @@ function deserializeBoxTSExternalModuleReference(pos) {
53695369
return deserializeTSExternalModuleReference(uint32[pos >> 2]);
53705370
}
53715371

5372-
function deserializeOptionNameSpan(pos) {
5373-
if (uint32[(pos + 8) >> 2] === 0 && uint32[(pos + 12) >> 2] === 0) return null;
5374-
return deserializeNameSpan(pos);
5375-
}
5376-
53775372
function deserializeU64(pos) {
53785373
const pos32 = pos >> 2;
53795374
return uint32[pos32] + uint32[pos32 + 1] * 4294967296;
53805375
}
53815376

5377+
function deserializeOptionNameSpan(pos) {
5378+
if (uint32[(pos + 8) >> 2] === 0 && uint32[(pos + 12) >> 2] === 0) return null;
5379+
return deserializeNameSpan(pos);
5380+
}
5381+
53825382
function deserializeOptionU64(pos) {
53835383
if (uint8[pos] === 0) return null;
53845384
return deserializeU64(pos + 8);

napi/parser/generated/lazy/constructors.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13752,17 +13752,17 @@ function constructBoxTSExternalModuleReference(pos, ast) {
1375213752
return new TSExternalModuleReference(ast.buffer.uint32[pos >> 2], ast);
1375313753
}
1375413754

13755-
function constructOptionNameSpan(pos, ast) {
13756-
if (ast.buffer.uint32[(pos + 8) >> 2] === 0 && ast.buffer.uint32[(pos + 12) >> 2] === 0) return null;
13757-
return new NameSpan(pos, ast);
13758-
}
13759-
1376013755
function constructU64(pos, ast) {
1376113756
const { uint32 } = ast.buffer,
1376213757
pos32 = pos >> 2;
1376313758
return uint32[pos32] + uint32[pos32 + 1] * 4294967296;
1376413759
}
1376513760

13761+
function constructOptionNameSpan(pos, ast) {
13762+
if (ast.buffer.uint32[(pos + 8) >> 2] === 0 && ast.buffer.uint32[(pos + 12) >> 2] === 0) return null;
13763+
return new NameSpan(pos, ast);
13764+
}
13765+
1376613766
function constructOptionU64(pos, ast) {
1376713767
if (ast.buffer[pos] === 0) return null;
1376813768
return constructU64(pos + 8, ast);

napi/parser/src/raw_transfer_types.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ pub struct RawTransferData<'a> {
2626
}
2727

2828
/// Metadata written to end of buffer.
29+
///
30+
/// Duplicated as `RawTransferMetadata2` in `crates/oxc_linter/src/lib.rs`.
31+
/// Any changes made here also need to be made there.
32+
/// `oxc_ast_tools` checks that the 2 copies are identical.
2933
#[ast]
3034
pub struct RawTransferMetadata {
3135
/// Offset of `RawTransferData` within buffer.

0 commit comments

Comments
 (0)