forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce bundle size of
parser-postcss.js
(prettier#12204)
* Extract utils * Move `utils.js` to `utils/index.js`
- Loading branch information
1 parent
54df16c
commit a0cc4ab
Showing
12 changed files
with
134 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"use strict"; | ||
|
||
const isNonEmptyArray = require("../../utils/is-non-empty-array.js"); | ||
|
||
function hasSCSSInterpolation(groupList) { | ||
if (isNonEmptyArray(groupList)) { | ||
for (let i = groupList.length - 1; i > 0; i--) { | ||
// If we find `#{`, return true. | ||
if ( | ||
groupList[i].type === "word" && | ||
groupList[i].value === "{" && | ||
groupList[i - 1].type === "word" && | ||
groupList[i - 1].value.endsWith("#") | ||
) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
module.exports = hasSCSSInterpolation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use strict"; | ||
|
||
const isNonEmptyArray = require("../../utils/is-non-empty-array.js"); | ||
|
||
function hasStringOrFunction(groupList) { | ||
if (isNonEmptyArray(groupList)) { | ||
for (let i = 0; i < groupList.length; i++) { | ||
if (groupList[i].type === "string" || groupList[i].type === "func") { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
module.exports = hasStringOrFunction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"use strict"; | ||
|
||
function isSCSSNestedPropertyNode(node) { | ||
/* istanbul ignore next */ | ||
if (!node.selector) { | ||
return false; | ||
} | ||
|
||
return node.selector | ||
.replace(/\/\*.*?\*\//, "") | ||
.replace(/\/\/.*\n/, "") | ||
.trim() | ||
.endsWith(":"); | ||
} | ||
|
||
module.exports = isSCSSNestedPropertyNode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"use strict"; | ||
|
||
function isSCSSVariable(node) { | ||
return Boolean(node && node.type === "word" && node.value.startsWith("$")); | ||
} | ||
|
||
module.exports = isSCSSVariable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
// TODO: only check `less` when we don't use `less` to parse `css` | ||
function isLessParser(options) { | ||
return options.parser === "css" || options.parser === "less"; | ||
} | ||
|
||
module.exports = isLessParser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"use strict"; | ||
|
||
const moduleRuleNames = new Set(["import", "use", "forward"]); | ||
|
||
function isModuleRuleName(name) { | ||
return moduleRuleNames.has(name); | ||
} | ||
|
||
module.exports = isModuleRuleName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"use strict"; | ||
|
||
function isSCSS(parser, text) { | ||
const hasExplicitParserChoice = parser === "less" || parser === "scss"; | ||
const IS_POSSIBLY_SCSS = /(?:\w\s*:\s*[^:}]+|#){|@import[^\n]+(?:url|,)/; | ||
return hasExplicitParserChoice | ||
? parser === "scss" | ||
: IS_POSSIBLY_SCSS.test(text); | ||
} | ||
|
||
module.exports = isSCSS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"use strict"; | ||
|
||
function stringifyNode(node) { | ||
if (node.groups) { | ||
const open = node.open && node.open.value ? node.open.value : ""; | ||
const groups = node.groups.reduce( | ||
(previousValue, currentValue, index) => | ||
previousValue + | ||
stringifyNode(currentValue) + | ||
(node.groups[0].type === "comma_group" && | ||
index !== node.groups.length - 1 | ||
? "," | ||
: ""), | ||
"" | ||
); | ||
const close = node.close && node.close.value ? node.close.value : ""; | ||
|
||
return open + groups + close; | ||
} | ||
|
||
const before = node.raws && node.raws.before ? node.raws.before : ""; | ||
const quote = node.raws && node.raws.quote ? node.raws.quote : ""; | ||
const atword = node.type === "atword" ? "@" : ""; | ||
const value = node.value ? node.value : ""; | ||
const unit = node.unit ? node.unit : ""; | ||
const group = node.group ? stringifyNode(node.group) : ""; | ||
const after = node.raws && node.raws.after ? node.raws.after : ""; | ||
|
||
return before + quote + atword + value + quote + unit + group + after; | ||
} | ||
|
||
module.exports = stringifyNode; |