File tree Expand file tree Collapse file tree 5 files changed +122
-0
lines changed Expand file tree Collapse file tree 5 files changed +122
-0
lines changed Original file line number Diff line number Diff line change @@ -101,6 +101,7 @@ const config = {
101101 '@liferay/imports-first' : 'error' ,
102102 '@liferay/no-abbreviations' : 'error' ,
103103 '@liferay/no-absolute-import' : 'error' ,
104+ '@liferay/no-anonymous-exports' : 'error' ,
104105 '@liferay/no-duplicate-imports' : 'error' ,
105106 '@liferay/no-dynamic-require' : 'error' ,
106107 '@liferay/no-get-data-attribute' : 'error' ,
Original file line number Diff line number Diff line change 1+ # Prefer exporting named functions (no-anonymous-exports)
2+
3+ To improve debugging and readability, it is preferable to export named functions to that they can be located in a callstack.
4+
5+ ## Rule Details
6+
7+ Examples of ** incorrect** code for this rule:
8+
9+ ``` js
10+ export const x = () => {};
11+
12+ export default () => {};
13+ ```
14+
15+ Examples of ** correct** code for this rule:
16+
17+ ``` js
18+ export function x () {}
19+
20+ export default function x () {}
21+ ```
22+
23+ ## See also
24+
25+ - https://github.com/liferay/liferay-frontend-projects/issues/25
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ module.exports = {
1313 'imports-first' : require ( './lib/rules/imports-first' ) ,
1414 'no-abbreviations' : require ( './lib/rules/no-abbreviations' ) ,
1515 'no-absolute-import' : require ( './lib/rules/no-absolute-import' ) ,
16+ 'no-anonymous-exports' : require ( './lib/rules/no-anonymous-exports' ) ,
1617 'no-arrow' : require ( './lib/rules/no-arrow' ) ,
1718 'no-duplicate-class-names' : require ( './lib/rules/no-duplicate-class-names' ) ,
1819 'no-duplicate-imports' : require ( './lib/rules/no-duplicate-imports' ) ,
Original file line number Diff line number Diff line change 1+ /**
2+ * SPDX-FileCopyrightText: © 2021 Liferay, Inc. <https://liferay.com>
3+ * SPDX-License-Identifier: MIT
4+ */
5+
6+ module . exports = {
7+ create ( context ) {
8+ return {
9+ ExportDefaultDeclaration ( node ) {
10+ if (
11+ node . declaration &&
12+ node . declaration . type === 'ArrowFunctionExpression'
13+ ) {
14+ context . report ( {
15+ message :
16+ "Use named function for export. Example: 'function fooBar() {}'" ,
17+ node,
18+ } ) ;
19+ }
20+ } ,
21+ ExportNamedDeclaration ( node ) {
22+ if (
23+ node . declaration &&
24+ node . declaration . type === 'VariableDeclaration' &&
25+ node . declaration . declarations &&
26+ node . declaration . declarations [ 0 ] &&
27+ node . declaration . declarations [ 0 ] . init &&
28+ node . declaration . declarations [ 0 ] . init . type ===
29+ 'ArrowFunctionExpression'
30+ ) {
31+ context . report ( {
32+ message :
33+ "Use named function for export instead of arrow function. Example: 'function fooBar() {}'" ,
34+ node,
35+ } ) ;
36+ }
37+ } ,
38+ } ;
39+ } ,
40+ meta : {
41+ category : 'Best Practices' ,
42+ description : 'Prefer exporting named functions' ,
43+ recommended : false ,
44+ url : 'https://github.com/liferay/liferay-frontend-projects/issues/25' ,
45+ } ,
46+ } ;
Original file line number Diff line number Diff line change 1+ /**
2+ * SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
3+ * SPDX-License-Identifier: MIT
4+ */
5+
6+ const MultiTester = require ( '../../../../../scripts/MultiTester' ) ;
7+ const rule = require ( '../../../lib/rules/no-anonymous-exports' ) ;
8+
9+ const parserOptions = {
10+ parserOptions : {
11+ ecmaVersion : 6 ,
12+ sourceType : 'module' ,
13+ } ,
14+ } ;
15+
16+ const ruleTester = new MultiTester ( parserOptions ) ;
17+
18+ ruleTester . run ( 'no-anonymous-exports' , rule , {
19+ invalid : [
20+ {
21+ code : `export const test = () => {}` ,
22+ errors : [
23+ {
24+ message :
25+ "Use named function for export instead of arrow function. Example: 'function fooBar() {}'" ,
26+ type : 'ExportNamedDeclaration' ,
27+ } ,
28+ ] ,
29+ } ,
30+ {
31+ code : `export default () => {}` ,
32+ errors : [
33+ {
34+ message :
35+ "Use named function for export. Example: 'function fooBar() {}'" ,
36+ type : 'ExportDefaultDeclaration' ,
37+ } ,
38+ ] ,
39+ } ,
40+ ] ,
41+ valid : [
42+ {
43+ code : `
44+ export function test() {}
45+ export default function fooBar() {}
46+ ` ,
47+ } ,
48+ ] ,
49+ } ) ;
You can’t perform that action at this time.
0 commit comments