Skip to content

Commit

Permalink
Better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
SkaceKamen committed Feb 9, 2024
1 parent 7cf9a11 commit 3a00963
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions server/src/sqflint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
preprocess,
} from "@bi-tools/preprocessor";
import { analyzeSqf } from "@bi-tools/sqf-analyzer";
import { parseSqfTokens, tokenizeSqf } from "@bi-tools/sqf-parser";
import {
SqfParserError,
parseSqfTokens,
tokenizeSqf,
} from "@bi-tools/sqf-parser";
import * as fs from "fs";
import * as path from "path";
import { Logger } from "./lib/logger";
Expand Down Expand Up @@ -56,53 +60,50 @@ export class SQFLint {
},
});

try {
const tokens = tokenizeSqf(preprocessed.code);
const data = parseSqfTokens(tokens, filename);
const analysis = analyzeSqf(data, tokens, preprocessed.code);
const sourceMap = preprocessed.sourceMap;
const fileContents = {} as Record<string, string>;
const sourceMap = preprocessed.sourceMap;
const fileContents = {} as Record<string, string>;

const getContents = async (filename: string) => {
if (!fileContents[filename]) {
fileContents[filename] = await fs.promises.readFile(
filename,
"utf-8"
);
}
return fileContents[filename];
};

const getProperOffset = async (offset: number) => {
const mapped = getMappedOffsetAt(
sourceMap,
offset,
filename
const getContents = async (filename: string) => {
if (!fileContents[filename]) {
fileContents[filename] = await fs.promises.readFile(
filename,
"utf-8"
);
}
return fileContents[filename];
};

const location = getLocationFromOffset(
mapped.offset,
await getContents(mapped.file)
);
const getProperOffset = async (offset: number) => {
const mapped = getMappedOffsetAt(sourceMap, offset, filename);

return location;
};
const location = getLocationFromOffset(
mapped.offset,
await getContents(mapped.file)
);

return location;
};

const offsetsToRange = async (start: number, end: number) => {
const startLocation = await getProperOffset(start);
const endLocation = await getProperOffset(end);
const offsetsToRange = async (start: number, end: number) => {
const startLocation = await getProperOffset(start);
const endLocation = await getProperOffset(end);

return new SQFLint.Range(
new SQFLint.Position(
startLocation.line - 1,
startLocation.column - 1
),
new SQFLint.Position(
endLocation.line - 1,
endLocation.column - 1
)
);
};
return new SQFLint.Range(
new SQFLint.Position(
startLocation.line - 1,
startLocation.column - 1
),
new SQFLint.Position(
endLocation.line - 1,
endLocation.column - 1
)
);
};

try {
const tokens = tokenizeSqf(preprocessed.code);
const data = parseSqfTokens(tokens, filename);
const analysis = analyzeSqf(data, tokens, preprocessed.code);

return {
errors: [
Expand Down Expand Up @@ -152,7 +153,6 @@ export class SQFLint {
};
} catch (err) {
console.error(err);

console.log(preprocessed.code);

return {
Expand All @@ -169,10 +169,15 @@ export class SQFLint {
),
new SQFLint.Error(
err.message,
new SQFLint.Range(
new SQFLint.Position(0, 0),
new SQFLint.Position(0, 0)
)
err instanceof SqfParserError
? await offsetsToRange(
err.token.position.from,
err.token.position.to
)
: new SQFLint.Range(
new SQFLint.Position(0, 0),
new SQFLint.Position(0, 0)
)
),
],
warnings: [],
Expand Down

0 comments on commit 3a00963

Please sign in to comment.