Skip to content

Commit

Permalink
feat(eslint-plugin): [space-infix-ops] missing error report for condi…
Browse files Browse the repository at this point in the history
…tional types (#5041)
  • Loading branch information
holazz committed May 26, 2022
1 parent eaa5d7b commit 0bfab6c
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
21 changes: 20 additions & 1 deletion packages/eslint-plugin/src/rules/space-infix-ops.ts
Expand Up @@ -69,7 +69,10 @@ export default util.createRule<Options, MessageIds>({
};

function isSpaceChar(token: TSESTree.Token): boolean {
return token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=';
return (
token.type === AST_TOKEN_TYPES.Punctuator &&
/^[=|?|:]$/.test(token.value)
);
}

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

function checkForTypeConditional(node: TSESTree.TSConditionalType): void {
const extendsTypeNode = sourceCode.getTokenByRangeStart(
node.extendsType.range[0],
)!;
const trueTypeNode = sourceCode.getTokenByRangeStart(
node.trueType.range[0],
)!;
const falseTypeNode = sourceCode.getTokenByRangeStart(
node.falseType.range[0],
);

checkAndReportAssignmentSpace(node, extendsTypeNode, trueTypeNode);
checkAndReportAssignmentSpace(node, trueTypeNode, falseTypeNode);
}

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

0 comments on commit 0bfab6c

Please sign in to comment.