Skip to content

Commit

Permalink
refactor(playground): upgrade playground to be compatible with biome 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Feb 12, 2025
1 parent 12ecea6 commit 06a2851
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 41 deletions.
8 changes: 4 additions & 4 deletions src/playground/tabs/SettingsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -852,12 +852,12 @@ function LinterSettings({
aria-describedby="analyzer-fix-mode-description"
name="analyzer-fix-mode"
disabled={!enabledLinting}
value={analyzerFixMode ?? "SafeFixes"}
value={analyzerFixMode ?? "safeFixes"}
onChange={(e) => setAnalyzerFixMode(e.target.value as FixFileMode)}
>
<option value={"SafeFixes"}>Safe Fixes</option>
<option value={"SafeAndUnsafeFixes"}>Safe and Unsafe Fixes</option>
<option value={"ApplySuppressions"}>Apply Suppressions</option>
<option value={"safeFixes"}>Safe Fixes</option>
<option value={"safeAndUnsafeFixes"}>Safe and Unsafe Fixes</option>
<option value={"applySuppressions"}>Apply Suppressions</option>
</select>
</div>
</section>
Expand Down
2 changes: 1 addition & 1 deletion src/playground/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export const defaultPlaygroundState: PlaygroundState = {
bracketSameLine: false,
lintRules: LintRules.Recommended,
enabledLinting: true,
analyzerFixMode: "SafeFixes",
analyzerFixMode: "safeFixes",
importSortingEnabled: true,
unsafeParameterDecoratorsEnabled: true,
allowComments: true,
Expand Down
85 changes: 49 additions & 36 deletions src/playground/workers/biomeWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import {
} from "@/playground/types";
import init, {
DiagnosticPrinter,
type PartialConfiguration as Configuration,
type Configuration,
type BiomePath,
type RuleCategories,
Workspace,
type ProjectKey,
} from "@biomejs/wasm-web";

let workspace: Workspace | null = null;
Expand All @@ -29,16 +30,13 @@ type File = {
};

const files: Map<string, File> = new Map();
let projectKey: ProjectKey = 0;

let configuration: undefined | Configuration;
let fullSettings: undefined | PlaygroundSettings;

function getPathForFile(file: File): BiomePath {
return {
path: file.filename,
kind: ["Handleable"],
was_written: false,
};
return file.filename;
}

self.addEventListener("message", async (e) => {
Expand All @@ -54,8 +52,9 @@ self.addEventListener("message", async (e) => {
}

workspace = new Workspace();
workspace.registerProjectFolder({
setAsCurrentWorkspace: true,
projectKey = workspace.openProject({
openUninitialized: true,
path: "/",
});

self.postMessage({ type: "init", loadingState: LoadingState.Success });
Expand Down Expand Up @@ -110,7 +109,7 @@ self.addEventListener("message", async (e) => {
enabled: enabledLinting,
},

organizeImports: {
assist: {
enabled: importSortingEnabled,
},

Expand Down Expand Up @@ -175,7 +174,8 @@ self.addEventListener("message", async (e) => {

workspace.updateSettings({
configuration,
gitignore_matches: [],
gitignoreMatches: [],
projectKey,
});
break;
}
Expand All @@ -200,7 +200,12 @@ self.addEventListener("message", async (e) => {
workspace.openFile({
path: getPathForFile(file),
version: 0,
content: code,
persistNodeCache: true,
projectKey,
content: {
type: "fromClient",
content: code,
},
});
} else {
file = {
Expand All @@ -213,29 +218,37 @@ self.addEventListener("message", async (e) => {
workspace.openFile({
path: getPathForFile(file),
version: file.version,
content: code,
persistNodeCache: true,
projectKey,
content: {
type: "fromClient",
content: code,
},
});
}
files.set(filename, file);
const path = getPathForFile(file);
const fileFeatures = workspace.fileFeatures({
features: ["Debug", "Format", "Lint", "OrganizeImports"],
features: ["debug", "format", "lint", "assist"],
projectKey,
path,
});

const syntaxTree =
fileFeatures.features_supported.get("Debug") === "Supported"
fileFeatures.featuresSupported.get("debug") === "supported"
? workspace.getSyntaxTree({
path,
projectKey,
})
: { ast: "Not supported", cst: "Not supported" };

let controlFlowGraph = "";
try {
controlFlowGraph =
fileFeatures.features_supported.get("Debug") === "Supported"
fileFeatures.featuresSupported.get("debug") === "supported"
? workspace.getControlFlowGraph({
path,
projectKey,
cursor: cursorPosition,
})
: "";
Expand All @@ -247,43 +260,41 @@ self.addEventListener("message", async (e) => {
let formatterIr = "";
try {
formatterIr =
fileFeatures.features_supported.get("Debug") === "Supported"
fileFeatures.featuresSupported.get("debug") === "supported"
? workspace.getFormatterIr({
path,
projectKey,
})
: "Not supported";
} catch (e) {
console.error(e);
formatterIr = "Can't format";
}

const importSorting =
fileFeatures.features_supported.get("OrganizeImports") === "Supported"
? workspace.organizeImports({
path,
})
: {
code:
fileFeatures.features_supported.get("OrganizeImports") ??
"Not supported",
};
const importSorting = {
code: "Moved to Analyzer Fixes tab",
};

const categories: RuleCategories = [];
if (configuration?.formatter?.enabled) {
categories.push("Syntax");
categories.push("syntax");
}
if (configuration?.linter?.enabled) {
categories.push("Lint");
categories.push("lint");
}
if (configuration?.assist?.enabled) {
categories.push("action");
}
const diagnosticsResult = workspace.pullDiagnostics({
path,
categories: categories,
max_diagnostics: Number.MAX_SAFE_INTEGER,
categories,
maxDiagnostics: Number.MAX_SAFE_INTEGER,
projectKey,
only: [],
skip: [],
});

const printer = new DiagnosticPrinter(path.path, code);
const printer = new DiagnosticPrinter(path, code);
for (const diag of diagnosticsResult.diagnostics) {
printer.print_verbose(diag);
}
Expand All @@ -293,9 +304,10 @@ self.addEventListener("message", async (e) => {
};
try {
printed =
fileFeatures.features_supported.get("Format") === "Supported"
fileFeatures.featuresSupported.get("format") === "supported"
? workspace.formatFile({
path,
projectKey,
})
: { code: "Not supported" };
} catch (e) {
Expand All @@ -310,14 +322,15 @@ self.addEventListener("message", async (e) => {
};
try {
fixed =
fileFeatures.features_supported.get("Lint") === "Supported"
fileFeatures.featuresSupported.get("lint") === "supported"
? workspace.fixFile({
path,
projectKey,
only: [],
skip: [],
rule_categories: ["Lint"],
should_format: false,
fix_file_mode: fullSettings?.analyzerFixMode ?? "SafeFixes",
ruleCategories: categories,
shouldFormat: false,
fixFileMode: fullSettings?.analyzerFixMode ?? "safeFixes",
})
: { code: "Not supported" };
} catch (e) {
Expand Down

0 comments on commit 06a2851

Please sign in to comment.