Skip to content

Commit

Permalink
refactor(eslint-plugin): avoid looking for nodes that do not match th…
Browse files Browse the repository at this point in the history
…e provided options
  • Loading branch information
rafaelss95 committed Sep 23, 2021
1 parent 9b13a86 commit b84b05f
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 221 deletions.
65 changes: 32 additions & 33 deletions packages/eslint-plugin/src/rules/class-literal-property-style.ts
Expand Up @@ -56,8 +56,8 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: ['fields'],
create(context, [style]) {
if (style === 'fields') {
return {
return {
...(style === 'fields' && {
MethodDefinition(node: TSESTree.MethodDefinition): void {
if (
node.kind !== 'get' ||
Expand Down Expand Up @@ -96,38 +96,37 @@ export default util.createRule<Options, MessageIds>({
},
});
},
};
}
}),
...(style === 'getters' && {
ClassProperty(node: TSESTree.ClassProperty): void {
if (!node.readonly || node.declare) {
return;
}

return {
ClassProperty(node: TSESTree.ClassProperty): void {
if (!node.readonly || node.declare) {
return;
}

const { value } = node;

if (!value || !isSupportedLiteral(value)) {
return;
}

context.report({
node: node.key,
messageId: 'preferGetterStyle',
fix(fixer) {
const sourceCode = context.getSourceCode();
const name = sourceCode.getText(node.key);

let text = '';

text += printNodeModifiers(node, 'get');
text += node.computed ? `[${name}]` : name;
text += `() { return ${sourceCode.getText(value)}; }`;

return fixer.replaceText(node, text);
},
});
},
const { value } = node;

if (!value || !isSupportedLiteral(value)) {
return;
}

context.report({
node: node.key,
messageId: 'preferGetterStyle',
fix(fixer) {
const sourceCode = context.getSourceCode();
const name = sourceCode.getText(node.key);

let text = '';

text += printNodeModifiers(node, 'get');
text += node.computed ? `[${name}]` : name;
text += `() { return ${sourceCode.getText(value)}; }`;

return fixer.replaceText(node, text);
},
});
},
}),
};
},
});
20 changes: 10 additions & 10 deletions packages/eslint-plugin/src/rules/consistent-type-definitions.ts
Expand Up @@ -48,10 +48,10 @@ export default util.createRule({
}

return {
"TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"(
node: TSESTree.TSTypeAliasDeclaration,
): void {
if (option === 'interface') {
...(option === 'interface' && {
"TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"(
node: TSESTree.TSTypeAliasDeclaration,
): void {
context.report({
node: node.id,
messageId: 'interfaceOverType',
Expand Down Expand Up @@ -82,10 +82,10 @@ export default util.createRule({
return fixes;
},
});
}
},
TSInterfaceDeclaration(node): void {
if (option === 'type') {
},
}),
...(option === 'type' && {
TSInterfaceDeclaration(node): void {
context.report({
node: node.id,
messageId: 'typeOverInterface',
Expand Down Expand Up @@ -138,8 +138,8 @@ export default util.createRule({
return fixes;
},
});
}
},
},
}),
};
},
});
Expand Up @@ -130,7 +130,6 @@ export default util.createRule<Options, MessageIds>({
TSExportAssignment(node): void {
checkNode(node.expression);
},

'ArrowFunctionExpression, FunctionDeclaration, FunctionExpression'(
node: FunctionNode,
): void {
Expand Down
182 changes: 89 additions & 93 deletions packages/eslint-plugin/src/rules/method-signature-style.ts
Expand Up @@ -117,29 +117,72 @@ export default util.createRule<Options, MessageIds>({
}

return {
TSMethodSignature(methodNode): void {
if (mode === 'method') {
return;
}

const parent = methodNode.parent;
const members =
parent?.type === AST_NODE_TYPES.TSInterfaceBody
? parent.body
: parent?.type === AST_NODE_TYPES.TSTypeLiteral
? parent.members
: [];

const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] =
members.filter(
(element): element is TSESTree.TSMethodSignature =>
element.type === AST_NODE_TYPES.TSMethodSignature &&
element !== methodNode &&
getMethodKey(element) === getMethodKey(methodNode),
);
const isParentModule = isNodeParentModuleDeclaration(methodNode);

if (duplicatedKeyMethodNodes.length > 0) {
...(mode === 'property' && {
TSMethodSignature(methodNode): void {
const parent = methodNode.parent;
const members =
parent?.type === AST_NODE_TYPES.TSInterfaceBody
? parent.body
: parent?.type === AST_NODE_TYPES.TSTypeLiteral
? parent.members
: [];

const duplicatedKeyMethodNodes: TSESTree.TSMethodSignature[] =
members.filter(
(element): element is TSESTree.TSMethodSignature =>
element.type === AST_NODE_TYPES.TSMethodSignature &&
element !== methodNode &&
getMethodKey(element) === getMethodKey(methodNode),
);
const isParentModule = isNodeParentModuleDeclaration(methodNode);

if (duplicatedKeyMethodNodes.length > 0) {
if (isParentModule) {
context.report({
node: methodNode,
messageId: 'errorMethod',
});
} else {
context.report({
node: methodNode,
messageId: 'errorMethod',
*fix(fixer) {
const methodNodes = [
methodNode,
...duplicatedKeyMethodNodes,
].sort((a, b) => (a.range[0] < b.range[0] ? -1 : 1));
const typeString = methodNodes
.map(node => {
const params = getMethodParams(node);
const returnType = getMethodReturnType(node);
return `(${params} => ${returnType})`;
})
.join(' & ');
const key = getMethodKey(methodNode);
const delimiter = getDelimiter(methodNode);
yield fixer.replaceText(
methodNode,
`${key}: ${typeString}${delimiter}`,
);
for (const node of duplicatedKeyMethodNodes) {
const lastToken = sourceCode.getLastToken(node);
if (lastToken) {
const nextToken = sourceCode.getTokenAfter(lastToken);
if (nextToken) {
yield fixer.remove(node);
yield fixer.replaceTextRange(
[lastToken.range[1], nextToken.range[0]],
'',
);
}
}
}
},
});
}
return;
}

if (isParentModule) {
context.report({
node: methodNode,
Expand All @@ -149,90 +192,43 @@ export default util.createRule<Options, MessageIds>({
context.report({
node: methodNode,
messageId: 'errorMethod',
*fix(fixer) {
const methodNodes = [
methodNode,
...duplicatedKeyMethodNodes,
].sort((a, b) => (a.range[0] < b.range[0] ? -1 : 1));
const typeString = methodNodes
.map(node => {
const params = getMethodParams(node);
const returnType = getMethodReturnType(node);
return `(${params} => ${returnType})`;
})
.join(' & ');
fix: fixer => {
const key = getMethodKey(methodNode);
const params = getMethodParams(methodNode);
const returnType = getMethodReturnType(methodNode);
const delimiter = getDelimiter(methodNode);
yield fixer.replaceText(
return fixer.replaceText(
methodNode,
`${key}: ${typeString}${delimiter}`,
`${key}: ${params} => ${returnType}${delimiter}`,
);
for (const node of duplicatedKeyMethodNodes) {
const lastToken = sourceCode.getLastToken(node);
if (lastToken) {
const nextToken = sourceCode.getTokenAfter(lastToken);
if (nextToken) {
yield fixer.remove(node);
yield fixer.replaceTextRange(
[lastToken.range[1], nextToken.range[0]],
'',
);
}
}
}
},
});
}
return;
}
},
}),
...(mode === 'method' && {
TSPropertySignature(propertyNode): void {
const typeNode = propertyNode.typeAnnotation?.typeAnnotation;
if (typeNode?.type !== AST_NODE_TYPES.TSFunctionType) {
return;
}

if (isParentModule) {
context.report({
node: methodNode,
messageId: 'errorMethod',
});
} else {
context.report({
node: methodNode,
messageId: 'errorMethod',
node: propertyNode,
messageId: 'errorProperty',
fix: fixer => {
const key = getMethodKey(methodNode);
const params = getMethodParams(methodNode);
const returnType = getMethodReturnType(methodNode);
const delimiter = getDelimiter(methodNode);
const key = getMethodKey(propertyNode);
const params = getMethodParams(typeNode);
const returnType = getMethodReturnType(typeNode);
const delimiter = getDelimiter(propertyNode);
return fixer.replaceText(
methodNode,
`${key}: ${params} => ${returnType}${delimiter}`,
propertyNode,
`${key}${params}: ${returnType}${delimiter}`,
);
},
});
}
},
TSPropertySignature(propertyNode): void {
const typeNode = propertyNode.typeAnnotation?.typeAnnotation;
if (typeNode?.type !== AST_NODE_TYPES.TSFunctionType) {
return;
}

if (mode === 'property') {
return;
}

context.report({
node: propertyNode,
messageId: 'errorProperty',
fix: fixer => {
const key = getMethodKey(propertyNode);
const params = getMethodParams(typeNode);
const returnType = getMethodReturnType(typeNode);
const delimiter = getDelimiter(propertyNode);
return fixer.replaceText(
propertyNode,
`${key}${params}: ${returnType}${delimiter}`,
);
},
});
},
},
}),
};
},
});
8 changes: 4 additions & 4 deletions packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Expand Up @@ -133,11 +133,11 @@ export default util.createRule<Options, MessageIds>({

return {
"BinaryExpression[operator='+']": checkPlusOperands,
"AssignmentExpression[operator='+=']"(node): void {
if (checkCompoundAssignments) {
...(checkCompoundAssignments && {
"AssignmentExpression[operator='+=']"(node): void {
checkPlusOperands(node);
}
},
},
}),
};
},
});
Expand Up @@ -240,16 +240,16 @@ export default util.createRule<Options, MessageIds>({
}

return {
TSIntersectionType(node): void {
if (checkIntersections === true) {
...(checkIntersections && {
TSIntersectionType(node): void {
checkSorting(node);
}
},
TSUnionType(node): void {
if (checkUnions === true) {
},
}),
...(checkUnions && {
TSUnionType(node): void {
checkSorting(node);
}
},
},
}),
};
},
});

0 comments on commit b84b05f

Please sign in to comment.