Skip to content

Commit dd5035d

Browse files
authored
ESLint - add support for class or inherited functions (#1272)
1 parent e993ab2 commit dd5035d

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

eslint-rules/lib/rules/function-deprecation.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,31 @@ module.exports = {
214214
}
215215
}
216216

217+
// Test for forbidden inherited functions (no source)
218+
function testClassBody(node) {
219+
if (node && node.body) {
220+
_.forEach(node.body, item => {
221+
const type = _.get(item, 'type');
222+
if (type === 'ClassProperty' || type === 'MethodDefinition') {
223+
const deprecation = _.find(deprecations,
224+
(deprecation) => !deprecation.source && deprecation.function === _.get(item, 'key.name')
225+
)
226+
227+
if (deprecation) {
228+
reportDeprecatedFunction(node, {
229+
name: deprecation.function,
230+
message: deprecation.message
231+
});
232+
}
233+
}
234+
});
235+
}
236+
}
237+
217238
return {
218239
ImportDeclaration: (node) => searchForPossibleDeprecation(node),
219240
CallExpression: (node) => relevantDeprecationsData.length > 0 && testCallExpression(node),
241+
ClassBody: (node) => testClassBody(node),
220242
};
221243
},
222244
};

eslint-rules/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-uilib",
3-
"version": "2.0.4",
3+
"version": "2.0.5",
44
"description": "uilib set of eslint rules",
55
"keywords": [
66
"eslint",

eslint-rules/tests/function_deprecation.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,9 @@
2121
]
2222
}
2323
]
24+
},
25+
{
26+
"function": "deprecatedInheritedFunction",
27+
"message": "please stop using it."
2428
}
2529
]

eslint-rules/tests/lib/rules/function-deprecation.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,48 @@ class Example extends Component {
9999
}
100100
}`;
101101

102+
const fullClassDeprecated3 = `
103+
import {Something} from '${notOurSource}';
104+
import * as Everything from '${ourSource}';
105+
import {SomethingElse} from '${notOurSource2}';
106+
class Example extends Component {
107+
108+
deprecatedInheritedFunction = () => {
109+
return true
110+
}
111+
}`;
112+
113+
const fullClassDeprecated4 = `
114+
import {Something} from '${notOurSource}';
115+
import * as Everything from '${ourSource}';
116+
import {SomethingElse} from '${notOurSource2}';
117+
class Example extends Component {
118+
119+
deprecatedInheritedFunction() {
120+
return true
121+
}
122+
}`;
123+
124+
const fullClassDeprecated5 = `
125+
import {someFunction} from '${notOurSource}';
126+
class Example extends Component {
127+
128+
deprecatedFunction = () => {
129+
someFunction(${validProps});
130+
}
131+
132+
render() {
133+
return (
134+
<TouchableOpacity flex center>
135+
onPress={this.deprecatedFunction}
136+
</TouchableOpacity>
137+
);
138+
}
139+
}`;
140+
102141
const functionError = "The 'deprecatedFunction' function is deprecated. Please use the 'validFunction' function instead (fix is available).";
103142
const propError = "The 'validFunction' function's prop 'deprecatedProp' is deprecated. Please use the 'validProp' prop instead (fix is available).";
143+
const classFunctionError = "The 'deprecatedInheritedFunction' function is deprecated. please stop using it.";
104144
const errorDate = ' Please fix this issue by 2 November, Friday!';
105145

106146
ruleTester.run('function-deprecation', rule, {
@@ -165,6 +205,10 @@ ruleTester.run('function-deprecation', rule, {
165205
options: options,
166206
code: `${fullClassValid}`,
167207
},
208+
{
209+
options: options,
210+
code: `${fullClassDeprecated5}`,
211+
},
168212
],
169213
invalid: [
170214
{
@@ -269,5 +313,19 @@ deprecatedFunction(getProp())`,
269313
{ message: functionError },
270314
],
271315
},
316+
{
317+
options: options,
318+
code: `${fullClassDeprecated3}`,
319+
errors: [
320+
{ message: classFunctionError }
321+
],
322+
},
323+
{
324+
options: options,
325+
code: `${fullClassDeprecated4}`,
326+
errors: [
327+
{ message: classFunctionError }
328+
],
329+
},
272330
],
273331
});

0 commit comments

Comments
 (0)