Skip to content

Commit 756892c

Browse files
committed
refactor: get rid of recast by using ast-types directly
1 parent 07d7310 commit 756892c

File tree

6 files changed

+109
-137
lines changed

6 files changed

+109
-137
lines changed

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"main": "distribution/index.js",
2727
"scripts": {
2828
"flow": "flow",
29-
"lint": "eslint src/ test/",
29+
"lint": "eslint source/",
3030
"build": "rimraf distribution/ && babel source/ --out-dir distribution/ --ignore **/*.spec.js",
3131
"pretest:unit": "npm run flow",
3232
"test:unit": "ava",
@@ -83,8 +83,7 @@
8383
"distribution/"
8484
],
8585
"peerDependencies": {
86-
"react-docgen": "^2.17.0 || ^3.0.0-beta",
87-
"recast": "0.12.6 || 0.13.0"
86+
"react-docgen": "^2.17.0 || ^3.0.0-beta"
8887
},
8988
"devDependencies": {
9089
"ava": "0.25.0",
@@ -99,9 +98,11 @@
9998
"eslint": "4.19.1",
10099
"flow-bin": "0.73.0",
101100
"react-docgen": "3.0.0-beta12",
102-
"recast": "0.13.0",
103101
"rimraf": "2.6.2",
104102
"semantic-release": "15.5.0",
105103
"test-all-versions": "3.3.3"
104+
},
105+
"dependencies": {
106+
"ast-types": "0.11.5"
106107
}
107108
}

source/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/* @flow */
22

33
import path from 'path';
4-
import recast from 'recast';
54
import { utils } from 'react-docgen';
6-
const { getMemberValuePath, getNameOrValue, isExportsOrModuleAssignment } = utils;
5+
import { namedTypes as types } from 'ast-types';
76

8-
const {types: {namedTypes: types}} = recast;
7+
const {
8+
getMemberValuePath,
9+
getNameOrValue,
10+
isExportsOrModuleAssignment
11+
} = utils;
912

1013
const DEFAULT_NAME = 'UnknownComponent';
1114

source/index.spec.js

Lines changed: 89 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,160 @@
11
import test from 'ava';
2-
import recast from 'recast';
3-
import Documentation from 'react-docgen/dist/Documentation';
4-
import { resolver } from 'react-docgen';
5-
import { parse } from '../tests/utils';
2+
import * as docgen from 'react-docgen';
63
import displayNameHandler, { createDisplayNameHandler } from './index';
7-
const { findAllComponentDefinitions } = resolver;
84

9-
let documentation = null;
5+
const { resolver: { findAllComponentDefinitions } } = docgen;
106

