-
Notifications
You must be signed in to change notification settings - Fork 61.6k
/
Copy pathhardcoded-data-variable.js
41 lines (38 loc) · 1.43 KB
/
hardcoded-data-variable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { addError, ellipsify } from 'markdownlint-rule-helpers'
import { getRange } from '../helpers/utils.js'
import frontmatter from '#src/frame/lib/read-frontmatter.js'
/*
This rule currently only checks for one hardcoded string but
can be generalized in the future to check for strings that
have data variables.
*/
export const hardcodedDataVariable = {
names: ['GHD005', 'hardcoded-data-variable'],
description:
'Strings that contain "personal access token" should use the product variable instead',
tags: ['single-source'],
function: (params, onError) => {
if (params.name.startsWith('data/variables/product.yml')) return
const frontmatterString = params.frontMatterLines.join('\n')
const fm = frontmatter(frontmatterString).data
if (fm && fm.autogenerated) return
for (let i = 0; i < params.lines.length; i++) {
const line = params.lines[i]
const regex = /personal access tokens?/gi
const matches = line.match(regex)
if (!matches) continue
for (const match of matches) {
const lineNumber = i + 1
const range = getRange(line, match)
addError(
onError,
lineNumber,
`The string ${match} can be replaced with a variable. You should use one of the variables from data/variables/product.yml instead of the literal phrase(s).`,
ellipsify(line),
range,
null, // No fix possible
)
}
}
},
}