Skip to content

Update postcss-less to v3#6981

Merged
thorn0 merged 32 commits intoprettier:nextfrom
fisker:update-postcss-less-3
Mar 9, 2020
Merged

Update postcss-less to v3#6981
thorn0 merged 32 commits intoprettier:nextfrom
fisker:update-postcss-less-3

Conversation

@fisker
Copy link
Copy Markdown
Member

@fisker fisker commented Nov 17, 2019

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@KeepDetachedRuleset();

&: EXTEND(.Keep ALL);
&:EXTEND(.Keep ALL);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Anyone know what is this? space matters?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 EXTEND does not appear to be valid Less. So it’s a weird test case. &:extend(.Keep ALL) seems to be valid syntax, while &: extend(.Keep ALL) is a syntax error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

what output is expect?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

My guess is that the input should be changed to &:extend(.Keep ALL) and the output should be the same (&:extend(.Keep ALL)).

@lydell
Copy link
Copy Markdown
Member

lydell commented Nov 17, 2019

Also feel free to switch to the next branch if Node.js 4 is giving you trouble – we should not waste more time on that old version.

@KeepDetachedRuleset();

&:EXTEND(.Keep ALL);
&:extend(.Keep ALL);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should keep EXTEND as is

.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;
}

@KeepDetachedRuleset();

&: EXTEND(.Keep ALL);
&:extend(.Keep ALL);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Comment thread scripts/build/babel-plugins/replace-array-includes-with-indexof.js Outdated
@fisker fisker mentioned this pull request Nov 17, 2019
4 tasks
@fisker
Copy link
Copy Markdown
Member Author

fisker commented Nov 17, 2019

ready for review

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Nov 17, 2019

each is still not handled correctly, but at least it's not a parser error as it was before (#5653).

  • @ should not be added in front of each
  • whitespace should not be added in front of @{value}
  • anonymous ruleset should not be formatted inline

Prettier pr-6981
Playground link

--parser less

Input:

@selectors: blue, green, red;

each(@selectors, {
  .sel-@{value} {
    a: b;
  }
});

Output:

@selectors: blue, green, red;

@each (@selectors, {.sel- @{value} {a: b;}}) ;

@fisker
Copy link
Copy Markdown
Member Author

fisker commented Nov 17, 2019

looking into this, different at-rule with function: true

@fisker fisker force-pushed the update-postcss-less-3 branch from ace46c2 to 7b4cf56 Compare November 17, 2019 17:25
@fisker
Copy link
Copy Markdown
Member Author

fisker commented Nov 17, 2019

@thorn0 add a test for it, print as it is, maybe we can make it pretty later, I guess ugly is better than broken. this PR only update this module

@fisker
Copy link
Copy Markdown
Member Author

fisker commented Nov 17, 2019

Prettier pr-6981
Playground link

--parser less

Input:

@selectors: blue, green, red;

each(@selectors, {
  .sel-@{value} {
    
a: b;
  }
});

Output:

@selectors: blue, green, red;

each(@selectors, {
  .sel-@{value} {
    
a: b;
  }
});

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Nov 17, 2019

@fisker 👍 I'm okay with whatever you decide as long as the output stays valid. First correctness, then aesthetics.

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Nov 17, 2019

#5420 is not a parser error now, but selectors assigned to a variable (docs) get corrupted by whitespace inserted after colons.

Prettier pr-6981
Playground link

--parser less

Input:

@selector: &:not([type='file']):not([type='checkbox']):not([type='radio']):not([type='range']):not([type='hidden']);

Output:

@selector: &: not([type= "file" ]): not([type= "checkbox" ]):
  not([type= "radio" ]): not([type= "range" ]): not([type= "hidden" ]);

@fisker
Copy link
Copy Markdown
Member Author

fisker commented Nov 18, 2019

variable support selector, maybe we need do a isSelector check, but should do in a separate PR

Copy link
Copy Markdown
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

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

Many problems, we should fix it

: "",
isTemplatePlaceholderNodeWithoutSemiColon ? "" : ";"
]);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Each node.mixin/node.function/node.variable should be:

if (isLessParser) { 
  if (node.mixin) { /* ... */ }
  if (node.function) { /* ... */ }
}

It is better for performance and other parsers can potentially have same properties

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

will do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add isLessParser check here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In fact we can't use options.parser === "less", since we has parser=css also use less parser, so I use options.parser !== "scss" in this commit

07010de

