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

Commit 07299c0

Browse files
authored
fix: disallow process.browser in no-env-in-hooks (#127)
1 parent 5c14eac commit 07299c0

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

Diff for: docs/rules/no-env-in-hooks.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# nuxt/no-env-in-hooks
22

3-
> Disallow `process.server` and `process.client` in the following lifecycle hooks: `beforeMount`, `mounted`, `beforeUpdate`, `updated`, `activated`, `deactivated`, `beforeDestroy` and `destroyed`.
3+
> Disallow `process.server`, `process.client` and `process.browser` in the following lifecycle hooks: `beforeMount`, `mounted`, `beforeUpdate`, `updated`, `activated`, `deactivated`, `beforeDestroy` and `destroyed`.
44
55
- :gear: This rule is included in `"plugin:nuxt/base"`.
66

77
## Rule Details
88

9-
This rule is for preventing using `process.server/process.client` in client only Vue lifecycle hooks since they're only executed in client side.
9+
This rule is for preventing using `process.server`/`process.client`/`process.browser` in client only Vue lifecycle hooks since they're only executed in client side.
1010

1111
Examples of **incorrect** code for this rule:
1212

@@ -22,6 +22,11 @@ export default {
2222
if (process.client) {
2323
const foo = 'bar'
2424
}
25+
},
26+
beforeDestroy() {
27+
if (process.browser) {
28+
const foo = 'bar'
29+
}
2530
}
2631
}
2732

@@ -38,6 +43,9 @@ export default {
3843
},
3944
beforeMount() {
4045
const foo = 'bar'
46+
},
47+
beforeDestroy() {
48+
const foo = 'bar'
4149
}
4250
}
4351

Diff for: lib/rules/__tests__/no-env-in-hooks.test.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview disallow `process.server/process.client` in `Vue Lifecycle Hooks`
2+
* @fileoverview disallow `process.server`/`process.client`/`process.browser` in `Vue Lifecycle Hooks`
33
* @author Xin Du <[email protected]>
44
*/
55
'use strict'
@@ -34,6 +34,9 @@ ruleTester.run('no-env-in-hooks', rule, {
3434
},
3535
beforeMount() {
3636
const foo = 'bar'
37+
},
38+
beforeDestroy() {
39+
const foo = 'bar'
3740
}
3841
}
3942
`,
@@ -55,6 +58,11 @@ ruleTester.run('no-env-in-hooks', rule, {
5558
if(process.client) {
5659
const foo = 'bar'
5760
}
61+
},
62+
beforeDestroy() {
63+
if(process.browser) {
64+
const foo = 'bar'
65+
}
5866
}
5967
}
6068
`,
@@ -64,6 +72,9 @@ ruleTester.run('no-env-in-hooks', rule, {
6472
}, {
6573
message: 'Unexpected process.client in beforeMount.',
6674
type: 'MemberExpression'
75+
}, {
76+
message: 'Unexpected process.browser in beforeDestroy.',
77+
type: 'MemberExpression'
6778
}],
6879
parserOptions
6980
},
@@ -80,6 +91,11 @@ ruleTester.run('no-env-in-hooks', rule, {
8091
if(process['server']) {
8192
const foo = 'bar'
8293
}
94+
},
95+
beforeDestroy() {
96+
if(process['browser']) {
97+
const foo = 'bar'
98+
}
8399
}
84100
}
85101
`,
@@ -89,6 +105,9 @@ ruleTester.run('no-env-in-hooks', rule, {
89105
}, {
90106
message: 'Unexpected process.server in beforeMount.',
91107
type: 'MemberExpression'
108+
}, {
109+
message: 'Unexpected process.browser in beforeDestroy.',
110+
type: 'MemberExpression'
92111
}],
93112
parserOptions
94113
}

Diff for: lib/rules/no-env-in-hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @fileoverview disallow process.server and process.client in the following lifecycle hooks: beforeMount, mounted, beforeUpdate, updated, activated, deactivated, beforeDestroy and destroyed
2+
* @fileoverview disallow process.server, process.client and process.browser in the following lifecycle hooks: beforeMount, mounted, beforeUpdate, updated, activated, deactivated, beforeDestroy and destroyed
33
* @author Xin Du <[email protected]>
44
*/
55
'use strict'
@@ -27,7 +27,7 @@ module.exports = {
2727
const forbiddenNodes = []
2828
const options = context.options[0] || {}
2929

30-
const ENV = ['server', 'client']
30+
const ENV = ['server', 'client', 'browser']
3131
const HOOKS = new Set(['beforeMount', 'mounted', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'beforeDestroy', 'destroyed'].concat(options.methods || []))
3232

3333
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)