vue_oxc_toolkit 0.6.0

A parser to generate semantically correct AST from Vue SFCs for code linting purposes.
Documentation
use oxc_allocator::{Allocator, Dummy};
use oxc_ast::ast::Program;
use oxc_diagnostics::OxcDiagnostic;
use oxc_parser::ParseOptions;
use oxc_span::Span;
use oxc_syntax::module_record::ModuleRecord;

use crate::parser::{ParserImpl, ParserImplReturn};

mod irregular_whitespaces;
mod parser;

#[cfg(test)]
mod test;

pub struct VueOxcParser<'a> {
  allocator: &'a Allocator,
  source_text: &'a str,
  options: ParseOptions,
}

/// The return value of [`VueOxcParser::parse`]
/// Has the same struct as [`oxc_parser::ParserReturn`]
/// A workaround to pass #[`non_exhaustive`] of [`oxc_parser::ParserReturn`]
///
/// Do not provide `is_flow_language` field, it should be always false, as vue do not support it
#[non_exhaustive]
pub struct VueParserReturn<'a> {
  pub program: Program<'a>,
  pub module_record: ModuleRecord<'a>,
  pub errors: Vec<OxcDiagnostic>,
  pub irregular_whitespaces: Box<[Span]>,
  pub panicked: bool,
}

impl<'a> VueOxcParser<'a> {
  pub fn new(allocator: &'a Allocator, source_text: &'a str) -> Self {
    Self { allocator, source_text, options: ParseOptions::default() }
  }

  #[must_use]
  pub const fn with_options(mut self, options: ParseOptions) -> Self {
    self.options = options;
    self
  }
}

impl<'a> VueOxcParser<'a> {
  #[must_use]
  pub fn parse(self) -> VueParserReturn<'a> {
    let ParserImplReturn { program, errors, fatal, module_record } =
      ParserImpl::new(self.allocator, self.source_text, self.options).parse();

    if fatal {
      VueParserReturn {
        program: Program::dummy(self.allocator),
        module_record, // Dummy one if fatal, can be directly passed there without recreate a new one
        errors,
        irregular_whitespaces: Box::new([]),
        panicked: true,
      }
    } else {
      VueParserReturn {
        program,
        errors,
        panicked: false,
        irregular_whitespaces: self.get_irregular_whitespaces(),
        module_record,
      }
    }
  }
}