@@ -15,9 +15,14 @@ const ConstDependency = require("./dependencies/ConstDependency");
1515const { evaluateToString } = require ( "./javascript/JavascriptParserHelpers" ) ;
1616const { parseResource } = require ( "./util/identifier" ) ;
1717
18- /** @typedef {import("estree").Expression } ExpressionNode */
19- /** @typedef {import("estree").Super } SuperNode */
18+ /** @typedef {import("estree").Expression } Expression */
19+ /** @typedef {import("estree").SourceLocation } SourceLocation */
20+ /** @typedef {import("estree").Statement } Statement */
21+ /** @typedef {import("estree").Super } Super */
2022/** @typedef {import("./Compiler") } Compiler */
23+ /** @typedef {import("./javascript/BasicEvaluatedExpression") } BasicEvaluatedExpression */
24+ /** @typedef {import("./javascript/JavascriptParser") } JavascriptParser */
25+ /** @typedef {import("./javascript/JavascriptParser").Range } Range */
2126
2227const collectDeclaration = ( declarations , pattern ) => {
2328 const stack = [ pattern ] ;
@@ -136,15 +141,21 @@ class ConstPlugin {
136141 new CachedConstDependency . Template ( )
137142 ) ;
138143
144+ /**
145+ * @param {JavascriptParser } parser the parser
146+ */
139147 const handler = parser => {
140148 parser . hooks . statementIf . tap ( PLUGIN_NAME , statement => {
141149 if ( parser . scope . isAsmJs ) return ;
142150 const param = parser . evaluateExpression ( statement . test ) ;
143151 const bool = param . asBool ( ) ;
144152 if ( typeof bool === "boolean" ) {
145153 if ( ! param . couldHaveSideEffects ( ) ) {
146- const dep = new ConstDependency ( `${ bool } ` , param . range ) ;
147- dep . loc = statement . loc ;
154+ const dep = new ConstDependency (
155+ `${ bool } ` ,
156+ /** @type {Range } */ ( param . range )
157+ ) ;
158+ dep . loc = /** @type {SourceLocation } */ ( statement . loc ) ;
148159 parser . state . module . addPresentationalDependency ( dep ) ;
149160 } else {
150161 parser . walkExpression ( statement . test ) ;
@@ -200,9 +211,9 @@ class ConstPlugin {
200211 }
201212 const dep = new ConstDependency (
202213 replacement ,
203- branchToRemove . range
214+ /** @type { Range } */ ( branchToRemove . range )
204215 ) ;
205- dep . loc = branchToRemove . loc ;
216+ dep . loc = /** @type { SourceLocation } */ ( branchToRemove . loc ) ;
206217 parser . state . module . addPresentationalDependency ( dep ) ;
207218 }
208219 return bool ;
@@ -216,8 +227,11 @@ class ConstPlugin {
216227 const bool = param . asBool ( ) ;
217228 if ( typeof bool === "boolean" ) {
218229 if ( ! param . couldHaveSideEffects ( ) ) {
219- const dep = new ConstDependency ( ` ${ bool } ` , param . range ) ;
220- dep . loc = expression . loc ;
230+ const dep = new ConstDependency (
231+ ` ${ bool } ` ,
232+ /** @type {Range } */ ( param . range )
233+ ) ;
234+ dep . loc = /** @type {SourceLocation } */ ( expression . loc ) ;
221235 parser . state . module . addPresentationalDependency ( dep ) ;
222236 } else {
223237 parser . walkExpression ( expression . test ) ;
@@ -236,8 +250,11 @@ class ConstPlugin {
236250 const branchToRemove = bool
237251 ? expression . alternate
238252 : expression . consequent ;
239- const dep = new ConstDependency ( "0" , branchToRemove . range ) ;
240- dep . loc = branchToRemove . loc ;
253+ const dep = new ConstDependency (
254+ "0" ,
255+ /** @type {Range } */ ( branchToRemove . range )
256+ ) ;
257+ dep . loc = /** @type {SourceLocation } */ ( branchToRemove . loc ) ;
241258 parser . state . module . addPresentationalDependency ( dep ) ;
242259 return bool ;
243260 }
@@ -313,18 +330,21 @@ class ConstPlugin {
313330 //
314331 // returnfalse&&'foo'
315332 //
316- const dep = new ConstDependency ( ` ${ bool } ` , param . range ) ;
317- dep . loc = expression . loc ;
333+ const dep = new ConstDependency (
334+ ` ${ bool } ` ,
335+ /** @type {Range } */ ( param . range )
336+ ) ;
337+ dep . loc = /** @type {SourceLocation } */ ( expression . loc ) ;
318338 parser . state . module . addPresentationalDependency ( dep ) ;
319339 } else {
320340 parser . walkExpression ( expression . left ) ;
321341 }
322342 if ( ! keepRight ) {
323343 const dep = new ConstDependency (
324344 "0" ,
325- expression . right . range
345+ /** @type { Range } */ ( expression . right . range )
326346 ) ;
327- dep . loc = expression . loc ;
347+ dep . loc = /** @type { SourceLocation } */ ( expression . loc ) ;
328348 parser . state . module . addPresentationalDependency ( dep ) ;
329349 }
330350 return keepRight ;
@@ -363,15 +383,18 @@ class ConstPlugin {
363383 //
364384 // returnnull??'foo'
365385 //
366- const dep = new ConstDependency ( " null" , param . range ) ;
367- dep . loc = expression . loc ;
386+ const dep = new ConstDependency (
387+ " null" ,
388+ /** @type {Range } */ ( param . range )
389+ ) ;
390+ dep . loc = /** @type {SourceLocation } */ ( expression . loc ) ;
368391 parser . state . module . addPresentationalDependency ( dep ) ;
369392 } else {
370393 const dep = new ConstDependency (
371394 "0" ,
372- expression . right . range
395+ /** @type { Range } */ ( expression . right . range )
373396 ) ;
374- dep . loc = expression . loc ;
397+ dep . loc = /** @type { SourceLocation } */ ( expression . loc ) ;
375398 parser . state . module . addPresentationalDependency ( dep ) ;
376399 parser . walkExpression ( expression . left ) ;
377400 }
@@ -382,9 +405,9 @@ class ConstPlugin {
382405 }
383406 ) ;
384407 parser . hooks . optionalChaining . tap ( PLUGIN_NAME , expr => {
385- /** @type {ExpressionNode [] } */
408+ /** @type {Expression [] } */
386409 const optionalExpressionsStack = [ ] ;
387- /** @type {ExpressionNode|SuperNode } */
410+ /** @type {Expression | Super } */
388411 let next = expr . expression ;
389412
390413 while (
@@ -395,15 +418,15 @@ class ConstPlugin {
395418 if ( next . optional ) {
396419 // SuperNode can not be optional
397420 optionalExpressionsStack . push (
398- /** @type {ExpressionNode } */ ( next . object )
421+ /** @type {Expression } */ ( next . object )
399422 ) ;
400423 }
401424 next = next . object ;
402425 } else {
403426 if ( next . optional ) {
404427 // SuperNode can not be optional
405428 optionalExpressionsStack . push (
406- /** @type {ExpressionNode } */ ( next . callee )
429+ /** @type {Expression } */ ( next . callee )
407430 ) ;
408431 }
409432 next = next . callee ;
@@ -412,7 +435,9 @@ class ConstPlugin {
412435
413436 while ( optionalExpressionsStack . length ) {
414437 const expression = optionalExpressionsStack . pop ( ) ;
415- const evaluated = parser . evaluateExpression ( expression ) ;
438+ const evaluated = parser . evaluateExpression (
439+ /** @type {Expression } */ ( expression )
440+ ) ;
416441
417442 if ( evaluated . asNullish ( ) ) {
418443 // ------------------------------------------
@@ -427,8 +452,11 @@ class ConstPlugin {
427452 //
428453 // ------------------------------------------
429454 //
430- const dep = new ConstDependency ( " undefined" , expr . range ) ;
431- dep . loc = expr . loc ;
455+ const dep = new ConstDependency (
456+ " undefined" ,
457+ /** @type {Range } */ ( expr . range )
458+ ) ;
459+ dep . loc = /** @type {SourceLocation } */ ( expr . loc ) ;
432460 parser . state . module . addPresentationalDependency ( dep ) ;
433461 return true ;
434462 }
@@ -452,10 +480,10 @@ class ConstPlugin {
452480 JSON . stringify (
453481 cachedParseResource ( parser . state . module . resource ) . query
454482 ) ,
455- expr . range ,
483+ /** @type { Range } */ ( expr . range ) ,
456484 "__resourceQuery"
457485 ) ;
458- dep . loc = expr . loc ;
486+ dep . loc = /** @type { SourceLocation } */ ( expr . loc ) ;
459487 parser . state . module . addPresentationalDependency ( dep ) ;
460488 return true ;
461489 } ) ;
@@ -478,10 +506,10 @@ class ConstPlugin {
478506 JSON . stringify (
479507 cachedParseResource ( parser . state . module . resource ) . fragment
480508 ) ,
481- expr . range ,
509+ /** @type { Range } */ ( expr . range ) ,
482510 "__resourceFragment"
483511 ) ;
484- dep . loc = expr . loc ;
512+ dep . loc = /** @type { SourceLocation } */ ( expr . loc ) ;
485513 parser . state . module . addPresentationalDependency ( dep ) ;
486514 return true ;
487515 } ) ;
0 commit comments