Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/pmb/node_modules/eslint-plugin-react/lib/rules/ drwxr-xr-x | |
| Viewing file: Select action/file-type: /**
* @fileoverview Require component props to be typed as read-only.
* @author Luke Zapart
*/
'use strict';
const Components = require('../util/Components');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
function isFlowPropertyType(node) {
return node.type === 'ObjectTypeProperty';
}
function isCovariant(node) {
return (node.variance && node.variance.kind === 'plus')
|| (
node.parent
&& node.parent.parent
&& node.parent.parent.parent
&& node.parent.parent.parent.id
&& node.parent.parent.parent.id.name === '$ReadOnly'
);
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const messages = {
readOnlyProp: 'Prop \'{{name}}\' should be read-only.',
};
module.exports = {
meta: {
docs: {
description: 'Enforce that props are read-only',
category: 'Stylistic Issues',
recommended: false,
url: docsUrl('prefer-read-only-props'),
},
fixable: 'code',
messages,
schema: [],
},
create: Components.detect((context, components) => ({
'Program:exit'() {
const list = components.list();
Object.keys(list).forEach((key) => {
const component = list[key];
if (!component.declaredPropTypes) {
return;
}
Object.keys(component.declaredPropTypes).forEach((propName) => {
const prop = component.declaredPropTypes[propName];
if (!prop.node || !isFlowPropertyType(prop.node)) {
return;
}
if (!isCovariant(prop.node)) {
report(context, messages.readOnlyProp, 'readOnlyProp', {
node: prop.node,
data: {
name: propName,
},
fix: (fixer) => {
if (!prop.node.variance) {
// Insert covariance
return fixer.insertTextBefore(prop.node, '+');
}
// Replace contravariance with covariance
return fixer.replaceText(prop.node.variance, '+');
},
});
}
});
});
},
})),
};
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0045 ]-- |