Skip to content

Commit

Permalink
Support annotated resolver in the cli
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Feb 3, 2023
1 parent 7509a12 commit 217a005
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 238 deletions.
11 changes: 11 additions & 0 deletions .changeset/six-cows-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@react-docgen/cli': minor
---

Add support for the `FindAnnotatedDefinitionsResolver`.

Can be used with

```
react-docgen --resolver find-all-annotated-components
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { ChainResolver } = builtinResolvers;
export enum ResolverConfigs {
FindAll = 'find-all-components',
FindAllExported = 'find-all-exported-components',
FindAnnotatedComponents = 'find-all-annotated-components',
FindExported = 'find-exported-component',
}

Expand All @@ -15,6 +16,8 @@ async function loadResolver(input: string): Promise<Resolver> {
return new builtinResolvers.FindAllDefinitionsResolver();
} else if (input === ResolverConfigs.FindAllExported) {
return new builtinResolvers.FindExportedDefinitionsResolver();
} else if (input === ResolverConfigs.FindAnnotatedComponents) {
return new builtinResolvers.FindAnnotatedDefinitionsResolver();
} else if (input === ResolverConfigs.FindExported) {
return new builtinResolvers.FindExportedDefinitionsResolver({
limit: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const React = require('react');

module.exports = React.createClass({
displayName: 'Component',
otherMethod: function () {},
render: function () {},
});
// @component
module.exports = class Component extends React.Component {
displayName = "Component"

otherMethod() {}
render() {}
};
231 changes: 0 additions & 231 deletions packages/react-docgen-cli/tests/integration/cli-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { readFile } from 'fs/promises';
import { join } from 'path';
import { temporaryFile } from 'tempy';
import { describe, expect, test } from 'vitest';
import { builtinHandlers, builtinImporters } from 'react-docgen';
import withFixture from './utils/withFixture';

describe('cli', () => {
Expand Down Expand Up @@ -172,236 +171,6 @@ describe('cli', () => {
});
});

describe('importer', () => {
describe('accepts the names of builtin importers', () => {
test.each(Object.keys(builtinImporters))('%s', async (importer) => {
await withFixture('basic', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--importer=${importer}`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('Component');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});
});

describe('custom importer', () => {
test('accepts an absolute local CommonJS path', async () => {
await withFixture('custom-importer-cjs', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--importer=${join(dir, 'importer.cjs')}`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"importer"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts a relative local CommonJS path', async () => {
await withFixture('custom-importer-cjs', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--importer',
'./importer.cjs',
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"importer"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts an absolute local ESM path', async () => {
await withFixture('custom-importer-esm', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--importer=${join(dir, 'importer.mjs')}`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"importer"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts a relative local ESM path', async () => {
await withFixture('custom-importer-esm', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--importer',
'./importer.mjs',
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"importer"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts a npm package', async () => {
await withFixture('custom-importer-npm', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--importer=test-react-docgen-importer',
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"importer"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('throws error when not found', async () => {
await withFixture('basic', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--importer=does-not-exist',
`${dir}/Component.js`,
]);

expect(stderr).toContain('Unknown importer: "does-not-exist"');
expect(stdout).toBe('');
});
});
});
});

describe('handlers', () => {
describe('accepts the names of builtin handlers', () => {
test.each(Object.keys(builtinHandlers))('%s', async (importer) => {
await withFixture('basic', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--handler=${importer}`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('Component');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});
});

describe('multiple handlers', () => {
test('multiple handlers arguments', async () => {
await withFixture('basic', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--handler=displayNameHandler`,
`--handler=componentDocblockHandler`,
`--handler=componentMethodsHandler`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"Component"');
expect(stdout).toContain('"description":""');
expect(stdout).toContain('"name":"otherMethod"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('multiple handlers comma separated', async () => {
await withFixture('basic', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--handler=displayNameHandler,componentDocblockHandler,componentMethodsHandler`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"Component"');
expect(stdout).toContain('"description":""');
expect(stdout).toContain('"name":"otherMethod"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});
});

describe('custom handlers', () => {
test('accepts an absolute local CommonJS path', async () => {
await withFixture('custom-handler-cjs', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--handler=${join(dir, 'handler.cjs')}`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"testhandler"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts a relative local CommonJS path', async () => {
await withFixture('custom-handler-cjs', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--handler',
'./handler.cjs',
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"testhandler"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts an absolute local ESM path', async () => {
await withFixture('custom-handler-esm', async ({ dir, run }) => {
const { stdout, stderr } = await run([
`--handler=${join(dir, 'handler.mjs')}`,
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"testhandler"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts a relative local ESM path', async () => {
await withFixture('custom-handler-esm', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--handler',
'./handler.mjs',
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"testhandler"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('accepts a npm package', async () => {
await withFixture('custom-handler-npm', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--handler=test-react-docgen-handler',
`${dir}/Component.js`,
]);

expect(stderr).toBe('');
expect(stdout).toContain('"displayName":"testhandler"');
expect(() => JSON.parse(stdout)).not.toThrowError();
});
});

test('throws error when not found', async () => {
await withFixture('basic', async ({ dir, run }) => {
const { stdout, stderr } = await run([
'--handler=does-not-exist',
`${dir}/Component.js`,
]);

expect(stderr).toContain('Unknown handler: "does-not-exist"');
expect(stdout).toBe('');
});
});
});
});

describe('pretty', () => {
test('by default does not prettify output', async () => {
await withFixture('basic', async ({ dir, run }) => {
Expand Down
Loading

0 comments on commit 217a005

Please sign in to comment.