-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(prettier-plugin-jsdoc): make the function to prepare types work w…
…ith tags
- Loading branch information
Showing
3 changed files
with
109 additions
and
57 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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,80 @@ | ||
const { format } = require('prettier'); | ||
const R = require('ramda'); | ||
const { isMatch } = require('./utils'); | ||
|
||
/** | ||
* @typedef {import('../types').PrettierOptions} PrettierOptions | ||
* @typedef {import('../types').CommentTag} CommentTag | ||
*/ | ||
|
||
/** | ||
* Checks whether a tag type uses "complex" features, like generics, union, intercection, etc. | ||
* | ||
* @callback HasComplexTypeFn | ||
* @param {CommentTag} tag The tag which type will be validated. | ||
* @returns {boolean} | ||
*/ | ||
|
||
/** | ||
* @type {HasComplexTypeFn} | ||
*/ | ||
const hasComplexType = R.compose( | ||
isMatch(/[&<\.\|]/), | ||
R.prop('type'), | ||
); | ||
|
||
/** | ||
* Takes the type of a tag, creates some fake TS code, sends it to Prettier, cleans the result | ||
* and sets it up as the new type of the tag. | ||
* | ||
* @callback PreparePrettyTypeFn | ||
* @param {PrettierOptions} options The options sent to the plugin. | ||
* @param {CommentTag} tag The tag which type will be formatted. | ||
* @returns {CommentTag} | ||
*/ | ||
|
||
/** | ||
* @type {PreparePrettyTypeFn} | ||
*/ | ||
const preparePrettyType = R.curry((options, tag) => { | ||
let result; | ||
try { | ||
const useType = tag.type.replace(/\*/g, 'any'); | ||
const prefix = 'type complex = '; | ||
const newType = format(`${prefix}${useType}`, { | ||
...options, | ||
parser: 'typescript', | ||
}) | ||
.substr(prefix.length) | ||
.trim() | ||
.replace(/;$/, ''); | ||
result = R.assocPath(['type'], newType, tag); | ||
} catch (ignore) { | ||
// Ignore the error because if it failed, it's an issue with Pettier. | ||
result = R.clone(tag); | ||
} | ||
|
||
return result; | ||
}); | ||
|
||
/** | ||
* Checks if tag type uses generics/union/intersections/properties in order to format it using | ||
* Prettier as if it were code. | ||
* | ||
* @callback PrepareTagPrettyType | ||
* @param {CommentTag} tag The tag to validate and format. | ||
* @param {PrettierOptions} options The options that were sent to the plugin, to send back to | ||
* Prettier. | ||
* @returns {CommentTag} | ||
*/ | ||
|
||
/** | ||
* @type {PrepareTagPrettyType} | ||
*/ | ||
const prepareTagPrettyType = R.curry((tag, options) => R.when( | ||
hasComplexType, | ||
preparePrettyType(options), | ||
tag, | ||
)); | ||
|
||
module.exports.prepareTagPrettyType = prepareTagPrettyType; |
This file contains 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