Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
feat(rome_cli, rome_service): ignore unknown files (#4547)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Jun 10, 2023
1 parent 7b65492 commit dad13a9
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 16 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@
## [Unreleased]

### CLI

#### Other changes

- Add a new option to ignore unknown files

```shell
rome format ./src --files-ignore-unknown=true
```
Doing so, Rome won't emit diagnostics for file that it doesn't know how to handle.

### Configuration

#### Other changes

- Add a new option to ignore unknown files:

```json
{
"files": {
"ignoreUnknown": true
}
}
```
Doing so, Rome won't emit diagnostics for file that it doesn't know how to handle.

### Editors
### Formatter
### Linter
Expand Down
31 changes: 31 additions & 0 deletions crates/rome_cli/tests/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2273,3 +2273,34 @@ if (true) {
result,
));
}

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

let file_path1 = Path::new("test.txt");
fs.insert(file_path1.into(), *b"content");

let file_path2 = Path::new("test.js");
fs.insert(file_path2.into(), *b"console.log('bar');\n");

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[
("check"),
file_path1.as_os_str().to_str().unwrap(),
file_path2.as_os_str().to_str().unwrap(),
"--files-ignore-unknown=true",
]),
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"ignores_unknown_file",
fs,
console,
result,
));
}
31 changes: 31 additions & 0 deletions crates/rome_cli/tests/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,34 @@ file2.js
result,
));
}

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

let file_path1 = Path::new("test.txt");
fs.insert(file_path1.into(), *b"content");

let file_path2 = Path::new("test.js");
fs.insert(file_path2.into(), *b"console.log('bar');\n");

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[
("ci"),
file_path1.as_os_str().to_str().unwrap(),
file_path2.as_os_str().to_str().unwrap(),
"--files-ignore-unknown=true",
]),
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"ignores_unknown_file",
fs,
console,
result,
));
}
33 changes: 32 additions & 1 deletion crates/rome_cli/tests/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,7 @@ fn no_supported_file_found() {
let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[("check"), "."]),
Args::from(&[("format"), "."]),
);

eprintln!("{:?}", console.out_buffer);
Expand Down Expand Up @@ -1608,3 +1608,34 @@ file2.js
result,
));
}

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

let file_path1 = Path::new("test.txt");
fs.insert(file_path1.into(), *b"content");

let file_path2 = Path::new("test.js");
fs.insert(file_path2.into(), *b"console.log('bar');\n");

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from(&[
("format"),
file_path1.as_os_str().to_str().unwrap(),
file_path2.as_os_str().to_str().unwrap(),
"--files-ignore-unknown=true",
]),
);

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"ignores_unknown_file",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Available options:
The configuration of the filesystem
--files-max-size <NUMBER> The maximum allowed size for source code files in bytes. Files
above this limit will be ignored for performance reason. Defaults to 1 MiB
--files-ignore-unknown <true|false> Tells Rome to not emit diagnostics when handling files
that doesn't know
Options applied to the formatter
--indent-style <tab|space> The indent style.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `test.js`

```js
console.log('bar');

```

## `test.txt`

```txt
content
```

# Emitted Messages

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


2 changes: 2 additions & 0 deletions crates/rome_cli/tests/snapshots/main_commands_ci/ci_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Available options:
The configuration of the filesystem
--files-max-size <NUMBER> The maximum allowed size for source code files in bytes. Files
above this limit will be ignored for performance reason. Defaults to 1 MiB
--files-ignore-unknown <true|false> Tells Rome to not emit diagnostics when handling files
that doesn't know
Options applied to the formatter
--indent-style <tab|space> The indent style.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `test.js`

```js
console.log('bar');

```

## `test.txt`

```txt
content
```

# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× Some errors were emitted while running checks
```

# Emitted Messages

```block
test.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
× File content differs from formatting output
1 │ - console.log('bar');
1 │ + console.log("bar");
2 2 │
```

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


Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Available options:
The configuration of the filesystem
--files-max-size <NUMBER> The maximum allowed size for source code files in bytes. Files
above this limit will be ignored for performance reason. Defaults to 1 MiB
--files-ignore-unknown <true|false> Tells Rome to not emit diagnostics when handling files
that doesn't know
--stdin-file-path <PATH> A file name with its extension to pass when reading from standard
in, e.g. echo 'let a;' | rome format --stdin-file-path=file.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
## `test.js`

```js
console.log('bar');

