Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Initial version of hard line breaks in object expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer committed Apr 22, 2022
1 parent 63a273b commit 3f8eb9c
Show file tree
Hide file tree
Showing 112 changed files with 1,051 additions and 459 deletions.
11 changes: 6 additions & 5 deletions crates/rome_js_formatter/src/js/expressions/object_expression.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{Format, FormatElement, FormatNode, Formatter};
use crate::{utils::has_leading_newline, Format, FormatElement, FormatNode, Formatter};
use rome_formatter::FormatResult;

use rome_js_syntax::JsObjectExpression;
use rome_js_syntax::JsObjectExpressionFields;
use rome_js_syntax::{JsObjectExpression, JsObjectExpressionFields};
use rome_rowan::AstNode;

impl FormatNode for JsObjectExpression {
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Expand All @@ -11,11 +11,12 @@ impl FormatNode for JsObjectExpression {
members,
r_curly_token,
} = self.as_fields();

let has_newline = has_leading_newline(members.syntax());
let members = members.format(formatter)?;

if members.is_empty() {
formatter.format_delimited_soft_block_indent(&l_curly_token?, members, &r_curly_token?)
} else if has_newline {
formatter.format_delimited_block_indent(&l_curly_token?, members, &r_curly_token?)
} else {
formatter.format_delimited_soft_block_spaces(&l_curly_token?, members, &r_curly_token?)
}
Expand Down
21 changes: 15 additions & 6 deletions crates/rome_js_formatter/src/js/module/export_named_from_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use crate::format_traits::FormatOptional;
use rome_formatter::FormatResult;

use crate::utils::format_with_semicolon;
use crate::{format_elements, space_token, Format, FormatElement, FormatNode, Formatter};
use crate::{
format_elements, space_token, utils::has_leading_newline, Format, FormatElement, FormatNode,
Formatter,
};

use rome_js_syntax::JsExportNamedFromClause;
use rome_js_syntax::JsExportNamedFromClauseFields;
use rome_rowan::AstNode;

impl FormatNode for JsExportNamedFromClause {
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Expand All @@ -23,13 +27,18 @@ impl FormatNode for JsExportNamedFromClause {
let type_token = type_token
.format_with_or_empty(formatter, |token| format_elements![token, space_token()])?;

let has_newline = has_leading_newline(specifiers.syntax());
let specifiers = specifiers.format(formatter)?;

let list = formatter.format_delimited_soft_block_spaces(
&l_curly_token?,
specifiers,
&r_curly_token?,
)?;
let list = if has_newline {
formatter.format_delimited_block_indent(&l_curly_token?, specifiers, &r_curly_token?)?
} else {
formatter.format_delimited_soft_block_spaces(
&l_curly_token?,
specifiers,
&r_curly_token?,
)?
};

let from = from_token.format(formatter)?;
let source = source.format(formatter)?;
Expand Down
27 changes: 18 additions & 9 deletions crates/rome_js_formatter/src/ts/declarations/enum_declaration.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::format_traits::{FormatOptional, FormatWith};
use crate::formatter::TrailingSeparator;
use crate::{
format_elements, join_elements, soft_line_break_or_space, space_token, token, FormatElement,
FormatNode, Formatter,
format_elements, join_elements, soft_line_break_or_space, space_token, token,
utils::has_leading_newline, FormatElement, FormatNode, Formatter,
};
use rome_formatter::FormatResult;
use rome_js_syntax::{TsEnumDeclaration, TsEnumDeclarationFields};
use rome_rowan::AstNode;

impl FormatNode for TsEnumDeclaration {
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
Expand All @@ -26,13 +27,21 @@ impl FormatNode for TsEnumDeclaration {
})?;
let id = id.format_with(formatter, |id| format_elements![id, space_token()])?;

let members =
formatter.format_separated(&members, || token(","), TrailingSeparator::default())?;
let list = formatter.format_delimited_soft_block_spaces(
&l_curly_token?,
join_elements(soft_line_break_or_space(), members),
&r_curly_token?,
)?;
let has_newline = has_leading_newline(members.syntax());
let members = join_elements(
soft_line_break_or_space(),
formatter.format_separated(&members, || token(","), TrailingSeparator::default())?,
);

let list = if has_newline {
formatter.format_delimited_block_indent(&l_curly_token?, members, &r_curly_token?)?
} else {
formatter.format_delimited_soft_block_spaces(
&l_curly_token?,
members,
&r_curly_token?,
)?
};

Ok(format_elements![const_token, enum_token, id, list])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ impl FormatNode for TsInterfaceDeclaration {
let extends = extends_clause.format_with_or_empty(formatter, |extends| {
format_elements![extends, space_token()]
})?;
let members = formatter.format_delimited_soft_block_spaces(
&l_curly_token?,
members.format(formatter)?,
&r_curly_token?,
)?;

let members = members.format(formatter)?;
let members = if members.is_empty() {
formatter.format_delimited_soft_block_indent(
&l_curly_token?,
members,
&r_curly_token?,
)?
} else {
formatter.format_delimited_block_indent(&l_curly_token?, members, &r_curly_token?)?
};
Ok(hard_group_elements(format_elements![
interface,
space_token(),
Expand Down
23 changes: 17 additions & 6 deletions crates/rome_js_formatter/src/ts/types/object_type.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
use crate::{Format, FormatElement, FormatNode, Formatter};
use crate::{utils::has_leading_newline, Format, FormatElement, FormatNode, Formatter};
use rome_formatter::FormatResult;

use rome_js_syntax::TsObjectType;
use rome_rowan::AstNode;

impl FormatNode for TsObjectType {
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
formatter.format_delimited_soft_block_spaces(
&self.l_curly_token()?,
self.members().format(formatter)?,
&self.r_curly_token()?,
)
let members = self.members();
if has_leading_newline(members.syntax()) {
formatter.format_delimited_block_indent(
&self.l_curly_token()?,
members.format(formatter)?,
&self.r_curly_token()?,
)
} else {
formatter.format_delimited_soft_block_spaces(
&self.l_curly_token()?,
members.format(formatter)?,
&self.r_curly_token()?,
)
}
}
}
12 changes: 12 additions & 0 deletions crates/rome_js_formatter/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ pub(crate) fn has_formatter_trivia(node: &JsSyntaxNode) -> bool {
false
}

/// Returns true if this node contains newlines in trivias.
pub(crate) fn has_leading_newline(node: &JsSyntaxNode) -> bool {
if let Some(leading_trivia) = node.first_leading_trivia() {
for piece in leading_trivia.pieces() {
if piece.is_newline() {
return true;
}
}
}
false
}

/// Format an element with a single line head and a body that might
/// be either a block or a single statement
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { hey } from "hey"
import { hey } from "hey";
import {
apple,
banana } from "fruits";
import {test} from "foo.json" assert { for: "for" }
import { // some funky comment
loooooooooooooooooooong as moreeeeeeloooooooooooooooooooong,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ expression: import_specifiers.js
# Input
import { hey } from "hey"
import { hey } from "hey";
import {
apple,
banana } from "fruits";
import {test} from "foo.json" assert { for: "for" }
import { // some funky comment
loooooooooooooooooooong as moreeeeeeloooooooooooooooooooong,
Expand All @@ -30,6 +33,7 @@ Quote style: Double Quotes
-----
import { hey } from "hey";
import { hey } from "hey";
import { apple, banana } from "fruits";
import { test } from "foo.json" assert { for: "for" };
import {
// some funky comment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Indent style: Tab
Line width: 80
Quote style: Double Quotes
-----
let a = { get foo() {} };
let b = { set foo(a) {} };
let a = {
get foo() {},
};
let b = {
set foo(a) {},
};

11 changes: 11 additions & 0 deletions crates/rome_js_formatter/tests/specs/js/module/object/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ let a = {

...spread,
}

const x = {apple: "banana"};

const y = {
apple: "banana",
};

({a, b, c} = {a: 'apple', b: 'banana', c: 'coconut'});

({
a, b, c} = {a: 'apple', b: 'banana', c: 'coconut'});
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ let a = {
...spread,
}

const x = {apple: "banana"};

const y = {
apple: "banana",
};

({a, b, c} = {a: 'apple', b: 'banana', c: 'coconut'});

({
a, b, c} = {a: 'apple', b: 'banana', c: 'coconut'});
=============================
# Outputs
## Output 1
Expand Down Expand Up @@ -53,3 +63,13 @@ let a = {
...spread,
};

const x = { apple: "banana" };

const y = {
apple: "banana",
};

({ a, b, c } = { a: "apple", b: "banana", c: "coconut" });

({ a, b, c } = { a: "apple", b: "banana", c: "coconut" });

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Quote style: Double Quotes
-----
const foo = {
"foo-bar": true,
"bar": { "lorem_ispsum": { "lorem-ipsum": true } },
"bar": {
"lorem_ispsum": {
"lorem-ipsum": true,
},
},
};

Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
assertion_line: 57
expression: call-with-template.js

---
# Input
```js
Expand All @@ -25,9 +23,13 @@ const result = template(
`
if (SOME_VAR === "") {}
`,
)({ SOME_VAR: value });
)({
SOME_VAR: value,
});

const output = template(`function f() %%A%%`)({ A: t.blockStatement([]) });
const output = template(`function f() %%A%%`)({
A: t.blockStatement([]),
});

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
assertion_line: 57
expression: lone-arg.js

---
# Input
```js
Expand Down Expand Up @@ -31,7 +29,11 @@ const bifornCringerMoshedPerplexSawderGlyphsHb = someBigFunctionName(`foo

# Output
```js
let vgChannel = pointPositionDefaultRef({ model, defaultPos, channel })();
let vgChannel = pointPositionDefaultRef({
model,
defaultPos,
channel,
})();
let vgChannel2 = pointPositionDefaultRef({ model, defaultPos, channel })();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
assertion_line: 57
expression: async-shorthand-method.js

---
# Input
```js
Expand All @@ -15,7 +13,10 @@ expression: async-shorthand-method.js

# Output
```js
({ async get() {}, async set() {} });
({
async get() {},
async set() {},
});
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
assertion_line: 121
expression: optional-chaining.js

---
# Input
```js
Expand Down Expand Up @@ -75,7 +73,13 @@ const ret = delete obj?.foo?.bar?.baz; // true
```js
// https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
const obj = { foo: { bar: { baz: 42 } } };
const obj = {
foo: {
bar: {
baz: 42,
},
},
};
const baz = obj?.foo?.bar?.baz; // 42
Expand Down Expand Up @@ -113,9 +117,19 @@ test?.(); // 42
exists?.(); // undefined
const obj3 = { foo: { bar: { baz: class {} } } };
const obj3 = {
foo: {
bar: {
baz: class {},
},
},
};
const obj4 = { foo: { bar: {} } };
const obj4 = {
foo: {
bar: {},
},
};
const ret = delete obj?.foo?.bar?.baz; // true
Expand Down
Loading

0 comments on commit 3f8eb9c

Please sign in to comment.