Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report an error on unclosed comments #520

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/src/main/java/com/facebook/ktfmt/format/Tokenizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.facebook.ktfmt.format

import java.util.regex.Pattern
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
Expand Down Expand Up @@ -53,6 +54,10 @@ class Tokenizer(private val fileText: String, val file: KtFile) : KtTreeVisitorV
val originalText = fileText.substring(startIndex, endIndex)
when (element) {
is PsiComment -> {
if (element.text.startsWith("/*") && !element.text.endsWith("*/")) {
throw ParseError(
"Unclosed comment", StringUtil.offsetToLineColumn(fileText, element.startOffset))
}
toks.add(
KotlinTok(
index = index,
Expand Down
60 changes: 60 additions & 0 deletions core/src/test/java/com/facebook/ktfmt/format/TokenizerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.facebook.ktfmt.format

import com.google.common.truth.Truth.assertThat
import kotlin.test.assertFailsWith
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
Expand Down Expand Up @@ -218,4 +219,63 @@ class TokenizerTest {
23)
.inOrder()
}

@Test
fun `Unclosed comment obvious`() {
assertParseError(
"""
|package a.b
|/*
|class A {}
|"""
.trimMargin(),
"2:1: error: Unclosed comment")
}

@Test
fun `Unclosed comment too short`() {
assertParseError(
"""
|package a.b
|/*/
|class A {}
|"""
.trimMargin(),
"2:1: error: Unclosed comment")
}

@Test
fun `Unclosed comment nested`() {
assertParseError(
"""
|package a.b
|/* /* */
|class A {}
|"""
.trimMargin(),
"2:1: error: Unclosed comment")
}

@Test
fun `Unclosed comment nested EOF`() {
// TODO: https://youtrack.jetbrains.com/issue/KT-72887 - This should be an error.
assertParseError(
"""
|package a.b
|class A {}
|/* /* */"""
.trimMargin(),
null)
}

private fun assertParseError(code: String, message: String?) {
val file = Parser.parse(code)
val tokenizer = Tokenizer(code, file)
if (message == null) {
file.accept(tokenizer)
} else {
val e = assertFailsWith<ParseError> { file.accept(tokenizer) }
assertThat(e).hasMessageThat().isEqualTo(message)
}
}
}