d2-ui
Version:
61 lines (47 loc) • 1.77 kB
JavaScript
/**
* @fileoverview Enforce label tags have htmlFor attribute.
* @author Ethan Cohen
*/
;
// ----------------------------------------------------------------------------
// Rule Definition
// ----------------------------------------------------------------------------
var _getAttribute = require('../util/getAttribute');
var _getAttribute2 = _interopRequireDefault(_getAttribute);
var _getAttributeValue = require('../util/getAttributeValue');
var _getAttributeValue2 = _interopRequireDefault(_getAttributeValue);
var _getNodeType = require('../util/getNodeType');
var _getNodeType2 = _interopRequireDefault(_getNodeType);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var errorMessage = 'Form controls using a label to identify them must be ' + 'programmatically associated with the control using htmlFor';
module.exports = function (context) {
return {
JSXOpeningElement: function JSXOpeningElement(node) {
var typeCheck = ['label'].concat(context.options[0]);
var nodeType = (0, _getNodeType2.default)(node);
// Only check 'label' elements and custom types.
if (typeCheck.indexOf(nodeType) === -1) {
return;
}
var htmlForAttr = (0, _getAttribute2.default)(node.attributes, 'htmlFor');
var htmlForValue = (0, _getAttributeValue2.default)(htmlForAttr);
var isInvalid = htmlForAttr === false || !htmlForValue;
if (isInvalid) {
context.report({
node: node,
message: errorMessage
});
}
}
};
};
module.exports.schema = [{
'oneOf': [{ 'type': 'string' }, {
'type': 'array',
'items': {
'type': 'string'
},
'minItems': 1,
'uniqueItems': true
}]
}];