Skip to content

Commit

Permalink
Merge branch 'main' into fix-suggestion-no-empty-static-block
Browse files Browse the repository at this point in the history
  • Loading branch information
tapanprakasht authored Oct 21, 2024
2 parents cb245ab + 70efa47 commit 388a32e
Show file tree
Hide file tree
Showing 47 changed files with 6,029 additions and 2,281 deletions.
1 change: 1 addition & 0 deletions apps/oxlint/fixtures/linter/fix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
debugger
26 changes: 24 additions & 2 deletions apps/oxlint/src/lint/mod.rs → apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ mod test {
let args = &["fixtures/linter"];
let result = test(args);
assert!(result.number_of_rules > 0);
assert_eq!(result.number_of_files, 2);
assert_eq!(result.number_of_warnings, 2);
assert_eq!(result.number_of_files, 3);
assert_eq!(result.number_of_warnings, 3);
assert_eq!(result.number_of_errors, 0);
}

Expand Down Expand Up @@ -564,4 +564,26 @@ mod test {
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);
}

#[test]
fn test_fix() {
use std::fs;
let file = "fixtures/linter/fix.js";
let args = &["--fix", file];
let content = fs::read_to_string(file).unwrap();
assert_eq!(&content, "debugger\n");

// Apply fix to the file.
let _ = test(args);
assert_eq!(fs::read_to_string(file).unwrap(), "\n");

// File should not be modified if no fix is applied.
let modified_before = fs::metadata(file).unwrap().modified().unwrap();
let _ = test(args);
let modified_after = fs::metadata(file).unwrap().modified().unwrap();
assert_eq!(modified_before, modified_after);

// Write the file back.
fs::write(file, content).unwrap();
}
}
25 changes: 25 additions & 0 deletions crates/oxc_allocator/src/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::ptr;

use crate::Box;

/// Memory address of an AST node in arena.
///
/// `Address` is generated from a `Box<T>`.
/// AST nodes in a `Box` in an arena are guaranteed to never move in memory,
/// so this address acts as a unique identifier for the duration of the arena's existence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Address(usize);

/// Trait for getting the memory address of an AST node.
pub trait GetAddress {
/// Get the memory address of a value allocated in the arena.
fn address(&self) -> Address;
}

impl<'a, T> GetAddress for Box<'a, T> {
/// Get the memory address of a value allocated in the arena.
#[inline]
fn address(&self) -> Address {
Address(ptr::addr_of!(**self) as usize)
}
}
16 changes: 0 additions & 16 deletions crates/oxc_allocator/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,6 @@ impl<'alloc, T: Hash> Hash for Box<'alloc, T> {
}
}

/// Memory address of an AST node in arena.
///
/// `Address` is generated from a `Box<T>`.
/// AST nodes in a `Box` in an arena are guaranteed to never move in memory,
/// so this address acts as a unique identifier for the duration of the arena's existence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Address(usize);

impl<'a, T> Box<'a, T> {
/// Get the memory address of a value allocated in the arena.
#[inline]
pub fn address(&self) -> Address {
Address(ptr::addr_of!(**self) as usize)
}
}

#[cfg(test)]
mod test {
use std::hash::{DefaultHasher, Hash, Hasher};
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ use std::{
pub use bumpalo::collections::String;
use bumpalo::Bump;

mod address;
mod boxed;
mod clone_in;
mod convert;
mod vec;

pub use boxed::{Address, Box};
pub use address::{Address, GetAddress};
pub use boxed::Box;
pub use clone_in::CloneIn;
pub use convert::{FromIn, IntoIn};
pub use vec::Vec;
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,6 @@ pub struct TryStatement<'a> {
/// The `catch` clause, including the parameter and the block statement
pub handler: Option<Box<'a, CatchClause<'a>>>,
/// The `finally` clause
#[visit(as(FinallyClause))]
pub finalizer: Option<Box<'a, BlockStatement<'a>>>,
}

Expand Down Expand Up @@ -1839,7 +1838,6 @@ pub struct Class<'a> {
/// class Foo extends Bar {}
/// // ^^^
/// ```
#[visit(as(ClassHeritage))]
pub super_class: Option<Expression<'a>>,
/// Type parameters passed to super class.
///
Expand Down
43 changes: 42 additions & 1 deletion crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(missing_docs)]
use std::{borrow::Cow, cell::Cell, fmt};

