Completed
Push — master ( 606ca4...edcce7 )
by Kacper
05:58
created

TypeScript::setupRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
ccs 0
cts 18
cp 0
crap 2
1
<?php
2
3
4
namespace Kadet\Highlighter\Language;
5
6
7
use Kadet\Highlighter\Matcher\RegexMatcher;
8
use Kadet\Highlighter\Parser\Rule;
9
use Kadet\Highlighter\Parser\Token\Token;
10
11
class TypeScript extends JavaScript
12
{
13
    public function setupRules()
14
    {
15
        parent::setupRules();
16
17
        $this->rules->addMany([
18
            'symbol.type' => [
19
                new Rule(new RegexMatcher('/(?:[)\w]\??:|\bas\b)\s*(\w+)/si'), [
20
                    'context' => ['!meta.json', '!string', '!comment']
21
                ]),
22
                new Rule(new RegexMatcher('/(?:(?=(<\w+(?1)?>))|\G)<(\w+)/six', [ 2 => Token::NAME ]), [
23
                    'context' => ['!meta.json', '!string', '!comment']
24
                ]),
25
            ],
26
        ]);
27
28
        $this->rules
29
            ->rule('symbol.function')
30
            ->setMatcher(new RegexMatcher('/function\s+([a-z_]\w+)\s*(?>(<(?>(?2)|[^<>])+>)\s*)?\(/i'));
31
32
        /** @var \Kadet\Highlighter\Matcher\WordMatcher $keywords */
33
        $keywords = $this->rules->rule('keyword')->getMatcher();
34
        $keywords = $keywords->merge(['as']);
35
36
        $this->rules->rule('keyword')->setMatcher($keywords);
37
    }
38
39
40 1
    public function getIdentifier()
41
    {
42 1
        return 'typescript';
43
    }
44
45
    public static function getMetadata()
46
    {
47
        return [
48
            'name'      => ['ts', 'typescript'],
49
            'mime'      => ['application/typescript', 'application/x-typescript', 'text/x-typescript', 'text/typescript'],
50
            'extension' => ['*.ts', '*.tsx'],
51
        ];
52
    }
53
}