Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⭐️New: Add rule
no-boolean-default
(#612)
* no-boolean-default * Rule files * PR changes * Added docs * Added more tests * Update implementation * Update config and docs
- Loading branch information
1 parent
ae03c28
commit 617139e
Showing
5 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-boolean-default | ||
description: disallow boolean defaults | ||
--- | ||
# vue/no-boolean-default | ||
> disallow boolean defaults | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
The rule prevents Boolean props from having a default value. | ||
|
||
|
||
## :book: Rule Details | ||
The rule is to enforce the HTML standard of always defaulting boolean attributes to false. | ||
|
||
<eslint-code-block fix :rules="{'vue/no-boolean-default': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
export default { | ||
props: { | ||
foo: { | ||
type: Boolean, | ||
default: true | ||
}, | ||
bar: { | ||
type: Boolean | ||
} | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
- `'no-default'` (default) allows a prop definition object, but enforces that the `default` property not be defined. | ||
- `'default-false'` enforces that the default can be set but must be set to `false`. | ||
|
||
```json | ||
"vue/no-boolean-default": ["error", "no-default|default-false"] | ||
``` | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-boolean-default.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-boolean-default.js) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @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.value.value !== false) { | ||
context.report({ | ||
node: defaultNode, | ||
message: 'Boolean prop should be defaulted to false.' | ||
}) | ||
} | ||
break | ||
} | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/** | ||
* @fileoverview Prevents boolean defaults from being set | ||
* @author Hiroki Osame | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/no-boolean-default') | ||
|
||
var RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
}) | ||
ruleTester.run('no-boolean-default', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
enabled: Boolean | ||
} | ||
} | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
const props = {}; | ||
export default { | ||
props: { | ||
...props, | ||
enabled: Boolean | ||
} | ||
} | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
const data = {}; | ||
export default { | ||
props: { | ||
enabled: { | ||
type: Boolean, | ||
...data | ||
} | ||
} | ||
} | ||
`, | ||
options: ['no-default'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
const data = {}; | ||
export default { | ||
props: { | ||
enabled: { | ||
type: Boolean, | ||
default: false, | ||
...data | ||
} | ||
} | ||
} | ||
`, | ||
options: ['default-false'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
const data = {}; | ||
export default { | ||
props: { | ||
enabled: data | ||
} | ||
} | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
const data = {}; | ||
export default { | ||
props: { | ||
enabled: { | ||
...data | ||
} | ||
} | ||
} | ||
` | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
enabled: { | ||
type: Boolean, | ||
default: true, | ||
} | ||
} | ||
} | ||
`, | ||
options: ['default-false'], | ||
errors: [{ | ||
message: 'Boolean prop should be defaulted to false.', | ||
line: 6 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
enabled: { | ||
type: Boolean, | ||
default: null, | ||
} | ||
} | ||
} | ||
`, | ||
options: ['default-false'], | ||
errors: [{ | ||
message: 'Boolean prop should be defaulted to false.', | ||
line: 6 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
enabled: { | ||
type: Boolean, | ||
default: false, | ||
} | ||
} | ||
} | ||
`, | ||
options: ['no-default'], | ||
errors: [{ | ||
message: 'Boolean prop should not set a default (Vue defaults it to false).', | ||
line: 6 | ||
}] | ||
} | ||
] | ||
}) |