Skip to content

Commit

Permalink
Make ErrorToFixMapper sort mixed require/requireType statements corre…
Browse files Browse the repository at this point in the history
…ctly.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=229467544
  • Loading branch information
tjgq authored and lauraharker committed Jan 16, 2019
1 parent 4620328 commit 7d192e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
Expand Up @@ -88,7 +88,7 @@ public static SuggestedFix getFixForJsError(JSError error, AbstractCompiler comp
return getFixForMissingSemicolon(error, compiler); return getFixForMissingSemicolon(error, compiler);
case "JSC_REQUIRES_NOT_SORTED": case "JSC_REQUIRES_NOT_SORTED":
return getFixForUnsortedRequiresOrProvides( return getFixForUnsortedRequiresOrProvides(
error, compiler, "goog.require", "goog.forwardDeclare"); error, compiler, "goog.require", "goog.requireType", "goog.forwardDeclare");
case "JSC_PROVIDES_NOT_SORTED": case "JSC_PROVIDES_NOT_SORTED":
return getFixForUnsortedRequiresOrProvides(error, compiler, "goog.provide"); return getFixForUnsortedRequiresOrProvides(error, compiler, "goog.provide");
case "JSC_DEBUGGER_STATEMENT_PRESENT": case "JSC_DEBUGGER_STATEMENT_PRESENT":
Expand Down
46 changes: 30 additions & 16 deletions test/com/google/javascript/refactoring/ErrorToFixMapperTest.java
Expand Up @@ -371,7 +371,7 @@ public void testInsertSemicolon2() {
} }


@Test @Test
public void testRequiresSorted1() { public void testRequiresSorted_standalone() {
assertChanges( assertChanges(
LINE_JOINER.join( LINE_JOINER.join(
"/**", "/**",
Expand All @@ -381,7 +381,8 @@ public void testRequiresSorted1() {
"", "",
"", "",
"goog.require('b');", "goog.require('b');",
"goog.require('a');", "goog.requireType('a');",
"goog.requireType('d');",
"goog.require('c');", "goog.require('c');",
"", "",
"", "",
Expand All @@ -393,16 +394,17 @@ public void testRequiresSorted1() {
" */", " */",
"", "",
"", "",
"goog.require('a');", "goog.requireType('a');",
"goog.require('b');", "goog.require('b');",
"goog.require('c');", "goog.require('c');",
"goog.requireType('d');",
"", "",
"", "",
"alert(1);")); "alert(1);"));
} }


@Test @Test
public void testRequiresSorted2() { public void testRequiresSorted_suppressExtra() {
assertChanges( assertChanges(
LINE_JOINER.join( LINE_JOINER.join(
"/**", "/**",
Expand All @@ -412,6 +414,8 @@ public void testRequiresSorted2() {
"goog.provide('x');", "goog.provide('x');",
"", "",
"/** @suppress {extraRequire} */", "/** @suppress {extraRequire} */",
"goog.requireType('c');",
"/** @suppress {extraRequire} */",
"goog.require('b');", "goog.require('b');",
"goog.require('a');", "goog.require('a');",
"", "",
Expand All @@ -426,6 +430,8 @@ public void testRequiresSorted2() {
"goog.require('a');", "goog.require('a');",
"/** @suppress {extraRequire} */", "/** @suppress {extraRequire} */",
"goog.require('b');", "goog.require('b');",
"/** @suppress {extraRequire} */",
"goog.requireType('c');",
"", "",
"alert(1);")); "alert(1);"));
} }
Expand Down Expand Up @@ -539,12 +545,12 @@ public void testSortRequiresInGoogModule_destructuring() {
"goog.module('m');", "goog.module('m');",
"", "",
"const {fooBar} = goog.require('x');", "const {fooBar} = goog.require('x');",
"const {foo, bar} = goog.require('y');"), "const {foo, bar} = goog.requireType('y');"),
LINE_JOINER.join( LINE_JOINER.join(
"/** @fileoverview @suppress {extraRequire} */", "/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');", "goog.module('m');",
"", "",
"const {foo, bar} = goog.require('y');", "const {foo, bar} = goog.requireType('y');",
"const {fooBar} = goog.require('x');")); "const {fooBar} = goog.require('x');"));
} }


