Skip to content

Commit ae9094a

Browse files
committed
comment per statement to disable static validation
1 parent 3895ee7 commit ae9094a

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

sample/definitions/function/static_error_disabled.pgsql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ begin
1414
end;
1515
$function$;
1616

17-
-- TODO some kind of flag for disabling static analysis only per statement
1817
-- plpgsql-language-server:disable-static
18+
create trigger update_users_2_modtime_disabled -- error silenced
19+
before update on users_2 for each row
20+
execute function update_updated_at_column ();
21+
1922
create trigger update_users_2_modtime -- should raise error
2023
before update on users_2 for each row
2124
execute function update_updated_at_column ();

server/src/postgres/queries/queryFileStaticAnalysis.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "@/postgres/parameters"
99
import { FunctionInfo, TriggerInfo } from "@/postgres/parsers/parseFunctions"
1010
import { Settings } from "@/settings"
11+
import { DISABLE_STATIC_VALIDATION_RE } from "@/utilities/regex"
1112
import {
1213
getLineRangeFromBuffer,
1314
getRangeFromBuffer, getTextAllRange,
@@ -162,40 +163,44 @@ export async function queryFileStaticAnalysis(
162163
location: number | undefined,
163164
stmtLen?: number,
164165
) {
165-
rows.forEach(
166-
(row) => {
167-
const range = (() => {
168-
if (location === undefined) {
169-
return getTextAllRange(document)
170-
}
171-
if (stmtLen) {
172-
return getRangeFromBuffer(
173-
fileText,
174-
location + 1,
175-
location + 1 + stmtLen,
176-
)
166+
rows.forEach((row) => {
167+
const range = (() => {
168+
if (location === undefined) {
169+
return getTextAllRange(document)
170+
}
171+
if (stmtLen) {
172+
const stmt = fileText.slice(location + 1, location + 1 + stmtLen)
173+
if (DISABLE_STATIC_VALIDATION_RE
174+
.test(stmt)) {
175+
return
177176
}
178177

179-
const lineRange = getLineRangeFromBuffer(
178+
return getRangeFromBuffer(
180179
fileText,
181-
location,
182-
row.lineno ? row.lineno - 1 : 0,
180+
location + 1,
181+
location + 1 + stmtLen,
183182
)
183+
}
184+
const lineRange = getLineRangeFromBuffer(
185+
fileText,
186+
location,
187+
row.lineno ? row.lineno - 1 : 0,
188+
)
189+
190+
if (!lineRange) {
191+
return getTextAllRange(document)
192+
}
193+
194+
return lineRange
195+
})()
196+
197+
if (!range) {
198+
return
199+
}
184200

185-
if (!lineRange) {
186-
return getTextAllRange(document)
187-
}
188-
189-
return lineRange
190-
})()
191-
192-
errors.push({
193-
level: row.level, range, message: row.message,
194-
})
195-
196-
}
197-
,
198-
)
199-
201+
errors.push({
202+
level: row.level, range, message: row.message,
203+
})
204+
})
200205
}
201206
}

server/src/services/validation.test.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,21 @@ describe("Validate Tests", () => {
9090
])
9191
})
9292

93-
// TODO
94-
it.skip("static analysis disabled on invalid statement", async () => {
93+
it("static analysis disabled on invalid statement", async () => {
9594
const diagnostics = await validateSampleFile(
9695
"definitions/function/static_error_disabled.pgsql",
9796
)
9897

99-
expect(diagnostics).toStrictEqual([])
98+
if (!diagnostics) {
99+
throw new Error("")
100+
}
101+
if (diagnostics?.length === 0) {
102+
throw new Error("")
103+
}
104+
105+
expect(diagnostics).toHaveLength(1)
106+
expect(diagnostics[0].message)
107+
.toContain("record \"new\" has no field \"updated_at\"")
100108
})
101109

102110
it("FUNCTION column does not exists", async () => {

server/src/utilities/regex.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable max-len */
2+
13
export function escapeRegex(string: string): string {
24
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")
35
}
@@ -8,5 +10,5 @@ export const BEGIN_RE = /^([\s]*begin[\s]*;)/igm
810
export const COMMIT_RE = /^([\s]*commit[\s]*;)/igm
911
export const ROLLBACK_RE = /^([\s]*rollback[\s]*;)/igm
1012

11-
// eslint-disable-next-line max-len
1213
export const DISABLE_STATEMENT_VALIDATION_RE = /^ *-- +plpgsql-language-server:disable *$/m
14+
export const DISABLE_STATIC_VALIDATION_RE = /^ *-- +plpgsql-language-server:disable-static *$/m

0 commit comments

Comments
 (0)