import { arrayFirst, isDefined } from "ts-extras";
import { ruleCreator } from "../_internal/rule-creator.js";
const disallowedSelector = "PropertyDefinition > Identifier.value[name='undefined'], VariableDeclarator > Identifier.init[name='undefined']";
const getUndefinedInitializationRemovalRange = (sourceCode, node) => {
    const equalsToken = sourceCode.getTokenBefore(node, {
        filter: (token) => token.value === "=",
    });
    if (equalsToken === null) {
        return undefined;
    }
    let fixStartIndex = arrayFirst(equalsToken.range);
    const sourceText = sourceCode.text;
    while (fixStartIndex > 0) {
        const previousCharacter = sourceText.at(fixStartIndex - 1);
        if (previousCharacter !== " " && previousCharacter !== "\t") {
            break;
        }
        fixStartIndex -= 1;
    }
    return [fixStartIndex, node.range[1]];
};
/**
 * Disallow explicit initialization to `undefined`.
 */
const rule = ruleCreator({
    create: (context) => {
        const sourceCode = context.sourceCode;
        return {
            [disallowedSelector]: (node) => {
                const removalRange = getUndefinedInitializationRemovalRange(sourceCode, node);
                if (!isDefined(removalRange)) {
                    return;
                }
                const fix = (fixer) => fixer.removeRange(removalRange);
                context.report({
                    fix,
                    messageId: "forbidden",
                    node,
                });
            },
        };
    },
    defaultOptions: [],
    meta: {
        deprecated: false,
        docs: {
            deprecated: false,
            description: "disallow unnecessary initialization to undefined.",
            frozen: false,
            recommended: false,
            url: "https://nick2bad4u.github.io/eslint-plugin-etc-misc/docs/rules/no-unnecessary-initialization",
        },
        fixable: "code",
        hasSuggestions: false,
        messages: {
            forbidden: "Unnecessary initialization to undefined.",
        },
        schema: [],
        type: "suggestion",
    },
    name: "no-unnecessary-initialization",
});
export default rule;
//# sourceMappingURL=no-unnecessary-initialization.js.map