diff --git a/packages/eslint-plugin/src/rules/space-infix-ops.ts b/packages/eslint-plugin/src/rules/space-infix-ops.ts index 57c56ad0b476..e81d8ef25bb7 100644 --- a/packages/eslint-plugin/src/rules/space-infix-ops.ts +++ b/packages/eslint-plugin/src/rules/space-infix-ops.ts @@ -34,20 +34,11 @@ export default util.createRule({ const rules = baseRule.create(context); const sourceCode = context.getSourceCode(); - /** - * Check if it has an assignment char and report if it's faulty - * @param node The node to report - */ - function checkForAssignmentSpace(node: TSESTree.TSEnumMember): void { - if (!node.initializer) { - return; - } - - const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!; - const rightNode = sourceCode.getTokenByRangeStart( - node.initializer.range[0], - )!; - + function checkAndReportAssignmentSpace( + node: TSESTree.Node, + leftNode: TSESTree.Token, + rightNode?: TSESTree.Token | null, + ): void { if (!rightNode) { return; } @@ -94,9 +85,60 @@ export default util.createRule({ } } + /** + * Check if it has an assignment char and report if it's faulty + * @param node The node to report + */ + function checkForEnumAssignmentSpace(node: TSESTree.TSEnumMember): void { + if (!node.initializer) { + return; + } + + const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!; + const rightNode = sourceCode.getTokenByRangeStart( + node.initializer.range[0], + )!; + + checkAndReportAssignmentSpace(node, leftNode, rightNode); + } + + /** + * Check if it has an assignment char and report if it's faulty + * @param node The node to report + */ + function checkForClassPropertyAssignmentSpace( + node: TSESTree.ClassProperty, + ): void { + const leftNode = sourceCode.getTokenByRangeStart( + node.typeAnnotation?.range[0] ?? node.range[0], + )!; + const rightNode = node.value + ? sourceCode.getTokenByRangeStart(node.value.range[0]) + : undefined; + + checkAndReportAssignmentSpace(node, leftNode, rightNode); + } + + /** + * Check if it has an assignment char and report if it's faulty + * @param node The node to report + */ + function checkForTypeAliasAssignmentSpace( + node: TSESTree.TSTypeAliasDeclaration, + ): void { + const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!; + const rightNode = sourceCode.getTokenByRangeStart( + node.typeAnnotation.range[0], + ); + + checkAndReportAssignmentSpace(node, leftNode, rightNode); + } + return { ...rules, - TSEnumMember: checkForAssignmentSpace, + TSEnumMember: checkForEnumAssignmentSpace, + ClassProperty: checkForClassPropertyAssignmentSpace, + TSTypeAliasDeclaration: checkForTypeAliasAssignmentSpace, }; }, }); diff --git a/packages/eslint-plugin/tests/rules/space-infix-ops.test.ts b/packages/eslint-plugin/tests/rules/space-infix-ops.test.ts index 0c90443ca4fc..6beb4ae06fac 100644 --- a/packages/eslint-plugin/tests/rules/space-infix-ops.test.ts +++ b/packages/eslint-plugin/tests/rules/space-infix-ops.test.ts @@ -33,6 +33,32 @@ ruleTester.run('space-infix-ops', rule, { } `, }, + { + code: ` + class Test { + public readonly value?: number; + } + `, + }, + { + code: ` + class Test { + public readonly value = 1; + } + `, + }, + { + code: ` + class Test { + private value:number = 1; + } + `, + }, + { + code: ` + type Test = string | boolean; + `, + }, ], invalid: [ { @@ -98,5 +124,73 @@ ruleTester.run('space-infix-ops', rule, { }, ], }, + { + code: ` + class Test { + public readonly value= 2; + } + `, + output: ` + class Test { + public readonly value = 2; + } + `, + errors: [ + { + messageId: 'missingSpace', + column: 32, + line: 3, + }, + ], + }, + { + code: ` + class Test { + public readonly value =2; + } + `, + output: ` + class Test { + public readonly value = 2; + } + `, + errors: [ + { + messageId: 'missingSpace', + column: 33, + line: 3, + }, + ], + }, + { + code: ` + type Test= string | number; + `, + output: ` + type Test = string | number; + `, + errors: [ + { + messageId: 'missingSpace', + column: 18, + line: 2, + }, + ], + }, + { + code: ` + type Test =string | number; + `, + output: ` + type Test = string | number; + `, + errors: [ + { + messageId: 'missingSpace', + column: 19, + line: 2, + }, + ], + }, ], });