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

Commit

Permalink
Fix the fixer :) for unsorted imports using as syntax (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jukben authored May 13, 2019
1 parent 60a3ed9 commit 6138b18
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
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.]

console.log('ok');

0 comments on commit 6138b18

Please sign in to comment.