Skip to content

Commit

Permalink
feat(napi/parser): add get_line_column_number API (#7765)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 10, 2024
1 parent 016ae92 commit f3cfa7f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions napi/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ doctest = false
[dependencies]
oxc = { workspace = true }
oxc_ast = { workspace = true, features = ["serialize"] } # enable feature only
oxc_data_structures = { workspace = true }
oxc_napi = { workspace = true }

rustc-hash = { workspace = true }
Expand Down
8 changes: 8 additions & 0 deletions napi/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

export * from '@oxc-project/types';
export declare class MagicString {
/** Get source text from utf8 offset. */
getSourceText(start: number, end: number): string
/** Get 0-based line and column number from utf8 offset. */
getLineColumnNumber(offset: number): LineColumn
length(): number
toString(): string
append(input: string): this
Expand Down Expand Up @@ -131,6 +134,11 @@ export declare const enum ImportNameKind {
Default = 'Default'
}

export interface LineColumn {
line: number
column: number
}

export interface OverwriteOptions {
contentOnly: boolean
}
Expand Down
23 changes: 22 additions & 1 deletion napi/parser/src/magic_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// use std::sync::Arc;

use napi_derive::napi;
use oxc_data_structures::rope::{get_line_column, Rope};

// use oxc_sourcemap::napi::SourceMap;
use self_cell::self_cell;
Expand All @@ -10,6 +11,7 @@ use string_wizard::MagicString as MS;
#[napi]
pub struct MagicString {
cell: MagicStringImpl,
rope: Option<Rope>,
}

self_cell!(
Expand All @@ -22,10 +24,19 @@ self_cell!(

impl MagicString {
pub fn new(source_text: String) -> Self {
Self { cell: MagicStringImpl::new(source_text, |s| string_wizard::MagicString::new(s)) }
Self {
cell: MagicStringImpl::new(source_text, |s| string_wizard::MagicString::new(s)),
rope: None,
}
}
}

#[napi(object)]
pub struct LineColumn {
pub line: u32,
pub column: u32,
}

#[napi(object)]
pub struct OverwriteOptions {
pub content_only: bool,
Expand All @@ -40,11 +51,21 @@ pub struct SourceMapOptions {

#[napi]
impl MagicString {
/// Get source text from utf8 offset.
#[napi]
pub fn get_source_text(&self, start: u32, end: u32) -> &str {
&self.cell.borrow_owner()[start as usize..end as usize]
}

/// Get 0-based line and column number from utf8 offset.
#[napi]
pub fn get_line_column_number(&mut self, offset: u32) -> LineColumn {
let source_text = self.cell.borrow_owner();
let rope = self.rope.get_or_insert_with(|| Rope::from_str(source_text));
let (line, column) = get_line_column(rope, offset, source_text);
LineColumn { line, column }
}

#[napi]
pub fn length(&self) -> u32 {
self.cell.borrow_dependent().len() as u32
Expand Down
6 changes: 6 additions & 0 deletions napi/parser/test/magic_string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ describe('simple', () => {
// Access source text by utf8 offset.
expect(ms.getSourceText(start, end)).toEqual('测试');

// Access line and column number from utf8 offset.
expect(ms.getLineColumnNumber(start)).toStrictEqual({
line: 0,
column: 19,
});

// Magic string manipulation.
ms.remove(start, end).append(';');
expect(ms.toString()).toEqual('const s: String = "";');
Expand Down

0 comments on commit f3cfa7f

Please sign in to comment.