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

Границы обращения к метаданным #1837

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
cd9ef8b
отработал Gradle, заготовки под диагностику
Sep 10, 2021
768ab7d
переименовал правильно
Sep 10, 2021
ea1e7dd
настройку думаю сделать мапой. читать ее из json?
Golovanoff Sep 12, 2021
b17d97f
попытался накидать чтение настройки из json. но не взлетает
Golovanoff Sep 21, 2021
2bc45e1
сделал параметр сразу мапой
Golovanoff Sep 22, 2021
f06515a
работающий вариант
Golovanoff Sep 22, 2021
65557a8
документация
Golovanoff Sep 22, 2021
fdad879
документация
Golovanoff Sep 23, 2021
8912ca2
убрал ворнинг на каст мапы
Golovanoff Sep 23, 2021
36f4e78
Merge pull request #1 from 1c-syntax/develop
Golovanoff Sep 23, 2021
f05deaa
Смержил с develop
Golovanoff Sep 23, 2021
1248793
Сделал диагностику отключенной по умолчанию
Golovanoff Sep 23, 2021
1379891
перетащил компиляцию паттернов и их фильтрацию в configure
Golovanoff Sep 26, 2021
84e4d9d
поправил импорты
Golovanoff Sep 26, 2021
7171d3f
оптимизация - переписал через visitFile
Golovanoff Sep 28, 2021
239f1c0
Убрал переносы строк из описания диагностики, вычистил левые строки и…
Golovanoff Sep 30, 2021
5ce7014
Поправил документацию и имя функции, убрал this.
Golovanoff Oct 7, 2021
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
75 changes: 75 additions & 0 deletions docs/diagnostics/MetadataBorders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Границы метаданных (MetadataBorders)

| Тип | Поддерживаются<br>языки | Важность | Включена<br>по умолчанию | Время на<br>исправление (мин) | Теги |
|:-------------:|:-----------------------------:|:----------------:|:------------------------------:|:-----------------------------------:|:--------:|
| `Дефект кода` | `BSL`<br>`OS` | `Информационный` | `Нет` | `1` | `design` |

## Параметры


| Имя | Тип | Описание | Значение<br>по умолчанию |
|:---------------------------:|:--------:|:---------------------------------------------------------------------------------------:|:------------------------------:|
| `metadataBordersParameters` | `Строка` | `{"регулярное выражение для операторов":"регулярное выражение для имени файла модуля"}` | `` |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

