mooz / js2-mode

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

Using '#' pound sign for 'private' properties in a .js file causes syntax issues to not be highlighted #588

Closed SamuelBanya closed 2 years ago

SamuelBanya commented 2 years ago

Hello,

I am using 'js2-mode' which is found in this block of my config:

(use-package js2-mode
  :ensure t
  :custom
  (js-indent-level 2)
  (js2-basic-offset 2)
  :init
  (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode)))

Problem:

Question:

My Affected Environment:

SamuelBanya commented 2 years ago

Should I use the local hack version provided in this comment? https://github.com/mooz/js2-mode/issues/537#issuecomment-578446942

I ask because I need to have a solid JS config for Emacs since I edit JS stuff all the time.

SamuelBanya commented 2 years ago

I tried using this as a hacky workaround but I'm still getting weird issues in LSP mode like '#radius is declared but its value is never read': https://www.draketo.de/software/emacs-javascript.html

Related config change I tried to add:

(use-package js2-mode
  :ensure t
  :custom
  (js-indent-level 2)
  (js2-basic-offset 2)
  :init
  (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  :config
  ;; Patch in basic private field support since it clearly doesn't work at all
  ;; This also has the super annoying side effect of causing tons of syntax issues
  ;; afterwards, so this HAS to be patched in order to use private methods
  ;; Used this blog post as a basis for this:
  ;; https://draketo.de/software/emacs-javascript.html
  (advice-add #'js2-identifier-part-p
              :after-until
              (lambda (c) (eq c ?#))))

Related code block that results in weirdness when even trying to use private methods as a basic example:

class Bird {
  #phrase;

  constructor(name) {
    this.name = name;
  }

  set phrase(phrase) {
    this.#phrase = phrase;
  }

  get speak() {
    return `${this.name} says ${this.#phrase || "Squawk"}`;
  }
}

class Circle {
  #radius;

  constructor(radius) {
    this.radius = radius;
  }

  get diameter(radius) {
    return this.radius * this.radius;
  }

  get circumference(radius) {
    return 2 * Math.pi * this.radius;
  }

  get area(radius) {
  }

  set diameter(radius) {
  }

  set circumference(radius) {
  }

  set area(radius) {
    return area = Math.sqrt(radius);
  }

}

const daffy = new Bird("Daffy");
console.log(`daffy.speak: ${daffy.speak}`);
daffy.phrase = "it's rabbit season!";
console.log(`daffy.speak: ${daffy.speak}`);
UwUnyaa commented 2 years ago

If you use LSP mode, is there a reason to still use JS2?

SamuelBanya commented 2 years ago

If I am understanding you correctly, I should just disable LSP and see if 'js2-mode' is causing the problem?

UwUnyaa commented 2 years ago

No, the other way around. JS2 provides error checking for js-mode, but things like syntax highlighting and indentation aren't handled by itself.

dgutov commented 2 years ago

Hi! Which version of js2-mode are you using?

This feature should work since https://github.com/mooz/js2-mode/issues/537, and indeed your example in https://github.com/mooz/js2-mode/issues/588#issuecomment-1146869291 highlights fine for me. Without extra configuration.