Commit 0020444
authored
rdf/turtle: emit a space before ';' and '.' terminators (JavaScriptSolidServer#419) (JavaScriptSolidServer#420)
* rdf/turtle: emit a space before `;` and `.` terminators (JavaScriptSolidServer#419)
Match the de-facto Turtle style used in W3C 1.1 spec examples,
Apache Jena's RIOT writer, and most hand-authored Turtle in the
Solid / linked-data ecosystem: a space between the previous
token and the statement terminator.
Before:
<s> foaf:name "Alice";
foaf:age 30.
After:
<s> foaf:name "Alice" ;
foaf:age 30 .
n3.js's writer hardcodes the no-space form (`;\n next`) and
exposes no config knob. Approach: a literal-aware post-pass on
the writer's output.
The naïve `\S;\n` → `\S ;\n` regex is unsafe — string literals
(especially triple-quoted) can contain `;\n` or `.\n` internally,
and inserting a space inside a literal would silently CHANGE the
literal's value. So:
1. Stash every string literal AND every <IRI> into placeholders
using a multi-character non-token-boundary sentinel
bracketed by NULs (n3.js escapes raw NUL inside literals
and never emits one in real Turtle output, so the sentinel
can't collide with real content).
2. Apply the spacing regex to the redacted output. Now `;`/`.`
only appear as actual statement terminators because all
literal/IRI internals are hidden.
3. Restore placeholders.
Stash order matters — triple-quoted before single-quoted, else
`"""` looks like an empty `""` followed by `"` to the
single-quoted regex. Same for triple-vs-single apostrophe.
Tests:
- "emits a space before ; and . terminators": positive
- "does NOT add a space inside literals containing ; or .":
safety regression — the post-pass MUST NOT corrupt literal
values like `"foo;bar"`, `"has.dot"`, `"a;b.c"`.
- "does NOT add a space inside an IRI containing ;": safety
regression for IRIs like `<https://example.test/path;with;semis>`.
Test count: 770 → 777 in full suite (+7: 3 JavaScriptSolidServer#419 tests + 4 JavaScriptSolidServer#416/JavaScriptSolidServer#417/JavaScriptSolidServer#415 follow-ups since main).
* Address copilot pass 1 on JavaScriptSolidServer#420 — assert by value, not by lexical form
The two safety regression tests for JavaScriptSolidServer#419 hardcoded that N3
serializes string literals with double quotes (`"foo;bar"`)
and IRIs as `<...>`. That made them sensitive to N3 writer
formatting choices — single quotes, long-string
`"""..."""`, IRI escaping, etc. — even when the underlying
literal value would still be preserved correctly.
Fix: parse the emitted Turtle back to quads and assert the
literal/IRI VALUES, not their lexical form. The properties
the tests actually want to enforce are:
- "foo;bar" round-trips as a literal whose value is foo;bar
(no inserted space)
- <https://example.test/path;with;semis> round-trips as a
NamedNode with that exact IRI
Both are now assertion-by-value. Resilient against any future
N3 upgrade that changes quote style, IRI escaping, or the long-
string threshold.
Test count: same 777.
* Address copilot pass 2 on JavaScriptSolidServer#420 — pin terminator presence
The "emits a space before ; and ." test asserted no offending
terminators (without preceding whitespace) but didn't assert
the terminators were actually present. If a future N3 upgrade
ever switched to the "one triple per statement, no ;
continuations" output style, the negative assertions would
pass vacuously and the test would stop exercising the spacing
behavior at all.
Fix: pin the presence of at least one ` ;` and one ` .` (with
preceding whitespace) before checking for offending forms. Now
the test fails loudly if either disappears.
Test count: same 777.
* Address copilot pass 3 on JavaScriptSolidServer#420 — require literal space, not any whitespace
The pass-2 assertions used `\s` / `[\s]` patterns, which would
treat `\n;`, `\t;`, etc. as acceptable too. The JavaScriptSolidServer#419 intent is
specifically a single SPACE before the terminator (matching
W3C Turtle 1.1 examples and Apache Jena's RIOT writer):
`value ;` / `value .`
If a future N3 upgrade ever emitted `\n;` or `\t;`, the pass-2
test would have passed despite the visually-different output.
Fix: tighten the assertions to require a literal ` ;` / ` .`
(a single space). Both presence-checks and the offending-form
negative checks use ` ` (space) explicitly instead of `\s`.
Test count: same 777.1 parent d783ffd commit 0020444
2 files changed
Lines changed: 89 additions & 0 deletions
Binary file not shown.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
156 | 245 | | |
157 | 246 | | |
158 | 247 | | |
| |||
0 commit comments