стало еще менее понятно :(

Предлагаю что-то вроде:

Строка в виде объекта JSON (в фигурных скобках) с перечислением пар "регулярное выражение для операторов": "регулярное выражение для имени файла модуля". Например, `{"Регистры?Сведений.КонтактнаяИнформация": "CommonModules/РаботаСКонтактами"}`.

Пускай пример будет прям в описании параметра, упростит понимание.

<!-- Блоки выше заполняются автоматически, не трогать -->
## Описание диагностики
Диагностика обращения к метаданным за пределами разрешенных границ. Нужна, если в компании установлены,
например, правила обращения к каким-то метаданным только из определенного общего модуля.

## Примеры
Получение контактной информации по внутренним правилам должно происходить только через функции
общего модуля РаботаСКонтактами. Тогда заданные настройки вида

```json
{"Регистры?Сведений.КонтактнаяИнформация":"CommonModules/РаботаСКонтактами"}
```

вызовут срабатывание диагностики на запросе с текстом

```
"ВЫБРАТЬ
...
ИЗ
РегистрСведений.КонтактнаяИнформация КАК КИ"
```

если этот запрос будет НЕ в общем модуле РаботаСКонтактами.
Так же эта диагностика с такой настройкой сработает и на оператор

```НаборЗаписей = РегистрыСведений.КонтактнаяИнформация.СоздатьНаборЗаписей();```

В настройках обе части - это регулярные выражения java.
Вторая часть настройки сравнивается с путем к файлу выгрузки конфигурации.

Можно запретить использование нескольких метаданных:

```json
{"Регистры?Сведений.КонтактнаяИнформация|Справочники?.Контакты":"CommonModules/РаботаСКонтактами"}
```

Настройки задаются в виде json-строки:
```json
{
"Регистры?Сведений.КонтактнаяИнформация":"CommonModules/РаботаСКонтактами",
"Справочники?.СтрокиСоединения":"DataProcessors/ВнешниеСоединения|CommonModules/ПодключениеК.*"
}
```

## Источники

## Сниппеты

<!-- Блоки ниже заполняются автоматически, не трогать -->
### Экранирование кода

```bsl
// BSLLS:MetadataBorders-off
// BSLLS:MetadataBorders-on
```

### Параметр конфигурационного файла

```json
"MetadataBorders": {
"metadataBordersParameters": ""
}
```
5 changes: 3 additions & 2 deletions docs/diagnostics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

## Список реализованных диагностик

Общее количество: **145**
Общее количество: **146**

* Потенциальная уязвимость: **4**
* Уязвимость: **4**
* Ошибка: **44**
* Дефект кода: **93**
* Дефект кода: **94**


| Ключ | Название | Включена по умолчанию | Важность | Тип | Тэги |
Expand Down Expand Up @@ -91,6 +91,7 @@
[LogicalOrInTheWhereSectionOfQuery](LogicalOrInTheWhereSectionOfQuery.md) | Использование логического "ИЛИ" в секции "ГДЕ" запроса | Да | Важный | Дефект кода | `sql`<br>`performance`<br>`standard`
[MagicDate](MagicDate.md) | Магические даты | Да | Незначительный | Дефект кода | `badpractice`<br>`brainoverload`
[MagicNumber](MagicNumber.md) | Магические числа | Да | Незначительный | Дефект кода | `badpractice`
[MetadataBorders](MetadataBorders.md) | Границы метаданных | Да | Информационный | Дефект кода | `error`
[MetadataObjectNameLength](MetadataObjectNameLength.md) | Имена объектов метаданных не должны превышать допустимой длины наименования | Да | Важный | Ошибка | `standard`
[MethodSize](MethodSize.md) | Ограничение на размер метода | Да | Важный | Дефект кода | `badpractice`
[MissingCodeTryCatchEx](MissingCodeTryCatchEx.md) | Конструкция "Попытка...Исключение...КонецПопытки" не содержит кода в исключении | Да | Важный | Ошибка | `standard`<br>`badpractice`
Expand Down
44 changes: 44 additions & 0 deletions docs/en/diagnostics/MetadataBorders.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Metadata borders (MetadataBorders)

| Type | Scope | Severity | Activated<br>by default | Minutes<br>to fix | Tags |
|:------------:|:-------------------:|:--------:|:-----------------------------:|:-----------------------:|:--------:|
| `Code smell` | `BSL`<br>`OS` | `Info` | `No` | `1` | `design` |

## Parameters


| Name | Type | Description | Default value |
|:---------------------------:|:--------:|:---------------------------------------------------:|:-------------:|
| `metadataBordersParameters` | `String` | `{"regex for statements":"regex for module names"}` | `` |
<!-- Блоки выше заполняются автоматически, не трогать -->
## Description
<!-- Описание диагностики заполняется вручную. Необходимо понятным языком описать смысл и схему работу -->

## Examples
<!-- В данном разделе приводятся примеры, на которые диагностика срабатывает, а также можно привести пример, как можно исправить ситуацию -->

## Sources
<!-- Необходимо указывать ссылки на все источники, из которых почерпнута информация для создания диагностики -->
<!-- Примеры источников

* Источник: [Стандарт: Тексты модулей](https://its.1c.ru/db/v8std#content:456:hdoc)
* Полезная информация: [Отказ от использования модальных окон](https://its.1c.ru/db/metod8dev#content:5272:hdoc)
* Источник: [Cognitive complexity, ver. 1.4](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) -->

## Snippets

<!-- Блоки ниже заполняются автоматически, не трогать -->
### Diagnostic ignorance in code

```bsl
// BSLLS:MetadataBorders-off
// BSLLS:MetadataBorders-on
```

### Parameter for config

```json
"MetadataBorders": {
"metadataBordersParameters": ""
}
```
5 changes: 3 additions & 2 deletions docs/en/diagnostics/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ To escape individual sections of code or files from triggering diagnostics, you

## Implemented diagnostics

Total: **145**
Total: **146**

* Security Hotspot: **4**
* Vulnerability: **4**
* Error: **44**
* Code smell: **93**
* Code smell: **94**


| Key | Name| Enabled by default | Severity | Type | Tags |
Expand Down Expand Up @@ -91,6 +91,7 @@ Total: **145**
[LogicalOrInTheWhereSectionOfQuery](LogicalOrInTheWhereSectionOfQuery.md) | Using a logical "OR" in the "WHERE" section of a query | Yes | Major | Code smell | `sql`<br>`performance`<br>`standard`
[MagicDate](MagicDate.md) | Magic dates | Yes | Minor | Code smell | `badpractice`<br>`brainoverload`
[MagicNumber](MagicNumber.md) | Magic numbers | Yes | Minor | Code smell | `badpractice`
[MetadataBorders](MetadataBorders.md) | Metadata borders | Yes | Info | Code smell | `error`
[MetadataObjectNameLength](MetadataObjectNameLength.md) | Metadata object names must not exceed the allowed length | Yes | Major | Error | `standard`
[MethodSize](MethodSize.md) | Method size | Yes | Major | Code smell | `badpractice`
[MissingCodeTryCatchEx](MissingCodeTryCatchEx.md) | Missing code in Raise block in "Try ... Raise ... EndTry" | Yes | Major | Error | `standard`<br>`badpractice`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2021
* Alexey Sosnoviy <labotamy@gmail.com>, Nikita Gryzlov <nixel2007@gmail.com> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* BSL Language Server is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* BSL Language Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BSL Language Server.
*/
package com.github._1c_syntax.bsl.languageserver.diagnostics;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
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.DiagnosticSeverity;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType;
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.utils.CaseInsensitivePattern;
import org.antlr.v4.runtime.tree.ParseTree;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@DiagnosticMetadata(
type = DiagnosticType.CODE_SMELL,
severity = DiagnosticSeverity.INFO,
minutesToFix = 1,
activatedByDefault = false,
tags = {
DiagnosticTag.DESIGN
}

)
public class MetadataBordersDiagnostic extends AbstractVisitorDiagnostic {

private static final String METADATA_BORDERS_DEFAULT = "";
nixel2007 marked this conversation as resolved.
Show resolved Hide resolved

@DiagnosticParameter(
type = String.class,
defaultValue = ""
)
private Map<Pattern, Pattern> metadataBordersParameters = mapFromJSON(METADATA_BORDERS_DEFAULT);

private List<Pattern> statementPatterns = Collections.emptyList();

private static Map<Pattern, Pattern> mapFromJSON(String userSettings) {
ObjectMapper mapper = new ObjectMapper();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

создание ObjectMapper на каждый configure - довольно дорогая операция. лучше его закэшировать или просто указать его конструкторе - его туда подложит спринг.

MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, String.class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapType тоже можно закэшировать в конструторе по принятому инстансу обджект-маппера

try {
Map<String, String> stringMap = mapper.readValue(userSettings, mapType);
return stringMap.entrySet().stream()
.filter(entry -> !entry.getKey().isBlank() && !entry.getValue().isBlank())
.collect(Collectors.toMap(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пробовал делать включать замер производительности? диагностика не вырывается в топ?

entry -> CaseInsensitivePattern.compile(entry.getKey()),
entry -> CaseInsensitivePattern.compile(entry.getValue())));
} catch (JsonProcessingException e) {
return Collections.emptyMap();
}
}

@Override
public void configure(Map<String, Object> configuration) {
metadataBordersParameters = mapFromJSON(
(String) configuration.getOrDefault("metadataBordersParameters", METADATA_BORDERS_DEFAULT));
}

@Override
public ParseTree visitFile(BSLParser.FileContext ctx) {
statementPatterns = metadataBordersParameters.entrySet().stream()
.filter(entry -> ! entry.getValue().matcher(documentContext.getUri().getPath()).find())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а почему пути к файлам используются? почему не mdoRef?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну и стоит проверять заполненность statementPatterns: для пустой делать возврат и не греть воздух

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а почему пути к файлам используются? почему не mdoRef?

вообще у нее scope захватывает OS, а для оскрипта mdoRef сейчас выглядит в виде uri. с другой стороны, вряд ли ее кто-то будет включать на os

Copy link
Contributor Author

@Golovanoff Golovanoff Oct 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

таки да. по-хорошему, если делать универсальненько и для людей, то надо вообще переделать, как Никита предлагал в телеге - на вхождение в подсистему.
но я такого пока не умею, это ж надо еще и тесты менять - конфу запихивать, разбираться с этим.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну и стоит проверять заполненность statementPatterns: для пустой делать возврат и не греть воздух

это сделано при формировании мапы паттернов. из стринговой мапы в мапу щаблонов попадают только те элементы, в которых и ключ, и значение не пустые

.map(Map.Entry::getKey)
.collect(Collectors.toList());

return super.visitFile(ctx);
}

@Override
public ParseTree visitStatement(BSLParser.StatementContext ctx){

statementPatterns.forEach(pattern -> {
Matcher matcher = pattern.matcher(ctx.getText());
while (matcher.find()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

вообще не понял - а цикл зачем?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

потому что матчер возвращает срабатывание по одному за раз

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а зачем одну и туже диагностику в цикле добавлять?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100 раз "addDiagnostic(ctx)"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

так, стоп, тут же где-то был другой код, когда в addDiagnostic передается start() и end() от матчера... или это в соседней диагностике?)

diagnosticStorage.addDiagnostic(ctx);
}
});

return super.visitStatement(ctx);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если ты проверяешь текст statement целиком, то нет смысла дергать super, т.к. вложенные стэйтменты проверят тот же самый кусок текста. т.е. здесь будет фонить на вложенных конструкциях вида

Если Истина Тогда
  Если Истина Тогда
    ЗапрещенныйВызов(); // тут кинет два срабатывания на одно и то же место
  КонецЕсли;
КонецЕсли;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,24 @@
},
"$id": "#/definitions/MagicNumber"
},
"MetadataBorders": {
"description": "Metadata borders",
"default": false,
"type": [
"boolean",
"object"
],
"title": "Metadata borders",
"properties": {
"metadataBordersParameters": {
"description": "{\"regex for statements\":\"regex for module names\"}",
"default": "",
"type": "string",
"title": "{\"regex for statements\":\"regex for module names\"}"
}
},
"$id": "#/definitions/MetadataBorders"
},
"MetadataObjectNameLength": {
"description": "Metadata object names must not exceed the allowed length",
"default": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@
"MagicNumber": {
"$ref": "parameters-schema.json#/definitions/MagicNumber"
},
"MetadataBorders": {
"$ref": "parameters-schema.json#/definitions/MetadataBorders"
},
"MetadataObjectNameLength": {
"$ref": "parameters-schema.json#/definitions/MetadataObjectNameLength"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
diagnosticMessage=Use metadata outside the borders
diagnosticName=Metadata borders
metadataBordersParameters={"regex for statements":"regex for module names"}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diagnosticMessage=Обращение к метаданным за пределами разрешенных границ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

может быть в сообщение стоит добавить регулярку/пару регулярок, на которой произошло срабатывание?

diagnosticName=Границы метаданных
metadataBordersParameters={"регулярное выражение для операторов":"регулярное выражение для имени файла модуля"}
Loading