From ff8eb6f32f299b130ffaabdf0d4dd3cb54471718 Mon Sep 17 00:00:00 2001 From: Alexandre Philibeaux Date: Fri, 7 Feb 2025 16:59:34 +0100 Subject: [PATCH] feat(valiate-icu): add option to ignore html tag --- .changeset/few-weeks-search.md | 5 ++++ .../src/__tests__/locales/en-1.js | 1 + .../src/__tests__/locales/en-ts.ts | 1 + packages/validate-icu-locales/src/index.ts | 26 ++++++++++++++----- 4 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 .changeset/few-weeks-search.md diff --git a/.changeset/few-weeks-search.md b/.changeset/few-weeks-search.md new file mode 100644 index 000000000..49bd4ecff --- /dev/null +++ b/.changeset/few-weeks-search.md @@ -0,0 +1,5 @@ +--- +"@scaleway/validate-icu-locales": patch +--- + +Add ignore tag option to validate icu diff --git a/packages/validate-icu-locales/src/__tests__/locales/en-1.js b/packages/validate-icu-locales/src/__tests__/locales/en-1.js index 8de515d93..f26cd1e1a 100644 --- a/packages/validate-icu-locales/src/__tests__/locales/en-1.js +++ b/packages/validate-icu-locales/src/__tests__/locales/en-1.js @@ -6,4 +6,5 @@ export const locales = { 'units.hours.label': '{count, plural, =0 {Hour} =1 {Hour} other {Hours}}', 'units.days.label': '{count, plural, =0 {Day} =1 {Day} other {Days}}', 'units.months.label': '{count, plural, =0 {Month} =1 {Month} other {Months}}', + 'units.chevron': '', } diff --git a/packages/validate-icu-locales/src/__tests__/locales/en-ts.ts b/packages/validate-icu-locales/src/__tests__/locales/en-ts.ts index 80f0cc89d..cd4ebffdf 100644 --- a/packages/validate-icu-locales/src/__tests__/locales/en-ts.ts +++ b/packages/validate-icu-locales/src/__tests__/locales/en-ts.ts @@ -6,4 +6,5 @@ export default { 'units.hours.label': '{count, plural, =0 {Hour} =1 {Hour} other {Hours}}', 'units.days.label': '{count, plural, =0 {Day} =1 {Day} other {Days}}', 'units.months.label': '{count, plural, =0 {Month} =1 {Month} other {Months}}', + 'units.chevron': '', } as const diff --git a/packages/validate-icu-locales/src/index.ts b/packages/validate-icu-locales/src/index.ts index b296326ad..4e0856820 100644 --- a/packages/validate-icu-locales/src/index.ts +++ b/packages/validate-icu-locales/src/index.ts @@ -1,13 +1,24 @@ #!/usr/bin/env node -import * as fs from 'fs/promises' +import { readFile } from 'node:fs/promises' +import { parseArgs } from 'node:util' +import type { ParseArgsConfig } from 'node:util' import { parse } from '@formatjs/icu-messageformat-parser' import type { ParserError } from '@formatjs/icu-messageformat-parser/error' import { globby } from 'globby' import { importFromString } from 'module-from-string' -const args = process.argv.slice(2) -const pattern = args[0] +const options: ParseArgsConfig['options'] = { + ignoreTag: { + type: 'boolean', + short: 'i', + default: false, + }, +} + +const { values, positionals } = parseArgs({ options, allowPositionals: true }) + +const pattern = positionals[0] type Locales = Record type ErrorICU = { @@ -29,7 +40,10 @@ const findICUErrors = ( const errors = Object.entries(locales) .map(([key, value]) => { try { - parse(value) + parse(value, { + // Need to cast as node doesn't allow generic to parseArgs + ignoreTag: values['ignoreTag'] as boolean, + }) return undefined } catch (err) { @@ -57,7 +71,7 @@ const readFiles = async (files: string[]): Promise => { if (extension === 'json') { try { - const data = await fs.readFile(file) + const data = await readFile(file) const jsonFile = data.toString() const locales = JSON.parse(jsonFile) as Locales @@ -71,7 +85,7 @@ const readFiles = async (files: string[]): Promise => { if (extension === 'ts' || extension === 'js') { try { - const data = await fs.readFile(file) + const data = await readFile(file) const javascriptFile = data.toString() const mod: unknown = await importFromString(javascriptFile, {