Skip to content

Commit

Permalink
feat(space-infix-ops): Add support for Union and intersection of type…
Browse files Browse the repository at this point in the history
… declarations
  • Loading branch information
doumart committed May 8, 2021
1 parent ddfab95 commit ff7c667
Show file tree
Hide file tree
Showing 3 changed files with 450 additions and 74 deletions.
118 changes: 90 additions & 28 deletions packages/eslint-plugin/src/rules/space-infix-ops.ts
Expand Up @@ -34,6 +34,34 @@ export default util.createRule<Options, MessageIds>({
const rules = baseRule.create(context);
const sourceCode = context.getSourceCode();

const report = (node: TSESTree.Node | TSESTree.Token, operator: TSESTree.Token) => {
context.report({
node: node,
loc: operator.loc,
messageId: 'missingSpace',
data: {
operator: operator.value,
},
fix(fixer) {
const previousToken = sourceCode.getTokenBefore(operator);
const afterToken = sourceCode.getTokenAfter(operator);
let fixString = '';

if (operator.range[0] - previousToken!.range[1] === 0) {
fixString = ' ';
}

fixString += operator.value;

if (afterToken!.range[0] - operator.range[1] === 0) {
fixString += ' ';
}

return fixer.replaceText(operator, fixString);
},
});
}

function checkAndReportAssignmentSpace(
node: TSESTree.Node,
leftNode: TSESTree.Token,
Expand All @@ -47,8 +75,9 @@ export default util.createRule<Options, MessageIds>({
leftNode,
rightNode,
token =>
token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=',
token.type === AST_TOKEN_TYPES.Punctuator && token.value==="=",
);

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

Expand All @@ -57,31 +86,7 @@ export default util.createRule<Options, MessageIds>({
(!sourceCode.isSpaceBetweenTokens(prev!, operator) ||
!sourceCode.isSpaceBetweenTokens(operator, next!))
) {
context.report({
node: node,
loc: operator.loc,
messageId: 'missingSpace',
data: {
operator: operator.value,
},
fix(fixer) {
const previousToken = sourceCode.getTokenBefore(operator);
const afterToken = sourceCode.getTokenAfter(operator);
let fixString = '';

if (operator.range[0] - previousToken!.range[1] === 0) {
fixString = ' ';
}

fixString += operator.value;

if (afterToken!.range[0] - operator.range[1] === 0) {
fixString += ' ';
}

return fixer.replaceText(operator, fixString);
},
});
report(node, operator)
}
}

Expand Down Expand Up @@ -119,11 +124,40 @@ export default util.createRule<Options, MessageIds>({
checkAndReportAssignmentSpace(node, leftNode, rightNode);
}


/**
* Check if it is missing spaces between type annotaions chaining
* @param typeAnnotation TypeAnnotations list
*/
function checkForTypeAnnotationSpace(typeAnnotation: TSESTree.TSIntersectionType | TSESTree.TSUnionType) {
const UNIONS = [
"|",
"&",
]
const types = typeAnnotation.types;

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

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

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

/**
* Check if it has an assignment char and report if it's faulty
* @param node The node to report
*/
function checkForTypeAliasAssignmentSpace(
function checkForTypeAliasAssignmentAndTypeAnnotationSpace(
node: TSESTree.TSTypeAliasDeclaration,
): void {
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
Expand All @@ -132,13 +166,41 @@ export default util.createRule<Options, MessageIds>({
);

checkAndReportAssignmentSpace(node, leftNode, rightNode);
if (node.typeAnnotation.type==="TSUnionType" || node.typeAnnotation.type==="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 === "TSPropertySignature"){
const propTypeAnnotation = prop.typeAnnotation!;

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

return {
...rules,
TSEnumMember: checkForEnumAssignmentSpace,
ClassProperty: checkForClassPropertyAssignmentSpace,
TSTypeAliasDeclaration: checkForTypeAliasAssignmentSpace,
TSTypeAliasDeclaration: checkForTypeAliasAssignmentAndTypeAnnotationSpace,
TSInterfaceDeclaration: checkForIntercaceDeclarationSpace,
};
},
});

0 comments on commit ff7c667

Please sign in to comment.