Skip to content

Commit

Permalink
feat: add consistent-return
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Aug 5, 2024
1 parent 852552c commit f5d2559
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
2 changes: 2 additions & 0 deletions projects/eslint-plugin-experience/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
{
Expand Down Expand Up @@ -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',
Expand Down
34 changes: 32 additions & 2 deletions projects/eslint-plugin-experience/rules/prefer-deep-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ 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,
) {
const importedEntities = importDeclaration.specifiers;
const packageName = importDeclaration.source.value;

context.report({
/**
* @param {import('eslint').Rule.RuleFixer} fixer
*/
fix: (fixer) => {
const allTsFiles = glob.globSync(
`node_modules/${packageName}/**/*.ts`,
Expand All @@ -26,6 +35,9 @@ module.exports = {
},
);
const importedEntitiesSourceFiles = importedEntities.map(
/**
* @param {any} entity
*/
({imported}) =>
allTsFiles
.find((path) => {
Expand All @@ -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
Expand Down Expand Up @@ -92,6 +115,10 @@ module.exports = {
},
};

/**
* @param {string} filePath
* @return {any}
*/
function findNearestEntryPoint(filePath) {
const pathSegments = filePath.split('/');

Expand All @@ -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('|')})$/`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 ||
Expand Down

0 comments on commit f5d2559

Please sign in to comment.