eslint-plugin-react-form-fields
Version:
React Form Fields specific linting rules for ESLint
58 lines (57 loc) • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_ast_utils_1 = require("jsx-ast-utils");
const docsUrl_1 = require("../utils/docsUrl");
const isCheckboxOrRadioInput_1 = require("../utils/isCheckboxOrRadioInput");
const isFormFieldTags_1 = require("../utils/isFormFieldTags");
const isHiddenInput_1 = require("../utils/isHiddenInput");
const rule = {
meta: {
docs: {
description: 'Forbid to specify only value/checked without onChange and readOnly props to form fields',
recommended: 'error',
url: (0, docsUrl_1.docsUrl)('no-only-value-prop'),
},
messages: {
'no-only-value-prop': 'You provided a `{{valueName}}` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use `{{defaultValueName}}`. Otherwise, set either `onChange` or `readOnly`.',
},
schema: [],
type: 'problem',
},
create: (context) => {
return {
JSXElement(node) {
if (!(0, isFormFieldTags_1.isFormFieldTags)(node) || (0, isHiddenInput_1.isHiddenInput)(node)) {
return;
}
const hasOnChangeProp = (0, jsx_ast_utils_1.hasProp)(node.openingElement.attributes, 'onChange');
const hasReadOnlyProp = (0, jsx_ast_utils_1.hasProp)(node.openingElement.attributes, 'readOnly');
if ((0, isCheckboxOrRadioInput_1.isCheckboxOrRadioInput)(node)) {
const hasCheckedProp = (0, jsx_ast_utils_1.hasProp)(node.openingElement.attributes, 'checked');
if (hasCheckedProp && !hasOnChangeProp && !hasReadOnlyProp) {
context.report({
node,
messageId: 'no-only-value-prop',
data: {
valueName: 'checked',
defaultValueName: 'defaultChecked',
},
});
}
}
const hasValueProp = (0, jsx_ast_utils_1.hasProp)(node.openingElement.attributes, 'value');
if (hasValueProp && !hasOnChangeProp && !hasReadOnlyProp) {
context.report({
node,
messageId: 'no-only-value-prop',
data: {
valueName: 'value',
defaultValueName: 'defaultValue',
},
});
}
},
};
},
};
exports.default = rule;