Skip to content

Commit 4280771

Browse files
committed
show exact static analysis errors location - pending possible getLineRangeFromBuffer fix
1 parent f709ce5 commit 4280771

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

server/src/postgres/parsers/parseFunctions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export interface FunctionInfo {
1414

1515
export interface TriggerInfo {
1616
functionName: string,
17-
location: number | undefined,
1817
relname: string,
18+
stmtLocation?: number,
1919
}
2020

2121
export async function parseFunctions(
@@ -137,8 +137,8 @@ function getCreateTriggers(
137137
return [
138138
{
139139
functionName,
140-
location: undefined,
141140
relname,
141+
stmtLocation: statement?.stmt_location,
142142
},
143143
]
144144
},

server/src/postgres/queries/queryFileStaticAnalysis.ts

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export async function queryFileStaticAnalysis(
4949
const [fileText] = sanitizeFileWithQueryParameters(
5050
document.getText(), options.queryParameterInfo, logger,
5151
)
52+
logger.info(`fileText.length: ${fileText.length}`)
5253

5354
try {
5455
const extensionCheck = await pgClient.query(`
@@ -101,17 +102,18 @@ export async function queryFileStaticAnalysis(
101102
await pgClient.query("BEGIN")
102103
if (options.isComplete) {
103104
const message = (error as Error).message
104-
logger.error(`StaticAnalysisError: ${message} (${document.uri})`)
105+
logger.error(`StaticAnalysisError (1): ${message} (${document.uri})`)
105106
}
106107
}
107108

108109
try {
109-
for (const { functionName, location, relname } of triggerInfos) {
110+
for (const triggerInfo of triggerInfos) {
111+
const { functionName, stmtLocation, relname } = triggerInfo
110112
logger.warn(`
111113
trigger:::
112114
relname: ${relname}
113115
functionName: ${functionName}
114-
location: ${location}`)
116+
stmtLocation: ${stmtLocation}`)
115117

116118
const result = await pgClient.query(
117119
`
@@ -138,37 +140,54 @@ export async function queryFileStaticAnalysis(
138140
continue
139141
}
140142

141-
extractError(rows, location)
143+
extractError(rows, stmtLocation)
142144
}
143145
}
144146
catch (error: unknown) {
145147
await pgClient.query("ROLLBACK to validated_syntax")
146148
await pgClient.query("BEGIN")
147149
if (options.isComplete) {
148150
const message = (error as Error).message
149-
logger.error(`StaticAnalysisError: ${message} (${document.uri})`)
151+
logger.error(`StaticAnalysisError (2): ${message} (${document.uri})`)
150152
}
151153
}
152154

153155
return errors
154156

155-
function extractError(rows: StaticAnalysisErrorRow[], location: number | undefined) {
157+
function extractError(
158+
rows: StaticAnalysisErrorRow[],
159+
location: number | undefined,
160+
) {
156161
rows.forEach(
157-
(row) => {
158-
const range = (() => {
159-
return (location === undefined)
160-
? getTextAllRange(document)
161-
: getLineRangeFromBuffer(
162-
fileText,
163-
location,
164-
row.lineno ? row.lineno - 1 : 0,
165-
) ?? getTextAllRange(document)
166-
})()
167-
168-
errors.push({
169-
level: row.level, range, message: row.message,
170-
})
171-
},
172-
)
162+
(row) => {
163+
let range: Range
164+
// FIXME getLineRangeFromBuffer
165+
// range may be larger than byte count for some cases at the end of the doc and throw err reading length of undefined.
166+
// both fileText.length and location from parsed stmt are correct
167+
try {
168+
range = (() => {
169+
return (location === undefined)
170+
? getTextAllRange(document)
171+
: getLineRangeFromBuffer(
172+
fileText,
173+
location,
174+
row.lineno ? row.lineno - 1 : 0,
175+
) ?? getTextAllRange(document)
176+
})()
177+
} catch (error: unknown) {
178+
logger.error(`Could not extract error from row.
179+
message: ${JSON.stringify(row.message)}
180+
lineno: ${row.lineno}
181+
location: ${location}`)
182+
range = getTextAllRange(document)
183+
}
184+
errors.push({
185+
level: row.level, range, message: row.message,
186+
})
187+
188+
}
189+
,
190+
)
191+
173192
}
174193
}

server/src/services/validation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,16 @@ async function validateStaticAnalysis(
161161
options: ValidateTextDocumentOptions,
162162
logger: Logger,
163163
): Promise<Diagnostic[]> {
164-
const [functions, triggers] = await parseFunctions(
164+
const [functionInfos, triggerInfos] = await parseFunctions(
165165
document.uri,
166166
options.queryParameterInfo,
167167
logger,
168168
)
169169
const errors = await queryFileStaticAnalysis(
170170
pgClient,
171171
document,
172-
functions,
173-
triggers,
172+
functionInfos,
173+
triggerInfos,
174174
{
175175
isComplete: options.isComplete,
176176
queryParameterInfo: options.queryParameterInfo,

0 commit comments

Comments
 (0)