Skip to content

Commit

Permalink
guard typo detector access behind a mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 20, 2022
1 parent 2f6d5fa commit eaa3ba8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions internal/css_ast/css_decl_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package css_ast

import (
"strings"
"sync"

"github.com/evanw/esbuild/internal/helpers"
)
Expand Down Expand Up @@ -648,20 +649,26 @@ var KnownDeclarations = map[string]D{
}

var typoDetector *helpers.TypoDetector
var typoDetectorMutex sync.Mutex

func MaybeCorrectDeclarationTypo(text string) (string, bool) {
// Ignore CSS variables, which should not be corrected to CSS properties
if strings.HasPrefix(text, "--") {
// Ignore CSS variables, which should not be corrected to CSS properties
return "", false
}

typoDetectorMutex.Lock()
defer typoDetectorMutex.Unlock()

// Lazily-initialize the typo detector for speed when it's not needed
if typoDetector == nil {
// Lazily-initialize the typo detector for speed when it's not needed
valid := make([]string, 0, len(KnownDeclarations))
for key := range KnownDeclarations {
valid = append(valid, key)
}
detector := helpers.MakeTypoDetector(valid)
typoDetector = &detector
}

return typoDetector.MaybeCorrectTypo(text)
}

0 comments on commit eaa3ba8

Please sign in to comment.