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

[New] no-anonymous-default-export: add allowNew option #2505

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`newline-after-import`]: add `considerComments` option ([#2399], thanks [@pri1311])
- [`no-cycle`]: add `allowUnsafeDynamicCyclicDependency` option ([#2387], thanks [@GerkinDev])
- [`no-restricted-paths`]: support arrays for `from` and `target` options ([#2466], thanks [@AdriAt360])
- [`no-anonymous-default-export`]: add `allowNew` option ([#2505], thanks [@DamienCassou])

### Fixed
- [`order`]: move nested imports closer to main import entry ([#2396], thanks [@pri1311])
Expand Down Expand Up @@ -1534,6 +1535,7 @@ for info on changes for earlier releases.
[@chrislloyd]: https://github.com/chrislloyd
[@christianvuerings]: https://github.com/christianvuerings
[@christophercurrie]: https://github.com/christophercurrie
[@DamienCassou]: https://github.com/DamienCassou
[@danny-andrews]: https://github.com/dany-andrews
[@darkartur]: https://github.com/darkartur
[@davidbonnet]: https://github.com/davidbonnet
Expand Down
6 changes: 6 additions & 0 deletions docs/rules/no-anonymous-default-export.md
Expand Up @@ -17,6 +17,7 @@ The complete default configuration looks like this.
"allowAnonymousClass": false,
"allowAnonymousFunction": false,
"allowCallExpression": true, // The true value here is for backward compatibility
"allowNew": false,
"allowLiteral": false,
"allowObject": false
}]
Expand All @@ -40,6 +41,8 @@ export default foo(bar)
export default 123

export default {}

export default new Foo()
```

### Pass
Expand Down Expand Up @@ -70,4 +73,7 @@ export default 123

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

/* eslint import/no-anonymous-default-export: [2, {"allowNew": true}] */
export default new Foo()
```
5 changes: 5 additions & 0 deletions src/rules/no-anonymous-default-export.js
Expand Up @@ -50,6 +50,11 @@ const defs = {
description: 'If `false`, will report default export of a literal',
message: 'Assign literal to a variable before exporting as module default',
},
NewExpression: {
option: 'allowNew',
description: 'If `false`, will report default export of a class instantiation',
message: 'Assign instance to a variable before exporting as module default',
},
};

const schemaProperties = Object.keys(defs)
Expand Down
2 changes: 2 additions & 0 deletions tests/src/rules/no-anonymous-default-export.js
Expand Up @@ -22,6 +22,7 @@ ruleTester.run('no-anonymous-default-export', rule, {
test({ code: 'export default `foo`', options: [{ allowLiteral: true }] }),
test({ code: 'export default {}', options: [{ allowObject: true }] }),
test({ code: 'export default foo(bar)', options: [{ allowCallExpression: true }] }),
test({ code: 'export default new Foo()', options: [{ allowNew: true }] }),

// Allow forbidden types with multiple options
test({ code: 'export default 123', options: [{ allowLiteral: true, allowObject: true }] }),
Expand Down Expand Up @@ -53,6 +54,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 {}', 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({ code: 'export default new Foo()', errors: [{ message: 'Assign instance 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