UNPKG

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