Permitting whitespace before colon (#4171)

Fixes #4150 by ignoring whitespace before a colon just like how whitespace after a colon is already ignored.
Added snapshot parser::parser_impl::snapshot_tests::bh for a kcl snippet that has whitespace before a colon.

Co-authored-by: 49fl <ircsurfer33@gmail.com>
This commit is contained in:
Arnav Gupta
2024-10-16 10:00:52 -07:00
committed by GitHub
parent 248ef8ebb3
commit 1c2e415c70
2 changed files with 113 additions and 0 deletions

View File

@ -567,6 +567,7 @@ fn array_end_start(i: TokenSlice) -> PResult<ArrayRangeExpression> {
fn object_property(i: TokenSlice) -> PResult<ObjectProperty> {
let key = identifier.context(expected("the property's key (the name or identifier of the property), e.g. in 'height: 4', 'height' is the property key")).parse_next(i)?;
ignore_whitespace(i);
colon
.context(expected(
"a colon, which separates the property's key from the value you're setting it to, e.g. 'height: 4'",
@ -3568,6 +3569,7 @@ const my14 = 4 ^ 2 - 3 ^ 2 * 2
snapshot_test!(be, "let x = 3 == 3");
snapshot_test!(bf, "let x = 3 != 3");
snapshot_test!(bg, r#"x = 4"#);
snapshot_test!(bh, "const obj = {center : [10, 10], radius: 5}");
}
#[allow(unused)]

View File

@ -0,0 +1,111 @@
---
source: kcl/src/parser/parser_impl.rs
assertion_line: 3718
expression: actual
---
{
"start": 0,
"end": 42,
"body": [
{
"type": "VariableDeclaration",
"type": "VariableDeclaration",
"start": 0,
"end": 42,
"declarations": [
{
"type": "VariableDeclarator",
"start": 6,
"end": 42,
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"name": "obj",
"digest": null
},
"init": {
"type": "ObjectExpression",
"type": "ObjectExpression",
"start": 12,
"end": 42,
"properties": [
{
"type": "ObjectProperty",
"start": 13,
"end": 30,
"key": {
"type": "Identifier",
"start": 13,
"end": 19,
"name": "center",
"digest": null
},
"value": {
"type": "ArrayExpression",
"type": "ArrayExpression",
"start": 22,
"end": 30,
"elements": [
{
"type": "Literal",
"type": "Literal",
"start": 23,
"end": 25,
"value": 10,
"raw": "10",
"digest": null
},
{
"type": "Literal",
"type": "Literal",
"start": 27,
"end": 29,
"value": 10,
"raw": "10",
"digest": null
}
],
"digest": null
},
"digest": null
},
{
"type": "ObjectProperty",
"start": 32,
"end": 41,
"key": {
"type": "Identifier",
"start": 32,
"end": 38,
"name": "radius",
"digest": null
},
"value": {
"type": "Literal",
"type": "Literal",
"start": 40,
"end": 41,
"value": 5,
"raw": "5",
"digest": null
},
"digest": null
}
],
"digest": null
},
"digest": null
}
],
"kind": "const",
"digest": null
}
],
"nonCodeMeta": {
"nonCodeNodes": {},
"start": [],
"digest": null
},
"digest": null
}