Skip to content

refactor: enhance fallbackKeysFilter readability #272

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/ast/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ export const KEYS = Evk.unionWith({

/**
* Check that the given key should be traversed or not.
* @this {Traversable}
* @param key The key to check.
* @param value The value of the key in the node.
* @returns `true` if the key should be traversed.
*/
function fallbackKeysFilter(this: any, key: string): boolean {
let value = null
function fallbackKeysFilter(key: string, value: any = null): boolean {
return (
key !== "comments" &&
key !== "leadingComments" &&
Expand All @@ -46,7 +45,7 @@ function fallbackKeysFilter(this: any, key: string): boolean {
key !== "range" &&
key !== "tokens" &&
key !== "trailingComments" &&
(value = this[key]) !== null &&
value !== null &&
typeof value === "object" &&
(typeof value.type === "string" || Array.isArray(value))
)
Expand All @@ -57,8 +56,10 @@ function fallbackKeysFilter(this: any, key: string): boolean {
* @param node The node to get.
* @returns The keys to traverse.
*/
function getFallbackKeys(node: Node): string[] {
return Object.keys(node).filter(fallbackKeysFilter, node)
export function getFallbackKeys(node: Node): string[] {
return Object.keys(node).filter((key) =>
fallbackKeysFilter(key, node[key as keyof Node]),
)
}

/**
Expand Down Expand Up @@ -119,5 +120,3 @@ export interface Visitor {
export function traverseNodes(node: Node, visitor: Visitor): void {
traverse(node, null, visitor)
}

export { getFallbackKeys }
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy