Subject: | JavaScript keywords used as hash keys aren't quoted |
Data::JavaScript::Anon 0.4 doesn't quote JavaScript keywords used as hash keys. This causes a JavaScript syntax error.
I've attached a patch containing failing tests, and a fix that makes the test pass.
-- Aaron Crane
diff -ur Data-JavaScript-Anon-0.4.orig/lib/Data/JavaScript/Anon.pm Data-JavaScript-Anon-0.4/lib/Data/JavaScript/Anon.pm
--- Data-JavaScript-Anon-0.4.orig/lib/Data/JavaScript/Anon.pm Fri Mar 25 04:10:26 2005
+++ Data-JavaScript-Anon-0.4/lib/Data/JavaScript/Anon.pm Fri Apr 1 10:05:57 2005
@@ -7,7 +7,7 @@
use strict;
use UNIVERSAL 'isa';
-use vars qw{$VERSION $errstr $RE_NUMERIC $RE_NUMERIC_HASHKEY};
+use vars qw{$VERSION $errstr $RE_NUMERIC $RE_NUMERIC_HASHKEY %KEYWORD};
BEGIN {
$VERSION = '0.4';
$errstr = '';
@@ -28,8 +28,17 @@
# The numeric for of the hash key is similar, but without the + or - allowed
$RE_NUMERIC_HASHKEY = qr/^(?:$real|$_hex|$_oct)$/;
-}
+ %KEYWORD = map { $_ => 1 } qw{
+ abstract boolean break byte case catch char class const
+ continue debugger default delete do double else enum export
+ extends false final finally float for function goto if
+ implements import in instanceof int interface long native new
+ null package private protected public return short static super
+ switch synchronized this throw throws transient true try typeof
+ var void volatile while with
+ };
+}
@@ -168,6 +177,9 @@
sub anon_hash_key {
my $class = shift;
my $value = (defined $_[0] and ! ref $_[0]) ? shift : return undef;
+
+ # Quote if it's a keyword
+ return "'$value'" if $KEYWORD{$value};
# Don't quote if it is just a set of word characters or numeric
return $value if $value =~ /^[^\W\d]\w*$/;
diff -ur Data-JavaScript-Anon-0.4.orig/t/02_results.t Data-JavaScript-Anon-0.4/t/02_results.t
--- Data-JavaScript-Anon-0.4.orig/t/02_results.t Fri Mar 25 04:10:26 2005
+++ Data-JavaScript-Anon-0.4/t/02_results.t Fri Apr 1 10:04:58 2005
@@ -15,7 +15,7 @@
}
}
-use Test::More tests => 73;
+use Test::More;
use Data::JavaScript::Anon;
# Thoroughly test the numeric tests
@@ -31,6 +31,18 @@
a 09 +09 -09 ++1 +-34
3com 2131.231fd2132 +0x21x
};
+
+my @keywords = qw{
+ abstract boolean break byte case catch char class const continue
+ debugger default delete do double else enum export extends false final
+ finally float for function goto if implements import in instanceof int
+ interface long native new null package private protected public return
+ short static super switch synchronized this throw throws transient true
+ try typeof var void volatile while with
+};
+
+plan tests => (@numbers + @not_numbers + @keywords + 4);
+
foreach ( @numbers ) {
ok( Data::JavaScript::Anon->is_a_number( $_ ), "$_ is a number" );
}
@@ -38,7 +50,11 @@
ok( ! Data::JavaScript::Anon->is_a_number( $_ ), "$_ is not a number" );
}
-
+# Test that keywords come out quoted
+foreach ( @keywords ) {
+ is( Data::JavaScript::Anon->anon_hash_key($_), "'$_'",
+ "anon_hash_key correctly quotes keyword $_ used as hash key" );
+}
# Do a simple test of most of the code in a single go