const { visit, getLocation } = require('graphql/language');
const { upperCase } = require('lodash');

const getMessage = (text, node) => {
  const message = `Property '${node.name
    .value}' has invalidly cased values. Please uppercase them.`;
  const location = getLocation({ body: text }, node.name.loc.start);

  return {
    message,
    line: location.line,
    column: location.column,
    ruleId: 'enum.casing',
    severity: 1
  };
};

module.exports = function(ast, text) {
  const messages = [];

  visit(ast, {
    EnumTypeDefinition(node) {
      let valid = true;

      visit(node, {
        EnumValueDefinition(node) {
          const uppercased = upperCase(node.name.value);
          if (uppercased !== node.name.value) {
            valid = false;
          }
        }
      });

      if (!valid) {
        const message = getMessage(text, node);
        if (message) {
          messages.push(message);
        }
      }
    }
  });

  return messages;
};

const { visit, getLocation } = require('graphql/language');
const { toUpper } = require('lodash');

const getMessage = (text, node) => {
  const message = `Property '${node.name
    .value}' has invalidly cased values. Please uppercase them.`;
  const location = getLocation({ body: text }, node.name.loc.start);

  return {
    message,
    line: location.line,
    column: location.column,
    ruleId: 'enum.casing',
    severity: 1
  };
};

module.exports = function(ast, text) {
  const messages = [];

  visit(ast, {
    EnumTypeDefinition(node) {
      let valid = true;

      visit(node, {
        EnumValueDefinition(node) {
          const uppercased = toUpper(node.name.value);
          if (uppercased !== node.name.value) {
            valid = false;
          }
        }
      });

      if (!valid) {
        const message = getMessage(text, node);
        if (message) {
          messages.push(message);
        }
      }
    }
  });

  return messages;
};

Allow underscores in enum values
Proposal: Allow underscores in enum values

Lodash's `upperCase` method will convert all underscores to spaces. So, an enum value of "NOT_CERTIFIED" will get changed to "NOT CERTIFIED" which will not match and cause the lint check to fail. Alternatively, if we use the `toUpper` method, the underscores will be preserved and the lint check will pass.