-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix: replace type assertion with type guard #4302
Changes from 2 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 |
---|---|---|
|
@@ -4,10 +4,9 @@ export function getOriginalLocation( | |
sourcemapChain: DecodedSourceMapOrMissing[], | ||
location: { column: number; line: number; name?: string; source?: string } | ||
): { column: number; line: number } { | ||
// This cast is guaranteed. If it were a missing Map, it wouldn't have a mappings. | ||
const filteredSourcemapChain = sourcemapChain.filter( | ||
sourcemap => sourcemap.mappings | ||
) as ExistingDecodedSourceMap[]; | ||
(sourcemap): sourcemap is ExistingDecodedSourceMap => sourcemap.mappings !== undefined | ||
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. Are you sure you want to explicitly compare against undefined and there will never be a 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. @lukastaegert uh, sorry, I think I came too late. :/ compare the original source code: I addd it only because according to the types,
there were none. ;) if you think 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. The previous check was a check for truthiness, which you replaced with an explicit but much more strict one. 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. the previous check seemed to be insufficient for typescript, otherwise I would have left it as is. 😉 https://github.com/rollup/rollup/runs/4639006105?check_suite_focus=true I assume that do you think the typing is incorrect then? can I think it makes the code more readable and more robust. it's really hard to follow any code paths otherwise, specially if the code and the types don't match. 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 vaguely remember that mappings could be an empty string, but I am not sure in this instance. Moreover, anything could be in a user generated source map. I am just saying, the previous check was concise and worked well, also for TypeScript. TypeScript understands very well that filter does an implicit cast to Boolean and does not flag anything here. The new check might work as well, but it needlessly takes the risk of breaking stuff for other people, and I do not see a need to do so here. |
||
); | ||
|
||
while (filteredSourcemapChain.length > 0) { | ||
const sourcemap = filteredSourcemapChain.pop()!; | ||
|
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.
Out of curiosity: Does
Array.from(iterator)
provide any typing benefits over[... iterator]
? From their behaviour if you do not use the optional second argument ofArray.from
, I would expect them to be more or less the same with the spread operator possibly being slightly more efficient as it needs to do less checking.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.
I think it's more or less the same. dunno about the runtime tho, as the former creates a new array instance and spreads the map values, while the latter only traverses the map. but all that might be optimized away anyways. 🤷