UNPKG

2.99 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const _ = require('lodash');
6
7const isWhitespace = require('../../utils/isWhitespace');
8const report = require('../../utils/report');
9const ruleMessages = require('../../utils/ruleMessages');
10const validateOptions = require('../../utils/validateOptions');
11
12const ruleName = 'comment-whitespace-inside';
13
14const 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
21function 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 // Return early if sourcemap or copyright comment
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
115rule.ruleName = ruleName;
116rule.messages = messages;
117module.exports = rule;