Skip to content

Commit

Permalink
fix(tasks): escape more special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Dec 3, 2023
1 parent 81cc6a6 commit b04c4f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
10 changes: 1 addition & 9 deletions tasks/prettier_conformance/prettier.snap.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Compatibility: 209/561 (37.25%)
Compatibility: 215/561 (38.32%)

# Failed

### arrays
* arrays/empty.js
* arrays/issue-10159.js
* arrays/numbers-in-args.js
* arrays/numbers-negative-comment-after-minus.js
* arrays/numbers-negative.js
Expand Down Expand Up @@ -45,9 +44,7 @@ Compatibility: 209/561 (37.25%)
* assignment/chain.js
* assignment/discussion-15196.js
* assignment/issue-10218.js
* assignment/issue-15534.js
* assignment/issue-2482-2.js
* assignment/issue-2540.js
* assignment/issue-3819.js
* assignment/issue-6922.js
* assignment/issue-7572.js
Expand Down Expand Up @@ -322,7 +319,6 @@ Compatibility: 209/561 (37.25%)
* method-chain/inline_merge.js
* method-chain/issue-11298.js
* method-chain/issue-3594.js
* method-chain/issue-3621.js
* method-chain/issue-4125.js
* method-chain/logical.js
* method-chain/multiple-members.js
Expand Down Expand Up @@ -409,9 +405,6 @@ Compatibility: 209/561 (37.25%)
* quotes/objects.js
* quotes/strings.js

### regex
* regex/test.js

### require-amd
* require-amd/named-amd-module.js
* require-amd/require.js
Expand All @@ -433,7 +426,6 @@ Compatibility: 209/561 (37.25%)
### strings
* strings/escaped.js
* strings/multiline-literal.js
* strings/non-octal-eight-and-nine.js
* strings/strings.js
* strings/template-literals.js

Expand Down
21 changes: 13 additions & 8 deletions tasks/prettier_conformance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,16 @@ impl TestRunner {
if snapshot_line.is_empty() { String::new() } else { title_snapshot_options }
);

let mut output = Self::prettier(path, input, prettier_options).replace('`', "\\`");
let mut input = input.replace('`', "\\`");
let need_eol_visualized = snap_content.contains("<LF>");
let output = Self::prettier(path, input, prettier_options);
let output = Self::escape_and_convert_snap_string(&output, need_eol_visualized);
let input = Self::escape_and_convert_snap_string(input, need_eol_visualized);
let snapshot_options = snapshot_options
.iter()
.map(|(k, v)| format!("{k}: {v}"))
.collect::<Vec<_>>()
.join("\n");

let need_eol_visualized = snap_content.contains("<LF>");
if need_eol_visualized {
input = Self::visualize_end_of_line(&input);
output = Self::visualize_end_of_line(&output);
}

let space_line = " ".repeat(prettier_options.print_width);
let snapshot_without_output = format!(
r#"
Expand Down Expand Up @@ -368,6 +364,15 @@ impl TestRunner {
result
}

fn escape_and_convert_snap_string(input: &str, need_eol_visualized: bool) -> String {
let input = input.replace('\\', "\\\\").replace('`', "\\`").replace("${", "\\${");
if need_eol_visualized {
Self::visualize_end_of_line(&input)
} else {
input
}
}

fn prettier(path: &Path, source_text: &str, prettier_options: PrettierOptions) -> String {
let allocator = Allocator::default();
let source_type = SourceType::from_path(path).unwrap();
Expand Down

0 comments on commit b04c4f7

Please sign in to comment.