[KT-4124] Add diagnostic that reports of local classes. Remove test for diagnostic of nested types

This commit is contained in:
Alexey Andreev
2016-02-12 15:55:18 +03:00
parent 8537d327fa
commit e3c7cd0021
9 changed files with 95 additions and 493 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.container.StorageComponentContainer
import org.jetbrains.kotlin.container.useImpl
import org.jetbrains.kotlin.container.useInstance
import org.jetbrains.kotlin.js.resolve.diagnostics.JsCallChecker
import org.jetbrains.kotlin.js.resolve.diagnostics.LocalClassChecker
import org.jetbrains.kotlin.resolve.IdentifierChecker
import org.jetbrains.kotlin.resolve.OverloadFilter
import org.jetbrains.kotlin.resolve.PlatformConfigurator
@@ -28,7 +29,7 @@ import org.jetbrains.kotlin.types.DynamicTypesAllowed
object JsPlatformConfigurator : PlatformConfigurator(
DynamicTypesAllowed(),
additionalDeclarationCheckers = listOf(NativeInvokeChecker(), NativeGetterChecker(), NativeSetterChecker()),
additionalDeclarationCheckers = listOf(NativeInvokeChecker(), NativeGetterChecker(), NativeSetterChecker(), LocalClassChecker()),
additionalCallCheckers = listOf(),
additionalTypeCheckers = listOf(),
additionalSymbolUsageValidators = listOf(),
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.resolve.diagnostics
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.rendering.renderKind
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DeclarationChecker
import org.jetbrains.kotlin.resolve.DescriptorUtils.*
class LocalClassChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, diagnosticHolder: DiagnosticSink,
bindingContext: BindingContext) {
if (descriptor !is ClassDescriptor || declaration !is KtNamedDeclaration) {
return;
}
if (isAnonymousObject(descriptor) || isObject(descriptor)) {
return
}
// hack to avoid to get diagnostics when compile kotlin builtins
val fqNameUnsafe = getFqName(descriptor)
if (fqNameUnsafe.asString().startsWith("kotlin.")) {
return
}
if (hasEnclosingFunction(descriptor)) {
diagnosticHolder.report(ErrorsJs.NON_TOPLEVEL_CLASS_DECLARATION.on(declaration, descriptor.renderKind()))
}
}
private fun hasEnclosingFunction(descriptor: DeclarationDescriptor?): Boolean {
var d = descriptor
while (d != null) {
if (d is FunctionDescriptor) {
return true
}
d = d.containingDeclaration
}
return false
}
}
@@ -545,20 +545,22 @@ public final class StaticContext {
@Nullable
@Override
public JsExpression apply(@NotNull DeclarationDescriptor descriptor) {
if (isNativeObject(descriptor) || isBuiltin(descriptor)) {
return null;
}
if (!(descriptor instanceof ClassDescriptor)) {
return null;
}
ClassDescriptor cls = (ClassDescriptor) descriptor;
if (cls.getKind() == ClassKind.ENUM_ENTRY || cls.getKind() == ClassKind.OBJECT) {
return null;
}
DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (container == null) {
return null;
}
if (isNativeObject(descriptor) || isBuiltin(descriptor)) {
return null;
}
ClassDescriptor cls = (ClassDescriptor) descriptor;
if (cls.getKind() == ClassKind.ENUM_ENTRY || cls.getKind() == ClassKind.OBJECT) {
return null;
}
JsExpression result = getQualifiedReference(container);
if (DescriptorUtils.isCompanionObject(container)) {
result = Namer.getCompanionObjectAccessor(result);
@@ -29,4 +29,4 @@ fun get(value: Int): Int {
invocationCount++
return value
}
var invocationCount = 0
var invocationCount = 0
+19 -18
View File
@@ -119,14 +119,14 @@ var Kotlin = {};
}
if (typeof staticProperties !== 'undefined') {
for (p in staticProperties) {
if (!staticProperties.hasOwnProperty(p)) {
continue;
}
//noinspection JSUnfilteredForInLoop
property = staticProperties[p];
if (typeof property === "function" && typeof property.type !== "undefined" && property.type === Kotlin.TYPE.INIT_FUN) {
if (typeof property === "function" && property.type === Kotlin.TYPE.INIT_FUN) {
//noinspection JSUnfilteredForInLoop
metadata.types[p] = property;
}
else {
//noinspection JSUnfilteredForInLoop
metadata.staticMembers[p] = property;
}
}
@@ -196,14 +196,15 @@ var Kotlin = {};
Kotlin.defineInnerTypes = function(constructor, types) {
for (var innerTypeName in types) {
if (types.hasOwnProperty(innerTypeName)) {
var innerType = types[innerTypeName];
innerType.className = innerTypeName;
Object.defineProperty(constructor, innerTypeName, {
get: innerType,
configurable: true
});
}
// since types object does not inherit from anything, it's just a map
//noinspection JSUnfilteredForInLoop
var innerType = types[innerTypeName];
innerType.className = innerTypeName;
//noinspection JSUnfilteredForInLoop
Object.defineProperty(constructor, innerTypeName, {
get: innerType,
configurable: true
});
}
};
@@ -213,12 +214,12 @@ var Kotlin = {};
var obj = new noNameClass();
noNameClass.$metadata$.type = Kotlin.TYPE.OBJECT;
for (var innerTypeName in metadata.types) {
if (metadata.types.hasOwnProperty(innerTypeName)) {
Object.defineProperty(obj, innerTypeName, {
get : metadata.types[innerTypeName],
configurable: true
});
}
// since types object does not inherit from anything, it's just a map
//noinspection JSUnfilteredForInLoop
Object.defineProperty(obj, innerTypeName, {
get : metadata.types[innerTypeName],
configurable: true
});
}
return obj;
};