From f5d2559e2aecba6ae618746286197a7a55345616 Mon Sep 17 00:00:00 2001 From: splincode Date: Mon, 5 Aug 2024 17:57:24 +0300 Subject: [PATCH] feat: add consistent-return --- projects/eslint-plugin-experience/all.js | 2 ++ .../rules/prefer-deep-imports.js | 34 +++++++++++++++++-- .../get-constructor-from-class-declaration.js | 11 ++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/projects/eslint-plugin-experience/all.js b/projects/eslint-plugin-experience/all.js index 2b3d2dcd..0a75adfd 100644 --- a/projects/eslint-plugin-experience/all.js +++ b/projects/eslint-plugin-experience/all.js @@ -248,6 +248,7 @@ module.exports = { ], '@typescript-eslint/consistent-generic-constructors': 'error', '@typescript-eslint/consistent-indexed-object-style': 'error', + '@typescript-eslint/consistent-return': 'error', '@typescript-eslint/consistent-type-assertions': [ 'error', { @@ -782,6 +783,7 @@ module.exports = { files: ['*.js'], rules: { '@taiga-ui/experience/no-implicit-public': 'off', + '@typescript-eslint/consistent-return': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-member-accessibility': 'off', '@typescript-eslint/no-empty-function': 'off', diff --git a/projects/eslint-plugin-experience/rules/prefer-deep-imports.js b/projects/eslint-plugin-experience/rules/prefer-deep-imports.js index 8ce5857d..1a24b4ed 100644 --- a/projects/eslint-plugin-experience/rules/prefer-deep-imports.js +++ b/projects/eslint-plugin-experience/rules/prefer-deep-imports.js @@ -5,10 +5,16 @@ const MESSAGE_ID = 'prefer-deep-imports'; const ERROR_MESSAGE = 'Import via root level entry point are prohibited for this package'; module.exports = { + /** + * @param {any} context + */ create(context) { const {importFilter} = context.options[0] || {}; return { + /** + * @param {{ specifiers: any; source: { value: any; }; importKind: string; range: import("eslint").AST.Range; }} importDeclaration + */ [`ImportDeclaration[source.value=${getFilterRegExp(importFilter)}]`]( importDeclaration, ) { @@ -16,6 +22,9 @@ module.exports = { const packageName = importDeclaration.source.value; context.report({ + /** + * @param {import('eslint').Rule.RuleFixer} fixer + */ fix: (fixer) => { const allTsFiles = glob.globSync( `node_modules/${packageName}/**/*.ts`, @@ -26,6 +35,9 @@ module.exports = { }, ); const importedEntitiesSourceFiles = importedEntities.map( + /** + * @param {any} entity + */ ({imported}) => allTsFiles .find((path) => { @@ -42,11 +54,22 @@ module.exports = { const entryPoints = importedEntitiesSourceFiles.map(findNearestEntryPoint); - if (entryPoints.some((e) => !e)) { + if ( + entryPoints.some( + /** + * @param {any} e + */ + (e) => !e, + ) + ) { return; // to prevent `import {A,B,C} from 'undefined';` } const newImports = importedEntities.map( + /** + * @param {any} entity + * @param {number} i + */ ({imported, local}, i) => { const importedEntity = imported.name === local.name @@ -92,6 +115,10 @@ module.exports = { }, }; +/** + * @param {string} filePath + * @return {any} + */ function findNearestEntryPoint(filePath) { const pathSegments = filePath.split('/'); @@ -104,13 +131,16 @@ function findNearestEntryPoint(filePath) { } } +/** + * @param {string} filter + */ function getFilterRegExp(filter) { if (typeof filter === 'string' && filter.startsWith('/')) { return filter; } const packages = typeof filter === 'string' ? [filter] : filter; - const [npmScope] = packages[0].split('/'); + const [npmScope] = packages[0]?.split('/') ?? []; const packageNames = packages.map((p) => p.split('/')[1]).filter(Boolean); return `/^${npmScope}\\u002F(${packageNames.join('|')})$/`; diff --git a/projects/eslint-plugin-experience/rules/utils/get-constructor-from-class-declaration.js b/projects/eslint-plugin-experience/rules/utils/get-constructor-from-class-declaration.js index f04b1a04..2cef2787 100644 --- a/projects/eslint-plugin-experience/rules/utils/get-constructor-from-class-declaration.js +++ b/projects/eslint-plugin-experience/rules/utils/get-constructor-from-class-declaration.js @@ -2,7 +2,7 @@ const isMethodDefinition = require('./is-method'); /** * Returns a constructor from a provided node if it exists. - * @param node {import('eslint').Rule.Node} + * @param node {any} * @returns {*} */ module.exports = function getConstructorFromClassDeclaration(node) { @@ -19,8 +19,13 @@ module.exports = function getConstructorFromClassDeclaration(node) { } const constructorMethodDefinition = classElements - .filter((classElement) => isMethodDefinition(classElement)) - .find((methodDefinition) => methodDefinition.kind === 'constructor'); + .filter((/** @type {import("eslint").Rule.Node} */ classElement) => + isMethodDefinition(classElement), + ) + .find( + (/** @type {{ kind: string; }} */ methodDefinition) => + methodDefinition.kind === 'constructor', + ); if ( !constructorMethodDefinition ||