1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const blockString = require('../../utils/blockString');
|
6 | const hasBlock = require('../../utils/hasBlock');
|
7 | const rawNodeString = require('../../utils/rawNodeString');
|
8 | const report = require('../../utils/report');
|
9 | const ruleMessages = require('../../utils/ruleMessages');
|
10 | const validateOptions = require('../../utils/validateOptions');
|
11 | const whitespaceChecker = require('../../utils/whitespaceChecker');
|
12 |
|
13 | const ruleName = 'block-closing-brace-space-after';
|
14 |
|
15 | const messages = ruleMessages(ruleName, {
|
16 | expectedAfter: () => 'Expected single space after "}"',
|
17 | rejectedAfter: () => 'Unexpected whitespace after "}"',
|
18 | expectedAfterSingleLine: () => 'Expected single space after "}" of a single-line block',
|
19 | rejectedAfterSingleLine: () => 'Unexpected whitespace after "}" of a single-line block',
|
20 | expectedAfterMultiLine: () => 'Expected single space after "}" of a multi-line block',
|
21 | rejectedAfterMultiLine: () => 'Unexpected whitespace after "}" of a multi-line block',
|
22 | });
|
23 |
|
24 | function rule(expectation) {
|
25 | const checker = whitespaceChecker('space', expectation, messages);
|
26 |
|
27 | return function (root, result) {
|
28 | const validOptions = validateOptions(result, ruleName, {
|
29 | actual: expectation,
|
30 | possible: [
|
31 | 'always',
|
32 | 'never',
|
33 | 'always-single-line',
|
34 | 'never-single-line',
|
35 | 'always-multi-line',
|
36 | 'never-multi-line',
|
37 | ],
|
38 | });
|
39 |
|
40 | if (!validOptions) {
|
41 | return;
|
42 | }
|
43 |
|
44 |
|
45 | root.walkRules(check);
|
46 | root.walkAtRules(check);
|
47 |
|
48 | function check(statement) {
|
49 | const nextNode = statement.next();
|
50 |
|
51 | if (!nextNode) {
|
52 | return;
|
53 | }
|
54 |
|
55 | if (!hasBlock(statement)) {
|
56 | return;
|
57 | }
|
58 |
|
59 | let reportIndex = statement.toString().length;
|
60 | let source = rawNodeString(nextNode);
|
61 |
|
62 |
|
63 | if (source && source.startsWith(';')) {
|
64 | source = source.slice(1);
|
65 | reportIndex++;
|
66 | }
|
67 |
|
68 | checker.after({
|
69 | source,
|
70 | index: -1,
|
71 | lineCheckStr: blockString(statement),
|
72 | err: (msg) => {
|
73 | report({
|
74 | message: msg,
|
75 | node: statement,
|
76 | index: reportIndex,
|
77 | result,
|
78 | ruleName,
|
79 | });
|
80 | },
|
81 | });
|
82 | }
|
83 | };
|
84 | }
|
85 |
|
86 | rule.ruleName = ruleName;
|
87 | rule.messages = messages;
|
88 | module.exports = rule;
|