2022-10-03 21:19:54 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { parse as parseIcuMessage } from '@formatjs/icu-messageformat-parser';
|
|
|
|
import type {
|
|
|
|
MessageFormatElement,
|
|
|
|
Location,
|
|
|
|
} from '@formatjs/icu-messageformat-parser';
|
|
|
|
import parseJsonToAst from 'json-to-ast';
|
|
|
|
import { readFile } from 'fs/promises';
|
|
|
|
import { join as pathJoin, relative as pathRelative } from 'path';
|
|
|
|
import chalk from 'chalk';
|
|
|
|
import { deepEqual } from 'assert';
|
|
|
|
import type { Rule } from './utils/rule';
|
|
|
|
|
|
|
|
import icuPrefix from './rules/icuPrefix';
|
2023-06-14 23:26:05 +00:00
|
|
|
import wrapEmoji from './rules/wrapEmoji';
|
2022-10-03 21:19:54 +00:00
|
|
|
import onePlural from './rules/onePlural';
|
|
|
|
import noLegacyVariables from './rules/noLegacyVariables';
|
|
|
|
import noNestedChoice from './rules/noNestedChoice';
|
|
|
|
import noOffset from './rules/noOffset';
|
2023-06-13 19:08:18 +00:00
|
|
|
import noOneChoice from './rules/noOneChoice';
|
2022-10-03 21:19:54 +00:00
|
|
|
import noOrdinal from './rules/noOrdinal';
|
2023-04-04 19:05:50 +00:00
|
|
|
import pluralPound from './rules/pluralPound';
|
2022-10-03 21:19:54 +00:00
|
|
|
|
|
|
|
const RULES = [
|
|
|
|
icuPrefix,
|
2023-06-14 23:26:05 +00:00
|
|
|
wrapEmoji,
|
2022-10-03 21:19:54 +00:00
|
|
|
noLegacyVariables,
|
|
|
|
noNestedChoice,
|
|
|
|
noOffset,
|
2023-06-13 19:08:18 +00:00
|
|
|
noOneChoice,
|
2022-10-03 21:19:54 +00:00
|
|
|
noOrdinal,
|
|
|
|
onePlural,
|
2023-04-04 19:05:50 +00:00
|
|
|
pluralPound,
|
2022-10-03 21:19:54 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
type Test = {
|
|
|
|
messageformat: string;
|
|
|
|
expectErrors: Array<string>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const tests: Record<string, Test> = {
|
2022-10-10 19:12:57 +00:00
|
|
|
'icu:err1': {
|
2023-06-13 19:08:18 +00:00
|
|
|
messageformat:
|
|
|
|
'{a, plural, one {a} other {as}} {b, plural, one {b} other {bs}}',
|
2022-10-03 21:19:54 +00:00
|
|
|
expectErrors: ['onePlural'],
|
|
|
|
},
|
2022-10-10 19:12:57 +00:00
|
|
|
'icu:err2': {
|
2023-06-13 19:08:18 +00:00
|
|
|
messageformat:
|
|
|
|
'{a, plural, one {a} other {{b, plural, one {b} other {bs}}}}',
|
2022-10-03 21:19:54 +00:00
|
|
|
expectErrors: ['noNestedChoice', 'onePlural'],
|
|
|
|
},
|
2022-10-10 19:12:57 +00:00
|
|
|
'icu:err3': {
|
2023-06-13 19:08:18 +00:00
|
|
|
messageformat:
|
|
|
|
'{a, select, one {a} other {{b, select, one {b} other {bs}}}}',
|
2022-10-03 21:19:54 +00:00
|
|
|
expectErrors: ['noNestedChoice'],
|
|
|
|
},
|
2022-10-10 19:12:57 +00:00
|
|
|
'icu:err4': {
|
2023-06-13 19:08:18 +00:00
|
|
|
messageformat: '{a, plural, offset:1 one {a} other {as}}',
|
2022-10-03 21:19:54 +00:00
|
|
|
expectErrors: ['noOffset'],
|
|
|
|
},
|
2023-06-13 19:08:18 +00:00
|
|
|
'icu:noOneChoice:1': {
|
|
|
|
messageformat: '{a, plural, other {a}}',
|
|
|
|
expectErrors: ['noOneChoice'],
|
|
|
|
},
|
2022-10-10 19:12:57 +00:00
|
|
|
'icu:err5': {
|
2023-06-13 19:08:18 +00:00
|
|
|
messageformat: '{a, selectordinal, one {a} other {as}}',
|
2022-10-03 21:19:54 +00:00
|
|
|
expectErrors: ['noOrdinal'],
|
|
|
|
},
|
2022-10-10 19:12:57 +00:00
|
|
|
'icu:err6': {
|
2022-10-03 21:19:54 +00:00
|
|
|
messageformat: '$a$',
|
|
|
|
expectErrors: ['noLegacyVariables'],
|
|
|
|
},
|
2023-06-14 23:26:05 +00:00
|
|
|
'icu:wrapEmoji:1': {
|
|
|
|
messageformat: '👩',
|
|
|
|
expectErrors: ['wrapEmoji'],
|
|
|
|
},
|
|
|
|
'icu:wrapEmoji:2': {
|
2023-06-15 00:57:04 +00:00
|
|
|
messageformat: '<emojify>👩 extra</emojify>',
|
2023-06-14 23:26:05 +00:00
|
|
|
expectErrors: ['wrapEmoji'],
|
|
|
|
},
|
|
|
|
'icu:wrapEmoji:3': {
|
2023-06-15 00:57:04 +00:00
|
|
|
messageformat: '<emojify>👩👩</emojify>',
|
2023-06-14 23:26:05 +00:00
|
|
|
expectErrors: ['wrapEmoji'],
|
|
|
|
},
|
|
|
|
'icu:wrapEmoji:4': {
|
2023-06-15 00:57:04 +00:00
|
|
|
messageformat: '<emojify>{emoji}</emojify>',
|
2023-06-14 23:26:05 +00:00
|
|
|
expectErrors: ['wrapEmoji'],
|
|
|
|
},
|
|
|
|
'icu:wrapEmoji:5': {
|
2023-06-15 00:57:04 +00:00
|
|
|
messageformat: '<emojify>👩</emojify>',
|
2023-06-14 23:26:05 +00:00
|
|
|
expectErrors: [],
|
|
|
|
},
|
2022-10-03 21:19:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type Report = {
|
|
|
|
id: string;
|
|
|
|
message: string;
|
|
|
|
location: Location | void;
|
2023-04-04 19:05:50 +00:00
|
|
|
locationOffset: number;
|
2022-10-03 21:19:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function lintMessage(
|
|
|
|
messageId: string,
|
|
|
|
elements: Array<MessageFormatElement>,
|
|
|
|
rules: Array<Rule>
|
|
|
|
) {
|
|
|
|
const reports: Array<Report> = [];
|
|
|
|
for (const rule of rules) {
|
|
|
|
rule.run(elements, {
|
|
|
|
messageId,
|
2023-04-04 19:05:50 +00:00
|
|
|
report(message, location, locationOffset = 0) {
|
|
|
|
reports.push({ id: rule.id, message, location, locationOffset });
|
2022-10-03 21:19:54 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return reports;
|
|
|
|
}
|
|
|
|
|
|
|
|
function assert(condition: unknown, message: string): asserts condition {
|
|
|
|
if (!condition) {
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function lintMessages() {
|
|
|
|
const repoRoot = pathJoin(__dirname, '..', '..');
|
|
|
|
const filePath = pathJoin(repoRoot, '_locales/en/messages.json');
|
|
|
|
const relativePath = pathRelative(repoRoot, filePath);
|
|
|
|
let file = await readFile(filePath, 'utf-8');
|
|
|
|
|
|
|
|
if (process.argv.includes('--test')) {
|
|
|
|
file = JSON.stringify(tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsonAst = parseJsonToAst(file);
|
|
|
|
|
2024-05-28 01:30:39 +00:00
|
|
|
let failed = false;
|
|
|
|
|
2022-10-03 21:19:54 +00:00
|
|
|
assert(jsonAst.type === 'Object', 'Expected an object');
|
|
|
|
for (const topProp of jsonAst.children) {
|
|
|
|
if (topProp.key.value === 'smartling') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const messageId = topProp.key.value;
|
|
|
|
|
|
|
|
assert(topProp.value.type === 'Object', 'Expected an object');
|
|
|
|
|
|
|
|
const icuMessageProp = topProp.value.children.find(messageProp => {
|
|
|
|
return messageProp.key.value === 'messageformat';
|
|
|
|
});
|
|
|
|
if (icuMessageProp == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const icuMesssageLiteral = icuMessageProp.value;
|
|
|
|
assert(
|
|
|
|
icuMesssageLiteral.type === 'Literal' &&
|
|
|
|
typeof icuMesssageLiteral.value === 'string',
|
|
|
|
'Expected a string'
|
|
|
|
);
|
|
|
|
|
|
|
|
const icuMessage: string = icuMesssageLiteral.value;
|
|
|
|
|
|
|
|
const ast = parseIcuMessage(icuMessage, {
|
|
|
|
captureLocation: true,
|
|
|
|
shouldParseSkeletons: true,
|
|
|
|
requiresOtherClause: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const reports = lintMessage(messageId, ast, RULES);
|
|
|
|
const key = topProp.key.value;
|
|
|
|
|
|
|
|
if (process.argv.includes('--test')) {
|
|
|
|
const test = tests[key];
|
|
|
|
const actualErrors = reports.map(report => report.id);
|
|
|
|
deepEqual(actualErrors, test.expectErrors);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const report of reports) {
|
|
|
|
let loc = '';
|
|
|
|
|
|
|
|
if (report.location != null && icuMesssageLiteral.loc != null) {
|
|
|
|
const line =
|
|
|
|
icuMesssageLiteral.loc.start.line + (report.location.start.line - 1);
|
|
|
|
const column =
|
2023-04-04 19:05:50 +00:00
|
|
|
icuMesssageLiteral.loc.start.column +
|
|
|
|
report.location.start.column +
|
|
|
|
report.locationOffset;
|
2022-10-03 21:19:54 +00:00
|
|
|
loc = `:${line}:${column}`;
|
|
|
|
} else if (icuMesssageLiteral.loc != null) {
|
|
|
|
const { line, column } = icuMesssageLiteral.loc.start;
|
2023-04-04 19:05:50 +00:00
|
|
|
loc = `:${line}:${column + report.locationOffset}`;
|
2022-10-03 21:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(
|
|
|
|
chalk`{bold.cyan ${relativePath}${loc}} ${report.message} {magenta ({underline ${report.id}})}`
|
|
|
|
);
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(chalk` {dim in ${key} is "}{red ${icuMessage}}{dim "}`);
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error();
|
2024-05-28 01:30:39 +00:00
|
|
|
|
|
|
|
failed = true;
|
2022-10-03 21:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-28 01:30:39 +00:00
|
|
|
|
|
|
|
if (failed) {
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-10-03 21:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lintMessages().catch(error => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|