forked from ember-cli/eslint-plugin-ember
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-private-routing-service.js
168 lines (156 loc) · 6.71 KB
/
no-private-routing-service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require('../../../lib/rules/no-private-routing-service');
const RuleTester = require('eslint').RuleTester;
const {
PRIVATE_ROUTING_SERVICE_ERROR_MESSAGE,
ROUTER_MICROLIB_ERROR_MESSAGE,
ROUTER_MAIN_ERROR_MESSAGE,
} = rule;
const EMBER_IMPORT = "import Ember from 'ember';";
const SERVICE_IMPORT = "import {inject as service} from '@ember/service';";
const NEW_SERVICE_IMPORT = "import {service} from '@ember/service';";
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester({
parser: require.resolve('@babel/eslint-parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
});
ruleTester.run('no-private-routing-service', rule, {
valid: [
// Classic
`${SERVICE_IMPORT} export default Component.extend({ someService: service('routing') });`,
`${SERVICE_IMPORT} export default Component.extend({ someService: service('-router') });`,
`${SERVICE_IMPORT} export default Component.extend({ '-routing': service('routing') });`,
`${SERVICE_IMPORT} export default Component.extend({ '-routing': service('-router') });`,
`${EMBER_IMPORT} export default Component.extend({ someService: Ember.inject.service('routing') });`,
`${EMBER_IMPORT} export default Component.extend({ someService: Ember.inject.service('-router') });`,
`${EMBER_IMPORT} export default Component.extend({ '-routing': Ember.inject.service('routing') });`,
`${EMBER_IMPORT} export default Component.extend({ '-routing': Ember.inject.service('-router') });`,
"Component.extend({ routing: someOtherFunction('-routing') });",
`${SERVICE_IMPORT} export default Component.extend({ someService: service() });`,
'export default Component.extend({ notAService: "a value" });',
'export default Component.extend({ anInt: 25 });',
// Octane
`${SERVICE_IMPORT} export default class MyComponent extends Component { @service router; }`,
`${SERVICE_IMPORT} export default class MyComponent extends Component { @service('router') routing; }`,
`${SERVICE_IMPORT} export default class MyComponent extends Component { @service routing; }`,
`${SERVICE_IMPORT} export default class MyComponent extends Component { @service('routing') routing; }`,
`${NEW_SERVICE_IMPORT} export default class MyComponent extends Component { @service('routing') routing; }`,
`
export default class MyComponent extends Component {
@computed('-routing', 'lastName')
get fullName() {
return \`${this.firstName} ${this.lastName}\`;
}
}
`,
`${SERVICE_IMPORT} class MyComponent extends Component { @service() routing; }`,
`${SERVICE_IMPORT} class MyComponent extends Component { @service() notRouting; }`,
'class MyComponent extends Component { aProp="routing"; }',
'class MyComponent extends Component { aProp="-routing"; }',
'class MyComponent extends Component { aProp="another value"; }',
'class MyComponent extends Component { anIntProp=25; }',
// _routerMicrolib
{ code: "get(this, 'router._routerMicrolib');", options: [{ catchRouterMicrolib: false }] },
{ code: 'this.router._routerMicrolib;', options: [{ catchRouterMicrolib: false }] },
"get(this, 'router.somethingElse');",
'this.router.somethingElse;',
// router:main
{ code: "getOwner(this).lookup('router:main');", options: [{ catchRouterMain: false }] },
{ code: "owner.lookup('router:main');", options: [{ catchRouterMain: false }] },
{ code: "this.owner.lookup('router:main');", options: [{ catchRouterMain: false }] },
"getOwner(this).lookup('router:somethingElse');",
"owner.lookup('router:somethingElse');",
"this.owner.lookup('router:somethingElse');",
],
invalid: [
// Classic
{
code: `${SERVICE_IMPORT} export default Component.extend({ routing: service('-routing') });`,
output: null,
errors: [{ message: PRIVATE_ROUTING_SERVICE_ERROR_MESSAGE, type: 'Property' }],
},
// Octane
{
code: `${SERVICE_IMPORT} export default class MyComponent extends Component { @service('-routing') routing; }`,
output: null,
errors: [
{
message: PRIVATE_ROUTING_SERVICE_ERROR_MESSAGE,
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
// Octane, new import
{
code: `${NEW_SERVICE_IMPORT} export default class MyComponent extends Component { @service('-routing') routing; }`,
output: null,
errors: [
{
message: PRIVATE_ROUTING_SERVICE_ERROR_MESSAGE,
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
// Octane, new import, renamed
{
code: "import {service as aliasedService} from '@ember/service'; export default class MyComponent extends Component { @aliasedService('-routing') routing; }",
output: null,
errors: [
{
message: PRIVATE_ROUTING_SERVICE_ERROR_MESSAGE,
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
// _routerMicrolib (`catchRouterMicrolib` option on)
{
code: "get(this, 'router._routerMicrolib');",
output: null,
errors: [{ message: ROUTER_MICROLIB_ERROR_MESSAGE, type: 'Literal' }],
},
{
code: "get(this, 'router._router._routerMicrolib');",
output: null,
errors: [{ message: ROUTER_MICROLIB_ERROR_MESSAGE, type: 'Literal' }],
},
{
code: 'this.router._routerMicrolib;',
output: null,
errors: [{ message: ROUTER_MICROLIB_ERROR_MESSAGE, type: 'Identifier' }],
},
{
code: 'this.router._router._routerMicrolib;',
output: null,
errors: [{ message: ROUTER_MICROLIB_ERROR_MESSAGE, type: 'Identifier' }],
},
// router:main (`catchRouterMain` option on)
{
code: "getOwner(this).lookup('router:main');",
output: null,
errors: [{ message: ROUTER_MAIN_ERROR_MESSAGE, type: 'CallExpression' }],
},
{
code: "owner.lookup('router:main');",
output: null,
errors: [{ message: ROUTER_MAIN_ERROR_MESSAGE, type: 'CallExpression' }],
},
{
code: "this.owner.lookup('router:main');",
output: null,
errors: [{ message: ROUTER_MAIN_ERROR_MESSAGE, type: 'CallExpression' }],
},
{
// Optional chaining.
code: "this?.owner?.lookup?.('router:main');",
output: null,
errors: [{ message: ROUTER_MAIN_ERROR_MESSAGE, type: 'CallExpression' }],
},
],
});