Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
eslint-plugin-sort-keys-plus
Advanced tools
Fork of eslint rule that sorts keys in objects (https://eslint.org/docs/rules/sort-keys) with extra features
Fork of eslint rule that sorts keys in objects (https://eslint.org/docs/rules/sort-keys) with autofix enabled
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-sort-keys-plus
:
$ npm install eslint-plugin-sort-keys-plus --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-sort-keys-plus
globally.
Add sort-keys-plus
to the plugins section of your .eslintrc
configuration file. You can omit the eslint-plugin-
prefix:
{
"plugins": [
"sort-keys-plus"
]
}
Then add sort-keys-plus rule under the rules section.
{
"rules": {
"sort-keys-plus/sort-keys": "warn"
}
}
Often it makes sense to enable sort-keys-plus
only for certain files/directories. For cases like that, use override key of eslint config:
{
"rules": {
// ...
},
"overrides": [
{
"files": ["src/alphabetical.js", "bin/*.js", "lib/*.js"],
"rules": {
"sort-keys-plus/sort-keys-plus": "warn"
}
}
]
}
In some cases, there should be a specific order for some properties of an object, such as with mongodb aggregations. For that, use the override key of the configuration:
{
"rules": {
"sort-keys-plus/sort-keys": ["warn", "asc", {
"overrides": [
{
"properties": ["$lookup"],
"order": ["from", "localField", "foreignField", "as"]
}
]
}]
}
}
For available config options, see official sort-keys reference. All options supported by sort-keys
are supported by sort-keys-plus
.
{
"sort-keys-shorthand/sort-keys-shorthand": [
"error",
"asc",
{
"caseSensitive": true,
"minKeys": 2,
"natural": false,
"ignoreSingleLine": false,
"allCaps": "ignore",
"shorthand": "ignore",
"overrides": [],
}
]
}
Additional properties that can be set in the 2nd option object supported are as follows:
ignoreSingleline
- if true
, this rule is ignored on single line objects. Default is false
.allCaps
handling for ALL_CAPS
properties
ignore
no rules for all capsfirst
all caps properties must be firstlast
all caps properties must be lastshorthand
handling for shorthand properties
ignore
no rules for shorthandsfirst
shorthand properties must be firstlast
shrothand properties must be lastshorthand
is checked after allCaps
, so ALL_CAPS will be before shorthand when both are 'first'
.
overrides
allows custom orders for specific sets of keys, or sub-objects with a specific parent key. See below for configuration.Examples of incorrect code for the {ignoreSingleLine: true}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {ignoreSingleLine: true}]*/
/*eslint-env es6*/
let obj = {
e: 1,
c: 3,
C: 4,
b: 2
};
Examples of correct code for the {ignoreSingleLine: true}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {ignoreSingleLine: true}]*/
/*eslint-env es6*/
let obj = { e: 1, b: 2, c: 3, C: 4 };
Examples of incorrect code for the {allCaps: 'first'}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {allCaps: 'first'}]*/
/*eslint-env es6*/
let obj = {
a: 1,
B_CONSTANT: 2, // not sorted correctly (should be 1st key)
c: 3,
d: 4
};
Examples of correct code for the {allCaps: 'first'}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {allCaps: 'first'}]*//
/*eslint-env es6*/
let obj = {
B_CONSTANT: 2,
a: 1,
c: 3,
};
Examples of incorrect code for the {shorthand: 'first'}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {shorthand: 'first'}]*/
/*eslint-env es6*/
const b = 2;
let obj = {
a: 1,
b, // not sorted correctly (should be 1st key)
c: 3,
d: 4
};
Examples of correct code for the {shorthand: 'first'}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {shorthand: 'first'}]*//
/*eslint-env es6*/
const b = 2;
let obj = {
b,
a: 1,
c: 3,
};
Configuration:
"overrides": [
{
"order": ["title", "description"],
"message": "`title` should be before `description`",
},
{
"
}
],
order
define the property keys that should be orderedproperties
define parent property key for this rule. If this is not provided, the override will apply to all objects with a subset of the keys in order
message
optional custom message when the rule is violatedExamples of incorrect code for the {overrides: [{order: ['b', 'a', 'd']}]}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{order: ['b', 'a', 'd']}]}]*/
/*eslint-env es6*/
let obj = {
a: 1,
b: 2, // not sorted correctly (should be 1st key)
d: 4
};
Examples of correct code for the {overrides: [{order: ['b', 'a', 'd']}]}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{order: ['b', 'a', 'd']}]}]*/
/*eslint-env es6*/
let obj = {
b: 2,
a: 1,
d: 4
};
// has additional properties
let obj = {
a: 1,
b: 2,
c: 3,
d: 4
};
Examples of incorrect code for the {overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]}]*/
/*eslint-env es6*/
let obj = { a:
{
a: 1,
b: 2, // not sorted correctly (should be 1st key)
c: 3,
d: 4 // not sorted correctly (should be 3rd key)
};
Examples of correct code for the {overrides: [{overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]}
option:
/*eslint sort-keys-plus/sort-keys: ["error", "asc", {overrides: [{overrides: [{properties: ['a'], order: ['b', 'a', 'd']}]}*/
/*eslint-env es6*/
let obj = {
b: 2,
a: 1,
d: 4,
c: 3
};
FAQs
Fork of eslint rule that sorts keys in objects (https://eslint.org/docs/rules/sort-keys) with extra features
We found that eslint-plugin-sort-keys-plus demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.