|
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,44 +36,25 @@ 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 | };
|
61 |
| - |
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; |
65 |
| - |
66 |
| - while (parent) { |
67 |
| - if (parent.type === 'atrule' && parent.name === 'mixin') { |
68 |
| - return parent.params; |
69 |
| - } |
70 |
| - |
71 |
| - parent = parent.parent; |
72 |
| - } |
73 |
| - |
74 |
| - return undefined; |
75 |
| - } |
76 | 52 | });
|
77 | 53 |
|
| 54 | +function hasInvalidAmpersandUsage(selector: string): boolean { |
| 55 | + return selector.split(',').some(part => part.trim().indexOf('&', 1) > -1); |
| 56 | +} |
| 57 | + |
78 | 58 | plugin.ruleName = ruleName;
|
79 | 59 | plugin.messages = messages;
|
80 | 60 | export default plugin;
|
0 commit comments