1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const { Parser: AcornParser, tokTypes } = require("acorn");
- const { SyncBailHook, HookMap } = require("tapable");
- const vm = require("vm");
- const Parser = require("../Parser");
- const StackedMap = require("../util/StackedMap");
- const binarySearchBounds = require("../util/binarySearchBounds");
- const {
- webpackCommentRegExp,
- createMagicCommentContext
- } = require("../util/magicComment");
- const memoize = require("../util/memoize");
- const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
- /** @typedef {import("acorn").Options} AcornOptions */
- /** @typedef {import("estree").AssignmentExpression} AssignmentExpression */
- /** @typedef {import("estree").BinaryExpression} BinaryExpression */
- /** @typedef {import("estree").BlockStatement} BlockStatement */
- /** @typedef {import("estree").SequenceExpression} SequenceExpression */
- /** @typedef {import("estree").CallExpression} CallExpression */
- /** @typedef {import("estree").BaseCallExpression} BaseCallExpression */
- /** @typedef {import("estree").StaticBlock} StaticBlock */
- /** @typedef {import("estree").ClassDeclaration} ClassDeclaration */
- /** @typedef {import("estree").ForStatement} ForStatement */
- /** @typedef {import("estree").SwitchStatement} SwitchStatement */
- /** @typedef {import("estree").ClassExpression} ClassExpression */
- /** @typedef {import("estree").Comment} Comment */
- /** @typedef {import("estree").ConditionalExpression} ConditionalExpression */
- /** @typedef {import("estree").Declaration} Declaration */
- /** @typedef {import("estree").PrivateIdentifier} PrivateIdentifier */
- /** @typedef {import("estree").PropertyDefinition} PropertyDefinition */
- /** @typedef {import("estree").Expression} Expression */
- /** @typedef {import("estree").Identifier} Identifier */
- /** @typedef {import("estree").VariableDeclaration} VariableDeclaration */
- /** @typedef {import("estree").IfStatement} IfStatement */
- /** @typedef {import("estree").LabeledStatement} LabeledStatement */
- /** @typedef {import("estree").Literal} Literal */
- /** @typedef {import("estree").LogicalExpression} LogicalExpression */
- /** @typedef {import("estree").ChainExpression} ChainExpression */
- /** @typedef {import("estree").MemberExpression} MemberExpression */
- /** @typedef {import("estree").YieldExpression} YieldExpression */
- /** @typedef {import("estree").MetaProperty} MetaProperty */
- /** @typedef {import("estree").Property} Property */
- /** @typedef {import("estree").AssignmentPattern} AssignmentPattern */
- /** @typedef {import("estree").ChainElement} ChainElement */
- /** @typedef {import("estree").Pattern} Pattern */
- /** @typedef {import("estree").UpdateExpression} UpdateExpression */
- /** @typedef {import("estree").ObjectExpression} ObjectExpression */
- /** @typedef {import("estree").UnaryExpression} UnaryExpression */
- /** @typedef {import("estree").ArrayExpression} ArrayExpression */
- /** @typedef {import("estree").ArrayPattern} ArrayPattern */
- /** @typedef {import("estree").AwaitExpression} AwaitExpression */
- /** @typedef {import("estree").ThisExpression} ThisExpression */
- /** @typedef {import("estree").RestElement} RestElement */
- /** @typedef {import("estree").ObjectPattern} ObjectPattern */
- /** @typedef {import("estree").SwitchCase} SwitchCase */
- /** @typedef {import("estree").CatchClause} CatchClause */
- /** @typedef {import("estree").VariableDeclarator} VariableDeclarator */
- /** @typedef {import("estree").ForInStatement} ForInStatement */
- /** @typedef {import("estree").ForOfStatement} ForOfStatement */
- /** @typedef {import("estree").ReturnStatement} ReturnStatement */
- /** @typedef {import("estree").WithStatement} WithStatement */
- /** @typedef {import("estree").ThrowStatement} ThrowStatement */
- /** @typedef {import("estree").MethodDefinition} MethodDefinition */
- /** @typedef {import("estree").NewExpression} NewExpression */
- /** @typedef {import("estree").SpreadElement} SpreadElement */
- /** @typedef {import("estree").FunctionExpression} FunctionExpression */
- /** @typedef {import("estree").WhileStatement} WhileStatement */
- /** @typedef {import("estree").ArrowFunctionExpression} ArrowFunctionExpression */
- /** @typedef {import("estree").ExpressionStatement} ExpressionStatement */
- /** @typedef {import("estree").FunctionDeclaration} FunctionDeclaration */
- /** @typedef {import("estree").DoWhileStatement} DoWhileStatement */
- /** @typedef {import("estree").TryStatement} TryStatement */
- /** @typedef {import("estree").Node} Node */
- /** @typedef {import("estree").Program} Program */
- /** @typedef {import("estree").Directive} Directive */
- /** @typedef {import("estree").Statement} Statement */
- /** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclaration */
- /** @typedef {import("estree").Super} Super */
- /** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpression */
- /** @typedef {import("estree").TemplateLiteral} TemplateLiteral */
- /** @typedef {import("estree").AssignmentProperty} AssignmentProperty */
- /** @typedef {import("estree").MaybeNamedFunctionDeclaration} MaybeNamedFunctionDeclaration */
- /** @typedef {import("estree").MaybeNamedClassDeclaration} MaybeNamedClassDeclaration */
- /**
- * @template T
- * @typedef {import("tapable").AsArray<T>} AsArray<T>
- */
- /** @typedef {import("../Parser").ParserState} ParserState */
- /** @typedef {import("../Parser").PreparsedAst} PreparsedAst */
- /** @typedef {{ name: string | VariableInfo, rootInfo: string | VariableInfo, getMembers: () => string[], getMembersOptionals: () => boolean[], getMemberRanges: () => Range[] }} GetInfoResult */
- /** @typedef {Statement | ModuleDeclaration | Expression | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} StatementPathItem */
- /** @typedef {(ident: string) => void} OnIdentString */
- /** @typedef {(ident: string, identifier: Identifier) => void} OnIdent */
- /** @typedef {StatementPathItem[]} StatementPath */
- // TODO remove cast when @types/estree has been updated to import assertions
- /** @typedef {import("estree").BaseNode & { type: "ImportAttribute", key: Identifier | Literal, value: Literal }} ImportAttribute */
- /** @typedef {import("estree").ImportDeclaration & { attributes?: Array<ImportAttribute> }} ImportDeclaration */
- /** @typedef {import("estree").ExportNamedDeclaration & { attributes?: Array<ImportAttribute> }} ExportNamedDeclaration */
- /** @typedef {import("estree").ExportAllDeclaration & { attributes?: Array<ImportAttribute> }} ExportAllDeclaration */
- /** @typedef {import("estree").ImportExpression & { options?: Expression | null }} ImportExpression */
- /** @typedef {ImportDeclaration | ExportNamedDeclaration | ExportDefaultDeclaration | ExportAllDeclaration} ModuleDeclaration */
- /** @type {string[]} */
- const EMPTY_ARRAY = [];
- const ALLOWED_MEMBER_TYPES_CALL_EXPRESSION = 0b01;
- const ALLOWED_MEMBER_TYPES_EXPRESSION = 0b10;
- const ALLOWED_MEMBER_TYPES_ALL = 0b11;
- const LEGACY_ASSERT_ATTRIBUTES = Symbol("assert");
- /** @type {(BaseParser: typeof AcornParser) => typeof AcornParser} */
- const importAssertions = Parser =>
- class extends Parser {
- /**
- * @this {TODO}
- * @returns {ImportAttribute[]} import attributes
- */
- parseWithClause() {
- /** @type {ImportAttribute[]} */
- const nodes = [];
- const isAssertLegacy = this.value === "assert";
- if (isAssertLegacy) {
- if (!this.eat(tokTypes.name)) {
- return nodes;
- }
- } else if (!this.eat(tokTypes._with)) {
- return nodes;
- }
- this.expect(tokTypes.braceL);
- /** @type {Record<string, boolean>} */
- const attributeKeys = {};
- let first = true;
- while (!this.eat(tokTypes.braceR)) {
- if (!first) {
- this.expect(tokTypes.comma);
- if (this.afterTrailingComma(tokTypes.braceR)) {
- break;
- }
- } else {
- first = false;
- }
- const attr =
- /** @type {ImportAttribute} */
- this.parseImportAttribute();
- const keyName =
- attr.key.type === "Identifier" ? attr.key.name : attr.key.value;
- if (Object.prototype.hasOwnProperty.call(attributeKeys, keyName)) {
- this.raiseRecoverable(
- attr.key.start,
- `Duplicate attribute key '${keyName}'`
- );
- }
- attributeKeys[keyName] = true;
- nodes.push(attr);
- }
- if (isAssertLegacy) {
- /** @type {EXPECTED_ANY} */
- (nodes)[LEGACY_ASSERT_ATTRIBUTES] = true;
- }
- return nodes;
- }
- };
- // Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
- const parser = AcornParser.extend(importAssertions);
- /** @typedef {Record<string, string> & { _isLegacyAssert?: boolean }} ImportAttributes */
- /**
- * @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | ImportExpression} node node with assertions
- * @returns {ImportAttributes | undefined} import attributes
- */
- const getImportAttributes = node => {
- if (node.type === "ImportExpression") {
- if (
- node.options &&
- node.options.type === "ObjectExpression" &&
- node.options.properties[0] &&
- node.options.properties[0].type === "Property" &&
- node.options.properties[0].key.type === "Identifier" &&
- (node.options.properties[0].key.name === "with" ||
- node.options.properties[0].key.name === "assert") &&
- node.options.properties[0].value.type === "ObjectExpression" &&
- node.options.properties[0].value.properties.length > 0
- ) {
- const properties =
- /** @type {Property[]} */
- (node.options.properties[0].value.properties);
- const result = /** @type {ImportAttributes} */ ({});
- for (const property of properties) {
- const key =
- /** @type {string} */
- (
- property.key.type === "Identifier"
- ? property.key.name
- : /** @type {Literal} */ (property.key).value
- );
- result[key] =
- /** @type {string} */
- (/** @type {Literal} */ (property.value).value);
- }
- const key =
- node.options.properties[0].key.type === "Identifier"
- ? node.options.properties[0].key.name
- : /** @type {Literal} */ (node.options.properties[0].key).value;
- if (key === "assert") {
- result._isLegacyAssert = true;
- }
- return result;
- }
- return;
- }
- if (node.attributes === undefined || node.attributes.length === 0) {
- return;
- }
- const result = /** @type {ImportAttributes} */ ({});
- for (const attribute of node.attributes) {
- const key =
- /** @type {string} */
- (
- attribute.key.type === "Identifier"
- ? attribute.key.name
- : attribute.key.value
- );
- result[key] = /** @type {string} */ (attribute.value.value);
- }
- if (/** @type {EXPECTED_ANY} */ (node.attributes)[LEGACY_ASSERT_ATTRIBUTES]) {
- result._isLegacyAssert = true;
- }
- return result;
- };
- class VariableInfo {
- /**
- * @param {ScopeInfo} declaredScope scope in which the variable is declared
- * @param {string | true | undefined} freeName which free name the variable aliases, or true when none
- * @param {TagInfo | undefined} tagInfo info about tags
- */
- constructor(declaredScope, freeName, tagInfo) {
- this.declaredScope = declaredScope;
- this.freeName = freeName;
- this.tagInfo = tagInfo;
- }
- }
- /** @typedef {string | ScopeInfo | VariableInfo} ExportedVariableInfo */
- /** @typedef {Literal | string | null | undefined} ImportSource */
- /** @typedef {Omit<AcornOptions, "sourceType" | "ecmaVersion"> & { sourceType: "module" | "script" | "auto", ecmaVersion?: AcornOptions["ecmaVersion"] }} ParseOptions */
- /** @typedef {symbol} Tag */
- /** @typedef {Record<string, TODO>} TagData */
- /**
- * @typedef {object} TagInfo
- * @property {Tag} tag
- * @property {TagData=} data
- * @property {TagInfo | undefined} next
- */
- const SCOPE_INFO_TERMINATED_RETURN = 1;
- const SCOPE_INFO_TERMINATED_THROW = 2;
- /**
- * @typedef {object} ScopeInfo
- * @property {StackedMap<string, VariableInfo | ScopeInfo>} definitions
- * @property {boolean | "arrow"} topLevelScope
- * @property {boolean | string} inShorthand
- * @property {boolean} inTaggedTemplateTag
- * @property {boolean} inTry
- * @property {boolean} isStrict
- * @property {boolean} isAsmJs
- * @property {undefined | 1 | 2} terminated
- */
- /** @typedef {[number, number]} Range */
- /**
- * @typedef {object} DestructuringAssignmentProperty
- * @property {string} id
- * @property {Range | undefined=} range
- * @property {boolean | string} shorthand
- */
- /**
- * Helper function for joining two ranges into a single range. This is useful
- * when working with AST nodes, as it allows you to combine the ranges of child nodes
- * to create the range of the _parent node_.
- * @param {Range} startRange start range to join
- * @param {Range} endRange end range to join
- * @returns {Range} joined range
- * @example
- * ```js
- * const startRange = [0, 5];
- * const endRange = [10, 15];
- * const joinedRange = joinRanges(startRange, endRange);
- * console.log(joinedRange); // [0, 15]
- * ```
- */
- const joinRanges = (startRange, endRange) => {
- if (!endRange) return startRange;
- if (!startRange) return endRange;
- return [startRange[0], endRange[1]];
- };
- /**
- * Helper function used to generate a string representation of a
- * [member expression](https://github.com/estree/estree/blob/master/es5.md#memberexpression).
- * @param {string} object object to name
- * @param {string[]} membersReversed reversed list of members
- * @returns {string} member expression as a string
- * @example
- * ```js
- * const membersReversed = ["property1", "property2", "property3"]; // Members parsed from the AST
- * const name = objectAndMembersToName("myObject", membersReversed);
- *
- * console.log(name); // "myObject.property1.property2.property3"
- * ```
- */
- const objectAndMembersToName = (object, membersReversed) => {
- let name = object;
- for (let i = membersReversed.length - 1; i >= 0; i--) {
- name = `${name}.${membersReversed[i]}`;
- }
- return name;
- };
- /**
- * Grabs the name of a given expression and returns it as a string or undefined. Has particular
- * handling for [Identifiers](https://github.com/estree/estree/blob/master/es5.md#identifier),
- * [ThisExpressions](https://github.com/estree/estree/blob/master/es5.md#identifier), and
- * [MetaProperties](https://github.com/estree/estree/blob/master/es2015.md#metaproperty) which is
- * specifically for handling the `new.target` meta property.
- * @param {Expression | SpreadElement | Super} expression expression
- * @returns {string | "this" | undefined} name or variable info
- */
- const getRootName = expression => {
- switch (expression.type) {
- case "Identifier":
- return expression.name;
- case "ThisExpression":
- return "this";
- case "MetaProperty":
- return `${expression.meta.name}.${expression.property.name}`;
- default:
- return undefined;
- }
- };
- /** @type {AcornOptions} */
- const defaultParserOptions = {
- ranges: true,
- locations: true,
- ecmaVersion: "latest",
- sourceType: "module",
- // https://github.com/tc39/proposal-hashbang
- allowHashBang: true,
- onComment: undefined
- };
- const EMPTY_COMMENT_OPTIONS = {
- options: null,
- errors: null
- };
- const CLASS_NAME = "JavascriptParser";
- class JavascriptParser extends Parser {
- /**
- * @param {"module" | "script" | "auto"} sourceType default source type
- */
- constructor(sourceType = "auto") {
- super();
- this.hooks = Object.freeze({
- /** @type {HookMap<SyncBailHook<[UnaryExpression], BasicEvaluatedExpression | null | undefined>>} */
- evaluateTypeof: new HookMap(() => new SyncBailHook(["expression"])),
- /** @type {HookMap<SyncBailHook<[Expression | SpreadElement | PrivateIdentifier | Super], BasicEvaluatedExpression | null | undefined>>} */
- evaluate: new HookMap(() => new SyncBailHook(["expression"])),
- /** @type {HookMap<SyncBailHook<[Identifier | ThisExpression | MemberExpression | MetaProperty], BasicEvaluatedExpression | null | undefined>>} */
- evaluateIdentifier: new HookMap(() => new SyncBailHook(["expression"])),
- /** @type {HookMap<SyncBailHook<[Identifier | ThisExpression | MemberExpression], BasicEvaluatedExpression | null | undefined>>} */
- evaluateDefinedIdentifier: new HookMap(
- () => new SyncBailHook(["expression"])
- ),
- /** @type {HookMap<SyncBailHook<[NewExpression], BasicEvaluatedExpression | null | undefined>>} */
- evaluateNewExpression: new HookMap(
- () => new SyncBailHook(["expression"])
- ),
- /** @type {HookMap<SyncBailHook<[CallExpression], BasicEvaluatedExpression | null | undefined>>} */
- evaluateCallExpression: new HookMap(
- () => new SyncBailHook(["expression"])
- ),
- /** @type {HookMap<SyncBailHook<[CallExpression, BasicEvaluatedExpression], BasicEvaluatedExpression | null | undefined>>} */
- evaluateCallExpressionMember: new HookMap(
- () => new SyncBailHook(["expression", "param"])
- ),
- /** @type {HookMap<SyncBailHook<[Expression | Declaration | PrivateIdentifier | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration, number], boolean | void>>} */
- isPure: new HookMap(
- () => new SyncBailHook(["expression", "commentsStartPosition"])
- ),
- /** @type {SyncBailHook<[Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration], boolean | void>} */
- preStatement: new SyncBailHook(["statement"]),
- /** @type {SyncBailHook<[Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration], boolean | void>} */
- blockPreStatement: new SyncBailHook(["declaration"]),
- /** @type {SyncBailHook<[Statement | ModuleDeclaration | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
- statement: new SyncBailHook(["statement"]),
- /** @type {SyncBailHook<[IfStatement], boolean | void>} */
- statementIf: new SyncBailHook(["statement"]),
- /** @type {SyncBailHook<[Expression, ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
- classExtendsExpression: new SyncBailHook([
- "expression",
- "classDefinition"
- ]),
- /** @type {SyncBailHook<[MethodDefinition | PropertyDefinition | StaticBlock, ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
- classBodyElement: new SyncBailHook(["element", "classDefinition"]),
- /** @type {SyncBailHook<[Expression, MethodDefinition | PropertyDefinition, ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration], boolean | void>} */
- classBodyValue: new SyncBailHook([
- "expression",
- "element",
- "classDefinition"
- ]),
- /** @type {HookMap<SyncBailHook<[LabeledStatement], boolean | void>>} */
- label: new HookMap(() => new SyncBailHook(["statement"])),
- /** @type {SyncBailHook<[ImportDeclaration, ImportSource], boolean | void>} */
- import: new SyncBailHook(["statement", "source"]),
- /** @type {SyncBailHook<[ImportDeclaration, ImportSource, string | null, string], boolean | void>} */
- importSpecifier: new SyncBailHook([
- "statement",
- "source",
- "exportName",
- "identifierName"
- ]),
- /** @type {SyncBailHook<[ExportDefaultDeclaration | ExportNamedDeclaration], boolean | void>} */
- export: new SyncBailHook(["statement"]),
- /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, ImportSource], boolean | void>} */
- exportImport: new SyncBailHook(["statement", "source"]),
- /** @type {SyncBailHook<[ExportDefaultDeclaration | ExportNamedDeclaration | ExportAllDeclaration, Declaration], boolean | void>} */
- exportDeclaration: new SyncBailHook(["statement", "declaration"]),
- /** @type {SyncBailHook<[ExportDefaultDeclaration, MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | Expression], boolean | void>} */
- exportExpression: new SyncBailHook(["statement", "node"]),
- /** @type {SyncBailHook<[ExportDefaultDeclaration | ExportNamedDeclaration | ExportAllDeclaration, string, string, number | undefined], boolean | void>} */
- exportSpecifier: new SyncBailHook([
- "statement",
- "identifierName",
- "exportName",
- "index"
- ]),
- /** @type {SyncBailHook<[ExportNamedDeclaration | ExportAllDeclaration, ImportSource, string | null, string | null, number | undefined], boolean | void>} */
- exportImportSpecifier: new SyncBailHook([
- "statement",
- "source",
- "identifierName",
- "exportName",
- "index"
- ]),
- /** @type {SyncBailHook<[VariableDeclarator, Statement], boolean | void>} */
- preDeclarator: new SyncBailHook(["declarator", "statement"]),
- /** @type {SyncBailHook<[VariableDeclarator, Statement], boolean | void>} */
- declarator: new SyncBailHook(["declarator", "statement"]),
- /** @type {HookMap<SyncBailHook<[Declaration], boolean | void>>} */
- varDeclaration: new HookMap(() => new SyncBailHook(["declaration"])),
- /** @type {HookMap<SyncBailHook<[Declaration], boolean | void>>} */
- varDeclarationLet: new HookMap(() => new SyncBailHook(["declaration"])),
- /** @type {HookMap<SyncBailHook<[Declaration], boolean | void>>} */
- varDeclarationConst: new HookMap(() => new SyncBailHook(["declaration"])),
- /** @type {HookMap<SyncBailHook<[Declaration], boolean | void>>} */
- varDeclarationVar: new HookMap(() => new SyncBailHook(["declaration"])),
- /** @type {HookMap<SyncBailHook<[Identifier], boolean | void>>} */
- pattern: new HookMap(() => new SyncBailHook(["pattern"])),
- /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
- canRename: new HookMap(() => new SyncBailHook(["initExpression"])),
- /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
- rename: new HookMap(() => new SyncBailHook(["initExpression"])),
- /** @type {HookMap<SyncBailHook<[AssignmentExpression], boolean | void>>} */
- assign: new HookMap(() => new SyncBailHook(["expression"])),
- /** @type {HookMap<SyncBailHook<[AssignmentExpression, string[]], boolean | void>>} */
- assignMemberChain: new HookMap(
- () => new SyncBailHook(["expression", "members"])
- ),
- /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
- typeof: new HookMap(() => new SyncBailHook(["expression"])),
- /** @type {SyncBailHook<[ImportExpression], boolean | void>} */
- importCall: new SyncBailHook(["expression"]),
- /** @type {SyncBailHook<[Expression | ForOfStatement], boolean | void>} */
- topLevelAwait: new SyncBailHook(["expression"]),
- /** @type {HookMap<SyncBailHook<[CallExpression], boolean | void>>} */
- call: new HookMap(() => new SyncBailHook(["expression"])),
- /** Something like "a.b()" */
- /** @type {HookMap<SyncBailHook<[CallExpression, string[], boolean[], Range[]], boolean | void>>} */
- callMemberChain: new HookMap(
- () =>
- new SyncBailHook([
- "expression",
- "members",
- "membersOptionals",
- "memberRanges"
- ])
- ),
- /** Something like "a.b().c.d" */
- /** @type {HookMap<SyncBailHook<[Expression, string[], CallExpression, string[], Range[]], boolean | void>>} */
- memberChainOfCallMemberChain: new HookMap(
- () =>
- new SyncBailHook([
- "expression",
- "calleeMembers",
- "callExpression",
- "members",
- "memberRanges"
- ])
- ),
- /** Something like "a.b().c.d()"" */
- /** @type {HookMap<SyncBailHook<[CallExpression, string[], CallExpression, string[], Range[]], boolean | void>>} */
- callMemberChainOfCallMemberChain: new HookMap(
- () =>
- new SyncBailHook([
- "expression",
- "calleeMembers",
- "innerCallExpression",
- "members",
- "memberRanges"
- ])
- ),
- /** @type {SyncBailHook<[ChainExpression], boolean | void>} */
- optionalChaining: new SyncBailHook(["optionalChaining"]),
- /** @type {HookMap<SyncBailHook<[NewExpression], boolean | void>>} */
- new: new HookMap(() => new SyncBailHook(["expression"])),
- /** @type {SyncBailHook<[BinaryExpression], boolean | void>} */
- binaryExpression: new SyncBailHook(["binaryExpression"]),
- /** @type {HookMap<SyncBailHook<[Expression], boolean | void>>} */
- expression: new HookMap(() => new SyncBailHook(["expression"])),
- /** @type {HookMap<SyncBailHook<[MemberExpression, string[], boolean[], Range[]], boolean | void>>} */
- expressionMemberChain: new HookMap(
- () =>
- new SyncBailHook([
- "expression",
- "members",
- "membersOptionals",
- "memberRanges"
- ])
- ),
- /** @type {HookMap<SyncBailHook<[MemberExpression, string[]], boolean | void>>} */
- unhandledExpressionMemberChain: new HookMap(
- () => new SyncBailHook(["expression", "members"])
- ),
- /** @type {SyncBailHook<[ConditionalExpression], boolean | void>} */
- expressionConditionalOperator: new SyncBailHook(["expression"]),
- /** @type {SyncBailHook<[LogicalExpression], boolean | void>} */
- expressionLogicalOperator: new SyncBailHook(["expression"]),
- /** @type {SyncBailHook<[Program, Comment[]], boolean | void>} */
- program: new SyncBailHook(["ast", "comments"]),
- /** @type {SyncBailHook<[ThrowStatement | ReturnStatement], boolean | void>} */
- terminate: new SyncBailHook(["statement"]),
- /** @type {SyncBailHook<[Program, Comment[]], boolean | void>} */
- finish: new SyncBailHook(["ast", "comments"])
- });
- this.sourceType = sourceType;
- /** @type {ScopeInfo} */
- this.scope = /** @type {TODO} */ (undefined);
- /** @type {ParserState} */
- this.state = /** @type {TODO} */ (undefined);
- /** @type {Comment[] | undefined} */
- this.comments = undefined;
- /** @type {Set<number> | undefined} */
- this.semicolons = undefined;
- /** @type {StatementPath | undefined} */
- this.statementPath = undefined;
- /** @type {Statement | ModuleDeclaration | Expression | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | undefined} */
- this.prevStatement = undefined;
- /** @type {WeakMap<Expression, Set<DestructuringAssignmentProperty>> | undefined} */
- this.destructuringAssignmentProperties = undefined;
- /** @type {TagData | undefined} */
- this.currentTagData = undefined;
- this.magicCommentContext = createMagicCommentContext();
- this._initializeEvaluating();
- }
- _initializeEvaluating() {
- this.hooks.evaluate.for("Literal").tap(CLASS_NAME, _expr => {
- const expr = /** @type {Literal} */ (_expr);
- switch (typeof expr.value) {
- case "number":
- return new BasicEvaluatedExpression()
- .setNumber(expr.value)
- .setRange(/** @type {Range} */ (expr.range));
- case "bigint":
- return new BasicEvaluatedExpression()
- .setBigInt(expr.value)
- .setRange(/** @type {Range} */ (expr.range));
- case "string":
- return new BasicEvaluatedExpression()
- .setString(expr.value)
- .setRange(/** @type {Range} */ (expr.range));
- case "boolean":
- return new BasicEvaluatedExpression()
- .setBoolean(expr.value)
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (expr.value === null) {
- return new BasicEvaluatedExpression()
- .setNull()
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (expr.value instanceof RegExp) {
- return new BasicEvaluatedExpression()
- .setRegExp(expr.value)
- .setRange(/** @type {Range} */ (expr.range));
- }
- });
- this.hooks.evaluate.for("NewExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {NewExpression} */ (_expr);
- const callee = expr.callee;
- if (callee.type !== "Identifier") return;
- if (callee.name !== "RegExp") {
- return this.callHooksForName(
- this.hooks.evaluateNewExpression,
- callee.name,
- expr
- );
- } else if (
- expr.arguments.length > 2 ||
- this.getVariableInfo("RegExp") !== "RegExp"
- )
- return;
- let regExp;
- const arg1 = expr.arguments[0];
- if (arg1) {
- if (arg1.type === "SpreadElement") return;
- const evaluatedRegExp = this.evaluateExpression(arg1);
- if (!evaluatedRegExp) return;
- regExp = evaluatedRegExp.asString();
- if (!regExp) return;
- } else {
- return (
- new BasicEvaluatedExpression()
- // eslint-disable-next-line prefer-regex-literals
- .setRegExp(new RegExp(""))
- .setRange(/** @type {Range} */ (expr.range))
- );
- }
- let flags;
- const arg2 = expr.arguments[1];
- if (arg2) {
- if (arg2.type === "SpreadElement") return;
- const evaluatedFlags = this.evaluateExpression(arg2);
- if (!evaluatedFlags) return;
- if (!evaluatedFlags.isUndefined()) {
- flags = evaluatedFlags.asString();
- if (
- flags === undefined ||
- !BasicEvaluatedExpression.isValidRegExpFlags(flags)
- )
- return;
- }
- }
- return new BasicEvaluatedExpression()
- .setRegExp(flags ? new RegExp(regExp, flags) : new RegExp(regExp))
- .setRange(/** @type {Range} */ (expr.range));
- });
- this.hooks.evaluate.for("LogicalExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {LogicalExpression} */ (_expr);
- const left = this.evaluateExpression(expr.left);
- let returnRight = false;
- /** @type {boolean | undefined} */
- let allowedRight;
- if (expr.operator === "&&") {
- const leftAsBool = left.asBool();
- if (leftAsBool === false)
- return left.setRange(/** @type {Range} */ (expr.range));
- returnRight = leftAsBool === true;
- allowedRight = false;
- } else if (expr.operator === "||") {
- const leftAsBool = left.asBool();
- if (leftAsBool === true)
- return left.setRange(/** @type {Range} */ (expr.range));
- returnRight = leftAsBool === false;
- allowedRight = true;
- } else if (expr.operator === "??") {
- const leftAsNullish = left.asNullish();
- if (leftAsNullish === false)
- return left.setRange(/** @type {Range} */ (expr.range));
- if (leftAsNullish !== true) return;
- returnRight = true;
- } else return;
- const right = this.evaluateExpression(expr.right);
- if (returnRight) {
- if (left.couldHaveSideEffects()) right.setSideEffects();
- return right.setRange(/** @type {Range} */ (expr.range));
- }
- const asBool = right.asBool();
- if (allowedRight === true && asBool === true) {
- return new BasicEvaluatedExpression()
- .setRange(/** @type {Range} */ (expr.range))
- .setTruthy();
- } else if (allowedRight === false && asBool === false) {
- return new BasicEvaluatedExpression()
- .setRange(/** @type {Range} */ (expr.range))
- .setFalsy();
- }
- });
- /**
- * In simple logical cases, we can use valueAsExpression to assist us in evaluating the expression on
- * either side of a [BinaryExpression](https://github.com/estree/estree/blob/master/es5.md#binaryexpression).
- * This supports scenarios in webpack like conditionally `import()`'ing modules based on some simple evaluation:
- *
- * ```js
- * if (1 === 3) {
- * import("./moduleA"); // webpack will auto evaluate this and not import the modules
- * }
- * ```
- *
- * Additional scenarios include evaluation of strings inside of dynamic import statements:
- *
- * ```js
- * const foo = "foo";
- * const bar = "bar";
- *
- * import("./" + foo + bar); // webpack will auto evaluate this into import("./foobar")
- * ```
- * @param {boolean | number | bigint | string} value the value to convert to an expression
- * @param {BinaryExpression | UnaryExpression} expr the expression being evaluated
- * @param {boolean} sideEffects whether the expression has side effects
- * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
- * @example
- *
- * ```js
- * const binaryExpr = new BinaryExpression("+",
- * { type: "Literal", value: 2 },
- * { type: "Literal", value: 3 }
- * );
- *
- * const leftValue = 2;
- * const rightValue = 3;
- *
- * const leftExpr = valueAsExpression(leftValue, binaryExpr.left, false);
- * const rightExpr = valueAsExpression(rightValue, binaryExpr.right, false);
- * const result = new BasicEvaluatedExpression()
- * .setNumber(leftExpr.number + rightExpr.number)
- * .setRange(binaryExpr.range);
- *
- * console.log(result.number); // Output: 5
- * ```
- */
- const valueAsExpression = (value, expr, sideEffects) => {
- switch (typeof value) {
- case "boolean":
- return new BasicEvaluatedExpression()
- .setBoolean(value)
- .setSideEffects(sideEffects)
- .setRange(/** @type {Range} */ (expr.range));
- case "number":
- return new BasicEvaluatedExpression()
- .setNumber(value)
- .setSideEffects(sideEffects)
- .setRange(/** @type {Range} */ (expr.range));
- case "bigint":
- return new BasicEvaluatedExpression()
- .setBigInt(value)
- .setSideEffects(sideEffects)
- .setRange(/** @type {Range} */ (expr.range));
- case "string":
- return new BasicEvaluatedExpression()
- .setString(value)
- .setSideEffects(sideEffects)
- .setRange(/** @type {Range} */ (expr.range));
- }
- };
- this.hooks.evaluate.for("BinaryExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {BinaryExpression} */ (_expr);
- /**
- * Evaluates a binary expression if and only if it is a const operation (e.g. 1 + 2, "a" + "b", etc.).
- * @template T
- * @param {(leftOperand: T, rightOperand: T) => boolean | number | bigint | string} operandHandler the handler for the operation (e.g. (a, b) => a + b)
- * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
- */
- const handleConstOperation = operandHandler => {
- const left = this.evaluateExpression(expr.left);
- if (!left.isCompileTimeValue()) return;
- const right = this.evaluateExpression(expr.right);
- if (!right.isCompileTimeValue()) return;
- const result = operandHandler(
- /** @type {T} */ (left.asCompileTimeValue()),
- /** @type {T} */ (right.asCompileTimeValue())
- );
- return valueAsExpression(
- result,
- expr,
- left.couldHaveSideEffects() || right.couldHaveSideEffects()
- );
- };
- /**
- * Helper function to determine if two booleans are always different. This is used in `handleStrictEqualityComparison`
- * to determine if an expressions boolean or nullish conversion is equal or not.
- * @param {boolean} a first boolean to compare
- * @param {boolean} b second boolean to compare
- * @returns {boolean} true if the two booleans are always different, false otherwise
- */
- const isAlwaysDifferent = (a, b) =>
- (a === true && b === false) || (a === false && b === true);
- /**
- * @param {BasicEvaluatedExpression} left left
- * @param {BasicEvaluatedExpression} right right
- * @param {BasicEvaluatedExpression} res res
- * @param {boolean} eql true for "===" and false for "!=="
- * @returns {BasicEvaluatedExpression | undefined} result
- */
- const handleTemplateStringCompare = (left, right, res, eql) => {
- /**
- * @param {BasicEvaluatedExpression[]} parts parts
- * @returns {string} value
- */
- const getPrefix = parts => {
- let value = "";
- for (const p of parts) {
- const v = p.asString();
- if (v !== undefined) value += v;
- else break;
- }
- return value;
- };
- /**
- * @param {BasicEvaluatedExpression[]} parts parts
- * @returns {string} value
- */
- const getSuffix = parts => {
- let value = "";
- for (let i = parts.length - 1; i >= 0; i--) {
- const v = parts[i].asString();
- if (v !== undefined) value = v + value;
- else break;
- }
- return value;
- };
- const leftPrefix = getPrefix(
- /** @type {BasicEvaluatedExpression[]} */ (left.parts)
- );
- const rightPrefix = getPrefix(
- /** @type {BasicEvaluatedExpression[]} */ (right.parts)
- );
- const leftSuffix = getSuffix(
- /** @type {BasicEvaluatedExpression[]} */ (left.parts)
- );
- const rightSuffix = getSuffix(
- /** @type {BasicEvaluatedExpression[]} */ (right.parts)
- );
- const lenPrefix = Math.min(leftPrefix.length, rightPrefix.length);
- const lenSuffix = Math.min(leftSuffix.length, rightSuffix.length);
- const prefixMismatch =
- lenPrefix > 0 &&
- leftPrefix.slice(0, lenPrefix) !== rightPrefix.slice(0, lenPrefix);
- const suffixMismatch =
- lenSuffix > 0 &&
- leftSuffix.slice(-lenSuffix) !== rightSuffix.slice(-lenSuffix);
- if (prefixMismatch || suffixMismatch) {
- return res
- .setBoolean(!eql)
- .setSideEffects(
- left.couldHaveSideEffects() || right.couldHaveSideEffects()
- );
- }
- };
- /**
- * Helper function to handle BinaryExpressions using strict equality comparisons (e.g. "===" and "!==").
- * @param {boolean} eql true for "===" and false for "!=="
- * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
- */
- const handleStrictEqualityComparison = eql => {
- const left = this.evaluateExpression(expr.left);
- const right = this.evaluateExpression(expr.right);
- const res = new BasicEvaluatedExpression();
- res.setRange(/** @type {Range} */ (expr.range));
- const leftConst = left.isCompileTimeValue();
- const rightConst = right.isCompileTimeValue();
- if (leftConst && rightConst) {
- return res
- .setBoolean(
- eql === (left.asCompileTimeValue() === right.asCompileTimeValue())
- )
- .setSideEffects(
- left.couldHaveSideEffects() || right.couldHaveSideEffects()
- );
- }
- if (left.isArray() && right.isArray()) {
- return res
- .setBoolean(!eql)
- .setSideEffects(
- left.couldHaveSideEffects() || right.couldHaveSideEffects()
- );
- }
- if (left.isTemplateString() && right.isTemplateString()) {
- return handleTemplateStringCompare(left, right, res, eql);
- }
- const leftPrimitive = left.isPrimitiveType();
- const rightPrimitive = right.isPrimitiveType();
- if (
- // Primitive !== Object or
- // compile-time object types are never equal to something at runtime
- (leftPrimitive === false && (leftConst || rightPrimitive === true)) ||
- (rightPrimitive === false &&
- (rightConst || leftPrimitive === true)) ||
- // Different nullish or boolish status also means not equal
- isAlwaysDifferent(
- /** @type {boolean} */ (left.asBool()),
- /** @type {boolean} */ (right.asBool())
- ) ||
- isAlwaysDifferent(
- /** @type {boolean} */ (left.asNullish()),
- /** @type {boolean} */ (right.asNullish())
- )
- ) {
- return res
- .setBoolean(!eql)
- .setSideEffects(
- left.couldHaveSideEffects() || right.couldHaveSideEffects()
- );
- }
- };
- /**
- * Helper function to handle BinaryExpressions using abstract equality comparisons (e.g. "==" and "!=").
- * @param {boolean} eql true for "==" and false for "!="
- * @returns {BasicEvaluatedExpression | undefined} the evaluated expression
- */
- const handleAbstractEqualityComparison = eql => {
- const left = this.evaluateExpression(expr.left);
- const right = this.evaluateExpression(expr.right);
- const res = new BasicEvaluatedExpression();
- res.setRange(/** @type {Range} */ (expr.range));
- const leftConst = left.isCompileTimeValue();
- const rightConst = right.isCompileTimeValue();
- if (leftConst && rightConst) {
- return res
- .setBoolean(
- eql ===
- // eslint-disable-next-line eqeqeq
- (left.asCompileTimeValue() == right.asCompileTimeValue())
- )
- .setSideEffects(
- left.couldHaveSideEffects() || right.couldHaveSideEffects()
- );
- }
- if (left.isArray() && right.isArray()) {
- return res
- .setBoolean(!eql)
- .setSideEffects(
- left.couldHaveSideEffects() || right.couldHaveSideEffects()
- );
- }
- if (left.isTemplateString() && right.isTemplateString()) {
- return handleTemplateStringCompare(left, right, res, eql);
- }
- };
- if (expr.operator === "+") {
- const left = this.evaluateExpression(expr.left);
- const right = this.evaluateExpression(expr.right);
- const res = new BasicEvaluatedExpression();
- if (left.isString()) {
- if (right.isString()) {
- res.setString(
- /** @type {string} */ (left.string) +
- /** @type {string} */ (right.string)
- );
- } else if (right.isNumber()) {
- res.setString(/** @type {string} */ (left.string) + right.number);
- } else if (
- right.isWrapped() &&
- right.prefix &&
- right.prefix.isString()
- ) {
- // "left" + ("prefix" + inner + "postfix")
- // => ("leftPrefix" + inner + "postfix")
- res.setWrapped(
- new BasicEvaluatedExpression()
- .setString(
- /** @type {string} */ (left.string) +
- /** @type {string} */ (right.prefix.string)
- )
- .setRange(
- joinRanges(
- /** @type {Range} */ (left.range),
- /** @type {Range} */ (right.prefix.range)
- )
- ),
- right.postfix,
- right.wrappedInnerExpressions
- );
- } else if (right.isWrapped()) {
- // "left" + ([null] + inner + "postfix")
- // => ("left" + inner + "postfix")
- res.setWrapped(left, right.postfix, right.wrappedInnerExpressions);
- } else {
- // "left" + expr
- // => ("left" + expr + "")
- res.setWrapped(left, null, [right]);
- }
- } else if (left.isNumber()) {
- if (right.isString()) {
- res.setString(left.number + /** @type {string} */ (right.string));
- } else if (right.isNumber()) {
- res.setNumber(
- /** @type {number} */ (left.number) +
- /** @type {number} */ (right.number)
- );
- } else {
- return;
- }
- } else if (left.isBigInt()) {
- if (right.isBigInt()) {
- res.setBigInt(
- /** @type {bigint} */ (left.bigint) +
- /** @type {bigint} */ (right.bigint)
- );
- }
- } else if (left.isWrapped()) {
- if (left.postfix && left.postfix.isString() && right.isString()) {
- // ("prefix" + inner + "postfix") + "right"
- // => ("prefix" + inner + "postfixRight")
- res.setWrapped(
- left.prefix,
- new BasicEvaluatedExpression()
- .setString(
- /** @type {string} */ (left.postfix.string) +
- /** @type {string} */ (right.string)
- )
- .setRange(
- joinRanges(
- /** @type {Range} */ (left.postfix.range),
- /** @type {Range} */ (right.range)
- )
- ),
- left.wrappedInnerExpressions
- );
- } else if (
- left.postfix &&
- left.postfix.isString() &&
- right.isNumber()
- ) {
- // ("prefix" + inner + "postfix") + 123
- // => ("prefix" + inner + "postfix123")
- res.setWrapped(
- left.prefix,
- new BasicEvaluatedExpression()
- .setString(
- /** @type {string} */ (left.postfix.string) +
- /** @type {number} */ (right.number)
- )
- .setRange(
- joinRanges(
- /** @type {Range} */ (left.postfix.range),
- /** @type {Range} */ (right.range)
- )
- ),
- left.wrappedInnerExpressions
- );
- } else if (right.isString()) {
- // ("prefix" + inner + [null]) + "right"
- // => ("prefix" + inner + "right")
- res.setWrapped(left.prefix, right, left.wrappedInnerExpressions);
- } else if (right.isNumber()) {
- // ("prefix" + inner + [null]) + 123
- // => ("prefix" + inner + "123")
- res.setWrapped(
- left.prefix,
- new BasicEvaluatedExpression()
- .setString(String(right.number))
- .setRange(/** @type {Range} */ (right.range)),
- left.wrappedInnerExpressions
- );
- } else if (right.isWrapped()) {
- // ("prefix1" + inner1 + "postfix1") + ("prefix2" + inner2 + "postfix2")
- // ("prefix1" + inner1 + "postfix1" + "prefix2" + inner2 + "postfix2")
- res.setWrapped(
- left.prefix,
- right.postfix,
- left.wrappedInnerExpressions &&
- right.wrappedInnerExpressions &&
- left.wrappedInnerExpressions
- .concat(left.postfix ? [left.postfix] : [])
- .concat(right.prefix ? [right.prefix] : [])
- .concat(right.wrappedInnerExpressions)
- );
- } else {
- // ("prefix" + inner + postfix) + expr
- // => ("prefix" + inner + postfix + expr + [null])
- res.setWrapped(
- left.prefix,
- null,
- left.wrappedInnerExpressions &&
- left.wrappedInnerExpressions.concat(
- left.postfix ? [left.postfix, right] : [right]
- )
- );
- }
- } else if (right.isString()) {
- // left + "right"
- // => ([null] + left + "right")
- res.setWrapped(null, right, [left]);
- } else if (right.isWrapped()) {
- // left + (prefix + inner + "postfix")
- // => ([null] + left + prefix + inner + "postfix")
- res.setWrapped(
- null,
- right.postfix,
- right.wrappedInnerExpressions &&
- (right.prefix ? [left, right.prefix] : [left]).concat(
- right.wrappedInnerExpressions
- )
- );
- } else {
- return;
- }
- if (left.couldHaveSideEffects() || right.couldHaveSideEffects())
- res.setSideEffects();
- res.setRange(/** @type {Range} */ (expr.range));
- return res;
- } else if (expr.operator === "-") {
- return handleConstOperation((l, r) => l - r);
- } else if (expr.operator === "*") {
- return handleConstOperation((l, r) => l * r);
- } else if (expr.operator === "/") {
- return handleConstOperation((l, r) => l / r);
- } else if (expr.operator === "**") {
- return handleConstOperation((l, r) => l ** r);
- } else if (expr.operator === "===") {
- return handleStrictEqualityComparison(true);
- } else if (expr.operator === "==") {
- return handleAbstractEqualityComparison(true);
- } else if (expr.operator === "!==") {
- return handleStrictEqualityComparison(false);
- } else if (expr.operator === "!=") {
- return handleAbstractEqualityComparison(false);
- } else if (expr.operator === "&") {
- return handleConstOperation((l, r) => l & r);
- } else if (expr.operator === "|") {
- return handleConstOperation((l, r) => l | r);
- } else if (expr.operator === "^") {
- return handleConstOperation((l, r) => l ^ r);
- } else if (expr.operator === ">>>") {
- return handleConstOperation((l, r) => l >>> r);
- } else if (expr.operator === ">>") {
- return handleConstOperation((l, r) => l >> r);
- } else if (expr.operator === "<<") {
- return handleConstOperation((l, r) => l << r);
- } else if (expr.operator === "<") {
- return handleConstOperation((l, r) => l < r);
- } else if (expr.operator === ">") {
- return handleConstOperation((l, r) => l > r);
- } else if (expr.operator === "<=") {
- return handleConstOperation((l, r) => l <= r);
- } else if (expr.operator === ">=") {
- return handleConstOperation((l, r) => l >= r);
- }
- });
- this.hooks.evaluate.for("UnaryExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {UnaryExpression} */ (_expr);
- /**
- * Evaluates a UnaryExpression if and only if it is a basic const operator (e.g. +a, -a, ~a).
- * @template T
- * @param {(operand: T) => boolean | number | bigint | string} operandHandler handler for the operand
- * @returns {BasicEvaluatedExpression | undefined} evaluated expression
- */
- const handleConstOperation = operandHandler => {
- const argument = this.evaluateExpression(expr.argument);
- if (!argument.isCompileTimeValue()) return;
- const result = operandHandler(
- /** @type {T} */ (argument.asCompileTimeValue())
- );
- return valueAsExpression(result, expr, argument.couldHaveSideEffects());
- };
- if (expr.operator === "typeof") {
- switch (expr.argument.type) {
- case "Identifier": {
- const res = this.callHooksForName(
- this.hooks.evaluateTypeof,
- expr.argument.name,
- expr
- );
- if (res !== undefined) return res;
- break;
- }
- case "MetaProperty": {
- const res = this.callHooksForName(
- this.hooks.evaluateTypeof,
- /** @type {string} */
- (getRootName(expr.argument)),
- expr
- );
- if (res !== undefined) return res;
- break;
- }
- case "MemberExpression": {
- const res = this.callHooksForExpression(
- this.hooks.evaluateTypeof,
- expr.argument,
- expr
- );
- if (res !== undefined) return res;
- break;
- }
- case "ChainExpression": {
- const res = this.callHooksForExpression(
- this.hooks.evaluateTypeof,
- expr.argument.expression,
- expr
- );
- if (res !== undefined) return res;
- break;
- }
- case "FunctionExpression": {
- return new BasicEvaluatedExpression()
- .setString("function")
- .setRange(/** @type {Range} */ (expr.range));
- }
- }
- const arg = this.evaluateExpression(expr.argument);
- if (arg.isUnknown()) return;
- if (arg.isString()) {
- return new BasicEvaluatedExpression()
- .setString("string")
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (arg.isWrapped()) {
- return new BasicEvaluatedExpression()
- .setString("string")
- .setSideEffects()
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (arg.isUndefined()) {
- return new BasicEvaluatedExpression()
- .setString("undefined")
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (arg.isNumber()) {
- return new BasicEvaluatedExpression()
- .setString("number")
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (arg.isBigInt()) {
- return new BasicEvaluatedExpression()
- .setString("bigint")
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (arg.isBoolean()) {
- return new BasicEvaluatedExpression()
- .setString("boolean")
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (arg.isConstArray() || arg.isRegExp() || arg.isNull()) {
- return new BasicEvaluatedExpression()
- .setString("object")
- .setRange(/** @type {Range} */ (expr.range));
- }
- if (arg.isArray()) {
- return new BasicEvaluatedExpression()
- .setString("object")
- .setSideEffects(arg.couldHaveSideEffects())
- .setRange(/** @type {Range} */ (expr.range));
- }
- } else if (expr.operator === "!") {
- const argument = this.evaluateExpression(expr.argument);
- const bool = argument.asBool();
- if (typeof bool !== "boolean") return;
- return new BasicEvaluatedExpression()
- .setBoolean(!bool)
- .setSideEffects(argument.couldHaveSideEffects())
- .setRange(/** @type {Range} */ (expr.range));
- } else if (expr.operator === "~") {
- return handleConstOperation(v => ~v);
- } else if (expr.operator === "+") {
- // eslint-disable-next-line no-implicit-coercion
- return handleConstOperation(v => +v);
- } else if (expr.operator === "-") {
- return handleConstOperation(v => -v);
- }
- });
- this.hooks.evaluateTypeof
- .for("undefined")
- .tap(CLASS_NAME, expr =>
- new BasicEvaluatedExpression()
- .setString("undefined")
- .setRange(/** @type {Range} */ (expr.range))
- );
- this.hooks.evaluate.for("Identifier").tap(CLASS_NAME, expr => {
- if (/** @type {Identifier} */ (expr).name === "undefined") {
- return new BasicEvaluatedExpression()
- .setUndefined()
- .setRange(/** @type {Range} */ (expr.range));
- }
- });
- /**
- * @param {"Identifier" | "ThisExpression" | "MemberExpression"} exprType expression type name
- * @param {(node: Expression | SpreadElement) => GetInfoResult | undefined} getInfo get info
- * @returns {void}
- */
- const tapEvaluateWithVariableInfo = (exprType, getInfo) => {
- /** @type {Expression | undefined} */
- let cachedExpression;
- /** @type {GetInfoResult | undefined} */
- let cachedInfo;
- this.hooks.evaluate.for(exprType).tap(CLASS_NAME, expr => {
- const expression =
- /** @type {Identifier | ThisExpression | MemberExpression} */ (expr);
- const info = getInfo(expression);
- if (info !== undefined) {
- return this.callHooksForInfoWithFallback(
- this.hooks.evaluateIdentifier,
- info.name,
- name => {
- cachedExpression = expression;
- cachedInfo = info;
- },
- name => {
- const hook = this.hooks.evaluateDefinedIdentifier.get(name);
- if (hook !== undefined) {
- return hook.call(expression);
- }
- },
- expression
- );
- }
- });
- this.hooks.evaluate
- .for(exprType)
- .tap({ name: CLASS_NAME, stage: 100 }, expr => {
- const expression =
- /** @type {Identifier | ThisExpression | MemberExpression} */
- (expr);
- const info =
- cachedExpression === expression ? cachedInfo : getInfo(expression);
- if (info !== undefined) {
- return new BasicEvaluatedExpression()
- .setIdentifier(
- info.name,
- info.rootInfo,
- info.getMembers,
- info.getMembersOptionals,
- info.getMemberRanges
- )
- .setRange(/** @type {Range} */ (expression.range));
- }
- });
- this.hooks.finish.tap(CLASS_NAME, () => {
- // Cleanup for GC
- cachedExpression = cachedInfo = undefined;
- });
- };
- tapEvaluateWithVariableInfo("Identifier", expr => {
- const info = this.getVariableInfo(/** @type {Identifier} */ (expr).name);
- if (
- typeof info === "string" ||
- (info instanceof VariableInfo && typeof info.freeName === "string")
- ) {
- return {
- name: info,
- rootInfo: info,
- getMembers: () => [],
- getMembersOptionals: () => [],
- getMemberRanges: () => []
- };
- }
- });
- tapEvaluateWithVariableInfo("ThisExpression", expr => {
- const info = this.getVariableInfo("this");
- if (
- typeof info === "string" ||
- (info instanceof VariableInfo && typeof info.freeName === "string")
- ) {
- return {
- name: info,
- rootInfo: info,
- getMembers: () => [],
- getMembersOptionals: () => [],
- getMemberRanges: () => []
- };
- }
- });
- this.hooks.evaluate.for("MetaProperty").tap(CLASS_NAME, expr => {
- const metaProperty = /** @type {MetaProperty} */ (expr);
- return this.callHooksForName(
- this.hooks.evaluateIdentifier,
- /** @type {string} */
- (getRootName(metaProperty)),
- metaProperty
- );
- });
- tapEvaluateWithVariableInfo("MemberExpression", expr =>
- this.getMemberExpressionInfo(
- /** @type {MemberExpression} */ (expr),
- ALLOWED_MEMBER_TYPES_EXPRESSION
- )
- );
- this.hooks.evaluate.for("CallExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {CallExpression} */ (_expr);
- if (
- expr.callee.type === "MemberExpression" &&
- expr.callee.property.type ===
- (expr.callee.computed ? "Literal" : "Identifier")
- ) {
- // type Super also possible here
- const param = this.evaluateExpression(
- /** @type {Expression} */ (expr.callee.object)
- );
- const property =
- expr.callee.property.type === "Literal"
- ? `${expr.callee.property.value}`
- : expr.callee.property.name;
- const hook = this.hooks.evaluateCallExpressionMember.get(property);
- if (hook !== undefined) {
- return hook.call(expr, param);
- }
- } else if (expr.callee.type === "Identifier") {
- return this.callHooksForName(
- this.hooks.evaluateCallExpression,
- expr.callee.name,
- expr
- );
- }
- });
- this.hooks.evaluateCallExpressionMember
- .for("indexOf")
- .tap(CLASS_NAME, (expr, param) => {
- if (!param.isString()) return;
- if (expr.arguments.length === 0) return;
- const [arg1, arg2] = expr.arguments;
- if (arg1.type === "SpreadElement") return;
- const arg1Eval = this.evaluateExpression(arg1);
- if (!arg1Eval.isString()) return;
- const arg1Value = /** @type {string} */ (arg1Eval.string);
- let result;
- if (arg2) {
- if (arg2.type === "SpreadElement") return;
- const arg2Eval = this.evaluateExpression(arg2);
- if (!arg2Eval.isNumber()) return;
- result = /** @type {string} */ (param.string).indexOf(
- arg1Value,
- arg2Eval.number
- );
- } else {
- result = /** @type {string} */ (param.string).indexOf(arg1Value);
- }
- return new BasicEvaluatedExpression()
- .setNumber(result)
- .setSideEffects(param.couldHaveSideEffects())
- .setRange(/** @type {Range} */ (expr.range));
- });
- this.hooks.evaluateCallExpressionMember
- .for("replace")
- .tap(CLASS_NAME, (expr, param) => {
- if (!param.isString()) return;
- if (expr.arguments.length !== 2) return;
- if (expr.arguments[0].type === "SpreadElement") return;
- if (expr.arguments[1].type === "SpreadElement") return;
- const arg1 = this.evaluateExpression(expr.arguments[0]);
- const arg2 = this.evaluateExpression(expr.arguments[1]);
- if (!arg1.isString() && !arg1.isRegExp()) return;
- const arg1Value = /** @type {string | RegExp} */ (
- arg1.regExp || arg1.string
- );
- if (!arg2.isString()) return;
- const arg2Value = /** @type {string} */ (arg2.string);
- return new BasicEvaluatedExpression()
- .setString(
- /** @type {string} */ (param.string).replace(arg1Value, arg2Value)
- )
- .setSideEffects(param.couldHaveSideEffects())
- .setRange(/** @type {Range} */ (expr.range));
- });
- for (const fn of ["substr", "substring", "slice"]) {
- this.hooks.evaluateCallExpressionMember
- .for(fn)
- .tap(CLASS_NAME, (expr, param) => {
- if (!param.isString()) return;
- let arg1;
- let result;
- const str = /** @type {string} */ (param.string);
- switch (expr.arguments.length) {
- case 1:
- if (expr.arguments[0].type === "SpreadElement") return;
- arg1 = this.evaluateExpression(expr.arguments[0]);
- if (!arg1.isNumber()) return;
- result = str[
- /** @type {"substr" | "substring" | "slice"} */ (fn)
- ](/** @type {number} */ (arg1.number));
- break;
- case 2: {
- if (expr.arguments[0].type === "SpreadElement") return;
- if (expr.arguments[1].type === "SpreadElement") return;
- arg1 = this.evaluateExpression(expr.arguments[0]);
- const arg2 = this.evaluateExpression(expr.arguments[1]);
- if (!arg1.isNumber()) return;
- if (!arg2.isNumber()) return;
- result = str[
- /** @type {"substr" | "substring" | "slice"} */ (fn)
- ](
- /** @type {number} */ (arg1.number),
- /** @type {number} */ (arg2.number)
- );
- break;
- }
- default:
- return;
- }
- return new BasicEvaluatedExpression()
- .setString(result)
- .setSideEffects(param.couldHaveSideEffects())
- .setRange(/** @type {Range} */ (expr.range));
- });
- }
- /**
- * @param {"cooked" | "raw"} kind kind of values to get
- * @param {TemplateLiteral} templateLiteralExpr TemplateLiteral expr
- * @returns {{quasis: BasicEvaluatedExpression[], parts: BasicEvaluatedExpression[]}} Simplified template
- */
- const getSimplifiedTemplateResult = (kind, templateLiteralExpr) => {
- /** @type {BasicEvaluatedExpression[]} */
- const quasis = [];
- /** @type {BasicEvaluatedExpression[]} */
- const parts = [];
- for (let i = 0; i < templateLiteralExpr.quasis.length; i++) {
- const quasiExpr = templateLiteralExpr.quasis[i];
- const quasi = quasiExpr.value[kind];
- if (i > 0) {
- const prevExpr = parts[parts.length - 1];
- const expr = this.evaluateExpression(
- templateLiteralExpr.expressions[i - 1]
- );
- const exprAsString = expr.asString();
- if (
- typeof exprAsString === "string" &&
- !expr.couldHaveSideEffects()
- ) {
- // We can merge quasi + expr + quasi when expr
- // is a const string
- prevExpr.setString(prevExpr.string + exprAsString + quasi);
- prevExpr.setRange([
- /** @type {Range} */ (prevExpr.range)[0],
- /** @type {Range} */ (quasiExpr.range)[1]
- ]);
- // We unset the expression as it doesn't match to a single expression
- prevExpr.setExpression(undefined);
- continue;
- }
- parts.push(expr);
- }
- const part = new BasicEvaluatedExpression()
- .setString(/** @type {string} */ (quasi))
- .setRange(/** @type {Range} */ (quasiExpr.range))
- .setExpression(quasiExpr);
- quasis.push(part);
- parts.push(part);
- }
- return {
- quasis,
- parts
- };
- };
- this.hooks.evaluate.for("TemplateLiteral").tap(CLASS_NAME, _node => {
- const node = /** @type {TemplateLiteral} */ (_node);
- const { quasis, parts } = getSimplifiedTemplateResult("cooked", node);
- if (parts.length === 1) {
- return parts[0].setRange(/** @type {Range} */ (node.range));
- }
- return new BasicEvaluatedExpression()
- .setTemplateString(quasis, parts, "cooked")
- .setRange(/** @type {Range} */ (node.range));
- });
- this.hooks.evaluate
- .for("TaggedTemplateExpression")
- .tap(CLASS_NAME, _node => {
- const node = /** @type {TaggedTemplateExpression} */ (_node);
- const tag = this.evaluateExpression(node.tag);
- if (tag.isIdentifier() && tag.identifier === "String.raw") {
- const { quasis, parts } = getSimplifiedTemplateResult(
- "raw",
- node.quasi
- );
- return new BasicEvaluatedExpression()
- .setTemplateString(quasis, parts, "raw")
- .setRange(/** @type {Range} */ (node.range));
- }
- });
- this.hooks.evaluateCallExpressionMember
- .for("concat")
- .tap(CLASS_NAME, (expr, param) => {
- if (!param.isString() && !param.isWrapped()) return;
- let stringSuffix = null;
- let hasUnknownParams = false;
- const innerExpressions = [];
- for (let i = expr.arguments.length - 1; i >= 0; i--) {
- const arg = expr.arguments[i];
- if (arg.type === "SpreadElement") return;
- const argExpr = this.evaluateExpression(arg);
- if (
- hasUnknownParams ||
- (!argExpr.isString() && !argExpr.isNumber())
- ) {
- hasUnknownParams = true;
- innerExpressions.push(argExpr);
- continue;
- }
- const value = argExpr.isString()
- ? /** @type {string} */ (argExpr.string)
- : String(argExpr.number);
- /** @type {string} */
- const newString =
- value +
- (stringSuffix ? /** @type {string} */ (stringSuffix.string) : "");
- const newRange = /** @type {Range} */ ([
- /** @type {Range} */ (argExpr.range)[0],
- /** @type {Range} */ ((stringSuffix || argExpr).range)[1]
- ]);
- stringSuffix = new BasicEvaluatedExpression()
- .setString(newString)
- .setSideEffects(
- (stringSuffix && stringSuffix.couldHaveSideEffects()) ||
- argExpr.couldHaveSideEffects()
- )
- .setRange(newRange);
- }
- if (hasUnknownParams) {
- const prefix = param.isString() ? param : param.prefix;
- const inner =
- param.isWrapped() && param.wrappedInnerExpressions
- ? param.wrappedInnerExpressions.concat(innerExpressions.reverse())
- : innerExpressions.reverse();
- return new BasicEvaluatedExpression()
- .setWrapped(prefix, stringSuffix, inner)
- .setRange(/** @type {Range} */ (expr.range));
- } else if (param.isWrapped()) {
- const postfix = stringSuffix || param.postfix;
- const inner = param.wrappedInnerExpressions
- ? param.wrappedInnerExpressions.concat(innerExpressions.reverse())
- : innerExpressions.reverse();
- return new BasicEvaluatedExpression()
- .setWrapped(param.prefix, postfix, inner)
- .setRange(/** @type {Range} */ (expr.range));
- }
- const newString =
- /** @type {string} */ (param.string) +
- (stringSuffix ? stringSuffix.string : "");
- return new BasicEvaluatedExpression()
- .setString(newString)
- .setSideEffects(
- (stringSuffix && stringSuffix.couldHaveSideEffects()) ||
- param.couldHaveSideEffects()
- )
- .setRange(/** @type {Range} */ (expr.range));
- });
- this.hooks.evaluateCallExpressionMember
- .for("split")
- .tap(CLASS_NAME, (expr, param) => {
- if (!param.isString()) return;
- if (expr.arguments.length !== 1) return;
- if (expr.arguments[0].type === "SpreadElement") return;
- let result;
- const arg = this.evaluateExpression(expr.arguments[0]);
- if (arg.isString()) {
- result =
- /** @type {string} */
- (param.string).split(/** @type {string} */ (arg.string));
- } else if (arg.isRegExp()) {
- result = /** @type {string} */ (param.string).split(
- /** @type {RegExp} */ (arg.regExp)
- );
- } else {
- return;
- }
- return new BasicEvaluatedExpression()
- .setArray(result)
- .setSideEffects(param.couldHaveSideEffects())
- .setRange(/** @type {Range} */ (expr.range));
- });
- this.hooks.evaluate.for("ConditionalExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {ConditionalExpression} */ (_expr);
- const condition = this.evaluateExpression(expr.test);
- const conditionValue = condition.asBool();
- let res;
- if (conditionValue === undefined) {
- const consequent = this.evaluateExpression(expr.consequent);
- const alternate = this.evaluateExpression(expr.alternate);
- res = new BasicEvaluatedExpression();
- if (consequent.isConditional()) {
- res.setOptions(
- /** @type {BasicEvaluatedExpression[]} */ (consequent.options)
- );
- } else {
- res.setOptions([consequent]);
- }
- if (alternate.isConditional()) {
- res.addOptions(
- /** @type {BasicEvaluatedExpression[]} */ (alternate.options)
- );
- } else {
- res.addOptions([alternate]);
- }
- } else {
- res = this.evaluateExpression(
- conditionValue ? expr.consequent : expr.alternate
- );
- if (condition.couldHaveSideEffects()) res.setSideEffects();
- }
- res.setRange(/** @type {Range} */ (expr.range));
- return res;
- });
- this.hooks.evaluate.for("ArrayExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {ArrayExpression} */ (_expr);
- const items = expr.elements.map(
- element =>
- element !== null &&
- element.type !== "SpreadElement" &&
- this.evaluateExpression(element)
- );
- if (!items.every(Boolean)) return;
- return new BasicEvaluatedExpression()
- .setItems(/** @type {BasicEvaluatedExpression[]} */ (items))
- .setRange(/** @type {Range} */ (expr.range));
- });
- this.hooks.evaluate.for("ChainExpression").tap(CLASS_NAME, _expr => {
- const expr = /** @type {ChainExpression} */ (_expr);
- /** @type {Expression[]} */
- const optionalExpressionsStack = [];
- /** @type {Expression|Super} */
- let next = expr.expression;
- while (
- next.type === "MemberExpression" ||
- next.type === "CallExpression"
- ) {
- if (next.type === "MemberExpression") {
- if (next.optional) {
- // SuperNode can not be optional
- optionalExpressionsStack.push(
- /** @type {Expression} */ (next.object)
- );
- }
- next = next.object;
- } else {
- if (next.optional) {
- // SuperNode can not be optional
- optionalExpressionsStack.push(
- /** @type {Expression} */ (next.callee)
- );
- }
- next = next.callee;
- }
- }
- while (optionalExpressionsStack.length > 0) {
- const expression =
- /** @type {Expression} */
- (optionalExpressionsStack.pop());
- const evaluated = this.evaluateExpression(expression);
- if (evaluated.asNullish()) {
- return evaluated.setRange(/** @type {Range} */ (_expr.range));
- }
- }
- return this.evaluateExpression(expr.expression);
- });
- }
- /**
- * @param {Expression} node node
- * @returns {Set<DestructuringAssignmentProperty> | undefined} destructured identifiers
- */
- destructuringAssignmentPropertiesFor(node) {
- if (!this.destructuringAssignmentProperties) return;
- return this.destructuringAssignmentProperties.get(node);
- }
- /**
- * @param {Expression | SpreadElement} expr expression
- * @returns {string | VariableInfo | undefined} identifier
- */
- getRenameIdentifier(expr) {
- const result = this.evaluateExpression(expr);
- if (result.isIdentifier()) {
- return result.identifier;
- }
- }
- /**
- * @param {ClassExpression | ClassDeclaration | MaybeNamedClassDeclaration} classy a class node
- * @returns {void}
- */
- walkClass(classy) {
- if (
- classy.superClass &&
- !this.hooks.classExtendsExpression.call(classy.superClass, classy)
- ) {
- this.walkExpression(classy.superClass);
- }
- if (classy.body && classy.body.type === "ClassBody") {
- const scopeParams = [];
- // Add class name in scope for recursive calls
- if (classy.id) {
- scopeParams.push(classy.id);
- }
- this.inClassScope(true, scopeParams, () => {
- for (const classElement of /** @type {TODO} */ (classy.body.body)) {
- if (!this.hooks.classBodyElement.call(classElement, classy)) {
- if (classElement.computed && classElement.key) {
- this.walkExpression(classElement.key);
- }
- if (classElement.value) {
- if (
- !this.hooks.classBodyValue.call(
- classElement.value,
- classElement,
- classy
- )
- ) {
- const wasTopLevel = this.scope.topLevelScope;
- this.scope.topLevelScope = false;
- this.walkExpression(classElement.value);
- this.scope.topLevelScope = wasTopLevel;
- }
- } else if (classElement.type === "StaticBlock") {
- const wasTopLevel = this.scope.topLevelScope;
- this.scope.topLevelScope = false;
- this.walkBlockStatement(classElement);
- this.scope.topLevelScope = wasTopLevel;
- }
- }
- }
- });
- }
- }
- /**
- * Pre walking iterates the scope for variable declarations
- * @param {(Statement | ModuleDeclaration)[]} statements statements
- */
- preWalkStatements(statements) {
- for (let index = 0, len = statements.length; index < len; index++) {
- const statement = statements[index];
- this.preWalkStatement(statement);
- }
- }
- /**
- * Block pre walking iterates the scope for block variable declarations
- * @param {(Statement | ModuleDeclaration)[]} statements statements
- */
- blockPreWalkStatements(statements) {
- for (let index = 0, len = statements.length; index < len; index++) {
- const statement = statements[index];
- this.blockPreWalkStatement(statement);
- }
- }
- /**
- * Walking iterates the statements and expressions and processes them
- * @param {(Statement | ModuleDeclaration)[]} statements statements
- */
- walkStatements(statements) {
- let onlyFunctionDeclaration = false;
- for (let index = 0, len = statements.length; index < len; index++) {
- const statement = statements[index];
- if (onlyFunctionDeclaration && statement.type !== "FunctionDeclaration")
- continue;
- this.walkStatement(statement);
- if (this.scope.terminated) {
- onlyFunctionDeclaration = true;
- }
- }
- }
- /**
- * Walking iterates the statements and expressions and processes them
- * @param {Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration} statement statement
- */
- preWalkStatement(statement) {
- /** @type {StatementPath} */
- (this.statementPath).push(statement);
- if (this.hooks.preStatement.call(statement)) {
- this.prevStatement =
- /** @type {StatementPath} */
- (this.statementPath).pop();
- return;
- }
- switch (statement.type) {
- case "BlockStatement":
- this.preWalkBlockStatement(statement);
- break;
- case "DoWhileStatement":
- this.preWalkDoWhileStatement(statement);
- break;
- case "ForInStatement":
- this.preWalkForInStatement(statement);
- break;
- case "ForOfStatement":
- this.preWalkForOfStatement(statement);
- break;
- case "ForStatement":
- this.preWalkForStatement(statement);
- break;
- case "FunctionDeclaration":
- this.preWalkFunctionDeclaration(statement);
- break;
- case "IfStatement":
- this.preWalkIfStatement(statement);
- break;
- case "LabeledStatement":
- this.preWalkLabeledStatement(statement);
- break;
- case "SwitchStatement":
- this.preWalkSwitchStatement(statement);
- break;
- case "TryStatement":
- this.preWalkTryStatement(statement);
- break;
- case "VariableDeclaration":
- this.preWalkVariableDeclaration(statement);
- break;
- case "WhileStatement":
- this.preWalkWhileStatement(statement);
- break;
- case "WithStatement":
- this.preWalkWithStatement(statement);
- break;
- }
- this.prevStatement =
- /** @type {StatementPath} */
- (this.statementPath).pop();
- }
- /**
- * @param {Statement | ModuleDeclaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration} statement statement
- */
- blockPreWalkStatement(statement) {
- /** @type {StatementPath} */
- (this.statementPath).push(statement);
- if (this.hooks.blockPreStatement.call(statement)) {
- this.prevStatement =
- /** @type {StatementPath} */
- (this.statementPath).pop();
- return;
- }
- switch (statement.type) {
- case "ImportDeclaration":
- this.blockPreWalkImportDeclaration(statement);
- break;
- case "ExportAllDeclaration":
- this.blockPreWalkExportAllDeclaration(statement);
- break;
- case "ExportDefaultDeclaration":
- this.blockPreWalkExportDefaultDeclaration(statement);
- break;
- case "ExportNamedDeclaration":
- this.blockPreWalkExportNamedDeclaration(statement);
- break;
- case "VariableDeclaration":
- this.blockPreWalkVariableDeclaration(statement);
- break;
- case "ClassDeclaration":
- this.blockPreWalkClassDeclaration(statement);
- break;
- case "ExpressionStatement":
- this.blockPreWalkExpressionStatement(statement);
- }
- this.prevStatement =
- /** @type {StatementPath} */
- (this.statementPath).pop();
- }
- /**
- * @param {Statement | ModuleDeclaration | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} statement statement
- */
- walkStatement(statement) {
- /** @type {StatementPath} */
- (this.statementPath).push(statement);
- if (this.hooks.statement.call(statement) !== undefined) {
- this.prevStatement =
- /** @type {StatementPath} */
- (this.statementPath).pop();
- return;
- }
- switch (statement.type) {
- case "BlockStatement":
- this.walkBlockStatement(statement);
- break;
- case "ClassDeclaration":
- this.walkClassDeclaration(statement);
- break;
- case "DoWhileStatement":
- this.walkDoWhileStatement(statement);
- break;
- case "ExportDefaultDeclaration":
- this.walkExportDefaultDeclaration(statement);
- break;
- case "ExportNamedDeclaration":
- this.walkExportNamedDeclaration(statement);
- break;
- case "ExpressionStatement":
- this.walkExpressionStatement(statement);
- break;
- case "ForInStatement":
- this.walkForInStatement(statement);
- break;
- case "ForOfStatement":
- this.walkForOfStatement(statement);
- break;
- case "ForStatement":
- this.walkForStatement(statement);
- break;
- case "FunctionDeclaration":
- this.walkFunctionDeclaration(statement);
- break;
- case "IfStatement":
- this.walkIfStatement(statement);
- break;
- case "LabeledStatement":
- this.walkLabeledStatement(statement);
- break;
- case "ReturnStatement":
- this.walkReturnStatement(statement);
- break;
- case "SwitchStatement":
- this.walkSwitchStatement(statement);
- break;
- case "ThrowStatement":
- this.walkThrowStatement(statement);
- break;
- case "TryStatement":
- this.walkTryStatement(statement);
- break;
- case "VariableDeclaration":
- this.walkVariableDeclaration(statement);
- break;
- case "WhileStatement":
- this.walkWhileStatement(statement);
- break;
- case "WithStatement":
- this.walkWithStatement(statement);
- break;
- }
- this.prevStatement =
- /** @type {StatementPath} */
- (this.statementPath).pop();
- }
- /**
- * Walks a statements that is nested within a parent statement
- * and can potentially be a non-block statement.
- * This enforces the nested statement to never be in ASI position.
- * @param {Statement} statement the nested statement
- */
- walkNestedStatement(statement) {
- this.prevStatement = undefined;
- this.walkStatement(statement);
- }
- // Real Statements
- /**
- * @param {BlockStatement} statement block statement
- */
- preWalkBlockStatement(statement) {
- this.preWalkStatements(statement.body);
- }
- /**
- * @param {BlockStatement} statement block statement
- */
- walkBlockStatement(statement) {
- this.inBlockScope(() => {
- const body = statement.body;
- const prev = this.prevStatement;
- this.blockPreWalkStatements(body);
- this.prevStatement = prev;
- this.walkStatements(body);
- }, true);
- }
- /**
- * @param {ExpressionStatement} statement expression statement
- */
- walkExpressionStatement(statement) {
- this.walkExpression(statement.expression);
- }
- /**
- * @param {IfStatement} statement if statement
- */
- preWalkIfStatement(statement) {
- this.preWalkStatement(statement.consequent);
- if (statement.alternate) {
- this.preWalkStatement(statement.alternate);
- }
- }
- /**
- * @param {IfStatement} statement if statement
- */
- walkIfStatement(statement) {
- const result = this.hooks.statementIf.call(statement);
- if (result === undefined) {
- this.walkExpression(statement.test);
- this.walkNestedStatement(statement.consequent);
- const consequentTerminated = this.scope.terminated;
- this.scope.terminated = undefined;
- if (statement.alternate) {
- this.walkNestedStatement(statement.alternate);
- }
- const alternateTerminated = this.scope.terminated;
- this.scope.terminated =
- consequentTerminated && alternateTerminated
- ? alternateTerminated
- : undefined;
- } else if (result) {
- this.walkNestedStatement(statement.consequent);
- } else if (statement.alternate) {
- this.walkNestedStatement(statement.alternate);
- }
- }
- /**
- * @param {LabeledStatement} statement with statement
- */
- preWalkLabeledStatement(statement) {
- this.preWalkStatement(statement.body);
- }
- /**
- * @param {LabeledStatement} statement with statement
- */
- walkLabeledStatement(statement) {
- const hook = this.hooks.label.get(statement.label.name);
- if (hook !== undefined) {
- const result = hook.call(statement);
- if (result === true) return;
- }
- this.inBlockScope(() => {
- this.walkNestedStatement(statement.body);
- });
- }
- /**
- * @param {WithStatement} statement with statement
- */
- preWalkWithStatement(statement) {
- this.preWalkStatement(statement.body);
- }
- /**
- * @param {WithStatement} statement with statement
- */
- walkWithStatement(statement) {
- this.inBlockScope(() => {
- this.walkExpression(statement.object);
- this.walkNestedStatement(statement.body);
- });
- }
- /**
- * @param {SwitchStatement} statement switch statement
- */
- preWalkSwitchStatement(statement) {
- this.preWalkSwitchCases(statement.cases);
- }
- /**
- * @param {SwitchStatement} statement switch statement
- */
- walkSwitchStatement(statement) {
- this.walkExpression(statement.discriminant);
- this.walkSwitchCases(statement.cases);
- }
- /**
- * @param {ReturnStatement | ThrowStatement} statement return or throw statement
- */
- walkTerminatingStatement(statement) {
- if (statement.argument) this.walkExpression(statement.argument);
- // Skip top level scope because to handle `export` and `module.exports` after terminate
- if (this.scope.topLevelScope === true) return;
- if (this.hooks.terminate.call(statement)) {
- this.scope.terminated =
- statement.type === "ReturnStatement"
- ? SCOPE_INFO_TERMINATED_RETURN
- : SCOPE_INFO_TERMINATED_THROW;
- }
- }
- /**
- * @param {ReturnStatement} statement return statement
- */
- walkReturnStatement(statement) {
- this.walkTerminatingStatement(statement);
- }
- /**
- * @param {ThrowStatement} statement return statement
- */
- walkThrowStatement(statement) {
- this.walkTerminatingStatement(statement);
- }
- /**
- * @param {TryStatement} statement try statement
- */
- preWalkTryStatement(statement) {
- this.preWalkStatement(statement.block);
- if (statement.handler) this.preWalkCatchClause(statement.handler);
- if (statement.finalizer) this.preWalkStatement(statement.finalizer);
- }
- /**
- * @param {TryStatement} statement try statement
- */
- walkTryStatement(statement) {
- if (this.scope.inTry) {
- this.walkStatement(statement.block);
- } else {
- this.scope.inTry = true;
- this.walkStatement(statement.block);
- this.scope.inTry = false;
- }
- const tryTerminated = this.scope.terminated;
- this.scope.terminated = undefined;
- if (statement.handler) this.walkCatchClause(statement.handler);
- const handlerTerminated = this.scope.terminated;
- this.scope.terminated = undefined;
- if (statement.finalizer) {
- this.walkStatement(statement.finalizer);
- }
- const finalizerTerminated = this.scope.terminated;
- this.scope.terminated = undefined;
- if (finalizerTerminated) {
- this.scope.terminated = finalizerTerminated;
- } else if (
- tryTerminated &&
- (statement.handler ? handlerTerminated : true)
- ) {
- this.scope.terminated = handlerTerminated || tryTerminated;
- }
- }
- /**
- * @param {WhileStatement} statement while statement
- */
- preWalkWhileStatement(statement) {
- this.preWalkStatement(statement.body);
- }
- /**
- * @param {WhileStatement} statement while statement
- */
- walkWhileStatement(statement) {
- this.inBlockScope(() => {
- this.walkExpression(statement.test);
- this.walkNestedStatement(statement.body);
- });
- }
- /**
- * @param {DoWhileStatement} statement do while statement
- */
- preWalkDoWhileStatement(statement) {
- this.preWalkStatement(statement.body);
- }
- /**
- * @param {DoWhileStatement} statement do while statement
- */
- walkDoWhileStatement(statement) {
- this.inBlockScope(() => {
- this.walkNestedStatement(statement.body);
- this.walkExpression(statement.test);
- });
- }
- /**
- * @param {ForStatement} statement for statement
- */
- preWalkForStatement(statement) {
- if (statement.init && statement.init.type === "VariableDeclaration") {
- this.preWalkStatement(statement.init);
- }
- this.preWalkStatement(statement.body);
- }
- /**
- * @param {ForStatement} statement for statement
- */
- walkForStatement(statement) {
- this.inBlockScope(() => {
- if (statement.init) {
- if (statement.init.type === "VariableDeclaration") {
- this.blockPreWalkVariableDeclaration(statement.init);
- this.prevStatement = undefined;
- this.walkStatement(statement.init);
- } else {
- this.walkExpression(statement.init);
- }
- }
- if (statement.test) {
- this.walkExpression(statement.test);
- }
- if (statement.update) {
- this.walkExpression(statement.update);
- }
- const body = statement.body;
- if (body.type === "BlockStatement") {
- // no need to add additional scope
- const prev = this.prevStatement;
- this.blockPreWalkStatements(body.body);
- this.prevStatement = prev;
- this.walkStatements(body.body);
- } else {
- this.walkNestedStatement(body);
- }
- });
- }
- /**
- * @param {ForInStatement} statement for statement
- */
- preWalkForInStatement(statement) {
- if (statement.left.type === "VariableDeclaration") {
- this.preWalkVariableDeclaration(statement.left);
- }
- this.preWalkStatement(statement.body);
- }
- /**
- * @param {ForInStatement} statement for statement
- */
- walkForInStatement(statement) {
- this.inBlockScope(() => {
- if (statement.left.type === "VariableDeclaration") {
- this.blockPreWalkVariableDeclaration(statement.left);
- this.walkVariableDeclaration(statement.left);
- } else {
- this.walkPattern(statement.left);
- }
- this.walkExpression(statement.right);
- const body = statement.body;
- if (body.type === "BlockStatement") {
- // no need to add additional scope
- const prev = this.prevStatement;
- this.blockPreWalkStatements(body.body);
- this.prevStatement = prev;
- this.walkStatements(body.body);
- } else {
- this.walkNestedStatement(body);
- }
- });
- }
- /**
- * @param {ForOfStatement} statement statement
- */
- preWalkForOfStatement(statement) {
- if (statement.await && this.scope.topLevelScope === true) {
- this.hooks.topLevelAwait.call(statement);
- }
- if (statement.left.type === "VariableDeclaration") {
- this.preWalkVariableDeclaration(statement.left);
- }
- this.preWalkStatement(statement.body);
- }
- /**
- * @param {ForOfStatement} statement for statement
- */
- walkForOfStatement(statement) {
- this.inBlockScope(() => {
- if (statement.left.type === "VariableDeclaration") {
- this.blockPreWalkVariableDeclaration(statement.left);
- this.walkVariableDeclaration(statement.left);
- } else {
- this.walkPattern(statement.left);
- }
- this.walkExpression(statement.right);
- const body = statement.body;
- if (body.type === "BlockStatement") {
- // no need to add additional scope
- const prev = this.prevStatement;
- this.blockPreWalkStatements(body.body);
- this.prevStatement = prev;
- this.walkStatements(body.body);
- } else {
- this.walkNestedStatement(body);
- }
- });
- }
- /**
- * @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
- */
- preWalkFunctionDeclaration(statement) {
- if (statement.id) {
- this.defineVariable(statement.id.name);
- }
- }
- /**
- * @param {FunctionDeclaration | MaybeNamedFunctionDeclaration} statement function declaration
- */
- walkFunctionDeclaration(statement) {
- const wasTopLevel = this.scope.topLevelScope;
- this.scope.topLevelScope = false;
- this.inFunctionScope(true, statement.params, () => {
- for (const param of statement.params) {
- this.walkPattern(param);
- }
- this.detectMode(statement.body.body);
- const prev = this.prevStatement;
- this.preWalkStatement(statement.body);
- this.prevStatement = prev;
- this.walkStatement(statement.body);
- });
- this.scope.topLevelScope = wasTopLevel;
- }
- /**
- * @param {ExpressionStatement} statement expression statement
- */
- blockPreWalkExpressionStatement(statement) {
- const expression = statement.expression;
- switch (expression.type) {
- case "AssignmentExpression":
- this.preWalkAssignmentExpression(expression);
- }
- }
- /**
- * @param {AssignmentExpression} expression assignment expression
- */
- preWalkAssignmentExpression(expression) {
- if (
- expression.left.type !== "ObjectPattern" ||
- !this.destructuringAssignmentProperties
- )
- return;
- const keys = this._preWalkObjectPattern(expression.left);
- if (!keys) return;
- // check multiple assignments
- if (this.destructuringAssignmentProperties.has(expression)) {
- const set =
- /** @type {Set<DestructuringAssignmentProperty>} */
- (this.destructuringAssignmentProperties.get(expression));
- this.destructuringAssignmentProperties.delete(expression);
- for (const id of set) keys.add(id);
- }
- this.destructuringAssignmentProperties.set(
- expression.right.type === "AwaitExpression"
- ? expression.right.argument
- : expression.right,
- keys
- );
- if (expression.right.type === "AssignmentExpression") {
- this.preWalkAssignmentExpression(expression.right);
- }
- }
- /**
- * @param {ImportDeclaration} statement statement
- */
- blockPreWalkImportDeclaration(statement) {
- const source = /** @type {ImportSource} */ (statement.source.value);
- this.hooks.import.call(statement, source);
- for (const specifier of statement.specifiers) {
- const name = specifier.local.name;
- switch (specifier.type) {
- case "ImportDefaultSpecifier":
- if (
- !this.hooks.importSpecifier.call(statement, source, "default", name)
- ) {
- this.defineVariable(name);
- }
- break;
- case "ImportSpecifier":
- if (
- !this.hooks.importSpecifier.call(
- statement,
- source,
- /** @type {Identifier} */
- (specifier.imported).name ||
- /** @type {string} */
- (
- /** @type {Literal} */
- (specifier.imported).value
- ),
- name
- )
- ) {
- this.defineVariable(name);
- }
- break;
- case "ImportNamespaceSpecifier":
- if (!this.hooks.importSpecifier.call(statement, source, null, name)) {
- this.defineVariable(name);
- }
- break;
- default:
- this.defineVariable(name);
- }
- }
- }
- /**
- * @param {Declaration} declaration declaration
- * @param {OnIdent} onIdent on ident callback
- */
- enterDeclaration(declaration, onIdent) {
- switch (declaration.type) {
- case "VariableDeclaration":
- for (const declarator of declaration.declarations) {
- switch (declarator.type) {
- case "VariableDeclarator": {
- this.enterPattern(declarator.id, onIdent);
- break;
- }
- }
- }
- break;
- case "FunctionDeclaration":
- this.enterPattern(declaration.id, onIdent);
- break;
- case "ClassDeclaration":
- this.enterPattern(declaration.id, onIdent);
- break;
- }
- }
- /**
- * @param {ExportNamedDeclaration} statement statement
- */
- blockPreWalkExportNamedDeclaration(statement) {
- let source;
- if (statement.source) {
- source = /** @type {ImportSource} */ (statement.source.value);
- this.hooks.exportImport.call(statement, source);
- } else {
- this.hooks.export.call(statement);
- }
- if (
- statement.declaration &&
- !this.hooks.exportDeclaration.call(statement, statement.declaration)
- ) {
- const prev = this.prevStatement;
- this.preWalkStatement(statement.declaration);
- this.prevStatement = prev;
- this.blockPreWalkStatement(statement.declaration);
- let index = 0;
- this.enterDeclaration(statement.declaration, def => {
- this.hooks.exportSpecifier.call(statement, def, def, index++);
- });
- }
- if (statement.specifiers) {
- for (
- let specifierIndex = 0;
- specifierIndex < statement.specifiers.length;
- specifierIndex++
- ) {
- const specifier = statement.specifiers[specifierIndex];
- switch (specifier.type) {
- case "ExportSpecifier": {
- const localName =
- /** @type {Identifier} */ (specifier.local).name ||
- /** @type {string} */ (
- /** @type {Literal} */ (specifier.local).value
- );
- const name =
- /** @type {Identifier} */
- (specifier.exported).name ||
- /** @type {string} */
- (/** @type {Literal} */ (specifier.exported).value);
- if (source) {
- this.hooks.exportImportSpecifier.call(
- statement,
- source,
- localName,
- name,
- specifierIndex
- );
- } else {
- this.hooks.exportSpecifier.call(
- statement,
- localName,
- name,
- specifierIndex
- );
- }
- break;
- }
- }
- }
- }
- }
- /**
- * @param {ExportNamedDeclaration} statement the statement
- */
- walkExportNamedDeclaration(statement) {
- if (statement.declaration) {
- this.walkStatement(statement.declaration);
- }
- }
- /**
- * @param {ExportDefaultDeclaration} statement statement
- */
- blockPreWalkExportDefaultDeclaration(statement) {
- const prev = this.prevStatement;
- this.preWalkStatement(/** @type {TODO} */ (statement.declaration));
- this.prevStatement = prev;
- this.blockPreWalkStatement(/** @type {TODO} */ (statement.declaration));
- if (
- /** @type {MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} */
- (statement.declaration).id &&
- statement.declaration.type !== "FunctionExpression" &&
- statement.declaration.type !== "ClassExpression"
- ) {
- const declaration =
- /** @type {MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration} */
- (statement.declaration);
- this.hooks.exportSpecifier.call(
- statement,
- /** @type {Identifier} */
- (declaration.id).name,
- "default",
- undefined
- );
- }
- }
- /**
- * @param {ExportDefaultDeclaration} statement statement
- */
- walkExportDefaultDeclaration(statement) {
- this.hooks.export.call(statement);
- if (
- /** @type {FunctionDeclaration | ClassDeclaration} */
- (statement.declaration).id &&
- statement.declaration.type !== "FunctionExpression" &&
- statement.declaration.type !== "ClassExpression"
- ) {
- const declaration =
- /** @type {FunctionDeclaration | ClassDeclaration} */
- (statement.declaration);
- if (!this.hooks.exportDeclaration.call(statement, declaration)) {
- this.walkStatement(declaration);
- }
- } else {
- // Acorn parses `export default function() {}` as `FunctionDeclaration` and
- // `export default class {}` as `ClassDeclaration`, both with `id = null`.
- // These nodes must be treated as expressions.
- if (
- statement.declaration.type === "FunctionDeclaration" ||
- statement.declaration.type === "ClassDeclaration"
- ) {
- this.walkStatement(statement.declaration);
- } else {
- this.walkExpression(statement.declaration);
- }
- if (!this.hooks.exportExpression.call(statement, statement.declaration)) {
- this.hooks.exportSpecifier.call(
- statement,
- /** @type {TODO} */
- (statement.declaration),
- "default",
- undefined
- );
- }
- }
- }
- /**
- * @param {ExportAllDeclaration} statement statement
- */
- blockPreWalkExportAllDeclaration(statement) {
- const source = /** @type {ImportSource} */ (statement.source.value);
- const name = statement.exported
- ? /** @type {Identifier} */
- (statement.exported).name ||
- /** @type {string} */
- (/** @type {Literal} */ (statement.exported).value)
- : null;
- this.hooks.exportImport.call(statement, source);
- this.hooks.exportImportSpecifier.call(statement, source, null, name, 0);
- }
- /**
- * @param {VariableDeclaration} statement variable declaration
- */
- preWalkVariableDeclaration(statement) {
- if (statement.kind !== "var") return;
- this._preWalkVariableDeclaration(statement, this.hooks.varDeclarationVar);
- }
- /**
- * @param {VariableDeclaration} statement variable declaration
- */
- blockPreWalkVariableDeclaration(statement) {
- if (statement.kind === "var") return;
- const hookMap =
- statement.kind === "const"
- ? this.hooks.varDeclarationConst
- : this.hooks.varDeclarationLet;
- this._preWalkVariableDeclaration(statement, hookMap);
- }
- /**
- * @param {VariableDeclaration} statement variable declaration
- * @param {TODO} hookMap map of hooks
- */
- _preWalkVariableDeclaration(statement, hookMap) {
- for (const declarator of statement.declarations) {
- switch (declarator.type) {
- case "VariableDeclarator": {
- this.preWalkVariableDeclarator(declarator);
- if (!this.hooks.preDeclarator.call(declarator, statement)) {
- this.enterPattern(declarator.id, (name, decl) => {
- let hook = hookMap.get(name);
- if (hook === undefined || !hook.call(decl)) {
- hook = this.hooks.varDeclaration.get(name);
- if (hook === undefined || !hook.call(decl)) {
- this.defineVariable(name);
- }
- }
- });
- }
- break;
- }
- }
- }
- }
- /**
- * @param {ObjectPattern} objectPattern object pattern
- * @returns {Set<DestructuringAssignmentProperty> | undefined} set of names or undefined if not all keys are identifiers
- */
- _preWalkObjectPattern(objectPattern) {
- /** @type {Set<DestructuringAssignmentProperty>} */
- const props = new Set();
- const properties = objectPattern.properties;
- for (let i = 0; i < properties.length; i++) {
- const property = properties[i];
- if (property.type !== "Property") return;
- if (property.shorthand && property.value.type === "Identifier") {
- this.scope.inShorthand = property.value.name;
- }
- const key = property.key;
- if (key.type === "Identifier") {
- props.add({
- id: key.name,
- range: key.range,
- shorthand: this.scope.inShorthand
- });
- } else {
- const id = this.evaluateExpression(key);
- const str = id.asString();
- if (str) {
- props.add({
- id: str,
- range: key.range,
- shorthand: this.scope.inShorthand
- });
- } else {
- // could not evaluate key
- return;
- }
- }
- this.scope.inShorthand = false;
- }
- return props;
- }
- /**
- * @param {VariableDeclarator} declarator variable declarator
- */
- preWalkVariableDeclarator(declarator) {
- if (
- !declarator.init ||
- declarator.id.type !== "ObjectPattern" ||
- !this.destructuringAssignmentProperties
- )
- return;
- const keys = this._preWalkObjectPattern(declarator.id);
- if (!keys) return;
- this.destructuringAssignmentProperties.set(
- declarator.init.type === "AwaitExpression"
- ? declarator.init.argument
- : declarator.init,
- keys
- );
- if (declarator.init.type === "AssignmentExpression") {
- this.preWalkAssignmentExpression(declarator.init);
- }
- }
- /**
- * @param {VariableDeclaration} statement variable declaration
- */
- walkVariableDeclaration(statement) {
- for (const declarator of statement.declarations) {
- switch (declarator.type) {
- case "VariableDeclarator": {
- const renameIdentifier =
- declarator.init && this.getRenameIdentifier(declarator.init);
- if (renameIdentifier && declarator.id.type === "Identifier") {
- const hook = this.hooks.canRename.get(renameIdentifier);
- if (
- hook !== undefined &&
- hook.call(/** @type {Expression} */ (declarator.init))
- ) {
- // renaming with "var a = b;"
- const hook = this.hooks.rename.get(renameIdentifier);
- if (
- hook === undefined ||
- !hook.call(/** @type {Expression} */ (declarator.init))
- ) {
- this.setVariable(declarator.id.name, renameIdentifier);
- }
- break;
- }
- }
- if (!this.hooks.declarator.call(declarator, statement)) {
- this.walkPattern(declarator.id);
- if (declarator.init) this.walkExpression(declarator.init);
- }
- break;
- }
- }
- }
- }
- /**
- * @param {ClassDeclaration | MaybeNamedClassDeclaration} statement class declaration
- */
- blockPreWalkClassDeclaration(statement) {
- if (statement.id) {
- this.defineVariable(statement.id.name);
- }
- }
- /**
- * @param {ClassDeclaration | MaybeNamedClassDeclaration} statement class declaration
- */
- walkClassDeclaration(statement) {
- this.walkClass(statement);
- }
- /**
- * @param {SwitchCase[]} switchCases switch statement
- */
- preWalkSwitchCases(switchCases) {
- for (let index = 0, len = switchCases.length; index < len; index++) {
- const switchCase = switchCases[index];
- this.preWalkStatements(switchCase.consequent);
- }
- }
- /**
- * @param {SwitchCase[]} switchCases switch statement
- */
- walkSwitchCases(switchCases) {
- this.inBlockScope(() => {
- const len = switchCases.length;
- // we need to pre walk all statements first since we can have invalid code
- // import A from "module";
- // switch(1) {
- // case 1:
- // console.log(A); // should fail at runtime
- // case 2:
- // const A = 1;
- // }
- for (let index = 0; index < len; index++) {
- const switchCase = switchCases[index];
- if (switchCase.consequent.length > 0) {
- const prev = this.prevStatement;
- this.blockPreWalkStatements(switchCase.consequent);
- this.prevStatement = prev;
- }
- }
- for (let index = 0; index < len; index++) {
- const switchCase = switchCases[index];
- if (switchCase.test) {
- this.walkExpression(switchCase.test);
- }
- if (switchCase.consequent.length > 0) {
- this.walkStatements(switchCase.consequent);
- this.scope.terminated = undefined;
- }
- }
- });
- }
- /**
- * @param {CatchClause} catchClause catch clause
- */
- preWalkCatchClause(catchClause) {
- this.preWalkStatement(catchClause.body);
- }
- /**
- * @param {CatchClause} catchClause catch clause
- */
- walkCatchClause(catchClause) {
- this.inBlockScope(() => {
- // Error binding is optional in catch clause since ECMAScript 2019
- if (catchClause.param !== null) {
- this.enterPattern(catchClause.param, ident => {
- this.defineVariable(ident);
- });
- this.walkPattern(catchClause.param);
- }
- const prev = this.prevStatement;
- this.blockPreWalkStatement(catchClause.body);
- this.prevStatement = prev;
- this.walkStatement(catchClause.body);
- }, true);
- }
- /**
- * @param {Pattern} pattern pattern
- */
- walkPattern(pattern) {
- switch (pattern.type) {
- case "ArrayPattern":
- this.walkArrayPattern(pattern);
- break;
- case "AssignmentPattern":
- this.walkAssignmentPattern(pattern);
- break;
- case "MemberExpression":
- this.walkMemberExpression(pattern);
- break;
- case "ObjectPattern":
- this.walkObjectPattern(pattern);
- break;
- case "RestElement":
- this.walkRestElement(pattern);
- break;
- }
- }
- /**
- * @param {AssignmentPattern} pattern assignment pattern
- */
- walkAssignmentPattern(pattern) {
- this.walkExpression(pattern.right);
- this.walkPattern(pattern.left);
- }
- /**
- * @param {ObjectPattern} pattern pattern
- */
- walkObjectPattern(pattern) {
- for (let i = 0, len = pattern.properties.length; i < len; i++) {
- const prop = pattern.properties[i];
- if (prop) {
- if (prop.type === "RestElement") {
- continue;
- }
- if (prop.computed) this.walkExpression(prop.key);
- if (prop.value) this.walkPattern(prop.value);
- }
- }
- }
- /**
- * @param {ArrayPattern} pattern array pattern
- */
- walkArrayPattern(pattern) {
- for (let i = 0, len = pattern.elements.length; i < len; i++) {
- const element = pattern.elements[i];
- if (element) this.walkPattern(element);
- }
- }
- /**
- * @param {RestElement} pattern rest element
- */
- walkRestElement(pattern) {
- this.walkPattern(pattern.argument);
- }
- /**
- * @param {(Expression | SpreadElement | null)[]} expressions expressions
- */
- walkExpressions(expressions) {
- for (const expression of expressions) {
- if (expression) {
- this.walkExpression(expression);
- }
- }
- }
- /**
- * @param {Expression | SpreadElement | PrivateIdentifier | Super} expression expression
- */
- walkExpression(expression) {
- switch (expression.type) {
- case "ArrayExpression":
- this.walkArrayExpression(expression);
- break;
- case "ArrowFunctionExpression":
- this.walkArrowFunctionExpression(expression);
- break;
- case "AssignmentExpression":
- this.walkAssignmentExpression(expression);
- break;
- case "AwaitExpression":
- this.walkAwaitExpression(expression);
- break;
- case "BinaryExpression":
- this.walkBinaryExpression(expression);
- break;
- case "CallExpression":
- this.walkCallExpression(expression);
- break;
- case "ChainExpression":
- this.walkChainExpression(expression);
- break;
- case "ClassExpression":
- this.walkClassExpression(expression);
- break;
- case "ConditionalExpression":
- this.walkConditionalExpression(expression);
- break;
- case "FunctionExpression":
- this.walkFunctionExpression(expression);
- break;
- case "Identifier":
- this.walkIdentifier(expression);
- break;
- case "ImportExpression":
- this.walkImportExpression(expression);
- break;
- case "LogicalExpression":
- this.walkLogicalExpression(expression);
- break;
- case "MetaProperty":
- this.walkMetaProperty(expression);
- break;
- case "MemberExpression":
- this.walkMemberExpression(expression);
- break;
- case "NewExpression":
- this.walkNewExpression(expression);
- break;
- case "ObjectExpression":
- this.walkObjectExpression(expression);
- break;
- case "SequenceExpression":
- this.walkSequenceExpression(expression);
- break;
- case "SpreadElement":
- this.walkSpreadElement(expression);
- break;
- case "TaggedTemplateExpression":
- this.walkTaggedTemplateExpression(expression);
- break;
- case "TemplateLiteral":
- this.walkTemplateLiteral(expression);
- break;
- case "ThisExpression":
- this.walkThisExpression(expression);
- break;
- case "UnaryExpression":
- this.walkUnaryExpression(expression);
- break;
- case "UpdateExpression":
- this.walkUpdateExpression(expression);
- break;
- case "YieldExpression":
- this.walkYieldExpression(expression);
- break;
- }
- }
- /**
- * @param {AwaitExpression} expression await expression
- */
- walkAwaitExpression(expression) {
- if (this.scope.topLevelScope === true)
- this.hooks.topLevelAwait.call(expression);
- this.walkExpression(expression.argument);
- }
- /**
- * @param {ArrayExpression} expression array expression
- */
- walkArrayExpression(expression) {
- if (expression.elements) {
- this.walkExpressions(expression.elements);
- }
- }
- /**
- * @param {SpreadElement} expression spread element
- */
- walkSpreadElement(expression) {
- if (expression.argument) {
- this.walkExpression(expression.argument);
- }
- }
- /**
- * @param {ObjectExpression} expression object expression
- */
- walkObjectExpression(expression) {
- for (
- let propIndex = 0, len = expression.properties.length;
- propIndex < len;
- propIndex++
- ) {
- const prop = expression.properties[propIndex];
- this.walkProperty(prop);
- }
- }
- /**
- * @param {Property | SpreadElement} prop property or spread element
- */
- walkProperty(prop) {
- if (prop.type === "SpreadElement") {
- this.walkExpression(prop.argument);
- return;
- }
- if (prop.computed) {
- this.walkExpression(prop.key);
- }
- if (prop.shorthand && prop.value && prop.value.type === "Identifier") {
- this.scope.inShorthand = prop.value.name;
- this.walkIdentifier(prop.value);
- this.scope.inShorthand = false;
- } else {
- this.walkExpression(
- /** @type {Exclude<Property["value"], AssignmentPattern | ObjectPattern | ArrayPattern | RestElement>} */
- (prop.value)
- );
- }
- }
- /**
- * @param {FunctionExpression} expression arrow function expression
- */
- walkFunctionExpression(expression) {
- const wasTopLevel = this.scope.topLevelScope;
- this.scope.topLevelScope = false;
- const scopeParams = [...expression.params];
- // Add function name in scope for recursive calls
- if (expression.id) {
- scopeParams.push(expression.id);
- }
- this.inFunctionScope(true, scopeParams, () => {
- for (const param of expression.params) {
- this.walkPattern(param);
- }
- this.detectMode(expression.body.body);
- const prev = this.prevStatement;
- this.preWalkStatement(expression.body);
- this.prevStatement = prev;
- this.walkStatement(expression.body);
- });
- this.scope.topLevelScope = wasTopLevel;
- }
- /**
- * @param {ArrowFunctionExpression} expression arrow function expression
- */
- walkArrowFunctionExpression(expression) {
- const wasTopLevel = this.scope.topLevelScope;
- this.scope.topLevelScope = wasTopLevel ? "arrow" : false;
- this.inFunctionScope(false, expression.params, () => {
- for (const param of expression.params) {
- this.walkPattern(param);
- }
- if (expression.body.type === "BlockStatement") {
- this.detectMode(expression.body.body);
- const prev = this.prevStatement;
- this.preWalkStatement(expression.body);
- this.prevStatement = prev;
- this.walkStatement(expression.body);
- } else {
- this.walkExpression(expression.body);
- }
- });
- this.scope.topLevelScope = wasTopLevel;
- }
- /**
- * @param {SequenceExpression} expression the sequence
- */
- walkSequenceExpression(expression) {
- if (!expression.expressions) return;
- // We treat sequence expressions like statements when they are one statement level
- // This has some benefits for optimizations that only work on statement level
- const currentStatement =
- /** @type {StatementPath} */
- (this.statementPath)[
- /** @type {StatementPath} */
- (this.statementPath).length - 1
- ];
- if (
- currentStatement === expression ||
- (currentStatement.type === "ExpressionStatement" &&
- currentStatement.expression === expression)
- ) {
- const old =
- /** @type {StatementPathItem} */
- (/** @type {StatementPath} */ (this.statementPath).pop());
- const prev = this.prevStatement;
- for (const expr of expression.expressions) {
- /** @type {StatementPath} */
- (this.statementPath).push(expr);
- this.walkExpression(expr);
- this.prevStatement =
- /** @type {StatementPath} */
- (this.statementPath).pop();
- }
- this.prevStatement = prev;
- /** @type {StatementPath} */
- (this.statementPath).push(old);
- } else {
- this.walkExpressions(expression.expressions);
- }
- }
- /**
- * @param {UpdateExpression} expression the update expression
- */
- walkUpdateExpression(expression) {
- this.walkExpression(expression.argument);
- }
- /**
- * @param {UnaryExpression} expression the unary expression
- */
- walkUnaryExpression(expression) {
- if (expression.operator === "typeof") {
- const result = this.callHooksForExpression(
- this.hooks.typeof,
- expression.argument,
- expression
- );
- if (result === true) return;
- if (expression.argument.type === "ChainExpression") {
- const result = this.callHooksForExpression(
- this.hooks.typeof,
- expression.argument.expression,
- expression
- );
- if (result === true) return;
- }
- }
- this.walkExpression(expression.argument);
- }
- /**
- * @param {LogicalExpression | BinaryExpression} expression the expression
- */
- walkLeftRightExpression(expression) {
- this.walkExpression(expression.left);
- this.walkExpression(expression.right);
- }
- /**
- * @param {BinaryExpression} expression the binary expression
- */
- walkBinaryExpression(expression) {
- if (this.hooks.binaryExpression.call(expression) === undefined) {
- this.walkLeftRightExpression(expression);
- }
- }
- /**
- * @param {LogicalExpression} expression the logical expression
- */
- walkLogicalExpression(expression) {
- const result = this.hooks.expressionLogicalOperator.call(expression);
- if (result === undefined) {
- this.walkLeftRightExpression(expression);
- } else if (result) {
- this.walkExpression(expression.right);
- }
- }
- /**
- * @param {AssignmentExpression} expression assignment expression
- */
- walkAssignmentExpression(expression) {
- if (expression.left.type === "Identifier") {
- const renameIdentifier = this.getRenameIdentifier(expression.right);
- if (
- renameIdentifier &&
- this.callHooksForInfo(
- this.hooks.canRename,
- renameIdentifier,
- expression.right
- )
- ) {
- // renaming "a = b;"
- if (
- !this.callHooksForInfo(
- this.hooks.rename,
- renameIdentifier,
- expression.right
- )
- ) {
- this.setVariable(
- expression.left.name,
- typeof renameIdentifier === "string"
- ? this.getVariableInfo(renameIdentifier)
- : renameIdentifier
- );
- }
- return;
- }
- this.walkExpression(expression.right);
- this.enterPattern(expression.left, (name, decl) => {
- if (!this.callHooksForName(this.hooks.assign, name, expression)) {
- this.walkExpression(
- /** @type {MemberExpression} */
- (expression.left)
- );
- }
- });
- } else if (expression.left.type.endsWith("Pattern")) {
- this.walkExpression(expression.right);
- this.enterPattern(expression.left, (name, decl) => {
- if (!this.callHooksForName(this.hooks.assign, name, expression)) {
- this.defineVariable(name);
- }
- });
- this.walkPattern(expression.left);
- } else if (expression.left.type === "MemberExpression") {
- const exprName = this.getMemberExpressionInfo(
- expression.left,
- ALLOWED_MEMBER_TYPES_EXPRESSION
- );
- if (
- exprName &&
- this.callHooksForInfo(
- this.hooks.assignMemberChain,
- exprName.rootInfo,
- expression,
- exprName.getMembers()
- )
- ) {
- return;
- }
- this.walkExpression(expression.right);
- this.walkExpression(expression.left);
- } else {
- this.walkExpression(expression.right);
- this.walkExpression(
- /** @type {Exclude<AssignmentExpression["left"], Identifier | RestElement | MemberExpression | ObjectPattern | ArrayPattern | AssignmentPattern>} */
- (expression.left)
- );
- }
- }
- /**
- * @param {ConditionalExpression} expression conditional expression
- */
- walkConditionalExpression(expression) {
- const result = this.hooks.expressionConditionalOperator.call(expression);
- if (result === undefined) {
- this.walkExpression(expression.test);
- this.walkExpression(expression.consequent);
- if (expression.alternate) {
- this.walkExpression(expression.alternate);
- }
- } else if (result) {
- this.walkExpression(expression.consequent);
- } else if (expression.alternate) {
- this.walkExpression(expression.alternate);
- }
- }
- /**
- * @param {NewExpression} expression new expression
- */
- walkNewExpression(expression) {
- const result = this.callHooksForExpression(
- this.hooks.new,
- expression.callee,
- expression
- );
- if (result === true) return;
- this.walkExpression(expression.callee);
- if (expression.arguments) {
- this.walkExpressions(expression.arguments);
- }
- }
- /**
- * @param {YieldExpression} expression yield expression
- */
- walkYieldExpression(expression) {
- if (expression.argument) {
- this.walkExpression(expression.argument);
- }
- }
- /**
- * @param {TemplateLiteral} expression template literal
- */
- walkTemplateLiteral(expression) {
- if (expression.expressions) {
- this.walkExpressions(expression.expressions);
- }
- }
- /**
- * @param {TaggedTemplateExpression} expression tagged template expression
- */
- walkTaggedTemplateExpression(expression) {
- if (expression.tag) {
- this.scope.inTaggedTemplateTag = true;
- this.walkExpression(expression.tag);
- this.scope.inTaggedTemplateTag = false;
- }
- if (expression.quasi && expression.quasi.expressions) {
- this.walkExpressions(expression.quasi.expressions);
- }
- }
- /**
- * @param {ClassExpression} expression the class expression
- */
- walkClassExpression(expression) {
- this.walkClass(expression);
- }
- /**
- * @param {ChainExpression} expression expression
- */
- walkChainExpression(expression) {
- const result = this.hooks.optionalChaining.call(expression);
- if (result === undefined) {
- if (expression.expression.type === "CallExpression") {
- this.walkCallExpression(expression.expression);
- } else {
- this.walkMemberExpression(expression.expression);
- }
- }
- }
- /**
- * @private
- * @param {FunctionExpression | ArrowFunctionExpression} functionExpression function expression
- * @param {(Expression | SpreadElement)[]} options options
- * @param {Expression | SpreadElement | null} currentThis current this
- */
- _walkIIFE(functionExpression, options, currentThis) {
- /**
- * @param {Expression | SpreadElement} argOrThis arg or this
- * @returns {string | VariableInfo | undefined} var info
- */
- const getVarInfo = argOrThis => {
- const renameIdentifier = this.getRenameIdentifier(argOrThis);
- if (
- renameIdentifier &&
- this.callHooksForInfo(
- this.hooks.canRename,
- renameIdentifier,
- /** @type {Expression} */
- (argOrThis)
- ) &&
- !this.callHooksForInfo(
- this.hooks.rename,
- renameIdentifier,
- /** @type {Expression} */
- (argOrThis)
- )
- ) {
- return typeof renameIdentifier === "string"
- ? /** @type {string} */ (this.getVariableInfo(renameIdentifier))
- : renameIdentifier;
- }
- this.walkExpression(argOrThis);
- };
- const { params, type } = functionExpression;
- const arrow = type === "ArrowFunctionExpression";
- const renameThis = currentThis ? getVarInfo(currentThis) : null;
- const varInfoForArgs = options.map(getVarInfo);
- const wasTopLevel = this.scope.topLevelScope;
- this.scope.topLevelScope = wasTopLevel && arrow ? "arrow" : false;
- const scopeParams =
- /** @type {(Identifier | string)[]} */
- (params.filter((identifier, idx) => !varInfoForArgs[idx]));
- // Add function name in scope for recursive calls
- if (
- functionExpression.type === "FunctionExpression" &&
- functionExpression.id
- ) {
- scopeParams.push(functionExpression.id.name);
- }
- this.inFunctionScope(true, scopeParams, () => {
- if (renameThis && !arrow) {
- this.setVariable("this", renameThis);
- }
- for (let i = 0; i < varInfoForArgs.length; i++) {
- const varInfo = varInfoForArgs[i];
- if (!varInfo) continue;
- if (!params[i] || params[i].type !== "Identifier") continue;
- this.setVariable(/** @type {Identifier} */ (params[i]).name, varInfo);
- }
- if (functionExpression.body.type === "BlockStatement") {
- this.detectMode(functionExpression.body.body);
- const prev = this.prevStatement;
- this.preWalkStatement(functionExpression.body);
- this.prevStatement = prev;
- this.walkStatement(functionExpression.body);
- } else {
- this.walkExpression(functionExpression.body);
- }
- });
- this.scope.topLevelScope = wasTopLevel;
- }
- /**
- * @param {ImportExpression} expression import expression
- */
- walkImportExpression(expression) {
- const result = this.hooks.importCall.call(expression);
- if (result === true) return;
- this.walkExpression(expression.source);
- }
- /**
- * @param {CallExpression} expression expression
- */
- walkCallExpression(expression) {
- /**
- * @param {FunctionExpression | ArrowFunctionExpression} fn function
- * @returns {boolean} true when simple function
- */
- const isSimpleFunction = fn =>
- fn.params.every(p => p.type === "Identifier");
- if (
- expression.callee.type === "MemberExpression" &&
- expression.callee.object.type.endsWith("FunctionExpression") &&
- !expression.callee.computed &&
- /** @type {boolean} */
- (
- /** @type {Identifier} */
- (expression.callee.property).name === "call" ||
- /** @type {Identifier} */
- (expression.callee.property).name === "bind"
- ) &&
- expression.arguments.length > 0 &&
- isSimpleFunction(
- /** @type {FunctionExpression | ArrowFunctionExpression} */
- (expression.callee.object)
- )
- ) {
- // (function(…) { }.call/bind(?, …))
- this._walkIIFE(
- /** @type {FunctionExpression | ArrowFunctionExpression} */
- (expression.callee.object),
- expression.arguments.slice(1),
- expression.arguments[0]
- );
- } else if (
- expression.callee.type.endsWith("FunctionExpression") &&
- isSimpleFunction(
- /** @type {FunctionExpression | ArrowFunctionExpression} */
- (expression.callee)
- )
- ) {
- // (function(…) { }(…))
- this._walkIIFE(
- /** @type {FunctionExpression | ArrowFunctionExpression} */
- (expression.callee),
- expression.arguments,
- null
- );
- } else {
- if (expression.callee.type === "MemberExpression") {
- const exprInfo = this.getMemberExpressionInfo(
- expression.callee,
- ALLOWED_MEMBER_TYPES_CALL_EXPRESSION
- );
- if (exprInfo && exprInfo.type === "call") {
- const result = this.callHooksForInfo(
- this.hooks.callMemberChainOfCallMemberChain,
- exprInfo.rootInfo,
- expression,
- exprInfo.getCalleeMembers(),
- exprInfo.call,
- exprInfo.getMembers(),
- exprInfo.getMemberRanges()
- );
- if (result === true) return;
- }
- }
- const callee = this.evaluateExpression(expression.callee);
- if (callee.isIdentifier()) {
- const result1 = this.callHooksForInfo(
- this.hooks.callMemberChain,
- /** @type {NonNullable<BasicEvaluatedExpression["rootInfo"]>} */
- (callee.rootInfo),
- expression,
- /** @type {NonNullable<BasicEvaluatedExpression["getMembers"]>} */
- (callee.getMembers)(),
- callee.getMembersOptionals
- ? callee.getMembersOptionals()
- : /** @type {NonNullable<BasicEvaluatedExpression["getMembers"]>} */
- (callee.getMembers)().map(() => false),
- callee.getMemberRanges ? callee.getMemberRanges() : []
- );
- if (result1 === true) return;
- const result2 = this.callHooksForInfo(
- this.hooks.call,
- /** @type {NonNullable<BasicEvaluatedExpression["identifier"]>} */
- (callee.identifier),
- expression
- );
- if (result2 === true) return;
- }
- if (expression.callee) {
- if (expression.callee.type === "MemberExpression") {
- // because of call context we need to walk the call context as expression
- this.walkExpression(expression.callee.object);
- if (expression.callee.computed === true)
- this.walkExpression(expression.callee.property);
- } else {
- this.walkExpression(expression.callee);
- }
- }
- if (expression.arguments) this.walkExpressions(expression.arguments);
- }
- }
- /**
- * @param {MemberExpression} expression member expression
- */
- walkMemberExpression(expression) {
- const exprInfo = this.getMemberExpressionInfo(
- expression,
- ALLOWED_MEMBER_TYPES_ALL
- );
- if (exprInfo) {
- switch (exprInfo.type) {
- case "expression": {
- const result1 = this.callHooksForInfo(
- this.hooks.expression,
- exprInfo.name,
- expression
- );
- if (result1 === true) return;
- const members = exprInfo.getMembers();
- const membersOptionals = exprInfo.getMembersOptionals();
- const memberRanges = exprInfo.getMemberRanges();
- const result2 = this.callHooksForInfo(
- this.hooks.expressionMemberChain,
- exprInfo.rootInfo,
- expression,
- members,
- membersOptionals,
- memberRanges
- );
- if (result2 === true) return;
- this.walkMemberExpressionWithExpressionName(
- expression,
- exprInfo.name,
- exprInfo.rootInfo,
- members.slice(),
- () =>
- this.callHooksForInfo(
- this.hooks.unhandledExpressionMemberChain,
- exprInfo.rootInfo,
- expression,
- members
- )
- );
- return;
- }
- case "call": {
- const result = this.callHooksForInfo(
- this.hooks.memberChainOfCallMemberChain,
- exprInfo.rootInfo,
- expression,
- exprInfo.getCalleeMembers(),
- exprInfo.call,
- exprInfo.getMembers(),
- exprInfo.getMemberRanges()
- );
- if (result === true) return;
- // Fast skip over the member chain as we already called memberChainOfCallMemberChain
- // and call computed property are literals anyway
- this.walkExpression(exprInfo.call);
- return;
- }
- }
- }
- this.walkExpression(expression.object);
- if (expression.computed === true) this.walkExpression(expression.property);
- }
- /**
- * @template R
- * @param {MemberExpression} expression member expression
- * @param {string} name name
- * @param {string | VariableInfo} rootInfo root info
- * @param {string[]} members members
- * @param {() => R | undefined} onUnhandled on unhandled callback
- */
- walkMemberExpressionWithExpressionName(
- expression,
- name,
- rootInfo,
- members,
- onUnhandled
- ) {
- if (expression.object.type === "MemberExpression") {
- // optimize the case where expression.object is a MemberExpression too.
- // we can keep info here when calling walkMemberExpression directly
- const property =
- /** @type {Identifier} */
- (expression.property).name ||
- `${/** @type {Literal} */ (expression.property).value}`;
- name = name.slice(0, -property.length - 1);
- members.pop();
- const result = this.callHooksForInfo(
- this.hooks.expression,
- name,
- expression.object
- );
- if (result === true) return;
- this.walkMemberExpressionWithExpressionName(
- expression.object,
- name,
- rootInfo,
- members,
- onUnhandled
- );
- } else if (!onUnhandled || !onUnhandled()) {
- this.walkExpression(expression.object);
- }
- if (expression.computed === true) this.walkExpression(expression.property);
- }
- /**
- * @param {ThisExpression} expression this expression
- */
- walkThisExpression(expression) {
- this.callHooksForName(this.hooks.expression, "this", expression);
- }
- /**
- * @param {Identifier} expression identifier
- */
- walkIdentifier(expression) {
- this.callHooksForName(this.hooks.expression, expression.name, expression);
- }
- /**
- * @param {MetaProperty} metaProperty meta property
- */
- walkMetaProperty(metaProperty) {
- this.hooks.expression.for(getRootName(metaProperty)).call(metaProperty);
- }
- /**
- * @template T
- * @template R
- * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
- * @param {Expression | Super} expr expression
- * @param {AsArray<T>} args args for the hook
- * @returns {R | undefined} result of hook
- */
- callHooksForExpression(hookMap, expr, ...args) {
- return this.callHooksForExpressionWithFallback(
- hookMap,
- expr,
- undefined,
- undefined,
- ...args
- );
- }
- /**
- * @template T
- * @template R
- * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
- * @param {Expression | Super} expr expression info
- * @param {((name: string, rootInfo: string | ScopeInfo | VariableInfo, getMembers: () => string[]) => TODO) | undefined} fallback callback when variable in not handled by hooks
- * @param {((result?: string) => R | undefined) | undefined} defined callback when variable is defined
- * @param {AsArray<T>} args args for the hook
- * @returns {R | undefined} result of hook
- */
- callHooksForExpressionWithFallback(
- hookMap,
- expr,
- fallback,
- defined,
- ...args
- ) {
- const exprName = this.getMemberExpressionInfo(
- expr,
- ALLOWED_MEMBER_TYPES_EXPRESSION
- );
- if (exprName !== undefined) {
- const members = exprName.getMembers();
- return this.callHooksForInfoWithFallback(
- hookMap,
- members.length === 0 ? exprName.rootInfo : exprName.name,
- fallback &&
- (name => fallback(name, exprName.rootInfo, exprName.getMembers)),
- defined && (() => defined(exprName.name)),
- ...args
- );
- }
- }
- /**
- * @template T
- * @template R
- * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
- * @param {string} name key in map
- * @param {AsArray<T>} args args for the hook
- * @returns {R | undefined} result of hook
- */
- callHooksForName(hookMap, name, ...args) {
- return this.callHooksForNameWithFallback(
- hookMap,
- name,
- undefined,
- undefined,
- ...args
- );
- }
- /**
- * @template T
- * @template R
- * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks that should be called
- * @param {ExportedVariableInfo} info variable info
- * @param {AsArray<T>} args args for the hook
- * @returns {R | undefined} result of hook
- */
- callHooksForInfo(hookMap, info, ...args) {
- return this.callHooksForInfoWithFallback(
- hookMap,
- info,
- undefined,
- undefined,
- ...args
- );
- }
- /**
- * @template T
- * @template R
- * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
- * @param {ExportedVariableInfo} info variable info
- * @param {((name: string) => TODO) | undefined} fallback callback when variable in not handled by hooks
- * @param {((result?: string) => TODO) | undefined} defined callback when variable is defined
- * @param {AsArray<T>} args args for the hook
- * @returns {R | undefined} result of hook
- */
- callHooksForInfoWithFallback(hookMap, info, fallback, defined, ...args) {
- let name;
- if (typeof info === "string") {
- name = info;
- } else {
- if (!(info instanceof VariableInfo)) {
- if (defined !== undefined) {
- return defined();
- }
- return;
- }
- let tagInfo = info.tagInfo;
- while (tagInfo !== undefined) {
- const hook = hookMap.get(tagInfo.tag);
- if (hook !== undefined) {
- this.currentTagData = tagInfo.data;
- const result = hook.call(...args);
- this.currentTagData = undefined;
- if (result !== undefined) return result;
- }
- tagInfo = tagInfo.next;
- }
- if (info.freeName === true) {
- if (defined !== undefined) {
- return defined();
- }
- return;
- }
- name = info.freeName;
- }
- const hook = hookMap.get(name);
- if (hook !== undefined) {
- const result = hook.call(...args);
- if (result !== undefined) return result;
- }
- if (fallback !== undefined) {
- return fallback(/** @type {string} */ (name));
- }
- }
- /**
- * @template T
- * @template R
- * @param {HookMap<SyncBailHook<T, R>>} hookMap hooks the should be called
- * @param {string} name key in map
- * @param {((value: string) => R | undefined) | undefined} fallback callback when variable in not handled by hooks
- * @param {(() => R) | undefined} defined callback when variable is defined
- * @param {AsArray<T>} args args for the hook
- * @returns {R | undefined} result of hook
- */
- callHooksForNameWithFallback(hookMap, name, fallback, defined, ...args) {
- return this.callHooksForInfoWithFallback(
- hookMap,
- this.getVariableInfo(name),
- fallback,
- defined,
- ...args
- );
- }
- /**
- * @deprecated
- * @param {(string | Pattern | Property)[]} params scope params
- * @param {() => void} fn inner function
- * @returns {void}
- */
- inScope(params, fn) {
- const oldScope = this.scope;
- this.scope = {
- topLevelScope: oldScope.topLevelScope,
- inTry: false,
- inShorthand: false,
- inTaggedTemplateTag: false,
- isStrict: oldScope.isStrict,
- isAsmJs: oldScope.isAsmJs,
- terminated: undefined,
- definitions: oldScope.definitions.createChild()
- };
- this.undefineVariable("this");
- this.enterPatterns(params, ident => {
- this.defineVariable(ident);
- });
- fn();
- this.scope = oldScope;
- }
- /**
- * @param {boolean} hasThis true, when this is defined
- * @param {Identifier[]} params scope params
- * @param {() => void} fn inner function
- * @returns {void}
- */
- inClassScope(hasThis, params, fn) {
- const oldScope = this.scope;
- this.scope = {
- topLevelScope: oldScope.topLevelScope,
- inTry: false,
- inShorthand: false,
- inTaggedTemplateTag: false,
- isStrict: oldScope.isStrict,
- isAsmJs: oldScope.isAsmJs,
- terminated: undefined,
- definitions: oldScope.definitions.createChild()
- };
- if (hasThis) {
- this.undefineVariable("this");
- }
- this.enterPatterns(params, ident => {
- this.defineVariable(ident);
- });
- fn();
- this.scope = oldScope;
- }
- /**
- * @param {boolean} hasThis true, when this is defined
- * @param {(Pattern | string)[]} params scope params
- * @param {() => void} fn inner function
- * @returns {void}
- */
- inFunctionScope(hasThis, params, fn) {
- const oldScope = this.scope;
- this.scope = {
- topLevelScope: oldScope.topLevelScope,
- inTry: false,
- inShorthand: false,
- inTaggedTemplateTag: false,
- isStrict: oldScope.isStrict,
- isAsmJs: oldScope.isAsmJs,
- terminated: undefined,
- definitions: oldScope.definitions.createChild()
- };
- if (hasThis) {
- this.undefineVariable("this");
- }
- this.enterPatterns(params, ident => {
- this.defineVariable(ident);
- });
- fn();
- this.scope = oldScope;
- }
- /**
- * @param {() => void} fn inner function
- * @param {boolean} inExecutedPath executed state
- * @returns {void}
- */
- inBlockScope(fn, inExecutedPath = false) {
- const oldScope = this.scope;
- this.scope = {
- topLevelScope: oldScope.topLevelScope,
- inTry: oldScope.inTry,
- inShorthand: false,
- inTaggedTemplateTag: false,
- isStrict: oldScope.isStrict,
- isAsmJs: oldScope.isAsmJs,
- terminated: oldScope.terminated,
- definitions: oldScope.definitions.createChild()
- };
- fn();
- const terminated = this.scope.terminated;
- if (inExecutedPath && terminated) {
- oldScope.terminated = terminated;
- }
- this.scope = oldScope;
- }
- /**
- * @param {Array<Directive | Statement | ModuleDeclaration>} statements statements
- */
- detectMode(statements) {
- const isLiteral =
- statements.length >= 1 &&
- statements[0].type === "ExpressionStatement" &&
- statements[0].expression.type === "Literal";
- if (
- isLiteral &&
- /** @type {Literal} */
- (/** @type {ExpressionStatement} */ (statements[0]).expression).value ===
- "use strict"
- ) {
- this.scope.isStrict = true;
- }
- if (
- isLiteral &&
- /** @type {Literal} */
- (/** @type {ExpressionStatement} */ (statements[0]).expression).value ===
- "use asm"
- ) {
- this.scope.isAsmJs = true;
- }
- }
- /**
- * @param {(string | Pattern | Property)[]} patterns patterns
- * @param {OnIdentString} onIdent on ident callback
- */
- enterPatterns(patterns, onIdent) {
- for (const pattern of patterns) {
- if (typeof pattern !== "string") {
- this.enterPattern(pattern, onIdent);
- } else if (pattern) {
- onIdent(pattern);
- }
- }
- }
- /**
- * @param {Pattern | Property} pattern pattern
- * @param {OnIdent} onIdent on ident callback
- */
- enterPattern(pattern, onIdent) {
- if (!pattern) return;
- switch (pattern.type) {
- case "ArrayPattern":
- this.enterArrayPattern(pattern, onIdent);
- break;
- case "AssignmentPattern":
- this.enterAssignmentPattern(pattern, onIdent);
- break;
- case "Identifier":
- this.enterIdentifier(pattern, onIdent);
- break;
- case "ObjectPattern":
- this.enterObjectPattern(pattern, onIdent);
- break;
- case "RestElement":
- this.enterRestElement(pattern, onIdent);
- break;
- case "Property":
- if (pattern.shorthand && pattern.value.type === "Identifier") {
- this.scope.inShorthand = pattern.value.name;
- this.enterIdentifier(pattern.value, onIdent);
- this.scope.inShorthand = false;
- } else {
- this.enterPattern(/** @type {Pattern} */ (pattern.value), onIdent);
- }
- break;
- }
- }
- /**
- * @param {Identifier} pattern identifier pattern
- * @param {OnIdent} onIdent callback
- */
- enterIdentifier(pattern, onIdent) {
- if (!this.callHooksForName(this.hooks.pattern, pattern.name, pattern)) {
- onIdent(pattern.name, pattern);
- }
- }
- /**
- * @param {ObjectPattern} pattern object pattern
- * @param {OnIdent} onIdent callback
- */
- enterObjectPattern(pattern, onIdent) {
- for (
- let propIndex = 0, len = pattern.properties.length;
- propIndex < len;
- propIndex++
- ) {
- const prop = pattern.properties[propIndex];
- this.enterPattern(prop, onIdent);
- }
- }
- /**
- * @param {ArrayPattern} pattern object pattern
- * @param {OnIdent} onIdent callback
- */
- enterArrayPattern(pattern, onIdent) {
- for (
- let elementIndex = 0, len = pattern.elements.length;
- elementIndex < len;
- elementIndex++
- ) {
- const element = pattern.elements[elementIndex];
- if (element) {
- this.enterPattern(element, onIdent);
- }
- }
- }
- /**
- * @param {RestElement} pattern object pattern
- * @param {OnIdent} onIdent callback
- */
- enterRestElement(pattern, onIdent) {
- this.enterPattern(pattern.argument, onIdent);
- }
- /**
- * @param {AssignmentPattern} pattern object pattern
- * @param {OnIdent} onIdent callback
- */
- enterAssignmentPattern(pattern, onIdent) {
- this.enterPattern(pattern.left, onIdent);
- }
- /**
- * @param {Expression | SpreadElement | PrivateIdentifier | Super} expression expression node
- * @returns {BasicEvaluatedExpression} evaluation result
- */
- evaluateExpression(expression) {
- try {
- const hook = this.hooks.evaluate.get(expression.type);
- if (hook !== undefined) {
- const result = hook.call(expression);
- if (result !== undefined && result !== null) {
- result.setExpression(expression);
- return result;
- }
- }
- } catch (err) {
- // eslint-disable-next-line no-console
- console.warn(err);
- // ignore error
- }
- return new BasicEvaluatedExpression()
- .setRange(/** @type {Range} */ (expression.range))
- .setExpression(expression);
- }
- /**
- * @param {Expression} expression expression
- * @returns {string} parsed string
- */
- parseString(expression) {
- switch (expression.type) {
- case "BinaryExpression":
- if (expression.operator === "+") {
- return (
- this.parseString(/** @type {Expression} */ (expression.left)) +
- this.parseString(expression.right)
- );
- }
- break;
- case "Literal":
- return String(expression.value);
- }
- throw new Error(
- `${expression.type} is not supported as parameter for require`
- );
- }
- /**
- * @param {Expression} expression expression
- * @returns {{ range?: Range, value: string, code: boolean, conditional: boolean | TODO }} result
- */
- parseCalculatedString(expression) {
- switch (expression.type) {
- case "BinaryExpression":
- if (expression.operator === "+") {
- const left = this.parseCalculatedString(
- /** @type {Expression} */
- (expression.left)
- );
- const right = this.parseCalculatedString(expression.right);
- if (left.code) {
- return {
- range: left.range,
- value: left.value,
- code: true,
- conditional: false
- };
- } else if (right.code) {
- return {
- range: [
- /** @type {Range} */
- (left.range)[0],
- right.range
- ? right.range[1]
- : /** @type {Range} */ (left.range)[1]
- ],
- value: left.value + right.value,
- code: true,
- conditional: false
- };
- }
- return {
- range: [
- /** @type {Range} */
- (left.range)[0],
- /** @type {Range} */
- (right.range)[1]
- ],
- value: left.value + right.value,
- code: false,
- conditional: false
- };
- }
- break;
- case "ConditionalExpression": {
- const consequent = this.parseCalculatedString(expression.consequent);
- const alternate = this.parseCalculatedString(expression.alternate);
- const items = [];
- if (consequent.conditional) {
- items.push(...consequent.conditional);
- } else if (!consequent.code) {
- items.push(consequent);
- } else {
- break;
- }
- if (alternate.conditional) {
- items.push(...alternate.conditional);
- } else if (!alternate.code) {
- items.push(alternate);
- } else {
- break;
- }
- return {
- range: undefined,
- value: "",
- code: true,
- conditional: items
- };
- }
- case "Literal":
- return {
- range: expression.range,
- value: String(expression.value),
- code: false,
- conditional: false
- };
- }
- return {
- range: undefined,
- value: "",
- code: true,
- conditional: false
- };
- }
- /**
- * @param {string | Buffer | PreparsedAst} source the source to parse
- * @param {ParserState} state the parser state
- * @returns {ParserState} the parser state
- */
- parse(source, state) {
- let ast;
- /** @type {import("acorn").Comment[]} */
- let comments;
- const semicolons = new Set();
- if (source === null) {
- throw new Error("source must not be null");
- }
- if (Buffer.isBuffer(source)) {
- source = source.toString("utf-8");
- }
- if (typeof source === "object") {
- ast = /** @type {Program} */ (source);
- comments = source.comments;
- if (source.semicolons) {
- // Forward semicolon information from the preparsed AST if present
- // This ensures the output is consistent with that of a fresh AST
- for (const pos of source.semicolons) {
- semicolons.add(pos);
- }
- }
- } else {
- comments = [];
- ast = JavascriptParser._parse(source, {
- sourceType: this.sourceType,
- onComment: comments,
- onInsertedSemicolon: pos => semicolons.add(pos)
- });
- }
- const oldScope = this.scope;
- const oldState = this.state;
- const oldComments = this.comments;
- const oldSemicolons = this.semicolons;
- const oldStatementPath = this.statementPath;
- const oldPrevStatement = this.prevStatement;
- this.scope = {
- topLevelScope: true,
- inTry: false,
- inShorthand: false,
- inTaggedTemplateTag: false,
- isStrict: false,
- isAsmJs: false,
- terminated: undefined,
- definitions: new StackedMap()
- };
- this.state = /** @type {ParserState} */ (state);
- this.comments = comments;
- this.semicolons = semicolons;
- this.statementPath = [];
- this.prevStatement = undefined;
- if (this.hooks.program.call(ast, comments) === undefined) {
- this.destructuringAssignmentProperties = new WeakMap();
- this.detectMode(ast.body);
- this.preWalkStatements(ast.body);
- this.prevStatement = undefined;
- this.blockPreWalkStatements(ast.body);
- this.prevStatement = undefined;
- this.walkStatements(ast.body);
- this.destructuringAssignmentProperties = undefined;
- }
- this.hooks.finish.call(ast, comments);
- this.scope = oldScope;
- this.state = oldState;
- this.comments = oldComments;
- this.semicolons = oldSemicolons;
- this.statementPath = oldStatementPath;
- this.prevStatement = oldPrevStatement;
- return state;
- }
- /**
- * @param {string} source source code
- * @returns {BasicEvaluatedExpression} evaluation result
- */
- evaluate(source) {
- const ast = JavascriptParser._parse(`(${source})`, {
- sourceType: this.sourceType,
- locations: false
- });
- if (ast.body.length !== 1 || ast.body[0].type !== "ExpressionStatement") {
- throw new Error("evaluate: Source is not a expression");
- }
- return this.evaluateExpression(ast.body[0].expression);
- }
- /**
- * @param {Expression | Declaration | PrivateIdentifier | MaybeNamedFunctionDeclaration | MaybeNamedClassDeclaration | null | undefined} expr an expression
- * @param {number} commentsStartPos source position from which annotation comments are checked
- * @returns {boolean} true, when the expression is pure
- */
- isPure(expr, commentsStartPos) {
- if (!expr) return true;
- const result = this.hooks.isPure
- .for(expr.type)
- .call(expr, commentsStartPos);
- if (typeof result === "boolean") return result;
- switch (expr.type) {
- // TODO handle more cases
- case "ClassDeclaration":
- case "ClassExpression": {
- if (expr.body.type !== "ClassBody") return false;
- if (
- expr.superClass &&
- !this.isPure(expr.superClass, /** @type {Range} */ (expr.range)[0])
- ) {
- return false;
- }
- const items =
- /** @type {TODO[]} */
- (expr.body.body);
- return items.every(item => {
- if (
- item.computed &&
- item.key &&
- !this.isPure(item.key, item.range[0])
- ) {
- return false;
- }
- if (
- item.static &&
- item.value &&
- !this.isPure(
- item.value,
- item.key ? item.key.range[1] : item.range[0]
- )
- ) {
- return false;
- }
- if (item.type === "StaticBlock") {
- return false;
- }
- if (
- expr.superClass &&
- item.type === "MethodDefinition" &&
- item.kind === "constructor"
- ) {
- return false;
- }
- return true;
- });
- }
- case "FunctionDeclaration":
- case "FunctionExpression":
- case "ArrowFunctionExpression":
- case "ThisExpression":
- case "Literal":
- case "TemplateLiteral":
- case "Identifier":
- case "PrivateIdentifier":
- return true;
- case "VariableDeclaration":
- return expr.declarations.every(decl =>
- this.isPure(decl.init, /** @type {Range} */ (decl.range)[0])
- );
- case "ConditionalExpression":
- return (
- this.isPure(expr.test, commentsStartPos) &&
- this.isPure(
- expr.consequent,
- /** @type {Range} */ (expr.test.range)[1]
- ) &&
- this.isPure(
- expr.alternate,
- /** @type {Range} */ (expr.consequent.range)[1]
- )
- );
- case "LogicalExpression":
- return (
- this.isPure(expr.left, commentsStartPos) &&
- this.isPure(expr.right, /** @type {Range} */ (expr.left.range)[1])
- );
- case "SequenceExpression":
- return expr.expressions.every(expr => {
- const pureFlag = this.isPure(expr, commentsStartPos);
- commentsStartPos = /** @type {Range} */ (expr.range)[1];
- return pureFlag;
- });
- case "CallExpression": {
- const pureFlag =
- /** @type {Range} */ (expr.range)[0] - commentsStartPos > 12 &&
- this.getComments([
- commentsStartPos,
- /** @type {Range} */ (expr.range)[0]
- ]).some(
- comment =>
- comment.type === "Block" &&
- /^\s*(#|@)__PURE__\s*$/.test(comment.value)
- );
- if (!pureFlag) return false;
- commentsStartPos = /** @type {Range} */ (expr.callee.range)[1];
- return expr.arguments.every(arg => {
- if (arg.type === "SpreadElement") return false;
- const pureFlag = this.isPure(arg, commentsStartPos);
- commentsStartPos = /** @type {Range} */ (arg.range)[1];
- return pureFlag;
- });
- }
- }
- const evaluated = this.evaluateExpression(expr);
- return !evaluated.couldHaveSideEffects();
- }
- /**
- * @param {Range} range range
- * @returns {Comment[]} comments in the range
- */
- getComments(range) {
- const [rangeStart, rangeEnd] = range;
- /**
- * @param {Comment} comment comment
- * @param {number} needle needle
- * @returns {number} compared
- */
- const compare = (comment, needle) =>
- /** @type {Range} */ (comment.range)[0] - needle;
- const comments = /** @type {Comment[]} */ (this.comments);
- let idx = binarySearchBounds.ge(comments, rangeStart, compare);
- /** @type {Comment[]} */
- const commentsInRange = [];
- while (
- comments[idx] &&
- /** @type {Range} */ (comments[idx].range)[1] <= rangeEnd
- ) {
- commentsInRange.push(comments[idx]);
- idx++;
- }
- return commentsInRange;
- }
- /**
- * @param {number} pos source code position
- * @returns {boolean} true when a semicolon has been inserted before this position, false if not
- */
- isAsiPosition(pos) {
- const currentStatement =
- /** @type {StatementPath} */
- (this.statementPath)[
- /** @type {StatementPath} */
- (this.statementPath).length - 1
- ];
- if (currentStatement === undefined) throw new Error("Not in statement");
- const range = /** @type {Range} */ (currentStatement.range);
- return (
- // Either asking directly for the end position of the current statement
- (range[1] === pos &&
- /** @type {Set<number>} */ (this.semicolons).has(pos)) ||
- // Or asking for the start position of the current statement,
- // here we have to check multiple things
- (range[0] === pos &&
- // is there a previous statement which might be relevant?
- this.prevStatement !== undefined &&
- // is the end position of the previous statement an ASI position?
- /** @type {Set<number>} */ (this.semicolons).has(
- /** @type {Range} */ (this.prevStatement.range)[1]
- ))
- );
- }
- /**
- * @param {number} pos source code position
- * @returns {void}
- */
- setAsiPosition(pos) {
- /** @type {Set<number>} */ (this.semicolons).add(pos);
- }
- /**
- * @param {number} pos source code position
- * @returns {void}
- */
- unsetAsiPosition(pos) {
- /** @type {Set<number>} */ (this.semicolons).delete(pos);
- }
- /**
- * @param {Expression} expr expression
- * @returns {boolean} true, when the expression is a statement level expression
- */
- isStatementLevelExpression(expr) {
- const currentStatement =
- /** @type {StatementPath} */
- (this.statementPath)[
- /** @type {StatementPath} */
- (this.statementPath).length - 1
- ];
- return (
- expr === currentStatement ||
- (currentStatement.type === "ExpressionStatement" &&
- currentStatement.expression === expr)
- );
- }
- /**
- * @param {string} name name
- * @param {Tag} tag tag info
- * @returns {TagData | undefined} tag data
- */
- getTagData(name, tag) {
- const info = this.scope.definitions.get(name);
- if (info instanceof VariableInfo) {
- let tagInfo = info.tagInfo;
- while (tagInfo !== undefined) {
- if (tagInfo.tag === tag) return tagInfo.data;
- tagInfo = tagInfo.next;
- }
- }
- }
- /**
- * @param {string} name name
- * @param {Tag} tag tag info
- * @param {TagData=} data data
- */
- tagVariable(name, tag, data) {
- const oldInfo = this.scope.definitions.get(name);
- /** @type {VariableInfo} */
- let newInfo;
- if (oldInfo === undefined) {
- newInfo = new VariableInfo(this.scope, name, {
- tag,
- data,
- next: undefined
- });
- } else if (oldInfo instanceof VariableInfo) {
- newInfo = new VariableInfo(oldInfo.declaredScope, oldInfo.freeName, {
- tag,
- data,
- next: oldInfo.tagInfo
- });
- } else {
- newInfo = new VariableInfo(oldInfo, true, {
- tag,
- data,
- next: undefined
- });
- }
- this.scope.definitions.set(name, newInfo);
- }
- /**
- * @param {string} name variable name
- */
- defineVariable(name) {
- const oldInfo = this.scope.definitions.get(name);
- // Don't redefine variable in same scope to keep existing tags
- if (oldInfo instanceof VariableInfo && oldInfo.declaredScope === this.scope)
- return;
- this.scope.definitions.set(name, this.scope);
- }
- /**
- * @param {string} name variable name
- */
- undefineVariable(name) {
- this.scope.definitions.delete(name);
- }
- /**
- * @param {string} name variable name
- * @returns {boolean} true, when variable is defined
- */
- isVariableDefined(name) {
- const info = this.scope.definitions.get(name);
- if (info === undefined) return false;
- if (info instanceof VariableInfo) {
- return info.freeName === true;
- }
- return true;
- }
- /**
- * @param {string} name variable name
- * @returns {string | ExportedVariableInfo} info for this variable
- */
- getVariableInfo(name) {
- const value = this.scope.definitions.get(name);
- if (value === undefined) {
- return name;
- }
- return value;
- }
- /**
- * @param {string} name variable name
- * @param {string | ExportedVariableInfo} variableInfo new info for this variable
- * @returns {void}
- */
- setVariable(name, variableInfo) {
- if (typeof variableInfo === "string") {
- if (variableInfo === name) {
- this.scope.definitions.delete(name);
- } else {
- this.scope.definitions.set(
- name,
- new VariableInfo(this.scope, variableInfo, undefined)
- );
- }
- } else {
- this.scope.definitions.set(name, variableInfo);
- }
- }
- /**
- * @param {TagInfo} tagInfo tag info
- * @returns {VariableInfo} variable info
- */
- evaluatedVariable(tagInfo) {
- return new VariableInfo(this.scope, undefined, tagInfo);
- }
- /**
- * @param {Range} range range of the comment
- * @returns {{ options: Record<string, EXPECTED_ANY> | null, errors: (Error & { comment: Comment })[] | null }} result
- */
- parseCommentOptions(range) {
- const comments = this.getComments(range);
- if (comments.length === 0) {
- return EMPTY_COMMENT_OPTIONS;
- }
- /** @type {Record<string, EXPECTED_ANY> } */
- const options = {};
- /** @type {(Error & { comment: Comment })[]} */
- const errors = [];
- for (const comment of comments) {
- const { value } = comment;
- if (value && webpackCommentRegExp.test(value)) {
- // try compile only if webpack options comment is present
- try {
- for (let [key, val] of Object.entries(
- vm.runInContext(
- `(function(){return {${value}};})()`,
- this.magicCommentContext
- )
- )) {
- if (typeof val === "object" && val !== null) {
- val =
- val.constructor.name === "RegExp"
- ? new RegExp(val)
- : JSON.parse(JSON.stringify(val));
- }
- options[key] = val;
- }
- } catch (err) {
- const newErr = new Error(String(/** @type {Error} */ (err).message));
- newErr.stack = String(/** @type {Error} */ (err).stack);
- Object.assign(newErr, { comment });
- errors.push(/** @type {(Error & { comment: Comment })} */ (newErr));
- }
- }
- }
- return { options, errors };
- }
- /**
- * @param {Expression | Super} expression a member expression
- * @returns {{ members: string[], object: Expression | Super, membersOptionals: boolean[], memberRanges: Range[] }} member names (reverse order) and remaining object
- */
- extractMemberExpressionChain(expression) {
- /** @type {Node} */
- let expr = expression;
- const members = [];
- const membersOptionals = [];
- const memberRanges = [];
- while (expr.type === "MemberExpression") {
- if (expr.computed) {
- if (expr.property.type !== "Literal") break;
- members.push(`${expr.property.value}`); // the literal
- memberRanges.push(/** @type {Range} */ (expr.object.range)); // the range of the expression fragment before the literal
- } else {
- if (expr.property.type !== "Identifier") break;
- members.push(expr.property.name); // the identifier
- memberRanges.push(/** @type {Range} */ (expr.object.range)); // the range of the expression fragment before the identifier
- }
- membersOptionals.push(expr.optional);
- expr = expr.object;
- }
- return {
- members,
- membersOptionals,
- memberRanges,
- object: expr
- };
- }
- /**
- * @param {string} varName variable name
- * @returns {{name: string, info: VariableInfo | string} | undefined} name of the free variable and variable info for that
- */
- getFreeInfoFromVariable(varName) {
- const info = this.getVariableInfo(varName);
- let name;
- if (info instanceof VariableInfo) {
- name = info.freeName;
- if (typeof name !== "string") return;
- } else if (typeof info !== "string") {
- return;
- } else {
- name = info;
- }
- return { info, name };
- }
- /** @typedef {{ type: "call", call: CallExpression, calleeName: string, rootInfo: string | VariableInfo, getCalleeMembers: () => string[], name: string, getMembers: () => string[], getMembersOptionals: () => boolean[], getMemberRanges: () => Range[]}} CallExpressionInfo */
- /** @typedef {{ type: "expression", rootInfo: string | VariableInfo, name: string, getMembers: () => string[], getMembersOptionals: () => boolean[], getMemberRanges: () => Range[]}} ExpressionExpressionInfo */
- /**
- * @param {Expression | Super} expression a member expression
- * @param {number} allowedTypes which types should be returned, presented in bit mask
- * @returns {CallExpressionInfo | ExpressionExpressionInfo | undefined} expression info
- */
- getMemberExpressionInfo(expression, allowedTypes) {
- const { object, members, membersOptionals, memberRanges } =
- this.extractMemberExpressionChain(expression);
- switch (object.type) {
- case "CallExpression": {
- if ((allowedTypes & ALLOWED_MEMBER_TYPES_CALL_EXPRESSION) === 0) return;
- let callee = object.callee;
- let rootMembers = EMPTY_ARRAY;
- if (callee.type === "MemberExpression") {
- ({ object: callee, members: rootMembers } =
- this.extractMemberExpressionChain(callee));
- }
- const rootName = getRootName(callee);
- if (!rootName) return;
- const result = this.getFreeInfoFromVariable(rootName);
- if (!result) return;
- const { info: rootInfo, name: resolvedRoot } = result;
- const calleeName = objectAndMembersToName(resolvedRoot, rootMembers);
- return {
- type: "call",
- call: object,
- calleeName,
- rootInfo,
- getCalleeMembers: memoize(() => rootMembers.reverse()),
- name: objectAndMembersToName(`${calleeName}()`, members),
- getMembers: memoize(() => members.reverse()),
- getMembersOptionals: memoize(() => membersOptionals.reverse()),
- getMemberRanges: memoize(() => memberRanges.reverse())
- };
- }
- case "Identifier":
- case "MetaProperty":
- case "ThisExpression": {
- if ((allowedTypes & ALLOWED_MEMBER_TYPES_EXPRESSION) === 0) return;
- const rootName = getRootName(object);
- if (!rootName) return;
- const result = this.getFreeInfoFromVariable(rootName);
- if (!result) return;
- const { info: rootInfo, name: resolvedRoot } = result;
- return {
- type: "expression",
- name: objectAndMembersToName(resolvedRoot, members),
- rootInfo,
- getMembers: memoize(() => members.reverse()),
- getMembersOptionals: memoize(() => membersOptionals.reverse()),
- getMemberRanges: memoize(() => memberRanges.reverse())
- };
- }
- }
- }
- /**
- * @param {MemberExpression} expression an expression
- * @returns {{ name: string, rootInfo: ExportedVariableInfo, getMembers: () => string[]} | undefined} name info
- */
- getNameForExpression(expression) {
- return this.getMemberExpressionInfo(
- expression,
- ALLOWED_MEMBER_TYPES_EXPRESSION
- );
- }
- /**
- * @param {string} code source code
- * @param {ParseOptions} options parsing options
- * @returns {Program} parsed ast
- */
- static _parse(code, options) {
- const type = options ? options.sourceType : "module";
- /** @type {AcornOptions} */
- const parserOptions = {
- ...defaultParserOptions,
- allowReturnOutsideFunction: type === "script",
- ...options,
- sourceType: type === "auto" ? "module" : type
- };
- /** @type {import("acorn").Program | undefined} */
- let ast;
- let error;
- let threw = false;
- try {
- ast = parser.parse(code, parserOptions);
- } catch (err) {
- error = err;
- threw = true;
- }
- if (threw && type === "auto") {
- parserOptions.sourceType = "script";
- if (!("allowReturnOutsideFunction" in options)) {
- parserOptions.allowReturnOutsideFunction = true;
- }
- if (Array.isArray(parserOptions.onComment)) {
- parserOptions.onComment.length = 0;
- }
- try {
- ast = parser.parse(code, parserOptions);
- threw = false;
- } catch (_err) {
- // we use the error from first parse try
- // so nothing to do here
- }
- }
- if (threw) {
- throw error;
- }
- return /** @type {Program} */ (ast);
- }
- }
- module.exports = JavascriptParser;
- module.exports.ALLOWED_MEMBER_TYPES_ALL = ALLOWED_MEMBER_TYPES_ALL;
- module.exports.ALLOWED_MEMBER_TYPES_EXPRESSION =
- ALLOWED_MEMBER_TYPES_EXPRESSION;
- module.exports.ALLOWED_MEMBER_TYPES_CALL_EXPRESSION =
- ALLOWED_MEMBER_TYPES_CALL_EXPRESSION;
- module.exports.getImportAttributes = getImportAttributes;
- module.exports.VariableInfo = VariableInfo;
|