@structured-types/prop-types-plugin
TypeScript icon, indicating that this package has built-in type declarations

3.46.12 • Public • Published

Table of contents

Overview

structured-types plugin to extract react prop-types specific properties. If you are using typescript props for your react components, please see the react plugin.

Getting started

1. Installation

$ npm install @structured-types/prop-types-plugin --save-dev

2. Your API source file (Component.tsx):

import React from 'react';
import PropTypes from 'prop-types';

/**
 * MyComponent special component
 */
export const MyComponent = ({ stringProp }) => <div>{stringProp}</div>;

MyComponent.propTypes = {
  /** stringProp description */
  stringProp: PropTypes.string,
  /** numberProp description */
  numberProp: PropTypes.number.isRequired,
};

MyComponent.defaultProps = {
  stringProp: 'test',
};

3. Your documentation extraction

import { parseFiles } from '@structured-types/api';
import propTypesPlugin from '@structured-types/prop-types-plugin';
//optional you can also use react plugin for any typescript react props
import reactPlugin from '@structured-types/react-plugin';

const docs = parseFiles(['../src/components/Component.tsx'], {
  plugins: [propTypesPlugin, reactPlugin],
});

4. The result

{
  "MyComponent": {
    "kind": 11,
    "extension": "react-prop-types",
    "properties": [
      {
        "name": "stringProp",
        "kind": 1,
        "optional": true,
        "description": "stringProp description",
        "value": "test"
      },
      {
        "name": "numberProp",
        "kind": 2,
        "description": "numberProp description"
      }
    ],
    "description": "MyComponent special component",
    "name": "MyComponent"
  }
}

Configuration

The react-prop-types typescript uses the default configurations from the react plugin, and you can also add or modify some of the other parsing options that will be specific only for the found react components. For other typescript/jsdoc types, the global options passed to parseFiles will still apply.

Enable the children props.

By default, the plugin removes the children property that is part of most react components, here is an example of how to re-enable the children properties by disabling the default filter:

import { parseFiles } from '@structured-types/api';
import propTypesPlugin from '@structured-types/prop-types-plugin';

const docs = parseFiles(['../src/components/Component.tsx'], {
  plugins: [{ ...propTypesPlugin, filter: undefined }],
});

ParsePlugin

type

Plugin type - provides the plugin name and the type resolver

defined in @structured-types/api/packages/api/src/ts-utils.ts

properties

