Skip to content

Commit

Permalink
feat(eslint-plugin): new no-deep-imports-rule (#2666)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov authored Sep 13, 2022
1 parent 3c9378b commit 467420e
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 16 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,15 @@ module.exports = {
},
],
'@taiga-ui/injection-token-description': 'error',
'@taiga-ui/no-deep-imports': [
'error',
{
currentProject: `(?<=taiga-ui/projects/)([-\\w]+)`,
ignoreImports: [
'\\?raw',
'@taiga-ui/testing/cypress', // TODO temporary workaround until it can be imported from @taiga-ui/testing
],
},
],
},
};
4 changes: 1 addition & 3 deletions projects/addon-charts/components/axes/axes.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import {
import {TUI_ALWAYS_DASHED, TUI_ALWAYS_SOLID} from '@taiga-ui/addon-charts/constants';
import {TuiLineHandler, TuiLineType} from '@taiga-ui/addon-charts/types';
import {CHAR_NO_BREAK_SPACE, tuiDefaultProp} from '@taiga-ui/cdk';
import {MODE_PROVIDER} from '@taiga-ui/core/providers';
import {TUI_MODE} from '@taiga-ui/core/tokens';
import {TuiBrightness} from '@taiga-ui/core/types';
import {MODE_PROVIDER, TUI_MODE, TuiBrightness} from '@taiga-ui/core';
import {Observable} from 'rxjs';

@Component({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TUI_CARD_NUMBER_TEXTS,
} from '@taiga-ui/addon-commerce/tokens';
import {tuiTypedFromEvent} from '@taiga-ui/cdk';
import {TUI_MEDIA} from '@taiga-ui/core/tokens';
import {TUI_MEDIA} from '@taiga-ui/core';
import {combineLatest, Observable, of} from 'rxjs';
import {map, startWith, switchMap} from 'rxjs/operators';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {TuiEditorFontOption} from '@taiga-ui/addon-editor/interfaces';
import {TuiLanguageEditor} from '@taiga-ui/i18n/interfaces';
import {TuiLanguageEditor} from '@taiga-ui/i18n';

export function tuiDefaultFontOptionsHandler(
texts: TuiLanguageEditor['editorFontOptions'],
Expand Down
2 changes: 1 addition & 1 deletion projects/addon-editor/tokens/editor-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@taiga-ui/addon-editor/constants';
import {TuiEditorFontOption} from '@taiga-ui/addon-editor/interfaces';
import {TuiHandler} from '@taiga-ui/cdk';
import {TuiLanguageEditor} from '@taiga-ui/i18n/interfaces';
import {TuiLanguageEditor} from '@taiga-ui/i18n';

export interface TuiEditorOptions {
readonly blankColor: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Component, forwardRef, Inject} from '@angular/core';
import {FormControl} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {TuiCodeCVCLength} from '@taiga-ui/addon-commerce/types';
import {TuiCodeCVCLength} from '@taiga-ui/addon-commerce';
import {TuiDocExample} from '@taiga-ui/addon-doc';
import {tuiIsString} from '@taiga-ui/cdk';
import {TuiAlertService} from '@taiga-ui/core';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
import {TuiAddonDocModule, tuiGenerateRoutes} from '@taiga-ui/addon-doc';
import {TuiLazyLoadingModule} from '@taiga-ui/kit/directives/lazy-loading';
import {TuiLazyLoadingModule} from '@taiga-ui/kit';

import {TuiLazyLoadingExample1} from './examples/1';
import {ExampleTuiLazyLoadingComponent} from './lazy-loading.component';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import {
TuiTextfieldControllerModule,
} from '@taiga-ui/core';
import {
TuiFieldErrorPipeModule,
TuiInputCountModule,
TuiInputModule,
TuiInputNumberModule,
TuiInputPhoneModule,
} from '@taiga-ui/kit';
import {TuiFieldErrorPipeModule} from '@taiga-ui/kit/pipes/field-error';
import {PolymorpheusModule} from '@tinkoff/ng-polymorpheus';

import {TuiFieldErrorPipeExample1} from './examples/1';
Expand Down
2 changes: 1 addition & 1 deletion projects/demo/src/modules/tables/reorder/reorder.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
import {RouterModule} from '@angular/router';
import {TuiAddonDocModule, tuiGenerateRoutes} from '@taiga-ui/addon-doc';
import {TuiReorderModule} from '@taiga-ui/addon-table/components/reorder';
import {TuiReorderModule} from '@taiga-ui/addon-table';

import {TuiReorderExample1} from './examples/1';
import {ExampleTuiReorderComponent} from './reorder.component';
Expand Down
1 change: 1 addition & 0 deletions projects/eslint-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
module.exports = {
rules: {
'injection-token-description': require('./injection-token-description'),
'no-deep-imports': require('./no-deep-imports'),
},
};
95 changes: 95 additions & 0 deletions projects/eslint-plugin/no-deep-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const MESSAGE_ID = `no-deep-imports`;
const ERROR_MESSAGE = `Deep imports of Taiga UI packages are prohibited`;

const DEFAULT_OPTIONS = {
importDeclaration: `^@taiga-ui*`,
deepImport: `(?<=^@taiga-ui/[\\w-]+)(/.+)$`,
currentProject: ``,
ignoreImports: [],
};

/**
* @type {import('eslint').Rule.RuleModule}
*/
module.exports = {
meta: {
type: `problem`,
docs: {description: ERROR_MESSAGE},
messages: {
[MESSAGE_ID]: ERROR_MESSAGE,
},
fixable: `code`,
schema: [
{
type: `object`,
properties: {
importDeclaration: {
type: `string`,
description: `RegExp string to detect import declarations for which this rule should be applied`,
},
deepImport: {
type: `string`,
description: `RegExp string to pick out deep import part`,
},
currentProject: {
type: `string`,
description: `RegExp string to pick out current project name of processed file`,
},
ignoreImports: {
type: `array`,
items: {
type: `string`,
},
description: `RegExp string to exclude import declarations which is selected by importDeclaration-option`,
},
},
additionalProperties: false,
},
],
},
create(context) {
const {importDeclaration, deepImport, currentProject, ignoreImports} = {
...DEFAULT_OPTIONS,
...(context.options[0] || {}),
};

return {
[`ImportDeclaration[source.value=/${importDeclaration}/]`]({
source: sourceNode,
}) {
const currentFilePath = context.getFilename();
const [currentFileProjectName] =
(currentProject &&
currentFilePath.match(new RegExp(currentProject, 'g'))) ||
[];
const importSource = sourceNode?.value || ``;
const isInsideTheSameEntryPoint = Boolean(
currentFileProjectName &&
importSource.includes(currentFileProjectName),
);

if (
isInsideTheSameEntryPoint ||
ignoreImports.some(p => importSource.match(new RegExp(p, 'g')))
) {
return;
}

if (importSource.match(new RegExp(deepImport, 'g'))?.length) {
context.report({
node: sourceNode,
messageId: MESSAGE_ID,
fix: fixer => {
const [start, end] = sourceNode.range;

return fixer.replaceTextRange(
[start + 1, end - 1], // keeps quotes
importSource.replace(new RegExp(deepImport, 'g'), ``),
);
},
});
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ChangeDetectionStrategy, Component, HostBinding} from '@angular/core';
import {TuiDestroyService, TuiHoveredService} from '@taiga-ui/cdk';
import {TuiHintComponent, TuiPositionService} from '@taiga-ui/core';
import {tuiFadeIn} from '@taiga-ui/core/animations';
import {tuiFadeIn, TuiHintComponent, TuiPositionService} from '@taiga-ui/core';

@Component({
selector: `tui-line-clamp-box`,
Expand Down
3 changes: 1 addition & 2 deletions projects/kit/components/radio/radio-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {InjectionToken, ValueProvider} from '@angular/core';
import {TuiAppearance} from '@taiga-ui/core/enums';
import {TuiSizeL} from '@taiga-ui/core/types';
import {TuiAppearance, TuiSizeL} from '@taiga-ui/core';

export interface TuiRadioOptions {
readonly size: TuiSizeL;
Expand Down
2 changes: 1 addition & 1 deletion projects/kit/components/tag/tag-options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {InjectionToken, ValueProvider} from '@angular/core';
import {TuiSizeL, TuiSizeS} from '@taiga-ui/core/types';
import {TuiSizeL, TuiSizeS} from '@taiga-ui/core';
import {TuiStatus} from '@taiga-ui/kit/types';

export interface TuiTagOptions {
Expand Down
2 changes: 1 addition & 1 deletion projects/testing/utils/replace-nbsp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CHAR_NO_BREAK_SPACE} from '@taiga-ui/cdk/constants';
import {CHAR_NO_BREAK_SPACE} from '@taiga-ui/cdk';

export function tuiReplaceNbsp(content?: string): string {
return content?.replace(new RegExp(CHAR_NO_BREAK_SPACE, `g`), ` `) ?? ``;
Expand Down

0 comments on commit 467420e

Please sign in to comment.