0

Refer the following code which I have taken from NET for auto subscribing:

export function AutoUnsub() {
  return function (constructor: any) {
    const orig = constructor.prototype.ngOnDestroy;
    constructor.prototype.ngOnDestroy = function () {
      for (const prop in this) {
        const property = this[prop];
        if (typeof property.subscribe === "function") {
          property.unsubscribe();
        }
      }
      orig.apply();
    };
  };
}

The lint issues are as follows:-

Line 2: non-arrow functions are forbidden.

Line 2: spaces before functions parens are disallowed

line 5: for (...in...) statements must be filtered with an if statement.

3rd issue, I can't put a space. The same goes away on save. Seems related to some settings in VScode.

Dharman
  • 30,962
  • 25
  • 85
  • 135
ShareYourExp
  • 11
  • 1
  • 1
  • 5
  • Have you checked rules in lint file of your project – Swarup Chavan Aug 08 '20 at 01:44
  • I am referring TS Lint documentation too. – ShareYourExp Aug 08 '20 at 19:01
  • Actually I am trying to figure out the solution. Which means what change, I need to do in the code so that code passes lint rule test. For instance error "spaces before....". I removed the space but the moment I saved the file, space was back. This might be happening due to some VS code Extension I installed. – ShareYourExp Aug 08 '20 at 19:33
  • "constructor.prototype.ngOnDestroy = function () {" changed to "constructor.prototype.ngOnDestroy = () => {" and the first issue is gone. – ShareYourExp Aug 08 '20 at 19:51
  • He can't use an arrow function, The Decorator is installing a method on the Prototype. Your linter is trying to prevent you from doing something legitimate. Change your lint rules – Aluan Haddad Aug 09 '20 at 12:30
  • To be honest I don't understand the code very much. I am not surprised by your comment because the functionality is not working. I tracked the memory using developer tools and I could see that things are not working. I am trying to find some links to know more on decorator. – ShareYourExp Aug 10 '20 at 14:33
  • If I switch back to the existing code (without making changes to the code for sorting lint issues) – ShareYourExp Aug 10 '20 at 14:52
  • I understand the functionality completely now. But still to resolve the lint issues. I am working on it. We need to change some rules for sure, looks like at the moment. – ShareYourExp Aug 11 '20 at 15:58

0 Answers0