MCPcopy Index your code
hub / github.com/microsoft/typescript-go / GetOrCreateToken

Method GetOrCreateToken

internal/ast/ast.go:2774–2802  ·  view source on GitHub ↗

Gets a token from the file's token cache, or creates it if it does not already exist. This function should NOT be used for creating synthetic tokens that are not in the file in the first place.

(
	kind Kind,
	pos int,
	end int,
	parent *Node,
	flags TokenFlags,
)

Source from the content-addressed store, hash-verified

2772// Gets a token from the file's token cache, or creates it if it does not already exist.
2773// This function should NOT be used for creating synthetic tokens that are not in the file in the first place.
2774func (node *SourceFile) GetOrCreateToken(
2775 kind Kind,
2776 pos int,
2777 end int,
2778 parent *Node,
2779 flags TokenFlags,
2780) *TokenNode {
2781 node.tokenCacheMu.Lock()
2782 defer node.tokenCacheMu.Unlock()
2783 loc := core.NewTextRange(pos, end)
2784 key := TokenCacheKey{parent, loc}
2785 if token, ok := node.tokenCache[key]; ok {
2786 if token.Kind != kind {
2787 panic(fmt.Sprintf("Token cache mismatch: %v != %v", token.Kind, kind))
2788 }
2789 return token
2790 }
2791 if parent.Flags&NodeFlagsReparsed != 0 {
2792 panic(fmt.Sprintf("Cannot create token from reparsed node of kind %v", parent.Kind))
2793 }
2794 if node.tokenCache == nil {
2795 node.tokenCache = make(map[TokenCacheKey]*Node)
2796 }
2797 token := createToken(kind, node, pos, end, flags)
2798 token.Loc = loc
2799 token.Parent = parent
2800 node.tokenCache[key] = token
2801 return token
2802}
2803
2804// `kind` should be a token kind.
2805func createToken(kind Kind, file *SourceFile, pos, end int, flags TokenFlags) *Node {

Callers 9

getTokenFromNodeListFunction · 0.80
nodeEndsWithFunction · 0.80
GetLastChildFunction · 0.80
GetFirstTokenFunction · 0.80
getTokenAtPositionFunction · 0.80
findRightmostValidTokenFunction · 0.80
FindNextTokenFunction · 0.80
FindChildOfKindFunction · 0.80

Calls 6

NewTextRangeFunction · 0.92
panicFunction · 0.85
createTokenFunction · 0.70
makeFunction · 0.50
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected