"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const ts = __importStar(require("typescript"));
function hasNullCheckAncestor(node) {
    let hasNullCheck = false;
    let ancestor = node.parent;
    while (ancestor && !hasNullCheck) {
        if (ts.isIfStatement(ancestor)) {
            const condText = ancestor.expression.getText();
            if (condText.includes('!') ||
                condText.includes('null') ||
                condText.includes('undefined') ||
                condText.includes('?.')) {
                hasNullCheck = true;
            }
        }
        if (ts.isBinaryExpression(ancestor) &&
            (ancestor.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken ||
                ancestor.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
                ancestor.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken ||
                ancestor.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken)) {
            const text = ancestor.getText();
            if (text.includes('null') || text.includes('undefined')) {
                hasNullCheck = true;
            }
        }
        ancestor = ancestor.parent;
    }
    return hasNullCheck;
}
function hasAwaitAncestor(node) {
    let ancestor = node.parent;
    while (ancestor) {
        if (ts.isAwaitExpression(ancestor))
            return true;
        if (ts.isExpressionStatement(ancestor) || ts.isVariableDeclaration(ancestor)) {
            return false;
        }
        ancestor = ancestor.parent;
    }
    return false;
}
function getBaseCallText(node) {
    let base = node.expression;
    while (ts.isPropertyAccessExpression(base))
        base = base.expression;
    return base.getText();
}
function detectMissingNullChecks(source) {
    const sourceFile = ts.createSourceFile('test.ts', source, ts.ScriptTarget.Latest, true);
    let count = 0;
    function visit(node) {
        if (ts.isPropertyAccessExpression(node)) {
            const baseText = getBaseCallText(node);
            if (baseText.includes('fetch') ||
                baseText.includes('axios') ||
                baseText.includes('prisma.') ||
                baseText.includes('db.')) {
                if (hasAwaitAncestor(node) && !hasNullCheckAncestor(node)) {
                    count++;
                }
            }
        }
        ts.forEachChild(node, visit);
    }
    visit(sourceFile);
    return count;
}
describe('Null check detection (Unit 1.3.4)', () => {
    test('await fetch without null check is flagged', () => {
        const code = `const x = await fetch('/api').then(r => r.json());`;
        expect(detectMissingNullChecks(code)).toBeGreaterThan(0);
    });
    test('await fetch wrapped in if-null check is NOT flagged', () => {
        const code = `
      const r = await fetch('/api');
      if (r !== null) {
        const data = await r.json();
      }
    `;
        const hits = detectMissingNullChecks(code);
        expect(hits).toBe(0);
    });
    test('await prisma.findFirst with optional chaining is NOT flagged', () => {
        const code = `const user = (await prisma.user.findFirst())?.name;`;
        expect(detectMissingNullChecks(code)).toBe(0);
    });
    test('non-AST-await property access is NOT flagged', () => {
        const code = `const x = obj.fetch.data;`;
        expect(detectMissingNullChecks(code)).toBe(0);
    });
});
//# sourceMappingURL=code-scanner-null-check.test.js.map