From 184d81a6745e1ed3948411dccb8da2a3e0fdac3d Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Sun, 22 Sep 2024 23:53:50 +0100 Subject: [PATCH] fix(pluginutils): improve regex performance (#1753) perf(pluginutils): improve regex performance --- packages/pluginutils/src/attachScopes.ts | 4 ++-- packages/pluginutils/src/dataToEsm.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/pluginutils/src/attachScopes.ts b/packages/pluginutils/src/attachScopes.ts index d5dace4ed..fc6392ddb 100755 --- a/packages/pluginutils/src/attachScopes.ts +++ b/packages/pluginutils/src/attachScopes.ts @@ -66,7 +66,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc const node = n as estree.Node; // function foo () {...} // class Foo {...} - if (/(Function|Class)Declaration/.test(node.type)) { + if (/(?:Function|Class)Declaration/.test(node.type)) { scope.addDeclaration(node, false, false); } @@ -98,7 +98,7 @@ const attachScopes: AttachScopes = function attachScopes(ast, propertyName = 'sc } // create new for scope - if (/For(In|Of)?Statement/.test(node.type)) { + if (/For(?:In|Of)?Statement/.test(node.type)) { newScope = new Scope({ parent: scope, block: true diff --git a/packages/pluginutils/src/dataToEsm.ts b/packages/pluginutils/src/dataToEsm.ts index 312017220..4be5d7ae4 100755 --- a/packages/pluginutils/src/dataToEsm.ts +++ b/packages/pluginutils/src/dataToEsm.ts @@ -91,7 +91,7 @@ const dataToEsm: DataToEsm = function dataToEsm(data, options = {}) { let maxUnderbarPrefixLength = 0; for (const key of Object.keys(data)) { - const underbarPrefixLength = key.match(/^(_+)/)?.[0].length ?? 0; + const underbarPrefixLength = /^(_+)/.exec(key)?.[0].length ?? 0; if (underbarPrefixLength > maxUnderbarPrefixLength) { maxUnderbarPrefixLength = underbarPrefixLength; }