fix trailing comma (#1574)
Signed-off-by: Jess Frazelle <github@jessfraz.com>
This commit is contained in:
@ -3511,6 +3511,33 @@ show(mySuperCoolPart)
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_recast_trailing_comma() {
|
||||||
|
let some_program_string = r#"startSketchOn('XY')
|
||||||
|
|> startProfileAt([0, 0], %)
|
||||||
|
|> arc({
|
||||||
|
radius: 1,
|
||||||
|
angle_start: 0,
|
||||||
|
angle_end: 180,
|
||||||
|
}, %)"#;
|
||||||
|
let tokens = crate::token::lexer(some_program_string);
|
||||||
|
let parser = crate::parser::Parser::new(tokens);
|
||||||
|
let program = parser.ast().unwrap();
|
||||||
|
|
||||||
|
let recasted = program.recast(&Default::default(), 0);
|
||||||
|
assert_eq!(
|
||||||
|
recasted,
|
||||||
|
r#"startSketchOn('XY')
|
||||||
|
|> startProfileAt([0, 0], %)
|
||||||
|
|> arc({
|
||||||
|
radius: 1,
|
||||||
|
angle_start: 0,
|
||||||
|
angle_end: 180
|
||||||
|
}, %)
|
||||||
|
"#
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_recast_negative_var() {
|
fn test_recast_negative_var() {
|
||||||
let some_program_string = r#"const w = 20
|
let some_program_string = r#"const w = 20
|
||||||
|
@ -440,6 +440,7 @@ fn object(i: TokenSlice) -> PResult<ObjectExpression> {
|
|||||||
"a comma-separated list of key-value pairs, e.g. 'height: 4, width: 3'",
|
"a comma-separated list of key-value pairs, e.g. 'height: 4, width: 3'",
|
||||||
))
|
))
|
||||||
.parse_next(i)?;
|
.parse_next(i)?;
|
||||||
|
ignore_trailing_comma(i);
|
||||||
ignore_whitespace(i);
|
ignore_whitespace(i);
|
||||||
let end = close_brace(i)?.end;
|
let end = close_brace(i)?.end;
|
||||||
Ok(ObjectExpression { start, end, properties })
|
Ok(ObjectExpression { start, end, properties })
|
||||||
@ -977,6 +978,11 @@ fn ignore_whitespace(i: TokenSlice) {
|
|||||||
let _: PResult<()> = repeat(0.., whitespace).parse_next(i);
|
let _: PResult<()> = repeat(0.., whitespace).parse_next(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A helper function to ignore a trailing comma.
|
||||||
|
fn ignore_trailing_comma(i: TokenSlice) {
|
||||||
|
let _ = opt(comma).parse_next(i);
|
||||||
|
}
|
||||||
|
|
||||||
/// Matches at least 1 whitespace.
|
/// Matches at least 1 whitespace.
|
||||||
fn require_whitespace(i: TokenSlice) -> PResult<()> {
|
fn require_whitespace(i: TokenSlice) -> PResult<()> {
|
||||||
repeat(1.., whitespace).parse_next(i)
|
repeat(1.., whitespace).parse_next(i)
|
||||||
|
Reference in New Issue
Block a user