-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[New] jsx-boolean-value
: add inverse option for always/never
#1250
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 all commits
Commits
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 |
---|---|---|
|
@@ -8,6 +8,41 @@ | |
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
const exceptionsSchema = { | ||
type: 'array', | ||
items: {type: 'string', minLength: 1}, | ||
uniqueItems: true | ||
}; | ||
|
||
const ALWAYS = 'always'; | ||
const NEVER = 'never'; | ||
|
||
const errorData = new WeakMap(); | ||
function getErrorData(exceptions) { | ||
if (!errorData.has(exceptions)) { | ||
const exceptionProps = Array.from(exceptions, (name) => `\`${name}\``).join(', '); | ||
const exceptionsMessage = exceptions.size > 0 ? ` for the following props: ${exceptionProps}` : ''; | ||
errorData.set(exceptions, {exceptionsMessage: exceptionsMessage}); | ||
} | ||
return errorData.get(exceptions); | ||
} | ||
|
||
function isAlways(configuration, exceptions, propName) { | ||
const isException = exceptions.has(propName); | ||
if (configuration === ALWAYS) { | ||
return !isException; | ||
} | ||
return isException; | ||
} | ||
|
||
function isNever(configuration, exceptions, propName) { | ||
const isException = exceptions.has(propName); | ||
if (configuration === NEVER) { | ||
return !isException; | ||
} | ||
return isException; | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
|
@@ -17,44 +52,73 @@ module.exports = { | |
}, | ||
fixable: 'code', | ||
|
||
schema: [{ | ||
enum: ['always', 'never'] | ||
}] | ||
schema: { | ||
anyOf: [{ | ||
type: 'array', | ||
items: [{enum: [ALWAYS, NEVER]}], | ||
additionalItems: false | ||
}, { | ||
type: 'array', | ||
items: [{ | ||
enum: [ALWAYS] | ||
}, { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
[NEVER]: exceptionsSchema | ||
} | ||
}], | ||
additionalItems: false | ||
}, { | ||
type: 'array', | ||
items: [{ | ||
enum: [NEVER] | ||
}, { | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
[ALWAYS]: exceptionsSchema | ||
} | ||
}], | ||
additionalItems: false | ||
}] | ||
} | ||
}, | ||
|
||
create: function(context) { | ||
var configuration = context.options[0] || 'never'; | ||
create(context) { | ||
const configuration = context.options[0] || NEVER; | ||
const configObject = context.options[1] || {}; | ||
const exceptions = new Set((configuration === ALWAYS ? configObject[NEVER] : configObject[ALWAYS]) || []); | ||
|
||
var NEVER_MESSAGE = 'Value must be omitted for boolean attributes'; | ||
var ALWAYS_MESSAGE = 'Value must be set for boolean attributes'; | ||
const NEVER_MESSAGE = 'Value must be omitted for boolean attributes{{exceptionsMessage}}'; | ||
const ALWAYS_MESSAGE = 'Value must be set for boolean attributes{{exceptionsMessage}}'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be nice to move some of this into a helper function that takes |
||
|
||
return { | ||
JSXAttribute: function(node) { | ||
switch (configuration) { | ||
case 'always': | ||
if (node.value === null) { | ||
context.report({ | ||
node: node, | ||
message: ALWAYS_MESSAGE, | ||
fix: function(fixer) { | ||
return fixer.insertTextAfter(node, '={true}'); | ||
} | ||
}); | ||
JSXAttribute(node) { | ||
const propName = node.name && node.name.name; | ||
const value = node.value; | ||
|
||
if (isAlways(configuration, exceptions, propName) && value === null) { | ||
const data = getErrorData(exceptions); | ||
context.report({ | ||
node: node, | ||
message: ALWAYS_MESSAGE, | ||
data: data, | ||
fix(fixer) { | ||
return fixer.insertTextAfter(node, '={true}'); | ||
} | ||
break; | ||
case 'never': | ||
if (node.value && node.value.type === 'JSXExpressionContainer' && node.value.expression.value === true) { | ||
context.report({ | ||
node: node, | ||
message: NEVER_MESSAGE, | ||
fix: function(fixer) { | ||
return fixer.removeRange([node.name.range[1], node.value.range[1]]); | ||
} | ||
}); | ||
}); | ||
} | ||
if (isNever(configuration, exceptions, propName) && value && value.type === 'JSXExpressionContainer' && value.expression.value === true) { | ||
const data = getErrorData(exceptions); | ||
context.report({ | ||
node: node, | ||
message: NEVER_MESSAGE, | ||
data: data, | ||
fix(fixer) { | ||
return fixer.removeRange([node.name.range[1], value.range[1]]); | ||
} | ||
break; | ||
default: | ||
break; | ||
}); | ||
} | ||
} | ||
}; | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on the fence about whether this should be
never
/always
, or something more general likeexceptions
orexcept
. As it is written here, it is possible to have bothnever
andalways
which would be a little weird. I think it is fine, but I think a unified option might make the documentation a little clearer and the code a small amount simpler. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, "exceptions" means "these things are not checked" - the semantic here is, everything is forced to something: some things are forced to always, and some to never.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see. That makes sense.