Repository: Nykakin/chompjs (language: python)

Issue:
Error when parsing function value without a comma
```py
src = '''
	{
		a: my_func
	}
'''

print(chompjs.parse_js_object(src))
```

Results in: `json.decoder.JSONDecodeError: Invalid control character`

Whereas each of the following works and results in: `{'a': 'my_func'}`

```py
src = '''
	{
		a: my_func,
	}
'''

print(chompjs.parse_js_object(src))
```

Note the trailing comma in the example above.

```py
src = '''
	{
		a: my_func
	}
'''

print(chompjs.parse_js_object(src.replace('\t', '').replace('\n', '')))
```

```py
src = '''
	{
		a: my_func}
'''

print(chompjs.parse_js_object(src))
```

There are various combinations to try. I think from my brief testing, a tab or a newline after a function value without a comma results in an error. In other words, a function value either has to be immediately followed by a comma, space, or a closing bracket.


Discussion / hints:

Fix patch:
diff --git a/_chompjs/parser.c b/_chompjs/parser.c
index 64264d5..3e8c624 100644
--- a/_chompjs/parser.c
+++ b/_chompjs/parser.c
@@ -381,6 +381,10 @@ struct State* handle_unrecognized(struct Lexer* lexer) {
                     emit(c, lexer);
                     lexer->unrecognized_nesting_depth -= 1;
                 } else {
+                    // remove trailing whitespaces after value
+                    while(isspace(last_char(lexer))) {
+                        pop(&lexer->output);
+                    }
                     emit_in_place('"', lexer);
                     return &states[JSON_STATE];
                 }
@@ -389,7 +393,7 @@ struct State* handle_unrecognized(struct Lexer* lexer) {
             case ',':
             case ':':
                 if(!currently_quoted_with && lexer->unrecognized_nesting_depth <= 0) {
-                    // remove trailing whitespaces
+                    // remove trailing whitespaces after key
                     while(isspace(last_char(lexer))) {
                         pop(&lexer->output);
                     }


Test patch:
diff --git a/chompjs/test_parser.py b/chompjs/test_parser.py
index a0c9140..6bbc9be 100644
--- a/chompjs/test_parser.py
+++ b/chompjs/test_parser.py
@@ -110,6 +110,12 @@ def test_parse_nan(self):
         ("{'a': 121.}", {'a': 121.0}),
         ("{abc : 100}", {'abc': 100}),
         ("{abc     :       100}", {'abc': 100}),
+        ("{abc: name }", {'abc': "name"}),
+        ("{abc: name\t}", {'abc': "name"}),
+        ("{abc: value\n}", {'abc': "value"}),
+        ("{abc:  name}", {'abc': "name"}),
+        ("{abc: \tname}", {'abc': "name"}),
+        ("{abc: \nvalue}", {'abc': "value"}),
     )
     def test_parse_strange_values(self, in_data, expected_data):
         result = parse_js_object(in_data)