diff --git a/src/rules/importGroupOrderRule.ts b/src/rules/importGroupOrderRule.ts index d01d089..dd7da43 100644 --- a/src/rules/importGroupOrderRule.ts +++ b/src/rules/importGroupOrderRule.ts @@ -59,23 +59,26 @@ type NormalizedConfiguration = { function formatImport(text: ImportType["i"]["text"], moduleSpecifier: string) { const namedImportsRegex = /{(([a-zA-Z0-9]*[, ]?)*)}/; + // let's omit the code style (new lines) entirely, it's prettier's job + let inlinedText = text.replace(/\n/g, ""); - const matches = text.match(namedImportsRegex); + const matches = inlinedText.match(namedImportsRegex); if (!matches) return text; const namedImports = matches[1] .split(",") .map(a => a.trim()) + .filter(a => a) .sort() .join(", "); // with syntax * as there might be moduleSpecifier ommited, so we need to reconstruct it. - if (!text.includes("from")) { - text += ` from '${moduleSpecifier}'`; + if (!inlinedText.includes("from")) { + inlinedText += ` from '${moduleSpecifier}'`; } - return text.replace(namedImportsRegex, `{ ${namedImports} }`); + return inlinedText.replace(namedImportsRegex, `{ ${namedImports} }`); } function isAbsolute(text: string) { diff --git a/test/rules/import-group-order/test.1.tsx.fix b/test/rules/import-group-order/test.1.tsx.fix index 998b1f0..1474570 100644 --- a/test/rules/import-group-order/test.1.tsx.fix +++ b/test/rules/import-group-order/test.1.tsx.fix @@ -1,11 +1,6 @@ import a from 'libs/flux/r'; import { b } from 'libs/flux/r'; -import { - c, - d, - f, - g, -} from 'modules/views/libs/v'; +import { c, d, f, g } from 'modules/views/libs/v'; import { h } from 'stores/u'; diff --git a/test/rules/import-group-order/test.4.tsx.fix b/test/rules/import-group-order/test.4.tsx.fix index d819993..9e15bbd 100644 --- a/test/rules/import-group-order/test.4.tsx.fix +++ b/test/rules/import-group-order/test.4.tsx.fix @@ -1,11 +1,6 @@ import a from 'libs/flux/r'; import { b } from 'libs/flux/r'; -import { - c, - d, - f, - g, -} from 'modules/views/libs/v'; +import { c, d, f, g } from 'modules/views/libs/v'; import { h } from 'stores/u'; diff --git a/test/rules/import-group-order/test.6.tsx.fix b/test/rules/import-group-order/test.6.tsx.fix new file mode 100644 index 0000000..614a9a5 --- /dev/null +++ b/test/rules/import-group-order/test.6.tsx.fix @@ -0,0 +1,3 @@ +import { SpaceMembership, SubscriptionPlan, SubscriptionPlanInterval, User } from 'stores/types'; + +console.log('test') diff --git a/test/rules/import-group-order/test.6.tsx.lint b/test/rules/import-group-order/test.6.tsx.lint new file mode 100644 index 0000000..84700bc --- /dev/null +++ b/test/rules/import-group-order/test.6.tsx.lint @@ -0,0 +1,5 @@ +import { SubscriptionPlan, SubscriptionPlanInterval, SpaceMembership, User } from 'stores/types'; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import convention has been violated. This is auto-fixable.] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Named imports are not sorted in alphabetical order] + +console.log('test') diff --git a/test/rules/import-group-order/test.7.tsx.fix b/test/rules/import-group-order/test.7.tsx.fix new file mode 100644 index 0000000..4c37a4d --- /dev/null +++ b/test/rules/import-group-order/test.7.tsx.fix @@ -0,0 +1,15 @@ +import React from 'react'; +import { Space, Subscription, SubscriptionSwag, User } from 'something'; + +import { action } from '@storybook/addon-actions'; + +import B from './B.react'; + +import { withSmartKnobs } from '@productboard/storybook-addon-smart-knobs'; +import { storiesOf } from '@storybook/react'; + +storiesOf('B', module) + .addDecorator(withSmartKnobs) + .add('B', () => ( + + )); diff --git a/test/rules/import-group-order/test.7.tsx.lint b/test/rules/import-group-order/test.7.tsx.lint new file mode 100644 index 0000000..5a2bddc --- /dev/null +++ b/test/rules/import-group-order/test.7.tsx.lint @@ -0,0 +1,29 @@ +import React from 'react'; + +import { action } from '@storybook/addon-actions'; +import { storiesOf } from '@storybook/react'; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Insert line above.] + +import { +~~~~~~~~ + Subscription, +~~~~~~~~~~~~~~~ + SubscriptionSwag, +~~~~~~~~~~~~~~~~~~~ + Space, +~~~~~~~~ + User, +~~~~~~~ +} from 'something'; +~~~~~~~~~~~~~~~~~~~ [Named imports are not sorted in alphabetical order] + +import B from './B.react'; +import { withSmartKnobs } from '@productboard/storybook-addon-smart-knobs'; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Import convention has been violated. This is auto-fixable.] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Insert line above.] + +storiesOf('B', module) + .addDecorator(withSmartKnobs) + .add('B', () => ( + + ));