Skip to content

Commit

Permalink
fix: regression of latest refactor (#2594)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Apr 25, 2024
1 parent bed20b8 commit 0c0eb99
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 167 deletions.
10 changes: 9 additions & 1 deletion crates/biome_formatter_test/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use biome_fs::BiomePath;
use biome_parser::AnyParse;
use biome_rowan::{TextRange, TextSize};
use biome_service::settings::{ServiceLanguage, Settings};
use biome_service::workspace::{DocumentFileSource, FeaturesBuilder, SupportsFeatureParams};
use biome_service::workspace::{
DocumentFileSource, FeaturesBuilder, RegisterProjectFolderParams, SupportsFeatureParams,
};
use biome_service::App;
use std::ops::Range;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -40,6 +42,12 @@ impl<'a> SpecTestFile<'a> {
spec_input_file.display()
);

app.workspace
.register_project_folder(RegisterProjectFolderParams {
set_as_current_workspace: true,
path: None,
})
.unwrap();
let mut input_file = BiomePath::new(file_path);
let can_format = app
.workspace
Expand Down
336 changes: 175 additions & 161 deletions crates/biome_service/tests/workspace.rs
Original file line number Diff line number Diff line change
@@ -1,162 +1,176 @@
use biome_fs::BiomePath;
use biome_js_syntax::{JsFileSource, TextSize};
use biome_service::file_handlers::DocumentFileSource;
use biome_service::workspace::{server, FileGuard, OpenFileParams};

#[test]
fn debug_control_flow() {
const SOURCE: &str = "function test () { return; }";
const GRAPH: &str = "flowchart TB
block_0[\"<b>block_0</b><br/>Return(JS_RETURN_STATEMENT 19..26)<br/>Return\"]
";

let workspace = server();

let file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("file.js"),
content: SOURCE.into(),
version: 0,
document_file_source: Some(DocumentFileSource::from(JsFileSource::default())),
},
)
.unwrap();

let cfg = file.get_control_flow_graph(TextSize::from(20)).unwrap();

assert_eq!(cfg, GRAPH);
}

#[test]
fn recognize_typescript_definition_file() {
let workspace = server();

let file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("file.d.ts"),
// the following code snippet can be correctly parsed in .d.ts file but not in .ts file
content: "export const foo: number".into(),
version: 0,
document_file_source: None,
},
)
.unwrap();

assert!(file.format_file().is_ok());
}

#[test]
fn correctly_handle_json_files() {
let workspace = server();

// ".json" file
let json_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("a.json"),
content: r#"{"a": 42}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(json_file.format_file().is_ok());

// ".json" file doesn't allow comments
let json_file_with_comments = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("b.json"),
content: r#"{"a": 42}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(json_file_with_comments.format_file().is_err());

// ".json" file doesn't allow trailing commas
let json_file_with_trailing_commas = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("c.json"),
content: r#"{"a": 42,}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(json_file_with_trailing_commas.format_file().is_err());

// ".jsonc" file allows comments
let jsonc_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("d.jsonc"),
content: r#"{"a": 42}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(jsonc_file.format_file().is_ok());

// ".jsonc" file allow trailing commas
let jsonc_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("e.jsonc"),
content: r#"{"a": 42,}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(jsonc_file.format_file().is_ok());

// well-known json-with-comments file allows comments
let well_known_json_with_comments_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new(".eslintrc.json"),
content: r#"{"a": 42}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(well_known_json_with_comments_file.format_file().is_ok());

// well-known json-with-comments file doesn't allow trailing commas
let well_known_json_with_comments_file_with_trailing_commas = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("dir/.eslintrc.json"),
content: r#"{"a": 42,}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(well_known_json_with_comments_file_with_trailing_commas
.format_file()
.is_err());

