3.46.12 • Published 2 years ago

@structured-types/api v3.46.12

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Table of contents

Overview

Extract structured documentation from javascript and typescript files using a combination of typescript types and jsdoc comments.

Comparable libraries

jsdoc typedoc ts-json-schema-generator documentation.js react-docgen-typescript react-docgen

Motivation

The creation of structured-types comes from the need for a library that can be used to document as well as instrument typescript and javascript code. The currently existing libraries are mostly meant just for documenting code.

  • Extract fully structured types, that can be used to fully interact with the analyzed code - this can be used to automatically create tests, examples, etc.
  • Use typescript types if available and supplement the type information with any jsdoc comments.
  • Extract documentation down to the member-level - for example for an enum extract comments for the enum type, as well as for the individual enum member fields.
  • Swiss-army extensible architecture using resolution plugins, where the library can be used to analyze typescript files, as well as extract react, angular, and more framework-specific types.

Getting started

1. Installation

$ npm install @structured-types/api --save-dev

2. Your API source file (sum.js):

/**
 * sum api function
 * @remarks
 * Unlike the summary, the remarks block may contain lengthy documentation content.
 * The remarks should not restate information from the summary, since the summary section
 * will always be displayed wherever the remarks section appears.  Other sections
 * (e.g. an `@example` block) will be shown after the remarks section.
 *
 * @param {number} a first parameter to add
 * @param {number} b second parameter to add
 * @returns {number} the sum of the two parameters
 *
 * @example
 *
 * ```js
 * import { sum } from './sum';
 *
 * expect(sum(1, 2)).toMatchObject({ a: 1, b: 2, result: 3});
 * ```
 */
export const sum = (a, b = 1) => ({ a, b, result: a + b });

3. Your documentation extraction

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

const docs = parseFiles(['../src/sum.js']);

4. The result

{
  "sum": {
    "name": "sum",
    "kind": 11,
    "parameters": [
      {
        "kind": 2,
        "name": "a",
        "description": "first parameter to add"
      },
      {
        "kind": 2,
        "name": "b",
        "value": 1,
        "description": "second parameter to add"
      }
    ],
    "examples": [
      {
        "content": "```js\nimport { sum } from './sum';\n\nexpect(sum(1, 2)).toMatchObject({ a: 1, b: 2, result: 3});\n```"
      }
    ],
    "returns": {
      "description": "the sum of the two parameters",
      "kind": 2
    },
    "tags": [
      {
        "tag": "remarks",
        "content": "Unlike the summary, the remarks block may contain lengthy documentation content.\nThe remarks should not restate information from the summary, since the summary section\nwill always be displayed wherever the remarks section appears.  Other sections\n(e.g. an `@example` block) will be shown after the remarks section."
      }
    ],
    "description": "sum api function"
  }
}

API

parseFiles

function

API to analyze the given files by also loading the local typescript options from tsconfig

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

parameters

NameTypeDescription
files*string[]list of files to be processed
options*DocsOptionsparsing options
programOptionsProgramOptionstypescript ts.program and ts.compilerHost
returnsPropTypesthe parsed types

example

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

const props = parseFiles(['index.ts'], {
 collectHelpers: true,
 collectSourceInfo: true,
})

analyzeFiles

function

API to analyze the given files

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

parameters

NameTypeDescription
files*string[]list of files to be processed
options*DocsOptionsparsing options
programOptions*ProgramOptionstypescript ts.program and ts.compilerHost
returnsPropTypesthe parsed types

example

import { analyzeFiles } from '@structured-types/api';

const props = analyzeFiles(['index.ts'], {
 collectHelpers: true,
 collectSourceInfo: true,
 tsOptions: {
   allowJs: true,
 }
})

DocsOptions

type

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

properties

