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

Commit

Permalink
Merge branch 'main' into fix-no-redeclare-for-index-signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
unvalley authored Jun 12, 2023
2 parents ee33b74 + 659412a commit 029c4a2
Show file tree
Hide file tree
Showing 229 changed files with 834 additions and 348 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
### Editors
### Formatter

- Added a new option called `--jsx-quote-style` to the formatter. This option allows you to choose between single and double quotes for JSX attributes. [#4486](https://github.com/rome/tools/issues/4486)

### Linter

- Fix a crash in the `NoParameterAssign` rule that occurred when there was a bogus binding. [#4323](https://github.com/rome/tools/issues/4323)

#### Other changes

- `noRedeclare`: allow redeclare of index signatures are in different type members [#4478](https://github.com/rome/tools/issues/4478)
Expand Down
32 changes: 32 additions & 0 deletions crates/rome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,38 @@ if (true) {
));
}

#[test]
fn apply_bogus_argument() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let file_path = Path::new("fix.js");
fs.insert(
file_path.into(),
"function _13_1_3_fun(arguments) { }".as_bytes(),
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[
("check"),
file_path.as_os_str().to_str().unwrap(),
("--apply-unsafe"),
]),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"apply_bogus_argument",
fs,
console,
result,
));
}

#[test]
fn ignores_unknown_file() {
let mut fs = MemoryFileSystem::default();
Expand Down
53 changes: 53 additions & 0 deletions crates/rome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ const CUSTOM_FORMAT_AFTER: &str = r#"function f() {
}
"#;

const APPLY_JSX_QUOTE_STYLE_BEFORE: &str = r#"
<div
bar="foo"
baz={"foo"}
/>"#;

const APPLY_JSX_QUOTE_STYLE_AFTER: &str = r#"<div bar='foo' baz={"foo"} />;
"#;

const APPLY_QUOTE_STYLE_BEFORE: &str = r#"
let a = "something";
let b = {
Expand Down Expand Up @@ -538,6 +547,50 @@ fn applies_custom_configuration_over_config_file_issue_3175_v2() {
));
}

#[test]
fn applies_custom_jsx_quote_style() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let file_path = Path::new("file.js");
fs.insert(file_path.into(), APPLY_JSX_QUOTE_STYLE_BEFORE.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[
("format"),
("--jsx-quote-style"),
("single"),
("--quote-properties"),
("preserve"),
("--write"),
file_path.as_os_str().to_str().unwrap(),
]),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

let mut file = fs
.open(file_path)
.expect("formatting target file was removed by the CLI");

let mut content = String::new();
file.read_to_string(&mut content)
.expect("failed to read file from memory FS");

assert_eq!(content, APPLY_JSX_QUOTE_STYLE_AFTER);

drop(file);
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"applies_custom_jsx_quote_style",
fs,
console,
result,
));
}

