Skip to content

Commit 04657bc

Browse files
committed
Fix type inconsistencies/casting
1 parent 5f0ddb0 commit 04657bc

17 files changed

+102
-97
lines changed

src/fixer.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
import { TSESLint } from '@typescript-eslint/utils'
22
import { ReportFixFunction } from '@typescript-eslint/utils/ts-eslint'
33

4-
import { AllRuleOptions, CreateReporterArgs, Node, SourceCode, TSType } from './types'
4+
import { AllRuleOptions, CreateReporterArgs, NodeOrToken, SourceCode } from './types'
55
import { getMemoized, memoize } from './utils/memo'
66
import { getBodyRange, getFixedBodyText } from './utils/sourcecode'
77

88
export const getFixerFunction = (
99
baseMemoKey: string,
1010
createReporterArgs: Pick<CreateReporterArgs<string, AllRuleOptions>, 'context'>,
11-
body: TSType[],
12-
sortedBody: TSType[],
11+
body: NodeOrToken[],
12+
sortedBody: NodeOrToken[],
1313
): ReportFixFunction =>
1414
function* (fixer: TSESLint.RuleFixer) {
1515
const sourceCode = createReporterArgs.context.sourceCode as SourceCode
1616

1717
const bodyRange = memoize(`bodyRange_${baseMemoKey}`, () =>
18-
getBodyRange(sourceCode, body as unknown as Node[]),
18+
getBodyRange(sourceCode, body),
1919
)
2020

2121
const fixedBodyTextMemoKey = `fixedBodyText_${baseMemoKey}`
2222
// Replace the entire body with the sorted body
2323
const fixedBodyText =
2424
getMemoized(fixedBodyTextMemoKey) ??
25-
memoize(fixedBodyTextMemoKey, () =>
26-
getFixedBodyText(
27-
sourceCode,
28-
sortedBody as unknown as Node[],
29-
body as unknown as Node[],
30-
),
31-
)
25+
memoize(fixedBodyTextMemoKey, () => getFixedBodyText(sourceCode, sortedBody, body))
3226

3327
yield fixer.replaceTextRange(bodyRange, fixedBodyText)
3428
}

