Skip to content

Commit af7d870

Browse files
committed
fix: Fix TypeScript types
1 parent 66f13f4 commit af7d870

16 files changed

+69
-58
lines changed

src/rules/max-expects.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ESTree from 'estree'
2-
import { getParent } from '../utils/ast.js'
32
import { createRule } from '../utils/createRule.js'
43
import { isTypeOfFnCall, parseFnCall } from '../utils/parseFnCall.js'
4+
import { NodeWithParent } from '../utils/types.js'
55

66
export default createRule({
77
create(context) {
@@ -13,7 +13,7 @@ export default createRule({
1313
let count = 0
1414

1515
const maybeResetCount = (node: ESTree.Node) => {
16-
const parent = getParent(node)
16+
const parent = (node as NodeWithParent).parent
1717
const isTestFn =
1818
parent?.type !== 'CallExpression' ||
1919
isTypeOfFnCall(context, parent, ['test'])
@@ -31,7 +31,7 @@ export default createRule({
3131

3232
if (
3333
call?.type !== 'expect' ||
34-
getParent(call.head.node)?.type === 'MemberExpression'
34+
(call.head.node as NodeWithParent).parent?.type === 'MemberExpression'
3535
) {
3636
return
3737
}

src/rules/missing-playwright-await.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Rule } from 'eslint'
21
import ESTree from 'estree'
3-
import { getParent, getStringValue, isIdentifier } from '../utils/ast.js'
2+
import { getStringValue, isIdentifier } from '../utils/ast.js'
43
import { createRule } from '../utils/createRule.js'
54
import { ParsedFnCall, parseFnCall } from '../utils/parseFnCall.js'
5+
import { NodeWithParent } from '../utils/types.js'
66

77
const validTypes = new Set([
88
'AwaitExpression',
@@ -60,7 +60,7 @@ const playwrightTestMatchers = [
6060
]
6161

6262
function getReportNode(node: ESTree.Node) {
63-
const parent = getParent(node)
63+
const parent = (node as NodeWithParent).parent
6464
return parent?.type === 'MemberExpression' ? parent : node
6565
}
6666

@@ -95,7 +95,7 @@ export default createRule({
9595
])
9696

9797
function checkValidity(node: ESTree.Node, visited: Set<ESTree.Node>) {
98-
const parent = getParent(node)
98+
const parent = (node as NodeWithParent).parent
9999
if (!parent) return false
100100

101101
if (visited.has(parent)) return false
@@ -129,7 +129,7 @@ export default createRule({
129129
const scope = context.sourceCode.getScope(parent.parent)
130130

131131
for (const ref of scope.references) {
132-
const refParent = (ref.identifier as Rule.Node).parent
132+
const refParent = (ref.identifier as NodeWithParent).parent
133133
if (visited.has(refParent)) continue
134134
// If the parent of the reference is valid, we can immediately return
135135
// true. Otherwise, we'll check the validity of the parent to continue

src/rules/no-commented-out-tests.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { Rule } from 'eslint'
22
import * as ESTree from 'estree'
33
import { createRule } from '../utils/createRule.js'
4+
import { Settings } from '../utils/types.js'
45

56
function getTestNames(context: Rule.RuleContext) {
6-
const aliases = context.settings.playwright?.globalAliases?.test ?? []
7+
const settings = context.settings as unknown as Settings | undefined
8+
const aliases = settings?.playwright?.globalAliases?.test ?? []
9+
710
return ['test', ...aliases]
811
}
912

src/rules/no-conditional-expect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Rule, Scope } from 'eslint'
22
import * as ESTree from 'estree'
3-
import { getParent, isPropertyAccessor } from '../utils/ast.js'
3+
import { isPropertyAccessor } from '../utils/ast.js'
44
import { createRule } from '../utils/createRule.js'
55
import { isTypeOfFnCall, parseFnCall } from '../utils/parseFnCall.js'
6-
import { KnownCallExpression } from '../utils/types.js'
6+
import { KnownCallExpression, NodeWithParent } from '../utils/types.js'
77

88
const isCatchCall = (
99
node: ESTree.CallExpression,
@@ -19,7 +19,7 @@ const getTestCallExpressionsFromDeclaredVariables = (
1919
(acc, { references }) => [
2020
...acc,
2121
...references
22-
.map(({ identifier }) => getParent(identifier))
22+
.map(({ identifier }) => (identifier as NodeWithParent).parent)
2323
.filter(
2424
// ESLint types are infurating
2525
(node): node is any =>

src/rules/no-nested-step.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ export default createRule({
77
const stack: number[] = []
88

99
function pushStepCallback(node: Rule.Node) {
10+
const { parent } = node
11+
1012
if (
11-
node.parent.type !== 'CallExpression' ||
12-
!isTypeOfFnCall(context, node.parent, ['step'])
13+
parent?.type !== 'CallExpression' ||
14+
!isTypeOfFnCall(context, parent, ['step'])
1315
) {
1416
return
1517
}
@@ -19,7 +21,7 @@ export default createRule({
1921
if (stack.length > 1) {
2022
context.report({
2123
messageId: 'noNestedStep',
22-
node: node.parent.callee,
24+
node: parent.callee,
2325
})
2426
}
2527
}
@@ -28,7 +30,7 @@ export default createRule({
2830
const { parent } = node
2931

3032
if (
31-
parent.type === 'CallExpression' &&
33+
parent?.type === 'CallExpression' &&
3234
isTypeOfFnCall(context, parent, ['step'])
3335
) {
3436
stack.pop()

src/rules/no-standalone-expect.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { Rule } from 'eslint'
22
import * as ESTree from 'estree'
3-
import { getParent, isFunction, isPropertyAccessor } from '../utils/ast.js'
3+
import { isFunction, isPropertyAccessor } from '../utils/ast.js'
44
import { createRule } from '../utils/createRule.js'
55
import { isTypeOfFnCall, parseFnCall } from '../utils/parseFnCall.js'
6+
import { NodeWithParent } from '../utils/types.js'
67

78
const getBlockType = (
89
context: Rule.RuleContext,
910
statement: ESTree.BlockStatement,
1011
): 'function' | 'describe' | null => {
11-
const func = getParent(statement)
12+
const func = (statement as NodeWithParent).parent
1213

1314
if (!func) {
1415
throw new Error(
@@ -85,7 +86,8 @@ export default createRule({
8586

8687
if (call?.type === 'expect') {
8788
if (
88-
getParent(call.head.node)?.type === 'MemberExpression' &&
89+
(call.head.node as NodeWithParent).parent?.type ===
90+
'MemberExpression' &&
8991
call.members.length === 1
9092
) {
9193
return

src/rules/no-unsafe-references.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '../utils/ast.js'
99
import { createRule } from '../utils/createRule.js'
1010
import { truthy } from '../utils/misc.js'
11+
import { NodeWithParent } from '../utils/types.js'
1112

1213
/** Collect all variable references in the parent scopes recursively. */
1314
function collectVariables(scope: Scope.Scope | null): string[] {
@@ -115,7 +116,7 @@ export default createRule({
115116
// then it's likely a global variable such as `Promise` or `console`.
116117
through
117118
.filter((ref) => {
118-
const parent = getParent(ref.identifier)
119+
const parent = (ref.identifier as NodeWithParent).parent
119120
return (parent?.type as string) !== 'TSTypeReference'
120121
})
121122
.filter((ref) => allRefs.has(ref.identifier.name))

src/rules/prefer-comparison-matcher.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as ESTree from 'estree'
22
import {
33
equalityMatchers,
4-
getParent,
54
getRawValue,
65
getStringValue,
76
isBooleanLiteral,
87
isStringLiteral,
98
} from '../utils/ast.js'
109
import { createRule } from '../utils/createRule.js'
1110
import { parseFnCall } from '../utils/parseFnCall.js'
11+
import { NodeWithParent } from '../utils/types.js'
1212

1313
const isString = (node: ESTree.Node) => {
1414
return isStringLiteral(node) || node.type === 'TemplateLiteral'
@@ -47,7 +47,7 @@ export default createRule({
4747
const call = parseFnCall(context, node)
4848
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return
4949

50-
const expect = getParent(call.head.node)
50+
const expect = (call.head.node as NodeWithParent).parent
5151
if (expect?.type !== 'CallExpression') return
5252

5353
const [comparison] = expect.arguments
@@ -94,7 +94,10 @@ export default createRule({
9494
),
9595
// Replace the current matcher & modifier with the preferred matcher
9696
fixer.replaceTextRange(
97-
[expectCallEnd, getParent(call.matcher)!.range![1]],
97+
[
98+
expectCallEnd,
99+
(call.matcher as NodeWithParent).parent!.range![1],
100+
],
98101
`${modifierText}.${preferredMatcher}`,
99102
),
100103
// Replace the matcher argument with the right-hand side of the comparison

src/rules/prefer-equality-matcher.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
22
equalityMatchers,
3-
getParent,
43
getRawValue,
54
getStringValue,
65
isBooleanLiteral,
76
} from '../utils/ast.js'
87
import { createRule } from '../utils/createRule.js'
98
import { parseFnCall } from '../utils/parseFnCall.js'
9+
import { NodeWithParent } from '../utils/types.js'
1010

1111
export default createRule({
1212
create(context) {
@@ -15,7 +15,7 @@ export default createRule({
1515
const call = parseFnCall(context, node)
1616
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return
1717

18-
const expect = getParent(call.head.node)
18+
const expect = (call.head.node as NodeWithParent).parent
1919
if (expect?.type !== 'CallExpression') return
2020

2121
const [comparison] = expect.arguments
@@ -67,7 +67,10 @@ export default createRule({
6767
),
6868
// replace the current matcher & modifier with the preferred matcher
6969
fixer.replaceTextRange(
70-
[expectCallEnd, getParent(call.matcher)!.range![1]],
70+
[
71+
expectCallEnd,
72+
(call.matcher as NodeWithParent).parent!.range![1],
73+
],
7174
`${modifierText}.${equalityMatcher}`,
7275
),
7376
// replace the matcher argument with the right-hand side of the comparison

src/rules/prefer-to-contain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../utils/ast.js'
99
import { createRule } from '../utils/createRule.js'
1010
import { parseFnCall } from '../utils/parseFnCall.js'
11-
import { KnownCallExpression } from '../utils/types.js'
11+
import { KnownCallExpression, NodeWithParent } from '../utils/types.js'
1212

1313
type FixableIncludesCallExpression = KnownCallExpression
1414

@@ -28,7 +28,7 @@ export default createRule({
2828
const call = parseFnCall(context, node)
2929
if (call?.type !== 'expect' || call.matcherArgs.length === 0) return
3030

31-
const expect = getParent(call.head.node)
31+
const expect = (call.head.node as NodeWithParent).parent
3232
if (expect?.type !== 'CallExpression') return
3333

3434
const [includesCall] = expect.arguments

0 commit comments

Comments
 (0)