reimplemented extension literal definition and calls to comply with jquery conventions

This commit is contained in:
Pavel Talanov
2012-01-27 19:43:41 +04:00
parent 32484eb784
commit 626a84a935
5 changed files with 49 additions and 12 deletions
@@ -103,8 +103,9 @@ public final class StandardClasses {
private static void declareJQuery(@NotNull StandardClasses standardClasses) {
standardClasses.declare().forFQ("jquery.JQuery").externalClass("jQuery")
.methods("addClass", "attr", "hasClass", "append");
.methods("addClass", "attr", "hasClass", "append", "text", "ready");
standardClasses.declare().forFQ("jquery.jq").externalFunction("jQuery");
standardClasses.declare().forFQ("jquery.get-document").externalObject("document");
}
//TODO: test all the methods
@@ -142,6 +143,8 @@ public final class StandardClasses {
standardClasses.declare().forFQ("jet.String").kotlinClass("String").
properties("length");
standardClasses.declare().forFQ("jet.Any.toString").kotlinFunction("toString");
}
private static void declareTopLevelFunctions(@NotNull StandardClasses standardClasses) {
@@ -1,5 +1,6 @@
package org.jetbrains.k2js.translate.expression;
import com.google.dart.compiler.backend.js.ast.*;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
@@ -195,7 +196,7 @@ public final class FunctionTranslator extends AbstractTranslator {
}
private boolean isExtensionFunction() {
return DescriptorUtils.isExtensionFunction(descriptor);
return DescriptorUtils.isExtensionFunction(descriptor) && !isLiteral();
}
private boolean isLiteral() {
@@ -2,6 +2,7 @@ package org.jetbrains.k2js.translate.reference;
import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsInvocation;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.backend.js.ast.JsNew;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
@@ -129,7 +130,6 @@ public final class CallTranslator extends AbstractTranslator {
public static JsExpression translate(@Nullable JsExpression receiver,
@NotNull CallableDescriptor descriptor,
@NotNull TranslationContext context) {
//TODO: HACK!
return translate(receiver, Collections.<JsExpression>emptyList(),
ResolvedCallImpl.create(descriptor), null, context);
}
@@ -224,12 +224,37 @@ public final class CallTranslator extends AbstractTranslator {
if (isConstructor()) {
return constructorCall();
}
if (isExtensionFunctionLiteral()) {
return extensionFunctionLiteralCall();
}
if (isExtensionFunction()) {
return extensionFunctionCall();
}
return methodCall();
}
@NotNull
private JsExpression extensionFunctionLiteralCall() {
List<JsExpression> callArguments = new ArrayList<JsExpression>();
assert receiver != null;
callArguments.add(thisObject());
callArguments.addAll(arguments);
receiver = null;
JsNameRef callMethodNameRef = AstUtil.newQualifiedNameRef("call");
JsInvocation callMethodInvocation = new JsInvocation();
callMethodInvocation.setQualifier(callMethodNameRef);
AstUtil.setQualifier(callMethodInvocation, calleeReference());
callMethodInvocation.setArguments(callArguments);
return callMethodInvocation;
}
private boolean isExtensionFunctionLiteral() {
boolean isLiteral = descriptor instanceof VariableAsFunctionDescriptor
|| descriptor instanceof ExpressionAsFunctionDescriptor;
return isExtensionFunction() && isLiteral;
}
@NotNull
private JsExpression extensionFunctionCall() {
receiver = getExtensionFunctionCallReceiver();
@@ -257,8 +282,10 @@ public final class CallTranslator extends AbstractTranslator {
return getThisObject(context(), expectedReceiverDescriptor);
}
@SuppressWarnings("UnnecessaryLocalVariable")
private boolean isExtensionFunction() {
return resolvedCall.getReceiverArgument().exists();
boolean hasReceiver = resolvedCall.getReceiverArgument().exists();
return hasReceiver;
}
@NotNull
@@ -1,5 +1,6 @@
package org.jetbrains.k2js.utils;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
@@ -10,6 +11,7 @@ import com.intellij.psi.impl.PsiFileFactoryImpl;
import com.intellij.testFramework.LightVirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.JetCoreEnvironment;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.JetLanguage;
@@ -21,13 +23,13 @@ import java.io.IOException;
*/
public final class JetFileUtils {
// @NotNull
// private static JetCoreEnvironment testOnlyEnvironment = new JetCoreEnvironment(new Disposable() {
//
// @Override
// public void dispose() {
// }
// });
@NotNull
private static JetCoreEnvironment testOnlyEnvironment = new JetCoreEnvironment(new Disposable() {
@Override
public void dispose() {
}
});
@NotNull
public static String loadFile(@NotNull String path) throws IOException {
@@ -63,7 +65,8 @@ public final class JetFileUtils {
virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
Project realProject = project;
if (realProject == null) {
throw new RuntimeException();
realProject = testOnlyEnvironment.getProject();
//throw new RuntimeException();
}
PsiFile result = ((PsiFileFactoryImpl) PsiFileFactory.getInstance(realProject))
.trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
+3
View File
@@ -902,6 +902,9 @@ Kotlin.StringBuilder = Kotlin.Class.create(
}
);
Kotlin.toString = function(obj) {
return obj.toString();
};
/**
* Copyright 2010 Tim Down.