-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathextension.ts
More file actions
170 lines (154 loc) · 7.24 KB
/
extension.ts
File metadata and controls
170 lines (154 loc) · 7.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import * as path from 'path';
import { ExtensionContext } from 'vscode';
import {
DidChangeConfigurationNotification,
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind
} from 'vscode-languageclient/node';
import * as vscode from 'vscode';
import { DevSkimSettings, DevSkimSettingsObject } from './common/devskimSettings';
import { getCodeFixMapping, getFileVersion } from './common/notificationNames';
import { selectors } from './common/selectors';
import { DevSkimFixer } from './devSkimFixer';
import { CodeFixMapping } from './common/codeFixMapping';
import { FileVersion } from './common/fileVersion';
let client: LanguageClient;
// Helper to rescan documents via custom server request
function rescanOpenDocuments() {
vscode.workspace.textDocuments
.filter(doc => selectors.some(s => vscode.languages.match(s, doc) > 0))
.forEach(doc => {
client?.sendRequest('devskim/rescanDocument', {
uri: doc.uri.toString(),
text: doc.getText(),
version: doc.version
}).catch(err => {
console.error(`DevSkim: Failed to rescan ${doc.uri.toString()}`, err);
});
});
}
async function resolveDotNetPath(): Promise<string> {
const result = await vscode.commands.executeCommand<any>(
"dotnet.acquire",
{
version: "8.0",
requestingExtensionId: "MS-CST-E.vscode-devskim",
}
);
return result?.dotnetPath;
}
function getDevSkimConfiguration(section='MS-CST-E.vscode-devskim' ): DevSkimSettings {
const settings: DevSkimSettings = new DevSkimSettingsObject();
settings.enableCriticalSeverityRules = vscode.workspace.getConfiguration(section).get('rules.enableCriticalSeverityRules', true);
settings.enableImportantSeverityRules = vscode.workspace.getConfiguration(section).get('rules.enableImportantSeverityRules', true);
settings.enableModerateSeverityRules = vscode.workspace.getConfiguration(section).get('rules.enableModerateSeverityRules', true);
settings.enableManualReviewSeverityRules = vscode.workspace.getConfiguration(section).get('rules.enableManualReviewSeverityRules', false);
settings.enableBestPracticeSeverityRules = vscode.workspace.getConfiguration(section).get('rules.enableBestPracticeSeverityRules', false);
settings.enableHighConfidenceRules = vscode.workspace.getConfiguration(section).get('rules.enableHighConfidenceRules', true);
settings.enableMediumConfidenceRules = vscode.workspace.getConfiguration(section).get('rules.enableMediumConfidenceRules', true);
settings.enableLowConfidenceRules = vscode.workspace.getConfiguration(section).get('rules.enableLowConfidenceRules', false);
settings.customRulesPaths = vscode.workspace.getConfiguration(section).get('rules.customRulesPath', []);
settings.customLanguagesPath = vscode.workspace.getConfiguration(section).get('rules.customLanguagesPath', "");
settings.customCommentsPath = vscode.workspace.getConfiguration(section).get('rules.customCommentsPath', "");
settings.suppressionDurationInDays = vscode.workspace.getConfiguration(section).get('suppressions.suppressionDurationInDays', 30);
settings.suppressionCommentStyle = vscode.workspace.getConfiguration(section).get('suppressions.suppressionCommentStyle', 'line');
settings.manualReviewerName = vscode.workspace.getConfiguration(section).get('suppressions.manualReviewerName', '');
settings.guidanceBaseURL = vscode.workspace.getConfiguration(section).get('guidance.guidanceBaseURL', "https://github.com/Microsoft/DevSkim/blob/main/guidance/");
settings.ignoreFiles = vscode.workspace.getConfiguration(section).get('ignores.ignoreFiles',
[ "out/.*", "bin/.*", "node_modules/.*", ".vscode/.*", "yarn.lock", "logs/.*", ".log", ".git" ]);
settings.ignoreRulesList = vscode.workspace.getConfiguration(section).get('ignores.ignoreRulesList', []);
settings.ignoreDefaultRules = vscode.workspace.getConfiguration(section).get('ignores.ignoreDefaultRules', false);
settings.removeFindingsOnClose = vscode.workspace.getConfiguration(section).get('findings.removeFindingsOnClose', false);
settings.scanOnOpen = vscode.workspace.getConfiguration(section).get('triggers.scanOnOpen', false);
settings.scanOnSave = vscode.workspace.getConfiguration(section).get('triggers.scanOnSave', false);
settings.scanOnChange = vscode.workspace.getConfiguration(section).get('triggers.scanOnChange', true);
settings.traceServer = vscode.workspace.getConfiguration(section).get('trace.server', false);
return settings;
}
export function activate(context: ExtensionContext) {
const config = getDevSkimConfiguration();
const fixer = new DevSkimFixer();
context.subscriptions.push(
vscode.languages.registerCodeActionsProvider(selectors, fixer, {
providedCodeActionKinds: DevSkimFixer.providedCodeActionKinds
})
);
// Register manual scan command
context.subscriptions.push(
vscode.commands.registerCommand('devskim.scanWorkspace', () => {
rescanOpenDocuments();
vscode.window.showInformationMessage('DevSkim: Rescanned all open files');
})
);
// The server bridge is implemented in .NET
const serverModule = vscode.Uri.joinPath(context.extensionUri, 'devskimBinaries', 'Microsoft.DevSkim.LanguageServer.dll');
resolveDotNetPath().then((dotNetPath) =>
{
if (dotNetPath == undefined || dotNetPath == null)
{
// Error, can't start extension
// TODO: Notify user
}
else
{
const workPath = path.dirname(serverModule.fsPath);
const serverOptions: ServerOptions = {
run: {
command: dotNetPath,
args: [serverModule.fsPath],
options: {cwd: workPath},
transport: TransportKind.pipe
},
debug: {
command: dotNetPath,
args: [serverModule.fsPath],
options: {cwd: workPath},
transport: TransportKind.pipe
}
};
// Options to control the language client
const clientOptions: LanguageClientOptions = {
documentSelector: selectors,
progressOnInitialization: true
};
client = new LanguageClient(
'MS-CST-E.vscode-devskim',
'DevSkim VS Code Client',
serverOptions,
clientOptions
);
// Start the client. This will also launch the server
client.registerProposedFeatures();
const disposable = client.start();
client.onReady().then(() =>
{
client.onNotification(getCodeFixMapping(), (mapping: CodeFixMapping) =>
{
fixer.ensureMapHasMappings(mapping);
});
client.onNotification(getFileVersion(), (fileversion: FileVersion) =>{
fixer.removeFindingsForOtherVersions(fileversion);
});
client.sendNotification(DidChangeConfigurationNotification.type, { settings: ""});
}
);
vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration("MS-CST-E.vscode-devskim"))
{
// Triggers server to query for client config.
// Hacky, but vscode insists a pull model should be used over a push model for transmitting settings.
client.sendNotification(DidChangeConfigurationNotification.type, { settings: "" });
rescanOpenDocuments();
}
});
// For disposal of the client
context.subscriptions.push(disposable);
}
});
}