What do you think? or maybe options.parser === "less" || options.parser === "csss"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 css parser. What about change the options.parser value change when we switch on other syntax?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Maybe we can do this later in a seperate PR, shall we?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 isLessParser, it is allow to fix problem in one places

Comment thread src/language-css/printer-postcss.js Outdated
" {",
node.selector &&
node.selector.type === "selector-unknown" &&
node.selector.value.endsWith("\n")
Copy link
Copy Markdown
Member

@alexander-akait alexander-akait Nov 18, 2019

Choose a reason for hiding this comment

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

Is is bad idea use node.selector.value.endsWith("\n")

Comment on lines +123 to +133
if (node.raws.inline || node.inline) {
const needBreakAfter = !(
node.source.input.css.split("\n")[node.source.end.line] || ""
).trim();
return concat([
"//",
node.raws.left + rawText,
needBreakAfter ? "\n" : ""
]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

"\n"? We should use prettier builders
node.source.input.css should be never used

this place is very bad, need rewrite this

Comment thread src/language-css/parser-postcss.js
Comment thread src/language-css/parser-postcss.js Outdated
// last line has inline comment
if (/\/\//.test(selector.split("\n").pop())) {
value += "\n";
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why? Using \n break code for developers who using CRLF

Comment thread src/language-css/parser-postcss.js Outdated

// Less variable
if (node.variable) {
delete node.params;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

variable, use value property

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Comment thread src/language-css/parser-postcss.js Outdated
Comment on lines +425 to +433
// Missing whitespace between variable and colon
if (
["page", "nest"].indexOf(node.name) === -1 &&
node.params &&
node.params[0] === ":"
) {
node.variable = true;
node.value = parseValue(node.params.slice(1));
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why?

Comment thread src/language-css/parser-postcss.js Outdated

if (node.type === "css-atrule") {
// mixin
if (node.mixin) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

isLessParser && node.mixin

Comment thread src/language-css/parser-postcss.js
Comment thread src/language-css/parser-postcss.js Outdated
}

// Whitespace between variable and colon
if (node.name.indexOf(":") !== -1 && !node.params) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should improve this check, Scss allow to use : in interpolations and can't have params, so this invalid

@alexander-akait
Copy link
Copy Markdown
Member

Please reduce using regexps, it is always bad, if you don't have enough information on node, please open issue in postcss-less instead using hacks

@fisker
Copy link
Copy Markdown
Member Author

fisker commented Nov 18, 2019

@evilebottnawi

https://github.com/shellscape/postcss-less#variables

Note: LESS variables are strictly parsed - a colon (:) must immediately follow a variable name.

postcss-less stop parsing

@foo : bar;

what do we do?

@alexander-akait
Copy link
Copy Markdown
Member

alexander-akait commented Nov 18, 2019

@fisker if less support that, we should open issue and fix problem in parser

@fisker
Copy link
Copy Markdown
Member Author

fisker commented Nov 18, 2019

@evilebottnawi shellscape/postcss-less#101 (comment) It was fixed in 2, but changed again in 3 with breaking change shellscape/postcss-less#113 , I don't think they are going to parse it

@alexander-akait
Copy link
Copy Markdown
Member

@fisker we should investigate how Less interpreted this, if parser don't parse that propertly we can't update parser until it will be fixed

@lydell
Copy link
Copy Markdown
Member

lydell commented Nov 18, 2019

I'm no Less expert, but it seems to be valid on http://lesscss.org/less-preview/

@foo : bar;

a {
  color: @foo;
}

becomes:

a {
  color: bar;
}

@alexander-akait
Copy link
Copy Markdown
Member

alexander-akait commented Nov 19, 2019

@fisker it is mean we can't update parser while it is not fixed

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Nov 19, 2019

But it works!

Prettier pr-6981
Playground link

--parser less

Input:

@foo : bar;

Output:

@foo: bar;

@thorn0 thorn0 force-pushed the update-postcss-less-3 branch from 0ed4d5c to 73e7ba7 Compare February 27, 2020 12:08
@thorn0 thorn0 merged commit acff41e into prettier:next Mar 9, 2020
thorn0 added a commit to thorn0/prettier that referenced this pull request Mar 10, 2020
thorn0 added a commit to thorn0/prettier that referenced this pull request Mar 10, 2020
thorn0 added a commit that referenced this pull request Mar 10, 2020
@fisker fisker deleted the update-postcss-less-3 branch March 17, 2020 02:51
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants