Skip to content

Commit

Permalink
refactor: switch statement to detect property type
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryBrown0 committed Jun 29, 2023
1 parent 0b87510 commit 39a44eb
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions lib/rules/prefer-read-only-props.js
Expand Up @@ -12,13 +12,8 @@ const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');

function isFlowPropertyType(node) {
return node.type === 'ObjectTypeProperty';
}

function isTypeScriptPropertyType(node) {
return node.type === 'TSPropertySignature';
}
const FLOW_PROPERTY_TYPE = 'ObjectTypeProperty';
const TYPESCRIPT_PROPERTY_TYPE = 'TSPropertySignature';

function isCovariant(node) {
return (node.variance && node.variance.kind === 'plus')
Expand Down Expand Up @@ -85,18 +80,12 @@ module.exports = {
return;

Check warning on line 80 in lib/rules/prefer-read-only-props.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/prefer-read-only-props.js#L80

Added line #L80 was not covered by tests
}

if (isTypeScriptPropertyType(prop.node)) {
if (!isReadonly(prop.node)) {
reportReadOnlyProp(prop, propName, (fixer) => (
fixer.insertTextBefore(prop.node, 'readonly ')
));
}
switch (prop.node.type) {
case FLOW_PROPERTY_TYPE:
if (isCovariant(prop.node)) {
break;
}

return;
}

if (isFlowPropertyType(prop.node)) {
if (!isCovariant(prop.node)) {
reportReadOnlyProp(prop, propName, (fixer) => {
if (!prop.node.variance) {
// Insert covariance
Expand All @@ -106,7 +95,20 @@ module.exports = {
// Replace contravariance with covariance
return fixer.replaceText(prop.node.variance, '+');
});
}

break;
case TYPESCRIPT_PROPERTY_TYPE:
if (isReadonly(prop.node)) {
break;

Check warning on line 102 in lib/rules/prefer-read-only-props.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/prefer-read-only-props.js#L100-L102

Added lines #L100 - L102 were not covered by tests
}

reportReadOnlyProp(prop, propName, (fixer) => (
fixer.insertTextBefore(prop.node, 'readonly ')

Check warning on line 106 in lib/rules/prefer-read-only-props.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/prefer-read-only-props.js#L105-L106

Added lines #L105 - L106 were not covered by tests
));

break;

Check warning on line 109 in lib/rules/prefer-read-only-props.js

View check run for this annotation

Codecov / codecov/patch

lib/rules/prefer-read-only-props.js#L109

Added line #L109 was not covered by tests
default:
break;
}
});
});
Expand Down

0 comments on commit 39a44eb

Please sign in to comment.