Regex для преобразования JSDoc из coffeescript в javascript - PullRequest
0 голосов
/ 12 ноября 2018

Я пытаюсь преобразовать проект из coffeescript в javascript.Я успешно использовал инструмент decaffeinate, но что-то скучно преобразовывать вручную: комментарии JSdoc

В coffescript мы привыкли писать их, как в формализме этого примера:

##
# This will remove the given file from the machine attachments list. If the file was previously uploaded
# to the server, it will be marked for deletion on the server. Otherwise, it will be simply truncated from
# the attachments array.
# @param file {Object} the file to delete
# @returns {boolean}
##

После использования decaffeinate они были преобразованы в нечто вроде этого:

// #
// This will remove the given file from the machine attachments list. If the file was previously uploaded
// to the server, it will be marked for deletion on the server. Otherwise, it will be simply truncated from
// the attachments array.
// @param file {Object} the file to delete
// @returns {boolean}
// #

Поэтому я попытался написать регулярное выражение perl, чтобы они выглядели как стандартный синтаксис JSdoc.Но я застрял с центральными линиями: я не могу найти способ поставить звезду в начале каждой строки ... Вот лучшее, к чему я пришел:

find . -type f -name "*.js" | xargs perl -0777 -i -pe 's~// #\n( +// (.+\n)+)( +)// #~/**\n$1$3 */~gm;'

Какие результатыв:

/**
// This will remove the given file from the machine attachments list. If the file was previously uploaded
// to the server, it will be marked for deletion on the server. Otherwise, it will be simply truncated from
// the attachments array.
// @param file {Object} the file to delete
// @returns {boolean}
 */

Но в идеале его следует преобразовать в:

/**
 * This will remove the given file from the machine attachments list. If the file was previously uploaded
 * to the server, it will be marked for deletion on the server. Otherwise, it will be simply truncated from
 * the attachments array.
 * @param file {Object} the file to delete
 * @returns {boolean}
 */

Вот пример полного файла для преобразования: https://gist.github.com/sylvainbx/96e53b879b4dd7ef7cdd153c3fc3c5b8

Любая помощьбыл бы оценен:)

1 Ответ

0 голосов
/ 12 ноября 2018

Вы не можете легко сделать это в пределах одного регулярного выражения, так как вам нужно захватить и заменить первое // # в отличие от второго // #. Следующий скрипт показывает, как вы можете вызвать подпрограмму справа от замены, и в этой подпрограмме вы можете использовать три простых регулярных выражения для преобразования вашего комментария в JSdoc:

#!perl
use strict;
use warnings;

my $js = <<'JS';

foo

// #
// This will remove the given file from the machine attachments list. If the file was previously uploaded
// to the server, it will be marked for deletion on the server. Otherwise, it will be simply truncated from
// the attachments array.
// @param file {Object} the file to delete
// @returns {boolean}
// #

// # List of trainings
bar

JS

sub uncomment {
    my($doc) = @_;
    $doc =~ s!^// #!/*!;
    $doc =~ s!// #$! */!;
    $doc =~ s!^//( #)?! $1 ||'' eq '#' ? '/*' : ' *' !gme;
    warn $doc;
    $doc
}

$js =~ s!(// #\R// (.+\n)+( *)// #\R)!uncomment($1)!gme;
print $js;

Преобразован в автономный канал документ-JSdoc, следующие работы:

#!perl
use strict;
use warnings;

local $/;
my $js = <>;

sub uncomment {
    my($doc) = @_;
    $doc =~ s!^// #!/*!;
    $doc =~ s!// #$! */!;
    $doc =~ s!^//( #)?! $1 ||'' eq '#' ? '/*' : ' *' !gme;
    warn $doc;
    $doc
}

$js =~ s!(// #\R// (.+?\n)+( *)// #\R)!uncomment($1)!gme;
print $js;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...