@@ -100,12 +100,10 @@ const promiseArrayExceptionKey = ({ start, end }: TSESTree.SourceLocation) =>
100
100
`${ start . line } :${ start . column } -${ end . line } :${ end . column } ` ;
101
101
102
102
type MessageIds =
103
- | 'multipleArgs'
104
- | 'noArgs'
105
- | 'noAssertions'
106
- | 'invalidProperty'
107
- | 'propertyWithoutMatcher'
108
- | 'matcherOnPropertyNotCalled'
103
+ | 'incorrectNumberOfArguments'
104
+ | 'modifierUnknown'
105
+ | 'matcherNotFound'
106
+ | 'matcherNotCalled'
109
107
| 'asyncMustBeAwaited'
110
108
| 'promisesWithAsyncAssertionsMustBeAwaited' ;
111
109
@@ -118,13 +116,10 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
118
116
recommended : 'error' ,
119
117
} ,
120
118
messages : {
121
- multipleArgs : 'More than one argument was passed to expect().' ,
122
- noArgs : 'No arguments were passed to expect().' ,
123
- noAssertions : 'No assertion was called on expect().' ,
124
- invalidProperty :
125
- '"{{ propertyName }}" is not a valid property of expect.' ,
126
- propertyWithoutMatcher : '"{{ propertyName }}" needs to call a matcher.' ,
127
- matcherOnPropertyNotCalled : '"{{ propertyName }}" was not called.' ,
119
+ incorrectNumberOfArguments : 'Expect takes one and only one argument.' ,
120
+ modifierUnknown : 'Expect has no modifier named "{{ modifierName }}".' ,
121
+ matcherNotFound : 'Expect must have a corresponding matcher call.' ,
122
+ matcherNotCalled : 'Matchers must be called to assert.' ,
128
123
asyncMustBeAwaited : 'Async assertions must be awaited{{ orReturned }}.' ,
129
124
promisesWithAsyncAssertionsMustBeAwaited :
130
125
'Promises which return async assertions must be awaited{{ orReturned }}.' ,
@@ -169,46 +164,45 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
169
164
170
165
const { expect, modifier, matcher } = parseExpectCall ( node ) ;
171
166
172
- if ( expect . arguments . length > 1 ) {
173
- const secondArgumentLocStart = expect . arguments [ 1 ] . loc . start ;
174
- const lastArgumentLocEnd =
175
- expect . arguments [ node . arguments . length - 1 ] . loc . end ;
167
+ if ( expect . arguments . length !== 1 ) {
168
+ const expectLength = getAccessorValue ( expect . callee ) . length ;
176
169
177
- context . report ( {
178
- loc : {
179
- end : {
180
- column : lastArgumentLocEnd . column - 1 ,
181
- line : lastArgumentLocEnd . line ,
182
- } ,
183
- start : secondArgumentLocStart ,
170
+ let loc : TSESTree . SourceLocation = {
171
+ start : {
172
+ column : node . loc . start . column + expectLength ,
173
+ line : node . loc . start . line ,
184
174
} ,
185
- messageId : 'multipleArgs' ,
186
- node,
187
- } ) ;
188
- } else if ( expect . arguments . length === 0 ) {
189
- const expectLength = getAccessorValue ( expect . callee ) . length ;
190
- context . report ( {
191
- loc : {
175
+ end : {
176
+ column : node . loc . start . column + expectLength + 1 ,
177
+ line : node . loc . start . line ,
178
+ } ,
179
+ } ;
180
+
181
+ if ( expect . arguments . length !== 0 ) {
182
+ const { start } = expect . arguments [ 1 ] . loc ;
183
+ const { end } = expect . arguments [ node . arguments . length - 1 ] . loc ;
184
+
185
+ loc = {
186
+ start,
192
187
end : {
193
- column : node . loc . start . column + expectLength + 1 ,
194
- line : node . loc . start . line ,
195
- } ,
196
- start : {
197
- column : node . loc . start . column + expectLength ,
198
- line : node . loc . start . line ,
188
+ column : end . column - 1 ,
189
+ line : end . line ,
199
190
} ,
200
- } ,
201
- messageId : 'noArgs' ,
191
+ } ;
192
+ }
193
+
194
+ context . report ( {
195
+ messageId : 'incorrectNumberOfArguments' ,
202
196
node,
197
+ loc,
203
198
} ) ;
204
199
}
205
200
206
201
// something was called on `expect()`
207
202
if ( ! matcher ) {
208
203
if ( modifier ) {
209
204
context . report ( {
210
- data : { propertyName : modifier . name } , // todo: rename to 'modifierName'
211
- messageId : 'propertyWithoutMatcher' , // todo: rename to 'modifierWithoutMatcher'
205
+ messageId : 'matcherNotFound' ,
212
206
node : modifier . node . property ,
213
207
} ) ;
214
208
}
@@ -218,8 +212,8 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
218
212
219
213
if ( matcher . node . parent && isExpectMember ( matcher . node . parent ) ) {
220
214
context . report ( {
221
- messageId : 'invalidProperty' , // todo: rename to 'invalidModifier'
222
- data : { propertyName : matcher . name } , // todo: rename to 'matcherName' (or modifierName?)
215
+ messageId : 'modifierUnknown' ,
216
+ data : { modifierName : matcher . name } ,
223
217
node : matcher . node . property ,
224
218
} ) ;
225
219
@@ -228,8 +222,7 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
228
222
229
223
if ( ! matcher . arguments ) {
230
224
context . report ( {
231
- data : { propertyName : matcher . name } , // todo: rename to 'matcherName'
232
- messageId : 'matcherOnPropertyNotCalled' , // todo: rename to 'matcherNotCalled'
225
+ messageId : 'matcherNotCalled' ,
233
226
node : matcher . node . property ,
234
227
} ) ;
235
228
}
@@ -287,7 +280,7 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
287
280
// nothing called on "expect()"
288
281
'CallExpression:exit' ( node : TSESTree . CallExpression ) {
289
282
if ( isExpectCall ( node ) && isNoAssertionsParentNode ( node . parent ) ) {
290
- context . report ( { messageId : 'noAssertions ' , node } ) ;
283
+ context . report ( { messageId : 'matcherNotFound ' , node } ) ;
291
284
}
292
285
} ,
293
286
} ;
0 commit comments