@@ -17,7 +17,6 @@ import (
1717 "regexp"
1818 "strings"
1919
20- "github.com/dlclark/regexp2"
2120 "github.com/microsoft/typescript-go/shim/ast"
2221 "github.com/web-infra-dev/rslint/internal/utils"
2322)
@@ -29,10 +28,6 @@ const (
2928 CompilerReactFunctionHook CompilerReactFunctionType = "Hook"
3029)
3130
32- type CompilerFunctionOptions struct {
33- HookPattern * regexp2.Regexp
34- }
35-
3631// hookNameTailRegex matches the suffix part of a hook identifier:
3732// after the leading `use`, the next character must be uppercase Latin
3833// or a digit. Mirrors upstream's `/^use[A-Z0-9]/`.
@@ -158,24 +153,18 @@ func IsHookCallee(node *ast.Node) bool {
158153
159154// IsCompilerHookCallee is the React Compiler variant of IsHookCallee:
160155// it accepts `useFoo` / `Namespace.useFoo`, but not the bare `use`.
161- func IsCompilerHookCallee (node * ast.Node , hookPattern * regexp2. Regexp ) bool {
156+ func IsCompilerHookCallee (node * ast.Node ) bool {
162157 if node == nil {
163158 return false
164159 }
165- isHookName := func (name string ) bool {
166- if hookPattern != nil {
167- return utils .Regexp2MatchString (hookPattern , name )
168- }
169- return IsCompilerHookName (name )
170- }
171160 n := ast .SkipParentheses (node )
172161 switch n .Kind {
173162 case ast .KindIdentifier :
174- return isHookName (n .AsIdentifier ().Text )
163+ return IsCompilerHookName (n .AsIdentifier ().Text )
175164 case ast .KindPropertyAccessExpression :
176165 pae := n .AsPropertyAccessExpression ()
177166 prop := pae .Name ()
178- if prop == nil || prop .Kind != ast .KindIdentifier || ! isHookName (prop .AsIdentifier ().Text ) {
167+ if prop == nil || prop .Kind != ast .KindIdentifier || ! IsCompilerHookName (prop .AsIdentifier ().Text ) {
179168 return false
180169 }
181170 obj := ast .SkipParentheses (pae .Expression )
@@ -199,25 +188,25 @@ func IsCompilerFunctionKind(node *ast.Node) bool {
199188// directly creates JSX or calls a hook, has component-like parameters, and does
200189// not return obviously non-ReactNode values; a hook-like function must directly
201190// create JSX or call a hook; memo/forwardRef callbacks are components.
202- func GetCompilerReactFunctionType (fn * ast.Node , opts CompilerFunctionOptions ) CompilerReactFunctionType {
191+ func GetCompilerReactFunctionType (fn * ast.Node ) CompilerReactFunctionType {
203192 name := GetFunctionName (fn )
204193 if name != nil && name .Kind == ast .KindIdentifier && IsComponentNameStr (name .AsIdentifier ().Text ) {
205- if CallsHooksOrCreatesJsx (fn , opts . HookPattern ) &&
194+ if CallsHooksOrCreatesJsx (fn ) &&
206195 IsValidCompilerComponentParams (fn ) &&
207196 ! ReturnsCompilerNonNode (fn ) {
208197 return CompilerReactFunctionComponent
209198 }
210199 return ""
211200 }
212- if name != nil && IsCompilerHookCallee (name , opts . HookPattern ) {
213- if CallsHooksOrCreatesJsx (fn , opts . HookPattern ) {
201+ if name != nil && IsCompilerHookCallee (name ) {
202+ if CallsHooksOrCreatesJsx (fn ) {
214203 return CompilerReactFunctionHook
215204 }
216205 return ""
217206 }
218207 if ast .IsFunctionExpressionOrArrowFunction (fn ) {
219208 if IsForwardRefOrMemoCallback (fn , "forwardRef" ) || IsForwardRefOrMemoCallback (fn , "memo" ) {
220- if CallsHooksOrCreatesJsx (fn , opts . HookPattern ) {
209+ if CallsHooksOrCreatesJsx (fn ) {
221210 return CompilerReactFunctionComponent
222211 }
223212 }
@@ -228,7 +217,7 @@ func GetCompilerReactFunctionType(fn *ast.Node, opts CompilerFunctionOptions) Co
228217// CallsHooksOrCreatesJsx reports whether `fn` directly creates JSX or directly
229218// calls a compiler hook. Nested function bodies are skipped, matching React
230219// Compiler's traversal boundary.
231- func CallsHooksOrCreatesJsx (fn * ast.Node , hookPattern * regexp2. Regexp ) bool {
220+ func CallsHooksOrCreatesJsx (fn * ast.Node ) bool {
232221 found := false
233222 var walk func (* ast.Node )
234223 walk = func (node * ast.Node ) {
@@ -243,7 +232,7 @@ func CallsHooksOrCreatesJsx(fn *ast.Node, hookPattern *regexp2.Regexp) bool {
243232 return
244233 }
245234 if ast .IsCallExpression (node ) {
246- if IsCompilerHookCallee (node .AsCallExpression ().Expression , hookPattern ) {
235+ if IsCompilerHookCallee (node .AsCallExpression ().Expression ) {
247236 found = true
248237 return
249238 }
0 commit comments