@borela-tech/eslint-config 2.2.2 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +308 -19
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +3589 -811
- package/dist/index.mjs.map +1 -1
- package/jest.config.ts +19 -0
- package/package.json +17 -8
- package/vite.config.ts +41 -0
- package/bin/build +0 -9
- package/bin/lint +0 -14
- package/bin/publish +0 -12
- package/bin/test +0 -12
- package/bin/typecheck +0 -8
- package/dist/index.d.mts +0 -7
- package/src/index.ts +0 -149
- package/src/lib/ReExportDeclaration.ts +0 -5
- package/src/lib/ReplacementRange.ts +0 -4
- package/src/lib/compare.ts +0 -3
- package/src/rules/__tests__/dedent/countLeadingSpaces.ts +0 -4
- package/src/rules/__tests__/dedent/findMinIndent.ts +0 -7
- package/src/rules/__tests__/dedent/index.ts +0 -17
- package/src/rules/__tests__/dedent/interpolate.ts +0 -11
- package/src/rules/__tests__/dedent/removeEmptyPrefix.ts +0 -6
- package/src/rules/__tests__/dedent/removeEmptySuffix.ts +0 -6
- package/src/rules/__tests__/dedent/removeIndent.ts +0 -3
- package/src/rules/__tests__/importsAndReExportsAtTop.test.ts +0 -88
- package/src/rules/__tests__/individualImports.test.ts +0 -44
- package/src/rules/__tests__/individualReExports.test.ts +0 -64
- package/src/rules/__tests__/multilineUnionTypes.test.ts +0 -75
- package/src/rules/__tests__/singleLineImports.test.ts +0 -129
- package/src/rules/__tests__/singleLineReExports.test.ts +0 -100
- package/src/rules/__tests__/sortedImports.test.ts +0 -227
- package/src/rules/__tests__/sortedReExports.test.ts +0 -220
- package/src/rules/importsAndReExportsAtTop/CategorizedStatements.ts +0 -8
- package/src/rules/importsAndReExportsAtTop/StatementIndices.ts +0 -5
- package/src/rules/importsAndReExportsAtTop/categorizeStatements.ts +0 -28
- package/src/rules/importsAndReExportsAtTop/findStatementIndices.ts +0 -25
- package/src/rules/importsAndReExportsAtTop/generateSortedText.ts +0 -16
- package/src/rules/importsAndReExportsAtTop/getStatementType.ts +0 -17
- package/src/rules/importsAndReExportsAtTop/hasViolation.ts +0 -25
- package/src/rules/importsAndReExportsAtTop/index.ts +0 -45
- package/src/rules/importsAndReExportsAtTop/isImportDeclaration.ts +0 -7
- package/src/rules/importsAndReExportsAtTop/isReExport.ts +0 -12
- package/src/rules/importsAndReExportsAtTop/statementType.ts +0 -4
- package/src/rules/individualImports.ts +0 -38
- package/src/rules/individualReExports.ts +0 -51
- package/src/rules/multilineUnionTypes/createFix.ts +0 -13
- package/src/rules/multilineUnionTypes/index.ts +0 -52
- package/src/rules/multilineUnionTypes/isMultiline.ts +0 -6
- package/src/rules/singleLineImports/createFix.ts +0 -23
- package/src/rules/singleLineImports/formatAttributes.ts +0 -20
- package/src/rules/singleLineImports/formatNamed.ts +0 -9
- package/src/rules/singleLineImports/formatSpecifiers.ts +0 -32
- package/src/rules/singleLineImports/index.ts +0 -34
- package/src/rules/singleLineImports/isMultiline.ts +0 -6
- package/src/rules/singleLineReExports/createFix.ts +0 -29
- package/src/rules/singleLineReExports/index.ts +0 -48
- package/src/rules/singleLineReExports/isMultiline.ts +0 -6
- package/src/rules/sortedImports/CategorizedImport.ts +0 -8
- package/src/rules/sortedImports/ImportError.ts +0 -9
- package/src/rules/sortedImports/ImportGroup.ts +0 -6
- package/src/rules/sortedImports/ImportGroupOrder.ts +0 -9
- package/src/rules/sortedImports/areSpecifiersSorted.ts +0 -9
- package/src/rules/sortedImports/categorizeImport.ts +0 -23
- package/src/rules/sortedImports/categorizeImports.ts +0 -12
- package/src/rules/sortedImports/checkAlphabeticalSorting.ts +0 -23
- package/src/rules/sortedImports/checkGroupOrdering.ts +0 -21
- package/src/rules/sortedImports/checkSpecifiersSorting.ts +0 -21
- package/src/rules/sortedImports/createFix/buildSortedCode.ts +0 -23
- package/src/rules/sortedImports/createFix/findLastImportIndex.ts +0 -12
- package/src/rules/sortedImports/createFix/formatNamedImport.ts +0 -21
- package/src/rules/sortedImports/createFix/getReplacementRange.ts +0 -14
- package/src/rules/sortedImports/createFix/groupImportsByType.ts +0 -19
- package/src/rules/sortedImports/createFix/index.ts +0 -47
- package/src/rules/sortedImports/createFix/sortImportGroups.ts +0 -13
- package/src/rules/sortedImports/getImportGroups.ts +0 -23
- package/src/rules/sortedImports/getNamedSpecifiers.ts +0 -7
- package/src/rules/sortedImports/getSortKey.ts +0 -26
- package/src/rules/sortedImports/getSpecifierName.ts +0 -7
- package/src/rules/sortedImports/index.ts +0 -63
- package/src/rules/sortedImports/sortSpecifiersText.ts +0 -16
- package/src/rules/sortedReExports/CategorizedNamedReExport.ts +0 -6
- package/src/rules/sortedReExports/CategorizedReExport.ts +0 -26
- package/src/rules/sortedReExports/ReExportError.ts +0 -9
- package/src/rules/sortedReExports/ReExportGroup.ts +0 -5
- package/src/rules/sortedReExports/ReExportGroupOrder.ts +0 -8
- package/src/rules/sortedReExports/areSpecifiersSorted.ts +0 -9
- package/src/rules/sortedReExports/categorizeReExport.ts +0 -21
- package/src/rules/sortedReExports/categorizeReExports.ts +0 -14
- package/src/rules/sortedReExports/checkAlphabeticalSorting.ts +0 -25
- package/src/rules/sortedReExports/checkGroupOrdering.ts +0 -21
- package/src/rules/sortedReExports/checkSpecifiersSorting.ts +0 -23
- package/src/rules/sortedReExports/createFix/buildSortedCode.ts +0 -29
- package/src/rules/sortedReExports/createFix/findFirstExportIndex.ts +0 -11
- package/src/rules/sortedReExports/createFix/findLastExportIndex.ts +0 -12
- package/src/rules/sortedReExports/createFix/formatNamedReExport.ts +0 -21
- package/src/rules/sortedReExports/createFix/getReplacementRange.ts +0 -22
- package/src/rules/sortedReExports/createFix/groupReExportsByType.ts +0 -18
- package/src/rules/sortedReExports/createFix/index.ts +0 -47
- package/src/rules/sortedReExports/createFix/sortExportGroups.ts +0 -12
- package/src/rules/sortedReExports/getNamedSpecifiers.ts +0 -9
- package/src/rules/sortedReExports/getReExportGroups.ts +0 -33
- package/src/rules/sortedReExports/getSortKey.ts +0 -22
- package/src/rules/sortedReExports/getSpecifierName.ts +0 -7
- package/src/rules/sortedReExports/index.ts +0 -63
- package/src/rules/sortedReExports/isNamedReExport.ts +0 -6
- package/src/rules/sortedReExports/sortSpecifiersText.ts +0 -16
- package/tsdown.config.ts +0 -22
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import {compare} from '@lib/compare'
|
|
2
|
-
import {getSpecifierName} from './getSpecifierName'
|
|
3
|
-
import type {TSESTree} from '@typescript-eslint/types'
|
|
4
|
-
|
|
5
|
-
export function areSpecifiersSorted(specifiers: TSESTree.ExportSpecifier[]): boolean {
|
|
6
|
-
const names = specifiers.map(s => getSpecifierName(s))
|
|
7
|
-
const sorted = [...names].sort((a, b) => compare(a, b))
|
|
8
|
-
return names.every((name, i) => name === sorted[i])
|
|
9
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type {ReExportDeclaration} from '@lib/ReExportDeclaration'
|
|
2
|
-
import type {ReExportGroup} from './ReExportGroup'
|
|
3
|
-
|
|
4
|
-
export function categorizeReExport(
|
|
5
|
-
declaration: ReExportDeclaration,
|
|
6
|
-
): ReExportGroup {
|
|
7
|
-
// Example: export * from 'module' or export * as ns from 'module'
|
|
8
|
-
if (declaration.type === 'ExportAllDeclaration') {
|
|
9
|
-
// Export * as ns from 'module' has an 'exported' property
|
|
10
|
-
if (declaration.exported)
|
|
11
|
-
return 're-export-namespace'
|
|
12
|
-
return 're-export-all'
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// Example: export type {Type} from 'module'
|
|
16
|
-
if (declaration.exportKind === 'type')
|
|
17
|
-
return 're-export-type'
|
|
18
|
-
|
|
19
|
-
// Example: export {value} from 'module'
|
|
20
|
-
return 're-export-named'
|
|
21
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import {categorizeReExport} from './categorizeReExport'
|
|
2
|
-
import {getSortKey} from './getSortKey'
|
|
3
|
-
import type {CategorizedReExport} from './CategorizedReExport'
|
|
4
|
-
import type {ReExportDeclaration} from '@lib/ReExportDeclaration'
|
|
5
|
-
|
|
6
|
-
export function categorizeReExports(declarations: ReExportDeclaration[]): CategorizedReExport[] {
|
|
7
|
-
return declarations.map(declaration => {
|
|
8
|
-
return {
|
|
9
|
-
declaration,
|
|
10
|
-
group: categorizeReExport(declaration),
|
|
11
|
-
sortKey: getSortKey(declaration),
|
|
12
|
-
} as CategorizedReExport
|
|
13
|
-
})
|
|
14
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {compare} from '@lib/compare'
|
|
2
|
-
import {reExportGroupOrder} from './ReExportGroupOrder'
|
|
3
|
-
import type {CategorizedReExport} from './CategorizedReExport'
|
|
4
|
-
import type {ReExportError} from './ReExportError'
|
|
5
|
-
|
|
6
|
-
export function checkAlphabeticalSorting(categorized: CategorizedReExport[]): ReExportError[] {
|
|
7
|
-
const errors: ReExportError[] = []
|
|
8
|
-
|
|
9
|
-
for (const group of reExportGroupOrder) {
|
|
10
|
-
const groupReExports = categorized.filter(c => c.group === group)
|
|
11
|
-
const sorted = [...groupReExports].sort((a, b) =>
|
|
12
|
-
compare(a.sortKey, b.sortKey),
|
|
13
|
-
)
|
|
14
|
-
for (let i = 0; i < groupReExports.length; i++) {
|
|
15
|
-
if (groupReExports[i] !== sorted[i]) {
|
|
16
|
-
errors.push({
|
|
17
|
-
node: groupReExports[i].declaration,
|
|
18
|
-
messageId: 'sortedReExports',
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return errors
|
|
25
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {reExportGroupOrder} from './ReExportGroupOrder'
|
|
2
|
-
import type {CategorizedReExport} from './CategorizedReExport'
|
|
3
|
-
import type {ReExportError} from './ReExportError'
|
|
4
|
-
|
|
5
|
-
export function checkGroupOrdering(categorized: CategorizedReExport[]): ReExportError[] {
|
|
6
|
-
const errors: ReExportError[] = []
|
|
7
|
-
|
|
8
|
-
let currentGroupIndex = -1
|
|
9
|
-
for (const {declaration, group} of categorized) {
|
|
10
|
-
const groupIndex = reExportGroupOrder.indexOf(group)
|
|
11
|
-
if (groupIndex < currentGroupIndex) {
|
|
12
|
-
errors.push({
|
|
13
|
-
node: declaration,
|
|
14
|
-
messageId: 'wrongGroup',
|
|
15
|
-
})
|
|
16
|
-
} else
|
|
17
|
-
currentGroupIndex = groupIndex
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return errors
|
|
21
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import {areSpecifiersSorted} from './areSpecifiersSorted'
|
|
2
|
-
import {getNamedSpecifiers} from './getNamedSpecifiers'
|
|
3
|
-
import {isNamedReExport} from './isNamedReExport'
|
|
4
|
-
import type {CategorizedReExport} from './CategorizedReExport'
|
|
5
|
-
import type {ReExportError} from './ReExportError'
|
|
6
|
-
|
|
7
|
-
export function checkSpecifiersSorting(categorized: CategorizedReExport[]): ReExportError[] {
|
|
8
|
-
const errors: ReExportError[] = []
|
|
9
|
-
const namedReExports = categorized.filter(isNamedReExport)
|
|
10
|
-
|
|
11
|
-
for (const {declaration} of namedReExports) {
|
|
12
|
-
const specifiers = getNamedSpecifiers(declaration)
|
|
13
|
-
const isSorted = areSpecifiersSorted(specifiers)
|
|
14
|
-
if (specifiers.length > 1 && !isSorted) {
|
|
15
|
-
errors.push({
|
|
16
|
-
node: declaration,
|
|
17
|
-
messageId: 'sortedNames',
|
|
18
|
-
})
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return errors
|
|
23
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {formatNamedReExport} from './formatNamedReExport'
|
|
2
|
-
import {isNamedReExport} from '../isNamedReExport'
|
|
3
|
-
import {reExportGroupOrder} from '../ReExportGroupOrder'
|
|
4
|
-
import type {CategorizedReExport} from '../CategorizedReExport'
|
|
5
|
-
import type {ReExportGroup} from '../ReExportGroup'
|
|
6
|
-
import type {TSESLint} from '@typescript-eslint/utils'
|
|
7
|
-
|
|
8
|
-
export function buildSortedCode(
|
|
9
|
-
grouped: Record<ReExportGroup, CategorizedReExport[]>,
|
|
10
|
-
sourceCode: TSESLint.SourceCode,
|
|
11
|
-
): string[] {
|
|
12
|
-
const sortedCode: string[] = []
|
|
13
|
-
|
|
14
|
-
for (const group of reExportGroupOrder) {
|
|
15
|
-
for (const item of grouped[group]) {
|
|
16
|
-
if (isNamedReExport(item)) {
|
|
17
|
-
sortedCode.push(
|
|
18
|
-
formatNamedReExport(
|
|
19
|
-
item.declaration,
|
|
20
|
-
sourceCode,
|
|
21
|
-
),
|
|
22
|
-
)
|
|
23
|
-
} else
|
|
24
|
-
sortedCode.push(sourceCode.getText(item.declaration))
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return sortedCode
|
|
29
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type {TSESTree} from '@typescript-eslint/types'
|
|
2
|
-
|
|
3
|
-
export function findFirstExportIndex(programBody: TSESTree.ProgramStatement[]): number {
|
|
4
|
-
for (let i = 0; i < programBody.length; i++) {
|
|
5
|
-
if (programBody[i].type === 'ExportNamedDeclaration'
|
|
6
|
-
|| programBody[i].type === 'ExportAllDeclaration') {
|
|
7
|
-
return i
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return -1
|
|
11
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type {TSESTree} from '@typescript-eslint/types'
|
|
2
|
-
|
|
3
|
-
export function findLastExportIndex(programBody: TSESTree.ProgramStatement[]): number {
|
|
4
|
-
let lastIndex = -1
|
|
5
|
-
for (let i = 0; i < programBody.length; i++) {
|
|
6
|
-
if (programBody[i].type === 'ExportNamedDeclaration'
|
|
7
|
-
|| programBody[i].type === 'ExportAllDeclaration') {
|
|
8
|
-
lastIndex = i
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
return lastIndex
|
|
12
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import {areSpecifiersSorted} from '../areSpecifiersSorted'
|
|
2
|
-
import {getNamedSpecifiers} from '../getNamedSpecifiers'
|
|
3
|
-
import {sortSpecifiersText} from '../sortSpecifiersText'
|
|
4
|
-
import type {TSESLint} from '@typescript-eslint/utils'
|
|
5
|
-
import type {TSESTree} from '@typescript-eslint/utils'
|
|
6
|
-
|
|
7
|
-
export function formatNamedReExport(
|
|
8
|
-
declaration: TSESTree.ExportNamedDeclaration,
|
|
9
|
-
sourceCode: TSESLint.SourceCode,
|
|
10
|
-
): string {
|
|
11
|
-
const specifiers = getNamedSpecifiers(declaration)
|
|
12
|
-
|
|
13
|
-
if (specifiers.length > 1 && !areSpecifiersSorted(specifiers)) {
|
|
14
|
-
const sortedSpecifiers = sortSpecifiersText(specifiers, sourceCode)
|
|
15
|
-
const source = declaration.source!.value
|
|
16
|
-
const prefix = declaration.exportKind === 'type' ? 'export type ' : 'export '
|
|
17
|
-
return `${prefix}{${sortedSpecifiers}} from '${source}'`
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return sourceCode.getText(declaration)
|
|
21
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {findFirstExportIndex} from './findFirstExportIndex'
|
|
2
|
-
import {findLastExportIndex} from './findLastExportIndex'
|
|
3
|
-
import type {ReplacementRange} from '@lib/ReplacementRange'
|
|
4
|
-
import type {TSESTree} from '@typescript-eslint/types'
|
|
5
|
-
|
|
6
|
-
export function getReplacementRange(
|
|
7
|
-
programBody: TSESTree.ProgramStatement[],
|
|
8
|
-
): ReplacementRange {
|
|
9
|
-
const firstIndex = findFirstExportIndex(programBody)
|
|
10
|
-
const lastIndex = findLastExportIndex(programBody)
|
|
11
|
-
|
|
12
|
-
if (firstIndex === -1 || lastIndex === -1)
|
|
13
|
-
return {start: 0, end: 0}
|
|
14
|
-
|
|
15
|
-
const firstExport = programBody[firstIndex]
|
|
16
|
-
const lastExport = programBody[lastIndex]
|
|
17
|
-
|
|
18
|
-
const start = firstExport.range[0]
|
|
19
|
-
const end = lastExport.range[1]
|
|
20
|
-
|
|
21
|
-
return {start, end}
|
|
22
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type {CategorizedReExport} from '../CategorizedReExport'
|
|
2
|
-
import type {ReExportGroup} from '../ReExportGroup'
|
|
3
|
-
|
|
4
|
-
export function groupReExportsByType(
|
|
5
|
-
categorized: CategorizedReExport[],
|
|
6
|
-
): Record<ReExportGroup, CategorizedReExport[]> {
|
|
7
|
-
const grouped: Record<ReExportGroup, CategorizedReExport[]> = {
|
|
8
|
-
're-export-all': [],
|
|
9
|
-
're-export-namespace': [],
|
|
10
|
-
're-export-named': [],
|
|
11
|
-
're-export-type': [],
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
for (const item of categorized)
|
|
15
|
-
grouped[item.group].push(item)
|
|
16
|
-
|
|
17
|
-
return grouped
|
|
18
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import {buildSortedCode} from './buildSortedCode'
|
|
2
|
-
import {categorizeReExports} from '../categorizeReExports'
|
|
3
|
-
import {groupReExportsByType} from './groupReExportsByType'
|
|
4
|
-
import {sortExportGroups} from './sortExportGroups'
|
|
5
|
-
import type {ReExportDeclaration} from '@lib/ReExportDeclaration'
|
|
6
|
-
import type {TSESLint} from '@typescript-eslint/utils'
|
|
7
|
-
|
|
8
|
-
function createFixForGroup(
|
|
9
|
-
fixer: TSESLint.RuleFixer,
|
|
10
|
-
reExportDeclarations: ReExportDeclaration[],
|
|
11
|
-
sourceCode: TSESLint.SourceCode,
|
|
12
|
-
) {
|
|
13
|
-
if (reExportDeclarations.length === 0)
|
|
14
|
-
return null
|
|
15
|
-
|
|
16
|
-
const categorized = categorizeReExports(reExportDeclarations)
|
|
17
|
-
const grouped = groupReExportsByType(categorized)
|
|
18
|
-
|
|
19
|
-
sortExportGroups(grouped)
|
|
20
|
-
|
|
21
|
-
const sortedCode = buildSortedCode(grouped, sourceCode)
|
|
22
|
-
.join('\n')
|
|
23
|
-
|
|
24
|
-
const firstReExport = reExportDeclarations[0]
|
|
25
|
-
const lastReExport = reExportDeclarations[reExportDeclarations.length - 1]
|
|
26
|
-
|
|
27
|
-
return fixer.replaceTextRange(
|
|
28
|
-
[firstReExport.range![0], lastReExport.range![1]],
|
|
29
|
-
sortedCode,
|
|
30
|
-
)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function createFix(
|
|
34
|
-
fixer: TSESLint.RuleFixer,
|
|
35
|
-
reExportGroups: ReExportDeclaration[][],
|
|
36
|
-
sourceCode: TSESLint.SourceCode,
|
|
37
|
-
): TSESLint.RuleFix[] {
|
|
38
|
-
const fixes: TSESLint.RuleFix[] = []
|
|
39
|
-
|
|
40
|
-
for (const group of reExportGroups) {
|
|
41
|
-
const fix = createFixForGroup(fixer, group, sourceCode)
|
|
42
|
-
if (fix)
|
|
43
|
-
fixes.push(fix)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return fixes
|
|
47
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import {compare} from '@lib/compare'
|
|
2
|
-
import type {CategorizedReExport} from '../CategorizedReExport'
|
|
3
|
-
import type {ReExportGroup} from '../ReExportGroup'
|
|
4
|
-
|
|
5
|
-
export function sortExportGroups(
|
|
6
|
-
grouped: Record<ReExportGroup, CategorizedReExport[]>,
|
|
7
|
-
): void {
|
|
8
|
-
grouped['re-export-all'].sort((a, b) => compare(a.sortKey, b.sortKey))
|
|
9
|
-
grouped['re-export-namespace'].sort((a, b) => compare(a.sortKey, b.sortKey))
|
|
10
|
-
grouped['re-export-named'].sort((a, b) => compare(a.sortKey, b.sortKey))
|
|
11
|
-
grouped['re-export-type'].sort((a, b) => compare(a.sortKey, b.sortKey))
|
|
12
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type {TSESTree} from '@typescript-eslint/types'
|
|
2
|
-
|
|
3
|
-
export function getNamedSpecifiers(
|
|
4
|
-
declaration: TSESTree.ExportNamedDeclaration,
|
|
5
|
-
): TSESTree.ExportSpecifier[] {
|
|
6
|
-
return declaration.specifiers.filter(
|
|
7
|
-
s => s.type === 'ExportSpecifier' && s.local.type === 'Identifier',
|
|
8
|
-
)
|
|
9
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type {ReExportDeclaration} from '@lib/ReExportDeclaration'
|
|
2
|
-
import type {TSESTree} from '@typescript-eslint/types'
|
|
3
|
-
|
|
4
|
-
function isReExportDeclaration(
|
|
5
|
-
statement: TSESTree.ProgramStatement,
|
|
6
|
-
): statement is ReExportDeclaration {
|
|
7
|
-
return (statement.type === 'ExportNamedDeclaration' && statement.source !== null)
|
|
8
|
-
|| statement.type === 'ExportAllDeclaration'
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function getReExportGroups(
|
|
12
|
-
programBody: TSESTree.ProgramStatement[],
|
|
13
|
-
): ReExportDeclaration[][] {
|
|
14
|
-
const groups: ReExportDeclaration[][] = []
|
|
15
|
-
let currentGroup: ReExportDeclaration[] = []
|
|
16
|
-
|
|
17
|
-
for (const statement of programBody) {
|
|
18
|
-
if (isReExportDeclaration(statement)) {
|
|
19
|
-
currentGroup.push(statement)
|
|
20
|
-
continue
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (currentGroup.length > 0) {
|
|
24
|
-
groups.push(currentGroup)
|
|
25
|
-
currentGroup = []
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (currentGroup.length > 0)
|
|
30
|
-
groups.push(currentGroup)
|
|
31
|
-
|
|
32
|
-
return groups
|
|
33
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import {categorizeReExport} from './categorizeReExport'
|
|
2
|
-
import type {ReExportDeclaration} from '@lib/ReExportDeclaration'
|
|
3
|
-
|
|
4
|
-
export function getSortKey(declaration: ReExportDeclaration): string {
|
|
5
|
-
const group = categorizeReExport(declaration)
|
|
6
|
-
|
|
7
|
-
if (declaration.type === 'ExportAllDeclaration') {
|
|
8
|
-
if (group === 're-export-namespace') {
|
|
9
|
-
if (declaration.exported?.type === 'Identifier')
|
|
10
|
-
return `*${declaration.exported.name}`
|
|
11
|
-
}
|
|
12
|
-
return declaration.source.value
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const specifier = declaration.specifiers[0]
|
|
16
|
-
if (!specifier)
|
|
17
|
-
return ''
|
|
18
|
-
|
|
19
|
-
return specifier.local.type === 'Identifier'
|
|
20
|
-
? specifier.local.name
|
|
21
|
-
: specifier.local.value
|
|
22
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import {categorizeReExports} from './categorizeReExports'
|
|
2
|
-
import {checkAlphabeticalSorting} from './checkAlphabeticalSorting'
|
|
3
|
-
import {checkGroupOrdering} from './checkGroupOrdering'
|
|
4
|
-
import {checkSpecifiersSorting} from './checkSpecifiersSorting'
|
|
5
|
-
import {createFix} from './createFix'
|
|
6
|
-
import {getReExportGroups} from './getReExportGroups'
|
|
7
|
-
import type {ReExportError} from './ReExportError'
|
|
8
|
-
import type {TSESLint} from '@typescript-eslint/utils'
|
|
9
|
-
|
|
10
|
-
type MessageIds =
|
|
11
|
-
| 'sortedReExports'
|
|
12
|
-
| 'sortedNames'
|
|
13
|
-
| 'wrongGroup'
|
|
14
|
-
|
|
15
|
-
export const sortedReExports: TSESLint.RuleModule<MessageIds, []> = {
|
|
16
|
-
meta: {
|
|
17
|
-
docs: {
|
|
18
|
-
description: 'Enforce sorted exports alphabetically',
|
|
19
|
-
},
|
|
20
|
-
fixable: 'code',
|
|
21
|
-
messages: {
|
|
22
|
-
sortedReExports: 'Exports should be sorted alphabetically',
|
|
23
|
-
sortedNames: 'Named exports should be sorted alphabetically',
|
|
24
|
-
wrongGroup: 'Export is in wrong group',
|
|
25
|
-
},
|
|
26
|
-
schema: [],
|
|
27
|
-
type: 'suggestion',
|
|
28
|
-
},
|
|
29
|
-
create(context) {
|
|
30
|
-
return {
|
|
31
|
-
Program(node) {
|
|
32
|
-
const body = node.body
|
|
33
|
-
const reExportGroups = getReExportGroups(body)
|
|
34
|
-
if (reExportGroups.length === 0)
|
|
35
|
-
return
|
|
36
|
-
|
|
37
|
-
const allErrors: ReExportError[] = []
|
|
38
|
-
|
|
39
|
-
// Check each re-export group independently
|
|
40
|
-
for (const group of reExportGroups) {
|
|
41
|
-
const categorized = categorizeReExports(group)
|
|
42
|
-
const errors: ReExportError[] = [
|
|
43
|
-
...checkGroupOrdering(categorized),
|
|
44
|
-
...checkAlphabeticalSorting(categorized),
|
|
45
|
-
...checkSpecifiersSorting(categorized),
|
|
46
|
-
]
|
|
47
|
-
allErrors.push(...errors)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
for (const error of allErrors) {
|
|
51
|
-
context.report({
|
|
52
|
-
node: error.node,
|
|
53
|
-
messageId: error.messageId,
|
|
54
|
-
fix(fixer) {
|
|
55
|
-
const sourceCode = context.sourceCode
|
|
56
|
-
return createFix(fixer, reExportGroups, sourceCode)
|
|
57
|
-
},
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type {CategorizedNamedReExport} from './CategorizedNamedReExport'
|
|
2
|
-
import type {CategorizedReExport} from './CategorizedReExport'
|
|
3
|
-
|
|
4
|
-
export function isNamedReExport(x: CategorizedReExport): x is CategorizedNamedReExport {
|
|
5
|
-
return x.group !== 're-export-all' && x.group !== 're-export-namespace'
|
|
6
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import {compare} from '@lib/compare'
|
|
2
|
-
import {getSpecifierName} from './getSpecifierName'
|
|
3
|
-
import type {TSESLint} from '@typescript-eslint/utils'
|
|
4
|
-
import type {TSESTree} from '@typescript-eslint/utils'
|
|
5
|
-
|
|
6
|
-
export function sortSpecifiersText(
|
|
7
|
-
specifiers: TSESTree.ExportSpecifier[],
|
|
8
|
-
sourceCode: TSESLint.SourceCode,
|
|
9
|
-
): string {
|
|
10
|
-
const sorted = [...specifiers].sort((a, b) => {
|
|
11
|
-
const nameA = getSpecifierName(a)
|
|
12
|
-
const nameB = getSpecifierName(b)
|
|
13
|
-
return compare(nameA, nameB)
|
|
14
|
-
})
|
|
15
|
-
return sorted.map(s => sourceCode.getText(s)).join(', ')
|
|
16
|
-
}
|
package/tsdown.config.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import {defineConfig} from 'tsdown'
|
|
3
|
-
|
|
4
|
-
export default defineConfig(options => {
|
|
5
|
-
return {
|
|
6
|
-
...options,
|
|
7
|
-
clean: true,
|
|
8
|
-
deps: {
|
|
9
|
-
skipNodeModulesBundle: true,
|
|
10
|
-
},
|
|
11
|
-
dts: true,
|
|
12
|
-
entry: {
|
|
13
|
-
index: 'src/index.ts',
|
|
14
|
-
},
|
|
15
|
-
format: ['esm'],
|
|
16
|
-
splitting: false,
|
|
17
|
-
sourcemap: true,
|
|
18
|
-
alias: {
|
|
19
|
-
'@lib': path.resolve(process.cwd(), 'src/lib'),
|
|
20
|
-
},
|
|
21
|
-
}
|
|
22
|
-
})
|