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): [space-infix-ops] missing error report for conditional types #5041

Merged
merged 4 commits into from May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 35 additions & 1 deletion packages/eslint-plugin/src/rules/space-infix-ops.ts
@@ -1,4 +1,10 @@
import { AST_TOKEN_TYPES, TSESTree } from '@typescript-eslint/utils';
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment */

import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
TSESTree,
} from '@typescript-eslint/utils';
import { getESLintCoreRule } from '../util/getESLintCoreRule';
import * as util from '../util';

Expand Down Expand Up @@ -180,13 +186,41 @@ export default util.createRule<Options, MessageIds>({
checkAndReportAssignmentSpace(node, leftNode, rightNode);
}

function checkForTypeConditional(node: TSESTree.TSConditionalType): void {
// transform it to a ConditionalExpression
return rules.ConditionalExpression({
type: AST_NODE_TYPES.ConditionalExpression,
test: {
type: AST_NODE_TYPES.BinaryExpression,
operator: 'extends',
left: node.checkType as any,
right: node.extendsType as any,

// location data
range: [node.checkType.range[0], node.extendsType.range[1]],
loc: {
start: node.checkType.loc.start,
end: node.extendsType.loc.end,
},
},
consequent: node.trueType as any,
alternate: node.falseType as any,

// location data
parent: node.parent,
range: node.range,
loc: node.loc,
});
}

return {
...rules,
TSEnumMember: checkForEnumAssignmentSpace,
PropertyDefinition: checkForPropertyDefinitionAssignmentSpace,
TSTypeAliasDeclaration: checkForTypeAliasAssignment,
TSUnionType: checkForTypeAnnotationSpace,
TSIntersectionType: checkForTypeAnnotationSpace,
TSConditionalType: checkForTypeConditional,
};
},
});
20 changes: 20 additions & 0 deletions packages/eslint-plugin/tests/rules/space-infix-ops.test.ts
Expand Up @@ -977,6 +977,26 @@ ruleTester.run('space-infix-ops', rule, {
},
],
},
{
code: `
type Test<T> = T extends boolean?true:false
holazz marked this conversation as resolved.
Show resolved Hide resolved
`,
output: `
type Test<T> = T extends boolean ? true : false
`,
errors: [
{
messageId: 'missingSpace',
column: 41,
line: 2,
},
{
messageId: 'missingSpace',
column: 46,
line: 2,
},
],
},
{
code: `
interface Test {
Expand Down