Skip to content

Commit

Permalink
Normalize line breaks in all string literals, not just template strings.
Browse files Browse the repository at this point in the history
This allows string literal line continuations to be stripped when the continuation uses /\r\n?/ at the break (not just /\n/). Line continuations will be stripped in all language modes, including modes that support them (ES5+) since they have no semantic value.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=196883605
  • Loading branch information
nreid260 authored and blickly committed May 17, 2018
1 parent ea14962 commit b1789cc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/com/google/javascript/jscomp/parsing/IRFactory.java
Expand Up @@ -2993,10 +2993,11 @@ String normalizeRegex(LiteralToken token) {

String normalizeString(LiteralToken token, boolean templateLiteral) {
String value = token.value;
if (templateLiteral) {
// <CR><LF> and <CR> are normalized as <LF> for raw string value
value = value.replaceAll("\r\n?", "\n");
}
// <CR><LF> and <CR> are normalized as <LF>. For raw template literal string values: this is the
// spec behaviour. For regular string literals: they can only be part of a line continuation,
// which we want to scrub.
value = value.replaceAll("\r\n?", "\n");

int start = templateLiteral ? 0 : 1; // skip the leading quote
int cur = value.indexOf('\\');
if (cur == -1) {
Expand Down
44 changes: 34 additions & 10 deletions test/com/google/javascript/jscomp/parsing/ParserTest.java
Expand Up @@ -2205,26 +2205,50 @@ public void testYield3() {
parseError("function * f() { yield , yield; }");
}

public void testStringLineContinuation() {
public void testStringLineContinuationWarningsByMode() {
String unrecommendedWarning =
"String continuations are not recommended. See"
+ " https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations";

expectFeatures(Feature.STRING_CONTINUATION);
mode = LanguageMode.ECMASCRIPT3;
strictMode = SLOPPY;
Node n = parseWarning(

mode = LanguageMode.ECMASCRIPT3;
parseWarning(
"'one\\\ntwo';",
requiresLanguageModeMessage(LanguageMode.ECMASCRIPT5, Feature.STRING_CONTINUATION),
unrecommendedWarning);

mode = LanguageMode.ECMASCRIPT5;
parseWarning("'one\\\ntwo';", unrecommendedWarning);

mode = LanguageMode.ECMASCRIPT6;
parseWarning("'one\\\ntwo';", unrecommendedWarning);
}

public void testStringLineContinuationNormalization() {
String unrecommendedWarning =
"String continuations are not recommended. See"
+ " https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations");
+ " https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations";

expectFeatures(Feature.STRING_CONTINUATION);
mode = LanguageMode.ECMASCRIPT6;
strictMode = SLOPPY;

Node n = parseWarning("'one\\\ntwo';", unrecommendedWarning);
assertThat(n.getFirstFirstChild().getString()).isEqualTo("onetwo");

mode = LanguageMode.ECMASCRIPT5;
parseWarning("'one\\\ntwo';", "String continuations are not recommended. See"
+ " https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations");
n = parseWarning("'one\\\rtwo';", unrecommendedWarning);
assertThat(n.getFirstFirstChild().getString()).isEqualTo("onetwo");

mode = LanguageMode.ECMASCRIPT6;
parseWarning("'one\\\ntwo';", "String continuations are not recommended. See"
+ " https://google.github.io/styleguide/jsguide.html#features-strings-no-line-continuations");
n = parseWarning("'one\\\r\ntwo';", unrecommendedWarning);
assertThat(n.getFirstFirstChild().getString()).isEqualTo("onetwo");

n = parseWarning("'one \\\ntwo';", unrecommendedWarning);
assertThat(n.getFirstFirstChild().getString()).isEqualTo("one two");

n = parseWarning("'one\\\n two';", unrecommendedWarning);
assertThat(n.getFirstFirstChild().getString()).isEqualTo("one two");
}

public void testStringLiteral() {
Expand Down

0 comments on commit b1789cc

Please sign in to comment.