Skip to content

Commit 282c5aa

Browse files
committed
Fixed bug with checking whether module is alias
It was only checking if the module name started with "alias/", which missed any aliases that were imported without a slash, like `from foo import 'myalias'`.
1 parent a328456 commit 282c5aa

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

js-isort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const getModuleType = (node, aliases) => {
8181
}
8282

8383
const isAlias = aliases.some((alias) => {
84-
return module.startsWith(`${alias}/`);
84+
return module === alias || module.startsWith(`${alias}/`);
8585
});
8686

8787
if (isAlias) {

js-isort.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,44 @@ import { aaa, bbb, ccc, ddd, eee, fff, ggg, hhh, iii, jjj, kkk } from "h";
645645
expect(isort(content)).toEqual(result);
646646
});
647647

648+
it(`works on aliases of different import styles`, () => {
649+
const aliases = [`actions`, `components`, `styles`];
650+
651+
const content = `
652+
import Button from 'components/Button';
653+
import actions from 'actions';
654+
import 'styles';
655+
`.trim();
656+
657+
const result = `
658+
import 'styles';
659+
660+
import actions from 'actions';
661+
import Button from 'components/Button';
662+
`.trim();
663+
664+
expect(isort(content, aliases)).toEqual(result);
665+
});
666+
667+
it(`does not match alias if other module starts with same name`, () => {
668+
const aliases = [`components`, `styles`];
669+
670+
const content = `
671+
import Button from 'components/Button';
672+
import styles from 'styles';
673+
import stylesParser from 'styles-parser';
674+
`.trim();
675+
676+
const result = `
677+
import stylesParser from 'styles-parser';
678+
679+
import Button from 'components/Button';
680+
import styles from 'styles';
681+
`.trim();
682+
683+
expect(isort(content, aliases)).toEqual(result);
684+
});
685+
648686
it(`does not add a newline after last group if it doesn't need to`, () => {
649687
const content = `
650688
import React from 'react';

0 commit comments

Comments
 (0)