Skip to content

Commit 1c4da1e

Browse files
niksysosukesuzuki
andauthored
Consistently quote Sass modules strings (#11461)
* Consistently quote Sass modules strings Fixes #11053. * Add module rule name check function * Add more tests for `@use` and `@forward` * Lint changelog * Lint files Co-authored-by: sosukesuzuki <[email protected]>
1 parent ce1bb16 commit 1c4da1e

File tree

6 files changed

+151
-1
lines changed

6 files changed

+151
-1
lines changed

changelog_unreleased/scss/11461.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#### Consistently quote Sass modules strings (#11461 by @niksy)
2+
3+
<!-- prettier-ignore -->
4+
```scss
5+
// Input
6+
@use "sass:math";
7+
@forward "list";
8+
9+
// Prettier stable
10+
@use "sass:math";
11+
@forward "list";
12+
13+
// Prettier main
14+
@use 'sass:math';
15+
@forward 'list';
16+
```

src/language-css/parser-postcss.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
isSCSSNestedPropertyNode,
1313
isSCSSVariable,
1414
stringifyNode,
15+
isModuleRuleName,
1516
} = require("./utils.js");
1617
const { locStart, locEnd } = require("./loc.js");
1718
const { calculateLoc, replaceQuotesInInlineComments } = require("./loc.js");
@@ -525,7 +526,7 @@ function parseNestedCSS(node, options) {
525526
return node;
526527
}
527528

528-
if (lowercasedName === "import") {
529+
if (isModuleRuleName(lowercasedName)) {
529530
node.import = true;
530531
delete node.filename;
531532
node.params = parseValue(params, options);

src/language-css/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const colorAdjusterFunctions = new Set([
2828
"hwb",
2929
"hwba",
3030
]);
31+
const moduleRuleNames = new Set(["import", "use", "forward"]);
3132

3233
function getAncestorCounter(path, typeOrTypes) {
3334
const types = Array.isArray(typeOrTypes) ? typeOrTypes : [typeOrTypes];
@@ -479,6 +480,10 @@ function isAtWordPlaceholderNode(node) {
479480
);
480481
}
481482

483+
function isModuleRuleName(name) {
484+
return moduleRuleNames.has(name);
485+
}
486+
482487
module.exports = {
483488
getAncestorCounter,
484489
getAncestorNode,
@@ -533,4 +538,5 @@ module.exports = {
533538
lastLineHasInlineComment,
534539
stringifyNode,
535540
isAtWordPlaceholderNode,
541+
isModuleRuleName,
536542
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`quotes.scss - {"singleQuote":true} format 1`] = `
4+
====================================options=====================================
5+
parsers: ["scss"]
6+
printWidth: 80
7+
singleQuote: true
8+
| printWidth
9+
=====================================input======================================
10+
@use "library";
11+
12+
@use "library" with (
13+
$black: #222,
14+
$border-radius: 0.1rem
15+
$font-family: "Helvetica, sans-serif"
16+
);
17+
18+
@use "library" as *;
19+
20+
@use "library" as f;
21+
22+
@use "sass:map";
23+
24+
@forward "library";
25+
26+
@forward "library" show border, $border-color;
27+
28+
@forward "library" hide gradient;
29+
30+
@forward "library" as btn-*;
31+
32+
=====================================output=====================================
33+
@use 'library';
34+
35+
@use 'library' with
36+
($black: #222, $border-radius: 0.1rem $font-family: 'Helvetica, sans-serif');
37+
38+
@use 'library' as *;
39+
40+
@use 'library' as f;
41+
42+
@use 'sass:map';
43+
44+
@forward 'library';
45+
46+
@forward 'library' show border, $border-color;
47+
48+
@forward 'library' hide gradient;
49+
50+
@forward 'library' as btn- *;
51+
52+
================================================================================
53+
`;
54+
55+
exports[`quotes.scss format 1`] = `
56+
====================================options=====================================
57+
parsers: ["scss"]
58+
printWidth: 80
59+
| printWidth
60+
=====================================input======================================
61+
@use "library";
62+
63+
@use "library" with (
64+
$black: #222,
65+
$border-radius: 0.1rem
66+
$font-family: "Helvetica, sans-serif"
67+
);
68+
69+
@use "library" as *;
70+
71+
@use "library" as f;
72+
73+
@use "sass:map";
74+
75+
@forward "library";
76+
77+
@forward "library" show border, $border-color;
78+
79+
@forward "library" hide gradient;
80+
81+
@forward "library" as btn-*;
82+
83+
=====================================output=====================================
84+
@use "library";
85+
86+
@use "library" with
87+
($black: #222, $border-radius: 0.1rem $font-family: "Helvetica, sans-serif");
88+
89+
@use "library" as *;
90+
91+
@use "library" as f;
92+
93+
@use "sass:map";
94+
95+
@forward "library";
96+
97+
@forward "library" show border, $border-color;
98+
99+
@forward "library" hide gradient;
100+
101+
@forward "library" as btn- *;
102+
103+
================================================================================
104+
`;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
run_spec(__dirname, ["scss"]);
2+
run_spec(__dirname, ["scss"], { singleQuote: true });

tests/format/scss/quotes/quotes.scss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@use "library";
2+
3+
@use "library" with (
4+
$black: #222,
5+
$border-radius: 0.1rem
6+
$font-family: "Helvetica, sans-serif"
7+
);
8+
9+
@use "library" as *;
10+
11+
@use "library" as f;
12+
13+
@use "sass:map";
14+
15+
@forward "library";
16+
17+
@forward "library" show border, $border-color;
18+
19+
@forward "library" hide gradient;
20+
21+
@forward "library" as btn-*;

0 commit comments

Comments
 (0)