From 6047dabb5e0c82bd8bcb22ed0a892e2e78679caf Mon Sep 17 00:00:00 2001 From: Mike Ralphson Date: Tue, 14 Apr 2020 16:32:25 +0100 Subject: [PATCH] Add onSaveAction setting, refs #20 --- CHANGELOG.md | 8 ++++++-- README.md | 2 +- extension.js | 21 +++++++++++++++++++-- package.json | 21 +++++++++++++++++++-- 4 files changed, 45 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c904d0..30d33dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,13 +3,17 @@ All notable changes to the "openapi-lint" extension will be documented in this f Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [1.3.0] +- Add configurable `onSaveAction` setting +- Update `oas-kit` to latest versions + ## [1.2.0] - Add AsyncAPI v2.0.x support -- Lint-on-save functionality +- Validate-on-save functionality ## [1.1.0] - Integrate errors/warnings with the 'Problems' (diagnostics) pane -- Accurate line/column positions will be coming in version 1.3 or 2.0 +- Accurate line/column positions will be coming in version 1.4 or 2.0 ## [1.0.0] - Update `oas-kit` to latest versions diff --git a/README.md b/README.md index c2b68a2..06a3e18 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ The extension should work as-is ## Extension Settings -No configuration is currently possible or needed. +The `onSaveAction` can be set to one of `none`, `validate` (the default), `resolveAndValidate`, `lint` or `resolveAndLint`. ## Known Issues diff --git a/extension.js b/extension.js index 3a2867b..f386619 100644 --- a/extension.js +++ b/extension.js @@ -169,12 +169,21 @@ function validate(lint, resolve) { } 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 = {}; - validator.validateSync(obj, 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; @@ -185,7 +194,15 @@ function optionallyValidateOnSave(document) { let range; // TODO diagnostics.push(new vscode.Diagnostic(range, ex.message, vscode.DiagnosticSeverity.Error)); for (let warning of options.warnings||[]) { - diagnostics.push(new vscode.Diagnostic(range, warning.message + ' ' + warning.ruleName, vscode.DiagnosticSeverity.Warning)); + 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; diff --git a/package.json b/package.json index d94f7a5..face5fb 100644 --- a/package.json +++ b/package.json @@ -158,11 +158,28 @@ "fileMatch": "*oas2.json", "url": "./schemas/openapi-2.0.json" } - ] + ], + "configuration": { + "title": "OpenAPI-Lint", + "properties": { + "openapi-lint.onSaveAction": { + "type": "string", + "description": "Action to take on saving recognised files.", + "default": "validate", + "enum": [ + "none", + "validate", + "resolveAndValidate", + "lint", + "resolveAndLint" + ] + } + } + } }, "icon": "images/icon.png", "scripts": { - "xostinstall": "node ./node_modules/vscode/bin/install", + "old-postinstall": "node ./node_modules/vscode/bin/install", "test": "node ./node_modules/vscode/bin/test" }, "devDependencies": {