Skip to content

Commit dd1bd2c

Browse files
author
e2tha-e
committed
replacing eval with JSON.parse
1 parent 757142a commit dd1bd2c

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

builder/parameter_hunter.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@
3535
console.log('found patternParameters for ' + partialName);
3636
}
3737

38-
//strip out the additional data and eval
38+
//strip out the additional data, convert string to JSON.
3939
var leftParen = pMatch.indexOf('(');
4040
var rightParen = pMatch.indexOf(')');
41-
var paramString = '({' + pMatch.substring(leftParen + 1, rightParen) + '})';
42-
43-
//do no evil. there is no good way to do this that I can think of without using a split, which then makes commas and colons special characters and unusable within the pattern params
44-
var paramData = eval(paramString);
41+
var paramString = '{' + pMatch.substring(leftParen + 1, rightParen) + '}';
42+
//if param keys are wrapped in single quotes, replace with double quotes.
43+
var paramStringWellFormed = paramString.replace(/(')([^'\s]+)(')(\s*\:)/gm, '"$2"$4');
44+
//if params keys are not wrapped in any quotes, wrap in double quotes.
45+
var paramStringWellFormed = paramStringWellFormed.replace(/([\{|,]\s*)([^\:\s]+)(\s*\:)/gm, '$1"$2"$3');
46+
//if param values are wrapped in single quotes, replace with double quotes.
47+
var paramStringWellFormed = paramStringWellFormed.replace(/(\:\s*)(')([^']+)(')/gm, '$1"$3"');
48+
var paramData = JSON.parse(paramStringWellFormed);
4549

4650
var globalData = JSON.parse(JSON.stringify(patternlab.data));
4751
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {}));

0 commit comments

Comments
 (0)