This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] messes up indentation when using JavaScript private fields and methods
ClosedPublic

Authored by MyDeveloperDay on Mar 13 2019, 3:51 AM.

Details

Reviewers
mprobst
Summary

Addresses PR40999 https://bugs.llvm.org/show_bug.cgi?id=40999

Private fields and methods in javasceipt would get incorrectly indented (it sees them as preprocessor directives and hence left aligns them)

In this revision "#identifier" tokens tok::hash->tok::identifier are merged into a single new token
tok::identifier with the '#' contained inside the TokenText

class Example {
  pub = 1;
#priv = 2;

  static pub2 = "foo";
  static #priv2 = "bar";

  method() { this.#priv = 5; }

  static staticMethod() {
    switch (this.#priv) {
    case '1':
#priv = 3;
      break;
    }
  }

#privateMethod() {
  this.#privateMethod(); // infinite loop
}

static #staticPrivateMethod() {}
}

After this fix the code will be correctly indented

class Example {
  pub = 1;
  #priv = 2;

  static pub2 = "foo";
  static #priv2 = "bar";

  method() { this.#priv = 5; }

  static staticMethod() {
    switch (this.#priv) {
    case '1':
      #priv = 3;
      break;
    }
  }

  #privateMethod() {
    this.#privateMethod(); // infinite loop
  }

  static #staticPrivateMethod() {}
}
NOTE: There might be some Javascript code out there which uses the C processor to preprocess .js files http://www.nongnu.org/espresso/js-cpp.html, Its not clear how this revision or even private fields and methods would interact.

Diff Detail

Event Timeline

MyDeveloperDay created this revision.Mar 13 2019, 3:51 AM

ping @mprobst,@mitchellwills noticed you just reviewed another clang-format-js commit, wondered if you were interested in reviewing this for me?, finding it hard to get any review traction.

mprobst accepted this revision.Mar 19 2019, 5:19 AM

Actually I'll fix the nits.

clang/docs/ReleaseNotes.rst
174

Honestly, I think we don't really do release notes for clang-format. Revert this, or I'll drop it when committing your change.

clang/lib/Format/FormatToken.h
66

nit: please keep alpha sorted, i.e. before Js[T]

This revision is now accepted and ready to land.Mar 19 2019, 5:19 AM
mprobst closed this revision.Mar 19 2019, 5:27 AM

Landed in r356449, thanks!