coffeelint-alphabetize-keys
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -0,1 +1,7 @@ | ||
### 1.5.0 (2016-03-07) | ||
* support overrides for specific ordering | ||
--- | ||
### 1.4.2 (2015-10-03) | ||
@@ -2,0 +8,0 @@ |
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var AlphabetizeKeys; | ||
var AlphabetizeKeys, | ||
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | ||
@@ -17,3 +18,4 @@ AlphabetizeKeys = (function() { | ||
message: 'Object and class keys should be alphabetized', | ||
name: 'alphabetize_keys' | ||
name: 'alphabetize_keys', | ||
overrides: [] | ||
}; | ||
@@ -77,3 +79,5 @@ | ||
AlphabetizeKeys.prototype._lintClass = function(node, astApi) { | ||
var classType, classTypeMapping, keys, keysMapping, results, valueType, visibility, visibilityMapping; | ||
var classType, classTypeMapping, keys, keysMapping, overrides, specificKeys, valueType, visibility, visibilityMapping; | ||
overrides = astApi.config[this.rule.name].overrides; | ||
specificKeys = []; | ||
keysMapping = this._emptyClassKeyMapping(); | ||
@@ -91,28 +95,21 @@ node.body.expressions.forEach((function(_this) { | ||
} | ||
return keysMapping[visibility][classType][valueType].push(key); | ||
if (indexOf.call(overrides, key) >= 0) { | ||
return specificKeys.push(key); | ||
} else { | ||
return keysMapping[visibility][classType][valueType].push(key); | ||
} | ||
}); | ||
}; | ||
})(this)); | ||
results = []; | ||
for (visibility in keysMapping) { | ||
visibilityMapping = keysMapping[visibility]; | ||
results.push((function() { | ||
var results1; | ||
results1 = []; | ||
for (classType in visibilityMapping) { | ||
classTypeMapping = visibilityMapping[classType]; | ||
results1.push((function() { | ||
var results2; | ||
results2 = []; | ||
for (valueType in classTypeMapping) { | ||
keys = classTypeMapping[valueType]; | ||
results2.push(this._lintNodeKeys(node, astApi, keys, [visibility, classType, valueType].join(' '))); | ||
} | ||
return results2; | ||
}).call(this)); | ||
for (classType in visibilityMapping) { | ||
classTypeMapping = visibilityMapping[classType]; | ||
for (valueType in classTypeMapping) { | ||
keys = classTypeMapping[valueType]; | ||
this._lintNodeKeys(node, astApi, keys, [visibility, classType, valueType].join(' ')); | ||
} | ||
return results1; | ||
}).call(this)); | ||
} | ||
} | ||
return results; | ||
return this._lintOverrideKeys(node, astApi, specificKeys); | ||
}; | ||
@@ -172,2 +169,22 @@ | ||
AlphabetizeKeys.prototype._lintOverrideKeys = function(node, astApi, keys) { | ||
var i, index, key, len, overrides, results; | ||
overrides = astApi.config[this.rule.name].overrides; | ||
keys = keys.map(this._stripQuotes); | ||
results = []; | ||
for (index = i = 0, len = keys.length; i < len; index = ++i) { | ||
key = keys[index]; | ||
if (index !== 0 && overrides.indexOf(keys[index - 1]) > overrides.indexOf(key)) { | ||
results.push(this.errors.push(astApi.createError({ | ||
lineNumber: node.locationData.first_line + 1, | ||
message: "Keys should respect overrides ordering: " + keys[index - 1] + " appears before " + key, | ||
rule: 'alphabetize_keys' | ||
}))); | ||
} else { | ||
results.push(void 0); | ||
} | ||
} | ||
return results; | ||
}; | ||
AlphabetizeKeys.prototype._stripQuotes = function(key) { | ||
@@ -174,0 +191,0 @@ var doubleQuoteMatch, singleQuoteMatch; |
{ | ||
"name": "coffeelint-alphabetize-keys", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "Coffeelint rule that verifies object keys are in alphabetical order", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -24,2 +24,6 @@ # coffeelint-alphabetize-keys | ||
### Configuration options | ||
* `overrides` - Array of keys to order as a separate category, keys must appear in the order provided. | ||
## Examples | ||
@@ -52,5 +56,5 @@ | ||
The keys are broken down into breaks 8 categories and | ||
The keys are broken down into 8 categories and | ||
each are required to only be individually alphabetical. | ||
Keys are seperated based on: | ||
Keys are separated based on: | ||
* function vs variable (based on the type of the value) | ||
@@ -61,1 +65,23 @@ * public vs private (key starting with `_` is private) | ||
The `constructor` function is ignored. | ||
#### Overrides | ||
```json | ||
"alphabetize_keys": { | ||
"module": "coffeelint-alphabetize-keys", | ||
"overrides": ["methodC", "methodB", "methodA"] | ||
} | ||
``` | ||
```coffee | ||
# Good | ||
class A | ||
methodC: -> | ||
methodB: -> | ||
methodA: -> | ||
# Bad | ||
class A | ||
methodA: -> | ||
methodB: -> | ||
methodC: -> | ||
``` |
10369
187
85