mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(parser): move import specifier parsing to list.rs (#167)
This commit is contained in:
parent
1b93d83ff7
commit
901f4948c1
2 changed files with 31 additions and 20 deletions
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<Vec<'a, ImportDeclarationSpecifier>> {
|
||||
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<ImportSpecifier> {
|
||||
pub fn parse_import_specifier(&mut self) -> Result<ImportSpecifier> {
|
||||
let specifier_span = self.start_span();
|
||||
let peek_kind = self.peek_kind();
|
||||
let mut import_kind = ImportOrExportKind::Value;
|
||||
|
|
|
|||
Loading…
Reference in a new issue