Skip to content

Commit

Permalink
Consistently quote Sass modules strings (#11461)
Browse files Browse the repository at this point in the history
* 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 <aosukeke@gmail.com>
  • Loading branch information
niksy and sosukesuzuki committed Sep 9, 2021
1 parent ce1bb16 commit 1c4da1e
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
16 changes: 16 additions & 0 deletions changelog_unreleased/scss/11461.md
@@ -0,0 +1,16 @@
#### Consistently quote Sass modules strings (#11461 by @niksy)

<!-- prettier-ignore -->
```scss
// Input
@use "sass:math";
@forward "list";

// Prettier stable
@use "sass:math";
@forward "list";

// Prettier main
@use 'sass:math';
@forward 'list';
```
3 changes: 2 additions & 1 deletion src/language-css/parser-postcss.js
Expand Up @@ -12,6 +12,7 @@ const {
isSCSSNestedPropertyNode,
isSCSSVariable,
stringifyNode,
isModuleRuleName,
} = require("./utils.js");
const { locStart, locEnd } = require("./loc.js");
const { calculateLoc, replaceQuotesInInlineComments } = require("./loc.js");
Expand Down Expand Up @@ -525,7 +526,7 @@ function parseNestedCSS(node, options) {
return node;
}

if (lowercasedName === "import") {
if (isModuleRuleName(lowercasedName)) {
node.import = true;
delete node.filename;
node.params = parseValue(params, options);
Expand Down
6 changes: 6 additions & 0 deletions src/language-css/utils.js
Expand Up @@ -28,6 +28,7 @@ const colorAdjusterFunctions = new Set([
"hwb",
"hwba",
]);
const moduleRuleNames = new Set(["import", "use", "forward"]);

function getAncestorCounter(path, typeOrTypes) {
const types = Array.isArray(typeOrTypes) ? typeOrTypes : [typeOrTypes];
Expand Down Expand Up @@ -479,6 +480,10 @@ function isAtWordPlaceholderNode(node) {
);
}

function isModuleRuleName(name) {
return moduleRuleNames.has(name);
}

module.exports = {
getAncestorCounter,
getAncestorNode,
Expand Down Expand Up @@ -533,4 +538,5 @@ module.exports = {
lastLineHasInlineComment,
stringifyNode,
isAtWordPlaceholderNode,
isModuleRuleName,
};
104 changes: 104 additions & 0 deletions tests/format/scss/quotes/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,104 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`quotes.scss - {"singleQuote":true} format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 80
singleQuote: true
| printWidth
=====================================input======================================
@use "library";
@use "library" with (
$black: #222,
$border-radius: 0.1rem
$font-family: "Helvetica, sans-serif"
);
@use "library" as *;
@use "library" as f;
@use "sass:map";
@forward "library";
@forward "library" show border, $border-color;
@forward "library" hide gradient;
@forward "library" as btn-*;
=====================================output=====================================
@use 'library';
@use 'library' with
($black: #222, $border-radius: 0.1rem $font-family: 'Helvetica, sans-serif');
@use 'library' as *;
@use 'library' as f;
@use 'sass:map';
@forward 'library';
@forward 'library' show border, $border-color;
@forward 'library' hide gradient;
@forward 'library' as btn- *;
================================================================================
`;

exports[`quotes.scss format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 80
| printWidth
=====================================input======================================
@use "library";
@use "library" with (
$black: #222,
$border-radius: 0.1rem
$font-family: "Helvetica, sans-serif"
);
@use "library" as *;
@use "library" as f;
@use "sass:map";
@forward "library";
@forward "library" show border, $border-color;
@forward "library" hide gradient;
@forward "library" as btn-*;
=====================================output=====================================
@use "library";
@use "library" with
($black: #222, $border-radius: 0.1rem $font-family: "Helvetica, sans-serif");
@use "library" as *;
@use "library" as f;
@use "sass:map";
@forward "library";
@forward "library" show border, $border-color;
@forward "library" hide gradient;
@forward "library" as btn- *;
================================================================================
`;
2 changes: 2 additions & 0 deletions tests/format/scss/quotes/jsfmt.spec.js
@@ -0,0 +1,2 @@
run_spec(__dirname, ["scss"]);
run_spec(__dirname, ["scss"], { singleQuote: true });
21 changes: 21 additions & 0 deletions tests/format/scss/quotes/quotes.scss
@@ -0,0 +1,21 @@
@use "library";

@use "library" with (
$black: #222,
$border-radius: 0.1rem
$font-family: "Helvetica, sans-serif"
);

@use "library" as *;

@use "library" as f;

@use "sass:map";

@forward "library";

@forward "library" show border, $border-color;

@forward "library" hide gradient;

@forward "library" as btn-*;

0 comments on commit 1c4da1e

Please sign in to comment.