Ejemplo n.º 1
0
 /*
  * @see ASTVisitor#visit(ThisExpression)
  */
 public boolean visit(ThisExpression node) {
   if (node.getQualifier() != null) {
     node.getQualifier().accept(this);
     this.fBuffer.append("."); // $NON-NLS-1$
   }
   this.fBuffer.append("this"); // $NON-NLS-1$
   return false;
 }
  // TODO fix
  public void XtestThisExpression() throws Exception {
    IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("function foo() {\n");
    buf.append("    return this;\n");
    buf.append("    return Outer.this;\n");
    buf.append("}\n");
    IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null);

    JavaScriptUnit astRoot = createAST(cu);
    ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());

    AST ast = astRoot.getAST();

    assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);

    FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo");
    Block block = methodDecl.getBody();
    List statements = block.statements();
    assertTrue("Number of statements not 2", statements.size() == 2);
    { // add qualifier
      ReturnStatement returnStatement = (ReturnStatement) statements.get(0);

      ThisExpression thisExpression = (ThisExpression) returnStatement.getExpression();

      SimpleName newExpression = ast.newSimpleName("X");
      rewrite.set(thisExpression, ThisExpression.QUALIFIER_PROPERTY, newExpression, null);
    }
    { // remove qualifier
      ReturnStatement returnStatement = (ReturnStatement) statements.get(1);

      ThisExpression thisExpression = (ThisExpression) returnStatement.getExpression();

      rewrite.remove(thisExpression.getQualifier(), null);
    }

    String preview = evaluateRewrite(cu, rewrite);

    buf = new StringBuffer();
    buf.append("    function foo() {\n");
    buf.append("        return X.this;\n");
    buf.append("        return this;\n");
    buf.append("    }\n");
    assertEqualString(preview, buf.toString());
  }