Commit d1d3f3ed authored by Wiktor Walasek's avatar Wiktor Walasek

Fixes, tests and short yields investigation

parent db6ed576
......@@ -43,7 +43,7 @@ const vertiacallyAlignableTags = [
'property',
'return',
'throws',
'yields',
// 'yields',
]
/**
......@@ -150,12 +150,12 @@ function jsdocParser(text, parsers, options) {
}
}
if (['description', 'param', 'return', 'todo'].includes(tag.title))
tag.description = formatDescription(tag.description, options.jsdocAddDotToDescription)
if (['description', 'param', 'property', 'return', 'yields', 'throws', 'todo'].includes(tag.title))
tag.description = formatDescription(tag.description, options.jsdocDescriptionWithDot)
if (!tag.description && ['description', 'param', 'return', 'todo', 'memberof'].includes(tag.title) &&
if (!tag.description && ['description', 'param', 'property', 'return', 'yields', 'throws', 'todo', 'memberof'].includes(tag.title) &&
(!tag.type || !['Undefined', 'undefined', 'Null', 'null', 'Void', 'void'].includes(tag.type.name)))
tag.description = formatDescription('TODO', options.jsdocAddDotToDescription)
tag.description = formatDescription('TODO', options.jsdocDescriptionWithDot)
return tag
})
......@@ -171,20 +171,14 @@ function jsdocParser(text, parsers, options) {
let descGapAdj = 0
if (options.jsdocVerticalAlignment && vertiacallyAlignableTags.includes(tag.title)) {
if (tag.title)
tagTitleGapAdj += maxTagTitleLength - tag.title.length
else
descGapAdj += maxTagTitleLength + gap.length
if (tag.type.name)
tagTypeGapAdj += maxTagTypeNameLength - tag.type.name.length
else
descGapAdj += maxTagTypeNameLength + gap.length
if (tag.name)
tagNameGapAdj += maxTagNameLength - tag.name.length
else
descGapAdj = maxTagNameLength + gap.length
if (tag.title) tagTitleGapAdj += maxTagTitleLength - tag.title.length
else if (maxTagTitleLength) descGapAdj += maxTagTitleLength + gap.length
if (tag.type && tag.type.name) tagTypeGapAdj += maxTagTypeNameLength - tag.type.name.length
else if (maxTagTypeNameLength) descGapAdj += maxTagTypeNameLength + gap.length
if (tag.name) tagNameGapAdj += maxTagNameLength - tag.name.length
else if (maxTagNameLength) descGapAdj = maxTagNameLength + gap.length
}
let useTagTitle = (tag.title !== 'description' || options.jsdocDescriptionTag)
......@@ -196,10 +190,10 @@ function jsdocParser(text, parsers, options) {
// Add description (complicated because of text wrap)
if (tag.description && tag.title !== 'example') {
if (useTagTitle) tagString += gap + ' '.repeat(descGapAdj)
if (['memberof', 'see'].includes(tag.title)) { // Avoid wrapping
tagString += tag.description
} else { // Wrap tag description
if (useTagTitle) tagString += gap + ' '.repeat(descGapAdj)
const marginLength = tagString.length
let maxWidth = printWidth
if (marginLength >= maxWidth) maxWidth = marginLength + 40
......@@ -289,29 +283,29 @@ module.exports = {
]}],
description: 'Define order of tags.',
},
jsdocAddDotToDescription: {
jsdocDescriptionWithDot: {
type: 'boolean',
category: 'Global',
default: false,
description: 'Punctuation, is: a. key?'
description: 'Should dot be inserted at the end of description'
},
jsdocDescriptionTag: {
type: 'boolean',
category: 'Global',
default: true,
description: 'please disable me',
description: 'Should description tag be used',
},
jsdocVerticalAlignment: {
type: 'boolean',
category: 'Global',
default: false,
description: 'keep nice and align',
description: 'Should tags, types, names and description be aligned',
}
},
defaultOptions: {
jsdocSpaces: 1,
jsdocPrintWidth: 80,
jsdocAddDotToDescription: false,
jsdocDescriptionWithDot: false,
jsdocDescriptionTag: true,
jsdocVerticalAlignment: false,
}
......
......@@ -8,7 +8,8 @@ function subject(code, options = {}) {
parser: 'jsdoc-parser',
plugins: ['.'],
jsdocSpaces: 1,
jsdocPrintWidth: 80
jsdocPrintWidth: 80,
...options
});
}
......@@ -152,9 +153,96 @@ test('Should add TODO for return desc if it has undefined|null|void type', () =>
*/
`
// expect(Result1).toEqual(Expected1)
expect(Result1).toEqual(Expected1)
expect(Result2).toEqual(Expected2)
expect(Result3).toEqual(Expected3)
})
test.todo('spaces option')
test('Should align align vartically param|property|return|throws if option set tu true', () => {
const options = {
jsdocVerticalAlignment: true
}
const Result1 = subject(`/**
* @property {Object} unalginedProp unaligned property descriptin
* @param {String} unalginedParam unaligned param description
* @returns {undefined}
*/`, options)
const Expected1 = `/**
* @property {Object} unalginedProp Unaligned property descriptin
* @param {String} unalginedParam Unaligned param description
* @return {undefined}
*/
`
const Result2 = subject(`/**
* @throws {CustomExceptio} unaligned throws description
* @returns {String} unaligned returns description
*/`, options)
const Expected2 = `/**
* @throws {CustomExceptio} Unaligned throws description
* @return {String} Unaligned returns description
*/
`
expect(Result1).toEqual(Expected1)
expect(Result2).toEqual(Expected2)
})
test('Should insert proper amount of spaces based on option', () => {
const options1 = {
jsdocSpaces: 2
}
const Result1 = subject(`/**
* @param {Object} paramName param description that goes on and on and on utill it will need to be wrapped
* @returns {Number} return description
*/`, options1)
const Expected1 = `/**
* @param {Object} paramName Param description that goes on and on and on
* utill it will need to be wrapped
* @return {Number} Return description
*/
`
const options2 = {
jsdocSpaces: 3
}
const Result2 = subject(`/**
* @param {Object} paramName param description that goes on and on and on utill it will need to be wrapped
* @returns {Number} return description
*/`, options2)
const Expected2 = `/**
* @param {Object} paramName Param description that goes on and on and on
* utill it will need to be wrapped
* @return {Number} Return description
*/
`
expect(Result1).toEqual(Expected1)
expect(Result2).toEqual(Expected2)
})
test.skip('yields is broken', () => {
/*
* it will not be aligned prperly as doctrine seems to have problems with
* type here. Result of parse is:
* { title: 'yields', description: '{Number} return description' }
* and jsDoc does support type in this field:
* https://jsdoc.app/tags-yields.html
* As doctrine is not maitained at the moment solution here is either to fork
* it and fix it or search for other parser
*/
const options1 = {
jsdocSpaces: 2
}
const Result1 = subject(`/**
* @yields {Number} return description
*/`, options1)
const Expected1 = `/**
* @yields {Number} Return description
*/
`
expect(Result1).toEqual(Expected1)
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment