Skip to content

Commit

Permalink
1C-Company#1117 Доработана проверка SelfReferenceCheck
Browse files Browse the repository at this point in the history
Доработана проверка SelfReferenceCheck: добавлена опция, позволяющая
пропускать проверку для модулей объектов, наборов записей, менеджеров
значений.
  • Loading branch information
VAGoncharov committed Mar 14, 2023
1 parent 582e2e9 commit 7d22eb7
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Self reference is excessive

Excessive usage of self reference with use of `ThisObject` (when referencing method, property or attribute)
Excessive usage of self reference with use of `ThisObject` (when referencing method, property or attribute).

Check common module, object module, recordset module, value manager module, form module.
Check of onject module, recordset module and value manager module can be disable, if
`Check object (recordset, value manager) module` isn't set.

For form modules only check self reference for methods and existing properties
(if `Check only existing form properties` parameter is set, otherwise, check for all cases)
(if `Check only existing form properties` parameter is set, otherwise, check for all cases).

## Noncompliant Code Example

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Избыточное использование псевдонима "ЭтотОбъект"

Избыточное обращение внутри модуля через псевдоним "ЭтотОбъект" (к методу, свойству или реквизиту)
Избыточное обращение внутри модуля через псевдоним "ЭтотОбъект" (к методу, свойству или реквизиту).

Проверяются общие модули, модули объектов, наборов записей, модули менеджеров значений и модули форм.
Проверку модулей объектов, наборов записей и менеджеров значений можно отключить
через параметр `Проверять модули объектов (наборов записей, менеджеров значений)`.

Для модулей форм проверяется только обращение к методам и существующим свойствам
(в случае если установлен параметр `Проверять только существовующие свойства в форме`, инчае проверяются все случаи)
(в случае если установлен параметр `Проверять только существовующие свойства в форме`, иначе проверяются все случаи).

## Неправильно

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ final class Messages
public static String QueryInLoop_Loop_has_query;
public static String QueryInLoop_title;

public static String SelfReferenceCheck_check_object_module;

public static String SelfReferenceCheck_check_only_existing_form_properties;

public static String SelfReferenceCheck_Description;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2022, 1C-Soft LLC and others.
* Copyright (C) 2023, 1C-Soft LLC and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -47,6 +47,7 @@
* (if PARAMETER_CHECK_ONLY_EXISTING_FORM_PROPERTIES is set, otherwise, check for all cases)
*
* @author Maxim Galios
* @author Vadim Goncharov
*
*/
public class SelfReferenceCheck
Expand All @@ -57,8 +58,13 @@ public class SelfReferenceCheck

private static final Collection<String> EXCESSIVE_NAMES = Set.of("ЭтотОбъект", "ThisObject"); //$NON-NLS-1$ //$NON-NLS-2$

private static final Set<ModuleType> OBJECT_MODULE_TYPE_LIST =
Set.of(ModuleType.OBJECT_MODULE, ModuleType.RECORDSET_MODULE, ModuleType.VALUE_MANAGER_MODULE);

public static final String PARAMETER_CHECK_ONLY_EXISTING_FORM_PROPERTIES = "checkOnlyExistingFormProperties"; //$NON-NLS-1$

public static final String PARAMETER_CHEKC_OBJECT_MODULE = "checkObjectModule"; //$NON-NLS-1$

private DynamicFeatureAccessComputer dynamicFeatureAccessComputer;

