Skip to content

Commit bbe5238

Browse files
committed
chore(utils): merge AccessorNode guards
1 parent acebbf9 commit bbe5238

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/rules/prefer-to-have-length.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
22
import {
33
createRule,
4-
isAccessorNode,
54
isExpectCall,
65
isParsedEqualityMatcherCall,
6+
isSupportedAccessor,
77
parseExpectCall,
88
} from './utils';
99

@@ -42,7 +42,7 @@ export default createRule({
4242
!isParsedEqualityMatcherCall(matcher) ||
4343
!argument ||
4444
argument.type !== AST_NODE_TYPES.MemberExpression ||
45-
!isAccessorNode(argument.property, 'length') ||
45+
!isSupportedAccessor(argument.property, 'length') ||
4646
argument.property.type !== AST_NODE_TYPES.Identifier
4747
) {
4848
return;

src/rules/utils.ts

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,35 +162,51 @@ const isIdentifier = <V extends string>(
162162
node.type === AST_NODE_TYPES.Identifier &&
163163
(name === undefined || node.name === name);
164164

165+
/**
166+
* Checks if the given `node` is a "supported accessor".
167+
*
168+
* This means that it's a node can be used to access properties,
169+
* and who's "value" can be statically determined.
170+
*
171+
* `MemberExpression` nodes most commonly contain accessors,
172+
* but it's possible for other nodes to contain them.
173+
*
174+
* If a `value` is provided & the `node` is an `AccessorNode`,
175+
* the `value` will be compared to that of the `AccessorNode`.
176+
*
177+
* Note that `value` here refers to the normalised value.
178+
* The property that holds the value is not always called `name`.
179+
*
180+
* @param {Node} node
181+
* @param {V?} value
182+
*
183+
* @return {node is AccessorNode<V>}
184+
*
185+
* @template V
186+
*/
165187
export const isSupportedAccessor = <V extends string>(
166188
node: TSESTree.Node,
167189
value?: V,
168190
): node is AccessorNode<V> =>
169191
isIdentifier(node, value) || isStringNode(node, value);
170192

193+
/**
194+
* Gets the value of the given `AccessorNode`,
195+
* account for the different node types.
196+
*
197+
* @param {AccessorNode<S>} accessor
198+
*
199+
* @return {S}
200+
*
201+
* @template S
202+
*/
171203
export const getAccessorValue = <S extends string = string>(
172204
accessor: AccessorNode<S>,
173205
): S =>
174206
accessor.type === AST_NODE_TYPES.Identifier
175207
? accessor.name
176208
: getStringValue(accessor);
177209

178-
/**
179-
* Checks if the given `node` is an `AccessorExpression` with the specific `value`.
180-
*
181-
* @param {Node} node
182-
* @param {V} accessor
183-
*
184-
* @return {node is SpecificAccessorExpression<V>}
185-
*
186-
* @template V
187-
*/
188-
export const isAccessorNode = <V extends string>(
189-
node: TSESTree.Node,
190-
accessor: V,
191-
): node is AccessorNode<V> =>
192-
isIdentifier(node, accessor) || isStringNode(node, accessor);
193-
194210
export interface ValidExpectCall<
195211
Argument extends TSESTree.Expression = TSESTree.Expression
196212
> extends ExpectCall {
@@ -215,7 +231,7 @@ type AccessorNode<Specifics extends string = string> =
215231
type ExpectAccessor = AccessorNode<'expect'>;
216232

217233
export const isExpectAccessor = (node: TSESTree.Node): node is ExpectAccessor =>
218-
isAccessorNode(node, 'expect');
234+
isSupportedAccessor(node, 'expect');
219235

220236
export interface ExpectCall extends TSESTree.CallExpression {
221237
callee: AccessorNode<'expect'>;

0 commit comments

Comments
 (0)