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 authored and ljharb committed Dec 10, 2019
1 parent f507f38 commit e517739
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

### Fixed
- [`import/extensions`]: ignore non-main modules ([#1563], thanks [@saschanaz])

## [2.19.1] - 2019-12-08
### Fixed
- [`no-extraneous-dependencies`]: ensure `node.source` exists
Expand Down Expand Up @@ -620,6 +623,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1563]: https://github.com/benmosher/eslint-plugin-import/pull/1563
[#1551]: https://github.com/benmosher/eslint-plugin-import/pull/1551
[#1542]: https://github.com/benmosher/eslint-plugin-import/pull/1542
[#1521]: https://github.com/benmosher/eslint-plugin-import/pull/1521
Expand Down
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 e517739

Please sign in to comment.