Skip to content

Instantly share code, notes, and snippets.

@davidwaterston
Last active August 29, 2015 14:18
Show Gist options
  • Save davidwaterston/9772bf769125755b45f1 to your computer and use it in GitHub Desktop.
Save davidwaterston/9772bf769125755b45f1 to your computer and use it in GitHub Desktop.
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
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment