refactor: at rules parse (#4023)

master
Evilebot Tnawi 2018-02-22 12:49:03 +03:00 committed by GitHub
parent 335fe638f5
commit 7b457fd3b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 56 deletions

View File

@ -310,26 +310,10 @@ function parseNestedCSS(node) {
return node; return node;
} }
if ( const name = node.name;
node.name === "charset" || const lowercasedName = node.name.toLowerCase();
node.name.toLowerCase() === "counter-style" ||
node.name.toLowerCase().endsWith("keyframes") ||
node.name.toLowerCase() === "page" ||
node.name.toLowerCase() === "font-feature-values" ||
node.name.toLowerCase() === "stylistic" ||
node.name.toLowerCase() === "historical-forms" ||
node.name.toLowerCase() === "styleset" ||
node.name.toLowerCase() === "character-variant" ||
node.name.toLowerCase() === "swash" ||
node.name.toLowerCase() === "ornaments" ||
node.name.toLowerCase() === "annotation"
) {
node.params = params;
return node; if (name === "warn" || name === "error") {
}
if (node.name === "warn" || node.name === "error") {
node.params = { node.params = {
type: "media-unknown", type: "media-unknown",
value: params value: params
@ -338,14 +322,14 @@ function parseNestedCSS(node) {
return node; return node;
} }
if (node.name === "extend" || node.name === "nest") { if (name === "extend" || name === "nest") {
node.selector = parseSelector(node.params); node.selector = parseSelector(params);
delete node.params; delete node.params;
return node; return node;
} }
if (node.name === "at-root") { if (name === "at-root") {
if (/^\(\s*(without|with)\s*:[\s\S]+\)$/.test(params)) { if (/^\(\s*(without|with)\s*:[\s\S]+\)$/.test(params)) {
node.params = parseMediaQuery(params); node.params = parseMediaQuery(params);
} else { } else {
@ -357,18 +341,20 @@ function parseNestedCSS(node) {
} }
if ( if (
node.name === "if" || [
node.name === "else" || "if",
node.name === "for" || "else",
node.name === "each" || "for",
node.name === "while" || "each",
node.name === "debug" || "while",
node.name === "mixin" || "debug",
node.name === "include" || "mixin",
node.name === "function" || "include",
node.name === "return" || "function",
node.name === "define-mixin" || "return",
node.name === "add-mixin" "define-mixin",
"add-mixin"
].indexOf(name) !== -1
) { ) {
// Remove unnecessary spaces in SCSS variable arguments // Remove unnecessary spaces in SCSS variable arguments
params = params.replace(/(\$\S+?)\s+?\.\.\./, "$1..."); params = params.replace(/(\$\S+?)\s+?\.\.\./, "$1...");
@ -381,15 +367,7 @@ function parseNestedCSS(node) {
return node; return node;
} }
if (params.includes("#{")) { if (name === "custom-selector") {
// Workaround for media at rule with scss interpolation
return {
type: "media-unknown",
value: params
};
}
if (/^custom-selector$/i.test(node.name)) {
const customSelector = params.match(/:--\S+?\s+/)[0].trim(); const customSelector = params.match(/:--\S+?\s+/)[0].trim();
node.customSelector = customSelector; node.customSelector = customSelector;
@ -399,7 +377,25 @@ function parseNestedCSS(node) {
return node; return node;
} }
node.params = parseMediaQuery(params); if (
["namespace", "import", "media", "supports", "custom-media"].indexOf(
lowercasedName
) !== -1
) {
if (params.includes("#{")) {
// Workaround for media at rule with scss interpolation
return {
type: "media-unknown",
value: params
};
}
node.params = parseMediaQuery(params);
return node;
}
node.params = params;
return node; return node;
} }

View File

@ -127,10 +127,7 @@ function genericPrint(path, options, print) {
const hasParams = const hasParams =
node.params && node.params &&
!(node.params.type === "media-query-list" && node.params.value === ""); !(node.params.type === "media-query-list" && node.params.value === "");
const isDetachedRulesetCall = const isDetachedRulesetCall = hasParams && /^\(\s*\)$/.test(node.params);
hasParams &&
node.params.type === "media-query-list" &&
/^\(\s*\)$/.test(node.params.value);
const hasParensAround = const hasParensAround =
node.value && node.value &&
node.value.group.group.type === "value-paren_group" && node.value.group.group.type === "value-paren_group" &&

View File

@ -176,11 +176,11 @@ one 'two' three {}
@import "foo.css" screen and (orientation: landscape); @import "foo.css" screen and (orientation: landscape);
@foo "one"; @foo "one";
@foo "one"; @foo 'one';
@foo "one" two "three"; @foo "one" two 'three';
@foo ("one"); @foo ("one");
@foo ("one"); @foo ('one');
@foo ("one" two "three"); @foo ("one" two 'three');
one "two" three { one "two" three {
} }
@ -364,12 +364,12 @@ one 'two' three {}
@import 'foo.css' screen and (orientation: landscape); @import 'foo.css' screen and (orientation: landscape);
@import 'foo.css' screen and (orientation: landscape); @import 'foo.css' screen and (orientation: landscape);
@foo "one";
@foo 'one'; @foo 'one';
@foo 'one'; @foo "one" two 'three';
@foo 'one' two 'three'; @foo ("one");
@foo ('one'); @foo ('one');
@foo ('one'); @foo ("one" two 'three');
@foo ('one' two 'three');
one 'two' three { one 'two' three {
} }