Skip to content

Commit 6ee5f59

Browse files
authored
Merge pull request #131 from Xvezda/feature/refactor
Fix utils test
2 parents 4bc36e5 + 3272166 commit 6ee5f59

2 files changed

Lines changed: 36 additions & 119 deletions

File tree

src/utils.js

Lines changed: 11 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,6 @@ const collectPaths = (node, untilPredicate) => {
267267
return paths.reverse();
268268
};
269269

270-
/**
271-
* @private
272-
* @param {import('@typescript-eslint/utils').ParserServicesWithTypeInformation} services
273-
* @param {import('@typescript-eslint/utils').TSESTree.Node} node
274-
* @return {import('typescript').Declaration[] | undefined}
275-
*/
276-
const getDeclarationsByNode = (services, node) => {
277-
return services
278-
.getSymbolAtLocation(node)
279-
?.declarations;
280-
};
281-
282270
/**
283271
* Get call expression node's signature.
284272
*
@@ -366,16 +354,20 @@ const getCalleeDeclaration = (services, node) => {
366354
.getTypeAtLocation(calleeNode)
367355
.symbol;
368356

369-
if (!symbol || !symbol.valueDeclaration) return null;
357+
if (!symbol || !symbol.valueDeclaration) {
358+
const declarations = services
359+
.getSymbolAtLocation(calleeNode)
360+
?.declarations;
370361

371-
declaration = symbol.valueDeclaration;
372-
}
373-
if (!declaration) return null;
362+
if (!declarations?.length) return null;
374363

375-
376-
if (!declaration) {
377-
return null;
364+
// If there are multiple declarations, we take the first one.
365+
declaration = declarations[0];
366+
} else {
367+
declaration = symbol.valueDeclaration;
368+
}
378369
}
370+
if (!declaration) return null;
379371

380372
switch (node.type) {
381373
/**
@@ -428,80 +420,6 @@ const getCalleeDeclaration = (services, node) => {
428420
return null;
429421
}
430422
};
431-
/**
432-
* Get all declaration nodes of the callee from the given node's type.
433-
*
434-
* @public
435-
* @param {import('@typescript-eslint/utils').ParserServicesWithTypeInformation} services
436-
* @param {import('@typescript-eslint/utils').TSESTree.Expression} node
437-
* @return {import('typescript').Node[]}
438-
*/
439-
const getCalleeDeclarations = (services, node) => {
440-
/** @type {import('@typescript-eslint/utils').TSESTree.Node | null} */
441-
const calleeNode = getCallee(node);
442-
if (!calleeNode) return [];
443-
444-
const declarations = getDeclarationsByNode(services, calleeNode);
445-
if (!declarations || !declarations.length) {
446-
return [];
447-
}
448-
449-
switch (node.type) {
450-
/**
451-
* Return type of setter when assigning
452-
*
453-
* @example
454-
* ```
455-
* foo.bar = 'baz';
456-
* // ^ This can be a setter
457-
* ```
458-
*/
459-
case AST_NODE_TYPES.AssignmentExpression: {
460-
const setter = declarations
461-
.filter(declaration => {
462-
const declarationNode =
463-
services.tsNodeToESTreeNodeMap.get(declaration);
464-
465-
return isAccessorNode(declarationNode) &&
466-
declarationNode.kind === 'set';
467-
});
468-
469-
return setter;
470-
}
471-
/**
472-
* Return type of getter when accessing
473-
*
474-
* @example
475-
* ```
476-
* const baz = foo.bar;
477-
* // ^ This can be a getter
478-
* ```
479-
*/
480-
case AST_NODE_TYPES.MemberExpression: {
481-
const getter = declarations
482-
.filter(declaration => {
483-
const declarationNode =
484-
services.tsNodeToESTreeNodeMap.get(declaration);
485-
486-
return isAccessorNode(declarationNode) &&
487-
declarationNode.kind === 'get';
488-
});
489-
490-
if (getter.length) {
491-
return getter;
492-
}
493-
// It is method call
494-
if (node.parent?.type === AST_NODE_TYPES.CallExpression) {
495-
return declarations;
496-
}
497-
return [];
498-
}
499-
case AST_NODE_TYPES.CallExpression:
500-
return declarations;
501-
default:
502-
return [];
503-
}
504-
};
505423

