line_push/node_modules/eslint-plugin-nuxt/lib/rules/no-env-in-hooks.js
2022-07-21 03:28:35 +00:00

69 lines
2.3 KiB
JavaScript

/**
* @fileoverview disallow process.server and process.client in the following lifecycle hooks: beforeMount, mounted, beforeUpdate, updated, activated, deactivated, beforeDestroy and destroyed
* @author Xin Du <clark.duxin@gmail.com>
*/
'use strict'
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description:
'disallow process.server and process.client in the following lifecycle hooks: beforeMount, mounted, beforeUpdate, updated, activated, deactivated, beforeDestroy and destroyed',
category: 'base'
},
messages: {
noEnv: 'Unexpected {{name}} in {{funcName}}.'
}
},
create (context) {
// variables should be defined here
const forbiddenNodes = []
const options = context.options[0] || {}
const ENV = ['server', 'client']
const HOOKS = new Set(['beforeMount', 'mounted', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'beforeDestroy', 'destroyed'].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)
if (func) {
for (const { name, node: child } of forbiddenNodes) {
if (utils.isInFunction(func, child)) {
context.report({
node: child,
messageId: 'noEnv',
data: {
name,
funcName
}
})
}
}
}
}
})
}
}
}