// well-known json-with-comments-and-trailing-commas file allows comments and trailing commas
let well_known_json_with_comments_and_trailing_commas_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("tsconfig.json"),
content: r#"{"a": 42,}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(well_known_json_with_comments_and_trailing_commas_file
.format_file()
.is_ok());
#[cfg(test)]
mod test {
use biome_fs::BiomePath;
use biome_js_syntax::{JsFileSource, TextSize};
use biome_service::file_handlers::DocumentFileSource;
use biome_service::workspace::{
server, FileGuard, OpenFileParams, RegisterProjectFolderParams,
};
use biome_service::Workspace;
fn create_server() -> Box<dyn Workspace> {
let workspace = server();
workspace
.register_project_folder(RegisterProjectFolderParams {
set_as_current_workspace: true,
path: None,
})
.unwrap();

workspace
}

#[test]
fn debug_control_flow() {
const SOURCE: &str = "function test () { return; }";
const GRAPH: &str = "flowchart TB
block_0[\"<b>block_0</b><br/>Return(JS_RETURN_STATEMENT 19..26)<br/>Return\"]\n\n";

let workspace = create_server();
let file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("file.js"),
content: SOURCE.into(),
version: 0,
document_file_source: Some(DocumentFileSource::from(JsFileSource::default())),
},
)
.unwrap();

let cfg = file.get_control_flow_graph(TextSize::from(20)).unwrap();

assert_eq!(cfg, GRAPH);
}

#[test]
fn recognize_typescript_definition_file() {
let workspace = create_server();

let file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("file.d.ts"),
// the following code snippet can be correctly parsed in .d.ts file but not in .ts file
content: "export const foo: number".into(),
version: 0,
document_file_source: None,
},
)
.unwrap();

assert!(file.format_file().is_ok());
}

#[test]
fn correctly_handle_json_files() {
let workspace = create_server();

// ".json" file
let json_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("a.json"),
content: r#"{"a": 42}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(json_file.format_file().is_ok());

// ".json" file doesn't allow comments
let json_file_with_comments = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("b.json"),
content: r#"{"a": 42}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(json_file_with_comments.format_file().is_err());

// ".json" file doesn't allow trailing commas
let json_file_with_trailing_commas = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("c.json"),
content: r#"{"a": 42,}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(json_file_with_trailing_commas.format_file().is_err());

// ".jsonc" file allows comments
let jsonc_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("d.jsonc"),
content: r#"{"a": 42}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(jsonc_file.format_file().is_ok());

// ".jsonc" file allow trailing commas
let jsonc_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("e.jsonc"),
content: r#"{"a": 42,}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(jsonc_file.format_file().is_ok());

// well-known json-with-comments file allows comments
let well_known_json_with_comments_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new(".eslintrc.json"),
content: r#"{"a": 42}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(well_known_json_with_comments_file.format_file().is_ok());

// well-known json-with-comments file doesn't allow trailing commas
let well_known_json_with_comments_file_with_trailing_commas = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("dir/.eslintrc.json"),
content: r#"{"a": 42,}"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(well_known_json_with_comments_file_with_trailing_commas
.format_file()
.is_err());

// well-known json-with-comments-and-trailing-commas file allows comments and trailing commas
let well_known_json_with_comments_and_trailing_commas_file = FileGuard::open(
workspace.as_ref(),
OpenFileParams {
path: BiomePath::new("tsconfig.json"),
content: r#"{"a": 42,}//comment"#.into(),
version: 0,
document_file_source: None,
},
)
.unwrap();
assert!(well_known_json_with_comments_and_trailing_commas_file
.format_file()
.is_ok());
}
}
12 changes: 7 additions & 5 deletions packages/@biomejs/backend-jsonrpc/tests/workspace.test.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
import {resolve} from "node:path";
import {fileURLToPath} from "node:url";
import {describe, expect, it} from "vitest";

import { createWorkspaceWithBinary } from "../dist/index.js";
import {createWorkspaceWithBinary} from "../dist/index.js";

describe("Workspace API", () => {
it("should process remote requests", async () => {
Expand All @@ -14,7 +14,9 @@ describe("Workspace API", () => {
);

const workspace = await createWorkspaceWithBinary(command);

workspace.registerProjectFolder({
setAsCurrentWorkspace: true
});
await workspace.openFile({
path: {
path: "test.js",
Expand Down

0 comments on commit 0c0eb99

Please sign in to comment.