-
Notifications
You must be signed in to change notification settings - Fork 57
G5V8DT-26726 #1498
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
Merged
Merged
G5V8DT-26726 #1498
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6f3ba19
G5V8DT-26726 Добавлена валидация для аннотаций типов строковых литералов
1d6ba6d
G5V8DT-26726 Исправить по замечаниям ревью
c2450bd
G5V8DT-26726 Добавить фильтр по аннотации
b31bc66
G5V8DT-26726 Исправить по замечаниям ревью
7431ff7
G5V8DT-26726 Исправить по результатам тестирования
f693bfe
G5V8DT-26726 Исправить по замечаниям ревью
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
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
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
242 changes: 242 additions & 0 deletions
242
...c.v8codestyle.bsl/src/com/e1c/v8codestyle/bsl/check/StringLiteralTypeAnnotationCheck.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,242 @@ | ||
| /** | ||
| * Copyright (C) 2025, 1C | ||
| */ | ||
| package com.e1c.v8codestyle.bsl.check; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.eclipse.core.runtime.IProgressMonitor; | ||
| import org.eclipse.emf.ecore.EObject; | ||
| import org.eclipse.xtext.nodemodel.ICompositeNode; | ||
| import org.eclipse.xtext.nodemodel.ILeafNode; | ||
| import org.eclipse.xtext.nodemodel.INode; | ||
| import org.eclipse.xtext.nodemodel.util.NodeModelUtils; | ||
| import org.eclipse.xtext.util.Triple; | ||
|
|
||
| import com._1c.g5.v8.dt.bsl.documentation.comment.BslCommentUtils; | ||
| import com._1c.g5.v8.dt.bsl.model.Conditional; | ||
| import com._1c.g5.v8.dt.bsl.model.IfStatement; | ||
| import com._1c.g5.v8.dt.bsl.model.LoopStatement; | ||
| import com._1c.g5.v8.dt.bsl.model.Module; | ||
| import com._1c.g5.v8.dt.bsl.model.PreprocessorItem; | ||
| import com._1c.g5.v8.dt.bsl.model.RegionPreprocessor; | ||
| import com._1c.g5.v8.dt.bsl.model.StringLiteral; | ||
| import com._1c.g5.v8.dt.bsl.model.TryExceptStatement; | ||
| import com._1c.g5.v8.dt.bsl.stringliteral.contenttypes.BslBuiltInLanguagePreferences; | ||
| import com._1c.g5.v8.dt.bsl.stringliteral.contenttypes.IStringLiteralTypeComputer; | ||
| import com._1c.g5.v8.dt.bsl.stringliteral.contenttypes.LiteralType; | ||
| import com._1c.g5.v8.dt.bsl.stringliteral.contenttypes.TypeUtil; | ||
| import com._1c.g5.v8.dt.common.StringUtils; | ||
| import com._1c.g5.v8.dt.core.platform.IV8Project; | ||
| import com._1c.g5.v8.dt.core.platform.IV8ProjectManager; | ||
| import com.e1c.g5.v8.dt.check.BslDirectLocationIssue; | ||
| import com.e1c.g5.v8.dt.check.CheckComplexity; | ||
| import com.e1c.g5.v8.dt.check.DirectLocation; | ||
| import com.e1c.g5.v8.dt.check.ICheckParameters; | ||
| import com.e1c.g5.v8.dt.check.components.BasicCheck; | ||
| import com.e1c.g5.v8.dt.check.settings.IssueSeverity; | ||
| import com.e1c.g5.v8.dt.check.settings.IssueType; | ||
| import com.e1c.v8codestyle.check.CommonSenseCheckExtension; | ||
| import com.e1c.v8codestyle.internal.bsl.BslPlugin; | ||
| import com.google.inject.Inject; | ||
|
|
||
| /** | ||
| * Checks the correct placement of annotations for typing string literals. | ||
| * | ||
| * @author Babin Nikolay | ||
| * | ||
| */ | ||
| public class StringLiteralTypeAnnotationCheck | ||
| extends BasicCheck<Void> | ||
| { | ||
| private static final String CHECK_ID = "string-literal-type-annotation-invalid-place"; //$NON-NLS-1$ | ||
|
|
||
| @Inject | ||
| private IV8ProjectManager projectManager; | ||
|
|
||
| @Inject | ||
| private IStringLiteralTypeComputer typeComputer; | ||
|
|
||
| private final AtomicReference<Set<String>> annotations = new AtomicReference<>(); | ||
|
|
||
| @Override | ||
| public String getCheckId() | ||
| { | ||
| return CHECK_ID; | ||
| } | ||
|
|
||
| @Override | ||
| protected void configureCheck(CheckConfigurer builder) | ||
| { | ||
| builder.title(Messages.StringLiteralTypeAnnotationCheck_Title) | ||
| .complexity(CheckComplexity.NORMAL) | ||
| .severity(IssueSeverity.MAJOR) | ||
| .issueType(IssueType.WARNING) | ||
| .extension(new CommonSenseCheckExtension(getCheckId(), BslPlugin.PLUGIN_ID)) | ||
| .module(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void check(Object object, ResultAcceptor resultAceptor, ICheckParameters parameters, | ||
| IProgressMonitor monitor) | ||
| { | ||
| if (!(object instanceof Module)) | ||
| return; | ||
|
|
||
| Module module = (Module)object; | ||
|
|
||
| if (!isApplyTagsToEntireExpression(module)) | ||
| return; | ||
|
|
||
| ICompositeNode moduleNode = NodeModelUtils.findActualNodeFor(module); | ||
| List<StringLiteral> moduleStringLiterals = new ArrayList<>(); | ||
| List<INode> moduleAnnotations = new ArrayList<>(); | ||
| if (moduleNode != null) | ||
| { | ||
| for (ILeafNode child : moduleNode.getLeafNodes()) | ||
| { | ||
| if (monitor.isCanceled()) | ||
| return; | ||
|
|
||
| EObject semantic = NodeModelUtils.findActualSemanticObjectFor(child); | ||
| if (semantic instanceof StringLiteral literal) | ||
| { | ||
| moduleStringLiterals.add(literal); | ||
| } | ||
| if (child.isHidden() && BslCommentUtils.isCommentNode(child) && isAllowAnnotation(child.getText())) | ||
| { | ||
| moduleAnnotations.add(child); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| List<INode> invalidAnnotations = getInvalidAnnotations(monitor, moduleStringLiterals, moduleAnnotations); | ||
|
|
||
| addIssues(resultAceptor, module, invalidAnnotations, monitor); | ||
| } | ||
|
|
||
| private void addIssues(ResultAcceptor resultAceptor, Module module, List<INode> invalidAnnotations, | ||
| IProgressMonitor monitor) | ||
| { | ||
| for (INode annotation : invalidAnnotations) | ||
| { | ||
| if (monitor.isCanceled()) | ||
| return; | ||
|
|
||
| int index = annotation.getText().indexOf("@"); //$NON-NLS-1$ | ||
| int offset = annotation.getTotalOffset() + index; | ||
|
|
||
| int length = annotation.getText() | ||
| .trim() | ||
| .replaceFirst(BslCommentUtils.START_COMMENT_TAG_BSL, StringUtils.EMPTY) | ||
| .trim() | ||
| .toLowerCase() | ||
| .length(); | ||
|
|
||
| DirectLocation directLocation = new DirectLocation(offset, length, annotation.getStartLine(), module); | ||
| BslDirectLocationIssue directLocationIssue = | ||
| new BslDirectLocationIssue( | ||
| Messages.StringLiteralTypeAnnotationCheck_Incorrect_annotation_location, directLocation, | ||
| StringUtils.EMPTY); | ||
|
|
||
| resultAceptor.addIssue(directLocationIssue); | ||
| } | ||
| } | ||
|
|
||
| private List<INode> getInvalidAnnotations(IProgressMonitor monitor, List<StringLiteral> moduleStringLiterals, | ||
| List<INode> moduleAnnotations) | ||
| { | ||
| List<INode> invalidAnnotations = new ArrayList<>(); | ||
|
|
||
| Set<INode> correctAnnotations = new HashSet<>(); | ||
| for (StringLiteral literal : moduleStringLiterals) | ||
| { | ||
| if (monitor.isCanceled()) | ||
| return invalidAnnotations; | ||
|
|
||
| EObject literalParent = findLiteralParent(literal); | ||
|
|
||
| if (literalParent != null) | ||
| { | ||
| List<INode> rightLines = TypeUtil.getCommentLinesFromRight(literalParent) | ||
| .stream() | ||
| .filter(node -> isAllowAnnotation(node.getText())) | ||
| .toList(); | ||
| correctAnnotations.addAll(rightLines); | ||
| } | ||
| } | ||
|
|
||
| for (INode node : moduleAnnotations) | ||
| { | ||
| if (monitor.isCanceled()) | ||
| return invalidAnnotations; | ||
|
|
||
| if (!correctAnnotations.contains(node)) | ||
| { | ||
| invalidAnnotations.add(node); | ||
| } | ||
| } | ||
| return invalidAnnotations; | ||
| } | ||
|
|
||
| private boolean isApplyTagsToEntireExpression(EObject object) | ||
| { | ||
| IV8Project project = projectManager.getProject(object); | ||
|
|
||
| return project != null && project.getProject() != null | ||
| && BslBuiltInLanguagePreferences.isApplyTagsToEntireExpression(project.getProject()); | ||
| } | ||
|
|
||
| private EObject findLiteralParent(StringLiteral literal) | ||
| { | ||
| for (EObject e = literal; e != null; e = e.eContainer()) | ||
| { | ||
| EObject container = e.eContainer(); | ||
| //@formatter:off | ||
| if (container instanceof com._1c.g5.v8.dt.bsl.model.Method | ||
| || container instanceof RegionPreprocessor | ||
| || container instanceof PreprocessorItem | ||
| || container instanceof Conditional | ||
| || container instanceof IfStatement | ||
| || container instanceof TryExceptStatement | ||
| || container instanceof LoopStatement) | ||
| { | ||
| //@formatter:on | ||
| return e; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private boolean isAllowAnnotation(String text) | ||
| { | ||
| List<Triple<String, Integer, String>> commentAnnotations = TypeUtil.parseHeaderAnnotations(text); | ||
| for (Triple<String, Integer, String> commentAnnotation : commentAnnotations) | ||
| { | ||
| if (getAllowAnnotations().contains(commentAnnotation.getFirst().toLowerCase())) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private Set<String> getAllowAnnotations() | ||
| { | ||
| Set<String> allowAnnotations = annotations.get(); | ||
| if (allowAnnotations == null) | ||
| { | ||
| allowAnnotations = typeComputer.allTypes() | ||
| .stream() | ||
| .filter(LiteralType::allowAnnotation) | ||
| .map(type -> type.getName().toLowerCase()) | ||
| .collect(Collectors.toSet()); | ||
| if (!annotations.compareAndSet(null, allowAnnotations)) | ||
| allowAnnotations = annotations.get(); | ||
| } | ||
| return allowAnnotations; | ||
| } | ||
| } | ||
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
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
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
36 changes: 36 additions & 0 deletions
36
...com.e1c.v8codestyle.bsl.itests/resources/string-literal-annotations-invalid-locations.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,36 @@ | ||
|
|
||
| // Новая процедура. | ||
| // | ||
| // Параметры: | ||
| // ЯвляетсяЗаявкойНаОплату Является заявкой на оплату | ||
| // ИмяСвойства Имя свойства | ||
| // ЗначенияСвойства Значения свойства | ||
| // ОбъектXDTO Объект XDTO | ||
| Процедура НоваяПроцедура(ЯвляетсяЗаявкойНаОплату, ИмяСвойства, ЗначенияСвойства, ОбъектXDTO) | ||
|
|
||
| Значение = ""; // Дата1, Число какое то число | ||
| // yj dsq | ||
|
|
||
| Сообщить(Значение); | ||
|
|
||
| Если ЯвляетсяЗаявкойНаОплату И ИмяСвойства = "recipient" //@non-nls | ||
| Тогда // @fqn | ||
ukolabrother marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ЗначенияСвойства.Добавить(ОбъектXDTO, "Получатель"); // @nOn-nls | ||
| ИначеЕсли ТипЗнч(ОбъектXDTO[ИмяСвойства]) = Тип("СписокXDTO") | ||
|
|
||
| Тогда // @noN-nls-1 | ||
| Для //@form | ||
ukolabrother marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Каждого ЭлементСпискаXDTO | ||
| //@non-nls | ||
| Из ОбъектXDTO[ИмяСвойства] Цикл | ||
|
|
||
| ЗначенияСвойства.Добавить("ыва"); //@non-Nls | ||
| ЗначенияСвойства.Добавить("Литерал"); //@non-nLs | ||
|
|
||
| КонецЦикла; | ||
| Иначе | ||
| ЗначенияСвойства.Добавить(ОбъектXDTO[ИмяСвойства]); | ||
| КонецЕсли; | ||
|
|
||
| КонецПроцедуры | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.