Skip to content

Commit 39a44eb

Browse files
committed
refactor: switch statement to detect property type
1 parent 0b87510 commit 39a44eb

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ const Components = require('../util/Components');
1212
const docsUrl = require('../util/docsUrl');
1313
const report = require('../util/report');
1414

15-
function isFlowPropertyType(node) {
16-
return node.type === 'ObjectTypeProperty';
17-
}
18-
19-
function isTypeScriptPropertyType(node) {
20-
return node.type === 'TSPropertySignature';
21-
}
15+
const FLOW_PROPERTY_TYPE = 'ObjectTypeProperty';
16+
const TYPESCRIPT_PROPERTY_TYPE = 'TSPropertySignature';
2217

2318
function isCovariant(node) {
2419
return (node.variance && node.variance.kind === 'plus')
@@ -85,18 +80,12 @@ module.exports = {
8580
return;
8681
}
8782

88-
if (isTypeScriptPropertyType(prop.node)) {
89-
if (!isReadonly(prop.node)) {
90-
reportReadOnlyProp(prop, propName, (fixer) => (
91-
fixer.insertTextBefore(prop.node, 'readonly ')
92-
));
93-
}
83+
switch (prop.node.type) {
84+
case FLOW_PROPERTY_TYPE:
85+
if (isCovariant(prop.node)) {
86+
break;
87+
}
9488

95-
return;
96-
}
97-
98-
if (isFlowPropertyType(prop.node)) {
99-
if (!isCovariant(prop.node)) {
10089
reportReadOnlyProp(prop, propName, (fixer) => {
10190
if (!prop.node.variance) {
10291
// Insert covariance
@@ -106,7 +95,20 @@ module.exports = {
10695
// Replace contravariance with covariance
10796
return fixer.replaceText(prop.node.variance, '+');
10897
});
109-
}
98+
99+
break;
100+
case TYPESCRIPT_PROPERTY_TYPE:
101+
if (isReadonly(prop.node)) {
102+
break;
103+
}
104+
105+
reportReadOnlyProp(prop, propName, (fixer) => (
106+
fixer.insertTextBefore(prop.node, 'readonly ')
107+
));
108+
109+
break;
110+
default:
111+
break;
110112
}
111113
});
112114
});

0 commit comments

Comments
 (0)