Skip to content

Commit

Permalink
show exact static analysis errors location - pending possible getLine…
Browse files Browse the repository at this point in the history
…RangeFromBuffer fix
  • Loading branch information
danicc097 committed Dec 1, 2022
1 parent f709ce5 commit 4280771
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
4 changes: 2 additions & 2 deletions server/src/postgres/parsers/parseFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface FunctionInfo {

export interface TriggerInfo {
functionName: string,
location: number | undefined,
relname: string,
stmtLocation?: number,
}

export async function parseFunctions(
Expand Down Expand Up @@ -137,8 +137,8 @@ function getCreateTriggers(
return [
{
functionName,
location: undefined,
relname,
stmtLocation: statement?.stmt_location,
},
]
},
Expand Down
63 changes: 41 additions & 22 deletions server/src/postgres/queries/queryFileStaticAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function queryFileStaticAnalysis(
const [fileText] = sanitizeFileWithQueryParameters(
document.getText(), options.queryParameterInfo, logger,
)
logger.info(`fileText.length: ${fileText.length}`)

try {
const extensionCheck = await pgClient.query(`
Expand Down Expand Up @@ -101,17 +102,18 @@ export async function queryFileStaticAnalysis(
await pgClient.query("BEGIN")
if (options.isComplete) {
const message = (error as Error).message
logger.error(`StaticAnalysisError: ${message} (${document.uri})`)
logger.error(`StaticAnalysisError (1): ${message} (${document.uri})`)
}
}

try {
for (const { functionName, location, relname } of triggerInfos) {
for (const triggerInfo of triggerInfos) {
const { functionName, stmtLocation, relname } = triggerInfo
logger.warn(`
trigger:::
relname: ${relname}
functionName: ${functionName}
location: ${location}`)
stmtLocation: ${stmtLocation}`)

const result = await pgClient.query(
`
Expand All @@ -138,37 +140,54 @@ export async function queryFileStaticAnalysis(
continue
}

extractError(rows, location)
extractError(rows, stmtLocation)
}
}
catch (error: unknown) {
await pgClient.query("ROLLBACK to validated_syntax")
await pgClient.query("BEGIN")
if (options.isComplete) {
const message = (error as Error).message
logger.error(`StaticAnalysisError: ${message} (${document.uri})`)
logger.error(`StaticAnalysisError (2): ${message} (${document.uri})`)
}
}

return errors

function extractError(rows: StaticAnalysisErrorRow[], location: number | undefined) {
function extractError(
rows: StaticAnalysisErrorRow[],
location: number | undefined,
) {
rows.forEach(
(row) => {
const range = (() => {
return (location === undefined)
? getTextAllRange(document)
: getLineRangeFromBuffer(
fileText,
location,
row.lineno ? row.lineno - 1 : 0,
) ?? getTextAllRange(document)
})()

errors.push({
level: row.level, range, message: row.message,
})
},
)
(row) => {
let range: Range
// FIXME getLineRangeFromBuffer
// range may be larger than byte count for some cases at the end of the doc and throw err reading length of undefined.
// both fileText.length and location from parsed stmt are correct
try {
range = (() => {
return (location === undefined)
? getTextAllRange(document)
: getLineRangeFromBuffer(
fileText,
location,
row.lineno ? row.lineno - 1 : 0,
) ?? getTextAllRange(document)
})()
} catch (error: unknown) {
logger.error(`Could not extract error from row.
message: ${JSON.stringify(row.message)}
lineno: ${row.lineno}
location: ${location}`)
range = getTextAllRange(document)
}
errors.push({
level: row.level, range, message: row.message,
})

}
,
)

}
}
6 changes: 3 additions & 3 deletions server/src/services/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ async function validateStaticAnalysis(
options: ValidateTextDocumentOptions,
logger: Logger,
): Promise<Diagnostic[]> {
const [functions, triggers] = await parseFunctions(
const [functionInfos, triggerInfos] = await parseFunctions(
document.uri,
options.queryParameterInfo,
logger,
)
const errors = await queryFileStaticAnalysis(
pgClient,
document,
functions,
triggers,
functionInfos,
triggerInfos,
{
isComplete: options.isComplete,
queryParameterInfo: options.queryParameterInfo,
Expand Down

0 comments on commit 4280771

Please sign in to comment.