Search in sources :

Example 56 with TestExternsBuilder

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

Example 57 with TestExternsBuilder

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");
}
Also used : FunctionType(com.google.javascript.rhino.jstype.FunctionType) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 58 with TestExternsBuilder

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));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 59 with TestExternsBuilder

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() {},", "  }", "});", "")));
}
Also used : TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Example 60 with TestExternsBuilder

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);
}
Also used : CompilerOptions(com.google.javascript.jscomp.CompilerOptions) SourceFile(com.google.javascript.jscomp.SourceFile) TestExternsBuilder(com.google.javascript.jscomp.testing.TestExternsBuilder) Test(org.junit.Test)

Aggregations

TestExternsBuilder (com.google.javascript.jscomp.testing.TestExternsBuilder)107 Test (org.junit.Test)106 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)58 Node (com.google.javascript.rhino.Node)17 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)17 Compiler (com.google.javascript.jscomp.Compiler)8 SourceFile (com.google.javascript.jscomp.SourceFile)7 Color (com.google.javascript.jscomp.colors.Color)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 CodeSubTree (com.google.javascript.jscomp.testing.CodeSubTree)2 ImmutableList (com.google.common.collect.ImmutableList)1 NoninjectingCompiler (com.google.javascript.jscomp.testing.NoninjectingCompiler)1 ClosureReverseAbstractInterpreter (com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter)1 SemanticReverseAbstractInterpreter (com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 File (java.io.File)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1