Skip to content

Commit

Permalink
Merge pull request #127 from keraion/sqlfluff_3_0_0_update
Browse files Browse the repository at this point in the history
Update extension for SQLFluff 3.0.0
  • Loading branch information
RobertOstermann authored Mar 17, 2024
2 parents ce1ae6b + 5408b3d commit 535e785
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path>`
By default you will be able use SQLFluff fix your file by formatting. Same as calling `sqlfluff fix <path>`

![plugin configuration](./media/format_config.gif)

Expand Down
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -127,8 +127,7 @@
"exasol",
"greenplum",
"hive",
"materializ",
"e",
"materialize",
"mysql",
"oracle",
"postgres",
Expand All @@ -138,7 +137,8 @@
"sparksql",
"sqlite",
"teradata",
"tsql"
"tsql",
"vertica"
],
"default": "",
"description": "The dialect of SQL to lint."
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
}
}
2 changes: 1 addition & 1 deletion src/features/helper/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ export default class Configuration {
}

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

return extraArguments;
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/helper/dbtInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 6 additions & 4 deletions src/features/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/features/providers/linter/types/violation.ts
Original file line number Diff line number Diff line change
@@ -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,
}
6 changes: 4 additions & 2 deletions test/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from "path";
import { runTests } from "vscode-test";
import { runTests } from "@vscode/test-electron";

const main = async () => {
try {
Expand All @@ -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);
Expand Down
17 changes: 7 additions & 10 deletions test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,34 @@ 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);

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);
});
}).timeout(TIMEOUT);

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);
Expand Down
4 changes: 3 additions & 1 deletion test/suite/helper.ts
Original file line number Diff line number Diff line change
@@ -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<vscode.TextDocument | undefined> => {
// 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");
Expand Down
2 changes: 2 additions & 0 deletions test/suite/test_sql/diagnostics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT a
from a_table;
File renamed without changes.

0 comments on commit 535e785

Please sign in to comment.