Skip to content

Commit

Permalink
[fix] default: make error message less confusing
Browse files Browse the repository at this point in the history
Fixes #751, fixes #786
  • Loading branch information
golopot authored and ljharb committed Sep 4, 2019
1 parent 7ffbf03 commit 726dda5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
- support `parseForESLint` from custom parser ([#1435], thanks [@JounQin])

### Fixed
- `default`: make error message less confusing ([#1470], thanks [@golopot])

## [2.18.2] - 2019-07-19
- Skip warning on type interfaces ([#1425], thanks [@lencioni])

Expand Down Expand Up @@ -601,6 +604,7 @@ for info on changes for earlier releases.

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

[#1470]: https://github.com/benmosher/eslint-plugin-import/pull/1470
[#1435]: https://github.com/benmosher/eslint-plugin-import/pull/1435
[#1425]: https://github.com/benmosher/eslint-plugin-import/pull/1425
[#1419]: https://github.com/benmosher/eslint-plugin-import/pull/1419
Expand Down
16 changes: 7 additions & 9 deletions src/rules/default.js
Expand Up @@ -13,14 +13,9 @@ module.exports = {

function checkDefault(specifierType, node) {

// poor man's Array.find
let defaultSpecifier
node.specifiers.some((n) => {
if (n.type === specifierType) {
defaultSpecifier = n
return true
}
})
const defaultSpecifier = node.specifiers.find(
specifier => specifier.type === specifierType
)

if (!defaultSpecifier) return
var imports = Exports.get(node.source.value, context)
Expand All @@ -29,7 +24,10 @@ module.exports = {
if (imports.errors.length) {
imports.reportErrors(context, node)
} else if (imports.get('default') === undefined) {
context.report(defaultSpecifier, 'No default export found in module.')
context.report({
node: defaultSpecifier,
message: `No default export found in imported module "${node.source.value}".`,
})
}
}

Expand Down
14 changes: 7 additions & 7 deletions tests/src/rules/default.js
Expand Up @@ -102,7 +102,7 @@ ruleTester.run('default', rule, {

test({
code: 'import baz from "./named-exports";',
errors: [{ message: 'No default export found in module.'
errors: [{ message: 'No default export found in imported module "./named-exports".'
, type: 'ImportDefaultSpecifier'}]}),

test({
Expand All @@ -114,29 +114,29 @@ ruleTester.run('default', rule, {
test({
code: 'export baz from "./named-exports"',
parser: require.resolve('babel-eslint'),
errors: ['No default export found in module.'],
errors: ['No default export found in imported module "./named-exports".'],
}),
test({
code: 'export baz, { bar } from "./named-exports"',
parser: require.resolve('babel-eslint'),
errors: ['No default export found in module.'],
errors: ['No default export found in imported module "./named-exports".'],
}),
test({
code: 'export baz, * as names from "./named-exports"',
parser: require.resolve('babel-eslint'),
errors: ['No default export found in module.'],
errors: ['No default export found in imported module "./named-exports".'],
}),
// exports default from a module with no default
test({
code: 'import twofer from "./broken-trampoline"',
parser: require.resolve('babel-eslint'),
errors: ['No default export found in module.'],
errors: ['No default export found in imported module "./broken-trampoline".'],
}),

// #328: * exports do not include default
test({
code: 'import barDefault from "./re-export"',
errors: [`No default export found in module.`],
errors: ['No default export found in imported module "./re-export".'],
}),
],
})
Expand All @@ -152,7 +152,7 @@ if (!CASE_SENSITIVE_FS) {
invalid: [
test({
code: 'import bar from "./Named-Exports"',
errors: ['No default export found in module.'],
errors: ['No default export found in imported module "./Named-Exports".'],
}),
],
})
Expand Down

0 comments on commit 726dda5

Please sign in to comment.