-
-
Notifications
You must be signed in to change notification settings - Fork 739
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
Improve filter specification typings #1390
Conversation
improve filter conversion by adding typing and helping typescript to guess types. add tests for filters mentioned in issue #1380.
@cns-solutions-admin feel free to review my changes. |
Bundle size report: Size Change: 0 B
ℹ️ View DetailsNo major changes |
Ad testing the typings:
Or just wait until somebody complains ;-) |
The question is more about removing the |
I tried to add the following test to the same file: test('non-legacy exprssions', () => {
function compileTimeCheck(filter: ExpressionFilterSpecification) {
return isExpressionFilter(filter);
}
expect(compileTimeCheck(['any'])).toBeTruthy();
expect(compileTimeCheck(['at', 2, ['array', 1, 2, 3]])).toBeTruthy();
expect(compileTimeCheck(['at', 2, [1, 2, 3]])).toBeTruthy();
}); But the test passes while the IDE (vs code) is showing the the last test it not "correct" from typescript point of view. Might be related to how jest and typescript are configured unfortunately :-( |
Ad expression test:
The function isExpressionFilter is rather a isDefinitelyNotALegacyFilter and might theoretically return a wrong result:
|
I know that this is not a valid expression, but the tests do not say that in any way - i.e. they continue to run instead of giving a transpilation error or something similar. |
As the types are compile-time only, you can only check the positive case, e.g. to test for valid legacy filters:
Similar for expressions. Anyway, there will probably always be expressions, that from a type perspective are correct, but due to the return types of feature properties are incorrect during evaluation. Thus it is important that all valid expressions will pass the type check, but not necessarily that all expressions that pass the type check, are valid. |
BTW, above "test" is not really a test, as it is successful, if it compiles. But it documents, what are valid expressions and will not compile, if somebody changes the type definition in an incorrect way. |
I would like to achieve the following which at this point I don't see how:
If you can suggest a way to accomplish this it will be great, otherwise adding these test won't protect us in the future in case we change the typings and break something... |
I think this is exactly, what you achieve with these "tests". They just do not fail by failing, but by not compiling. Ah, didn't see, that the transpilation does not fail. Hm. |
The problem is that jest uses @swc/jest instead of babel-jest or ts-jest for transforming the typescript code. With package.json changes:
and jest.config.js changes:
it throws compile errors, if a type test does not compile. As a lot of tests do not compile, only the relevant ones should be transformed by ts-jest (here feature_filter.test.ts). Tested with the following test in feature_filter.test.ts:
Replacing 'has' with 'hax' fails the test (compile error). |
The fact that a lot of tests do not compile isn't good... I'll see if I can spend some time fixing them. |
I think I found the issue talking about swc not being a type checker: swc-project/swc#571 |
From a testing perspective in most cases it does not really matter, if the test code is correct typescript code as long as it tests what it should test. However, for those tests that test typings (and those are rather few), we have to make sure that they are correct typescript code and are also type checked. This can be selectively done as described above and only marginally effects unit test speed. |
I'll wait for the following PR to be merged in order to add the relevant tests and later on merge this: #1399 |
I've merged the main branch after the migration to ts-node and it seems the either the test code is not accurate or there are some problems with the definition of the types that were introduced. |
Whoever wrote this test, tested that "Sh* in, sh* out", i.e. if something is passed in that cannot be understood, it passes through unchanged. According to the documentation valid colors are strings (see https://docs.mapbox.com/mapbox-gl-js/style-spec/types/#color), e.g. '#888' (= '#888888'), '#808080', 'red'. So a valid test would be
So, the current test is bad, the typing is correct. And you can't write such a bad test in future! P.S. unit test before (typescript checks): 34s, after: 238s, but second time just 70s, so not too bad. |
Yea, I saw this issue too, I'm wondering if it's worth just type checking instead... |
Any change you could fix the tests so I could merge this? |
I've added a few tests, which I checked are now failing if there's a transpiration error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for adding so many types to this. LGTM
@cns-solutions-admin any chance for some help here in giving more use cases we can test before this is merged? |
I think you could add a "test" (i.e. if it compiles) for each of the expressions, but I don't think it's worth the effort. |
I added a few tests that checks for compilation correctness as part of this PR, I think it worth the effort. Maybe not cover all cases, but at least cover some, or some complicated once at least. |
Examples from our configurations (property names added for context only):
Filter:
|
There is a missing ] for 'within' and an additional ] for interpolate.
|
And that's why you need tests :-) |
maplibre change log https://github.com/maplibre/maplibre-gl-js/blob/main/CHANGELOG.md#310 Breaking changes that required fixes * 3.0.0 Remove "mapbox-gl-supported" package from API. If needed, please reference it directly instead of going through MapLibre. (maplibre/maplibre-gl-js#2451) * 3.0.0 Resize map when container element is resized. The "resize"-related events now has different data associated with it (maplibre/maplibre-gl-js#2157, maplibre/maplibre-gl-js#2551). Previously the originalEvent field was the reason of this change, for example it could be a resize event from the browser. Now it is ResizeObserverEntry, see more [here](https://developer.mozilla.org/en-US/docs/web/api/resizeobserverentry). * 2.2.0 Improve filter specification typings (maplibre/maplibre-gl-js#1390) --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Launch Checklist
Improve typings for filter specifications, a PR in continue to #1383 that solves #1380.
maplibre-gl-js
changelog:<changelog></changelog>
.