Skip to content
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

Fix nested property scss syntax false positive #3283

Merged
merged 6 commits into from Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/rules/no-descending-specificity/__tests__/index.js
Expand Up @@ -76,6 +76,9 @@ testRule(rule, {
},
{
code: ":root { --foo: {}; }"
},
{
code: ":root { foo: {}; }"
}
],

Expand Down
5 changes: 3 additions & 2 deletions lib/rules/no-descending-specificity/index.js
Expand Up @@ -3,6 +3,7 @@
const _ = require("lodash");
const findAtRuleContext = require("../../utils/findAtRuleContext");
const isCustomPropertySet = require("../../utils/isCustomPropertySet");
const isNestedProperty = require("../../utils/isNestedProperty");
const keywordSets = require("../../reference/keywordSets");
const nodeContextLookup = require("../../utils/nodeContextLookup");
const parseSelector = require("../../utils/parseSelector");
Expand All @@ -28,8 +29,8 @@ const rule = function(actual) {
const selectorContextLookup = nodeContextLookup();

root.walkRules(rule => {
// Ignore custom property set `--foo: {};`
if (isCustomPropertySet(rule)) {
// Ignore custom property set `--foo: {};` and nested property `foo: {};`
if (isCustomPropertySet(rule) || isNestedProperty(rule)) {
return;
}

Expand Down
26 changes: 26 additions & 0 deletions lib/utils/__tests__/isNestedProperty.test.js
@@ -0,0 +1,26 @@
"use strict";

const isNestedProperty = require("../isNestedProperty");
const postcss = require("postcss");

describe("isNestedProperty", () => {
it("accepts nested property", () => {
return nestedProperty("foo: {};", nestedProperty => {
expect(isNestedProperty(nestedProperty)).toBeTruthy();
});
});

it("rejects not nested property", () => {
return nestedProperty("foo: red;", nestedProperty => {
expect(isNestedProperty(nestedProperty)).toBeFalsy();
});
});
});

function nestedProperty(css, cb) {
return postcss()
.process(css, { from: undefined })
.then(result => {
result.root.walk(cb);
});
}
14 changes: 14 additions & 0 deletions lib/utils/isNestedProperty.js
@@ -0,0 +1,14 @@
/* @flow */
"use strict";

const _ = require("lodash");
const hasBlock = require("../utils/hasBlock");

/**
* Check whether a Node is a nested property
*/
module.exports = function(node /*: Object*/) /*: boolean*/ {
const selector = _.get(node, "raws.selector.raw", node.selector);

return node.type === "rule" && hasBlock(node) && selector.slice(-1) === ":";
};