Skip to content

Commit c3a230b

Browse files
committed
[Fix] import/extensions: ignore non-main modules
1 parent f507f38 commit c3a230b

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

src/core/importType.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function isExternalPath(path, name, settings) {
3434
}
3535

3636
const externalModuleRegExp = /^\w/
37-
function isExternalModule(name, settings, path) {
37+
export function isExternalModule(name, settings, path) {
3838
return externalModuleRegExp.test(name) && isExternalPath(path, name, settings)
3939
}
4040

@@ -44,7 +44,7 @@ export function isExternalModuleMain(name, settings, path) {
4444
}
4545

4646
const scopedRegExp = /^@[^/]+\/?[^/]+/
47-
function isScoped(name) {
47+
export function isScoped(name) {
4848
return scopedRegExp.test(name)
4949
}
5050

src/rules/extensions.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22

33
import resolve from 'eslint-module-utils/resolve'
4-
import { isBuiltIn, isExternalModuleMain, isScopedMain } from '../core/importType'
4+
import { isBuiltIn, isExternalModule, isScoped } from '../core/importType'
55
import docsUrl from '../docsUrl'
66

77
const enumValues = { enum: [ 'always', 'ignorePackages', 'never' ] }
@@ -110,8 +110,8 @@ module.exports = {
110110
return props.pattern[extension] || props.defaultConfig
111111
}
112112

113-
function isUseOfExtensionRequired(extension, isPackageMain) {
114-
return getModifier(extension) === 'always' && (!props.ignorePackages || !isPackageMain)
113+
function isUseOfExtensionRequired(extension, isPackage) {
114+
return getModifier(extension) === 'always' && (!props.ignorePackages || !isPackage)
115115
}
116116

117117
function isUseOfExtensionForbidden(extension) {
@@ -144,11 +144,11 @@ module.exports = {
144144
const extension = path.extname(resolvedPath || importPath).substring(1)
145145

146146
// determine if this is a module
147-
const isPackageMain = isExternalModuleMain(importPath, context.settings)
148-
|| isScopedMain(importPath)
147+
const isPackage = isExternalModule(importPath, context.settings)
148+
|| isScoped(importPath)
149149

150150
if (!extension || !importPath.endsWith(`.${extension}`)) {
151-
const extensionRequired = isUseOfExtensionRequired(extension, isPackageMain)
151+
const extensionRequired = isUseOfExtensionRequired(extension, isPackage)
152152
const extensionForbidden = isUseOfExtensionForbidden(extension)
153153
if (extensionRequired && !extensionForbidden) {
154154
context.report({

tests/src/rules/extensions.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ ruleTester.run('extensions', rule, {
293293
import bar from './bar.json'
294294
import Component from './Component'
295295
import baz from 'foo/baz'
296+
import baw from '@scoped/baw/import'
296297
import express from 'express'
297298
`,
298299
options: [ 'always', {ignorePackages: true} ],
@@ -301,10 +302,6 @@ ruleTester.run('extensions', rule, {
301302
message: 'Missing file extension for "./Component"',
302303
line: 4,
303304
column: 31,
304-
}, {
305-
message: 'Missing file extension for "foo/baz"',
306-
line: 5,
307-
column: 25,
308305
},
309306
],
310307
}),
@@ -315,6 +312,7 @@ ruleTester.run('extensions', rule, {
315312
import bar from './bar.json'
316313
import Component from './Component'
317314
import baz from 'foo/baz'
315+
import baw from '@scoped/baw/import'
318316
import express from 'express'
319317
`,
320318
options: [ 'ignorePackages' ],
@@ -323,10 +321,6 @@ ruleTester.run('extensions', rule, {
323321
message: 'Missing file extension for "./Component"',
324322
line: 4,
325323
column: 31,
326-
}, {
327-
message: 'Missing file extension for "foo/baz"',
328-
line: 5,
329-
column: 25,
330324
},
331325
],
332326
}),

0 commit comments

Comments
 (0)