test: check replaced iff translatable comment
All translatable comments should be replaced, but nothing else.
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
d2ecd03013
commit
793fa8fb52
9
curriculum/test/utils/comment-to-data.js
Normal file
9
curriculum/test/utils/comment-to-data.js
Normal file
@ -0,0 +1,9 @@
|
||||
function commentToData(file, comment) {
|
||||
if (file.data[comment]) {
|
||||
file.data[comment]++;
|
||||
} else {
|
||||
file.data[comment] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
exports.commentToData = commentToData;
|
12
curriculum/test/utils/extract-css-comments.js
Normal file
12
curriculum/test/utils/extract-css-comments.js
Normal 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;
|
32
curriculum/test/utils/extract-css-comments.test.js
Normal file
32
curriculum/test/utils/extract-css-comments.test.js
Normal 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);
|
||||
});
|
||||
});
|
12
curriculum/test/utils/extract-html-comments.js
Normal file
12
curriculum/test/utils/extract-html-comments.js
Normal 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;
|
22
curriculum/test/utils/extract-html-comments.test.js
Normal file
22
curriculum/test/utils/extract-html-comments.test.js
Normal 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);
|
||||
});
|
||||
});
|
17
curriculum/test/utils/extract-js-comments.js
Normal file
17
curriculum/test/utils/extract-js-comments.js
Normal 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;
|
25
curriculum/test/utils/extract-js-comments.test.js
Normal file
25
curriculum/test/utils/extract-js-comments.test.js
Normal 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);
|
||||
});
|
||||
});
|
18
curriculum/test/utils/extract-jsx-comments.js
Normal file
18
curriculum/test/utils/extract-jsx-comments.js
Normal 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;
|
29
curriculum/test/utils/extract-jsx-comments.test.js
Normal file
29
curriculum/test/utils/extract-jsx-comments.test.js
Normal 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);
|
||||
});
|
||||
});
|
29
curriculum/test/utils/plugins/get-css-comments.js
Normal file
29
curriculum/test/utils/plugins/get-css-comments.js
Normal 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;
|
20
curriculum/test/utils/plugins/get-html-comments.js
Normal file
20
curriculum/test/utils/plugins/get-html-comments.js
Normal 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;
|
Reference in New Issue
Block a user