fix(sqlserver): diagnose DECLARE without data type in routine bodies

This commit is contained in:
zipg authored and GitHub committed 2026-09-21 11:38:04 +08:00
1 parent 2271d840d8
commit 79d6ab64e9
8 files changed
+538 -3

No files matched your search

+71 -1
View File
@@ -1,6 +1,6 @@
import { strict as assert } from "node:assert";
import { test } from "vitest";
import { buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, areSqlSemanticDiagnosticsEqual, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics, sqlSemanticDiagnosticRangesForViewport } from "../../apps/desktop/src/lib/sql/semantic/diagnostics.ts";
import { buildSqlParserErrorDiagnostic, buildSqlSemanticDiagnostics, areSqlSemanticDiagnosticsEqual, isSqlSemanticDiagnosticInputContext, shouldRunSqlSemanticDiagnostics, sqlSemanticDiagnosticRangesForViewport, sqlServerRoutineDefinitionRangesForViewport } from "../../apps/desktop/src/lib/sql/semantic/diagnostics.ts";
import { sqlReferenceAnalysisDialectFor } from "../../apps/desktop/src/lib/sql/semantic/dialect.ts";
import type { SqlReferenceAnalysis } from "../../apps/desktop/src/types/database.ts";
@@ -620,3 +620,73 @@ test("uses executable soft statement ranges for viewport diagnostics", () => {
["SELECT missing_field FROM second"],
);
});
const PROCEDURE_SQL = `ALTER PROCEDURE [COMMON].[TEST]
@BeginTime varchar(100)=null
AS
BEGIN
SET NOCOUNT ON;
DECLARE @temp_nums = 2;
END`;
test("recovers SQL Server routine batches for routine-only syntax rules", () => {
const ranges = sqlServerRoutineDefinitionRangesForViewport(PROCEDURE_SQL, [{ from: 0, to: PROCEDURE_SQL.length }]);
assert.deepEqual(
ranges.map((range) => ({ from: range.from, to: range.to })),
[{ from: 0, to: PROCEDURE_SQL.length }],
);
assert.equal(ranges[0]?.sql, PROCEDURE_SQL);
assert.deepEqual(sqlSemanticDiagnosticRangesForViewport(PROCEDURE_SQL, [{ from: 0, to: PROCEDURE_SQL.length }], "sqlserver"), []);
});
test("keeps SQL Server routine batches separated by GO, but skips leading query batches", () => {
const sql = `SELECT 1;
GO
CREATE OR ALTER PROCEDURE dbo.refresh_users AS
BEGIN
DECLARE @temp_nums = 2;
END
GO
ALTER FUNCTION [COMMON].[uf_answer]()
RETURNS int
AS
BEGIN
RETURN 42;
END`;
const ranges = sqlServerRoutineDefinitionRangesForViewport(sql, [{ from: 0, to: sql.length }]);
assert.deepEqual(
ranges.map((range) => range.sql),
[
"CREATE OR ALTER PROCEDURE dbo.refresh_users AS\nBEGIN\n DECLARE @temp_nums = 2;\nEND\n",
"ALTER FUNCTION [COMMON].[uf_answer]()\nRETURNS int\nAS\nBEGIN\n RETURN 42;\nEND",
],
);
});
test("only reports SQL Server routine batches that intersect the viewport", () => {
const sql = `CREATE PROCEDURE dbo.p_test AS
BEGIN
DECLARE @temp_nums = 2;
END
GO
SELECT missing_field FROM dbo.users;`;
const visibleFrom = sql.indexOf("SELECT missing_field");
const ranges = sqlServerRoutineDefinitionRangesForViewport(sql, [{ from: visibleFrom, to: sql.length }]);
assert.deepEqual(ranges, []);
const procedureFrom = sql.indexOf("CREATE PROCEDURE");
const visibleProcedure = sqlServerRoutineDefinitionRangesForViewport(sql, [{ from: procedureFrom, to: procedureFrom + 10 }]);
assert.equal(visibleProcedure.length, 1);
assert.equal(visibleProcedure[0]?.from, procedureFrom);
});
test("ignores SQL Server non-routine batches and empty viewports", () => {
const sql = "SELECT missing_field FROM dbo.users;\nGO\nUPDATE dbo.users SET name = 'x';";
assert.deepEqual(sqlServerRoutineDefinitionRangesForViewport(sql, [{ from: 0, to: sql.length }]), []);
assert.deepEqual(sqlServerRoutineDefinitionRangesForViewport(PROCEDURE_SQL, []), []);
});
@@ -0,0 +1,89 @@
import { expect, test } from "vitest";
import { buildSqlServerRoutineSyntaxDiagnostics, supportsSqlServerRoutineSyntaxDiagnostics, SQLSERVER_DECLARE_MISSING_DATA_TYPE_MESSAGE } from "../../apps/desktop/src/lib/sql/sqlServerRoutineSyntaxDiagnostics.ts";
function messages(sql: string, databaseType: Parameters<typeof buildSqlServerRoutineSyntaxDiagnostics>[1] = "sqlserver") {
return buildSqlServerRoutineSyntaxDiagnostics(sql, databaseType).map((diagnostic) => diagnostic.message);
}
function spanAt(sql: string, token: string, occurrence = 0) {
const upperSql = sql.toUpperCase();
const upperToken = token.toUpperCase();
let offset = -1;
let from = 0;
for (let index = 0; index <= occurrence; index += 1) {
offset = upperSql.indexOf(upperToken, from);
from = offset + token.length;
}
expect(offset).not.toBe(-1);
const lineStart = sql.lastIndexOf("\n", offset - 1) + 1;
const line = sql.slice(0, offset).split(/\r?\n/).length;
return { start_line: line, start_column: offset - lineStart + 1, end_line: line, end_column: offset - lineStart + token.length };
}
test("reports a DECLARE default value that has no data type", () => {
const sql = "ALTER PROCEDURE dbo.p_test @x INT AS\nBEGIN\n DECLARE @temp_nums = 2;\n SELECT @temp_nums AS v;\nEND";
const diagnostics = buildSqlServerRoutineSyntaxDiagnostics(sql, "sqlserver");
expect(diagnostics.map((diagnostic) => diagnostic.message)).toEqual([SQLSERVER_DECLARE_MISSING_DATA_TYPE_MESSAGE]);
expect(diagnostics[0]?.span).toEqual(spanAt(sql, "=", 0));
expect(diagnostics[0]?.severity).toBe("error");
});
test("reports every untyped declaration in a comma separated list", () => {
const sql = "DECLARE @a INT = 1, @b = 2, @c VARCHAR(5) = 'x', @d = 4;";
const diagnostics = buildSqlServerRoutineSyntaxDiagnostics(sql, "sqlserver");
expect(diagnostics.map((diagnostic) => diagnostic.span)).toEqual([spanAt(sql, "=", 1), spanAt(sql, "=", 3)]);
});
test("tracks CRLF and mixed-case declarations", () => {
const sql = "declare\r\n @first = 1,\r\n @second as = 2;";
const diagnostics = buildSqlServerRoutineSyntaxDiagnostics(sql, "sqlserver");
expect(diagnostics.map((diagnostic) => diagnostic.span)).toEqual([spanAt(sql, "=", 0), spanAt(sql, "=", 1)]);
});
test.each([
"DECLARE @a INT = 1;",
"DECLARE @a AS INT = 1;",
"DECLARE @a DECIMAL(10,2) = 1.5, @b VARCHAR(20) = 'x,y';",
"DECLARE @a INT, @b VARCHAR(5) = 'z';",
"DECLARE @a INT;",
"DECLARE @a dbo.MyType = 1;",
"DECLARE @t TABLE (id INT, name VARCHAR(10) DEFAULT 'a,b');",
"DECLARE @t TABLE (id INT, CONSTRAINT pk PRIMARY KEY (id));",
"DECLARE @c CURSOR;",
"DECLARE cur CURSOR FOR SELECT id, name FROM dbo.t;",
"DECLARE @c CURSOR FOR SELECT a, b FROM dbo.t;",
"SELECT @a = 1; SET @b = 2; UPDATE dbo.t SET @c = 3 WHERE id = 1;",
"EXEC dbo.p @a = 1, @b = 2;",
"SELECT * FROM dbo.t WHERE a = 1, b = 2;",
"-- DECLARE @a = 1\n/* DECLARE @b = 2 */",
"PRINT 'DECLARE @a = 1';",
"EXEC sp_executesql N'DECLARE @a = 1';",
"CREATE PROCEDURE dbo.p @a INT = 1 AS BEGIN SELECT @a; END",
"CREATE FUNCTION dbo.f(@a INT = 1) RETURNS INT AS BEGIN RETURN @a; END",
"DECLARE @a INT = (SELECT TOP 1 id FROM dbo.t);",
])("accepts the valid T-SQL declaration %s", (sql) => {
expect(messages(sql, "sqlserver")).toEqual([]);
});
test("stops at the statement that follows an unterminated declaration list", () => {
const sql = "DECLARE @a INT\nSELECT @b = 1\nDECLARE @c = 2";
const diagnostics = buildSqlServerRoutineSyntaxDiagnostics(sql, "sqlserver");
expect(diagnostics.map((diagnostic) => diagnostic.span)).toEqual([spanAt(sql, "=", 1)]);
});
test("stays silent for other dialects", () => {
const sql = "DECLARE @a = 1;";
expect(messages(sql, "mysql")).toEqual([]);
expect(messages(sql, "oracle")).toEqual([]);
expect(buildSqlServerRoutineSyntaxDiagnostics(sql, undefined)).toEqual([]);
});
test("only supports SQL Server", () => {
expect(supportsSqlServerRoutineSyntaxDiagnostics("sqlserver")).toBe(true);
expect(supportsSqlServerRoutineSyntaxDiagnostics("mysql")).toBe(false);
expect(supportsSqlServerRoutineSyntaxDiagnostics(undefined)).toBe(false);
});