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