Skip to content

Commit ad0397c

Browse files
committed
✨ Show warnings if keys dont have description
Resolves #5
1 parent a4b5ea2 commit ad0397c

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ The extension suggests the names of the variables that are present in the `env.j
1818
If your `config.ts` file contains a variable that is not present in the `env.jsonc` file, the extension will underline it as an error.
1919
![Example](./resources/error_highlighting.png)
2020

21+
### Highlighting of secrets missing a description
22+
If your `.env.jsonc` file contains a secret key that does not have description provided in a comment, the extension will underline it as a warning.
23+
![Example](./resources/secret_missing_desdescription_warning.jpeg)
24+
2125
**Enjoy!**
Loading

src/extension.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ export function activate(context: vscode.ExtensionContext) {
9595
}
9696
diagnosticCollection.set(file.document.uri, diagnostics)
9797
}
98+
99+
// ======= Highlight keys without description when .env.jsonc file is changed =======
100+
highlightKeysWithoutDescription(file.document)
101+
})
102+
103+
vscode.workspace.onDidOpenTextDocument(async _ => {
104+
highlightKeysWithoutDescription(vscode.window.activeTextEditor?.document)
98105
})
99106

100107
// ======================== Add suggestions to the editor ========================
@@ -156,6 +163,58 @@ const matchMultipleLines = (
156163
return isMatching
157164
}
158165

166+
const highlightKeysWithoutDescription = (document?: vscode.TextDocument) => {
167+
if (!vscode.workspace.workspaceFolders) {
168+
return
169+
}
170+
if (document?.fileName.endsWith('.env.jsonc')) {
171+
const text = document.getText()
172+
const pattern = /\".*":/g
173+
const matchedConfigKeys = text.match(pattern) ?? []
174+
const diagnostics: vscode.Diagnostic[] = []
175+
176+
for (const key of matchedConfigKeys) {
177+
const keyStartPos = text.indexOf(key)
178+
const keyLine = document.positionAt(keyStartPos).line
179+
180+
const folderUri = vscode.workspace.workspaceFolders[0].uri
181+
const fileUri = folderUri.with({
182+
path: path.posix.join(folderUri.path, '.env.jsonc'),
183+
})
184+
185+
const lineAboveKey = document.lineAt(keyLine - 1).text.trim()
186+
if (!lineAboveKey.startsWith('//') && !lineAboveKey.endsWith('*/')) {
187+
const keyName = key.split('"')[1]
188+
const startPos = text.indexOf(`"${keyName}"`)
189+
const endPos = startPos + keyName.length + 2
190+
const keyRange = new vscode.Range(
191+
document.positionAt(startPos),
192+
document.positionAt(endPos)
193+
)
194+
195+
const warningMessage = new vscode.Diagnostic(
196+
keyRange,
197+
`Key '${keyName}' does not have a description. Add a comment describing its purpose.`,
198+
vscode.DiagnosticSeverity.Warning
199+
)
200+
warningMessage.relatedInformation = [
201+
{
202+
location: new vscode.Location(document.uri, keyRange),
203+
message: 'Missing description for this key.',
204+
},
205+
]
206+
warningMessage.code = {
207+
value: 'key-without-description',
208+
target: fileUri,
209+
}
210+
warningMessage.source = 'configuru'
211+
diagnostics.push(warningMessage)
212+
}
213+
}
214+
diagnosticCollection.set(document.uri, diagnostics)
215+
}
216+
}
217+
159218
// This method is called when extension is deactivated
160219
export function deactivate() {
161220
if (diagnosticCollection) {

0 commit comments

Comments
 (0)