File tree Expand file tree Collapse file tree 6 files changed +142
-3
lines changed Expand file tree Collapse file tree 6 files changed +142
-3
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ module.exports = {
16
16
'ember-best-practices/no-observers' : 2 ,
17
17
'ember-best-practices/require-dependent-keys' : 2 ,
18
18
'ember-best-practices/no-lifecycle-events' : 2 ,
19
- 'ember-best-practices/no-attrs-snapshot' : 2
19
+ 'ember-best-practices/no-attrs-snapshot' : 2 ,
20
+ 'ember-best-practices/no-global-jquery' : 2
20
21
}
21
22
} ;
Original file line number Diff line number Diff line change
1
+ # No Global jQuery
2
+
3
+ ** TL;DR** Do not use global ` $ ` . Use ` Ember.$ ` instead.
4
+
5
+ In general, we want application code to reference the version of jQuery that's been directly pinned
6
+ to the version of Ember used. This helps avoid version conflicts, and ensures that code inside modules
7
+ isn't reliant on global variables.
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @fileOverview Disallow the use of global `$`.
3
+ * @author Steve Calvert
4
+ */
5
+ 'use strict' ;
6
+
7
+ /**
8
+ * Determines if this expression matches a global jQuery invocation.
9
+ * @param {ASTNode } node The identifier node.
10
+ * @returns {Boolean } Returns true if the expression matches, otherwise false.
11
+ */
12
+ function isGlobalJquery ( node ) {
13
+ return node . callee && node . callee . name === '$' ;
14
+ }
15
+
16
+ const MESSAGE = 'Do not use global `$`. Use `Ember.$` instead. Please see the following guide for more infromation: https://github.com/chadhietala/ember-best-practices/blob/master/guides/rules/no-global-jquery.md' ;
17
+
18
+ module . exports = {
19
+ docs : {
20
+ description : 'Disallow the use of global `$`' ,
21
+ category : 'Best Practices' ,
22
+ recommended : true
23
+ } ,
24
+ meta : {
25
+ message : MESSAGE
26
+ } ,
27
+ create ( context ) {
28
+ return {
29
+ CallExpression ( node ) {
30
+ if ( isGlobalJquery ( node ) ) {
31
+ context . report ( node , MESSAGE ) ;
32
+ }
33
+ }
34
+ } ;
35
+ }
36
+ } ;
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ const MESSAGE = rule.meta.message;
3
3
const RuleTester = require ( 'eslint' ) . RuleTester ;
4
4
const ruleTester = new RuleTester ( ) ;
5
5
6
- ruleTester . run ( 'no-observers ' , rule , {
6
+ ruleTester . run ( 'no-attrs ' , rule , {
7
7
valid : [
8
8
{
9
9
code : `
Original file line number Diff line number Diff line change
1
+ const rule = require ( '../../../lib/rules/no-global-jquery' ) ;
2
+ const MESSAGE = rule . meta . message ;
3
+ const RuleTester = require ( 'eslint' ) . RuleTester ;
4
+ const ruleTester = new RuleTester ( ) ;
5
+
6
+ ruleTester . run ( 'no-global-jquery' , rule , {
7
+ valid : [
8
+ {
9
+ code : `
10
+ export default Ember.Component({
11
+ valid1() {
12
+ this.v1 = Ember.$('.v1');
13
+ },
14
+ });` ,
15
+ parserOptions : {
16
+ ecmaVersion : 6 ,
17
+ sourceType : 'module'
18
+ }
19
+ } ,
20
+ {
21
+ code : `
22
+ export default Ember.Component({
23
+ valid2() {
24
+ this.v2 = this.$();
25
+ },
26
+ });` ,
27
+ parserOptions : {
28
+ ecmaVersion : 6 ,
29
+ sourceType : 'module'
30
+ }
31
+ } ,
32
+ {
33
+ code : `
34
+ export default Ember.Component({
35
+ actions: {
36
+ valid3() {
37
+ this.v3 = Ember.$('v3');
38
+ }
39
+ }
40
+ });` ,
41
+ parserOptions : {
42
+ ecmaVersion : 6 ,
43
+ sourceType : 'module'
44
+ }
45
+ } ,
46
+ {
47
+ code : `
48
+ export default Ember.Component({
49
+ actions: {
50
+ valid4() {
51
+ this.v4 = this.$('v4');
52
+ }
53
+ }
54
+ });` ,
55
+ parserOptions : {
56
+ ecmaVersion : 6 ,
57
+ sourceType : 'module'
58
+ }
59
+ }
60
+ ] ,
61
+ invalid : [
62
+ {
63
+ code : `
64
+ export default Ember.Component({
65
+ init() {
66
+ this.el = $('.test');
67
+ }
68
+ });` ,
69
+ parserOptions : {
70
+ ecmaVersion : 6 ,
71
+ sourceType : 'module'
72
+ } ,
73
+ errors : [ {
74
+ message : MESSAGE
75
+ } ]
76
+ } ,
77
+ {
78
+ code : `
79
+ export default Ember.Component({
80
+ actions: {
81
+ invalid1() {
82
+ this.inv1 = $('.invalid1');
83
+ }
84
+ }
85
+ });` ,
86
+ parserOptions : {
87
+ ecmaVersion : 6 ,
88
+ sourceType : 'module'
89
+ } ,
90
+ errors : [ {
91
+ message : MESSAGE
92
+ } ]
93
+ }
94
+ ]
95
+ } ) ;
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ const MESSAGE = rule.meta.message;
3
3
const RuleTester = require ( 'eslint' ) . RuleTester ;
4
4
const ruleTester = new RuleTester ( ) ;
5
5
6
- ruleTester . run ( 'no-observers ' , rule , {
6
+ ruleTester . run ( 'require-dependent-keys ' , rule , {
7
7
valid : [
8
8
{
9
9
code : `
You can’t perform that action at this time.
0 commit comments