Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently quote Sass modules strings #11461

Merged
merged 6 commits into from Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions changelog_unreleased/scss/11461.md
@@ -0,0 +1,42 @@
<!--

1. Choose a folder based on which language your PR is for.

- For JavaScript, choose `javascript/` etc.
- For TypeScript specific syntax, choose `typescript/`.
- If your PR applies to multiple languages, such as TypeScript/Flow, choose one folder and mention which languages it applies to.

2. In your chosen folder, create a file with your PR number: `XXXX.md`. For example: `typescript/6728.md`.

3. Copy the content below and paste it in your new file.

4. Fill in a title, the PR number and your user name.

5. Optionally write a description. Many times it’s enough with just sample code.

6. Change ```jsx to your language. For example, ```yaml.

7. Change the `// Input` and `// Prettier` comments to the comment syntax of your language. For example, `# Input`.

8. Choose some nice input example code. Paste it along with the output before and after your PR.

-->

#### Consistently quote Sass modules strings (#11461 by @niksy)

<!-- Optional description if it makes sense. -->

<!-- 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
};
34 changes: 34 additions & 0 deletions tests/format/scss/quotes/__snapshots__/jsfmt.spec.js.snap
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`quotes.scss - {"singleQuote":true} format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 80
singleQuote: true
| printWidth
=====================================input======================================
@use "module";
@forward "module";

=====================================output=====================================
@use 'module';
@forward 'module';

================================================================================
`;

exports[`quotes.scss format 1`] = `
====================================options=====================================
parsers: ["scss"]
printWidth: 80
| printWidth
=====================================input======================================
@use "module";
@forward "module";

=====================================output=====================================
@use "module";
@forward "module";

================================================================================
`;
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 });
2 changes: 2 additions & 0 deletions tests/format/scss/quotes/quotes.scss
@@ -0,0 +1,2 @@
@use "module";
@forward "module";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need more tests here, because we use parser here and it can break output, please add:

@use 'library' with (
  $black: #222,
  $border-radius: 0.1rem
);

ideally even more