JavascriptParser.js 155 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const vm = require("vm");
  7. const { Parser: AcornParser, tokTypes } = require("acorn");
  8. const { HookMap, SyncBailHook } = require("tapable");
  9. const Parser = require("../Parser");
  10. const StackedMap = require("../util/StackedMap");
  11. const binarySearchBounds = require("../util/binarySearchBounds");
  12. const {
  13. createMagicCommentContext,
  14. webpackCommentRegExp
  15. } = require("../util/magicComment");
  16. const memoize = require("../util/memoize");
  17. const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
  18. /** @typedef {import("acorn").Options} AcornOptions */
  19. /** @typedef {import("estree").AssignmentExpression} AssignmentExpression */
  20. /** @typedef {import("estree").BinaryExpression} BinaryExpression */
  21. /** @typedef {import("estree").BlockStatement} BlockStatement */
  22. /** @typedef {import("estree").SequenceExpression} SequenceExpression */
  23. /** @typedef {import("estree").CallExpression} CallExpression */
  24. /** @typedef {import("estree").BaseCallExpression} BaseCallExpression */
  25. /** @typedef {import("estree").StaticBlock} StaticBlock */
  26. /** @typedef {import("estree").ClassDeclaration} ClassDeclaration */
  27. /** @typedef {import("estree").ForStatement} ForStatement */
  28. /** @typedef {import("estree").SwitchStatement} SwitchStatement */
  29. /** @typedef {import("estree").ClassExpression} ClassExpression */
  30. /** @typedef {import("estree").Comment} Comment */
  31. /** @typedef {import("estree").ConditionalExpression} ConditionalExpression */
  32. /** @typedef {import("estree").Declaration} Declaration */
  33. /** @typedef {import("estree").PrivateIdentifier} PrivateIdentifier */
  34. /** @typedef {import("estree").PropertyDefinition} PropertyDefinition */
  35. /** @typedef {import("estree").Expression} Expression */
  36. /** @typedef {import("estree").Identifier} Identifier */
  37. /** @typedef {import("estree").VariableDeclaration} VariableDeclaration */
  38. /** @typedef {import("estree").IfStatement} IfStatement */
  39. /** @typedef {import("estree").LabeledStatement} LabeledStatement */
  40. /** @typedef {import("estree").Literal} Literal */
  41. /** @typedef {import("estree").LogicalExpression} LogicalExpression */
  42. /** @typedef {import("estree").ChainExpression} ChainExpression */
  43. /** @typedef {import("estree").MemberExpression} MemberExpression */
  44. /** @typedef {import("estree").YieldExpression} YieldExpression */
  45. /** @typedef {import("estree").MetaProperty} MetaProperty */
  46. /** @typedef {import("estree").Property} Property */
  47. /** @typedef {import("estree").AssignmentPattern} AssignmentPattern */
  48. /** @typedef {import("estree").ChainElement} ChainElement */
  49. /** @typedef {import("estree").Pattern} Pattern */
  50. /** @typedef {import("estree").UpdateExpression} UpdateExpression */
  51. /** @typedef {import("estree").ObjectExpression} ObjectExpression */
  52. /** @typedef {import("estree").UnaryExpression} UnaryExpression */
  53. /** @typedef {import("estree").ArrayExpression} ArrayExpression */
  54. /** @typedef {import("estree").ArrayPattern} ArrayPattern */
  55. /** @typedef {import("estree").AwaitExpression} AwaitExpression */
  56. /** @typedef {import("estree").ThisExpression} ThisExpression */
  57. /** @typedef {import("estree").RestElement} RestElement */
  58. /** @typedef {import("estree").ObjectPattern} ObjectPattern */
  59. /** @typedef {import("estree").SwitchCase} SwitchCase */
  60. /** @typedef {import("estree").CatchClause} CatchClause */
  61. /** @typedef {import("estree").VariableDeclarator} VariableDeclarator */
  62. /** @typedef {import("estree").ForInStatement} ForInStatement */
  63. /** @typedef {import("estree").ForOfStatement} ForOfStatement */
  64. /** @typedef {import("estree").ReturnStatement} ReturnStatement */
  65. /** @typedef {import("estree").WithStatement} WithStatement */
  66. /** @typedef {import("estree").ThrowStatement} ThrowStatement */
  67. /** @typedef {import("estree").MethodDefinition} MethodDefinition */
  68. /** @typedef {import("estree").NewExpression} NewExpression */
  69. /** @typedef {import("estree").SpreadElement} SpreadElement */
  70. /** @typedef {import("estree").FunctionExpression} FunctionExpression */
  71. /** @typedef {import("estree").WhileStatement} WhileStatement */
  72. /** @typedef {import("estree").ArrowFunctionExpression} ArrowFunctionExpression */
  73. /** @typedef {import("estree").ExpressionStatement} ExpressionStatement */
  74. /** @typedef {import("estree").FunctionDeclaration} FunctionDeclaration */
  75. /** @typedef {import("estree").DoWhileStatement} DoWhileStatement */
  76. /** @typedef {import("estree").TryStatement} TryStatement */
  77. /** @typedef {import("estree").Node} Node */
  78. /** @typedef {import("estree").Program} Program */
  79. /** @typedef {import("estree").Directive} Directive */
  80. /** @typedef {import("estree").Statement} Statement */
  81. /** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclaration */
  82. /** @typedef {import("estree").Super} Super */
  83. /** @typedef {import("estree").ImportSpecifier} ImportSpecifier */
  84. /** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpression */
  85. /** @typedef {import("estree").TemplateLiteral} TemplateLiteral */
  86. /** @typedef {import("estree").AssignmentProperty} AssignmentProperty */
  87. /** @typedef {import("estree").MaybeNamedFunctionDeclaration} MaybeNamedFunctionDeclaration */
  88. /** @typedef {import("estree").MaybeNamedClassDeclaration} MaybeNamedClassDeclaration */
  89. /**
  90. * @template T
  91. * @typedef {import("tapable").AsArray<T>} AsArray<T>
  92. */
  93. /** @typedef {import("../Parser").ParserState} ParserState */
  94. /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
  95. /** @typedef {{ name: string | VariableInfo, rootInfo: string | VariableInfo, getMembers: () => string[], getMembersOptionals: () => boolean[], getMemberRanges: () => Range[] }} GetInfoResult */
  96. /** @typedef {Statement | ModuleDeclaration | Expression | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} StatementPathItem */
  97. /** @typedef {(ident: string) => void} OnIdentString */
  98. /** @typedef {(ident: string, identifier: Identifier) => void} OnIdent */
  99. /** @typedef {StatementPathItem[]} StatementPath */
  100. // TODO remove cast when @types/estree has been updated to import assertions
  101. /** @typedef {import("estree").BaseNode & { type: "ImportAttribute", key: Identifier | Literal, value: Literal }} ImportAttribute */
  102. /** @typedef {import("estree").ImportDeclaration & { attributes?: Array<ImportAttribute>, phase?: "defer" }} ImportDeclaration */
  103. /** @typedef {import("estree").ExportNamedDeclaration & { attributes?: Array<ImportAttribute> }} ExportNamedDeclaration */
  104. /** @typedef {import("estree").ExportAllDeclaration & { attributes?: Array<ImportAttribute> }} ExportAllDeclaration */
  105. /** @typedef {import("estree").ImportExpression & { options?: Expression | null, phase?: "defer" }} ImportExpression */
  106. /** @typedef {ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration | ExportAllDeclaration} ModuleDeclaration */
  107. /** @type {string[]} */
  108. const EMPTY_ARRAY = [];
  109. const ALLOWED_MEMBER_TYPES_CALL_EXPRESSION = 0b01;
  110. const ALLOWED_MEMBER_TYPES_EXPRESSION = 0b10;
  111. const ALLOWED_MEMBER_TYPES_ALL = 0b11;
  112. const LEGACY_ASSERT_ATTRIBUTES = Symbol("assert");
  113. /** @type {(BaseParser: typeof AcornParser) => typeof AcornParser} */
  114. const importAssertions = (Parser) =>
  115. class extends Parser {
  116. /**
  117. * @this {InstanceType<AcornParser>}
  118. * @returns {ImportAttribute[]} import attributes
  119. */
  120. parseWithClause() {
  121. /** @type {ImportAttribute[]} */
  122. const nodes = [];
  123. const isAssertLegacy = this.value === "assert";
  124. if (isAssertLegacy) {
  125. if (!this.eat(tokTypes.name)) {
  126. return nodes;
  127. }
  128. } else if (!this.eat(tokTypes._with)) {
  129. return nodes;
  130. }
  131. this.expect(tokTypes.braceL);
  132. /** @type {Record<string, boolean>} */
  133. const attributeKeys = {};
  134. let first = true;
  135. while (!this.eat(tokTypes.braceR)) {
  136. if (!first) {
  137. this.expect(tokTypes.comma);
  138. if (this.afterTrailingComma(tokTypes.braceR)) {
  139. break;
  140. }
  141. } else {
  142. first = false;
  143. }
  144. const attr =
  145. /** @type {ImportAttribute} */
  146. this.parseImportAttribute();
  147. const keyName =
  148. attr.key.type === "Identifier" ? attr.key.name : attr.key.value;
  149. if (Object.prototype.hasOwnProperty.call(attributeKeys, keyName)) {
  150. this.raiseRecoverable(
  151. attr.key.start,
  152. `Duplicate attribute key '${keyName}'`
  153. );
  154. }
  155. attributeKeys[keyName] = true;
  156. nodes.push(attr);
  157. }
  158. if (isAssertLegacy) {
  159. /** @type {EXPECTED_ANY} */
  160. (nodes)[LEGACY_ASSERT_ATTRIBUTES] = true;
  161. }
  162. return nodes;
  163. }
  164. };
  165. // Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
  166. let parser = AcornParser.extend(importAssertions);
  167. /** @typedef {Record<string, string> & { _isLegacyAssert?: boolean }} ImportAttributes */
  168. /**
  169. * @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | ImportExpression} node node with assertions
  170. * @returns {ImportAttributes | undefined} import attributes
  171. */
  172. const getImportAttributes = (node) => {
  173. if (node.type === "ImportExpression") {
  174. if (
  175. node.options &&
  176. node.options.type === "ObjectExpression" &&
  177. node.options.properties[0] &&
  178. node.options.properties[0].type === "Property" &&
  179. node.options.properties[0].key.type === "Identifier" &&
  180. (node.options.properties[0].key.name === "with" ||
  181. node.options.properties[0].key.name === "assert") &&
  182. node.options.properties[0].value.type === "ObjectExpression" &&
  183. node.options.properties[0].value.properties.length > 0
  184. ) {
  185. const properties =
  186. /** @type {Property[]} */
  187. (node.options.properties[0].value.properties);
  188. const result = /** @type {ImportAttributes} */ ({});
  189. for (const property of properties) {
  190. const key =
  191. /** @type {string} */
  192. (
  193. property.key.type === "Identifier"
  194. ? property.key.name
  195. : /** @type {Literal} */ (property.key).value
  196. );
  197. result[key] =
  198. /** @type {string} */
  199. (/** @type {Literal} */ (property.value).value);
  200. }
  201. const key =
  202. node.options.properties[0].key.type === "Identifier"
  203. ? node.options.properties[0].key.name
  204. : /** @type {Literal} */ (node.options.properties[0].key).value;
  205. if (key === "assert") {
  206. result._isLegacyAssert = true;
  207. }
  208. return result;
  209. }
  210. return;
  211. }
  212. if (node.attributes === undefined || node.attributes.length === 0) {
  213. return;
  214. }
  215. const result = /** @type {ImportAttributes} */ ({});
  216. for (const attribute of node.attributes) {
  217. const key =
  218. /** @type {string} */
  219. (
  220. attribute.key.type === "Identifier"
  221. ? attribute.key.name
  222. : attribute.key.value
  223. );
  224. result[key] = /** @type {string} */ (attribute.value.value);
  225. }
  226. if (/** @type {EXPECTED_ANY} */ (node.attributes)[LEGACY_ASSERT_ATTRIBUTES]) {
  227. result._isLegacyAssert = true;
  228. }
  229. return result;
  230. };
  231. /** @typedef {typeof VariableInfoFlags.Evaluated | typeof VariableInfoFlags.Free | typeof VariableInfoFlags.Normal | typeof VariableInfoFlags.Tagged} VariableInfoFlagsType */
  232. const VariableInfoFlags = Object.freeze({
  233. Evaluated: 0b000,
  234. Free: 0b001,
  235. Normal: 0b010,
  236. Tagged: 0b100
  237. });
  238. class VariableInfo {
  239. /**
  240. * @param {ScopeInfo} declaredScope scope in which the variable is declared
  241. * @param {string | undefined} name which name the variable use, defined name or free name or tagged name
  242. * @param {VariableInfoFlagsType} flags how the variable is created
  243. * @param {TagInfo | undefined} tagInfo info about tags
  244. */
  245. constructor(declaredScope, name, flags, tagInfo) {
  246. this.declaredScope = declaredScope;
  247. this.name = name;
  248. this.flags = flags;
  249. this.tagInfo = tagInfo;
  250. }
  251. /**
  252. * @returns {boolean} the variable is free or not
  253. */
  254. isFree() {
  255. return (this.flags & VariableInfoFlags.Free) > 0;
  256. }
  257. /**
  258. * @returns {boolean} the variable is tagged by tagVariable or not
  259. */
  260. isTagged() {
  261. return (this.flags & VariableInfoFlags.Tagged) > 0;
  262. }
  263. }
  264. /** @typedef {string | ScopeInfo | VariableInfo} ExportedVariableInfo */
  265. /** @typedef {Literal | string | null | undefined} ImportSource */
  266. /** @typedef {Omit<AcornOptions, "sourceType" | "ecmaVersion"> & { sourceType: "module" | "script" | "auto", ecmaVersion?: AcornOptions["ecmaVersion"] }} ParseOptions */
  267. /** @typedef {symbol} Tag */
  268. /** @typedef {Record<string, TODO>} TagData */
  269. /**
  270. * @typedef {object} TagInfo
  271. * @property {Tag} tag
  272. * @property {TagData=} data
  273. * @property {TagInfo | undefined} next
  274. */
  275. const SCOPE_INFO_TERMINATED_RETURN = 1;
  276. const SCOPE_INFO_TERMINATED_THROW = 2;
  277. /**
  278. * @typedef {object} ScopeInfo
  279. * @property {StackedMap<string, VariableInfo | ScopeInfo>} definitions
  280. * @property {boolean | "arrow"} topLevelScope
  281. * @property {boolean | string} inShorthand
  282. * @property {boolean} inTaggedTemplateTag
  283. * @property {boolean} inTry
  284. * @property {boolean} isStrict
  285. * @property {boolean} isAsmJs
  286. * @property {undefined | 1 | 2} terminated
  287. */
  288. /** @typedef {[number, number]} Range */
  289. /**
  290. * @typedef {object} DestructuringAssignmentProperty
  291. * @property {string} id
  292. * @property {Range | undefined=} range
  293. * @property {boolean | string} shorthand
  294. */
  295. /**
  296. * Helper function for joining two ranges into a single range. This is useful
  297. * when working with AST nodes, as it allows you to combine the ranges of child nodes
  298. * to create the range of the _parent node_.
  299. * @param {Range} startRange start range to join
  300. * @param {Range} endRange end range to join
  301. * @returns {Range} joined range
  302. * @example
  303. * ```js
  304. * const startRange = [0, 5];
  305. * const endRange = [10, 15];
  306. * const joinedRange = joinRanges(startRange, endRange);
  307. * console.log(joinedRange); // [0, 15]
  308. * ```
  309. */
  310. const joinRanges = (startRange, endRange) => {
  311. if (!endRange) return startRange;
  312. if (!startRange) return endRange;
  313. return [startRange[0], endRange[1]];
  314. };
  315. /**
  316. * Helper function used to generate a string representation of a
  317. * [member expression](https://github.com/estree/estree/blob/master/es5.md#memberexpression).
  318. * @param {string} object object to name
  319. * @param {string[]} membersReversed reversed list of members
  320. * @returns {string} member expression as a string
  321. * @example
  322. * ```js
  323. * const membersReversed = ["property1", "property2", "property3"]; // Members parsed from the AST
  324. * const name = objectAndMembersToName("myObject", membersReversed);
  325. *
  326. * console.log(name); // "myObject.property1.property2.property3"
  327. * ```
  328. */
  329. const objectAndMembersToName = (object, membersReversed) => {
  330. let name = object;
  331. for (let i = membersReversed.length - 1; i >= 0; i--) {
  332. name = `${name}.${membersReversed[i]}`;
  333. }
  334. return name;
  335. };
  336. /**
  337. * Grabs the name of a given expression and returns it as a string or undefined. Has particular
  338. * handling for [Identifiers](https://github.com/estree/estree/blob/master/es5.md#identifier),
  339. * [ThisExpressions](https://github.com/estree/estree/blob/master/es5.md#identifier), and
  340. * [MetaProperties](https://github.com/estree/estree/blob/master/es2015.md#metaproperty) which is
  341. * specifically for handling the `new.target` meta property.
  342. * @param {Expression | SpreadElement | Super} expression expression
  343. * @returns {string | "this" | undefined} name or variable info
  344. */
  345. const getRootName = (expression) => {
  346. switch (expression.type) {
  347. case "Identifier":
  348. return expression.name;
  349. case "ThisExpression":
  350. return "this";
  351. case "MetaProperty":
  352. return `${expression.meta.name}.${expression.property.name}`;
  353. default:
  354. return undefined;
  355. }
  356. };
  357. /** @type {AcornOptions} */
  358. const defaultParserOptions = {
  359. ranges: true,
  360. locations: true,
  361. ecmaVersion: "latest",
  362. sourceType: "module",
  363. // https://github.com/tc39/proposal-hashbang
  364. allowHashBang: true,
  365. onComment: undefined
  366. };
  367. const EMPTY_COMMENT_OPTIONS = {
  368. options: null,
  369. errors: null
  370. };
  371. const CLASS_NAME = "JavascriptParser";
  372. class JavascriptParser extends Parser {
  373. /**
  374. * @param {"module" | "script" | "auto"} sourceType default source type
  375. */
  376. constructor(sourceType = "auto") {
  377. super();
  378. this.hooks = Object.freeze({
  379. /** @type {HookMap<SyncBailHook<[UnaryExpression], BasicEvaluatedExpression | null | undefined>>} */
  380. evaluateTypeof: new HookMap(() => new SyncBailHook(["expression"])),
  381. /** @type {HookMap<SyncBailHook<[Expression | SpreadElement | PrivateIdentifier | Super], BasicEvaluatedExpression | null | undefined>>} */
  382. evaluate: new HookMap(() => new SyncBailHook(["expression"])),
  383. /** @type {HookMap<SyncBailHook<[Identifier | ThisExpression | MemberExpression | MetaProperty], BasicEvaluatedExpression | null | undefined>>} */
  384. evaluateIdentifier: new HookMap(() => new SyncBailHook(["expression"])),
  385. /** @type {HookMap<SyncBailHook<[Identifier | ThisExpression | MemberExpression], BasicEvaluatedExpression | null | undefined>>} */
  386. evaluateDefinedIdentifier: new HookMap(
  387. () => new SyncBailHook(["expression"])
  388. ),
  389. /** @type {HookMap<SyncBailHook<[NewExpression], BasicEvaluatedExpression | null | undefined>>} */
  390. evaluateNewExpression: new HookMap(
  391. () => new SyncBailHook(["expression"])
  392. ),
  393. /** @type {HookMap<SyncBailHook<[CallExpression], BasicEvaluatedExpression | null | undefined>>} */
  394. evaluateCallExpression: new HookMap(
  395. () => new SyncBailHook(["expression"])
  396. ),
  397. /** @type {HookMap<SyncBailHook<[CallExpression, BasicEvaluatedExpression], BasicEvaluatedExpression | null | undefined>>} */
  398. evaluateCallExpressionMember: new HookMap(
  399. () => new SyncBailHook(["expression", "param"])
  400. ),
  401. /** @type {HookMap<SyncBailHook<[Expression | Declaration | PrivateIdentifier | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration, number], boolean | void>>} */
  402. isPure: new HookMap(
  403. () => new SyncBailHook(["expression", "commentsStartPosition"])
  404. ),
  405. /** @type {SyncBailHook<[Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration], boolean | void>} */
  406. preStatement: new SyncBailHook(["statement"]),
  407. /** @type {SyncBailHook<[Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration], boolean | void>} */
  408. blockPreStatement: new SyncBailHook(["declaration"]),
  409. /** @type {SyncBailHook<[Statement | ModuleDeclaration | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
  410. statement: new SyncBailHook(["statement"]),
  411. /** @type {SyncBailHook<[IfStatement], boolean | void>} */
  412. statementIf: new SyncBailHook(["statement"]),
  413. /** @type {SyncBailHook<[Expression, ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
  414. classExtendsExpression: new SyncBailHook([
  415. "expression",
  416. "classDefinition"
  417. ]),
  418. /** @type {SyncBailHook<[MethodDefinition | PropertyDefinition | StaticBlock, ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
  419. classBodyElement: new SyncBailHook(["element", "classDefinition"]),
  420. /** @type {SyncBailHook<[Expression, MethodDefinition | PropertyDefinition, ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
  421. classBodyValue: new SyncBailHook([
  422. "expression",
  423. "element",
  424. "classDefinition"
  425. ]),
  426. /** @type {HookMap<SyncBailHook<[LabeledStatement], boolean | void>>} */
  427. label: new HookMap(() => new SyncBailHook(["statement"])),
  428. /** @type {SyncBailHook<[ImportDeclaration, ImportSource], boolean | void>} */
  429. import: new SyncBailHook(["statement", "source"]),
  430. /** @type {SyncBailHook<[ImportDeclaration, ImportSource, string | null, string], boolean | void>} */
  431. importSpecifier: new SyncBailHook([
  432. "statement",
  433. "source",
  434. "exportName",
  435. "identifierName"
  436. ]),
  437. /** @type {SyncBailHook<[ExportDefaultDeclaration | ExportNamedDeclaration], boolean | void>} */
  438. export: new SyncBailHook(["statement"]),
  439. /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, ImportSource], boolean | void>} */
  440. exportImport: new SyncBailHook(["statement", "source"]),
  441. /** @type {SyncBailHook<[ExportDefaultDeclaration | ExportNamedDeclaration | ExportAllDeclaration, Declaration], boolean | void>} */
  442. exportDeclaration: new SyncBailHook(["statement", "declaration"]),
  443. /** @type {SyncBailHook<[ExportDefaultDeclaration, MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | Expression], boolean | void>} */
  444. exportExpression: new SyncBailHook(["statement", "node"]),
  445. /** @type {SyncBailHook<[ExportDefaultDeclaration | ExportNamedDeclaration | ExportAllDeclaration, string, string, number | undefined], boolean | void>} */
  446. exportSpecifier: new SyncBailHook([
  447. "statement",
  448. "identifierName",
  449. "exportName",
  450. "index"
  451. ]),
  452. /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, ImportSource, string | null, string | null, number | undefined], boolean | void>} */
  453. exportImportSpecifier: new SyncBailHook([
  454. "statement",
  455. "source",
  456. "identifierName",
  457. "exportName",
  458. "index"
  459. ]),
  460. /** @type {SyncBailHook<[VariableDeclarator, Statement], boolean | void>} */
  461. preDeclarator: new SyncBailHook(["declarator", "statement"]),
  462. /** @type {SyncBailHook<[VariableDeclarator, Statement], boolean | void>} */
  463. declarator: new SyncBailHook(["declarator", "statement"]),
  464. /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
  465. varDeclaration: new HookMap(() => new SyncBailHook(["declaration"])),
  466. /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
  467. varDeclarationLet: new HookMap(() => new SyncBailHook(["declaration"])),
  468. /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
  469. varDeclarationConst: new HookMap(() => new SyncBailHook(["declaration"])),
  470. /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
  471. varDeclarationUsing: new HookMap(() => new SyncBailHook(["declaration"])),
  472. /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
  473. varDeclarationVar: new HookMap(() => new SyncBailHook(["declaration"])),
  474. /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
  475. pattern: new HookMap(() => new SyncBailHook(["pattern"])),
  476. /** @type {SyncBailHook<[Expression], boolean | void>} */
  477. collectDestructuringAssignmentProperties: new SyncBailHook([
  478. "expression"
  479. ]),
  480. /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
  481. canRename: new HookMap(() => new SyncBailHook(["initExpression"])),
  482. /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
  483. rename: new HookMap(() => new SyncBailHook(["initExpression"])),
  484. /** @type {HookMap<SyncBailHook<[AssignmentExpression], boolean | void>>} */
  485. assign: new HookMap(() => new SyncBailHook(["expression"])),
  486. /** @type {HookMap<SyncBailHook<[AssignmentExpression, string[]], boolean | void>>} */
  487. assignMemberChain: new HookMap(
  488. () => new SyncBailHook(["expression", "members"])
  489. ),
  490. /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
  491. typeof: new HookMap(() => new SyncBailHook(["expression"])),
  492. /** @type {SyncBailHook<[ImportExpression], boolean | void>} */
  493. importCall: new SyncBailHook(["expression"]),
  494. /** @type {SyncBailHook<[Expression | ForOfStatement], boolean | void>} */
  495. topLevelAwait: new SyncBailHook(["expression"]),
  496. /** @type {HookMap<SyncBailHook<[CallExpression], boolean | void>>} */
  497. call: new HookMap(() => new SyncBailHook(["expression"])),
  498. /** Something like "a.b()" */
  499. /** @type {HookMap<SyncBailHook<[CallExpression, string[], boolean[], Range[]], boolean | void>>} */
  500. callMemberChain: new HookMap(
  501. () =>
  502. new SyncBailHook([
  503. "expression",
  504. "members",
  505. "membersOptionals",
  506. "memberRanges"
  507. ])
  508. ),
  509. /** Something like "a.b().c.d" */
  510. /** @type {HookMap<SyncBailHook<[Expression, string[], CallExpression, string[], Range[]], boolean | void>>} */
  511. memberChainOfCallMemberChain: new HookMap(
  512. () =>
  513. new SyncBailHook([
  514. "expression",
  515. "calleeMembers",
  516. "callExpression",
  517. "members",
  518. "memberRanges"
  519. ])
  520. ),
  521. /** Something like "a.b().c.d()"" */
  522. /** @type {HookMap<SyncBailHook<[CallExpression, string[], CallExpression, string[], Range[]], boolean | void>>} */
  523. callMemberChainOfCallMemberChain: new HookMap(
  524. () =>
  525. new SyncBailHook([
  526. "expression",
  527. "calleeMembers",
  528. "innerCallExpression",
  529. "members",
  530. "memberRanges"
  531. ])
  532. ),
  533. /** @type {SyncBailHook<[ChainExpression], boolean | void>} */
  534. optionalChaining: new SyncBailHook(["optionalChaining"]),
  535. /** @type {HookMap<SyncBailHook<[NewExpression], boolean | void>>} */
  536. new: new HookMap(() => new SyncBailHook(["expression"])),
  537. /** @type {SyncBailHook<[BinaryExpression], boolean | void>} */
  538. binaryExpression: new SyncBailHook(["binaryExpression"]),
  539. /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
  540. expression: new HookMap(() => new SyncBailHook(["expression"])),
  541. /** @type {HookMap<SyncBailHook<[MemberExpression, string[], boolean[], Range[]], boolean | void>>} */
  542. expressionMemberChain: new HookMap(
  543. () =>
  544. new SyncBailHook([
  545. "expression",
  546. "members",
  547. "membersOptionals",
  548. "memberRanges"
  549. ])
  550. ),
  551. /** @type {HookMap<SyncBailHook<[MemberExpression, string[]], boolean | void>>} */
  552. unhandledExpressionMemberChain: new HookMap(
  553. () => new SyncBailHook(["expression", "members"])
  554. ),
  555. /** @type {SyncBailHook<[ConditionalExpression], boolean | void>} */
  556. expressionConditionalOperator: new SyncBailHook(["expression"]),
  557. /** @type {SyncBailHook<[LogicalExpression], boolean | void>} */
  558. expressionLogicalOperator: new SyncBailHook(["expression"]),
  559. /** @type {SyncBailHook<[Program, Comment[]], boolean | void>} */
  560. program: new SyncBailHook(["ast", "comments"]),
  561. /** @type {SyncBailHook<[ThrowStatement | ReturnStatement], boolean | void>} */
  562. terminate: new SyncBailHook(["statement"]),
  563. /** @type {SyncBailHook<[Program, Comment[]], boolean | void>} */
  564. finish: new SyncBailHook(["ast", "comments"]),
  565. /** @type {SyncBailHook<[Statement], boolean | void>} */
  566. unusedStatement: new SyncBailHook(["statement"])
  567. });
  568. this.sourceType = sourceType;
  569. /** @type {ScopeInfo} */
  570. this.scope = /** @type {TODO} */ (undefined);
  571. /** @type {ParserState} */
  572. this.state = /** @type {TODO} */ (undefined);
  573. /** @type {Comment[] | undefined} */
  574. this.comments = undefined;
  575. /** @type {Set<number> | undefined} */
  576. this.semicolons = undefined;
  577. /** @type {StatementPath | undefined} */
  578. this.statementPath = undefined;
  579. /** @type {Statement | ModuleDeclaration | Expression | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | undefined} */
  580. this.prevStatement = undefined;
  581. /** @type {WeakMap<Expression, Set<DestructuringAssignmentProperty>> | undefined} */
  582. this.destructuringAssignmentProperties = undefined;
  583. /** @type {TagData | undefined} */
  584. this.currentTagData = undefined;
  585. this.magicCommentContext = createMagicCommentContext();
  586. this._initializeEvaluating();
  587. }
  588. _initializeEvaluating() {
  589. this.hooks.evaluate.for("Literal").tap(CLASS_NAME, (_expr) => {
  590. const expr = /** @type {Literal} */ (_expr);
  591. switch (typeof expr.value) {
  592. case "number":
  593. return new BasicEvaluatedExpression()
  594. .setNumber(expr.value)
  595. .setRange(/** @type {Range} */ (expr.range));
  596. case "bigint":
  597. return new BasicEvaluatedExpression()
  598. .setBigInt(expr.value)
  599. .setRange(/** @type {Range} */ (expr.range));
  600. case "string":
  601. return new BasicEvaluatedExpression()
  602. .setString(expr.value)
  603. .setRange(/** @type {Range} */ (expr.range));
  604. case "boolean":
  605. return new BasicEvaluatedExpression()
  606. .setBoolean(expr.value)
  607. .setRange(/** @type {Range} */ (expr.range));
  608. }
  609. if (expr.value === null) {
  610. return new BasicEvaluatedExpression()
  611. .setNull()
  612. .setRange(/** @type {Range} */ (expr.range));
  613. }
  614. if (expr.value instanceof RegExp) {
  615. return new BasicEvaluatedExpression()
  616. .setRegExp(expr.value)
  617. .setRange(/** @type {Range} */ (expr.range));
  618. }
  619. });
  620. this.hooks.evaluate.for("NewExpression").tap(CLASS_NAME, (_expr) => {
  621. const expr = /** @type {NewExpression} */ (_expr);
  622. const callee = expr.callee;
  623. if (callee.type !== "Identifier") return;
  624. if (callee.name !== "RegExp") {
  625. return this.callHooksForName(
  626. this.hooks.evaluateNewExpression,
  627. callee.name,
  628. expr
  629. );
  630. } else if (
  631. expr.arguments.length > 2 ||
  632. this.getVariableInfo("RegExp") !== "RegExp"
  633. ) {
  634. return;
  635. }
  636. let regExp;
  637. const arg1 = expr.arguments[0];
  638. if (arg1) {
  639. if (arg1.type === "SpreadElement") return;
  640. const evaluatedRegExp = this.evaluateExpression(arg1);
  641. if (!evaluatedRegExp) return;
  642. regExp = evaluatedRegExp.asString();
  643. if (!regExp) return;
  644. } else {
  645. return (
  646. new BasicEvaluatedExpression()
  647. // eslint-disable-next-line prefer-regex-literals
  648. .setRegExp(new RegExp(""))
  649. .setRange(/** @type {Range} */ (expr.range))
  650. );
  651. }
  652. let flags;
  653. const arg2 = expr.arguments[1];
  654. if (arg2) {
  655. if (arg2.type === "SpreadElement") return;
  656. const evaluatedFlags = this.evaluateExpression(arg2);
  657. if (!evaluatedFlags) return;
  658. if (!evaluatedFlags.isUndefined()) {
  659. flags = evaluatedFlags.asString();
  660. if (
  661. flags === undefined ||
  662. !BasicEvaluatedExpression.isValidRegExpFlags(flags)
  663. ) {
  664. return;
  665. }
  666. }
  667. }
  668. return new BasicEvaluatedExpression()
  669. .setRegExp(flags ? new RegExp(regExp, flags) : new RegExp(regExp))
  670. .setRange(/** @type {Range} */ (expr.range));
  671. });
  672. this.hooks.evaluate.for("LogicalExpression").tap(CLASS_NAME, (_expr) => {
  673. const expr = /** @type {LogicalExpression} */ (_expr);
  674. const left = this.evaluateExpression(expr.left);
  675. let returnRight = false;
  676. /** @type {boolean | undefined} */
  677. let allowedRight;
  678. if (expr.operator === "&&") {
  679. const leftAsBool = left.asBool();
  680. if (leftAsBool === false) {
  681. return left.setRange(/** @type {Range} */ (expr.range));
  682. }
  683. returnRight = leftAsBool === true;
  684. allowedRight = false;
  685. } else if (expr.operator === "||") {
  686. const leftAsBool = left.asBool();
  687. if (leftAsBool === true) {
  688. return left.setRange(/** @type {Range} */ (expr.range));
  689. }
  690. returnRight = leftAsBool === false;
  691. allowedRight = true;
  692. } else if (expr.operator === "??") {
  693. const leftAsNullish = left.asNullish();
  694. if (leftAsNullish === false) {
  695. return left.setRange(/** @type {Range} */ (expr.range));
  696. }
  697. if (leftAsNullish !== true) return;
  698. returnRight = true;
  699. } else {
  700. return;
  701. }
  702. const right = this.evaluateExpression(expr.right);
  703. if (returnRight) {
  704. if (left.couldHaveSideEffects()) right.setSideEffects();
  705. return right.setRange(/** @type {Range} */ (expr.range));
  706. }
  707. const asBool = right.asBool();
  708. if (allowedRight === true && asBool === true) {
  709. return new BasicEvaluatedExpression()
  710. .setRange(/** @type {Range} */ (expr.range))
  711. .setTruthy();
  712. } else if (allowedRight === false && asBool === false) {
  713. return new BasicEvaluatedExpression()
  714. .setRange(/** @type {Range} */ (expr.range))
  715. .setFalsy();
  716. }
  717. });
  718. /**
  719. * In simple logical cases, we can use valueAsExpression to assist us in evaluating the expression on
  720. * either side of a [BinaryExpression](https://github.com/estree/estree/blob/master/es5.md#binaryexpression).
  721. * This supports scenarios in webpack like conditionally `import()`'ing modules based on some simple evaluation:
  722. *
  723. * ```js
  724. * if (1 === 3) {
  725. * import("./moduleA"); // webpack will auto evaluate this and not import the modules
  726. * }
  727. * ```
  728. *
  729. * Additional scenarios include evaluation of strings inside of dynamic import statements:
  730. *
  731. * ```js
  732. * const foo = "foo";
  733. * const bar = "bar";
  734. *
  735. * import("./" + foo + bar); // webpack will auto evaluate this into import("./foobar")
  736. * ```
  737. * @param {boolean | number | bigint | string} value the value to convert to an expression
  738. * @param {BinaryExpression | UnaryExpression} expr the expression being evaluated
  739. * @param {boolean} sideEffects whether the expression has side effects
  740. * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
  741. * @example
  742. *
  743. * ```js
  744. * const binaryExpr = new BinaryExpression("+",
  745. * { type: "Literal", value: 2 },
  746. * { type: "Literal", value: 3 }
  747. * );
  748. *
  749. * const leftValue = 2;
  750. * const rightValue = 3;
  751. *
  752. * const leftExpr = valueAsExpression(leftValue, binaryExpr.left, false);
  753. * const rightExpr = valueAsExpression(rightValue, binaryExpr.right, false);
  754. * const result = new BasicEvaluatedExpression()
  755. * .setNumber(leftExpr.number + rightExpr.number)
  756. * .setRange(binaryExpr.range);
  757. *
  758. * console.log(result.number); // Output: 5
  759. * ```
  760. */
  761. const valueAsExpression = (value, expr, sideEffects) => {
  762. switch (typeof value) {
  763. case "boolean":
  764. return new BasicEvaluatedExpression()
  765. .setBoolean(value)
  766. .setSideEffects(sideEffects)
  767. .setRange(/** @type {Range} */ (expr.range));
  768. case "number":
  769. return new BasicEvaluatedExpression()
  770. .setNumber(value)
  771. .setSideEffects(sideEffects)
  772. .setRange(/** @type {Range} */ (expr.range));
  773. case "bigint":
  774. return new BasicEvaluatedExpression()
  775. .setBigInt(value)
  776. .setSideEffects(sideEffects)
  777. .setRange(/** @type {Range} */ (expr.range));
  778. case "string":
  779. return new BasicEvaluatedExpression()
  780. .setString(value)
  781. .setSideEffects(sideEffects)
  782. .setRange(/** @type {Range} */ (expr.range));
  783. }
  784. };
  785. this.hooks.evaluate.for("BinaryExpression").tap(CLASS_NAME, (_expr) => {
  786. const expr = /** @type {BinaryExpression} */ (_expr);
  787. /**
  788. * Evaluates a binary expression if and only if it is a const operation (e.g. 1 + 2, "a" + "b", etc.).
  789. * @template T
  790. * @param {(leftOperand: T, rightOperand: T) => boolean | number | bigint | string} operandHandler the handler for the operation (e.g. (a, b) => a + b)
  791. * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
  792. */
  793. const handleConstOperation = (operandHandler) => {
  794. const left = this.evaluateExpression(expr.left);
  795. if (!left.isCompileTimeValue()) return;
  796. const right = this.evaluateExpression(expr.right);
  797. if (!right.isCompileTimeValue()) return;
  798. const result = operandHandler(
  799. /** @type {T} */ (left.asCompileTimeValue()),
  800. /** @type {T} */ (right.asCompileTimeValue())
  801. );
  802. return valueAsExpression(
  803. result,
  804. expr,
  805. left.couldHaveSideEffects() || right.couldHaveSideEffects()
  806. );
  807. };
  808. /**
  809. * Helper function to determine if two booleans are always different. This is used in `handleStrictEqualityComparison`
  810. * to determine if an expressions boolean or nullish conversion is equal or not.
  811. * @param {boolean} a first boolean to compare
  812. * @param {boolean} b second boolean to compare
  813. * @returns {boolean} true if the two booleans are always different, false otherwise
  814. */
  815. const isAlwaysDifferent = (a, b) =>
  816. (a === true && b === false) || (a === false && b === true);
  817. /**
  818. * @param {BasicEvaluatedExpression} left left
  819. * @param {BasicEvaluatedExpression} right right
  820. * @param {BasicEvaluatedExpression} res res
  821. * @param {boolean} eql true for "===" and false for "!=="
  822. * @returns {BasicEvaluatedExpression | undefined} result
  823. */
  824. const handleTemplateStringCompare = (left, right, res, eql) => {
  825. /**
  826. * @param {BasicEvaluatedExpression[]} parts parts
  827. * @returns {string} value
  828. */
  829. const getPrefix = (parts) => {
  830. let value = "";
  831. for (const p of parts) {
  832. const v = p.asString();
  833. if (v !== undefined) value += v;
  834. else break;
  835. }
  836. return value;
  837. };
  838. /**
  839. * @param {BasicEvaluatedExpression[]} parts parts
  840. * @returns {string} value
  841. */
  842. const getSuffix = (parts) => {
  843. let value = "";
  844. for (let i = parts.length - 1; i >= 0; i--) {
  845. const v = parts[i].asString();
  846. if (v !== undefined) value = v + value;
  847. else break;
  848. }
  849. return value;
  850. };
  851. const leftPrefix = getPrefix(
  852. /** @type {BasicEvaluatedExpression[]} */ (left.parts)
  853. );
  854. const rightPrefix = getPrefix(
  855. /** @type {BasicEvaluatedExpression[]} */ (right.parts)
  856. );
  857. const leftSuffix = getSuffix(
  858. /** @type {BasicEvaluatedExpression[]} */ (left.parts)
  859. );
  860. const rightSuffix = getSuffix(
  861. /** @type {BasicEvaluatedExpression[]} */ (right.parts)
  862. );
  863. const lenPrefix = Math.min(leftPrefix.length, rightPrefix.length);
  864. const lenSuffix = Math.min(leftSuffix.length, rightSuffix.length);
  865. const prefixMismatch =
  866. lenPrefix > 0 &&
  867. leftPrefix.slice(0, lenPrefix) !== rightPrefix.slice(0, lenPrefix);
  868. const suffixMismatch =
  869. lenSuffix > 0 &&
  870. leftSuffix.slice(-lenSuffix) !== rightSuffix.slice(-lenSuffix);
  871. if (prefixMismatch || suffixMismatch) {
  872. return res
  873. .setBoolean(!eql)
  874. .setSideEffects(
  875. left.couldHaveSideEffects() || right.couldHaveSideEffects()
  876. );
  877. }
  878. };
  879. /**
  880. * Helper function to handle BinaryExpressions using strict equality comparisons (e.g. "===" and "!==").
  881. * @param {boolean} eql true for "===" and false for "!=="
  882. * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
  883. */
  884. const handleStrictEqualityComparison = (eql) => {
  885. const left = this.evaluateExpression(expr.left);
  886. const right = this.evaluateExpression(expr.right);
  887. const res = new BasicEvaluatedExpression();
  888. res.setRange(/** @type {Range} */ (expr.range));
  889. const leftConst = left.isCompileTimeValue();
  890. const rightConst = right.isCompileTimeValue();
  891. if (leftConst && rightConst) {
  892. return res
  893. .setBoolean(
  894. eql === (left.asCompileTimeValue() === right.asCompileTimeValue())
  895. )
  896. .setSideEffects(
  897. left.couldHaveSideEffects() || right.couldHaveSideEffects()
  898. );
  899. }
  900. if (left.isArray() && right.isArray()) {
  901. return res
  902. .setBoolean(!eql)
  903. .setSideEffects(
  904. left.couldHaveSideEffects() || right.couldHaveSideEffects()
  905. );
  906. }
  907. if (left.isTemplateString() && right.isTemplateString()) {
  908. return handleTemplateStringCompare(left, right, res, eql);
  909. }
  910. const leftPrimitive = left.isPrimitiveType();
  911. const rightPrimitive = right.isPrimitiveType();
  912. if (
  913. // Primitive !== Object or
  914. // compile-time object types are never equal to something at runtime
  915. (leftPrimitive === false && (leftConst || rightPrimitive === true)) ||
  916. (rightPrimitive === false &&
  917. (rightConst || leftPrimitive === true)) ||
  918. // Different nullish or boolish status also means not equal
  919. isAlwaysDifferent(
  920. /** @type {boolean} */ (left.asBool()),
  921. /** @type {boolean} */ (right.asBool())
  922. ) ||
  923. isAlwaysDifferent(
  924. /** @type {boolean} */ (left.asNullish()),
  925. /** @type {boolean} */ (right.asNullish())
  926. )
  927. ) {
  928. return res
  929. .setBoolean(!eql)
  930. .setSideEffects(
  931. left.couldHaveSideEffects() || right.couldHaveSideEffects()
  932. );
  933. }
  934. };
  935. /**
  936. * Helper function to handle BinaryExpressions using abstract equality comparisons (e.g. "==" and "!=").
  937. * @param {boolean} eql true for "==" and false for "!="
  938. * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
  939. */
  940. const handleAbstractEqualityComparison = (eql) => {
  941. const left = this.evaluateExpression(expr.left);
  942. const right = this.evaluateExpression(expr.right);
  943. const res = new BasicEvaluatedExpression();
  944. res.setRange(/** @type {Range} */ (expr.range));
  945. const leftConst = left.isCompileTimeValue();
  946. const rightConst = right.isCompileTimeValue();
  947. if (leftConst && rightConst) {
  948. return res
  949. .setBoolean(
  950. eql ===
  951. // eslint-disable-next-line eqeqeq
  952. (left.asCompileTimeValue() == right.asCompileTimeValue())
  953. )
  954. .setSideEffects(
  955. left.couldHaveSideEffects() || right.couldHaveSideEffects()
  956. );
  957. }
  958. if (left.isArray() && right.isArray()) {
  959. return res
  960. .setBoolean(!eql)
  961. .setSideEffects(
  962. left.couldHaveSideEffects() || right.couldHaveSideEffects()
  963. );
  964. }
  965. if (left.isTemplateString() && right.isTemplateString()) {
  966. return handleTemplateStringCompare(left, right, res, eql);
  967. }
  968. };
  969. if (expr.operator === "+") {
  970. const left = this.evaluateExpression(expr.left);
  971. const right = this.evaluateExpression(expr.right);
  972. const res = new BasicEvaluatedExpression();
  973. if (left.isString()) {
  974. if (right.isString()) {
  975. res.setString(
  976. /** @type {string} */ (left.string) +
  977. /** @type {string} */ (right.string)
  978. );
  979. } else if (right.isNumber()) {
  980. res.setString(/** @type {string} */ (left.string) + right.number);
  981. } else if (
  982. right.isWrapped() &&
  983. right.prefix &&
  984. right.prefix.isString()
  985. ) {
  986. // "left" + ("prefix" + inner + "postfix")
  987. // => ("leftPrefix" + inner + "postfix")
  988. res.setWrapped(
  989. new BasicEvaluatedExpression()
  990. .setString(
  991. /** @type {string} */ (left.string) +
  992. /** @type {string} */ (right.prefix.string)
  993. )
  994. .setRange(
  995. joinRanges(
  996. /** @type {Range} */ (left.range),
  997. /** @type {Range} */ (right.prefix.range)
  998. )
  999. ),
  1000. right.postfix,
  1001. right.wrappedInnerExpressions
  1002. );
  1003. } else if (right.isWrapped()) {
  1004. // "left" + ([null] + inner + "postfix")
  1005. // => ("left" + inner + "postfix")
  1006. res.setWrapped(left, right.postfix, right.wrappedInnerExpressions);
  1007. } else {
  1008. // "left" + expr
  1009. // => ("left" + expr + "")
  1010. res.setWrapped(left, null, [right]);
  1011. }
  1012. } else if (left.isNumber()) {
  1013. if (right.isString()) {
  1014. res.setString(left.number + /** @type {string} */ (right.string));
  1015. } else if (right.isNumber()) {
  1016. res.setNumber(
  1017. /** @type {number} */ (left.number) +
  1018. /** @type {number} */ (right.number)
  1019. );
  1020. } else {
  1021. return;
  1022. }
  1023. } else if (left.isBigInt()) {
  1024. if (right.isBigInt()) {
  1025. res.setBigInt(
  1026. /** @type {bigint} */ (left.bigint) +
  1027. /** @type {bigint} */ (right.bigint)
  1028. );
  1029. }
  1030. } else if (left.isWrapped()) {
  1031. if (left.postfix && left.postfix.isString() && right.isString()) {
  1032. // ("prefix" + inner + "postfix") + "right"
  1033. // => ("prefix" + inner + "postfixRight")
  1034. res.setWrapped(
  1035. left.prefix,
  1036. new BasicEvaluatedExpression()
  1037. .setString(
  1038. /** @type {string} */ (left.postfix.string) +
  1039. /** @type {string} */ (right.string)
  1040. )
  1041. .setRange(
  1042. joinRanges(
  1043. /** @type {Range} */ (left.postfix.range),
  1044. /** @type {Range} */ (right.range)
  1045. )
  1046. ),
  1047. left.wrappedInnerExpressions
  1048. );
  1049. } else if (
  1050. left.postfix &&
  1051. left.postfix.isString() &&
  1052. right.isNumber()
  1053. ) {
  1054. // ("prefix" + inner + "postfix") + 123
  1055. // => ("prefix" + inner + "postfix123")
  1056. res.setWrapped(
  1057. left.prefix,
  1058. new BasicEvaluatedExpression()
  1059. .setString(
  1060. /** @type {string} */ (left.postfix.string) +
  1061. /** @type {number} */ (right.number)
  1062. )
  1063. .setRange(
  1064. joinRanges(
  1065. /** @type {Range} */ (left.postfix.range),
  1066. /** @type {Range} */ (right.range)
  1067. )
  1068. ),
  1069. left.wrappedInnerExpressions
  1070. );
  1071. } else if (right.isString()) {
  1072. // ("prefix" + inner + [null]) + "right"
  1073. // => ("prefix" + inner + "right")
  1074. res.setWrapped(left.prefix, right, left.wrappedInnerExpressions);
  1075. } else if (right.isNumber()) {
  1076. // ("prefix" + inner + [null]) + 123
  1077. // => ("prefix" + inner + "123")
  1078. res.setWrapped(
  1079. left.prefix,
  1080. new BasicEvaluatedExpression()
  1081. .setString(String(right.number))
  1082. .setRange(/** @type {Range} */ (right.range)),
  1083. left.wrappedInnerExpressions
  1084. );
  1085. } else if (right.isWrapped()) {
  1086. // ("prefix1" + inner1 + "postfix1") + ("prefix2" + inner2 + "postfix2")
  1087. // ("prefix1" + inner1 + "postfix1" + "prefix2" + inner2 + "postfix2")
  1088. res.setWrapped(
  1089. left.prefix,
  1090. right.postfix,
  1091. left.wrappedInnerExpressions &&
  1092. right.wrappedInnerExpressions && [
  1093. ...left.wrappedInnerExpressions,
  1094. ...(left.postfix ? [left.postfix] : []),
  1095. ...(right.prefix ? [right.prefix] : []),
  1096. ...right.wrappedInnerExpressions
  1097. ]
  1098. );
  1099. } else {
  1100. // ("prefix" + inner + postfix) + expr
  1101. // => ("prefix" + inner + postfix + expr + [null])
  1102. res.setWrapped(
  1103. left.prefix,
  1104. null,
  1105. left.wrappedInnerExpressions && [
  1106. ...left.wrappedInnerExpressions,
  1107. ...(left.postfix ? [left.postfix, right] : [right])
  1108. ]
  1109. );
  1110. }
  1111. } else if (right.isString()) {
  1112. // left + "right"
  1113. // => ([null] + left + "right")
  1114. res.setWrapped(null, right, [left]);
  1115. } else if (right.isWrapped()) {
  1116. // left + (prefix + inner + "postfix")
  1117. // => ([null] + left + prefix + inner + "postfix")
  1118. res.setWrapped(
  1119. null,
  1120. right.postfix,
  1121. right.wrappedInnerExpressions && [
  1122. ...(right.prefix ? [left, right.prefix] : [left]),
  1123. ...right.wrappedInnerExpressions
  1124. ]
  1125. );
  1126. } else {
  1127. return;
  1128. }
  1129. if (left.couldHaveSideEffects() || right.couldHaveSideEffects()) {
  1130. res.setSideEffects();
  1131. }
  1132. res.setRange(/** @type {Range} */ (expr.range));
  1133. return res;
  1134. } else if (expr.operator === "-") {
  1135. return handleConstOperation((l, r) => l - r);
  1136. } else if (expr.operator === "*") {
  1137. return handleConstOperation((l, r) => l * r);
  1138. } else if (expr.operator === "/") {
  1139. return handleConstOperation((l, r) => l / r);
  1140. } else if (expr.operator === "**") {
  1141. return handleConstOperation((l, r) => l ** r);
  1142. } else if (expr.operator === "===") {
  1143. return handleStrictEqualityComparison(true);
  1144. } else if (expr.operator === "==") {
  1145. return handleAbstractEqualityComparison(true);
  1146. } else if (expr.operator === "!==") {
  1147. return handleStrictEqualityComparison(false);
  1148. } else if (expr.operator === "!=") {
  1149. return handleAbstractEqualityComparison(false);
  1150. } else if (expr.operator === "&") {
  1151. return handleConstOperation((l, r) => l & r);
  1152. } else if (expr.operator === "|") {
  1153. return handleConstOperation((l, r) => l | r);
  1154. } else if (expr.operator === "^") {
  1155. return handleConstOperation((l, r) => l ^ r);
  1156. } else if (expr.operator === ">>>") {
  1157. return handleConstOperation((l, r) => l >>> r);
  1158. } else if (expr.operator === ">>") {
  1159. return handleConstOperation((l, r) => l >> r);
  1160. } else if (expr.operator === "<<") {
  1161. return handleConstOperation((l, r) => l << r);
  1162. } else if (expr.operator === "<") {
  1163. return handleConstOperation((l, r) => l < r);
  1164. } else if (expr.operator === ">") {
  1165. return handleConstOperation((l, r) => l > r);
  1166. } else if (expr.operator === "<=") {
  1167. return handleConstOperation((l, r) => l <= r);
  1168. } else if (expr.operator === ">=") {
  1169. return handleConstOperation((l, r) => l >= r);
  1170. }
  1171. });
  1172. this.hooks.evaluate.for("UnaryExpression").tap(CLASS_NAME, (_expr) => {
  1173. const expr = /** @type {UnaryExpression} */ (_expr);
  1174. /**
  1175. * Evaluates a UnaryExpression if and only if it is a basic const operator (e.g. +a, -a, ~a).
  1176. * @template T
  1177. * @param {(operand: T) => boolean | number | bigint | string} operandHandler handler for the operand
  1178. * @returns {BasicEvaluatedExpression | undefined} evaluated expression
  1179. */
  1180. const handleConstOperation = (operandHandler) => {
  1181. const argument = this.evaluateExpression(expr.argument);
  1182. if (!argument.isCompileTimeValue()) return;
  1183. const result = operandHandler(
  1184. /** @type {T} */ (argument.asCompileTimeValue())
  1185. );
  1186. return valueAsExpression(result, expr, argument.couldHaveSideEffects());
  1187. };
  1188. if (expr.operator === "typeof") {
  1189. switch (expr.argument.type) {
  1190. case "Identifier": {
  1191. const res = this.callHooksForName(
  1192. this.hooks.evaluateTypeof,
  1193. expr.argument.name,
  1194. expr
  1195. );
  1196. if (res !== undefined) return res;
  1197. break;
  1198. }
  1199. case "MetaProperty": {
  1200. const res = this.callHooksForName(
  1201. this.hooks.evaluateTypeof,
  1202. /** @type {string} */
  1203. (getRootName(expr.argument)),
  1204. expr
  1205. );
  1206. if (res !== undefined) return res;
  1207. break;
  1208. }
  1209. case "MemberExpression": {
  1210. const res = this.callHooksForExpression(
  1211. this.hooks.evaluateTypeof,
  1212. expr.argument,
  1213. expr
  1214. );
  1215. if (res !== undefined) return res;
  1216. break;
  1217. }
  1218. case "ChainExpression": {
  1219. const res = this.callHooksForExpression(
  1220. this.hooks.evaluateTypeof,
  1221. expr.argument.expression,
  1222. expr
  1223. );
  1224. if (res !== undefined) return res;
  1225. break;
  1226. }
  1227. case "FunctionExpression": {
  1228. return new BasicEvaluatedExpression()
  1229. .setString("function")
  1230. .setRange(/** @type {Range} */ (expr.range));
  1231. }
  1232. }
  1233. const arg = this.evaluateExpression(expr.argument);
  1234. if (arg.isUnknown()) return;
  1235. if (arg.isString()) {
  1236. return new BasicEvaluatedExpression()
  1237. .setString("string")
  1238. .setRange(/** @type {Range} */ (expr.range));
  1239. }
  1240. if (arg.isWrapped()) {
  1241. return new BasicEvaluatedExpression()
  1242. .setString("string")
  1243. .setSideEffects()
  1244. .setRange(/** @type {Range} */ (expr.range));
  1245. }
  1246. if (arg.isUndefined()) {
  1247. return new BasicEvaluatedExpression()
  1248. .setString("undefined")
  1249. .setRange(/** @type {Range} */ (expr.range));
  1250. }
  1251. if (arg.isNumber()) {
  1252. return new BasicEvaluatedExpression()
  1253. .setString("number")
  1254. .setRange(/** @type {Range} */ (expr.range));
  1255. }
  1256. if (arg.isBigInt()) {
  1257. return new BasicEvaluatedExpression()
  1258. .setString("bigint")
  1259. .setRange(/** @type {Range} */ (expr.range));
  1260. }
  1261. if (arg.isBoolean()) {
  1262. return new BasicEvaluatedExpression()
  1263. .setString("boolean")
  1264. .setRange(/** @type {Range} */ (expr.range));
  1265. }
  1266. if (arg.isConstArray() || arg.isRegExp() || arg.isNull()) {
  1267. return new BasicEvaluatedExpression()
  1268. .setString("object")
  1269. .setRange(/** @type {Range} */ (expr.range));
  1270. }
  1271. if (arg.isArray()) {
  1272. return new BasicEvaluatedExpression()
  1273. .setString("object")
  1274. .setSideEffects(arg.couldHaveSideEffects())
  1275. .setRange(/** @type {Range} */ (expr.range));
  1276. }
  1277. } else if (expr.operator === "!") {
  1278. const argument = this.evaluateExpression(expr.argument);
  1279. const bool = argument.asBool();
  1280. if (typeof bool !== "boolean") return;
  1281. return new BasicEvaluatedExpression()
  1282. .setBoolean(!bool)
  1283. .setSideEffects(argument.couldHaveSideEffects())
  1284. .setRange(/** @type {Range} */ (expr.range));
  1285. } else if (expr.operator === "~") {
  1286. return handleConstOperation((v) => ~v);
  1287. } else if (expr.operator === "+") {
  1288. // eslint-disable-next-line no-implicit-coercion
  1289. return handleConstOperation((v) => +v);
  1290. } else if (expr.operator === "-") {
  1291. return handleConstOperation((v) => -v);
  1292. }
  1293. });
  1294. this.hooks.evaluateTypeof
  1295. .for("undefined")
  1296. .tap(CLASS_NAME, (expr) =>
  1297. new BasicEvaluatedExpression()
  1298. .setString("undefined")
  1299. .setRange(/** @type {Range} */ (expr.range))
  1300. );
  1301. this.hooks.evaluate.for("Identifier").tap(CLASS_NAME, (expr) => {
  1302. if (/** @type {Identifier} */ (expr).name === "undefined") {
  1303. return new BasicEvaluatedExpression()
  1304. .setUndefined()
  1305. .setRange(/** @type {Range} */ (expr.range));
  1306. }
  1307. });
  1308. /**
  1309. * @param {"Identifier" | "ThisExpression" | "MemberExpression"} exprType expression type name
  1310. * @param {(node: Expression | SpreadElement) => GetInfoResult | undefined} getInfo get info
  1311. * @returns {void}
  1312. */
  1313. const tapEvaluateWithVariableInfo = (exprType, getInfo) => {
  1314. /** @type {Expression | undefined} */
  1315. let cachedExpression;
  1316. /** @type {GetInfoResult | undefined} */
  1317. let cachedInfo;
  1318. this.hooks.evaluate.for(exprType).tap(CLASS_NAME, (expr) => {
  1319. const expression =
  1320. /** @type {Identifier | ThisExpression | MemberExpression} */ (expr);
  1321. const info = getInfo(expression);
  1322. if (info !== undefined) {
  1323. return this.callHooksForInfoWithFallback(
  1324. this.hooks.evaluateIdentifier,
  1325. info.name,
  1326. (_name) => {
  1327. cachedExpression = expression;
  1328. cachedInfo = info;
  1329. return undefined;
  1330. },
  1331. (name) => {
  1332. const hook = this.hooks.evaluateDefinedIdentifier.get(name);
  1333. if (hook !== undefined) {
  1334. return hook.call(expression);
  1335. }
  1336. },
  1337. expression
  1338. );
  1339. }
  1340. });
  1341. this.hooks.evaluate
  1342. .for(exprType)
  1343. .tap({ name: CLASS_NAME, stage: 100 }, (expr) => {
  1344. const expression =
  1345. /** @type {Identifier | ThisExpression | MemberExpression} */
  1346. (expr);
  1347. const info =
  1348. cachedExpression === expression ? cachedInfo : getInfo(expression);
  1349. if (info !== undefined) {
  1350. return new BasicEvaluatedExpression()
  1351. .setIdentifier(
  1352. info.name,
  1353. info.rootInfo,
  1354. info.getMembers,
  1355. info.getMembersOptionals,
  1356. info.getMemberRanges
  1357. )
  1358. .setRange(/** @type {Range} */ (expression.range));
  1359. }
  1360. });
  1361. this.hooks.finish.tap(CLASS_NAME, () => {
  1362. // Cleanup for GC
  1363. cachedExpression = cachedInfo = undefined;
  1364. });
  1365. };
  1366. tapEvaluateWithVariableInfo("Identifier", (expr) => {
  1367. const info = this.getVariableInfo(/** @type {Identifier} */ (expr).name);
  1368. if (
  1369. typeof info === "string" ||
  1370. (info instanceof VariableInfo && (info.isFree() || info.isTagged()))
  1371. ) {
  1372. return {
  1373. name: info,
  1374. rootInfo: info,
  1375. getMembers: () => [],
  1376. getMembersOptionals: () => [],
  1377. getMemberRanges: () => []
  1378. };
  1379. }
  1380. });
  1381. tapEvaluateWithVariableInfo("ThisExpression", (_expr) => {
  1382. const info = this.getVariableInfo("this");
  1383. if (
  1384. typeof info === "string" ||
  1385. (info instanceof VariableInfo && (info.isFree() || info.isTagged()))
  1386. ) {
  1387. return {
  1388. name: info,
  1389. rootInfo: info,
  1390. getMembers: () => [],
  1391. getMembersOptionals: () => [],
  1392. getMemberRanges: () => []
  1393. };
  1394. }
  1395. });
  1396. this.hooks.evaluate.for("MetaProperty").tap(CLASS_NAME, (expr) => {
  1397. const metaProperty = /** @type {MetaProperty} */ (expr);
  1398. return this.callHooksForName(
  1399. this.hooks.evaluateIdentifier,
  1400. /** @type {string} */
  1401. (getRootName(metaProperty)),
  1402. metaProperty
  1403. );
  1404. });
  1405. tapEvaluateWithVariableInfo("MemberExpression", (expr) =>
  1406. this.getMemberExpressionInfo(
  1407. /** @type {MemberExpression} */ (expr),
  1408. ALLOWED_MEMBER_TYPES_EXPRESSION
  1409. )
  1410. );
  1411. this.hooks.evaluate.for("CallExpression").tap(CLASS_NAME, (_expr) => {
  1412. const expr = /** @type {CallExpression} */ (_expr);
  1413. if (
  1414. expr.callee.type === "MemberExpression" &&
  1415. expr.callee.property.type ===
  1416. (expr.callee.computed ? "Literal" : "Identifier")
  1417. ) {
  1418. // type Super also possible here
  1419. const param = this.evaluateExpression(
  1420. /** @type {Expression} */ (expr.callee.object)
  1421. );
  1422. const property =
  1423. expr.callee.property.type === "Literal"
  1424. ? `${expr.callee.property.value}`
  1425. : expr.callee.property.name;
  1426. const hook = this.hooks.evaluateCallExpressionMember.get(property);
  1427. if (hook !== undefined) {
  1428. return hook.call(expr, param);
  1429. }
  1430. } else if (expr.callee.type === "Identifier") {
  1431. return this.callHooksForName(
  1432. this.hooks.evaluateCallExpression,
  1433. expr.callee.name,
  1434. expr
  1435. );
  1436. }
  1437. });
  1438. this.hooks.evaluateCallExpressionMember
  1439. .for("indexOf")
  1440. .tap(CLASS_NAME, (expr, param) => {
  1441. if (!param.isString()) return;
  1442. if (expr.arguments.length === 0) return;
  1443. const [arg1, arg2] = expr.arguments;
  1444. if (arg1.type === "SpreadElement") return;
  1445. const arg1Eval = this.evaluateExpression(arg1);
  1446. if (!arg1Eval.isString()) return;
  1447. const arg1Value = /** @type {string} */ (arg1Eval.string);
  1448. let result;
  1449. if (arg2) {
  1450. if (arg2.type === "SpreadElement") return;
  1451. const arg2Eval = this.evaluateExpression(arg2);
  1452. if (!arg2Eval.isNumber()) return;
  1453. result = /** @type {string} */ (param.string).indexOf(
  1454. arg1Value,
  1455. arg2Eval.number
  1456. );
  1457. } else {
  1458. result = /** @type {string} */ (param.string).indexOf(arg1Value);
  1459. }
  1460. return new BasicEvaluatedExpression()
  1461. .setNumber(result)
  1462. .setSideEffects(param.couldHaveSideEffects())
  1463. .setRange(/** @type {Range} */ (expr.range));
  1464. });
  1465. this.hooks.evaluateCallExpressionMember
  1466. .for("replace")
  1467. .tap(CLASS_NAME, (expr, param) => {
  1468. if (!param.isString()) return;
  1469. if (expr.arguments.length !== 2) return;
  1470. if (expr.arguments[0].type === "SpreadElement") return;
  1471. if (expr.arguments[1].type === "SpreadElement") return;
  1472. const arg1 = this.evaluateExpression(expr.arguments[0]);
  1473. const arg2 = this.evaluateExpression(expr.arguments[1]);
  1474. if (!arg1.isString() && !arg1.isRegExp()) return;
  1475. const arg1Value = /** @type {string | RegExp} */ (
  1476. arg1.regExp || arg1.string
  1477. );
  1478. if (!arg2.isString()) return;
  1479. const arg2Value = /** @type {string} */ (arg2.string);
  1480. return new BasicEvaluatedExpression()
  1481. .setString(
  1482. /** @type {string} */ (param.string).replace(arg1Value, arg2Value)
  1483. )
  1484. .setSideEffects(param.couldHaveSideEffects())
  1485. .setRange(/** @type {Range} */ (expr.range));
  1486. });
  1487. for (const fn of ["substr", "substring", "slice"]) {
  1488. this.hooks.evaluateCallExpressionMember
  1489. .for(fn)
  1490. .tap(CLASS_NAME, (expr, param) => {
  1491. if (!param.isString()) return;
  1492. let arg1;
  1493. let result;
  1494. const str = /** @type {string} */ (param.string);
  1495. switch (expr.arguments.length) {
  1496. case 1:
  1497. if (expr.arguments[0].type === "SpreadElement") return;
  1498. arg1 = this.evaluateExpression(expr.arguments[0]);
  1499. if (!arg1.isNumber()) return;
  1500. result = str[
  1501. /** @type {"substr" | "substring" | "slice"} */ (fn)
  1502. ](/** @type {number} */ (arg1.number));
  1503. break;
  1504. case 2: {
  1505. if (expr.arguments[0].type === "SpreadElement") return;
  1506. if (expr.arguments[1].type === "SpreadElement") return;
  1507. arg1 = this.evaluateExpression(expr.arguments[0]);
  1508. const arg2 = this.evaluateExpression(expr.arguments[1]);
  1509. if (!arg1.isNumber()) return;
  1510. if (!arg2.isNumber()) return;
  1511. result = str[
  1512. /** @type {"substr" | "substring" | "slice"} */ (fn)
  1513. ](
  1514. /** @type {number} */ (arg1.number),
  1515. /** @type {number} */ (arg2.number)
  1516. );
  1517. break;
  1518. }
  1519. default:
  1520. return;
  1521. }
  1522. return new BasicEvaluatedExpression()
  1523. .setString(result)
  1524. .setSideEffects(param.couldHaveSideEffects())
  1525. .setRange(/** @type {Range} */ (expr.range));
  1526. });
  1527. }
  1528. /**
  1529. * @param {"cooked" | "raw"} kind kind of values to get
  1530. * @param {TemplateLiteral} templateLiteralExpr TemplateLiteral expr
  1531. * @returns {{quasis: BasicEvaluatedExpression[], parts: BasicEvaluatedExpression[]}} Simplified template
  1532. */
  1533. const getSimplifiedTemplateResult = (kind, templateLiteralExpr) => {
  1534. /** @type {BasicEvaluatedExpression[]} */
  1535. const quasis = [];
  1536. /** @type {BasicEvaluatedExpression[]} */
  1537. const parts = [];
  1538. for (let i = 0; i < templateLiteralExpr.quasis.length; i++) {
  1539. const quasiExpr = templateLiteralExpr.quasis[i];
  1540. const quasi = quasiExpr.value[kind];
  1541. if (i > 0) {
  1542. const prevExpr = parts[parts.length - 1];
  1543. const expr = this.evaluateExpression(
  1544. templateLiteralExpr.expressions[i - 1]
  1545. );
  1546. const exprAsString = expr.asString();
  1547. if (
  1548. typeof exprAsString === "string" &&
  1549. !expr.couldHaveSideEffects()
  1550. ) {
  1551. // We can merge quasi + expr + quasi when expr
  1552. // is a const string
  1553. prevExpr.setString(prevExpr.string + exprAsString + quasi);
  1554. prevExpr.setRange([
  1555. /** @type {Range} */ (prevExpr.range)[0],
  1556. /** @type {Range} */ (quasiExpr.range)[1]
  1557. ]);
  1558. // We unset the expression as it doesn't match to a single expression
  1559. prevExpr.setExpression(undefined);
  1560. continue;
  1561. }
  1562. parts.push(expr);
  1563. }
  1564. const part = new BasicEvaluatedExpression()
  1565. .setString(/** @type {string} */ (quasi))
  1566. .setRange(/** @type {Range} */ (quasiExpr.range))
  1567. .setExpression(quasiExpr);
  1568. quasis.push(part);
  1569. parts.push(part);
  1570. }
  1571. return {
  1572. quasis,
  1573. parts
  1574. };
  1575. };
  1576. this.hooks.evaluate.for("TemplateLiteral").tap(CLASS_NAME, (_node) => {
  1577. const node = /** @type {TemplateLiteral} */ (_node);
  1578. const { quasis, parts } = getSimplifiedTemplateResult("cooked", node);
  1579. if (parts.length === 1) {
  1580. return parts[0].setRange(/** @type {Range} */ (node.range));
  1581. }
  1582. return new BasicEvaluatedExpression()
  1583. .setTemplateString(quasis, parts, "cooked")
  1584. .setRange(/** @type {Range} */ (node.range));
  1585. });
  1586. this.hooks.evaluate
  1587. .for("TaggedTemplateExpression")
  1588. .tap(CLASS_NAME, (_node) => {
  1589. const node = /** @type {TaggedTemplateExpression} */ (_node);
  1590. const tag = this.evaluateExpression(node.tag);
  1591. if (tag.isIdentifier() && tag.identifier === "String.raw") {
  1592. const { quasis, parts } = getSimplifiedTemplateResult(
  1593. "raw",
  1594. node.quasi
  1595. );
  1596. return new BasicEvaluatedExpression()
  1597. .setTemplateString(quasis, parts, "raw")
  1598. .setRange(/** @type {Range} */ (node.range));
  1599. }
  1600. });
  1601. this.hooks.evaluateCallExpressionMember
  1602. .for("concat")
  1603. .tap(CLASS_NAME, (expr, param) => {
  1604. if (!param.isString() && !param.isWrapped()) return;
  1605. let stringSuffix = null;
  1606. let hasUnknownParams = false;
  1607. /** @type {BasicEvaluatedExpression[]} */
  1608. const innerExpressions = [];
  1609. for (let i = expr.arguments.length - 1; i >= 0; i--) {
  1610. const arg = expr.arguments[i];
  1611. if (arg.type === "SpreadElement") return;
  1612. const argExpr = this.evaluateExpression(arg);
  1613. if (
  1614. hasUnknownParams ||
  1615. (!argExpr.isString() && !argExpr.isNumber())
  1616. ) {
  1617. hasUnknownParams = true;
  1618. innerExpressions.push(argExpr);
  1619. continue;
  1620. }
  1621. const value = argExpr.isString()
  1622. ? /** @type {string} */ (argExpr.string)
  1623. : String(argExpr.number);
  1624. /** @type {string} */
  1625. const newString =
  1626. value +
  1627. (stringSuffix ? /** @type {string} */ (stringSuffix.string) : "");
  1628. const newRange = /** @type {Range} */ ([
  1629. /** @type {Range} */ (argExpr.range)[0],
  1630. /** @type {Range} */ ((stringSuffix || argExpr).range)[1]
  1631. ]);
  1632. stringSuffix = new BasicEvaluatedExpression()
  1633. .setString(newString)
  1634. .setSideEffects(
  1635. (stringSuffix && stringSuffix.couldHaveSideEffects()) ||
  1636. argExpr.couldHaveSideEffects()
  1637. )
  1638. .setRange(newRange);
  1639. }
  1640. if (hasUnknownParams) {
  1641. const prefix = param.isString() ? param : param.prefix;
  1642. const inner =
  1643. param.isWrapped() && param.wrappedInnerExpressions
  1644. ? [
  1645. ...param.wrappedInnerExpressions,
  1646. ...innerExpressions.reverse()
  1647. ]
  1648. : innerExpressions.reverse();
  1649. return new BasicEvaluatedExpression()
  1650. .setWrapped(prefix, stringSuffix, inner)
  1651. .setRange(/** @type {Range} */ (expr.range));
  1652. } else if (param.isWrapped()) {
  1653. const postfix = stringSuffix || param.postfix;
  1654. const inner = param.wrappedInnerExpressions
  1655. ? [...param.wrappedInnerExpressions, ...innerExpressions.reverse()]
  1656. : innerExpressions.reverse();
  1657. return new BasicEvaluatedExpression()
  1658. .setWrapped(param.prefix, postfix, inner)
  1659. .setRange(/** @type {Range} */ (expr.range));
  1660. }
  1661. const newString =
  1662. /** @type {string} */ (param.string) +
  1663. (stringSuffix ? stringSuffix.string : "");
  1664. return new BasicEvaluatedExpression()
  1665. .setString(newString)
  1666. .setSideEffects(
  1667. (stringSuffix && stringSuffix.couldHaveSideEffects()) ||
  1668. param.couldHaveSideEffects()
  1669. )
  1670. .setRange(/** @type {Range} */ (expr.range));
  1671. });
  1672. this.hooks.evaluateCallExpressionMember
  1673. .for("split")
  1674. .tap(CLASS_NAME, (expr, param) => {
  1675. if (!param.isString()) return;
  1676. if (expr.arguments.length !== 1) return;
  1677. if (expr.arguments[0].type === "SpreadElement") return;
  1678. let result;
  1679. const arg = this.evaluateExpression(expr.arguments[0]);
  1680. if (arg.isString()) {
  1681. result =
  1682. /** @type {string} */
  1683. (param.string).split(/** @type {string} */ (arg.string));
  1684. } else if (arg.isRegExp()) {
  1685. result = /** @type {string} */ (param.string).split(
  1686. /** @type {RegExp} */ (arg.regExp)
  1687. );
  1688. } else {
  1689. return;
  1690. }
  1691. return new BasicEvaluatedExpression()
  1692. .setArray(result)
  1693. .setSideEffects(param.couldHaveSideEffects())
  1694. .setRange(/** @type {Range} */ (expr.range));
  1695. });
  1696. this.hooks.evaluate
  1697. .for("ConditionalExpression")
  1698. .tap(CLASS_NAME, (_expr) => {
  1699. const expr = /** @type {ConditionalExpression} */ (_expr);
  1700. const condition = this.evaluateExpression(expr.test);
  1701. const conditionValue = condition.asBool();
  1702. let res;
  1703. if (conditionValue === undefined) {
  1704. const consequent = this.evaluateExpression(expr.consequent);
  1705. const alternate = this.evaluateExpression(expr.alternate);
  1706. res = new BasicEvaluatedExpression();
  1707. if (consequent.isConditional()) {
  1708. res.setOptions(
  1709. /** @type {BasicEvaluatedExpression[]} */ (consequent.options)
  1710. );
  1711. } else {
  1712. res.setOptions([consequent]);
  1713. }
  1714. if (alternate.isConditional()) {
  1715. res.addOptions(
  1716. /** @type {BasicEvaluatedExpression[]} */ (alternate.options)
  1717. );
  1718. } else {
  1719. res.addOptions([alternate]);
  1720. }
  1721. } else {
  1722. res = this.evaluateExpression(
  1723. conditionValue ? expr.consequent : expr.alternate
  1724. );
  1725. if (condition.couldHaveSideEffects()) res.setSideEffects();
  1726. }
  1727. res.setRange(/** @type {Range} */ (expr.range));
  1728. return res;
  1729. });
  1730. this.hooks.evaluate.for("ArrayExpression").tap(CLASS_NAME, (_expr) => {
  1731. const expr = /** @type {ArrayExpression} */ (_expr);
  1732. const items = expr.elements.map(
  1733. (element) =>
  1734. element !== null &&
  1735. element.type !== "SpreadElement" &&
  1736. this.evaluateExpression(element)
  1737. );
  1738. if (!items.every(Boolean)) return;
  1739. return new BasicEvaluatedExpression()
  1740. .setItems(/** @type {BasicEvaluatedExpression[]} */ (items))
  1741. .setRange(/** @type {Range} */ (expr.range));
  1742. });
  1743. this.hooks.evaluate.for("ChainExpression").tap(CLASS_NAME, (_expr) => {
  1744. const expr = /** @type {ChainExpression} */ (_expr);
  1745. /** @type {Expression[]} */
  1746. const optionalExpressionsStack = [];
  1747. /** @type {Expression|Super} */
  1748. let next = expr.expression;
  1749. while (
  1750. next.type === "MemberExpression" ||
  1751. next.type === "CallExpression"
  1752. ) {
  1753. if (next.type === "MemberExpression") {
  1754. if (next.optional) {
  1755. // SuperNode can not be optional
  1756. optionalExpressionsStack.push(
  1757. /** @type {Expression} */ (next.object)
  1758. );
  1759. }
  1760. next = next.object;
  1761. } else {
  1762. if (next.optional) {
  1763. // SuperNode can not be optional
  1764. optionalExpressionsStack.push(
  1765. /** @type {Expression} */ (next.callee)
  1766. );
  1767. }
  1768. next = next.callee;
  1769. }
  1770. }
  1771. while (optionalExpressionsStack.length > 0) {
  1772. const expression =
  1773. /** @type {Expression} */
  1774. (optionalExpressionsStack.pop());
  1775. const evaluated = this.evaluateExpression(expression);
  1776. if (evaluated.asNullish()) {
  1777. return evaluated.setRange(/** @type {Range} */ (_expr.range));
  1778. }
  1779. }
  1780. return this.evaluateExpression(expr.expression);
  1781. });
  1782. }
  1783. /**
  1784. * @param {Expression} node node
  1785. * @returns {Set<DestructuringAssignmentProperty> | undefined} destructured identifiers
  1786. */
  1787. destructuringAssignmentPropertiesFor(node) {
  1788. if (!this.destructuringAssignmentProperties) return;
  1789. return this.destructuringAssignmentProperties.get(node);
  1790. }
  1791. /**
  1792. * @param {Expression | SpreadElement} expr expression
  1793. * @returns {string | VariableInfo | undefined} identifier
  1794. */
  1795. getRenameIdentifier(expr) {
  1796. const result = this.evaluateExpression(expr);
  1797. if (result.isIdentifier()) {
  1798. return result.identifier;
  1799. }
  1800. }
  1801. /**
  1802. * @param {ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration} classy a class node
  1803. * @returns {void}
  1804. */
  1805. walkClass(classy) {
  1806. if (
  1807. classy.superClass &&
  1808. !this.hooks.classExtendsExpression.call(classy.superClass, classy)
  1809. ) {
  1810. this.walkExpression(classy.superClass);
  1811. }
  1812. if (classy.body && classy.body.type === "ClassBody") {
  1813. const scopeParams = [];
  1814. // Add class name in scope for recursive calls
  1815. if (classy.id) {
  1816. scopeParams.push(classy.id);
  1817. }
  1818. this.inClassScope(true, scopeParams, () => {
  1819. for (const classElement of classy.body.body) {
  1820. if (!this.hooks.classBodyElement.call(classElement, classy)) {
  1821. if (classElement.type === "StaticBlock") {
  1822. const wasTopLevel = this.scope.topLevelScope;
  1823. this.scope.topLevelScope = false;
  1824. this.walkBlockStatement(classElement);
  1825. this.scope.topLevelScope = wasTopLevel;
  1826. } else {
  1827. if (classElement.computed && classElement.key) {
  1828. this.walkExpression(classElement.key);
  1829. }
  1830. if (
  1831. classElement.value &&
  1832. !this.hooks.classBodyValue.call(
  1833. classElement.value,
  1834. classElement,
  1835. classy
  1836. )
  1837. ) {
  1838. const wasTopLevel = this.scope.topLevelScope;
  1839. this.scope.topLevelScope = false;
  1840. this.walkExpression(classElement.value);
  1841. this.scope.topLevelScope = wasTopLevel;
  1842. }
  1843. }
  1844. }
  1845. }
  1846. });
  1847. }
  1848. }
  1849. /**
  1850. * Module pre walking iterates the scope for import entries
  1851. * @param {(Statement | ModuleDeclaration)[]} statements statements
  1852. */
  1853. modulePreWalkStatements(statements) {
  1854. for (let index = 0, len = statements.length; index < len; index++) {
  1855. const statement = statements[index];
  1856. /** @type {StatementPath} */
  1857. (this.statementPath).push(statement);
  1858. switch (statement.type) {
  1859. case "ImportDeclaration":
  1860. this.modulePreWalkImportDeclaration(statement);
  1861. break;
  1862. case "ExportAllDeclaration":
  1863. this.modulePreWalkExportAllDeclaration(statement);
  1864. break;
  1865. case "ExportNamedDeclaration":
  1866. this.modulePreWalkExportNamedDeclaration(statement);
  1867. break;
  1868. }
  1869. this.prevStatement =
  1870. /** @type {StatementPath} */
  1871. (this.statementPath).pop();
  1872. }
  1873. }
  1874. /**
  1875. * Pre walking iterates the scope for variable declarations
  1876. * @param {(Statement | ModuleDeclaration)[]} statements statements
  1877. */
  1878. preWalkStatements(statements) {
  1879. for (let index = 0, len = statements.length; index < len; index++) {
  1880. const statement = statements[index];
  1881. this.preWalkStatement(statement);
  1882. }
  1883. }
  1884. /**
  1885. * Block pre walking iterates the scope for block variable declarations
  1886. * @param {(Statement | ModuleDeclaration)[]} statements statements
  1887. */
  1888. blockPreWalkStatements(statements) {
  1889. for (let index = 0, len = statements.length; index < len; index++) {
  1890. const statement = statements[index];
  1891. this.blockPreWalkStatement(statement);
  1892. }
  1893. }
  1894. /**
  1895. * Walking iterates the statements and expressions and processes them
  1896. * @param {(Statement | ModuleDeclaration)[]} statements statements
  1897. */
  1898. walkStatements(statements) {
  1899. let onlyFunctionDeclaration = false;
  1900. for (let index = 0, len = statements.length; index < len; index++) {
  1901. const statement = statements[index];
  1902. if (
  1903. onlyFunctionDeclaration &&
  1904. statement.type !== "FunctionDeclaration" &&
  1905. this.hooks.unusedStatement.call(/** @type {Statement} */ (statement))
  1906. ) {
  1907. continue;
  1908. }
  1909. this.walkStatement(statement);
  1910. if (this.scope.terminated) {
  1911. onlyFunctionDeclaration = true;
  1912. }
  1913. }
  1914. }
  1915. /**
  1916. * Walking iterates the statements and expressions and processes them
  1917. * @param {Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration} statement statement
  1918. */
  1919. preWalkStatement(statement) {
  1920. /** @type {StatementPath} */
  1921. (this.statementPath).push(statement);
  1922. if (this.hooks.preStatement.call(statement)) {
  1923. this.prevStatement =
  1924. /** @type {StatementPath} */
  1925. (this.statementPath).pop();
  1926. return;
  1927. }
  1928. switch (statement.type) {
  1929. case "BlockStatement":
  1930. this.preWalkBlockStatement(statement);
  1931. break;
  1932. case "DoWhileStatement":
  1933. this.preWalkDoWhileStatement(statement);
  1934. break;
  1935. case "ForInStatement":
  1936. this.preWalkForInStatement(statement);
  1937. break;
  1938. case "ForOfStatement":
  1939. this.preWalkForOfStatement(statement);
  1940. break;
  1941. case "ForStatement":
  1942. this.preWalkForStatement(statement);
  1943. break;
  1944. case "FunctionDeclaration":
  1945. this.preWalkFunctionDeclaration(statement);
  1946. break;
  1947. case "IfStatement":
  1948. this.preWalkIfStatement(statement);
  1949. break;
  1950. case "LabeledStatement":
  1951. this.preWalkLabeledStatement(statement);
  1952. break;
  1953. case "SwitchStatement":
  1954. this.preWalkSwitchStatement(statement);
  1955. break;
  1956. case "TryStatement":
  1957. this.preWalkTryStatement(statement);
  1958. break;
  1959. case "VariableDeclaration":
  1960. this.preWalkVariableDeclaration(statement);
  1961. break;
  1962. case "WhileStatement":
  1963. this.preWalkWhileStatement(statement);
  1964. break;
  1965. case "WithStatement":
  1966. this.preWalkWithStatement(statement);
  1967. break;
  1968. }
  1969. this.prevStatement =
  1970. /** @type {StatementPath} */
  1971. (this.statementPath).pop();
  1972. }
  1973. /**
  1974. * @param {Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration} statement statement
  1975. */
  1976. blockPreWalkStatement(statement) {
  1977. /** @type {StatementPath} */
  1978. (this.statementPath).push(statement);
  1979. if (this.hooks.blockPreStatement.call(statement)) {
  1980. this.prevStatement =
  1981. /** @type {StatementPath} */
  1982. (this.statementPath).pop();
  1983. return;
  1984. }
  1985. switch (statement.type) {
  1986. case "ExportDefaultDeclaration":
  1987. this.blockPreWalkExportDefaultDeclaration(statement);
  1988. break;
  1989. case "ExportNamedDeclaration":
  1990. this.blockPreWalkExportNamedDeclaration(statement);
  1991. break;
  1992. case "VariableDeclaration":
  1993. this.blockPreWalkVariableDeclaration(statement);
  1994. break;
  1995. case "ClassDeclaration":
  1996. this.blockPreWalkClassDeclaration(statement);
  1997. break;
  1998. case "ExpressionStatement":
  1999. this.blockPreWalkExpressionStatement(statement);
  2000. }
  2001. this.prevStatement =
  2002. /** @type {StatementPath} */
  2003. (this.statementPath).pop();
  2004. }
  2005. /**
  2006. * @param {Statement | ModuleDeclaration | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} statement statement
  2007. */
  2008. walkStatement(statement) {
  2009. /** @type {StatementPath} */
  2010. (this.statementPath).push(statement);
  2011. if (this.hooks.statement.call(statement) !== undefined) {
  2012. this.prevStatement =
  2013. /** @type {StatementPath} */
  2014. (this.statementPath).pop();
  2015. return;
  2016. }
  2017. switch (statement.type) {
  2018. case "BlockStatement":
  2019. this.walkBlockStatement(statement);
  2020. break;
  2021. case "ClassDeclaration":
  2022. this.walkClassDeclaration(statement);
  2023. break;
  2024. case "DoWhileStatement":
  2025. this.walkDoWhileStatement(statement);
  2026. break;
  2027. case "ExportDefaultDeclaration":
  2028. this.walkExportDefaultDeclaration(statement);
  2029. break;
  2030. case "ExportNamedDeclaration":
  2031. this.walkExportNamedDeclaration(statement);
  2032. break;
  2033. case "ExpressionStatement":
  2034. this.walkExpressionStatement(statement);
  2035. break;
  2036. case "ForInStatement":
  2037. this.walkForInStatement(statement);
  2038. break;
  2039. case "ForOfStatement":
  2040. this.walkForOfStatement(statement);
  2041. break;
  2042. case "ForStatement":
  2043. this.walkForStatement(statement);
  2044. break;
  2045. case "FunctionDeclaration":
  2046. this.walkFunctionDeclaration(statement);
  2047. break;
  2048. case "IfStatement":
  2049. this.walkIfStatement(statement);
  2050. break;
  2051. case "LabeledStatement":
  2052. this.walkLabeledStatement(statement);
  2053. break;
  2054. case "ReturnStatement":
  2055. this.walkReturnStatement(statement);
  2056. break;
  2057. case "SwitchStatement":
  2058. this.walkSwitchStatement(statement);
  2059. break;
  2060. case "ThrowStatement":
  2061. this.walkThrowStatement(statement);
  2062. break;
  2063. case "TryStatement":
  2064. this.walkTryStatement(statement);
  2065. break;
  2066. case "VariableDeclaration":
  2067. this.walkVariableDeclaration(statement);
  2068. break;
  2069. case "WhileStatement":
  2070. this.walkWhileStatement(statement);
  2071. break;
  2072. case "WithStatement":
  2073. this.walkWithStatement(statement);
  2074. break;
  2075. }
  2076. this.prevStatement =
  2077. /** @type {StatementPath} */
  2078. (this.statementPath).pop();
  2079. }
  2080. /**
  2081. * Walks a statements that is nested within a parent statement
  2082. * and can potentially be a non-block statement.
  2083. * This enforces the nested statement to never be in ASI position.
  2084. * @param {Statement} statement the nested statement
  2085. */
  2086. walkNestedStatement(statement) {
  2087. this.prevStatement = undefined;
  2088. this.walkStatement(statement);
  2089. }
  2090. // Real Statements
  2091. /**
  2092. * @param {BlockStatement} statement block statement
  2093. */
  2094. preWalkBlockStatement(statement) {
  2095. this.preWalkStatements(statement.body);
  2096. }
  2097. /**
  2098. * @param {BlockStatement | StaticBlock} statement block statement
  2099. */
  2100. walkBlockStatement(statement) {
  2101. this.inBlockScope(() => {
  2102. const body = statement.body;
  2103. const prev = this.prevStatement;
  2104. this.blockPreWalkStatements(body);
  2105. this.prevStatement = prev;
  2106. this.walkStatements(body);
  2107. }, true);
  2108. }
  2109. /**
  2110. * @param {ExpressionStatement} statement expression statement
  2111. */
  2112. walkExpressionStatement(statement) {
  2113. this.walkExpression(statement.expression);
  2114. }
  2115. /**
  2116. * @param {IfStatement} statement if statement
  2117. */
  2118. preWalkIfStatement(statement) {
  2119. this.preWalkStatement(statement.consequent);
  2120. if (statement.alternate) {
  2121. this.preWalkStatement(statement.alternate);
  2122. }
  2123. }
  2124. /**
  2125. * @param {IfStatement} statement if statement
  2126. */
  2127. walkIfStatement(statement) {
  2128. const result = this.hooks.statementIf.call(statement);
  2129. if (result === undefined) {
  2130. this.walkExpression(statement.test);
  2131. this.walkNestedStatement(statement.consequent);
  2132. const consequentTerminated = this.scope.terminated;
  2133. this.scope.terminated = undefined;
  2134. if (statement.alternate) {
  2135. this.walkNestedStatement(statement.alternate);
  2136. }
  2137. const alternateTerminated = this.scope.terminated;
  2138. this.scope.terminated =
  2139. consequentTerminated && alternateTerminated
  2140. ? alternateTerminated
  2141. : undefined;
  2142. } else if (result) {
  2143. this.walkNestedStatement(statement.consequent);
  2144. } else if (statement.alternate) {
  2145. this.walkNestedStatement(statement.alternate);
  2146. }
  2147. }
  2148. /**
  2149. * @param {LabeledStatement} statement with statement
  2150. */
  2151. preWalkLabeledStatement(statement) {
  2152. this.preWalkStatement(statement.body);
  2153. }
  2154. /**
  2155. * @param {LabeledStatement} statement with statement
  2156. */
  2157. walkLabeledStatement(statement) {
  2158. const hook = this.hooks.label.get(statement.label.name);
  2159. if (hook !== undefined) {
  2160. const result = hook.call(statement);
  2161. if (result === true) return;
  2162. }
  2163. this.inBlockScope(() => {
  2164. this.walkNestedStatement(statement.body);
  2165. });
  2166. }
  2167. /**
  2168. * @param {WithStatement} statement with statement
  2169. */
  2170. preWalkWithStatement(statement) {
  2171. this.preWalkStatement(statement.body);
  2172. }
  2173. /**
  2174. * @param {WithStatement} statement with statement
  2175. */
  2176. walkWithStatement(statement) {
  2177. this.inBlockScope(() => {
  2178. this.walkExpression(statement.object);
  2179. this.walkNestedStatement(statement.body);
  2180. });
  2181. }
  2182. /**
  2183. * @param {SwitchStatement} statement switch statement
  2184. */
  2185. preWalkSwitchStatement(statement) {
  2186. this.preWalkSwitchCases(statement.cases);
  2187. }
  2188. /**
  2189. * @param {SwitchStatement} statement switch statement
  2190. */
  2191. walkSwitchStatement(statement) {
  2192. this.walkExpression(statement.discriminant);
  2193. this.walkSwitchCases(statement.cases);
  2194. }
  2195. /**
  2196. * @param {ReturnStatement | ThrowStatement} statement return or throw statement
  2197. */
  2198. walkTerminatingStatement(statement) {
  2199. if (statement.argument) this.walkExpression(statement.argument);
  2200. // Skip top level scope because to handle `export` and `module.exports` after terminate
  2201. if (this.scope.topLevelScope === true) return;
  2202. if (this.hooks.terminate.call(statement)) {
  2203. this.scope.terminated =
  2204. statement.type === "ReturnStatement"
  2205. ? SCOPE_INFO_TERMINATED_RETURN
  2206. : SCOPE_INFO_TERMINATED_THROW;
  2207. }
  2208. }
  2209. /**
  2210. * @param {ReturnStatement} statement return statement
  2211. */
  2212. walkReturnStatement(statement) {
  2213. this.walkTerminatingStatement(statement);
  2214. }
  2215. /**
  2216. * @param {ThrowStatement} statement return statement
  2217. */
  2218. walkThrowStatement(statement) {
  2219. this.walkTerminatingStatement(statement);
  2220. }
  2221. /**
  2222. * @param {TryStatement} statement try statement
  2223. */
  2224. preWalkTryStatement(statement) {
  2225. this.preWalkStatement(statement.block);
  2226. if (statement.handler) this.preWalkCatchClause(statement.handler);
  2227. if (statement.finalizer) this.preWalkStatement(statement.finalizer);
  2228. }
  2229. /**
  2230. * @param {TryStatement} statement try statement
  2231. */
  2232. walkTryStatement(statement) {
  2233. if (this.scope.inTry) {
  2234. this.walkStatement(statement.block);
  2235. } else {
  2236. this.scope.inTry = true;
  2237. this.walkStatement(statement.block);
  2238. this.scope.inTry = false;
  2239. }
  2240. const tryTerminated = this.scope.terminated;
  2241. this.scope.terminated = undefined;
  2242. if (statement.handler) this.walkCatchClause(statement.handler);
  2243. const handlerTerminated = this.scope.terminated;
  2244. this.scope.terminated = undefined;
  2245. if (statement.finalizer) {
  2246. this.walkStatement(statement.finalizer);
  2247. }
  2248. const finalizerTerminated = this.scope.terminated;
  2249. this.scope.terminated = undefined;
  2250. if (finalizerTerminated) {
  2251. this.scope.terminated = finalizerTerminated;
  2252. } else if (
  2253. tryTerminated &&
  2254. (statement.handler ? handlerTerminated : true)
  2255. ) {
  2256. this.scope.terminated = handlerTerminated || tryTerminated;
  2257. }
  2258. }
  2259. /**
  2260. * @param {WhileStatement} statement while statement
  2261. */
  2262. preWalkWhileStatement(statement) {
  2263. this.preWalkStatement(statement.body);
  2264. }
  2265. /**
  2266. * @param {WhileStatement} statement while statement
  2267. */
  2268. walkWhileStatement(statement) {
  2269. this.inBlockScope(() => {
  2270. this.walkExpression(statement.test);
  2271. this.walkNestedStatement(statement.body);
  2272. });
  2273. }
  2274. /**
  2275. * @param {DoWhileStatement} statement do while statement
  2276. */
  2277. preWalkDoWhileStatement(statement) {
  2278. this.preWalkStatement(statement.body);
  2279. }
  2280. /**
  2281. * @param {DoWhileStatement} statement do while statement
  2282. */
  2283. walkDoWhileStatement(statement) {
  2284. this.inBlockScope(() => {
  2285. this.walkNestedStatement(statement.body);
  2286. this.walkExpression(statement.test);
  2287. });
  2288. }
  2289. /**
  2290. * @param {ForStatement} statement for statement
  2291. */
  2292. preWalkForStatement(statement) {
  2293. if (statement.init && statement.init.type === "VariableDeclaration") {
  2294. this.preWalkStatement(statement.init);
  2295. }
  2296. this.preWalkStatement(statement.body);
  2297. }
  2298. /**
  2299. * @param {ForStatement} statement for statement
  2300. */
  2301. walkForStatement(statement) {
  2302. this.inBlockScope(() => {
  2303. if (statement.init) {
  2304. if (statement.init.type === "VariableDeclaration") {
  2305. this.blockPreWalkVariableDeclaration(statement.init);
  2306. this.prevStatement = undefined;
  2307. this.walkStatement(statement.init);
  2308. } else {
  2309. this.walkExpression(statement.init);
  2310. }
  2311. }
  2312. if (statement.test) {
  2313. this.walkExpression(statement.test);
  2314. }
  2315. if (statement.update) {
  2316. this.walkExpression(statement.update);
  2317. }
  2318. const body = statement.body;
  2319. if (body.type === "BlockStatement") {
  2320. // no need to add additional scope
  2321. const prev = this.prevStatement;
  2322. this.blockPreWalkStatements(body.body);
  2323. this.prevStatement = prev;
  2324. this.walkStatements(body.body);
  2325. } else {
  2326. this.walkNestedStatement(body);
  2327. }
  2328. });
  2329. }
  2330. /**
  2331. * @param {ForInStatement} statement for statement
  2332. */
  2333. preWalkForInStatement(statement) {
  2334. if (statement.left.type === "VariableDeclaration") {
  2335. this.preWalkVariableDeclaration(statement.left);
  2336. }
  2337. this.preWalkStatement(statement.body);
  2338. }
  2339. /**
  2340. * @param {ForInStatement} statement for statement
  2341. */
  2342. walkForInStatement(statement) {
  2343. this.inBlockScope(() => {
  2344. if (statement.left.type === "VariableDeclaration") {
  2345. this.blockPreWalkVariableDeclaration(statement.left);
  2346. this.walkVariableDeclaration(statement.left);
  2347. } else {
  2348. this.walkPattern(statement.left);
  2349. }
  2350. this.walkExpression(statement.right);
  2351. const body = statement.body;
  2352. if (body.type === "BlockStatement") {
  2353. // no need to add additional scope
  2354. const prev = this.prevStatement;
  2355. this.blockPreWalkStatements(body.body);
  2356. this.prevStatement = prev;
  2357. this.walkStatements(body.body);
  2358. } else {
  2359. this.walkNestedStatement(body);
  2360. }
  2361. });
  2362. }
  2363. /**
  2364. * @param {ForOfStatement} statement statement
  2365. */
  2366. preWalkForOfStatement(statement) {
  2367. if (statement.await && this.scope.topLevelScope === true) {
  2368. this.hooks.topLevelAwait.call(statement);
  2369. }
  2370. if (statement.left.type === "VariableDeclaration") {
  2371. this.preWalkVariableDeclaration(statement.left);
  2372. }
  2373. this.preWalkStatement(statement.body);
  2374. }
  2375. /**
  2376. * @param {ForOfStatement} statement for statement
  2377. */
  2378. walkForOfStatement(statement) {
  2379. this.inBlockScope(() => {
  2380. if (statement.left.type === "VariableDeclaration") {
  2381. this.blockPreWalkVariableDeclaration(statement.left);
  2382. this.walkVariableDeclaration(statement.left);
  2383. } else {
  2384. this.walkPattern(statement.left);
  2385. }
  2386. this.walkExpression(statement.right);
  2387. const body = statement.body;
  2388. if (body.type === "BlockStatement") {
  2389. // no need to add additional scope
  2390. const prev = this.prevStatement;
  2391. this.blockPreWalkStatements(body.body);
  2392. this.prevStatement = prev;
  2393. this.walkStatements(body.body);
  2394. } else {
  2395. this.walkNestedStatement(body);
  2396. }
  2397. });
  2398. }
  2399. /**
  2400. * @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
  2401. */
  2402. preWalkFunctionDeclaration(statement) {
  2403. if (statement.id) {
  2404. this.defineVariable(statement.id.name);
  2405. }
  2406. }
  2407. /**
  2408. * @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
  2409. */
  2410. walkFunctionDeclaration(statement) {
  2411. const wasTopLevel = this.scope.topLevelScope;
  2412. this.scope.topLevelScope = false;
  2413. this.inFunctionScope(true, statement.params, () => {
  2414. for (const param of statement.params) {
  2415. this.walkPattern(param);
  2416. }
  2417. this.detectMode(statement.body.body);
  2418. const prev = this.prevStatement;
  2419. this.preWalkStatement(statement.body);
  2420. this.prevStatement = prev;
  2421. this.walkStatement(statement.body);
  2422. });
  2423. this.scope.topLevelScope = wasTopLevel;
  2424. }
  2425. /**
  2426. * @param {ExpressionStatement} statement expression statement
  2427. */
  2428. blockPreWalkExpressionStatement(statement) {
  2429. const expression = statement.expression;
  2430. switch (expression.type) {
  2431. case "AssignmentExpression":
  2432. this.preWalkAssignmentExpression(expression);
  2433. }
  2434. }
  2435. /**
  2436. * @param {AssignmentExpression} expression assignment expression
  2437. */
  2438. preWalkAssignmentExpression(expression) {
  2439. this.enterDestructuringAssignment(expression.left, expression.right);
  2440. }
  2441. /**
  2442. * @param {Pattern} pattern pattern
  2443. * @param {Expression} expression assignment expression
  2444. * @returns {Expression | undefined} destructuring expression
  2445. */
  2446. enterDestructuringAssignment(pattern, expression) {
  2447. if (
  2448. pattern.type !== "ObjectPattern" ||
  2449. !this.destructuringAssignmentProperties
  2450. ) {
  2451. return;
  2452. }
  2453. const expr =
  2454. expression.type === "AwaitExpression" ? expression.argument : expression;
  2455. const destructuring =
  2456. expr.type === "AssignmentExpression"
  2457. ? this.enterDestructuringAssignment(expr.left, expr.right)
  2458. : this.hooks.collectDestructuringAssignmentProperties.call(expr)
  2459. ? expr
  2460. : undefined;
  2461. if (destructuring) {
  2462. const keys = this._preWalkObjectPattern(pattern);
  2463. if (!keys) return;
  2464. // check multiple assignments
  2465. if (this.destructuringAssignmentProperties.has(destructuring)) {
  2466. const set =
  2467. /** @type {Set<DestructuringAssignmentProperty>} */
  2468. (this.destructuringAssignmentProperties.get(destructuring));
  2469. for (const id of keys) set.add(id);
  2470. } else {
  2471. this.destructuringAssignmentProperties.set(destructuring, keys);
  2472. }
  2473. }
  2474. return destructuring;
  2475. }
  2476. /**
  2477. * @param {ImportDeclaration} statement statement
  2478. */
  2479. modulePreWalkImportDeclaration(statement) {
  2480. const source = /** @type {ImportSource} */ (statement.source.value);
  2481. this.hooks.import.call(statement, source);
  2482. for (const specifier of statement.specifiers) {
  2483. const name = specifier.local.name;
  2484. switch (specifier.type) {
  2485. case "ImportDefaultSpecifier":
  2486. if (
  2487. !this.hooks.importSpecifier.call(statement, source, "default", name)
  2488. ) {
  2489. this.defineVariable(name);
  2490. }
  2491. break;
  2492. case "ImportSpecifier":
  2493. if (
  2494. !this.hooks.importSpecifier.call(
  2495. statement,
  2496. source,
  2497. /** @type {Identifier} */
  2498. (specifier.imported).name ||
  2499. /** @type {string} */
  2500. (
  2501. /** @type {Literal} */
  2502. (specifier.imported).value
  2503. ),
  2504. name
  2505. )
  2506. ) {
  2507. this.defineVariable(name);
  2508. }
  2509. break;
  2510. case "ImportNamespaceSpecifier":
  2511. if (!this.hooks.importSpecifier.call(statement, source, null, name)) {
  2512. this.defineVariable(name);
  2513. }
  2514. break;
  2515. default:
  2516. this.defineVariable(name);
  2517. }
  2518. }
  2519. }
  2520. /**
  2521. * @param {Declaration} declaration declaration
  2522. * @param {OnIdent} onIdent on ident callback
  2523. */
  2524. enterDeclaration(declaration, onIdent) {
  2525. switch (declaration.type) {
  2526. case "VariableDeclaration":
  2527. for (const declarator of declaration.declarations) {
  2528. switch (declarator.type) {
  2529. case "VariableDeclarator": {
  2530. this.enterPattern(declarator.id, onIdent);
  2531. break;
  2532. }
  2533. }
  2534. }
  2535. break;
  2536. case "FunctionDeclaration":
  2537. this.enterPattern(declaration.id, onIdent);
  2538. break;
  2539. case "ClassDeclaration":
  2540. this.enterPattern(declaration.id, onIdent);
  2541. break;
  2542. }
  2543. }
  2544. /**
  2545. * @param {ExportNamedDeclaration} statement statement
  2546. */
  2547. modulePreWalkExportNamedDeclaration(statement) {
  2548. if (!statement.source) return;
  2549. const source = /** @type {ImportSource} */ (statement.source.value);
  2550. this.hooks.exportImport.call(statement, source);
  2551. if (statement.specifiers) {
  2552. for (
  2553. let specifierIndex = 0;
  2554. specifierIndex < statement.specifiers.length;
  2555. specifierIndex++
  2556. ) {
  2557. const specifier = statement.specifiers[specifierIndex];
  2558. switch (specifier.type) {
  2559. case "ExportSpecifier": {
  2560. const localName =
  2561. /** @type {Identifier} */ (specifier.local).name ||
  2562. /** @type {string} */ (
  2563. /** @type {Literal} */ (specifier.local).value
  2564. );
  2565. const name =
  2566. /** @type {Identifier} */
  2567. (specifier.exported).name ||
  2568. /** @type {string} */
  2569. (/** @type {Literal} */ (specifier.exported).value);
  2570. this.hooks.exportImportSpecifier.call(
  2571. statement,
  2572. source,
  2573. localName,
  2574. name,
  2575. specifierIndex
  2576. );
  2577. break;
  2578. }
  2579. }
  2580. }
  2581. }
  2582. }
  2583. /**
  2584. * @param {ExportNamedDeclaration} statement statement
  2585. */
  2586. blockPreWalkExportNamedDeclaration(statement) {
  2587. if (statement.source) return;
  2588. this.hooks.export.call(statement);
  2589. if (
  2590. statement.declaration &&
  2591. !this.hooks.exportDeclaration.call(statement, statement.declaration)
  2592. ) {
  2593. const prev = this.prevStatement;
  2594. this.preWalkStatement(statement.declaration);
  2595. this.prevStatement = prev;
  2596. this.blockPreWalkStatement(statement.declaration);
  2597. let index = 0;
  2598. this.enterDeclaration(statement.declaration, (def) => {
  2599. this.hooks.exportSpecifier.call(statement, def, def, index++);
  2600. });
  2601. }
  2602. if (statement.specifiers) {
  2603. for (
  2604. let specifierIndex = 0;
  2605. specifierIndex < statement.specifiers.length;
  2606. specifierIndex++
  2607. ) {
  2608. const specifier = statement.specifiers[specifierIndex];
  2609. switch (specifier.type) {
  2610. case "ExportSpecifier": {
  2611. const localName =
  2612. /** @type {Identifier} */ (specifier.local).name ||
  2613. /** @type {string} */ (
  2614. /** @type {Literal} */ (specifier.local).value
  2615. );
  2616. const name =
  2617. /** @type {Identifier} */
  2618. (specifier.exported).name ||
  2619. /** @type {string} */
  2620. (/** @type {Literal} */ (specifier.exported).value);
  2621. this.hooks.exportSpecifier.call(
  2622. statement,
  2623. localName,
  2624. name,
  2625. specifierIndex
  2626. );
  2627. break;
  2628. }
  2629. }
  2630. }
  2631. }
  2632. }
  2633. /**
  2634. * @param {ExportNamedDeclaration} statement the statement
  2635. */
  2636. walkExportNamedDeclaration(statement) {
  2637. if (statement.declaration) {
  2638. this.walkStatement(statement.declaration);
  2639. }
  2640. }
  2641. /**
  2642. * @param {ExportDefaultDeclaration} statement statement
  2643. */
  2644. blockPreWalkExportDefaultDeclaration(statement) {
  2645. const prev = this.prevStatement;
  2646. this.preWalkStatement(/** @type {TODO} */ (statement.declaration));
  2647. this.prevStatement = prev;
  2648. this.blockPreWalkStatement(/** @type {TODO} */ (statement.declaration));
  2649. if (
  2650. /** @type {MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} */
  2651. (statement.declaration).id &&
  2652. statement.declaration.type !== "FunctionExpression" &&
  2653. statement.declaration.type !== "ClassExpression"
  2654. ) {
  2655. const declaration =
  2656. /** @type {MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} */
  2657. (statement.declaration);
  2658. this.hooks.exportSpecifier.call(
  2659. statement,
  2660. /** @type {Identifier} */
  2661. (declaration.id).name,
  2662. "default",
  2663. undefined
  2664. );
  2665. }
  2666. }
  2667. /**
  2668. * @param {ExportDefaultDeclaration} statement statement
  2669. */
  2670. walkExportDefaultDeclaration(statement) {
  2671. this.hooks.export.call(statement);
  2672. if (
  2673. /** @type {FunctionDeclaration | ClassDeclaration} */
  2674. (statement.declaration).id &&
  2675. statement.declaration.type !== "FunctionExpression" &&
  2676. statement.declaration.type !== "ClassExpression"
  2677. ) {
  2678. const declaration =
  2679. /** @type {FunctionDeclaration | ClassDeclaration} */
  2680. (statement.declaration);
  2681. if (!this.hooks.exportDeclaration.call(statement, declaration)) {
  2682. this.walkStatement(declaration);
  2683. }
  2684. } else {
  2685. // Acorn parses `export default function() {}` as `FunctionDeclaration` and
  2686. // `export default class {}` as `ClassDeclaration`, both with `id = null`.
  2687. // These nodes must be treated as expressions.
  2688. if (
  2689. statement.declaration.type === "FunctionDeclaration" ||
  2690. statement.declaration.type === "ClassDeclaration"
  2691. ) {
  2692. this.walkStatement(statement.declaration);
  2693. } else {
  2694. this.walkExpression(statement.declaration);
  2695. }
  2696. if (!this.hooks.exportExpression.call(statement, statement.declaration)) {
  2697. this.hooks.exportSpecifier.call(
  2698. statement,
  2699. /** @type {TODO} */
  2700. (statement.declaration),
  2701. "default",
  2702. undefined
  2703. );
  2704. }
  2705. }
  2706. }
  2707. /**
  2708. * @param {ExportAllDeclaration} statement statement
  2709. */
  2710. modulePreWalkExportAllDeclaration(statement) {
  2711. const source = /** @type {ImportSource} */ (statement.source.value);
  2712. const name = statement.exported
  2713. ? /** @type {Identifier} */
  2714. (statement.exported).name ||
  2715. /** @type {string} */
  2716. (/** @type {Literal} */ (statement.exported).value)
  2717. : null;
  2718. this.hooks.exportImport.call(statement, source);
  2719. this.hooks.exportImportSpecifier.call(statement, source, null, name, 0);
  2720. }
  2721. /**
  2722. * @param {VariableDeclaration} statement variable declaration
  2723. */
  2724. preWalkVariableDeclaration(statement) {
  2725. if (statement.kind !== "var") return;
  2726. this._preWalkVariableDeclaration(statement, this.hooks.varDeclarationVar);
  2727. }
  2728. /**
  2729. * @param {VariableDeclaration} statement variable declaration
  2730. */
  2731. blockPreWalkVariableDeclaration(statement) {
  2732. if (statement.kind === "var") return;
  2733. const hookMap =
  2734. statement.kind === "const"
  2735. ? this.hooks.varDeclarationConst
  2736. : statement.kind === "using" || statement.kind === "await using"
  2737. ? this.hooks.varDeclarationUsing
  2738. : this.hooks.varDeclarationLet;
  2739. this._preWalkVariableDeclaration(statement, hookMap);
  2740. }
  2741. /**
  2742. * @param {VariableDeclaration} statement variable declaration
  2743. * @param {HookMap<SyncBailHook<[Identifier], boolean | void>>} hookMap map of hooks
  2744. */
  2745. _preWalkVariableDeclaration(statement, hookMap) {
  2746. for (const declarator of statement.declarations) {
  2747. switch (declarator.type) {
  2748. case "VariableDeclarator": {
  2749. this.preWalkVariableDeclarator(declarator);
  2750. if (!this.hooks.preDeclarator.call(declarator, statement)) {
  2751. this.enterPattern(declarator.id, (name, ident) => {
  2752. let hook = hookMap.get(name);
  2753. if (hook === undefined || !hook.call(ident)) {
  2754. hook = this.hooks.varDeclaration.get(name);
  2755. if (hook === undefined || !hook.call(ident)) {
  2756. this.defineVariable(name);
  2757. }
  2758. }
  2759. });
  2760. }
  2761. break;
  2762. }
  2763. }
  2764. }
  2765. }
  2766. /**
  2767. * @param {ObjectPattern} objectPattern object pattern
  2768. * @returns {Set<DestructuringAssignmentProperty> | undefined} set of names or undefined if not all keys are identifiers
  2769. */
  2770. _preWalkObjectPattern(objectPattern) {
  2771. /** @type {Set<DestructuringAssignmentProperty>} */
  2772. const props = new Set();
  2773. const properties = objectPattern.properties;
  2774. for (let i = 0; i < properties.length; i++) {
  2775. const property = properties[i];
  2776. if (property.type !== "Property") return;
  2777. if (property.shorthand) {
  2778. if (property.value.type === "Identifier") {
  2779. this.scope.inShorthand = property.value.name;
  2780. } else if (
  2781. property.value.type === "AssignmentPattern" &&
  2782. property.value.left.type === "Identifier"
  2783. ) {
  2784. this.scope.inShorthand = property.value.left.name;
  2785. }
  2786. }
  2787. const key = property.key;
  2788. if (key.type === "Identifier" && !property.computed) {
  2789. props.add({
  2790. id: key.name,
  2791. range: key.range,
  2792. shorthand: this.scope.inShorthand
  2793. });
  2794. } else {
  2795. const id = this.evaluateExpression(key);
  2796. const str = id.asString();
  2797. if (str) {
  2798. props.add({
  2799. id: str,
  2800. range: key.range,
  2801. shorthand: this.scope.inShorthand
  2802. });
  2803. } else {
  2804. // could not evaluate key
  2805. return;
  2806. }
  2807. }
  2808. this.scope.inShorthand = false;
  2809. }
  2810. return props;
  2811. }
  2812. /**
  2813. * @param {VariableDeclarator} declarator variable declarator
  2814. */
  2815. preWalkVariableDeclarator(declarator) {
  2816. if (declarator.init) {
  2817. this.enterDestructuringAssignment(declarator.id, declarator.init);
  2818. }
  2819. }
  2820. /**
  2821. * @param {VariableDeclaration} statement variable declaration
  2822. */
  2823. walkVariableDeclaration(statement) {
  2824. for (const declarator of statement.declarations) {
  2825. switch (declarator.type) {
  2826. case "VariableDeclarator": {
  2827. const renameIdentifier =
  2828. declarator.init && this.getRenameIdentifier(declarator.init);
  2829. if (renameIdentifier && declarator.id.type === "Identifier") {
  2830. const hook = this.hooks.canRename.get(renameIdentifier);
  2831. if (
  2832. hook !== undefined &&
  2833. hook.call(/** @type {Expression} */ (declarator.init))
  2834. ) {
  2835. // renaming with "var a = b;"
  2836. const hook = this.hooks.rename.get(renameIdentifier);
  2837. if (
  2838. hook === undefined ||
  2839. !hook.call(/** @type {Expression} */ (declarator.init))
  2840. ) {
  2841. this.setVariable(declarator.id.name, renameIdentifier);
  2842. }
  2843. break;
  2844. }
  2845. }
  2846. if (!this.hooks.declarator.call(declarator, statement)) {
  2847. this.walkPattern(declarator.id);
  2848. if (declarator.init) this.walkExpression(declarator.init);
  2849. }
  2850. break;
  2851. }
  2852. }
  2853. }
  2854. }
  2855. /**
  2856. * @param {ClassDeclaration | MaybeNamedClassDeclaration} statement class declaration
  2857. */
  2858. blockPreWalkClassDeclaration(statement) {
  2859. if (statement.id) {
  2860. this.defineVariable(statement.id.name);
  2861. }
  2862. }
  2863. /**
  2864. * @param {ClassDeclaration | MaybeNamedClassDeclaration} statement class declaration
  2865. */
  2866. walkClassDeclaration(statement) {
  2867. this.walkClass(statement);
  2868. }
  2869. /**
  2870. * @param {SwitchCase[]} switchCases switch statement
  2871. */
  2872. preWalkSwitchCases(switchCases) {
  2873. for (let index = 0, len = switchCases.length; index < len; index++) {
  2874. const switchCase = switchCases[index];
  2875. this.preWalkStatements(switchCase.consequent);
  2876. }
  2877. }
  2878. /**
  2879. * @param {SwitchCase[]} switchCases switch statement
  2880. */
  2881. walkSwitchCases(switchCases) {
  2882. this.inBlockScope(() => {
  2883. const len = switchCases.length;
  2884. // we need to pre walk all statements first since we can have invalid code
  2885. // import A from "module";
  2886. // switch(1) {
  2887. // case 1:
  2888. // console.log(A); // should fail at runtime
  2889. // case 2:
  2890. // const A = 1;
  2891. // }
  2892. for (let index = 0; index < len; index++) {
  2893. const switchCase = switchCases[index];
  2894. if (switchCase.consequent.length > 0) {
  2895. const prev = this.prevStatement;
  2896. this.blockPreWalkStatements(switchCase.consequent);
  2897. this.prevStatement = prev;
  2898. }
  2899. }
  2900. for (let index = 0; index < len; index++) {
  2901. const switchCase = switchCases[index];
  2902. if (switchCase.test) {
  2903. this.walkExpression(switchCase.test);
  2904. }
  2905. if (switchCase.consequent.length > 0) {
  2906. this.walkStatements(switchCase.consequent);
  2907. this.scope.terminated = undefined;
  2908. }
  2909. }
  2910. });
  2911. }
  2912. /**
  2913. * @param {CatchClause} catchClause catch clause
  2914. */
  2915. preWalkCatchClause(catchClause) {
  2916. this.preWalkStatement(catchClause.body);
  2917. }
  2918. /**
  2919. * @param {CatchClause} catchClause catch clause
  2920. */
  2921. walkCatchClause(catchClause) {
  2922. this.inBlockScope(() => {
  2923. // Error binding is optional in catch clause since ECMAScript 2019
  2924. if (catchClause.param !== null) {
  2925. this.enterPattern(catchClause.param, (ident) => {
  2926. this.defineVariable(ident);
  2927. });
  2928. this.walkPattern(catchClause.param);
  2929. }
  2930. const prev = this.prevStatement;
  2931. this.blockPreWalkStatement(catchClause.body);
  2932. this.prevStatement = prev;
  2933. this.walkStatement(catchClause.body);
  2934. }, true);
  2935. }
  2936. /**
  2937. * @param {Pattern} pattern pattern
  2938. */
  2939. walkPattern(pattern) {
  2940. switch (pattern.type) {
  2941. case "ArrayPattern":
  2942. this.walkArrayPattern(pattern);
  2943. break;
  2944. case "AssignmentPattern":
  2945. this.walkAssignmentPattern(pattern);
  2946. break;
  2947. case "MemberExpression":
  2948. this.walkMemberExpression(pattern);
  2949. break;
  2950. case "ObjectPattern":
  2951. this.walkObjectPattern(pattern);
  2952. break;
  2953. case "RestElement":
  2954. this.walkRestElement(pattern);
  2955. break;
  2956. }
  2957. }
  2958. /**
  2959. * @param {AssignmentPattern} pattern assignment pattern
  2960. */
  2961. walkAssignmentPattern(pattern) {
  2962. this.walkExpression(pattern.right);
  2963. this.walkPattern(pattern.left);
  2964. }
  2965. /**
  2966. * @param {ObjectPattern} pattern pattern
  2967. */
  2968. walkObjectPattern(pattern) {
  2969. for (let i = 0, len = pattern.properties.length; i < len; i++) {
  2970. const prop = pattern.properties[i];
  2971. if (prop) {
  2972. if (prop.type === "RestElement") {
  2973. continue;
  2974. }
  2975. if (prop.computed) this.walkExpression(prop.key);
  2976. if (prop.value) this.walkPattern(prop.value);
  2977. }
  2978. }
  2979. }
  2980. /**
  2981. * @param {ArrayPattern} pattern array pattern
  2982. */
  2983. walkArrayPattern(pattern) {
  2984. for (let i = 0, len = pattern.elements.length; i < len; i++) {
  2985. const element = pattern.elements[i];
  2986. if (element) this.walkPattern(element);
  2987. }
  2988. }
  2989. /**
  2990. * @param {RestElement} pattern rest element
  2991. */
  2992. walkRestElement(pattern) {
  2993. this.walkPattern(pattern.argument);
  2994. }
  2995. /**
  2996. * @param {(Expression | SpreadElement | null)[]} expressions expressions
  2997. */
  2998. walkExpressions(expressions) {
  2999. for (const expression of expressions) {
  3000. if (expression) {
  3001. this.walkExpression(expression);
  3002. }
  3003. }
  3004. }
  3005. /**
  3006. * @param {Expression | SpreadElement | PrivateIdentifier | Super} expression expression
  3007. */
  3008. walkExpression(expression) {
  3009. switch (expression.type) {
  3010. case "ArrayExpression":
  3011. this.walkArrayExpression(expression);
  3012. break;
  3013. case "ArrowFunctionExpression":
  3014. this.walkArrowFunctionExpression(expression);
  3015. break;
  3016. case "AssignmentExpression":
  3017. this.walkAssignmentExpression(expression);
  3018. break;
  3019. case "AwaitExpression":
  3020. this.walkAwaitExpression(expression);
  3021. break;
  3022. case "BinaryExpression":
  3023. this.walkBinaryExpression(expression);
  3024. break;
  3025. case "CallExpression":
  3026. this.walkCallExpression(expression);
  3027. break;
  3028. case "ChainExpression":
  3029. this.walkChainExpression(expression);
  3030. break;
  3031. case "ClassExpression":
  3032. this.walkClassExpression(expression);
  3033. break;
  3034. case "ConditionalExpression":
  3035. this.walkConditionalExpression(expression);
  3036. break;
  3037. case "FunctionExpression":
  3038. this.walkFunctionExpression(expression);
  3039. break;
  3040. case "Identifier":
  3041. this.walkIdentifier(expression);
  3042. break;
  3043. case "ImportExpression":
  3044. this.walkImportExpression(expression);
  3045. break;
  3046. case "LogicalExpression":
  3047. this.walkLogicalExpression(expression);
  3048. break;
  3049. case "MetaProperty":
  3050. this.walkMetaProperty(expression);
  3051. break;
  3052. case "MemberExpression":
  3053. this.walkMemberExpression(expression);
  3054. break;
  3055. case "NewExpression":
  3056. this.walkNewExpression(expression);
  3057. break;
  3058. case "ObjectExpression":
  3059. this.walkObjectExpression(expression);
  3060. break;
  3061. case "SequenceExpression":
  3062. this.walkSequenceExpression(expression);
  3063. break;
  3064. case "SpreadElement":
  3065. this.walkSpreadElement(expression);
  3066. break;
  3067. case "TaggedTemplateExpression":
  3068. this.walkTaggedTemplateExpression(expression);
  3069. break;
  3070. case "TemplateLiteral":
  3071. this.walkTemplateLiteral(expression);
  3072. break;
  3073. case "ThisExpression":
  3074. this.walkThisExpression(expression);
  3075. break;
  3076. case "UnaryExpression":
  3077. this.walkUnaryExpression(expression);
  3078. break;
  3079. case "UpdateExpression":
  3080. this.walkUpdateExpression(expression);
  3081. break;
  3082. case "YieldExpression":
  3083. this.walkYieldExpression(expression);
  3084. break;
  3085. }
  3086. }
  3087. /**
  3088. * @param {AwaitExpression} expression await expression
  3089. */
  3090. walkAwaitExpression(expression) {
  3091. if (this.scope.topLevelScope === true) {
  3092. this.hooks.topLevelAwait.call(expression);
  3093. }
  3094. this.walkExpression(expression.argument);
  3095. }
  3096. /**
  3097. * @param {ArrayExpression} expression array expression
  3098. */
  3099. walkArrayExpression(expression) {
  3100. if (expression.elements) {
  3101. this.walkExpressions(expression.elements);
  3102. }
  3103. }
  3104. /**
  3105. * @param {SpreadElement} expression spread element
  3106. */
  3107. walkSpreadElement(expression) {
  3108. if (expression.argument) {
  3109. this.walkExpression(expression.argument);
  3110. }
  3111. }
  3112. /**
  3113. * @param {ObjectExpression} expression object expression
  3114. */
  3115. walkObjectExpression(expression) {
  3116. for (
  3117. let propIndex = 0, len = expression.properties.length;
  3118. propIndex < len;
  3119. propIndex++
  3120. ) {
  3121. const prop = expression.properties[propIndex];
  3122. this.walkProperty(prop);
  3123. }
  3124. }
  3125. /**
  3126. * @param {Property | SpreadElement} prop property or spread element
  3127. */
  3128. walkProperty(prop) {
  3129. if (prop.type === "SpreadElement") {
  3130. this.walkExpression(prop.argument);
  3131. return;
  3132. }
  3133. if (prop.computed) {
  3134. this.walkExpression(prop.key);
  3135. }
  3136. if (prop.shorthand && prop.value && prop.value.type === "Identifier") {
  3137. this.scope.inShorthand = prop.value.name;
  3138. this.walkIdentifier(prop.value);
  3139. this.scope.inShorthand = false;
  3140. } else {
  3141. this.walkExpression(
  3142. /** @type {Exclude<Property["value"], AssignmentPattern | ObjectPattern | ArrayPattern | RestElement>} */
  3143. (prop.value)
  3144. );
  3145. }
  3146. }
  3147. /**
  3148. * @param {FunctionExpression} expression arrow function expression
  3149. */
  3150. walkFunctionExpression(expression) {
  3151. const wasTopLevel = this.scope.topLevelScope;
  3152. this.scope.topLevelScope = false;
  3153. const scopeParams = [...expression.params];
  3154. // Add function name in scope for recursive calls
  3155. if (expression.id) {
  3156. scopeParams.push(expression.id);
  3157. }
  3158. this.inFunctionScope(true, scopeParams, () => {
  3159. for (const param of expression.params) {
  3160. this.walkPattern(param);
  3161. }
  3162. this.detectMode(expression.body.body);
  3163. const prev = this.prevStatement;
  3164. this.preWalkStatement(expression.body);
  3165. this.prevStatement = prev;
  3166. this.walkStatement(expression.body);
  3167. });
  3168. this.scope.topLevelScope = wasTopLevel;
  3169. }
  3170. /**
  3171. * @param {ArrowFunctionExpression} expression arrow function expression
  3172. */
  3173. walkArrowFunctionExpression(expression) {
  3174. const wasTopLevel = this.scope.topLevelScope;
  3175. this.scope.topLevelScope = wasTopLevel ? "arrow" : false;
  3176. this.inFunctionScope(false, expression.params, () => {
  3177. for (const param of expression.params) {
  3178. this.walkPattern(param);
  3179. }
  3180. if (expression.body.type === "BlockStatement") {
  3181. this.detectMode(expression.body.body);
  3182. const prev = this.prevStatement;
  3183. this.preWalkStatement(expression.body);
  3184. this.prevStatement = prev;
  3185. this.walkStatement(expression.body);
  3186. } else {
  3187. this.walkExpression(expression.body);
  3188. }
  3189. });
  3190. this.scope.topLevelScope = wasTopLevel;
  3191. }
  3192. /**
  3193. * @param {SequenceExpression} expression the sequence
  3194. */
  3195. walkSequenceExpression(expression) {
  3196. if (!expression.expressions) return;
  3197. // We treat sequence expressions like statements when they are one statement level
  3198. // This has some benefits for optimizations that only work on statement level
  3199. const currentStatement =
  3200. /** @type {StatementPath} */
  3201. (this.statementPath)[
  3202. /** @type {StatementPath} */
  3203. (this.statementPath).length - 1
  3204. ];
  3205. if (
  3206. currentStatement === expression ||
  3207. (currentStatement.type === "ExpressionStatement" &&
  3208. currentStatement.expression === expression)
  3209. ) {
  3210. const old =
  3211. /** @type {StatementPathItem} */
  3212. (/** @type {StatementPath} */ (this.statementPath).pop());
  3213. const prev = this.prevStatement;
  3214. for (const expr of expression.expressions) {
  3215. /** @type {StatementPath} */
  3216. (this.statementPath).push(expr);
  3217. this.walkExpression(expr);
  3218. this.prevStatement =
  3219. /** @type {StatementPath} */
  3220. (this.statementPath).pop();
  3221. }
  3222. this.prevStatement = prev;
  3223. /** @type {StatementPath} */
  3224. (this.statementPath).push(old);
  3225. } else {
  3226. this.walkExpressions(expression.expressions);
  3227. }
  3228. }
  3229. /**
  3230. * @param {UpdateExpression} expression the update expression
  3231. */
  3232. walkUpdateExpression(expression) {
  3233. this.walkExpression(expression.argument);
  3234. }
  3235. /**
  3236. * @param {UnaryExpression} expression the unary expression
  3237. */
  3238. walkUnaryExpression(expression) {
  3239. if (expression.operator === "typeof") {
  3240. const result = this.callHooksForExpression(
  3241. this.hooks.typeof,
  3242. expression.argument,
  3243. expression
  3244. );
  3245. if (result === true) return;
  3246. if (expression.argument.type === "ChainExpression") {
  3247. const result = this.callHooksForExpression(
  3248. this.hooks.typeof,
  3249. expression.argument.expression,
  3250. expression
  3251. );
  3252. if (result === true) return;
  3253. }
  3254. }
  3255. this.walkExpression(expression.argument);
  3256. }
  3257. /**
  3258. * @param {LogicalExpression | BinaryExpression} expression the expression
  3259. */
  3260. walkLeftRightExpression(expression) {
  3261. this.walkExpression(expression.left);
  3262. this.walkExpression(expression.right);
  3263. }
  3264. /**
  3265. * @param {BinaryExpression} expression the binary expression
  3266. */
  3267. walkBinaryExpression(expression) {
  3268. if (this.hooks.binaryExpression.call(expression) === undefined) {
  3269. this.walkLeftRightExpression(expression);
  3270. }
  3271. }
  3272. /**
  3273. * @param {LogicalExpression} expression the logical expression
  3274. */
  3275. walkLogicalExpression(expression) {
  3276. const result = this.hooks.expressionLogicalOperator.call(expression);
  3277. if (result === undefined) {
  3278. this.walkLeftRightExpression(expression);
  3279. } else if (result) {
  3280. this.walkExpression(expression.right);
  3281. }
  3282. }
  3283. /**
  3284. * @param {AssignmentExpression} expression assignment expression
  3285. */
  3286. walkAssignmentExpression(expression) {
  3287. if (expression.left.type === "Identifier") {
  3288. const renameIdentifier = this.getRenameIdentifier(expression.right);
  3289. if (
  3290. renameIdentifier &&
  3291. this.callHooksForInfo(
  3292. this.hooks.canRename,
  3293. renameIdentifier,
  3294. expression.right
  3295. )
  3296. ) {
  3297. // renaming "a = b;"
  3298. if (
  3299. !this.callHooksForInfo(
  3300. this.hooks.rename,
  3301. renameIdentifier,
  3302. expression.right
  3303. )
  3304. ) {
  3305. this.setVariable(
  3306. expression.left.name,
  3307. typeof renameIdentifier === "string"
  3308. ? this.getVariableInfo(renameIdentifier)
  3309. : renameIdentifier
  3310. );
  3311. }
  3312. return;
  3313. }
  3314. this.walkExpression(expression.right);
  3315. this.enterPattern(expression.left, (name, _decl) => {
  3316. if (!this.callHooksForName(this.hooks.assign, name, expression)) {
  3317. this.walkExpression(
  3318. /** @type {MemberExpression} */
  3319. (expression.left)
  3320. );
  3321. }
  3322. });
  3323. } else if (expression.left.type.endsWith("Pattern")) {
  3324. this.walkExpression(expression.right);
  3325. this.enterPattern(expression.left, (name, _decl) => {
  3326. if (!this.callHooksForName(this.hooks.assign, name, expression)) {
  3327. this.defineVariable(name);
  3328. }
  3329. });
  3330. this.walkPattern(expression.left);
  3331. } else if (expression.left.type === "MemberExpression") {
  3332. const exprName = this.getMemberExpressionInfo(
  3333. expression.left,
  3334. ALLOWED_MEMBER_TYPES_EXPRESSION
  3335. );
  3336. if (
  3337. exprName &&
  3338. this.callHooksForInfo(
  3339. this.hooks.assignMemberChain,
  3340. exprName.rootInfo,
  3341. expression,
  3342. exprName.getMembers()
  3343. )
  3344. ) {
  3345. return;
  3346. }
  3347. this.walkExpression(expression.right);
  3348. this.walkExpression(expression.left);
  3349. } else {
  3350. this.walkExpression(expression.right);
  3351. this.walkExpression(
  3352. /** @type {Exclude<AssignmentExpression["left"], Identifier | RestElement | MemberExpression | ObjectPattern | ArrayPattern | AssignmentPattern>} */
  3353. (expression.left)
  3354. );
  3355. }
  3356. }
  3357. /**
  3358. * @param {ConditionalExpression} expression conditional expression
  3359. */
  3360. walkConditionalExpression(expression) {
  3361. const result = this.hooks.expressionConditionalOperator.call(expression);
  3362. if (result === undefined) {
  3363. this.walkExpression(expression.test);
  3364. this.walkExpression(expression.consequent);
  3365. if (expression.alternate) {
  3366. this.walkExpression(expression.alternate);
  3367. }
  3368. } else if (result) {
  3369. this.walkExpression(expression.consequent);
  3370. } else if (expression.alternate) {
  3371. this.walkExpression(expression.alternate);
  3372. }
  3373. }
  3374. /**
  3375. * @param {NewExpression} expression new expression
  3376. */
  3377. walkNewExpression(expression) {
  3378. const result = this.callHooksForExpression(
  3379. this.hooks.new,
  3380. expression.callee,
  3381. expression
  3382. );
  3383. if (result === true) return;
  3384. this.walkExpression(expression.callee);
  3385. if (expression.arguments) {
  3386. this.walkExpressions(expression.arguments);
  3387. }
  3388. }
  3389. /**
  3390. * @param {YieldExpression} expression yield expression
  3391. */
  3392. walkYieldExpression(expression) {
  3393. if (expression.argument) {
  3394. this.walkExpression(expression.argument);
  3395. }
  3396. }
  3397. /**
  3398. * @param {TemplateLiteral} expression template literal
  3399. */
  3400. walkTemplateLiteral(expression) {
  3401. if (expression.expressions) {
  3402. this.walkExpressions(expression.expressions);
  3403. }
  3404. }
  3405. /**
  3406. * @param {TaggedTemplateExpression} expression tagged template expression
  3407. */
  3408. walkTaggedTemplateExpression(expression) {
  3409. if (expression.tag) {
  3410. this.scope.inTaggedTemplateTag = true;
  3411. this.walkExpression(expression.tag);
  3412. this.scope.inTaggedTemplateTag = false;
  3413. }
  3414. if (expression.quasi && expression.quasi.expressions) {
  3415. this.walkExpressions(expression.quasi.expressions);
  3416. }
  3417. }
  3418. /**
  3419. * @param {ClassExpression} expression the class expression
  3420. */
  3421. walkClassExpression(expression) {
  3422. this.walkClass(expression);
  3423. }
  3424. /**
  3425. * @param {ChainExpression} expression expression
  3426. */
  3427. walkChainExpression(expression) {
  3428. const result = this.hooks.optionalChaining.call(expression);
  3429. if (result === undefined) {
  3430. if (expression.expression.type === "CallExpression") {
  3431. this.walkCallExpression(expression.expression);
  3432. } else {
  3433. this.walkMemberExpression(expression.expression);
  3434. }
  3435. }
  3436. }
  3437. /**
  3438. * @private
  3439. * @param {FunctionExpression | ArrowFunctionExpression} functionExpression function expression
  3440. * @param {(Expression | SpreadElement)[]} options options
  3441. * @param {Expression | SpreadElement | null} currentThis current this
  3442. */
  3443. _walkIIFE(functionExpression, options, currentThis) {
  3444. /**
  3445. * @param {Expression | SpreadElement} argOrThis arg or this
  3446. * @returns {string | VariableInfo | undefined} var info
  3447. */
  3448. const getVarInfo = (argOrThis) => {
  3449. const renameIdentifier = this.getRenameIdentifier(argOrThis);
  3450. if (
  3451. renameIdentifier &&
  3452. this.callHooksForInfo(
  3453. this.hooks.canRename,
  3454. renameIdentifier,
  3455. /** @type {Expression} */
  3456. (argOrThis)
  3457. ) &&
  3458. !this.callHooksForInfo(
  3459. this.hooks.rename,
  3460. renameIdentifier,
  3461. /** @type {Expression} */
  3462. (argOrThis)
  3463. )
  3464. ) {
  3465. return typeof renameIdentifier === "string"
  3466. ? /** @type {string} */ (this.getVariableInfo(renameIdentifier))
  3467. : renameIdentifier;
  3468. }
  3469. this.walkExpression(argOrThis);
  3470. };
  3471. const { params, type } = functionExpression;
  3472. const arrow = type === "ArrowFunctionExpression";
  3473. const renameThis = currentThis ? getVarInfo(currentThis) : null;
  3474. const varInfoForArgs = options.map(getVarInfo);
  3475. const wasTopLevel = this.scope.topLevelScope;
  3476. this.scope.topLevelScope = wasTopLevel && arrow ? "arrow" : false;
  3477. const scopeParams =
  3478. /** @type {(Identifier | string)[]} */
  3479. (params.filter((identifier, idx) => !varInfoForArgs[idx]));
  3480. // Add function name in scope for recursive calls
  3481. if (
  3482. functionExpression.type === "FunctionExpression" &&
  3483. functionExpression.id
  3484. ) {
  3485. scopeParams.push(functionExpression.id.name);
  3486. }
  3487. this.inFunctionScope(true, scopeParams, () => {
  3488. if (renameThis && !arrow) {
  3489. this.setVariable("this", renameThis);
  3490. }
  3491. for (let i = 0; i < varInfoForArgs.length; i++) {
  3492. const varInfo = varInfoForArgs[i];
  3493. if (!varInfo) continue;
  3494. if (!params[i] || params[i].type !== "Identifier") continue;
  3495. this.setVariable(/** @type {Identifier} */ (params[i]).name, varInfo);
  3496. }
  3497. if (functionExpression.body.type === "BlockStatement") {
  3498. this.detectMode(functionExpression.body.body);
  3499. const prev = this.prevStatement;
  3500. this.preWalkStatement(functionExpression.body);
  3501. this.prevStatement = prev;
  3502. this.walkStatement(functionExpression.body);
  3503. } else {
  3504. this.walkExpression(functionExpression.body);
  3505. }
  3506. });
  3507. this.scope.topLevelScope = wasTopLevel;
  3508. }
  3509. /**
  3510. * @param {ImportExpression} expression import expression
  3511. */
  3512. walkImportExpression(expression) {
  3513. const result = this.hooks.importCall.call(expression);
  3514. if (result === true) return;
  3515. this.walkExpression(expression.source);
  3516. }
  3517. /**
  3518. * @param {CallExpression} expression expression
  3519. */
  3520. walkCallExpression(expression) {
  3521. /**
  3522. * @param {FunctionExpression | ArrowFunctionExpression} fn function
  3523. * @returns {boolean} true when simple function
  3524. */
  3525. const isSimpleFunction = (fn) =>
  3526. fn.params.every((p) => p.type === "Identifier");
  3527. if (
  3528. expression.callee.type === "MemberExpression" &&
  3529. expression.callee.object.type.endsWith("FunctionExpression") &&
  3530. !expression.callee.computed &&
  3531. /** @type {boolean} */
  3532. (
  3533. /** @type {Identifier} */
  3534. (expression.callee.property).name === "call" ||
  3535. /** @type {Identifier} */
  3536. (expression.callee.property).name === "bind"
  3537. ) &&
  3538. expression.arguments.length > 0 &&
  3539. isSimpleFunction(
  3540. /** @type {FunctionExpression | ArrowFunctionExpression} */
  3541. (expression.callee.object)
  3542. )
  3543. ) {
  3544. // (function(…) { }.call/bind(?, …))
  3545. this._walkIIFE(
  3546. /** @type {FunctionExpression | ArrowFunctionExpression} */
  3547. (expression.callee.object),
  3548. expression.arguments.slice(1),
  3549. expression.arguments[0]
  3550. );
  3551. } else if (
  3552. expression.callee.type.endsWith("FunctionExpression") &&
  3553. isSimpleFunction(
  3554. /** @type {FunctionExpression | ArrowFunctionExpression} */
  3555. (expression.callee)
  3556. )
  3557. ) {
  3558. // (function(…) { }(…))
  3559. this._walkIIFE(
  3560. /** @type {FunctionExpression | ArrowFunctionExpression} */
  3561. (expression.callee),
  3562. expression.arguments,
  3563. null
  3564. );
  3565. } else {
  3566. if (expression.callee.type === "MemberExpression") {
  3567. const exprInfo = this.getMemberExpressionInfo(
  3568. expression.callee,
  3569. ALLOWED_MEMBER_TYPES_CALL_EXPRESSION
  3570. );
  3571. if (exprInfo && exprInfo.type === "call") {
  3572. const result = this.callHooksForInfo(
  3573. this.hooks.callMemberChainOfCallMemberChain,
  3574. exprInfo.rootInfo,
  3575. expression,
  3576. exprInfo.getCalleeMembers(),
  3577. exprInfo.call,
  3578. exprInfo.getMembers(),
  3579. exprInfo.getMemberRanges()
  3580. );
  3581. if (result === true) return;
  3582. }
  3583. }
  3584. const callee = this.evaluateExpression(expression.callee);
  3585. if (callee.isIdentifier()) {
  3586. const result1 = this.callHooksForInfo(
  3587. this.hooks.callMemberChain,
  3588. /** @type {NonNullable<BasicEvaluatedExpression["rootInfo"]>} */
  3589. (callee.rootInfo),
  3590. expression,
  3591. /** @type {NonNullable<BasicEvaluatedExpression["getMembers"]>} */
  3592. (callee.getMembers)(),
  3593. callee.getMembersOptionals
  3594. ? callee.getMembersOptionals()
  3595. : /** @type {NonNullable<BasicEvaluatedExpression["getMembers"]>} */
  3596. (callee.getMembers)().map(() => false),
  3597. callee.getMemberRanges ? callee.getMemberRanges() : []
  3598. );
  3599. if (result1 === true) return;
  3600. const result2 = this.callHooksForInfo(
  3601. this.hooks.call,
  3602. /** @type {NonNullable<BasicEvaluatedExpression["identifier"]>} */
  3603. (callee.identifier),
  3604. expression
  3605. );
  3606. if (result2 === true) return;
  3607. }
  3608. if (expression.callee) {
  3609. if (expression.callee.type === "MemberExpression") {
  3610. // because of call context we need to walk the call context as expression
  3611. this.walkExpression(expression.callee.object);
  3612. if (expression.callee.computed === true) {
  3613. this.walkExpression(expression.callee.property);
  3614. }
  3615. } else {
  3616. this.walkExpression(expression.callee);
  3617. }
  3618. }
  3619. if (expression.arguments) this.walkExpressions(expression.arguments);
  3620. }
  3621. }
  3622. /**
  3623. * @param {MemberExpression} expression member expression
  3624. */
  3625. walkMemberExpression(expression) {
  3626. const exprInfo = this.getMemberExpressionInfo(
  3627. expression,
  3628. ALLOWED_MEMBER_TYPES_ALL
  3629. );
  3630. if (exprInfo) {
  3631. switch (exprInfo.type) {
  3632. case "expression": {
  3633. const result1 = this.callHooksForInfo(
  3634. this.hooks.expression,
  3635. exprInfo.name,
  3636. expression
  3637. );
  3638. if (result1 === true) return;
  3639. const members = exprInfo.getMembers();
  3640. const membersOptionals = exprInfo.getMembersOptionals();
  3641. const memberRanges = exprInfo.getMemberRanges();
  3642. const result2 = this.callHooksForInfo(
  3643. this.hooks.expressionMemberChain,
  3644. exprInfo.rootInfo,
  3645. expression,
  3646. members,
  3647. membersOptionals,
  3648. memberRanges
  3649. );
  3650. if (result2 === true) return;
  3651. this.walkMemberExpressionWithExpressionName(
  3652. expression,
  3653. exprInfo.name,
  3654. exprInfo.rootInfo,
  3655. [...members],
  3656. () =>
  3657. this.callHooksForInfo(
  3658. this.hooks.unhandledExpressionMemberChain,
  3659. exprInfo.rootInfo,
  3660. expression,
  3661. members
  3662. )
  3663. );
  3664. return;
  3665. }
  3666. case "call": {
  3667. const result = this.callHooksForInfo(
  3668. this.hooks.memberChainOfCallMemberChain,
  3669. exprInfo.rootInfo,
  3670. expression,
  3671. exprInfo.getCalleeMembers(),
  3672. exprInfo.call,
  3673. exprInfo.getMembers(),
  3674. exprInfo.getMemberRanges()
  3675. );
  3676. if (result === true) return;
  3677. // Fast skip over the member chain as we already called memberChainOfCallMemberChain
  3678. // and call computed property are literals anyway
  3679. this.walkExpression(exprInfo.call);
  3680. return;
  3681. }
  3682. }
  3683. }
  3684. this.walkExpression(expression.object);
  3685. if (expression.computed === true) this.walkExpression(expression.property);
  3686. }
  3687. /**
  3688. * @template R
  3689. * @param {MemberExpression} expression member expression
  3690. * @param {string} name name
  3691. * @param {string | VariableInfo} rootInfo root info
  3692. * @param {string[]} members members
  3693. * @param {() => R | undefined} onUnhandled on unhandled callback
  3694. */
  3695. walkMemberExpressionWithExpressionName(
  3696. expression,
  3697. name,
  3698. rootInfo,
  3699. members,
  3700. onUnhandled
  3701. ) {
  3702. if (expression.object.type === "MemberExpression") {
  3703. // optimize the case where expression.object is a MemberExpression too.
  3704. // we can keep info here when calling walkMemberExpression directly
  3705. const property =
  3706. /** @type {Identifier} */
  3707. (expression.property).name ||
  3708. `${/** @type {Literal} */ (expression.property).value}`;
  3709. name = name.slice(0, -property.length - 1);
  3710. members.pop();
  3711. const result = this.callHooksForInfo(
  3712. this.hooks.expression,
  3713. name,
  3714. expression.object
  3715. );
  3716. if (result === true) return;
  3717. this.walkMemberExpressionWithExpressionName(
  3718. expression.object,
  3719. name,
  3720. rootInfo,
  3721. members,
  3722. onUnhandled
  3723. );
  3724. } else if (!onUnhandled || !onUnhandled()) {
  3725. this.walkExpression(expression.object);
  3726. }
  3727. if (expression.computed === true) this.walkExpression(expression.property);
  3728. }
  3729. /**
  3730. * @param {ThisExpression} expression this expression
  3731. */
  3732. walkThisExpression(expression) {
  3733. this.callHooksForName(this.hooks.expression, "this", expression);
  3734. }
  3735. /**
  3736. * @param {Identifier} expression identifier
  3737. */
  3738. walkIdentifier(expression) {
  3739. this.callHooksForName(this.hooks.expression, expression.name, expression);
  3740. }
  3741. /**
  3742. * @param {MetaProperty} metaProperty meta property
  3743. */
  3744. walkMetaProperty(metaProperty) {
  3745. this.hooks.expression.for(getRootName(metaProperty)).call(metaProperty);
  3746. }
  3747. /**
  3748. * @template T
  3749. * @template R
  3750. * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
  3751. * @param {Expression | Super} expr expression
  3752. * @param {AsArray<T>} args args for the hook
  3753. * @returns {R | undefined} result of hook
  3754. */
  3755. callHooksForExpression(hookMap, expr, ...args) {
  3756. return this.callHooksForExpressionWithFallback(
  3757. hookMap,
  3758. expr,
  3759. undefined,
  3760. undefined,
  3761. ...args
  3762. );
  3763. }
  3764. /**
  3765. * @template T
  3766. * @template R
  3767. * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
  3768. * @param {Expression | Super} expr expression info
  3769. * @param {((name: string, rootInfo: string | ScopeInfo | VariableInfo, getMembers: () => string[]) => R) | undefined} fallback callback when variable in not handled by hooks
  3770. * @param {((result?: string) => R | undefined) | undefined} defined callback when variable is defined
  3771. * @param {AsArray<T>} args args for the hook
  3772. * @returns {R | undefined} result of hook
  3773. */
  3774. callHooksForExpressionWithFallback(
  3775. hookMap,
  3776. expr,
  3777. fallback,
  3778. defined,
  3779. ...args
  3780. ) {
  3781. const exprName = this.getMemberExpressionInfo(
  3782. expr,
  3783. ALLOWED_MEMBER_TYPES_EXPRESSION
  3784. );
  3785. if (exprName !== undefined) {
  3786. const members = exprName.getMembers();
  3787. return this.callHooksForInfoWithFallback(
  3788. hookMap,
  3789. members.length === 0 ? exprName.rootInfo : exprName.name,
  3790. fallback &&
  3791. ((name) => fallback(name, exprName.rootInfo, exprName.getMembers)),
  3792. defined && (() => defined(exprName.name)),
  3793. ...args
  3794. );
  3795. }
  3796. }
  3797. /**
  3798. * @template T
  3799. * @template R
  3800. * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
  3801. * @param {string} name key in map
  3802. * @param {AsArray<T>} args args for the hook
  3803. * @returns {R | undefined} result of hook
  3804. */
  3805. callHooksForName(hookMap, name, ...args) {
  3806. return this.callHooksForNameWithFallback(
  3807. hookMap,
  3808. name,
  3809. undefined,
  3810. undefined,
  3811. ...args
  3812. );
  3813. }
  3814. /**
  3815. * @template T
  3816. * @template R
  3817. * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks that should be called
  3818. * @param {ExportedVariableInfo} info variable info
  3819. * @param {AsArray<T>} args args for the hook
  3820. * @returns {R | undefined} result of hook
  3821. */
  3822. callHooksForInfo(hookMap, info, ...args) {
  3823. return this.callHooksForInfoWithFallback(
  3824. hookMap,
  3825. info,
  3826. undefined,
  3827. undefined,
  3828. ...args
  3829. );
  3830. }
  3831. /**
  3832. * @template T
  3833. * @template R
  3834. * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
  3835. * @param {ExportedVariableInfo} info variable info
  3836. * @param {((name: string) => R | undefined) | undefined} fallback callback when variable in not handled by hooks
  3837. * @param {((result?: string) => TODO) | undefined} defined callback when variable is defined
  3838. * @param {AsArray<T>} args args for the hook
  3839. * @returns {R | undefined} result of hook
  3840. */
  3841. callHooksForInfoWithFallback(hookMap, info, fallback, defined, ...args) {
  3842. let name;
  3843. if (typeof info === "string") {
  3844. name = info;
  3845. } else {
  3846. if (!(info instanceof VariableInfo)) {
  3847. if (defined !== undefined) {
  3848. return defined();
  3849. }
  3850. return;
  3851. }
  3852. let tagInfo = info.tagInfo;
  3853. while (tagInfo !== undefined) {
  3854. const hook = hookMap.get(tagInfo.tag);
  3855. if (hook !== undefined) {
  3856. this.currentTagData = tagInfo.data;
  3857. const result = hook.call(...args);
  3858. this.currentTagData = undefined;
  3859. if (result !== undefined) return result;
  3860. }
  3861. tagInfo = tagInfo.next;
  3862. }
  3863. if (!info.isFree() && !info.isTagged()) {
  3864. if (defined !== undefined) {
  3865. return defined();
  3866. }
  3867. return;
  3868. }
  3869. name = info.name;
  3870. }
  3871. const hook = hookMap.get(name);
  3872. if (hook !== undefined) {
  3873. const result = hook.call(...args);
  3874. if (result !== undefined) return result;
  3875. }
  3876. if (fallback !== undefined) {
  3877. return fallback(/** @type {string} */ (name));
  3878. }
  3879. }
  3880. /**
  3881. * @template T
  3882. * @template R
  3883. * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
  3884. * @param {string} name key in map
  3885. * @param {((value: string) => R | undefined) | undefined} fallback callback when variable in not handled by hooks
  3886. * @param {(() => R) | undefined} defined callback when variable is defined
  3887. * @param {AsArray<T>} args args for the hook
  3888. * @returns {R | undefined} result of hook
  3889. */
  3890. callHooksForNameWithFallback(hookMap, name, fallback, defined, ...args) {
  3891. return this.callHooksForInfoWithFallback(
  3892. hookMap,
  3893. this.getVariableInfo(name),
  3894. fallback,
  3895. defined,
  3896. ...args
  3897. );
  3898. }
  3899. /**
  3900. * @deprecated
  3901. * @param {(string | Pattern | Property)[]} params scope params
  3902. * @param {() => void} fn inner function
  3903. * @returns {void}
  3904. */
  3905. inScope(params, fn) {
  3906. const oldScope = this.scope;
  3907. this.scope = {
  3908. topLevelScope: oldScope.topLevelScope,
  3909. inTry: false,
  3910. inShorthand: false,
  3911. inTaggedTemplateTag: false,
  3912. isStrict: oldScope.isStrict,
  3913. isAsmJs: oldScope.isAsmJs,
  3914. terminated: undefined,
  3915. definitions: oldScope.definitions.createChild()
  3916. };
  3917. this.undefineVariable("this");
  3918. this.enterPatterns(params, (ident) => {
  3919. this.defineVariable(ident);
  3920. });
  3921. fn();
  3922. this.scope = oldScope;
  3923. }
  3924. /**
  3925. * @param {boolean} hasThis true, when this is defined
  3926. * @param {Identifier[]} params scope params
  3927. * @param {() => void} fn inner function
  3928. * @returns {void}
  3929. */
  3930. inClassScope(hasThis, params, fn) {
  3931. const oldScope = this.scope;
  3932. this.scope = {
  3933. topLevelScope: oldScope.topLevelScope,
  3934. inTry: false,
  3935. inShorthand: false,
  3936. inTaggedTemplateTag: false,
  3937. isStrict: oldScope.isStrict,
  3938. isAsmJs: oldScope.isAsmJs,
  3939. terminated: undefined,
  3940. definitions: oldScope.definitions.createChild()
  3941. };
  3942. if (hasThis) {
  3943. this.undefineVariable("this");
  3944. }
  3945. this.enterPatterns(params, (ident) => {
  3946. this.defineVariable(ident);
  3947. });
  3948. fn();
  3949. this.scope = oldScope;
  3950. }
  3951. /**
  3952. * @param {boolean} hasThis true, when this is defined
  3953. * @param {(Pattern | string)[]} params scope params
  3954. * @param {() => void} fn inner function
  3955. * @returns {void}
  3956. */
  3957. inFunctionScope(hasThis, params, fn) {
  3958. const oldScope = this.scope;
  3959. this.scope = {
  3960. topLevelScope: oldScope.topLevelScope,
  3961. inTry: false,
  3962. inShorthand: false,
  3963. inTaggedTemplateTag: false,
  3964. isStrict: oldScope.isStrict,
  3965. isAsmJs: oldScope.isAsmJs,
  3966. terminated: undefined,
  3967. definitions: oldScope.definitions.createChild()
  3968. };
  3969. if (hasThis) {
  3970. this.undefineVariable("this");
  3971. }
  3972. this.enterPatterns(params, (ident) => {
  3973. this.defineVariable(ident);
  3974. });
  3975. fn();
  3976. this.scope = oldScope;
  3977. }
  3978. /**
  3979. * @param {() => void} fn inner function
  3980. * @param {boolean} inExecutedPath executed state
  3981. * @returns {void}
  3982. */
  3983. inBlockScope(fn, inExecutedPath = false) {
  3984. const oldScope = this.scope;
  3985. this.scope = {
  3986. topLevelScope: oldScope.topLevelScope,
  3987. inTry: oldScope.inTry,
  3988. inShorthand: false,
  3989. inTaggedTemplateTag: false,
  3990. isStrict: oldScope.isStrict,
  3991. isAsmJs: oldScope.isAsmJs,
  3992. terminated: oldScope.terminated,
  3993. definitions: oldScope.definitions.createChild()
  3994. };
  3995. fn();
  3996. const terminated = this.scope.terminated;
  3997. if (inExecutedPath && terminated) {
  3998. oldScope.terminated = terminated;
  3999. }
  4000. this.scope = oldScope;
  4001. }
  4002. /**
  4003. * @param {Array<Directive | Statement | ModuleDeclaration>} statements statements
  4004. */
  4005. detectMode(statements) {
  4006. const isLiteral =
  4007. statements.length >= 1 &&
  4008. statements[0].type === "ExpressionStatement" &&
  4009. statements[0].expression.type === "Literal";
  4010. if (
  4011. isLiteral &&
  4012. /** @type {Literal} */
  4013. (/** @type {ExpressionStatement} */ (statements[0]).expression).value ===
  4014. "use strict"
  4015. ) {
  4016. this.scope.isStrict = true;
  4017. }
  4018. if (
  4019. isLiteral &&
  4020. /** @type {Literal} */
  4021. (/** @type {ExpressionStatement} */ (statements[0]).expression).value ===
  4022. "use asm"
  4023. ) {
  4024. this.scope.isAsmJs = true;
  4025. }
  4026. }
  4027. /**
  4028. * @param {(string | Pattern | Property)[]} patterns patterns
  4029. * @param {OnIdentString} onIdent on ident callback
  4030. */
  4031. enterPatterns(patterns, onIdent) {
  4032. for (const pattern of patterns) {
  4033. if (typeof pattern !== "string") {
  4034. this.enterPattern(pattern, onIdent);
  4035. } else if (pattern) {
  4036. onIdent(pattern);
  4037. }
  4038. }
  4039. }
  4040. /**
  4041. * @param {Pattern | Property} pattern pattern
  4042. * @param {OnIdent} onIdent on ident callback
  4043. */
  4044. enterPattern(pattern, onIdent) {
  4045. if (!pattern) return;
  4046. switch (pattern.type) {
  4047. case "ArrayPattern":
  4048. this.enterArrayPattern(pattern, onIdent);
  4049. break;
  4050. case "AssignmentPattern":
  4051. this.enterAssignmentPattern(pattern, onIdent);
  4052. break;
  4053. case "Identifier":
  4054. this.enterIdentifier(pattern, onIdent);
  4055. break;
  4056. case "ObjectPattern":
  4057. this.enterObjectPattern(pattern, onIdent);
  4058. break;
  4059. case "RestElement":
  4060. this.enterRestElement(pattern, onIdent);
  4061. break;
  4062. case "Property":
  4063. if (pattern.shorthand && pattern.value.type === "Identifier") {
  4064. this.scope.inShorthand = pattern.value.name;
  4065. this.enterIdentifier(pattern.value, onIdent);
  4066. this.scope.inShorthand = false;
  4067. } else {
  4068. this.enterPattern(/** @type {Pattern} */ (pattern.value), onIdent);
  4069. }
  4070. break;
  4071. }
  4072. }
  4073. /**
  4074. * @param {Identifier} pattern identifier pattern
  4075. * @param {OnIdent} onIdent callback
  4076. */
  4077. enterIdentifier(pattern, onIdent) {
  4078. if (!this.callHooksForName(this.hooks.pattern, pattern.name, pattern)) {
  4079. onIdent(pattern.name, pattern);
  4080. }
  4081. }
  4082. /**
  4083. * @param {ObjectPattern} pattern object pattern
  4084. * @param {OnIdent} onIdent callback
  4085. */
  4086. enterObjectPattern(pattern, onIdent) {
  4087. for (
  4088. let propIndex = 0, len = pattern.properties.length;
  4089. propIndex < len;
  4090. propIndex++
  4091. ) {
  4092. const prop = pattern.properties[propIndex];
  4093. this.enterPattern(prop, onIdent);
  4094. }
  4095. }
  4096. /**
  4097. * @param {ArrayPattern} pattern object pattern
  4098. * @param {OnIdent} onIdent callback
  4099. */
  4100. enterArrayPattern(pattern, onIdent) {
  4101. for (
  4102. let elementIndex = 0, len = pattern.elements.length;
  4103. elementIndex < len;
  4104. elementIndex++
  4105. ) {
  4106. const element = pattern.elements[elementIndex];
  4107. if (element) {
  4108. this.enterPattern(element, onIdent);
  4109. }
  4110. }
  4111. }
  4112. /**
  4113. * @param {RestElement} pattern object pattern
  4114. * @param {OnIdent} onIdent callback
  4115. */
  4116. enterRestElement(pattern, onIdent) {
  4117. this.enterPattern(pattern.argument, onIdent);
  4118. }
  4119. /**
  4120. * @param {AssignmentPattern} pattern object pattern
  4121. * @param {OnIdent} onIdent callback
  4122. */
  4123. enterAssignmentPattern(pattern, onIdent) {
  4124. this.enterPattern(pattern.left, onIdent);
  4125. }
  4126. /**
  4127. * @param {Expression | SpreadElement | PrivateIdentifier | Super} expression expression node
  4128. * @returns {BasicEvaluatedExpression} evaluation result
  4129. */
  4130. evaluateExpression(expression) {
  4131. try {
  4132. const hook = this.hooks.evaluate.get(expression.type);
  4133. if (hook !== undefined) {
  4134. const result = hook.call(expression);
  4135. if (result !== undefined && result !== null) {
  4136. result.setExpression(expression);
  4137. return result;
  4138. }
  4139. }
  4140. } catch (err) {
  4141. // eslint-disable-next-line no-console
  4142. console.warn(err);
  4143. // ignore error
  4144. }
  4145. return new BasicEvaluatedExpression()
  4146. .setRange(/** @type {Range} */ (expression.range))
  4147. .setExpression(expression);
  4148. }
  4149. /**
  4150. * @param {Expression} expression expression
  4151. * @returns {string} parsed string
  4152. */
  4153. parseString(expression) {
  4154. switch (expression.type) {
  4155. case "BinaryExpression":
  4156. if (expression.operator === "+") {
  4157. return (
  4158. this.parseString(/** @type {Expression} */ (expression.left)) +
  4159. this.parseString(expression.right)
  4160. );
  4161. }
  4162. break;
  4163. case "Literal":
  4164. return String(expression.value);
  4165. }
  4166. throw new Error(
  4167. `${expression.type} is not supported as parameter for require`
  4168. );
  4169. }
  4170. /** @typedef {{ range?: Range, value: string, code: boolean, conditional: false | CalculatedStringResult[] }} CalculatedStringResult */
  4171. /**
  4172. * @param {Expression} expression expression
  4173. * @returns {CalculatedStringResult} result
  4174. */
  4175. parseCalculatedString(expression) {
  4176. switch (expression.type) {
  4177. case "BinaryExpression":
  4178. if (expression.operator === "+") {
  4179. const left = this.parseCalculatedString(
  4180. /** @type {Expression} */
  4181. (expression.left)
  4182. );
  4183. const right = this.parseCalculatedString(expression.right);
  4184. if (left.code) {
  4185. return {
  4186. range: left.range,
  4187. value: left.value,
  4188. code: true,
  4189. conditional: false
  4190. };
  4191. } else if (right.code) {
  4192. return {
  4193. range: [
  4194. /** @type {Range} */
  4195. (left.range)[0],
  4196. right.range
  4197. ? right.range[1]
  4198. : /** @type {Range} */ (left.range)[1]
  4199. ],
  4200. value: left.value + right.value,
  4201. code: true,
  4202. conditional: false
  4203. };
  4204. }
  4205. return {
  4206. range: [
  4207. /** @type {Range} */
  4208. (left.range)[0],
  4209. /** @type {Range} */
  4210. (right.range)[1]
  4211. ],
  4212. value: left.value + right.value,
  4213. code: false,
  4214. conditional: false
  4215. };
  4216. }
  4217. break;
  4218. case "ConditionalExpression": {
  4219. const consequent = this.parseCalculatedString(expression.consequent);
  4220. const alternate = this.parseCalculatedString(expression.alternate);
  4221. /** @type {CalculatedStringResult[]} */
  4222. const items = [];
  4223. if (consequent.conditional) {
  4224. items.push(...consequent.conditional);
  4225. } else if (!consequent.code) {
  4226. items.push(consequent);
  4227. } else {
  4228. break;
  4229. }
  4230. if (alternate.conditional) {
  4231. items.push(...alternate.conditional);
  4232. } else if (!alternate.code) {
  4233. items.push(alternate);
  4234. } else {
  4235. break;
  4236. }
  4237. return {
  4238. range: undefined,
  4239. value: "",
  4240. code: true,
  4241. conditional: items
  4242. };
  4243. }
  4244. case "Literal":
  4245. return {
  4246. range: expression.range,
  4247. value: String(expression.value),
  4248. code: false,
  4249. conditional: false
  4250. };
  4251. }
  4252. return {
  4253. range: undefined,
  4254. value: "",
  4255. code: true,
  4256. conditional: false
  4257. };
  4258. }
  4259. /**
  4260. * @param {string | Buffer | PreparsedAst} source the source to parse
  4261. * @param {ParserState} state the parser state
  4262. * @returns {ParserState} the parser state
  4263. */
  4264. parse(source, state) {
  4265. let ast;
  4266. /** @type {import("acorn").Comment[]} */
  4267. let comments;
  4268. const semicolons = new Set();
  4269. if (source === null) {
  4270. throw new Error("source must not be null");
  4271. }
  4272. if (Buffer.isBuffer(source)) {
  4273. source = source.toString("utf8");
  4274. }
  4275. if (typeof source === "object") {
  4276. ast = /** @type {Program} */ (source);
  4277. comments = source.comments;
  4278. if (source.semicolons) {
  4279. // Forward semicolon information from the preparsed AST if present
  4280. // This ensures the output is consistent with that of a fresh AST
  4281. for (const pos of source.semicolons) {
  4282. semicolons.add(pos);
  4283. }
  4284. }
  4285. } else {
  4286. comments = [];
  4287. ast = JavascriptParser._parse(source, {
  4288. sourceType: this.sourceType,
  4289. onComment: comments,
  4290. onInsertedSemicolon: (pos) => semicolons.add(pos)
  4291. });
  4292. }
  4293. const oldScope = this.scope;
  4294. const oldState = this.state;
  4295. const oldComments = this.comments;
  4296. const oldSemicolons = this.semicolons;
  4297. const oldStatementPath = this.statementPath;
  4298. const oldPrevStatement = this.prevStatement;
  4299. this.scope = {
  4300. topLevelScope: true,
  4301. inTry: false,
  4302. inShorthand: false,
  4303. inTaggedTemplateTag: false,
  4304. isStrict: false,
  4305. isAsmJs: false,
  4306. terminated: undefined,
  4307. definitions: new StackedMap()
  4308. };
  4309. this.state = /** @type {ParserState} */ (state);
  4310. this.comments = comments;
  4311. this.semicolons = semicolons;
  4312. this.statementPath = [];
  4313. this.prevStatement = undefined;
  4314. if (this.hooks.program.call(ast, comments) === undefined) {
  4315. this.destructuringAssignmentProperties = new WeakMap();
  4316. this.detectMode(ast.body);
  4317. this.modulePreWalkStatements(ast.body);
  4318. this.preWalkStatements(ast.body);
  4319. this.prevStatement = undefined;
  4320. this.blockPreWalkStatements(ast.body);
  4321. this.prevStatement = undefined;
  4322. this.walkStatements(ast.body);
  4323. this.destructuringAssignmentProperties = undefined;
  4324. }
  4325. this.hooks.finish.call(ast, comments);
  4326. this.scope = oldScope;
  4327. this.state = oldState;
  4328. this.comments = oldComments;
  4329. this.semicolons = oldSemicolons;
  4330. this.statementPath = oldStatementPath;
  4331. this.prevStatement = oldPrevStatement;
  4332. return state;
  4333. }
  4334. /**
  4335. * @param {string} source source code
  4336. * @returns {BasicEvaluatedExpression} evaluation result
  4337. */
  4338. evaluate(source) {
  4339. const ast = JavascriptParser._parse(`(${source})`, {
  4340. sourceType: this.sourceType,
  4341. locations: false
  4342. });
  4343. if (ast.body.length !== 1 || ast.body[0].type !== "ExpressionStatement") {
  4344. throw new Error("evaluate: Source is not a expression");
  4345. }
  4346. return this.evaluateExpression(ast.body[0].expression);
  4347. }
  4348. /**
  4349. * @param {Expression | Declaration | PrivateIdentifier | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | null | undefined} expr an expression
  4350. * @param {number} commentsStartPos source position from which annotation comments are checked
  4351. * @returns {boolean} true, when the expression is pure
  4352. */
  4353. isPure(expr, commentsStartPos) {
  4354. if (!expr) return true;
  4355. const result = this.hooks.isPure
  4356. .for(expr.type)
  4357. .call(expr, commentsStartPos);
  4358. if (typeof result === "boolean") return result;
  4359. switch (expr.type) {
  4360. // TODO handle more cases
  4361. case "ClassDeclaration":
  4362. case "ClassExpression": {
  4363. if (expr.body.type !== "ClassBody") return false;
  4364. if (
  4365. expr.superClass &&
  4366. !this.isPure(expr.superClass, /** @type {Range} */ (expr.range)[0])
  4367. ) {
  4368. return false;
  4369. }
  4370. const items = expr.body.body;
  4371. return items.every((item) => {
  4372. if (item.type === "StaticBlock") {
  4373. return false;
  4374. }
  4375. if (
  4376. item.computed &&
  4377. item.key &&
  4378. !this.isPure(
  4379. item.key,
  4380. /** @type {Range} */
  4381. (item.range)[0]
  4382. )
  4383. ) {
  4384. return false;
  4385. }
  4386. if (
  4387. item.static &&
  4388. item.value &&
  4389. !this.isPure(
  4390. item.value,
  4391. item.key
  4392. ? /** @type {Range} */ (item.key.range)[1]
  4393. : /** @type {Range} */ (item.range)[0]
  4394. )
  4395. ) {
  4396. return false;
  4397. }
  4398. if (
  4399. expr.superClass &&
  4400. item.type === "MethodDefinition" &&
  4401. item.kind === "constructor"
  4402. ) {
  4403. return false;
  4404. }
  4405. return true;
  4406. });
  4407. }
  4408. case "FunctionDeclaration":
  4409. case "FunctionExpression":
  4410. case "ArrowFunctionExpression":
  4411. case "ThisExpression":
  4412. case "Literal":
  4413. case "TemplateLiteral":
  4414. case "Identifier":
  4415. case "PrivateIdentifier":
  4416. return true;
  4417. case "VariableDeclaration":
  4418. return expr.declarations.every((decl) =>
  4419. this.isPure(decl.init, /** @type {Range} */ (decl.range)[0])
  4420. );
  4421. case "ConditionalExpression":
  4422. return (
  4423. this.isPure(expr.test, commentsStartPos) &&
  4424. this.isPure(
  4425. expr.consequent,
  4426. /** @type {Range} */ (expr.test.range)[1]
  4427. ) &&
  4428. this.isPure(
  4429. expr.alternate,
  4430. /** @type {Range} */ (expr.consequent.range)[1]
  4431. )
  4432. );
  4433. case "LogicalExpression":
  4434. return (
  4435. this.isPure(expr.left, commentsStartPos) &&
  4436. this.isPure(expr.right, /** @type {Range} */ (expr.left.range)[1])
  4437. );
  4438. case "SequenceExpression":
  4439. return expr.expressions.every((expr) => {
  4440. const pureFlag = this.isPure(expr, commentsStartPos);
  4441. commentsStartPos = /** @type {Range} */ (expr.range)[1];
  4442. return pureFlag;
  4443. });
  4444. case "CallExpression": {
  4445. const pureFlag =
  4446. /** @type {Range} */ (expr.range)[0] - commentsStartPos > 12 &&
  4447. this.getComments([
  4448. commentsStartPos,
  4449. /** @type {Range} */ (expr.range)[0]
  4450. ]).some(
  4451. (comment) =>
  4452. comment.type === "Block" &&
  4453. /^\s*(#|@)__PURE__\s*$/.test(comment.value)
  4454. );
  4455. if (!pureFlag) return false;
  4456. commentsStartPos = /** @type {Range} */ (expr.callee.range)[1];
  4457. return expr.arguments.every((arg) => {
  4458. if (arg.type === "SpreadElement") return false;
  4459. const pureFlag = this.isPure(arg, commentsStartPos);
  4460. commentsStartPos = /** @type {Range} */ (arg.range)[1];
  4461. return pureFlag;
  4462. });
  4463. }
  4464. }
  4465. const evaluated = this.evaluateExpression(expr);
  4466. return !evaluated.couldHaveSideEffects();
  4467. }
  4468. /**
  4469. * @param {Range} range range
  4470. * @returns {Comment[]} comments in the range
  4471. */
  4472. getComments(range) {
  4473. const [rangeStart, rangeEnd] = range;
  4474. /**
  4475. * @param {Comment} comment comment
  4476. * @param {number} needle needle
  4477. * @returns {number} compared
  4478. */
  4479. const compare = (comment, needle) =>
  4480. /** @type {Range} */ (comment.range)[0] - needle;
  4481. const comments = /** @type {Comment[]} */ (this.comments);
  4482. let idx = binarySearchBounds.ge(comments, rangeStart, compare);
  4483. /** @type {Comment[]} */
  4484. const commentsInRange = [];
  4485. while (
  4486. comments[idx] &&
  4487. /** @type {Range} */ (comments[idx].range)[1] <= rangeEnd
  4488. ) {
  4489. commentsInRange.push(comments[idx]);
  4490. idx++;
  4491. }
  4492. return commentsInRange;
  4493. }
  4494. /**
  4495. * @param {number} pos source code position
  4496. * @returns {boolean} true when a semicolon has been inserted before this position, false if not
  4497. */
  4498. isAsiPosition(pos) {
  4499. const currentStatement =
  4500. /** @type {StatementPath} */
  4501. (this.statementPath)[
  4502. /** @type {StatementPath} */
  4503. (this.statementPath).length - 1
  4504. ];
  4505. if (currentStatement === undefined) throw new Error("Not in statement");
  4506. const range = /** @type {Range} */ (currentStatement.range);
  4507. return (
  4508. // Either asking directly for the end position of the current statement
  4509. (range[1] === pos &&
  4510. /** @type {Set<number>} */ (this.semicolons).has(pos)) ||
  4511. // Or asking for the start position of the current statement,
  4512. // here we have to check multiple things
  4513. (range[0] === pos &&
  4514. // is there a previous statement which might be relevant?
  4515. this.prevStatement !== undefined &&
  4516. // is the end position of the previous statement an ASI position?
  4517. /** @type {Set<number>} */ (this.semicolons).has(
  4518. /** @type {Range} */ (this.prevStatement.range)[1]
  4519. ))
  4520. );
  4521. }
  4522. /**
  4523. * @param {number} pos source code position
  4524. * @returns {void}
  4525. */
  4526. setAsiPosition(pos) {
  4527. /** @type {Set<number>} */ (this.semicolons).add(pos);
  4528. }
  4529. /**
  4530. * @param {number} pos source code position
  4531. * @returns {void}
  4532. */
  4533. unsetAsiPosition(pos) {
  4534. /** @type {Set<number>} */ (this.semicolons).delete(pos);
  4535. }
  4536. /**
  4537. * @param {Expression} expr expression
  4538. * @returns {boolean} true, when the expression is a statement level expression
  4539. */
  4540. isStatementLevelExpression(expr) {
  4541. const currentStatement =
  4542. /** @type {StatementPath} */
  4543. (this.statementPath)[
  4544. /** @type {StatementPath} */
  4545. (this.statementPath).length - 1
  4546. ];
  4547. return (
  4548. expr === currentStatement ||
  4549. (currentStatement.type === "ExpressionStatement" &&
  4550. currentStatement.expression === expr)
  4551. );
  4552. }
  4553. /**
  4554. * @param {string} name name
  4555. * @param {Tag} tag tag info
  4556. * @returns {TagData | undefined} tag data
  4557. */
  4558. getTagData(name, tag) {
  4559. const info = this.scope.definitions.get(name);
  4560. if (info instanceof VariableInfo) {
  4561. let tagInfo = info.tagInfo;
  4562. while (tagInfo !== undefined) {
  4563. if (tagInfo.tag === tag) return tagInfo.data;
  4564. tagInfo = tagInfo.next;
  4565. }
  4566. }
  4567. }
  4568. /**
  4569. * @param {string} name name
  4570. * @param {Tag} tag tag info
  4571. * @param {TagData=} data data
  4572. * @param {VariableInfoFlagsType=} flags flags
  4573. */
  4574. tagVariable(name, tag, data, flags = VariableInfoFlags.Tagged) {
  4575. const oldInfo = this.scope.definitions.get(name);
  4576. /** @type {VariableInfo} */
  4577. let newInfo;
  4578. if (oldInfo === undefined) {
  4579. newInfo = new VariableInfo(this.scope, name, flags, {
  4580. tag,
  4581. data,
  4582. next: undefined
  4583. });
  4584. } else if (oldInfo instanceof VariableInfo) {
  4585. newInfo = new VariableInfo(
  4586. oldInfo.declaredScope,
  4587. oldInfo.name,
  4588. /** @type {VariableInfoFlagsType} */ (oldInfo.flags | flags),
  4589. {
  4590. tag,
  4591. data,
  4592. next: oldInfo.tagInfo
  4593. }
  4594. );
  4595. } else {
  4596. newInfo = new VariableInfo(oldInfo, name, flags, {
  4597. tag,
  4598. data,
  4599. next: undefined
  4600. });
  4601. }
  4602. this.scope.definitions.set(name, newInfo);
  4603. }
  4604. /**
  4605. * @param {string} name variable name
  4606. */
  4607. defineVariable(name) {
  4608. const oldInfo = this.scope.definitions.get(name);
  4609. // Don't redefine variable in same scope to keep existing tags
  4610. if (
  4611. oldInfo instanceof VariableInfo &&
  4612. oldInfo.declaredScope === this.scope
  4613. ) {
  4614. return;
  4615. }
  4616. this.scope.definitions.set(name, this.scope);
  4617. }
  4618. /**
  4619. * @param {string} name variable name
  4620. */
  4621. undefineVariable(name) {
  4622. this.scope.definitions.delete(name);
  4623. }
  4624. /**
  4625. * @param {string} name variable name
  4626. * @returns {boolean} true, when variable is defined
  4627. */
  4628. isVariableDefined(name) {
  4629. const info = this.scope.definitions.get(name);
  4630. if (info === undefined) return false;
  4631. if (info instanceof VariableInfo) {
  4632. return !info.isFree();
  4633. }
  4634. return true;
  4635. }
  4636. /**
  4637. * @param {string} name variable name
  4638. * @returns {string | ExportedVariableInfo} info for this variable
  4639. */
  4640. getVariableInfo(name) {
  4641. const value = this.scope.definitions.get(name);
  4642. if (value === undefined) {
  4643. return name;
  4644. }
  4645. return value;
  4646. }
  4647. /**
  4648. * @param {string} name variable name
  4649. * @param {string | ExportedVariableInfo} variableInfo new info for this variable
  4650. * @returns {void}
  4651. */
  4652. setVariable(name, variableInfo) {
  4653. if (typeof variableInfo === "string") {
  4654. if (variableInfo === name) {
  4655. this.scope.definitions.delete(name);
  4656. } else {
  4657. this.scope.definitions.set(
  4658. name,
  4659. new VariableInfo(
  4660. this.scope,
  4661. variableInfo,
  4662. VariableInfoFlags.Free,
  4663. undefined
  4664. )
  4665. );
  4666. }
  4667. } else {
  4668. this.scope.definitions.set(name, variableInfo);
  4669. }
  4670. }
  4671. /**
  4672. * @param {TagInfo} tagInfo tag info
  4673. * @returns {VariableInfo} variable info
  4674. */
  4675. evaluatedVariable(tagInfo) {
  4676. return new VariableInfo(
  4677. this.scope,
  4678. undefined,
  4679. VariableInfoFlags.Evaluated,
  4680. tagInfo
  4681. );
  4682. }
  4683. /**
  4684. * @param {Range} range range of the comment
  4685. * @returns {{ options: Record<string, EXPECTED_ANY> | null, errors: (Error & { comment: Comment })[] | null }} result
  4686. */
  4687. parseCommentOptions(range) {
  4688. const comments = this.getComments(range);
  4689. if (comments.length === 0) {
  4690. return EMPTY_COMMENT_OPTIONS;
  4691. }
  4692. /** @type {Record<string, EXPECTED_ANY> } */
  4693. const options = {};
  4694. /** @type {(Error & { comment: Comment })[]} */
  4695. const errors = [];
  4696. for (const comment of comments) {
  4697. const { value } = comment;
  4698. if (value && webpackCommentRegExp.test(value)) {
  4699. // try compile only if webpack options comment is present
  4700. try {
  4701. for (let [key, val] of Object.entries(
  4702. vm.runInContext(
  4703. `(function(){return {${value}};})()`,
  4704. this.magicCommentContext
  4705. )
  4706. )) {
  4707. if (typeof val === "object" && val !== null) {
  4708. val =
  4709. val.constructor.name === "RegExp"
  4710. ? new RegExp(val)
  4711. : JSON.parse(JSON.stringify(val));
  4712. }
  4713. options[key] = val;
  4714. }
  4715. } catch (err) {
  4716. const newErr = new Error(String(/** @type {Error} */ (err).message));
  4717. newErr.stack = String(/** @type {Error} */ (err).stack);
  4718. Object.assign(newErr, { comment });
  4719. errors.push(/** @type {(Error & { comment: Comment })} */ (newErr));
  4720. }
  4721. }
  4722. }
  4723. return { options, errors };
  4724. }
  4725. /**
  4726. * @param {Expression | Super} expression a member expression
  4727. * @returns {{ members: string[], object: Expression | Super, membersOptionals: boolean[], memberRanges: Range[] }} member names (reverse order) and remaining object
  4728. */
  4729. extractMemberExpressionChain(expression) {
  4730. /** @type {Node} */
  4731. let expr = expression;
  4732. const members = [];
  4733. const membersOptionals = [];
  4734. const memberRanges = [];
  4735. while (expr.type === "MemberExpression") {
  4736. if (expr.computed) {
  4737. if (expr.property.type !== "Literal") break;
  4738. members.push(`${expr.property.value}`); // the literal
  4739. memberRanges.push(/** @type {Range} */ (expr.object.range)); // the range of the expression fragment before the literal
  4740. } else {
  4741. if (expr.property.type !== "Identifier") break;
  4742. members.push(expr.property.name); // the identifier
  4743. memberRanges.push(/** @type {Range} */ (expr.object.range)); // the range of the expression fragment before the identifier
  4744. }
  4745. membersOptionals.push(expr.optional);
  4746. expr = expr.object;
  4747. }
  4748. return {
  4749. members,
  4750. membersOptionals,
  4751. memberRanges,
  4752. object: expr
  4753. };
  4754. }
  4755. /**
  4756. * @param {string} varName variable name
  4757. * @returns {{name: string, info: VariableInfo | string} | undefined} name of the free variable and variable info for that
  4758. */
  4759. getFreeInfoFromVariable(varName) {
  4760. const info = this.getVariableInfo(varName);
  4761. let name;
  4762. if (info instanceof VariableInfo && info.name) {
  4763. if (!info.isFree()) return;
  4764. name = info.name;
  4765. } else if (typeof info !== "string") {
  4766. return;
  4767. } else {
  4768. name = info;
  4769. }
  4770. return { info, name };
  4771. }
  4772. /**
  4773. * @param {string} varName variable name
  4774. * @returns {{name: string, info: VariableInfo | string} | undefined} name of the free variable and variable info for that
  4775. */
  4776. getNameInfoFromVariable(varName) {
  4777. const info = this.getVariableInfo(varName);
  4778. let name;
  4779. if (info instanceof VariableInfo && info.name) {
  4780. if (!info.isFree() && !info.isTagged()) return;
  4781. name = info.name;
  4782. } else if (typeof info !== "string") {
  4783. return;
  4784. } else {
  4785. name = info;
  4786. }
  4787. return { info, name };
  4788. }
  4789. /** @typedef {{ type: "call", call: CallExpression, calleeName: string, rootInfo: string | VariableInfo, getCalleeMembers: () => string[], name: string, getMembers: () => string[], getMembersOptionals: () => boolean[], getMemberRanges: () => Range[]}} CallExpressionInfo */
  4790. /** @typedef {{ type: "expression", rootInfo: string | VariableInfo, name: string, getMembers: () => string[], getMembersOptionals: () => boolean[], getMemberRanges: () => Range[]}} ExpressionExpressionInfo */
  4791. /**
  4792. * @param {Expression | Super} expression a member expression
  4793. * @param {number} allowedTypes which types should be returned, presented in bit mask
  4794. * @returns {CallExpressionInfo | ExpressionExpressionInfo | undefined} expression info
  4795. */
  4796. getMemberExpressionInfo(expression, allowedTypes) {
  4797. const { object, members, membersOptionals, memberRanges } =
  4798. this.extractMemberExpressionChain(expression);
  4799. switch (object.type) {
  4800. case "CallExpression": {
  4801. if ((allowedTypes & ALLOWED_MEMBER_TYPES_CALL_EXPRESSION) === 0) return;
  4802. let callee = object.callee;
  4803. let rootMembers = EMPTY_ARRAY;
  4804. if (callee.type === "MemberExpression") {
  4805. ({ object: callee, members: rootMembers } =
  4806. this.extractMemberExpressionChain(callee));
  4807. }
  4808. const rootName = getRootName(callee);
  4809. if (!rootName) return;
  4810. const result = this.getNameInfoFromVariable(rootName);
  4811. if (!result) return;
  4812. const { info: rootInfo, name: resolvedRoot } = result;
  4813. const calleeName = objectAndMembersToName(resolvedRoot, rootMembers);
  4814. return {
  4815. type: "call",
  4816. call: object,
  4817. calleeName,
  4818. rootInfo,
  4819. getCalleeMembers: memoize(() => rootMembers.reverse()),
  4820. name: objectAndMembersToName(`${calleeName}()`, members),
  4821. getMembers: memoize(() => members.reverse()),
  4822. getMembersOptionals: memoize(() => membersOptionals.reverse()),
  4823. getMemberRanges: memoize(() => memberRanges.reverse())
  4824. };
  4825. }
  4826. case "Identifier":
  4827. case "MetaProperty":
  4828. case "ThisExpression": {
  4829. if ((allowedTypes & ALLOWED_MEMBER_TYPES_EXPRESSION) === 0) return;
  4830. const rootName = getRootName(object);
  4831. if (!rootName) return;
  4832. const result = this.getNameInfoFromVariable(rootName);
  4833. if (!result) return;
  4834. const { info: rootInfo, name: resolvedRoot } = result;
  4835. return {
  4836. type: "expression",
  4837. name: objectAndMembersToName(resolvedRoot, members),
  4838. rootInfo,
  4839. getMembers: memoize(() => members.reverse()),
  4840. getMembersOptionals: memoize(() => membersOptionals.reverse()),
  4841. getMemberRanges: memoize(() => memberRanges.reverse())
  4842. };
  4843. }
  4844. }
  4845. }
  4846. /**
  4847. * @param {Expression} expression an expression
  4848. * @returns {{ name: string, rootInfo: ExportedVariableInfo, getMembers: () => string[]} | undefined} name info
  4849. */
  4850. getNameForExpression(expression) {
  4851. return this.getMemberExpressionInfo(
  4852. expression,
  4853. ALLOWED_MEMBER_TYPES_EXPRESSION
  4854. );
  4855. }
  4856. /**
  4857. * @param {string} code source code
  4858. * @param {ParseOptions} options parsing options
  4859. * @returns {Program} parsed ast
  4860. */
  4861. static _parse(code, options) {
  4862. const type = options ? options.sourceType : "module";
  4863. /** @type {AcornOptions} */
  4864. const parserOptions = {
  4865. ...defaultParserOptions,
  4866. allowReturnOutsideFunction: type === "script",
  4867. ...options,
  4868. sourceType: type === "auto" ? "module" : type
  4869. };
  4870. /** @type {import("acorn").Program | undefined} */
  4871. let ast;
  4872. let error;
  4873. let threw = false;
  4874. try {
  4875. ast = parser.parse(code, parserOptions);
  4876. } catch (err) {
  4877. error = err;
  4878. threw = true;
  4879. }
  4880. if (threw && type === "auto") {
  4881. parserOptions.sourceType = "script";
  4882. if (!("allowReturnOutsideFunction" in options)) {
  4883. parserOptions.allowReturnOutsideFunction = true;
  4884. }
  4885. if (Array.isArray(parserOptions.onComment)) {
  4886. parserOptions.onComment.length = 0;
  4887. }
  4888. try {
  4889. ast = parser.parse(code, parserOptions);
  4890. threw = false;
  4891. } catch (_err) {
  4892. // we use the error from first parse try
  4893. // so nothing to do here
  4894. }
  4895. }
  4896. if (threw) {
  4897. throw error;
  4898. }
  4899. return /** @type {Program} */ (ast);
  4900. }
  4901. /**
  4902. * @param {((BaseParser: typeof AcornParser) => typeof AcornParser)[]} plugins parser plugin
  4903. * @returns {typeof JavascriptParser} parser
  4904. */
  4905. static extend(...plugins) {
  4906. parser = parser.extend(...plugins);
  4907. return JavascriptParser;
  4908. }
  4909. }
  4910. module.exports = JavascriptParser;
  4911. module.exports.ALLOWED_MEMBER_TYPES_ALL = ALLOWED_MEMBER_TYPES_ALL;
  4912. module.exports.ALLOWED_MEMBER_TYPES_CALL_EXPRESSION =
  4913. ALLOWED_MEMBER_TYPES_CALL_EXPRESSION;
  4914. module.exports.ALLOWED_MEMBER_TYPES_EXPRESSION =
  4915. ALLOWED_MEMBER_TYPES_EXPRESSION;
  4916. module.exports.VariableInfo = VariableInfo;
  4917. module.exports.VariableInfoFlags = VariableInfoFlags;
  4918. module.exports.getImportAttributes = getImportAttributes;