Skip to content

Commit

Permalink
Integration updates (#786)
Browse files Browse the repository at this point in the history
* Rename Documentation to DocumentationBuilder

Rename DocumentationObjet to Documentation
Rename toObject to build

* Re-export types from babel

* Remove unnecessary Node types
  • Loading branch information
danez committed Apr 29, 2023
1 parent 622036f commit 0a2481d
Show file tree
Hide file tree
Showing 24 changed files with 94 additions and 66 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-moles-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': minor
---

Export the type for the DocumentationBuilder.
9 changes: 9 additions & 0 deletions .changeset/heavy-plums-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'react-docgen': minor
---

The types `NodePath` and `babelTypes` are now exported.

These types are useful when building integrations in TypeScript.

`babelTypes` includes all types from `@babel/types`.
7 changes: 7 additions & 0 deletions .changeset/smart-trainers-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'react-docgen': major
---

Renamed the method `toObject` to `build` in the DocumentationBuilder.

This method might be used by integrations.
8 changes: 4 additions & 4 deletions packages/react-docgen/src/Documentation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface DocumentationObject {
export interface Documentation {
childContext?: Record<string, PropDescriptor>;
composes?: string[];
context?: Record<string, PropDescriptor>;
Expand Down Expand Up @@ -141,7 +141,7 @@ export interface PropDescriptor {
description?: string;
}

export default class Documentation {
export default class DocumentationBuilder {
#props: Map<string, PropDescriptor>;
#context: Map<string, PropDescriptor>;
#childContext: Map<string, PropDescriptor>;
Expand Down Expand Up @@ -199,8 +199,8 @@ export default class Documentation {
return propDescriptor;
}

toObject(): DocumentationObject {
const obj: DocumentationObject = {};
build(): Documentation {
const obj: Documentation = {};

for (const [key, value] of this.#data) {
obj[key] = value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, makeMockImporter } from '../../../tests/utils';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import codeTypeHandler from '../codeTypeHandler.js';
import type DocumentationMock from '../../__mocks__/Documentation';
import type {
Expand All @@ -20,10 +20,11 @@ vi.mock('../../utils/getFlowType.js', () => ({
}));

describe('codeTypeHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

const mockImporter = makeMockImporter({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeMockImporter, parse } from '../../../tests/utils';
import componentDocblockHandler from '../componentDocblockHandler.js';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import type DocumentationMock from '../../__mocks__/Documentation';
import type { NodePath } from '@babel/traverse';
import type {
Expand All @@ -20,10 +20,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
vi.mock('../../Documentation.js');

describe('componentDocblockHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

function testDockblockHandler(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse, makeMockImporter } from '../../../tests/utils';
import componentMethodsHandler from '../componentMethodsHandler.js';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import type DocumentationMock from '../../__mocks__/Documentation';
import type {
ArrowFunctionExpression,
Expand All @@ -18,10 +18,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
vi.mock('../../Documentation.js');

describe('componentMethodsHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

const mockImporter = makeMockImporter({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { NodePath } from '@babel/traverse';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import type { ComponentNode } from '../../resolver';
import type DocumentationMock from '../../__mocks__/Documentation';
import componentMethodsJsDocHandler from '../componentMethodsJsDocHandler.js';
import { beforeEach, describe, expect, test } from 'vitest';

describe('componentMethodsJsDocHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

test('stays the same when no docblock is present', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import type {
VariableDeclaration,
} from '@babel/types';
import { parse, makeMockImporter } from '../../../tests/utils';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import type DocumentationMock from '../../__mocks__/Documentation';
import defaultPropsHandler from '../defaultPropsHandler.js';
import { beforeEach, describe, expect, test, vi } from 'vitest';

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

describe('defaultPropsHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

const mockImporter = makeMockImporter({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, makeMockImporter } from '../../../tests/utils';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import displayNameHandler from '../displayNameHandler.js';
import type DocumentationMock from '../../__mocks__/Documentation';
import type {
Expand All @@ -17,10 +17,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
vi.mock('../../Documentation.js');

describe('defaultPropsHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

const mockImporter = makeMockImporter({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NodePath } from '@babel/traverse';
import type { ClassDeclaration, ObjectExpression } from '@babel/types';
import { parse, makeMockImporter, noopImporter } from '../../../tests/utils';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import type { Importer } from '../../importer';
import type { ComponentNode } from '../../resolver';
import type DocumentationMock from '../../__mocks__/Documentation';
Expand All @@ -11,10 +11,11 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
vi.mock('../../Documentation.js');

describe('propDocblockHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

const mockImporter = makeMockImporter({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parse, makeMockImporter, noopImporter } from '../../../tests/utils';
import propTypeCompositionHandler from '../propTypeCompositionHandler.js';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import type DocumentationMock from '../../__mocks__/Documentation';
import type { NodePath } from '@babel/traverse';
import type { Importer } from '../../importer';
Expand All @@ -12,10 +12,11 @@ vi.mock('../../Documentation.js');
vi.mock('../../utils/getPropType.js', () => () => ({}));

describe('propTypeCompositionHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

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

documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
definition = parseSrc(`
${getSrc('SharedProps')}
var SharedProps = require("SharedProps");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parse, makeMockImporter, noopImporter } from '../../../tests/utils';
import Documentation from '../../Documentation';
import DocumentationBuilder from '../../Documentation';
import type DocumentationMock from '../../__mocks__/Documentation';
import { propTypeHandler } from '../propTypeHandler.js';
import getPropType from '../../utils/getPropType';
Expand All @@ -20,10 +20,11 @@ vi.mock('../../Documentation.js');
vi.mock('../../utils/getPropType.js', () => ({ default: vi.fn(() => ({})) }));

describe('propTypeHandler', () => {
let documentation: Documentation & DocumentationMock;
let documentation: DocumentationBuilder & DocumentationMock;

beforeEach(() => {
documentation = new Documentation() as Documentation & DocumentationMock;
documentation = new DocumentationBuilder() as DocumentationBuilder &
DocumentationMock;
});

const mockImporter = makeMockImporter({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type Documentation from '../Documentation.js';
import { getDocblock } from '../utils/docblock.js';
import isReactForwardRefCall from '../utils/isReactForwardRefCall.js';
import resolveToValue from '../utils/resolveToValue.js';
import type { NodePath, Node } from '@babel/traverse';
import type { NodePath } from '@babel/traverse';
import type { ComponentNode } from '../resolver/index.js';
import type { Handler } from './index.js';

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

while (searchPath && !searchPath.isStatement()) {
searchPath = searchPath.parentPath;
Expand Down
3 changes: 1 addition & 2 deletions packages/react-docgen/src/handlers/defaultPropsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type Documentation from '../Documentation.js';
import type { DefaultValueDescriptor } from '../Documentation.js';
import type { NodePath } from '@babel/traverse';
import type {
Node,
ObjectMethod,
ObjectProperty,
RestElement,
Expand Down Expand Up @@ -70,7 +69,7 @@ function getStatelessPropsPath(
function getDefaultPropsPath(
componentDefinition: NodePath<ComponentNode>,
): NodePath | null {
let defaultPropsPath: NodePath<Node> | null = getMemberValuePath(
let defaultPropsPath: NodePath | null = getMemberValuePath(
componentDefinition,
'defaultProps',
);
Expand Down
3 changes: 1 addition & 2 deletions packages/react-docgen/src/handlers/propDocblockHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { NodePath } from '@babel/traverse';
import type { Node } from '@babel/types';
import getMemberValuePath from '../utils/getMemberValuePath.js';
import resolveToValue from '../utils/resolveToValue.js';
import setPropDescription from '../utils/setPropDescription.js';
Expand Down Expand Up @@ -33,7 +32,7 @@ const propDocblockHandler: Handler = function (
documentation: Documentation,
componentDefinition: NodePath<ComponentNode>,
): void {
let propTypesPath: NodePath<Node> | null = getMemberValuePath(
let propTypesPath: NodePath | null = getMemberValuePath(
componentDefinition,
'propTypes',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import resolveToModule from '../utils/resolveToModule.js';
import resolveToValue from '../utils/resolveToValue.js';
import type Documentation from '../Documentation.js';
import type { NodePath } from '@babel/traverse';
import type { ObjectExpression, Node } from '@babel/types';
import type { ObjectExpression } from '@babel/types';
import type { Handler } from './index.js';
import type { ComponentNode } from '../resolver/index.js';

Expand Down Expand Up @@ -37,7 +37,7 @@ const propTypeCompositionHandler: Handler = function (
documentation: Documentation,
componentDefinition: NodePath<ComponentNode>,
): void {
let propTypesPath: NodePath<Node> | null = getMemberValuePath(
let propTypesPath: NodePath | null = getMemberValuePath(
componentDefinition,
'propTypes',
);
Expand Down
3 changes: 1 addition & 2 deletions packages/react-docgen/src/handlers/propTypeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import resolveToValue from '../utils/resolveToValue.js';
import type Documentation from '../Documentation.js';
import type { PropDescriptor, PropTypeDescriptor } from '../Documentation.js';
import type { NodePath } from '@babel/traverse';
import type { Node } from '@babel/types';
import type { Handler } from './index.js';
import type { ComponentNode } from '../resolver/index.js';

Expand Down Expand Up @@ -65,7 +64,7 @@ function getPropTypeHandler(propName: string): Handler {
documentation: Documentation,
componentDefinition: NodePath<ComponentNode>,
): void {
let propTypesPath: NodePath<Node> | null = getMemberValuePath(
let propTypesPath: NodePath | null = getMemberValuePath(
componentDefinition,
propName,
);
Expand Down
7 changes: 6 additions & 1 deletion packages/react-docgen/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
makeFsImporter,
} from './importer/index.js';
import * as utils from './utils/index.js';
import type { DocumentationObject as Documentation } from './Documentation.js';
import type { Documentation } from './Documentation.js';
import type DocumentationBuilder from './Documentation.js';
import type {
Resolver,
ResolverClass,
Expand Down Expand Up @@ -60,6 +61,9 @@ function defaultParse(
return parse(String(src), defaultConfig);
}

export type { NodePath } from '@babel/traverse';
export type * as babelTypes from '@babel/types';

export {
builtinHandlers,
builtinResolvers,
Expand All @@ -80,4 +84,5 @@ export type {
FileState,
Config,
Documentation,
DocumentationBuilder,
};

0 comments on commit 0a2481d

Please sign in to comment.