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

Commit

Permalink
fix: disallow process.browser in no-env-in-hooks (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
FloEdelmann committed Dec 19, 2020
1 parent 5c14eac commit 07299c0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
12 changes: 10 additions & 2 deletions docs/rules/no-env-in-hooks.md
@@ -1,12 +1,12 @@
# nuxt/no-env-in-hooks

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

## Rule Details

This rule is for preventing using `process.server/process.client` in client only Vue lifecycle hooks since they're only executed in client side.
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.

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

Expand All @@ -22,6 +22,11 @@ export default {
if (process.client) {
const foo = 'bar'
}
},
beforeDestroy() {
if (process.browser) {
const foo = 'bar'
}
}
}

Expand All @@ -38,6 +43,9 @@ export default {
},
beforeMount() {
const foo = 'bar'
},
beforeDestroy() {
const foo = 'bar'
}
}

Expand Down
21 changes: 20 additions & 1 deletion lib/rules/__tests__/no-env-in-hooks.test.js
@@ -1,5 +1,5 @@
/**
* @fileoverview disallow `process.server/process.client` in `Vue Lifecycle Hooks`
* @fileoverview disallow `process.server`/`process.client`/`process.browser` in `Vue Lifecycle Hooks`
* @author Xin Du <clark.duxin@gmail.com>
*/
'use strict'
Expand Down Expand Up @@ -34,6 +34,9 @@ ruleTester.run('no-env-in-hooks', rule, {
},
beforeMount() {
const foo = 'bar'
},
beforeDestroy() {
const foo = 'bar'
}
}
`,
Expand All @@ -55,6 +58,11 @@ ruleTester.run('no-env-in-hooks', rule, {
if(process.client) {
const foo = 'bar'
}
},
beforeDestroy() {
if(process.browser) {
const foo = 'bar'
}
}
}
`,
Expand All @@ -64,6 +72,9 @@ ruleTester.run('no-env-in-hooks', rule, {
}, {
message: 'Unexpected process.client in beforeMount.',
type: 'MemberExpression'
}, {
message: 'Unexpected process.browser in beforeDestroy.',
type: 'MemberExpression'
}],
parserOptions
},
Expand All @@ -80,6 +91,11 @@ ruleTester.run('no-env-in-hooks', rule, {
if(process['server']) {
const foo = 'bar'
}
},
beforeDestroy() {
if(process['browser']) {
const foo = 'bar'
}
}
}
`,
Expand All @@ -89,6 +105,9 @@ ruleTester.run('no-env-in-hooks', rule, {
}, {
message: 'Unexpected process.server in beforeMount.',
type: 'MemberExpression'
}, {
message: 'Unexpected process.browser in beforeDestroy.',
type: 'MemberExpression'
}],
parserOptions
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-env-in-hooks.js
@@ -1,5 +1,5 @@
/**
* @fileoverview disallow process.server and process.client in the following lifecycle hooks: beforeMount, mounted, beforeUpdate, updated, activated, deactivated, beforeDestroy and destroyed
* @fileoverview disallow process.server, process.client and process.browser in the following lifecycle hooks: beforeMount, mounted, beforeUpdate, updated, activated, deactivated, beforeDestroy and destroyed
* @author Xin Du <clark.duxin@gmail.com>
*/
'use strict'
Expand Down Expand Up @@ -27,7 +27,7 @@ module.exports = {
const forbiddenNodes = []
const options = context.options[0] || {}

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

// ----------------------------------------------------------------------
Expand Down

0 comments on commit 07299c0

Please sign in to comment.