Report warning on characters which can cause problems on Windows
As soon as we fix KT-17438, this warning will be turned into an error.
This commit is contained in:
+20
-8
@@ -21,11 +21,15 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticSink
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.IdentifierChecker
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
|
||||
|
||||
object JvmSimpleNameBacktickChecker : IdentifierChecker {
|
||||
// See The Java Virtual Machine Specification, section 4.7.9.1 https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.9.1
|
||||
val INVALID_CHARS = setOf('.', ';', '[', ']', '/', '<', '>', ':', '\\')
|
||||
|
||||
// These characters can cause problems on Windows. '?*"|' are not allowed in file names, and % leads to unexpected env var expansion.
|
||||
private val DANGEROUS_CHARS = setOf('?', '*', '"', '|', '%')
|
||||
|
||||
override fun checkIdentifier(simpleNameExpression: KtSimpleNameExpression, diagnosticHolder: DiagnosticSink) {
|
||||
reportIfNeeded(simpleNameExpression.getReferencedName(), { simpleNameExpression.getIdentifier() }, diagnosticHolder)
|
||||
}
|
||||
@@ -53,15 +57,23 @@ object JvmSimpleNameBacktickChecker : IdentifierChecker {
|
||||
|
||||
private fun reportIfNeeded(name: String, reportOn: () -> PsiElement?, diagnosticHolder: DiagnosticSink) {
|
||||
val text = KtPsiUtil.unquoteIdentifier(name)
|
||||
if (text.isEmpty()) {
|
||||
diagnosticHolder.report(Errors.INVALID_CHARACTERS.on(reportOn() ?: return, "should not be empty"))
|
||||
} else if (text.any { it in INVALID_CHARS }) {
|
||||
diagnosticHolder.report(
|
||||
Errors.INVALID_CHARACTERS.on(
|
||||
reportOn() ?: return,
|
||||
"contains illegal characters: ${INVALID_CHARS.intersect(text.toSet()).joinToString("")}"
|
||||
when {
|
||||
text.isEmpty() -> {
|
||||
diagnosticHolder.report(Errors.INVALID_CHARACTERS.on(reportOn() ?: return, "should not be empty"))
|
||||
}
|
||||
text.any { it in INVALID_CHARS } -> {
|
||||
diagnosticHolder.report(
|
||||
Errors.INVALID_CHARACTERS.on(
|
||||
reportOn() ?: return,
|
||||
"contains illegal characters: ${INVALID_CHARS.intersect(text.toSet()).joinToString("")}"
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
text.any { it in DANGEROUS_CHARS } -> {
|
||||
diagnosticHolder.report(
|
||||
ErrorsJvm.DANGEROUS_CHARACTERS.on(reportOn() ?: return, DANGEROUS_CHARS.intersect(text.toSet()).joinToString(""))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
@@ -165,6 +165,8 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
|
||||
"or annotate the class with @JvmDefaultWithoutCompatibility. " +
|
||||
"Please refer to KT-39603 for details",
|
||||
COMPACT, SHORT_NAMES_IN_TYPES);
|
||||
|
||||
MAP.put(DANGEROUS_CHARACTERS, "Name contains characters which can cause problems on Windows: {0}", STRING);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -141,6 +141,8 @@ public interface ErrorsJvm {
|
||||
|
||||
DiagnosticFactory2<KtDeclaration, CallableDescriptor, CallableDescriptor> EXPLICIT_OVERRIDE_REQUIRED_IN_COMPATIBILITY_MODE = DiagnosticFactory2.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
|
||||
|
||||
DiagnosticFactory1<PsiElement, String> DANGEROUS_CHARACTERS = DiagnosticFactory1.create(WARNING);
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
Object _initializer = new Object() {
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user