Skip to content

Commit

Permalink
feat: Add option to disable newlines within group sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Mar 3, 2022
1 parent b0e6f7f commit b114b69
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ function mutateRanksToAlphabetize(imported, alphabetizeOptions) {

function computePathRank(ranks, pathGroups, path, maxPosition) {
for (let i = 0, l = pathGroups.length; i < l; i++) {
const { pattern, patternOptions, group, position = 1 } = pathGroups[i];
const { pattern, patternOptions, group, position = 1, insertNewline } = pathGroups[i];
if (!insertNewline) {
return undefined;
}

if (minimatch(path, pattern, patternOptions || { nocomment: true })) {
return ranks[group] + (position / maxPosition);
}
Expand Down Expand Up @@ -547,7 +551,12 @@ module.exports = {
type: 'string',
enum: ['after', 'before'],
},
insertNewline: {
type: 'boolean',
default: true,
},
},
additionalProperties: false,
required: ['pattern', 'group'],
},
},
Expand Down
74 changes: 70 additions & 4 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,13 @@ ruleTester.run('order', rule, {
import express from 'express';
import service from '@/api/service';
import fooParent from '../foo';
import fooSibling from './foo';
import index from './';
import internalDoesNotExistSoIsUnknown from '@/does-not-exist';
`,
options: [
Expand All @@ -884,6 +884,45 @@ ruleTester.run('order', rule, {
},
},
}),
test({
code: `
import a from "a"
import A from "A"
`,
options: [{
alphabetize: { caseInsensitive: true },
pathGroups: [
{
pattern: '[a-z]*',
group: 'external',
position: 'before',
insertNewline: true,
},
],
pathGroupsExcludedImportTypes: [],
'newlines-between': 'always',
}],
}),
test({
code: `
import a from "a"
import A from "A"
`,
options: [{
alphabetize: { caseInsensitive: true },
pathGroups: [
{
pattern: '[a-z]*',
group: 'external',
position: 'before',
insertNewline: false,
},
],
pathGroupsExcludedImportTypes: [],
'newlines-between': 'always',
}],
}),
],
invalid: [
// builtin before external module (require)
Expand Down Expand Up @@ -2314,6 +2353,33 @@ ruleTester.run('order', rule, {
}],
}),
],
test({
code: `
import a from "a"
import A from "A"
`,
output: `
import a from "a"
import A from "A"
`,
options: [{
alphabetize: { caseInsensitive: true },
pathGroups: [
{
pattern: '[a-z]*',
group: 'external',
position: 'before',
insertNewline: true,
},
],
pathGroupsExcludedImportTypes: [],
'newlines-between': 'always',
}],
errors: [{
message: 'There should be at least one empty line between import groups',
}],
}),
].filter((t) => !!t),
});

Expand Down

0 comments on commit b114b69

Please sign in to comment.