-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New package to auto-generate public API documentation #13329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
213 commits
Select commit
Hold shift + click to select a range
0a6b23c
First, find export declarations
oandregal 81d4b78
Second, parse JSDoc
oandregal c5ad307
Third, create markdown files with exports and JDoc descriptions.
oandregal 16c178c
Refactor and prevent against no JSDoc
oandregal 9ee1aea
Move script to own package
oandregal 4e6b22b
Split api into cli, engine, and formatter
oandregal 7225849
Add initial tests
oandregal de314f5
Test named export (const)
oandregal f27fb08
Add test for default named functions
oandregal 07e21fa
Add support for default export of anonymous functions
oandregal 7b3dafc
Better phrasing for test
oandregal 441ba4b
Add support for default exports (identifiers)
oandregal d305f7c
Add support for named exports of a single identifier
oandregal e1b83c0
Add test for many exports at once
oandregal 04b6c8e
Tweak text and reorder tests
oandregal acef18c
Teach engine to take JSDoc from identifier declaration
oandregal e042c8c
Cover undocumented exports
oandregal 4e74cb1
Be more explicit about what is a test file
oandregal fd60891
Add more test to cover potential regressions
oandregal 5fa808c
Expose script as npm script
oandregal f2b7c98
CLI: manage errors
oandregal 97ae482
Refactor to pave the way to next step
oandregal 5f45ad7
Refactor to prepare next step
oandregal c92e15b
Refactor to prepare next step
oandregal ef0d41e
Add helper functions
oandregal 48281e5
Add mechanism to query upper layers for dependency code
oandregal 84f5e1c
Engine can lookup JSDoc in dependency file
oandregal 6e006ea
Formatter - adapt a new engine API
oandregal 560abda
Add test for looking up JSDoc in dependency
oandregal 444891a
CLI: adapt to new engine API
oandregal fe14068
Engine: enable JSX support
oandregal 0ab135f
Evaluate how much coverage do we have already
oandregal a2a5a6a
Remove unused file
oandregal 2d7268a
Add test for multiple named exports
oandregal f017dfe
Add more info for debugging purposes
oandregal 84f4dbc
Add tests for get name of named function and variable
oandregal 844d74a
Add support and test for parsing classes
oandregal bb34043
Add test for single identifier
oandregal 8cc1be8
Add test for multiple named identifiers
oandregal 0f80013
Add tests for default export of functions (anonymous and named)
oandregal 0fb9542
Add tests for default export of variables
oandregal c3a74d7
Add tests for default export of classes
oandregal 633e0f9
Add tests for default export of identifier
oandregal 009cf5d
Add test for export redirects (* and default)
oandregal d939c4a
Placeholder to implement ExportAllDeclaration
oandregal aca12d4
Update engine and tests
oandregal 6986119
Adapt get-name-declaration to return an array
oandregal 4b063a0
Implement retrieval of multiple specifiers
oandregal 255af56
Reorganize test to prepare next steps
oandregal ec547fc
Refactor default and namespace exports
oandregal 9f5eafb
Flatten importName and localName
oandregal dc95707
get-name-declaration: refactor to return export entry list
oandregal 10b271f
get-name-declaration renamed to get-export-entries
oandregal 461ec44
Reorder dependency statements
oandregal e714f9a
Remove uncessary test
oandregal d59f26c
Refactor get IR and add tests for default exports
oandregal 421cdf0
Rename file
oandregal f9b3a86
Flatten export entries returned by IR
oandregal 927ec0b
Fix engine tests
oandregal 0b1e42c
Update tests for IR
oandregal d41beb8
Fix tests
oandregal 45eb32e
getExportEntries: retrieve module from export statement
oandregal 6f8849e
getIR: fix dependency lookup
oandregal 2a72444
Protect against no code provided
oandregal 27756e8
Better test names
oandregal e79bb85
Implement exported names
oandregal 7f73a4f
Fix exported names
oandregal 71d170e
Update coverage
oandregal f363338
Add test for namespace export
oandregal 9fbac97
Update coverage.md
oandregal b7b2b30
Fix export entries test
oandregal f116fbf
IR: add support for namespace exports
oandregal 93b9044
Fix method call
oandregal 73f4b83
Extract constants for better encapsulation
oandregal 2d5ab5c
Update coverage
oandregal 721cfdc
Add test for named and default exports
oandregal 12621e9
Add JSDoc lookup for named inline nodes such as:
oandregal 626ad86
Update coverage
oandregal ef09517
Better test organization
oandregal d98b214
Implement JSDoc lookup in default identifier from import
oandregal 2b4019d
Better test organization
oandregal a21d8c0
getExportEntries: test for _import default from module_
oandregal a8b7d0d
Add support for JSDoc lookup of imported identifiers
oandregal 970fcd7
Update coverage
oandregal 3465afd
Install docgen package
oandregal 33ee82b
Improve CLI
oandregal e304911
Add tests for retrieving JSDoc of destructured objects
oandregal c72c154
Add support for JSDoc lookup of destructured objects
oandregal bfc9a3b
Adapt tests to new engine API
oandregal c3bb4e9
Fix JSDoc lookup for named export
oandregal 3826642
Update coverage
oandregal a592ad7
Remove docgen from package.json
oandregal 12acf30
Better README
oandregal 34fc211
Update coverage
oandregal 26c51ca
Ignore comments in namespace exports
oandregal 9d4cc08
Update coverage
oandregal c632bec
CLI generates output by adding suffixes to input
oandregal 002f204
Add test for export imported namespaces
oandregal 15a753f
Add test for export imported namespaces
oandregal 5768a6f
Add named-import-namespace AST
oandregal 10d0a0b
Extract method
oandregal e2ef4b0
IR - add breaking test
oandregal 8ff0e45
Fix bug on export redirection
oandregal 2ac38a6
Add support for import namespace
oandregal ba1e68f
Update coverage
oandregal 90258c9
Respect the dependency extension if any
oandregal 9615010
Update coverage
oandregal a49bfcd
Add test for undocumented declaration (no comments)
oandregal f68a3d2
Add test for undocumented declaration (// comment)
oandregal 2cb4014
Update README
oandregal dbac1dc
Add a debug mode
oandregal 6a83ff8
Update coverage
oandregal fce4ad8
Better example
oandregal 86c4350
Better README.md
oandregal a81b7a6
Add params tags to IR and formatter
oandregal 49ede6f
Formatter: show return tag output
oandregal 52bd077
Adapt engine tests to new API
oandregal 31bdfa7
Adapt formatter tests to new API
oandregal 27ac534
Adapt IR tests to new API
oandregal 23ee549
Update fixtures and test data to uncover some bug
oandregal 174dbb0
Reorder tests
oandregal 18257d0
Fix bug on retrieving JSDoc from module dep
oandregal 3bf4732
Markdown: clean spaces in descriptions
oandregal 7816de2
Add support for nullable types
oandregal a0bdec9
Extract getParamType and add tests
oandregal c2f814b
getParamType: add support for RestType
oandregal 72b1adc
getParamType: add support for unions
oandregal 2b3fe94
Markdown tweaks: better formatting, adding output type
oandregal 3de973d
Markdown: sort output by symbol name
oandregal ad3e48b
Markdown: improve spacing
oandregal 39fd98f
Use | symbols for type unions
oandregal 88ed5bf
Use ... symbols for rest tipes
oandregal c2dc8e2
Markdown: update to consider uniont and rest types
oandregal 09763d7
getParamType: add support for nullable params
oandregal 28f06f4
Markdown: add example tag to output
oandregal 78db25b
Update tests to API changes
oandregal a89f9e5
Prevent bug when paragraph is null
oandregal 796ad3f
Update coverage
oandregal 3e3810f
Update coverage
oandregal 5f2cf4a
Add support for see tag
oandregal ca2fde9
Add support for link tag
oandregal 2a2743a
Allow having several examples for one symbol
oandregal 5f60034
Less space between types: (object|string)
oandregal dbae6fe
Keep description format as it is
oandregal b76e001
Add support for type tag
oandregal 9d3dcc2
Rename get-param-type-as-string to get-type-as-string
oandregal c149320
Adapt test to API changes
oandregal 88004d9
Add support for * type
oandregal b8e21b0
Add support for type applications
oandregal d8c8a18
Add support for NullLiteral
oandregal 3b3cd21
Add support for UndefinedLiteral and optional parameters
oandregal f485e71
Add support for default values
oandregal bf2a2d4
Add support for deprecated tag
oandregal 6753293
params and return tags are no longer special
oandregal 9bcce9e
Refactor formatter to compact code
oandregal 22f78f0
Update coverage
oandregal b16d957
Add line numbers (start, end) to export tokens
oandregal acc8ccb
Adapt tests to new API
oandregal b913608
Add line number to intermediate representation
oandregal de6d74b
Fix line numbers for namespace export
oandregal c31d43c
Remove duplicated tests
oandregal a14178f
Better test names
oandregal ab9dd27
Add path to intermediate representation
oandregal a935f66
Adapt tests to API changes
oandregal 6070c62
Add path and line numbers to markdown output
oandregal 8016fb7
Add TODO file
oandregal e842a63
Use optionator to process CLI options
oandregal c0ba93b
Allow to specify a markdown output file
oandregal 915091c
Allow to pass a formatter
oandregal f7577df
Document new added options
oandregal 9ab9cfb
CLI: add ability to ignore symbols by name
oandregal 63c6702
Update README
oandregal d90aaf5
CLI: support append to existing Markdown file
oandregal 4abfe52
Make linter happy
oandregal 3dfb4cc
Add package to manifest
oandregal 1b6578b
Update as per new package recommendations
oandregal 09e4775
Update package name
oandregal fe3c531
Add docgen as a dev dependency
oandregal 03e779c
Add wordpress keyword to package.json
oandregal 3dc7410
Mark as a bash command
oandregal bf08a54
CLI: allow for walking parent directories
oandregal 26411bb
Add tests for tags
oandregal d4c1f80
Adapt formatter test to API
oandregal 8a0695c
Move IR tests related to JSDoc to its own tester
oandregal d994a27
get-jsdoc-from-token returns a human-readable type
oandregal 2849440
Implement a token-based injection for Markdown
oandregal 3d5b911
Add embed JSDoc
oandregal 287a1fb
Formatter: let heading index be configurable
oandregal 5e22438
Use proper heading when embedding content within token
oandregal 25b0898
Move default markdown code to own folder
oandregal 59d5f19
Migrate engine tests to jest
oandregal a58775b
Reorganize fixtures
oandregal 62878c6
Migrate export tests to jest
oandregal ba86e53
Migrate getTypeFromJSDoc tests to jest
oandregal a4af39d
Migrate getJSDocFromToken tests to jest
oandregal 99a41f3
Migrate getIntermediateRepresentation tests to jest
oandregal 7e68a46
Migrate Markdown formatter tests to jest
oandregal 71a64d6
Extract bin/cli utility
oandregal 95239cb
Uninstall no longer needed dev deps
oandregal 94411fa
Fix docgen dependencies
oandregal 1f617be
Add command to generate docs
oandregal 996e65a
Update docs
oandregal ae3cf05
Update README
oandregal 3e842bc
Add --use-token option
oandregal 8429aa2
Extract constants
oandregal 5c30f1c
Update README
oandregal fb03ec9
Make formatter-markdown test be deterministic
oandregal 77e3d8e
Make formatter-markdown test be deterministic
oandregal f255304
Update packages/docgen/README.md
chrisvanpatten 6616bc0
Update packages/docgen/README.md
chrisvanpatten c8fcab7
Update packages/docgen/README.md
chrisvanpatten 47b78e3
Add command in the examples
oandregal 2610e87
Update package-lock using the latest npm
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package-lock=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 1.0.0 (Unreleased) | ||
|
|
||
| - Initial release |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| # `docgen` | ||
|
|
||
| `docgen` helps you to generate the _public API_ of your code. Given an entry point file, it outputs the ES6 export statements and their corresponding JSDoc comments in human-readable format. | ||
|
|
||
| Some characteristics: | ||
|
|
||
| * If the export statement doesn't contain any JSDoc, it'll look up for JSDoc up to the declaration. | ||
| * It can resolve relative dependencies, either files or directories. For example, `import default from './dependency'` will find `dependency.js` or `dependency/index.js` | ||
|
|
||
| ## Installation | ||
|
|
||
| Install the module | ||
|
|
||
| ```bash | ||
| npm install @wordpress/docgen --save-dev | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| npx docgen <entry-point.js> | ||
| ``` | ||
|
|
||
| This command will generate a file named `entry-point-api.md` containing all the exports and their JSDoc comments. | ||
|
|
||
| ### CLI options | ||
|
|
||
| * **--formatter** `(String)`: A path to a custom formatter to control the contents of the output file. It should be a CommonJS module that exports a function that takes as input: | ||
| * *rootDir* `(String)`: current working directory as seen by docgen. | ||
| * *docPath* `(String)`: path of the output document to generate. | ||
| * *symbols* `(Array)`: the symbols found. | ||
| * **--ignore** `(RegExp)`: A regular expression used to ignore symbols whose name match it. | ||
| * **--output** `(String)`: Output file that will contain the API documentation. | ||
| * **--to-section** `(String)`: Append generated documentation to this section in the Markdown output. To be used by the default Markdown formatter. Depends on `--output` and bypasses the custom `--formatter` passed, if any. | ||
| * **--to-token**: Embed generated documentation within the start and end tokens in the Markdown output. To be used by the default Markdown formatter.Depends on `--output` and bypasses the custom `--formatter` passed, if any. | ||
| * Start token: `<!-- START TOKEN(Autogenerated API docs) -->` | ||
| * End token: `<!-- END TOKEN(Autogenerated API docs) -->` | ||
| * **--use-token** `(String)`: This options allows you to customize the string between the tokens. For example, `--use-token my-api` will look up for the start token `<!-- START TOKEN(my-api) -->` and the end token `<!-- END TOKEN(my-api) -->`. Depends on `--to-token`. | ||
| * **--debug**: Run in debug mode, which outputs some intermediate files useful for debugging. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Default export | ||
|
|
||
| Entry point `index.js`: | ||
|
|
||
| ```js | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Adds two numbers. | ||
| * | ||
| * @param {number} term1 First number. | ||
| * @param {number} term2 Second number. | ||
| * @return {number} The result of adding the two numbers. | ||
| */ | ||
| export default function addition( term1, term2 ) { | ||
| // Implementation would go here. | ||
| } | ||
| ``` | ||
|
|
||
| Output of `npx docgen index.js` would be `index-api.js`: | ||
|
|
||
| ```markdown | ||
| # API | ||
|
|
||
| ## default | ||
|
|
||
| [example.js#L8-L10](example.js#L8-L10) | ||
|
|
||
| Adds two numbers. | ||
|
|
||
| **Parameters** | ||
|
|
||
| - **term1** `number`: First number. | ||
| - **term2** `number`: Second number. | ||
|
|
||
| **Returns** | ||
|
|
||
| `number` The result of adding the two numbers. | ||
| ``` | ||
|
|
||
| ### Named export | ||
|
|
||
| Entry point `index.js`: | ||
|
|
||
| ```js | ||
| /** | ||
| * Adds two numbers. | ||
| * | ||
| * @param {number} term1 First number. | ||
| * @param {number} term2 Second number. | ||
| * @return {number} The result of adding the two numbers. | ||
| */ | ||
| function addition( term1, term2 ) { | ||
| return term1 + term2; | ||
| } | ||
|
|
||
| /** | ||
| * Adds two numbers. | ||
| * | ||
| * @deprecated Use `addition` instead. | ||
| * | ||
| * @param {number} term1 First number. | ||
| * @param {number} term2 Second number. | ||
| * @return {number} The result of adding the two numbers. | ||
| */ | ||
| function count( term1, term2 ) { | ||
| return term1 + term2; | ||
| } | ||
|
|
||
| export { count, addition }; | ||
| ``` | ||
|
|
||
| Output of `npx docgen index.js` would be `index-api.js`: | ||
|
|
||
| ```markdown | ||
| # API | ||
|
|
||
| ## addition | ||
|
|
||
| [example.js#L25-L25](example.js#L25-L25) | ||
|
|
||
| Adds two numbers. | ||
|
|
||
| **Parameters** | ||
|
|
||
| - **term1** `number`: First number. | ||
| - **term2** `number`: Second number. | ||
|
|
||
| **Returns** | ||
|
|
||
| `number` The result of adding the two numbers. | ||
|
|
||
| ## count | ||
|
|
||
| [example.js#L25-L25](example.js#L25-L25) | ||
|
|
||
| > **Deprecated** Use `addition` instead. | ||
|
|
||
| Adds two numbers. | ||
|
|
||
| **Parameters** | ||
|
|
||
| - **term1** `number`: First number. | ||
| - **term2** `number`: Second number. | ||
|
|
||
| **Returns** | ||
|
|
||
| `number` The result of adding the two numbers. | ||
| ``` | ||
|
|
||
| ### Namespace export | ||
|
|
||
| Let the entry point be `index.js`: | ||
|
|
||
| ```js | ||
| export * from './count'; | ||
| ``` | ||
|
|
||
| with `./count/index.js` contents being: | ||
|
|
||
| ```js | ||
| /** | ||
| * Substracts two numbers. | ||
| * | ||
| * @example | ||
| * | ||
| * ```js | ||
| * const result = substraction( 5, 2 ); | ||
| * console.log( result ); // Will log 3 | ||
| * ``` | ||
| * | ||
| * @param {number} term1 First number. | ||
| * @param {number} term2 Second number. | ||
| * @return {number} The result of subtracting the two numbers. | ||
| */ | ||
| export function substraction( term1, term2 ) { | ||
| return term1 - term2; | ||
| } | ||
|
|
||
| /** | ||
| * Adds two numbers. | ||
| * | ||
| * @example | ||
| * | ||
| * ```js | ||
| * const result = addition( 5, 2 ); | ||
| * console.log( result ); // Will log 7 | ||
| * ``` | ||
| * | ||
| * @param {number} term1 First number. | ||
| * @param {number} term2 Second number. | ||
| * @return {number} The result of adding the two numbers. | ||
| */ | ||
| export function addition( term1, term2 ) { | ||
| // Implementation would go here. | ||
| return term1 - term2; | ||
| } | ||
| ``` | ||
|
|
||
| Output of `npx docgen index.js` would be `index-api.js`: | ||
|
|
||
| ````markdown | ||
| # API | ||
|
|
||
| ## addition | ||
|
|
||
| [example-module.js#L1-L1](example-module.js#L1-L1) | ||
|
|
||
| Adds two numbers. | ||
|
|
||
| **Usage** | ||
|
|
||
| ```js | ||
| const result = addition( 5, 2 ); | ||
| console.log( result ); // Will log 7 | ||
| ``` | ||
|
|
||
| **Parameters** | ||
|
|
||
| - **term1** `number`: First number. | ||
| - **term2** `number`: Second number. | ||
|
|
||
| **Returns** | ||
|
|
||
| `number` The result of adding the two numbers. | ||
|
|
||
| ## substraction | ||
|
|
||
| [example-module.js#L1-L1](example-module.js#L1-L1) | ||
|
|
||
| Substracts two numbers. | ||
|
|
||
| **Usage** | ||
|
|
||
| ```js | ||
| const result = substraction( 5, 2 ); | ||
| console.log( result ); // Will log 3 | ||
| ``` | ||
|
|
||
| **Parameters** | ||
|
|
||
| - **term1** `number`: First number. | ||
| - **term2** `number`: Second number. | ||
|
|
||
| **Returns** | ||
|
|
||
| `number` The result of subtracting the two numbers. | ||
| ```` | ||
|
|
||
| <br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const docgen = require( '../src' ); | ||
|
|
||
| const optionator = require( 'optionator' )( { | ||
| prepend: 'Usage: node <path-to-docgen> <relative-path-to-entry-point>', | ||
| options: [ { | ||
| option: 'formatter', | ||
| type: 'String', | ||
| description: 'A custom function to format the generated documentation. By default, a Markdown formatter will be used.', | ||
| }, { | ||
| option: 'output', | ||
| type: 'String', | ||
| description: 'Output file to contain the API documentation.', | ||
| }, { | ||
| option: 'ignore', | ||
| type: 'RegExp', | ||
| description: 'A regular expression used to ignore symbols whose name match it.', | ||
| }, { | ||
| option: 'to-section', | ||
| type: 'String', | ||
| description: 'Append generated documentation to this section in the Markdown output. To be used by the default Markdown formatter.', | ||
| dependsOn: 'output', | ||
| }, { | ||
| option: 'to-token', | ||
| type: 'Boolean', | ||
| description: 'Embed generated documentation within this token in the Markdown output. To be used by the default Markdown formatter.', | ||
| dependsOn: 'output', | ||
| }, { | ||
| option: 'use-token', | ||
| type: 'String', | ||
| default: 'Autogenerated API docs', | ||
| description: 'Add this string to the start/end tokens.', | ||
| dependsOn: 'to-token', | ||
| }, { | ||
| option: 'debug', | ||
| type: 'Boolean', | ||
| default: false, | ||
| description: 'Run in debug mode, which outputs some intermediate files useful for debugging.', | ||
| } ], | ||
| } ); | ||
|
|
||
| const options = optionator.parseArgv( process.argv ); | ||
| docgen( options._[ 0 ], options ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.