```

## `test.txt`

```txt
content
```

# Emitted Messages

```block
test.js format ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Formatter would have printed the following content:
1 │ - console.log('bar');
1 │ + console.log("bar");
2 2 │
```

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


Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internalError/io ━━━━━━━━━━━━━━━━━━━━━
# Emitted Messages

```block
Checked 0 file(s) in <TIME>
Compared 0 file(s) in <TIME>
```


6 changes: 3 additions & 3 deletions crates/rome_service/src/configuration/merge.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// Simple trait to merge two configuration types
/// Simple trait to merge two types of the same type
pub trait MergeWith<T> {
/// Merges one configuration type with another
/// Merges one type with another
fn merge_with(&mut self, other: T);

/// Merges one configuration type with another open
/// Merges one type with another, if the condition is met
fn merge_with_if(&mut self, other: T, condition: bool) {
if condition {
self.merge_with(other)
Expand Down
10 changes: 9 additions & 1 deletion crates/rome_service/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,15 @@ pub struct FilesConfiguration {
#[serde(skip_serializing_if = "Option::is_none")]
#[bpaf(hide)]
pub ignore: Option<StringSet>,

/// Tells Rome to not emit diagnostics when handling files that doesn't know
#[serde(skip_serializing_if = "Option::is_none")]
#[bpaf(long("files-ignore-unknown"), argument("true|false"), optional)]
pub ignore_unknown: Option<bool>,
}

impl FilesConfiguration {
const KNOWN_KEYS: &'static [&'static str] = &["maxSize", "ignore"];
const KNOWN_KEYS: &'static [&'static str] = &["maxSize", "ignore", "ignoreUnknown"];
}

impl MergeWith<FilesConfiguration> for FilesConfiguration {
Expand All @@ -258,6 +263,9 @@ impl MergeWith<FilesConfiguration> for FilesConfiguration {
if let Some(max_size) = other.max_size {
self.max_size = Some(max_size)
}
if let Some(ignore_unknown) = other.ignore_unknown {
self.ignore_unknown = Some(ignore_unknown)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/rome_service/src/configuration/parse/json/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ impl VisitNode<JsonLanguage> for FilesConfiguration {
.map_to_index_set_string(&value, name_text, diagnostics)
.map(StringSet::new);
}
"ignoreUnknown" => {
self.ignore_unknown = self.map_to_boolean(&value, name_text, diagnostics);
}
_ => {}
}
Some(())
Expand Down
5 changes: 5 additions & 0 deletions crates/rome_service/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ pub struct FilesSettings {

/// List of paths/files to matcher
pub ignored_files: Matcher,

/// Files not recognized by Rome should not emit a diagnostic
pub ignore_unknown: bool,
}

/// Limit the size of files to 1.0 MiB by default
Expand All @@ -257,6 +260,7 @@ impl Default for FilesSettings {
require_literal_leading_dot: false,
require_literal_separator: false,
}),
ignore_unknown: false,
}
}
}
Expand Down Expand Up @@ -285,6 +289,7 @@ impl TryFrom<FilesConfiguration> for FilesSettings {
Ok(Self {
max_size: config.max_size.unwrap_or(DEFAULT_FILE_SIZE_LIMIT),
ignored_files: matcher,
ignore_unknown: config.ignore_unknown.unwrap_or_default(),
})
}
}
Expand Down
13 changes: 8 additions & 5 deletions crates/rome_service/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,14 @@ impl FileFeaturesResult {
.unwrap_or_default()
}

pub fn is_ignored_for(&self, feature: &FeatureName) -> bool {
self.features_supported
.get(feature)
.map(|support_kind| matches!(support_kind, SupportKind::Ignored))
.unwrap_or_default()
/// Loops through all the features of the current file, and if a feature is [SupportKind::FileNotSupported],
/// it gets changed to [SupportKind::Ignored]
pub fn ignore_not_supported(&mut self) {
for support_kind in self.features_supported.values_mut() {
if matches!(support_kind, SupportKind::FileNotSupported) {
*support_kind = SupportKind::Ignored;
}
}
}

pub fn support_kind_for(&self, feature: &FeatureName) -> Option<&SupportKind> {
Expand Down
Loading

0 comments on commit dad13a9

Please sign in to comment.