-
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add vulnerability quick-fix + hint #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
abbb343
feat: add vulnerability quick-fix + hint
nitodeco 3afa90f
chore: remove unused variable
nitodeco b451e10
refactor: improve vulnerability diagnostic messaging
nitodeco ecacd9c
Merge branch 'main' into pr/39
9romise e17e843
cleanup
9romise fe1cdde
Merge remote-tracking branch 'origin/main' into pr/39
9romise 0f13ef2
refactor: simplify
9romise dfdbef5
Update src/providers/diagnostics/rules/vulnerability.ts
nitodeco 7df5add
refactor: move test to code-actions
9romise 50e0a32
test: update
9romise ff072e3
refactor: simplify `getBestFixedInVersion`
9romise File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import type { CodeActionContext, CodeActionProvider, Diagnostic, Range, TextDocument } from 'vscode' | ||
| import { CodeAction, CodeActionKind, WorkspaceEdit } from 'vscode' | ||
|
|
||
| const FIXED_VERSION_MESSAGE_PATTERN = / Upgrade to (?<fixedInVersion>\S+) to fix\.$/ | ||
|
|
||
| function getDiagnosticCodeValue(diagnostic: Diagnostic): string | null { | ||
| if (typeof diagnostic.code === 'string') | ||
| return diagnostic.code | ||
|
|
||
| if (typeof diagnostic.code === 'object' && typeof diagnostic.code.value === 'string') | ||
| return diagnostic.code.value | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| function isVulnerabilityDiagnostic(diagnostic: Diagnostic): boolean { | ||
| return getDiagnosticCodeValue(diagnostic) === 'vulnerability' | ||
| } | ||
|
|
||
| function getFixedInVersion(diagnostic: Diagnostic): string | null { | ||
| const fixedInVersionMatch = FIXED_VERSION_MESSAGE_PATTERN.exec(diagnostic.message) | ||
| const fixedInVersion = fixedInVersionMatch?.groups?.fixedInVersion | ||
| return fixedInVersion && fixedInVersion.length > 0 ? fixedInVersion : null | ||
| } | ||
|
|
||
| function createUpdateVersionAction(document: TextDocument, range: Range, fixedInVersion: string): CodeAction { | ||
| const codeAction = new CodeAction(`Update to ${fixedInVersion} to fix vulnerabilities`, CodeActionKind.QuickFix) | ||
| codeAction.isPreferred = true | ||
| const workspaceEdit = new WorkspaceEdit() | ||
| workspaceEdit.replace(document.uri, range, fixedInVersion) | ||
| codeAction.edit = workspaceEdit | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return codeAction | ||
| } | ||
|
|
||
| export class VulnerabilityCodeActionProvider implements CodeActionProvider { | ||
| provideCodeActions(document: TextDocument, _range: Range, context: CodeActionContext): CodeAction[] { | ||
| return context.diagnostics.flatMap((diagnostic) => { | ||
| if (!isVulnerabilityDiagnostic(diagnostic)) | ||
| return [] | ||
|
|
||
| const fixedInVersion = getFixedInVersion(diagnostic) | ||
| if (!fixedInVersion) | ||
| return [] | ||
|
|
||
| return [createUpdateVersionAction(document, diagnostic.range, fixedInVersion)] | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import type { CodeActionContext, Diagnostic, TextDocument } from 'vscode' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { Range, Uri } from 'vscode' | ||
| import { VulnerabilityCodeActionProvider } from '../../src/providers/code-actions/vulnerability' | ||
|
|
||
| function createDiagnostic(options: { code: string | { value: string }, message: string }): Diagnostic { | ||
| return { | ||
| code: options.code, | ||
| message: options.message, | ||
| range: new Range(0, 0, 0, 6), | ||
| } as Diagnostic | ||
| } | ||
|
|
||
| function createTextDocument(versionText: string): TextDocument { | ||
| return { | ||
| uri: Uri.parse('file:///package.json'), | ||
| getText: vi.fn(() => versionText), | ||
| } as unknown as TextDocument | ||
| } | ||
|
|
||
| function createCodeActionContext(diagnostics: Diagnostic[]): CodeActionContext { | ||
| return { | ||
| diagnostics, | ||
| triggerKind: 1 as CodeActionContext['triggerKind'], | ||
| only: undefined, | ||
| } | ||
| } | ||
|
|
||
| describe('vulnerability code action provider', () => { | ||
| it('provides a quick fix when vulnerability message includes upgrade version', () => { | ||
| const provider = new VulnerabilityCodeActionProvider() | ||
| const textDocument = createTextDocument('^1.0.0') | ||
|
|
||
| const diagnostic = createDiagnostic({ | ||
| code: { value: 'vulnerability' }, | ||
| message: 'This version has 1 high vulnerability. Upgrade to ^1.2.3 to fix.', | ||
| }) | ||
|
|
||
| const codeActions = provider.provideCodeActions( | ||
| textDocument, | ||
| diagnostic.range, | ||
| createCodeActionContext([diagnostic]), | ||
| ) | ||
|
|
||
| expect(codeActions).toEqual([ | ||
| expect.objectContaining({ | ||
| title: 'Update to ^1.2.3 to fix vulnerabilities', | ||
| isPreferred: true, | ||
| }), | ||
| ]) | ||
| }) | ||
|
|
||
| it('does not provide a quick fix when vulnerability message has no upgrade target', () => { | ||
| const provider = new VulnerabilityCodeActionProvider() | ||
| const textDocument = createTextDocument('^1.0.0') | ||
|
|
||
| const diagnostic = createDiagnostic({ | ||
| code: { value: 'vulnerability' }, | ||
| message: 'This version has 1 high vulnerability.', | ||
| }) | ||
|
|
||
| const codeActions = provider.provideCodeActions( | ||
| textDocument, | ||
| diagnostic.range, | ||
| createCodeActionContext([diagnostic]), | ||
| ) | ||
|
|
||
| expect(codeActions).toHaveLength(0) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.