Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
fix(ConfigPatternCache): fixes an issue when #eval(...) interprets …
Browse files Browse the repository at this point in the history
…`JSON.stringify`-values as template properties

This fixes issue #45, and closes PR #46.
  • Loading branch information
mdreizin authored Sep 20, 2016
1 parent 45a3f7c commit 2a8d6ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/ConfigPatternCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
* @private
* @type {RegExp}
*/
const DEFAULT_INTERPOLATE = /\[([\s\S]+?)]/g;
const DEFAULT_INTERPOLATE = /\[([\w\s]+?)]/g;

/**
* @private
Expand All @@ -21,7 +21,7 @@ const INTERPOLATE = new WeakMap();
class ConfigPatternCache extends Map {
/**
* @constructor
* @param {RegExp} [interpolate=/\[([\s\S]+?)]/g]
* @param {RegExp} [interpolate=/\[([\w\s]+?)]/g]
*/
constructor(interpolate = DEFAULT_INTERPOLATE) {
super();
Expand All @@ -42,7 +42,7 @@ class ConfigPatternCache extends Map {
* patternCache
* } from 'webpack-config';
*
* patternCache.interpolate = /{([\s\S]+?)}/g;
* patternCache.interpolate = /{([\w\s]+?)}/g;
* @param {RegExp} value
*/
set interpolate(value) {
Expand Down
24 changes: 20 additions & 4 deletions test/ConfigPatternCache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,31 @@ describe('ConfigPatternCache', () => {

describe('#eval()', () => {
it('should replace `[foo]` with `bar`', () => {
const value = patternCache.eval('[foo]-[foo]', {
expect(patternCache.eval('[foo]-[foo]', {
foo: 'bar'
})).toEqual('bar-bar');

expect(patternCache.eval('[ foo ]-[ foo ]', {
foo: 'bar'
})).toEqual('bar-bar');
});

it('should not replace `JSON.stringify`-strings', () => {
const str = JSON.stringify({
foo: [
{ x: 1 },
{ y: 2 },
{ z: 3 }
]
});

expect(value).toEqual('bar-bar');
expect(patternCache.eval(str, {})).toEqual(str);
});
});

it('should replace `{foo}` with `bar`', () => {
patternCache.interpolate = /{([\s\S]+?)}/g;
describe('#interpolate', () => {
it('should replace `{foo}` with `bar` using custom `interpolate`', () => {
patternCache.interpolate = /{([\w\s]+?)}/g;

const value = patternCache.eval('{foo}-{foo}', {
foo: 'bar'
Expand Down

0 comments on commit 2a8d6ea

Please sign in to comment.