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

feat(eslint-plugin): new option allowExplicitAny strict-boolean-exp #1900

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -59,6 +59,7 @@ Options may be provided as an object with:
- `allowNullable` to allow `undefined` and `null` in addition to `boolean` as a type of all boolean expressions. (`false` by default).
- `allowSafe` to allow non-falsy types (i.e. non string / number / boolean) in addition to `boolean` as a type of all boolean expressions. (`false` by default).
- `ignoreRhs` to skip the check on the right hand side of expressions like `a && b` or `a || b` - allows these operators to be used for their short-circuiting behavior. (`false` by default).
- `allowExplicitAny` to allow explicit `any` typed variables to be lint free.

## Related To

Expand Down
11 changes: 10 additions & 1 deletion packages/eslint-plugin/src/rules/strict-boolean-expressions.ts
Expand Up @@ -11,6 +11,7 @@ type Options = [
ignoreRhs?: boolean;
allowNullable?: boolean;
allowSafe?: boolean;
allowExplicitAny?: boolean;
},
];

Expand Down Expand Up @@ -49,6 +50,9 @@ export default util.createRule<Options, MessageId>({
allowSafe: {
type: 'boolean',
},
allowExplicitAny: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand Down Expand Up @@ -91,12 +95,13 @@ export default util.createRule<Options, MessageId>({
ignoreRhs: false,
allowNullable: false,
allowSafe: false,
allowExplicitAny: false,
},
],
create(context, [options]) {
const service = util.getParserServices(context);
const checker = service.program.getTypeChecker();

const { allowExplicitAny } = options;
const checkedNodes = new Set<TSESTree.Node>();

return {
Expand Down Expand Up @@ -172,9 +177,13 @@ export default util.createRule<Options, MessageId>({

const tsNode = service.esTreeNodeToTSNodeMap.get(node);
const type = util.getConstrainedTypeAtLocation(checker, tsNode);

let messageId: MessageId | undefined;

const types = inspectVariantTypes(tsutils.unionTypeParts(type));
if (allowExplicitAny && types.has('any')) {
return false;
}

const is = (...wantedTypes: readonly VariantType[]): boolean =>
types.size === wantedTypes.length &&
Expand Down
Expand Up @@ -222,6 +222,24 @@ ruleTester.run('strict-boolean-expressions', rule, {
function f5(x: never | boolean) { if (!x) { } }
`,
}),
{
code: `
const x: any;
if (x) {
}
`,
options: [{ allowExplicitAny: true }],
},
{
code: `
let bool1: any;
let bool2 = true;
while (bool1 && bool2) {
return;
}
`,
options: [{ allowExplicitAny: true }],
},
],

invalid: [
Expand Down Expand Up @@ -1235,5 +1253,55 @@ ruleTester.run('strict-boolean-expressions', rule, {
const f3 = (x?: { x: any } | null) => (x ? 1 : 0);
`,
}),
{
code: `
let bool1: any;
let bool2: boolean = true;
let bool3;
while (bool3 && bool2) {
return;
}
`,
options: [{ allowExplicitAny: true }],
errors: [
{
line: 5,
column: 16,
messageId: 'conditionErrorNullish',
},
],
},
{
code: `
const x: any;
if (x) {
}
`,
options: [{ allowExplicitAny: false }],
errors: [
{
line: 3,
column: 5,
messageId: 'conditionErrorAny',
},
],
},
{
code: `
let bool1: any;
let bool2 = true;
while (bool1 && bool2) {
return;
}
`,
options: [{ allowExplicitAny: false }],
errors: [
{
line: 4,
column: 16,
messageId: 'conditionErrorAny',
},
],
},
],
});