mprobst created this revision. mprobst added a reviewer: djasper. mprobst added a subscriber: cfe-commits. Herald added a subscriber: klimek.
Previously, automatic semicolon insertion would add an unwrapped line when a template string contained a line break. var x = `foo${ bar}`; Would be formatted with `bar...` on a separate line and no indent. https://reviews.llvm.org/D25675 Files: lib/Format/UnwrappedLineParser.cpp unittests/Format/FormatTestJS.cpp Index: unittests/Format/FormatTestJS.cpp =================================================================== --- unittests/Format/FormatTestJS.cpp +++ unittests/Format/FormatTestJS.cpp @@ -1276,6 +1276,12 @@ verifyFormat("var x = ` \\${foo}`;\n"); } +TEST_F(FormatTestJS, TemplateStringASI) { + verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" + " world\n" + "}`;"); +} + TEST_F(FormatTestJS, NestedTemplateStrings) { verifyFormat( "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;"); Index: lib/Format/UnwrappedLineParser.cpp =================================================================== --- lib/Format/UnwrappedLineParser.cpp +++ lib/Format/UnwrappedLineParser.cpp @@ -727,6 +727,8 @@ return; bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); + bool PreviousStartsTemplateExpr = + Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) { // If the token before the previous one is an '@', the previous token is an // annotation and can precede another identifier/value. @@ -737,9 +739,12 @@ if (Next->is(tok::exclaim) && PreviousMustBeValue) addUnwrappedLine(); bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); - if (NextMustBeValue && (PreviousMustBeValue || - Previous->isOneOf(tok::r_square, tok::r_paren, - tok::plusplus, tok::minusminus))) + bool NextEndsTemplateExpr = + Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); + if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && + (PreviousMustBeValue || + Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, + tok::minusminus))) addUnwrappedLine(); if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next)) addUnwrappedLine();
Index: unittests/Format/FormatTestJS.cpp =================================================================== --- unittests/Format/FormatTestJS.cpp +++ unittests/Format/FormatTestJS.cpp @@ -1276,6 +1276,12 @@ verifyFormat("var x = ` \\${foo}`;\n"); } +TEST_F(FormatTestJS, TemplateStringASI) { + verifyFormat("var x = `hello${world}`;", "var x = `hello${\n" + " world\n" + "}`;"); +} + TEST_F(FormatTestJS, NestedTemplateStrings) { verifyFormat( "var x = `<ul>${xs.map(x => `<li>${x}</li>`).join('\\n')}</ul>`;"); Index: lib/Format/UnwrappedLineParser.cpp =================================================================== --- lib/Format/UnwrappedLineParser.cpp +++ lib/Format/UnwrappedLineParser.cpp @@ -727,6 +727,8 @@ return; bool PreviousMustBeValue = mustBeJSIdentOrValue(Keywords, Previous); + bool PreviousStartsTemplateExpr = + Previous->is(TT_TemplateString) && Previous->TokenText.endswith("${"); if (PreviousMustBeValue && Line && Line->Tokens.size() > 1) { // If the token before the previous one is an '@', the previous token is an // annotation and can precede another identifier/value. @@ -737,9 +739,12 @@ if (Next->is(tok::exclaim) && PreviousMustBeValue) addUnwrappedLine(); bool NextMustBeValue = mustBeJSIdentOrValue(Keywords, Next); - if (NextMustBeValue && (PreviousMustBeValue || - Previous->isOneOf(tok::r_square, tok::r_paren, - tok::plusplus, tok::minusminus))) + bool NextEndsTemplateExpr = + Next->is(TT_TemplateString) && Next->TokenText.startswith("}"); + if (NextMustBeValue && !NextEndsTemplateExpr && !PreviousStartsTemplateExpr && + (PreviousMustBeValue || + Previous->isOneOf(tok::r_square, tok::r_paren, tok::plusplus, + tok::minusminus))) addUnwrappedLine(); if (PreviousMustBeValue && isJSDeclOrStmt(Keywords, Next)) addUnwrappedLine();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits