Permalink
Browse files

Sanitize names for script class files

Use the same process that is done for package parts in the JVM back-end, except
adding the "Kt" suffix

 #KT-15225 Fixed
  • Loading branch information...
1 parent 6ffa56b commit abe2ad155cb473cb4167442f3fa67e26be73b0bb @udalov udalov committed Jan 11, 2017
@@ -20,6 +20,7 @@ import com.intellij.openapi.fileTypes.LanguageFileType
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.name.Name
+import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.psi.KtScript
import java.io.File
@@ -41,7 +42,7 @@ open class KotlinScriptDefinition(val template: KClass<out Any>) {
getFileName(file).endsWith(KotlinParserDefinition.STD_SCRIPT_EXT)
open fun getScriptName(script: KtScript): Name =
- ScriptNameUtil.fileNameWithExtensionStripped(script)
+ NameUtils.getScriptNameForFile(script.containingKtFile.name)
open fun <TF: Any> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? = null
}
@@ -23,7 +23,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import org.jetbrains.kotlin.name.Name
-import org.jetbrains.kotlin.parsing.KotlinParserDefinition
+import org.jetbrains.kotlin.name.NameUtils
import org.jetbrains.kotlin.psi.KtAnnotationEntry
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtScript
@@ -85,7 +85,7 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate(
scriptFilePattern.let { Regex(it).matches(getFileName(file)) }
// TODO: implement other strategy - e.g. try to extract something from match with ScriptFilePattern
- override fun getScriptName(script: KtScript): Name = ScriptNameUtil.fileNameWithExtensionStripped(script)
+ override fun getScriptName(script: KtScript): Name = NameUtils.getScriptNameForFile(script.containingKtFile.name)
override fun <TF: Any> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? {
@@ -1,33 +0,0 @@
-/*
- * Copyright 2010-2015 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.script
-
-import org.jetbrains.kotlin.name.Name
-import org.jetbrains.kotlin.psi.KtScript
-
-object ScriptNameUtil {
- fun fileNameWithExtensionStripped(script: KtScript): Name =
- Name.identifier(generateNameByFileName(script.containingKtFile.name))
-
- fun generateNameByFileName(filePath: String): String {
- val fileName = filePath.substringAfterLast('/')
- val nameWithoutExtension = fileName.substringBeforeLast('.')
- return nameWithoutExtension
- .replace('.', '_')
- .capitalize()
- }
-}
@@ -0,0 +1 @@
+val result = "OK"
@@ -90,6 +90,13 @@ class ScriptGenTest : CodegenTestCase() {
assertEquals(239, invoke as Int / 2)
}
+ fun testNameSanitation() {
+ setUpEnvironment("scriptCustom/1#@2.kts")
+
+ val aClass = generateClass("_1__2")
+ assertEquals("OK", aClass.getDeclaredMethod("getResult")(aClass.newInstance()))
+ }
+
private fun setUpEnvironment(sourcePath: String) {
setUpEnvironment(listOf(sourcePath))
}
@@ -45,4 +45,9 @@ object NameUtils {
str.substring(0, 1).toUpperCase(Locale.ENGLISH) + str.substring(1)
else
"_$str"
+
+ // "pkg/someScript.kts" -> "SomeScript"
+ @JvmStatic
+ fun getScriptNameForFile(filePath: String): Name =
+ Name.identifier(NameUtils.getPackagePartClassNamePrefix(filePath.substringAfterLast('/').substringBeforeLast('.')))
}
@@ -35,9 +35,7 @@ import com.intellij.refactoring.listeners.RefactoringElementListener
import org.jdom.Element
import org.jetbrains.kotlin.idea.core.script.KotlinScriptConfigurationManager
import org.jetbrains.kotlin.idea.run.script.standalone.KotlinStandaloneScriptRunConfigurationProducer.Companion.pathFromPsiElement
-import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.psi.KtFile
-import org.jetbrains.kotlin.script.ScriptNameUtil
import org.jetbrains.kotlin.utils.PathUtil
import java.io.File
import java.util.*

0 comments on commit abe2ad1

Please sign in to comment.