Skip to content

Commit

Permalink
fix: add test cases for relative parent
Browse files Browse the repository at this point in the history
  • Loading branch information
lachieh committed Oct 8, 2024
1 parent 3938ac6 commit 0684ab7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export function parseTsAliases(basePath: string, paths: ts.MapLike<string[]>) {

for (const [pathWithAsterisk, replacements] of Object.entries(paths)) {
const find = new RegExp(
`^${pathWithAsterisk.replace(regexpSymbolRE, '\\$1').replace(asteriskRE, '(?!\\.\\/)([^*]+)')}$`
`^${pathWithAsterisk.replace(regexpSymbolRE, '\\$1').replace(asteriskRE, '(?!\\.{1,2}\\/)([^*]+)')}$`
)

let index = 1
Expand Down
17 changes: 13 additions & 4 deletions tests/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ describe('transform tests', () => {

it('test: transformCode (process aliases)', () => {
const aliases: Alias[] = [
{ find: /^@\/(?!\.\/)([^*]+)/, replacement: resolve(__dirname, '../$1') },
{ find: /^@\/(?!\.{1,2}\/)([^*]+)/, replacement: resolve(__dirname, '../$1') },
{
find: /^@components\/(?!\.\/)([^*]+)/,
find: /^@components\/(?!\.{1,2}\/)([^*]+)/,
replacement: resolve(__dirname, '../src/components/$1')
},
{ find: /^~\//, replacement: resolve(__dirname, '../src/') },
{ find: '$src', replacement: resolve(__dirname, '../src') },
{ find: /^(?!\.\/)([^*]+)$/, replacement: resolve(__dirname, '../src/$1') }
{ find: /^(?!\.{1,2}\/)([^*]+)/, replacement: resolve(__dirname, '../src/$1') }
]
const filePath = resolve(__dirname, '../src/index.ts')

Expand Down Expand Up @@ -138,6 +138,12 @@ describe('transform tests', () => {
content: 'import { Sample } from "./test.data";',
output: "import { Sample } from './test.data';\n"
},
{
description: 'wildcard alias at root level with relative parent import and dot in name',
filePath: './src/components/Sample/index.ts',
content: 'import { Sample } from "../test.data";',
output: "import { Sample } from '../test.data';\n"
},
{
description: 'wildcard alias at root level with relative import and dot in name',
filePath: './src/components/Sample/index.ts',
Expand Down Expand Up @@ -193,7 +199,6 @@ describe('transform tests', () => {
},
{
description: 'alias as everything, relative import',
aliases: [{ find: /^(.+)$/, replacement: resolve(__dirname, '../src/$1') }],
content: 'import { TestBase } from "test";',
output: "import { TestBase } from './test';\n"
}
Expand Down Expand Up @@ -249,6 +254,10 @@ describe('transform tests', () => {
expect(transformCode(options('import { TestBase } from "./test.path";')).content).toEqual(
"import { TestBase } from './test.path';\n"
)

expect(transformCode(options('import { TestBase } from "../test.path";')).content).toEqual(
"import { TestBase } from '../test.path';\n"
)
})

it('test: transformCode (remove pure imports)', () => {
Expand Down
14 changes: 7 additions & 7 deletions tests/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,14 @@ describe('utils tests', () => {
})
).toStrictEqual([
{
find: /^@\/(?!\.\/)([^*]+)$/,
find: /^@\/(?!\.{1,2}\/)([^*]+)$/,
replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/at/$1'))
}
])

expect(parseTsAliases('/tmp/fake/project/root', { '~/*': ['./tilde/*'] })).toStrictEqual([
{
find: /^~\/(?!\.\/)([^*]+)$/,
find: /^~\/(?!\.{1,2}\/)([^*]+)$/,
replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/tilde/$1'))
}
])
Expand All @@ -184,7 +184,7 @@ describe('utils tests', () => {
parseTsAliases('/tmp/fake/project/root', { '@/no-dot-prefix/*': ['no-dot-prefix/*'] })
).toStrictEqual([
{
find: /^@\/no-dot-prefix\/(?!\.\/)([^*]+)$/,
find: /^@\/no-dot-prefix\/(?!\.{1,2}\/)([^*]+)$/,
replacement: expect.stringMatching(
maybeWindowsPath('/tmp/fake/project/root/no-dot-prefix/$1')
)
Expand All @@ -195,7 +195,7 @@ describe('utils tests', () => {
parseTsAliases('/tmp/fake/project/root', { '@/components/*': ['./at/components/*'] })
).toStrictEqual([
{
find: /^@\/components\/(?!\.\/)([^*]+)$/,
find: /^@\/components\/(?!\.{1,2}\/)([^*]+)$/,
replacement: expect.stringMatching(
maybeWindowsPath('/tmp/fake/project/root/at/components/$1')
)
Expand All @@ -204,7 +204,7 @@ describe('utils tests', () => {

expect(parseTsAliases('/tmp/fake/project/root', { 'top/*': ['./top/*'] })).toStrictEqual([
{
find: /^top\/(?!\.\/)([^*]+)$/,
find: /^top\/(?!\.{1,2}\/)([^*]+)$/,
replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/top/$1'))
}
])
Expand All @@ -219,15 +219,15 @@ describe('utils tests', () => {
// https://github.com/qmhc/vite-plugin-dts/issues/330
expect(parseTsAliases('/tmp/fake/project/root', { '*': ['./src/*'] })).toStrictEqual([
{
find: /^(?!\.\/)([^*]+)$/,
find: /^(?!\.{1,2}\/)([^*]+)$/,
replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/src/$1'))
}
])

// https://github.com/qmhc/vite-plugin-dts/issues/290#issuecomment-1872495764
expect(parseTsAliases('/tmp/fake/project/root', { '#*': ['./hashed/*'] })).toStrictEqual([
{
find: /^#(?!\.\/)([^*]+)$/,
find: /^#(?!\.{1,2}\/)([^*]+)$/,
replacement: expect.stringMatching(maybeWindowsPath('/tmp/fake/project/root/hashed/$1'))
}
])
Expand Down

0 comments on commit 0684ab7

Please sign in to comment.