Skip to content

Commit

Permalink
refactor(ast): rename Expression::without_parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 4, 2024
1 parent 9652077 commit 858cdb9
Show file tree
Hide file tree
Showing 53 changed files with 114 additions and 115 deletions.
6 changes: 3 additions & 3 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl<'a> Expression<'a> {
}

/// Remove nested parentheses from this expression.
pub fn without_parenthesized(&self) -> &Self {
pub fn without_parentheses(&self) -> &Self {
let mut expr = self;
while let Expression::ParenthesizedExpression(paran_expr) = expr {
expr = &paran_expr.expression;
Expand Down Expand Up @@ -505,12 +505,12 @@ impl<'a> MemberExpression<'a> {
}

pub fn through_optional_is_specific_member_access(&self, object: &str, property: &str) -> bool {
let object_matches = match self.object().without_parenthesized() {
let object_matches = match self.object().without_parentheses() {
Expression::ChainExpression(x) => match &x.expression {
ChainElement::CallExpression(_) => false,
match_member_expression!(ChainElement) => {
let member_expr = x.expression.to_member_expression();
member_expr.object().without_parenthesized().is_specific_id(object)
member_expr.object().without_parentheses().is_specific_id(object)
}
},
x => x.is_specific_id(object),
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_codegen/src/binary_expr_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ pub enum Binaryish<'a> {
impl<'a> Binaryish<'a> {
pub fn left(&self) -> &'a Expression<'a> {
match self {
Self::Binary(e) => e.left.without_parenthesized(),
Self::Logical(e) => e.left.without_parenthesized(),
Self::Binary(e) => e.left.without_parentheses(),
Self::Logical(e) => e.left.without_parentheses(),
}
}

pub fn right(&self) -> &'a Expression<'a> {
match self {
Self::Binary(e) => e.right.without_parenthesized(),
Self::Logical(e) => e.right.without_parenthesized(),
Self::Binary(e) => e.right.without_parentheses(),
Self::Logical(e) => e.right.without_parentheses(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ impl<'a> Gen for ObjectProperty<'a> {

let mut shorthand = false;
if let PropertyKey::StaticIdentifier(key) = &self.key {
if let Expression::Identifier(ident) = self.value.without_parenthesized() {
if let Expression::Identifier(ident) = self.value.without_parentheses() {
if key.name == p.get_identifier_reference_name(ident) && key.name != "__proto__" {
shorthand = true;
}
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_linter/src/ast_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ pub fn is_method_call<'a>(
}
}

let Some(member_expr) = call_expr.callee.without_parenthesized().as_member_expression() else {
let Some(member_expr) = call_expr.callee.without_parentheses().as_member_expression() else {
return false;
};

if let Some(objects) = objects {
let Expression::Identifier(ident) = member_expr.object().without_parenthesized() else {
let Expression::Identifier(ident) = member_expr.object().without_parentheses() else {
return false;
};
if !objects.contains(&ident.name.as_str()) {
Expand Down Expand Up @@ -367,7 +367,7 @@ pub fn is_new_expression<'a>(
}
}

let Expression::Identifier(ident) = new_expr.callee.without_parenthesized() else {
let Expression::Identifier(ident) = new_expr.callee.without_parentheses() else {
return false;
};

Expand All @@ -381,12 +381,12 @@ pub fn is_new_expression<'a>(
pub fn call_expr_method_callee_info<'a>(
call_expr: &'a CallExpression<'a>,
) -> Option<(Span, &'a str)> {
let member_expr = call_expr.callee.without_parenthesized().as_member_expression()?;
let member_expr = call_expr.callee.without_parentheses().as_member_expression()?;
member_expr.static_property_info()
}

pub fn get_new_expr_ident_name<'a>(new_expr: &'a NewExpression<'a>) -> Option<&'a str> {
let Expression::Identifier(ident) = new_expr.callee.without_parenthesized() else {
let Expression::Identifier(ident) = new_expr.callee.without_parentheses() else {
return None;
};

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl GetterReturn {
}

fn handle_actual_expression<'a>(callee: &'a Expression<'a>) -> bool {
match callee.without_parenthesized() {
match callee.without_parentheses() {
expr @ match_member_expression!(Expression) => {
Self::handle_member_expression(expr.to_member_expression())
}
Expand All @@ -112,7 +112,7 @@ impl GetterReturn {
}

fn handle_paren_expr<'a>(expr: &'a Expression<'a>) -> bool {
match expr.without_parenthesized() {
match expr.without_parentheses() {
Expression::CallExpression(ce) => Self::handle_actual_expression(&ce.callee),
_ => false,
}
Expand Down
18 changes: 9 additions & 9 deletions crates/oxc_linter/src/rules/eslint/no_extra_boolean_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ fn is_bool_fn_or_constructor_call(node: &AstNode) -> bool {
match node.kind() {
AstKind::CallExpression(expr) => expr
.callee
.without_parenthesized()
.without_parentheses()
.get_identifier_reference()
.is_some_and(|x| x.name == "Boolean"),
AstKind::NewExpression(expr) => expr
.callee
.without_parenthesized()
.without_parentheses()
.get_identifier_reference()
.is_some_and(|x| x.name == "Boolean"),
_ => false,
Expand All @@ -137,14 +137,14 @@ fn is_first_arg(node: &AstNode, parent: &AstNode) -> bool {
match parent.kind() {
AstKind::CallExpression(expr) => expr.arguments.first().map_or(false, |arg| {
if let Some(expr) = arg.as_expression() {
expr.without_parenthesized().span() == node.kind().span()
expr.without_parentheses().span() == node.kind().span()
} else {
false
}
}),
AstKind::NewExpression(expr) => expr.arguments.first().map_or(false, |arg| {
if let Some(expr) = arg.as_expression() {
expr.without_parenthesized().span() == node.kind().span()
expr.without_parentheses().span() == node.kind().span()
} else {
false
}
Expand All @@ -156,23 +156,23 @@ fn is_first_arg(node: &AstNode, parent: &AstNode) -> bool {
fn is_inside_test_condition(node: &AstNode, ctx: &LintContext) -> bool {
get_real_parent(node, ctx).map_or(false, |parent| match parent.kind() {
AstKind::IfStatement(stmt) => {
let expr_span = stmt.test.get_inner_expression().without_parenthesized().span();
let expr_span = stmt.test.get_inner_expression().without_parentheses().span();
expr_span == node.kind().span()
}
AstKind::DoWhileStatement(stmt) => {
let expr_span = stmt.test.get_inner_expression().without_parenthesized().span();
let expr_span = stmt.test.get_inner_expression().without_parentheses().span();
expr_span == node.kind().span()
}
AstKind::WhileStatement(stmt) => {
let expr_span = stmt.test.get_inner_expression().without_parenthesized().span();
let expr_span = stmt.test.get_inner_expression().without_parentheses().span();
expr_span == node.kind().span()
}
AstKind::ConditionalExpression(stmt) => {
let expr_span = stmt.test.get_inner_expression().without_parenthesized().span();
let expr_span = stmt.test.get_inner_expression().without_parentheses().span();
expr_span == node.kind().span()
}
AstKind::ForStatement(stmt) => stmt.test.as_ref().map_or(false, |expr| {
let expr_span = expr.get_inner_expression().without_parenthesized().span();
let expr_span = expr.get_inner_expression().without_parentheses().span();
expr_span == node.kind().span()
}),
_ => false,
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_self_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl NoSelfAssign {
match left {
match_simple_assignment_target!(AssignmentTarget) => {
let simple_assignment_target = left.to_simple_assignment_target();
if let Expression::Identifier(id2) = right.without_parenthesized() {
if let Expression::Identifier(id2) = right.without_parentheses() {
let self_assign = matches!(simple_assignment_target.get_expression(), Some(Expression::Identifier(id1)) if id1.name == id2.name)
|| matches!(simple_assignment_target, SimpleAssignmentTarget::AssignmentTargetIdentifier(id1) if id1.name == id2.name);

Expand All @@ -94,7 +94,7 @@ impl NoSelfAssign {
}

if let Some(member_target) = simple_assignment_target.as_member_expression() {
if let Some(member_expr) = right.without_parenthesized().get_member_expr() {
if let Some(member_expr) = right.without_parentheses().get_member_expr() {
if self.is_member_expression_same_reference(member_expr, member_target) {
ctx.diagnostic(no_self_assign_diagnostic(member_expr.span()));
}
Expand All @@ -103,7 +103,7 @@ impl NoSelfAssign {
}

AssignmentTarget::ArrayAssignmentTarget(array_pattern) => {
if let Expression::ArrayExpression(array_expr) = right.without_parenthesized() {
if let Expression::ArrayExpression(array_expr) = right.without_parentheses() {
let end =
std::cmp::min(array_pattern.elements.len(), array_expr.elements.len());
let mut i = 0;
Expand Down Expand Up @@ -236,7 +236,7 @@ impl NoSelfAssign {
&**obj_prop
{
if key.static_name().is_some_and(|name| name == id1.binding.name) {
if let Expression::Identifier(id2) = expr.without_parenthesized() {
if let Expression::Identifier(id2) = expr.without_parentheses() {
if id1.binding.name == id2.name {
ctx.diagnostic(no_self_assign_diagnostic(*span));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Rule for PreferExponentiationOperator {
return;
}

if let Expression::Identifier(ident) = member_expr.object().without_parenthesized()
if let Expression::Identifier(ident) = member_expr.object().without_parentheses()
{
if GLOBAL_OBJECT_NAMES.contains(ident.name.as_str())
&& ctx.semantic().is_reference_to_global_variable(ident)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ declare_oxc_lint!(
impl Rule for PreferNumericLiterals {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::CallExpression(call_expr) = node.kind() {
match &call_expr.callee.without_parenthesized() {
match &call_expr.callee.without_parentheses() {
Expression::Identifier(ident) if ident.name == "parseInt" => {
if is_parse_int_call(ctx, ident, None) {
check_arguments(call_expr, ctx);
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_linter/src/rules/eslint/radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Rule for Radix {

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::CallExpression(call_expr) = node.kind() {
match call_expr.callee.without_parenthesized() {
match call_expr.callee.without_parentheses() {
Expression::Identifier(ident) => {
if ident.name == "parseInt"
&& ctx.symbols().is_global_reference(ident.reference_id().unwrap())
Expand All @@ -76,8 +76,7 @@ impl Rule for Radix {
}
}
Expression::StaticMemberExpression(member_expr) => {
if let Expression::Identifier(ident) =
member_expr.object.without_parenthesized()
if let Expression::Identifier(ident) = member_expr.object.without_parentheses()
{
if ident.name == "Number"
&& member_expr.property.name == "parseInt"
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/nextjs/inline_script_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Rule for InlineScriptId {
}
JSXAttributeItem::SpreadAttribute(spread_attr) => {
if let Expression::ObjectExpression(obj_expr) =
spread_attr.argument.without_parenthesized()
spread_attr.argument.without_parentheses()
{
for prop in &obj_expr.properties {
if let ObjectPropertyKind::ObjectProperty(obj_prop) = prop {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/oxc/bad_replace_all_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn resolve_flags<'a>(
expr: &'a Expression<'a>,
ctx: &LintContext<'a>,
) -> Option<(RegExpFlags, Span)> {
match expr.without_parenthesized() {
match expr.without_parentheses() {
Expression::RegExpLiteral(regexp_literal) => {
Some((regexp_literal.regex.flags, regexp_literal.span))
}
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_linter/src/rules/oxc/const_comparisons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ impl Rule for ConstComparisons {
}

let Some((right_cmp_op, right_expr, right_const_expr, _)) =
comparison_to_const(logical_expr.right.without_parenthesized())
comparison_to_const(logical_expr.right.without_parentheses())
else {
return;
};

for (left_cmp_op, left_expr, left_const_expr, left_span) in
all_and_comparison_to_const(logical_expr.left.without_parenthesized())
all_and_comparison_to_const(logical_expr.left.without_parentheses())
{
let Some(ordering) = left_const_expr.value.partial_cmp(&right_const_expr.value) else {
return;
Expand Down Expand Up @@ -135,7 +135,7 @@ impl Rule for ConstComparisons {

ctx.diagnostic(impossible(
left_span,
logical_expr.right.without_parenthesized().span(),
logical_expr.right.without_parentheses().span(),
&format!(
"`{} {} {}` ",
expr_str,
Expand All @@ -162,7 +162,7 @@ fn comparison_to_const<'a, 'b>(
) -> Option<(CmpOp, &'b Expression<'a>, &'b NumericLiteral<'a>, Span)> {
if let Expression::BinaryExpression(bin_expr) = expr {
if let Ok(cmp_op) = CmpOp::try_from(bin_expr.operator) {
match (&bin_expr.left.without_parenthesized(), &bin_expr.right.without_parenthesized())
match (&bin_expr.left.without_parentheses(), &bin_expr.right.without_parentheses())
{
(Expression::NumericLiteral(lit), _) => {
return Some((cmp_op.reverse(), &bin_expr.right, lit, bin_expr.span));
Expand All @@ -185,9 +185,9 @@ fn all_and_comparison_to_const<'a, 'b>(
Expression::LogicalExpression(logical_expr)
if logical_expr.operator == LogicalOperator::And =>
{
let left_iter = all_and_comparison_to_const(logical_expr.left.without_parenthesized());
let left_iter = all_and_comparison_to_const(logical_expr.left.without_parentheses());
let right_iter =
all_and_comparison_to_const(logical_expr.right.without_parenthesized());
all_and_comparison_to_const(logical_expr.right.without_parentheses());
Box::new(left_iter.chain(right_iter))
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/oxc/erasing_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Rule for ErasingOp {
}

fn is_number_value(expr: &Expression, value: f64) -> bool {
if let Expression::NumericLiteral(number_literal) = expr.without_parenthesized() {
if let Expression::NumericLiteral(number_literal) = expr.without_parentheses() {
(number_literal.value - value).abs() < f64::EPSILON
} else {
false
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/button_has_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl ButtonHasType {
}

fn is_valid_button_type_prop_expression(&self, expr: &Expression) -> bool {
match expr.without_parenthesized() {
match expr.without_parentheses() {
Expression::StringLiteral(str) => {
self.is_valid_button_type_prop_string_literal(str.value.as_str())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/jsx_boolean_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Rule for JsxBooleanValue {
}
Some(JSXAttributeValue::ExpressionContainer(container)) => {
if let Some(expr) = container.expression.as_expression() {
if let Expression::BooleanLiteral(expr) = expr.without_parenthesized() {
if let Expression::BooleanLiteral(expr) = expr.without_parentheses() {
if expr.value && self.is_never(ident.name.as_str()) {
let span = Span::new(ident.span.end, jsx_attr.span.end);
ctx.diagnostic_with_fix(
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/jsx_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn is_in_array_or_iter<'a, 'b>(
return Some(InsideArrayOrIterator::Array);
}
AstKind::CallExpression(v) => {
let callee = &v.callee.without_parenthesized();
let callee = &v.callee.without_parentheses();

if let Some(v) = callee.as_member_expression() {
if let Some((span, ident)) = v.static_property_info() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl Rule for JsxPropsNoSpreadMulti {
let mut duplicate_spreads: FxHashMap<&Atom, Vec<Span>> = FxHashMap::default();

for spread_attr in spread_attrs {
let argument_without_parenthesized = spread_attr.argument.without_parenthesized();
let argument_without_parenthesized = spread_attr.argument.without_parentheses();

if let Some(identifier_name) =
argument_without_parenthesized.get_identifier_reference().map(|arg| &arg.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl ReactPerfRule for JsxNoJsxAsProp {
}

fn check_expression(expr: &Expression) -> Option<Span> {
match expr.without_parenthesized() {
match expr.without_parentheses() {
Expression::JSXElement(expr) => Some(expr.span),
Expression::LogicalExpression(expr) => {
check_expression(&expr.left).or_else(|| check_expression(&expr.right))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl ReactPerfRule for JsxNoNewArrayAsProp {
}

fn check_expression(expr: &Expression) -> Option<Span> {
match expr.without_parenthesized() {
match expr.without_parentheses() {
Expression::ArrayExpression(expr) => Some(expr.span),
Expression::CallExpression(expr) => {
if is_constructor_matching_name(&expr.callee, "Array")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl ReactPerfRule for JsxNoNewFunctionAsProp {
}

fn check_expression(expr: &Expression) -> Option<Span> {
match expr.without_parenthesized() {
match expr.without_parentheses() {
Expression::ArrowFunctionExpression(expr) => Some(expr.span),
Expression::FunctionExpression(expr) => Some(expr.span),
Expression::CallExpression(expr) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl ReactPerfRule for JsxNoNewObjectAsProp {
}

fn check_expression(expr: &Expression) -> Option<Span> {
match expr.without_parenthesized() {
match expr.without_parentheses() {
Expression::ObjectExpression(expr) => Some(expr.span),
Expression::CallExpression(expr) => {
if is_constructor_matching_name(&expr.callee, "Object")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ fn is_function_argument(parent: &AstNode, callee: Option<&AstNode>) -> bool {
return true;
}

match call_expr.callee.without_parenthesized() {
match call_expr.callee.without_parentheses() {
Expression::FunctionExpression(func_expr) => {
let AstKind::Function(callee_func_expr) = callee.unwrap().kind() else { return false };
func_expr.span != callee_func_expr.span
Expand Down
Loading

0 comments on commit 858cdb9

Please sign in to comment.