-
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 to format TS types
- Loading branch information
Showing
3 changed files
with
103 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const R = require('ramda'); | ||
const { capitalize, ensureArray } = require('./utils'); | ||
|
||
/** | ||
* Utility function generated by {@link getFormater} and that can be used to modify a type name. | ||
* The difference between the formatter and the modifier is that the formatter can extract single | ||
* types from a comple type and run them through the modifier, where the modifier can only handle | ||
* one type. | ||
* | ||
* @callback TypeFormatter | ||
* @param {string} type The original type. | ||
* @returns {string} | ||
*/ | ||
|
||
/** | ||
* Utility function that is sent to {@link getFormatter} and that the formatter will use to modify | ||
* each single type name referenced on the given type. | ||
* | ||
* @callback TypeModifier | ||
* @param {string} type The original type. | ||
* @returns {string} | ||
*/ | ||
|
||
/** | ||
* Generates a formatter function that will replace a type (or types) after sending it through | ||
* a modification function. | ||
* | ||
* @param {string|string[]} type The type or types to format. | ||
* @param {TypeModifier} modifier The function that will modify each type. | ||
* @returns {TypeFormatter} | ||
*/ | ||
const getFormater = (type, modifier) => R.compose( | ||
(types) => R.replace( | ||
new RegExp(`([^\\w]|^)(${types})([^\\w]|$)`, 'g'), | ||
(_, before, rawType, after) => `${before}${modifier(rawType)}${after}`, | ||
), | ||
R.join('|'), | ||
ensureArray, | ||
)(type); | ||
|
||
/** | ||
* Formats each type reference on a type in order to match the casing allowed by TypeScript. | ||
* | ||
* @callback FormatTSTypesFn | ||
* @param {string} type The type to format. | ||
* @returns {string} | ||
*/ | ||
|
||
/** | ||
* @type {FormatTSTypesFn} | ||
*/ | ||
const formatTSTypes = R.compose( | ||
getFormater(['array', 'object'], capitalize), | ||
getFormater(['String', 'Number', 'Boolean'], R.toLower), | ||
); | ||
|
||
module.exports.formatTSTypes = formatTSTypes; |
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,29 @@ | ||
jest.unmock('../../src/fns/formatTSTypes'); | ||
jest.unmock('../../src/fns/utils'); | ||
|
||
const { formatTSTypes } = require('../../src/fns/formatTSTypes'); | ||
|
||
describe('formatTSTypes', () => { | ||
it | ||
.each([ | ||
['String', 'string'], | ||
['string', 'string'], | ||
['Number', 'number'], | ||
['number', 'number'], | ||
['Boolean', 'boolean'], | ||
['boolean', 'boolean'], | ||
['Array', 'Array'], | ||
['array', 'Array'], | ||
['Object', 'Object'], | ||
['object', 'Object'], | ||
['otherType', 'otherType'], | ||
['OtherType', 'OtherType'], | ||
])('should transform \'%s\' into \'%s\'', (input, output) => { | ||
// Given | ||
let result = null; | ||
// When | ||
result = formatTSTypes(input); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
}); |