Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-sorting

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-sorting - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

README.md

6

index.js

@@ -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 @@ }

4

package.json
{
"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' }"),

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc