Skip to content

Commit

Permalink
fix(formatter/member-chain): regex formatting (#474)
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-teles authored Oct 3, 2023
1 parent 369998a commit b2ec1ad
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 392 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::utils::is_call_like_expression;
use biome_js_syntax::{
AnyJsArrayElement, AnyJsCallArgument, AnyJsExpression, AnyJsName, AnyJsObjectMember,
AnyJsObjectMemberName, AnyJsTemplateElement, JsSpread, JsStaticMemberExpressionFields,
JsTemplateExpression, JsUnaryOperator,
AnyJsArrayElement, AnyJsCallArgument, AnyJsExpression, AnyJsLiteralExpression, AnyJsName,
AnyJsObjectMember, AnyJsObjectMemberName, AnyJsTemplateElement, JsSpread,
JsStaticMemberExpressionFields, JsTemplateExpression, JsUnaryOperator,
};
use biome_rowan::{AstSeparatedList, SyntaxResult};

Expand All @@ -14,7 +14,9 @@ use biome_rowan::{AstSeparatedList, SyntaxResult};
///
/// Criteria are different:
/// - *complex*: if the chain of simple arguments exceeds the depth 2 or higher
/// - *complex*: if the argument is a [JsRegexLiteralExpression] with len() greater than 5
/// - *simple*: the argument is a literal
/// - *simple*: the argument is a [JsRegexLiteralExpression] with len() less than 5
/// - *simple*: the argument is a [JsThisExpression]
/// - *simple*: the argument is a [JsIdentifierExpression]
/// - *simple*: the argument is a [JsSuperExpression]
Expand Down Expand Up @@ -57,6 +59,7 @@ impl SimpleArgument {
if depth >= 2 {
return false;
}

if self.is_simple_literal() {
return true;
}
Expand All @@ -73,6 +76,7 @@ impl SimpleArgument {
.unwrap_or(false)
|| self.is_simple_call_like_expression(depth).unwrap_or(false)
|| self.is_simple_object_expression(depth)
|| self.is_simple_regex_expression()
}

fn is_simple_call_like_expression(&self, depth: u8) -> SyntaxResult<bool> {
Expand Down Expand Up @@ -161,6 +165,19 @@ impl SimpleArgument {
}
}

fn is_simple_regex_expression(&self) -> bool {
if let SimpleArgument::Expression(AnyJsExpression::AnyJsLiteralExpression(
AnyJsLiteralExpression::JsRegexLiteralExpression(regex),
)) = self
{
if let Ok((pattern, _)) = regex.decompose() {
return pattern.text().len() <= 5;
}
}

false
}

fn is_simple_array_expression(&self, depth: u8) -> bool {
if let SimpleArgument::Expression(AnyJsExpression::JsArrayExpression(array_expression)) =
self
Expand Down Expand Up @@ -193,13 +210,20 @@ impl SimpleArgument {
return true;
}

if let SimpleArgument::Expression(AnyJsExpression::AnyJsLiteralExpression(
AnyJsLiteralExpression::JsRegexLiteralExpression(_),
)) = self
{
return false;
}

matches!(
self,
SimpleArgument::Expression(
AnyJsExpression::AnyJsLiteralExpression(_)
| AnyJsExpression::JsThisExpression(_)
| AnyJsExpression::JsIdentifierExpression(_)
| AnyJsExpression::JsSuperExpression(_),
| AnyJsExpression::JsSuperExpression(_)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 5-len regex
z.string().min(2).max(200).regex(/[a-z]/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})

// <5-len regex
z.string().min(2).max(200).regex(/\w+/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})


// >5-len regex
z.string().min(2).max(200).regex(/^[a-zA-Z\-]+$/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})

const a = {
locales: z.record(
localeKeySchema,
z.string().min(2).regex(/^[a-zA-Z\-]+$/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})
),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/expression/member-chain/static_member-regex.js
---

# Input

```js
// 5-len regex
z.string().min(2).max(200).regex(/[a-z]/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})

// <5-len regex
z.string().min(2).max(200).regex(/\w+/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})


// >5-len regex
z.string().min(2).max(200).regex(/^[a-zA-Z\-]+$/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})

const a = {
locales: z.record(
localeKeySchema,
z.string().min(2).regex(/^[a-zA-Z\-]+$/gm, {
message: 'Only English alphabet symbols and hyphen allowed',
})
),
}

```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
-----

```js
// 5-len regex
z.string().min(2).max(200).regex(/[a-z]/gm, {
message: "Only English alphabet symbols and hyphen allowed",
});

// <5-len regex
z.string().min(2).max(200).regex(/\w+/gm, {
message: "Only English alphabet symbols and hyphen allowed",
});

// >5-len regex
z.string()
.min(2)
.max(200)
.regex(/^[a-zA-Z\-]+$/gm, {
message: "Only English alphabet symbols and hyphen allowed",
});

const a = {
locales: z.record(
localeKeySchema,
z
.string()
.min(2)
.regex(/^[a-zA-Z\-]+$/gm, {
message: "Only English alphabet symbols and hyphen allowed",
}),
),
};
```


This file was deleted.

Loading

0 comments on commit b2ec1ad

Please sign in to comment.