|
| 1 | +/** |
| 2 | + * @fileoverview Utility functions for React components detection |
| 3 | + * @author Yannick Croissant |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Record that a particular variable has been used in code |
| 9 | + * |
| 10 | + * Similar to the one coming from ESLint but with a patch for babel-eslint |
| 11 | + * to avoid https://github.com/babel/babel-eslint/issues/21 |
| 12 | + * |
| 13 | + * @param {String} name The name of the variable to mark as used. |
| 14 | + * @returns {Boolean} True if the variable was found and marked as used, false if not. |
| 15 | + */ |
| 16 | +function markVariableAsUsed(context, name) { |
| 17 | + var scope = context.getScope(); |
| 18 | + var variables; |
| 19 | + var i; |
| 20 | + var len; |
| 21 | + |
| 22 | + // Special Node.js scope means we need to start one level deeper |
| 23 | + if (scope.type === 'global' && scope.childScopes.length) { |
| 24 | + scope = scope.childScopes[0]; |
| 25 | + if (scope.childScopes.length) { |
| 26 | + scope = scope.childScopes[0]; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + do { |
| 31 | + variables = scope.variables; |
| 32 | + for (i = 0, len = variables.length; i < len; i++) { |
| 33 | + if (variables[i].name === name) { |
| 34 | + variables[i].eslintUsed = true; |
| 35 | + return true; |
| 36 | + } |
| 37 | + } |
| 38 | + scope = scope.upper; |
| 39 | + } while (scope); |
| 40 | + |
| 41 | + return false; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Search a particular variable in a list |
| 46 | + * @param {Array} variables The variables list. |
| 47 | + * @param {Array} name The name of the variable to search. |
| 48 | + * @returns {Boolean} True if the variable was found, false if not. |
| 49 | + */ |
| 50 | +function findVariable(variables, name) { |
| 51 | + var i; |
| 52 | + var len; |
| 53 | + |
| 54 | + for (i = 0, len = variables.length; i < len; i++) { |
| 55 | + if (variables[i].name === name) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return false; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * List all variable in a given scope |
| 65 | + * |
| 66 | + * Contain a patch for babel-eslint to avoid https://github.com/babel/babel-eslint/issues/21 |
| 67 | + * |
| 68 | + * @param {Object} context The current rule context. |
| 69 | + * @param {Array} name The name of the variable to search. |
| 70 | + * @returns {Boolean} True if the variable was found, false if not. |
| 71 | + */ |
| 72 | +function variablesInScope(context) { |
| 73 | + var scope = context.getScope(); |
| 74 | + var variables = scope.variables; |
| 75 | + |
| 76 | + while (scope.type !== 'global') { |
| 77 | + scope = scope.upper; |
| 78 | + variables = scope.variables.concat(variables); |
| 79 | + } |
| 80 | + if (scope.childScopes.length) { |
| 81 | + variables = scope.childScopes[0].variables.concat(variables); |
| 82 | + if (scope.childScopes[0].childScopes.length) { |
| 83 | + variables = scope.childScopes[0].childScopes[0].variables.concat(variables); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return variables; |
| 88 | +} |
| 89 | + |
| 90 | +module.exports = { |
| 91 | + findVariable: findVariable, |
| 92 | + variablesInScope: variablesInScope, |
| 93 | + markVariableAsUsed: markVariableAsUsed |
| 94 | +}; |
0 commit comments