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

Support for nullish coalescing operator (??) #559

Closed
jming422 opened this issue Jun 19, 2020 · 4 comments
Closed

Support for nullish coalescing operator (??) #559

jming422 opened this issue Jun 19, 2020 · 4 comments

Comments

@jming422
Copy link
Contributor

This is related to, but not the same as, the optional chaining feature implemented by #533/#506. See:

As of now, an expression like x ?? "default" appears as a linter error from the second ? onward - it'd be great to have linter support for this new language feature.

I wanted to try and open a PR for this feature myself, but after reading the code, I felt pretty unqualified in the elisp and syntax-parsing knowledge department. I'm still going to try to hack at it this weekend, but I may not be able to produce anything that works by myself 😬

@UwUnyaa
Copy link

UwUnyaa commented Jun 19, 2020

It's worth noting that this proposal is at stage 4, so it's stable and will end up in the language, possibly in the next spec.

@ArneBab
Copy link
Contributor

ArneBab commented Jul 9, 2020

I patched in "this is not an error" like this:

;; (??
;;  (if (js2-match-char ?.)
;;      (throw 'return js2-OPTIONAL-CHAINING)
;;    (if (js2-match-char ??)
;;        (throw 'return js2-OR) ;; bab: hack to not throw errors on ??
;;      (throw 'return js2-HOOK))))

Is there a similarly easy advice to add support for the ?? as for private fields (#537)?

https://github.com/tc39/proposal-nullish-coalescing

Short form:

const response = {
  settings: {
    nullValue: null,
    height: 400,
    animationDuration: 0,
    headerText: '',
    showSplashScreen: false
  }
};

const undefinedValue = response.settings.undefinedValue ?? 'some other default'; // result: 'some other default'
const nullValue = response.settings.nullValue ?? 'some other default'; // result: 'some other default'
const headerText = response.settings.headerText ?? 'Hello, world!'; // result: ''
const animationDuration = response.settings.animationDuration ?? 300; // result: 0
const showSplashScreen = response.settings.showSplashScreen ?? true; // result: false```

@dgutov
Copy link
Collaborator

dgutov commented Jul 9, 2020

Would somebody like to try implementing the proper support for it?

@dgutov
Copy link
Collaborator

dgutov commented Jul 25, 2020

Merged!

@dgutov dgutov closed this as completed Jul 25, 2020
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

No branches or pull requests

4 participants