Search in sources :

Example 51 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class DisambiguateProperties method invalidateNonDeclaredPropertyAccesses.

/**
 * Invalidate all property accesses that cause "missing property" warnings.
 *
 * <p>This behavior is not inherently necessary for correctness. It only exists because the older
 * version of the disambiguator invalidated these properties and some projects began to rely on
 * this.
 *
 * <p>TODO(b/177695515): delete this method
 */
private void invalidateNonDeclaredPropertyAccesses(ColorGraphNode colorGraphNode) {
    Color color = colorGraphNode.getColor();
    checkArgument(!color.isInvalidating(), "Not applicable to invalidating types. All their properties are invalidated");
    for (PropertyClustering prop : colorGraphNode.getAssociatedProps().keySet()) {
        if (prop.isInvalidated()) {
            // Skip unnecessary `hasProperty` lookups which can be expensive.
            continue;
        }
        if (!mayHaveProperty(color, prop.getName())) {
            prop.invalidate(Invalidation.undeclaredAccess(colorGraphNode.getIndex()));
        }
    }
}
Also used : Color(com.google.javascript.jscomp.colors.Color)

Example 52 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class ColorFindPropertyReferences method handleObjectLit.

private void handleObjectLit(Node n) {
    // Object.defineProperties literals are handled at the CALL node.
    if (n.getParent().isCall() && NodeUtil.isObjectDefinePropertiesDefinition(n.getParent())) {
        return;
    }
    Color owner = n.getColor();
    this.traverseObjectlitLike(n, (m) -> owner);
}
Also used : Color(com.google.javascript.jscomp.colors.Color)

Example 53 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class ColorFindPropertyReferences method handleObjectPattern.

private void handleObjectPattern(Node pattern) {
    Color owner = pattern.getColor();
    this.traverseObjectlitLike(pattern, (m) -> owner);
}
Also used : Color(com.google.javascript.jscomp.colors.Color)

Example 54 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class RewriteAsyncFunctionsTest method testInnerSuperReference.

@Test
public void testInnerSuperReference() {
    test(externs(new TestExternsBuilder().addFunction().addJSCompLibraries().build()), srcs(lines("class A {", "  m() {", "    return this;", "  }", "}", "class X extends A {", "  async m() {", "    const tmp = super.m;", "    return tmp.call(null);", "  }", "}")), expected(lines("class A {", "  m() {", "    return this;", "  }", "}", "class X extends A {", "  m() {", "    const $jscomp$async$super$get$m = () => super.m;", "    return $jscomp.asyncExecutePromiseGeneratorFunction(", "        function* () {", "          const tmp = $jscomp$async$super$get$m();", // type of tmp will indicate it requires `this` be provided, but will allow null.
    "          return tmp.call(null);", "        });", "  }", "}")));
    // type of A.prototype.m
    Color classAPropertyMType = findClassDefinition(getLastCompiler(), "A").findMethodDefinition("m").getRootNode().getColor();
    Color classAInstanceType = getGlobalInstanceColor("A");
    CodeSubTree classXMethodMDefinition = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m");
    // Check type information on wrapper function for `super.m`
    ImmutableList<Node> superMethodWrapperNameNodes = classXMethodMDefinition.findMatchingQNameReferences("$jscomp$async$super$get$m");
    // one declaration and one reference
    assertThat(superMethodWrapperNameNodes).hasSize(2);
    // first name node is declaration
    // const $jscomp$async$super$get$m = () => super.m;
    Node wrapperDeclarationNameNode = superMethodWrapperNameNodes.get(0);
    // arrow function has a Color representing a object
    Node wrapperArrowFunction = wrapperDeclarationNameNode.getOnlyChild();
    assertNode(wrapperArrowFunction).isArrowFunction().hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
    // wrapper function variable has type matching the function itself
    Color wrapperArrowColor = wrapperArrowFunction.getColor();
    assertNode(wrapperDeclarationNameNode).hasColorThat().isEqualTo(wrapperArrowColor);
    // get `super.m` from `() => `super.m`
    Node superDotM = wrapperArrowFunction.getLastChild();
    assertNode(superDotM).matchesQualifiedName("super.m").hasColorThat().isEqualTo(classAPropertyMType);
    Node superNode = superDotM.getFirstChild();
    assertNode(superNode).hasColorThat().isEqualTo(classAInstanceType);
    // second name node is reference
    // const tmp = $jscomp$async$super$get$m();
    Node wrapperReferenceNameNode = superMethodWrapperNameNodes.get(1);
    // optimization colors don't track function signatures
    assertNode(wrapperReferenceNameNode).hasColorThat().isEqualTo(StandardColors.TOP_OBJECT);
    // `$jscomp$async$super$get$m()`
    Node wrapperCallNode = wrapperReferenceNameNode.getParent();
    assertNode(wrapperCallNode).isCall().hasColorThat().isEqualTo(classAPropertyMType);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) CodeSubTree(com.google.javascript.jscomp.testing.CodeSubTree) Test(org.junit.Test)

