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