From 5af9208414a28b8505cd99aa6087caccc5c36fc2 Mon Sep 17 00:00:00 2001 From: tjgq Date: Thu, 7 Mar 2019 14:08:38 -0800 Subject: [PATCH] Remove CheckRequiresAndProvidesSorted in favor of CheckProvidesSorted and CheckRequiresSorted. We can also reenable the disabled tests in ErrorToFixMapperTest, since the linter and suggested fix logic is now the same. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=237321038 --- .../javascript/jscomp/DefaultPassConfig.java | 9 +- .../javascript/jscomp/DiagnosticGroups.java | 9 +- .../javascript/jscomp/LintPassConfig.java | 6 +- .../jscomp/lint/CheckProvidesSorted.java | 7 +- .../lint/CheckRequiresAndProvidesSorted.java | 227 -------- .../jscomp/lint/CheckRequiresSorted.java | 7 +- .../jscomp/lint/CheckProvidesSortedTest.java | 2 +- .../CheckRequiresAndProvidesSortedTest.java | 489 ------------------ .../jscomp/lint/CheckRequiresSortedTest.java | 2 +- .../refactoring/ErrorToFixMapperTest.java | 14 - 10 files changed, 29 insertions(+), 743 deletions(-) delete mode 100644 src/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSorted.java delete mode 100644 test/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSortedTest.java diff --git a/src/com/google/javascript/jscomp/DefaultPassConfig.java b/src/com/google/javascript/jscomp/DefaultPassConfig.java index 09f1da47ef3..d36739bb05b 100644 --- a/src/com/google/javascript/jscomp/DefaultPassConfig.java +++ b/src/com/google/javascript/jscomp/DefaultPassConfig.java @@ -55,7 +55,8 @@ import com.google.javascript.jscomp.lint.CheckNullableReturn; import com.google.javascript.jscomp.lint.CheckPrimitiveAsObject; import com.google.javascript.jscomp.lint.CheckPrototypeProperties; -import com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted; +import com.google.javascript.jscomp.lint.CheckProvidesSorted; +import com.google.javascript.jscomp.lint.CheckRequiresSorted; import com.google.javascript.jscomp.lint.CheckUnusedLabels; import com.google.javascript.jscomp.lint.CheckUselessBlocks; import com.google.javascript.jscomp.modules.ModuleMapCreator; @@ -2074,7 +2075,11 @@ protected FeatureSet featureSet() { new HotSwapPassFactory("checkRequiresAndProvidesSorted") { @Override protected HotSwapCompilerPass create(AbstractCompiler compiler) { - return new CheckRequiresAndProvidesSorted(compiler); + return combineChecks( + compiler, + ImmutableList.of( + new CheckProvidesSorted(CheckProvidesSorted.Mode.COLLECT_AND_REPORT), + new CheckRequiresSorted(CheckRequiresSorted.Mode.COLLECT_AND_REPORT))); } @Override diff --git a/src/com/google/javascript/jscomp/DiagnosticGroups.java b/src/com/google/javascript/jscomp/DiagnosticGroups.java index f0caaf0328a..a0e72e57d8c 100644 --- a/src/com/google/javascript/jscomp/DiagnosticGroups.java +++ b/src/com/google/javascript/jscomp/DiagnosticGroups.java @@ -41,7 +41,8 @@ import com.google.javascript.jscomp.lint.CheckNullableReturn; import com.google.javascript.jscomp.lint.CheckPrimitiveAsObject; import com.google.javascript.jscomp.lint.CheckPrototypeProperties; -import com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted; +import com.google.javascript.jscomp.lint.CheckProvidesSorted; +import com.google.javascript.jscomp.lint.CheckRequiresSorted; import com.google.javascript.jscomp.lint.CheckUnusedLabels; import com.google.javascript.jscomp.lint.CheckUselessBlocks; import com.google.javascript.jscomp.modules.ModuleMapCreator; @@ -616,10 +617,8 @@ public DiagnosticGroup forName(String name) { CheckPrimitiveAsObject.NEW_PRIMITIVE_OBJECT, CheckPrimitiveAsObject.PRIMITIVE_OBJECT_DECLARATION, CheckPrototypeProperties.ILLEGAL_PROTOTYPE_MEMBER, - CheckRequiresAndProvidesSorted.DUPLICATE_REQUIRE, - CheckRequiresAndProvidesSorted.REQUIRES_NOT_SORTED, - CheckRequiresAndProvidesSorted.PROVIDES_NOT_SORTED, - CheckRequiresAndProvidesSorted.PROVIDES_AFTER_REQUIRES, + CheckProvidesSorted.PROVIDES_NOT_SORTED, + CheckRequiresSorted.REQUIRES_NOT_SORTED, CheckUnusedLabels.UNUSED_LABEL, CheckUselessBlocks.USELESS_BLOCK, ClosureCheckModule.DECLARE_LEGACY_NAMESPACE_IN_NON_MODULE, diff --git a/src/com/google/javascript/jscomp/LintPassConfig.java b/src/com/google/javascript/jscomp/LintPassConfig.java index 7c5490bb442..5985b7ced87 100644 --- a/src/com/google/javascript/jscomp/LintPassConfig.java +++ b/src/com/google/javascript/jscomp/LintPassConfig.java @@ -26,7 +26,8 @@ import com.google.javascript.jscomp.lint.CheckNullabilityModifiers; import com.google.javascript.jscomp.lint.CheckPrimitiveAsObject; import com.google.javascript.jscomp.lint.CheckPrototypeProperties; -import com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted; +import com.google.javascript.jscomp.lint.CheckProvidesSorted; +import com.google.javascript.jscomp.lint.CheckRequiresSorted; import com.google.javascript.jscomp.lint.CheckUnusedLabels; import com.google.javascript.jscomp.lint.CheckUselessBlocks; import com.google.javascript.jscomp.parsing.parser.FeatureSet; @@ -92,7 +93,8 @@ protected CompilerPass create(AbstractCompiler compiler) { new CheckPrimitiveAsObject(compiler), new ClosureCheckModule(compiler, compiler.getModuleMetadataMap()), new CheckNullabilityModifiers(compiler), - new CheckRequiresAndProvidesSorted(compiler), + new CheckProvidesSorted(CheckProvidesSorted.Mode.COLLECT_AND_REPORT), + new CheckRequiresSorted(CheckRequiresSorted.Mode.COLLECT_AND_REPORT), new CheckSideEffects( compiler, /* report */ true, /* protectSideEffectFreeCode */ false), new CheckTypeImportCodeReferences(compiler), diff --git a/src/com/google/javascript/jscomp/lint/CheckProvidesSorted.java b/src/com/google/javascript/jscomp/lint/CheckProvidesSorted.java index 960317a8cb0..f843fc10087 100644 --- a/src/com/google/javascript/jscomp/lint/CheckProvidesSorted.java +++ b/src/com/google/javascript/jscomp/lint/CheckProvidesSorted.java @@ -17,9 +17,9 @@ package com.google.javascript.jscomp.lint; import static com.google.common.collect.ImmutableList.toImmutableList; -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.PROVIDES_NOT_SORTED; import com.google.common.collect.Iterables; +import com.google.javascript.jscomp.DiagnosticType; import com.google.javascript.jscomp.NodeTraversal; import com.google.javascript.rhino.Node; import java.util.ArrayList; @@ -31,6 +31,11 @@ * information to produce a suggested fix. */ public final class CheckProvidesSorted implements NodeTraversal.Callback { + public static final DiagnosticType PROVIDES_NOT_SORTED = + DiagnosticType.warning( + "JSC_PROVIDES_NOT_SORTED", + "goog.provide() statements are not sorted." + + " The correct order is:\n\n{0}\n"); /** Operation modes. */ public enum Mode { diff --git a/src/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSorted.java b/src/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSorted.java deleted file mode 100644 index 1ab4a960835..00000000000 --- a/src/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSorted.java +++ /dev/null @@ -1,227 +0,0 @@ -/* - * Copyright 2015 The Closure Compiler Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.javascript.jscomp.lint; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; - -import com.google.common.collect.Ordering; -import com.google.javascript.jscomp.AbstractCompiler; -import com.google.javascript.jscomp.DiagnosticType; -import com.google.javascript.jscomp.HotSwapCompilerPass; -import com.google.javascript.jscomp.JSError; -import com.google.javascript.jscomp.NodeTraversal; -import com.google.javascript.jscomp.NodeTraversal.AbstractShallowCallback; -import com.google.javascript.jscomp.NodeUtil; -import com.google.javascript.rhino.Node; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * Checks that goog.require() and goog.provide() calls are sorted alphabetically. - */ -public final class CheckRequiresAndProvidesSorted extends AbstractShallowCallback - implements HotSwapCompilerPass { - public static final DiagnosticType REQUIRES_NOT_SORTED = - DiagnosticType.warning( - "JSC_REQUIRES_NOT_SORTED", - "goog.require() and goog.requireType() statements are not sorted." - + " The correct order is:\n\n{0}\n"); - - public static final DiagnosticType PROVIDES_NOT_SORTED = - DiagnosticType.warning( - "JSC_PROVIDES_NOT_SORTED", - "goog.provide() statements are not sorted." - + " The correct order is:\n\n{0}\n"); - - public static final DiagnosticType PROVIDES_AFTER_REQUIRES = - DiagnosticType.warning( - "JSC_PROVIDES_AFTER_REQUIRES", - "goog.provide() statements should be before" - + " goog.require() and goog.requireType() statements."); - - public static final DiagnosticType DUPLICATE_REQUIRE = - DiagnosticType.warning("JSC_DUPLICATE_REQUIRE", "''{0}'' required more than once."); - - private final List requires; - private final List provides; - - private final AbstractCompiler compiler; - - public CheckRequiresAndProvidesSorted(AbstractCompiler compiler) { - this.compiler = compiler; - this.requires = new ArrayList<>(); - this.provides = new ArrayList<>(); - } - - @Override - public void process(Node externs, Node root) { - NodeTraversal.traverse(compiler, root, this); - } - - @Override - public void hotSwapScript(Node scriptRoot, Node originalRoot) { - NodeTraversal.traverse(compiler, scriptRoot, this); - } - - public static final String getSortKey(Node n) { - String key = null; - boolean isForwardDeclare = false; - if (NodeUtil.isNameDeclaration(n)) { - if (n.getFirstChild().isName()) { - // Case 1: - // var x = goog.require('w.x'); - // or - // var x = goog.forwardDeclare('w.x'); - key = n.getFirstChild().getString(); - if (n.getFirstFirstChild().getFirstChild().matchesQualifiedName("goog.forwardDeclare")) { - isForwardDeclare = true; - } - } else if (n.getFirstChild().isDestructuringLhs()) { - // Case 2: var {y} = goog.require('w.x'); - // All case 2 nodes should come after all case 1 nodes. ('{' sorts after a-z) - Node pattern = n.getFirstFirstChild(); - checkState(pattern.isObjectPattern(), pattern); - Node call = n.getFirstChild().getLastChild(); - checkState(call.isCall(), call); - checkState( - call.getFirstChild().matchesQualifiedName("goog.require") - || call.getFirstChild().matchesQualifiedName("goog.requireType"), - call.getFirstChild()); - if (!pattern.hasChildren()) { - key = "{"; - } else { - key = "{" + pattern.getFirstChild().getString(); - } - } - } else if (n.isExprResult()) { - // Case 3, one of: - // goog.provide('a.b.c'); - // goog.require('a.b.c'); - // goog.forwardDeclare('a.b.c'); - // All case 3 nodes should come after case 1 and 2 nodes, so prepend - // '|' which sorts after '{' - key = "|" + n.getFirstChild().getLastChild().getString(); - if (n.getFirstFirstChild().matchesQualifiedName("goog.forwardDeclare")) { - isForwardDeclare = true; - } - } else { - throw new IllegalArgumentException("Unexpected node " + n); - } - // Make sure all forwardDeclares come after all requires. - return (isForwardDeclare ? "z" : "a") + checkNotNull(key); - } - - private static final String getNamespace(Node requireStatement) { - if (requireStatement.isExprResult()) { - // goog.require('a.b.c'); - return requireStatement.getFirstChild().getLastChild().getString(); - } else if (NodeUtil.isNameDeclaration(requireStatement)) { - if (requireStatement.getFirstChild().isName()) { - // const x = goog.require('a.b.c'); - return requireStatement.getFirstFirstChild().getLastChild().getString(); - } else if (requireStatement.getFirstChild().isDestructuringLhs()) { - // const {x} = goog.require('a.b.c'); - return requireStatement.getFirstChild().getLastChild().getLastChild().getString(); - } - } - throw new IllegalArgumentException("Unexpected node " + requireStatement); - } - - private final Ordering alphabetical = - Ordering.natural().onResultOf(CheckRequiresAndProvidesSorted::getSortKey); - - @Override - public void visit(NodeTraversal t, Node n, Node parent) { - switch (n.getToken()) { - case SCRIPT: - // Duplicate provides are already checked in ProcessClosurePrimitives. - checkForDuplicates(requires); - reportIfOutOfOrder(requires, REQUIRES_NOT_SORTED); - reportIfOutOfOrder(provides, PROVIDES_NOT_SORTED); - reset(); - break; - case CALL: - Node callee = n.getFirstChild(); - if (!callee.matchesQualifiedName("goog.require") - && !callee.matchesQualifiedName("goog.requireType") - && !callee.matchesQualifiedName("goog.forwardDeclare") - && !callee.matchesQualifiedName("goog.provide") - && !callee.matchesQualifiedName("goog.module")) { - return; - } - - if (parent.isExprResult() && NodeUtil.isTopLevel(parent.getParent())) { - Node namespaceNode = n.getLastChild(); - if (!namespaceNode.isString()) { - return; - } - String namespace = namespaceNode.getString(); - if (namespace == null) { - return; - } - if (callee.matchesQualifiedName("goog.require") - || callee.matchesQualifiedName("goog.requireType") - || callee.matchesQualifiedName("goog.forwardDeclare")) { - requires.add(parent); - } else { - if (!requires.isEmpty()) { - t.report(n, PROVIDES_AFTER_REQUIRES); - } - if (callee.matchesQualifiedName("goog.provide")) { - provides.add(parent); - } - } - } else if (NodeUtil.isNameDeclaration(parent.getParent()) - && (callee.matchesQualifiedName("goog.require") - || callee.matchesQualifiedName("goog.requireType") - || callee.matchesQualifiedName("goog.forwardDeclare"))) { - requires.add(parent.getParent()); - } - break; - default: - break; - } - } - - private void reportIfOutOfOrder(List requiresOrProvides, DiagnosticType warning) { - if (!alphabetical.isOrdered(requiresOrProvides)) { - StringBuilder correctOrder = new StringBuilder(); - for (Node n : alphabetical.sortedCopy(requiresOrProvides)) { - correctOrder.append(compiler.toSource(n)); - } - compiler.report( - JSError.make(requiresOrProvides.get(0), warning, correctOrder.toString())); - } - } - - private void checkForDuplicates(List requires) { - Set namespaces = new HashSet<>(); - for (Node require : requires) { - String namespace = getNamespace(require); - if (!namespaces.add(namespace)) { - compiler.report(JSError.make(require, DUPLICATE_REQUIRE, namespace)); - } - } - } - - private void reset() { - requires.clear(); - provides.clear(); - } -} diff --git a/src/com/google/javascript/jscomp/lint/CheckRequiresSorted.java b/src/com/google/javascript/jscomp/lint/CheckRequiresSorted.java index f025631e48f..f4d460bb574 100644 --- a/src/com/google/javascript/jscomp/lint/CheckRequiresSorted.java +++ b/src/com/google/javascript/jscomp/lint/CheckRequiresSorted.java @@ -17,7 +17,6 @@ package com.google.javascript.jscomp.lint; import static com.google.common.collect.ImmutableList.toImmutableList; -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.REQUIRES_NOT_SORTED; import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; @@ -27,6 +26,7 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Multimap; import com.google.errorprone.annotations.Immutable; +import com.google.javascript.jscomp.DiagnosticType; import com.google.javascript.jscomp.NodeTraversal; import com.google.javascript.jscomp.NodeUtil; import com.google.javascript.rhino.JSDocInfo; @@ -42,6 +42,11 @@ * are sorted and deduplicated, exposing the necessary information to produce a suggested fix. */ public final class CheckRequiresSorted implements NodeTraversal.Callback { + public static final DiagnosticType REQUIRES_NOT_SORTED = + DiagnosticType.warning( + "JSC_REQUIRES_NOT_SORTED", + "goog.require() and goog.requireType() statements are not sorted." + + " The correct order is:\n\n{0}\n"); /** Operation modes. */ public enum Mode { diff --git a/test/com/google/javascript/jscomp/lint/CheckProvidesSortedTest.java b/test/com/google/javascript/jscomp/lint/CheckProvidesSortedTest.java index 72f74041f12..fa6e72b14c6 100644 --- a/test/com/google/javascript/jscomp/lint/CheckProvidesSortedTest.java +++ b/test/com/google/javascript/jscomp/lint/CheckProvidesSortedTest.java @@ -15,7 +15,7 @@ */ package com.google.javascript.jscomp.lint; -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.PROVIDES_NOT_SORTED; +import static com.google.javascript.jscomp.lint.CheckProvidesSorted.PROVIDES_NOT_SORTED; import com.google.javascript.jscomp.Compiler; import com.google.javascript.jscomp.CompilerOptions; diff --git a/test/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSortedTest.java b/test/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSortedTest.java deleted file mode 100644 index 09bd99d37df..00000000000 --- a/test/com/google/javascript/jscomp/lint/CheckRequiresAndProvidesSortedTest.java +++ /dev/null @@ -1,489 +0,0 @@ -/* - * Copyright 2015 The Closure Compiler Authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.javascript.jscomp.lint; - -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.DUPLICATE_REQUIRE; -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.PROVIDES_AFTER_REQUIRES; -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.PROVIDES_NOT_SORTED; -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.REQUIRES_NOT_SORTED; - -import com.google.javascript.jscomp.CheckLevel; -import com.google.javascript.jscomp.Compiler; -import com.google.javascript.jscomp.CompilerOptions; -import com.google.javascript.jscomp.CompilerOptions.LanguageMode; -import com.google.javascript.jscomp.CompilerPass; -import com.google.javascript.jscomp.CompilerTestCase; -import com.google.javascript.jscomp.DiagnosticGroups; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public final class CheckRequiresAndProvidesSortedTest extends CompilerTestCase { - @Override - protected CompilerPass getProcessor(Compiler compiler) { - return new CheckRequiresAndProvidesSorted(compiler); - } - - @Override - protected int getNumRepetitions() { - return 1; - } - - @Override - @Before - public void setUp() throws Exception { - super.setUp(); - setAcceptedLanguage(LanguageMode.ECMASCRIPT_2017); - } - - @Override - protected CompilerOptions getOptions() { - CompilerOptions options = super.getOptions(); - options.setPrettyPrint(true); - options.setPreserveTypeAnnotations(true); - options.setPreferSingleQuotes(true); - options.setEmitUseStrict(false); - options.setWarningLevel(DiagnosticGroups.MODULE_LOAD, CheckLevel.OFF); - return options; - } - - @Test - public void testNoWarning_require() { - testNoWarning("goog.require('a.b');\ngoog.require('a.c')"); - - testNoWarning( - lines( - "goog.require('namespace');", - "goog.require('namespace.ExampleClass');", - "goog.require('namespace.ExampleClass.ExampleInnerClass');")); - - testNoWarning( - lines( - "goog.require('namespace.Example');", - "goog.require('namespace.example');")); - - testNoWarning(lines("goog.requireType('namespace1');", "goog.require('namespace2');")); - } - - @Test - public void testNoWarning_provide() { - testNoWarning("goog.provide('a.b');\ngoog.provide('a.c')"); - - testNoWarning( - lines( - "goog.provide('namespace');", - "goog.provide('namespace.ExampleClass');", - "goog.provide('namespace.ExampleClass.ExampleInnerClass');")); - - testNoWarning( - lines( - "goog.provide('namespace.Example');", - "goog.provide('namespace.example');")); - } - - @Test - public void testWarning_require() { - test( - srcs(lines("goog.require('a.c');", "goog.require('a.b')")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", "", "goog.require('a.b');", "goog.require('a.c');"))); - - test( - srcs(lines("goog.require('a.c');", "goog.require('a')")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines("The correct order is:", "", "goog.require('a');", "goog.require('a.c');"))); - - test( - srcs(lines("goog.require('a.c');", "goog.requireType('a')")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "goog.requireType('a');", - "goog.require('a.c');"))); - - test( - srcs(lines("goog.requireType('a.c');", "goog.require('a')")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "goog.require('a');", - "goog.requireType('a.c');"))); - } - - @Test - public void testWarning_requireWithJSDoc() { - test( - srcs(lines( - "goog.require('a.c');", - "/** @suppress {extraRequire} */", - "goog.require('a.b')")), - warning(REQUIRES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "/**", - " @suppress {extraRequire}", - " */", - "goog.require('a.b');", - "goog.require('a.c');"))); - - test( - srcs( - lines( - "goog.require('a.c');", - "/** @suppress {extraRequire} */", - "goog.requireType('a.b')")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "/**", - " @suppress {extraRequire}", - " */", - "goog.requireType('a.b');", - "goog.require('a.c');"))); - - test( - srcs( - lines( - "goog.requireType('a.c');", - "/** @suppress {extraRequire} */", - "goog.require('a.b')")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "/**", - " @suppress {extraRequire}", - " */", - "goog.require('a.b');", - "goog.requireType('a.c');"))); - } - - @Test - public void testWarning_provide() { - test( - srcs("goog.provide('a.c');\ngoog.provide('a.b')"), - warning(PROVIDES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "goog.provide('a.b');", - "goog.provide('a.c');"))); - test( - srcs("goog.provide('a.c');\ngoog.provide('a')"), - warning(PROVIDES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "goog.provide('a');", - "goog.provide('a.c');"))); - } - - @Test - public void testWarning_requiresFirst() { - test( - srcs("goog.require('a');\ngoog.provide('b')"), - warning(PROVIDES_AFTER_REQUIRES)); - - test(srcs("goog.requireType('a');\ngoog.provide('b')"), warning(PROVIDES_AFTER_REQUIRES)); - } - - @Test - public void testB3473189() { - testNoWarning( - lines( - "goog.provide('abc');", - "if (typeof goog != 'undefined' && typeof goog.provide == 'function') {", - " goog.provide('MyLib.Base');", - "}")); - } - - @Test - public void testGoogModule_shorthandAndStandalone() { - test( - srcs(lines( - "goog.module('m');", - "", - "goog.require('a.c');", - "const d = goog.require('a.b.d');", - "goog.require('a.b.c');", - "", - "alert(1);")), - warning(REQUIRES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "const d = goog.require('a.b.d');", - "goog.require('a.b.c');", - "goog.require('a.c');"))); - - test( - srcs( - lines( - "goog.module('m');", - "", - "goog.require('a.c');", - "const d = goog.requireType('a.b.d');", - "goog.require('a.b.c');", - "", - "alert(1);")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "const d = goog.requireType('a.b.d');", - "goog.require('a.b.c');", - "goog.require('a.c');"))); - } - - @Test - public void testGoogModule_destructuring() { - test( - srcs( - lines( - "goog.module('m');", - "", - "goog.require('z');", - "goog.requireType('a');", - "var {someFunction, anotherFunction} = goog.require('example.utils');", - "var {A_CONST, ANOTHER_CONST} = goog.requireType('example.constants');", - "", - "alert(1);")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "var {A_CONST, ANOTHER_CONST} = goog.requireType('example.constants');", - "var {someFunction, anotherFunction} = goog.require('example.utils');", - "goog.requireType('a');", - "goog.require('z');"))); - } - - @Test - public void testGoogModule_emptyDestructuring() { - test( - srcs(lines( - "goog.module('m');", - "", - "const {FOO} = goog.require('example.constants');", - "const {} = goog.require('just.forthe.side.effects');", - "", - "alert(1);")), - warning(REQUIRES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "const {} = goog.require('just.forthe.side.effects');", - "const {FOO} = goog.require('example.constants');"))); - - testNoWarning( - lines( - "goog.module('m');", - "", - "const {} = goog.require('just.forthe.side.effects');", - "const {FOO} = goog.require('example.constants');", - "", - "alert(1);")); - } - - @Test - public void testGoogModule_allThreeStyles() { - test( - srcs( - lines( - "/** @fileoverview @suppress {extraRequire} */", - "goog.module('m');", - "", - "const shorthand2 = goog.require('a');", - "goog.require('standalone.two');", - "const {destructuring} = goog.require('c');", - "goog.require('standalone.one');", - "const shorthand1 = goog.require('b');")), - warning(REQUIRES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "const shorthand1 = goog.require('b');", - "const shorthand2 = goog.require('a');", - "const {destructuring} = goog.require('c');", - "goog.require('standalone.one');", - "goog.require('standalone.two');"))); - } - - @Test - public void testGoogModule_shorthand() { - test( - srcs( - lines( - "goog.module('m');", - "", - "var d = goog.require('a.b.d');", - "var c = goog.requireType('a.c');", - "", - "alert(1);")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "var c = goog.requireType('a.c');", - "var d = goog.require('a.b.d');"))); - } - - @Test - public void testGoogModule_shorthand_destructuring() { - test( - srcs(lines( - "goog.module('m');", - "", - "var a = goog.require('a.b.d');", - "var {b} = goog.require('a.a.a');", - "var c = goog.require('a.c');", - "", - "alert(1);")), - warning(REQUIRES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "var a = goog.require('a.b.d');", - "var c = goog.require('a.c');", - "var {b} = goog.require('a.a.a');"))); - } - - @Test - public void testGoogModule_standalone() { - test( - srcs( - lines( - "goog.module('m');", - "", - "goog.requireType('a.c');", - "goog.require('a.b.d');", - "goog.require('a.b.c');", - "", - "alert(1);")), - warning(REQUIRES_NOT_SORTED) - .withMessageContaining( - lines( - "The correct order is:", - "", - "goog.require('a.b.c');", - "goog.require('a.b.d');", - "goog.requireType('a.c');"))); - } - - @Test - public void testGoogModule_forwardDeclares() { - test( - srcs(lines( - "goog.module('x');", - "", - "const s = goog.require('s');", - "const f = goog.forwardDeclare('f');", - "const r = goog.require('r');")), - warning(REQUIRES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "const r = goog.require('r');", - "const s = goog.require('s');", - "const f = goog.forwardDeclare('f');"))); - } - - @Test - public void testForwardDeclares() { - test( - srcs( - lines( - "goog.provide('x');", - "", - "goog.require('s');", - "goog.forwardDeclare('f');", - "goog.require('r');")), - warning(REQUIRES_NOT_SORTED).withMessageContaining(lines( - "The correct order is:", - "", - "goog.require('r');", - "goog.require('s');", - "goog.forwardDeclare('f');"))); - } - - @Test - public void testDuplicate() { - testWarning( - lines( - "goog.require('Bar');", - "goog.require('Bar');"), - DUPLICATE_REQUIRE); - - testWarning(lines("goog.requireType('Bar');", "goog.requireType('Bar');"), DUPLICATE_REQUIRE); - - testWarning(lines("goog.require('Bar');", "goog.requireType('Bar');"), DUPLICATE_REQUIRE); - } - - @Test - public void testDuplicate_shorthand() { - testWarning( - lines( - "const Bar1 = goog.require('Bar');", - "const Bar2 = goog.require('Bar');"), - DUPLICATE_REQUIRE); - - testWarning( - lines("const Bar1 = goog.requireType('Bar');", "const Bar2 = goog.requireType('Bar');"), - DUPLICATE_REQUIRE); - - testWarning( - lines("const Bar1 = goog.require('Bar');", "const Bar2 = goog.requireType('Bar');"), - DUPLICATE_REQUIRE); - } - - @Test - public void testDuplicate_destructuring() { - testWarning( - lines( - "const Bar = goog.require('Bar');", - "const {Foo} = goog.require('Bar');"), - DUPLICATE_REQUIRE); - - testWarning( - lines("const Bar = goog.requireType('Bar');", "const {Foo} = goog.requireType('Bar');"), - DUPLICATE_REQUIRE); - - testWarning( - lines("const Bar = goog.require('Bar');", "const {Foo} = goog.requireType('Bar');"), - DUPLICATE_REQUIRE); - } - - // Compiler doesn't sort ES6 modules yet, because semantics not yet finalized. - // Simple test to make sure compiler does not crash. - @Test - public void testES6Modules() { - testNoWarning( - lines( - "import foo from '/bar';", - "import bar from '/foo';", - "import * as a from '/b';", - "import {a, b} from '/c';", - "import '/foobar';")); - } -} diff --git a/test/com/google/javascript/jscomp/lint/CheckRequiresSortedTest.java b/test/com/google/javascript/jscomp/lint/CheckRequiresSortedTest.java index 79a841f8c87..8bef810d3e8 100644 --- a/test/com/google/javascript/jscomp/lint/CheckRequiresSortedTest.java +++ b/test/com/google/javascript/jscomp/lint/CheckRequiresSortedTest.java @@ -15,7 +15,7 @@ */ package com.google.javascript.jscomp.lint; -import static com.google.javascript.jscomp.lint.CheckRequiresAndProvidesSorted.REQUIRES_NOT_SORTED; +import static com.google.javascript.jscomp.lint.CheckRequiresSorted.REQUIRES_NOT_SORTED; import com.google.javascript.jscomp.Compiler; import com.google.javascript.jscomp.CompilerOptions; diff --git a/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java b/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java index f33b2a62715..dd8bcb176a5 100644 --- a/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java +++ b/test/com/google/javascript/refactoring/ErrorToFixMapperTest.java @@ -482,7 +482,6 @@ public void testFixRequires_sortAllTypes() { useInType("b", "c", "e"))); } - @Ignore @Test public void testFixRequires_deduplicate_standalone() { assertChanges( @@ -552,7 +551,6 @@ public void testFixRequires_mergeDestructuring_multiplePrimitives() { "const {a, b, c} = goog.require('a');", useInCode("a", "c"), useInType("a", "b"))); } - @Ignore @Test public void testFixRequires_emptyDestructuring_alone() { assertChanges( @@ -560,7 +558,6 @@ public void testFixRequires_emptyDestructuring_alone() { fileWithImports("goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_emptyDestructuringStandaloneBySamePrimitive() { assertChanges( @@ -568,7 +565,6 @@ public void testFixRequires_emptyDestructuringStandaloneBySamePrimitive() { fileWithImports("goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_emptyDestructuringStandaloneByStrongerPrimitive() { assertChanges( @@ -576,7 +572,6 @@ public void testFixRequires_emptyDestructuringStandaloneByStrongerPrimitive() { fileWithImports("goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_emptyDestructuringStandaloneByWeakerPrimitive() { assertChanges( @@ -621,7 +616,6 @@ public void testFixRequires_aliasPreservedWhenDestructuring() { useInType("a"))); } - @Ignore @Test public void testFixRequires_standaloneAliasedBySamePrimitive() { assertChanges( @@ -629,7 +623,6 @@ public void testFixRequires_standaloneAliasedBySamePrimitive() { fileWithImports("const a = goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_standaloneAliasedByStrongerPrimitive() { assertChanges( @@ -637,7 +630,6 @@ public void testFixRequires_standaloneAliasedByStrongerPrimitive() { fileWithImports("const a = goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_standaloneAliasedByWeakerPrimitive() { assertChanges( @@ -645,7 +637,6 @@ public void testFixRequires_standaloneAliasedByWeakerPrimitive() { fileWithImports("const a = goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_standaloneDestructuredBySamePrimitive() { assertChanges( @@ -653,7 +644,6 @@ public void testFixRequires_standaloneDestructuredBySamePrimitive() { fileWithImports("const {a} = goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_standaloneDestructuredByStrongerPrimitive() { assertChanges( @@ -661,7 +651,6 @@ public void testFixRequires_standaloneDestructuredByStrongerPrimitive() { fileWithImports("const {a} = goog.require('a');", useInCode("a"))); } - @Ignore @Test public void testFixRequires_standaloneDestructuredByWeakerPrimitive() { assertChanges( @@ -707,7 +696,6 @@ public void testFixRequires_preserveJsDoc_whenSorting() { useInCode("a", "b", "c"))); } - @Ignore @Test public void testFixRequires_preserveJsDoc_whenMergingStandalone() { assertChanges( @@ -726,7 +714,6 @@ public void testFixRequires_preserveJsDoc_whenMergingStandalone() { useInCode("a"))); } - @Ignore @Test public void testFixRequires_preserveJsDoc_whenMergingDestructures_single() { assertChanges( @@ -745,7 +732,6 @@ public void testFixRequires_preserveJsDoc_whenMergingDestructures_single() { useInCode("b", "c"))); } - @Ignore @Test public void testFixRequires_preserveJsDoc_whenMergingDestructures_multiple() { // TODO(tjgq): Consider merging multiple @suppress annotations into a single comment.