Skip to content

Commit 886b45d

Browse files
committed
Add AwaitExpression & YieldExpression & node.async
1 parent 37ba44b commit 886b45d

File tree

2 files changed

+90
-60
lines changed

2 files changed

+90
-60
lines changed

lib/rules/no-async-in-computed-properties.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,77 @@
66

77
const utils = require('../utils')
88

9-
const methodNames = [
10-
'then',
11-
'catch'
12-
]
9+
// const methodNames = [
10+
// 'then',
11+
// 'catch'
12+
// ]
1313

1414
/**
1515
* Checks if a node has a promise method called.
1616
*/
17-
function hasPromiseMethod (node) {
18-
if (node.type === 'CallExpression' && node.callee) {
19-
var property = node.callee.property
20-
if (property && methodNames.indexOf(property.name) >= 0) {
21-
return property.name
22-
}
23-
}
24-
return false
25-
}
26-
27-
function checkAsync (data, context) {
28-
29-
}
17+
// function hasPromiseMethod (node) {
18+
// if (node.type === 'CallExpression' && node.callee) {
19+
// var property = node.callee.property
20+
// if (property && methodNames.indexOf(property.name) >= 0) {
21+
// return property.name
22+
// }
23+
// }
24+
// return false
25+
// }
3026

3127
function create (context) {
3228
const forbiddenNodes = []
3329

30+
function onFunctionExit (node) {
31+
}
32+
33+
function onFunctionEnter (node) {
34+
if (node.async) {
35+
forbiddenNodes.push({
36+
node: node,
37+
type: 'async'
38+
})
39+
}
40+
}
41+
3442
return Object.assign({},
3543
{
44+
FunctionDeclaration: onFunctionEnter,
45+
'FunctionDeclaration:exit': onFunctionExit,
46+
47+
FunctionExpression: onFunctionEnter,
48+
'FunctionExpression:exit': onFunctionExit,
3649

50+
ArrowFunctionExpression: onFunctionEnter,
51+
'ArrowFunctionExpression:exit': onFunctionExit,
52+
53+
YieldExpression (node) {
54+
// await nodes are YieldExpression's with babel-eslint < 7.0.0
55+
forbiddenNodes.push({
56+
node: node,
57+
type: 'yield'
58+
})
59+
},
60+
61+
AwaitExpression (node) {
62+
forbiddenNodes.push({
63+
node: node,
64+
type: 'await'
65+
})
66+
}
3767
},
3868
utils.executeOnVueComponent(context, (properties) => {
3969
const computedProperties = utils.getComputedProperties(properties)
4070

4171
computedProperties.forEach(cp => {
42-
forbiddenNodes.forEach(node => {
72+
forbiddenNodes.forEach(({ type, node }) => {
4373
if (
4474
node.loc.start.line >= cp.value.loc.start.line &&
4575
node.loc.end.line <= cp.value.loc.end.line
4676
) {
4777
context.report({
4878
node: node,
49-
message: `Unexpected async effect in "${cp.key}" computed property.`
79+
message: `Unexpected ${type} effect in "${cp.key}" computed property.`
5080
})
5181
}
5282
})

tests/lib/rules/no-async-in-computed-properties.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const rule = require('../../../lib/rules/no-async-in-computed-properties')
1717

1818
const ruleTester = new RuleTester({
1919
parser: 'vue-eslint-parser',
20-
parserOptions: { ecmaVersion: 2015 }
20+
parserOptions: { ecmaVersion: 2015, sourceType: 'module' }
2121
})
2222

2323
ruleTester.run('no-async-in-computed-properties', rule, {
@@ -30,18 +30,18 @@ ruleTester.run('no-async-in-computed-properties', rule, {
3030
{
3131
filename: 'test.vue',
3232
code: `
33-
computed: {
34-
foo: function () {
35-
var bar = 0
36-
try {
37-
bar = bar / 0
38-
} catch (e) {
39-
return e
40-
} finally {
41-
return bar
33+
computed: {
34+
foo: function () {
35+
var bar = 0
36+
try {
37+
bar = bar / 0
38+
} catch (e) {
39+
return e
40+
} finally {
41+
return bar
42+
}
4243
}
4344
}
44-
}
4545
`
4646
}
4747
],
@@ -50,78 +50,78 @@ ruleTester.run('no-async-in-computed-properties', rule, {
5050
{
5151
filename: 'test.vue',
5252
code: `
53-
export default {
54-
computed: {
55-
foo: async function () {
56-
return await someFunc()
53+
export default {
54+
computed: {
55+
foo: async function () {
56+
return await someFunc()
57+
}
5758
}
5859
}
59-
}
6060
`,
6161
errors: ['Computed properies cannot have side effect.']
6262
},
6363
{
6464
filename: 'test.vue',
6565
code: `
66-
export default {
67-
computed: {
68-
foo: async function () {
69-
return new Promise((resolve, reject) => {}) }
66+
export default {
67+
computed: {
68+
foo: async function () {
69+
return new Promise((resolve, reject) => {}) }
70+
}
7071
}
7172
}
72-
}
7373
`,
7474
errors: ['Computed properies cannot have side effect.']
7575
},
7676
{
7777
filename: 'test.vue',
7878
code: `
79-
export default {
80-
computed: {
81-
foo: function () {
82-
return bar.then(response => {})
79+
export default {
80+
computed: {
81+
foo: function () {
82+
return bar.then(response => {})
83+
}
8384
}
8485
}
85-
}
8686
`,
8787
errors: ['Computed properies cannot have side effect.']
8888
},
8989
{
9090
filename: 'test.vue',
9191
code: `
92-
export default {
93-
computed: {
94-
foo: function () {
95-
return bar.catch(e => {})
92+
export default {
93+
computed: {
94+
foo: function () {
95+
return bar.catch(e => {})
96+
}
9697
}
9798
}
98-
}
9999
`,
100100
errors: ['Computed properies cannot have side effect.']
101101
},
102102
{
103103
filename: 'test.vue',
104104
code: `
105-
export default {
106-
computed: {
107-
foo: function () {
108-
return Promise.all([])
105+
export default {
106+
computed: {
107+
foo: function () {
108+
return Promise.all([])
109+
}
109110
}
110111
}
111-
}
112112
`,
113113
errors: ['Computed properies cannot have side effect.']
114114
},
115115
{
116116
filename: 'test.vue',
117117
code: `
118-
export default {
119-
computed: {
120-
foo: function () {
121-
return bar.finally(res => {})
118+
export default {
119+
computed: {
120+
foo: function () {
121+
return bar.finally(res => {})
122+
}
122123
}
123124
}
124-
}
125125
`,
126126
errors: ['Computed properies cannot have side effect.']
127127
},

0 commit comments

Comments
 (0)