Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

member-ordering's variables-before-functions treat function properties as methods #4156

Closed
mgol opened this issue Sep 6, 2018 · 3 comments
Closed

Comments

@mgol
Copy link

mgol commented Sep 6, 2018

Bug Report

  • TSLint version: 5.7.0, whatever the playground uses
  • TypeScript version: 2.8.3, whatever the playground uses
  • Running TSLint via: (pick one) CLI / Playground

TypeScript code being linted

// A fake debounce implementation.
const debounce = (fn, delay) => setTimeout(fn, delay);

class C {
    value = 42;
    returnValue = () => this.value;
    debouncedReturnValue = debounce(this.returnValue, 1000);
}

with tslint.json configuration:

{
    "rules": {
        "member-ordering": [true,
            "variables-before-functions"
        ]
    }
}

See it in the playground: https://palantir.github.io/tslint-playground/?saved=N4Igxg9gJgpiBcID0SAEBBVAzAhgaxlVgCMIBXAOzEIEsBbABwBsY6YKAXHDmiCgOgA6VPgGcORGKUrVUAXlQAKLBQA0kpjgCeASnkA+VKJgcAKvRjkOytRu06A3MOFhNo0agDCqYMNT-UADccJjJCBQAWACYnCgDUACcTMgSKADUQsPklPTlDDgALGlF+YNCYWPiScioYKAAlZNSM8uzqmRhFQuL+JI4U9MyYdQBGAAYJx2EAX2EQVXA+LBoAcwQQXziAwRAEshZRHfgfP3jtkDY6YhgEgFoIBNgEmgoVo9QAbQ494dOzs52wWeOGIB1u1ywDxgtywMh4Yh2fzOAF0kbMKOiQNMgA

Actual behavior

It errors with:

Declaration of field not allowed after declaration of method. Instead, this should come at the beginning of the class/interface.

Expected behavior

There should be no error.

The problem is TSLint treats class fields defined as functions as methods when they are not methods. This creates problems - for methods, their order doesn't matter. For fields it sometimes does. In my example I have two fields with function values, the second one of them created not directly but by another function. I cannot change the order of those fields as the debounced returnValue function must already be defined when debouncedReturnValue is being defined.

@pcroc
Copy link

pcroc commented Sep 7, 2018

We have the same issue, where an arrow function assigned to a variable is treated as a method.

This rule should provide the option of whether to treat arrow functions as methods or not, and/or a way to disable this rule for a particular line:

// (This doesn't work)

// tslint:disable-next-line:member-order
public filter = (text: string) => this.defaultFilter(text); 

@mgol
Copy link
Author

mgol commented Sep 7, 2018

@pcroc You made a typo in the rule name. Disabling it just for the next line works fine.

@JoshuaKGoldberg
Copy link
Contributor

Duplicate of #3589.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants