Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Clone in Desktop Download ZIP
106 lines (95 sloc) 2.47 KB
/**
* Requires identifiers to be camelCased or UPPERCASE_WITH_UNDERSCORES
*
* Types: `Boolean` or `String`
*
* Values:
*
* - `true`
* - `"ignoreProperties"` allows an exception for object property names.
*
* JSHint: [`camelcase`](http://jshint.com/docs/options/#camelcase)
*
* #### Example
*
* ```js
* "requireCamelCaseOrUpperCaseIdentifiers": true
* ```
*
* ##### Valid for mode `true`
*
* ```js
* var camelCase = 0;
* var CamelCase = 1;
* var _camelCase = 2;
* var camelCase_ = 3;
* var UPPER_CASE = 4;
* ```
*
* ##### Invalid for mode `true`
*
* ```js
* var lower_case = 1;
* var Mixed_case = 2;
* var mixed_Case = 3;
* ```
*
* ##### Valid for mode `ignoreProperties`
*
* ```js
* var camelCase = 0;
* var CamelCase = 1;
* var _camelCase = 2;
* var camelCase_ = 3;
* var UPPER_CASE = 4;
* var obj.snake_case = 5;
* var camelCase = { snake_case: 6 };
* ```
*
* ##### Invalid for mode `ignoreProperties`
*
* ```js
* var lower_case = 1;
* var Mixed_case = 2;
* var mixed_Case = 3;
* var snake_case = { snake_case: 6 };
* ```
*/
var assert = require('assert');
module.exports = function() {};
module.exports.prototype = {
configure: function(options) {
assert(
options === true || options === 'ignoreProperties',
this.getOptionName() + ' option requires a true value or `ignoreProperties`'
);
this._ignoreProperties = (options === 'ignoreProperties');
},
getOptionName: function() {
return 'requireCamelCaseOrUpperCaseIdentifiers';
},
check: function(file, errors) {
file.iterateTokensByType('Identifier', function(token) {
var value = token.value;
if (value.replace(/^_+|_+$/g, '').indexOf('_') === -1 || value.toUpperCase() === value) {
return;
}
if (this._ignoreProperties) {
var nextToken = file.getNextToken(token);
var prevToken = file.getPrevToken(token);
if (nextToken && nextToken.value === ':') {
return;
}
if (prevToken && (prevToken.value === '.' ||
prevToken.value === 'get' || prevToken.value === 'set')) {
return;
}
}
errors.add(
'All identifiers must be camelCase or UPPER_CASE',
token.loc.start.line,
token.loc.start.column
);
}.bind(this));
}
};
Jump to Line
Something went wrong with that request. Please try again.