Skip to content

Commit 8a8ff73

Browse files
committed
Add type checking script and improve error handling for attribute parsing in XML parser
1 parent 4cad2d5 commit 8a8ff73

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "XML parser that 100% retains all formatting for creating identical XML on roundtrips",
55
"scripts": {
66
"test": "bun test --coverage",
7-
"format": "prettier --write ."
7+
"format": "prettier --write .",
8+
"typecheck": "tsc --noEmit"
89
},
910
"keywords": [
1011
"XML",

xmlParser.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class XmlParser {
145145
}
146146
const wsBeforeEqual = this.readWhitespace();
147147
let wsAfterEqual = '';
148-
let quote = '';
148+
let quote = '"';
149149
let value = '';
150150
if (this.peek() === '=') {
151151
this.pos++; // skip "="
@@ -163,8 +163,9 @@ export class XmlParser {
163163
value = this.input.substring(startVal, endVal);
164164
this.pos = endVal + 1; // skip closing quote
165165
} else {
166-
// Unquoted attribute value.
167-
value = this.readUntil(/[\s>]/);
166+
throw new Error(
167+
`Expected quote character at position ${this.pos} for attribute "${attrName}"`,
168+
);
168169
}
169170
}
170171
attributes.push(
@@ -174,7 +175,7 @@ export class XmlParser {
174175
leadingWs,
175176
wsBeforeEqual,
176177
wsAfterEqual,
177-
quote,
178+
quote === '"' || quote === "'" ? quote : '"',
178179
),
179180
);
180181
}

0 commit comments

Comments
 (0)