UNPKG

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