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 false positives for parametric mixins in selector-class-pattern #5378

Merged
merged 6 commits into from Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 6 additions & 15 deletions lib/utils/__tests__/isStandardSyntaxRule.test.js
Expand Up @@ -38,8 +38,8 @@ describe('isStandardSyntaxRule', () => {
it('Scss nested properties', () => {
expect(isStandardSyntaxRule(node('foo: {};'))).toBeFalsy();
});
it('called Less class parametric mixin', () => {
expect(isStandardSyntaxRule(lessNode('.mixin-name(@var);'))).toBeFalsy();
it('Less class parametric mixin', () => {
expect(isStandardSyntaxRule(lessNode('.mixin-name(@var) {}'))).toBeFalsy();
});
it('non-outputting parametric Less class mixin definition', () => {
expect(isStandardSyntaxRule(lessNode('.mixin-name() {}'))).toBeFalsy();
Expand All @@ -53,32 +53,23 @@ describe('isStandardSyntaxRule', () => {
it('non-outputting Less ID mixin definition', () => {
expect(isStandardSyntaxRule(lessNode('#mixin-name() {}'))).toBeFalsy();
});
it('called Less ID mixin', () => {
expect(isStandardSyntaxRule(lessNode('#mixin-name;'))).toBeFalsy();
});
it('called namespaced Less mixin (child)', () => {
expect(isStandardSyntaxRule(lessNode('#namespace > .mixin-name;'))).toBeFalsy();
});
it('called namespaced Less mixin (descendant)', () => {
expect(isStandardSyntaxRule(lessNode('#namespace .mixin-name;'))).toBeFalsy();
});
it('called namespaced Less mixin (compound)', () => {
expect(isStandardSyntaxRule(lessNode('#namespace.mixin-name;'))).toBeFalsy();
});
it('less mixin', () => {
expect(
isStandardSyntaxRule(lessNode('.box-shadow(@style, @c) when (iscolor(@c)) {}')),
).toBeFalsy();
});
it('less extend', () => {
expect(isStandardSyntaxRule(lessNode('&:extend(.inline);'))).toBeFalsy();
expect(isStandardSyntaxRule(lessNode('&:extend(.inline) {}'))).toBeFalsy();
});
it('less detached rulesets', () => {
expect(isStandardSyntaxRule(lessNode('@foo: {};'))).toBeFalsy();
});
it('less guarded namespaces', () => {
expect(isStandardSyntaxRule(lessNode('#namespace when (@mode=huge) {}'))).toBeFalsy();
});
it('less parametric mixins', () => {
expect(isStandardSyntaxRule(lessNode('.mixin (@variable: 5) {}'))).toBeFalsy();
});
it('mixin guards', () => {
expect(
isStandardSyntaxRule(lessNode('.mixin (@variable) when (@variable = 10px) {}')),
Expand Down
27 changes: 2 additions & 25 deletions lib/utils/isStandardSyntaxRule.js
Expand Up @@ -22,36 +22,13 @@ module.exports = function (rule) {
return false;
}

// Called Less mixin (e.g. a { .mixin() })
// @ts-expect-error -- TODO TYPES support LESS and SASS types somehow
if (rule.mixin) {
return false;
}

// Less detached rulesets
if (selector.startsWith('@') && selector.endsWith(':')) {
return false;
}

// Ignore Less &:extend rule
if ('extend' in rule && rule.extend) {
return false;
}
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved

// Ignore mixin or &:extend rule
// https://github.com/shellscape/postcss-less/blob/master/lib/less-parser.js#L52
// @ts-expect-error -- TODO TYPES support LESS and SASS types somehow
if (rule.params && rule.params[0]) {
return false;
}

// Non-outputting Less mixin definition (e.g. .mixin() {})
if (selector.endsWith(')') && !selector.includes(':')) {
return false;
}

// Less guards
if (/when\s+(not\s+)*\(/.test(selector)) {
// Less Parametric mixins (e.g. .mixin(@variable: x) {})
if (/\(@.*\)$/.test(selector)) {
return false;
}
ybiquitous marked this conversation as resolved.
Show resolved Hide resolved

Expand Down