Как раздеть комментарии в javascript? - PullRequest
0 голосов
/ 26 ноября 2018

У меня есть несколько файлов, которые начинаются с комментариев, таких как:

/*
 * @title Force email verification
 * @overview Only allow access to users with verified emails.
 * @gallery true
 * @category access control
 *
 * This rule will only allow access users that have verified their emails.
 *
 * > Note: It might be a better UX to make this verification from your application.
 *
 * If you are using [Lock](https://auth0.com/docs/lock), the default behavior is to log in a user immediately after they have signed up.
 * To prevent this from immediately displaying an error to the user, you can pass the following option to `lock.show()` or similar: `loginAfterSignup: false`.
 *
 * If you are using [auth0.js](https://auth0.com/docs/libraries/auth0js), the equivalent option is `auto_login: false`.
 *
 */
//jshint -W025
function (user, context, callback) {
  if (!user.email_verified) {
    return callback(new UnauthorizedError('Please verify your email before logging in.'));
  } else {
    return callback(null, user, context);
  }
}

Все файлы содержат комментарии двух типов, например /**/ и // Теперь я читаю этот файл в своем коде JavaScript и хочуудалите комментарии и получите реальный код в переменной, например

function (user, context, callback) {
  if (!user.email_verified) {
    return callback(new UnauthorizedError('Please verify your email before logging in.'));
  } else {
    return callback(null, user, context);
  }
}

Я пробовал использовать полосовые комментарии и парс-комментарии npm, но ни одна из этих работ не работает.Вот код:

const fs = require('fs');
const path = require('path');
const strip = require('strip-comments');
module.exports = function (ruleFileName, globals, stubs) {
    globals = globals || {};
    stubs = stubs || {};
    const fileName = path.join(__dirname, '../src/rules', ruleFileName + '.js');
    const data = fs.readFileSync(fileName, 'utf8');
    const code = strip(data);
    console.log(code);
    return compile(code, globals, stubs);
}

и с комментариями синтаксического анализа, которые я пробовал как:не возвращает строку.Я также попробовал data.toString(), но это также не сработало.Так как же я могу удалить комментарии из контента?Есть ли другое решение?

1 Ответ

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

попробуйте использовать regx для замены /\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm

   var Text = `/*
     * @title Force email verification
     * @overview Only allow access to users with verified emails.
     * @gallery true
     * @category access control
     *
     * This rule will only allow access users that have verified their emails.
     *
     * > Note: It might be a better UX to make this verification from your application.
     *
     * If you are using [Lock](https://auth0.com/docs/lock), the default behavior is to log in a user immediately after they have signed up.
     * To prevent this from immediately displaying an error to the user, you can pass the following option to "lock.show()" or similar: "loginAfterSignup: false".
     *
     * If you are using [auth0.js](https://auth0.com/docs/libraries/auth0js), the equivalent option is "auto_login: false".
     *
     */
    //jshint -W025
    function (user, context, callback) {
      if (!user.email_verified) {
        return callback(new UnauthorizedError('Please verify your email before logging in.'));
      } else {
        return callback(null, user, context);
      }
    }`

     console.log(Text.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm,''))

вот так https://codepen.io/anon/pen/eQKrWP

...