Repository: import-js/eslint-plugin-import
Pull request #2505 (2022-07-20 13:06:12)

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


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 821d943e..9809e886 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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])
@@ -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
diff --git a/docs/rules/no-anonymous-default-export.md b/docs/rules/no-anonymous-default-export.md
index c8db8979..3edac631 100644
--- a/docs/rules/no-anonymous-default-export.md
+++ b/docs/rules/no-anonymous-default-export.md
@@ -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
 }]
@@ -40,6 +41,8 @@ export default foo(bar)
 export default 123
 
 export default {}
+
+export default new Foo()
 ```
 
 ### Pass
@@ -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()
 ```
diff --git a/src/rules/no-anonymous-default-export.js b/src/rules/no-anonymous-default-export.js
index f1f495ca..106f43b0 100644
--- a/src/rules/no-anonymous-default-export.js
+++ b/src/rules/no-anonymous-default-export.js
@@ -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)


Test patch:
diff --git a/tests/src/rules/no-anonymous-default-export.js b/tests/src/rules/no-anonymous-default-export.js
index 0428ee1b..53b2fc6f 100644
--- a/tests/src/rules/no-anonymous-default-export.js
+++ b/tests/src/rules/no-anonymous-default-export.js
@@ -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 }] }),
@@ -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' }] }),