|
1 | 1 | import {createPlugin, utils} from 'stylelint';
|
2 | 2 | import {basename} from 'path';
|
3 |
| -import {Node} from './stylelint-postcss-types'; |
4 | 3 |
|
5 | 4 | const isStandardSyntaxRule = require('stylelint/lib/utils/isStandardSyntaxRule');
|
6 | 5 | const isStandardSyntaxSelector = require('stylelint/lib/utils/isStandardSyntaxSelector');
|
@@ -37,43 +36,38 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
|
37 | 36 | }
|
38 | 37 |
|
39 | 38 | root.walkRules(rule => {
|
40 |
| - if ( |
41 |
| - rule.parent.type === 'rule' && |
42 |
| - isStandardSyntaxRule(rule) && |
43 |
| - isStandardSyntaxSelector(rule.selector) && |
44 |
| - // Using the ampersand at the beginning is fine, anything else can cause issues in themes. |
45 |
| - rule.selector.indexOf('&') > 0) { |
46 |
| - |
47 |
| - const mixinName = getClosestMixinName(rule); |
48 |
| - |
49 |
| - // Skip rules inside private mixins. |
50 |
| - if (!mixinName || !mixinName.startsWith('_')) { |
51 |
| - utils.report({ |
52 |
| - result, |
53 |
| - ruleName, |
54 |
| - message: messages.expected(), |
55 |
| - node: rule |
56 |
| - }); |
57 |
| - } |
| 39 | + if (rule.parent.type === 'rule' && |
| 40 | + isStandardSyntaxRule(rule) && |
| 41 | + isStandardSyntaxSelector(rule.selector) && |
| 42 | + hasInvalidAmpersandUsage(rule.selector)) { |
| 43 | + utils.report({ |
| 44 | + result, |
| 45 | + ruleName, |
| 46 | + message: messages.expected(), |
| 47 | + node: rule |
| 48 | + }); |
58 | 49 | }
|
59 | 50 | });
|
60 | 51 | };
|
| 52 | +}); |
| 53 | + |
| 54 | +function hasInvalidAmpersandUsage(selector: string): boolean { |
| 55 | + const parts = selector.split(',').map(part => part.trim()); |
61 | 56 |
|
62 |
| - /** Walks up the AST and finds the name of the closest mixin. */ |
63 |
| - function getClosestMixinName(node: Node): string | undefined { |
64 |
| - let parent = node.parent; |
| 57 | + for (const part of parts) { |
| 58 | + let index = part.indexOf('&'); |
65 | 59 |
|
66 |
| - while (parent) { |
67 |
| - if (parent.type === 'atrule' && parent.name === 'mixin') { |
68 |
| - return parent.params; |
| 60 | + while (index > -1) { |
| 61 | + if (index > 0) { |
| 62 | + return true; |
69 | 63 | }
|
70 | 64 |
|
71 |
| - parent = parent.parent; |
| 65 | + index = part.indexOf('&', index + 1); |
72 | 66 | }
|
73 |
| - |
74 |
| - return undefined; |
75 | 67 | }
|
76 |
| -}); |
| 68 | + |
| 69 | + return false; |
| 70 | +} |
77 | 71 |
|
78 | 72 | plugin.ruleName = ruleName;
|
79 | 73 | plugin.messages = messages;
|
|
0 commit comments