use oxc_allocator::{Box, FromIn, Vec};
use oxc_allocator::{Address, Box, FromIn, GetAddress, Vec};
use oxc_span::{Atom, GetSpan, Span};
use oxc_syntax::{
operator::UnaryOperator,
Expand Down Expand Up @@ -768,6 +768,47 @@ impl<'a> FromIn<'a, Expression<'a>> for Statement<'a> {
}
}

impl<'a> GetAddress for Statement<'a> {
// `#[inline]` because compiler should boil this down to a single assembly instruction
#[inline]
fn address(&self) -> Address {
match self {
Statement::BlockStatement(s) => s.address(),
Statement::BreakStatement(s) => s.address(),
Statement::ContinueStatement(s) => s.address(),
Statement::DebuggerStatement(s) => s.address(),
Statement::DoWhileStatement(s) => s.address(),
Statement::EmptyStatement(s) => s.address(),
Statement::ExpressionStatement(s) => s.address(),
Statement::ForInStatement(s) => s.address(),
Statement::ForOfStatement(s) => s.address(),
Statement::ForStatement(s) => s.address(),
Statement::IfStatement(s) => s.address(),
Statement::LabeledStatement(s) => s.address(),
Statement::ReturnStatement(s) => s.address(),
Statement::SwitchStatement(s) => s.address(),
Statement::ThrowStatement(s) => s.address(),
Statement::TryStatement(s) => s.address(),
Statement::WhileStatement(s) => s.address(),
Statement::WithStatement(s) => s.address(),
Statement::VariableDeclaration(s) => s.address(),
Statement::FunctionDeclaration(s) => s.address(),
Statement::ClassDeclaration(s) => s.address(),
Statement::TSTypeAliasDeclaration(s) => s.address(),
Statement::TSInterfaceDeclaration(s) => s.address(),
Statement::TSEnumDeclaration(s) => s.address(),
Statement::TSModuleDeclaration(s) => s.address(),
Statement::TSImportEqualsDeclaration(s) => s.address(),
Statement::ImportDeclaration(s) => s.address(),
Statement::ExportAllDeclaration(s) => s.address(),
Statement::ExportDefaultDeclaration(s) => s.address(),
Statement::ExportNamedDeclaration(s) => s.address(),
Statement::TSExportAssignment(s) => s.address(),
Statement::TSNamespaceExportDeclaration(s) => s.address(),
}
}
}

