UNPKG

2.25 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const blockString = require('../../utils/blockString');
6const hasBlock = require('../../utils/hasBlock');
7const rawNodeString = require('../../utils/rawNodeString');
8const report = require('../../utils/report');
9const ruleMessages = require('../../utils/ruleMessages');
10const validateOptions = require('../../utils/validateOptions');
11const whitespaceChecker = require('../../utils/whitespaceChecker');
12
13const ruleName = 'block-closing-brace-space-after';
14
15const 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
24function 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 // Check both kinds of statements: rules and at-rules
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 // Skip a semicolon at the beginning, if any
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
86rule.ruleName = ruleName;
87rule.messages = messages;
88module.exports = rule;