lib/Serge/Engine/Plugin/parse_js.pm

filter: Statementstmt 17.3% 8 / 46 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 28.5% 2 / 7 Podpod 0.0% 0 / 3 totaltotal 11.4% 10 / 87 SCAR 51.7
CC
15
Cov
11%
_unescape
72.0
parse_callback
20.0
_escape
20.0
CRAP 175.4
« lib/Serge/Mail.pm ↑ Summary lib/Serge/Sync/Plugin/VCS/mercurial.pm »
1 package Serge::Engine::Plugin::parse_js;
2 1 ▶use parent Serge::Engine::Plugin::Base::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = 2;
7  
8 sub name {
9 0 ▶ return 'Generic JavaScript object parser plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
10 }
11  
12 sub parse {
13 0 ▶ my ($self, $textref, $callbackref, $lang) = @_;
Subroutine parse: not called CC=2 SCAR=17.9
Pod undocumented
14  
15 0 ▶ die 'callbackref not specified' unless $callbackref;
Branch: 1 branch
BranchTrueFalseTotal
unless $callbackref 0 0 0
16  
17 0 my $translated_text = $$textref;
18  
19 # Parse strings
20  
21 # Example:
22 # "key" : "value",
23 # "key":"value",
24 # "key": "value", // comment
25  
26 0 $translated_text =~ s!^(\h*")(.+?)("\h*:\h*")((?:\\\\|\\"|[^"])+?)("\h*(?:,\h*)?(?://\h*(.*)\h*)?)$!$1.$2.$3.$self->parse_callback($callbackref, $4, undef, $2, $6, $lang).$5!mgie;
27  
28 0 return $translated_text;
29 }
30  
31 sub parse_callback {
32 0 ▶ my ($self, $callbackref, $string, $context, $key, $comment, $lang) = @_;
Subroutine parse_callback: not called CC=4 SCAR=30.0
Pod undocumented
33  
34 0 my @hint;
35 0 ▶ push @hint, $key if $key ne '' && $key ne $string;
Branch: 1 branch
BranchTrueFalseTotal
if $key ne "" and $key ne $string 0 0 0
Condition0%0/3
$key ne "" and $key ne $string
!ll&&!rl&&r
000
MC/DC0%0/2
$key ne "" and $key ne $string
$key ne "" $key ne $string
Truth table0%0/3
A and B
A= $key ne ""B= $key ne $string
ABresult
0X0
100
111
36 0 ▶ push @hint, $comment if $comment ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $comment ne "" 0 0 0
37  
38 0 $string =~ s/(\\\d{1,3}|\\x[0-9a-f]{2}|\\u[0-9a-f]{4}|\\u\{[0-9a-f]{1,6}\}|\\[bfnrtv"'\\])/_unescape($1)/sgie;
39  
40 0 my $translated_string = &$callbackref($string, $context, join("\n", @hint), undef, $lang, $key);
41  
42 0 $translated_string =~ s/(\\[bfrv]|[\\"'\n\t])/_escape($1)/sge;
43  
44 0 return $translated_string;
45 }
46  
47 sub _unescape {
48 0 ▶ my $s = shift;
Subroutine _unescape: not called CC=8 SCAR=42.8
49 0 ▶ $s eq '\\n' && return "\n";
Branch: 1 branch
BranchTrueFalseTotal
$s eq "\\n" and return "\n" 0 0 0
50 0 ▶ $s eq '\\t' && return "\t";
Branch: 1 branch
BranchTrueFalseTotal
$s eq "\\t" and return "\t" 0 0 0
51  
52 0 ▶ if ($s =~ m/^\\(\d{1,3})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\\(\d{1,3})$/) 0 0 0
53 0 return chr oct $1;
54 }
55  
56 0 ▶ if ($s =~ m/^\\x([0-9a-f]{2})$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\\x([0-9a-f]{2})$/i) 0 0 0
57 0 return chr hex $1;
58 }
59  
60 0 ▶ if ($s =~ m/^\\u([0-9a-f]{4})$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\\u([0-9a-f]{4})$/i) 0 0 0
61 0 return chr hex $1;
62 }
63  
64 0 ▶ if ($s =~ m/^\\u\{([0-9a-f]{1,6})\}$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\\u\{([0-9a-f]{1,6})\}$/i) 0 0 0
65 0 return chr hex $1;
66 }
67  
68 0 ▶ if ($s =~ m/^\\[bfrv]$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\\[bfrv]$/i) 0 0 0
69 0 return $s;
70 }
71  
72 0 $s =~ s/^\\//;
73 0 return $s;
74 }
75  
76 sub _escape {
77 0 ▶ my $s = shift;
Subroutine _escape: not called CC=4 SCAR=30.0
78 0 ▶ $s eq "\n" && return '\n';
Branch: 1 branch
BranchTrueFalseTotal
$s eq "\n" and return "\\n" 0 0 0
79 0 ▶ $s eq "\t" && return '\t';
Branch: 1 branch
BranchTrueFalseTotal
$s eq "\t" and return "\\t" 0 0 0
80 0 ▶ if ($s =~ m/^\\[bfrv]$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^\\[bfrv]$/i) 0 0 0
81 0 return $s;
82 }
83 0 return '\\'.$s;
84 }
85  
86 1 1;
« lib/Serge/Mail.pm ↑ Summary lib/Serge/Sync/Plugin/VCS/mercurial.pm »