WebUI: Enforce querySelector<Type>() pattern via ESLint, part 7 (last)
In this part, amending the existing checks to also catch such cases.
All violations have been fixed in precursor CLs.
A common misuse of querySelector/querySelectorAll is to use a type cast,
like
this.shadowRoot.querySelector(...) as Type
instead of leveraging the type parameter, like
this.shadowRoot.querySelector<Type>(...)
The previously added ESLint checks miss cases where a "not null"
typecast exists, like
this.shadowRoot.querySelector(...)! as Type
Fixed: 1521107
Change-Id: I0bebe973d3467b92c53525ac4bc0337814481bde
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5502593
Auto-Submit: Demetrios Papadopoulos <[email protected]>
Reviewed-by: Rebekah Potter <[email protected]>
Commit-Queue: Rebekah Potter <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1294846}
diff --git a/.eslintrc.js b/.eslintrc.js
index d13be07..7b47717 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -75,6 +75,11 @@
},
{
// https://google.github.io/styleguide/tsguide.html#return-type-only-generics
+ 'selector': 'TSAsExpression > TSNonNullExpression > CallExpression > MemberExpression[property.name=/^querySelector$/]',
+ 'message': 'Don\'t use \'querySelector(...)! as Type\'. Use the type parameter, \'querySelector<Type>(...)\', followed by an assertion instead',
+ },
+ {
+ // https://google.github.io/styleguide/tsguide.html#return-type-only-generics
'selector': 'TSAsExpression > CallExpression > MemberExpression[property.name=/^querySelectorAll$/]',
'message': 'Don\'t use \'querySelectorAll(...) as Type\'. Use the type parameter, \'querySelectorAll<Type>(...)\' instead',
},