UNPKG

1.59 kBJavaScriptView Raw
1'use strict';
2
3const _ = require('lodash');
4const isCustomPropertySet = require('../utils/isCustomPropertySet');
5const isStandardSyntaxSelector = require('../utils/isStandardSyntaxSelector');
6
7/**
8 * Check whether a Node is a standard rule
9 *
10 * @param {import('postcss').Rule | import('postcss-less').Rule} rule
11 * @returns {boolean}
12 */
13module.exports = function (rule) {
14 if (rule.type !== 'rule') {
15 return false;
16 }
17
18 // Get full selector
19 const selector = _.get(rule, 'raws.selector.raw', rule.selector);
20
21 if (!isStandardSyntaxSelector(rule.selector)) {
22 return false;
23 }
24
25 // Custom property set (e.g. --custom-property-set: {})
26 if (isCustomPropertySet(rule)) {
27 return false;
28 }
29
30 // Called Less mixin (e.g. a { .mixin() })
31 // @ts-ignore TODO TYPES support LESS and SASS types somehow
32 if (rule.mixin) {
33 return false;
34 }
35
36 // Less detached rulesets
37 if (selector.startsWith('@') && selector.endsWith(':')) {
38 return false;
39 }
40
41 // Ignore Less &:extend rule
42 if ('extend' in rule && rule.extend) {
43 return false;
44 }
45
46 // Ignore mixin or &:extend rule
47 // https://github.com/shellscape/postcss-less/blob/master/lib/less-parser.js#L52
48 // @ts-ignore TODO TYPES support LESS and SASS types somehow
49 if (rule.params && rule.params[0]) {
50 return false;
51 }
52
53 // Non-outputting Less mixin definition (e.g. .mixin() {})
54 if (selector.endsWith(')') && !selector.includes(':')) {
55 return false;
56 }
57
58 // Less guards
59 if (/when\s+(not\s+)*\(/.test(selector)) {
60 return false;
61 }
62
63 // Ignore Scss nested properties
64 if (selector.endsWith(':')) {
65 return false;
66 }
67
68 return true;
69};