Skip to content

Commit 67109b9

Browse files
committed
Correctly resolve identifiers to class or function definitions
Currently they are not resolved at all but returned as is.
1 parent 17a8287 commit 67109b9

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

src/utils/__tests__/resolveToValue-test.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
/*global jest, describe, beforeEach, it, expect*/
1212

13-
"use strict";
14-
1513
jest.autoMockOff();
1614

1715
describe('resolveToValue', () => {
18-
var astNodesAreEquivalent;
1916
var builders;
2017
var utils;
2118
var resolveToValue;
@@ -27,7 +24,6 @@ describe('resolveToValue', () => {
2724

2825
beforeEach(() => {
2926
var recast = require('recast');
30-
astNodesAreEquivalent = recast.types.astNodesAreEquivalent;
3127
builders = recast.types.builders;
3228
resolveToValue = require('../resolveToValue');
3329
utils = require('../../../tests/utils');
@@ -36,72 +32,77 @@ describe('resolveToValue', () => {
3632
it('resolves simple variable declarations', () => {
3733
var path = parse([
3834
'var foo = 42;',
39-
'foo;'
35+
'foo;',
4036
].join('\n'));
41-
expect(astNodesAreEquivalent(
42-
resolveToValue(path).node,
43-
builders.literal(42)
44-
)).toBe(true);
37+
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(42));
4538
});
4639

4740
it('resolves object destructuring', () => {
4841
var path = parse([
4942
'var {foo: {bar: baz}} = bar;',
50-
'baz;'
43+
'baz;',
5144
].join('\n'));
5245

5346
// Node should be equal to bar.foo.bar
54-
expect(astNodesAreEquivalent(
55-
resolveToValue(path).node,
47+
expect(resolveToValue(path).node).toEqualASTNode(
5648
builders.memberExpression(
5749
builders.memberExpression(
5850
builders.identifier('bar'),
5951
builders.identifier('foo')
6052
),
6153
builders.identifier('bar')
6254
)
63-
)).toBe(true);
55+
);
6456
});
6557

6658
it('handles SpreadProperties properly', () => {
6759
var path = parse([
6860
'var {foo: {bar}, ...baz} = bar;',
69-
'baz;'
61+
'baz;',
7062
].join('\n'));
7163

72-
expect(astNodesAreEquivalent(
73-
resolveToValue(path).node,
74-
path.node
75-
)).toBe(true);
64+
expect(resolveToValue(path).node).toEqualASTNode(path.node);
7665
});
7766

7867
it('returns the original path if it cannot be resolved', () => {
7968
var path = parse([
8069
'function foo() {}',
81-
'foo()'
70+
'foo()',
8271
].join('\n'));
8372

84-
expect(astNodesAreEquivalent(
85-
resolveToValue(path).node,
86-
path.node
87-
)).toBe(true);
73+
expect(resolveToValue(path).node).toEqualASTNode(path.node);
8874
});
8975

9076
it('resolves variable declarators to their init value', () => {
9177
var path = utils.parse('var foo = 42;').get('body', 0, 'declarations', 0);
9278

93-
expect(astNodesAreEquivalent(
94-
resolveToValue(path).node,
95-
builders.literal(42)
96-
)).toBe(true);
79+
expect(resolveToValue(path).node).toEqualASTNode(builders.literal(42));
80+
});
81+
82+
it('resolves to class declarations', () => {
83+
var program = utils.parse(`
84+
class Foo {}
85+
Foo;
86+
`);
87+
expect(resolveToValue(program.get('body', 1, 'expression')).node.type)
88+
.toBe('ClassDeclaration');
89+
});
90+
91+
it('resolves to class function declaration', () => {
92+
var program = utils.parse(`
93+
function foo() {}
94+
foo;
95+
`);
96+
expect(resolveToValue(program.get('body', 1, 'expression')).node.type)
97+
.toBe('FunctionDeclaration');
9798
});
9899

99100
describe('ImportDeclaration', () => {
100101

101102
it('resolves default import references to the import declaration', () => {
102103
var path = parse([
103104
'import foo from "Foo"',
104-
'foo;'
105+
'foo;',
105106
].join('\n'));
106107

107108
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
@@ -110,7 +111,7 @@ describe('resolveToValue', () => {
110111
it('resolves named import references to the import declaration', () => {
111112
var path = parse([
112113
'import {foo} from "Foo"',
113-
'foo;'
114+
'foo;',
114115
].join('\n'));
115116

116117
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');
@@ -119,7 +120,7 @@ describe('resolveToValue', () => {
119120
it('resolves aliased import references to the import declaration', () => {
120121
var path = parse([
121122
'import {foo as bar} from "Foo"',
122-
'bar;'
123+
'bar;',
123124
].join('\n'));
124125

125126
expect(resolveToValue(path).node.type).toBe('ImportDeclaration');

src/utils/resolveToValue.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ function resolveToValue(path: NodePath): NodePath {
6363
return resolveToValue(node.get('right'));
6464
}
6565
} else if (types.Identifier.check(node)) {
66+
if ((types.ClassDeclaration.check(path.parentPath.node) ||
67+
types.ClassExpression.check(path.parentPath.node) ||
68+
types.Function.check(path.parentPath.node)) &&
69+
path.parentPath.get('id') === path) {
70+
return path.parentPath;
71+
}
72+
6673
var scope = path.scope.lookup(node.name);
6774
if (scope) {
6875
var bindings = scope.getBindings()[node.name];

0 commit comments

Comments
 (0)