globalizejs / globalize-compiler

Globalize.js runtime compiler for your formatters and parsers
Other
10 stars 14 forks source link

undefined in runtimeArgs generates invalid code #33

Open nkovacs opened 7 years ago

nkovacs commented 7 years ago

I was experimenting with https://github.com/globalizejs/globalize/issues/720 when I found this. If runtimeArgs contains undefined, the compiler will generate invalid code. E.g.:

var Globalize = require( "globalize" );
var globalizeCompiler = require('globalize-compiler');

Globalize.load( require( "cldr-data" ).entireSupplemental() );
Globalize.load( require( "cldr-data" ).entireMainFor( "en" ) );

var GyMMMd = Globalize( "en" ).dateFormatter({ skeleton: "GyMMMd" });
GyMMMd.runtimeArgs.push(undefined);
GyMMMd.runtimeArgs.push(undefined);

var out = globalizeCompiler.compile([
    GyMMMd,
], {});

The generated code will look like this:

Globalize.b938155015 = dateFormatterFn({"1":Globalize("en").numberFormatter({"raw":"0"})}, {"pattern":"MMM d, y G","timeSeparator":":","months":{"M":{"3":{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May","6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}}},"eras":{"0":"BC","1":"AD","0-alt-variant":"BCE","1-alt-variant":"CE"}}, , );

The , , ); is a syntax error.

The replacement happens here: https://github.com/globalizejs/globalize-compiler/blob/v1.0.0/lib/compile.js#L142

I suspect this isn't likely to come up in real-world usage, but shouldn't the compiler generate undefined? i.e.:

Globalize.b938155015 = dateFormatterFn({"1":Globalize("en").numberFormatter({"raw":"0"})}, {"pattern":"MMM d, y G","timeSeparator":":","months":{"M":{"3":{"1":"Jan","2":"Feb","3":"Mar","4":"Apr","5":"May","6":"Jun","7":"Jul","8":"Aug","9":"Sep","10":"Oct","11":"Nov","12":"Dec"}}},"eras":{"0":"BC","1":"AD","0-alt-variant":"BCE","1-alt-variant":"CE"}}, undefined, undefined);