utils

package
v0.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: MIT, MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ExpectParseReasonNone            = ""
	ExpectParseReasonMatcherNotFound = "matcher-not-found"
	ExpectParseReasonModifierUnknown = "modifier-unknown"
)
View Source
const DefaultJestVersion = "29.0.0"

DefaultJestVersion is used when the Jest version cannot be resolved from settings or package.json.

Variables

View Source
var EQUALITY_METHOD_NAMES = map[string]bool{
	"toBe":          true,
	"toEqual":       true,
	"toStrictEqual": true,
}
View Source
var EXPECT_MODIFIER_NAMES = map[string]bool{
	"not":      true,
	"rejects":  true,
	"resolves": true,
}
View Source
var JEST_HOOKS_ORDER = []string{"beforeAll", "beforeEach", "afterEach", "afterAll"}
View Source
var JEST_METHOD_NAMES = map[string]bool{
	"afterAll":   true,
	"afterEach":  true,
	"beforeAll":  true,
	"beforeEach": true,
	"describe":   true,
	"expect":     true,
	"fdescribe":  true,
	"fit":        true,
	"it":         true,
	"jest":       true,
	"test":       true,
	"xdescribe":  true,
	"xit":        true,
	"xtest":      true,
}
View Source
var VALID_JEST_FN_CALL_CHAINS = map[string]bool{
	"afterAll":                  true,
	"afterEach":                 true,
	"beforeAll":                 true,
	"beforeEach":                true,
	"describe":                  true,
	"describe.each":             true,
	"describe.only":             true,
	"describe.only.each":        true,
	"describe.skip":             true,
	"describe.skip.each":        true,
	"fdescribe":                 true,
	"fdescribe.each":            true,
	"fit":                       true,
	"fit.concurrent":            true,
	"fit.each":                  true,
	"fit.failing":               true,
	"fit.fails":                 true,
	"it":                        true,
	"it.concurrent":             true,
	"it.concurrent.each":        true,
	"it.concurrent.only.each":   true,
	"it.concurrent.skip.each":   true,
	"it.each":                   true,
	"it.failing":                true,
	"it.failing.each":           true,
	"it.fails":                  true,
	"it.only":                   true,
	"it.only.each":              true,
	"it.only.failing":           true,
	"it.only.fails":             true,
	"it.skip":                   true,
	"it.skip.each":              true,
	"it.skip.failing":           true,
	"it.skip.fails":             true,
	"it.todo":                   true,
	"test":                      true,
	"test.concurrent":           true,
	"test.concurrent.each":      true,
	"test.concurrent.only.each": true,
	"test.concurrent.skip.each": true,
	"test.each":                 true,
	"test.failing":              true,
	"test.fails":                true,
	"test.only":                 true,
	"test.only.each":            true,
	"test.only.failing":         true,
	"test.only.fails":           true,
	"test.skip":                 true,
	"test.skip.each":            true,
	"test.skip.failing":         true,
	"test.skip.fails":           true,
	"test.todo":                 true,
	"xdescribe":                 true,
	"xdescribe.each":            true,
	"xit":                       true,
	"xit.concurrent":            true,
	"xit.each":                  true,
	"xit.failing":               true,
	"xit.fails":                 true,
	"xtest":                     true,
	"xtest.each":                true,
	"xtest.failing":             true,
	"xtest.fails":               true,
}

Functions

func ApplyGlobalJestAlias added in v0.22.0

func ApplyGlobalJestAlias(name string, settings map[string]interface{}) string

ApplyGlobalJestAlias maps settings.jest.globalAliases so that e.g. `context` is treated as `describe`, matching eslint-plugin-jest.

func CalleeChainName added in v0.22.0

func CalleeChainName(expr *ast.Node) string

CalleeChainName returns a dotted name for a call callee expression, mirroring eslint-plugin-jest getNodeName for CallExpression callees (used by expect-expect assertFunctionNames matching).

It differs from GetJestFnMemberEntries / getElementAccessName: bracket notation contributes a segment only when the index matches eslint-plugin-jest's supported accessor names (identifier, string literal, or no-substitution template). Unsupported keys break the chain entirely. NewExpression is peeled so e.g. new (require('x')).y becomes a chain.

func FindExpectModifiersAndMatcher added in v0.22.0

func FindExpectModifiersAndMatcher(entries []ParsedJestFnMemberEntry) (
	[]ParsedJestFnMemberEntry,
	*ParsedJestFnMemberEntry,
	string,
)

func FindImportDeclaration

func FindImportDeclaration(node *ast.Node) *ast.ImportDeclaration

func FindTopMostCallExpression added in v0.22.0

func FindTopMostCallExpression(node *ast.Node) *ast.Node

FindTopMostCallExpression walks up member/call chains to the outermost CallExpression, matching eslint-plugin-jest's findTopMostCallExpression.

func GetAccessorReceiverAndParent added in v0.22.0

func GetAccessorReceiverAndParent(entry *ParsedJestFnMemberEntry) (*ast.Node, *ast.Node)

func GetJestVersion added in v0.22.0

func GetJestVersion(ctx rule.RuleContext) string

GetJestVersion returns the effective Jest version: explicit settings, then the nearest package.json, then DefaultJestVersion.

func IsBooleanLiteral added in v0.22.0

func IsBooleanLiteral(node *ast.Node) (value bool, ok bool)

IsBooleanLiteral reports whether node is a `true`/`false` literal after stripping parentheses and basic TS type assertions, and returns its value.

