diff --git a/crates/oxc_parser/src/js/list.rs b/crates/oxc_parser/src/js/list.rs index 1e9d0ab0c..243735111 100644 --- a/crates/oxc_parser/src/js/list.rs +++ b/crates/oxc_parser/src/js/list.rs @@ -478,3 +478,28 @@ impl<'a> NormalList<'a> for SwitchCases<'a> { Ok(()) } } + +pub struct ImportSpecifierList<'a> { + pub import_specifiers: Vec<'a, ImportDeclarationSpecifier>, +} + +impl<'a> SeparatedList<'a> for ImportSpecifierList<'a> { + fn new(p: &Parser<'a>) -> Self { + Self { import_specifiers: p.ast.new_vec() } + } + + fn open(&self) -> Kind { + Kind::LCurly + } + + fn close(&self) -> Kind { + Kind::RCurly + } + + fn parse_element(&mut self, p: &mut Parser<'a>) -> Result<()> { + let import_specifier = p.parse_import_specifier()?; + let specifier = ImportDeclarationSpecifier::ImportSpecifier(import_specifier); + self.import_specifiers.push(specifier); + Ok(()) + } +} diff --git a/crates/oxc_parser/src/js/module.rs b/crates/oxc_parser/src/js/module.rs index 554450304..3773fc286 100644 --- a/crates/oxc_parser/src/js/module.rs +++ b/crates/oxc_parser/src/js/module.rs @@ -3,7 +3,7 @@ use oxc_ast::{ast::*, context::Context, Span}; use oxc_diagnostics::Result; use super::function::FunctionKind; -use super::list::{AssertEntries, ExportNamedSpecifiers}; +use super::list::{AssertEntries, ExportNamedSpecifiers, ImportSpecifierList}; use crate::{diagnostics, lexer::Kind, list::SeparatedList, Parser}; impl<'a> Parser<'a> { @@ -127,24 +127,10 @@ impl<'a> Parser<'a> { // import { export1 , export2 as alias2 , [...] } from "module-name"; fn parse_import_specifiers(&mut self) -> Result> { - self.bump_any(); // advance `{` - let mut first = true; - let mut specifiers = self.ast.new_vec(); - // TODO: move to list.rs - while !self.at(Kind::RCurly) && !self.at(Kind::Eof) { - if first { - first = false; - } else { - self.expect(Kind::Comma)?; - if self.at(Kind::RCurly) { - break; - } - } - let import_specifier = self.parse_import_specifier()?; - let specifier = ImportDeclarationSpecifier::ImportSpecifier(import_specifier); - specifiers.push(specifier); - } - self.expect(Kind::RCurly)?; + let ctx = self.ctx; + self.ctx = Context::default(); + let specifiers = ImportSpecifierList::parse(self)?.import_specifiers; + self.ctx = ctx; Ok(specifiers) } @@ -350,7 +336,7 @@ impl<'a> Parser<'a> { // ImportSpecifier : // ImportedBinding // ModuleExportName as ImportedBinding - fn parse_import_specifier(&mut self) -> Result { + pub fn parse_import_specifier(&mut self) -> Result { let specifier_span = self.start_span(); let peek_kind = self.peek_kind(); let mut import_kind = ImportOrExportKind::Value;