|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of context |
| 3 | + * @author Zach Guo |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +var Components = require('../util/Components'); |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +// Rule Definition |
| 11 | +// ------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +module.exports = { |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: 'Prevent usage of context', |
| 17 | + category: 'Best Practices', |
| 18 | + recommended: true |
| 19 | + }, |
| 20 | + schema: [] |
| 21 | + }, |
| 22 | + |
| 23 | + create: Components.detect(function(context) { |
| 24 | + |
| 25 | + /** |
| 26 | + * Checks if we are using context |
| 27 | + * @param {ASTNode} node The AST node being checked. |
| 28 | + * @returns {Boolean} True if we are using context, false if not. |
| 29 | + */ |
| 30 | + function isContextUsage(node) { |
| 31 | + return Boolean( |
| 32 | + node.object.type === 'ThisExpression' && |
| 33 | + node.property.name === 'context' |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Checks if we are declaring a context |
| 39 | + * @param {ASTNode} node The AST node being checked. |
| 40 | + * @returns {Boolean} True if we are declaring a context, false if not. |
| 41 | + */ |
| 42 | + function isContextTypesDeclaration(node) { |
| 43 | + // Special case for class properties |
| 44 | + // (babel-eslint does not expose property name so we have to rely on tokens) |
| 45 | + if (node && node.type === 'ClassProperty') { |
| 46 | + var tokens = context.getFirstTokens(node, 2); |
| 47 | + if ( |
| 48 | + tokens[0].value === 'contextTypes' || |
| 49 | + (tokens[1] && tokens[1].value === 'contextTypes') |
| 50 | + ) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + return false; |
| 54 | + } |
| 55 | + return Boolean( |
| 56 | + node && |
| 57 | + node.name === 'contextTypes' |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param {ASTNode} node We expect either an ArrowFunctionExpression, |
| 63 | + * FunctionDeclaration, or FunctionExpression |
| 64 | + * @returns {Boolean} True if we are using context, false if not. |
| 65 | + */ |
| 66 | + function isContextUsageInStatelessComponent(node) { |
| 67 | + return Boolean( |
| 68 | + node && |
| 69 | + node.params && |
| 70 | + node.params[1] |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param {ASTNode} node We expect either an ArrowFunctionExpression, |
| 76 | + * FunctionDeclaration, or FunctionExpression |
| 77 | + */ |
| 78 | + function handleStatelessComponent(node) { |
| 79 | + if (isContextUsageInStatelessComponent(node)) { |
| 80 | + context.report({ |
| 81 | + node: node, |
| 82 | + message: 'Using context is not allowed.' |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + |
| 89 | + ClassProperty: function(node) { |
| 90 | + if (isContextTypesDeclaration(node)) { |
| 91 | + context.report({ |
| 92 | + node: node, |
| 93 | + message: 'Using context is not allowed.' |
| 94 | + }); |
| 95 | + } |
| 96 | + }, |
| 97 | + |
| 98 | + MemberExpression: function(node) { |
| 99 | + if ( |
| 100 | + isContextUsage(node) || |
| 101 | + isContextTypesDeclaration(node) || |
| 102 | + isContextTypesDeclaration(node.property) |
| 103 | + ) { |
| 104 | + context.report({ |
| 105 | + node: node, |
| 106 | + message: 'Using context is not allowed.' |
| 107 | + }); |
| 108 | + } |
| 109 | + }, |
| 110 | + |
| 111 | + ObjectExpression: function(node) { |
| 112 | + node.properties.forEach(function(property) { |
| 113 | + if (!isContextTypesDeclaration(property.key)) { |
| 114 | + return; |
| 115 | + } |
| 116 | + if (property.value.type === 'ObjectExpression') { |
| 117 | + context.report({ |
| 118 | + node: node, |
| 119 | + message: 'Using context is not allowed.' |
| 120 | + }); |
| 121 | + } |
| 122 | + }); |
| 123 | + }, |
| 124 | + |
| 125 | + FunctionDeclaration: handleStatelessComponent, |
| 126 | + |
| 127 | + ArrowFunctionExpression: handleStatelessComponent, |
| 128 | + |
| 129 | + FunctionExpression: handleStatelessComponent |
| 130 | + |
| 131 | + }; |
| 132 | + |
| 133 | + }) |
| 134 | +}; |
0 commit comments