|
1 | | -import { Text } from "@codemirror/state"; |
| 1 | +import { EditorState, Text } from "@codemirror/state"; |
2 | 2 | import type { EditorView } from "@codemirror/view"; |
3 | 3 | import { describe, expect, it, vi } from "vitest"; |
4 | | -import { sqlLinter } from "../diagnostics.js"; |
| 4 | +import { exportedForTesting, type SqlLinterConfig, sqlLinter } from "../diagnostics.js"; |
| 5 | +import { NodeSqlParser } from "../parser.js"; |
5 | 6 | import type { SqlParser } from "../types.js"; |
6 | 7 |
|
7 | | -// Mock EditorView |
8 | | -const _createMockView = (content: string) => { |
9 | | - const doc = Text.of(content.split("\n")); |
| 8 | +const { createLintSource } = exportedForTesting; |
| 9 | + |
| 10 | +const createMockView = (content: string) => { |
10 | 11 | return { |
11 | | - state: { doc }, |
| 12 | + state: EditorState.create({ doc: content }), |
12 | 13 | } as EditorView; |
13 | 14 | }; |
14 | 15 |
|
| 16 | +const lint = (content: string, config: SqlLinterConfig = {}) => { |
| 17 | + return createLintSource(config)(createMockView(content)); |
| 18 | +}; |
| 19 | + |
15 | 20 | describe("sqlLinter", () => { |
16 | 21 | it("should create a linter extension", () => { |
17 | 22 | const linter = sqlLinter(); |
@@ -48,3 +53,123 @@ describe("sqlLinter", () => { |
48 | 53 | expect(linter).toBeDefined(); |
49 | 54 | }); |
50 | 55 | }); |
| 56 | + |
| 57 | +describe("lint source", () => { |
| 58 | + it("should return no diagnostics for an empty document", async () => { |
| 59 | + expect(await lint("")).toEqual([]); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should return no diagnostics for a whitespace-only document", async () => { |
| 63 | + expect(await lint(" \n\n \t ")).toEqual([]); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should return no diagnostics for valid statements", async () => { |
| 67 | + const diagnostics = await lint( |
| 68 | + "SELECT * FROM users;\nINSERT INTO users (name) VALUES ('John');", |
| 69 | + ); |
| 70 | + expect(diagnostics).toEqual([]); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should report one diagnostic per broken statement", async () => { |
| 74 | + const doc = "SELCT * FROM users;\nSELECT * FORM users;\nDELETE FROMM users;"; |
| 75 | + const diagnostics = await lint(doc); |
| 76 | + |
| 77 | + expect(diagnostics).toHaveLength(3); |
| 78 | + |
| 79 | + const text = Text.of(doc.split("\n")); |
| 80 | + expect(text.lineAt(diagnostics[0].from).number).toBe(1); |
| 81 | + expect(text.lineAt(diagnostics[1].from).number).toBe(2); |
| 82 | + expect(text.lineAt(diagnostics[2].from).number).toBe(3); |
| 83 | + for (const diagnostic of diagnostics) { |
| 84 | + expect(diagnostic.severity).toBe("error"); |
| 85 | + expect(diagnostic.source).toBe("sql-parser"); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it("should position diagnostics in a broken statement that follows a valid one", async () => { |
| 90 | + const doc = "SELECT * FROM users;\nSELCT * FROM orders;"; |
| 91 | + const diagnostics = await lint(doc); |
| 92 | + |
| 93 | + expect(diagnostics).toHaveLength(1); |
| 94 | + const secondStatementStart = doc.indexOf("SELCT"); |
| 95 | + expect(diagnostics[0].from).toBeGreaterThanOrEqual(secondStatementStart); |
| 96 | + expect(diagnostics[0].to).toBeLessThanOrEqual(doc.length); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should not split statements on semicolons inside string literals", async () => { |
| 100 | + const doc = "SELECT 'a; b' FROM users;\nSELCT * FROM orders;"; |
| 101 | + const diagnostics = await lint(doc); |
| 102 | + |
| 103 | + expect(diagnostics).toHaveLength(1); |
| 104 | + expect(diagnostics[0].from).toBeGreaterThanOrEqual(doc.indexOf("SELCT")); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should not split statements on semicolons inside comments", async () => { |
| 108 | + const doc = "SELECT 1 -- trailing; comment\n;\nSELCT * FROM orders;"; |
| 109 | + const diagnostics = await lint(doc); |
| 110 | + |
| 111 | + expect(diagnostics).toHaveLength(1); |
| 112 | + expect(diagnostics[0].from).toBeGreaterThanOrEqual(doc.indexOf("SELCT")); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should widen the diagnostic span to cover the offending token", async () => { |
| 116 | + // node-sql-parser reports this error at the "users" token |
| 117 | + const doc = "SELECT 1 FROMM users;"; |
| 118 | + const diagnostics = await lint(doc); |
| 119 | + |
| 120 | + expect(diagnostics).toHaveLength(1); |
| 121 | + const { from, to } = diagnostics[0]; |
| 122 | + // The span should cover the whole token, not a single character |
| 123 | + expect(doc.slice(from, to)).toBe("users"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should fall back to a one-character span when the error is not at a token", async () => { |
| 127 | + // node-sql-parser reports this error at the ";" character |
| 128 | + const doc = "SELECT * FROM users WHERE;"; |
| 129 | + const diagnostics = await lint(doc); |
| 130 | + |
| 131 | + expect(diagnostics).toHaveLength(1); |
| 132 | + expect(diagnostics[0].to).toBe(diagnostics[0].from + 1); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should handle statements separated on the same line", async () => { |
| 136 | + const doc = "SELECT 1; SELCT 2;"; |
| 137 | + const diagnostics = await lint(doc); |
| 138 | + |
| 139 | + expect(diagnostics).toHaveLength(1); |
| 140 | + expect(diagnostics[0].from).toBeGreaterThanOrEqual(doc.indexOf("SELCT")); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should report a single diagnostic when perStatement is disabled", async () => { |
| 144 | + const doc = "SELCT * FROM users;\nSELCT * FROM orders;"; |
| 145 | + const diagnostics = await lint(doc, { perStatement: false }); |
| 146 | + |
| 147 | + expect(diagnostics).toHaveLength(1); |
| 148 | + const text = Text.of(doc.split("\n")); |
| 149 | + expect(text.lineAt(diagnostics[0].from).number).toBe(1); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should keep diagnostics within statement bounds", async () => { |
| 153 | + const doc = "SELECT * FROM users;\nSELECT FROM WHERE;\nSELECT 2;"; |
| 154 | + const diagnostics = await lint(doc); |
| 155 | + |
| 156 | + expect(diagnostics.length).toBeGreaterThanOrEqual(1); |
| 157 | + const stmtFrom = doc.indexOf("SELECT FROM"); |
| 158 | + const stmtTo = doc.indexOf("SELECT 2"); |
| 159 | + for (const diagnostic of diagnostics) { |
| 160 | + expect(diagnostic.from).toBeGreaterThanOrEqual(stmtFrom); |
| 161 | + expect(diagnostic.to).toBeLessThanOrEqual(stmtTo); |
| 162 | + expect(diagnostic.to).toBeGreaterThanOrEqual(diagnostic.from); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + it("should reuse errors from a provided structure analyzer without re-validating", async () => { |
| 167 | + const parser = new NodeSqlParser(); |
| 168 | + const validateSpy = vi.spyOn(parser, "validateSql"); |
| 169 | + |
| 170 | + const diagnostics = await lint("SELCT 1;\nSELCT 2;", { parser }); |
| 171 | + |
| 172 | + expect(diagnostics).toHaveLength(2); |
| 173 | + expect(validateSpy).not.toHaveBeenCalled(); |
| 174 | + }); |
| 175 | +}); |
0 commit comments