* Search for lines of the form
(codeSnippet, identifier)
| 9 | * Search for lines of the form |
| 10 | */ |
| 11 | function processHighlighting(codeSnippet, identifier) { |
| 12 | const lines = codeSnippet.split("\n"); |
| 13 | /** |
| 14 | * For an identifier = bar: |
| 15 | * |
| 16 | * Matches of the form: `highlight-next-line:foo:bar:baz` will be replaced with "highlight-next-line". |
| 17 | * Matches of the form: `highlight-next-line:foo:baz` will be replaced with "". |
| 18 | */ |
| 19 | const regex1 = /highlight-next-line:([a-zA-Z0-9-._:]+)/; |
| 20 | const replacement1 = "highlight-next-line"; |
| 21 | const regex2 = /highlight-start:([a-zA-Z0-9-._:]+)/; |
| 22 | const replacement2 = "highlight-start"; |
| 23 | const regex3 = /highlight-end:([a-zA-Z0-9-._:]+)/; |
| 24 | const replacement3 = "highlight-end"; |
| 25 | const regex4 = /this-will-error:([a-zA-Z0-9-._:]+)/; |
| 26 | const replacement4 = "this-will-error"; |
| 27 | |
| 28 | let mutated = false; |
| 29 | |
| 30 | const processLine = (line, regex, replacement) => { |
| 31 | const match = line.match(regex); |
| 32 | if (match) { |
| 33 | mutated = true; |
| 34 | |
| 35 | const identifiers = match[1].split(":"); |
| 36 | if (identifiers.includes(identifier)) { |
| 37 | line = line.replace(match[0], replacement); |
| 38 | } else { |
| 39 | // Remove matched text completely |
| 40 | line = line.replace(match[0], ""); |
| 41 | } |
| 42 | } else { |
| 43 | // No match: it's an ordinary line of code. |
| 44 | } |
| 45 | return line.trim() == "//" || line.trim() == "#" ? "" : line; |
| 46 | }; |
| 47 | |
| 48 | const countLeadingSpaces = (line) => { |
| 49 | const match = line.match(/^ */); |
| 50 | return match ? match[0].length : 0; |
| 51 | }; |
| 52 | let indention = 200; |
| 53 | let resultLines = []; |
| 54 | |
| 55 | for (let line of lines) { |
| 56 | mutated = false; |
| 57 | line = processLine(line, regex1, replacement1); |
| 58 | line = processLine(line, regex2, replacement2); |
| 59 | line = processLine(line, regex3, replacement3); |
| 60 | line = processLine(line, regex4, replacement4); |
| 61 | |
| 62 | if (!(line === "" && mutated)) { |
| 63 | resultLines.push(line); |
| 64 | |
| 65 | const leadingSpaces = countLeadingSpaces(line); |
| 66 | if (line.length > 0 && leadingSpaces < indention) { |
| 67 | indention = leadingSpaces; |
| 68 | } |
no test coverage detected