2 * @fileoverview Rule to enforce the position of line comments
3 * @author Alberto RodrÃguez
7 const astUtils = require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description: "enforce position of line comments",
19 category: "Stylistic Issues",
21 url: "https://eslint.org/docs/rules/line-comment-position"
28 enum: ["above", "beside"]
34 enum: ["above", "beside"]
39 applyDefaultPatterns: {
42 applyDefaultIgnorePatterns: {
46 additionalProperties: false
52 above: "Expected comment to be above code.",
53 beside: "Expected comment to be beside code."
58 const options = context.options[0];
62 applyDefaultIgnorePatterns = true;
64 if (!options || typeof options === "string") {
65 above = !options || options === "above";
68 above = !options.position || options.position === "above";
69 ignorePattern = options.ignorePattern;
71 if (Object.prototype.hasOwnProperty.call(options, "applyDefaultIgnorePatterns")) {
72 applyDefaultIgnorePatterns = options.applyDefaultIgnorePatterns;
74 applyDefaultIgnorePatterns = options.applyDefaultPatterns !== false;
78 const defaultIgnoreRegExp = astUtils.COMMENTS_IGNORE_PATTERN;
79 const fallThroughRegExp = /^\s*falls?\s?through/u;
80 const customIgnoreRegExp = new RegExp(ignorePattern, "u");
81 const sourceCode = context.getSourceCode();
83 //--------------------------------------------------------------------------
85 //--------------------------------------------------------------------------
89 const comments = sourceCode.getAllComments();
91 comments.filter(token => token.type === "Line").forEach(node => {
92 if (applyDefaultIgnorePatterns && (defaultIgnoreRegExp.test(node.value) || fallThroughRegExp.test(node.value))) {
96 if (ignorePattern && customIgnoreRegExp.test(node.value)) {
100 const previous = sourceCode.getTokenBefore(node, { includeComments: true });
101 const isOnSameLine = previous && previous.loc.end.line === node.loc.start.line;