Skip to content

Commit 01f986b

Browse files
authored
Make isSCSS* function stricter (#9093)
1 parent 7d69167 commit 01f986b

16 files changed

Lines changed: 1314 additions & 1143 deletions

File tree

changelog_unreleased/css/pr-7933.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#### [BREAKING] Add the pure `css` parser (#7933 by @fisker)
1+
#### [BREAKING] Add the pure `css` parser (#7933, #9092, #9093 by @fisker)
22

33
Previously, when `--parser=css` was passed, Prettier tried to parse the content using `postcss-scss` and `postcss-less`. This caused confusion, and made syntax errors difficult to spot. Now `--parser=css` works only with the vanilla CSS syntax.
44

src/language-css/parser-postcss.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const { hasPragma } = require("./pragma");
66
const {
77
hasSCSSInterpolation,
88
hasStringOrFunction,
9-
isSCSS,
109
isSCSSNestedPropertyNode,
1110
isSCSSVariable,
1211
stringifyNode,
@@ -40,7 +39,7 @@ function parseValueNode(valueNode, options) {
4039
const node = nodes[i];
4140

4241
if (
43-
isSCSS(options.parser, node.value) &&
42+
options.parser === "scss" &&
4443
node.type === "number" &&
4544
node.unit === ".." &&
4645
node.value[node.value.length - 1] === "."
@@ -80,7 +79,8 @@ function parseValueNode(valueNode, options) {
8079
// Stringify if the value parser can't handle the content.
8180
if (
8281
hasSCSSInterpolation(groupList) ||
83-
(!hasStringOrFunction(groupList) && !isSCSSVariable(groupList[0]))
82+
(!hasStringOrFunction(groupList) &&
83+
!isSCSSVariable(groupList[0], options))
8484
) {
8585
const stringifiedContent = stringifyNode({
8686
groups: node.group.groups,
@@ -370,7 +370,7 @@ function parseNestedCSS(node, options) {
370370
}
371371

372372
// Check on SCSS nested property
373-
if (isSCSSNestedPropertyNode(node)) {
373+
if (isSCSSNestedPropertyNode(node, options)) {
374374
node.isSCSSNesterProperty = true;
375375
}
376376

src/language-css/printer-postcss.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const {
3838
insideURLFunctionInImportAtRuleNode,
3939
isKeyframeAtRuleKeywords,
4040
isWideKeywords,
41-
isSCSS,
4241
isLastNode,
4342
isSCSSControlDirectiveNode,
4443
isDetachedRulesetDeclarationNode,
@@ -277,7 +276,7 @@ function genericPrint(path, options, print) {
277276
concat([
278277
" ",
279278
path.call(print, "value"),
280-
isSCSSControlDirectiveNode(node)
279+
isSCSSControlDirectiveNode(node, options)
281280
? hasParensAroundNode(node)
282281
? " "
283282
: line
@@ -289,7 +288,7 @@ function genericPrint(path, options, print) {
289288
: "",
290289
node.nodes
291290
? concat([
292-
isSCSSControlDirectiveNode(node)
291+
isSCSSControlDirectiveNode(node, options)
293292
? ""
294293
: (node.selector &&
295294
!node.selector.nodes &&
@@ -537,7 +536,8 @@ function genericPrint(path, options, print) {
537536
declAncestorProp.startsWith("grid-template"));
538537
const atRuleAncestorNode = getAncestorNode(path, "css-atrule");
539538
const isControlDirective =
540-
atRuleAncestorNode && isSCSSControlDirectiveNode(atRuleAncestorNode);
539+
atRuleAncestorNode &&
540+
isSCSSControlDirectiveNode(atRuleAncestorNode, options);
541541

542542
const printed = path.map(print, "groups");
543543
const parts = [];
@@ -856,7 +856,7 @@ function genericPrint(path, options, print) {
856856
return group(indent(fill(res)));
857857
}
858858

859-
const isSCSSMapItem = isSCSSMapItemNode(path);
859+
const isSCSSMapItem = isSCSSMapItemNode(path, options);
860860

861861
const lastItem = node.groups[node.groups.length - 1];
862862
const isLastItemComment = lastItem && lastItem.type === "value-comment";
@@ -895,7 +895,7 @@ function genericPrint(path, options, print) {
895895
),
896896
ifBreak(
897897
!isLastItemComment &&
898-
isSCSS(options.parser, options.originalText) &&
898+
options.parser === "scss" &&
899899
isSCSSMapItem &&
900900
shouldPrintComma(options)
901901
? ","

src/language-css/utils.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,13 @@ function hasStringOrFunction(groupList) {
8686
return false;
8787
}
8888

89-
function isSCSS(parser, text) {
90-
const hasExplicitParserChoice = parser === "less" || parser === "scss";
91-
const IS_POSSIBLY_SCSS = /(\w\s*:\s*[^:}]+|#){|@import[^\n]+(?:url|,)/;
92-
return hasExplicitParserChoice
93-
? parser === "scss"
94-
: IS_POSSIBLY_SCSS.test(text);
95-
}
96-
97-
function isSCSSVariable(node) {
98-
return !!(node && node.type === "word" && node.value.startsWith("$"));
89+
function isSCSSVariable(node, options) {
90+
return (
91+
options.parser === "scss" &&
92+
node &&
93+
node.type === "word" &&
94+
node.value.startsWith("$")
95+
);
9996
}
10097

10198
function isWideKeywords(value) {
@@ -256,14 +253,19 @@ function isRelationalOperatorNode(node) {
256253
);
257254
}
258255

259-
function isSCSSControlDirectiveNode(node) {
256+
function isSCSSControlDirectiveNode(node, options) {
260257
return (
258+
options.parser === "scss" &&
261259
node.type === "css-atrule" &&
262260
["if", "else", "for", "each", "while"].includes(node.name)
263261
);
264262
}
265263

266-
function isSCSSNestedPropertyNode(node) {
264+
function isSCSSNestedPropertyNode(node, options) {
265+
if (options.parser !== "scss") {
266+
return false;
267+
}
268+
267269
/* istanbul ignore next */
268270
if (!node.selector) {
269271
return false;
@@ -341,7 +343,11 @@ function isKeyValuePairInParenGroupNode(node) {
341343
);
342344
}
343345

344-
function isSCSSMapItemNode(path) {
346+
function isSCSSMapItemNode(path, options) {
347+
if (options.parser !== "scss") {
348+
return false;
349+
}
350+
345351
const node = path.getValue();
346352

347353
// Ignore empty item (i.e. `$key: ()`)
@@ -469,7 +475,6 @@ module.exports = {
469475
insideURLFunctionInImportAtRuleNode,
470476
isKeyframeAtRuleKeywords,
471477
isWideKeywords,
472-
isSCSS,
473478
isSCSSVariable,
474479
isLastNode,
475480
isSCSSControlDirectiveNode,

0 commit comments

Comments
 (0)