use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class Es6ConvertSuperTest method testSynthesizingConstructorOfDerivedClassInSource.
@Test
public void testSynthesizingConstructorOfDerivedClassInSource() {
test(externs(new TestExternsBuilder().addArguments().build()), srcs(lines(// Force wrapping.
"class A {", " constructor() { }", "}", "", "class B extends A { }")), expected(lines("class A {", " constructor() { }", "}", "", "class B extends A {", " constructor() { super(...arguments); }", "}")));
// class A { ... }
Node classANode = findClassDefinition(getLastCompiler(), "A").getRootNode();
Color classAConstructorType = classANode.getColor();
// class B extends A { ... }
Node classBNode = findClassDefinition(getLastCompiler(), "B").getRootNode();
Color classBConstructorType = classBNode.getColor();
Color classBInstanceType = Color.createUnion(classBConstructorType.getInstanceColors());
// constructor() { }
Node constructorMemberFunctionDefForB = classBNode.getLastChild().getFirstChild();
assertNode(constructorMemberFunctionDefForB).isMemberFunctionDef("constructor").hasLineno(// synthetic constructor gets position and length of original class definition
5).hasCharno(0).isIndexable(false);
assertNode(constructorMemberFunctionDefForB).hasColorThat().isEqualTo(classBConstructorType);
Node constructorFunctionForB = constructorMemberFunctionDefForB.getOnlyChild();
assertNode(constructorFunctionForB).hasToken(Token.FUNCTION).hasLineno(// synthetic constructor gets position and length of original class definition
5).hasCharno(0).isIndexable(false);
assertNode(constructorFunctionForB).hasColorThat().isEqualTo(classBConstructorType);
// super(...arguments)
Node superConstructorCall = constructorFunctionForB.getLastChild().getFirstChild().getOnlyChild();
assertNode(superConstructorCall).hasToken(Token.CALL).hasLineno(// synthetic constructor gets position and length of original class definition
5).hasCharno(0).isIndexable(false);
assertNode(superConstructorCall).hasColorThat().isEqualTo(classBInstanceType);
Node superNode = superConstructorCall.getFirstChild();
assertNode(superNode).hasToken(Token.SUPER).hasLineno(// synthetic constructor gets position and length of original class definition
5).hasCharno(0).isIndexable(false);
assertNode(superNode).hasColorThat().isEqualTo(classAConstructorType);
Node argumentsNode = superNode.getNext().getOnlyChild();
assertNode(argumentsNode).isName("arguments").hasLineno(// synthetic constructor gets position and length of original class definition
5).hasCharno(0).isIndexable(false);
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class TypedScopeCreatorTest method testGoogModule_requireShadowsGlobal_usedInExtendsClause.
@Test
public void testGoogModule_requireShadowsGlobal_usedInExtendsClause() {
processClosurePrimitives = true;
testSame(externs(new TestExternsBuilder().addClosureExterns().build(), "/** @interface */ function EventTarget() {}"), srcs(lines("goog.provide('goog.events.EventTarget');", "/** @constructor */", "goog.events.EventTarget = function() {};"), lines(//
"goog.module('mod.A');", "const EventTarget = goog.require('goog.events.EventTarget');", "class Proxy extends EventTarget {}", "PROXY: Proxy;")));
FunctionType proxyType = getLabeledStatement("PROXY").statementNode.getOnlyChild().getJSType().toMaybeFunctionType();
assertType(proxyType.getSuperClassConstructor()).isFunctionTypeThat().isConstructorFor("goog.events.EventTarget");
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class AmbiguatePropertiesTest method testPredeclaredType.
@Test
public void testPredeclaredType() {
this.enableClosurePass();
this.enableRewriteClosureProvides();
String js = lines("goog.forwardDeclare('goog.Foo');", "/** @constructor */ ", "function A() {", " this.x = 3;", "}", "/** @param {goog.Foo} x */", "function f(x) { x.y = 4; }");
String result = lines("/** @constructor */ ", "function A() {", " this.a = 3;", "}", "/** @param {goog.Foo} x */", "function f(x) { x.y = 4; }");
test(externs(EXTERNS + new TestExternsBuilder().addClosureExterns().build()), srcs(js), expected(result));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class Es6RewriteClassTest method testClassComputedPropGetterAndSetter_mixedWithOtherMembers.
@Test
public void testClassComputedPropGetterAndSetter_mixedWithOtherMembers() {
setLanguageOut(LanguageMode.ECMASCRIPT5);
test(externs(new TestExternsBuilder().addObject().addReflect().addJSCompLibraries().addExtra(lines(//
"function one() {}", "function two() {}", "function three() {}")).build()), srcs(lines("/** @unrestricted */", "class C {", " get [one()]() { return true; }", " get a() {}", " [two()]() {}", " get b() {}", " c() {}", " set [three()](val) { return true; }", "}")), expected(lines("/** @constructor */", "let C = function() {};", "$jscomp.global.Object.defineProperty(C.prototype, one(), {", " configurable: true,", " enumerable: true,", " get: function() {", " return true", " },", "});", "C.prototype[two()] = function() {};", "C.prototype.c = function() {};", "$jscomp.global.Object.defineProperty(C.prototype, three(), {", " configurable: true,", " enumerable: true,", " set: function(val) {", " return true", " },", "});", "$jscomp.global.Object.defineProperties(C.prototype, {", " a: {", " configurable: true,", " enumerable: true,", " get: function() {},", " },", " b: {", " configurable: true,", " enumerable: true,", " get: function() {},", " }", "});", "")));
}
use of com.google.javascript.jscomp.testing.TestExternsBuilder in project closure-compiler by google.
the class ClosureIntegrationTest method testMalformedGoogModulesGracefullyError.
@Test
public void testMalformedGoogModulesGracefullyError() {
CompilerOptions options = createCompilerOptions();
options.setClosurePass(true);
externs = ImmutableList.<SourceFile>builder().addAll(externs).add(SourceFile.fromCode("closure_externs.js", new TestExternsBuilder().addClosureExterns().build())).build();
test(options, lines(//
"goog.module('m');", "var x;", "goog.module.declareLegacyNamespace();"), DiagnosticGroups.MALFORMED_GOOG_MODULE);
test(options, "var x; goog.module('m');", DiagnosticGroups.MALFORMED_GOOG_MODULE);
}
Aggregations