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

chore(ts): Convert src/sentry/static/sentry/app/utils.jsx to typescript #14199

Merged
merged 1 commit into from
Aug 13, 2019

Conversation

dashed
Copy link
Member

@dashed dashed commented Jul 30, 2019

@dashed dashed requested a review from a team July 30, 2019 03:45
@dashed dashed self-assigned this Jul 30, 2019
function objectMatchesSubset(obj, other, deep) {
let k;
function objectMatchesSubset(obj?: object, other?: object, deep?: boolean): boolean {
let k: string | number;
Copy link
Member

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?

Copy link
Member Author

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:

Screen Shot 2019-07-29 at 11 54 28 PM

Copy link
Member

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.

Copy link
Member Author

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 {
Copy link
Member

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>
Copy link
Member

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?

Copy link
Member Author

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 {
Copy link
Member

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 {
Copy link
Member

@lynnagara lynnagara Jul 30, 2019

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

Copy link
Member

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?

Copy link
Member

@lynnagara lynnagara Aug 1, 2019

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

Copy link
Member

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 😬

Copy link
Member Author

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);
Copy link
Member

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;
Copy link
Member Author

Choose a reason for hiding this comment

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

@@ -9,6 +9,8 @@ export type Project = {
id: string;
slug: string;
isMember: boolean;

isBookmarked?: boolean;
Copy link
Member

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

Copy link
Member Author

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;
Copy link
Member

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;
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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;
Copy link
Member

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)

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

Ahhh, yeah makes sense.

@dashed dashed merged commit f21520b into master Aug 13, 2019
@dashed dashed deleted the typescript/app-utils branch August 13, 2019 20:56
@github-actions github-actions bot locked and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants