From 3c98eb2cd580e2abec9122765b82ab1d28151c8f Mon Sep 17 00:00:00 2001 From: Pearce Date: Mon, 2 Sep 2024 19:16:11 -0700 Subject: [PATCH] More comments --- lib/rules/jsx-no-literals.js | 39 ++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index ec7550a6c7..93e9a99106 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -31,7 +31,9 @@ function trimIfString(value) { const overridableElementPattern = '^[A-Z][\\w.]*$'; const reOverridableElement = new RegExp(overridableElementPattern); +const reIsWhiteSpace = /^[\s]+$/; const jsxElementTypes = new Set(['JSXElement', 'JSXFragment']); +const standardJSXNodeParentTypes = new Set(['JSXAttribute', 'JSXElement', 'JSXExpressionContainer', 'JSXFragment']); const messages = { invalidPropValue: 'Invalid prop value: "{{text}}"', @@ -65,22 +67,23 @@ const commonPropertiesSchema = { }; /** - * @typedef RawElementConfig + * @typedef RawElementConfigProperties * @property {boolean} [noStrings] * @property {string[]} [allowedStrings] * @property {boolean} [ignoreProps] * @property {boolean} [noAttributeStrings] * - * @typedef RawOverrideOnlyConfig + * @typedef RawOverrideConfigProperties * @property {boolean} [allowElement] * @property {boolean} [applyToNestedElements=true] * - * @typedef {RawElementConfig & RawOverrideOnlyConfig} RawOverrideConfig + * @typedef {RawElementConfigProperties} RawElementConfig + * @typedef {RawElementConfigProperties & RawElementConfigProperties} RawOverrideConfig * - * @typedef RawOverrideProperty + * @typedef RawElementOverrides * @property {Record} [elementOverrides] * - * @typedef {RawElementConfig & RawOverrideProperty} RawConfig + * @typedef {RawElementConfig & RawElementOverrides} RawConfig * * ---------------------------------------------------------------------- * @@ -93,17 +96,14 @@ const commonPropertiesSchema = { * @property {boolean} ignoreProps * @property {boolean} noAttributeStrings * - * @typedef OverrideConfigType - * @property {'override'} type - * * @typedef OverrideConfigProperties + * @property {'override'} type * @property {string} name * @property {boolean} allowElement * @property {boolean} applyToNestedElements * * @typedef {ElementConfigType & ElementConfigProperties} ElementConfig - * - * @typedef {OverrideConfigType & ElementConfigProperties & OverrideConfigProperties} OverrideConfig + * @typedef {OverrideConfigProperties & ElementConfigProperties} OverrideConfig * * @typedef ElementOverrides * @property {Record} elementOverrides @@ -145,19 +145,18 @@ function normalizeConfig(config) { reduce( iterFrom(entries(config.elementOverrides)), (acc, entry) => { - const key = entry[0]; - const value = entry[1]; + const elementName = entry[0]; + const rawElementConfig = entry[1]; - if (!(key && value)) return acc; - if (!reOverridableElement.test(key)) return acc; + if (!reOverridableElement.test(elementName)) return acc; acc.push([ - key, - Object.assign(normalizeElementConfig(value), { + elementName, + Object.assign(normalizeElementConfig(rawElementConfig), { type: 'override', - name: key, - allowElement: !!value.allowElement, - applyToNestedElements: value.applyToNestedElements !== undefined ? value.applyToNestedElements : true, + name: elementName, + allowElement: !!rawElementConfig.allowElement, + applyToNestedElements: rawElementConfig.applyToNestedElements !== undefined ? rawElementConfig.applyToNestedElements : true, }), ]); @@ -368,7 +367,7 @@ module.exports = { const parent = getParentIgnoringBinaryExpressions(node); let isStandardJSXNode = false; - if (typeof node.value === 'string' && !/^[\s]+$/.test(node.value) && parent.type.startsWith('JSX')) { + if (typeof node.value === 'string' && !reIsWhiteSpace.test(node.value) && standardJSXNodeParentTypes.has(parent.type)) { if (resolvedConfig.noAttributeStrings) { isStandardJSXNode = parent.type === 'JSXAttribute' || parent.type === 'JSXElement'; } else {