8000 feat: support TypeScript syntax in `class-methods-use-this` (#19498) · eslint/eslint@be56a68 · GitHub
[go: up one dir, main page]

Skip to content

Commit be56a68

Browse files
feat: support TypeScript syntax in class-methods-use-this (#19498)
1 parent 7a699a6 commit be56a68

File tree

2 files changed

+235
-0
lines changed

2 files changed

+235
-0
lines changed

lib/rules/class-methods-use-this.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const astUtils = require("./utils/ast-utils");
1818
/** @type {import('../shared/types').Rule} */
1919
module.exports = {
2020
meta: {
21+
dialects: ["javascript", "typescript"],
22+
language: "javascript",
2123
type: "suggestion",
2224

2325
defaultOptions: [{

tests/lib/rules/class-methods-use-this.js

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,236 @@ ruleTester.run("class-methods-use-this", rule, {
214214
}
215215
]
216216
});
217+
218+
const ruleTesterTypeScript = new RuleTester({
219+
languageOptions: {
220+
parser: require("@typescript-eslint/parser")
221+
}
222+
});
223+
224+
ruleTesterTypeScript.run("class-methods-use-this", rule, {
225+
valid: [
226+
"class A { constructor() {} }",
227+
"class A { foo() {this} }",
228+
"class A { foo() {this.bar = 'bar';} }",
229+
"class A { foo() {bar(this);} }",
230+
"class A extends B { foo() {super.foo();} }",
231+
"class A { foo() { if(true) { return this; } } }",
232+
"class A { static foo() {} }",
233+
"({ a(){} });",
234+
"class A { foo() { () => this; } }",
235+
"({ a: function () {} });",
236+
{ code: "class A { foo() {this} bar() {} }", options: [{ exceptMethods: ["bar"] }] },
237+
{ code: "class A { \"foo\"() { } }", options: [{ exceptMethods: ["foo"] }] },
238+
{ code: "class A { 42() { } }", options: [{ exceptMethods: ["42"] }] },
239+
"class A { foo = function() {this} }",
240+
"class A { foo = () => {this} }",
241+
"class A { foo = () => {super.toString} }",
242+
"class A { static foo = function() {} }",
243+
"class A { static foo = () => {} }",
244+
{ code: "class A { #bar() {} }", options: [{ exceptMethods: ["#bar"] }] },
245+
{ code: "class A { foo = function () {} }", options: [{ enforceForClassFields: false }] },
246+
{ code: "class A { foo = () => {} }", options: [{ enforceForClassFields: false }] },
247+
"class A { foo() { return class { [this.foo] = 1 }; } }",
248+
"class A { static {} }"
249+
],
250+
invalid: [
251+
{
252+
code: `
253+
class Foo {
254+
method() {}
255+
}
256+
`,
257+
errors: [
258+
{
259+
messageId: "missingThis"
260+
}
261+
]
262+
},
263+
{
264+
code: `
265+
class Foo {
266+
private method() {}
267+
}
268+
`,
269+
errors: [
270+
{
271+
messageId: "missingThis"
272+
}
273+
]
274+
},
275+
{
276+
code: `
277+
class Foo {
278+
protected method() {}
279+
}
280+
`,
281+
errors: [
282+
{
283+
messageId: "missingThis"
284+
}
285+
]
286+
},
287+
{
288+
code: `
289+
class Derived extends Base {
290+
override method() {}
291+
}
292+
`,
293+
errors: [
294+
{
295+
messageId: "missingThis"
296+
}
297+
]
298+
},
299+
{
300+
code: `
301+
class Derived extends Base {
302+
property = () => {}
303+
}
304+
`,
305+
errors: [
306+
{
307+
messageId: "missingThis"
308+
}
309+
]
310+
},
311+
{
312+
code: `
313+
class Derived extends Base {
314+
public property = () => {}
315+
}
316+
`,
317+
errors: [
318+
{
319+
messageId: "missingThis"
320+
}
321+
]
322+
},
323+
{
324+
code: `
325+
class Derived extends Base {
326+
override property = () => {}
327+
}
328+
`,
329+
errors: [
330+
{
331+
messageId: "missingThis"
332+
}
333+
]
334+
},
335+
{
336+
code: `
337+
class Foo {
338+
#method() {}
339+
}
340+
`,
341+
errors: [
342+
{
343+
messageId: "missingThis"
344+
}
345+
]
346+
},
347+
{
348+
code: `
349+
class Foo {
350+
get getter(): number {}
351+
}
352+
`,
353+
errors: [
354+
{
355+
messageId: "missingThis"
356+
}
357+
]
358+
},
359+
{
360+
code: `
361+
class Foo {
362+
private get getter(): number {}
363+
}
364+
`,
365+
errors: [
366+
{
367+
messageId: "missingThis"
368+
}
369+
]
370+
},
371+
{
372+
code: `
373+
class Foo {
374+
protected get getter(): number {}
375+
}
376+
`,
377+
errors: [
378+
{
379+
messageId: "missingThis"
380+
}
381+
]
382+
},
383+
{
384+
code: `
385+
class Foo {
386+
get #getter(): number {}
387+
}
388+
`,
389+
errors: [
390+
{
391+
messageId: "missingThis"
392+
}
393+
]
394+
},
395+
{
396+
code: `
397+
class Foo {
398+
private set setter(b: number) {}
399+
}
400+
`,
401+
errors: [
402+
{
403+
messageId: "missingThis"
404+
}
405+
]
406+
},
407+
{
408+
code: `
409+
class Foo {
410+
protected set setter(b: number) {}
411+
}
412+
`,
413+
errors: [
414+
{
415+
messageId: "missingThis"
416+
}
417+
]
418+
},
419+
{
420+
code: `
421+
class Foo {
422+
set #setter(b: number) {}
423+
}
424+
`,
425+
errors: [
426+
{
427+
messageId: "missingThis"
428+
}
429+
]
430+
},
431+
432+
{
433+
code: `
434+
function fn() {
435+
this.foo = 303;
436+
437+
class Foo {
438+
method() {}
439+
}
440+
}
441+
`,
442+
errors: [
443+
{
444+
messageId: "missingThis"
445+
}
446+
]
447+
}
448+
]
449+
});

0 commit comments

Comments
 (0)
0