1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

fix(cli/repl): ignore pair matching inside literals (#8037)

This commit is contained in:
Casper Beyer 2020-10-20 01:13:23 +08:00 committed by GitHub
parent 08441b855d
commit 9d664f8375
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 9 deletions

View file

@ -33,7 +33,33 @@ impl Validator for Helper {
ctx: &mut ValidationContext, ctx: &mut ValidationContext,
) -> Result<ValidationResult, ReadlineError> { ) -> Result<ValidationResult, ReadlineError> {
let mut stack: Vec<char> = Vec::new(); let mut stack: Vec<char> = Vec::new();
let mut literal: Option<char> = None;
let mut escape: bool = false;
for c in ctx.input().chars() { for c in ctx.input().chars() {
if escape {
escape = false;
continue;
}
if c == '\\' {
escape = true;
continue;
}
if let Some(v) = literal {
if c == v {
literal = None
}
continue;
} else {
literal = match c {
'`' | '"' | '/' | '\'' => Some(c),
_ => None,
};
}
match c { match c {
'(' | '[' | '{' => stack.push(c), '(' | '[' | '{' => stack.push(c),
')' | ']' | '}' => match (stack.pop(), c) { ')' | ']' | '}' => match (stack.pop(), c) {
@ -51,19 +77,11 @@ impl Validator for Helper {
)))) ))))
} }
}, },
'`' => {
if stack.is_empty() || stack.last().unwrap() != &c {
stack.push(c);
} else {
stack.pop();
}
}
_ => {} _ => {}
} }
} }
if !stack.is_empty() { if !stack.is_empty() || literal == Some('`') {
return Ok(ValidationResult::Incomplete); return Ok(ValidationResult::Incomplete);
} }

View file

@ -1139,6 +1139,14 @@ fn repl_test_pty_multiline() {
master.write_all(b"(\n1 + 2\n)\n").unwrap(); master.write_all(b"(\n1 + 2\n)\n").unwrap();
master.write_all(b"{\nfoo: \"foo\"\n}\n").unwrap(); master.write_all(b"{\nfoo: \"foo\"\n}\n").unwrap();
master.write_all(b"`\nfoo\n`\n").unwrap(); master.write_all(b"`\nfoo\n`\n").unwrap();
master.write_all(b"`\n\\`\n`\n").unwrap();
master.write_all(b"'{'\n").unwrap();
master.write_all(b"'('\n").unwrap();
master.write_all(b"'['\n").unwrap();
master.write_all(b"/{/'\n").unwrap();
master.write_all(b"/(/'\n").unwrap();
master.write_all(b"/[/'\n").unwrap();
master.write_all(b"console.log(\"{test1} abc {test2} def {{test3}}\".match(/{([^{].+?)}/));\n").unwrap();
master.write_all(b"close();\n").unwrap(); master.write_all(b"close();\n").unwrap();
let mut output = String::new(); let mut output = String::new();
@ -1147,6 +1155,14 @@ fn repl_test_pty_multiline() {
assert!(output.contains('3')); assert!(output.contains('3'));
assert!(output.contains("{ foo: \"foo\" }")); assert!(output.contains("{ foo: \"foo\" }"));
assert!(output.contains("\"\\nfoo\\n\"")); assert!(output.contains("\"\\nfoo\\n\""));
assert!(output.contains("\"\\n`\\n\""));
assert!(output.contains("\"{\""));
assert!(output.contains("\"(\""));
assert!(output.contains("\"[\""));
assert!(output.contains("/{/"));
assert!(output.contains("/(/"));
assert!(output.contains("/{/"));
assert!(output.contains("[ \"{test1}\", \"test1\" ]"));
fork.wait().unwrap(); fork.wait().unwrap();
} else { } else {