1 |
|
2 |
|
3 | 'use strict';
|
4 |
|
5 | const _ = require('lodash');
|
6 |
|
7 | const isWhitespace = require('../../utils/isWhitespace');
|
8 | const report = require('../../utils/report');
|
9 | const ruleMessages = require('../../utils/ruleMessages');
|
10 | const validateOptions = require('../../utils/validateOptions');
|
11 |
|
12 | const ruleName = 'comment-whitespace-inside';
|
13 |
|
14 | const messages = ruleMessages(ruleName, {
|
15 | expectedOpening: 'Expected whitespace after "/*"',
|
16 | rejectedOpening: 'Unexpected whitespace after "/*"',
|
17 | expectedClosing: 'Expected whitespace before "*/"',
|
18 | rejectedClosing: 'Unexpected whitespace before "*/"',
|
19 | });
|
20 |
|
21 | function rule(expectation, options, context) {
|
22 | return function (root, result) {
|
23 | const validOptions = validateOptions(result, ruleName, {
|
24 | actual: expectation,
|
25 | possible: ['always', 'never'],
|
26 | });
|
27 |
|
28 | if (!validOptions) {
|
29 | return;
|
30 | }
|
31 |
|
32 | root.walkComments((comment) => {
|
33 | if (comment.raws.inline || comment.inline) {
|
34 | return;
|
35 | }
|
36 |
|
37 | const rawComment = comment.toString();
|
38 | const firstFourChars = rawComment.substr(0, 4);
|
39 |
|
40 |
|
41 | if (/^\/\*[#!]\s/.test(firstFourChars)) {
|
42 | return;
|
43 | }
|
44 |
|
45 | const leftMatches = rawComment.match(/(^\/\*+)(\s)?/);
|
46 | const rightMatches = rawComment.match(/(\s)?(\*+\/)$/);
|
47 | const opener = leftMatches[1];
|
48 | const leftSpace = leftMatches[2] || '';
|
49 | const rightSpace = rightMatches[1] || '';
|
50 | const closer = rightMatches[2];
|
51 |
|
52 | if (expectation === 'never' && leftSpace !== '') {
|
53 | complain(messages.rejectedOpening, opener.length);
|
54 | }
|
55 |
|
56 | if (expectation === 'always' && !isWhitespace(leftSpace)) {
|
57 | complain(messages.expectedOpening, opener.length);
|
58 | }
|
59 |
|
60 | if (expectation === 'never' && rightSpace !== '') {
|
61 | complain(messages.rejectedClosing, comment.toString().length - closer.length - 1);
|
62 | }
|
63 |
|
64 | if (expectation === 'always' && !isWhitespace(rightSpace)) {
|
65 | complain(messages.expectedClosing, comment.toString().length - closer.length - 1);
|
66 | }
|
67 |
|
68 | function addWhitespaceBefore(comment) {
|
69 | if (comment.text.startsWith('*')) {
|
70 | comment.text = comment.text.replace(/^(\*+)/, `$1 `);
|
71 | } else {
|
72 | comment.raws.left = ' ';
|
73 | }
|
74 | }
|
75 |
|
76 | function addWhitespaceAfter(comment) {
|
77 | if (_.last(comment.text) === '*') {
|
78 | comment.text = comment.text.replace(/(\*+)$/, ` $1`);
|
79 | } else {
|
80 | comment.raws.right = ' ';
|
81 | }
|
82 | }
|
83 |
|
84 | function complain(message, index) {
|
85 | if (context.fix) {
|
86 | if (expectation === 'never') {
|
87 | comment.raws.left = '';
|
88 | comment.raws.right = '';
|
89 | comment.text = comment.text.replace(/^(\*+)(\s+)?/, '$1').replace(/(\s+)?(\*+)$/, '$2');
|
90 | } else {
|
91 | if (!leftSpace) {
|
92 | addWhitespaceBefore(comment);
|
93 | }
|
94 |
|
95 | if (!rightSpace) {
|
96 | addWhitespaceAfter(comment);
|
97 | }
|
98 | }
|
99 |
|
100 | return;
|
101 | }
|
102 |
|
103 | report({
|
104 | message,
|
105 | index,
|
106 | result,
|
107 | ruleName,
|
108 | node: comment,
|
109 | });
|
110 | }
|
111 | });
|
112 | };
|
113 | }
|
114 |
|
115 | rule.ruleName = ruleName;
|
116 | rule.messages = messages;
|
117 | module.exports = rule;
|