Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] import/extensions: ignore non-main modules #1563

Merged
merged 1 commit into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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