@@ -100,12 +100,10 @@ const promiseArrayExceptionKey = ({ start, end }: TSESTree.SourceLocation) =>
100100 `${ start . line } :${ start . column } -${ end . line } :${ end . column } ` ;
101101
102102type MessageIds =
103- | 'multipleArgs'
104- | 'noArgs'
105- | 'noAssertions'
106- | 'invalidProperty'
107- | 'propertyWithoutMatcher'
108- | 'matcherOnPropertyNotCalled'
103+ | 'incorrectNumberOfArguments'
104+ | 'modifierUnknown'
105+ | 'matcherNotFound'
106+ | 'matcherNotCalled'
109107 | 'asyncMustBeAwaited'
110108 | 'promisesWithAsyncAssertionsMustBeAwaited' ;
111109
@@ -118,13 +116,10 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
118116 recommended : 'error' ,
119117 } ,
120118 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.' ,
128123 asyncMustBeAwaited : 'Async assertions must be awaited{{ orReturned }}.' ,
129124 promisesWithAsyncAssertionsMustBeAwaited :
130125 'Promises which return async assertions must be awaited{{ orReturned }}.' ,
@@ -169,46 +164,45 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
169164
170165 const { expect, modifier, matcher } = parseExpectCall ( node ) ;
171166
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 ;
176169
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 ,
184174 } ,
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,
192187 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 ,
199190 } ,
200- } ,
201- messageId : 'noArgs' ,
191+ } ;
192+ }
193+
194+ context . report ( {
195+ messageId : 'incorrectNumberOfArguments' ,
202196 node,
197+ loc,
203198 } ) ;
204199 }
205200
206201 // something was called on `expect()`
207202 if ( ! matcher ) {
208203 if ( modifier ) {
209204 context . report ( {
210- data : { propertyName : modifier . name } , // todo: rename to 'modifierName'
211- messageId : 'propertyWithoutMatcher' , // todo: rename to 'modifierWithoutMatcher'
205+ messageId : 'matcherNotFound' ,
212206 node : modifier . node . property ,
213207 } ) ;
214208 }
@@ -218,8 +212,8 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
218212
219213 if ( matcher . node . parent && isExpectMember ( matcher . node . parent ) ) {
220214 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 } ,
223217 node : matcher . node . property ,
224218 } ) ;
225219
@@ -228,8 +222,7 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
228222
229223 if ( ! matcher . arguments ) {
230224 context . report ( {
231- data : { propertyName : matcher . name } , // todo: rename to 'matcherName'
232- messageId : 'matcherOnPropertyNotCalled' , // todo: rename to 'matcherNotCalled'
225+ messageId : 'matcherNotCalled' ,
233226 node : matcher . node . property ,
234227 } ) ;
235228 }
@@ -287,7 +280,7 @@ export default createRule<[{ alwaysAwait?: boolean }], MessageIds>({
287280 // nothing called on "expect()"
288281 'CallExpression:exit' ( node : TSESTree . CallExpression ) {
289282 if ( isExpectCall ( node ) && isNoAssertionsParentNode ( node . parent ) ) {
290- context . report ( { messageId : 'noAssertions ' , node } ) ;
283+ context . report ( { messageId : 'matcherNotFound ' , node } ) ;
291284 }
292285 } ,
293286 } ;
0 commit comments