-
Notifications
You must be signed in to change notification settings - Fork 137
feat: new diagnostic InsertionWithoutChangeAndValidate #4397
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
Open
pravets
wants to merge
1
commit into
1c-syntax:develop
Choose a base branch
from
pravets:feat/new-diagnostic-insertion-without-change-and-validate
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Использование #Вставка без &ИзменениеИКонтроль (InsertionWithoutChangeAndValidate) | ||
|
|
||
| <!-- Блоки выше заполняются автоматически, не трогать --> | ||
| ## Описание диагностики | ||
|
|
||
| Директива `#Вставка` / `#КонецВставки` используется в теле метода (процедуры или функции), который не помечен аннотацией `&ИзменениеИКонтроль` (`&ChangeAndValidate`). Это приводит к ошибке компиляции модуля. | ||
|
|
||
| Директивы `#Вставка` и `#КонецВставки` предназначены для вставки кода расширения конфигурации. Платформа разрешает их использование только в методах с аннотацией `&ИзменениеИКонтроль`, которые явно декларируют намерение изменять код расширяемого объекта. | ||
|
|
||
| ## Примеры | ||
|
|
||
| ### Неправильно | ||
|
|
||
| ```bsl | ||
| Процедура ДобавитьРеквизит() | ||
|
|
||
| #Вставка | ||
| ... // код вставки | ||
| #КонецВставки | ||
|
|
||
| КонецПроцедуры | ||
| ``` | ||
|
|
||
| ### Правильно | ||
|
|
||
| ```bsl | ||
| &ИзменениеИКонтроль | ||
| Процедура ДобавитьРеквизит() | ||
|
|
||
| #Вставка | ||
| ... // код вставки | ||
| #КонецВставки | ||
|
|
||
| КонецПроцедуры | ||
| ``` | ||
|
|
||
| ## См. также | ||
|
|
||
| - [Стандарт 455: Структура модуля](https://its.1c.ru/db/v8std/content/455/hdoc) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Using #Insert without &ChangeAndValidate (InsertionWithoutChangeAndValidate) | ||
|
|
||
| <!-- Блоки выше заполняются автоматически, не трогать --> | ||
| ## Diagnostic description | ||
|
|
||
| The `#Insert` / `#EndInsert` directive is used inside a method (procedure or function) that is not annotated with `&ChangeAndValidate`. This causes a module compilation error. | ||
|
|
||
| The `#Insert` and `#EndInsert` directives are intended for configuration extension code insertion. The platform only allows them in methods annotated with `&ChangeAndValidate`, which explicitly declare the intent to modify the code of the extended object. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Incorrect | ||
|
|
||
| ```bsl | ||
| Procedure AddAttribute() | ||
|
|
||
| #Insert | ||
| ... // insertion code | ||
| #EndInsert | ||
|
|
||
| EndProcedure | ||
| ``` | ||
|
|
||
| ### Correct | ||
|
|
||
| ```bsl | ||
| &ChangeAndValidate | ||
| Procedure AddAttribute() | ||
|
|
||
| #Insert | ||
| ... // insertion code | ||
| #EndInsert | ||
|
|
||
| EndProcedure | ||
| ``` | ||
|
|
||
| ## See also | ||
|
|
||
| - [Standard 455: Module structure](https://its.1c.ru/db/v8std/content/455/hdoc) |
75 changes: 75 additions & 0 deletions
75
...1c_syntax/bsl/languageserver/diagnostics/InsertionWithoutChangeAndValidateDiagnostic.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2026 | ||
| * Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <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.github._1c_syntax.bsl.languageserver.context.symbol.annotations.Annotation; | ||
| import com.github._1c_syntax.bsl.languageserver.context.symbol.annotations.AnnotationKind; | ||
| import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata; | ||
| 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; | ||
| import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType; | ||
| import com.github._1c_syntax.bsl.languageserver.utils.Ranges; | ||
| import com.github._1c_syntax.bsl.parser.BSLLexer; | ||
| import org.eclipse.lsp4j.Position; | ||
|
|
||
| /** | ||
| * Директива {@code #Вставка} / {@code #КонецВставки} используется внутри метода | ||
| * (процедуры или функции), который не помечен аннотацией {@code &ИзменениеИКонтроль}. | ||
| * Без этой аннотации платформа не сможет скомпилировать модуль — вставка кода расширения | ||
| * разрешена только в методах, явно объявленных как изменяющие. | ||
| * | ||
| * @see <a href="https://its.1c.ru/db/v8std/content/455/hdoc">Стандарт 455</a> | ||
| */ | ||
| @DiagnosticMetadata( | ||
| type = DiagnosticType.ERROR, | ||
| severity = DiagnosticSeverity.CRITICAL, | ||
| scope = DiagnosticScope.BSL, | ||
| minutesToFix = 1, | ||
| tags = { | ||
| DiagnosticTag.ERROR | ||
| } | ||
| ) | ||
| public class InsertionWithoutChangeAndValidateDiagnostic extends AbstractDiagnostic { | ||
|
|
||
| @Override | ||
| public void check() { | ||
| var tokens = documentContext.getTokens(); | ||
|
|
||
| documentContext.getSymbolTree().getMethods() | ||
| .stream() | ||
| .filter(method -> method.getAnnotations().stream() | ||
| .map(Annotation::getKind) | ||
| .noneMatch(kind -> kind == AnnotationKind.CHANGEANDVALIDATE)) | ||
| .filter(method -> tokens.stream() | ||
| .filter(token -> token.getType() == BSLLexer.PREPROC_INSERT | ||
| || token.getType() == BSLLexer.PREPROC_ENDINSERT) | ||
| .anyMatch(token -> { | ||
| var tokenPosition = new Position(token.getLine() - 1, 0); | ||
| return Ranges.containsPosition(method.getRange(), tokenPosition); | ||
| })) | ||
| .forEach(method -> | ||
| diagnosticStorage.addDiagnostic(method.getSubNameRange(), | ||
| info.getMessage(method.getName()))); | ||
| } | ||
|
|
||
| } | ||
2 changes: 2 additions & 0 deletions
2
.../bsl/languageserver/diagnostics/InsertionWithoutChangeAndValidateDiagnostic_en.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| diagnosticMessage=Method "%s" uses #Insert / #EndInsert directive without &ChangeAndValidate annotation | ||
| diagnosticName=Using #Insert without &ChangeAndValidate |
2 changes: 2 additions & 0 deletions
2
.../bsl/languageserver/diagnostics/InsertionWithoutChangeAndValidateDiagnostic_ru.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| diagnosticMessage=В методе "%s" используется директива #Вставка / #КонецВставки без аннотации &ИзменениеИКонтроль | ||
| diagnosticName=Использование #Вставка без &ИзменениеИКонтроль |
49 changes: 49 additions & 0 deletions
49
...yntax/bsl/languageserver/diagnostics/InsertionWithoutChangeAndValidateDiagnosticTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * This file is a part of BSL Language Server. | ||
| * | ||
| * Copyright (c) 2018-2026 | ||
| * Alexey Sosnoviy <labotamy@gmail.com>, Nikita Fedkin <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 org.eclipse.lsp4j.Diagnostic; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.github._1c_syntax.bsl.languageserver.util.Assertions.assertThat; | ||
|
|
||
| class InsertionWithoutChangeAndValidateDiagnosticTest | ||
| extends AbstractDiagnosticTest<InsertionWithoutChangeAndValidateDiagnostic> { | ||
|
|
||
| InsertionWithoutChangeAndValidateDiagnosticTest() { | ||
| super(InsertionWithoutChangeAndValidateDiagnostic.class); | ||
| } | ||
|
|
||
| @Test | ||
| void test() { | ||
| List<Diagnostic> diagnostics = getDiagnostics(); | ||
|
|
||
| // Должна сработать только на БезИзменения (без аннотации &ИзменениеИКонтроль) | ||
| // СИзменением и ФункцияСИзменением — пропускаются (есть аннотация) | ||
| // ПустаяПроцедура — пропускается (нет #Вставка) | ||
| assertThat(diagnostics).hasSize(1); | ||
| assertThat(diagnostics, true) | ||
| .hasRange(9, 10, 9, 22); // БезИзменения | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/test/resources/diagnostics/InsertionWithoutChangeAndValidateDiagnostic.bsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| &ИзменениеИКонтроль | ||
| Процедура СИзменением() | ||
|
|
||
| #Вставка | ||
| // какой-то код | ||
| #КонецВставки | ||
|
|
||
| КонецПроцедуры | ||
|
|
||
| Процедура БезИзменения() | ||
|
|
||
| #Вставка | ||
| // какой-то код | ||
| #КонецВставки | ||
|
|
||
| КонецПроцедуры | ||
|
|
||
| &ИзменениеИКонтроль | ||
| Функция ФункцияСИзменением() | ||
|
|
||
| #Вставка | ||
| // код | ||
| #КонецВставки | ||
|
|
||
| КонецФункции | ||
|
|
||
| Процедура ПустаяПроцедура() | ||
| // нет вставки | ||
| КонецПроцедуры |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вот тут точно не прокатит. Это n*m поиск (токены в документе, количество методов).
Кажется, дешевле будет подписаться на узлы preproc insert/end и проверить что метод не имеет нужной директивы