Skip to content

Commit 0ef8cba

Browse files
DamienCassouljharb
authored andcommitted
[New] no-anonymous-default-export: add allowNew option
1 parent d82670c commit 0ef8cba

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1010
- [`newline-after-import`]: add `considerComments` option ([#2399], thanks [@pri1311])
1111
- [`no-cycle`]: add `allowUnsafeDynamicCyclicDependency` option ([#2387], thanks [@GerkinDev])
1212
- [`no-restricted-paths`]: support arrays for `from` and `target` options ([#2466], thanks [@AdriAt360])
13+
- [`no-anonymous-default-export`]: add `allowNew` option ([#2505], thanks [@DamienCassou])
1314

1415
### Fixed
1516
- [`order`]: move nested imports closer to main import entry ([#2396], thanks [@pri1311])
@@ -1534,6 +1535,7 @@ for info on changes for earlier releases.
15341535
[@chrislloyd]: https://github.com/chrislloyd
15351536
[@christianvuerings]: https://github.com/christianvuerings
15361537
[@christophercurrie]: https://github.com/christophercurrie
1538+
[@DamienCassou]: https://github.com/DamienCassou
15371539
[@danny-andrews]: https://github.com/dany-andrews
15381540
[@darkartur]: https://github.com/darkartur
15391541
[@davidbonnet]: https://github.com/davidbonnet

Diff for: docs/rules/no-anonymous-default-export.md

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The complete default configuration looks like this.
1717
"allowAnonymousClass": false,
1818
"allowAnonymousFunction": false,
1919
"allowCallExpression": true, // The true value here is for backward compatibility
20+
"allowNew": false,
2021
"allowLiteral": false,
2122
"allowObject": false
2223
}]
@@ -40,6 +41,8 @@ export default foo(bar)
4041
export default 123
4142

4243
export default {}
44+
45+
export default new Foo()
4346
```
4447

4548
### Pass
@@ -70,4 +73,7 @@ export default 123
7073

7174
/* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */
7275
export default {}
76+
77+
/* eslint import/no-anonymous-default-export: [2, {"allowNew": true}] */
78+
export default new Foo()
7379
```

Diff for: src/rules/no-anonymous-default-export.js

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ const defs = {
5050
description: 'If `false`, will report default export of a literal',
5151
message: 'Assign literal to a variable before exporting as module default',
5252
},
53+
NewExpression: {
54+
option: 'allowNew',
55+
description: 'If `false`, will report default export of a class instantiation',
56+
message: 'Assign instance to a variable before exporting as module default',
57+
},
5358
};
5459

5560
const schemaProperties = Object.keys(defs)

Diff for: tests/src/rules/no-anonymous-default-export.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ruleTester.run('no-anonymous-default-export', rule, {
2222
test({ code: 'export default `foo`', options: [{ allowLiteral: true }] }),
2323
test({ code: 'export default {}', options: [{ allowObject: true }] }),
2424
test({ code: 'export default foo(bar)', options: [{ allowCallExpression: true }] }),
25+
test({ code: 'export default new Foo()', options: [{ allowNew: true }] }),
2526

2627
// Allow forbidden types with multiple options
2728
test({ code: 'export default 123', options: [{ allowLiteral: true, allowObject: true }] }),
@@ -53,6 +54,7 @@ ruleTester.run('no-anonymous-default-export', rule, {
5354
test({ code: 'export default `foo`', errors: [{ message: 'Assign literal to a variable before exporting as module default' }] }),
5455
test({ code: 'export default {}', errors: [{ message: 'Assign object to a variable before exporting as module default' }] }),
5556
test({ code: 'export default foo(bar)', options: [{ allowCallExpression: false }], errors: [{ message: 'Assign call result to a variable before exporting as module default' }] }),
57+
test({ code: 'export default new Foo()', errors: [{ message: 'Assign instance to a variable before exporting as module default' }] }),
5658

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

0 commit comments

Comments
 (0)