-
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): move each function to its own file
- Loading branch information
Showing
9 changed files
with
749 additions
and
728 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,84 @@ | ||
const R = require('ramda'); | ||
const { findTagIndex } = require('./utils'); | ||
|
||
/** | ||
* @typedef {import('../types').PJPAccessTagOptions} PJPAccessTagOptions | ||
* @typedef {import('../types').CommentTag} CommentTag | ||
*/ | ||
|
||
/** | ||
* Formats and normalizes the use of the access tag based on the plugin options. | ||
* | ||
* @param {CommentTag[]} tags The list of tags where the transformations should happen. | ||
* @param {PJPAccessTagOptions} options The plugin options for the access tag. | ||
* @returns {CommentTag[]} | ||
*/ | ||
const formatAccessTag = (tags, options) => { | ||
const indexes = tags.reduce( | ||
R.compose( | ||
findTagIndex('access', 'accessTag'), | ||
findTagIndex( | ||
['public', 'protected', 'private'], | ||
'typeTag', | ||
), | ||
)(R.identity), | ||
{ | ||
accessTag: -1, | ||
typeTag: -1, | ||
}, | ||
); | ||
|
||
let result; | ||
// If @access tags are allowed. | ||
if (options.jsdocAllowAccessTag) { | ||
// If @access tag needs to be enforced and there's a access type tag. | ||
if (options.jsdocEnforceAccessTag && indexes.typeTag > -1) { | ||
// If there's also an @access tag, just remove the access type tag. | ||
if (indexes.accessTag > -1) { | ||
result = R.remove(indexes.typeTag, 1, tags); | ||
indexes.typeTag = -1; | ||
} else { | ||
// If there's a access type tag but no @access tag, replace the access type tag with one. | ||
result = R.adjust(indexes.typeTag, (typeTag) => ({ | ||
tag: 'access', | ||
type: '', | ||
name: typeTag.tag, | ||
description: '', | ||
}), tags); | ||
} | ||
} | ||
// If @access tags are not allowed but there's one. | ||
} else if (indexes.accessTag > -1) { | ||
// If there's also an access type tag, just remove the @access tag. | ||
if (indexes.typeTag > -1) { | ||
result = R.remove(indexes.accessTag, 1, tags); | ||
indexes.accessTag = -1; | ||
} else { | ||
// If there's an @access tag but not access type tag, replace the @access tag with one. | ||
result = R.adjust(indexes.accessTag, (accessTag) => ({ | ||
tag: accessTag.name, | ||
type: '', | ||
name: '', | ||
description: '', | ||
}), tags); | ||
} | ||
} | ||
|
||
/** | ||
* If for some reason, there's an access tag and an access type tag and the options allow them | ||
* both, remove the first one declared. | ||
*/ | ||
if (indexes.accessTag > -1 && indexes.typeTag > -1) { | ||
const removeIndex = R.min(indexes.accessTag, indexes.typeTag); | ||
result = R.remove(removeIndex, 1, tags); | ||
} | ||
|
||
/** | ||
* Return the modified object, if there's one, or just a clone of the tags list. The reason | ||
* for the short circuit is so the implementation can always asume that the method returns | ||
* a clone, no matter if no modification was made. | ||
*/ | ||
return result || R.clone(tags); | ||
}; | ||
|
||
module.exports.formatAccessTag = formatAccessTag; |
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,92 @@ | ||
const R = require('ramda'); | ||
|
||
/** | ||
* @typedef {import('../types').PJPDescriptionTagOptions} PJPDescriptionTagOptions | ||
* @typedef {import('../types').CommentTag} CommentTag | ||
* @typedef {import('../types').CommentBlock} CommentBlock | ||
*/ | ||
|
||
/** | ||
* Finds all possible descriptions of a block (body, description tag, after a definition, etc.), | ||
* puts them together, and, depending on the options, it adds it on the body or a description tag. | ||
* | ||
* @param {CommentBlock} block The block to format. | ||
* @param {PJPDescriptionTagOptions} options The options that will tell the method how to handle | ||
* the description. | ||
* @returns {CommentBlock} | ||
*/ | ||
const formatDescription = (block, options) => { | ||
const result = R.clone(block); | ||
const { parts, tags, tagIndex } = result.tags.reduce( | ||
(acc, tag, index) => { | ||
let nextAcc; | ||
if (['typedef', 'callback', 'function'].includes(tag.tag)) { | ||
nextAcc = { | ||
parts: acc.parts.includes(tag.description) ? | ||
acc.parts : | ||
[...acc.parts, tag.description], | ||
tags: [...acc.tags, { ...tag, description: '' }], | ||
tagIndex: acc.tagIndex, | ||
}; | ||
} else if (tag.tag === 'description') { | ||
const value = `${tag.name} ${tag.description}`.trim(); | ||
nextAcc = { | ||
parts: acc.parts.includes(value) ? | ||
acc.parts : | ||
[...acc.parts, value], | ||
tags: [...acc.tags, { ...tag, name: '', description: '' }], | ||
tagIndex: index, | ||
}; | ||
} else { | ||
nextAcc = { | ||
...acc, | ||
tags: [...acc.tags, tag], | ||
}; | ||
} | ||
|
||
return nextAcc; | ||
}, | ||
{ | ||
parts: [result.description], | ||
tags: [], | ||
tagIndex: -1, | ||
}, | ||
); | ||
|
||
const description = parts | ||
.filter((part) => part.trim()) | ||
.join('\n\n'); | ||
|
||
if (options.jsdocAllowDescriptionTag) { | ||
if (tagIndex > -1) { | ||
result.description = ''; | ||
tags[tagIndex].description = description; | ||
} else if (options.jsdocUseDescriptionTag) { | ||
result.description = ''; | ||
const descriptionTag = { | ||
tag: 'description', | ||
name: '', | ||
description, | ||
type: '', | ||
}; | ||
if (tags.length > 1) { | ||
tags.splice(1, 0, descriptionTag); | ||
} else { | ||
tags.push(descriptionTag); | ||
} | ||
} else { | ||
result.description = description; | ||
} | ||
} else { | ||
if (tagIndex > -1) { | ||
tags.splice(tagIndex, 1); | ||
} | ||
|
||
result.description = description; | ||
} | ||
|
||
result.tags = tags; | ||
return result; | ||
}; | ||
|
||
module.exports.formatDescription = formatDescription; |
Oops, something went wrong.