2023-01-05 22:43:33 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
const rule = require('./valid-i18n-keys');
|
|
|
|
const RuleTester = require('eslint').RuleTester;
|
|
|
|
|
|
|
|
const messagesCacheKey = rule.messagesCacheKey;
|
|
|
|
|
2023-03-30 00:03:25 +00:00
|
|
|
const __mockMessages__ = {
|
|
|
|
legacy_real_message: {
|
|
|
|
message: 'Legacy $message$',
|
|
|
|
},
|
|
|
|
'icu:real_message': {
|
|
|
|
messageformat: 'ICU {message}',
|
|
|
|
},
|
|
|
|
'icu:deleted_message': {
|
|
|
|
messageformat: 'shouldnt use me anymore',
|
|
|
|
description: '(deleted 01/01/1970)',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-01-05 22:43:33 +00:00
|
|
|
// Need to load so mocha doesn't complain about polluting the global namespace
|
|
|
|
require('@typescript-eslint/parser');
|
|
|
|
|
|
|
|
const ruleTester = new RuleTester({
|
|
|
|
parser: require.resolve('@typescript-eslint/parser'),
|
|
|
|
parserOptions: {
|
|
|
|
ecmaVersion: 2018,
|
|
|
|
sourceType: 'module',
|
|
|
|
ecmaFeatures: {
|
|
|
|
jsx: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ruleTester.run('valid-i18n-keys', rule, {
|
|
|
|
valid: [
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: `i18n("icu:real_message")`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: `window.i18n("icu:real_message")`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: `let jsx = <Intl id="icu:real_message"/>`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
},
|
2023-03-29 17:15:54 +00:00
|
|
|
],
|
|
|
|
invalid: [
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: `i18n("legacy_real_message")`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'i18n() key "legacy_real_message" is not an ICU message in _locales/en/messages.json',
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: `window.i18n("legacy_real_message")`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'i18n() key "legacy_real_message" is not an ICU message in _locales/en/messages.json',
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: `let jsx = <Intl id="legacy_real_message"/>`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'<Intl> id "legacy_real_message" is not an ICU message in _locales/en/messages.json',
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: 'i18n(`icu:real_${message}`)',
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-03-29 17:15:54 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: "i18n()'s first argument should always be a literal string",
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: 'window.i18n(`icu:real_${message}`)',
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-03-29 17:15:54 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: "i18n()'s first argument should always be a literal string",
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-01-05 22:43:33 +00:00
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: `let jsx = <Intl id={"icu:real_message"}/>`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-03-29 17:15:54 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
"<Intl> must always be provided an 'id' attribute with a literal string",
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
2023-01-05 22:43:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: 'let jsx = <Intl id={`icu:real_message`}/>',
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-03-29 17:15:54 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
"<Intl> must always be provided an 'id' attribute with a literal string",
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
2023-01-05 22:43:33 +00:00
|
|
|
},
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: 'let jsx = <Intl id={`icu:real_${message}`}/>',
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-03-29 17:15:54 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
"<Intl> must always be provided an 'id' attribute with a literal string",
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
2023-01-05 22:43:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
code: `i18n("THIS_KEY_SHOULD_NEVER_EXIST")`,
|
2023-03-30 00:03:25 +00:00
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'i18n() key "THIS_KEY_SHOULD_NEVER_EXIST" not found in _locales/en/messages.json',
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: `i18n(cond ? "icu:real_message" : "icu:real_message")`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: "i18n()'s first argument should always be a literal string",
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: `i18n(42)`,
|
2023-03-30 00:03:25 +00:00
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message: "i18n()'s first argument should always be a literal string",
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: `let jsx = <Intl id="THIS_KEY_SHOULD_NEVER_EXIST"/>`,
|
2023-03-30 00:03:25 +00:00
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'<Intl> id "THIS_KEY_SHOULD_NEVER_EXIST" not found in _locales/en/messages.json',
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
2023-03-30 00:03:25 +00:00
|
|
|
code: `let jsx = <Intl id={cond ? "icu:real_message" : "icu:real_message"}/>`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
"<Intl> must always be provided an 'id' attribute with a literal string",
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: `let jsx = <Intl id={42}/>`,
|
2023-03-30 00:03:25 +00:00
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
2023-01-05 22:43:33 +00:00
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
"<Intl> must always be provided an 'id' attribute with a literal string",
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-03-30 00:03:25 +00:00
|
|
|
{
|
|
|
|
code: `i18n("icu:deleted_message")`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'i18n() key "icu:deleted_message" is marked as deleted in _locales/en/messages.json',
|
|
|
|
type: 'CallExpression',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: `let jsx = <Intl id="icu:deleted_message"/>`,
|
|
|
|
options: [{ messagesCacheKey, __mockMessages__ }],
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
message:
|
|
|
|
'<Intl> id "icu:deleted_message" is marked as deleted in _locales/en/messages.json',
|
|
|
|
type: 'JSXOpeningElement',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-01-05 22:43:33 +00:00
|
|
|
],
|
|
|
|
});
|