impl<'a> Directive<'a> {
/// A Use Strict Directive is an ExpressionStatement in a Directive Prologue whose StringLiteral is either of the exact code point sequences "use strict" or 'use strict'.
/// A Use Strict Directive may not contain an EscapeSequence or LineContinuation.
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ impl<'a> AstKind<'a> {

Self::SwitchCase(_) => "SwitchCase".into(),
Self::CatchClause(_) => "CatchClause".into(),
Self::FinallyClause(_) => "FinallyClause".into(),

Self::VariableDeclaration(_) => "VariableDeclaration".into(),
Self::VariableDeclarator(v) => format!(
Expand Down Expand Up @@ -298,7 +297,6 @@ impl<'a> AstKind<'a> {
Self::AssignmentTargetWithDefault(_) => "AssignmentTargetWithDefault".into(),
Self::SpreadElement(_) => "SpreadElement".into(),
Self::Elision(_) => "Elision".into(),
Self::ExpressionArrayElement(_) => "ExpressionArrayElement".into(),
Self::BindingRestElement(_) => "BindingRestElement".into(),

Self::Function(x) => format!("Function({})", or_anonymous(x.id.as_ref())).into(),
Expand All @@ -314,7 +312,6 @@ impl<'a> AstKind<'a> {
Self::Class(c) => format!("Class({})", or_anonymous(c.id.as_ref())).into(),
Self::TSClassImplements(_) => "TSClassImplements".into(),
Self::ClassBody(_) => "ClassBody".into(),
Self::ClassHeritage(_) => "ClassHeritage".into(),
Self::StaticBlock(_) => "StaticBlock".into(),
Self::PropertyDefinition(_) => "PropertyDefinition".into(),
Self::MethodDefinition(_) => "MethodDefinition".into(),
Expand Down
36 changes: 0 additions & 36 deletions crates/oxc_ast/src/generated/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ pub enum AstType {
LabeledStatement,
ThrowStatement,
TryStatement,
FinallyClause,
CatchClause,
CatchParameter,
DebuggerStatement,
Expand All @@ -90,7 +89,6 @@ pub enum AstType {
ArrowFunctionExpression,
YieldExpression,
Class,
ClassHeritage,
ClassBody,
MethodDefinition,
PropertyDefinition,
Expand Down Expand Up @@ -175,7 +173,6 @@ pub enum AstType {
JSXSpreadAttribute,
JSXIdentifier,
JSXText,
ExpressionArrayElement,
}

/// Untyped AST Node Kind
Expand Down Expand Up @@ -248,7 +245,6 @@ pub enum AstKind<'a> {
LabeledStatement(&'a LabeledStatement<'a>),
ThrowStatement(&'a ThrowStatement<'a>),
TryStatement(&'a TryStatement<'a>),
FinallyClause(&'a BlockStatement<'a>),
CatchClause(&'a CatchClause<'a>),
CatchParameter(&'a CatchParameter<'a>),
DebuggerStatement(&'a DebuggerStatement),
Expand All @@ -263,7 +259,6 @@ pub enum AstKind<'a> {
ArrowFunctionExpression(&'a ArrowFunctionExpression<'a>),
YieldExpression(&'a YieldExpression<'a>),
Class(&'a Class<'a>),
ClassHeritage(&'a Expression<'a>),
ClassBody(&'a ClassBody<'a>),
MethodDefinition(&'a MethodDefinition<'a>),
PropertyDefinition(&'a PropertyDefinition<'a>),
Expand Down Expand Up @@ -348,7 +343,6 @@ pub enum AstKind<'a> {
JSXSpreadAttribute(&'a JSXSpreadAttribute<'a>),
JSXIdentifier(&'a JSXIdentifier<'a>),
JSXText(&'a JSXText<'a>),
ExpressionArrayElement(&'a Expression<'a>),
}

impl<'a> GetSpan for AstKind<'a> {
Expand Down Expand Up @@ -422,7 +416,6 @@ impl<'a> GetSpan for AstKind<'a> {
Self::LabeledStatement(it) => it.span(),
Self::ThrowStatement(it) => it.span(),
Self::TryStatement(it) => it.span(),
Self::FinallyClause(it) => it.span(),
Self::CatchClause(it) => it.span(),
Self::CatchParameter(it) => it.span(),
Self::DebuggerStatement(it) => it.span(),
Expand All @@ -437,7 +430,6 @@ impl<'a> GetSpan for AstKind<'a> {
Self::ArrowFunctionExpression(it) => it.span(),
Self::YieldExpression(it) => it.span(),
Self::Class(it) => it.span(),
Self::ClassHeritage(it) => it.span(),
Self::ClassBody(it) => it.span(),
Self::MethodDefinition(it) => it.span(),
Self::PropertyDefinition(it) => it.span(),
Expand Down Expand Up @@ -522,7 +514,6 @@ impl<'a> GetSpan for AstKind<'a> {
Self::JSXSpreadAttribute(it) => it.span(),
Self::JSXIdentifier(it) => it.span(),
Self::JSXText(it) => it.span(),
Self::ExpressionArrayElement(it) => it.span(),
}
}
}
Expand Down Expand Up @@ -1131,15 +1122,6 @@ impl<'a> AstKind<'a> {
}
}

#[inline]
pub fn as_finally_clause(&self) -> Option<&'a BlockStatement<'a>> {
if let Self::FinallyClause(v) = self {
Some(*v)
} else {
None
}
}

#[inline]
pub fn as_catch_clause(&self) -> Option<&'a CatchClause<'a>> {
if let Self::CatchClause(v) = self {
Expand Down Expand Up @@ -1266,15 +1248,6 @@ impl<'a> AstKind<'a> {
}
}

#[inline]
pub fn as_class_heritage(&self) -> Option<&'a Expression<'a>> {
if let Self::ClassHeritage(v) = self {
Some(*v)
} else {
None
}
}

#[inline]
pub fn as_class_body(&self) -> Option<&'a ClassBody<'a>> {
if let Self::ClassBody(v) = self {
Expand Down Expand Up @@ -2034,13 +2007,4 @@ impl<'a> AstKind<'a> {
None
}
}

#[inline]
pub fn as_expression_array_element(&self) -> Option<&'a Expression<'a>> {
if let Self::ExpressionArrayElement(v) = self {
Some(*v)
} else {
None
}
}
}
Loading

0 comments on commit 388a32e

Please sign in to comment.