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

fix: keep quotes in the case of keys that are octal literals #4309

Merged
merged 3 commits into from
Mar 20, 2023
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
11 changes: 8 additions & 3 deletions crates/rome_js_formatter/src/utils/string_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ impl<'token> LiteralStringNormaliser<'token> {

let mut has_seen_number = false;
text_to_check.chars().enumerate().all(|(index, c)| {
if index == 0 && c.is_numeric() {
if index == 0 && c.is_ascii_digit() {
// We can't remove quotes if the member is octal literals.
if c == '0' && text_to_check.len() > 1 {
return false;
}

// In TypeScript, numbers like members have different meaning from numbers.
// Hence, if we see a number, we bail straightaway
if file_source == SourceFileKind::TypeScript {
Expand All @@ -258,9 +263,9 @@ impl<'token> LiteralStringNormaliser<'token> {

let is_eligible_character = if has_seen_number {
// as we've seen a number, now eligible characters can only contain numbers
c.is_numeric()
c.is_ascii_digit()
} else {
c.is_alphanumeric()
c.is_alphabetic() || c.is_ascii_digit()
};
is_eligible_character || matches!(c, '_' | '$')
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const x = {
8: 'yay',
01: 'one',
'0': 'zero',
'010': 'oh no',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
info: js/module/object/octal_literals_key.js
---

# Input

```js
const x = {
8: 'yay',
01: 'one',
'0': 'zero',
'010': 'oh no',
};

```


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

# Outputs

## Output 1

-----
Indent style: Tab
Line width: 80
Quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
-----

```js
const x = {
8: "yay",
01: "one",
0: "zero",
"010": "oh no",
};
```


Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,8 @@ let { "_$_ff$_morning_not_quotes": test, "_$_ff$_morning_yes_quotes_@": test } =
let { "_$_$_%": test } = value;

let { "0197": test, "3n": test, "3p": test, "p9": test } = value;

const x = {
'¾¾¾¾': 'test1',
'①': 'test2',
};
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/string/properties_quotes.js
info: js/module/string/properties_quotes.js
---

# Input
Expand Down Expand Up @@ -46,6 +45,11 @@ let { "_$_$_%": test } = value;

let { "0197": test, "3n": test, "3p": test, "p9": test } = value;

const x = {
'¾¾¾¾': 'test1',
'①': 'test2',
};

```


Expand Down Expand Up @@ -103,7 +107,12 @@ let { _$_ff$_morning_not_quotes: test, "_$_ff$_morning_yes_quotes_@": test } =

let { "_$_$_%": test } = value;

let { 0197: test, "3n": test, "3p": test, p9: test } = value;
let { "0197": test, "3n": test, "3p": test, p9: test } = value;

const x = {
"¾¾¾¾": "test1",
"①": "test2",
};
```

## Output 2
Expand Down Expand Up @@ -156,7 +165,12 @@ let { _$_ff$_morning_not_quotes: test, '_$_ff$_morning_yes_quotes_@': test } =

let { '_$_$_%': test } = value;

let { 0197: test, '3n': test, '3p': test, p9: test } = value;
let { '0197': test, '3n': test, '3p': test, p9: test } = value;

const x = {
'¾¾¾¾': 'test1',
'①': 'test2',
};
```

## Output 3
Expand Down Expand Up @@ -210,6 +224,11 @@ let { "_$_ff$_morning_not_quotes": test, "_$_ff$_morning_yes_quotes_@": test } =
let { "_$_$_%": test } = value;

let { "0197": test, "3n": test, "3p": test, "p9": test } = value;

const x = {
"¾¾¾¾": "test1",
"①": "test2",
};
```