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

Настройка исключений для диагностики CommentedCodeDiagnostic #2917

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@
import org.eclipse.lsp4j.TextEdit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@DiagnosticMetadata(
Expand All @@ -67,6 +70,11 @@ public class CommentedCodeDiagnostic extends AbstractDiagnostic implements Quick
)
private float threshold = COMMENTED_CODE_THRESHOLD;

@DiagnosticParameter(
type = String.class
)
private Set<String> exclusionPrefixes = Collections.emptySet();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Т.к. по умолчанию сет пустой, в целом не вижу причин, почему бы не влить не глядя. Но все же спрошу - пробовал замерять с включенной настройкой? не сильно просаживается анализ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nixel2007 протестим

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сделал замеры

feature/CommentedCodeDiagnostic_exclusionPrefixes

---------------------------------------------- benchmark: 1 tests ---------------------------------------------
Name (time in s)           Min      Max     Mean  StdDev   Median     IQR  Outliers     OPS  Rounds  Iterations
---------------------------------------------------------------------------------------------------------------
test_analyze_ssl31     15.3787  15.9920  15.7355  0.3187  15.8356  0.4600       1;0  0.0636       3           1
---------------------------------------------------------------------------------------------------------------
test_analyze_ssl31     14.8679  15.8270  15.3444  0.4796  15.3382  0.7193       1;0  0.0652       3           1
---------------------------------------------------------------------------------------------------------------


develop

---------------------------------------------- benchmark: 1 tests ---------------------------------------------
Name (time in s)           Min      Max     Mean  StdDev   Median     IQR  Outliers     OPS  Rounds  Iterations
---------------------------------------------------------------------------------------------------------------
test_analyze_ssl31     14.5382  15.2764  14.8924  0.3700  14.8626  0.5537       1;0  0.0671       3           1
---------------------------------------------------------------------------------------------------------------
test_analyze_ssl31     14.8964  16.0918  15.3080  0.6791  14.9359  0.8966       1;0  0.0653       3           1
---------------------------------------------------------------------------------------------------------------

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В пределах погрешности.


private List<MethodDescription> methodDescriptions;
private CodeRecognizer codeRecognizer;

Expand All @@ -78,6 +86,12 @@ public CommentedCodeDiagnostic() {
public void configure(Map<String, Object> configuration) {
threshold = ((Number) configuration.getOrDefault("threshold", threshold)).floatValue();
codeRecognizer = new CodeRecognizer(threshold, new BSLFootprint());

String excludePrefixesString = (String) configuration.getOrDefault("exclusionPrefixes", "");
exclusionPrefixes = Arrays.stream(excludePrefixesString.split(","))
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toSet());
}

@Override
Expand Down Expand Up @@ -170,11 +184,18 @@ private void checkCommentGroup(List<Token> commentGroup) {
}

private boolean isTextParsedAsCode(String text) {
String uncommented = uncomment(text);

for (String prefix : exclusionPrefixes) {
if (uncommented.startsWith(prefix)) {
return false;
}
}
if (!codeRecognizer.meetsCondition(text)) {
return false;
}

BSLTokenizer tokenizer = new BSLTokenizer(uncomment(text));
BSLTokenizer tokenizer = new BSLTokenizer(uncommented);
final List<Token> tokens = tokenizer.getTokens();

// Если меньше двух токенов нет смысла анализировать - это код
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@
],
"title": "Commented out code",
"properties": {
"exclusionPrefixes": {
"description": "Exclusion prefixes",
"default": "",
"type": "string",
"title": "Exclusion prefixes"
},
"threshold": {
"description": "Threshold",
"default": 0.9,
Expand Down Expand Up @@ -837,7 +843,7 @@
},
"listOfIncorrectFirstSymbol": {
"description": "Vertical bar-separated characters that should not start the line (special characters must be escaped)",
"default": "\\)|;|,|\\);",
"default": "\\)|;|,\\s*\\S+|\\);",
"type": "string",
"title": "Vertical bar-separated characters that should not start the line (special characters must be escaped)"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
diagnosticMessage=Modules must not have commented out code
diagnosticName=Commented out code
quickFixMessage=Remove commented code
threshold=Threshold
threshold=Threshold
exclusionPrefixes=Exclusion prefixes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
diagnosticMessage=Программные модули не должны иметь закомментированных фрагментов кода
diagnosticName=Закомментированный фрагмент кода
quickFixMessage=Удалить закомментированный код
threshold=Порог чуствительности
threshold=Порог чуствительности
exclusionPrefixes=Префиксы исключений
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,23 @@ class CommentedCodeDiagnosticTest extends AbstractDiagnosticTest<CommentedCodeDi
@Test
void runTest() {
List<Diagnostic> diagnostics = getDiagnostics();
check(diagnostics, false);
}

assertThat(diagnostics).hasSize(11);
@Test
void exclusionPrefixesTest(){
Map<String, Object> configuration = diagnosticInstance.info.getDefaultConfiguration();
configuration.put("exclusionPrefixes", "<code>");
diagnosticInstance.configure(configuration);

var diagnostics = getDiagnostics();
check(diagnostics, true);
}

void check(List<Diagnostic> diagnostics, boolean excludePrefixes){
int expectedSize = excludePrefixes?11:12;

assertThat(diagnostics).hasSize(expectedSize);
assertThat(diagnostics, true)
.hasRange(0, 0, 6, 81)
.hasRange(16, 4, 34, 16)
Expand All @@ -54,6 +69,10 @@ void runTest() {
.hasRange(117, 0, 118, 24)
.hasRange(203, 0, 203, 32)
.hasRange(244, 0, 264, 152);

if(!excludePrefixes){
assertThat(diagnostics, true).hasRange(268, 4, 270, 22);
}
}

@Test
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/diagnostics/CommentedCodeDiagnostic.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@
// Синтаксис:
// Процедура ПриЗавершенииСеансов(ФормаВладелец, Знач МассивСеансов, СтандартнаяОбработка, Знач ОповещениеПослеЗавершенияСеансов = Неопределено) Экспорт

Процедура ШаблонМетода(Параметр)

//<code>Если Истина Тогда
//<code>Возврат;
//<code>КонецЕсли;

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

#КонецОбласти

////////////////////////////////////////////////////////////////////////////////
Expand Down