Skip to content

Commit

Permalink
fix(js-api): fix type definition for IFileFeaturesResult
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Feb 5, 2025
1 parent a17ca7d commit 149f011
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/violet-dingos-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@biomejs/biome_wasm": patch
"@biomejs/js-api": patch
---

Fixed the type definition of `IFileFeaturesResult.featuresSupported`
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 40 additions & 3 deletions crates/biome_service/src/workspace_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,10 @@ pub fn generate_type<'a>(
// Create a property signature member in the interface for each
// property of the corresponding schema object
let object = schema.object.as_deref().unwrap();
for (property, schema) in &object.properties {
for (property_str, schema) in &object.properties {
let (ts_type, optional, description) = schema_type(queue, root_schema, schema);

let mut property = make::ident(property);
let mut property = make::ident(property_str);
if let Some(description) = description {
let comment = format!("/**\n\t* {description} \n\t */");
let trivia = vec![
Expand All @@ -350,10 +350,47 @@ pub fn generate_type<'a>(
property = property.with_leading_trivia(trivia);
}

let type_annotation = if property_str == "featuresSupported" {
// HACK: force the `featuresSupported` property to be a Map<FeatureKind, SupportKind>
// This is a temporary workaround to fix the type annotation for this property. The
// better fix would be to use the `transform` feature that is available in `schemars` 1.0 to
// add a metadata field that we can pick up here to generate the correct type annotation.
// Alternatively, we could generate these types based on the actual rust types instead of the
// json schema.
let full_type = make::ts_reference_type(
make::js_reference_identifier(make::ident("Map")).into(),
)
.with_type_arguments(make::ts_type_arguments(
make::token(T![<]),
make::ts_type_argument_list(
[
make::ts_reference_type(
make::js_reference_identifier(make::ident("FeatureKind"))
.into(),
)
.build()
.into(),
make::ts_reference_type(
make::js_reference_identifier(make::ident("SupportKind"))
.into(),
)
.build()
.into(),
],
[make::token(T![,])],
),
make::token(T![>]),
))
.build();
make::ts_type_annotation(make::token(T![:]), full_type.into())
} else {
make::ts_type_annotation(make::token(T![:]), ts_type)
};

let mut builder = make::ts_property_signature_type_member(
AnyJsObjectMemberName::from(make::js_literal_member_name(property)),
)
.with_type_annotation(make::ts_type_annotation(make::token(T![:]), ts_type));
.with_type_annotation(type_annotation);

if optional {
builder = builder.with_optional_token(make::token(T![?]));
Expand Down
1 change: 1 addition & 0 deletions crates/biome_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ biome_js_formatter = { workspace = true }
biome_rowan = { workspace = true }
biome_service = { workspace = true, features = ["schema"] }
quote = "1.0.14"
schemars = { workspace = true }

[lints]
workspace = true
Expand Down
5 changes: 5 additions & 0 deletions crates/biome_wasm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use biome_js_formatter::{context::JsFormatOptions, format_node};
use biome_rowan::AstNode;
use biome_service::workspace_types::{generate_type, methods, ModuleQueue};
use quote::{format_ident, quote};
use schemars::gen::{SchemaGenerator, SchemaSettings};
use std::{env, fs, io, path::PathBuf};

fn main() -> io::Result<()> {
Expand All @@ -19,6 +20,10 @@ fn main() -> io::Result<()> {
generate_type(&mut items, &mut queue, &method.params);
generate_type(&mut items, &mut queue, &method.result);
}
// HACK: SupportKind doesn't get picked up in the loop above, so we add it manually
let support_kind_schema = SchemaGenerator::from(SchemaSettings::openapi3())
.root_schema_for::<biome_service::workspace::SupportKind>();
generate_type(&mut items, &mut queue, &support_kind_schema);

let module = make::js_module(
make::js_directive_list(None),
Expand Down
4 changes: 2 additions & 2 deletions packages/@biomejs/wasm-web/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@biomejs/wasm-web",
"name": "@biomejs/biome_wasm",
"type": "module",
"collaborators": [
"Biome Developers and Contributors"
Expand Down Expand Up @@ -28,4 +28,4 @@
"formatter",
"wasm"
]
}
}

0 comments on commit 149f011

Please sign in to comment.