Skip to content

Commit

Permalink
perf: remove unnecessary second filter for type narrowing (#2417)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynzh authored Mar 5, 2024
1 parent 02e6bbf commit 8e8e1e8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/rules/enforce-style-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ module.exports = {
return {}
}

const topLevelElements = documentFragment.children.filter(isVElement)
const topLevelStyleTags = topLevelElements.filter(
(element) => element.rawName === 'style'
const topLevelStyleTags = documentFragment.children.filter(
/** @returns {element is VElement} */
(element) => isVElement(element) && element.rawName === 'style'
)

if (topLevelStyleTags.length === 0) {
Expand Down
7 changes: 4 additions & 3 deletions lib/rules/require-explicit-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ module.exports = {
if (!documentFragment) {
return {}
}
const scripts = documentFragment.children
.filter(utils.isVElement)
.filter((element) => element.name === 'script')
const scripts = documentFragment.children.filter(
/** @returns {element is VElement} */
(element) => utils.isVElement(element) && element.name === 'script'
)
if (scripts.every((script) => !utils.hasAttribute(script, 'lang', 'ts'))) {
return {}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2643,9 +2643,10 @@ function getScriptSetupElement(context) {
if (!df) {
return null
}
const scripts = df.children
.filter(isVElement)
.filter((e) => e.name === 'script')
const scripts = df.children.filter(
/** @returns {e is VElement} */
(e) => isVElement(e) && e.name === 'script'
)
if (scripts.length === 2) {
return scripts.find((e) => hasAttribute(e, 'setup')) || null
} else {
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,10 @@ function buildPseudoNthVElementMatcher(testIndex) {
*/
function buildPseudoNthOfTypeVElementMatcher(testIndex) {
return (element) => {
const elements = element.parent.children
.filter(isVElement)
.filter((e) => e.rawName === element.rawName)
const elements = element.parent.children.filter(
/** @returns {e is VElement} */
(e) => isVElement(e) && e.rawName === element.rawName
)
return testIndex(elements.indexOf(element), elements.length)
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/style-variables/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ function getStyleVariablesContext(context) {
if (!df) {
return null
}
const styles = df.children
.filter(isVElement)
.filter((e) => e.name === 'style')
const styles = df.children.filter(
/** @returns {e is VElement} */
(e) => isVElement(e) && e.name === 'style'
)
if (styles.length === 0) {
return null
}
Expand Down

0 comments on commit 8e8e1e8

Please sign in to comment.