eslint-plugin-sort
Advanced tools
Comparing version 2.7.1 to 2.8.0
@@ -34,3 +34,3 @@ // src/utils.ts | ||
} else if (caseSensitive) { | ||
return (a, b) => a.localeCompare(b); | ||
return (a, b) => a < b ? -1 : a > b ? 1 : 0; | ||
} else if (natural) { | ||
@@ -671,2 +671,73 @@ return (a, b) => naturalCompare(a.toLowerCase(), b.toLowerCase()); | ||
// src/rules/string-unions.ts | ||
import { | ||
ESLintUtils as ESLintUtils2, | ||
TSESTree as TSESTree3 | ||
} from "@typescript-eslint/experimental-utils"; | ||
function getSortValue4(node) { | ||
return node.type === TSESTree3.AST_NODE_TYPES.TSLiteralType && node.literal.type === TSESTree3.AST_NODE_TYPES.Literal && typeof node.literal.value === "string" ? node.literal.value : null; | ||
} | ||
var string_unions_default = ESLintUtils2.RuleCreator.withoutDocs({ | ||
create(context) { | ||
const source = context.getSourceCode(); | ||
const options = context.options[0]; | ||
const sorter = getSorter({ | ||
caseSensitive: options?.caseSensitive, | ||
natural: options?.natural | ||
}); | ||
return { | ||
TSUnionType(node) { | ||
const nodes = node.types; | ||
if (nodes.length < 2) | ||
return; | ||
if (nodes.map(getSortValue4).some((value) => value === null)) | ||
return; | ||
const sorted = nodes.slice().sort((a, b) => sorter(getSortValue4(a) ?? "", getSortValue4(b) ?? "")); | ||
const firstUnsortedNode = isUnsorted(nodes, sorted); | ||
if (firstUnsortedNode) { | ||
context.report({ | ||
node: firstUnsortedNode, | ||
messageId: "unsorted", | ||
*fix(fixer) { | ||
for (const [node2, complement] of enumerate(nodes, sorted)) { | ||
yield fixer.replaceText(node2, getNodeText2(source, complement)); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
recommended: false, | ||
url: docsURL("string-unions"), | ||
description: `Sorts TypeScript string unions alphabetically and case insensitive in ascending order.` | ||
}, | ||
fixable: "code", | ||
messages: { | ||
unsorted: "String unions should be sorted alphabetically." | ||
}, | ||
schema: [ | ||
{ | ||
additionalProperties: false, | ||
default: { caseSensitive: false, natural: true }, | ||
properties: { | ||
caseSensitive: { | ||
type: "boolean", | ||
default: false | ||
}, | ||
natural: { | ||
type: "boolean", | ||
default: true | ||
} | ||
}, | ||
type: "object" | ||
} | ||
], | ||
type: "suggestion" | ||
}, | ||
defaultOptions: [{}] | ||
}); | ||
// src/index.ts | ||
@@ -715,3 +786,4 @@ var config = { | ||
"object-properties": object_properties_default, | ||
"type-properties": type_properties_default | ||
"type-properties": type_properties_default, | ||
"string-unions": string_unions_default | ||
} | ||
@@ -718,0 +790,0 @@ }; |
{ | ||
"name": "eslint-plugin-sort", | ||
"description": "Auto-fixable sort rules for ESLint.", | ||
"version": "2.7.1", | ||
"version": "2.8.0", | ||
"author": "Mark Skelton", | ||
@@ -6,0 +6,0 @@ "packageManager": "pnpm@7.29.1", |
@@ -55,1 +55,2 @@ # eslint-plugin-sort | ||
| | 🔧 | [sort/type-properties](docs/rules/type-properties.md) | Sorts TypeScript type properties | | ||
| | 🔧 | [sort/string-unions](docs/rules/string-unions.md) | Sorts TypeScript string unions | |
Sorry, the diff of this file is not supported yet
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
51088
1585
56