Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Проверка модификации ключей структуры вне функции-конструктора #1054 #1393

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#### Код модулей

- Проверка на уникальность имени поля в doc-comment-field-name #1392
- Проверка модификации ключей структуры вне функции-конструктора #1054


#### Запросы

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Модификация ключа структуры вне функции-конструктора

Если Структура создана функцией-конструктором, то изменение состава ключей структуры методами
`Структура.Вставить("Ключ", ...); Структура.Удалить("Ключ"); Структура.Очистить();`
может приводить к ошибкам из-за неопределенности состава данных.

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

Неправильно удалять все ключи структуры, удалять ключ или заменять ключ внешней структуры.

```bsl
// @strict-types

Процедура Неправильно1() Экспорт

ТестоваяСтруктура = Конструктор();
ТестоваяСтруктура.Очистить();
ТестоваяСтруктура.Вставить("Ключ1", 10);
ТестоваяСтруктура.Удалить("Ключ2");

КонецПроцедуры

// Возвращаемое значение:
// Структура:
// * Ключ1 - Булево -
// * Ключ2 - Число -
Функция Конструктор() Экспорт

ТестоваяСтруктура = новый Структура("Ключ1, Ключ2", Истина, 10);
ТестоваяСтруктура.Вставить("Ключ2", 20);

Возврат ТестоваяСтруктура;

КонецФункции
```

## Правильно

Обращаться к существующему ключу необходимо напрямую и устанавливать новое значение.
Вместо удаления ключа необходимо устанавливать значение, которое представляет пустое или начальное значение.

```bsl
// @strict-types

Процедура Правильно1() Экспорт

ТестоваяСтруктура = Конструктор();
ТестоваяСтруктура.Ключ1 = false;
ТестоваяСтруктура.Ключ2 = -1;

КонецПроцедуры

// Возвращаемое значение:
// Структура:
// * Ключ1 - Булево -
// * Ключ2 - Число -
Функция Конструктор() Экспорт

ТестоваяСтруктура = новый Структура("Ключ1, Ключ2", Истина, 10);
ТестоваяСтруктура.Вставить("Ключ2", 20);

Возврат ТестоваяСтруктура;

КонецФункции
```

## См.
marmyshev marked this conversation as resolved.
Show resolved Hide resolved

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Structure key modification outside constructor function

If Structure was created by constructor function then changing the composition of the structure keys using methods
`Structure.Insert("Key", ...); Structure.Delete("Key"); Structure.Clear();`
can lead to errors due to the uncertainty of data composition.

marmyshev marked this conversation as resolved.
Show resolved Hide resolved

## Noncompliant Code Example

Should not clear all structure keys, delete key or replace key of external structure.

```bsl
// @strict-types

Procedure Incorrect1() Export

TestStucture = Constructor();
TestStucture.Clear();
TestStucture.Insert("Key1", 10);
TestStucture.Delete("Key2");

EndProcedure

// Returns:
// Structure:
// * Key1 - Boolean -
// * Key2 - Number -
Function Constructor() Export

TestStucture = new Structure("Key1, Key2", true, 10);
TestStucture.Insert("Key2", 20);

Return TestStucture;

EndFunction
```

## Compliant Solution

Access to existing key directly and set new value.
Instead of delete key should set a value that represents blank or initial value.

```bsl
// @strict-types

Procedure Correct1() Export

TestStucture = Constructor();
TestStucture.Key1 = false;
TestStucture.Key2 = -1;

EndProcedure

// Returns:
// Structure:
// * Key1 - Boolean -
// * Key2 - Number -
Function Constructor() Export

TestStucture = new Structure("Key1, Key2", true, 10);
TestStucture.Insert("Key2", 10);

Return TestStucture;

EndFunction
```

## See

4 changes: 4 additions & 0 deletions bundles/com.e1c.v8codestyle.bsl/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@
category="com.e1c.v8codestyle.bsl.strict"
class="com.e1c.v8codestyle.internal.bsl.ExecutableExtensionFactory:com.e1c.v8codestyle.bsl.strict.check.TypedValueAddingToUntypedCollectionCheck">
</check>
<check
category="com.e1c.v8codestyle.bsl.strict"
class="com.e1c.v8codestyle.internal.bsl.ExecutableExtensionFactory:com.e1c.v8codestyle.bsl.strict.check.StructureKeyModificationCheck">
</check>
<check
category="com.e1c.v8codestyle.bsl"
class="com.e1c.v8codestyle.internal.bsl.ExecutableExtensionFactory:com.e1c.v8codestyle.bsl.check.EventHandlerBooleanParamCheck">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ final class Messages
public static String StructureCtorValueTypeCheck_Structure_key__N__K__has_no_default_value_initializer;
public static String StructureCtorValueTypeCheck_Structure_key__N__K__value_initialized_with_empty_types;
public static String StructureCtorValueTypeCheck_title;
public static String StructureKeyModificationCheck_Check_Clear_method;
public static String StructureKeyModificationCheck_Check_Delete_method;
public static String StructureKeyModificationCheck_Check_Insert_method;
public static String StructureKeyModificationCheck_description;
public static String StructureKeyModificationCheck_error_message_Clear;
public static String StructureKeyModificationCheck_error_message_Delete;
public static String StructureKeyModificationCheck_error_message_Insert;
public static String StructureKeyModificationCheck_title;
public static String TypedValueAddingToUntypedCollectionCheck_description;
public static String TypedValueAddingToUntypedCollectionCheck_title;
public static String VariableTypeCheck_description;
Expand Down
Loading
Loading