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 prefer default #351

Merged
merged 3 commits into from
May 19, 2016
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 @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- Make [`order`]'s `newline-between` option handle multiline import statements ([#313], thanks [@singles])
- Make [`order`]'s `newline-between` option handle not assigned import statements ([#313], thanks [@singles])
- Make [`order`]'s `newline-between` option ignore `require` statements inside object literals ([#313], thanks [@singles])
- [`prefer-default-export`] properly handles deep destructuring, `export * from ...`, and files with no exports. ([#342]+[#343], thanks [@scottnonnenberg])

## [1.8.0] - 2016-05-11
### Added
Expand Down Expand Up @@ -220,6 +221,7 @@ for info on changes for earlier releases.
[`no-mutable-exports`]: ./docs/rules/no-mutable-exports.md
[`prefer-default-export`]: ./docs/rules/prefer-default-export.md

[#343]: https://github.com/benmosher/eslint-plugin-import/pull/343
[#332]: https://github.com/benmosher/eslint-plugin-import/pull/332
[#322]: https://github.com/benmosher/eslint-plugin-import/pull/322
[#316]: https://github.com/benmosher/eslint-plugin-import/pull/316
Expand All @@ -246,6 +248,7 @@ for info on changes for earlier releases.
[#164]: https://github.com/benmosher/eslint-plugin-import/pull/164
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157

[#342]: https://github.com/benmosher/eslint-plugin-import/issues/342
[#328]: https://github.com/benmosher/eslint-plugin-import/issues/328
[#317]: https://github.com/benmosher/eslint-plugin-import/issues/317
[#286]: https://github.com/benmosher/eslint-plugin-import/issues/286
Expand Down Expand Up @@ -303,3 +306,4 @@ for info on changes for earlier releases.
[@borisyankov]: https://github.com/borisyankov
[@gavriguy]: https://github.com/gavriguy
[@jkimbo]: https://github.com/jkimbo
[@scottnonnenberg]: https://github.com/scottnonnenberg
7 changes: 7 additions & 0 deletions docs/rules/prefer-default-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ export { foo, bar }
const foo = 'foo';
export { foo as default }
```

```javascript
// export-star.js

// Any batch export will disable this rule. The remote module is not inspected.
export * from './other-module'
```
4 changes: 3 additions & 1 deletion src/rules/newline-after-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ module.exports = function (context) {
return
}

if (nextStatement && (!nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) {
if (nextStatement &&
(!nextRequireCall || !containsNodeOrEqual(nextStatement, nextRequireCall))) {

checkForNewLine(statementWithRequireCall, nextStatement, 'require')
}
})
Expand Down
34 changes: 31 additions & 3 deletions src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict'

module.exports = function(context) {
let namedExportCount = 0
let specifierExportCount = 0
let hasDefaultExport = false
let hasStarExport = false
let namedExportNode = null

return {
'ExportSpecifier': function(node) {
if (node.exported.name === 'default') {
Expand All @@ -14,16 +15,43 @@ module.exports = function(context) {
namedExportNode = node
}
},

'ExportNamedDeclaration': function(node) {
namedExportCount++
// if there are specifiers, node.declaration should be null
if (!node.declaration) return

function captureDeclaration(identifierOrPattern) {
if (identifierOrPattern.type === 'ObjectPattern') {
// recursively capture
identifierOrPattern.properties
.forEach(function(property) {
captureDeclaration(property.value)
})
} else {
// assume it's a single standard identifier
specifierExportCount++
}
}

if (node.declaration.declarations) {
node.declaration.declarations.forEach(function(declaration) {
captureDeclaration(declaration.id)
})
}

namedExportNode = node
},

'ExportDefaultDeclaration': function() {
hasDefaultExport = true
},

'ExportAllDeclaration': function() {
hasStarExport = true
},

'Program:exit': function() {
if (namedExportCount === 1 && specifierExportCount < 2 && !hasDefaultExport) {
if (specifierExportCount === 1 && !hasDefaultExport && !hasStarExport) {
context.report(namedExportNode, 'Prefer default export.')
}
},
Expand Down
45 changes: 45 additions & 0 deletions tests/src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,39 @@ ruleTester.run('prefer-default-export', rule, {
code: `
export { foo, bar }`,
}),
test({
code: `
export const { foo, bar } = item;`,
}),
test({
code: `
export const { foo, bar: baz } = item;`,
}),
test({
code: `
export const { foo: { bar, baz } } = item;`,
}),
test({
code: `
export const foo = item;
export { item };`,
}),
test({
code: `
export { foo as default }`,
}),
test({
code: `
export * from './foo';`,
}),

// no exports at all
test({
code: `
import * as foo from './foo';`,
}),

// ...SYNTAX_CASES,
],
invalid: [
test({
Expand All @@ -44,5 +73,21 @@ ruleTester.run('prefer-default-export', rule, {
message: 'Prefer default export.',
}],
}),
test({
code: `
export const { foo } = { foo: "bar" };`,
errors: [{
ruleId: 'ExportNamedDeclaration',
message: 'Prefer default export.',
}],
}),
test({
code: `
export const { foo: { bar } } = { foo: { bar: "baz" } };`,
errors: [{
ruleId: 'ExportNamedDeclaration',
message: 'Prefer default export.',
}],
}),
],
})