11'use strict' ;
22
33const { getScriptType } = require ( '../util/metadata' ) ;
4- const { scriptTypes } = require ( '../util/scriptTypes' ) ;
4+ const { scriptTypeMap , scriptTypes } = require ( '../util/scriptTypes' ) ;
55
66module . exports = {
77 meta : {
@@ -11,75 +11,71 @@ module.exports = {
1111 } ,
1212 } ,
1313
14- create : function ( context ) {
15- return {
16- 'CallExpression[callee.name=define]' : function ( node ) {
17- let hasValidEntryPoint = false ;
18- let scriptType = getScriptType ( context ) ;
14+ create : ( context ) => ( {
15+ 'CallExpression[callee.name=define]' : ( node ) => {
16+ let hasValidEntryPoint = false ;
17+ let scriptType = getScriptType ( context ) ;
1918
20- scriptType = scriptType && scriptType . value ;
19+ scriptType = scriptType && scriptType . value ;
2120
22- if ( ! scriptType || ! scriptTypes . includes ( scriptType ) ) {
23- return ;
24- }
25-
26- const argsCount = node . arguments . length ;
27- const callback = node . arguments [ argsCount - 1 ] ;
28-
29- if ( ! callback || callback . type !== 'FunctionExpression' ) {
30- return ;
31- }
21+ if ( ! scriptType || ! scriptTypes . includes ( scriptType ) ) {
22+ return ;
23+ }
3224
33- const callbackBody = callback . body . body ;
34- const returnStatement =
35- callbackBody . length > 0 &&
36- callbackBody . find ( ( n ) => n . type === 'ReturnStatement' ) ;
37- const returnArgument = returnStatement && returnStatement . argument ;
25+ const argsCount = node . arguments . length ;
26+ const callback = node . arguments [ argsCount - 1 ] ;
3827
39- if ( returnArgument && returnArgument . type === 'ObjectExpression' ) {
40- for ( const property of returnArgument . properties ) {
41- if (
42- scriptTypeMap [ scriptType ] . entryPoints . includes ( property . key . name ) &&
43- ( property . value . type === 'Identifier' ||
44- property . value . type === 'FunctionExpression' )
45- ) {
46- hasValidEntryPoint = true ;
47- break ;
48- }
49- }
50- }
28+ if ( ! callback || callback . type !== 'FunctionExpression' ) {
29+ return ;
30+ }
5131
52- if ( returnArgument && returnArgument . type === 'Identifier' ) {
53- const returnAssignments = callbackBody . filter ( function ( n ) {
54- return (
55- n . type === 'ExpressionStatement' &&
56- n . expression . left . type === 'MemberExpression' &&
57- n . expression . left . object . name === returnArgument . name &&
58- scriptTypeMap [ scriptType ] . entryPoints . includes (
59- n . expression . left . property . name
60- )
61- ) ;
62- } ) ;
32+ const callbackBody = callback . body . body ;
33+ const returnStatement =
34+ callbackBody . length > 0 && callbackBody . find ( ( n ) => n . type === 'ReturnStatement' ) ;
35+ const returnArgument = returnStatement && returnStatement . argument ;
6336
64- if ( returnAssignments . length > 0 ) {
37+ if ( returnArgument && returnArgument . type === 'ObjectExpression' ) {
38+ for ( const property of returnArgument . properties ) {
39+ if (
40+ scriptTypeMap [ scriptType ] . entryPoints . includes ( property . key . name ) &&
41+ ( property . value . type === 'Identifier' ||
42+ property . value . type === 'FunctionExpression' )
43+ ) {
6544 hasValidEntryPoint = true ;
45+ break ;
6646 }
6747 }
48+ }
6849
69- if ( scriptTypeMap [ scriptType ] . entryPoints . length === 0 ) {
50+ if ( returnArgument && returnArgument . type === 'Identifier' ) {
51+ const returnAssignments = callbackBody . filter (
52+ ( n ) =>
53+ n . type === 'ExpressionStatement' &&
54+ n . expression . left . type === 'MemberExpression' &&
55+ n . expression . left . object . name === returnArgument . name &&
56+ scriptTypeMap [ scriptType ] . entryPoints . includes (
57+ n . expression . left . property . name
58+ )
59+ ) ;
60+
61+ if ( returnAssignments . length > 0 ) {
7062 hasValidEntryPoint = true ;
7163 }
64+ }
7265
73- if ( ! hasValidEntryPoint ) {
74- context . report ( {
75- node : returnStatement || callback ,
76- messageId : 'returnEntryPoint' ,
77- data : {
78- type : scriptType ,
79- } ,
80- } ) ;
81- }
82- } ,
83- } ;
84- } ,
66+ if ( scriptTypeMap [ scriptType ] . entryPoints . length === 0 ) {
67+ hasValidEntryPoint = true ;
68+ }
69+
70+ if ( ! hasValidEntryPoint ) {
71+ context . report ( {
72+ node : returnStatement || callback ,
73+ messageId : 'returnEntryPoint' ,
74+ data : {
75+ type : scriptType ,
76+ } ,
77+ } ) ;
78+ }
79+ } ,
80+ } ) ,
8581} ;
0 commit comments