NameTypeParentDefaultDescription
tsOptionsts.CompilerOptions
internalTypesRecord<string, PropKind>ParseOptionsinternal types - libs by default includes classes such as String , Function ...
extractstring[]ParseOptionslist of export names to be extracted. by default all exports are extracted
filterfunction (prop*: PropType) => booleanParseOptionsfilter properties function. By default filter out all props with ignore === true
isInternalfunction (file*: SourceFilenode*: Node) => boolean | undefinedParseOptionscallback function to determine if a node is an internal (typescript) symbol return undefined if you need to use the default isInternal processing
maxDepthnumberParseOptions6max depth for extracting child props.
collectHelpersbooleanParseOptionswhether to save "helper" props that are used by the main parsed props if set to false will result in a smaller result set
collectGenericsbooleanParseOptionstruewhether to collect generics parameters
collectParametersbooleanParseOptionstruewhether to collect function parameters
collectParametersUsagebooleanParseOptionswhether to collect function parameters usage locations within the function body
collectPropertiesbooleanParseOptionstruewhether to collect object/type properties
collectInheritancebooleanParseOptionstruewhether to collect the inheritance properties
collectExtensionbooleanParseOptionstruewhether to collect the plugin/extension name
collectDiagnosticsbooleanParseOptionswhether to collect errors/diagnostics
collectAliasNamebooleanParseOptionstruewhether to collect alias names - for example: when imported default symbol from another file, this will be the import name
collectInternalsbooleanParseOptionswhether to collect internal (typescript) symbols
pluginsParsePlugin[]ParseOptionsinstalled plugins can modify default options and install type resolvers
scope"exports" | "all"ParseOptionsby default collects only the exported symbols
collectSourceInfoboolean | "body"ParseOptionswhether 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.
collectInnerLocationsbooleanParseOptionswhether to collect the source code location for inner symbol declarations if set to true, the data will be collected in the loc prop
moduleCallbackfunction (module*: Symbolchecker*: TypeChecker) => voidParseOptionscallback with the parsed module. Can be used to retrieve additional information such as the imports of a file etc.

ParseOptions

interface

parsing options

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

properties

NameTypeDefaultDescription
internalTypesRecord<string, PropKind>internal types - libs by default includes classes such as String , Function ...
extractstring[]list of export names to be extracted. by default all exports are extracted
filterfunction (prop*: PropType) => booleanfilter properties function. By default filter out all props with ignore === true
isInternalfunction (file*: SourceFilenode*: Node) => boolean | undefinedcallback function to determine if a node is an internal (typescript) symbol return undefined if you need to use the default isInternal processing
maxDepthnumber6max depth for extracting child props.
collectHelpersbooleanwhether to save "helper" props that are used by the main parsed props if set to false will result in a smaller result set
collectGenericsbooleantruewhether to collect generics parameters
collectParametersbooleantruewhether to collect function parameters
collectParametersUsagebooleanwhether to collect function parameters usage locations within the function body
collectPropertiesbooleantruewhether to collect object/type properties
collectInheritancebooleantruewhether to collect the inheritance properties
collectExtensionbooleantruewhether to collect the plugin/extension name
collectDiagnosticsbooleanwhether to collect errors/diagnostics
collectAliasNamebooleantruewhether to collect alias names - for example: when imported default symbol from another file, this will be the import name
collectInternalsbooleanwhether to collect internal (typescript) symbols
pluginsParsePlugin[]installed plugins can modify default options and install type resolvers
scope"exports" | "all"by default collects only the exported symbols
collectSourceInfoboolean | "body"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.
collectInnerLocationsbooleanwhether to collect the source code location for inner symbol declarations if set to true, the data will be collected in the loc prop
moduleCallbackfunction (module*: Symbolchecker*: TypeChecker) => voidcallback with the parsed module. Can be used to retrieve additional information such as the imports of a file etc.

ProgramOptions

type

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

properties

NameTypeDescription
hostts.CompilerHost
programts.Program
hostCallbackfunction (host*: CompilerHost) => voidcallback with the created host, gives an opportunity to change some properties of the host.

PropTypes

type

Top-level prop type, with added optional __helpers and __diagnostics fields.

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

properties

NameTypeDescription
anonymous[string]: PropType
__helpersRecord<string, PropType>Utility symbols such as parent types are stored here. Only available if option collectHelpers is set to true.
__diagnosticsPropDiagnostic[]Typescript program diagnostics / errors. Only available if option collectDiagnostics is set to true.

PropType

interface

Base prop type interface

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

properties

NameTypeDescription
kindPropKindgeneric properties
namestringname of the property
aliasstringalias - when the assigned property is being imported as an aliased name
parentPropParentParent of a property field
locSourceLocationsource location of the symbol and source file position will be available when collectSourceInfo option is set to true
optionalbooleanby default, properties are required
readonlybooleanreadonly property
abstractbooleanabstract property
asyncbooleanasync function
visibility"private" | "protected" | "public"property visibility
staticbooleantrue, of the class property is static
typestringtype name of the property or lookup into __helpers list of symbols
extensionstringused plugin name ie 'react'...
descriptionstringjsdoc description
firesstring[]jsdoc fires events list
seestring[]jsdoc see links list
examplesJSDocExample[]jsdoc examples list
tagsJSDocPropTag[]jsdoc generic tags, not covered by other props
summarystringjsdoc summary
deprecatedstring | truejsdoc deprecated tag
ignorebooleanjsdoc ignore tag, to be excluded from documentations
usagetype[]if collectParametersUsage option is set, this will collect parameters usage in function body

PropKind

enum

The property type or kind

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

properties

