eslint-plugin-jquery
eslint-plugin-jquery copied to clipboard
Inspections can't detect jQuery usages if non-`$` alias is used
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 👍
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
}
},
}
I think it makes sense to implement this feature only after merging #20
Otherwise we will have a lot of conflicts