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

Handle call expressions in the no-anonymous-default-export rule #977

Merged
merged 1 commit into from
Jan 4, 2018
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
6 changes: 6 additions & 0 deletions docs/rules/no-anonymous-default-export.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The complete default configuration looks like this.
"allowArrowFunction": false,
"allowAnonymousClass": false,
"allowAnonymousFunction": false,
"allowCallExpression": true, // The true value here is for backward compatibility
"allowLiteral": false,
"allowObject": false
}]
Expand All @@ -33,6 +34,9 @@ export default class {}

export default function () {}

/* eslint import/no-anonymous-default-export: [2, {"allowCallExpression": false}] */
export default foo(bar)

export default 123

export default {}
Expand All @@ -59,6 +63,8 @@ export default class {}
/* eslint import/no-anonymous-default-export: [2, {"allowAnonymousFunction": true}] */
export default function () {}

export default foo(bar)

/* eslint import/no-anonymous-default-export: [2, {"allowLiteral": true}] */
export default 123

Expand Down
22 changes: 17 additions & 5 deletions src/rules/no-anonymous-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const defs = {
description: 'If `false`, will report default export of an arrow function',
message: 'Assign arrow function to a variable before exporting as module default',
},
CallExpression: {
option: 'allowCallExpression',
description: 'If `false`, will report default export of a function call',
message: 'Assign call result to a variable before exporting as module default',
default: true,
},
ClassDeclaration: {
option: 'allowAnonymousClass',
description: 'If `false`, will report default export of an anonymous class',
Expand Down Expand Up @@ -43,18 +49,24 @@ const defs = {
},
}

const schemaProperties = Object.keys(defs).
map((key) => defs[key]).
reduce((acc, def) => {
const schemaProperties = Object.keys(defs)
.map((key) => defs[key])
.reduce((acc, def) => {
acc[def.option] = {
description: def.description,
type: 'boolean',
default: false,
}

return acc
}, {})

const defaults = Object.keys(defs)
.map((key) => defs[key])
.reduce((acc, def) => {
acc[def.option] = def.hasOwnProperty('default') ? def.default : false
return acc
}, {})

module.exports = {
meta: {
schema: [
Expand All @@ -67,7 +79,7 @@ module.exports = {
},

create: function (context) {
const options = Object.assign({}, context.options[0])
const options = Object.assign({}, defaults, context.options[0])

return {
'ExportDefaultDeclaration': (node) => {
Expand Down
5 changes: 5 additions & 0 deletions tests/src/rules/no-anonymous-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ruleTester.run('no-anonymous-default-export', rule, {
test({ code: 'export default \'foo\'', options: [{ allowLiteral: true }] }),
test({ code: 'export default `foo`', options: [{ allowLiteral: true }] }),
test({ code: 'export default {}', options: [{ allowObject: true }] }),
test({ code: 'export default foo(bar)', options: [{ allowCallExpression: true }] }),

// Allow forbidden types with multiple options
test({ code: 'export default 123', options: [{ allowLiteral: true, allowObject: true }] }),
Expand All @@ -31,6 +32,9 @@ ruleTester.run('no-anonymous-default-export', rule, {
test({ code: 'const foo = 123\nexport { foo }' }),
test({ code: 'const foo = 123\nexport { foo as default }' }),

// Allow call expressions by default for backwards compatibility
test({ code: 'export default foo(bar)' }),

...SYNTAX_CASES,
],

Expand All @@ -43,6 +47,7 @@ ruleTester.run('no-anonymous-default-export', rule, {
test({ code: 'export default \'foo\'', errors: [{ message: 'Assign literal to a variable before exporting as module default' }] }),
test({ code: 'export default `foo`', errors: [{ message: 'Assign literal to a variable before exporting as module default' }] }),
test({ code: 'export default {}', errors: [{ message: 'Assign object to a variable before exporting as module default' }] }),
test({ code: 'export default foo(bar)', options: [{ allowCallExpression: false }], errors: [{ message: 'Assign call result to a variable before exporting as module default' }] }),

// Test failure with non-covering exception
test({ code: 'export default 123', options: [{ allowObject: true }], errors: [{ message: 'Assign literal to a variable before exporting as module default' }] }),
Expand Down