8000 fix(eslint-plugin): [sort-ngmodule-metadata-arrays]: properly handle multiple fixes at once by rafaelss95 · Pull Request #695 · angular-eslint/angular-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(eslint-plugin): [sort-ngmodule-metadata-arrays]: properly handle multiple fixes at once #695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions packages/eslint-plugin/docs/rules/sort-ngmodule-metadata-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Ensures ASC alphabetical order for `NgModule` metadata arrays for easy visual sc
- Category: Best Practices
- 🔧 Supports autofix (`--fix`)

- 💡 Provides suggestions on how to fix issues (https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions)

<br>

## Rule Options
Expand Down Expand Up @@ -263,6 +265,211 @@ class Test {}

#### ❌ Invalid Code

```ts
@NgModule({
providers: [
// @ts-ignore
AProvider,
// @ts-expect-error
{
useClass: MyProvider,
// eslint-disable-next-line sort-keys
provide: 'myprovider',
},
cProvider,
bProvider, // TODO: This provider should be removed soon.
~~~~~~~~~
/* CommentAfter */ DProvider,
]
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/sort-ngmodule-metadata-arrays": [
"error"
]
}
}
```

<br>

#### ❌ Invalid Code

```ts
@NgModule({
bootstrap,
declarations: declarations,
providers: providers(),
schemas: [],
[imports]: [
aModule,
bModule,
DModule,
cModule,
~~~~~~~
],
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/sort-ngmodule-metadata-arrays": [
"error"
]
}
}
```

<br>

#### ❌ Invalid Code

```ts
@NgModule({
bootstrap,
declarations: declarations,
providers: providers(),
schemas: [],
imports: [
Module1,
[...commonModules, Module4, Module0],
~~~~~~~
],
})
class Test {}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/sort-ngmodule-metadata-arrays": [
"error"
]
}
}
```

<br>

#### ❌ Invalid Code

```ts
@NgModule({
imports: [
AppRoutingModule,
BrowserModule,
DeprecatedModule as unknown as Type<unknown>,
FlexLayoutModule,
HttpClientModule,
new Array([TestModule]),
MatListModule,
...(shouldLoadModules ? [ModuleA, ModuleB] : []),
MatMenuModule,
StoreModule.forRoot({}),
MatSidenavModule,
MatToolbarModule,
...[sharedDeclarations],
BrowserAnimationsModule,
~~~~~~~~~~~~~~~~~~~~~~~
Test!,
],
declarations: [
AutoHeightDirective,
NgxColumnComponent,
NgxOptionsComponent,
TableBuilderComponent,
TableTbodyComponent,
TableTheadComponent,
TableCellComponent,
~~~~~~~~~~~~~~~~~~
TemplateBodyTdDirective,
[B, A, C],
~
TemplateHeadThDirective,
ObserverViewDirective,
NgxContextMenuComponent,
NgxContextMenuItemComponent,
NgxContextMenuDividerComponent,
NgxMenuContentComponent,
NgxEmptyComponent,
NgxHeaderComponent,
NgxFooterComponent,
NgxFilterViewerComponent,
NgxFilterComponent,
NgxFilterDirective,
DragIconComponent,
NgxSourceNullComponent,
DisableRowPipe,
TableSelectedItemsPipe,
MapToTableEntriesPipe,
VirtualForDirective,
GetFreeSizePipe,
GetClientHeightPipe
],
providers: [
{provide: 'TOKEN', useFactory: useToken},
WebWorkerThreadService,
],
})
class TableBuilderModule {
static forRoot(): ModuleWithProviders<TableBuilderModule> {
return { ngModule: TableBuilderModule, providers: [] };
}
}
```

<br>

---

<br>

#### Default Config

```json
{
"rules": {
"@angular-eslint/sort-ngmodule-metadata-arrays": [
"error"
]
}
}
```

<br>

#### ❌ Invalid Code

