Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow lint rule that contains @media (#134) #140

Merged
merged 1 commit into from
Mar 25, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions lib/get-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ function hasNoDeclarations(node) {
return hasChildNodes(node) && node.every(child => child.type !== 'decl');
}

function hasOnlyExtends(node) {
return (
hasChildNodes(node) &&
node.every(child => child.type === 'atrule' && child.name === 'extend')
);
function hasOnlyAllowedAtRules(node) {
let containsAllowed = false;
let containsNotAllowed = false;

if (hasChildNodes(node)) {
node.each(child => {
if (
child.type === 'atrule' &&
(child.name === 'extend' || child.name === 'media')
) {
containsAllowed = true;
} else if (child.type !== 'comment') {
containsNotAllowed = true;
}
});
}
return containsAllowed && !containsNotAllowed;
}

function getComponentRootRule(node) {
Expand All @@ -44,8 +56,8 @@ function unWrapSelectors(parent, rule) {

function getSelectors(rule) {
// Skip validation on rules with no declarations
// as these don't exist after rules have been unwrapped (unless the selector contains only a @extend)
if (hasNoDeclarations(rule) && !hasOnlyExtends(rule)) {
// as these don't exist after rules have been unwrapped (unless the selector contains only a @extend or @media)
if (hasNoDeclarations(rule) && !hasOnlyAllowedAtRules(rule)) {
return [];
}

Expand Down
22 changes: 21 additions & 1 deletion test/nesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('getSelectors', () => {
componentRoot.append(media);

assert.deepEqual(getSelectors(rule), ['.Component .Component-d']);
assert.deepEqual(getSelectors(componentRoot), []);
assert.deepEqual(getSelectors(componentRoot), ['.Component']);
});

it('should return a selector for a ruleset with declarations and nested media query', () => {
Expand All @@ -165,5 +165,25 @@ describe('getSelectors', () => {
assert.deepEqual(getSelectors(rule), ['.Component .Component-d']);
assert.deepEqual(getSelectors(componentRoot), ['.Component']);
});

it('should return selector if selector contains @media without other declarations', () => {
const rule = postcss.rule({selector: '.Component-d'});
const media = postcss.atRule({name: 'media'});
rule.append(media);
componentRoot.append(rule);

assert.deepEqual(getSelectors(rule), ['.Component .Component-d']);
});

it('should return selector if selector contains @media and comment', () => {
const rule = postcss.rule({selector: '.Component-d'});
const comment = postcss.comment({text: 'comment'});
const media = postcss.atRule({name: 'media'});
rule.append(comment);
rule.append(media);
componentRoot.append(rule);

assert.deepEqual(getSelectors(rule), ['.Component .Component-d']);
});
});
});