11-
function findComponent(source) {
12-
const code = `var React = require('react');\n${source}`;
13-
return findAllComponentDefinitions(parse(code), recast)[0];
7+
function parse(source, handler) {
8+
const code = `
9+
var React = require('react');
10+
${source}
11+
`;
12+
return docgen.parse(code, findAllComponentDefinitions, [handler])[0];
1413
}
1514

16-
test.beforeEach(() => {
17-
documentation = new Documentation();
18-
});
19-
2015
test('Explicitly set displayName as member of React.createClass', (t) => {
21-
const definition = findComponent(`
16+
const doc = parse(`
2217
var MyComponent = React.createClass({ displayName: 'foo' });
23-
`);
24-
displayNameHandler(documentation, definition);
25-
26-
const actual = documentation.get('displayName');
27-
const expected = 'foo';
18+
`, displayNameHandler);
2819

29-
t.is(actual, expected,
30-
'should set the displayName property on documentation.');
20+
t.deepEqual(
21+
doc,
22+
{ displayName: 'foo' },
23+
'should set the displayName property on documentation.'
24+
);
3125
});
3226

3327
test('Explicitly set displayName as static class member', (t) => {
34-
const definition = findComponent(`
28+
const doc = parse(`
3529
class MyComponent { static displayName = 'foo'; render() {} }
36-
`);
37-
displayNameHandler(documentation, definition);
38-
39-
const actual = documentation.get('displayName');
40-
const expected = 'foo';
30+
`, displayNameHandler);
4131

42-
t.is(actual, expected,
43-
'should set the displayName property on documentation.');
32+
t.deepEqual(
33+
doc,
34+
{ displayName: 'foo' },
35+
'should set the displayName property on documentation.'
36+
);
4437
});
4538

4639
test('Infer displayName from function declaration/expression name', (t) => {
4740
{
48-
const definition = findComponent(`
41+
const doc = parse(`
4942
function MyComponent() { return <div />; }
50-
`);
51-
displayNameHandler(documentation, definition);
52-
53-
const actual = documentation.get('displayName');
54-
const expected = 'MyComponent';
43+
`, displayNameHandler);
5544

56-
t.is(actual, expected, 'should use function name as displayName');
45+
t.deepEqual(
46+
doc,
47+
{ displayName: 'MyComponent' },
48+
'should use function name as displayName'
49+
);
5750
}
5851
{
59-
const definition = findComponent(`
52+
const doc = parse(`
6053
var x = function MyComponent() { return <div />; }
61-
`);
62-
displayNameHandler(documentation, definition);
54+
`, displayNameHandler);
6355

64-
const actual = documentation.get('displayName');
65-
const expected = 'MyComponent';
66-
67-
t.is(actual, expected,
68-
'should also take function name from function expressions');
56+
t.deepEqual(
57+
doc,
58+
{ displayName: 'MyComponent' },
59+
'should also take function name from function expressions'
60+
);
6961
}
7062
});
7163

7264
test('Infer displayName from class declaration/expression name', (t) => {
7365
{
74-
const definition = findComponent(`
66+
const doc = parse(`
7567
class MyComponent { render() {} }
76-
`);
77-
displayNameHandler(documentation, definition);
78-
79-
const actual = documentation.get('displayName');
80-
const expected = 'MyComponent';
68+
`, displayNameHandler);
8169

82-
t.is(actual, expected, 'should use class name as displayName');
70+
t.deepEqual(
71+
doc,
72+
{ displayName: 'MyComponent' },
73+
'should use class name as displayName'
74+
);
8375
}
8476
{
85-
const definition = findComponent(`
77+
const doc = parse(`
8678
var x = class MyComponent { render() {} }
87-
`);
88-
displayNameHandler(documentation, definition);
79+
`, displayNameHandler);
8980

90-
const actual = documentation.get('displayName');
91-
const expected = 'MyComponent';
92-
93-
t.is(actual, expected, 'should also use class name from ClassExpression');
81+
t.deepEqual(
82+
doc,
83+
{ displayName: 'MyComponent' },
84+
'should also use class name from ClassExpression'
85+
);
9486
}
9587
});
9688

9789
test('Infer displayName from variable declaration name', (t) => {
98-
const definition = findComponent(`
90+
const doc = parse(`
9991
var Foo = React.createClass({});
100-
`);
101-
displayNameHandler(documentation, definition);
102-
103-
const actual = documentation.get('displayName');
104-
const expected = 'Foo';
92+
`, displayNameHandler);
10593

106-
t.is(actual, expected,
107-
'should set the displayName property on documentation.');
94+
t.deepEqual(
95+
doc,
96+
{ displayName: 'Foo' },
97+
'should set the displayName property on documentation.'
98+
);
10899
});
109100

110101
test('Infer displayName from assignment', t => {
111-
const definition = findComponent(`
102+
const doc = parse(`
112103
var Foo = {};
113104
Foo.Bar = () => <div />
114-
`);
115-
displayNameHandler(documentation, definition);
116-
117-
const actual = documentation.get('displayName');
118-
const expected = 'Foo.Bar';
105+
`, displayNameHandler);
119106

120-
t.is(
121-
actual,
122-
expected,
107+
t.deepEqual(
108+
doc,
109+
{ displayName: 'Foo.Bar' },
123110
'should set the displayName property on documentation.'
124111
);
125112
})
126113

127114
test('Infer displayName from file name', (t) => {
128-
const definition = findComponent(`
115+
const doc = parse(`
129116
module.exports = () => <div />;
130-
`);
131-
createDisplayNameHandler('foo/bar/MyComponent.js')(documentation, definition);
132-
133-
const actual = documentation.get('displayName');
134-
const expected = 'MyComponent';
117+
`, createDisplayNameHandler('foo/bar/MyComponent.js'));
135118

136-
t.is(actual, expected, 'should use the file name as displayName');
119+
t.deepEqual(
120+
doc,
121+
{ displayName: 'MyComponent' },
122+
'should use the file name as displayName'
123+
);
137124
});
138125

139126
test('Infer displayName from file path', (t) => {
140127
{
141-
const definition = findComponent(`
128+
const doc = parse(`
142129
module.exports = () => <div />;
143-
`);
144-
createDisplayNameHandler('foo/MyComponent/index.js')(documentation, definition);
145-
146-
const actual = documentation.get('displayName');
147-
const expected = 'MyComponent';
130+
`, createDisplayNameHandler('foo/MyComponent/index.js'));
148131

149-
t.is(actual, expected, 'should use the file path as displayName');
132+
t.deepEqual(
133+
doc,
134+
{ displayName: 'MyComponent' },
135+
'should use the file path as displayName'
136+
);
150137
}
151138
{
152-
const definition = findComponent(`
139+
const doc = parse(`
153140
module.exports = () => <div />;
154-
`);
155-
createDisplayNameHandler('foo/my-component/index.js')(documentation, definition);
156-
157-
const actual = documentation.get('displayName');
158-
const expected = 'MyComponent';
141+
`, createDisplayNameHandler('foo/my-component/index.js'));
159142

160-
t.is(actual, expected, 'should replace hyphens with uppercase characters');
143+
t.deepEqual(
144+
doc,
145+
{ displayName: 'MyComponent' },
146+
'should replace hyphens with uppercase characters'
147+
);
161148
}
162149
});
163150

164151
test('Use default if displayName cannot be inferred', (t) => {
165-
const definition = findComponent(`
152+
const doc = parse(`
166153
module.exports = () => <div />;
167-
`);
168-
displayNameHandler(documentation, definition);
154+
`, displayNameHandler);
169155

170-
const actual = documentation.get('displayName');
171-
const expected = 'UnknownComponent';
172-
173-
t.is(actual, expected, 'should use the default name');
156+
t.deepEqual(
157+
doc,
158+
{ displayName: 'UnknownComponent' }, 'should use the default name'
159+
);
174160
});

tests/utils.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

wallaby.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module.exports = function (wallaby) {
22
return {
33
files: [
44
'source/**/*.js',
5-
'tests/**/*.js',
65
'!source/**/*.spec.js',
76
],
87

0 commit comments

Comments
 (0)