import { utils } from "stylelint";
import { namespace } from "../../utils";

export const ruleName = namespace("comment-no-loud");

export const messages = utils.ruleMessages(ruleName, {
  expected: "Expected // for comments instead of /*"
});

function rule(primary) {
  return (root, result) => {
    const validOptions = utils.validateOptions(result, ruleName, {
      actual: primary
    });

    if (!validOptions) {
      return;
    }

    root.walkComments(comment => {
      if (isLoudComment(comment)) {
        utils.report({
          message: messages.expected,
          node: comment,
          result,
          ruleName
        });
      }
    });
  };
}

function isLoudComment(comment) {
  const regex = new RegExp(/^[ \t\n]*\/\*/);

  return regex.test(comment.source.input.css);
}

export default rule;

import { utils } from "stylelint";
import { namespace } from "../../utils";

export const ruleName = namespace("comment-no-loud");

export const messages = utils.ruleMessages(ruleName, {
  expected: "Expected // for comments instead of /*"
});

function rule(primary) {
  return (root, result) => {
    const validOptions = utils.validateOptions(result, ruleName, {
      actual: primary
    });

    if (!validOptions) {
      return;
    }

    root.walkComments(comment => {
      if (isLoudComment(comment)) {
        utils.report({
          message: messages.expected,
          node: comment,
          result,
          ruleName
        });
      }
    });
  };
}

function isLoudComment(comment) {
  const regex = new RegExp(/^[ \t\n]*\/\*/);

  const splitComment = comment.source.input.css.split("\n");
  const commentFirstLine = splitComment[comment.source.start.line - 1];

  return regex.test(commentFirstLine);
}

export default rule;

Fix comment-no-loud only catching first line
Fix comment-no-loud only catching first line

This changes the approach to looking up comments for the comment-no-loud
rule. This rule had an issue where it would only identify comments if
they were the first node within an SCSS file.

Previously this check matched a regex against `source.input.css` of the
comment node. `source.input.css` is actually the full input of the
source file and not the input of the node (as I imagine was expected
when this was initially authored). This meant that only comments that
were not preceded by any other nodes matched the rule. The tests all
succeeded for this as they all checked against the first node.

This new approach instead takes the source input and splits into an array
based on a `\n` separator. It then looks up the first line of the
comment with `source.start.line` variable. This provides the first
line of the comment input as was given in the source file.

I did also try using `comment.toString()` as a simpler means to get the
original source value, however this approach didn't provide an exact
copy of the original input and instead converted SCSS comments (`//`)
into CSS ones (`/*`) meaning that all SCSS comments were identified as
loud.
