|
| 1 | +/** |
| 2 | + * @fileoverview Forbid certain propTypes |
| 3 | + */ |
| 4 | +'use strict'; |
| 5 | + |
| 6 | +// ------------------------------------------------------------------------------ |
| 7 | +// Constants |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +var DEFAULTS = ['any', 'array', 'object']; |
| 11 | + |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | +// Rule Definition |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | + |
| 16 | +module.exports = function(context) { |
| 17 | + |
| 18 | + function isForbidden(type) { |
| 19 | + var configuration = context.options[0] || {}; |
| 20 | + |
| 21 | + var forbid = configuration.forbid || DEFAULTS; |
| 22 | + return forbid.indexOf(type) >= 0; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Checks if node is `propTypes` declaration |
| 27 | + * @param {ASTNode} node The AST node being checked. |
| 28 | + * @returns {Boolean} True if node is `propTypes` declaration, false if not. |
| 29 | + */ |
| 30 | + function isPropTypesDeclaration(node) { |
| 31 | + |
| 32 | + // Special case for class properties |
| 33 | + // (babel-eslint does not expose property name so we have to rely on tokens) |
| 34 | + if (node.type === 'ClassProperty') { |
| 35 | + var tokens = context.getFirstTokens(node, 2); |
| 36 | + if (tokens[0].value === 'propTypes' || tokens[1].value === 'propTypes') { |
| 37 | + return true; |
| 38 | + } |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + return Boolean( |
| 43 | + node && |
| 44 | + node.name === 'propTypes' |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Checks if propTypes declarations are forbidden |
| 51 | + * @param {Array} declarations The array of AST nodes being checked. |
| 52 | + * @returns {void} |
| 53 | + */ |
| 54 | + function checkForbidden(declarations) { |
| 55 | + declarations.forEach(function(declaration) { |
| 56 | + if ( |
| 57 | + declaration.value.type === 'MemberExpression' && |
| 58 | + declaration.value.property && |
| 59 | + declaration.value.property.name && |
| 60 | + declaration.value.property.name === 'isRequired' |
| 61 | + ) { |
| 62 | + declaration.value = declaration.value.object; |
| 63 | + } |
| 64 | + |
| 65 | + if (isForbidden(declaration.value.property.name)) { |
| 66 | + context.report(declaration, 'Prop type `' + declaration.value.property.name + '` is forbidden'); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + ClassProperty: function(node) { |
| 73 | + if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') { |
| 74 | + checkForbidden(node.value.properties); |
| 75 | + } |
| 76 | + }, |
| 77 | + |
| 78 | + MemberExpression: function(node) { |
| 79 | + if (isPropTypesDeclaration(node.property)) { |
| 80 | + var right = node.parent.right; |
| 81 | + if (right && right.type === 'ObjectExpression') { |
| 82 | + checkForbidden(right.properties); |
| 83 | + } |
| 84 | + } |
| 85 | + }, |
| 86 | + |
| 87 | + ObjectExpression: function(node) { |
| 88 | + node.properties.forEach(function(property) { |
| 89 | + if (!property.key) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (!isPropTypesDeclaration(property.key)) { |
| 94 | + return; |
| 95 | + } |
| 96 | + if (property.value.type === 'ObjectExpression') { |
| 97 | + checkForbidden(property.value.properties); |
| 98 | + } |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + }; |
| 103 | +}; |
| 104 | + |
| 105 | +module.exports.schema = [{ |
| 106 | + type: 'object', |
| 107 | + properties: { |
| 108 | + forbid: { |
| 109 | + type: 'array', |
| 110 | + items: { |
| 111 | + type: 'string' |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + additionalProperties: true |
| 116 | +}]; |
0 commit comments