UNPKG

eslint-plugin-sonarjs

Version:
54 lines (53 loc) 2.78 kB
/** * Shared assertion detection used by rules that need to recognise assertion * calls and gate on the presence of an assertion library / test runner * (e.g. S2699 "tests should include assertions" and S8784 "assertions should * be inside test cases or hooks"). * * This is intentionally distinct from `assertions.ts`, which extracts the * *structure* of an assertion (subject, predicate, comparison) for rules that * reason about assertion arguments. Here we only answer two yes/no questions: * "does this file use a supported assertion library?" and "is this node an * assertion call?". */ import type { Rule } from 'eslint'; import type estree from 'estree'; import type { ParserServicesWithTypeInformation } from '@typescript-eslint/utils'; import ts from 'typescript'; /** * Whether the linted file imports or the project depends on a supported * assertion library / test runner. Rules use this to avoid raising issues in * files that are not tests. */ export declare function hasSupportedAssertionLibrary(context: Rule.RuleContext): boolean; export declare function hasSupportedTestFramework(context: Rule.RuleContext): boolean; /** * Whether the given AST node is an assertion call, recognised across chai, * sinon, vitest, supertest, cypress, global `expect*(...)` chains and node * `assert`. Pure-AST: does not require type information. */ export declare function isAssertion(context: Rule.RuleContext, node: estree.Node): boolean; /** * Whether `node` is an assertion from a library that runs in a plain script with * no test runner (node `assert`, chai, sinon, supertest). The complement among * assertions — vitest, cypress, global `expect` — is "runner-bound". Callers * deciding "is this runner-bound?" should test * `isAssertion(...) && !isScriptCapableAssertion(...)`. */ export declare function isScriptCapableAssertion(context: Rule.RuleContext, node: estree.Node): boolean; /** * Whether `node` is a compile-time-only type check that must not be flagged for * placement outside a test case. */ export declare function isTypeLevelAssertion(context: Rule.RuleContext, node: estree.Node): boolean; /** * Incomplete Chai `foo.should` property chains are not assertions on their own. * We exclude them so S2699 does not treat an unfinished `should` chain as an assertion * and miss the "Add at least one assertion to this test case." issue. */ export declare function isIncompleteShouldAccess(context: Rule.RuleContext, node: estree.Node): boolean; /** * Type-checker-aware counterpart of {@link isAssertion}, operating on TypeScript * AST nodes. Used when parser services are available to follow resolved types. */ export declare function isTSAssertion(services: ParserServicesWithTypeInformation, node: ts.Node): boolean;