UNPKG

eslint-plugin-sonarjs

Version:
58 lines (57 loc) • 2.19 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.isAssertion = isAssertion; exports.isTSAssertion = isTSAssertion; const typescript_1 = __importDefault(require("typescript")); const module_js_1 = require("../module.js"); const module_ts_js_1 = require("../module-ts.js"); // The documented `uvu/assert` API: https://github.com/lukeed/uvu/blob/master/docs/api.assert.md // `is` and `not` are absent because both are handled on their own in `isFQNAssertion`: // they are callable *and* carry sub-assertions (`is.not`, `not.equal`, ...). const ASSERT_METHOD_NAMES = new Set([ 'equal', 'fixture', 'instance', 'match', 'ok', 'snapshot', 'throws', 'type', 'unreachable', ]); // `not.*` mirrors the positive methods — including `not.ok`, an alias of `not` itself — // except `unreachable`, which has no negated form. const NEGATED_ASSERT_METHOD_NAMES = new Set([...ASSERT_METHOD_NAMES].filter(name => name !== 'unreachable')); function isAssertion(context, node) { if (node.type !== 'CallExpression') { return false; } return isFQNAssertion((0, module_js_1.getFullyQualifiedName)(context, node)); } function isTSAssertion(services, node) { if (node.kind !== typescript_1.default.SyntaxKind.CallExpression) { return false; } return isFQNAssertion((0, module_ts_js_1.getFullyQualifiedNameTS)(services, node)); } function isFQNAssertion(fqn) { const normalized = fqn?.replaceAll('/', '.'); const prefix = 'uvu.assert.'; if (!normalized?.startsWith(prefix)) { return false; } const parts = normalized.slice(prefix.length).split('.'); if (parts[0] === 'default') { parts.shift(); } if (parts[0] === 'is') { return parts.length === 1 || (parts.length === 2 && parts[1] === 'not'); } if (parts[0] === 'not') { return parts.length === 1 || (parts.length === 2 && NEGATED_ASSERT_METHOD_NAMES.has(parts[1])); } return parts.length === 1 && ASSERT_METHOD_NAMES.has(parts[0]); }