fix(tools): upgrade warning to throw (#40880)

This commit is contained in:
Oliver Eyton-Williams
2021-02-03 10:43:32 +01:00
committed by GitHub
parent 1a642ba542
commit 7ef29e62a8
2 changed files with 42 additions and 24 deletions

View File

@ -137,8 +137,8 @@ function translateGeneric(text, config, regexBefore, regexAfter) {
for (const [match, before, comment, after] of matches) { for (const [match, before, comment, after] of matches) {
if (knownComments.includes(comment)) { if (knownComments.includes(comment)) {
text = text.replace(match, `${before}${dict[comment][lang]}${after}`); text = text.replace(match, `${before}${dict[comment][lang]}${after}`);
} else { } else if (comment.trim()) {
console.warn(`${comment} does not appear in the comment dictionary`); throw `${comment} does not appear in the comment dictionary`;
} }
} }

View File

@ -235,6 +235,14 @@ describe('translation parser', () => {
); );
}); });
it('ignores empty comments', () => {
expect.assertions(1);
const seed = '//';
expect(translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'js')).toBe(
seed
);
});
it('only replaces text inside comments, not between them', () => { it('only replaces text inside comments, not between them', () => {
const seed = `multiline comment /* Add your code below this line */ const seed = `multiline comment /* Add your code below this line */
/* Add your code above this line */ Add your code below this line /* */ `; /* Add your code above this line */ Add your code below this line /* */ `;
@ -363,33 +371,36 @@ describe('translation parser', () => {
).toBe(seed); ).toBe(seed);
}); });
it('only replaces exact matches (js)', () => { it('throws if there is not an exact match (js)', () => {
expect.assertions(2);
const seedMulti = `/* Add your code below this line const seedMulti = `/* Add your code below this line
Add your code above this line */ <span>change code below this line</span> `; Add your code above this line */ <span>change code below this line</span> `;
const seedInline = `// Add your code below this line, please`; const seedInline = `// Add your code below this line, please`;
expect( expect(() =>
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js') translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js')
).toBe(seedMulti); ).toThrow();
expect( expect(() =>
translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js') translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js')
).toBe(seedInline); ).toThrow();
}); });
it('only replaces exact matches (jsx)', () => { it('only replaces exact matches (jsx)', () => {
expect.assertions(1);
const seedMulti = `{ /* Add your code below this line const seedMulti = `{ /* Add your code below this line
Add your code above this line */ } <span>change code below this line</span> `; Add your code above this line */ } <span>change code below this line</span> `;
expect( expect(() =>
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'jsx') translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'jsx')
).toBe(seedMulti); ).toThrow();
}); });
it('only replaces exact matches (html)', () => { it('only replaces exact matches (html)', () => {
expect.assertions(1);
const seed = `<div> <!-- Add your code below this line const seed = `<div> <!-- Add your code below this line
Add your code above this line --> <span>change code below this line</span> `; Add your code above this line --> <span>change code below this line</span> `;
expect( expect(() =>
translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html') translateComments(seed, 'chinese', SIMPLE_TRANSLATION, 'html')
).toBe(seed); ).toThrow();
}); });
it('only translates jsx comments once', () => { it('only translates jsx comments once', () => {
@ -400,7 +411,8 @@ describe('translation parser', () => {
).toBe(transSeed); ).toBe(transSeed);
}); });
it('warns if the comment is not in the dictionary', () => { it('throws if the comment is not in the dictionary', () => {
expect.assertions(6);
const seedJSX = `{ /* this is not a comment */ }`; const seedJSX = `{ /* this is not a comment */ }`;
const seedInline = `// this is not a comment `; const seedInline = `// this is not a comment `;
const seedMulti = `/* this is not a comment */`; const seedMulti = `/* this is not a comment */`;
@ -410,18 +422,24 @@ describe('translation parser', () => {
const seedHTML = `<div> <!-- this is not a comment --> `; const seedHTML = `<div> <!-- this is not a comment --> `;
const seedScript = `<script> // this is not a comment </script>`; const seedScript = `<script> // this is not a comment </script>`;
translateComments(seedJSX, 'chinese', SIMPLE_TRANSLATION, 'jsx'); expect(() =>
expect(logSpy).toBeCalledTimes(1); translateComments(seedJSX, 'chinese', SIMPLE_TRANSLATION, 'jsx')
translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js'); ).toThrow();
expect(logSpy).toBeCalledTimes(2); expect(() =>
translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js'); translateComments(seedInline, 'chinese', SIMPLE_TRANSLATION, 'js')
expect(logSpy).toBeCalledTimes(3); ).toThrow();
translateComments(seedCSS, 'chinese', SIMPLE_TRANSLATION, 'html'); expect(() =>
expect(logSpy).toBeCalledTimes(4); translateComments(seedMulti, 'chinese', SIMPLE_TRANSLATION, 'js')
translateComments(seedHTML, 'chinese', SIMPLE_TRANSLATION, 'html'); ).toThrow();
expect(logSpy).toBeCalledTimes(5); expect(() =>
translateComments(seedScript, 'chinese', SIMPLE_TRANSLATION, 'html'); translateComments(seedCSS, 'chinese', SIMPLE_TRANSLATION, 'html')
expect(logSpy).toBeCalledTimes(6); ).toThrow();
expect(() =>
translateComments(seedHTML, 'chinese', SIMPLE_TRANSLATION, 'html')
).toThrow();
expect(() =>
translateComments(seedScript, 'chinese', SIMPLE_TRANSLATION, 'html')
).toThrow();
}); });
}); });
}); });