Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicated backslashes in regular expressions #536

Closed
PepijnSenders opened this issue May 17, 2017 · 6 comments
Closed

Duplicated backslashes in regular expressions #536

PepijnSenders opened this issue May 17, 2017 · 6 comments

Comments

@PepijnSenders
Copy link

PepijnSenders commented May 17, 2017

Found this issue while trying to get the babili-webpack-plugin to work with date-fns.

The issue is the following, when using the date-fns/format function the formatting regular expression will get messed up.

This code:

var formattingTokensRegExp = new RegExp(
    '(\\[[^\\[]*\\])|(\\\\)?' + '(' + formattingTokens.join('|') + '|.)', 'g'
  )

Will become:

j=new RegExp('(\\\\[[^\\\\[]*\\\\])|(\\\\\\\\)?('+h.join('|')+'|.)','g')

As you can probably see this will result in too many slashes and subsequently break the date-fns/format function.

Wondering if anyone else found this issue before and if it's something persistent if someone could fix it.

For testing this was my .babelrc:

{
    "presets": ["es2015"]
}

My webpack.config.js:

const fs = require('fs');
const path = require('path');

const BabiliPlugin = require('babili-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

const babelConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '.babelrc')));

module.exports = {
    entry: {
        main: path.join(__dirname, 'src', 'index.js')
    },
    module: {
        rules: [{
            test: /\.js$/,
            loader: 'babel-loader',
            query: babelConfig,
            include: [path.join(__dirname, 'src')],
        }],
    },
    plugins: [
        new BabiliPlugin(),
        new HtmlWebpackPlugin(),
    ],
    output: {
        filename: 'bundle.js'
    }
};

And the file to compile:

import format from 'date-fns/format';

(() => {
    format(new Date(), 'GGGG-[W]WW');
})();
@PepijnSenders
Copy link
Author

Btw if someone runs into the same issue, a temporary fix is:

import format from 'date-fns/format';
import enLocale from 'date-fns/locale/en';


(() => {
    console.log(format(new Date(), 'GGGG-[W]WW', {
        locale: {
            format: {
                formatters: enLocale.format.formatters,
                formattingTokensRegExp: /(\[[^\[]*\])|(\\)?(x|ss|s|mm|m|hh|h|do|dddd|ddd|dd|d|aa|a|ZZ|Z|YYYY|YY|X|Wo|WW|W|SSS|SS|S|Qo|Q|Mo|MMMM|MMM|MM|M|HH|H|GGGG|GG|E|Do|DDDo|DDDD|DDD|DD|D|A|.)/g
            }
        }
    }));
})();

@PepijnSenders
Copy link
Author

Also by the way the expected output of the console.log is: 2017-W20. Currently it is 2017-[20]20.

@PepijnSenders PepijnSenders changed the title Backslashes Regex problem Duplicated backslashes in regular expressions May 17, 2017
@vigneshshanmugam
Copy link
Member

vigneshshanmugam commented May 17, 2017

This issue was already fixed in master. This is the output code on master branch

var formattingTokensRegExp = new RegExp('([[^[]*])|(\\)?(' + formattingTokens.join('|') + '|.)', 'g');

PR - #490

@PepijnSenders
Copy link
Author

Thanks! Do you know when this will be released?

@vigneshshanmugam
Copy link
Member

vigneshshanmugam commented May 18, 2017

We are doing some cleanups in the new CLI, pretty soon.

@boopathi
Copy link
Member

Closing this as it is fixed in master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants