Skip to content

Commit

Permalink
Add JSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Mar 28, 2024
1 parent b9739ac commit 18d6186
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 14 deletions.
2 changes: 0 additions & 2 deletions src/Languages/DocComment/DocCommentLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use Tempest\Highlight\Languages\Base\BaseLanguage;
use Tempest\Highlight\Languages\DocComment\Patterns\DocCommentTagPattern;
use Tempest\Highlight\Languages\Php\Injections\PhpGenericTypeInjection;

class DocCommentLanguage extends BaseLanguage
{
public function getInjections(): array
{
return [
...parent::getInjections(),
new PhpGenericTypeInjection(),
];
}

Expand Down
25 changes: 25 additions & 0 deletions src/Languages/JavaScript/Injections/JsDocInjection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\JavaScript\Injections;

use Tempest\Highlight\Highlighter;
use Tempest\Highlight\Injection;
use Tempest\Highlight\IsInjection;
use Tempest\Highlight\Languages\JavaScript\JsDocLanguage;

final readonly class JsDocInjection implements Injection
{
use IsInjection;

public function getPattern(): string
{
return '/(?<match>\/\*\*(.|\n)*?\*\/)/m';
}

public function parseContent(string $content, Highlighter $highlighter): string
{
return $highlighter->parse($content, new JsDocLanguage());
}
}
2 changes: 2 additions & 0 deletions src/Languages/JavaScript/JavaScriptLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tempest\Highlight\Languages\JavaScript;

use Tempest\Highlight\Languages\Base\BaseLanguage;
use Tempest\Highlight\Languages\JavaScript\Injections\JsDocInjection;
use Tempest\Highlight\Languages\JavaScript\Patterns\JsClassNamePattern;
use Tempest\Highlight\Languages\JavaScript\Patterns\JsDoubleQuoteValuePattern;
use Tempest\Highlight\Languages\JavaScript\Patterns\JsKeywordPattern;
Expand All @@ -24,6 +25,7 @@ public function getInjections(): array
{
return [
...parent::getInjections(),
new JsDocInjection(),
];
}

Expand Down
21 changes: 21 additions & 0 deletions src/Languages/JavaScript/JsDocLanguage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\JavaScript;

use Tempest\Highlight\Languages\DocComment\DocCommentLanguage;
use Tempest\Highlight\Languages\JavaScript\Patterns\JsDocParamNamePattern;
use Tempest\Highlight\Languages\JavaScript\Patterns\JsDocTypePattern;

class JsDocLanguage extends DocCommentLanguage
{
public function getPatterns(): array
{
return [
...parent::getPatterns(),
new JsDocTypePattern(),
new JsDocParamNamePattern(),
];
}
}
29 changes: 29 additions & 0 deletions src/Languages/JavaScript/Patterns/JsDocParamNamePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\JavaScript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest('@param {string} foo ', 'foo')]
#[PatternTest('@param {Object[]} foo', 'foo')]
#[PatternTest('@param {string} employees[].name', 'employees[].name')]
#[PatternTest('@param {string} employees[].name The name', 'employees[].name')]
final readonly class JsDocParamNamePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '@param\s(.*?)\s(?<match>(.*?))(\s|$)';
}

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::VALUE;
}
}
27 changes: 27 additions & 0 deletions src/Languages/JavaScript/Patterns/JsDocTypePattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Tempest\Highlight\Languages\JavaScript\Patterns;

use Tempest\Highlight\IsPattern;
use Tempest\Highlight\Pattern;
use Tempest\Highlight\PatternTest;
use Tempest\Highlight\Tokens\TokenTypeEnum;

#[PatternTest('@param {string} foo ', '{string}')]
#[PatternTest('@param {Object[]} foo', '{Object[]}')]
final readonly class JsDocTypePattern implements Pattern
{
use IsPattern;

public function getPattern(): string
{
return '(?<match>\{(.*?)\})';
}

public function getTokenType(): TokenTypeEnum
{
return TokenTypeEnum::TYPE;
}
}
9 changes: 9 additions & 0 deletions src/Languages/Php/PhpDocCommentLanguage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tempest\Highlight\Languages\Php;

use Tempest\Highlight\Languages\DocComment\DocCommentLanguage;
use Tempest\Highlight\Languages\Php\Injections\PhpGenericTypeInjection;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentGenericTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentParamTypePattern;
use Tempest\Highlight\Languages\Php\Patterns\PhpDocCommentReturnTypePattern;
Expand All @@ -14,6 +15,14 @@

class PhpDocCommentLanguage extends DocCommentLanguage
{
public function getInjections(): array
{
return [
...parent::getInjections(),
new PhpGenericTypeInjection(),
];
}

public function getPatterns(): array
{
return [
Expand Down
6 changes: 3 additions & 3 deletions tests/Languages/JavaScript/JavaScriptLanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ function getMandatedParams() {
<<<'TXT'
<span class="hl-comment">/**
* Class making something fun and easy.
* @param {string} arg1 An argument that makes this more interesting.
* @param {Array.&lt;number&gt;} arg2 List of numbers to be processed.
* @constructor
* <span class="hl-value">@param</span> <span class="hl-type">{string}</span> <span class="hl-value">arg1</span> An argument that makes this more interesting.
* <span class="hl-value">@param</span> <span class="hl-type">{Array.&lt;number&gt;}</span> <span class="hl-value">arg2</span> List of numbers to be processed.
* <span class="hl-value">@constructor</span>
*/</span>
<span class="hl-keyword">function</span> <span class="hl-property">someMethod</span>(obj) {
<span class="hl-comment">// ...</span>
Expand Down
14 changes: 5 additions & 9 deletions tests/targets/test.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
```php
```js
/**
* This function will do some things
*
* @template T
* @param class-string<T> $className the class' name
* @param int $id
* @return T|null
*
* That's about all
* Class making something fun and easy.
* @param {string} arg1 An argument that makes this more interesting.
* @param {Array.<number>} arg2 List of numbers to be processed.
* @constructor
*/
```

0 comments on commit 18d6186

Please sign in to comment.