Thread Schreibfehler in Label (14 answers)
Opened by barney at 2023-12-14 12:33

haj
 2023-12-15 18:44
#195678 #195678
User since
2015-01-07
531 Artikel
BenutzerIn

user image
2023-12-15T07:31:04 barney
Ja, in OTRS/Znuny/OTOBO gibt es dazu die Regel RequireLabel. Diese Regel verlangt aber nur dass Labels verwendet werden sollen. Und dass die Label mit einem ASCII Großbuchstaben anfangen. Ob die Zielmarke definiert ist wird dabei nicht überprüft.
SCNR: next __PACKAGE__ ist mit dieser Regel erlaubt.

2023-12-15T07:31:04 barney
Wenn man es richtig machen will dann braucht man eigentlich nur einen Stapel mit den angetroffenen Label und eine Stapel mit Zählern wie viele Labels es im aktuellen Scope, eval, sub, method, file gibt. Dann kann man beim Verlassen des Bereiches die Stapel wieder abbauen.

Ich habe ähnliches ziemlich oft im XML-Umfeld gebraucht... ist tatsächlich gar nicht so wild. Sorgfältige Tests und Dokumentation bleiben als Übungsaufgabe für den Leser.

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package Perl::Critic::Policy::GoodLabels;

use strict;
use warnings;

our $VERSION = 0.01;
use 5.032;
use Perl::Critic::Utils qw{};
use parent 'Perl::Critic::Policy';

my $description = q{Unknown Label};
my $explanation = <<'UNKNOWN_LABEL';
The label '%s' used as a target of next, last, or redo is not defined
in the current scope
UNKNOWN_LABEL
chomp $explanation;

sub supported_parameters { return; }
sub default_severity     { return $Perl::Critic::Utils::SEVERITY_HIGHEST; }
sub applies_to           { return 'PPI::Statement::Break' }

sub prepare_to_scan_document {
    my ( $self, $document ) = @_;

    return 1;
}

my %allowed_labels;

sub violates {
    my ( $self, $element, $document ) = @_;

    %allowed_labels or _collect_labels($document);

    my @children = $element->schildren();
    if (   $children[0]->content() ne 'next'
        && $children[0]->content() ne 'last'
        && $children[0]->content() ne 'redo' )
    {
        return;
    }

    my $label   = $children[0]->snext_sibling();
    my $content = $label->content;

    my $found = 0;
  CANDIDATE:
    for my $allowed ( @{ $allowed_labels{$content} } ) {
        if ( $allowed->contains($label) ) {
            $found = 1;
            last CANDIDATE;
        }
    }
    if ( !$found ) {
        return $self->violation( $description,
            sprintf( $explanation, $content ), $label );
    }

    return;
}

sub _collect_labels {
    my ($document) = @_;
    my $compounds = $document->find(q(Statement::Compound));
    for my $compound ( @{$compounds} ) {
        for my $label ( grep { $_->isa(q(PPI::Token::Label)) }
            $compound->children )
        {
            my $content = $label->content;
            $content =~ s/:$//xms;
            $allowed_labels{$content} //= [];
            push @{ $allowed_labels{$content} }, $compound;
        }
    }
    return;
}

1;

View full thread Schreibfehler in Label