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 ae9094a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
5 changes: 4 additions & 1 deletion sample/definitions/function/static_error_disabled.pgsql
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 ();
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,
})
})
}
}
14 changes: 11 additions & 3 deletions server/src/services/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

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 ae9094a

Please sign in to comment.