-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add fixer for use-pipe-transform-interface (#260)
- Loading branch information
1 parent
e1057dd
commit e3f4db6
Showing
5 changed files
with
147 additions
and
51 deletions.
There are no files selected for viewing
98 changes: 84 additions & 14 deletions
98
packages/eslint-plugin/src/rules/use-pipe-transform-interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,115 @@ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { TSESTree, TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import { createESLintRule } from '../utils/create-eslint-rule'; | ||
import { PIPE_CLASS_DECORATOR } from '../utils/selectors'; | ||
import { | ||
AngularClassDecorators, | ||
getDeclaredInterfaceName, | ||
} from '../utils/utils'; | ||
import { getDeclaredInterfaceName, isImportDeclaration } from '../utils/utils'; | ||
|
||
type Options = []; | ||
export type MessageIds = 'usePipeTransformInterface'; | ||
export const RULE_NAME = 'use-pipe-transform-interface'; | ||
|
||
const PIPE_TRANSFORM = 'PipeTransform'; | ||
const ANGULAR_CORE_MODULE_PATH = '@angular/core'; | ||
|
||
export default createESLintRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: `Ensures tht classes decorated with @${AngularClassDecorators.Pipe} implement ${PIPE_TRANSFORM} interface`, | ||
description: `Ensures that Pipes implement \`${PIPE_TRANSFORM}\` interface`, | ||
category: 'Best Practices', | ||
recommended: 'error', | ||
}, | ||
fixable: 'code', | ||
schema: [], | ||
messages: { | ||
usePipeTransformInterface: `Classes decorated with @${AngularClassDecorators.Pipe} decorator should implement ${PIPE_TRANSFORM} interface`, | ||
usePipeTransformInterface: `Pipes should implement \`${PIPE_TRANSFORM}\` interface`, | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
[PIPE_CLASS_DECORATOR](node: TSESTree.Decorator) { | ||
const classParent = node.parent as TSESTree.ClassDeclaration; | ||
if (getDeclaredInterfaceName(classParent, PIPE_TRANSFORM)) { | ||
return; | ||
} | ||
[PIPE_CLASS_DECORATOR]({ | ||
parent: classDeclaration, | ||
}: TSESTree.Decorator & { parent: TSESTree.ClassDeclaration }) { | ||
if (getDeclaredInterfaceName(classDeclaration, PIPE_TRANSFORM)) return; | ||
|
||
const { | ||
errorNode, | ||
implementsNodeReplace, | ||
implementsTextReplace, | ||
} = getErrorSchemaOptions(classDeclaration); | ||
|
||
context.report({ | ||
node: classParent, | ||
node: errorNode, | ||
messageId: 'usePipeTransformInterface', | ||
fix: (fixer) => [ | ||
getImportFix(classDeclaration, fixer), | ||
fixer.insertTextAfter(implementsNodeReplace, implementsTextReplace), | ||
], | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
function getErrorSchemaOptions(classDeclaration: TSESTree.ClassDeclaration) { | ||
const classDeclarationIdentifier = classDeclaration.id as TSESTree.Identifier; | ||
const [ | ||
errorNode, | ||
implementsNodeReplace, | ||
implementsTextReplace, | ||
] = classDeclaration.implements | ||
? [ | ||
classDeclarationIdentifier, | ||
getLast(classDeclaration.implements), | ||
`, ${PIPE_TRANSFORM}`, | ||
] | ||
: [ | ||
classDeclarationIdentifier, | ||
classDeclarationIdentifier, | ||
` implements ${PIPE_TRANSFORM}`, | ||
]; | ||
|
||
return { errorNode, implementsNodeReplace, implementsTextReplace } as const; | ||
} | ||
|
||
function getImportDeclaration( | ||
node: TSESTree.ClassDeclaration, | ||
module: string, | ||
): TSESTree.ImportDeclaration | undefined { | ||
let parentNode: TSESTree.Node | undefined = node; | ||
|
||
while ((parentNode = parentNode.parent)) { | ||
if (parentNode.type !== 'Program') continue; | ||
|
||
return parentNode.body.find( | ||
(node) => isImportDeclaration(node) && node.source.value === module, | ||
) as TSESTree.ImportDeclaration | undefined; | ||
} | ||
|
||
return parentNode; | ||
} | ||
|
||
function getImportFix( | ||
node: TSESTree.ClassDeclaration, | ||
fixer: TSESLint.RuleFixer, | ||
) { | ||
const importDeclaration = getImportDeclaration( | ||
node, | ||
ANGULAR_CORE_MODULE_PATH, | ||
); | ||
|
||
if (!importDeclaration?.specifiers.length) { | ||
return fixer.insertTextAfterRange( | ||
[0, 0], | ||
`import { ${PIPE_TRANSFORM} } from '${ANGULAR_CORE_MODULE_PATH}';\n`, | ||
); | ||
} | ||
|
||
const lastImportSpecifier = getLast(importDeclaration.specifiers); | ||
|
||
return fixer.insertTextAfter(lastImportSpecifier, `, ${PIPE_TRANSFORM}`); | ||
} | ||
|
||
function getLast<T extends readonly unknown[]>(items: T): T[0] { | ||
return items.slice(-1)[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters