@@ -14,7 +14,6 @@ const { isGlobalIdentifier } = require('./util/scope');
1414 */
1515function reachableDuringSSRPartial ( ) {
1616 let moduleInfo ;
17- let insideLWC = false ;
1817 let reachableFunctionThatWeAreIn = null ;
1918 let reachableMethodThatWeAreIn = null ;
2019 let skippedBlockThatWeAreIn = null ;
@@ -24,22 +23,16 @@ function reachableDuringSSRPartial() {
2423 Program : ( node ) => {
2524 moduleInfo = analyze ( node ) ;
2625 } ,
27- ClassDeclaration : ( node ) => {
28- if ( node === moduleInfo . lwcClassDeclaration ) {
29- insideLWC = true ;
30- }
31- } ,
32- 'ClassDeclaration:exit' : ( node ) => {
33- if ( node === moduleInfo . lwcClassDeclaration ) {
34- insideLWC = false ;
35- }
36- } ,
3726 FunctionDeclaration : ( node ) => {
3827 if (
3928 ! reachableFunctionThatWeAreIn &&
4029 node . id &&
4130 node . id . type === 'Identifier' &&
42- moduleInfo . moduleScopedFunctionsReachableDuringSSR . has ( node . id && node . id . name )
31+ ( ( moduleInfo . isLWC &&
32+ moduleInfo . moduleScopedFunctionsReachableDuringSSR . has (
33+ node . id && node . id . name ,
34+ ) ) ||
35+ ! moduleInfo . isLWC )
4336 ) {
4437 reachableFunctionThatWeAreIn = node ;
4538 }
@@ -54,7 +47,9 @@ function reachableDuringSSRPartial() {
5447 ! reachableFunctionThatWeAreIn &&
5548 node . parent . type === 'VariableDeclarator' &&
5649 node . parent . id . type === 'Identifier' &&
57- moduleInfo . moduleScopedFunctionsReachableDuringSSR . has ( node . parent . id . name )
50+ ( ( moduleInfo . isLWC &&
51+ moduleInfo . moduleScopedFunctionsReachableDuringSSR . has ( node . parent . id . name ) ) ||
52+ ! moduleInfo . isLWC )
5853 ) {
5954 reachableFunctionThatWeAreIn = node ;
6055 }
@@ -69,7 +64,9 @@ function reachableDuringSSRPartial() {
6964 ! reachableFunctionThatWeAreIn &&
7065 node . parent . type === 'VariableDeclarator' &&
7166 node . parent . id . type === 'Identifier' &&
72- moduleInfo . moduleScopedFunctionsReachableDuringSSR . has ( node . parent . id . name )
67+ ( ( moduleInfo . isLWC &&
68+ moduleInfo . moduleScopedFunctionsReachableDuringSSR . has ( node . parent . id . name ) ) ||
69+ ! moduleInfo . isLWC )
7370 ) {
7471 reachableFunctionThatWeAreIn = node ;
7572 }
@@ -81,9 +78,9 @@ function reachableDuringSSRPartial() {
8178 } ,
8279 MethodDefinition : ( node ) => {
8380 if (
84- insideLWC &&
8581 node . key . type === 'Identifier' &&
86- moduleInfo . methodsReachableDuringSSR . has ( node . key . name )
82+ ( ( moduleInfo . isLWC && moduleInfo . methodsReachableDuringSSR . has ( node . key . name ) ) ||
83+ ! moduleInfo . isLWC )
8784 ) {
8885 reachableMethodThatWeAreIn = node ;
8986 }
@@ -115,7 +112,7 @@ function reachableDuringSSRPartial() {
115112
116113 return {
117114 withinLWCVisitors,
118- isInsideReachableMethod : ( ) => insideLWC && ! ! reachableMethodThatWeAreIn ,
115+ isInsideReachableMethod : ( ) => ! ! reachableMethodThatWeAreIn ,
119116 isInsideReachableFunction : ( ) => ! ! reachableFunctionThatWeAreIn ,
120117 isInsideSkippedBlock : ( ) => ! ! skippedBlockThatWeAreIn || ! ! skippedConditionThatWeAreIn ,
121118 } ;
@@ -193,22 +190,6 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
193190 property : node . parent . property . name ,
194191 } ,
195192 } ) ;
196- } else if (
197- node . parent . type !== 'MemberExpression' &&
198- node . object . type === 'Identifier' &&
199- forbiddenGlobalNames . has ( node . object . name ) &&
200- isGlobalIdentifier ( node . object , context . getScope ( ) )
201- ) {
202- // Prevents expressions like:
203- // document.addEventListener('click', () => { ... });
204- // document?.addEventListener('click', () => { ... });
205- context . report ( {
206- messageId : messageIds . at ( 0 ) ,
207- node,
208- data : {
209- identifier : node . object . name ,
210- } ,
211- } ) ;
212193 } else if (
213194 node . parent . type === 'CallExpression' &&
214195 node . parent . optional !== true &&
@@ -233,6 +214,25 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
233214 : node . object . name ,
234215 } ,
235216 } ) ;
217+ } else if (
218+ node . parent . type !== 'MemberExpression' &&
219+ node . object . type === 'Identifier' &&
220+ forbiddenGlobalNames . has ( node . object . name ) &&
221+ isGlobalIdentifier ( node . object , context . getScope ( ) )
222+ ) {
223+ // Prevents expressions like:
224+ // window.addEventListener('click', () => { ... });
225+ // window?.addEventListener('click', () => { ... });
226+ // document.addEventListener('click', () => { ... });
227+ // document?.addEventListener('click', () => { ... });
228+ context . report ( {
229+ messageId : messageIds . at ( 0 ) ,
230+ node,
231+ data : {
232+ identifier :
233+ node . object . name === 'window' ? node . property . name : node . object . name ,
234+ } ,
235+ } ) ;
236236 }
237237 } ,
238238 Identifier : ( node ) => {
@@ -244,6 +244,7 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
244244 ) {
245245 return ;
246246 }
247+
247248 if (
248249 noReferenceParentQualifiers . has ( node . parent . type ) &&
249250 forbiddenGlobalNames . has ( node . name ) &&
@@ -255,6 +256,43 @@ module.exports.noReferenceDuringSSR = function noReferenceDuringSSR(
255256
256257 // Allows expressions like:
257258 // doSomethingWith(globalThis)
259+ context . report ( {
260+ messageId : messageIds . at ( 0 ) ,
261+ node,
262+ data : {
263+ identifier : node . name ,
264+ } ,
265+ } ) ;
266+ } else if (
267+ node . parent . type === 'BinaryExpression' &&
268+ node . parent . operator === 'instanceof' &&
269+ node . parent . right === node &&
270+ forbiddenGlobalNames . has ( node . name ) &&
271+ isGlobalIdentifier ( node , context . getScope ( ) )
272+ ) {
273+ // Prevents expressions like:
274+ // if (value instanceof Element) { ... }
275+ // value instanceof Element ? doX() : doY();
276+ context . report ( {
277+ messageId : messageIds . at ( 0 ) ,
278+ node,
279+ data : {
280+ identifier : node . name ,
281+ } ,
282+ } ) ;
283+ }
284+ } ,
285+ ExpressionStatement : ( node ) => {
286+ if (
287+ node . parent . type === 'Program' &&
288+ node . expression . type === 'CallExpression' &&
289+ node . expression . callee . type === 'Identifier' &&
290+ forbiddenGlobalNames . has ( node . expression . callee . name ) &&
291+ isGlobalIdentifier ( node , context . getScope ( ) )
292+ ) {
293+ // Prevents global expressions like:
294+ // addEventListener('resize', () => {...});
295+
258296 context . report ( {
259297 messageId : messageIds . at ( 0 ) ,
260298 node,
0 commit comments