Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(project): initial partial support for .vue files #1722

Merged
merged 6 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

<div></div>
```
- Add partial for `.vue` files. Biome is able to format the script block of Vue files. Contributed by @nhedger

```diff
<script setup lang="ts">
- statement ( );
+ statement();
</script/>

<template></template>
```

#### Bug fixes

Expand Down
19 changes: 18 additions & 1 deletion crates/biome_cli/src/execute/process_file/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::execute::process_file::{
};
use crate::execute::TraversalMode;
use biome_diagnostics::{category, DiagnosticExt};
use biome_service::file_handlers::ASTRO_FENCE;
use biome_service::file_handlers::{ASTRO_FENCE, VUE_FENCE};
use biome_service::workspace::RuleCategories;
use std::path::Path;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -84,6 +84,23 @@ pub(crate) fn format_with_guard<'ctx>(
}
}

if workspace_file.as_extension() == Some("vue") {
if output.is_empty() {
return Ok(FileStatus::Ignored);
}
if let Some(script) = VUE_FENCE
.captures(&input)
.and_then(|captures| captures.name("script"))
{
output = format!(
"{}{}{}",
&input[..script.start()],
output.as_str(),
&input[script.end()..]
);
}
}

if output != input {
if should_write {
workspace_file.update_file(output)?;
Expand Down
299 changes: 299 additions & 0 deletions crates/biome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ statement();
---
<div></div>"#;

const VUE_IMPLICIT_JS_FILE_UNFORMATTED: &str = r#"<script>
import { something } from "file.vue";
statement ( ) ;
</script>
<template></template>"#;

const VUE_IMPLICIT_JS_FILE_FORMATTED: &str = r#"<script>
import { something } from "file.vue";
statement();
</script>
<template></template>"#;

const VUE_EXPLICIT_JS_FILE_UNFORMATTED: &str = r#"<script lang="js">
import { something } from "file.vue";
statement ( ) ;
</script>
<template></template>"#;

const VUE_EXPLICIT_JS_FILE_FORMATTED: &str = r#"<script lang="js">
import { something } from "file.vue";
statement();
</script>
<template></template>"#;

const VUE_TS_FILE_UNFORMATTED: &str = r#"<script setup lang="ts">
import { type something } from "file.vue";
const hello : string = "world";
</script>
<template></template>"#;

const VUE_TS_FILE_FORMATTED: &str = r#"<script setup lang="ts">
import { type something } from "file.vue";
const hello: string = "world";
</script>
<template></template>"#;

const APPLY_TRAILING_COMMA_BEFORE: &str = r#"
const a = [
longlonglonglongItem1longlonglonglongItem1,
Expand Down Expand Up @@ -3028,3 +3064,266 @@ fn format_empty_astro_files_write() {
result,
));
}

#[test]
fn format_vue_implicit_js_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(
vue_file_path.into(),
VUE_IMPLICIT_JS_FILE_UNFORMATTED.as_bytes(),
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("format"), vue_file_path.as_os_str().to_str().unwrap()].as_slice()),
);

assert!(result.is_err(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, VUE_IMPLICIT_JS_FILE_UNFORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_vue_implicit_js_files",
fs,
console,
result,
));
}

#[test]
fn format_vue_implicit_js_files_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(
vue_file_path.into(),
VUE_IMPLICIT_JS_FILE_UNFORMATTED.as_bytes(),
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
"format",
"--write",
vue_file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, VUE_IMPLICIT_JS_FILE_FORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_vue_implicit_js_files_write",
fs,
console,
result,
));
}

#[test]
fn format_vue_explicit_js_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(
vue_file_path.into(),
VUE_EXPLICIT_JS_FILE_UNFORMATTED.as_bytes(),
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("format"), vue_file_path.as_os_str().to_str().unwrap()].as_slice()),
);

assert!(result.is_err(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, VUE_EXPLICIT_JS_FILE_UNFORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_vue_explicit_js_files",
fs,
console,
result,
));
}

#[test]
fn format_vue_explicit_js_files_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(
vue_file_path.into(),
VUE_EXPLICIT_JS_FILE_UNFORMATTED.as_bytes(),
);

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
"format",
"--write",
vue_file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, VUE_EXPLICIT_JS_FILE_FORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_vue_explicit_js_files_write",
fs,
console,
result,
));
}

#[test]
fn format_empty_vue_js_files_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(vue_file_path.into(), "<template></template>".as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
"format",
"--write",
vue_file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, "<template></template>");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_empty_vue_js_files_write",
fs,
console,
result,
));
}

#[test]
fn format_vue_ts_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(vue_file_path.into(), VUE_TS_FILE_UNFORMATTED.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("format"), vue_file_path.as_os_str().to_str().unwrap()].as_slice()),
);

assert!(result.is_err(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, VUE_TS_FILE_UNFORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_vue_ts_files",
fs,
console,
result,
));
}

#[test]
fn format_vue_ts_files_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(vue_file_path.into(), VUE_TS_FILE_UNFORMATTED.as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
"format",
"--write",
vue_file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, VUE_TS_FILE_FORMATTED);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_vue_ts_files_write",
fs,
console,
result,
));
}

#[test]
fn format_empty_vue_ts_files_write() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let vue_file_path = Path::new("file.vue");
fs.insert(vue_file_path.into(), "<template></template>".as_bytes());

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(
[
"format",
"--write",
vue_file_path.as_os_str().to_str().unwrap(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");

assert_file_contents(&fs, vue_file_path, "<template></template>");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"format_empty_vue_ts_files_write",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.vue`

```vue
<template></template>
```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: content
---
## `file.vue`

```vue
<template></template>
```

# Emitted Messages

```block
Formatted 1 file(s) in <TIME>
```


Loading
Loading