1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const _ = require('lodash');
|
6 | const beforeBlockString = require('../../utils/beforeBlockString');
|
7 | const hasBlock = require('../../utils/hasBlock');
|
8 | const hasEmptyBlock = require('../../utils/hasEmptyBlock');
|
9 | const optionsMatches = require('../../utils/optionsMatches');
|
10 | const report = require('../../utils/report');
|
11 | const ruleMessages = require('../../utils/ruleMessages');
|
12 | const validateOptions = require('../../utils/validateOptions');
|
13 |
|
14 | const ruleName = 'block-no-empty';
|
15 |
|
16 | const messages = ruleMessages(ruleName, {
|
17 | rejected: 'Unexpected empty block',
|
18 | });
|
19 |
|
20 | function 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 |
|
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 |
|
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 |
|
81 | rule.ruleName = ruleName;
|
82 | rule.messages = messages;
|
83 | module.exports = rule;
|