```ts
@NgModule({
bootstrap,
Expand Down
99 changes: 83 additions & 16 deletions packages/eslint-plugin/src/rules/sort-ngmodule-metadata-arrays.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Selectors } from '@angular-eslint/utils';
import type { TSESTree } from '@typescript-eslint/experimental-utils';
import { ASTUtils, partition, Selectors } from '@angular-eslint/utils';
import type { TSESLint, TSESTree } from '@typescript-eslint/experimental-utils';
import { ASTUtils as TSESLintASTUtils } from '@typescript-eslint/experimental-utils';
import { createESLintRule } from '../utils/create-eslint-rule';

type Options = [];
export type MessageIds = 'sortNgmoduleMetadataArrays';
export type MessageIds = 'sortNgmoduleMetadataArrays' | 'suggestFix';
export const RULE_NAME = 'sort-ngmodule-metadata-arrays';

export default createESLintRule<Options, MessageIds>({
Expand All @@ -16,39 +16,106 @@ export default createESLintRule<Options, MessageIds>({
'Ensures ASC alphabetical order for `NgModule` metadata arrays for easy visual scanning',
category: 'Best Practices',
recommended: false,
suggestion: true,
},
fixable: 'code',
schema: [],
messages: {
sortNgmoduleMetadataArrays:
'`NgModule` metadata arrays should be sorted in ASC alphabetical order',
suggestFix: 'Sort members (WARNING! comments will probably be messed up)',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, suggest a better messageId/description if that's the case.

},
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode();

return {
[`${Selectors.MODULE_CLASS_DECORATOR} Property ArrayExpression`]({
elements,
}: TSESTree.ArrayExpression) {
const unorderedNodes = elements
.filter(TSESLintASTUtils.isIdentifier)
.map((current, index, list) => [current, list[index + 1]])
.find(([current, next]) => {
return next && current.name.localeCompare(next.name) === 1;
});
const [expressions, identifiers] = partition(
elements,
TSESLintASTUtils.isIdentifier,
);
const firstUnorderedNode = getFirstUnderedNode(identifiers);

if (!unorderedNodes) return;
if (!firstUnorderedNode) {
return;
}

const [unorderedNode, nextNode] = unorderedNodes;
const hasComments = elements.some((element) =>
ASTUtils.hasComments(sourceCode, element),
);
const fix: TSESLint.ReportFixFunction = (fixer) =>
getFixes(sourceCode, fixer, elements, expressions, identifiers);
context.report({
node: nextNode,
node: firstUnorderedNode,
messageId: 'sortNgmoduleMetadataArrays',
fix: (fixer) => [
fixer.replaceText(unorderedNode, nextNode.name),
fixer.replaceText(nextNode, unorderedNode.name),
],
...(hasComments
? { suggest: [{ messageId: 'suggestFix', fix }] }
: { fix }),
Comment on lines +55 to +57
Copy link
Member Author
@rafaelss95 rafaelss95 Sep 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I even tried to use fix, even if there were comments, but besides being very costly to maintain such an implementation, it's also very prone to failure. 😞

Example of hard cases can be found here, for example.

Copy link
Member Author
@rafaelss95 rafaelss95 Sep 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing that came to mind now is, since this is something we have control over and wouldn't cause any compilation errors if applied, we could add an option called something like alwaysFixEvenWithComments/forceFix (I'm really bad at naming) to the rule to ignore comments and always fix the reports. It would be false by default, of course.

Let me know what you think, so I can send another PR for this if that's the case.

});
},
};
},
});

function getFirstUnderedNode(identifiers: readonly TSESTree.Identifier[]) {
return identifiers.find((identifier, index, array) => {
const previousElement = array[index - 1];
return (
previousElement &&
previousElement.name.localeCompare(identifier.name) === 1
);
});
}

function getFixes(
sourceCode: Readonly<TSESLint.SourceCode>,
fixer: TSESLint.RuleFixer,
elements: readonly TSESTree.Expression[],
expressions: readonly TSESTree.Expression[],
identifiers: readonly TSESTree.Identifier[],
) {
const sortedIdentifiers = [...identifiers].sort((one, other) =>
one.name.localeCompare(other.name),
);
const sortedExpressions = [...sortedIdentifiers, ...expressions];
const text = sortedExpressions.reduce(toText(sourceCode, elements), '');
return fixer.replaceTextRange(getRangeFromFirstToLast(elements), text);
}

function toText(
sourceCode: Readonly<TSESLint.SourceCode>,
elements: readonly TSESTree.Expression[],
) {
return (
accumulator: string,
expression: TSESTree.Expression,
index: number,
) => {
const isLastElement = index === elements.length - 1;
const textAfterExpression = isLastElement
? ''
: sourceCode
.getText()
.slice(...getRangeBetweenCurrentAndNext(elements, index));
return `${accumulator}${sourceCode.getText(
expression,
)}${textAfterExpression}`;
};
}

function getRangeBetweenCurrentAndNext(
elements: readonly TSESTree.Expression[],
index: number,
): TSESTree.Range {
return [elements[index].range[1], elements[index + 1].range[0]];
}

function getRangeFromFirstToLast(
elements: readonly TSESTree.Expression[],
): TSESTree.Range {
return [elements[0].range[0], elements[elements.length - 1].range[1]];
}
Loading
0