-
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 arrays
This version of the formatArrays is based on a the prototype and it will be refactored in an upcoming commit.
- Loading branch information
Showing
2 changed files
with
157 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,29 @@ | ||
/** | ||
* @typedef {import('../types').PJPTypesOptions} PJPTypesOptions | ||
*/ | ||
|
||
/** | ||
* 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 formatArrays = (type, options) => { | ||
if (!type.match(/Array\.?\s*</)) return type; | ||
let result = type; | ||
if (options.jsdocUseShortArrays) { | ||
result = result.replace(/([^\w]|^)Array\s*(?:\.)?\s*<([\w\(\)|]+)>/g, '$1$2[]'); | ||
} | ||
|
||
if (options.jsdocFormatDotForArraysAndObjects) { | ||
result = options.jsdocUseDotForArraysAndObjects ? | ||
result.replace(/([^\w]|^)Array\s*</g, '$1Array.<') : | ||
result.replace(/([^\w]|^)Array\s*\.\s*</g, '$1Array<'); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
module.exports.formatArrays = formatArrays; |
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,128 @@ | ||
jest.unmock('../../src/fns/formatArrays'); | ||
|
||
const { formatArrays } = require('../../src/fns/formatArrays'); | ||
|
||
describe('formatArrays', () => { | ||
describe('shortForm', () => { | ||
it('should ignore a type that doesn\'t have an array', () => { | ||
// Given | ||
const input = 'string'; | ||
const output = 'string'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('shouldn\'t transform an array if the option is disabled', () => { | ||
// Given | ||
const input = 'Array<string>'; | ||
const output = 'Array<string>'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: false, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('should transform an array', () => { | ||
// Given | ||
const input = 'Array<string>'; | ||
const output = 'string[]'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: true, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('should transform an array inside a generic', () => { | ||
// Given | ||
const input = 'Promise<Array<string>>'; | ||
const output = 'Promise<string[]>'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: true, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('shouldn\'t transform an array inside another array', () => { | ||
// Given | ||
const input = 'Array<Array<string>>'; | ||
const output = 'Array<string[]>'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: true, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('shouldn\'t transform an array inside another array, inside a generic', () => { | ||
// Given | ||
const input = 'Promise<Array<Array<string>>>'; | ||
const output = 'Promise<Array<string[]>>'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: true, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('should transform multiple arrays inside a type', () => { | ||
// Given | ||
const input = 'Promise<Array<Array<string>>|string>|Array<number>'; | ||
const output = 'Promise<Array<string[]>|string>|number[]'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: true, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
}); | ||
|
||
describe('dot', () => { | ||
it('should add a dot before arrays\' generics', () => { | ||
// Given | ||
const input = 'Array<Promise<Array<Array<string>>|string>|Array<number>>'; | ||
const output = 'Array.<Promise<Array.<string[]>|string>|number[]>'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: true, | ||
jsdocFormatDotForArraysAndObjects: true, | ||
jsdocUseDotForArraysAndObjects: true, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
|
||
it('should remove the dot before arrays\' generics', () => { | ||
// Given | ||
const input = 'Array.<Promise<Array.<Array.<string>>|string>|Array.<number>>'; | ||
const output = 'Array<Promise<Array<string[]>|string>|number[]>'; | ||
let result = null; | ||
// When | ||
result = formatArrays(input, { | ||
jsdocUseShortArrays: true, | ||
jsdocFormatDotForArraysAndObjects: true, | ||
jsdocUseDotForArraysAndObjects: false, | ||
}); | ||
// Then | ||
expect(result).toBe(output); | ||
}); | ||
}); | ||
}); |