mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
refactor: improve code coverage in various places (#721)
This commit is contained in:
parent
c6ed90900c
commit
fdf288c685
9 changed files with 116 additions and 51 deletions
5
.github/codecov.yml
vendored
5
.github/codecov.yml
vendored
|
|
@ -16,8 +16,13 @@ ignore:
|
|||
- "tasks"
|
||||
- "editor"
|
||||
- "crates/oxc_ast/src/visit_mut.rs"
|
||||
- "crates/oxc_ast/src/ast_kind.rs"
|
||||
- "crates/oxc_ast/src/span.rs"
|
||||
- "crates/oxc_hir/src/hir_kind.rs"
|
||||
- "crates/oxc_hir/src/span.rs"
|
||||
- "crates/oxc_wasm" # Remove this once wasm is completed
|
||||
- "crates/oxc_napi"
|
||||
- "crates/oxc_parser/fuzz"
|
||||
- "crates/oxc_diagnostics"
|
||||
- "crates/oxc_type_synthesis"
|
||||
- "crates/oxc_query" # Not aiming for test coverage right now with @u9g
|
||||
|
|
|
|||
|
|
@ -48,14 +48,15 @@ impl<'alloc, T: ?Sized + Debug> Debug for Box<'alloc, T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'alloc, T> PartialEq for Box<'alloc, T>
|
||||
where
|
||||
T: PartialEq<T> + ?Sized,
|
||||
{
|
||||
fn eq(&self, other: &Box<'alloc, T>) -> bool {
|
||||
PartialEq::eq(&**self, &**other)
|
||||
}
|
||||
}
|
||||
// Unused right now.
|
||||
// impl<'alloc, T> PartialEq for Box<'alloc, T>
|
||||
// where
|
||||
// T: PartialEq<T> + ?Sized,
|
||||
// {
|
||||
// fn eq(&self, other: &Box<'alloc, T>) -> bool {
|
||||
// PartialEq::eq(&**self, &**other)
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<'alloc, T> Serialize for Box<'alloc, T>
|
||||
where
|
||||
|
|
|
|||
|
|
@ -48,19 +48,21 @@ pub use crate::{
|
|||
// The following test make sure all enum variants are boxed, resulting 16 bytes for each enum.
|
||||
// Read `https://nnethercote.github.io/perf-book/type-sizes.html` for more details.
|
||||
#[cfg(target_pointer_width = "64")]
|
||||
mod size_asserts {
|
||||
#[test]
|
||||
fn size_asserts() {
|
||||
use crate::ast;
|
||||
use oxc_index::assert_eq_size;
|
||||
|
||||
assert_eq_size!(crate::ast::Statement, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::Expression, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::Declaration, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::BindingPatternKind, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::ModuleDeclaration, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::ClassElement, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::ExportDefaultDeclarationKind, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::AssignmentTargetPattern, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::AssignmentTargetMaybeDefault, [u8; 24]);
|
||||
assert_eq_size!(crate::ast::AssignmentTargetProperty, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::TSLiteral, [u8; 16]);
|
||||
assert_eq_size!(crate::ast::TSType, [u8; 16]);
|
||||
assert_eq_size!(ast::Statement, [u8; 16]);
|
||||
assert_eq_size!(ast::Expression, [u8; 16]);
|
||||
assert_eq_size!(ast::Declaration, [u8; 16]);
|
||||
assert_eq_size!(ast::BindingPatternKind, [u8; 16]);
|
||||
assert_eq_size!(ast::ModuleDeclaration, [u8; 16]);
|
||||
assert_eq_size!(ast::ClassElement, [u8; 16]);
|
||||
assert_eq_size!(ast::ExportDefaultDeclarationKind, [u8; 16]);
|
||||
assert_eq_size!(ast::AssignmentTargetPattern, [u8; 16]);
|
||||
assert_eq_size!(ast::AssignmentTargetMaybeDefault, [u8; 24]);
|
||||
assert_eq_size!(ast::AssignmentTargetProperty, [u8; 16]);
|
||||
assert_eq_size!(ast::TSLiteral, [u8; 16]);
|
||||
assert_eq_size!(ast::TSType, [u8; 16]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ impl<'a> GetPrecedence for Expression<'a> {
|
|||
Self::NewExpression(expr) => expr.precedence(),
|
||||
Self::CallExpression(expr) => expr.precedence(),
|
||||
Self::MemberExpression(expr) => expr.precedence(),
|
||||
_ => Precedence::highest(),
|
||||
_ => panic!("All cases should be covered"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -260,3 +260,47 @@ impl fmt::Display for ResolveOptions {
|
|||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{AliasValue, EnforceExtension, ResolveOptions, Restriction};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[test]
|
||||
fn enforce_extension() {
|
||||
assert!(EnforceExtension::Auto.is_auto());
|
||||
assert!(!EnforceExtension::Enabled.is_auto());
|
||||
assert!(!EnforceExtension::Disabled.is_auto());
|
||||
|
||||
assert!(!EnforceExtension::Auto.is_enabled());
|
||||
assert!(EnforceExtension::Enabled.is_enabled());
|
||||
assert!(!EnforceExtension::Disabled.is_enabled());
|
||||
|
||||
assert!(!EnforceExtension::Auto.is_disabled());
|
||||
assert!(!EnforceExtension::Enabled.is_disabled());
|
||||
assert!(EnforceExtension::Disabled.is_disabled());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display() {
|
||||
let options = ResolveOptions {
|
||||
alias: vec![("a".into(), vec![AliasValue::Ignore])],
|
||||
alias_fields: vec!["browser".into()],
|
||||
condition_names: vec!["require".into()],
|
||||
enforce_extension: EnforceExtension::Enabled,
|
||||
extension_alias: vec![(".js".into(), vec![".ts".into()])],
|
||||
exports_fields: vec!["exports".into()],
|
||||
fallback: vec![("fallback".into(), vec![AliasValue::Ignore])],
|
||||
fully_specified: true,
|
||||
resolve_to_context: true,
|
||||
prefer_relative: true,
|
||||
prefer_absolute: true,
|
||||
restrictions: vec![Restriction::Path(PathBuf::from("restrictions"))],
|
||||
roots: vec![PathBuf::from("roots")],
|
||||
..ResolveOptions::default()
|
||||
};
|
||||
|
||||
let expected = r#"alias:[("a", [Ignore])],alias_fields:["browser"],condition_names:["require"],enforce_extension:Enabled,exports_fields:["exports"],extension_alias:[(".js", [".ts"])],extensions:[".js", ".json", ".node"],fallback:[("fallback", [Ignore])],fully_specified:true,main_fields:["main"],main_files:["index"],modules:["node_modules"],resolve_to_context:true,prefer_relative:true,prefer_absolute:true,restrictions:[Path("restrictions")],roots:["roots"],symlinks:true,"#;
|
||||
assert_eq!(format!("{options}"), expected);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,12 +83,6 @@ impl<T: AsRef<str>> PartialEq<T> for Atom {
|
|||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Atom> for String {
|
||||
fn eq(&self, other: &Atom) -> bool {
|
||||
self.as_str() == other.0.as_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<Atom> for &str {
|
||||
fn eq(&self, other: &Atom) -> bool {
|
||||
*self == other.0.as_str()
|
||||
|
|
|
|||
|
|
@ -165,13 +165,6 @@ pub enum ExportLocalName {
|
|||
}
|
||||
|
||||
impl ExportLocalName {
|
||||
pub fn name(&self) -> Option<&Atom> {
|
||||
match self {
|
||||
Self::Name(ns) => Some(ns.name()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_default(&self) -> bool {
|
||||
matches!(self, Self::Default(_))
|
||||
}
|
||||
|
|
@ -180,3 +173,45 @@ impl ExportLocalName {
|
|||
matches!(self, Self::Null)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{ExportExportName, ExportLocalName, ImportImportName, NameSpan};
|
||||
use oxc_span::Span;
|
||||
|
||||
#[test]
|
||||
fn import_import_name() {
|
||||
let name = NameSpan::new("name".into(), Span::new(0, 0));
|
||||
assert!(!ImportImportName::Name(name.clone()).is_default());
|
||||
assert!(!ImportImportName::NamespaceObject.is_default());
|
||||
assert!(ImportImportName::Default(Span::new(0, 0)).is_default());
|
||||
|
||||
assert!(!ImportImportName::Name(name).is_namespace_object());
|
||||
assert!(ImportImportName::NamespaceObject.is_namespace_object());
|
||||
assert!(!ImportImportName::Default(Span::new(0, 0)).is_namespace_object());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_import_name() {
|
||||
let name = NameSpan::new("name".into(), Span::new(0, 0));
|
||||
assert!(!ExportExportName::Name(name.clone()).is_default());
|
||||
assert!(ExportExportName::Default(Span::new(0, 0)).is_default());
|
||||
assert!(!ExportExportName::Null.is_default());
|
||||
|
||||
assert!(!ExportExportName::Name(name).is_null());
|
||||
assert!(!ExportExportName::Default(Span::new(0, 0)).is_null());
|
||||
assert!(ExportExportName::Null.is_null());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_local_name() {
|
||||
let name = NameSpan::new("name".into(), Span::new(0, 0));
|
||||
assert!(!ExportLocalName::Name(name.clone()).is_default());
|
||||
assert!(ExportLocalName::Default(Span::new(0, 0)).is_default());
|
||||
assert!(!ExportLocalName::Null.is_default());
|
||||
|
||||
assert!(!ExportLocalName::Name(name).is_null());
|
||||
assert!(!ExportLocalName::Default(Span::new(0, 0)).is_null());
|
||||
assert!(ExportLocalName::Null.is_null());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
use std::fmt;
|
||||
|
||||
pub trait GetPrecedence {
|
||||
fn precedence(&self) -> Precedence;
|
||||
}
|
||||
|
|
@ -45,17 +43,7 @@ impl Precedence {
|
|||
Self::Comma
|
||||
}
|
||||
|
||||
pub fn highest() -> Self {
|
||||
Self::Grouping
|
||||
}
|
||||
|
||||
pub fn is_right_associative(&self) -> bool {
|
||||
matches!(self, Self::Exponential)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Precedence {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{:?}({})", self, (*self) as u8)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,10 +54,6 @@ impl ScopeFlags {
|
|||
self.intersects(Self::Var)
|
||||
}
|
||||
|
||||
pub fn is_class(&self) -> bool {
|
||||
self.intersects(Self::Var)
|
||||
}
|
||||
|
||||
pub fn is_set_accessor(&self) -> bool {
|
||||
self.contains(Self::SetAccessor)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue