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