|
| 1 | +/** |
| 2 | + * @fileoverview Forbid certain elements |
| 3 | + * @author Kenneth Chung |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +var has = require('has'); |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +module.exports = { |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: 'Forbid certain elements', |
| 17 | + category: 'Best Practices', |
| 18 | + recommended: false |
| 19 | + }, |
| 20 | + |
| 21 | + schema: [{ |
| 22 | + type: 'object', |
| 23 | + properties: { |
| 24 | + forbid: { |
| 25 | + type: 'array', |
| 26 | + items: { |
| 27 | + anyOf: [ |
| 28 | + {type: 'string'}, |
| 29 | + { |
| 30 | + type: 'object', |
| 31 | + properties: { |
| 32 | + element: {type: 'string'}, |
| 33 | + message: {type: 'string'} |
| 34 | + }, |
| 35 | + required: ['element'], |
| 36 | + additionalProperties: false |
| 37 | + } |
| 38 | + ] |
| 39 | + } |
| 40 | + } |
| 41 | + }, |
| 42 | + additionalProperties: false |
| 43 | + }] |
| 44 | + }, |
| 45 | + |
| 46 | + create: function(context) { |
| 47 | + var sourceCode = context.getSourceCode(); |
| 48 | + var configuration = context.options[0] || {}; |
| 49 | + var forbidConfiguration = configuration.forbid || []; |
| 50 | + |
| 51 | + var indexedForbidConfigs = {}; |
| 52 | + |
| 53 | + forbidConfiguration.forEach(function(item) { |
| 54 | + if (typeof item === 'string') { |
| 55 | + indexedForbidConfigs[item] = {element: item}; |
| 56 | + } else { |
| 57 | + indexedForbidConfigs[item.element] = item; |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + function errorMessageForElement(name) { |
| 62 | + var message = '<' + name + '> is forbidden'; |
| 63 | + var additionalMessage = indexedForbidConfigs[name].message; |
| 64 | + |
| 65 | + if (additionalMessage) { |
| 66 | + message = message + ', ' + additionalMessage; |
| 67 | + } |
| 68 | + |
| 69 | + return message; |
| 70 | + } |
| 71 | + |
| 72 | + function isValidCreateElement(node) { |
| 73 | + return node.callee |
| 74 | + && node.callee.type === 'MemberExpression' |
| 75 | + && node.callee.object.name === 'React' |
| 76 | + && node.callee.property.name === 'createElement' |
| 77 | + && node.arguments.length > 0; |
| 78 | + } |
| 79 | + |
| 80 | + function reportIfForbidden(element, node) { |
| 81 | + if (has(indexedForbidConfigs, element)) { |
| 82 | + context.report({ |
| 83 | + node: node, |
| 84 | + message: errorMessageForElement(element) |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return { |
| 90 | + JSXOpeningElement: function(node) { |
| 91 | + reportIfForbidden(sourceCode.getText(node.name), node.name); |
| 92 | + }, |
| 93 | + |
| 94 | + CallExpression: function(node) { |
| 95 | + if (!isValidCreateElement(node)) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + var argument = node.arguments[0]; |
| 100 | + var argType = argument.type; |
| 101 | + |
| 102 | + if (argType === 'Identifier' && /^[A-Z_]/.test(argument.name)) { |
| 103 | + reportIfForbidden(argument.name, argument); |
| 104 | + } else if (argType === 'Literal' && /^[a-z][^\.]*$/.test(argument.value)) { |
| 105 | + reportIfForbidden(argument.value, argument); |
| 106 | + } else if (argType === 'MemberExpression') { |
| 107 | + reportIfForbidden(sourceCode.getText(argument), argument); |
| 108 | + } |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | +}; |
0 commit comments