mooz / js2-mode

Improved JavaScript editing mode for GNU Emacs
GNU General Public License v3.0
1.32k stars 186 forks source link

Support for nullish coalescing operator (??) #559

Closed jming422 closed 4 years ago

jming422 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 (https://github.com/mooz/js2-mode/issues/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 commented 4 years ago

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

dgutov commented 4 years ago

Merged!