117616: Added support to alias-import to disallow aliasing certain imports

This commit is contained in:
Alexandre Vryghem
2025-04-21 14:58:34 +02:00
parent aed0460cfe
commit f0a00aca95
13 changed files with 379 additions and 47 deletions

View File

@@ -270,7 +270,7 @@
{ {
"package": "rxjs", "package": "rxjs",
"imported": "of", "imported": "of",
"local": "observableOf" "local": "of"
} }
] ]
} }

View File

@@ -22,18 +22,21 @@ _______
<button [dsBtnDisabled]="true">Submit</button> <button [dsBtnDisabled]="true">Submit</button>
``` ```
##### disabled attribute is still valid on non-button elements ##### disabled attribute is still valid on non-button elements
```html ```html
<input disabled> <input disabled>
``` ```
##### [disabled] attribute is still valid on non-button elements ##### [disabled] attribute is still valid on non-button elements
```html ```html
<input [disabled]="true"> <input [disabled]="true">
``` ```
##### angular dynamic attributes that use disabled are still valid ##### angular dynamic attributes that use disabled are still valid
```html ```html
@@ -43,12 +46,16 @@ _______
#### Invalid code &amp; automatic fixes #### Invalid code &amp; automatic fixes
##### should not use disabled attribute in HTML templates ##### should not use disabled attribute in HTML templates
```html ```html
<button disabled>Submit</button> <button disabled>Submit</button>
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -65,6 +72,9 @@ Result of `yarn lint --fix`:
```html ```html
<button [disabled]="true">Submit</button> <button [disabled]="true">Submit</button>
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -26,6 +26,7 @@ _______
<ds-test-themeable [test]="something"></ds-test-themeable> <ds-test-themeable [test]="something"></ds-test-themeable>
``` ```
##### use no-prefix selectors in TypeScript templates ##### use no-prefix selectors in TypeScript templates
```html ```html
@@ -36,6 +37,7 @@ class Test {
} }
``` ```
##### use no-prefix selectors in TypeScript test templates ##### use no-prefix selectors in TypeScript test templates
Filename: `lint/test/fixture/src/test.spec.ts` Filename: `lint/test/fixture/src/test.spec.ts`
@@ -48,6 +50,7 @@ class Test {
} }
``` ```
##### base selectors are also allowed in TypeScript test templates ##### base selectors are also allowed in TypeScript test templates
Filename: `lint/test/fixture/src/test.spec.ts` Filename: `lint/test/fixture/src/test.spec.ts`
@@ -63,6 +66,7 @@ class Test {
#### Invalid code &amp; automatic fixes #### Invalid code &amp; automatic fixes
##### themed override selectors are not allowed in HTML templates ##### themed override selectors are not allowed in HTML templates
@@ -71,6 +75,9 @@ class Test {
<ds-themed-test-themeable/> <ds-themed-test-themeable/>
<ds-themed-test-themeable></ds-themed-test-themeable> <ds-themed-test-themeable></ds-themed-test-themeable>
<ds-themed-test-themeable [test]="something"></ds-themed-test-themeable> <ds-themed-test-themeable [test]="something"></ds-themed-test-themeable>
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -93,6 +100,9 @@ Result of `yarn lint --fix`:
<ds-base-test-themeable/> <ds-base-test-themeable/>
<ds-base-test-themeable></ds-base-test-themeable> <ds-base-test-themeable></ds-base-test-themeable>
<ds-base-test-themeable [test]="something"></ds-base-test-themeable> <ds-base-test-themeable [test]="something"></ds-base-test-themeable>
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -33,6 +33,42 @@ A list of all the imports that you want to alias for clarity. Every alias should
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
``` ```
With options:
```json
{
"aliases": [
{
"package": "rxjs",
"imported": "of",
"local": "observableOf"
}
]
}
```
##### enforce unaliased import
```typescript
import { combineLatest } from 'rxjs';
```
With options:
```json
{
"aliases": [
{
"package": "rxjs",
"imported": "combineLatest",
"local": "combineLatest"
}
]
}
```
@@ -42,6 +78,9 @@ import { of as observableOf } from 'rxjs';
```typescript ```typescript
import { of } from 'rxjs'; import { of } from 'rxjs';
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -58,6 +97,9 @@ import { of as observableOf } from 'rxjs';
```typescript ```typescript
import { of as ofSomething } from 'rxjs'; import { of as ofSomething } from 'rxjs';
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -70,4 +112,37 @@ import { of as observableOf } from 'rxjs';
``` ```
##### disallow aliasing import
```typescript
import { combineLatest as observableCombineLatest } from 'rxjs';
With options:
```json
{
"aliases": [
{
"package": "rxjs",
"imported": "combineLatest",
"local": "combineLatest"
}
]
}
```
```
Will produce the following error(s):
```
This import should not use an alias
```
Result of `yarn lint --fix`:
```typescript
import { combineLatest } from 'rxjs';
```

View File

@@ -45,6 +45,7 @@ Whether the last import should have a trailing comma (only applicable for multil
export class AppComponent {} export class AppComponent {}
``` ```
##### should not inlines singular imports when maxItems is 0 ##### should not inlines singular imports when maxItems is 0
```typescript ```typescript
@@ -60,6 +61,7 @@ export class AppComponent {}
export class AppComponent {} export class AppComponent {}
``` ```
##### should inline singular imports when maxItems is 1 ##### should inline singular imports when maxItems is 1
```typescript ```typescript
@@ -73,6 +75,15 @@ export class AppComponent {}
export class AppComponent {} export class AppComponent {}
``` ```
With options:
```json
{
"maxItems": 1
}
```
@@ -92,6 +103,9 @@ export class AppComponent {}
], ],
}) })
export class AppComponent {} export class AppComponent {}
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -125,6 +139,9 @@ export class AppComponent {}
imports: [RootComponent], imports: [RootComponent],
}) })
export class AppComponent {} export class AppComponent {}
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -159,6 +176,17 @@ export class AppComponent {}
], ],
}) })
export class AppComponent {} export class AppComponent {}
With options:
```json
{
"maxItems": 1
}
```
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -189,6 +217,9 @@ export class AppComponent {}
imports: [AsyncPipe, RootComponent], imports: [AsyncPipe, RootComponent],
}) })
export class AppComponent {} export class AppComponent {}
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -29,6 +29,7 @@ class Something {
} }
``` ```
##### Base component ##### Base component
```typescript ```typescript
@@ -40,6 +41,7 @@ class TestThemeableComponent {
} }
``` ```
##### Wrapper component ##### Wrapper component
Filename: `lint/test/fixture/src/app/test/themed-test-themeable.component.ts` Filename: `lint/test/fixture/src/app/test/themed-test-themeable.component.ts`
@@ -56,6 +58,7 @@ class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponen
} }
``` ```
##### Override component ##### Override component
Filename: `lint/test/fixture/src/themes/test/app/test/test-themeable.component.ts` Filename: `lint/test/fixture/src/themes/test/app/test/test-themeable.component.ts`
@@ -72,6 +75,7 @@ class Override extends BaseComponent {
#### Invalid code &amp; automatic fixes #### Invalid code &amp; automatic fixes
##### Base component must be standalone ##### Base component must be standalone
@@ -82,6 +86,9 @@ class Override extends BaseComponent {
}) })
class TestThemeableComponent { class TestThemeableComponent {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -109,6 +116,9 @@ Filename: `lint/test/fixture/src/app/test/themed-test-themeable.component.ts`
}) })
class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> { class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -139,6 +149,9 @@ Filename: `lint/test/fixture/src/app/test/themed-test-themeable.component.ts`
}) })
class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> { class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -173,6 +186,9 @@ import { SomethingElse } from './somewhere-else';
}) })
class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> { class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -209,6 +225,9 @@ import { Something, SomethingElse } from './somewhere-else';
}) })
class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> { class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -239,6 +258,9 @@ Filename: `lint/test/fixture/src/themes/test/app/test/test-themeable.component.t
}) })
class Override extends BaseComponent { class Override extends BaseComponent {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -34,6 +34,7 @@ class Something {
} }
``` ```
##### Themeable component selector should replace the original version, unthemed version should be changed to ds-base- ##### Themeable component selector should replace the original version, unthemed version should be changed to ds-base-
```typescript ```typescript
@@ -56,6 +57,7 @@ class OverrideSomething extends Something {
} }
``` ```
##### Other themed component wrappers should not interfere ##### Other themed component wrappers should not interfere
```typescript ```typescript
@@ -75,6 +77,7 @@ class ThemedSomethingElse extends ThemedComponent<SomethingElse> {
#### Invalid code &amp; automatic fixes #### Invalid code &amp; automatic fixes
##### Wrong selector for base component ##### Wrong selector for base component
@@ -87,6 +90,9 @@ Filename: `lint/test/fixture/src/app/test/test-themeable.component.ts`
}) })
class TestThemeableComponent { class TestThemeableComponent {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -113,6 +119,9 @@ Filename: `lint/test/fixture/src/app/test/themed-test-themeable.component.ts`
}) })
class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> { class ThemedTestThemeableComponent extends ThemedComponent<TestThemeableComponent> {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -139,6 +148,9 @@ Filename: `lint/test/fixture/src/themes/test/app/test/test-themeable.component.t
}) })
class TestThememeableComponent extends BaseComponent { class TestThememeableComponent extends BaseComponent {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -33,6 +33,7 @@ const config = {
} }
``` ```
##### allow base class in class declaration ##### allow base class in class declaration
```typescript ```typescript
@@ -40,6 +41,7 @@ export class TestThemeableComponent {
} }
``` ```
##### allow inheriting from base class ##### allow inheriting from base class
```typescript ```typescript
@@ -49,6 +51,7 @@ export class ThemedAdminSidebarComponent extends ThemedComponent<TestThemeableCo
} }
``` ```
##### allow base class in ViewChild ##### allow base class in ViewChild
```typescript ```typescript
@@ -59,6 +62,7 @@ export class Something {
} }
``` ```
##### allow wrapper selectors in test queries ##### allow wrapper selectors in test queries
Filename: `lint/test/fixture/src/app/test/test.component.spec.ts` Filename: `lint/test/fixture/src/app/test/test.component.spec.ts`
@@ -68,6 +72,7 @@ By.css('ds-themeable');
By.css('#test > ds-themeable > #nest'); By.css('#test > ds-themeable > #nest');
``` ```
##### allow wrapper selectors in cypress queries ##### allow wrapper selectors in cypress queries
Filename: `lint/test/fixture/src/app/test/test.component.cy.ts` Filename: `lint/test/fixture/src/app/test/test.component.cy.ts`
@@ -80,6 +85,7 @@ By.css('#test > ds-themeable > #nest');
#### Invalid code &amp; automatic fixes #### Invalid code &amp; automatic fixes
##### disallow direct usages of base class ##### disallow direct usages of base class
@@ -92,6 +98,9 @@ const config = {
a: TestThemeableComponent, a: TestThemeableComponent,
b: TestComponent, b: TestComponent,
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -122,6 +131,9 @@ const config = {
b: TestComponent, b: TestComponent,
c: Something, c: Something,
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -152,6 +164,9 @@ const DECLARATIONS = [
Something, Something,
ThemedTestThemeableComponent, ThemedTestThemeableComponent,
]; ];
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -175,6 +190,9 @@ Filename: `lint/test/fixture/src/app/test/test.component.spec.ts`
```typescript ```typescript
By.css('ds-themed-themeable'); By.css('ds-themed-themeable');
By.css('#test > ds-themed-themeable > #nest'); By.css('#test > ds-themed-themeable > #nest');
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -196,6 +214,9 @@ Filename: `lint/test/fixture/src/app/test/test.component.spec.ts`
```typescript ```typescript
By.css('ds-base-themeable'); By.css('ds-base-themeable');
By.css('#test > ds-base-themeable > #nest'); By.css('#test > ds-base-themeable > #nest');
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -217,6 +238,9 @@ Filename: `lint/test/fixture/src/app/test/test.component.cy.ts`
```typescript ```typescript
cy.get('ds-themed-themeable'); cy.get('ds-themed-themeable');
cy.get('#test > ds-themed-themeable > #nest'); cy.get('#test > ds-themed-themeable > #nest');
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -238,6 +262,9 @@ Filename: `lint/test/fixture/src/app/test/test.component.cy.ts`
```typescript ```typescript
cy.get('ds-base-themeable'); cy.get('ds-base-themeable');
cy.get('#test > ds-base-themeable > #nest'); cy.get('#test > ds-base-themeable > #nest');
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -268,6 +295,9 @@ import { TestThemeableComponent } from '../../../../app/test/test-themeable.comp
}) })
export class UsageComponent { export class UsageComponent {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -308,6 +338,9 @@ import { ThemedTestThemeableComponent } from '../../../../app/test/themed-test-t
}) })
export class UsageComponent { export class UsageComponent {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -30,6 +30,7 @@ export class Something extends SomethingElse {
} }
``` ```
##### plain file declares no theme in @listableObjectComponent ##### plain file declares no theme in @listableObjectComponent
Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.ts` Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.ts`
@@ -40,6 +41,7 @@ export class Something extends SomethingElse {
} }
``` ```
##### plain file declares explicit undefined theme in @listableObjectComponent ##### plain file declares explicit undefined theme in @listableObjectComponent
Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.ts` Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.ts`
@@ -50,6 +52,7 @@ export class Something extends SomethingElse {
} }
``` ```
##### test file declares theme outside of theme directory ##### test file declares theme outside of theme directory
Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.spec.ts` Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.spec.ts`
@@ -60,6 +63,7 @@ export class Something extends SomethingElse {
} }
``` ```
##### only track configured decorators ##### only track configured decorators
Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.ts` Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.ts`
@@ -73,6 +77,7 @@ export class Something extends SomethingElse {
#### Invalid code &amp; automatic fixes #### Invalid code &amp; automatic fixes
##### theme file declares the wrong theme in @listableObjectComponent ##### theme file declares the wrong theme in @listableObjectComponent
@@ -83,6 +88,9 @@ Filename: `lint/test/fixture/src/themes/test/app/dynamic-component/dynamic-compo
@listableObjectComponent(something, somethingElse, undefined, 'test-2') @listableObjectComponent(something, somethingElse, undefined, 'test-2')
export class Something extends SomethingElse { export class Something extends SomethingElse {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -105,6 +113,9 @@ Filename: `lint/test/fixture/src/app/dynamic-component/dynamic-component.ts`
@listableObjectComponent(something, somethingElse, undefined, 'test-2') @listableObjectComponent(something, somethingElse, undefined, 'test-2')
export class Something extends SomethingElse { export class Something extends SomethingElse {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -127,6 +138,9 @@ Filename: `lint/test/fixture/src/themes/test-2/app/dynamic-component/dynamic-com
@listableObjectComponent(something, somethingElse, undefined) @listableObjectComponent(something, somethingElse, undefined)
export class Something extends SomethingElse { export class Something extends SomethingElse {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -149,6 +163,9 @@ Filename: `lint/test/fixture/src/themes/test-2/app/dynamic-component/dynamic-com
@listableObjectComponent(something, somethingElse, undefined, undefined) @listableObjectComponent(something, somethingElse, undefined, undefined)
export class Something extends SomethingElse { export class Something extends SomethingElse {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -24,6 +24,7 @@ test;
} }
``` ```
##### Regular class defines an input with a default value ##### Regular class defines an input with a default value
```typescript ```typescript
@@ -37,6 +38,7 @@ test = 'test';
#### Invalid code #### Invalid code
##### ThemedComponent wrapper defines an input with a default value ##### ThemedComponent wrapper defines an input with a default value
@@ -56,6 +58,9 @@ test2: number = 123;
@Input() @Input()
test3: number[] = [1,2,3]; test3: number[] = [1,2,3];
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```
@@ -74,6 +79,9 @@ export class TTest extends ThemedComponent<Test> {
@Input() @Input()
test = undefined; test = undefined;
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -44,6 +44,7 @@ export class C {
} }
``` ```
##### unchecked decorator, some repetitions ##### unchecked decorator, some repetitions
```typescript ```typescript
@@ -59,6 +60,7 @@ export class B {
#### Invalid code #### Invalid code
##### checked decorator, some repetitions ##### checked decorator, some repetitions
@@ -71,6 +73,9 @@ export class A {
@listableObjectComponent(a) @listableObjectComponent(a)
export class B { export class B {
} }
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```

View File

@@ -13,9 +13,10 @@ import {
} from '../../util/structure'; } from '../../util/structure';
export enum Message { export enum Message {
NO_ALIAS = 'noAlias', MISSING_ALIAS = 'missingAlias',
WRONG_ALIAS = 'wrongAlias', WRONG_ALIAS = 'wrongAlias',
MULTIPLE_ALIASES = 'multipleAliases', MULTIPLE_ALIASES = 'multipleAliases',
UNNECESSARY_ALIAS = 'unnecessaryAlias',
} }
interface AliasImportOptions { interface AliasImportOptions {
@@ -39,9 +40,10 @@ export const info: DSpaceESLintRuleInfo<[AliasImportOptions], [AliasImportDocOpt
description: 'Unclear imports should be aliased for clarity', description: 'Unclear imports should be aliased for clarity',
}, },
messages: { messages: {
[Message.NO_ALIAS]: 'This import must be aliased', [Message.MISSING_ALIAS]: 'This import must be aliased',
[Message.WRONG_ALIAS]: 'This import uses the wrong alias (should be {{ local }})', [Message.WRONG_ALIAS]: 'This import uses the wrong alias (should be {{ local }})',
[Message.MULTIPLE_ALIASES]: 'This import was used twice with a different alias (should be {{ local }})', [Message.MULTIPLE_ALIASES]: 'This import was used twice with a different alias (should be {{ local }})',
[Message.UNNECESSARY_ALIAS]: 'This import should not use an alias',
}, },
fixable: 'code', fixable: 'code',
type: 'problem', type: 'problem',
@@ -103,6 +105,34 @@ export const tests: NamedTests = {
code: ` code: `
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
`, `,
options: [
{
aliases: [
{
package: 'rxjs',
imported: 'of',
local: 'observableOf',
},
],
},
],
},
{
name: 'enforce unaliased import',
code: `
import { combineLatest } from 'rxjs';
`,
options: [
{
aliases: [
{
package: 'rxjs',
imported: 'combineLatest',
local: 'combineLatest',
},
],
},
],
}, },
], ],
invalid: [ invalid: [
@@ -113,7 +143,7 @@ import { of } from 'rxjs';
`, `,
errors: [ errors: [
{ {
messageId: 'noAlias', messageId: 'missingAlias',
}, },
], ],
output: ` output: `
@@ -134,6 +164,31 @@ import { of as ofSomething } from 'rxjs';
import { of as observableOf } from 'rxjs'; import { of as observableOf } from 'rxjs';
`, `,
}, },
{
name: 'disallow aliasing import',
code: `
import { combineLatest as observableCombineLatest } from 'rxjs';
`,
errors: [
{
messageId: 'unnecessaryAlias',
},
],
output: `
import { combineLatest } from 'rxjs';
`,
options: [
{
aliases: [
{
package: 'rxjs',
imported: 'combineLatest',
local: 'combineLatest',
},
],
},
],
},
], ],
}; };
@@ -145,9 +200,46 @@ import { of as observableOf } from 'rxjs';
* @param node The incorrect import node that should be fixed * @param node The incorrect import node that should be fixed
*/ */
function handleUnaliasedImport(context: TSESLint.RuleContext<Message, unknown[]>, option: AliasImportOption, node: TSESTree.ImportSpecifier): void { function handleUnaliasedImport(context: TSESLint.RuleContext<Message, unknown[]>, option: AliasImportOption, node: TSESTree.ImportSpecifier): void {
const hasAliasedImport: boolean = (node.parent as TSESTree.ImportDeclaration).specifiers.find((specifier: TSESTree.ImportClause) => specifier.local.name === option.local && specifier.type === AST_NODE_TYPES.ImportSpecifier && (specifier as TSESTree.ImportSpecifier).imported.name === option.imported) !== undefined; const hasCorrectAliasedImport: boolean = (node.parent as TSESTree.ImportDeclaration).specifiers.find((specifier: TSESTree.ImportClause) => specifier.local.name === option.local && specifier.type === AST_NODE_TYPES.ImportSpecifier && (specifier as TSESTree.ImportSpecifier).imported.name === option.imported) !== undefined;
if (option.imported === option.local) {
if (hasCorrectAliasedImport) {
context.report({
messageId: Message.MULTIPLE_ALIASES,
data: { local: option.local },
node: node,
fix(fixer: TSESLint.RuleFixer) {
const fixes: TSESLint.RuleFix[] = [];
if (hasAliasedImport) { const commaAfter = context.sourceCode.getTokenAfter(node, {
filter: (token: TSESTree.Token) => token.value === ',',
});
if (commaAfter) {
fixes.push(fixer.removeRange([node.range[0], commaAfter.range[1]]));
} else {
fixes.push(fixer.remove(node));
}
fixes.push(...retrieveUsageReplacementFixes(context, fixer, node, option.local));
return fixes;
},
});
} else {
context.report({
messageId: Message.UNNECESSARY_ALIAS,
data: { local: option.local },
node: node,
fix(fixer: TSESLint.RuleFixer) {
const fixes: TSESLint.RuleFix[] = [];
fixes.push(fixer.replaceText(node, option.imported));
fixes.push(...retrieveUsageReplacementFixes(context, fixer, node, option.local));
return fixes;
},
});
}
} else {
if (hasCorrectAliasedImport) {
context.report({ context.report({
messageId: Message.MULTIPLE_ALIASES, messageId: Message.MULTIPLE_ALIASES,
data: { local: option.local }, data: { local: option.local },
@@ -170,7 +262,7 @@ function handleUnaliasedImport(context: TSESLint.RuleContext<Message, unknown[]>
}); });
} else if (node.local.name === node.imported.name) { } else if (node.local.name === node.imported.name) {
context.report({ context.report({
messageId: Message.NO_ALIAS, messageId: Message.MISSING_ALIAS,
node: node, node: node,
fix(fixer: TSESLint.RuleFixer) { fix(fixer: TSESLint.RuleFixer) {
const fixes: TSESLint.RuleFix[] = []; const fixes: TSESLint.RuleFix[] = [];
@@ -196,6 +288,7 @@ function handleUnaliasedImport(context: TSESLint.RuleContext<Message, unknown[]>
}, },
}); });
} }
}
} }
/** /**

View File

@@ -24,6 +24,13 @@ Filename: `<%- test.filename %>`
```<%- plugin.language.toLowerCase() %> ```<%- plugin.language.toLowerCase() %>
<%- test.code.trim() %> <%- test.code.trim() %>
``` ```
<% if (test?.options?.length > 0) { %>
With options:
```json
<%- JSON.stringify(test.options[0], null, 2) %>
```
<% }%>
<% }) %> <% }) %>
<% } %> <% } %>
@@ -36,6 +43,15 @@ Filename: `<%- test.filename %>`
<% } %> <% } %>
```<%- plugin.language.toLowerCase() %> ```<%- plugin.language.toLowerCase() %>
<%- test.code.trim() %> <%- test.code.trim() %>
<% if (test?.options?.length > 0) { %>
With options:
```json
<%- JSON.stringify(test.options[0], null, 2) %>
```
<% }%>
``` ```
Will produce the following error(s): Will produce the following error(s):
``` ```