Skip to content

Commit

Permalink
refactor(utils): change return type of fixer utilities (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 authored Jun 11, 2021
1 parent a770d3a commit 0fd16e3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
4 changes: 2 additions & 2 deletions packages/eslint-plugin-template/src/rules/eqeqeq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ const getFix = ({
start: number;
end: number;
fixer: TSESLint.RuleFixer;
}): TSESLint.RuleFix | TSESLint.RuleFix[] => {
}): TSESLint.RuleFix | null => {
const { source } = getNearestNodeFrom(node, isASTWithSource) ?? {};

if (!source) return [];
if (!source) return null;

return fixer.insertTextAfterRange(
[start + getSpanLength(left) + 1, end - getSpanLength(right) - 1],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export function getNearestNodeFrom<T extends ASTOrNodeWithParent>(
): T | null {
while (parent && !isProgram(parent)) {
if (predicate(parent)) {
return (parent as unknown) as T;
return parent;
}

parent = parent.parent as ASTOrNodeWithParent | undefined;
parent = parent.parent;
}

return null;
Expand Down
14 changes: 7 additions & 7 deletions packages/eslint-plugin/src/rules/no-empty-lifecycle-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
getImplementsRemoveFix,
getImportDeclarations,
getImportRemoveFix,
isNotNullOrUndefined,
toPattern,
} from '../utils/utils';

Expand Down Expand Up @@ -54,13 +55,12 @@ export default createESLintRule<Options, MessageIds>({
{
messageId: 'suggestRemoveLifecycleMethod',
fix: (fixer) => {
const importDeclarations = getImportDeclarations(
node,
'@angular/core',
);
const importDeclarations =
getImportDeclarations(node, '@angular/core') ?? [];
const interfaceName = node.key.name.replace(/^ng+/, '');

return [fixer.remove(node)].concat(
return [
fixer.remove(node),
getImplementsRemoveFix(
sourceCode,
node.parent.parent,
Expand All @@ -69,11 +69,11 @@ export default createESLintRule<Options, MessageIds>({
),
getImportRemoveFix(
sourceCode,
importDeclarations ?? [],
importDeclarations,
interfaceName,
fixer,
),
);
].filter(isNotNullOrUndefined);
},
},
],
Expand Down
23 changes: 10 additions & 13 deletions packages/eslint-plugin/src/rules/no-lifecycle-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { TSESTree } from '@typescript-eslint/experimental-utils';
import { createESLintRule } from '../utils/create-eslint-rule';
import {
ANGULAR_LIFECYCLE_METHODS,
isSuper,
getAngularClassDecorator,
getNearestNodeFrom,
isClassDeclaration,
isIdentifier,
isMethodDefinition,
isSuper,
toPattern,
getAngularClassDecorator,
getNearestNodeFrom,
} from '../utils/utils';

type Options = [];
Expand Down Expand Up @@ -36,28 +36,25 @@ export default createESLintRule<Options, MessageIds>({
]);

return {
[`ClassDeclaration MemberExpression[property.name=${angularLifeCycleMethodsPattern}]`]: (
node: TSESTree.MemberExpression,
[`ClassDeclaration CallExpression > MemberExpression[property.name=${angularLifeCycleMethodsPattern}]`]: (
node: TSESTree.MemberExpression & { parent: TSESTree.CallExpression },
) => {
const classDeclaration = getNearestNodeFrom(node, isClassDeclaration);

if (
!getAngularClassDecorator(getClassDeclaration(node)!) ||
!classDeclaration ||
!getAngularClassDecorator(classDeclaration) ||
(isSuper(node.object) && isSuperCallAllowed(node))
) {
return;
}

context.report({ node: node.parent!, messageId: 'noLifecycleCall' });
context.report({ node: node.parent, messageId: 'noLifecycleCall' });
},
};
},
});

function getClassDeclaration(
node: TSESTree.MemberExpression,
): TSESTree.ClassDeclaration | null {
return getNearestNodeFrom(node, isClassDeclaration);
}

function hasSameName(
{ property }: TSESTree.MemberExpression,
{ key }: TSESTree.MethodDefinition,
Expand Down
40 changes: 18 additions & 22 deletions packages/eslint-plugin/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ export function getNearestNodeFrom<T extends TSESTree.Node>(
): T | null {
while (parent && !isProgram(parent)) {
if (predicate(parent)) {
return (parent as unknown) as T;
return parent;
}

parent = parent.parent as TSESTree.Node | undefined;
parent = parent.parent;
}

return null;
Expand All @@ -301,7 +301,7 @@ export function getImportDeclarations(
let parentNode: TSESTree.Node | undefined = node;

while ((parentNode = parentNode.parent)) {
if (parentNode.type !== 'Program') continue;
if (!isProgram(parentNode)) continue;

return parentNode.body.filter(
(node): node is TSESTree.ImportDeclaration =>
Expand All @@ -317,17 +317,17 @@ export function getImplementsRemoveFix(
classDeclaration: TSESTree.ClassDeclaration,
interfaceName: string,
fixer: TSESLint.RuleFixer,
): TSESLint.RuleFix | TSESLint.RuleFix[] {
): TSESLint.RuleFix | undefined {
const { implements: classImplements } = classDeclaration;

if (!classImplements) return [];
if (!classImplements) return undefined;

const identifier = classImplements
.map(({ expression }) => expression)
.filter(isIdentifier)
.find(({ name }) => name === interfaceName);

if (!identifier) return [];
if (!identifier) return undefined;

const isFirstInterface = classImplements[0].expression === identifier;
const isLastInterface =
Expand All @@ -352,14 +352,12 @@ export function getImplementsRemoveFix(

const tokenBeforeInterface = sourceCode.getTokenBefore(identifier);

if (tokenBeforeInterface) {
return fixer.removeRange([
tokenBeforeInterface.range[0],
identifier.range[1],
]);
}
if (!tokenBeforeInterface) return undefined;

return [];
return fixer.removeRange([
tokenBeforeInterface.range[0],
identifier.range[1],
]);
}

function getImportDeclarationSpecifier(
Expand Down Expand Up @@ -414,11 +412,11 @@ export function getImportRemoveFix(
importDeclarations: readonly TSESTree.ImportDeclaration[],
importedName: string,
fixer: TSESLint.RuleFixer,
): TSESLint.RuleFix | TSESLint.RuleFix[] {
): TSESLint.RuleFix | undefined {
const { importDeclaration, importSpecifier } =
getImportDeclarationSpecifier(importDeclarations, importedName) ?? {};

if (!importDeclaration || !importSpecifier) return [];
if (!importDeclaration || !importSpecifier) return undefined;

const isFirstImportSpecifier =
importDeclaration.specifiers[0] === importSpecifier;
Expand All @@ -443,14 +441,12 @@ export function getImportRemoveFix(

const tokenBeforeImportSpecifier = sourceCode.getTokenBefore(importSpecifier);

if (tokenBeforeImportSpecifier) {
return fixer.removeRange([
tokenBeforeImportSpecifier.range[0],
importSpecifier.range[1],
]);
}
if (!tokenBeforeImportSpecifier) return undefined;

return [];
return fixer.removeRange([
tokenBeforeImportSpecifier.range[0],
importSpecifier.range[1],
]);
}

export function getImplementsSchemaFixer(
Expand Down

0 comments on commit 0fd16e3

Please sign in to comment.