Skip to content

Commit

Permalink
refactor: switch to non-deprecated typescript-eslint api
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Nov 21, 2023
1 parent 6a68393 commit da903e7
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 97 deletions.
11 changes: 7 additions & 4 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
'spread-last': false,
})

let source = context.getSourceCode()

let nodes: (SortingNode & { type: string })[] = elements
.reduce(
(
Expand All @@ -109,7 +107,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
name:
element.type === 'Literal'
? `${element.value}`
: source.text.slice(...element.range),
: context.sourceCode.text.slice(...element.range),
size: rangeToDiff(element.range),
type: element.type,
node: element,
Expand Down Expand Up @@ -160,7 +158,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
}

return makeFixes(fixer, nodes, sortedNodes, source)
return makeFixes(
fixer,
nodes,
sortedNodes,
context.sourceCode,
)
},
})
}
Expand Down
13 changes: 8 additions & 5 deletions rules/sort-astro-attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
],
// @ts-ignore
create: context => {
if (path.extname(context.getFilename()) !== '.astro') {
if (path.extname(context.filename) !== '.astro') {
return {}
}

Expand All @@ -108,8 +108,6 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
})

let source = context.getSourceCode()

let parts: SortingNode[][] = attributes.reduce(
(accumulator: SortingNode[][], attribute) => {
if (attribute.type === 'JSXSpreadAttribute') {
Expand All @@ -120,7 +118,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
let name =
typeof attribute.name.name === 'string'
? attribute.name.name
: source.text.slice(...attribute.name.range)
: context.sourceCode.text.slice(...attribute.name.range)

let { getGroup, defineGroup, setCustomGroups } = useGroups(
options.groups,
Expand Down Expand Up @@ -194,7 +192,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
sortedNodes.push(...sortNodes(grouped[group], options))
}

return makeFixes(fixer, nodes, sortedNodes, source)
return makeFixes(
fixer,
nodes,
sortedNodes,
context.sourceCode,
)
},
})
}
Expand Down
15 changes: 8 additions & 7 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,22 @@ export default createEslintRule<Options, MESSAGE_ID>({
groups: ['property', 'constructor', 'method', 'unknown'],
})

let source = context.getSourceCode()

let nodes: SortingNode[] = node.body.map(member => {
let name: string
let { getGroup, defineGroup } = useGroups(options.groups)

if (member.type === 'StaticBlock') {
name = 'static'
} else if (member.type === 'TSIndexSignature') {
name = source.text.slice(
name = context.sourceCode.text.slice(
member.range.at(0),
member.typeAnnotation?.range.at(0) ?? member.range.at(1),
)
} else {
if (member.key.type === 'Identifier') {
;({ name } = member.key)
} else {
name = source.text.slice(...member.key.range)
name = context.sourceCode.text.slice(...member.key.range)
}
}

Expand Down Expand Up @@ -232,9 +230,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
for (let i = 0, max = formatted.length; i < max; i++) {
fixes.push(
fixer.replaceTextRange(
getNodeRange(nodes.at(i)!.node, source),
source.text.slice(
...getNodeRange(formatted.at(i)!.node, source),
getNodeRange(nodes.at(i)!.node, context.sourceCode),
context.sourceCode.text.slice(
...getNodeRange(
formatted.at(i)!.node,
context.sourceCode,
),
),
),
)
Expand Down
11 changes: 7 additions & 4 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
'ignore-case': false,
})

let source = context.getSourceCode()

let nodes: SortingNode[] = node.members.map(member => ({
name:
member.id.type === 'Literal'
? `${member.id.value}`
: `${source.text.slice(...member.id.range)}`,
: `${context.sourceCode.text.slice(...member.id.range)}`,
size: rangeToDiff(member.range),
node: member,
}))
Expand All @@ -100,7 +98,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortNodes(nodes, options), source),
makeFixes(
fixer,
nodes,
sortNodes(nodes, options),
context.sourceCode,
),
})
}
})
Expand Down
9 changes: 6 additions & 3 deletions rules/sort-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
},
'Program:exit': () => {
let source = context.getSourceCode()

for (let nodes of parts) {
pairwise(nodes, (left, right) => {
if (isPositive(compare(left, right, options))) {
Expand All @@ -114,7 +112,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortNodes(nodes, options), source),
makeFixes(
fixer,
nodes,
sortNodes(nodes, options),
context.sourceCode,
),
})
}
})
Expand Down
47 changes: 30 additions & 17 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
options.groups = [...options.groups, 'unknown']
}

let source = context.getSourceCode()

let nodes: SortingNode[] = []

let isSideEffectImport = (node: TSESTree.Node) =>
Expand Down Expand Up @@ -365,7 +363,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
) {
name = `${node.moduleReference.expression.value}`
} else {
name = source.text.slice(...node.moduleReference.range)
name = context.sourceCode.text.slice(...node.moduleReference.range)
}
}

