-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: common placeholders validation check
- Loading branch information
Showing
3 changed files
with
88 additions
and
2 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
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,57 @@ | ||
import { sortBy } from 'lodash-es' | ||
import { decorationsStore } from 'src/stores/models' | ||
|
||
export function checkCommonPlaceholders (text) { | ||
const matchRgx = /(?:[^a-z0-9]|RFC)(?<term>TBD|TBA|XX|YY|NN|MM|0000|TODO)(?:[^a-z0-9])/gi | ||
const textLines = text.split('\n') | ||
|
||
const decorations = [] | ||
const occurences = [] | ||
const details = [] | ||
for (const [lineIdx, line] of textLines.entries()) { | ||
for (const match of line.matchAll(matchRgx)) { | ||
const term = match[1].toLowerCase() | ||
const termStartIndex = match[0].indexOf(match[1]) | ||
let occIdx = occurences.indexOf(term) | ||
if (occIdx < 0) { | ||
occIdx = occurences.push(term) - 1 | ||
} | ||
decorations.push({ | ||
options: { | ||
hoverMessage: { | ||
value: `Common placeholder term ${match[1]} detected.` | ||
}, | ||
className: 'dec-warning', | ||
minimap: { | ||
position: 1 | ||
}, | ||
glyphMarginClassName: 'dec-warning-margin' | ||
}, | ||
range: { | ||
startLineNumber: lineIdx + 1, | ||
startColumn: match.index + termStartIndex + 1, | ||
endLineNumber: lineIdx + 1, | ||
endColumn: match.index + termStartIndex + 1 + match[1].length | ||
} | ||
}) | ||
details.push({ | ||
key: crypto.randomUUID(), | ||
group: occIdx + 1, | ||
message: match[1].toLowerCase(), | ||
range: { | ||
startLineNumber: lineIdx + 1, | ||
startColumn: match.index + termStartIndex + 1, | ||
endLineNumber: lineIdx + 1, | ||
endColumn: match.index + termStartIndex + 1 + match[1].length | ||
} | ||
}) | ||
} | ||
} | ||
|
||
decorationsStore.get('placeholders').set(decorations) | ||
|
||
return { | ||
count: decorations.length, | ||
details: sortBy(details, d => d.range.startLineNumber) | ||
} | ||
} |