NameTypeValue
String*number1
Number*number2
Boolean*number3
Union*number4
Enum*number5
Tuple*number6
Rest*number7
Undefined*number8
Unknown*number9
Null*number10
Function*number11
Void*number12
Class*number13
Interface*number14
Type*number15
Array*number16
Any*number17
Index*number20
Constructor*number21
Getter*number22
Setter*number23
BigInt*number24
Component*number25
Object*number26
Namespace*number27
RegEx*number28

ParsePlugin

type

Plugin type - provides the plugin name and the type resolver

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

properties

NameTypeParentDefaultDescription
tsOptionsts.CompilerOptionsDocsOptions
internalTypesRecord<string, PropKind>ParseOptionsinternal types - libs by default includes classes such as String , Function ...
extractstring[]ParseOptionslist of export names to be extracted. by default all exports are extracted
filterfunction (prop*: PropType) => booleanParseOptionsfilter properties function. By default filter out all props with ignore === true
maxDepthnumberParseOptions6max depth for extracting child props.
collectHelpersbooleanParseOptionswhether to save "helper" props that are used by the main parsed props if set to false will result in a smaller result set
collectGenericsbooleanParseOptionstruewhether to collect generics parameters
collectParametersbooleanParseOptionstruewhether to collect function parameters
collectParametersUsagebooleanParseOptionswhether to collect function parameters usage locations within the function body
collectPropertiesbooleanParseOptionstruewhether to collect object/type properties
collectInheritancebooleanParseOptionstruewhether to collect the inheritance properties
collectExtensionbooleanParseOptionstruewhether to collect the plugin/extension name
collectDiagnosticsbooleanParseOptionswhether to collect errors/diagnostics
collectAliasNamebooleanParseOptionstruewhether to collect alias names - for example: when imported default symbol from another file, this will be the import name
collectInternalsbooleanParseOptionswhether to collect internal (typescript) symbols
pluginsParsePlugin[]ParseOptionsinstalled plugins can modify default options and install type resolvers
scope"exports" | "all"ParseOptionsby default collects only the exported symbols
collectSourceInfoboolean | "body"ParseOptionswhether 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.
collectInnerLocationsbooleanParseOptionswhether to collect the source code location for inner symbol declarations if set to true, the data will be collected in the loc prop
moduleCallbackfunction (module*: Symbolchecker*: TypeChecker) => voidParseOptionscallback with the parsed module. Can be used to retrieve additional information such as the imports of a file etc.
typesResolve*function (props*symbolType*: Typedeclaration: ts.Declarationparser*: ISymbolParserexpression: ts.Expression) => ResolverReturnType | undefinedtype resolving custom function ie from a react component will return the props type if the plugin does not recognize the type, should return undefined
pluginNamestringplugin name

PropDiagnostic

type

diagnostics row data

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

properties

NameTypeDescription
category*error category
message*stringerror text message
rownumbersource code line of the error
columnnumbersource code column of the error
fileNamestringsource file name

PropParent

interface

Parent of a property field

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

properties

NameTypeDescription
name*stringthe parent type name
locSourceLocationoptional source location. will be available when collectSourceInfo option is set to true

SourceLocation

interface

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

properties

NameTypeDescription
filePathstringname of the file where the symbol is defined only if different from the default file path
locSourcePositionsstart*: SourcePositionend*: SourcePositionsource code location for the symbol declaration

JSDocExample

interface

JSDoc example item

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

properties

NameTypeDescription
captionstringexample caption/title
contentstringexample source/content

JSDocPropTag

interface

JSDoc generic tag item

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

properties

NameTypeDescription
tag*stringtag name
contentstringoptional tag content

SourcePosition

interface

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

properties

NameTypeDescription
line*numbersource line of the symbol
col*numbersource column of the symbol

ResolverReturnType

type

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

properties

