From 1f7959fad80c0a2ce5cd4ec743881d563164e5cd Mon Sep 17 00:00:00 2001 From: FDIM Date: Sat, 27 Mar 2021 17:40:41 +0100 Subject: [PATCH 1/2] fix(eslint-plugin): [space-infix-ops] support for class properties --- .../src/rules/space-infix-ops.ts | 56 +++++++++++++----- .../tests/rules/space-infix-ops.test.ts | 59 +++++++++++++++++++ 2 files changed, 100 insertions(+), 15 deletions(-) diff --git a/packages/eslint-plugin/src/rules/space-infix-ops.ts b/packages/eslint-plugin/src/rules/space-infix-ops.ts index 57c56ad0b476..8adddf8af5f7 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,44 @@ 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); + } + return { ...rules, - TSEnumMember: checkForAssignmentSpace, + TSEnumMember: checkForEnumAssignmentSpace, + ClassProperty: checkForClassPropertyAssignmentSpace, }; }, }); 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..b32ad15156a8 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,27 @@ 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; + } + `, + }, ], invalid: [ { @@ -98,5 +119,43 @@ 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, + }, + ], + }, ], }); From 394922bce9b1288663ee5187de3af3108042fadf Mon Sep 17 00:00:00 2001 From: FDIM Date: Sun, 28 Mar 2021 18:07:05 +0200 Subject: [PATCH 2/2] fix(eslint-plugin): [space-infix-ops] support for type aliases --- .../src/rules/space-infix-ops.ts | 16 +++++++++ .../tests/rules/space-infix-ops.test.ts | 35 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/packages/eslint-plugin/src/rules/space-infix-ops.ts b/packages/eslint-plugin/src/rules/space-infix-ops.ts index 8adddf8af5f7..e81d8ef25bb7 100644 --- a/packages/eslint-plugin/src/rules/space-infix-ops.ts +++ b/packages/eslint-plugin/src/rules/space-infix-ops.ts @@ -119,10 +119,26 @@ export default util.createRule({ 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: 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 b32ad15156a8..6beb4ae06fac 100644 --- a/packages/eslint-plugin/tests/rules/space-infix-ops.test.ts +++ b/packages/eslint-plugin/tests/rules/space-infix-ops.test.ts @@ -54,6 +54,11 @@ ruleTester.run('space-infix-ops', rule, { } `, }, + { + code: ` + type Test = string | boolean; + `, + }, ], invalid: [ { @@ -157,5 +162,35 @@ ruleTester.run('space-infix-ops', rule, { }, ], }, + { + 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, + }, + ], + }, ], });