2 * @fileoverview Rule to require sorting of variables within a single Variable Declaration block
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description: "require variables within the same declaration block to be sorted",
18 category: "Stylistic Issues",
20 url: "https://eslint.org/docs/rules/sort-vars"
32 additionalProperties: false
41 const configuration = context.options[0] || {},
42 ignoreCase = configuration.ignoreCase || false,
43 sourceCode = context.getSourceCode();
46 VariableDeclaration(node) {
47 const idDeclarations = node.declarations.filter(decl => decl.id.type === "Identifier");
48 const getSortableName = ignoreCase ? decl => decl.id.name.toLowerCase() : decl => decl.id.name;
49 const unfixable = idDeclarations.some(decl => decl.init !== null && decl.init.type !== "Literal");
52 idDeclarations.slice(1).reduce((memo, decl) => {
53 const lastVariableName = getSortableName(memo),
54 currentVariableName = getSortableName(decl);
56 if (currentVariableName < lastVariableName) {
59 message: "Variables within the same declaration block should be sorted alphabetically.",
61 if (unfixable || fixed) {
64 return fixer.replaceTextRange(
65 [idDeclarations[0].range[0], idDeclarations[idDeclarations.length - 1].range[1]],
68 // Clone the idDeclarations array to avoid mutating it
71 // Sort the array into the desired order
72 .sort((declA, declB) => {
73 const aName = getSortableName(declA);
74 const bName = getSortableName(declB);
76 return aName > bName ? 1 : -1;
79 // Build a string out of the sorted list of identifier declarations and the text between the originals
80 .reduce((sourceText, identifier, index) => {
81 const textAfterIdentifier = index === idDeclarations.length - 1
83 : sourceCode.getText().slice(idDeclarations[index].range[1], idDeclarations[index + 1].range[0]);
85 return sourceText + sourceCode.getText(identifier) + textAfterIdentifier;
96 }, idDeclarations[0]);