#[test]
fn applies_custom_quote_style() {
let mut fs = MemoryFileSystem::default();
Expand Down
1 change: 1 addition & 0 deletions crates/rome_cli/tests/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub const CONFIG_ALL_FIELDS: &str = r#"{
"globals": ["$"],
"formatter": {
"quoteStyle": "double",
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `fix.js`

```js
function _13_1_3_fun(arguments) { }

```

# Emitted Messages

```block
Fixed 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Available options:
--line-width <NUMBER> What's the max width of a line. Defaults to 80.
--quote-style <double|single> The style for quotes. Defaults to double.
--jsx-quote-style <double|single> The style for JSX quotes. Defaults to double.
--quote-properties <preserve|as-needed> When properties in objects are quoted. Defaults to
asNeeded.
--trailing-comma <all|es5|none> Print trailing commas wherever possible in multi-line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Available options:
--line-width <NUMBER> What's the max width of a line. Defaults to 80.
--quote-style <double|single> The style for quotes. Defaults to double.
--jsx-quote-style <double|single> The style for JSX quotes. Defaults to double.
--quote-properties <preserve|as-needed> When properties in objects are quoted. Defaults to
asNeeded.
--trailing-comma <all|es5|none> Print trailing commas wherever possible in multi-line
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `file.js`

```js
<div bar='foo' baz={"foo"} />;

```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```


Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Available options:
--line-width <NUMBER> What's the max width of a line. Defaults to 80.
--quote-style <double|single> The style for quotes. Defaults to double.
--jsx-quote-style <double|single> The style for JSX quotes. Defaults to double.
--quote-properties <preserve|as-needed> When properties in objects are quoted. Defaults to
asNeeded.
--trailing-comma <all|es5|none> Print trailing commas wherever possible in multi-line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ expression: content
"globals": ["$"],
"formatter": {
"quoteStyle": "double",
"jsxQuoteStyle": "double",
"quoteProperties": "asNeeded"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::semantic_services::Semantic;
use rome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_js_semantic::{AllBindingWriteReferencesIter, Reference, ReferencesExtensions};
use rome_js_syntax::{AnyJsBindingPattern, AnyJsFormalParameter, AnyJsParameter};
use rome_js_syntax::{AnyJsBinding, AnyJsBindingPattern, AnyJsFormalParameter, AnyJsParameter};
use rome_rowan::AstNode;

declare_rule! {
Expand Down Expand Up @@ -71,10 +71,10 @@ impl Rule for NoParameterAssign {
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let param = ctx.query();
let model = ctx.model();
if let Some(binding) = binding_of(param) {
if let Some(binding) = binding.as_any_js_binding() {
return binding.all_writes(model);
}
if let Some(AnyJsBindingPattern::AnyJsBinding(AnyJsBinding::JsIdentifierBinding(binding))) =
binding_of(param)
{
return binding.all_writes(model);
}
// Empty iterator that conforms to `AllBindingWriteReferencesIter` type.
std::iter::successors(None, |_| None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@
"function foo(a) { for ([a.b] of []); }",
"function foo(a) { a.b &&= c; }",
"function foo(a) { a.b.c ||= d; }",
"function foo(a) { a[b] ??= c; }"
"function foo(a) { a[b] ??= c; }",
"function foo(arguments) { }"
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/rome_js_analyze/tests/spec_tests.rs
assertion_line: 91
expression: valid.jsonc
---
# Input
Expand Down Expand Up @@ -298,4 +297,9 @@ function foo(a) { a.b.c ||= d; }
function foo(a) { a[b] ??= c; }
```
# Input
```js
function foo(arguments) { }
```
14 changes: 14 additions & 0 deletions crates/rome_js_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ pub struct JsFormatOptions {
/// The style for quotes. Defaults to double.
quote_style: QuoteStyle,

/// The style for JSX quotes. Defaults to double.
jsx_quote_style: QuoteStyle,

/// When properties in objects are quoted. Defaults to as-needed.
quote_properties: QuoteProperties,

Expand All @@ -164,6 +167,7 @@ impl JsFormatOptions {
indent_style: IndentStyle::default(),
line_width: LineWidth::default(),
quote_style: QuoteStyle::default(),
jsx_quote_style: QuoteStyle::default(),
quote_properties: QuoteProperties::default(),
trailing_comma: TrailingComma::default(),
semicolons: Semicolons::default(),
Expand All @@ -185,6 +189,11 @@ impl JsFormatOptions {
self
}

pub fn with_jsx_quote_style(mut self, jsx_quote_style: QuoteStyle) -> Self {
self.jsx_quote_style = jsx_quote_style;
self
}

pub fn with_quote_properties(mut self, quote_properties: QuoteProperties) -> Self {
self.quote_properties = quote_properties;
self
Expand All @@ -204,6 +213,10 @@ impl JsFormatOptions {
self.quote_style
}

pub fn jsx_quote_style(&self) -> QuoteStyle {
self.jsx_quote_style
}

pub fn quote_properties(&self) -> QuoteProperties {
self.quote_properties
}
Expand Down Expand Up @@ -247,6 +260,7 @@ impl fmt::Display for JsFormatOptions {
writeln!(f, "Indent style: {}", self.indent_style)?;
writeln!(f, "Line width: {}", self.line_width.value())?;
writeln!(f, "Quote style: {}", self.quote_style)?;
writeln!(f, "JSX quote style: {}", self.jsx_quote_style)?;
writeln!(f, "Quote properties: {}", self.quote_properties)?;
writeln!(f, "Trailing comma: {}", self.trailing_comma)?;
writeln!(f, "Semicolons: {}", self.semicolons)
Expand Down
5 changes: 4 additions & 1 deletion crates/rome_js_formatter/src/utils/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ impl<'token> FormatLiteralStringToken<'token> {
JS_STRING_LITERAL | JSX_STRING_LITERAL
));

let chosen_quote_style = options.quote_style();
let chosen_quote_style = match token.kind() {
JSX_STRING_LITERAL => options.jsx_quote_style(),
_ => options.quote_style(),
};
let chosen_quote_properties = options.quote_properties();

let mut string_cleaner =
Expand Down
7 changes: 7 additions & 0 deletions crates/rome_js_formatter/tests/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ pub struct JsSerializableFormatOptions {
/// The style for quotes. Defaults to double.
pub quote_style: Option<JsSerializableQuoteStyle>,

/// The style for JSX quotes. Defaults to double.
pub jsx_quote_style: Option<JsSerializableQuoteStyle>,

/// When properties in objects are quoted. Defaults to as-needed.
pub quote_properties: Option<JsSerializableQuoteProperties>,

Expand All @@ -180,6 +183,10 @@ impl JsSerializableFormatOptions {
.and_then(|width| LineWidth::try_from(width).ok())
.unwrap_or_default(),
)
.with_jsx_quote_style(
self.jsx_quote_style
.map_or_else(|| QuoteStyle::Double, |value| value.into()),
)
.with_quote_style(
self.quote_style
.map_or_else(|| QuoteStyle::Double, |value| value.into()),
Expand Down
16 changes: 11 additions & 5 deletions crates/rome_js_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rome_formatter_test::check_reformat::CheckReformat;
use rome_js_formatter::context::{JsFormatOptions, Semicolons};
use rome_js_formatter::context::{JsFormatOptions, QuoteStyle, Semicolons};
use rome_js_formatter::format_node;
use rome_js_parser::parse;
use rome_js_syntax::JsFileSource;
Expand All @@ -13,13 +13,19 @@ mod language {
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"
const foo = @deco class {
//
};
(
<div
attr=''
atrr={''}
/>
)
"#;
let syntax = JsFileSource::tsx();
let tree = parse(src, syntax);
let options = JsFormatOptions::new(syntax).with_semicolons(Semicolons::AsNeeded);
let options = JsFormatOptions::new(syntax)
.with_semicolons(Semicolons::AsNeeded)
.with_quote_style(QuoteStyle::Double)
.with_jsx_quote_style(QuoteStyle::Single);
let result = format_node(options.clone(), &tree.syntax())
.unwrap()
.print()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
info:
test_file: js/module/array/array_nested.js
info: js/module/array/array_nested.js
---

# Input
Expand Down Expand Up @@ -62,6 +61,7 @@ let o1 = [{ a, b }, { a, b, c }];
Indent style: Tab
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
info:
test_file: js/module/array/binding_pattern.js
info: js/module/array/binding_pattern.js
---

# Input
Expand All @@ -24,6 +23,7 @@ let [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,...cccccccccc
Indent style: Tab
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
Expand Down
Loading

0 comments on commit 029c4a2

Please sign in to comment.