Skip to content

Commit

Permalink
feat: move to typescript-eslint v8
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io authored Aug 2, 2024
1 parent f3906f9 commit 05b6502
Show file tree
Hide file tree
Showing 29 changed files with 1,179 additions and 760 deletions.
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
"vue-eslint-parser": ">=9.0.0"
},
"dependencies": {
"@typescript-eslint/types": "^7.18.0",
"@typescript-eslint/utils": "^7.18.0",
"@typescript-eslint/types": "^8.0.0",
"@typescript-eslint/utils": "^8.0.0",
"minimatch": "^10.0.1",
"natural-compare-lite": "^1.4.0"
},
Expand All @@ -95,9 +95,9 @@
"@types/natural-compare-lite": "^1.4.2",
"@types/node": "^22.0.1",
"@types/unist": "^3.0.2",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"@typescript-eslint/rule-tester": "^7.18.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"@typescript-eslint/rule-tester": "^8.0.0",
"@typescript-eslint/types": "^6.13.0",
"@vercel/og": "^0.6.2",
"@vitest/coverage-v8": "^2.0.5",
Expand All @@ -107,7 +107,7 @@
"changelogen": "^0.5.5",
"clean-publish": "^5.0.0",
"cspell": "^8.13.0",
"eslint": "^8.57.0",
"eslint": "^9.8.0",
"eslint-plugin-astro": "^1.2.3",
"eslint-plugin-eslint-plugin": "^6.2.0",
"eslint-plugin-import": "^2.29.1",
Expand Down Expand Up @@ -150,6 +150,7 @@
"vite": "^5.3.5",
"vite-plugin-dts": "4.0.0-beta.2",
"vite-plugin-lightningcss": "^0.0.5",
"vitest": "^2.0.5"
"vitest": "^2.0.5",
"vue-eslint-parser": "^9.4.3"
}
}
525 changes: 270 additions & 255 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
create: context => ({
TSEnumDeclaration: node => {
if (
node.members.length > 1 &&
node.members.every(({ initializer }) => initializer)
node.body.members.length > 1 &&
node.body.members.every(({ initializer }) => initializer)
) {
let options = complete(context.options.at(0), {
partitionByComment: false,
Expand All @@ -102,7 +102,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
let sourceCode = getSourceCode(context)
let partitionComment = options.partitionByComment

let formattedMembers: SortingNode[][] = node.members.reduce(
let formattedMembers: SortingNode[][] = node.body.members.reduce(
(accumulator: SortingNode[][], member) => {
let comment = getCommentBefore(member, sourceCode)

Expand Down
219 changes: 103 additions & 116 deletions rules/sort-jsx-props.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { TSESTree } from '@typescript-eslint/types'

import { minimatch } from 'minimatch'
import path from 'node:path'

import type { SortingNode } from '../typings'

Expand Down Expand Up @@ -125,135 +124,123 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
customGroups: {},
},
],
create: context => {
if (
['.svelte', '.astro', '.vue'].includes(path.extname(context.filename))
) {
return {}
}
return {
JSXElement: node => {
if (node.openingElement.attributes.length > 1) {
let options = complete(context.options.at(0), {
type: 'alphabetical',
ignorePattern: [],
ignoreCase: true,
customGroups: {},
order: 'asc',
groups: [],
} as const)

let sourceCode = getSourceCode(context)

let shouldIgnore = false
if (options.ignorePattern.length) {
let tagName = sourceCode.text.slice(
...node.openingElement.name.range,
)
shouldIgnore = options.ignorePattern.some(pattern =>
minimatch(tagName, pattern),
)
}

if (!shouldIgnore && node.openingElement.attributes.length > 1) {
let parts: SortingNode[][] = node.openingElement.attributes.reduce(
(
accumulator: SortingNode[][],
attribute: TSESTree.JSXSpreadAttribute | TSESTree.JSXAttribute,
) => {
if (attribute.type === 'JSXSpreadAttribute') {
accumulator.push([])
return accumulator
}

let name =
attribute.name.type === 'JSXNamespacedName'
? `${attribute.name.namespace.name}:${attribute.name.name.name}`
: attribute.name.name

let { getGroup, defineGroup, setCustomGroups } = useGroups(
options.groups,
)
create: context => ({
JSXElement: node => {
if (node.openingElement.attributes.length > 1) {
let options = complete(context.options.at(0), {
type: 'alphabetical',
ignorePattern: [],
ignoreCase: true,
customGroups: {},
order: 'asc',
groups: [],
} as const)

let sourceCode = getSourceCode(context)

let shouldIgnore = false
if (options.ignorePattern.length) {
let tagName = sourceCode.text.slice(...node.openingElement.name.range)
shouldIgnore = options.ignorePattern.some(pattern =>
minimatch(tagName, pattern),
)
}

setCustomGroups(options.customGroups, name)
if (!shouldIgnore && node.openingElement.attributes.length > 1) {
let parts: SortingNode[][] = node.openingElement.attributes.reduce(
(
accumulator: SortingNode[][],
attribute: TSESTree.JSXSpreadAttribute | TSESTree.JSXAttribute,
) => {
if (attribute.type === 'JSXSpreadAttribute') {
accumulator.push([])
return accumulator
}

if (attribute.value === null) {
defineGroup('shorthand')
}
let name =
attribute.name.type === 'JSXNamespacedName'
? `${attribute.name.namespace.name}:${attribute.name.name.name}`
: attribute.name.name

if (attribute.loc.start.line !== attribute.loc.end.line) {
defineGroup('multiline')
}
let { getGroup, defineGroup, setCustomGroups } = useGroups(
options.groups,
)

let jsxNode = {
size: rangeToDiff(attribute.range),
node: structuredClone(attribute),
group: getGroup(),
name,
}
setCustomGroups(options.customGroups, name)

accumulator.at(-1)!.push(jsxNode)
if (attribute.value === null) {
defineGroup('shorthand')
}

return accumulator
},
[[]],
)
if (attribute.loc.start.line !== attribute.loc.end.line) {
defineGroup('multiline')
}

for (let nodes of parts) {
pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)
let jsxNode = {
size: rangeToDiff(attribute.range),
node: structuredClone(attribute),
group: getGroup(),
name,
}

if (
leftNum > rightNum ||
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedJSXPropsOrder',
data: {
left: left.name,
right: right.name,
},
node: right.node,
fix: fixer => {
let grouped: {
[key: string]: SortingNode[]
} = {}
accumulator.at(-1)!.push(jsxNode)

for (let currentNode of nodes) {
let groupNum = getGroupNumber(
options.groups,
currentNode,
return accumulator
},
[[]],
)

for (let nodes of parts) {
pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)

if (
leftNum > rightNum ||
(leftNum === rightNum &&
isPositive(compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedJSXPropsOrder',
data: {
left: left.name,
right: right.name,
},
node: right.node,
fix: fixer => {
let grouped: {
[key: string]: SortingNode[]
} = {}

for (let currentNode of nodes) {
let groupNum = getGroupNumber(options.groups, currentNode)

if (!(groupNum in grouped)) {
grouped[groupNum] = [currentNode]
} else {
grouped[groupNum] = sortNodes(
[...grouped[groupNum], currentNode],
options,
)

if (!(groupNum in grouped)) {
grouped[groupNum] = [currentNode]
} else {
grouped[groupNum] = sortNodes(
[...grouped[groupNum], currentNode],
options,
)
}
}
}

let sortedNodes: SortingNode[] = []
let sortedNodes: SortingNode[] = []

for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}
for (let group of Object.keys(grouped).sort(
(a, b) => Number(a) - Number(b),
)) {
sortedNodes.push(...sortNodes(grouped[group], options))
}

return makeFixes(fixer, nodes, sortedNodes, sourceCode)
},
})
}
})
}
return makeFixes(fixer, nodes, sortedNodes, sourceCode)
},
})
}
})
}
}
},
}
},
}
},
}),
})
Empty file added test/fixtures/file.astro
Empty file.
Empty file added test/fixtures/file.svelte
Empty file.
Empty file added test/fixtures/file.ts
Empty file.
Empty file added test/fixtures/file.vue
Empty file.
Empty file added test/fixtures/react.tsx
Empty file.
12 changes: 12 additions & 0 deletions test/fixtures/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": ["es2015", "es2017", "esnext"],
"experimentalDecorators": true,
"esModuleInterop": true,
"module": "commonjs",
"jsx": "preserve",
"target": "es5",
"strict": true
},
"include": ["file.svelte", "file.astro", "react.tsx", "file.vue", "file.ts"]
}
4 changes: 1 addition & 3 deletions test/sort-array-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ describe(ruleName, () => {
RuleTester.itSkip = it.skip
RuleTester.it = it

let ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
})
let ruleTester = new RuleTester()

describe(`${ruleName}: sorting by alphabetical order`, () => {
let type = 'alphabetical-order'
Expand Down
Loading

0 comments on commit 05b6502

Please sign in to comment.