Skip to content

Commit

Permalink
fix: handle unimplemented file capabilities slightly more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Sep 20, 2024
1 parent f39b044 commit 48a9366
Showing 1 changed file with 30 additions and 38 deletions.
68 changes: 30 additions & 38 deletions src/playground/workers/biomeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ import {
QuoteStyle,
Semicolons,
} from "@/playground/types";
import {
isCssFilename,
isFrameworkTemplateFilename,
isGraphqlFilename,
isJsonFilename,
} from "@/playground/utils";
import init, {
DiagnosticPrinter,
type PartialConfiguration as Configuration,
Expand Down Expand Up @@ -221,42 +215,38 @@ self.addEventListener("message", async (e) => {
}
files.set(filename, file);
const path = getPathForFile(file);
const isFrameworkTemplate = isFrameworkTemplateFilename(filename);

const syntaxTree = !isFrameworkTemplate
? workspace.getSyntaxTree({
path,
})
: {
ast: "Not available",
cst: "Not available",
};
const fileFeatures =
workspace.fileFeatures({
features: ["Debug", "Format", "Lint", "OrganizeImports"],
path,
});

const isGraphql = isGraphqlFilename(filename);
const syntaxTree = fileFeatures.features_supported.get("Debug") === "Supported" ? workspace.getSyntaxTree({
path,
}) : { ast: "Not available", cst: "Not available" };

const controlFlowGraph = !(
isJsonFilename(filename) ||
isCssFilename(filename) ||
isGraphql ||
isFrameworkTemplate
)
? workspace.getControlFlowGraph({
let controlFlowGraph = "";
try {
controlFlowGraph = fileFeatures.features_supported.get("Debug") === "Supported"
? workspace.getControlFlowGraph({
path,
cursor: cursorPosition,
})
: "";
: "";
} catch (e) {
console.warn("Failed to get control flow graph:", e);
controlFlowGraph = "";
}

const formatterIr = !isFrameworkTemplate
? workspace.getFormatterIr({
path,
})
: "Not available";
const formatterIr = fileFeatures.features_supported.get("Debug") === "Supported" ? workspace.getFormatterIr({
path,
}) : "Not available";

const importSorting = isGraphql
? { code: "" }
: workspace.organizeImports({
path,
});
const importSorting = fileFeatures.features_supported.get("OrganizeImports") === "Supported"
? workspace.organizeImports({
path,
})
: { code: fileFeatures.features_supported.get("OrganizeImports") ?? "Not available" };

const categories: RuleCategories = [];
if (configuration?.formatter?.enabled) {
Expand All @@ -278,9 +268,11 @@ self.addEventListener("message", async (e) => {
printer.print_verbose(diag);
}

const printed = workspace.formatFile({
path,
});
const printed = fileFeatures.features_supported.get("Format") === "Supported"
? workspace.formatFile({
path,
})
: { code };

const biomeOutput: BiomeOutput = {
syntax: {
Expand Down

0 comments on commit 48a9366

Please sign in to comment.