-
Notifications
You must be signed in to change notification settings - Fork 2
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 objects
- Loading branch information
Showing
2 changed files
with
72 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,38 @@ | ||
const R = require('ramda'); | ||
const { isMatch, replaceDotOnTypeGeneric } = require('./utils'); | ||
/** | ||
* @typedef {import('../types').PJPTypesOptions} PJPTypesOptions | ||
*/ | ||
|
||
/** | ||
* This is the function that actuall processes the types and the options of {@link formatObjects}. | ||
* The reason this is on a separated function is to avoid adding composition inside the `when`. | ||
* | ||
* @callback ProcessTypeFn | ||
* @param {string} type The type to format. | ||
* @param {PJPTypesOptions} options The customization options. | ||
* @returns {string} | ||
*/ | ||
|
||
/** | ||
* @type {ProcessTypeFn} | ||
*/ | ||
const processType = R.curry((options, type) => R.when( | ||
R.always(options.jsdocFormatDotForArraysAndObjects), | ||
replaceDotOnTypeGeneric('Object', options.jsdocUseDotForArraysAndObjects), | ||
)(type)); | ||
|
||
/** | ||
* Formats array types depending on the customization options. If the type doesn't contain an | ||
* array, it will be returned without modifications. | ||
* | ||
* @param {string} type The type to format. | ||
* @param {PJPTypesOptions} options The customization options. | ||
* @returns {string} | ||
*/ | ||
const formatObjects = (type, options) => R.when( | ||
isMatch(/Object\s*\.?\s*</), | ||
processType(options), | ||
)(type); | ||
|
||
module.exports.formatObjects = formatObjects; |
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,34 @@ | ||
jest.unmock('../../src/fns/formatObjects'); | ||
jest.unmock('../../src/fns/utils'); | ||
|
||
const { formatObjects } = require('../../src/fns/formatObjects'); | ||
|
||
describe('formatObjects', () => { | ||
it('should add a dot before objects\' generics', () => { | ||
// Given | ||
const input = 'Object<string,Object.<string,Object<string,number>>>'; | ||
const output = 'Object.<string,Object.<string,Object.<string,number>>>'; | ||
let result = null; | ||
// When | ||
result = formatObjects(input, { | ||
jsdocFormatDotForArraysAndObjects: true, | ||
jsdocUseDotForArraysAndObjects: true, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('should remove the dot before objcects\' generics', () => { | ||
// Given | ||
const input = 'Object.<string,Object<string,Object.<string,number>>>'; | ||
const output = 'Object<string,Object<string,Object<string,number>>>'; | ||
let result = null; | ||
// When | ||
result = formatObjects(input, { | ||
jsdocFormatDotForArraysAndObjects: true, | ||
jsdocUseDotForArraysAndObjects: false, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
}); |