Skip to content

Commit

Permalink
Handle new expressions in the no-anonymous-default-export rule
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed Jul 20, 2022
1 parent d82670c commit 83567c8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
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

0 comments on commit 83567c8

Please sign in to comment.