From 7de58a1b15dfe093fd49533861b169ca624e0f52 Mon Sep 17 00:00:00 2001 From: Artur Ayukhanov Date: Sun, 23 Jul 2023 16:51:34 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D1=80=20=D0=B4=D0=BB=D1=8F=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE?= =?UTF-8?q?=D0=B6=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=BA=D0=B8=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D0=B5?= =?UTF-8?q?=D0=B9=20=D0=BE=D0=B1=D1=8A=D0=B5=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit по умолчанию, как и сейчас, модули объектов игнорятся #3111 --- .../UnusedLocalMethodDiagnostic.java | 16 ++++- .../UnusedLocalMethodDiagnostic_en.properties | 1 + .../UnusedLocalMethodDiagnostic_ru.properties | 1 + .../UnusedLocalMethodDiagnosticTest.java | 70 ++++++++++++++++++- 4 files changed, 86 insertions(+), 2 deletions(-) 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/diagnostics/UnusedLocalMethodDiagnostic_en.properties b/src/main/resources/com/github/_1c_syntax/bsl/languageserver/diagnostics/UnusedLocalMethodDiagnostic_en.properties index ebadb58a518..20347033cdb 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 \ No newline at end of file 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..d564ad0c4c4 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=Проверять модули объектов \ No newline at end of file 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())); + } } From 35108d517013a643a2b5bbb489c6fa16990f2f46 Mon Sep 17 00:00:00 2001 From: Artur Ayukhanov Date: Sun, 23 Jul 2023 16:52:55 +0300 Subject: [PATCH 2/3] precommit #3111 --- .../bsl/languageserver/configuration/parameters-schema.json | 6 ++++++ 1 file changed, 6 insertions(+) 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" From 719cae584f48983531a79a66481999c31da77cb6 Mon Sep 17 00:00:00 2001 From: Artur Ayukhanov Date: Wed, 13 Sep 2023 15:39:46 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../diagnostics/UnusedLocalMethodDiagnostic_en.properties | 2 +- .../diagnostics/UnusedLocalMethodDiagnostic_ru.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 20347033cdb..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,4 +1,4 @@ diagnosticMessage=Method "%s" is not called in the module diagnosticName=Unused local method attachableMethodPrefixes=Method prefixes (comma separated) -checkObjectModule=Check object modules \ No newline at end of file +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 d564ad0c4c4..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,4 +1,4 @@ diagnosticMessage=Метод "%s" не вызывается в теле модуля diagnosticName=Неиспользуемый локальный метод attachableMethodPrefixes=Префиксы подключаемых методов (через запятую) -checkObjectModule=Проверять модули объектов \ No newline at end of file +checkObjectModule=Проверять модули объектов