Skip to content

Commit

Permalink
Added tests for: For # to start a comment, then it either need to b…
Browse files Browse the repository at this point in the history
…e the first character of the token or prefixed with ` ` (space). (nushell#10327)
  • Loading branch information
RobbingDaHood committed Dec 7, 2024
1 parent b757646 commit bde96f3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
4 changes: 1 addition & 3 deletions crates/nu-parser/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ pub fn lex_item(
if is_item_terminator(&block_level, c, additional_whitespace, special_tokens) {
break;
}
in_comment = previous_char
.map(|pc| pc == b' ')
.unwrap_or(true);
in_comment = previous_char.map(|pc| pc == b' ').unwrap_or(true);
} else if c == b'\n' || c == b'\r' {
in_comment = false;
if is_item_terminator(&block_level, c, additional_whitespace, special_tokens) {
Expand Down
23 changes: 23 additions & 0 deletions crates/nu-parser/tests/test_lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,29 @@ fn lex_comment() {
);
}

#[test]
fn lex_not_comment_needs_space_in_front_of_hashtag() {
let file = b"1..10 | each {echo test#testing }";

let output = lex(file, 0, &[], &[], false);

assert!(output.1.is_none());
}

#[test]
fn lex_comment_with_space_in_front_of_hashtag() {
let file = b"1..10 | each {echo test #testing }";

let output = lex(file, 0, &[], &[], false);

assert!(output.1.is_some());
assert!(matches!(
output.1.unwrap(),
ParseError::UnexpectedEof(missing_token, span) if missing_token == "}"
&& span == Span::new(33, 34)
));
}

#[test]
fn lex_is_incomplete() {
let file = b"let x = 300 | ;";
Expand Down
23 changes: 23 additions & 0 deletions tests/repl/test_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,24 @@ fn comment_skipping_in_pipeline_3() -> TestResult {
)
}

#[test]
fn still_string_if_hashtag_is_middle_of_string() -> TestResult {
run_test(r#"echo test#testing"#, "test#testing")
}

#[test]
fn still_string_if_hashtag_is_middle_of_string_inside_each() -> TestResult {
run_test(
r#"1..1 | each {echo test#testing } | get 0"#,
"test#testing",
)
}

#[test]
fn still_string_if_hashtag_is_middle_of_string_inside_each_also_with_dot() -> TestResult {
run_test(r#"1..1 | each {echo '.#testing' } | get 0"#, ".#testing")
}

#[test]
fn bad_var_name() -> TestResult {
fail_test(r#"let $"foo bar" = 4"#, "can't contain")
Expand Down Expand Up @@ -282,6 +300,11 @@ fn raw_string_with_equals() -> TestResult {
)
}

#[test]
fn raw_string_with_hashtag() -> TestResult {
run_test(r#"r##' one # two '##"#, "one # two")
}

#[test]
fn list_quotes_with_equals() -> TestResult {
run_test(
Expand Down

0 comments on commit bde96f3

Please sign in to comment.