Skip to content

Commit

Permalink
опция для проверки вложенных вызовов
Browse files Browse the repository at this point in the history
  • Loading branch information
artbear committed Nov 17, 2023
1 parent 69eac62 commit cb922b8
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.github._1c_syntax.bsl.languageserver.context.symbol.ModuleSymbol;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticParameter;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticScope;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag;
Expand All @@ -49,18 +50,18 @@
)
@RequiredArgsConstructor
public class PrivilegedModuleMethodCallDiagnostic extends AbstractDiagnostic {

private static final boolean VALIDATE_NESTED_CALLS = true;

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

private final ReferenceIndex referenceIndex;
private List<ModuleSymbol> privilegedModuleSymbols = new ArrayList<>();

private static boolean isReferenceToModules(Reference reference, List<ModuleSymbol> privilegedModuleSymbols) {
return reference.getSourceDefinedSymbol()
.flatMap(sourceDefinedSymbol -> sourceDefinedSymbol.getRootParent(SymbolKind.Module))
.filter(ModuleSymbol.class::isInstance)
.map(ModuleSymbol.class::cast)
.filter(privilegedModuleSymbols::contains)
.isPresent();
}

@Override
protected void check() {
if (privilegedModuleSymbols.isEmpty()){
Expand All @@ -71,7 +72,7 @@ protected void check() {
}

referenceIndex.getReferencesFrom(documentContext.getUri(), SymbolKind.Method).stream()
.filter(reference -> isReferenceToModules(reference, privilegedModuleSymbols))
.filter(this::isReferenceToModules)
.forEach(this::fireIssue);
}

Expand All @@ -89,6 +90,18 @@ private Optional<ModuleSymbol> getPrivilegedModuleSymbol(MDCommonModule mdCommon
.map(documentContext1 -> documentContext1.getSymbolTree().getModule());
}

private boolean isReferenceToModules(Reference reference) {
if (!validateNestedCalls && reference.getUri().equals(documentContext.getUri())){
return false;
}
return reference.getSourceDefinedSymbol()
.flatMap(sourceDefinedSymbol -> sourceDefinedSymbol.getRootParent(SymbolKind.Module))
.filter(ModuleSymbol.class::isInstance)
.map(ModuleSymbol.class::cast)
.filter(privilegedModuleSymbols::contains)
.isPresent();
}

private void fireIssue(Reference reference) {
diagnosticStorage.addDiagnostic(reference.getSelectionRange(),
info.getMessage(reference.getSymbol().getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,14 @@
"object"
],
"title": "Accessing privileged module methods",
"properties": {
"validateNestedCalls": {
"description": "Validate nested method calls from privileged modules",
"default": true,
"type": "boolean",
"title": "Validate nested method calls from privileged modules"
}
},
"$id": "#/definitions/PrivilegedModuleMethodCall"
},
"ProcedureReturnsValue": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
diagnosticMessage=Check the %s method access of the privileged module
diagnosticName=Accessing privileged module methods
diagnosticName=Accessing privileged module methods
validateNestedCalls=Validate nested method calls from privileged modules
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
diagnosticMessage=Проверьте обращение к методу %s привилегированного модуля
diagnosticName=Обращение к методам привилегированных модулей
validateNestedCalls=Проверять вложенные вызовы методов из привилегированных модулей
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,34 @@
import org.eclipse.lsp4j.Diagnostic;
import org.junit.jupiter.api.Test;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

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

class PrivilegedModuleMethodCallDiagnosticTest extends AbstractDiagnosticTest<PrivilegedModuleMethodCallDiagnostic> {
private static final String PATH_TO_METADATA = "src/test/resources/metadata/privilegedModules";
private static final String PATH_TO_MODULE_FILE = PATH_TO_METADATA + "/CommonModules/ПривилегированныйМодуль1/Ext/Module.bsl";

PrivilegedModuleMethodCallDiagnosticTest() {
super(PrivilegedModuleMethodCallDiagnostic.class);
}

@Test
void testWithoutMetadata() {
var diagnostics = getDiagnostics();
assertThat(diagnostics).isEmpty();
}

@Test
void test() {
initServerContext(PATH_TO_METADATA);

List<Diagnostic> diagnostics = getDiagnostics();
var diagnostics = getDiagnostics();

assertThat(diagnostics).hasSize(2);
assertThat(diagnostics, true)
Expand All @@ -48,9 +60,32 @@ void test() {
}

@Test
void testWithoutMetadata() {
void getNestedCalls() {
var diagnostics = getDiagnosticsAsCommonModule();
assertThat(diagnostics).hasSize(2);
assertThat(diagnostics, true)
.hasMessageOnRange("Проверьте обращение к методу ПубличнаяФункция привилегированного модуля", 15, 15, 31)
.hasMessageOnRange("Проверьте обращение к методу ПубличнаяПроцедура привилегированного модуля", 19, 4, 22);
}

List<Diagnostic> diagnostics = getDiagnostics();
@Test
void testParameterValidateNestedCalls() {
Map<String, Object> configuration = diagnosticInstance.getInfo().getDefaultConfiguration();
configuration.put("validateNestedCalls", false);
diagnosticInstance.configure(configuration);

var diagnostics = getDiagnosticsAsCommonModule();
assertThat(diagnostics).isEmpty();
}

private List<Diagnostic> getDiagnosticsAsCommonModule() {
Path moduleFile = Paths.get(PATH_TO_MODULE_FILE).toAbsolutePath();

initServerContext(PATH_TO_METADATA);

var documentContext = spy(getDocumentContext(diagnosticInstance.getClass().getSimpleName()));
when(documentContext.getUri()).thenReturn(moduleFile.toUri());

return getDiagnostics(documentContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
#Область СлужебныеПроцедурыИФункции

Функция ПриватнаяФункция()

Значение = ПубличнаяФункция();
КонецФункции

Процедура ПриватнаяПроцедура()

ПубличнаяПроцедура();
КонецПроцедуры

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

0 comments on commit cb922b8

Please sign in to comment.