506424
/**
507425
* @public
@@ -1025,7 +943,6 @@ module.exports = {
1025943
getCallSignatureDeclaration,
1026944
getCallee,
1027945
getCalleeDeclaration,
1028-
getCalleeDeclarations,
1029946
getJSDocThrowsTags,
1030947
getJSDocThrowsTagTypes,
1031948
toFlattenedTypeArray,

tests/utils.test.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const {
2020
findParent,
2121
getCallSignatureDeclaration,
2222
getCallee,
23-
getCalleeDeclarations,
23+
getCalleeDeclaration,
2424
getJSDocThrowsTags,
2525
getJSDocThrowsTagTypes,
2626
toFlattenedTypeArray,
@@ -455,8 +455,8 @@ debugger;
455455
);
456456
});
457457

458-
describe('getCalleeDeclarations', () => {
459-
test('get declaration of a function calls', (t) => {
458+
describe('getCalleeDeclaration', () => {
459+
test.only('get declaration of a function calls', (t) => {
460460
const { ast, services, sourceCode } = parse(`
461461
// foo declaration
462462
function foo() {}
@@ -500,31 +500,31 @@ obj.baz = 42;
500500

501501
t.assert.ok(
502502
sourceCode
503-
.getCommentsBefore(
504-
services.tsNodeToESTreeNodeMap
505-
.get(getCalleeDeclarations(services, callExpression)[0])
506-
)
507-
.some(({ value }) => value.includes('foo declaration')),
503+
.getCommentsBefore(
504+
services.tsNodeToESTreeNodeMap
505+
.get(getCalleeDeclaration(services, callExpression))
506+
)
507+
.some(({ value }) => value.includes('foo declaration')),
508508
'`foo()` must return the declaration of `foo`',
509509
);
510510

511511
t.assert.ok(
512512
sourceCode
513-
.getCommentsBefore(
514-
services.tsNodeToESTreeNodeMap
515-
.get(getCalleeDeclarations(services, memberExpression)[0]),
516-
)
517-
.some(({ value }) => value.includes('bar declaration')),
513+
.getCommentsBefore(
514+
services.tsNodeToESTreeNodeMap
515+
.get(getCalleeDeclaration(services, memberExpression)),
516+
)
517+
.some(({ value }) => value.includes('bar declaration')),
518518
'`const value = obj.bar` must return the declaration of `obj.bar`',
519519
);
520520

521521
t.assert.ok(
522522
sourceCode
523-
.getCommentsBefore(
524-
services.tsNodeToESTreeNodeMap
525-
.get(getCalleeDeclarations(services, assignmentExpression)[0]),
526-
)
527-
.some(({ value }) => value.includes('baz declaration')),
523+
.getCommentsBefore(
524+
services.tsNodeToESTreeNodeMap
525+
.get(getCalleeDeclaration(services, assignmentExpression)),
526+
)
527+
.some(({ value }) => value.includes('baz declaration')),
528528
'`obj.baz = 42` must return the declaration of `obj.baz`',
529529
);
530530
});
@@ -561,13 +561,13 @@ obj.baz = 42;
561561
const assignmentExpression = map.get(AST_NODE_TYPES.AssignmentExpression);
562562

563563
t.assert.equal(
564-
getCalleeDeclarations(services, memberExpression).length,
565-
0
564+
getCalleeDeclaration(services, memberExpression),
565+
null
566566
);
567567

568568
t.assert.equal(
569-
getCalleeDeclarations(services, assignmentExpression).length,
570-
0
569+
getCalleeDeclaration(services, assignmentExpression),
570+
null
571571
);
572572
});
573573

@@ -579,9 +579,9 @@ a;
579579

580580
const identifier = getFirstFoundIdentifier(ast, 'a');
581581
t.assert.equal(
582-
getCalleeDeclarations(services, identifier).length,
583-
0,
584-
'non-callable node should return nothing',
582+
getCalleeDeclaration(services, identifier),
583+
null,
584+
'non-callable node should return null',
585585
);
586586
});
587587
});

0 commit comments

Comments
 (0)