-
Notifications
You must be signed in to change notification settings - Fork 991
Increase strictness of @turf/line-chunk input #2970
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ import { | |
| * If the line is shorter than the segment length then the original line is returned. | ||
| * | ||
| * @function | ||
| * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the lines to split | ||
| * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the LineString or MultiLineStrings to split | ||
| * @param {number} segmentLength how long to make each segment | ||
| * @param {Object} [options={}] Optional parameters | ||
| * @param {Units} [options.units='kilometers'] Supports all valid Turf {@link https://turfjs.org/docs/api/types/Units Units} | ||
|
|
@@ -34,8 +34,8 @@ function lineChunk<T extends LineString | MultiLineString>( | |
| | Feature<T> | ||
| | FeatureCollection<T> | ||
| | T | ||
| | GeometryCollection | ||
| | Feature<GeometryCollection>, | ||
| | GeometryCollection<T> | ||
| | Feature<GeometryCollection<T>>, | ||
| segmentLength: number, | ||
| options: { | ||
| units?: Units; | ||
|
|
@@ -57,6 +57,12 @@ function lineChunk<T extends LineString | MultiLineString>( | |
|
|
||
| // Flatten each feature to simple LineString | ||
| flattenEach(geojson, (feature: Feature<T>) => { | ||
| if (feature.geometry.type !== "LineString") { | ||
| throw new Error( | ||
| "Only LineString and MultiLineString geometry types are supported" | ||
| ); | ||
| } | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Open to handling points differently here, but this has the nice property of aligning the TypeScript strictness with the runtime behavior.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, what do we want Turf's runtime behaviour to be when it's gets unexpected / inappropriate input? Halt on unexpected input? Or soldier on silently (or with a warning)?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think about this along a few axes. The first is something like "what is this method supposed to accept?", in this case we have a method that only makes sense on line-shaped things, so points and polygons should be considered invalid and strongly rejected. But we should take a more moderate stance on the container shape. Send a The second is the type checking errors vs runtime errors. Ideally these would be consistent, but I think its safer to add strictness at type checking time (it won't surprise the consumer in production). We're flagging things that are likely already buggy at runtime for them anyhow. At one point we started removing the runtime checks because we added guards at the type level, but I'm not sure we'll ever be able to get away with that because the runtime guards provide way clearer feedback to the dev than some error deep within the implementation. I'm not a big fan of Overall I think the current state of this PR reflects my idea of what 'ideal' looks like in this case, but I understand that we might be reluctant to actually merge this without a major release because of the risk of runtime breakage. I would certainly consider removing the runtime error for We have a pretty limited velocity on this project, and blocking little improvements like these until the next major winds up delaying the improvements getting rolled out to users. I'm not sure what the right balance is between getting things out quickly vs disrupting the folks that are tracking our releases more closely.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This seems a good balance. Caller has to be more alert about what they pass in. So for a geometry collection containing 99 lines and 1 point, we would throw?
As is I think this would have to be a major release candidate:
To keep moving forward between majors then, do we make this PR about the helpers.geometryCollection part? That doesn't narrow input types, and doesn't broaden the return type either. Runtime behaviour would stay the same.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I'll send this to the v8 milestone and go look at everything in helpers tomorrow to see if there's anything else that can get a similar treatment.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got eager and broke that out here #2971 |
||
|
|
||
| // reverses coordinates to start the first chunked segment at the end | ||
| if (reverse) { | ||
| feature.geometry.coordinates = feature.geometry.coordinates.reverse(); | ||
|
|
||
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.
This change has an interesting backstory.
packages/turf-line-chunk/types.ts has
lineChunk(geomCollection, 2);, butgeometryCollectionwas erasing the specific subtype (the default is justGeometryCollection<Geometry>) instead of inferring them forward into the return type. This changesgeometryCollectionto preserve any sort of information we did have for the things getting put into the collection.I believe that this class of type strictness error may hit other consumers if they are creating geometry collections using other methods.