0

I have multiple files that start with comments like:

/*
 * @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);
  }
}

All files contains two types of comments i.e /**/ and // Now I am reading this file in my javascript code and want to remove comments and get the actual code in the variable e.g

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);
  }
}

I have tried using strip-comments and parse-comments npm but none of these work. Here is the code:

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);
}

and with parse-comments I tried like:

const parsed = parseComments(data)[0];
const code = data.split('\n').slice(parsed.comment.end).join('\n').trim();

I think strip comment is not working because it takes string as an argument but fs.readFileSync doesn't return string. I have also tried data.toString()but that also didn't work. So how can I strip comments from the content? Is there any other solution?

4
  • check out this [link]github.com/jonschlinkert/strip-comments
    – Iniamudhan
    Commented Nov 26, 2018 at 6:26
  • already saw that, but it's not working as strip-comments takes string as an argument
    – Ask
    Commented Nov 26, 2018 at 6:29
  • If you use Komodo Edit, you can use a RegEx across multiple files. cntrl+h.
    – StackSlave
    Commented Nov 26, 2018 at 6:35
  • "but fs.readFileSync doesn't return string" - actually, it does return a string since you pass an encoding option.
    – Bergi
    Commented Nov 26, 2018 at 7:40

1 Answer 1

2

try use regx to replace /\/\*[\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,''))

like this https://codepen.io/anon/pen/eQKrWP

1
  • 1
    thanks, it works but I had to add .trim() in the end to mke it work.i.e data.replace(/\/*[\s\S]*?*\/|([^:]|^)\/\/.*$/gm, '').trim()
    – Ask
    Commented Nov 26, 2018 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.