func IsFunction added in v0.22.0

func IsFunction(node *ast.Node) bool

func IsMemberAccessNode added in v0.22.0

func IsMemberAccessNode(node *ast.Node) bool

func IsNamedMember added in v0.22.0

func IsNamedMember(node *ast.Node, name string) bool

func IsStaticExpectMatcher added in v0.22.0

func IsStaticExpectMatcher(matcher string, headNode *ast.Node) bool

IsStaticExpectMatcher reports static expect APIs such as expect.any(...) or expect.not.stringContaining(...), but not assertion matchers like toBe.

func IsTypeOfJestFnCall

func IsTypeOfJestFnCall(node *ast.Node, ctx rule.RuleContext, kinds ...JestFnType) bool

func JestFnMemberEntriesRange added in v0.22.0

func JestFnMemberEntriesRange(entries []ParsedJestFnMemberEntry) (core.TextRange, bool)

JestFnMemberEntriesRange returns the source range spanning the first through last member entry nodes in a parsed jest/expect call chain.

func JestHookOrderIndex added in v0.22.0

func JestHookOrderIndex(name string) int

JestHookOrderIndex returns the expected declaration order index for a Jest hook name, or -1 if unknown.

func JestVersionMajor added in v0.22.0

func JestVersionMajor(v string) int

JestVersionMajor extracts the major version number from an npm version or range (e.g. "^29.0.0", "~27.1.0"). It returns 29 when the version cannot be parsed, matching the historical default of Jest 29.

func JoinJestFnMemberEntries added in v0.22.0

func JoinJestFnMemberEntries(entries []ParsedJestFnMemberEntry) string

func ReceiverBeforeInvocation added in v0.22.0

func ReceiverBeforeInvocation(matcherCall *ast.Node) *ast.Node

ReceiverBeforeInvocation returns the expression before .m() or ["m"]() on a call, such as `expect(x).not` before `.toBe()`.

func ResolveFirstIdentifier added in v0.22.0

func ResolveFirstIdentifier(node *ast.Node) *ast.Node

ResolveFirstIdentifier walks the left side of a call/member chain and returns the first identifier it finds, if any.

func ResolveNamedFunctionCallback added in v0.22.0

func ResolveNamedFunctionCallback(ctx rule.RuleContext, callExpr *ast.CallExpression) (*ast.Node, string)

ResolveNamedFunctionCallback returns the function declaration node and name when a Jest test call uses a named function reference as its callback (e.g. it('foo', getValue)).

func UnwrapBasicTypeAssertions added in v0.22.0

func UnwrapBasicTypeAssertions(node *ast.Node) *ast.Node

func UnwrapTypeAssertions added in v0.22.0

func UnwrapTypeAssertions(node *ast.Node) *ast.Node

Types

type JestFnType

type JestFnType string
const (
	JestFnTypeExpect   JestFnType = "expect"
	JestFnTypeDescribe JestFnType = "describe"
	JestFnTypeHook     JestFnType = "hook"
	JestFnTypeJest     JestFnType = "jest"
	JestFnTypeTest     JestFnType = "test"
	JestFnTypeUnknown  JestFnType = "unknown"
)

func GetJestKind

func GetJestKind(name string) JestFnType

type JestImportMode

type JestImportMode string
const (
	JEST_GLOBAL_MODE JestImportMode = "global"
	JEST_IMPORT_MODE JestImportMode = "import"
)

func ResolveJestFunctionReference added in v0.22.0

func ResolveJestFunctionReference(node *ast.Node, localName string, localNode *ast.Node, ctx rule.RuleContext) (string, *ast.Node, JestImportMode)

type ParsedJestFnCall

type ParsedJestFnCall struct {
	Name            string
	LocalName       string
	Kind            JestFnType
	Members         []string
	MemberEntries   []ParsedJestFnMemberEntry
	Modifiers       []string
	ModifierEntries []ParsedJestFnMemberEntry
	Matcher         string
	MatcherEntry    *ParsedJestFnMemberEntry
	Head            ParsedJestFnCallHead
}

func ParseJestFnCall

func ParseJestFnCall(node *ast.Node, ctx rule.RuleContext) *ParsedJestFnCall

type ParsedJestFnCallHead

type ParsedJestFnCallHead struct {
	Type     JestImportMode
	Local    ParsedJestFnCallHeadEntry
	Original ParsedJestFnCallHeadEntry
}

type ParsedJestFnCallHeadEntry

type ParsedJestFnCallHeadEntry struct {
	Value string
	Node  *ast.Node
}

type ParsedJestFnMemberEntry

type ParsedJestFnMemberEntry struct {
	Name string
	Node *ast.Node
	Call *ast.Node
}

func GetJestFnMemberEntries

func GetJestFnMemberEntries(node *ast.Node) []ParsedJestFnMemberEntry

type TestCallbackInfo added in v0.22.0

type TestCallbackInfo struct {
	FunctionNode *ast.Node
	Name         string
}

TestCallbackInfo describes a Jest test callback passed by reference (e.g. it('foo', getValue)).

func ResolveTestCallbackFunction added in v0.22.0

func ResolveTestCallbackFunction(ctx rule.RuleContext, callExpr *ast.CallExpression) TestCallbackInfo

ResolveTestCallbackFunction resolves the callback function node for a Jest test call. Inline callbacks are not returned; they are tracked via the enclosing test call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL