forked from 1c-syntax/bsl-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Заготовка диагностики и красный тест на построитель выражений
- Loading branch information
Andrey Ovsyankin
committed
Jun 3, 2024
1 parent
d50117c
commit c1d49f5
Showing
8 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Двойные отрицания (DoubleNegatives) | ||
|
||
<!-- Блоки выше заполняются автоматически, не трогать --> | ||
## Описание диагностики | ||
|
||
Использование двойных отрицаний усложняет понимание кода и может приводить к ошибкам, когда вместо истины разработчик "в уме" вычислил Ложь, или наоборот. | ||
Двойные отрицания рекомендуется заменять на выражения условий, которые прямо выражают намерения автора. | ||
|
||
## Примеры | ||
|
||
### Неправильно | ||
|
||
```bsl | ||
Если Не ТаблицаЗначений.Найти(ИскомоеЗначение, "Колонка") <> Неопределено Тогда | ||
// Сделать действие | ||
КонецЕсли; | ||
``` | ||
|
||
### Правильно | ||
|
||
```bsl | ||
Если ТаблицаЗначений.Найти(ИскомоеЗначение, "Колонка") = Неопределено Тогда | ||
// Сделать действие | ||
КонецЕсли; | ||
``` | ||
|
||
## Источники | ||
<!-- Необходимо указывать ссылки на все источники, из которых почерпнута информация для создания диагностики --> | ||
|
||
* Источник: [Remove double negative](https://www.refactoring.com/catalog/removeDoubleNegative.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Double negatives (DoubleNegatives) | ||
|
||
<!-- Блоки выше заполняются автоматически, не трогать --> | ||
## Description | ||
|
||
Using double negatives complicates the understanding of the code and can lead to errors when instead of truth the developer "in his mind" calculated False, or vice versa. It is recommended to replace double negatives with conditions that directly express the author's intentions. | ||
|
||
## Examples | ||
|
||
### Wrong | ||
|
||
```bsl | ||
If Not ValueTable.Find(ValueToSearch, "Column") <> Undefined Тогда | ||
// Act | ||
EndIf; | ||
``` | ||
|
||
### Correct | ||
|
||
```bsl | ||
If ValueTable.Find(ValueToSearch, "Column") = Undefined Тогда | ||
// Act | ||
EndIf; | ||
``` | ||
|
||
## Sources | ||
|
||
* Источник: [Remove double negative](https://www.refactoring.com/catalog/removeDoubleNegative.html) |
38 changes: 38 additions & 0 deletions
38
.../java/com/github/_1c_syntax/bsl/languageserver/diagnostics/DoubleNegativesDiagnostic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
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.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.languageserver.utils.expressiontree.ExpressionParseTreeRewriter; | ||
import com.github._1c_syntax.bsl.parser.BSLParser; | ||
import org.antlr.v4.runtime.tree.ParseTree; | ||
|
||
@DiagnosticMetadata( | ||
type = DiagnosticType.CODE_SMELL, | ||
severity = DiagnosticSeverity.MAJOR, | ||
minutesToFix = 3, | ||
tags = { | ||
DiagnosticTag.BRAINOVERLOAD, | ||
DiagnosticTag.BADPRACTICE | ||
} | ||
) | ||
public class DoubleNegativesDiagnostic extends AbstractVisitorDiagnostic { | ||
|
||
private static final int MIN_EXPRESSION_SIZE = 3; | ||
|
||
@Override | ||
public ParseTree visitExpression(BSLParser.ExpressionContext ctx) { | ||
|
||
if (sufficientSize(ctx)) | ||
return ctx; | ||
|
||
var tree = ExpressionParseTreeRewriter.buildExpressionTree(ctx); | ||
|
||
return ctx; | ||
} | ||
|
||
private static boolean sufficientSize(BSLParser.ExpressionContext ctx) { | ||
return ctx.children.size() < MIN_EXPRESSION_SIZE; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
.../github/_1c_syntax/bsl/languageserver/diagnostics/DoubleNegativesDiagnostic_en.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
diagnosticMessage=Using double negatives complicates understandong of code | ||
diagnosticName=Double negatives |
2 changes: 2 additions & 0 deletions
2
.../github/_1c_syntax/bsl/languageserver/diagnostics/DoubleNegativesDiagnostic_ru.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
diagnosticMessage=Использование двойных отрицаний усложняет понимание кода | ||
diagnosticName=Двойные отрицания |
25 changes: 25 additions & 0 deletions
25
...a/com/github/_1c_syntax/bsl/languageserver/diagnostics/DoubleNegativesDiagnosticTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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 DoubleNegativesDiagnosticTest extends AbstractDiagnosticTest<DoubleNegativesDiagnostic> { | ||
DoubleNegativesDiagnosticTest() { | ||
super(DoubleNegativesDiagnostic.class); | ||
} | ||
|
||
@Test | ||
void test() { | ||
|
||
List<Diagnostic> diagnostics = getDiagnostics(); | ||
|
||
assertThat(diagnostics, true) | ||
.hasRange(3, 6, 3, 74) | ||
; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/test/resources/diagnostics/DoubleNegativesDiagnostic.bsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Выражение в условии | ||
Если Не ТаблицаЗначений.Найти(ИскомоеЗначение, "Колонка") <> Неопределено Тогда | ||
// Сделать действие | ||
КонецЕсли; | ||
|
||
// Отрицание с проверкой на литерал | ||
|
||
А = Не Отказ = Ложь; | ||
А = Не (Отказ = Ложь); | ||
А = Не Отказ <> Ложь; | ||
А = Не (Отказ <> Ложь); | ||
А = Не НекотороеЗначение() <> Неопределено; | ||
А = Не Неопределено <> НекотороеЗначение(); | ||
А = Не (А <> Неопределено); // срабатывает | ||
А = Не А <> Неопределено И Б = 5; // срабатывает | ||
А = Не (А <> Неопределено и Б = 5); // не срабатывает | ||
А = Не (А <> Неопределено или Б = 5); // не срабатывает | ||
А = Не (Б = 5 и А <> Неопределено); // не срабатывает | ||
|
||
Пока Не Таблица.Данные <> Неопределено Цикл | ||
КонецЦикла; | ||
|
||
Б = Не (Не А = 1 или Б <> Неопределено); // срабатывает на "Не А = 1" | ||
Б = Не (А <> 1 или Не Б <> Неопределено); // срабатывает на "Не Б <> Неопределено" | ||
Б = Не (А <> 1 или Не Б = Неопределено); // не срабатывает на "Не Б <> Неопределено" т.к. сравнения вида Не Х = Неопределено популярны | ||
|
||
Если Не Т.Найти(Значение) = Неопределено Тогда | ||
// не срабатывает, т.к. популярный код | ||
КонецЕсли; | ||
|
||
// Отрицание с проверкой на неравенство нелитералу | ||
|
||
А = Не (Отказ <> НеЛитерал); // срабатывает | ||
А = Не СложнаяФункция() <> НеЛитерал; // срабатывает | ||
|
||
Б = Не (А = 1 или Б <> НеЛитерал); // не срабатывает | ||
|
||
// Прямое двойное отрицание | ||
|
||
Б = Не (Не Значение); |