Skip to content

Commit

Permalink
магические даты находим везде
Browse files Browse the repository at this point in the history
кроме присваиваний
closes 1c-syntax#3206
  • Loading branch information
artbear committed Dec 14, 2023
1 parent 1c83f52 commit 9858fb7
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.bsl.parser.BSLParserRuleContext;
import com.github._1c_syntax.utils.CaseInsensitivePattern;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -83,51 +81,72 @@ public void configure(Map<String, Object> configuration) {
authorizedDates.addAll(authD);
}

@Override
public ParseTree visitGlobalMethodCall(BSLParser.GlobalMethodCallContext ctx) {
Optional.of(ctx)
.filter(it -> methodPattern.matcher(it.methodName().getText()).matches())
.map(BSLParser.GlobalMethodCallContext::doCall)
.map(BSLParser.DoCallContext::callParamList)
.filter(callParamList -> paramPattern.matcher(callParamList.getText()).matches())
.ifPresent(this::checkExclAddDiagnostic);

return super.visitGlobalMethodCall(ctx);
private static Optional<BSLParserRuleContext> getExpression(Optional<BSLParser.ConstValueContext> contextOptional) {
return contextOptional
.map(BSLParserRuleContext::getParent)
.filter(context -> context.getChildCount() == 1)
.map(BSLParserRuleContext::getParent)
.filter(context -> context.getChildCount() == 1)
.filter(BSLParser.ExpressionContext.class::isInstance);
}

@Override
public ParseTree visitConstValue(BSLParser.ConstValueContext ctx) {
TerminalNode tNode = ctx.DATETIME();
if (tNode != null) {
checkExclAddDiagnostic(ctx);
}
private static boolean insideSimpleDateAssignment(Optional<BSLParserRuleContext> expressionContext) {
return expressionContext
.map(BSLParserRuleContext::getParent)
.filter(BSLParser.AssignmentContext.class::isInstance)
.isPresent();
}

return ctx;
private static boolean insideAssignmentWithDateMethodForSimpleDate(Optional<BSLParserRuleContext> expressionContext) {
return expressionContext
.map(BSLParserRuleContext::getParent) // callParam
.filter(context -> context.getChildCount() == 1)
.map(BSLParserRuleContext::getParent) // callParamList
.filter(context -> context.getChildCount() == 1)
.map(BSLParserRuleContext::getParent) // doCall
.map(BSLParserRuleContext::getParent) // globalCall - метод Дата(ХХХ)
.filter(BSLParser.GlobalMethodCallContext.class::isInstance)
.map(BSLParser.GlobalMethodCallContext.class::cast)
.filter(context -> methodPattern.matcher(context.methodName().getText()).matches())
.map(BSLParserRuleContext::getParent) // complexId
.filter(context -> context.getChildCount() == 1)
.map(BSLParserRuleContext::getParent) // member
.filter(context -> context.getChildCount() == 1)
.map(BSLParserRuleContext::getParent) // expression
.filter(context -> context.getChildCount() == 1)
.map(BSLParserRuleContext::getParent)
.filter(BSLParser.AssignmentContext.class::isInstance)
.isPresent();
}

private void checkExclAddDiagnostic(BSLParserRuleContext ctx){
String checked = ctx.getText();
if (checked != null && !isExcluded(checked)) {
ParserRuleContext expression;
if (ctx instanceof BSLParser.CallParamListContext){
expression = ctx.getParent().getParent().getParent().getParent().getParent();
} else {
expression = ctx.getParent().getParent();
@Override
public ParseTree visitConstValue(BSLParser.ConstValueContext ctx) {
var tNode = ctx.DATETIME();
var sNode = ctx.string();
if ((tNode != null || sNode != null) && isAccepted(ctx)) {
var contextOptional = Optional.of(ctx);
if (sNode != null) {
contextOptional = contextOptional
.filter(constValueContext -> paramPattern.matcher(constValueContext.getText()).matches());
}
if (expression instanceof BSLParser.ExpressionContext
&& (!isAssignExpression((BSLParser.ExpressionContext) expression))) {
diagnosticStorage.addDiagnostic(ctx.stop, info.getMessage(checked));

final var expressionContext = getExpression(contextOptional);
if (!insideSimpleDateAssignment(expressionContext)
&& !insideAssignmentWithDateMethodForSimpleDate(expressionContext)) {
diagnosticStorage.addDiagnostic(ctx, info.getMessage(ctx.getText()));
}
}
}

private boolean isExcluded(String sIn) {
String s = nonNumberPattern.matcher(sIn).replaceAll("");
return authorizedDates.contains(s);
return ctx;
}

private static boolean isAssignExpression(BSLParser.ExpressionContext expression) {
return (expression.getChildCount() <= 1);
private boolean isAccepted(BSLParserRuleContext ctx) {
String text = ctx.getText();
return text != null && !text.isEmpty() && !isExcluded(text);
}

private boolean isExcluded(String text) {
String s = nonNumberPattern.matcher(text).replaceAll("");
return authorizedDates.contains(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,40 @@ void test() {

List<Diagnostic> diagnostics = getDiagnostics();

assertThat(diagnostics).hasSize(5);
assertThat(diagnostics).hasSize(12);
assertThat(diagnostics, true)
.hasRange(1, 12, 1, 22)
.hasRange(2, 12, 2, 28)
.hasRange(3, 7, 3, 17)
.hasRange(4, 14, 4, 24)
.hasRange(13, 7, 13, 26);
.hasRange(1, 12, 22)
.hasRange(2, 12, 28)
.hasRange(3, 7, 17)
.hasRange(4, 14, 24)
.hasRange(13, 7, 26)
.hasRange(15, 87, 97)
.hasRange(16, 80, 90)
.hasRange(16, 92, 102)
.hasRange(17, 22, 32)
.hasRange(18, 19, 35)
.hasRange(19, 10, 26)
.hasRange(19, 29, 39);

}

@Test
void testConfigure() {

Map<String, Object> configuration = diagnosticInstance.getInfo().getDefaultConfiguration();
configuration.put("authorizedDates", "00010101,00010101000000,000101010000,12340101, 00020101 ,,");
configuration.put("authorizedDates", "00010101,00010101000000,000101010000,00050101,00020501121314,12340101, 00020101 ,,");
diagnosticInstance.configure(configuration);

List<Diagnostic> diagnostics = getDiagnostics();

assertThat(diagnostics).hasSize(3);
assertThat(diagnostics).hasSize(5);
assertThat(diagnostics, true)
.hasRange(2, 12, 2, 28)
.hasRange(3, 7, 3, 17)
.hasRange(13, 7, 13, 26);
.hasRange(2, 12, 28)
.hasRange(3, 7, 17)
.hasRange(13, 7, 26)
.hasRange(13, 7, 26)
.hasRange(17, 22, 32)
.hasRange(19, 29, 39);

}
}
8 changes: 7 additions & 1 deletion src/test/resources/diagnostics/MagicDateDiagnostic.bsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@
КонецЦикла;
День = Дата("00010101") + Шаг; // исключение
День = '0001-01why not?01' + Шаг; // исключение
День = '0001-01why not?02' + Шаг; // ошибка
День = '0001-01why not?02' + Шаг; // ошибка

ИменаПараметров = СтроковыеФункции.РазложитьСтрокуВМассивПодстрок(ИмяПараметра, , Дата("00050101")); // замечание
ИменаПараметров = СтроковыеФункции.РазложитьСтрокуВМассивПодстрок(ИмяПараметра, "00050101", "00050101"); // замечание
Настройки = Настройки('12350101'); // замечание
Настройки.Свойство("00020501121314", ЗначениеЕдиничногоПараметра); // замечание
Выполнить("00020501121314" + '12350101'); // замечание

0 comments on commit 9858fb7

Please sign in to comment.