Expand All @@ -391,9 +389,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
left: SortingNode,
right: SortingNode,
): boolean =>
!!source.getTokensBetween(
!!context.sourceCode.getTokensBetween(
left.node,
getCommentBefore(right.node, source) || right.node,
getCommentBefore(right.node, context.sourceCode) || right.node,
{
includeComments: true,
},
Expand Down Expand Up @@ -437,8 +435,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

fixes.push(
fixer.replaceTextRange(
getNodeRange(nodesToFix.at(i)!.node, source),
source.text.slice(...getNodeRange(node.node, source)),
getNodeRange(nodesToFix.at(i)!.node, context.sourceCode),
context.sourceCode.text.slice(
...getNodeRange(node.node, context.sourceCode),
),
),
)

Expand All @@ -447,7 +447,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (nextNode) {
let linesBetweenImports = getLinesBetween(
source,
context.sourceCode,
nodesToFix.at(i)!,
nodesToFix.at(i + 1)!,
)
Expand All @@ -462,9 +462,14 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
) {
fixes.push(
fixer.removeRange([
getNodeRange(nodesToFix.at(i)!.node, source).at(1)!,
getNodeRange(nodesToFix.at(i + 1)!.node, source).at(0)! -
1,
getNodeRange(
nodesToFix.at(i)!.node,
context.sourceCode,
).at(1)!,
getNodeRange(
nodesToFix.at(i + 1)!.node,
context.sourceCode,
).at(0)! - 1,
]),
)
}
Expand All @@ -478,10 +483,14 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
fixes.push(
fixer.replaceTextRange(
[
getNodeRange(nodesToFix.at(i)!.node, source).at(1)!,
getNodeRange(nodesToFix.at(i + 1)!.node, source).at(
0,
)! - 1,
getNodeRange(
nodesToFix.at(i)!.node,
context.sourceCode,
).at(1)!,
getNodeRange(
nodesToFix.at(i + 1)!.node,
context.sourceCode,
).at(0)! - 1,
],
'\n',
),
Expand All @@ -496,7 +505,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
) {
fixes.push(
fixer.insertTextAfterRange(
getNodeRange(nodesToFix.at(i)!.node, source),
getNodeRange(nodesToFix.at(i)!.node, context.sourceCode),
'\n',
),
)
Expand Down Expand Up @@ -525,7 +534,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)

let numberOfEmptyLinesBetween = getLinesBetween(source, left, right)
let numberOfEmptyLinesBetween = getLinesBetween(
context.sourceCode,
left,
right,
)

if (
!(
Expand Down
27 changes: 20 additions & 7 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}),
)
) {
let source = context.getSourceCode()

let formattedMembers: SortingNode[][] = node.body.body.reduce(
(accumulator: SortingNode[][], element) => {
if (element.type === 'TSCallSignatureDeclaration') {
Expand All @@ -142,18 +140,24 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
element.typeAnnotation?.range.at(0) ??
element.range.at(1)! - (element.optional ? '?'.length : 0)

name = source.text.slice(element.range.at(0), end)
name = context.sourceCode.text.slice(element.range.at(0), end)
}
} else if (element.type === 'TSIndexSignature') {
let endIndex: number =
element.typeAnnotation?.range.at(0) ?? element.range.at(1)!

name = source.text.slice(element.range.at(0), endIndex)
name = context.sourceCode.text.slice(
element.range.at(0),
endIndex,
)
} else {
let endIndex: number =
element.returnType?.range.at(0) ?? element.range.at(1)!

name = source.text.slice(element.range.at(0), endIndex)
name = context.sourceCode.text.slice(
element.range.at(0),
endIndex,
)
}

let elementSortingNode = {
Expand All @@ -165,7 +169,11 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
if (
options['partition-by-new-line'] &&
lastElement &&
getLinesBetween(source, lastElement, elementSortingNode)
getLinesBetween(
context.sourceCode,
lastElement,
elementSortingNode,
)
) {
accumulator.push([])
}
Expand Down Expand Up @@ -227,7 +235,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
sortedNodes.push(...sortNodes(grouped[group], options))
}

return makeFixes(fixer, nodes, sortedNodes, source)
return makeFixes(
fixer,
nodes,
sortedNodes,
context.sourceCode,
)
},
})
}
Expand Down
13 changes: 7 additions & 6 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
],
create: context => {
if (
['.svelte', '.astro', '.vue'].includes(
path.extname(context.getFilename()),
)
['.svelte', '.astro', '.vue'].includes(path.extname(context.filename))
) {
return {}
}
Expand All @@ -105,8 +103,6 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
})

let source = context.getSourceCode()

let parts: SortingNode[][] = node.openingElement.attributes.reduce(
(
accumulator: SortingNode[][],
Expand Down Expand Up @@ -191,7 +187,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
sortedNodes.push(...sortNodes(grouped[group], options))
}

return makeFixes(fixer, nodes, sortedNodes, source)
return makeFixes(
fixer,
nodes,
sortedNodes,
context.sourceCode,
)
},
})
}
Expand Down
Loading

0 comments on commit da903e7

Please sign in to comment.