eslint-plugin-jquery icon indicating copy to clipboard operation
eslint-plugin-jquery copied to clipboard

Inspections can't detect jQuery usages if non-`$` alias is used

Open alies-dev opened this issue 7 years ago • 2 comments

Inspections can't detect jQuery usages if non-$ alias is used

Example of a code that can be omitted by jquery/no-ajax rule:

import jQuery from 'jquery';

jQuery
    .ajax({ url: url, method: 'POST' })
    .done(() => { });

That's because some rules contains the following code:

if (node.callee.object.name !== '$') return

the quick-fix solution is to check for jQuery/jquery also:

const jQueryAliases = ['$', 'jQuery', 'jquery'];
const calleeObjectName = node.callee.object.name;
if (! jQueryAliases.includes(calleeObjectName)) return

Ideally jQueryAliases should be a configurable option

PS: If you agree with this solution - I can create a PR 👍

alies-dev avatar Sep 10 '18 10:09 alies-dev

Not a maintainer of this plugin, but I think this would be a good idea. I wonder if this would be a use case for using ESLint's shared settings so the aliases are accessible across all rules.

// .eslintrc.js
module.exports = {
  settings: {
    jQueryAliases: ['jquery', 'jQuery', '$'],
  },
}

// in your rule
module.exports = {
  create(context) {
    const jQueryAliases =
      context.settings && context.settings.jQueryAliases
        ? [].concat(context.settings.jQueryAliases)
        : ['$']

    return {
      // use `jQueryAliases` in your visitor
    }
  },
}

macklinu avatar Sep 24 '18 19:09 macklinu

I think it makes sense to implement this feature only after merging #20

Otherwise we will have a lot of conflicts

alies-dev avatar Oct 16 '18 19:10 alies-dev