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

UnusedLocalMethod - "Неиспользуемый локальный метод" - поддержка модулей объектов через параметр правила #3112

Merged
merged 3 commits into from
Oct 4, 2023
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 @@ -48,7 +48,8 @@
type = DiagnosticType.CODE_SMELL,
severity = DiagnosticSeverity.MAJOR,
modules = {
ModuleType.CommonModule
ModuleType.CommonModule,
ModuleType.ObjectModule
},
minutesToFix = 1,
tags = {
Expand All @@ -74,17 +75,26 @@ public class UnusedLocalMethodDiagnostic extends AbstractVisitorDiagnostic {
AnnotationKind.BEFORE,
AnnotationKind.CHANGEANDVALIDATE
);
private static final boolean CHECK_OBJECT_MODULE = false;

@DiagnosticParameter(
type = String.class,
defaultValue = ATTACHABLE_METHOD_PREFIXES
)
private Pattern attachableMethodPrefixes = DiagnosticHelper.createPatternFromString(ATTACHABLE_METHOD_PREFIXES);

@DiagnosticParameter(
type = Boolean.class,
defaultValue = "" + CHECK_OBJECT_MODULE
)
private boolean checkObjectModule = CHECK_OBJECT_MODULE;

@Override
public void configure(Map<String, Object> configuration) {
this.attachableMethodPrefixes = DiagnosticHelper.createPatternFromString(
(String) configuration.getOrDefault("attachableMethodPrefixes", ATTACHABLE_METHOD_PREFIXES));

this.checkObjectModule = (boolean) configuration.getOrDefault("checkObjectModule", CHECK_OBJECT_MODULE);
}

private boolean isAttachable(MethodSymbol methodSymbol) {
Expand All @@ -104,6 +114,10 @@ private static boolean isOverride(MethodSymbol method) {

@Override
public ParseTree visitFile(BSLParser.FileContext ctx) {
var moduleType = documentContext.getModuleType();
if (!checkObjectModule && moduleType == ModuleType.ObjectModule) {
return ctx;
}

List<String> collect = Trees.findAllRuleNodes(ctx, BSLParser.RULE_globalMethodCall)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,12 @@
"default": "\u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u044b\u0439_,attachable_",
"type": "string",
"title": "Method prefixes (comma separated)"
},
"checkObjectModule": {
"description": "Check object modules",
"default": false,
"type": "boolean",
"title": "Check object modules"
}
},
"$id": "#/definitions/UnusedLocalMethod"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
diagnosticMessage=Method "%s" is not called in the module
diagnosticName=Unused local method
attachableMethodPrefixes=Method prefixes (comma separated)
checkObjectModule=Check object modules
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
diagnosticMessage=Метод "%s" не вызывается в теле модуля
diagnosticName=Неиспользуемый локальный метод
attachableMethodPrefixes=Префиксы подключаемых методов (через запятую)
checkObjectModule=Проверять модули объектов
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,58 @@
*/
package com.github._1c_syntax.bsl.languageserver.diagnostics;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.util.TestUtils;
import com.github._1c_syntax.bsl.types.ModuleType;
import com.github._1c_syntax.mdclasses.mdo.MDCommonModule;
import com.github._1c_syntax.utils.Absolute;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.eclipse.lsp4j.Diagnostic;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.github._1c_syntax.bsl.languageserver.util.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

class UnusedLocalMethodDiagnosticTest extends AbstractDiagnosticTest<UnusedLocalMethodDiagnostic> {
private static final String PATH_TO_METADATA = "src/test/resources/metadata/designer";
private static final String PATH_TO_MODULE_FILE = PATH_TO_METADATA + "/CommonModules/ПервыйОбщийМодуль/Ext/Module.bsl";
private static final String PATH_TO_MODULE_CONTENT = "src/test/resources/diagnostics/UnusedLocalMethodDiagnostic.bsl";

private MDCommonModule module;
private DocumentContext documentContext;
UnusedLocalMethodDiagnosticTest() {
super(UnusedLocalMethodDiagnostic.class);
}

@Test
void test() {

List<Diagnostic> diagnostics = getDiagnostics();
checkByDefault(diagnostics);
}

private static void checkByDefault(List<Diagnostic> diagnostics) {
assertThat(diagnostics).hasSize(2);
assertThat(diagnostics, true)
.hasRange(1, 10, 24)
.hasRange(70, 10, 41)
;
}

@Test
void testObjectModuleByDefault() {
getObjectModuleDocumentContext();

List<Diagnostic> diagnostics = getDiagnostics(documentContext);
assertThat(diagnostics).isEmpty();
}

@Test
Expand All @@ -65,4 +93,44 @@ void testConfigure() {
.hasRange(63, 10, 39)
;
}

@Test
void testObjectModuleWithEnabledConfiguration() {
// given
getObjectModuleDocumentContext();

Map<String, Object> configuration = diagnosticInstance.getInfo().getDefaultConfiguration();
configuration.put("checkObjectModule", true);
diagnosticInstance.configure(configuration);

// when
List<Diagnostic> diagnostics = getDiagnostics(documentContext);

// then
checkByDefault(diagnostics);
}

private void getObjectModuleDocumentContext() {
Path testFile = Paths.get(PATH_TO_MODULE_CONTENT).toAbsolutePath();
getDocumentContextFromFile(testFile);
when(documentContext.getModuleType()).thenReturn(ModuleType.ObjectModule);
when(documentContext.getMdObject()).thenReturn(Optional.of(module));
}

@SneakyThrows
void getDocumentContextFromFile(Path testFile) {

Path path = Absolute.path(PATH_TO_METADATA);
Path moduleFile = Paths.get(PATH_TO_MODULE_FILE).toAbsolutePath();

initServerContext(path);
var configuration = context.getConfiguration();
documentContext = spy(TestUtils.getDocumentContext(
testFile.toUri(),
FileUtils.readFileToString(testFile.toFile(), StandardCharsets.UTF_8),
context
));

module = spy((MDCommonModule) configuration.getModulesByObject().get(moduleFile.toUri()));
}
}
Loading