Skip to content

Commit

Permalink
Adress most comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Pearce-Ropion committed Sep 3, 2024
1 parent e8f20fd commit e974011
Showing 1 changed file with 64 additions and 65 deletions.
129 changes: 64 additions & 65 deletions lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ function trimIfString(value) {
return typeof value === 'string' ? value.trim() : value;
}

/**
* @param {boolean | undefined} value
* @returns {boolean}
*/
function valueOrTrue(value) {
return value !== undefined ? value : true;
}

const overridableElementPattern = '^[A-Z][\\w.]*$';
const reOverridableElement = new RegExp(overridableElementPattern);
const jsxElementTypes = new Set(['JSXElement', 'JSXFragment']);
Expand All @@ -45,18 +37,14 @@ const messages = {
invalidPropValue: 'Invalid prop value: "{{text}}"',
invalidPropValueInElement: 'Invalid prop value: "{{text}}" in {{element}}',
noStringsInAttributes: 'Strings not allowed in attributes: "{{text}}"',
noStringsInAttributesInElement:
'Strings not allowed in attributes: "{{text}}" in {{element}}',
noStringsInAttributesInElement: 'Strings not allowed in attributes: "{{text}}" in {{element}}',
noStringsInJSX: 'Strings not allowed in JSX files: "{{text}}"',
noStringsInJSXInElement:
'Strings not allowed in JSX files: "{{text}}" in {{element}}',
literalNotInJSXExpression:
'Missing JSX expression container around literal string: "{{text}}"',
literalNotInJSXExpressionInElement:
'Missing JSX expression container around literal string: "{{text}}" in {{element}}',
noStringsInJSXInElement: 'Strings not allowed in JSX files: "{{text}}" in {{element}}',
literalNotInJSXExpression: 'Missing JSX expression container around literal string: "{{text}}"',
literalNotInJSXExpressionInElement: 'Missing JSX expression container around literal string: "{{text}}" in {{element}}',
};

/** @type {import('json-schema').JSONSchema4['properties']} */
/** @type {Exclude<import('eslint').Rule.RuleModule['meta']['schema'], unknown[]>['properties']} */
const commonPropertiesSchema = {
noStrings: {
type: 'boolean',
Expand Down Expand Up @@ -96,77 +84,88 @@ const commonPropertiesSchema = {
*
* ----------------------------------------------------------------------
*
* @typedef ElementConfig
* @property {'element' | 'override'} type
* @typedef ElementConfigType
* @property {'element'} type
*
* @typedef ElementConfigProperties
* @property {boolean} noStrings
* @property {Set<string>} allowedStrings
* @property {boolean} ignoreProps
* @property {boolean} noAttributeStrings
*
* @typedef OverrideOnlyConfig
* @typedef OverrideConfigType
* @property {'override'} type
*
* @typedef OverrideConfigProperties
* @property {string} name
* @property {boolean} allowElement
* @property {boolean} applyToNestedElements
*
* @typedef {ElementConfig & OverrideOnlyConfig} OverrideConfig
* @typedef {ElementConfigType & ElementConfigProperties} ElementConfig
*
* @typedef OverrideProperty
* @property {Record<string, OverrideConfig>} elementOverrides
* @typedef {OverrideConfigType & ElementConfigProperties & OverrideConfigProperties} OverrideConfig
*
* @typedef {ElementConfig & OverrideProperty} Config
* @typedef ElementOverrides
* @property {Record<string, OverrideConfig>} elementOverrides
*
* @typedef {ElementConfig & ElementOverrides} Config
* @typedef {Config | OverrideConfig} ResolvedConfig
*/

/**
* Normalizes the config and applies default values to all config options
* Normalizes the element portion of the config
* @param {RawConfig} config
* @param {boolean} [handleElementOverrides=true]
* @returns {Config}
* @returns {ElementConfig}
*/
function normalizeConfig(config, handleElementOverrides) {
/** @type {Config} */
const normalizedConfig = {
function normalizeElementConfig(config) {
return {
type: 'element',
noStrings: config.noStrings || false,
noStrings: !!config.noStrings,
allowedStrings: config.allowedStrings
? new Set(map(iterFrom(config.allowedStrings), trimIfString))
: new Set(),
ignoreProps: config.ignoreProps || false,
noAttributeStrings: config.noAttributeStrings || false,
elementOverrides: {},
ignoreProps: !!config.ignoreProps,
noAttributeStrings: !!config.noAttributeStrings,
};
}

if (valueOrTrue(handleElementOverrides)) {
if (config.elementOverrides) {
normalizedConfig.elementOverrides = fromEntries(
reduce(
iterFrom(entries(config.elementOverrides)),
(acc, entry) => {
const key = entry[0];
const value = entry[1];

if (!(key && value)) return acc;
if (!reOverridableElement.test(key)) return acc;

acc.push([
key,
Object.assign(normalizeConfig(value, false), {
type: 'override',
name: key,
allowElement: value.allowElement || false,
applyToNestedElements: valueOrTrue(value.applyToNestedElements),
}),
]);

return acc;
},
[]
)
);
}
} else {
delete normalizedConfig.elementOverrides;
/**
* Normalizes the config and applies default values to all config options
* @param {RawConfig} config
* @returns {Config}
*/
function normalizeConfig(config) {
/** @type {Config} */
const normalizedConfig = Object.assign(normalizeElementConfig(config), {
elementOverrides: {},
});

if (config.elementOverrides) {
normalizedConfig.elementOverrides = fromEntries(
reduce(
iterFrom(entries(config.elementOverrides)),
(acc, entry) => {
const key = entry[0];
const value = entry[1];

if (!(key && value)) return acc;
if (!reOverridableElement.test(key)) return acc;

acc.push([
key,
Object.assign(normalizeElementConfig(value), {
type: 'override',
name: key,
allowElement: !!value.allowElement,
applyToNestedElements: value.applyToNestedElements !== undefined ? value.applyToNestedElements : true,
}),
]);

return acc;
},
[]
)
);
}

return normalizedConfig;
Expand Down Expand Up @@ -219,7 +218,7 @@ module.exports = {
const rawConfig = (context.options.length && context.options[0]) || {};
const config = normalizeConfig(rawConfig);

const hasElementOverrides = !!Object.keys(config.elementOverrides).length;
const hasElementOverrides = Object.keys(config.elementOverrides).length > 0;

/** @type {Map<string, string>} */
const renamedImportMap = new Map();
Expand Down

0 comments on commit e974011

Please sign in to comment.