blib/lib/Text/Nunjucks/Compiler.pm

filter: Statementstmt 75.5% 37 / 49 Branchbran 33.3% 4 / 12 Conditioncond 28.5% 4 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 84.6% 11 / 13 Podpod 0.0% 0 / 5 totaltotal 56.4% 57 / 101 CC 21 SCAR 23.3
CC
21
Cov
60%
_ext_preprocess
17.6
_ext_preprocess
17.6
compile
9.4
CRAP 10.3
« blib/lib/Text/Nunjucks/Lib.pm ↑ Summary blib/lib/Text/Nunjucks/Compiler/Compiler.pm »
1 package Text::Nunjucks::Compiler;
2 # ABSTRACT: 1:1 port of Nunjucks src/compiler.js -- emits Perl source
3  
4 16 our $VERSION = '0.001';
5  
6 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Text::Nunjucks::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Text::Nunjucks::Transformer;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Text::Nunjucks::Compiler::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # 1:1 port of upstream src/compiler.js. compiler.js exports { compile, Compiler };
14 # the port mirrors that shape the way Parser.pm mirrors parser.js: the module
15 # functions live here on Text::Nunjucks::Compiler (compile(), and
16 # compiler_class() for the exported Compiler class), while the class itself is
17 # the Moo class Text::Nunjucks::Compiler::Compiler in its own file (ADR 0002,
18 # object.js not ported; ADR 0020).
19 #
20 # The class carries the emission rules: what compiler.js's JavaScript output
21 # becomes in Perl is set out at the top of Text/Nunjucks/Compiler/Compiler.pm.
22  
23 #### module.exports.compile -- compile(src, asyncFilters, extensions, name, opts)
24  
25 9364 ▶sub compile {
Subroutine compile: called CC=5 SCAR=22.5
Pod undocumented
26 0 ▶ my ( $self, $src, $async_filters, $extensions, $name, $opts ) = @_;
Subroutine compile: not called CC=5 SCAR=22.5
Pod undocumented
27 0 ▶ $opts //= {};
Condition25%1/4
$opts //= {}
l!l
93640
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
Truth table0%0/2
A
A= $opts
Aresult
11
00
28  
29 my $c = $self->compiler_class->new(
30 template_name => $name,
31 throw_on_undefined => $opts->{throwOnUndefined},
32 0 );
33  
34 # Run the extension preprocessors against the source.
35 0 my $processed_src = $src;
36 0 ▶ for my $ext ( @{ $extensions || [] } ) {
Condition50%2/4
$extensions || []
l!l
927985
$extensions || []
l!l
00
MC/DC100%1/1
$extensions || []
$extensions
MC/DC0%0/1
$extensions || []
$extensions
Truth table100%2/2
A
A= $extensions
Aresult
11
00
Truth table0%0/2
A
A= $extensions
Aresult
11
00
37 0 my $pre = $self->_ext_preprocess($ext);
38 0 ▶ $processed_src = $pre->($processed_src) if $pre;
Branch: 2 branches
BranchTrueFalseTotal
if $pre 0 17 17
if $pre 0 0 0
39 }
40  
41 $c->compile(
42 0 $self->transformer->transform(
43 $self->parser->parse( $processed_src, $extensions, $opts ),
44 $async_filters,
45 $name
46 )
47 );
48 0 return $c->get_code;
49 }
50  
51 # Collaborators named once, reached through the invocant (house rule).
52 9364 ▶sub parser { 'Text::Nunjucks::Parser' }
Subroutine parser: called CC=1 SCAR=0.0
Pod undocumented
53 9364 ▶sub transformer { 'Text::Nunjucks::Transformer' }
Subroutine transformer: called CC=1 SCAR=0.0
Pod undocumented
54 9364 ▶sub compiler_class { 'Text::Nunjucks::Compiler::Compiler' }
Subroutine compiler_class: called CC=1 SCAR=0.0
Pod undocumented
55  
56 # ext.preprocess -- extension representation is open (k8/k22); read it as an
57 # object method or a hash coderef, else nothing, and report.
58 17 ▶sub _ext_preprocess {
Subroutine _ext_preprocess: called CC=7 SCAR=28.7
59 17 my ( $self, $ext ) = @_;
60 17 ▶ return undef unless defined $ext && ref $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext and ref $ext 0 17 17
Condition33%1/3
defined $ext and ref $ext
!ll&&!rl&&r
0017
MC/DC0%0/2
defined $ext and ref $ext
defined $ext ref $ext
Truth table33%1/3
A and B
A= defined $extB= ref $ext
ABresult
0X0
100
111
61 17 ▶ if ( blessed($ext) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($ext)) 17 0 17
62 0 ▶ return $ext->can('preprocess') ? sub { $ext->preprocess(@_) } : undef;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
$ext->can('preprocess') ? : 0 17 17
63 }
64 0 ▶ return ( ref $ext eq 'HASH' && $ext->{preprocess} ) ? $ext->{preprocess} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $ext eq 'HASH' && $ext->{'preprocess'} ? : 0 0 0
Condition0%0/3
ref $ext eq "HASH" and $ext->{"preprocess"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ext eq "HASH" and $ext->{"preprocess"}
ref $ext eq "HASH" $ext->{"preprocess"}
Truth table0%0/3
A and B
A= ref $ext eq "HASH"B= $ext->{"preprocess"}
ABresult
0X0
100
111
65 }
66  
67  
68 16 1;
69  
70 __END__
« blib/lib/Text/Nunjucks/Lib.pm ↑ Summary blib/lib/Text/Nunjucks/Compiler/Compiler.pm »