This repository has been archived on 2024-07-27. You can view files and clone it, but cannot push or open issues or pull requests.
keksAccountGUI/node_modulesOLD/eslint-plugin-vue/lib/rules/no-boolean-default.js
2019-08-11 20:48:02 +02:00

95 lines
2.2 KiB
JavaScript

/**
* @fileoverview Prevents boolean defaults from being set
* @author Hiroki Osame
*/
'use strict'
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
function isBooleanProp (prop) {
return (
prop.type === 'Property' &&
prop.key.type === 'Identifier' &&
prop.key.name === 'type' &&
prop.value.type === 'Identifier' &&
prop.value.name === 'Boolean'
)
}
function getBooleanProps (props) {
return props
.filter(prop => (
prop.value &&
prop.value.properties &&
prop.value.properties.find(isBooleanProp)
))
}
function getDefaultNode (propDef) {
return propDef.value.properties.find(p => {
return (
p.type === 'Property' &&
p.key.type === 'Identifier' &&
p.key.name === 'default'
)
})
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow boolean defaults',
category: undefined,
url: 'https://eslint.vuejs.org/rules/no-boolean-default.html'
},
fixable: 'code',
schema: [
{
enum: ['default-false', 'no-default']
}
]
},
create (context) {
return utils.executeOnVueComponent(context, (obj) => {
const props = utils.getComponentProps(obj)
const booleanProps = getBooleanProps(props)
if (!booleanProps.length) return
const booleanType = context.options[0] || 'no-default'
booleanProps.forEach((propDef) => {
const defaultNode = getDefaultNode(propDef)
switch (booleanType) {
case 'no-default':
if (defaultNode) {
context.report({
node: defaultNode,
message: 'Boolean prop should not set a default (Vue defaults it to false).'
})
}
break
case 'default-false':
if (
defaultNode &&
defaultNode.value.value !== false
) {
context.report({
node: defaultNode,
message: 'Boolean prop should only be defaulted to false.'
})
}
break
}
})
})
}
}