From 925b2e7afe44462b782fcb34fb799d2c8e24d283 Mon Sep 17 00:00:00 2001 From: Asaf Agami Date: Tue, 15 Aug 2023 11:53:10 +0300 Subject: [PATCH] fix: Snyk Learn link missing [HEAD-558] (#370) * fix: restore state directly in the script * refactor: use actual types --- src/snyk/common/views/webviewProvider.ts | 6 +- .../codeSuggestionWebviewProvider.ts | 4 +- .../suggestion/codeSuggestionWebviewScript.ts | 85 +++++++++++++++---- 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/snyk/common/views/webviewProvider.ts b/src/snyk/common/views/webviewProvider.ts index b83515b3d..f999aa54c 100644 --- a/src/snyk/common/views/webviewProvider.ts +++ b/src/snyk/common/views/webviewProvider.ts @@ -64,11 +64,11 @@ export abstract class WebviewProvider implements IWebViewProvider { + protected checkVisibility(): void { if (this.panel && this.panel.visible) { try { - await this.panel.webview.postMessage({ type: 'get' }); - await this.panel.webview.postMessage({ type: 'getLesson' }); + void this.panel.webview.postMessage({ type: 'get' }); + void this.panel.webview.postMessage({ type: 'getLesson' }); } catch (e) { if (!this.panel) return; // can happen due to asynchronicity, ignore such cases Logger.error(`Failed to restore the '${this.panel.title}' webview.`); diff --git a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts index dc16366ed..2721227ba 100644 --- a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts +++ b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewProvider.ts @@ -10,6 +10,7 @@ import { ErrorHandler } from '../../../common/error/errorHandler'; import { CodeIssueData, ExampleCommitFix, Issue, Marker, Point } from '../../../common/languageServer/types'; import { ILog } from '../../../common/logger/interfaces'; import { messages as learnMessages } from '../../../common/messages/learn'; +import { LearnService } from '../../../common/services/learnService'; import { getNonce } from '../../../common/views/nonce'; import { WebviewPanelSerializer } from '../../../common/views/webviewPanelSerializer'; import { WebviewProvider } from '../../../common/views/webviewProvider'; @@ -22,7 +23,6 @@ import { messages as errorMessages } from '../../messages/error'; import { getAbsoluteMarkerFilePath } from '../../utils/analysisUtils'; import { IssueUtils } from '../../utils/issueUtils'; import { ICodeSuggestionWebviewProvider } from '../interfaces'; -import { LearnService } from '../../../common/services/learnService'; type Suggestion = { id: string; @@ -114,7 +114,7 @@ export class CodeSuggestionWebviewProvider this.panel.webview.html = this.getHtmlForWebview(this.panel.webview); - await this.panel.webview.postMessage({ type: 'set', args: this.mapToModel(issue) }); + void this.panel.webview.postMessage({ type: 'set', args: this.mapToModel(issue) }); void this.postLearnLessonMessage(issue); this.issue = issue; diff --git a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts index 9bc44c6b7..ba4edb850 100644 --- a/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts +++ b/src/snyk/snykCode/views/suggestion/codeSuggestionWebviewScript.ts @@ -16,9 +16,45 @@ title: string; }; - let lesson: Lesson | null; + type ExampleCommitFix = { + commitURL: string; + lines: CommitChangeLine[]; + }; + type CommitChangeLine = { + line: string; + lineNumber: number; + lineChange: 'removed' | 'added' | 'none'; + }; + type Marker = { + msg: Point; + pos: MarkerPosition[]; + }; + type MarkerPosition = { + cols: Point; + rows: Point; + file: string; + }; + type Point = [number, number]; + type Suggestion = { + id: string; + message: string; + severity: string; + leadURL?: string; + rule: string; + repoDatasetSize: number; + exampleCommitFixes: ExampleCommitFix[]; + cwe: string[]; + title: string; + text: string; + isSecurityType: boolean; + uri: string; + markers?: Marker[]; + cols: Point; + rows: Point; + }; const vscode = acquireVsCodeApi(); + function navigateToUrl(url: string) { sendMessage({ type: 'openBrowser', @@ -27,19 +63,27 @@ } let exampleCount = 0; - let suggestion = {} as any; + + // Try to restore the previous state + let lesson: Lesson | null = vscode.getState()?.lesson || null; + fillLearnLink(); + let suggestion: Suggestion | null = vscode.getState()?.suggestion || null; + showCurrentSuggestion(); function navigateToLeadURL() { - if (!suggestion.leadURL) return; + if (!suggestion?.leadURL) return; navigateToUrl(suggestion.leadURL); } function navigateToIssue(_e: any, range: any) { + if (!suggestion) return; sendMessage({ type: 'openLocal', - args: getSuggestionPosition(range), + args: getSuggestionPosition(suggestion, range), }); } function navigateToCurrentExample() { + if (!suggestion?.exampleCommitFixes) return; + const url = suggestion.exampleCommitFixes[exampleCount].commitURL; sendMessage({ type: 'openBrowser', @@ -47,10 +91,12 @@ }); } function ignoreIssue(lineOnly: boolean) { + if (!suggestion) return; + sendMessage({ type: 'ignoreIssue', args: { - ...getSuggestionPosition(), + ...getSuggestionPosition(suggestion), message: suggestion.message, rule: suggestion.rule, id: suggestion.id, @@ -67,12 +113,12 @@ }, }); } - function getSuggestionPosition(position?: { file: string; rows: any; cols: any }) { + function getSuggestionPosition(suggestionParam: Suggestion, position?: { file: string; rows: any; cols: any }) { return { - uri: position?.file ?? suggestion.uri, - rows: position ? position.rows : suggestion.rows, - cols: position ? position.cols : suggestion.cols, - suggestionUri: suggestion.uri, + uri: position?.file ?? suggestionParam.uri, + rows: position ? position.rows : suggestionParam.rows, + cols: position ? position.cols : suggestionParam.cols, + suggestionUri: suggestionParam.uri, }; } function previousExample() { @@ -88,8 +134,7 @@ } function showCurrentExample() { if ( - !suggestion || - !suggestion.exampleCommitFixes.length || + !suggestion?.exampleCommitFixes?.length || exampleCount < 0 || exampleCount >= suggestion.exampleCommitFixes.length ) @@ -141,6 +186,10 @@ } function showCurrentSuggestion() { + if (!suggestion) { + return; + } + exampleCount = 0; const currentSeverity = getCurrentSeverity(); const severity = document.getElementById('severity')!; @@ -185,7 +234,7 @@ }; title.appendChild(mark); const markMsg = document.createElement('span'); - markMsg.innerHTML = suggestion.message.substring(m.msg[0], (m.msg[1] as number) + 1); + markMsg.innerHTML = suggestion.message.substring(m.msg[0], m.msg[1] + 1); mark.appendChild(markMsg); let markLineText = ' ['; let first = true; @@ -199,7 +248,7 @@ markLine.innerHTML = markLineText; markLine.className = 'mark-position'; mark.appendChild(markLine); - i = (m.msg[1] as number) + 1; + i = m.msg[1] + 1; } const postText = suggestion.message.substring(i); const postMark = document.createTextNode(postText); @@ -219,7 +268,7 @@ const dataset = document.getElementById('dataset-number')!; const infoTop = document.getElementById('info-top')!; if (suggestion.repoDatasetSize) { - dataset.innerHTML = suggestion.repoDatasetSize; + dataset.innerHTML = suggestion.repoDatasetSize.toString(); infoTop.className = 'font-light'; } else { infoTop.className = 'font-light hidden'; @@ -228,13 +277,13 @@ const exampleTop = document.getElementById('example-top')!; const example = document.getElementById('example')!; const noExamples = document.getElementById('info-no-examples')!; - if (suggestion.exampleCommitFixes.length) { + if (suggestion?.exampleCommitFixes?.length) { exampleTop.className = 'row between'; example.className = ''; const exNum = document.getElementById('example-number')!; - exNum.innerHTML = suggestion.exampleCommitFixes.length; + exNum.innerHTML = suggestion.exampleCommitFixes.length.toString(); const exNum2 = document.getElementById('example-number2')!; - exNum2.innerHTML = suggestion.exampleCommitFixes.length; + exNum2.innerHTML = suggestion.exampleCommitFixes.length.toString(); noExamples.className = 'hidden'; showCurrentExample(); } else {