Skip to content

ESLint - add support for class or inherited functions #1272

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions eslint-rules/lib/rules/function-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,31 @@ module.exports = {
}
}

// Test for forbidden inherited functions (no source)
function testClassBody(node) {
if (node && node.body) {
_.forEach(node.body, item => {
const type = _.get(item, 'type');
if (type === 'ClassProperty' || type === 'MethodDefinition') {
const deprecation = _.find(deprecations,
(deprecation) => !deprecation.source && deprecation.function === _.get(item, 'key.name')
)

if (deprecation) {
reportDeprecatedFunction(node, {
name: deprecation.function,
message: deprecation.message
});
}
}
});
}
}

return {
ImportDeclaration: (node) => searchForPossibleDeprecation(node),
CallExpression: (node) => relevantDeprecationsData.length > 0 && testCallExpression(node),
ClassBody: (node) => testClassBody(node),
};
},
};
2 changes: 1 addition & 1 deletion eslint-rules/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-uilib",
"version": "2.0.4",
"version": "2.0.5",
"description": "uilib set of eslint rules",
"keywords": [
"eslint",
Expand Down
4 changes: 4 additions & 0 deletions eslint-rules/tests/function_deprecation.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
]
}
]
},
{
"function": "deprecatedInheritedFunction",
"message": "please stop using it."
}
]
58 changes: 58 additions & 0 deletions eslint-rules/tests/lib/rules/function-deprecation.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,48 @@ class Example extends Component {
}
}`;

const fullClassDeprecated3 = `
import {Something} from '${notOurSource}';
import * as Everything from '${ourSource}';
import {SomethingElse} from '${notOurSource2}';
class Example extends Component {

deprecatedInheritedFunction = () => {
return true
}
}`;

const fullClassDeprecated4 = `
import {Something} from '${notOurSource}';
import * as Everything from '${ourSource}';
import {SomethingElse} from '${notOurSource2}';
class Example extends Component {

deprecatedInheritedFunction() {
return true
}
}`;

const fullClassDeprecated5 = `
import {someFunction} from '${notOurSource}';
class Example extends Component {

deprecatedFunction = () => {
someFunction(${validProps});
}

render() {
return (
<TouchableOpacity flex center>
onPress={this.deprecatedFunction}
</TouchableOpacity>
);
}
}`;

const functionError = "The 'deprecatedFunction' function is deprecated. Please use the 'validFunction' function instead (fix is available).";
const propError = "The 'validFunction' function's prop 'deprecatedProp' is deprecated. Please use the 'validProp' prop instead (fix is available).";
const classFunctionError = "The 'deprecatedInheritedFunction' function is deprecated. please stop using it.";
const errorDate = ' Please fix this issue by 2 November, Friday!';

ruleTester.run('function-deprecation', rule, {
Expand Down Expand Up @@ -165,6 +205,10 @@ ruleTester.run('function-deprecation', rule, {
options: options,
code: `${fullClassValid}`,
},
{
options: options,
code: `${fullClassDeprecated5}`,
},
],
invalid: [
{
Expand Down Expand Up @@ -269,5 +313,19 @@ deprecatedFunction(getProp())`,
{ message: functionError },
],
},
{
options: options,
code: `${fullClassDeprecated3}`,
errors: [
{ message: classFunctionError }
],
},
{
options: options,
code: `${fullClassDeprecated4}`,
errors: [
{ message: classFunctionError }
],
},
],
});