Skip to content

New Rule: Don't reassign to dependency parameters #101

@cowchimp

Description

@cowchimp

Hi.
Thanks for this great plugin!

I'm interested in sending a PR to add a new rule.
Wanted your take\input before I take the time to work on it.

The goal of this rule is to catch cases where one of the parameters in the module function is reassigned.
It's easy to miss that the variable you think refers to the dependency now refers to something else.

Please LMK if you think you'd be interested in something like this,
and if so, some pointers on what I should pay attention to to increase changes of accepting the PR.

Thanks!

Invalid code example (only the assignment of a is invalid):

define(['path/to/a', 'path/to/b'], function(a, b) {
  a = 1;
  var c = b;
  c = 2;
});

Rule implementation (wip)

function create(context) {
    const params = [];
    return {
        CallExpression(node) {
            if (!isAmdWithCallback(node)) return;
            const moduleFunction = getModuleFunction(node);
            params = moduleFunction.params.map(x => x.name);
        },
        AssignmentExpression(node) {
            if (node.left.type !== "Identifier") return;
            const name = node.left.name;
            if (params.some(x => x === name)) {
                context.report(node, `Cannot assign to a module dependency named ${name}`);
            }
        }
    };
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions