From 7f084b1f9e812279d6ff92f4f06e3c6221b1ed08 Mon Sep 17 00:00:00 2001 From: HenryBrown0 <26250092+HenryBrown0@users.noreply.github.com> Date: Thu, 29 Jun 2023 18:06:13 +0100 Subject: [PATCH] refactor: revert switch statement --- lib/rules/prefer-read-only-props.js | 32 ++++++++++++++--------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/rules/prefer-read-only-props.js b/lib/rules/prefer-read-only-props.js index 8b4deddbef..31d7114643 100644 --- a/lib/rules/prefer-read-only-props.js +++ b/lib/rules/prefer-read-only-props.js @@ -12,8 +12,13 @@ const Components = require('../util/Components'); const docsUrl = require('../util/docsUrl'); const report = require('../util/report'); -const FLOW_PROPERTY_TYPE = 'ObjectTypeProperty'; -const TYPESCRIPT_PROPERTY_TYPE = 'TSPropertySignature'; +function isFlowPropertyType(node) { + return node.type === 'ObjectTypeProperty'; +} + +function isTypescriptPropertyType(node) { + return node.type === 'TSPropertySignature'; +} function isCovariant(node) { return (node.variance && node.variance.kind === 'plus') @@ -80,12 +85,8 @@ module.exports = { return; } - switch (prop.node.type) { - case FLOW_PROPERTY_TYPE: - if (isCovariant(prop.node)) { - break; - } - + if (isFlowPropertyType(prop.node)) { + if (!isCovariant(prop.node)) { reportReadOnlyProp(prop, propName, (fixer) => { if (!prop.node.variance) { // Insert covariance @@ -95,20 +96,17 @@ module.exports = { // Replace contravariance with covariance return fixer.replaceText(prop.node.variance, '+'); }); + } - break; - case TYPESCRIPT_PROPERTY_TYPE: - if (isReadonly(prop.node)) { - break; - } + return; + } + if (isTypescriptPropertyType(prop.node)) { + if (!isReadonly(prop.node)) { reportReadOnlyProp(prop, propName, (fixer) => ( fixer.insertTextBefore(prop.node, 'readonly ') )); - - break; - default: - break; + } } }); });