Skip to content

Commit

Permalink
refactor: revert switch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryBrown0 committed Jun 29, 2023
1 parent 39a44eb commit 7f084b1
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions lib/rules/prefer-read-only-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
}
});
});
Expand Down

0 comments on commit 7f084b1

Please sign in to comment.