diff --git a/packages/kbn-eslint-plugin-i18n/helpers/get_intent_from_node.ts b/packages/kbn-eslint-plugin-i18n/helpers/get_intent_from_node.ts index d27f2407ce64c..9db084da2fb7b 100644 --- a/packages/kbn-eslint-plugin-i18n/helpers/get_intent_from_node.ts +++ b/packages/kbn-eslint-plugin-i18n/helpers/get_intent_from_node.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree'; -import { cleanString, lowerCaseFirstLetter, upperCaseFirstLetter } from './utils'; +import { cleanIdentifier, lowerCaseFirstLetter, upperCaseFirstLetter } from './utils'; const EXEMPTED_TAG_NAMES = ['EuiCode', 'EuiBetaBadge', 'FormattedMessage']; @@ -15,7 +15,7 @@ export function getIntentFromNode( parent: TSESTree.Node | undefined ): string | false { const processedValue = lowerCaseFirstLetter( - cleanString(value) + cleanIdentifier(value) .split(' ') .filter((_, i) => i < 4) .map(upperCaseFirstLetter) diff --git a/packages/kbn-eslint-plugin-i18n/helpers/utils.test.ts b/packages/kbn-eslint-plugin-i18n/helpers/utils.test.ts index 9d36c7a7e1e9c..0f9353932cd73 100644 --- a/packages/kbn-eslint-plugin-i18n/helpers/utils.test.ts +++ b/packages/kbn-eslint-plugin-i18n/helpers/utils.test.ts @@ -42,10 +42,26 @@ describe('Utils', () => { ).toBe('hello great'); }); - it('should remove all non alphabet characters', () => { - expect(cleanString('abc123!@#')).toBe('abc'); + it('should remove all non alphabet characters unless they are incorporated in a sentence', () => { + expect(cleanString('1')).toBe(''); + expect(cleanString('12')).toBe(''); + expect(cleanString('123')).toBe(''); + expect(cleanString('?')).toBe(''); + expect(cleanString('!')).toBe(''); + expect(cleanString('!!')).toBe(''); + expect(cleanString('!!!')).toBe(''); + expect(cleanString('!!!!')).toBe(''); + expect(cleanString('@')).toBe(''); expect(cleanString('!@#$%^&*()_+{}|')).toBe(''); - expect(cleanString(' Hey, this is great! Success. ')).toBe('Hey this is great Success'); + + expect(cleanString('Hey, you.')).toBe('Hey, you.'); + expect(cleanString(' Hey, you. ')).toBe('Hey, you.'); + expect(cleanString(' Hey? ')).toBe('Hey?'); + expect(cleanString('Hey?')).toBe('Hey?'); + expect(cleanString('Hey, this is great! Success.')).toBe('Hey, this is great! Success.'); + expect(cleanString(' Hey, this is great! Success. ')).toBe( + 'Hey, this is great! Success.' + ); }); it('should leave markdown alone', () => { diff --git a/packages/kbn-eslint-plugin-i18n/helpers/utils.ts b/packages/kbn-eslint-plugin-i18n/helpers/utils.ts index bfa0ceaf2aecc..0c212b5507a2d 100644 --- a/packages/kbn-eslint-plugin-i18n/helpers/utils.ts +++ b/packages/kbn-eslint-plugin-i18n/helpers/utils.ts @@ -24,10 +24,35 @@ function isUpperCase(val: string) { return /^[A-Z]+$/.test(val); } -export function cleanString(str: string) { +export function cleanIdentifier(str: string) { return str .replace(/```\w*```/g, '') .replace(/\s+/g, ' ') .replace(/[^a-zA-Z\s]*/g, '') .trim(); } + +export function cleanString(str: string) { + const strTrimmed = str.trim(); + + if (strTrimmed.length === 1) { + return ''; + } + + // Numbers + if (strTrimmed.replace(/[0-9]+/g, '').length === 0) { + return ''; + } + + // Markdown + if (strTrimmed.replace(/```\w*```/g, '').length === 0) { + return ''; + } + + // Special characters + if (strTrimmed.replace(/[!\@\#\$\%\^\&\*\(\)\_\+\{\}\|]+/g, '').length === 0) { + return ''; + } + + return strTrimmed; +} diff --git a/packages/kbn-eslint-plugin-i18n/jest.config.js b/packages/kbn-eslint-plugin-i18n/jest.config.js index 1ab5f78f024bf..eb06fa28e20ef 100644 --- a/packages/kbn-eslint-plugin-i18n/jest.config.js +++ b/packages/kbn-eslint-plugin-i18n/jest.config.js @@ -10,4 +10,7 @@ module.exports = { preset: '@kbn/test/jest_node', rootDir: '../..', roots: ['/packages/kbn-eslint-plugin-i18n'], + globals: { + structuredClone: {}, + }, };