Skip to content

Commit 083fea9

Browse files
committed
feat(napi/parser)!: represent empty optional fields on JS side as null (#16411)
Follow-on after #16383 and #16403. While working on those I discovered an option NAPI-RS has to represent `Option::None` as `null`, instead of omitting the field entirely. Use that option on the structs used for transferring errors and module record over to JS. I think this is likely more performant because: 1. NAPI-RS can create all properties of the objects using only the faster `node_api_create_object_with_properties` API. 2. It produces consistent object shapes, so JS engine can better optimize code using these objects. It also brings the shape of the data perfectly into line between standard transfer and raw transfer, and is consistent with how empty fields in the AST are represented as `null`. I would have used this option before if I'd known it existed. ### Breaking change I've marked this as a breaking change because code consuming these objects would now need to check for empty fields with `value === null` instead of `value === undefined`. However in practice, most people probably use `!value` or `value ?? ...`, so it's unlikely to affect many users. This only affects module record and errors anyway, not the AST itself, as we transfer that as JSON, not via NAPI.
1 parent c6778f3 commit 083fea9

File tree

9 files changed

+274
-121
lines changed

9 files changed

+274
-121
lines changed

crates/oxc_napi/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use napi_derive::napi;
44

55
use oxc_diagnostics::{LabeledSpan, NamedSource, OxcDiagnostic};
66

7-
#[napi(object)]
7+
#[napi(object, use_nullable = true)]
88
pub struct OxcError {
99
pub severity: Severity,
1010
pub message: String,
@@ -64,7 +64,7 @@ impl From<&OxcDiagnostic> for OxcError {
6464
}
6565
}
6666

67-
#[napi(object)]
67+
#[napi(object, use_nullable = true)]
6868
pub struct ErrorLabel {
6969
pub message: Option<String>,
7070
pub start: u32,

napi/minify/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export interface Comment {
191191
}
192192

193193
export interface ErrorLabel {
194-
message?: string
194+
message: string | null
195195
start: number
196196
end: number
197197
}
@@ -200,8 +200,8 @@ export interface OxcError {
200200
severity: Severity
201201
message: string
202202
labels: Array<ErrorLabel>
203-
helpMessage?: string
204-
codeframe?: string
203+
helpMessage: string | null
204+
codeframe: string | null
205205
}
206206

207207
export declare const enum Severity {

napi/parser/src-js/index.d.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export interface Comment {
2222
}
2323

2424
export interface ErrorLabel {
25-
message?: string
25+
message: string | null
2626
start: number
2727
end: number
2828
}
@@ -31,8 +31,8 @@ export interface OxcError {
3131
severity: Severity
3232
message: string
3333
labels: Array<ErrorLabel>
34-
helpMessage?: string
35-
codeframe?: string
34+
helpMessage: string | null
35+
codeframe: string | null
3636
}
3737

3838
export declare const enum Severity {
@@ -74,9 +74,9 @@ export interface EcmaScriptModule {
7474

7575
export interface ExportExportName {
7676
kind: ExportExportNameKind
77-
name?: string
78-
start?: number
79-
end?: number
77+
name: string | null
78+
start: number | null
79+
end: number | null
8080
}
8181

8282
export declare const enum ExportExportNameKind {
@@ -90,9 +90,9 @@ export declare const enum ExportExportNameKind {
9090

9191
export interface ExportImportName {
9292
kind: ExportImportNameKind
93-
name?: string
94-
start?: number
95-
end?: number
93+
name: string | null
94+
start: number | null
95+
end: number | null
9696
}
9797

9898
export declare const enum ExportImportNameKind {
@@ -108,9 +108,9 @@ export declare const enum ExportImportNameKind {
108108

109109
export interface ExportLocalName {
110110
kind: ExportLocalNameKind
111-
name?: string
112-
start?: number
113-
end?: number
111+
name: string | null
112+
start: number | null
113+
end: number | null
114114
}
115115

116116
export declare const enum ExportLocalNameKind {
@@ -127,9 +127,9 @@ export declare const enum ExportLocalNameKind {
127127

128128
export interface ImportName {
129129
kind: ImportNameKind
130-
name?: string
131-
start?: number
132-
end?: number
130+
name: string | null
131+
start: number | null
132+
end: number | null
133133
}
134134

135135
export declare const enum ImportNameKind {
@@ -229,7 +229,7 @@ export interface StaticExportEntry {
229229
* ```
230230
*/
231231
isType: boolean
232-
moduleRequest?: ValueSpan
232+
moduleRequest: ValueSpan | null
233233
}
234234

235235
export interface StaticImport {

napi/parser/src/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ pub enum ImportNameKind {
170170
Default,
171171
}
172172

173-
#[napi(object)]
173+
#[napi(object, use_nullable = true)]
174174
pub struct ImportName {
175175
pub kind: ImportNameKind,
176176
pub name: Option<String>,
177177
pub start: Option<u32>,
178178
pub end: Option<u32>,
179179
}
180180

181-
#[napi(object)]
181+
#[napi(object, use_nullable = true)]
182182
pub struct StaticExportEntry {
183183
pub start: u32,
184184
pub end: u32,
@@ -222,7 +222,7 @@ pub enum ExportImportNameKind {
222222
None,
223223
}
224224

225-
#[napi(object)]
225+
#[napi(object, use_nullable = true)]
226226
pub struct ExportImportName {
227227
pub kind: ExportImportNameKind,
228228
pub name: Option<String>,
@@ -240,15 +240,15 @@ pub enum ExportExportNameKind {
240240
None,
241241
}
242242

243-
#[napi(object)]
243+
#[napi(object, use_nullable = true)]
244244
pub struct ExportExportName {
245245
pub kind: ExportExportNameKind,
246246
pub name: Option<String>,
247247
pub start: Option<u32>,
248248
pub end: Option<u32>,
249249
}
250250

251-
#[napi(object)]
251+
#[napi(object, use_nullable = true)]
252252
pub struct ExportLocalName {
253253
pub kind: ExportLocalNameKind,
254254
pub name: Option<String>,

0 commit comments

Comments
 (0)