NameTypeParentDefaultDescription
type*ts.Type | undefined
initializerts.Node
declarationts.Node
propPropType
pluginNamestring
isInternalfunction (file*: SourceFilenode*: Node) => boolean | undefinedParseOptionscallback function to determine if a node is an internal (typescript) symbol return undefined if you need to use the default isInternal processing
tsOptionsts.CompilerOptionsDocsOptions
internalTypesRecord<string, PropKind>ParseOptionsinternal types - libs by default includes classes such as String , Function ...
extractstring[]ParseOptionslist of export names to be extracted. by default all exports are extracted
filterfunction (prop*: PropType) => booleanParseOptionsfilter properties function. By default filter out all props with ignore === true
maxDepthnumberParseOptions6max depth for extracting child props.
collectHelpersbooleanParseOptionswhether to save "helper" props that are used by the main parsed props if set to false will result in a smaller result set
collectGenericsbooleanParseOptionstruewhether to collect generics parameters
collectParametersbooleanParseOptionstruewhether to collect function parameters
collectParametersUsagebooleanParseOptionswhether to collect function parameters usage locations within the function body
collectPropertiesbooleanParseOptionstruewhether to collect object/type properties
collectInheritancebooleanParseOptionstruewhether to collect the inheritance properties
collectExtensionbooleanParseOptionstruewhether to collect the plugin/extension name
collectDiagnosticsbooleanParseOptionswhether to collect errors/diagnostics
collectAliasNamebooleanParseOptionstruewhether to collect alias names - for example: when imported default symbol from another file, this will be the import name
collectInternalsbooleanParseOptionswhether to collect internal (typescript) symbols
pluginsParsePlugin[]ParseOptionsinstalled plugins can modify default options and install type resolvers
scope"exports" | "all"ParseOptionsby default collects only the exported symbols
collectSourceInfoboolean | "body"ParseOptionswhether 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.
collectInnerLocationsbooleanParseOptionswhether to collect the source code location for inner symbol declarations if set to true, the data will be collected in the loc prop
moduleCallbackfunction (module*: Symbolchecker*: TypeChecker) => voidParseOptionscallback with the parsed module. Can be used to retrieve additional information such as the imports of a file etc.

ISymbolParser

interface

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

properties

NameType
checker*TypeChecker
options*ParseOptions
parseProperties*function (properties*[number]: Toptions*: ParseOptionstypes: PropType[]) => PropType[]
updateSymbolName*function (prop*: PropTypenode: ts.Declaration) => PropType
parseType*function (prop*: PropTypeoptions*: ParseOptionsnode: ts.Node) => PropType
parseTypeValueComments*function (prop*: PropTypeoptions*: ParseOptionsdeclaration: ts.Nodeinitializer: ts.Node) => PropType | null
parseSymbol*function (symbol*: Symboloptions*: ParseOptions) => PropType | undefined
3.46.12

2 years ago

3.46.11

2 years ago

3.46.9

2 years ago

3.46.8

2 years ago

3.46.7

2 years ago

3.41.0

2 years ago

3.43.0

2 years ago

3.43.1

2 years ago

3.45.0

2 years ago

3.43.2

2 years ago

3.43.3

2 years ago

3.43.4

2 years ago

3.40.0

2 years ago

3.40.1

2 years ago

3.42.0

2 years ago

3.40.2

2 years ago

3.42.1

2 years ago

3.44.0

2 years ago

3.40.4

2 years ago

3.44.1

2 years ago

3.40.5

2 years ago

3.46.0

2 years ago

3.44.2

2 years ago

3.46.1

2 years ago

3.44.3

2 years ago

3.40.7

2 years ago

3.46.2

2 years ago

3.40.10

2 years ago

3.46.3

2 years ago

3.40.9

2 years ago

3.46.4

2 years ago

3.46.5

2 years ago

3.46.6

2 years ago

3.39.10

2 years ago

3.36.0

2 years ago

3.36.2

2 years ago

3.38.1

2 years ago

3.39.5

2 years ago

3.39.6

2 years ago

3.39.7

2 years ago

3.39.8

2 years ago

3.37.0

2 years ago

3.35.2

3 years ago

3.37.1

2 years ago

3.35.3

3 years ago

3.39.3

2 years ago

3.39.4

2 years ago

3.35.1

3 years ago

3.35.0

3 years ago

3.34.2

3 years ago

3.33.0

3 years ago

3.34.0

3 years ago

3.34.1

3 years ago

3.32.0

3 years ago

3.31.0

3 years ago

3.30.0

3 years ago

3.26.0

3 years ago

3.25.0

3 years ago

3.28.0

3 years ago

3.27.0

3 years ago

3.24.6

3 years ago

3.29.0

3 years ago

3.28.1

3 years ago

3.24.4

3 years ago

3.24.3

3 years ago

3.24.2

3 years ago

3.24.1

3 years ago

3.24.0

3 years ago

3.23.0

3 years ago

3.22.0

3 years ago

3.21.1

3 years ago

3.21.0

3 years ago

3.20.4

3 years ago

3.20.2

3 years ago

3.20.1

3 years ago

3.20.3

3 years ago

3.20.0

3 years ago

3.19.0

3 years ago

3.18.1

3 years ago

3.18.0

3 years ago

3.17.2

3 years ago

3.17.1

3 years ago

3.17.0

3 years ago

3.16.5

3 years ago

3.16.4

3 years ago

3.16.2

3 years ago

3.16.1

3 years ago

3.16.0

3 years ago

0.5.3

3 years ago

0.5.2

3 years ago