Search code examples
javascriptnode.jsnpmecmascript-6auth0

How to strip comments in javascript?


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?


Solution

  • 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