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

Commit a0e3f73

Browse files
committedMar 17, 2020
fix: enable no-this-in-fetch only for nuxt < 2.12
1 parent d56b5be commit a0e3f73

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed
 

‎docs/rules/no-this-in-fetch-data.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
This rule is for preventing using `this` in `asyncData/fetch`
1010

11+
> **NOTE**: `No this in fetch` is only for Nuxt.js < 2.12.0, Nuxt.js v2.12 has introduced new `fetch` API which supports `this`.
12+
1113
Examples of **incorrect** code for this rule:
1214

1315
```js

‎lib/rules/__tests__/no-this-in-fetch-data.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ ruleTester.run('no-this-in-fetch-data', rule, {
119119
message: 'Unexpected this in fetch.',
120120
type: 'ThisExpression'
121121
}],
122+
options: [{ methods: ['fetch'] }],
122123
parserOptions
123124
},
124125
{
@@ -135,6 +136,7 @@ ruleTester.run('no-this-in-fetch-data', rule, {
135136
message: 'Unexpected this in fetch.',
136137
type: 'ThisExpression'
137138
}],
139+
options: [{ methods: ['fetch'] }],
138140
parserOptions
139141
},
140142
{
@@ -151,6 +153,7 @@ ruleTester.run('no-this-in-fetch-data', rule, {
151153
message: 'Unexpected this in fetch.',
152154
type: 'ThisExpression'
153155
}],
156+
options: [{ methods: ['fetch'] }],
154157
parserOptions
155158
},
156159
{
@@ -167,6 +170,7 @@ ruleTester.run('no-this-in-fetch-data', rule, {
167170
message: 'Unexpected this in fetch.',
168171
type: 'ThisExpression'
169172
}],
173+
options: [{ methods: ['fetch'] }],
170174
parserOptions
171175
}
172176
]

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*/
55
'use strict'
66

7+
const satisfies = require('semver/ranges/intersects')
78
const utils = require('../utils')
9+
const resolver = require('../utils/resolver')
810

911
// ------------------------------------------------------------------------------
1012
// Rule Definition
@@ -27,9 +29,15 @@ module.exports = {
2729
const options = context.options[0] || {}
2830

2931
const HOOKS = new Set(
30-
['asyncData', 'fetch'].concat(options.methods || [])
32+
['asyncData'].concat(options.methods || [])
3133
)
3234

35+
const { version } = resolver
36+
// new fetch API can use `this` since 2.12.0
37+
if (version && satisfies(version, '<2.12.0')) {
38+
HOOKS.add('fetch')
39+
}
40+
3341
let nodeUsingThis = []
3442

3543
// ----------------------------------------------------------------------

‎lib/utils/resolver.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
__version: undefined,
3+
get version () {
4+
if (this.__version === undefined) {
5+
return this.loadNuxtPkg()
6+
}
7+
return this.__version
8+
},
9+
loadNuxtPkg () {
10+
try {
11+
const { version } = require('nuxt/package.json')
12+
this.__version = version
13+
} catch (e) {
14+
this.__version = false
15+
}
16+
17+
return this.__version
18+
}
19+
}

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
},
5252
"dependencies": {
5353
"eslint-plugin-vue": "^6.2.1",
54+
"semver": "^7.1.3",
5455
"vue-eslint-parser": "^7.0.0"
5556
}
5657
}

‎yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4420,7 +4420,7 @@ semver@6.3.0, semver@^6.0.0, semver@^6.1.0, semver@^6.1.2, semver@^6.3.0:
44204420
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
44214421
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==
44224422

4423-
semver@^7.1.1:
4423+
semver@^7.1.1, semver@^7.1.3:
44244424
version "7.1.3"
44254425
resolved "https://registry.yarnpkg.com/semver/-/semver-7.1.3.tgz#e4345ce73071c53f336445cfc19efb1c311df2a6"
44264426
integrity sha512-ekM0zfiA9SCBlsKa2X1hxyxiI4L3B6EbVJkkdgQXnSEEaHlGdvyodMruTiulSRWMMB4NeIuYNMC9rTKTz97GxA==

0 commit comments

Comments
 (0)
This repository has been archived.