eslint-plugin-sorting
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -10,3 +10,7 @@ "use strict"; | ||
rules: { | ||
"sort-object-props": [1, { ignoreCase: true, ignoreMethods: false }] | ||
"sort-object-props": [1, { | ||
ignoreCase: true, | ||
ignoreMethods: false, | ||
ignoreMethodSiblings: false | ||
}] | ||
} | ||
@@ -13,0 +17,0 @@ } |
@@ -65,2 +65,37 @@ "use strict"; | ||
/** | ||
* @param {Object} opts: ESLint options. | ||
* @param {string} firstKey: An object field name. | ||
* @param {string} secondKey: An other object field name. | ||
* @param {string} nodeParentName: Parent node name. | ||
* @returns {boolean} If firstKey is lesser than or equal to secondKey. | ||
*/ | ||
function isLesserThanOrEqual(opts, firstKey, secondKey, nodeParentName) { | ||
if (opts.customSortOrder) { | ||
var excludeParent = opts.customSortOrder.excludeParent || []; | ||
if (nodeParentName && excludeParent.indexOf(nodeParentName) !== -1) { | ||
return firstKey <= secondKey; | ||
} | ||
var start = opts.customSortOrder.start || []; | ||
var end = opts.customSortOrder.end || []; | ||
if (start.indexOf(firstKey) !== -1 || end.indexOf(secondKey) !== -1) { | ||
return true; | ||
} | ||
if (start.indexOf(secondKey) !== -1 || end.indexOf(firstKey) !== -1) { | ||
return false; | ||
} | ||
} | ||
return firstKey <= secondKey; | ||
} | ||
/** | ||
* @param {Object} node: ObjectExpression node. | ||
* @returns {boolean} `true` if node has properties that are methods. | ||
*/ | ||
function hasMethods(node) { | ||
return node.properties.some(function(p) { | ||
return p.value.type === "FunctionExpression" || p.value.type === "ArrowFunctionExpression"; | ||
}); | ||
} | ||
module.exports = { | ||
@@ -71,2 +106,5 @@ create: function(context) { | ||
ObjectExpression: function(node) { | ||
if (opts.ignoreMethodSiblings && hasMethods(node)) { | ||
return; | ||
} | ||
node.properties.reduce(function(prev, current) { | ||
@@ -89,3 +127,4 @@ if (checkIgnoredTypes(current, prev, opts)) { | ||
if (currentKey < prevKey) { | ||
var nodeParentName = node.parent && node.parent.key && node.parent.key.name; | ||
if (!isLesserThanOrEqual(opts, prevKey, currentKey, nodeParentName)) { | ||
context.report(current, "Property names in an object literal should be sorted alphabetically"); | ||
@@ -92,0 +131,0 @@ } |
{ | ||
"name": "eslint-plugin-sorting", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Sorting rules for eslint", | ||
@@ -27,4 +27,4 @@ "keywords": [ | ||
"eslint-config-eslint": "^3.0.0", | ||
"mocha": "^2.5.3" | ||
"mocha": "^5.2.0" | ||
} | ||
} |
@@ -15,7 +15,9 @@ "use strict"; | ||
* @param {String} code: Code to be tested | ||
* @param {Object} options: Options to test with | ||
* @returns {object} Object that will be tested | ||
*/ | ||
function transpile(code) { | ||
function transpile(code, options) { | ||
return { | ||
code: code, | ||
options: options, | ||
parser: "babel-eslint", | ||
@@ -74,3 +76,7 @@ rules: {strict: 0} | ||
transpile("var nestedMember = {[a[b][c]]: d, [b[a][c]]: d}"), | ||
transpile("var binaryExpression = { [a + b]: c}") | ||
transpile("var binaryExpression = { [a + b]: c}"), | ||
transpile("var withMethods = { b: function() {}, a: function() {} }", [ { ignoreMethods: true } ]), | ||
transpile("var withMethodSiblings = { c: 1, b: 2, a: function() {} }", [ { ignoreMethodSiblings: true } ]), | ||
transpile("var withMethodSiblings = { c: 1, b: 2, a: () => {} }", [ { ignoreMethodSiblings: true } ]), | ||
transpile("var withMethodSiblings = { c: 1, b: 2, a() {} }", [ { ignoreMethodSiblings: true } ]) | ||
], | ||
@@ -83,2 +89,4 @@ invalid: [ | ||
expectError("var obj = { a: true, A: false }"), | ||
expectError("var withMethods = { b: function() {}, a: function() {} }"), | ||
expectError("var withMethodSiblings = { b: 2, a: function() {} }"), | ||
transpileExpectError("var functionCallWithArg = { [a('banana')]: 'a', [a('apple')]: 'b' }"), | ||
@@ -85,0 +93,0 @@ transpileExpectError("var functionCallWithArgs = { [a('apple','orange')]: 'a', [a('apple','banana')]: 'b' }"), |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
13202
8
275
1
73