Skip to content

Commit 87176d7

Browse files
committed
refactor: adapt new xmldom api
1 parent b294dae commit 87176d7

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

src/index.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,26 @@ export function xmlValidator(
4949

5050
try {
5151
new DOMParser({
52-
errorHandler: (level, message) => {
52+
onError: (level: string, message: string) => {
5353
const replacedMessage = message.replace(/\[xmldom (warning|.*Error)\]\s+/g, '') ?? '';
5454

5555
errorList.push(`${underline(file.relative)}: <${level}> ${replacedMessage}`);
5656
},
5757
}).parseFromString(file.contents.toString(), options?.mimeType ?? 'text/xml');
5858
} catch (error) {
59-
this.emit(
60-
'error',
61-
new PluginError(packageName, error as Error, {
62-
fileName: file.path,
63-
}),
64-
);
59+
if (error instanceof Error) {
60+
errorList.push(`${underline(file.relative)}: <fatalError> ${error.message}`);
61+
}
6562
}
6663

6764
if (errorList && errorList.length > 0) {
68-
this.emit(
69-
'error',
65+
callback(
7066
new PluginError(packageName, `\n${errorList.join('\n')}`, {
7167
fileName: file.path,
7268
showStack: false,
7369
}),
7470
);
71+
return;
7572
}
7673

7774
callback(null, file);

tests/test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test('fail on mismatching tags', async () => {
4343
.once('error', (error: PluginError) => resolve(error));
4444
});
4545

46-
assert.ok(message.includes('\x1B[4mmismatching_tags.xml\x1B[24m: <warning> unclosed xml attribute'));
46+
assert.ok(message.includes('\x1B[4mmismatching_tags.xml\x1B[24m: <fatalError> Opening and ending tag mismatch'));
4747
});
4848

4949
test('fail on missing close tags', async () => {
@@ -56,7 +56,7 @@ test('fail on missing close tags', async () => {
5656
.once('error', (error: PluginError) => resolve(error));
5757
});
5858

59-
assert.ok(message.includes('\x1B[4mmissing_close_tag.xml\x1B[24m: <warning> unclosed xml attribute'));
59+
assert.ok(message.includes('\x1B[4mmissing_close_tag.xml\x1B[24m: <fatalError>'));
6060
});
6161

6262
test('fail on missing quote', async () => {
@@ -71,7 +71,7 @@ test('fail on missing quote', async () => {
7171

7272
assert.ok(
7373
message.includes(
74-
`\x1B[4mmissing_quote.xml\x1B[24m: <error> [xmldom error]\telement parse error: Error: attribute value no end '"' match`,
74+
`\x1B[4mmissing_quote.xml\x1B[24m: <error> element parse error: Error: attribute value no end '"' match`,
7575
),
7676
);
7777
});
@@ -86,11 +86,7 @@ test('fail on invalid tag', async () => {
8686
.once('error', (error: PluginError) => resolve(error));
8787
});
8888

89-
assert.ok(
90-
message.includes(
91-
'\x1B[4minvalid_tag.xml\x1B[24m: <error> [xmldom error]\telement parse error: Error: invalid tagName:1',
92-
),
93-
);
89+
assert.ok(message.includes('\x1B[4minvalid_tag.xml\x1B[24m: <error> element parse error: Error: invalid tagName:1'));
9490
});
9591

9692
test.run();

0 commit comments

Comments
 (0)