Skip to content

Commit 8fe330e

Browse files
committed
format
1 parent 2905dba commit 8fe330e

File tree

3 files changed

+94
-75
lines changed

3 files changed

+94
-75
lines changed

src/index.ts

+54-46
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,74 @@ import type Vinyl from 'vinyl';
77

88
type PluginOptions = {
99
mimeType?: string;
10-
}
10+
};
1111

1212
/**
1313
* Gulp plugin to validate XML files using the xmldom library.
1414
* @returns {Transform} A transform stream that validates XML files.
1515
*/
16-
export function xmlValidator(options: PluginOptions = {
17-
mimeType: 'text/xml'
18-
}): Transform {
16+
export function xmlValidator(
17+
options: PluginOptions = {
18+
mimeType: 'text/xml',
19+
},
20+
): Transform {
1921
const packageName = 'gulp-xml-validator';
2022

2123
return new Transform({
22-
objectMode: true,
24+
objectMode: true,
2325

24-
/**
25-
* Transform function for the Gulp plugin.
26-
* @param {Vinyl} file - The vinyl file being processed.
27-
* @param {BufferEncoding} _encoding - The encoding of the file.
28-
* @param {TransformCallback} callback - The callback function to signal the completion of the transformation.
29-
*/
30-
transform(file: Vinyl, _encoding: BufferEncoding, callback: TransformCallback) {
31-
if (file.isNull()) {
32-
callback(null, file);
33-
return;
34-
}
26+
/**
27+
* Transform function for the Gulp plugin.
28+
* @param {Vinyl} file - The vinyl file being processed.
29+
* @param {BufferEncoding} _encoding - The encoding of the file.
30+
* @param {TransformCallback} callback - The callback function to signal the completion of the transformation.
31+
*/
32+
transform(file: Vinyl, _encoding: BufferEncoding, callback: TransformCallback) {
33+
if (file.isNull()) {
34+
callback(null, file);
35+
return;
36+
}
3537

36-
if (file.isStream()) {
37-
callback(new PluginError(packageName, 'Streaming not supported'));
38-
return;
39-
}
38+
if (file.isStream()) {
39+
callback(new PluginError(packageName, 'Streaming not supported'));
40+
return;
41+
}
4042

41-
if (!file.contents) {
42-
callback(new PluginError(packageName, 'Empty file'));
43-
return;
44-
}
43+
if (!file.contents) {
44+
callback(new PluginError(packageName, 'Empty file'));
45+
return;
46+
}
4547

46-
const errorList: string[] = [];
48+
const errorList: string[] = [];
4749

48-
try {
49-
new DOMParser({
50-
locator: {},
51-
errorHandler: function errorHandler(level, message) {
52-
message = message.replace(/\[xmldom (warning|.*Error)\]\s+/g, '');
53-
errorList.push(`${underline(file.relative)}: <${level}> ${message}`);
54-
}
50+
try {
51+
new DOMParser({
52+
locator: {},
53+
errorHandler: function errorHandler(level, message) {
54+
message = message.replace(/\[xmldom (warning|.*Error)\]\s+/g, '');
55+
errorList.push(`${underline(file.relative)}: <${level}> ${message}`);
56+
},
5557
}).parseFromString(file.contents.toString(), options?.mimeType ?? 'text/xml');
56-
} catch (error) {
57-
this.emit('error', new PluginError(packageName, error as Error, {
58-
fileName: file.path
59-
}));
60-
}
58+
} catch (error) {
59+
this.emit(
60+
'error',
61+
new PluginError(packageName, error as Error, {
62+
fileName: file.path,
63+
}),
64+
);
65+
}
6166

62-
if (errorList && errorList.length > 0) {
63-
this.emit('error', new PluginError(packageName, `\n${errorList.join('\n')}`, {
64-
fileName: file.path,
65-
showStack: false
66-
}));
67-
}
67+
if (errorList && errorList.length > 0) {
68+
this.emit(
69+
'error',
70+
new PluginError(packageName, `\n${errorList.join('\n')}`, {
71+
fileName: file.path,
72+
showStack: false,
73+
}),
74+
);
75+
}
6876

69-
callback(null, file);
70-
}
71-
});
77+
callback(null, file);
78+
},
79+
});
7280
}

tests/test.ts

+32-17
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ function resolveFixture(fileName) {
1212
test('should emit error on streamed file', async () => {
1313
const fixture = resolveFixture('valid.xml');
1414

15-
const { message } = await new Promise(resolve => {
16-
gulp.src(fixture, { buffer: false })
15+
const { message } = await new Promise((resolve) => {
16+
gulp
17+
.src(fixture, { buffer: false })
1718
.pipe(xmlValidator())
18-
.once('error', error => resolve(error));
19+
.once('error', (error) => resolve(error));
1920
});
2021

2122
assert.is(message, 'Streaming not supported');
@@ -24,19 +25,22 @@ test('should emit error on streamed file', async () => {
2425
test('pass on valid xml', async () => {
2526
const fixture = resolveFixture('valid.xml');
2627

27-
assert.not.throws(async () => await new Promise(() => {
28-
gulp.src(fixture)
29-
.pipe(xmlValidator());
30-
}));
28+
assert.not.throws(
29+
async () =>
30+
await new Promise(() => {
31+
gulp.src(fixture).pipe(xmlValidator());
32+
}),
33+
);
3134
});
3235

3336
test('fail on mismatching tags', async () => {
3437
const fixture = resolveFixture('mismatching_tags.xml');
3538

3639
const { message } = await new Promise((resolve) => {
37-
gulp.src(fixture)
40+
gulp
41+
.src(fixture)
3842
.pipe(xmlValidator())
39-
.once('error', error => resolve(error));
43+
.once('error', (error) => resolve(error));
4044
});
4145

4246
assert.ok(message.includes('\x1B[4mmismatching_tags.xml\x1B[24m: <warning> unclosed xml attribute'));
@@ -46,9 +50,10 @@ test('fail on missing close tags', async () => {
4650
const fixture = resolveFixture('missing_close_tag.xml');
4751

4852
const { message } = await new Promise((resolve) => {
49-
gulp.src(fixture)
53+
gulp
54+
.src(fixture)
5055
.pipe(xmlValidator())
51-
.once('error', error => resolve(error));
56+
.once('error', (error) => resolve(error));
5257
});
5358

5459
assert.ok(message.includes('\x1B[4mmissing_close_tag.xml\x1B[24m: <warning> unclosed xml attribute'));
@@ -58,24 +63,34 @@ test('fail on missing quote', async () => {
5863
const fixture = resolveFixture('missing_quote.xml');
5964

6065
const { message } = await new Promise((resolve) => {
61-
gulp.src(fixture)
66+
gulp
67+
.src(fixture)
6268
.pipe(xmlValidator())
63-
.once('error', error => resolve(error));
69+
.once('error', (error) => resolve(error));
6470
});
6571

66-
assert.ok(message.includes(`\x1B[4mmissing_quote.xml\x1B[24m: <error> [xmldom error]\telement parse error: Error: attribute value no end '"' match`));
72+
assert.ok(
73+
message.includes(
74+
`\x1B[4mmissing_quote.xml\x1B[24m: <error> [xmldom error]\telement parse error: Error: attribute value no end '"' match`,
75+
),
76+
);
6777
});
6878

6979
test('fail on invalid tag', async () => {
7080
const fixture = resolveFixture('invalid_tag.xml');
7181

7282
const { message } = await new Promise((resolve) => {
73-
gulp.src(fixture)
83+
gulp
84+
.src(fixture)
7485
.pipe(xmlValidator())
75-
.once('error', error => resolve(error));
86+
.once('error', (error) => resolve(error));
7687
});
7788

78-
assert.ok(message.includes('\x1B[4minvalid_tag.xml\x1B[24m: <error> [xmldom error]\telement parse error: Error: invalid tagName:1'));
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+
);
7994
});
8095

8196
test.run();

tsup.config.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@ import { defineConfig } from 'tsup';
22

33
export default defineConfig({
44
target: 'esnext',
5-
clean: true,
6-
dts: true,
7-
entry: ['src/index.ts'],
8-
external: [
9-
'@xmldom/xmldom',
10-
'kleur',
11-
'external:plugin-error'
12-
],
13-
format: 'esm',
14-
minify: true,
15-
outDir: 'dist',
5+
clean: true,
6+
dts: true,
7+
entry: ['src/index.ts'],
8+
external: ['@xmldom/xmldom', 'kleur', 'external:plugin-error'],
9+
format: 'esm',
10+
minify: true,
11+
outDir: 'dist',
1612
platform: 'node',
17-
treeshake: 'recommended'
13+
treeshake: 'recommended',
1814
});

0 commit comments

Comments
 (0)