mirror of
https://github.com/Stremio/stremio-web.git
synced 2026-03-11 21:27:05 +00:00
Feat: refactor the translations scanner into a test
This commit is contained in:
parent
18b70402a4
commit
bafa6a7ad2
2 changed files with 23 additions and 21 deletions
|
|
@ -4,7 +4,6 @@ const recast = require('recast');
|
||||||
const babelParser = require('@babel/parser');
|
const babelParser = require('@babel/parser');
|
||||||
|
|
||||||
const directoryToScan = './src';
|
const directoryToScan = './src';
|
||||||
const report = [];
|
|
||||||
|
|
||||||
function toKey(str) {
|
function toKey(str) {
|
||||||
return str
|
return str
|
||||||
|
|
@ -14,7 +13,7 @@ function toKey(str) {
|
||||||
.slice(0, 40);
|
.slice(0, 40);
|
||||||
}
|
}
|
||||||
|
|
||||||
function scanFile(filePath) {
|
function scanFile(filePath, report) {
|
||||||
try {
|
try {
|
||||||
const code = fs.readFileSync(filePath, 'utf8');
|
const code = fs.readFileSync(filePath, 'utf8');
|
||||||
const ast = babelParser.parse(code, {
|
const ast = babelParser.parse(code, {
|
||||||
|
|
@ -31,7 +30,6 @@ function scanFile(filePath) {
|
||||||
});
|
});
|
||||||
|
|
||||||
recast.types.visit(ast, {
|
recast.types.visit(ast, {
|
||||||
// Text directly inside JSX
|
|
||||||
visitJSXText(path) {
|
visitJSXText(path) {
|
||||||
const text = path.node.value.trim();
|
const text = path.node.value.trim();
|
||||||
if (text.length > 1 && /\w/.test(text)) {
|
if (text.length > 1 && /\w/.test(text)) {
|
||||||
|
|
@ -46,11 +44,9 @@ function scanFile(filePath) {
|
||||||
this.traverse(path);
|
this.traverse(path);
|
||||||
},
|
},
|
||||||
|
|
||||||
// { "hello" } style
|
|
||||||
visitJSXExpressionContainer(path) {
|
visitJSXExpressionContainer(path) {
|
||||||
const expr = path.node.expression;
|
const expr = path.node.expression;
|
||||||
|
|
||||||
// Skip expressions that call t()
|
|
||||||
if (
|
if (
|
||||||
expr.type === 'CallExpression' &&
|
expr.type === 'CallExpression' &&
|
||||||
expr.callee.type === 'Identifier' &&
|
expr.callee.type === 'Identifier' &&
|
||||||
|
|
@ -59,7 +55,6 @@ function scanFile(filePath) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find only { "text" } expressions
|
|
||||||
if (expr.type === 'StringLiteral') {
|
if (expr.type === 'StringLiteral') {
|
||||||
const parent = path.parentPath.node;
|
const parent = path.parentPath.node;
|
||||||
if (parent.type === 'JSXElement') {
|
if (parent.type === 'JSXElement') {
|
||||||
|
|
@ -82,26 +77,33 @@ function scanFile(filePath) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function walk(dir) {
|
function walk(dir, report) {
|
||||||
fs.readdirSync(dir).forEach((file) => {
|
fs.readdirSync(dir).forEach((file) => {
|
||||||
const fullPath = path.join(dir, file);
|
const fullPath = path.join(dir, file);
|
||||||
if (fs.statSync(fullPath).isDirectory()) {
|
if (fs.statSync(fullPath).isDirectory()) {
|
||||||
walk(fullPath);
|
walk(fullPath, report);
|
||||||
} else if (/\.(js|jsx|ts|tsx)$/.test(file)) {
|
} else if (/\.(js|jsx|ts|tsx)$/.test(file)) {
|
||||||
console.log('Scanning file:', fullPath);
|
// console.log('📄 Scanning file:', fullPath);
|
||||||
scanFile(fullPath);
|
scanFile(fullPath, report);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function writeReport() {
|
describe('i18n hardcoded string scan', () => {
|
||||||
const header = `File,Line,Hardcoded String,Suggested Key\n`;
|
it('should print hardcoded strings with suggested keys', () => {
|
||||||
const rows = report.map((row) =>
|
const report = [];
|
||||||
`"${row.file}",${row.line},"${row.string.replace(/"/g, '""')}","${row.key}"`
|
walk(directoryToScan, report);
|
||||||
);
|
|
||||||
fs.writeFileSync('i18n-report.csv', header + rows.join('\n'));
|
|
||||||
console.log('✅ Report written to i18n-report.csv');
|
|
||||||
}
|
|
||||||
|
|
||||||
walk(directoryToScan);
|
if (report.length === 0) {
|
||||||
writeReport();
|
console.log('✅ No hardcoded strings found.');
|
||||||
|
} else {
|
||||||
|
console.log('🚨 Hardcoded strings found:');
|
||||||
|
report.forEach(row => {
|
||||||
|
console.log(`File: ${row.file}, Line: ${row.line}, String: "${row.string}", Suggested Key: ${row.key}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional: expect no hardcoded strings if you want the test to fail in that case
|
||||||
|
// expect(report.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
@ -11,7 +11,7 @@
|
||||||
"build": "webpack --mode production",
|
"build": "webpack --mode production",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"lint": "eslint src",
|
"lint": "eslint src",
|
||||||
"scan-translations": "node scan-i18n-report.js"
|
"scan-translations": "npx jest i18nScan.test.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/runtime": "7.26.0",
|
"@babel/runtime": "7.26.0",
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue