-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
extension.js
306 lines (276 loc) · 11 KB
/
extension.js
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');
const yaml = require('yaml');
const validator = require('oas-validator');
const resolver = require('oas-resolver');
const converter = require('swagger2openapi');
const dc = vscode.languages.createDiagnosticCollection('openapi-lint');
function convert(yamlMode, resolve) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('You must have an open editor window to convert an OpenAPI document');
return; // No open text editor
}
if (resolve && editor.document.isUntitled) {
vscode.window.showWarningMessage('Document must be saved in order to resolve correctly');
return; // No open text editor
}
converter.convertStr(editor.document.getText(),{ patch: true, warnOnly: true, resolve: resolve, source: editor.document.fileName, fatal: true }, function(err, options) {
if (yamlMode) {
vscode.workspace.openTextDocument({ language: 'yaml', content: yaml.stringify(options.openapi) })
.then(function(doc) {
vscode.window.showTextDocument(doc);
});
}
else {
vscode.workspace.openTextDocument({ language: 'json', content: JSON.stringify(options.openapi, null, 2)})
.then(function(doc) {
vscode.window.showTextDocument(doc);
});
}
});
}
function translate(yamlMode) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('You must have an open editor window to convert an OpenAPI document');
return; // No open text editor
}
let text = editor.document.getText();
try {
let obj = yaml.parse(text);
let out = '';
if (yamlMode) {
out = yaml.stringify(obj);
}
else {
out = JSON.stringify(obj, null, 2);
}
editor.edit(builder => {
const document = editor.document;
const lastLine = document.lineAt(document.lineCount - 2);
const start = new vscode.Position(0, 0);
const end = new vscode.Position(document.lineCount - 1, lastLine.text.length);
builder.replace(new vscode.Range(start, end), out);
});
}
catch (ex) {
vscode.window.showErrorMessage('Could not parse OpenAPI document!\n'+ex.message);
console.warn(ex.message);
}
}
function bundle() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('You must have an open editor window to resolve an OpenAPI document');
return; // No open text editor
}
if (editor.document.isUntitled) {
vscode.window.showWarningMessage('Document must be saved in order to resolve correctly');
return; // No open text editor
}
let text = editor.document.getText();
let yamlMode = false;
let obj = {};
try {
obj = JSON.parse(text);
}
catch (ex) {
try {
obj = yaml.parse(text);
yamlMode = true;
}
catch (ex) {
vscode.window.showErrorMessage('Could not parse OpenAPI document as JSON or YAML');
console.warn(ex.message);
return;
}
}
resolver.resolve(obj, editor.document.fileName, { fatal: true })
.then(function(options){
if (yamlMode) {
vscode.workspace.openTextDocument({ language: 'yaml', content: yaml.stringify(options.openapi) })
.then(function(doc) {
vscode.window.showTextDocument(doc);
})
.then(function(ex) {
console.error(ex);
});
}
else {
vscode.workspace.openTextDocument({ language: 'json', content: JSON.stringify(options.openapi, null, 2)})
.then(function(doc) {
vscode.window.showTextDocument(doc);
})
.then(function(ex){
console.error(ex);
});
}
})
.catch(function(ex){
vscode.window.showWarningMessage('Could not parse OpenAPI document as JSON or YAML');
console.warn(ex.message);
});
}
function validate(lint, resolve) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('You must have an open editor window to validate an OpenAPI document');
return; // No open text editor
}
if (resolve && editor.document.isUntitled) {
vscode.window.showWarningMessage('Document must be saved in order to resolve correctly');
return; // No open text editor
}
let text = editor.document.getText();
try {
let options = { lint: lint, resolve: resolve, fatal: true, source: editor.document.fileName };
let obj = yaml.parse(text);
validator.validate(obj, options)
.then(function(){
vscode.window.showInformationMessage('Your OpenAPI document is '+(lint ? 'excellent!' : 'valid.'));
})
.catch(function(ex){
dc.delete(editor.document.uri);
const diagnostics = [];
let range; // TODO
let error = new vscode.Diagnostic(range, ex.message, vscode.DiagnosticSeverity.Error);
error.source = 'openapi-lint';
diagnostics.push(error);
for (let warning of options.warnings||[]) {
let warn = new vscode.Diagnostic(range, warning.message, vscode.DiagnosticSeverity.Warning);
warn.source = 'openapi-lint';
if (warning.rule.url) {
warn.code = { value: warning.ruleName, target: vscode.Uri.parse(warning.rule.url+'#'+warning.ruleName) };
}
else {
warn.code = warning.ruleName;
}
diagnostics.push(warn);
}
dc.set(editor.document.uri, diagnostics);
});
}
catch (ex) {
vscode.window.showErrorMessage('Could not parse OpenAPI document as JSON or YAML!');
console.warn(ex.message);
}
}
function optionallyValidateOnSave(document) {
const onSaveAction = vscode.workspace.getConfiguration('openapi-lint').get('onSaveAction');
if (onSaveAction === 'none') return true;
let text = document.getText();
try {
let obj = yaml.parse(text);
if (!obj || (!obj.openapi && !obj.swagger)) return false;
let options = {};
if ((onSaveAction === 'resolveAndValidate') || (onSaveAction === 'resolveAndLint')) {
options.resolve = true;
}
if ((onSaveAction === 'lint') || (onSaveAction === 'resolveAndLint')) {
options.lint = true;
}
validator.validate(obj, options)
.then(result => {
dc.delete(document.uri);
return result;
})
.catch(ex => {
dc.delete(document.uri);
const diagnostics = [];
let range; // TODO
diagnostics.push(new vscode.Diagnostic(range, ex.message, vscode.DiagnosticSeverity.Error));
for (let warning of options.warnings||[]) {
let warn = new vscode.Diagnostic(range, warning.message, vscode.DiagnosticSeverity.Warning);
warn.source = 'openapi-lint';
if (warning.rule.url) {
warn.code = { value: warning.ruleName, target: vscode.Uri.parse(warning.rule.url+'#'+warning.ruleName) };
}
else {
warn.code = warning.ruleName;
}
diagnostics.push(warn);
}
dc.set(document.uri, diagnostics);
return false;
});
}
catch (ex) {
// fail to parse is not a problem
return true;
}
}
function cleanArrayPrototype() {
for (const e in []) {
console.log('Cleaning Array.prototype pollution:',e);
Object.defineProperty(Array.prototype, e, {
value: Array.prototype[e],
enumerable: false
});
}
}
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
function activate(context) {
cleanArrayPrototype();
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
let cmdValidate = vscode.commands.registerCommand('extension.openapi-validate', function () {
validate(false, false);
});
let cmdLint = vscode.commands.registerCommand('extension.openapi-lint', function () {
validate(true, false);
});
let cmdValidateResolved = vscode.commands.registerCommand('extension.openapi-validate-resolved', function () {
validate(false, true);
});
let cmdLintResolved = vscode.commands.registerCommand('extension.openapi-lint-resolved', function () {
validate(true, true);
});
let cmdBundle = vscode.commands.registerCommand('extension.openapi-bundle', function () {
bundle();
});
let cmdTranslateToJson = vscode.commands.registerCommand('extension.openapi-to-json', function() {
translate(false);
});
let cmdTranslateToYaml = vscode.commands.registerCommand('extension.openapi-to-yaml', function() {
translate(true);
});
let cmdConvertJson = vscode.commands.registerCommand('extension.openapi-convert-json', function() {
convert(false, false);
});
let cmdConvertYaml = vscode.commands.registerCommand('extension.openapi-convert-yaml', function() {
convert(true, false);
});
let cmdConvertJsonResolved = vscode.commands.registerCommand('extension.openapi-convert-json-resolved', function() {
convert(false, true);
});
let cmdConvertYamlResolved = vscode.commands.registerCommand('extension.openapi-convert-yaml-resolved', function() {
convert(true, true);
});
context.subscriptions.push(cmdValidate);
context.subscriptions.push(cmdLint);
context.subscriptions.push(cmdValidateResolved);
context.subscriptions.push(cmdLintResolved);
context.subscriptions.push(cmdBundle);
context.subscriptions.push(cmdConvertJson);
context.subscriptions.push(cmdConvertYaml);
context.subscriptions.push(cmdConvertJsonResolved);
context.subscriptions.push(cmdConvertYamlResolved);
context.subscriptions.push(cmdTranslateToJson);
context.subscriptions.push(cmdTranslateToYaml);
console.log('Extension "openapi-lint" activated. '+context.subscriptions.length);
context.subscriptions.push(vscode.workspace.onDidSaveTextDocument(function(document){
return optionallyValidateOnSave(document);
}));
console.log('openapi-lint: Installed save handler');
// you can return an API from your extension for use by other extensions
// or tests etc
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {
}
exports.deactivate = deactivate;