Move check to build script

This commit is contained in:
Ludeeus 2020-08-06 08:21:48 +00:00
parent a12f007110
commit 04df530fdd
2 changed files with 10 additions and 1 deletions

View File

@ -16,6 +16,11 @@ function hasHtml(data) {
return /<[a-z][\s\S]*>/i.test(data); return /<[a-z][\s\S]*>/i.test(data);
} }
function hasUnquotedHTags(data) {
// Guard against unquoted HTML tags https://github.com/formatjs/formatjs/issues/1878
return /[^'](<.*>)[^']/i.test(data);
}
function recursiveCheckHasHtml(file, data, errors, recKey) { function recursiveCheckHasHtml(file, data, errors, recKey) {
Object.keys(data).forEach(function (key) { Object.keys(data).forEach(function (key) {
if (typeof data[key] === "object") { if (typeof data[key] === "object") {
@ -23,6 +28,10 @@ function recursiveCheckHasHtml(file, data, errors, recKey) {
recursiveCheckHasHtml(file, data[key], errors, nextRecKey); recursiveCheckHasHtml(file, data[key], errors, nextRecKey);
} else if (hasHtml(data[key])) { } else if (hasHtml(data[key])) {
errors.push(`HTML found in ${file.path} at key ${recKey}.${key}`); errors.push(`HTML found in ${file.path} at key ${recKey}.${key}`);
} else if (hasUnquotedHTags(data[key])) {
errors.push(
`Unquoted tags found in ${file.path} at key ${recKey}.${key}`
);
} }
}); });
} }

View File

@ -64,7 +64,7 @@ export const computeLocalize = (
if (!translatedMessage) { if (!translatedMessage) {
translatedMessage = new IntlMessageFormat( translatedMessage = new IntlMessageFormat(
translatedValue.replace(/[^'](<.*>)[^']/g, "'$1'"), translatedValue,
language, language,
formats formats
); );