/**
Expand Down Expand Up @@ -88,10 +94,14 @@ protected void configureCheck(CheckConfigurer builder)
.severity(IssueSeverity.MINOR)
.issueType(IssueType.CODE_STYLE)
.extension(new StandardCheckExtension(467, getCheckId(), BslPlugin.PLUGIN_ID))
.extension(ModuleTypeFilter.onlyTypes(ModuleType.COMMON_MODULE, ModuleType.OBJECT_MODULE,
ModuleType.VALUE_MANAGER_MODULE, ModuleType.RECORDSET_MODULE, ModuleType.FORM_MODULE))
.module()
.checkedObjectType(DYNAMIC_FEATURE_ACCESS)
.parameter(PARAMETER_CHECK_ONLY_EXISTING_FORM_PROPERTIES, Boolean.class, Boolean.TRUE.toString(),
Messages.SelfReferenceCheck_check_only_existing_form_properties);
Messages.SelfReferenceCheck_check_only_existing_form_properties)
.parameter(PARAMETER_CHEKC_OBJECT_MODULE, Boolean.class, Boolean.TRUE.toString(),
Messages.SelfReferenceCheck_check_object_module);
}

@Override
Expand All @@ -108,17 +118,25 @@ protected void check(Object object, ResultAcceptor resultAceptor, ICheckParamete

StaticFeatureAccess source = (StaticFeatureAccess)featureAccessSource;

if (isReferenceExcessive(dynamicFeatureAccess, source,
parameters.getBoolean(PARAMETER_CHECK_ONLY_EXISTING_FORM_PROPERTIES)))
if (isReferenceExcessive(dynamicFeatureAccess, source, parameters))
{
resultAceptor.addIssue(Messages.SelfReferenceCheck_Issue,
source);
resultAceptor.addIssue(Messages.SelfReferenceCheck_Issue, source);
}
}

private boolean isReferenceExcessive(DynamicFeatureAccess dynamicFeatureAccess, StaticFeatureAccess source,
boolean checkOnlyExistingFormProperties)
ICheckParameters parameters)
{

boolean checkOnlyExistingFormProperties = parameters.getBoolean(PARAMETER_CHECK_ONLY_EXISTING_FORM_PROPERTIES);
boolean checkObjectModule = parameters.getBoolean(PARAMETER_CHEKC_OBJECT_MODULE);

Module module = EcoreUtil2.getContainerOfType(dynamicFeatureAccess, Module.class);

if (!checkObjectModule && OBJECT_MODULE_TYPE_LIST.contains(module.getModuleType()))
{
return false;
}
if (!EXCESSIVE_NAMES.contains(source.getName()))
{
return false;
Expand All @@ -128,8 +146,6 @@ private boolean isReferenceExcessive(DynamicFeatureAccess dynamicFeatureAccess,
return true;
}

Module module = EcoreUtil2.getContainerOfType(dynamicFeatureAccess, Module.class);

return !(module.getModuleType() == ModuleType.FORM_MODULE
&& isEmptySource(dynamicFeatureAccessComputer.resolveObject(dynamicFeatureAccess, module.environments())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,8 @@ SelfReferenceCheck_Issue = Excessive usage of self reference (when referencing m

SelfReferenceCheck_Title = Excessive self reference

SelfReferenceCheck_check_object_module = Check object (recordset, value manager) module

SelfReferenceCheck_check_only_existing_form_properties = Check only existing form properties

ServerExecutionSafeModeCheck_description = Safe mode is not enabled when calling "Execute" or "Eval'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,8 @@ SelfReferenceCheck_Issue = Избыточное обращение внутри

SelfReferenceCheck_Title = Избыточное использование псевдонима "ЭтотОбъект"

SelfReferenceCheck_check_object_module = Проверять модули объектов (наборов записей, менеджеров значений)

SelfReferenceCheck_check_only_existing_form_properties = Проверять только существующие свойства в форме

ServerExecutionSafeModeCheck_description = Отсутствует включение безопасного режима перед вызовом метода "Выполнить" или "Вычислить"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (C) 2022, 1C-Soft LLC and others.
* Copyright (C) 2023, 1C-Soft LLC and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -35,6 +35,7 @@
* The test for class {@link SelfReferenceCheck}.
*
* @author Maxim Galios
* @author Vadim Goncharov
*
*/
public class SelfReferenceCheckTest
Expand Down Expand Up @@ -94,12 +95,8 @@ public void testFormModule() throws Exception

IDtProject dtProject = getProject();
IProject project = dtProject.getWorkspaceProject();

ICheckSettings settings = checkRepository.getSettings(new CheckUid(getCheckId(), BslPlugin.PLUGIN_ID), project);
settings.getParameters()
.get(SelfReferenceCheck.PARAMETER_CHECK_ONLY_EXISTING_FORM_PROPERTIES)
.setValue(Boolean.toString(false));
checkRepository.applyChanges(Collections.singleton(settings), project);
changeProjectSetting(project, SelfReferenceCheck.PARAMETER_CHECK_ONLY_EXISTING_FORM_PROPERTIES,
Boolean.toString(false));
waitForDD(dtProject);

List<Marker> markersAfterSettingsChange = getMarkers(FORM_MODULE_FILE_NAME);
Expand Down Expand Up @@ -132,6 +129,15 @@ public void testObjectModule() throws Exception
assertEquals("8", markers.get(1).getExtraInfo().get(IExtraInfoKeys.TEXT_EXTRA_INFO_LINE_KEY));
assertEquals("9", markers.get(2).getExtraInfo().get(IExtraInfoKeys.TEXT_EXTRA_INFO_LINE_KEY));
assertEquals("9", markers.get(3).getExtraInfo().get(IExtraInfoKeys.TEXT_EXTRA_INFO_LINE_KEY));

IDtProject dtProject = getProject();
IProject project = dtProject.getWorkspaceProject();
changeProjectSetting(project, SelfReferenceCheck.PARAMETER_CHEKC_OBJECT_MODULE, Boolean.toString(false));
waitForDD(dtProject);

List<Marker> markersAfterSettingsChange = getMarkers(OBJECT_MODULE_FILE_NAME);
assertEquals(0, markersAfterSettingsChange.size());

}

private List<Marker> getMarkers(String moduleFileName)
Expand All @@ -146,4 +152,12 @@ private List<Marker> getMarkers(String moduleFileName)
.filter(marker -> chekcId.equals(getCheckIdFromMarker(marker, getProject())))
.collect(Collectors.toList());
}

private void changeProjectSetting(IProject project, String parameter, String value)
{
ICheckSettings settings = checkRepository.getSettings(new CheckUid(getCheckId(), BslPlugin.PLUGIN_ID), project);
settings.getParameters().get(parameter).setValue(value);
checkRepository.applyChanges(Collections.singleton(settings), project);
}

}

0 comments on commit 7d22eb7

Please sign in to comment.