Skip to content

Commit 0a2481d

Browse files
authored
Integration updates (#786)
* Rename Documentation to DocumentationBuilder Rename DocumentationObjet to Documentation Rename toObject to build * Re-export types from babel * Remove unnecessary Node types
1 parent 622036f commit 0a2481d

24 files changed

+94
-66
lines changed

.changeset/healthy-moles-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': minor
3+
---
4+
5+
Export the type for the DocumentationBuilder.

.changeset/heavy-plums-repair.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'react-docgen': minor
3+
---
4+
5+
The types `NodePath` and `babelTypes` are now exported.
6+
7+
These types are useful when building integrations in TypeScript.
8+
9+
`babelTypes` includes all types from `@babel/types`.

.changeset/smart-trainers-film.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'react-docgen': major
3+
---
4+
5+
Renamed the method `toObject` to `build` in the DocumentationBuilder.
6+
7+
This method might be used by integrations.

packages/react-docgen/src/Documentation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export interface DocumentationObject {
1+
export interface Documentation {
22
childContext?: Record<string, PropDescriptor>;
33
composes?: string[];
44
context?: Record<string, PropDescriptor>;
@@ -141,7 +141,7 @@ export interface PropDescriptor {
141141
description?: string;
142142
}
143143

144-
export default class Documentation {
144+
export default class DocumentationBuilder {
145145
#props: Map<string, PropDescriptor>;
146146
#context: Map<string, PropDescriptor>;
147147
#childContext: Map<string, PropDescriptor>;
@@ -199,8 +199,8 @@ export default class Documentation {
199199
return propDescriptor;
200200
}
201201

202-
toObject(): DocumentationObject {
203-
const obj: DocumentationObject = {};
202+
build(): Documentation {
203+
const obj: Documentation = {};
204204

205205
for (const [key, value] of this.#data) {
206206
obj[key] = value;

packages/react-docgen/src/handlers/__tests__/codeTypeHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse, makeMockImporter } from '../../../tests/utils';
2-
import Documentation from '../../Documentation';
2+
import DocumentationBuilder from '../../Documentation';
33
import codeTypeHandler from '../codeTypeHandler.js';
44
import type DocumentationMock from '../../__mocks__/Documentation';
55
import type {
@@ -20,10 +20,11 @@ vi.mock('../../utils/getFlowType.js', () => ({
2020
}));
2121

2222
describe('codeTypeHandler', () => {
23-
let documentation: Documentation & DocumentationMock;
23+
let documentation: DocumentationBuilder & DocumentationMock;
2424

2525
beforeEach(() => {
26-
documentation = new Documentation() as Documentation & DocumentationMock;
26+
documentation = new DocumentationBuilder() as DocumentationBuilder &
27+
DocumentationMock;
2728
});
2829

2930
const mockImporter = makeMockImporter({

packages/react-docgen/src/handlers/__tests__/componentDocblockHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { makeMockImporter, parse } from '../../../tests/utils';
22
import componentDocblockHandler from '../componentDocblockHandler.js';
3-
import Documentation from '../../Documentation';
3+
import DocumentationBuilder from '../../Documentation';
44
import type DocumentationMock from '../../__mocks__/Documentation';
55
import type { NodePath } from '@babel/traverse';
66
import type {
@@ -20,10 +20,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
2020
vi.mock('../../Documentation.js');
2121

2222
describe('componentDocblockHandler', () => {
23-
let documentation: Documentation & DocumentationMock;
23+
let documentation: DocumentationBuilder & DocumentationMock;
2424

2525
beforeEach(() => {
26-
documentation = new Documentation() as Documentation & DocumentationMock;
26+
documentation = new DocumentationBuilder() as DocumentationBuilder &
27+
DocumentationMock;
2728
});
2829

2930
function testDockblockHandler(

packages/react-docgen/src/handlers/__tests__/componentMethodsHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse, makeMockImporter } from '../../../tests/utils';
22
import componentMethodsHandler from '../componentMethodsHandler.js';
3-
import Documentation from '../../Documentation';
3+
import DocumentationBuilder from '../../Documentation';
44
import type DocumentationMock from '../../__mocks__/Documentation';
55
import type {
66
ArrowFunctionExpression,
@@ -18,10 +18,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
1818
vi.mock('../../Documentation.js');
1919

2020
describe('componentMethodsHandler', () => {
21-
let documentation: Documentation & DocumentationMock;
21+
let documentation: DocumentationBuilder & DocumentationMock;
2222

2323
beforeEach(() => {
24-
documentation = new Documentation() as Documentation & DocumentationMock;
24+
documentation = new DocumentationBuilder() as DocumentationBuilder &
25+
DocumentationMock;
2526
});
2627

2728
const mockImporter = makeMockImporter({

packages/react-docgen/src/handlers/__tests__/componentMethodsJsDocHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { NodePath } from '@babel/traverse';
2-
import Documentation from '../../Documentation';
2+
import DocumentationBuilder from '../../Documentation';
33
import type { ComponentNode } from '../../resolver';
44
import type DocumentationMock from '../../__mocks__/Documentation';
55
import componentMethodsJsDocHandler from '../componentMethodsJsDocHandler.js';
66
import { beforeEach, describe, expect, test } from 'vitest';
77

88
describe('componentMethodsJsDocHandler', () => {
9-
let documentation: Documentation & DocumentationMock;
9+
let documentation: DocumentationBuilder & DocumentationMock;
1010

1111
beforeEach(() => {
12-
documentation = new Documentation() as Documentation & DocumentationMock;
12+
documentation = new DocumentationBuilder() as DocumentationBuilder &
13+
DocumentationMock;
1314
});
1415

1516
test('stays the same when no docblock is present', () => {

packages/react-docgen/src/handlers/__tests__/defaultPropsHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ import type {
88
VariableDeclaration,
99
} from '@babel/types';
1010
import { parse, makeMockImporter } from '../../../tests/utils';
11-
import Documentation from '../../Documentation';
11+
import DocumentationBuilder from '../../Documentation';
1212
import type DocumentationMock from '../../__mocks__/Documentation';
1313
import defaultPropsHandler from '../defaultPropsHandler.js';
1414
import { beforeEach, describe, expect, test, vi } from 'vitest';
1515

1616
vi.mock('../../Documentation.js');
1717

1818
describe('defaultPropsHandler', () => {
19-
let documentation: Documentation & DocumentationMock;
19+
let documentation: DocumentationBuilder & DocumentationMock;
2020

2121
beforeEach(() => {
22-
documentation = new Documentation() as Documentation & DocumentationMock;
22+
documentation = new DocumentationBuilder() as DocumentationBuilder &
23+
DocumentationMock;
2324
});
2425

2526
const mockImporter = makeMockImporter({

packages/react-docgen/src/handlers/__tests__/displayNameHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse, makeMockImporter } from '../../../tests/utils';
2-
import Documentation from '../../Documentation';
2+
import DocumentationBuilder from '../../Documentation';
33
import displayNameHandler from '../displayNameHandler.js';
44
import type DocumentationMock from '../../__mocks__/Documentation';
55
import type {
@@ -17,10 +17,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
1717
vi.mock('../../Documentation.js');
1818

1919
describe('defaultPropsHandler', () => {
20-
let documentation: Documentation & DocumentationMock;
20+
let documentation: DocumentationBuilder & DocumentationMock;
2121

2222
beforeEach(() => {
23-
documentation = new Documentation() as Documentation & DocumentationMock;
23+
documentation = new DocumentationBuilder() as DocumentationBuilder &
24+
DocumentationMock;
2425
});
2526

2627
const mockImporter = makeMockImporter({

packages/react-docgen/src/handlers/__tests__/propDocblockHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { NodePath } from '@babel/traverse';
22
import type { ClassDeclaration, ObjectExpression } from '@babel/types';
33
import { parse, makeMockImporter, noopImporter } from '../../../tests/utils';
4-
import Documentation from '../../Documentation';
4+
import DocumentationBuilder from '../../Documentation';
55
import type { Importer } from '../../importer';
66
import type { ComponentNode } from '../../resolver';
77
import type DocumentationMock from '../../__mocks__/Documentation';
@@ -11,10 +11,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
1111
vi.mock('../../Documentation.js');
1212

1313
describe('propDocblockHandler', () => {
14-
let documentation: Documentation & DocumentationMock;
14+
let documentation: DocumentationBuilder & DocumentationMock;
1515

1616
beforeEach(() => {
17-
documentation = new Documentation() as Documentation & DocumentationMock;
17+
documentation = new DocumentationBuilder() as DocumentationBuilder &
18+
DocumentationMock;
1819
});
1920

2021
const mockImporter = makeMockImporter({

packages/react-docgen/src/handlers/__tests__/propTypeCompositionHandler-test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse, makeMockImporter, noopImporter } from '../../../tests/utils';
22
import propTypeCompositionHandler from '../propTypeCompositionHandler.js';
3-
import Documentation from '../../Documentation';
3+
import DocumentationBuilder from '../../Documentation';
44
import type DocumentationMock from '../../__mocks__/Documentation';
55
import type { NodePath } from '@babel/traverse';
66
import type { Importer } from '../../importer';
@@ -12,10 +12,11 @@ vi.mock('../../Documentation.js');
1212
vi.mock('../../utils/getPropType.js', () => () => ({}));
1313

1414
describe('propTypeCompositionHandler', () => {
15-
let documentation: Documentation & DocumentationMock;
15+
let documentation: DocumentationBuilder & DocumentationMock;
1616

1717
beforeEach(() => {
18-
documentation = new Documentation() as Documentation & DocumentationMock;
18+
documentation = new DocumentationBuilder() as DocumentationBuilder &
19+
DocumentationMock;
1920
});
2021

2122
const mockImporter = makeMockImporter({
@@ -49,7 +50,8 @@ describe('propTypeCompositionHandler', () => {
4950
propTypeCompositionHandler(documentation, definition);
5051
expect(documentation.composes).toEqual(['Foo.react']);
5152

52-
documentation = new Documentation() as Documentation & DocumentationMock;
53+
documentation = new DocumentationBuilder() as DocumentationBuilder &
54+
DocumentationMock;
5355
definition = parseSrc(`
5456
${getSrc('SharedProps')}
5557
var SharedProps = require("SharedProps");

packages/react-docgen/src/handlers/__tests__/propTypeHandler-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse, makeMockImporter, noopImporter } from '../../../tests/utils';
2-
import Documentation from '../../Documentation';
2+
import DocumentationBuilder from '../../Documentation';
33
import type DocumentationMock from '../../__mocks__/Documentation';
44
import { propTypeHandler } from '../propTypeHandler.js';
55
import getPropType from '../../utils/getPropType';
@@ -20,10 +20,11 @@ vi.mock('../../Documentation.js');
2020
vi.mock('../../utils/getPropType.js', () => ({ default: vi.fn(() => ({})) }));
2121

2222
describe('propTypeHandler', () => {
23-
let documentation: Documentation & DocumentationMock;
23+
let documentation: DocumentationBuilder & DocumentationMock;
2424

2525
beforeEach(() => {
26-
documentation = new Documentation() as Documentation & DocumentationMock;
26+
documentation = new DocumentationBuilder() as DocumentationBuilder &
27+
DocumentationMock;
2728
});
2829

2930
const mockImporter = makeMockImporter({

packages/react-docgen/src/handlers/componentDocblockHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type Documentation from '../Documentation.js';
22
import { getDocblock } from '../utils/docblock.js';
33
import isReactForwardRefCall from '../utils/isReactForwardRefCall.js';
44
import resolveToValue from '../utils/resolveToValue.js';
5-
import type { NodePath, Node } from '@babel/traverse';
5+
import type { NodePath } from '@babel/traverse';
66
import type { ComponentNode } from '../resolver/index.js';
77
import type { Handler } from './index.js';
88

@@ -21,7 +21,7 @@ function getDocblockFromComponent(path: NodePath): string | null {
2121
}
2222
if (description == null) {
2323
// Find parent statement (e.g. var Component = React.createClass(<path>);)
24-
let searchPath: NodePath<Node> | null = path;
24+
let searchPath: NodePath | null = path;
2525

2626
while (searchPath && !searchPath.isStatement()) {
2727
searchPath = searchPath.parentPath;

packages/react-docgen/src/handlers/defaultPropsHandler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type Documentation from '../Documentation.js';
99
import type { DefaultValueDescriptor } from '../Documentation.js';
1010
import type { NodePath } from '@babel/traverse';
1111
import type {
12-
Node,
1312
ObjectMethod,
1413
ObjectProperty,
1514
RestElement,
@@ -70,7 +69,7 @@ function getStatelessPropsPath(
7069
function getDefaultPropsPath(
7170
componentDefinition: NodePath<ComponentNode>,
7271
): NodePath | null {
73-
let defaultPropsPath: NodePath<Node> | null = getMemberValuePath(
72+
let defaultPropsPath: NodePath | null = getMemberValuePath(
7473
componentDefinition,
7574
'defaultProps',
7675
);

packages/react-docgen/src/handlers/propDocblockHandler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { NodePath } from '@babel/traverse';
2-
import type { Node } from '@babel/types';
32
import getMemberValuePath from '../utils/getMemberValuePath.js';
43
import resolveToValue from '../utils/resolveToValue.js';
54
import setPropDescription from '../utils/setPropDescription.js';
@@ -33,7 +32,7 @@ const propDocblockHandler: Handler = function (
3332
documentation: Documentation,
3433
componentDefinition: NodePath<ComponentNode>,
3534
): void {
36-
let propTypesPath: NodePath<Node> | null = getMemberValuePath(
35+
let propTypesPath: NodePath | null = getMemberValuePath(
3736
componentDefinition,
3837
'propTypes',
3938
);

packages/react-docgen/src/handlers/propTypeCompositionHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import resolveToModule from '../utils/resolveToModule.js';
33
import resolveToValue from '../utils/resolveToValue.js';
44
import type Documentation from '../Documentation.js';
55
import type { NodePath } from '@babel/traverse';
6-
import type { ObjectExpression, Node } from '@babel/types';
6+
import type { ObjectExpression } from '@babel/types';
77
import type { Handler } from './index.js';
88
import type { ComponentNode } from '../resolver/index.js';
99

@@ -37,7 +37,7 @@ const propTypeCompositionHandler: Handler = function (
3737
documentation: Documentation,
3838
componentDefinition: NodePath<ComponentNode>,
3939
): void {
40-
let propTypesPath: NodePath<Node> | null = getMemberValuePath(
40+
let propTypesPath: NodePath | null = getMemberValuePath(
4141
componentDefinition,
4242
'propTypes',
4343
);

packages/react-docgen/src/handlers/propTypeHandler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import resolveToValue from '../utils/resolveToValue.js';
99
import type Documentation from '../Documentation.js';
1010
import type { PropDescriptor, PropTypeDescriptor } from '../Documentation.js';
1111
import type { NodePath } from '@babel/traverse';
12-
import type { Node } from '@babel/types';
1312
import type { Handler } from './index.js';
1413
import type { ComponentNode } from '../resolver/index.js';
1514

@@ -65,7 +64,7 @@ function getPropTypeHandler(propName: string): Handler {
6564
documentation: Documentation,
6665
componentDefinition: NodePath<ComponentNode>,
6766
): void {
68-
let propTypesPath: NodePath<Node> | null = getMemberValuePath(
67+
let propTypesPath: NodePath | null = getMemberValuePath(
6968
componentDefinition,
7069
propName,
7170
);

packages/react-docgen/src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
makeFsImporter,
88
} from './importer/index.js';
99
import * as utils from './utils/index.js';
10-
import type { DocumentationObject as Documentation } from './Documentation.js';
10+
import type { Documentation } from './Documentation.js';
11+
import type DocumentationBuilder from './Documentation.js';
1112
import type {
1213
Resolver,
1314
ResolverClass,
@@ -60,6 +61,9 @@ function defaultParse(
6061
return parse(String(src), defaultConfig);
6162
}
6263

64+
export type { NodePath } from '@babel/traverse';
65+
export type * as babelTypes from '@babel/types';
66+
6367
export {
6468
builtinHandlers,
6569
builtinResolvers,
@@ -80,4 +84,5 @@ export type {
8084
FileState,
8185
Config,
8286
Documentation,
87+
DocumentationBuilder,
8388
};

0 commit comments

Comments
 (0)