Skip to content

Commit

Permalink
feat: Add caseSensitive and natural options to imports and `exp…
Browse files Browse the repository at this point in the history
…orts` rules

Fixes #46
  • Loading branch information
mskelton committed Apr 14, 2023
1 parent 7fc1db1 commit 8fb2928
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 49 deletions.
139 changes: 128 additions & 11 deletions src/__tests__/exports.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dedent from "dedent"
import { vi } from "vitest"
import { createRuleTester } from "../test-utils.js"

Expand Down Expand Up @@ -32,9 +33,51 @@ ruleTester.run("sort/exports", rule, {
export { c } from "./c"
`.trim(),

// Case sensitivity and natural sort
{
code: dedent`
export { a1 } from 'a1'
export { a12 } from 'a12'
export { a2 } from 'a2'
export { b } from 'b'
export { C } from 'C'
`,
options: [{ caseSensitive: false, natural: false }],
},
{
code: dedent`
export { C } from 'C'
export { a1 } from 'a1'
export { a12 } from 'a12'
export { a2 } from 'a2'
export { b } from 'b'
`,
options: [{ caseSensitive: true, natural: false }],
},
{
code: dedent`
export { a1 } from 'a1'
export { a2 } from 'a2'
export { a12 } from 'a12'
export { b } from 'b'
export { C } from 'C'
`,
options: [{ caseSensitive: false, natural: true }],
},
{
code: dedent`
export { C } from 'C'
export { a1 } from 'a1'
export { a2 } from 'a2'
export { a12 } from 'a12'
export { b } from 'b'
`,
options: [{ caseSensitive: true, natural: true }],
},

// Sort groups
{
code: `
code: dedent`
const mark = ''
export default React
Expand All @@ -61,11 +104,11 @@ ruleTester.run("sort/exports", rule, {
],
invalid: [
{
code: `
code: dedent`
export { b } from 'b'
export { a } from 'a'
`,
output: `
output: dedent`
export { a } from 'a'
export { b } from 'b'
`,
Expand All @@ -74,7 +117,7 @@ ruleTester.run("sort/exports", rule, {

// All types of exports
{
code: `
code: dedent`
const mark = ''
export { c } from 'c'
Expand All @@ -83,7 +126,7 @@ ruleTester.run("sort/exports", rule, {
export { mark }
export { a } from 'a'
`,
output: `
output: dedent`
const mark = ''
export default React
Expand All @@ -95,17 +138,91 @@ ruleTester.run("sort/exports", rule, {
errors: [{ messageId: "unsorted" }],
},

// Case sensitivity and natural sort
{
code: dedent`
export { a2 } from 'a2'
export { C } from 'C'
export { a1 } from 'a1'
export { b } from 'b'
export { a12 } from 'a12'
`,
output: dedent`
export { a1 } from 'a1'
export { a12 } from 'a12'
export { a2 } from 'a2'
export { b } from 'b'
export { C } from 'C'
`,
options: [{ caseSensitive: false, natural: false }],
errors: [{ messageId: "unsorted" }],
},
{
code: dedent`
export { a2 } from 'a2'
export { b } from 'b'
export { a1 } from 'a1'
export { a12 } from 'a12'
export { C } from 'C'
`,
output: dedent`
export { C } from 'C'
export { a1 } from 'a1'
export { a12 } from 'a12'
export { a2 } from 'a2'
export { b } from 'b'
`,
options: [{ caseSensitive: true, natural: false }],
errors: [{ messageId: "unsorted" }],
},
{
code: dedent`
export { a12 } from 'a12'
export { C } from 'C'
export { b } from 'b'
export { a2 } from 'a2'
export { a1 } from 'a1'
`,
output: dedent`
export { a1 } from 'a1'
export { a2 } from 'a2'
export { a12 } from 'a12'
export { b } from 'b'
export { C } from 'C'
`,
options: [{ caseSensitive: false, natural: true }],
errors: [{ messageId: "unsorted" }],
},
{
code: dedent`
export { a2 } from 'a2'
export { b } from 'b'
export { a1 } from 'a1'
export { C } from 'C'
export { a12 } from 'a12'
`,
output: dedent`
export { C } from 'C'
export { a1 } from 'a1'
export { a2 } from 'a2'
export { a12 } from 'a12'
export { b } from 'b'
`,
options: [{ caseSensitive: true, natural: true }],
errors: [{ messageId: "unsorted" }],
},

// Comments
{
code: `
code: dedent`
// c
export { c } from "c"
// b
export { b } from "b"
// a
export { a } from "a"
`.trim(),
output: `
output: dedent`
// a
export { a } from "a"
// b
Expand All @@ -118,7 +235,7 @@ ruleTester.run("sort/exports", rule, {

// Sort groups
{
code: `
code: dedent`
const mark = ''
export { depB } from 'dependency-b'
Expand All @@ -130,7 +247,7 @@ ruleTester.run("sort/exports", rule, {
export { depA } from 'dependency-a'
export { b } from 'b'
`.trim(),
output: `
output: dedent`
const mark = ''
export { relA } from './a'
Expand All @@ -154,7 +271,7 @@ ruleTester.run("sort/exports", rule, {
errors: [{ messageId: "unsorted" }],
},
{
code: `
code: dedent`
const mark = ''
export { depB } from 'dependency-b'
Expand All @@ -166,7 +283,7 @@ ruleTester.run("sort/exports", rule, {
export { depA } from 'dependency-a'
export { b } from 'b'
`.trim(),
output: `
output: dedent`
const mark = ''
export default React
Expand Down
116 changes: 116 additions & 0 deletions src/__tests__/imports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,48 @@ ruleTester.run("sort/imports", rule, {
`,
},

// Case sensitivity and natural sort
{
code: `
import { a1 } from 'a1'
import { a12 } from 'a12'
import { a2 } from 'a2'
import { b } from 'b'
import { C } from 'C'
`,
options: [{ caseSensitive: false, natural: false }],
},
{
code: `
import { C } from 'C'
import { a1 } from 'a1'
import { a12 } from 'a12'
import { a2 } from 'a2'
import { b } from 'b'
`,
options: [{ caseSensitive: true, natural: false }],
},
{
code: `
import { a1 } from 'a1'
import { a2 } from 'a2'
import { a12 } from 'a12'
import { b } from 'b'
import { C } from 'C'
`,
options: [{ caseSensitive: false, natural: true }],
},
{
code: `
import { C } from 'C'
import { a1 } from 'a1'
import { a2 } from 'a2'
import { a12 } from 'a12'
import { b } from 'b'
`,
options: [{ caseSensitive: true, natural: true }],
},

// Sort groups
{
name: "Sort groups",
Expand Down Expand Up @@ -162,6 +204,80 @@ ruleTester.run("sort/imports", rule, {
errors: [{ messageId: "unsorted" }],
},

// Case sensitivity and natural sort
{
code: dedent`
import { a2 } from 'a2'
import { C } from 'C'
import { a1 } from 'a1'
import { b } from 'b'
import { a12 } from 'a12'
`,
output: dedent`
import { a1 } from 'a1'
import { a12 } from 'a12'
import { a2 } from 'a2'
import { b } from 'b'
import { C } from 'C'
`,
options: [{ caseSensitive: false, natural: false }],
errors: [{ messageId: "unsorted" }],
},
{
code: dedent`
import { a2 } from 'a2'
import { b } from 'b'
import { a1 } from 'a1'
import { a12 } from 'a12'
import { C } from 'C'
`,
output: dedent`
import { C } from 'C'
import { a1 } from 'a1'
import { a12 } from 'a12'
import { a2 } from 'a2'
import { b } from 'b'
`,
options: [{ caseSensitive: true, natural: false }],
errors: [{ messageId: "unsorted" }],
},
{
code: dedent`
import { a12 } from 'a12'
import { C } from 'C'
import { b } from 'b'
import { a2 } from 'a2'
import { a1 } from 'a1'
`,
output: dedent`
import { a1 } from 'a1'
import { a2 } from 'a2'
import { a12 } from 'a12'
import { b } from 'b'
import { C } from 'C'
`,
options: [{ caseSensitive: false, natural: true }],
errors: [{ messageId: "unsorted" }],
},
{
code: dedent`
import { a2 } from 'a2'
import { b } from 'b'
import { a1 } from 'a1'
import { C } from 'C'
import { a12 } from 'a12'
`,
output: dedent`
import { C } from 'C'
import { a1 } from 'a1'
import { a2 } from 'a2'
import { a12 } from 'a12'
import { b } from 'b'
`,
options: [{ caseSensitive: true, natural: true }],
errors: [{ messageId: "unsorted" }],
},

// Sort groups
{
name: "Sort groups - 1",
Expand Down
5 changes: 1 addition & 4 deletions src/rules/destructuring-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { docsURL, filterNodes, getName, getSorter, report } from "../utils.js"
export default {
create(context) {
const options = context.options[0]
const sorter = getSorter({
caseSensitive: options?.caseSensitive,
natural: options?.natural,
})
const sorter = getSorter(options)

return {
ObjectPattern(pattern) {
Expand Down
5 changes: 1 addition & 4 deletions src/rules/export-members.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { docsURL, getSorter, report } from "../utils.js"
export default {
create(context) {
const options = context.options[0]
const sorter = getSorter({
caseSensitive: options?.caseSensitive,
natural: options?.natural,
})
const sorter = getSorter(options)

return {
ExportNamedDeclaration({ specifiers: nodes }) {
Expand Down
Loading

0 comments on commit 8fb2928

Please sign in to comment.