Skip to content

Commit 79fbc3a

Browse files
committed
esparse should handle regular expression literals.
As suggested by Arian Stolwijk. https://code.google.com/p/esprima/issues/detail?id=468
1 parent 5e72885 commit 79fbc3a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

bin/esparse.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,20 @@ if (typeof fname !== 'string') {
107107
process.exit(1);
108108
}
109109

110+
// Special handling for regular expression literal since we need to
111+
// convert it to a string literal, otherwise it will be decoded
112+
// as object "{}" and the regular expression would be lost.
113+
function adjustRegexLiteral(key, value) {
114+
if (key === 'value' && value instanceof RegExp) {
115+
value = value.toString();
116+
}
117+
return value;
118+
}
119+
110120
try {
111121
content = fs.readFileSync(fname, 'utf-8');
112122
syntax = esprima.parse(content, options);
113-
console.log(JSON.stringify(syntax, null, 4));
123+
console.log(JSON.stringify(syntax, adjustRegexLiteral, 4));
114124
} catch (e) {
115125
console.log('Error: ' + e.message);
116126
process.exit(1);

0 commit comments

Comments
 (0)