Skip to content
This repository has been archived by the owner on Oct 6, 2020. It is now read-only.

Fix the fixer :) for unsorted imports using as syntax #43

Merged
merged 1 commit into from
May 13, 2019
Merged
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
14 changes: 13 additions & 1 deletion src/rules/importGroupOrderRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type NormalizedConfiguration = {

function formatImport(text: ImportType["i"]["text"], moduleSpecifier: string) {
const namedImportsRegex = /{(.*)}/;
const asImportRegex = /as *(\w*)/;
// let's omit the code style (new lines) entirely, it's prettier's job
let inlinedText = text.replace(/\n/g, "");

Expand All @@ -70,7 +71,18 @@ function formatImport(text: ImportType["i"]["text"], moduleSpecifier: string) {
.split(",")
.map(a => a.trim())
.filter(a => a)
.sort()
.map(imp => {
const matches = imp.match(asImportRegex);
if (matches && matches[1]) return [matches[1], imp];

return [imp];
})
.sort(([a], [b]) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
})
.map(([sortBase, imp]) => (imp ? imp : sortBase))
.join(", ");

// with syntax * as there might be moduleSpecifier ommited, so we need to reconstruct it.
Expand Down
7 changes: 7 additions & 0 deletions test/rules/import-group-order/test.10.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as React from 'react';

import { RandomGroupId, RandomGroup as TRandomGroup } from 'stores/RandomStore';

import { createMagic } from 'constants/magic';

console.log('ok');
9 changes: 9 additions & 0 deletions test/rules/import-group-order/test.10.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as React from 'react';

import { RandomGroup as TRandomGroup, RandomGroupId } from 'stores/RandomStore';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Named imports are not sorted in alphabetical order]

import { createMagic } from 'constants/magic';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import convention has been violated. This is auto-fixable.]
Copy link
Contributor

Choose a reason for hiding this comment

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

What's wrong with this one?

Copy link
Contributor Author

@jukben jukben May 13, 2019

Choose a reason for hiding this comment

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

This is actually nasty hack we are using. This line is there every time the issue is autofixable – that's because the fixer is connected to this line and the fixer works that it rewrite all the imports at once, not issue per issue. * shy boi *

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see! Okay makes sense, thanks 🎂


console.log('ok');