Sha256: 814af864f73f38db1843b91b84e1d88edb2ae2db63734946598c728da430053f

Contents?: true

Size: 1.97 KB

Versions: 45

Compression:

Stored size: 1.97 KB

Contents

/**
 * @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

        };
    }
};

Version data entries

45 entries across 45 versions & 2 rubygems

Version Path
govuk_publishing_components-18.0.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.21.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.20.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.19.1 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.19.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.18.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.17.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.16.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.15.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.14.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.13.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.12.2 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.12.1 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.12.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.11.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.10.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.9.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.8.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.7.0 node_modules/eslint/lib/rules/no-inline-comments.js
govuk_publishing_components-17.6.1 node_modules/eslint/lib/rules/no-inline-comments.js