Skip to content

Commit

Permalink
fix: Handle missing fn.name
Browse files Browse the repository at this point in the history
Fixes #51
  • Loading branch information
Jan Krems committed Jan 13, 2016
1 parent cfa48a1 commit c5591b3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 7 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var defaultReviver, nodeTypeString, nodes, parse, parseRegExpLiteral, parseStringLiteral, runInThisContext, syntaxErrorMessage;
var defaultReviver, getFunctionNameIE, nodeTypeString, nodes, parse, parseRegExpLiteral, parseStringLiteral, runInThisContext, syntaxErrorMessage;

runInThisContext = require('vm').runInThisContext;

Expand All @@ -40,8 +40,13 @@ defaultReviver = function(key, value) {
return value;
};

getFunctionNameIE = function(fn) {
return csNode.constructor.toString().match(/^function\s*([^( ]+)/)[1];
};

nodeTypeString = function(csNode) {
return csNode.constructor.name;
var ref;
return (ref = csNode.constructor.name) != null ? ref : getFunctionNameIE(csNode.constructor);
};

syntaxErrorMessage = function(csNode, msg) {
Expand Down
9 changes: 8 additions & 1 deletion src/parse.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

defaultReviver = (key, value) -> value

getFunctionNameIE = (fn) ->
# fn.name is only standard in ES2015+ and won't work in IE
# This takes the source of the function and extracts the name,
# e.g. 'function fooBar ()' becomes fooBar.
csNode.constructor.toString()
.match(/^function\s*([^( ]+)/)[1]

nodeTypeString = (csNode) ->
csNode.constructor.name
csNode.constructor.name ? getFunctionNameIE(csNode.constructor)

syntaxErrorMessage = (csNode, msg) ->
{
Expand Down

0 comments on commit c5591b3

Please sign in to comment.