Skip to content

Commit

Permalink
Merge remote-tracking branch 'oxc/main' into feat-linter-add-eslint-n…
Browse files Browse the repository at this point in the history
…ew-cap
  • Loading branch information
Sysix committed Dec 28, 2024
2 parents 35bfb86 + 37c9959 commit f2a1e89
Show file tree
Hide file tree
Showing 25 changed files with 1,085 additions and 111 deletions.
16 changes: 16 additions & 0 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,22 @@ impl Statement<'_> {
| Statement::WhileStatement(_)
)
}

/// Returns the single statement from block statement, or self
pub fn get_one_child(&self) -> Option<&Self> {
if let Statement::BlockStatement(block_stmt) = self {
return (block_stmt.body.len() == 1).then(|| &block_stmt.body[0]);
}
Some(self)
}

/// Returns the single statement from block statement, or self
pub fn get_one_child_mut(&mut self) -> Option<&mut Self> {
if let Statement::BlockStatement(block_stmt) = self {
return (block_stmt.body.len() == 1).then_some(&mut block_stmt.body[0]);
}
Some(self)
}
}

impl<'a> FromIn<'a, Expression<'a>> for Statement<'a> {
Expand Down
9 changes: 7 additions & 2 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;

use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::{Parser, ParserReturn};
use oxc_parser::{ParseOptions, Parser, ParserReturn};
use oxc_span::SourceType;
use pico_args::Arguments;

Expand Down Expand Up @@ -52,7 +52,12 @@ fn parse<'a>(
source_text: &'a str,
source_type: SourceType,
) -> Option<ParserReturn<'a>> {
let ret = Parser::new(allocator, source_text, source_type).parse();
let ret = Parser::new(allocator, source_text, source_type)
.with_options(ParseOptions {
allow_return_outside_function: true,
..ParseOptions::default()
})
.parse();
if !ret.errors.is_empty() {
for error in ret.errors {
println!("{:?}", error.with_source_code(source_text.to_string()));
Expand Down
7 changes: 6 additions & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ fn print_if(if_stmt: &IfStatement<'_>, p: &mut Codegen, ctx: Context) {
p.print_soft_newline();
}
}
stmt => p.print_body(stmt, false, ctx),
stmt => {
p.print_body(stmt, false, ctx);
if if_stmt.alternate.is_some() {
p.print_indent();
}
}
}
if let Some(alternate) = if_stmt.alternate.as_ref() {
p.print_semicolon_if_needed();
Expand Down
12 changes: 12 additions & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ fn for_stmt() {
);
}

#[test]
fn if_stmt() {
test(
"function f() { if (foo) return foo; else if (bar) return foo; }",
"function f() {\n\tif (foo) return foo;\n\telse if (bar) return foo;\n}\n",
);
test_minify(
"function f() { if (foo) return foo; else if (bar) return foo; }",
"function f(){if(foo)return foo;else if(bar)return foo}",
);
}

#[test]
fn shorthand() {
test("let _ = { x }", "let _ = { x };\n");
Expand Down
21 changes: 21 additions & 0 deletions crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,27 @@ pub trait ConstantEvaluation<'a> {
}
None
}
BinaryOperator::Instanceof => {
if left.may_have_side_effects() {
return None;
}

let left_ty = ValueType::from(left);
if left_ty == ValueType::Undetermined {
return None;
}
if left_ty == ValueType::Object {
if let Some(right_ident) = right.get_identifier_reference() {
if right_ident.name == "Object" && self.is_global_reference(right_ident) {
return Some(ConstantValue::Boolean(true));
}
}
None
} else {
// Non-object types are never instances.
Some(ConstantValue::Boolean(false))
}
}
_ => None,
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_ecmascript/src/to_int_32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ impl ToInt32 for f64 {

let number = *self;

// NOTE: this also matches with negative zero
if !number.is_finite() || number == 0.0 {
return 0;
}

if number.is_finite() && number <= f64::from(i32::MAX) && number >= f64::from(i32::MIN) {
let i = number as i32;
if f64::from(i) == number {
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ mod eslint {
pub mod no_irregular_whitespace;
pub mod no_iterator;
pub mod no_label_var;
pub mod no_labels;
pub mod no_loss_of_precision;
pub mod no_magic_numbers;
pub mod no_multi_str;
pub mod no_nested_ternary;
pub mod no_new;
pub mod no_new_func;
pub mod no_new_native_nonconstructor;
Expand Down Expand Up @@ -536,6 +538,8 @@ oxc_macros::declare_all_lint_rules! {
eslint::max_lines,
eslint::max_params,
eslint::new_cap,
eslint::no_nested_ternary,
eslint::no_labels,
eslint::no_restricted_imports,
eslint::no_object_constructor,
eslint::no_duplicate_imports,
Expand Down
Loading

0 comments on commit f2a1e89

Please sign in to comment.