diff --git a/docs/rules/no-env-in-hooks.md b/docs/rules/no-env-in-hooks.md index dbec1e1..e74babf 100644 --- a/docs/rules/no-env-in-hooks.md +++ b/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: @@ -22,6 +22,11 @@ export default { if (process.client) { const foo = 'bar' } + }, + beforeDestroy() { + if (process.browser) { + const foo = 'bar' + } } } @@ -38,6 +43,9 @@ export default { }, beforeMount() { const foo = 'bar' + }, + beforeDestroy() { + const foo = 'bar' } } diff --git a/lib/rules/__tests__/no-env-in-hooks.test.js b/lib/rules/__tests__/no-env-in-hooks.test.js index 4be206a..e2d9207 100644 --- a/lib/rules/__tests__/no-env-in-hooks.test.js +++ b/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 */ 'use strict' @@ -34,6 +34,9 @@ ruleTester.run('no-env-in-hooks', rule, { }, beforeMount() { const foo = 'bar' + }, + beforeDestroy() { + const foo = 'bar' } } `, @@ -55,6 +58,11 @@ ruleTester.run('no-env-in-hooks', rule, { if(process.client) { const foo = 'bar' } + }, + beforeDestroy() { + if(process.browser) { + const foo = 'bar' + } } } `, @@ -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 }, @@ -80,6 +91,11 @@ ruleTester.run('no-env-in-hooks', rule, { if(process['server']) { const foo = 'bar' } + }, + beforeDestroy() { + if(process['browser']) { + const foo = 'bar' + } } } `, @@ -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 } diff --git a/lib/rules/no-env-in-hooks.js b/lib/rules/no-env-in-hooks.js index a6bf192..1d3c713 100644 --- a/lib/rules/no-env-in-hooks.js +++ b/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 */ 'use strict' @@ -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 || [])) // ----------------------------------------------------------------------