Do not include annotations and KDoc into NO_EXPLICIT_VISIBILITY_IN_API_MODE report range

Rename positioning strategy to better reflect its behavior.
#KT-36021 fixed.
This commit is contained in:
Leonid Startsev
2020-01-22 16:39:18 +03:00
parent 4285c6045a
commit 43858e8169
9 changed files with 112 additions and 18 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -208,9 +208,10 @@ public interface Errors {
DiagnosticFactory2<PsiElement, KtModifierKeywordToken, String> DEPRECATED_MODIFIER_FOR_TARGET = DiagnosticFactory2.create(WARNING);
DiagnosticFactory2<PsiElement, KtModifierKeywordToken, KtModifierKeywordToken> DEPRECATED_MODIFIER = DiagnosticFactory2.create(WARNING);
DiagnosticFactory2<PsiElement, KtModifierKeywordToken, String> REDUNDANT_MODIFIER_FOR_TARGET = DiagnosticFactory2.create(WARNING);
DiagnosticFactory0<KtDeclaration> NO_EXPLICIT_VISIBILITY_IN_API_MODE = DiagnosticFactory0.create(ERROR, DECLARATION_MODIFIERS_AND_NAME);
DiagnosticFactory0<KtDeclaration> NO_EXPLICIT_VISIBILITY_IN_API_MODE = DiagnosticFactory0.create(ERROR, DECLARATION_START_TO_NAME);
DiagnosticFactory0<KtNamedDeclaration> NO_EXPLICIT_RETURN_TYPE_IN_API_MODE = DiagnosticFactory0.create(ERROR, DECLARATION_NAME);
DiagnosticFactory0<KtDeclaration> NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING = DiagnosticFactory0.create(WARNING, DECLARATION_MODIFIERS_AND_NAME);
DiagnosticFactory0<KtDeclaration> NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING = DiagnosticFactory0.create(WARNING,
DECLARATION_START_TO_NAME);
DiagnosticFactory0<KtNamedDeclaration> NO_EXPLICIT_RETURN_TYPE_IN_API_MODE_WARNING = DiagnosticFactory0.create(WARNING, DECLARATION_NAME);
DiagnosticFactory2<PsiElement, KtModifierKeywordToken, String> WRONG_MODIFIER_CONTAINING_DECLARATION = DiagnosticFactory2.create(ERROR);
DiagnosticFactory2<PsiElement, KtModifierKeywordToken, String> DEPRECATED_MODIFIER_CONTAINING_DECLARATION = DiagnosticFactory2.create(WARNING);
@@ -1,24 +1,15 @@
/*
* Copyright 2010-2017 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.
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.diagnostics
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.cfg.UnreachableCode
@@ -189,9 +180,27 @@ object PositioningStrategies {
}
@JvmField
val DECLARATION_MODIFIERS_AND_NAME: PositioningStrategy<KtDeclaration> = object : DeclarationHeader<KtDeclaration>() {
val DECLARATION_START_TO_NAME: PositioningStrategy<KtDeclaration> = object : DeclarationHeader<KtDeclaration>() {
private fun PsiElement.firstNonCommentNonAnnotationLeaf(): PsiElement? {
var child: PsiElement? = firstChild ?: return this // this is leaf
while (true) {
if (child is PsiComment || child is PsiWhiteSpace || child is KtAnnotationEntry) {
child = child.nextSibling
continue
}
if (child == null) return null // no children of accepted type in this
val leaf = child.firstNonCommentNonAnnotationLeaf()
if (leaf == null) {
child = child.nextSibling
continue
}
return leaf
}
}
override fun mark(element: KtDeclaration): List<TextRange> {
val startElement = element.firstChild
val startElement = element.firstNonCommentNonAnnotationLeaf() ?: element
val nameIdentifier = (element as? KtNamedDeclaration)?.nameIdentifier
return if (nameIdentifier != null) {
markRange(startElement, nameIdentifier)
@@ -0,0 +1,45 @@
// SKIP_TXT
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>annotation class A<!>
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.PROPERTY,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.FUNCTION
)
public annotation class B
/**
* Foo1 KDoc
*/
@B
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>class Foo1<!>() {}
public class Foo2() {
/**
* KDoc for methodWithAnnotations
*/
@B
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun methodWithAnnotations<!>() {}
/**
* Property KDoc
*/
@B
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var simple<!>: Int = 10
}
public open class ClassWithOpen {
/**
* constructor KDoc
*/
@B
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>constructor<!>() {}
/**
* KDoc for openAnnotatedMethod
*/
@B
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>open fun openAnnotatedMethod<!>() {}
}
@@ -1,9 +1,19 @@
// SKIP_TXT
/**
* KDoc for Foo1
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>class Foo1<!>() {}
public class Foo2() {
/**
* KDoc for method
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun method<!>() {}
/**
* KDoc for method2
*/
public fun method2() {}
private fun method3() {}
@@ -12,6 +22,18 @@ public class Foo2() {
public fun implicit3(): Int = 10
}
public open class ClassWithOpen() {
/**
* KDoc for method
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun method<!>() {}
/**
* KDoc for openMethod
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>open fun openMethod<!>() {}
}
public data class FooData(val i: Int, val s: String)
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>data class FooData2<!>(val i: Int, val s: String)
@@ -9,5 +9,8 @@ public class Bar2 {
}
public class Bar3 {
/**
* Nested object KDoc
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>object NestedObject<!> {}
}
@@ -6,6 +6,9 @@ public class Foo3 public constructor() {}
public class Foo4 private constructor() {}
public class Foo5 {
/**
* constructor KDoc
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>constructor<!>() {}
}
@@ -1,6 +1,9 @@
// SKIP_TXT
public class Foo(<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>val bar<!>: Int, private var bar2: String, internal var bar3: Long, public var bar4: Int) {
/**
* Property KDoc
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>var simple<!>: Int = 10
public var simple2: Int = 10
@@ -1,5 +1,8 @@
// SKIP_TXT
/**
* foo KDoc
*/
<!NO_EXPLICIT_VISIBILITY_IN_API_MODE!>fun foo<!>() {}
public fun foo2() {}
@@ -28,6 +28,11 @@ public class DiagnosticsWithExplicitApiGenerated extends AbstractDiagnosticsWith
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithExplicitApi"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@TestMetadata("annotations.kt")
public void testAnnotations() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/annotations.kt");
}
@TestMetadata("classes.kt")
public void testClasses() throws Exception {
runTest("compiler/testData/diagnostics/testsWithExplicitApi/classes.kt");