]> granicus.if.org Git - clang/commitdiff
clang-format: [JS] Support pseudo-keywords
authorDaniel Jasper <djasper@google.com>
Mon, 28 Sep 2015 14:29:45 +0000 (14:29 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 28 Sep 2015 14:29:45 +0000 (14:29 +0000)
JavaScript allows keywords to appear in IdenfierName positions, e.g.
fields, or object literal members, but not as plain identifiers.

Patch by Martin Probst. Thank you!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248714 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestJS.cpp

index 068f9727b89cd320b5b3f9e931ecfdfd772fe362..a569f6c56b0873e32d70463ad3ce94fcc9f4debd 100644 (file)
@@ -374,7 +374,7 @@ private:
           FormatToken *Previous = CurrentToken->getPreviousNonComment();
           if ((CurrentToken->is(tok::colon) ||
                Style.Language == FormatStyle::LK_Proto) &&
-              Previous->is(tok::identifier))
+              Previous->Tok.getIdentifierInfo())
             Previous->Type = TT_SelectorName;
           if (CurrentToken->is(tok::colon) ||
               Style.Language == FormatStyle::LK_JavaScript)
index d8301e69612dff7bf5f26a9cf2ff99f74780034b..08115036cdf735eac7212af4b96726bafc06e2d6 100644 (file)
@@ -844,6 +844,11 @@ void UnwrappedLineParser::parseStructuralElement() {
       if (Style.Language == FormatStyle::LK_Java && FormatTok &&
           FormatTok->is(tok::kw_class))
         nextToken();
+      if (Style.Language == FormatStyle::LK_JavaScript && FormatTok &&
+          FormatTok->Tok.getIdentifierInfo())
+        // JavaScript only has pseudo keywords, all keywords are allowed to
+        // appear in "IdentifierName" positions. See http://es5.github.io/#x7.6
+        nextToken();
       break;
     case tok::semi:
       nextToken();
index c02d51be5058a09ca25616db462170d2ed5d3df8..f09df28142942d16b522cb36d83c541fe9741966 100644 (file)
@@ -99,6 +99,19 @@ TEST_F(FormatTestJS, LiteralOperatorsCanBeKeywords) {
   verifyFormat("not.and.or.not_eq = 1;");
 }
 
+TEST_F(FormatTestJS, ReservedWords) {
+  // JavaScript reserved words (aka keywords) are only illegal when used as
+  // Identifiers, but are legal as IdentifierNames.
+  verifyFormat("x.class.struct = 1;");
+  verifyFormat("x.case = 1;");
+  verifyFormat("x.interface = 1;");
+  verifyFormat("x = {\n"
+               "  a: 12,\n"
+               "  interface: 1,\n"
+               "  switch: 1,\n"
+               "};");
+}
+
 TEST_F(FormatTestJS, ES6DestructuringAssignment) {
   verifyFormat("var [a, b, c] = [1, 2, 3];");
   verifyFormat("let [a, b, c] = [1, 2, 3];");