Skip to content

Commit

Permalink
tests: additional coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryThePenguin committed Jan 11, 2021
1 parent 5c704ab commit 43a4ef3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
11 changes: 6 additions & 5 deletions lib/rules/no-global-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const { ReferenceTracker } = require('eslint-utils');
const jqueryMethods = require('../utils/jquery-methods');

const ERROR_MESSAGE = 'Do not use global `$` or `jQuery`';

const jqueryMap = {
[ReferenceTracker.CALL]: true,
};
Expand All @@ -11,7 +13,10 @@ for (const method of jqueryMethods) {
jqueryMap[method] = { [ReferenceTracker.CALL]: true };
}

const ERROR_MESSAGE = 'Do not use global `$` or `jQuery`';
const traceMap = {
$: jqueryMap,
jQuery: jqueryMap,
};

//------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -37,10 +42,6 @@ module.exports = {
return {
Program() {
const tracker = new ReferenceTracker(context.getScope());
const traceMap = {
$: jqueryMap,
jQuery: jqueryMap,
};

for (const { node } of tracker.iterateGlobalReferences(traceMap)) {
context.report(node, ERROR_MESSAGE);
Expand Down
54 changes: 30 additions & 24 deletions lib/rules/no-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,38 @@ const { ReferenceTracker } = require('eslint-utils');
const { isCallExpression } = require('../utils/types');
const jqueryMethods = require('../utils/jquery-methods');

//------------------------------------------------------------------------------
// General rule - Disallow usage of jQuery
//------------------------------------------------------------------------------

const ERROR_MESSAGE = 'Do not use jQuery';

const jqueryMap = {
[ReferenceTracker.CALL]: true,
};

for (const method of jqueryMethods) {
jqueryMap[method] = { [ReferenceTracker.READ]: true };
jqueryMap[method] = { [ReferenceTracker.CALL]: true };
}

//------------------------------------------------------------------------------
// General rule - Disallow usage of jQuery
//------------------------------------------------------------------------------
const globalMap = {
$: jqueryMap,
jQuery: jqueryMap,
};

const ERROR_MESSAGE = 'Do not use jQuery';
const esmMap = {
jquery: {
[ReferenceTracker.ESM]: true,
default: jqueryMap,
},
ember: {
[ReferenceTracker.ESM]: true,
default: {
$: jqueryMap,
},
$: jqueryMap,
},
};

module.exports = {
meta: {
Expand Down Expand Up @@ -59,27 +78,9 @@ module.exports = {
const scope = context.getScope();
const tracker = new ReferenceTracker(scope);

const globalMap = {
$: jqueryMap,
jQuery: jqueryMap,
};

const esmMap = {
jquery: {
[ReferenceTracker.ESM]: true,
default: jqueryMap,
},
ember: {
[ReferenceTracker.ESM]: true,
default: {
$: jqueryMap,
},
$: jqueryMap,
},
};

/**
* Global references
*
* eg; $(body) and $.post()
*/
for (const { node } of tracker.iterateGlobalReferences(globalMap)) {
Expand All @@ -88,6 +89,11 @@ module.exports = {

/**
* ESM references
* import $ from 'jquery'
* import { $ as jq } from 'ember'
*
* eg;
* $(body) and jq.post()
*/
for (const { node } of tracker.iterateEsmReferences(esmMap)) {
report(node);
Expand Down
16 changes: 16 additions & 0 deletions tests/lib/rules/no-global-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,5 +457,21 @@ ruleTester.run('no-global-jquery', rule, {
},
],
},
{
code: `
export default Ember.Component({
init() {
jQuery.extend();
}
});`,
parserOptions,
globals,
output: null,
errors: [
{
message: ERROR_MESSAGE,
},
],
},
],
});
13 changes: 7 additions & 6 deletions tests/lib/rules/no-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ eslintTester.run('no-jquery', rule, {
errors: [
{
message: ERROR_MESSAGE,
type: 'MemberExpression',
type: 'CallExpression',
},
],
},
Expand Down Expand Up @@ -110,7 +110,7 @@ eslintTester.run('no-jquery', rule, {
errors: [
{
message: ERROR_MESSAGE,
type: 'MemberExpression',
type: 'CallExpression',
},
],
},
Expand All @@ -129,12 +129,12 @@ eslintTester.run('no-jquery', rule, {
errors: [
{
message: ERROR_MESSAGE,
type: 'MemberExpression',
type: 'CallExpression',
line: 6,
},
{
message: ERROR_MESSAGE,
type: 'MemberExpression',
type: 'CallExpression',
line: 7,
},
],
Expand All @@ -160,7 +160,7 @@ eslintTester.run('no-jquery', rule, {
{
code: `
import E from 'ember';
export default Ember.Component({
export default E.Component({
didInsertElement() {
E.$(body).addClass('active')
}
Expand All @@ -176,7 +176,7 @@ eslintTester.run('no-jquery', rule, {
// import $ from ember
{
code: `
import { $ } from 'ember';
import Ember, { $ } from 'ember';
export default Ember.Component({
didInsertElement() {
$(body).addClass('active')
Expand Down Expand Up @@ -264,6 +264,7 @@ eslintTester.run('no-jquery', rule, {
},
{
code: `
import Ember from 'ember';
export default Ember.Component({
didInsertElement() {
this.$.extend({}, a, b)
Expand Down

0 comments on commit 43a4ef3

Please sign in to comment.