eslint-rules
Version: 
My custom eslint rules in addition to the ones provided at http://eslint.org/
47 lines (39 loc) • 1.16 kB
JavaScript
module.exports = function (context) {
  'use strict';
  function isConstant(str) {
    var allCapsWithUnderScore = /^[A-Z_]+$/;
    return allCapsWithUnderScore.test(str);
  }
  function isFrontOrBackUnderscore(str) {
    var k = str.indexOf('_');
    if (k === 0 || k === str.length - 1) {
      return true;
    }
    k = str.lastIndexOf('_');
    if (k === str.length - 1) {
      return true;
    }
    return false;
  }
  return {
    Identifier: function (node) {
      var nameWithMaybeColon = context.getSource(node, 0, 1);
      if (nameWithMaybeColon[nameWithMaybeColon.length - 1] !== ':') {
        if (nameWithMaybeColon.indexOf('_') !== -1) {
          // allow constants FOO_BAR with all caps to use _
          var justName = node.name.trim();
          if (isConstant(justName)) {
            return;
          }
          // allow dangling underscore at the front or back only
          if (isFrontOrBackUnderscore(justName)) {
            return;
          }
          context.report(node, '`{{identifier}}` : _ in names only allowed in properties', {
            identifier: node.name
          });
        }
      }
    }
  };
};