TEST_F(FormatTestJS, Closures) { verifyFormat("doFoo(function() { return 1; });"); verifyFormat("var func = function() { return 1; };"); verifyFormat("return {\n" " body: {\n" " setAttribute: function(key, val) { this[key] = val; },\n" " getAttribute: function(key) { return this[key]; },\n" " style: {direction: ''}\n" " }\n" "};"); EXPECT_EQ("abc = xyz ? function() { return 1; } : function() { return -1; };", format("abc=xyz?function(){return 1;}:function(){return -1;};")); verifyFormat("var closure = goog.bind(\n" " function() { // comment\n" " foo();\n" " bar();\n" " },\n" " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" " arg3IsReallyLongAndNeeedsLineBreaks);"); verifyFormat("var closure = goog.bind(function() { // comment\n" " foo();\n" " bar();\n" "}, this);"); verifyFormat("var x = {a: function() { return 1; }};", getGoogleJSStyleWithColumns(38)); verifyFormat("var x = {\n" " a: function() { return 1; }\n" "};", getGoogleJSStyleWithColumns(37)); }
TEST_F(FormatTestJS, RegexLiteralLength) { verifyFormat("var regex = /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", getGoogleJSStyleWithColumns(60)); verifyFormat("var regex =\n" " /aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/;", getGoogleJSStyleWithColumns(60)); }
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, WrapAfterParen) { verifyFormat("xxxxxxxxxxx(\n" " aaa, aaa);", getGoogleJSStyleWithColumns(20)); verifyFormat("xxxxxxxxxxx(\n" " aaa, aaa, aaa,\n" " aaa, aaa, aaa);", getGoogleJSStyleWithColumns(20)); verifyFormat("xxxxxxxxxxx(\n" " aaaaaaaaaaaaaaaaaaaaaaaa,\n" " function(x) {\n" " y(); //\n" " });", getGoogleJSStyleWithColumns(40)); verifyFormat("while (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}"); }
TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { verifyFormat("a == = b;"); verifyFormat("a != = b;"); verifyFormat("a === b;"); verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a !== b;"); verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("if (a + b + c +\n" " d !==\n" " e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); verifyFormat("a >> >= b;"); verifyFormat("a >>> b;"); verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a >>>= b;"); verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("if (a + b + c +\n" " d >>>\n" " e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); verifyFormat("var x = aaaaaaaaaa ?\n" " bbbbbb :\n" " ccc;", getGoogleJSStyleWithColumns(20)); }
TEST_F(FormatTestJS, JSDocAnnotations) { EXPECT_EQ("/**\n" " * @export {this.is.a.long.path.to.a.Type}\n" " */", format("/**\n" " * @export {this.is.a.long.path.to.a.Type}\n" " */", getGoogleJSStyleWithColumns(20))); }
TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { verifyFormat("a == = b;"); verifyFormat("a != = b;"); verifyFormat("a === b;"); verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a !== b;"); verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("if (a + b + c +\n" " d !==\n" " e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); verifyFormat("a >> >= b;"); verifyFormat("a >>> b;"); verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a >>>= b;"); verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("if (a + b + c +\n" " d >>>\n" " e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); verifyFormat("var x = aaaaaaaaaa ?\n" " bbbbbb :\n" " ccc;", getGoogleJSStyleWithColumns(20)); verifyFormat("var b = a.map((x) => x + 1);"); verifyFormat("return ('aaa') in bbbb;"); verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); FormatStyle Style = getGoogleJSStyleWithColumns(80); Style.AlignOperands = true; verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa() in\n" " aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; verifyFormat("var x = aaaaaaaaaaaaaaaaaaaaaaaaa()\n" " in aaaa.aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;", Style); // ES6 spread operator. verifyFormat("someFunction(...a);"); verifyFormat("var x = [1, ...a, 2];"); }
TEST_F(FormatTestJS, AutomaticSemicolonInsertion) { // The following statements must not wrap, as otherwise the program meaning // would change due to automatic semicolon insertion. // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1. verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10)); verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10)); verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10)); verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10)); verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10)); verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10)); }
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, TypeAnnotations) { verifyFormat("var x: string;"); verifyFormat("var x: {a: string; b: number;} = {};"); verifyFormat("function x(): string {\n return 'x';\n}"); verifyFormat("function x(): {x: string} {\n return {x: 'x'};\n}"); verifyFormat("function x(y: string): string {\n return 'x';\n}"); verifyFormat("for (var y: string in x) {\n x();\n}"); verifyFormat("function x(y: {a?: number;} = {}): number {\n" " return 12;\n" "}"); verifyFormat("((a: string, b: number): string => a + b);"); verifyFormat("var x: (y: number) => string;"); verifyFormat("var x: P<string, (a: number) => string>;"); verifyFormat("var x = {y: function(): z { return 1; }};"); verifyFormat("var x = {y: function(): {a: number} { return 1; }};"); verifyFormat("function someFunc(args: string[]):\n" " {longReturnValue: string[]} {}", getGoogleJSStyleWithColumns(60)); }
TEST_F(FormatTestJS, ArrayLiterals) { verifyFormat("var aaaaa: List<SomeThing> =\n" " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];"); verifyFormat("return [\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" " ccccccccccccccccccccccccccc\n" "];"); verifyFormat("return [\n" " aaaa().bbbbbbbb('A'),\n" " aaaa().bbbbbbbb('B'),\n" " aaaa().bbbbbbbb('C'),\n" "];"); verifyFormat("var someVariable = SomeFunction([\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" " ccccccccccccccccccccccccccc\n" "]);"); verifyFormat("var someVariable = SomeFunction([\n" " [aaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbb],\n" "]);", getGoogleJSStyleWithColumns(51)); verifyFormat("var someVariable = SomeFunction(aaaa, [\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" " ccccccccccccccccccccccccccc\n" "]);"); verifyFormat("var someVariable = SomeFunction(\n" " aaaa,\n" " [\n" " aaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbbbbbbbbbbbb,\n" " cccccccccccccccccccccccccc\n" " ],\n" " aaaa);"); verifyFormat("var aaaa = aaaaa || // wrap\n" " [];"); verifyFormat("someFunction([], {a: a});"); }
TEST_F(FormatTestJS, UnderstandsJavaScriptOperators) { verifyFormat("a == = b;"); verifyFormat("a != = b;"); verifyFormat("a === b;"); verifyFormat("aaaaaaa ===\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a !== b;"); verifyFormat("aaaaaaa !==\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("if (a + b + c +\n" " d !==\n" " e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); verifyFormat("a >> >= b;"); verifyFormat("a >>> b;"); verifyFormat("aaaaaaa >>>\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("a >>>= b;"); verifyFormat("aaaaaaa >>>=\n b;", getGoogleJSStyleWithColumns(10)); verifyFormat("if (a + b + c +\n" " d >>>\n" " e + f + g)\n" " q();", getGoogleJSStyleWithColumns(20)); verifyFormat("var x = aaaaaaaaaa ?\n" " bbbbbb :\n" " ccc;", getGoogleJSStyleWithColumns(20)); verifyFormat("var b = a.map((x) => x + 1);"); verifyFormat("return ('aaa') in bbbb;"); // ES6 spread operator. verifyFormat("someFunction(...a);"); verifyFormat("var x = [1, ...a, 2];"); }
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`;"); }
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" " !`;")); verifyFormat("var x =\n" " `hello ${world}` >= some();", getGoogleJSStyleWithColumns(34)); // Barely doesn't fit. verifyFormat("var x = `hello ${world}` >= some();", getGoogleJSStyleWithColumns(35)); // 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(11)); EXPECT_EQ( "var x =\n `multi\n line`;", format("var x = `multi\n line`;", getGoogleJSStyleWithColumns(13))); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`);"); // Make sure template strings get a proper ColumnWidth assigned, even if they // are first token in line. verifyFormat( "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); // Two template strings. verifyFormat("var x = `hello` == `hello`;"); // Comments in template strings. EXPECT_EQ("var x = `//a`;\n" "var y;", format("var x =\n `//a`;\n" "var y ;")); EXPECT_EQ("var x = `/*a`;\n" "var y;", format("var x =\n `/*a`;\n" "var y;")); // Unterminated string literals in a template string. verifyFormat("var x = `'`; // comment with matching quote '\n" "var y;"); verifyFormat("var x = `\"`; // comment with matching quote \"\n" "var y;"); EXPECT_EQ("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa);", format("it(`'aaaaaaaaaaaaaaa `, aaaaaaaaa) ;", getGoogleJSStyleWithColumns(40))); // Backticks in a comment - not a template string. EXPECT_EQ("var x = 1 // `/*a`;\n" " ;", format("var x =\n 1 // `/*a`;\n" " ;")); EXPECT_EQ("/* ` */ var x = 1; /* ` */", format("/* ` */ var x\n= 1; /* ` */")); // Comment spans multiple template strings. EXPECT_EQ("var x = `/*a`;\n" "var y = ` */ `;", format("var x =\n `/*a`;\n" "var y =\n ` */ `;")); // Escaped backtick. EXPECT_EQ("var x = ` \\` a`;\n" "var y;", format("var x = ` \\` a`;\n" "var y;")); }
TEST_F(FormatTestJS, Modules) { verifyFormat("import SomeThing from 'some/module.js';"); verifyFormat("import {X, Y} from 'some/module.js';"); verifyFormat("import a, {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 = {import: 1};\nx.import = 2;"); verifyFormat("export function fn() {\n" " return 'fn';\n" "}"); verifyFormat("export function A() {}\n" "export default function B() {}\n" "export function C() {}"); 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 abstract 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("class C {}\n" "export function f() {}\n" "var v;"); verifyFormat("export var x: number = 12;"); verifyFormat("export const y = {\n" " a: 1,\n" " b: 2\n" "};"); verifyFormat("export enum Foo {\n" " BAR,\n" " // adsdasd\n" " BAZ\n" "}"); verifyFormat("export default [\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n" "];"); verifyFormat("export default [];"); verifyFormat("export default () => {};"); verifyFormat("export interface Foo { foo: number; }\n" "export class Bar {\n" " blah(): string { return this.blah; };\n" "}"); }
TEST_F(FormatTestJS, RegexLiteralSpecialCharacters) { verifyFormat("var regex = /=/;"); verifyFormat("var regex = /a*/;"); verifyFormat("var regex = /a+/;"); verifyFormat("var regex = /a?/;"); verifyFormat("var regex = /.a./;"); verifyFormat("var regex = /a\\*/;"); verifyFormat("var regex = /^a$/;"); verifyFormat("var regex = /\\/a/;"); verifyFormat("var regex = /(?:x)/;"); verifyFormat("var regex = /x(?=y)/;"); verifyFormat("var regex = /x(?!y)/;"); verifyFormat("var regex = /x|y/;"); verifyFormat("var regex = /a{2}/;"); verifyFormat("var regex = /a{1,3}/;"); verifyFormat("var regex = /[abc]/;"); verifyFormat("var regex = /[^abc]/;"); verifyFormat("var regex = /[\\b]/;"); verifyFormat("var regex = /[/]/;"); verifyFormat("var regex = /[\\/]/;"); verifyFormat("var regex = /\\[/;"); verifyFormat("var regex = /\\\\[/]/;"); verifyFormat("var regex = /}[\"]/;"); verifyFormat("var regex = /}[/\"]/;"); verifyFormat("var regex = /}[\"/]/;"); verifyFormat("var regex = /\\b/;"); verifyFormat("var regex = /\\B/;"); verifyFormat("var regex = /\\d/;"); verifyFormat("var regex = /\\D/;"); verifyFormat("var regex = /\\f/;"); verifyFormat("var regex = /\\n/;"); verifyFormat("var regex = /\\r/;"); verifyFormat("var regex = /\\s/;"); verifyFormat("var regex = /\\S/;"); verifyFormat("var regex = /\\t/;"); verifyFormat("var regex = /\\v/;"); verifyFormat("var regex = /\\w/;"); verifyFormat("var regex = /\\W/;"); verifyFormat("var regex = /a(a)\\1/;"); verifyFormat("var regex = /\\0/;"); verifyFormat("var regex = /\\\\/g;"); verifyFormat("var regex = /\\a\\\\/g;"); verifyFormat("var regex = /\a\\//g;"); verifyFormat("var regex = /a\\//;\n" "var x = 0;"); EXPECT_EQ("var regex = /'/g;", format("var regex = /'/g ;")); EXPECT_EQ("var regex = /'/g; //'", format("var regex = /'/g ; //'")); EXPECT_EQ("var regex = /\\/*/;\n" "var x = 0;", format("var regex = /\\/*/;\n" "var x=0;")); EXPECT_EQ("var x = /a\\//;", format("var x = /a\\// \n;")); verifyFormat("var regex = /\"/;", getGoogleJSStyleWithColumns(16)); verifyFormat("var regex =\n" " /\"/;", getGoogleJSStyleWithColumns(15)); verifyFormat("var regex = //\n" " /a/;"); verifyFormat("var regexs = [\n" " /d/, //\n" " /aa/, //\n" "];"); }
TEST_F(FormatTestJS, FunctionLiterals) { verifyFormat("doFoo(function() {});"); verifyFormat("doFoo(function() { return 1; });"); verifyFormat("var func = function() {\n" " return 1;\n" "};"); verifyFormat("var func = //\n" " function() {\n" " return 1;\n" "};"); verifyFormat("return {\n" " body: {\n" " setAttribute: function(key, val) { this[key] = val; },\n" " getAttribute: function(key) { return this[key]; },\n" " style: {direction: ''}\n" " }\n" "};"); verifyFormat("abc = xyz ? function() {\n" " return 1;\n" "} : function() {\n" " return -1;\n" "};"); verifyFormat("var closure = goog.bind(\n" " function() { // comment\n" " foo();\n" " bar();\n" " },\n" " this, arg1IsReallyLongAndNeeedsLineBreaks,\n" " arg3IsReallyLongAndNeeedsLineBreaks);"); verifyFormat("var closure = goog.bind(function() { // comment\n" " foo();\n" " bar();\n" "}, this);"); verifyFormat("return {\n" " a: 'E',\n" " b: function() {\n" " return function() {\n" " f(); //\n" " };\n" " }\n" "};"); verifyFormat("{\n" " var someVariable = function(x) {\n" " return x.zIsTooLongForOneLineWithTheDeclarationLine();\n" " };\n" "}"); verifyFormat("someLooooooooongFunction(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" " function(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" " // code\n" " });"); verifyFormat("f({a: function() { return 1; }});", getGoogleJSStyleWithColumns(33)); verifyFormat("f({\n" " a: function() { return 1; }\n" "});", getGoogleJSStyleWithColumns(32)); verifyFormat("return {\n" " a: function SomeFunction() {\n" " // ...\n" " return 1;\n" " }\n" "};"); verifyFormat("this.someObject.doSomething(aaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " .then(goog.bind(function(aaaaaaaaaaa) {\n" " someFunction();\n" " someFunction();\n" " }, this), aaaaaaaaaaaaaaaaa);"); verifyFormat("someFunction(goog.bind(function() {\n" " doSomething();\n" " doSomething();\n" "}, this), goog.bind(function() {\n" " doSomething();\n" " doSomething();\n" "}, this));"); // FIXME: This is bad, we should be wrapping before "function() {". verifyFormat("someFunction(function() {\n" " doSomething(); // break\n" "})\n" " .doSomethingElse(\n" " // break\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))); // Make sure template strings get a proper ColumnWidth assigned, even if they // are first token in line. verifyFormat( "var a = aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" " `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`;"); // Two template strings. verifyFormat("var x = `hello` == `hello`;"); // Comments in template strings. EXPECT_EQ("var x = `//a`;\n" "var y;", format("var x =\n `//a`;\n" "var y ;")); EXPECT_EQ("var x = `/*a`;\n" "var y;", format("var x =\n `/*a`;\n" "var y;")); // Backticks in a comment - not a template string. EXPECT_EQ("var x = 1 // `/*a`;\n" " ;", format("var x =\n 1 // `/*a`;\n" " ;")); EXPECT_EQ("/* ` */ var x = 1; /* ` */", format("/* ` */ var x\n= 1; /* ` */")); // Comment spans multiple template strings. EXPECT_EQ("var x = `/*a`;\n" "var y = ` */ `;", format("var x =\n `/*a`;\n" "var y =\n ` */ `;")); // Escaped backtick. EXPECT_EQ("var x = ` \\` a`;\n" "var y;", format("var x = ` \\` a`;\n" "var y;")); }