Skip to content

Commit 7711691

Browse files
committed
feat: support singleQuote
1 parent bbe94d5 commit 7711691

7 files changed

Lines changed: 117 additions & 24 deletions

File tree

src/language-yaml/printer-yaml.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,48 @@ function _print(node, parentNode, path, options, print) {
169169
return concat(["&", node.value]);
170170
case "plain":
171171
return join(hardline, node.value.replace(/\n/g, "\n\n").split("\n"));
172-
case "quoteDouble": // TODO: --single-quote
173-
case "quoteSingle":
174-
return options.originalText.slice(
175-
node.position.start.offset,
176-
node.position.end.offset
172+
case "quoteDouble":
173+
case "quoteSingle": {
174+
const singleQuote = "'";
175+
const doubleQuote = '"';
176+
const originalQuote =
177+
node.type === "quoteDouble" ? doubleQuote : singleQuote;
178+
const raw = options.originalText.slice(
179+
node.position.start.offset + 1,
180+
node.position.end.offset - 1
177181
);
182+
if (
183+
(node.type === "quoteSingle" && raw.includes("\\")) ||
184+
(node.type === "quoteDouble" && /\\[^"]/.test(raw))
185+
) {
186+
// only quoteDouble can use escape chars
187+
// and quoteSingle do not need to escape backslashes
188+
return originalQuote + raw + originalQuote;
189+
} else if (raw.includes(doubleQuote)) {
190+
return (
191+
singleQuote +
192+
(node.type === "quoteDouble"
193+
? // double quote needs to be escaped by backslash in quoteDouble
194+
raw.replace(/\\"/g, doubleQuote)
195+
: raw) +
196+
singleQuote
197+
);
198+
}
199+
200+
if (raw.includes(singleQuote)) {
201+
return (
202+
doubleQuote +
203+
(node.type === "quoteSingle"
204+
? // single quote needs to be escaped by 2 single quotes in quoteSingle
205+
raw.replace(/''/g, singleQuote)
206+
: raw) +
207+
doubleQuote
208+
);
209+
}
210+
211+
const quote = options.singleQuote ? singleQuote : doubleQuote;
212+
return quote + raw + quote;
213+
}
178214
case "blockFolded": // TODO: --prose-wrap
179215
case "blockLiteral": {
180216
const value =
@@ -453,6 +489,10 @@ function clean(node, newNode /*, parent */) {
453489
return null;
454490
}
455491
break;
492+
case "quoteDouble":
493+
case "quoteSingle":
494+
newNode.type = "quote";
495+
break;
456496
}
457497
}
458498
}

tests/markdown/__snapshots__/jsfmt.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ Copy the following config into your \`.pre-commit-config.yaml\` file:
13931393
13941394
\`\`\`yaml
13951395
- repo: https://github.com/prettier/prettier
1396-
sha: '' # Use the sha or tag you want to point at
1396+
sha: "" # Use the sha or tag you want to point at
13971397
hooks:
13981398
- id: prettier
13991399
\`\`\`
@@ -3772,7 +3772,7 @@ YAML:
37723772
\`\`\`yaml
37733773
semi: false
37743774
overrides:
3775-
- files: "*.test.js"
3775+
- files: '*.test.js'
37763776
options:
37773777
semi: true
37783778
\`\`\`

tests/yaml_mapping/__snapshots__/jsfmt.spec.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ exports[`quote-key.yml 1`] = `
295295
'b': 123
296296
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
297297
"a": 123
298-
'b': 123
298+
"b": 123
299299
300300
`;
301301

@@ -304,7 +304,7 @@ exports[`quote-key.yml 2`] = `
304304
'b': 123
305305
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
306306
"a": 123
307-
'b': 123
307+
"b": 123
308308
309309
`;
310310

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`quote.yml 1`] = `
4+
- "123"
5+
- '123'
6+
- "''"
7+
- '""'
8+
- ''''
9+
- "\\"\\""
10+
- '\\n123'
11+
- "\\n123"
12+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13+
- "123"
14+
- "123"
15+
- "''"
16+
- '""'
17+
- "'"
18+
- '""'
19+
- '\\n123'
20+
- "\\n123"
21+
22+
`;
23+
24+
exports[`quote.yml 2`] = `
25+
- "123"
26+
- '123'
27+
- "''"
28+
- '""'
29+
- ''''
30+
- "\\"\\""
31+
- '\\n123'
32+
- "\\n123"
33+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
34+
- '123'
35+
- '123'
36+
- "''"
37+
- '""'
38+
- "'"
39+
- '""'
40+
- '\\n123'
41+
- "\\n123"
42+
43+
`;

tests/yaml_quote/jsfmt.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run_spec(__dirname, ["yaml"]);
2+
run_spec(__dirname, ["yaml"], { singleQuote: true });

tests/yaml_quote/quote.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- "123"
2+
- '123'
3+
- "''"
4+
- '""'
5+
- ''''
6+
- "\"\""
7+
- '\n123'
8+
- "\n123"

tests/yaml_spec/__snapshots__/jsfmt.spec.js.snap

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ baz: 2
295295
exports[`colon-in-double-quoted-string.yml 1`] = `
296296
"foo: bar\\": baz"
297297
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
298-
"foo: bar\\": baz"
298+
'foo: bar": baz'
299299
300300
`;
301301
@@ -719,7 +719,7 @@ exports[`non-specific-tags-on-scalars.yml 1`] = `
719719
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
720720
- plain
721721
- "double quoted"
722-
- 'single quoted'
722+
- "single quoted"
723723
- >
724724
block
725725
- plain again
@@ -1114,7 +1114,7 @@ control: "\\b1998\\t1999\\t2000\\n"
11141114
hex esc: "\\x0d\\x0a is \\r\\n"
11151115
11161116
single: '"Howdy!" he cried.'
1117-
quoted: ' # Not a ''comment''.'
1117+
quoted: " # Not a 'comment'."
11181118
tie-fighter: '|\\-*-/|'
11191119
11201120
`;
@@ -1412,7 +1412,7 @@ exports[`spec-example-5-8-quoted-scalar-indicators.yml 1`] = `
14121412
single: 'text'
14131413
double: "text"
14141414
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1415-
single: 'text'
1415+
single: "text"
14161416
double: "text"
14171417
14181418
`;
@@ -1883,7 +1883,7 @@ exports[`spec-example-7-8-single-quoted-implicit-keys.yml 1`] = `
18831883
'implicit flow key' : value,
18841884
]
18851885
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1886-
'implicit block key': ['implicit flow key': value]
1886+
"implicit block key": ["implicit flow key": value]
18871887
18881888
`;
18891889
@@ -1893,10 +1893,10 @@ exports[`spec-example-7-9-single-quoted-lines.yml 1`] = `
18931893
2nd non-empty
18941894
3rd non-empty '
18951895
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1896-
' 1st non-empty
1896+
" 1st non-empty
18971897
18981898
2nd non-empty
1899-
3rd non-empty '
1899+
3rd non-empty "
19001900
19011901
`;
19021902
@@ -1966,8 +1966,8 @@ single: pair,
19661966
]
19671967
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19681968
["double
1969-
quoted", 'single
1970-
quoted', plain text, [nested], single: pair]
1969+
quoted", "single
1970+
quoted", plain text, [nested], single: pair]
19711971
19721972
`;
19731973
@@ -2054,7 +2054,7 @@ exports[`spec-example-7-23-flow-content.yml 1`] = `
20542054
- [a, b]
20552055
- { a: b }
20562056
- "a"
2057-
- 'b'
2057+
- "b"
20582058
- c
20592059
20602060
`;
@@ -2067,7 +2067,7 @@ exports[`spec-example-7-24-flow-nodes.yml 1`] = `
20672067
- !!str
20682068
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20692069
- !!str "a"
2070-
- 'b'
2070+
- "b"
20712071
- &anchor "c"
20722072
- *anchor
20732073
- !!str
@@ -2726,7 +2726,7 @@ exports[`whitespace-after-scalars-in-flow.yml 1`] = `
27262726
- [ ]
27272727
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27282728
- [a, b, c]
2729-
- { "a": b, c: 'd', e: "f" }
2729+
- { "a": b, c: "d", e: "f" }
27302730
- []
27312731
27322732
`;
@@ -2747,15 +2747,15 @@ top6:
27472747
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27482748
"top1":
27492749
"key1": &alias1 scalar1
2750-
'top2':
2751-
'key2': &alias2 scalar2
2750+
"top2":
2751+
"key2": &alias2 scalar2
27522752
top3: &node3
27532753
*alias1 : scalar3
27542754
top4:
27552755
*alias2 : scalar4
27562756
top5: scalar5
27572757
top6:
2758-
&anchor6 'key6': scalar6
2758+
&anchor6 "key6": scalar6
27592759
27602760
`;
27612761

0 commit comments

Comments
 (0)