node/tools/node_modules/eslint/lib/rules/no-inline-comments.js
Rich Trott 6ad12d47f5 tools: update ESLint to 5.3.0
PR-URL: https://github.com/nodejs/node/pull/22134
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
2018-08-07 22:28:45 -07:00

67 lines
2.2 KiB
JavaScript

/**
* @fileoverview Enforces or disallows inline comments.
* @author Greg Cochard
*/
"use strict";
const astUtils = require("../util/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow inline comments after code",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/no-inline-comments"
},
schema: []
},
create(context) {
const 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
const startLine = String(sourceCode.lines[node.loc.start.line - 1]);
const endLine = String(sourceCode.lines[node.loc.end.line - 1]);
const preamble = startLine.slice(0, node.loc.start.column).trim();
// Also check after the comment
const postamble = endLine.slice(node.loc.end.column).trim();
// Check that this comment isn't an ESLint directive
const isDirective = astUtils.isDirectiveComment(node);
// Should be empty if there was only whitespace around the comment
if (!isDirective && (preamble || postamble)) {
context.report({ node, message: "Unexpected comment inline with code." });
}
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
Program() {
const comments = sourceCode.getAllComments();
comments.filter(token => token.type !== "Shebang").forEach(testCodeAroundComment);
}
};
}
};