Skip to content

Commit e26065d

Browse files
committed
Update handlers to work with class definitions
This also removes the delegation from propTypehandler to propTypeCompositionHandler.
1 parent f956faa commit e26065d

12 files changed

+606
-622
lines changed

src/handlers/__tests__/componentDocblockHandler-test.js

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,77 +8,95 @@
88
*
99
*/
1010

11-
"use strict";
11+
/*global jest, describe, beforeEach, it, expect*/
1212

1313
jest.autoMockOff();
1414
jest.mock('../../Documentation');
1515

1616
describe('componentDocblockHandler', () => {
17-
var utils;
17+
var parse;
1818
var documentation;
1919
var componentDocblockHandler;
2020

21-
function parse(src) {
22-
var programPath = utils.parse(src);
21+
function lastStatement(src) {
22+
var programPath = parse(src);
2323
return programPath.get(
2424
'body',
25-
programPath.node.body.length - 1,
26-
'declarations',
27-
0,
28-
'init',
29-
'arguments',
30-
0
25+
programPath.node.body.length - 1
3126
);
3227
}
3328

3429
beforeEach(() => {
35-
utils = require('../../../tests/utils');
30+
({parse} = require('../../../tests/utils'));
3631
documentation = new (require('../../Documentation'));
3732
componentDocblockHandler = require('../componentDocblockHandler');
3833
});
3934

40-
it('finds docblocks for component definitions', () => {
41-
var definition = parse([
42-
'/**',
43-
' * Component description',
44-
' */',
45-
'var Component = React.createClass({});',
46-
].join('\n'));
35+
function test(definitionSrc, parse) { // eslint-disable-line no-shadow
36+
it('finds docblocks for component definitions', () => {
37+
var definition = parse(`
38+
/**
39+
* Component description
40+
*/
41+
${definitionSrc}
42+
`);
4743

48-
componentDocblockHandler(documentation, definition);
49-
expect(documentation.description).toBe('Component description');
50-
});
44+
componentDocblockHandler(documentation, definition);
45+
expect(documentation.description).toBe('Component description');
46+
});
47+
48+
it('ignores other types of comments', () => {
49+
var definition = parse(`
50+
/*
51+
* This is not a docblock',
52+
*/
53+
${definitionSrc}
54+
`);
55+
56+
componentDocblockHandler(documentation, definition);
57+
expect(documentation.description).toBe('');
58+
59+
definition = parse(`
60+
// Inline comment'
61+
${definitionSrc}
62+
`);
63+
64+
componentDocblockHandler(documentation, definition);
65+
expect(documentation.description).toBe('');
66+
});
5167

52-
it('ignores other types of comments', () => {
53-
var definition = parse([
54-
'/*',
55-
' * This is not a docblock',
56-
' */',
57-
'var Component = React.createClass({});',
58-
].join('\n'));
68+
it('only considers the docblock directly above the definition', () => {
69+
var definition = parse(`
70+
/**
71+
* This is the wrong docblock
72+
*/
73+
var something_else = "foo";
74+
${definitionSrc}
75+
`);
5976

60-
componentDocblockHandler(documentation, definition);
61-
expect(documentation.description).toBe('');
77+
componentDocblockHandler(documentation, definition);
78+
expect(documentation.description).toBe('');
79+
});
80+
}
6281

63-
definition = parse([
64-
'// Inline comment',
65-
'var Component = React.createClass({});',
66-
].join('\n'));
82+
describe('React.createClass', () => {
83+
test(
84+
'var Component = React.createClass({})',
85+
src => lastStatement(src).get('declarations', 0, 'init', 'arguments', 0)
86+
);
87+
});
6788

68-
componentDocblockHandler(documentation, definition);
69-
expect(documentation.description).toBe('');
89+
describe('ClassDeclaration', () => {
90+
test(
91+
'class Component {}',
92+
src => lastStatement(src)
93+
);
7094
});
7195

72-
it('only considers the docblock directly above the definition', () => {
73-
var definition = parse([
74-
'/**',
75-
' * This is the wrong docblock',
76-
' */',
77-
'var something_else = "foo";',
78-
'var Component = React.createClass({});',
79-
].join('\n'));
80-
81-
componentDocblockHandler(documentation, definition);
82-
expect(documentation.description).toBe('');
96+
describe('ClassExpression', () => {
97+
test(
98+
'var Compoent = class {};',
99+
src => lastStatement(src).get('declarations', 0, 'init')
100+
);
83101
});
84102
});

src/handlers/__tests__/defaultPropsHandler-test.js

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,99 @@
88
*
99
*/
1010

11-
"use strict";
11+
/*global jest, describe, beforeEach, it, expect*/
1212

1313
jest.autoMockOff();
1414
jest.mock('../../Documentation');
1515

1616
describe('defaultPropsHandler', () => {
17-
var utils;
1817
var documentation;
19-
var defaultValueHandler;
20-
21-
function parse(src) {
22-
return utils.parse(src).get('body', 0, 'expression');
23-
}
18+
var defaultPropsHandler;
19+
var parse;
2420

2521
beforeEach(() => {
26-
utils = require('../../../tests/utils');
22+
({parse} = require('../../../tests/utils'));
2723
documentation = new (require('../../Documentation'));
2824
defaultPropsHandler = require('../defaultPropsHandler');
2925
});
3026

31-
it ('should find prop default values that are literals', () => {
32-
var definition = parse([
33-
'({',
34-
' getDefaultProps: function() {',
35-
' return {',
36-
' foo: "bar",',
37-
' bar: 42,',
38-
' baz: ["foo", "bar"],',
39-
' abc: {xyz: abc.def, 123: 42}',
40-
' };',
41-
' }',
42-
'});'
43-
].join('\n'));
44-
27+
function test(definition) {
4528
defaultPropsHandler(documentation, definition);
4629
expect(documentation.descriptors).toEqual({
4730
foo: {
4831
defaultValue: {
4932
value: '"bar"',
50-
computed: false
51-
}
33+
computed: false,
34+
},
5235
},
5336
bar: {
5437
defaultValue: {
5538
value: '42',
56-
computed: false
57-
}
39+
computed: false,
40+
},
5841
},
5942
baz: {
6043
defaultValue: {
6144
value: '["foo", "bar"]',
62-
computed: false
63-
}
45+
computed: false,
46+
},
6447
},
6548
abc: {
6649
defaultValue: {
6750
value: '{xyz: abc.def, 123: 42}',
68-
computed: false
51+
computed: false,
52+
},
53+
},
54+
});
55+
}
56+
57+
describe('ObjectExpression', () => {
58+
it('should find prop default values that are literals', () => {
59+
var src = `
60+
({
61+
getDefaultProps: function() {
62+
return {
63+
foo: "bar",
64+
bar: 42,
65+
baz: ["foo", "bar"],
66+
abc: {xyz: abc.def, 123: 42}
67+
};
68+
}
69+
})
70+
`;
71+
test(parse(src).get('body', 0, 'expression'));
72+
});
73+
});
74+
75+
describe('ClassDeclaration with static defaultProps', () => {
76+
it.only('should find prop default values that are literals', () => {
77+
var src = `
78+
class Foo {
79+
static defaultProps = {
80+
foo: "bar",
81+
bar: 42,
82+
baz: ["foo", "bar"],
83+
abc: {xyz: abc.def, 123: 42}
84+
};
6985
}
70-
}
86+
`;
87+
test(parse(src).get('body', 0));
7188
});
7289
});
90+
91+
describe('ClassExpression with static defaultProps', () => {
92+
it('should find prop default values that are literals', () => {
93+
var src = `
94+
var Bar = class {
95+
static defaultProps = {
96+
foo: "bar",
97+
bar: 42,
98+
baz: ["foo", "bar"],
99+
abc: {xyz: abc.def, 123: 42}
100+
};
101+
}`;
102+
test(parse(src).get('body', 0, 'declarations', 0, 'init'));
103+
});
104+
});
105+
73106
});

0 commit comments

Comments
 (0)