Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the extension to accept warnings from the underlying linter #140

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
```bash
cd vscode-sqlfluff
npm install
vsce package
npx vsce package
```
4. Load into VS Code:
1. Open VS Code and Navigate to the Extensions Panel
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
"esbuild-base": "rimraf out && esbuild ./src/extension.ts --bundle --outfile=out/src/extension.js --external:vscode --format=cjs --keep-names --platform=node",
"esbuild": "npm run -S esbuild-base -- --sourcemap",
"esbuild-watch": "npm run -S esbuild-base -- --sourcemap --watch",
"postbuild": "copyfiles \"./test/suite/test_sql/**/*.sql\" \"./out\" && copyfiles \"./test/.sqlfluff\" \"./out\"",
"postbuild": "copyfiles \"./test/suite/test_sql/**/*.sql\" \"./out\" && copyfiles \"./test/.sqlfluff\" \"./out\" && copyfiles copyfiles \"./test/.vscode/settings.json\" \"./out\"",
"deploy": "vsce publish",
"build-vsix": "vsce package"
},
Expand Down
4 changes: 2 additions & 2 deletions src/features/helper/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ export default class Configuration {
return DiagnosticSeverity.Error;
}

public static diagnosticSeverityByRule(violation: string): number {
public static diagnosticSeverityByRule(violation: string, defaultSeverity: number | null = null): number {
const diagnosticSeverityByRule = vscode.workspace
.getConfiguration("sqlfluff.linter")
.get<DiagnosticSetting[]>("diagnosticSeverityByRule", []);
Expand All @@ -345,7 +345,7 @@ export default class Configuration {
return DiagnosticSeverity.Information;
}

return this.diagnosticSeverity();
return defaultSeverity == null ? this.diagnosticSeverity() : defaultSeverity;
}

public static lintEntireProject(): boolean {
Expand Down
5 changes: 3 additions & 2 deletions src/features/linter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
"use strict";
import * as vscode from "vscode";
import { Diagnostic, Disposable } from "vscode";
import { Diagnostic, DiagnosticSeverity, Disposable } from "vscode";

import Configuration from "./helper/configuration";
import Utilities from "./helper/utilities";
Expand Down Expand Up @@ -49,6 +49,7 @@ export default class LinterProvider implements Linter {
const end_line_no = Math.max(0, (violation.line_no ?? violation.end_line_no ?? 1) - 1);
const end_line_pos = Math.max(0, (violation.line_pos ?? violation.end_line_pos ?? 1) - 1);
const violationPosition = new vscode.Position(start_line_no, start_line_pos);
const warning: boolean = violation?.warning || false;
let range = new vscode.Range(start_line_no, start_line_pos, end_line_no, end_line_pos);

if (editor) {
Expand All @@ -58,7 +59,7 @@ export default class LinterProvider implements Linter {
}
}

const diagnosticSeverity = Configuration.diagnosticSeverityByRule(violation.code);
const diagnosticSeverity = Configuration.diagnosticSeverityByRule(violation.code, warning ? DiagnosticSeverity.Warning : null);

const diagnostic = new Diagnostic(
range,
Expand Down
1 change: 1 addition & 0 deletions src/features/providers/linter/types/violation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export default interface Violation {
end_line_pos?: number,
description: string,
code: string,
warning?: boolean;
}
1 change: 1 addition & 0 deletions test/.sqlfluff
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[sqlfluff]
dialect = postgres
warnings = AM04

[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = upper
Expand Down
2 changes: 1 addition & 1 deletion test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const main = async () => {
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, "./suite/index");

const launchArgs = [path.resolve(__dirname)]
const launchArgs = [path.resolve(__dirname)];

// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath, launchArgs });
Expand Down
18 changes: 16 additions & 2 deletions test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,23 @@ suite("Extension Test Suite", () => {

}).timeout(TIMEOUT);

const assertDiagnosticIsEqual = (actual: vscode.Diagnostic, expected: { range: any; message: any; code: any; }) => {
test("Warning SQL should have the correct diagnostics", async () => {
const documentUri = Helper.getDocumentUri("/test_sql/warning.sql");
await Helper.activate(documentUri);

const actualDiagnostics = vscode.languages.getDiagnostics(documentUri);

assert.strictEqual(actualDiagnostics.length, 1, JSON.stringify(actualDiagnostics, null, 4));
[
{ range: Helper.toRange(0, 0, 0, 6), message: "Query produces an unknown number of result columns.", code: "AM04", severity: vscode.DiagnosticSeverity.Warning },
].forEach((expectedDiagnostic, i) => {
assertDiagnosticIsEqual(actualDiagnostics[i], expectedDiagnostic);
});
}).timeout(TIMEOUT);

const assertDiagnosticIsEqual = (actual: vscode.Diagnostic, expected: { range: any; message: any; code: any; severity?: vscode.DiagnosticSeverity }) => {
assert.deepStrictEqual(actual.range, expected.range);
assert.strictEqual(actual.severity, 0);
assert.strictEqual(actual.severity, expected.severity == undefined ? 0 : expected.severity);
assert.strictEqual(actual.message, expected.message);
assert.strictEqual(actual.code, expected.code);
assert.strictEqual(actual.source, "sqlfluff");
Expand Down
1 change: 1 addition & 0 deletions test/suite/test_sql/warning.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a.* FROM a INNER JOIN b ON a.id = b.id
Loading