Skip to content

Commit

Permalink
fix: allow internal imports starting with a hash character
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Sep 12, 2023
1 parent 8020ba7 commit f35deef
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 13 deletions.
13 changes: 6 additions & 7 deletions rules/sort-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groups: [],
})

let tsPaths: string[] = []

let source = context.getSourceCode()

let nodes: SortingNode[] = []
Expand Down Expand Up @@ -194,11 +192,12 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
let { getGroup, defineGroup, setCustomGroups } = useGroups(options.groups)

let isInternal = (nodeElement: TSESTree.ImportDeclaration) =>
(options['internal-pattern'].length &&
options['internal-pattern'].some(pattern =>
minimatch(nodeElement.source.value, pattern),
)) ||
tsPaths.some(pattern => minimatch(nodeElement.source.value, pattern))
options['internal-pattern'].length &&
options['internal-pattern'].some(pattern =>
minimatch(nodeElement.source.value, pattern, {
nocomment: true,
}),
)

let isCoreModule = (value: string) =>
builtinModules.includes(
Expand Down
4 changes: 3 additions & 1 deletion rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

if (
!options['ignore-pattern'].some(pattern =>
minimatch(node.id.name, pattern),
minimatch(node.id.name, pattern, {
nocomment: true,
}),
)
) {
let source = context.getSourceCode()
Expand Down
320 changes: 319 additions & 1 deletion test/sort-imports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,112 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}(${type}): allows to hash symbol in internal pattern`,
rule,
{
valid: [
{
code: dedent`
import type { Characters } from 'skip-and-loafer'
import { events } from 'skip-and-loafer'
import type { Student } from '#school'
import Satonosuke from '#pets'
import { MitsumiIwakura, SousukeShima } from '#school'
import { Nao } from '../family'
`,
options: [
{
...options,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['#**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
},
],
invalid: [
{
code: dedent`
import type { Student } from '#school'
import type { Characters } from 'skip-and-loafer'
import Satonosuke from '#pets'
import { events } from 'skip-and-loafer'
import { MitsumiIwakura, SousukeShima } from '#school'
import { Nao } from '../family'
`,
output: dedent`
import type { Characters } from 'skip-and-loafer'
import { events } from 'skip-and-loafer'
import type { Student } from '#school'
import Satonosuke from '#pets'
import { MitsumiIwakura, SousukeShima } from '#school'
import { Nao } from '../family'
`,
options: [
{
...options,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['#**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
errors: [
{
messageId: 'unexpectedImportsOrder',
data: {
left: '#school',
right: 'skip-and-loafer',
},
},
{
messageId: 'unexpectedImportsOrder',
data: {
left: '#pets',
right: 'skip-and-loafer',
},
},
{
messageId: 'missedSpacingBetweenImports',
data: {
left: 'skip-and-loafer',
right: '#school',
},
},
],
},
],
},
)
})

describe(`${RULE_NAME}: sorting by natural order`, () => {
Expand Down Expand Up @@ -2079,6 +2185,112 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}(${type}): allows to hash symbol in internal pattern`,
rule,
{
valid: [
{
code: dedent`
import type { Characters } from 'skip-and-loafer'
import { events } from 'skip-and-loafer'
import type { Student } from '#school'
import Satonosuke from '#pets'
import { MitsumiIwakura, SousukeShima } from '#school'
import { Nao } from '../family'
`,
options: [
{
...options,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['#**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
},
],
invalid: [
{
code: dedent`
import type { Student } from '#school'
import type { Characters } from 'skip-and-loafer'
import Satonosuke from '#pets'
import { events } from 'skip-and-loafer'
import { MitsumiIwakura, SousukeShima } from '#school'
import { Nao } from '../family'
`,
output: dedent`
import type { Characters } from 'skip-and-loafer'
import { events } from 'skip-and-loafer'
import type { Student } from '#school'
import Satonosuke from '#pets'
import { MitsumiIwakura, SousukeShima } from '#school'
import { Nao } from '../family'
`,
options: [
{
...options,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['#**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
errors: [
{
messageId: 'unexpectedImportsOrder',
data: {
left: '#school',
right: 'skip-and-loafer',
},
},
{
messageId: 'unexpectedImportsOrder',
data: {
left: '#pets',
right: 'skip-and-loafer',
},
},
{
messageId: 'missedSpacingBetweenImports',
data: {
left: 'skip-and-loafer',
right: '#school',
},
},
],
},
],
},
)
})

describe(`${RULE_NAME}: sorting by line length`, () => {
Expand Down Expand Up @@ -3159,6 +3371,112 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}(${type}): allows to hash symbol in internal pattern`,
rule,
{
valid: [
{
code: dedent`
import type { Characters } from 'skip-and-loafer'
import { events } from 'skip-and-loafer'
import type { Student } from '#school'
import { MitsumiIwakura, SousukeShima } from '#school'
import Satonosuke from '#pets'
import { Nao } from '../family'
`,
options: [
{
...options,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['#**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
},
],
invalid: [
{
code: dedent`
import type { Student } from '#school'
import type { Characters } from 'skip-and-loafer'
import Satonosuke from '#pets'
import { events } from 'skip-and-loafer'
import { MitsumiIwakura, SousukeShima } from '#school'
import { Nao } from '../family'
`,
output: dedent`
import type { Characters } from 'skip-and-loafer'
import { events } from 'skip-and-loafer'
import type { Student } from '#school'
import { MitsumiIwakura, SousukeShima } from '#school'
import Satonosuke from '#pets'
import { Nao } from '../family'
`,
options: [
{
...options,
'newlines-between': NewlinesBetweenValue.always,
'internal-pattern': ['#**'],
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
},
],
errors: [
{
messageId: 'unexpectedImportsOrder',
data: {
left: '#school',
right: 'skip-and-loafer',
},
},
{
messageId: 'unexpectedImportsOrder',
data: {
left: '#pets',
right: 'skip-and-loafer',
},
},
{
messageId: 'missedSpacingBetweenImports',
data: {
left: 'skip-and-loafer',
right: '#school',
},
},
],
},
],
},
)
})

describe(`${RULE_NAME}: misc`, () => {
Expand Down Expand Up @@ -3241,7 +3559,7 @@ describe(RULE_NAME, () => {
{
code: dedent`
import { writeFile } from 'node:fs/promises'
import { useEffect } from 'react'
`,
options: [
Expand Down
Loading

0 comments on commit f35deef

Please sign in to comment.