UNPKG

1.78 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const _ = require('lodash');
6const beforeBlockString = require('../../utils/beforeBlockString');
7const hasBlock = require('../../utils/hasBlock');
8const hasEmptyBlock = require('../../utils/hasEmptyBlock');
9const optionsMatches = require('../../utils/optionsMatches');
10const report = require('../../utils/report');
11const ruleMessages = require('../../utils/ruleMessages');
12const validateOptions = require('../../utils/validateOptions');
13
14const ruleName = 'block-no-empty';
15
16const messages = ruleMessages(ruleName, {
17 rejected: 'Unexpected empty block',
18});
19
20function rule(primary, options = {}) {
21 return (root, result) => {
22 const validOptions = validateOptions(
23 result,
24 ruleName,
25 {
26 actual: primary,
27 possible: _.isBoolean,
28 },
29 {
30 actual: options,
31 possible: {
32 ignore: ['comments'],
33 },
34 optional: true,
35 },
36 );
37
38 if (!validOptions) {
39 return;
40 }
41
42 const ignoreComments = optionsMatches(options, 'ignore', 'comments');
43
44 // Check both kinds of statements: rules and at-rules
45 root.walkRules(check);
46 root.walkAtRules(check);
47
48 function check(statement) {
49 if (!hasEmptyBlock(statement) && !ignoreComments) {
50 return;
51 }
52
53 if (!hasBlock(statement)) {
54 return;
55 }
56
57 const hasCommentsOnly = statement.nodes.every((node) => node.type === 'comment');
58
59 if (!hasCommentsOnly) {
60 return;
61 }
62
63 let index = beforeBlockString(statement, { noRawBefore: true }).length;
64
65 // For empty blocks when using SugarSS parser
66 if (statement.raws.between === undefined) {
67 index--;
68 }
69
70 report({
71 message: messages.rejected,
72 node: statement,
73 index,
74 result,
75 ruleName,
76 });
77 }
78 };
79}
80
81rule.ruleName = ruleName;
82rule.messages = messages;
83module.exports = rule;