Skip to content

Commit

Permalink
fix(prettier-plugin-jsdoc): move the dots function to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
homer0 committed Oct 5, 2020
1 parent b4ea963 commit c111d54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/fns/formatArrays.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
const R = require('ramda');
const { isMatch } = require('./utils');
const { isMatch, replaceDotOnTypeGeneric } = require('./utils');
/**
* @typedef {import('../types').PJPTypesOptions} PJPTypesOptions
*/

/**
* Depending on whether dots before array generics are allowed or not, this method will remove
* them or add them if not present.
*
* @callback ReplaceDotFn
* @param {boolean} useDot Whether or not the dots should be present.
* @param {string} type The actual type where the dots will be added or removed.
* @returns {string}
*/

/**
* @type {ReplaceDotFn}
*/
const replaceDot = R.curry((useDot, type) => R.ifElse(
R.always(useDot),
R.replace(/([^\w]|^)Array\s*</g, '$1Array.<'),
R.replace(/([^\w]|^)Array\s*\.\s*</g, '$1Array<'),
)(type));

/**
* This is the function that actuall processes the types and the options of {@link formatArrays}.
* The reason this is on a separated function is to avoid adding all this composition inside the
Expand All @@ -40,7 +21,7 @@ const replaceDot = R.curry((useDot, type) => R.ifElse(
const processType = R.curry((options, type) => R.compose(
R.when(
R.always(options.jsdocFormatDotForArraysAndObjects),
replaceDot(options.jsdocUseDotForArraysAndObjects),
replaceDotOnTypeGeneric('Array', options.jsdocUseDotForArraysAndObjects),
),
R.when(
R.always(options.jsdocUseShortArrays),
Expand Down
24 changes: 24 additions & 0 deletions src/fns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,34 @@ const isMatch = R.curry((expression, str) => R.compose(
R.match(expression),
)(str));

/**
* Depending on `useDot`, this function will ensure that the type name and the generics for a
* target type are separated, or not, with a dot.
* For the use of generics, JSDoc recommends the use a dot before listing them
* (i.e. `Array.<string>`), but that is unnecessary if you are using JSDoc for TypeScript
* annotations.
*
* @callback ReplaceDotOnTypeGeneric
* @param {string} targetType The type that should/shouldn't have a dot before generics.
* @param {boolean} useDot Whether or not the dots should be present.
* @param {string} type The actual type where the dots will be added or removed.
* @returns {string}
*/

/**
* @type {ReplaceDotOnTypeGeneric}
*/
const replaceDotOnTypeGeneric = R.curry((targetType, useDot, type) => R.ifElse(
R.always(useDot),
R.replace(new RegExp(`([^\\w]|^)${targetType}\\s*<`, 'g'), `$1${targetType}.<`),
R.replace(new RegExp(`([^\\w]|^)${targetType}\\s*\\.\\s*<`, 'g'), `$1${targetType}<`),
)(type));

module.exports.ensureArray = ensureArray;
module.exports.findTagIndex = findTagIndex;
module.exports.appendIfNotPresent = appendIfNotPresent;
module.exports.joinIfNotEmpty = joinIfNotEmpty;
module.exports.replaceLastItem = replaceLastItem;
module.exports.hasItems = hasItems;
module.exports.isMatch = isMatch;
module.exports.replaceDotOnTypeGeneric = replaceDotOnTypeGeneric;

0 comments on commit c111d54

Please sign in to comment.