-
-
Notifications
You must be signed in to change notification settings - Fork 690
⭐️New: Add rule no-boolean-default
#612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fca66bb
no-boolean-default
42c252c
Rule files
b401f7d
PR changes
d2acc73
Added docs
7a1d6f4
Added more tests
872820d
Merge branch 'master' into pr/612
michalsnik 70bfa06
Update implementation
michalsnik 36c18b6
Update config and docs
michalsnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,38 @@ | ||
# Prevents boolean defaults from being set (vue/no-boolean-default) | ||
|
||
- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/strongly-recommended"` and `"plugin:vue/recommended"`. | ||
|
||
Please describe the origin of the rule here. | ||
|
||
|
||
## Rule Details | ||
|
||
This rule aims to... | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
|
||
// fill me in | ||
|
||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
|
||
// fill me in | ||
|
||
``` | ||
|
||
### Options | ||
|
||
If there are any options, describe them here. Otherwise, delete this section. | ||
|
||
## When Not To Use It | ||
|
||
Give a short description of when it would be appropriate to turn off this rule. | ||
|
||
## Further Reading | ||
|
||
If there are other links that describe the issue this rule addresses, please include them here in a bulleted list. |
This file contains hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
/** | ||
* @fileoverview Prevents boolean defaults from being set | ||
* @author Hiroki Osame | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow boolean defaults', | ||
category: 'essential', | ||
michalsnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
recommended: false, | ||
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/no-boolean-default.md' | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
enum: ['default-false', 'no-default', 'constructor'] | ||
} | ||
] | ||
}, | ||
|
||
create (context) { | ||
function getPropsDef (vueComp) { | ||
return vueComp.properties.find(p => | ||
p.type === 'Property' && | ||
p.key.type === 'Identifier' && | ||
p.key.name === 'props' && | ||
p.value.type === 'ObjectExpression' | ||
) | ||
} | ||
|
||
function getBooleanProps (propsDef) { | ||
return propsDef.value.properties | ||
armano2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.filter(p => { | ||
michalsnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return (p.value.type === 'ObjectExpression') && p.value.properties.find(p => ( | ||
p.key.type === 'Identifier' && | ||
michalsnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
p.key.name === 'type' && | ||
p.value.type === 'Identifier' && | ||
p.value.name === 'Boolean' | ||
)) | ||
}) | ||
} | ||
|
||
function getDefaultNode (propDef) { | ||
return propDef.value.properties.find(p => (p.key.type === 'Identifier' && p.key.name === 'default')) | ||
} | ||
|
||
return utils.executeOnVueComponent(context, (obj) => { | ||
const propsDef = getPropsDef(obj) | ||
armano2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!propsDef) { return } | ||
|
||
const booleanProps = getBooleanProps(propsDef) | ||
|
||
if (!booleanProps.length) { return } | ||
|
||
const booleanType = context.options[0] || 'constructor' | ||
|
||
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 | ||
|
||
default: | ||
const nonConstProperties = propDef.value.properties.filter(p => p.key.name !== 'type') | ||
|
||
let fix | ||
if ( | ||
nonConstProperties.length === 0 || | ||
(nonConstProperties.length === 1 && (nonConstProperties[0].key.name === 'default' && nonConstProperties[0].value.value === false)) | ||
) { | ||
fix = fixer => fixer.replaceText(propDef.value, 'Boolean') | ||
} | ||
|
||
context.report({ | ||
node: propDef, | ||
message: 'Boolean prop should use a constructor.', | ||
fix | ||
}) | ||
} | ||
}) | ||
}) | ||
} | ||
} |
This file contains hidden or 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,113 @@ | ||
/** | ||
* @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: 2015, | ||
sourceType: 'module' | ||
} | ||
}) | ||
ruleTester.run('no-boolean-default', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
enabled: Boolean | ||
} | ||
} | ||
` | ||
} | ||
michalsnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
|
||
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 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
enabled: { | ||
type: Boolean, | ||
} | ||
} | ||
} | ||
`, | ||
options: ['constructor'], | ||
errors: [{ | ||
message: 'Boolean prop should use a constructor.', | ||
line: 4 | ||
}] | ||
} | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.