david-w3
4/6/2015 - 10:46 AM

ESLint custom rule: Disallow multiple object properties to be declared on one line (no-multi-object-properties-one-line)

ESLint custom rule: Disallow multiple object properties to be declared on one line (no-multi-object-properties-one-line)

/* global module */

"use strict";

module.exports = function (context) {

    function checkObjectExpression(node) {

        var multiplePropertiesOnOneLine;
        var numberOfLines;
        var numberOfProperties = node.properties.length;
        var objHasMultipleProperties = (numberOfProperties > 1);

        if (objHasMultipleProperties) {
            numberOfLines = ((node.properties[node.properties.length - 1].loc.start.line) - node.properties[0].loc.start.line) + 1;
            multiplePropertiesOnOneLine = (numberOfLines < numberOfProperties);

            if (multiplePropertiesOnOneLine) {
                context.report(node, "multiple object properties on one line");
            }
        }
    }

    return {
        "ObjectExpression": checkObjectExpression
    };

};