-
Notifications
You must be signed in to change notification settings - Fork 470
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: Migrate ByRole
to TypeScript
#1186
chore: Migrate ByRole
to TypeScript
#1186
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 9e5d4b4:
|
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.
For TS migration please don't change runtime behavior (yet). This makes reviewing harder than it needs to be.
If the type-checker has issues with things, we can use @ts-expect-error
or as any
for now and then slowly migrate to a more type-safe version.
@eps1lon Ohh okay, thanks. I didn't know that but that make sense. Let me update the PR to just have ts migration here. Thanks, again |
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.
Lint is failing. Is there anything else that's unclear to you from the CI logs?
CI needs to be green for this to be mergeable. If you're stuck at some point, feel free to ping me and we can go through the CI logs to see what went wrong.
Codecov Report
@@ Coverage Diff @@
## alpha #1186 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 24
Lines 998 1000 +2
Branches 327 328 +1
=========================================
+ Hits 998 1000 +2
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
@eps1lon I delayed more in make the fixes becuase lint is not working locally. So, I used the github jobs to check and fix it. Related to the changes, I think that the types are not completly clean or organized. I will try to get into it a little bit more now that roles are also in ts. Thanks for awesome welcoming to contribute here, I'm happy to could do it. |
@eps1lon there anything more that i need to change? 🤔 |
@eps1lon I updated based on |
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.
Sorry for letting this slide. This is going in the right direction.
src/queries/role.ts
Outdated
// guard against using `level` option with any role other than `heading` | ||
if (level !== undefined && role !== 'heading') { | ||
throw new Error(`Role "${role}" cannot have "level" property.`) |
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.
The double nesting is intentional to be consistent with the other option. It allows folding away options in your code editor you're not interested in. Please revert this change.
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.
Ohh! It's to be cosisten with the other queries right? Sounds good.
src/queries/role.ts
Outdated
const roleValue = node.getAttribute('role') | ||
/* istanbul ignore next */ | ||
const roleValue = node.getAttribute('role') ?? '' |
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 use a non-nullable assertion instead. Generally, let's not change any runtime in these migrations. We can do them in a follow-up.
We don't need the fallback since roleValue
will always be defined since we just confirmed that node.hasAttribute('role')
@@ -129,7 +156,7 @@ function queryAllByRole( | |||
return matcher(firstWord, node, role, matchNormalizer) | |||
} | |||
|
|||
const implicitRoles = getImplicitAriaRoles(node) | |||
const implicitRoles = getImplicitAriaRoles(node) 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.
question: what would the type be without the cast`
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.
role-helpers need migration to typescript so it return any[]
src/queries/role.ts
Outdated
Array.from(container.children).forEach(childElement => { | ||
Array.from( | ||
/* istanbul ignore next */ | ||
container?.children ?? [], |
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.
Same here. Let's not change runtime and use type casting/assertions instead.
types/__tests__/type-tests.ts
Outdated
@@ -178,15 +178,9 @@ export async function testByRole() { | |||
console.assert(queryByRole(element, 'button', {name: /^Log/}) === null) | |||
console.assert( | |||
queryByRole(element, 'button', { | |||
name: (name, el) => name === 'Login' && el.hasAttribute('disabled'), | |||
name: (name, el) => name === 'Login' && !!el?.hasAttribute('disabled'), |
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 does this need to change?
It's also pretty hard to grok what this is doing.
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.
Name require a boolean value. el
variable could be undefined based on typing. The boolean casting is because type result could be udefined by el
or boolean by hasAttribute
Maybe is more readable like that
name: (name, el) => name === 'Login' && !!el && el.hasAttribute('disabled'),
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 is el
nullable? Shouldn't that always be defined?
types/__tests__/type-tests.ts
Outdated
console.assert(queryByRole(element, 'foo') === null) | ||
console.assert(queryByRole(element, /foo/) === null) | ||
console.assert(screen.queryByRole('foo') === null) | ||
console.assert(screen.queryByRole(/foo/) === null) |
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 remove these?
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.
Because with the right typing it not be allowed to do it. If we want to continue using it, we could skip typescript in ths couple of lines
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 is this not allowed? query*
can return a nullable value
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.
Because "foo" is not a valid role according to types
types/queries.d.ts
Outdated
| RegExp | ||
| string | ||
| ((accessibleName: string, element: Element) => boolean) | ||
name?: RegExp | string | MatcherFunction |
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.
The original signature included useful type hints (e.g. accessibleName
). These are lost with a generic MatcherFunction
.
Could we instead use a concrete NameMatcherFunction
to get type hints and keep the improved union?
types/queries.d.ts
Outdated
role: ByRoleMatcher, | ||
options?: ByRoleOptions, | ||
) => T[] | ||
export type AllByRole = QueryMethod< |
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.
Same here. By using the generic QueryMethod
we loose type hints. Let's type it out properly.
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.
Oh okay, I think QueryMethod was the right way. Do you know what are the type hints that we lose that you mention.
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.
Just to take in care when I work in 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.
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.
ohhh!
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!
@eps1lon hi, i keep the changes simple and i dismiss any change that affect run time. There some ts comments, but I think it's fine for the migration. There others things that need to be change in general, but for the proposal of this PR, i think that with this changes is enough. Let me know if there any thing that you look weird in this changes. And thanks in advance for the time that you take to revisit it |
d72f79a
to
c88c915
Compare
c88c915
to
9e5d4b4
Compare
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.
Added some finishing touches. Thank you for sticking with it!
@all-contributors add @DaniAcu for code |
I've put up a pull request to add @DaniAcu! 🎉 |
What:
Migrate role.js to role.ts.
Why:
Because is the only file that is in js. I wold like to add more inteligence to ByRole, but for any other thing we need to add it first as ts file.
How:
Checklist:
docs site