/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Checks that identifiers are written in camelCase (or UPPER_CASE), with the
 * exception of optional parameters (opt_foo), which follow Closure convention.
 */

'use strict';

module.exports = function() {};

/**
 * Returns the option name to JSCS.
 * @return {string} the option name.
 */
module.exports.prototype.getOptionName = function() {
  return 'closureCamelCase';
};

/**
 * Configure the rule parameters.
 * @param  {boolean} value the value for this rule.
 */
module.exports.prototype.configure = function(value) {
  // rule preparation and configuration
  this._options = value;
};

/**
 * Checks whether or not the rule is being broken.
 * @param  {Object} file   the file being checked
 * @param  {Object} errors the current list of errors on the file
 */
module.exports.prototype.check = function(file, errors) {
  if (!this._options) {
    return;
  }
  file.iterateTokensByType('Identifier', function(token) {
    var name = token.value;
    if (name.replace(/^_+/g, '')
            .replace(/^opt_+/g, '')
            .replace(/_+$/g, '')
            .indexOf('_') === -1 ||
        name.toUpperCase() === name) {
      return;
    }
    errors.add(
      'All identifiers must be camelCase or UPPER_CASE',
      token.loc.start.line,
      token.loc.start.column
    );
  });
};
