-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.ts
175 lines (150 loc) · 5.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable no-use-before-define */
// Imported from https://github.com/jest-community/eslint-plugin-jest/blob/main/src/rules/utils/accessors.ts#L6
import { TSESLint, AST_NODE_TYPES, ESLintUtils, TSESTree } from '@typescript-eslint/utils'
import { KnownMemberExpression } from './parseVitestFnCall'
export const createEslintRule = ESLintUtils.RuleCreator((ruleName) => `https://github.com/veritem/eslint-plugin-vitest/blob/main/docs/rules/${ruleName}.md`)
export const joinNames = (a: string | null, b: string | null): string | null => a && b ? `${a}.${b}` : null
interface TemplateLiteral<Value extends string = string>
extends TSESTree.TemplateLiteral {
quasis: [TSESTree.TemplateElement & { value: { raw: Value; cooked: Value } }];
}
interface StringLiteral<Value extends string = string>
extends TSESTree.StringLiteral {
value: Value;
}
export type StringNode<S extends string = string> =
| StringLiteral<S>
| TemplateLiteral<S>;
export type FunctionExpression =
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression;
export const isFunction = (node: TSESTree.Node): node is FunctionExpression =>
node.type === AST_NODE_TYPES.FunctionExpression ||
node.type === AST_NODE_TYPES.ArrowFunctionExpression
/**
* An `Identifier` with a known `name` value - i.e `expect`.
*/
interface KnownIdentifier<Name extends string> extends TSESTree.Identifier {
name: Name;
}
export function getNodeName(node: TSESTree.Node): string | null {
if (isSupportedAccessor(node))
return getAccessorValue(node)
switch (node.type) {
case AST_NODE_TYPES.TaggedTemplateExpression:
return getNodeName(node.tag)
case AST_NODE_TYPES.MemberExpression:
return joinNames(getNodeName(node.object), getNodeName(node.property))
case AST_NODE_TYPES.NewExpression:
case AST_NODE_TYPES.CallExpression:
return getNodeName(node.callee)
}
return null
}
export type AccessorNode<Specifics extends string = string> = StringNode<Specifics> | KnownIdentifier<Specifics>
export const isSupportedAccessor = <V extends string>(node: TSESTree.Node, value?: V): node is AccessorNode<V> => {
return isIdentifier(node, value) || isStringNode(node, value)
}
/**
* Checks if the given `node` is an `Identifier`.
*
* If a `name` is provided, & the `node` is an `Identifier`,
* the `name` will be compared to that of the `identifier`.
*/
export const isIdentifier = <V extends string>(
node: TSESTree.Node,
name?: V
): node is KnownIdentifier<V> => {
return node.type === AST_NODE_TYPES.Identifier &&
(name === undefined || node.name === name)
}
/**
* Checks if the given `node` is a `TemplateLiteral`.
*
* Complex `TemplateLiteral`s are not considered specific, and so will return `false`.
*
* If a `value` is provided & the `node` is a `TemplateLiteral`,
* the `value` will be compared to that of the `TemplateLiteral`.
*/
const isTemplateLiteral = <V extends string>(
node: TSESTree.Node,
value?: V
): node is StringLiteral<V> => {
return node.type === AST_NODE_TYPES.TemplateLiteral &&
node.quasis.length === 1 &&
(value === undefined || node.quasis[0].value.raw === value)
}
/**
* Checks if the given `node` is a `StringLiteral`.
*
* If a `value` is provided & the `node` is a `StringLiteral`,
* the `value` will be compared to that of the `StringLiteral`.
*/
const isStringLiteral = <V extends string>(
node: TSESTree.Node,
value?: V
): node is StringLiteral<V> =>
node.type === AST_NODE_TYPES.Literal &&
typeof node.value === 'string' &&
(value === undefined || node.value === value)
/**
* Checks if the given `node` is a {@link StringNode}.
*/
export const isStringNode = <V extends string>(
node: TSESTree.Node,
specifics?: V
): node is StringNode<V> =>
isStringLiteral(node, specifics) || isTemplateLiteral(node, specifics)
/**
* Gets the value of the given `AccessorNode`,
* account for the different node types.
*/
export const getAccessorValue =
<S extends string = string>(accessor: AccessorNode<S>): S =>
accessor.type === AST_NODE_TYPES.Identifier
? accessor.name
: getStringValue(accessor)
/**
* Gets the value of the given `StringNode`.
*
* If the `node` is a `TemplateLiteral`, the `raw` value is used;
* otherwise, `value` is returned instead.
*/
export const getStringValue =
<S extends string>(node: StringNode<S>): S =>
node?.type === AST_NODE_TYPES.TemplateLiteral
? node.quasis[0].value.raw
: node?.value
export const replaceAccessorFixer = (
fixer: TSESLint.RuleFixer,
node: AccessorNode,
text: string
) => {
return fixer.replaceText(
node,
node.type === AST_NODE_TYPES.Identifier ? text : `'${text}'`
)
}
export const removeExtraArgumentsFixer = (
fixer: TSESLint.RuleFixer,
context: TSESLint.RuleContext<string, unknown[]>,
func: TSESTree.CallExpression,
from: number
): TSESLint.RuleFix => {
const firstArg = func.arguments[from]
const lastArg = func.arguments[func.arguments.length - 1]
const sourceCode = context.getSourceCode()
let tokenAfterLastParam = sourceCode.getTokenAfter(lastArg)!
if (tokenAfterLastParam.value === ',')
tokenAfterLastParam = sourceCode.getTokenAfter(tokenAfterLastParam)!
return fixer.removeRange([firstArg.range[0], tokenAfterLastParam.range[0]])
}
interface CalledKnownMemberExpression<Name extends string = string>
extends KnownMemberExpression<Name> {
parent: KnownCallExpression<Name>;
}
export interface KnownCallExpression<Name extends string = string>
extends TSESTree.CallExpression {
callee: CalledKnownMemberExpression<Name>;
}