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

Commit

Permalink
feat: add no-env-in-mounted
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdo committed Dec 10, 2018
1 parent b81fbbd commit 0037b34
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/rules/no-env-in-mounted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# nuxt/no-env-in-mounted

> disallow `process.server/process.client` in `mounted/beforeMount`
- :gear: This rule is included in `"plugin:nuxt/base"`.

## Rule Details

This rule is for preventing using `process.server/process.client` in `mounted/beforeMount` since they're only executed in client side.

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

```js

export default {
mounted() {
const foo = 'bar'
},
beforeMount() {
const foo = 'bar'
}
}

```

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

```js

export default {
mounted() {
if(process.server) {
const foo = 'bar'
}
},
beforeMount() {
if(process.client) {
const foo = 'bar'
}
}
}

```

## :mag: Implementation

- [Rule source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/no-env-in-mounted.js)
- [Test source](https://github.com/nuxt/eslint-plugin-nuxt/blob/master/lib/rules/__test__/no-env-in-mounted.test.js)
1 change: 1 addition & 0 deletions lib/configs/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
],
rules: {
'nuxt/no-env-in-context': 'error',
'nuxt/no-env-in-mounted': 'error',
'nuxt/no-this-in-fetch-data': 'error'
}
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
rules: {
'no-env-in-context': require('./rules/no-env-in-context'),
'no-env-in-mounted': require('./rules/no-env-in-mounted'),
'no-globals-in-created': require('./rules/no-globals-in-created'),
'no-this-in-fetch-data': require('./rules/no-this-in-fetch-data'),
'no-timing-in-fetch-data': require('./rules/no-timing-in-fetch-data')
Expand Down
96 changes: 96 additions & 0 deletions lib/rules/__test__/no-env-in-mounted.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @fileoverview disallow `process.server/process.client` in `mounted/beforeMount`
* @author Xin Du <clark.duxin@gmail.com>
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

var rule = require('../no-env-in-mounted')

var RuleTester = require('eslint').RuleTester

const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module'
}

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

var ruleTester = new RuleTester()
ruleTester.run('no-env-in-mounted', rule, {

valid: [
{
filename: 'test.vue',
code: `
export default {
mounted() {
const foo = 'bar'
},
beforeMount() {
const foo = 'bar'
}
}
`,
parserOptions
}
],

invalid: [
{
filename: 'test.vue',
code: `
export default {
mounted() {
if(process.server) {
const foo = 'bar'
}
},
beforeMount() {
if(process.client) {
const foo = 'bar'
}
}
}
`,
errors: [{
message: 'Unexpected process.server in mounted.',
type: 'MemberExpression'
}, {
message: 'Unexpected process.client in beforeMount.',
type: 'MemberExpression'
}],
parserOptions
},
{
filename: 'test.vue',
code: `
export default {
mounted() {
if(process['client']) {
const foo = 'bar'
}
},
beforeMount() {
if(process['server']) {
const foo = 'bar'
}
}
}
`,
errors: [{
message: 'Unexpected process.client in mounted.',
type: 'MemberExpression'
}, {
message: 'Unexpected process.server in beforeMount.',
type: 'MemberExpression'
}],
parserOptions
}
]
})
66 changes: 66 additions & 0 deletions lib/rules/no-env-in-mounted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @fileoverview disallow `process.server/process.client` in `mounted/beforeMount`
* @author Xin Du <clark.duxin@gmail.com>
*/
'use strict'

const utils = require('../utils')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
meta: {
docs: {
description:
'disallow `process.server/process.client` in `mounted/beforeMount`',
category: 'base'
},
messages: {
noEnv: 'Unexpected {{name}} in {{funcName}}.'
}
},

create: function (context) {
// variables should be defined here
const forbiddenNodes = []
const options = context.options[0] || {}

const ENV = ['server', 'client']
const HOOKS = new Set(['mounted', 'beforeMount'].concat(options.methods || []))

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return {
MemberExpression (node) {
const objectName = node.object.name
if (objectName === 'process') {
const propertyName = node.computed ? node.property.value : node.property.name
if (propertyName && ENV.includes(propertyName)) {
forbiddenNodes.push({ name: 'process.' + propertyName, node })
}
}
},
...utils.executeOnVue(context, obj => {
for (const funcName of HOOKS) {
const func = utils.getFunctionWithName(obj, funcName)
for (const { name, node: child } of forbiddenNodes) {
if (utils.isInFunction(func, child)) {
context.report({
node: child,
messageId: 'noEnv',
data: {
name,
funcName
}
})
}
}
}
})
}
}
}

0 comments on commit 0037b34

Please sign in to comment.