3 const isWhitespace = require("../../utils/isWhitespace");
4 const report = require("../../utils/report");
5 const ruleMessages = require("../../utils/ruleMessages");
6 const validateOptions = require("../../utils/validateOptions");
8 const ruleName = "comment-whitespace-inside";
10 const messages = ruleMessages(ruleName, {
11 expectedOpening: 'Expected whitespace after "/*"',
12 rejectedOpening: 'Unexpected whitespace after "/*"',
13 expectedClosing: 'Expected whitespace before "*/"',
14 rejectedClosing: 'Unexpected whitespace before "*/"'
17 const rule = function(expectation) {
18 return function(root, result) {
19 const validOptions = validateOptions(result, ruleName, {
21 possible: ["always", "never"]
27 root.walkComments(function(comment) {
28 if (comment.raws.inline || comment.inline) {
32 const rawComment = comment.toString();
33 const firstFourChars = rawComment.substr(0, 4);
35 // Return early if sourcemap or copyright comment
36 if (/^\/\*[#!]\s/.test(firstFourChars)) {
40 const leftMatches = rawComment.match(/(^\/\*+)(\s)?/);
41 const rightMatches = rawComment.match(/(\s)?(\*+\/)$/);
42 const opener = leftMatches[1];
43 const leftSpace = leftMatches[2] || "";
44 const rightSpace = rightMatches[1] || "";
45 const closer = rightMatches[2];
47 if (expectation === "never" && leftSpace !== "") {
48 complain(messages.rejectedOpening, opener.length);
51 if (expectation === "always" && !isWhitespace(leftSpace)) {
52 complain(messages.expectedOpening, opener.length);
55 if (expectation === "never" && rightSpace !== "") {
57 messages.rejectedClosing,
58 comment.toString().length - closer.length - 1
62 if (expectation === "always" && !isWhitespace(rightSpace)) {
64 messages.expectedClosing,
65 comment.toString().length - closer.length - 1
69 function complain(message, index) {
82 rule.ruleName = ruleName;
83 rule.messages = messages;
84 module.exports = rule;