Skip to content

Commit c910b64

Browse files
committed
Handle cases with path like '../index.js'
1 parent a278359 commit c910b64

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/rules/default-import-match-filename.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,18 @@ function getFilename(path, context) {
9191
return undefined
9292
}
9393

94-
// like require('.'), require('..'), require('../..')
95-
if (isAncestorRelativePath(path)) {
96-
return getNameFromPackageJsonOrDirname(path, context)
97-
}
98-
9994
const basename = Path.basename(path)
10095

101-
const filename = /^index$|^index\./.test(basename)
102-
? Path.basename(Path.dirname(path))
103-
: basename
96+
const processedPath = /^index$|^index\./.test(basename)
97+
? Path.dirname(path)
98+
: path
10499

105-
if (filename === '' || filename === '.' || filename === '..') {
106-
return undefined
100+
// like require('.'), require('..'), require('../..')
101+
if (isAncestorRelativePath(processedPath)) {
102+
return getNameFromPackageJsonOrDirname(processedPath, context)
107103
}
108-
return filename
104+
105+
return Path.basename(processedPath)
109106
}
110107

111108
/**

tests/src/rules/default-import-match-filename.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ ruleTester.run('default-import-match-filename', rule, {
5858
'import doge from "loud-cat"',
5959
'import doge from ".cat"',
6060
'import doge from ""',
61-
'import doge from "../index"',
6261
'import {doge} from "./cat"',
6362
'import cat, {doge} from "./cat"',
6463
'const cat = require("./cat")',
@@ -96,6 +95,10 @@ ruleTester.run('default-import-match-filename', rule, {
9695
'default-import-match-filename/some-directory/a.js'
9796
),
9897
},
98+
{
99+
code: 'import doge from "../index.js"',
100+
filename: 'doge/a/a.js',
101+
},
99102
],
100103
invalid: [
101104
fail('import cat0 from "./cat"', 'cat'),
@@ -125,6 +128,11 @@ ruleTester.run('default-import-match-filename', rule, {
125128
'package-name',
126129
testFilePath('default-import-match-filename/some-directory/a.js')
127130
),
131+
fail(
132+
'import nope from "../../index.js"',
133+
'package-name',
134+
testFilePath('default-import-match-filename/some-directory/foo/a.js')
135+
),
128136
{
129137
code: `import QWERTY from '../bbb/ccc';`,
130138
options: [{ignorePaths: ['aaa']}],

0 commit comments

Comments
 (0)