Example 55 with Color

use of com.google.javascript.jscomp.colors.Color in project closure-compiler by google.

the class RewriteAsyncFunctionsTest method testInnerArrowFunctionUsingArguments.

@Test
public void testInnerArrowFunctionUsingArguments() {
    test(externs(new TestExternsBuilder().addArguments().addJSCompLibraries().build()), srcs(lines("class X {", "  async m() {", "    return new Promise((resolve, reject) => {", "      return arguments;", "    });", "  }", "}")), expected(lines("class X {", "  m() {", "    const $jscomp$async$arguments = arguments;", "    return $jscomp.asyncExecutePromiseGeneratorFunction(", "        function* () {", "          return new Promise((resolve, reject) => {", "            return $jscomp$async$arguments", "          });", "        });", "  }", "}")));
    ImmutableList<Node> argumentsAliasRefs = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m").findMatchingQNameReferences("$jscomp$async$arguments");
    // one declaration and 1 use
    assertThat(argumentsAliasRefs).hasSize(2);
    Color argumentsColor = getGlobalColor(StandardColors.ARGUMENTS_ID);
    // declaration reference
    // const $jscomp$async$arguments = arguments;
    Node argumentsAliasDeclaration = argumentsAliasRefs.get(0);
    Node argumentsValue = argumentsAliasDeclaration.getOnlyChild();
    assertNode(argumentsValue).matchesQualifiedName("arguments").hasColorThat().isEqualTo(argumentsColor);
    assertNode(argumentsAliasDeclaration).matchesQualifiedName("$jscomp$async$arguments").hasColorThat().isEqualTo(argumentsColor);
    // usage reference
    // return $jscomp$async$arguments;
    Node argumentsAliasUsage = argumentsAliasRefs.get(1);
    assertNode(argumentsAliasUsage).matchesQualifiedName("$jscomp$async$arguments").hasColorThat().isEqualTo(argumentsColor);
}
Also used : NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Aggregations

Color (com.google.javascript.jscomp.colors.Color)57 Node (com.google.javascript.rhino.Node)37 Test (org.junit.Test)37 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)29 JSType (com.google.javascript.rhino.jstype.JSType)6 TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)5 ImmutableSet (com.google.common.collect.ImmutableSet)2 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 AbstractCompiler (com.google.javascript.jscomp.AbstractCompiler)1 CompilerPass (com.google.javascript.jscomp.CompilerPass)1 DefaultNameGenerator (com.google.javascript.jscomp.DefaultNameGenerator)1 GatherGetterAndSetterProperties (com.google.javascript.jscomp.GatherGetterAndSetterProperties)1 NameGenerator (com.google.javascript.jscomp.NameGenerator)1 NodeTraversal (com.google.javascript.jscomp.NodeTraversal)1 AbstractPostOrderCallback (com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback)1