Skip to content

Commit eda953a

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

File tree

6 files changed

+59
-260
lines changed

6 files changed

+59
-260
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 ();

sample/definitions/function/static_error_disabled.pgsql.json

Lines changed: 5 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,4 @@
11
[
2-
{
3-
"RawStmt": {
4-
"stmt": {
5-
"DropStmt": {
6-
"objects": [
7-
{
8-
"List": {
9-
"items": [
10-
{
11-
"String": {
12-
"str": "users_1"
13-
}
14-
}
15-
]
16-
}
17-
}
18-
],
19-
"removeType": "OBJECT_TABLE",
20-
"behavior": "DROP_CASCADE",
21-
"missing_ok": true
22-
}
23-
},
24-
"stmt_len": 36
25-
}
26-
},
272
{
283
"RawStmt": {
294
"stmt": {
@@ -46,126 +21,7 @@
4621
"missing_ok": true
4722
}
4823
},
49-
"stmt_len": 37
50-
}
51-
},
52-
{
53-
"RawStmt": {
54-
"stmt": {
55-
"DropStmt": {
56-
"objects": [
57-
{
58-
"List": {
59-
"items": [
60-
{
61-
"String": {
62-
"str": "users_3"
63-
}
64-
}
65-
]
66-
}
67-
}
68-
],
69-
"removeType": "OBJECT_TABLE",
70-
"behavior": "DROP_CASCADE",
71-
"missing_ok": true
72-
}
73-
},
74-
"stmt_len": 37
75-
}
76-
},
77-
{
78-
"RawStmt": {
79-
"stmt": {
80-
"CreateStmt": {
81-
"relation": {
82-
"relname": "users_1",
83-
"inh": true,
84-
"relpersistence": "p"
85-
},
86-
"tableElts": [
87-
{
88-
"ColumnDef": {
89-
"colname": "id",
90-
"typeName": {
91-
"names": [
92-
{
93-
"String": {
94-
"str": "pg_catalog"
95-
}
96-
},
97-
{
98-
"String": {
99-
"str": "int4"
100-
}
101-
}
102-
],
103-
"typemod": -1
104-
},
105-
"is_local": true,
106-
"constraints": [
107-
{
108-
"Constraint": {
109-
"contype": "CONSTR_NOTNULL"
110-
}
111-
},
112-
{
113-
"Constraint": {
114-
"contype": "CONSTR_PRIMARY"
115-
}
116-
}
117-
]
118-
}
119-
},
120-
{
121-
"ColumnDef": {
122-
"colname": "updated_at",
123-
"typeName": {
124-
"names": [
125-
{
126-
"String": {
127-
"str": "pg_catalog"
128-
}
129-
},
130-
{
131-
"String": {
132-
"str": "timestamptz"
133-
}
134-
}
135-
],
136-
"typemod": -1
137-
},
138-
"is_local": true,
139-
"constraints": [
140-
{
141-
"Constraint": {
142-
"contype": "CONSTR_NOTNULL"
143-
}
144-
},
145-
{
146-
"Constraint": {
147-
"contype": "CONSTR_DEFAULT",
148-
"raw_expr": {
149-
"FuncCall": {
150-
"funcname": [
151-
{
152-
"String": {
153-
"str": "now"
154-
}
155-
}
156-
]
157-
}
158-
}
159-
}
160-
}
161-
]
162-
}
163-
}
164-
],
165-
"oncommit": "ONCOMMIT_NOOP"
166-
}
167-
},
168-
"stmt_len": 122
24+
"stmt_len": 36
16925
}
17026
},
17127
{
@@ -215,57 +71,7 @@
21571
"oncommit": "ONCOMMIT_NOOP"
21672
}
21773
},
218-
"stmt_len": 59
219-
}
220-
},
221-
{
222-
"RawStmt": {
223-
"stmt": {
224-
"CreateStmt": {
225-
"relation": {
226-
"relname": "users_3",
227-
"inh": true,
228-
"relpersistence": "p"
229-
},
230-
"tableElts": [
231-
{
232-
"ColumnDef": {
233-
"colname": "id",
234-
"typeName": {
235-
"names": [
236-
{
237-
"String": {
238-
"str": "pg_catalog"
239-
}
240-
},
241-
{
242-
"String": {
243-
"str": "int4"
244-
}
245-
}
246-
],
247-
"typemod": -1
248-
},
249-
"is_local": true,
250-
"constraints": [
251-
{
252-
"Constraint": {
253-
"contype": "CONSTR_NOTNULL"
254-
}
255-
},
256-
{
257-
"Constraint": {
258-
"contype": "CONSTR_PRIMARY"
259-
}
260-
}
261-
]
262-
}
263-
}
264-
],
265-
"oncommit": "ONCOMMIT_NOOP"
266-
}
267-
},
268-
"stmt_len": 59
74+
"stmt_len": 60
26975
}
27076
},
27177
{
@@ -329,34 +135,9 @@
329135
"RawStmt": {
330136
"stmt": {
331137
"CreateTrigStmt": {
332-
"trigname": "update_users_3_modtime",
333-
"relation": {
334-
"relname": "users_3",
335-
"inh": true,
336-
"relpersistence": "p"
337-
},
338-
"funcname": [
339-
{
340-
"String": {
341-
"str": "update_updated_at_column"
342-
}
343-
}
344-
],
345-
"row": true,
346-
"timing": 2,
347-
"events": 16
348-
}
349-
},
350-
"stmt_len": 190
351-
}
352-
},
353-
{
354-
"RawStmt": {
355-
"stmt": {
356-
"CreateTrigStmt": {
357-
"trigname": "update_users_1_modtime",
138+
"trigname": "update_users_2_modtime_disabled",
358139
"relation": {
359-
"relname": "users_1",
140+
"relname": "users_2",
360141
"inh": true,
361142
"relpersistence": "p"
362143
},
@@ -372,7 +153,7 @@
372153
"events": 16
373154
}
374155
},
375-
"stmt_len": 126
156+
"stmt_len": 195
376157
}
377158
},
378159
{

sample/definitions/function/static_error_trigger_column_does_not_exist.pgsql.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
"events": 16
348348
}
349349
},
350-
"stmt_len": 190
350+
"stmt_len": 148
351351
}
352352
},
353353
{

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 () => {

0 commit comments

Comments
 (0)