Skip to content

Commit

Permalink
✨ Show warnings if keys dont have description
Browse files Browse the repository at this point in the history
Resolves #5
  • Loading branch information
hofp37 committed Jan 28, 2025
1 parent a4b5ea2 commit ad0397c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ The extension suggests the names of the variables that are present in the `env.j
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.
![Example](./resources/error_highlighting.png)

### Highlighting of secrets missing a description
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.
![Example](./resources/secret_missing_desdescription_warning.jpeg)

**Enjoy!**
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 59 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ export function activate(context: vscode.ExtensionContext) {
}
diagnosticCollection.set(file.document.uri, diagnostics)
}

// ======= Highlight keys without description when .env.jsonc file is changed =======
highlightKeysWithoutDescription(file.document)
})

vscode.workspace.onDidOpenTextDocument(async _ => {
highlightKeysWithoutDescription(vscode.window.activeTextEditor?.document)
})

// ======================== Add suggestions to the editor ========================
Expand Down Expand Up @@ -156,6 +163,58 @@ const matchMultipleLines = (
return isMatching
}

const highlightKeysWithoutDescription = (document?: vscode.TextDocument) => {
if (!vscode.workspace.workspaceFolders) {
return
}
if (document?.fileName.endsWith('.env.jsonc')) {
const text = document.getText()
const pattern = /\".*":/g
const matchedConfigKeys = text.match(pattern) ?? []
const diagnostics: vscode.Diagnostic[] = []

for (const key of matchedConfigKeys) {
const keyStartPos = text.indexOf(key)
const keyLine = document.positionAt(keyStartPos).line

const folderUri = vscode.workspace.workspaceFolders[0].uri
const fileUri = folderUri.with({
path: path.posix.join(folderUri.path, '.env.jsonc'),
})

const lineAboveKey = document.lineAt(keyLine - 1).text.trim()
if (!lineAboveKey.startsWith('//') && !lineAboveKey.endsWith('*/')) {
const keyName = key.split('"')[1]
const startPos = text.indexOf(`"${keyName}"`)
const endPos = startPos + keyName.length + 2
const keyRange = new vscode.Range(
document.positionAt(startPos),
document.positionAt(endPos)
)

const warningMessage = new vscode.Diagnostic(
keyRange,
`Key '${keyName}' does not have a description. Add a comment describing its purpose.`,
vscode.DiagnosticSeverity.Warning
)
warningMessage.relatedInformation = [
{
location: new vscode.Location(document.uri, keyRange),
message: 'Missing description for this key.',
},
]
warningMessage.code = {
value: 'key-without-description',
target: fileUri,
}
warningMessage.source = 'configuru'
diagnostics.push(warningMessage)
}
}
diagnosticCollection.set(document.uri, diagnostics)
}
}

// This method is called when extension is deactivated
export function deactivate() {
if (diagnosticCollection) {
Expand Down

0 comments on commit ad0397c

Please sign in to comment.