Skip to content

Commit

Permalink
Simplify rules and refactor for more readability
Browse files Browse the repository at this point in the history
  • Loading branch information
doumart committed May 27, 2021
1 parent 2987a5d commit 63f5658
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 45 deletions.
63 changes: 18 additions & 45 deletions packages/eslint-plugin/src/rules/space-infix-ops.ts
@@ -1,5 +1,4 @@
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
Expand All @@ -9,6 +8,8 @@ import * as util from '../util';
export type Options = util.InferOptionsTypeFromRule<typeof baseRule>;
export type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>;

const UNIONS = ['|', '&'];

export default util.createRule<Options, MessageIds>({
name: 'space-infix-ops',
meta: {
Expand Down Expand Up @@ -66,6 +67,10 @@ export default util.createRule<Options, MessageIds>({
});
};

function isSpaceChar(token: TSESTree.Token): boolean {
return token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=';
}

function checkAndReportAssignmentSpace(
node: TSESTree.Node,
leftNode: TSESTree.Token,
Expand All @@ -78,19 +83,17 @@ export default util.createRule<Options, MessageIds>({
const operator = sourceCode.getFirstTokenBetween(
leftNode,
rightNode,
token =>
token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=',
isSpaceChar,
);

const prev = sourceCode.getTokenBefore(operator!);
const next = sourceCode.getTokenAfter(operator!);

if (
operator &&
(!sourceCode.isSpaceBetweenTokens(prev!, operator) ||
!sourceCode.isSpaceBetweenTokens(operator, next!))
!sourceCode.isSpaceBetween!(prev!, operator!) ||
!sourceCode.isSpaceBetween!(operator!, next!)
) {
report(node, operator);
report(node, operator!);
}
}

Expand Down Expand Up @@ -135,21 +138,20 @@ export default util.createRule<Options, MessageIds>({
function checkForTypeAnnotationSpace(
typeAnnotation: TSESTree.TSIntersectionType | TSESTree.TSUnionType,
): void {
const UNIONS = ['|', '&'];
const types = typeAnnotation.types;

types.forEach(type => {
const operator = sourceCode.getTokenBefore(type);

if (!!operator && UNIONS.includes(operator.value)) {
if (operator != null && UNIONS.includes(operator.value)) {
const prev = sourceCode.getTokenBefore(operator);
const next = sourceCode.getTokenAfter(operator);

if (
!sourceCode.isSpaceBetweenTokens(prev!, operator) ||
!sourceCode.isSpaceBetweenTokens(operator, next!)
!sourceCode.isSpaceBetween!(prev!, operator) ||
!sourceCode.isSpaceBetween!(operator, next!)
) {
report(operator, operator);
report(typeAnnotation, operator);
}
}
});
Expand All @@ -159,7 +161,7 @@ export default util.createRule<Options, MessageIds>({
* Check if it has an assignment char and report if it's faulty
* @param node The node to report
*/
function checkForTypeAliasAssignmentAndTypeAnnotationSpace(
function checkForTypeAliasAssignment(
node: TSESTree.TSTypeAliasDeclaration,
): void {
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
Expand All @@ -168,44 +170,15 @@ export default util.createRule<Options, MessageIds>({
);

checkAndReportAssignmentSpace(node, leftNode, rightNode);
if (
node.typeAnnotation.type === AST_NODE_TYPES.TSUnionType ||
node.typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType
) {
checkForTypeAnnotationSpace(node.typeAnnotation);
}
}

/**
* Check if it has an assignment char and report if it's faulty
* @param node The node to report
*/
function checkForIntercaceDeclarationSpace(
node: TSESTree.TSInterfaceDeclaration,
): void {
const properties = node.body.body;

properties.forEach((prop: TSESTree.TypeElement) => {
if (prop.type === AST_NODE_TYPES.TSPropertySignature) {
const propTypeAnnotation = prop.typeAnnotation!;

const typeAnnotation = propTypeAnnotation.typeAnnotation;
if (
typeAnnotation.type === AST_NODE_TYPES.TSUnionType ||
typeAnnotation.type === AST_NODE_TYPES.TSIntersectionType
) {
checkForTypeAnnotationSpace(typeAnnotation);
}
}
});
}

return {
...rules,
TSEnumMember: checkForEnumAssignmentSpace,
ClassProperty: checkForClassPropertyAssignmentSpace,
TSTypeAliasDeclaration: checkForTypeAliasAssignmentAndTypeAnnotationSpace,
TSInterfaceDeclaration: checkForIntercaceDeclarationSpace,
TSTypeAliasDeclaration: checkForTypeAliasAssignment,
TSUnionType: checkForTypeAnnotationSpace,
TSIntersectionType: checkForTypeAnnotationSpace,
};
},
});
244 changes: 244 additions & 0 deletions packages/eslint-plugin/tests/rules/space-infix-ops.test.ts
Expand Up @@ -136,6 +136,28 @@ ruleTester.run('space-infix-ops', rule, {
}
`,
},
{
code: `
const x: string & number;
`,
},
{
code: `
class Test {
value: string & number;
}
`,
},
{
code: `
function foo<T extends string & number>() {}
`,
},
{
code: `
function bar(): string & number {}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -489,5 +511,227 @@ ruleTester.run('space-infix-ops', rule, {
},
],
},
{
code: `
const x: string &number;
`,
output: `
const x: string & number;
`,
errors: [
{
messageId: 'missingSpace',
column: 25,
line: 2,
},
],
},
{
code: `
const x: string& number;
`,
output: `
const x: string & number;
`,
errors: [
{
messageId: 'missingSpace',
column: 24,
line: 2,
},
],
},
{
code: `
const x: string| number;
`,
output: `
const x: string | number;
`,
errors: [
{
messageId: 'missingSpace',
column: 24,
line: 2,
},
],
},
{
code: `
class Test {
value: string |number;
}
`,
output: `
class Test {
value: string | number;
}
`,
errors: [
{
messageId: 'missingSpace',
column: 25,
line: 3,
},
],
},
{
code: `
class Test {
value: string& number;
}
`,
output: `
class Test {
value: string & number;
}
`,
errors: [
{
messageId: 'missingSpace',
column: 24,
line: 3,
},
],
},
{
code: `
class Test {
value: string| number;
}
`,
output: `
class Test {
value: string | number;
}
`,
errors: [
{
messageId: 'missingSpace',
column: 24,
line: 3,
},
],
},
{
code: `
function foo<T extends string &number>() {}
`,
output: `
function foo<T extends string & number>() {}
`,
errors: [
{
messageId: 'missingSpace',
column: 39,
line: 2,
},
],
},
{
code: `
function foo<T extends string& number>() {}
`,
output: `
function foo<T extends string & number>() {}
`,
errors: [
{
messageId: 'missingSpace',
column: 38,
line: 2,
},
],
},
{
code: `
function foo<T extends string |number>() {}
`,
output: `
function foo<T extends string | number>() {}
`,
errors: [
{
messageId: 'missingSpace',
column: 39,
line: 2,
},
],
},
{
code: `
function foo<T extends string| number>() {}
`,
output: `
function foo<T extends string | number>() {}
`,
errors: [
{
messageId: 'missingSpace',
column: 38,
line: 2,
},
],
},
{
code: `
function bar(): string &number {}
`,
output: `
function bar(): string & number {}
`,
errors: [
{
messageId: 'missingSpace',
column: 32,
line: 2,
},
],
},
{
code: `
function bar(): string& number {}
`,
output: `
function bar(): string & number {}
`,
errors: [
{
messageId: 'missingSpace',
column: 31,
line: 2,
},
],
},
{
code: `
function bar(): string |number {}
`,
output: `
function bar(): string | number {}
`,
errors: [
{
messageId: 'missingSpace',
column: 32,
line: 2,
},
],
},
{
code: `
function bar(): string| number {}
`,
output: `
function bar(): string | number {}
`,
errors: [
{
messageId: 'missingSpace',
column: 31,
line: 2,
},
],
},
],
});

0 comments on commit 63f5658

Please sign in to comment.