-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Update postcss-less to v3
#6981
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
Changes from all commits
6fe6109
1160388
3f7f2f2
86fccab
c73d9f4
52fca3b
0a9724f
e5a2adc
1de072b
a9b0a40
482946a
3a385ec
9f05b86
d43be21
f7d65ea
0fb8884
a230e3a
4c88599
8117419
b88d2ed
c803206
d4fbcf8
d8cd76c
9a6f525
1e0424d
0783475
55e864b
ec35e5b
fc77d17
42930a7
717e6dd
73e7ba7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ const createError = require("../common/parser-create-error"); | |
| const parseFrontMatter = require("../utils/front-matter"); | ||
| const lineColumnToIndex = require("../utils/line-column-to-index"); | ||
| const { hasPragma } = require("./pragma"); | ||
| const { isSCSS, isSCSSNestedPropertyNode } = require("./utils"); | ||
| const { isLessParser, isSCSS, isSCSSNestedPropertyNode } = require("./utils"); | ||
|
|
||
| function parseValueNodes(nodes) { | ||
| let parenGroup = { | ||
|
|
@@ -199,10 +199,10 @@ function parseSelector(selector) { | |
| // the content of the comment as selectors which turns it into complete | ||
| // garbage. Better to print the whole selector as-is and not try to parse | ||
| // and reformat it. | ||
| if (selector.match(/\/\/|\/\*/)) { | ||
| if (/\/\/|\/\*/.test(selector)) { | ||
| return { | ||
| type: "selector-unknown", | ||
| value: selector.replace(/^ +/, "").replace(/ +$/, "") | ||
| value: selector.trim() | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -283,16 +283,6 @@ function parseNestedCSS(node, options) { | |
| node.raws.selector = selector; | ||
| } | ||
|
|
||
| // [email protected] parse `custom-selector` as `css-decl` | ||
| if ( | ||
| options.parser === "css" && | ||
| node.type === "css-decl" && | ||
| node.prop === "@custom-selector" | ||
| ) { | ||
| selector = node.value; | ||
| node.raws.value = selector; | ||
| } | ||
|
|
||
| let value = ""; | ||
|
|
||
| if (typeof node.value === "string") { | ||
|
|
@@ -385,6 +375,64 @@ function parseNestedCSS(node, options) { | |
| node.value = parseValue(value); | ||
| } | ||
|
|
||
| if (node.type === "css-atrule") { | ||
| if (isLessParser(options)) { | ||
| // mixin | ||
| if (node.mixin) { | ||
| const source = | ||
| node.raws.identifier + | ||
| node.name + | ||
| node.raws.afterName + | ||
| node.raws.params; | ||
| node.selector = parseSelector(source); | ||
| delete node.params; | ||
| return node; | ||
| } | ||
|
|
||
| // function | ||
| if (node.function) { | ||
| return node; | ||
| } | ||
| } | ||
|
|
||
| // only css support custom-selector | ||
| if (options.parser === "css" && node.name === "custom-selector") { | ||
| const customSelector = node.params.match(/:--\S+?\s+/)[0].trim(); | ||
| node.customSelector = customSelector; | ||
| node.selector = parseSelector( | ||
| node.params.slice(customSelector.length).trim() | ||
| ); | ||
| delete node.params; | ||
| return node; | ||
| } | ||
|
|
||
| if (isLessParser(options)) { | ||
| // Whitespace between variable and colon | ||
| if (node.name.includes(":") && !node.params) { | ||
| node.variable = true; | ||
| const parts = node.name.split(":"); | ||
| node.name = parts[0]; | ||
| node.value = parseValue(parts.slice(1).join(":")); | ||
| } | ||
|
|
||
| // Missing whitespace between variable and colon | ||
| if ( | ||
| !["page", "nest"].includes(node.name) && | ||
| node.params && | ||
| node.params[0] === ":" | ||
| ) { | ||
| node.variable = true; | ||
| node.value = parseValue(node.params.slice(1)); | ||
| } | ||
|
|
||
| // Less variable | ||
| if (node.variable) { | ||
| delete node.params; | ||
| return node; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (node.type === "css-atrule" && params.length > 0) { | ||
| const { name } = node; | ||
| const lowercasedName = node.name.toLowerCase(); | ||
|
|
@@ -417,6 +465,8 @@ function parseNestedCSS(node, options) { | |
| } | ||
|
|
||
| if (lowercasedName === "import") { | ||
| node.import = true; | ||
| delete node.filename; | ||
|
fisker marked this conversation as resolved.
|
||
| node.params = parseValue(params); | ||
| return node; | ||
| } | ||
|
|
@@ -450,16 +500,6 @@ function parseNestedCSS(node, options) { | |
| return node; | ||
| } | ||
|
|
||
| if (name === "custom-selector") { | ||
| const customSelector = params.match(/:--\S+?\s+/)[0].trim(); | ||
|
|
||
| node.customSelector = customSelector; | ||
| node.selector = parseSelector(params.slice(customSelector.length)); | ||
| delete node.params; | ||
|
|
||
| return node; | ||
| } | ||
|
|
||
| if (["media", "custom-media"].includes(lowercasedName)) { | ||
| if (params.includes("#{")) { | ||
| // Workaround for media at rule with scss interpolation | ||
|
|
@@ -513,13 +553,6 @@ function requireParser(isSCSSParser) { | |
| return require("postcss-scss"); | ||
| } | ||
|
|
||
| // TODO: Remove this hack when this issue is fixed: | ||
| // https://github.com/shellscape/postcss-less/issues/88 | ||
| const LessParser = require("postcss-less/dist/less-parser"); | ||
| LessParser.prototype.atrule = function(...args) { | ||
| return Object.getPrototypeOf(LessParser.prototype).atrule.apply(this, args); | ||
| }; | ||
|
|
||
| return require("postcss-less"); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ const { | |
| isWideKeywords, | ||
| isSCSS, | ||
| isLastNode, | ||
| isLessParser, | ||
| isSCSSControlDirectiveNode, | ||
| isDetachedRulesetDeclarationNode, | ||
| isRelationalOperatorNode, | ||
|
|
@@ -68,7 +69,8 @@ const { | |
| isWordNode, | ||
| isColonNode, | ||
| isMediaAndSupportsKeywords, | ||
| isColorAdjusterFuncNode | ||
| isColorAdjusterFuncNode, | ||
| lastLineHasInlineComment | ||
| } = require("./utils"); | ||
|
|
||
| function shouldPrintComma(options) { | ||
|
|
@@ -115,12 +117,20 @@ function genericPrint(path, options, print) { | |
| options.locStart(node), | ||
| options.locEnd(node) | ||
| ); | ||
|
|
||
| const rawText = node.raws.text || node.text; | ||
| // Workaround a bug where the location is off. | ||
| // https://github.com/postcss/postcss-scss/issues/63 | ||
| if (!text.includes(rawText)) { | ||
| if (node.raws.inline) { | ||
| return concat(["// ", rawText]); | ||
| if (node.raws.inline || node.inline) { | ||
| const needBreakAfter = !( | ||
| node.source.input.css.split(/[\r\n]/)[node.source.end.line] || "" | ||
| ).trim(); | ||
| return concat([ | ||
| "//", | ||
| node.raws.left + rawText, | ||
| needBreakAfter ? line : "" | ||
| ]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can simplify that, it was workaround for less parsers bugs, so maybe they were fixed
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will check this
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #7021 seems fixing this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, let's keep that as is here, and refactor in other PRs |
||
| } | ||
| return concat(["/* ", rawText, " */"]); | ||
| } | ||
|
|
@@ -132,7 +142,12 @@ function genericPrint(path, options, print) { | |
| node.important ? " !important" : "", | ||
| node.nodes | ||
| ? concat([ | ||
| " {", | ||
| node.selector && | ||
| node.selector.type === "selector-unknown" && | ||
| lastLineHasInlineComment(node.selector.value) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| ? line | ||
| : " ", | ||
| "{", | ||
| node.nodes.length > 0 | ||
| ? indent( | ||
| concat([hardline, printNodeSequence(path, options, print)]) | ||
|
|
@@ -189,6 +204,52 @@ function genericPrint(path, options, print) { | |
| } | ||
| case "css-atrule": { | ||
| const parentNode = path.getParentNode(); | ||
| const isTemplatePlaceholderNodeWithoutSemiColon = | ||
| isTemplatePlaceholderNode(node) && | ||
| !parentNode.raws.semicolon && | ||
| options.originalText[options.locEnd(node) - 1] !== ";"; | ||
|
|
||
| if (isLessParser(options)) { | ||
| if (node.mixin) { | ||
| return concat([ | ||
| path.call(print, "selector"), | ||
| node.important ? " !important" : "", | ||
| isTemplatePlaceholderNodeWithoutSemiColon ? "" : ";" | ||
| ]); | ||
| } | ||
|
|
||
| if (node.function) { | ||
| return concat([ | ||
| node.name, | ||
| concat([path.call(print, "params")]), | ||
| isTemplatePlaceholderNodeWithoutSemiColon ? "" : ";" | ||
| ]); | ||
| } | ||
|
|
||
| if (node.variable) { | ||
| return concat([ | ||
| "@", | ||
| node.name, | ||
| ": ", | ||
| node.value ? concat([path.call(print, "value")]) : "", | ||
| node.raws.between.trim() ? node.raws.between.trim() + " " : "", | ||
| node.nodes | ||
| ? concat([ | ||
| "{", | ||
| indent( | ||
| concat([ | ||
| node.nodes.length > 0 ? softline : "", | ||
| printNodeSequence(path, options, print) | ||
| ]) | ||
| ), | ||
| softline, | ||
| "}" | ||
| ]) | ||
| : "", | ||
| isTemplatePlaceholderNodeWithoutSemiColon ? "" : ";" | ||
| ]); | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Each It is better for performance and other parsers can potentially have same properties
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact we can't use What do you think? or maybe
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honestly we should throw error on less syntax when developer set
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can do this later in a seperate PR, shall we?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, don't forget open new issue after merge, anyway let's encapsulate logic in one util function |
||
|
|
||
| return concat([ | ||
| "@", | ||
|
|
@@ -242,9 +303,7 @@ function genericPrint(path, options, print) { | |
| softline, | ||
| "}" | ||
| ]) | ||
| : isTemplatePlaceholderNode(node) && | ||
| !parentNode.raws.semicolon && | ||
| options.originalText[options.locEnd(node) - 1] !== ";" | ||
| : isTemplatePlaceholderNodeWithoutSemiColon | ||
| ? "" | ||
| : ";" | ||
| ]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -217,13 +217,16 @@ $KeepScssVar: val; | |
|
|
||
| @KeepDetachedRuleset(); | ||
|
|
||
| &:extend(.Keep ALL); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should keep .bucket {
tr { // nested ruleset with target selector
color: blue;
}
}
.some-class:extend(.bucket tr) {}-> .bucket tr,
.some-class {
color: blue;
}But: .bucket {
tr { // nested ruleset with target selector
color: blue;
}
}
.some-class:EXTEND(.bucket tr) {}-> .bucket tr {
color: blue;
} |
||
| &:EXTEND(.Keep ALL); | ||
|
|
||
| .Keep; | ||
| .Keep(); | ||
| .Keep(4PX)!IMPORTANT; | ||
| .Keep() when (@Keep=Keep); | ||
| .Keep() when (@Keep=12PX); | ||
| .Keep() when (@Keep="12px"); | ||
| .Keep() when (@Keep="12PX"); | ||
| .Keep() when (@Keep=Keep12PX); | ||
| } | ||
|
|
||
|
|
@@ -308,7 +311,7 @@ svg[viewBox] linearGradient, | |
| --Keep-custom-Prop: red; | ||
| background: Var(--Keep-custom-Prop); | ||
| animation-name: KeepAnimationName; | ||
| important: something !IMPORTANT; | ||
| important: something !important; | ||
| font-family: initial; | ||
| padding: unset; | ||
| border: inherit; | ||
|
|
@@ -364,13 +367,16 @@ $KeepScssVar: val; | |
|
|
||
| @KeepDetachedRuleset(); | ||
|
|
||
| &: EXTEND(.Keep ALL); | ||
| &:extend(.Keep ALL); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| &:EXTEND(.Keep ALL); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Anyone know what is this? space matters?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I added this test case when I added support for lowercasing of stuff back in the day. Testing on http://lesscss.org/less-preview/, uppercase
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what output is expect?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My guess is that the input should be changed to |
||
|
|
||
| .Keep; | ||
| .Keep(); | ||
| .Keep(4px) !important; | ||
| .Keep() when (@Keep=Keep); | ||
| .Keep() when (@Keep=12PX); | ||
| .Keep() when (@Keep=12px); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should keep
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add test for |
||
| .Keep() when (@Keep="12px"); | ||
| .Keep() when (@Keep="12PX"); | ||
| .Keep() when (@Keep=Keep12PX); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.