Skip to content

Commit

Permalink
More comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pearce-Ropion committed Sep 3, 2024
1 parent 26303e2 commit 3c98eb2
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}}"',
Expand Down Expand Up @@ -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<string, RawOverrideConfig>} [elementOverrides]
*
* @typedef {RawElementConfig & RawOverrideProperty} RawConfig
* @typedef {RawElementConfig & RawElementOverrides} RawConfig
*
* ----------------------------------------------------------------------
*
Expand All @@ -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<string, OverrideConfig>} elementOverrides
Expand Down Expand Up @@ -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,
}),
]);

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 3c98eb2

Please sign in to comment.