UNPKG

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