Skip to content

Commit c9a2afc

Browse files
blueaveekdy1
andauthored
feat(bindings/core): Enhance existing parse function to accept both string and buffer types(#10371)
**Description:** - Enhanced existing `parse` and `parseSync` functions to accept string and Buffer inputs. - Updated TypeScript definitions and JavaScript bindings to reflect the new input types. - Improved tests to cover the new capabilities for parsing from both string and Buffer inputs. **Related issue:** - Closes #9876 --- Co-authored-by: Donny/강동윤 <[email protected]>
1 parent 34f4e41 commit c9a2afc

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

bindings/binding_core_node/src/parse.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
use anyhow::Context as _;
77
use napi::{
88
bindgen_prelude::{AbortSignal, AsyncTask, Buffer},
9-
Env, Task,
9+
Either, Env, Task,
1010
};
1111
use swc_core::{
1212
base::{
@@ -136,16 +136,24 @@ impl Task for ParseFileTask {
136136
}
137137
}
138138

139+
fn stringify(src: Either<Buffer, String>) -> String {
140+
match src {
141+
Either::A(src) => String::from_utf8_lossy(src.as_ref()).into_owned(),
142+
Either::B(src) => src,
143+
}
144+
}
145+
139146
#[napi]
140147
pub fn parse(
141-
src: String,
148+
src: Either<Buffer, String>,
142149
options: Buffer,
143150
filename: Option<String>,
144151
signal: Option<AbortSignal>,
145152
) -> AsyncTask<ParseTask> {
146153
crate::util::init_default_trace_subscriber();
147154

148155
let c = get_compiler();
156+
let src = stringify(src);
149157
let options = String::from_utf8_lossy(options.as_ref()).to_string();
150158
let filename = if let Some(value) = filename {
151159
FileName::Real(value.into())
@@ -165,10 +173,15 @@ pub fn parse(
165173
}
166174

167175
#[napi]
168-
pub fn parse_sync(src: String, opts: Buffer, filename: Option<String>) -> napi::Result<String> {
176+
pub fn parse_sync(
177+
src: Either<Buffer, String>,
178+
opts: Buffer,
179+
filename: Option<String>,
180+
) -> napi::Result<String> {
169181
crate::util::init_default_trace_subscriber();
170-
let c = get_compiler();
171182

183+
let c = get_compiler();
184+
let src = stringify(src);
172185
let options: ParseOptions = get_deserialized(&opts)?;
173186
let filename = if let Some(value) = filename {
174187
FileName::Real(value.into())

packages/core/__tests__/parse/api_test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,17 @@ it("can be emit code back synchronously", async () => {
2929

3030
expect(out.code.trim().replace("\n", "")).toBe(`class Foo {}`);
3131
});
32+
33+
it("can be emit code back asynchronously for buffer input", async () => {
34+
const m = await swc.parse(Buffer.from(`class Foo {}`));
35+
const out = await swc.print(m);
36+
37+
expect(out.code.trim().replace("\n", "")).toBe(`class Foo {}`);
38+
});
39+
40+
it("can be emit code back synchronously for buffer input", async () => {
41+
const m = swc.parseSync(Buffer.from(`class Foo {}`));
42+
const out = swc.printSync(m);
43+
44+
expect(out.code.trim().replace("\n", "")).toBe(`class Foo {}`);
45+
});

packages/core/binding.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ export interface NapiMinifyExtra {
2424

2525
export declare function newMangleNameCache(): object
2626

27-
export declare function parse(src: string, options: Buffer, filename?: string | undefined | null, signal?: AbortSignal | undefined | null): Promise<string>
27+
export declare function parse(src: Buffer | string, options: Buffer, filename?: string | undefined | null, signal?: AbortSignal | undefined | null): Promise<string>
2828

2929
export declare function parseFile(path: string, options: Buffer, signal?: AbortSignal | undefined | null): Promise<string>
3030

3131
export declare function parseFileSync(path: string, opts: Buffer): string
3232

33-
export declare function parseSync(src: string, opts: Buffer, filename?: string | undefined | null): string
33+
export declare function parseSync(src: Buffer | string, opts: Buffer, filename?: string | undefined | null): string
3434

3535
export declare function print(programJson: string, options: Buffer, signal?: AbortSignal | undefined | null): Promise<TransformOutput>
3636

0 commit comments

Comments
 (0)