Skip to content

Commit

Permalink
replace new rule with extending genericSpacing
Browse files Browse the repository at this point in the history
  • Loading branch information
hsimah committed Jan 23, 2022
1 parent 52606f1 commit ef00cf8
Show file tree
Hide file tree
Showing 8 changed files with 23,243 additions and 4,539 deletions.
18,714 changes: 18,714 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import spaceAfterTypeColon from './rules/spaceAfterTypeColon';
import spaceBeforeGenericBracket from './rules/spaceBeforeGenericBracket';
import spaceBeforeTypeColon from './rules/spaceBeforeTypeColon';
import spreadExactType from './rules/spreadExactType';
import typeAnnotationSpacing from './rules/typeAnnotationSpacing';
import typeIdMatch from './rules/typeIdMatch';
import typeImportStyle from './rules/typeImportStyle';
import unionIntersectionSpacing from './rules/unionIntersectionSpacing';
Expand Down Expand Up @@ -99,7 +98,6 @@ const rules = {
'space-before-generic-bracket': spaceBeforeGenericBracket,
'space-before-type-colon': spaceBeforeTypeColon,
'spread-exact-type': spreadExactType,
'type-annotation-spacing': typeAnnotationSpacing,
'type-id-match': typeIdMatch,
'type-import-style': typeImportStyle,
'union-intersection-spacing': unionIntersectionSpacing,
Expand Down Expand Up @@ -151,7 +149,6 @@ export default {
'space-before-generic-bracket': 0,
'space-before-type-colon': 0,
'spread-exact-type': 0,
'type-annotation-spacing': 0,
'type-id-match': 0,
'type-import-style': 0,
'union-intersection-spacing': 0,
Expand Down
197 changes: 197 additions & 0 deletions src/rules/genericSpacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,209 @@ const schema = [
},
];

function isNeverOption(context) {
return (context.options[0] || 'never') === 'never';
}

function isWhitespaceCRLF(whitespace) {
return whitespace !== '\n' && whitespace !== '\r';
}

function spacesOutside(node, context) {
const { callee, typeArguments } = node;
if (typeArguments == null) {
return;
}

const sourceCode = context.getSourceCode();
const { name } = callee;
const never = isNeverOption(context);
const parentheses = sourceCode.getTokenAfter(typeArguments);

const spacesBefore = typeArguments.range[0] - callee.range[1];
const spacesAfter = parentheses.range[0] - typeArguments.range[1];

if (never) {
if (spacesBefore) {
const whiteSpaceBefore = sourceCode.text[typeArguments.range[0]];

if (isWhitespaceCRLF(whiteSpaceBefore)) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesBefore(typeArguments, spacesBefore),
message: 'There must be no space before "{{name}}" type annotation',
node,
});
}
}

if (spacesAfter) {
const whiteSpaceAfter = sourceCode.text[typeArguments.range[1] - 1];

if (isWhitespaceCRLF(whiteSpaceAfter)) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesAfter(typeArguments, spacesAfter),
message: 'There must be no space after "{{name}}" type annotation',
node,
});
}
}

return;
}

if (!never) {
if (spacesBefore > 1) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesBefore(typeArguments, spacesBefore - 1),
message: 'There must be one space before "{{name}}" generic type annotation bracket',
node,
});
}

if (spacesBefore === 0) {
context.report({
data: { name },
fix: spacingFixers.addSpaceBefore(typeArguments),
message: 'There must be a space before "{{name}}" generic type annotation bracket',
node,
});
}

if (spacesAfter > 1) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesAfter(typeArguments, spacesAfter),
message: 'There must be one space before "{{name}}" generic type annotation bracket',
node,
});
}

if (spacesAfter === 0) {
context.report({
data: { name },
fix: spacingFixers.addSpaceAfter(typeArguments),
message: 'There must be a space before "{{name}}" generic type annotation bracket',
node,
});
}
}
}

function spacesInside(node, context) {
const { callee, typeArguments } = node;
if (typeArguments == null) {
return;
}

const sourceCode = context.getSourceCode();
const { name } = callee;
const never = isNeverOption(context);
const isNullable = typeArguments.params[0].type === 'NullableTypeAnnotation';
const [
opener,
firstInnerToken,
secondInnerToken,
] = sourceCode.getFirstTokens(typeArguments, 3);
const [
lastInnerToken,
closer,
] = sourceCode.getLastTokens(typeArguments, 2);

const spacesBefore = firstInnerToken.range[0] - opener.range[1];
const spaceBetweenNullToken = secondInnerToken.range[0] - firstInnerToken.range[1];
const spacesAfter = closer.range[0] - lastInnerToken.range[1];

if (never) {
if (spacesBefore) {
const whiteSpaceBefore = sourceCode.text[opener.range[1]];

if (whiteSpaceBefore !== '\n' && whiteSpaceBefore !== '\r') {
context.report({
data: { name },
fix: spacingFixers.stripSpacesAfter(opener, spacesBefore),
message: 'There must be no spaces inside at the start of "{{name}}" type annotation',
node,
});
}
}

if (isNullable && spaceBetweenNullToken) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesAfter(firstInnerToken, spaceBetweenNullToken),
message: 'There must be no spaces inside "{{name}}" type annotation',
node,
});
}

if (spacesAfter) {
const whiteSpaceAfter = sourceCode.text[closer.range[0] - 1];

if (isWhitespaceCRLF(whiteSpaceAfter)) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesAfter(lastInnerToken, spacesAfter),
message: 'There must be no spaces inside at the end of "{{name}}" type annotation',
node,
});
}
}

return;
}

if (!never) {
if (spacesBefore > 1) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesBefore(opener, spacesBefore - 1),
message: 'There must be one space before "{{name}}" generic type annotation bracket',
node,
});
}

if (spacesBefore === 0) {
context.report({
data: { name },
fix: spacingFixers.addSpaceBefore(opener),
message: 'There must be a space before "{{name}}" generic type annotation bracket',
node,
});
}

if (spacesAfter > 1) {
context.report({
data: { name },
fix: spacingFixers.stripSpacesAfter(closer, spacesAfter),
message: 'There must be one space before "{{name}}" generic type annotation bracket',
node,
});
}

if (spacesAfter === 0) {
context.report({
data: { name },
fix: spacingFixers.addSpaceAfter(closer),
message: 'There must be a space before "{{name}}" generic type annotation bracket',
node,
});
}
}
}

const create = (context) => {
const sourceCode = context.getSourceCode();

const never = (context.options[0] || 'never') === 'never';

return {
CallExpression(node) {
spacesOutside(node, context);
spacesInside(node, context);
},
GenericTypeAnnotation(node) {
const types = node.typeParameters;

Expand Down
Loading

0 comments on commit ef00cf8

Please sign in to comment.