Expand All @@ -555,17 +561,17 @@ public void testSortRequiresInGoogModule_shorthandAndStandalone() {
"/** @fileoverview @suppress {extraRequire} */", "/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');", "goog.module('m');",
"", "",
"const shorthand2 = goog.require('a');", "const shorthand2 = goog.requireType('a');",
"goog.require('standalone.two');", "goog.require('standalone.two');",
"goog.require('standalone.one');", "goog.requireType('standalone.one');",
"const shorthand1 = goog.require('b');"), "const shorthand1 = goog.require('b');"),
LINE_JOINER.join( LINE_JOINER.join(
"/** @fileoverview @suppress {extraRequire} */", "/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');", "goog.module('m');",
"", "",
"const shorthand1 = goog.require('b');", "const shorthand1 = goog.require('b');",
"const shorthand2 = goog.require('a');", "const shorthand2 = goog.requireType('a');",
"goog.require('standalone.one');", "goog.requireType('standalone.one');",
"goog.require('standalone.two');")); "goog.require('standalone.two');"));
} }


Expand All @@ -576,19 +582,21 @@ public void testSortRequiresInGoogModule_allThreeStyles() {
"/** @fileoverview @suppress {extraRequire} */", "/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');", "goog.module('m');",
"", "",
"const shorthand2 = goog.require('a');", "const shorthand2 = goog.requireType('a');",
"goog.require('standalone.two');", "goog.require('standalone.two');",
"const {destructuring} = goog.require('c');", "const {destructuring2} = goog.requireType('c');",
"goog.require('standalone.one');", "const {destructuring1} = goog.require('d');",
"goog.requireType('standalone.one');",
"const shorthand1 = goog.require('b');"), "const shorthand1 = goog.require('b');"),
LINE_JOINER.join( LINE_JOINER.join(
"/** @fileoverview @suppress {extraRequire} */", "/** @fileoverview @suppress {extraRequire} */",
"goog.module('m');", "goog.module('m');",
"", "",
"const shorthand1 = goog.require('b');", "const shorthand1 = goog.require('b');",
"const shorthand2 = goog.require('a');", "const shorthand2 = goog.requireType('a');",
"const {destructuring} = goog.require('c');", "const {destructuring1} = goog.require('d');",
"goog.require('standalone.one');", "const {destructuring2} = goog.requireType('c');",
"goog.requireType('standalone.one');",
"goog.require('standalone.two');")); "goog.require('standalone.two');"));
} }


Expand Down Expand Up @@ -626,6 +634,7 @@ public void testSortRequiresInGoogModule_withOtherStatements() {
"const bar = goog.require('bar');", "const bar = goog.require('bar');",
"const {Bar} = bar;", "const {Bar} = bar;",
"const util = goog.require('util');", "const util = goog.require('util');",
"const type = goog.requireType('type');",
"const {doCoolThings} = util;", "const {doCoolThings} = util;",
"", "",
"doCoolThings(foo, Bar);"), "doCoolThings(foo, Bar);"),
Expand All @@ -636,6 +645,7 @@ public void testSortRequiresInGoogModule_withOtherStatements() {
"const foo = goog.require('foo');", "const foo = goog.require('foo');",
"const {Bar} = bar;", "const {Bar} = bar;",
"const util = goog.require('util');", "const util = goog.require('util');",
"const type = goog.requireType('type');",
"const {doCoolThings} = util;", "const {doCoolThings} = util;",
"", "",
"doCoolThings(foo, Bar);")); "doCoolThings(foo, Bar);"));
Expand All @@ -648,16 +658,20 @@ public void testSortRequiresInGoogModule_veryLongRequire() {
"goog.module('m');", "goog.module('m');",
"", "",
"const {veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} = goog.require('other');", "const {veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} = goog.require('other');",
"const {anotherVeryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} = goog.requireType('type');",
"const shorter = goog.require('shorter');", "const shorter = goog.require('shorter');",
"", "",
"/** @type {!anotherVeryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} */ var x;",
"use(veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace);", "use(veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace);",
"use(shorter);"), "use(shorter);"),
LINE_JOINER.join( LINE_JOINER.join(
"goog.module('m');", "goog.module('m');",
"", "",
"const shorter = goog.require('shorter');", "const shorter = goog.require('shorter');",
"const {anotherVeryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} = goog.requireType('type');",
"const {veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} = goog.require('other');", "const {veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} = goog.require('other');",
"", "",
"/** @type {!anotherVeryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace} */ var x;",
"use(veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace);", "use(veryLongDestructuringStatementSoLongThatWeGoPast80CharactersBeforeGettingToTheClosingCurlyBrace);",
"use(shorter);")); "use(shorter);"));
} }
Expand Down

0 comments on commit 7d192e8

Please sign in to comment.