-
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.
feat(prettier-plugin-jsdoc): add function that applies the types form…
…atters
- Loading branch information
Showing
8 changed files
with
296 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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,84 @@ | ||
const R = require('ramda'); | ||
const { formatTSTypes } = require('./formatTSTypes'); | ||
const { formatStringLiterals } = require('./formatStringLiterals'); | ||
const { formatArrays } = require('./formatArrays'); | ||
const { formatObjects } = require('./formatObjects'); | ||
|
||
/** | ||
* @typedef {import('../types').PJPTypesOptions} PJPTypesOptions | ||
* @typedef {import('../types').CommentTag} CommentTag | ||
*/ | ||
|
||
/** | ||
* A function generated by {@link getTypeFormatter} in order to run specific transformations | ||
* based on the `options` that were sent when it was created. | ||
* | ||
* @callback TypeFormatter | ||
* @param {string} type The type to format. | ||
* @returns {string} | ||
*/ | ||
|
||
/** | ||
* Generates a {@link TypeFormatter} function to modify a type based on the given `options`. | ||
* | ||
* @param {PJPTypesOptions} options The options that tell the function which formatters should | ||
* be included and which don't. | ||
* @returns {TypeFormatter} | ||
*/ | ||
const getTypeFormatter = (options) => { | ||
const fns = []; | ||
if (options.jsdocUseTypeScriptTypesCasing) { | ||
fns.push(formatTSTypes); | ||
} | ||
|
||
if (options.jsdocFormatStringLiterals) { | ||
fns.push(formatStringLiterals(R.__, options)); | ||
} | ||
|
||
if (options.jsdocUseShortArrays || options.jsdocFormatDotForArraysAndObjects) { | ||
fns.push(formatArrays(R.__, options)); | ||
if (options.jsdocFormatDotForArraysAndObjects) { | ||
fns.push(formatObjects(R.__, options)); | ||
} | ||
} | ||
|
||
return fns.length ? R.compose(...fns.reverse()) : R.identity; | ||
}; | ||
|
||
/** | ||
* This function will take the `type` of a tag, run it through the `formatter` and return the | ||
* updated `tag`. | ||
* | ||
* @callback FormatTagTypeFn | ||
* @param {TypeFormatter} formatter The formatter function for the tag. | ||
* @param {CommentTag} tag The tag which type will be formatted. | ||
* @returns {CommentTag} | ||
*/ | ||
|
||
/** | ||
* @type {FormatTagTypeFn} | ||
*/ | ||
const formatTagType = R.curry((formatter, tag) => R.compose( | ||
(type) => ({ ...tag, type }), | ||
formatter, | ||
R.prop('type'), | ||
)(tag)); | ||
|
||
/** | ||
* Formats the types of a list of tags. | ||
* | ||
* @callback FormatTagsTypes | ||
* @param {CommentTag[]} tags The list of tags to format. | ||
* @param {PJPTypesOptions} options The customization options for the formatter. | ||
* @returns {CommentTag[]} | ||
*/ | ||
|
||
/** | ||
* @type {FormatTagsTypes} | ||
*/ | ||
const formatTagsTypes = R.curry((tags, options) => R.map(R.when( | ||
R.has('type'), | ||
formatTagType(getTypeFormatter(options)), | ||
))(tags)); | ||
|
||
module.exports.formatTagsTypes = formatTagsTypes; |
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
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
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
Oops, something went wrong.