2 * @fileoverview Enforces or disallows inline comments.
7 const astUtils = require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description: "disallow inline comments after code",
19 category: "Stylistic Issues",
21 url: "https://eslint.org/docs/rules/no-inline-comments"
28 const sourceCode = context.getSourceCode();
31 * Will check that comments are not on lines starting with or ending with code
32 * @param {ASTNode} node The comment node to check
36 function testCodeAroundComment(node) {
38 const startLine = String(sourceCode.lines[node.loc.start.line - 1]),
39 endLine = String(sourceCode.lines[node.loc.end.line - 1]),
40 preamble = startLine.slice(0, node.loc.start.column).trim(),
41 postamble = endLine.slice(node.loc.end.column).trim(),
42 isPreambleEmpty = !preamble,
43 isPostambleEmpty = !postamble;
45 // Nothing on both sides
46 if (isPreambleEmpty && isPostambleEmpty) {
52 (isPreambleEmpty || preamble === "{") &&
53 (isPostambleEmpty || postamble === "}")
55 const enclosingNode = sourceCode.getNodeByRangeIndex(node.range[0]);
57 if (enclosingNode && enclosingNode.type === "JSXEmptyExpression") {
62 // Don't report ESLint directive comments
63 if (astUtils.isDirectiveComment(node)) {
67 context.report({ node, message: "Unexpected comment inline with code." });
70 //--------------------------------------------------------------------------
72 //--------------------------------------------------------------------------
76 const comments = sourceCode.getAllComments();
78 comments.filter(token => token.type !== "Shebang").forEach(testCodeAroundComment);