Skip to content

Commit c4ab1ce

Browse files
committed
fix: failed to keep block comment when turned off
1 parent bd4f009 commit c4ab1ce

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn main() {
2323
r#"
2424
{
2525
"name": /* full */ "John Doe",
26-
"age": 43,
26+
"age": 43, # hash line comment
2727
"phones": [
2828
"+44 1234567", // work phone
2929
"+44 2345678", // home phone

examples/example.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
r#"
66
{
77
"name": /* full */ "John Doe",
8-
"age": 43,
8+
"age": 43, # hash comment
99
"phones": [
1010
"+44 1234567", // work phone
1111
"+44 2345678", // home phone

src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,9 @@ fn top(c: &mut u8, settings: CommentSettings) -> State {
344344
match *c {
345345
b'"' => InString,
346346
b'/' => {
347-
*c = b' ';
347+
if settings.block_comments || settings.slash_line_comments {
348+
*c = b' ';
349+
}
348350
InComment
349351
}
350352
b'#' if settings.hash_line_comments => {
@@ -507,6 +509,32 @@ mod tests {
507509
assert_eq!(err.kind(), ErrorKind::InvalidData);
508510
}
509511

512+
#[test]
513+
fn keep_all() {
514+
let original = String::from(
515+
r#"
516+
{
517+
"name": /* full */ "John Doe",
518+
"age": 43, # hash line comment
519+
"phones": [
520+
"+44 1234567", // work phone
521+
"+44 2345678", // home phone
522+
], /** comment **/
523+
}"#,
524+
);
525+
let mut changed = original.clone();
526+
let _ = strip_comments_in_place(
527+
&mut changed,
528+
CommentSettings {
529+
block_comments: false,
530+
slash_line_comments: false,
531+
hash_line_comments: false,
532+
trailing_commas: false,
533+
},
534+
);
535+
assert_eq!(original, changed);
536+
}
537+
510538
#[test]
511539
fn strip_in_place() {
512540
let mut json = String::from(r#"{/* Comment */"hi": /** abc */ "bye"}"#);

wasm/test/test.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { strip } from '../../npm/json_strip_comments_wasm.js';
44
const s = `
55
{
66
"name": /* full */ "John Doe",
7-
"age": 43,
7+
"age": 43, # hash line comment
88
"phones": [
99
"+44 1234567", // work phone
1010
"+44 2345678", // home phone
@@ -31,4 +31,4 @@ const expected = `
3131
assert.deepStrictEqual(JSON.parse(stripped), JSON.parse(expected));
3232
});
3333

34-
// assert.strictEqual(strip(s, { blockComments: false, slashLineComments: false, hashLineComments: false, trailingCommas: false }), s);
34+
assert.strictEqual(strip(s, { blockComments: false, slashLineComments: false, hashLineComments: false, trailingCommas: false }), s);

0 commit comments

Comments
 (0)