Skip to content

Commit

Permalink
refactor: refactor rules code
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Sep 26, 2024
1 parent 99a7e33 commit 682d422
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
8 changes: 3 additions & 5 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,10 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
member.range.at(0),
member.typeAnnotation?.range.at(0) ?? member.range.at(1),
)
} else if (member.key.type === 'Identifier') {
;({ name } = member.key)
} else {
if (member.key.type === 'Identifier') {
;({ name } = member.key)
} else {
name = sourceCode.text.slice(...member.key.range)
}
name = sourceCode.text.slice(...member.key.range)
}

let isPrivateHash =
Expand Down
27 changes: 13 additions & 14 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,8 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
hasUnknownGroup = true
}
}
} else {
if (group === 'unknown') {
hasUnknownGroup = true
}
} else if (group === 'unknown') {
hasUnknownGroup = true
}
}

Expand Down Expand Up @@ -326,9 +324,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
'.',
].includes(value)

let isParent = (value: string) => value.indexOf('..') === 0
let isParent = (value: string) => value.startsWith('..')

let isSibling = (value: string) => value.indexOf('./') === 0
let isSibling = (value: string) => value.startsWith('./')

let { getGroup, defineGroup, setCustomGroups } = useGroups(options.groups)

Expand Down Expand Up @@ -531,15 +529,16 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (!(groupNum in grouped)) {
grouped[groupNum] = [node]
} else if (
!options.sortSideEffects &&
isSideEffectImport(node.node)
) {
grouped[groupNum] = [...grouped[groupNum], node]
} else {
if (!options.sortSideEffects && isSideEffectImport(node.node)) {
grouped[groupNum] = [...grouped[groupNum], node]
} else {
grouped[groupNum] = sortNodes(
[...grouped[groupNum], node],
options,
)
}
grouped[groupNum] = sortNodes(
[...grouped[groupNum], node],
options,
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
)

let formatName = (value: string): string =>
value.replace(/(,|;)$/, '')
value.replace(/([,;])$/, '')

if (member.type === 'TSPropertySignature') {
if (member.key.type === 'Identifier') {
Expand Down
8 changes: 3 additions & 5 deletions rules/sort-svelte-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (attribute.key.type === 'SvelteSpecialDirectiveKey') {
name = sourceCode.text.slice(...attribute.key.range)
} else if (typeof attribute.key.name === 'string') {
;({ name } = attribute.key)
} else {
if (typeof attribute.key.name === 'string') {
;({ name } = attribute.key)
} else {
name = sourceCode.text.slice(...attribute.key.range)
}
name = sourceCode.text.slice(...attribute.key.range)
}

setCustomGroups(options.customGroups, name)
Expand Down

0 comments on commit 682d422

Please sign in to comment.