davidwaterston
4/6/2015 - 10:42 AM

ESLint custom rule: Disallow object property values from appearing on a different line from their key

ESLint custom rule: Disallow object property values from appearing on a different line from their key

/* global module */

"use strict";

module.exports = function (context) {

    function checkObjectExpression(node) {

        var props = node.properties;
        var numberOfProperties = props.length;
        var i;
        var nameAndValueAreOnDifferentLines;

        for (i = 0; i < numberOfProperties; i++) {
            nameAndValueAreOnDifferentLines = (props[i].key.loc.start.line !== props[i].value.loc.start.line);
            if (nameAndValueAreOnDifferentLines) {
                context.report(
                    node,
                    {
                        line: props[i].key.loc.start.line,
                        column: props[i].key.loc.start.column
                    },
                    "object property key '" + props[i].key.name + "' has its value on a different line"
                );
            }
        }

    }

    return {
        "ObjectExpression": checkObjectExpression
    };

};