blob: e313eac06f01fbec7385d249f2e5ab851bd7c393 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
/**
* @fileoverview Enforces or disallows inline comments.
* @author Greg Cochard
*/
"use strict";
var astUtils = require("../ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow inline comments after code",
category: "Stylistic Issues",
recommended: false
},
schema: []
},
create: function(context) {
var sourceCode = context.getSourceCode();
/**
* Will check that comments are not on lines starting with or ending with code
* @param {ASTNode} node The comment node to check
* @private
* @returns {void}
*/
function testCodeAroundComment(node) {
// Get the whole line and cut it off at the start of the comment
var startLine = String(sourceCode.lines[node.loc.start.line - 1]);
var endLine = String(sourceCode.lines[node.loc.end.line - 1]);
var preamble = startLine.slice(0, node.loc.start.column).trim();
// Also check after the comment
var postamble = endLine.slice(node.loc.end.column).trim();
// Check that this comment isn't an ESLint directive
var isDirective = astUtils.isDirectiveComment(node);
// Should be empty if there was only whitespace around the comment
if (!isDirective && (preamble || postamble)) {
context.report(node, "Unexpected comment inline with code.");
}
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
LineComment: testCodeAroundComment,
BlockComment: testCodeAroundComment
};
}
};
|