From 90a5b6b4aeff7343783f85418c683f2c9901ab07 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Thu, 21 Oct 2021 14:02:31 +0200 Subject: [PATCH] Chore: improve performance of `:function` selector (#15181) --- lib/linter/node-event-generator.js | 7 +++++++ tests/lib/linter/node-event-generator.js | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/linter/node-event-generator.js b/lib/linter/node-event-generator.js index 89ebd281048..d56bef2fa9d 100644 --- a/lib/linter/node-event-generator.js +++ b/lib/linter/node-event-generator.js @@ -98,6 +98,13 @@ function getPossibleTypes(parsedSelector) { case "adjacent": return getPossibleTypes(parsedSelector.right); + case "class": + if (parsedSelector.name === "function") { + return ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]; + } + + return null; + default: return null; diff --git a/tests/lib/linter/node-event-generator.js b/tests/lib/linter/node-event-generator.js index afedc9d0171..44f8e512d67 100644 --- a/tests/lib/linter/node-event-generator.js +++ b/tests/lib/linter/node-event-generator.js @@ -257,6 +257,20 @@ describe("NodeEventGenerator", () => { ] ); + assertEmissions( + "function foo(){} var x; (function (p){}); () => {};", + [":function", "ExpressionStatement > :function", "VariableDeclaration, :function[params.length=1]"], + ast => [ + [":function", ast.body[0]], // function foo(){} + ["VariableDeclaration, :function[params.length=1]", ast.body[1]], // var x; + [":function", ast.body[2].expression], // function (p){} + ["ExpressionStatement > :function", ast.body[2].expression], // function (p){} + ["VariableDeclaration, :function[params.length=1]", ast.body[2].expression], // function (p){} + [":function", ast.body[3].expression], // () => {} + ["ExpressionStatement > :function", ast.body[3].expression] // () => {} + ] + ); + assertEmissions( "foo;", [