@biomejs/js-api
Version:
JavaScript APIs for the Biome package
189 lines • 6.17 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.BiomeCommon = void 0;
const wasm_1 = require("./wasm");
function isFormatContentDebug(options) {
return "debug" in options && options.debug !== undefined;
}
/**
* List of modules that have been initialized.
*/
const initialized = new WeakSet();
class BiomeCommon {
constructor(module) {
this.module = module;
if (!initialized.has(module)) {
module.main();
initialized.add(module);
}
this.workspace = new module.Workspace();
}
/**
* Stop this instance of Biome
*
* After calling `shutdown()` on this object, it should be considered
* unusable as calling any method on it will fail
*/
shutdown() {
this.workspace.free();
}
/**
* Allows to apply a custom configuration.
*
* If fails when the configuration is incorrect.
*
* @param projectKey The identifier of the project
* @param configuration
*/
applyConfiguration(projectKey, configuration) {
(0, wasm_1.tryCatchWrapper)(() => {
this.workspace.updateSettings({
projectKey,
configuration,
workspaceDirectory: "./",
});
});
}
/**
* Open a possible workspace project folder. Returns the key of said project. Use this key when you want to switch to different projects.
*
* @param [path]
*/
openProject(path) {
return this.workspace.openProject({
path: path || "",
openUninitialized: true,
});
}
withFile(projectKey, path, content, func) {
return (0, wasm_1.tryCatchWrapper)(() => {
this.workspace.openFile({
projectKey,
content: { type: "fromClient", content, version: 0 },
path,
});
try {
return func(path);
}
finally {
this.workspace.closeFile({
projectKey,
path,
});
}
});
}
/**
* If formats some content.
*
* @param projectKey The identifier of the project
* @param content The content to format
* @param options Options needed when formatting some content
*/
formatContent(projectKey, content, options) {
return this.withFile(projectKey, options.filePath, content, (path) => {
let code = content;
const result = this.workspace.pullDiagnostics({
projectKey,
path,
categories: ["syntax"],
pullCodeActions: false,
});
if (0 === result.errors) {
if (options.range) {
const result = this.workspace.formatRange({
projectKey,
path,
range: options.range,
});
code = result.code;
}
else {
const result = this.workspace.formatFile({
projectKey,
path,
});
code = result.code;
}
if (isFormatContentDebug(options)) {
const ir = this.workspace.getFormatterIr({
projectKey,
path,
});
return {
content: code,
diagnostics: result.diagnostics,
ir,
};
}
}
return {
content: code,
diagnostics: result.diagnostics,
};
});
}
/**
* Lint the content of a file.
*
* @param projectKey The identifier of the project
* @param content The content to lint
* @param options Options needed when linting some content
*/
lintContent(projectKey, content, { filePath, fixFileMode }) {
const maybeFixedContent = fixFileMode
? this.withFile(projectKey, filePath, content, (path) => {
const { code } = this.workspace.fixFile({
projectKey,
path,
fixFileMode: fixFileMode,
shouldFormat: false,
ruleCategories: ["syntax", "lint", "action"],
});
return code;
})
: content;
return this.withFile(projectKey, filePath, maybeFixedContent, (path) => {
const { diagnostics } = this.workspace.pullDiagnostics({
projectKey,
path,
categories: ["syntax", "lint", "action"],
pullCodeActions: false,
});
return {
content: maybeFixedContent,
diagnostics,
};
});
}
/**
* Print a list of diagnostics to an HTML string.
*
* @param diagnostics The list of diagnostics to print
* @param options Options needed for printing the diagnostics
*/
printDiagnostics(diagnostics, options) {
return (0, wasm_1.tryCatchWrapper)(() => {
const printer = new this.module.DiagnosticPrinter(options.filePath, options.fileSource);
try {
for (const diag of diagnostics) {
if (options.verbose) {
printer.print_verbose(diag);
}
else {
printer.print_simple(diag);
}
}
return printer.finish();
}
catch (err) {
// Only call `free` if the `print` method throws, `finish` will
// take care of deallocating the printer even if it fails
printer.free();
throw err;
}
});
}
}
exports.BiomeCommon = BiomeCommon;
//# sourceMappingURL=common.js.map
;