Update aosp/master clang for rebase to r230699.
Change-Id: I6a546ab3d4ae37119eebb735e102cca4f80ab520
diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp
index 6ee150c..55e89da 100644
--- a/unittests/Format/FormatTestJS.cpp
+++ b/unittests/Format/FormatTestJS.cpp
@@ -94,7 +94,10 @@
TEST_F(FormatTestJS, ES6DestructuringAssignment) {
verifyFormat("var [a, b, c] = [1, 2, 3];");
- verifyFormat("var {a, b} = {a: 1, b: 2};");
+ verifyFormat("var {a, b} = {\n"
+ " a: 1,\n"
+ " b: 2\n"
+ "};");
}
TEST_F(FormatTestJS, ContainerLiterals) {
@@ -131,20 +134,25 @@
" //\n"
" a\n"
"};");
+ verifyFormat("var obj = {\n"
+ " fooooooooo: function(x) {\n"
+ " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
+ " }\n"
+ "};");
}
TEST_F(FormatTestJS, SpacesInContainerLiterals) {
verifyFormat("var arr = [1, 2, 3];");
- verifyFormat("var obj = {a: 1, b: 2, c: 3};");
+ verifyFormat("f({a: 1, b: 2, c: 3});");
verifyFormat("var object_literal_with_long_name = {\n"
" a: 'aaaaaaaaaaaaaaaaaa',\n"
" b: 'bbbbbbbbbbbbbbbbbb'\n"
"};");
- verifyFormat("var obj = {a: 1, b: 2, c: 3};",
+ verifyFormat("f({a: 1, b: 2, c: 3});",
getChromiumStyle(FormatStyle::LK_JavaScript));
- verifyFormat("someVariable = {'a': [{}]};");
+ verifyFormat("f({'a': [{}]});");
}
TEST_F(FormatTestJS, SingleQuoteStrings) {
@@ -158,6 +166,22 @@
"}); // goog.scope");
}
+TEST_F(FormatTestJS, GoogModules) {
+ verifyFormat("goog.module('this.is.really.absurdly.long');",
+ getGoogleJSStyleWithColumns(40));
+ verifyFormat("goog.require('this.is.really.absurdly.long');",
+ getGoogleJSStyleWithColumns(40));
+ verifyFormat("goog.provide('this.is.really.absurdly.long');",
+ getGoogleJSStyleWithColumns(40));
+ verifyFormat("var long = goog.require('this.is.really.absurdly.long');",
+ getGoogleJSStyleWithColumns(40));
+
+ // These should be wrapped normally.
+ verifyFormat(
+ "var MyLongClassName =\n"
+ " goog.module.get('my.long.module.name.followedBy.MyLongClassName');");
+}
+
TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
verifyFormat("function outer1(a, b) {\n"
" function inner1(a, b) { return a; }\n"
@@ -182,14 +206,13 @@
" style: {direction: ''}\n"
" }\n"
"};");
- // FIXME: The formatting here probably isn't ideal.
EXPECT_EQ("abc = xyz ?\n"
" function() {\n"
" return 1;\n"
" } :\n"
" function() {\n"
- " return -1;\n"
- "};",
+ " return -1;\n"
+ " };",
format("abc=xyz?function(){return 1;}:function(){return -1;};"));
verifyFormat("var closure = goog.bind(\n"
@@ -211,13 +234,18 @@
" };\n"
" }\n"
"};");
+ verifyFormat("{\n"
+ " var someVariable = function(x) {\n"
+ " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n"
+ " };\n"
+ "}");
- verifyFormat("var x = {a: function() { return 1; }};",
- getGoogleJSStyleWithColumns(38));
- verifyFormat("var x = {\n"
+ verifyFormat("f({a: function() { return 1; }});",
+ getGoogleJSStyleWithColumns(33));
+ verifyFormat("f({\n"
" a: function() { return 1; }\n"
- "};",
- getGoogleJSStyleWithColumns(37));
+ "});",
+ getGoogleJSStyleWithColumns(32));
verifyFormat("return {\n"
" a: function SomeFunction() {\n"
@@ -225,6 +253,23 @@
" return 1;\n"
" }\n"
"};");
+ verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " .then(goog.bind(function(aaaaaaaaaaa) {\n"
+ " someFunction();\n"
+ " someFunction();\n"
+ " }, this), aaaaaaaaaaaaaaaaa);");
+
+ // FIXME: This is not ideal yet.
+ verifyFormat("someFunction(goog.bind(\n"
+ " function() {\n"
+ " doSomething();\n"
+ " doSomething();\n"
+ " },\n"
+ " this),\n"
+ " goog.bind(function() {\n"
+ " doSomething();\n"
+ " doSomething();\n"
+ " }, this));");
}
TEST_F(FormatTestJS, InliningFunctionLiterals) {
@@ -312,7 +357,10 @@
verifyFormat("getSomeLongPromise()\n"
" .then(function(value) { body(); })\n"
- " .thenCatch(function(error) { body(); });");
+ " .thenCatch(function(error) {\n"
+ " body();\n"
+ " body();\n"
+ " });");
verifyFormat("getSomeLongPromise()\n"
" .then(function(value) {\n"
" body();\n"
@@ -322,6 +370,11 @@
" body();\n"
" body();\n"
" });");
+
+ // FIXME: This is bad, but it used to be formatted correctly by accident.
+ verifyFormat("getSomeLongPromise().then(function(value) {\n"
+ " body();\n"
+ "}).thenCatch(function(error) { body(); });");
}
TEST_F(FormatTestJS, ReturnStatements) {
@@ -345,6 +398,8 @@
// But, of course, "catch" is a perfectly fine function name in JavaScript.
verifyFormat("someObject.catch();");
+ verifyFormat("someObject.new();");
+ verifyFormat("someObject.delete();");
}
TEST_F(FormatTestJS, StringLiteralConcatenation) {
@@ -427,11 +482,136 @@
verifyFormat("var regex =\n"
" /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
getGoogleJSStyleWithColumns(60));
+ verifyFormat("var regex = /\\xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;",
+ getGoogleJSStyleWithColumns(50));
}
TEST_F(FormatTestJS, RegexLiteralExamples) {
verifyFormat("var regex = search.match(/(?:\?|&)times=([^?&]+)/i);");
}
+TEST_F(FormatTestJS, TypeAnnotations) {
+ verifyFormat("var x: string;");
+ verifyFormat("function x(): string {\n return 'x';\n}");
+ verifyFormat("function x(y: string): string {\n return 'x';\n}");
+ verifyFormat("for (var y: string in x) {\n x();\n}");
+ verifyFormat("((a: string, b: number): string => a + b);");
+ verifyFormat("var x: (y: number) => string;");
+ verifyFormat("var x: P<string, (a: number) => string>;");
+}
+
+TEST_F(FormatTestJS, ClassDeclarations) {
+ verifyFormat("class C {\n x: string = 12;\n}");
+ verifyFormat("class C {\n x(): string => 12;\n}");
+ verifyFormat("class C {\n ['x' + 2]: string = 12;\n}");
+ verifyFormat("class C {\n private x: string = 12;\n}");
+ verifyFormat("class C {\n private static x: string = 12;\n}");
+ verifyFormat("class C {\n static x(): string { return 'asd'; }\n}");
+ verifyFormat("class C extends P implements I {}");
+}
+
+TEST_F(FormatTestJS, MetadataAnnotations) {
+ verifyFormat("@A\nclass C {\n}");
+ verifyFormat("@A({arg: 'value'})\nclass C {\n}");
+ verifyFormat("@A\n@B\nclass C {\n}");
+ verifyFormat("class C {\n @A x: string;\n}");
+ verifyFormat("class C {\n"
+ " @A\n"
+ " private x(): string {\n"
+ " return 'y';\n"
+ " }\n"
+ "}");
+ verifyFormat("class X {}\n"
+ "class Y {}");
+}
+
+TEST_F(FormatTestJS, Modules) {
+ verifyFormat("import SomeThing from 'some/module.js';");
+ verifyFormat("import {X, Y} from 'some/module.js';");
+ verifyFormat("import {\n"
+ " VeryLongImportsAreAnnoying,\n"
+ " VeryLongImportsAreAnnoying,\n"
+ " VeryLongImportsAreAnnoying,\n"
+ " VeryLongImportsAreAnnoying\n"
+ "} from 'some/module.js';");
+ verifyFormat("import {\n"
+ " X,\n"
+ " Y,\n"
+ "} from 'some/module.js';");
+ verifyFormat("import {\n"
+ " X,\n"
+ " Y,\n"
+ "} from 'some/long/module.js';",
+ getGoogleJSStyleWithColumns(20));
+ verifyFormat("import {X as myLocalX, Y as myLocalY} from 'some/module.js';");
+ verifyFormat("import * as lib from 'some/module.js';");
+ verifyFormat("var x = {\n import: 1\n};\nx.import = 2;");
+
+ verifyFormat("export function fn() {\n"
+ " return 'fn';\n"
+ "}");
+ verifyFormat("export const x = 12;");
+ verifyFormat("export default class X {}");
+ verifyFormat("export {X, Y} from 'some/module.js';");
+ verifyFormat("export {\n"
+ " X,\n"
+ " Y,\n"
+ "} from 'some/module.js';");
+ verifyFormat("export class C {\n"
+ " x: number;\n"
+ " y: string;\n"
+ "}");
+ verifyFormat("export class X { y: number; }");
+ verifyFormat("export default class X { y: number }");
+ verifyFormat("export default function() {\n return 1;\n}");
+ verifyFormat("export var x = 12;");
+ verifyFormat("export var x: number = 12;");
+ verifyFormat("export const y = {\n"
+ " a: 1,\n"
+ " b: 2\n"
+ "};");
+}
+
+TEST_F(FormatTestJS, TemplateStrings) {
+ // Keeps any whitespace/indentation within the template string.
+ EXPECT_EQ("var x = `hello\n"
+ " ${ name }\n"
+ " !`;",
+ format("var x = `hello\n"
+ " ${ name }\n"
+ " !`;"));
+
+ // FIXME: +1 / -1 offsets are to work around clang-format miscalculating
+ // widths for unknown tokens that are not whitespace (e.g. '`'). Remove when
+ // the code is corrected.
+
+ verifyFormat("var x =\n"
+ " `hello ${world}` >= some();",
+ getGoogleJSStyleWithColumns(34)); // Barely doesn't fit.
+ verifyFormat("var x = `hello ${world}` >= some();",
+ getGoogleJSStyleWithColumns(35 + 1)); // Barely fits.
+ EXPECT_EQ("var x = `hello\n"
+ " ${world}` >=\n"
+ " some();",
+ format("var x =\n"
+ " `hello\n"
+ " ${world}` >= some();",
+ getGoogleJSStyleWithColumns(21))); // Barely doesn't fit.
+ EXPECT_EQ("var x = `hello\n"
+ " ${world}` >= some();",
+ format("var x =\n"
+ " `hello\n"
+ " ${world}` >= some();",
+ getGoogleJSStyleWithColumns(22))); // Barely fits.
+
+ verifyFormat("var x =\n `h`;", getGoogleJSStyleWithColumns(13 - 1));
+ EXPECT_EQ(
+ "var x =\n `multi\n line`;",
+ format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(14 - 1)));
+
+ // Two template strings.
+ verifyFormat("var x = `hello` == `hello`;");
+}
+
} // end namespace tooling
} // end namespace clang