Skip to content

Commit 76327cd

Browse files
authored
Merge pull request #61 from ember-best-practices/fix-no-global-jquery
Fixing no-global-jquery to include references to `jQuery`
2 parents 1ebfffa + c592105 commit 76327cd

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

guides/rules/no-global-jquery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# No Global jQuery
22

3-
**TL;DR** Do not use global `$`. Use `Ember.$` instead.
3+
**TL;DR** Do not use global `$` or `jQuery`. Use `Ember.$` instead.
44

55
In general, we want application code to reference the version of jQuery that's been directly pinned
66
to the version of Ember used. This helps avoid version conflicts, and ensures that code inside modules

lib/rules/no-global-jquery.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
*/
55
'use strict';
66

7+
const ALIASES = ['$', 'jQuery'];
8+
79
/**
8-
* Determines if this expression matches a global jQuery invocation.
10+
* Determines if this expression matches a global jQuery invocation, either `$` or `jQuery`.
911
* @param {ASTNode} node The identifier node.
1012
* @returns {Boolean} Returns true if the expression matches, otherwise false.
1113
*/
1214
function isGlobalJquery(node) {
13-
return node.callee && node.callee.name === '$';
15+
return node.callee && ALIASES.includes(node.callee.name);
1416
}
1517

16-
const MESSAGE = 'Do not use global `$`. Use `Ember.$` instead. Please see the following guide for more information: https://github.com/ember-best-practices/eslint-plugin-ember-best-practices/blob/master/guides/rules/no-global-jquery.md';
18+
const MESSAGE = 'Do not use global `$` or `jQuery`. Use `Ember.$` instead. Please see the following guide for more information: https://github.com/ember-best-practices/eslint-plugin-ember-best-practices/blob/master/guides/rules/no-global-jquery.md';
1719

1820
module.exports = {
1921
docs: {

tests/lib/rules/no-global-jquery.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,38 @@ ruleTester.run('no-global-jquery', rule, {
9090
errors: [{
9191
message: MESSAGE
9292
}]
93+
},
94+
{
95+
code: `
96+
export default Ember.Component({
97+
init() {
98+
this.el = jQuery('.test');
99+
}
100+
});`,
101+
parserOptions: {
102+
ecmaVersion: 6,
103+
sourceType: 'module'
104+
},
105+
errors: [{
106+
message: MESSAGE
107+
}]
108+
},
109+
{
110+
code: `
111+
export default Ember.Component({
112+
actions: {
113+
invalid1() {
114+
this.inv1 = jQuery('.invalid1');
115+
}
116+
}
117+
});`,
118+
parserOptions: {
119+
ecmaVersion: 6,
120+
sourceType: 'module'
121+
},
122+
errors: [{
123+
message: MESSAGE
124+
}]
93125
}
94126
]
95127
});

0 commit comments

Comments
 (0)