Skip to content

Allow NUMBER and DATETIME to take strings, numbers, dates #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

stasm
Copy link
Contributor

@stasm stasm commented Jul 26, 2019

This PR relaxes the type guards inthe NUMBER and DATETIME builtins. They now accept strings, FluentNumbers, and FluentDateTimes. However, if the native value of the argument results in a NaN during parseFloat, an error is thrown.

Furthermore, FluentDateTime now stores the date as a number, which works well with the isNaN check mentioned above. DateTimeFormat.format() converts its Date-typed argument to a number anyways.

Found in https://bugzilla.mozilla.org/show_bug.cgi?id=1568914.

@stasm stasm requested a review from Pike July 26, 2019 08:09

errors = [];
msg = bundle.getMessage('num-bad-opt');
assert.strictEqual(bundle.formatPattern(msg.value, args, errors), '{NUMBER()}');
assert.strictEqual(bundle.formatPattern(msg.value, args, errors), '1475107200000');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value comes from here:

return this.value;

The variable reference resolved fine, but the bad option caused toString to go into the error branch. The error is correctly reported, but I'm not sure if just return this.value is the right fallback. We need to return a string here, so FluentNone is not option.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps simulating the result of a FluentNone would be better? I.e. return "{???}".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, we could format using some reasonable defaults.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 81cbe84 I changed this to return safe defaults.

Copy link
Contributor

@Pike Pike left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two nits, and one issue.

Reading through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat, I despise parseFloat. I know you don't introduce it into the number scheme. But at least for FluentDateTime, I think we should go for the strict Number() directly.

For FluentNumber, I think we should do the same, but that could be changing behavior, so I'm not sure if that's OK to drive-by. Happy to spin that into a separate issue.

I like the fallback values that you ended up with.

@@ -134,10 +134,10 @@ function VariableReference(scope, {name}) {
case "string":
return arg;
case "number":
return new FluentNumber(arg);
return new FluentNumber(parseFloat(arg));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one's not needed, we know we have a number.

@@ -107,12 +106,14 @@ export class FluentDateTime extends FluentType {
/**
* Create an instance of `FluentDateTime` with options to the
* `Intl.DateTimeFormat` constructor.
* @param {(Date|number|string)} value
* @param {number} value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a bit more precise? timestamp in milliseconds or so?

@@ -44,9 +45,10 @@ function DATETIME([arg], opts) {
return new FluentNone(`DATETIME(${arg.valueOf()})`);
}

if (arg instanceof FluentDateTime) {
return new FluentDateTime(arg.valueOf(), merge(arg.opts, opts));
let value = parseFloat(arg.valueOf());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use Number here.

@stasm stasm merged commit fb12ab6 into projectfluent:release-fluent-zero-thirteen Jul 26, 2019
@stasm stasm deleted the builtin-type-guards branch July 26, 2019 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants