Skip to content

Commit a6a09ca

Browse files
committed
✨ Highlight hidden config keys that have unsafe default values
Resolves #6
1 parent 240b624 commit a6a09ca

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/extension.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,46 @@ export function activate(context: vscode.ExtensionContext) {
3636
const missingKeysInDotEnv = configTsKeys.filter(
3737
x => !parsedDotEnvKeys.includes(x)
3838
)
39+
const hiddenPattern = /hidden\([\n\r\s]*('|").*("|')[\n\r\s]*\)/g
40+
const hiddenTsKeys =
41+
text
42+
.match(hiddenPattern)
43+
?.map(x => x.slice(7, -1).trim().slice(1, -1)) ?? []
44+
45+
const diagnostics: vscode.Diagnostic[] = []
46+
47+
// ============== Underline keys with unsafe default values in config.ts file ==============
48+
for (const hiddenKey of hiddenTsKeys) {
49+
const safeDefaultValue =
50+
parsedDotEnv[hiddenKey] === '' ||
51+
parsedDotEnv[hiddenKey] === `__${hiddenKey}__`
52+
if (!safeDefaultValue) {
53+
const startPos = file.document.getText().indexOf(`'${hiddenKey}'`)
54+
const endPos = startPos + hiddenKey.length + 2
55+
const keyRange = new vscode.Range(
56+
file.document.positionAt(startPos),
57+
file.document.positionAt(endPos)
58+
)
59+
const warningMessage = new vscode.Diagnostic(
60+
keyRange,
61+
`Key '${hiddenKey}' should have a safe default value. Use empty string or '__${hiddenKey}__' in .env.jsonc.`,
62+
vscode.DiagnosticSeverity.Warning
63+
)
64+
warningMessage.relatedInformation = [
65+
{
66+
location: new vscode.Location(file.document.uri, keyRange),
67+
message: 'Unsafe default value in .env',
68+
},
69+
]
70+
warningMessage.code = {
71+
value: 'key-unsafe-default-value',
72+
target: fileUri,
73+
}
74+
warningMessage.source = 'configuru'
75+
76+
diagnostics.push(warningMessage)
77+
}
78+
}
3979

4080
// ============== Underline missing keys in config.ts file ==============
4181
// Get all comments in the file in order to not underline missing keys in comments
@@ -52,7 +92,6 @@ export function activate(context: vscode.ExtensionContext) {
5292
}) ?? []
5393

5494
// Underline missing keys in config.ts file
55-
const diagnostics: vscode.Diagnostic[] = []
5695
for (const key of missingKeysInDotEnv) {
5796
const startPos = file.document.getText().indexOf(`'${key}'`)
5897
const endPos = startPos + key.length + 2

0 commit comments

Comments
 (0)