"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
const camelcase_1 = __importDefault(require("eslint/lib/rules/camelcase"));
const util = __importStar(require("../util"));
exports.default = util.createRule({
    name: 'camelcase',
    meta: {
        type: 'suggestion',
        docs: {
            description: 'Enforce camelCase naming convention',
            category: 'Stylistic Issues',
            recommended: 'error',
        },
        schema: camelcase_1.default.meta.schema,
        messages: camelcase_1.default.meta.messages,
    },
    defaultOptions: [
        {
            allow: ['^UNSAFE_'],
            ignoreDestructuring: false,
            properties: 'never',
        },
    ],
    create(context, [options]) {
        const rules = camelcase_1.default.create(context);
        const TS_PROPERTY_TYPES = [
            typescript_estree_1.AST_NODE_TYPES.TSPropertySignature,
            typescript_estree_1.AST_NODE_TYPES.ClassProperty,
            typescript_estree_1.AST_NODE_TYPES.TSParameterProperty,
            typescript_estree_1.AST_NODE_TYPES.TSAbstractClassProperty,
        ];
        const properties = options.properties;
        const allow = options.allow;
        /**
         * Checks if a string contains an underscore and isn't all upper-case
         * @param  name The string to check.
         */
        function isUnderscored(name) {
            // if there's an underscore, it might be A_CONSTANT, which is okay
            return name.indexOf('_') > -1 && name !== name.toUpperCase();
        }
        /**
         * Checks if a string match the ignore list
         * @param name The string to check.
         * @returns if the string is ignored
         * @private
         */
        function isAllowed(name) {
            return (allow.findIndex(entry => name === entry || name.match(new RegExp(entry)) !== null) !== -1);
        }
        /**
         * Checks if the the node is a valid TypeScript property type.
         * @param node the node to be validated.
         * @returns true if the node is a TypeScript property type.
         * @private
         */
        function isTSPropertyType(node) {
            if (!node.parent) {
                return false;
            }
            if (TS_PROPERTY_TYPES.includes(node.parent.type)) {
                return true;
            }
            if (node.parent.type === typescript_estree_1.AST_NODE_TYPES.AssignmentPattern) {
                return (node.parent.parent !== undefined &&
                    TS_PROPERTY_TYPES.includes(node.parent.parent.type));
            }
            return false;
        }
        return {
            Identifier(node) {
                /*
                 * Leading and trailing underscores are commonly used to flag
                 * private/protected identifiers, strip them
                 */
                const name = node.name.replace(/^_+|_+$/g, '');
                // First, we ignore the node if it match the ignore list
                if (isAllowed(name)) {
                    return;
                }
                // Check TypeScript specific nodes
                if (isTSPropertyType(node)) {
                    if (properties === 'always' && isUnderscored(name)) {
                        context.report({
                            node,
                            messageId: 'notCamelCase',
                            data: { name: node.name },
                        });
                    }
                    return;
                }
                // Let the base rule deal with the rest
                rules.Identifier(node);
            },
        };
    },
});
//# sourceMappingURL=camelcase.js.map