New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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.2.2 to 0.3.0

65

lib/rules/sort-object-props.js

@@ -6,18 +6,61 @@ "use strict";

/**
* @param {Property} prop ObjectExpression property.
*
* @returns {String} Property key as a string.
* Recursively converts Nodes to strings so they can be sorted
* @param {Node} node: Babel AST Node
* @returns {String} Node value as a string
*/
function getKey(prop) {
try {
if (prop.key.type === "Literal") {
return prop.key.value.toString();
function nodeToString(node) {
switch (node.type) {
case ("BinaryExpression"): {
return nodeToString(node.left) + node.operator.toString() + nodeToString(node.right);
}
return prop.key.name.toString();
} catch (err) {
console.log("prop:", prop);
return "";
case ("CallExpression"): {
var args = node.arguments.map(function(arg) {
return nodeToString(arg);
}).toString();
return nodeToString(node.callee) + "(" + args + ")";
}
case ("ConditionalExpression"): {
return nodeToString(node.test) + "?" + nodeToString(node.consequent) + ":" + nodeToString(node.alternate);
}
case ("Identifier"): {
return node.name.toString();
}
case ("Literal"): {
return node.value.toString();
}
case ("MemberExpression"): {
return nodeToString(node.object) + "[" + nodeToString(node.property) + "]";
}
case ("TemplateElement"): {
return node.value.raw.toString();
}
case ("TemplateLiteral"): {
// interleave quasis with expressions
var s = [];
node.quasis.forEach(function(quasi, i) {
if (quasi.value.raw) {
s.push(nodeToString(quasi));
}
var expression = node.expressions[i];
if (expression) {
s.push(nodeToString(expression));
}
});
return s.join("");
}
default: {
// Silently ignore Nodes with types we don't handle
return "";
}
}
}
/**
* @param {Property} prop: ObjectExpression property.
* @returns {String} Property key as a string.
*/
function getKey(prop) {
return nodeToString(prop.key);
}
module.exports = {

@@ -24,0 +67,0 @@ create: function(context) {

9

lib/util/check-ignored-types.js

@@ -7,11 +7,2 @@ "use strict";

}, {
nodeType: "key",
type: "CallExpression"
}, {
nodeType: "key",
type: "ConditionalExpression"
}, {
nodeType: "key",
type: "MemberExpression"
}, {
nodeType: "value",

@@ -18,0 +9,0 @@ type: "FunctionExpression",

{
"name": "eslint-plugin-sorting",
"version": "0.2.2",
"version": "0.3.0",
"description": "Sorting rules for eslint",

@@ -5,0 +5,0 @@ "keywords": [

@@ -12,5 +12,45 @@ "use strict";

/**
* Wraps a block of code and that needs to be tested with babel-eslint
* @param {String} code: Code to be tested
* @returns {object} Object that will be tested
*/
function transpile(code) {
return {
code: code,
parser: "babel-eslint",
rules: {strict: 0}
};
}
/**
* Wraps a block of code and that that should error
* @param {String} code: Code to be tested
* @returns {object} Object that will be tested
*/
function expectError(code) {
return {
code: code,
errors: [ expectedError ]
};
}
/**
* Wraps a block of code that needs to be tested with babel-eslint and will error
* @param {String} code: Code to be tested
* @returns {object} Object that will be tested
*/
function transpileExpectError(code) {
return {
code: code,
errors: [ expectedError ],
parser: "babel-eslint",
rules: {strict: 0}
};
}
ruleTester.run("sort-object-props", rule, {
valid: [
"var obj = { a: true, apple: true }",
"var obj = { a: true, b: true, c: false }",

@@ -22,14 +62,28 @@ "var obj = { 'a': true, b: true, 'c': false }",

"var obj = { A: 'eggs', a: 'spam' }",
{ code: "var a = { a:'a' }; var b = {a:1, ...a, b:2}", "parser": "babel-eslint", rules: {strict: 0} },
{ code: "var a = { [a()]: 'a' }", "parser": "babel-eslint", rules: {strict: 0} },
{ code: "var a = { [a?b:c]: d }", "parser": "babel-eslint", rules: {strict: 0} },
{ code: "var a = { b: c }; var d = {[a.b]: e}", "parser": "babel-eslint", rules: {strict: 0} }
"var obj = { false: 'eggs', true: 'spam' }",
transpile("var template = { [`${a}-key`]: 'foo', 'b-key': 'bar' }"),
transpile("var templateWithBinaryExpression = { [`${a + b}-value`]: false, 'b+a': true }"),
transpile("var spread = { a:'a' }; var b = {a:1, ...a, b:2}"),
transpile("var functionCall = { [a()]: 'a', [b()]: 'b' }"),
transpile("var functionCallWithArg = { [a('apple')]: 'a', [a('banana')]: 'b' }"),
transpile("var functionCallWithArgs = { [a('apple','banana')]: 'a', [a('apple','orange')]: 'b' }"),
transpile("var conditional = { [a?b:c]: d }"),
transpile("var conditional = { [a ? b : c]: d }"),
transpile("var conditional = { [(a !== b)?b:c]: d, [(a === b)?b:c]: d, }"),
transpile("var member = {[a.b]: c}"),
transpile("var nestedMember = {[a[b][c]]: d, [b[a][c]]: d}"),
transpile("var binaryExpression = { [a + b]: c}")
],
invalid: [
{ code: "var obj = { b: 'spam', a: 'eggs', c: 'foo' }", errors: [ expectedError ] },
{ code: "var obj = { 'a': 'foo', '1': 'spam' }", errors: [ expectedError ] },
{ code: "var obj = { a: 'foo', 1: 'spam' }", errors: [ expectedError ] },
{ code: "var obj = { z: 'foo', a: 'spam' }", errors: [ expectedError ] },
{ code: "var obj = { a: true, A: false }", errors: [ expectedError ] }
expectError("var obj = { b: 'spam', a: 'eggs', c: 'foo' }"),
expectError("var obj = { 'a': 'foo', '1': 'spam' }"),
expectError("var obj = { a: 'foo', 1: 'spam' }"),
expectError("var obj = { z: 'foo', a: 'spam' }"),
expectError("var obj = { a: true, A: false }"),
transpileExpectError("var functionCallWithArg = { [a('banana')]: 'a', [a('apple')]: 'b' }"),
transpileExpectError("var functionCallWithArgs = { [a('apple','orange')]: 'a', [a('apple','banana')]: 'b' }"),
transpileExpectError("var conditional = { [c?b:a]: d, [a?b:c]: d }"),
transpileExpectError("var nestedMember = {[b[a][c]]: d, [a[b][c]]: d}"),
transpileExpectError("var binaryExpression = { apple: true, [a + b]: c}")
]
});
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