@@ -20,83 +20,93 @@ module.exports = {
2020 } ,
2121 fixable : 'code' ,
2222 messages : {
23- invalidEventName : 'Custom events must be named in all lower case with no punctuation.' ,
24- invalidEventNameReference : 'When referencing a custom event name, it must be accessed as ClassName.eventName.'
23+ invalidEventName :
24+ 'Custom events must be named in all lower case with no punctuation.' ,
25+ invalidEventNameReference :
26+ 'When referencing a custom event name, it must be accessed as ClassName.eventName.' ,
2527 } ,
26- schema : [ ] // no options
28+ schema : [ ] , // no options
2729 } ,
28- create : function ( context ) {
30+ create : function ( context ) {
2931 let foundLocalEventClassDeclaration = false ;
3032 const classDeclarationsToLint = [ ] ;
3133
3234 function lintClassNode ( node ) {
33-
34- const constructor =
35- node . body . body . find ( node => node . type === 'MethodDefinition' && node . kind === 'constructor' ) ;
36- if ( ! constructor ) {
37- return ;
38- }
39- const superCall = constructor . value . body . body . find ( bodyNode => {
40- return bodyNode . type === 'ExpressionStatement' && bodyNode . expression . type === 'CallExpression' &&
41- bodyNode . expression . callee . type === 'Super' ;
42- } ) ;
43- if ( ! superCall ) {
44- return ;
45- }
46-
47- const firstArgToSuper = superCall . expression . arguments [ 0 ] ;
48- if ( ! firstArgToSuper ) {
49- // This is invalid, but TypeScript will catch this for us so no need to
50- // error in ESLint land as well.
51- return ;
52- }
53- if ( firstArgToSuper . type === 'Literal' ) {
54- const firstArgLiteralValue = firstArgToSuper . value ;
55- if ( ! firstArgLiteralValue . match ( VALID_EVENT_NAME_REGEX ) ) {
56- context . report ( { node, messageId : 'invalidEventName' } ) ;
57- }
58- return ;
59- }
60-
61- if ( firstArgToSuper . type !== 'MemberExpression' ) {
62- // This means it's a variable but not of the form ClassName.eventName, which we do not allow.
63- context . report ( { node, messageId : 'invalidEventNameReference' } ) ;
64- return ;
65- }
66-
67- // the name of the custom event class we're looking at
68- const eventClassName = node . id . name ;
69- const objectName = firstArgToSuper . object . name ;
70- const propertyName = firstArgToSuper . property . name ;
71-
72- if ( objectName !== eventClassName || propertyName !== 'eventName' ) {
73- context . report ( { node, messageId : 'invalidEventNameReference' } ) ;
74- return ;
75- }
76-
77- // If the reference is right, let's find the value of the static eventName property and make sure it is valid.
78- const eventNameProperty = node . body . body . find ( classBodyPart => {
79- return classBodyPart . type === 'PropertyDefinition' && classBodyPart . key . name === 'eventName' ;
80- } ) ;
81-
82- // This should always exist because we checked for its existence
83- // previously, no error loudly as this is a bug in the lint rule.
84- if ( ! eventNameProperty ) {
85- throw new Error ( `Could not find static eventName property for ${ eventClassName } .` ) ;
86- }
87-
88- // We don't let people use static eventName = SOME_VAR;
89- if ( eventNameProperty . value . type !== 'Literal' ) {
90- context . report ( { node, messageId : 'invalidEventNameReference' } ) ;
91- return ;
92- }
93-
94- // Grab the value of static eventName and confirm it follows the
95- // required conventions.
96- const valueOfEventName = eventNameProperty . value . value ;
97- if ( ! valueOfEventName . match ( VALID_EVENT_NAME_REGEX ) ) {
98- context . report ( { node, messageId : 'invalidEventName' } ) ;
35+ const constructor = node . body . body . find (
36+ node => node . type === 'MethodDefinition' && node . kind === 'constructor' ,
37+ ) ;
38+ if ( ! constructor ) {
39+ return ;
40+ }
41+ const superCall = constructor . value . body . body . find ( bodyNode => {
42+ return (
43+ bodyNode . type === 'ExpressionStatement' &&
44+ bodyNode . expression . type === 'CallExpression' &&
45+ bodyNode . expression . callee . type === 'Super'
46+ ) ;
47+ } ) ;
48+ if ( ! superCall ) {
49+ return ;
50+ }
51+
52+ const firstArgToSuper = superCall . expression . arguments [ 0 ] ;
53+ if ( ! firstArgToSuper ) {
54+ // This is invalid, but TypeScript will catch this for us so no need to
55+ // error in ESLint land as well.
56+ return ;
57+ }
58+ if ( firstArgToSuper . type === 'Literal' ) {
59+ const firstArgLiteralValue = firstArgToSuper . value ;
60+ if ( ! firstArgLiteralValue . match ( VALID_EVENT_NAME_REGEX ) ) {
61+ context . report ( { node, messageId : 'invalidEventName' } ) ;
9962 }
63+ return ;
64+ }
65+
66+ if ( firstArgToSuper . type !== 'MemberExpression' ) {
67+ // This means it's a variable but not of the form ClassName.eventName, which we do not allow.
68+ context . report ( { node, messageId : 'invalidEventNameReference' } ) ;
69+ return ;
70+ }
71+
72+ // the name of the custom event class we're looking at
73+ const eventClassName = node . id . name ;
74+ const objectName = firstArgToSuper . object . name ;
75+ const propertyName = firstArgToSuper . property . name ;
76+
77+ if ( objectName !== eventClassName || propertyName !== 'eventName' ) {
78+ context . report ( { node, messageId : 'invalidEventNameReference' } ) ;
79+ return ;
80+ }
81+
82+ // If the reference is right, let's find the value of the static eventName property and make sure it is valid.
83+ const eventNameProperty = node . body . body . find ( classBodyPart => {
84+ return (
85+ classBodyPart . type === 'PropertyDefinition' &&
86+ classBodyPart . key . name === 'eventName'
87+ ) ;
88+ } ) ;
89+
90+ // This should always exist because we checked for its existence
91+ // previously, no error loudly as this is a bug in the lint rule.
92+ if ( ! eventNameProperty ) {
93+ throw new Error (
94+ `Could not find static eventName property for ${ eventClassName } .` ,
95+ ) ;
96+ }
97+
98+ // We don't let people use static eventName = SOME_VAR;
99+ if ( eventNameProperty . value . type !== 'Literal' ) {
100+ context . report ( { node, messageId : 'invalidEventNameReference' } ) ;
101+ return ;
102+ }
103+
104+ // Grab the value of static eventName and confirm it follows the
105+ // required conventions.
106+ const valueOfEventName = eventNameProperty . value . value ;
107+ if ( ! valueOfEventName . match ( VALID_EVENT_NAME_REGEX ) ) {
108+ context . report ( { node, messageId : 'invalidEventName' } ) ;
109+ }
100110 }
101111
102112 return {
@@ -110,7 +120,11 @@ module.exports = {
110120 return ;
111121 }
112122
113- if ( ! node . superClass || node . superClass . name !== 'Event' ) {
123+ if (
124+ ! node . superClass ||
125+ node . superClass . type !== 'Identifier' ||
126+ node . superClass . name !== 'Event'
127+ ) {
114128 return ;
115129 }
116130
@@ -126,5 +140,5 @@ module.exports = {
126140 } ) ;
127141 } ,
128142 } ;
129- }
143+ } ,
130144} ;
0 commit comments