Skip to content

Commit 33c8e7c

Browse files
committed
Add new resolver to find all class definitions (see also previous commit)
1 parent 2a841ff commit 33c8e7c

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
/*global jest, describe, beforeEach, it, expect*/
12+
13+
jest.autoMockOff();
14+
15+
describe('findAllClassDefinitions', () => {
16+
var findAllClassDefinitions;
17+
var recast;
18+
19+
function parse(source) {
20+
return findAllClassDefinitions(
21+
recast.parse(source).program,
22+
recast
23+
);
24+
}
25+
26+
beforeEach(() => {
27+
findAllClassDefinitions = require('../findAllClassDefinitions');
28+
recast = require('recast');
29+
});
30+
31+
32+
it('finds component classes', () => {
33+
var source = `
34+
import React from 'React';
35+
class ComponentA extends React.Component {}
36+
class ComponentB { render() {} }
37+
var ComponentC = class extends React.Component {}
38+
var ComponentD = class { render() {} }
39+
class NotAComponent {}
40+
`;
41+
42+
var result = parse(source);
43+
expect(Array.isArray(result)).toBe(true);
44+
expect(result.length).toBe(4);
45+
});
46+
47+
it('finds React.createClass, independent of the var name', () => {
48+
var source = `
49+
import R from 'React';
50+
class Component extends R.Component {};
51+
`;
52+
53+
var result = parse(source);
54+
expect(Array.isArray(result)).toBe(true);
55+
expect(result.length).toBe(1);
56+
});
57+
58+
it('does not process X.createClass of other modules', () => {
59+
var source = `
60+
import R from 'FakeReact';
61+
class Component extends R.Component {};
62+
`;
63+
64+
var result = parse(source);
65+
expect(Array.isArray(result)).toBe(true);
66+
expect(result.length).toBe(0);
67+
});
68+
69+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
* @flow
10+
*
11+
*/
12+
13+
import isReactComponentClass from '../utils/isReactComponentClass';
14+
import normalizeClassDefinition from '../utils/normalizeClassDefinition';
15+
16+
/**
17+
* Given an AST, this function tries to find all object expressions that are
18+
* passed to `React.createClass` calls, by resolving all references properly.
19+
*/
20+
export default function findAllClassDefinitions(
21+
ast: ASTNode,
22+
recast: Object
23+
): Array<NodePath> {
24+
var definitions = [];
25+
26+
function visitor(path) {
27+
if (isReactComponentClass(path)) {
28+
normalizeClassDefinition(path);
29+
definitions.push(path);
30+
}
31+
return false;
32+
}
33+
34+
recast.visit(ast, {
35+
visitClassExpression: visitor,
36+
visitClassDeclaration: visitor,
37+
});
38+
39+
return definitions;
40+
}

0 commit comments

Comments
 (0)