Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): add function that applies the types form…
Browse files Browse the repository at this point in the history
…atters
  • Loading branch information
homer0 committed Oct 5, 2020
1 parent e4766a2 commit 7828864
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 24 deletions.
9 changes: 7 additions & 2 deletions src/fns/formatArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ const processType = R.curry((options, type) => R.compose(
* Formats array types depending on the customization options. If the type doesn't contain an
* array, it will be returned without modifications.
*
* @callback FormatArraysFn
* @param {string} type The type to format.
* @param {PJPTypesOptions} options The customization options.
* @returns {string}
*/
const formatArrays = (type, options) => R.when(

/**
* @type {FormatArraysFn}
*/
const formatArrays = R.curry((type, options) => R.when(
isMatch(/Array\s*\.?\s*</),
processType(options),
)(type);
)(type));

module.exports.formatArrays = formatArrays;
9 changes: 7 additions & 2 deletions src/fns/formatObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ const processType = R.curry((options, type) => R.when(
* Formats array types depending on the customization options. If the type doesn't contain an
* array, it will be returned without modifications.
*
* @callback FormatObjectsFn
* @param {string} type The type to format.
* @param {PJPTypesOptions} options The customization options.
* @returns {string}
*/
const formatObjects = (type, options) => R.when(

/**
* @type {FormatObjectsFn}
*/
const formatObjects = R.curry((type, options) => R.when(
isMatch(/Object\s*\.?\s*</),
processType(options),
)(type);
)(type));

module.exports.formatObjects = formatObjects;
18 changes: 11 additions & 7 deletions src/fns/formatStringLiterals.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const R = require('ramda');
/**
* @typedef {import('../types').PJPStringLiteralsOptions} PJPStringLiteralsOptions
* @typedef {import('../types').PJPTypesOptions} PJPTypesOptions
*/

/**
Expand Down Expand Up @@ -41,7 +41,7 @@ const getFormatter = (padding, quote) => R.compose(
/**
* Generates the reducer to update a type and format any string literal type inside it.
*
* @param {PJPStringLiteralsOptions} options The options for the formatter.
* @param {PJPTypesOptions} options The options for the formatter.
* @returns {StringLiteralsReducer}
*/
const getReducer = (options) => {
Expand All @@ -67,18 +67,22 @@ const extractLiterals = R.match(/['"][\w\|\-\s'"]+['"](?:\s+)?/g);
* Formats the styling of string literals inside a type. If the type doesn't use string literals,
* it will be returned without modification.
*
* @param {string} type The type to format.
* @param {PJPStringLiteralsOptions} options The options that tell the function how to format the
* type.
* @callback FormatStringLiteralsFn
* @param {string} type The type to format.
* @param {PJPTypesOptions} options The options that tell the function how to format the type.
* @returns {string}
*/
const formatStringLiterals = (type, options) => R.compose(

/**
* @type {FormatStringLiteralsFn}
*/
const formatStringLiterals = R.curry((type, options) => R.compose(
(literals) => (
literals.length ?
R.reduce(getReducer(options), type, literals) :
type
),
extractLiterals,
)(type);
)(type));

module.exports.formatStringLiterals = formatStringLiterals;
84 changes: 84 additions & 0 deletions src/fns/formatTagsTypes.js
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;
18 changes: 7 additions & 11 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@
* tags. Default `true`.
*/

/**
* @typedef {Object} PJPStringLiteralsOptions
* @property {boolean} jsdocFormatStringLiterals
* Whether or not to apply transformations to string literal types. Default `true`.
* @property {boolean} jsdocUseSingleQuotesForStringLiterals
* Whether or not to use single quotes for string literals' types. Default `true`.
* @property {number} jsdocSpacesBetweenStringLiterals
* How many spaces should there be between string literals on a type. Default `0`.
*/

/**
* @typedef {Object} PJPTypesOptions
* @property {boolean} jsdocUseTypeScriptTypesCasing
Expand All @@ -53,6 +43,12 @@
* @property {boolean} jsdocUseDotForArraysAndObjects
* If the formatting for dots is enabled, this options will specify whether the dot is added or
* removed. Default `true`.
* @property {boolean} jsdocFormatStringLiterals
* Whether or not to apply transformations to string literal types. Default `true`.
* @property {boolean} jsdocUseSingleQuotesForStringLiterals
* Whether or not to use single quotes for string literals' types. Default `true`.
* @property {number} jsdocSpacesBetweenStringLiterals
* How many spaces should there be between string literals on a type. Default `0`.
*/

/**
Expand Down Expand Up @@ -95,7 +91,7 @@

/* eslint-disable max-len */
/**
* @typedef {PJPDescriptionTagOptions & PJPAccessTagOptions & PJPStringLiteralsOptions & PJPTypesOptions & PJPTagsOptions & PJPStyleOptions } PJPOptions
* @typedef {PJPDescriptionTagOptions & PJPAccessTagOptions & PJPTypesOptions & PJPTagsOptions & PJPStyleOptions } PJPOptions
*/
/* eslint-enable max-len */

Expand Down
2 changes: 1 addition & 1 deletion test/fns/formatArrays.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('formatArrays', () => {
const output = 'string';
let result = null;
// When
result = formatArrays(input);
result = formatArrays(input, {});
// Then
expect(result).toBe(output);
});
Expand Down
2 changes: 1 addition & 1 deletion test/fns/formatStringLiterals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('formatStringLiterals', () => {
const output = 'string';
let result = null;
// When
result = formatStringLiterals(input);
result = formatStringLiterals(input, {});
// Then
expect(result).toBe(output);
});
Expand Down

0 comments on commit 7828864

Please sign in to comment.