Skip to content

Commit

Permalink
feat(prettier-plugin-jsdoc): add function to format arrays
Browse files Browse the repository at this point in the history
This version of the formatArrays is based on a the prototype and it will be refactored
in an upcoming commit.
  • Loading branch information
homer0 committed Oct 5, 2020
1 parent b537123 commit 4719eef
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/fns/formatArrays.js
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;
128 changes: 128 additions & 0 deletions test/fns/formatArrays.test.js
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);
});
});
});

0 comments on commit 4719eef

Please sign in to comment.