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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations