Skip to content

Commit 217a005

Browse files
committed
Support annotated resolver in the cli
1 parent 7509a12 commit 217a005

File tree

7 files changed

+281
-238
lines changed

7 files changed

+281
-238
lines changed

.changeset/six-cows-trade.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@react-docgen/cli': minor
3+
---
4+
5+
Add support for the `FindAnnotatedDefinitionsResolver`.
6+
7+
Can be used with
8+
9+
```
10+
react-docgen --resolver find-all-annotated-components
11+
```

packages/react-docgen-cli/src/commands/parse/options/loadResolvers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { ChainResolver } = builtinResolvers;
77
export enum ResolverConfigs {
88
FindAll = 'find-all-components',
99
FindAllExported = 'find-all-exported-components',
10+
FindAnnotatedComponents = 'find-all-annotated-components',
1011
FindExported = 'find-exported-component',
1112
}
1213

@@ -15,6 +16,8 @@ async function loadResolver(input: string): Promise<Resolver> {
1516
return new builtinResolvers.FindAllDefinitionsResolver();
1617
} else if (input === ResolverConfigs.FindAllExported) {
1718
return new builtinResolvers.FindExportedDefinitionsResolver();
19+
} else if (input === ResolverConfigs.FindAnnotatedComponents) {
20+
return new builtinResolvers.FindAnnotatedDefinitionsResolver();
1821
} else if (input === ResolverConfigs.FindExported) {
1922
return new builtinResolvers.FindExportedDefinitionsResolver({
2023
limit: 1,
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const React = require('react');
22

3-
module.exports = React.createClass({
4-
displayName: 'Component',
5-
otherMethod: function () {},
6-
render: function () {},
7-
});
3+
// @component
4+
module.exports = class Component extends React.Component {
5+
displayName = "Component"
6+
7+
otherMethod() {}
8+
render() {}
9+
};

packages/react-docgen-cli/tests/integration/cli-test.ts

Lines changed: 0 additions & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { readFile } from 'fs/promises';
22
import { join } from 'path';
33
import { temporaryFile } from 'tempy';
44
import { describe, expect, test } from 'vitest';
5-
import { builtinHandlers, builtinImporters } from 'react-docgen';
65
import withFixture from './utils/withFixture';
76

87
describe('cli', () => {
@@ -172,236 +171,6 @@ describe('cli', () => {
172171
});
173172
});
174173

175-
describe('importer', () => {
176-
describe('accepts the names of builtin importers', () => {
177-
test.each(Object.keys(builtinImporters))('%s', async (importer) => {
178-
await withFixture('basic', async ({ dir, run }) => {
179-
const { stdout, stderr } = await run([
180-
`--importer=${importer}`,
181-
`${dir}/Component.js`,
182-
]);
183-
184-
expect(stderr).toBe('');
185-
expect(stdout).toContain('Component');
186-
expect(() => JSON.parse(stdout)).not.toThrowError();
187-
});
188-
});
189-
});
190-
191-
describe('custom importer', () => {
192-
test('accepts an absolute local CommonJS path', async () => {
193-
await withFixture('custom-importer-cjs', async ({ dir, run }) => {
194-
const { stdout, stderr } = await run([
195-
`--importer=${join(dir, 'importer.cjs')}`,
196-
`${dir}/Component.js`,
197-
]);
198-
199-
expect(stderr).toBe('');
200-
expect(stdout).toContain('"displayName":"importer"');
201-
expect(() => JSON.parse(stdout)).not.toThrowError();
202-
});
203-
});
204-
205-
test('accepts a relative local CommonJS path', async () => {
206-
await withFixture('custom-importer-cjs', async ({ dir, run }) => {
207-
const { stdout, stderr } = await run([
208-
'--importer',
209-
'./importer.cjs',
210-
`${dir}/Component.js`,
211-
]);
212-
213-
expect(stderr).toBe('');
214-
expect(stdout).toContain('"displayName":"importer"');
215-
expect(() => JSON.parse(stdout)).not.toThrowError();
216-
});
217-
});
218-
219-
test('accepts an absolute local ESM path', async () => {
220-
await withFixture('custom-importer-esm', async ({ dir, run }) => {
221-
const { stdout, stderr } = await run([
222-
`--importer=${join(dir, 'importer.mjs')}`,
223-
`${dir}/Component.js`,
224-
]);
225-
226-
expect(stderr).toBe('');
227-
expect(stdout).toContain('"displayName":"importer"');
228-
expect(() => JSON.parse(stdout)).not.toThrowError();
229-
});
230-
});
231-
232-
test('accepts a relative local ESM path', async () => {
233-
await withFixture('custom-importer-esm', async ({ dir, run }) => {
234-
const { stdout, stderr } = await run([
235-
'--importer',
236-
'./importer.mjs',
237-
`${dir}/Component.js`,
238-
]);
239-
240-
expect(stderr).toBe('');
241-
expect(stdout).toContain('"displayName":"importer"');
242-
expect(() => JSON.parse(stdout)).not.toThrowError();
243-
});
244-
});
245-
246-
test('accepts a npm package', async () => {
247-
await withFixture('custom-importer-npm', async ({ dir, run }) => {
248-
const { stdout, stderr } = await run([
249-
'--importer=test-react-docgen-importer',
250-
`${dir}/Component.js`,
251-
]);
252-
253-
expect(stderr).toBe('');
254-
expect(stdout).toContain('"displayName":"importer"');
255-
expect(() => JSON.parse(stdout)).not.toThrowError();
256-
});
257-
});
258-
259-
test('throws error when not found', async () => {
260-
await withFixture('basic', async ({ dir, run }) => {
261-
const { stdout, stderr } = await run([
262-
'--importer=does-not-exist',
263-
`${dir}/Component.js`,
264-
]);
265-
266-
expect(stderr).toContain('Unknown importer: "does-not-exist"');
267-
expect(stdout).toBe('');
268-
});
269-
});
270-
});
271-
});
272-
273-
describe('handlers', () => {
274-
describe('accepts the names of builtin handlers', () => {
275-
test.each(Object.keys(builtinHandlers))('%s', async (importer) => {
276-
await withFixture('basic', async ({ dir, run }) => {
277-
const { stdout, stderr } = await run([
278-
`--handler=${importer}`,
279-
`${dir}/Component.js`,
280-
]);
281-
282-
expect(stderr).toBe('');
283-
expect(stdout).toContain('Component');
284-
expect(() => JSON.parse(stdout)).not.toThrowError();
285-
});
286-
});
287-
});
288-
289-
describe('multiple handlers', () => {
290-
test('multiple handlers arguments', async () => {
291-
await withFixture('basic', async ({ dir, run }) => {
292-
const { stdout, stderr } = await run([
293-
`--handler=displayNameHandler`,
294-
`--handler=componentDocblockHandler`,
295-
`--handler=componentMethodsHandler`,
296-
`${dir}/Component.js`,
297-
]);
298-
299-
expect(stderr).toBe('');
300-
expect(stdout).toContain('"displayName":"Component"');
301-
expect(stdout).toContain('"description":""');
302-
expect(stdout).toContain('"name":"otherMethod"');
303-
expect(() => JSON.parse(stdout)).not.toThrowError();
304-
});
305-
});
306-
307-
test('multiple handlers comma separated', async () => {
308-
await withFixture('basic', async ({ dir, run }) => {
309-
const { stdout, stderr } = await run([
310-
`--handler=displayNameHandler,componentDocblockHandler,componentMethodsHandler`,
311-
`${dir}/Component.js`,
312-
]);
313-
314-
expect(stderr).toBe('');
315-
expect(stdout).toContain('"displayName":"Component"');
316-
expect(stdout).toContain('"description":""');
317-
expect(stdout).toContain('"name":"otherMethod"');
318-
expect(() => JSON.parse(stdout)).not.toThrowError();
319-
});
320-
});
321-
});
322-
323-
describe('custom handlers', () => {
324-
test('accepts an absolute local CommonJS path', async () => {
325-
await withFixture('custom-handler-cjs', async ({ dir, run }) => {
326-
const { stdout, stderr } = await run([
327-
`--handler=${join(dir, 'handler.cjs')}`,
328-
`${dir}/Component.js`,
329-
]);
330-
331-
expect(stderr).toBe('');
332-
expect(stdout).toContain('"displayName":"testhandler"');
333-
expect(() => JSON.parse(stdout)).not.toThrowError();
334-
});
335-
});
336-
337-
test('accepts a relative local CommonJS path', async () => {
338-
await withFixture('custom-handler-cjs', async ({ dir, run }) => {
339-
const { stdout, stderr } = await run([
340-
'--handler',
341-
'./handler.cjs',
342-
`${dir}/Component.js`,
343-
]);
344-
345-
expect(stderr).toBe('');
346-
expect(stdout).toContain('"displayName":"testhandler"');
347-
expect(() => JSON.parse(stdout)).not.toThrowError();
348-
});
349-
});
350-
351-
test('accepts an absolute local ESM path', async () => {
352-
await withFixture('custom-handler-esm', async ({ dir, run }) => {
353-
const { stdout, stderr } = await run([
354-
`--handler=${join(dir, 'handler.mjs')}`,
355-
`${dir}/Component.js`,
356-
]);
357-
358-
expect(stderr).toBe('');
359-
expect(stdout).toContain('"displayName":"testhandler"');
360-
expect(() => JSON.parse(stdout)).not.toThrowError();
361-
});
362-
});
363-
364-
test('accepts a relative local ESM path', async () => {
365-
await withFixture('custom-handler-esm', async ({ dir, run }) => {
366-
const { stdout, stderr } = await run([
367-
'--handler',
368-
'./handler.mjs',
369-
`${dir}/Component.js`,
370-
]);
371-
372-
expect(stderr).toBe('');
373-
expect(stdout).toContain('"displayName":"testhandler"');
374-
expect(() => JSON.parse(stdout)).not.toThrowError();
375-
});
376-
});
377-
378-
test('accepts a npm package', async () => {
379-
await withFixture('custom-handler-npm', async ({ dir, run }) => {
380-
const { stdout, stderr } = await run([
381-
'--handler=test-react-docgen-handler',
382-
`${dir}/Component.js`,
383-
]);
384-
385-
expect(stderr).toBe('');
386-
expect(stdout).toContain('"displayName":"testhandler"');
387-
expect(() => JSON.parse(stdout)).not.toThrowError();
388-
});
389-
});
390-
391-
test('throws error when not found', async () => {
392-
await withFixture('basic', async ({ dir, run }) => {
393-
const { stdout, stderr } = await run([
394-
'--handler=does-not-exist',
395-
`${dir}/Component.js`,
396-
]);
397-
398-
expect(stderr).toContain('Unknown handler: "does-not-exist"');
399-
expect(stdout).toBe('');
400-
});
401-
});
402-
});
403-
});
404-
405174
describe('pretty', () => {
406175
test('by default does not prettify output', async () => {
407176
await withFixture('basic', async ({ dir, run }) => {

0 commit comments

Comments
 (0)