Name Type Parent Default Description
tsOptions ts.CompilerOptions DocsOptions
internalTypes Record<string, PropKind> ParseOptions internal types - libs by default includes classes such as String , Function ...
extract string[] ParseOptions list of export names to be extracted. by default all exports are extracted
filter function (
prop*
kind:
name: string
alias: string
parent
name*: string
loc: SourceLocation
loc
filePath: string
loc: SourcePositions
optional: boolean
readonly: boolean
abstract: boolean
async: boolean
visibility: "private" | "protected" | "public"
static: boolean
type: string
extension: string
description: string
fires: string[]
see: string[]
examples: JSDocExample[]
tags: JSDocPropTag[]
summary: string
deprecated: string | true
ignore: boolean
usage: type[]
) => boolean
ParseOptions filter properties function. By default filter out all props with ignore === true
maxDepth number ParseOptions 6 max depth for extracting child props.
collectHelpers boolean ParseOptions whether to save "helper" props that are used by the main parsed props if set to false will result in a smaller result set
collectGenerics boolean ParseOptions true whether to collect generics parameters
collectParameters boolean ParseOptions true whether to collect function parameters
collectParametersUsage boolean ParseOptions whether to collect function parameters usage locations within the function body
collectProperties boolean ParseOptions true whether to collect object/type properties
collectInheritance boolean ParseOptions true whether to collect the inheritance properties
collectExtension boolean ParseOptions true whether to collect the plugin/extension name
collectDiagnostics boolean ParseOptions whether to collect errors/diagnostics
collectAliasName boolean ParseOptions true whether to collect alias names - for example: when imported default symbol from another file, this will be the import name
collectInternals boolean ParseOptions whether to collect internal (typescript) symbols
plugins ParsePlugin[] ParseOptions installed plugins can modify default options and install type resolvers
scope "exports" | "all" ParseOptions by default collects only the exported symbols
collectSourceInfo boolean | "body" ParseOptions whether to collect the file path and the source code location for the symbol declarations. If set to 'body', the source will refer to the function body instead of the variable declaration.
collectInnerLocations boolean ParseOptions whether to collect the source code location for inner symbol declarations if set to true, the data will be collected in the loc prop
moduleCallback function (
module*: Symbol
checker*: TypeChecker
) => void
ParseOptions callback with the parsed module. Can be used to retrieve additional information such as the imports of a file etc.
typesResolve* function (
props*
symbolType*: Type
declaration: ts.Declaration
parser*
checker*: TypeChecker
options*
internalTypes: Record<string, PropKind>
extract: string[]
filter: function (
prop*:
) => boolean
isInternal: function (
file*:
node*:
) => boolean | undefined
maxDepth: number
collectHelpers: boolean
collectGenerics: boolean
collectParameters: boolean
collectParametersUsage: boolean
collectProperties: boolean
collectInheritance: boolean
collectExtension: boolean
collectDiagnostics: boolean
collectAliasName: boolean
collectInternals: boolean
plugins: ParsePlugin[]
scope: "exports" | "all"
collectSourceInfo: boolean | "body"
collectInnerLocations: boolean
moduleCallback: function (
module*:
checker*:
) => void
parseProperties*: function (
properties*
[number]: T
options*
internalTypes*:
extract*:
filter*:
isInternal*:
maxDepth*:
collectHelpers*:
collectGenerics*:
collectParameters*:
collectParametersUsage*:
collectProperties*:
collectInheritance*:
collectExtension*:
collectDiagnostics*:
collectAliasName*:
collectInternals*:
plugins*:
scope*:
collectSourceInfo*:
collectInnerLocations*:
moduleCallback*:
types: PropType[]
) => PropType[]
updateSymbolName*: function (
prop*
kind*:
name*:
alias*:
parent*:
loc*:
optional*:
readonly*:
abstract*:
async*:
visibility*:
static*:
type*:
extension*:
description*:
fires*:
see*:
examples*:
tags*:
summary*:
deprecated*:
ignore*:
usage*:
node: ts.Declaration
) =>
PropType
kind*:
name*:
alias*:
parent*:
loc*:
optional*:
readonly*:
abstract*:
async*:
visibility*:
static*:
type*:
extension*:
description*:
fires*:
see*:
examples*:
tags*:
summary*:
deprecated*:
ignore*:
usage*:

parseType*: function (
prop*
kind*:
name*:
alias*:
parent*:
loc*:
optional*:
readonly*:
abstract*:
async*:
visibility*:
static*:
type*:
extension*:
description*:
fires*:
see*:
examples*:
tags*:
summary*:
deprecated*:
ignore*:
usage*:
options*
internalTypes*:
extract*:
filter*:
isInternal*:
maxDepth*:
collectHelpers*:
collectGenerics*:
collectParameters*:
collectParametersUsage*:
collectProperties*:
collectInheritance*:
collectExtension*:
collectDiagnostics*:
collectAliasName*:
collectInternals*:
plugins*:
scope*:
collectSourceInfo*:
collectInnerLocations*:
moduleCallback*:
node: ts.Node
) =>
PropType
kind*:
name*:
alias*:
parent*:
loc*:
optional*:
readonly*:
abstract*:
async*:
visibility*:
static*:
type*:
extension*:
description*:
fires*:
see*:
examples*:
tags*:
summary*:
deprecated*:
ignore*:
usage*:

parseTypeValueComments*: function (
prop*
kind*:
name*:
alias*:
parent*:
loc*:
optional*:
readonly*:
abstract*:
async*:
visibility*:
static*:
type*:
extension*:
description*:
fires*:
see*:
examples*:
tags*:
summary*:
deprecated*:
ignore*:
usage*:
options*
internalTypes*:
extract*:
filter*:
isInternal*:
maxDepth*:
collectHelpers*:
collectGenerics*:
collectParameters*:
collectParametersUsage*:
collectProperties*:
collectInheritance*:
collectExtension*:
collectDiagnostics*:
collectAliasName*:
collectInternals*:
plugins*:
scope*:
collectSourceInfo*:
collectInnerLocations*:
moduleCallback*:
declaration: ts.Node
initializer: ts.Node
) => PropType | null
parseSymbol*: function (
symbol*: Symbol
options*
internalTypes*:
extract*:
filter*:
isInternal*:
maxDepth*:
collectHelpers*:
collectGenerics*:
collectParameters*:
collectParametersUsage*:
collectProperties*:
collectInheritance*:
collectExtension*:
collectDiagnostics*:
collectAliasName*:
collectInternals*:
plugins*:
scope*:
collectSourceInfo*:
collectInnerLocations*:
moduleCallback*:
) => PropType | undefined
expression: ts.Expression
) => ResolverReturnType | undefined
type resolving custom function ie from a react component will return the props type if the plugin does not recognize the type, should return undefined
pluginName string plugin name

Readme

Keywords

Package Sidebar

Install

npm i @structured-types/prop-types-plugin

Weekly Downloads

2

Version

3.46.12

License

MIT

Unpacked Size

202 kB

Total Files

7

Last publish

Collaborators

  • atanasster