Search in sources :

Example 46 with JSType

use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.

the class AstFactoryTest method testCreateEmptyFunction.

@Test
public void testCreateEmptyFunction() {
    AstFactory astFactory = createTestAstFactory();
    // just a quick way to get a valid function type
    Node root = parseAndAddTypes("function foo() {}");
    JSType functionType = // script
    root.getFirstChild().getFirstChild().getJSType();
    Node emptyFunction = astFactory.createEmptyFunction(type(functionType));
    assertNode(emptyFunction).hasToken(Token.FUNCTION);
    assertType(emptyFunction.getJSType()).isEqualTo(functionType);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Test(org.junit.Test)

Example 47 with JSType

use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.

the class AstFactoryTest method testCreateObjectLit_empty.

@Test
public void testCreateObjectLit_empty() {
    AstFactory astFactory = createTestAstFactory();
    // just a quick way to get a valid object literal type
    Node root = parseAndAddTypes("({})");
    JSType objectLitType = // script
    root.getFirstChild().getFirstChild().getOnlyChild().getJSType();
    Node objectLit = astFactory.createObjectLit();
    assertType(objectLit.getJSType()).toStringIsEqualTo("{}");
    assertThat(objectLit.getJSType()).isInstanceOf(objectLitType.getClass());
    assertNode(objectLit).hasToken(Token.OBJECTLIT);
    assertNode(objectLit).hasChildren(false);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Test(org.junit.Test)

Example 48 with JSType

use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.

the class PartialCompilationTest method testUnresolvedBaseClassDoesNotHideFields.

@Test
public void testUnresolvedBaseClassDoesNotHideFields() throws Exception {
    assertPartialCompilationSucceeds("/** @constructor @extends {MissingBase} */", "var Klass = function () {", "  /** @type {string} */", "  this.foo;", "};");
    TypedVar x = compiler.getTopScope().getSlot("Klass");
    JSType type = x.getType();
    assertThat(type.isFunctionType()).isTrue();
    FunctionType fType = (FunctionType) type;
    assertThat(fType.getTypeOfThis().hasProperty("foo")).isTrue();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType) Test(org.junit.Test)

Example 49 with JSType

use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.

the class FunctionInjector method inlineReturnValue.

/**
 * Inline a function that fulfills the requirements of canInlineReferenceDirectly into the call
 * site, replacing only the CALL node.
 */
private Node inlineReturnValue(Reference ref, Node fnNode) {
    Node callNode = ref.callNode;
    Node block = fnNode.getLastChild();
    // NOTE: As the normalize pass guarantees globals aren't being
    // shadowed and an expression can't introduce new names, there is
    // no need to check for conflicts.
    // Create an argName -> expression map, checking for side effects.
    Map<String, Node> argMap = functionArgumentInjector.getFunctionCallParameterMap(fnNode, callNode, this.safeNameIdSupplier);
    Node newExpression;
    if (!block.hasChildren()) {
        Node srcLocation = block;
        newExpression = NodeUtil.newUndefinedNode(srcLocation);
    } else {
        Node returnNode = block.getFirstChild();
        checkArgument(returnNode.isReturn(), returnNode);
        // Clone the return node first.
        Node safeReturnNode = returnNode.cloneTree();
        Node inlineResult = functionArgumentInjector.inject(null, safeReturnNode, null, argMap);
        checkArgument(safeReturnNode == inlineResult);
        newExpression = safeReturnNode.removeFirstChild();
        NodeUtil.markNewScopesChanged(newExpression, compiler);
    }
    // If the call site had a cast ensure it's persisted to the new expression that replaces it.
    JSType typeBeforeCast = callNode.getJSTypeBeforeCast();
    if (typeBeforeCast != null) {
        newExpression.setJSTypeBeforeCast(typeBeforeCast);
        newExpression.setJSType(callNode.getJSType());
    }
    // though)
    if (callNode.getColor() != null && callNode.isColorFromTypeCast()) {
        newExpression.setColor(callNode.getColor());
        newExpression.setColorFromTypeCast();
    }
    callNode.replaceWith(newExpression);
    NodeUtil.markFunctionsDeleted(callNode, compiler);
    return newExpression;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node)

Example 50 with JSType

use of com.google.javascript.rhino.jstype.JSType in project closure-compiler by google.

the class GenerateExports method addExtern.

private void addExtern(String export) {
    Node objectPrototype = NodeUtil.newQName(compiler, "Object.prototype");
    JSType objCtor = compiler.getTypeRegistry().getNativeType(JSTypeNative.OBJECT_FUNCTION_TYPE);
    objectPrototype.getFirstChild().setJSType(objCtor);
    Node propstmt = IR.exprResult(IR.getprop(objectPrototype, export));
    propstmt.srcrefTree(getSynthesizedExternsRoot());
    propstmt.setOriginalName(export);
    getSynthesizedExternsRoot().addChildToBack(propstmt);
    compiler.reportChangeToEnclosingScope(propstmt);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node)

Aggregations

JSType (com.google.javascript.rhino.jstype.JSType)447 Test (org.junit.Test)182 Node (com.google.javascript.rhino.Node)158 FunctionType (com.google.javascript.rhino.jstype.FunctionType)71 ObjectType (com.google.javascript.rhino.jstype.ObjectType)71 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)30 JSDocInfo (com.google.javascript.rhino.JSDocInfo)19 TemplateType (com.google.javascript.rhino.jstype.TemplateType)14 FlowScope (com.google.javascript.jscomp.type.FlowScope)13 CheckReturnValue (com.google.errorprone.annotations.CheckReturnValue)11 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)9 UnionType (com.google.javascript.rhino.jstype.UnionType)9 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)8 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)8 LinkedHashMap (java.util.LinkedHashMap)8 ImmutableList (com.google.common.collect.ImmutableList)7 Color (com.google.javascript.jscomp.colors.Color)7 StaticTypedSlot (com.google.javascript.rhino.jstype.StaticTypedSlot)7 ArrayList (java.util.ArrayList)7 ImmutableMap (com.google.common.collect.ImmutableMap)6