|
| 1 | +import type { Element, ElementContent } from 'hast' |
| 2 | +import type { MatchAlgorithm } from './notation-transformer' |
| 3 | + |
| 4 | +export type ParsedComments = { |
| 5 | + line: Element |
| 6 | + token: Element |
| 7 | + info: [prefix: string, content: string, suffix?: string] |
| 8 | + isJsxStyle: boolean |
| 9 | +}[] |
| 10 | + |
| 11 | +/** |
| 12 | + * some comment formats have to be located at the end of line |
| 13 | + * hence we can skip matching them for other tokens |
| 14 | + */ |
| 15 | +const matchers: [re: RegExp, endOfLine: boolean][] = [ |
| 16 | + [/^(<!--)(.+)(-->)$/, false], |
| 17 | + [/^(\/\*)(.+)(\*\/)$/, false], |
| 18 | + [/^(\/\/|["'#]|;{1,2}|%{1,2}|--)(.*)$/, true], |
| 19 | + /** |
| 20 | + * for multi-line comments like this |
| 21 | + */ |
| 22 | + [/^(\*)(.+)$/, true], |
| 23 | +] |
| 24 | + |
| 25 | +/** |
| 26 | + * @param lines line tokens |
| 27 | + * @param jsx enable JSX parsing |
| 28 | + * @param matchAlgorithm matching algorithm |
| 29 | + */ |
| 30 | +export function parseComments( |
| 31 | + lines: Element[], |
| 32 | + jsx: boolean, |
| 33 | + matchAlgorithm: MatchAlgorithm, |
| 34 | +): ParsedComments { |
| 35 | + const out: ParsedComments = [] |
| 36 | + |
| 37 | + for (const line of lines) { |
| 38 | + const elements = line.children |
| 39 | + let start = elements.length - 1 |
| 40 | + if (matchAlgorithm === 'v1') |
| 41 | + start = 0 |
| 42 | + else if (jsx) |
| 43 | + // one step further for JSX as comment is inside curly brackets |
| 44 | + start = elements.length - 2 |
| 45 | + |
| 46 | + for (let i = Math.max(start, 0); i < elements.length; i++) { |
| 47 | + const token = elements[i] |
| 48 | + if (token.type !== 'element') |
| 49 | + continue |
| 50 | + const head = token.children.at(0) |
| 51 | + if (head?.type !== 'text') |
| 52 | + continue |
| 53 | + |
| 54 | + const isLast = i === elements.length - 1 |
| 55 | + const match = matchToken(head.value, isLast) |
| 56 | + if (!match) |
| 57 | + continue |
| 58 | + |
| 59 | + if (jsx && !isLast && i !== 0) { |
| 60 | + out.push({ |
| 61 | + info: match, |
| 62 | + line, |
| 63 | + token, |
| 64 | + isJsxStyle: isValue(elements[i - 1], '{') && isValue(elements[i + 1], '}'), |
| 65 | + }) |
| 66 | + } |
| 67 | + else { |
| 68 | + out.push({ |
| 69 | + info: match, |
| 70 | + line, |
| 71 | + token, |
| 72 | + isJsxStyle: false, |
| 73 | + }) |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return out |
| 79 | +} |
| 80 | + |
| 81 | +function isValue(element: ElementContent, value: string): boolean { |
| 82 | + if (element.type !== 'element') |
| 83 | + return false |
| 84 | + const text = element.children[0] |
| 85 | + if (text.type !== 'text') |
| 86 | + return false |
| 87 | + |
| 88 | + return text.value.trim() === value |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @param text text value of comment node |
| 93 | + * @param isLast whether the token is located at the end of line |
| 94 | + */ |
| 95 | +function matchToken(text: string, isLast: boolean): [prefix: string, content: string, suffix?: string] | undefined { |
| 96 | + // no leading and trailing spaces allowed for matchers |
| 97 | + // we extract the spaces |
| 98 | + let trimmed = text.trimStart() |
| 99 | + const spaceFront = text.length - trimmed.length |
| 100 | + |
| 101 | + trimmed = trimmed.trimEnd() |
| 102 | + const spaceEnd = text.length - trimmed.length - spaceFront |
| 103 | + |
| 104 | + for (const [matcher, endOfLine] of matchers) { |
| 105 | + if (endOfLine && !isLast) |
| 106 | + continue |
| 107 | + |
| 108 | + const result = matcher.exec(trimmed) |
| 109 | + if (!result) |
| 110 | + continue |
| 111 | + |
| 112 | + return [ |
| 113 | + ' '.repeat(spaceFront) + result[1], |
| 114 | + result[2], |
| 115 | + result[3] ? result[3] + ' '.repeat(spaceEnd) : undefined, |
| 116 | + ] |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Remove empty comment prefixes at line end, e.g. `// ` |
| 122 | + * |
| 123 | + * For matchAlgorithm v1 |
| 124 | + */ |
| 125 | +export function v1ClearEndCommentPrefix(text: string): string { |
| 126 | + const regex = /(?:\/\/|["'#]|;{1,2}|%{1,2}|--)(.*)$/ |
| 127 | + const result = regex.exec(text) |
| 128 | + |
| 129 | + if (result && result[1].trim().length === 0) { |
| 130 | + return text.slice(0, result.index) |
| 131 | + } |
| 132 | + |
| 133 | + return text |
| 134 | +} |
0 commit comments