JS: add checker that checks if JsQualifier has wrong format
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
// FILE: a.kt
|
||||||
|
@file:JsQualifier(<!WRONG_JS_QUALIFIER!>""<!>)
|
||||||
|
|
||||||
|
// FILE: b.kt
|
||||||
|
@file:JsQualifier(<!WRONG_JS_QUALIFIER!>"a..b"<!>)
|
||||||
|
|
||||||
|
// FILE: c.kt
|
||||||
|
@file:JsQualifier(<!WRONG_JS_QUALIFIER!>"a."<!>)
|
||||||
|
|
||||||
|
// FILE: d.kt
|
||||||
|
@file:JsQualifier(<!WRONG_JS_QUALIFIER!>".a"<!>)
|
||||||
|
|
||||||
|
// FILE: e.kt
|
||||||
|
@file:JsQualifier(<!WRONG_JS_QUALIFIER!>"%^&"<!>)
|
||||||
|
|
||||||
|
// FILE: f.kt
|
||||||
|
@file:JsQualifier("a.bc.d23._$")
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
package
|
||||||
+21
@@ -996,6 +996,27 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("compiler/testData/diagnostics/testsWithJsStdLib/qualifier")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class Qualifier extends AbstractDiagnosticsTestWithJsStdLib {
|
||||||
|
public void testAllFilesPresentInQualifier() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithJsStdLib/qualifier"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("jsQualifierNonExternal.kt")
|
||||||
|
public void testJsQualifierNonExternal() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/qualifier/jsQualifierNonExternal.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("wrongQualifier.kt")
|
||||||
|
public void testWrongQualifier() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/qualifier/wrongQualifier.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("compiler/testData/diagnostics/testsWithJsStdLib/reflection")
|
@TestMetadata("compiler/testData/diagnostics/testsWithJsStdLib/reflection")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
@@ -68,5 +68,6 @@ object JsPlatformConfigurator : PlatformConfigurator(
|
|||||||
container.useImpl<JsNativeRttiChecker>()
|
container.useImpl<JsNativeRttiChecker>()
|
||||||
container.useImpl<JsReifiedNativeChecker>()
|
container.useImpl<JsReifiedNativeChecker>()
|
||||||
container.useInstance(ExtensionFunctionToExternalIsInlinable)
|
container.useInstance(ExtensionFunctionToExternalIsInlinable)
|
||||||
|
container.useInstance(JsQualifierChecker)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.js.resolve.diagnostics
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||||
|
import org.jetbrains.kotlin.js.translate.utils.AnnotationsUtils
|
||||||
|
import org.jetbrains.kotlin.psi.KtAnnotationEntry
|
||||||
|
import org.jetbrains.kotlin.resolve.AdditionalAnnotationChecker
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
import org.jetbrains.kotlin.resolve.BindingTrace
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||||
|
|
||||||
|
object JsQualifierChecker : AdditionalAnnotationChecker {
|
||||||
|
override fun checkEntries(entries: List<KtAnnotationEntry>, actualTargets: List<KotlinTarget>, trace: BindingTrace) {
|
||||||
|
val bindingContext = trace.bindingContext
|
||||||
|
for (entry in entries) {
|
||||||
|
val annotation = bindingContext[BindingContext.ANNOTATION, entry] ?: continue
|
||||||
|
if (annotation.type.constructor.declarationDescriptor?.fqNameSafe != AnnotationsUtils.JS_QUALIFIER_ANNOTATION) continue
|
||||||
|
val argument = annotation.allValueArguments.values.singleOrNull()?.value as? String ?: continue
|
||||||
|
if (!validateQualifier(argument)) {
|
||||||
|
val argumentPsi = entry.valueArgumentList!!.arguments[0]
|
||||||
|
trace.report(ErrorsJs.WRONG_JS_QUALIFIER.on(argumentPsi))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun validateQualifier(qualifier: String): Boolean {
|
||||||
|
val parts = qualifier.split('.')
|
||||||
|
if (parts.isEmpty()) return false
|
||||||
|
|
||||||
|
return parts.all { part ->
|
||||||
|
part.isNotEmpty() && part[0].isJavaIdentifierStart() && part.drop(1).all(Char::isJavaIdentifierPart)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user