Search in sources :

Example 16 with Color

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

the class Es6RewriteDestructuringTest method testObjectDestructuringDefaultValue_getsCorrectTypes.

@Test
public void testObjectDestructuringDefaultValue_getsCorrectTypes() {
    test(lines(// 
    "const obj = {a: 3, b: 'string', c: null};", "const {a = 4} = obj;"), lines("const obj = {a: 3, b: 'string', c: null};", "/** @const */ var $jscomp$destructuring$var0=obj;", "const a = $jscomp$destructuring$var0.a === void 0", "    ? 4: $jscomp$destructuring$var0.a;"));
    Node jsRoot = getLastCompiler().getJsRoot();
    Node aName = getNodeMatchingQName(jsRoot, "a");
    assertNode(aName).hasColorThat().isEqualTo(StandardColors.NUMBER);
    Color objType = getNodeMatchingQName(jsRoot, "obj").getColor();
    // `$jscomp$destructuring$var0` has the same type as `obj`
    assertThat(getAllNodesMatchingQName(jsRoot, "$jscomp$destructuring$var0").stream().map(Node::getColor).collect(Collectors.toSet())).containsExactly(objType);
}
Also used : NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 17 with Color

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

the class Es6RewriteDestructuringTest method testObjectDestructuringComputedPropWithDefault_getsCorrectTypes.

@Test
public void testObjectDestructuringComputedPropWithDefault_getsCorrectTypes() {
    test(lines(// 
    "const /** !Object<string, number> */ obj = {['a']: 3};", "const {['a']: a = 4} = obj;"), lines("const obj = {['a']: 3};", "/** @const */ var $jscomp$destructuring$var0=obj;", "var $jscomp$destructuring$var1 = $jscomp$destructuring$var0['a'];", "const a = $jscomp$destructuring$var1 === void 0", "    ? 4: $jscomp$destructuring$var1;"));
    Node jsRoot = getLastCompiler().getJsRoot();
    Node aName = getNodeMatchingQName(jsRoot, "a");
    assertNode(aName).hasColorThat().isEqualTo(StandardColors.NUMBER);
    // `$jscomp$destructuring$var0` has the same type as `obj`
    Node jscompDestructuringVar0Name = getNodeMatchingQName(jsRoot, "$jscomp$destructuring$var0");
    Color objType = jscompDestructuringVar0Name.getOnlyChild().getColor();
    assertThat(objType).isEqualTo(StandardColors.TOP_OBJECT);
    assertNode(jscompDestructuringVar0Name).hasColorThat().isEqualTo(objType);
    // `$jscomp$destructuring$var1` is typed as `number` (this is probably less important!)
    Node jscompDestructuringVar1Name = getNodeMatchingQName(jsRoot, "$jscomp$destructuring$var0");
    assertNode(jscompDestructuringVar1Name).hasColorThat().isEqualTo(objType);
}
Also used : NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 18 with Color

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

the class RuntimeLibraryTypedAstInjectionTest method testInjection_carriesReconciledTypeInformation.

@Test
public void testInjection_carriesReconciledTypeInformation() {
    ensureLibraryInjected("util/owns");
    test(srcs("Object;"), expected(lines(// 
    "/** @const */ var $jscomp = $jscomp || {};", "/** @const */ $jscomp.scope = {};", "$jscomp.owns = function(obj, prop) {", "  return Object.prototype.hasOwnProperty.call(obj, prop);", "};", "Object;")));
    ImmutableList<Node> objectNameNodes = findNodesNamed(this.getLastCompiler().getRoot(), "Object");
    assertThat(objectNameNodes).hasSize(2);
    Node injectedObjectNode = objectNameNodes.get(0);
    Node inferredObjectNode = objectNameNodes.get(1);
    assertThat(injectedObjectNode.getSourceFileName()).contains("util/owns");
    assertThat(inferredObjectNode.getSourceFileName()).contains("testcode");
    Color injectedObjectCtor = injectedObjectNode.getColor();
    Color inferredObjectCtor = inferredObjectNode.getColor();
    assertThat(injectedObjectCtor).isNotNull();
    assertThat(injectedObjectCtor).isSameInstanceAs(inferredObjectCtor);
}
Also used : NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Color(com.google.javascript.jscomp.colors.Color) Test(org.junit.Test)

