test: check replaced iff translatable comment

All translatable comments should be replaced, but nothing else.
This commit is contained in:
Oliver Eyton-Williams
2020-09-23 16:38:20 +02:00
committed by Mrugesh Mohapatra
parent d2ecd03013
commit 793fa8fb52
15 changed files with 1057 additions and 1304 deletions

View File

@ -0,0 +1,9 @@
function commentToData(file, comment) {
if (file.data[comment]) {
file.data[comment]++;
} else {
file.data[comment] = 1;
}
}
exports.commentToData = commentToData;

View File

@ -0,0 +1,12 @@
var rehype = require('rehype');
var vfile = require('vfile');
var getComments = require('./plugins/get-css-comments');
const processor = rehype().use(getComments);
function extractComments(html) {
return processor.processSync(vfile(html)).data;
}
module.exports = extractComments;

View File

@ -0,0 +1,32 @@
/* global expect */
const extractCSSComments = require('./extract-css-comments');
const someHTMLWithCSS = `<body>
Some text
<style>
.body {
color: red;
/* comment 1 */
}
.comment {
/* comment 1 */
background: green;
/* comment 2 */
}
</style>
</body>
`;
// NOTE: this is a bit finicky. If the css is, say, missing a semi-colon,
// nearby comments may be missed.
describe('extractCSSComments', () => {
it('should return an object with comment keys and count values', () => {
const commentCounts = {
'comment 1': 2,
'comment 2': 1
};
expect(extractCSSComments(someHTMLWithCSS)).toEqual(commentCounts);
});
});

View File

@ -0,0 +1,12 @@
var rehype = require('rehype');
var vfile = require('vfile');
var getComments = require('./plugins/get-html-comments');
const processor = rehype().use(getComments);
function extractComments(html) {
return processor.processSync(vfile(html)).data;
}
module.exports = extractComments;

View File

@ -0,0 +1,22 @@
/* global expect */
const extractHTMLComments = require('./extract-html-comments');
const someHTML = `<body>
Some text
<!-- a comment -->
<!-- a comment --><!-- another comment -->
</body>
`;
describe('extractHTMLComments', () => {
it('should return an object with comment keys and count values', () => {
const commentCounts = {
'a comment': 2,
'another comment': 1
};
expect(extractHTMLComments(someHTML)).toEqual(commentCounts);
});
});

View File

@ -0,0 +1,17 @@
const acorn = require('acorn');
const { commentToData } = require('./comment-to-data');
const parser = acorn.Parser;
function extractComments(js) {
let comments = [];
const file = { data: {} };
parser.parse(js, { onComment: comments, ecmaVersion: 2020 });
comments
.map(({ value }) => value.trim())
.forEach(comment => commentToData(file, comment));
return file.data;
}
module.exports = extractComments;

View File

@ -0,0 +1,25 @@
/* global expect */
const extractJSComments = require('./extract-js-comments');
const someJS = `
// single line comment
/*
a multiline comment
*/
var x = 'y'; // single line comment
var y = '// single line comment';
`;
describe('extractJSComments', () => {
it('should return an object with comment keys and count values', () => {
const commentCounts = {
'single line comment': 2,
'a multiline comment': 1
};
expect(extractJSComments(someJS)).toEqual(commentCounts);
});
});

View File

@ -0,0 +1,18 @@
const acorn = require('acorn');
const jsx = require('acorn-jsx');
const { commentToData } = require('./comment-to-data');
const parser = acorn.Parser.extend(jsx());
function extractComments(jsx) {
let comments = [];
const file = { data: {} };
parser.parse(jsx, { onComment: comments, ecmaVersion: 2020 });
comments
.map(({ value }) => value.trim())
.forEach(comment => commentToData(file, comment));
return file.data;
}
module.exports = extractComments;

View File

@ -0,0 +1,29 @@
/* global expect */
const extractJSXComments = require('./extract-jsx-comments');
const someJSX = `<Link
className='btn-invert'
to='/username'
>
Show me my public portfolio
{/* JSX comment */}
</Link>
// single line comment
{/* JSX comment */}
/*
a multiline comment
*/
`;
describe('extractJSXComments', () => {
it('should return an object with comment keys and count values', () => {
const commentCounts = {
'JSX comment': 2,
'single line comment': 1,
'a multiline comment': 1
};
expect(extractJSXComments(someJSX)).toEqual(commentCounts);
});
});

View File

@ -0,0 +1,29 @@
const { isEmpty } = require('lodash');
const visit = require('unist-util-visit');
var css = require('css');
var visitCss = require('rework-visit');
const { commentToData } = require('../comment-to-data');
function plugin() {
return transformer;
function transformer(tree, file) {
if (isEmpty(file.data)) file.data = {};
visit(tree, { type: 'element', tagName: 'style' }, styleVisitor);
function styleVisitor(node) {
visit(node, 'text', cssVisitor);
}
function cssVisitor(node) {
const ast = css.parse(node.value);
visitCss(ast.stylesheet, dec => {
let comments = dec
.filter(({ type }) => type === 'comment')
.map(({ comment }) => comment.trim());
comments.forEach(comment => commentToData(file, comment));
});
}
}
}
module.exports = plugin;

View File

@ -0,0 +1,20 @@
const { isEmpty } = require('lodash');
const visit = require('unist-util-visit');
const { commentToData } = require('../comment-to-data');
function plugin() {
return transformer;
function transformer(tree, file) {
if (isEmpty(file.data)) {
file.data = {};
}
visit(tree, 'comment', visitor);
function visitor(node) {
commentToData(file, node.value.trim());
}
}
}
module.exports = plugin;