eslint-plugin-sf-plugin
Version: 
Helpful eslint rules for sf plugins.
74 lines (73 loc) • 3.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.noArgsParseWithoutStrictFalse = void 0;
/*
 * Copyright (c) 2020, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
 */
const eslint_utils_1 = require("@typescript-eslint/utils/eslint-utils");
const utils_1 = require("@typescript-eslint/utils");
const commands_1 = require("../shared/commands");
exports.noArgsParseWithoutStrictFalse = eslint_utils_1.RuleCreator.withoutDocs({
    meta: {
        docs: {
            description: 'If you parse args/argv, the class should have strict set to false',
            recommended: 'recommended',
        },
        messages: {
            summary: 'If you parse args/argv, the class should have strict set to false',
        },
        type: 'problem',
        schema: [],
        fixable: 'code',
    },
    defaultOptions: [],
    create(context) {
        return (0, commands_1.isInCommandDirectory)(context)
            ? {
                // eslint-disable-next-line complexity
                VariableDeclarator(node) {
                    var _a;
                    if (utils_1.ASTUtils.isAwaitExpression(node.init) &&
                        node.init.argument.type === utils_1.AST_NODE_TYPES.CallExpression &&
                        node.init.argument.callee.type === utils_1.AST_NODE_TYPES.MemberExpression &&
                        node.init.argument.callee.object.type === utils_1.AST_NODE_TYPES.ThisExpression &&
                        node.init.argument.callee.property.type === utils_1.AST_NODE_TYPES.Identifier &&
                        node.init.argument.callee.property.name === 'parse' &&
                        node.id.type === utils_1.AST_NODE_TYPES.ObjectPattern &&
                        node.id.properties.some((p) => p.type === utils_1.AST_NODE_TYPES.Property &&
                            p.key.type === utils_1.AST_NODE_TYPES.Identifier &&
                            p.key.name === 'argv')) {
                        // Verify that the class has strict = false
                        const sfCommand = (0, commands_1.getSfCommand)(context);
                        if (!sfCommand) {
                            return;
                        }
                        const strictProperty = sfCommand.body.body.find((p) => p.type === utils_1.AST_NODE_TYPES.PropertyDefinition &&
                            utils_1.ASTUtils.isIdentifier(p.key) &&
                            p.key.name === 'strict');
                        if ((strictProperty === null || strictProperty === void 0 ? void 0 : strictProperty.type) === utils_1.AST_NODE_TYPES.PropertyDefinition &&
                            ((_a = strictProperty.value) === null || _a === void 0 ? void 0 : _a.type) === utils_1.AST_NODE_TYPES.Literal &&
                            strictProperty.value.value === true) {
                            context.report({
                                node: strictProperty,
                                messageId: 'summary',
                                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                                fix: (fixer) => fixer.replaceText(strictProperty.value, 'false'),
                            });
                        }
                        else if (!strictProperty) {
                            context.report({
                                node: node.id,
                                messageId: 'summary',
                                fix: (fixer) => fixer.insertTextBefore(sfCommand.body.body[0], 'public static readonly strict = false;'),
                            });
                        }
                    }
                },
            }
            : {};
    },
});