From 58c50c3a7a502d51664ca93791d70f7d20d4455f Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Mon, 9 Dec 2024 17:55:48 +0530 Subject: [PATCH 1/7] add: Added eslint rules for no toggle word --- .eslintrc.js | 1 + packages/eslint-plugin/configs/i18n.js | 1 + .../eslint-plugin/rules/i18n-no-toggle.js | 60 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 packages/eslint-plugin/rules/i18n-no-toggle.js diff --git a/.eslintrc.js b/.eslintrc.js index d0c22090b93e87..3c36e33f1f31b6 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -180,6 +180,7 @@ module.exports = { '@wordpress/react-no-unsafe-timeout': 'error', '@wordpress/i18n-hyphenated-range': 'error', '@wordpress/i18n-no-flanking-whitespace': 'error', + '@wordpress/i18n-no-toggle': 'error', '@wordpress/i18n-text-domain': [ 'error', { diff --git a/packages/eslint-plugin/configs/i18n.js b/packages/eslint-plugin/configs/i18n.js index fb8cad233232d4..72dbda7f0ac92e 100644 --- a/packages/eslint-plugin/configs/i18n.js +++ b/packages/eslint-plugin/configs/i18n.js @@ -10,5 +10,6 @@ module.exports = { '@wordpress/i18n-ellipsis': 'error', '@wordpress/i18n-no-flanking-whitespace': 'error', '@wordpress/i18n-hyphenated-range': 'error', + '@wordpress/i18n-no-toggle': 'error', }, }; diff --git a/packages/eslint-plugin/rules/i18n-no-toggle.js b/packages/eslint-plugin/rules/i18n-no-toggle.js new file mode 100644 index 00000000000000..29be0b57e67041 --- /dev/null +++ b/packages/eslint-plugin/rules/i18n-no-toggle.js @@ -0,0 +1,60 @@ +/** + * Internal dependencies + */ +const { + TRANSLATION_FUNCTIONS, + getTextContentFromNode, + getTranslateFunctionName, + getTranslateFunctionArgs, +} = require( '../utils' ); + +module.exports = { + meta: { + type: 'problem', + schema: [], + messages: { + noToggle: + "Avoid using the verb '{{ word }}' in translatable strings.", + }, + fixable: null, + }, + create( context ) { + return { + CallExpression( node ) { + const { callee, arguments: args } = node; + + const functionName = getTranslateFunctionName( callee ); + + if ( ! TRANSLATION_FUNCTIONS.has( functionName ) ) { + return; + } + + const candidates = getTranslateFunctionArgs( + functionName, + args + ); + + for ( const arg of candidates ) { + const argumentString = getTextContentFromNode( arg ); + if ( ! argumentString ) { + continue; + } + + const match = /\btoggle\b/i.exec( argumentString ); + + if ( match ) { + const matchedWord = match[ 0 ]; + + context.report( { + node, + messageId: 'noToggle', + data: { + word: matchedWord, + }, + } ); + } + } + }, + }; + }, +}; From 2b74256193de1c7e32438ed0471e2559190d928d Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 11 Dec 2024 11:26:48 +0530 Subject: [PATCH 2/7] add: Add tests for the eslint rule to prevent use of word toggle --- .../rules/__tests__/i18n-no-toggle.js | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js diff --git a/packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js b/packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js new file mode 100644 index 00000000000000..aec308831a5109 --- /dev/null +++ b/packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js @@ -0,0 +1,74 @@ +/** + * External dependencies + */ +import { RuleTester } from 'eslint'; + +/** + * Internal dependencies + */ +const rule = require( '../i18n-no-toggle' ); + +const ruleTester = new RuleTester( { + parserOptions: { + ecmaVersion: 6, + }, +} ); + +ruleTester.run( 'i18n-no-toggle', rule, { + valid: [ + `__('Click to switch something');`, + `__('Enable this feature.');`, + `_x('Activate the option', 'context', 'context');`, + `_n('There is one item', 'There are many items', 1, 'context');`, + `_nx('Add the item', 'Add the items', 1, 'context', 'context');`, + `i18n.__('Activate the setting.');`, + ], + + invalid: [ + { + code: `__('Click to toggle something');`, + errors: [ + { + message: + "Avoid using the verb 'toggle' in translatable strings.", + }, + ], + }, + { + code: `_x('Toggle the feature', 'context', 'context');`, + errors: [ + { + message: + "Avoid using the verb 'Toggle' in translatable strings.", + }, + ], + }, + { + code: `_n('There is one toggle', 'There are many toggles', 1, 'context');`, + errors: [ + { + message: + "Avoid using the verb 'toggle' in translatable strings.", + }, + ], + }, + { + code: `_nx('Enable the toggle', 'Enable the toggles', 1, 'context', 'context');`, + errors: [ + { + message: + "Avoid using the verb 'toggle' in translatable strings.", + }, + ], + }, + { + code: `i18n.__('Click to Toggle something');`, + errors: [ + { + message: + "Avoid using the verb 'Toggle' in translatable strings.", + }, + ], + }, + ], +} ); From 2b34273f1a560e5d8eca2daa176a1f7c17ed0b92 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 18 Dec 2024 12:59:26 +0530 Subject: [PATCH 3/7] fix: revert custom eslint rule for toggle word --- .eslintrc.js | 1 - packages/eslint-plugin/configs/i18n.js | 1 - .../rules/__tests__/i18n-no-toggle.js | 74 ------------------- .../eslint-plugin/rules/i18n-no-toggle.js | 60 --------------- 4 files changed, 136 deletions(-) delete mode 100644 packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js delete mode 100644 packages/eslint-plugin/rules/i18n-no-toggle.js diff --git a/.eslintrc.js b/.eslintrc.js index 3c36e33f1f31b6..d0c22090b93e87 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -180,7 +180,6 @@ module.exports = { '@wordpress/react-no-unsafe-timeout': 'error', '@wordpress/i18n-hyphenated-range': 'error', '@wordpress/i18n-no-flanking-whitespace': 'error', - '@wordpress/i18n-no-toggle': 'error', '@wordpress/i18n-text-domain': [ 'error', { diff --git a/packages/eslint-plugin/configs/i18n.js b/packages/eslint-plugin/configs/i18n.js index 72dbda7f0ac92e..fb8cad233232d4 100644 --- a/packages/eslint-plugin/configs/i18n.js +++ b/packages/eslint-plugin/configs/i18n.js @@ -10,6 +10,5 @@ module.exports = { '@wordpress/i18n-ellipsis': 'error', '@wordpress/i18n-no-flanking-whitespace': 'error', '@wordpress/i18n-hyphenated-range': 'error', - '@wordpress/i18n-no-toggle': 'error', }, }; diff --git a/packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js b/packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js deleted file mode 100644 index aec308831a5109..00000000000000 --- a/packages/eslint-plugin/rules/__tests__/i18n-no-toggle.js +++ /dev/null @@ -1,74 +0,0 @@ -/** - * External dependencies - */ -import { RuleTester } from 'eslint'; - -/** - * Internal dependencies - */ -const rule = require( '../i18n-no-toggle' ); - -const ruleTester = new RuleTester( { - parserOptions: { - ecmaVersion: 6, - }, -} ); - -ruleTester.run( 'i18n-no-toggle', rule, { - valid: [ - `__('Click to switch something');`, - `__('Enable this feature.');`, - `_x('Activate the option', 'context', 'context');`, - `_n('There is one item', 'There are many items', 1, 'context');`, - `_nx('Add the item', 'Add the items', 1, 'context', 'context');`, - `i18n.__('Activate the setting.');`, - ], - - invalid: [ - { - code: `__('Click to toggle something');`, - errors: [ - { - message: - "Avoid using the verb 'toggle' in translatable strings.", - }, - ], - }, - { - code: `_x('Toggle the feature', 'context', 'context');`, - errors: [ - { - message: - "Avoid using the verb 'Toggle' in translatable strings.", - }, - ], - }, - { - code: `_n('There is one toggle', 'There are many toggles', 1, 'context');`, - errors: [ - { - message: - "Avoid using the verb 'toggle' in translatable strings.", - }, - ], - }, - { - code: `_nx('Enable the toggle', 'Enable the toggles', 1, 'context', 'context');`, - errors: [ - { - message: - "Avoid using the verb 'toggle' in translatable strings.", - }, - ], - }, - { - code: `i18n.__('Click to Toggle something');`, - errors: [ - { - message: - "Avoid using the verb 'Toggle' in translatable strings.", - }, - ], - }, - ], -} ); diff --git a/packages/eslint-plugin/rules/i18n-no-toggle.js b/packages/eslint-plugin/rules/i18n-no-toggle.js deleted file mode 100644 index 29be0b57e67041..00000000000000 --- a/packages/eslint-plugin/rules/i18n-no-toggle.js +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Internal dependencies - */ -const { - TRANSLATION_FUNCTIONS, - getTextContentFromNode, - getTranslateFunctionName, - getTranslateFunctionArgs, -} = require( '../utils' ); - -module.exports = { - meta: { - type: 'problem', - schema: [], - messages: { - noToggle: - "Avoid using the verb '{{ word }}' in translatable strings.", - }, - fixable: null, - }, - create( context ) { - return { - CallExpression( node ) { - const { callee, arguments: args } = node; - - const functionName = getTranslateFunctionName( callee ); - - if ( ! TRANSLATION_FUNCTIONS.has( functionName ) ) { - return; - } - - const candidates = getTranslateFunctionArgs( - functionName, - args - ); - - for ( const arg of candidates ) { - const argumentString = getTextContentFromNode( arg ); - if ( ! argumentString ) { - continue; - } - - const match = /\btoggle\b/i.exec( argumentString ); - - if ( match ) { - const matchedWord = match[ 0 ]; - - context.report( { - node, - messageId: 'noToggle', - data: { - word: matchedWord, - }, - } ); - } - } - }, - }; - }, -}; From 340765291879c666c5ecd0788f48b1db1ffc22b3 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 18 Dec 2024 13:07:43 +0530 Subject: [PATCH 4/7] fix: Add Eslint restrictedSyntax for toggle word --- .eslintrc.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index d0c22090b93e87..032ca3b17e4e5e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -137,6 +137,10 @@ const restrictedSyntax = [ message: 'Avoid truthy checks on length property rendering, as zero length is rendered verbatim.', }, + { + selector: 'Literal[value=/^toggle\\b/i]', + message: "Avoid using the verb 'Toggle' in translatable strings", + }, ]; /** `no-restricted-syntax` rules for components. */ From 420aeb73472eee4ab8b8250df92ccbb30ac78c31 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 18 Dec 2024 16:45:51 +0530 Subject: [PATCH 5/7] fix: Add Eslint CallExpression for toggle word --- .eslintrc.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index 032ca3b17e4e5e..e5f42eea656b90 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -138,7 +138,8 @@ const restrictedSyntax = [ 'Avoid truthy checks on length property rendering, as zero length is rendered verbatim.', }, { - selector: 'Literal[value=/^toggle\\b/i]', + selector: + 'CallExpression[callee.name=/^(__|_x|_n|_nx)$/] > Literal[value=/^toggle\\b/i]', message: "Avoid using the verb 'Toggle' in translatable strings", }, ]; From 3a502d746d57fc77d61e7bd7ad8626e5b10faa32 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Thu, 19 Dec 2024 15:00:00 +0530 Subject: [PATCH 6/7] fix: Removed usage of toggle word --- .../src/components/responsive-block-control/index.js | 2 +- packages/block-library/src/table-of-contents/edit.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/responsive-block-control/index.js b/packages/block-editor/src/components/responsive-block-control/index.js index 148ba9600f0032..388e7ec543693a 100644 --- a/packages/block-editor/src/components/responsive-block-control/index.js +++ b/packages/block-editor/src/components/responsive-block-control/index.js @@ -57,7 +57,7 @@ function ResponsiveBlockControl( props ) { ); const toggleHelpText = __( - 'Toggle between using the same value for all screen sizes or using a unique value per screen size.' + 'Choose whether to use the same value for all screen sizes or a unique value for each screen size.' ); const defaultControl = renderDefaultControl( diff --git a/packages/block-library/src/table-of-contents/edit.js b/packages/block-library/src/table-of-contents/edit.js index c95b89200cb88c..735875fd6ed4e0 100644 --- a/packages/block-library/src/table-of-contents/edit.js +++ b/packages/block-library/src/table-of-contents/edit.js @@ -122,7 +122,7 @@ export default function TableOfContentsEdit( { 'Only including headings from the current page (if the post is paginated).' ) : __( - 'Toggle to only include headings from the current page (if the post is paginated).' + 'Only include headings from the current page (if the post is paginated).' ) } /> From d54337cded1248e0b30d53a4a6ca2e3d2c0f1262 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Thu, 19 Dec 2024 15:14:17 +0530 Subject: [PATCH 7/7] fix: Updated static string used in help prop --- packages/block-library/src/table-of-contents/edit.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-library/src/table-of-contents/edit.js b/packages/block-library/src/table-of-contents/edit.js index 735875fd6ed4e0..394ff2666067d4 100644 --- a/packages/block-library/src/table-of-contents/edit.js +++ b/packages/block-library/src/table-of-contents/edit.js @@ -122,7 +122,7 @@ export default function TableOfContentsEdit( { 'Only including headings from the current page (if the post is paginated).' ) : __( - 'Only include headings from the current page (if the post is paginated).' + 'Include headings from all pages (if the post is paginated).' ) } />