Skip to main content

Allow files without a trailing semicolon

ID
815cb70
date
2024-08-17 06:47:12+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
5117b31
message
Allow files without a trailing semicolon
changed files
2 files, 2 additions, 6 deletions

Changed files

src/javascript/__init__.py (1565) → src/javascript/__init__.py (1442)

diff --git a/src/javascript/__init__.py b/src/javascript/__init__.py
index 1d8a43d..aa95ed5 100644
--- a/src/javascript/__init__.py
+++ b/src/javascript/__init__.py
@@ -28,9 +28,6 @@ def read_js(p: pathlib.Path | str, *, varname: str) -> typing.Any:
             f"File {p} does not start with JavaScript `const` declaration!"
         )
 
-    if not contents.rstrip().endswith(";"):
-        raise ValueError(f"File {p} does not end with a trailing semicolon!")
-
     json_string = contents.replace(f"const {varname} = ", "").rstrip().rstrip(";")
 
     return json.loads(json_string)

tests/test_javascript.py (2603) → tests/test_javascript.py (2546)

diff --git a/tests/test_javascript.py b/tests/test_javascript.py
index 711f1f3..97a6d93 100644
--- a/tests/test_javascript.py
+++ b/tests/test_javascript.py
@@ -36,12 +36,11 @@ class TestReadJs:
         ):
             read_js(js_path, varname="blueTriangle")
 
-    def test_no_trailing_semicolon_is_error(self, tmp_path: pathlib.Path) -> None:
+    def test_allows_trailing_semicolon(self, tmp_path: pathlib.Path) -> None:
         js_path = tmp_path / "shape.js"
         js_path.write_text('const redPentagon = {\n  "sides": 5,\n  "colour": "red"\n}')
 
-        with pytest.raises(ValueError, match="does not end with a trailing semicolon"):
-            read_js(js_path, varname="redPentagon")
+        assert read_js(js_path, varname="redPentagon") == {"sides": 5, "colour": "red"}
 
 
 class TestWriteJs: