Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCI] Fix type errors in i18n #3629

Merged
merged 7 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [MD] Refactor data source error handling ([#2661](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2661))
- Refactor and improve Discover field summaries ([#2391](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2391))
- [Vis Builder] Removed Hard Coded Strings and Used i18n to transalte([#2867](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2867))
- [Console] Replace jQuery.ajax with core.http when calling OSD APIs in console ([#3080]https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3080))
- [Console] Replace jQuery.ajax with core.http when calling OSD APIs in console ([#3080](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3080))
- [I18n] Fix Listr type errors and error handlers ([#3629](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3629))

### 🔩 Tests

Expand Down
2 changes: 1 addition & 1 deletion src/dev/i18n/integrate_locale_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function verifyMessages(
typeof message === 'string' ? message : message.text,
messageId
);
} catch (err) {
} catch (err: any) {
if (options.ignoreIncompatible) {
localizedMessagesMap.delete(messageId);
options.log.warning(`Incompatible translation ignored: ${err.message}`);
Expand Down
9 changes: 8 additions & 1 deletion src/dev/i18n/tasks/check_compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ export interface I18nFlags {
ignoreMissing: boolean;
}

export function checkCompatibility(config: I18nConfig, flags: I18nFlags, log: ToolingLog) {
export function checkCompatibility(
config: I18nConfig | undefined,
flags: I18nFlags,
log: ToolingLog
) {
if (!config) {
throw new Error('Config is missing');
}
const { fix, ignoreIncompatible, ignoreUnused, ignoreMalformed, ignoreMissing } = flags;
return config.translations.map((translationsPath) => ({
task: async ({ messages }: { messages: Map<string, { message: string }> }) => {
Expand Down
6 changes: 3 additions & 3 deletions src/dev/i18n/tasks/check_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
* specific language governing permissions and limitations
* under the License.
*/

import { resolve, join } from 'path';
import { ListrContext } from '.';
import { I18N_RC } from '../constants';
import { ErrorReporter, checkConfigNamespacePrefix, arrayify } from '..';
import { checkConfigNamespacePrefix, arrayify } from '..';

export function checkConfigs(additionalConfigPaths: string | string[] = []) {
const root = join(__dirname, '../../../../');
Expand All @@ -39,7 +39,7 @@ export function checkConfigs(additionalConfigPaths: string | string[] = []) {
const configPaths = [opensearchDashboardsRC, ...arrayify(additionalConfigPaths)];

return configPaths.map((configPath) => ({
task: async (context: { reporter: ErrorReporter }) => {
task: async (context: ListrContext) => {
try {
await checkConfigNamespacePrefix(configPath);
} catch (err) {
Expand Down
5 changes: 4 additions & 1 deletion src/dev/i18n/tasks/extract_default_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ import chalk from 'chalk';
import { createFailError } from '@osd/dev-utils';
import { ErrorReporter, extractMessagesFromPathToMap, filterConfigPaths, I18nConfig } from '..';

export function extractDefaultMessages(config: I18nConfig, inputPaths: string[]) {
export function extractDefaultMessages(config: I18nConfig | undefined, inputPaths: string[]) {
if (!config) {
throw new Error('Config is missing');
}
const filteredPaths = filterConfigPaths(inputPaths, config) as string[];
if (filteredPaths.length === 0) {
throw createFailError(
Expand Down
14 changes: 6 additions & 8 deletions src/dev/i18n/tasks/extract_untracked_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@
*/

import { createFailError } from '@osd/dev-utils';
import {
I18nConfig,
matchEntriesWithExctractors,
normalizePath,
readFileAsync,
ErrorReporter,
} from '..';
import { ListrContext } from '.';
import { I18nConfig, matchEntriesWithExctractors, normalizePath, readFileAsync } from '..';

function filterEntries(entries: string[], exclude: string[]) {
return entries.filter((entry: string) =>
Expand Down Expand Up @@ -104,8 +99,11 @@ export async function extractUntrackedMessagesTask({
export function extractUntrackedMessages(inputPaths: string[]) {
return inputPaths.map((inputPath) => ({
title: `Checking untracked messages in ${inputPath}`,
task: async (context: { reporter: ErrorReporter; config: I18nConfig }) => {
task: async (context: ListrContext) => {
const { reporter, config } = context;
if (!config) {
throw new Error('Config is not defined');
}
const initialErrorsNumber = reporter.errors.length;
const result = await extractUntrackedMessagesTask({ path: inputPath, config, reporter });
if (reporter.errors.length === initialErrorsNumber) {
Expand Down
8 changes: 8 additions & 0 deletions src/dev/i18n/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@
* under the License.
*/

import { ErrorReporter, I18nConfig } from '..';

export { extractDefaultMessages } from './extract_default_translations';
export { extractUntrackedMessages } from './extract_untracked_translations';
export { checkCompatibility } from './check_compatibility';
export { mergeConfigs } from './merge_configs';
export { checkConfigs } from './check_configs';

export interface ListrContext {
config?: I18nConfig;
reporter: ErrorReporter;
messages: Map<string, { message: string }>;
}
6 changes: 3 additions & 3 deletions src/dev/i18n/tasks/merge_configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* specific language governing permissions and limitations
* under the License.
*/

import { resolve, join } from 'path';
import { ErrorReporter, I18nConfig, assignConfigFromPath, arrayify } from '..';
import { ListrContext } from '.';
import { assignConfigFromPath, arrayify } from '..';

export function mergeConfigs(additionalConfigPaths: string | string[] = []) {
const root = join(__dirname, '../../../../');
Expand All @@ -38,7 +38,7 @@ export function mergeConfigs(additionalConfigPaths: string | string[] = []) {
const configPaths = [opensearchDashboardsRC, ...arrayify(additionalConfigPaths)];

return configPaths.map((configPath) => ({
task: async (context: { reporter: ErrorReporter; config?: I18nConfig }) => {
task: async (context: ListrContext) => {
try {
context.config = await assignConfigFromPath(context.config, configPath);
} catch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/i18n/utils/verify_icu_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export function verifyICUMessage(message: string) {
verifySelectFormatNode(node.format);
}
}
} catch (error) {
if (error.name === 'SyntaxError') {
} catch (error: unknown) {
if (error instanceof parser.SyntaxError && error.name === 'SyntaxError') {
const errorWithContext = createParserErrorMessage(message, {
loc: {
line: error.location.start.line,
Expand Down
24 changes: 13 additions & 11 deletions src/dev/run_i18n_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ import chalk from 'chalk';
import Listr from 'listr';

import { createFailError, run } from '@osd/dev-utils';
import { ErrorReporter, I18nConfig } from './i18n';
import { ErrorReporter } from './i18n';
import {
extractDefaultMessages,
extractUntrackedMessages,
checkCompatibility,
checkConfigs,
mergeConfigs,
ListrContext,
} from './i18n/tasks';

const skipOnNoTranslations = ({ config }: { config: I18nConfig }) =>
!config.translations.length && 'No translations found.';

const skipOnNoTranslations = (context: ListrContext) =>
!context.config?.translations?.length && 'No translations found.';
run(
async ({
flags: {
Expand Down Expand Up @@ -85,7 +85,7 @@ run(

const srcPaths = Array().concat(path || ['./src', './packages']);

const list = new Listr(
const list = new Listr<ListrContext>(
[
{
title: 'Checking .i18nrc.json files',
Expand All @@ -105,14 +105,15 @@ run(
{
title: 'Validating Default Messages',
skip: skipOnNoTranslations,
task: ({ config }) =>
new Listr(extractDefaultMessages(config, srcPaths), { exitOnError: true }),
task: ({ config }) => {
return new Listr(extractDefaultMessages(config, srcPaths), { exitOnError: true });
},
},
{
title: 'Compatibility Checks',
skip: skipOnNoTranslations,
task: ({ config }) =>
new Listr(
task: ({ config }) => {
Nicksqain marked this conversation as resolved.
Show resolved Hide resolved
return new Listr<ListrContext>(
checkCompatibility(
config,
{
Expand All @@ -125,7 +126,8 @@ run(
log
),
{ exitOnError: true }
),
);
},
},
],
{
Expand All @@ -138,7 +140,7 @@ run(
const reporter = new ErrorReporter();
const messages: Map<string, { message: string }> = new Map();
await list.run({ messages, reporter });
} catch (error) {
} catch (error: ErrorReporter | Error) {
process.exitCode = 1;
if (error instanceof ErrorReporter) {
error.errors.forEach((e: string | Error) => log.error(e));
Expand Down
8 changes: 4 additions & 4 deletions src/dev/run_i18n_extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { resolve } from 'path';

import { createFailError, run } from '@osd/dev-utils';
import { ErrorReporter, serializeToJson, serializeToJson5, writeFileAsync } from './i18n';
import { extractDefaultMessages, mergeConfigs } from './i18n/tasks';
import { extractDefaultMessages, mergeConfigs, ListrContext } from './i18n/tasks';

run(
async ({
Expand All @@ -59,7 +59,7 @@ run(
}
const srcPaths = Array().concat(path || ['./src', './packages']);

const list = new Listr([
const list = new Listr<ListrContext>([
{
title: 'Merging .i18nrc.json files',
task: () => new Listr(mergeConfigs(includeConfig), { exitOnError: true }),
Expand All @@ -71,7 +71,7 @@ run(
},
{
title: 'Writing to file',
enabled: (ctx) => outputDir && ctx.messages.size,
enabled: (ctx) => Boolean(outputDir && ctx.messages.size),
task: async (ctx) => {
const sortedMessages = [...ctx.messages].sort(([key1], [key2]) =>
key1.localeCompare(key2)
Expand All @@ -90,7 +90,7 @@ run(
const reporter = new ErrorReporter();
const messages: Map<string, { message: string }> = new Map();
await list.run({ messages, reporter });
} catch (error) {
} catch (error: ErrorReporter | Error) {
process.exitCode = 1;
if (error instanceof ErrorReporter) {
error.errors.forEach((e: string | Error) => log.error(e));
Expand Down
32 changes: 18 additions & 14 deletions src/dev/run_i18n_integrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Listr from 'listr';

import { createFailError, run } from '@osd/dev-utils';
import { ErrorReporter, integrateLocaleFiles } from './i18n';
import { extractDefaultMessages, mergeConfigs } from './i18n/tasks';
import { extractDefaultMessages, mergeConfigs, ListrContext } from './i18n/tasks';

run(
async ({
Expand Down Expand Up @@ -90,7 +90,7 @@ run(

const srcPaths = Array().concat(path || ['./src', './packages']);

const list = new Listr([
const list = new Listr<ListrContext>([
{
title: 'Merging .i18nrc.json files',
task: () => new Listr(mergeConfigs(includeConfig), { exitOnError: true }),
Expand All @@ -103,17 +103,21 @@ run(
{
title: 'Integrating Locale File',
task: async ({ messages, config }) => {
await integrateLocaleFiles(messages, {
sourceFileName: source,
targetFileName: target,
dryRun,
ignoreIncompatible,
ignoreUnused,
ignoreMissing,
ignoreMalformed,
config,
log,
});
if (!config) {
throw new Error('Config is missing');
} else {
await integrateLocaleFiles(messages, {
sourceFileName: source,
targetFileName: target,
dryRun,
ignoreIncompatible,
ignoreUnused,
ignoreMissing,
ignoreMalformed,
config,
log,
});
}
},
},
]);
Expand All @@ -123,7 +127,7 @@ run(
const messages: Map<string, { message: string }> = new Map();
await list.run({ messages, reporter });
process.exitCode = 0;
} catch (error) {
} catch (error: ErrorReporter | Error) {
process.exitCode = 1;
if (error instanceof ErrorReporter) {
error.errors.forEach((e: string | Error) => log.error(e));
Expand Down