refactor generators to inner classes
This commit is contained in:
@@ -21,7 +21,7 @@ public abstract class Config {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private static final String PATH_TO_JS_LIB_SRC = getPathToJsLibSrc();
|
private static final String PATH_TO_JS_LIB_SRC = getPathToJsLibSrc();
|
||||||
|
|
||||||
//TODO: provide some generic way to access
|
//TODO: provide some generic way to get the files of the project
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final List<String> LIB_FILE_NAMES = Arrays.asList(
|
private static final List<String> LIB_FILE_NAMES = Arrays.asList(
|
||||||
PATH_TO_JS_LIB_SRC + "\\core\\annotations.kt",
|
PATH_TO_JS_LIB_SRC + "\\core\\annotations.kt",
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.jetbrains.k2js.translate.context;
|
|||||||
import com.google.dart.compiler.backend.js.ast.JsName;
|
import com.google.dart.compiler.backend.js.ast.JsName;
|
||||||
import com.google.dart.compiler.backend.js.ast.JsScope;
|
import com.google.dart.compiler.backend.js.ast.JsScope;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
@@ -12,29 +11,25 @@ public final class NamingScope {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static NamingScope rootScope(@NotNull JsScope rootScope) {
|
public static NamingScope rootScope(@NotNull JsScope rootScope) {
|
||||||
return new NamingScope(rootScope, null);
|
return new NamingScope(rootScope);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final JsScope scope;
|
private final JsScope scope;
|
||||||
|
|
||||||
@Nullable
|
private NamingScope(@NotNull JsScope correspondingScope) {
|
||||||
private final NamingScope parent;
|
|
||||||
|
|
||||||
private NamingScope(@NotNull JsScope correspondingScope, @Nullable NamingScope parent) {
|
|
||||||
this.scope = correspondingScope;
|
this.scope = correspondingScope;
|
||||||
this.parent = parent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public NamingScope innerScope(@NotNull String scopeName) {
|
public NamingScope innerScope(@NotNull String scopeName) {
|
||||||
JsScope innerJsScope = new JsScope(jsScope(), scopeName);
|
JsScope innerJsScope = new JsScope(jsScope(), scopeName);
|
||||||
return new NamingScope(innerJsScope, this);
|
return new NamingScope(innerJsScope);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public NamingScope innerScope(@NotNull JsScope correspondingScope) {
|
public NamingScope innerScope(@NotNull JsScope correspondingScope) {
|
||||||
return new NamingScope(correspondingScope, this);
|
return new NamingScope(correspondingScope);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -56,11 +56,11 @@ public class StaticContext {
|
|||||||
private final NamingScope rootScope;
|
private final NamingScope rootScope;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Generator<JsName> names = nameGenerator();
|
private final Generator<JsName> names = new NameGenerator();
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Generator<NamingScope> scopes = scopeGenerator();
|
private final Generator<NamingScope> scopes = new ScopeGenerator();
|
||||||
@NotNull
|
@NotNull
|
||||||
private final Generator<JsNameRef> qualifiers = qualifierGenerator();
|
private final Generator<JsNameRef> qualifiers = new QualifierGenerator();
|
||||||
|
|
||||||
|
|
||||||
//TODO: too many parameters in constructor
|
//TODO: too many parameters in constructor
|
||||||
@@ -112,10 +112,33 @@ public class StaticContext {
|
|||||||
return standardClasses;
|
return standardClasses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
||||||
|
NamingScope namingScope = scopes.get(descriptor);
|
||||||
|
assert namingScope != null : "Must have a scope for descriptor";
|
||||||
|
return namingScope;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public JsNameRef getQualifierForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
||||||
|
//TODO: hack!
|
||||||
|
if (AnnotationsUtils.isNativeObject(descriptor)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return qualifiers.get(descriptor.getOriginal());
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private Generator<JsName> nameGenerator() {
|
public JsName getNameForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
||||||
|
JsName name = names.get(descriptor.getOriginal());
|
||||||
|
assert name != null : "Must have name for descriptor";
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private class NameGenerator extends Generator<JsName> {
|
||||||
|
public NameGenerator() {
|
||||||
Rule<JsName> aliasOverridesNames = new Rule<JsName>() {
|
Rule<JsName> aliasOverridesNames = new Rule<JsName>() {
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -251,18 +274,17 @@ public class StaticContext {
|
|||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Generator<JsName> nameGenerator = new Generator<JsName>();
|
addRule(namesForStandardClasses);
|
||||||
nameGenerator.addRule(namesForStandardClasses);
|
addRule(aliasOverridesNames);
|
||||||
nameGenerator.addRule(aliasOverridesNames);
|
addRule(constructorHasTheSameNameAsTheClass);
|
||||||
nameGenerator.addRule(constructorHasTheSameNameAsTheClass);
|
addRule(namesAnnotatedAsLibraryHasUnobfuscatableNames);
|
||||||
nameGenerator.addRule(namesAnnotatedAsLibraryHasUnobfuscatableNames);
|
addRule(namesForNativeObjectsAreUnobfuscatable);
|
||||||
nameGenerator.addRule(namesForNativeObjectsAreUnobfuscatable);
|
addRule(toStringHack);
|
||||||
nameGenerator.addRule(toStringHack);
|
addRule(propertiesCorrespondToSpeciallyTreatedBackingFieldNames);
|
||||||
nameGenerator.addRule(propertiesCorrespondToSpeciallyTreatedBackingFieldNames);
|
addRule(namespacesShouldBeDefinedInRootScope);
|
||||||
nameGenerator.addRule(namespacesShouldBeDefinedInRootScope);
|
addRule(accessorsHasNamesWithSpecialPrefixes);
|
||||||
nameGenerator.addRule(accessorsHasNamesWithSpecialPrefixes);
|
addRule(memberDeclarationsInsideParentsScope);
|
||||||
nameGenerator.addRule(memberDeclarationsInsideParentsScope);
|
}
|
||||||
return nameGenerator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private NamingScope getEnclosingScope(@NotNull DeclarationDescriptor descriptor) {
|
private NamingScope getEnclosingScope(@NotNull DeclarationDescriptor descriptor) {
|
||||||
@@ -270,9 +292,10 @@ public class StaticContext {
|
|||||||
return getScopeForDescriptor(containingDeclaration.getOriginal());
|
return getScopeForDescriptor(containingDeclaration.getOriginal());
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private Generator<NamingScope> scopeGenerator() {
|
private class ScopeGenerator extends Generator<NamingScope> {
|
||||||
Generator<NamingScope> scopeGenerator = new Generator<NamingScope>();
|
|
||||||
|
public ScopeGenerator() {
|
||||||
Rule<NamingScope> generateNewScopesForNamespaceDescriptors = new Rule<NamingScope>() {
|
Rule<NamingScope> generateNewScopesForNamespaceDescriptors = new Rule<NamingScope>() {
|
||||||
@Override
|
@Override
|
||||||
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
|
public NamingScope apply(@NotNull DeclarationDescriptor descriptor) {
|
||||||
@@ -289,15 +312,15 @@ public class StaticContext {
|
|||||||
return enclosingScope.innerScope("scope for member " + descriptor.getName());
|
return enclosingScope.innerScope("scope for member " + descriptor.getName());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
scopeGenerator.addRule(generateNewScopesForNamespaceDescriptors);
|
addRule(generateNewScopesForNamespaceDescriptors);
|
||||||
scopeGenerator.addRule(generateInnerScopesForMembers);
|
addRule(generateInnerScopesForMembers);
|
||||||
|
}
|
||||||
|
|
||||||
return scopeGenerator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
private class QualifierGenerator extends Generator<JsNameRef> {
|
||||||
private Generator<JsNameRef> qualifierGenerator() {
|
public QualifierGenerator() {
|
||||||
Rule<JsNameRef> namespacesHaveNoQualifiers = new Rule<JsNameRef>() {
|
Rule<JsNameRef> namespacesHaveNoQualifiers = new Rule<JsNameRef>() {
|
||||||
@Override
|
@Override
|
||||||
public JsNameRef apply(@NotNull DeclarationDescriptor descriptor) {
|
public JsNameRef apply(@NotNull DeclarationDescriptor descriptor) {
|
||||||
@@ -356,38 +379,11 @@ public class StaticContext {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Generator<JsNameRef> qualifierGenerator = new Generator<JsNameRef>();
|
addRule(libraryObjectsHaveKotlinQualifier);
|
||||||
qualifierGenerator.addRule(libraryObjectsHaveKotlinQualifier);
|
addRule(membersOfAnnotatedClassesHaveKotlinQualifier);
|
||||||
qualifierGenerator.addRule(membersOfAnnotatedClassesHaveKotlinQualifier);
|
addRule(constructorHaveTheSameQualifierAsTheClass);
|
||||||
qualifierGenerator.addRule(constructorHaveTheSameQualifierAsTheClass);
|
addRule(namespacesHaveNoQualifiers);
|
||||||
qualifierGenerator.addRule(namespacesHaveNoQualifiers);
|
addRule(namespaceLevelDeclarationsHaveEnclosingNamespacesNamesAsQualifier);
|
||||||
qualifierGenerator.addRule(namespaceLevelDeclarationsHaveEnclosingNamespacesNamesAsQualifier);
|
|
||||||
return qualifierGenerator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public NamingScope getScopeForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
|
||||||
NamingScope namingScope = scopes.get(descriptor);
|
|
||||||
assert namingScope != null : "Must have a scope for descriptor";
|
|
||||||
return namingScope;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public JsNameRef getQualifierForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
|
||||||
//TODO: hack!
|
|
||||||
if (AnnotationsUtils.isNativeObject(descriptor)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return qualifiers.get(descriptor.getOriginal());
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public JsName getNameForDescriptor(@NotNull DeclarationDescriptor descriptor) {
|
|
||||||
JsName name = names.get(descriptor.getOriginal());
|
|
||||||
assert name != null : "Must have name for descriptor";
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,7 +12,7 @@ import java.util.Map;
|
|||||||
/**
|
/**
|
||||||
* @author Pavel Talanov
|
* @author Pavel Talanov
|
||||||
*/
|
*/
|
||||||
public final class Generator<V> {
|
public class Generator<V> {
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
Reference in New Issue
Block a user