Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create eslint plugin #709

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft

Create eslint plugin #709

wants to merge 6 commits into from

Conversation

NullVoxPopuli
Copy link
Owner

@NullVoxPopuli NullVoxPopuli commented Dec 17, 2022

Goal is to help folks out with issues like what is reported here: #707

Todo: Lints for

  • warn: short circuiting logic can prevent other data from ever being consumed

    if (foo && bar) { /* ... */ }

    in this situation, if both foo and bar are tracked, and foo is always false, the resource will never add bar to it's list of reactive dependencies

  • against: setting variables on this within a resource

    • docs and explanation
    • within trackedFunction
      class Demo {
        // ❌ bad
        /* ... */ = trackedFunction(() => {
          this.prop = 1;
          /* ... */
        });
      }
    • within resource()
      class Demo {
        // ❌ bad
        /* ... */ = resource(() => {
          this.prop = 1;
          /* ... */
        });
      }
    • within argument thunk (args to a resource blueprint/factory/from)
      class Demo {
        // ❌ bad
        /* ... */ = MyResource.from(() => {
          this.prop = 1;
          return [ ... ];
        });
        // ❌ bad
        /* ... */ = RemoteData(() => {
          this.prop = 1;
          return 'https://...';
        });
      }
  • against: setting tracked state after the state was created (unless inside an awaited IIFE)

    class Demo {
      // ❌ bad
      /* ... */ = resource(() => {
        let state = new TrackedObject();
        state.data = 2
        /* ... */
      });
    
      // ✅ good
      /* ... */ = resource(() => {
        let state = new TrackedObject({ data: 2 });
          
        /* ... */
      });
    
      // ✅ good
      /* ... */ = resource(() => {
        let state = new TrackedObject();
          
        (async () => {
          state.data = 2        
        })();
        /* ... */
      });
    
      }
  • autofix: if this is accessed after an await, suggested and fix with destroyable protection (if isDestroyed || isDestroying) -- see also: Propose new rule: no-unsafe-this-access ember-cli/eslint-plugin-ember#1421

 class Demo {
   // ❌ bad
   /* ... */ = resource(() => {
     (async () => {
       await Promise.resolve();
       let foo = this.foo;
     })();
     /* ... */
   });

   // ✅ good
   /* ... */ = resource(() => {
     (async () => {
       await Promise.resolve();
       if (isDestroying(this) || isDestroyed(this)) return;

       let foo = this.foo;
     })();        
     /* ... */
   });

Stretch goal lints

  • against: if a function call within a resource is within the same file, and if it sets properties on this

@changeset-bot
Copy link

changeset-bot bot commented Dec 17, 2022

🦋 Changeset detected

Latest commit: 566d0e2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-ember-resources Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Dec 17, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 427c4a1
Status: ✅  Deploy successful!
Preview URL: https://8ebed39f.ember-resources.pages.dev
Branch Preview URL: https://create-eslint-plugin.ember-resources.pages.dev

View logs

@github-actions
Copy link
Contributor

github-actions bot commented Dec 17, 2022

Estimated impact to a consuming app, depending on which bundle is imported

js min min + gzip min + brotli
/index.js 13.97 kB 3.33 kB 1.36 kB 1.2 kB
├── core/class-based/index.js 4.43 kB 1.88 kB 929 B 798 B
├── core/function-based/index.js 6.03 kB 552 B 269 B 213 B
└── core/use.js 2.91 kB 415 B 256 B 203 B
/util/cell.js 2.28 kB 917 B 434 B 366 B
/util/debounce.js 2.64 kB 771 B 409 B 340 B
/util/ember-concurrency.js 4.33 kB 1.53 kB 733 B 626 B
/util/function-resource.js 216 B 154 B 123 B 87 B
/util/function.js 4.82 kB 2.99 kB 1.05 kB 905 B
/util/helper.js 1.79 kB 303 B 218 B 177 B
/util/keep-latest.js 1.75 kB 512 B 296 B 235 B
/util/map.js 5.19 kB 2.13 kB 918 B 794 B
/util/remote-data.js 4.86 kB 1.75 kB 675 B 596 B

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

Successfully merging this pull request may close these issues.

1 participant