Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 94552c7

Browse files
committedDec 6, 2018
feat: add no-this-in-fetch
1 parent fb94d7b commit 94552c7

File tree

4 files changed

+160
-13
lines changed

4 files changed

+160
-13
lines changed
 
+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @fileoverview Prevent using this in fetch
3+
* @author Clark Du
4+
*/
5+
'use strict'
6+
7+
// ------------------------------------------------------------------------------
8+
// Requirements
9+
// ------------------------------------------------------------------------------
10+
11+
var rule = require('../no-this-in-fetch')
12+
13+
var RuleTester = require('eslint').RuleTester
14+
15+
const parserOptions = {
16+
ecmaVersion: 2018,
17+
sourceType: 'module'
18+
}
19+
20+
// ------------------------------------------------------------------------------
21+
// Tests
22+
// ------------------------------------------------------------------------------
23+
24+
var ruleTester = new RuleTester()
25+
ruleTester.run('no-this-in-fetch', rule, {
26+
27+
valid: [
28+
{
29+
filename: 'test.vue',
30+
code: `
31+
export default {
32+
...foo,
33+
fetch() {
34+
}
35+
}
36+
`,
37+
parserOptions
38+
}
39+
],
40+
41+
invalid: [
42+
{
43+
filename: 'test.vue',
44+
code: `
45+
export default {
46+
...foo,
47+
fetch() {
48+
if(this.$route.path === 'foo') {}
49+
}
50+
}
51+
`,
52+
errors: [{
53+
message: 'Unexpected this in fetch.',
54+
type: 'Identifier'
55+
}],
56+
parserOptions
57+
},
58+
{
59+
filename: 'test.vue',
60+
code: `
61+
export default {
62+
...foo,
63+
async fetch() {
64+
if(this.$route.path === 'foo') {}
65+
}
66+
}
67+
`,
68+
errors: [{
69+
message: 'Unexpected this in fetch.',
70+
type: 'Identifier'
71+
}],
72+
parserOptions
73+
},
74+
{
75+
filename: 'test.vue',
76+
code: `
77+
export default {
78+
...foo,
79+
fetch: () => {
80+
if(this.$route.path === 'foo') {}
81+
}
82+
}
83+
`,
84+
errors: [{
85+
message: 'Unexpected this in fetch.',
86+
type: 'Identifier'
87+
}],
88+
parserOptions
89+
},
90+
{
91+
filename: 'test.vue',
92+
code: `
93+
export default {
94+
...foo,
95+
fetch: function test() {
96+
if(this.$route.path === 'foo') {}
97+
}
98+
}
99+
`,
100+
errors: [{
101+
message: 'Unexpected this in fetch.',
102+
type: 'Identifier'
103+
}],
104+
parserOptions
105+
}
106+
]
107+
})

‎lib/rules/no-this-in-async-data.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ module.exports = {
4848
isThisUsed = true
4949
}
5050

51-
function getFuncNodeWithName (node, name) {
52-
return node.properties.find(item => item.type === 'Property' &&
53-
utils.getStaticPropertyName(item) === name &&
54-
(item.value.type === 'ArrowFunctionExpression' || item.value.type === 'FunctionExpression')
55-
)
56-
}
57-
5851
// ----------------------------------------------------------------------
5952
// Public
6053
// ----------------------------------------------------------------------
@@ -67,7 +60,7 @@ module.exports = {
6760
ThisExpression: markThisUsed,
6861
Super: markThisUsed,
6962
...utils.executeOnVue(context, obj => {
70-
const node = getFuncNodeWithName(obj, 'asyncData')
63+
const node = utils.getFuncNodeWithName(obj, 'asyncData')
7164
if (!node) return
7265

7366
forbiddenNodes.forEach(el => {

‎lib/rules/no-this-in-fetch.js

+39-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
'use strict'
66

7+
const utils = require('../utils')
8+
79
// ------------------------------------------------------------------------------
810
// Rule Definition
911
// ------------------------------------------------------------------------------
@@ -17,26 +19,59 @@ module.exports = {
1719
fixable: null, // or "code" or "whitespace"
1820
schema: [
1921
// fill in your schema
20-
]
22+
],
23+
messages: {
24+
noThis: 'Unexpected this in fetch.'
25+
}
2126
},
2227

2328
create: function (context) {
2429
// variables should be defined here
30+
const forbiddenNodes = []
31+
let isThisUsed
2532

2633
// ----------------------------------------------------------------------
2734
// Helpers
2835
// ----------------------------------------------------------------------
2936

30-
// any helper functions should go here or else delete this section
37+
function enterFunction () {
38+
isThisUsed = false
39+
}
40+
41+
function exitFunction (node) {
42+
if (isThisUsed) {
43+
forbiddenNodes.push(node)
44+
}
45+
}
46+
47+
function markThisUsed () {
48+
isThisUsed = true
49+
}
3150

3251
// ----------------------------------------------------------------------
3352
// Public
3453
// ----------------------------------------------------------------------
3554

3655
return {
56+
FunctionExpression: enterFunction,
57+
'FunctionExpression:exit': exitFunction,
58+
ArrowFunctionExpression: enterFunction,
59+
'ArrowFunctionExpression:exit': exitFunction,
60+
ThisExpression: markThisUsed,
61+
Super: markThisUsed,
62+
...utils.executeOnVue(context, obj => {
63+
const node = utils.getFuncNodeWithName(obj, 'fetch')
64+
if (!node) return
3765

38-
// give me methods
39-
66+
forbiddenNodes.forEach(el => {
67+
if (node.value === el) {
68+
context.report({
69+
node: node.key,
70+
messageId: 'noThis'
71+
})
72+
}
73+
})
74+
})
4075
}
4176
}
4277
}

‎lib/utils/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
module.exports = require('eslint-plugin-vue/lib/utils')
1+
const utils = require('eslint-plugin-vue/lib/utils')
2+
3+
module.exports = Object.assign(
4+
{
5+
getFuncNodeWithName (node, name) {
6+
return node.properties.find(item => item.type === 'Property' &&
7+
utils.getStaticPropertyName(item) === name &&
8+
(item.value.type === 'ArrowFunctionExpression' || item.value.type === 'FunctionExpression')
9+
)
10+
}
11+
},
12+
utils
13+
)

0 commit comments

Comments
 (0)
This repository has been archived.