Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,15 +568,16 @@ impl<'a> ParserImpl<'a> {
meta: IdentifierName<'a>,
) -> Result<Expression<'a>> {
self.bump_any(); // bump `.`
let kind = self.cur_kind();
let property = match self.cur_kind() {
Kind::Meta => {
self.module_record_builder.visit_import_meta();
self.parse_keyword_identifier(Kind::Meta)
}
Kind::Meta => self.parse_keyword_identifier(Kind::Meta),
Kind::Target => self.parse_keyword_identifier(Kind::Target),
_ => self.parse_identifier_name()?,
};
let span = self.end_span(span);
if kind == Kind::Meta {
self.module_record_builder.visit_import_meta(span);
}
Ok(self.ast.expression_meta_property(span, meta, property))
}

Expand Down
12 changes: 11 additions & 1 deletion crates/oxc_parser/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ impl<'a> ModuleRecordBuilder<'a> {
}
}

pub fn visit_import_meta(&mut self) {
pub fn visit_import_meta(&mut self, span: Span) {
self.module_record.has_module_syntax = true;
self.module_record.import_metas.push(span);
}

pub fn visit_module_declaration(&mut self, module_decl: &ModuleDeclaration<'a>) {
Expand Down Expand Up @@ -691,4 +692,13 @@ mod module_record_tests {
}
);
}

#[test]
fn import_meta() {
let allocator = Allocator::default();
let module_record = build(&allocator, "import.meta.foo; import.meta.bar");
assert_eq!(module_record.import_metas.len(), 2);
assert_eq!(module_record.import_metas[0], Span::new(0, 11));
assert_eq!(module_record.import_metas[1], Span::new(17, 28));
}
}
4 changes: 4 additions & 0 deletions crates/oxc_syntax/src/module_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub struct ModuleRecord<'a> {

/// Local exported bindings
pub exported_bindings: FxHashMap<Atom<'a>, Span>,

/// Span position of `import.meta`.
pub import_metas: Vec<'a, Span>,
}

impl<'a> ModuleRecord<'a> {
Expand All @@ -69,6 +72,7 @@ impl<'a> ModuleRecord<'a> {
indirect_export_entries: Vec::new_in(allocator),
star_export_entries: Vec::new_in(allocator),
exported_bindings: FxHashMap::default(),
import_metas: Vec::new_in(allocator),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions napi/parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface EcmaScriptModule {
staticImports: Array<StaticImport>
/** Export Statements. */
staticExports: Array<StaticExport>
/** Span positions` of `import.meta` */
importMetas: Array<Span>
}

export interface ExportExportName {
Expand Down Expand Up @@ -133,6 +135,11 @@ export declare function parseSync(filename: string, sourceText: string, options?
*/
export declare function parseWithoutReturn(filename: string, sourceText: string, options?: ParserOptions | undefined | null): void

export interface Span {
start: number
end: number
}

export interface StaticExport {
start: number
end: number
Expand Down
24 changes: 17 additions & 7 deletions napi/parser/src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use rustc_hash::FxHashMap;

use oxc::{
span::Span,
syntax::module_record::{self, ModuleRecord},
};
use oxc::syntax::module_record::{self, ModuleRecord};

use crate::types::{
EcmaScriptModule, ExportExportName, ExportExportNameKind, ExportImportName,
ExportImportNameKind, ExportLocalName, ExportLocalNameKind, ImportName, ImportNameKind,
ExportImportNameKind, ExportLocalName, ExportLocalNameKind, ImportName, ImportNameKind, Span,
StaticExport, StaticExportEntry, StaticImport, StaticImportEntry, ValueSpan,
};

Expand Down Expand Up @@ -49,7 +46,7 @@ impl From<&ModuleRecord<'_>> for EcmaScriptModule {
.map(|e| (e.statement_span, StaticExportEntry::from(e)))
.collect::<Vec<_>>()
.into_iter()
.fold(FxHashMap::<Span, Vec<StaticExportEntry>>::default(), |mut acc, (span, e)| {
.fold(FxHashMap::<_, Vec<StaticExportEntry>>::default(), |mut acc, (span, e)| {
acc.entry(span).or_default().push(e);
acc
})
Expand All @@ -58,7 +55,20 @@ impl From<&ModuleRecord<'_>> for EcmaScriptModule {
.collect::<Vec<_>>();
static_exports.sort_unstable_by_key(|e| e.start);

Self { has_module_syntax: record.has_module_syntax, static_imports, static_exports }
let import_metas = record.import_metas.iter().map(Span::from).collect();

Self {
has_module_syntax: record.has_module_syntax,
static_imports,
static_exports,
import_metas,
}
}
}

impl From<&oxc::span::Span> for Span {
fn from(span: &oxc::span::Span) -> Self {
Self { start: span.start, end: span.end }
}
}

Expand Down
8 changes: 8 additions & 0 deletions napi/parser/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ pub struct EcmaScriptModule {
pub static_imports: Vec<StaticImport>,
/// Export Statements.
pub static_exports: Vec<StaticExport>,
/// Span positions` of `import.meta`
pub import_metas: Vec<Span>,
}

#[napi(object)]
pub struct Span {
pub start: u32,
pub end: u32,
}

#[napi(object)]
Expand Down
Loading