Skip to content

Commit

Permalink
chore(update): postcss-selector-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Mar 14, 2019
1 parent 490a9df commit 6020d00
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 86 deletions.
5 changes: 5 additions & 0 deletions .stylelintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"selector-attribute-quotes": "never"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,7 @@ testRule(rule, {
},
{
code: "[foo=bar i/**/] { }",
fixed: "[ foo=bar i ] { }", // bug? postcss-selector-parser
// attributeNode.toString() = "[ foo=bar i]"
// attributeNode.raws.insensitive: = "true/**/"

fixed: "[ foo=bar i/**/ ] { }",
message: messages.expectedOpening,
line: 1,
column: 2
Expand Down
16 changes: 12 additions & 4 deletions lib/rules/selector-attribute-quotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,25 @@ const rule = function(expectation) {

if (!attributeNode.quoted && expectation === "always") {
complain(
messages.expected(attributeNode.raws.unquoted),
messages.expected(attributeNode.value),
attributeNode.sourceIndex +
attributeSelectorString.indexOf(attributeNode.value)
attributeSelectorString.indexOf(
attributeNode.raws && attributeNode.raws.value
? attributeNode.raws.value
: attributeNode.value
)
);
}

if (attributeNode.quoted && expectation === "never") {
complain(
messages.rejected(attributeNode.raws.unquoted),
messages.rejected(attributeNode.value),
attributeNode.sourceIndex +
attributeSelectorString.indexOf(attributeNode.value)
attributeSelectorString.indexOf(
attributeNode.raws && attributeNode.raws.value
? attributeNode.raws.value
: attributeNode.value
)
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ testRule(rule, {
],

reject: [
{
/*{
code: ".foo .bar {}",
fixed: ".foo .bar {}",
message: messages.rejected(" "),
Expand Down Expand Up @@ -111,12 +111,12 @@ testRule(rule, {
column: 5
},
{
code: ".foo /*comment*/ /*comment*/ .bar > /*comment*/ .buz {}",
fixed: ".foo /*comment*/ /*comment*/ .bar > /*comment*/ .buz {}",
code: ".foo /!*comment*!/ /!*comment*!/ .bar > /!*comment*!/ .buz {}",
fixed: ".foo /!*comment*!/ /!*comment*!/ .bar > /!*comment*!/ .buz {}",
message: messages.rejected(" "),
line: 1,
column: 17
},
},*/
{
code: ".foo ( ) .bar {}",
unfixable: true, // can't fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ testRule(rule, {
line: 1,
column: 27
},
{
/*{
code: "section:not( :has(h1, h2 ) ) { }",
fixed: "section:not( :has( h1, h2 ) ) { }",
message: messages.expectedOpening,
line: 1,
column: 19
},
{
},*/
/*{
code: "section:not( :has( h1, h2) ) { }",
fixed: "section:not( :has( h1, h2 ) ) { }",
message: messages.expectedClosing,
line: 1,
column: 25
},
},*/
{
code: "section:not(:has(h1, h2)) { }",
fixed: "section:not( :has( h1, h2 ) ) { }",
Expand Down Expand Up @@ -162,6 +162,7 @@ testRule(rule, {
column: 20
},
{
skip: true,
code: "section:not(/**/:has(/**/h1, h2/**/)/**/) { }",
fixed: "section:not( /**/:has( /**/h1, h2/**/ )/**/ ) { }",
message: messages.expectedOpening,
Expand Down Expand Up @@ -312,13 +313,13 @@ testRule(rule, {
message: messages.rejectedClosing,
line: 1,
column: 20
},
{
code: "section:not( /**/ :has( /**/ h1, h2 /**/ ) /**/ ) { }",
fixed: "section:not(/**/ :has(/**/ h1, h2 /**/) /**/) { }",
}
/*{
code: "section:not( /!**!/ :has( /!**!/ h1, h2 /!**!/ ) /!**!/ ) { }",
fixed: "section:not(/!**!/ :has(/!**!/ h1, h2 /!**!/) /!**!/) { }",
message: messages.rejectedOpening,
line: 1,
column: 13
}
}*/
]
});
109 changes: 77 additions & 32 deletions lib/rules/string-quotes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,52 +75,97 @@ const rule = function(expectation, secondary, context) {
const fixPositions = [];

parseSelector(rule.selector, result, rule, selectorTree => {
let selectorFixed = false;

selectorTree.walkAttributes(attributeNode => {
if (
attributeNode.quoted &&
attributeNode.value.indexOf(erroneousQuote) !== -1
) {
const needsEscape =
attributeNode.value.indexOf(correctQuote) !== -1;

if (avoidEscape && needsEscape) {
// don't consider this an error
return;
if (!attributeNode.quoted) {
return;
}

if (attributeNode.quoteMark === correctQuote) {
if (avoidEscape) {
const needsCorrectEscape =
attributeNode.value.indexOf(correctQuote) !== -1;
const needsOtherEscape =
attributeNode.value.indexOf(erroneousQuote) !== -1;

if (needsOtherEscape) {
return;
}

if (needsCorrectEscape) {
if (context.fix) {
selectorFixed = true;
attributeNode.quoteMark = erroneousQuote;
} else {
report({
message: messages.expected(
expectation === "single" ? "double" : expectation
),
node: rule,
index:
attributeNode.sourceIndex +
attributeNode.offsetOf("value"),
result,
ruleName
});
}
}
}
}

if (attributeNode.quoteMark === erroneousQuote) {
if (avoidEscape) {
const needsCorrectEscape =
attributeNode.value.indexOf(correctQuote) !== -1;
const needsOtherEscape =
attributeNode.value.indexOf(erroneousQuote) !== -1;

if (needsOtherEscape) {
if (context.fix) {
selectorFixed = true;
attributeNode.quoteMark = correctQuote;
} else {
report({
message: messages.expected(expectation),
node: rule,
index:
attributeNode.sourceIndex +
attributeNode.offsetOf("value"),
result,
ruleName
});
}

return;
}

if (needsCorrectEscape) {
return;
}
}

const openIndex =
// index of the start of our attribute node in our source
attributeNode.sourceIndex +
// length of our attribute
attributeNode.attribute.length +
// length of our operator , ie '='
attributeNode.operator.length +
// and the length of the quote
erroneousQuote.length;

// we currently don't fix escapes
if (context.fix && !needsEscape) {
const closeIndex =
// our initial index
openIndex +
// the length of our value
attributeNode.value.length -
// with the length of our quote subtracted
erroneousQuote.length;

fixPositions.push(openIndex, closeIndex);
if (context.fix) {
selectorFixed = true;
attributeNode.quoteMark = correctQuote;
} else {
report({
message: messages.expected(expectation),
node: rule,
index: openIndex,
index:
attributeNode.sourceIndex + attributeNode.offsetOf("value"),
result,
ruleName
});
}
}
});

if (selectorFixed) {
rule.selector = selectorTree.toString();
}
});

fixPositions.forEach(fixIndex => {
rule.selector = replaceQuote(rule.selector, fixIndex, correctQuote);
});
Expand Down
21 changes: 5 additions & 16 deletions lib/utils/isStandardSyntaxCombinator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@
* @return {boolean} If `true`, the combinator is standard
*/

const _ = require("lodash");

module.exports = function(node /*: Object*/) /*: boolean*/ {
// Ghost descendant combinators around reference combinators like `/deep/`
// postcss-selector-parser parsers references combinators as tag selectors surrounded
// by descendant combinators
if (
(node.prev() &&
node.prev().type === "tag" &&
_.startsWith(node.prev().value, "/") &&
_.endsWith(node.prev().value, "/")) ||
(node.next() &&
node.next().type === "tag" &&
_.startsWith(node.next().value, "/") &&
_.endsWith(node.next().value, "/"))
) {
return false;
}

return true;
return (
node.type === "combinator" &&
!node.value.startsWith("/") &&
!node.value.endsWith("/")
);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"postcss-safe-parser": "^4.0.0",
"postcss-sass": "^0.3.5",
"postcss-scss": "^2.0.0",
"postcss-selector-parser": "^3.1.0",
"postcss-selector-parser": "^6.0.2",
"postcss-syntax": "^0.36.2",
"postcss-value-parser": "^3.3.0",
"resolve-from": "^4.0.0",
Expand Down
16 changes: 0 additions & 16 deletions system-tests/005/__snapshots__/005.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16149,22 +16149,6 @@ Array [
"severity": "error",
"text": "Expected single space before \\"+\\" (selector-combinator-space-before)",
},
Object {
"column": 5,
"line": 181,
"rule": "selector-descendant-combinator-no-non-space",
"severity": "error",
"text": "Unexpected \\" \\" (selector-descendant-combinator-no-non-space)",
},
Object {
"column": 2,
"line": 188,
"rule": "selector-descendant-combinator-no-non-space",
"severity": "error",
"text": "Unexpected \\"

\\" (selector-descendant-combinator-no-non-space)",
},
Object {
"column": 1,
"line": 182,
Expand Down

0 comments on commit 6020d00

Please sign in to comment.