Skip to content

Commit

Permalink
v0.1.5 : Allow specific linter severity settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertOstermann committed Oct 22, 2022
1 parent aeefbb8 commit 3459e0d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ All notable changes to the "sqlfluff" extension will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [0.1.5] - 2022-10-22

- Add `linter.severity` configuration option.

## [0.1.4] - 2022-10-11

- Add `format.arguments` and `linter.arguments`.
- Add `format.arguments` and `linter.arguments` configuration options..

## [0.1.3] - 2022-10-02

Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,25 @@ If you want to manually update the `settings.json` file, open the VS Code comman
"sqlfluff.dialect": "mysql",
"sqlfluff.excludeRules": ["L009"],
"sqlfluff.executablePath": "sqlfluff",
"sqlfluff.format.arguments": ["--FIX-EVEN-UNPARSABLE"],
"sqlfluff.format.enabled": true,
"sqlfluff.format.workingDirectory": "",
"sqlfluff.ignoreLocalConfig": false,
"sqlfluff.ignoreParsing": false,
"sqlfluff.rules": [],
"sqlfluff.suppressNotifications": false,
"sqlfluff.workingDirectory": "",
/* Linter */
"sqlfluff.linter.arguments": [],
"sqlfluff.linter.run": "onType",
"sqlfluff.rules": [],
"sqlfluff.linter.diagnosticSeverity": "error",
"sqlfluff.linter.diagnosticSeverityByRule": [
{
"rule": "L010",
"severity": "warning"
}
],
/* Formatter */
"sqlfluff.format.arguments": ["--FIX-EVEN-UNPARSABLE"],
"sqlfluff.format.enabled": true,

```

## DBT Configuration
Expand Down
32 changes: 31 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vscode-sqlfluff",
"displayName": "sqlfluff",
"version": "0.1.4",
"version": "0.1.5",
"description": "A linter and auto-formatter for SQLfluff, a popular linting tool for SQL and dbt.",
"publisher": "dorzey",
"icon": "images/icon.png",
Expand Down Expand Up @@ -122,6 +122,36 @@
"default": [],
"markdownDescription": "This is useful for setting extra arguments for the `sqlfluff lint` command."
},
"sqlfluff.linter.diagnosticSeverity": {
"type": ["string"],
"enum": ["error", "warning", "information", "hint"],
"default": "error",
"markdownDescription": "Set the default diagnostic severity of the linting results."
},
"sqlfluff.linter.diagnosticSeverityByRule": {
"type": "array",
"default": [],
"items": {
"type": "object",
"additionalProperties": false,
"required": [
"rule",
"severity"
],
"properties": {
"rule": {
"type": "string",
"markdownDescription": "The specific rule for the diagnostic severity."
},
"severity": {
"type": "string",
"enum": ["error", "warning", "information", "hint"],
"markdownDescription": "Set the diagnostic severity for the given rule."
}
}
},
"markdownDescription": "Set the diagnostic severity of individual linting results."
},
"sqlfluff.linter.run": {
"type": "string",
"enum": [
Expand Down
56 changes: 52 additions & 4 deletions src/features/helper/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as path from "path";
import * as vscode from "vscode";
import { DiagnosticSeverity } from "vscode";

import { RunTrigger } from "../providers/lint";
import { DiagnosticSetting } from "./types/diagnosticSetting";
import Variables from "./types/variables";
import { normalize, Utilities } from "./utilities";

Expand Down Expand Up @@ -139,18 +141,64 @@ export class Configuration {
.get<string>("run", RunTrigger.onType);
}

public static diagnosticSeverity(): number {
const diagnosticSeverity = vscode.workspace
.getConfiguration("sqlfluff.linter")
.get<string>("diagnosticSeverity", "error");

switch (diagnosticSeverity) {
case "error":
return DiagnosticSeverity.Error;
case "warning":
return DiagnosticSeverity.Warning;
case "hint":
return DiagnosticSeverity.Hint;
case "information":
return DiagnosticSeverity.Information;
}

return DiagnosticSeverity.Error;
}

public static diagnosticSeverityByRule(violation: string): number {
const diagnosticSeverityByRule = vscode.workspace
.getConfiguration("sqlfluff.linter")
.get<DiagnosticSetting[]>("diagnosticSeverityByRule", []);

let diagnosticSeverity = undefined;
for (const diagnosticSetting of diagnosticSeverityByRule) {
const rule = diagnosticSetting?.rule ?? undefined;
const severity = diagnosticSetting?.severity ?? undefined;
if (violation === rule) {
diagnosticSeverity = severity;
break;
}
}

switch (diagnosticSeverity) {
case "error":
return DiagnosticSeverity.Error;
case "warning":
return DiagnosticSeverity.Warning;
case "hint":
return DiagnosticSeverity.Hint;
case "information":
return DiagnosticSeverity.Information;
}

return this.diagnosticSeverity();
}

/* Arguments */

public static lintFileArguments(): string[] {
let extraArguments = ["--format", "json"];
extraArguments = extraArguments.concat(this.lintArguments());
const extraArguments = [...this.lintArguments(), "--format", "json"]

return extraArguments;
}

public static formatFileArguments(): string[] {
let extraArguments = ["--force"];
extraArguments = extraArguments.concat(this.fixArguments());
const extraArguments = [...this.fixArguments(), "--force"];

return extraArguments;
}
Expand Down
4 changes: 4 additions & 0 deletions src/features/helper/types/diagnosticSetting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface DiagnosticSetting {
rule: string
severity: string
}
5 changes: 4 additions & 1 deletion src/features/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as vscode from "vscode";
import { Diagnostic, DiagnosticSeverity, Disposable, Range } from "vscode";

import { Configuration } from "./helper/configuration";
import { normalize } from "./helper/utilities";
import { FormattingProvider } from "./providers/format";
import { Linter, LintingProvider } from "./providers/lint";
Expand Down Expand Up @@ -49,10 +50,12 @@ export class LinterProvider implements Linter {
range = vscode.window.activeTextEditor.document.getWordRangeAtPosition(violationPosition) || range;
}

const diagnosticSeverity = Configuration.diagnosticSeverityByRule(violation.code);

const diagnostic = new Diagnostic(
range,
violation.description,
DiagnosticSeverity.Error,
diagnosticSeverity,
);
diagnostic.code = violation.code;
diagnostic.source = "sqlfluff";
Expand Down

0 comments on commit 3459e0d

Please sign in to comment.