]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-inline-comments.js
2 * @fileoverview Enforces or disallows inline comments.
7 const astUtils
= require("./utils/ast-utils");
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
18 description
: "disallow inline comments after code",
20 url
: "https://eslint.org/docs/rules/no-inline-comments"
31 additionalProperties
: false
36 unexpectedInlineComment
: "Unexpected comment inline with code."
41 const sourceCode
= context
.getSourceCode();
42 const options
= context
.options
[0];
43 let customIgnoreRegExp
;
45 if (options
&& options
.ignorePattern
) {
46 customIgnoreRegExp
= new RegExp(options
.ignorePattern
, "u");
50 * Will check that comments are not on lines starting with or ending with code
51 * @param {ASTNode} node The comment node to check
55 function testCodeAroundComment(node
) {
57 const startLine
= String(sourceCode
.lines
[node
.loc
.start
.line
- 1]),
58 endLine
= String(sourceCode
.lines
[node
.loc
.end
.line
- 1]),
59 preamble
= startLine
.slice(0, node
.loc
.start
.column
).trim(),
60 postamble
= endLine
.slice(node
.loc
.end
.column
).trim(),
61 isPreambleEmpty
= !preamble
,
62 isPostambleEmpty
= !postamble
;
64 // Nothing on both sides
65 if (isPreambleEmpty
&& isPostambleEmpty
) {
69 // Matches the ignore pattern
70 if (customIgnoreRegExp
&& customIgnoreRegExp
.test(node
.value
)) {
76 (isPreambleEmpty
|| preamble
=== "{") &&
77 (isPostambleEmpty
|| postamble
=== "}")
79 const enclosingNode
= sourceCode
.getNodeByRangeIndex(node
.range
[0]);
81 if (enclosingNode
&& enclosingNode
.type
=== "JSXEmptyExpression") {
86 // Don't report ESLint directive comments
87 if (astUtils
.isDirectiveComment(node
)) {
93 messageId
: "unexpectedInlineComment"
97 //--------------------------------------------------------------------------
99 //--------------------------------------------------------------------------
103 sourceCode
.getAllComments()
104 .filter(token
=> token
.type
!== "Shebang")
105 .forEach(testCodeAroundComment
);