8000 Fix: Treat function-type fields as fields, not methods (#116) · armanio123/typescript-eslint@9bc5a22 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bc5a22

Browse files
macklinuJamesHenry
authored andcommitted
Fix: Treat function-type fields as fields, not methods (typescript-eslint#116)
1 parent 470c264 commit 9bc5a22

File tree

2 files changed

+26
-49
lines changed

2 files changed

+26
-49
lines changed

packages/eslint-plugin-typescript/lib/rules/member-ordering.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -172,23 +172,9 @@ module.exports = {
172172
*/
173173
function shouldBeProcessedAsMethod(node) {
174174
// check for bound methods in ClassProperty nodes.
175-
if (
176-
node.value &&
177-
functionExpressions.indexOf(node.value.type) > -1
178-
) {
179-
return true;
180-
}
181-
182-
// check for bound methods in TSPropertySignature nodes.
183-
if (
184-
node.typeAnnotation &&
185-
node.typeAnnotation.typeAnnotation &&
186-
node.typeAnnotation.typeAnnotation.type === "TSFunctionType"
187-
) {
188-
return true;
189-
}
190-
191-
return false;
175+
return (
176+
node.value && functionExpressions.indexOf(node.value.type) > -1
177+
);
192178
}
193179

194180
/**

packages/eslint-plugin-typescript/tests/lib/rules/member-ordering.js

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,21 @@ class Foo {
540540
},
541541
{
542542
code: `
543+
class Foo {
544+
private required: boolean;
545+
private typeChecker: (data: any) => boolean;
546+
constructor(validator: (data: any) => boolean) {
547+
this.typeChecker = validator;
548+
}
549+
check(data: any): boolean {
550+
return this.typeChecker(data);
551+
}
552+
}
553+
`,
554+
options: [{ classes: ["field", "constructor", "method"] }]
555+
},
556+
{
557+
code: `
543558
class Foo {
544559
public static G() {}
545560
protected static H() {}
@@ -1184,18 +1199,18 @@ class Foo {
11841199
class Foo {
11851200
J() {}
11861201
K = () => {}
1187-
L: () => {}
11881202
constructor () {}
11891203
A: string;
1204+
L: () => {}
11901205
}
11911206
`,
11921207
options: [{ default: ["method", "constructor", "field"] }]
11931208
},
11941209
`
11951210
interface Foo {
11961211
A: string;
1212+
K: () => {};
11971213
J();
1198-
K: () => {}
11991214
}
12001215
`,
12011216
{
@@ -1211,8 +1226,8 @@ interface Foo {
12111226
`
12121227
type Foo = {
12131228
A: string;
1214-
J();
12151229
K: () => {}
1230+
J();
12161231
}
12171232
`,
12181233
{
@@ -2903,45 +2918,21 @@ class Foo {
29032918
"Member K should be declared before all constructor definitions.",
29042919
line: 5,
29052920
column: 5
2906-
},
2907-
{
2908-
message:
2909-
"Member L should be declared before all constructor definitions.",
2910-
line: 6,
2911-
column: 5
29122921
}
29132922
]
29142923
},
29152924
{
29162925
code: `
29172926
interface Foo {
29182927
K: () => {}
2919-
A: string;
2920-
J();
2921-
}
2922-
`,
2923-
errors: [
2924-
{
2925-
message:
2926-
"Member A should be declared before all method definitions.",
2927-
line: 4,
2928-
column: 5
2929-
}
2930-
]
2931-
},
2932-
{
2933-
code: `
2934-
interface Foo {
29352928
J();
29362929
A: string;
2937-
K: () => {}
29382930
}
29392931
`,
2940-
options: [{ default: ["method", "constructor", "field"] }],
29412932
errors: [
29422933
{
29432934
message:
2944-
"Member K should be declared before all field definitions.",
2935+
"Member A should be declared before all method definitions.",
29452936
line: 5,
29462937
column: 5
29472938
}
@@ -2951,32 +2942,32 @@ interface Foo {
29512942
code: `
29522943
type Foo = {
29532944
K: () => {}
2954-
A: string;
29552945
J();
2946+
A: string;
29562947
}
29572948
`,
29582949
errors: [
29592950
{
29602951
message:
29612952
"Member A should be declared before all method definitions.",
2962-
line: 4,
2953+
line: 5,
29632954
column: 5
29642955
}
29652956
]
29662957
},
29672958
{
29682959
code: `
29692960
type Foo = {
2970-
J();
29712961
A: string;
29722962
K: () => {}
2963+
J();
29732964
}
29742965
`,
29752966
options: [{ default: ["method", "constructor", "field"] }],
29762967
errors: [
29772968
{
29782969
message:
2979-
"Member K should be declared before all field definitions.",
2970+
"Member J should be declared before all field definitions.",
29802971
line: 5,
29812972
column: 5
29822973
}

0 commit comments

Comments
 (0)
0