Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formatter/member-chain): regex formatting #474

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
victor-teles marked this conversation as resolved.
Show resolved Hide resolved
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