diff --git a/.gitignore b/.gitignore index 0e9e4c005a6..3bd29799d88 100644 --- a/.gitignore +++ b/.gitignore @@ -1,63 +1,65 @@ -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff -.idea/**/workspace.xml -.idea/**/tasks.xml -.idea/**/usage.statistics.xml -.idea/**/dictionaries -.idea/**/shelf - -# Generated files -.idea/**/contentModel.xml - -# Sensitive or high-churn files -.idea/**/dataSources/ -.idea/**/dataSources.ids -.idea/**/dataSources.local.xml -.idea/**/sqlDataSources.xml -.idea/**/dynamic.xml -.idea/**/uiDesigner.xml -.idea/**/dbnavigator.xml - -# Gradle -.idea/**/gradle.xml -.idea/**/libraries - -# Gradle and Maven with auto-import -# When using Gradle or Maven with auto-import, you should exclude module files, -# since they will be recreated, and may cause churn. Uncomment if using -# auto-import. -.idea/modules.xml -.idea/*.iml -.idea/modules - -# IntelliJ -out/ - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties - -# Editor-based Rest Client -.idea/httpRequests - -.gradle/ -*.zip -*.ps1 -target/ -build/ - -# Scala compiler user settings -.idea/hydra.xml - -.idea/sonarlint/ - -intellij-bsl/src/test/resources/parser/.idea/ - -gen/ - -.idea/sonarlint-state.xml -.idea/sonarlint.xml +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +.idea/modules.xml +.idea/*.iml +.idea/modules + +# IntelliJ +out/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Scala compiler user settings +.idea/hydra.xml + +# Sonar configuration +.idea/sonarlint/ +.idea/sonarlint-state.xml +.idea/sonarlint.xml + +# Archive files +*.zip +*.ps1 + +# Other folder +intellij-bsl/src/test/resources/parser/.idea/ +gen/ +target/ +build/ +gradle/ + diff --git a/.idea/misc.xml b/.idea/misc.xml index 84da703c3b6..cc42a578650 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ + + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 598873e20ee..1ce364f2896 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,14 +1,22 @@ -# Руководство контрибьютора - -## Требования к окружению - -* Java Development Kit 8 -* IntelliJ IDEA Community Edition -* Lombok Plugin к IntelliJ IDEA -* EditorConfig Plugin к IntelliJ IDEA - -## Настройка IntelliJ IDEA - -* Java SDK на JDK8 -* File | Settings | Build, Execution, Deployment | Compiler | Annotation Processors | Enable annotation processing - checked - +# Руководство контрибьютора + +## Требования к окружению + +* Java Development Kit 8 +* IntelliJ IDEA Community Edition +* Lombok Plugin к IntelliJ IDEA +* EditorConfig Plugin к IntelliJ IDEA + +## Настройка IntelliJ IDEA + +* Java SDK на JDK8 +* File + * Settings + * Build, Execution, Deployment + * Compiler + * Annotation Processors + * Enable annotation processing - checked + +## Добавление диагностики + +Пример добавления диагностики в [WIKI проекта](https://github.com/1c-syntax/bsl-language-server/wiki/%D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5-%D0%B4%D0%B8%D0%B0%D0%B3%D0%BD%D0%BE%D1%81%D1%82%D0%B8%D0%BA%D0%B8) 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/docs/en/index.md b/docs/en/index.md index 01d39b71c76..0e881553e2f 100644 --- a/docs/en/index.md +++ b/docs/en/index.md @@ -165,4 +165,5 @@ Some of diagnostics are disabled by default. Use config | [UsingFindElementByString](diagnostics/UsingFindElementByString.md) | Restriction on the use of "FindByDescription" and "FindByCode" methods | Yes | | [UsingGoto](diagnostics/UsingGoto.md) | "goto" statement should not be used | Yes | | [UsingServiceTag](diagnostics/UsingServiceTag.md) | Using service tags | Yes | +| [YodaStyleDiagnostic](diagnostics/YodaStyle.md) | Using Yoda style for comparing values | Yes | | [YoLetterUsageDiagnostic](diagnostics/YoLetterUsage.md) | Using "Ё" letter in code | Yes | diff --git a/docs/index.md b/docs/index.md index 29109f859a8..0149b66ce65 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,3 +1,8 @@ +--- +title: Home +layout: post +--- + # BSL Language Server [![Build Status](https://travis-ci.org/1c-syntax/bsl-language-server.svg?branch=master)](https://travis-ci.org/1c-syntax/bsl-language-server) diff --git a/src/main/java/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic.java b/src/main/java/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic.java new file mode 100644 index 00000000000..54b6d00969a --- /dev/null +++ b/src/main/java/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic.java @@ -0,0 +1,56 @@ +/* + * 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 org.github._1c_syntax.bsl.languageserver.diagnostics; + +import org.antlr.v4.runtime.tree.ParseTree; +import org.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata; +import org.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope; +import org.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity; +import org.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType; +import org.github._1c_syntax.bsl.parser.BSLParser; + +import java.util.regex.Pattern; + +@DiagnosticMetadata( + type = DiagnosticType.CODE_SMELL, + scope = DiagnosticScope.BSL, + severity = DiagnosticSeverity.MINOR, + minutesToFix = 2 +) + +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 visitGlobalMethodCall(BSLParser.GlobalMethodCallContext ctx) { + + if (messagePattern.matcher(ctx.methodName().getText()).matches()) { + diagnosticStorage.addDiagnostic(ctx); + } + + return super.visitGlobalMethodCall(ctx); + } + +} diff --git a/src/main/resources/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_en.properties b/src/main/resources/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_en.properties new file mode 100644 index 00000000000..29a2d38294a --- /dev/null +++ b/src/main/resources/org/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/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_ru.properties b/src/main/resources/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnostic_ru.properties new file mode 100644 index 00000000000..79b46e65631 --- /dev/null +++ b/src/main/resources/org/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/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnosticTest.java b/src/test/java/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnosticTest.java new file mode 100644 index 00000000000..f79f613a1ee --- /dev/null +++ b/src/test/java/org/github/_1c_syntax/bsl/languageserver/diagnostics/YodaStyleDiagnosticTest.java @@ -0,0 +1,51 @@ +/* + * 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 org.github._1c_syntax.bsl.languageserver.diagnostics; + +import org.eclipse.lsp4j.Diagnostic; +import org.github._1c_syntax.bsl.languageserver.utils.RangeHelper; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class YodaStyleDiagnosticTest extends AbstractDiagnosticTest { + + YodaStyleDiagnosticTest() { + super(YodaStyleDiagnostic.class); + } + + @Test + void test() { + // when + List diagnostics = getDiagnostics(); + // then + assertThat(diagnostics).hasSize(3); + assertThat(diagnostics) + .anyMatch(diagnostic -> diagnostic.getRange().equals(RangeHelper.newRange(3, 0 , 3, 23))); + assertThat(diagnostics) + .anyMatch(diagnostic -> diagnostic.getRange().equals(RangeHelper.newRange(9, 0 , 9, 28))); + assertThat(diagnostics) + .anyMatch(diagnostic -> diagnostic.getRange().equals(RangeHelper.newRange(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