Skip to content

Commit

Permalink
feat: common placeholders validation check
Browse files Browse the repository at this point in the history
  • Loading branch information
NGPixel committed Jan 29, 2025
1 parent 7ce83f0 commit ffa15f0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/components/DrawerChecks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { checkArticles } from 'src/tools/articles'
import { checkHyphenation } from 'src/tools/hyphenation'
import { checkInclusiveLanguage } from 'src/tools/inclusive-language'
import { checkNonAscii } from 'src/tools/non-ascii'
import { checkCommonPlaceholders } from 'src/tools/placeholders'
import { useDocsStore } from 'src/stores/docs'
import { useEditorStore } from 'src/stores/editor'
import { modelStore } from 'src/stores/models'
Expand Down Expand Up @@ -108,6 +109,13 @@ const valChecks = [
description: 'Check for non-ASCII characters',
icon: 'mdi-translate',
click: () => nonAsciiCheck()
},
{
key: 'placeholders',
title: 'Placeholders Check',
description: 'Check for common placeholders',
icon: 'mdi-select-remove',
click: () => placeholdersCheck()
}
]
Expand Down Expand Up @@ -187,6 +195,25 @@ function nonAsciiCheck (silent = false) {
}
}
function placeholdersCheck (silent = false) {
const results = checkCommonPlaceholders(modelStore[docsStore.activeDocument.id].getValue())
if (results.count < 1) {
editorStore.setValidationCheckState('placeholders', 1)
editorStore.setValidationCheckDetails('placeholders', [])
if (!silent) {
$q.notify({
message: 'Looks good!',
caption: 'No common placeholders found.',
color: 'positive',
icon: 'mdi-select-remove'
})
}
} else {
editorStore.setValidationCheckState('placeholders', -2)
editorStore.setValidationCheckDetails('placeholders', results.details)
}
}
function runAllChecks () {
editorStore.clearErrors()
articlesCheck(true)
Expand Down
6 changes: 4 additions & 2 deletions src/stores/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export const useEditorStore = defineStore('editor', {
articles: 0,
hyphenation: 0,
inclusiveLanguage: 0,
nonAscii: 0
nonAscii: 0,
placeholders: 0
},
validationChecksDetails: {
articles: [],
hyphenation: [],
inclusiveLanguage: [],
nonAscii: []
nonAscii: [],
placeholders: []
},
wordWrap: true,
workingDirectory: '',
Expand Down
57 changes: 57 additions & 0 deletions src/tools/placeholders.js
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)
}
}

0 comments on commit ffa15f0

Please sign in to comment.