|
| 1 | +const _ = require("lodash"); |
| 2 | +const utils = require("../utils"); |
| 3 | + |
| 4 | +const MAP_SCHEMA = { |
| 5 | + type: "object", |
| 6 | + additionalProperties: true, |
| 7 | +}; |
| 8 | + |
| 9 | +const FIX_TYPES = { |
| 10 | + PROP_NAME: "propName", |
| 11 | + FUNCTION_NAME: "functionName", |
| 12 | +}; |
| 13 | + |
| 14 | +module.exports = { |
| 15 | + meta: { |
| 16 | + docs: { |
| 17 | + description: "function or some of the props sent to it are deprecated", |
| 18 | + category: "Best Practices", |
| 19 | + recommended: true, |
| 20 | + }, |
| 21 | + messages: { |
| 22 | + uiLib: "This function is deprecated or contains deprecated props.", |
| 23 | + }, |
| 24 | + fixable: "code", |
| 25 | + schema: [MAP_SCHEMA], |
| 26 | + }, |
| 27 | + create(context) { |
| 28 | + function reportDeprecatedFunction(node, options) { |
| 29 | + try { |
| 30 | + const { dueDate } = context.options[0]; |
| 31 | + const dueDateNotice = dueDate |
| 32 | + ? ` Please fix this issue by ${dueDate}!` |
| 33 | + : ""; |
| 34 | + const msg = |
| 35 | + options.prop === undefined |
| 36 | + ? `The '${options.name}' function is deprecated. ${options.message}${dueDateNotice}` |
| 37 | + : `The '${options.name}' function's prop '${options.prop}' is deprecated. ${options.message}${dueDateNotice}`; |
| 38 | + |
| 39 | + context.report({ |
| 40 | + node, |
| 41 | + message: `${msg}`, |
| 42 | + fix(fixer) { |
| 43 | + if (options.fix) { |
| 44 | + const type = Object.keys(options.fix)[0]; |
| 45 | + const fix = Object.values(options.fix)[0]; |
| 46 | + let fixed; |
| 47 | + // console.warn('fix'); |
| 48 | + switch (type) { |
| 49 | + case FIX_TYPES.PROP_NAME: |
| 50 | + // Fix for prop name change only (when prop's value and type does not change) |
| 51 | + // console.warn('fix prop'); |
| 52 | + // console.warn('from', node.arguments[options.argumentIndex]); |
| 53 | + const prop = _.find(node.arguments[options.argumentIndex].properties, prop => prop.key.name === options.prop); |
| 54 | + const propIndex = node.arguments[options.argumentIndex].properties.indexOf(prop); |
| 55 | + fixed = fixer.replaceText(node.arguments[options.argumentIndex].properties[propIndex], fix) |
| 56 | + // console.warn('to', fixed); |
| 57 | + return fixed; |
| 58 | + case FIX_TYPES.FUNCTION_NAME: |
| 59 | + if (node.type === "ImportDeclaration") { |
| 60 | + // console.warn('fix function import'); |
| 61 | + const index = utils.getSpecifierIndex(node, options.name); |
| 62 | + // console.warn('from', node.specifiers[index]); |
| 63 | + fixed = fixer.replaceText(node.specifiers[index], fix); |
| 64 | + // console.warn('to', fixed); |
| 65 | + return fixed; |
| 66 | + } |
| 67 | + |
| 68 | + // console.warn('fix function not import'); |
| 69 | + // console.warn('from', node.callee.name); |
| 70 | + fixed = fixer.replaceText(node.callee, fix) |
| 71 | + // console.warn('to', fixed); |
| 72 | + return fixed; |
| 73 | + default: |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + }); |
| 79 | + } catch (err) { |
| 80 | + console.log("Found error in: ", err, context.getFilename()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + const { deprecations, source } = context.options[0]; |
| 85 | + const relevantDeprecationsData = []; |
| 86 | + let everythingIsImported = false; |
| 87 | + |
| 88 | + function getDeprecation(value) { |
| 89 | + if (value && value.name) { |
| 90 | + const name = value.name; |
| 91 | + return _.find( |
| 92 | + deprecations, |
| 93 | + (deprecation) => deprecation.function === name |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + function searchForPossibleDeprecation(node) { |
| 99 | + const importSource = node.source.value; |
| 100 | + if (source === importSource) { |
| 101 | + const specifiers = node.specifiers; |
| 102 | + if (specifiers) { |
| 103 | + _.map(specifiers, (specifier) => { |
| 104 | + const deprecation = getDeprecation(specifier.imported); |
| 105 | + if (deprecation) { |
| 106 | + let type = FIX_TYPES.PROP_NAME; |
| 107 | + if (!deprecation.arguments) { |
| 108 | + type = FIX_TYPES.FUNCTION_NAME; |
| 109 | + reportDeprecatedFunction(node, { |
| 110 | + name: deprecation.function, |
| 111 | + message: deprecation.message, |
| 112 | + fix: deprecation.fix, |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + relevantDeprecationsData.push({ |
| 117 | + localFunctionName: specifier.local.name, |
| 118 | + type, |
| 119 | + deprecation, |
| 120 | + }); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + if (relevantDeprecationsData.length === 0) { // someone is importing everything (*) |
| 126 | + everythingIsImported = true; |
| 127 | + _.map(deprecations, deprecation => { |
| 128 | + relevantDeprecationsData.push({ |
| 129 | + localFunctionName: deprecation.function, |
| 130 | + type: deprecation.arguments ? FIX_TYPES.PROP_NAME : FIX_TYPES.FUNCTION_NAME, |
| 131 | + deprecation, |
| 132 | + }); |
| 133 | + }); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + function findRelevantDeprecation(functionName) { |
| 139 | + return _.find( |
| 140 | + relevantDeprecationsData, |
| 141 | + (relevantDeprecationData) => |
| 142 | + relevantDeprecationData.localFunctionName === functionName |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + function getArgumentsSent(node) { |
| 147 | + const argumentsSent = []; |
| 148 | + _.map(node.arguments, argument => { |
| 149 | + const propsSentToArgument = []; |
| 150 | + if (argument.properties) { |
| 151 | + _.map(argument.properties, prop => { |
| 152 | + if (prop.key && prop.key.name) { |
| 153 | + propsSentToArgument.push(prop.key.name); |
| 154 | + } |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + argumentsSent.push(propsSentToArgument); |
| 159 | + }); |
| 160 | + |
| 161 | + return argumentsSent; |
| 162 | + } |
| 163 | + |
| 164 | + function getFunctionName(node) { |
| 165 | + const propName = everythingIsImported ? 'callee.property.name' : 'callee.name'; |
| 166 | + return _.get(node, propName); |
| 167 | + } |
| 168 | + |
| 169 | + function testCallExpression(node) { |
| 170 | + const functionName = getFunctionName(node); |
| 171 | + if (functionName) { |
| 172 | + const relevantDeprecation = findRelevantDeprecation(functionName); |
| 173 | + if (relevantDeprecation) { |
| 174 | + if (relevantDeprecation.type === FIX_TYPES.PROP_NAME) { |
| 175 | + const argumentsSent = getArgumentsSent(node); |
| 176 | + _.map(relevantDeprecation.deprecation.arguments, (argument, index) => { |
| 177 | + if (argument.props && argument.props.length > 0 && argumentsSent.length >= index) { |
| 178 | + const deprecationProps = argument.props; |
| 179 | + const sentProps = argumentsSent[index]; |
| 180 | + _.map(sentProps, sentProp => { |
| 181 | + const deprecationProp = _.find(deprecationProps, deprecationProp => deprecationProp.prop === sentProp); |
| 182 | + if (deprecationProp) { |
| 183 | + reportDeprecatedFunction(node, { |
| 184 | + name: functionName, |
| 185 | + message: deprecationProp.message, |
| 186 | + argumentIndex: index, |
| 187 | + prop: deprecationProp.prop, |
| 188 | + fix: deprecationProp.fix, |
| 189 | + }); |
| 190 | + } |
| 191 | + }); |
| 192 | + } |
| 193 | + }); |
| 194 | + } else { |
| 195 | + reportDeprecatedFunction(node, { |
| 196 | + name: relevantDeprecation.deprecation.function, |
| 197 | + message: relevantDeprecation.deprecation.message, |
| 198 | + fix: relevantDeprecation.deprecation.fix, |
| 199 | + }); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return { |
| 206 | + ImportDeclaration: (node) => searchForPossibleDeprecation(node), |
| 207 | + CallExpression: (node) => relevantDeprecationsData.length > 0 && testCallExpression(node), |
| 208 | + |
| 209 | + // MemberExpression: node => localImportSpecifier && testMemberDeprecation(node), |
| 210 | + // JSXAttribute: node => testJSXAttribute(node), |
| 211 | + // JSXOpeningElement: node => testJSXOpeningElement(node), |
| 212 | + // ObjectExpression: node => testObjectExpression(node), |
| 213 | + // VariableDeclarator: node => testVariableDeclarator(node), |
| 214 | + // Property: node => testProperty(node), |
| 215 | + // JSXSpreadAttribute: node => testJSXSpreadAttribute(node) |
| 216 | + }; |
| 217 | + }, |
| 218 | +}; |
0 commit comments