fix(tests): add validation of comment translations (#41537)

This commit is contained in:
Oliver Eyton-Williams
2021-03-20 07:29:13 +01:00
committed by GitHub
parent 654d66186e
commit 0d3158d4f4
12 changed files with 345 additions and 527 deletions

View File

@ -0,0 +1,29 @@
const { isEmpty } = require('lodash');
const visit = require('unist-util-visit');
const acorn = require('acorn');
const { commentToData } = require('../comment-to-data');
const parser = acorn.Parser;
function plugin() {
return transformer;
function transformer(tree, file) {
if (isEmpty(file.data)) file.data = {};
visit(tree, { type: 'element', tagName: 'script' }, scriptVisitor);
function scriptVisitor(node) {
visit(node, 'text', jsVisitor);
}
function jsVisitor(node) {
let comments = [];
parser.parse(node.value, { onComment: comments, ecmaVersion: 2020 });
comments
.map(({ value }) => value.trim())
.forEach(comment => commentToData(file, comment));
}
}
}
module.exports = plugin;