Skip to content
Draft
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
15 changes: 15 additions & 0 deletions docs/diagnostics/YodaStyle.md
Original file line number Diff line number Diff line change
@@ -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)
15 changes: 15 additions & 0 deletions docs/en/diagnostics/YodaStyle.md
Original file line number Diff line number Diff line change
@@ -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/)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright © 2018-2019
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Gryzlov <nixel2007@gmail.com> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
diagnosticMessage=The Yoda style is not allowed in the texts of the modules.
diagnosticName=Using Yoda style in code
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
diagnosticMessage=� ������� �������, ��� �������� ���������� ������� ���������� ������ ���� ����� �� ����� ���������� ��������, � �������� ������.
diagnosticName=������������� yoda style ��� �������� ����������
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright © 2018-2019
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Gryzlov <nixel2007@gmail.com> 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<YodaStyleDiagnostic> {

YodaStyleDiagnosticTest() {
super(YodaStyleDiagnostic.class);
}

@Test
void test() {
// when
List<Diagnostic> 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);
}
}
27 changes: 27 additions & 0 deletions src/test/resources/diagnostics/YodaStyleDiagnostic.bsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Процедура А()

// Выражение с ошибкой
Если 100 = ЙодаГоворит Тогда
КонецЕсли;

// Выражение с ошибкой
Если 100 + 10 = ЙодаГоворит Тогда
КонецЕсли;

// Выражение с ошибкой
Если "МолчитИода" = ЙодаГоворит Тогда
КонецЕсли;

// Корректное выражение
Если ЙодаГоворит = 100 Тогда
КонецЕсли;

// Корректное выражение
Если ЙодаГоворит = 100 + 10 Тогда
КонецЕсли;

// Корректное выражение
Если ЙодаГоворит = "МолчитИода" Тогда
КонецЕсли;

КонецПроцедуры;