-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an unique alphabetical order array rule
- Loading branch information
Showing
13 changed files
with
218 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.yarn/* | ||
node_modules | ||
dist | ||
/.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum Rules { | ||
ALPHABETICAL_ORDER = 'eslint: enable-alphabetical-order-array', | ||
UNIQUE_ALPHABETICAL_ORDER = 'eslint: enable-unique-alphabetical-order-array', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Rule } from 'eslint'; | ||
import { ArrayExpression } from 'estree'; | ||
|
||
import { Options } from '../index'; | ||
import { arrayFixer } from '../utils/arrayFixer'; | ||
import { arrayItemsScanner, NodeItem } from '../utils/arrayItemsScanner'; | ||
import { arrayItemsSorter } from '../utils/arrayItemsSorter'; | ||
|
||
export function alphabeticalOrderErrorsFixer( | ||
elements: readonly NodeItem[], | ||
sortedElements: readonly NodeItem[], | ||
node: ArrayExpression, | ||
context: Rule.RuleContext, | ||
): boolean { | ||
if (sortedElements.some((element: NodeItem, index: number): boolean => element !== elements[index])) { | ||
arrayFixer(sortedElements, 'Array elements are not sorted alphabetically.', node, context); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export function alphabeticalOrderArrayRule(node: ArrayExpression, context: Rule.RuleContext, options: Options): void { | ||
const elements = arrayItemsScanner(node, context.getSourceCode()); | ||
const sortedElements = arrayItemsSorter([...elements], options.spreadVariablesFirst || false); | ||
|
||
alphabeticalOrderErrorsFixer(elements, sortedElements, node, context); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Rule } from 'eslint'; | ||
import { ArrayExpression } from 'estree'; | ||
|
||
import { Rules } from '../enums/Rules'; | ||
import { Options } from '../index'; | ||
|
||
import { alphabeticalOrderArrayRule } from './alphabeticalOrderArrayRule'; | ||
import { uniqueAlphabeticalOrderArrayRule } from './uniqueAlphabeticalOrderArrayRule'; | ||
|
||
type Callback = (node: ArrayExpression, context: Rule.RuleContext, options: Options) => void; | ||
|
||
const ruleCallbackMapper: Record<Rules, Callback> = { | ||
[Rules.ALPHABETICAL_ORDER]: alphabeticalOrderArrayRule, | ||
[Rules.UNIQUE_ALPHABETICAL_ORDER]: uniqueAlphabeticalOrderArrayRule, | ||
}; | ||
|
||
export function arrayTokesRules(node: ArrayExpression, context: Rule.RuleContext, rule: Rules, options: Options): void { | ||
ruleCallbackMapper[rule](node, context, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Rule } from 'eslint'; | ||
import { ArrayExpression } from 'estree'; | ||
|
||
import { Options } from '../index'; | ||
import { arrayFixer } from '../utils/arrayFixer'; | ||
import { arrayItemsScanner, NodeItem } from '../utils/arrayItemsScanner'; | ||
import { arrayItemsSorter } from '../utils/arrayItemsSorter'; | ||
|
||
import { alphabeticalOrderErrorsFixer } from './alphabeticalOrderArrayRule'; | ||
|
||
export function uniqueAlphabeticalOrderArrayCheckerFixer( | ||
sortedElements: readonly NodeItem[], | ||
node: ArrayExpression, | ||
context: Rule.RuleContext, | ||
): void { | ||
const uniqueItemSet = new Set<string>(); | ||
const uniqueItems: NodeItem[] = new Array(sortedElements.length); | ||
let cursor = 0; | ||
let hasUniqueItems = true; | ||
|
||
for (const element of sortedElements) { | ||
if (uniqueItemSet.has(element.value) === false) { | ||
uniqueItems[cursor] = element; | ||
cursor++; | ||
uniqueItemSet.add(element.value); | ||
|
||
continue; | ||
} | ||
|
||
hasUniqueItems = false; | ||
} | ||
|
||
if (hasUniqueItems !== false) { | ||
return; | ||
} | ||
|
||
arrayFixer(uniqueItems, 'Array elements are not uniqueness.', node, context); | ||
} | ||
|
||
export function uniqueAlphabeticalOrderArrayRule( | ||
node: ArrayExpression, | ||
context: Rule.RuleContext, | ||
options: Options, | ||
): void { | ||
const elements = arrayItemsScanner(node, context.getSourceCode()); | ||
const sortedElements = arrayItemsSorter([...elements], options.spreadVariablesFirst || false); | ||
|
||
//has no alphabetical errors, validate uniqueness | ||
if (alphabeticalOrderErrorsFixer(elements, sortedElements, node, context) === false) { | ||
uniqueAlphabeticalOrderArrayCheckerFixer(sortedElements, node, context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Rule } from 'eslint'; | ||
import { ArrayExpression } from 'estree'; | ||
|
||
import { NodeItem } from './arrayItemsScanner'; | ||
|
||
export function arrayFixer( | ||
sortedElements: readonly NodeItem[], | ||
message: string, | ||
node: ArrayExpression, | ||
context: Rule.RuleContext, | ||
): void { | ||
const newCode = `[${sortedElements | ||
.map((element: NodeItem): string => (element.isSpread ? `...${element.value}` : element.value)) | ||
.join(', ')}]`; | ||
|
||
context.report({ | ||
node, | ||
message, | ||
fix: (fixer: Rule.RuleFixer): Rule.Fix => fixer.replaceText(node, newCode), | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { SourceCode } from 'eslint'; | ||
import { ArrayExpression } from 'estree'; | ||
|
||
export type NodeItem = { | ||
readonly value: string; | ||
readonly isSpread: boolean; | ||
}; | ||
|
||
export function arrayItemsScanner(node: ArrayExpression, sourceCode: SourceCode): readonly NodeItem[] { | ||
const elements: NodeItem[] = new Array(node.elements.length); | ||
let cursor = 0; | ||
|
||
for (const element of node.elements) { | ||
if (element === null) { | ||
continue; | ||
} | ||
|
||
if (element.type === 'SpreadElement') { | ||
elements[cursor] = { | ||
value: sourceCode.getText(element.argument), | ||
isSpread: true, | ||
}; | ||
} else { | ||
elements[cursor] = { | ||
value: sourceCode.getText(element), | ||
isSpread: false, | ||
}; | ||
} | ||
|
||
cursor++; | ||
} | ||
|
||
return elements; | ||
} |
Oops, something went wrong.