diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic.java index 886c7f2ae55..725c1958979 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic.java @@ -48,7 +48,8 @@ type = DiagnosticType.CODE_SMELL, severity = DiagnosticSeverity.MAJOR, modules = { - ModuleType.CommonModule + ModuleType.CommonModule, + ModuleType.ObjectModule }, minutesToFix = 1, tags = { @@ -74,6 +75,7 @@ public class UnusedLocalMethodDiagnostic extends AbstractVisitorDiagnostic { AnnotationKind.BEFORE, AnnotationKind.CHANGEANDVALIDATE ); + private static final boolean CHECK_OBJECT_MODULE = false; @DiagnosticParameter( type = String.class, @@ -81,10 +83,18 @@ public class UnusedLocalMethodDiagnostic extends AbstractVisitorDiagnostic { ) 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 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) { @@ -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 collect = Trees.findAllRuleNodes(ctx, BSLParser.RULE_globalMethodCall) .stream() diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json index 9310767b6eb..49076e741f5 100644 --- a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/configuration/parameters-schema.json @@ -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" diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_en.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_en.properties index ebadb58a518..14fb4339fec 100644 --- a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_en.properties +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_en.properties @@ -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 diff --git a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_ru.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_ru.properties index bcebece21c0..07da1efa33f 100644 --- a/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_ru.properties +++ b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_ru.properties @@ -1,3 +1,4 @@ diagnosticMessage=Метод "%s" не вызывается в теле модуля diagnosticName=Неиспользуемый локальный метод attachableMethodPrefixes=Префиксы подключаемых методов (через запятую) +checkObjectModule=Проверять модули объектов diff --git a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnosticTest.java b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnosticTest.java index 747bf7b8243..681a61283e0 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnosticTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnosticTest.java @@ -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 { + 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 diagnostics = getDiagnostics(); + checkByDefault(diagnostics); + } + private static void checkByDefault(List diagnostics) { assertThat(diagnostics).hasSize(2); assertThat(diagnostics, true) .hasRange(1, 10, 24) .hasRange(70, 10, 41) ; + } + + @Test + void testObjectModuleByDefault() { + getObjectModuleDocumentContext(); + List diagnostics = getDiagnostics(documentContext); + assertThat(diagnostics).isEmpty(); } @Test @@ -65,4 +93,44 @@ void testConfigure() { .hasRange(63, 10, 39) ; } + + @Test + void testObjectModuleWithEnabledConfiguration() { + // given + getObjectModuleDocumentContext(); + + Map configuration = diagnosticInstance.getInfo().getDefaultConfiguration(); + configuration.put("checkObjectModule", true); + diagnosticInstance.configure(configuration); + + // when + List 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())); + } }