src/plugin.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { TSESTree } from '@typescript-eslint/utils'
2-
31
import { getOptions } from './common/options'
42
import { getFixerFunction } from './fixer'
53
import { reportBodyNodes, reportParentNode } from './report'
6-
import { AllRuleOptions, CreateReporterArgs, NodePositionInfo, TSType } from './types'
4+
import {
5+
AllRuleOptions,
6+
CreateReporterArgs,
7+
Declaration,
8+
NodeOrToken,
9+
NodePositionInfo,
10+
} from './types'
711
import { getPropertyIsOptional, getPropertyName } from './utils/ast'
812
import { compareFn } from './utils/compare'
913
import { memoize } from './utils/memo'
@@ -13,9 +17,9 @@ import { getUnsortedInfo } from './utils/reportUtils'
1317
* Returns the body sorted according to the options and sorting function.
1418
*/
1519
function getSortedBody(
16-
body: TSType[],
20+
body: NodeOrToken[],
1721
isRequiredFirst: boolean,
18-
sortFunction: (a: TSType, b: TSType) => number,
22+
sortFunction: (a: NodeOrToken, b: NodeOrToken) => number,
1923
) {
2024
return isRequiredFirst
2125
? [
@@ -39,11 +43,11 @@ export function createReporter(
3943
createReporterArgs.context,
4044
)
4145
const compare = compareFn(isAscending, isInsensitive, isNatural)
42-
const sortFunction = (a: TSType, b: TSType) =>
46+
const sortFunction = (a: NodeOrToken, b: NodeOrToken) =>
4347
compare(getPropertyName(a), getPropertyName(b))
4448

4549
// Reporter function
46-
return (bodyParent: TSESTree.Node, body: TSType[]) => {
50+
return (bodyParent: Declaration, body: NodeOrToken[]) => {
4751
if (body.length < 2) {
4852
return
4953
}
@@ -56,13 +60,13 @@ export function createReporter(
5660
source: sourceCode.getText(), // Disambiguator when same body on both a type and interface
5761
})
5862

59-
const sortedBody: TSType[] = memoize(`sortedBody_${baseMemoKey}`, () =>
63+
const sortedBody = memoize(`sortedBody_${baseMemoKey}`, () =>
6064
getSortedBody(body, isRequiredFirst, sortFunction),
6165
)
62-
const nodePositions: Map<TSType, NodePositionInfo> = memoize(
66+
const nodePositions: Map<NodeOrToken, NodePositionInfo> = memoize(
6367
`nodePositions_${baseMemoKey}`,
6468
() =>
65-
new Map<TSType, NodePositionInfo>(
69+
new Map<NodeOrToken, NodePositionInfo>(
6670
body.map((n, index) => [
6771
n,
6872
{ initialIndex: index, finalIndex: sortedBody.indexOf(n) },
@@ -80,14 +84,9 @@ export function createReporter(
8084
const fixerFunction = memoize(fixerFunctionMemoKey, () =>
8185
getFixerFunction(baseMemoKey, createReporterArgs, body, sortedBody),
8286
)
83-
reportParentNode(createReporterArgs, bodyParent, unsortedCount, fixerFunction)
84-
reportBodyNodes(
85-
createReporterArgs,
86-
nodePositions,
87-
sortedBody,
88-
finalIndicesToReport,
89-
fixerFunction,
90-
)
87+
88+
reportParentNode(createReporterArgs, bodyParent.loc, unsortedCount, fixerFunction)
89+
reportBodyNodes(createReporterArgs, nodePositions, sortedBody, finalIndicesToReport)
9190
}
9291
}
9392
}

src/report.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import { TSESTree } from '@typescript-eslint/utils'
44
import { ReportFixFunction } from '@typescript-eslint/utils/ts-eslint'
55

66
import { getOptions } from './common/options'
7-
import { AllRuleOptions, CreateReporterArgs, NodePositionInfo, TSType } from './types'
7+
import {
8+
AllRuleOptions,
9+
CreateReporterArgs,
10+
NodeOrToken,
11+
NodePositionInfo,
12+
} from './types'
813
import { getPropertyName } from './utils/ast'
914
import { getDeprecationMessage } from './utils/reportUtils'
1015

@@ -16,12 +21,12 @@ export function reportParentNode(
1621
CreateReporterArgs<string, AllRuleOptions>,
1722
'createReportPropertiesObject'
1823
>,
19-
bodyParent: TSESTree.Node,
24+
_loc: TSESTree.SourceLocation,
2025
unsortedCount: number,
2126
fixerFunction: ReportFixFunction,
2227
) {
2328
const { context, createReportParentObject } = createReporterArgs
24-
const { loc, messageId } = createReportParentObject(bodyParent)
29+
const { loc, messageId } = createReportParentObject(_loc)
2530
context.report({
2631
loc,
2732
messageId,
@@ -42,8 +47,8 @@ export function reportBodyNodes(
4247
CreateReporterArgs<string, AllRuleOptions>,
4348
'createReportParentObject'
4449
>,
45-
nodePositions: Map<TSType, NodePositionInfo>,
46-
sortedBody: TSType[],
50+
nodePositions: Map<NodeOrToken, NodePositionInfo>,
51+
sortedBody: NodeOrToken[],
4752
finalIndicesToReport: boolean[],
4853
fixerFunction: ReportFixFunction,
4954
) {
@@ -54,7 +59,7 @@ export function reportBodyNodes(
5459
for (const [node, { finalIndex }] of nodePositions.entries()) {
5560
// If the node is not in the correct position, report it
5661
if (finalIndicesToReport[finalIndex]) {
57-
const { loc, messageId } = createReportPropertiesObject(node)
62+
const { loc, messageId } = createReportPropertiesObject(node.loc)
5863

5964
// Sanity check
6065
assert(loc, 'createReportObject return value must include a node location')

src/rules/enum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ export const rule = createRule<errorMessageKeys, RuleOptions>({
8282
create(context) {
8383
const compareNodeListAndReport = createReporter({
8484
context,
85-
createReportPropertiesObject: ({ loc }: TSESTree.Node) => ({
85+
createReportPropertiesObject: (loc: TSESTree.SourceLocation) => ({
8686
loc,
8787
messageId: 'invalidOrderBody' as any,
8888
}),
89-
createReportParentObject: ({ loc }: TSESTree.Node) => ({
89+
createReportParentObject: (loc: TSESTree.SourceLocation) => ({
9090
loc,
9191
messageId: 'invalidOrderParent' as any,
9292
}),

src/rules/interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ export const rule = createRule<errorMessageKeys, RuleOptions>({
8888
create(context) {
8989
const compareNodeListAndReport = createReporter({
9090
context,
91-
createReportPropertiesObject: ({ loc }: TSESTree.Node) => ({
91+
createReportPropertiesObject: (loc: TSESTree.SourceLocation) => ({
9292
loc,
9393
messageId: 'invalidOrderBody' as any,
9494
}),
95-
createReportParentObject: ({ loc }: TSESTree.Node) => ({
95+
createReportParentObject: (loc: TSESTree.SourceLocation) => ({
9696
loc,
9797
messageId: 'invalidOrderParent' as any,
9898
}),

src/rules/string-enum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ export const rule = createRule<errorMessageKeys, RuleOptions>({
8888
create(context) {
8989
const compareNodeListAndReport = createReporter({
9090
context,
91-
createReportPropertiesObject: ({ loc }: TSESTree.Node) => ({
91+
createReportPropertiesObject: (loc: TSESTree.SourceLocation) => ({
9292
loc,
9393
messageId: 'invalidOrderBody' as any,
9494
}),
95-
createReportParentObject: ({ loc }: TSESTree.Node) => ({
95+
createReportParentObject: (loc: TSESTree.SourceLocation) => ({
9696
loc,
9797
messageId: 'invalidOrderParent' as any,
9898
}),

src/types/node.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { TSESTree } from '@typescript-eslint/utils'
22

3-
export type Node = Omit<TSESTree.Node, 'type'> & { type: any; value: string }
4-
export type TSType = TSESTree.TypeElement | TSESTree.TSEnumMember
3+
export type NodeOrToken = (TSESTree.Node | TSESTree.Token) & { parent?: NodeOrToken }
4+
export type Declaration =
5+
| TSESTree.TSEnumDeclaration
6+
| TSESTree.TSInterfaceDeclaration
7+
| TSESTree.TSTypeLiteral
58
export type NodePositionInfo = { initialIndex: number; finalIndex: number }

src/types/report.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { TSESTree } from '@typescript-eslint/utils'
22
import {
3-
SourceCode as Lib_SourceCode,
43
RuleContext as UtilRuleContext,
4+
SourceCode as Lib_SourceCode,
55
} from '@typescript-eslint/utils/ts-eslint'
66

77
export type SourceCode = Lib_SourceCode & {
88
lineStartIndices: number[]
99
}
1010

1111
export type ReportObjectCreator = <MessageIds extends string>(
12-
node: TSESTree.Node,
12+
loc: TSESTree.SourceLocation,
1313
) => {
1414
readonly loc: TSESTree.SourceLocation
1515
readonly messageId: MessageIds

src/utils/ast.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { NodeOrToken } from 'src/types'
2+
13
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'
24

3-
import { TSType } from 'types'
45
import { indexSignature } from './common'
56

67
export function getObjectBody(
@@ -18,7 +19,7 @@ export function getObjectBody(
1819
}
1920
}
2021

21-
function getProperty(node: TSESTree.Node) {
22+
function getProperty(node: NodeOrToken) {
2223
switch (node.type) {
2324
case AST_NODE_TYPES.TSIndexSignature: {
2425
const [identifier] = node.parameters
@@ -72,7 +73,7 @@ function getProperty(node: TSESTree.Node) {
7273
* let a = {[tag`b`]: 1} // => undefined
7374
* let a = {[`${b}`]: 1} // => undefined
7475
*/
75-
export function getPropertyName(node: TSType) {
76+
export function getPropertyName(node: NodeOrToken) {
7677
const property = getProperty(node as TSESTree.Node)
7778

7879
switch (property?.type) {
@@ -88,11 +89,14 @@ export function getPropertyName(node: TSType) {
8889
}
8990

9091
// Returns whether a method or property signature is optional.
91-
export function getPropertyIsOptional(node: TSType) {
92+
export function getPropertyIsOptional(node: NodeOrToken) {
9293
switch (node.type) {
9394
case AST_NODE_TYPES.TSMethodSignature:
9495
case AST_NODE_TYPES.TSPropertySignature:
95-
return Boolean(node.optional)
96+
return Boolean(
97+
(node as TSESTree.TSMethodSignature | TSESTree.TSPropertySignature).optional,
98+
)
99+
96100
default:
97101
return false
98102
}

src/utils/memo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const memo: Record<string, any> = {}
22

33
// Getting strange test failures? Could be this! Disable and try again.
4-
export function memoize(key: string, valueFn: () => any): any {
4+
export function memoize<T>(key: string, valueFn: () => T): T {
55
if (hasMemoized(key)) {
66
return memo[key]
77
}

src/utils/reportUtils.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import { PLUGIN_NAME } from 'config/constants'
2-
import { name as enumRuleName } from 'rules/enum'
3-
import { name as enumRuleNameDeprecated } from 'rules/string-enum'
4-
import { NodePositionInfo, TSType } from 'types'
5-
6-
import { getRuleDocsUrl } from './rule'
1+
import { NodeOrToken, NodePositionInfo } from '../types'
72

83
/**
94
* Check if the node is locally sorted or not.
@@ -12,8 +7,8 @@ import { getRuleDocsUrl } from './rule'
127
* before 'b' even though they're both positioned wrongly.
138
*/
149
export function shouldReportUnsorted(
15-
sortedBody: TSType[],
16-
unsortedBody: TSType[],
10+
sortedBody: NodeOrToken[],
11+
unsortedBody: NodeOrToken[],
1712
nodeInfo: NodePositionInfo,
1813
) {
1914
const { initialIndex, finalIndex } = nodeInfo
@@ -27,9 +22,9 @@ export function shouldReportUnsorted(
2722

2823
// Helpful metadata on nodes to report/skip reporting
2924
export function getUnsortedInfo(
30-
sortedBody: TSType[],
31-
unsortedBody: TSType[],
32-
nodePositions: Map<TSType, NodePositionInfo>,
25+
sortedBody: NodeOrToken[],
26+
unsortedBody: NodeOrToken[],
27+
nodePositions: Map<NodeOrToken, NodePositionInfo>,
3328
) {
3429
const finalIndicesToReport = new Array(sortedBody.length).fill(false)
3530
const unsortedCount = Array.from(nodePositions.entries()).reduce(

0 commit comments

Comments
 (0)