diff --git a/src/com/google/javascript/jscomp/parsing/IRFactory.java b/src/com/google/javascript/jscomp/parsing/IRFactory.java index 594d428b9ce..65f5ee12cf4 100644 --- a/src/com/google/javascript/jscomp/parsing/IRFactory.java +++ b/src/com/google/javascript/jscomp/parsing/IRFactory.java @@ -2993,10 +2993,11 @@ String normalizeRegex(LiteralToken token) { String normalizeString(LiteralToken token, boolean templateLiteral) { String value = token.value; - if (templateLiteral) { - // and are normalized as for raw string value - value = value.replaceAll("\r\n?", "\n"); - } + // and are normalized as . 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) { diff --git a/test/com/google/javascript/jscomp/parsing/ParserTest.java b/test/com/google/javascript/jscomp/parsing/ParserTest.java index 1dd7f34d713..e64303dc4dc 100644 --- a/test/com/google/javascript/jscomp/parsing/ParserTest.java +++ b/test/com/google/javascript/jscomp/parsing/ParserTest.java @@ -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() {