Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(
check-line-alignment
): allow tags
option to apply with "alwa…
…ys"; #703 Adds custom align transform with support for tags Co-authored-by: Renatho De Carli Rosa <renatho@automattic.com> Co-authored-by: Brett Zamir <brettz9@yahoo.com>
- Loading branch information
Showing
4 changed files
with
539 additions
and
22 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
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,180 @@ | ||
import { | ||
Markers, | ||
} from 'comment-parser/lib/primitives'; | ||
import { | ||
rewireSource, | ||
} from 'comment-parser/lib/util'; | ||
|
||
const zeroWidth = { | ||
name: 0, | ||
start: 0, | ||
tag: 0, | ||
type: 0, | ||
}; | ||
|
||
const shouldAlign = (tags, index, source) => { | ||
const tag = source[index].tokens.tag.replace('@', ''); | ||
const includesTag = tags.includes(tag); | ||
|
||
if (includesTag) { | ||
return true; | ||
} | ||
|
||
if (tag !== '') { | ||
return false; | ||
} | ||
|
||
for (let iterator = index; iterator >= 0; iterator--) { | ||
const previousTag = source[iterator].tokens.tag.replace('@', ''); | ||
|
||
if (previousTag !== '') { | ||
if (tags.includes(previousTag)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
const getWidth = (tags) => { | ||
return (width, {tokens}, index, source) => { | ||
if (!shouldAlign(tags, index, source)) { | ||
return width; | ||
} | ||
|
||
return { | ||
name: Math.max(width.name, tokens.name.length), | ||
start: tokens.delimiter === Markers.start ? tokens.start.length : width.start, | ||
tag: Math.max(width.tag, tokens.tag.length), | ||
type: Math.max(width.type, tokens.type.length), | ||
}; | ||
}; | ||
}; | ||
|
||
const space = (len) => { | ||
return ''.padStart(len, ' '); | ||
}; | ||
|
||
const alignTransform = (tags, indent) => { | ||
let intoTags = false; | ||
let width; | ||
|
||
const alignTokens = (tokens) => { | ||
const nothingAfter = { | ||
delim: false, | ||
name: false, | ||
tag: false, | ||
type: false, | ||
}; | ||
|
||
if (tokens.description === '') { | ||
nothingAfter.name = true; | ||
tokens.postName = ''; | ||
|
||
if (tokens.name === '') { | ||
nothingAfter.type = true; | ||
tokens.postType = ''; | ||
|
||
if (tokens.type === '') { | ||
nothingAfter.tag = true; | ||
tokens.postTag = ''; | ||
|
||
/* istanbul ignore next: Never happens because the !intoTags return. But it's here for consistency with the original align transform */ | ||
if (tokens.tag === '') { | ||
nothingAfter.delim = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
tokens.postDelimiter = nothingAfter.delim ? '' : ' '; | ||
|
||
if (!nothingAfter.tag) { | ||
tokens.postTag = space(width.tag - tokens.tag.length + 1); | ||
} | ||
if (!nothingAfter.type) { | ||
tokens.postType = space(width.type - tokens.type.length + 1); | ||
} | ||
if (!nothingAfter.name) { | ||
// If post name is empty for all lines (name width 0), don't add post name spacing. | ||
tokens.postName = width.name === 0 ? '' : space(width.name - tokens.name.length + 1); | ||
} | ||
|
||
return tokens; | ||
}; | ||
|
||
const update = (line, index, source) => { | ||
const tokens = {...line.tokens}; | ||
if (tokens.tag !== '') { | ||
intoTags = true; | ||
} | ||
|
||
const isEmpty = | ||
tokens.tag === '' && | ||
tokens.name === '' && | ||
tokens.type === '' && | ||
tokens.description === ''; | ||
|
||
// dangling '*/' | ||
if (tokens.end === Markers.end && isEmpty) { | ||
tokens.start = indent + ' '; | ||
|
||
return { | ||
...line, | ||
tokens, | ||
}; | ||
} | ||
|
||
/* eslint-disable indent */ | ||
switch (tokens.delimiter) { | ||
case Markers.start: | ||
tokens.start = indent; | ||
break; | ||
case Markers.delim: | ||
tokens.start = indent + ' '; | ||
break; | ||
default: | ||
tokens.delimiter = ''; | ||
|
||
// compensate delimiter | ||
tokens.start = indent + ' '; | ||
} | ||
/* eslint-enable */ | ||
|
||
if (!intoTags) { | ||
tokens.postDelimiter = tokens.description === '' ? '' : ' '; | ||
|
||
return { | ||
...line, | ||
tokens, | ||
}; | ||
} | ||
|
||
// Not align. | ||
if (!shouldAlign(tags, index, source)) { | ||
return { | ||
...line, | ||
tokens, | ||
}; | ||
} | ||
|
||
return { | ||
...line, | ||
tokens: alignTokens(tokens), | ||
}; | ||
}; | ||
|
||
return ({source, ...fields}) => { | ||
width = source.reduce(getWidth(tags), {...zeroWidth}); | ||
|
||
return rewireSource({ | ||
...fields, | ||
source: source.map(update), | ||
}); | ||
}; | ||
}; | ||
|
||
export default alignTransform; |
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
Oops, something went wrong.