node-reporter-sonarqube
Version:
Node test reporter for SonarQube
39 lines • 1.25 kB
JavaScript
import { fileURLToPath } from 'node:url';
export function escapeXmlAttribute(value) {
const escapeMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
return value.replace(/[&<>"']/gu, (char) => escapeMap[char]);
}
export function tag(name, attrs = {}, close = true, content = undefined) {
const end = close && !content ? '/>' : '>';
const pairs = [];
for (const key in attrs) {
if (Object.hasOwn(attrs, key)) {
const k = escapeXmlAttribute(key);
const v = escapeXmlAttribute(attrs[key]);
pairs.push(`${k}="${v}"`);
}
}
const attrsStr = pairs.length ? ` ${pairs.join(' ')}` : '';
let tag = `<${name}${attrsStr}${end}`;
if (content) {
tag += `${content}</${name}${end}`;
}
return tag;
}
export function getFilename(file) {
if (!file) {
return '-';
}
if (file.startsWith('file://')) {
file = fileURLToPath(file);
}
const prefix = process.env['GITHUB_WORKSPACE'] ?? process.env['npm_config_local_prefix'] ?? process.cwd();
return prefix && file.startsWith(prefix) ? file.slice(prefix.length + 1) : file;
}
//# sourceMappingURL=utils.mjs.map