-
Notifications
You must be signed in to change notification settings - Fork 734
Lint - add function-deprecation #780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
const _ = require("lodash"); | ||
const utils = require("../utils"); | ||
|
||
const MAP_SCHEMA = { | ||
type: "object", | ||
additionalProperties: true, | ||
}; | ||
|
||
const FIX_TYPES = { | ||
PROP_NAME: "propName", | ||
FUNCTION_NAME: "functionName", | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "function or some of the props sent to it are deprecated", | ||
category: "Best Practices", | ||
recommended: true, | ||
}, | ||
messages: { | ||
uiLib: "This function is deprecated or contains deprecated props.", | ||
}, | ||
fixable: "code", | ||
schema: [MAP_SCHEMA], | ||
}, | ||
create(context) { | ||
function reportDeprecatedFunction(node, options) { | ||
try { | ||
const { dueDate } = context.options[0]; | ||
const dueDateNotice = dueDate | ||
? ` Please fix this issue by ${dueDate}!` | ||
: ""; | ||
const msg = | ||
options.prop === undefined | ||
? `The '${options.name}' function is deprecated. ${options.message}${dueDateNotice}` | ||
: `The '${options.name}' function's prop '${options.prop}' is deprecated. ${options.message}${dueDateNotice}`; | ||
|
||
context.report({ | ||
node, | ||
message: `${msg}`, | ||
fix(fixer) { | ||
if (options.fix) { | ||
const type = Object.keys(options.fix)[0]; | ||
const fix = Object.values(options.fix)[0]; | ||
let fixed; | ||
// console.warn('fix'); | ||
switch (type) { | ||
case FIX_TYPES.PROP_NAME: | ||
// Fix for prop name change only (when prop's value and type does not change) | ||
// console.warn('fix prop'); | ||
// console.warn('from', node.arguments[options.argumentIndex]); | ||
const prop = _.find(node.arguments[options.argumentIndex].properties, prop => prop.key.name === options.prop); | ||
const propIndex = node.arguments[options.argumentIndex].properties.indexOf(prop); | ||
fixed = fixer.replaceText(node.arguments[options.argumentIndex].properties[propIndex], fix) | ||
// console.warn('to', fixed); | ||
return fixed; | ||
case FIX_TYPES.FUNCTION_NAME: | ||
if (node.type === "ImportDeclaration") { | ||
// console.warn('fix function import'); | ||
const index = utils.getSpecifierIndex(node, options.name); | ||
// console.warn('from', node.specifiers[index]); | ||
fixed = fixer.replaceText(node.specifiers[index], fix); | ||
// console.warn('to', fixed); | ||
return fixed; | ||
} | ||
|
||
// console.warn('fix function not import'); | ||
// console.warn('from', node.callee.name); | ||
fixed = fixer.replaceText(node.callee, fix) | ||
// console.warn('to', fixed); | ||
return fixed; | ||
default: | ||
break; | ||
} | ||
} | ||
}, | ||
}); | ||
} catch (err) { | ||
console.log("Found error in: ", err, context.getFilename()); | ||
} | ||
} | ||
|
||
const { deprecations, source } = context.options[0]; | ||
const relevantDeprecationsData = []; | ||
let everythingIsImported = false; | ||
|
||
function getDeprecation(value) { | ||
if (value && value.name) { | ||
const name = value.name; | ||
return _.find( | ||
deprecations, | ||
(deprecation) => deprecation.function === name | ||
); | ||
} | ||
} | ||
|
||
function searchForPossibleDeprecation(node) { | ||
const importSource = node.source.value; | ||
if (source === importSource) { | ||
const specifiers = node.specifiers; | ||
if (specifiers) { | ||
_.map(specifiers, (specifier) => { | ||
const deprecation = getDeprecation(specifier.imported); | ||
if (deprecation) { | ||
let type = FIX_TYPES.PROP_NAME; | ||
if (!deprecation.arguments) { | ||
type = FIX_TYPES.FUNCTION_NAME; | ||
reportDeprecatedFunction(node, { | ||
name: deprecation.function, | ||
message: deprecation.message, | ||
fix: deprecation.fix, | ||
}); | ||
} | ||
|
||
relevantDeprecationsData.push({ | ||
localFunctionName: specifier.local.name, | ||
type, | ||
deprecation, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
if (relevantDeprecationsData.length === 0) { // someone is importing everything (*) | ||
everythingIsImported = true; | ||
_.map(deprecations, deprecation => { | ||
relevantDeprecationsData.push({ | ||
localFunctionName: deprecation.function, | ||
type: deprecation.arguments ? FIX_TYPES.PROP_NAME : FIX_TYPES.FUNCTION_NAME, | ||
deprecation, | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
function findRelevantDeprecation(functionName) { | ||
return _.find( | ||
relevantDeprecationsData, | ||
(relevantDeprecationData) => | ||
relevantDeprecationData.localFunctionName === functionName | ||
); | ||
} | ||
|
||
function getArgumentsSent(node) { | ||
const argumentsSent = []; | ||
_.map(node.arguments, argument => { | ||
const propsSentToArgument = []; | ||
if (argument.properties) { | ||
_.map(argument.properties, prop => { | ||
if (prop.key && prop.key.name) { | ||
propsSentToArgument.push(prop.key.name); | ||
} | ||
}); | ||
} | ||
|
||
argumentsSent.push(propsSentToArgument); | ||
}); | ||
|
||
return argumentsSent; | ||
} | ||
|
||
function getFunctionName(node) { | ||
const propName = everythingIsImported ? 'callee.property.name' : 'callee.name'; | ||
return _.get(node, propName); | ||
} | ||
|
||
function testCallExpression(node) { | ||
const functionName = getFunctionName(node); | ||
if (functionName) { | ||
const relevantDeprecation = findRelevantDeprecation(functionName); | ||
if (relevantDeprecation) { | ||
if (relevantDeprecation.type === FIX_TYPES.PROP_NAME) { | ||
const argumentsSent = getArgumentsSent(node); | ||
_.map(relevantDeprecation.deprecation.arguments, (argument, index) => { | ||
if (argument.props && argument.props.length > 0 && argumentsSent.length >= index) { | ||
const deprecationProps = argument.props; | ||
const sentProps = argumentsSent[index]; | ||
_.map(sentProps, sentProp => { | ||
const deprecationProp = _.find(deprecationProps, deprecationProp => deprecationProp.prop === sentProp); | ||
if (deprecationProp) { | ||
reportDeprecatedFunction(node, { | ||
name: functionName, | ||
message: deprecationProp.message, | ||
argumentIndex: index, | ||
prop: deprecationProp.prop, | ||
fix: deprecationProp.fix, | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} else { | ||
reportDeprecatedFunction(node, { | ||
name: relevantDeprecation.deprecation.function, | ||
message: relevantDeprecation.deprecation.message, | ||
fix: relevantDeprecation.deprecation.fix, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { | ||
ImportDeclaration: (node) => searchForPossibleDeprecation(node), | ||
CallExpression: (node) => relevantDeprecationsData.length > 0 && testCallExpression(node), | ||
|
||
// MemberExpression: node => localImportSpecifier && testMemberDeprecation(node), | ||
// JSXAttribute: node => testJSXAttribute(node), | ||
// JSXOpeningElement: node => testJSXOpeningElement(node), | ||
// ObjectExpression: node => testObjectExpression(node), | ||
// VariableDeclarator: node => testVariableDeclarator(node), | ||
// Property: node => testProperty(node), | ||
// JSXSpreadAttribute: node => testJSXSpreadAttribute(node) | ||
}; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[ | ||
{ | ||
"function": "deprecatedFunction", | ||
"source": "our-source", | ||
"message": "Please use the 'validFunction' function instead (fix is available).", | ||
"fix": {"functionName": "validFunction"} | ||
}, | ||
{ | ||
"function": "validFunction", | ||
"source": "our-source", | ||
"message": "", | ||
"arguments": [ | ||
{}, | ||
{ | ||
"props": [ | ||
{ | ||
"prop": "deprecatedProp", | ||
"message": "Please use the 'validProp' prop instead (fix is available).", | ||
"fix": {"propName": "validProp"} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not adding a warning phase also? or we can just use the regular
function-deprecation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed