Skip to content

pull lowercasing out of class matcher#141

Merged
michaelficarra merged 5 commits intoestools:masterfrom
nzakas:perf-fix
Mar 7, 2023
Merged

pull lowercasing out of class matcher#141
michaelficarra merged 5 commits intoestools:masterfrom
nzakas:perf-fix

Conversation

@nzakas
Copy link
Copy Markdown
Contributor

@nzakas nzakas commented Mar 6, 2023

Moves the selector name .toLowerCase() back outside of the created function to avoid multiple calls.

@michaelficarra
Copy link
Copy Markdown
Member

I mean, we could also pull out the comparisons. Not sure how far we want to go with this. But this is an improvement, so we can merge as-is.

case 'class': {
    switch(selector.name.toLowerCase()) {
        case 'statement':
            return (node, ancestry, options) => {
                if (options && options.matchClass) {
                    return options.matchClass(selector.name, node, ancestry);
                }

                if (options && options.nodeTypeKey) return false;   

                if(node.type.slice(-9) === 'Statement') return true;
                return node.type.slice(-11) === 'Declaration';
            };

        case 'declaration':
            return (node, ancestry, options) => {
                if (options && options.matchClass) {
                    return options.matchClass(selector.name, node, ancestry);
                }

                if (options && options.nodeTypeKey) return false;   

                return node.type.slice(-11) === 'Declaration';
            }

        case 'pattern':
            return (node, ancestry, options) => {
                if (options && options.matchClass) {
                    return options.matchClass(selector.name, node, ancestry);
                }

                if (options && options.nodeTypeKey) return false;   

                if(node.type.slice(-7) === 'Pattern') return true;
                return node.type.slice(-10) === 'Expression' ||
                    node.type.slice(-7) === 'Literal' ||
                    (
                        node.type === 'Identifier' &&
                        (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty')
                    ) ||
                    node.type === 'MetaProperty';
            }

        case 'expression':
            return (node, ancestry, options) => {
                if (options && options.matchClass) {
                    return options.matchClass(selector.name, node, ancestry);
                }

                if (options && options.nodeTypeKey) return false;   

                return node.type.slice(-10) === 'Expression' ||
                    node.type.slice(-7) === 'Literal' ||
                    (
                        node.type === 'Identifier' &&
                        (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty')
                    ) ||
                    node.type === 'MetaProperty';
            }

        case 'function':
            return (node, ancestry, options) => {
                if (options && options.matchClass) {
                    return options.matchClass(selector.name, node, ancestry);
                }

                if (options && options.nodeTypeKey) return false;   

                return node.type === 'FunctionDeclaration' ||
                    node.type === 'FunctionExpression' ||
                    node.type === 'ArrowFunctionExpression';
            }
    }
}

@michaelficarra michaelficarra changed the title Perf fix pull lowercasing out of class matcher Mar 7, 2023
@michaelficarra michaelficarra merged commit 909bea6 into estools:master Mar 7, 2023
@nzakas nzakas deleted the perf-fix branch March 7, 2023 18:48
@nzakas
Copy link
Copy Markdown
Contributor Author

nzakas commented Mar 7, 2023

I just wanted to restore to the previous state. Too many refactors while I was working on it. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants