Skip to content
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

Less verbose function -> expression conversion #7337

Merged
merged 2 commits into from
Sep 26, 2018

Conversation

mourner
Copy link
Member

@mourner mourner commented Sep 26, 2018

Fixes #7336. Note that typeof obj === 'object' should be true for all non-primitive values in the spec (including arrays), so we don't need more exhaustive checks.

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality covered by existing tests
  • document any changes to public APIs
  • post benchmark scores n/a
  • manually test the debug page
  • tagged @mapbox/studio if this PR includes style spec changes

@mourner mourner requested a review from samanpwbb September 26, 2018 15:34
Copy link
Contributor

@samanpwbb samanpwbb left a comment

Choose a reason for hiding this comment

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

Thanks for taking care of this so quickly – looks like exactly the change we need in Studio.

@mourner mourner requested a review from ChrisLoer September 26, 2018 15:45
@ChrisLoer
Copy link
Contributor

This looks right to me -- it's basically specifically string/boolean/number that we're allowing without the "literal" wrapper, right?

Can we add a legacy expression test that uses @samanpwbb 's function from #7336 ?

@mourner
Copy link
Member Author

mourner commented Sep 26, 2018

@ChrisLoer there are already legacy tests like this, e.g. this one, it's just that the tests don't test conversion output itself — only the output after creating a style expression object and serializing it, which somehow strips the unnecessary "literal" wrappers. Not sure how to go about this.

@mourner
Copy link
Member Author

mourner commented Sep 26, 2018

@ChrisLoer tried adding comparison of raw-converted legacy function with serialized one, but there are many expressions which don't roundtrip like this — e.g. ["linear"] will turn into ["exponential", 1], and "black" will turn into ["rgba", 0, 0, 0, 1]. It's reassuring though that 44 failing tests with the test change get down to 22 in this branch.

@ChrisLoer
Copy link
Contributor

Ah right, that's happening from the serialize logic built into Literal:

serialize(): Array<mixed> {
if (this.type.kind === 'array' || this.type.kind === 'object') {
return ["literal", this.value];
} else if (this.value instanceof Color) {
// Constant-folding can generate Literal expressions that you
// couldn't actually generate with a "literal" expression,
// so we have to implement an equivalent serialization here
return ["rgba"].concat(this.value.toArray());
} else if (this.value instanceof Formatted) {
// Same as Color
return this.value.serialize();
} else {
assert(this.value === null ||
typeof this.value === 'string' ||
typeof this.value === 'number' ||
typeof this.value === 'boolean');
return (this.value: any);
}
}

What about something similar to the "converts token strings into expressions" test in migrate.test.js?

Copy link
Contributor

@ChrisLoer ChrisLoer left a comment

Choose a reason for hiding this comment

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

I went ahead and added a simple test using @samanpwbb's expression to migrate.test.js, along with another variant that requires the literal syntax. The coverage in that unit test is pretty small and I think this issue highlights that maybe relying on test-expressions to cover the migration isn't as robust as we'd hope. Would it make sense to check in an entire style sheet for use by this test in order to get better coverage? At any rate, I think what I added is enough coverage for this change.

Legacy function conversion is covered by the expression test suite, but the serialization code it uses for testing against fixtures hides extraneous `Literal` expressions.
@ChrisLoer ChrisLoer force-pushed the less-verbose-convert-function branch from 0341713 to 087a661 Compare September 26, 2018 19:38
@ryanhamley ryanhamley merged commit beae7b7 into master Sep 26, 2018
@ryanhamley ryanhamley deleted the less-verbose-convert-function branch September 26, 2018 19:49
@ryanhamley ryanhamley mentioned this pull request Sep 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants