From bb72fce640d2b3580f1babef99d15661eaa24c19 Mon Sep 17 00:00:00 2001 From: Cameron Loos <105471409+keraion@users.noreply.github.com> Date: Wed, 13 Mar 2024 18:48:59 -0400 Subject: [PATCH 1/3] Update extension for SQLFluff 3.0.0 - Add Vertica Dialect. - Handle new outputs for line/position. - Removed `--force` from `fix`. - Enabled some basic testing. - VSCode dependency updates. --- CHANGELOG.md | 6 +++++ README.md | 2 +- package.json | 17 +++++++------- src/features/helper/configuration.ts | 2 +- src/features/helper/dbtInterface.ts | 2 +- src/features/linter.ts | 10 ++++---- .../providers/linter/types/violation.ts | 8 +++++-- test/runTest.ts | 6 +++-- test/suite/extension.test.ts | 23 ++++++++----------- test/suite/helper.ts | 4 +++- test/suite/test_sql/diagnostics.sql | 2 ++ .../test_sql/{bad.sql => unparsable.sql} | 0 12 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 test/suite/test_sql/diagnostics.sql rename test/suite/test_sql/{bad.sql => unparsable.sql} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d54bbb..5673dd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to the "sqlfluff" extension will be documented in this file. +## [3.0.0] - 2024-03-13 + +- Update for sqlfluff version 3.0.0. +- Add Vertica dialect. +- Removed `--force` from `fix` as it is the default for > 3.0.0. + ## [2.4.4] - 2023-09-12 - Switch DBT Osmosis to DBT Core Interface. Thanks to BAntonellini's [Pull Request](https://github.com/sqlfluff/vscode-sqlfluff/pull/111) diff --git a/README.md b/README.md index 14ba76e..abbbd35 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ DBT setup requires these settings to lint and format the document. ### Format file -By default you will be able use SQLFluff fix your file by formatting. Same as calling `sqlfluff fix --force ` +By default you will be able use SQLFluff fix your file by formatting. Same as calling `sqlfluff fix ` ![plugin configuration](./media/format_config.gif) diff --git a/package.json b/package.json index 9a60b8c..0b917d6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vscode-sqlfluff", "displayName": "sqlfluff", - "version": "2.4.4", + "version": "3.0.0", "description": "A linter and auto-formatter for SQLfluff, a popular linting tool for SQL and dbt.", "publisher": "dorzey", "icon": "images/icon.png", @@ -127,8 +127,7 @@ "exasol", "greenplum", "hive", - "materializ", - "e", + "materialize", "mysql", "oracle", "postgres", @@ -138,7 +137,8 @@ "sparksql", "sqlite", "teradata", - "tsql" + "tsql", + "vertica" ], "default": "", "description": "The dialect of SQL to lint." @@ -259,7 +259,7 @@ "sqlfluff.format.arguments": { "type": "array", "default": [], - "markdownDescription": "This is useful for setting extra arguments for the `sqlfluff fix` command." + "markdownDescription": "This is useful for setting extra arguments for the `sqlfluff fix` command. Include `--force` if running sqlfluff < 3.0.0." }, "sqlfluff.format.enabled": { "type": "boolean", @@ -432,7 +432,6 @@ "node-fetch": "^2.6.7" }, "devDependencies": { - "@types/glob": "^8.1.0", "@types/mocha": "^10.0.1", "@types/node": "^20.4.2", "@types/node-fetch": "^2.6.4", @@ -449,8 +448,8 @@ "glob": "^10.3.3", "mocha": "^10.2.0", "typescript": "^5.1.6", - "vsce": "^2.15.0", - "vscode-dts": "^0.3.3", - "vscode-test": "^1.6.1" + "@vscode/vsce": "^2.15.0", + "@vscode/dts": "^0.4.0", + "@vscode/test-electron": "^1.6.1" } } diff --git a/src/features/helper/configuration.ts b/src/features/helper/configuration.ts index 4fe8983..6cad68c 100644 --- a/src/features/helper/configuration.ts +++ b/src/features/helper/configuration.ts @@ -377,7 +377,7 @@ export default class Configuration { } public static formatFileArguments(): string[] { - const extraArguments = [...this.fixArguments(), "--force"]; + const extraArguments = [...this.fixArguments()]; return extraArguments; } diff --git a/src/features/helper/dbtInterface.ts b/src/features/helper/dbtInterface.ts index 8735b4c..2828ba8 100644 --- a/src/features/helper/dbtInterface.ts +++ b/src/features/helper/dbtInterface.ts @@ -74,7 +74,7 @@ export class DbtInterface { `http://${Configuration.dbtInterfaceHost()}:${Configuration.dbtInterfacePort()}/health`, { method: "GET", - signal: abortController.signal as AbortSignal + signal: abortController.signal as AbortSignal, }, ); if (response.status === 200) { diff --git a/src/features/linter.ts b/src/features/linter.ts index 840809e..6c15ef7 100644 --- a/src/features/linter.ts +++ b/src/features/linter.ts @@ -44,10 +44,12 @@ export default class LinterProvider implements Linter { filePath.violations.forEach((violation: Violation) => { const path = filePath.filepath; - const line = violation.line_no - 1 > 0 ? violation.line_no - 1 : 0; - const character = violation.line_pos - 1 > 0 ? violation.line_pos - 1 : 0; - const violationPosition = new vscode.Position(line, character); - let range = new vscode.Range(line, character, line, character); + const start_line_no = Math.max(0, (violation.line_no ?? violation.start_line_no ?? 1) - 1); + const start_line_pos = Math.max(0, (violation.line_pos ?? violation.start_line_pos ?? 1) - 1); + 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); + let range = new vscode.Range(start_line_no, start_line_pos, end_line_no, end_line_pos); if (editor) { const editorPath = Utilities.normalizePath(editor.document.fileName); diff --git a/src/features/providers/linter/types/violation.ts b/src/features/providers/linter/types/violation.ts index 9384721..c309b82 100644 --- a/src/features/providers/linter/types/violation.ts +++ b/src/features/providers/linter/types/violation.ts @@ -1,6 +1,10 @@ export default interface Violation { - line_no: number, - line_pos: number, + line_no?: number, + line_pos?: number, + start_line_no?: number, + start_line_pos?: number, + end_line_no?: number, + end_line_pos?: number, description: string, code: string, } diff --git a/test/runTest.ts b/test/runTest.ts index 62ec431..39f128b 100644 --- a/test/runTest.ts +++ b/test/runTest.ts @@ -1,5 +1,5 @@ import * as path from "path"; -import { runTests } from "vscode-test"; +import { runTests } from "@vscode/test-electron"; const main = async () => { try { @@ -11,8 +11,10 @@ const main = async () => { // Passed to --extensionTestsPath const extensionTestsPath = path.resolve(__dirname, "./suite/index"); + const launchArgs = [path.resolve(__dirname)] + // Download VS Code, unzip it and run the integration test - await runTests({ extensionDevelopmentPath, extensionTestsPath }); + await runTests({ extensionDevelopmentPath, extensionTestsPath, launchArgs }); } catch (err) { console.error("Failed to run tests"); process.exit(1); diff --git a/test/suite/extension.test.ts b/test/suite/extension.test.ts index 6800de6..f328e7d 100644 --- a/test/suite/extension.test.ts +++ b/test/suite/extension.test.ts @@ -19,18 +19,16 @@ suite("Extension Test Suite", () => { }).timeout(TIMEOUT); - // TODO: Fix Tests - /* test("Bad SQL should have the correct diagnostics", async () => { - const documentUri = Helper.getDocumentUri("/test_sql/bad.sql"); + const documentUri = Helper.getDocumentUri("/test_sql/diagnostics.sql"); await Helper.activate(documentUri); - - const actualDiagnostics = vscode.languages.getDiagnostics(documentUri); - + + const actualDiagnostics = vscode.languages.getDiagnostics(documentUri) + assert.strictEqual(actualDiagnostics.length, 2); [ - { range: Helper.toRange(1, 11, 1, 10), message: "Keywords must be consistently upper case.", code: "CP01" }, - { range: Helper.toRange(2, 1, 2, 1), message: "Files must end with a single trailing newline.", code: "LT12" }, + { range: Helper.toRange(1, 0, 1, 4), message: "Keywords must be upper case.", code: "CP01" }, + { range: Helper.toRange(1, 5, 1, 12), message: "Files must end with a single trailing newline.", code: "LT12" }, ].forEach((expectedDiagnostic, i) => { assertDiagnosticIsEqual(actualDiagnostics[i], expectedDiagnostic); }); @@ -38,18 +36,17 @@ suite("Extension Test Suite", () => { test("Bad SQL has zero diagnostics after document format", async () => { const documentUri = Helper.getDocumentUri("/test_sql/format.sql"); - // const document = await Helper.activate(documentUri); - // const preFormatDiagnostics = vscode.languages.getDiagnostics(documentUri); - // assert.strictEqual(preFormatDiagnostics.length, 1, "Pre-format diagnostics not expected length"); + const document = await Helper.activate(documentUri); + const preFormatDiagnostics = vscode.languages.getDiagnostics(documentUri); + assert.strictEqual(preFormatDiagnostics.length, 1, "Pre-format diagnostics not expected length"); await Helper.format(documentUri); - // await Helper.activate(documentUri); + await Helper.activate(documentUri); const postFormatDiagnostics = vscode.languages.getDiagnostics(documentUri); assert.strictEqual(postFormatDiagnostics.length, 0, "Post-format diagnostics not expected length"); }).timeout(TIMEOUT); - */ const assertDiagnosticIsEqual = (actual: vscode.Diagnostic, expected: { range: any; message: any; code: any; }) => { assert.deepStrictEqual(actual.range, expected.range); diff --git a/test/suite/helper.ts b/test/suite/helper.ts index 047e897..9a9fde1 100644 --- a/test/suite/helper.ts +++ b/test/suite/helper.ts @@ -1,10 +1,12 @@ +import * as assert from "assert"; import * as vscode from "vscode"; export const SLEEP_TIME = 10000; export const activate = async (documentUri: vscode.Uri): Promise => { // The extensionId is `publisher.name` from package.json - const extension = vscode.extensions.getExtension("vscode-sqlfluff"); + const extension = vscode.extensions.getExtension("dorzey.vscode-sqlfluff"); + assert.notStrictEqual(extension, undefined); await extension?.activate(); try { await vscode.commands.executeCommand("workbench.action.closeActiveEditor"); diff --git a/test/suite/test_sql/diagnostics.sql b/test/suite/test_sql/diagnostics.sql new file mode 100644 index 0000000..814ff2f --- /dev/null +++ b/test/suite/test_sql/diagnostics.sql @@ -0,0 +1,2 @@ +SELECT a +from a_table; \ No newline at end of file diff --git a/test/suite/test_sql/bad.sql b/test/suite/test_sql/unparsable.sql similarity index 100% rename from test/suite/test_sql/bad.sql rename to test/suite/test_sql/unparsable.sql From 2c1c971fcce1a367f1bcea1f6f97a0c71de9fbff Mon Sep 17 00:00:00 2001 From: Cameron Loos <105471409+keraion@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:00:36 -0400 Subject: [PATCH 2/3] Fixed typos and linting inconsistencies --- CHANGELOG.md | 2 +- test/suite/extension.test.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5673dd1..688a888 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to the "sqlfluff" extension will be documented in this file. - Update for sqlfluff version 3.0.0. - Add Vertica dialect. -- Removed `--force` from `fix` as it is the default for > 3.0.0. +- Removed `--force` from `fix` as it is the default for >= 3.0.0. ## [2.4.4] - 2023-09-12 diff --git a/test/suite/extension.test.ts b/test/suite/extension.test.ts index f328e7d..a8ed696 100644 --- a/test/suite/extension.test.ts +++ b/test/suite/extension.test.ts @@ -22,9 +22,9 @@ suite("Extension Test Suite", () => { test("Bad SQL should have the correct diagnostics", async () => { const documentUri = Helper.getDocumentUri("/test_sql/diagnostics.sql"); await Helper.activate(documentUri); - - const actualDiagnostics = vscode.languages.getDiagnostics(documentUri) - + + const actualDiagnostics = vscode.languages.getDiagnostics(documentUri); + assert.strictEqual(actualDiagnostics.length, 2); [ { range: Helper.toRange(1, 0, 1, 4), message: "Keywords must be upper case.", code: "CP01" }, From 5408b3df4fdd5822eb28fb2b46c223b94553f82c Mon Sep 17 00:00:00 2001 From: Cameron Loos <105471409+keraion@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:17:36 -0400 Subject: [PATCH 3/3] Update CI to supported versions. --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 532464d..7a545b7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,12 +21,12 @@ jobs: - name: Install Node.js uses: actions/setup-node@v1 with: - node-version: 16.x + node-version: 20.x - run: npm install - name: Set up Python uses: actions/setup-python@v2 with: - python-version: 3.7 + python-version: 3.8 - name: Install sqlfluff run: pip install sqlfluff - name: Run tests