-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Replace doctrine with comment-parser. #22061
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
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
5131f49
Replace doctrine.
sainthkh 05506b7
Fix test
sainthkh f61c42c
Preserve tabs in code.
sainthkh 8ce7355
Generate type strings with jsontypeparser.
sainthkh f68f0fe
Preserve spaces.
sainthkh a499824
Restore parentheses style.
sainthkh 02be44a
Handle optional types.
sainthkh c81c67b
Remove space between types.
sainthkh 9096a24
Make tests readable.
sainthkh ce9d695
example tag edge cases.
sainthkh b6cb3ee
Remove unnecessary parentheses.
sainthkh 5f1e63c
Handle <caption> tag.
sainthkh d523b68
Handle a tab right after *.
sainthkh e493ceb
Handle code inside jsdoc.description.
sainthkh 3ee312d
Handle @type description.
sainthkh 76b719d
Handle @see when explanation goes on after the link.
sainthkh 484055d
Handle @typedef, @property.
sainthkh c00c9e4
Update docs.
sainthkh 2651e4b
Fix comments.
sainthkh bc233b1
Handle parse failure.
sainthkh fa3146c
Update package-lock.json
sainthkh 8525c60
Show only the leaf type name from imported types.
sainthkh 907f9be
Update docs.
sainthkh 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
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 |
|---|---|---|
| @@ -1,47 +1,63 @@ | ||
| const maybeAddDefault = function( value, defaultValue ) { | ||
| if ( defaultValue ) { | ||
| return `value=${ defaultValue }`; | ||
| const { parse } = require( 'jsdoctypeparser' ); | ||
|
|
||
| const getType = ( ast, typeString, noUnionParenthesis ) => { | ||
| if ( ast.type === 'NAME' ) { | ||
| return ast.name; | ||
| } | ||
|
|
||
| if ( ast.type === 'ANY' ) { | ||
| return '*'; | ||
| } | ||
|
|
||
| if ( ast.type === 'GENERIC' ) { | ||
| const types = ast.objects.map( ( o ) => getType( o ) ).join( ',' ); | ||
| return `${ getType( ast.subject ) }<${ types }>`; | ||
| } | ||
| return value; | ||
| }; | ||
|
|
||
| const getType = function( param, defaultValue ) { | ||
| if ( ! defaultValue ) { | ||
| defaultValue = param.default; | ||
| } | ||
|
|
||
| if ( param.type.type ) { | ||
| return getType( param.type, defaultValue ); | ||
| } else if ( param.expression ) { | ||
| if ( param.type === 'RestType' ) { | ||
| return `...${ getType( param.expression, defaultValue ) }`; | ||
| } else if ( param.type === 'NullableType' ) { | ||
| return `?${ getType( param.expression, defaultValue ) }`; | ||
| } else if ( param.type === 'TypeApplication' ) { | ||
| return `${ getType( | ||
| param.expression, | ||
| defaultValue | ||
| ) }<${ param.applications | ||
| .map( ( application ) => getType( application ) ) | ||
| .join( ',' ) }>`; | ||
| } else if ( param.type === 'OptionalType' ) { | ||
| return `[${ getType( param.expression, defaultValue ) }]`; | ||
| } | ||
| return getType( param.expression, defaultValue ); | ||
| } else if ( param.elements ) { | ||
| const types = param.elements.map( ( element ) => getType( element ) ); | ||
| return maybeAddDefault( `(${ types.join( '|' ) })`, defaultValue ); | ||
| } else if ( param.type === 'AllLiteral' ) { | ||
| return maybeAddDefault( '*', defaultValue ); | ||
| } else if ( param.type === 'NullLiteral' ) { | ||
| return maybeAddDefault( 'null', defaultValue ); | ||
| } else if ( param.type === 'UndefinedLiteral' ) { | ||
| return maybeAddDefault( 'undefined', defaultValue ); | ||
| } | ||
|
|
||
| return maybeAddDefault( param.name, defaultValue ); | ||
| if ( ast.type === 'NULLABLE' ) { | ||
| return `?${ getType( ast.value ) }`; | ||
| } | ||
|
|
||
| if ( ast.type === 'VARIADIC' ) { | ||
| return `...${ getType( ast.value ) }`; | ||
| } | ||
|
|
||
| if ( ast.type === 'UNION' ) { | ||
| const left = getType( ast.left ); | ||
| const right = getType( ast.right, null, true ); | ||
| const type = `${ left }|${ right }`; | ||
|
|
||
| return noUnionParenthesis ? type : `(${ type })`; | ||
| } | ||
|
|
||
| if ( ast.type === 'PARENTHESIS' ) { | ||
| const type = getType( ast.value ); | ||
| return type[ 0 ] === '(' && type[ type.length - 1 ] === ')' | ||
| ? type | ||
| : `(${ type })`; | ||
| } | ||
|
|
||
| if ( ast.type === 'OPTIONAL' ) { | ||
| return `[${ getType( ast.value ) }]`; | ||
| } | ||
|
|
||
| if ( ast.type === 'MEMBER' && ast.owner.type === 'IMPORT' ) { | ||
| return `${ ast.name }`; | ||
| } | ||
|
|
||
| return typeString || 'unknown type'; | ||
| }; | ||
|
|
||
| module.exports = function( param ) { | ||
| return getType( param ); | ||
| module.exports = function( typeString, optional ) { | ||
| let ast; | ||
|
|
||
| try { | ||
| ast = parse( typeString ); | ||
| } catch { | ||
| return 'unknown type'; | ||
| } | ||
|
|
||
| const type = getType( ast, typeString ); | ||
|
|
||
| return optional ? `[${ type }]` : type; | ||
| }; |
2 changes: 1 addition & 1 deletion
2
packages/docgen/src/test/fixtures/default-parse-error/exports.json
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| [{"type":"ExportDefaultDeclaration","start":173,"end":288,"loc":{"start":{"line":7,"column":0},"end":{"line":9,"column":1}},"range":[173,288],"declaration":{"type":"FunctionDeclaration","start":188,"end":288,"loc":{"start":{"line":7,"column":15},"end":{"line":9,"column":1}},"range":[188,288],"id":{"type":"Identifier","start":197,"end":221,"loc":{"start":{"line":7,"column":24},"end":{"line":7,"column":48}},"range":[197,221],"name":"invokeCallbackAfterDelay"},"generator":false,"expression":false,"async":false,"params":[{"type":"Identifier","start":223,"end":231,"loc":{"start":{"line":7,"column":50},"end":{"line":7,"column":58}},"range":[223,231],"name":"callback"}],"body":{"type":"BlockStatement","start":234,"end":288,"loc":{"start":{"line":7,"column":61},"end":{"line":9,"column":1}},"range":[234,288],"body":[{"type":"ExpressionStatement","start":237,"end":286,"loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":50}},"range":[237,286],"expression":{"type":"CallExpression","start":237,"end":285,"loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":49}},"range":[237,285],"callee":{"type":"Identifier","start":237,"end":247,"loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":11}},"range":[237,247],"name":"setTimeout"},"arguments":[{"type":"ArrowFunctionExpression","start":249,"end":277,"loc":{"start":{"line":8,"column":13},"end":{"line":8,"column":41}},"range":[249,277],"id":null,"generator":false,"expression":true,"async":false,"params":[],"body":{"type":"CallExpression","start":255,"end":277,"loc":{"start":{"line":8,"column":19},"end":{"line":8,"column":41}},"range":[255,277],"callee":{"type":"Identifier","start":255,"end":263,"loc":{"start":{"line":8,"column":19},"end":{"line":8,"column":27}},"range":[255,263],"name":"callback"},"arguments":[{"type":"CallExpression","start":265,"end":275,"loc":{"start":{"line":8,"column":29},"end":{"line":8,"column":39}},"range":[265,275],"callee":{"type":"MemberExpression","start":265,"end":273,"loc":{"start":{"line":8,"column":29},"end":{"line":8,"column":37}},"range":[265,273],"object":{"type":"Identifier","start":265,"end":269,"loc":{"start":{"line":8,"column":29},"end":{"line":8,"column":33}},"range":[265,269],"name":"Date"},"property":{"type":"Identifier","start":270,"end":273,"loc":{"start":{"line":8,"column":34},"end":{"line":8,"column":37}},"range":[270,273],"name":"now"},"computed":false},"arguments":[]}]}},{"type":"Literal","start":279,"end":283,"loc":{"start":{"line":8,"column":43},"end":{"line":8,"column":47}},"range":[279,283],"value":1000,"raw":"1000"}]}}]}},"leadingComments":[{"type":"Block","value":"*\n * Function invoking callback after delay with current timestamp in milliseconds\n * since epoch.\n *\n * @param {(timestamp:number)=>void} callback Callback function.\n ","start":0,"end":172,"range":[0,172],"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":3}}}]}] | ||
| [{"type":"ExportDefaultDeclaration","start":173,"end":288,"loc":{"start":{"line":7,"column":0},"end":{"line":9,"column":1}},"range":[173,288],"declaration":{"type":"FunctionDeclaration","start":188,"end":288,"loc":{"start":{"line":7,"column":15},"end":{"line":9,"column":1}},"range":[188,288],"id":{"type":"Identifier","start":197,"end":221,"loc":{"start":{"line":7,"column":24},"end":{"line":7,"column":48}},"range":[197,221],"name":"invokeCallbackAfterDelay"},"generator":false,"expression":false,"async":false,"params":[{"type":"Identifier","start":223,"end":231,"loc":{"start":{"line":7,"column":50},"end":{"line":7,"column":58}},"range":[223,231],"name":"callback"}],"body":{"type":"BlockStatement","start":234,"end":288,"loc":{"start":{"line":7,"column":61},"end":{"line":9,"column":1}},"range":[234,288],"body":[{"type":"ExpressionStatement","start":237,"end":286,"loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":50}},"range":[237,286],"expression":{"type":"CallExpression","start":237,"end":285,"loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":49}},"range":[237,285],"callee":{"type":"Identifier","start":237,"end":247,"loc":{"start":{"line":8,"column":1},"end":{"line":8,"column":11}},"range":[237,247],"name":"setTimeout"},"arguments":[{"type":"ArrowFunctionExpression","start":249,"end":277,"loc":{"start":{"line":8,"column":13},"end":{"line":8,"column":41}},"range":[249,277],"id":null,"generator":false,"expression":true,"async":false,"params":[],"body":{"type":"CallExpression","start":255,"end":277,"loc":{"start":{"line":8,"column":19},"end":{"line":8,"column":41}},"range":[255,277],"callee":{"type":"Identifier","start":255,"end":263,"loc":{"start":{"line":8,"column":19},"end":{"line":8,"column":27}},"range":[255,263],"name":"callback"},"arguments":[{"type":"CallExpression","start":265,"end":275,"loc":{"start":{"line":8,"column":29},"end":{"line":8,"column":39}},"range":[265,275],"callee":{"type":"MemberExpression","start":265,"end":273,"loc":{"start":{"line":8,"column":29},"end":{"line":8,"column":37}},"range":[265,273],"object":{"type":"Identifier","start":265,"end":269,"loc":{"start":{"line":8,"column":29},"end":{"line":8,"column":33}},"range":[265,269],"name":"Date"},"property":{"type":"Identifier","start":270,"end":273,"loc":{"start":{"line":8,"column":34},"end":{"line":8,"column":37}},"range":[270,273],"name":"now"},"computed":false},"arguments":[]}]}},{"type":"Literal","start":279,"end":283,"loc":{"start":{"line":8,"column":43},"end":{"line":8,"column":47}},"range":[279,283],"value":1000,"raw":"1000"}]}}]}},"leadingComments":[{"type":"Block","value":"*\n * Function invoking callback after delay with current timestamp in milliseconds\n * since epoch.\n *\n * @param {A&B} callback Callback function.\n ","start":0,"end":172,"range":[0,172],"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":3}}}]}] |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ignored the tab.