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

Allow Duck-typing checks for conditional API compat logic #26

Closed
amilajack opened this issue Jan 3, 2017 · 10 comments · Fixed by #327
Closed

Allow Duck-typing checks for conditional API compat logic #26

amilajack opened this issue Jan 3, 2017 · 10 comments · Fixed by #327
Assignees

Comments

@amilajack
Copy link
Owner

amilajack commented Jan 3, 2017

UPDATE: Here's a related discussion in the eslint-typescript repo that's worth looking into

Allow checking for if lib or property is defined. Disallow calling the method

// Checking if objects are defined
if (typeof ServiceWorker === 'undefined') {} // ✅ allowed

// Checking if properties/methods are defined
if (String.prototype.includes) {} // ✅ allowed

// Checking if properties/methods are defined and
// subsequently invoking/accessing them
if (String.prototype.includes) { // ✅ allowed
  if ('some'.includes('e')) { // ✅ allowed
    // do some action
  }
}

// ✅ allowed
if ("serviceWorker" in navigator) {
  navigator.serviceWorker...
}

// ✅ allowed
if (String.prototype.includes && 'some'.includes('e')) {}
if ('foo'.includes && 'some'.includes('e')) {}

// ❌ disallowed
if ('some'.includes('e')) {}

// ❌ disallowed
if (!'some'.includes('e')) {
  'some'.includes('e')
}
@amilajack amilajack changed the title Allow Duck-typing checks for testing if supported RFC: Allow Duck-typing checks for testing if supported Jan 3, 2017
@ljharb
Copy link
Collaborator

ljharb commented Jan 3, 2017

Wouldn't ServiceWorker throw a ReferenceError when it's not defined, unless it's used with typeof?

@ljharb
Copy link
Collaborator

ljharb commented Jan 3, 2017

In other words, duck-typing should only be allowed when chained off of a definitely-present object.

@amilajack
Copy link
Owner Author

Oh yea, that's right. I've updated the rfc the initial comment to reflect this. Thanks for letting me know about this!

@deecewan
Copy link

deecewan commented Jan 4, 2017

what would happen in this case:

if (String.prototype.includes) {
  if ('some'.includes('e')) {
    // do some action
  }
}

In this case, you've validated that the function exists before using it.

@amilajack
Copy link
Owner Author

amilajack commented Jan 19, 2017

@deecewan Sorry for the late response. Somehow I missed the notification for this. That should be allowed. I just added this example to my initial comment

@amilajack amilajack changed the title RFC: Allow Duck-typing checks for testing if supported Allow Duck-typing checks for testing if supported Apr 2, 2019
@amilajack amilajack changed the title Allow Duck-typing checks for testing if supported Allow Duck-typing checks for conditional API compat logic Apr 6, 2019
@TrevorBurnham
Copy link
Contributor

@amilajack Say, we're very interested in this feature at my company. Have you made any headway? If you need help, me and some of my colleagues would be happy to roll up our sleeves!

@SalimBensiali
Copy link

Hi @amilajack, I am also interested in this feature, has any progress been made since? Would you like any contribution? Cheers

@amilajack
Copy link
Owner Author

@SalimBensiali I always love contributions. Unfortunately I don't have enough time to contribute to this project. Would really love if anyone could work on this!

@SalimBensiali
Copy link

No worries @amilajack , is there an initial branch, POC? Is this issue here the one source of truth? Happy to look at this. Cheers.

@amilajack
Copy link
Owner Author

amilajack commented Aug 18, 2019

Nope, nothing yet

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

Successfully merging a pull request may close this issue.

6 participants