Skip to content

Commit

Permalink
fix(check-line-alignment): handle spaces in type and skip for singl…
Browse files Browse the repository at this point in the history
…e line (@renatho); closes #638

Reapplies changes from @renatho (now on differently-named files)
  • Loading branch information
Brett Zamir committed Jan 11, 2021
1 parent d76fe3c commit 577863b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -2011,6 +2011,19 @@ const config = {
// Options: ["always"]
// Message: Expected JSDoc block lines to be aligned.

/**
* My object.
*
* @typedef {Object} MyObject
*
* @property {{a: number, b: string, c}} lorem Description.
* @property {Object.<string, Class>} sit Description multi words.
* @property {Object.<string, Class>} amet Description} weird {multi} {{words}}.
* @property {Object.<string, Class>} dolor
*/
// Options: ["always"]
// Message: Expected JSDoc block lines to be aligned.

/**
* Not implemented yet.
*
Expand Down Expand Up @@ -2128,6 +2141,22 @@ const config = {
*/
// Options: ["always"]

/**
* My object.
*
* @typedef {Object} MyObject
*
* @property {{a: number, b: string, c}} lorem Description.
* @property {Object.<string, Class>} sit Description multi words.
* @property {Object.<string, Class>} amet Description} weird {multi} {{words}}.
* @property {Object.<string, Class>} dolor
*/
// Options: ["always"]

/** @param {number} lorem */
const fn = ( lorem ) => {}
// Options: ["always"]

/**
* Not validating without option.
*
Expand Down
9 changes: 7 additions & 2 deletions src/rules/checkLineAlignment.js
Expand Up @@ -31,7 +31,7 @@ const matchAll = (string, regexp, callback, limit) => {
* @returns {string} The full description.
*/
const getFullDescription = (lineString) => {
return /(?:\S+\s+){4}(.*)/.exec(lineString)[1];
return /\S+\s+(?:{{.*?}}|{.*?})\s+\S+\s+(.*)/.exec(lineString)[1];
};

/**
Expand Down Expand Up @@ -139,7 +139,7 @@ const checkCommentPerTag = (comment, tag, tagIndentation, report) => {
// All line parts until the first word of the description (if description exists).
matchAll(
lineString,
/\S+/g,
/{{.*?}}|{.*?}|\S+/g,
({0: match, index: position}, partIndex) => {
set(partsMatrix, [lineIndex, partIndex], {
position,
Expand Down Expand Up @@ -187,6 +187,11 @@ export default iterateJsdoc(({
return;
}

// Skip if it contains only a single line.
if (!jsdocNode.value.includes('\n')) {
return;
}

// `indent` is whitespace from line 1 (`/**`), so slice and account for "/".
const tagIndentation = indent + ' ';

Expand Down
61 changes: 61 additions & 0 deletions test/rules/assertions/checkLineAlignment.js
Expand Up @@ -364,6 +364,41 @@ export default {
*/
`,
},
{
code: `
/**
* My object.
*
* @typedef {Object} MyObject
*
* @property {{a: number, b: string, c}} lorem Description.
* @property {Object.<string, Class>} sit Description multi words.
* @property {Object.<string, Class>} amet Description} weird {multi} {{words}}.
* @property {Object.<string, Class>} dolor
*/
`,
errors: [
{
message: 'Expected JSDoc block lines to be aligned.',
type: 'Block',
},
],
options: [
'always',
],
output: `
/**
* My object.
*
* @typedef {Object} MyObject
*
* @property {{a: number, b: string, c}} lorem Description.
* @property {Object.<string, Class>} sit Description multi words.
* @property {Object.<string, Class>} amet Description} weird {multi} {{words}}.
* @property {Object.<string, Class>} dolor
*/
`,
},
{
code: `
/**
Expand Down Expand Up @@ -549,6 +584,32 @@ export default {
'always',
],
},
{
code: `
/**
* My object.
*
* @typedef {Object} MyObject
*
* @property {{a: number, b: string, c}} lorem Description.
* @property {Object.<string, Class>} sit Description multi words.
* @property {Object.<string, Class>} amet Description} weird {multi} {{words}}.
* @property {Object.<string, Class>} dolor
*/
`,
options: [
'always',
],
},
{
code: `
/** @param {number} lorem */
const fn = ( lorem ) => {}
`,
options: [
'always',
],
},
{
code: `
/**
Expand Down

0 comments on commit 577863b

Please sign in to comment.