-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
chore(ts): Convert src/sentry/static/sentry/app/utils.jsx to typescript #14199
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
Conversation
function objectMatchesSubset(obj, other, deep) { | ||
let k; | ||
function objectMatchesSubset(obj?: object, other?: object, deep?: boolean): boolean { | ||
let k: string | number; |
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.
Why number, shouldn't it always be string?
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.
@lynnagara I thought so too. I used vscode to infer the type for me:
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.
Is this because array is a valid type of object (keys are just numbers instead of strings?). In our usage of this function I think it should only be an object with key as string.
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 made k
a string.
} | ||
|
||
export function intcomma(x) { | ||
export function intcomma(x: object): string { |
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.
x should be number
?
} | ||
return result; | ||
export function objectToArray<T>( | ||
obj: {[s: string]: T} | ArrayLike<T> |
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.
When is it not a regular object?
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'm just going to use export const objectToArray = Object.entries;
return value | ||
.split('\n') | ||
.map(f => trim(f)) | ||
.filter(f => f !== ''); | ||
} | ||
|
||
function projectDisplayCompare(a, b) { | ||
interface Project { |
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.
return typeof feature.match !== 'function' | ||
? feature | ||
: feature.match(/(?:^(?:projects|organizations):)?(.*)/).pop(); | ||
export function descopeFeatureName(feature: string): string { |
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 (feature) seems like it's not always a string
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.
where is it not a string?
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.
If it's always a string then line 222 - 224 doesn't make sense
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.
Ah hm, I forget why I wrote it like that 😬
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've adjusted the types for this function
@@ -249,5 +258,5 @@ export function deepFreeze(object) { | |||
object[name] = value && typeof value === 'object' ? deepFreeze(value) : value; | |||
} | |||
|
|||
return Object.freeze(object); | |||
return Object.freeze<T>(object); |
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.
Is there value in this type annotation? We could probably drop this
const re = /(?:github\.com|bitbucket\.org)\/([^\/]+\/[^\/]+)/i; | ||
const match = repo.match(re); | ||
const parsedRepo = match ? match[1] : repo; | ||
return parsedRepo; | ||
return parsedRepo as any; |
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.
So this needs microsoft/TypeScript#22348
@@ -9,6 +9,8 @@ export type Project = { | |||
id: string; | |||
slug: string; | |||
isMember: boolean; | |||
|
|||
isBookmarked?: boolean; |
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.
Might not need to be optional? AFAIK we always get an isBookmarked
value for a project
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.
done.
} | ||
return result; | ||
} | ||
export const objectToArray = Object.entries; |
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.
Could we refactor this method away? I don't see there being much value in having an alias to stdlib.
} | ||
return result; | ||
} | ||
export const objectToArray = Object.entries; |
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 wonder if this will have implications, since right now this would iterate keys like constructor
. Not sure if Object.entries
will do that as well.
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 don't think we ever want to iterate keys like constructor
, Object.entries
only iterates its own properties.
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.
Good points. I'll revisit this to see if the refactor is generally small enough to fit into this PR.
I don't want to accidentally introduce a bug by "correcting" this utility function.
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.
Let's not tackle it here, IMO we should fix it though, I recall a case of a project named "constructor" recently that did have some problems.
return results.pop()!; | ||
} | ||
|
||
return feature; |
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.
Personal preference, but I think the original shorter version was easier to read (But I'm also biased, because I wrote it)
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.
Even this last bit I think could just be return results ? results.pop() : feature
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.
@evanpurkhiser oh interesting. I'll revisit this.
Typescript indicated that the match()
can return null
, which is why I rewrote it like this.
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.
Ahhh, yeah makes sense.
d97d3f3
to
544f071
Compare
157984f
to
496fb5c
Compare
3b25e34
to
0f2f85a
Compare
0f2f85a
to
76c29ca
Compare
76c29ca
to
92b3a4f
Compare
TODO
descopeFeatureName()
Deferred to another followup PR