Skip to content
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

Update: add enforceForJSX option to no-unused-expressions rule #14012

Merged
merged 11 commits into from Feb 10, 2021
Merged
10 changes: 9 additions & 1 deletion lib/rules/no-unused-expressions.js
Expand Up @@ -50,6 +50,10 @@ module.exports = {
allowTaggedTemplates: {
type: "boolean",
default: false
},
disallowJSX: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see how the name disallowJSX follows from the implementation, but I'm wondering if the double negative might confuse users. For consistency with the other options, I could go for your original option name allowJSX but defaulting to true so it's not a breaking change. I also think checkJSX defaulting to false would be clear. Anyone have any preferences?

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s always super weird when absence and false aren’t the same - iow, i think a boolean option should never default to true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s a great point. I also wasn’t thrilled about another “allow” option having the opposite default, so checkJSX seems like the better trade off now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe enforceForJSX?

type: "boolean",
default: false
}
},
additionalProperties: false
Expand All @@ -65,7 +69,8 @@ module.exports = {
const config = context.options[0] || {},
allowShortCircuit = config.allowShortCircuit || false,
allowTernary = config.allowTernary || false,
allowTaggedTemplates = config.allowTaggedTemplates || false;
allowTaggedTemplates = config.allowTaggedTemplates || false,
disallowJSX = config.disallowJSX || false;

// eslint-disable-next-line jsdoc/require-description
/**
Expand Down Expand Up @@ -140,6 +145,9 @@ module.exports = {
},
FunctionExpression: alwaysTrue,
Identifier: alwaysTrue,
JSXElement() {
return disallowJSX;
},
duncanbeevers marked this conversation as resolved.
Show resolved Hide resolved
Literal: alwaysTrue,
LogicalExpression(node) {
if (allowShortCircuit) {
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/rules/no-unused-expressions.js
Expand Up @@ -82,6 +82,16 @@ ruleTester.run("no-unused-expressions", rule, {
{
code: "obj?.foo(\"bar\")",
parserOptions: { ecmaVersion: 11 }
},

// JSX
{
code: "<div />",
parserOptions: { ecmaFeatures: { jsx: true } }
},
{
code: "var partial = <div />",
parserOptions: { ecmaFeatures: { jsx: true } }
duncanbeevers marked this conversation as resolved.
Show resolved Hide resolved
}
],
invalid: [
Expand Down Expand Up @@ -152,6 +162,14 @@ ruleTester.run("no-unused-expressions", rule, {
code: "obj?.foo().bar",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }]
},

// JSX
{
code: "<div />",
options: [{ disallowJSX: true }],
parserOptions: { ecmaFeatures: { jsx: true } },
errors: [{ messageId: "unusedExpression", type: "ExpressionStatement" }]
}
]
});