diff --git a/docs/diagnostics/YodaStyle.md b/docs/diagnostics/YodaStyle.md new file mode 100644 index 00000000000..c11b4cd6ac6 --- /dev/null +++ b/docs/diagnostics/YodaStyle.md @@ -0,0 +1,15 @@ +# В операциях сравнения переменная должна быть слева от оператора, значение должно быть справа + +Сравнивая значения и переменные помни: переменные слева быть должны (С) Yoda + +Плохо: +```BSL +Если 0 = Сумма Тогда +``` + +Хорошо: +```BSL +Если Сумма = 0 Тогда +``` + +[Источник wikipedia](https://ru.wikipedia.org/wiki/%D0%A3%D1%81%D0%BB%D0%BE%D0%B2%D0%B8%D1%8F_%D0%99%D0%BE%D0%B4%D1%8B) \ No newline at end of file diff --git a/docs/en/diagnostics/YodaStyle.md b/docs/en/diagnostics/YodaStyle.md new file mode 100644 index 00000000000..babe0f1b8c3 --- /dev/null +++ b/docs/en/diagnostics/YodaStyle.md @@ -0,0 +1,15 @@ +# In comparison operations, the variable must be to the left of the operator, the value must be to the right + +Compare values and variables, remember: the variables on the left should be (С) Yoda + +Bad: +```BSL +If 0 = Summ Then +``` + +Good: +```BSL +If Summ = 0 Then +``` + +[Source](https://www.united-coders.com/christian-harms/what-are-yoda-conditions/) \ No newline at end of file diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic.java new file mode 100644 index 00000000000..6af688af0f2 --- /dev/null +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic.java @@ -0,0 +1,59 @@ +/* + * This file is a part of BSL Language Server. + * + * Copyright © 2018-2019 + * Alexey Sosnoviy , Nikita Gryzlov and contributors + * + * SPDX-License-Identifier: LGPL-3.0-or-later + * + * BSL Language Server is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * BSL Language Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with BSL Language Server. + */ +package com.github._1c_syntax.bsl.languageserver.diagnostics; + +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata; +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope; +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity; +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag; +import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType; +import com.github._1c_syntax.bsl.parser.BSLParser; +import org.antlr.v4.runtime.tree.ParseTree; + +import java.util.regex.Pattern; + +@DiagnosticMetadata( + type = DiagnosticType.CODE_SMELL, + severity = DiagnosticSeverity.MINOR, + scope = DiagnosticScope.BSL, + minutesToFix = 1, + tags = {DiagnosticTag.BRAINOVERLOAD} +) + +public class YodaStyleDiagnostic extends AbstractVisitorDiagnostic { + private static final Pattern messagePattern = Pattern.compile( + "(Если|if)\\s+([\\d]|\\\")(.)*\\=(.)(?!\\\"|\\d)", + Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE + ); + + @Override + public ParseTree visitIfBranch(BSLParser.IfBranchContext ctx) { + + + if (messagePattern.matcher(ctx.getText()).matches()) { + diagnosticStorage.addDiagnostic(ctx); + } + + return super.visitIfBranch(ctx); + } + +} diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_en.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_en.properties new file mode 100644 index 00000000000..29a2d38294a --- /dev/null +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_en.properties @@ -0,0 +1,2 @@ +diagnosticMessage=The Yoda style is not allowed in the texts of the modules. +diagnosticName=Using Yoda style in code \ No newline at end of file diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_ru.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_ru.properties new file mode 100644 index 00000000000..79b46e65631 --- /dev/null +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_ru.properties @@ -0,0 +1,2 @@ +diagnosticMessage= , , . +diagnosticName= yoda style \ No newline at end of file diff --git a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnosticTest.java b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnosticTest.java new file mode 100644 index 00000000000..2527c4c84c6 --- /dev/null +++ b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnosticTest.java @@ -0,0 +1,48 @@ +/* + * This file is a part of BSL Language Server. + * + * Copyright © 2018-2019 + * Alexey Sosnoviy , Nikita Gryzlov and contributors + * + * SPDX-License-Identifier: LGPL-3.0-or-later + * + * BSL Language Server is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3.0 of the License, or (at your option) any later version. + * + * BSL Language Server is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with BSL Language Server. + */ +package com.github._1c_syntax.bsl.languageserver.diagnostics; + +import org.eclipse.lsp4j.Diagnostic; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static com.github._1c_syntax.bsl.languageserver.util.Assertions.assertThat; + +class YodaStyleDiagnosticTest extends AbstractDiagnosticTest { + + YodaStyleDiagnosticTest() { + super(YodaStyleDiagnostic.class); + } + + @Test + void test() { + // when + List diagnostics = getDiagnostics(); + // then + assertThat(diagnostics).hasSize(3); + assertThat(diagnostics, true) + .hasRange(3, 0, 3, 23) + .hasRange(9, 0, 9, 28) + .hasRange(13, 0, 13, 32); + } +} diff --git a/src/test/resources/diagnostics/YodaStyleDiagnostic.bsl b/src/test/resources/diagnostics/YodaStyleDiagnostic.bsl new file mode 100644 index 00000000000..9a73e72f241 --- /dev/null +++ b/src/test/resources/diagnostics/YodaStyleDiagnostic.bsl @@ -0,0 +1,27 @@ +Процедура А() + +// Выражение с ошибкой +Если 100 = ЙодаГоворит Тогда +КонецЕсли; + +// Выражение с ошибкой +Если 100 + 10 = ЙодаГоворит Тогда +КонецЕсли; + +// Выражение с ошибкой +Если "МолчитИода" = ЙодаГоворит Тогда +КонецЕсли; + +// Корректное выражение +Если ЙодаГоворит = 100 Тогда +КонецЕсли; + +// Корректное выражение +Если ЙодаГоворит = 100 + 10 Тогда +КонецЕсли; + +// Корректное выражение +Если ЙодаГоворит = "МолчитИода" Тогда +КонецЕсли; + +КонецПроцедуры; \ No newline at end of file