Skip to content

Commit

Permalink
comment per statement to disable static validation
Browse files Browse the repository at this point in the history
  • Loading branch information
danicc097 committed Dec 1, 2022
1 parent 3895ee7 commit 7052778
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 441 deletions.
403 changes: 0 additions & 403 deletions sample/definitions/function/static_error_disabled.pgsql.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ begin
end;
$function$;

-- TODO some kind of flag for disabling static analysis only per statement
-- plpgsql-language-server:disable-static
create trigger update_users_2_modtime_disabled -- error silenced
before update on users_2 for each row
execute function update_updated_at_column ();

create trigger update_users_2_modtime -- should raise error
before update on users_2 for each row
execute function update_updated_at_column ();
184 changes: 184 additions & 0 deletions sample/definitions/trigger/static_error_disabled.pgsql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
[
{
"RawStmt": {
"stmt": {
"DropStmt": {
"objects": [
{
"List": {
"items": [
{
"String": {
"str": "users_2"
}
}
]
}
}
],
"removeType": "OBJECT_TABLE",
"behavior": "DROP_CASCADE",
"missing_ok": true
}
},
"stmt_len": 36
}
},
{
"RawStmt": {
"stmt": {
"CreateStmt": {
"relation": {
"relname": "users_2",
"inh": true,
"relpersistence": "p"
},
"tableElts": [
{
"ColumnDef": {
"colname": "id",
"typeName": {
"names": [
{
"String": {
"str": "pg_catalog"
}
},
{
"String": {
"str": "int4"
}
}
],
"typemod": -1
},
"is_local": true,
"constraints": [
{
"Constraint": {
"contype": "CONSTR_NOTNULL"
}
},
{
"Constraint": {
"contype": "CONSTR_PRIMARY"
}
}
]
}
}
],
"oncommit": "ONCOMMIT_NOOP"
}
},
"stmt_len": 60
}
},
{
"RawStmt": {
"stmt": {
"CreateFunctionStmt": {
"replace": true,
"funcname": [
{
"String": {
"str": "update_updated_at_column"
}
}
],
"returnType": {
"names": [
{
"String": {
"str": "trigger"
}
}
],
"typemod": -1
},
"options": [
{
"DefElem": {
"defname": "language",
"arg": {
"String": {
"str": "plpgsql"
}
},
"defaction": "DEFELEM_UNSPEC"
}
},
{
"DefElem": {
"defname": "as",
"arg": {
"List": {
"items": [
{
"String": {
"str": "\nbegin\n new.updated_at = NOW();\n return new;\nend;\n"
}
}
]
}
},
"defaction": "DEFELEM_UNSPEC"
}
}
]
}
},
"stmt_len": 171
}
},
{
"RawStmt": {
"stmt": {
"CreateTrigStmt": {
"trigname": "update_users_2_modtime_disabled",
"relation": {
"relname": "users_2",
"inh": true,
"relpersistence": "p"
},
"funcname": [
{
"String": {
"str": "update_updated_at_column"
}
}
],
"row": true,
"timing": 2,
"events": 16
}
},
"stmt_len": 195
}
},
{
"RawStmt": {
"stmt": {
"CreateTrigStmt": {
"trigname": "update_users_2_modtime",
"relation": {
"relname": "users_2",
"inh": true,
"relpersistence": "p"
},
"funcname": [
{
"String": {
"str": "update_updated_at_column"
}
}
],
"row": true,
"timing": 2,
"events": 16
}
},
"stmt_len": 148
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@
"events": 16
}
},
"stmt_len": 190
"stmt_len": 148
}
},
{
Expand Down
65 changes: 35 additions & 30 deletions server/src/postgres/queries/queryFileStaticAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "@/postgres/parameters"
import { FunctionInfo, TriggerInfo } from "@/postgres/parsers/parseFunctions"
import { Settings } from "@/settings"
import { DISABLE_STATIC_VALIDATION_RE } from "@/utilities/regex"
import {
getLineRangeFromBuffer,
getRangeFromBuffer, getTextAllRange,
Expand Down Expand Up @@ -162,40 +163,44 @@ export async function queryFileStaticAnalysis(
location: number | undefined,
stmtLen?: number,
) {
rows.forEach(
(row) => {
const range = (() => {
if (location === undefined) {
return getTextAllRange(document)
}
if (stmtLen) {
return getRangeFromBuffer(
fileText,
location + 1,
location + 1 + stmtLen,
)
rows.forEach((row) => {
const range = (() => {
if (location === undefined) {
return getTextAllRange(document)
}
if (stmtLen) {
const stmt = fileText.slice(location + 1, location + 1 + stmtLen)
if (DISABLE_STATIC_VALIDATION_RE
.test(stmt)) {
return
}

const lineRange = getLineRangeFromBuffer(
return getRangeFromBuffer(
fileText,
location,
row.lineno ? row.lineno - 1 : 0,
location + 1,
location + 1 + stmtLen,
)
}
const lineRange = getLineRangeFromBuffer(
fileText,
location,
row.lineno ? row.lineno - 1 : 0,
)

if (!lineRange) {
return getTextAllRange(document)
}

return lineRange
})()

if (!range) {
return
}

if (!lineRange) {
return getTextAllRange(document)
}

return lineRange
})()

errors.push({
level: row.level, range, message: row.message,
})

}
,
)

errors.push({
level: row.level, range, message: row.message,
})
})
}
}
18 changes: 13 additions & 5 deletions server/src/services/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe("Validate Tests", () => {

it("TRIGGER on inexistent field", async () => {
const diagnostics = await validateSampleFile(
"definitions/function/static_error_trigger_column_does_not_exist.pgsql",
"definitions/trigger/static_error_trigger_column_does_not_exist.pgsql",
)

expect(diagnostics).toStrictEqual([
Expand All @@ -90,13 +90,21 @@ describe("Validate Tests", () => {
])
})

// TODO
it.skip("static analysis disabled on invalid statement", async () => {
it("static analysis disabled on invalid statement", async () => {
const diagnostics = await validateSampleFile(
"definitions/function/static_error_disabled.pgsql",
"definitions/trigger/static_error_disabled.pgsql",
)

expect(diagnostics).toStrictEqual([])
if (!diagnostics) {
throw new Error("")
}
if (diagnostics?.length === 0) {
throw new Error("")
}

expect(diagnostics).toHaveLength(1)
expect(diagnostics[0].message)
.toContain("record \"new\" has no field \"updated_at\"")
})

it("FUNCTION column does not exists", async () => {
Expand Down
4 changes: 3 additions & 1 deletion server/src/utilities/regex.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable max-len */

export function escapeRegex(string: string): string {
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&")
}
Expand All @@ -8,5 +10,5 @@ export const BEGIN_RE = /^([\s]*begin[\s]*;)/igm
export const COMMIT_RE = /^([\s]*commit[\s]*;)/igm
export const ROLLBACK_RE = /^([\s]*rollback[\s]*;)/igm

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

0 comments on commit 7052778

Please sign in to comment.