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

Should delete a?.b be allowed or forbidden? #14

Closed
claudepache opened this issue Jul 19, 2017 · 8 comments
Closed

Should delete a?.b be allowed or forbidden? #14

claudepache opened this issue Jul 19, 2017 · 8 comments

Comments

@claudepache
Copy link
Collaborator

The right semantics of delete a?.b is evident and requires practically zero effort to spec. Should it be allowed or not?

@ljharb
Copy link
Member

ljharb commented Jul 19, 2017

Deletion is strange enough; I'm not sure why we would want conditional deletion. We shouldn't be striving to add optional chaining everywhere it's possible imo; only where it's useful.

@rattrayalex
Copy link

Agreed with @ljharb .

delete a?.b seems like the kind of thing I would immediately write a linter rule to prevent if I ever saw it in the wild; it sounds like sloppy code for a case that shouldn't be encouraged. Furthermore, if I ever had an inclination to write it, I don't think I would expect it to work (at least, I would check "the docs", but I wouldn't assume either way).

@jridgewell
Copy link
Member

Babel itself could benefit from optional deletion: https://github.com/babel/babel/blob/28ae47a174f67a8ae6f4527e0a66e88896814170/packages/babel-helper-builder-react-jsx/src/index.js#L66-L69

Not every parser adds the extra object to Node, and babylon doesn't necessarily need to do it either.

@ljharb
Copy link
Member

ljharb commented Jul 19, 2017

That there exists a use case isn't necessarily a reason to add the feature :-)

@claudepache
Copy link
Collaborator Author

Thanks for the use case.

I expect to provisionally keep the “optional delete” feature when resolving #13 (but this Issue will remain open until a final decision is made).

@claudepache
Copy link
Collaborator Author

claudepache commented Jul 21, 2017

After careful consideration, I think that delete a?.b must be allowed, because:

  • it is consistent with delete a.b not throwing when a evaluates to, say, a String;
  • it has a reasonable use case;
  • its semantics is not confusing.

Or said the other way, forbidding that form, will introduce an inconsistency in the semantics of delete (which is very permissive, even in strict mode), will prevent a reasonable use case, and won’t resolve any real issue.

@claudepache
Copy link
Collaborator Author

The ongoing refactoring of Issue #20 will imply that optional delete will be implemented even without changing a jot in the current specification of the delete operator. Disallowing it would be artificial. So, in absence of strong reason for forbidding, I am closing this issue.

@caub
Copy link

caub commented Aug 27, 2019

I don't think delete should be supported if assignment is not (either mutations are supported or not, for consistency)

const o={foo: 1}: o?.foo=undefined; o

is not supported as I see

But I'd rather not support both of them (#18)

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

No branches or pull requests

5 participants