Skip to content

Commit

Permalink
[Fix] import/extensions: ignore non-main modules
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Dec 10, 2019
1 parent f507f38 commit c3a230b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/core/importType.js
Expand Up @@ -34,7 +34,7 @@ function isExternalPath(path, name, settings) {
}

const externalModuleRegExp = /^\w/
function isExternalModule(name, settings, path) {
export function isExternalModule(name, settings, path) {
return externalModuleRegExp.test(name) && isExternalPath(path, name, settings)
}

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

const scopedRegExp = /^@[^/]+\/?[^/]+/
function isScoped(name) {
export function isScoped(name) {
return scopedRegExp.test(name)
}

Expand Down
12 changes: 6 additions & 6 deletions src/rules/extensions.js
@@ -1,7 +1,7 @@
import path from 'path'

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

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

function isUseOfExtensionRequired(extension, isPackageMain) {
return getModifier(extension) === 'always' && (!props.ignorePackages || !isPackageMain)
function isUseOfExtensionRequired(extension, isPackage) {
return getModifier(extension) === 'always' && (!props.ignorePackages || !isPackage)
}

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

// determine if this is a module
const isPackageMain = isExternalModuleMain(importPath, context.settings)
|| isScopedMain(importPath)
const isPackage = isExternalModule(importPath, context.settings)
|| isScoped(importPath)

if (!extension || !importPath.endsWith(`.${extension}`)) {
const extensionRequired = isUseOfExtensionRequired(extension, isPackageMain)
const extensionRequired = isUseOfExtensionRequired(extension, isPackage)
const extensionForbidden = isUseOfExtensionForbidden(extension)
if (extensionRequired && !extensionForbidden) {
context.report({
Expand Down
10 changes: 2 additions & 8 deletions tests/src/rules/extensions.js
Expand Up @@ -293,6 +293,7 @@ ruleTester.run('extensions', rule, {
import bar from './bar.json'
import Component from './Component'
import baz from 'foo/baz'
import baw from '@scoped/baw/import'
import express from 'express'
`,
options: [ 'always', {ignorePackages: true} ],
Expand All @@ -301,10 +302,6 @@ ruleTester.run('extensions', rule, {
message: 'Missing file extension for "./Component"',
line: 4,
column: 31,
}, {
message: 'Missing file extension for "foo/baz"',
line: 5,
column: 25,
},
],
}),
Expand All @@ -315,6 +312,7 @@ ruleTester.run('extensions', rule, {
import bar from './bar.json'
import Component from './Component'
import baz from 'foo/baz'
import baw from '@scoped/baw/import'
import express from 'express'
`,
options: [ 'ignorePackages' ],
Expand All @@ -323,10 +321,6 @@ ruleTester.run('extensions', rule, {
message: 'Missing file extension for "./Component"',
line: 4,
column: 31,
}, {
message: 'Missing file extension for "foo/baz"',
line: 5,
column: 25,
},
],
}),
Expand Down

0 comments on commit c3a230b

Please sign in to comment.