Example 19 with Color

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

the class RewriteAsyncFunctionsTest method testInnerArrowFunctionUsingThis.

@Test
public void testInnerArrowFunctionUsingThis() {
    test(lines("class X {", "  async m() {", "    return new Promise((resolve, reject) => {", "      return this;", "    });", "  }", "}"), lines("class X {", "  m() {", "    const $jscomp$async$this = this;", "    return $jscomp.asyncExecutePromiseGeneratorFunction(", "        function* () {", "          return new Promise((resolve, reject) => {", "            return $jscomp$async$this;", "          });", "        });", "  }", "}"));
    Color classXInstanceType = getGlobalInstanceColor("X");
    ImmutableList<Node> thisAliasNameReferences = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m").findMatchingQNameReferences("$jscomp$async$this");
    assertThat(thisAliasNameReferences).hasSize(2);
    // const $jscomp$async$this = this;
    // confirm that `this` and `$jscomp$async$this` nodes have the right types in declaration
    Node aliasDeclarationReference = thisAliasNameReferences.get(0);
    assertNode(aliasDeclarationReference).hasColorThat().isEqualTo(classXInstanceType);
    Node thisNode = aliasDeclarationReference.getOnlyChild();
    assertNode(thisNode).isThis().hasColorThat().isEqualTo(classXInstanceType);
    // make sure the single reference to $jscomp$async$this has the right type
    assertNode(thisAliasNameReferences.get(1)).hasColorThat().isEqualTo(classXInstanceType);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) Test(org.junit.Test)

Example 20 with Color

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

the class RewriteAsyncFunctionsTest method testDefaultParameterUsingThis.

@Test
public void testDefaultParameterUsingThis() {
    test(lines("class X {", "  /**", "   * @param {number} a", "   */", "  constructor(a) {", "    /** @const */ this.a = a;", "  }", "  /**", "   * @param {number} b", "   * @return {!Promise<number>}", "   */", "  async m(b = this.a) {", "      return this.a + b;", "  }", "}"), lines("class X {", "  constructor(a) {", "    /** @const */ this.a = a;", "  }", // this in parameter default value doesn't get changed
    "  m(b = this.a) {", "    const $jscomp$async$this = this;", "    return $jscomp.asyncExecutePromiseGeneratorFunction(", "        function* () {", "            return $jscomp$async$this.a + b;", "        });", "  }", "}"));
    Color classXInstanceType = getGlobalInstanceColor("X");
    ImmutableList<Node> thisAliasNameReferences = findClassDefinition(getLastCompiler(), "X").findMethodDefinition("m").findMatchingQNameReferences("$jscomp$async$this");
    assertThat(thisAliasNameReferences).hasSize(2);
    // const $jscomp$async$this = this;
    // confirm that `this` and `$jscomp$async$this` nodes have the right types in declaration
    Node aliasDeclarationReference = thisAliasNameReferences.get(0);
    assertNode(aliasDeclarationReference).hasColorThat().isEqualTo(classXInstanceType);
    Node thisNode = aliasDeclarationReference.getOnlyChild();
    assertNode(thisNode).isThis().hasColorThat().isEqualTo(classXInstanceType);
    // make sure the single reference to $jscomp$async$this has the right type
    assertNode(thisAliasNameReferences.get(1)).hasColorThat().isEqualTo(classXInstanceType);
}
Also used : Color(com.google.javascript.jscomp.colors.Color) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Node(com.google.javascript.rhino.Node) 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