Skip to content

Commit

Permalink
Merge pull request #3112 from artbear/unused-method-param
Browse files Browse the repository at this point in the history
UnusedLocalMethod - "Неиспользуемый локальный метод" - поддержка модулей объектов через параметр правила
  • Loading branch information
theshadowco authored Oct 4, 2023
2 parents 5c1b485 + 719cae5 commit c660dd8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
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 @@ -1902,6 +1902,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()));
}
}

0 comments on commit c660dd8

Please sign in to comment.