checker-5pyJrZ9G.cjs 1.9 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469
  1. 'use strict';
  2. /**
  3. * @license Angular v19.2.13
  4. * (c) 2010-2025 Google LLC. https://angular.io/
  5. * License: MIT
  6. */
  7. 'use strict';
  8. var ts = require('typescript');
  9. require('os');
  10. var fs$1 = require('fs');
  11. var module$1 = require('module');
  12. var p = require('path');
  13. var url = require('url');
  14. var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
  15. function _interopNamespaceDefault(e) {
  16. var n = Object.create(null);
  17. if (e) {
  18. Object.keys(e).forEach(function (k) {
  19. if (k !== 'default') {
  20. var d = Object.getOwnPropertyDescriptor(e, k);
  21. Object.defineProperty(n, k, d.get ? d : {
  22. enumerable: true,
  23. get: function () { return e[k]; }
  24. });
  25. }
  26. });
  27. }
  28. n.default = e;
  29. return Object.freeze(n);
  30. }
  31. var p__namespace = /*#__PURE__*/_interopNamespaceDefault(p);
  32. const _SELECTOR_REGEXP = new RegExp('(\\:not\\()|' + // 1: ":not("
  33. '(([\\.\\#]?)[-\\w]+)|' + // 2: "tag"; 3: "."/"#";
  34. // "-" should appear first in the regexp below as FF31 parses "[.-\w]" as a range
  35. // 4: attribute; 5: attribute_string; 6: attribute_value
  36. '(?:\\[([-.\\w*\\\\$]+)(?:=(["\']?)([^\\]"\']*)\\5)?\\])|' + // "[name]", "[name=value]",
  37. // "[name="value"]",
  38. // "[name='value']"
  39. '(\\))|' + // 7: ")"
  40. '(\\s*,\\s*)', // 8: ","
  41. 'g');
  42. /**
  43. * A css selector contains an element name,
  44. * css classes and attribute/value pairs with the purpose
  45. * of selecting subsets out of them.
  46. */
  47. class CssSelector {
  48. element = null;
  49. classNames = [];
  50. /**
  51. * The selectors are encoded in pairs where:
  52. * - even locations are attribute names
  53. * - odd locations are attribute values.
  54. *
  55. * Example:
  56. * Selector: `[key1=value1][key2]` would parse to:
  57. * ```
  58. * ['key1', 'value1', 'key2', '']
  59. * ```
  60. */
  61. attrs = [];
  62. notSelectors = [];
  63. static parse(selector) {
  64. const results = [];
  65. const _addResult = (res, cssSel) => {
  66. if (cssSel.notSelectors.length > 0 &&
  67. !cssSel.element &&
  68. cssSel.classNames.length == 0 &&
  69. cssSel.attrs.length == 0) {
  70. cssSel.element = '*';
  71. }
  72. res.push(cssSel);
  73. };
  74. let cssSelector = new CssSelector();
  75. let match;
  76. let current = cssSelector;
  77. let inNot = false;
  78. _SELECTOR_REGEXP.lastIndex = 0;
  79. while ((match = _SELECTOR_REGEXP.exec(selector))) {
  80. if (match[1 /* SelectorRegexp.NOT */]) {
  81. if (inNot) {
  82. throw new Error('Nesting :not in a selector is not allowed');
  83. }
  84. inNot = true;
  85. current = new CssSelector();
  86. cssSelector.notSelectors.push(current);
  87. }
  88. const tag = match[2 /* SelectorRegexp.TAG */];
  89. if (tag) {
  90. const prefix = match[3 /* SelectorRegexp.PREFIX */];
  91. if (prefix === '#') {
  92. // #hash
  93. current.addAttribute('id', tag.slice(1));
  94. }
  95. else if (prefix === '.') {
  96. // Class
  97. current.addClassName(tag.slice(1));
  98. }
  99. else {
  100. // Element
  101. current.setElement(tag);
  102. }
  103. }
  104. const attribute = match[4 /* SelectorRegexp.ATTRIBUTE */];
  105. if (attribute) {
  106. current.addAttribute(current.unescapeAttribute(attribute), match[6 /* SelectorRegexp.ATTRIBUTE_VALUE */]);
  107. }
  108. if (match[7 /* SelectorRegexp.NOT_END */]) {
  109. inNot = false;
  110. current = cssSelector;
  111. }
  112. if (match[8 /* SelectorRegexp.SEPARATOR */]) {
  113. if (inNot) {
  114. throw new Error('Multiple selectors in :not are not supported');
  115. }
  116. _addResult(results, cssSelector);
  117. cssSelector = current = new CssSelector();
  118. }
  119. }
  120. _addResult(results, cssSelector);
  121. return results;
  122. }
  123. /**
  124. * Unescape `\$` sequences from the CSS attribute selector.
  125. *
  126. * This is needed because `$` can have a special meaning in CSS selectors,
  127. * but we might want to match an attribute that contains `$`.
  128. * [MDN web link for more
  129. * info](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors).
  130. * @param attr the attribute to unescape.
  131. * @returns the unescaped string.
  132. */
  133. unescapeAttribute(attr) {
  134. let result = '';
  135. let escaping = false;
  136. for (let i = 0; i < attr.length; i++) {
  137. const char = attr.charAt(i);
  138. if (char === '\\') {
  139. escaping = true;
  140. continue;
  141. }
  142. if (char === '$' && !escaping) {
  143. throw new Error(`Error in attribute selector "${attr}". ` +
  144. `Unescaped "$" is not supported. Please escape with "\\$".`);
  145. }
  146. escaping = false;
  147. result += char;
  148. }
  149. return result;
  150. }
  151. /**
  152. * Escape `$` sequences from the CSS attribute selector.
  153. *
  154. * This is needed because `$` can have a special meaning in CSS selectors,
  155. * with this method we are escaping `$` with `\$'.
  156. * [MDN web link for more
  157. * info](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors).
  158. * @param attr the attribute to escape.
  159. * @returns the escaped string.
  160. */
  161. escapeAttribute(attr) {
  162. return attr.replace(/\\/g, '\\\\').replace(/\$/g, '\\$');
  163. }
  164. isElementSelector() {
  165. return (this.hasElementSelector() &&
  166. this.classNames.length == 0 &&
  167. this.attrs.length == 0 &&
  168. this.notSelectors.length === 0);
  169. }
  170. hasElementSelector() {
  171. return !!this.element;
  172. }
  173. setElement(element = null) {
  174. this.element = element;
  175. }
  176. getAttrs() {
  177. const result = [];
  178. if (this.classNames.length > 0) {
  179. result.push('class', this.classNames.join(' '));
  180. }
  181. return result.concat(this.attrs);
  182. }
  183. addAttribute(name, value = '') {
  184. this.attrs.push(name, (value && value.toLowerCase()) || '');
  185. }
  186. addClassName(name) {
  187. this.classNames.push(name.toLowerCase());
  188. }
  189. toString() {
  190. let res = this.element || '';
  191. if (this.classNames) {
  192. this.classNames.forEach((klass) => (res += `.${klass}`));
  193. }
  194. if (this.attrs) {
  195. for (let i = 0; i < this.attrs.length; i += 2) {
  196. const name = this.escapeAttribute(this.attrs[i]);
  197. const value = this.attrs[i + 1];
  198. res += `[${name}${value ? '=' + value : ''}]`;
  199. }
  200. }
  201. this.notSelectors.forEach((notSelector) => (res += `:not(${notSelector})`));
  202. return res;
  203. }
  204. }
  205. /**
  206. * Reads a list of CssSelectors and allows to calculate which ones
  207. * are contained in a given CssSelector.
  208. */
  209. class SelectorMatcher {
  210. static createNotMatcher(notSelectors) {
  211. const notMatcher = new SelectorMatcher();
  212. notMatcher.addSelectables(notSelectors, null);
  213. return notMatcher;
  214. }
  215. _elementMap = new Map();
  216. _elementPartialMap = new Map();
  217. _classMap = new Map();
  218. _classPartialMap = new Map();
  219. _attrValueMap = new Map();
  220. _attrValuePartialMap = new Map();
  221. _listContexts = [];
  222. addSelectables(cssSelectors, callbackCtxt) {
  223. let listContext = null;
  224. if (cssSelectors.length > 1) {
  225. listContext = new SelectorListContext(cssSelectors);
  226. this._listContexts.push(listContext);
  227. }
  228. for (let i = 0; i < cssSelectors.length; i++) {
  229. this._addSelectable(cssSelectors[i], callbackCtxt, listContext);
  230. }
  231. }
  232. /**
  233. * Add an object that can be found later on by calling `match`.
  234. * @param cssSelector A css selector
  235. * @param callbackCtxt An opaque object that will be given to the callback of the `match` function
  236. */
  237. _addSelectable(cssSelector, callbackCtxt, listContext) {
  238. let matcher = this;
  239. const element = cssSelector.element;
  240. const classNames = cssSelector.classNames;
  241. const attrs = cssSelector.attrs;
  242. const selectable = new SelectorContext(cssSelector, callbackCtxt, listContext);
  243. if (element) {
  244. const isTerminal = attrs.length === 0 && classNames.length === 0;
  245. if (isTerminal) {
  246. this._addTerminal(matcher._elementMap, element, selectable);
  247. }
  248. else {
  249. matcher = this._addPartial(matcher._elementPartialMap, element);
  250. }
  251. }
  252. if (classNames) {
  253. for (let i = 0; i < classNames.length; i++) {
  254. const isTerminal = attrs.length === 0 && i === classNames.length - 1;
  255. const className = classNames[i];
  256. if (isTerminal) {
  257. this._addTerminal(matcher._classMap, className, selectable);
  258. }
  259. else {
  260. matcher = this._addPartial(matcher._classPartialMap, className);
  261. }
  262. }
  263. }
  264. if (attrs) {
  265. for (let i = 0; i < attrs.length; i += 2) {
  266. const isTerminal = i === attrs.length - 2;
  267. const name = attrs[i];
  268. const value = attrs[i + 1];
  269. if (isTerminal) {
  270. const terminalMap = matcher._attrValueMap;
  271. let terminalValuesMap = terminalMap.get(name);
  272. if (!terminalValuesMap) {
  273. terminalValuesMap = new Map();
  274. terminalMap.set(name, terminalValuesMap);
  275. }
  276. this._addTerminal(terminalValuesMap, value, selectable);
  277. }
  278. else {
  279. const partialMap = matcher._attrValuePartialMap;
  280. let partialValuesMap = partialMap.get(name);
  281. if (!partialValuesMap) {
  282. partialValuesMap = new Map();
  283. partialMap.set(name, partialValuesMap);
  284. }
  285. matcher = this._addPartial(partialValuesMap, value);
  286. }
  287. }
  288. }
  289. }
  290. _addTerminal(map, name, selectable) {
  291. let terminalList = map.get(name);
  292. if (!terminalList) {
  293. terminalList = [];
  294. map.set(name, terminalList);
  295. }
  296. terminalList.push(selectable);
  297. }
  298. _addPartial(map, name) {
  299. let matcher = map.get(name);
  300. if (!matcher) {
  301. matcher = new SelectorMatcher();
  302. map.set(name, matcher);
  303. }
  304. return matcher;
  305. }
  306. /**
  307. * Find the objects that have been added via `addSelectable`
  308. * whose css selector is contained in the given css selector.
  309. * @param cssSelector A css selector
  310. * @param matchedCallback This callback will be called with the object handed into `addSelectable`
  311. * @return boolean true if a match was found
  312. */
  313. match(cssSelector, matchedCallback) {
  314. let result = false;
  315. const element = cssSelector.element;
  316. const classNames = cssSelector.classNames;
  317. const attrs = cssSelector.attrs;
  318. for (let i = 0; i < this._listContexts.length; i++) {
  319. this._listContexts[i].alreadyMatched = false;
  320. }
  321. result = this._matchTerminal(this._elementMap, element, cssSelector, matchedCallback) || result;
  322. result =
  323. this._matchPartial(this._elementPartialMap, element, cssSelector, matchedCallback) || result;
  324. if (classNames) {
  325. for (let i = 0; i < classNames.length; i++) {
  326. const className = classNames[i];
  327. result =
  328. this._matchTerminal(this._classMap, className, cssSelector, matchedCallback) || result;
  329. result =
  330. this._matchPartial(this._classPartialMap, className, cssSelector, matchedCallback) ||
  331. result;
  332. }
  333. }
  334. if (attrs) {
  335. for (let i = 0; i < attrs.length; i += 2) {
  336. const name = attrs[i];
  337. const value = attrs[i + 1];
  338. const terminalValuesMap = this._attrValueMap.get(name);
  339. if (value) {
  340. result =
  341. this._matchTerminal(terminalValuesMap, '', cssSelector, matchedCallback) || result;
  342. }
  343. result =
  344. this._matchTerminal(terminalValuesMap, value, cssSelector, matchedCallback) || result;
  345. const partialValuesMap = this._attrValuePartialMap.get(name);
  346. if (value) {
  347. result = this._matchPartial(partialValuesMap, '', cssSelector, matchedCallback) || result;
  348. }
  349. result =
  350. this._matchPartial(partialValuesMap, value, cssSelector, matchedCallback) || result;
  351. }
  352. }
  353. return result;
  354. }
  355. /** @internal */
  356. _matchTerminal(map, name, cssSelector, matchedCallback) {
  357. if (!map || typeof name !== 'string') {
  358. return false;
  359. }
  360. let selectables = map.get(name) || [];
  361. const starSelectables = map.get('*');
  362. if (starSelectables) {
  363. selectables = selectables.concat(starSelectables);
  364. }
  365. if (selectables.length === 0) {
  366. return false;
  367. }
  368. let selectable;
  369. let result = false;
  370. for (let i = 0; i < selectables.length; i++) {
  371. selectable = selectables[i];
  372. result = selectable.finalize(cssSelector, matchedCallback) || result;
  373. }
  374. return result;
  375. }
  376. /** @internal */
  377. _matchPartial(map, name, cssSelector, matchedCallback) {
  378. if (!map || typeof name !== 'string') {
  379. return false;
  380. }
  381. const nestedSelector = map.get(name);
  382. if (!nestedSelector) {
  383. return false;
  384. }
  385. // TODO(perf): get rid of recursion and measure again
  386. // TODO(perf): don't pass the whole selector into the recursion,
  387. // but only the not processed parts
  388. return nestedSelector.match(cssSelector, matchedCallback);
  389. }
  390. }
  391. class SelectorListContext {
  392. selectors;
  393. alreadyMatched = false;
  394. constructor(selectors) {
  395. this.selectors = selectors;
  396. }
  397. }
  398. // Store context to pass back selector and context when a selector is matched
  399. class SelectorContext {
  400. selector;
  401. cbContext;
  402. listContext;
  403. notSelectors;
  404. constructor(selector, cbContext, listContext) {
  405. this.selector = selector;
  406. this.cbContext = cbContext;
  407. this.listContext = listContext;
  408. this.notSelectors = selector.notSelectors;
  409. }
  410. finalize(cssSelector, callback) {
  411. let result = true;
  412. if (this.notSelectors.length > 0 && (!this.listContext || !this.listContext.alreadyMatched)) {
  413. const notMatcher = SelectorMatcher.createNotMatcher(this.notSelectors);
  414. result = !notMatcher.match(cssSelector, null);
  415. }
  416. if (result && callback && (!this.listContext || !this.listContext.alreadyMatched)) {
  417. if (this.listContext) {
  418. this.listContext.alreadyMatched = true;
  419. }
  420. callback(this.selector, this.cbContext);
  421. }
  422. return result;
  423. }
  424. }
  425. // Attention:
  426. // This file duplicates types and values from @angular/core
  427. // so that we are able to make @angular/compiler independent of @angular/core.
  428. // This is important to prevent a build cycle, as @angular/core needs to
  429. // be compiled with the compiler.
  430. // Stores the default value of `emitDistinctChangesOnly` when the `emitDistinctChangesOnly` is not
  431. // explicitly set.
  432. const emitDistinctChangesOnlyDefaultValue = true;
  433. exports.ViewEncapsulation = void 0;
  434. (function (ViewEncapsulation) {
  435. ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";
  436. // Historically the 1 value was for `Native` encapsulation which has been removed as of v11.
  437. ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";
  438. ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom";
  439. })(exports.ViewEncapsulation || (exports.ViewEncapsulation = {}));
  440. exports.ChangeDetectionStrategy = void 0;
  441. (function (ChangeDetectionStrategy) {
  442. ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
  443. ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
  444. })(exports.ChangeDetectionStrategy || (exports.ChangeDetectionStrategy = {}));
  445. /** Flags describing an input for a directive. */
  446. var InputFlags;
  447. (function (InputFlags) {
  448. InputFlags[InputFlags["None"] = 0] = "None";
  449. InputFlags[InputFlags["SignalBased"] = 1] = "SignalBased";
  450. InputFlags[InputFlags["HasDecoratorInputTransform"] = 2] = "HasDecoratorInputTransform";
  451. })(InputFlags || (InputFlags = {}));
  452. const CUSTOM_ELEMENTS_SCHEMA = {
  453. name: 'custom-elements',
  454. };
  455. const NO_ERRORS_SCHEMA = {
  456. name: 'no-errors-schema',
  457. };
  458. var SecurityContext;
  459. (function (SecurityContext) {
  460. SecurityContext[SecurityContext["NONE"] = 0] = "NONE";
  461. SecurityContext[SecurityContext["HTML"] = 1] = "HTML";
  462. SecurityContext[SecurityContext["STYLE"] = 2] = "STYLE";
  463. SecurityContext[SecurityContext["SCRIPT"] = 3] = "SCRIPT";
  464. SecurityContext[SecurityContext["URL"] = 4] = "URL";
  465. SecurityContext[SecurityContext["RESOURCE_URL"] = 5] = "RESOURCE_URL";
  466. })(SecurityContext || (SecurityContext = {}));
  467. var MissingTranslationStrategy;
  468. (function (MissingTranslationStrategy) {
  469. MissingTranslationStrategy[MissingTranslationStrategy["Error"] = 0] = "Error";
  470. MissingTranslationStrategy[MissingTranslationStrategy["Warning"] = 1] = "Warning";
  471. MissingTranslationStrategy[MissingTranslationStrategy["Ignore"] = 2] = "Ignore";
  472. })(MissingTranslationStrategy || (MissingTranslationStrategy = {}));
  473. function parserSelectorToSimpleSelector(selector) {
  474. const classes = selector.classNames && selector.classNames.length
  475. ? [8 /* SelectorFlags.CLASS */, ...selector.classNames]
  476. : [];
  477. const elementName = selector.element && selector.element !== '*' ? selector.element : '';
  478. return [elementName, ...selector.attrs, ...classes];
  479. }
  480. function parserSelectorToNegativeSelector(selector) {
  481. const classes = selector.classNames && selector.classNames.length
  482. ? [8 /* SelectorFlags.CLASS */, ...selector.classNames]
  483. : [];
  484. if (selector.element) {
  485. return [
  486. 1 /* SelectorFlags.NOT */ | 4 /* SelectorFlags.ELEMENT */,
  487. selector.element,
  488. ...selector.attrs,
  489. ...classes,
  490. ];
  491. }
  492. else if (selector.attrs.length) {
  493. return [1 /* SelectorFlags.NOT */ | 2 /* SelectorFlags.ATTRIBUTE */, ...selector.attrs, ...classes];
  494. }
  495. else {
  496. return selector.classNames && selector.classNames.length
  497. ? [1 /* SelectorFlags.NOT */ | 8 /* SelectorFlags.CLASS */, ...selector.classNames]
  498. : [];
  499. }
  500. }
  501. function parserSelectorToR3Selector(selector) {
  502. const positive = parserSelectorToSimpleSelector(selector);
  503. const negative = selector.notSelectors && selector.notSelectors.length
  504. ? selector.notSelectors.map((notSelector) => parserSelectorToNegativeSelector(notSelector))
  505. : [];
  506. return positive.concat(...negative);
  507. }
  508. function parseSelectorToR3Selector(selector) {
  509. return selector ? CssSelector.parse(selector).map(parserSelectorToR3Selector) : [];
  510. }
  511. exports.FactoryTarget = void 0;
  512. (function (FactoryTarget) {
  513. FactoryTarget[FactoryTarget["Directive"] = 0] = "Directive";
  514. FactoryTarget[FactoryTarget["Component"] = 1] = "Component";
  515. FactoryTarget[FactoryTarget["Injectable"] = 2] = "Injectable";
  516. FactoryTarget[FactoryTarget["Pipe"] = 3] = "Pipe";
  517. FactoryTarget[FactoryTarget["NgModule"] = 4] = "NgModule";
  518. })(exports.FactoryTarget || (exports.FactoryTarget = {}));
  519. var R3TemplateDependencyKind;
  520. (function (R3TemplateDependencyKind) {
  521. R3TemplateDependencyKind[R3TemplateDependencyKind["Directive"] = 0] = "Directive";
  522. R3TemplateDependencyKind[R3TemplateDependencyKind["Pipe"] = 1] = "Pipe";
  523. R3TemplateDependencyKind[R3TemplateDependencyKind["NgModule"] = 2] = "NgModule";
  524. })(R3TemplateDependencyKind || (R3TemplateDependencyKind = {}));
  525. var ViewEncapsulation;
  526. (function (ViewEncapsulation) {
  527. ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";
  528. // Historically the 1 value was for `Native` encapsulation which has been removed as of v11.
  529. ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";
  530. ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom";
  531. })(ViewEncapsulation || (ViewEncapsulation = {}));
  532. /**
  533. * A lazily created TextEncoder instance for converting strings into UTF-8 bytes
  534. */
  535. let textEncoder;
  536. /**
  537. * Return the message id or compute it using the XLIFF1 digest.
  538. */
  539. function digest$1(message) {
  540. return message.id || computeDigest(message);
  541. }
  542. /**
  543. * Compute the message id using the XLIFF1 digest.
  544. */
  545. function computeDigest(message) {
  546. return sha1(serializeNodes(message.nodes).join('') + `[${message.meaning}]`);
  547. }
  548. /**
  549. * Return the message id or compute it using the XLIFF2/XMB/$localize digest.
  550. */
  551. function decimalDigest(message) {
  552. return message.id || computeDecimalDigest(message);
  553. }
  554. /**
  555. * Compute the message id using the XLIFF2/XMB/$localize digest.
  556. */
  557. function computeDecimalDigest(message) {
  558. const visitor = new _SerializerIgnoreIcuExpVisitor();
  559. const parts = message.nodes.map((a) => a.visit(visitor, null));
  560. return computeMsgId(parts.join(''), message.meaning);
  561. }
  562. /**
  563. * Serialize the i18n ast to something xml-like in order to generate an UID.
  564. *
  565. * The visitor is also used in the i18n parser tests
  566. *
  567. * @internal
  568. */
  569. class _SerializerVisitor {
  570. visitText(text, context) {
  571. return text.value;
  572. }
  573. visitContainer(container, context) {
  574. return `[${container.children.map((child) => child.visit(this)).join(', ')}]`;
  575. }
  576. visitIcu(icu, context) {
  577. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  578. return `{${icu.expression}, ${icu.type}, ${strCases.join(', ')}}`;
  579. }
  580. visitTagPlaceholder(ph, context) {
  581. return ph.isVoid
  582. ? `<ph tag name="${ph.startName}"/>`
  583. : `<ph tag name="${ph.startName}">${ph.children
  584. .map((child) => child.visit(this))
  585. .join(', ')}</ph name="${ph.closeName}">`;
  586. }
  587. visitPlaceholder(ph, context) {
  588. return ph.value ? `<ph name="${ph.name}">${ph.value}</ph>` : `<ph name="${ph.name}"/>`;
  589. }
  590. visitIcuPlaceholder(ph, context) {
  591. return `<ph icu name="${ph.name}">${ph.value.visit(this)}</ph>`;
  592. }
  593. visitBlockPlaceholder(ph, context) {
  594. return `<ph block name="${ph.startName}">${ph.children
  595. .map((child) => child.visit(this))
  596. .join(', ')}</ph name="${ph.closeName}">`;
  597. }
  598. }
  599. const serializerVisitor$1 = new _SerializerVisitor();
  600. function serializeNodes(nodes) {
  601. return nodes.map((a) => a.visit(serializerVisitor$1, null));
  602. }
  603. /**
  604. * Serialize the i18n ast to something xml-like in order to generate an UID.
  605. *
  606. * Ignore the ICU expressions so that message IDs stays identical if only the expression changes.
  607. *
  608. * @internal
  609. */
  610. class _SerializerIgnoreIcuExpVisitor extends _SerializerVisitor {
  611. visitIcu(icu) {
  612. let strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  613. // Do not take the expression into account
  614. return `{${icu.type}, ${strCases.join(', ')}}`;
  615. }
  616. }
  617. /**
  618. * Compute the SHA1 of the given string
  619. *
  620. * see https://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  621. *
  622. * WARNING: this function has not been designed not tested with security in mind.
  623. * DO NOT USE IT IN A SECURITY SENSITIVE CONTEXT.
  624. */
  625. function sha1(str) {
  626. textEncoder ??= new TextEncoder();
  627. const utf8 = [...textEncoder.encode(str)];
  628. const words32 = bytesToWords32(utf8, Endian.Big);
  629. const len = utf8.length * 8;
  630. const w = new Uint32Array(80);
  631. let a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476, e = 0xc3d2e1f0;
  632. words32[len >> 5] |= 0x80 << (24 - (len % 32));
  633. words32[(((len + 64) >> 9) << 4) + 15] = len;
  634. for (let i = 0; i < words32.length; i += 16) {
  635. const h0 = a, h1 = b, h2 = c, h3 = d, h4 = e;
  636. for (let j = 0; j < 80; j++) {
  637. if (j < 16) {
  638. w[j] = words32[i + j];
  639. }
  640. else {
  641. w[j] = rol32(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
  642. }
  643. const fkVal = fk(j, b, c, d);
  644. const f = fkVal[0];
  645. const k = fkVal[1];
  646. const temp = [rol32(a, 5), f, e, k, w[j]].reduce(add32);
  647. e = d;
  648. d = c;
  649. c = rol32(b, 30);
  650. b = a;
  651. a = temp;
  652. }
  653. a = add32(a, h0);
  654. b = add32(b, h1);
  655. c = add32(c, h2);
  656. d = add32(d, h3);
  657. e = add32(e, h4);
  658. }
  659. // Convert the output parts to a 160-bit hexadecimal string
  660. return toHexU32(a) + toHexU32(b) + toHexU32(c) + toHexU32(d) + toHexU32(e);
  661. }
  662. /**
  663. * Convert and format a number as a string representing a 32-bit unsigned hexadecimal number.
  664. * @param value The value to format as a string.
  665. * @returns A hexadecimal string representing the value.
  666. */
  667. function toHexU32(value) {
  668. // unsigned right shift of zero ensures an unsigned 32-bit number
  669. return (value >>> 0).toString(16).padStart(8, '0');
  670. }
  671. function fk(index, b, c, d) {
  672. if (index < 20) {
  673. return [(b & c) | (~b & d), 0x5a827999];
  674. }
  675. if (index < 40) {
  676. return [b ^ c ^ d, 0x6ed9eba1];
  677. }
  678. if (index < 60) {
  679. return [(b & c) | (b & d) | (c & d), 0x8f1bbcdc];
  680. }
  681. return [b ^ c ^ d, 0xca62c1d6];
  682. }
  683. /**
  684. * Compute the fingerprint of the given string
  685. *
  686. * The output is 64 bit number encoded as a decimal string
  687. *
  688. * based on:
  689. * https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/GoogleJsMessageIdGenerator.java
  690. */
  691. function fingerprint(str) {
  692. textEncoder ??= new TextEncoder();
  693. const utf8 = textEncoder.encode(str);
  694. const view = new DataView(utf8.buffer, utf8.byteOffset, utf8.byteLength);
  695. let hi = hash32(view, utf8.length, 0);
  696. let lo = hash32(view, utf8.length, 102072);
  697. if (hi == 0 && (lo == 0 || lo == 1)) {
  698. hi = hi ^ 0x130f9bef;
  699. lo = lo ^ -1801410264;
  700. }
  701. return (BigInt.asUintN(32, BigInt(hi)) << BigInt(32)) | BigInt.asUintN(32, BigInt(lo));
  702. }
  703. function computeMsgId(msg, meaning = '') {
  704. let msgFingerprint = fingerprint(msg);
  705. if (meaning) {
  706. // Rotate the 64-bit message fingerprint one bit to the left and then add the meaning
  707. // fingerprint.
  708. msgFingerprint =
  709. BigInt.asUintN(64, msgFingerprint << BigInt(1)) |
  710. ((msgFingerprint >> BigInt(63)) & BigInt(1));
  711. msgFingerprint += fingerprint(meaning);
  712. }
  713. return BigInt.asUintN(63, msgFingerprint).toString();
  714. }
  715. function hash32(view, length, c) {
  716. let a = 0x9e3779b9, b = 0x9e3779b9;
  717. let index = 0;
  718. const end = length - 12;
  719. for (; index <= end; index += 12) {
  720. a += view.getUint32(index, true);
  721. b += view.getUint32(index + 4, true);
  722. c += view.getUint32(index + 8, true);
  723. const res = mix(a, b, c);
  724. (a = res[0]), (b = res[1]), (c = res[2]);
  725. }
  726. const remainder = length - index;
  727. // the first byte of c is reserved for the length
  728. c += length;
  729. if (remainder >= 4) {
  730. a += view.getUint32(index, true);
  731. index += 4;
  732. if (remainder >= 8) {
  733. b += view.getUint32(index, true);
  734. index += 4;
  735. // Partial 32-bit word for c
  736. if (remainder >= 9) {
  737. c += view.getUint8(index++) << 8;
  738. }
  739. if (remainder >= 10) {
  740. c += view.getUint8(index++) << 16;
  741. }
  742. if (remainder === 11) {
  743. c += view.getUint8(index++) << 24;
  744. }
  745. }
  746. else {
  747. // Partial 32-bit word for b
  748. if (remainder >= 5) {
  749. b += view.getUint8(index++);
  750. }
  751. if (remainder >= 6) {
  752. b += view.getUint8(index++) << 8;
  753. }
  754. if (remainder === 7) {
  755. b += view.getUint8(index++) << 16;
  756. }
  757. }
  758. }
  759. else {
  760. // Partial 32-bit word for a
  761. if (remainder >= 1) {
  762. a += view.getUint8(index++);
  763. }
  764. if (remainder >= 2) {
  765. a += view.getUint8(index++) << 8;
  766. }
  767. if (remainder === 3) {
  768. a += view.getUint8(index++) << 16;
  769. }
  770. }
  771. return mix(a, b, c)[2];
  772. }
  773. function mix(a, b, c) {
  774. a -= b;
  775. a -= c;
  776. a ^= c >>> 13;
  777. b -= c;
  778. b -= a;
  779. b ^= a << 8;
  780. c -= a;
  781. c -= b;
  782. c ^= b >>> 13;
  783. a -= b;
  784. a -= c;
  785. a ^= c >>> 12;
  786. b -= c;
  787. b -= a;
  788. b ^= a << 16;
  789. c -= a;
  790. c -= b;
  791. c ^= b >>> 5;
  792. a -= b;
  793. a -= c;
  794. a ^= c >>> 3;
  795. b -= c;
  796. b -= a;
  797. b ^= a << 10;
  798. c -= a;
  799. c -= b;
  800. c ^= b >>> 15;
  801. return [a, b, c];
  802. }
  803. // Utils
  804. var Endian;
  805. (function (Endian) {
  806. Endian[Endian["Little"] = 0] = "Little";
  807. Endian[Endian["Big"] = 1] = "Big";
  808. })(Endian || (Endian = {}));
  809. function add32(a, b) {
  810. return add32to64(a, b)[1];
  811. }
  812. function add32to64(a, b) {
  813. const low = (a & 0xffff) + (b & 0xffff);
  814. const high = (a >>> 16) + (b >>> 16) + (low >>> 16);
  815. return [high >>> 16, (high << 16) | (low & 0xffff)];
  816. }
  817. // Rotate a 32b number left `count` position
  818. function rol32(a, count) {
  819. return (a << count) | (a >>> (32 - count));
  820. }
  821. function bytesToWords32(bytes, endian) {
  822. const size = (bytes.length + 3) >>> 2;
  823. const words32 = [];
  824. for (let i = 0; i < size; i++) {
  825. words32[i] = wordAt(bytes, i * 4, endian);
  826. }
  827. return words32;
  828. }
  829. function byteAt(bytes, index) {
  830. return index >= bytes.length ? 0 : bytes[index];
  831. }
  832. function wordAt(bytes, index, endian) {
  833. let word = 0;
  834. if (endian === Endian.Big) {
  835. for (let i = 0; i < 4; i++) {
  836. word += byteAt(bytes, index + i) << (24 - 8 * i);
  837. }
  838. }
  839. else {
  840. for (let i = 0; i < 4; i++) {
  841. word += byteAt(bytes, index + i) << (8 * i);
  842. }
  843. }
  844. return word;
  845. }
  846. //// Types
  847. var TypeModifier;
  848. (function (TypeModifier) {
  849. TypeModifier[TypeModifier["None"] = 0] = "None";
  850. TypeModifier[TypeModifier["Const"] = 1] = "Const";
  851. })(TypeModifier || (TypeModifier = {}));
  852. class Type {
  853. modifiers;
  854. constructor(modifiers = TypeModifier.None) {
  855. this.modifiers = modifiers;
  856. }
  857. hasModifier(modifier) {
  858. return (this.modifiers & modifier) !== 0;
  859. }
  860. }
  861. var BuiltinTypeName;
  862. (function (BuiltinTypeName) {
  863. BuiltinTypeName[BuiltinTypeName["Dynamic"] = 0] = "Dynamic";
  864. BuiltinTypeName[BuiltinTypeName["Bool"] = 1] = "Bool";
  865. BuiltinTypeName[BuiltinTypeName["String"] = 2] = "String";
  866. BuiltinTypeName[BuiltinTypeName["Int"] = 3] = "Int";
  867. BuiltinTypeName[BuiltinTypeName["Number"] = 4] = "Number";
  868. BuiltinTypeName[BuiltinTypeName["Function"] = 5] = "Function";
  869. BuiltinTypeName[BuiltinTypeName["Inferred"] = 6] = "Inferred";
  870. BuiltinTypeName[BuiltinTypeName["None"] = 7] = "None";
  871. })(BuiltinTypeName || (BuiltinTypeName = {}));
  872. class BuiltinType extends Type {
  873. name;
  874. constructor(name, modifiers) {
  875. super(modifiers);
  876. this.name = name;
  877. }
  878. visitType(visitor, context) {
  879. return visitor.visitBuiltinType(this, context);
  880. }
  881. }
  882. class ExpressionType extends Type {
  883. value;
  884. typeParams;
  885. constructor(value, modifiers, typeParams = null) {
  886. super(modifiers);
  887. this.value = value;
  888. this.typeParams = typeParams;
  889. }
  890. visitType(visitor, context) {
  891. return visitor.visitExpressionType(this, context);
  892. }
  893. }
  894. class TransplantedType extends Type {
  895. type;
  896. constructor(type, modifiers) {
  897. super(modifiers);
  898. this.type = type;
  899. }
  900. visitType(visitor, context) {
  901. return visitor.visitTransplantedType(this, context);
  902. }
  903. }
  904. const DYNAMIC_TYPE = new BuiltinType(BuiltinTypeName.Dynamic);
  905. const INFERRED_TYPE = new BuiltinType(BuiltinTypeName.Inferred);
  906. const BOOL_TYPE = new BuiltinType(BuiltinTypeName.Bool);
  907. new BuiltinType(BuiltinTypeName.Int);
  908. const NUMBER_TYPE = new BuiltinType(BuiltinTypeName.Number);
  909. const STRING_TYPE = new BuiltinType(BuiltinTypeName.String);
  910. new BuiltinType(BuiltinTypeName.Function);
  911. const NONE_TYPE = new BuiltinType(BuiltinTypeName.None);
  912. ///// Expressions
  913. var UnaryOperator;
  914. (function (UnaryOperator) {
  915. UnaryOperator[UnaryOperator["Minus"] = 0] = "Minus";
  916. UnaryOperator[UnaryOperator["Plus"] = 1] = "Plus";
  917. })(UnaryOperator || (UnaryOperator = {}));
  918. var BinaryOperator;
  919. (function (BinaryOperator) {
  920. BinaryOperator[BinaryOperator["Equals"] = 0] = "Equals";
  921. BinaryOperator[BinaryOperator["NotEquals"] = 1] = "NotEquals";
  922. BinaryOperator[BinaryOperator["Identical"] = 2] = "Identical";
  923. BinaryOperator[BinaryOperator["NotIdentical"] = 3] = "NotIdentical";
  924. BinaryOperator[BinaryOperator["Minus"] = 4] = "Minus";
  925. BinaryOperator[BinaryOperator["Plus"] = 5] = "Plus";
  926. BinaryOperator[BinaryOperator["Divide"] = 6] = "Divide";
  927. BinaryOperator[BinaryOperator["Multiply"] = 7] = "Multiply";
  928. BinaryOperator[BinaryOperator["Modulo"] = 8] = "Modulo";
  929. BinaryOperator[BinaryOperator["And"] = 9] = "And";
  930. BinaryOperator[BinaryOperator["Or"] = 10] = "Or";
  931. BinaryOperator[BinaryOperator["BitwiseOr"] = 11] = "BitwiseOr";
  932. BinaryOperator[BinaryOperator["BitwiseAnd"] = 12] = "BitwiseAnd";
  933. BinaryOperator[BinaryOperator["Lower"] = 13] = "Lower";
  934. BinaryOperator[BinaryOperator["LowerEquals"] = 14] = "LowerEquals";
  935. BinaryOperator[BinaryOperator["Bigger"] = 15] = "Bigger";
  936. BinaryOperator[BinaryOperator["BiggerEquals"] = 16] = "BiggerEquals";
  937. BinaryOperator[BinaryOperator["NullishCoalesce"] = 17] = "NullishCoalesce";
  938. })(BinaryOperator || (BinaryOperator = {}));
  939. function nullSafeIsEquivalent(base, other) {
  940. if (base == null || other == null) {
  941. return base == other;
  942. }
  943. return base.isEquivalent(other);
  944. }
  945. function areAllEquivalentPredicate(base, other, equivalentPredicate) {
  946. const len = base.length;
  947. if (len !== other.length) {
  948. return false;
  949. }
  950. for (let i = 0; i < len; i++) {
  951. if (!equivalentPredicate(base[i], other[i])) {
  952. return false;
  953. }
  954. }
  955. return true;
  956. }
  957. function areAllEquivalent(base, other) {
  958. return areAllEquivalentPredicate(base, other, (baseElement, otherElement) => baseElement.isEquivalent(otherElement));
  959. }
  960. class Expression {
  961. type;
  962. sourceSpan;
  963. constructor(type, sourceSpan) {
  964. this.type = type || null;
  965. this.sourceSpan = sourceSpan || null;
  966. }
  967. prop(name, sourceSpan) {
  968. return new ReadPropExpr(this, name, null, sourceSpan);
  969. }
  970. key(index, type, sourceSpan) {
  971. return new ReadKeyExpr(this, index, type, sourceSpan);
  972. }
  973. callFn(params, sourceSpan, pure) {
  974. return new InvokeFunctionExpr(this, params, null, sourceSpan, pure);
  975. }
  976. instantiate(params, type, sourceSpan) {
  977. return new InstantiateExpr(this, params, type, sourceSpan);
  978. }
  979. conditional(trueCase, falseCase = null, sourceSpan) {
  980. return new ConditionalExpr(this, trueCase, falseCase, null, sourceSpan);
  981. }
  982. equals(rhs, sourceSpan) {
  983. return new BinaryOperatorExpr(BinaryOperator.Equals, this, rhs, null, sourceSpan);
  984. }
  985. notEquals(rhs, sourceSpan) {
  986. return new BinaryOperatorExpr(BinaryOperator.NotEquals, this, rhs, null, sourceSpan);
  987. }
  988. identical(rhs, sourceSpan) {
  989. return new BinaryOperatorExpr(BinaryOperator.Identical, this, rhs, null, sourceSpan);
  990. }
  991. notIdentical(rhs, sourceSpan) {
  992. return new BinaryOperatorExpr(BinaryOperator.NotIdentical, this, rhs, null, sourceSpan);
  993. }
  994. minus(rhs, sourceSpan) {
  995. return new BinaryOperatorExpr(BinaryOperator.Minus, this, rhs, null, sourceSpan);
  996. }
  997. plus(rhs, sourceSpan) {
  998. return new BinaryOperatorExpr(BinaryOperator.Plus, this, rhs, null, sourceSpan);
  999. }
  1000. divide(rhs, sourceSpan) {
  1001. return new BinaryOperatorExpr(BinaryOperator.Divide, this, rhs, null, sourceSpan);
  1002. }
  1003. multiply(rhs, sourceSpan) {
  1004. return new BinaryOperatorExpr(BinaryOperator.Multiply, this, rhs, null, sourceSpan);
  1005. }
  1006. modulo(rhs, sourceSpan) {
  1007. return new BinaryOperatorExpr(BinaryOperator.Modulo, this, rhs, null, sourceSpan);
  1008. }
  1009. and(rhs, sourceSpan) {
  1010. return new BinaryOperatorExpr(BinaryOperator.And, this, rhs, null, sourceSpan);
  1011. }
  1012. bitwiseOr(rhs, sourceSpan, parens = true) {
  1013. return new BinaryOperatorExpr(BinaryOperator.BitwiseOr, this, rhs, null, sourceSpan, parens);
  1014. }
  1015. bitwiseAnd(rhs, sourceSpan, parens = true) {
  1016. return new BinaryOperatorExpr(BinaryOperator.BitwiseAnd, this, rhs, null, sourceSpan, parens);
  1017. }
  1018. or(rhs, sourceSpan) {
  1019. return new BinaryOperatorExpr(BinaryOperator.Or, this, rhs, null, sourceSpan);
  1020. }
  1021. lower(rhs, sourceSpan) {
  1022. return new BinaryOperatorExpr(BinaryOperator.Lower, this, rhs, null, sourceSpan);
  1023. }
  1024. lowerEquals(rhs, sourceSpan) {
  1025. return new BinaryOperatorExpr(BinaryOperator.LowerEquals, this, rhs, null, sourceSpan);
  1026. }
  1027. bigger(rhs, sourceSpan) {
  1028. return new BinaryOperatorExpr(BinaryOperator.Bigger, this, rhs, null, sourceSpan);
  1029. }
  1030. biggerEquals(rhs, sourceSpan) {
  1031. return new BinaryOperatorExpr(BinaryOperator.BiggerEquals, this, rhs, null, sourceSpan);
  1032. }
  1033. isBlank(sourceSpan) {
  1034. // Note: We use equals by purpose here to compare to null and undefined in JS.
  1035. // We use the typed null to allow strictNullChecks to narrow types.
  1036. return this.equals(TYPED_NULL_EXPR, sourceSpan);
  1037. }
  1038. nullishCoalesce(rhs, sourceSpan) {
  1039. return new BinaryOperatorExpr(BinaryOperator.NullishCoalesce, this, rhs, null, sourceSpan);
  1040. }
  1041. toStmt() {
  1042. return new ExpressionStatement(this, null);
  1043. }
  1044. }
  1045. class ReadVarExpr extends Expression {
  1046. name;
  1047. constructor(name, type, sourceSpan) {
  1048. super(type, sourceSpan);
  1049. this.name = name;
  1050. }
  1051. isEquivalent(e) {
  1052. return e instanceof ReadVarExpr && this.name === e.name;
  1053. }
  1054. isConstant() {
  1055. return false;
  1056. }
  1057. visitExpression(visitor, context) {
  1058. return visitor.visitReadVarExpr(this, context);
  1059. }
  1060. clone() {
  1061. return new ReadVarExpr(this.name, this.type, this.sourceSpan);
  1062. }
  1063. set(value) {
  1064. return new WriteVarExpr(this.name, value, null, this.sourceSpan);
  1065. }
  1066. }
  1067. class TypeofExpr extends Expression {
  1068. expr;
  1069. constructor(expr, type, sourceSpan) {
  1070. super(type, sourceSpan);
  1071. this.expr = expr;
  1072. }
  1073. visitExpression(visitor, context) {
  1074. return visitor.visitTypeofExpr(this, context);
  1075. }
  1076. isEquivalent(e) {
  1077. return e instanceof TypeofExpr && e.expr.isEquivalent(this.expr);
  1078. }
  1079. isConstant() {
  1080. return this.expr.isConstant();
  1081. }
  1082. clone() {
  1083. return new TypeofExpr(this.expr.clone());
  1084. }
  1085. }
  1086. class WrappedNodeExpr extends Expression {
  1087. node;
  1088. constructor(node, type, sourceSpan) {
  1089. super(type, sourceSpan);
  1090. this.node = node;
  1091. }
  1092. isEquivalent(e) {
  1093. return e instanceof WrappedNodeExpr && this.node === e.node;
  1094. }
  1095. isConstant() {
  1096. return false;
  1097. }
  1098. visitExpression(visitor, context) {
  1099. return visitor.visitWrappedNodeExpr(this, context);
  1100. }
  1101. clone() {
  1102. return new WrappedNodeExpr(this.node, this.type, this.sourceSpan);
  1103. }
  1104. }
  1105. class WriteVarExpr extends Expression {
  1106. name;
  1107. value;
  1108. constructor(name, value, type, sourceSpan) {
  1109. super(type || value.type, sourceSpan);
  1110. this.name = name;
  1111. this.value = value;
  1112. }
  1113. isEquivalent(e) {
  1114. return e instanceof WriteVarExpr && this.name === e.name && this.value.isEquivalent(e.value);
  1115. }
  1116. isConstant() {
  1117. return false;
  1118. }
  1119. visitExpression(visitor, context) {
  1120. return visitor.visitWriteVarExpr(this, context);
  1121. }
  1122. clone() {
  1123. return new WriteVarExpr(this.name, this.value.clone(), this.type, this.sourceSpan);
  1124. }
  1125. toDeclStmt(type, modifiers) {
  1126. return new DeclareVarStmt(this.name, this.value, type, modifiers, this.sourceSpan);
  1127. }
  1128. toConstDecl() {
  1129. return this.toDeclStmt(INFERRED_TYPE, exports.StmtModifier.Final);
  1130. }
  1131. }
  1132. class WriteKeyExpr extends Expression {
  1133. receiver;
  1134. index;
  1135. value;
  1136. constructor(receiver, index, value, type, sourceSpan) {
  1137. super(type || value.type, sourceSpan);
  1138. this.receiver = receiver;
  1139. this.index = index;
  1140. this.value = value;
  1141. }
  1142. isEquivalent(e) {
  1143. return (e instanceof WriteKeyExpr &&
  1144. this.receiver.isEquivalent(e.receiver) &&
  1145. this.index.isEquivalent(e.index) &&
  1146. this.value.isEquivalent(e.value));
  1147. }
  1148. isConstant() {
  1149. return false;
  1150. }
  1151. visitExpression(visitor, context) {
  1152. return visitor.visitWriteKeyExpr(this, context);
  1153. }
  1154. clone() {
  1155. return new WriteKeyExpr(this.receiver.clone(), this.index.clone(), this.value.clone(), this.type, this.sourceSpan);
  1156. }
  1157. }
  1158. class WritePropExpr extends Expression {
  1159. receiver;
  1160. name;
  1161. value;
  1162. constructor(receiver, name, value, type, sourceSpan) {
  1163. super(type || value.type, sourceSpan);
  1164. this.receiver = receiver;
  1165. this.name = name;
  1166. this.value = value;
  1167. }
  1168. isEquivalent(e) {
  1169. return (e instanceof WritePropExpr &&
  1170. this.receiver.isEquivalent(e.receiver) &&
  1171. this.name === e.name &&
  1172. this.value.isEquivalent(e.value));
  1173. }
  1174. isConstant() {
  1175. return false;
  1176. }
  1177. visitExpression(visitor, context) {
  1178. return visitor.visitWritePropExpr(this, context);
  1179. }
  1180. clone() {
  1181. return new WritePropExpr(this.receiver.clone(), this.name, this.value.clone(), this.type, this.sourceSpan);
  1182. }
  1183. }
  1184. class InvokeFunctionExpr extends Expression {
  1185. fn;
  1186. args;
  1187. pure;
  1188. constructor(fn, args, type, sourceSpan, pure = false) {
  1189. super(type, sourceSpan);
  1190. this.fn = fn;
  1191. this.args = args;
  1192. this.pure = pure;
  1193. }
  1194. // An alias for fn, which allows other logic to handle calls and property reads together.
  1195. get receiver() {
  1196. return this.fn;
  1197. }
  1198. isEquivalent(e) {
  1199. return (e instanceof InvokeFunctionExpr &&
  1200. this.fn.isEquivalent(e.fn) &&
  1201. areAllEquivalent(this.args, e.args) &&
  1202. this.pure === e.pure);
  1203. }
  1204. isConstant() {
  1205. return false;
  1206. }
  1207. visitExpression(visitor, context) {
  1208. return visitor.visitInvokeFunctionExpr(this, context);
  1209. }
  1210. clone() {
  1211. return new InvokeFunctionExpr(this.fn.clone(), this.args.map((arg) => arg.clone()), this.type, this.sourceSpan, this.pure);
  1212. }
  1213. }
  1214. class TaggedTemplateLiteralExpr extends Expression {
  1215. tag;
  1216. template;
  1217. constructor(tag, template, type, sourceSpan) {
  1218. super(type, sourceSpan);
  1219. this.tag = tag;
  1220. this.template = template;
  1221. }
  1222. isEquivalent(e) {
  1223. return (e instanceof TaggedTemplateLiteralExpr &&
  1224. this.tag.isEquivalent(e.tag) &&
  1225. this.template.isEquivalent(e.template));
  1226. }
  1227. isConstant() {
  1228. return false;
  1229. }
  1230. visitExpression(visitor, context) {
  1231. return visitor.visitTaggedTemplateLiteralExpr(this, context);
  1232. }
  1233. clone() {
  1234. return new TaggedTemplateLiteralExpr(this.tag.clone(), this.template.clone(), this.type, this.sourceSpan);
  1235. }
  1236. }
  1237. class InstantiateExpr extends Expression {
  1238. classExpr;
  1239. args;
  1240. constructor(classExpr, args, type, sourceSpan) {
  1241. super(type, sourceSpan);
  1242. this.classExpr = classExpr;
  1243. this.args = args;
  1244. }
  1245. isEquivalent(e) {
  1246. return (e instanceof InstantiateExpr &&
  1247. this.classExpr.isEquivalent(e.classExpr) &&
  1248. areAllEquivalent(this.args, e.args));
  1249. }
  1250. isConstant() {
  1251. return false;
  1252. }
  1253. visitExpression(visitor, context) {
  1254. return visitor.visitInstantiateExpr(this, context);
  1255. }
  1256. clone() {
  1257. return new InstantiateExpr(this.classExpr.clone(), this.args.map((arg) => arg.clone()), this.type, this.sourceSpan);
  1258. }
  1259. }
  1260. class LiteralExpr extends Expression {
  1261. value;
  1262. constructor(value, type, sourceSpan) {
  1263. super(type, sourceSpan);
  1264. this.value = value;
  1265. }
  1266. isEquivalent(e) {
  1267. return e instanceof LiteralExpr && this.value === e.value;
  1268. }
  1269. isConstant() {
  1270. return true;
  1271. }
  1272. visitExpression(visitor, context) {
  1273. return visitor.visitLiteralExpr(this, context);
  1274. }
  1275. clone() {
  1276. return new LiteralExpr(this.value, this.type, this.sourceSpan);
  1277. }
  1278. }
  1279. class TemplateLiteralExpr extends Expression {
  1280. elements;
  1281. expressions;
  1282. constructor(elements, expressions, sourceSpan) {
  1283. super(null, sourceSpan);
  1284. this.elements = elements;
  1285. this.expressions = expressions;
  1286. }
  1287. isEquivalent(e) {
  1288. return (e instanceof TemplateLiteralExpr &&
  1289. areAllEquivalentPredicate(this.elements, e.elements, (a, b) => a.text === b.text) &&
  1290. areAllEquivalent(this.expressions, e.expressions));
  1291. }
  1292. isConstant() {
  1293. return false;
  1294. }
  1295. visitExpression(visitor, context) {
  1296. return visitor.visitTemplateLiteralExpr(this, context);
  1297. }
  1298. clone() {
  1299. return new TemplateLiteralExpr(this.elements.map((el) => el.clone()), this.expressions.map((expr) => expr.clone()));
  1300. }
  1301. }
  1302. class TemplateLiteralElementExpr extends Expression {
  1303. text;
  1304. rawText;
  1305. constructor(text, sourceSpan, rawText) {
  1306. super(STRING_TYPE, sourceSpan);
  1307. this.text = text;
  1308. // If `rawText` is not provided, "fake" the raw string by escaping the following sequences:
  1309. // - "\" would otherwise indicate that the next character is a control character.
  1310. // - "`" and "${" are template string control sequences that would otherwise prematurely
  1311. // indicate the end of the template literal element.
  1312. // Note that we can't rely on the `sourceSpan` here, because it may be incorrect (see
  1313. // https://github.com/angular/angular/pull/60267#discussion_r1986402524).
  1314. this.rawText = rawText ?? escapeForTemplateLiteral(escapeSlashes(text));
  1315. }
  1316. visitExpression(visitor, context) {
  1317. return visitor.visitTemplateLiteralElementExpr(this, context);
  1318. }
  1319. isEquivalent(e) {
  1320. return (e instanceof TemplateLiteralElementExpr && e.text === this.text && e.rawText === this.rawText);
  1321. }
  1322. isConstant() {
  1323. return true;
  1324. }
  1325. clone() {
  1326. return new TemplateLiteralElementExpr(this.text, this.sourceSpan, this.rawText);
  1327. }
  1328. }
  1329. class LiteralPiece {
  1330. text;
  1331. sourceSpan;
  1332. constructor(text, sourceSpan) {
  1333. this.text = text;
  1334. this.sourceSpan = sourceSpan;
  1335. }
  1336. }
  1337. class PlaceholderPiece {
  1338. text;
  1339. sourceSpan;
  1340. associatedMessage;
  1341. /**
  1342. * Create a new instance of a `PlaceholderPiece`.
  1343. *
  1344. * @param text the name of this placeholder (e.g. `PH_1`).
  1345. * @param sourceSpan the location of this placeholder in its localized message the source code.
  1346. * @param associatedMessage reference to another message that this placeholder is associated with.
  1347. * The `associatedMessage` is mainly used to provide a relationship to an ICU message that has
  1348. * been extracted out from the message containing the placeholder.
  1349. */
  1350. constructor(text, sourceSpan, associatedMessage) {
  1351. this.text = text;
  1352. this.sourceSpan = sourceSpan;
  1353. this.associatedMessage = associatedMessage;
  1354. }
  1355. }
  1356. const MEANING_SEPARATOR$1 = '|';
  1357. const ID_SEPARATOR$1 = '@@';
  1358. const LEGACY_ID_INDICATOR = '␟';
  1359. class LocalizedString extends Expression {
  1360. metaBlock;
  1361. messageParts;
  1362. placeHolderNames;
  1363. expressions;
  1364. constructor(metaBlock, messageParts, placeHolderNames, expressions, sourceSpan) {
  1365. super(STRING_TYPE, sourceSpan);
  1366. this.metaBlock = metaBlock;
  1367. this.messageParts = messageParts;
  1368. this.placeHolderNames = placeHolderNames;
  1369. this.expressions = expressions;
  1370. }
  1371. isEquivalent(e) {
  1372. // return e instanceof LocalizedString && this.message === e.message;
  1373. return false;
  1374. }
  1375. isConstant() {
  1376. return false;
  1377. }
  1378. visitExpression(visitor, context) {
  1379. return visitor.visitLocalizedString(this, context);
  1380. }
  1381. clone() {
  1382. return new LocalizedString(this.metaBlock, this.messageParts, this.placeHolderNames, this.expressions.map((expr) => expr.clone()), this.sourceSpan);
  1383. }
  1384. /**
  1385. * Serialize the given `meta` and `messagePart` into "cooked" and "raw" strings that can be used
  1386. * in a `$localize` tagged string. The format of the metadata is the same as that parsed by
  1387. * `parseI18nMeta()`.
  1388. *
  1389. * @param meta The metadata to serialize
  1390. * @param messagePart The first part of the tagged string
  1391. */
  1392. serializeI18nHead() {
  1393. let metaBlock = this.metaBlock.description || '';
  1394. if (this.metaBlock.meaning) {
  1395. metaBlock = `${this.metaBlock.meaning}${MEANING_SEPARATOR$1}${metaBlock}`;
  1396. }
  1397. if (this.metaBlock.customId) {
  1398. metaBlock = `${metaBlock}${ID_SEPARATOR$1}${this.metaBlock.customId}`;
  1399. }
  1400. if (this.metaBlock.legacyIds) {
  1401. this.metaBlock.legacyIds.forEach((legacyId) => {
  1402. metaBlock = `${metaBlock}${LEGACY_ID_INDICATOR}${legacyId}`;
  1403. });
  1404. }
  1405. return createCookedRawString(metaBlock, this.messageParts[0].text, this.getMessagePartSourceSpan(0));
  1406. }
  1407. getMessagePartSourceSpan(i) {
  1408. return this.messageParts[i]?.sourceSpan ?? this.sourceSpan;
  1409. }
  1410. getPlaceholderSourceSpan(i) {
  1411. return (this.placeHolderNames[i]?.sourceSpan ?? this.expressions[i]?.sourceSpan ?? this.sourceSpan);
  1412. }
  1413. /**
  1414. * Serialize the given `placeholderName` and `messagePart` into "cooked" and "raw" strings that
  1415. * can be used in a `$localize` tagged string.
  1416. *
  1417. * The format is `:<placeholder-name>[@@<associated-id>]:`.
  1418. *
  1419. * The `associated-id` is the message id of the (usually an ICU) message to which this placeholder
  1420. * refers.
  1421. *
  1422. * @param partIndex The index of the message part to serialize.
  1423. */
  1424. serializeI18nTemplatePart(partIndex) {
  1425. const placeholder = this.placeHolderNames[partIndex - 1];
  1426. const messagePart = this.messageParts[partIndex];
  1427. let metaBlock = placeholder.text;
  1428. if (placeholder.associatedMessage?.legacyIds.length === 0) {
  1429. metaBlock += `${ID_SEPARATOR$1}${computeMsgId(placeholder.associatedMessage.messageString, placeholder.associatedMessage.meaning)}`;
  1430. }
  1431. return createCookedRawString(metaBlock, messagePart.text, this.getMessagePartSourceSpan(partIndex));
  1432. }
  1433. }
  1434. const escapeSlashes = (str) => str.replace(/\\/g, '\\\\');
  1435. const escapeStartingColon = (str) => str.replace(/^:/, '\\:');
  1436. const escapeColons = (str) => str.replace(/:/g, '\\:');
  1437. const escapeForTemplateLiteral = (str) => str.replace(/`/g, '\\`').replace(/\${/g, '$\\{');
  1438. /**
  1439. * Creates a `{cooked, raw}` object from the `metaBlock` and `messagePart`.
  1440. *
  1441. * The `raw` text must have various character sequences escaped:
  1442. * * "\" would otherwise indicate that the next character is a control character.
  1443. * * "`" and "${" are template string control sequences that would otherwise prematurely indicate
  1444. * the end of a message part.
  1445. * * ":" inside a metablock would prematurely indicate the end of the metablock.
  1446. * * ":" at the start of a messagePart with no metablock would erroneously indicate the start of a
  1447. * metablock.
  1448. *
  1449. * @param metaBlock Any metadata that should be prepended to the string
  1450. * @param messagePart The message part of the string
  1451. */
  1452. function createCookedRawString(metaBlock, messagePart, range) {
  1453. if (metaBlock === '') {
  1454. return {
  1455. cooked: messagePart,
  1456. raw: escapeForTemplateLiteral(escapeStartingColon(escapeSlashes(messagePart))),
  1457. range,
  1458. };
  1459. }
  1460. else {
  1461. return {
  1462. cooked: `:${metaBlock}:${messagePart}`,
  1463. raw: escapeForTemplateLiteral(`:${escapeColons(escapeSlashes(metaBlock))}:${escapeSlashes(messagePart)}`),
  1464. range,
  1465. };
  1466. }
  1467. }
  1468. class ExternalExpr extends Expression {
  1469. value;
  1470. typeParams;
  1471. constructor(value, type, typeParams = null, sourceSpan) {
  1472. super(type, sourceSpan);
  1473. this.value = value;
  1474. this.typeParams = typeParams;
  1475. }
  1476. isEquivalent(e) {
  1477. return (e instanceof ExternalExpr &&
  1478. this.value.name === e.value.name &&
  1479. this.value.moduleName === e.value.moduleName);
  1480. }
  1481. isConstant() {
  1482. return false;
  1483. }
  1484. visitExpression(visitor, context) {
  1485. return visitor.visitExternalExpr(this, context);
  1486. }
  1487. clone() {
  1488. return new ExternalExpr(this.value, this.type, this.typeParams, this.sourceSpan);
  1489. }
  1490. }
  1491. class ExternalReference {
  1492. moduleName;
  1493. name;
  1494. constructor(moduleName, name) {
  1495. this.moduleName = moduleName;
  1496. this.name = name;
  1497. }
  1498. }
  1499. class ConditionalExpr extends Expression {
  1500. condition;
  1501. falseCase;
  1502. trueCase;
  1503. constructor(condition, trueCase, falseCase = null, type, sourceSpan) {
  1504. super(type || trueCase.type, sourceSpan);
  1505. this.condition = condition;
  1506. this.falseCase = falseCase;
  1507. this.trueCase = trueCase;
  1508. }
  1509. isEquivalent(e) {
  1510. return (e instanceof ConditionalExpr &&
  1511. this.condition.isEquivalent(e.condition) &&
  1512. this.trueCase.isEquivalent(e.trueCase) &&
  1513. nullSafeIsEquivalent(this.falseCase, e.falseCase));
  1514. }
  1515. isConstant() {
  1516. return false;
  1517. }
  1518. visitExpression(visitor, context) {
  1519. return visitor.visitConditionalExpr(this, context);
  1520. }
  1521. clone() {
  1522. return new ConditionalExpr(this.condition.clone(), this.trueCase.clone(), this.falseCase?.clone(), this.type, this.sourceSpan);
  1523. }
  1524. }
  1525. class DynamicImportExpr extends Expression {
  1526. url;
  1527. urlComment;
  1528. constructor(url, sourceSpan, urlComment) {
  1529. super(null, sourceSpan);
  1530. this.url = url;
  1531. this.urlComment = urlComment;
  1532. }
  1533. isEquivalent(e) {
  1534. return e instanceof DynamicImportExpr && this.url === e.url && this.urlComment === e.urlComment;
  1535. }
  1536. isConstant() {
  1537. return false;
  1538. }
  1539. visitExpression(visitor, context) {
  1540. return visitor.visitDynamicImportExpr(this, context);
  1541. }
  1542. clone() {
  1543. return new DynamicImportExpr(typeof this.url === 'string' ? this.url : this.url.clone(), this.sourceSpan, this.urlComment);
  1544. }
  1545. }
  1546. class NotExpr extends Expression {
  1547. condition;
  1548. constructor(condition, sourceSpan) {
  1549. super(BOOL_TYPE, sourceSpan);
  1550. this.condition = condition;
  1551. }
  1552. isEquivalent(e) {
  1553. return e instanceof NotExpr && this.condition.isEquivalent(e.condition);
  1554. }
  1555. isConstant() {
  1556. return false;
  1557. }
  1558. visitExpression(visitor, context) {
  1559. return visitor.visitNotExpr(this, context);
  1560. }
  1561. clone() {
  1562. return new NotExpr(this.condition.clone(), this.sourceSpan);
  1563. }
  1564. }
  1565. class FnParam {
  1566. name;
  1567. type;
  1568. constructor(name, type = null) {
  1569. this.name = name;
  1570. this.type = type;
  1571. }
  1572. isEquivalent(param) {
  1573. return this.name === param.name;
  1574. }
  1575. clone() {
  1576. return new FnParam(this.name, this.type);
  1577. }
  1578. }
  1579. class FunctionExpr extends Expression {
  1580. params;
  1581. statements;
  1582. name;
  1583. constructor(params, statements, type, sourceSpan, name) {
  1584. super(type, sourceSpan);
  1585. this.params = params;
  1586. this.statements = statements;
  1587. this.name = name;
  1588. }
  1589. isEquivalent(e) {
  1590. return ((e instanceof FunctionExpr || e instanceof DeclareFunctionStmt) &&
  1591. areAllEquivalent(this.params, e.params) &&
  1592. areAllEquivalent(this.statements, e.statements));
  1593. }
  1594. isConstant() {
  1595. return false;
  1596. }
  1597. visitExpression(visitor, context) {
  1598. return visitor.visitFunctionExpr(this, context);
  1599. }
  1600. toDeclStmt(name, modifiers) {
  1601. return new DeclareFunctionStmt(name, this.params, this.statements, this.type, modifiers, this.sourceSpan);
  1602. }
  1603. clone() {
  1604. // TODO: Should we deep clone statements?
  1605. return new FunctionExpr(this.params.map((p) => p.clone()), this.statements, this.type, this.sourceSpan, this.name);
  1606. }
  1607. }
  1608. class ArrowFunctionExpr extends Expression {
  1609. params;
  1610. body;
  1611. // Note that `body: Expression` represents `() => expr` whereas
  1612. // `body: Statement[]` represents `() => { expr }`.
  1613. constructor(params, body, type, sourceSpan) {
  1614. super(type, sourceSpan);
  1615. this.params = params;
  1616. this.body = body;
  1617. }
  1618. isEquivalent(e) {
  1619. if (!(e instanceof ArrowFunctionExpr) || !areAllEquivalent(this.params, e.params)) {
  1620. return false;
  1621. }
  1622. if (this.body instanceof Expression && e.body instanceof Expression) {
  1623. return this.body.isEquivalent(e.body);
  1624. }
  1625. if (Array.isArray(this.body) && Array.isArray(e.body)) {
  1626. return areAllEquivalent(this.body, e.body);
  1627. }
  1628. return false;
  1629. }
  1630. isConstant() {
  1631. return false;
  1632. }
  1633. visitExpression(visitor, context) {
  1634. return visitor.visitArrowFunctionExpr(this, context);
  1635. }
  1636. clone() {
  1637. // TODO: Should we deep clone statements?
  1638. return new ArrowFunctionExpr(this.params.map((p) => p.clone()), Array.isArray(this.body) ? this.body : this.body.clone(), this.type, this.sourceSpan);
  1639. }
  1640. toDeclStmt(name, modifiers) {
  1641. return new DeclareVarStmt(name, this, INFERRED_TYPE, modifiers, this.sourceSpan);
  1642. }
  1643. }
  1644. class UnaryOperatorExpr extends Expression {
  1645. operator;
  1646. expr;
  1647. parens;
  1648. constructor(operator, expr, type, sourceSpan, parens = true) {
  1649. super(type || NUMBER_TYPE, sourceSpan);
  1650. this.operator = operator;
  1651. this.expr = expr;
  1652. this.parens = parens;
  1653. }
  1654. isEquivalent(e) {
  1655. return (e instanceof UnaryOperatorExpr &&
  1656. this.operator === e.operator &&
  1657. this.expr.isEquivalent(e.expr));
  1658. }
  1659. isConstant() {
  1660. return false;
  1661. }
  1662. visitExpression(visitor, context) {
  1663. return visitor.visitUnaryOperatorExpr(this, context);
  1664. }
  1665. clone() {
  1666. return new UnaryOperatorExpr(this.operator, this.expr.clone(), this.type, this.sourceSpan, this.parens);
  1667. }
  1668. }
  1669. class BinaryOperatorExpr extends Expression {
  1670. operator;
  1671. rhs;
  1672. parens;
  1673. lhs;
  1674. constructor(operator, lhs, rhs, type, sourceSpan, parens = true) {
  1675. super(type || lhs.type, sourceSpan);
  1676. this.operator = operator;
  1677. this.rhs = rhs;
  1678. this.parens = parens;
  1679. this.lhs = lhs;
  1680. }
  1681. isEquivalent(e) {
  1682. return (e instanceof BinaryOperatorExpr &&
  1683. this.operator === e.operator &&
  1684. this.lhs.isEquivalent(e.lhs) &&
  1685. this.rhs.isEquivalent(e.rhs));
  1686. }
  1687. isConstant() {
  1688. return false;
  1689. }
  1690. visitExpression(visitor, context) {
  1691. return visitor.visitBinaryOperatorExpr(this, context);
  1692. }
  1693. clone() {
  1694. return new BinaryOperatorExpr(this.operator, this.lhs.clone(), this.rhs.clone(), this.type, this.sourceSpan, this.parens);
  1695. }
  1696. }
  1697. class ReadPropExpr extends Expression {
  1698. receiver;
  1699. name;
  1700. constructor(receiver, name, type, sourceSpan) {
  1701. super(type, sourceSpan);
  1702. this.receiver = receiver;
  1703. this.name = name;
  1704. }
  1705. // An alias for name, which allows other logic to handle property reads and keyed reads together.
  1706. get index() {
  1707. return this.name;
  1708. }
  1709. isEquivalent(e) {
  1710. return (e instanceof ReadPropExpr && this.receiver.isEquivalent(e.receiver) && this.name === e.name);
  1711. }
  1712. isConstant() {
  1713. return false;
  1714. }
  1715. visitExpression(visitor, context) {
  1716. return visitor.visitReadPropExpr(this, context);
  1717. }
  1718. set(value) {
  1719. return new WritePropExpr(this.receiver, this.name, value, null, this.sourceSpan);
  1720. }
  1721. clone() {
  1722. return new ReadPropExpr(this.receiver.clone(), this.name, this.type, this.sourceSpan);
  1723. }
  1724. }
  1725. class ReadKeyExpr extends Expression {
  1726. receiver;
  1727. index;
  1728. constructor(receiver, index, type, sourceSpan) {
  1729. super(type, sourceSpan);
  1730. this.receiver = receiver;
  1731. this.index = index;
  1732. }
  1733. isEquivalent(e) {
  1734. return (e instanceof ReadKeyExpr &&
  1735. this.receiver.isEquivalent(e.receiver) &&
  1736. this.index.isEquivalent(e.index));
  1737. }
  1738. isConstant() {
  1739. return false;
  1740. }
  1741. visitExpression(visitor, context) {
  1742. return visitor.visitReadKeyExpr(this, context);
  1743. }
  1744. set(value) {
  1745. return new WriteKeyExpr(this.receiver, this.index, value, null, this.sourceSpan);
  1746. }
  1747. clone() {
  1748. return new ReadKeyExpr(this.receiver.clone(), this.index.clone(), this.type, this.sourceSpan);
  1749. }
  1750. }
  1751. class LiteralArrayExpr extends Expression {
  1752. entries;
  1753. constructor(entries, type, sourceSpan) {
  1754. super(type, sourceSpan);
  1755. this.entries = entries;
  1756. }
  1757. isConstant() {
  1758. return this.entries.every((e) => e.isConstant());
  1759. }
  1760. isEquivalent(e) {
  1761. return e instanceof LiteralArrayExpr && areAllEquivalent(this.entries, e.entries);
  1762. }
  1763. visitExpression(visitor, context) {
  1764. return visitor.visitLiteralArrayExpr(this, context);
  1765. }
  1766. clone() {
  1767. return new LiteralArrayExpr(this.entries.map((e) => e.clone()), this.type, this.sourceSpan);
  1768. }
  1769. }
  1770. class LiteralMapEntry {
  1771. key;
  1772. value;
  1773. quoted;
  1774. constructor(key, value, quoted) {
  1775. this.key = key;
  1776. this.value = value;
  1777. this.quoted = quoted;
  1778. }
  1779. isEquivalent(e) {
  1780. return this.key === e.key && this.value.isEquivalent(e.value);
  1781. }
  1782. clone() {
  1783. return new LiteralMapEntry(this.key, this.value.clone(), this.quoted);
  1784. }
  1785. }
  1786. class LiteralMapExpr extends Expression {
  1787. entries;
  1788. valueType = null;
  1789. constructor(entries, type, sourceSpan) {
  1790. super(type, sourceSpan);
  1791. this.entries = entries;
  1792. if (type) {
  1793. this.valueType = type.valueType;
  1794. }
  1795. }
  1796. isEquivalent(e) {
  1797. return e instanceof LiteralMapExpr && areAllEquivalent(this.entries, e.entries);
  1798. }
  1799. isConstant() {
  1800. return this.entries.every((e) => e.value.isConstant());
  1801. }
  1802. visitExpression(visitor, context) {
  1803. return visitor.visitLiteralMapExpr(this, context);
  1804. }
  1805. clone() {
  1806. const entriesClone = this.entries.map((entry) => entry.clone());
  1807. return new LiteralMapExpr(entriesClone, this.type, this.sourceSpan);
  1808. }
  1809. }
  1810. const NULL_EXPR = new LiteralExpr(null, null, null);
  1811. const TYPED_NULL_EXPR = new LiteralExpr(null, INFERRED_TYPE, null);
  1812. //// Statements
  1813. exports.StmtModifier = void 0;
  1814. (function (StmtModifier) {
  1815. StmtModifier[StmtModifier["None"] = 0] = "None";
  1816. StmtModifier[StmtModifier["Final"] = 1] = "Final";
  1817. StmtModifier[StmtModifier["Private"] = 2] = "Private";
  1818. StmtModifier[StmtModifier["Exported"] = 4] = "Exported";
  1819. StmtModifier[StmtModifier["Static"] = 8] = "Static";
  1820. })(exports.StmtModifier || (exports.StmtModifier = {}));
  1821. class LeadingComment {
  1822. text;
  1823. multiline;
  1824. trailingNewline;
  1825. constructor(text, multiline, trailingNewline) {
  1826. this.text = text;
  1827. this.multiline = multiline;
  1828. this.trailingNewline = trailingNewline;
  1829. }
  1830. toString() {
  1831. return this.multiline ? ` ${this.text} ` : this.text;
  1832. }
  1833. }
  1834. class JSDocComment extends LeadingComment {
  1835. tags;
  1836. constructor(tags) {
  1837. super('', /* multiline */ true, /* trailingNewline */ true);
  1838. this.tags = tags;
  1839. }
  1840. toString() {
  1841. return serializeTags(this.tags);
  1842. }
  1843. }
  1844. class Statement {
  1845. modifiers;
  1846. sourceSpan;
  1847. leadingComments;
  1848. constructor(modifiers = exports.StmtModifier.None, sourceSpan = null, leadingComments) {
  1849. this.modifiers = modifiers;
  1850. this.sourceSpan = sourceSpan;
  1851. this.leadingComments = leadingComments;
  1852. }
  1853. hasModifier(modifier) {
  1854. return (this.modifiers & modifier) !== 0;
  1855. }
  1856. addLeadingComment(leadingComment) {
  1857. this.leadingComments = this.leadingComments ?? [];
  1858. this.leadingComments.push(leadingComment);
  1859. }
  1860. }
  1861. class DeclareVarStmt extends Statement {
  1862. name;
  1863. value;
  1864. type;
  1865. constructor(name, value, type, modifiers, sourceSpan, leadingComments) {
  1866. super(modifiers, sourceSpan, leadingComments);
  1867. this.name = name;
  1868. this.value = value;
  1869. this.type = type || (value && value.type) || null;
  1870. }
  1871. isEquivalent(stmt) {
  1872. return (stmt instanceof DeclareVarStmt &&
  1873. this.name === stmt.name &&
  1874. (this.value ? !!stmt.value && this.value.isEquivalent(stmt.value) : !stmt.value));
  1875. }
  1876. visitStatement(visitor, context) {
  1877. return visitor.visitDeclareVarStmt(this, context);
  1878. }
  1879. }
  1880. class DeclareFunctionStmt extends Statement {
  1881. name;
  1882. params;
  1883. statements;
  1884. type;
  1885. constructor(name, params, statements, type, modifiers, sourceSpan, leadingComments) {
  1886. super(modifiers, sourceSpan, leadingComments);
  1887. this.name = name;
  1888. this.params = params;
  1889. this.statements = statements;
  1890. this.type = type || null;
  1891. }
  1892. isEquivalent(stmt) {
  1893. return (stmt instanceof DeclareFunctionStmt &&
  1894. areAllEquivalent(this.params, stmt.params) &&
  1895. areAllEquivalent(this.statements, stmt.statements));
  1896. }
  1897. visitStatement(visitor, context) {
  1898. return visitor.visitDeclareFunctionStmt(this, context);
  1899. }
  1900. }
  1901. class ExpressionStatement extends Statement {
  1902. expr;
  1903. constructor(expr, sourceSpan, leadingComments) {
  1904. super(exports.StmtModifier.None, sourceSpan, leadingComments);
  1905. this.expr = expr;
  1906. }
  1907. isEquivalent(stmt) {
  1908. return stmt instanceof ExpressionStatement && this.expr.isEquivalent(stmt.expr);
  1909. }
  1910. visitStatement(visitor, context) {
  1911. return visitor.visitExpressionStmt(this, context);
  1912. }
  1913. }
  1914. class ReturnStatement extends Statement {
  1915. value;
  1916. constructor(value, sourceSpan = null, leadingComments) {
  1917. super(exports.StmtModifier.None, sourceSpan, leadingComments);
  1918. this.value = value;
  1919. }
  1920. isEquivalent(stmt) {
  1921. return stmt instanceof ReturnStatement && this.value.isEquivalent(stmt.value);
  1922. }
  1923. visitStatement(visitor, context) {
  1924. return visitor.visitReturnStmt(this, context);
  1925. }
  1926. }
  1927. class IfStmt extends Statement {
  1928. condition;
  1929. trueCase;
  1930. falseCase;
  1931. constructor(condition, trueCase, falseCase = [], sourceSpan, leadingComments) {
  1932. super(exports.StmtModifier.None, sourceSpan, leadingComments);
  1933. this.condition = condition;
  1934. this.trueCase = trueCase;
  1935. this.falseCase = falseCase;
  1936. }
  1937. isEquivalent(stmt) {
  1938. return (stmt instanceof IfStmt &&
  1939. this.condition.isEquivalent(stmt.condition) &&
  1940. areAllEquivalent(this.trueCase, stmt.trueCase) &&
  1941. areAllEquivalent(this.falseCase, stmt.falseCase));
  1942. }
  1943. visitStatement(visitor, context) {
  1944. return visitor.visitIfStmt(this, context);
  1945. }
  1946. }
  1947. let RecursiveAstVisitor$1 = class RecursiveAstVisitor {
  1948. visitType(ast, context) {
  1949. return ast;
  1950. }
  1951. visitExpression(ast, context) {
  1952. if (ast.type) {
  1953. ast.type.visitType(this, context);
  1954. }
  1955. return ast;
  1956. }
  1957. visitBuiltinType(type, context) {
  1958. return this.visitType(type, context);
  1959. }
  1960. visitExpressionType(type, context) {
  1961. type.value.visitExpression(this, context);
  1962. if (type.typeParams !== null) {
  1963. type.typeParams.forEach((param) => this.visitType(param, context));
  1964. }
  1965. return this.visitType(type, context);
  1966. }
  1967. visitArrayType(type, context) {
  1968. return this.visitType(type, context);
  1969. }
  1970. visitMapType(type, context) {
  1971. return this.visitType(type, context);
  1972. }
  1973. visitTransplantedType(type, context) {
  1974. return type;
  1975. }
  1976. visitWrappedNodeExpr(ast, context) {
  1977. return ast;
  1978. }
  1979. visitTypeofExpr(ast, context) {
  1980. return this.visitExpression(ast, context);
  1981. }
  1982. visitReadVarExpr(ast, context) {
  1983. return this.visitExpression(ast, context);
  1984. }
  1985. visitWriteVarExpr(ast, context) {
  1986. ast.value.visitExpression(this, context);
  1987. return this.visitExpression(ast, context);
  1988. }
  1989. visitWriteKeyExpr(ast, context) {
  1990. ast.receiver.visitExpression(this, context);
  1991. ast.index.visitExpression(this, context);
  1992. ast.value.visitExpression(this, context);
  1993. return this.visitExpression(ast, context);
  1994. }
  1995. visitWritePropExpr(ast, context) {
  1996. ast.receiver.visitExpression(this, context);
  1997. ast.value.visitExpression(this, context);
  1998. return this.visitExpression(ast, context);
  1999. }
  2000. visitDynamicImportExpr(ast, context) {
  2001. return this.visitExpression(ast, context);
  2002. }
  2003. visitInvokeFunctionExpr(ast, context) {
  2004. ast.fn.visitExpression(this, context);
  2005. this.visitAllExpressions(ast.args, context);
  2006. return this.visitExpression(ast, context);
  2007. }
  2008. visitTaggedTemplateLiteralExpr(ast, context) {
  2009. ast.tag.visitExpression(this, context);
  2010. ast.template.visitExpression(this, context);
  2011. return this.visitExpression(ast, context);
  2012. }
  2013. visitInstantiateExpr(ast, context) {
  2014. ast.classExpr.visitExpression(this, context);
  2015. this.visitAllExpressions(ast.args, context);
  2016. return this.visitExpression(ast, context);
  2017. }
  2018. visitLiteralExpr(ast, context) {
  2019. return this.visitExpression(ast, context);
  2020. }
  2021. visitLocalizedString(ast, context) {
  2022. return this.visitExpression(ast, context);
  2023. }
  2024. visitExternalExpr(ast, context) {
  2025. if (ast.typeParams) {
  2026. ast.typeParams.forEach((type) => type.visitType(this, context));
  2027. }
  2028. return this.visitExpression(ast, context);
  2029. }
  2030. visitConditionalExpr(ast, context) {
  2031. ast.condition.visitExpression(this, context);
  2032. ast.trueCase.visitExpression(this, context);
  2033. ast.falseCase.visitExpression(this, context);
  2034. return this.visitExpression(ast, context);
  2035. }
  2036. visitNotExpr(ast, context) {
  2037. ast.condition.visitExpression(this, context);
  2038. return this.visitExpression(ast, context);
  2039. }
  2040. visitFunctionExpr(ast, context) {
  2041. this.visitAllStatements(ast.statements, context);
  2042. return this.visitExpression(ast, context);
  2043. }
  2044. visitArrowFunctionExpr(ast, context) {
  2045. if (Array.isArray(ast.body)) {
  2046. this.visitAllStatements(ast.body, context);
  2047. }
  2048. else {
  2049. // Note: `body.visitExpression`, rather than `this.visitExpressiont(body)`,
  2050. // because the latter won't recurse into the sub-expressions.
  2051. ast.body.visitExpression(this, context);
  2052. }
  2053. return this.visitExpression(ast, context);
  2054. }
  2055. visitUnaryOperatorExpr(ast, context) {
  2056. ast.expr.visitExpression(this, context);
  2057. return this.visitExpression(ast, context);
  2058. }
  2059. visitBinaryOperatorExpr(ast, context) {
  2060. ast.lhs.visitExpression(this, context);
  2061. ast.rhs.visitExpression(this, context);
  2062. return this.visitExpression(ast, context);
  2063. }
  2064. visitReadPropExpr(ast, context) {
  2065. ast.receiver.visitExpression(this, context);
  2066. return this.visitExpression(ast, context);
  2067. }
  2068. visitReadKeyExpr(ast, context) {
  2069. ast.receiver.visitExpression(this, context);
  2070. ast.index.visitExpression(this, context);
  2071. return this.visitExpression(ast, context);
  2072. }
  2073. visitLiteralArrayExpr(ast, context) {
  2074. this.visitAllExpressions(ast.entries, context);
  2075. return this.visitExpression(ast, context);
  2076. }
  2077. visitLiteralMapExpr(ast, context) {
  2078. ast.entries.forEach((entry) => entry.value.visitExpression(this, context));
  2079. return this.visitExpression(ast, context);
  2080. }
  2081. visitCommaExpr(ast, context) {
  2082. this.visitAllExpressions(ast.parts, context);
  2083. return this.visitExpression(ast, context);
  2084. }
  2085. visitTemplateLiteralExpr(ast, context) {
  2086. this.visitAllExpressions(ast.elements, context);
  2087. this.visitAllExpressions(ast.expressions, context);
  2088. return this.visitExpression(ast, context);
  2089. }
  2090. visitTemplateLiteralElementExpr(ast, context) {
  2091. return this.visitExpression(ast, context);
  2092. }
  2093. visitAllExpressions(exprs, context) {
  2094. exprs.forEach((expr) => expr.visitExpression(this, context));
  2095. }
  2096. visitDeclareVarStmt(stmt, context) {
  2097. if (stmt.value) {
  2098. stmt.value.visitExpression(this, context);
  2099. }
  2100. if (stmt.type) {
  2101. stmt.type.visitType(this, context);
  2102. }
  2103. return stmt;
  2104. }
  2105. visitDeclareFunctionStmt(stmt, context) {
  2106. this.visitAllStatements(stmt.statements, context);
  2107. if (stmt.type) {
  2108. stmt.type.visitType(this, context);
  2109. }
  2110. return stmt;
  2111. }
  2112. visitExpressionStmt(stmt, context) {
  2113. stmt.expr.visitExpression(this, context);
  2114. return stmt;
  2115. }
  2116. visitReturnStmt(stmt, context) {
  2117. stmt.value.visitExpression(this, context);
  2118. return stmt;
  2119. }
  2120. visitIfStmt(stmt, context) {
  2121. stmt.condition.visitExpression(this, context);
  2122. this.visitAllStatements(stmt.trueCase, context);
  2123. this.visitAllStatements(stmt.falseCase, context);
  2124. return stmt;
  2125. }
  2126. visitAllStatements(stmts, context) {
  2127. stmts.forEach((stmt) => stmt.visitStatement(this, context));
  2128. }
  2129. };
  2130. function leadingComment(text, multiline = false, trailingNewline = true) {
  2131. return new LeadingComment(text, multiline, trailingNewline);
  2132. }
  2133. function jsDocComment(tags = []) {
  2134. return new JSDocComment(tags);
  2135. }
  2136. function variable(name, type, sourceSpan) {
  2137. return new ReadVarExpr(name, type, sourceSpan);
  2138. }
  2139. function importExpr(id, typeParams = null, sourceSpan) {
  2140. return new ExternalExpr(id, null, typeParams, sourceSpan);
  2141. }
  2142. function expressionType(expr, typeModifiers, typeParams) {
  2143. return new ExpressionType(expr, typeModifiers, typeParams);
  2144. }
  2145. function transplantedType(type, typeModifiers) {
  2146. return new TransplantedType(type, typeModifiers);
  2147. }
  2148. function typeofExpr(expr) {
  2149. return new TypeofExpr(expr);
  2150. }
  2151. function literalArr(values, type, sourceSpan) {
  2152. return new LiteralArrayExpr(values, type, sourceSpan);
  2153. }
  2154. function literalMap(values, type = null) {
  2155. return new LiteralMapExpr(values.map((e) => new LiteralMapEntry(e.key, e.value, e.quoted)), type, null);
  2156. }
  2157. function not(expr, sourceSpan) {
  2158. return new NotExpr(expr, sourceSpan);
  2159. }
  2160. function fn(params, body, type, sourceSpan, name) {
  2161. return new FunctionExpr(params, body, type, sourceSpan, name);
  2162. }
  2163. function arrowFn(params, body, type, sourceSpan) {
  2164. return new ArrowFunctionExpr(params, body, type, sourceSpan);
  2165. }
  2166. function ifStmt(condition, thenClause, elseClause, sourceSpan, leadingComments) {
  2167. return new IfStmt(condition, thenClause, elseClause, sourceSpan, leadingComments);
  2168. }
  2169. function taggedTemplate(tag, template, type, sourceSpan) {
  2170. return new TaggedTemplateLiteralExpr(tag, template, type, sourceSpan);
  2171. }
  2172. function literal$1(value, type, sourceSpan) {
  2173. return new LiteralExpr(value, type, sourceSpan);
  2174. }
  2175. function localizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan) {
  2176. return new LocalizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan);
  2177. }
  2178. /*
  2179. * Serializes a `Tag` into a string.
  2180. * Returns a string like " @foo {bar} baz" (note the leading whitespace before `@foo`).
  2181. */
  2182. function tagToString(tag) {
  2183. let out = '';
  2184. if (tag.tagName) {
  2185. out += ` @${tag.tagName}`;
  2186. }
  2187. if (tag.text) {
  2188. if (tag.text.match(/\/\*|\*\//)) {
  2189. throw new Error('JSDoc text cannot contain "/*" and "*/"');
  2190. }
  2191. out += ' ' + tag.text.replace(/@/g, '\\@');
  2192. }
  2193. return out;
  2194. }
  2195. function serializeTags(tags) {
  2196. if (tags.length === 0)
  2197. return '';
  2198. if (tags.length === 1 && tags[0].tagName && !tags[0].text) {
  2199. // The JSDOC comment is a single simple tag: e.g `/** @tagname */`.
  2200. return `*${tagToString(tags[0])} `;
  2201. }
  2202. let out = '*\n';
  2203. for (const tag of tags) {
  2204. out += ' *';
  2205. // If the tagToString is multi-line, insert " * " prefixes on lines.
  2206. out += tagToString(tag).replace(/\n/g, '\n * ');
  2207. out += '\n';
  2208. }
  2209. out += ' ';
  2210. return out;
  2211. }
  2212. const CONSTANT_PREFIX = '_c';
  2213. /**
  2214. * `ConstantPool` tries to reuse literal factories when two or more literals are identical.
  2215. * We determine whether literals are identical by creating a key out of their AST using the
  2216. * `KeyVisitor`. This constant is used to replace dynamic expressions which can't be safely
  2217. * converted into a key. E.g. given an expression `{foo: bar()}`, since we don't know what
  2218. * the result of `bar` will be, we create a key that looks like `{foo: <unknown>}`. Note
  2219. * that we use a variable, rather than something like `null` in order to avoid collisions.
  2220. */
  2221. const UNKNOWN_VALUE_KEY = variable('<unknown>');
  2222. /**
  2223. * Context to use when producing a key.
  2224. *
  2225. * This ensures we see the constant not the reference variable when producing
  2226. * a key.
  2227. */
  2228. const KEY_CONTEXT = {};
  2229. /**
  2230. * Generally all primitive values are excluded from the `ConstantPool`, but there is an exclusion
  2231. * for strings that reach a certain length threshold. This constant defines the length threshold for
  2232. * strings.
  2233. */
  2234. const POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS = 50;
  2235. /**
  2236. * A node that is a place-holder that allows the node to be replaced when the actual
  2237. * node is known.
  2238. *
  2239. * This allows the constant pool to change an expression from a direct reference to
  2240. * a constant to a shared constant. It returns a fix-up node that is later allowed to
  2241. * change the referenced expression.
  2242. */
  2243. class FixupExpression extends Expression {
  2244. resolved;
  2245. original;
  2246. shared = false;
  2247. constructor(resolved) {
  2248. super(resolved.type);
  2249. this.resolved = resolved;
  2250. this.original = resolved;
  2251. }
  2252. visitExpression(visitor, context) {
  2253. if (context === KEY_CONTEXT) {
  2254. // When producing a key we want to traverse the constant not the
  2255. // variable used to refer to it.
  2256. return this.original.visitExpression(visitor, context);
  2257. }
  2258. else {
  2259. return this.resolved.visitExpression(visitor, context);
  2260. }
  2261. }
  2262. isEquivalent(e) {
  2263. return e instanceof FixupExpression && this.resolved.isEquivalent(e.resolved);
  2264. }
  2265. isConstant() {
  2266. return true;
  2267. }
  2268. clone() {
  2269. throw new Error(`Not supported.`);
  2270. }
  2271. fixup(expression) {
  2272. this.resolved = expression;
  2273. this.shared = true;
  2274. }
  2275. }
  2276. /**
  2277. * A constant pool allows a code emitter to share constant in an output context.
  2278. *
  2279. * The constant pool also supports sharing access to ivy definitions references.
  2280. */
  2281. class ConstantPool {
  2282. isClosureCompilerEnabled;
  2283. statements = [];
  2284. literals = new Map();
  2285. literalFactories = new Map();
  2286. sharedConstants = new Map();
  2287. /**
  2288. * Constant pool also tracks claimed names from {@link uniqueName}.
  2289. * This is useful to avoid collisions if variables are intended to be
  2290. * named a certain way- but may conflict. We wouldn't want to always suffix
  2291. * them with unique numbers.
  2292. */
  2293. _claimedNames = new Map();
  2294. nextNameIndex = 0;
  2295. constructor(isClosureCompilerEnabled = false) {
  2296. this.isClosureCompilerEnabled = isClosureCompilerEnabled;
  2297. }
  2298. getConstLiteral(literal, forceShared) {
  2299. if ((literal instanceof LiteralExpr && !isLongStringLiteral(literal)) ||
  2300. literal instanceof FixupExpression) {
  2301. // Do no put simple literals into the constant pool or try to produce a constant for a
  2302. // reference to a constant.
  2303. return literal;
  2304. }
  2305. const key = GenericKeyFn.INSTANCE.keyOf(literal);
  2306. let fixup = this.literals.get(key);
  2307. let newValue = false;
  2308. if (!fixup) {
  2309. fixup = new FixupExpression(literal);
  2310. this.literals.set(key, fixup);
  2311. newValue = true;
  2312. }
  2313. if ((!newValue && !fixup.shared) || (newValue && forceShared)) {
  2314. // Replace the expression with a variable
  2315. const name = this.freshName();
  2316. let definition;
  2317. let usage;
  2318. if (this.isClosureCompilerEnabled && isLongStringLiteral(literal)) {
  2319. // For string literals, Closure will **always** inline the string at
  2320. // **all** usages, duplicating it each time. For large strings, this
  2321. // unnecessarily bloats bundle size. To work around this restriction, we
  2322. // wrap the string in a function, and call that function for each usage.
  2323. // This tricks Closure into using inline logic for functions instead of
  2324. // string literals. Function calls are only inlined if the body is small
  2325. // enough to be worth it. By doing this, very large strings will be
  2326. // shared across multiple usages, rather than duplicating the string at
  2327. // each usage site.
  2328. //
  2329. // const myStr = function() { return "very very very long string"; };
  2330. // const usage1 = myStr();
  2331. // const usage2 = myStr();
  2332. definition = variable(name).set(new FunctionExpr([], // Params.
  2333. [
  2334. // Statements.
  2335. new ReturnStatement(literal),
  2336. ]));
  2337. usage = variable(name).callFn([]);
  2338. }
  2339. else {
  2340. // Just declare and use the variable directly, without a function call
  2341. // indirection. This saves a few bytes and avoids an unnecessary call.
  2342. definition = variable(name).set(literal);
  2343. usage = variable(name);
  2344. }
  2345. this.statements.push(definition.toDeclStmt(INFERRED_TYPE, exports.StmtModifier.Final));
  2346. fixup.fixup(usage);
  2347. }
  2348. return fixup;
  2349. }
  2350. getSharedConstant(def, expr) {
  2351. const key = def.keyOf(expr);
  2352. if (!this.sharedConstants.has(key)) {
  2353. const id = this.freshName();
  2354. this.sharedConstants.set(key, variable(id));
  2355. this.statements.push(def.toSharedConstantDeclaration(id, expr));
  2356. }
  2357. return this.sharedConstants.get(key);
  2358. }
  2359. getLiteralFactory(literal) {
  2360. // Create a pure function that builds an array of a mix of constant and variable expressions
  2361. if (literal instanceof LiteralArrayExpr) {
  2362. const argumentsForKey = literal.entries.map((e) => (e.isConstant() ? e : UNKNOWN_VALUE_KEY));
  2363. const key = GenericKeyFn.INSTANCE.keyOf(literalArr(argumentsForKey));
  2364. return this._getLiteralFactory(key, literal.entries, (entries) => literalArr(entries));
  2365. }
  2366. else {
  2367. const expressionForKey = literalMap(literal.entries.map((e) => ({
  2368. key: e.key,
  2369. value: e.value.isConstant() ? e.value : UNKNOWN_VALUE_KEY,
  2370. quoted: e.quoted,
  2371. })));
  2372. const key = GenericKeyFn.INSTANCE.keyOf(expressionForKey);
  2373. return this._getLiteralFactory(key, literal.entries.map((e) => e.value), (entries) => literalMap(entries.map((value, index) => ({
  2374. key: literal.entries[index].key,
  2375. value,
  2376. quoted: literal.entries[index].quoted,
  2377. }))));
  2378. }
  2379. }
  2380. // TODO: useUniqueName(false) is necessary for naming compatibility with
  2381. // TemplateDefinitionBuilder, but should be removed once Template Pipeline is the default.
  2382. getSharedFunctionReference(fn, prefix, useUniqueName = true) {
  2383. const isArrow = fn instanceof ArrowFunctionExpr;
  2384. for (const current of this.statements) {
  2385. // Arrow functions are saved as variables so we check if the
  2386. // value of the variable is the same as the arrow function.
  2387. if (isArrow && current instanceof DeclareVarStmt && current.value?.isEquivalent(fn)) {
  2388. return variable(current.name);
  2389. }
  2390. // Function declarations are saved as function statements
  2391. // so we compare them directly to the passed-in function.
  2392. if (!isArrow &&
  2393. current instanceof DeclareFunctionStmt &&
  2394. fn instanceof FunctionExpr &&
  2395. fn.isEquivalent(current)) {
  2396. return variable(current.name);
  2397. }
  2398. }
  2399. // Otherwise declare the function.
  2400. const name = useUniqueName ? this.uniqueName(prefix) : prefix;
  2401. this.statements.push(fn instanceof FunctionExpr
  2402. ? fn.toDeclStmt(name, exports.StmtModifier.Final)
  2403. : new DeclareVarStmt(name, fn, INFERRED_TYPE, exports.StmtModifier.Final, fn.sourceSpan));
  2404. return variable(name);
  2405. }
  2406. _getLiteralFactory(key, values, resultMap) {
  2407. let literalFactory = this.literalFactories.get(key);
  2408. const literalFactoryArguments = values.filter((e) => !e.isConstant());
  2409. if (!literalFactory) {
  2410. const resultExpressions = values.map((e, index) => e.isConstant() ? this.getConstLiteral(e, true) : variable(`a${index}`));
  2411. const parameters = resultExpressions
  2412. .filter(isVariable)
  2413. .map((e) => new FnParam(e.name, DYNAMIC_TYPE));
  2414. const pureFunctionDeclaration = arrowFn(parameters, resultMap(resultExpressions), INFERRED_TYPE);
  2415. const name = this.freshName();
  2416. this.statements.push(variable(name)
  2417. .set(pureFunctionDeclaration)
  2418. .toDeclStmt(INFERRED_TYPE, exports.StmtModifier.Final));
  2419. literalFactory = variable(name);
  2420. this.literalFactories.set(key, literalFactory);
  2421. }
  2422. return { literalFactory, literalFactoryArguments };
  2423. }
  2424. /**
  2425. * Produce a unique name in the context of this pool.
  2426. *
  2427. * The name might be unique among different prefixes if any of the prefixes end in
  2428. * a digit so the prefix should be a constant string (not based on user input) and
  2429. * must not end in a digit.
  2430. */
  2431. uniqueName(name, alwaysIncludeSuffix = true) {
  2432. const count = this._claimedNames.get(name) ?? 0;
  2433. const result = count === 0 && !alwaysIncludeSuffix ? `${name}` : `${name}${count}`;
  2434. this._claimedNames.set(name, count + 1);
  2435. return result;
  2436. }
  2437. freshName() {
  2438. return this.uniqueName(CONSTANT_PREFIX);
  2439. }
  2440. }
  2441. class GenericKeyFn {
  2442. static INSTANCE = new GenericKeyFn();
  2443. keyOf(expr) {
  2444. if (expr instanceof LiteralExpr && typeof expr.value === 'string') {
  2445. return `"${expr.value}"`;
  2446. }
  2447. else if (expr instanceof LiteralExpr) {
  2448. return String(expr.value);
  2449. }
  2450. else if (expr instanceof LiteralArrayExpr) {
  2451. const entries = [];
  2452. for (const entry of expr.entries) {
  2453. entries.push(this.keyOf(entry));
  2454. }
  2455. return `[${entries.join(',')}]`;
  2456. }
  2457. else if (expr instanceof LiteralMapExpr) {
  2458. const entries = [];
  2459. for (const entry of expr.entries) {
  2460. let key = entry.key;
  2461. if (entry.quoted) {
  2462. key = `"${key}"`;
  2463. }
  2464. entries.push(key + ':' + this.keyOf(entry.value));
  2465. }
  2466. return `{${entries.join(',')}}`;
  2467. }
  2468. else if (expr instanceof ExternalExpr) {
  2469. return `import("${expr.value.moduleName}", ${expr.value.name})`;
  2470. }
  2471. else if (expr instanceof ReadVarExpr) {
  2472. return `read(${expr.name})`;
  2473. }
  2474. else if (expr instanceof TypeofExpr) {
  2475. return `typeof(${this.keyOf(expr.expr)})`;
  2476. }
  2477. else {
  2478. throw new Error(`${this.constructor.name} does not handle expressions of type ${expr.constructor.name}`);
  2479. }
  2480. }
  2481. }
  2482. function isVariable(e) {
  2483. return e instanceof ReadVarExpr;
  2484. }
  2485. function isLongStringLiteral(expr) {
  2486. return (expr instanceof LiteralExpr &&
  2487. typeof expr.value === 'string' &&
  2488. expr.value.length >= POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS);
  2489. }
  2490. const CORE = '@angular/core';
  2491. class Identifiers {
  2492. /* Methods */
  2493. static NEW_METHOD = 'factory';
  2494. static TRANSFORM_METHOD = 'transform';
  2495. static PATCH_DEPS = 'patchedDeps';
  2496. static core = { name: null, moduleName: CORE };
  2497. /* Instructions */
  2498. static namespaceHTML = { name: 'ɵɵnamespaceHTML', moduleName: CORE };
  2499. static namespaceMathML = { name: 'ɵɵnamespaceMathML', moduleName: CORE };
  2500. static namespaceSVG = { name: 'ɵɵnamespaceSVG', moduleName: CORE };
  2501. static element = { name: 'ɵɵelement', moduleName: CORE };
  2502. static elementStart = { name: 'ɵɵelementStart', moduleName: CORE };
  2503. static elementEnd = { name: 'ɵɵelementEnd', moduleName: CORE };
  2504. static advance = { name: 'ɵɵadvance', moduleName: CORE };
  2505. static syntheticHostProperty = {
  2506. name: 'ɵɵsyntheticHostProperty',
  2507. moduleName: CORE,
  2508. };
  2509. static syntheticHostListener = {
  2510. name: 'ɵɵsyntheticHostListener',
  2511. moduleName: CORE,
  2512. };
  2513. static attribute = { name: 'ɵɵattribute', moduleName: CORE };
  2514. static attributeInterpolate1 = {
  2515. name: 'ɵɵattributeInterpolate1',
  2516. moduleName: CORE,
  2517. };
  2518. static attributeInterpolate2 = {
  2519. name: 'ɵɵattributeInterpolate2',
  2520. moduleName: CORE,
  2521. };
  2522. static attributeInterpolate3 = {
  2523. name: 'ɵɵattributeInterpolate3',
  2524. moduleName: CORE,
  2525. };
  2526. static attributeInterpolate4 = {
  2527. name: 'ɵɵattributeInterpolate4',
  2528. moduleName: CORE,
  2529. };
  2530. static attributeInterpolate5 = {
  2531. name: 'ɵɵattributeInterpolate5',
  2532. moduleName: CORE,
  2533. };
  2534. static attributeInterpolate6 = {
  2535. name: 'ɵɵattributeInterpolate6',
  2536. moduleName: CORE,
  2537. };
  2538. static attributeInterpolate7 = {
  2539. name: 'ɵɵattributeInterpolate7',
  2540. moduleName: CORE,
  2541. };
  2542. static attributeInterpolate8 = {
  2543. name: 'ɵɵattributeInterpolate8',
  2544. moduleName: CORE,
  2545. };
  2546. static attributeInterpolateV = {
  2547. name: 'ɵɵattributeInterpolateV',
  2548. moduleName: CORE,
  2549. };
  2550. static classProp = { name: 'ɵɵclassProp', moduleName: CORE };
  2551. static elementContainerStart = {
  2552. name: 'ɵɵelementContainerStart',
  2553. moduleName: CORE,
  2554. };
  2555. static elementContainerEnd = {
  2556. name: 'ɵɵelementContainerEnd',
  2557. moduleName: CORE,
  2558. };
  2559. static elementContainer = { name: 'ɵɵelementContainer', moduleName: CORE };
  2560. static styleMap = { name: 'ɵɵstyleMap', moduleName: CORE };
  2561. static styleMapInterpolate1 = {
  2562. name: 'ɵɵstyleMapInterpolate1',
  2563. moduleName: CORE,
  2564. };
  2565. static styleMapInterpolate2 = {
  2566. name: 'ɵɵstyleMapInterpolate2',
  2567. moduleName: CORE,
  2568. };
  2569. static styleMapInterpolate3 = {
  2570. name: 'ɵɵstyleMapInterpolate3',
  2571. moduleName: CORE,
  2572. };
  2573. static styleMapInterpolate4 = {
  2574. name: 'ɵɵstyleMapInterpolate4',
  2575. moduleName: CORE,
  2576. };
  2577. static styleMapInterpolate5 = {
  2578. name: 'ɵɵstyleMapInterpolate5',
  2579. moduleName: CORE,
  2580. };
  2581. static styleMapInterpolate6 = {
  2582. name: 'ɵɵstyleMapInterpolate6',
  2583. moduleName: CORE,
  2584. };
  2585. static styleMapInterpolate7 = {
  2586. name: 'ɵɵstyleMapInterpolate7',
  2587. moduleName: CORE,
  2588. };
  2589. static styleMapInterpolate8 = {
  2590. name: 'ɵɵstyleMapInterpolate8',
  2591. moduleName: CORE,
  2592. };
  2593. static styleMapInterpolateV = {
  2594. name: 'ɵɵstyleMapInterpolateV',
  2595. moduleName: CORE,
  2596. };
  2597. static classMap = { name: 'ɵɵclassMap', moduleName: CORE };
  2598. static classMapInterpolate1 = {
  2599. name: 'ɵɵclassMapInterpolate1',
  2600. moduleName: CORE,
  2601. };
  2602. static classMapInterpolate2 = {
  2603. name: 'ɵɵclassMapInterpolate2',
  2604. moduleName: CORE,
  2605. };
  2606. static classMapInterpolate3 = {
  2607. name: 'ɵɵclassMapInterpolate3',
  2608. moduleName: CORE,
  2609. };
  2610. static classMapInterpolate4 = {
  2611. name: 'ɵɵclassMapInterpolate4',
  2612. moduleName: CORE,
  2613. };
  2614. static classMapInterpolate5 = {
  2615. name: 'ɵɵclassMapInterpolate5',
  2616. moduleName: CORE,
  2617. };
  2618. static classMapInterpolate6 = {
  2619. name: 'ɵɵclassMapInterpolate6',
  2620. moduleName: CORE,
  2621. };
  2622. static classMapInterpolate7 = {
  2623. name: 'ɵɵclassMapInterpolate7',
  2624. moduleName: CORE,
  2625. };
  2626. static classMapInterpolate8 = {
  2627. name: 'ɵɵclassMapInterpolate8',
  2628. moduleName: CORE,
  2629. };
  2630. static classMapInterpolateV = {
  2631. name: 'ɵɵclassMapInterpolateV',
  2632. moduleName: CORE,
  2633. };
  2634. static styleProp = { name: 'ɵɵstyleProp', moduleName: CORE };
  2635. static stylePropInterpolate1 = {
  2636. name: 'ɵɵstylePropInterpolate1',
  2637. moduleName: CORE,
  2638. };
  2639. static stylePropInterpolate2 = {
  2640. name: 'ɵɵstylePropInterpolate2',
  2641. moduleName: CORE,
  2642. };
  2643. static stylePropInterpolate3 = {
  2644. name: 'ɵɵstylePropInterpolate3',
  2645. moduleName: CORE,
  2646. };
  2647. static stylePropInterpolate4 = {
  2648. name: 'ɵɵstylePropInterpolate4',
  2649. moduleName: CORE,
  2650. };
  2651. static stylePropInterpolate5 = {
  2652. name: 'ɵɵstylePropInterpolate5',
  2653. moduleName: CORE,
  2654. };
  2655. static stylePropInterpolate6 = {
  2656. name: 'ɵɵstylePropInterpolate6',
  2657. moduleName: CORE,
  2658. };
  2659. static stylePropInterpolate7 = {
  2660. name: 'ɵɵstylePropInterpolate7',
  2661. moduleName: CORE,
  2662. };
  2663. static stylePropInterpolate8 = {
  2664. name: 'ɵɵstylePropInterpolate8',
  2665. moduleName: CORE,
  2666. };
  2667. static stylePropInterpolateV = {
  2668. name: 'ɵɵstylePropInterpolateV',
  2669. moduleName: CORE,
  2670. };
  2671. static nextContext = { name: 'ɵɵnextContext', moduleName: CORE };
  2672. static resetView = { name: 'ɵɵresetView', moduleName: CORE };
  2673. static templateCreate = { name: 'ɵɵtemplate', moduleName: CORE };
  2674. static defer = { name: 'ɵɵdefer', moduleName: CORE };
  2675. static deferWhen = { name: 'ɵɵdeferWhen', moduleName: CORE };
  2676. static deferOnIdle = { name: 'ɵɵdeferOnIdle', moduleName: CORE };
  2677. static deferOnImmediate = { name: 'ɵɵdeferOnImmediate', moduleName: CORE };
  2678. static deferOnTimer = { name: 'ɵɵdeferOnTimer', moduleName: CORE };
  2679. static deferOnHover = { name: 'ɵɵdeferOnHover', moduleName: CORE };
  2680. static deferOnInteraction = { name: 'ɵɵdeferOnInteraction', moduleName: CORE };
  2681. static deferOnViewport = { name: 'ɵɵdeferOnViewport', moduleName: CORE };
  2682. static deferPrefetchWhen = { name: 'ɵɵdeferPrefetchWhen', moduleName: CORE };
  2683. static deferPrefetchOnIdle = {
  2684. name: 'ɵɵdeferPrefetchOnIdle',
  2685. moduleName: CORE,
  2686. };
  2687. static deferPrefetchOnImmediate = {
  2688. name: 'ɵɵdeferPrefetchOnImmediate',
  2689. moduleName: CORE,
  2690. };
  2691. static deferPrefetchOnTimer = {
  2692. name: 'ɵɵdeferPrefetchOnTimer',
  2693. moduleName: CORE,
  2694. };
  2695. static deferPrefetchOnHover = {
  2696. name: 'ɵɵdeferPrefetchOnHover',
  2697. moduleName: CORE,
  2698. };
  2699. static deferPrefetchOnInteraction = {
  2700. name: 'ɵɵdeferPrefetchOnInteraction',
  2701. moduleName: CORE,
  2702. };
  2703. static deferPrefetchOnViewport = {
  2704. name: 'ɵɵdeferPrefetchOnViewport',
  2705. moduleName: CORE,
  2706. };
  2707. static deferHydrateWhen = { name: 'ɵɵdeferHydrateWhen', moduleName: CORE };
  2708. static deferHydrateNever = { name: 'ɵɵdeferHydrateNever', moduleName: CORE };
  2709. static deferHydrateOnIdle = {
  2710. name: 'ɵɵdeferHydrateOnIdle',
  2711. moduleName: CORE,
  2712. };
  2713. static deferHydrateOnImmediate = {
  2714. name: 'ɵɵdeferHydrateOnImmediate',
  2715. moduleName: CORE,
  2716. };
  2717. static deferHydrateOnTimer = {
  2718. name: 'ɵɵdeferHydrateOnTimer',
  2719. moduleName: CORE,
  2720. };
  2721. static deferHydrateOnHover = {
  2722. name: 'ɵɵdeferHydrateOnHover',
  2723. moduleName: CORE,
  2724. };
  2725. static deferHydrateOnInteraction = {
  2726. name: 'ɵɵdeferHydrateOnInteraction',
  2727. moduleName: CORE,
  2728. };
  2729. static deferHydrateOnViewport = {
  2730. name: 'ɵɵdeferHydrateOnViewport',
  2731. moduleName: CORE,
  2732. };
  2733. static deferEnableTimerScheduling = {
  2734. name: 'ɵɵdeferEnableTimerScheduling',
  2735. moduleName: CORE,
  2736. };
  2737. static conditional = { name: 'ɵɵconditional', moduleName: CORE };
  2738. static repeater = { name: 'ɵɵrepeater', moduleName: CORE };
  2739. static repeaterCreate = { name: 'ɵɵrepeaterCreate', moduleName: CORE };
  2740. static repeaterTrackByIndex = {
  2741. name: 'ɵɵrepeaterTrackByIndex',
  2742. moduleName: CORE,
  2743. };
  2744. static repeaterTrackByIdentity = {
  2745. name: 'ɵɵrepeaterTrackByIdentity',
  2746. moduleName: CORE,
  2747. };
  2748. static componentInstance = { name: 'ɵɵcomponentInstance', moduleName: CORE };
  2749. static text = { name: 'ɵɵtext', moduleName: CORE };
  2750. static enableBindings = { name: 'ɵɵenableBindings', moduleName: CORE };
  2751. static disableBindings = { name: 'ɵɵdisableBindings', moduleName: CORE };
  2752. static getCurrentView = { name: 'ɵɵgetCurrentView', moduleName: CORE };
  2753. static textInterpolate = { name: 'ɵɵtextInterpolate', moduleName: CORE };
  2754. static textInterpolate1 = { name: 'ɵɵtextInterpolate1', moduleName: CORE };
  2755. static textInterpolate2 = { name: 'ɵɵtextInterpolate2', moduleName: CORE };
  2756. static textInterpolate3 = { name: 'ɵɵtextInterpolate3', moduleName: CORE };
  2757. static textInterpolate4 = { name: 'ɵɵtextInterpolate4', moduleName: CORE };
  2758. static textInterpolate5 = { name: 'ɵɵtextInterpolate5', moduleName: CORE };
  2759. static textInterpolate6 = { name: 'ɵɵtextInterpolate6', moduleName: CORE };
  2760. static textInterpolate7 = { name: 'ɵɵtextInterpolate7', moduleName: CORE };
  2761. static textInterpolate8 = { name: 'ɵɵtextInterpolate8', moduleName: CORE };
  2762. static textInterpolateV = { name: 'ɵɵtextInterpolateV', moduleName: CORE };
  2763. static restoreView = { name: 'ɵɵrestoreView', moduleName: CORE };
  2764. static pureFunction0 = { name: 'ɵɵpureFunction0', moduleName: CORE };
  2765. static pureFunction1 = { name: 'ɵɵpureFunction1', moduleName: CORE };
  2766. static pureFunction2 = { name: 'ɵɵpureFunction2', moduleName: CORE };
  2767. static pureFunction3 = { name: 'ɵɵpureFunction3', moduleName: CORE };
  2768. static pureFunction4 = { name: 'ɵɵpureFunction4', moduleName: CORE };
  2769. static pureFunction5 = { name: 'ɵɵpureFunction5', moduleName: CORE };
  2770. static pureFunction6 = { name: 'ɵɵpureFunction6', moduleName: CORE };
  2771. static pureFunction7 = { name: 'ɵɵpureFunction7', moduleName: CORE };
  2772. static pureFunction8 = { name: 'ɵɵpureFunction8', moduleName: CORE };
  2773. static pureFunctionV = { name: 'ɵɵpureFunctionV', moduleName: CORE };
  2774. static pipeBind1 = { name: 'ɵɵpipeBind1', moduleName: CORE };
  2775. static pipeBind2 = { name: 'ɵɵpipeBind2', moduleName: CORE };
  2776. static pipeBind3 = { name: 'ɵɵpipeBind3', moduleName: CORE };
  2777. static pipeBind4 = { name: 'ɵɵpipeBind4', moduleName: CORE };
  2778. static pipeBindV = { name: 'ɵɵpipeBindV', moduleName: CORE };
  2779. static hostProperty = { name: 'ɵɵhostProperty', moduleName: CORE };
  2780. static property = { name: 'ɵɵproperty', moduleName: CORE };
  2781. static propertyInterpolate = {
  2782. name: 'ɵɵpropertyInterpolate',
  2783. moduleName: CORE,
  2784. };
  2785. static propertyInterpolate1 = {
  2786. name: 'ɵɵpropertyInterpolate1',
  2787. moduleName: CORE,
  2788. };
  2789. static propertyInterpolate2 = {
  2790. name: 'ɵɵpropertyInterpolate2',
  2791. moduleName: CORE,
  2792. };
  2793. static propertyInterpolate3 = {
  2794. name: 'ɵɵpropertyInterpolate3',
  2795. moduleName: CORE,
  2796. };
  2797. static propertyInterpolate4 = {
  2798. name: 'ɵɵpropertyInterpolate4',
  2799. moduleName: CORE,
  2800. };
  2801. static propertyInterpolate5 = {
  2802. name: 'ɵɵpropertyInterpolate5',
  2803. moduleName: CORE,
  2804. };
  2805. static propertyInterpolate6 = {
  2806. name: 'ɵɵpropertyInterpolate6',
  2807. moduleName: CORE,
  2808. };
  2809. static propertyInterpolate7 = {
  2810. name: 'ɵɵpropertyInterpolate7',
  2811. moduleName: CORE,
  2812. };
  2813. static propertyInterpolate8 = {
  2814. name: 'ɵɵpropertyInterpolate8',
  2815. moduleName: CORE,
  2816. };
  2817. static propertyInterpolateV = {
  2818. name: 'ɵɵpropertyInterpolateV',
  2819. moduleName: CORE,
  2820. };
  2821. static i18n = { name: 'ɵɵi18n', moduleName: CORE };
  2822. static i18nAttributes = { name: 'ɵɵi18nAttributes', moduleName: CORE };
  2823. static i18nExp = { name: 'ɵɵi18nExp', moduleName: CORE };
  2824. static i18nStart = { name: 'ɵɵi18nStart', moduleName: CORE };
  2825. static i18nEnd = { name: 'ɵɵi18nEnd', moduleName: CORE };
  2826. static i18nApply = { name: 'ɵɵi18nApply', moduleName: CORE };
  2827. static i18nPostprocess = { name: 'ɵɵi18nPostprocess', moduleName: CORE };
  2828. static pipe = { name: 'ɵɵpipe', moduleName: CORE };
  2829. static projection = { name: 'ɵɵprojection', moduleName: CORE };
  2830. static projectionDef = { name: 'ɵɵprojectionDef', moduleName: CORE };
  2831. static reference = { name: 'ɵɵreference', moduleName: CORE };
  2832. static inject = { name: 'ɵɵinject', moduleName: CORE };
  2833. static injectAttribute = { name: 'ɵɵinjectAttribute', moduleName: CORE };
  2834. static directiveInject = { name: 'ɵɵdirectiveInject', moduleName: CORE };
  2835. static invalidFactory = { name: 'ɵɵinvalidFactory', moduleName: CORE };
  2836. static invalidFactoryDep = { name: 'ɵɵinvalidFactoryDep', moduleName: CORE };
  2837. static templateRefExtractor = {
  2838. name: 'ɵɵtemplateRefExtractor',
  2839. moduleName: CORE,
  2840. };
  2841. static forwardRef = { name: 'forwardRef', moduleName: CORE };
  2842. static resolveForwardRef = { name: 'resolveForwardRef', moduleName: CORE };
  2843. static replaceMetadata = { name: 'ɵɵreplaceMetadata', moduleName: CORE };
  2844. static getReplaceMetadataURL = {
  2845. name: 'ɵɵgetReplaceMetadataURL',
  2846. moduleName: CORE,
  2847. };
  2848. static ɵɵdefineInjectable = { name: 'ɵɵdefineInjectable', moduleName: CORE };
  2849. static declareInjectable = { name: 'ɵɵngDeclareInjectable', moduleName: CORE };
  2850. static InjectableDeclaration = {
  2851. name: 'ɵɵInjectableDeclaration',
  2852. moduleName: CORE,
  2853. };
  2854. static resolveWindow = { name: 'ɵɵresolveWindow', moduleName: CORE };
  2855. static resolveDocument = { name: 'ɵɵresolveDocument', moduleName: CORE };
  2856. static resolveBody = { name: 'ɵɵresolveBody', moduleName: CORE };
  2857. static getComponentDepsFactory = {
  2858. name: 'ɵɵgetComponentDepsFactory',
  2859. moduleName: CORE,
  2860. };
  2861. static defineComponent = { name: 'ɵɵdefineComponent', moduleName: CORE };
  2862. static declareComponent = { name: 'ɵɵngDeclareComponent', moduleName: CORE };
  2863. static setComponentScope = { name: 'ɵɵsetComponentScope', moduleName: CORE };
  2864. static ChangeDetectionStrategy = {
  2865. name: 'ChangeDetectionStrategy',
  2866. moduleName: CORE,
  2867. };
  2868. static ViewEncapsulation = {
  2869. name: 'ViewEncapsulation',
  2870. moduleName: CORE,
  2871. };
  2872. static ComponentDeclaration = {
  2873. name: 'ɵɵComponentDeclaration',
  2874. moduleName: CORE,
  2875. };
  2876. static FactoryDeclaration = {
  2877. name: 'ɵɵFactoryDeclaration',
  2878. moduleName: CORE,
  2879. };
  2880. static declareFactory = { name: 'ɵɵngDeclareFactory', moduleName: CORE };
  2881. static FactoryTarget = { name: 'ɵɵFactoryTarget', moduleName: CORE };
  2882. static defineDirective = { name: 'ɵɵdefineDirective', moduleName: CORE };
  2883. static declareDirective = { name: 'ɵɵngDeclareDirective', moduleName: CORE };
  2884. static DirectiveDeclaration = {
  2885. name: 'ɵɵDirectiveDeclaration',
  2886. moduleName: CORE,
  2887. };
  2888. static InjectorDef = { name: 'ɵɵInjectorDef', moduleName: CORE };
  2889. static InjectorDeclaration = {
  2890. name: 'ɵɵInjectorDeclaration',
  2891. moduleName: CORE,
  2892. };
  2893. static defineInjector = { name: 'ɵɵdefineInjector', moduleName: CORE };
  2894. static declareInjector = { name: 'ɵɵngDeclareInjector', moduleName: CORE };
  2895. static NgModuleDeclaration = {
  2896. name: 'ɵɵNgModuleDeclaration',
  2897. moduleName: CORE,
  2898. };
  2899. static ModuleWithProviders = {
  2900. name: 'ModuleWithProviders',
  2901. moduleName: CORE,
  2902. };
  2903. static defineNgModule = { name: 'ɵɵdefineNgModule', moduleName: CORE };
  2904. static declareNgModule = { name: 'ɵɵngDeclareNgModule', moduleName: CORE };
  2905. static setNgModuleScope = { name: 'ɵɵsetNgModuleScope', moduleName: CORE };
  2906. static registerNgModuleType = {
  2907. name: 'ɵɵregisterNgModuleType',
  2908. moduleName: CORE,
  2909. };
  2910. static PipeDeclaration = { name: 'ɵɵPipeDeclaration', moduleName: CORE };
  2911. static definePipe = { name: 'ɵɵdefinePipe', moduleName: CORE };
  2912. static declarePipe = { name: 'ɵɵngDeclarePipe', moduleName: CORE };
  2913. static declareClassMetadata = {
  2914. name: 'ɵɵngDeclareClassMetadata',
  2915. moduleName: CORE,
  2916. };
  2917. static declareClassMetadataAsync = {
  2918. name: 'ɵɵngDeclareClassMetadataAsync',
  2919. moduleName: CORE,
  2920. };
  2921. static setClassMetadata = { name: 'ɵsetClassMetadata', moduleName: CORE };
  2922. static setClassMetadataAsync = {
  2923. name: 'ɵsetClassMetadataAsync',
  2924. moduleName: CORE,
  2925. };
  2926. static setClassDebugInfo = { name: 'ɵsetClassDebugInfo', moduleName: CORE };
  2927. static queryRefresh = { name: 'ɵɵqueryRefresh', moduleName: CORE };
  2928. static viewQuery = { name: 'ɵɵviewQuery', moduleName: CORE };
  2929. static loadQuery = { name: 'ɵɵloadQuery', moduleName: CORE };
  2930. static contentQuery = { name: 'ɵɵcontentQuery', moduleName: CORE };
  2931. // Signal queries
  2932. static viewQuerySignal = { name: 'ɵɵviewQuerySignal', moduleName: CORE };
  2933. static contentQuerySignal = { name: 'ɵɵcontentQuerySignal', moduleName: CORE };
  2934. static queryAdvance = { name: 'ɵɵqueryAdvance', moduleName: CORE };
  2935. // Two-way bindings
  2936. static twoWayProperty = { name: 'ɵɵtwoWayProperty', moduleName: CORE };
  2937. static twoWayBindingSet = { name: 'ɵɵtwoWayBindingSet', moduleName: CORE };
  2938. static twoWayListener = { name: 'ɵɵtwoWayListener', moduleName: CORE };
  2939. static declareLet = { name: 'ɵɵdeclareLet', moduleName: CORE };
  2940. static storeLet = { name: 'ɵɵstoreLet', moduleName: CORE };
  2941. static readContextLet = { name: 'ɵɵreadContextLet', moduleName: CORE };
  2942. static attachSourceLocations = {
  2943. name: 'ɵɵattachSourceLocations',
  2944. moduleName: CORE,
  2945. };
  2946. static NgOnChangesFeature = { name: 'ɵɵNgOnChangesFeature', moduleName: CORE };
  2947. static InheritDefinitionFeature = {
  2948. name: 'ɵɵInheritDefinitionFeature',
  2949. moduleName: CORE,
  2950. };
  2951. static CopyDefinitionFeature = {
  2952. name: 'ɵɵCopyDefinitionFeature',
  2953. moduleName: CORE,
  2954. };
  2955. static ProvidersFeature = { name: 'ɵɵProvidersFeature', moduleName: CORE };
  2956. static HostDirectivesFeature = {
  2957. name: 'ɵɵHostDirectivesFeature',
  2958. moduleName: CORE,
  2959. };
  2960. static ExternalStylesFeature = {
  2961. name: 'ɵɵExternalStylesFeature',
  2962. moduleName: CORE,
  2963. };
  2964. static listener = { name: 'ɵɵlistener', moduleName: CORE };
  2965. static getInheritedFactory = {
  2966. name: 'ɵɵgetInheritedFactory',
  2967. moduleName: CORE,
  2968. };
  2969. // sanitization-related functions
  2970. static sanitizeHtml = { name: 'ɵɵsanitizeHtml', moduleName: CORE };
  2971. static sanitizeStyle = { name: 'ɵɵsanitizeStyle', moduleName: CORE };
  2972. static sanitizeResourceUrl = {
  2973. name: 'ɵɵsanitizeResourceUrl',
  2974. moduleName: CORE,
  2975. };
  2976. static sanitizeScript = { name: 'ɵɵsanitizeScript', moduleName: CORE };
  2977. static sanitizeUrl = { name: 'ɵɵsanitizeUrl', moduleName: CORE };
  2978. static sanitizeUrlOrResourceUrl = {
  2979. name: 'ɵɵsanitizeUrlOrResourceUrl',
  2980. moduleName: CORE,
  2981. };
  2982. static trustConstantHtml = { name: 'ɵɵtrustConstantHtml', moduleName: CORE };
  2983. static trustConstantResourceUrl = {
  2984. name: 'ɵɵtrustConstantResourceUrl',
  2985. moduleName: CORE,
  2986. };
  2987. static validateIframeAttribute = {
  2988. name: 'ɵɵvalidateIframeAttribute',
  2989. moduleName: CORE,
  2990. };
  2991. // type-checking
  2992. static InputSignalBrandWriteType = { name: 'ɵINPUT_SIGNAL_BRAND_WRITE_TYPE', moduleName: CORE };
  2993. static UnwrapDirectiveSignalInputs = { name: 'ɵUnwrapDirectiveSignalInputs', moduleName: CORE };
  2994. static unwrapWritableSignal = { name: 'ɵunwrapWritableSignal', moduleName: CORE };
  2995. }
  2996. const DASH_CASE_REGEXP = /-+([a-z0-9])/g;
  2997. function dashCaseToCamelCase(input) {
  2998. return input.replace(DASH_CASE_REGEXP, (...m) => m[1].toUpperCase());
  2999. }
  3000. function splitAtColon(input, defaultValues) {
  3001. return _splitAt(input, ':', defaultValues);
  3002. }
  3003. function splitAtPeriod(input, defaultValues) {
  3004. return _splitAt(input, '.', defaultValues);
  3005. }
  3006. function _splitAt(input, character, defaultValues) {
  3007. const characterIndex = input.indexOf(character);
  3008. if (characterIndex == -1)
  3009. return defaultValues;
  3010. return [input.slice(0, characterIndex).trim(), input.slice(characterIndex + 1).trim()];
  3011. }
  3012. function utf8Encode(str) {
  3013. let encoded = [];
  3014. for (let index = 0; index < str.length; index++) {
  3015. let codePoint = str.charCodeAt(index);
  3016. // decode surrogate
  3017. // see https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  3018. if (codePoint >= 0xd800 && codePoint <= 0xdbff && str.length > index + 1) {
  3019. const low = str.charCodeAt(index + 1);
  3020. if (low >= 0xdc00 && low <= 0xdfff) {
  3021. index++;
  3022. codePoint = ((codePoint - 0xd800) << 10) + low - 0xdc00 + 0x10000;
  3023. }
  3024. }
  3025. if (codePoint <= 0x7f) {
  3026. encoded.push(codePoint);
  3027. }
  3028. else if (codePoint <= 0x7ff) {
  3029. encoded.push(((codePoint >> 6) & 0x1f) | 0xc0, (codePoint & 0x3f) | 0x80);
  3030. }
  3031. else if (codePoint <= 0xffff) {
  3032. encoded.push((codePoint >> 12) | 0xe0, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  3033. }
  3034. else if (codePoint <= 0x1fffff) {
  3035. encoded.push(((codePoint >> 18) & 0x07) | 0xf0, ((codePoint >> 12) & 0x3f) | 0x80, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  3036. }
  3037. }
  3038. return encoded;
  3039. }
  3040. function stringify(token) {
  3041. if (typeof token === 'string') {
  3042. return token;
  3043. }
  3044. if (Array.isArray(token)) {
  3045. return `[${token.map(stringify).join(', ')}]`;
  3046. }
  3047. if (token == null) {
  3048. return '' + token;
  3049. }
  3050. const name = token.overriddenName || token.name;
  3051. if (name) {
  3052. return `${name}`;
  3053. }
  3054. if (!token.toString) {
  3055. return 'object';
  3056. }
  3057. // WARNING: do not try to `JSON.stringify(token)` here
  3058. // see https://github.com/angular/angular/issues/23440
  3059. const result = token.toString();
  3060. if (result == null) {
  3061. return '' + result;
  3062. }
  3063. const newLineIndex = result.indexOf('\n');
  3064. return newLineIndex >= 0 ? result.slice(0, newLineIndex) : result;
  3065. }
  3066. class Version {
  3067. full;
  3068. major;
  3069. minor;
  3070. patch;
  3071. constructor(full) {
  3072. this.full = full;
  3073. const splits = full.split('.');
  3074. this.major = splits[0];
  3075. this.minor = splits[1];
  3076. this.patch = splits.slice(2).join('.');
  3077. }
  3078. }
  3079. const _global = globalThis;
  3080. const V1_TO_18 = /^([1-9]|1[0-8])\./;
  3081. function getJitStandaloneDefaultForVersion(version) {
  3082. if (version.startsWith('0.')) {
  3083. // 0.0.0 is always "latest", default is true.
  3084. return true;
  3085. }
  3086. if (V1_TO_18.test(version)) {
  3087. // Angular v2 - v18 default is false.
  3088. return false;
  3089. }
  3090. // All other Angular versions (v19+) default to true.
  3091. return true;
  3092. }
  3093. // https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
  3094. const VERSION = 3;
  3095. const JS_B64_PREFIX = '# sourceMappingURL=data:application/json;base64,';
  3096. class SourceMapGenerator {
  3097. file;
  3098. sourcesContent = new Map();
  3099. lines = [];
  3100. lastCol0 = 0;
  3101. hasMappings = false;
  3102. constructor(file = null) {
  3103. this.file = file;
  3104. }
  3105. // The content is `null` when the content is expected to be loaded using the URL
  3106. addSource(url, content = null) {
  3107. if (!this.sourcesContent.has(url)) {
  3108. this.sourcesContent.set(url, content);
  3109. }
  3110. return this;
  3111. }
  3112. addLine() {
  3113. this.lines.push([]);
  3114. this.lastCol0 = 0;
  3115. return this;
  3116. }
  3117. addMapping(col0, sourceUrl, sourceLine0, sourceCol0) {
  3118. if (!this.currentLine) {
  3119. throw new Error(`A line must be added before mappings can be added`);
  3120. }
  3121. if (sourceUrl != null && !this.sourcesContent.has(sourceUrl)) {
  3122. throw new Error(`Unknown source file "${sourceUrl}"`);
  3123. }
  3124. if (col0 == null) {
  3125. throw new Error(`The column in the generated code must be provided`);
  3126. }
  3127. if (col0 < this.lastCol0) {
  3128. throw new Error(`Mapping should be added in output order`);
  3129. }
  3130. if (sourceUrl && (sourceLine0 == null || sourceCol0 == null)) {
  3131. throw new Error(`The source location must be provided when a source url is provided`);
  3132. }
  3133. this.hasMappings = true;
  3134. this.lastCol0 = col0;
  3135. this.currentLine.push({ col0, sourceUrl, sourceLine0, sourceCol0 });
  3136. return this;
  3137. }
  3138. /**
  3139. * @internal strip this from published d.ts files due to
  3140. * https://github.com/microsoft/TypeScript/issues/36216
  3141. */
  3142. get currentLine() {
  3143. return this.lines.slice(-1)[0];
  3144. }
  3145. toJSON() {
  3146. if (!this.hasMappings) {
  3147. return null;
  3148. }
  3149. const sourcesIndex = new Map();
  3150. const sources = [];
  3151. const sourcesContent = [];
  3152. Array.from(this.sourcesContent.keys()).forEach((url, i) => {
  3153. sourcesIndex.set(url, i);
  3154. sources.push(url);
  3155. sourcesContent.push(this.sourcesContent.get(url) || null);
  3156. });
  3157. let mappings = '';
  3158. let lastCol0 = 0;
  3159. let lastSourceIndex = 0;
  3160. let lastSourceLine0 = 0;
  3161. let lastSourceCol0 = 0;
  3162. this.lines.forEach((segments) => {
  3163. lastCol0 = 0;
  3164. mappings += segments
  3165. .map((segment) => {
  3166. // zero-based starting column of the line in the generated code
  3167. let segAsStr = toBase64VLQ(segment.col0 - lastCol0);
  3168. lastCol0 = segment.col0;
  3169. if (segment.sourceUrl != null) {
  3170. // zero-based index into the “sources” list
  3171. segAsStr += toBase64VLQ(sourcesIndex.get(segment.sourceUrl) - lastSourceIndex);
  3172. lastSourceIndex = sourcesIndex.get(segment.sourceUrl);
  3173. // the zero-based starting line in the original source
  3174. segAsStr += toBase64VLQ(segment.sourceLine0 - lastSourceLine0);
  3175. lastSourceLine0 = segment.sourceLine0;
  3176. // the zero-based starting column in the original source
  3177. segAsStr += toBase64VLQ(segment.sourceCol0 - lastSourceCol0);
  3178. lastSourceCol0 = segment.sourceCol0;
  3179. }
  3180. return segAsStr;
  3181. })
  3182. .join(',');
  3183. mappings += ';';
  3184. });
  3185. mappings = mappings.slice(0, -1);
  3186. return {
  3187. 'file': this.file || '',
  3188. 'version': VERSION,
  3189. 'sourceRoot': '',
  3190. 'sources': sources,
  3191. 'sourcesContent': sourcesContent,
  3192. 'mappings': mappings,
  3193. };
  3194. }
  3195. toJsComment() {
  3196. return this.hasMappings
  3197. ? '//' + JS_B64_PREFIX + toBase64String(JSON.stringify(this, null, 0))
  3198. : '';
  3199. }
  3200. }
  3201. function toBase64String(value) {
  3202. let b64 = '';
  3203. const encoded = utf8Encode(value);
  3204. for (let i = 0; i < encoded.length;) {
  3205. const i1 = encoded[i++];
  3206. const i2 = i < encoded.length ? encoded[i++] : null;
  3207. const i3 = i < encoded.length ? encoded[i++] : null;
  3208. b64 += toBase64Digit(i1 >> 2);
  3209. b64 += toBase64Digit(((i1 & 3) << 4) | (i2 === null ? 0 : i2 >> 4));
  3210. b64 += i2 === null ? '=' : toBase64Digit(((i2 & 15) << 2) | (i3 === null ? 0 : i3 >> 6));
  3211. b64 += i2 === null || i3 === null ? '=' : toBase64Digit(i3 & 63);
  3212. }
  3213. return b64;
  3214. }
  3215. function toBase64VLQ(value) {
  3216. value = value < 0 ? (-value << 1) + 1 : value << 1;
  3217. let out = '';
  3218. do {
  3219. let digit = value & 31;
  3220. value = value >> 5;
  3221. if (value > 0) {
  3222. digit = digit | 32;
  3223. }
  3224. out += toBase64Digit(digit);
  3225. } while (value > 0);
  3226. return out;
  3227. }
  3228. const B64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  3229. function toBase64Digit(value) {
  3230. if (value < 0 || value >= 64) {
  3231. throw new Error(`Can only encode value in the range [0, 63]`);
  3232. }
  3233. return B64_DIGITS[value];
  3234. }
  3235. const _SINGLE_QUOTE_ESCAPE_STRING_RE = /'|\\|\n|\r|\$/g;
  3236. const _LEGAL_IDENTIFIER_RE = /^[$A-Z_][0-9A-Z_$]*$/i;
  3237. const _INDENT_WITH = ' ';
  3238. class _EmittedLine {
  3239. indent;
  3240. partsLength = 0;
  3241. parts = [];
  3242. srcSpans = [];
  3243. constructor(indent) {
  3244. this.indent = indent;
  3245. }
  3246. }
  3247. class EmitterVisitorContext {
  3248. _indent;
  3249. static createRoot() {
  3250. return new EmitterVisitorContext(0);
  3251. }
  3252. _lines;
  3253. constructor(_indent) {
  3254. this._indent = _indent;
  3255. this._lines = [new _EmittedLine(_indent)];
  3256. }
  3257. /**
  3258. * @internal strip this from published d.ts files due to
  3259. * https://github.com/microsoft/TypeScript/issues/36216
  3260. */
  3261. get _currentLine() {
  3262. return this._lines[this._lines.length - 1];
  3263. }
  3264. println(from, lastPart = '') {
  3265. this.print(from || null, lastPart, true);
  3266. }
  3267. lineIsEmpty() {
  3268. return this._currentLine.parts.length === 0;
  3269. }
  3270. lineLength() {
  3271. return this._currentLine.indent * _INDENT_WITH.length + this._currentLine.partsLength;
  3272. }
  3273. print(from, part, newLine = false) {
  3274. if (part.length > 0) {
  3275. this._currentLine.parts.push(part);
  3276. this._currentLine.partsLength += part.length;
  3277. this._currentLine.srcSpans.push((from && from.sourceSpan) || null);
  3278. }
  3279. if (newLine) {
  3280. this._lines.push(new _EmittedLine(this._indent));
  3281. }
  3282. }
  3283. removeEmptyLastLine() {
  3284. if (this.lineIsEmpty()) {
  3285. this._lines.pop();
  3286. }
  3287. }
  3288. incIndent() {
  3289. this._indent++;
  3290. if (this.lineIsEmpty()) {
  3291. this._currentLine.indent = this._indent;
  3292. }
  3293. }
  3294. decIndent() {
  3295. this._indent--;
  3296. if (this.lineIsEmpty()) {
  3297. this._currentLine.indent = this._indent;
  3298. }
  3299. }
  3300. toSource() {
  3301. return this.sourceLines
  3302. .map((l) => (l.parts.length > 0 ? _createIndent(l.indent) + l.parts.join('') : ''))
  3303. .join('\n');
  3304. }
  3305. toSourceMapGenerator(genFilePath, startsAtLine = 0) {
  3306. const map = new SourceMapGenerator(genFilePath);
  3307. let firstOffsetMapped = false;
  3308. const mapFirstOffsetIfNeeded = () => {
  3309. if (!firstOffsetMapped) {
  3310. // Add a single space so that tools won't try to load the file from disk.
  3311. // Note: We are using virtual urls like `ng:///`, so we have to
  3312. // provide a content here.
  3313. map.addSource(genFilePath, ' ').addMapping(0, genFilePath, 0, 0);
  3314. firstOffsetMapped = true;
  3315. }
  3316. };
  3317. for (let i = 0; i < startsAtLine; i++) {
  3318. map.addLine();
  3319. mapFirstOffsetIfNeeded();
  3320. }
  3321. this.sourceLines.forEach((line, lineIdx) => {
  3322. map.addLine();
  3323. const spans = line.srcSpans;
  3324. const parts = line.parts;
  3325. let col0 = line.indent * _INDENT_WITH.length;
  3326. let spanIdx = 0;
  3327. // skip leading parts without source spans
  3328. while (spanIdx < spans.length && !spans[spanIdx]) {
  3329. col0 += parts[spanIdx].length;
  3330. spanIdx++;
  3331. }
  3332. if (spanIdx < spans.length && lineIdx === 0 && col0 === 0) {
  3333. firstOffsetMapped = true;
  3334. }
  3335. else {
  3336. mapFirstOffsetIfNeeded();
  3337. }
  3338. while (spanIdx < spans.length) {
  3339. const span = spans[spanIdx];
  3340. const source = span.start.file;
  3341. const sourceLine = span.start.line;
  3342. const sourceCol = span.start.col;
  3343. map
  3344. .addSource(source.url, source.content)
  3345. .addMapping(col0, source.url, sourceLine, sourceCol);
  3346. col0 += parts[spanIdx].length;
  3347. spanIdx++;
  3348. // assign parts without span or the same span to the previous segment
  3349. while (spanIdx < spans.length && (span === spans[spanIdx] || !spans[spanIdx])) {
  3350. col0 += parts[spanIdx].length;
  3351. spanIdx++;
  3352. }
  3353. }
  3354. });
  3355. return map;
  3356. }
  3357. spanOf(line, column) {
  3358. const emittedLine = this._lines[line];
  3359. if (emittedLine) {
  3360. let columnsLeft = column - _createIndent(emittedLine.indent).length;
  3361. for (let partIndex = 0; partIndex < emittedLine.parts.length; partIndex++) {
  3362. const part = emittedLine.parts[partIndex];
  3363. if (part.length > columnsLeft) {
  3364. return emittedLine.srcSpans[partIndex];
  3365. }
  3366. columnsLeft -= part.length;
  3367. }
  3368. }
  3369. return null;
  3370. }
  3371. /**
  3372. * @internal strip this from published d.ts files due to
  3373. * https://github.com/microsoft/TypeScript/issues/36216
  3374. */
  3375. get sourceLines() {
  3376. if (this._lines.length && this._lines[this._lines.length - 1].parts.length === 0) {
  3377. return this._lines.slice(0, -1);
  3378. }
  3379. return this._lines;
  3380. }
  3381. }
  3382. class AbstractEmitterVisitor {
  3383. _escapeDollarInStrings;
  3384. constructor(_escapeDollarInStrings) {
  3385. this._escapeDollarInStrings = _escapeDollarInStrings;
  3386. }
  3387. printLeadingComments(stmt, ctx) {
  3388. if (stmt.leadingComments === undefined) {
  3389. return;
  3390. }
  3391. for (const comment of stmt.leadingComments) {
  3392. if (comment instanceof JSDocComment) {
  3393. ctx.print(stmt, `/*${comment.toString()}*/`, comment.trailingNewline);
  3394. }
  3395. else {
  3396. if (comment.multiline) {
  3397. ctx.print(stmt, `/* ${comment.text} */`, comment.trailingNewline);
  3398. }
  3399. else {
  3400. comment.text.split('\n').forEach((line) => {
  3401. ctx.println(stmt, `// ${line}`);
  3402. });
  3403. }
  3404. }
  3405. }
  3406. }
  3407. visitExpressionStmt(stmt, ctx) {
  3408. this.printLeadingComments(stmt, ctx);
  3409. stmt.expr.visitExpression(this, ctx);
  3410. ctx.println(stmt, ';');
  3411. return null;
  3412. }
  3413. visitReturnStmt(stmt, ctx) {
  3414. this.printLeadingComments(stmt, ctx);
  3415. ctx.print(stmt, `return `);
  3416. stmt.value.visitExpression(this, ctx);
  3417. ctx.println(stmt, ';');
  3418. return null;
  3419. }
  3420. visitIfStmt(stmt, ctx) {
  3421. this.printLeadingComments(stmt, ctx);
  3422. ctx.print(stmt, `if (`);
  3423. stmt.condition.visitExpression(this, ctx);
  3424. ctx.print(stmt, `) {`);
  3425. const hasElseCase = stmt.falseCase != null && stmt.falseCase.length > 0;
  3426. if (stmt.trueCase.length <= 1 && !hasElseCase) {
  3427. ctx.print(stmt, ` `);
  3428. this.visitAllStatements(stmt.trueCase, ctx);
  3429. ctx.removeEmptyLastLine();
  3430. ctx.print(stmt, ` `);
  3431. }
  3432. else {
  3433. ctx.println();
  3434. ctx.incIndent();
  3435. this.visitAllStatements(stmt.trueCase, ctx);
  3436. ctx.decIndent();
  3437. if (hasElseCase) {
  3438. ctx.println(stmt, `} else {`);
  3439. ctx.incIndent();
  3440. this.visitAllStatements(stmt.falseCase, ctx);
  3441. ctx.decIndent();
  3442. }
  3443. }
  3444. ctx.println(stmt, `}`);
  3445. return null;
  3446. }
  3447. visitWriteVarExpr(expr, ctx) {
  3448. const lineWasEmpty = ctx.lineIsEmpty();
  3449. if (!lineWasEmpty) {
  3450. ctx.print(expr, '(');
  3451. }
  3452. ctx.print(expr, `${expr.name} = `);
  3453. expr.value.visitExpression(this, ctx);
  3454. if (!lineWasEmpty) {
  3455. ctx.print(expr, ')');
  3456. }
  3457. return null;
  3458. }
  3459. visitWriteKeyExpr(expr, ctx) {
  3460. const lineWasEmpty = ctx.lineIsEmpty();
  3461. if (!lineWasEmpty) {
  3462. ctx.print(expr, '(');
  3463. }
  3464. expr.receiver.visitExpression(this, ctx);
  3465. ctx.print(expr, `[`);
  3466. expr.index.visitExpression(this, ctx);
  3467. ctx.print(expr, `] = `);
  3468. expr.value.visitExpression(this, ctx);
  3469. if (!lineWasEmpty) {
  3470. ctx.print(expr, ')');
  3471. }
  3472. return null;
  3473. }
  3474. visitWritePropExpr(expr, ctx) {
  3475. const lineWasEmpty = ctx.lineIsEmpty();
  3476. if (!lineWasEmpty) {
  3477. ctx.print(expr, '(');
  3478. }
  3479. expr.receiver.visitExpression(this, ctx);
  3480. ctx.print(expr, `.${expr.name} = `);
  3481. expr.value.visitExpression(this, ctx);
  3482. if (!lineWasEmpty) {
  3483. ctx.print(expr, ')');
  3484. }
  3485. return null;
  3486. }
  3487. visitInvokeFunctionExpr(expr, ctx) {
  3488. const shouldParenthesize = expr.fn instanceof ArrowFunctionExpr;
  3489. if (shouldParenthesize) {
  3490. ctx.print(expr.fn, '(');
  3491. }
  3492. expr.fn.visitExpression(this, ctx);
  3493. if (shouldParenthesize) {
  3494. ctx.print(expr.fn, ')');
  3495. }
  3496. ctx.print(expr, `(`);
  3497. this.visitAllExpressions(expr.args, ctx, ',');
  3498. ctx.print(expr, `)`);
  3499. return null;
  3500. }
  3501. visitTaggedTemplateLiteralExpr(expr, ctx) {
  3502. expr.tag.visitExpression(this, ctx);
  3503. expr.template.visitExpression(this, ctx);
  3504. return null;
  3505. }
  3506. visitTemplateLiteralExpr(expr, ctx) {
  3507. ctx.print(expr, '`');
  3508. for (let i = 0; i < expr.elements.length; i++) {
  3509. expr.elements[i].visitExpression(this, ctx);
  3510. const expression = i < expr.expressions.length ? expr.expressions[i] : null;
  3511. if (expression !== null) {
  3512. ctx.print(expression, '${');
  3513. expression.visitExpression(this, ctx);
  3514. ctx.print(expression, '}');
  3515. }
  3516. }
  3517. ctx.print(expr, '`');
  3518. }
  3519. visitTemplateLiteralElementExpr(expr, ctx) {
  3520. ctx.print(expr, expr.rawText);
  3521. }
  3522. visitWrappedNodeExpr(ast, ctx) {
  3523. throw new Error('Abstract emitter cannot visit WrappedNodeExpr.');
  3524. }
  3525. visitTypeofExpr(expr, ctx) {
  3526. ctx.print(expr, 'typeof ');
  3527. expr.expr.visitExpression(this, ctx);
  3528. }
  3529. visitReadVarExpr(ast, ctx) {
  3530. ctx.print(ast, ast.name);
  3531. return null;
  3532. }
  3533. visitInstantiateExpr(ast, ctx) {
  3534. ctx.print(ast, `new `);
  3535. ast.classExpr.visitExpression(this, ctx);
  3536. ctx.print(ast, `(`);
  3537. this.visitAllExpressions(ast.args, ctx, ',');
  3538. ctx.print(ast, `)`);
  3539. return null;
  3540. }
  3541. visitLiteralExpr(ast, ctx) {
  3542. const value = ast.value;
  3543. if (typeof value === 'string') {
  3544. ctx.print(ast, escapeIdentifier(value, this._escapeDollarInStrings));
  3545. }
  3546. else {
  3547. ctx.print(ast, `${value}`);
  3548. }
  3549. return null;
  3550. }
  3551. visitLocalizedString(ast, ctx) {
  3552. const head = ast.serializeI18nHead();
  3553. ctx.print(ast, '$localize `' + head.raw);
  3554. for (let i = 1; i < ast.messageParts.length; i++) {
  3555. ctx.print(ast, '${');
  3556. ast.expressions[i - 1].visitExpression(this, ctx);
  3557. ctx.print(ast, `}${ast.serializeI18nTemplatePart(i).raw}`);
  3558. }
  3559. ctx.print(ast, '`');
  3560. return null;
  3561. }
  3562. visitConditionalExpr(ast, ctx) {
  3563. ctx.print(ast, `(`);
  3564. ast.condition.visitExpression(this, ctx);
  3565. ctx.print(ast, '? ');
  3566. ast.trueCase.visitExpression(this, ctx);
  3567. ctx.print(ast, ': ');
  3568. ast.falseCase.visitExpression(this, ctx);
  3569. ctx.print(ast, `)`);
  3570. return null;
  3571. }
  3572. visitDynamicImportExpr(ast, ctx) {
  3573. ctx.print(ast, `import(${ast.url})`);
  3574. }
  3575. visitNotExpr(ast, ctx) {
  3576. ctx.print(ast, '!');
  3577. ast.condition.visitExpression(this, ctx);
  3578. return null;
  3579. }
  3580. visitUnaryOperatorExpr(ast, ctx) {
  3581. let opStr;
  3582. switch (ast.operator) {
  3583. case UnaryOperator.Plus:
  3584. opStr = '+';
  3585. break;
  3586. case UnaryOperator.Minus:
  3587. opStr = '-';
  3588. break;
  3589. default:
  3590. throw new Error(`Unknown operator ${ast.operator}`);
  3591. }
  3592. if (ast.parens)
  3593. ctx.print(ast, `(`);
  3594. ctx.print(ast, opStr);
  3595. ast.expr.visitExpression(this, ctx);
  3596. if (ast.parens)
  3597. ctx.print(ast, `)`);
  3598. return null;
  3599. }
  3600. visitBinaryOperatorExpr(ast, ctx) {
  3601. let opStr;
  3602. switch (ast.operator) {
  3603. case BinaryOperator.Equals:
  3604. opStr = '==';
  3605. break;
  3606. case BinaryOperator.Identical:
  3607. opStr = '===';
  3608. break;
  3609. case BinaryOperator.NotEquals:
  3610. opStr = '!=';
  3611. break;
  3612. case BinaryOperator.NotIdentical:
  3613. opStr = '!==';
  3614. break;
  3615. case BinaryOperator.And:
  3616. opStr = '&&';
  3617. break;
  3618. case BinaryOperator.BitwiseOr:
  3619. opStr = '|';
  3620. break;
  3621. case BinaryOperator.BitwiseAnd:
  3622. opStr = '&';
  3623. break;
  3624. case BinaryOperator.Or:
  3625. opStr = '||';
  3626. break;
  3627. case BinaryOperator.Plus:
  3628. opStr = '+';
  3629. break;
  3630. case BinaryOperator.Minus:
  3631. opStr = '-';
  3632. break;
  3633. case BinaryOperator.Divide:
  3634. opStr = '/';
  3635. break;
  3636. case BinaryOperator.Multiply:
  3637. opStr = '*';
  3638. break;
  3639. case BinaryOperator.Modulo:
  3640. opStr = '%';
  3641. break;
  3642. case BinaryOperator.Lower:
  3643. opStr = '<';
  3644. break;
  3645. case BinaryOperator.LowerEquals:
  3646. opStr = '<=';
  3647. break;
  3648. case BinaryOperator.Bigger:
  3649. opStr = '>';
  3650. break;
  3651. case BinaryOperator.BiggerEquals:
  3652. opStr = '>=';
  3653. break;
  3654. case BinaryOperator.NullishCoalesce:
  3655. opStr = '??';
  3656. break;
  3657. default:
  3658. throw new Error(`Unknown operator ${ast.operator}`);
  3659. }
  3660. if (ast.parens)
  3661. ctx.print(ast, `(`);
  3662. ast.lhs.visitExpression(this, ctx);
  3663. ctx.print(ast, ` ${opStr} `);
  3664. ast.rhs.visitExpression(this, ctx);
  3665. if (ast.parens)
  3666. ctx.print(ast, `)`);
  3667. return null;
  3668. }
  3669. visitReadPropExpr(ast, ctx) {
  3670. ast.receiver.visitExpression(this, ctx);
  3671. ctx.print(ast, `.`);
  3672. ctx.print(ast, ast.name);
  3673. return null;
  3674. }
  3675. visitReadKeyExpr(ast, ctx) {
  3676. ast.receiver.visitExpression(this, ctx);
  3677. ctx.print(ast, `[`);
  3678. ast.index.visitExpression(this, ctx);
  3679. ctx.print(ast, `]`);
  3680. return null;
  3681. }
  3682. visitLiteralArrayExpr(ast, ctx) {
  3683. ctx.print(ast, `[`);
  3684. this.visitAllExpressions(ast.entries, ctx, ',');
  3685. ctx.print(ast, `]`);
  3686. return null;
  3687. }
  3688. visitLiteralMapExpr(ast, ctx) {
  3689. ctx.print(ast, `{`);
  3690. this.visitAllObjects((entry) => {
  3691. ctx.print(ast, `${escapeIdentifier(entry.key, this._escapeDollarInStrings, entry.quoted)}:`);
  3692. entry.value.visitExpression(this, ctx);
  3693. }, ast.entries, ctx, ',');
  3694. ctx.print(ast, `}`);
  3695. return null;
  3696. }
  3697. visitCommaExpr(ast, ctx) {
  3698. ctx.print(ast, '(');
  3699. this.visitAllExpressions(ast.parts, ctx, ',');
  3700. ctx.print(ast, ')');
  3701. return null;
  3702. }
  3703. visitAllExpressions(expressions, ctx, separator) {
  3704. this.visitAllObjects((expr) => expr.visitExpression(this, ctx), expressions, ctx, separator);
  3705. }
  3706. visitAllObjects(handler, expressions, ctx, separator) {
  3707. let incrementedIndent = false;
  3708. for (let i = 0; i < expressions.length; i++) {
  3709. if (i > 0) {
  3710. if (ctx.lineLength() > 80) {
  3711. ctx.print(null, separator, true);
  3712. if (!incrementedIndent) {
  3713. // continuation are marked with double indent.
  3714. ctx.incIndent();
  3715. ctx.incIndent();
  3716. incrementedIndent = true;
  3717. }
  3718. }
  3719. else {
  3720. ctx.print(null, separator, false);
  3721. }
  3722. }
  3723. handler(expressions[i]);
  3724. }
  3725. if (incrementedIndent) {
  3726. // continuation are marked with double indent.
  3727. ctx.decIndent();
  3728. ctx.decIndent();
  3729. }
  3730. }
  3731. visitAllStatements(statements, ctx) {
  3732. statements.forEach((stmt) => stmt.visitStatement(this, ctx));
  3733. }
  3734. }
  3735. function escapeIdentifier(input, escapeDollar, alwaysQuote = true) {
  3736. if (input == null) {
  3737. return null;
  3738. }
  3739. const body = input.replace(_SINGLE_QUOTE_ESCAPE_STRING_RE, (...match) => {
  3740. if (match[0] == '$') {
  3741. return escapeDollar ? '\\$' : '$';
  3742. }
  3743. else if (match[0] == '\n') {
  3744. return '\\n';
  3745. }
  3746. else if (match[0] == '\r') {
  3747. return '\\r';
  3748. }
  3749. else {
  3750. return `\\${match[0]}`;
  3751. }
  3752. });
  3753. const requiresQuotes = alwaysQuote || !_LEGAL_IDENTIFIER_RE.test(body);
  3754. return requiresQuotes ? `'${body}'` : body;
  3755. }
  3756. function _createIndent(count) {
  3757. let res = '';
  3758. for (let i = 0; i < count; i++) {
  3759. res += _INDENT_WITH;
  3760. }
  3761. return res;
  3762. }
  3763. function typeWithParameters(type, numParams) {
  3764. if (numParams === 0) {
  3765. return expressionType(type);
  3766. }
  3767. const params = [];
  3768. for (let i = 0; i < numParams; i++) {
  3769. params.push(DYNAMIC_TYPE);
  3770. }
  3771. return expressionType(type, undefined, params);
  3772. }
  3773. function getSafePropertyAccessString(accessor, name) {
  3774. const escapedName = escapeIdentifier(name, false, false);
  3775. return escapedName !== name ? `${accessor}[${escapedName}]` : `${accessor}.${name}`;
  3776. }
  3777. function jitOnlyGuardedExpression(expr) {
  3778. return guardedExpression('ngJitMode', expr);
  3779. }
  3780. function devOnlyGuardedExpression(expr) {
  3781. return guardedExpression('ngDevMode', expr);
  3782. }
  3783. function guardedExpression(guard, expr) {
  3784. const guardExpr = new ExternalExpr({ name: guard, moduleName: null });
  3785. const guardNotDefined = new BinaryOperatorExpr(BinaryOperator.Identical, new TypeofExpr(guardExpr), literal$1('undefined'));
  3786. const guardUndefinedOrTrue = new BinaryOperatorExpr(BinaryOperator.Or, guardNotDefined, guardExpr,
  3787. /* type */ undefined,
  3788. /* sourceSpan */ undefined, true);
  3789. return new BinaryOperatorExpr(BinaryOperator.And, guardUndefinedOrTrue, expr);
  3790. }
  3791. function wrapReference(value) {
  3792. const wrapped = new WrappedNodeExpr(value);
  3793. return { value: wrapped, type: wrapped };
  3794. }
  3795. function refsToArray(refs, shouldForwardDeclare) {
  3796. const values = literalArr(refs.map((ref) => ref.value));
  3797. return shouldForwardDeclare ? arrowFn([], values) : values;
  3798. }
  3799. function createMayBeForwardRefExpression(expression, forwardRef) {
  3800. return { expression, forwardRef };
  3801. }
  3802. /**
  3803. * Convert a `MaybeForwardRefExpression` to an `Expression`, possibly wrapping its expression in a
  3804. * `forwardRef()` call.
  3805. *
  3806. * If `MaybeForwardRefExpression.forwardRef` is `ForwardRefHandling.Unwrapped` then the expression
  3807. * was originally wrapped in a `forwardRef()` call to prevent the value from being eagerly evaluated
  3808. * in the code.
  3809. *
  3810. * See `packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts` and
  3811. * `packages/compiler/src/jit_compiler_facade.ts` for more information.
  3812. */
  3813. function convertFromMaybeForwardRefExpression({ expression, forwardRef, }) {
  3814. switch (forwardRef) {
  3815. case 0 /* ForwardRefHandling.None */:
  3816. case 1 /* ForwardRefHandling.Wrapped */:
  3817. return expression;
  3818. case 2 /* ForwardRefHandling.Unwrapped */:
  3819. return generateForwardRef(expression);
  3820. }
  3821. }
  3822. /**
  3823. * Generate an expression that has the given `expr` wrapped in the following form:
  3824. *
  3825. * ```ts
  3826. * forwardRef(() => expr)
  3827. * ```
  3828. */
  3829. function generateForwardRef(expr) {
  3830. return importExpr(Identifiers.forwardRef).callFn([arrowFn([], expr)]);
  3831. }
  3832. var R3FactoryDelegateType;
  3833. (function (R3FactoryDelegateType) {
  3834. R3FactoryDelegateType[R3FactoryDelegateType["Class"] = 0] = "Class";
  3835. R3FactoryDelegateType[R3FactoryDelegateType["Function"] = 1] = "Function";
  3836. })(R3FactoryDelegateType || (R3FactoryDelegateType = {}));
  3837. /**
  3838. * Construct a factory function expression for the given `R3FactoryMetadata`.
  3839. */
  3840. function compileFactoryFunction(meta) {
  3841. const t = variable('__ngFactoryType__');
  3842. let baseFactoryVar = null;
  3843. // The type to instantiate via constructor invocation. If there is no delegated factory, meaning
  3844. // this type is always created by constructor invocation, then this is the type-to-create
  3845. // parameter provided by the user (t) if specified, or the current type if not. If there is a
  3846. // delegated factory (which is used to create the current type) then this is only the type-to-
  3847. // create parameter (t).
  3848. const typeForCtor = !isDelegatedFactoryMetadata(meta)
  3849. ? new BinaryOperatorExpr(BinaryOperator.Or, t, meta.type.value)
  3850. : t;
  3851. let ctorExpr = null;
  3852. if (meta.deps !== null) {
  3853. // There is a constructor (either explicitly or implicitly defined).
  3854. if (meta.deps !== 'invalid') {
  3855. ctorExpr = new InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target));
  3856. }
  3857. }
  3858. else {
  3859. // There is no constructor, use the base class' factory to construct typeForCtor.
  3860. baseFactoryVar = variable(`ɵ${meta.name}_BaseFactory`);
  3861. ctorExpr = baseFactoryVar.callFn([typeForCtor]);
  3862. }
  3863. const body = [];
  3864. let retExpr = null;
  3865. function makeConditionalFactory(nonCtorExpr) {
  3866. const r = variable('__ngConditionalFactory__');
  3867. body.push(r.set(NULL_EXPR).toDeclStmt());
  3868. const ctorStmt = ctorExpr !== null
  3869. ? r.set(ctorExpr).toStmt()
  3870. : importExpr(Identifiers.invalidFactory).callFn([]).toStmt();
  3871. body.push(ifStmt(t, [ctorStmt], [r.set(nonCtorExpr).toStmt()]));
  3872. return r;
  3873. }
  3874. if (isDelegatedFactoryMetadata(meta)) {
  3875. // This type is created with a delegated factory. If a type parameter is not specified, call
  3876. // the factory instead.
  3877. const delegateArgs = injectDependencies(meta.delegateDeps, meta.target);
  3878. // Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType.
  3879. const factoryExpr = new (meta.delegateType === R3FactoryDelegateType.Class ? InstantiateExpr : InvokeFunctionExpr)(meta.delegate, delegateArgs);
  3880. retExpr = makeConditionalFactory(factoryExpr);
  3881. }
  3882. else if (isExpressionFactoryMetadata(meta)) {
  3883. // TODO(alxhub): decide whether to lower the value here or in the caller
  3884. retExpr = makeConditionalFactory(meta.expression);
  3885. }
  3886. else {
  3887. retExpr = ctorExpr;
  3888. }
  3889. if (retExpr === null) {
  3890. // The expression cannot be formed so render an `ɵɵinvalidFactory()` call.
  3891. body.push(importExpr(Identifiers.invalidFactory).callFn([]).toStmt());
  3892. }
  3893. else if (baseFactoryVar !== null) {
  3894. // This factory uses a base factory, so call `ɵɵgetInheritedFactory()` to compute it.
  3895. const getInheritedFactoryCall = importExpr(Identifiers.getInheritedFactory).callFn([meta.type.value]);
  3896. // Memoize the base factoryFn: `baseFactory || (baseFactory = ɵɵgetInheritedFactory(...))`
  3897. const baseFactory = new BinaryOperatorExpr(BinaryOperator.Or, baseFactoryVar, baseFactoryVar.set(getInheritedFactoryCall));
  3898. body.push(new ReturnStatement(baseFactory.callFn([typeForCtor])));
  3899. }
  3900. else {
  3901. // This is straightforward factory, just return it.
  3902. body.push(new ReturnStatement(retExpr));
  3903. }
  3904. let factoryFn = fn([new FnParam(t.name, DYNAMIC_TYPE)], body, INFERRED_TYPE, undefined, `${meta.name}_Factory`);
  3905. if (baseFactoryVar !== null) {
  3906. // There is a base factory variable so wrap its declaration along with the factory function into
  3907. // an IIFE.
  3908. factoryFn = arrowFn([], [new DeclareVarStmt(baseFactoryVar.name), new ReturnStatement(factoryFn)])
  3909. .callFn([], /* sourceSpan */ undefined, /* pure */ true);
  3910. }
  3911. return {
  3912. expression: factoryFn,
  3913. statements: [],
  3914. type: createFactoryType(meta),
  3915. };
  3916. }
  3917. function createFactoryType(meta) {
  3918. const ctorDepsType = meta.deps !== null && meta.deps !== 'invalid' ? createCtorDepsType(meta.deps) : NONE_TYPE;
  3919. return expressionType(importExpr(Identifiers.FactoryDeclaration, [
  3920. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  3921. ctorDepsType,
  3922. ]));
  3923. }
  3924. function injectDependencies(deps, target) {
  3925. return deps.map((dep, index) => compileInjectDependency(dep, target, index));
  3926. }
  3927. function compileInjectDependency(dep, target, index) {
  3928. // Interpret the dependency according to its resolved type.
  3929. if (dep.token === null) {
  3930. return importExpr(Identifiers.invalidFactoryDep).callFn([literal$1(index)]);
  3931. }
  3932. else if (dep.attributeNameType === null) {
  3933. // Build up the injection flags according to the metadata.
  3934. const flags = 0 /* InjectFlags.Default */ |
  3935. (dep.self ? 2 /* InjectFlags.Self */ : 0) |
  3936. (dep.skipSelf ? 4 /* InjectFlags.SkipSelf */ : 0) |
  3937. (dep.host ? 1 /* InjectFlags.Host */ : 0) |
  3938. (dep.optional ? 8 /* InjectFlags.Optional */ : 0) |
  3939. (target === exports.FactoryTarget.Pipe ? 16 /* InjectFlags.ForPipe */ : 0);
  3940. // If this dependency is optional or otherwise has non-default flags, then additional
  3941. // parameters describing how to inject the dependency must be passed to the inject function
  3942. // that's being used.
  3943. let flagsParam = flags !== 0 /* InjectFlags.Default */ || dep.optional ? literal$1(flags) : null;
  3944. // Build up the arguments to the injectFn call.
  3945. const injectArgs = [dep.token];
  3946. if (flagsParam) {
  3947. injectArgs.push(flagsParam);
  3948. }
  3949. const injectFn = getInjectFn(target);
  3950. return importExpr(injectFn).callFn(injectArgs);
  3951. }
  3952. else {
  3953. // The `dep.attributeTypeName` value is defined, which indicates that this is an `@Attribute()`
  3954. // type dependency. For the generated JS we still want to use the `dep.token` value in case the
  3955. // name given for the attribute is not a string literal. For example given `@Attribute(foo())`,
  3956. // we want to generate `ɵɵinjectAttribute(foo())`.
  3957. //
  3958. // The `dep.attributeTypeName` is only actually used (in `createCtorDepType()`) to generate
  3959. // typings.
  3960. return importExpr(Identifiers.injectAttribute).callFn([dep.token]);
  3961. }
  3962. }
  3963. function createCtorDepsType(deps) {
  3964. let hasTypes = false;
  3965. const attributeTypes = deps.map((dep) => {
  3966. const type = createCtorDepType(dep);
  3967. if (type !== null) {
  3968. hasTypes = true;
  3969. return type;
  3970. }
  3971. else {
  3972. return literal$1(null);
  3973. }
  3974. });
  3975. if (hasTypes) {
  3976. return expressionType(literalArr(attributeTypes));
  3977. }
  3978. else {
  3979. return NONE_TYPE;
  3980. }
  3981. }
  3982. function createCtorDepType(dep) {
  3983. const entries = [];
  3984. if (dep.attributeNameType !== null) {
  3985. entries.push({ key: 'attribute', value: dep.attributeNameType, quoted: false });
  3986. }
  3987. if (dep.optional) {
  3988. entries.push({ key: 'optional', value: literal$1(true), quoted: false });
  3989. }
  3990. if (dep.host) {
  3991. entries.push({ key: 'host', value: literal$1(true), quoted: false });
  3992. }
  3993. if (dep.self) {
  3994. entries.push({ key: 'self', value: literal$1(true), quoted: false });
  3995. }
  3996. if (dep.skipSelf) {
  3997. entries.push({ key: 'skipSelf', value: literal$1(true), quoted: false });
  3998. }
  3999. return entries.length > 0 ? literalMap(entries) : null;
  4000. }
  4001. function isDelegatedFactoryMetadata(meta) {
  4002. return meta.delegateType !== undefined;
  4003. }
  4004. function isExpressionFactoryMetadata(meta) {
  4005. return meta.expression !== undefined;
  4006. }
  4007. function getInjectFn(target) {
  4008. switch (target) {
  4009. case exports.FactoryTarget.Component:
  4010. case exports.FactoryTarget.Directive:
  4011. case exports.FactoryTarget.Pipe:
  4012. return Identifiers.directiveInject;
  4013. case exports.FactoryTarget.NgModule:
  4014. case exports.FactoryTarget.Injectable:
  4015. default:
  4016. return Identifiers.inject;
  4017. }
  4018. }
  4019. class ParserError {
  4020. input;
  4021. errLocation;
  4022. ctxLocation;
  4023. message;
  4024. constructor(message, input, errLocation, ctxLocation) {
  4025. this.input = input;
  4026. this.errLocation = errLocation;
  4027. this.ctxLocation = ctxLocation;
  4028. this.message = `Parser Error: ${message} ${errLocation} [${input}] in ${ctxLocation}`;
  4029. }
  4030. }
  4031. class ParseSpan {
  4032. start;
  4033. end;
  4034. constructor(start, end) {
  4035. this.start = start;
  4036. this.end = end;
  4037. }
  4038. toAbsolute(absoluteOffset) {
  4039. return new AbsoluteSourceSpan(absoluteOffset + this.start, absoluteOffset + this.end);
  4040. }
  4041. }
  4042. class AST {
  4043. span;
  4044. sourceSpan;
  4045. constructor(span,
  4046. /**
  4047. * Absolute location of the expression AST in a source code file.
  4048. */
  4049. sourceSpan) {
  4050. this.span = span;
  4051. this.sourceSpan = sourceSpan;
  4052. }
  4053. toString() {
  4054. return 'AST';
  4055. }
  4056. }
  4057. class ASTWithName extends AST {
  4058. nameSpan;
  4059. constructor(span, sourceSpan, nameSpan) {
  4060. super(span, sourceSpan);
  4061. this.nameSpan = nameSpan;
  4062. }
  4063. }
  4064. let EmptyExpr$1 = class EmptyExpr extends AST {
  4065. visit(visitor, context = null) {
  4066. // do nothing
  4067. }
  4068. };
  4069. class ImplicitReceiver extends AST {
  4070. visit(visitor, context = null) {
  4071. return visitor.visitImplicitReceiver(this, context);
  4072. }
  4073. }
  4074. /**
  4075. * Receiver when something is accessed through `this` (e.g. `this.foo`). Note that this class
  4076. * inherits from `ImplicitReceiver`, because accessing something through `this` is treated the
  4077. * same as accessing it implicitly inside of an Angular template (e.g. `[attr.title]="this.title"`
  4078. * is the same as `[attr.title]="title"`.). Inheriting allows for the `this` accesses to be treated
  4079. * the same as implicit ones, except for a couple of exceptions like `$event` and `$any`.
  4080. * TODO: we should find a way for this class not to extend from `ImplicitReceiver` in the future.
  4081. */
  4082. class ThisReceiver extends ImplicitReceiver {
  4083. visit(visitor, context = null) {
  4084. return visitor.visitThisReceiver?.(this, context);
  4085. }
  4086. }
  4087. /**
  4088. * Multiple expressions separated by a semicolon.
  4089. */
  4090. class Chain extends AST {
  4091. expressions;
  4092. constructor(span, sourceSpan, expressions) {
  4093. super(span, sourceSpan);
  4094. this.expressions = expressions;
  4095. }
  4096. visit(visitor, context = null) {
  4097. return visitor.visitChain(this, context);
  4098. }
  4099. }
  4100. class Conditional extends AST {
  4101. condition;
  4102. trueExp;
  4103. falseExp;
  4104. constructor(span, sourceSpan, condition, trueExp, falseExp) {
  4105. super(span, sourceSpan);
  4106. this.condition = condition;
  4107. this.trueExp = trueExp;
  4108. this.falseExp = falseExp;
  4109. }
  4110. visit(visitor, context = null) {
  4111. return visitor.visitConditional(this, context);
  4112. }
  4113. }
  4114. class PropertyRead extends ASTWithName {
  4115. receiver;
  4116. name;
  4117. constructor(span, sourceSpan, nameSpan, receiver, name) {
  4118. super(span, sourceSpan, nameSpan);
  4119. this.receiver = receiver;
  4120. this.name = name;
  4121. }
  4122. visit(visitor, context = null) {
  4123. return visitor.visitPropertyRead(this, context);
  4124. }
  4125. }
  4126. class PropertyWrite extends ASTWithName {
  4127. receiver;
  4128. name;
  4129. value;
  4130. constructor(span, sourceSpan, nameSpan, receiver, name, value) {
  4131. super(span, sourceSpan, nameSpan);
  4132. this.receiver = receiver;
  4133. this.name = name;
  4134. this.value = value;
  4135. }
  4136. visit(visitor, context = null) {
  4137. return visitor.visitPropertyWrite(this, context);
  4138. }
  4139. }
  4140. class SafePropertyRead extends ASTWithName {
  4141. receiver;
  4142. name;
  4143. constructor(span, sourceSpan, nameSpan, receiver, name) {
  4144. super(span, sourceSpan, nameSpan);
  4145. this.receiver = receiver;
  4146. this.name = name;
  4147. }
  4148. visit(visitor, context = null) {
  4149. return visitor.visitSafePropertyRead(this, context);
  4150. }
  4151. }
  4152. class KeyedRead extends AST {
  4153. receiver;
  4154. key;
  4155. constructor(span, sourceSpan, receiver, key) {
  4156. super(span, sourceSpan);
  4157. this.receiver = receiver;
  4158. this.key = key;
  4159. }
  4160. visit(visitor, context = null) {
  4161. return visitor.visitKeyedRead(this, context);
  4162. }
  4163. }
  4164. class SafeKeyedRead extends AST {
  4165. receiver;
  4166. key;
  4167. constructor(span, sourceSpan, receiver, key) {
  4168. super(span, sourceSpan);
  4169. this.receiver = receiver;
  4170. this.key = key;
  4171. }
  4172. visit(visitor, context = null) {
  4173. return visitor.visitSafeKeyedRead(this, context);
  4174. }
  4175. }
  4176. class KeyedWrite extends AST {
  4177. receiver;
  4178. key;
  4179. value;
  4180. constructor(span, sourceSpan, receiver, key, value) {
  4181. super(span, sourceSpan);
  4182. this.receiver = receiver;
  4183. this.key = key;
  4184. this.value = value;
  4185. }
  4186. visit(visitor, context = null) {
  4187. return visitor.visitKeyedWrite(this, context);
  4188. }
  4189. }
  4190. class BindingPipe extends ASTWithName {
  4191. exp;
  4192. name;
  4193. args;
  4194. constructor(span, sourceSpan, exp, name, args, nameSpan) {
  4195. super(span, sourceSpan, nameSpan);
  4196. this.exp = exp;
  4197. this.name = name;
  4198. this.args = args;
  4199. }
  4200. visit(visitor, context = null) {
  4201. return visitor.visitPipe(this, context);
  4202. }
  4203. }
  4204. class LiteralPrimitive extends AST {
  4205. value;
  4206. constructor(span, sourceSpan, value) {
  4207. super(span, sourceSpan);
  4208. this.value = value;
  4209. }
  4210. visit(visitor, context = null) {
  4211. return visitor.visitLiteralPrimitive(this, context);
  4212. }
  4213. }
  4214. class LiteralArray extends AST {
  4215. expressions;
  4216. constructor(span, sourceSpan, expressions) {
  4217. super(span, sourceSpan);
  4218. this.expressions = expressions;
  4219. }
  4220. visit(visitor, context = null) {
  4221. return visitor.visitLiteralArray(this, context);
  4222. }
  4223. }
  4224. class LiteralMap extends AST {
  4225. keys;
  4226. values;
  4227. constructor(span, sourceSpan, keys, values) {
  4228. super(span, sourceSpan);
  4229. this.keys = keys;
  4230. this.values = values;
  4231. }
  4232. visit(visitor, context = null) {
  4233. return visitor.visitLiteralMap(this, context);
  4234. }
  4235. }
  4236. let Interpolation$1 = class Interpolation extends AST {
  4237. strings;
  4238. expressions;
  4239. constructor(span, sourceSpan, strings, expressions) {
  4240. super(span, sourceSpan);
  4241. this.strings = strings;
  4242. this.expressions = expressions;
  4243. }
  4244. visit(visitor, context = null) {
  4245. return visitor.visitInterpolation(this, context);
  4246. }
  4247. };
  4248. class Binary extends AST {
  4249. operation;
  4250. left;
  4251. right;
  4252. constructor(span, sourceSpan, operation, left, right) {
  4253. super(span, sourceSpan);
  4254. this.operation = operation;
  4255. this.left = left;
  4256. this.right = right;
  4257. }
  4258. visit(visitor, context = null) {
  4259. return visitor.visitBinary(this, context);
  4260. }
  4261. }
  4262. /**
  4263. * For backwards compatibility reasons, `Unary` inherits from `Binary` and mimics the binary AST
  4264. * node that was originally used. This inheritance relation can be deleted in some future major,
  4265. * after consumers have been given a chance to fully support Unary.
  4266. */
  4267. class Unary extends Binary {
  4268. operator;
  4269. expr;
  4270. // Redeclare the properties that are inherited from `Binary` as `never`, as consumers should not
  4271. // depend on these fields when operating on `Unary`.
  4272. left = null;
  4273. right = null;
  4274. operation = null;
  4275. /**
  4276. * Creates a unary minus expression "-x", represented as `Binary` using "0 - x".
  4277. */
  4278. static createMinus(span, sourceSpan, expr) {
  4279. return new Unary(span, sourceSpan, '-', expr, '-', new LiteralPrimitive(span, sourceSpan, 0), expr);
  4280. }
  4281. /**
  4282. * Creates a unary plus expression "+x", represented as `Binary` using "x - 0".
  4283. */
  4284. static createPlus(span, sourceSpan, expr) {
  4285. return new Unary(span, sourceSpan, '+', expr, '-', expr, new LiteralPrimitive(span, sourceSpan, 0));
  4286. }
  4287. /**
  4288. * During the deprecation period this constructor is private, to avoid consumers from creating
  4289. * a `Unary` with the fallback properties for `Binary`.
  4290. */
  4291. constructor(span, sourceSpan, operator, expr, binaryOp, binaryLeft, binaryRight) {
  4292. super(span, sourceSpan, binaryOp, binaryLeft, binaryRight);
  4293. this.operator = operator;
  4294. this.expr = expr;
  4295. }
  4296. visit(visitor, context = null) {
  4297. if (visitor.visitUnary !== undefined) {
  4298. return visitor.visitUnary(this, context);
  4299. }
  4300. return visitor.visitBinary(this, context);
  4301. }
  4302. }
  4303. class PrefixNot extends AST {
  4304. expression;
  4305. constructor(span, sourceSpan, expression) {
  4306. super(span, sourceSpan);
  4307. this.expression = expression;
  4308. }
  4309. visit(visitor, context = null) {
  4310. return visitor.visitPrefixNot(this, context);
  4311. }
  4312. }
  4313. class TypeofExpression extends AST {
  4314. expression;
  4315. constructor(span, sourceSpan, expression) {
  4316. super(span, sourceSpan);
  4317. this.expression = expression;
  4318. }
  4319. visit(visitor, context = null) {
  4320. return visitor.visitTypeofExpression(this, context);
  4321. }
  4322. }
  4323. class NonNullAssert extends AST {
  4324. expression;
  4325. constructor(span, sourceSpan, expression) {
  4326. super(span, sourceSpan);
  4327. this.expression = expression;
  4328. }
  4329. visit(visitor, context = null) {
  4330. return visitor.visitNonNullAssert(this, context);
  4331. }
  4332. }
  4333. class Call extends AST {
  4334. receiver;
  4335. args;
  4336. argumentSpan;
  4337. constructor(span, sourceSpan, receiver, args, argumentSpan) {
  4338. super(span, sourceSpan);
  4339. this.receiver = receiver;
  4340. this.args = args;
  4341. this.argumentSpan = argumentSpan;
  4342. }
  4343. visit(visitor, context = null) {
  4344. return visitor.visitCall(this, context);
  4345. }
  4346. }
  4347. class SafeCall extends AST {
  4348. receiver;
  4349. args;
  4350. argumentSpan;
  4351. constructor(span, sourceSpan, receiver, args, argumentSpan) {
  4352. super(span, sourceSpan);
  4353. this.receiver = receiver;
  4354. this.args = args;
  4355. this.argumentSpan = argumentSpan;
  4356. }
  4357. visit(visitor, context = null) {
  4358. return visitor.visitSafeCall(this, context);
  4359. }
  4360. }
  4361. class TemplateLiteral extends AST {
  4362. elements;
  4363. expressions;
  4364. constructor(span, sourceSpan, elements, expressions) {
  4365. super(span, sourceSpan);
  4366. this.elements = elements;
  4367. this.expressions = expressions;
  4368. }
  4369. visit(visitor, context) {
  4370. return visitor.visitTemplateLiteral(this, context);
  4371. }
  4372. }
  4373. class TemplateLiteralElement extends AST {
  4374. text;
  4375. constructor(span, sourceSpan, text) {
  4376. super(span, sourceSpan);
  4377. this.text = text;
  4378. }
  4379. visit(visitor, context) {
  4380. return visitor.visitTemplateLiteralElement(this, context);
  4381. }
  4382. }
  4383. /**
  4384. * Records the absolute position of a text span in a source file, where `start` and `end` are the
  4385. * starting and ending byte offsets, respectively, of the text span in a source file.
  4386. */
  4387. class AbsoluteSourceSpan {
  4388. start;
  4389. end;
  4390. constructor(start, end) {
  4391. this.start = start;
  4392. this.end = end;
  4393. }
  4394. }
  4395. class ASTWithSource extends AST {
  4396. ast;
  4397. source;
  4398. location;
  4399. errors;
  4400. constructor(ast, source, location, absoluteOffset, errors) {
  4401. super(new ParseSpan(0, source === null ? 0 : source.length), new AbsoluteSourceSpan(absoluteOffset, source === null ? absoluteOffset : absoluteOffset + source.length));
  4402. this.ast = ast;
  4403. this.source = source;
  4404. this.location = location;
  4405. this.errors = errors;
  4406. }
  4407. visit(visitor, context = null) {
  4408. if (visitor.visitASTWithSource) {
  4409. return visitor.visitASTWithSource(this, context);
  4410. }
  4411. return this.ast.visit(visitor, context);
  4412. }
  4413. toString() {
  4414. return `${this.source} in ${this.location}`;
  4415. }
  4416. }
  4417. class VariableBinding {
  4418. sourceSpan;
  4419. key;
  4420. value;
  4421. /**
  4422. * @param sourceSpan entire span of the binding.
  4423. * @param key name of the LHS along with its span.
  4424. * @param value optional value for the RHS along with its span.
  4425. */
  4426. constructor(sourceSpan, key, value) {
  4427. this.sourceSpan = sourceSpan;
  4428. this.key = key;
  4429. this.value = value;
  4430. }
  4431. }
  4432. class ExpressionBinding {
  4433. sourceSpan;
  4434. key;
  4435. value;
  4436. /**
  4437. * @param sourceSpan entire span of the binding.
  4438. * @param key binding name, like ngForOf, ngForTrackBy, ngIf, along with its
  4439. * span. Note that the length of the span may not be the same as
  4440. * `key.source.length`. For example,
  4441. * 1. key.source = ngFor, key.span is for "ngFor"
  4442. * 2. key.source = ngForOf, key.span is for "of"
  4443. * 3. key.source = ngForTrackBy, key.span is for "trackBy"
  4444. * @param value optional expression for the RHS.
  4445. */
  4446. constructor(sourceSpan, key, value) {
  4447. this.sourceSpan = sourceSpan;
  4448. this.key = key;
  4449. this.value = value;
  4450. }
  4451. }
  4452. class RecursiveAstVisitor {
  4453. visit(ast, context) {
  4454. // The default implementation just visits every node.
  4455. // Classes that extend RecursiveAstVisitor should override this function
  4456. // to selectively visit the specified node.
  4457. ast.visit(this, context);
  4458. }
  4459. visitUnary(ast, context) {
  4460. this.visit(ast.expr, context);
  4461. }
  4462. visitBinary(ast, context) {
  4463. this.visit(ast.left, context);
  4464. this.visit(ast.right, context);
  4465. }
  4466. visitChain(ast, context) {
  4467. this.visitAll(ast.expressions, context);
  4468. }
  4469. visitConditional(ast, context) {
  4470. this.visit(ast.condition, context);
  4471. this.visit(ast.trueExp, context);
  4472. this.visit(ast.falseExp, context);
  4473. }
  4474. visitPipe(ast, context) {
  4475. this.visit(ast.exp, context);
  4476. this.visitAll(ast.args, context);
  4477. }
  4478. visitImplicitReceiver(ast, context) { }
  4479. visitThisReceiver(ast, context) { }
  4480. visitInterpolation(ast, context) {
  4481. this.visitAll(ast.expressions, context);
  4482. }
  4483. visitKeyedRead(ast, context) {
  4484. this.visit(ast.receiver, context);
  4485. this.visit(ast.key, context);
  4486. }
  4487. visitKeyedWrite(ast, context) {
  4488. this.visit(ast.receiver, context);
  4489. this.visit(ast.key, context);
  4490. this.visit(ast.value, context);
  4491. }
  4492. visitLiteralArray(ast, context) {
  4493. this.visitAll(ast.expressions, context);
  4494. }
  4495. visitLiteralMap(ast, context) {
  4496. this.visitAll(ast.values, context);
  4497. }
  4498. visitLiteralPrimitive(ast, context) { }
  4499. visitPrefixNot(ast, context) {
  4500. this.visit(ast.expression, context);
  4501. }
  4502. visitTypeofExpression(ast, context) {
  4503. this.visit(ast.expression, context);
  4504. }
  4505. visitNonNullAssert(ast, context) {
  4506. this.visit(ast.expression, context);
  4507. }
  4508. visitPropertyRead(ast, context) {
  4509. this.visit(ast.receiver, context);
  4510. }
  4511. visitPropertyWrite(ast, context) {
  4512. this.visit(ast.receiver, context);
  4513. this.visit(ast.value, context);
  4514. }
  4515. visitSafePropertyRead(ast, context) {
  4516. this.visit(ast.receiver, context);
  4517. }
  4518. visitSafeKeyedRead(ast, context) {
  4519. this.visit(ast.receiver, context);
  4520. this.visit(ast.key, context);
  4521. }
  4522. visitCall(ast, context) {
  4523. this.visit(ast.receiver, context);
  4524. this.visitAll(ast.args, context);
  4525. }
  4526. visitSafeCall(ast, context) {
  4527. this.visit(ast.receiver, context);
  4528. this.visitAll(ast.args, context);
  4529. }
  4530. visitTemplateLiteral(ast, context) {
  4531. // Iterate in the declaration order. Note that there will
  4532. // always be one expression less than the number of elements.
  4533. for (let i = 0; i < ast.elements.length; i++) {
  4534. this.visit(ast.elements[i], context);
  4535. const expression = i < ast.expressions.length ? ast.expressions[i] : null;
  4536. if (expression !== null) {
  4537. this.visit(expression, context);
  4538. }
  4539. }
  4540. }
  4541. visitTemplateLiteralElement(ast, context) { }
  4542. // This is not part of the AstVisitor interface, just a helper method
  4543. visitAll(asts, context) {
  4544. for (const ast of asts) {
  4545. this.visit(ast, context);
  4546. }
  4547. }
  4548. }
  4549. // Bindings
  4550. class ParsedProperty {
  4551. name;
  4552. expression;
  4553. type;
  4554. sourceSpan;
  4555. keySpan;
  4556. valueSpan;
  4557. isLiteral;
  4558. isAnimation;
  4559. constructor(name, expression, type, sourceSpan, keySpan, valueSpan) {
  4560. this.name = name;
  4561. this.expression = expression;
  4562. this.type = type;
  4563. this.sourceSpan = sourceSpan;
  4564. this.keySpan = keySpan;
  4565. this.valueSpan = valueSpan;
  4566. this.isLiteral = this.type === ParsedPropertyType.LITERAL_ATTR;
  4567. this.isAnimation = this.type === ParsedPropertyType.ANIMATION;
  4568. }
  4569. }
  4570. var ParsedPropertyType;
  4571. (function (ParsedPropertyType) {
  4572. ParsedPropertyType[ParsedPropertyType["DEFAULT"] = 0] = "DEFAULT";
  4573. ParsedPropertyType[ParsedPropertyType["LITERAL_ATTR"] = 1] = "LITERAL_ATTR";
  4574. ParsedPropertyType[ParsedPropertyType["ANIMATION"] = 2] = "ANIMATION";
  4575. ParsedPropertyType[ParsedPropertyType["TWO_WAY"] = 3] = "TWO_WAY";
  4576. })(ParsedPropertyType || (ParsedPropertyType = {}));
  4577. exports.ParsedEventType = void 0;
  4578. (function (ParsedEventType) {
  4579. // DOM or Directive event
  4580. ParsedEventType[ParsedEventType["Regular"] = 0] = "Regular";
  4581. // Animation specific event
  4582. ParsedEventType[ParsedEventType["Animation"] = 1] = "Animation";
  4583. // Event side of a two-way binding (e.g. `[(property)]="expression"`).
  4584. ParsedEventType[ParsedEventType["TwoWay"] = 2] = "TwoWay";
  4585. })(exports.ParsedEventType || (exports.ParsedEventType = {}));
  4586. class ParsedEvent {
  4587. name;
  4588. targetOrPhase;
  4589. type;
  4590. handler;
  4591. sourceSpan;
  4592. handlerSpan;
  4593. keySpan;
  4594. constructor(name, targetOrPhase, type, handler, sourceSpan, handlerSpan, keySpan) {
  4595. this.name = name;
  4596. this.targetOrPhase = targetOrPhase;
  4597. this.type = type;
  4598. this.handler = handler;
  4599. this.sourceSpan = sourceSpan;
  4600. this.handlerSpan = handlerSpan;
  4601. this.keySpan = keySpan;
  4602. }
  4603. }
  4604. /**
  4605. * ParsedVariable represents a variable declaration in a microsyntax expression.
  4606. */
  4607. class ParsedVariable {
  4608. name;
  4609. value;
  4610. sourceSpan;
  4611. keySpan;
  4612. valueSpan;
  4613. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  4614. this.name = name;
  4615. this.value = value;
  4616. this.sourceSpan = sourceSpan;
  4617. this.keySpan = keySpan;
  4618. this.valueSpan = valueSpan;
  4619. }
  4620. }
  4621. exports.BindingType = void 0;
  4622. (function (BindingType) {
  4623. // A regular binding to a property (e.g. `[property]="expression"`).
  4624. BindingType[BindingType["Property"] = 0] = "Property";
  4625. // A binding to an element attribute (e.g. `[attr.name]="expression"`).
  4626. BindingType[BindingType["Attribute"] = 1] = "Attribute";
  4627. // A binding to a CSS class (e.g. `[class.name]="condition"`).
  4628. BindingType[BindingType["Class"] = 2] = "Class";
  4629. // A binding to a style rule (e.g. `[style.rule]="expression"`).
  4630. BindingType[BindingType["Style"] = 3] = "Style";
  4631. // A binding to an animation reference (e.g. `[animate.key]="expression"`).
  4632. BindingType[BindingType["Animation"] = 4] = "Animation";
  4633. // Property side of a two-way binding (e.g. `[(property)]="expression"`).
  4634. BindingType[BindingType["TwoWay"] = 5] = "TwoWay";
  4635. })(exports.BindingType || (exports.BindingType = {}));
  4636. class BoundElementProperty {
  4637. name;
  4638. type;
  4639. securityContext;
  4640. value;
  4641. unit;
  4642. sourceSpan;
  4643. keySpan;
  4644. valueSpan;
  4645. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan) {
  4646. this.name = name;
  4647. this.type = type;
  4648. this.securityContext = securityContext;
  4649. this.value = value;
  4650. this.unit = unit;
  4651. this.sourceSpan = sourceSpan;
  4652. this.keySpan = keySpan;
  4653. this.valueSpan = valueSpan;
  4654. }
  4655. }
  4656. exports.TagContentType = void 0;
  4657. (function (TagContentType) {
  4658. TagContentType[TagContentType["RAW_TEXT"] = 0] = "RAW_TEXT";
  4659. TagContentType[TagContentType["ESCAPABLE_RAW_TEXT"] = 1] = "ESCAPABLE_RAW_TEXT";
  4660. TagContentType[TagContentType["PARSABLE_DATA"] = 2] = "PARSABLE_DATA";
  4661. })(exports.TagContentType || (exports.TagContentType = {}));
  4662. function splitNsName(elementName, fatal = true) {
  4663. if (elementName[0] != ':') {
  4664. return [null, elementName];
  4665. }
  4666. const colonIndex = elementName.indexOf(':', 1);
  4667. if (colonIndex === -1) {
  4668. if (fatal) {
  4669. throw new Error(`Unsupported format "${elementName}" expecting ":namespace:name"`);
  4670. }
  4671. else {
  4672. return [null, elementName];
  4673. }
  4674. }
  4675. return [elementName.slice(1, colonIndex), elementName.slice(colonIndex + 1)];
  4676. }
  4677. // `<ng-container>` tags work the same regardless the namespace
  4678. function isNgContainer(tagName) {
  4679. return splitNsName(tagName)[1] === 'ng-container';
  4680. }
  4681. // `<ng-content>` tags work the same regardless the namespace
  4682. function isNgContent(tagName) {
  4683. return splitNsName(tagName)[1] === 'ng-content';
  4684. }
  4685. // `<ng-template>` tags work the same regardless the namespace
  4686. function isNgTemplate(tagName) {
  4687. return splitNsName(tagName)[1] === 'ng-template';
  4688. }
  4689. function getNsPrefix(fullName) {
  4690. return fullName === null ? null : splitNsName(fullName)[0];
  4691. }
  4692. function mergeNsAndName(prefix, localName) {
  4693. return prefix ? `:${prefix}:${localName}` : localName;
  4694. }
  4695. /**
  4696. * This is an R3 `Node`-like wrapper for a raw `html.Comment` node. We do not currently
  4697. * require the implementation of a visitor for Comments as they are only collected at
  4698. * the top-level of the R3 AST, and only if `Render3ParseOptions['collectCommentNodes']`
  4699. * is true.
  4700. */
  4701. let Comment$1 = class Comment {
  4702. value;
  4703. sourceSpan;
  4704. constructor(value, sourceSpan) {
  4705. this.value = value;
  4706. this.sourceSpan = sourceSpan;
  4707. }
  4708. visit(_visitor) {
  4709. throw new Error('visit() not implemented for Comment');
  4710. }
  4711. };
  4712. let Text$3 = class Text {
  4713. value;
  4714. sourceSpan;
  4715. constructor(value, sourceSpan) {
  4716. this.value = value;
  4717. this.sourceSpan = sourceSpan;
  4718. }
  4719. visit(visitor) {
  4720. return visitor.visitText(this);
  4721. }
  4722. };
  4723. class BoundText {
  4724. value;
  4725. sourceSpan;
  4726. i18n;
  4727. constructor(value, sourceSpan, i18n) {
  4728. this.value = value;
  4729. this.sourceSpan = sourceSpan;
  4730. this.i18n = i18n;
  4731. }
  4732. visit(visitor) {
  4733. return visitor.visitBoundText(this);
  4734. }
  4735. }
  4736. /**
  4737. * Represents a text attribute in the template.
  4738. *
  4739. * `valueSpan` may not be present in cases where there is no value `<div a></div>`.
  4740. * `keySpan` may also not be present for synthetic attributes from ICU expansions.
  4741. */
  4742. class TextAttribute {
  4743. name;
  4744. value;
  4745. sourceSpan;
  4746. keySpan;
  4747. valueSpan;
  4748. i18n;
  4749. constructor(name, value, sourceSpan, keySpan, valueSpan, i18n) {
  4750. this.name = name;
  4751. this.value = value;
  4752. this.sourceSpan = sourceSpan;
  4753. this.keySpan = keySpan;
  4754. this.valueSpan = valueSpan;
  4755. this.i18n = i18n;
  4756. }
  4757. visit(visitor) {
  4758. return visitor.visitTextAttribute(this);
  4759. }
  4760. }
  4761. class BoundAttribute {
  4762. name;
  4763. type;
  4764. securityContext;
  4765. value;
  4766. unit;
  4767. sourceSpan;
  4768. keySpan;
  4769. valueSpan;
  4770. i18n;
  4771. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan, i18n) {
  4772. this.name = name;
  4773. this.type = type;
  4774. this.securityContext = securityContext;
  4775. this.value = value;
  4776. this.unit = unit;
  4777. this.sourceSpan = sourceSpan;
  4778. this.keySpan = keySpan;
  4779. this.valueSpan = valueSpan;
  4780. this.i18n = i18n;
  4781. }
  4782. static fromBoundElementProperty(prop, i18n) {
  4783. if (prop.keySpan === undefined) {
  4784. throw new Error(`Unexpected state: keySpan must be defined for bound attributes but was not for ${prop.name}: ${prop.sourceSpan}`);
  4785. }
  4786. return new BoundAttribute(prop.name, prop.type, prop.securityContext, prop.value, prop.unit, prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n);
  4787. }
  4788. visit(visitor) {
  4789. return visitor.visitBoundAttribute(this);
  4790. }
  4791. }
  4792. class BoundEvent {
  4793. name;
  4794. type;
  4795. handler;
  4796. target;
  4797. phase;
  4798. sourceSpan;
  4799. handlerSpan;
  4800. keySpan;
  4801. constructor(name, type, handler, target, phase, sourceSpan, handlerSpan, keySpan) {
  4802. this.name = name;
  4803. this.type = type;
  4804. this.handler = handler;
  4805. this.target = target;
  4806. this.phase = phase;
  4807. this.sourceSpan = sourceSpan;
  4808. this.handlerSpan = handlerSpan;
  4809. this.keySpan = keySpan;
  4810. }
  4811. static fromParsedEvent(event) {
  4812. const target = event.type === exports.ParsedEventType.Regular ? event.targetOrPhase : null;
  4813. const phase = event.type === exports.ParsedEventType.Animation ? event.targetOrPhase : null;
  4814. if (event.keySpan === undefined) {
  4815. throw new Error(`Unexpected state: keySpan must be defined for bound event but was not for ${event.name}: ${event.sourceSpan}`);
  4816. }
  4817. return new BoundEvent(event.name, event.type, event.handler, target, phase, event.sourceSpan, event.handlerSpan, event.keySpan);
  4818. }
  4819. visit(visitor) {
  4820. return visitor.visitBoundEvent(this);
  4821. }
  4822. }
  4823. let Element$1 = class Element {
  4824. name;
  4825. attributes;
  4826. inputs;
  4827. outputs;
  4828. children;
  4829. references;
  4830. sourceSpan;
  4831. startSourceSpan;
  4832. endSourceSpan;
  4833. i18n;
  4834. constructor(name, attributes, inputs, outputs, children, references, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  4835. this.name = name;
  4836. this.attributes = attributes;
  4837. this.inputs = inputs;
  4838. this.outputs = outputs;
  4839. this.children = children;
  4840. this.references = references;
  4841. this.sourceSpan = sourceSpan;
  4842. this.startSourceSpan = startSourceSpan;
  4843. this.endSourceSpan = endSourceSpan;
  4844. this.i18n = i18n;
  4845. }
  4846. visit(visitor) {
  4847. return visitor.visitElement(this);
  4848. }
  4849. };
  4850. class DeferredTrigger {
  4851. nameSpan;
  4852. sourceSpan;
  4853. prefetchSpan;
  4854. whenOrOnSourceSpan;
  4855. hydrateSpan;
  4856. constructor(nameSpan, sourceSpan, prefetchSpan, whenOrOnSourceSpan, hydrateSpan) {
  4857. this.nameSpan = nameSpan;
  4858. this.sourceSpan = sourceSpan;
  4859. this.prefetchSpan = prefetchSpan;
  4860. this.whenOrOnSourceSpan = whenOrOnSourceSpan;
  4861. this.hydrateSpan = hydrateSpan;
  4862. }
  4863. visit(visitor) {
  4864. return visitor.visitDeferredTrigger(this);
  4865. }
  4866. }
  4867. class BoundDeferredTrigger extends DeferredTrigger {
  4868. value;
  4869. constructor(value, sourceSpan, prefetchSpan, whenSourceSpan, hydrateSpan) {
  4870. // BoundDeferredTrigger is for 'when' triggers. These aren't really "triggers" and don't have a
  4871. // nameSpan. Trigger names are the built in event triggers like hover, interaction, etc.
  4872. super(/** nameSpan */ null, sourceSpan, prefetchSpan, whenSourceSpan, hydrateSpan);
  4873. this.value = value;
  4874. }
  4875. }
  4876. class NeverDeferredTrigger extends DeferredTrigger {
  4877. }
  4878. class IdleDeferredTrigger extends DeferredTrigger {
  4879. }
  4880. class ImmediateDeferredTrigger extends DeferredTrigger {
  4881. }
  4882. class HoverDeferredTrigger extends DeferredTrigger {
  4883. reference;
  4884. constructor(reference, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  4885. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  4886. this.reference = reference;
  4887. }
  4888. }
  4889. class TimerDeferredTrigger extends DeferredTrigger {
  4890. delay;
  4891. constructor(delay, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  4892. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  4893. this.delay = delay;
  4894. }
  4895. }
  4896. class InteractionDeferredTrigger extends DeferredTrigger {
  4897. reference;
  4898. constructor(reference, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  4899. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  4900. this.reference = reference;
  4901. }
  4902. }
  4903. class ViewportDeferredTrigger extends DeferredTrigger {
  4904. reference;
  4905. constructor(reference, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  4906. super(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  4907. this.reference = reference;
  4908. }
  4909. }
  4910. class BlockNode {
  4911. nameSpan;
  4912. sourceSpan;
  4913. startSourceSpan;
  4914. endSourceSpan;
  4915. constructor(nameSpan, sourceSpan, startSourceSpan, endSourceSpan) {
  4916. this.nameSpan = nameSpan;
  4917. this.sourceSpan = sourceSpan;
  4918. this.startSourceSpan = startSourceSpan;
  4919. this.endSourceSpan = endSourceSpan;
  4920. }
  4921. }
  4922. class DeferredBlockPlaceholder extends BlockNode {
  4923. children;
  4924. minimumTime;
  4925. i18n;
  4926. constructor(children, minimumTime, nameSpan, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  4927. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  4928. this.children = children;
  4929. this.minimumTime = minimumTime;
  4930. this.i18n = i18n;
  4931. }
  4932. visit(visitor) {
  4933. return visitor.visitDeferredBlockPlaceholder(this);
  4934. }
  4935. }
  4936. class DeferredBlockLoading extends BlockNode {
  4937. children;
  4938. afterTime;
  4939. minimumTime;
  4940. i18n;
  4941. constructor(children, afterTime, minimumTime, nameSpan, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  4942. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  4943. this.children = children;
  4944. this.afterTime = afterTime;
  4945. this.minimumTime = minimumTime;
  4946. this.i18n = i18n;
  4947. }
  4948. visit(visitor) {
  4949. return visitor.visitDeferredBlockLoading(this);
  4950. }
  4951. }
  4952. class DeferredBlockError extends BlockNode {
  4953. children;
  4954. i18n;
  4955. constructor(children, nameSpan, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  4956. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  4957. this.children = children;
  4958. this.i18n = i18n;
  4959. }
  4960. visit(visitor) {
  4961. return visitor.visitDeferredBlockError(this);
  4962. }
  4963. }
  4964. class DeferredBlock extends BlockNode {
  4965. children;
  4966. placeholder;
  4967. loading;
  4968. error;
  4969. mainBlockSpan;
  4970. i18n;
  4971. triggers;
  4972. prefetchTriggers;
  4973. hydrateTriggers;
  4974. definedTriggers;
  4975. definedPrefetchTriggers;
  4976. definedHydrateTriggers;
  4977. constructor(children, triggers, prefetchTriggers, hydrateTriggers, placeholder, loading, error, nameSpan, sourceSpan, mainBlockSpan, startSourceSpan, endSourceSpan, i18n) {
  4978. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  4979. this.children = children;
  4980. this.placeholder = placeholder;
  4981. this.loading = loading;
  4982. this.error = error;
  4983. this.mainBlockSpan = mainBlockSpan;
  4984. this.i18n = i18n;
  4985. this.triggers = triggers;
  4986. this.prefetchTriggers = prefetchTriggers;
  4987. this.hydrateTriggers = hydrateTriggers;
  4988. // We cache the keys since we know that they won't change and we
  4989. // don't want to enumarate them every time we're traversing the AST.
  4990. this.definedTriggers = Object.keys(triggers);
  4991. this.definedPrefetchTriggers = Object.keys(prefetchTriggers);
  4992. this.definedHydrateTriggers = Object.keys(hydrateTriggers);
  4993. }
  4994. visit(visitor) {
  4995. return visitor.visitDeferredBlock(this);
  4996. }
  4997. visitAll(visitor) {
  4998. // Visit the hydrate triggers first to match their insertion order.
  4999. this.visitTriggers(this.definedHydrateTriggers, this.hydrateTriggers, visitor);
  5000. this.visitTriggers(this.definedTriggers, this.triggers, visitor);
  5001. this.visitTriggers(this.definedPrefetchTriggers, this.prefetchTriggers, visitor);
  5002. visitAll$1(visitor, this.children);
  5003. const remainingBlocks = [this.placeholder, this.loading, this.error].filter((x) => x !== null);
  5004. visitAll$1(visitor, remainingBlocks);
  5005. }
  5006. visitTriggers(keys, triggers, visitor) {
  5007. visitAll$1(visitor, keys.map((k) => triggers[k]));
  5008. }
  5009. }
  5010. class SwitchBlock extends BlockNode {
  5011. expression;
  5012. cases;
  5013. unknownBlocks;
  5014. constructor(expression, cases,
  5015. /**
  5016. * These blocks are only captured to allow for autocompletion in the language service. They
  5017. * aren't meant to be processed in any other way.
  5018. */
  5019. unknownBlocks, sourceSpan, startSourceSpan, endSourceSpan, nameSpan) {
  5020. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5021. this.expression = expression;
  5022. this.cases = cases;
  5023. this.unknownBlocks = unknownBlocks;
  5024. }
  5025. visit(visitor) {
  5026. return visitor.visitSwitchBlock(this);
  5027. }
  5028. }
  5029. class SwitchBlockCase extends BlockNode {
  5030. expression;
  5031. children;
  5032. i18n;
  5033. constructor(expression, children, sourceSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5034. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5035. this.expression = expression;
  5036. this.children = children;
  5037. this.i18n = i18n;
  5038. }
  5039. visit(visitor) {
  5040. return visitor.visitSwitchBlockCase(this);
  5041. }
  5042. }
  5043. class ForLoopBlock extends BlockNode {
  5044. item;
  5045. expression;
  5046. trackBy;
  5047. trackKeywordSpan;
  5048. contextVariables;
  5049. children;
  5050. empty;
  5051. mainBlockSpan;
  5052. i18n;
  5053. constructor(item, expression, trackBy, trackKeywordSpan, contextVariables, children, empty, sourceSpan, mainBlockSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5054. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5055. this.item = item;
  5056. this.expression = expression;
  5057. this.trackBy = trackBy;
  5058. this.trackKeywordSpan = trackKeywordSpan;
  5059. this.contextVariables = contextVariables;
  5060. this.children = children;
  5061. this.empty = empty;
  5062. this.mainBlockSpan = mainBlockSpan;
  5063. this.i18n = i18n;
  5064. }
  5065. visit(visitor) {
  5066. return visitor.visitForLoopBlock(this);
  5067. }
  5068. }
  5069. class ForLoopBlockEmpty extends BlockNode {
  5070. children;
  5071. i18n;
  5072. constructor(children, sourceSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5073. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5074. this.children = children;
  5075. this.i18n = i18n;
  5076. }
  5077. visit(visitor) {
  5078. return visitor.visitForLoopBlockEmpty(this);
  5079. }
  5080. }
  5081. class IfBlock extends BlockNode {
  5082. branches;
  5083. constructor(branches, sourceSpan, startSourceSpan, endSourceSpan, nameSpan) {
  5084. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5085. this.branches = branches;
  5086. }
  5087. visit(visitor) {
  5088. return visitor.visitIfBlock(this);
  5089. }
  5090. }
  5091. class IfBlockBranch extends BlockNode {
  5092. expression;
  5093. children;
  5094. expressionAlias;
  5095. i18n;
  5096. constructor(expression, children, expressionAlias, sourceSpan, startSourceSpan, endSourceSpan, nameSpan, i18n) {
  5097. super(nameSpan, sourceSpan, startSourceSpan, endSourceSpan);
  5098. this.expression = expression;
  5099. this.children = children;
  5100. this.expressionAlias = expressionAlias;
  5101. this.i18n = i18n;
  5102. }
  5103. visit(visitor) {
  5104. return visitor.visitIfBlockBranch(this);
  5105. }
  5106. }
  5107. class UnknownBlock {
  5108. name;
  5109. sourceSpan;
  5110. nameSpan;
  5111. constructor(name, sourceSpan, nameSpan) {
  5112. this.name = name;
  5113. this.sourceSpan = sourceSpan;
  5114. this.nameSpan = nameSpan;
  5115. }
  5116. visit(visitor) {
  5117. return visitor.visitUnknownBlock(this);
  5118. }
  5119. }
  5120. let LetDeclaration$1 = class LetDeclaration {
  5121. name;
  5122. value;
  5123. sourceSpan;
  5124. nameSpan;
  5125. valueSpan;
  5126. constructor(name, value, sourceSpan, nameSpan, valueSpan) {
  5127. this.name = name;
  5128. this.value = value;
  5129. this.sourceSpan = sourceSpan;
  5130. this.nameSpan = nameSpan;
  5131. this.valueSpan = valueSpan;
  5132. }
  5133. visit(visitor) {
  5134. return visitor.visitLetDeclaration(this);
  5135. }
  5136. };
  5137. class Template {
  5138. tagName;
  5139. attributes;
  5140. inputs;
  5141. outputs;
  5142. templateAttrs;
  5143. children;
  5144. references;
  5145. variables;
  5146. sourceSpan;
  5147. startSourceSpan;
  5148. endSourceSpan;
  5149. i18n;
  5150. constructor(
  5151. // tagName is the name of the container element, if applicable.
  5152. // `null` is a special case for when there is a structural directive on an `ng-template` so
  5153. // the renderer can differentiate between the synthetic template and the one written in the
  5154. // file.
  5155. tagName, attributes, inputs, outputs, templateAttrs, children, references, variables, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  5156. this.tagName = tagName;
  5157. this.attributes = attributes;
  5158. this.inputs = inputs;
  5159. this.outputs = outputs;
  5160. this.templateAttrs = templateAttrs;
  5161. this.children = children;
  5162. this.references = references;
  5163. this.variables = variables;
  5164. this.sourceSpan = sourceSpan;
  5165. this.startSourceSpan = startSourceSpan;
  5166. this.endSourceSpan = endSourceSpan;
  5167. this.i18n = i18n;
  5168. }
  5169. visit(visitor) {
  5170. return visitor.visitTemplate(this);
  5171. }
  5172. }
  5173. class Content {
  5174. selector;
  5175. attributes;
  5176. children;
  5177. sourceSpan;
  5178. i18n;
  5179. name = 'ng-content';
  5180. constructor(selector, attributes, children, sourceSpan, i18n) {
  5181. this.selector = selector;
  5182. this.attributes = attributes;
  5183. this.children = children;
  5184. this.sourceSpan = sourceSpan;
  5185. this.i18n = i18n;
  5186. }
  5187. visit(visitor) {
  5188. return visitor.visitContent(this);
  5189. }
  5190. }
  5191. class Variable {
  5192. name;
  5193. value;
  5194. sourceSpan;
  5195. keySpan;
  5196. valueSpan;
  5197. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  5198. this.name = name;
  5199. this.value = value;
  5200. this.sourceSpan = sourceSpan;
  5201. this.keySpan = keySpan;
  5202. this.valueSpan = valueSpan;
  5203. }
  5204. visit(visitor) {
  5205. return visitor.visitVariable(this);
  5206. }
  5207. }
  5208. let Reference$1 = class Reference {
  5209. name;
  5210. value;
  5211. sourceSpan;
  5212. keySpan;
  5213. valueSpan;
  5214. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  5215. this.name = name;
  5216. this.value = value;
  5217. this.sourceSpan = sourceSpan;
  5218. this.keySpan = keySpan;
  5219. this.valueSpan = valueSpan;
  5220. }
  5221. visit(visitor) {
  5222. return visitor.visitReference(this);
  5223. }
  5224. };
  5225. let Icu$1 = class Icu {
  5226. vars;
  5227. placeholders;
  5228. sourceSpan;
  5229. i18n;
  5230. constructor(vars, placeholders, sourceSpan, i18n) {
  5231. this.vars = vars;
  5232. this.placeholders = placeholders;
  5233. this.sourceSpan = sourceSpan;
  5234. this.i18n = i18n;
  5235. }
  5236. visit(visitor) {
  5237. return visitor.visitIcu(this);
  5238. }
  5239. };
  5240. let RecursiveVisitor$1 = class RecursiveVisitor {
  5241. visitElement(element) {
  5242. visitAll$1(this, element.attributes);
  5243. visitAll$1(this, element.inputs);
  5244. visitAll$1(this, element.outputs);
  5245. visitAll$1(this, element.children);
  5246. visitAll$1(this, element.references);
  5247. }
  5248. visitTemplate(template) {
  5249. visitAll$1(this, template.attributes);
  5250. visitAll$1(this, template.inputs);
  5251. visitAll$1(this, template.outputs);
  5252. visitAll$1(this, template.children);
  5253. visitAll$1(this, template.references);
  5254. visitAll$1(this, template.variables);
  5255. }
  5256. visitDeferredBlock(deferred) {
  5257. deferred.visitAll(this);
  5258. }
  5259. visitDeferredBlockPlaceholder(block) {
  5260. visitAll$1(this, block.children);
  5261. }
  5262. visitDeferredBlockError(block) {
  5263. visitAll$1(this, block.children);
  5264. }
  5265. visitDeferredBlockLoading(block) {
  5266. visitAll$1(this, block.children);
  5267. }
  5268. visitSwitchBlock(block) {
  5269. visitAll$1(this, block.cases);
  5270. }
  5271. visitSwitchBlockCase(block) {
  5272. visitAll$1(this, block.children);
  5273. }
  5274. visitForLoopBlock(block) {
  5275. const blockItems = [block.item, ...block.contextVariables, ...block.children];
  5276. block.empty && blockItems.push(block.empty);
  5277. visitAll$1(this, blockItems);
  5278. }
  5279. visitForLoopBlockEmpty(block) {
  5280. visitAll$1(this, block.children);
  5281. }
  5282. visitIfBlock(block) {
  5283. visitAll$1(this, block.branches);
  5284. }
  5285. visitIfBlockBranch(block) {
  5286. const blockItems = block.children;
  5287. block.expressionAlias && blockItems.push(block.expressionAlias);
  5288. visitAll$1(this, blockItems);
  5289. }
  5290. visitContent(content) {
  5291. visitAll$1(this, content.children);
  5292. }
  5293. visitVariable(variable) { }
  5294. visitReference(reference) { }
  5295. visitTextAttribute(attribute) { }
  5296. visitBoundAttribute(attribute) { }
  5297. visitBoundEvent(attribute) { }
  5298. visitText(text) { }
  5299. visitBoundText(text) { }
  5300. visitIcu(icu) { }
  5301. visitDeferredTrigger(trigger) { }
  5302. visitUnknownBlock(block) { }
  5303. visitLetDeclaration(decl) { }
  5304. };
  5305. function visitAll$1(visitor, nodes) {
  5306. const result = [];
  5307. if (visitor.visit) {
  5308. for (const node of nodes) {
  5309. visitor.visit(node) || node.visit(visitor);
  5310. }
  5311. }
  5312. else {
  5313. for (const node of nodes) {
  5314. const newNode = node.visit(visitor);
  5315. if (newNode) {
  5316. result.push(newNode);
  5317. }
  5318. }
  5319. }
  5320. return result;
  5321. }
  5322. class Message {
  5323. nodes;
  5324. placeholders;
  5325. placeholderToMessage;
  5326. meaning;
  5327. description;
  5328. customId;
  5329. sources;
  5330. id;
  5331. /** The ids to use if there are no custom id and if `i18nLegacyMessageIdFormat` is not empty */
  5332. legacyIds = [];
  5333. messageString;
  5334. /**
  5335. * @param nodes message AST
  5336. * @param placeholders maps placeholder names to static content and their source spans
  5337. * @param placeholderToMessage maps placeholder names to messages (used for nested ICU messages)
  5338. * @param meaning
  5339. * @param description
  5340. * @param customId
  5341. */
  5342. constructor(nodes, placeholders, placeholderToMessage, meaning, description, customId) {
  5343. this.nodes = nodes;
  5344. this.placeholders = placeholders;
  5345. this.placeholderToMessage = placeholderToMessage;
  5346. this.meaning = meaning;
  5347. this.description = description;
  5348. this.customId = customId;
  5349. this.id = this.customId;
  5350. this.messageString = serializeMessage(this.nodes);
  5351. if (nodes.length) {
  5352. this.sources = [
  5353. {
  5354. filePath: nodes[0].sourceSpan.start.file.url,
  5355. startLine: nodes[0].sourceSpan.start.line + 1,
  5356. startCol: nodes[0].sourceSpan.start.col + 1,
  5357. endLine: nodes[nodes.length - 1].sourceSpan.end.line + 1,
  5358. endCol: nodes[0].sourceSpan.start.col + 1,
  5359. },
  5360. ];
  5361. }
  5362. else {
  5363. this.sources = [];
  5364. }
  5365. }
  5366. }
  5367. let Text$2 = class Text {
  5368. value;
  5369. sourceSpan;
  5370. constructor(value, sourceSpan) {
  5371. this.value = value;
  5372. this.sourceSpan = sourceSpan;
  5373. }
  5374. visit(visitor, context) {
  5375. return visitor.visitText(this, context);
  5376. }
  5377. };
  5378. // TODO(vicb): do we really need this node (vs an array) ?
  5379. class Container {
  5380. children;
  5381. sourceSpan;
  5382. constructor(children, sourceSpan) {
  5383. this.children = children;
  5384. this.sourceSpan = sourceSpan;
  5385. }
  5386. visit(visitor, context) {
  5387. return visitor.visitContainer(this, context);
  5388. }
  5389. }
  5390. class Icu {
  5391. expression;
  5392. type;
  5393. cases;
  5394. sourceSpan;
  5395. expressionPlaceholder;
  5396. constructor(expression, type, cases, sourceSpan, expressionPlaceholder) {
  5397. this.expression = expression;
  5398. this.type = type;
  5399. this.cases = cases;
  5400. this.sourceSpan = sourceSpan;
  5401. this.expressionPlaceholder = expressionPlaceholder;
  5402. }
  5403. visit(visitor, context) {
  5404. return visitor.visitIcu(this, context);
  5405. }
  5406. }
  5407. class TagPlaceholder {
  5408. tag;
  5409. attrs;
  5410. startName;
  5411. closeName;
  5412. children;
  5413. isVoid;
  5414. sourceSpan;
  5415. startSourceSpan;
  5416. endSourceSpan;
  5417. constructor(tag, attrs, startName, closeName, children, isVoid,
  5418. // TODO sourceSpan should cover all (we need a startSourceSpan and endSourceSpan)
  5419. sourceSpan, startSourceSpan, endSourceSpan) {
  5420. this.tag = tag;
  5421. this.attrs = attrs;
  5422. this.startName = startName;
  5423. this.closeName = closeName;
  5424. this.children = children;
  5425. this.isVoid = isVoid;
  5426. this.sourceSpan = sourceSpan;
  5427. this.startSourceSpan = startSourceSpan;
  5428. this.endSourceSpan = endSourceSpan;
  5429. }
  5430. visit(visitor, context) {
  5431. return visitor.visitTagPlaceholder(this, context);
  5432. }
  5433. }
  5434. class Placeholder {
  5435. value;
  5436. name;
  5437. sourceSpan;
  5438. constructor(value, name, sourceSpan) {
  5439. this.value = value;
  5440. this.name = name;
  5441. this.sourceSpan = sourceSpan;
  5442. }
  5443. visit(visitor, context) {
  5444. return visitor.visitPlaceholder(this, context);
  5445. }
  5446. }
  5447. class IcuPlaceholder {
  5448. value;
  5449. name;
  5450. sourceSpan;
  5451. /** Used to capture a message computed from a previous processing pass (see `setI18nRefs()`). */
  5452. previousMessage;
  5453. constructor(value, name, sourceSpan) {
  5454. this.value = value;
  5455. this.name = name;
  5456. this.sourceSpan = sourceSpan;
  5457. }
  5458. visit(visitor, context) {
  5459. return visitor.visitIcuPlaceholder(this, context);
  5460. }
  5461. }
  5462. class BlockPlaceholder {
  5463. name;
  5464. parameters;
  5465. startName;
  5466. closeName;
  5467. children;
  5468. sourceSpan;
  5469. startSourceSpan;
  5470. endSourceSpan;
  5471. constructor(name, parameters, startName, closeName, children, sourceSpan, startSourceSpan, endSourceSpan) {
  5472. this.name = name;
  5473. this.parameters = parameters;
  5474. this.startName = startName;
  5475. this.closeName = closeName;
  5476. this.children = children;
  5477. this.sourceSpan = sourceSpan;
  5478. this.startSourceSpan = startSourceSpan;
  5479. this.endSourceSpan = endSourceSpan;
  5480. }
  5481. visit(visitor, context) {
  5482. return visitor.visitBlockPlaceholder(this, context);
  5483. }
  5484. }
  5485. // Clone the AST
  5486. class CloneVisitor {
  5487. visitText(text, context) {
  5488. return new Text$2(text.value, text.sourceSpan);
  5489. }
  5490. visitContainer(container, context) {
  5491. const children = container.children.map((n) => n.visit(this, context));
  5492. return new Container(children, container.sourceSpan);
  5493. }
  5494. visitIcu(icu, context) {
  5495. const cases = {};
  5496. Object.keys(icu.cases).forEach((key) => (cases[key] = icu.cases[key].visit(this, context)));
  5497. const msg = new Icu(icu.expression, icu.type, cases, icu.sourceSpan, icu.expressionPlaceholder);
  5498. return msg;
  5499. }
  5500. visitTagPlaceholder(ph, context) {
  5501. const children = ph.children.map((n) => n.visit(this, context));
  5502. return new TagPlaceholder(ph.tag, ph.attrs, ph.startName, ph.closeName, children, ph.isVoid, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  5503. }
  5504. visitPlaceholder(ph, context) {
  5505. return new Placeholder(ph.value, ph.name, ph.sourceSpan);
  5506. }
  5507. visitIcuPlaceholder(ph, context) {
  5508. return new IcuPlaceholder(ph.value, ph.name, ph.sourceSpan);
  5509. }
  5510. visitBlockPlaceholder(ph, context) {
  5511. const children = ph.children.map((n) => n.visit(this, context));
  5512. return new BlockPlaceholder(ph.name, ph.parameters, ph.startName, ph.closeName, children, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  5513. }
  5514. }
  5515. // Visit all the nodes recursively
  5516. class RecurseVisitor {
  5517. visitText(text, context) { }
  5518. visitContainer(container, context) {
  5519. container.children.forEach((child) => child.visit(this));
  5520. }
  5521. visitIcu(icu, context) {
  5522. Object.keys(icu.cases).forEach((k) => {
  5523. icu.cases[k].visit(this);
  5524. });
  5525. }
  5526. visitTagPlaceholder(ph, context) {
  5527. ph.children.forEach((child) => child.visit(this));
  5528. }
  5529. visitPlaceholder(ph, context) { }
  5530. visitIcuPlaceholder(ph, context) { }
  5531. visitBlockPlaceholder(ph, context) {
  5532. ph.children.forEach((child) => child.visit(this));
  5533. }
  5534. }
  5535. /**
  5536. * Serialize the message to the Localize backtick string format that would appear in compiled code.
  5537. */
  5538. function serializeMessage(messageNodes) {
  5539. const visitor = new LocalizeMessageStringVisitor();
  5540. const str = messageNodes.map((n) => n.visit(visitor)).join('');
  5541. return str;
  5542. }
  5543. class LocalizeMessageStringVisitor {
  5544. visitText(text) {
  5545. return text.value;
  5546. }
  5547. visitContainer(container) {
  5548. return container.children.map((child) => child.visit(this)).join('');
  5549. }
  5550. visitIcu(icu) {
  5551. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  5552. return `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
  5553. }
  5554. visitTagPlaceholder(ph) {
  5555. const children = ph.children.map((child) => child.visit(this)).join('');
  5556. return `{$${ph.startName}}${children}{$${ph.closeName}}`;
  5557. }
  5558. visitPlaceholder(ph) {
  5559. return `{$${ph.name}}`;
  5560. }
  5561. visitIcuPlaceholder(ph) {
  5562. return `{$${ph.name}}`;
  5563. }
  5564. visitBlockPlaceholder(ph) {
  5565. const children = ph.children.map((child) => child.visit(this)).join('');
  5566. return `{$${ph.startName}}${children}{$${ph.closeName}}`;
  5567. }
  5568. }
  5569. class Serializer {
  5570. // Creates a name mapper, see `PlaceholderMapper`
  5571. // Returning `null` means that no name mapping is used.
  5572. createNameMapper(message) {
  5573. return null;
  5574. }
  5575. }
  5576. /**
  5577. * A simple mapper that take a function to transform an internal name to a public name
  5578. */
  5579. class SimplePlaceholderMapper extends RecurseVisitor {
  5580. mapName;
  5581. internalToPublic = {};
  5582. publicToNextId = {};
  5583. publicToInternal = {};
  5584. // create a mapping from the message
  5585. constructor(message, mapName) {
  5586. super();
  5587. this.mapName = mapName;
  5588. message.nodes.forEach((node) => node.visit(this));
  5589. }
  5590. toPublicName(internalName) {
  5591. return this.internalToPublic.hasOwnProperty(internalName)
  5592. ? this.internalToPublic[internalName]
  5593. : null;
  5594. }
  5595. toInternalName(publicName) {
  5596. return this.publicToInternal.hasOwnProperty(publicName)
  5597. ? this.publicToInternal[publicName]
  5598. : null;
  5599. }
  5600. visitText(text, context) {
  5601. return null;
  5602. }
  5603. visitTagPlaceholder(ph, context) {
  5604. this.visitPlaceholderName(ph.startName);
  5605. super.visitTagPlaceholder(ph, context);
  5606. this.visitPlaceholderName(ph.closeName);
  5607. }
  5608. visitPlaceholder(ph, context) {
  5609. this.visitPlaceholderName(ph.name);
  5610. }
  5611. visitBlockPlaceholder(ph, context) {
  5612. this.visitPlaceholderName(ph.startName);
  5613. super.visitBlockPlaceholder(ph, context);
  5614. this.visitPlaceholderName(ph.closeName);
  5615. }
  5616. visitIcuPlaceholder(ph, context) {
  5617. this.visitPlaceholderName(ph.name);
  5618. }
  5619. // XMB placeholders could only contains A-Z, 0-9 and _
  5620. visitPlaceholderName(internalName) {
  5621. if (!internalName || this.internalToPublic.hasOwnProperty(internalName)) {
  5622. return;
  5623. }
  5624. let publicName = this.mapName(internalName);
  5625. if (this.publicToInternal.hasOwnProperty(publicName)) {
  5626. // Create a new XMB when it has already been used
  5627. const nextId = this.publicToNextId[publicName];
  5628. this.publicToNextId[publicName] = nextId + 1;
  5629. publicName = `${publicName}_${nextId}`;
  5630. }
  5631. else {
  5632. this.publicToNextId[publicName] = 1;
  5633. }
  5634. this.internalToPublic[internalName] = publicName;
  5635. this.publicToInternal[publicName] = internalName;
  5636. }
  5637. }
  5638. let _Visitor$2 = class _Visitor {
  5639. visitTag(tag) {
  5640. const strAttrs = this._serializeAttributes(tag.attrs);
  5641. if (tag.children.length == 0) {
  5642. return `<${tag.name}${strAttrs}/>`;
  5643. }
  5644. const strChildren = tag.children.map((node) => node.visit(this));
  5645. return `<${tag.name}${strAttrs}>${strChildren.join('')}</${tag.name}>`;
  5646. }
  5647. visitText(text) {
  5648. return text.value;
  5649. }
  5650. visitDeclaration(decl) {
  5651. return `<?xml${this._serializeAttributes(decl.attrs)} ?>`;
  5652. }
  5653. _serializeAttributes(attrs) {
  5654. const strAttrs = Object.keys(attrs)
  5655. .map((name) => `${name}="${attrs[name]}"`)
  5656. .join(' ');
  5657. return strAttrs.length > 0 ? ' ' + strAttrs : '';
  5658. }
  5659. visitDoctype(doctype) {
  5660. return `<!DOCTYPE ${doctype.rootTag} [\n${doctype.dtd}\n]>`;
  5661. }
  5662. };
  5663. const _visitor = new _Visitor$2();
  5664. function serialize$1(nodes) {
  5665. return nodes.map((node) => node.visit(_visitor)).join('');
  5666. }
  5667. class Declaration {
  5668. attrs = {};
  5669. constructor(unescapedAttrs) {
  5670. Object.keys(unescapedAttrs).forEach((k) => {
  5671. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  5672. });
  5673. }
  5674. visit(visitor) {
  5675. return visitor.visitDeclaration(this);
  5676. }
  5677. }
  5678. class Doctype {
  5679. rootTag;
  5680. dtd;
  5681. constructor(rootTag, dtd) {
  5682. this.rootTag = rootTag;
  5683. this.dtd = dtd;
  5684. }
  5685. visit(visitor) {
  5686. return visitor.visitDoctype(this);
  5687. }
  5688. }
  5689. class Tag {
  5690. name;
  5691. children;
  5692. attrs = {};
  5693. constructor(name, unescapedAttrs = {}, children = []) {
  5694. this.name = name;
  5695. this.children = children;
  5696. Object.keys(unescapedAttrs).forEach((k) => {
  5697. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  5698. });
  5699. }
  5700. visit(visitor) {
  5701. return visitor.visitTag(this);
  5702. }
  5703. }
  5704. let Text$1 = class Text {
  5705. value;
  5706. constructor(unescapedValue) {
  5707. this.value = escapeXml(unescapedValue);
  5708. }
  5709. visit(visitor) {
  5710. return visitor.visitText(this);
  5711. }
  5712. };
  5713. class CR extends Text$1 {
  5714. constructor(ws = 0) {
  5715. super(`\n${new Array(ws + 1).join(' ')}`);
  5716. }
  5717. }
  5718. const _ESCAPED_CHARS = [
  5719. [/&/g, '&amp;'],
  5720. [/"/g, '&quot;'],
  5721. [/'/g, '&apos;'],
  5722. [/</g, '&lt;'],
  5723. [/>/g, '&gt;'],
  5724. ];
  5725. // Escape `_ESCAPED_CHARS` characters in the given text with encoded entities
  5726. function escapeXml(text) {
  5727. return _ESCAPED_CHARS.reduce((text, entry) => text.replace(entry[0], entry[1]), text);
  5728. }
  5729. /**
  5730. * Defines the `handler` value on the serialized XMB, indicating that Angular
  5731. * generated the bundle. This is useful for analytics in Translation Console.
  5732. *
  5733. * NOTE: Keep in sync with
  5734. * packages/localize/tools/src/extract/translation_files/xmb_translation_serializer.ts.
  5735. */
  5736. const _XMB_HANDLER = 'angular';
  5737. const _MESSAGES_TAG = 'messagebundle';
  5738. const _MESSAGE_TAG = 'msg';
  5739. const _PLACEHOLDER_TAG = 'ph';
  5740. const _EXAMPLE_TAG = 'ex';
  5741. const _SOURCE_TAG = 'source';
  5742. const _DOCTYPE = `<!ELEMENT messagebundle (msg)*>
  5743. <!ATTLIST messagebundle class CDATA #IMPLIED>
  5744. <!ELEMENT msg (#PCDATA|ph|source)*>
  5745. <!ATTLIST msg id CDATA #IMPLIED>
  5746. <!ATTLIST msg seq CDATA #IMPLIED>
  5747. <!ATTLIST msg name CDATA #IMPLIED>
  5748. <!ATTLIST msg desc CDATA #IMPLIED>
  5749. <!ATTLIST msg meaning CDATA #IMPLIED>
  5750. <!ATTLIST msg obsolete (obsolete) #IMPLIED>
  5751. <!ATTLIST msg xml:space (default|preserve) "default">
  5752. <!ATTLIST msg is_hidden CDATA #IMPLIED>
  5753. <!ELEMENT source (#PCDATA)>
  5754. <!ELEMENT ph (#PCDATA|ex)*>
  5755. <!ATTLIST ph name CDATA #REQUIRED>
  5756. <!ELEMENT ex (#PCDATA)>`;
  5757. class Xmb extends Serializer {
  5758. write(messages, locale) {
  5759. const exampleVisitor = new ExampleVisitor();
  5760. const visitor = new _Visitor$1();
  5761. const rootNode = new Tag(_MESSAGES_TAG);
  5762. rootNode.attrs['handler'] = _XMB_HANDLER;
  5763. messages.forEach((message) => {
  5764. const attrs = { id: message.id };
  5765. if (message.description) {
  5766. attrs['desc'] = message.description;
  5767. }
  5768. if (message.meaning) {
  5769. attrs['meaning'] = message.meaning;
  5770. }
  5771. let sourceTags = [];
  5772. message.sources.forEach((source) => {
  5773. sourceTags.push(new Tag(_SOURCE_TAG, {}, [
  5774. new Text$1(`${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`),
  5775. ]));
  5776. });
  5777. rootNode.children.push(new CR(2), new Tag(_MESSAGE_TAG, attrs, [...sourceTags, ...visitor.serialize(message.nodes)]));
  5778. });
  5779. rootNode.children.push(new CR());
  5780. return serialize$1([
  5781. new Declaration({ version: '1.0', encoding: 'UTF-8' }),
  5782. new CR(),
  5783. new Doctype(_MESSAGES_TAG, _DOCTYPE),
  5784. new CR(),
  5785. exampleVisitor.addDefaultExamples(rootNode),
  5786. new CR(),
  5787. ]);
  5788. }
  5789. load(content, url) {
  5790. throw new Error('Unsupported');
  5791. }
  5792. digest(message) {
  5793. return digest(message);
  5794. }
  5795. createNameMapper(message) {
  5796. return new SimplePlaceholderMapper(message, toPublicName);
  5797. }
  5798. }
  5799. let _Visitor$1 = class _Visitor {
  5800. visitText(text, context) {
  5801. return [new Text$1(text.value)];
  5802. }
  5803. visitContainer(container, context) {
  5804. const nodes = [];
  5805. container.children.forEach((node) => nodes.push(...node.visit(this)));
  5806. return nodes;
  5807. }
  5808. visitIcu(icu, context) {
  5809. const nodes = [new Text$1(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  5810. Object.keys(icu.cases).forEach((c) => {
  5811. nodes.push(new Text$1(`${c} {`), ...icu.cases[c].visit(this), new Text$1(`} `));
  5812. });
  5813. nodes.push(new Text$1(`}`));
  5814. return nodes;
  5815. }
  5816. visitTagPlaceholder(ph, context) {
  5817. const startTagAsText = new Text$1(`<${ph.tag}>`);
  5818. const startEx = new Tag(_EXAMPLE_TAG, {}, [startTagAsText]);
  5819. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5820. const startTagPh = new Tag(_PLACEHOLDER_TAG, { name: ph.startName }, [
  5821. startEx,
  5822. startTagAsText,
  5823. ]);
  5824. if (ph.isVoid) {
  5825. // void tags have no children nor closing tags
  5826. return [startTagPh];
  5827. }
  5828. const closeTagAsText = new Text$1(`</${ph.tag}>`);
  5829. const closeEx = new Tag(_EXAMPLE_TAG, {}, [closeTagAsText]);
  5830. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5831. const closeTagPh = new Tag(_PLACEHOLDER_TAG, { name: ph.closeName }, [
  5832. closeEx,
  5833. closeTagAsText,
  5834. ]);
  5835. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  5836. }
  5837. visitPlaceholder(ph, context) {
  5838. const interpolationAsText = new Text$1(`{{${ph.value}}}`);
  5839. // Example tag needs to be not-empty for TC.
  5840. const exTag = new Tag(_EXAMPLE_TAG, {}, [interpolationAsText]);
  5841. return [
  5842. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5843. new Tag(_PLACEHOLDER_TAG, { name: ph.name }, [exTag, interpolationAsText]),
  5844. ];
  5845. }
  5846. visitBlockPlaceholder(ph, context) {
  5847. const startAsText = new Text$1(`@${ph.name}`);
  5848. const startEx = new Tag(_EXAMPLE_TAG, {}, [startAsText]);
  5849. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5850. const startTagPh = new Tag(_PLACEHOLDER_TAG, { name: ph.startName }, [startEx, startAsText]);
  5851. const closeAsText = new Text$1(`}`);
  5852. const closeEx = new Tag(_EXAMPLE_TAG, {}, [closeAsText]);
  5853. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5854. const closeTagPh = new Tag(_PLACEHOLDER_TAG, { name: ph.closeName }, [closeEx, closeAsText]);
  5855. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  5856. }
  5857. visitIcuPlaceholder(ph, context) {
  5858. const icuExpression = ph.value.expression;
  5859. const icuType = ph.value.type;
  5860. const icuCases = Object.keys(ph.value.cases)
  5861. .map((value) => value + ' {...}')
  5862. .join(' ');
  5863. const icuAsText = new Text$1(`{${icuExpression}, ${icuType}, ${icuCases}}`);
  5864. const exTag = new Tag(_EXAMPLE_TAG, {}, [icuAsText]);
  5865. return [
  5866. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  5867. new Tag(_PLACEHOLDER_TAG, { name: ph.name }, [exTag, icuAsText]),
  5868. ];
  5869. }
  5870. serialize(nodes) {
  5871. return [].concat(...nodes.map((node) => node.visit(this)));
  5872. }
  5873. };
  5874. function digest(message) {
  5875. return decimalDigest(message);
  5876. }
  5877. // TC requires at least one non-empty example on placeholders
  5878. class ExampleVisitor {
  5879. addDefaultExamples(node) {
  5880. node.visit(this);
  5881. return node;
  5882. }
  5883. visitTag(tag) {
  5884. if (tag.name === _PLACEHOLDER_TAG) {
  5885. if (!tag.children || tag.children.length == 0) {
  5886. const exText = new Text$1(tag.attrs['name'] || '...');
  5887. tag.children = [new Tag(_EXAMPLE_TAG, {}, [exText])];
  5888. }
  5889. }
  5890. else if (tag.children) {
  5891. tag.children.forEach((node) => node.visit(this));
  5892. }
  5893. }
  5894. visitText(text) { }
  5895. visitDeclaration(decl) { }
  5896. visitDoctype(doctype) { }
  5897. }
  5898. // XMB/XTB placeholders can only contain A-Z, 0-9 and _
  5899. function toPublicName(internalName) {
  5900. return internalName.toUpperCase().replace(/[^A-Z0-9_]/g, '_');
  5901. }
  5902. /** Name of the i18n attributes **/
  5903. const I18N_ATTR = 'i18n';
  5904. const I18N_ATTR_PREFIX = 'i18n-';
  5905. /** Prefix of var expressions used in ICUs */
  5906. const I18N_ICU_VAR_PREFIX = 'VAR_';
  5907. function isI18nAttribute(name) {
  5908. return name === I18N_ATTR || name.startsWith(I18N_ATTR_PREFIX);
  5909. }
  5910. function hasI18nAttrs(element) {
  5911. return element.attrs.some((attr) => isI18nAttribute(attr.name));
  5912. }
  5913. function icuFromI18nMessage(message) {
  5914. return message.nodes[0];
  5915. }
  5916. /**
  5917. * Format the placeholder names in a map of placeholders to expressions.
  5918. *
  5919. * The placeholder names are converted from "internal" format (e.g. `START_TAG_DIV_1`) to "external"
  5920. * format (e.g. `startTagDiv_1`).
  5921. *
  5922. * @param params A map of placeholder names to expressions.
  5923. * @param useCamelCase whether to camelCase the placeholder name when formatting.
  5924. * @returns A new map of formatted placeholder names to expressions.
  5925. */
  5926. function formatI18nPlaceholderNamesInMap(params = {}, useCamelCase) {
  5927. const _params = {};
  5928. if (params && Object.keys(params).length) {
  5929. Object.keys(params).forEach((key) => (_params[formatI18nPlaceholderName(key, useCamelCase)] = params[key]));
  5930. }
  5931. return _params;
  5932. }
  5933. /**
  5934. * Converts internal placeholder names to public-facing format
  5935. * (for example to use in goog.getMsg call).
  5936. * Example: `START_TAG_DIV_1` is converted to `startTagDiv_1`.
  5937. *
  5938. * @param name The placeholder name that should be formatted
  5939. * @returns Formatted placeholder name
  5940. */
  5941. function formatI18nPlaceholderName(name, useCamelCase = true) {
  5942. const publicName = toPublicName(name);
  5943. if (!useCamelCase) {
  5944. return publicName;
  5945. }
  5946. const chunks = publicName.split('_');
  5947. if (chunks.length === 1) {
  5948. // if no "_" found - just lowercase the value
  5949. return name.toLowerCase();
  5950. }
  5951. let postfix;
  5952. // eject last element if it's a number
  5953. if (/^\d+$/.test(chunks[chunks.length - 1])) {
  5954. postfix = chunks.pop();
  5955. }
  5956. let raw = chunks.shift().toLowerCase();
  5957. if (chunks.length) {
  5958. raw += chunks.map((c) => c.charAt(0).toUpperCase() + c.slice(1).toLowerCase()).join('');
  5959. }
  5960. return postfix ? `${raw}_${postfix}` : raw;
  5961. }
  5962. /**
  5963. * Checks whether an object key contains potentially unsafe chars, thus the key should be wrapped in
  5964. * quotes. Note: we do not wrap all keys into quotes, as it may have impact on minification and may
  5965. * not work in some cases when object keys are mangled by a minifier.
  5966. *
  5967. * TODO(FW-1136): this is a temporary solution, we need to come up with a better way of working with
  5968. * inputs that contain potentially unsafe chars.
  5969. */
  5970. const UNSAFE_OBJECT_KEY_NAME_REGEXP = /[-.]/;
  5971. /** Name of the temporary to use during data binding */
  5972. const TEMPORARY_NAME = '_t';
  5973. /** Name of the context parameter passed into a template function */
  5974. const CONTEXT_NAME = 'ctx';
  5975. /** Name of the RenderFlag passed into a template function */
  5976. const RENDER_FLAGS = 'rf';
  5977. /**
  5978. * Creates an allocator for a temporary variable.
  5979. *
  5980. * A variable declaration is added to the statements the first time the allocator is invoked.
  5981. */
  5982. function temporaryAllocator(pushStatement, name) {
  5983. let temp = null;
  5984. return () => {
  5985. if (!temp) {
  5986. pushStatement(new DeclareVarStmt(TEMPORARY_NAME, undefined, DYNAMIC_TYPE));
  5987. temp = variable(name);
  5988. }
  5989. return temp;
  5990. };
  5991. }
  5992. function asLiteral(value) {
  5993. if (Array.isArray(value)) {
  5994. return literalArr(value.map(asLiteral));
  5995. }
  5996. return literal$1(value, INFERRED_TYPE);
  5997. }
  5998. /**
  5999. * Serializes inputs and outputs for `defineDirective` and `defineComponent`.
  6000. *
  6001. * This will attempt to generate optimized data structures to minimize memory or
  6002. * file size of fully compiled applications.
  6003. */
  6004. function conditionallyCreateDirectiveBindingLiteral(map, forInputs) {
  6005. const keys = Object.getOwnPropertyNames(map);
  6006. if (keys.length === 0) {
  6007. return null;
  6008. }
  6009. return literalMap(keys.map((key) => {
  6010. const value = map[key];
  6011. let declaredName;
  6012. let publicName;
  6013. let minifiedName;
  6014. let expressionValue;
  6015. if (typeof value === 'string') {
  6016. // canonical syntax: `dirProp: publicProp`
  6017. declaredName = key;
  6018. minifiedName = key;
  6019. publicName = value;
  6020. expressionValue = asLiteral(publicName);
  6021. }
  6022. else {
  6023. minifiedName = key;
  6024. declaredName = value.classPropertyName;
  6025. publicName = value.bindingPropertyName;
  6026. const differentDeclaringName = publicName !== declaredName;
  6027. const hasDecoratorInputTransform = value.transformFunction !== null;
  6028. let flags = InputFlags.None;
  6029. // Build up input flags
  6030. if (value.isSignal) {
  6031. flags |= InputFlags.SignalBased;
  6032. }
  6033. if (hasDecoratorInputTransform) {
  6034. flags |= InputFlags.HasDecoratorInputTransform;
  6035. }
  6036. // Inputs, compared to outputs, will track their declared name (for `ngOnChanges`), support
  6037. // decorator input transform functions, or store flag information if there is any.
  6038. if (forInputs &&
  6039. (differentDeclaringName || hasDecoratorInputTransform || flags !== InputFlags.None)) {
  6040. const result = [literal$1(flags), asLiteral(publicName)];
  6041. if (differentDeclaringName || hasDecoratorInputTransform) {
  6042. result.push(asLiteral(declaredName));
  6043. if (hasDecoratorInputTransform) {
  6044. result.push(value.transformFunction);
  6045. }
  6046. }
  6047. expressionValue = literalArr(result);
  6048. }
  6049. else {
  6050. expressionValue = asLiteral(publicName);
  6051. }
  6052. }
  6053. return {
  6054. key: minifiedName,
  6055. // put quotes around keys that contain potentially unsafe characters
  6056. quoted: UNSAFE_OBJECT_KEY_NAME_REGEXP.test(minifiedName),
  6057. value: expressionValue,
  6058. };
  6059. }));
  6060. }
  6061. /**
  6062. * A representation for an object literal used during codegen of definition objects. The generic
  6063. * type `T` allows to reference a documented type of the generated structure, such that the
  6064. * property names that are set can be resolved to their documented declaration.
  6065. */
  6066. class DefinitionMap {
  6067. values = [];
  6068. set(key, value) {
  6069. if (value) {
  6070. const existing = this.values.find((value) => value.key === key);
  6071. if (existing) {
  6072. existing.value = value;
  6073. }
  6074. else {
  6075. this.values.push({ key: key, value, quoted: false });
  6076. }
  6077. }
  6078. }
  6079. toLiteralMap() {
  6080. return literalMap(this.values);
  6081. }
  6082. }
  6083. /**
  6084. * Creates a `CssSelector` from an AST node.
  6085. */
  6086. function createCssSelectorFromNode(node) {
  6087. const elementName = node instanceof Element$1 ? node.name : 'ng-template';
  6088. const attributes = getAttrsForDirectiveMatching(node);
  6089. const cssSelector = new CssSelector();
  6090. const elementNameNoNs = splitNsName(elementName)[1];
  6091. cssSelector.setElement(elementNameNoNs);
  6092. Object.getOwnPropertyNames(attributes).forEach((name) => {
  6093. const nameNoNs = splitNsName(name)[1];
  6094. const value = attributes[name];
  6095. cssSelector.addAttribute(nameNoNs, value);
  6096. if (name.toLowerCase() === 'class') {
  6097. const classes = value.trim().split(/\s+/);
  6098. classes.forEach((className) => cssSelector.addClassName(className));
  6099. }
  6100. });
  6101. return cssSelector;
  6102. }
  6103. /**
  6104. * Extract a map of properties to values for a given element or template node, which can be used
  6105. * by the directive matching machinery.
  6106. *
  6107. * @param elOrTpl the element or template in question
  6108. * @return an object set up for directive matching. For attributes on the element/template, this
  6109. * object maps a property name to its (static) value. For any bindings, this map simply maps the
  6110. * property name to an empty string.
  6111. */
  6112. function getAttrsForDirectiveMatching(elOrTpl) {
  6113. const attributesMap = {};
  6114. if (elOrTpl instanceof Template && elOrTpl.tagName !== 'ng-template') {
  6115. elOrTpl.templateAttrs.forEach((a) => (attributesMap[a.name] = ''));
  6116. }
  6117. else {
  6118. elOrTpl.attributes.forEach((a) => {
  6119. if (!isI18nAttribute(a.name)) {
  6120. attributesMap[a.name] = a.value;
  6121. }
  6122. });
  6123. elOrTpl.inputs.forEach((i) => {
  6124. if (i.type === exports.BindingType.Property || i.type === exports.BindingType.TwoWay) {
  6125. attributesMap[i.name] = '';
  6126. }
  6127. });
  6128. elOrTpl.outputs.forEach((o) => {
  6129. attributesMap[o.name] = '';
  6130. });
  6131. }
  6132. return attributesMap;
  6133. }
  6134. function compileInjectable(meta, resolveForwardRefs) {
  6135. let result = null;
  6136. const factoryMeta = {
  6137. name: meta.name,
  6138. type: meta.type,
  6139. typeArgumentCount: meta.typeArgumentCount,
  6140. deps: [],
  6141. target: exports.FactoryTarget.Injectable,
  6142. };
  6143. if (meta.useClass !== undefined) {
  6144. // meta.useClass has two modes of operation. Either deps are specified, in which case `new` is
  6145. // used to instantiate the class with dependencies injected, or deps are not specified and
  6146. // the factory of the class is used to instantiate it.
  6147. //
  6148. // A special case exists for useClass: Type where Type is the injectable type itself and no
  6149. // deps are specified, in which case 'useClass' is effectively ignored.
  6150. const useClassOnSelf = meta.useClass.expression.isEquivalent(meta.type.value);
  6151. let deps = undefined;
  6152. if (meta.deps !== undefined) {
  6153. deps = meta.deps;
  6154. }
  6155. if (deps !== undefined) {
  6156. // factory: () => new meta.useClass(...deps)
  6157. result = compileFactoryFunction({
  6158. ...factoryMeta,
  6159. delegate: meta.useClass.expression,
  6160. delegateDeps: deps,
  6161. delegateType: R3FactoryDelegateType.Class,
  6162. });
  6163. }
  6164. else if (useClassOnSelf) {
  6165. result = compileFactoryFunction(factoryMeta);
  6166. }
  6167. else {
  6168. result = {
  6169. statements: [],
  6170. expression: delegateToFactory(meta.type.value, meta.useClass.expression, resolveForwardRefs),
  6171. };
  6172. }
  6173. }
  6174. else if (meta.useFactory !== undefined) {
  6175. if (meta.deps !== undefined) {
  6176. result = compileFactoryFunction({
  6177. ...factoryMeta,
  6178. delegate: meta.useFactory,
  6179. delegateDeps: meta.deps || [],
  6180. delegateType: R3FactoryDelegateType.Function,
  6181. });
  6182. }
  6183. else {
  6184. result = { statements: [], expression: arrowFn([], meta.useFactory.callFn([])) };
  6185. }
  6186. }
  6187. else if (meta.useValue !== undefined) {
  6188. // Note: it's safe to use `meta.useValue` instead of the `USE_VALUE in meta` check used for
  6189. // client code because meta.useValue is an Expression which will be defined even if the actual
  6190. // value is undefined.
  6191. result = compileFactoryFunction({
  6192. ...factoryMeta,
  6193. expression: meta.useValue.expression,
  6194. });
  6195. }
  6196. else if (meta.useExisting !== undefined) {
  6197. // useExisting is an `inject` call on the existing token.
  6198. result = compileFactoryFunction({
  6199. ...factoryMeta,
  6200. expression: importExpr(Identifiers.inject).callFn([meta.useExisting.expression]),
  6201. });
  6202. }
  6203. else {
  6204. result = {
  6205. statements: [],
  6206. expression: delegateToFactory(meta.type.value, meta.type.value, resolveForwardRefs),
  6207. };
  6208. }
  6209. const token = meta.type.value;
  6210. const injectableProps = new DefinitionMap();
  6211. injectableProps.set('token', token);
  6212. injectableProps.set('factory', result.expression);
  6213. // Only generate providedIn property if it has a non-null value
  6214. if (meta.providedIn.expression.value !== null) {
  6215. injectableProps.set('providedIn', convertFromMaybeForwardRefExpression(meta.providedIn));
  6216. }
  6217. const expression = importExpr(Identifiers.ɵɵdefineInjectable)
  6218. .callFn([injectableProps.toLiteralMap()], undefined, true);
  6219. return {
  6220. expression,
  6221. type: createInjectableType(meta),
  6222. statements: result.statements,
  6223. };
  6224. }
  6225. function createInjectableType(meta) {
  6226. return new ExpressionType(importExpr(Identifiers.InjectableDeclaration, [
  6227. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  6228. ]));
  6229. }
  6230. function delegateToFactory(type, useType, unwrapForwardRefs) {
  6231. if (type.node === useType.node) {
  6232. // The types are the same, so we can simply delegate directly to the type's factory.
  6233. // ```
  6234. // factory: type.ɵfac
  6235. // ```
  6236. return useType.prop('ɵfac');
  6237. }
  6238. if (!unwrapForwardRefs) {
  6239. // The type is not wrapped in a `forwardRef()`, so we create a simple factory function that
  6240. // accepts a sub-type as an argument.
  6241. // ```
  6242. // factory: function(t) { return useType.ɵfac(t); }
  6243. // ```
  6244. return createFactoryFunction(useType);
  6245. }
  6246. // The useType is actually wrapped in a `forwardRef()` so we need to resolve that before
  6247. // calling its factory.
  6248. // ```
  6249. // factory: function(t) { return core.resolveForwardRef(type).ɵfac(t); }
  6250. // ```
  6251. const unwrappedType = importExpr(Identifiers.resolveForwardRef).callFn([useType]);
  6252. return createFactoryFunction(unwrappedType);
  6253. }
  6254. function createFactoryFunction(type) {
  6255. const t = new FnParam('__ngFactoryType__', DYNAMIC_TYPE);
  6256. return arrowFn([t], type.prop('ɵfac').callFn([variable(t.name)]));
  6257. }
  6258. const UNUSABLE_INTERPOLATION_REGEXPS = [
  6259. /@/, // control flow reserved symbol
  6260. /^\s*$/, // empty
  6261. /[<>]/, // html tag
  6262. /^[{}]$/, // i18n expansion
  6263. /&(#|[a-z])/i, // character reference,
  6264. /^\/\//, // comment
  6265. ];
  6266. function assertInterpolationSymbols(identifier, value) {
  6267. if (value != null && !(Array.isArray(value) && value.length == 2)) {
  6268. throw new Error(`Expected '${identifier}' to be an array, [start, end].`);
  6269. }
  6270. else if (value != null) {
  6271. const start = value[0];
  6272. const end = value[1];
  6273. // Check for unusable interpolation symbols
  6274. UNUSABLE_INTERPOLATION_REGEXPS.forEach((regexp) => {
  6275. if (regexp.test(start) || regexp.test(end)) {
  6276. throw new Error(`['${start}', '${end}'] contains unusable interpolation symbol.`);
  6277. }
  6278. });
  6279. }
  6280. }
  6281. class InterpolationConfig {
  6282. start;
  6283. end;
  6284. static fromArray(markers) {
  6285. if (!markers) {
  6286. return DEFAULT_INTERPOLATION_CONFIG;
  6287. }
  6288. assertInterpolationSymbols('interpolation', markers);
  6289. return new InterpolationConfig(markers[0], markers[1]);
  6290. }
  6291. constructor(start, end) {
  6292. this.start = start;
  6293. this.end = end;
  6294. }
  6295. }
  6296. const DEFAULT_INTERPOLATION_CONFIG = new InterpolationConfig('{{', '}}');
  6297. const DEFAULT_CONTAINER_BLOCKS = new Set(['switch']);
  6298. const $EOF = 0;
  6299. const $BSPACE = 8;
  6300. const $TAB = 9;
  6301. const $LF = 10;
  6302. const $VTAB = 11;
  6303. const $FF = 12;
  6304. const $CR = 13;
  6305. const $SPACE = 32;
  6306. const $BANG = 33;
  6307. const $DQ = 34;
  6308. const $HASH = 35;
  6309. const $$ = 36;
  6310. const $PERCENT = 37;
  6311. const $AMPERSAND = 38;
  6312. const $SQ = 39;
  6313. const $LPAREN = 40;
  6314. const $RPAREN = 41;
  6315. const $STAR = 42;
  6316. const $PLUS = 43;
  6317. const $COMMA = 44;
  6318. const $MINUS = 45;
  6319. const $PERIOD = 46;
  6320. const $SLASH = 47;
  6321. const $COLON = 58;
  6322. const $SEMICOLON = 59;
  6323. const $LT = 60;
  6324. const $EQ = 61;
  6325. const $GT = 62;
  6326. const $QUESTION = 63;
  6327. const $0 = 48;
  6328. const $7 = 55;
  6329. const $9 = 57;
  6330. const $A = 65;
  6331. const $E = 69;
  6332. const $F = 70;
  6333. const $X = 88;
  6334. const $Z = 90;
  6335. const $LBRACKET = 91;
  6336. const $BACKSLASH = 92;
  6337. const $RBRACKET = 93;
  6338. const $CARET = 94;
  6339. const $_ = 95;
  6340. const $a = 97;
  6341. const $b = 98;
  6342. const $e = 101;
  6343. const $f = 102;
  6344. const $n = 110;
  6345. const $r = 114;
  6346. const $t = 116;
  6347. const $u = 117;
  6348. const $v = 118;
  6349. const $x = 120;
  6350. const $z = 122;
  6351. const $LBRACE = 123;
  6352. const $BAR = 124;
  6353. const $RBRACE = 125;
  6354. const $NBSP = 160;
  6355. const $AT = 64;
  6356. const $BT = 96;
  6357. function isWhitespace(code) {
  6358. return (code >= $TAB && code <= $SPACE) || code == $NBSP;
  6359. }
  6360. function isDigit(code) {
  6361. return $0 <= code && code <= $9;
  6362. }
  6363. function isAsciiLetter(code) {
  6364. return (code >= $a && code <= $z) || (code >= $A && code <= $Z);
  6365. }
  6366. function isAsciiHexDigit(code) {
  6367. return (code >= $a && code <= $f) || (code >= $A && code <= $F) || isDigit(code);
  6368. }
  6369. function isNewLine(code) {
  6370. return code === $LF || code === $CR;
  6371. }
  6372. function isOctalDigit(code) {
  6373. return $0 <= code && code <= $7;
  6374. }
  6375. function isQuote(code) {
  6376. return code === $SQ || code === $DQ || code === $BT;
  6377. }
  6378. class ParseLocation {
  6379. file;
  6380. offset;
  6381. line;
  6382. col;
  6383. constructor(file, offset, line, col) {
  6384. this.file = file;
  6385. this.offset = offset;
  6386. this.line = line;
  6387. this.col = col;
  6388. }
  6389. toString() {
  6390. return this.offset != null ? `${this.file.url}@${this.line}:${this.col}` : this.file.url;
  6391. }
  6392. moveBy(delta) {
  6393. const source = this.file.content;
  6394. const len = source.length;
  6395. let offset = this.offset;
  6396. let line = this.line;
  6397. let col = this.col;
  6398. while (offset > 0 && delta < 0) {
  6399. offset--;
  6400. delta++;
  6401. const ch = source.charCodeAt(offset);
  6402. if (ch == $LF) {
  6403. line--;
  6404. const priorLine = source
  6405. .substring(0, offset - 1)
  6406. .lastIndexOf(String.fromCharCode($LF));
  6407. col = priorLine > 0 ? offset - priorLine : offset;
  6408. }
  6409. else {
  6410. col--;
  6411. }
  6412. }
  6413. while (offset < len && delta > 0) {
  6414. const ch = source.charCodeAt(offset);
  6415. offset++;
  6416. delta--;
  6417. if (ch == $LF) {
  6418. line++;
  6419. col = 0;
  6420. }
  6421. else {
  6422. col++;
  6423. }
  6424. }
  6425. return new ParseLocation(this.file, offset, line, col);
  6426. }
  6427. // Return the source around the location
  6428. // Up to `maxChars` or `maxLines` on each side of the location
  6429. getContext(maxChars, maxLines) {
  6430. const content = this.file.content;
  6431. let startOffset = this.offset;
  6432. if (startOffset != null) {
  6433. if (startOffset > content.length - 1) {
  6434. startOffset = content.length - 1;
  6435. }
  6436. let endOffset = startOffset;
  6437. let ctxChars = 0;
  6438. let ctxLines = 0;
  6439. while (ctxChars < maxChars && startOffset > 0) {
  6440. startOffset--;
  6441. ctxChars++;
  6442. if (content[startOffset] == '\n') {
  6443. if (++ctxLines == maxLines) {
  6444. break;
  6445. }
  6446. }
  6447. }
  6448. ctxChars = 0;
  6449. ctxLines = 0;
  6450. while (ctxChars < maxChars && endOffset < content.length - 1) {
  6451. endOffset++;
  6452. ctxChars++;
  6453. if (content[endOffset] == '\n') {
  6454. if (++ctxLines == maxLines) {
  6455. break;
  6456. }
  6457. }
  6458. }
  6459. return {
  6460. before: content.substring(startOffset, this.offset),
  6461. after: content.substring(this.offset, endOffset + 1),
  6462. };
  6463. }
  6464. return null;
  6465. }
  6466. }
  6467. class ParseSourceFile {
  6468. content;
  6469. url;
  6470. constructor(content, url) {
  6471. this.content = content;
  6472. this.url = url;
  6473. }
  6474. }
  6475. class ParseSourceSpan {
  6476. start;
  6477. end;
  6478. fullStart;
  6479. details;
  6480. /**
  6481. * Create an object that holds information about spans of tokens/nodes captured during
  6482. * lexing/parsing of text.
  6483. *
  6484. * @param start
  6485. * The location of the start of the span (having skipped leading trivia).
  6486. * Skipping leading trivia makes source-spans more "user friendly", since things like HTML
  6487. * elements will appear to begin at the start of the opening tag, rather than at the start of any
  6488. * leading trivia, which could include newlines.
  6489. *
  6490. * @param end
  6491. * The location of the end of the span.
  6492. *
  6493. * @param fullStart
  6494. * The start of the token without skipping the leading trivia.
  6495. * This is used by tooling that splits tokens further, such as extracting Angular interpolations
  6496. * from text tokens. Such tooling creates new source-spans relative to the original token's
  6497. * source-span. If leading trivia characters have been skipped then the new source-spans may be
  6498. * incorrectly offset.
  6499. *
  6500. * @param details
  6501. * Additional information (such as identifier names) that should be associated with the span.
  6502. */
  6503. constructor(start, end, fullStart = start, details = null) {
  6504. this.start = start;
  6505. this.end = end;
  6506. this.fullStart = fullStart;
  6507. this.details = details;
  6508. }
  6509. toString() {
  6510. return this.start.file.content.substring(this.start.offset, this.end.offset);
  6511. }
  6512. }
  6513. var ParseErrorLevel;
  6514. (function (ParseErrorLevel) {
  6515. ParseErrorLevel[ParseErrorLevel["WARNING"] = 0] = "WARNING";
  6516. ParseErrorLevel[ParseErrorLevel["ERROR"] = 1] = "ERROR";
  6517. })(ParseErrorLevel || (ParseErrorLevel = {}));
  6518. class ParseError {
  6519. span;
  6520. msg;
  6521. level;
  6522. relatedError;
  6523. constructor(
  6524. /** Location of the error. */
  6525. span,
  6526. /** Error message. */
  6527. msg,
  6528. /** Severity level of the error. */
  6529. level = ParseErrorLevel.ERROR,
  6530. /**
  6531. * Error that caused the error to be surfaced. For example, an error in a sub-expression that
  6532. * couldn't be parsed. Not guaranteed to be defined, but can be used to provide more context.
  6533. */
  6534. relatedError) {
  6535. this.span = span;
  6536. this.msg = msg;
  6537. this.level = level;
  6538. this.relatedError = relatedError;
  6539. }
  6540. contextualMessage() {
  6541. const ctx = this.span.start.getContext(100, 3);
  6542. return ctx
  6543. ? `${this.msg} ("${ctx.before}[${ParseErrorLevel[this.level]} ->]${ctx.after}")`
  6544. : this.msg;
  6545. }
  6546. toString() {
  6547. const details = this.span.details ? `, ${this.span.details}` : '';
  6548. return `${this.contextualMessage()}: ${this.span.start}${details}`;
  6549. }
  6550. }
  6551. /**
  6552. * Generates Source Span object for a given R3 Type for JIT mode.
  6553. *
  6554. * @param kind Component or Directive.
  6555. * @param typeName name of the Component or Directive.
  6556. * @param sourceUrl reference to Component or Directive source.
  6557. * @returns instance of ParseSourceSpan that represent a given Component or Directive.
  6558. */
  6559. function r3JitTypeSourceSpan(kind, typeName, sourceUrl) {
  6560. const sourceFileName = `in ${kind} ${typeName} in ${sourceUrl}`;
  6561. const sourceFile = new ParseSourceFile('', sourceFileName);
  6562. return new ParseSourceSpan(new ParseLocation(sourceFile, -1, -1, -1), new ParseLocation(sourceFile, -1, -1, -1));
  6563. }
  6564. let _anonymousTypeIndex = 0;
  6565. function identifierName(compileIdentifier) {
  6566. if (!compileIdentifier || !compileIdentifier.reference) {
  6567. return null;
  6568. }
  6569. const ref = compileIdentifier.reference;
  6570. if (ref['__anonymousType']) {
  6571. return ref['__anonymousType'];
  6572. }
  6573. if (ref['__forward_ref__']) {
  6574. // We do not want to try to stringify a `forwardRef()` function because that would cause the
  6575. // inner function to be evaluated too early, defeating the whole point of the `forwardRef`.
  6576. return '__forward_ref__';
  6577. }
  6578. let identifier = stringify(ref);
  6579. if (identifier.indexOf('(') >= 0) {
  6580. // case: anonymous functions!
  6581. identifier = `anonymous_${_anonymousTypeIndex++}`;
  6582. ref['__anonymousType'] = identifier;
  6583. }
  6584. else {
  6585. identifier = sanitizeIdentifier(identifier);
  6586. }
  6587. return identifier;
  6588. }
  6589. function sanitizeIdentifier(name) {
  6590. return name.replace(/\W/g, '_');
  6591. }
  6592. /**
  6593. * In TypeScript, tagged template functions expect a "template object", which is an array of
  6594. * "cooked" strings plus a `raw` property that contains an array of "raw" strings. This is
  6595. * typically constructed with a function called `__makeTemplateObject(cooked, raw)`, but it may not
  6596. * be available in all environments.
  6597. *
  6598. * This is a JavaScript polyfill that uses __makeTemplateObject when it's available, but otherwise
  6599. * creates an inline helper with the same functionality.
  6600. *
  6601. * In the inline function, if `Object.defineProperty` is available we use that to attach the `raw`
  6602. * array.
  6603. */
  6604. const makeTemplateObjectPolyfill = '(this&&this.__makeTemplateObject||function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e})';
  6605. class AbstractJsEmitterVisitor extends AbstractEmitterVisitor {
  6606. constructor() {
  6607. super(false);
  6608. }
  6609. visitWrappedNodeExpr(ast, ctx) {
  6610. throw new Error('Cannot emit a WrappedNodeExpr in Javascript.');
  6611. }
  6612. visitDeclareVarStmt(stmt, ctx) {
  6613. ctx.print(stmt, `var ${stmt.name}`);
  6614. if (stmt.value) {
  6615. ctx.print(stmt, ' = ');
  6616. stmt.value.visitExpression(this, ctx);
  6617. }
  6618. ctx.println(stmt, `;`);
  6619. return null;
  6620. }
  6621. visitTaggedTemplateLiteralExpr(ast, ctx) {
  6622. // The following convoluted piece of code is effectively the downlevelled equivalent of
  6623. // ```
  6624. // tag`...`
  6625. // ```
  6626. // which is effectively like:
  6627. // ```
  6628. // tag(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  6629. // ```
  6630. const elements = ast.template.elements;
  6631. ast.tag.visitExpression(this, ctx);
  6632. ctx.print(ast, `(${makeTemplateObjectPolyfill}(`);
  6633. ctx.print(ast, `[${elements.map((part) => escapeIdentifier(part.text, false)).join(', ')}], `);
  6634. ctx.print(ast, `[${elements.map((part) => escapeIdentifier(part.rawText, false)).join(', ')}])`);
  6635. ast.template.expressions.forEach((expression) => {
  6636. ctx.print(ast, ', ');
  6637. expression.visitExpression(this, ctx);
  6638. });
  6639. ctx.print(ast, ')');
  6640. return null;
  6641. }
  6642. visitTemplateLiteralExpr(expr, ctx) {
  6643. ctx.print(expr, '`');
  6644. for (let i = 0; i < expr.elements.length; i++) {
  6645. expr.elements[i].visitExpression(this, ctx);
  6646. const expression = i < expr.expressions.length ? expr.expressions[i] : null;
  6647. if (expression !== null) {
  6648. ctx.print(expression, '${');
  6649. expression.visitExpression(this, ctx);
  6650. ctx.print(expression, '}');
  6651. }
  6652. }
  6653. ctx.print(expr, '`');
  6654. }
  6655. visitTemplateLiteralElementExpr(expr, ctx) {
  6656. ctx.print(expr, expr.rawText);
  6657. return null;
  6658. }
  6659. visitFunctionExpr(ast, ctx) {
  6660. ctx.print(ast, `function${ast.name ? ' ' + ast.name : ''}(`);
  6661. this._visitParams(ast.params, ctx);
  6662. ctx.println(ast, `) {`);
  6663. ctx.incIndent();
  6664. this.visitAllStatements(ast.statements, ctx);
  6665. ctx.decIndent();
  6666. ctx.print(ast, `}`);
  6667. return null;
  6668. }
  6669. visitArrowFunctionExpr(ast, ctx) {
  6670. ctx.print(ast, '(');
  6671. this._visitParams(ast.params, ctx);
  6672. ctx.print(ast, ') =>');
  6673. if (Array.isArray(ast.body)) {
  6674. ctx.println(ast, `{`);
  6675. ctx.incIndent();
  6676. this.visitAllStatements(ast.body, ctx);
  6677. ctx.decIndent();
  6678. ctx.print(ast, `}`);
  6679. }
  6680. else {
  6681. const isObjectLiteral = ast.body instanceof LiteralMapExpr;
  6682. if (isObjectLiteral) {
  6683. ctx.print(ast, '(');
  6684. }
  6685. ast.body.visitExpression(this, ctx);
  6686. if (isObjectLiteral) {
  6687. ctx.print(ast, ')');
  6688. }
  6689. }
  6690. return null;
  6691. }
  6692. visitDeclareFunctionStmt(stmt, ctx) {
  6693. ctx.print(stmt, `function ${stmt.name}(`);
  6694. this._visitParams(stmt.params, ctx);
  6695. ctx.println(stmt, `) {`);
  6696. ctx.incIndent();
  6697. this.visitAllStatements(stmt.statements, ctx);
  6698. ctx.decIndent();
  6699. ctx.println(stmt, `}`);
  6700. return null;
  6701. }
  6702. visitLocalizedString(ast, ctx) {
  6703. // The following convoluted piece of code is effectively the downlevelled equivalent of
  6704. // ```
  6705. // $localize `...`
  6706. // ```
  6707. // which is effectively like:
  6708. // ```
  6709. // $localize(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  6710. // ```
  6711. ctx.print(ast, `$localize(${makeTemplateObjectPolyfill}(`);
  6712. const parts = [ast.serializeI18nHead()];
  6713. for (let i = 1; i < ast.messageParts.length; i++) {
  6714. parts.push(ast.serializeI18nTemplatePart(i));
  6715. }
  6716. ctx.print(ast, `[${parts.map((part) => escapeIdentifier(part.cooked, false)).join(', ')}], `);
  6717. ctx.print(ast, `[${parts.map((part) => escapeIdentifier(part.raw, false)).join(', ')}])`);
  6718. ast.expressions.forEach((expression) => {
  6719. ctx.print(ast, ', ');
  6720. expression.visitExpression(this, ctx);
  6721. });
  6722. ctx.print(ast, ')');
  6723. return null;
  6724. }
  6725. _visitParams(params, ctx) {
  6726. this.visitAllObjects((param) => ctx.print(null, param.name), params, ctx, ',');
  6727. }
  6728. }
  6729. /**
  6730. * @fileoverview
  6731. * A module to facilitate use of a Trusted Types policy within the JIT
  6732. * compiler. It lazily constructs the Trusted Types policy, providing helper
  6733. * utilities for promoting strings to Trusted Types. When Trusted Types are not
  6734. * available, strings are used as a fallback.
  6735. * @security All use of this module is security-sensitive and should go through
  6736. * security review.
  6737. */
  6738. /**
  6739. * The Trusted Types policy, or null if Trusted Types are not
  6740. * enabled/supported, or undefined if the policy has not been created yet.
  6741. */
  6742. let policy;
  6743. /**
  6744. * Returns the Trusted Types policy, or null if Trusted Types are not
  6745. * enabled/supported. The first call to this function will create the policy.
  6746. */
  6747. function getPolicy() {
  6748. if (policy === undefined) {
  6749. const trustedTypes = _global['trustedTypes'];
  6750. policy = null;
  6751. if (trustedTypes) {
  6752. try {
  6753. policy = trustedTypes.createPolicy('angular#unsafe-jit', {
  6754. createScript: (s) => s,
  6755. });
  6756. }
  6757. catch {
  6758. // trustedTypes.createPolicy throws if called with a name that is
  6759. // already registered, even in report-only mode. Until the API changes,
  6760. // catch the error not to break the applications functionally. In such
  6761. // cases, the code will fall back to using strings.
  6762. }
  6763. }
  6764. }
  6765. return policy;
  6766. }
  6767. /**
  6768. * Unsafely promote a string to a TrustedScript, falling back to strings when
  6769. * Trusted Types are not available.
  6770. * @security In particular, it must be assured that the provided string will
  6771. * never cause an XSS vulnerability if used in a context that will be
  6772. * interpreted and executed as a script by a browser, e.g. when calling eval.
  6773. */
  6774. function trustedScriptFromString(script) {
  6775. return getPolicy()?.createScript(script) || script;
  6776. }
  6777. /**
  6778. * Unsafely call the Function constructor with the given string arguments.
  6779. * @security This is a security-sensitive function; any use of this function
  6780. * must go through security review. In particular, it must be assured that it
  6781. * is only called from the JIT compiler, as use in other code can lead to XSS
  6782. * vulnerabilities.
  6783. */
  6784. function newTrustedFunctionForJIT(...args) {
  6785. if (!_global['trustedTypes']) {
  6786. // In environments that don't support Trusted Types, fall back to the most
  6787. // straightforward implementation:
  6788. return new Function(...args);
  6789. }
  6790. // Chrome currently does not support passing TrustedScript to the Function
  6791. // constructor. The following implements the workaround proposed on the page
  6792. // below, where the Chromium bug is also referenced:
  6793. // https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
  6794. const fnArgs = args.slice(0, -1).join(',');
  6795. const fnBody = args[args.length - 1];
  6796. const body = `(function anonymous(${fnArgs}
  6797. ) { ${fnBody}
  6798. })`;
  6799. // Using eval directly confuses the compiler and prevents this module from
  6800. // being stripped out of JS binaries even if not used. The global['eval']
  6801. // indirection fixes that.
  6802. const fn = _global['eval'](trustedScriptFromString(body));
  6803. if (fn.bind === undefined) {
  6804. // Workaround for a browser bug that only exists in Chrome 83, where passing
  6805. // a TrustedScript to eval just returns the TrustedScript back without
  6806. // evaluating it. In that case, fall back to the most straightforward
  6807. // implementation:
  6808. return new Function(...args);
  6809. }
  6810. // To completely mimic the behavior of calling "new Function", two more
  6811. // things need to happen:
  6812. // 1. Stringifying the resulting function should return its source code
  6813. fn.toString = () => body;
  6814. // 2. When calling the resulting function, `this` should refer to `global`
  6815. return fn.bind(_global);
  6816. // When Trusted Types support in Function constructors is widely available,
  6817. // the implementation of this function can be simplified to:
  6818. // return new Function(...args.map(a => trustedScriptFromString(a)));
  6819. }
  6820. /**
  6821. * A helper class to manage the evaluation of JIT generated code.
  6822. */
  6823. class JitEvaluator {
  6824. /**
  6825. *
  6826. * @param sourceUrl The URL of the generated code.
  6827. * @param statements An array of Angular statement AST nodes to be evaluated.
  6828. * @param refResolver Resolves `o.ExternalReference`s into values.
  6829. * @param createSourceMaps If true then create a source-map for the generated code and include it
  6830. * inline as a source-map comment.
  6831. * @returns A map of all the variables in the generated code.
  6832. */
  6833. evaluateStatements(sourceUrl, statements, refResolver, createSourceMaps) {
  6834. const converter = new JitEmitterVisitor(refResolver);
  6835. const ctx = EmitterVisitorContext.createRoot();
  6836. // Ensure generated code is in strict mode
  6837. if (statements.length > 0 && !isUseStrictStatement(statements[0])) {
  6838. statements = [literal$1('use strict').toStmt(), ...statements];
  6839. }
  6840. converter.visitAllStatements(statements, ctx);
  6841. converter.createReturnStmt(ctx);
  6842. return this.evaluateCode(sourceUrl, ctx, converter.getArgs(), createSourceMaps);
  6843. }
  6844. /**
  6845. * Evaluate a piece of JIT generated code.
  6846. * @param sourceUrl The URL of this generated code.
  6847. * @param ctx A context object that contains an AST of the code to be evaluated.
  6848. * @param vars A map containing the names and values of variables that the evaluated code might
  6849. * reference.
  6850. * @param createSourceMap If true then create a source-map for the generated code and include it
  6851. * inline as a source-map comment.
  6852. * @returns The result of evaluating the code.
  6853. */
  6854. evaluateCode(sourceUrl, ctx, vars, createSourceMap) {
  6855. let fnBody = `"use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl}`;
  6856. const fnArgNames = [];
  6857. const fnArgValues = [];
  6858. for (const argName in vars) {
  6859. fnArgValues.push(vars[argName]);
  6860. fnArgNames.push(argName);
  6861. }
  6862. if (createSourceMap) {
  6863. // using `new Function(...)` generates a header, 1 line of no arguments, 2 lines otherwise
  6864. // E.g. ```
  6865. // function anonymous(a,b,c
  6866. // /**/) { ... }```
  6867. // We don't want to hard code this fact, so we auto detect it via an empty function first.
  6868. const emptyFn = newTrustedFunctionForJIT(...fnArgNames.concat('return null;')).toString();
  6869. const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1;
  6870. fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()}`;
  6871. }
  6872. const fn = newTrustedFunctionForJIT(...fnArgNames.concat(fnBody));
  6873. return this.executeFunction(fn, fnArgValues);
  6874. }
  6875. /**
  6876. * Execute a JIT generated function by calling it.
  6877. *
  6878. * This method can be overridden in tests to capture the functions that are generated
  6879. * by this `JitEvaluator` class.
  6880. *
  6881. * @param fn A function to execute.
  6882. * @param args The arguments to pass to the function being executed.
  6883. * @returns The return value of the executed function.
  6884. */
  6885. executeFunction(fn, args) {
  6886. return fn(...args);
  6887. }
  6888. }
  6889. /**
  6890. * An Angular AST visitor that converts AST nodes into executable JavaScript code.
  6891. */
  6892. class JitEmitterVisitor extends AbstractJsEmitterVisitor {
  6893. refResolver;
  6894. _evalArgNames = [];
  6895. _evalArgValues = [];
  6896. _evalExportedVars = [];
  6897. constructor(refResolver) {
  6898. super();
  6899. this.refResolver = refResolver;
  6900. }
  6901. createReturnStmt(ctx) {
  6902. const stmt = new ReturnStatement(new LiteralMapExpr(this._evalExportedVars.map((resultVar) => new LiteralMapEntry(resultVar, variable(resultVar), false))));
  6903. stmt.visitStatement(this, ctx);
  6904. }
  6905. getArgs() {
  6906. const result = {};
  6907. for (let i = 0; i < this._evalArgNames.length; i++) {
  6908. result[this._evalArgNames[i]] = this._evalArgValues[i];
  6909. }
  6910. return result;
  6911. }
  6912. visitExternalExpr(ast, ctx) {
  6913. this._emitReferenceToExternal(ast, this.refResolver.resolveExternalReference(ast.value), ctx);
  6914. return null;
  6915. }
  6916. visitWrappedNodeExpr(ast, ctx) {
  6917. this._emitReferenceToExternal(ast, ast.node, ctx);
  6918. return null;
  6919. }
  6920. visitDeclareVarStmt(stmt, ctx) {
  6921. if (stmt.hasModifier(exports.StmtModifier.Exported)) {
  6922. this._evalExportedVars.push(stmt.name);
  6923. }
  6924. return super.visitDeclareVarStmt(stmt, ctx);
  6925. }
  6926. visitDeclareFunctionStmt(stmt, ctx) {
  6927. if (stmt.hasModifier(exports.StmtModifier.Exported)) {
  6928. this._evalExportedVars.push(stmt.name);
  6929. }
  6930. return super.visitDeclareFunctionStmt(stmt, ctx);
  6931. }
  6932. _emitReferenceToExternal(ast, value, ctx) {
  6933. let id = this._evalArgValues.indexOf(value);
  6934. if (id === -1) {
  6935. id = this._evalArgValues.length;
  6936. this._evalArgValues.push(value);
  6937. const name = identifierName({ reference: value }) || 'val';
  6938. this._evalArgNames.push(`jit_${name}_${id}`);
  6939. }
  6940. ctx.print(ast, this._evalArgNames[id]);
  6941. }
  6942. }
  6943. function isUseStrictStatement(statement) {
  6944. return statement.isEquivalent(literal$1('use strict').toStmt());
  6945. }
  6946. function compileInjector(meta) {
  6947. const definitionMap = new DefinitionMap();
  6948. if (meta.providers !== null) {
  6949. definitionMap.set('providers', meta.providers);
  6950. }
  6951. if (meta.imports.length > 0) {
  6952. definitionMap.set('imports', literalArr(meta.imports));
  6953. }
  6954. const expression = importExpr(Identifiers.defineInjector)
  6955. .callFn([definitionMap.toLiteralMap()], undefined, true);
  6956. const type = createInjectorType(meta);
  6957. return { expression, type, statements: [] };
  6958. }
  6959. function createInjectorType(meta) {
  6960. return new ExpressionType(importExpr(Identifiers.InjectorDeclaration, [new ExpressionType(meta.type.type)]));
  6961. }
  6962. /**
  6963. * Implementation of `CompileReflector` which resolves references to @angular/core
  6964. * symbols at runtime, according to a consumer-provided mapping.
  6965. *
  6966. * Only supports `resolveExternalReference`, all other methods throw.
  6967. */
  6968. class R3JitReflector {
  6969. context;
  6970. constructor(context) {
  6971. this.context = context;
  6972. }
  6973. resolveExternalReference(ref) {
  6974. // This reflector only handles @angular/core imports.
  6975. if (ref.moduleName !== '@angular/core') {
  6976. throw new Error(`Cannot resolve external reference to ${ref.moduleName}, only references to @angular/core are supported.`);
  6977. }
  6978. if (!this.context.hasOwnProperty(ref.name)) {
  6979. throw new Error(`No value provided for @angular/core symbol '${ref.name}'.`);
  6980. }
  6981. return this.context[ref.name];
  6982. }
  6983. }
  6984. /**
  6985. * How the selector scope of an NgModule (its declarations, imports, and exports) should be emitted
  6986. * as a part of the NgModule definition.
  6987. */
  6988. exports.R3SelectorScopeMode = void 0;
  6989. (function (R3SelectorScopeMode) {
  6990. /**
  6991. * Emit the declarations inline into the module definition.
  6992. *
  6993. * This option is useful in certain contexts where it's known that JIT support is required. The
  6994. * tradeoff here is that this emit style prevents directives and pipes from being tree-shaken if
  6995. * they are unused, but the NgModule is used.
  6996. */
  6997. R3SelectorScopeMode[R3SelectorScopeMode["Inline"] = 0] = "Inline";
  6998. /**
  6999. * Emit the declarations using a side effectful function call, `ɵɵsetNgModuleScope`, that is
  7000. * guarded with the `ngJitMode` flag.
  7001. *
  7002. * This form of emit supports JIT and can be optimized away if the `ngJitMode` flag is set to
  7003. * false, which allows unused directives and pipes to be tree-shaken.
  7004. */
  7005. R3SelectorScopeMode[R3SelectorScopeMode["SideEffect"] = 1] = "SideEffect";
  7006. /**
  7007. * Don't generate selector scopes at all.
  7008. *
  7009. * This is useful for contexts where JIT support is known to be unnecessary.
  7010. */
  7011. R3SelectorScopeMode[R3SelectorScopeMode["Omit"] = 2] = "Omit";
  7012. })(exports.R3SelectorScopeMode || (exports.R3SelectorScopeMode = {}));
  7013. /**
  7014. * The type of the NgModule meta data.
  7015. * - Global: Used for full and partial compilation modes which mainly includes R3References.
  7016. * - Local: Used for the local compilation mode which mainly includes the raw expressions as appears
  7017. * in the NgModule decorator.
  7018. */
  7019. exports.R3NgModuleMetadataKind = void 0;
  7020. (function (R3NgModuleMetadataKind) {
  7021. R3NgModuleMetadataKind[R3NgModuleMetadataKind["Global"] = 0] = "Global";
  7022. R3NgModuleMetadataKind[R3NgModuleMetadataKind["Local"] = 1] = "Local";
  7023. })(exports.R3NgModuleMetadataKind || (exports.R3NgModuleMetadataKind = {}));
  7024. /**
  7025. * Construct an `R3NgModuleDef` for the given `R3NgModuleMetadata`.
  7026. */
  7027. function compileNgModule(meta) {
  7028. const statements = [];
  7029. const definitionMap = new DefinitionMap();
  7030. definitionMap.set('type', meta.type.value);
  7031. // Assign bootstrap definition. In local compilation mode (i.e., for
  7032. // `R3NgModuleMetadataKind.LOCAL`) we assign the bootstrap field using the runtime
  7033. // `ɵɵsetNgModuleScope`.
  7034. if (meta.kind === exports.R3NgModuleMetadataKind.Global && meta.bootstrap.length > 0) {
  7035. definitionMap.set('bootstrap', refsToArray(meta.bootstrap, meta.containsForwardDecls));
  7036. }
  7037. if (meta.selectorScopeMode === exports.R3SelectorScopeMode.Inline) {
  7038. // If requested to emit scope information inline, pass the `declarations`, `imports` and
  7039. // `exports` to the `ɵɵdefineNgModule()` call directly.
  7040. if (meta.declarations.length > 0) {
  7041. definitionMap.set('declarations', refsToArray(meta.declarations, meta.containsForwardDecls));
  7042. }
  7043. if (meta.imports.length > 0) {
  7044. definitionMap.set('imports', refsToArray(meta.imports, meta.containsForwardDecls));
  7045. }
  7046. if (meta.exports.length > 0) {
  7047. definitionMap.set('exports', refsToArray(meta.exports, meta.containsForwardDecls));
  7048. }
  7049. }
  7050. else if (meta.selectorScopeMode === exports.R3SelectorScopeMode.SideEffect) {
  7051. // In this mode, scope information is not passed into `ɵɵdefineNgModule` as it
  7052. // would prevent tree-shaking of the declarations, imports and exports references. Instead, it's
  7053. // patched onto the NgModule definition with a `ɵɵsetNgModuleScope` call that's guarded by the
  7054. // `ngJitMode` flag.
  7055. const setNgModuleScopeCall = generateSetNgModuleScopeCall(meta);
  7056. if (setNgModuleScopeCall !== null) {
  7057. statements.push(setNgModuleScopeCall);
  7058. }
  7059. }
  7060. else ;
  7061. if (meta.schemas !== null && meta.schemas.length > 0) {
  7062. definitionMap.set('schemas', literalArr(meta.schemas.map((ref) => ref.value)));
  7063. }
  7064. if (meta.id !== null) {
  7065. definitionMap.set('id', meta.id);
  7066. // Generate a side-effectful call to register this NgModule by its id, as per the semantics of
  7067. // NgModule ids.
  7068. statements.push(importExpr(Identifiers.registerNgModuleType).callFn([meta.type.value, meta.id]).toStmt());
  7069. }
  7070. const expression = importExpr(Identifiers.defineNgModule)
  7071. .callFn([definitionMap.toLiteralMap()], undefined, true);
  7072. const type = createNgModuleType(meta);
  7073. return { expression, type, statements };
  7074. }
  7075. /**
  7076. * This function is used in JIT mode to generate the call to `ɵɵdefineNgModule()` from a call to
  7077. * `ɵɵngDeclareNgModule()`.
  7078. */
  7079. function compileNgModuleDeclarationExpression(meta) {
  7080. const definitionMap = new DefinitionMap();
  7081. definitionMap.set('type', new WrappedNodeExpr(meta.type));
  7082. if (meta.bootstrap !== undefined) {
  7083. definitionMap.set('bootstrap', new WrappedNodeExpr(meta.bootstrap));
  7084. }
  7085. if (meta.declarations !== undefined) {
  7086. definitionMap.set('declarations', new WrappedNodeExpr(meta.declarations));
  7087. }
  7088. if (meta.imports !== undefined) {
  7089. definitionMap.set('imports', new WrappedNodeExpr(meta.imports));
  7090. }
  7091. if (meta.exports !== undefined) {
  7092. definitionMap.set('exports', new WrappedNodeExpr(meta.exports));
  7093. }
  7094. if (meta.schemas !== undefined) {
  7095. definitionMap.set('schemas', new WrappedNodeExpr(meta.schemas));
  7096. }
  7097. if (meta.id !== undefined) {
  7098. definitionMap.set('id', new WrappedNodeExpr(meta.id));
  7099. }
  7100. return importExpr(Identifiers.defineNgModule).callFn([definitionMap.toLiteralMap()]);
  7101. }
  7102. function createNgModuleType(meta) {
  7103. if (meta.kind === exports.R3NgModuleMetadataKind.Local) {
  7104. return new ExpressionType(meta.type.value);
  7105. }
  7106. const { type: moduleType, declarations, exports: exports$1, imports, includeImportTypes, publicDeclarationTypes, } = meta;
  7107. return new ExpressionType(importExpr(Identifiers.NgModuleDeclaration, [
  7108. new ExpressionType(moduleType.type),
  7109. publicDeclarationTypes === null
  7110. ? tupleTypeOf(declarations)
  7111. : tupleOfTypes(publicDeclarationTypes),
  7112. includeImportTypes ? tupleTypeOf(imports) : NONE_TYPE,
  7113. tupleTypeOf(exports$1),
  7114. ]));
  7115. }
  7116. /**
  7117. * Generates a function call to `ɵɵsetNgModuleScope` with all necessary information so that the
  7118. * transitive module scope can be computed during runtime in JIT mode. This call is marked pure
  7119. * such that the references to declarations, imports and exports may be elided causing these
  7120. * symbols to become tree-shakeable.
  7121. */
  7122. function generateSetNgModuleScopeCall(meta) {
  7123. const scopeMap = new DefinitionMap();
  7124. if (meta.kind === exports.R3NgModuleMetadataKind.Global) {
  7125. if (meta.declarations.length > 0) {
  7126. scopeMap.set('declarations', refsToArray(meta.declarations, meta.containsForwardDecls));
  7127. }
  7128. }
  7129. else {
  7130. if (meta.declarationsExpression) {
  7131. scopeMap.set('declarations', meta.declarationsExpression);
  7132. }
  7133. }
  7134. if (meta.kind === exports.R3NgModuleMetadataKind.Global) {
  7135. if (meta.imports.length > 0) {
  7136. scopeMap.set('imports', refsToArray(meta.imports, meta.containsForwardDecls));
  7137. }
  7138. }
  7139. else {
  7140. if (meta.importsExpression) {
  7141. scopeMap.set('imports', meta.importsExpression);
  7142. }
  7143. }
  7144. if (meta.kind === exports.R3NgModuleMetadataKind.Global) {
  7145. if (meta.exports.length > 0) {
  7146. scopeMap.set('exports', refsToArray(meta.exports, meta.containsForwardDecls));
  7147. }
  7148. }
  7149. else {
  7150. if (meta.exportsExpression) {
  7151. scopeMap.set('exports', meta.exportsExpression);
  7152. }
  7153. }
  7154. if (meta.kind === exports.R3NgModuleMetadataKind.Local && meta.bootstrapExpression) {
  7155. scopeMap.set('bootstrap', meta.bootstrapExpression);
  7156. }
  7157. if (Object.keys(scopeMap.values).length === 0) {
  7158. return null;
  7159. }
  7160. // setNgModuleScope(...)
  7161. const fnCall = new InvokeFunctionExpr(
  7162. /* fn */ importExpr(Identifiers.setNgModuleScope),
  7163. /* args */ [meta.type.value, scopeMap.toLiteralMap()]);
  7164. // (ngJitMode guard) && setNgModuleScope(...)
  7165. const guardedCall = jitOnlyGuardedExpression(fnCall);
  7166. // function() { (ngJitMode guard) && setNgModuleScope(...); }
  7167. const iife = new FunctionExpr(/* params */ [], /* statements */ [guardedCall.toStmt()]);
  7168. // (function() { (ngJitMode guard) && setNgModuleScope(...); })()
  7169. const iifeCall = new InvokeFunctionExpr(/* fn */ iife, /* args */ []);
  7170. return iifeCall.toStmt();
  7171. }
  7172. function tupleTypeOf(exp) {
  7173. const types = exp.map((ref) => typeofExpr(ref.type));
  7174. return exp.length > 0 ? expressionType(literalArr(types)) : NONE_TYPE;
  7175. }
  7176. function tupleOfTypes(types) {
  7177. const typeofTypes = types.map((type) => typeofExpr(type));
  7178. return types.length > 0 ? expressionType(literalArr(typeofTypes)) : NONE_TYPE;
  7179. }
  7180. function compilePipeFromMetadata(metadata) {
  7181. const definitionMapValues = [];
  7182. // e.g. `name: 'myPipe'`
  7183. definitionMapValues.push({ key: 'name', value: literal$1(metadata.pipeName), quoted: false });
  7184. // e.g. `type: MyPipe`
  7185. definitionMapValues.push({ key: 'type', value: metadata.type.value, quoted: false });
  7186. // e.g. `pure: true`
  7187. definitionMapValues.push({ key: 'pure', value: literal$1(metadata.pure), quoted: false });
  7188. if (metadata.isStandalone === false) {
  7189. definitionMapValues.push({ key: 'standalone', value: literal$1(false), quoted: false });
  7190. }
  7191. const expression = importExpr(Identifiers.definePipe)
  7192. .callFn([literalMap(definitionMapValues)], undefined, true);
  7193. const type = createPipeType(metadata);
  7194. return { expression, type, statements: [] };
  7195. }
  7196. function createPipeType(metadata) {
  7197. return new ExpressionType(importExpr(Identifiers.PipeDeclaration, [
  7198. typeWithParameters(metadata.type.type, metadata.typeArgumentCount),
  7199. new ExpressionType(new LiteralExpr(metadata.pipeName)),
  7200. new ExpressionType(new LiteralExpr(metadata.isStandalone)),
  7201. ]));
  7202. }
  7203. exports.R3TemplateDependencyKind = void 0;
  7204. (function (R3TemplateDependencyKind) {
  7205. R3TemplateDependencyKind[R3TemplateDependencyKind["Directive"] = 0] = "Directive";
  7206. R3TemplateDependencyKind[R3TemplateDependencyKind["Pipe"] = 1] = "Pipe";
  7207. R3TemplateDependencyKind[R3TemplateDependencyKind["NgModule"] = 2] = "NgModule";
  7208. })(exports.R3TemplateDependencyKind || (exports.R3TemplateDependencyKind = {}));
  7209. /**
  7210. * The following set contains all keywords that can be used in the animation css shorthand
  7211. * property and is used during the scoping of keyframes to make sure such keywords
  7212. * are not modified.
  7213. */
  7214. const animationKeywords = new Set([
  7215. // global values
  7216. 'inherit',
  7217. 'initial',
  7218. 'revert',
  7219. 'unset',
  7220. // animation-direction
  7221. 'alternate',
  7222. 'alternate-reverse',
  7223. 'normal',
  7224. 'reverse',
  7225. // animation-fill-mode
  7226. 'backwards',
  7227. 'both',
  7228. 'forwards',
  7229. 'none',
  7230. // animation-play-state
  7231. 'paused',
  7232. 'running',
  7233. // animation-timing-function
  7234. 'ease',
  7235. 'ease-in',
  7236. 'ease-in-out',
  7237. 'ease-out',
  7238. 'linear',
  7239. 'step-start',
  7240. 'step-end',
  7241. // `steps()` function
  7242. 'end',
  7243. 'jump-both',
  7244. 'jump-end',
  7245. 'jump-none',
  7246. 'jump-start',
  7247. 'start',
  7248. ]);
  7249. /**
  7250. * The following array contains all of the CSS at-rule identifiers which are scoped.
  7251. */
  7252. const scopedAtRuleIdentifiers = [
  7253. '@media',
  7254. '@supports',
  7255. '@document',
  7256. '@layer',
  7257. '@container',
  7258. '@scope',
  7259. '@starting-style',
  7260. ];
  7261. /**
  7262. * The following class has its origin from a port of shadowCSS from webcomponents.js to TypeScript.
  7263. * It has since diverge in many ways to tailor Angular's needs.
  7264. *
  7265. * Source:
  7266. * https://github.com/webcomponents/webcomponentsjs/blob/4efecd7e0e/src/ShadowCSS/ShadowCSS.js
  7267. *
  7268. * The original file level comment is reproduced below
  7269. */
  7270. /*
  7271. This is a limited shim for ShadowDOM css styling.
  7272. https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html#styles
  7273. The intention here is to support only the styling features which can be
  7274. relatively simply implemented. The goal is to allow users to avoid the
  7275. most obvious pitfalls and do so without compromising performance significantly.
  7276. For ShadowDOM styling that's not covered here, a set of best practices
  7277. can be provided that should allow users to accomplish more complex styling.
  7278. The following is a list of specific ShadowDOM styling features and a brief
  7279. discussion of the approach used to shim.
  7280. Shimmed features:
  7281. * :host, :host-context: ShadowDOM allows styling of the shadowRoot's host
  7282. element using the :host rule. To shim this feature, the :host styles are
  7283. reformatted and prefixed with a given scope name and promoted to a
  7284. document level stylesheet.
  7285. For example, given a scope name of .foo, a rule like this:
  7286. :host {
  7287. background: red;
  7288. }
  7289. }
  7290. becomes:
  7291. .foo {
  7292. background: red;
  7293. }
  7294. * encapsulation: Styles defined within ShadowDOM, apply only to
  7295. dom inside the ShadowDOM.
  7296. The selectors are scoped by adding an attribute selector suffix to each
  7297. simple selector that contains the host element tag name. Each element
  7298. in the element's ShadowDOM template is also given the scope attribute.
  7299. Thus, these rules match only elements that have the scope attribute.
  7300. For example, given a scope name of x-foo, a rule like this:
  7301. div {
  7302. font-weight: bold;
  7303. }
  7304. becomes:
  7305. div[x-foo] {
  7306. font-weight: bold;
  7307. }
  7308. Note that elements that are dynamically added to a scope must have the scope
  7309. selector added to them manually.
  7310. * upper/lower bound encapsulation: Styles which are defined outside a
  7311. shadowRoot should not cross the ShadowDOM boundary and should not apply
  7312. inside a shadowRoot.
  7313. This styling behavior is not emulated. Some possible ways to do this that
  7314. were rejected due to complexity and/or performance concerns include: (1) reset
  7315. every possible property for every possible selector for a given scope name;
  7316. (2) re-implement css in javascript.
  7317. As an alternative, users should make sure to use selectors
  7318. specific to the scope in which they are working.
  7319. * ::distributed: This behavior is not emulated. It's often not necessary
  7320. to style the contents of a specific insertion point and instead, descendants
  7321. of the host element can be styled selectively. Users can also create an
  7322. extra node around an insertion point and style that node's contents
  7323. via descendent selectors. For example, with a shadowRoot like this:
  7324. <style>
  7325. ::content(div) {
  7326. background: red;
  7327. }
  7328. </style>
  7329. <content></content>
  7330. could become:
  7331. <style>
  7332. / *@polyfill .content-container div * /
  7333. ::content(div) {
  7334. background: red;
  7335. }
  7336. </style>
  7337. <div class="content-container">
  7338. <content></content>
  7339. </div>
  7340. Note the use of @polyfill in the comment above a ShadowDOM specific style
  7341. declaration. This is a directive to the styling shim to use the selector
  7342. in comments in lieu of the next selector when running under polyfill.
  7343. */
  7344. class ShadowCss {
  7345. /*
  7346. * Shim some cssText with the given selector. Returns cssText that can be included in the document
  7347. *
  7348. * The selector is the attribute added to all elements inside the host,
  7349. * The hostSelector is the attribute added to the host itself.
  7350. */
  7351. shimCssText(cssText, selector, hostSelector = '') {
  7352. // **NOTE**: Do not strip comments as this will cause component sourcemaps to break
  7353. // due to shift in lines.
  7354. // Collect comments and replace them with a placeholder, this is done to avoid complicating
  7355. // the rule parsing RegExp and keep it safer.
  7356. const comments = [];
  7357. cssText = cssText.replace(_commentRe, (m) => {
  7358. if (m.match(_commentWithHashRe)) {
  7359. comments.push(m);
  7360. }
  7361. else {
  7362. // Replace non hash comments with empty lines.
  7363. // This is done so that we do not leak any sensitive data in comments.
  7364. const newLinesMatches = m.match(_newLinesRe);
  7365. comments.push((newLinesMatches?.join('') ?? '') + '\n');
  7366. }
  7367. return COMMENT_PLACEHOLDER;
  7368. });
  7369. cssText = this._insertDirectives(cssText);
  7370. const scopedCssText = this._scopeCssText(cssText, selector, hostSelector);
  7371. // Add back comments at the original position.
  7372. let commentIdx = 0;
  7373. return scopedCssText.replace(_commentWithHashPlaceHolderRe, () => comments[commentIdx++]);
  7374. }
  7375. _insertDirectives(cssText) {
  7376. cssText = this._insertPolyfillDirectivesInCssText(cssText);
  7377. return this._insertPolyfillRulesInCssText(cssText);
  7378. }
  7379. /**
  7380. * Process styles to add scope to keyframes.
  7381. *
  7382. * Modify both the names of the keyframes defined in the component styles and also the css
  7383. * animation rules using them.
  7384. *
  7385. * Animation rules using keyframes defined elsewhere are not modified to allow for globally
  7386. * defined keyframes.
  7387. *
  7388. * For example, we convert this css:
  7389. *
  7390. * ```scss
  7391. * .box {
  7392. * animation: box-animation 1s forwards;
  7393. * }
  7394. *
  7395. * @keyframes box-animation {
  7396. * to {
  7397. * background-color: green;
  7398. * }
  7399. * }
  7400. * ```
  7401. *
  7402. * to this:
  7403. *
  7404. * ```scss
  7405. * .box {
  7406. * animation: scopeName_box-animation 1s forwards;
  7407. * }
  7408. *
  7409. * @keyframes scopeName_box-animation {
  7410. * to {
  7411. * background-color: green;
  7412. * }
  7413. * }
  7414. * ```
  7415. *
  7416. * @param cssText the component's css text that needs to be scoped.
  7417. * @param scopeSelector the component's scope selector.
  7418. *
  7419. * @returns the scoped css text.
  7420. */
  7421. _scopeKeyframesRelatedCss(cssText, scopeSelector) {
  7422. const unscopedKeyframesSet = new Set();
  7423. const scopedKeyframesCssText = processRules(cssText, (rule) => this._scopeLocalKeyframeDeclarations(rule, scopeSelector, unscopedKeyframesSet));
  7424. return processRules(scopedKeyframesCssText, (rule) => this._scopeAnimationRule(rule, scopeSelector, unscopedKeyframesSet));
  7425. }
  7426. /**
  7427. * Scopes local keyframes names, returning the updated css rule and it also
  7428. * adds the original keyframe name to a provided set to collect all keyframes names
  7429. * so that it can later be used to scope the animation rules.
  7430. *
  7431. * For example, it takes a rule such as:
  7432. *
  7433. * ```scss
  7434. * @keyframes box-animation {
  7435. * to {
  7436. * background-color: green;
  7437. * }
  7438. * }
  7439. * ```
  7440. *
  7441. * and returns:
  7442. *
  7443. * ```scss
  7444. * @keyframes scopeName_box-animation {
  7445. * to {
  7446. * background-color: green;
  7447. * }
  7448. * }
  7449. * ```
  7450. * and as a side effect it adds "box-animation" to the `unscopedKeyframesSet` set
  7451. *
  7452. * @param cssRule the css rule to process.
  7453. * @param scopeSelector the component's scope selector.
  7454. * @param unscopedKeyframesSet the set of unscoped keyframes names (which can be
  7455. * modified as a side effect)
  7456. *
  7457. * @returns the css rule modified with the scoped keyframes name.
  7458. */
  7459. _scopeLocalKeyframeDeclarations(rule, scopeSelector, unscopedKeyframesSet) {
  7460. return {
  7461. ...rule,
  7462. selector: rule.selector.replace(/(^@(?:-webkit-)?keyframes(?:\s+))(['"]?)(.+)\2(\s*)$/, (_, start, quote, keyframeName, endSpaces) => {
  7463. unscopedKeyframesSet.add(unescapeQuotes(keyframeName, quote));
  7464. return `${start}${quote}${scopeSelector}_${keyframeName}${quote}${endSpaces}`;
  7465. }),
  7466. };
  7467. }
  7468. /**
  7469. * Function used to scope a keyframes name (obtained from an animation declaration)
  7470. * using an existing set of unscopedKeyframes names to discern if the scoping needs to be
  7471. * performed (keyframes names of keyframes not defined in the component's css need not to be
  7472. * scoped).
  7473. *
  7474. * @param keyframe the keyframes name to check.
  7475. * @param scopeSelector the component's scope selector.
  7476. * @param unscopedKeyframesSet the set of unscoped keyframes names.
  7477. *
  7478. * @returns the scoped name of the keyframe, or the original name is the name need not to be
  7479. * scoped.
  7480. */
  7481. _scopeAnimationKeyframe(keyframe, scopeSelector, unscopedKeyframesSet) {
  7482. return keyframe.replace(/^(\s*)(['"]?)(.+?)\2(\s*)$/, (_, spaces1, quote, name, spaces2) => {
  7483. name = `${unscopedKeyframesSet.has(unescapeQuotes(name, quote)) ? scopeSelector + '_' : ''}${name}`;
  7484. return `${spaces1}${quote}${name}${quote}${spaces2}`;
  7485. });
  7486. }
  7487. /**
  7488. * Regular expression used to extrapolate the possible keyframes from an
  7489. * animation declaration (with possibly multiple animation definitions)
  7490. *
  7491. * The regular expression can be divided in three parts
  7492. * - (^|\s+|,)
  7493. * captures how many (if any) leading whitespaces are present or a comma
  7494. * - (?:(?:(['"])((?:\\\\|\\\2|(?!\2).)+)\2)|(-?[A-Za-z][\w\-]*))
  7495. * captures two different possible keyframes, ones which are quoted or ones which are valid css
  7496. * indents (custom properties excluded)
  7497. * - (?=[,\s;]|$)
  7498. * simply matches the end of the possible keyframe, valid endings are: a comma, a space, a
  7499. * semicolon or the end of the string
  7500. */
  7501. _animationDeclarationKeyframesRe = /(^|\s+|,)(?:(?:(['"])((?:\\\\|\\\2|(?!\2).)+)\2)|(-?[A-Za-z][\w\-]*))(?=[,\s]|$)/g;
  7502. /**
  7503. * Scope an animation rule so that the keyframes mentioned in such rule
  7504. * are scoped if defined in the component's css and left untouched otherwise.
  7505. *
  7506. * It can scope values of both the 'animation' and 'animation-name' properties.
  7507. *
  7508. * @param rule css rule to scope.
  7509. * @param scopeSelector the component's scope selector.
  7510. * @param unscopedKeyframesSet the set of unscoped keyframes names.
  7511. *
  7512. * @returns the updated css rule.
  7513. **/
  7514. _scopeAnimationRule(rule, scopeSelector, unscopedKeyframesSet) {
  7515. let content = rule.content.replace(/((?:^|\s+|;)(?:-webkit-)?animation\s*:\s*),*([^;]+)/g, (_, start, animationDeclarations) => start +
  7516. animationDeclarations.replace(this._animationDeclarationKeyframesRe, (original, leadingSpaces, quote = '', quotedName, nonQuotedName) => {
  7517. if (quotedName) {
  7518. return `${leadingSpaces}${this._scopeAnimationKeyframe(`${quote}${quotedName}${quote}`, scopeSelector, unscopedKeyframesSet)}`;
  7519. }
  7520. else {
  7521. return animationKeywords.has(nonQuotedName)
  7522. ? original
  7523. : `${leadingSpaces}${this._scopeAnimationKeyframe(nonQuotedName, scopeSelector, unscopedKeyframesSet)}`;
  7524. }
  7525. }));
  7526. content = content.replace(/((?:^|\s+|;)(?:-webkit-)?animation-name(?:\s*):(?:\s*))([^;]+)/g, (_match, start, commaSeparatedKeyframes) => `${start}${commaSeparatedKeyframes
  7527. .split(',')
  7528. .map((keyframe) => this._scopeAnimationKeyframe(keyframe, scopeSelector, unscopedKeyframesSet))
  7529. .join(',')}`);
  7530. return { ...rule, content };
  7531. }
  7532. /*
  7533. * Process styles to convert native ShadowDOM rules that will trip
  7534. * up the css parser; we rely on decorating the stylesheet with inert rules.
  7535. *
  7536. * For example, we convert this rule:
  7537. *
  7538. * polyfill-next-selector { content: ':host menu-item'; }
  7539. * ::content menu-item {
  7540. *
  7541. * to this:
  7542. *
  7543. * scopeName menu-item {
  7544. *
  7545. **/
  7546. _insertPolyfillDirectivesInCssText(cssText) {
  7547. return cssText.replace(_cssContentNextSelectorRe, function (...m) {
  7548. return m[2] + '{';
  7549. });
  7550. }
  7551. /*
  7552. * Process styles to add rules which will only apply under the polyfill
  7553. *
  7554. * For example, we convert this rule:
  7555. *
  7556. * polyfill-rule {
  7557. * content: ':host menu-item';
  7558. * ...
  7559. * }
  7560. *
  7561. * to this:
  7562. *
  7563. * scopeName menu-item {...}
  7564. *
  7565. **/
  7566. _insertPolyfillRulesInCssText(cssText) {
  7567. return cssText.replace(_cssContentRuleRe, (...m) => {
  7568. const rule = m[0].replace(m[1], '').replace(m[2], '');
  7569. return m[4] + rule;
  7570. });
  7571. }
  7572. /* Ensure styles are scoped. Pseudo-scoping takes a rule like:
  7573. *
  7574. * .foo {... }
  7575. *
  7576. * and converts this to
  7577. *
  7578. * scopeName .foo { ... }
  7579. */
  7580. _scopeCssText(cssText, scopeSelector, hostSelector) {
  7581. const unscopedRules = this._extractUnscopedRulesFromCssText(cssText);
  7582. // replace :host and :host-context with -shadowcsshost and -shadowcsshostcontext respectively
  7583. cssText = this._insertPolyfillHostInCssText(cssText);
  7584. cssText = this._convertColonHost(cssText);
  7585. cssText = this._convertColonHostContext(cssText);
  7586. cssText = this._convertShadowDOMSelectors(cssText);
  7587. if (scopeSelector) {
  7588. cssText = this._scopeKeyframesRelatedCss(cssText, scopeSelector);
  7589. cssText = this._scopeSelectors(cssText, scopeSelector, hostSelector);
  7590. }
  7591. cssText = cssText + '\n' + unscopedRules;
  7592. return cssText.trim();
  7593. }
  7594. /*
  7595. * Process styles to add rules which will only apply under the polyfill
  7596. * and do not process via CSSOM. (CSSOM is destructive to rules on rare
  7597. * occasions, e.g. -webkit-calc on Safari.)
  7598. * For example, we convert this rule:
  7599. *
  7600. * @polyfill-unscoped-rule {
  7601. * content: 'menu-item';
  7602. * ... }
  7603. *
  7604. * to this:
  7605. *
  7606. * menu-item {...}
  7607. *
  7608. **/
  7609. _extractUnscopedRulesFromCssText(cssText) {
  7610. let r = '';
  7611. let m;
  7612. _cssContentUnscopedRuleRe.lastIndex = 0;
  7613. while ((m = _cssContentUnscopedRuleRe.exec(cssText)) !== null) {
  7614. const rule = m[0].replace(m[2], '').replace(m[1], m[4]);
  7615. r += rule + '\n\n';
  7616. }
  7617. return r;
  7618. }
  7619. /*
  7620. * convert a rule like :host(.foo) > .bar { }
  7621. *
  7622. * to
  7623. *
  7624. * .foo<scopeName> > .bar
  7625. */
  7626. _convertColonHost(cssText) {
  7627. return cssText.replace(_cssColonHostRe, (_, hostSelectors, otherSelectors) => {
  7628. if (hostSelectors) {
  7629. const convertedSelectors = [];
  7630. const hostSelectorArray = hostSelectors.split(',').map((p) => p.trim());
  7631. for (const hostSelector of hostSelectorArray) {
  7632. if (!hostSelector)
  7633. break;
  7634. const convertedSelector = _polyfillHostNoCombinator + hostSelector.replace(_polyfillHost, '') + otherSelectors;
  7635. convertedSelectors.push(convertedSelector);
  7636. }
  7637. return convertedSelectors.join(',');
  7638. }
  7639. else {
  7640. return _polyfillHostNoCombinator + otherSelectors;
  7641. }
  7642. });
  7643. }
  7644. /*
  7645. * convert a rule like :host-context(.foo) > .bar { }
  7646. *
  7647. * to
  7648. *
  7649. * .foo<scopeName> > .bar, .foo <scopeName> > .bar { }
  7650. *
  7651. * and
  7652. *
  7653. * :host-context(.foo:host) .bar { ... }
  7654. *
  7655. * to
  7656. *
  7657. * .foo<scopeName> .bar { ... }
  7658. */
  7659. _convertColonHostContext(cssText) {
  7660. const length = cssText.length;
  7661. let parens = 0;
  7662. let prev = 0;
  7663. let result = '';
  7664. // Splits up the selectors on their top-level commas, processes the :host-context in them
  7665. // individually and stitches them back together. This ensures that individual selectors don't
  7666. // affect each other.
  7667. for (let i = 0; i < length; i++) {
  7668. const char = cssText[i];
  7669. // If we hit a comma and there are no open parentheses, take the current chunk and process it.
  7670. if (char === ',' && parens === 0) {
  7671. result += this._convertColonHostContextInSelectorPart(cssText.slice(prev, i)) + ',';
  7672. prev = i + 1;
  7673. continue;
  7674. }
  7675. // We've hit the end. Take everything since the last comma.
  7676. if (i === length - 1) {
  7677. result += this._convertColonHostContextInSelectorPart(cssText.slice(prev));
  7678. break;
  7679. }
  7680. if (char === '(') {
  7681. parens++;
  7682. }
  7683. else if (char === ')') {
  7684. parens--;
  7685. }
  7686. }
  7687. return result;
  7688. }
  7689. _convertColonHostContextInSelectorPart(cssText) {
  7690. return cssText.replace(_cssColonHostContextReGlobal, (selectorText, pseudoPrefix) => {
  7691. // We have captured a selector that contains a `:host-context` rule.
  7692. // For backward compatibility `:host-context` may contain a comma separated list of selectors.
  7693. // Each context selector group will contain a list of host-context selectors that must match
  7694. // an ancestor of the host.
  7695. // (Normally `contextSelectorGroups` will only contain a single array of context selectors.)
  7696. const contextSelectorGroups = [[]];
  7697. // There may be more than `:host-context` in this selector so `selectorText` could look like:
  7698. // `:host-context(.one):host-context(.two)`.
  7699. // Execute `_cssColonHostContextRe` over and over until we have extracted all the
  7700. // `:host-context` selectors from this selector.
  7701. let match;
  7702. while ((match = _cssColonHostContextRe.exec(selectorText))) {
  7703. // `match` = [':host-context(<selectors>)<rest>', <selectors>, <rest>]
  7704. // The `<selectors>` could actually be a comma separated list: `:host-context(.one, .two)`.
  7705. const newContextSelectors = (match[1] ?? '')
  7706. .trim()
  7707. .split(',')
  7708. .map((m) => m.trim())
  7709. .filter((m) => m !== '');
  7710. // We must duplicate the current selector group for each of these new selectors.
  7711. // For example if the current groups are:
  7712. // ```
  7713. // [
  7714. // ['a', 'b', 'c'],
  7715. // ['x', 'y', 'z'],
  7716. // ]
  7717. // ```
  7718. // And we have a new set of comma separated selectors: `:host-context(m,n)` then the new
  7719. // groups are:
  7720. // ```
  7721. // [
  7722. // ['a', 'b', 'c', 'm'],
  7723. // ['x', 'y', 'z', 'm'],
  7724. // ['a', 'b', 'c', 'n'],
  7725. // ['x', 'y', 'z', 'n'],
  7726. // ]
  7727. // ```
  7728. const contextSelectorGroupsLength = contextSelectorGroups.length;
  7729. repeatGroups(contextSelectorGroups, newContextSelectors.length);
  7730. for (let i = 0; i < newContextSelectors.length; i++) {
  7731. for (let j = 0; j < contextSelectorGroupsLength; j++) {
  7732. contextSelectorGroups[j + i * contextSelectorGroupsLength].push(newContextSelectors[i]);
  7733. }
  7734. }
  7735. // Update the `selectorText` and see repeat to see if there are more `:host-context`s.
  7736. selectorText = match[2];
  7737. }
  7738. // The context selectors now must be combined with each other to capture all the possible
  7739. // selectors that `:host-context` can match. See `_combineHostContextSelectors()` for more
  7740. // info about how this is done.
  7741. return contextSelectorGroups
  7742. .map((contextSelectors) => _combineHostContextSelectors(contextSelectors, selectorText, pseudoPrefix))
  7743. .join(', ');
  7744. });
  7745. }
  7746. /*
  7747. * Convert combinators like ::shadow and pseudo-elements like ::content
  7748. * by replacing with space.
  7749. */
  7750. _convertShadowDOMSelectors(cssText) {
  7751. return _shadowDOMSelectorsRe.reduce((result, pattern) => result.replace(pattern, ' '), cssText);
  7752. }
  7753. // change a selector like 'div' to 'name div'
  7754. _scopeSelectors(cssText, scopeSelector, hostSelector) {
  7755. return processRules(cssText, (rule) => {
  7756. let selector = rule.selector;
  7757. let content = rule.content;
  7758. if (rule.selector[0] !== '@') {
  7759. selector = this._scopeSelector({
  7760. selector,
  7761. scopeSelector,
  7762. hostSelector,
  7763. isParentSelector: true,
  7764. });
  7765. }
  7766. else if (scopedAtRuleIdentifiers.some((atRule) => rule.selector.startsWith(atRule))) {
  7767. content = this._scopeSelectors(rule.content, scopeSelector, hostSelector);
  7768. }
  7769. else if (rule.selector.startsWith('@font-face') || rule.selector.startsWith('@page')) {
  7770. content = this._stripScopingSelectors(rule.content);
  7771. }
  7772. return new CssRule(selector, content);
  7773. });
  7774. }
  7775. /**
  7776. * Handle a css text that is within a rule that should not contain scope selectors by simply
  7777. * removing them! An example of such a rule is `@font-face`.
  7778. *
  7779. * `@font-face` rules cannot contain nested selectors. Nor can they be nested under a selector.
  7780. * Normally this would be a syntax error by the author of the styles. But in some rare cases, such
  7781. * as importing styles from a library, and applying `:host ::ng-deep` to the imported styles, we
  7782. * can end up with broken css if the imported styles happen to contain @font-face rules.
  7783. *
  7784. * For example:
  7785. *
  7786. * ```
  7787. * :host ::ng-deep {
  7788. * import 'some/lib/containing/font-face';
  7789. * }
  7790. *
  7791. * Similar logic applies to `@page` rules which can contain a particular set of properties,
  7792. * as well as some specific at-rules. Since they can't be encapsulated, we have to strip
  7793. * any scoping selectors from them. For more information: https://www.w3.org/TR/css-page-3
  7794. * ```
  7795. */
  7796. _stripScopingSelectors(cssText) {
  7797. return processRules(cssText, (rule) => {
  7798. const selector = rule.selector
  7799. .replace(_shadowDeepSelectors, ' ')
  7800. .replace(_polyfillHostNoCombinatorRe, ' ');
  7801. return new CssRule(selector, rule.content);
  7802. });
  7803. }
  7804. _safeSelector;
  7805. _shouldScopeIndicator;
  7806. // `isParentSelector` is used to distinguish the selectors which are coming from
  7807. // the initial selector string and any nested selectors, parsed recursively,
  7808. // for example `selector = 'a:where(.one)'` could be the parent, while recursive call
  7809. // would have `selector = '.one'`.
  7810. _scopeSelector({ selector, scopeSelector, hostSelector, isParentSelector = false, }) {
  7811. // Split the selector into independent parts by `,` (comma) unless
  7812. // comma is within parenthesis, for example `:is(.one, two)`.
  7813. // Negative lookup after comma allows not splitting inside nested parenthesis,
  7814. // up to three levels (((,))).
  7815. const selectorSplitRe = / ?,(?!(?:[^)(]*(?:\([^)(]*(?:\([^)(]*(?:\([^)(]*\)[^)(]*)*\)[^)(]*)*\)[^)(]*)*\))) ?/;
  7816. return selector
  7817. .split(selectorSplitRe)
  7818. .map((part) => part.split(_shadowDeepSelectors))
  7819. .map((deepParts) => {
  7820. const [shallowPart, ...otherParts] = deepParts;
  7821. const applyScope = (shallowPart) => {
  7822. if (this._selectorNeedsScoping(shallowPart, scopeSelector)) {
  7823. return this._applySelectorScope({
  7824. selector: shallowPart,
  7825. scopeSelector,
  7826. hostSelector,
  7827. isParentSelector,
  7828. });
  7829. }
  7830. else {
  7831. return shallowPart;
  7832. }
  7833. };
  7834. return [applyScope(shallowPart), ...otherParts].join(' ');
  7835. })
  7836. .join(', ');
  7837. }
  7838. _selectorNeedsScoping(selector, scopeSelector) {
  7839. const re = this._makeScopeMatcher(scopeSelector);
  7840. return !re.test(selector);
  7841. }
  7842. _makeScopeMatcher(scopeSelector) {
  7843. const lre = /\[/g;
  7844. const rre = /\]/g;
  7845. scopeSelector = scopeSelector.replace(lre, '\\[').replace(rre, '\\]');
  7846. return new RegExp('^(' + scopeSelector + ')' + _selectorReSuffix, 'm');
  7847. }
  7848. // scope via name and [is=name]
  7849. _applySimpleSelectorScope(selector, scopeSelector, hostSelector) {
  7850. // In Android browser, the lastIndex is not reset when the regex is used in String.replace()
  7851. _polyfillHostRe.lastIndex = 0;
  7852. if (_polyfillHostRe.test(selector)) {
  7853. const replaceBy = `[${hostSelector}]`;
  7854. let result = selector;
  7855. while (result.match(_polyfillHostNoCombinatorRe)) {
  7856. result = result.replace(_polyfillHostNoCombinatorRe, (_hnc, selector) => {
  7857. return selector.replace(/([^:\)]*)(:*)(.*)/, (_, before, colon, after) => {
  7858. return before + replaceBy + colon + after;
  7859. });
  7860. });
  7861. }
  7862. return result.replace(_polyfillHostRe, replaceBy);
  7863. }
  7864. return scopeSelector + ' ' + selector;
  7865. }
  7866. // return a selector with [name] suffix on each simple selector
  7867. // e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name] /** @internal */
  7868. _applySelectorScope({ selector, scopeSelector, hostSelector, isParentSelector, }) {
  7869. const isRe = /\[is=([^\]]*)\]/g;
  7870. scopeSelector = scopeSelector.replace(isRe, (_, ...parts) => parts[0]);
  7871. const attrName = `[${scopeSelector}]`;
  7872. const _scopeSelectorPart = (p) => {
  7873. let scopedP = p.trim();
  7874. if (!scopedP) {
  7875. return p;
  7876. }
  7877. if (p.includes(_polyfillHostNoCombinator)) {
  7878. scopedP = this._applySimpleSelectorScope(p, scopeSelector, hostSelector);
  7879. if (!p.match(_polyfillHostNoCombinatorOutsidePseudoFunction)) {
  7880. const [_, before, colon, after] = scopedP.match(/([^:]*)(:*)([\s\S]*)/);
  7881. scopedP = before + attrName + colon + after;
  7882. }
  7883. }
  7884. else {
  7885. // remove :host since it should be unnecessary
  7886. const t = p.replace(_polyfillHostRe, '');
  7887. if (t.length > 0) {
  7888. const matches = t.match(/([^:]*)(:*)([\s\S]*)/);
  7889. if (matches) {
  7890. scopedP = matches[1] + attrName + matches[2] + matches[3];
  7891. }
  7892. }
  7893. }
  7894. return scopedP;
  7895. };
  7896. // Wraps `_scopeSelectorPart()` to not use it directly on selectors with
  7897. // pseudo selector functions like `:where()`. Selectors within pseudo selector
  7898. // functions are recursively sent to `_scopeSelector()`.
  7899. const _pseudoFunctionAwareScopeSelectorPart = (selectorPart) => {
  7900. let scopedPart = '';
  7901. // Collect all outer `:where()` and `:is()` selectors,
  7902. // counting parenthesis to keep nested selectors intact.
  7903. const pseudoSelectorParts = [];
  7904. let pseudoSelectorMatch;
  7905. while ((pseudoSelectorMatch = _cssPrefixWithPseudoSelectorFunction.exec(selectorPart)) !== null) {
  7906. let openedBrackets = 1;
  7907. let index = _cssPrefixWithPseudoSelectorFunction.lastIndex;
  7908. while (index < selectorPart.length) {
  7909. const currentSymbol = selectorPart[index];
  7910. index++;
  7911. if (currentSymbol === '(') {
  7912. openedBrackets++;
  7913. continue;
  7914. }
  7915. if (currentSymbol === ')') {
  7916. openedBrackets--;
  7917. if (openedBrackets === 0) {
  7918. break;
  7919. }
  7920. continue;
  7921. }
  7922. }
  7923. pseudoSelectorParts.push(`${pseudoSelectorMatch[0]}${selectorPart.slice(_cssPrefixWithPseudoSelectorFunction.lastIndex, index)}`);
  7924. _cssPrefixWithPseudoSelectorFunction.lastIndex = index;
  7925. }
  7926. // If selector consists of only `:where()` and `:is()` on the outer level
  7927. // scope those pseudo-selectors individually, otherwise scope the whole
  7928. // selector.
  7929. if (pseudoSelectorParts.join('') === selectorPart) {
  7930. scopedPart = pseudoSelectorParts
  7931. .map((selectorPart) => {
  7932. const [cssPseudoSelectorFunction] = selectorPart.match(_cssPrefixWithPseudoSelectorFunction) ?? [];
  7933. // Unwrap the pseudo selector to scope its contents.
  7934. // For example,
  7935. // - `:where(selectorToScope)` -> `selectorToScope`;
  7936. // - `:is(.foo, .bar)` -> `.foo, .bar`.
  7937. const selectorToScope = selectorPart.slice(cssPseudoSelectorFunction?.length, -1);
  7938. if (selectorToScope.includes(_polyfillHostNoCombinator)) {
  7939. this._shouldScopeIndicator = true;
  7940. }
  7941. const scopedInnerPart = this._scopeSelector({
  7942. selector: selectorToScope,
  7943. scopeSelector,
  7944. hostSelector,
  7945. });
  7946. // Put the result back into the pseudo selector function.
  7947. return `${cssPseudoSelectorFunction}${scopedInnerPart})`;
  7948. })
  7949. .join('');
  7950. }
  7951. else {
  7952. this._shouldScopeIndicator =
  7953. this._shouldScopeIndicator || selectorPart.includes(_polyfillHostNoCombinator);
  7954. scopedPart = this._shouldScopeIndicator ? _scopeSelectorPart(selectorPart) : selectorPart;
  7955. }
  7956. return scopedPart;
  7957. };
  7958. if (isParentSelector) {
  7959. this._safeSelector = new SafeSelector(selector);
  7960. selector = this._safeSelector.content();
  7961. }
  7962. let scopedSelector = '';
  7963. let startIndex = 0;
  7964. let res;
  7965. // Combinators aren't used as a delimiter if they are within parenthesis,
  7966. // for example `:where(.one .two)` stays intact.
  7967. // Similarly to selector separation by comma initially, negative lookahead
  7968. // is used here to not break selectors within nested parenthesis up to three
  7969. // nested layers.
  7970. const sep = /( |>|\+|~(?!=))(?!([^)(]*(?:\([^)(]*(?:\([^)(]*(?:\([^)(]*\)[^)(]*)*\)[^)(]*)*\)[^)(]*)*\)))\s*/g;
  7971. // If a selector appears before :host it should not be shimmed as it
  7972. // matches on ancestor elements and not on elements in the host's shadow
  7973. // `:host-context(div)` is transformed to
  7974. // `-shadowcsshost-no-combinatordiv, div -shadowcsshost-no-combinator`
  7975. // the `div` is not part of the component in the 2nd selectors and should not be scoped.
  7976. // Historically `component-tag:host` was matching the component so we also want to preserve
  7977. // this behavior to avoid breaking legacy apps (it should not match).
  7978. // The behavior should be:
  7979. // - `tag:host` -> `tag[h]` (this is to avoid breaking legacy apps, should not match anything)
  7980. // - `tag :host` -> `tag [h]` (`tag` is not scoped because it's considered part of a
  7981. // `:host-context(tag)`)
  7982. const hasHost = selector.includes(_polyfillHostNoCombinator);
  7983. // Only scope parts after or on the same level as the first `-shadowcsshost-no-combinator`
  7984. // when it is present. The selector has the same level when it is a part of a pseudo
  7985. // selector, like `:where()`, for example `:where(:host, .foo)` would result in `.foo`
  7986. // being scoped.
  7987. if (isParentSelector || this._shouldScopeIndicator) {
  7988. this._shouldScopeIndicator = !hasHost;
  7989. }
  7990. while ((res = sep.exec(selector)) !== null) {
  7991. const separator = res[1];
  7992. // Do not trim the selector, as otherwise this will break sourcemaps
  7993. // when they are defined on multiple lines
  7994. // Example:
  7995. // div,
  7996. // p { color: red}
  7997. const part = selector.slice(startIndex, res.index);
  7998. // A space following an escaped hex value and followed by another hex character
  7999. // (ie: ".\fc ber" for ".über") is not a separator between 2 selectors
  8000. // also keep in mind that backslashes are replaced by a placeholder by SafeSelector
  8001. // These escaped selectors happen for example when esbuild runs with optimization.minify.
  8002. if (part.match(/__esc-ph-(\d+)__/) && selector[res.index + 1]?.match(/[a-fA-F\d]/)) {
  8003. continue;
  8004. }
  8005. const scopedPart = _pseudoFunctionAwareScopeSelectorPart(part);
  8006. scopedSelector += `${scopedPart} ${separator} `;
  8007. startIndex = sep.lastIndex;
  8008. }
  8009. const part = selector.substring(startIndex);
  8010. scopedSelector += _pseudoFunctionAwareScopeSelectorPart(part);
  8011. // replace the placeholders with their original values
  8012. // using values stored inside the `safeSelector` instance.
  8013. return this._safeSelector.restore(scopedSelector);
  8014. }
  8015. _insertPolyfillHostInCssText(selector) {
  8016. return selector
  8017. .replace(_colonHostContextRe, _polyfillHostContext)
  8018. .replace(_colonHostRe, _polyfillHost);
  8019. }
  8020. }
  8021. class SafeSelector {
  8022. placeholders = [];
  8023. index = 0;
  8024. _content;
  8025. constructor(selector) {
  8026. // Replaces attribute selectors with placeholders.
  8027. // The WS in [attr="va lue"] would otherwise be interpreted as a selector separator.
  8028. selector = this._escapeRegexMatches(selector, /(\[[^\]]*\])/g);
  8029. // CSS allows for certain special characters to be used in selectors if they're escaped.
  8030. // E.g. `.foo:blue` won't match a class called `foo:blue`, because the colon denotes a
  8031. // pseudo-class, but writing `.foo\:blue` will match, because the colon was escaped.
  8032. // Replace all escape sequences (`\` followed by a character) with a placeholder so
  8033. // that our handling of pseudo-selectors doesn't mess with them.
  8034. // Escaped characters have a specific placeholder so they can be detected separately.
  8035. selector = selector.replace(/(\\.)/g, (_, keep) => {
  8036. const replaceBy = `__esc-ph-${this.index}__`;
  8037. this.placeholders.push(keep);
  8038. this.index++;
  8039. return replaceBy;
  8040. });
  8041. // Replaces the expression in `:nth-child(2n + 1)` with a placeholder.
  8042. // WS and "+" would otherwise be interpreted as selector separators.
  8043. this._content = selector.replace(/(:nth-[-\w]+)(\([^)]+\))/g, (_, pseudo, exp) => {
  8044. const replaceBy = `__ph-${this.index}__`;
  8045. this.placeholders.push(exp);
  8046. this.index++;
  8047. return pseudo + replaceBy;
  8048. });
  8049. }
  8050. restore(content) {
  8051. return content.replace(/__(?:ph|esc-ph)-(\d+)__/g, (_ph, index) => this.placeholders[+index]);
  8052. }
  8053. content() {
  8054. return this._content;
  8055. }
  8056. /**
  8057. * Replaces all of the substrings that match a regex within a
  8058. * special string (e.g. `__ph-0__`, `__ph-1__`, etc).
  8059. */
  8060. _escapeRegexMatches(content, pattern) {
  8061. return content.replace(pattern, (_, keep) => {
  8062. const replaceBy = `__ph-${this.index}__`;
  8063. this.placeholders.push(keep);
  8064. this.index++;
  8065. return replaceBy;
  8066. });
  8067. }
  8068. }
  8069. const _cssScopedPseudoFunctionPrefix = '(:(where|is)\\()?';
  8070. const _cssPrefixWithPseudoSelectorFunction = /:(where|is)\(/gi;
  8071. const _cssContentNextSelectorRe = /polyfill-next-selector[^}]*content:[\s]*?(['"])(.*?)\1[;\s]*}([^{]*?){/gim;
  8072. const _cssContentRuleRe = /(polyfill-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  8073. const _cssContentUnscopedRuleRe = /(polyfill-unscoped-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  8074. const _polyfillHost = '-shadowcsshost';
  8075. // note: :host-context pre-processed to -shadowcsshostcontext.
  8076. const _polyfillHostContext = '-shadowcsscontext';
  8077. const _parenSuffix = '(?:\\((' + '(?:\\([^)(]*\\)|[^)(]*)+?' + ')\\))';
  8078. const _cssColonHostRe = new RegExp(_polyfillHost + _parenSuffix + '?([^,{]*)', 'gim');
  8079. // note: :host-context patterns are terminated with `{`, as opposed to :host which
  8080. // is both `{` and `,` because :host-context handles top-level commas differently.
  8081. const _hostContextPattern = _polyfillHostContext + _parenSuffix + '?([^{]*)';
  8082. const _cssColonHostContextReGlobal = new RegExp(`${_cssScopedPseudoFunctionPrefix}(${_hostContextPattern})`, 'gim');
  8083. const _cssColonHostContextRe = new RegExp(_hostContextPattern, 'im');
  8084. const _polyfillHostNoCombinator = _polyfillHost + '-no-combinator';
  8085. const _polyfillHostNoCombinatorOutsidePseudoFunction = new RegExp(`${_polyfillHostNoCombinator}(?![^(]*\\))`, 'g');
  8086. const _polyfillHostNoCombinatorRe = /-shadowcsshost-no-combinator([^\s,]*)/;
  8087. const _shadowDOMSelectorsRe = [
  8088. /::shadow/g,
  8089. /::content/g,
  8090. // Deprecated selectors
  8091. /\/shadow-deep\//g,
  8092. /\/shadow\//g,
  8093. ];
  8094. // The deep combinator is deprecated in the CSS spec
  8095. // Support for `>>>`, `deep`, `::ng-deep` is then also deprecated and will be removed in the future.
  8096. // see https://github.com/angular/angular/pull/17677
  8097. const _shadowDeepSelectors = /(?:>>>)|(?:\/deep\/)|(?:::ng-deep)/g;
  8098. const _selectorReSuffix = '([>\\s~+[.,{:][\\s\\S]*)?$';
  8099. const _polyfillHostRe = /-shadowcsshost/gim;
  8100. const _colonHostRe = /:host/gim;
  8101. const _colonHostContextRe = /:host-context/gim;
  8102. const _newLinesRe = /\r?\n/g;
  8103. const _commentRe = /\/\*[\s\S]*?\*\//g;
  8104. const _commentWithHashRe = /\/\*\s*#\s*source(Mapping)?URL=/g;
  8105. const COMMENT_PLACEHOLDER = '%COMMENT%';
  8106. const _commentWithHashPlaceHolderRe = new RegExp(COMMENT_PLACEHOLDER, 'g');
  8107. const BLOCK_PLACEHOLDER = '%BLOCK%';
  8108. const _ruleRe = new RegExp(`(\\s*(?:${COMMENT_PLACEHOLDER}\\s*)*)([^;\\{\\}]+?)(\\s*)((?:{%BLOCK%}?\\s*;?)|(?:\\s*;))`, 'g');
  8109. const CONTENT_PAIRS = new Map([['{', '}']]);
  8110. const COMMA_IN_PLACEHOLDER = '%COMMA_IN_PLACEHOLDER%';
  8111. const SEMI_IN_PLACEHOLDER = '%SEMI_IN_PLACEHOLDER%';
  8112. const COLON_IN_PLACEHOLDER = '%COLON_IN_PLACEHOLDER%';
  8113. const _cssCommaInPlaceholderReGlobal = new RegExp(COMMA_IN_PLACEHOLDER, 'g');
  8114. const _cssSemiInPlaceholderReGlobal = new RegExp(SEMI_IN_PLACEHOLDER, 'g');
  8115. const _cssColonInPlaceholderReGlobal = new RegExp(COLON_IN_PLACEHOLDER, 'g');
  8116. class CssRule {
  8117. selector;
  8118. content;
  8119. constructor(selector, content) {
  8120. this.selector = selector;
  8121. this.content = content;
  8122. }
  8123. }
  8124. function processRules(input, ruleCallback) {
  8125. const escaped = escapeInStrings(input);
  8126. const inputWithEscapedBlocks = escapeBlocks(escaped, CONTENT_PAIRS, BLOCK_PLACEHOLDER);
  8127. let nextBlockIndex = 0;
  8128. const escapedResult = inputWithEscapedBlocks.escapedString.replace(_ruleRe, (...m) => {
  8129. const selector = m[2];
  8130. let content = '';
  8131. let suffix = m[4];
  8132. let contentPrefix = '';
  8133. if (suffix && suffix.startsWith('{' + BLOCK_PLACEHOLDER)) {
  8134. content = inputWithEscapedBlocks.blocks[nextBlockIndex++];
  8135. suffix = suffix.substring(BLOCK_PLACEHOLDER.length + 1);
  8136. contentPrefix = '{';
  8137. }
  8138. const rule = ruleCallback(new CssRule(selector, content));
  8139. return `${m[1]}${rule.selector}${m[3]}${contentPrefix}${rule.content}${suffix}`;
  8140. });
  8141. return unescapeInStrings(escapedResult);
  8142. }
  8143. class StringWithEscapedBlocks {
  8144. escapedString;
  8145. blocks;
  8146. constructor(escapedString, blocks) {
  8147. this.escapedString = escapedString;
  8148. this.blocks = blocks;
  8149. }
  8150. }
  8151. function escapeBlocks(input, charPairs, placeholder) {
  8152. const resultParts = [];
  8153. const escapedBlocks = [];
  8154. let openCharCount = 0;
  8155. let nonBlockStartIndex = 0;
  8156. let blockStartIndex = -1;
  8157. let openChar;
  8158. let closeChar;
  8159. for (let i = 0; i < input.length; i++) {
  8160. const char = input[i];
  8161. if (char === '\\') {
  8162. i++;
  8163. }
  8164. else if (char === closeChar) {
  8165. openCharCount--;
  8166. if (openCharCount === 0) {
  8167. escapedBlocks.push(input.substring(blockStartIndex, i));
  8168. resultParts.push(placeholder);
  8169. nonBlockStartIndex = i;
  8170. blockStartIndex = -1;
  8171. openChar = closeChar = undefined;
  8172. }
  8173. }
  8174. else if (char === openChar) {
  8175. openCharCount++;
  8176. }
  8177. else if (openCharCount === 0 && charPairs.has(char)) {
  8178. openChar = char;
  8179. closeChar = charPairs.get(char);
  8180. openCharCount = 1;
  8181. blockStartIndex = i + 1;
  8182. resultParts.push(input.substring(nonBlockStartIndex, blockStartIndex));
  8183. }
  8184. }
  8185. if (blockStartIndex !== -1) {
  8186. escapedBlocks.push(input.substring(blockStartIndex));
  8187. resultParts.push(placeholder);
  8188. }
  8189. else {
  8190. resultParts.push(input.substring(nonBlockStartIndex));
  8191. }
  8192. return new StringWithEscapedBlocks(resultParts.join(''), escapedBlocks);
  8193. }
  8194. /**
  8195. * Object containing as keys characters that should be substituted by placeholders
  8196. * when found in strings during the css text parsing, and as values the respective
  8197. * placeholders
  8198. */
  8199. const ESCAPE_IN_STRING_MAP = {
  8200. ';': SEMI_IN_PLACEHOLDER,
  8201. ',': COMMA_IN_PLACEHOLDER,
  8202. ':': COLON_IN_PLACEHOLDER,
  8203. };
  8204. /**
  8205. * Parse the provided css text and inside strings (meaning, inside pairs of unescaped single or
  8206. * double quotes) replace specific characters with their respective placeholders as indicated
  8207. * by the `ESCAPE_IN_STRING_MAP` map.
  8208. *
  8209. * For example convert the text
  8210. * `animation: "my-anim:at\"ion" 1s;`
  8211. * to
  8212. * `animation: "my-anim%COLON_IN_PLACEHOLDER%at\"ion" 1s;`
  8213. *
  8214. * This is necessary in order to remove the meaning of some characters when found inside strings
  8215. * (for example `;` indicates the end of a css declaration, `,` the sequence of values and `:` the
  8216. * division between property and value during a declaration, none of these meanings apply when such
  8217. * characters are within strings and so in order to prevent parsing issues they need to be replaced
  8218. * with placeholder text for the duration of the css manipulation process).
  8219. *
  8220. * @param input the original css text.
  8221. *
  8222. * @returns the css text with specific characters in strings replaced by placeholders.
  8223. **/
  8224. function escapeInStrings(input) {
  8225. let result = input;
  8226. let currentQuoteChar = null;
  8227. for (let i = 0; i < result.length; i++) {
  8228. const char = result[i];
  8229. if (char === '\\') {
  8230. i++;
  8231. }
  8232. else {
  8233. if (currentQuoteChar !== null) {
  8234. // index i is inside a quoted sub-string
  8235. if (char === currentQuoteChar) {
  8236. currentQuoteChar = null;
  8237. }
  8238. else {
  8239. const placeholder = ESCAPE_IN_STRING_MAP[char];
  8240. if (placeholder) {
  8241. result = `${result.substr(0, i)}${placeholder}${result.substr(i + 1)}`;
  8242. i += placeholder.length - 1;
  8243. }
  8244. }
  8245. }
  8246. else if (char === "'" || char === '"') {
  8247. currentQuoteChar = char;
  8248. }
  8249. }
  8250. }
  8251. return result;
  8252. }
  8253. /**
  8254. * Replace in a string all occurrences of keys in the `ESCAPE_IN_STRING_MAP` map with their
  8255. * original representation, this is simply used to revert the changes applied by the
  8256. * escapeInStrings function.
  8257. *
  8258. * For example it reverts the text:
  8259. * `animation: "my-anim%COLON_IN_PLACEHOLDER%at\"ion" 1s;`
  8260. * to it's original form of:
  8261. * `animation: "my-anim:at\"ion" 1s;`
  8262. *
  8263. * Note: For the sake of simplicity this function does not check that the placeholders are
  8264. * actually inside strings as it would anyway be extremely unlikely to find them outside of strings.
  8265. *
  8266. * @param input the css text containing the placeholders.
  8267. *
  8268. * @returns the css text without the placeholders.
  8269. */
  8270. function unescapeInStrings(input) {
  8271. let result = input.replace(_cssCommaInPlaceholderReGlobal, ',');
  8272. result = result.replace(_cssSemiInPlaceholderReGlobal, ';');
  8273. result = result.replace(_cssColonInPlaceholderReGlobal, ':');
  8274. return result;
  8275. }
  8276. /**
  8277. * Unescape all quotes present in a string, but only if the string was actually already
  8278. * quoted.
  8279. *
  8280. * This generates a "canonical" representation of strings which can be used to match strings
  8281. * which would otherwise only differ because of differently escaped quotes.
  8282. *
  8283. * For example it converts the string (assumed to be quoted):
  8284. * `this \\"is\\" a \\'\\\\'test`
  8285. * to:
  8286. * `this "is" a '\\\\'test`
  8287. * (note that the latter backslashes are not removed as they are not actually escaping the single
  8288. * quote)
  8289. *
  8290. *
  8291. * @param input the string possibly containing escaped quotes.
  8292. * @param isQuoted boolean indicating whether the string was quoted inside a bigger string (if not
  8293. * then it means that it doesn't represent an inner string and thus no unescaping is required)
  8294. *
  8295. * @returns the string in the "canonical" representation without escaped quotes.
  8296. */
  8297. function unescapeQuotes(str, isQuoted) {
  8298. return !isQuoted ? str : str.replace(/((?:^|[^\\])(?:\\\\)*)\\(?=['"])/g, '$1');
  8299. }
  8300. /**
  8301. * Combine the `contextSelectors` with the `hostMarker` and the `otherSelectors`
  8302. * to create a selector that matches the same as `:host-context()`.
  8303. *
  8304. * Given a single context selector `A` we need to output selectors that match on the host and as an
  8305. * ancestor of the host:
  8306. *
  8307. * ```
  8308. * A <hostMarker>, A<hostMarker> {}
  8309. * ```
  8310. *
  8311. * When there is more than one context selector we also have to create combinations of those
  8312. * selectors with each other. For example if there are `A` and `B` selectors the output is:
  8313. *
  8314. * ```
  8315. * AB<hostMarker>, AB <hostMarker>, A B<hostMarker>,
  8316. * B A<hostMarker>, A B <hostMarker>, B A <hostMarker> {}
  8317. * ```
  8318. *
  8319. * And so on...
  8320. *
  8321. * @param contextSelectors an array of context selectors that will be combined.
  8322. * @param otherSelectors the rest of the selectors that are not context selectors.
  8323. */
  8324. function _combineHostContextSelectors(contextSelectors, otherSelectors, pseudoPrefix = '') {
  8325. const hostMarker = _polyfillHostNoCombinator;
  8326. _polyfillHostRe.lastIndex = 0; // reset the regex to ensure we get an accurate test
  8327. const otherSelectorsHasHost = _polyfillHostRe.test(otherSelectors);
  8328. // If there are no context selectors then just output a host marker
  8329. if (contextSelectors.length === 0) {
  8330. return hostMarker + otherSelectors;
  8331. }
  8332. const combined = [contextSelectors.pop() || ''];
  8333. while (contextSelectors.length > 0) {
  8334. const length = combined.length;
  8335. const contextSelector = contextSelectors.pop();
  8336. for (let i = 0; i < length; i++) {
  8337. const previousSelectors = combined[i];
  8338. // Add the new selector as a descendant of the previous selectors
  8339. combined[length * 2 + i] = previousSelectors + ' ' + contextSelector;
  8340. // Add the new selector as an ancestor of the previous selectors
  8341. combined[length + i] = contextSelector + ' ' + previousSelectors;
  8342. // Add the new selector to act on the same element as the previous selectors
  8343. combined[i] = contextSelector + previousSelectors;
  8344. }
  8345. }
  8346. // Finally connect the selector to the `hostMarker`s: either acting directly on the host
  8347. // (A<hostMarker>) or as an ancestor (A <hostMarker>).
  8348. return combined
  8349. .map((s) => otherSelectorsHasHost
  8350. ? `${pseudoPrefix}${s}${otherSelectors}`
  8351. : `${pseudoPrefix}${s}${hostMarker}${otherSelectors}, ${pseudoPrefix}${s} ${hostMarker}${otherSelectors}`)
  8352. .join(',');
  8353. }
  8354. /**
  8355. * Mutate the given `groups` array so that there are `multiples` clones of the original array
  8356. * stored.
  8357. *
  8358. * For example `repeatGroups([a, b], 3)` will result in `[a, b, a, b, a, b]` - but importantly the
  8359. * newly added groups will be clones of the original.
  8360. *
  8361. * @param groups An array of groups of strings that will be repeated. This array is mutated
  8362. * in-place.
  8363. * @param multiples The number of times the current groups should appear.
  8364. */
  8365. function repeatGroups(groups, multiples) {
  8366. const length = groups.length;
  8367. for (let i = 1; i < multiples; i++) {
  8368. for (let j = 0; j < length; j++) {
  8369. groups[j + i * length] = groups[j].slice(0);
  8370. }
  8371. }
  8372. }
  8373. /**
  8374. * Distinguishes different kinds of IR operations.
  8375. *
  8376. * Includes both creation and update operations.
  8377. */
  8378. var OpKind;
  8379. (function (OpKind) {
  8380. /**
  8381. * A special operation type which is used to represent the beginning and end nodes of a linked
  8382. * list of operations.
  8383. */
  8384. OpKind[OpKind["ListEnd"] = 0] = "ListEnd";
  8385. /**
  8386. * An operation which wraps an output AST statement.
  8387. */
  8388. OpKind[OpKind["Statement"] = 1] = "Statement";
  8389. /**
  8390. * An operation which declares and initializes a `SemanticVariable`.
  8391. */
  8392. OpKind[OpKind["Variable"] = 2] = "Variable";
  8393. /**
  8394. * An operation to begin rendering of an element.
  8395. */
  8396. OpKind[OpKind["ElementStart"] = 3] = "ElementStart";
  8397. /**
  8398. * An operation to render an element with no children.
  8399. */
  8400. OpKind[OpKind["Element"] = 4] = "Element";
  8401. /**
  8402. * An operation which declares an embedded view.
  8403. */
  8404. OpKind[OpKind["Template"] = 5] = "Template";
  8405. /**
  8406. * An operation to end rendering of an element previously started with `ElementStart`.
  8407. */
  8408. OpKind[OpKind["ElementEnd"] = 6] = "ElementEnd";
  8409. /**
  8410. * An operation to begin an `ng-container`.
  8411. */
  8412. OpKind[OpKind["ContainerStart"] = 7] = "ContainerStart";
  8413. /**
  8414. * An operation for an `ng-container` with no children.
  8415. */
  8416. OpKind[OpKind["Container"] = 8] = "Container";
  8417. /**
  8418. * An operation to end an `ng-container`.
  8419. */
  8420. OpKind[OpKind["ContainerEnd"] = 9] = "ContainerEnd";
  8421. /**
  8422. * An operation disable binding for subsequent elements, which are descendants of a non-bindable
  8423. * node.
  8424. */
  8425. OpKind[OpKind["DisableBindings"] = 10] = "DisableBindings";
  8426. /**
  8427. * An op to conditionally render a template.
  8428. */
  8429. OpKind[OpKind["Conditional"] = 11] = "Conditional";
  8430. /**
  8431. * An operation to re-enable binding, after it was previously disabled.
  8432. */
  8433. OpKind[OpKind["EnableBindings"] = 12] = "EnableBindings";
  8434. /**
  8435. * An operation to render a text node.
  8436. */
  8437. OpKind[OpKind["Text"] = 13] = "Text";
  8438. /**
  8439. * An operation declaring an event listener for an element.
  8440. */
  8441. OpKind[OpKind["Listener"] = 14] = "Listener";
  8442. /**
  8443. * An operation to interpolate text into a text node.
  8444. */
  8445. OpKind[OpKind["InterpolateText"] = 15] = "InterpolateText";
  8446. /**
  8447. * An intermediate binding op, that has not yet been processed into an individual property,
  8448. * attribute, style, etc.
  8449. */
  8450. OpKind[OpKind["Binding"] = 16] = "Binding";
  8451. /**
  8452. * An operation to bind an expression to a property of an element.
  8453. */
  8454. OpKind[OpKind["Property"] = 17] = "Property";
  8455. /**
  8456. * An operation to bind an expression to a style property of an element.
  8457. */
  8458. OpKind[OpKind["StyleProp"] = 18] = "StyleProp";
  8459. /**
  8460. * An operation to bind an expression to a class property of an element.
  8461. */
  8462. OpKind[OpKind["ClassProp"] = 19] = "ClassProp";
  8463. /**
  8464. * An operation to bind an expression to the styles of an element.
  8465. */
  8466. OpKind[OpKind["StyleMap"] = 20] = "StyleMap";
  8467. /**
  8468. * An operation to bind an expression to the classes of an element.
  8469. */
  8470. OpKind[OpKind["ClassMap"] = 21] = "ClassMap";
  8471. /**
  8472. * An operation to advance the runtime's implicit slot context during the update phase of a view.
  8473. */
  8474. OpKind[OpKind["Advance"] = 22] = "Advance";
  8475. /**
  8476. * An operation to instantiate a pipe.
  8477. */
  8478. OpKind[OpKind["Pipe"] = 23] = "Pipe";
  8479. /**
  8480. * An operation to associate an attribute with an element.
  8481. */
  8482. OpKind[OpKind["Attribute"] = 24] = "Attribute";
  8483. /**
  8484. * An attribute that has been extracted for inclusion in the consts array.
  8485. */
  8486. OpKind[OpKind["ExtractedAttribute"] = 25] = "ExtractedAttribute";
  8487. /**
  8488. * An operation that configures a `@defer` block.
  8489. */
  8490. OpKind[OpKind["Defer"] = 26] = "Defer";
  8491. /**
  8492. * An operation that controls when a `@defer` loads.
  8493. */
  8494. OpKind[OpKind["DeferOn"] = 27] = "DeferOn";
  8495. /**
  8496. * An operation that controls when a `@defer` loads, using a custom expression as the condition.
  8497. */
  8498. OpKind[OpKind["DeferWhen"] = 28] = "DeferWhen";
  8499. /**
  8500. * An i18n message that has been extracted for inclusion in the consts array.
  8501. */
  8502. OpKind[OpKind["I18nMessage"] = 29] = "I18nMessage";
  8503. /**
  8504. * A host binding property.
  8505. */
  8506. OpKind[OpKind["HostProperty"] = 30] = "HostProperty";
  8507. /**
  8508. * A namespace change, which causes the subsequent elements to be processed as either HTML or SVG.
  8509. */
  8510. OpKind[OpKind["Namespace"] = 31] = "Namespace";
  8511. /**
  8512. * Configure a content projeciton definition for the view.
  8513. */
  8514. OpKind[OpKind["ProjectionDef"] = 32] = "ProjectionDef";
  8515. /**
  8516. * Create a content projection slot.
  8517. */
  8518. OpKind[OpKind["Projection"] = 33] = "Projection";
  8519. /**
  8520. * Create a repeater creation instruction op.
  8521. */
  8522. OpKind[OpKind["RepeaterCreate"] = 34] = "RepeaterCreate";
  8523. /**
  8524. * An update up for a repeater.
  8525. */
  8526. OpKind[OpKind["Repeater"] = 35] = "Repeater";
  8527. /**
  8528. * An operation to bind an expression to the property side of a two-way binding.
  8529. */
  8530. OpKind[OpKind["TwoWayProperty"] = 36] = "TwoWayProperty";
  8531. /**
  8532. * An operation declaring the event side of a two-way binding.
  8533. */
  8534. OpKind[OpKind["TwoWayListener"] = 37] = "TwoWayListener";
  8535. /**
  8536. * A creation-time operation that initializes the slot for a `@let` declaration.
  8537. */
  8538. OpKind[OpKind["DeclareLet"] = 38] = "DeclareLet";
  8539. /**
  8540. * An update-time operation that stores the current value of a `@let` declaration.
  8541. */
  8542. OpKind[OpKind["StoreLet"] = 39] = "StoreLet";
  8543. /**
  8544. * The start of an i18n block.
  8545. */
  8546. OpKind[OpKind["I18nStart"] = 40] = "I18nStart";
  8547. /**
  8548. * A self-closing i18n on a single element.
  8549. */
  8550. OpKind[OpKind["I18n"] = 41] = "I18n";
  8551. /**
  8552. * The end of an i18n block.
  8553. */
  8554. OpKind[OpKind["I18nEnd"] = 42] = "I18nEnd";
  8555. /**
  8556. * An expression in an i18n message.
  8557. */
  8558. OpKind[OpKind["I18nExpression"] = 43] = "I18nExpression";
  8559. /**
  8560. * An instruction that applies a set of i18n expressions.
  8561. */
  8562. OpKind[OpKind["I18nApply"] = 44] = "I18nApply";
  8563. /**
  8564. * An instruction to create an ICU expression.
  8565. */
  8566. OpKind[OpKind["IcuStart"] = 45] = "IcuStart";
  8567. /**
  8568. * An instruction to update an ICU expression.
  8569. */
  8570. OpKind[OpKind["IcuEnd"] = 46] = "IcuEnd";
  8571. /**
  8572. * An instruction representing a placeholder in an ICU expression.
  8573. */
  8574. OpKind[OpKind["IcuPlaceholder"] = 47] = "IcuPlaceholder";
  8575. /**
  8576. * An i18n context containing information needed to generate an i18n message.
  8577. */
  8578. OpKind[OpKind["I18nContext"] = 48] = "I18nContext";
  8579. /**
  8580. * A creation op that corresponds to i18n attributes on an element.
  8581. */
  8582. OpKind[OpKind["I18nAttributes"] = 49] = "I18nAttributes";
  8583. /**
  8584. * Creation op that attaches the location at which an element was defined in a template to it.
  8585. */
  8586. OpKind[OpKind["SourceLocation"] = 50] = "SourceLocation";
  8587. })(OpKind || (OpKind = {}));
  8588. /**
  8589. * Distinguishes different kinds of IR expressions.
  8590. */
  8591. var ExpressionKind;
  8592. (function (ExpressionKind) {
  8593. /**
  8594. * Read of a variable in a lexical scope.
  8595. */
  8596. ExpressionKind[ExpressionKind["LexicalRead"] = 0] = "LexicalRead";
  8597. /**
  8598. * A reference to the current view context.
  8599. */
  8600. ExpressionKind[ExpressionKind["Context"] = 1] = "Context";
  8601. /**
  8602. * A reference to the view context, for use inside a track function.
  8603. */
  8604. ExpressionKind[ExpressionKind["TrackContext"] = 2] = "TrackContext";
  8605. /**
  8606. * Read of a variable declared in a `VariableOp`.
  8607. */
  8608. ExpressionKind[ExpressionKind["ReadVariable"] = 3] = "ReadVariable";
  8609. /**
  8610. * Runtime operation to navigate to the next view context in the view hierarchy.
  8611. */
  8612. ExpressionKind[ExpressionKind["NextContext"] = 4] = "NextContext";
  8613. /**
  8614. * Runtime operation to retrieve the value of a local reference.
  8615. */
  8616. ExpressionKind[ExpressionKind["Reference"] = 5] = "Reference";
  8617. /**
  8618. * A call storing the value of a `@let` declaration.
  8619. */
  8620. ExpressionKind[ExpressionKind["StoreLet"] = 6] = "StoreLet";
  8621. /**
  8622. * A reference to a `@let` declaration read from the context view.
  8623. */
  8624. ExpressionKind[ExpressionKind["ContextLetReference"] = 7] = "ContextLetReference";
  8625. /**
  8626. * Runtime operation to snapshot the current view context.
  8627. */
  8628. ExpressionKind[ExpressionKind["GetCurrentView"] = 8] = "GetCurrentView";
  8629. /**
  8630. * Runtime operation to restore a snapshotted view.
  8631. */
  8632. ExpressionKind[ExpressionKind["RestoreView"] = 9] = "RestoreView";
  8633. /**
  8634. * Runtime operation to reset the current view context after `RestoreView`.
  8635. */
  8636. ExpressionKind[ExpressionKind["ResetView"] = 10] = "ResetView";
  8637. /**
  8638. * Defines and calls a function with change-detected arguments.
  8639. */
  8640. ExpressionKind[ExpressionKind["PureFunctionExpr"] = 11] = "PureFunctionExpr";
  8641. /**
  8642. * Indicates a positional parameter to a pure function definition.
  8643. */
  8644. ExpressionKind[ExpressionKind["PureFunctionParameterExpr"] = 12] = "PureFunctionParameterExpr";
  8645. /**
  8646. * Binding to a pipe transformation.
  8647. */
  8648. ExpressionKind[ExpressionKind["PipeBinding"] = 13] = "PipeBinding";
  8649. /**
  8650. * Binding to a pipe transformation with a variable number of arguments.
  8651. */
  8652. ExpressionKind[ExpressionKind["PipeBindingVariadic"] = 14] = "PipeBindingVariadic";
  8653. /*
  8654. * A safe property read requiring expansion into a null check.
  8655. */
  8656. ExpressionKind[ExpressionKind["SafePropertyRead"] = 15] = "SafePropertyRead";
  8657. /**
  8658. * A safe keyed read requiring expansion into a null check.
  8659. */
  8660. ExpressionKind[ExpressionKind["SafeKeyedRead"] = 16] = "SafeKeyedRead";
  8661. /**
  8662. * A safe function call requiring expansion into a null check.
  8663. */
  8664. ExpressionKind[ExpressionKind["SafeInvokeFunction"] = 17] = "SafeInvokeFunction";
  8665. /**
  8666. * An intermediate expression that will be expanded from a safe read into an explicit ternary.
  8667. */
  8668. ExpressionKind[ExpressionKind["SafeTernaryExpr"] = 18] = "SafeTernaryExpr";
  8669. /**
  8670. * An empty expression that will be stipped before generating the final output.
  8671. */
  8672. ExpressionKind[ExpressionKind["EmptyExpr"] = 19] = "EmptyExpr";
  8673. /*
  8674. * An assignment to a temporary variable.
  8675. */
  8676. ExpressionKind[ExpressionKind["AssignTemporaryExpr"] = 20] = "AssignTemporaryExpr";
  8677. /**
  8678. * A reference to a temporary variable.
  8679. */
  8680. ExpressionKind[ExpressionKind["ReadTemporaryExpr"] = 21] = "ReadTemporaryExpr";
  8681. /**
  8682. * An expression that will cause a literal slot index to be emitted.
  8683. */
  8684. ExpressionKind[ExpressionKind["SlotLiteralExpr"] = 22] = "SlotLiteralExpr";
  8685. /**
  8686. * A test expression for a conditional op.
  8687. */
  8688. ExpressionKind[ExpressionKind["ConditionalCase"] = 23] = "ConditionalCase";
  8689. /**
  8690. * An expression that will be automatically extracted to the component const array.
  8691. */
  8692. ExpressionKind[ExpressionKind["ConstCollected"] = 24] = "ConstCollected";
  8693. /**
  8694. * Operation that sets the value of a two-way binding.
  8695. */
  8696. ExpressionKind[ExpressionKind["TwoWayBindingSet"] = 25] = "TwoWayBindingSet";
  8697. })(ExpressionKind || (ExpressionKind = {}));
  8698. var VariableFlags;
  8699. (function (VariableFlags) {
  8700. VariableFlags[VariableFlags["None"] = 0] = "None";
  8701. /**
  8702. * Always inline this variable, regardless of the number of times it's used.
  8703. * An `AlwaysInline` variable may not depend on context, because doing so may cause side effects
  8704. * that are illegal when multi-inlined. (The optimizer will enforce this constraint.)
  8705. */
  8706. VariableFlags[VariableFlags["AlwaysInline"] = 1] = "AlwaysInline";
  8707. })(VariableFlags || (VariableFlags = {}));
  8708. /**
  8709. * Distinguishes between different kinds of `SemanticVariable`s.
  8710. */
  8711. var SemanticVariableKind;
  8712. (function (SemanticVariableKind) {
  8713. /**
  8714. * Represents the context of a particular view.
  8715. */
  8716. SemanticVariableKind[SemanticVariableKind["Context"] = 0] = "Context";
  8717. /**
  8718. * Represents an identifier declared in the lexical scope of a view.
  8719. */
  8720. SemanticVariableKind[SemanticVariableKind["Identifier"] = 1] = "Identifier";
  8721. /**
  8722. * Represents a saved state that can be used to restore a view in a listener handler function.
  8723. */
  8724. SemanticVariableKind[SemanticVariableKind["SavedView"] = 2] = "SavedView";
  8725. /**
  8726. * An alias generated by a special embedded view type (e.g. a `@for` block).
  8727. */
  8728. SemanticVariableKind[SemanticVariableKind["Alias"] = 3] = "Alias";
  8729. })(SemanticVariableKind || (SemanticVariableKind = {}));
  8730. /**
  8731. * Whether to compile in compatibilty mode. In compatibility mode, the template pipeline will
  8732. * attempt to match the output of `TemplateDefinitionBuilder` as exactly as possible, at the cost
  8733. * of producing quirky or larger code in some cases.
  8734. */
  8735. var CompatibilityMode;
  8736. (function (CompatibilityMode) {
  8737. CompatibilityMode[CompatibilityMode["Normal"] = 0] = "Normal";
  8738. CompatibilityMode[CompatibilityMode["TemplateDefinitionBuilder"] = 1] = "TemplateDefinitionBuilder";
  8739. })(CompatibilityMode || (CompatibilityMode = {}));
  8740. /**
  8741. * Enumeration of the types of attributes which can be applied to an element.
  8742. */
  8743. var BindingKind;
  8744. (function (BindingKind) {
  8745. /**
  8746. * Static attributes.
  8747. */
  8748. BindingKind[BindingKind["Attribute"] = 0] = "Attribute";
  8749. /**
  8750. * Class bindings.
  8751. */
  8752. BindingKind[BindingKind["ClassName"] = 1] = "ClassName";
  8753. /**
  8754. * Style bindings.
  8755. */
  8756. BindingKind[BindingKind["StyleProperty"] = 2] = "StyleProperty";
  8757. /**
  8758. * Dynamic property bindings.
  8759. */
  8760. BindingKind[BindingKind["Property"] = 3] = "Property";
  8761. /**
  8762. * Property or attribute bindings on a template.
  8763. */
  8764. BindingKind[BindingKind["Template"] = 4] = "Template";
  8765. /**
  8766. * Internationalized attributes.
  8767. */
  8768. BindingKind[BindingKind["I18n"] = 5] = "I18n";
  8769. /**
  8770. * Animation property bindings.
  8771. */
  8772. BindingKind[BindingKind["Animation"] = 6] = "Animation";
  8773. /**
  8774. * Property side of a two-way binding.
  8775. */
  8776. BindingKind[BindingKind["TwoWayProperty"] = 7] = "TwoWayProperty";
  8777. })(BindingKind || (BindingKind = {}));
  8778. /**
  8779. * Enumeration of possible times i18n params can be resolved.
  8780. */
  8781. var I18nParamResolutionTime;
  8782. (function (I18nParamResolutionTime) {
  8783. /**
  8784. * Param is resolved at message creation time. Most params should be resolved at message creation
  8785. * time. However, ICU params need to be handled in post-processing.
  8786. */
  8787. I18nParamResolutionTime[I18nParamResolutionTime["Creation"] = 0] = "Creation";
  8788. /**
  8789. * Param is resolved during post-processing. This should be used for params whose value comes from
  8790. * an ICU.
  8791. */
  8792. I18nParamResolutionTime[I18nParamResolutionTime["Postproccessing"] = 1] = "Postproccessing";
  8793. })(I18nParamResolutionTime || (I18nParamResolutionTime = {}));
  8794. /**
  8795. * The contexts in which an i18n expression can be used.
  8796. */
  8797. var I18nExpressionFor;
  8798. (function (I18nExpressionFor) {
  8799. /**
  8800. * This expression is used as a value (i.e. inside an i18n block).
  8801. */
  8802. I18nExpressionFor[I18nExpressionFor["I18nText"] = 0] = "I18nText";
  8803. /**
  8804. * This expression is used in a binding.
  8805. */
  8806. I18nExpressionFor[I18nExpressionFor["I18nAttribute"] = 1] = "I18nAttribute";
  8807. })(I18nExpressionFor || (I18nExpressionFor = {}));
  8808. /**
  8809. * Flags that describe what an i18n param value. These determine how the value is serialized into
  8810. * the final map.
  8811. */
  8812. var I18nParamValueFlags;
  8813. (function (I18nParamValueFlags) {
  8814. I18nParamValueFlags[I18nParamValueFlags["None"] = 0] = "None";
  8815. /**
  8816. * This value represents an element tag.
  8817. */
  8818. I18nParamValueFlags[I18nParamValueFlags["ElementTag"] = 1] = "ElementTag";
  8819. /**
  8820. * This value represents a template tag.
  8821. */
  8822. I18nParamValueFlags[I18nParamValueFlags["TemplateTag"] = 2] = "TemplateTag";
  8823. /**
  8824. * This value represents the opening of a tag.
  8825. */
  8826. I18nParamValueFlags[I18nParamValueFlags["OpenTag"] = 4] = "OpenTag";
  8827. /**
  8828. * This value represents the closing of a tag.
  8829. */
  8830. I18nParamValueFlags[I18nParamValueFlags["CloseTag"] = 8] = "CloseTag";
  8831. /**
  8832. * This value represents an i18n expression index.
  8833. */
  8834. I18nParamValueFlags[I18nParamValueFlags["ExpressionIndex"] = 16] = "ExpressionIndex";
  8835. })(I18nParamValueFlags || (I18nParamValueFlags = {}));
  8836. /**
  8837. * Whether the active namespace is HTML, MathML, or SVG mode.
  8838. */
  8839. var Namespace;
  8840. (function (Namespace) {
  8841. Namespace[Namespace["HTML"] = 0] = "HTML";
  8842. Namespace[Namespace["SVG"] = 1] = "SVG";
  8843. Namespace[Namespace["Math"] = 2] = "Math";
  8844. })(Namespace || (Namespace = {}));
  8845. /**
  8846. * The type of a `@defer` trigger, for use in the ir.
  8847. */
  8848. var DeferTriggerKind;
  8849. (function (DeferTriggerKind) {
  8850. DeferTriggerKind[DeferTriggerKind["Idle"] = 0] = "Idle";
  8851. DeferTriggerKind[DeferTriggerKind["Immediate"] = 1] = "Immediate";
  8852. DeferTriggerKind[DeferTriggerKind["Timer"] = 2] = "Timer";
  8853. DeferTriggerKind[DeferTriggerKind["Hover"] = 3] = "Hover";
  8854. DeferTriggerKind[DeferTriggerKind["Interaction"] = 4] = "Interaction";
  8855. DeferTriggerKind[DeferTriggerKind["Viewport"] = 5] = "Viewport";
  8856. DeferTriggerKind[DeferTriggerKind["Never"] = 6] = "Never";
  8857. })(DeferTriggerKind || (DeferTriggerKind = {}));
  8858. /**
  8859. * Kinds of i18n contexts. They can be created because of root i18n blocks, or ICUs.
  8860. */
  8861. var I18nContextKind;
  8862. (function (I18nContextKind) {
  8863. I18nContextKind[I18nContextKind["RootI18n"] = 0] = "RootI18n";
  8864. I18nContextKind[I18nContextKind["Icu"] = 1] = "Icu";
  8865. I18nContextKind[I18nContextKind["Attr"] = 2] = "Attr";
  8866. })(I18nContextKind || (I18nContextKind = {}));
  8867. var TemplateKind;
  8868. (function (TemplateKind) {
  8869. TemplateKind[TemplateKind["NgTemplate"] = 0] = "NgTemplate";
  8870. TemplateKind[TemplateKind["Structural"] = 1] = "Structural";
  8871. TemplateKind[TemplateKind["Block"] = 2] = "Block";
  8872. })(TemplateKind || (TemplateKind = {}));
  8873. /**
  8874. * Marker symbol for `ConsumesSlotOpTrait`.
  8875. */
  8876. const ConsumesSlot = Symbol('ConsumesSlot');
  8877. /**
  8878. * Marker symbol for `DependsOnSlotContextOpTrait`.
  8879. */
  8880. const DependsOnSlotContext = Symbol('DependsOnSlotContext');
  8881. /**
  8882. * Marker symbol for `ConsumesVars` trait.
  8883. */
  8884. const ConsumesVarsTrait = Symbol('ConsumesVars');
  8885. /**
  8886. * Marker symbol for `UsesVarOffset` trait.
  8887. */
  8888. const UsesVarOffset = Symbol('UsesVarOffset');
  8889. /**
  8890. * Default values for most `ConsumesSlotOpTrait` fields (used with the spread operator to initialize
  8891. * implementors of the trait).
  8892. */
  8893. const TRAIT_CONSUMES_SLOT = {
  8894. [ConsumesSlot]: true,
  8895. numSlotsUsed: 1,
  8896. };
  8897. /**
  8898. * Default values for most `DependsOnSlotContextOpTrait` fields (used with the spread operator to
  8899. * initialize implementors of the trait).
  8900. */
  8901. const TRAIT_DEPENDS_ON_SLOT_CONTEXT = {
  8902. [DependsOnSlotContext]: true,
  8903. };
  8904. /**
  8905. * Default values for `UsesVars` fields (used with the spread operator to initialize
  8906. * implementors of the trait).
  8907. */
  8908. const TRAIT_CONSUMES_VARS = {
  8909. [ConsumesVarsTrait]: true,
  8910. };
  8911. /**
  8912. * Test whether an operation implements `ConsumesSlotOpTrait`.
  8913. */
  8914. function hasConsumesSlotTrait(op) {
  8915. return op[ConsumesSlot] === true;
  8916. }
  8917. function hasDependsOnSlotContextTrait(value) {
  8918. return value[DependsOnSlotContext] === true;
  8919. }
  8920. function hasConsumesVarsTrait(value) {
  8921. return value[ConsumesVarsTrait] === true;
  8922. }
  8923. /**
  8924. * Test whether an expression implements `UsesVarOffsetTrait`.
  8925. */
  8926. function hasUsesVarOffsetTrait(expr) {
  8927. return expr[UsesVarOffset] === true;
  8928. }
  8929. /**
  8930. * Create a `StatementOp`.
  8931. */
  8932. function createStatementOp(statement) {
  8933. return {
  8934. kind: OpKind.Statement,
  8935. statement,
  8936. ...NEW_OP,
  8937. };
  8938. }
  8939. /**
  8940. * Create a `VariableOp`.
  8941. */
  8942. function createVariableOp(xref, variable, initializer, flags) {
  8943. return {
  8944. kind: OpKind.Variable,
  8945. xref,
  8946. variable,
  8947. initializer,
  8948. flags,
  8949. ...NEW_OP,
  8950. };
  8951. }
  8952. /**
  8953. * Static structure shared by all operations.
  8954. *
  8955. * Used as a convenience via the spread operator (`...NEW_OP`) when creating new operations, and
  8956. * ensures the fields are always in the same order.
  8957. */
  8958. const NEW_OP = {
  8959. debugListId: null,
  8960. prev: null,
  8961. next: null,
  8962. };
  8963. /**
  8964. * Create an `InterpolationTextOp`.
  8965. */
  8966. function createInterpolateTextOp(xref, interpolation, sourceSpan) {
  8967. return {
  8968. kind: OpKind.InterpolateText,
  8969. target: xref,
  8970. interpolation,
  8971. sourceSpan,
  8972. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  8973. ...TRAIT_CONSUMES_VARS,
  8974. ...NEW_OP,
  8975. };
  8976. }
  8977. class Interpolation {
  8978. strings;
  8979. expressions;
  8980. i18nPlaceholders;
  8981. constructor(strings, expressions, i18nPlaceholders) {
  8982. this.strings = strings;
  8983. this.expressions = expressions;
  8984. this.i18nPlaceholders = i18nPlaceholders;
  8985. if (i18nPlaceholders.length !== 0 && i18nPlaceholders.length !== expressions.length) {
  8986. throw new Error(`Expected ${expressions.length} placeholders to match interpolation expression count, but got ${i18nPlaceholders.length}`);
  8987. }
  8988. }
  8989. }
  8990. /**
  8991. * Create a `BindingOp`, not yet transformed into a particular type of binding.
  8992. */
  8993. function createBindingOp(target, kind, name, expression, unit, securityContext, isTextAttribute, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan) {
  8994. return {
  8995. kind: OpKind.Binding,
  8996. bindingKind: kind,
  8997. target,
  8998. name,
  8999. expression,
  9000. unit,
  9001. securityContext,
  9002. isTextAttribute,
  9003. isStructuralTemplateAttribute,
  9004. templateKind,
  9005. i18nContext: null,
  9006. i18nMessage,
  9007. sourceSpan,
  9008. ...NEW_OP,
  9009. };
  9010. }
  9011. /**
  9012. * Create a `PropertyOp`.
  9013. */
  9014. function createPropertyOp(target, name, expression, isAnimationTrigger, securityContext, isStructuralTemplateAttribute, templateKind, i18nContext, i18nMessage, sourceSpan) {
  9015. return {
  9016. kind: OpKind.Property,
  9017. target,
  9018. name,
  9019. expression,
  9020. isAnimationTrigger,
  9021. securityContext,
  9022. sanitizer: null,
  9023. isStructuralTemplateAttribute,
  9024. templateKind,
  9025. i18nContext,
  9026. i18nMessage,
  9027. sourceSpan,
  9028. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9029. ...TRAIT_CONSUMES_VARS,
  9030. ...NEW_OP,
  9031. };
  9032. }
  9033. /**
  9034. * Create a `TwoWayPropertyOp`.
  9035. */
  9036. function createTwoWayPropertyOp(target, name, expression, securityContext, isStructuralTemplateAttribute, templateKind, i18nContext, i18nMessage, sourceSpan) {
  9037. return {
  9038. kind: OpKind.TwoWayProperty,
  9039. target,
  9040. name,
  9041. expression,
  9042. securityContext,
  9043. sanitizer: null,
  9044. isStructuralTemplateAttribute,
  9045. templateKind,
  9046. i18nContext,
  9047. i18nMessage,
  9048. sourceSpan,
  9049. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9050. ...TRAIT_CONSUMES_VARS,
  9051. ...NEW_OP,
  9052. };
  9053. }
  9054. /** Create a `StylePropOp`. */
  9055. function createStylePropOp(xref, name, expression, unit, sourceSpan) {
  9056. return {
  9057. kind: OpKind.StyleProp,
  9058. target: xref,
  9059. name,
  9060. expression,
  9061. unit,
  9062. sourceSpan,
  9063. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9064. ...TRAIT_CONSUMES_VARS,
  9065. ...NEW_OP,
  9066. };
  9067. }
  9068. /**
  9069. * Create a `ClassPropOp`.
  9070. */
  9071. function createClassPropOp(xref, name, expression, sourceSpan) {
  9072. return {
  9073. kind: OpKind.ClassProp,
  9074. target: xref,
  9075. name,
  9076. expression,
  9077. sourceSpan,
  9078. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9079. ...TRAIT_CONSUMES_VARS,
  9080. ...NEW_OP,
  9081. };
  9082. }
  9083. /** Create a `StyleMapOp`. */
  9084. function createStyleMapOp(xref, expression, sourceSpan) {
  9085. return {
  9086. kind: OpKind.StyleMap,
  9087. target: xref,
  9088. expression,
  9089. sourceSpan,
  9090. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9091. ...TRAIT_CONSUMES_VARS,
  9092. ...NEW_OP,
  9093. };
  9094. }
  9095. /**
  9096. * Create a `ClassMapOp`.
  9097. */
  9098. function createClassMapOp(xref, expression, sourceSpan) {
  9099. return {
  9100. kind: OpKind.ClassMap,
  9101. target: xref,
  9102. expression,
  9103. sourceSpan,
  9104. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9105. ...TRAIT_CONSUMES_VARS,
  9106. ...NEW_OP,
  9107. };
  9108. }
  9109. /**
  9110. * Create an `AttributeOp`.
  9111. */
  9112. function createAttributeOp(target, namespace, name, expression, securityContext, isTextAttribute, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan) {
  9113. return {
  9114. kind: OpKind.Attribute,
  9115. target,
  9116. namespace,
  9117. name,
  9118. expression,
  9119. securityContext,
  9120. sanitizer: null,
  9121. isTextAttribute,
  9122. isStructuralTemplateAttribute,
  9123. templateKind,
  9124. i18nContext: null,
  9125. i18nMessage,
  9126. sourceSpan,
  9127. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9128. ...TRAIT_CONSUMES_VARS,
  9129. ...NEW_OP,
  9130. };
  9131. }
  9132. /**
  9133. * Create an `AdvanceOp`.
  9134. */
  9135. function createAdvanceOp(delta, sourceSpan) {
  9136. return {
  9137. kind: OpKind.Advance,
  9138. delta,
  9139. sourceSpan,
  9140. ...NEW_OP,
  9141. };
  9142. }
  9143. /**
  9144. * Create a conditional op, which will display an embedded view according to a condtion.
  9145. */
  9146. function createConditionalOp(target, test, conditions, sourceSpan) {
  9147. return {
  9148. kind: OpKind.Conditional,
  9149. target,
  9150. test,
  9151. conditions,
  9152. processed: null,
  9153. sourceSpan,
  9154. contextValue: null,
  9155. ...NEW_OP,
  9156. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9157. ...TRAIT_CONSUMES_VARS,
  9158. };
  9159. }
  9160. function createRepeaterOp(repeaterCreate, targetSlot, collection, sourceSpan) {
  9161. return {
  9162. kind: OpKind.Repeater,
  9163. target: repeaterCreate,
  9164. targetSlot,
  9165. collection,
  9166. sourceSpan,
  9167. ...NEW_OP,
  9168. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9169. };
  9170. }
  9171. function createDeferWhenOp(target, expr, modifier, sourceSpan) {
  9172. return {
  9173. kind: OpKind.DeferWhen,
  9174. target,
  9175. expr,
  9176. modifier,
  9177. sourceSpan,
  9178. ...NEW_OP,
  9179. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9180. ...TRAIT_CONSUMES_VARS,
  9181. };
  9182. }
  9183. /**
  9184. * Create an i18n expression op.
  9185. */
  9186. function createI18nExpressionOp(context, target, i18nOwner, handle, expression, icuPlaceholder, i18nPlaceholder, resolutionTime, usage, name, sourceSpan) {
  9187. return {
  9188. kind: OpKind.I18nExpression,
  9189. context,
  9190. target,
  9191. i18nOwner,
  9192. handle,
  9193. expression,
  9194. icuPlaceholder,
  9195. i18nPlaceholder,
  9196. resolutionTime,
  9197. usage,
  9198. name,
  9199. sourceSpan,
  9200. ...NEW_OP,
  9201. ...TRAIT_CONSUMES_VARS,
  9202. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9203. };
  9204. }
  9205. /**
  9206. * Creates an op to apply i18n expression ops.
  9207. */
  9208. function createI18nApplyOp(owner, handle, sourceSpan) {
  9209. return {
  9210. kind: OpKind.I18nApply,
  9211. owner,
  9212. handle,
  9213. sourceSpan,
  9214. ...NEW_OP,
  9215. };
  9216. }
  9217. /**
  9218. * Creates a `StoreLetOp`.
  9219. */
  9220. function createStoreLetOp(target, declaredName, value, sourceSpan) {
  9221. return {
  9222. kind: OpKind.StoreLet,
  9223. target,
  9224. declaredName,
  9225. value,
  9226. sourceSpan,
  9227. ...TRAIT_DEPENDS_ON_SLOT_CONTEXT,
  9228. ...TRAIT_CONSUMES_VARS,
  9229. ...NEW_OP,
  9230. };
  9231. }
  9232. /**
  9233. * Check whether a given `o.Expression` is a logical IR expression type.
  9234. */
  9235. function isIrExpression(expr) {
  9236. return expr instanceof ExpressionBase;
  9237. }
  9238. /**
  9239. * Base type used for all logical IR expressions.
  9240. */
  9241. class ExpressionBase extends Expression {
  9242. constructor(sourceSpan = null) {
  9243. super(null, sourceSpan);
  9244. }
  9245. }
  9246. /**
  9247. * Logical expression representing a lexical read of a variable name.
  9248. */
  9249. class LexicalReadExpr extends ExpressionBase {
  9250. name;
  9251. kind = ExpressionKind.LexicalRead;
  9252. constructor(name) {
  9253. super();
  9254. this.name = name;
  9255. }
  9256. visitExpression(visitor, context) { }
  9257. isEquivalent(other) {
  9258. // We assume that the lexical reads are in the same context, which must be true for parent
  9259. // expressions to be equivalent.
  9260. // TODO: is this generally safe?
  9261. return this.name === other.name;
  9262. }
  9263. isConstant() {
  9264. return false;
  9265. }
  9266. transformInternalExpressions() { }
  9267. clone() {
  9268. return new LexicalReadExpr(this.name);
  9269. }
  9270. }
  9271. /**
  9272. * Runtime operation to retrieve the value of a local reference.
  9273. */
  9274. class ReferenceExpr extends ExpressionBase {
  9275. target;
  9276. targetSlot;
  9277. offset;
  9278. kind = ExpressionKind.Reference;
  9279. constructor(target, targetSlot, offset) {
  9280. super();
  9281. this.target = target;
  9282. this.targetSlot = targetSlot;
  9283. this.offset = offset;
  9284. }
  9285. visitExpression() { }
  9286. isEquivalent(e) {
  9287. return e instanceof ReferenceExpr && e.target === this.target;
  9288. }
  9289. isConstant() {
  9290. return false;
  9291. }
  9292. transformInternalExpressions() { }
  9293. clone() {
  9294. return new ReferenceExpr(this.target, this.targetSlot, this.offset);
  9295. }
  9296. }
  9297. class StoreLetExpr extends ExpressionBase {
  9298. target;
  9299. value;
  9300. sourceSpan;
  9301. kind = ExpressionKind.StoreLet;
  9302. [ConsumesVarsTrait] = true;
  9303. [DependsOnSlotContext] = true;
  9304. constructor(target, value, sourceSpan) {
  9305. super();
  9306. this.target = target;
  9307. this.value = value;
  9308. this.sourceSpan = sourceSpan;
  9309. }
  9310. visitExpression() { }
  9311. isEquivalent(e) {
  9312. return (e instanceof StoreLetExpr && e.target === this.target && e.value.isEquivalent(this.value));
  9313. }
  9314. isConstant() {
  9315. return false;
  9316. }
  9317. transformInternalExpressions(transform, flags) {
  9318. this.value = transformExpressionsInExpression(this.value, transform, flags);
  9319. }
  9320. clone() {
  9321. return new StoreLetExpr(this.target, this.value, this.sourceSpan);
  9322. }
  9323. }
  9324. class ContextLetReferenceExpr extends ExpressionBase {
  9325. target;
  9326. targetSlot;
  9327. kind = ExpressionKind.ContextLetReference;
  9328. constructor(target, targetSlot) {
  9329. super();
  9330. this.target = target;
  9331. this.targetSlot = targetSlot;
  9332. }
  9333. visitExpression() { }
  9334. isEquivalent(e) {
  9335. return e instanceof ContextLetReferenceExpr && e.target === this.target;
  9336. }
  9337. isConstant() {
  9338. return false;
  9339. }
  9340. transformInternalExpressions() { }
  9341. clone() {
  9342. return new ContextLetReferenceExpr(this.target, this.targetSlot);
  9343. }
  9344. }
  9345. /**
  9346. * A reference to the current view context (usually the `ctx` variable in a template function).
  9347. */
  9348. class ContextExpr extends ExpressionBase {
  9349. view;
  9350. kind = ExpressionKind.Context;
  9351. constructor(view) {
  9352. super();
  9353. this.view = view;
  9354. }
  9355. visitExpression() { }
  9356. isEquivalent(e) {
  9357. return e instanceof ContextExpr && e.view === this.view;
  9358. }
  9359. isConstant() {
  9360. return false;
  9361. }
  9362. transformInternalExpressions() { }
  9363. clone() {
  9364. return new ContextExpr(this.view);
  9365. }
  9366. }
  9367. /**
  9368. * A reference to the current view context inside a track function.
  9369. */
  9370. class TrackContextExpr extends ExpressionBase {
  9371. view;
  9372. kind = ExpressionKind.TrackContext;
  9373. constructor(view) {
  9374. super();
  9375. this.view = view;
  9376. }
  9377. visitExpression() { }
  9378. isEquivalent(e) {
  9379. return e instanceof TrackContextExpr && e.view === this.view;
  9380. }
  9381. isConstant() {
  9382. return false;
  9383. }
  9384. transformInternalExpressions() { }
  9385. clone() {
  9386. return new TrackContextExpr(this.view);
  9387. }
  9388. }
  9389. /**
  9390. * Runtime operation to navigate to the next view context in the view hierarchy.
  9391. */
  9392. class NextContextExpr extends ExpressionBase {
  9393. kind = ExpressionKind.NextContext;
  9394. steps = 1;
  9395. constructor() {
  9396. super();
  9397. }
  9398. visitExpression() { }
  9399. isEquivalent(e) {
  9400. return e instanceof NextContextExpr && e.steps === this.steps;
  9401. }
  9402. isConstant() {
  9403. return false;
  9404. }
  9405. transformInternalExpressions() { }
  9406. clone() {
  9407. const expr = new NextContextExpr();
  9408. expr.steps = this.steps;
  9409. return expr;
  9410. }
  9411. }
  9412. /**
  9413. * Runtime operation to snapshot the current view context.
  9414. *
  9415. * The result of this operation can be stored in a variable and later used with the `RestoreView`
  9416. * operation.
  9417. */
  9418. class GetCurrentViewExpr extends ExpressionBase {
  9419. kind = ExpressionKind.GetCurrentView;
  9420. constructor() {
  9421. super();
  9422. }
  9423. visitExpression() { }
  9424. isEquivalent(e) {
  9425. return e instanceof GetCurrentViewExpr;
  9426. }
  9427. isConstant() {
  9428. return false;
  9429. }
  9430. transformInternalExpressions() { }
  9431. clone() {
  9432. return new GetCurrentViewExpr();
  9433. }
  9434. }
  9435. /**
  9436. * Runtime operation to restore a snapshotted view.
  9437. */
  9438. class RestoreViewExpr extends ExpressionBase {
  9439. view;
  9440. kind = ExpressionKind.RestoreView;
  9441. constructor(view) {
  9442. super();
  9443. this.view = view;
  9444. }
  9445. visitExpression(visitor, context) {
  9446. if (typeof this.view !== 'number') {
  9447. this.view.visitExpression(visitor, context);
  9448. }
  9449. }
  9450. isEquivalent(e) {
  9451. if (!(e instanceof RestoreViewExpr) || typeof e.view !== typeof this.view) {
  9452. return false;
  9453. }
  9454. if (typeof this.view === 'number') {
  9455. return this.view === e.view;
  9456. }
  9457. else {
  9458. return this.view.isEquivalent(e.view);
  9459. }
  9460. }
  9461. isConstant() {
  9462. return false;
  9463. }
  9464. transformInternalExpressions(transform, flags) {
  9465. if (typeof this.view !== 'number') {
  9466. this.view = transformExpressionsInExpression(this.view, transform, flags);
  9467. }
  9468. }
  9469. clone() {
  9470. return new RestoreViewExpr(this.view instanceof Expression ? this.view.clone() : this.view);
  9471. }
  9472. }
  9473. /**
  9474. * Runtime operation to reset the current view context after `RestoreView`.
  9475. */
  9476. class ResetViewExpr extends ExpressionBase {
  9477. expr;
  9478. kind = ExpressionKind.ResetView;
  9479. constructor(expr) {
  9480. super();
  9481. this.expr = expr;
  9482. }
  9483. visitExpression(visitor, context) {
  9484. this.expr.visitExpression(visitor, context);
  9485. }
  9486. isEquivalent(e) {
  9487. return e instanceof ResetViewExpr && this.expr.isEquivalent(e.expr);
  9488. }
  9489. isConstant() {
  9490. return false;
  9491. }
  9492. transformInternalExpressions(transform, flags) {
  9493. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  9494. }
  9495. clone() {
  9496. return new ResetViewExpr(this.expr.clone());
  9497. }
  9498. }
  9499. class TwoWayBindingSetExpr extends ExpressionBase {
  9500. target;
  9501. value;
  9502. kind = ExpressionKind.TwoWayBindingSet;
  9503. constructor(target, value) {
  9504. super();
  9505. this.target = target;
  9506. this.value = value;
  9507. }
  9508. visitExpression(visitor, context) {
  9509. this.target.visitExpression(visitor, context);
  9510. this.value.visitExpression(visitor, context);
  9511. }
  9512. isEquivalent(other) {
  9513. return this.target.isEquivalent(other.target) && this.value.isEquivalent(other.value);
  9514. }
  9515. isConstant() {
  9516. return false;
  9517. }
  9518. transformInternalExpressions(transform, flags) {
  9519. this.target = transformExpressionsInExpression(this.target, transform, flags);
  9520. this.value = transformExpressionsInExpression(this.value, transform, flags);
  9521. }
  9522. clone() {
  9523. return new TwoWayBindingSetExpr(this.target, this.value);
  9524. }
  9525. }
  9526. /**
  9527. * Read of a variable declared as an `ir.VariableOp` and referenced through its `ir.XrefId`.
  9528. */
  9529. class ReadVariableExpr extends ExpressionBase {
  9530. xref;
  9531. kind = ExpressionKind.ReadVariable;
  9532. name = null;
  9533. constructor(xref) {
  9534. super();
  9535. this.xref = xref;
  9536. }
  9537. visitExpression() { }
  9538. isEquivalent(other) {
  9539. return other instanceof ReadVariableExpr && other.xref === this.xref;
  9540. }
  9541. isConstant() {
  9542. return false;
  9543. }
  9544. transformInternalExpressions() { }
  9545. clone() {
  9546. const expr = new ReadVariableExpr(this.xref);
  9547. expr.name = this.name;
  9548. return expr;
  9549. }
  9550. }
  9551. class PureFunctionExpr extends ExpressionBase {
  9552. kind = ExpressionKind.PureFunctionExpr;
  9553. [ConsumesVarsTrait] = true;
  9554. [UsesVarOffset] = true;
  9555. varOffset = null;
  9556. /**
  9557. * The expression which should be memoized as a pure computation.
  9558. *
  9559. * This expression contains internal `PureFunctionParameterExpr`s, which are placeholders for the
  9560. * positional argument expressions in `args.
  9561. */
  9562. body;
  9563. /**
  9564. * Positional arguments to the pure function which will memoize the `body` expression, which act
  9565. * as memoization keys.
  9566. */
  9567. args;
  9568. /**
  9569. * Once extracted to the `ConstantPool`, a reference to the function which defines the computation
  9570. * of `body`.
  9571. */
  9572. fn = null;
  9573. constructor(expression, args) {
  9574. super();
  9575. this.body = expression;
  9576. this.args = args;
  9577. }
  9578. visitExpression(visitor, context) {
  9579. this.body?.visitExpression(visitor, context);
  9580. for (const arg of this.args) {
  9581. arg.visitExpression(visitor, context);
  9582. }
  9583. }
  9584. isEquivalent(other) {
  9585. if (!(other instanceof PureFunctionExpr) || other.args.length !== this.args.length) {
  9586. return false;
  9587. }
  9588. return (other.body !== null &&
  9589. this.body !== null &&
  9590. other.body.isEquivalent(this.body) &&
  9591. other.args.every((arg, idx) => arg.isEquivalent(this.args[idx])));
  9592. }
  9593. isConstant() {
  9594. return false;
  9595. }
  9596. transformInternalExpressions(transform, flags) {
  9597. if (this.body !== null) {
  9598. // TODO: figure out if this is the right flag to pass here.
  9599. this.body = transformExpressionsInExpression(this.body, transform, flags | VisitorContextFlag.InChildOperation);
  9600. }
  9601. else if (this.fn !== null) {
  9602. this.fn = transformExpressionsInExpression(this.fn, transform, flags);
  9603. }
  9604. for (let i = 0; i < this.args.length; i++) {
  9605. this.args[i] = transformExpressionsInExpression(this.args[i], transform, flags);
  9606. }
  9607. }
  9608. clone() {
  9609. const expr = new PureFunctionExpr(this.body?.clone() ?? null, this.args.map((arg) => arg.clone()));
  9610. expr.fn = this.fn?.clone() ?? null;
  9611. expr.varOffset = this.varOffset;
  9612. return expr;
  9613. }
  9614. }
  9615. class PureFunctionParameterExpr extends ExpressionBase {
  9616. index;
  9617. kind = ExpressionKind.PureFunctionParameterExpr;
  9618. constructor(index) {
  9619. super();
  9620. this.index = index;
  9621. }
  9622. visitExpression() { }
  9623. isEquivalent(other) {
  9624. return other instanceof PureFunctionParameterExpr && other.index === this.index;
  9625. }
  9626. isConstant() {
  9627. return true;
  9628. }
  9629. transformInternalExpressions() { }
  9630. clone() {
  9631. return new PureFunctionParameterExpr(this.index);
  9632. }
  9633. }
  9634. class PipeBindingExpr extends ExpressionBase {
  9635. target;
  9636. targetSlot;
  9637. name;
  9638. args;
  9639. kind = ExpressionKind.PipeBinding;
  9640. [ConsumesVarsTrait] = true;
  9641. [UsesVarOffset] = true;
  9642. varOffset = null;
  9643. constructor(target, targetSlot, name, args) {
  9644. super();
  9645. this.target = target;
  9646. this.targetSlot = targetSlot;
  9647. this.name = name;
  9648. this.args = args;
  9649. }
  9650. visitExpression(visitor, context) {
  9651. for (const arg of this.args) {
  9652. arg.visitExpression(visitor, context);
  9653. }
  9654. }
  9655. isEquivalent() {
  9656. return false;
  9657. }
  9658. isConstant() {
  9659. return false;
  9660. }
  9661. transformInternalExpressions(transform, flags) {
  9662. for (let idx = 0; idx < this.args.length; idx++) {
  9663. this.args[idx] = transformExpressionsInExpression(this.args[idx], transform, flags);
  9664. }
  9665. }
  9666. clone() {
  9667. const r = new PipeBindingExpr(this.target, this.targetSlot, this.name, this.args.map((a) => a.clone()));
  9668. r.varOffset = this.varOffset;
  9669. return r;
  9670. }
  9671. }
  9672. class PipeBindingVariadicExpr extends ExpressionBase {
  9673. target;
  9674. targetSlot;
  9675. name;
  9676. args;
  9677. numArgs;
  9678. kind = ExpressionKind.PipeBindingVariadic;
  9679. [ConsumesVarsTrait] = true;
  9680. [UsesVarOffset] = true;
  9681. varOffset = null;
  9682. constructor(target, targetSlot, name, args, numArgs) {
  9683. super();
  9684. this.target = target;
  9685. this.targetSlot = targetSlot;
  9686. this.name = name;
  9687. this.args = args;
  9688. this.numArgs = numArgs;
  9689. }
  9690. visitExpression(visitor, context) {
  9691. this.args.visitExpression(visitor, context);
  9692. }
  9693. isEquivalent() {
  9694. return false;
  9695. }
  9696. isConstant() {
  9697. return false;
  9698. }
  9699. transformInternalExpressions(transform, flags) {
  9700. this.args = transformExpressionsInExpression(this.args, transform, flags);
  9701. }
  9702. clone() {
  9703. const r = new PipeBindingVariadicExpr(this.target, this.targetSlot, this.name, this.args.clone(), this.numArgs);
  9704. r.varOffset = this.varOffset;
  9705. return r;
  9706. }
  9707. }
  9708. class SafePropertyReadExpr extends ExpressionBase {
  9709. receiver;
  9710. name;
  9711. kind = ExpressionKind.SafePropertyRead;
  9712. constructor(receiver, name) {
  9713. super();
  9714. this.receiver = receiver;
  9715. this.name = name;
  9716. }
  9717. // An alias for name, which allows other logic to handle property reads and keyed reads together.
  9718. get index() {
  9719. return this.name;
  9720. }
  9721. visitExpression(visitor, context) {
  9722. this.receiver.visitExpression(visitor, context);
  9723. }
  9724. isEquivalent() {
  9725. return false;
  9726. }
  9727. isConstant() {
  9728. return false;
  9729. }
  9730. transformInternalExpressions(transform, flags) {
  9731. this.receiver = transformExpressionsInExpression(this.receiver, transform, flags);
  9732. }
  9733. clone() {
  9734. return new SafePropertyReadExpr(this.receiver.clone(), this.name);
  9735. }
  9736. }
  9737. class SafeKeyedReadExpr extends ExpressionBase {
  9738. receiver;
  9739. index;
  9740. kind = ExpressionKind.SafeKeyedRead;
  9741. constructor(receiver, index, sourceSpan) {
  9742. super(sourceSpan);
  9743. this.receiver = receiver;
  9744. this.index = index;
  9745. }
  9746. visitExpression(visitor, context) {
  9747. this.receiver.visitExpression(visitor, context);
  9748. this.index.visitExpression(visitor, context);
  9749. }
  9750. isEquivalent() {
  9751. return false;
  9752. }
  9753. isConstant() {
  9754. return false;
  9755. }
  9756. transformInternalExpressions(transform, flags) {
  9757. this.receiver = transformExpressionsInExpression(this.receiver, transform, flags);
  9758. this.index = transformExpressionsInExpression(this.index, transform, flags);
  9759. }
  9760. clone() {
  9761. return new SafeKeyedReadExpr(this.receiver.clone(), this.index.clone(), this.sourceSpan);
  9762. }
  9763. }
  9764. class SafeInvokeFunctionExpr extends ExpressionBase {
  9765. receiver;
  9766. args;
  9767. kind = ExpressionKind.SafeInvokeFunction;
  9768. constructor(receiver, args) {
  9769. super();
  9770. this.receiver = receiver;
  9771. this.args = args;
  9772. }
  9773. visitExpression(visitor, context) {
  9774. this.receiver.visitExpression(visitor, context);
  9775. for (const a of this.args) {
  9776. a.visitExpression(visitor, context);
  9777. }
  9778. }
  9779. isEquivalent() {
  9780. return false;
  9781. }
  9782. isConstant() {
  9783. return false;
  9784. }
  9785. transformInternalExpressions(transform, flags) {
  9786. this.receiver = transformExpressionsInExpression(this.receiver, transform, flags);
  9787. for (let i = 0; i < this.args.length; i++) {
  9788. this.args[i] = transformExpressionsInExpression(this.args[i], transform, flags);
  9789. }
  9790. }
  9791. clone() {
  9792. return new SafeInvokeFunctionExpr(this.receiver.clone(), this.args.map((a) => a.clone()));
  9793. }
  9794. }
  9795. class SafeTernaryExpr extends ExpressionBase {
  9796. guard;
  9797. expr;
  9798. kind = ExpressionKind.SafeTernaryExpr;
  9799. constructor(guard, expr) {
  9800. super();
  9801. this.guard = guard;
  9802. this.expr = expr;
  9803. }
  9804. visitExpression(visitor, context) {
  9805. this.guard.visitExpression(visitor, context);
  9806. this.expr.visitExpression(visitor, context);
  9807. }
  9808. isEquivalent() {
  9809. return false;
  9810. }
  9811. isConstant() {
  9812. return false;
  9813. }
  9814. transformInternalExpressions(transform, flags) {
  9815. this.guard = transformExpressionsInExpression(this.guard, transform, flags);
  9816. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  9817. }
  9818. clone() {
  9819. return new SafeTernaryExpr(this.guard.clone(), this.expr.clone());
  9820. }
  9821. }
  9822. class EmptyExpr extends ExpressionBase {
  9823. kind = ExpressionKind.EmptyExpr;
  9824. visitExpression(visitor, context) { }
  9825. isEquivalent(e) {
  9826. return e instanceof EmptyExpr;
  9827. }
  9828. isConstant() {
  9829. return true;
  9830. }
  9831. clone() {
  9832. return new EmptyExpr();
  9833. }
  9834. transformInternalExpressions() { }
  9835. }
  9836. class AssignTemporaryExpr extends ExpressionBase {
  9837. expr;
  9838. xref;
  9839. kind = ExpressionKind.AssignTemporaryExpr;
  9840. name = null;
  9841. constructor(expr, xref) {
  9842. super();
  9843. this.expr = expr;
  9844. this.xref = xref;
  9845. }
  9846. visitExpression(visitor, context) {
  9847. this.expr.visitExpression(visitor, context);
  9848. }
  9849. isEquivalent() {
  9850. return false;
  9851. }
  9852. isConstant() {
  9853. return false;
  9854. }
  9855. transformInternalExpressions(transform, flags) {
  9856. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  9857. }
  9858. clone() {
  9859. const a = new AssignTemporaryExpr(this.expr.clone(), this.xref);
  9860. a.name = this.name;
  9861. return a;
  9862. }
  9863. }
  9864. class ReadTemporaryExpr extends ExpressionBase {
  9865. xref;
  9866. kind = ExpressionKind.ReadTemporaryExpr;
  9867. name = null;
  9868. constructor(xref) {
  9869. super();
  9870. this.xref = xref;
  9871. }
  9872. visitExpression(visitor, context) { }
  9873. isEquivalent() {
  9874. return this.xref === this.xref;
  9875. }
  9876. isConstant() {
  9877. return false;
  9878. }
  9879. transformInternalExpressions(transform, flags) { }
  9880. clone() {
  9881. const r = new ReadTemporaryExpr(this.xref);
  9882. r.name = this.name;
  9883. return r;
  9884. }
  9885. }
  9886. class SlotLiteralExpr extends ExpressionBase {
  9887. slot;
  9888. kind = ExpressionKind.SlotLiteralExpr;
  9889. constructor(slot) {
  9890. super();
  9891. this.slot = slot;
  9892. }
  9893. visitExpression(visitor, context) { }
  9894. isEquivalent(e) {
  9895. return e instanceof SlotLiteralExpr && e.slot === this.slot;
  9896. }
  9897. isConstant() {
  9898. return true;
  9899. }
  9900. clone() {
  9901. return new SlotLiteralExpr(this.slot);
  9902. }
  9903. transformInternalExpressions() { }
  9904. }
  9905. class ConditionalCaseExpr extends ExpressionBase {
  9906. expr;
  9907. target;
  9908. targetSlot;
  9909. alias;
  9910. kind = ExpressionKind.ConditionalCase;
  9911. /**
  9912. * Create an expression for one branch of a conditional.
  9913. * @param expr The expression to be tested for this case. Might be null, as in an `else` case.
  9914. * @param target The Xref of the view to be displayed if this condition is true.
  9915. */
  9916. constructor(expr, target, targetSlot, alias = null) {
  9917. super();
  9918. this.expr = expr;
  9919. this.target = target;
  9920. this.targetSlot = targetSlot;
  9921. this.alias = alias;
  9922. }
  9923. visitExpression(visitor, context) {
  9924. if (this.expr !== null) {
  9925. this.expr.visitExpression(visitor, context);
  9926. }
  9927. }
  9928. isEquivalent(e) {
  9929. return e instanceof ConditionalCaseExpr && e.expr === this.expr;
  9930. }
  9931. isConstant() {
  9932. return true;
  9933. }
  9934. clone() {
  9935. return new ConditionalCaseExpr(this.expr, this.target, this.targetSlot);
  9936. }
  9937. transformInternalExpressions(transform, flags) {
  9938. if (this.expr !== null) {
  9939. this.expr = transformExpressionsInExpression(this.expr, transform, flags);
  9940. }
  9941. }
  9942. }
  9943. class ConstCollectedExpr extends ExpressionBase {
  9944. expr;
  9945. kind = ExpressionKind.ConstCollected;
  9946. constructor(expr) {
  9947. super();
  9948. this.expr = expr;
  9949. }
  9950. transformInternalExpressions(transform, flags) {
  9951. this.expr = transform(this.expr, flags);
  9952. }
  9953. visitExpression(visitor, context) {
  9954. this.expr.visitExpression(visitor, context);
  9955. }
  9956. isEquivalent(e) {
  9957. if (!(e instanceof ConstCollectedExpr)) {
  9958. return false;
  9959. }
  9960. return this.expr.isEquivalent(e.expr);
  9961. }
  9962. isConstant() {
  9963. return this.expr.isConstant();
  9964. }
  9965. clone() {
  9966. return new ConstCollectedExpr(this.expr);
  9967. }
  9968. }
  9969. /**
  9970. * Visits all `Expression`s in the AST of `op` with the `visitor` function.
  9971. */
  9972. function visitExpressionsInOp(op, visitor) {
  9973. transformExpressionsInOp(op, (expr, flags) => {
  9974. visitor(expr, flags);
  9975. return expr;
  9976. }, VisitorContextFlag.None);
  9977. }
  9978. var VisitorContextFlag;
  9979. (function (VisitorContextFlag) {
  9980. VisitorContextFlag[VisitorContextFlag["None"] = 0] = "None";
  9981. VisitorContextFlag[VisitorContextFlag["InChildOperation"] = 1] = "InChildOperation";
  9982. })(VisitorContextFlag || (VisitorContextFlag = {}));
  9983. function transformExpressionsInInterpolation(interpolation, transform, flags) {
  9984. for (let i = 0; i < interpolation.expressions.length; i++) {
  9985. interpolation.expressions[i] = transformExpressionsInExpression(interpolation.expressions[i], transform, flags);
  9986. }
  9987. }
  9988. /**
  9989. * Transform all `Expression`s in the AST of `op` with the `transform` function.
  9990. *
  9991. * All such operations will be replaced with the result of applying `transform`, which may be an
  9992. * identity transformation.
  9993. */
  9994. function transformExpressionsInOp(op, transform, flags) {
  9995. switch (op.kind) {
  9996. case OpKind.StyleProp:
  9997. case OpKind.StyleMap:
  9998. case OpKind.ClassProp:
  9999. case OpKind.ClassMap:
  10000. case OpKind.Binding:
  10001. if (op.expression instanceof Interpolation) {
  10002. transformExpressionsInInterpolation(op.expression, transform, flags);
  10003. }
  10004. else {
  10005. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10006. }
  10007. break;
  10008. case OpKind.Property:
  10009. case OpKind.HostProperty:
  10010. case OpKind.Attribute:
  10011. if (op.expression instanceof Interpolation) {
  10012. transformExpressionsInInterpolation(op.expression, transform, flags);
  10013. }
  10014. else {
  10015. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10016. }
  10017. op.sanitizer =
  10018. op.sanitizer && transformExpressionsInExpression(op.sanitizer, transform, flags);
  10019. break;
  10020. case OpKind.TwoWayProperty:
  10021. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10022. op.sanitizer =
  10023. op.sanitizer && transformExpressionsInExpression(op.sanitizer, transform, flags);
  10024. break;
  10025. case OpKind.I18nExpression:
  10026. op.expression = transformExpressionsInExpression(op.expression, transform, flags);
  10027. break;
  10028. case OpKind.InterpolateText:
  10029. transformExpressionsInInterpolation(op.interpolation, transform, flags);
  10030. break;
  10031. case OpKind.Statement:
  10032. transformExpressionsInStatement(op.statement, transform, flags);
  10033. break;
  10034. case OpKind.Variable:
  10035. op.initializer = transformExpressionsInExpression(op.initializer, transform, flags);
  10036. break;
  10037. case OpKind.Conditional:
  10038. for (const condition of op.conditions) {
  10039. if (condition.expr === null) {
  10040. // This is a default case.
  10041. continue;
  10042. }
  10043. condition.expr = transformExpressionsInExpression(condition.expr, transform, flags);
  10044. }
  10045. if (op.processed !== null) {
  10046. op.processed = transformExpressionsInExpression(op.processed, transform, flags);
  10047. }
  10048. if (op.contextValue !== null) {
  10049. op.contextValue = transformExpressionsInExpression(op.contextValue, transform, flags);
  10050. }
  10051. break;
  10052. case OpKind.Listener:
  10053. case OpKind.TwoWayListener:
  10054. for (const innerOp of op.handlerOps) {
  10055. transformExpressionsInOp(innerOp, transform, flags | VisitorContextFlag.InChildOperation);
  10056. }
  10057. break;
  10058. case OpKind.ExtractedAttribute:
  10059. op.expression =
  10060. op.expression && transformExpressionsInExpression(op.expression, transform, flags);
  10061. op.trustedValueFn =
  10062. op.trustedValueFn && transformExpressionsInExpression(op.trustedValueFn, transform, flags);
  10063. break;
  10064. case OpKind.RepeaterCreate:
  10065. if (op.trackByOps === null) {
  10066. op.track = transformExpressionsInExpression(op.track, transform, flags);
  10067. }
  10068. else {
  10069. for (const innerOp of op.trackByOps) {
  10070. transformExpressionsInOp(innerOp, transform, flags | VisitorContextFlag.InChildOperation);
  10071. }
  10072. }
  10073. if (op.trackByFn !== null) {
  10074. op.trackByFn = transformExpressionsInExpression(op.trackByFn, transform, flags);
  10075. }
  10076. break;
  10077. case OpKind.Repeater:
  10078. op.collection = transformExpressionsInExpression(op.collection, transform, flags);
  10079. break;
  10080. case OpKind.Defer:
  10081. if (op.loadingConfig !== null) {
  10082. op.loadingConfig = transformExpressionsInExpression(op.loadingConfig, transform, flags);
  10083. }
  10084. if (op.placeholderConfig !== null) {
  10085. op.placeholderConfig = transformExpressionsInExpression(op.placeholderConfig, transform, flags);
  10086. }
  10087. if (op.resolverFn !== null) {
  10088. op.resolverFn = transformExpressionsInExpression(op.resolverFn, transform, flags);
  10089. }
  10090. break;
  10091. case OpKind.I18nMessage:
  10092. for (const [placeholder, expr] of op.params) {
  10093. op.params.set(placeholder, transformExpressionsInExpression(expr, transform, flags));
  10094. }
  10095. for (const [placeholder, expr] of op.postprocessingParams) {
  10096. op.postprocessingParams.set(placeholder, transformExpressionsInExpression(expr, transform, flags));
  10097. }
  10098. break;
  10099. case OpKind.DeferWhen:
  10100. op.expr = transformExpressionsInExpression(op.expr, transform, flags);
  10101. break;
  10102. case OpKind.StoreLet:
  10103. op.value = transformExpressionsInExpression(op.value, transform, flags);
  10104. break;
  10105. case OpKind.Advance:
  10106. case OpKind.Container:
  10107. case OpKind.ContainerEnd:
  10108. case OpKind.ContainerStart:
  10109. case OpKind.DeferOn:
  10110. case OpKind.DisableBindings:
  10111. case OpKind.Element:
  10112. case OpKind.ElementEnd:
  10113. case OpKind.ElementStart:
  10114. case OpKind.EnableBindings:
  10115. case OpKind.I18n:
  10116. case OpKind.I18nApply:
  10117. case OpKind.I18nContext:
  10118. case OpKind.I18nEnd:
  10119. case OpKind.I18nStart:
  10120. case OpKind.IcuEnd:
  10121. case OpKind.IcuStart:
  10122. case OpKind.Namespace:
  10123. case OpKind.Pipe:
  10124. case OpKind.Projection:
  10125. case OpKind.ProjectionDef:
  10126. case OpKind.Template:
  10127. case OpKind.Text:
  10128. case OpKind.I18nAttributes:
  10129. case OpKind.IcuPlaceholder:
  10130. case OpKind.DeclareLet:
  10131. case OpKind.SourceLocation:
  10132. // These operations contain no expressions.
  10133. break;
  10134. default:
  10135. throw new Error(`AssertionError: transformExpressionsInOp doesn't handle ${OpKind[op.kind]}`);
  10136. }
  10137. }
  10138. /**
  10139. * Transform all `Expression`s in the AST of `expr` with the `transform` function.
  10140. *
  10141. * All such operations will be replaced with the result of applying `transform`, which may be an
  10142. * identity transformation.
  10143. */
  10144. function transformExpressionsInExpression(expr, transform, flags) {
  10145. if (expr instanceof ExpressionBase) {
  10146. expr.transformInternalExpressions(transform, flags);
  10147. }
  10148. else if (expr instanceof BinaryOperatorExpr) {
  10149. expr.lhs = transformExpressionsInExpression(expr.lhs, transform, flags);
  10150. expr.rhs = transformExpressionsInExpression(expr.rhs, transform, flags);
  10151. }
  10152. else if (expr instanceof UnaryOperatorExpr) {
  10153. expr.expr = transformExpressionsInExpression(expr.expr, transform, flags);
  10154. }
  10155. else if (expr instanceof ReadPropExpr) {
  10156. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10157. }
  10158. else if (expr instanceof ReadKeyExpr) {
  10159. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10160. expr.index = transformExpressionsInExpression(expr.index, transform, flags);
  10161. }
  10162. else if (expr instanceof WritePropExpr) {
  10163. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10164. expr.value = transformExpressionsInExpression(expr.value, transform, flags);
  10165. }
  10166. else if (expr instanceof WriteKeyExpr) {
  10167. expr.receiver = transformExpressionsInExpression(expr.receiver, transform, flags);
  10168. expr.index = transformExpressionsInExpression(expr.index, transform, flags);
  10169. expr.value = transformExpressionsInExpression(expr.value, transform, flags);
  10170. }
  10171. else if (expr instanceof InvokeFunctionExpr) {
  10172. expr.fn = transformExpressionsInExpression(expr.fn, transform, flags);
  10173. for (let i = 0; i < expr.args.length; i++) {
  10174. expr.args[i] = transformExpressionsInExpression(expr.args[i], transform, flags);
  10175. }
  10176. }
  10177. else if (expr instanceof LiteralArrayExpr) {
  10178. for (let i = 0; i < expr.entries.length; i++) {
  10179. expr.entries[i] = transformExpressionsInExpression(expr.entries[i], transform, flags);
  10180. }
  10181. }
  10182. else if (expr instanceof LiteralMapExpr) {
  10183. for (let i = 0; i < expr.entries.length; i++) {
  10184. expr.entries[i].value = transformExpressionsInExpression(expr.entries[i].value, transform, flags);
  10185. }
  10186. }
  10187. else if (expr instanceof ConditionalExpr) {
  10188. expr.condition = transformExpressionsInExpression(expr.condition, transform, flags);
  10189. expr.trueCase = transformExpressionsInExpression(expr.trueCase, transform, flags);
  10190. if (expr.falseCase !== null) {
  10191. expr.falseCase = transformExpressionsInExpression(expr.falseCase, transform, flags);
  10192. }
  10193. }
  10194. else if (expr instanceof TypeofExpr) {
  10195. expr.expr = transformExpressionsInExpression(expr.expr, transform, flags);
  10196. }
  10197. else if (expr instanceof WriteVarExpr) {
  10198. expr.value = transformExpressionsInExpression(expr.value, transform, flags);
  10199. }
  10200. else if (expr instanceof LocalizedString) {
  10201. for (let i = 0; i < expr.expressions.length; i++) {
  10202. expr.expressions[i] = transformExpressionsInExpression(expr.expressions[i], transform, flags);
  10203. }
  10204. }
  10205. else if (expr instanceof NotExpr) {
  10206. expr.condition = transformExpressionsInExpression(expr.condition, transform, flags);
  10207. }
  10208. else if (expr instanceof TaggedTemplateLiteralExpr) {
  10209. expr.tag = transformExpressionsInExpression(expr.tag, transform, flags);
  10210. expr.template.expressions = expr.template.expressions.map((e) => transformExpressionsInExpression(e, transform, flags));
  10211. }
  10212. else if (expr instanceof ArrowFunctionExpr) {
  10213. if (Array.isArray(expr.body)) {
  10214. for (let i = 0; i < expr.body.length; i++) {
  10215. transformExpressionsInStatement(expr.body[i], transform, flags);
  10216. }
  10217. }
  10218. else {
  10219. expr.body = transformExpressionsInExpression(expr.body, transform, flags);
  10220. }
  10221. }
  10222. else if (expr instanceof WrappedNodeExpr) ;
  10223. else if (expr instanceof TemplateLiteralExpr) {
  10224. for (let i = 0; i < expr.expressions.length; i++) {
  10225. expr.expressions[i] = transformExpressionsInExpression(expr.expressions[i], transform, flags);
  10226. }
  10227. }
  10228. else if (expr instanceof ReadVarExpr ||
  10229. expr instanceof ExternalExpr ||
  10230. expr instanceof LiteralExpr) ;
  10231. else {
  10232. throw new Error(`Unhandled expression kind: ${expr.constructor.name}`);
  10233. }
  10234. return transform(expr, flags);
  10235. }
  10236. /**
  10237. * Transform all `Expression`s in the AST of `stmt` with the `transform` function.
  10238. *
  10239. * All such operations will be replaced with the result of applying `transform`, which may be an
  10240. * identity transformation.
  10241. */
  10242. function transformExpressionsInStatement(stmt, transform, flags) {
  10243. if (stmt instanceof ExpressionStatement) {
  10244. stmt.expr = transformExpressionsInExpression(stmt.expr, transform, flags);
  10245. }
  10246. else if (stmt instanceof ReturnStatement) {
  10247. stmt.value = transformExpressionsInExpression(stmt.value, transform, flags);
  10248. }
  10249. else if (stmt instanceof DeclareVarStmt) {
  10250. if (stmt.value !== undefined) {
  10251. stmt.value = transformExpressionsInExpression(stmt.value, transform, flags);
  10252. }
  10253. }
  10254. else if (stmt instanceof IfStmt) {
  10255. stmt.condition = transformExpressionsInExpression(stmt.condition, transform, flags);
  10256. for (const caseStatement of stmt.trueCase) {
  10257. transformExpressionsInStatement(caseStatement, transform, flags);
  10258. }
  10259. for (const caseStatement of stmt.falseCase) {
  10260. transformExpressionsInStatement(caseStatement, transform, flags);
  10261. }
  10262. }
  10263. else {
  10264. throw new Error(`Unhandled statement kind: ${stmt.constructor.name}`);
  10265. }
  10266. }
  10267. /**
  10268. * Checks whether the given expression is a string literal.
  10269. */
  10270. function isStringLiteral(expr) {
  10271. return expr instanceof LiteralExpr && typeof expr.value === 'string';
  10272. }
  10273. /**
  10274. * A linked list of `Op` nodes of a given subtype.
  10275. *
  10276. * @param OpT specific subtype of `Op` nodes which this list contains.
  10277. */
  10278. class OpList {
  10279. static nextListId = 0;
  10280. /**
  10281. * Debug ID of this `OpList` instance.
  10282. */
  10283. debugListId = OpList.nextListId++;
  10284. // OpList uses static head/tail nodes of a special `ListEnd` type.
  10285. // This avoids the need for special casing of the first and last list
  10286. // elements in all list operations.
  10287. head = {
  10288. kind: OpKind.ListEnd,
  10289. next: null,
  10290. prev: null,
  10291. debugListId: this.debugListId,
  10292. };
  10293. tail = {
  10294. kind: OpKind.ListEnd,
  10295. next: null,
  10296. prev: null,
  10297. debugListId: this.debugListId,
  10298. };
  10299. constructor() {
  10300. // Link `head` and `tail` together at the start (list is empty).
  10301. this.head.next = this.tail;
  10302. this.tail.prev = this.head;
  10303. }
  10304. /**
  10305. * Push a new operation to the tail of the list.
  10306. */
  10307. push(op) {
  10308. if (Array.isArray(op)) {
  10309. for (const o of op) {
  10310. this.push(o);
  10311. }
  10312. return;
  10313. }
  10314. OpList.assertIsNotEnd(op);
  10315. OpList.assertIsUnowned(op);
  10316. op.debugListId = this.debugListId;
  10317. // The old "previous" node (which might be the head, if the list is empty).
  10318. const oldLast = this.tail.prev;
  10319. // Insert `op` following the old last node.
  10320. op.prev = oldLast;
  10321. oldLast.next = op;
  10322. // Connect `op` with the list tail.
  10323. op.next = this.tail;
  10324. this.tail.prev = op;
  10325. }
  10326. /**
  10327. * Prepend one or more nodes to the start of the list.
  10328. */
  10329. prepend(ops) {
  10330. if (ops.length === 0) {
  10331. return;
  10332. }
  10333. for (const op of ops) {
  10334. OpList.assertIsNotEnd(op);
  10335. OpList.assertIsUnowned(op);
  10336. op.debugListId = this.debugListId;
  10337. }
  10338. const first = this.head.next;
  10339. let prev = this.head;
  10340. for (const op of ops) {
  10341. prev.next = op;
  10342. op.prev = prev;
  10343. prev = op;
  10344. }
  10345. prev.next = first;
  10346. first.prev = prev;
  10347. }
  10348. /**
  10349. * `OpList` is iterable via the iteration protocol.
  10350. *
  10351. * It's safe to mutate the part of the list that has already been returned by the iterator, up to
  10352. * and including the last operation returned. Mutations beyond that point _may_ be safe, but may
  10353. * also corrupt the iteration position and should be avoided.
  10354. */
  10355. *[Symbol.iterator]() {
  10356. let current = this.head.next;
  10357. while (current !== this.tail) {
  10358. // Guards against corruption of the iterator state by mutations to the tail of the list during
  10359. // iteration.
  10360. OpList.assertIsOwned(current, this.debugListId);
  10361. const next = current.next;
  10362. yield current;
  10363. current = next;
  10364. }
  10365. }
  10366. *reversed() {
  10367. let current = this.tail.prev;
  10368. while (current !== this.head) {
  10369. OpList.assertIsOwned(current, this.debugListId);
  10370. const prev = current.prev;
  10371. yield current;
  10372. current = prev;
  10373. }
  10374. }
  10375. /**
  10376. * Replace `oldOp` with `newOp` in the list.
  10377. */
  10378. static replace(oldOp, newOp) {
  10379. OpList.assertIsNotEnd(oldOp);
  10380. OpList.assertIsNotEnd(newOp);
  10381. OpList.assertIsOwned(oldOp);
  10382. OpList.assertIsUnowned(newOp);
  10383. newOp.debugListId = oldOp.debugListId;
  10384. if (oldOp.prev !== null) {
  10385. oldOp.prev.next = newOp;
  10386. newOp.prev = oldOp.prev;
  10387. }
  10388. if (oldOp.next !== null) {
  10389. oldOp.next.prev = newOp;
  10390. newOp.next = oldOp.next;
  10391. }
  10392. oldOp.debugListId = null;
  10393. oldOp.prev = null;
  10394. oldOp.next = null;
  10395. }
  10396. /**
  10397. * Replace `oldOp` with some number of new operations in the list (which may include `oldOp`).
  10398. */
  10399. static replaceWithMany(oldOp, newOps) {
  10400. if (newOps.length === 0) {
  10401. // Replacing with an empty list -> pure removal.
  10402. OpList.remove(oldOp);
  10403. return;
  10404. }
  10405. OpList.assertIsNotEnd(oldOp);
  10406. OpList.assertIsOwned(oldOp);
  10407. const listId = oldOp.debugListId;
  10408. oldOp.debugListId = null;
  10409. for (const newOp of newOps) {
  10410. OpList.assertIsNotEnd(newOp);
  10411. // `newOp` might be `oldOp`, but at this point it's been marked as unowned.
  10412. OpList.assertIsUnowned(newOp);
  10413. }
  10414. // It should be safe to reuse `oldOp` in the `newOps` list - maybe you want to sandwich an
  10415. // operation between two new ops.
  10416. const { prev: oldPrev, next: oldNext } = oldOp;
  10417. oldOp.prev = null;
  10418. oldOp.next = null;
  10419. let prev = oldPrev;
  10420. for (const newOp of newOps) {
  10421. this.assertIsUnowned(newOp);
  10422. newOp.debugListId = listId;
  10423. prev.next = newOp;
  10424. newOp.prev = prev;
  10425. // This _should_ be the case, but set it just in case.
  10426. newOp.next = null;
  10427. prev = newOp;
  10428. }
  10429. // At the end of iteration, `prev` holds the last node in the list.
  10430. const first = newOps[0];
  10431. const last = prev;
  10432. // Replace `oldOp` with the chain `first` -> `last`.
  10433. if (oldPrev !== null) {
  10434. oldPrev.next = first;
  10435. first.prev = oldPrev;
  10436. }
  10437. if (oldNext !== null) {
  10438. oldNext.prev = last;
  10439. last.next = oldNext;
  10440. }
  10441. }
  10442. /**
  10443. * Remove the given node from the list which contains it.
  10444. */
  10445. static remove(op) {
  10446. OpList.assertIsNotEnd(op);
  10447. OpList.assertIsOwned(op);
  10448. op.prev.next = op.next;
  10449. op.next.prev = op.prev;
  10450. // Break any link between the node and this list to safeguard against its usage in future
  10451. // operations.
  10452. op.debugListId = null;
  10453. op.prev = null;
  10454. op.next = null;
  10455. }
  10456. /**
  10457. * Insert `op` before `target`.
  10458. */
  10459. static insertBefore(op, target) {
  10460. if (Array.isArray(op)) {
  10461. for (const o of op) {
  10462. this.insertBefore(o, target);
  10463. }
  10464. return;
  10465. }
  10466. OpList.assertIsOwned(target);
  10467. if (target.prev === null) {
  10468. throw new Error(`AssertionError: illegal operation on list start`);
  10469. }
  10470. OpList.assertIsNotEnd(op);
  10471. OpList.assertIsUnowned(op);
  10472. op.debugListId = target.debugListId;
  10473. // Just in case.
  10474. op.prev = null;
  10475. target.prev.next = op;
  10476. op.prev = target.prev;
  10477. op.next = target;
  10478. target.prev = op;
  10479. }
  10480. /**
  10481. * Insert `op` after `target`.
  10482. */
  10483. static insertAfter(op, target) {
  10484. OpList.assertIsOwned(target);
  10485. if (target.next === null) {
  10486. throw new Error(`AssertionError: illegal operation on list end`);
  10487. }
  10488. OpList.assertIsNotEnd(op);
  10489. OpList.assertIsUnowned(op);
  10490. op.debugListId = target.debugListId;
  10491. target.next.prev = op;
  10492. op.next = target.next;
  10493. op.prev = target;
  10494. target.next = op;
  10495. }
  10496. /**
  10497. * Asserts that `op` does not currently belong to a list.
  10498. */
  10499. static assertIsUnowned(op) {
  10500. if (op.debugListId !== null) {
  10501. throw new Error(`AssertionError: illegal operation on owned node: ${OpKind[op.kind]}`);
  10502. }
  10503. }
  10504. /**
  10505. * Asserts that `op` currently belongs to a list. If `byList` is passed, `op` is asserted to
  10506. * specifically belong to that list.
  10507. */
  10508. static assertIsOwned(op, byList) {
  10509. if (op.debugListId === null) {
  10510. throw new Error(`AssertionError: illegal operation on unowned node: ${OpKind[op.kind]}`);
  10511. }
  10512. else if (byList !== undefined && op.debugListId !== byList) {
  10513. throw new Error(`AssertionError: node belongs to the wrong list (expected ${byList}, actual ${op.debugListId})`);
  10514. }
  10515. }
  10516. /**
  10517. * Asserts that `op` is not a special `ListEnd` node.
  10518. */
  10519. static assertIsNotEnd(op) {
  10520. if (op.kind === OpKind.ListEnd) {
  10521. throw new Error(`AssertionError: illegal operation on list head or tail`);
  10522. }
  10523. }
  10524. }
  10525. class SlotHandle {
  10526. slot = null;
  10527. }
  10528. /**
  10529. * The set of OpKinds that represent the creation of an element or container
  10530. */
  10531. const elementContainerOpKinds = new Set([
  10532. OpKind.Element,
  10533. OpKind.ElementStart,
  10534. OpKind.Container,
  10535. OpKind.ContainerStart,
  10536. OpKind.Template,
  10537. OpKind.RepeaterCreate,
  10538. ]);
  10539. /**
  10540. * Checks whether the given operation represents the creation of an element or container.
  10541. */
  10542. function isElementOrContainerOp(op) {
  10543. return elementContainerOpKinds.has(op.kind);
  10544. }
  10545. /**
  10546. * Create an `ElementStartOp`.
  10547. */
  10548. function createElementStartOp(tag, xref, namespace, i18nPlaceholder, startSourceSpan, wholeSourceSpan) {
  10549. return {
  10550. kind: OpKind.ElementStart,
  10551. xref,
  10552. tag,
  10553. handle: new SlotHandle(),
  10554. attributes: null,
  10555. localRefs: [],
  10556. nonBindable: false,
  10557. namespace,
  10558. i18nPlaceholder,
  10559. startSourceSpan,
  10560. wholeSourceSpan,
  10561. ...TRAIT_CONSUMES_SLOT,
  10562. ...NEW_OP,
  10563. };
  10564. }
  10565. /**
  10566. * Create a `TemplateOp`.
  10567. */
  10568. function createTemplateOp(xref, templateKind, tag, functionNameSuffix, namespace, i18nPlaceholder, startSourceSpan, wholeSourceSpan) {
  10569. return {
  10570. kind: OpKind.Template,
  10571. xref,
  10572. templateKind,
  10573. attributes: null,
  10574. tag,
  10575. handle: new SlotHandle(),
  10576. functionNameSuffix,
  10577. decls: null,
  10578. vars: null,
  10579. localRefs: [],
  10580. nonBindable: false,
  10581. namespace,
  10582. i18nPlaceholder,
  10583. startSourceSpan,
  10584. wholeSourceSpan,
  10585. ...TRAIT_CONSUMES_SLOT,
  10586. ...NEW_OP,
  10587. };
  10588. }
  10589. function createRepeaterCreateOp(primaryView, emptyView, tag, track, varNames, emptyTag, i18nPlaceholder, emptyI18nPlaceholder, startSourceSpan, wholeSourceSpan) {
  10590. return {
  10591. kind: OpKind.RepeaterCreate,
  10592. attributes: null,
  10593. xref: primaryView,
  10594. handle: new SlotHandle(),
  10595. emptyView,
  10596. track,
  10597. trackByFn: null,
  10598. trackByOps: null,
  10599. tag,
  10600. emptyTag,
  10601. emptyAttributes: null,
  10602. functionNameSuffix: 'For',
  10603. namespace: Namespace.HTML,
  10604. nonBindable: false,
  10605. localRefs: [],
  10606. decls: null,
  10607. vars: null,
  10608. varNames,
  10609. usesComponentInstance: false,
  10610. i18nPlaceholder,
  10611. emptyI18nPlaceholder,
  10612. startSourceSpan,
  10613. wholeSourceSpan,
  10614. ...TRAIT_CONSUMES_SLOT,
  10615. ...NEW_OP,
  10616. ...TRAIT_CONSUMES_VARS,
  10617. numSlotsUsed: emptyView === null ? 2 : 3,
  10618. };
  10619. }
  10620. /**
  10621. * Create an `ElementEndOp`.
  10622. */
  10623. function createElementEndOp(xref, sourceSpan) {
  10624. return {
  10625. kind: OpKind.ElementEnd,
  10626. xref,
  10627. sourceSpan,
  10628. ...NEW_OP,
  10629. };
  10630. }
  10631. function createDisableBindingsOp(xref) {
  10632. return {
  10633. kind: OpKind.DisableBindings,
  10634. xref,
  10635. ...NEW_OP,
  10636. };
  10637. }
  10638. function createEnableBindingsOp(xref) {
  10639. return {
  10640. kind: OpKind.EnableBindings,
  10641. xref,
  10642. ...NEW_OP,
  10643. };
  10644. }
  10645. /**
  10646. * Create a `TextOp`.
  10647. */
  10648. function createTextOp(xref, initialValue, icuPlaceholder, sourceSpan) {
  10649. return {
  10650. kind: OpKind.Text,
  10651. xref,
  10652. handle: new SlotHandle(),
  10653. initialValue,
  10654. icuPlaceholder,
  10655. sourceSpan,
  10656. ...TRAIT_CONSUMES_SLOT,
  10657. ...NEW_OP,
  10658. };
  10659. }
  10660. /**
  10661. * Create a `ListenerOp`. Host bindings reuse all the listener logic.
  10662. */
  10663. function createListenerOp(target, targetSlot, name, tag, handlerOps, animationPhase, eventTarget, hostListener, sourceSpan) {
  10664. const handlerList = new OpList();
  10665. handlerList.push(handlerOps);
  10666. return {
  10667. kind: OpKind.Listener,
  10668. target,
  10669. targetSlot,
  10670. tag,
  10671. hostListener,
  10672. name,
  10673. handlerOps: handlerList,
  10674. handlerFnName: null,
  10675. consumesDollarEvent: false,
  10676. isAnimationListener: animationPhase !== null,
  10677. animationPhase,
  10678. eventTarget,
  10679. sourceSpan,
  10680. ...NEW_OP,
  10681. };
  10682. }
  10683. /**
  10684. * Create a `TwoWayListenerOp`.
  10685. */
  10686. function createTwoWayListenerOp(target, targetSlot, name, tag, handlerOps, sourceSpan) {
  10687. const handlerList = new OpList();
  10688. handlerList.push(handlerOps);
  10689. return {
  10690. kind: OpKind.TwoWayListener,
  10691. target,
  10692. targetSlot,
  10693. tag,
  10694. name,
  10695. handlerOps: handlerList,
  10696. handlerFnName: null,
  10697. sourceSpan,
  10698. ...NEW_OP,
  10699. };
  10700. }
  10701. function createPipeOp(xref, slot, name) {
  10702. return {
  10703. kind: OpKind.Pipe,
  10704. xref,
  10705. handle: slot,
  10706. name,
  10707. ...NEW_OP,
  10708. ...TRAIT_CONSUMES_SLOT,
  10709. };
  10710. }
  10711. function createNamespaceOp(namespace) {
  10712. return {
  10713. kind: OpKind.Namespace,
  10714. active: namespace,
  10715. ...NEW_OP,
  10716. };
  10717. }
  10718. function createProjectionDefOp(def) {
  10719. return {
  10720. kind: OpKind.ProjectionDef,
  10721. def,
  10722. ...NEW_OP,
  10723. };
  10724. }
  10725. function createProjectionOp(xref, selector, i18nPlaceholder, fallbackView, sourceSpan) {
  10726. return {
  10727. kind: OpKind.Projection,
  10728. xref,
  10729. handle: new SlotHandle(),
  10730. selector,
  10731. i18nPlaceholder,
  10732. fallbackView,
  10733. projectionSlotIndex: 0,
  10734. attributes: null,
  10735. localRefs: [],
  10736. sourceSpan,
  10737. ...NEW_OP,
  10738. ...TRAIT_CONSUMES_SLOT,
  10739. numSlotsUsed: fallbackView === null ? 1 : 2,
  10740. };
  10741. }
  10742. /**
  10743. * Create an `ExtractedAttributeOp`.
  10744. */
  10745. function createExtractedAttributeOp(target, bindingKind, namespace, name, expression, i18nContext, i18nMessage, securityContext) {
  10746. return {
  10747. kind: OpKind.ExtractedAttribute,
  10748. target,
  10749. bindingKind,
  10750. namespace,
  10751. name,
  10752. expression,
  10753. i18nContext,
  10754. i18nMessage,
  10755. securityContext,
  10756. trustedValueFn: null,
  10757. ...NEW_OP,
  10758. };
  10759. }
  10760. function createDeferOp(xref, main, mainSlot, ownResolverFn, resolverFn, sourceSpan) {
  10761. return {
  10762. kind: OpKind.Defer,
  10763. xref,
  10764. handle: new SlotHandle(),
  10765. mainView: main,
  10766. mainSlot,
  10767. loadingView: null,
  10768. loadingSlot: null,
  10769. loadingConfig: null,
  10770. loadingMinimumTime: null,
  10771. loadingAfterTime: null,
  10772. placeholderView: null,
  10773. placeholderSlot: null,
  10774. placeholderConfig: null,
  10775. placeholderMinimumTime: null,
  10776. errorView: null,
  10777. errorSlot: null,
  10778. ownResolverFn,
  10779. resolverFn,
  10780. flags: null,
  10781. sourceSpan,
  10782. ...NEW_OP,
  10783. ...TRAIT_CONSUMES_SLOT,
  10784. numSlotsUsed: 2,
  10785. };
  10786. }
  10787. function createDeferOnOp(defer, trigger, modifier, sourceSpan) {
  10788. return {
  10789. kind: OpKind.DeferOn,
  10790. defer,
  10791. trigger,
  10792. modifier,
  10793. sourceSpan,
  10794. ...NEW_OP,
  10795. };
  10796. }
  10797. /**
  10798. * Creates a `DeclareLetOp`.
  10799. */
  10800. function createDeclareLetOp(xref, declaredName, sourceSpan) {
  10801. return {
  10802. kind: OpKind.DeclareLet,
  10803. xref,
  10804. declaredName,
  10805. sourceSpan,
  10806. handle: new SlotHandle(),
  10807. ...TRAIT_CONSUMES_SLOT,
  10808. ...NEW_OP,
  10809. };
  10810. }
  10811. /**
  10812. * Create an `ExtractedMessageOp`.
  10813. */
  10814. function createI18nMessageOp(xref, i18nContext, i18nBlock, message, messagePlaceholder, params, postprocessingParams, needsPostprocessing) {
  10815. return {
  10816. kind: OpKind.I18nMessage,
  10817. xref,
  10818. i18nContext,
  10819. i18nBlock,
  10820. message,
  10821. messagePlaceholder,
  10822. params,
  10823. postprocessingParams,
  10824. needsPostprocessing,
  10825. subMessages: [],
  10826. ...NEW_OP,
  10827. };
  10828. }
  10829. /**
  10830. * Create an `I18nStartOp`.
  10831. */
  10832. function createI18nStartOp(xref, message, root, sourceSpan) {
  10833. return {
  10834. kind: OpKind.I18nStart,
  10835. xref,
  10836. handle: new SlotHandle(),
  10837. root: root ?? xref,
  10838. message,
  10839. messageIndex: null,
  10840. subTemplateIndex: null,
  10841. context: null,
  10842. sourceSpan,
  10843. ...NEW_OP,
  10844. ...TRAIT_CONSUMES_SLOT,
  10845. };
  10846. }
  10847. /**
  10848. * Create an `I18nEndOp`.
  10849. */
  10850. function createI18nEndOp(xref, sourceSpan) {
  10851. return {
  10852. kind: OpKind.I18nEnd,
  10853. xref,
  10854. sourceSpan,
  10855. ...NEW_OP,
  10856. };
  10857. }
  10858. /**
  10859. * Creates an ICU start op.
  10860. */
  10861. function createIcuStartOp(xref, message, messagePlaceholder, sourceSpan) {
  10862. return {
  10863. kind: OpKind.IcuStart,
  10864. xref,
  10865. message,
  10866. messagePlaceholder,
  10867. context: null,
  10868. sourceSpan,
  10869. ...NEW_OP,
  10870. };
  10871. }
  10872. /**
  10873. * Creates an ICU end op.
  10874. */
  10875. function createIcuEndOp(xref) {
  10876. return {
  10877. kind: OpKind.IcuEnd,
  10878. xref,
  10879. ...NEW_OP,
  10880. };
  10881. }
  10882. /**
  10883. * Creates an ICU placeholder op.
  10884. */
  10885. function createIcuPlaceholderOp(xref, name, strings) {
  10886. return {
  10887. kind: OpKind.IcuPlaceholder,
  10888. xref,
  10889. name,
  10890. strings,
  10891. expressionPlaceholders: [],
  10892. ...NEW_OP,
  10893. };
  10894. }
  10895. function createI18nContextOp(contextKind, xref, i18nBlock, message, sourceSpan) {
  10896. if (i18nBlock === null && contextKind !== I18nContextKind.Attr) {
  10897. throw new Error('AssertionError: i18nBlock must be provided for non-attribute contexts.');
  10898. }
  10899. return {
  10900. kind: OpKind.I18nContext,
  10901. contextKind,
  10902. xref,
  10903. i18nBlock,
  10904. message,
  10905. sourceSpan,
  10906. params: new Map(),
  10907. postprocessingParams: new Map(),
  10908. ...NEW_OP,
  10909. };
  10910. }
  10911. function createI18nAttributesOp(xref, handle, target) {
  10912. return {
  10913. kind: OpKind.I18nAttributes,
  10914. xref,
  10915. handle,
  10916. target,
  10917. i18nAttributesConfig: null,
  10918. ...NEW_OP,
  10919. ...TRAIT_CONSUMES_SLOT,
  10920. };
  10921. }
  10922. /** Create a `SourceLocationOp`. */
  10923. function createSourceLocationOp(templatePath, locations) {
  10924. return {
  10925. kind: OpKind.SourceLocation,
  10926. templatePath,
  10927. locations,
  10928. ...NEW_OP,
  10929. };
  10930. }
  10931. function createHostPropertyOp(name, expression, isAnimationTrigger, i18nContext, securityContext, sourceSpan) {
  10932. return {
  10933. kind: OpKind.HostProperty,
  10934. name,
  10935. expression,
  10936. isAnimationTrigger,
  10937. i18nContext,
  10938. securityContext,
  10939. sanitizer: null,
  10940. sourceSpan,
  10941. ...TRAIT_CONSUMES_VARS,
  10942. ...NEW_OP,
  10943. };
  10944. }
  10945. /**
  10946. * When referenced in the template's context parameters, this indicates a reference to the entire
  10947. * context object, rather than a specific parameter.
  10948. */
  10949. const CTX_REF = 'CTX_REF_MARKER';
  10950. var CompilationJobKind;
  10951. (function (CompilationJobKind) {
  10952. CompilationJobKind[CompilationJobKind["Tmpl"] = 0] = "Tmpl";
  10953. CompilationJobKind[CompilationJobKind["Host"] = 1] = "Host";
  10954. CompilationJobKind[CompilationJobKind["Both"] = 2] = "Both";
  10955. })(CompilationJobKind || (CompilationJobKind = {}));
  10956. /**
  10957. * An entire ongoing compilation, which will result in one or more template functions when complete.
  10958. * Contains one or more corresponding compilation units.
  10959. */
  10960. class CompilationJob {
  10961. componentName;
  10962. pool;
  10963. compatibility;
  10964. constructor(componentName, pool, compatibility) {
  10965. this.componentName = componentName;
  10966. this.pool = pool;
  10967. this.compatibility = compatibility;
  10968. }
  10969. kind = CompilationJobKind.Both;
  10970. /**
  10971. * Generate a new unique `ir.XrefId` in this job.
  10972. */
  10973. allocateXrefId() {
  10974. return this.nextXrefId++;
  10975. }
  10976. /**
  10977. * Tracks the next `ir.XrefId` which can be assigned as template structures are ingested.
  10978. */
  10979. nextXrefId = 0;
  10980. }
  10981. /**
  10982. * Compilation-in-progress of a whole component's template, including the main template and any
  10983. * embedded views or host bindings.
  10984. */
  10985. class ComponentCompilationJob extends CompilationJob {
  10986. relativeContextFilePath;
  10987. i18nUseExternalIds;
  10988. deferMeta;
  10989. allDeferrableDepsFn;
  10990. relativeTemplatePath;
  10991. enableDebugLocations;
  10992. constructor(componentName, pool, compatibility, relativeContextFilePath, i18nUseExternalIds, deferMeta, allDeferrableDepsFn, relativeTemplatePath, enableDebugLocations) {
  10993. super(componentName, pool, compatibility);
  10994. this.relativeContextFilePath = relativeContextFilePath;
  10995. this.i18nUseExternalIds = i18nUseExternalIds;
  10996. this.deferMeta = deferMeta;
  10997. this.allDeferrableDepsFn = allDeferrableDepsFn;
  10998. this.relativeTemplatePath = relativeTemplatePath;
  10999. this.enableDebugLocations = enableDebugLocations;
  11000. this.root = new ViewCompilationUnit(this, this.allocateXrefId(), null);
  11001. this.views.set(this.root.xref, this.root);
  11002. }
  11003. kind = CompilationJobKind.Tmpl;
  11004. fnSuffix = 'Template';
  11005. /**
  11006. * The root view, representing the component's template.
  11007. */
  11008. root;
  11009. views = new Map();
  11010. /**
  11011. * Causes ngContentSelectors to be emitted, for content projection slots in the view. Possibly a
  11012. * reference into the constant pool.
  11013. */
  11014. contentSelectors = null;
  11015. /**
  11016. * Add a `ViewCompilation` for a new embedded view to this compilation.
  11017. */
  11018. allocateView(parent) {
  11019. const view = new ViewCompilationUnit(this, this.allocateXrefId(), parent);
  11020. this.views.set(view.xref, view);
  11021. return view;
  11022. }
  11023. get units() {
  11024. return this.views.values();
  11025. }
  11026. /**
  11027. * Add a constant `o.Expression` to the compilation and return its index in the `consts` array.
  11028. */
  11029. addConst(newConst, initializers) {
  11030. for (let idx = 0; idx < this.consts.length; idx++) {
  11031. if (this.consts[idx].isEquivalent(newConst)) {
  11032. return idx;
  11033. }
  11034. }
  11035. const idx = this.consts.length;
  11036. this.consts.push(newConst);
  11037. if (initializers) {
  11038. this.constsInitializers.push(...initializers);
  11039. }
  11040. return idx;
  11041. }
  11042. /**
  11043. * Constant expressions used by operations within this component's compilation.
  11044. *
  11045. * This will eventually become the `consts` array in the component definition.
  11046. */
  11047. consts = [];
  11048. /**
  11049. * Initialization statements needed to set up the consts.
  11050. */
  11051. constsInitializers = [];
  11052. }
  11053. /**
  11054. * A compilation unit is compiled into a template function. Some example units are views and host
  11055. * bindings.
  11056. */
  11057. class CompilationUnit {
  11058. xref;
  11059. constructor(xref) {
  11060. this.xref = xref;
  11061. }
  11062. /**
  11063. * List of creation operations for this view.
  11064. *
  11065. * Creation operations may internally contain other operations, including update operations.
  11066. */
  11067. create = new OpList();
  11068. /**
  11069. * List of update operations for this view.
  11070. */
  11071. update = new OpList();
  11072. /**
  11073. * Name of the function which will be generated for this unit.
  11074. *
  11075. * May be `null` if not yet determined.
  11076. */
  11077. fnName = null;
  11078. /**
  11079. * Number of variable slots used within this view, or `null` if variables have not yet been
  11080. * counted.
  11081. */
  11082. vars = null;
  11083. /**
  11084. * Iterate over all `ir.Op`s within this view.
  11085. *
  11086. * Some operations may have child operations, which this iterator will visit.
  11087. */
  11088. *ops() {
  11089. for (const op of this.create) {
  11090. yield op;
  11091. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  11092. for (const listenerOp of op.handlerOps) {
  11093. yield listenerOp;
  11094. }
  11095. }
  11096. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  11097. for (const trackOp of op.trackByOps) {
  11098. yield trackOp;
  11099. }
  11100. }
  11101. }
  11102. for (const op of this.update) {
  11103. yield op;
  11104. }
  11105. }
  11106. }
  11107. /**
  11108. * Compilation-in-progress of an individual view within a template.
  11109. */
  11110. class ViewCompilationUnit extends CompilationUnit {
  11111. job;
  11112. parent;
  11113. constructor(job, xref, parent) {
  11114. super(xref);
  11115. this.job = job;
  11116. this.parent = parent;
  11117. }
  11118. /**
  11119. * Map of declared variables available within this view to the property on the context object
  11120. * which they alias.
  11121. */
  11122. contextVariables = new Map();
  11123. /**
  11124. * Set of aliases available within this view. An alias is a variable whose provided expression is
  11125. * inlined at every location it is used. It may also depend on context variables, by name.
  11126. */
  11127. aliases = new Set();
  11128. /**
  11129. * Number of declaration slots used within this view, or `null` if slots have not yet been
  11130. * allocated.
  11131. */
  11132. decls = null;
  11133. }
  11134. /**
  11135. * Compilation-in-progress of a host binding, which contains a single unit for that host binding.
  11136. */
  11137. class HostBindingCompilationJob extends CompilationJob {
  11138. constructor(componentName, pool, compatibility) {
  11139. super(componentName, pool, compatibility);
  11140. this.root = new HostBindingCompilationUnit(this);
  11141. }
  11142. kind = CompilationJobKind.Host;
  11143. fnSuffix = 'HostBindings';
  11144. root;
  11145. get units() {
  11146. return [this.root];
  11147. }
  11148. }
  11149. class HostBindingCompilationUnit extends CompilationUnit {
  11150. job;
  11151. constructor(job) {
  11152. super(0);
  11153. this.job = job;
  11154. }
  11155. /**
  11156. * Much like an element can have attributes, so can a host binding function.
  11157. */
  11158. attributes = null;
  11159. }
  11160. /**
  11161. * Find any function calls to `$any`, excluding `this.$any`, and delete them, since they have no
  11162. * runtime effects.
  11163. */
  11164. function deleteAnyCasts(job) {
  11165. for (const unit of job.units) {
  11166. for (const op of unit.ops()) {
  11167. transformExpressionsInOp(op, removeAnys, VisitorContextFlag.None);
  11168. }
  11169. }
  11170. }
  11171. function removeAnys(e) {
  11172. if (e instanceof InvokeFunctionExpr &&
  11173. e.fn instanceof LexicalReadExpr &&
  11174. e.fn.name === '$any') {
  11175. if (e.args.length !== 1) {
  11176. throw new Error('The $any builtin function expects exactly one argument.');
  11177. }
  11178. return e.args[0];
  11179. }
  11180. return e;
  11181. }
  11182. /**
  11183. * Adds apply operations after i18n expressions.
  11184. */
  11185. function applyI18nExpressions(job) {
  11186. const i18nContexts = new Map();
  11187. for (const unit of job.units) {
  11188. for (const op of unit.create) {
  11189. if (op.kind === OpKind.I18nContext) {
  11190. i18nContexts.set(op.xref, op);
  11191. }
  11192. }
  11193. }
  11194. for (const unit of job.units) {
  11195. for (const op of unit.update) {
  11196. // Only add apply after expressions that are not followed by more expressions.
  11197. if (op.kind === OpKind.I18nExpression && needsApplication(i18nContexts, op)) {
  11198. // TODO: what should be the source span for the apply op?
  11199. OpList.insertAfter(createI18nApplyOp(op.i18nOwner, op.handle, null), op);
  11200. }
  11201. }
  11202. }
  11203. }
  11204. /**
  11205. * Checks whether the given expression op needs to be followed with an apply op.
  11206. */
  11207. function needsApplication(i18nContexts, op) {
  11208. // If the next op is not another expression, we need to apply.
  11209. if (op.next?.kind !== OpKind.I18nExpression) {
  11210. return true;
  11211. }
  11212. const context = i18nContexts.get(op.context);
  11213. const nextContext = i18nContexts.get(op.next.context);
  11214. if (context === undefined) {
  11215. throw new Error("AssertionError: expected an I18nContextOp to exist for the I18nExpressionOp's context");
  11216. }
  11217. if (nextContext === undefined) {
  11218. throw new Error("AssertionError: expected an I18nContextOp to exist for the next I18nExpressionOp's context");
  11219. }
  11220. // If the next op is an expression targeting a different i18n block (or different element, in the
  11221. // case of i18n attributes), we need to apply.
  11222. // First, handle the case of i18n blocks.
  11223. if (context.i18nBlock !== null) {
  11224. // This is a block context. Compare the blocks.
  11225. if (context.i18nBlock !== nextContext.i18nBlock) {
  11226. return true;
  11227. }
  11228. return false;
  11229. }
  11230. // Second, handle the case of i18n attributes.
  11231. if (op.i18nOwner !== op.next.i18nOwner) {
  11232. return true;
  11233. }
  11234. return false;
  11235. }
  11236. /**
  11237. * Updates i18n expression ops to target the last slot in their owning i18n block, and moves them
  11238. * after the last update instruction that depends on that slot.
  11239. */
  11240. function assignI18nSlotDependencies(job) {
  11241. for (const unit of job.units) {
  11242. // The first update op.
  11243. let updateOp = unit.update.head;
  11244. // I18n expressions currently being moved during the iteration.
  11245. let i18nExpressionsInProgress = [];
  11246. // Non-null while we are iterating through an i18nStart/i18nEnd pair
  11247. let state = null;
  11248. for (const createOp of unit.create) {
  11249. if (createOp.kind === OpKind.I18nStart) {
  11250. state = {
  11251. blockXref: createOp.xref,
  11252. lastSlotConsumer: createOp.xref,
  11253. };
  11254. }
  11255. else if (createOp.kind === OpKind.I18nEnd) {
  11256. for (const op of i18nExpressionsInProgress) {
  11257. op.target = state.lastSlotConsumer;
  11258. OpList.insertBefore(op, updateOp);
  11259. }
  11260. i18nExpressionsInProgress.length = 0;
  11261. state = null;
  11262. }
  11263. if (hasConsumesSlotTrait(createOp)) {
  11264. if (state !== null) {
  11265. state.lastSlotConsumer = createOp.xref;
  11266. }
  11267. while (true) {
  11268. if (updateOp.next === null) {
  11269. break;
  11270. }
  11271. if (state !== null &&
  11272. updateOp.kind === OpKind.I18nExpression &&
  11273. updateOp.usage === I18nExpressionFor.I18nText &&
  11274. updateOp.i18nOwner === state.blockXref) {
  11275. const opToRemove = updateOp;
  11276. updateOp = updateOp.next;
  11277. OpList.remove(opToRemove);
  11278. i18nExpressionsInProgress.push(opToRemove);
  11279. continue;
  11280. }
  11281. if (hasDependsOnSlotContextTrait(updateOp) && updateOp.target !== createOp.xref) {
  11282. break;
  11283. }
  11284. updateOp = updateOp.next;
  11285. }
  11286. }
  11287. }
  11288. }
  11289. }
  11290. /**
  11291. * Gets a map of all elements in the given view by their xref id.
  11292. */
  11293. function createOpXrefMap(unit) {
  11294. const map = new Map();
  11295. for (const op of unit.create) {
  11296. if (!hasConsumesSlotTrait(op)) {
  11297. continue;
  11298. }
  11299. map.set(op.xref, op);
  11300. // TODO(dylhunn): `@for` loops with `@empty` blocks need to be special-cased here,
  11301. // because the slot consumer trait currently only supports one slot per consumer and we
  11302. // need two. This should be revisited when making the refactors mentioned in:
  11303. // https://github.com/angular/angular/pull/53620#discussion_r1430918822
  11304. if (op.kind === OpKind.RepeaterCreate && op.emptyView !== null) {
  11305. map.set(op.emptyView, op);
  11306. }
  11307. }
  11308. return map;
  11309. }
  11310. /**
  11311. * Find all extractable attribute and binding ops, and create ExtractedAttributeOps for them.
  11312. * In cases where no instruction needs to be generated for the attribute or binding, it is removed.
  11313. */
  11314. function extractAttributes(job) {
  11315. for (const unit of job.units) {
  11316. const elements = createOpXrefMap(unit);
  11317. for (const op of unit.ops()) {
  11318. switch (op.kind) {
  11319. case OpKind.Attribute:
  11320. extractAttributeOp(unit, op, elements);
  11321. break;
  11322. case OpKind.Property:
  11323. if (!op.isAnimationTrigger) {
  11324. let bindingKind;
  11325. if (op.i18nMessage !== null && op.templateKind === null) {
  11326. // If the binding has an i18n context, it is an i18n attribute, and should have that
  11327. // kind in the consts array.
  11328. bindingKind = BindingKind.I18n;
  11329. }
  11330. else if (op.isStructuralTemplateAttribute) {
  11331. bindingKind = BindingKind.Template;
  11332. }
  11333. else {
  11334. bindingKind = BindingKind.Property;
  11335. }
  11336. OpList.insertBefore(
  11337. // Deliberately null i18nMessage value
  11338. createExtractedAttributeOp(op.target, bindingKind, null, op.name,
  11339. /* expression */ null,
  11340. /* i18nContext */ null,
  11341. /* i18nMessage */ null, op.securityContext), lookupElement$2(elements, op.target));
  11342. }
  11343. break;
  11344. case OpKind.TwoWayProperty:
  11345. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.TwoWayProperty, null, op.name,
  11346. /* expression */ null,
  11347. /* i18nContext */ null,
  11348. /* i18nMessage */ null, op.securityContext), lookupElement$2(elements, op.target));
  11349. break;
  11350. case OpKind.StyleProp:
  11351. case OpKind.ClassProp:
  11352. // TODO: Can style or class bindings be i18n attributes?
  11353. // The old compiler treated empty style bindings as regular bindings for the purpose of
  11354. // directive matching. That behavior is incorrect, but we emulate it in compatibility
  11355. // mode.
  11356. if (unit.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  11357. op.expression instanceof EmptyExpr) {
  11358. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.Property, null, op.name,
  11359. /* expression */ null,
  11360. /* i18nContext */ null,
  11361. /* i18nMessage */ null, SecurityContext.STYLE), lookupElement$2(elements, op.target));
  11362. }
  11363. break;
  11364. case OpKind.Listener:
  11365. if (!op.isAnimationListener) {
  11366. const extractedAttributeOp = createExtractedAttributeOp(op.target, BindingKind.Property, null, op.name,
  11367. /* expression */ null,
  11368. /* i18nContext */ null,
  11369. /* i18nMessage */ null, SecurityContext.NONE);
  11370. if (job.kind === CompilationJobKind.Host) {
  11371. if (job.compatibility) {
  11372. // TemplateDefinitionBuilder does not extract listener bindings to the const array
  11373. // (which is honestly pretty inconsistent).
  11374. break;
  11375. }
  11376. // This attribute will apply to the enclosing host binding compilation unit, so order
  11377. // doesn't matter.
  11378. unit.create.push(extractedAttributeOp);
  11379. }
  11380. else {
  11381. OpList.insertBefore(extractedAttributeOp, lookupElement$2(elements, op.target));
  11382. }
  11383. }
  11384. break;
  11385. case OpKind.TwoWayListener:
  11386. // Two-way listeners aren't supported in host bindings.
  11387. if (job.kind !== CompilationJobKind.Host) {
  11388. const extractedAttributeOp = createExtractedAttributeOp(op.target, BindingKind.Property, null, op.name,
  11389. /* expression */ null,
  11390. /* i18nContext */ null,
  11391. /* i18nMessage */ null, SecurityContext.NONE);
  11392. OpList.insertBefore(extractedAttributeOp, lookupElement$2(elements, op.target));
  11393. }
  11394. break;
  11395. }
  11396. }
  11397. }
  11398. }
  11399. /**
  11400. * Looks up an element in the given map by xref ID.
  11401. */
  11402. function lookupElement$2(elements, xref) {
  11403. const el = elements.get(xref);
  11404. if (el === undefined) {
  11405. throw new Error('All attributes should have an element-like target.');
  11406. }
  11407. return el;
  11408. }
  11409. /**
  11410. * Extracts an attribute binding.
  11411. */
  11412. function extractAttributeOp(unit, op, elements) {
  11413. if (op.expression instanceof Interpolation) {
  11414. return;
  11415. }
  11416. let extractable = op.isTextAttribute || op.expression.isConstant();
  11417. if (unit.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  11418. // TemplateDefinitionBuilder only extracts text attributes. It does not extract attriibute
  11419. // bindings, even if they are constants.
  11420. extractable &&= op.isTextAttribute;
  11421. }
  11422. if (extractable) {
  11423. const extractedAttributeOp = createExtractedAttributeOp(op.target, op.isStructuralTemplateAttribute ? BindingKind.Template : BindingKind.Attribute, op.namespace, op.name, op.expression, op.i18nContext, op.i18nMessage, op.securityContext);
  11424. if (unit.job.kind === CompilationJobKind.Host) {
  11425. // This attribute will apply to the enclosing host binding compilation unit, so order doesn't
  11426. // matter.
  11427. unit.create.push(extractedAttributeOp);
  11428. }
  11429. else {
  11430. const ownerOp = lookupElement$2(elements, op.target);
  11431. OpList.insertBefore(extractedAttributeOp, ownerOp);
  11432. }
  11433. OpList.remove(op);
  11434. }
  11435. }
  11436. /**
  11437. * Looks up an element in the given map by xref ID.
  11438. */
  11439. function lookupElement$1(elements, xref) {
  11440. const el = elements.get(xref);
  11441. if (el === undefined) {
  11442. throw new Error('All attributes should have an element-like target.');
  11443. }
  11444. return el;
  11445. }
  11446. function specializeBindings(job) {
  11447. const elements = new Map();
  11448. for (const unit of job.units) {
  11449. for (const op of unit.create) {
  11450. if (!isElementOrContainerOp(op)) {
  11451. continue;
  11452. }
  11453. elements.set(op.xref, op);
  11454. }
  11455. }
  11456. for (const unit of job.units) {
  11457. for (const op of unit.ops()) {
  11458. if (op.kind !== OpKind.Binding) {
  11459. continue;
  11460. }
  11461. switch (op.bindingKind) {
  11462. case BindingKind.Attribute:
  11463. if (op.name === 'ngNonBindable') {
  11464. OpList.remove(op);
  11465. const target = lookupElement$1(elements, op.target);
  11466. target.nonBindable = true;
  11467. }
  11468. else {
  11469. const [namespace, name] = splitNsName(op.name);
  11470. OpList.replace(op, createAttributeOp(op.target, namespace, name, op.expression, op.securityContext, op.isTextAttribute, op.isStructuralTemplateAttribute, op.templateKind, op.i18nMessage, op.sourceSpan));
  11471. }
  11472. break;
  11473. case BindingKind.Property:
  11474. case BindingKind.Animation:
  11475. if (job.kind === CompilationJobKind.Host) {
  11476. OpList.replace(op, createHostPropertyOp(op.name, op.expression, op.bindingKind === BindingKind.Animation, op.i18nContext, op.securityContext, op.sourceSpan));
  11477. }
  11478. else {
  11479. OpList.replace(op, createPropertyOp(op.target, op.name, op.expression, op.bindingKind === BindingKind.Animation, op.securityContext, op.isStructuralTemplateAttribute, op.templateKind, op.i18nContext, op.i18nMessage, op.sourceSpan));
  11480. }
  11481. break;
  11482. case BindingKind.TwoWayProperty:
  11483. if (!(op.expression instanceof Expression)) {
  11484. // We shouldn't be able to hit this code path since interpolations in two-way bindings
  11485. // result in a parser error. We assert here so that downstream we can assume that
  11486. // the value is always an expression.
  11487. throw new Error(`Expected value of two-way property binding "${op.name}" to be an expression`);
  11488. }
  11489. OpList.replace(op, createTwoWayPropertyOp(op.target, op.name, op.expression, op.securityContext, op.isStructuralTemplateAttribute, op.templateKind, op.i18nContext, op.i18nMessage, op.sourceSpan));
  11490. break;
  11491. case BindingKind.I18n:
  11492. case BindingKind.ClassName:
  11493. case BindingKind.StyleProperty:
  11494. throw new Error(`Unhandled binding of kind ${BindingKind[op.bindingKind]}`);
  11495. }
  11496. }
  11497. }
  11498. }
  11499. const CHAINABLE = new Set([
  11500. Identifiers.attribute,
  11501. Identifiers.classProp,
  11502. Identifiers.element,
  11503. Identifiers.elementContainer,
  11504. Identifiers.elementContainerEnd,
  11505. Identifiers.elementContainerStart,
  11506. Identifiers.elementEnd,
  11507. Identifiers.elementStart,
  11508. Identifiers.hostProperty,
  11509. Identifiers.i18nExp,
  11510. Identifiers.listener,
  11511. Identifiers.listener,
  11512. Identifiers.property,
  11513. Identifiers.styleProp,
  11514. Identifiers.stylePropInterpolate1,
  11515. Identifiers.stylePropInterpolate2,
  11516. Identifiers.stylePropInterpolate3,
  11517. Identifiers.stylePropInterpolate4,
  11518. Identifiers.stylePropInterpolate5,
  11519. Identifiers.stylePropInterpolate6,
  11520. Identifiers.stylePropInterpolate7,
  11521. Identifiers.stylePropInterpolate8,
  11522. Identifiers.stylePropInterpolateV,
  11523. Identifiers.syntheticHostListener,
  11524. Identifiers.syntheticHostProperty,
  11525. Identifiers.templateCreate,
  11526. Identifiers.twoWayProperty,
  11527. Identifiers.twoWayListener,
  11528. Identifiers.declareLet,
  11529. ]);
  11530. /**
  11531. * Chaining results in repeated call expressions, causing a deep AST of receiver expressions. To prevent running out of
  11532. * stack depth the maximum number of chained instructions is limited to this threshold, which has been selected
  11533. * arbitrarily.
  11534. */
  11535. const MAX_CHAIN_LENGTH = 256;
  11536. /**
  11537. * Post-process a reified view compilation and convert sequential calls to chainable instructions
  11538. * into chain calls.
  11539. *
  11540. * For example, two `elementStart` operations in sequence:
  11541. *
  11542. * ```ts
  11543. * elementStart(0, 'div');
  11544. * elementStart(1, 'span');
  11545. * ```
  11546. *
  11547. * Can be called as a chain instead:
  11548. *
  11549. * ```ts
  11550. * elementStart(0, 'div')(1, 'span');
  11551. * ```
  11552. */
  11553. function chain(job) {
  11554. for (const unit of job.units) {
  11555. chainOperationsInList(unit.create);
  11556. chainOperationsInList(unit.update);
  11557. }
  11558. }
  11559. function chainOperationsInList(opList) {
  11560. let chain = null;
  11561. for (const op of opList) {
  11562. if (op.kind !== OpKind.Statement || !(op.statement instanceof ExpressionStatement)) {
  11563. // This type of statement isn't chainable.
  11564. chain = null;
  11565. continue;
  11566. }
  11567. if (!(op.statement.expr instanceof InvokeFunctionExpr) ||
  11568. !(op.statement.expr.fn instanceof ExternalExpr)) {
  11569. // This is a statement, but not an instruction-type call, so not chainable.
  11570. chain = null;
  11571. continue;
  11572. }
  11573. const instruction = op.statement.expr.fn.value;
  11574. if (!CHAINABLE.has(instruction)) {
  11575. // This instruction isn't chainable.
  11576. chain = null;
  11577. continue;
  11578. }
  11579. // This instruction can be chained. It can either be added on to the previous chain (if
  11580. // compatible) or it can be the start of a new chain.
  11581. if (chain !== null && chain.instruction === instruction && chain.length < MAX_CHAIN_LENGTH) {
  11582. // This instruction can be added onto the previous chain.
  11583. const expression = chain.expression.callFn(op.statement.expr.args, op.statement.expr.sourceSpan, op.statement.expr.pure);
  11584. chain.expression = expression;
  11585. chain.op.statement = expression.toStmt();
  11586. chain.length++;
  11587. OpList.remove(op);
  11588. }
  11589. else {
  11590. // Leave this instruction alone for now, but consider it the start of a new chain.
  11591. chain = {
  11592. op,
  11593. instruction,
  11594. expression: op.statement.expr,
  11595. length: 1,
  11596. };
  11597. }
  11598. }
  11599. }
  11600. /**
  11601. * Attribute interpolations of the form `[attr.foo]="{{foo}}""` should be "collapsed" into a plain
  11602. * attribute instruction, instead of an `attributeInterpolate` instruction.
  11603. *
  11604. * (We cannot do this for singleton property interpolations, because `propertyInterpolate`
  11605. * stringifies its expression.)
  11606. *
  11607. * The reification step is also capable of performing this transformation, but doing it early in the
  11608. * pipeline allows other phases to accurately know what instruction will be emitted.
  11609. */
  11610. function collapseSingletonInterpolations(job) {
  11611. for (const unit of job.units) {
  11612. for (const op of unit.update) {
  11613. const eligibleOpKind = op.kind === OpKind.Attribute;
  11614. if (eligibleOpKind &&
  11615. op.expression instanceof Interpolation &&
  11616. op.expression.strings.length === 2 &&
  11617. op.expression.strings.every((s) => s === '')) {
  11618. op.expression = op.expression.expressions[0];
  11619. }
  11620. }
  11621. }
  11622. }
  11623. /**
  11624. * Collapse the various conditions of conditional ops (if, switch) into a single test expression.
  11625. */
  11626. function generateConditionalExpressions(job) {
  11627. for (const unit of job.units) {
  11628. for (const op of unit.ops()) {
  11629. if (op.kind !== OpKind.Conditional) {
  11630. continue;
  11631. }
  11632. let test;
  11633. // Any case with a `null` condition is `default`. If one exists, default to it instead.
  11634. const defaultCase = op.conditions.findIndex((cond) => cond.expr === null);
  11635. if (defaultCase >= 0) {
  11636. const slot = op.conditions.splice(defaultCase, 1)[0].targetSlot;
  11637. test = new SlotLiteralExpr(slot);
  11638. }
  11639. else {
  11640. // By default, a switch evaluates to `-1`, causing no template to be displayed.
  11641. test = literal$1(-1);
  11642. }
  11643. // Switch expressions assign their main test to a temporary, to avoid re-executing it.
  11644. let tmp = op.test == null ? null : new AssignTemporaryExpr(op.test, job.allocateXrefId());
  11645. // For each remaining condition, test whether the temporary satifies the check. (If no temp is
  11646. // present, just check each expression directly.)
  11647. for (let i = op.conditions.length - 1; i >= 0; i--) {
  11648. let conditionalCase = op.conditions[i];
  11649. if (conditionalCase.expr === null) {
  11650. continue;
  11651. }
  11652. if (tmp !== null) {
  11653. const useTmp = i === 0 ? tmp : new ReadTemporaryExpr(tmp.xref);
  11654. conditionalCase.expr = new BinaryOperatorExpr(BinaryOperator.Identical, useTmp, conditionalCase.expr);
  11655. }
  11656. else if (conditionalCase.alias !== null) {
  11657. const caseExpressionTemporaryXref = job.allocateXrefId();
  11658. conditionalCase.expr = new AssignTemporaryExpr(conditionalCase.expr, caseExpressionTemporaryXref);
  11659. op.contextValue = new ReadTemporaryExpr(caseExpressionTemporaryXref);
  11660. }
  11661. test = new ConditionalExpr(conditionalCase.expr, new SlotLiteralExpr(conditionalCase.targetSlot), test);
  11662. }
  11663. // Save the resulting aggregate Joost-expression.
  11664. op.processed = test;
  11665. // Clear the original conditions array, since we no longer need it, and don't want it to
  11666. // affect subsequent phases (e.g. pipe creation).
  11667. op.conditions = [];
  11668. }
  11669. }
  11670. }
  11671. const BINARY_OPERATORS$3 = new Map([
  11672. ['&&', BinaryOperator.And],
  11673. ['>', BinaryOperator.Bigger],
  11674. ['>=', BinaryOperator.BiggerEquals],
  11675. ['|', BinaryOperator.BitwiseOr],
  11676. ['&', BinaryOperator.BitwiseAnd],
  11677. ['/', BinaryOperator.Divide],
  11678. ['==', BinaryOperator.Equals],
  11679. ['===', BinaryOperator.Identical],
  11680. ['<', BinaryOperator.Lower],
  11681. ['<=', BinaryOperator.LowerEquals],
  11682. ['-', BinaryOperator.Minus],
  11683. ['%', BinaryOperator.Modulo],
  11684. ['*', BinaryOperator.Multiply],
  11685. ['!=', BinaryOperator.NotEquals],
  11686. ['!==', BinaryOperator.NotIdentical],
  11687. ['??', BinaryOperator.NullishCoalesce],
  11688. ['||', BinaryOperator.Or],
  11689. ['+', BinaryOperator.Plus],
  11690. ]);
  11691. function namespaceForKey(namespacePrefixKey) {
  11692. const NAMESPACES = new Map([
  11693. ['svg', Namespace.SVG],
  11694. ['math', Namespace.Math],
  11695. ]);
  11696. if (namespacePrefixKey === null) {
  11697. return Namespace.HTML;
  11698. }
  11699. return NAMESPACES.get(namespacePrefixKey) ?? Namespace.HTML;
  11700. }
  11701. function keyForNamespace(namespace) {
  11702. const NAMESPACES = new Map([
  11703. ['svg', Namespace.SVG],
  11704. ['math', Namespace.Math],
  11705. ]);
  11706. for (const [k, n] of NAMESPACES.entries()) {
  11707. if (n === namespace) {
  11708. return k;
  11709. }
  11710. }
  11711. return null; // No namespace prefix for HTML
  11712. }
  11713. function prefixWithNamespace(strippedTag, namespace) {
  11714. if (namespace === Namespace.HTML) {
  11715. return strippedTag;
  11716. }
  11717. return `:${keyForNamespace(namespace)}:${strippedTag}`;
  11718. }
  11719. function literalOrArrayLiteral(value) {
  11720. if (Array.isArray(value)) {
  11721. return literalArr(value.map(literalOrArrayLiteral));
  11722. }
  11723. return literal$1(value);
  11724. }
  11725. /**
  11726. * Converts the semantic attributes of element-like operations (elements, templates) into constant
  11727. * array expressions, and lifts them into the overall component `consts`.
  11728. */
  11729. function collectElementConsts(job) {
  11730. // Collect all extracted attributes.
  11731. const allElementAttributes = new Map();
  11732. for (const unit of job.units) {
  11733. for (const op of unit.create) {
  11734. if (op.kind === OpKind.ExtractedAttribute) {
  11735. const attributes = allElementAttributes.get(op.target) || new ElementAttributes(job.compatibility);
  11736. allElementAttributes.set(op.target, attributes);
  11737. attributes.add(op.bindingKind, op.name, op.expression, op.namespace, op.trustedValueFn);
  11738. OpList.remove(op);
  11739. }
  11740. }
  11741. }
  11742. // Serialize the extracted attributes into the const array.
  11743. if (job instanceof ComponentCompilationJob) {
  11744. for (const unit of job.units) {
  11745. for (const op of unit.create) {
  11746. // TODO: Simplify and combine these cases.
  11747. if (op.kind == OpKind.Projection) {
  11748. const attributes = allElementAttributes.get(op.xref);
  11749. if (attributes !== undefined) {
  11750. const attrArray = serializeAttributes(attributes);
  11751. if (attrArray.entries.length > 0) {
  11752. op.attributes = attrArray;
  11753. }
  11754. }
  11755. }
  11756. else if (isElementOrContainerOp(op)) {
  11757. op.attributes = getConstIndex(job, allElementAttributes, op.xref);
  11758. // TODO(dylhunn): `@for` loops with `@empty` blocks need to be special-cased here,
  11759. // because the slot consumer trait currently only supports one slot per consumer and we
  11760. // need two. This should be revisited when making the refactors mentioned in:
  11761. // https://github.com/angular/angular/pull/53620#discussion_r1430918822
  11762. if (op.kind === OpKind.RepeaterCreate && op.emptyView !== null) {
  11763. op.emptyAttributes = getConstIndex(job, allElementAttributes, op.emptyView);
  11764. }
  11765. }
  11766. }
  11767. }
  11768. }
  11769. else if (job instanceof HostBindingCompilationJob) {
  11770. // TODO: If the host binding case further diverges, we may want to split it into its own
  11771. // phase.
  11772. for (const [xref, attributes] of allElementAttributes.entries()) {
  11773. if (xref !== job.root.xref) {
  11774. throw new Error(`An attribute would be const collected into the host binding's template function, but is not associated with the root xref.`);
  11775. }
  11776. const attrArray = serializeAttributes(attributes);
  11777. if (attrArray.entries.length > 0) {
  11778. job.root.attributes = attrArray;
  11779. }
  11780. }
  11781. }
  11782. }
  11783. function getConstIndex(job, allElementAttributes, xref) {
  11784. const attributes = allElementAttributes.get(xref);
  11785. if (attributes !== undefined) {
  11786. const attrArray = serializeAttributes(attributes);
  11787. if (attrArray.entries.length > 0) {
  11788. return job.addConst(attrArray);
  11789. }
  11790. }
  11791. return null;
  11792. }
  11793. /**
  11794. * Shared instance of an empty array to avoid unnecessary array allocations.
  11795. */
  11796. const FLYWEIGHT_ARRAY = Object.freeze([]);
  11797. /**
  11798. * Container for all of the various kinds of attributes which are applied on an element.
  11799. */
  11800. class ElementAttributes {
  11801. compatibility;
  11802. known = new Map();
  11803. byKind = new Map();
  11804. propertyBindings = null;
  11805. projectAs = null;
  11806. get attributes() {
  11807. return this.byKind.get(BindingKind.Attribute) ?? FLYWEIGHT_ARRAY;
  11808. }
  11809. get classes() {
  11810. return this.byKind.get(BindingKind.ClassName) ?? FLYWEIGHT_ARRAY;
  11811. }
  11812. get styles() {
  11813. return this.byKind.get(BindingKind.StyleProperty) ?? FLYWEIGHT_ARRAY;
  11814. }
  11815. get bindings() {
  11816. return this.propertyBindings ?? FLYWEIGHT_ARRAY;
  11817. }
  11818. get template() {
  11819. return this.byKind.get(BindingKind.Template) ?? FLYWEIGHT_ARRAY;
  11820. }
  11821. get i18n() {
  11822. return this.byKind.get(BindingKind.I18n) ?? FLYWEIGHT_ARRAY;
  11823. }
  11824. constructor(compatibility) {
  11825. this.compatibility = compatibility;
  11826. }
  11827. isKnown(kind, name) {
  11828. const nameToValue = this.known.get(kind) ?? new Set();
  11829. this.known.set(kind, nameToValue);
  11830. if (nameToValue.has(name)) {
  11831. return true;
  11832. }
  11833. nameToValue.add(name);
  11834. return false;
  11835. }
  11836. add(kind, name, value, namespace, trustedValueFn) {
  11837. // TemplateDefinitionBuilder puts duplicate attribute, class, and style values into the consts
  11838. // array. This seems inefficient, we can probably keep just the first one or the last value
  11839. // (whichever actually gets applied when multiple values are listed for the same attribute).
  11840. const allowDuplicates = this.compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  11841. (kind === BindingKind.Attribute ||
  11842. kind === BindingKind.ClassName ||
  11843. kind === BindingKind.StyleProperty);
  11844. if (!allowDuplicates && this.isKnown(kind, name)) {
  11845. return;
  11846. }
  11847. // TODO: Can this be its own phase
  11848. if (name === 'ngProjectAs') {
  11849. if (value === null ||
  11850. !(value instanceof LiteralExpr) ||
  11851. value.value == null ||
  11852. typeof value.value?.toString() !== 'string') {
  11853. throw Error('ngProjectAs must have a string literal value');
  11854. }
  11855. this.projectAs = value.value.toString();
  11856. // TODO: TemplateDefinitionBuilder allows `ngProjectAs` to also be assigned as a literal
  11857. // attribute. Is this sane?
  11858. }
  11859. const array = this.arrayFor(kind);
  11860. array.push(...getAttributeNameLiterals(namespace, name));
  11861. if (kind === BindingKind.Attribute || kind === BindingKind.StyleProperty) {
  11862. if (value === null) {
  11863. throw Error('Attribute, i18n attribute, & style element attributes must have a value');
  11864. }
  11865. if (trustedValueFn !== null) {
  11866. if (!isStringLiteral(value)) {
  11867. throw Error('AssertionError: extracted attribute value should be string literal');
  11868. }
  11869. array.push(taggedTemplate(trustedValueFn, new TemplateLiteralExpr([new TemplateLiteralElementExpr(value.value)], []), undefined, value.sourceSpan));
  11870. }
  11871. else {
  11872. array.push(value);
  11873. }
  11874. }
  11875. }
  11876. arrayFor(kind) {
  11877. if (kind === BindingKind.Property || kind === BindingKind.TwoWayProperty) {
  11878. this.propertyBindings ??= [];
  11879. return this.propertyBindings;
  11880. }
  11881. else {
  11882. if (!this.byKind.has(kind)) {
  11883. this.byKind.set(kind, []);
  11884. }
  11885. return this.byKind.get(kind);
  11886. }
  11887. }
  11888. }
  11889. /**
  11890. * Gets an array of literal expressions representing the attribute's namespaced name.
  11891. */
  11892. function getAttributeNameLiterals(namespace, name) {
  11893. const nameLiteral = literal$1(name);
  11894. if (namespace) {
  11895. return [literal$1(0 /* core.AttributeMarker.NamespaceURI */), literal$1(namespace), nameLiteral];
  11896. }
  11897. return [nameLiteral];
  11898. }
  11899. /**
  11900. * Serializes an ElementAttributes object into an array expression.
  11901. */
  11902. function serializeAttributes({ attributes, bindings, classes, i18n, projectAs, styles, template, }) {
  11903. const attrArray = [...attributes];
  11904. if (projectAs !== null) {
  11905. // Parse the attribute value into a CssSelectorList. Note that we only take the
  11906. // first selector, because we don't support multiple selectors in ngProjectAs.
  11907. const parsedR3Selector = parseSelectorToR3Selector(projectAs)[0];
  11908. attrArray.push(literal$1(5 /* core.AttributeMarker.ProjectAs */), literalOrArrayLiteral(parsedR3Selector));
  11909. }
  11910. if (classes.length > 0) {
  11911. attrArray.push(literal$1(1 /* core.AttributeMarker.Classes */), ...classes);
  11912. }
  11913. if (styles.length > 0) {
  11914. attrArray.push(literal$1(2 /* core.AttributeMarker.Styles */), ...styles);
  11915. }
  11916. if (bindings.length > 0) {
  11917. attrArray.push(literal$1(3 /* core.AttributeMarker.Bindings */), ...bindings);
  11918. }
  11919. if (template.length > 0) {
  11920. attrArray.push(literal$1(4 /* core.AttributeMarker.Template */), ...template);
  11921. }
  11922. if (i18n.length > 0) {
  11923. attrArray.push(literal$1(6 /* core.AttributeMarker.I18n */), ...i18n);
  11924. }
  11925. return literalArr(attrArray);
  11926. }
  11927. /**
  11928. * Some binding instructions in the update block may actually correspond to i18n bindings. In that
  11929. * case, they should be replaced with i18nExp instructions for the dynamic portions.
  11930. */
  11931. function convertI18nBindings(job) {
  11932. const i18nAttributesByElem = new Map();
  11933. for (const unit of job.units) {
  11934. for (const op of unit.create) {
  11935. if (op.kind === OpKind.I18nAttributes) {
  11936. i18nAttributesByElem.set(op.target, op);
  11937. }
  11938. }
  11939. for (const op of unit.update) {
  11940. switch (op.kind) {
  11941. case OpKind.Property:
  11942. case OpKind.Attribute:
  11943. if (op.i18nContext === null) {
  11944. continue;
  11945. }
  11946. if (!(op.expression instanceof Interpolation)) {
  11947. continue;
  11948. }
  11949. const i18nAttributesForElem = i18nAttributesByElem.get(op.target);
  11950. if (i18nAttributesForElem === undefined) {
  11951. throw new Error('AssertionError: An i18n attribute binding instruction requires the owning element to have an I18nAttributes create instruction');
  11952. }
  11953. if (i18nAttributesForElem.target !== op.target) {
  11954. throw new Error('AssertionError: Expected i18nAttributes target element to match binding target element');
  11955. }
  11956. const ops = [];
  11957. for (let i = 0; i < op.expression.expressions.length; i++) {
  11958. const expr = op.expression.expressions[i];
  11959. if (op.expression.i18nPlaceholders.length !== op.expression.expressions.length) {
  11960. throw new Error(`AssertionError: An i18n attribute binding instruction requires the same number of expressions and placeholders, but found ${op.expression.i18nPlaceholders.length} placeholders and ${op.expression.expressions.length} expressions`);
  11961. }
  11962. ops.push(createI18nExpressionOp(op.i18nContext, i18nAttributesForElem.target, i18nAttributesForElem.xref, i18nAttributesForElem.handle, expr, null, op.expression.i18nPlaceholders[i], I18nParamResolutionTime.Creation, I18nExpressionFor.I18nAttribute, op.name, op.sourceSpan));
  11963. }
  11964. OpList.replaceWithMany(op, ops);
  11965. break;
  11966. }
  11967. }
  11968. }
  11969. }
  11970. /**
  11971. * Resolve the dependency function of a deferred block.
  11972. */
  11973. function resolveDeferDepsFns(job) {
  11974. for (const unit of job.units) {
  11975. for (const op of unit.create) {
  11976. if (op.kind === OpKind.Defer) {
  11977. if (op.resolverFn !== null) {
  11978. continue;
  11979. }
  11980. if (op.ownResolverFn !== null) {
  11981. if (op.handle.slot === null) {
  11982. throw new Error('AssertionError: slot must be assigned before extracting defer deps functions');
  11983. }
  11984. const fullPathName = unit.fnName?.replace('_Template', '');
  11985. op.resolverFn = job.pool.getSharedFunctionReference(op.ownResolverFn, `${fullPathName}_Defer_${op.handle.slot}_DepsFn`,
  11986. /* Don't use unique names for TDB compatibility */ false);
  11987. }
  11988. }
  11989. }
  11990. }
  11991. }
  11992. /**
  11993. * Create one helper context op per i18n block (including generate descending blocks).
  11994. *
  11995. * Also, if an ICU exists inside an i18n block that also contains other localizable content (such as
  11996. * string), create an additional helper context op for the ICU.
  11997. *
  11998. * These context ops are later used for generating i18n messages. (Although we generate at least one
  11999. * context op per nested view, we will collect them up the tree later, to generate a top-level
  12000. * message.)
  12001. */
  12002. function createI18nContexts(job) {
  12003. // Create i18n context ops for i18n attrs.
  12004. const attrContextByMessage = new Map();
  12005. for (const unit of job.units) {
  12006. for (const op of unit.ops()) {
  12007. switch (op.kind) {
  12008. case OpKind.Binding:
  12009. case OpKind.Property:
  12010. case OpKind.Attribute:
  12011. case OpKind.ExtractedAttribute:
  12012. if (op.i18nMessage === null) {
  12013. continue;
  12014. }
  12015. if (!attrContextByMessage.has(op.i18nMessage)) {
  12016. const i18nContext = createI18nContextOp(I18nContextKind.Attr, job.allocateXrefId(), null, op.i18nMessage, null);
  12017. unit.create.push(i18nContext);
  12018. attrContextByMessage.set(op.i18nMessage, i18nContext.xref);
  12019. }
  12020. op.i18nContext = attrContextByMessage.get(op.i18nMessage);
  12021. break;
  12022. }
  12023. }
  12024. }
  12025. // Create i18n context ops for root i18n blocks.
  12026. const blockContextByI18nBlock = new Map();
  12027. for (const unit of job.units) {
  12028. for (const op of unit.create) {
  12029. switch (op.kind) {
  12030. case OpKind.I18nStart:
  12031. if (op.xref === op.root) {
  12032. const contextOp = createI18nContextOp(I18nContextKind.RootI18n, job.allocateXrefId(), op.xref, op.message, null);
  12033. unit.create.push(contextOp);
  12034. op.context = contextOp.xref;
  12035. blockContextByI18nBlock.set(op.xref, contextOp);
  12036. }
  12037. break;
  12038. }
  12039. }
  12040. }
  12041. // Assign i18n contexts for child i18n blocks. These don't need their own conext, instead they
  12042. // should inherit from their root i18n block.
  12043. for (const unit of job.units) {
  12044. for (const op of unit.create) {
  12045. if (op.kind === OpKind.I18nStart && op.xref !== op.root) {
  12046. const rootContext = blockContextByI18nBlock.get(op.root);
  12047. if (rootContext === undefined) {
  12048. throw Error('AssertionError: Root i18n block i18n context should have been created.');
  12049. }
  12050. op.context = rootContext.xref;
  12051. blockContextByI18nBlock.set(op.xref, rootContext);
  12052. }
  12053. }
  12054. }
  12055. // Create or assign i18n contexts for ICUs.
  12056. let currentI18nOp = null;
  12057. for (const unit of job.units) {
  12058. for (const op of unit.create) {
  12059. switch (op.kind) {
  12060. case OpKind.I18nStart:
  12061. currentI18nOp = op;
  12062. break;
  12063. case OpKind.I18nEnd:
  12064. currentI18nOp = null;
  12065. break;
  12066. case OpKind.IcuStart:
  12067. if (currentI18nOp === null) {
  12068. throw Error('AssertionError: Unexpected ICU outside of an i18n block.');
  12069. }
  12070. if (op.message.id !== currentI18nOp.message.id) {
  12071. // This ICU is a sub-message inside its parent i18n block message. We need to give it
  12072. // its own context.
  12073. const contextOp = createI18nContextOp(I18nContextKind.Icu, job.allocateXrefId(), currentI18nOp.root, op.message, null);
  12074. unit.create.push(contextOp);
  12075. op.context = contextOp.xref;
  12076. }
  12077. else {
  12078. // This ICU is the only translatable content in its parent i18n block. We need to
  12079. // convert the parent's context into an ICU context.
  12080. op.context = currentI18nOp.context;
  12081. blockContextByI18nBlock.get(currentI18nOp.xref).contextKind = I18nContextKind.Icu;
  12082. }
  12083. break;
  12084. }
  12085. }
  12086. }
  12087. }
  12088. /**
  12089. * Deduplicate text bindings, e.g. <div class="cls1" class="cls2">
  12090. */
  12091. function deduplicateTextBindings(job) {
  12092. const seen = new Map();
  12093. for (const unit of job.units) {
  12094. for (const op of unit.update.reversed()) {
  12095. if (op.kind === OpKind.Binding && op.isTextAttribute) {
  12096. const seenForElement = seen.get(op.target) || new Set();
  12097. if (seenForElement.has(op.name)) {
  12098. if (job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  12099. // For most duplicated attributes, TemplateDefinitionBuilder lists all of the values in
  12100. // the consts array. However, for style and class attributes it only keeps the last one.
  12101. // We replicate that behavior here since it has actual consequences for apps with
  12102. // duplicate class or style attrs.
  12103. if (op.name === 'style' || op.name === 'class') {
  12104. OpList.remove(op);
  12105. }
  12106. }
  12107. }
  12108. seenForElement.add(op.name);
  12109. seen.set(op.target, seenForElement);
  12110. }
  12111. }
  12112. }
  12113. }
  12114. /**
  12115. * Defer instructions take a configuration array, which should be collected into the component
  12116. * consts. This phase finds the config options, and creates the corresponding const array.
  12117. */
  12118. function configureDeferInstructions(job) {
  12119. for (const unit of job.units) {
  12120. for (const op of unit.create) {
  12121. if (op.kind !== OpKind.Defer) {
  12122. continue;
  12123. }
  12124. if (op.placeholderMinimumTime !== null) {
  12125. op.placeholderConfig = new ConstCollectedExpr(literalOrArrayLiteral([op.placeholderMinimumTime]));
  12126. }
  12127. if (op.loadingMinimumTime !== null || op.loadingAfterTime !== null) {
  12128. op.loadingConfig = new ConstCollectedExpr(literalOrArrayLiteral([op.loadingMinimumTime, op.loadingAfterTime]));
  12129. }
  12130. }
  12131. }
  12132. }
  12133. /**
  12134. * Some `defer` conditions can reference other elements in the template, using their local reference
  12135. * names. However, the semantics are quite different from the normal local reference system: in
  12136. * particular, we need to look at local reference names in enclosing views. This phase resolves
  12137. * all such references to actual xrefs.
  12138. */
  12139. function resolveDeferTargetNames(job) {
  12140. const scopes = new Map();
  12141. function getScopeForView(view) {
  12142. if (scopes.has(view.xref)) {
  12143. return scopes.get(view.xref);
  12144. }
  12145. const scope = new Scope$2();
  12146. for (const op of view.create) {
  12147. // add everything that can be referenced.
  12148. if (!isElementOrContainerOp(op) || op.localRefs === null) {
  12149. continue;
  12150. }
  12151. if (!Array.isArray(op.localRefs)) {
  12152. throw new Error('LocalRefs were already processed, but were needed to resolve defer targets.');
  12153. }
  12154. for (const ref of op.localRefs) {
  12155. if (ref.target !== '') {
  12156. continue;
  12157. }
  12158. scope.targets.set(ref.name, { xref: op.xref, slot: op.handle });
  12159. }
  12160. }
  12161. scopes.set(view.xref, scope);
  12162. return scope;
  12163. }
  12164. function resolveTrigger(deferOwnerView, op, placeholderView) {
  12165. switch (op.trigger.kind) {
  12166. case DeferTriggerKind.Idle:
  12167. case DeferTriggerKind.Never:
  12168. case DeferTriggerKind.Immediate:
  12169. case DeferTriggerKind.Timer:
  12170. return;
  12171. case DeferTriggerKind.Hover:
  12172. case DeferTriggerKind.Interaction:
  12173. case DeferTriggerKind.Viewport:
  12174. if (op.trigger.targetName === null) {
  12175. // A `null` target name indicates we should default to the first element in the
  12176. // placeholder block.
  12177. if (placeholderView === null) {
  12178. throw new Error('defer on trigger with no target name must have a placeholder block');
  12179. }
  12180. const placeholder = job.views.get(placeholderView);
  12181. if (placeholder == undefined) {
  12182. throw new Error('AssertionError: could not find placeholder view for defer on trigger');
  12183. }
  12184. for (const placeholderOp of placeholder.create) {
  12185. if (hasConsumesSlotTrait(placeholderOp) &&
  12186. (isElementOrContainerOp(placeholderOp) ||
  12187. placeholderOp.kind === OpKind.Projection)) {
  12188. op.trigger.targetXref = placeholderOp.xref;
  12189. op.trigger.targetView = placeholderView;
  12190. op.trigger.targetSlotViewSteps = -1;
  12191. op.trigger.targetSlot = placeholderOp.handle;
  12192. return;
  12193. }
  12194. }
  12195. return;
  12196. }
  12197. let view = placeholderView !== null ? job.views.get(placeholderView) : deferOwnerView;
  12198. let step = placeholderView !== null ? -1 : 0;
  12199. while (view !== null) {
  12200. const scope = getScopeForView(view);
  12201. if (scope.targets.has(op.trigger.targetName)) {
  12202. const { xref, slot } = scope.targets.get(op.trigger.targetName);
  12203. op.trigger.targetXref = xref;
  12204. op.trigger.targetView = view.xref;
  12205. op.trigger.targetSlotViewSteps = step;
  12206. op.trigger.targetSlot = slot;
  12207. return;
  12208. }
  12209. view = view.parent !== null ? job.views.get(view.parent) : null;
  12210. step++;
  12211. }
  12212. break;
  12213. default:
  12214. throw new Error(`Trigger kind ${op.trigger.kind} not handled`);
  12215. }
  12216. }
  12217. // Find the defer ops, and assign the data about their targets.
  12218. for (const unit of job.units) {
  12219. const defers = new Map();
  12220. for (const op of unit.create) {
  12221. switch (op.kind) {
  12222. case OpKind.Defer:
  12223. defers.set(op.xref, op);
  12224. break;
  12225. case OpKind.DeferOn:
  12226. const deferOp = defers.get(op.defer);
  12227. resolveTrigger(unit, op, op.modifier === "hydrate" /* ir.DeferOpModifierKind.HYDRATE */
  12228. ? deferOp.mainView
  12229. : deferOp.placeholderView);
  12230. break;
  12231. }
  12232. }
  12233. }
  12234. }
  12235. let Scope$2 = class Scope {
  12236. targets = new Map();
  12237. };
  12238. const REPLACEMENTS = new Map([
  12239. [OpKind.ElementEnd, [OpKind.ElementStart, OpKind.Element]],
  12240. [OpKind.ContainerEnd, [OpKind.ContainerStart, OpKind.Container]],
  12241. [OpKind.I18nEnd, [OpKind.I18nStart, OpKind.I18n]],
  12242. ]);
  12243. /**
  12244. * Op kinds that should not prevent merging of start/end ops.
  12245. */
  12246. const IGNORED_OP_KINDS = new Set([OpKind.Pipe]);
  12247. /**
  12248. * Replace sequences of mergable instructions (e.g. `ElementStart` and `ElementEnd`) with a
  12249. * consolidated instruction (e.g. `Element`).
  12250. */
  12251. function collapseEmptyInstructions(job) {
  12252. for (const unit of job.units) {
  12253. for (const op of unit.create) {
  12254. // Find end ops that may be able to be merged.
  12255. const opReplacements = REPLACEMENTS.get(op.kind);
  12256. if (opReplacements === undefined) {
  12257. continue;
  12258. }
  12259. const [startKind, mergedKind] = opReplacements;
  12260. // Locate the previous (non-ignored) op.
  12261. let prevOp = op.prev;
  12262. while (prevOp !== null && IGNORED_OP_KINDS.has(prevOp.kind)) {
  12263. prevOp = prevOp.prev;
  12264. }
  12265. // If the previous op is the corresponding start op, we can megre.
  12266. if (prevOp !== null && prevOp.kind === startKind) {
  12267. // Transmute the start instruction to the merged version. This is safe as they're designed
  12268. // to be identical apart from the `kind`.
  12269. prevOp.kind = mergedKind;
  12270. // Remove the end instruction.
  12271. OpList.remove(op);
  12272. }
  12273. }
  12274. }
  12275. }
  12276. /**
  12277. * Safe read expressions such as `a?.b` have different semantics in Angular templates as
  12278. * compared to JavaScript. In particular, they default to `null` instead of `undefined`. This phase
  12279. * finds all unresolved safe read expressions, and converts them into the appropriate output AST
  12280. * reads, guarded by null checks. We generate temporaries as needed, to avoid re-evaluating the same
  12281. * sub-expression multiple times.
  12282. */
  12283. function expandSafeReads(job) {
  12284. for (const unit of job.units) {
  12285. for (const op of unit.ops()) {
  12286. transformExpressionsInOp(op, (e) => safeTransform(e, { job }), VisitorContextFlag.None);
  12287. transformExpressionsInOp(op, ternaryTransform, VisitorContextFlag.None);
  12288. }
  12289. }
  12290. }
  12291. // A lookup set of all the expression kinds that require a temporary variable to be generated.
  12292. [
  12293. InvokeFunctionExpr,
  12294. LiteralArrayExpr,
  12295. LiteralMapExpr,
  12296. SafeInvokeFunctionExpr,
  12297. PipeBindingExpr,
  12298. ].map((e) => e.constructor.name);
  12299. function needsTemporaryInSafeAccess(e) {
  12300. // TODO: We probably want to use an expression visitor to recursively visit all descendents.
  12301. // However, that would potentially do a lot of extra work (because it cannot short circuit), so we
  12302. // implement the logic ourselves for now.
  12303. if (e instanceof UnaryOperatorExpr) {
  12304. return needsTemporaryInSafeAccess(e.expr);
  12305. }
  12306. else if (e instanceof BinaryOperatorExpr) {
  12307. return needsTemporaryInSafeAccess(e.lhs) || needsTemporaryInSafeAccess(e.rhs);
  12308. }
  12309. else if (e instanceof ConditionalExpr) {
  12310. if (e.falseCase && needsTemporaryInSafeAccess(e.falseCase))
  12311. return true;
  12312. return needsTemporaryInSafeAccess(e.condition) || needsTemporaryInSafeAccess(e.trueCase);
  12313. }
  12314. else if (e instanceof NotExpr) {
  12315. return needsTemporaryInSafeAccess(e.condition);
  12316. }
  12317. else if (e instanceof AssignTemporaryExpr) {
  12318. return needsTemporaryInSafeAccess(e.expr);
  12319. }
  12320. else if (e instanceof ReadPropExpr) {
  12321. return needsTemporaryInSafeAccess(e.receiver);
  12322. }
  12323. else if (e instanceof ReadKeyExpr) {
  12324. return needsTemporaryInSafeAccess(e.receiver) || needsTemporaryInSafeAccess(e.index);
  12325. }
  12326. // TODO: Switch to a method which is exhaustive of newly added expression subtypes.
  12327. return (e instanceof InvokeFunctionExpr ||
  12328. e instanceof LiteralArrayExpr ||
  12329. e instanceof LiteralMapExpr ||
  12330. e instanceof SafeInvokeFunctionExpr ||
  12331. e instanceof PipeBindingExpr);
  12332. }
  12333. function temporariesIn(e) {
  12334. const temporaries = new Set();
  12335. // TODO: Although it's not currently supported by the transform helper, we should be able to
  12336. // short-circuit exploring the tree to do less work. In particular, we don't have to penetrate
  12337. // into the subexpressions of temporary assignments.
  12338. transformExpressionsInExpression(e, (e) => {
  12339. if (e instanceof AssignTemporaryExpr) {
  12340. temporaries.add(e.xref);
  12341. }
  12342. return e;
  12343. }, VisitorContextFlag.None);
  12344. return temporaries;
  12345. }
  12346. function eliminateTemporaryAssignments(e, tmps, ctx) {
  12347. // TODO: We can be more efficient than the transform helper here. We don't need to visit any
  12348. // descendents of temporary assignments.
  12349. transformExpressionsInExpression(e, (e) => {
  12350. if (e instanceof AssignTemporaryExpr && tmps.has(e.xref)) {
  12351. const read = new ReadTemporaryExpr(e.xref);
  12352. // `TemplateDefinitionBuilder` has the (accidental?) behavior of generating assignments of
  12353. // temporary variables to themselves. This happens because some subexpression that the
  12354. // temporary refers to, possibly through nested temporaries, has a function call. We copy that
  12355. // behavior here.
  12356. return ctx.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder
  12357. ? new AssignTemporaryExpr(read, read.xref)
  12358. : read;
  12359. }
  12360. return e;
  12361. }, VisitorContextFlag.None);
  12362. return e;
  12363. }
  12364. /**
  12365. * Creates a safe ternary guarded by the input expression, and with a body generated by the provided
  12366. * callback on the input expression. Generates a temporary variable assignment if needed, and
  12367. * deduplicates nested temporary assignments if needed.
  12368. */
  12369. function safeTernaryWithTemporary(guard, body, ctx) {
  12370. let result;
  12371. if (needsTemporaryInSafeAccess(guard)) {
  12372. const xref = ctx.job.allocateXrefId();
  12373. result = [new AssignTemporaryExpr(guard, xref), new ReadTemporaryExpr(xref)];
  12374. }
  12375. else {
  12376. result = [guard, guard.clone()];
  12377. // Consider an expression like `a?.[b?.c()]?.d`. The `b?.c()` will be transformed first,
  12378. // introducing a temporary assignment into the key. Then, as part of expanding the `?.d`. That
  12379. // assignment will be duplicated into both the guard and expression sides. We de-duplicate it,
  12380. // by transforming it from an assignment into a read on the expression side.
  12381. eliminateTemporaryAssignments(result[1], temporariesIn(result[0]), ctx);
  12382. }
  12383. return new SafeTernaryExpr(result[0], body(result[1]));
  12384. }
  12385. function isSafeAccessExpression(e) {
  12386. return (e instanceof SafePropertyReadExpr ||
  12387. e instanceof SafeKeyedReadExpr ||
  12388. e instanceof SafeInvokeFunctionExpr);
  12389. }
  12390. function isUnsafeAccessExpression(e) {
  12391. return (e instanceof ReadPropExpr || e instanceof ReadKeyExpr || e instanceof InvokeFunctionExpr);
  12392. }
  12393. function isAccessExpression$1(e) {
  12394. return isSafeAccessExpression(e) || isUnsafeAccessExpression(e);
  12395. }
  12396. function deepestSafeTernary(e) {
  12397. if (isAccessExpression$1(e) && e.receiver instanceof SafeTernaryExpr) {
  12398. let st = e.receiver;
  12399. while (st.expr instanceof SafeTernaryExpr) {
  12400. st = st.expr;
  12401. }
  12402. return st;
  12403. }
  12404. return null;
  12405. }
  12406. // TODO: When strict compatibility with TemplateDefinitionBuilder is not required, we can use `&&`
  12407. // instead to save some code size.
  12408. function safeTransform(e, ctx) {
  12409. if (!isAccessExpression$1(e)) {
  12410. return e;
  12411. }
  12412. const dst = deepestSafeTernary(e);
  12413. if (dst) {
  12414. if (e instanceof InvokeFunctionExpr) {
  12415. dst.expr = dst.expr.callFn(e.args);
  12416. return e.receiver;
  12417. }
  12418. if (e instanceof ReadPropExpr) {
  12419. dst.expr = dst.expr.prop(e.name);
  12420. return e.receiver;
  12421. }
  12422. if (e instanceof ReadKeyExpr) {
  12423. dst.expr = dst.expr.key(e.index);
  12424. return e.receiver;
  12425. }
  12426. if (e instanceof SafeInvokeFunctionExpr) {
  12427. dst.expr = safeTernaryWithTemporary(dst.expr, (r) => r.callFn(e.args), ctx);
  12428. return e.receiver;
  12429. }
  12430. if (e instanceof SafePropertyReadExpr) {
  12431. dst.expr = safeTernaryWithTemporary(dst.expr, (r) => r.prop(e.name), ctx);
  12432. return e.receiver;
  12433. }
  12434. if (e instanceof SafeKeyedReadExpr) {
  12435. dst.expr = safeTernaryWithTemporary(dst.expr, (r) => r.key(e.index), ctx);
  12436. return e.receiver;
  12437. }
  12438. }
  12439. else {
  12440. if (e instanceof SafeInvokeFunctionExpr) {
  12441. return safeTernaryWithTemporary(e.receiver, (r) => r.callFn(e.args), ctx);
  12442. }
  12443. if (e instanceof SafePropertyReadExpr) {
  12444. return safeTernaryWithTemporary(e.receiver, (r) => r.prop(e.name), ctx);
  12445. }
  12446. if (e instanceof SafeKeyedReadExpr) {
  12447. return safeTernaryWithTemporary(e.receiver, (r) => r.key(e.index), ctx);
  12448. }
  12449. }
  12450. return e;
  12451. }
  12452. function ternaryTransform(e) {
  12453. if (!(e instanceof SafeTernaryExpr)) {
  12454. return e;
  12455. }
  12456. return new ConditionalExpr(new BinaryOperatorExpr(BinaryOperator.Equals, e.guard, NULL_EXPR), NULL_EXPR, e.expr);
  12457. }
  12458. /**
  12459. * The escape sequence used indicate message param values.
  12460. */
  12461. const ESCAPE$1 = '\uFFFD';
  12462. /**
  12463. * Marker used to indicate an element tag.
  12464. */
  12465. const ELEMENT_MARKER = '#';
  12466. /**
  12467. * Marker used to indicate a template tag.
  12468. */
  12469. const TEMPLATE_MARKER = '*';
  12470. /**
  12471. * Marker used to indicate closing of an element or template tag.
  12472. */
  12473. const TAG_CLOSE_MARKER = '/';
  12474. /**
  12475. * Marker used to indicate the sub-template context.
  12476. */
  12477. const CONTEXT_MARKER = ':';
  12478. /**
  12479. * Marker used to indicate the start of a list of values.
  12480. */
  12481. const LIST_START_MARKER = '[';
  12482. /**
  12483. * Marker used to indicate the end of a list of values.
  12484. */
  12485. const LIST_END_MARKER = ']';
  12486. /**
  12487. * Delimiter used to separate multiple values in a list.
  12488. */
  12489. const LIST_DELIMITER = '|';
  12490. /**
  12491. * Formats the param maps on extracted message ops into a maps of `Expression` objects that can be
  12492. * used in the final output.
  12493. */
  12494. function extractI18nMessages(job) {
  12495. // Create an i18n message for each context.
  12496. // TODO: Merge the context op with the message op since they're 1:1 anyways.
  12497. const i18nMessagesByContext = new Map();
  12498. const i18nBlocks = new Map();
  12499. const i18nContexts = new Map();
  12500. for (const unit of job.units) {
  12501. for (const op of unit.create) {
  12502. switch (op.kind) {
  12503. case OpKind.I18nContext:
  12504. const i18nMessageOp = createI18nMessage(job, op);
  12505. unit.create.push(i18nMessageOp);
  12506. i18nMessagesByContext.set(op.xref, i18nMessageOp);
  12507. i18nContexts.set(op.xref, op);
  12508. break;
  12509. case OpKind.I18nStart:
  12510. i18nBlocks.set(op.xref, op);
  12511. break;
  12512. }
  12513. }
  12514. }
  12515. // Associate sub-messages for ICUs with their root message. At this point we can also remove the
  12516. // ICU start/end ops, as they are no longer needed.
  12517. let currentIcu = null;
  12518. for (const unit of job.units) {
  12519. for (const op of unit.create) {
  12520. switch (op.kind) {
  12521. case OpKind.IcuStart:
  12522. currentIcu = op;
  12523. OpList.remove(op);
  12524. // Skip any contexts not associated with an ICU.
  12525. const icuContext = i18nContexts.get(op.context);
  12526. if (icuContext.contextKind !== I18nContextKind.Icu) {
  12527. continue;
  12528. }
  12529. // Skip ICUs that share a context with their i18n message. These represent root-level
  12530. // ICUs, not sub-messages.
  12531. const i18nBlock = i18nBlocks.get(icuContext.i18nBlock);
  12532. if (i18nBlock.context === icuContext.xref) {
  12533. continue;
  12534. }
  12535. // Find the root message and push this ICUs message as a sub-message.
  12536. const rootI18nBlock = i18nBlocks.get(i18nBlock.root);
  12537. const rootMessage = i18nMessagesByContext.get(rootI18nBlock.context);
  12538. if (rootMessage === undefined) {
  12539. throw Error('AssertionError: ICU sub-message should belong to a root message.');
  12540. }
  12541. const subMessage = i18nMessagesByContext.get(icuContext.xref);
  12542. subMessage.messagePlaceholder = op.messagePlaceholder;
  12543. rootMessage.subMessages.push(subMessage.xref);
  12544. break;
  12545. case OpKind.IcuEnd:
  12546. currentIcu = null;
  12547. OpList.remove(op);
  12548. break;
  12549. case OpKind.IcuPlaceholder:
  12550. // Add ICU placeholders to the message, then remove the ICU placeholder ops.
  12551. if (currentIcu === null || currentIcu.context == null) {
  12552. throw Error('AssertionError: Unexpected ICU placeholder outside of i18n context');
  12553. }
  12554. const msg = i18nMessagesByContext.get(currentIcu.context);
  12555. msg.postprocessingParams.set(op.name, literal$1(formatIcuPlaceholder(op)));
  12556. OpList.remove(op);
  12557. break;
  12558. }
  12559. }
  12560. }
  12561. }
  12562. /**
  12563. * Create an i18n message op from an i18n context op.
  12564. */
  12565. function createI18nMessage(job, context, messagePlaceholder) {
  12566. let formattedParams = formatParams(context.params);
  12567. const formattedPostprocessingParams = formatParams(context.postprocessingParams);
  12568. let needsPostprocessing = [...context.params.values()].some((v) => v.length > 1);
  12569. return createI18nMessageOp(job.allocateXrefId(), context.xref, context.i18nBlock, context.message, null, formattedParams, formattedPostprocessingParams, needsPostprocessing);
  12570. }
  12571. /**
  12572. * Formats an ICU placeholder into a single string with expression placeholders.
  12573. */
  12574. function formatIcuPlaceholder(op) {
  12575. if (op.strings.length !== op.expressionPlaceholders.length + 1) {
  12576. throw Error(`AssertionError: Invalid ICU placeholder with ${op.strings.length} strings and ${op.expressionPlaceholders.length} expressions`);
  12577. }
  12578. const values = op.expressionPlaceholders.map(formatValue);
  12579. return op.strings.flatMap((str, i) => [str, values[i] || '']).join('');
  12580. }
  12581. /**
  12582. * Formats a map of `I18nParamValue[]` values into a map of `Expression` values.
  12583. */
  12584. function formatParams(params) {
  12585. const formattedParams = new Map();
  12586. for (const [placeholder, placeholderValues] of params) {
  12587. const serializedValues = formatParamValues(placeholderValues);
  12588. if (serializedValues !== null) {
  12589. formattedParams.set(placeholder, literal$1(serializedValues));
  12590. }
  12591. }
  12592. return formattedParams;
  12593. }
  12594. /**
  12595. * Formats an `I18nParamValue[]` into a string (or null for empty array).
  12596. */
  12597. function formatParamValues(values) {
  12598. if (values.length === 0) {
  12599. return null;
  12600. }
  12601. const serializedValues = values.map((value) => formatValue(value));
  12602. return serializedValues.length === 1
  12603. ? serializedValues[0]
  12604. : `${LIST_START_MARKER}${serializedValues.join(LIST_DELIMITER)}${LIST_END_MARKER}`;
  12605. }
  12606. /**
  12607. * Formats a single `I18nParamValue` into a string
  12608. */
  12609. function formatValue(value) {
  12610. // Element tags with a structural directive use a special form that concatenates the element and
  12611. // template values.
  12612. if (value.flags & I18nParamValueFlags.ElementTag &&
  12613. value.flags & I18nParamValueFlags.TemplateTag) {
  12614. if (typeof value.value !== 'object') {
  12615. throw Error('AssertionError: Expected i18n param value to have an element and template slot');
  12616. }
  12617. const elementValue = formatValue({
  12618. ...value,
  12619. value: value.value.element,
  12620. flags: value.flags & ~I18nParamValueFlags.TemplateTag,
  12621. });
  12622. const templateValue = formatValue({
  12623. ...value,
  12624. value: value.value.template,
  12625. flags: value.flags & ~I18nParamValueFlags.ElementTag,
  12626. });
  12627. // TODO(mmalerba): This is likely a bug in TemplateDefinitionBuilder, we should not need to
  12628. // record the template value twice. For now I'm re-implementing the behavior here to keep the
  12629. // output consistent with TemplateDefinitionBuilder.
  12630. if (value.flags & I18nParamValueFlags.OpenTag &&
  12631. value.flags & I18nParamValueFlags.CloseTag) {
  12632. return `${templateValue}${elementValue}${templateValue}`;
  12633. }
  12634. // To match the TemplateDefinitionBuilder output, flip the order depending on whether the
  12635. // values represent a closing or opening tag (or both).
  12636. // TODO(mmalerba): Figure out if this makes a difference in terms of either functionality,
  12637. // or the resulting message ID. If not, we can remove the special-casing in the future.
  12638. return value.flags & I18nParamValueFlags.CloseTag
  12639. ? `${elementValue}${templateValue}`
  12640. : `${templateValue}${elementValue}`;
  12641. }
  12642. // Self-closing tags use a special form that concatenates the start and close tag values.
  12643. if (value.flags & I18nParamValueFlags.OpenTag &&
  12644. value.flags & I18nParamValueFlags.CloseTag) {
  12645. return `${formatValue({
  12646. ...value,
  12647. flags: value.flags & ~I18nParamValueFlags.CloseTag,
  12648. })}${formatValue({ ...value, flags: value.flags & ~I18nParamValueFlags.OpenTag })}`;
  12649. }
  12650. // If there are no special flags, just return the raw value.
  12651. if (value.flags === I18nParamValueFlags.None) {
  12652. return `${value.value}`;
  12653. }
  12654. // Encode the remaining flags as part of the value.
  12655. let tagMarker = '';
  12656. let closeMarker = '';
  12657. if (value.flags & I18nParamValueFlags.ElementTag) {
  12658. tagMarker = ELEMENT_MARKER;
  12659. }
  12660. else if (value.flags & I18nParamValueFlags.TemplateTag) {
  12661. tagMarker = TEMPLATE_MARKER;
  12662. }
  12663. if (tagMarker !== '') {
  12664. closeMarker = value.flags & I18nParamValueFlags.CloseTag ? TAG_CLOSE_MARKER : '';
  12665. }
  12666. const context = value.subTemplateIndex === null ? '' : `${CONTEXT_MARKER}${value.subTemplateIndex}`;
  12667. return `${ESCAPE$1}${closeMarker}${tagMarker}${value.value}${context}${ESCAPE$1}`;
  12668. }
  12669. /**
  12670. * Generate `ir.AdvanceOp`s in between `ir.UpdateOp`s that ensure the runtime's implicit slot
  12671. * context will be advanced correctly.
  12672. */
  12673. function generateAdvance(job) {
  12674. for (const unit of job.units) {
  12675. // First build a map of all of the declarations in the view that have assigned slots.
  12676. const slotMap = new Map();
  12677. for (const op of unit.create) {
  12678. if (!hasConsumesSlotTrait(op)) {
  12679. continue;
  12680. }
  12681. else if (op.handle.slot === null) {
  12682. throw new Error(`AssertionError: expected slots to have been allocated before generating advance() calls`);
  12683. }
  12684. slotMap.set(op.xref, op.handle.slot);
  12685. }
  12686. // Next, step through the update operations and generate `ir.AdvanceOp`s as required to ensure
  12687. // the runtime's implicit slot counter will be set to the correct slot before executing each
  12688. // update operation which depends on it.
  12689. //
  12690. // To do that, we track what the runtime's slot counter will be through the update operations.
  12691. let slotContext = 0;
  12692. for (const op of unit.update) {
  12693. let consumer = null;
  12694. if (hasDependsOnSlotContextTrait(op)) {
  12695. consumer = op;
  12696. }
  12697. else {
  12698. visitExpressionsInOp(op, (expr) => {
  12699. if (consumer === null && hasDependsOnSlotContextTrait(expr)) {
  12700. consumer = expr;
  12701. }
  12702. });
  12703. }
  12704. if (consumer === null) {
  12705. continue;
  12706. }
  12707. if (!slotMap.has(consumer.target)) {
  12708. // We expect ops that _do_ depend on the slot counter to point at declarations that exist in
  12709. // the `slotMap`.
  12710. throw new Error(`AssertionError: reference to unknown slot for target ${consumer.target}`);
  12711. }
  12712. const slot = slotMap.get(consumer.target);
  12713. // Does the slot counter need to be adjusted?
  12714. if (slotContext !== slot) {
  12715. // If so, generate an `ir.AdvanceOp` to advance the counter.
  12716. const delta = slot - slotContext;
  12717. if (delta < 0) {
  12718. throw new Error(`AssertionError: slot counter should never need to move backwards`);
  12719. }
  12720. OpList.insertBefore(createAdvanceOp(delta, consumer.sourceSpan), op);
  12721. slotContext = slot;
  12722. }
  12723. }
  12724. }
  12725. }
  12726. /**
  12727. * Locate projection slots, populate the each component's `ngContentSelectors` literal field,
  12728. * populate `project` arguments, and generate the required `projectionDef` instruction for the job's
  12729. * root view.
  12730. */
  12731. function generateProjectionDefs(job) {
  12732. // TODO: Why does TemplateDefinitionBuilder force a shared constant?
  12733. const share = job.compatibility === CompatibilityMode.TemplateDefinitionBuilder;
  12734. // Collect all selectors from this component, and its nested views. Also, assign each projection a
  12735. // unique ascending projection slot index.
  12736. const selectors = [];
  12737. let projectionSlotIndex = 0;
  12738. for (const unit of job.units) {
  12739. for (const op of unit.create) {
  12740. if (op.kind === OpKind.Projection) {
  12741. selectors.push(op.selector);
  12742. op.projectionSlotIndex = projectionSlotIndex++;
  12743. }
  12744. }
  12745. }
  12746. if (selectors.length > 0) {
  12747. // Create the projectionDef array. If we only found a single wildcard selector, then we use the
  12748. // default behavior with no arguments instead.
  12749. let defExpr = null;
  12750. if (selectors.length > 1 || selectors[0] !== '*') {
  12751. const def = selectors.map((s) => (s === '*' ? s : parseSelectorToR3Selector(s)));
  12752. defExpr = job.pool.getConstLiteral(literalOrArrayLiteral(def), share);
  12753. }
  12754. // Create the ngContentSelectors constant.
  12755. job.contentSelectors = job.pool.getConstLiteral(literalOrArrayLiteral(selectors), share);
  12756. // The projection def instruction goes at the beginning of the root view, before any
  12757. // `projection` instructions.
  12758. job.root.create.prepend([createProjectionDefOp(defExpr)]);
  12759. }
  12760. }
  12761. /**
  12762. * Generate a preamble sequence for each view creation block and listener function which declares
  12763. * any variables that be referenced in other operations in the block.
  12764. *
  12765. * Variables generated include:
  12766. * * a saved view context to be used to restore the current view in event listeners.
  12767. * * the context of the restored view within event listener handlers.
  12768. * * context variables from the current view as well as all parent views (including the root
  12769. * context if needed).
  12770. * * local references from elements within the current view and any lexical parents.
  12771. *
  12772. * Variables are generated here unconditionally, and may optimized away in future operations if it
  12773. * turns out their values (and any side effects) are unused.
  12774. */
  12775. function generateVariables(job) {
  12776. recursivelyProcessView(job.root, /* there is no parent scope for the root view */ null);
  12777. }
  12778. /**
  12779. * Process the given `ViewCompilation` and generate preambles for it and any listeners that it
  12780. * declares.
  12781. *
  12782. * @param `parentScope` a scope extracted from the parent view which captures any variables which
  12783. * should be inherited by this view. `null` if the current view is the root view.
  12784. */
  12785. function recursivelyProcessView(view, parentScope) {
  12786. // Extract a `Scope` from this view.
  12787. const scope = getScopeForView(view, parentScope);
  12788. for (const op of view.create) {
  12789. switch (op.kind) {
  12790. case OpKind.Template:
  12791. // Descend into child embedded views.
  12792. recursivelyProcessView(view.job.views.get(op.xref), scope);
  12793. break;
  12794. case OpKind.Projection:
  12795. if (op.fallbackView !== null) {
  12796. recursivelyProcessView(view.job.views.get(op.fallbackView), scope);
  12797. }
  12798. break;
  12799. case OpKind.RepeaterCreate:
  12800. // Descend into child embedded views.
  12801. recursivelyProcessView(view.job.views.get(op.xref), scope);
  12802. if (op.emptyView) {
  12803. recursivelyProcessView(view.job.views.get(op.emptyView), scope);
  12804. }
  12805. if (op.trackByOps !== null) {
  12806. op.trackByOps.prepend(generateVariablesInScopeForView(view, scope, false));
  12807. }
  12808. break;
  12809. case OpKind.Listener:
  12810. case OpKind.TwoWayListener:
  12811. // Prepend variables to listener handler functions.
  12812. op.handlerOps.prepend(generateVariablesInScopeForView(view, scope, true));
  12813. break;
  12814. }
  12815. }
  12816. view.update.prepend(generateVariablesInScopeForView(view, scope, false));
  12817. }
  12818. /**
  12819. * Process a view and generate a `Scope` representing the variables available for reference within
  12820. * that view.
  12821. */
  12822. function getScopeForView(view, parent) {
  12823. const scope = {
  12824. view: view.xref,
  12825. viewContextVariable: {
  12826. kind: SemanticVariableKind.Context,
  12827. name: null,
  12828. view: view.xref,
  12829. },
  12830. contextVariables: new Map(),
  12831. aliases: view.aliases,
  12832. references: [],
  12833. letDeclarations: [],
  12834. parent,
  12835. };
  12836. for (const identifier of view.contextVariables.keys()) {
  12837. scope.contextVariables.set(identifier, {
  12838. kind: SemanticVariableKind.Identifier,
  12839. name: null,
  12840. identifier,
  12841. local: false,
  12842. });
  12843. }
  12844. for (const op of view.create) {
  12845. switch (op.kind) {
  12846. case OpKind.ElementStart:
  12847. case OpKind.Template:
  12848. if (!Array.isArray(op.localRefs)) {
  12849. throw new Error(`AssertionError: expected localRefs to be an array`);
  12850. }
  12851. // Record available local references from this element.
  12852. for (let offset = 0; offset < op.localRefs.length; offset++) {
  12853. scope.references.push({
  12854. name: op.localRefs[offset].name,
  12855. targetId: op.xref,
  12856. targetSlot: op.handle,
  12857. offset,
  12858. variable: {
  12859. kind: SemanticVariableKind.Identifier,
  12860. name: null,
  12861. identifier: op.localRefs[offset].name,
  12862. local: false,
  12863. },
  12864. });
  12865. }
  12866. break;
  12867. case OpKind.DeclareLet:
  12868. scope.letDeclarations.push({
  12869. targetId: op.xref,
  12870. targetSlot: op.handle,
  12871. variable: {
  12872. kind: SemanticVariableKind.Identifier,
  12873. name: null,
  12874. identifier: op.declaredName,
  12875. local: false,
  12876. },
  12877. });
  12878. break;
  12879. }
  12880. }
  12881. return scope;
  12882. }
  12883. /**
  12884. * Generate declarations for all variables that are in scope for a given view.
  12885. *
  12886. * This is a recursive process, as views inherit variables available from their parent view, which
  12887. * itself may have inherited variables, etc.
  12888. */
  12889. function generateVariablesInScopeForView(view, scope, isListener) {
  12890. const newOps = [];
  12891. if (scope.view !== view.xref) {
  12892. // Before generating variables for a parent view, we need to switch to the context of the parent
  12893. // view with a `nextContext` expression. This context switching operation itself declares a
  12894. // variable, because the context of the view may be referenced directly.
  12895. newOps.push(createVariableOp(view.job.allocateXrefId(), scope.viewContextVariable, new NextContextExpr(), VariableFlags.None));
  12896. }
  12897. // Add variables for all context variables available in this scope's view.
  12898. const scopeView = view.job.views.get(scope.view);
  12899. for (const [name, value] of scopeView.contextVariables) {
  12900. const context = new ContextExpr(scope.view);
  12901. // We either read the context, or, if the variable is CTX_REF, use the context directly.
  12902. const variable = value === CTX_REF ? context : new ReadPropExpr(context, value);
  12903. // Add the variable declaration.
  12904. newOps.push(createVariableOp(view.job.allocateXrefId(), scope.contextVariables.get(name), variable, VariableFlags.None));
  12905. }
  12906. for (const alias of scopeView.aliases) {
  12907. newOps.push(createVariableOp(view.job.allocateXrefId(), alias, alias.expression.clone(), VariableFlags.AlwaysInline));
  12908. }
  12909. // Add variables for all local references declared for elements in this scope.
  12910. for (const ref of scope.references) {
  12911. newOps.push(createVariableOp(view.job.allocateXrefId(), ref.variable, new ReferenceExpr(ref.targetId, ref.targetSlot, ref.offset), VariableFlags.None));
  12912. }
  12913. if (scope.view !== view.xref || isListener) {
  12914. for (const decl of scope.letDeclarations) {
  12915. newOps.push(createVariableOp(view.job.allocateXrefId(), decl.variable, new ContextLetReferenceExpr(decl.targetId, decl.targetSlot), VariableFlags.None));
  12916. }
  12917. }
  12918. if (scope.parent !== null) {
  12919. // Recursively add variables from the parent scope.
  12920. newOps.push(...generateVariablesInScopeForView(view, scope.parent, false));
  12921. }
  12922. return newOps;
  12923. }
  12924. /**
  12925. * `ir.ConstCollectedExpr` may be present in any IR expression. This means that expression needs to
  12926. * be lifted into the component const array, and replaced with a reference to the const array at its
  12927. *
  12928. * usage site. This phase walks the IR and performs this transformation.
  12929. */
  12930. function collectConstExpressions(job) {
  12931. for (const unit of job.units) {
  12932. for (const op of unit.ops()) {
  12933. transformExpressionsInOp(op, (expr) => {
  12934. if (!(expr instanceof ConstCollectedExpr)) {
  12935. return expr;
  12936. }
  12937. return literal$1(job.addConst(expr.expr));
  12938. }, VisitorContextFlag.None);
  12939. }
  12940. }
  12941. }
  12942. const STYLE_DOT = 'style.';
  12943. const CLASS_DOT = 'class.';
  12944. const STYLE_BANG = 'style!';
  12945. const CLASS_BANG = 'class!';
  12946. const BANG_IMPORTANT = '!important';
  12947. /**
  12948. * Host bindings are compiled using a different parser entrypoint, and are parsed quite differently
  12949. * as a result. Therefore, we need to do some extra parsing for host style properties, as compared
  12950. * to non-host style properties.
  12951. * TODO: Unify host bindings and non-host bindings in the parser.
  12952. */
  12953. function parseHostStyleProperties(job) {
  12954. for (const op of job.root.update) {
  12955. if (!(op.kind === OpKind.Binding && op.bindingKind === BindingKind.Property)) {
  12956. continue;
  12957. }
  12958. if (op.name.endsWith(BANG_IMPORTANT)) {
  12959. // Delete any `!important` suffixes from the binding name.
  12960. op.name = op.name.substring(0, op.name.length - BANG_IMPORTANT.length);
  12961. }
  12962. if (op.name.startsWith(STYLE_DOT)) {
  12963. op.bindingKind = BindingKind.StyleProperty;
  12964. op.name = op.name.substring(STYLE_DOT.length);
  12965. if (!isCssCustomProperty(op.name)) {
  12966. op.name = hyphenate$1(op.name);
  12967. }
  12968. const { property, suffix } = parseProperty(op.name);
  12969. op.name = property;
  12970. op.unit = suffix;
  12971. }
  12972. else if (op.name.startsWith(STYLE_BANG)) {
  12973. op.bindingKind = BindingKind.StyleProperty;
  12974. op.name = 'style';
  12975. }
  12976. else if (op.name.startsWith(CLASS_DOT)) {
  12977. op.bindingKind = BindingKind.ClassName;
  12978. op.name = parseProperty(op.name.substring(CLASS_DOT.length)).property;
  12979. }
  12980. else if (op.name.startsWith(CLASS_BANG)) {
  12981. op.bindingKind = BindingKind.ClassName;
  12982. op.name = parseProperty(op.name.substring(CLASS_BANG.length)).property;
  12983. }
  12984. }
  12985. }
  12986. /**
  12987. * Checks whether property name is a custom CSS property.
  12988. * See: https://www.w3.org/TR/css-variables-1
  12989. */
  12990. function isCssCustomProperty(name) {
  12991. return name.startsWith('--');
  12992. }
  12993. function hyphenate$1(value) {
  12994. return value
  12995. .replace(/[a-z][A-Z]/g, (v) => {
  12996. return v.charAt(0) + '-' + v.charAt(1);
  12997. })
  12998. .toLowerCase();
  12999. }
  13000. function parseProperty(name) {
  13001. const overrideIndex = name.indexOf('!important');
  13002. if (overrideIndex !== -1) {
  13003. name = overrideIndex > 0 ? name.substring(0, overrideIndex) : '';
  13004. }
  13005. let suffix = null;
  13006. let property = name;
  13007. const unitIndex = name.lastIndexOf('.');
  13008. if (unitIndex > 0) {
  13009. suffix = name.slice(unitIndex + 1);
  13010. property = name.substring(0, unitIndex);
  13011. }
  13012. return { property, suffix };
  13013. }
  13014. function mapLiteral(obj, quoted = false) {
  13015. return literalMap(Object.keys(obj).map((key) => ({
  13016. key,
  13017. quoted,
  13018. value: obj[key],
  13019. })));
  13020. }
  13021. class IcuSerializerVisitor {
  13022. visitText(text) {
  13023. return text.value;
  13024. }
  13025. visitContainer(container) {
  13026. return container.children.map((child) => child.visit(this)).join('');
  13027. }
  13028. visitIcu(icu) {
  13029. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  13030. const result = `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
  13031. return result;
  13032. }
  13033. visitTagPlaceholder(ph) {
  13034. return ph.isVoid
  13035. ? this.formatPh(ph.startName)
  13036. : `${this.formatPh(ph.startName)}${ph.children
  13037. .map((child) => child.visit(this))
  13038. .join('')}${this.formatPh(ph.closeName)}`;
  13039. }
  13040. visitPlaceholder(ph) {
  13041. return this.formatPh(ph.name);
  13042. }
  13043. visitBlockPlaceholder(ph) {
  13044. return `${this.formatPh(ph.startName)}${ph.children
  13045. .map((child) => child.visit(this))
  13046. .join('')}${this.formatPh(ph.closeName)}`;
  13047. }
  13048. visitIcuPlaceholder(ph, context) {
  13049. return this.formatPh(ph.name);
  13050. }
  13051. formatPh(value) {
  13052. return `{${formatI18nPlaceholderName(value, /* useCamelCase */ false)}}`;
  13053. }
  13054. }
  13055. const serializer = new IcuSerializerVisitor();
  13056. function serializeIcuNode(icu) {
  13057. return icu.visit(serializer);
  13058. }
  13059. class NodeWithI18n {
  13060. sourceSpan;
  13061. i18n;
  13062. constructor(sourceSpan, i18n) {
  13063. this.sourceSpan = sourceSpan;
  13064. this.i18n = i18n;
  13065. }
  13066. }
  13067. class Text extends NodeWithI18n {
  13068. value;
  13069. tokens;
  13070. constructor(value, sourceSpan, tokens, i18n) {
  13071. super(sourceSpan, i18n);
  13072. this.value = value;
  13073. this.tokens = tokens;
  13074. }
  13075. visit(visitor, context) {
  13076. return visitor.visitText(this, context);
  13077. }
  13078. }
  13079. class Expansion extends NodeWithI18n {
  13080. switchValue;
  13081. type;
  13082. cases;
  13083. switchValueSourceSpan;
  13084. constructor(switchValue, type, cases, sourceSpan, switchValueSourceSpan, i18n) {
  13085. super(sourceSpan, i18n);
  13086. this.switchValue = switchValue;
  13087. this.type = type;
  13088. this.cases = cases;
  13089. this.switchValueSourceSpan = switchValueSourceSpan;
  13090. }
  13091. visit(visitor, context) {
  13092. return visitor.visitExpansion(this, context);
  13093. }
  13094. }
  13095. class ExpansionCase {
  13096. value;
  13097. expression;
  13098. sourceSpan;
  13099. valueSourceSpan;
  13100. expSourceSpan;
  13101. constructor(value, expression, sourceSpan, valueSourceSpan, expSourceSpan) {
  13102. this.value = value;
  13103. this.expression = expression;
  13104. this.sourceSpan = sourceSpan;
  13105. this.valueSourceSpan = valueSourceSpan;
  13106. this.expSourceSpan = expSourceSpan;
  13107. }
  13108. visit(visitor, context) {
  13109. return visitor.visitExpansionCase(this, context);
  13110. }
  13111. }
  13112. class Attribute extends NodeWithI18n {
  13113. name;
  13114. value;
  13115. keySpan;
  13116. valueSpan;
  13117. valueTokens;
  13118. constructor(name, value, sourceSpan, keySpan, valueSpan, valueTokens, i18n) {
  13119. super(sourceSpan, i18n);
  13120. this.name = name;
  13121. this.value = value;
  13122. this.keySpan = keySpan;
  13123. this.valueSpan = valueSpan;
  13124. this.valueTokens = valueTokens;
  13125. }
  13126. visit(visitor, context) {
  13127. return visitor.visitAttribute(this, context);
  13128. }
  13129. }
  13130. class Element extends NodeWithI18n {
  13131. name;
  13132. attrs;
  13133. children;
  13134. startSourceSpan;
  13135. endSourceSpan;
  13136. constructor(name, attrs, children, sourceSpan, startSourceSpan, endSourceSpan = null, i18n) {
  13137. super(sourceSpan, i18n);
  13138. this.name = name;
  13139. this.attrs = attrs;
  13140. this.children = children;
  13141. this.startSourceSpan = startSourceSpan;
  13142. this.endSourceSpan = endSourceSpan;
  13143. }
  13144. visit(visitor, context) {
  13145. return visitor.visitElement(this, context);
  13146. }
  13147. }
  13148. class Comment {
  13149. value;
  13150. sourceSpan;
  13151. constructor(value, sourceSpan) {
  13152. this.value = value;
  13153. this.sourceSpan = sourceSpan;
  13154. }
  13155. visit(visitor, context) {
  13156. return visitor.visitComment(this, context);
  13157. }
  13158. }
  13159. class Block extends NodeWithI18n {
  13160. name;
  13161. parameters;
  13162. children;
  13163. nameSpan;
  13164. startSourceSpan;
  13165. endSourceSpan;
  13166. constructor(name, parameters, children, sourceSpan, nameSpan, startSourceSpan, endSourceSpan = null, i18n) {
  13167. super(sourceSpan, i18n);
  13168. this.name = name;
  13169. this.parameters = parameters;
  13170. this.children = children;
  13171. this.nameSpan = nameSpan;
  13172. this.startSourceSpan = startSourceSpan;
  13173. this.endSourceSpan = endSourceSpan;
  13174. }
  13175. visit(visitor, context) {
  13176. return visitor.visitBlock(this, context);
  13177. }
  13178. }
  13179. class BlockParameter {
  13180. expression;
  13181. sourceSpan;
  13182. constructor(expression, sourceSpan) {
  13183. this.expression = expression;
  13184. this.sourceSpan = sourceSpan;
  13185. }
  13186. visit(visitor, context) {
  13187. return visitor.visitBlockParameter(this, context);
  13188. }
  13189. }
  13190. class LetDeclaration {
  13191. name;
  13192. value;
  13193. sourceSpan;
  13194. nameSpan;
  13195. valueSpan;
  13196. constructor(name, value, sourceSpan, nameSpan, valueSpan) {
  13197. this.name = name;
  13198. this.value = value;
  13199. this.sourceSpan = sourceSpan;
  13200. this.nameSpan = nameSpan;
  13201. this.valueSpan = valueSpan;
  13202. }
  13203. visit(visitor, context) {
  13204. return visitor.visitLetDeclaration(this, context);
  13205. }
  13206. }
  13207. function visitAll(visitor, nodes, context = null) {
  13208. const result = [];
  13209. const visit = visitor.visit
  13210. ? (ast) => visitor.visit(ast, context) || ast.visit(visitor, context)
  13211. : (ast) => ast.visit(visitor, context);
  13212. nodes.forEach((ast) => {
  13213. const astResult = visit(ast);
  13214. if (astResult) {
  13215. result.push(astResult);
  13216. }
  13217. });
  13218. return result;
  13219. }
  13220. class RecursiveVisitor {
  13221. constructor() { }
  13222. visitElement(ast, context) {
  13223. this.visitChildren(context, (visit) => {
  13224. visit(ast.attrs);
  13225. visit(ast.children);
  13226. });
  13227. }
  13228. visitAttribute(ast, context) { }
  13229. visitText(ast, context) { }
  13230. visitComment(ast, context) { }
  13231. visitExpansion(ast, context) {
  13232. return this.visitChildren(context, (visit) => {
  13233. visit(ast.cases);
  13234. });
  13235. }
  13236. visitExpansionCase(ast, context) { }
  13237. visitBlock(block, context) {
  13238. this.visitChildren(context, (visit) => {
  13239. visit(block.parameters);
  13240. visit(block.children);
  13241. });
  13242. }
  13243. visitBlockParameter(ast, context) { }
  13244. visitLetDeclaration(decl, context) { }
  13245. visitChildren(context, cb) {
  13246. let results = [];
  13247. let t = this;
  13248. function visit(children) {
  13249. if (children)
  13250. results.push(visitAll(t, children, context));
  13251. }
  13252. cb(visit);
  13253. return Array.prototype.concat.apply([], results);
  13254. }
  13255. }
  13256. // Mapping between all HTML entity names and their unicode representation.
  13257. // Generated from https://html.spec.whatwg.org/multipage/entities.json by stripping
  13258. // the `&` and `;` from the keys and removing the duplicates.
  13259. // see https://www.w3.org/TR/html51/syntax.html#named-character-references
  13260. const NAMED_ENTITIES = {
  13261. 'AElig': '\u00C6',
  13262. 'AMP': '\u0026',
  13263. 'amp': '\u0026',
  13264. 'Aacute': '\u00C1',
  13265. 'Abreve': '\u0102',
  13266. 'Acirc': '\u00C2',
  13267. 'Acy': '\u0410',
  13268. 'Afr': '\uD835\uDD04',
  13269. 'Agrave': '\u00C0',
  13270. 'Alpha': '\u0391',
  13271. 'Amacr': '\u0100',
  13272. 'And': '\u2A53',
  13273. 'Aogon': '\u0104',
  13274. 'Aopf': '\uD835\uDD38',
  13275. 'ApplyFunction': '\u2061',
  13276. 'af': '\u2061',
  13277. 'Aring': '\u00C5',
  13278. 'angst': '\u00C5',
  13279. 'Ascr': '\uD835\uDC9C',
  13280. 'Assign': '\u2254',
  13281. 'colone': '\u2254',
  13282. 'coloneq': '\u2254',
  13283. 'Atilde': '\u00C3',
  13284. 'Auml': '\u00C4',
  13285. 'Backslash': '\u2216',
  13286. 'setminus': '\u2216',
  13287. 'setmn': '\u2216',
  13288. 'smallsetminus': '\u2216',
  13289. 'ssetmn': '\u2216',
  13290. 'Barv': '\u2AE7',
  13291. 'Barwed': '\u2306',
  13292. 'doublebarwedge': '\u2306',
  13293. 'Bcy': '\u0411',
  13294. 'Because': '\u2235',
  13295. 'becaus': '\u2235',
  13296. 'because': '\u2235',
  13297. 'Bernoullis': '\u212C',
  13298. 'Bscr': '\u212C',
  13299. 'bernou': '\u212C',
  13300. 'Beta': '\u0392',
  13301. 'Bfr': '\uD835\uDD05',
  13302. 'Bopf': '\uD835\uDD39',
  13303. 'Breve': '\u02D8',
  13304. 'breve': '\u02D8',
  13305. 'Bumpeq': '\u224E',
  13306. 'HumpDownHump': '\u224E',
  13307. 'bump': '\u224E',
  13308. 'CHcy': '\u0427',
  13309. 'COPY': '\u00A9',
  13310. 'copy': '\u00A9',
  13311. 'Cacute': '\u0106',
  13312. 'Cap': '\u22D2',
  13313. 'CapitalDifferentialD': '\u2145',
  13314. 'DD': '\u2145',
  13315. 'Cayleys': '\u212D',
  13316. 'Cfr': '\u212D',
  13317. 'Ccaron': '\u010C',
  13318. 'Ccedil': '\u00C7',
  13319. 'Ccirc': '\u0108',
  13320. 'Cconint': '\u2230',
  13321. 'Cdot': '\u010A',
  13322. 'Cedilla': '\u00B8',
  13323. 'cedil': '\u00B8',
  13324. 'CenterDot': '\u00B7',
  13325. 'centerdot': '\u00B7',
  13326. 'middot': '\u00B7',
  13327. 'Chi': '\u03A7',
  13328. 'CircleDot': '\u2299',
  13329. 'odot': '\u2299',
  13330. 'CircleMinus': '\u2296',
  13331. 'ominus': '\u2296',
  13332. 'CirclePlus': '\u2295',
  13333. 'oplus': '\u2295',
  13334. 'CircleTimes': '\u2297',
  13335. 'otimes': '\u2297',
  13336. 'ClockwiseContourIntegral': '\u2232',
  13337. 'cwconint': '\u2232',
  13338. 'CloseCurlyDoubleQuote': '\u201D',
  13339. 'rdquo': '\u201D',
  13340. 'rdquor': '\u201D',
  13341. 'CloseCurlyQuote': '\u2019',
  13342. 'rsquo': '\u2019',
  13343. 'rsquor': '\u2019',
  13344. 'Colon': '\u2237',
  13345. 'Proportion': '\u2237',
  13346. 'Colone': '\u2A74',
  13347. 'Congruent': '\u2261',
  13348. 'equiv': '\u2261',
  13349. 'Conint': '\u222F',
  13350. 'DoubleContourIntegral': '\u222F',
  13351. 'ContourIntegral': '\u222E',
  13352. 'conint': '\u222E',
  13353. 'oint': '\u222E',
  13354. 'Copf': '\u2102',
  13355. 'complexes': '\u2102',
  13356. 'Coproduct': '\u2210',
  13357. 'coprod': '\u2210',
  13358. 'CounterClockwiseContourIntegral': '\u2233',
  13359. 'awconint': '\u2233',
  13360. 'Cross': '\u2A2F',
  13361. 'Cscr': '\uD835\uDC9E',
  13362. 'Cup': '\u22D3',
  13363. 'CupCap': '\u224D',
  13364. 'asympeq': '\u224D',
  13365. 'DDotrahd': '\u2911',
  13366. 'DJcy': '\u0402',
  13367. 'DScy': '\u0405',
  13368. 'DZcy': '\u040F',
  13369. 'Dagger': '\u2021',
  13370. 'ddagger': '\u2021',
  13371. 'Darr': '\u21A1',
  13372. 'Dashv': '\u2AE4',
  13373. 'DoubleLeftTee': '\u2AE4',
  13374. 'Dcaron': '\u010E',
  13375. 'Dcy': '\u0414',
  13376. 'Del': '\u2207',
  13377. 'nabla': '\u2207',
  13378. 'Delta': '\u0394',
  13379. 'Dfr': '\uD835\uDD07',
  13380. 'DiacriticalAcute': '\u00B4',
  13381. 'acute': '\u00B4',
  13382. 'DiacriticalDot': '\u02D9',
  13383. 'dot': '\u02D9',
  13384. 'DiacriticalDoubleAcute': '\u02DD',
  13385. 'dblac': '\u02DD',
  13386. 'DiacriticalGrave': '\u0060',
  13387. 'grave': '\u0060',
  13388. 'DiacriticalTilde': '\u02DC',
  13389. 'tilde': '\u02DC',
  13390. 'Diamond': '\u22C4',
  13391. 'diam': '\u22C4',
  13392. 'diamond': '\u22C4',
  13393. 'DifferentialD': '\u2146',
  13394. 'dd': '\u2146',
  13395. 'Dopf': '\uD835\uDD3B',
  13396. 'Dot': '\u00A8',
  13397. 'DoubleDot': '\u00A8',
  13398. 'die': '\u00A8',
  13399. 'uml': '\u00A8',
  13400. 'DotDot': '\u20DC',
  13401. 'DotEqual': '\u2250',
  13402. 'doteq': '\u2250',
  13403. 'esdot': '\u2250',
  13404. 'DoubleDownArrow': '\u21D3',
  13405. 'Downarrow': '\u21D3',
  13406. 'dArr': '\u21D3',
  13407. 'DoubleLeftArrow': '\u21D0',
  13408. 'Leftarrow': '\u21D0',
  13409. 'lArr': '\u21D0',
  13410. 'DoubleLeftRightArrow': '\u21D4',
  13411. 'Leftrightarrow': '\u21D4',
  13412. 'hArr': '\u21D4',
  13413. 'iff': '\u21D4',
  13414. 'DoubleLongLeftArrow': '\u27F8',
  13415. 'Longleftarrow': '\u27F8',
  13416. 'xlArr': '\u27F8',
  13417. 'DoubleLongLeftRightArrow': '\u27FA',
  13418. 'Longleftrightarrow': '\u27FA',
  13419. 'xhArr': '\u27FA',
  13420. 'DoubleLongRightArrow': '\u27F9',
  13421. 'Longrightarrow': '\u27F9',
  13422. 'xrArr': '\u27F9',
  13423. 'DoubleRightArrow': '\u21D2',
  13424. 'Implies': '\u21D2',
  13425. 'Rightarrow': '\u21D2',
  13426. 'rArr': '\u21D2',
  13427. 'DoubleRightTee': '\u22A8',
  13428. 'vDash': '\u22A8',
  13429. 'DoubleUpArrow': '\u21D1',
  13430. 'Uparrow': '\u21D1',
  13431. 'uArr': '\u21D1',
  13432. 'DoubleUpDownArrow': '\u21D5',
  13433. 'Updownarrow': '\u21D5',
  13434. 'vArr': '\u21D5',
  13435. 'DoubleVerticalBar': '\u2225',
  13436. 'par': '\u2225',
  13437. 'parallel': '\u2225',
  13438. 'shortparallel': '\u2225',
  13439. 'spar': '\u2225',
  13440. 'DownArrow': '\u2193',
  13441. 'ShortDownArrow': '\u2193',
  13442. 'darr': '\u2193',
  13443. 'downarrow': '\u2193',
  13444. 'DownArrowBar': '\u2913',
  13445. 'DownArrowUpArrow': '\u21F5',
  13446. 'duarr': '\u21F5',
  13447. 'DownBreve': '\u0311',
  13448. 'DownLeftRightVector': '\u2950',
  13449. 'DownLeftTeeVector': '\u295E',
  13450. 'DownLeftVector': '\u21BD',
  13451. 'leftharpoondown': '\u21BD',
  13452. 'lhard': '\u21BD',
  13453. 'DownLeftVectorBar': '\u2956',
  13454. 'DownRightTeeVector': '\u295F',
  13455. 'DownRightVector': '\u21C1',
  13456. 'rhard': '\u21C1',
  13457. 'rightharpoondown': '\u21C1',
  13458. 'DownRightVectorBar': '\u2957',
  13459. 'DownTee': '\u22A4',
  13460. 'top': '\u22A4',
  13461. 'DownTeeArrow': '\u21A7',
  13462. 'mapstodown': '\u21A7',
  13463. 'Dscr': '\uD835\uDC9F',
  13464. 'Dstrok': '\u0110',
  13465. 'ENG': '\u014A',
  13466. 'ETH': '\u00D0',
  13467. 'Eacute': '\u00C9',
  13468. 'Ecaron': '\u011A',
  13469. 'Ecirc': '\u00CA',
  13470. 'Ecy': '\u042D',
  13471. 'Edot': '\u0116',
  13472. 'Efr': '\uD835\uDD08',
  13473. 'Egrave': '\u00C8',
  13474. 'Element': '\u2208',
  13475. 'in': '\u2208',
  13476. 'isin': '\u2208',
  13477. 'isinv': '\u2208',
  13478. 'Emacr': '\u0112',
  13479. 'EmptySmallSquare': '\u25FB',
  13480. 'EmptyVerySmallSquare': '\u25AB',
  13481. 'Eogon': '\u0118',
  13482. 'Eopf': '\uD835\uDD3C',
  13483. 'Epsilon': '\u0395',
  13484. 'Equal': '\u2A75',
  13485. 'EqualTilde': '\u2242',
  13486. 'eqsim': '\u2242',
  13487. 'esim': '\u2242',
  13488. 'Equilibrium': '\u21CC',
  13489. 'rightleftharpoons': '\u21CC',
  13490. 'rlhar': '\u21CC',
  13491. 'Escr': '\u2130',
  13492. 'expectation': '\u2130',
  13493. 'Esim': '\u2A73',
  13494. 'Eta': '\u0397',
  13495. 'Euml': '\u00CB',
  13496. 'Exists': '\u2203',
  13497. 'exist': '\u2203',
  13498. 'ExponentialE': '\u2147',
  13499. 'ee': '\u2147',
  13500. 'exponentiale': '\u2147',
  13501. 'Fcy': '\u0424',
  13502. 'Ffr': '\uD835\uDD09',
  13503. 'FilledSmallSquare': '\u25FC',
  13504. 'FilledVerySmallSquare': '\u25AA',
  13505. 'blacksquare': '\u25AA',
  13506. 'squarf': '\u25AA',
  13507. 'squf': '\u25AA',
  13508. 'Fopf': '\uD835\uDD3D',
  13509. 'ForAll': '\u2200',
  13510. 'forall': '\u2200',
  13511. 'Fouriertrf': '\u2131',
  13512. 'Fscr': '\u2131',
  13513. 'GJcy': '\u0403',
  13514. 'GT': '\u003E',
  13515. 'gt': '\u003E',
  13516. 'Gamma': '\u0393',
  13517. 'Gammad': '\u03DC',
  13518. 'Gbreve': '\u011E',
  13519. 'Gcedil': '\u0122',
  13520. 'Gcirc': '\u011C',
  13521. 'Gcy': '\u0413',
  13522. 'Gdot': '\u0120',
  13523. 'Gfr': '\uD835\uDD0A',
  13524. 'Gg': '\u22D9',
  13525. 'ggg': '\u22D9',
  13526. 'Gopf': '\uD835\uDD3E',
  13527. 'GreaterEqual': '\u2265',
  13528. 'ge': '\u2265',
  13529. 'geq': '\u2265',
  13530. 'GreaterEqualLess': '\u22DB',
  13531. 'gel': '\u22DB',
  13532. 'gtreqless': '\u22DB',
  13533. 'GreaterFullEqual': '\u2267',
  13534. 'gE': '\u2267',
  13535. 'geqq': '\u2267',
  13536. 'GreaterGreater': '\u2AA2',
  13537. 'GreaterLess': '\u2277',
  13538. 'gl': '\u2277',
  13539. 'gtrless': '\u2277',
  13540. 'GreaterSlantEqual': '\u2A7E',
  13541. 'geqslant': '\u2A7E',
  13542. 'ges': '\u2A7E',
  13543. 'GreaterTilde': '\u2273',
  13544. 'gsim': '\u2273',
  13545. 'gtrsim': '\u2273',
  13546. 'Gscr': '\uD835\uDCA2',
  13547. 'Gt': '\u226B',
  13548. 'NestedGreaterGreater': '\u226B',
  13549. 'gg': '\u226B',
  13550. 'HARDcy': '\u042A',
  13551. 'Hacek': '\u02C7',
  13552. 'caron': '\u02C7',
  13553. 'Hat': '\u005E',
  13554. 'Hcirc': '\u0124',
  13555. 'Hfr': '\u210C',
  13556. 'Poincareplane': '\u210C',
  13557. 'HilbertSpace': '\u210B',
  13558. 'Hscr': '\u210B',
  13559. 'hamilt': '\u210B',
  13560. 'Hopf': '\u210D',
  13561. 'quaternions': '\u210D',
  13562. 'HorizontalLine': '\u2500',
  13563. 'boxh': '\u2500',
  13564. 'Hstrok': '\u0126',
  13565. 'HumpEqual': '\u224F',
  13566. 'bumpe': '\u224F',
  13567. 'bumpeq': '\u224F',
  13568. 'IEcy': '\u0415',
  13569. 'IJlig': '\u0132',
  13570. 'IOcy': '\u0401',
  13571. 'Iacute': '\u00CD',
  13572. 'Icirc': '\u00CE',
  13573. 'Icy': '\u0418',
  13574. 'Idot': '\u0130',
  13575. 'Ifr': '\u2111',
  13576. 'Im': '\u2111',
  13577. 'image': '\u2111',
  13578. 'imagpart': '\u2111',
  13579. 'Igrave': '\u00CC',
  13580. 'Imacr': '\u012A',
  13581. 'ImaginaryI': '\u2148',
  13582. 'ii': '\u2148',
  13583. 'Int': '\u222C',
  13584. 'Integral': '\u222B',
  13585. 'int': '\u222B',
  13586. 'Intersection': '\u22C2',
  13587. 'bigcap': '\u22C2',
  13588. 'xcap': '\u22C2',
  13589. 'InvisibleComma': '\u2063',
  13590. 'ic': '\u2063',
  13591. 'InvisibleTimes': '\u2062',
  13592. 'it': '\u2062',
  13593. 'Iogon': '\u012E',
  13594. 'Iopf': '\uD835\uDD40',
  13595. 'Iota': '\u0399',
  13596. 'Iscr': '\u2110',
  13597. 'imagline': '\u2110',
  13598. 'Itilde': '\u0128',
  13599. 'Iukcy': '\u0406',
  13600. 'Iuml': '\u00CF',
  13601. 'Jcirc': '\u0134',
  13602. 'Jcy': '\u0419',
  13603. 'Jfr': '\uD835\uDD0D',
  13604. 'Jopf': '\uD835\uDD41',
  13605. 'Jscr': '\uD835\uDCA5',
  13606. 'Jsercy': '\u0408',
  13607. 'Jukcy': '\u0404',
  13608. 'KHcy': '\u0425',
  13609. 'KJcy': '\u040C',
  13610. 'Kappa': '\u039A',
  13611. 'Kcedil': '\u0136',
  13612. 'Kcy': '\u041A',
  13613. 'Kfr': '\uD835\uDD0E',
  13614. 'Kopf': '\uD835\uDD42',
  13615. 'Kscr': '\uD835\uDCA6',
  13616. 'LJcy': '\u0409',
  13617. 'LT': '\u003C',
  13618. 'lt': '\u003C',
  13619. 'Lacute': '\u0139',
  13620. 'Lambda': '\u039B',
  13621. 'Lang': '\u27EA',
  13622. 'Laplacetrf': '\u2112',
  13623. 'Lscr': '\u2112',
  13624. 'lagran': '\u2112',
  13625. 'Larr': '\u219E',
  13626. 'twoheadleftarrow': '\u219E',
  13627. 'Lcaron': '\u013D',
  13628. 'Lcedil': '\u013B',
  13629. 'Lcy': '\u041B',
  13630. 'LeftAngleBracket': '\u27E8',
  13631. 'lang': '\u27E8',
  13632. 'langle': '\u27E8',
  13633. 'LeftArrow': '\u2190',
  13634. 'ShortLeftArrow': '\u2190',
  13635. 'larr': '\u2190',
  13636. 'leftarrow': '\u2190',
  13637. 'slarr': '\u2190',
  13638. 'LeftArrowBar': '\u21E4',
  13639. 'larrb': '\u21E4',
  13640. 'LeftArrowRightArrow': '\u21C6',
  13641. 'leftrightarrows': '\u21C6',
  13642. 'lrarr': '\u21C6',
  13643. 'LeftCeiling': '\u2308',
  13644. 'lceil': '\u2308',
  13645. 'LeftDoubleBracket': '\u27E6',
  13646. 'lobrk': '\u27E6',
  13647. 'LeftDownTeeVector': '\u2961',
  13648. 'LeftDownVector': '\u21C3',
  13649. 'dharl': '\u21C3',
  13650. 'downharpoonleft': '\u21C3',
  13651. 'LeftDownVectorBar': '\u2959',
  13652. 'LeftFloor': '\u230A',
  13653. 'lfloor': '\u230A',
  13654. 'LeftRightArrow': '\u2194',
  13655. 'harr': '\u2194',
  13656. 'leftrightarrow': '\u2194',
  13657. 'LeftRightVector': '\u294E',
  13658. 'LeftTee': '\u22A3',
  13659. 'dashv': '\u22A3',
  13660. 'LeftTeeArrow': '\u21A4',
  13661. 'mapstoleft': '\u21A4',
  13662. 'LeftTeeVector': '\u295A',
  13663. 'LeftTriangle': '\u22B2',
  13664. 'vartriangleleft': '\u22B2',
  13665. 'vltri': '\u22B2',
  13666. 'LeftTriangleBar': '\u29CF',
  13667. 'LeftTriangleEqual': '\u22B4',
  13668. 'ltrie': '\u22B4',
  13669. 'trianglelefteq': '\u22B4',
  13670. 'LeftUpDownVector': '\u2951',
  13671. 'LeftUpTeeVector': '\u2960',
  13672. 'LeftUpVector': '\u21BF',
  13673. 'uharl': '\u21BF',
  13674. 'upharpoonleft': '\u21BF',
  13675. 'LeftUpVectorBar': '\u2958',
  13676. 'LeftVector': '\u21BC',
  13677. 'leftharpoonup': '\u21BC',
  13678. 'lharu': '\u21BC',
  13679. 'LeftVectorBar': '\u2952',
  13680. 'LessEqualGreater': '\u22DA',
  13681. 'leg': '\u22DA',
  13682. 'lesseqgtr': '\u22DA',
  13683. 'LessFullEqual': '\u2266',
  13684. 'lE': '\u2266',
  13685. 'leqq': '\u2266',
  13686. 'LessGreater': '\u2276',
  13687. 'lessgtr': '\u2276',
  13688. 'lg': '\u2276',
  13689. 'LessLess': '\u2AA1',
  13690. 'LessSlantEqual': '\u2A7D',
  13691. 'leqslant': '\u2A7D',
  13692. 'les': '\u2A7D',
  13693. 'LessTilde': '\u2272',
  13694. 'lesssim': '\u2272',
  13695. 'lsim': '\u2272',
  13696. 'Lfr': '\uD835\uDD0F',
  13697. 'Ll': '\u22D8',
  13698. 'Lleftarrow': '\u21DA',
  13699. 'lAarr': '\u21DA',
  13700. 'Lmidot': '\u013F',
  13701. 'LongLeftArrow': '\u27F5',
  13702. 'longleftarrow': '\u27F5',
  13703. 'xlarr': '\u27F5',
  13704. 'LongLeftRightArrow': '\u27F7',
  13705. 'longleftrightarrow': '\u27F7',
  13706. 'xharr': '\u27F7',
  13707. 'LongRightArrow': '\u27F6',
  13708. 'longrightarrow': '\u27F6',
  13709. 'xrarr': '\u27F6',
  13710. 'Lopf': '\uD835\uDD43',
  13711. 'LowerLeftArrow': '\u2199',
  13712. 'swarr': '\u2199',
  13713. 'swarrow': '\u2199',
  13714. 'LowerRightArrow': '\u2198',
  13715. 'searr': '\u2198',
  13716. 'searrow': '\u2198',
  13717. 'Lsh': '\u21B0',
  13718. 'lsh': '\u21B0',
  13719. 'Lstrok': '\u0141',
  13720. 'Lt': '\u226A',
  13721. 'NestedLessLess': '\u226A',
  13722. 'll': '\u226A',
  13723. 'Map': '\u2905',
  13724. 'Mcy': '\u041C',
  13725. 'MediumSpace': '\u205F',
  13726. 'Mellintrf': '\u2133',
  13727. 'Mscr': '\u2133',
  13728. 'phmmat': '\u2133',
  13729. 'Mfr': '\uD835\uDD10',
  13730. 'MinusPlus': '\u2213',
  13731. 'mnplus': '\u2213',
  13732. 'mp': '\u2213',
  13733. 'Mopf': '\uD835\uDD44',
  13734. 'Mu': '\u039C',
  13735. 'NJcy': '\u040A',
  13736. 'Nacute': '\u0143',
  13737. 'Ncaron': '\u0147',
  13738. 'Ncedil': '\u0145',
  13739. 'Ncy': '\u041D',
  13740. 'NegativeMediumSpace': '\u200B',
  13741. 'NegativeThickSpace': '\u200B',
  13742. 'NegativeThinSpace': '\u200B',
  13743. 'NegativeVeryThinSpace': '\u200B',
  13744. 'ZeroWidthSpace': '\u200B',
  13745. 'NewLine': '\u000A',
  13746. 'Nfr': '\uD835\uDD11',
  13747. 'NoBreak': '\u2060',
  13748. 'NonBreakingSpace': '\u00A0',
  13749. 'nbsp': '\u00A0',
  13750. 'Nopf': '\u2115',
  13751. 'naturals': '\u2115',
  13752. 'Not': '\u2AEC',
  13753. 'NotCongruent': '\u2262',
  13754. 'nequiv': '\u2262',
  13755. 'NotCupCap': '\u226D',
  13756. 'NotDoubleVerticalBar': '\u2226',
  13757. 'npar': '\u2226',
  13758. 'nparallel': '\u2226',
  13759. 'nshortparallel': '\u2226',
  13760. 'nspar': '\u2226',
  13761. 'NotElement': '\u2209',
  13762. 'notin': '\u2209',
  13763. 'notinva': '\u2209',
  13764. 'NotEqual': '\u2260',
  13765. 'ne': '\u2260',
  13766. 'NotEqualTilde': '\u2242\u0338',
  13767. 'nesim': '\u2242\u0338',
  13768. 'NotExists': '\u2204',
  13769. 'nexist': '\u2204',
  13770. 'nexists': '\u2204',
  13771. 'NotGreater': '\u226F',
  13772. 'ngt': '\u226F',
  13773. 'ngtr': '\u226F',
  13774. 'NotGreaterEqual': '\u2271',
  13775. 'nge': '\u2271',
  13776. 'ngeq': '\u2271',
  13777. 'NotGreaterFullEqual': '\u2267\u0338',
  13778. 'ngE': '\u2267\u0338',
  13779. 'ngeqq': '\u2267\u0338',
  13780. 'NotGreaterGreater': '\u226B\u0338',
  13781. 'nGtv': '\u226B\u0338',
  13782. 'NotGreaterLess': '\u2279',
  13783. 'ntgl': '\u2279',
  13784. 'NotGreaterSlantEqual': '\u2A7E\u0338',
  13785. 'ngeqslant': '\u2A7E\u0338',
  13786. 'nges': '\u2A7E\u0338',
  13787. 'NotGreaterTilde': '\u2275',
  13788. 'ngsim': '\u2275',
  13789. 'NotHumpDownHump': '\u224E\u0338',
  13790. 'nbump': '\u224E\u0338',
  13791. 'NotHumpEqual': '\u224F\u0338',
  13792. 'nbumpe': '\u224F\u0338',
  13793. 'NotLeftTriangle': '\u22EA',
  13794. 'nltri': '\u22EA',
  13795. 'ntriangleleft': '\u22EA',
  13796. 'NotLeftTriangleBar': '\u29CF\u0338',
  13797. 'NotLeftTriangleEqual': '\u22EC',
  13798. 'nltrie': '\u22EC',
  13799. 'ntrianglelefteq': '\u22EC',
  13800. 'NotLess': '\u226E',
  13801. 'nless': '\u226E',
  13802. 'nlt': '\u226E',
  13803. 'NotLessEqual': '\u2270',
  13804. 'nle': '\u2270',
  13805. 'nleq': '\u2270',
  13806. 'NotLessGreater': '\u2278',
  13807. 'ntlg': '\u2278',
  13808. 'NotLessLess': '\u226A\u0338',
  13809. 'nLtv': '\u226A\u0338',
  13810. 'NotLessSlantEqual': '\u2A7D\u0338',
  13811. 'nleqslant': '\u2A7D\u0338',
  13812. 'nles': '\u2A7D\u0338',
  13813. 'NotLessTilde': '\u2274',
  13814. 'nlsim': '\u2274',
  13815. 'NotNestedGreaterGreater': '\u2AA2\u0338',
  13816. 'NotNestedLessLess': '\u2AA1\u0338',
  13817. 'NotPrecedes': '\u2280',
  13818. 'npr': '\u2280',
  13819. 'nprec': '\u2280',
  13820. 'NotPrecedesEqual': '\u2AAF\u0338',
  13821. 'npre': '\u2AAF\u0338',
  13822. 'npreceq': '\u2AAF\u0338',
  13823. 'NotPrecedesSlantEqual': '\u22E0',
  13824. 'nprcue': '\u22E0',
  13825. 'NotReverseElement': '\u220C',
  13826. 'notni': '\u220C',
  13827. 'notniva': '\u220C',
  13828. 'NotRightTriangle': '\u22EB',
  13829. 'nrtri': '\u22EB',
  13830. 'ntriangleright': '\u22EB',
  13831. 'NotRightTriangleBar': '\u29D0\u0338',
  13832. 'NotRightTriangleEqual': '\u22ED',
  13833. 'nrtrie': '\u22ED',
  13834. 'ntrianglerighteq': '\u22ED',
  13835. 'NotSquareSubset': '\u228F\u0338',
  13836. 'NotSquareSubsetEqual': '\u22E2',
  13837. 'nsqsube': '\u22E2',
  13838. 'NotSquareSuperset': '\u2290\u0338',
  13839. 'NotSquareSupersetEqual': '\u22E3',
  13840. 'nsqsupe': '\u22E3',
  13841. 'NotSubset': '\u2282\u20D2',
  13842. 'nsubset': '\u2282\u20D2',
  13843. 'vnsub': '\u2282\u20D2',
  13844. 'NotSubsetEqual': '\u2288',
  13845. 'nsube': '\u2288',
  13846. 'nsubseteq': '\u2288',
  13847. 'NotSucceeds': '\u2281',
  13848. 'nsc': '\u2281',
  13849. 'nsucc': '\u2281',
  13850. 'NotSucceedsEqual': '\u2AB0\u0338',
  13851. 'nsce': '\u2AB0\u0338',
  13852. 'nsucceq': '\u2AB0\u0338',
  13853. 'NotSucceedsSlantEqual': '\u22E1',
  13854. 'nsccue': '\u22E1',
  13855. 'NotSucceedsTilde': '\u227F\u0338',
  13856. 'NotSuperset': '\u2283\u20D2',
  13857. 'nsupset': '\u2283\u20D2',
  13858. 'vnsup': '\u2283\u20D2',
  13859. 'NotSupersetEqual': '\u2289',
  13860. 'nsupe': '\u2289',
  13861. 'nsupseteq': '\u2289',
  13862. 'NotTilde': '\u2241',
  13863. 'nsim': '\u2241',
  13864. 'NotTildeEqual': '\u2244',
  13865. 'nsime': '\u2244',
  13866. 'nsimeq': '\u2244',
  13867. 'NotTildeFullEqual': '\u2247',
  13868. 'ncong': '\u2247',
  13869. 'NotTildeTilde': '\u2249',
  13870. 'nap': '\u2249',
  13871. 'napprox': '\u2249',
  13872. 'NotVerticalBar': '\u2224',
  13873. 'nmid': '\u2224',
  13874. 'nshortmid': '\u2224',
  13875. 'nsmid': '\u2224',
  13876. 'Nscr': '\uD835\uDCA9',
  13877. 'Ntilde': '\u00D1',
  13878. 'Nu': '\u039D',
  13879. 'OElig': '\u0152',
  13880. 'Oacute': '\u00D3',
  13881. 'Ocirc': '\u00D4',
  13882. 'Ocy': '\u041E',
  13883. 'Odblac': '\u0150',
  13884. 'Ofr': '\uD835\uDD12',
  13885. 'Ograve': '\u00D2',
  13886. 'Omacr': '\u014C',
  13887. 'Omega': '\u03A9',
  13888. 'ohm': '\u03A9',
  13889. 'Omicron': '\u039F',
  13890. 'Oopf': '\uD835\uDD46',
  13891. 'OpenCurlyDoubleQuote': '\u201C',
  13892. 'ldquo': '\u201C',
  13893. 'OpenCurlyQuote': '\u2018',
  13894. 'lsquo': '\u2018',
  13895. 'Or': '\u2A54',
  13896. 'Oscr': '\uD835\uDCAA',
  13897. 'Oslash': '\u00D8',
  13898. 'Otilde': '\u00D5',
  13899. 'Otimes': '\u2A37',
  13900. 'Ouml': '\u00D6',
  13901. 'OverBar': '\u203E',
  13902. 'oline': '\u203E',
  13903. 'OverBrace': '\u23DE',
  13904. 'OverBracket': '\u23B4',
  13905. 'tbrk': '\u23B4',
  13906. 'OverParenthesis': '\u23DC',
  13907. 'PartialD': '\u2202',
  13908. 'part': '\u2202',
  13909. 'Pcy': '\u041F',
  13910. 'Pfr': '\uD835\uDD13',
  13911. 'Phi': '\u03A6',
  13912. 'Pi': '\u03A0',
  13913. 'PlusMinus': '\u00B1',
  13914. 'plusmn': '\u00B1',
  13915. 'pm': '\u00B1',
  13916. 'Popf': '\u2119',
  13917. 'primes': '\u2119',
  13918. 'Pr': '\u2ABB',
  13919. 'Precedes': '\u227A',
  13920. 'pr': '\u227A',
  13921. 'prec': '\u227A',
  13922. 'PrecedesEqual': '\u2AAF',
  13923. 'pre': '\u2AAF',
  13924. 'preceq': '\u2AAF',
  13925. 'PrecedesSlantEqual': '\u227C',
  13926. 'prcue': '\u227C',
  13927. 'preccurlyeq': '\u227C',
  13928. 'PrecedesTilde': '\u227E',
  13929. 'precsim': '\u227E',
  13930. 'prsim': '\u227E',
  13931. 'Prime': '\u2033',
  13932. 'Product': '\u220F',
  13933. 'prod': '\u220F',
  13934. 'Proportional': '\u221D',
  13935. 'prop': '\u221D',
  13936. 'propto': '\u221D',
  13937. 'varpropto': '\u221D',
  13938. 'vprop': '\u221D',
  13939. 'Pscr': '\uD835\uDCAB',
  13940. 'Psi': '\u03A8',
  13941. 'QUOT': '\u0022',
  13942. 'quot': '\u0022',
  13943. 'Qfr': '\uD835\uDD14',
  13944. 'Qopf': '\u211A',
  13945. 'rationals': '\u211A',
  13946. 'Qscr': '\uD835\uDCAC',
  13947. 'RBarr': '\u2910',
  13948. 'drbkarow': '\u2910',
  13949. 'REG': '\u00AE',
  13950. 'circledR': '\u00AE',
  13951. 'reg': '\u00AE',
  13952. 'Racute': '\u0154',
  13953. 'Rang': '\u27EB',
  13954. 'Rarr': '\u21A0',
  13955. 'twoheadrightarrow': '\u21A0',
  13956. 'Rarrtl': '\u2916',
  13957. 'Rcaron': '\u0158',
  13958. 'Rcedil': '\u0156',
  13959. 'Rcy': '\u0420',
  13960. 'Re': '\u211C',
  13961. 'Rfr': '\u211C',
  13962. 'real': '\u211C',
  13963. 'realpart': '\u211C',
  13964. 'ReverseElement': '\u220B',
  13965. 'SuchThat': '\u220B',
  13966. 'ni': '\u220B',
  13967. 'niv': '\u220B',
  13968. 'ReverseEquilibrium': '\u21CB',
  13969. 'leftrightharpoons': '\u21CB',
  13970. 'lrhar': '\u21CB',
  13971. 'ReverseUpEquilibrium': '\u296F',
  13972. 'duhar': '\u296F',
  13973. 'Rho': '\u03A1',
  13974. 'RightAngleBracket': '\u27E9',
  13975. 'rang': '\u27E9',
  13976. 'rangle': '\u27E9',
  13977. 'RightArrow': '\u2192',
  13978. 'ShortRightArrow': '\u2192',
  13979. 'rarr': '\u2192',
  13980. 'rightarrow': '\u2192',
  13981. 'srarr': '\u2192',
  13982. 'RightArrowBar': '\u21E5',
  13983. 'rarrb': '\u21E5',
  13984. 'RightArrowLeftArrow': '\u21C4',
  13985. 'rightleftarrows': '\u21C4',
  13986. 'rlarr': '\u21C4',
  13987. 'RightCeiling': '\u2309',
  13988. 'rceil': '\u2309',
  13989. 'RightDoubleBracket': '\u27E7',
  13990. 'robrk': '\u27E7',
  13991. 'RightDownTeeVector': '\u295D',
  13992. 'RightDownVector': '\u21C2',
  13993. 'dharr': '\u21C2',
  13994. 'downharpoonright': '\u21C2',
  13995. 'RightDownVectorBar': '\u2955',
  13996. 'RightFloor': '\u230B',
  13997. 'rfloor': '\u230B',
  13998. 'RightTee': '\u22A2',
  13999. 'vdash': '\u22A2',
  14000. 'RightTeeArrow': '\u21A6',
  14001. 'map': '\u21A6',
  14002. 'mapsto': '\u21A6',
  14003. 'RightTeeVector': '\u295B',
  14004. 'RightTriangle': '\u22B3',
  14005. 'vartriangleright': '\u22B3',
  14006. 'vrtri': '\u22B3',
  14007. 'RightTriangleBar': '\u29D0',
  14008. 'RightTriangleEqual': '\u22B5',
  14009. 'rtrie': '\u22B5',
  14010. 'trianglerighteq': '\u22B5',
  14011. 'RightUpDownVector': '\u294F',
  14012. 'RightUpTeeVector': '\u295C',
  14013. 'RightUpVector': '\u21BE',
  14014. 'uharr': '\u21BE',
  14015. 'upharpoonright': '\u21BE',
  14016. 'RightUpVectorBar': '\u2954',
  14017. 'RightVector': '\u21C0',
  14018. 'rharu': '\u21C0',
  14019. 'rightharpoonup': '\u21C0',
  14020. 'RightVectorBar': '\u2953',
  14021. 'Ropf': '\u211D',
  14022. 'reals': '\u211D',
  14023. 'RoundImplies': '\u2970',
  14024. 'Rrightarrow': '\u21DB',
  14025. 'rAarr': '\u21DB',
  14026. 'Rscr': '\u211B',
  14027. 'realine': '\u211B',
  14028. 'Rsh': '\u21B1',
  14029. 'rsh': '\u21B1',
  14030. 'RuleDelayed': '\u29F4',
  14031. 'SHCHcy': '\u0429',
  14032. 'SHcy': '\u0428',
  14033. 'SOFTcy': '\u042C',
  14034. 'Sacute': '\u015A',
  14035. 'Sc': '\u2ABC',
  14036. 'Scaron': '\u0160',
  14037. 'Scedil': '\u015E',
  14038. 'Scirc': '\u015C',
  14039. 'Scy': '\u0421',
  14040. 'Sfr': '\uD835\uDD16',
  14041. 'ShortUpArrow': '\u2191',
  14042. 'UpArrow': '\u2191',
  14043. 'uarr': '\u2191',
  14044. 'uparrow': '\u2191',
  14045. 'Sigma': '\u03A3',
  14046. 'SmallCircle': '\u2218',
  14047. 'compfn': '\u2218',
  14048. 'Sopf': '\uD835\uDD4A',
  14049. 'Sqrt': '\u221A',
  14050. 'radic': '\u221A',
  14051. 'Square': '\u25A1',
  14052. 'squ': '\u25A1',
  14053. 'square': '\u25A1',
  14054. 'SquareIntersection': '\u2293',
  14055. 'sqcap': '\u2293',
  14056. 'SquareSubset': '\u228F',
  14057. 'sqsub': '\u228F',
  14058. 'sqsubset': '\u228F',
  14059. 'SquareSubsetEqual': '\u2291',
  14060. 'sqsube': '\u2291',
  14061. 'sqsubseteq': '\u2291',
  14062. 'SquareSuperset': '\u2290',
  14063. 'sqsup': '\u2290',
  14064. 'sqsupset': '\u2290',
  14065. 'SquareSupersetEqual': '\u2292',
  14066. 'sqsupe': '\u2292',
  14067. 'sqsupseteq': '\u2292',
  14068. 'SquareUnion': '\u2294',
  14069. 'sqcup': '\u2294',
  14070. 'Sscr': '\uD835\uDCAE',
  14071. 'Star': '\u22C6',
  14072. 'sstarf': '\u22C6',
  14073. 'Sub': '\u22D0',
  14074. 'Subset': '\u22D0',
  14075. 'SubsetEqual': '\u2286',
  14076. 'sube': '\u2286',
  14077. 'subseteq': '\u2286',
  14078. 'Succeeds': '\u227B',
  14079. 'sc': '\u227B',
  14080. 'succ': '\u227B',
  14081. 'SucceedsEqual': '\u2AB0',
  14082. 'sce': '\u2AB0',
  14083. 'succeq': '\u2AB0',
  14084. 'SucceedsSlantEqual': '\u227D',
  14085. 'sccue': '\u227D',
  14086. 'succcurlyeq': '\u227D',
  14087. 'SucceedsTilde': '\u227F',
  14088. 'scsim': '\u227F',
  14089. 'succsim': '\u227F',
  14090. 'Sum': '\u2211',
  14091. 'sum': '\u2211',
  14092. 'Sup': '\u22D1',
  14093. 'Supset': '\u22D1',
  14094. 'Superset': '\u2283',
  14095. 'sup': '\u2283',
  14096. 'supset': '\u2283',
  14097. 'SupersetEqual': '\u2287',
  14098. 'supe': '\u2287',
  14099. 'supseteq': '\u2287',
  14100. 'THORN': '\u00DE',
  14101. 'TRADE': '\u2122',
  14102. 'trade': '\u2122',
  14103. 'TSHcy': '\u040B',
  14104. 'TScy': '\u0426',
  14105. 'Tab': '\u0009',
  14106. 'Tau': '\u03A4',
  14107. 'Tcaron': '\u0164',
  14108. 'Tcedil': '\u0162',
  14109. 'Tcy': '\u0422',
  14110. 'Tfr': '\uD835\uDD17',
  14111. 'Therefore': '\u2234',
  14112. 'there4': '\u2234',
  14113. 'therefore': '\u2234',
  14114. 'Theta': '\u0398',
  14115. 'ThickSpace': '\u205F\u200A',
  14116. 'ThinSpace': '\u2009',
  14117. 'thinsp': '\u2009',
  14118. 'Tilde': '\u223C',
  14119. 'sim': '\u223C',
  14120. 'thicksim': '\u223C',
  14121. 'thksim': '\u223C',
  14122. 'TildeEqual': '\u2243',
  14123. 'sime': '\u2243',
  14124. 'simeq': '\u2243',
  14125. 'TildeFullEqual': '\u2245',
  14126. 'cong': '\u2245',
  14127. 'TildeTilde': '\u2248',
  14128. 'ap': '\u2248',
  14129. 'approx': '\u2248',
  14130. 'asymp': '\u2248',
  14131. 'thickapprox': '\u2248',
  14132. 'thkap': '\u2248',
  14133. 'Topf': '\uD835\uDD4B',
  14134. 'TripleDot': '\u20DB',
  14135. 'tdot': '\u20DB',
  14136. 'Tscr': '\uD835\uDCAF',
  14137. 'Tstrok': '\u0166',
  14138. 'Uacute': '\u00DA',
  14139. 'Uarr': '\u219F',
  14140. 'Uarrocir': '\u2949',
  14141. 'Ubrcy': '\u040E',
  14142. 'Ubreve': '\u016C',
  14143. 'Ucirc': '\u00DB',
  14144. 'Ucy': '\u0423',
  14145. 'Udblac': '\u0170',
  14146. 'Ufr': '\uD835\uDD18',
  14147. 'Ugrave': '\u00D9',
  14148. 'Umacr': '\u016A',
  14149. 'UnderBar': '\u005F',
  14150. 'lowbar': '\u005F',
  14151. 'UnderBrace': '\u23DF',
  14152. 'UnderBracket': '\u23B5',
  14153. 'bbrk': '\u23B5',
  14154. 'UnderParenthesis': '\u23DD',
  14155. 'Union': '\u22C3',
  14156. 'bigcup': '\u22C3',
  14157. 'xcup': '\u22C3',
  14158. 'UnionPlus': '\u228E',
  14159. 'uplus': '\u228E',
  14160. 'Uogon': '\u0172',
  14161. 'Uopf': '\uD835\uDD4C',
  14162. 'UpArrowBar': '\u2912',
  14163. 'UpArrowDownArrow': '\u21C5',
  14164. 'udarr': '\u21C5',
  14165. 'UpDownArrow': '\u2195',
  14166. 'updownarrow': '\u2195',
  14167. 'varr': '\u2195',
  14168. 'UpEquilibrium': '\u296E',
  14169. 'udhar': '\u296E',
  14170. 'UpTee': '\u22A5',
  14171. 'bot': '\u22A5',
  14172. 'bottom': '\u22A5',
  14173. 'perp': '\u22A5',
  14174. 'UpTeeArrow': '\u21A5',
  14175. 'mapstoup': '\u21A5',
  14176. 'UpperLeftArrow': '\u2196',
  14177. 'nwarr': '\u2196',
  14178. 'nwarrow': '\u2196',
  14179. 'UpperRightArrow': '\u2197',
  14180. 'nearr': '\u2197',
  14181. 'nearrow': '\u2197',
  14182. 'Upsi': '\u03D2',
  14183. 'upsih': '\u03D2',
  14184. 'Upsilon': '\u03A5',
  14185. 'Uring': '\u016E',
  14186. 'Uscr': '\uD835\uDCB0',
  14187. 'Utilde': '\u0168',
  14188. 'Uuml': '\u00DC',
  14189. 'VDash': '\u22AB',
  14190. 'Vbar': '\u2AEB',
  14191. 'Vcy': '\u0412',
  14192. 'Vdash': '\u22A9',
  14193. 'Vdashl': '\u2AE6',
  14194. 'Vee': '\u22C1',
  14195. 'bigvee': '\u22C1',
  14196. 'xvee': '\u22C1',
  14197. 'Verbar': '\u2016',
  14198. 'Vert': '\u2016',
  14199. 'VerticalBar': '\u2223',
  14200. 'mid': '\u2223',
  14201. 'shortmid': '\u2223',
  14202. 'smid': '\u2223',
  14203. 'VerticalLine': '\u007C',
  14204. 'verbar': '\u007C',
  14205. 'vert': '\u007C',
  14206. 'VerticalSeparator': '\u2758',
  14207. 'VerticalTilde': '\u2240',
  14208. 'wr': '\u2240',
  14209. 'wreath': '\u2240',
  14210. 'VeryThinSpace': '\u200A',
  14211. 'hairsp': '\u200A',
  14212. 'Vfr': '\uD835\uDD19',
  14213. 'Vopf': '\uD835\uDD4D',
  14214. 'Vscr': '\uD835\uDCB1',
  14215. 'Vvdash': '\u22AA',
  14216. 'Wcirc': '\u0174',
  14217. 'Wedge': '\u22C0',
  14218. 'bigwedge': '\u22C0',
  14219. 'xwedge': '\u22C0',
  14220. 'Wfr': '\uD835\uDD1A',
  14221. 'Wopf': '\uD835\uDD4E',
  14222. 'Wscr': '\uD835\uDCB2',
  14223. 'Xfr': '\uD835\uDD1B',
  14224. 'Xi': '\u039E',
  14225. 'Xopf': '\uD835\uDD4F',
  14226. 'Xscr': '\uD835\uDCB3',
  14227. 'YAcy': '\u042F',
  14228. 'YIcy': '\u0407',
  14229. 'YUcy': '\u042E',
  14230. 'Yacute': '\u00DD',
  14231. 'Ycirc': '\u0176',
  14232. 'Ycy': '\u042B',
  14233. 'Yfr': '\uD835\uDD1C',
  14234. 'Yopf': '\uD835\uDD50',
  14235. 'Yscr': '\uD835\uDCB4',
  14236. 'Yuml': '\u0178',
  14237. 'ZHcy': '\u0416',
  14238. 'Zacute': '\u0179',
  14239. 'Zcaron': '\u017D',
  14240. 'Zcy': '\u0417',
  14241. 'Zdot': '\u017B',
  14242. 'Zeta': '\u0396',
  14243. 'Zfr': '\u2128',
  14244. 'zeetrf': '\u2128',
  14245. 'Zopf': '\u2124',
  14246. 'integers': '\u2124',
  14247. 'Zscr': '\uD835\uDCB5',
  14248. 'aacute': '\u00E1',
  14249. 'abreve': '\u0103',
  14250. 'ac': '\u223E',
  14251. 'mstpos': '\u223E',
  14252. 'acE': '\u223E\u0333',
  14253. 'acd': '\u223F',
  14254. 'acirc': '\u00E2',
  14255. 'acy': '\u0430',
  14256. 'aelig': '\u00E6',
  14257. 'afr': '\uD835\uDD1E',
  14258. 'agrave': '\u00E0',
  14259. 'alefsym': '\u2135',
  14260. 'aleph': '\u2135',
  14261. 'alpha': '\u03B1',
  14262. 'amacr': '\u0101',
  14263. 'amalg': '\u2A3F',
  14264. 'and': '\u2227',
  14265. 'wedge': '\u2227',
  14266. 'andand': '\u2A55',
  14267. 'andd': '\u2A5C',
  14268. 'andslope': '\u2A58',
  14269. 'andv': '\u2A5A',
  14270. 'ang': '\u2220',
  14271. 'angle': '\u2220',
  14272. 'ange': '\u29A4',
  14273. 'angmsd': '\u2221',
  14274. 'measuredangle': '\u2221',
  14275. 'angmsdaa': '\u29A8',
  14276. 'angmsdab': '\u29A9',
  14277. 'angmsdac': '\u29AA',
  14278. 'angmsdad': '\u29AB',
  14279. 'angmsdae': '\u29AC',
  14280. 'angmsdaf': '\u29AD',
  14281. 'angmsdag': '\u29AE',
  14282. 'angmsdah': '\u29AF',
  14283. 'angrt': '\u221F',
  14284. 'angrtvb': '\u22BE',
  14285. 'angrtvbd': '\u299D',
  14286. 'angsph': '\u2222',
  14287. 'angzarr': '\u237C',
  14288. 'aogon': '\u0105',
  14289. 'aopf': '\uD835\uDD52',
  14290. 'apE': '\u2A70',
  14291. 'apacir': '\u2A6F',
  14292. 'ape': '\u224A',
  14293. 'approxeq': '\u224A',
  14294. 'apid': '\u224B',
  14295. 'apos': '\u0027',
  14296. 'aring': '\u00E5',
  14297. 'ascr': '\uD835\uDCB6',
  14298. 'ast': '\u002A',
  14299. 'midast': '\u002A',
  14300. 'atilde': '\u00E3',
  14301. 'auml': '\u00E4',
  14302. 'awint': '\u2A11',
  14303. 'bNot': '\u2AED',
  14304. 'backcong': '\u224C',
  14305. 'bcong': '\u224C',
  14306. 'backepsilon': '\u03F6',
  14307. 'bepsi': '\u03F6',
  14308. 'backprime': '\u2035',
  14309. 'bprime': '\u2035',
  14310. 'backsim': '\u223D',
  14311. 'bsim': '\u223D',
  14312. 'backsimeq': '\u22CD',
  14313. 'bsime': '\u22CD',
  14314. 'barvee': '\u22BD',
  14315. 'barwed': '\u2305',
  14316. 'barwedge': '\u2305',
  14317. 'bbrktbrk': '\u23B6',
  14318. 'bcy': '\u0431',
  14319. 'bdquo': '\u201E',
  14320. 'ldquor': '\u201E',
  14321. 'bemptyv': '\u29B0',
  14322. 'beta': '\u03B2',
  14323. 'beth': '\u2136',
  14324. 'between': '\u226C',
  14325. 'twixt': '\u226C',
  14326. 'bfr': '\uD835\uDD1F',
  14327. 'bigcirc': '\u25EF',
  14328. 'xcirc': '\u25EF',
  14329. 'bigodot': '\u2A00',
  14330. 'xodot': '\u2A00',
  14331. 'bigoplus': '\u2A01',
  14332. 'xoplus': '\u2A01',
  14333. 'bigotimes': '\u2A02',
  14334. 'xotime': '\u2A02',
  14335. 'bigsqcup': '\u2A06',
  14336. 'xsqcup': '\u2A06',
  14337. 'bigstar': '\u2605',
  14338. 'starf': '\u2605',
  14339. 'bigtriangledown': '\u25BD',
  14340. 'xdtri': '\u25BD',
  14341. 'bigtriangleup': '\u25B3',
  14342. 'xutri': '\u25B3',
  14343. 'biguplus': '\u2A04',
  14344. 'xuplus': '\u2A04',
  14345. 'bkarow': '\u290D',
  14346. 'rbarr': '\u290D',
  14347. 'blacklozenge': '\u29EB',
  14348. 'lozf': '\u29EB',
  14349. 'blacktriangle': '\u25B4',
  14350. 'utrif': '\u25B4',
  14351. 'blacktriangledown': '\u25BE',
  14352. 'dtrif': '\u25BE',
  14353. 'blacktriangleleft': '\u25C2',
  14354. 'ltrif': '\u25C2',
  14355. 'blacktriangleright': '\u25B8',
  14356. 'rtrif': '\u25B8',
  14357. 'blank': '\u2423',
  14358. 'blk12': '\u2592',
  14359. 'blk14': '\u2591',
  14360. 'blk34': '\u2593',
  14361. 'block': '\u2588',
  14362. 'bne': '\u003D\u20E5',
  14363. 'bnequiv': '\u2261\u20E5',
  14364. 'bnot': '\u2310',
  14365. 'bopf': '\uD835\uDD53',
  14366. 'bowtie': '\u22C8',
  14367. 'boxDL': '\u2557',
  14368. 'boxDR': '\u2554',
  14369. 'boxDl': '\u2556',
  14370. 'boxDr': '\u2553',
  14371. 'boxH': '\u2550',
  14372. 'boxHD': '\u2566',
  14373. 'boxHU': '\u2569',
  14374. 'boxHd': '\u2564',
  14375. 'boxHu': '\u2567',
  14376. 'boxUL': '\u255D',
  14377. 'boxUR': '\u255A',
  14378. 'boxUl': '\u255C',
  14379. 'boxUr': '\u2559',
  14380. 'boxV': '\u2551',
  14381. 'boxVH': '\u256C',
  14382. 'boxVL': '\u2563',
  14383. 'boxVR': '\u2560',
  14384. 'boxVh': '\u256B',
  14385. 'boxVl': '\u2562',
  14386. 'boxVr': '\u255F',
  14387. 'boxbox': '\u29C9',
  14388. 'boxdL': '\u2555',
  14389. 'boxdR': '\u2552',
  14390. 'boxdl': '\u2510',
  14391. 'boxdr': '\u250C',
  14392. 'boxhD': '\u2565',
  14393. 'boxhU': '\u2568',
  14394. 'boxhd': '\u252C',
  14395. 'boxhu': '\u2534',
  14396. 'boxminus': '\u229F',
  14397. 'minusb': '\u229F',
  14398. 'boxplus': '\u229E',
  14399. 'plusb': '\u229E',
  14400. 'boxtimes': '\u22A0',
  14401. 'timesb': '\u22A0',
  14402. 'boxuL': '\u255B',
  14403. 'boxuR': '\u2558',
  14404. 'boxul': '\u2518',
  14405. 'boxur': '\u2514',
  14406. 'boxv': '\u2502',
  14407. 'boxvH': '\u256A',
  14408. 'boxvL': '\u2561',
  14409. 'boxvR': '\u255E',
  14410. 'boxvh': '\u253C',
  14411. 'boxvl': '\u2524',
  14412. 'boxvr': '\u251C',
  14413. 'brvbar': '\u00A6',
  14414. 'bscr': '\uD835\uDCB7',
  14415. 'bsemi': '\u204F',
  14416. 'bsol': '\u005C',
  14417. 'bsolb': '\u29C5',
  14418. 'bsolhsub': '\u27C8',
  14419. 'bull': '\u2022',
  14420. 'bullet': '\u2022',
  14421. 'bumpE': '\u2AAE',
  14422. 'cacute': '\u0107',
  14423. 'cap': '\u2229',
  14424. 'capand': '\u2A44',
  14425. 'capbrcup': '\u2A49',
  14426. 'capcap': '\u2A4B',
  14427. 'capcup': '\u2A47',
  14428. 'capdot': '\u2A40',
  14429. 'caps': '\u2229\uFE00',
  14430. 'caret': '\u2041',
  14431. 'ccaps': '\u2A4D',
  14432. 'ccaron': '\u010D',
  14433. 'ccedil': '\u00E7',
  14434. 'ccirc': '\u0109',
  14435. 'ccups': '\u2A4C',
  14436. 'ccupssm': '\u2A50',
  14437. 'cdot': '\u010B',
  14438. 'cemptyv': '\u29B2',
  14439. 'cent': '\u00A2',
  14440. 'cfr': '\uD835\uDD20',
  14441. 'chcy': '\u0447',
  14442. 'check': '\u2713',
  14443. 'checkmark': '\u2713',
  14444. 'chi': '\u03C7',
  14445. 'cir': '\u25CB',
  14446. 'cirE': '\u29C3',
  14447. 'circ': '\u02C6',
  14448. 'circeq': '\u2257',
  14449. 'cire': '\u2257',
  14450. 'circlearrowleft': '\u21BA',
  14451. 'olarr': '\u21BA',
  14452. 'circlearrowright': '\u21BB',
  14453. 'orarr': '\u21BB',
  14454. 'circledS': '\u24C8',
  14455. 'oS': '\u24C8',
  14456. 'circledast': '\u229B',
  14457. 'oast': '\u229B',
  14458. 'circledcirc': '\u229A',
  14459. 'ocir': '\u229A',
  14460. 'circleddash': '\u229D',
  14461. 'odash': '\u229D',
  14462. 'cirfnint': '\u2A10',
  14463. 'cirmid': '\u2AEF',
  14464. 'cirscir': '\u29C2',
  14465. 'clubs': '\u2663',
  14466. 'clubsuit': '\u2663',
  14467. 'colon': '\u003A',
  14468. 'comma': '\u002C',
  14469. 'commat': '\u0040',
  14470. 'comp': '\u2201',
  14471. 'complement': '\u2201',
  14472. 'congdot': '\u2A6D',
  14473. 'copf': '\uD835\uDD54',
  14474. 'copysr': '\u2117',
  14475. 'crarr': '\u21B5',
  14476. 'cross': '\u2717',
  14477. 'cscr': '\uD835\uDCB8',
  14478. 'csub': '\u2ACF',
  14479. 'csube': '\u2AD1',
  14480. 'csup': '\u2AD0',
  14481. 'csupe': '\u2AD2',
  14482. 'ctdot': '\u22EF',
  14483. 'cudarrl': '\u2938',
  14484. 'cudarrr': '\u2935',
  14485. 'cuepr': '\u22DE',
  14486. 'curlyeqprec': '\u22DE',
  14487. 'cuesc': '\u22DF',
  14488. 'curlyeqsucc': '\u22DF',
  14489. 'cularr': '\u21B6',
  14490. 'curvearrowleft': '\u21B6',
  14491. 'cularrp': '\u293D',
  14492. 'cup': '\u222A',
  14493. 'cupbrcap': '\u2A48',
  14494. 'cupcap': '\u2A46',
  14495. 'cupcup': '\u2A4A',
  14496. 'cupdot': '\u228D',
  14497. 'cupor': '\u2A45',
  14498. 'cups': '\u222A\uFE00',
  14499. 'curarr': '\u21B7',
  14500. 'curvearrowright': '\u21B7',
  14501. 'curarrm': '\u293C',
  14502. 'curlyvee': '\u22CE',
  14503. 'cuvee': '\u22CE',
  14504. 'curlywedge': '\u22CF',
  14505. 'cuwed': '\u22CF',
  14506. 'curren': '\u00A4',
  14507. 'cwint': '\u2231',
  14508. 'cylcty': '\u232D',
  14509. 'dHar': '\u2965',
  14510. 'dagger': '\u2020',
  14511. 'daleth': '\u2138',
  14512. 'dash': '\u2010',
  14513. 'hyphen': '\u2010',
  14514. 'dbkarow': '\u290F',
  14515. 'rBarr': '\u290F',
  14516. 'dcaron': '\u010F',
  14517. 'dcy': '\u0434',
  14518. 'ddarr': '\u21CA',
  14519. 'downdownarrows': '\u21CA',
  14520. 'ddotseq': '\u2A77',
  14521. 'eDDot': '\u2A77',
  14522. 'deg': '\u00B0',
  14523. 'delta': '\u03B4',
  14524. 'demptyv': '\u29B1',
  14525. 'dfisht': '\u297F',
  14526. 'dfr': '\uD835\uDD21',
  14527. 'diamondsuit': '\u2666',
  14528. 'diams': '\u2666',
  14529. 'digamma': '\u03DD',
  14530. 'gammad': '\u03DD',
  14531. 'disin': '\u22F2',
  14532. 'div': '\u00F7',
  14533. 'divide': '\u00F7',
  14534. 'divideontimes': '\u22C7',
  14535. 'divonx': '\u22C7',
  14536. 'djcy': '\u0452',
  14537. 'dlcorn': '\u231E',
  14538. 'llcorner': '\u231E',
  14539. 'dlcrop': '\u230D',
  14540. 'dollar': '\u0024',
  14541. 'dopf': '\uD835\uDD55',
  14542. 'doteqdot': '\u2251',
  14543. 'eDot': '\u2251',
  14544. 'dotminus': '\u2238',
  14545. 'minusd': '\u2238',
  14546. 'dotplus': '\u2214',
  14547. 'plusdo': '\u2214',
  14548. 'dotsquare': '\u22A1',
  14549. 'sdotb': '\u22A1',
  14550. 'drcorn': '\u231F',
  14551. 'lrcorner': '\u231F',
  14552. 'drcrop': '\u230C',
  14553. 'dscr': '\uD835\uDCB9',
  14554. 'dscy': '\u0455',
  14555. 'dsol': '\u29F6',
  14556. 'dstrok': '\u0111',
  14557. 'dtdot': '\u22F1',
  14558. 'dtri': '\u25BF',
  14559. 'triangledown': '\u25BF',
  14560. 'dwangle': '\u29A6',
  14561. 'dzcy': '\u045F',
  14562. 'dzigrarr': '\u27FF',
  14563. 'eacute': '\u00E9',
  14564. 'easter': '\u2A6E',
  14565. 'ecaron': '\u011B',
  14566. 'ecir': '\u2256',
  14567. 'eqcirc': '\u2256',
  14568. 'ecirc': '\u00EA',
  14569. 'ecolon': '\u2255',
  14570. 'eqcolon': '\u2255',
  14571. 'ecy': '\u044D',
  14572. 'edot': '\u0117',
  14573. 'efDot': '\u2252',
  14574. 'fallingdotseq': '\u2252',
  14575. 'efr': '\uD835\uDD22',
  14576. 'eg': '\u2A9A',
  14577. 'egrave': '\u00E8',
  14578. 'egs': '\u2A96',
  14579. 'eqslantgtr': '\u2A96',
  14580. 'egsdot': '\u2A98',
  14581. 'el': '\u2A99',
  14582. 'elinters': '\u23E7',
  14583. 'ell': '\u2113',
  14584. 'els': '\u2A95',
  14585. 'eqslantless': '\u2A95',
  14586. 'elsdot': '\u2A97',
  14587. 'emacr': '\u0113',
  14588. 'empty': '\u2205',
  14589. 'emptyset': '\u2205',
  14590. 'emptyv': '\u2205',
  14591. 'varnothing': '\u2205',
  14592. 'emsp13': '\u2004',
  14593. 'emsp14': '\u2005',
  14594. 'emsp': '\u2003',
  14595. 'eng': '\u014B',
  14596. 'ensp': '\u2002',
  14597. 'eogon': '\u0119',
  14598. 'eopf': '\uD835\uDD56',
  14599. 'epar': '\u22D5',
  14600. 'eparsl': '\u29E3',
  14601. 'eplus': '\u2A71',
  14602. 'epsi': '\u03B5',
  14603. 'epsilon': '\u03B5',
  14604. 'epsiv': '\u03F5',
  14605. 'straightepsilon': '\u03F5',
  14606. 'varepsilon': '\u03F5',
  14607. 'equals': '\u003D',
  14608. 'equest': '\u225F',
  14609. 'questeq': '\u225F',
  14610. 'equivDD': '\u2A78',
  14611. 'eqvparsl': '\u29E5',
  14612. 'erDot': '\u2253',
  14613. 'risingdotseq': '\u2253',
  14614. 'erarr': '\u2971',
  14615. 'escr': '\u212F',
  14616. 'eta': '\u03B7',
  14617. 'eth': '\u00F0',
  14618. 'euml': '\u00EB',
  14619. 'euro': '\u20AC',
  14620. 'excl': '\u0021',
  14621. 'fcy': '\u0444',
  14622. 'female': '\u2640',
  14623. 'ffilig': '\uFB03',
  14624. 'fflig': '\uFB00',
  14625. 'ffllig': '\uFB04',
  14626. 'ffr': '\uD835\uDD23',
  14627. 'filig': '\uFB01',
  14628. 'fjlig': '\u0066\u006A',
  14629. 'flat': '\u266D',
  14630. 'fllig': '\uFB02',
  14631. 'fltns': '\u25B1',
  14632. 'fnof': '\u0192',
  14633. 'fopf': '\uD835\uDD57',
  14634. 'fork': '\u22D4',
  14635. 'pitchfork': '\u22D4',
  14636. 'forkv': '\u2AD9',
  14637. 'fpartint': '\u2A0D',
  14638. 'frac12': '\u00BD',
  14639. 'half': '\u00BD',
  14640. 'frac13': '\u2153',
  14641. 'frac14': '\u00BC',
  14642. 'frac15': '\u2155',
  14643. 'frac16': '\u2159',
  14644. 'frac18': '\u215B',
  14645. 'frac23': '\u2154',
  14646. 'frac25': '\u2156',
  14647. 'frac34': '\u00BE',
  14648. 'frac35': '\u2157',
  14649. 'frac38': '\u215C',
  14650. 'frac45': '\u2158',
  14651. 'frac56': '\u215A',
  14652. 'frac58': '\u215D',
  14653. 'frac78': '\u215E',
  14654. 'frasl': '\u2044',
  14655. 'frown': '\u2322',
  14656. 'sfrown': '\u2322',
  14657. 'fscr': '\uD835\uDCBB',
  14658. 'gEl': '\u2A8C',
  14659. 'gtreqqless': '\u2A8C',
  14660. 'gacute': '\u01F5',
  14661. 'gamma': '\u03B3',
  14662. 'gap': '\u2A86',
  14663. 'gtrapprox': '\u2A86',
  14664. 'gbreve': '\u011F',
  14665. 'gcirc': '\u011D',
  14666. 'gcy': '\u0433',
  14667. 'gdot': '\u0121',
  14668. 'gescc': '\u2AA9',
  14669. 'gesdot': '\u2A80',
  14670. 'gesdoto': '\u2A82',
  14671. 'gesdotol': '\u2A84',
  14672. 'gesl': '\u22DB\uFE00',
  14673. 'gesles': '\u2A94',
  14674. 'gfr': '\uD835\uDD24',
  14675. 'gimel': '\u2137',
  14676. 'gjcy': '\u0453',
  14677. 'glE': '\u2A92',
  14678. 'gla': '\u2AA5',
  14679. 'glj': '\u2AA4',
  14680. 'gnE': '\u2269',
  14681. 'gneqq': '\u2269',
  14682. 'gnap': '\u2A8A',
  14683. 'gnapprox': '\u2A8A',
  14684. 'gne': '\u2A88',
  14685. 'gneq': '\u2A88',
  14686. 'gnsim': '\u22E7',
  14687. 'gopf': '\uD835\uDD58',
  14688. 'gscr': '\u210A',
  14689. 'gsime': '\u2A8E',
  14690. 'gsiml': '\u2A90',
  14691. 'gtcc': '\u2AA7',
  14692. 'gtcir': '\u2A7A',
  14693. 'gtdot': '\u22D7',
  14694. 'gtrdot': '\u22D7',
  14695. 'gtlPar': '\u2995',
  14696. 'gtquest': '\u2A7C',
  14697. 'gtrarr': '\u2978',
  14698. 'gvertneqq': '\u2269\uFE00',
  14699. 'gvnE': '\u2269\uFE00',
  14700. 'hardcy': '\u044A',
  14701. 'harrcir': '\u2948',
  14702. 'harrw': '\u21AD',
  14703. 'leftrightsquigarrow': '\u21AD',
  14704. 'hbar': '\u210F',
  14705. 'hslash': '\u210F',
  14706. 'planck': '\u210F',
  14707. 'plankv': '\u210F',
  14708. 'hcirc': '\u0125',
  14709. 'hearts': '\u2665',
  14710. 'heartsuit': '\u2665',
  14711. 'hellip': '\u2026',
  14712. 'mldr': '\u2026',
  14713. 'hercon': '\u22B9',
  14714. 'hfr': '\uD835\uDD25',
  14715. 'hksearow': '\u2925',
  14716. 'searhk': '\u2925',
  14717. 'hkswarow': '\u2926',
  14718. 'swarhk': '\u2926',
  14719. 'hoarr': '\u21FF',
  14720. 'homtht': '\u223B',
  14721. 'hookleftarrow': '\u21A9',
  14722. 'larrhk': '\u21A9',
  14723. 'hookrightarrow': '\u21AA',
  14724. 'rarrhk': '\u21AA',
  14725. 'hopf': '\uD835\uDD59',
  14726. 'horbar': '\u2015',
  14727. 'hscr': '\uD835\uDCBD',
  14728. 'hstrok': '\u0127',
  14729. 'hybull': '\u2043',
  14730. 'iacute': '\u00ED',
  14731. 'icirc': '\u00EE',
  14732. 'icy': '\u0438',
  14733. 'iecy': '\u0435',
  14734. 'iexcl': '\u00A1',
  14735. 'ifr': '\uD835\uDD26',
  14736. 'igrave': '\u00EC',
  14737. 'iiiint': '\u2A0C',
  14738. 'qint': '\u2A0C',
  14739. 'iiint': '\u222D',
  14740. 'tint': '\u222D',
  14741. 'iinfin': '\u29DC',
  14742. 'iiota': '\u2129',
  14743. 'ijlig': '\u0133',
  14744. 'imacr': '\u012B',
  14745. 'imath': '\u0131',
  14746. 'inodot': '\u0131',
  14747. 'imof': '\u22B7',
  14748. 'imped': '\u01B5',
  14749. 'incare': '\u2105',
  14750. 'infin': '\u221E',
  14751. 'infintie': '\u29DD',
  14752. 'intcal': '\u22BA',
  14753. 'intercal': '\u22BA',
  14754. 'intlarhk': '\u2A17',
  14755. 'intprod': '\u2A3C',
  14756. 'iprod': '\u2A3C',
  14757. 'iocy': '\u0451',
  14758. 'iogon': '\u012F',
  14759. 'iopf': '\uD835\uDD5A',
  14760. 'iota': '\u03B9',
  14761. 'iquest': '\u00BF',
  14762. 'iscr': '\uD835\uDCBE',
  14763. 'isinE': '\u22F9',
  14764. 'isindot': '\u22F5',
  14765. 'isins': '\u22F4',
  14766. 'isinsv': '\u22F3',
  14767. 'itilde': '\u0129',
  14768. 'iukcy': '\u0456',
  14769. 'iuml': '\u00EF',
  14770. 'jcirc': '\u0135',
  14771. 'jcy': '\u0439',
  14772. 'jfr': '\uD835\uDD27',
  14773. 'jmath': '\u0237',
  14774. 'jopf': '\uD835\uDD5B',
  14775. 'jscr': '\uD835\uDCBF',
  14776. 'jsercy': '\u0458',
  14777. 'jukcy': '\u0454',
  14778. 'kappa': '\u03BA',
  14779. 'kappav': '\u03F0',
  14780. 'varkappa': '\u03F0',
  14781. 'kcedil': '\u0137',
  14782. 'kcy': '\u043A',
  14783. 'kfr': '\uD835\uDD28',
  14784. 'kgreen': '\u0138',
  14785. 'khcy': '\u0445',
  14786. 'kjcy': '\u045C',
  14787. 'kopf': '\uD835\uDD5C',
  14788. 'kscr': '\uD835\uDCC0',
  14789. 'lAtail': '\u291B',
  14790. 'lBarr': '\u290E',
  14791. 'lEg': '\u2A8B',
  14792. 'lesseqqgtr': '\u2A8B',
  14793. 'lHar': '\u2962',
  14794. 'lacute': '\u013A',
  14795. 'laemptyv': '\u29B4',
  14796. 'lambda': '\u03BB',
  14797. 'langd': '\u2991',
  14798. 'lap': '\u2A85',
  14799. 'lessapprox': '\u2A85',
  14800. 'laquo': '\u00AB',
  14801. 'larrbfs': '\u291F',
  14802. 'larrfs': '\u291D',
  14803. 'larrlp': '\u21AB',
  14804. 'looparrowleft': '\u21AB',
  14805. 'larrpl': '\u2939',
  14806. 'larrsim': '\u2973',
  14807. 'larrtl': '\u21A2',
  14808. 'leftarrowtail': '\u21A2',
  14809. 'lat': '\u2AAB',
  14810. 'latail': '\u2919',
  14811. 'late': '\u2AAD',
  14812. 'lates': '\u2AAD\uFE00',
  14813. 'lbarr': '\u290C',
  14814. 'lbbrk': '\u2772',
  14815. 'lbrace': '\u007B',
  14816. 'lcub': '\u007B',
  14817. 'lbrack': '\u005B',
  14818. 'lsqb': '\u005B',
  14819. 'lbrke': '\u298B',
  14820. 'lbrksld': '\u298F',
  14821. 'lbrkslu': '\u298D',
  14822. 'lcaron': '\u013E',
  14823. 'lcedil': '\u013C',
  14824. 'lcy': '\u043B',
  14825. 'ldca': '\u2936',
  14826. 'ldrdhar': '\u2967',
  14827. 'ldrushar': '\u294B',
  14828. 'ldsh': '\u21B2',
  14829. 'le': '\u2264',
  14830. 'leq': '\u2264',
  14831. 'leftleftarrows': '\u21C7',
  14832. 'llarr': '\u21C7',
  14833. 'leftthreetimes': '\u22CB',
  14834. 'lthree': '\u22CB',
  14835. 'lescc': '\u2AA8',
  14836. 'lesdot': '\u2A7F',
  14837. 'lesdoto': '\u2A81',
  14838. 'lesdotor': '\u2A83',
  14839. 'lesg': '\u22DA\uFE00',
  14840. 'lesges': '\u2A93',
  14841. 'lessdot': '\u22D6',
  14842. 'ltdot': '\u22D6',
  14843. 'lfisht': '\u297C',
  14844. 'lfr': '\uD835\uDD29',
  14845. 'lgE': '\u2A91',
  14846. 'lharul': '\u296A',
  14847. 'lhblk': '\u2584',
  14848. 'ljcy': '\u0459',
  14849. 'llhard': '\u296B',
  14850. 'lltri': '\u25FA',
  14851. 'lmidot': '\u0140',
  14852. 'lmoust': '\u23B0',
  14853. 'lmoustache': '\u23B0',
  14854. 'lnE': '\u2268',
  14855. 'lneqq': '\u2268',
  14856. 'lnap': '\u2A89',
  14857. 'lnapprox': '\u2A89',
  14858. 'lne': '\u2A87',
  14859. 'lneq': '\u2A87',
  14860. 'lnsim': '\u22E6',
  14861. 'loang': '\u27EC',
  14862. 'loarr': '\u21FD',
  14863. 'longmapsto': '\u27FC',
  14864. 'xmap': '\u27FC',
  14865. 'looparrowright': '\u21AC',
  14866. 'rarrlp': '\u21AC',
  14867. 'lopar': '\u2985',
  14868. 'lopf': '\uD835\uDD5D',
  14869. 'loplus': '\u2A2D',
  14870. 'lotimes': '\u2A34',
  14871. 'lowast': '\u2217',
  14872. 'loz': '\u25CA',
  14873. 'lozenge': '\u25CA',
  14874. 'lpar': '\u0028',
  14875. 'lparlt': '\u2993',
  14876. 'lrhard': '\u296D',
  14877. 'lrm': '\u200E',
  14878. 'lrtri': '\u22BF',
  14879. 'lsaquo': '\u2039',
  14880. 'lscr': '\uD835\uDCC1',
  14881. 'lsime': '\u2A8D',
  14882. 'lsimg': '\u2A8F',
  14883. 'lsquor': '\u201A',
  14884. 'sbquo': '\u201A',
  14885. 'lstrok': '\u0142',
  14886. 'ltcc': '\u2AA6',
  14887. 'ltcir': '\u2A79',
  14888. 'ltimes': '\u22C9',
  14889. 'ltlarr': '\u2976',
  14890. 'ltquest': '\u2A7B',
  14891. 'ltrPar': '\u2996',
  14892. 'ltri': '\u25C3',
  14893. 'triangleleft': '\u25C3',
  14894. 'lurdshar': '\u294A',
  14895. 'luruhar': '\u2966',
  14896. 'lvertneqq': '\u2268\uFE00',
  14897. 'lvnE': '\u2268\uFE00',
  14898. 'mDDot': '\u223A',
  14899. 'macr': '\u00AF',
  14900. 'strns': '\u00AF',
  14901. 'male': '\u2642',
  14902. 'malt': '\u2720',
  14903. 'maltese': '\u2720',
  14904. 'marker': '\u25AE',
  14905. 'mcomma': '\u2A29',
  14906. 'mcy': '\u043C',
  14907. 'mdash': '\u2014',
  14908. 'mfr': '\uD835\uDD2A',
  14909. 'mho': '\u2127',
  14910. 'micro': '\u00B5',
  14911. 'midcir': '\u2AF0',
  14912. 'minus': '\u2212',
  14913. 'minusdu': '\u2A2A',
  14914. 'mlcp': '\u2ADB',
  14915. 'models': '\u22A7',
  14916. 'mopf': '\uD835\uDD5E',
  14917. 'mscr': '\uD835\uDCC2',
  14918. 'mu': '\u03BC',
  14919. 'multimap': '\u22B8',
  14920. 'mumap': '\u22B8',
  14921. 'nGg': '\u22D9\u0338',
  14922. 'nGt': '\u226B\u20D2',
  14923. 'nLeftarrow': '\u21CD',
  14924. 'nlArr': '\u21CD',
  14925. 'nLeftrightarrow': '\u21CE',
  14926. 'nhArr': '\u21CE',
  14927. 'nLl': '\u22D8\u0338',
  14928. 'nLt': '\u226A\u20D2',
  14929. 'nRightarrow': '\u21CF',
  14930. 'nrArr': '\u21CF',
  14931. 'nVDash': '\u22AF',
  14932. 'nVdash': '\u22AE',
  14933. 'nacute': '\u0144',
  14934. 'nang': '\u2220\u20D2',
  14935. 'napE': '\u2A70\u0338',
  14936. 'napid': '\u224B\u0338',
  14937. 'napos': '\u0149',
  14938. 'natur': '\u266E',
  14939. 'natural': '\u266E',
  14940. 'ncap': '\u2A43',
  14941. 'ncaron': '\u0148',
  14942. 'ncedil': '\u0146',
  14943. 'ncongdot': '\u2A6D\u0338',
  14944. 'ncup': '\u2A42',
  14945. 'ncy': '\u043D',
  14946. 'ndash': '\u2013',
  14947. 'neArr': '\u21D7',
  14948. 'nearhk': '\u2924',
  14949. 'nedot': '\u2250\u0338',
  14950. 'nesear': '\u2928',
  14951. 'toea': '\u2928',
  14952. 'nfr': '\uD835\uDD2B',
  14953. 'nharr': '\u21AE',
  14954. 'nleftrightarrow': '\u21AE',
  14955. 'nhpar': '\u2AF2',
  14956. 'nis': '\u22FC',
  14957. 'nisd': '\u22FA',
  14958. 'njcy': '\u045A',
  14959. 'nlE': '\u2266\u0338',
  14960. 'nleqq': '\u2266\u0338',
  14961. 'nlarr': '\u219A',
  14962. 'nleftarrow': '\u219A',
  14963. 'nldr': '\u2025',
  14964. 'nopf': '\uD835\uDD5F',
  14965. 'not': '\u00AC',
  14966. 'notinE': '\u22F9\u0338',
  14967. 'notindot': '\u22F5\u0338',
  14968. 'notinvb': '\u22F7',
  14969. 'notinvc': '\u22F6',
  14970. 'notnivb': '\u22FE',
  14971. 'notnivc': '\u22FD',
  14972. 'nparsl': '\u2AFD\u20E5',
  14973. 'npart': '\u2202\u0338',
  14974. 'npolint': '\u2A14',
  14975. 'nrarr': '\u219B',
  14976. 'nrightarrow': '\u219B',
  14977. 'nrarrc': '\u2933\u0338',
  14978. 'nrarrw': '\u219D\u0338',
  14979. 'nscr': '\uD835\uDCC3',
  14980. 'nsub': '\u2284',
  14981. 'nsubE': '\u2AC5\u0338',
  14982. 'nsubseteqq': '\u2AC5\u0338',
  14983. 'nsup': '\u2285',
  14984. 'nsupE': '\u2AC6\u0338',
  14985. 'nsupseteqq': '\u2AC6\u0338',
  14986. 'ntilde': '\u00F1',
  14987. 'nu': '\u03BD',
  14988. 'num': '\u0023',
  14989. 'numero': '\u2116',
  14990. 'numsp': '\u2007',
  14991. 'nvDash': '\u22AD',
  14992. 'nvHarr': '\u2904',
  14993. 'nvap': '\u224D\u20D2',
  14994. 'nvdash': '\u22AC',
  14995. 'nvge': '\u2265\u20D2',
  14996. 'nvgt': '\u003E\u20D2',
  14997. 'nvinfin': '\u29DE',
  14998. 'nvlArr': '\u2902',
  14999. 'nvle': '\u2264\u20D2',
  15000. 'nvlt': '\u003C\u20D2',
  15001. 'nvltrie': '\u22B4\u20D2',
  15002. 'nvrArr': '\u2903',
  15003. 'nvrtrie': '\u22B5\u20D2',
  15004. 'nvsim': '\u223C\u20D2',
  15005. 'nwArr': '\u21D6',
  15006. 'nwarhk': '\u2923',
  15007. 'nwnear': '\u2927',
  15008. 'oacute': '\u00F3',
  15009. 'ocirc': '\u00F4',
  15010. 'ocy': '\u043E',
  15011. 'odblac': '\u0151',
  15012. 'odiv': '\u2A38',
  15013. 'odsold': '\u29BC',
  15014. 'oelig': '\u0153',
  15015. 'ofcir': '\u29BF',
  15016. 'ofr': '\uD835\uDD2C',
  15017. 'ogon': '\u02DB',
  15018. 'ograve': '\u00F2',
  15019. 'ogt': '\u29C1',
  15020. 'ohbar': '\u29B5',
  15021. 'olcir': '\u29BE',
  15022. 'olcross': '\u29BB',
  15023. 'olt': '\u29C0',
  15024. 'omacr': '\u014D',
  15025. 'omega': '\u03C9',
  15026. 'omicron': '\u03BF',
  15027. 'omid': '\u29B6',
  15028. 'oopf': '\uD835\uDD60',
  15029. 'opar': '\u29B7',
  15030. 'operp': '\u29B9',
  15031. 'or': '\u2228',
  15032. 'vee': '\u2228',
  15033. 'ord': '\u2A5D',
  15034. 'order': '\u2134',
  15035. 'orderof': '\u2134',
  15036. 'oscr': '\u2134',
  15037. 'ordf': '\u00AA',
  15038. 'ordm': '\u00BA',
  15039. 'origof': '\u22B6',
  15040. 'oror': '\u2A56',
  15041. 'orslope': '\u2A57',
  15042. 'orv': '\u2A5B',
  15043. 'oslash': '\u00F8',
  15044. 'osol': '\u2298',
  15045. 'otilde': '\u00F5',
  15046. 'otimesas': '\u2A36',
  15047. 'ouml': '\u00F6',
  15048. 'ovbar': '\u233D',
  15049. 'para': '\u00B6',
  15050. 'parsim': '\u2AF3',
  15051. 'parsl': '\u2AFD',
  15052. 'pcy': '\u043F',
  15053. 'percnt': '\u0025',
  15054. 'period': '\u002E',
  15055. 'permil': '\u2030',
  15056. 'pertenk': '\u2031',
  15057. 'pfr': '\uD835\uDD2D',
  15058. 'phi': '\u03C6',
  15059. 'phiv': '\u03D5',
  15060. 'straightphi': '\u03D5',
  15061. 'varphi': '\u03D5',
  15062. 'phone': '\u260E',
  15063. 'pi': '\u03C0',
  15064. 'piv': '\u03D6',
  15065. 'varpi': '\u03D6',
  15066. 'planckh': '\u210E',
  15067. 'plus': '\u002B',
  15068. 'plusacir': '\u2A23',
  15069. 'pluscir': '\u2A22',
  15070. 'plusdu': '\u2A25',
  15071. 'pluse': '\u2A72',
  15072. 'plussim': '\u2A26',
  15073. 'plustwo': '\u2A27',
  15074. 'pointint': '\u2A15',
  15075. 'popf': '\uD835\uDD61',
  15076. 'pound': '\u00A3',
  15077. 'prE': '\u2AB3',
  15078. 'prap': '\u2AB7',
  15079. 'precapprox': '\u2AB7',
  15080. 'precnapprox': '\u2AB9',
  15081. 'prnap': '\u2AB9',
  15082. 'precneqq': '\u2AB5',
  15083. 'prnE': '\u2AB5',
  15084. 'precnsim': '\u22E8',
  15085. 'prnsim': '\u22E8',
  15086. 'prime': '\u2032',
  15087. 'profalar': '\u232E',
  15088. 'profline': '\u2312',
  15089. 'profsurf': '\u2313',
  15090. 'prurel': '\u22B0',
  15091. 'pscr': '\uD835\uDCC5',
  15092. 'psi': '\u03C8',
  15093. 'puncsp': '\u2008',
  15094. 'qfr': '\uD835\uDD2E',
  15095. 'qopf': '\uD835\uDD62',
  15096. 'qprime': '\u2057',
  15097. 'qscr': '\uD835\uDCC6',
  15098. 'quatint': '\u2A16',
  15099. 'quest': '\u003F',
  15100. 'rAtail': '\u291C',
  15101. 'rHar': '\u2964',
  15102. 'race': '\u223D\u0331',
  15103. 'racute': '\u0155',
  15104. 'raemptyv': '\u29B3',
  15105. 'rangd': '\u2992',
  15106. 'range': '\u29A5',
  15107. 'raquo': '\u00BB',
  15108. 'rarrap': '\u2975',
  15109. 'rarrbfs': '\u2920',
  15110. 'rarrc': '\u2933',
  15111. 'rarrfs': '\u291E',
  15112. 'rarrpl': '\u2945',
  15113. 'rarrsim': '\u2974',
  15114. 'rarrtl': '\u21A3',
  15115. 'rightarrowtail': '\u21A3',
  15116. 'rarrw': '\u219D',
  15117. 'rightsquigarrow': '\u219D',
  15118. 'ratail': '\u291A',
  15119. 'ratio': '\u2236',
  15120. 'rbbrk': '\u2773',
  15121. 'rbrace': '\u007D',
  15122. 'rcub': '\u007D',
  15123. 'rbrack': '\u005D',
  15124. 'rsqb': '\u005D',
  15125. 'rbrke': '\u298C',
  15126. 'rbrksld': '\u298E',
  15127. 'rbrkslu': '\u2990',
  15128. 'rcaron': '\u0159',
  15129. 'rcedil': '\u0157',
  15130. 'rcy': '\u0440',
  15131. 'rdca': '\u2937',
  15132. 'rdldhar': '\u2969',
  15133. 'rdsh': '\u21B3',
  15134. 'rect': '\u25AD',
  15135. 'rfisht': '\u297D',
  15136. 'rfr': '\uD835\uDD2F',
  15137. 'rharul': '\u296C',
  15138. 'rho': '\u03C1',
  15139. 'rhov': '\u03F1',
  15140. 'varrho': '\u03F1',
  15141. 'rightrightarrows': '\u21C9',
  15142. 'rrarr': '\u21C9',
  15143. 'rightthreetimes': '\u22CC',
  15144. 'rthree': '\u22CC',
  15145. 'ring': '\u02DA',
  15146. 'rlm': '\u200F',
  15147. 'rmoust': '\u23B1',
  15148. 'rmoustache': '\u23B1',
  15149. 'rnmid': '\u2AEE',
  15150. 'roang': '\u27ED',
  15151. 'roarr': '\u21FE',
  15152. 'ropar': '\u2986',
  15153. 'ropf': '\uD835\uDD63',
  15154. 'roplus': '\u2A2E',
  15155. 'rotimes': '\u2A35',
  15156. 'rpar': '\u0029',
  15157. 'rpargt': '\u2994',
  15158. 'rppolint': '\u2A12',
  15159. 'rsaquo': '\u203A',
  15160. 'rscr': '\uD835\uDCC7',
  15161. 'rtimes': '\u22CA',
  15162. 'rtri': '\u25B9',
  15163. 'triangleright': '\u25B9',
  15164. 'rtriltri': '\u29CE',
  15165. 'ruluhar': '\u2968',
  15166. 'rx': '\u211E',
  15167. 'sacute': '\u015B',
  15168. 'scE': '\u2AB4',
  15169. 'scap': '\u2AB8',
  15170. 'succapprox': '\u2AB8',
  15171. 'scaron': '\u0161',
  15172. 'scedil': '\u015F',
  15173. 'scirc': '\u015D',
  15174. 'scnE': '\u2AB6',
  15175. 'succneqq': '\u2AB6',
  15176. 'scnap': '\u2ABA',
  15177. 'succnapprox': '\u2ABA',
  15178. 'scnsim': '\u22E9',
  15179. 'succnsim': '\u22E9',
  15180. 'scpolint': '\u2A13',
  15181. 'scy': '\u0441',
  15182. 'sdot': '\u22C5',
  15183. 'sdote': '\u2A66',
  15184. 'seArr': '\u21D8',
  15185. 'sect': '\u00A7',
  15186. 'semi': '\u003B',
  15187. 'seswar': '\u2929',
  15188. 'tosa': '\u2929',
  15189. 'sext': '\u2736',
  15190. 'sfr': '\uD835\uDD30',
  15191. 'sharp': '\u266F',
  15192. 'shchcy': '\u0449',
  15193. 'shcy': '\u0448',
  15194. 'shy': '\u00AD',
  15195. 'sigma': '\u03C3',
  15196. 'sigmaf': '\u03C2',
  15197. 'sigmav': '\u03C2',
  15198. 'varsigma': '\u03C2',
  15199. 'simdot': '\u2A6A',
  15200. 'simg': '\u2A9E',
  15201. 'simgE': '\u2AA0',
  15202. 'siml': '\u2A9D',
  15203. 'simlE': '\u2A9F',
  15204. 'simne': '\u2246',
  15205. 'simplus': '\u2A24',
  15206. 'simrarr': '\u2972',
  15207. 'smashp': '\u2A33',
  15208. 'smeparsl': '\u29E4',
  15209. 'smile': '\u2323',
  15210. 'ssmile': '\u2323',
  15211. 'smt': '\u2AAA',
  15212. 'smte': '\u2AAC',
  15213. 'smtes': '\u2AAC\uFE00',
  15214. 'softcy': '\u044C',
  15215. 'sol': '\u002F',
  15216. 'solb': '\u29C4',
  15217. 'solbar': '\u233F',
  15218. 'sopf': '\uD835\uDD64',
  15219. 'spades': '\u2660',
  15220. 'spadesuit': '\u2660',
  15221. 'sqcaps': '\u2293\uFE00',
  15222. 'sqcups': '\u2294\uFE00',
  15223. 'sscr': '\uD835\uDCC8',
  15224. 'star': '\u2606',
  15225. 'sub': '\u2282',
  15226. 'subset': '\u2282',
  15227. 'subE': '\u2AC5',
  15228. 'subseteqq': '\u2AC5',
  15229. 'subdot': '\u2ABD',
  15230. 'subedot': '\u2AC3',
  15231. 'submult': '\u2AC1',
  15232. 'subnE': '\u2ACB',
  15233. 'subsetneqq': '\u2ACB',
  15234. 'subne': '\u228A',
  15235. 'subsetneq': '\u228A',
  15236. 'subplus': '\u2ABF',
  15237. 'subrarr': '\u2979',
  15238. 'subsim': '\u2AC7',
  15239. 'subsub': '\u2AD5',
  15240. 'subsup': '\u2AD3',
  15241. 'sung': '\u266A',
  15242. 'sup1': '\u00B9',
  15243. 'sup2': '\u00B2',
  15244. 'sup3': '\u00B3',
  15245. 'supE': '\u2AC6',
  15246. 'supseteqq': '\u2AC6',
  15247. 'supdot': '\u2ABE',
  15248. 'supdsub': '\u2AD8',
  15249. 'supedot': '\u2AC4',
  15250. 'suphsol': '\u27C9',
  15251. 'suphsub': '\u2AD7',
  15252. 'suplarr': '\u297B',
  15253. 'supmult': '\u2AC2',
  15254. 'supnE': '\u2ACC',
  15255. 'supsetneqq': '\u2ACC',
  15256. 'supne': '\u228B',
  15257. 'supsetneq': '\u228B',
  15258. 'supplus': '\u2AC0',
  15259. 'supsim': '\u2AC8',
  15260. 'supsub': '\u2AD4',
  15261. 'supsup': '\u2AD6',
  15262. 'swArr': '\u21D9',
  15263. 'swnwar': '\u292A',
  15264. 'szlig': '\u00DF',
  15265. 'target': '\u2316',
  15266. 'tau': '\u03C4',
  15267. 'tcaron': '\u0165',
  15268. 'tcedil': '\u0163',
  15269. 'tcy': '\u0442',
  15270. 'telrec': '\u2315',
  15271. 'tfr': '\uD835\uDD31',
  15272. 'theta': '\u03B8',
  15273. 'thetasym': '\u03D1',
  15274. 'thetav': '\u03D1',
  15275. 'vartheta': '\u03D1',
  15276. 'thorn': '\u00FE',
  15277. 'times': '\u00D7',
  15278. 'timesbar': '\u2A31',
  15279. 'timesd': '\u2A30',
  15280. 'topbot': '\u2336',
  15281. 'topcir': '\u2AF1',
  15282. 'topf': '\uD835\uDD65',
  15283. 'topfork': '\u2ADA',
  15284. 'tprime': '\u2034',
  15285. 'triangle': '\u25B5',
  15286. 'utri': '\u25B5',
  15287. 'triangleq': '\u225C',
  15288. 'trie': '\u225C',
  15289. 'tridot': '\u25EC',
  15290. 'triminus': '\u2A3A',
  15291. 'triplus': '\u2A39',
  15292. 'trisb': '\u29CD',
  15293. 'tritime': '\u2A3B',
  15294. 'trpezium': '\u23E2',
  15295. 'tscr': '\uD835\uDCC9',
  15296. 'tscy': '\u0446',
  15297. 'tshcy': '\u045B',
  15298. 'tstrok': '\u0167',
  15299. 'uHar': '\u2963',
  15300. 'uacute': '\u00FA',
  15301. 'ubrcy': '\u045E',
  15302. 'ubreve': '\u016D',
  15303. 'ucirc': '\u00FB',
  15304. 'ucy': '\u0443',
  15305. 'udblac': '\u0171',
  15306. 'ufisht': '\u297E',
  15307. 'ufr': '\uD835\uDD32',
  15308. 'ugrave': '\u00F9',
  15309. 'uhblk': '\u2580',
  15310. 'ulcorn': '\u231C',
  15311. 'ulcorner': '\u231C',
  15312. 'ulcrop': '\u230F',
  15313. 'ultri': '\u25F8',
  15314. 'umacr': '\u016B',
  15315. 'uogon': '\u0173',
  15316. 'uopf': '\uD835\uDD66',
  15317. 'upsi': '\u03C5',
  15318. 'upsilon': '\u03C5',
  15319. 'upuparrows': '\u21C8',
  15320. 'uuarr': '\u21C8',
  15321. 'urcorn': '\u231D',
  15322. 'urcorner': '\u231D',
  15323. 'urcrop': '\u230E',
  15324. 'uring': '\u016F',
  15325. 'urtri': '\u25F9',
  15326. 'uscr': '\uD835\uDCCA',
  15327. 'utdot': '\u22F0',
  15328. 'utilde': '\u0169',
  15329. 'uuml': '\u00FC',
  15330. 'uwangle': '\u29A7',
  15331. 'vBar': '\u2AE8',
  15332. 'vBarv': '\u2AE9',
  15333. 'vangrt': '\u299C',
  15334. 'varsubsetneq': '\u228A\uFE00',
  15335. 'vsubne': '\u228A\uFE00',
  15336. 'varsubsetneqq': '\u2ACB\uFE00',
  15337. 'vsubnE': '\u2ACB\uFE00',
  15338. 'varsupsetneq': '\u228B\uFE00',
  15339. 'vsupne': '\u228B\uFE00',
  15340. 'varsupsetneqq': '\u2ACC\uFE00',
  15341. 'vsupnE': '\u2ACC\uFE00',
  15342. 'vcy': '\u0432',
  15343. 'veebar': '\u22BB',
  15344. 'veeeq': '\u225A',
  15345. 'vellip': '\u22EE',
  15346. 'vfr': '\uD835\uDD33',
  15347. 'vopf': '\uD835\uDD67',
  15348. 'vscr': '\uD835\uDCCB',
  15349. 'vzigzag': '\u299A',
  15350. 'wcirc': '\u0175',
  15351. 'wedbar': '\u2A5F',
  15352. 'wedgeq': '\u2259',
  15353. 'weierp': '\u2118',
  15354. 'wp': '\u2118',
  15355. 'wfr': '\uD835\uDD34',
  15356. 'wopf': '\uD835\uDD68',
  15357. 'wscr': '\uD835\uDCCC',
  15358. 'xfr': '\uD835\uDD35',
  15359. 'xi': '\u03BE',
  15360. 'xnis': '\u22FB',
  15361. 'xopf': '\uD835\uDD69',
  15362. 'xscr': '\uD835\uDCCD',
  15363. 'yacute': '\u00FD',
  15364. 'yacy': '\u044F',
  15365. 'ycirc': '\u0177',
  15366. 'ycy': '\u044B',
  15367. 'yen': '\u00A5',
  15368. 'yfr': '\uD835\uDD36',
  15369. 'yicy': '\u0457',
  15370. 'yopf': '\uD835\uDD6A',
  15371. 'yscr': '\uD835\uDCCE',
  15372. 'yucy': '\u044E',
  15373. 'yuml': '\u00FF',
  15374. 'zacute': '\u017A',
  15375. 'zcaron': '\u017E',
  15376. 'zcy': '\u0437',
  15377. 'zdot': '\u017C',
  15378. 'zeta': '\u03B6',
  15379. 'zfr': '\uD835\uDD37',
  15380. 'zhcy': '\u0436',
  15381. 'zigrarr': '\u21DD',
  15382. 'zopf': '\uD835\uDD6B',
  15383. 'zscr': '\uD835\uDCCF',
  15384. 'zwj': '\u200D',
  15385. 'zwnj': '\u200C',
  15386. };
  15387. // The &ngsp; pseudo-entity is denoting a space.
  15388. // 0xE500 is a PUA (Private Use Areas) unicode character
  15389. // This is inspired by the Angular Dart implementation.
  15390. const NGSP_UNICODE = '\uE500';
  15391. NAMED_ENTITIES['ngsp'] = NGSP_UNICODE;
  15392. class TokenError extends ParseError {
  15393. tokenType;
  15394. constructor(errorMsg, tokenType, span) {
  15395. super(span, errorMsg);
  15396. this.tokenType = tokenType;
  15397. }
  15398. }
  15399. class TokenizeResult {
  15400. tokens;
  15401. errors;
  15402. nonNormalizedIcuExpressions;
  15403. constructor(tokens, errors, nonNormalizedIcuExpressions) {
  15404. this.tokens = tokens;
  15405. this.errors = errors;
  15406. this.nonNormalizedIcuExpressions = nonNormalizedIcuExpressions;
  15407. }
  15408. }
  15409. function tokenize(source, url, getTagDefinition, options = {}) {
  15410. const tokenizer = new _Tokenizer(new ParseSourceFile(source, url), getTagDefinition, options);
  15411. tokenizer.tokenize();
  15412. return new TokenizeResult(mergeTextTokens(tokenizer.tokens), tokenizer.errors, tokenizer.nonNormalizedIcuExpressions);
  15413. }
  15414. const _CR_OR_CRLF_REGEXP = /\r\n?/g;
  15415. function _unexpectedCharacterErrorMsg(charCode) {
  15416. const char = charCode === $EOF ? 'EOF' : String.fromCharCode(charCode);
  15417. return `Unexpected character "${char}"`;
  15418. }
  15419. function _unknownEntityErrorMsg(entitySrc) {
  15420. return `Unknown entity "${entitySrc}" - use the "&#<decimal>;" or "&#x<hex>;" syntax`;
  15421. }
  15422. function _unparsableEntityErrorMsg(type, entityStr) {
  15423. return `Unable to parse entity "${entityStr}" - ${type} character reference entities must end with ";"`;
  15424. }
  15425. var CharacterReferenceType;
  15426. (function (CharacterReferenceType) {
  15427. CharacterReferenceType["HEX"] = "hexadecimal";
  15428. CharacterReferenceType["DEC"] = "decimal";
  15429. })(CharacterReferenceType || (CharacterReferenceType = {}));
  15430. class _ControlFlowError {
  15431. error;
  15432. constructor(error) {
  15433. this.error = error;
  15434. }
  15435. }
  15436. // See https://www.w3.org/TR/html51/syntax.html#writing-html-documents
  15437. class _Tokenizer {
  15438. _getTagDefinition;
  15439. _cursor;
  15440. _tokenizeIcu;
  15441. _interpolationConfig;
  15442. _leadingTriviaCodePoints;
  15443. _currentTokenStart = null;
  15444. _currentTokenType = null;
  15445. _expansionCaseStack = [];
  15446. _inInterpolation = false;
  15447. _preserveLineEndings;
  15448. _i18nNormalizeLineEndingsInICUs;
  15449. _tokenizeBlocks;
  15450. _tokenizeLet;
  15451. tokens = [];
  15452. errors = [];
  15453. nonNormalizedIcuExpressions = [];
  15454. /**
  15455. * @param _file The html source file being tokenized.
  15456. * @param _getTagDefinition A function that will retrieve a tag definition for a given tag name.
  15457. * @param options Configuration of the tokenization.
  15458. */
  15459. constructor(_file, _getTagDefinition, options) {
  15460. this._getTagDefinition = _getTagDefinition;
  15461. this._tokenizeIcu = options.tokenizeExpansionForms || false;
  15462. this._interpolationConfig = options.interpolationConfig || DEFAULT_INTERPOLATION_CONFIG;
  15463. this._leadingTriviaCodePoints =
  15464. options.leadingTriviaChars && options.leadingTriviaChars.map((c) => c.codePointAt(0) || 0);
  15465. const range = options.range || {
  15466. endPos: _file.content.length,
  15467. startPos: 0,
  15468. startLine: 0,
  15469. startCol: 0,
  15470. };
  15471. this._cursor = options.escapedString
  15472. ? new EscapedCharacterCursor(_file, range)
  15473. : new PlainCharacterCursor(_file, range);
  15474. this._preserveLineEndings = options.preserveLineEndings || false;
  15475. this._i18nNormalizeLineEndingsInICUs = options.i18nNormalizeLineEndingsInICUs || false;
  15476. this._tokenizeBlocks = options.tokenizeBlocks ?? true;
  15477. this._tokenizeLet = options.tokenizeLet ?? true;
  15478. try {
  15479. this._cursor.init();
  15480. }
  15481. catch (e) {
  15482. this.handleError(e);
  15483. }
  15484. }
  15485. _processCarriageReturns(content) {
  15486. if (this._preserveLineEndings) {
  15487. return content;
  15488. }
  15489. // https://www.w3.org/TR/html51/syntax.html#preprocessing-the-input-stream
  15490. // In order to keep the original position in the source, we can not
  15491. // pre-process it.
  15492. // Instead CRs are processed right before instantiating the tokens.
  15493. return content.replace(_CR_OR_CRLF_REGEXP, '\n');
  15494. }
  15495. tokenize() {
  15496. while (this._cursor.peek() !== $EOF) {
  15497. const start = this._cursor.clone();
  15498. try {
  15499. if (this._attemptCharCode($LT)) {
  15500. if (this._attemptCharCode($BANG)) {
  15501. if (this._attemptCharCode($LBRACKET)) {
  15502. this._consumeCdata(start);
  15503. }
  15504. else if (this._attemptCharCode($MINUS)) {
  15505. this._consumeComment(start);
  15506. }
  15507. else {
  15508. this._consumeDocType(start);
  15509. }
  15510. }
  15511. else if (this._attemptCharCode($SLASH)) {
  15512. this._consumeTagClose(start);
  15513. }
  15514. else {
  15515. this._consumeTagOpen(start);
  15516. }
  15517. }
  15518. else if (this._tokenizeLet &&
  15519. // Use `peek` instead of `attempCharCode` since we
  15520. // don't want to advance in case it's not `@let`.
  15521. this._cursor.peek() === $AT &&
  15522. !this._inInterpolation &&
  15523. this._attemptStr('@let')) {
  15524. this._consumeLetDeclaration(start);
  15525. }
  15526. else if (this._tokenizeBlocks && this._attemptCharCode($AT)) {
  15527. this._consumeBlockStart(start);
  15528. }
  15529. else if (this._tokenizeBlocks &&
  15530. !this._inInterpolation &&
  15531. !this._isInExpansionCase() &&
  15532. !this._isInExpansionForm() &&
  15533. this._attemptCharCode($RBRACE)) {
  15534. this._consumeBlockEnd(start);
  15535. }
  15536. else if (!(this._tokenizeIcu && this._tokenizeExpansionForm())) {
  15537. // In (possibly interpolated) text the end of the text is given by `isTextEnd()`, while
  15538. // the premature end of an interpolation is given by the start of a new HTML element.
  15539. this._consumeWithInterpolation(5 /* TokenType.TEXT */, 8 /* TokenType.INTERPOLATION */, () => this._isTextEnd(), () => this._isTagStart());
  15540. }
  15541. }
  15542. catch (e) {
  15543. this.handleError(e);
  15544. }
  15545. }
  15546. this._beginToken(33 /* TokenType.EOF */);
  15547. this._endToken([]);
  15548. }
  15549. _getBlockName() {
  15550. // This allows us to capture up something like `@else if`, but not `@ if`.
  15551. let spacesInNameAllowed = false;
  15552. const nameCursor = this._cursor.clone();
  15553. this._attemptCharCodeUntilFn((code) => {
  15554. if (isWhitespace(code)) {
  15555. return !spacesInNameAllowed;
  15556. }
  15557. if (isBlockNameChar(code)) {
  15558. spacesInNameAllowed = true;
  15559. return false;
  15560. }
  15561. return true;
  15562. });
  15563. return this._cursor.getChars(nameCursor).trim();
  15564. }
  15565. _consumeBlockStart(start) {
  15566. this._beginToken(24 /* TokenType.BLOCK_OPEN_START */, start);
  15567. const startToken = this._endToken([this._getBlockName()]);
  15568. if (this._cursor.peek() === $LPAREN) {
  15569. // Advance past the opening paren.
  15570. this._cursor.advance();
  15571. // Capture the parameters.
  15572. this._consumeBlockParameters();
  15573. // Allow spaces before the closing paren.
  15574. this._attemptCharCodeUntilFn(isNotWhitespace);
  15575. if (this._attemptCharCode($RPAREN)) {
  15576. // Allow spaces after the paren.
  15577. this._attemptCharCodeUntilFn(isNotWhitespace);
  15578. }
  15579. else {
  15580. startToken.type = 28 /* TokenType.INCOMPLETE_BLOCK_OPEN */;
  15581. return;
  15582. }
  15583. }
  15584. if (this._attemptCharCode($LBRACE)) {
  15585. this._beginToken(25 /* TokenType.BLOCK_OPEN_END */);
  15586. this._endToken([]);
  15587. }
  15588. else {
  15589. startToken.type = 28 /* TokenType.INCOMPLETE_BLOCK_OPEN */;
  15590. }
  15591. }
  15592. _consumeBlockEnd(start) {
  15593. this._beginToken(26 /* TokenType.BLOCK_CLOSE */, start);
  15594. this._endToken([]);
  15595. }
  15596. _consumeBlockParameters() {
  15597. // Trim the whitespace until the first parameter.
  15598. this._attemptCharCodeUntilFn(isBlockParameterChar);
  15599. while (this._cursor.peek() !== $RPAREN && this._cursor.peek() !== $EOF) {
  15600. this._beginToken(27 /* TokenType.BLOCK_PARAMETER */);
  15601. const start = this._cursor.clone();
  15602. let inQuote = null;
  15603. let openParens = 0;
  15604. // Consume the parameter until the next semicolon or brace.
  15605. // Note that we skip over semicolons/braces inside of strings.
  15606. while ((this._cursor.peek() !== $SEMICOLON && this._cursor.peek() !== $EOF) ||
  15607. inQuote !== null) {
  15608. const char = this._cursor.peek();
  15609. // Skip to the next character if it was escaped.
  15610. if (char === $BACKSLASH) {
  15611. this._cursor.advance();
  15612. }
  15613. else if (char === inQuote) {
  15614. inQuote = null;
  15615. }
  15616. else if (inQuote === null && isQuote(char)) {
  15617. inQuote = char;
  15618. }
  15619. else if (char === $LPAREN && inQuote === null) {
  15620. openParens++;
  15621. }
  15622. else if (char === $RPAREN && inQuote === null) {
  15623. if (openParens === 0) {
  15624. break;
  15625. }
  15626. else if (openParens > 0) {
  15627. openParens--;
  15628. }
  15629. }
  15630. this._cursor.advance();
  15631. }
  15632. this._endToken([this._cursor.getChars(start)]);
  15633. // Skip to the next parameter.
  15634. this._attemptCharCodeUntilFn(isBlockParameterChar);
  15635. }
  15636. }
  15637. _consumeLetDeclaration(start) {
  15638. this._beginToken(29 /* TokenType.LET_START */, start);
  15639. // Require at least one white space after the `@let`.
  15640. if (isWhitespace(this._cursor.peek())) {
  15641. this._attemptCharCodeUntilFn(isNotWhitespace);
  15642. }
  15643. else {
  15644. const token = this._endToken([this._cursor.getChars(start)]);
  15645. token.type = 32 /* TokenType.INCOMPLETE_LET */;
  15646. return;
  15647. }
  15648. const startToken = this._endToken([this._getLetDeclarationName()]);
  15649. // Skip over white space before the equals character.
  15650. this._attemptCharCodeUntilFn(isNotWhitespace);
  15651. // Expect an equals sign.
  15652. if (!this._attemptCharCode($EQ)) {
  15653. startToken.type = 32 /* TokenType.INCOMPLETE_LET */;
  15654. return;
  15655. }
  15656. // Skip spaces after the equals.
  15657. this._attemptCharCodeUntilFn((code) => isNotWhitespace(code) && !isNewLine(code));
  15658. this._consumeLetDeclarationValue();
  15659. // Terminate the `@let` with a semicolon.
  15660. const endChar = this._cursor.peek();
  15661. if (endChar === $SEMICOLON) {
  15662. this._beginToken(31 /* TokenType.LET_END */);
  15663. this._endToken([]);
  15664. this._cursor.advance();
  15665. }
  15666. else {
  15667. startToken.type = 32 /* TokenType.INCOMPLETE_LET */;
  15668. startToken.sourceSpan = this._cursor.getSpan(start);
  15669. }
  15670. }
  15671. _getLetDeclarationName() {
  15672. const nameCursor = this._cursor.clone();
  15673. let allowDigit = false;
  15674. this._attemptCharCodeUntilFn((code) => {
  15675. if (isAsciiLetter(code) ||
  15676. code === $$ ||
  15677. code === $_ ||
  15678. // `@let` names can't start with a digit, but digits are valid anywhere else in the name.
  15679. (allowDigit && isDigit(code))) {
  15680. allowDigit = true;
  15681. return false;
  15682. }
  15683. return true;
  15684. });
  15685. return this._cursor.getChars(nameCursor).trim();
  15686. }
  15687. _consumeLetDeclarationValue() {
  15688. const start = this._cursor.clone();
  15689. this._beginToken(30 /* TokenType.LET_VALUE */, start);
  15690. while (this._cursor.peek() !== $EOF) {
  15691. const char = this._cursor.peek();
  15692. // `@let` declarations terminate with a semicolon.
  15693. if (char === $SEMICOLON) {
  15694. break;
  15695. }
  15696. // If we hit a quote, skip over its content since we don't care what's inside.
  15697. if (isQuote(char)) {
  15698. this._cursor.advance();
  15699. this._attemptCharCodeUntilFn((inner) => {
  15700. if (inner === $BACKSLASH) {
  15701. this._cursor.advance();
  15702. return false;
  15703. }
  15704. return inner === char;
  15705. });
  15706. }
  15707. this._cursor.advance();
  15708. }
  15709. this._endToken([this._cursor.getChars(start)]);
  15710. }
  15711. /**
  15712. * @returns whether an ICU token has been created
  15713. * @internal
  15714. */
  15715. _tokenizeExpansionForm() {
  15716. if (this.isExpansionFormStart()) {
  15717. this._consumeExpansionFormStart();
  15718. return true;
  15719. }
  15720. if (isExpansionCaseStart(this._cursor.peek()) && this._isInExpansionForm()) {
  15721. this._consumeExpansionCaseStart();
  15722. return true;
  15723. }
  15724. if (this._cursor.peek() === $RBRACE) {
  15725. if (this._isInExpansionCase()) {
  15726. this._consumeExpansionCaseEnd();
  15727. return true;
  15728. }
  15729. if (this._isInExpansionForm()) {
  15730. this._consumeExpansionFormEnd();
  15731. return true;
  15732. }
  15733. }
  15734. return false;
  15735. }
  15736. _beginToken(type, start = this._cursor.clone()) {
  15737. this._currentTokenStart = start;
  15738. this._currentTokenType = type;
  15739. }
  15740. _endToken(parts, end) {
  15741. if (this._currentTokenStart === null) {
  15742. throw new TokenError('Programming error - attempted to end a token when there was no start to the token', this._currentTokenType, this._cursor.getSpan(end));
  15743. }
  15744. if (this._currentTokenType === null) {
  15745. throw new TokenError('Programming error - attempted to end a token which has no token type', null, this._cursor.getSpan(this._currentTokenStart));
  15746. }
  15747. const token = {
  15748. type: this._currentTokenType,
  15749. parts,
  15750. sourceSpan: (end ?? this._cursor).getSpan(this._currentTokenStart, this._leadingTriviaCodePoints),
  15751. };
  15752. this.tokens.push(token);
  15753. this._currentTokenStart = null;
  15754. this._currentTokenType = null;
  15755. return token;
  15756. }
  15757. _createError(msg, span) {
  15758. if (this._isInExpansionForm()) {
  15759. msg += ` (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)`;
  15760. }
  15761. const error = new TokenError(msg, this._currentTokenType, span);
  15762. this._currentTokenStart = null;
  15763. this._currentTokenType = null;
  15764. return new _ControlFlowError(error);
  15765. }
  15766. handleError(e) {
  15767. if (e instanceof CursorError) {
  15768. e = this._createError(e.msg, this._cursor.getSpan(e.cursor));
  15769. }
  15770. if (e instanceof _ControlFlowError) {
  15771. this.errors.push(e.error);
  15772. }
  15773. else {
  15774. throw e;
  15775. }
  15776. }
  15777. _attemptCharCode(charCode) {
  15778. if (this._cursor.peek() === charCode) {
  15779. this._cursor.advance();
  15780. return true;
  15781. }
  15782. return false;
  15783. }
  15784. _attemptCharCodeCaseInsensitive(charCode) {
  15785. if (compareCharCodeCaseInsensitive(this._cursor.peek(), charCode)) {
  15786. this._cursor.advance();
  15787. return true;
  15788. }
  15789. return false;
  15790. }
  15791. _requireCharCode(charCode) {
  15792. const location = this._cursor.clone();
  15793. if (!this._attemptCharCode(charCode)) {
  15794. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  15795. }
  15796. }
  15797. _attemptStr(chars) {
  15798. const len = chars.length;
  15799. if (this._cursor.charsLeft() < len) {
  15800. return false;
  15801. }
  15802. const initialPosition = this._cursor.clone();
  15803. for (let i = 0; i < len; i++) {
  15804. if (!this._attemptCharCode(chars.charCodeAt(i))) {
  15805. // If attempting to parse the string fails, we want to reset the parser
  15806. // to where it was before the attempt
  15807. this._cursor = initialPosition;
  15808. return false;
  15809. }
  15810. }
  15811. return true;
  15812. }
  15813. _attemptStrCaseInsensitive(chars) {
  15814. for (let i = 0; i < chars.length; i++) {
  15815. if (!this._attemptCharCodeCaseInsensitive(chars.charCodeAt(i))) {
  15816. return false;
  15817. }
  15818. }
  15819. return true;
  15820. }
  15821. _requireStr(chars) {
  15822. const location = this._cursor.clone();
  15823. if (!this._attemptStr(chars)) {
  15824. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  15825. }
  15826. }
  15827. _attemptCharCodeUntilFn(predicate) {
  15828. while (!predicate(this._cursor.peek())) {
  15829. this._cursor.advance();
  15830. }
  15831. }
  15832. _requireCharCodeUntilFn(predicate, len) {
  15833. const start = this._cursor.clone();
  15834. this._attemptCharCodeUntilFn(predicate);
  15835. if (this._cursor.diff(start) < len) {
  15836. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  15837. }
  15838. }
  15839. _attemptUntilChar(char) {
  15840. while (this._cursor.peek() !== char) {
  15841. this._cursor.advance();
  15842. }
  15843. }
  15844. _readChar() {
  15845. // Don't rely upon reading directly from `_input` as the actual char value
  15846. // may have been generated from an escape sequence.
  15847. const char = String.fromCodePoint(this._cursor.peek());
  15848. this._cursor.advance();
  15849. return char;
  15850. }
  15851. _consumeEntity(textTokenType) {
  15852. this._beginToken(9 /* TokenType.ENCODED_ENTITY */);
  15853. const start = this._cursor.clone();
  15854. this._cursor.advance();
  15855. if (this._attemptCharCode($HASH)) {
  15856. const isHex = this._attemptCharCode($x) || this._attemptCharCode($X);
  15857. const codeStart = this._cursor.clone();
  15858. this._attemptCharCodeUntilFn(isDigitEntityEnd);
  15859. if (this._cursor.peek() != $SEMICOLON) {
  15860. // Advance cursor to include the peeked character in the string provided to the error
  15861. // message.
  15862. this._cursor.advance();
  15863. const entityType = isHex ? CharacterReferenceType.HEX : CharacterReferenceType.DEC;
  15864. throw this._createError(_unparsableEntityErrorMsg(entityType, this._cursor.getChars(start)), this._cursor.getSpan());
  15865. }
  15866. const strNum = this._cursor.getChars(codeStart);
  15867. this._cursor.advance();
  15868. try {
  15869. const charCode = parseInt(strNum, isHex ? 16 : 10);
  15870. this._endToken([String.fromCharCode(charCode), this._cursor.getChars(start)]);
  15871. }
  15872. catch {
  15873. throw this._createError(_unknownEntityErrorMsg(this._cursor.getChars(start)), this._cursor.getSpan());
  15874. }
  15875. }
  15876. else {
  15877. const nameStart = this._cursor.clone();
  15878. this._attemptCharCodeUntilFn(isNamedEntityEnd);
  15879. if (this._cursor.peek() != $SEMICOLON) {
  15880. // No semicolon was found so abort the encoded entity token that was in progress, and treat
  15881. // this as a text token
  15882. this._beginToken(textTokenType, start);
  15883. this._cursor = nameStart;
  15884. this._endToken(['&']);
  15885. }
  15886. else {
  15887. const name = this._cursor.getChars(nameStart);
  15888. this._cursor.advance();
  15889. const char = NAMED_ENTITIES.hasOwnProperty(name) && NAMED_ENTITIES[name];
  15890. if (!char) {
  15891. throw this._createError(_unknownEntityErrorMsg(name), this._cursor.getSpan(start));
  15892. }
  15893. this._endToken([char, `&${name};`]);
  15894. }
  15895. }
  15896. }
  15897. _consumeRawText(consumeEntities, endMarkerPredicate) {
  15898. this._beginToken(consumeEntities ? 6 /* TokenType.ESCAPABLE_RAW_TEXT */ : 7 /* TokenType.RAW_TEXT */);
  15899. const parts = [];
  15900. while (true) {
  15901. const tagCloseStart = this._cursor.clone();
  15902. const foundEndMarker = endMarkerPredicate();
  15903. this._cursor = tagCloseStart;
  15904. if (foundEndMarker) {
  15905. break;
  15906. }
  15907. if (consumeEntities && this._cursor.peek() === $AMPERSAND) {
  15908. this._endToken([this._processCarriageReturns(parts.join(''))]);
  15909. parts.length = 0;
  15910. this._consumeEntity(6 /* TokenType.ESCAPABLE_RAW_TEXT */);
  15911. this._beginToken(6 /* TokenType.ESCAPABLE_RAW_TEXT */);
  15912. }
  15913. else {
  15914. parts.push(this._readChar());
  15915. }
  15916. }
  15917. this._endToken([this._processCarriageReturns(parts.join(''))]);
  15918. }
  15919. _consumeComment(start) {
  15920. this._beginToken(10 /* TokenType.COMMENT_START */, start);
  15921. this._requireCharCode($MINUS);
  15922. this._endToken([]);
  15923. this._consumeRawText(false, () => this._attemptStr('-->'));
  15924. this._beginToken(11 /* TokenType.COMMENT_END */);
  15925. this._requireStr('-->');
  15926. this._endToken([]);
  15927. }
  15928. _consumeCdata(start) {
  15929. this._beginToken(12 /* TokenType.CDATA_START */, start);
  15930. this._requireStr('CDATA[');
  15931. this._endToken([]);
  15932. this._consumeRawText(false, () => this._attemptStr(']]>'));
  15933. this._beginToken(13 /* TokenType.CDATA_END */);
  15934. this._requireStr(']]>');
  15935. this._endToken([]);
  15936. }
  15937. _consumeDocType(start) {
  15938. this._beginToken(18 /* TokenType.DOC_TYPE */, start);
  15939. const contentStart = this._cursor.clone();
  15940. this._attemptUntilChar($GT);
  15941. const content = this._cursor.getChars(contentStart);
  15942. this._cursor.advance();
  15943. this._endToken([content]);
  15944. }
  15945. _consumePrefixAndName() {
  15946. const nameOrPrefixStart = this._cursor.clone();
  15947. let prefix = '';
  15948. while (this._cursor.peek() !== $COLON && !isPrefixEnd(this._cursor.peek())) {
  15949. this._cursor.advance();
  15950. }
  15951. let nameStart;
  15952. if (this._cursor.peek() === $COLON) {
  15953. prefix = this._cursor.getChars(nameOrPrefixStart);
  15954. this._cursor.advance();
  15955. nameStart = this._cursor.clone();
  15956. }
  15957. else {
  15958. nameStart = nameOrPrefixStart;
  15959. }
  15960. this._requireCharCodeUntilFn(isNameEnd, prefix === '' ? 0 : 1);
  15961. const name = this._cursor.getChars(nameStart);
  15962. return [prefix, name];
  15963. }
  15964. _consumeTagOpen(start) {
  15965. let tagName;
  15966. let prefix;
  15967. let openTagToken;
  15968. try {
  15969. if (!isAsciiLetter(this._cursor.peek())) {
  15970. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  15971. }
  15972. openTagToken = this._consumeTagOpenStart(start);
  15973. prefix = openTagToken.parts[0];
  15974. tagName = openTagToken.parts[1];
  15975. this._attemptCharCodeUntilFn(isNotWhitespace);
  15976. while (this._cursor.peek() !== $SLASH &&
  15977. this._cursor.peek() !== $GT &&
  15978. this._cursor.peek() !== $LT &&
  15979. this._cursor.peek() !== $EOF) {
  15980. this._consumeAttributeName();
  15981. this._attemptCharCodeUntilFn(isNotWhitespace);
  15982. if (this._attemptCharCode($EQ)) {
  15983. this._attemptCharCodeUntilFn(isNotWhitespace);
  15984. this._consumeAttributeValue();
  15985. }
  15986. this._attemptCharCodeUntilFn(isNotWhitespace);
  15987. }
  15988. this._consumeTagOpenEnd();
  15989. }
  15990. catch (e) {
  15991. if (e instanceof _ControlFlowError) {
  15992. if (openTagToken) {
  15993. // We errored before we could close the opening tag, so it is incomplete.
  15994. openTagToken.type = 4 /* TokenType.INCOMPLETE_TAG_OPEN */;
  15995. }
  15996. else {
  15997. // When the start tag is invalid, assume we want a "<" as text.
  15998. // Back to back text tokens are merged at the end.
  15999. this._beginToken(5 /* TokenType.TEXT */, start);
  16000. this._endToken(['<']);
  16001. }
  16002. return;
  16003. }
  16004. throw e;
  16005. }
  16006. const contentTokenType = this._getTagDefinition(tagName).getContentType(prefix);
  16007. if (contentTokenType === exports.TagContentType.RAW_TEXT) {
  16008. this._consumeRawTextWithTagClose(prefix, tagName, false);
  16009. }
  16010. else if (contentTokenType === exports.TagContentType.ESCAPABLE_RAW_TEXT) {
  16011. this._consumeRawTextWithTagClose(prefix, tagName, true);
  16012. }
  16013. }
  16014. _consumeRawTextWithTagClose(prefix, tagName, consumeEntities) {
  16015. this._consumeRawText(consumeEntities, () => {
  16016. if (!this._attemptCharCode($LT))
  16017. return false;
  16018. if (!this._attemptCharCode($SLASH))
  16019. return false;
  16020. this._attemptCharCodeUntilFn(isNotWhitespace);
  16021. if (!this._attemptStrCaseInsensitive(tagName))
  16022. return false;
  16023. this._attemptCharCodeUntilFn(isNotWhitespace);
  16024. return this._attemptCharCode($GT);
  16025. });
  16026. this._beginToken(3 /* TokenType.TAG_CLOSE */);
  16027. this._requireCharCodeUntilFn((code) => code === $GT, 3);
  16028. this._cursor.advance(); // Consume the `>`
  16029. this._endToken([prefix, tagName]);
  16030. }
  16031. _consumeTagOpenStart(start) {
  16032. this._beginToken(0 /* TokenType.TAG_OPEN_START */, start);
  16033. const parts = this._consumePrefixAndName();
  16034. return this._endToken(parts);
  16035. }
  16036. _consumeAttributeName() {
  16037. const attrNameStart = this._cursor.peek();
  16038. if (attrNameStart === $SQ || attrNameStart === $DQ) {
  16039. throw this._createError(_unexpectedCharacterErrorMsg(attrNameStart), this._cursor.getSpan());
  16040. }
  16041. this._beginToken(14 /* TokenType.ATTR_NAME */);
  16042. const prefixAndName = this._consumePrefixAndName();
  16043. this._endToken(prefixAndName);
  16044. }
  16045. _consumeAttributeValue() {
  16046. if (this._cursor.peek() === $SQ || this._cursor.peek() === $DQ) {
  16047. const quoteChar = this._cursor.peek();
  16048. this._consumeQuote(quoteChar);
  16049. // In an attribute then end of the attribute value and the premature end to an interpolation
  16050. // are both triggered by the `quoteChar`.
  16051. const endPredicate = () => this._cursor.peek() === quoteChar;
  16052. this._consumeWithInterpolation(16 /* TokenType.ATTR_VALUE_TEXT */, 17 /* TokenType.ATTR_VALUE_INTERPOLATION */, endPredicate, endPredicate);
  16053. this._consumeQuote(quoteChar);
  16054. }
  16055. else {
  16056. const endPredicate = () => isNameEnd(this._cursor.peek());
  16057. this._consumeWithInterpolation(16 /* TokenType.ATTR_VALUE_TEXT */, 17 /* TokenType.ATTR_VALUE_INTERPOLATION */, endPredicate, endPredicate);
  16058. }
  16059. }
  16060. _consumeQuote(quoteChar) {
  16061. this._beginToken(15 /* TokenType.ATTR_QUOTE */);
  16062. this._requireCharCode(quoteChar);
  16063. this._endToken([String.fromCodePoint(quoteChar)]);
  16064. }
  16065. _consumeTagOpenEnd() {
  16066. const tokenType = this._attemptCharCode($SLASH)
  16067. ? 2 /* TokenType.TAG_OPEN_END_VOID */
  16068. : 1 /* TokenType.TAG_OPEN_END */;
  16069. this._beginToken(tokenType);
  16070. this._requireCharCode($GT);
  16071. this._endToken([]);
  16072. }
  16073. _consumeTagClose(start) {
  16074. this._beginToken(3 /* TokenType.TAG_CLOSE */, start);
  16075. this._attemptCharCodeUntilFn(isNotWhitespace);
  16076. const prefixAndName = this._consumePrefixAndName();
  16077. this._attemptCharCodeUntilFn(isNotWhitespace);
  16078. this._requireCharCode($GT);
  16079. this._endToken(prefixAndName);
  16080. }
  16081. _consumeExpansionFormStart() {
  16082. this._beginToken(19 /* TokenType.EXPANSION_FORM_START */);
  16083. this._requireCharCode($LBRACE);
  16084. this._endToken([]);
  16085. this._expansionCaseStack.push(19 /* TokenType.EXPANSION_FORM_START */);
  16086. this._beginToken(7 /* TokenType.RAW_TEXT */);
  16087. const condition = this._readUntil($COMMA);
  16088. const normalizedCondition = this._processCarriageReturns(condition);
  16089. if (this._i18nNormalizeLineEndingsInICUs) {
  16090. // We explicitly want to normalize line endings for this text.
  16091. this._endToken([normalizedCondition]);
  16092. }
  16093. else {
  16094. // We are not normalizing line endings.
  16095. const conditionToken = this._endToken([condition]);
  16096. if (normalizedCondition !== condition) {
  16097. this.nonNormalizedIcuExpressions.push(conditionToken);
  16098. }
  16099. }
  16100. this._requireCharCode($COMMA);
  16101. this._attemptCharCodeUntilFn(isNotWhitespace);
  16102. this._beginToken(7 /* TokenType.RAW_TEXT */);
  16103. const type = this._readUntil($COMMA);
  16104. this._endToken([type]);
  16105. this._requireCharCode($COMMA);
  16106. this._attemptCharCodeUntilFn(isNotWhitespace);
  16107. }
  16108. _consumeExpansionCaseStart() {
  16109. this._beginToken(20 /* TokenType.EXPANSION_CASE_VALUE */);
  16110. const value = this._readUntil($LBRACE).trim();
  16111. this._endToken([value]);
  16112. this._attemptCharCodeUntilFn(isNotWhitespace);
  16113. this._beginToken(21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16114. this._requireCharCode($LBRACE);
  16115. this._endToken([]);
  16116. this._attemptCharCodeUntilFn(isNotWhitespace);
  16117. this._expansionCaseStack.push(21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16118. }
  16119. _consumeExpansionCaseEnd() {
  16120. this._beginToken(22 /* TokenType.EXPANSION_CASE_EXP_END */);
  16121. this._requireCharCode($RBRACE);
  16122. this._endToken([]);
  16123. this._attemptCharCodeUntilFn(isNotWhitespace);
  16124. this._expansionCaseStack.pop();
  16125. }
  16126. _consumeExpansionFormEnd() {
  16127. this._beginToken(23 /* TokenType.EXPANSION_FORM_END */);
  16128. this._requireCharCode($RBRACE);
  16129. this._endToken([]);
  16130. this._expansionCaseStack.pop();
  16131. }
  16132. /**
  16133. * Consume a string that may contain interpolation expressions.
  16134. *
  16135. * The first token consumed will be of `tokenType` and then there will be alternating
  16136. * `interpolationTokenType` and `tokenType` tokens until the `endPredicate()` returns true.
  16137. *
  16138. * If an interpolation token ends prematurely it will have no end marker in its `parts` array.
  16139. *
  16140. * @param textTokenType the kind of tokens to interleave around interpolation tokens.
  16141. * @param interpolationTokenType the kind of tokens that contain interpolation.
  16142. * @param endPredicate a function that should return true when we should stop consuming.
  16143. * @param endInterpolation a function that should return true if there is a premature end to an
  16144. * interpolation expression - i.e. before we get to the normal interpolation closing marker.
  16145. */
  16146. _consumeWithInterpolation(textTokenType, interpolationTokenType, endPredicate, endInterpolation) {
  16147. this._beginToken(textTokenType);
  16148. const parts = [];
  16149. while (!endPredicate()) {
  16150. const current = this._cursor.clone();
  16151. if (this._interpolationConfig && this._attemptStr(this._interpolationConfig.start)) {
  16152. this._endToken([this._processCarriageReturns(parts.join(''))], current);
  16153. parts.length = 0;
  16154. this._consumeInterpolation(interpolationTokenType, current, endInterpolation);
  16155. this._beginToken(textTokenType);
  16156. }
  16157. else if (this._cursor.peek() === $AMPERSAND) {
  16158. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16159. parts.length = 0;
  16160. this._consumeEntity(textTokenType);
  16161. this._beginToken(textTokenType);
  16162. }
  16163. else {
  16164. parts.push(this._readChar());
  16165. }
  16166. }
  16167. // It is possible that an interpolation was started but not ended inside this text token.
  16168. // Make sure that we reset the state of the lexer correctly.
  16169. this._inInterpolation = false;
  16170. this._endToken([this._processCarriageReturns(parts.join(''))]);
  16171. }
  16172. /**
  16173. * Consume a block of text that has been interpreted as an Angular interpolation.
  16174. *
  16175. * @param interpolationTokenType the type of the interpolation token to generate.
  16176. * @param interpolationStart a cursor that points to the start of this interpolation.
  16177. * @param prematureEndPredicate a function that should return true if the next characters indicate
  16178. * an end to the interpolation before its normal closing marker.
  16179. */
  16180. _consumeInterpolation(interpolationTokenType, interpolationStart, prematureEndPredicate) {
  16181. const parts = [];
  16182. this._beginToken(interpolationTokenType, interpolationStart);
  16183. parts.push(this._interpolationConfig.start);
  16184. // Find the end of the interpolation, ignoring content inside quotes.
  16185. const expressionStart = this._cursor.clone();
  16186. let inQuote = null;
  16187. let inComment = false;
  16188. while (this._cursor.peek() !== $EOF &&
  16189. (prematureEndPredicate === null || !prematureEndPredicate())) {
  16190. const current = this._cursor.clone();
  16191. if (this._isTagStart()) {
  16192. // We are starting what looks like an HTML element in the middle of this interpolation.
  16193. // Reset the cursor to before the `<` character and end the interpolation token.
  16194. // (This is actually wrong but here for backward compatibility).
  16195. this._cursor = current;
  16196. parts.push(this._getProcessedChars(expressionStart, current));
  16197. this._endToken(parts);
  16198. return;
  16199. }
  16200. if (inQuote === null) {
  16201. if (this._attemptStr(this._interpolationConfig.end)) {
  16202. // We are not in a string, and we hit the end interpolation marker
  16203. parts.push(this._getProcessedChars(expressionStart, current));
  16204. parts.push(this._interpolationConfig.end);
  16205. this._endToken(parts);
  16206. return;
  16207. }
  16208. else if (this._attemptStr('//')) {
  16209. // Once we are in a comment we ignore any quotes
  16210. inComment = true;
  16211. }
  16212. }
  16213. const char = this._cursor.peek();
  16214. this._cursor.advance();
  16215. if (char === $BACKSLASH) {
  16216. // Skip the next character because it was escaped.
  16217. this._cursor.advance();
  16218. }
  16219. else if (char === inQuote) {
  16220. // Exiting the current quoted string
  16221. inQuote = null;
  16222. }
  16223. else if (!inComment && inQuote === null && isQuote(char)) {
  16224. // Entering a new quoted string
  16225. inQuote = char;
  16226. }
  16227. }
  16228. // We hit EOF without finding a closing interpolation marker
  16229. parts.push(this._getProcessedChars(expressionStart, this._cursor));
  16230. this._endToken(parts);
  16231. }
  16232. _getProcessedChars(start, end) {
  16233. return this._processCarriageReturns(end.getChars(start));
  16234. }
  16235. _isTextEnd() {
  16236. if (this._isTagStart() || this._cursor.peek() === $EOF) {
  16237. return true;
  16238. }
  16239. if (this._tokenizeIcu && !this._inInterpolation) {
  16240. if (this.isExpansionFormStart()) {
  16241. // start of an expansion form
  16242. return true;
  16243. }
  16244. if (this._cursor.peek() === $RBRACE && this._isInExpansionCase()) {
  16245. // end of and expansion case
  16246. return true;
  16247. }
  16248. }
  16249. if (this._tokenizeBlocks &&
  16250. !this._inInterpolation &&
  16251. !this._isInExpansion() &&
  16252. (this._cursor.peek() === $AT || this._cursor.peek() === $RBRACE)) {
  16253. return true;
  16254. }
  16255. return false;
  16256. }
  16257. /**
  16258. * Returns true if the current cursor is pointing to the start of a tag
  16259. * (opening/closing/comments/cdata/etc).
  16260. */
  16261. _isTagStart() {
  16262. if (this._cursor.peek() === $LT) {
  16263. // We assume that `<` followed by whitespace is not the start of an HTML element.
  16264. const tmp = this._cursor.clone();
  16265. tmp.advance();
  16266. // If the next character is alphabetic, ! nor / then it is a tag start
  16267. const code = tmp.peek();
  16268. if (($a <= code && code <= $z) ||
  16269. ($A <= code && code <= $Z) ||
  16270. code === $SLASH ||
  16271. code === $BANG) {
  16272. return true;
  16273. }
  16274. }
  16275. return false;
  16276. }
  16277. _readUntil(char) {
  16278. const start = this._cursor.clone();
  16279. this._attemptUntilChar(char);
  16280. return this._cursor.getChars(start);
  16281. }
  16282. _isInExpansion() {
  16283. return this._isInExpansionCase() || this._isInExpansionForm();
  16284. }
  16285. _isInExpansionCase() {
  16286. return (this._expansionCaseStack.length > 0 &&
  16287. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  16288. 21 /* TokenType.EXPANSION_CASE_EXP_START */);
  16289. }
  16290. _isInExpansionForm() {
  16291. return (this._expansionCaseStack.length > 0 &&
  16292. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  16293. 19 /* TokenType.EXPANSION_FORM_START */);
  16294. }
  16295. isExpansionFormStart() {
  16296. if (this._cursor.peek() !== $LBRACE) {
  16297. return false;
  16298. }
  16299. if (this._interpolationConfig) {
  16300. const start = this._cursor.clone();
  16301. const isInterpolation = this._attemptStr(this._interpolationConfig.start);
  16302. this._cursor = start;
  16303. return !isInterpolation;
  16304. }
  16305. return true;
  16306. }
  16307. }
  16308. function isNotWhitespace(code) {
  16309. return !isWhitespace(code) || code === $EOF;
  16310. }
  16311. function isNameEnd(code) {
  16312. return (isWhitespace(code) ||
  16313. code === $GT ||
  16314. code === $LT ||
  16315. code === $SLASH ||
  16316. code === $SQ ||
  16317. code === $DQ ||
  16318. code === $EQ ||
  16319. code === $EOF);
  16320. }
  16321. function isPrefixEnd(code) {
  16322. return ((code < $a || $z < code) &&
  16323. (code < $A || $Z < code) &&
  16324. (code < $0 || code > $9));
  16325. }
  16326. function isDigitEntityEnd(code) {
  16327. return code === $SEMICOLON || code === $EOF || !isAsciiHexDigit(code);
  16328. }
  16329. function isNamedEntityEnd(code) {
  16330. return code === $SEMICOLON || code === $EOF || !isAsciiLetter(code);
  16331. }
  16332. function isExpansionCaseStart(peek) {
  16333. return peek !== $RBRACE;
  16334. }
  16335. function compareCharCodeCaseInsensitive(code1, code2) {
  16336. return toUpperCaseCharCode(code1) === toUpperCaseCharCode(code2);
  16337. }
  16338. function toUpperCaseCharCode(code) {
  16339. return code >= $a && code <= $z ? code - $a + $A : code;
  16340. }
  16341. function isBlockNameChar(code) {
  16342. return isAsciiLetter(code) || isDigit(code) || code === $_;
  16343. }
  16344. function isBlockParameterChar(code) {
  16345. return code !== $SEMICOLON && isNotWhitespace(code);
  16346. }
  16347. function mergeTextTokens(srcTokens) {
  16348. const dstTokens = [];
  16349. let lastDstToken = undefined;
  16350. for (let i = 0; i < srcTokens.length; i++) {
  16351. const token = srcTokens[i];
  16352. if ((lastDstToken && lastDstToken.type === 5 /* TokenType.TEXT */ && token.type === 5 /* TokenType.TEXT */) ||
  16353. (lastDstToken &&
  16354. lastDstToken.type === 16 /* TokenType.ATTR_VALUE_TEXT */ &&
  16355. token.type === 16 /* TokenType.ATTR_VALUE_TEXT */)) {
  16356. lastDstToken.parts[0] += token.parts[0];
  16357. lastDstToken.sourceSpan.end = token.sourceSpan.end;
  16358. }
  16359. else {
  16360. lastDstToken = token;
  16361. dstTokens.push(lastDstToken);
  16362. }
  16363. }
  16364. return dstTokens;
  16365. }
  16366. class PlainCharacterCursor {
  16367. state;
  16368. file;
  16369. input;
  16370. end;
  16371. constructor(fileOrCursor, range) {
  16372. if (fileOrCursor instanceof PlainCharacterCursor) {
  16373. this.file = fileOrCursor.file;
  16374. this.input = fileOrCursor.input;
  16375. this.end = fileOrCursor.end;
  16376. const state = fileOrCursor.state;
  16377. // Note: avoid using `{...fileOrCursor.state}` here as that has a severe performance penalty.
  16378. // In ES5 bundles the object spread operator is translated into the `__assign` helper, which
  16379. // is not optimized by VMs as efficiently as a raw object literal. Since this constructor is
  16380. // called in tight loops, this difference matters.
  16381. this.state = {
  16382. peek: state.peek,
  16383. offset: state.offset,
  16384. line: state.line,
  16385. column: state.column,
  16386. };
  16387. }
  16388. else {
  16389. if (!range) {
  16390. throw new Error('Programming error: the range argument must be provided with a file argument.');
  16391. }
  16392. this.file = fileOrCursor;
  16393. this.input = fileOrCursor.content;
  16394. this.end = range.endPos;
  16395. this.state = {
  16396. peek: -1,
  16397. offset: range.startPos,
  16398. line: range.startLine,
  16399. column: range.startCol,
  16400. };
  16401. }
  16402. }
  16403. clone() {
  16404. return new PlainCharacterCursor(this);
  16405. }
  16406. peek() {
  16407. return this.state.peek;
  16408. }
  16409. charsLeft() {
  16410. return this.end - this.state.offset;
  16411. }
  16412. diff(other) {
  16413. return this.state.offset - other.state.offset;
  16414. }
  16415. advance() {
  16416. this.advanceState(this.state);
  16417. }
  16418. init() {
  16419. this.updatePeek(this.state);
  16420. }
  16421. getSpan(start, leadingTriviaCodePoints) {
  16422. start = start || this;
  16423. let fullStart = start;
  16424. if (leadingTriviaCodePoints) {
  16425. while (this.diff(start) > 0 && leadingTriviaCodePoints.indexOf(start.peek()) !== -1) {
  16426. if (fullStart === start) {
  16427. start = start.clone();
  16428. }
  16429. start.advance();
  16430. }
  16431. }
  16432. const startLocation = this.locationFromCursor(start);
  16433. const endLocation = this.locationFromCursor(this);
  16434. const fullStartLocation = fullStart !== start ? this.locationFromCursor(fullStart) : startLocation;
  16435. return new ParseSourceSpan(startLocation, endLocation, fullStartLocation);
  16436. }
  16437. getChars(start) {
  16438. return this.input.substring(start.state.offset, this.state.offset);
  16439. }
  16440. charAt(pos) {
  16441. return this.input.charCodeAt(pos);
  16442. }
  16443. advanceState(state) {
  16444. if (state.offset >= this.end) {
  16445. this.state = state;
  16446. throw new CursorError('Unexpected character "EOF"', this);
  16447. }
  16448. const currentChar = this.charAt(state.offset);
  16449. if (currentChar === $LF) {
  16450. state.line++;
  16451. state.column = 0;
  16452. }
  16453. else if (!isNewLine(currentChar)) {
  16454. state.column++;
  16455. }
  16456. state.offset++;
  16457. this.updatePeek(state);
  16458. }
  16459. updatePeek(state) {
  16460. state.peek = state.offset >= this.end ? $EOF : this.charAt(state.offset);
  16461. }
  16462. locationFromCursor(cursor) {
  16463. return new ParseLocation(cursor.file, cursor.state.offset, cursor.state.line, cursor.state.column);
  16464. }
  16465. }
  16466. class EscapedCharacterCursor extends PlainCharacterCursor {
  16467. internalState;
  16468. constructor(fileOrCursor, range) {
  16469. if (fileOrCursor instanceof EscapedCharacterCursor) {
  16470. super(fileOrCursor);
  16471. this.internalState = { ...fileOrCursor.internalState };
  16472. }
  16473. else {
  16474. super(fileOrCursor, range);
  16475. this.internalState = this.state;
  16476. }
  16477. }
  16478. advance() {
  16479. this.state = this.internalState;
  16480. super.advance();
  16481. this.processEscapeSequence();
  16482. }
  16483. init() {
  16484. super.init();
  16485. this.processEscapeSequence();
  16486. }
  16487. clone() {
  16488. return new EscapedCharacterCursor(this);
  16489. }
  16490. getChars(start) {
  16491. const cursor = start.clone();
  16492. let chars = '';
  16493. while (cursor.internalState.offset < this.internalState.offset) {
  16494. chars += String.fromCodePoint(cursor.peek());
  16495. cursor.advance();
  16496. }
  16497. return chars;
  16498. }
  16499. /**
  16500. * Process the escape sequence that starts at the current position in the text.
  16501. *
  16502. * This method is called to ensure that `peek` has the unescaped value of escape sequences.
  16503. */
  16504. processEscapeSequence() {
  16505. const peek = () => this.internalState.peek;
  16506. if (peek() === $BACKSLASH) {
  16507. // We have hit an escape sequence so we need the internal state to become independent
  16508. // of the external state.
  16509. this.internalState = { ...this.state };
  16510. // Move past the backslash
  16511. this.advanceState(this.internalState);
  16512. // First check for standard control char sequences
  16513. if (peek() === $n) {
  16514. this.state.peek = $LF;
  16515. }
  16516. else if (peek() === $r) {
  16517. this.state.peek = $CR;
  16518. }
  16519. else if (peek() === $v) {
  16520. this.state.peek = $VTAB;
  16521. }
  16522. else if (peek() === $t) {
  16523. this.state.peek = $TAB;
  16524. }
  16525. else if (peek() === $b) {
  16526. this.state.peek = $BSPACE;
  16527. }
  16528. else if (peek() === $f) {
  16529. this.state.peek = $FF;
  16530. }
  16531. // Now consider more complex sequences
  16532. else if (peek() === $u) {
  16533. // Unicode code-point sequence
  16534. this.advanceState(this.internalState); // advance past the `u` char
  16535. if (peek() === $LBRACE) {
  16536. // Variable length Unicode, e.g. `\x{123}`
  16537. this.advanceState(this.internalState); // advance past the `{` char
  16538. // Advance past the variable number of hex digits until we hit a `}` char
  16539. const digitStart = this.clone();
  16540. let length = 0;
  16541. while (peek() !== $RBRACE) {
  16542. this.advanceState(this.internalState);
  16543. length++;
  16544. }
  16545. this.state.peek = this.decodeHexDigits(digitStart, length);
  16546. }
  16547. else {
  16548. // Fixed length Unicode, e.g. `\u1234`
  16549. const digitStart = this.clone();
  16550. this.advanceState(this.internalState);
  16551. this.advanceState(this.internalState);
  16552. this.advanceState(this.internalState);
  16553. this.state.peek = this.decodeHexDigits(digitStart, 4);
  16554. }
  16555. }
  16556. else if (peek() === $x) {
  16557. // Hex char code, e.g. `\x2F`
  16558. this.advanceState(this.internalState); // advance past the `x` char
  16559. const digitStart = this.clone();
  16560. this.advanceState(this.internalState);
  16561. this.state.peek = this.decodeHexDigits(digitStart, 2);
  16562. }
  16563. else if (isOctalDigit(peek())) {
  16564. // Octal char code, e.g. `\012`,
  16565. let octal = '';
  16566. let length = 0;
  16567. let previous = this.clone();
  16568. while (isOctalDigit(peek()) && length < 3) {
  16569. previous = this.clone();
  16570. octal += String.fromCodePoint(peek());
  16571. this.advanceState(this.internalState);
  16572. length++;
  16573. }
  16574. this.state.peek = parseInt(octal, 8);
  16575. // Backup one char
  16576. this.internalState = previous.internalState;
  16577. }
  16578. else if (isNewLine(this.internalState.peek)) {
  16579. // Line continuation `\` followed by a new line
  16580. this.advanceState(this.internalState); // advance over the newline
  16581. this.state = this.internalState;
  16582. }
  16583. else {
  16584. // If none of the `if` blocks were executed then we just have an escaped normal character.
  16585. // In that case we just, effectively, skip the backslash from the character.
  16586. this.state.peek = this.internalState.peek;
  16587. }
  16588. }
  16589. }
  16590. decodeHexDigits(start, length) {
  16591. const hex = this.input.slice(start.internalState.offset, start.internalState.offset + length);
  16592. const charCode = parseInt(hex, 16);
  16593. if (!isNaN(charCode)) {
  16594. return charCode;
  16595. }
  16596. else {
  16597. start.state = start.internalState;
  16598. throw new CursorError('Invalid hexadecimal escape sequence', start);
  16599. }
  16600. }
  16601. }
  16602. class CursorError {
  16603. msg;
  16604. cursor;
  16605. constructor(msg, cursor) {
  16606. this.msg = msg;
  16607. this.cursor = cursor;
  16608. }
  16609. }
  16610. class TreeError extends ParseError {
  16611. elementName;
  16612. static create(elementName, span, msg) {
  16613. return new TreeError(elementName, span, msg);
  16614. }
  16615. constructor(elementName, span, msg) {
  16616. super(span, msg);
  16617. this.elementName = elementName;
  16618. }
  16619. }
  16620. class ParseTreeResult {
  16621. rootNodes;
  16622. errors;
  16623. constructor(rootNodes, errors) {
  16624. this.rootNodes = rootNodes;
  16625. this.errors = errors;
  16626. }
  16627. }
  16628. let Parser$1 = class Parser {
  16629. getTagDefinition;
  16630. constructor(getTagDefinition) {
  16631. this.getTagDefinition = getTagDefinition;
  16632. }
  16633. parse(source, url, options) {
  16634. const tokenizeResult = tokenize(source, url, this.getTagDefinition, options);
  16635. const parser = new _TreeBuilder(tokenizeResult.tokens, this.getTagDefinition);
  16636. parser.build();
  16637. return new ParseTreeResult(parser.rootNodes, tokenizeResult.errors.concat(parser.errors));
  16638. }
  16639. };
  16640. class _TreeBuilder {
  16641. tokens;
  16642. getTagDefinition;
  16643. _index = -1;
  16644. // `_peek` will be initialized by the call to `_advance()` in the constructor.
  16645. _peek;
  16646. _containerStack = [];
  16647. rootNodes = [];
  16648. errors = [];
  16649. constructor(tokens, getTagDefinition) {
  16650. this.tokens = tokens;
  16651. this.getTagDefinition = getTagDefinition;
  16652. this._advance();
  16653. }
  16654. build() {
  16655. while (this._peek.type !== 33 /* TokenType.EOF */) {
  16656. if (this._peek.type === 0 /* TokenType.TAG_OPEN_START */ ||
  16657. this._peek.type === 4 /* TokenType.INCOMPLETE_TAG_OPEN */) {
  16658. this._consumeStartTag(this._advance());
  16659. }
  16660. else if (this._peek.type === 3 /* TokenType.TAG_CLOSE */) {
  16661. this._consumeEndTag(this._advance());
  16662. }
  16663. else if (this._peek.type === 12 /* TokenType.CDATA_START */) {
  16664. this._closeVoidElement();
  16665. this._consumeCdata(this._advance());
  16666. }
  16667. else if (this._peek.type === 10 /* TokenType.COMMENT_START */) {
  16668. this._closeVoidElement();
  16669. this._consumeComment(this._advance());
  16670. }
  16671. else if (this._peek.type === 5 /* TokenType.TEXT */ ||
  16672. this._peek.type === 7 /* TokenType.RAW_TEXT */ ||
  16673. this._peek.type === 6 /* TokenType.ESCAPABLE_RAW_TEXT */) {
  16674. this._closeVoidElement();
  16675. this._consumeText(this._advance());
  16676. }
  16677. else if (this._peek.type === 19 /* TokenType.EXPANSION_FORM_START */) {
  16678. this._consumeExpansion(this._advance());
  16679. }
  16680. else if (this._peek.type === 24 /* TokenType.BLOCK_OPEN_START */) {
  16681. this._closeVoidElement();
  16682. this._consumeBlockOpen(this._advance());
  16683. }
  16684. else if (this._peek.type === 26 /* TokenType.BLOCK_CLOSE */) {
  16685. this._closeVoidElement();
  16686. this._consumeBlockClose(this._advance());
  16687. }
  16688. else if (this._peek.type === 28 /* TokenType.INCOMPLETE_BLOCK_OPEN */) {
  16689. this._closeVoidElement();
  16690. this._consumeIncompleteBlock(this._advance());
  16691. }
  16692. else if (this._peek.type === 29 /* TokenType.LET_START */) {
  16693. this._closeVoidElement();
  16694. this._consumeLet(this._advance());
  16695. }
  16696. else if (this._peek.type === 32 /* TokenType.INCOMPLETE_LET */) {
  16697. this._closeVoidElement();
  16698. this._consumeIncompleteLet(this._advance());
  16699. }
  16700. else {
  16701. // Skip all other tokens...
  16702. this._advance();
  16703. }
  16704. }
  16705. for (const leftoverContainer of this._containerStack) {
  16706. // Unlike HTML elements, blocks aren't closed implicitly by the end of the file.
  16707. if (leftoverContainer instanceof Block) {
  16708. this.errors.push(TreeError.create(leftoverContainer.name, leftoverContainer.sourceSpan, `Unclosed block "${leftoverContainer.name}"`));
  16709. }
  16710. }
  16711. }
  16712. _advance() {
  16713. const prev = this._peek;
  16714. if (this._index < this.tokens.length - 1) {
  16715. // Note: there is always an EOF token at the end
  16716. this._index++;
  16717. }
  16718. this._peek = this.tokens[this._index];
  16719. return prev;
  16720. }
  16721. _advanceIf(type) {
  16722. if (this._peek.type === type) {
  16723. return this._advance();
  16724. }
  16725. return null;
  16726. }
  16727. _consumeCdata(_startToken) {
  16728. this._consumeText(this._advance());
  16729. this._advanceIf(13 /* TokenType.CDATA_END */);
  16730. }
  16731. _consumeComment(token) {
  16732. const text = this._advanceIf(7 /* TokenType.RAW_TEXT */);
  16733. const endToken = this._advanceIf(11 /* TokenType.COMMENT_END */);
  16734. const value = text != null ? text.parts[0].trim() : null;
  16735. const sourceSpan = endToken == null
  16736. ? token.sourceSpan
  16737. : new ParseSourceSpan(token.sourceSpan.start, endToken.sourceSpan.end, token.sourceSpan.fullStart);
  16738. this._addToParent(new Comment(value, sourceSpan));
  16739. }
  16740. _consumeExpansion(token) {
  16741. const switchValue = this._advance();
  16742. const type = this._advance();
  16743. const cases = [];
  16744. // read =
  16745. while (this._peek.type === 20 /* TokenType.EXPANSION_CASE_VALUE */) {
  16746. const expCase = this._parseExpansionCase();
  16747. if (!expCase)
  16748. return; // error
  16749. cases.push(expCase);
  16750. }
  16751. // read the final }
  16752. if (this._peek.type !== 23 /* TokenType.EXPANSION_FORM_END */) {
  16753. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16754. return;
  16755. }
  16756. const sourceSpan = new ParseSourceSpan(token.sourceSpan.start, this._peek.sourceSpan.end, token.sourceSpan.fullStart);
  16757. this._addToParent(new Expansion(switchValue.parts[0], type.parts[0], cases, sourceSpan, switchValue.sourceSpan));
  16758. this._advance();
  16759. }
  16760. _parseExpansionCase() {
  16761. const value = this._advance();
  16762. // read {
  16763. if (this._peek.type !== 21 /* TokenType.EXPANSION_CASE_EXP_START */) {
  16764. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '{'.`));
  16765. return null;
  16766. }
  16767. // read until }
  16768. const start = this._advance();
  16769. const exp = this._collectExpansionExpTokens(start);
  16770. if (!exp)
  16771. return null;
  16772. const end = this._advance();
  16773. exp.push({ type: 33 /* TokenType.EOF */, parts: [], sourceSpan: end.sourceSpan });
  16774. // parse everything in between { and }
  16775. const expansionCaseParser = new _TreeBuilder(exp, this.getTagDefinition);
  16776. expansionCaseParser.build();
  16777. if (expansionCaseParser.errors.length > 0) {
  16778. this.errors = this.errors.concat(expansionCaseParser.errors);
  16779. return null;
  16780. }
  16781. const sourceSpan = new ParseSourceSpan(value.sourceSpan.start, end.sourceSpan.end, value.sourceSpan.fullStart);
  16782. const expSourceSpan = new ParseSourceSpan(start.sourceSpan.start, end.sourceSpan.end, start.sourceSpan.fullStart);
  16783. return new ExpansionCase(value.parts[0], expansionCaseParser.rootNodes, sourceSpan, value.sourceSpan, expSourceSpan);
  16784. }
  16785. _collectExpansionExpTokens(start) {
  16786. const exp = [];
  16787. const expansionFormStack = [21 /* TokenType.EXPANSION_CASE_EXP_START */];
  16788. while (true) {
  16789. if (this._peek.type === 19 /* TokenType.EXPANSION_FORM_START */ ||
  16790. this._peek.type === 21 /* TokenType.EXPANSION_CASE_EXP_START */) {
  16791. expansionFormStack.push(this._peek.type);
  16792. }
  16793. if (this._peek.type === 22 /* TokenType.EXPANSION_CASE_EXP_END */) {
  16794. if (lastOnStack(expansionFormStack, 21 /* TokenType.EXPANSION_CASE_EXP_START */)) {
  16795. expansionFormStack.pop();
  16796. if (expansionFormStack.length === 0)
  16797. return exp;
  16798. }
  16799. else {
  16800. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16801. return null;
  16802. }
  16803. }
  16804. if (this._peek.type === 23 /* TokenType.EXPANSION_FORM_END */) {
  16805. if (lastOnStack(expansionFormStack, 19 /* TokenType.EXPANSION_FORM_START */)) {
  16806. expansionFormStack.pop();
  16807. }
  16808. else {
  16809. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16810. return null;
  16811. }
  16812. }
  16813. if (this._peek.type === 33 /* TokenType.EOF */) {
  16814. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  16815. return null;
  16816. }
  16817. exp.push(this._advance());
  16818. }
  16819. }
  16820. _consumeText(token) {
  16821. const tokens = [token];
  16822. const startSpan = token.sourceSpan;
  16823. let text = token.parts[0];
  16824. if (text.length > 0 && text[0] === '\n') {
  16825. const parent = this._getContainer();
  16826. if (parent != null &&
  16827. parent.children.length === 0 &&
  16828. this.getTagDefinition(parent.name).ignoreFirstLf) {
  16829. text = text.substring(1);
  16830. tokens[0] = { type: token.type, sourceSpan: token.sourceSpan, parts: [text] };
  16831. }
  16832. }
  16833. while (this._peek.type === 8 /* TokenType.INTERPOLATION */ ||
  16834. this._peek.type === 5 /* TokenType.TEXT */ ||
  16835. this._peek.type === 9 /* TokenType.ENCODED_ENTITY */) {
  16836. token = this._advance();
  16837. tokens.push(token);
  16838. if (token.type === 8 /* TokenType.INTERPOLATION */) {
  16839. // For backward compatibility we decode HTML entities that appear in interpolation
  16840. // expressions. This is arguably a bug, but it could be a considerable breaking change to
  16841. // fix it. It should be addressed in a larger project to refactor the entire parser/lexer
  16842. // chain after View Engine has been removed.
  16843. text += token.parts.join('').replace(/&([^;]+);/g, decodeEntity);
  16844. }
  16845. else if (token.type === 9 /* TokenType.ENCODED_ENTITY */) {
  16846. text += token.parts[0];
  16847. }
  16848. else {
  16849. text += token.parts.join('');
  16850. }
  16851. }
  16852. if (text.length > 0) {
  16853. const endSpan = token.sourceSpan;
  16854. this._addToParent(new Text(text, new ParseSourceSpan(startSpan.start, endSpan.end, startSpan.fullStart, startSpan.details), tokens));
  16855. }
  16856. }
  16857. _closeVoidElement() {
  16858. const el = this._getContainer();
  16859. if (el instanceof Element && this.getTagDefinition(el.name).isVoid) {
  16860. this._containerStack.pop();
  16861. }
  16862. }
  16863. _consumeStartTag(startTagToken) {
  16864. const [prefix, name] = startTagToken.parts;
  16865. const attrs = [];
  16866. while (this._peek.type === 14 /* TokenType.ATTR_NAME */) {
  16867. attrs.push(this._consumeAttr(this._advance()));
  16868. }
  16869. const fullName = this._getElementFullName(prefix, name, this._getClosestParentElement());
  16870. let selfClosing = false;
  16871. // Note: There could have been a tokenizer error
  16872. // so that we don't get a token for the end tag...
  16873. if (this._peek.type === 2 /* TokenType.TAG_OPEN_END_VOID */) {
  16874. this._advance();
  16875. selfClosing = true;
  16876. const tagDef = this.getTagDefinition(fullName);
  16877. if (!(tagDef.canSelfClose || getNsPrefix(fullName) !== null || tagDef.isVoid)) {
  16878. this.errors.push(TreeError.create(fullName, startTagToken.sourceSpan, `Only void, custom and foreign elements can be self closed "${startTagToken.parts[1]}"`));
  16879. }
  16880. }
  16881. else if (this._peek.type === 1 /* TokenType.TAG_OPEN_END */) {
  16882. this._advance();
  16883. selfClosing = false;
  16884. }
  16885. const end = this._peek.sourceSpan.fullStart;
  16886. const span = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  16887. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  16888. const startSpan = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  16889. const el = new Element(fullName, attrs, [], span, startSpan, undefined);
  16890. const parentEl = this._getContainer();
  16891. this._pushContainer(el, parentEl instanceof Element &&
  16892. this.getTagDefinition(parentEl.name).isClosedByChild(el.name));
  16893. if (selfClosing) {
  16894. // Elements that are self-closed have their `endSourceSpan` set to the full span, as the
  16895. // element start tag also represents the end tag.
  16896. this._popContainer(fullName, Element, span);
  16897. }
  16898. else if (startTagToken.type === 4 /* TokenType.INCOMPLETE_TAG_OPEN */) {
  16899. // We already know the opening tag is not complete, so it is unlikely it has a corresponding
  16900. // close tag. Let's optimistically parse it as a full element and emit an error.
  16901. this._popContainer(fullName, Element, null);
  16902. this.errors.push(TreeError.create(fullName, span, `Opening tag "${fullName}" not terminated.`));
  16903. }
  16904. }
  16905. _pushContainer(node, isClosedByChild) {
  16906. if (isClosedByChild) {
  16907. this._containerStack.pop();
  16908. }
  16909. this._addToParent(node);
  16910. this._containerStack.push(node);
  16911. }
  16912. _consumeEndTag(endTagToken) {
  16913. const fullName = this._getElementFullName(endTagToken.parts[0], endTagToken.parts[1], this._getClosestParentElement());
  16914. if (this.getTagDefinition(fullName).isVoid) {
  16915. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, `Void elements do not have end tags "${endTagToken.parts[1]}"`));
  16916. }
  16917. else if (!this._popContainer(fullName, Element, endTagToken.sourceSpan)) {
  16918. const errMsg = `Unexpected closing tag "${fullName}". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags`;
  16919. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, errMsg));
  16920. }
  16921. }
  16922. /**
  16923. * Closes the nearest element with the tag name `fullName` in the parse tree.
  16924. * `endSourceSpan` is the span of the closing tag, or null if the element does
  16925. * not have a closing tag (for example, this happens when an incomplete
  16926. * opening tag is recovered).
  16927. */
  16928. _popContainer(expectedName, expectedType, endSourceSpan) {
  16929. let unexpectedCloseTagDetected = false;
  16930. for (let stackIndex = this._containerStack.length - 1; stackIndex >= 0; stackIndex--) {
  16931. const node = this._containerStack[stackIndex];
  16932. if ((node.name === expectedName || expectedName === null) && node instanceof expectedType) {
  16933. // Record the parse span with the element that is being closed. Any elements that are
  16934. // removed from the element stack at this point are closed implicitly, so they won't get
  16935. // an end source span (as there is no explicit closing element).
  16936. node.endSourceSpan = endSourceSpan;
  16937. node.sourceSpan.end = endSourceSpan !== null ? endSourceSpan.end : node.sourceSpan.end;
  16938. this._containerStack.splice(stackIndex, this._containerStack.length - stackIndex);
  16939. return !unexpectedCloseTagDetected;
  16940. }
  16941. // Blocks and most elements are not self closing.
  16942. if (node instanceof Block ||
  16943. (node instanceof Element && !this.getTagDefinition(node.name).closedByParent)) {
  16944. // Note that we encountered an unexpected close tag but continue processing the element
  16945. // stack so we can assign an `endSourceSpan` if there is a corresponding start tag for this
  16946. // end tag in the stack.
  16947. unexpectedCloseTagDetected = true;
  16948. }
  16949. }
  16950. return false;
  16951. }
  16952. _consumeAttr(attrName) {
  16953. const fullName = mergeNsAndName(attrName.parts[0], attrName.parts[1]);
  16954. let attrEnd = attrName.sourceSpan.end;
  16955. // Consume any quote
  16956. if (this._peek.type === 15 /* TokenType.ATTR_QUOTE */) {
  16957. this._advance();
  16958. }
  16959. // Consume the attribute value
  16960. let value = '';
  16961. const valueTokens = [];
  16962. let valueStartSpan = undefined;
  16963. let valueEnd = undefined;
  16964. // NOTE: We need to use a new variable `nextTokenType` here to hide the actual type of
  16965. // `_peek.type` from TS. Otherwise TS will narrow the type of `_peek.type` preventing it from
  16966. // being able to consider `ATTR_VALUE_INTERPOLATION` as an option. This is because TS is not
  16967. // able to see that `_advance()` will actually mutate `_peek`.
  16968. const nextTokenType = this._peek.type;
  16969. if (nextTokenType === 16 /* TokenType.ATTR_VALUE_TEXT */) {
  16970. valueStartSpan = this._peek.sourceSpan;
  16971. valueEnd = this._peek.sourceSpan.end;
  16972. while (this._peek.type === 16 /* TokenType.ATTR_VALUE_TEXT */ ||
  16973. this._peek.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */ ||
  16974. this._peek.type === 9 /* TokenType.ENCODED_ENTITY */) {
  16975. const valueToken = this._advance();
  16976. valueTokens.push(valueToken);
  16977. if (valueToken.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */) {
  16978. // For backward compatibility we decode HTML entities that appear in interpolation
  16979. // expressions. This is arguably a bug, but it could be a considerable breaking change to
  16980. // fix it. It should be addressed in a larger project to refactor the entire parser/lexer
  16981. // chain after View Engine has been removed.
  16982. value += valueToken.parts.join('').replace(/&([^;]+);/g, decodeEntity);
  16983. }
  16984. else if (valueToken.type === 9 /* TokenType.ENCODED_ENTITY */) {
  16985. value += valueToken.parts[0];
  16986. }
  16987. else {
  16988. value += valueToken.parts.join('');
  16989. }
  16990. valueEnd = attrEnd = valueToken.sourceSpan.end;
  16991. }
  16992. }
  16993. // Consume any quote
  16994. if (this._peek.type === 15 /* TokenType.ATTR_QUOTE */) {
  16995. const quoteToken = this._advance();
  16996. attrEnd = quoteToken.sourceSpan.end;
  16997. }
  16998. const valueSpan = valueStartSpan &&
  16999. valueEnd &&
  17000. new ParseSourceSpan(valueStartSpan.start, valueEnd, valueStartSpan.fullStart);
  17001. return new Attribute(fullName, value, new ParseSourceSpan(attrName.sourceSpan.start, attrEnd, attrName.sourceSpan.fullStart), attrName.sourceSpan, valueSpan, valueTokens.length > 0 ? valueTokens : undefined, undefined);
  17002. }
  17003. _consumeBlockOpen(token) {
  17004. const parameters = [];
  17005. while (this._peek.type === 27 /* TokenType.BLOCK_PARAMETER */) {
  17006. const paramToken = this._advance();
  17007. parameters.push(new BlockParameter(paramToken.parts[0], paramToken.sourceSpan));
  17008. }
  17009. if (this._peek.type === 25 /* TokenType.BLOCK_OPEN_END */) {
  17010. this._advance();
  17011. }
  17012. const end = this._peek.sourceSpan.fullStart;
  17013. const span = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17014. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  17015. const startSpan = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17016. const block = new Block(token.parts[0], parameters, [], span, token.sourceSpan, startSpan);
  17017. this._pushContainer(block, false);
  17018. }
  17019. _consumeBlockClose(token) {
  17020. if (!this._popContainer(null, Block, token.sourceSpan)) {
  17021. this.errors.push(TreeError.create(null, token.sourceSpan, `Unexpected closing block. The block may have been closed earlier. ` +
  17022. `If you meant to write the } character, you should use the "&#125;" ` +
  17023. `HTML entity instead.`));
  17024. }
  17025. }
  17026. _consumeIncompleteBlock(token) {
  17027. const parameters = [];
  17028. while (this._peek.type === 27 /* TokenType.BLOCK_PARAMETER */) {
  17029. const paramToken = this._advance();
  17030. parameters.push(new BlockParameter(paramToken.parts[0], paramToken.sourceSpan));
  17031. }
  17032. const end = this._peek.sourceSpan.fullStart;
  17033. const span = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17034. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  17035. const startSpan = new ParseSourceSpan(token.sourceSpan.start, end, token.sourceSpan.fullStart);
  17036. const block = new Block(token.parts[0], parameters, [], span, token.sourceSpan, startSpan);
  17037. this._pushContainer(block, false);
  17038. // Incomplete blocks don't have children so we close them immediately and report an error.
  17039. this._popContainer(null, Block, null);
  17040. this.errors.push(TreeError.create(token.parts[0], span, `Incomplete block "${token.parts[0]}". If you meant to write the @ character, ` +
  17041. `you should use the "&#64;" HTML entity instead.`));
  17042. }
  17043. _consumeLet(startToken) {
  17044. const name = startToken.parts[0];
  17045. let valueToken;
  17046. let endToken;
  17047. if (this._peek.type !== 30 /* TokenType.LET_VALUE */) {
  17048. this.errors.push(TreeError.create(startToken.parts[0], startToken.sourceSpan, `Invalid @let declaration "${name}". Declaration must have a value.`));
  17049. return;
  17050. }
  17051. else {
  17052. valueToken = this._advance();
  17053. }
  17054. // Type cast is necessary here since TS narrowed the type of `peek` above.
  17055. if (this._peek.type !== 31 /* TokenType.LET_END */) {
  17056. this.errors.push(TreeError.create(startToken.parts[0], startToken.sourceSpan, `Unterminated @let declaration "${name}". Declaration must be terminated with a semicolon.`));
  17057. return;
  17058. }
  17059. else {
  17060. endToken = this._advance();
  17061. }
  17062. const end = endToken.sourceSpan.fullStart;
  17063. const span = new ParseSourceSpan(startToken.sourceSpan.start, end, startToken.sourceSpan.fullStart);
  17064. // The start token usually captures the `@let`. Construct a name span by
  17065. // offsetting the start by the length of any text before the name.
  17066. const startOffset = startToken.sourceSpan.toString().lastIndexOf(name);
  17067. const nameStart = startToken.sourceSpan.start.moveBy(startOffset);
  17068. const nameSpan = new ParseSourceSpan(nameStart, startToken.sourceSpan.end);
  17069. const node = new LetDeclaration(name, valueToken.parts[0], span, nameSpan, valueToken.sourceSpan);
  17070. this._addToParent(node);
  17071. }
  17072. _consumeIncompleteLet(token) {
  17073. // Incomplete `@let` declaration may end up with an empty name.
  17074. const name = token.parts[0] ?? '';
  17075. const nameString = name ? ` "${name}"` : '';
  17076. // If there's at least a name, we can salvage an AST node that can be used for completions.
  17077. if (name.length > 0) {
  17078. const startOffset = token.sourceSpan.toString().lastIndexOf(name);
  17079. const nameStart = token.sourceSpan.start.moveBy(startOffset);
  17080. const nameSpan = new ParseSourceSpan(nameStart, token.sourceSpan.end);
  17081. const valueSpan = new ParseSourceSpan(token.sourceSpan.start, token.sourceSpan.start.moveBy(0));
  17082. const node = new LetDeclaration(name, '', token.sourceSpan, nameSpan, valueSpan);
  17083. this._addToParent(node);
  17084. }
  17085. this.errors.push(TreeError.create(token.parts[0], token.sourceSpan, `Incomplete @let declaration${nameString}. ` +
  17086. `@let declarations must be written as \`@let <name> = <value>;\``));
  17087. }
  17088. _getContainer() {
  17089. return this._containerStack.length > 0
  17090. ? this._containerStack[this._containerStack.length - 1]
  17091. : null;
  17092. }
  17093. _getClosestParentElement() {
  17094. for (let i = this._containerStack.length - 1; i > -1; i--) {
  17095. if (this._containerStack[i] instanceof Element) {
  17096. return this._containerStack[i];
  17097. }
  17098. }
  17099. return null;
  17100. }
  17101. _addToParent(node) {
  17102. const parent = this._getContainer();
  17103. if (parent === null) {
  17104. this.rootNodes.push(node);
  17105. }
  17106. else {
  17107. parent.children.push(node);
  17108. }
  17109. }
  17110. _getElementFullName(prefix, localName, parentElement) {
  17111. if (prefix === '') {
  17112. prefix = this.getTagDefinition(localName).implicitNamespacePrefix || '';
  17113. if (prefix === '' && parentElement != null) {
  17114. const parentTagName = splitNsName(parentElement.name)[1];
  17115. const parentTagDefinition = this.getTagDefinition(parentTagName);
  17116. if (!parentTagDefinition.preventNamespaceInheritance) {
  17117. prefix = getNsPrefix(parentElement.name);
  17118. }
  17119. }
  17120. }
  17121. return mergeNsAndName(prefix, localName);
  17122. }
  17123. }
  17124. function lastOnStack(stack, element) {
  17125. return stack.length > 0 && stack[stack.length - 1] === element;
  17126. }
  17127. /**
  17128. * Decode the `entity` string, which we believe is the contents of an HTML entity.
  17129. *
  17130. * If the string is not actually a valid/known entity then just return the original `match` string.
  17131. */
  17132. function decodeEntity(match, entity) {
  17133. if (NAMED_ENTITIES[entity] !== undefined) {
  17134. return NAMED_ENTITIES[entity] || match;
  17135. }
  17136. if (/^#x[a-f0-9]+$/i.test(entity)) {
  17137. return String.fromCodePoint(parseInt(entity.slice(2), 16));
  17138. }
  17139. if (/^#\d+$/.test(entity)) {
  17140. return String.fromCodePoint(parseInt(entity.slice(1), 10));
  17141. }
  17142. return match;
  17143. }
  17144. const PRESERVE_WS_ATTR_NAME = 'ngPreserveWhitespaces';
  17145. const SKIP_WS_TRIM_TAGS = new Set(['pre', 'template', 'textarea', 'script', 'style']);
  17146. // Equivalent to \s with \u00a0 (non-breaking space) excluded.
  17147. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
  17148. const WS_CHARS = ' \f\n\r\t\v\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff';
  17149. const NO_WS_REGEXP = new RegExp(`[^${WS_CHARS}]`);
  17150. const WS_REPLACE_REGEXP = new RegExp(`[${WS_CHARS}]{2,}`, 'g');
  17151. function hasPreserveWhitespacesAttr(attrs) {
  17152. return attrs.some((attr) => attr.name === PRESERVE_WS_ATTR_NAME);
  17153. }
  17154. /**
  17155. * &ngsp; is a placeholder for non-removable space
  17156. * &ngsp; is converted to the 0xE500 PUA (Private Use Areas) unicode character
  17157. * and later on replaced by a space.
  17158. */
  17159. function replaceNgsp(value) {
  17160. // lexer is replacing the &ngsp; pseudo-entity with NGSP_UNICODE
  17161. return value.replace(new RegExp(NGSP_UNICODE, 'g'), ' ');
  17162. }
  17163. /**
  17164. * This visitor can walk HTML parse tree and remove / trim text nodes using the following rules:
  17165. * - consider spaces, tabs and new lines as whitespace characters;
  17166. * - drop text nodes consisting of whitespace characters only;
  17167. * - for all other text nodes replace consecutive whitespace characters with one space;
  17168. * - convert &ngsp; pseudo-entity to a single space;
  17169. *
  17170. * Removal and trimming of whitespaces have positive performance impact (less code to generate
  17171. * while compiling templates, faster view creation). At the same time it can be "destructive"
  17172. * in some cases (whitespaces can influence layout). Because of the potential of breaking layout
  17173. * this visitor is not activated by default in Angular 5 and people need to explicitly opt-in for
  17174. * whitespace removal. The default option for whitespace removal will be revisited in Angular 6
  17175. * and might be changed to "on" by default.
  17176. *
  17177. * If `originalNodeMap` is provided, the transformed nodes will be mapped back to their original
  17178. * inputs. Any output nodes not in the map were not transformed. This supports correlating and
  17179. * porting information between the trimmed nodes and original nodes (such as `i18n` properties)
  17180. * such that trimming whitespace does not does not drop required information from the node.
  17181. */
  17182. class WhitespaceVisitor {
  17183. preserveSignificantWhitespace;
  17184. originalNodeMap;
  17185. requireContext;
  17186. // How many ICU expansions which are currently being visited. ICUs can be nested, so this
  17187. // tracks the current depth of nesting. If this depth is greater than 0, then this visitor is
  17188. // currently processing content inside an ICU expansion.
  17189. icuExpansionDepth = 0;
  17190. constructor(preserveSignificantWhitespace, originalNodeMap, requireContext = true) {
  17191. this.preserveSignificantWhitespace = preserveSignificantWhitespace;
  17192. this.originalNodeMap = originalNodeMap;
  17193. this.requireContext = requireContext;
  17194. }
  17195. visitElement(element, context) {
  17196. if (SKIP_WS_TRIM_TAGS.has(element.name) || hasPreserveWhitespacesAttr(element.attrs)) {
  17197. // don't descent into elements where we need to preserve whitespaces
  17198. // but still visit all attributes to eliminate one used as a market to preserve WS
  17199. const newElement = new Element(element.name, visitAllWithSiblings(this, element.attrs), element.children, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  17200. this.originalNodeMap?.set(newElement, element);
  17201. return newElement;
  17202. }
  17203. const newElement = new Element(element.name, element.attrs, visitAllWithSiblings(this, element.children), element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  17204. this.originalNodeMap?.set(newElement, element);
  17205. return newElement;
  17206. }
  17207. visitAttribute(attribute, context) {
  17208. return attribute.name !== PRESERVE_WS_ATTR_NAME ? attribute : null;
  17209. }
  17210. visitText(text, context) {
  17211. const isNotBlank = text.value.match(NO_WS_REGEXP);
  17212. const hasExpansionSibling = context && (context.prev instanceof Expansion || context.next instanceof Expansion);
  17213. // Do not trim whitespace within ICU expansions when preserving significant whitespace.
  17214. // Historically, ICU whitespace was never trimmed and this is really a bug. However fixing it
  17215. // would change message IDs which we can't easily do. Instead we only trim ICU whitespace within
  17216. // ICU expansions when not preserving significant whitespace, which is the new behavior where it
  17217. // most matters.
  17218. const inIcuExpansion = this.icuExpansionDepth > 0;
  17219. if (inIcuExpansion && this.preserveSignificantWhitespace)
  17220. return text;
  17221. if (isNotBlank || hasExpansionSibling) {
  17222. // Process the whitespace in the tokens of this Text node
  17223. const tokens = text.tokens.map((token) => token.type === 5 /* TokenType.TEXT */ ? createWhitespaceProcessedTextToken(token) : token);
  17224. // Fully trim message when significant whitespace is not preserved.
  17225. if (!this.preserveSignificantWhitespace && tokens.length > 0) {
  17226. // The first token should only call `.trimStart()` and the last token
  17227. // should only call `.trimEnd()`, but there might be only one token which
  17228. // needs to call both.
  17229. const firstToken = tokens[0];
  17230. tokens.splice(0, 1, trimLeadingWhitespace(firstToken, context));
  17231. const lastToken = tokens[tokens.length - 1]; // Could be the same as the first token.
  17232. tokens.splice(tokens.length - 1, 1, trimTrailingWhitespace(lastToken, context));
  17233. }
  17234. // Process the whitespace of the value of this Text node. Also trim the leading/trailing
  17235. // whitespace when we don't need to preserve significant whitespace.
  17236. const processed = processWhitespace(text.value);
  17237. const value = this.preserveSignificantWhitespace
  17238. ? processed
  17239. : trimLeadingAndTrailingWhitespace(processed, context);
  17240. const result = new Text(value, text.sourceSpan, tokens, text.i18n);
  17241. this.originalNodeMap?.set(result, text);
  17242. return result;
  17243. }
  17244. return null;
  17245. }
  17246. visitComment(comment, context) {
  17247. return comment;
  17248. }
  17249. visitExpansion(expansion, context) {
  17250. this.icuExpansionDepth++;
  17251. let newExpansion;
  17252. try {
  17253. newExpansion = new Expansion(expansion.switchValue, expansion.type, visitAllWithSiblings(this, expansion.cases), expansion.sourceSpan, expansion.switchValueSourceSpan, expansion.i18n);
  17254. }
  17255. finally {
  17256. this.icuExpansionDepth--;
  17257. }
  17258. this.originalNodeMap?.set(newExpansion, expansion);
  17259. return newExpansion;
  17260. }
  17261. visitExpansionCase(expansionCase, context) {
  17262. const newExpansionCase = new ExpansionCase(expansionCase.value, visitAllWithSiblings(this, expansionCase.expression), expansionCase.sourceSpan, expansionCase.valueSourceSpan, expansionCase.expSourceSpan);
  17263. this.originalNodeMap?.set(newExpansionCase, expansionCase);
  17264. return newExpansionCase;
  17265. }
  17266. visitBlock(block, context) {
  17267. const newBlock = new Block(block.name, block.parameters, visitAllWithSiblings(this, block.children), block.sourceSpan, block.nameSpan, block.startSourceSpan, block.endSourceSpan);
  17268. this.originalNodeMap?.set(newBlock, block);
  17269. return newBlock;
  17270. }
  17271. visitBlockParameter(parameter, context) {
  17272. return parameter;
  17273. }
  17274. visitLetDeclaration(decl, context) {
  17275. return decl;
  17276. }
  17277. visit(_node, context) {
  17278. // `visitAllWithSiblings` provides context necessary for ICU messages to be handled correctly.
  17279. // Prefer that over calling `html.visitAll` directly on this visitor.
  17280. if (this.requireContext && !context) {
  17281. throw new Error(`WhitespaceVisitor requires context. Visit via \`visitAllWithSiblings\` to get this context.`);
  17282. }
  17283. return false;
  17284. }
  17285. }
  17286. function trimLeadingWhitespace(token, context) {
  17287. if (token.type !== 5 /* TokenType.TEXT */)
  17288. return token;
  17289. const isFirstTokenInTag = !context?.prev;
  17290. if (!isFirstTokenInTag)
  17291. return token;
  17292. return transformTextToken(token, (text) => text.trimStart());
  17293. }
  17294. function trimTrailingWhitespace(token, context) {
  17295. if (token.type !== 5 /* TokenType.TEXT */)
  17296. return token;
  17297. const isLastTokenInTag = !context?.next;
  17298. if (!isLastTokenInTag)
  17299. return token;
  17300. return transformTextToken(token, (text) => text.trimEnd());
  17301. }
  17302. function trimLeadingAndTrailingWhitespace(text, context) {
  17303. const isFirstTokenInTag = !context?.prev;
  17304. const isLastTokenInTag = !context?.next;
  17305. const maybeTrimmedStart = isFirstTokenInTag ? text.trimStart() : text;
  17306. const maybeTrimmed = isLastTokenInTag ? maybeTrimmedStart.trimEnd() : maybeTrimmedStart;
  17307. return maybeTrimmed;
  17308. }
  17309. function createWhitespaceProcessedTextToken({ type, parts, sourceSpan }) {
  17310. return { type, parts: [processWhitespace(parts[0])], sourceSpan };
  17311. }
  17312. function transformTextToken({ type, parts, sourceSpan }, transform) {
  17313. // `TextToken` only ever has one part as defined in its type, so we just transform the first element.
  17314. return { type, parts: [transform(parts[0])], sourceSpan };
  17315. }
  17316. function processWhitespace(text) {
  17317. return replaceNgsp(text).replace(WS_REPLACE_REGEXP, ' ');
  17318. }
  17319. function visitAllWithSiblings(visitor, nodes) {
  17320. const result = [];
  17321. nodes.forEach((ast, i) => {
  17322. const context = { prev: nodes[i - 1], next: nodes[i + 1] };
  17323. const astResult = ast.visit(visitor, context);
  17324. if (astResult) {
  17325. result.push(astResult);
  17326. }
  17327. });
  17328. return result;
  17329. }
  17330. var TokenType;
  17331. (function (TokenType) {
  17332. TokenType[TokenType["Character"] = 0] = "Character";
  17333. TokenType[TokenType["Identifier"] = 1] = "Identifier";
  17334. TokenType[TokenType["PrivateIdentifier"] = 2] = "PrivateIdentifier";
  17335. TokenType[TokenType["Keyword"] = 3] = "Keyword";
  17336. TokenType[TokenType["String"] = 4] = "String";
  17337. TokenType[TokenType["Operator"] = 5] = "Operator";
  17338. TokenType[TokenType["Number"] = 6] = "Number";
  17339. TokenType[TokenType["Error"] = 7] = "Error";
  17340. })(TokenType || (TokenType = {}));
  17341. var StringTokenKind;
  17342. (function (StringTokenKind) {
  17343. StringTokenKind[StringTokenKind["Plain"] = 0] = "Plain";
  17344. StringTokenKind[StringTokenKind["TemplateLiteralPart"] = 1] = "TemplateLiteralPart";
  17345. StringTokenKind[StringTokenKind["TemplateLiteralEnd"] = 2] = "TemplateLiteralEnd";
  17346. })(StringTokenKind || (StringTokenKind = {}));
  17347. const KEYWORDS = [
  17348. 'var',
  17349. 'let',
  17350. 'as',
  17351. 'null',
  17352. 'undefined',
  17353. 'true',
  17354. 'false',
  17355. 'if',
  17356. 'else',
  17357. 'this',
  17358. 'typeof',
  17359. ];
  17360. class Lexer {
  17361. tokenize(text) {
  17362. return new _Scanner(text).scan();
  17363. }
  17364. }
  17365. class Token {
  17366. index;
  17367. end;
  17368. type;
  17369. numValue;
  17370. strValue;
  17371. constructor(index, end, type, numValue, strValue) {
  17372. this.index = index;
  17373. this.end = end;
  17374. this.type = type;
  17375. this.numValue = numValue;
  17376. this.strValue = strValue;
  17377. }
  17378. isCharacter(code) {
  17379. return this.type === TokenType.Character && this.numValue === code;
  17380. }
  17381. isNumber() {
  17382. return this.type === TokenType.Number;
  17383. }
  17384. isString() {
  17385. return this.type === TokenType.String;
  17386. }
  17387. isOperator(operator) {
  17388. return this.type === TokenType.Operator && this.strValue === operator;
  17389. }
  17390. isIdentifier() {
  17391. return this.type === TokenType.Identifier;
  17392. }
  17393. isPrivateIdentifier() {
  17394. return this.type === TokenType.PrivateIdentifier;
  17395. }
  17396. isKeyword() {
  17397. return this.type === TokenType.Keyword;
  17398. }
  17399. isKeywordLet() {
  17400. return this.type === TokenType.Keyword && this.strValue === 'let';
  17401. }
  17402. isKeywordAs() {
  17403. return this.type === TokenType.Keyword && this.strValue === 'as';
  17404. }
  17405. isKeywordNull() {
  17406. return this.type === TokenType.Keyword && this.strValue === 'null';
  17407. }
  17408. isKeywordUndefined() {
  17409. return this.type === TokenType.Keyword && this.strValue === 'undefined';
  17410. }
  17411. isKeywordTrue() {
  17412. return this.type === TokenType.Keyword && this.strValue === 'true';
  17413. }
  17414. isKeywordFalse() {
  17415. return this.type === TokenType.Keyword && this.strValue === 'false';
  17416. }
  17417. isKeywordThis() {
  17418. return this.type === TokenType.Keyword && this.strValue === 'this';
  17419. }
  17420. isKeywordTypeof() {
  17421. return this.type === TokenType.Keyword && this.strValue === 'typeof';
  17422. }
  17423. isError() {
  17424. return this.type === TokenType.Error;
  17425. }
  17426. toNumber() {
  17427. return this.type === TokenType.Number ? this.numValue : -1;
  17428. }
  17429. isTemplateLiteralPart() {
  17430. return this.isString() && this.kind === StringTokenKind.TemplateLiteralPart;
  17431. }
  17432. isTemplateLiteralEnd() {
  17433. return this.isString() && this.kind === StringTokenKind.TemplateLiteralEnd;
  17434. }
  17435. isTemplateLiteralInterpolationStart() {
  17436. return this.isOperator('${');
  17437. }
  17438. isTemplateLiteralInterpolationEnd() {
  17439. return this.isOperator('}');
  17440. }
  17441. toString() {
  17442. switch (this.type) {
  17443. case TokenType.Character:
  17444. case TokenType.Identifier:
  17445. case TokenType.Keyword:
  17446. case TokenType.Operator:
  17447. case TokenType.PrivateIdentifier:
  17448. case TokenType.String:
  17449. case TokenType.Error:
  17450. return this.strValue;
  17451. case TokenType.Number:
  17452. return this.numValue.toString();
  17453. default:
  17454. return null;
  17455. }
  17456. }
  17457. }
  17458. class StringToken extends Token {
  17459. kind;
  17460. constructor(index, end, strValue, kind) {
  17461. super(index, end, TokenType.String, 0, strValue);
  17462. this.kind = kind;
  17463. }
  17464. }
  17465. function newCharacterToken(index, end, code) {
  17466. return new Token(index, end, TokenType.Character, code, String.fromCharCode(code));
  17467. }
  17468. function newIdentifierToken(index, end, text) {
  17469. return new Token(index, end, TokenType.Identifier, 0, text);
  17470. }
  17471. function newPrivateIdentifierToken(index, end, text) {
  17472. return new Token(index, end, TokenType.PrivateIdentifier, 0, text);
  17473. }
  17474. function newKeywordToken(index, end, text) {
  17475. return new Token(index, end, TokenType.Keyword, 0, text);
  17476. }
  17477. function newOperatorToken(index, end, text) {
  17478. return new Token(index, end, TokenType.Operator, 0, text);
  17479. }
  17480. function newNumberToken(index, end, n) {
  17481. return new Token(index, end, TokenType.Number, n, '');
  17482. }
  17483. function newErrorToken(index, end, message) {
  17484. return new Token(index, end, TokenType.Error, 0, message);
  17485. }
  17486. const EOF = new Token(-1, -1, TokenType.Character, 0, '');
  17487. class _Scanner {
  17488. input;
  17489. tokens = [];
  17490. length;
  17491. peek = 0;
  17492. index = -1;
  17493. literalInterpolationDepth = 0;
  17494. braceDepth = 0;
  17495. constructor(input) {
  17496. this.input = input;
  17497. this.length = input.length;
  17498. this.advance();
  17499. }
  17500. scan() {
  17501. let token = this.scanToken();
  17502. while (token !== null) {
  17503. this.tokens.push(token);
  17504. token = this.scanToken();
  17505. }
  17506. return this.tokens;
  17507. }
  17508. advance() {
  17509. this.peek = ++this.index >= this.length ? $EOF : this.input.charCodeAt(this.index);
  17510. }
  17511. scanToken() {
  17512. const input = this.input;
  17513. const length = this.length;
  17514. let peek = this.peek;
  17515. let index = this.index;
  17516. // Skip whitespace.
  17517. while (peek <= $SPACE) {
  17518. if (++index >= length) {
  17519. peek = $EOF;
  17520. break;
  17521. }
  17522. else {
  17523. peek = input.charCodeAt(index);
  17524. }
  17525. }
  17526. this.peek = peek;
  17527. this.index = index;
  17528. if (index >= length) {
  17529. return null;
  17530. }
  17531. // Handle identifiers and numbers.
  17532. if (isIdentifierStart(peek)) {
  17533. return this.scanIdentifier();
  17534. }
  17535. if (isDigit(peek)) {
  17536. return this.scanNumber(index);
  17537. }
  17538. const start = index;
  17539. switch (peek) {
  17540. case $PERIOD:
  17541. this.advance();
  17542. return isDigit(this.peek)
  17543. ? this.scanNumber(start)
  17544. : newCharacterToken(start, this.index, $PERIOD);
  17545. case $LPAREN:
  17546. case $RPAREN:
  17547. case $LBRACKET:
  17548. case $RBRACKET:
  17549. case $COMMA:
  17550. case $COLON:
  17551. case $SEMICOLON:
  17552. return this.scanCharacter(start, peek);
  17553. case $LBRACE:
  17554. return this.scanOpenBrace(start, peek);
  17555. case $RBRACE:
  17556. return this.scanCloseBrace(start, peek);
  17557. case $SQ:
  17558. case $DQ:
  17559. return this.scanString();
  17560. case $BT:
  17561. this.advance();
  17562. return this.scanTemplateLiteralPart(start);
  17563. case $HASH:
  17564. return this.scanPrivateIdentifier();
  17565. case $PLUS:
  17566. case $MINUS:
  17567. case $STAR:
  17568. case $SLASH:
  17569. case $PERCENT:
  17570. case $CARET:
  17571. return this.scanOperator(start, String.fromCharCode(peek));
  17572. case $QUESTION:
  17573. return this.scanQuestion(start);
  17574. case $LT:
  17575. case $GT:
  17576. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=');
  17577. case $BANG:
  17578. case $EQ:
  17579. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=', $EQ, '=');
  17580. case $AMPERSAND:
  17581. return this.scanComplexOperator(start, '&', $AMPERSAND, '&');
  17582. case $BAR:
  17583. return this.scanComplexOperator(start, '|', $BAR, '|');
  17584. case $NBSP:
  17585. while (isWhitespace(this.peek))
  17586. this.advance();
  17587. return this.scanToken();
  17588. }
  17589. this.advance();
  17590. return this.error(`Unexpected character [${String.fromCharCode(peek)}]`, 0);
  17591. }
  17592. scanCharacter(start, code) {
  17593. this.advance();
  17594. return newCharacterToken(start, this.index, code);
  17595. }
  17596. scanOperator(start, str) {
  17597. this.advance();
  17598. return newOperatorToken(start, this.index, str);
  17599. }
  17600. scanOpenBrace(start, code) {
  17601. this.braceDepth++;
  17602. this.advance();
  17603. return newCharacterToken(start, this.index, code);
  17604. }
  17605. scanCloseBrace(start, code) {
  17606. this.advance();
  17607. if (this.braceDepth === 0 && this.literalInterpolationDepth > 0) {
  17608. this.literalInterpolationDepth--;
  17609. this.tokens.push(newOperatorToken(start, this.index, '}'));
  17610. return this.scanTemplateLiteralPart(this.index);
  17611. }
  17612. this.braceDepth--;
  17613. return newCharacterToken(start, this.index, code);
  17614. }
  17615. /**
  17616. * Tokenize a 2/3 char long operator
  17617. *
  17618. * @param start start index in the expression
  17619. * @param one first symbol (always part of the operator)
  17620. * @param twoCode code point for the second symbol
  17621. * @param two second symbol (part of the operator when the second code point matches)
  17622. * @param threeCode code point for the third symbol
  17623. * @param three third symbol (part of the operator when provided and matches source expression)
  17624. */
  17625. scanComplexOperator(start, one, twoCode, two, threeCode, three) {
  17626. this.advance();
  17627. let str = one;
  17628. if (this.peek == twoCode) {
  17629. this.advance();
  17630. str += two;
  17631. }
  17632. if (threeCode != null && this.peek == threeCode) {
  17633. this.advance();
  17634. str += three;
  17635. }
  17636. return newOperatorToken(start, this.index, str);
  17637. }
  17638. scanIdentifier() {
  17639. const start = this.index;
  17640. this.advance();
  17641. while (isIdentifierPart(this.peek))
  17642. this.advance();
  17643. const str = this.input.substring(start, this.index);
  17644. return KEYWORDS.indexOf(str) > -1
  17645. ? newKeywordToken(start, this.index, str)
  17646. : newIdentifierToken(start, this.index, str);
  17647. }
  17648. /** Scans an ECMAScript private identifier. */
  17649. scanPrivateIdentifier() {
  17650. const start = this.index;
  17651. this.advance();
  17652. if (!isIdentifierStart(this.peek)) {
  17653. return this.error('Invalid character [#]', -1);
  17654. }
  17655. while (isIdentifierPart(this.peek))
  17656. this.advance();
  17657. const identifierName = this.input.substring(start, this.index);
  17658. return newPrivateIdentifierToken(start, this.index, identifierName);
  17659. }
  17660. scanNumber(start) {
  17661. let simple = this.index === start;
  17662. let hasSeparators = false;
  17663. this.advance(); // Skip initial digit.
  17664. while (true) {
  17665. if (isDigit(this.peek)) ;
  17666. else if (this.peek === $_) {
  17667. // Separators are only valid when they're surrounded by digits. E.g. `1_0_1` is
  17668. // valid while `_101` and `101_` are not. The separator can't be next to the decimal
  17669. // point or another separator either. Note that it's unlikely that we'll hit a case where
  17670. // the underscore is at the start, because that's a valid identifier and it will be picked
  17671. // up earlier in the parsing. We validate for it anyway just in case.
  17672. if (!isDigit(this.input.charCodeAt(this.index - 1)) ||
  17673. !isDigit(this.input.charCodeAt(this.index + 1))) {
  17674. return this.error('Invalid numeric separator', 0);
  17675. }
  17676. hasSeparators = true;
  17677. }
  17678. else if (this.peek === $PERIOD) {
  17679. simple = false;
  17680. }
  17681. else if (isExponentStart(this.peek)) {
  17682. this.advance();
  17683. if (isExponentSign(this.peek))
  17684. this.advance();
  17685. if (!isDigit(this.peek))
  17686. return this.error('Invalid exponent', -1);
  17687. simple = false;
  17688. }
  17689. else {
  17690. break;
  17691. }
  17692. this.advance();
  17693. }
  17694. let str = this.input.substring(start, this.index);
  17695. if (hasSeparators) {
  17696. str = str.replace(/_/g, '');
  17697. }
  17698. const value = simple ? parseIntAutoRadix(str) : parseFloat(str);
  17699. return newNumberToken(start, this.index, value);
  17700. }
  17701. scanString() {
  17702. const start = this.index;
  17703. const quote = this.peek;
  17704. this.advance(); // Skip initial quote.
  17705. let buffer = '';
  17706. let marker = this.index;
  17707. const input = this.input;
  17708. while (this.peek != quote) {
  17709. if (this.peek == $BACKSLASH) {
  17710. const result = this.scanStringBackslash(buffer, marker);
  17711. if (typeof result !== 'string') {
  17712. return result; // Error
  17713. }
  17714. buffer = result;
  17715. marker = this.index;
  17716. }
  17717. else if (this.peek == $EOF) {
  17718. return this.error('Unterminated quote', 0);
  17719. }
  17720. else {
  17721. this.advance();
  17722. }
  17723. }
  17724. const last = input.substring(marker, this.index);
  17725. this.advance(); // Skip terminating quote.
  17726. return new StringToken(start, this.index, buffer + last, StringTokenKind.Plain);
  17727. }
  17728. scanQuestion(start) {
  17729. this.advance();
  17730. let str = '?';
  17731. // Either `a ?? b` or 'a?.b'.
  17732. if (this.peek === $QUESTION || this.peek === $PERIOD) {
  17733. str += this.peek === $PERIOD ? '.' : '?';
  17734. this.advance();
  17735. }
  17736. return newOperatorToken(start, this.index, str);
  17737. }
  17738. scanTemplateLiteralPart(start) {
  17739. let buffer = '';
  17740. let marker = this.index;
  17741. while (this.peek !== $BT) {
  17742. if (this.peek === $BACKSLASH) {
  17743. const result = this.scanStringBackslash(buffer, marker);
  17744. if (typeof result !== 'string') {
  17745. return result; // Error
  17746. }
  17747. buffer = result;
  17748. marker = this.index;
  17749. }
  17750. else if (this.peek === $$) {
  17751. const dollar = this.index;
  17752. this.advance();
  17753. // @ts-expect-error
  17754. if (this.peek === $LBRACE) {
  17755. this.literalInterpolationDepth++;
  17756. this.tokens.push(new StringToken(start, dollar, buffer + this.input.substring(marker, dollar), StringTokenKind.TemplateLiteralPart));
  17757. this.advance();
  17758. return newOperatorToken(dollar, this.index, this.input.substring(dollar, this.index));
  17759. }
  17760. }
  17761. else if (this.peek === $EOF) {
  17762. return this.error('Unterminated template literal', 0);
  17763. }
  17764. else {
  17765. this.advance();
  17766. }
  17767. }
  17768. const last = this.input.substring(marker, this.index);
  17769. this.advance();
  17770. return new StringToken(start, this.index, buffer + last, StringTokenKind.TemplateLiteralEnd);
  17771. }
  17772. error(message, offset) {
  17773. const position = this.index + offset;
  17774. return newErrorToken(position, this.index, `Lexer Error: ${message} at column ${position} in expression [${this.input}]`);
  17775. }
  17776. scanStringBackslash(buffer, marker) {
  17777. buffer += this.input.substring(marker, this.index);
  17778. let unescapedCode;
  17779. this.advance();
  17780. if (this.peek === $u) {
  17781. // 4 character hex code for unicode character.
  17782. const hex = this.input.substring(this.index + 1, this.index + 5);
  17783. if (/^[0-9a-f]+$/i.test(hex)) {
  17784. unescapedCode = parseInt(hex, 16);
  17785. }
  17786. else {
  17787. return this.error(`Invalid unicode escape [\\u${hex}]`, 0);
  17788. }
  17789. for (let i = 0; i < 5; i++) {
  17790. this.advance();
  17791. }
  17792. }
  17793. else {
  17794. unescapedCode = unescape$1(this.peek);
  17795. this.advance();
  17796. }
  17797. buffer += String.fromCharCode(unescapedCode);
  17798. return buffer;
  17799. }
  17800. }
  17801. function isIdentifierStart(code) {
  17802. return (($a <= code && code <= $z) ||
  17803. ($A <= code && code <= $Z) ||
  17804. code == $_ ||
  17805. code == $$);
  17806. }
  17807. function isIdentifierPart(code) {
  17808. return isAsciiLetter(code) || isDigit(code) || code == $_ || code == $$;
  17809. }
  17810. function isExponentStart(code) {
  17811. return code == $e || code == $E;
  17812. }
  17813. function isExponentSign(code) {
  17814. return code == $MINUS || code == $PLUS;
  17815. }
  17816. function unescape$1(code) {
  17817. switch (code) {
  17818. case $n:
  17819. return $LF;
  17820. case $f:
  17821. return $FF;
  17822. case $r:
  17823. return $CR;
  17824. case $t:
  17825. return $TAB;
  17826. case $v:
  17827. return $VTAB;
  17828. default:
  17829. return code;
  17830. }
  17831. }
  17832. function parseIntAutoRadix(text) {
  17833. const result = parseInt(text);
  17834. if (isNaN(result)) {
  17835. throw new Error('Invalid integer literal when parsing ' + text);
  17836. }
  17837. return result;
  17838. }
  17839. class SplitInterpolation {
  17840. strings;
  17841. expressions;
  17842. offsets;
  17843. constructor(strings, expressions, offsets) {
  17844. this.strings = strings;
  17845. this.expressions = expressions;
  17846. this.offsets = offsets;
  17847. }
  17848. }
  17849. class TemplateBindingParseResult {
  17850. templateBindings;
  17851. warnings;
  17852. errors;
  17853. constructor(templateBindings, warnings, errors) {
  17854. this.templateBindings = templateBindings;
  17855. this.warnings = warnings;
  17856. this.errors = errors;
  17857. }
  17858. }
  17859. class Parser {
  17860. _lexer;
  17861. errors = [];
  17862. constructor(_lexer) {
  17863. this._lexer = _lexer;
  17864. }
  17865. parseAction(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17866. this._checkNoInterpolation(input, location, interpolationConfig);
  17867. const sourceToLex = this._stripComments(input);
  17868. const tokens = this._lexer.tokenize(sourceToLex);
  17869. const ast = new _ParseAST(input, location, absoluteOffset, tokens, 1 /* ParseFlags.Action */, this.errors, 0).parseChain();
  17870. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  17871. }
  17872. parseBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17873. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  17874. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  17875. }
  17876. checkSimpleExpression(ast) {
  17877. const checker = new SimpleExpressionChecker();
  17878. ast.visit(checker);
  17879. return checker.errors;
  17880. }
  17881. // Host bindings parsed here
  17882. parseSimpleBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17883. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  17884. const errors = this.checkSimpleExpression(ast);
  17885. if (errors.length > 0) {
  17886. this._reportError(`Host binding expression cannot contain ${errors.join(' ')}`, input, location);
  17887. }
  17888. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  17889. }
  17890. _reportError(message, input, errLocation, ctxLocation) {
  17891. this.errors.push(new ParserError(message, input, errLocation, ctxLocation));
  17892. }
  17893. _parseBindingAst(input, location, absoluteOffset, interpolationConfig) {
  17894. this._checkNoInterpolation(input, location, interpolationConfig);
  17895. const sourceToLex = this._stripComments(input);
  17896. const tokens = this._lexer.tokenize(sourceToLex);
  17897. return new _ParseAST(input, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0).parseChain();
  17898. }
  17899. /**
  17900. * Parse microsyntax template expression and return a list of bindings or
  17901. * parsing errors in case the given expression is invalid.
  17902. *
  17903. * For example,
  17904. * ```html
  17905. * <div *ngFor="let item of items">
  17906. * ^ ^ absoluteValueOffset for `templateValue`
  17907. * absoluteKeyOffset for `templateKey`
  17908. * ```
  17909. * contains three bindings:
  17910. * 1. ngFor -> null
  17911. * 2. item -> NgForOfContext.$implicit
  17912. * 3. ngForOf -> items
  17913. *
  17914. * This is apparent from the de-sugared template:
  17915. * ```html
  17916. * <ng-template ngFor let-item [ngForOf]="items">
  17917. * ```
  17918. *
  17919. * @param templateKey name of directive, without the * prefix. For example: ngIf, ngFor
  17920. * @param templateValue RHS of the microsyntax attribute
  17921. * @param templateUrl template filename if it's external, component filename if it's inline
  17922. * @param absoluteKeyOffset start of the `templateKey`
  17923. * @param absoluteValueOffset start of the `templateValue`
  17924. */
  17925. parseTemplateBindings(templateKey, templateValue, templateUrl, absoluteKeyOffset, absoluteValueOffset) {
  17926. const tokens = this._lexer.tokenize(templateValue);
  17927. const parser = new _ParseAST(templateValue, templateUrl, absoluteValueOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0 /* relative offset */);
  17928. return parser.parseTemplateBindings({
  17929. source: templateKey,
  17930. span: new AbsoluteSourceSpan(absoluteKeyOffset, absoluteKeyOffset + templateKey.length),
  17931. });
  17932. }
  17933. parseInterpolation(input, location, absoluteOffset, interpolatedTokens, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17934. const { strings, expressions, offsets } = this.splitInterpolation(input, location, interpolatedTokens, interpolationConfig);
  17935. if (expressions.length === 0)
  17936. return null;
  17937. const expressionNodes = [];
  17938. for (let i = 0; i < expressions.length; ++i) {
  17939. const expressionText = expressions[i].text;
  17940. const sourceToLex = this._stripComments(expressionText);
  17941. const tokens = this._lexer.tokenize(sourceToLex);
  17942. const ast = new _ParseAST(input, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, offsets[i]).parseChain();
  17943. expressionNodes.push(ast);
  17944. }
  17945. return this.createInterpolationAst(strings.map((s) => s.text), expressionNodes, input, location, absoluteOffset);
  17946. }
  17947. /**
  17948. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  17949. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  17950. * This is used for parsing the switch expression in ICUs.
  17951. */
  17952. parseInterpolationExpression(expression, location, absoluteOffset) {
  17953. const sourceToLex = this._stripComments(expression);
  17954. const tokens = this._lexer.tokenize(sourceToLex);
  17955. const ast = new _ParseAST(expression, location, absoluteOffset, tokens, 0 /* ParseFlags.None */, this.errors, 0).parseChain();
  17956. const strings = ['', '']; // The prefix and suffix strings are both empty
  17957. return this.createInterpolationAst(strings, [ast], expression, location, absoluteOffset);
  17958. }
  17959. createInterpolationAst(strings, expressions, input, location, absoluteOffset) {
  17960. const span = new ParseSpan(0, input.length);
  17961. const interpolation = new Interpolation$1(span, span.toAbsolute(absoluteOffset), strings, expressions);
  17962. return new ASTWithSource(interpolation, input, location, absoluteOffset, this.errors);
  17963. }
  17964. /**
  17965. * Splits a string of text into "raw" text segments and expressions present in interpolations in
  17966. * the string.
  17967. * Returns `null` if there are no interpolations, otherwise a
  17968. * `SplitInterpolation` with splits that look like
  17969. * <raw text> <expression> <raw text> ... <raw text> <expression> <raw text>
  17970. */
  17971. splitInterpolation(input, location, interpolatedTokens, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  17972. const strings = [];
  17973. const expressions = [];
  17974. const offsets = [];
  17975. const inputToTemplateIndexMap = interpolatedTokens
  17976. ? getIndexMapForOriginalTemplate(interpolatedTokens)
  17977. : null;
  17978. let i = 0;
  17979. let atInterpolation = false;
  17980. let extendLastString = false;
  17981. let { start: interpStart, end: interpEnd } = interpolationConfig;
  17982. while (i < input.length) {
  17983. if (!atInterpolation) {
  17984. // parse until starting {{
  17985. const start = i;
  17986. i = input.indexOf(interpStart, i);
  17987. if (i === -1) {
  17988. i = input.length;
  17989. }
  17990. const text = input.substring(start, i);
  17991. strings.push({ text, start, end: i });
  17992. atInterpolation = true;
  17993. }
  17994. else {
  17995. // parse from starting {{ to ending }} while ignoring content inside quotes.
  17996. const fullStart = i;
  17997. const exprStart = fullStart + interpStart.length;
  17998. const exprEnd = this._getInterpolationEndIndex(input, interpEnd, exprStart);
  17999. if (exprEnd === -1) {
  18000. // Could not find the end of the interpolation; do not parse an expression.
  18001. // Instead we should extend the content on the last raw string.
  18002. atInterpolation = false;
  18003. extendLastString = true;
  18004. break;
  18005. }
  18006. const fullEnd = exprEnd + interpEnd.length;
  18007. const text = input.substring(exprStart, exprEnd);
  18008. if (text.trim().length === 0) {
  18009. this._reportError('Blank expressions are not allowed in interpolated strings', input, `at column ${i} in`, location);
  18010. }
  18011. expressions.push({ text, start: fullStart, end: fullEnd });
  18012. const startInOriginalTemplate = inputToTemplateIndexMap?.get(fullStart) ?? fullStart;
  18013. const offset = startInOriginalTemplate + interpStart.length;
  18014. offsets.push(offset);
  18015. i = fullEnd;
  18016. atInterpolation = false;
  18017. }
  18018. }
  18019. if (!atInterpolation) {
  18020. // If we are now at a text section, add the remaining content as a raw string.
  18021. if (extendLastString) {
  18022. const piece = strings[strings.length - 1];
  18023. piece.text += input.substring(i);
  18024. piece.end = input.length;
  18025. }
  18026. else {
  18027. strings.push({ text: input.substring(i), start: i, end: input.length });
  18028. }
  18029. }
  18030. return new SplitInterpolation(strings, expressions, offsets);
  18031. }
  18032. wrapLiteralPrimitive(input, location, absoluteOffset) {
  18033. const span = new ParseSpan(0, input == null ? 0 : input.length);
  18034. return new ASTWithSource(new LiteralPrimitive(span, span.toAbsolute(absoluteOffset), input), input, location, absoluteOffset, this.errors);
  18035. }
  18036. _stripComments(input) {
  18037. const i = this._commentStart(input);
  18038. return i != null ? input.substring(0, i) : input;
  18039. }
  18040. _commentStart(input) {
  18041. let outerQuote = null;
  18042. for (let i = 0; i < input.length - 1; i++) {
  18043. const char = input.charCodeAt(i);
  18044. const nextChar = input.charCodeAt(i + 1);
  18045. if (char === $SLASH && nextChar == $SLASH && outerQuote == null)
  18046. return i;
  18047. if (outerQuote === char) {
  18048. outerQuote = null;
  18049. }
  18050. else if (outerQuote == null && isQuote(char)) {
  18051. outerQuote = char;
  18052. }
  18053. }
  18054. return null;
  18055. }
  18056. _checkNoInterpolation(input, location, { start, end }) {
  18057. let startIndex = -1;
  18058. let endIndex = -1;
  18059. for (const charIndex of this._forEachUnquotedChar(input, 0)) {
  18060. if (startIndex === -1) {
  18061. if (input.startsWith(start)) {
  18062. startIndex = charIndex;
  18063. }
  18064. }
  18065. else {
  18066. endIndex = this._getInterpolationEndIndex(input, end, charIndex);
  18067. if (endIndex > -1) {
  18068. break;
  18069. }
  18070. }
  18071. }
  18072. if (startIndex > -1 && endIndex > -1) {
  18073. this._reportError(`Got interpolation (${start}${end}) where expression was expected`, input, `at column ${startIndex} in`, location);
  18074. }
  18075. }
  18076. /**
  18077. * Finds the index of the end of an interpolation expression
  18078. * while ignoring comments and quoted content.
  18079. */
  18080. _getInterpolationEndIndex(input, expressionEnd, start) {
  18081. for (const charIndex of this._forEachUnquotedChar(input, start)) {
  18082. if (input.startsWith(expressionEnd, charIndex)) {
  18083. return charIndex;
  18084. }
  18085. // Nothing else in the expression matters after we've
  18086. // hit a comment so look directly for the end token.
  18087. if (input.startsWith('//', charIndex)) {
  18088. return input.indexOf(expressionEnd, charIndex);
  18089. }
  18090. }
  18091. return -1;
  18092. }
  18093. /**
  18094. * Generator used to iterate over the character indexes of a string that are outside of quotes.
  18095. * @param input String to loop through.
  18096. * @param start Index within the string at which to start.
  18097. */
  18098. *_forEachUnquotedChar(input, start) {
  18099. let currentQuote = null;
  18100. let escapeCount = 0;
  18101. for (let i = start; i < input.length; i++) {
  18102. const char = input[i];
  18103. // Skip the characters inside quotes. Note that we only care about the outer-most
  18104. // quotes matching up and we need to account for escape characters.
  18105. if (isQuote(input.charCodeAt(i)) &&
  18106. (currentQuote === null || currentQuote === char) &&
  18107. escapeCount % 2 === 0) {
  18108. currentQuote = currentQuote === null ? char : null;
  18109. }
  18110. else if (currentQuote === null) {
  18111. yield i;
  18112. }
  18113. escapeCount = char === '\\' ? escapeCount + 1 : 0;
  18114. }
  18115. }
  18116. }
  18117. /** Describes a stateful context an expression parser is in. */
  18118. var ParseContextFlags;
  18119. (function (ParseContextFlags) {
  18120. ParseContextFlags[ParseContextFlags["None"] = 0] = "None";
  18121. /**
  18122. * A Writable context is one in which a value may be written to an lvalue.
  18123. * For example, after we see a property access, we may expect a write to the
  18124. * property via the "=" operator.
  18125. * prop
  18126. * ^ possible "=" after
  18127. */
  18128. ParseContextFlags[ParseContextFlags["Writable"] = 1] = "Writable";
  18129. })(ParseContextFlags || (ParseContextFlags = {}));
  18130. class _ParseAST {
  18131. input;
  18132. location;
  18133. absoluteOffset;
  18134. tokens;
  18135. parseFlags;
  18136. errors;
  18137. offset;
  18138. rparensExpected = 0;
  18139. rbracketsExpected = 0;
  18140. rbracesExpected = 0;
  18141. context = ParseContextFlags.None;
  18142. // Cache of expression start and input indeces to the absolute source span they map to, used to
  18143. // prevent creating superfluous source spans in `sourceSpan`.
  18144. // A serial of the expression start and input index is used for mapping because both are stateful
  18145. // and may change for subsequent expressions visited by the parser.
  18146. sourceSpanCache = new Map();
  18147. index = 0;
  18148. constructor(input, location, absoluteOffset, tokens, parseFlags, errors, offset) {
  18149. this.input = input;
  18150. this.location = location;
  18151. this.absoluteOffset = absoluteOffset;
  18152. this.tokens = tokens;
  18153. this.parseFlags = parseFlags;
  18154. this.errors = errors;
  18155. this.offset = offset;
  18156. }
  18157. peek(offset) {
  18158. const i = this.index + offset;
  18159. return i < this.tokens.length ? this.tokens[i] : EOF;
  18160. }
  18161. get next() {
  18162. return this.peek(0);
  18163. }
  18164. /** Whether all the parser input has been processed. */
  18165. get atEOF() {
  18166. return this.index >= this.tokens.length;
  18167. }
  18168. /**
  18169. * Index of the next token to be processed, or the end of the last token if all have been
  18170. * processed.
  18171. */
  18172. get inputIndex() {
  18173. return this.atEOF ? this.currentEndIndex : this.next.index + this.offset;
  18174. }
  18175. /**
  18176. * End index of the last processed token, or the start of the first token if none have been
  18177. * processed.
  18178. */
  18179. get currentEndIndex() {
  18180. if (this.index > 0) {
  18181. const curToken = this.peek(-1);
  18182. return curToken.end + this.offset;
  18183. }
  18184. // No tokens have been processed yet; return the next token's start or the length of the input
  18185. // if there is no token.
  18186. if (this.tokens.length === 0) {
  18187. return this.input.length + this.offset;
  18188. }
  18189. return this.next.index + this.offset;
  18190. }
  18191. /**
  18192. * Returns the absolute offset of the start of the current token.
  18193. */
  18194. get currentAbsoluteOffset() {
  18195. return this.absoluteOffset + this.inputIndex;
  18196. }
  18197. /**
  18198. * Retrieve a `ParseSpan` from `start` to the current position (or to `artificialEndIndex` if
  18199. * provided).
  18200. *
  18201. * @param start Position from which the `ParseSpan` will start.
  18202. * @param artificialEndIndex Optional ending index to be used if provided (and if greater than the
  18203. * natural ending index)
  18204. */
  18205. span(start, artificialEndIndex) {
  18206. let endIndex = this.currentEndIndex;
  18207. if (artificialEndIndex !== undefined && artificialEndIndex > this.currentEndIndex) {
  18208. endIndex = artificialEndIndex;
  18209. }
  18210. // In some unusual parsing scenarios (like when certain tokens are missing and an `EmptyExpr` is
  18211. // being created), the current token may already be advanced beyond the `currentEndIndex`. This
  18212. // appears to be a deep-seated parser bug.
  18213. //
  18214. // As a workaround for now, swap the start and end indices to ensure a valid `ParseSpan`.
  18215. // TODO(alxhub): fix the bug upstream in the parser state, and remove this workaround.
  18216. if (start > endIndex) {
  18217. const tmp = endIndex;
  18218. endIndex = start;
  18219. start = tmp;
  18220. }
  18221. return new ParseSpan(start, endIndex);
  18222. }
  18223. sourceSpan(start, artificialEndIndex) {
  18224. const serial = `${start}@${this.inputIndex}:${artificialEndIndex}`;
  18225. if (!this.sourceSpanCache.has(serial)) {
  18226. this.sourceSpanCache.set(serial, this.span(start, artificialEndIndex).toAbsolute(this.absoluteOffset));
  18227. }
  18228. return this.sourceSpanCache.get(serial);
  18229. }
  18230. advance() {
  18231. this.index++;
  18232. }
  18233. /**
  18234. * Executes a callback in the provided context.
  18235. */
  18236. withContext(context, cb) {
  18237. this.context |= context;
  18238. const ret = cb();
  18239. this.context ^= context;
  18240. return ret;
  18241. }
  18242. consumeOptionalCharacter(code) {
  18243. if (this.next.isCharacter(code)) {
  18244. this.advance();
  18245. return true;
  18246. }
  18247. else {
  18248. return false;
  18249. }
  18250. }
  18251. peekKeywordLet() {
  18252. return this.next.isKeywordLet();
  18253. }
  18254. peekKeywordAs() {
  18255. return this.next.isKeywordAs();
  18256. }
  18257. /**
  18258. * Consumes an expected character, otherwise emits an error about the missing expected character
  18259. * and skips over the token stream until reaching a recoverable point.
  18260. *
  18261. * See `this.error` and `this.skip` for more details.
  18262. */
  18263. expectCharacter(code) {
  18264. if (this.consumeOptionalCharacter(code))
  18265. return;
  18266. this.error(`Missing expected ${String.fromCharCode(code)}`);
  18267. }
  18268. consumeOptionalOperator(op) {
  18269. if (this.next.isOperator(op)) {
  18270. this.advance();
  18271. return true;
  18272. }
  18273. else {
  18274. return false;
  18275. }
  18276. }
  18277. expectOperator(operator) {
  18278. if (this.consumeOptionalOperator(operator))
  18279. return;
  18280. this.error(`Missing expected operator ${operator}`);
  18281. }
  18282. prettyPrintToken(tok) {
  18283. return tok === EOF ? 'end of input' : `token ${tok}`;
  18284. }
  18285. expectIdentifierOrKeyword() {
  18286. const n = this.next;
  18287. if (!n.isIdentifier() && !n.isKeyword()) {
  18288. if (n.isPrivateIdentifier()) {
  18289. this._reportErrorForPrivateIdentifier(n, 'expected identifier or keyword');
  18290. }
  18291. else {
  18292. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier or keyword`);
  18293. }
  18294. return null;
  18295. }
  18296. this.advance();
  18297. return n.toString();
  18298. }
  18299. expectIdentifierOrKeywordOrString() {
  18300. const n = this.next;
  18301. if (!n.isIdentifier() && !n.isKeyword() && !n.isString()) {
  18302. if (n.isPrivateIdentifier()) {
  18303. this._reportErrorForPrivateIdentifier(n, 'expected identifier, keyword or string');
  18304. }
  18305. else {
  18306. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier, keyword, or string`);
  18307. }
  18308. return '';
  18309. }
  18310. this.advance();
  18311. return n.toString();
  18312. }
  18313. parseChain() {
  18314. const exprs = [];
  18315. const start = this.inputIndex;
  18316. while (this.index < this.tokens.length) {
  18317. const expr = this.parsePipe();
  18318. exprs.push(expr);
  18319. if (this.consumeOptionalCharacter($SEMICOLON)) {
  18320. if (!(this.parseFlags & 1 /* ParseFlags.Action */)) {
  18321. this.error('Binding expression cannot contain chained expression');
  18322. }
  18323. while (this.consumeOptionalCharacter($SEMICOLON)) { } // read all semicolons
  18324. }
  18325. else if (this.index < this.tokens.length) {
  18326. const errorIndex = this.index;
  18327. this.error(`Unexpected token '${this.next}'`);
  18328. // The `error` call above will skip ahead to the next recovery point in an attempt to
  18329. // recover part of the expression, but that might be the token we started from which will
  18330. // lead to an infinite loop. If that's the case, break the loop assuming that we can't
  18331. // parse further.
  18332. if (this.index === errorIndex) {
  18333. break;
  18334. }
  18335. }
  18336. }
  18337. if (exprs.length === 0) {
  18338. // We have no expressions so create an empty expression that spans the entire input length
  18339. const artificialStart = this.offset;
  18340. const artificialEnd = this.offset + this.input.length;
  18341. return new EmptyExpr$1(this.span(artificialStart, artificialEnd), this.sourceSpan(artificialStart, artificialEnd));
  18342. }
  18343. if (exprs.length == 1)
  18344. return exprs[0];
  18345. return new Chain(this.span(start), this.sourceSpan(start), exprs);
  18346. }
  18347. parsePipe() {
  18348. const start = this.inputIndex;
  18349. let result = this.parseExpression();
  18350. if (this.consumeOptionalOperator('|')) {
  18351. if (this.parseFlags & 1 /* ParseFlags.Action */) {
  18352. this.error(`Cannot have a pipe in an action expression`);
  18353. }
  18354. do {
  18355. const nameStart = this.inputIndex;
  18356. let nameId = this.expectIdentifierOrKeyword();
  18357. let nameSpan;
  18358. let fullSpanEnd = undefined;
  18359. if (nameId !== null) {
  18360. nameSpan = this.sourceSpan(nameStart);
  18361. }
  18362. else {
  18363. // No valid identifier was found, so we'll assume an empty pipe name ('').
  18364. nameId = '';
  18365. // However, there may have been whitespace present between the pipe character and the next
  18366. // token in the sequence (or the end of input). We want to track this whitespace so that
  18367. // the `BindingPipe` we produce covers not just the pipe character, but any trailing
  18368. // whitespace beyond it. Another way of thinking about this is that the zero-length name
  18369. // is assumed to be at the end of any whitespace beyond the pipe character.
  18370. //
  18371. // Therefore, we push the end of the `ParseSpan` for this pipe all the way up to the
  18372. // beginning of the next token, or until the end of input if the next token is EOF.
  18373. fullSpanEnd = this.next.index !== -1 ? this.next.index : this.input.length + this.offset;
  18374. // The `nameSpan` for an empty pipe name is zero-length at the end of any whitespace
  18375. // beyond the pipe character.
  18376. nameSpan = new ParseSpan(fullSpanEnd, fullSpanEnd).toAbsolute(this.absoluteOffset);
  18377. }
  18378. const args = [];
  18379. while (this.consumeOptionalCharacter($COLON)) {
  18380. args.push(this.parseExpression());
  18381. // If there are additional expressions beyond the name, then the artificial end for the
  18382. // name is no longer relevant.
  18383. }
  18384. result = new BindingPipe(this.span(start), this.sourceSpan(start, fullSpanEnd), result, nameId, args, nameSpan);
  18385. } while (this.consumeOptionalOperator('|'));
  18386. }
  18387. return result;
  18388. }
  18389. parseExpression() {
  18390. return this.parseConditional();
  18391. }
  18392. parseConditional() {
  18393. const start = this.inputIndex;
  18394. const result = this.parseLogicalOr();
  18395. if (this.consumeOptionalOperator('?')) {
  18396. const yes = this.parsePipe();
  18397. let no;
  18398. if (!this.consumeOptionalCharacter($COLON)) {
  18399. const end = this.inputIndex;
  18400. const expression = this.input.substring(start, end);
  18401. this.error(`Conditional expression ${expression} requires all 3 expressions`);
  18402. no = new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18403. }
  18404. else {
  18405. no = this.parsePipe();
  18406. }
  18407. return new Conditional(this.span(start), this.sourceSpan(start), result, yes, no);
  18408. }
  18409. else {
  18410. return result;
  18411. }
  18412. }
  18413. parseLogicalOr() {
  18414. // '||'
  18415. const start = this.inputIndex;
  18416. let result = this.parseLogicalAnd();
  18417. while (this.consumeOptionalOperator('||')) {
  18418. const right = this.parseLogicalAnd();
  18419. result = new Binary(this.span(start), this.sourceSpan(start), '||', result, right);
  18420. }
  18421. return result;
  18422. }
  18423. parseLogicalAnd() {
  18424. // '&&'
  18425. const start = this.inputIndex;
  18426. let result = this.parseNullishCoalescing();
  18427. while (this.consumeOptionalOperator('&&')) {
  18428. const right = this.parseNullishCoalescing();
  18429. result = new Binary(this.span(start), this.sourceSpan(start), '&&', result, right);
  18430. }
  18431. return result;
  18432. }
  18433. parseNullishCoalescing() {
  18434. // '??'
  18435. const start = this.inputIndex;
  18436. let result = this.parseEquality();
  18437. while (this.consumeOptionalOperator('??')) {
  18438. const right = this.parseEquality();
  18439. result = new Binary(this.span(start), this.sourceSpan(start), '??', result, right);
  18440. }
  18441. return result;
  18442. }
  18443. parseEquality() {
  18444. // '==','!=','===','!=='
  18445. const start = this.inputIndex;
  18446. let result = this.parseRelational();
  18447. while (this.next.type == TokenType.Operator) {
  18448. const operator = this.next.strValue;
  18449. switch (operator) {
  18450. case '==':
  18451. case '===':
  18452. case '!=':
  18453. case '!==':
  18454. this.advance();
  18455. const right = this.parseRelational();
  18456. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18457. continue;
  18458. }
  18459. break;
  18460. }
  18461. return result;
  18462. }
  18463. parseRelational() {
  18464. // '<', '>', '<=', '>='
  18465. const start = this.inputIndex;
  18466. let result = this.parseAdditive();
  18467. while (this.next.type == TokenType.Operator) {
  18468. const operator = this.next.strValue;
  18469. switch (operator) {
  18470. case '<':
  18471. case '>':
  18472. case '<=':
  18473. case '>=':
  18474. this.advance();
  18475. const right = this.parseAdditive();
  18476. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18477. continue;
  18478. }
  18479. break;
  18480. }
  18481. return result;
  18482. }
  18483. parseAdditive() {
  18484. // '+', '-'
  18485. const start = this.inputIndex;
  18486. let result = this.parseMultiplicative();
  18487. while (this.next.type == TokenType.Operator) {
  18488. const operator = this.next.strValue;
  18489. switch (operator) {
  18490. case '+':
  18491. case '-':
  18492. this.advance();
  18493. let right = this.parseMultiplicative();
  18494. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18495. continue;
  18496. }
  18497. break;
  18498. }
  18499. return result;
  18500. }
  18501. parseMultiplicative() {
  18502. // '*', '%', '/'
  18503. const start = this.inputIndex;
  18504. let result = this.parsePrefix();
  18505. while (this.next.type == TokenType.Operator) {
  18506. const operator = this.next.strValue;
  18507. switch (operator) {
  18508. case '*':
  18509. case '%':
  18510. case '/':
  18511. this.advance();
  18512. let right = this.parsePrefix();
  18513. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  18514. continue;
  18515. }
  18516. break;
  18517. }
  18518. return result;
  18519. }
  18520. parsePrefix() {
  18521. if (this.next.type == TokenType.Operator) {
  18522. const start = this.inputIndex;
  18523. const operator = this.next.strValue;
  18524. let result;
  18525. switch (operator) {
  18526. case '+':
  18527. this.advance();
  18528. result = this.parsePrefix();
  18529. return Unary.createPlus(this.span(start), this.sourceSpan(start), result);
  18530. case '-':
  18531. this.advance();
  18532. result = this.parsePrefix();
  18533. return Unary.createMinus(this.span(start), this.sourceSpan(start), result);
  18534. case '!':
  18535. this.advance();
  18536. result = this.parsePrefix();
  18537. return new PrefixNot(this.span(start), this.sourceSpan(start), result);
  18538. }
  18539. }
  18540. else if (this.next.isKeywordTypeof()) {
  18541. this.advance();
  18542. const start = this.inputIndex;
  18543. let result = this.parsePrefix();
  18544. return new TypeofExpression(this.span(start), this.sourceSpan(start), result);
  18545. }
  18546. return this.parseCallChain();
  18547. }
  18548. parseCallChain() {
  18549. const start = this.inputIndex;
  18550. let result = this.parsePrimary();
  18551. while (true) {
  18552. if (this.consumeOptionalCharacter($PERIOD)) {
  18553. result = this.parseAccessMember(result, start, false);
  18554. }
  18555. else if (this.consumeOptionalOperator('?.')) {
  18556. if (this.consumeOptionalCharacter($LPAREN)) {
  18557. result = this.parseCall(result, start, true);
  18558. }
  18559. else {
  18560. result = this.consumeOptionalCharacter($LBRACKET)
  18561. ? this.parseKeyedReadOrWrite(result, start, true)
  18562. : this.parseAccessMember(result, start, true);
  18563. }
  18564. }
  18565. else if (this.consumeOptionalCharacter($LBRACKET)) {
  18566. result = this.parseKeyedReadOrWrite(result, start, false);
  18567. }
  18568. else if (this.consumeOptionalCharacter($LPAREN)) {
  18569. result = this.parseCall(result, start, false);
  18570. }
  18571. else if (this.consumeOptionalOperator('!')) {
  18572. result = new NonNullAssert(this.span(start), this.sourceSpan(start), result);
  18573. }
  18574. else {
  18575. return result;
  18576. }
  18577. }
  18578. }
  18579. parsePrimary() {
  18580. const start = this.inputIndex;
  18581. if (this.consumeOptionalCharacter($LPAREN)) {
  18582. this.rparensExpected++;
  18583. const result = this.parsePipe();
  18584. this.rparensExpected--;
  18585. this.expectCharacter($RPAREN);
  18586. return result;
  18587. }
  18588. else if (this.next.isKeywordNull()) {
  18589. this.advance();
  18590. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), null);
  18591. }
  18592. else if (this.next.isKeywordUndefined()) {
  18593. this.advance();
  18594. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), void 0);
  18595. }
  18596. else if (this.next.isKeywordTrue()) {
  18597. this.advance();
  18598. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), true);
  18599. }
  18600. else if (this.next.isKeywordFalse()) {
  18601. this.advance();
  18602. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), false);
  18603. }
  18604. else if (this.next.isKeywordThis()) {
  18605. this.advance();
  18606. return new ThisReceiver(this.span(start), this.sourceSpan(start));
  18607. }
  18608. else if (this.consumeOptionalCharacter($LBRACKET)) {
  18609. this.rbracketsExpected++;
  18610. const elements = this.parseExpressionList($RBRACKET);
  18611. this.rbracketsExpected--;
  18612. this.expectCharacter($RBRACKET);
  18613. return new LiteralArray(this.span(start), this.sourceSpan(start), elements);
  18614. }
  18615. else if (this.next.isCharacter($LBRACE)) {
  18616. return this.parseLiteralMap();
  18617. }
  18618. else if (this.next.isIdentifier()) {
  18619. return this.parseAccessMember(new ImplicitReceiver(this.span(start), this.sourceSpan(start)), start, false);
  18620. }
  18621. else if (this.next.isNumber()) {
  18622. const value = this.next.toNumber();
  18623. this.advance();
  18624. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), value);
  18625. }
  18626. else if (this.next.isTemplateLiteralEnd()) {
  18627. return this.parseNoInterpolationTemplateLiteral();
  18628. }
  18629. else if (this.next.isTemplateLiteralPart()) {
  18630. return this.parseTemplateLiteral();
  18631. }
  18632. else if (this.next.isString() && this.next.kind === StringTokenKind.Plain) {
  18633. const literalValue = this.next.toString();
  18634. this.advance();
  18635. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), literalValue);
  18636. }
  18637. else if (this.next.isPrivateIdentifier()) {
  18638. this._reportErrorForPrivateIdentifier(this.next, null);
  18639. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18640. }
  18641. else if (this.index >= this.tokens.length) {
  18642. this.error(`Unexpected end of expression: ${this.input}`);
  18643. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18644. }
  18645. else {
  18646. this.error(`Unexpected token ${this.next}`);
  18647. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18648. }
  18649. }
  18650. parseExpressionList(terminator) {
  18651. const result = [];
  18652. do {
  18653. if (!this.next.isCharacter(terminator)) {
  18654. result.push(this.parsePipe());
  18655. }
  18656. else {
  18657. break;
  18658. }
  18659. } while (this.consumeOptionalCharacter($COMMA));
  18660. return result;
  18661. }
  18662. parseLiteralMap() {
  18663. const keys = [];
  18664. const values = [];
  18665. const start = this.inputIndex;
  18666. this.expectCharacter($LBRACE);
  18667. if (!this.consumeOptionalCharacter($RBRACE)) {
  18668. this.rbracesExpected++;
  18669. do {
  18670. const keyStart = this.inputIndex;
  18671. const quoted = this.next.isString();
  18672. const key = this.expectIdentifierOrKeywordOrString();
  18673. const literalMapKey = { key, quoted };
  18674. keys.push(literalMapKey);
  18675. // Properties with quoted keys can't use the shorthand syntax.
  18676. if (quoted) {
  18677. this.expectCharacter($COLON);
  18678. values.push(this.parsePipe());
  18679. }
  18680. else if (this.consumeOptionalCharacter($COLON)) {
  18681. values.push(this.parsePipe());
  18682. }
  18683. else {
  18684. literalMapKey.isShorthandInitialized = true;
  18685. const span = this.span(keyStart);
  18686. const sourceSpan = this.sourceSpan(keyStart);
  18687. values.push(new PropertyRead(span, sourceSpan, sourceSpan, new ImplicitReceiver(span, sourceSpan), key));
  18688. }
  18689. } while (this.consumeOptionalCharacter($COMMA) &&
  18690. !this.next.isCharacter($RBRACE));
  18691. this.rbracesExpected--;
  18692. this.expectCharacter($RBRACE);
  18693. }
  18694. return new LiteralMap(this.span(start), this.sourceSpan(start), keys, values);
  18695. }
  18696. parseAccessMember(readReceiver, start, isSafe) {
  18697. const nameStart = this.inputIndex;
  18698. const id = this.withContext(ParseContextFlags.Writable, () => {
  18699. const id = this.expectIdentifierOrKeyword() ?? '';
  18700. if (id.length === 0) {
  18701. this.error(`Expected identifier for property access`, readReceiver.span.end);
  18702. }
  18703. return id;
  18704. });
  18705. const nameSpan = this.sourceSpan(nameStart);
  18706. let receiver;
  18707. if (isSafe) {
  18708. if (this.consumeOptionalOperator('=')) {
  18709. this.error("The '?.' operator cannot be used in the assignment");
  18710. receiver = new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18711. }
  18712. else {
  18713. receiver = new SafePropertyRead(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id);
  18714. }
  18715. }
  18716. else {
  18717. if (this.consumeOptionalOperator('=')) {
  18718. if (!(this.parseFlags & 1 /* ParseFlags.Action */)) {
  18719. this.error('Bindings cannot contain assignments');
  18720. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18721. }
  18722. const value = this.parseConditional();
  18723. receiver = new PropertyWrite(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id, value);
  18724. }
  18725. else {
  18726. receiver = new PropertyRead(this.span(start), this.sourceSpan(start), nameSpan, readReceiver, id);
  18727. }
  18728. }
  18729. return receiver;
  18730. }
  18731. parseCall(receiver, start, isSafe) {
  18732. const argumentStart = this.inputIndex;
  18733. this.rparensExpected++;
  18734. const args = this.parseCallArguments();
  18735. const argumentSpan = this.span(argumentStart, this.inputIndex).toAbsolute(this.absoluteOffset);
  18736. this.expectCharacter($RPAREN);
  18737. this.rparensExpected--;
  18738. const span = this.span(start);
  18739. const sourceSpan = this.sourceSpan(start);
  18740. return isSafe
  18741. ? new SafeCall(span, sourceSpan, receiver, args, argumentSpan)
  18742. : new Call(span, sourceSpan, receiver, args, argumentSpan);
  18743. }
  18744. parseCallArguments() {
  18745. if (this.next.isCharacter($RPAREN))
  18746. return [];
  18747. const positionals = [];
  18748. do {
  18749. positionals.push(this.parsePipe());
  18750. } while (this.consumeOptionalCharacter($COMMA));
  18751. return positionals;
  18752. }
  18753. /**
  18754. * Parses an identifier, a keyword, a string with an optional `-` in between,
  18755. * and returns the string along with its absolute source span.
  18756. */
  18757. expectTemplateBindingKey() {
  18758. let result = '';
  18759. let operatorFound = false;
  18760. const start = this.currentAbsoluteOffset;
  18761. do {
  18762. result += this.expectIdentifierOrKeywordOrString();
  18763. operatorFound = this.consumeOptionalOperator('-');
  18764. if (operatorFound) {
  18765. result += '-';
  18766. }
  18767. } while (operatorFound);
  18768. return {
  18769. source: result,
  18770. span: new AbsoluteSourceSpan(start, start + result.length),
  18771. };
  18772. }
  18773. /**
  18774. * Parse microsyntax template expression and return a list of bindings or
  18775. * parsing errors in case the given expression is invalid.
  18776. *
  18777. * For example,
  18778. * ```html
  18779. * <div *ngFor="let item of items; index as i; trackBy: func">
  18780. * ```
  18781. * contains five bindings:
  18782. * 1. ngFor -> null
  18783. * 2. item -> NgForOfContext.$implicit
  18784. * 3. ngForOf -> items
  18785. * 4. i -> NgForOfContext.index
  18786. * 5. ngForTrackBy -> func
  18787. *
  18788. * For a full description of the microsyntax grammar, see
  18789. * https://gist.github.com/mhevery/d3530294cff2e4a1b3fe15ff75d08855
  18790. *
  18791. * @param templateKey name of the microsyntax directive, like ngIf, ngFor,
  18792. * without the *, along with its absolute span.
  18793. */
  18794. parseTemplateBindings(templateKey) {
  18795. const bindings = [];
  18796. // The first binding is for the template key itself
  18797. // In *ngFor="let item of items", key = "ngFor", value = null
  18798. // In *ngIf="cond | pipe", key = "ngIf", value = "cond | pipe"
  18799. bindings.push(...this.parseDirectiveKeywordBindings(templateKey));
  18800. while (this.index < this.tokens.length) {
  18801. // If it starts with 'let', then this must be variable declaration
  18802. const letBinding = this.parseLetBinding();
  18803. if (letBinding) {
  18804. bindings.push(letBinding);
  18805. }
  18806. else {
  18807. // Two possible cases here, either `value "as" key` or
  18808. // "directive-keyword expression". We don't know which case, but both
  18809. // "value" and "directive-keyword" are template binding key, so consume
  18810. // the key first.
  18811. const key = this.expectTemplateBindingKey();
  18812. // Peek at the next token, if it is "as" then this must be variable
  18813. // declaration.
  18814. const binding = this.parseAsBinding(key);
  18815. if (binding) {
  18816. bindings.push(binding);
  18817. }
  18818. else {
  18819. // Otherwise the key must be a directive keyword, like "of". Transform
  18820. // the key to actual key. Eg. of -> ngForOf, trackBy -> ngForTrackBy
  18821. key.source =
  18822. templateKey.source + key.source.charAt(0).toUpperCase() + key.source.substring(1);
  18823. bindings.push(...this.parseDirectiveKeywordBindings(key));
  18824. }
  18825. }
  18826. this.consumeStatementTerminator();
  18827. }
  18828. return new TemplateBindingParseResult(bindings, [] /* warnings */, this.errors);
  18829. }
  18830. parseKeyedReadOrWrite(receiver, start, isSafe) {
  18831. return this.withContext(ParseContextFlags.Writable, () => {
  18832. this.rbracketsExpected++;
  18833. const key = this.parsePipe();
  18834. if (key instanceof EmptyExpr$1) {
  18835. this.error(`Key access cannot be empty`);
  18836. }
  18837. this.rbracketsExpected--;
  18838. this.expectCharacter($RBRACKET);
  18839. if (this.consumeOptionalOperator('=')) {
  18840. if (isSafe) {
  18841. this.error("The '?.' operator cannot be used in the assignment");
  18842. }
  18843. else {
  18844. const value = this.parseConditional();
  18845. return new KeyedWrite(this.span(start), this.sourceSpan(start), receiver, key, value);
  18846. }
  18847. }
  18848. else {
  18849. return isSafe
  18850. ? new SafeKeyedRead(this.span(start), this.sourceSpan(start), receiver, key)
  18851. : new KeyedRead(this.span(start), this.sourceSpan(start), receiver, key);
  18852. }
  18853. return new EmptyExpr$1(this.span(start), this.sourceSpan(start));
  18854. });
  18855. }
  18856. /**
  18857. * Parse a directive keyword, followed by a mandatory expression.
  18858. * For example, "of items", "trackBy: func".
  18859. * The bindings are: ngForOf -> items, ngForTrackBy -> func
  18860. * There could be an optional "as" binding that follows the expression.
  18861. * For example,
  18862. * ```
  18863. * *ngFor="let item of items | slice:0:1 as collection".
  18864. * ^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
  18865. * keyword bound target optional 'as' binding
  18866. * ```
  18867. *
  18868. * @param key binding key, for example, ngFor, ngIf, ngForOf, along with its
  18869. * absolute span.
  18870. */
  18871. parseDirectiveKeywordBindings(key) {
  18872. const bindings = [];
  18873. this.consumeOptionalCharacter($COLON); // trackBy: trackByFunction
  18874. const value = this.getDirectiveBoundTarget();
  18875. let spanEnd = this.currentAbsoluteOffset;
  18876. // The binding could optionally be followed by "as". For example,
  18877. // *ngIf="cond | pipe as x". In this case, the key in the "as" binding
  18878. // is "x" and the value is the template key itself ("ngIf"). Note that the
  18879. // 'key' in the current context now becomes the "value" in the next binding.
  18880. const asBinding = this.parseAsBinding(key);
  18881. if (!asBinding) {
  18882. this.consumeStatementTerminator();
  18883. spanEnd = this.currentAbsoluteOffset;
  18884. }
  18885. const sourceSpan = new AbsoluteSourceSpan(key.span.start, spanEnd);
  18886. bindings.push(new ExpressionBinding(sourceSpan, key, value));
  18887. if (asBinding) {
  18888. bindings.push(asBinding);
  18889. }
  18890. return bindings;
  18891. }
  18892. /**
  18893. * Return the expression AST for the bound target of a directive keyword
  18894. * binding. For example,
  18895. * ```
  18896. * *ngIf="condition | pipe"
  18897. * ^^^^^^^^^^^^^^^^ bound target for "ngIf"
  18898. * *ngFor="let item of items"
  18899. * ^^^^^ bound target for "ngForOf"
  18900. * ```
  18901. */
  18902. getDirectiveBoundTarget() {
  18903. if (this.next === EOF || this.peekKeywordAs() || this.peekKeywordLet()) {
  18904. return null;
  18905. }
  18906. const ast = this.parsePipe(); // example: "condition | async"
  18907. const { start, end } = ast.span;
  18908. const value = this.input.substring(start, end);
  18909. return new ASTWithSource(ast, value, this.location, this.absoluteOffset + start, this.errors);
  18910. }
  18911. /**
  18912. * Return the binding for a variable declared using `as`. Note that the order
  18913. * of the key-value pair in this declaration is reversed. For example,
  18914. * ```
  18915. * *ngFor="let item of items; index as i"
  18916. * ^^^^^ ^
  18917. * value key
  18918. * ```
  18919. *
  18920. * @param value name of the value in the declaration, "ngIf" in the example
  18921. * above, along with its absolute span.
  18922. */
  18923. parseAsBinding(value) {
  18924. if (!this.peekKeywordAs()) {
  18925. return null;
  18926. }
  18927. this.advance(); // consume the 'as' keyword
  18928. const key = this.expectTemplateBindingKey();
  18929. this.consumeStatementTerminator();
  18930. const sourceSpan = new AbsoluteSourceSpan(value.span.start, this.currentAbsoluteOffset);
  18931. return new VariableBinding(sourceSpan, key, value);
  18932. }
  18933. /**
  18934. * Return the binding for a variable declared using `let`. For example,
  18935. * ```
  18936. * *ngFor="let item of items; let i=index;"
  18937. * ^^^^^^^^ ^^^^^^^^^^^
  18938. * ```
  18939. * In the first binding, `item` is bound to `NgForOfContext.$implicit`.
  18940. * In the second binding, `i` is bound to `NgForOfContext.index`.
  18941. */
  18942. parseLetBinding() {
  18943. if (!this.peekKeywordLet()) {
  18944. return null;
  18945. }
  18946. const spanStart = this.currentAbsoluteOffset;
  18947. this.advance(); // consume the 'let' keyword
  18948. const key = this.expectTemplateBindingKey();
  18949. let value = null;
  18950. if (this.consumeOptionalOperator('=')) {
  18951. value = this.expectTemplateBindingKey();
  18952. }
  18953. this.consumeStatementTerminator();
  18954. const sourceSpan = new AbsoluteSourceSpan(spanStart, this.currentAbsoluteOffset);
  18955. return new VariableBinding(sourceSpan, key, value);
  18956. }
  18957. parseNoInterpolationTemplateLiteral() {
  18958. const text = this.next.strValue;
  18959. const start = this.inputIndex;
  18960. this.advance();
  18961. const span = this.span(start);
  18962. const sourceSpan = this.sourceSpan(start);
  18963. return new TemplateLiteral(span, sourceSpan, [new TemplateLiteralElement(span, sourceSpan, text)], []);
  18964. }
  18965. parseTemplateLiteral() {
  18966. const start = this.inputIndex;
  18967. const elements = [];
  18968. const expressions = [];
  18969. while (this.next !== EOF) {
  18970. const token = this.next;
  18971. if (token.isTemplateLiteralPart() || token.isTemplateLiteralEnd()) {
  18972. const partStart = this.inputIndex;
  18973. this.advance();
  18974. elements.push(new TemplateLiteralElement(this.span(partStart), this.sourceSpan(partStart), token.strValue));
  18975. if (token.isTemplateLiteralEnd()) {
  18976. break;
  18977. }
  18978. }
  18979. else if (token.isTemplateLiteralInterpolationStart()) {
  18980. this.advance();
  18981. const expression = this.parsePipe();
  18982. if (expression instanceof EmptyExpr$1) {
  18983. this.error('Template literal interpolation cannot be empty');
  18984. }
  18985. else {
  18986. expressions.push(expression);
  18987. }
  18988. }
  18989. else {
  18990. this.advance();
  18991. }
  18992. }
  18993. return new TemplateLiteral(this.span(start), this.sourceSpan(start), elements, expressions);
  18994. }
  18995. /**
  18996. * Consume the optional statement terminator: semicolon or comma.
  18997. */
  18998. consumeStatementTerminator() {
  18999. this.consumeOptionalCharacter($SEMICOLON) || this.consumeOptionalCharacter($COMMA);
  19000. }
  19001. /**
  19002. * Records an error and skips over the token stream until reaching a recoverable point. See
  19003. * `this.skip` for more details on token skipping.
  19004. */
  19005. error(message, index = null) {
  19006. this.errors.push(new ParserError(message, this.input, this.locationText(index), this.location));
  19007. this.skip();
  19008. }
  19009. locationText(index = null) {
  19010. if (index == null)
  19011. index = this.index;
  19012. return index < this.tokens.length
  19013. ? `at column ${this.tokens[index].index + 1} in`
  19014. : `at the end of the expression`;
  19015. }
  19016. /**
  19017. * Records an error for an unexpected private identifier being discovered.
  19018. * @param token Token representing a private identifier.
  19019. * @param extraMessage Optional additional message being appended to the error.
  19020. */
  19021. _reportErrorForPrivateIdentifier(token, extraMessage) {
  19022. let errorMessage = `Private identifiers are not supported. Unexpected private identifier: ${token}`;
  19023. if (extraMessage !== null) {
  19024. errorMessage += `, ${extraMessage}`;
  19025. }
  19026. this.error(errorMessage);
  19027. }
  19028. /**
  19029. * Error recovery should skip tokens until it encounters a recovery point.
  19030. *
  19031. * The following are treated as unconditional recovery points:
  19032. * - end of input
  19033. * - ';' (parseChain() is always the root production, and it expects a ';')
  19034. * - '|' (since pipes may be chained and each pipe expression may be treated independently)
  19035. *
  19036. * The following are conditional recovery points:
  19037. * - ')', '}', ']' if one of calling productions is expecting one of these symbols
  19038. * - This allows skip() to recover from errors such as '(a.) + 1' allowing more of the AST to
  19039. * be retained (it doesn't skip any tokens as the ')' is retained because of the '(' begins
  19040. * an '(' <expr> ')' production).
  19041. * The recovery points of grouping symbols must be conditional as they must be skipped if
  19042. * none of the calling productions are not expecting the closing token else we will never
  19043. * make progress in the case of an extraneous group closing symbol (such as a stray ')').
  19044. * That is, we skip a closing symbol if we are not in a grouping production.
  19045. * - '=' in a `Writable` context
  19046. * - In this context, we are able to recover after seeing the `=` operator, which
  19047. * signals the presence of an independent rvalue expression following the `=` operator.
  19048. *
  19049. * If a production expects one of these token it increments the corresponding nesting count,
  19050. * and then decrements it just prior to checking if the token is in the input.
  19051. */
  19052. skip() {
  19053. let n = this.next;
  19054. while (this.index < this.tokens.length &&
  19055. !n.isCharacter($SEMICOLON) &&
  19056. !n.isOperator('|') &&
  19057. (this.rparensExpected <= 0 || !n.isCharacter($RPAREN)) &&
  19058. (this.rbracesExpected <= 0 || !n.isCharacter($RBRACE)) &&
  19059. (this.rbracketsExpected <= 0 || !n.isCharacter($RBRACKET)) &&
  19060. (!(this.context & ParseContextFlags.Writable) || !n.isOperator('='))) {
  19061. if (this.next.isError()) {
  19062. this.errors.push(new ParserError(this.next.toString(), this.input, this.locationText(), this.location));
  19063. }
  19064. this.advance();
  19065. n = this.next;
  19066. }
  19067. }
  19068. }
  19069. class SimpleExpressionChecker extends RecursiveAstVisitor {
  19070. errors = [];
  19071. visitPipe() {
  19072. this.errors.push('pipes');
  19073. }
  19074. }
  19075. /**
  19076. * Computes the real offset in the original template for indexes in an interpolation.
  19077. *
  19078. * Because templates can have encoded HTML entities and the input passed to the parser at this stage
  19079. * of the compiler is the _decoded_ value, we need to compute the real offset using the original
  19080. * encoded values in the interpolated tokens. Note that this is only a special case handling for
  19081. * `MlParserTokenType.ENCODED_ENTITY` token types. All other interpolated tokens are expected to
  19082. * have parts which exactly match the input string for parsing the interpolation.
  19083. *
  19084. * @param interpolatedTokens The tokens for the interpolated value.
  19085. *
  19086. * @returns A map of index locations in the decoded template to indexes in the original template
  19087. */
  19088. function getIndexMapForOriginalTemplate(interpolatedTokens) {
  19089. let offsetMap = new Map();
  19090. let consumedInOriginalTemplate = 0;
  19091. let consumedInInput = 0;
  19092. let tokenIndex = 0;
  19093. while (tokenIndex < interpolatedTokens.length) {
  19094. const currentToken = interpolatedTokens[tokenIndex];
  19095. if (currentToken.type === 9 /* MlParserTokenType.ENCODED_ENTITY */) {
  19096. const [decoded, encoded] = currentToken.parts;
  19097. consumedInOriginalTemplate += encoded.length;
  19098. consumedInInput += decoded.length;
  19099. }
  19100. else {
  19101. const lengthOfParts = currentToken.parts.reduce((sum, current) => sum + current.length, 0);
  19102. consumedInInput += lengthOfParts;
  19103. consumedInOriginalTemplate += lengthOfParts;
  19104. }
  19105. offsetMap.set(consumedInInput, consumedInOriginalTemplate);
  19106. tokenIndex++;
  19107. }
  19108. return offsetMap;
  19109. }
  19110. /** Serializes the given AST into a normalized string format. */
  19111. function serialize(expression) {
  19112. return expression.visit(new SerializeExpressionVisitor());
  19113. }
  19114. class SerializeExpressionVisitor {
  19115. visitUnary(ast, context) {
  19116. return `${ast.operator}${ast.expr.visit(this, context)}`;
  19117. }
  19118. visitBinary(ast, context) {
  19119. return `${ast.left.visit(this, context)} ${ast.operation} ${ast.right.visit(this, context)}`;
  19120. }
  19121. visitChain(ast, context) {
  19122. return ast.expressions.map((e) => e.visit(this, context)).join('; ');
  19123. }
  19124. visitConditional(ast, context) {
  19125. return `${ast.condition.visit(this, context)} ? ${ast.trueExp.visit(this, context)} : ${ast.falseExp.visit(this, context)}`;
  19126. }
  19127. visitThisReceiver() {
  19128. return 'this';
  19129. }
  19130. visitImplicitReceiver() {
  19131. return '';
  19132. }
  19133. visitInterpolation(ast, context) {
  19134. return interleave(ast.strings, ast.expressions.map((e) => e.visit(this, context))).join('');
  19135. }
  19136. visitKeyedRead(ast, context) {
  19137. return `${ast.receiver.visit(this, context)}[${ast.key.visit(this, context)}]`;
  19138. }
  19139. visitKeyedWrite(ast, context) {
  19140. return `${ast.receiver.visit(this, context)}[${ast.key.visit(this, context)}] = ${ast.value.visit(this, context)}`;
  19141. }
  19142. visitLiteralArray(ast, context) {
  19143. return `[${ast.expressions.map((e) => e.visit(this, context)).join(', ')}]`;
  19144. }
  19145. visitLiteralMap(ast, context) {
  19146. return `{${zip(ast.keys.map((literal) => (literal.quoted ? `'${literal.key}'` : literal.key)), ast.values.map((value) => value.visit(this, context)))
  19147. .map(([key, value]) => `${key}: ${value}`)
  19148. .join(', ')}}`;
  19149. }
  19150. visitLiteralPrimitive(ast) {
  19151. if (ast.value === null)
  19152. return 'null';
  19153. switch (typeof ast.value) {
  19154. case 'number':
  19155. case 'boolean':
  19156. return ast.value.toString();
  19157. case 'undefined':
  19158. return 'undefined';
  19159. case 'string':
  19160. return `'${ast.value.replace(/'/g, `\\'`)}'`;
  19161. default:
  19162. throw new Error(`Unsupported primitive type: ${ast.value}`);
  19163. }
  19164. }
  19165. visitPipe(ast, context) {
  19166. return `${ast.exp.visit(this, context)} | ${ast.name}`;
  19167. }
  19168. visitPrefixNot(ast, context) {
  19169. return `!${ast.expression.visit(this, context)}`;
  19170. }
  19171. visitNonNullAssert(ast, context) {
  19172. return `${ast.expression.visit(this, context)}!`;
  19173. }
  19174. visitPropertyRead(ast, context) {
  19175. if (ast.receiver instanceof ImplicitReceiver) {
  19176. return ast.name;
  19177. }
  19178. else {
  19179. return `${ast.receiver.visit(this, context)}.${ast.name}`;
  19180. }
  19181. }
  19182. visitPropertyWrite(ast, context) {
  19183. if (ast.receiver instanceof ImplicitReceiver) {
  19184. return `${ast.name} = ${ast.value.visit(this, context)}`;
  19185. }
  19186. else {
  19187. return `${ast.receiver.visit(this, context)}.${ast.name} = ${ast.value.visit(this, context)}`;
  19188. }
  19189. }
  19190. visitSafePropertyRead(ast, context) {
  19191. return `${ast.receiver.visit(this, context)}?.${ast.name}`;
  19192. }
  19193. visitSafeKeyedRead(ast, context) {
  19194. return `${ast.receiver.visit(this, context)}?.[${ast.key.visit(this, context)}]`;
  19195. }
  19196. visitCall(ast, context) {
  19197. return `${ast.receiver.visit(this, context)}(${ast.args
  19198. .map((e) => e.visit(this, context))
  19199. .join(', ')})`;
  19200. }
  19201. visitSafeCall(ast, context) {
  19202. return `${ast.receiver.visit(this, context)}?.(${ast.args
  19203. .map((e) => e.visit(this, context))
  19204. .join(', ')})`;
  19205. }
  19206. visitTypeofExpression(ast, context) {
  19207. return `typeof ${ast.expression.visit(this, context)}`;
  19208. }
  19209. visitASTWithSource(ast, context) {
  19210. return ast.ast.visit(this, context);
  19211. }
  19212. visitTemplateLiteral(ast, context) {
  19213. let result = '';
  19214. for (let i = 0; i < ast.elements.length; i++) {
  19215. result += ast.elements[i].visit(this, context);
  19216. const expression = i < ast.expressions.length ? ast.expressions[i] : null;
  19217. if (expression !== null) {
  19218. result += '${' + expression.visit(this, context) + '}';
  19219. }
  19220. }
  19221. return '`' + result + '`';
  19222. }
  19223. visitTemplateLiteralElement(ast, context) {
  19224. return ast.text;
  19225. }
  19226. }
  19227. /** Zips the two input arrays into a single array of pairs of elements at the same index. */
  19228. function zip(left, right) {
  19229. if (left.length !== right.length)
  19230. throw new Error('Array lengths must match');
  19231. return left.map((l, i) => [l, right[i]]);
  19232. }
  19233. /**
  19234. * Interleaves the two arrays, starting with the first item on the left, then the first item
  19235. * on the right, second item from the left, and so on. When the first array's items are exhausted,
  19236. * the remaining items from the other array are included with no interleaving.
  19237. */
  19238. function interleave(left, right) {
  19239. const result = [];
  19240. for (let index = 0; index < Math.max(left.length, right.length); index++) {
  19241. if (index < left.length)
  19242. result.push(left[index]);
  19243. if (index < right.length)
  19244. result.push(right[index]);
  19245. }
  19246. return result;
  19247. }
  19248. // =================================================================================================
  19249. // =================================================================================================
  19250. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  19251. // =================================================================================================
  19252. // =================================================================================================
  19253. //
  19254. // DO NOT EDIT THIS LIST OF SECURITY SENSITIVE PROPERTIES WITHOUT A SECURITY REVIEW!
  19255. // Reach out to mprobst for details.
  19256. //
  19257. // =================================================================================================
  19258. /** Map from tagName|propertyName to SecurityContext. Properties applying to all tags use '*'. */
  19259. let _SECURITY_SCHEMA;
  19260. function SECURITY_SCHEMA() {
  19261. if (!_SECURITY_SCHEMA) {
  19262. _SECURITY_SCHEMA = {};
  19263. // Case is insignificant below, all element and attribute names are lower-cased for lookup.
  19264. registerContext(SecurityContext.HTML, ['iframe|srcdoc', '*|innerHTML', '*|outerHTML']);
  19265. registerContext(SecurityContext.STYLE, ['*|style']);
  19266. // NB: no SCRIPT contexts here, they are never allowed due to the parser stripping them.
  19267. registerContext(SecurityContext.URL, [
  19268. '*|formAction',
  19269. 'area|href',
  19270. 'area|ping',
  19271. 'audio|src',
  19272. 'a|href',
  19273. 'a|ping',
  19274. 'blockquote|cite',
  19275. 'body|background',
  19276. 'del|cite',
  19277. 'form|action',
  19278. 'img|src',
  19279. 'input|src',
  19280. 'ins|cite',
  19281. 'q|cite',
  19282. 'source|src',
  19283. 'track|src',
  19284. 'video|poster',
  19285. 'video|src',
  19286. ]);
  19287. registerContext(SecurityContext.RESOURCE_URL, [
  19288. 'applet|code',
  19289. 'applet|codebase',
  19290. 'base|href',
  19291. 'embed|src',
  19292. 'frame|src',
  19293. 'head|profile',
  19294. 'html|manifest',
  19295. 'iframe|src',
  19296. 'link|href',
  19297. 'media|src',
  19298. 'object|codebase',
  19299. 'object|data',
  19300. 'script|src',
  19301. ]);
  19302. }
  19303. return _SECURITY_SCHEMA;
  19304. }
  19305. function registerContext(ctx, specs) {
  19306. for (const spec of specs)
  19307. _SECURITY_SCHEMA[spec.toLowerCase()] = ctx;
  19308. }
  19309. /**
  19310. * The set of security-sensitive attributes of an `<iframe>` that *must* be
  19311. * applied as a static attribute only. This ensures that all security-sensitive
  19312. * attributes are taken into account while creating an instance of an `<iframe>`
  19313. * at runtime.
  19314. *
  19315. * Note: avoid using this set directly, use the `isIframeSecuritySensitiveAttr` function
  19316. * in the code instead.
  19317. */
  19318. const IFRAME_SECURITY_SENSITIVE_ATTRS = new Set([
  19319. 'sandbox',
  19320. 'allow',
  19321. 'allowfullscreen',
  19322. 'referrerpolicy',
  19323. 'csp',
  19324. 'fetchpriority',
  19325. ]);
  19326. /**
  19327. * Checks whether a given attribute name might represent a security-sensitive
  19328. * attribute of an <iframe>.
  19329. */
  19330. function isIframeSecuritySensitiveAttr(attrName) {
  19331. // The `setAttribute` DOM API is case-insensitive, so we lowercase the value
  19332. // before checking it against a known security-sensitive attributes.
  19333. return IFRAME_SECURITY_SENSITIVE_ATTRS.has(attrName.toLowerCase());
  19334. }
  19335. class ElementSchemaRegistry {
  19336. }
  19337. const BOOLEAN = 'boolean';
  19338. const NUMBER = 'number';
  19339. const STRING = 'string';
  19340. const OBJECT = 'object';
  19341. /**
  19342. * This array represents the DOM schema. It encodes inheritance, properties, and events.
  19343. *
  19344. * ## Overview
  19345. *
  19346. * Each line represents one kind of element. The `element_inheritance` and properties are joined
  19347. * using `element_inheritance|properties` syntax.
  19348. *
  19349. * ## Element Inheritance
  19350. *
  19351. * The `element_inheritance` can be further subdivided as `element1,element2,...^parentElement`.
  19352. * Here the individual elements are separated by `,` (commas). Every element in the list
  19353. * has identical properties.
  19354. *
  19355. * An `element` may inherit additional properties from `parentElement` If no `^parentElement` is
  19356. * specified then `""` (blank) element is assumed.
  19357. *
  19358. * NOTE: The blank element inherits from root `[Element]` element, the super element of all
  19359. * elements.
  19360. *
  19361. * NOTE an element prefix such as `:svg:` has no special meaning to the schema.
  19362. *
  19363. * ## Properties
  19364. *
  19365. * Each element has a set of properties separated by `,` (commas). Each property can be prefixed
  19366. * by a special character designating its type:
  19367. *
  19368. * - (no prefix): property is a string.
  19369. * - `*`: property represents an event.
  19370. * - `!`: property is a boolean.
  19371. * - `#`: property is a number.
  19372. * - `%`: property is an object.
  19373. *
  19374. * ## Query
  19375. *
  19376. * The class creates an internal squas representation which allows to easily answer the query of
  19377. * if a given property exist on a given element.
  19378. *
  19379. * NOTE: We don't yet support querying for types or events.
  19380. * NOTE: This schema is auto extracted from `schema_extractor.ts` located in the test folder,
  19381. * see dom_element_schema_registry_spec.ts
  19382. */
  19383. // =================================================================================================
  19384. // =================================================================================================
  19385. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  19386. // =================================================================================================
  19387. // =================================================================================================
  19388. //
  19389. // DO NOT EDIT THIS DOM SCHEMA WITHOUT A SECURITY REVIEW!
  19390. //
  19391. // Newly added properties must be security reviewed and assigned an appropriate SecurityContext in
  19392. // dom_security_schema.ts. Reach out to mprobst & rjamet for details.
  19393. //
  19394. // =================================================================================================
  19395. const SCHEMA = [
  19396. '[Element]|textContent,%ariaAtomic,%ariaAutoComplete,%ariaBusy,%ariaChecked,%ariaColCount,%ariaColIndex,%ariaColSpan,%ariaCurrent,%ariaDescription,%ariaDisabled,%ariaExpanded,%ariaHasPopup,%ariaHidden,%ariaKeyShortcuts,%ariaLabel,%ariaLevel,%ariaLive,%ariaModal,%ariaMultiLine,%ariaMultiSelectable,%ariaOrientation,%ariaPlaceholder,%ariaPosInSet,%ariaPressed,%ariaReadOnly,%ariaRelevant,%ariaRequired,%ariaRoleDescription,%ariaRowCount,%ariaRowIndex,%ariaRowSpan,%ariaSelected,%ariaSetSize,%ariaSort,%ariaValueMax,%ariaValueMin,%ariaValueNow,%ariaValueText,%classList,className,elementTiming,id,innerHTML,*beforecopy,*beforecut,*beforepaste,*fullscreenchange,*fullscreenerror,*search,*webkitfullscreenchange,*webkitfullscreenerror,outerHTML,%part,#scrollLeft,#scrollTop,slot' +
  19397. /* added manually to avoid breaking changes */
  19398. ',*message,*mozfullscreenchange,*mozfullscreenerror,*mozpointerlockchange,*mozpointerlockerror,*webglcontextcreationerror,*webglcontextlost,*webglcontextrestored',
  19399. '[HTMLElement]^[Element]|accessKey,autocapitalize,!autofocus,contentEditable,dir,!draggable,enterKeyHint,!hidden,!inert,innerText,inputMode,lang,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,outerText,!spellcheck,%style,#tabIndex,title,!translate,virtualKeyboardPolicy',
  19400. 'abbr,address,article,aside,b,bdi,bdo,cite,content,code,dd,dfn,dt,em,figcaption,figure,footer,header,hgroup,i,kbd,main,mark,nav,noscript,rb,rp,rt,rtc,ruby,s,samp,search,section,small,strong,sub,sup,u,var,wbr^[HTMLElement]|accessKey,autocapitalize,!autofocus,contentEditable,dir,!draggable,enterKeyHint,!hidden,innerText,inputMode,lang,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,outerText,!spellcheck,%style,#tabIndex,title,!translate,virtualKeyboardPolicy',
  19401. 'media^[HTMLElement]|!autoplay,!controls,%controlsList,%crossOrigin,#currentTime,!defaultMuted,#defaultPlaybackRate,!disableRemotePlayback,!loop,!muted,*encrypted,*waitingforkey,#playbackRate,preload,!preservesPitch,src,%srcObject,#volume',
  19402. ':svg:^[HTMLElement]|!autofocus,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,%style,#tabIndex',
  19403. ':svg:graphics^:svg:|',
  19404. ':svg:animation^:svg:|*begin,*end,*repeat',
  19405. ':svg:geometry^:svg:|',
  19406. ':svg:componentTransferFunction^:svg:|',
  19407. ':svg:gradient^:svg:|',
  19408. ':svg:textContent^:svg:graphics|',
  19409. ':svg:textPositioning^:svg:textContent|',
  19410. 'a^[HTMLElement]|charset,coords,download,hash,host,hostname,href,hreflang,name,password,pathname,ping,port,protocol,referrerPolicy,rel,%relList,rev,search,shape,target,text,type,username',
  19411. 'area^[HTMLElement]|alt,coords,download,hash,host,hostname,href,!noHref,password,pathname,ping,port,protocol,referrerPolicy,rel,%relList,search,shape,target,username',
  19412. 'audio^media|',
  19413. 'br^[HTMLElement]|clear',
  19414. 'base^[HTMLElement]|href,target',
  19415. 'body^[HTMLElement]|aLink,background,bgColor,link,*afterprint,*beforeprint,*beforeunload,*blur,*error,*focus,*hashchange,*languagechange,*load,*message,*messageerror,*offline,*online,*pagehide,*pageshow,*popstate,*rejectionhandled,*resize,*scroll,*storage,*unhandledrejection,*unload,text,vLink',
  19416. 'button^[HTMLElement]|!disabled,formAction,formEnctype,formMethod,!formNoValidate,formTarget,name,type,value',
  19417. 'canvas^[HTMLElement]|#height,#width',
  19418. 'content^[HTMLElement]|select',
  19419. 'dl^[HTMLElement]|!compact',
  19420. 'data^[HTMLElement]|value',
  19421. 'datalist^[HTMLElement]|',
  19422. 'details^[HTMLElement]|!open',
  19423. 'dialog^[HTMLElement]|!open,returnValue',
  19424. 'dir^[HTMLElement]|!compact',
  19425. 'div^[HTMLElement]|align',
  19426. 'embed^[HTMLElement]|align,height,name,src,type,width',
  19427. 'fieldset^[HTMLElement]|!disabled,name',
  19428. 'font^[HTMLElement]|color,face,size',
  19429. 'form^[HTMLElement]|acceptCharset,action,autocomplete,encoding,enctype,method,name,!noValidate,target',
  19430. 'frame^[HTMLElement]|frameBorder,longDesc,marginHeight,marginWidth,name,!noResize,scrolling,src',
  19431. 'frameset^[HTMLElement]|cols,*afterprint,*beforeprint,*beforeunload,*blur,*error,*focus,*hashchange,*languagechange,*load,*message,*messageerror,*offline,*online,*pagehide,*pageshow,*popstate,*rejectionhandled,*resize,*scroll,*storage,*unhandledrejection,*unload,rows',
  19432. 'hr^[HTMLElement]|align,color,!noShade,size,width',
  19433. 'head^[HTMLElement]|',
  19434. 'h1,h2,h3,h4,h5,h6^[HTMLElement]|align',
  19435. 'html^[HTMLElement]|version',
  19436. 'iframe^[HTMLElement]|align,allow,!allowFullscreen,!allowPaymentRequest,csp,frameBorder,height,loading,longDesc,marginHeight,marginWidth,name,referrerPolicy,%sandbox,scrolling,src,srcdoc,width',
  19437. 'img^[HTMLElement]|align,alt,border,%crossOrigin,decoding,#height,#hspace,!isMap,loading,longDesc,lowsrc,name,referrerPolicy,sizes,src,srcset,useMap,#vspace,#width',
  19438. 'input^[HTMLElement]|accept,align,alt,autocomplete,!checked,!defaultChecked,defaultValue,dirName,!disabled,%files,formAction,formEnctype,formMethod,!formNoValidate,formTarget,#height,!incremental,!indeterminate,max,#maxLength,min,#minLength,!multiple,name,pattern,placeholder,!readOnly,!required,selectionDirection,#selectionEnd,#selectionStart,#size,src,step,type,useMap,value,%valueAsDate,#valueAsNumber,#width',
  19439. 'li^[HTMLElement]|type,#value',
  19440. 'label^[HTMLElement]|htmlFor',
  19441. 'legend^[HTMLElement]|align',
  19442. 'link^[HTMLElement]|as,charset,%crossOrigin,!disabled,href,hreflang,imageSizes,imageSrcset,integrity,media,referrerPolicy,rel,%relList,rev,%sizes,target,type',
  19443. 'map^[HTMLElement]|name',
  19444. 'marquee^[HTMLElement]|behavior,bgColor,direction,height,#hspace,#loop,#scrollAmount,#scrollDelay,!trueSpeed,#vspace,width',
  19445. 'menu^[HTMLElement]|!compact',
  19446. 'meta^[HTMLElement]|content,httpEquiv,media,name,scheme',
  19447. 'meter^[HTMLElement]|#high,#low,#max,#min,#optimum,#value',
  19448. 'ins,del^[HTMLElement]|cite,dateTime',
  19449. 'ol^[HTMLElement]|!compact,!reversed,#start,type',
  19450. 'object^[HTMLElement]|align,archive,border,code,codeBase,codeType,data,!declare,height,#hspace,name,standby,type,useMap,#vspace,width',
  19451. 'optgroup^[HTMLElement]|!disabled,label',
  19452. 'option^[HTMLElement]|!defaultSelected,!disabled,label,!selected,text,value',
  19453. 'output^[HTMLElement]|defaultValue,%htmlFor,name,value',
  19454. 'p^[HTMLElement]|align',
  19455. 'param^[HTMLElement]|name,type,value,valueType',
  19456. 'picture^[HTMLElement]|',
  19457. 'pre^[HTMLElement]|#width',
  19458. 'progress^[HTMLElement]|#max,#value',
  19459. 'q,blockquote,cite^[HTMLElement]|',
  19460. 'script^[HTMLElement]|!async,charset,%crossOrigin,!defer,event,htmlFor,integrity,!noModule,%referrerPolicy,src,text,type',
  19461. 'select^[HTMLElement]|autocomplete,!disabled,#length,!multiple,name,!required,#selectedIndex,#size,value',
  19462. 'slot^[HTMLElement]|name',
  19463. 'source^[HTMLElement]|#height,media,sizes,src,srcset,type,#width',
  19464. 'span^[HTMLElement]|',
  19465. 'style^[HTMLElement]|!disabled,media,type',
  19466. 'search^[HTMLELement]|',
  19467. 'caption^[HTMLElement]|align',
  19468. 'th,td^[HTMLElement]|abbr,align,axis,bgColor,ch,chOff,#colSpan,headers,height,!noWrap,#rowSpan,scope,vAlign,width',
  19469. 'col,colgroup^[HTMLElement]|align,ch,chOff,#span,vAlign,width',
  19470. 'table^[HTMLElement]|align,bgColor,border,%caption,cellPadding,cellSpacing,frame,rules,summary,%tFoot,%tHead,width',
  19471. 'tr^[HTMLElement]|align,bgColor,ch,chOff,vAlign',
  19472. 'tfoot,thead,tbody^[HTMLElement]|align,ch,chOff,vAlign',
  19473. 'template^[HTMLElement]|',
  19474. 'textarea^[HTMLElement]|autocomplete,#cols,defaultValue,dirName,!disabled,#maxLength,#minLength,name,placeholder,!readOnly,!required,#rows,selectionDirection,#selectionEnd,#selectionStart,value,wrap',
  19475. 'time^[HTMLElement]|dateTime',
  19476. 'title^[HTMLElement]|text',
  19477. 'track^[HTMLElement]|!default,kind,label,src,srclang',
  19478. 'ul^[HTMLElement]|!compact,type',
  19479. 'unknown^[HTMLElement]|',
  19480. 'video^media|!disablePictureInPicture,#height,*enterpictureinpicture,*leavepictureinpicture,!playsInline,poster,#width',
  19481. ':svg:a^:svg:graphics|',
  19482. ':svg:animate^:svg:animation|',
  19483. ':svg:animateMotion^:svg:animation|',
  19484. ':svg:animateTransform^:svg:animation|',
  19485. ':svg:circle^:svg:geometry|',
  19486. ':svg:clipPath^:svg:graphics|',
  19487. ':svg:defs^:svg:graphics|',
  19488. ':svg:desc^:svg:|',
  19489. ':svg:discard^:svg:|',
  19490. ':svg:ellipse^:svg:geometry|',
  19491. ':svg:feBlend^:svg:|',
  19492. ':svg:feColorMatrix^:svg:|',
  19493. ':svg:feComponentTransfer^:svg:|',
  19494. ':svg:feComposite^:svg:|',
  19495. ':svg:feConvolveMatrix^:svg:|',
  19496. ':svg:feDiffuseLighting^:svg:|',
  19497. ':svg:feDisplacementMap^:svg:|',
  19498. ':svg:feDistantLight^:svg:|',
  19499. ':svg:feDropShadow^:svg:|',
  19500. ':svg:feFlood^:svg:|',
  19501. ':svg:feFuncA^:svg:componentTransferFunction|',
  19502. ':svg:feFuncB^:svg:componentTransferFunction|',
  19503. ':svg:feFuncG^:svg:componentTransferFunction|',
  19504. ':svg:feFuncR^:svg:componentTransferFunction|',
  19505. ':svg:feGaussianBlur^:svg:|',
  19506. ':svg:feImage^:svg:|',
  19507. ':svg:feMerge^:svg:|',
  19508. ':svg:feMergeNode^:svg:|',
  19509. ':svg:feMorphology^:svg:|',
  19510. ':svg:feOffset^:svg:|',
  19511. ':svg:fePointLight^:svg:|',
  19512. ':svg:feSpecularLighting^:svg:|',
  19513. ':svg:feSpotLight^:svg:|',
  19514. ':svg:feTile^:svg:|',
  19515. ':svg:feTurbulence^:svg:|',
  19516. ':svg:filter^:svg:|',
  19517. ':svg:foreignObject^:svg:graphics|',
  19518. ':svg:g^:svg:graphics|',
  19519. ':svg:image^:svg:graphics|decoding',
  19520. ':svg:line^:svg:geometry|',
  19521. ':svg:linearGradient^:svg:gradient|',
  19522. ':svg:mpath^:svg:|',
  19523. ':svg:marker^:svg:|',
  19524. ':svg:mask^:svg:|',
  19525. ':svg:metadata^:svg:|',
  19526. ':svg:path^:svg:geometry|',
  19527. ':svg:pattern^:svg:|',
  19528. ':svg:polygon^:svg:geometry|',
  19529. ':svg:polyline^:svg:geometry|',
  19530. ':svg:radialGradient^:svg:gradient|',
  19531. ':svg:rect^:svg:geometry|',
  19532. ':svg:svg^:svg:graphics|#currentScale,#zoomAndPan',
  19533. ':svg:script^:svg:|type',
  19534. ':svg:set^:svg:animation|',
  19535. ':svg:stop^:svg:|',
  19536. ':svg:style^:svg:|!disabled,media,title,type',
  19537. ':svg:switch^:svg:graphics|',
  19538. ':svg:symbol^:svg:|',
  19539. ':svg:tspan^:svg:textPositioning|',
  19540. ':svg:text^:svg:textPositioning|',
  19541. ':svg:textPath^:svg:textContent|',
  19542. ':svg:title^:svg:|',
  19543. ':svg:use^:svg:graphics|',
  19544. ':svg:view^:svg:|#zoomAndPan',
  19545. 'data^[HTMLElement]|value',
  19546. 'keygen^[HTMLElement]|!autofocus,challenge,!disabled,form,keytype,name',
  19547. 'menuitem^[HTMLElement]|type,label,icon,!disabled,!checked,radiogroup,!default',
  19548. 'summary^[HTMLElement]|',
  19549. 'time^[HTMLElement]|dateTime',
  19550. ':svg:cursor^:svg:|',
  19551. ':math:^[HTMLElement]|!autofocus,nonce,*abort,*animationend,*animationiteration,*animationstart,*auxclick,*beforeinput,*beforematch,*beforetoggle,*beforexrselect,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contentvisibilityautostatechange,*contextlost,*contextmenu,*contextrestored,*copy,*cuechange,*cut,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*formdata,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*paste,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerrawupdate,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*scrollend,*securitypolicyviolation,*seeked,*seeking,*select,*selectionchange,*selectstart,*slotchange,*stalled,*submit,*suspend,*timeupdate,*toggle,*transitioncancel,*transitionend,*transitionrun,*transitionstart,*volumechange,*waiting,*webkitanimationend,*webkitanimationiteration,*webkitanimationstart,*webkittransitionend,*wheel,%style,#tabIndex',
  19552. ':math:math^:math:|',
  19553. ':math:maction^:math:|',
  19554. ':math:menclose^:math:|',
  19555. ':math:merror^:math:|',
  19556. ':math:mfenced^:math:|',
  19557. ':math:mfrac^:math:|',
  19558. ':math:mi^:math:|',
  19559. ':math:mmultiscripts^:math:|',
  19560. ':math:mn^:math:|',
  19561. ':math:mo^:math:|',
  19562. ':math:mover^:math:|',
  19563. ':math:mpadded^:math:|',
  19564. ':math:mphantom^:math:|',
  19565. ':math:mroot^:math:|',
  19566. ':math:mrow^:math:|',
  19567. ':math:ms^:math:|',
  19568. ':math:mspace^:math:|',
  19569. ':math:msqrt^:math:|',
  19570. ':math:mstyle^:math:|',
  19571. ':math:msub^:math:|',
  19572. ':math:msubsup^:math:|',
  19573. ':math:msup^:math:|',
  19574. ':math:mtable^:math:|',
  19575. ':math:mtd^:math:|',
  19576. ':math:mtext^:math:|',
  19577. ':math:mtr^:math:|',
  19578. ':math:munder^:math:|',
  19579. ':math:munderover^:math:|',
  19580. ':math:semantics^:math:|',
  19581. ];
  19582. const _ATTR_TO_PROP = new Map(Object.entries({
  19583. 'class': 'className',
  19584. 'for': 'htmlFor',
  19585. 'formaction': 'formAction',
  19586. 'innerHtml': 'innerHTML',
  19587. 'readonly': 'readOnly',
  19588. 'tabindex': 'tabIndex',
  19589. }));
  19590. // Invert _ATTR_TO_PROP.
  19591. const _PROP_TO_ATTR = Array.from(_ATTR_TO_PROP).reduce((inverted, [propertyName, attributeName]) => {
  19592. inverted.set(propertyName, attributeName);
  19593. return inverted;
  19594. }, new Map());
  19595. class DomElementSchemaRegistry extends ElementSchemaRegistry {
  19596. _schema = new Map();
  19597. // We don't allow binding to events for security reasons. Allowing event bindings would almost
  19598. // certainly introduce bad XSS vulnerabilities. Instead, we store events in a separate schema.
  19599. _eventSchema = new Map();
  19600. constructor() {
  19601. super();
  19602. SCHEMA.forEach((encodedType) => {
  19603. const type = new Map();
  19604. const events = new Set();
  19605. const [strType, strProperties] = encodedType.split('|');
  19606. const properties = strProperties.split(',');
  19607. const [typeNames, superName] = strType.split('^');
  19608. typeNames.split(',').forEach((tag) => {
  19609. this._schema.set(tag.toLowerCase(), type);
  19610. this._eventSchema.set(tag.toLowerCase(), events);
  19611. });
  19612. const superType = superName && this._schema.get(superName.toLowerCase());
  19613. if (superType) {
  19614. for (const [prop, value] of superType) {
  19615. type.set(prop, value);
  19616. }
  19617. for (const superEvent of this._eventSchema.get(superName.toLowerCase())) {
  19618. events.add(superEvent);
  19619. }
  19620. }
  19621. properties.forEach((property) => {
  19622. if (property.length > 0) {
  19623. switch (property[0]) {
  19624. case '*':
  19625. events.add(property.substring(1));
  19626. break;
  19627. case '!':
  19628. type.set(property.substring(1), BOOLEAN);
  19629. break;
  19630. case '#':
  19631. type.set(property.substring(1), NUMBER);
  19632. break;
  19633. case '%':
  19634. type.set(property.substring(1), OBJECT);
  19635. break;
  19636. default:
  19637. type.set(property, STRING);
  19638. }
  19639. }
  19640. });
  19641. });
  19642. }
  19643. hasProperty(tagName, propName, schemaMetas) {
  19644. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  19645. return true;
  19646. }
  19647. if (tagName.indexOf('-') > -1) {
  19648. if (isNgContainer(tagName) || isNgContent(tagName)) {
  19649. return false;
  19650. }
  19651. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  19652. // Can't tell now as we don't know which properties a custom element will get
  19653. // once it is instantiated
  19654. return true;
  19655. }
  19656. }
  19657. const elementProperties = this._schema.get(tagName.toLowerCase()) || this._schema.get('unknown');
  19658. return elementProperties.has(propName);
  19659. }
  19660. hasElement(tagName, schemaMetas) {
  19661. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  19662. return true;
  19663. }
  19664. if (tagName.indexOf('-') > -1) {
  19665. if (isNgContainer(tagName) || isNgContent(tagName)) {
  19666. return true;
  19667. }
  19668. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  19669. // Allow any custom elements
  19670. return true;
  19671. }
  19672. }
  19673. return this._schema.has(tagName.toLowerCase());
  19674. }
  19675. /**
  19676. * securityContext returns the security context for the given property on the given DOM tag.
  19677. *
  19678. * Tag and property name are statically known and cannot change at runtime, i.e. it is not
  19679. * possible to bind a value into a changing attribute or tag name.
  19680. *
  19681. * The filtering is based on a list of allowed tags|attributes. All attributes in the schema
  19682. * above are assumed to have the 'NONE' security context, i.e. that they are safe inert
  19683. * string values. Only specific well known attack vectors are assigned their appropriate context.
  19684. */
  19685. securityContext(tagName, propName, isAttribute) {
  19686. if (isAttribute) {
  19687. // NB: For security purposes, use the mapped property name, not the attribute name.
  19688. propName = this.getMappedPropName(propName);
  19689. }
  19690. // Make sure comparisons are case insensitive, so that case differences between attribute and
  19691. // property names do not have a security impact.
  19692. tagName = tagName.toLowerCase();
  19693. propName = propName.toLowerCase();
  19694. let ctx = SECURITY_SCHEMA()[tagName + '|' + propName];
  19695. if (ctx) {
  19696. return ctx;
  19697. }
  19698. ctx = SECURITY_SCHEMA()['*|' + propName];
  19699. return ctx ? ctx : SecurityContext.NONE;
  19700. }
  19701. getMappedPropName(propName) {
  19702. return _ATTR_TO_PROP.get(propName) ?? propName;
  19703. }
  19704. getDefaultComponentElementName() {
  19705. return 'ng-component';
  19706. }
  19707. validateProperty(name) {
  19708. if (name.toLowerCase().startsWith('on')) {
  19709. const msg = `Binding to event property '${name}' is disallowed for security reasons, ` +
  19710. `please use (${name.slice(2)})=...` +
  19711. `\nIf '${name}' is a directive input, make sure the directive is imported by the` +
  19712. ` current module.`;
  19713. return { error: true, msg: msg };
  19714. }
  19715. else {
  19716. return { error: false };
  19717. }
  19718. }
  19719. validateAttribute(name) {
  19720. if (name.toLowerCase().startsWith('on')) {
  19721. const msg = `Binding to event attribute '${name}' is disallowed for security reasons, ` +
  19722. `please use (${name.slice(2)})=...`;
  19723. return { error: true, msg: msg };
  19724. }
  19725. else {
  19726. return { error: false };
  19727. }
  19728. }
  19729. allKnownElementNames() {
  19730. return Array.from(this._schema.keys());
  19731. }
  19732. allKnownAttributesOfElement(tagName) {
  19733. const elementProperties = this._schema.get(tagName.toLowerCase()) || this._schema.get('unknown');
  19734. // Convert properties to attributes.
  19735. return Array.from(elementProperties.keys()).map((prop) => _PROP_TO_ATTR.get(prop) ?? prop);
  19736. }
  19737. allKnownEventsOfElement(tagName) {
  19738. return Array.from(this._eventSchema.get(tagName.toLowerCase()) ?? []);
  19739. }
  19740. normalizeAnimationStyleProperty(propName) {
  19741. return dashCaseToCamelCase(propName);
  19742. }
  19743. normalizeAnimationStyleValue(camelCaseProp, userProvidedProp, val) {
  19744. let unit = '';
  19745. const strVal = val.toString().trim();
  19746. let errorMsg = null;
  19747. if (_isPixelDimensionStyle(camelCaseProp) && val !== 0 && val !== '0') {
  19748. if (typeof val === 'number') {
  19749. unit = 'px';
  19750. }
  19751. else {
  19752. const valAndSuffixMatch = val.match(/^[+-]?[\d\.]+([a-z]*)$/);
  19753. if (valAndSuffixMatch && valAndSuffixMatch[1].length == 0) {
  19754. errorMsg = `Please provide a CSS unit value for ${userProvidedProp}:${val}`;
  19755. }
  19756. }
  19757. }
  19758. return { error: errorMsg, value: strVal + unit };
  19759. }
  19760. }
  19761. function _isPixelDimensionStyle(prop) {
  19762. switch (prop) {
  19763. case 'width':
  19764. case 'height':
  19765. case 'minWidth':
  19766. case 'minHeight':
  19767. case 'maxWidth':
  19768. case 'maxHeight':
  19769. case 'left':
  19770. case 'top':
  19771. case 'bottom':
  19772. case 'right':
  19773. case 'fontSize':
  19774. case 'outlineWidth':
  19775. case 'outlineOffset':
  19776. case 'paddingTop':
  19777. case 'paddingLeft':
  19778. case 'paddingBottom':
  19779. case 'paddingRight':
  19780. case 'marginTop':
  19781. case 'marginLeft':
  19782. case 'marginBottom':
  19783. case 'marginRight':
  19784. case 'borderRadius':
  19785. case 'borderWidth':
  19786. case 'borderTopWidth':
  19787. case 'borderLeftWidth':
  19788. case 'borderRightWidth':
  19789. case 'borderBottomWidth':
  19790. case 'textIndent':
  19791. return true;
  19792. default:
  19793. return false;
  19794. }
  19795. }
  19796. class HtmlTagDefinition {
  19797. closedByChildren = {};
  19798. contentType;
  19799. closedByParent = false;
  19800. implicitNamespacePrefix;
  19801. isVoid;
  19802. ignoreFirstLf;
  19803. canSelfClose;
  19804. preventNamespaceInheritance;
  19805. constructor({ closedByChildren, implicitNamespacePrefix, contentType = exports.TagContentType.PARSABLE_DATA, closedByParent = false, isVoid = false, ignoreFirstLf = false, preventNamespaceInheritance = false, canSelfClose = false, } = {}) {
  19806. if (closedByChildren && closedByChildren.length > 0) {
  19807. closedByChildren.forEach((tagName) => (this.closedByChildren[tagName] = true));
  19808. }
  19809. this.isVoid = isVoid;
  19810. this.closedByParent = closedByParent || isVoid;
  19811. this.implicitNamespacePrefix = implicitNamespacePrefix || null;
  19812. this.contentType = contentType;
  19813. this.ignoreFirstLf = ignoreFirstLf;
  19814. this.preventNamespaceInheritance = preventNamespaceInheritance;
  19815. this.canSelfClose = canSelfClose ?? isVoid;
  19816. }
  19817. isClosedByChild(name) {
  19818. return this.isVoid || name.toLowerCase() in this.closedByChildren;
  19819. }
  19820. getContentType(prefix) {
  19821. if (typeof this.contentType === 'object') {
  19822. const overrideType = prefix === undefined ? undefined : this.contentType[prefix];
  19823. return overrideType ?? this.contentType.default;
  19824. }
  19825. return this.contentType;
  19826. }
  19827. }
  19828. let DEFAULT_TAG_DEFINITION;
  19829. // see https://www.w3.org/TR/html51/syntax.html#optional-tags
  19830. // This implementation does not fully conform to the HTML5 spec.
  19831. let TAG_DEFINITIONS;
  19832. function getHtmlTagDefinition(tagName) {
  19833. if (!TAG_DEFINITIONS) {
  19834. DEFAULT_TAG_DEFINITION = new HtmlTagDefinition({ canSelfClose: true });
  19835. TAG_DEFINITIONS = Object.assign(Object.create(null), {
  19836. 'base': new HtmlTagDefinition({ isVoid: true }),
  19837. 'meta': new HtmlTagDefinition({ isVoid: true }),
  19838. 'area': new HtmlTagDefinition({ isVoid: true }),
  19839. 'embed': new HtmlTagDefinition({ isVoid: true }),
  19840. 'link': new HtmlTagDefinition({ isVoid: true }),
  19841. 'img': new HtmlTagDefinition({ isVoid: true }),
  19842. 'input': new HtmlTagDefinition({ isVoid: true }),
  19843. 'param': new HtmlTagDefinition({ isVoid: true }),
  19844. 'hr': new HtmlTagDefinition({ isVoid: true }),
  19845. 'br': new HtmlTagDefinition({ isVoid: true }),
  19846. 'source': new HtmlTagDefinition({ isVoid: true }),
  19847. 'track': new HtmlTagDefinition({ isVoid: true }),
  19848. 'wbr': new HtmlTagDefinition({ isVoid: true }),
  19849. 'p': new HtmlTagDefinition({
  19850. closedByChildren: [
  19851. 'address',
  19852. 'article',
  19853. 'aside',
  19854. 'blockquote',
  19855. 'div',
  19856. 'dl',
  19857. 'fieldset',
  19858. 'footer',
  19859. 'form',
  19860. 'h1',
  19861. 'h2',
  19862. 'h3',
  19863. 'h4',
  19864. 'h5',
  19865. 'h6',
  19866. 'header',
  19867. 'hgroup',
  19868. 'hr',
  19869. 'main',
  19870. 'nav',
  19871. 'ol',
  19872. 'p',
  19873. 'pre',
  19874. 'section',
  19875. 'table',
  19876. 'ul',
  19877. ],
  19878. closedByParent: true,
  19879. }),
  19880. 'thead': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'] }),
  19881. 'tbody': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'], closedByParent: true }),
  19882. 'tfoot': new HtmlTagDefinition({ closedByChildren: ['tbody'], closedByParent: true }),
  19883. 'tr': new HtmlTagDefinition({ closedByChildren: ['tr'], closedByParent: true }),
  19884. 'td': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  19885. 'th': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  19886. 'col': new HtmlTagDefinition({ isVoid: true }),
  19887. 'svg': new HtmlTagDefinition({ implicitNamespacePrefix: 'svg' }),
  19888. 'foreignObject': new HtmlTagDefinition({
  19889. // Usually the implicit namespace here would be redundant since it will be inherited from
  19890. // the parent `svg`, but we have to do it for `foreignObject`, because the way the parser
  19891. // works is that the parent node of an end tag is its own start tag which means that
  19892. // the `preventNamespaceInheritance` on `foreignObject` would have it default to the
  19893. // implicit namespace which is `html`, unless specified otherwise.
  19894. implicitNamespacePrefix: 'svg',
  19895. // We want to prevent children of foreignObject from inheriting its namespace, because
  19896. // the point of the element is to allow nodes from other namespaces to be inserted.
  19897. preventNamespaceInheritance: true,
  19898. }),
  19899. 'math': new HtmlTagDefinition({ implicitNamespacePrefix: 'math' }),
  19900. 'li': new HtmlTagDefinition({ closedByChildren: ['li'], closedByParent: true }),
  19901. 'dt': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'] }),
  19902. 'dd': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'], closedByParent: true }),
  19903. 'rb': new HtmlTagDefinition({
  19904. closedByChildren: ['rb', 'rt', 'rtc', 'rp'],
  19905. closedByParent: true,
  19906. }),
  19907. 'rt': new HtmlTagDefinition({
  19908. closedByChildren: ['rb', 'rt', 'rtc', 'rp'],
  19909. closedByParent: true,
  19910. }),
  19911. 'rtc': new HtmlTagDefinition({ closedByChildren: ['rb', 'rtc', 'rp'], closedByParent: true }),
  19912. 'rp': new HtmlTagDefinition({
  19913. closedByChildren: ['rb', 'rt', 'rtc', 'rp'],
  19914. closedByParent: true,
  19915. }),
  19916. 'optgroup': new HtmlTagDefinition({ closedByChildren: ['optgroup'], closedByParent: true }),
  19917. 'option': new HtmlTagDefinition({
  19918. closedByChildren: ['option', 'optgroup'],
  19919. closedByParent: true,
  19920. }),
  19921. 'pre': new HtmlTagDefinition({ ignoreFirstLf: true }),
  19922. 'listing': new HtmlTagDefinition({ ignoreFirstLf: true }),
  19923. 'style': new HtmlTagDefinition({ contentType: exports.TagContentType.RAW_TEXT }),
  19924. 'script': new HtmlTagDefinition({ contentType: exports.TagContentType.RAW_TEXT }),
  19925. 'title': new HtmlTagDefinition({
  19926. // The browser supports two separate `title` tags which have to use
  19927. // a different content type: `HTMLTitleElement` and `SVGTitleElement`
  19928. contentType: {
  19929. default: exports.TagContentType.ESCAPABLE_RAW_TEXT,
  19930. svg: exports.TagContentType.PARSABLE_DATA,
  19931. },
  19932. }),
  19933. 'textarea': new HtmlTagDefinition({
  19934. contentType: exports.TagContentType.ESCAPABLE_RAW_TEXT,
  19935. ignoreFirstLf: true,
  19936. }),
  19937. });
  19938. new DomElementSchemaRegistry().allKnownElementNames().forEach((knownTagName) => {
  19939. if (!TAG_DEFINITIONS[knownTagName] && getNsPrefix(knownTagName) === null) {
  19940. TAG_DEFINITIONS[knownTagName] = new HtmlTagDefinition({ canSelfClose: false });
  19941. }
  19942. });
  19943. }
  19944. // We have to make both a case-sensitive and a case-insensitive lookup, because
  19945. // HTML tag names are case insensitive, whereas some SVG tags are case sensitive.
  19946. return (TAG_DEFINITIONS[tagName] ?? TAG_DEFINITIONS[tagName.toLowerCase()] ?? DEFAULT_TAG_DEFINITION);
  19947. }
  19948. const TAG_TO_PLACEHOLDER_NAMES = {
  19949. 'A': 'LINK',
  19950. 'B': 'BOLD_TEXT',
  19951. 'BR': 'LINE_BREAK',
  19952. 'EM': 'EMPHASISED_TEXT',
  19953. 'H1': 'HEADING_LEVEL1',
  19954. 'H2': 'HEADING_LEVEL2',
  19955. 'H3': 'HEADING_LEVEL3',
  19956. 'H4': 'HEADING_LEVEL4',
  19957. 'H5': 'HEADING_LEVEL5',
  19958. 'H6': 'HEADING_LEVEL6',
  19959. 'HR': 'HORIZONTAL_RULE',
  19960. 'I': 'ITALIC_TEXT',
  19961. 'LI': 'LIST_ITEM',
  19962. 'LINK': 'MEDIA_LINK',
  19963. 'OL': 'ORDERED_LIST',
  19964. 'P': 'PARAGRAPH',
  19965. 'Q': 'QUOTATION',
  19966. 'S': 'STRIKETHROUGH_TEXT',
  19967. 'SMALL': 'SMALL_TEXT',
  19968. 'SUB': 'SUBSTRIPT',
  19969. 'SUP': 'SUPERSCRIPT',
  19970. 'TBODY': 'TABLE_BODY',
  19971. 'TD': 'TABLE_CELL',
  19972. 'TFOOT': 'TABLE_FOOTER',
  19973. 'TH': 'TABLE_HEADER_CELL',
  19974. 'THEAD': 'TABLE_HEADER',
  19975. 'TR': 'TABLE_ROW',
  19976. 'TT': 'MONOSPACED_TEXT',
  19977. 'U': 'UNDERLINED_TEXT',
  19978. 'UL': 'UNORDERED_LIST',
  19979. };
  19980. /**
  19981. * Creates unique names for placeholder with different content.
  19982. *
  19983. * Returns the same placeholder name when the content is identical.
  19984. */
  19985. class PlaceholderRegistry {
  19986. // Count the occurrence of the base name top generate a unique name
  19987. _placeHolderNameCounts = {};
  19988. // Maps signature to placeholder names
  19989. _signatureToName = {};
  19990. getStartTagPlaceholderName(tag, attrs, isVoid) {
  19991. const signature = this._hashTag(tag, attrs, isVoid);
  19992. if (this._signatureToName[signature]) {
  19993. return this._signatureToName[signature];
  19994. }
  19995. const upperTag = tag.toUpperCase();
  19996. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  19997. const name = this._generateUniqueName(isVoid ? baseName : `START_${baseName}`);
  19998. this._signatureToName[signature] = name;
  19999. return name;
  20000. }
  20001. getCloseTagPlaceholderName(tag) {
  20002. const signature = this._hashClosingTag(tag);
  20003. if (this._signatureToName[signature]) {
  20004. return this._signatureToName[signature];
  20005. }
  20006. const upperTag = tag.toUpperCase();
  20007. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  20008. const name = this._generateUniqueName(`CLOSE_${baseName}`);
  20009. this._signatureToName[signature] = name;
  20010. return name;
  20011. }
  20012. getPlaceholderName(name, content) {
  20013. const upperName = name.toUpperCase();
  20014. const signature = `PH: ${upperName}=${content}`;
  20015. if (this._signatureToName[signature]) {
  20016. return this._signatureToName[signature];
  20017. }
  20018. const uniqueName = this._generateUniqueName(upperName);
  20019. this._signatureToName[signature] = uniqueName;
  20020. return uniqueName;
  20021. }
  20022. getUniquePlaceholder(name) {
  20023. return this._generateUniqueName(name.toUpperCase());
  20024. }
  20025. getStartBlockPlaceholderName(name, parameters) {
  20026. const signature = this._hashBlock(name, parameters);
  20027. if (this._signatureToName[signature]) {
  20028. return this._signatureToName[signature];
  20029. }
  20030. const placeholder = this._generateUniqueName(`START_BLOCK_${this._toSnakeCase(name)}`);
  20031. this._signatureToName[signature] = placeholder;
  20032. return placeholder;
  20033. }
  20034. getCloseBlockPlaceholderName(name) {
  20035. const signature = this._hashClosingBlock(name);
  20036. if (this._signatureToName[signature]) {
  20037. return this._signatureToName[signature];
  20038. }
  20039. const placeholder = this._generateUniqueName(`CLOSE_BLOCK_${this._toSnakeCase(name)}`);
  20040. this._signatureToName[signature] = placeholder;
  20041. return placeholder;
  20042. }
  20043. // Generate a hash for a tag - does not take attribute order into account
  20044. _hashTag(tag, attrs, isVoid) {
  20045. const start = `<${tag}`;
  20046. const strAttrs = Object.keys(attrs)
  20047. .sort()
  20048. .map((name) => ` ${name}=${attrs[name]}`)
  20049. .join('');
  20050. const end = isVoid ? '/>' : `></${tag}>`;
  20051. return start + strAttrs + end;
  20052. }
  20053. _hashClosingTag(tag) {
  20054. return this._hashTag(`/${tag}`, {}, false);
  20055. }
  20056. _hashBlock(name, parameters) {
  20057. const params = parameters.length === 0 ? '' : ` (${parameters.sort().join('; ')})`;
  20058. return `@${name}${params} {}`;
  20059. }
  20060. _hashClosingBlock(name) {
  20061. return this._hashBlock(`close_${name}`, []);
  20062. }
  20063. _toSnakeCase(name) {
  20064. return name.toUpperCase().replace(/[^A-Z0-9]/g, '_');
  20065. }
  20066. _generateUniqueName(base) {
  20067. const seen = this._placeHolderNameCounts.hasOwnProperty(base);
  20068. if (!seen) {
  20069. this._placeHolderNameCounts[base] = 1;
  20070. return base;
  20071. }
  20072. const id = this._placeHolderNameCounts[base];
  20073. this._placeHolderNameCounts[base] = id + 1;
  20074. return `${base}_${id}`;
  20075. }
  20076. }
  20077. const _expParser = new Parser(new Lexer());
  20078. /**
  20079. * Returns a function converting html nodes to an i18n Message given an interpolationConfig
  20080. */
  20081. function createI18nMessageFactory(interpolationConfig, containerBlocks, retainEmptyTokens, preserveExpressionWhitespace) {
  20082. const visitor = new _I18nVisitor(_expParser, interpolationConfig, containerBlocks, retainEmptyTokens, preserveExpressionWhitespace);
  20083. return (nodes, meaning, description, customId, visitNodeFn) => visitor.toI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  20084. }
  20085. function noopVisitNodeFn(_html, i18n) {
  20086. return i18n;
  20087. }
  20088. class _I18nVisitor {
  20089. _expressionParser;
  20090. _interpolationConfig;
  20091. _containerBlocks;
  20092. _retainEmptyTokens;
  20093. _preserveExpressionWhitespace;
  20094. constructor(_expressionParser, _interpolationConfig, _containerBlocks, _retainEmptyTokens, _preserveExpressionWhitespace) {
  20095. this._expressionParser = _expressionParser;
  20096. this._interpolationConfig = _interpolationConfig;
  20097. this._containerBlocks = _containerBlocks;
  20098. this._retainEmptyTokens = _retainEmptyTokens;
  20099. this._preserveExpressionWhitespace = _preserveExpressionWhitespace;
  20100. }
  20101. toI18nMessage(nodes, meaning = '', description = '', customId = '', visitNodeFn) {
  20102. const context = {
  20103. isIcu: nodes.length == 1 && nodes[0] instanceof Expansion,
  20104. icuDepth: 0,
  20105. placeholderRegistry: new PlaceholderRegistry(),
  20106. placeholderToContent: {},
  20107. placeholderToMessage: {},
  20108. visitNodeFn: visitNodeFn || noopVisitNodeFn,
  20109. };
  20110. const i18nodes = visitAll(this, nodes, context);
  20111. return new Message(i18nodes, context.placeholderToContent, context.placeholderToMessage, meaning, description, customId);
  20112. }
  20113. visitElement(el, context) {
  20114. const children = visitAll(this, el.children, context);
  20115. const attrs = {};
  20116. el.attrs.forEach((attr) => {
  20117. // Do not visit the attributes, translatable ones are top-level ASTs
  20118. attrs[attr.name] = attr.value;
  20119. });
  20120. const isVoid = getHtmlTagDefinition(el.name).isVoid;
  20121. const startPhName = context.placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid);
  20122. context.placeholderToContent[startPhName] = {
  20123. text: el.startSourceSpan.toString(),
  20124. sourceSpan: el.startSourceSpan,
  20125. };
  20126. let closePhName = '';
  20127. if (!isVoid) {
  20128. closePhName = context.placeholderRegistry.getCloseTagPlaceholderName(el.name);
  20129. context.placeholderToContent[closePhName] = {
  20130. text: `</${el.name}>`,
  20131. sourceSpan: el.endSourceSpan ?? el.sourceSpan,
  20132. };
  20133. }
  20134. const node = new TagPlaceholder(el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  20135. return context.visitNodeFn(el, node);
  20136. }
  20137. visitAttribute(attribute, context) {
  20138. const node = attribute.valueTokens === undefined || attribute.valueTokens.length === 1
  20139. ? new Text$2(attribute.value, attribute.valueSpan || attribute.sourceSpan)
  20140. : this._visitTextWithInterpolation(attribute.valueTokens, attribute.valueSpan || attribute.sourceSpan, context, attribute.i18n);
  20141. return context.visitNodeFn(attribute, node);
  20142. }
  20143. visitText(text, context) {
  20144. const node = text.tokens.length === 1
  20145. ? new Text$2(text.value, text.sourceSpan)
  20146. : this._visitTextWithInterpolation(text.tokens, text.sourceSpan, context, text.i18n);
  20147. return context.visitNodeFn(text, node);
  20148. }
  20149. visitComment(comment, context) {
  20150. return null;
  20151. }
  20152. visitExpansion(icu, context) {
  20153. context.icuDepth++;
  20154. const i18nIcuCases = {};
  20155. const i18nIcu = new Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
  20156. icu.cases.forEach((caze) => {
  20157. i18nIcuCases[caze.value] = new Container(caze.expression.map((node) => node.visit(this, context)), caze.expSourceSpan);
  20158. });
  20159. context.icuDepth--;
  20160. if (context.isIcu || context.icuDepth > 0) {
  20161. // Returns an ICU node when:
  20162. // - the message (vs a part of the message) is an ICU message, or
  20163. // - the ICU message is nested.
  20164. const expPh = context.placeholderRegistry.getUniquePlaceholder(`VAR_${icu.type}`);
  20165. i18nIcu.expressionPlaceholder = expPh;
  20166. context.placeholderToContent[expPh] = {
  20167. text: icu.switchValue,
  20168. sourceSpan: icu.switchValueSourceSpan,
  20169. };
  20170. return context.visitNodeFn(icu, i18nIcu);
  20171. }
  20172. // Else returns a placeholder
  20173. // ICU placeholders should not be replaced with their original content but with the their
  20174. // translations.
  20175. // TODO(vicb): add a html.Node -> i18n.Message cache to avoid having to re-create the msg
  20176. const phName = context.placeholderRegistry.getPlaceholderName('ICU', icu.sourceSpan.toString());
  20177. context.placeholderToMessage[phName] = this.toI18nMessage([icu], '', '', '', undefined);
  20178. const node = new IcuPlaceholder(i18nIcu, phName, icu.sourceSpan);
  20179. return context.visitNodeFn(icu, node);
  20180. }
  20181. visitExpansionCase(_icuCase, _context) {
  20182. throw new Error('Unreachable code');
  20183. }
  20184. visitBlock(block, context) {
  20185. const children = visitAll(this, block.children, context);
  20186. if (this._containerBlocks.has(block.name)) {
  20187. return new Container(children, block.sourceSpan);
  20188. }
  20189. const parameters = block.parameters.map((param) => param.expression);
  20190. const startPhName = context.placeholderRegistry.getStartBlockPlaceholderName(block.name, parameters);
  20191. const closePhName = context.placeholderRegistry.getCloseBlockPlaceholderName(block.name);
  20192. context.placeholderToContent[startPhName] = {
  20193. text: block.startSourceSpan.toString(),
  20194. sourceSpan: block.startSourceSpan,
  20195. };
  20196. context.placeholderToContent[closePhName] = {
  20197. text: block.endSourceSpan ? block.endSourceSpan.toString() : '}',
  20198. sourceSpan: block.endSourceSpan ?? block.sourceSpan,
  20199. };
  20200. const node = new BlockPlaceholder(block.name, parameters, startPhName, closePhName, children, block.sourceSpan, block.startSourceSpan, block.endSourceSpan);
  20201. return context.visitNodeFn(block, node);
  20202. }
  20203. visitBlockParameter(_parameter, _context) {
  20204. throw new Error('Unreachable code');
  20205. }
  20206. visitLetDeclaration(decl, context) {
  20207. return null;
  20208. }
  20209. /**
  20210. * Convert, text and interpolated tokens up into text and placeholder pieces.
  20211. *
  20212. * @param tokens The text and interpolated tokens.
  20213. * @param sourceSpan The span of the whole of the `text` string.
  20214. * @param context The current context of the visitor, used to compute and store placeholders.
  20215. * @param previousI18n Any i18n metadata associated with this `text` from a previous pass.
  20216. */
  20217. _visitTextWithInterpolation(tokens, sourceSpan, context, previousI18n) {
  20218. // Return a sequence of `Text` and `Placeholder` nodes grouped in a `Container`.
  20219. const nodes = [];
  20220. // We will only create a container if there are actually interpolations,
  20221. // so this flag tracks that.
  20222. let hasInterpolation = false;
  20223. for (const token of tokens) {
  20224. switch (token.type) {
  20225. case 8 /* TokenType.INTERPOLATION */:
  20226. case 17 /* TokenType.ATTR_VALUE_INTERPOLATION */:
  20227. hasInterpolation = true;
  20228. const [startMarker, expression, endMarker] = token.parts;
  20229. const baseName = extractPlaceholderName(expression) || 'INTERPOLATION';
  20230. const phName = context.placeholderRegistry.getPlaceholderName(baseName, expression);
  20231. if (this._preserveExpressionWhitespace) {
  20232. context.placeholderToContent[phName] = {
  20233. text: token.parts.join(''),
  20234. sourceSpan: token.sourceSpan,
  20235. };
  20236. nodes.push(new Placeholder(expression, phName, token.sourceSpan));
  20237. }
  20238. else {
  20239. const normalized = this.normalizeExpression(token);
  20240. context.placeholderToContent[phName] = {
  20241. text: `${startMarker}${normalized}${endMarker}`,
  20242. sourceSpan: token.sourceSpan,
  20243. };
  20244. nodes.push(new Placeholder(normalized, phName, token.sourceSpan));
  20245. }
  20246. break;
  20247. default:
  20248. // Try to merge text tokens with previous tokens. We do this even for all tokens
  20249. // when `retainEmptyTokens == true` because whitespace tokens may have non-zero
  20250. // length, but will be trimmed by `WhitespaceVisitor` in one extraction pass and
  20251. // be considered "empty" there. Therefore a whitespace token with
  20252. // `retainEmptyTokens === true` should be treated like an empty token and either
  20253. // retained or merged into the previous node. Since extraction does two passes with
  20254. // different trimming behavior, the second pass needs to have identical node count
  20255. // to reuse source spans, so we need this check to get the same answer when both
  20256. // trimming and not trimming.
  20257. if (token.parts[0].length > 0 || this._retainEmptyTokens) {
  20258. // This token is text or an encoded entity.
  20259. // If it is following on from a previous text node then merge it into that node
  20260. // Otherwise, if it is following an interpolation, then add a new node.
  20261. const previous = nodes[nodes.length - 1];
  20262. if (previous instanceof Text$2) {
  20263. previous.value += token.parts[0];
  20264. previous.sourceSpan = new ParseSourceSpan(previous.sourceSpan.start, token.sourceSpan.end, previous.sourceSpan.fullStart, previous.sourceSpan.details);
  20265. }
  20266. else {
  20267. nodes.push(new Text$2(token.parts[0], token.sourceSpan));
  20268. }
  20269. }
  20270. else {
  20271. // Retain empty tokens to avoid breaking dropping entire nodes such that source
  20272. // spans should not be reusable across multiple parses of a template. We *should*
  20273. // do this all the time, however we need to maintain backwards compatibility
  20274. // with existing message IDs so we can't do it by default and should only enable
  20275. // this when removing significant whitespace.
  20276. if (this._retainEmptyTokens) {
  20277. nodes.push(new Text$2(token.parts[0], token.sourceSpan));
  20278. }
  20279. }
  20280. break;
  20281. }
  20282. }
  20283. if (hasInterpolation) {
  20284. // Whitespace removal may have invalidated the interpolation source-spans.
  20285. reusePreviousSourceSpans(nodes, previousI18n);
  20286. return new Container(nodes, sourceSpan);
  20287. }
  20288. else {
  20289. return nodes[0];
  20290. }
  20291. }
  20292. // Normalize expression whitespace by parsing and re-serializing it. This makes
  20293. // message IDs more durable to insignificant whitespace changes.
  20294. normalizeExpression(token) {
  20295. const expression = token.parts[1];
  20296. const expr = this._expressionParser.parseBinding(expression,
  20297. /* location */ token.sourceSpan.start.toString(),
  20298. /* absoluteOffset */ token.sourceSpan.start.offset, this._interpolationConfig);
  20299. return serialize(expr);
  20300. }
  20301. }
  20302. /**
  20303. * Re-use the source-spans from `previousI18n` metadata for the `nodes`.
  20304. *
  20305. * Whitespace removal can invalidate the source-spans of interpolation nodes, so we
  20306. * reuse the source-span stored from a previous pass before the whitespace was removed.
  20307. *
  20308. * @param nodes The `Text` and `Placeholder` nodes to be processed.
  20309. * @param previousI18n Any i18n metadata for these `nodes` stored from a previous pass.
  20310. */
  20311. function reusePreviousSourceSpans(nodes, previousI18n) {
  20312. if (previousI18n instanceof Message) {
  20313. // The `previousI18n` is an i18n `Message`, so we are processing an `Attribute` with i18n
  20314. // metadata. The `Message` should consist only of a single `Container` that contains the
  20315. // parts (`Text` and `Placeholder`) to process.
  20316. assertSingleContainerMessage(previousI18n);
  20317. previousI18n = previousI18n.nodes[0];
  20318. }
  20319. if (previousI18n instanceof Container) {
  20320. // The `previousI18n` is a `Container`, which means that this is a second i18n extraction pass
  20321. // after whitespace has been removed from the AST nodes.
  20322. assertEquivalentNodes(previousI18n.children, nodes);
  20323. // Reuse the source-spans from the first pass.
  20324. for (let i = 0; i < nodes.length; i++) {
  20325. nodes[i].sourceSpan = previousI18n.children[i].sourceSpan;
  20326. }
  20327. }
  20328. }
  20329. /**
  20330. * Asserts that the `message` contains exactly one `Container` node.
  20331. */
  20332. function assertSingleContainerMessage(message) {
  20333. const nodes = message.nodes;
  20334. if (nodes.length !== 1 || !(nodes[0] instanceof Container)) {
  20335. throw new Error('Unexpected previous i18n message - expected it to consist of only a single `Container` node.');
  20336. }
  20337. }
  20338. /**
  20339. * Asserts that the `previousNodes` and `node` collections have the same number of elements and
  20340. * corresponding elements have the same node type.
  20341. */
  20342. function assertEquivalentNodes(previousNodes, nodes) {
  20343. if (previousNodes.length !== nodes.length) {
  20344. throw new Error(`
  20345. The number of i18n message children changed between first and second pass.
  20346. First pass (${previousNodes.length} tokens):
  20347. ${previousNodes.map((node) => `"${node.sourceSpan.toString()}"`).join('\n')}
  20348. Second pass (${nodes.length} tokens):
  20349. ${nodes.map((node) => `"${node.sourceSpan.toString()}"`).join('\n')}
  20350. `.trim());
  20351. }
  20352. if (previousNodes.some((node, i) => nodes[i].constructor !== node.constructor)) {
  20353. throw new Error('The types of the i18n message children changed between first and second pass.');
  20354. }
  20355. }
  20356. const _CUSTOM_PH_EXP = /\/\/[\s\S]*i18n[\s\S]*\([\s\S]*ph[\s\S]*=[\s\S]*("|')([\s\S]*?)\1[\s\S]*\)/g;
  20357. function extractPlaceholderName(input) {
  20358. return input.split(_CUSTOM_PH_EXP)[2];
  20359. }
  20360. /**
  20361. * An i18n error.
  20362. */
  20363. class I18nError extends ParseError {
  20364. constructor(span, msg) {
  20365. super(span, msg);
  20366. }
  20367. }
  20368. /**
  20369. * Set of tagName|propertyName corresponding to Trusted Types sinks. Properties applying to all
  20370. * tags use '*'.
  20371. *
  20372. * Extracted from, and should be kept in sync with
  20373. * https://w3c.github.io/webappsec-trusted-types/dist/spec/#integrations
  20374. */
  20375. const TRUSTED_TYPES_SINKS = new Set([
  20376. // NOTE: All strings in this set *must* be lowercase!
  20377. // TrustedHTML
  20378. 'iframe|srcdoc',
  20379. '*|innerhtml',
  20380. '*|outerhtml',
  20381. // NB: no TrustedScript here, as the corresponding tags are stripped by the compiler.
  20382. // TrustedScriptURL
  20383. 'embed|src',
  20384. 'object|codebase',
  20385. 'object|data',
  20386. ]);
  20387. /**
  20388. * isTrustedTypesSink returns true if the given property on the given DOM tag is a Trusted Types
  20389. * sink. In that case, use `ElementSchemaRegistry.securityContext` to determine which particular
  20390. * Trusted Type is required for values passed to the sink:
  20391. * - SecurityContext.HTML corresponds to TrustedHTML
  20392. * - SecurityContext.RESOURCE_URL corresponds to TrustedScriptURL
  20393. */
  20394. function isTrustedTypesSink(tagName, propName) {
  20395. // Make sure comparisons are case insensitive, so that case differences between attribute and
  20396. // property names do not have a security impact.
  20397. tagName = tagName.toLowerCase();
  20398. propName = propName.toLowerCase();
  20399. return (TRUSTED_TYPES_SINKS.has(tagName + '|' + propName) || TRUSTED_TYPES_SINKS.has('*|' + propName));
  20400. }
  20401. const setI18nRefs = (originalNodeMap) => {
  20402. return (trimmedNode, i18nNode) => {
  20403. // We need to set i18n properties on the original, untrimmed AST nodes. The i18n nodes needs to
  20404. // use the trimmed content for message IDs to make messages more stable to whitespace changes.
  20405. // But we don't want to actually trim the content, so we can't use the trimmed HTML AST for
  20406. // general code gen. Instead we map the trimmed HTML AST back to the original AST and then
  20407. // attach the i18n nodes so we get trimmed i18n nodes on the original (untrimmed) HTML AST.
  20408. const originalNode = originalNodeMap.get(trimmedNode) ?? trimmedNode;
  20409. if (originalNode instanceof NodeWithI18n) {
  20410. if (i18nNode instanceof IcuPlaceholder && originalNode.i18n instanceof Message) {
  20411. // This html node represents an ICU but this is a second processing pass, and the legacy id
  20412. // was computed in the previous pass and stored in the `i18n` property as a message.
  20413. // We are about to wipe out that property so capture the previous message to be reused when
  20414. // generating the message for this ICU later. See `_generateI18nMessage()`.
  20415. i18nNode.previousMessage = originalNode.i18n;
  20416. }
  20417. originalNode.i18n = i18nNode;
  20418. }
  20419. return i18nNode;
  20420. };
  20421. };
  20422. /**
  20423. * This visitor walks over HTML parse tree and converts information stored in
  20424. * i18n-related attributes ("i18n" and "i18n-*") into i18n meta object that is
  20425. * stored with other element's and attribute's information.
  20426. */
  20427. class I18nMetaVisitor {
  20428. interpolationConfig;
  20429. keepI18nAttrs;
  20430. enableI18nLegacyMessageIdFormat;
  20431. containerBlocks;
  20432. preserveSignificantWhitespace;
  20433. retainEmptyTokens;
  20434. // whether visited nodes contain i18n information
  20435. hasI18nMeta = false;
  20436. _errors = [];
  20437. constructor(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG, keepI18nAttrs = false, enableI18nLegacyMessageIdFormat = false, containerBlocks = DEFAULT_CONTAINER_BLOCKS, preserveSignificantWhitespace = true,
  20438. // When dropping significant whitespace we need to retain empty tokens or
  20439. // else we won't be able to reuse source spans because empty tokens would be
  20440. // removed and cause a mismatch. Unfortunately this still needs to be
  20441. // configurable and sometimes needs to be set independently in order to make
  20442. // sure the number of nodes don't change between parses, even when
  20443. // `preserveSignificantWhitespace` changes.
  20444. retainEmptyTokens = !preserveSignificantWhitespace) {
  20445. this.interpolationConfig = interpolationConfig;
  20446. this.keepI18nAttrs = keepI18nAttrs;
  20447. this.enableI18nLegacyMessageIdFormat = enableI18nLegacyMessageIdFormat;
  20448. this.containerBlocks = containerBlocks;
  20449. this.preserveSignificantWhitespace = preserveSignificantWhitespace;
  20450. this.retainEmptyTokens = retainEmptyTokens;
  20451. }
  20452. _generateI18nMessage(nodes, meta = '', visitNodeFn) {
  20453. const { meaning, description, customId } = this._parseMetadata(meta);
  20454. const createI18nMessage = createI18nMessageFactory(this.interpolationConfig, this.containerBlocks, this.retainEmptyTokens,
  20455. /* preserveExpressionWhitespace */ this.preserveSignificantWhitespace);
  20456. const message = createI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  20457. this._setMessageId(message, meta);
  20458. this._setLegacyIds(message, meta);
  20459. return message;
  20460. }
  20461. visitAllWithErrors(nodes) {
  20462. const result = nodes.map((node) => node.visit(this, null));
  20463. return new ParseTreeResult(result, this._errors);
  20464. }
  20465. visitElement(element) {
  20466. let message = undefined;
  20467. if (hasI18nAttrs(element)) {
  20468. this.hasI18nMeta = true;
  20469. const attrs = [];
  20470. const attrsMeta = {};
  20471. for (const attr of element.attrs) {
  20472. if (attr.name === I18N_ATTR) {
  20473. // root 'i18n' node attribute
  20474. const i18n = element.i18n || attr.value;
  20475. // Generate a new AST with whitespace trimmed, but also generate a map
  20476. // to correlate each new node to its original so we can apply i18n
  20477. // information to the original node based on the trimmed content.
  20478. //
  20479. // `WhitespaceVisitor` removes *insignificant* whitespace as well as
  20480. // significant whitespace. Enabling this visitor should be conditional
  20481. // on `preserveWhitespace` rather than `preserveSignificantWhitespace`,
  20482. // however this would be a breaking change for existing behavior where
  20483. // `preserveWhitespace` was not respected correctly when generating
  20484. // message IDs. This is really a bug but one we need to keep to maintain
  20485. // backwards compatibility.
  20486. const originalNodeMap = new Map();
  20487. const trimmedNodes = this.preserveSignificantWhitespace
  20488. ? element.children
  20489. : visitAllWithSiblings(new WhitespaceVisitor(false /* preserveSignificantWhitespace */, originalNodeMap), element.children);
  20490. message = this._generateI18nMessage(trimmedNodes, i18n, setI18nRefs(originalNodeMap));
  20491. if (message.nodes.length === 0) {
  20492. // Ignore the message if it is empty.
  20493. message = undefined;
  20494. }
  20495. // Store the message on the element
  20496. element.i18n = message;
  20497. }
  20498. else if (attr.name.startsWith(I18N_ATTR_PREFIX)) {
  20499. // 'i18n-*' attributes
  20500. const name = attr.name.slice(I18N_ATTR_PREFIX.length);
  20501. if (isTrustedTypesSink(element.name, name)) {
  20502. this._reportError(attr, `Translating attribute '${name}' is disallowed for security reasons.`);
  20503. }
  20504. else {
  20505. attrsMeta[name] = attr.value;
  20506. }
  20507. }
  20508. else {
  20509. // non-i18n attributes
  20510. attrs.push(attr);
  20511. }
  20512. }
  20513. // set i18n meta for attributes
  20514. if (Object.keys(attrsMeta).length) {
  20515. for (const attr of attrs) {
  20516. const meta = attrsMeta[attr.name];
  20517. // do not create translation for empty attributes
  20518. if (meta !== undefined && attr.value) {
  20519. attr.i18n = this._generateI18nMessage([attr], attr.i18n || meta);
  20520. }
  20521. }
  20522. }
  20523. if (!this.keepI18nAttrs) {
  20524. // update element's attributes,
  20525. // keeping only non-i18n related ones
  20526. element.attrs = attrs;
  20527. }
  20528. }
  20529. visitAll(this, element.children, message);
  20530. return element;
  20531. }
  20532. visitExpansion(expansion, currentMessage) {
  20533. let message;
  20534. const meta = expansion.i18n;
  20535. this.hasI18nMeta = true;
  20536. if (meta instanceof IcuPlaceholder) {
  20537. // set ICU placeholder name (e.g. "ICU_1"),
  20538. // generated while processing root element contents,
  20539. // so we can reference it when we output translation
  20540. const name = meta.name;
  20541. message = this._generateI18nMessage([expansion], meta);
  20542. const icu = icuFromI18nMessage(message);
  20543. icu.name = name;
  20544. if (currentMessage !== null) {
  20545. // Also update the placeholderToMessage map with this new message
  20546. currentMessage.placeholderToMessage[name] = message;
  20547. }
  20548. }
  20549. else {
  20550. // ICU is a top level message, try to use metadata from container element if provided via
  20551. // `context` argument. Note: context may not be available for standalone ICUs (without
  20552. // wrapping element), so fallback to ICU metadata in this case.
  20553. message = this._generateI18nMessage([expansion], currentMessage || meta);
  20554. }
  20555. expansion.i18n = message;
  20556. return expansion;
  20557. }
  20558. visitText(text) {
  20559. return text;
  20560. }
  20561. visitAttribute(attribute) {
  20562. return attribute;
  20563. }
  20564. visitComment(comment) {
  20565. return comment;
  20566. }
  20567. visitExpansionCase(expansionCase) {
  20568. return expansionCase;
  20569. }
  20570. visitBlock(block, context) {
  20571. visitAll(this, block.children, context);
  20572. return block;
  20573. }
  20574. visitBlockParameter(parameter, context) {
  20575. return parameter;
  20576. }
  20577. visitLetDeclaration(decl, context) {
  20578. return decl;
  20579. }
  20580. /**
  20581. * Parse the general form `meta` passed into extract the explicit metadata needed to create a
  20582. * `Message`.
  20583. *
  20584. * There are three possibilities for the `meta` variable
  20585. * 1) a string from an `i18n` template attribute: parse it to extract the metadata values.
  20586. * 2) a `Message` from a previous processing pass: reuse the metadata values in the message.
  20587. * 4) other: ignore this and just process the message metadata as normal
  20588. *
  20589. * @param meta the bucket that holds information about the message
  20590. * @returns the parsed metadata.
  20591. */
  20592. _parseMetadata(meta) {
  20593. return typeof meta === 'string'
  20594. ? parseI18nMeta(meta)
  20595. : meta instanceof Message
  20596. ? meta
  20597. : {};
  20598. }
  20599. /**
  20600. * Generate (or restore) message id if not specified already.
  20601. */
  20602. _setMessageId(message, meta) {
  20603. if (!message.id) {
  20604. message.id = (meta instanceof Message && meta.id) || decimalDigest(message);
  20605. }
  20606. }
  20607. /**
  20608. * Update the `message` with a `legacyId` if necessary.
  20609. *
  20610. * @param message the message whose legacy id should be set
  20611. * @param meta information about the message being processed
  20612. */
  20613. _setLegacyIds(message, meta) {
  20614. if (this.enableI18nLegacyMessageIdFormat) {
  20615. message.legacyIds = [computeDigest(message), computeDecimalDigest(message)];
  20616. }
  20617. else if (typeof meta !== 'string') {
  20618. // This occurs if we are doing the 2nd pass after whitespace removal (see `parseTemplate()` in
  20619. // `packages/compiler/src/render3/view/template.ts`).
  20620. // In that case we want to reuse the legacy message generated in the 1st pass (see
  20621. // `setI18nRefs()`).
  20622. const previousMessage = meta instanceof Message
  20623. ? meta
  20624. : meta instanceof IcuPlaceholder
  20625. ? meta.previousMessage
  20626. : undefined;
  20627. message.legacyIds = previousMessage ? previousMessage.legacyIds : [];
  20628. }
  20629. }
  20630. _reportError(node, msg) {
  20631. this._errors.push(new I18nError(node.sourceSpan, msg));
  20632. }
  20633. }
  20634. /** I18n separators for metadata **/
  20635. const I18N_MEANING_SEPARATOR = '|';
  20636. const I18N_ID_SEPARATOR = '@@';
  20637. /**
  20638. * Parses i18n metas like:
  20639. * - "@@id",
  20640. * - "description[@@id]",
  20641. * - "meaning|description[@@id]"
  20642. * and returns an object with parsed output.
  20643. *
  20644. * @param meta String that represents i18n meta
  20645. * @returns Object with id, meaning and description fields
  20646. */
  20647. function parseI18nMeta(meta = '') {
  20648. let customId;
  20649. let meaning;
  20650. let description;
  20651. meta = meta.trim();
  20652. if (meta) {
  20653. const idIndex = meta.indexOf(I18N_ID_SEPARATOR);
  20654. const descIndex = meta.indexOf(I18N_MEANING_SEPARATOR);
  20655. let meaningAndDesc;
  20656. [meaningAndDesc, customId] =
  20657. idIndex > -1 ? [meta.slice(0, idIndex), meta.slice(idIndex + 2)] : [meta, ''];
  20658. [meaning, description] =
  20659. descIndex > -1
  20660. ? [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)]
  20661. : ['', meaningAndDesc];
  20662. }
  20663. return { customId, meaning, description };
  20664. }
  20665. // Converts i18n meta information for a message (id, description, meaning)
  20666. // to a JsDoc statement formatted as expected by the Closure compiler.
  20667. function i18nMetaToJSDoc(meta) {
  20668. const tags = [];
  20669. if (meta.description) {
  20670. tags.push({ tagName: "desc" /* o.JSDocTagName.Desc */, text: meta.description });
  20671. }
  20672. else {
  20673. // Suppress the JSCompiler warning that a `@desc` was not given for this message.
  20674. tags.push({ tagName: "suppress" /* o.JSDocTagName.Suppress */, text: '{msgDescriptions}' });
  20675. }
  20676. if (meta.meaning) {
  20677. tags.push({ tagName: "meaning" /* o.JSDocTagName.Meaning */, text: meta.meaning });
  20678. }
  20679. return jsDocComment(tags);
  20680. }
  20681. /** Closure uses `goog.getMsg(message)` to lookup translations */
  20682. const GOOG_GET_MSG = 'goog.getMsg';
  20683. /**
  20684. * Generates a `goog.getMsg()` statement and reassignment. The template:
  20685. *
  20686. * ```html
  20687. * <div i18n>Sent from {{ sender }} to <span class="receiver">{{ receiver }}</span></div>
  20688. * ```
  20689. *
  20690. * Generates:
  20691. *
  20692. * ```ts
  20693. * const MSG_FOO = goog.getMsg(
  20694. * // Message template.
  20695. * 'Sent from {$interpolation} to {$startTagSpan}{$interpolation_1}{$closeTagSpan}.',
  20696. * // Placeholder values, set to magic strings which get replaced by the Angular runtime.
  20697. * {
  20698. * 'interpolation': '\uFFFD0\uFFFD',
  20699. * 'startTagSpan': '\uFFFD1\uFFFD',
  20700. * 'interpolation_1': '\uFFFD2\uFFFD',
  20701. * 'closeTagSpan': '\uFFFD3\uFFFD',
  20702. * },
  20703. * // Options bag.
  20704. * {
  20705. * // Maps each placeholder to the original Angular source code which generates it's value.
  20706. * original_code: {
  20707. * 'interpolation': '{{ sender }}',
  20708. * 'startTagSpan': '<span class="receiver">',
  20709. * 'interpolation_1': '{{ receiver }}',
  20710. * 'closeTagSpan': '</span>',
  20711. * },
  20712. * },
  20713. * );
  20714. * const I18N_0 = MSG_FOO;
  20715. * ```
  20716. */
  20717. function createGoogleGetMsgStatements(variable$1, message, closureVar, placeholderValues) {
  20718. const messageString = serializeI18nMessageForGetMsg(message);
  20719. const args = [literal$1(messageString)];
  20720. if (Object.keys(placeholderValues).length) {
  20721. // Message template parameters containing the magic strings replaced by the Angular runtime with
  20722. // real data, e.g. `{'interpolation': '\uFFFD0\uFFFD'}`.
  20723. args.push(mapLiteral(formatI18nPlaceholderNamesInMap(placeholderValues, true /* useCamelCase */), true /* quoted */));
  20724. // Message options object, which contains original source code for placeholders (as they are
  20725. // present in a template, e.g.
  20726. // `{original_code: {'interpolation': '{{ name }}', 'startTagSpan': '<span>'}}`.
  20727. args.push(mapLiteral({
  20728. original_code: literalMap(Object.keys(placeholderValues).map((param) => ({
  20729. key: formatI18nPlaceholderName(param),
  20730. quoted: true,
  20731. value: message.placeholders[param]
  20732. ? // Get source span for typical placeholder if it exists.
  20733. literal$1(message.placeholders[param].sourceSpan.toString())
  20734. : // Otherwise must be an ICU expression, get it's source span.
  20735. literal$1(message.placeholderToMessage[param].nodes
  20736. .map((node) => node.sourceSpan.toString())
  20737. .join('')),
  20738. }))),
  20739. }));
  20740. }
  20741. // /**
  20742. // * @desc description of message
  20743. // * @meaning meaning of message
  20744. // */
  20745. // const MSG_... = goog.getMsg(..);
  20746. // I18N_X = MSG_...;
  20747. const googGetMsgStmt = closureVar.set(variable(GOOG_GET_MSG).callFn(args)).toConstDecl();
  20748. googGetMsgStmt.addLeadingComment(i18nMetaToJSDoc(message));
  20749. const i18nAssignmentStmt = new ExpressionStatement(variable$1.set(closureVar));
  20750. return [googGetMsgStmt, i18nAssignmentStmt];
  20751. }
  20752. /**
  20753. * This visitor walks over i18n tree and generates its string representation, including ICUs and
  20754. * placeholders in `{$placeholder}` (for plain messages) or `{PLACEHOLDER}` (inside ICUs) format.
  20755. */
  20756. class GetMsgSerializerVisitor {
  20757. formatPh(value) {
  20758. return `{$${formatI18nPlaceholderName(value)}}`;
  20759. }
  20760. visitText(text) {
  20761. return text.value;
  20762. }
  20763. visitContainer(container) {
  20764. return container.children.map((child) => child.visit(this)).join('');
  20765. }
  20766. visitIcu(icu) {
  20767. return serializeIcuNode(icu);
  20768. }
  20769. visitTagPlaceholder(ph) {
  20770. return ph.isVoid
  20771. ? this.formatPh(ph.startName)
  20772. : `${this.formatPh(ph.startName)}${ph.children
  20773. .map((child) => child.visit(this))
  20774. .join('')}${this.formatPh(ph.closeName)}`;
  20775. }
  20776. visitPlaceholder(ph) {
  20777. return this.formatPh(ph.name);
  20778. }
  20779. visitBlockPlaceholder(ph) {
  20780. return `${this.formatPh(ph.startName)}${ph.children
  20781. .map((child) => child.visit(this))
  20782. .join('')}${this.formatPh(ph.closeName)}`;
  20783. }
  20784. visitIcuPlaceholder(ph, context) {
  20785. return this.formatPh(ph.name);
  20786. }
  20787. }
  20788. const serializerVisitor = new GetMsgSerializerVisitor();
  20789. function serializeI18nMessageForGetMsg(message) {
  20790. return message.nodes.map((node) => node.visit(serializerVisitor, null)).join('');
  20791. }
  20792. function createLocalizeStatements(variable, message, params) {
  20793. const { messageParts, placeHolders } = serializeI18nMessageForLocalize(message);
  20794. const sourceSpan = getSourceSpan(message);
  20795. const expressions = placeHolders.map((ph) => params[ph.text]);
  20796. const localizedString$1 = localizedString(message, messageParts, placeHolders, expressions, sourceSpan);
  20797. const variableInitialization = variable.set(localizedString$1);
  20798. return [new ExpressionStatement(variableInitialization)];
  20799. }
  20800. /**
  20801. * This visitor walks over an i18n tree, capturing literal strings and placeholders.
  20802. *
  20803. * The result can be used for generating the `$localize` tagged template literals.
  20804. */
  20805. class LocalizeSerializerVisitor {
  20806. placeholderToMessage;
  20807. pieces;
  20808. constructor(placeholderToMessage, pieces) {
  20809. this.placeholderToMessage = placeholderToMessage;
  20810. this.pieces = pieces;
  20811. }
  20812. visitText(text) {
  20813. if (this.pieces[this.pieces.length - 1] instanceof LiteralPiece) {
  20814. // Two literal pieces in a row means that there was some comment node in-between.
  20815. this.pieces[this.pieces.length - 1].text += text.value;
  20816. }
  20817. else {
  20818. const sourceSpan = new ParseSourceSpan(text.sourceSpan.fullStart, text.sourceSpan.end, text.sourceSpan.fullStart, text.sourceSpan.details);
  20819. this.pieces.push(new LiteralPiece(text.value, sourceSpan));
  20820. }
  20821. }
  20822. visitContainer(container) {
  20823. container.children.forEach((child) => child.visit(this));
  20824. }
  20825. visitIcu(icu) {
  20826. this.pieces.push(new LiteralPiece(serializeIcuNode(icu), icu.sourceSpan));
  20827. }
  20828. visitTagPlaceholder(ph) {
  20829. this.pieces.push(this.createPlaceholderPiece(ph.startName, ph.startSourceSpan ?? ph.sourceSpan));
  20830. if (!ph.isVoid) {
  20831. ph.children.forEach((child) => child.visit(this));
  20832. this.pieces.push(this.createPlaceholderPiece(ph.closeName, ph.endSourceSpan ?? ph.sourceSpan));
  20833. }
  20834. }
  20835. visitPlaceholder(ph) {
  20836. this.pieces.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan));
  20837. }
  20838. visitBlockPlaceholder(ph) {
  20839. this.pieces.push(this.createPlaceholderPiece(ph.startName, ph.startSourceSpan ?? ph.sourceSpan));
  20840. ph.children.forEach((child) => child.visit(this));
  20841. this.pieces.push(this.createPlaceholderPiece(ph.closeName, ph.endSourceSpan ?? ph.sourceSpan));
  20842. }
  20843. visitIcuPlaceholder(ph) {
  20844. this.pieces.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan, this.placeholderToMessage[ph.name]));
  20845. }
  20846. createPlaceholderPiece(name, sourceSpan, associatedMessage) {
  20847. return new PlaceholderPiece(formatI18nPlaceholderName(name, /* useCamelCase */ false), sourceSpan, associatedMessage);
  20848. }
  20849. }
  20850. /**
  20851. * Serialize an i18n message into two arrays: messageParts and placeholders.
  20852. *
  20853. * These arrays will be used to generate `$localize` tagged template literals.
  20854. *
  20855. * @param message The message to be serialized.
  20856. * @returns an object containing the messageParts and placeholders.
  20857. */
  20858. function serializeI18nMessageForLocalize(message) {
  20859. const pieces = [];
  20860. const serializerVisitor = new LocalizeSerializerVisitor(message.placeholderToMessage, pieces);
  20861. message.nodes.forEach((node) => node.visit(serializerVisitor));
  20862. return processMessagePieces(pieces);
  20863. }
  20864. function getSourceSpan(message) {
  20865. const startNode = message.nodes[0];
  20866. const endNode = message.nodes[message.nodes.length - 1];
  20867. return new ParseSourceSpan(startNode.sourceSpan.fullStart, endNode.sourceSpan.end, startNode.sourceSpan.fullStart, startNode.sourceSpan.details);
  20868. }
  20869. /**
  20870. * Convert the list of serialized MessagePieces into two arrays.
  20871. *
  20872. * One contains the literal string pieces and the other the placeholders that will be replaced by
  20873. * expressions when rendering `$localize` tagged template literals.
  20874. *
  20875. * @param pieces The pieces to process.
  20876. * @returns an object containing the messageParts and placeholders.
  20877. */
  20878. function processMessagePieces(pieces) {
  20879. const messageParts = [];
  20880. const placeHolders = [];
  20881. if (pieces[0] instanceof PlaceholderPiece) {
  20882. // The first piece was a placeholder so we need to add an initial empty message part.
  20883. messageParts.push(createEmptyMessagePart(pieces[0].sourceSpan.start));
  20884. }
  20885. for (let i = 0; i < pieces.length; i++) {
  20886. const part = pieces[i];
  20887. if (part instanceof LiteralPiece) {
  20888. messageParts.push(part);
  20889. }
  20890. else {
  20891. placeHolders.push(part);
  20892. if (pieces[i - 1] instanceof PlaceholderPiece) {
  20893. // There were two placeholders in a row, so we need to add an empty message part.
  20894. messageParts.push(createEmptyMessagePart(pieces[i - 1].sourceSpan.end));
  20895. }
  20896. }
  20897. }
  20898. if (pieces[pieces.length - 1] instanceof PlaceholderPiece) {
  20899. // The last piece was a placeholder so we need to add a final empty message part.
  20900. messageParts.push(createEmptyMessagePart(pieces[pieces.length - 1].sourceSpan.end));
  20901. }
  20902. return { messageParts, placeHolders };
  20903. }
  20904. function createEmptyMessagePart(location) {
  20905. return new LiteralPiece('', new ParseSourceSpan(location, location));
  20906. }
  20907. /** Name of the global variable that is used to determine if we use Closure translations or not */
  20908. const NG_I18N_CLOSURE_MODE = 'ngI18nClosureMode';
  20909. /**
  20910. * Prefix for non-`goog.getMsg` i18n-related vars.
  20911. * Note: the prefix uses lowercase characters intentionally due to a Closure behavior that
  20912. * considers variables like `I18N_0` as constants and throws an error when their value changes.
  20913. */
  20914. const TRANSLATION_VAR_PREFIX = 'i18n_';
  20915. /** Prefix of ICU expressions for post processing */
  20916. const I18N_ICU_MAPPING_PREFIX = 'I18N_EXP_';
  20917. /**
  20918. * The escape sequence used for message param values.
  20919. */
  20920. const ESCAPE = '\uFFFD';
  20921. /* Closure variables holding messages must be named `MSG_[A-Z0-9]+` */
  20922. const CLOSURE_TRANSLATION_VAR_PREFIX = 'MSG_';
  20923. /**
  20924. * Generates a prefix for translation const name.
  20925. *
  20926. * @param extra Additional local prefix that should be injected into translation var name
  20927. * @returns Complete translation const prefix
  20928. */
  20929. function getTranslationConstPrefix(extra) {
  20930. return `${CLOSURE_TRANSLATION_VAR_PREFIX}${extra}`.toUpperCase();
  20931. }
  20932. /**
  20933. * Generate AST to declare a variable. E.g. `var I18N_1;`.
  20934. * @param variable the name of the variable to declare.
  20935. */
  20936. function declareI18nVariable(variable) {
  20937. return new DeclareVarStmt(variable.name, undefined, INFERRED_TYPE, undefined, variable.sourceSpan);
  20938. }
  20939. /**
  20940. * Lifts i18n properties into the consts array.
  20941. * TODO: Can we use `ConstCollectedExpr`?
  20942. * TODO: The way the various attributes are linked together is very complex. Perhaps we could
  20943. * simplify the process, maybe by combining the context and message ops?
  20944. */
  20945. function collectI18nConsts(job) {
  20946. const fileBasedI18nSuffix = job.relativeContextFilePath.replace(/[^A-Za-z0-9]/g, '_').toUpperCase() + '_';
  20947. // Step One: Build up various lookup maps we need to collect all the consts.
  20948. // Context Xref -> Extracted Attribute Ops
  20949. const extractedAttributesByI18nContext = new Map();
  20950. // Element/ElementStart Xref -> I18n Attributes config op
  20951. const i18nAttributesByElement = new Map();
  20952. // Element/ElementStart Xref -> All I18n Expression ops for attrs on that target
  20953. const i18nExpressionsByElement = new Map();
  20954. // I18n Message Xref -> I18n Message Op (TODO: use a central op map)
  20955. const messages = new Map();
  20956. for (const unit of job.units) {
  20957. for (const op of unit.ops()) {
  20958. if (op.kind === OpKind.ExtractedAttribute && op.i18nContext !== null) {
  20959. const attributes = extractedAttributesByI18nContext.get(op.i18nContext) ?? [];
  20960. attributes.push(op);
  20961. extractedAttributesByI18nContext.set(op.i18nContext, attributes);
  20962. }
  20963. else if (op.kind === OpKind.I18nAttributes) {
  20964. i18nAttributesByElement.set(op.target, op);
  20965. }
  20966. else if (op.kind === OpKind.I18nExpression &&
  20967. op.usage === I18nExpressionFor.I18nAttribute) {
  20968. const expressions = i18nExpressionsByElement.get(op.target) ?? [];
  20969. expressions.push(op);
  20970. i18nExpressionsByElement.set(op.target, expressions);
  20971. }
  20972. else if (op.kind === OpKind.I18nMessage) {
  20973. messages.set(op.xref, op);
  20974. }
  20975. }
  20976. }
  20977. // Step Two: Serialize the extracted i18n messages for root i18n blocks and i18n attributes into
  20978. // the const array.
  20979. //
  20980. // Also, each i18n message will have a variable expression that can refer to its
  20981. // value. Store these expressions in the appropriate place:
  20982. // 1. For normal i18n content, it also goes in the const array. We save the const index to use
  20983. // later.
  20984. // 2. For extracted attributes, it becomes the value of the extracted attribute instruction.
  20985. // 3. For i18n bindings, it will go in a separate const array instruction below; for now, we just
  20986. // save it.
  20987. const i18nValuesByContext = new Map();
  20988. const messageConstIndices = new Map();
  20989. for (const unit of job.units) {
  20990. for (const op of unit.create) {
  20991. if (op.kind === OpKind.I18nMessage) {
  20992. if (op.messagePlaceholder === null) {
  20993. const { mainVar, statements } = collectMessage(job, fileBasedI18nSuffix, messages, op);
  20994. if (op.i18nBlock !== null) {
  20995. // This is a regular i18n message with a corresponding i18n block. Collect it into the
  20996. // const array.
  20997. const i18nConst = job.addConst(mainVar, statements);
  20998. messageConstIndices.set(op.i18nBlock, i18nConst);
  20999. }
  21000. else {
  21001. // This is an i18n attribute. Extract the initializers into the const pool.
  21002. job.constsInitializers.push(...statements);
  21003. // Save the i18n variable value for later.
  21004. i18nValuesByContext.set(op.i18nContext, mainVar);
  21005. // This i18n message may correspond to an individual extracted attribute. If so, The
  21006. // value of that attribute is updated to read the extracted i18n variable.
  21007. const attributesForMessage = extractedAttributesByI18nContext.get(op.i18nContext);
  21008. if (attributesForMessage !== undefined) {
  21009. for (const attr of attributesForMessage) {
  21010. attr.expression = mainVar.clone();
  21011. }
  21012. }
  21013. }
  21014. }
  21015. OpList.remove(op);
  21016. }
  21017. }
  21018. }
  21019. // Step Three: Serialize I18nAttributes configurations into the const array. Each I18nAttributes
  21020. // instruction has a config array, which contains k-v pairs describing each binding name, and the
  21021. // i18n variable that provides the value.
  21022. for (const unit of job.units) {
  21023. for (const elem of unit.create) {
  21024. if (isElementOrContainerOp(elem)) {
  21025. const i18nAttributes = i18nAttributesByElement.get(elem.xref);
  21026. if (i18nAttributes === undefined) {
  21027. // This element is not associated with an i18n attributes configuration instruction.
  21028. continue;
  21029. }
  21030. let i18nExpressions = i18nExpressionsByElement.get(elem.xref);
  21031. if (i18nExpressions === undefined) {
  21032. // Unused i18nAttributes should have already been removed.
  21033. // TODO: Should the removal of those dead instructions be merged with this phase?
  21034. throw new Error('AssertionError: Could not find any i18n expressions associated with an I18nAttributes instruction');
  21035. }
  21036. // Find expressions for all the unique property names, removing duplicates.
  21037. const seenPropertyNames = new Set();
  21038. i18nExpressions = i18nExpressions.filter((i18nExpr) => {
  21039. const seen = seenPropertyNames.has(i18nExpr.name);
  21040. seenPropertyNames.add(i18nExpr.name);
  21041. return !seen;
  21042. });
  21043. const i18nAttributeConfig = i18nExpressions.flatMap((i18nExpr) => {
  21044. const i18nExprValue = i18nValuesByContext.get(i18nExpr.context);
  21045. if (i18nExprValue === undefined) {
  21046. throw new Error("AssertionError: Could not find i18n expression's value");
  21047. }
  21048. return [literal$1(i18nExpr.name), i18nExprValue];
  21049. });
  21050. i18nAttributes.i18nAttributesConfig = job.addConst(new LiteralArrayExpr(i18nAttributeConfig));
  21051. }
  21052. }
  21053. }
  21054. // Step Four: Propagate the extracted const index into i18n ops that messages were extracted from.
  21055. for (const unit of job.units) {
  21056. for (const op of unit.create) {
  21057. if (op.kind === OpKind.I18nStart) {
  21058. const msgIndex = messageConstIndices.get(op.root);
  21059. if (msgIndex === undefined) {
  21060. throw new Error('AssertionError: Could not find corresponding i18n block index for an i18n message op; was an i18n message incorrectly assumed to correspond to an attribute?');
  21061. }
  21062. op.messageIndex = msgIndex;
  21063. }
  21064. }
  21065. }
  21066. }
  21067. /**
  21068. * Collects the given message into a set of statements that can be added to the const array.
  21069. * This will recursively collect any sub-messages referenced from the parent message as well.
  21070. */
  21071. function collectMessage(job, fileBasedI18nSuffix, messages, messageOp) {
  21072. // Recursively collect any sub-messages, record each sub-message's main variable under its
  21073. // placeholder so that we can add them to the params for the parent message. It is possible
  21074. // that multiple sub-messages will share the same placeholder, so we need to track an array of
  21075. // variables for each placeholder.
  21076. const statements = [];
  21077. const subMessagePlaceholders = new Map();
  21078. for (const subMessageId of messageOp.subMessages) {
  21079. const subMessage = messages.get(subMessageId);
  21080. const { mainVar: subMessageVar, statements: subMessageStatements } = collectMessage(job, fileBasedI18nSuffix, messages, subMessage);
  21081. statements.push(...subMessageStatements);
  21082. const subMessages = subMessagePlaceholders.get(subMessage.messagePlaceholder) ?? [];
  21083. subMessages.push(subMessageVar);
  21084. subMessagePlaceholders.set(subMessage.messagePlaceholder, subMessages);
  21085. }
  21086. addSubMessageParams(messageOp, subMessagePlaceholders);
  21087. // Sort the params for consistency with TemaplateDefinitionBuilder output.
  21088. messageOp.params = new Map([...messageOp.params.entries()].sort());
  21089. const mainVar = variable(job.pool.uniqueName(TRANSLATION_VAR_PREFIX));
  21090. // Closure Compiler requires const names to start with `MSG_` but disallows any other
  21091. // const to start with `MSG_`. We define a variable starting with `MSG_` just for the
  21092. // `goog.getMsg` call
  21093. const closureVar = i18nGenerateClosureVar(job.pool, messageOp.message.id, fileBasedI18nSuffix, job.i18nUseExternalIds);
  21094. let transformFn = undefined;
  21095. // If nescessary, add a post-processing step and resolve any placeholder params that are
  21096. // set in post-processing.
  21097. if (messageOp.needsPostprocessing || messageOp.postprocessingParams.size > 0) {
  21098. // Sort the post-processing params for consistency with TemaplateDefinitionBuilder output.
  21099. const postprocessingParams = Object.fromEntries([...messageOp.postprocessingParams.entries()].sort());
  21100. const formattedPostprocessingParams = formatI18nPlaceholderNamesInMap(postprocessingParams,
  21101. /* useCamelCase */ false);
  21102. const extraTransformFnParams = [];
  21103. if (messageOp.postprocessingParams.size > 0) {
  21104. extraTransformFnParams.push(mapLiteral(formattedPostprocessingParams, /* quoted */ true));
  21105. }
  21106. transformFn = (expr) => importExpr(Identifiers.i18nPostprocess).callFn([expr, ...extraTransformFnParams]);
  21107. }
  21108. // Add the message's statements
  21109. statements.push(...getTranslationDeclStmts(messageOp.message, mainVar, closureVar, messageOp.params, transformFn));
  21110. return { mainVar, statements };
  21111. }
  21112. /**
  21113. * Adds the given subMessage placeholders to the given message op.
  21114. *
  21115. * If a placeholder only corresponds to a single sub-message variable, we just set that variable
  21116. * as the param value. However, if the placeholder corresponds to multiple sub-message
  21117. * variables, we need to add a special placeholder value that is handled by the post-processing
  21118. * step. We then add the array of variables as a post-processing param.
  21119. */
  21120. function addSubMessageParams(messageOp, subMessagePlaceholders) {
  21121. for (const [placeholder, subMessages] of subMessagePlaceholders) {
  21122. if (subMessages.length === 1) {
  21123. messageOp.params.set(placeholder, subMessages[0]);
  21124. }
  21125. else {
  21126. messageOp.params.set(placeholder, literal$1(`${ESCAPE}${I18N_ICU_MAPPING_PREFIX}${placeholder}${ESCAPE}`));
  21127. messageOp.postprocessingParams.set(placeholder, literalArr(subMessages));
  21128. }
  21129. }
  21130. }
  21131. /**
  21132. * Generate statements that define a given translation message.
  21133. *
  21134. * ```ts
  21135. * var I18N_1;
  21136. * if (typeof ngI18nClosureMode !== undefined && ngI18nClosureMode) {
  21137. * var MSG_EXTERNAL_XXX = goog.getMsg(
  21138. * "Some message with {$interpolation}!",
  21139. * { "interpolation": "\uFFFD0\uFFFD" }
  21140. * );
  21141. * I18N_1 = MSG_EXTERNAL_XXX;
  21142. * }
  21143. * else {
  21144. * I18N_1 = $localize`Some message with ${'\uFFFD0\uFFFD'}!`;
  21145. * }
  21146. * ```
  21147. *
  21148. * @param message The original i18n AST message node
  21149. * @param variable The variable that will be assigned the translation, e.g. `I18N_1`.
  21150. * @param closureVar The variable for Closure `goog.getMsg` calls, e.g. `MSG_EXTERNAL_XXX`.
  21151. * @param params Object mapping placeholder names to their values (e.g.
  21152. * `{ "interpolation": "\uFFFD0\uFFFD" }`).
  21153. * @param transformFn Optional transformation function that will be applied to the translation
  21154. * (e.g.
  21155. * post-processing).
  21156. * @returns An array of statements that defined a given translation.
  21157. */
  21158. function getTranslationDeclStmts(message, variable, closureVar, params, transformFn) {
  21159. const paramsObject = Object.fromEntries(params);
  21160. const statements = [
  21161. declareI18nVariable(variable),
  21162. ifStmt(createClosureModeGuard(), createGoogleGetMsgStatements(variable, message, closureVar, paramsObject), createLocalizeStatements(variable, message, formatI18nPlaceholderNamesInMap(paramsObject, /* useCamelCase */ false))),
  21163. ];
  21164. if (transformFn) {
  21165. statements.push(new ExpressionStatement(variable.set(transformFn(variable))));
  21166. }
  21167. return statements;
  21168. }
  21169. /**
  21170. * Create the expression that will be used to guard the closure mode block
  21171. * It is equivalent to:
  21172. *
  21173. * ```ts
  21174. * typeof ngI18nClosureMode !== undefined && ngI18nClosureMode
  21175. * ```
  21176. */
  21177. function createClosureModeGuard() {
  21178. return typeofExpr(variable(NG_I18N_CLOSURE_MODE))
  21179. .notIdentical(literal$1('undefined', STRING_TYPE))
  21180. .and(variable(NG_I18N_CLOSURE_MODE));
  21181. }
  21182. /**
  21183. * Generates vars with Closure-specific names for i18n blocks (i.e. `MSG_XXX`).
  21184. */
  21185. function i18nGenerateClosureVar(pool, messageId, fileBasedI18nSuffix, useExternalIds) {
  21186. let name;
  21187. const suffix = fileBasedI18nSuffix;
  21188. if (useExternalIds) {
  21189. const prefix = getTranslationConstPrefix(`EXTERNAL_`);
  21190. const uniqueSuffix = pool.uniqueName(suffix);
  21191. name = `${prefix}${sanitizeIdentifier(messageId)}$$${uniqueSuffix}`;
  21192. }
  21193. else {
  21194. const prefix = getTranslationConstPrefix(suffix);
  21195. name = pool.uniqueName(prefix);
  21196. }
  21197. return variable(name);
  21198. }
  21199. /**
  21200. * Removes text nodes within i18n blocks since they are already hardcoded into the i18n message.
  21201. * Also, replaces interpolations on these text nodes with i18n expressions of the non-text portions,
  21202. * which will be applied later.
  21203. */
  21204. function convertI18nText(job) {
  21205. for (const unit of job.units) {
  21206. // Remove all text nodes within i18n blocks, their content is already captured in the i18n
  21207. // message.
  21208. let currentI18n = null;
  21209. let currentIcu = null;
  21210. const textNodeI18nBlocks = new Map();
  21211. const textNodeIcus = new Map();
  21212. const icuPlaceholderByText = new Map();
  21213. for (const op of unit.create) {
  21214. switch (op.kind) {
  21215. case OpKind.I18nStart:
  21216. if (op.context === null) {
  21217. throw Error('I18n op should have its context set.');
  21218. }
  21219. currentI18n = op;
  21220. break;
  21221. case OpKind.I18nEnd:
  21222. currentI18n = null;
  21223. break;
  21224. case OpKind.IcuStart:
  21225. if (op.context === null) {
  21226. throw Error('Icu op should have its context set.');
  21227. }
  21228. currentIcu = op;
  21229. break;
  21230. case OpKind.IcuEnd:
  21231. currentIcu = null;
  21232. break;
  21233. case OpKind.Text:
  21234. if (currentI18n !== null) {
  21235. textNodeI18nBlocks.set(op.xref, currentI18n);
  21236. textNodeIcus.set(op.xref, currentIcu);
  21237. if (op.icuPlaceholder !== null) {
  21238. // Create an op to represent the ICU placeholder. Initially set its static text to the
  21239. // value of the text op, though this may be overwritten later if this text op is a
  21240. // placeholder for an interpolation.
  21241. const icuPlaceholderOp = createIcuPlaceholderOp(job.allocateXrefId(), op.icuPlaceholder, [op.initialValue]);
  21242. OpList.replace(op, icuPlaceholderOp);
  21243. icuPlaceholderByText.set(op.xref, icuPlaceholderOp);
  21244. }
  21245. else {
  21246. // Otherwise just remove the text op, since its value is already accounted for in the
  21247. // translated message.
  21248. OpList.remove(op);
  21249. }
  21250. }
  21251. break;
  21252. }
  21253. }
  21254. // Update any interpolations to the removed text, and instead represent them as a series of i18n
  21255. // expressions that we then apply.
  21256. for (const op of unit.update) {
  21257. switch (op.kind) {
  21258. case OpKind.InterpolateText:
  21259. if (!textNodeI18nBlocks.has(op.target)) {
  21260. continue;
  21261. }
  21262. const i18nOp = textNodeI18nBlocks.get(op.target);
  21263. const icuOp = textNodeIcus.get(op.target);
  21264. const icuPlaceholder = icuPlaceholderByText.get(op.target);
  21265. const contextId = icuOp ? icuOp.context : i18nOp.context;
  21266. const resolutionTime = icuOp
  21267. ? I18nParamResolutionTime.Postproccessing
  21268. : I18nParamResolutionTime.Creation;
  21269. const ops = [];
  21270. for (let i = 0; i < op.interpolation.expressions.length; i++) {
  21271. const expr = op.interpolation.expressions[i];
  21272. // For now, this i18nExpression depends on the slot context of the enclosing i18n block.
  21273. // Later, we will modify this, and advance to a different point.
  21274. ops.push(createI18nExpressionOp(contextId, i18nOp.xref, i18nOp.xref, i18nOp.handle, expr, icuPlaceholder?.xref ?? null, op.interpolation.i18nPlaceholders[i] ?? null, resolutionTime, I18nExpressionFor.I18nText, '', expr.sourceSpan ?? op.sourceSpan));
  21275. }
  21276. OpList.replaceWithMany(op, ops);
  21277. // If this interpolation is part of an ICU placeholder, add the strings and expressions to
  21278. // the placeholder.
  21279. if (icuPlaceholder !== undefined) {
  21280. icuPlaceholder.strings = op.interpolation.strings;
  21281. }
  21282. break;
  21283. }
  21284. }
  21285. }
  21286. }
  21287. /**
  21288. * Lifts local reference declarations on element-like structures within each view into an entry in
  21289. * the `consts` array for the whole component.
  21290. */
  21291. function liftLocalRefs(job) {
  21292. for (const unit of job.units) {
  21293. for (const op of unit.create) {
  21294. switch (op.kind) {
  21295. case OpKind.ElementStart:
  21296. case OpKind.Template:
  21297. if (!Array.isArray(op.localRefs)) {
  21298. throw new Error(`AssertionError: expected localRefs to be an array still`);
  21299. }
  21300. op.numSlotsUsed += op.localRefs.length;
  21301. if (op.localRefs.length > 0) {
  21302. const localRefs = serializeLocalRefs(op.localRefs);
  21303. op.localRefs = job.addConst(localRefs);
  21304. }
  21305. else {
  21306. op.localRefs = null;
  21307. }
  21308. break;
  21309. }
  21310. }
  21311. }
  21312. }
  21313. function serializeLocalRefs(refs) {
  21314. const constRefs = [];
  21315. for (const ref of refs) {
  21316. constRefs.push(literal$1(ref.name), literal$1(ref.target));
  21317. }
  21318. return literalArr(constRefs);
  21319. }
  21320. /**
  21321. * Change namespaces between HTML, SVG and MathML, depending on the next element.
  21322. */
  21323. function emitNamespaceChanges(job) {
  21324. for (const unit of job.units) {
  21325. let activeNamespace = Namespace.HTML;
  21326. for (const op of unit.create) {
  21327. if (op.kind !== OpKind.ElementStart) {
  21328. continue;
  21329. }
  21330. if (op.namespace !== activeNamespace) {
  21331. OpList.insertBefore(createNamespaceOp(op.namespace), op);
  21332. activeNamespace = op.namespace;
  21333. }
  21334. }
  21335. }
  21336. }
  21337. /**
  21338. * Parses string representation of a style and converts it into object literal.
  21339. *
  21340. * @param value string representation of style as used in the `style` attribute in HTML.
  21341. * Example: `color: red; height: auto`.
  21342. * @returns An array of style property name and value pairs, e.g. `['color', 'red', 'height',
  21343. * 'auto']`
  21344. */
  21345. function parse(value) {
  21346. // we use a string array here instead of a string map
  21347. // because a string-map is not guaranteed to retain the
  21348. // order of the entries whereas a string array can be
  21349. // constructed in a [key, value, key, value] format.
  21350. const styles = [];
  21351. let i = 0;
  21352. let parenDepth = 0;
  21353. let quote = 0 /* Char.QuoteNone */;
  21354. let valueStart = 0;
  21355. let propStart = 0;
  21356. let currentProp = null;
  21357. while (i < value.length) {
  21358. const token = value.charCodeAt(i++);
  21359. switch (token) {
  21360. case 40 /* Char.OpenParen */:
  21361. parenDepth++;
  21362. break;
  21363. case 41 /* Char.CloseParen */:
  21364. parenDepth--;
  21365. break;
  21366. case 39 /* Char.QuoteSingle */:
  21367. // valueStart needs to be there since prop values don't
  21368. // have quotes in CSS
  21369. if (quote === 0 /* Char.QuoteNone */) {
  21370. quote = 39 /* Char.QuoteSingle */;
  21371. }
  21372. else if (quote === 39 /* Char.QuoteSingle */ && value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */) {
  21373. quote = 0 /* Char.QuoteNone */;
  21374. }
  21375. break;
  21376. case 34 /* Char.QuoteDouble */:
  21377. // same logic as above
  21378. if (quote === 0 /* Char.QuoteNone */) {
  21379. quote = 34 /* Char.QuoteDouble */;
  21380. }
  21381. else if (quote === 34 /* Char.QuoteDouble */ && value.charCodeAt(i - 1) !== 92 /* Char.BackSlash */) {
  21382. quote = 0 /* Char.QuoteNone */;
  21383. }
  21384. break;
  21385. case 58 /* Char.Colon */:
  21386. if (!currentProp && parenDepth === 0 && quote === 0 /* Char.QuoteNone */) {
  21387. // TODO: Do not hyphenate CSS custom property names like: `--intentionallyCamelCase`
  21388. currentProp = hyphenate(value.substring(propStart, i - 1).trim());
  21389. valueStart = i;
  21390. }
  21391. break;
  21392. case 59 /* Char.Semicolon */:
  21393. if (currentProp && valueStart > 0 && parenDepth === 0 && quote === 0 /* Char.QuoteNone */) {
  21394. const styleVal = value.substring(valueStart, i - 1).trim();
  21395. styles.push(currentProp, styleVal);
  21396. propStart = i;
  21397. valueStart = 0;
  21398. currentProp = null;
  21399. }
  21400. break;
  21401. }
  21402. }
  21403. if (currentProp && valueStart) {
  21404. const styleVal = value.slice(valueStart).trim();
  21405. styles.push(currentProp, styleVal);
  21406. }
  21407. return styles;
  21408. }
  21409. function hyphenate(value) {
  21410. return value
  21411. .replace(/[a-z][A-Z]/g, (v) => {
  21412. return v.charAt(0) + '-' + v.charAt(1);
  21413. })
  21414. .toLowerCase();
  21415. }
  21416. /**
  21417. * Parses extracted style and class attributes into separate ExtractedAttributeOps per style or
  21418. * class property.
  21419. */
  21420. function parseExtractedStyles(job) {
  21421. const elements = new Map();
  21422. for (const unit of job.units) {
  21423. for (const op of unit.create) {
  21424. if (isElementOrContainerOp(op)) {
  21425. elements.set(op.xref, op);
  21426. }
  21427. }
  21428. }
  21429. for (const unit of job.units) {
  21430. for (const op of unit.create) {
  21431. if (op.kind === OpKind.ExtractedAttribute &&
  21432. op.bindingKind === BindingKind.Attribute &&
  21433. isStringLiteral(op.expression)) {
  21434. const target = elements.get(op.target);
  21435. if (target !== undefined &&
  21436. target.kind === OpKind.Template &&
  21437. target.templateKind === TemplateKind.Structural) {
  21438. // TemplateDefinitionBuilder will not apply class and style bindings to structural
  21439. // directives; instead, it will leave them as attributes.
  21440. // (It's not clear what that would mean, anyway -- classes and styles on a structural
  21441. // element should probably be a parse error.)
  21442. // TODO: We may be able to remove this once Template Pipeline is the default.
  21443. continue;
  21444. }
  21445. if (op.name === 'style') {
  21446. const parsedStyles = parse(op.expression.value);
  21447. for (let i = 0; i < parsedStyles.length - 1; i += 2) {
  21448. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.StyleProperty, null, parsedStyles[i], literal$1(parsedStyles[i + 1]), null, null, SecurityContext.STYLE), op);
  21449. }
  21450. OpList.remove(op);
  21451. }
  21452. else if (op.name === 'class') {
  21453. const parsedClasses = op.expression.value.trim().split(/\s+/g);
  21454. for (const parsedClass of parsedClasses) {
  21455. OpList.insertBefore(createExtractedAttributeOp(op.target, BindingKind.ClassName, null, parsedClass, null, null, null, SecurityContext.NONE), op);
  21456. }
  21457. OpList.remove(op);
  21458. }
  21459. }
  21460. }
  21461. }
  21462. }
  21463. /**
  21464. * Generate names for functions and variables across all views.
  21465. *
  21466. * This includes propagating those names into any `ir.ReadVariableExpr`s of those variables, so that
  21467. * the reads can be emitted correctly.
  21468. */
  21469. function nameFunctionsAndVariables(job) {
  21470. addNamesToView(job.root, job.componentName, { index: 0 }, job.compatibility === CompatibilityMode.TemplateDefinitionBuilder);
  21471. }
  21472. function addNamesToView(unit, baseName, state, compatibility) {
  21473. if (unit.fnName === null) {
  21474. // Ensure unique names for view units. This is necessary because there might be multiple
  21475. // components with same names in the context of the same pool. Only add the suffix
  21476. // if really needed.
  21477. unit.fnName = unit.job.pool.uniqueName(sanitizeIdentifier(`${baseName}_${unit.job.fnSuffix}`),
  21478. /* alwaysIncludeSuffix */ false);
  21479. }
  21480. // Keep track of the names we assign to variables in the view. We'll need to propagate these
  21481. // into reads of those variables afterwards.
  21482. const varNames = new Map();
  21483. for (const op of unit.ops()) {
  21484. switch (op.kind) {
  21485. case OpKind.Property:
  21486. case OpKind.HostProperty:
  21487. if (op.isAnimationTrigger) {
  21488. op.name = '@' + op.name;
  21489. }
  21490. break;
  21491. case OpKind.Listener:
  21492. if (op.handlerFnName !== null) {
  21493. break;
  21494. }
  21495. if (!op.hostListener && op.targetSlot.slot === null) {
  21496. throw new Error(`Expected a slot to be assigned`);
  21497. }
  21498. let animation = '';
  21499. if (op.isAnimationListener) {
  21500. op.name = `@${op.name}.${op.animationPhase}`;
  21501. animation = 'animation';
  21502. }
  21503. if (op.hostListener) {
  21504. op.handlerFnName = `${baseName}_${animation}${op.name}_HostBindingHandler`;
  21505. }
  21506. else {
  21507. op.handlerFnName = `${unit.fnName}_${op.tag.replace('-', '_')}_${animation}${op.name}_${op.targetSlot.slot}_listener`;
  21508. }
  21509. op.handlerFnName = sanitizeIdentifier(op.handlerFnName);
  21510. break;
  21511. case OpKind.TwoWayListener:
  21512. if (op.handlerFnName !== null) {
  21513. break;
  21514. }
  21515. if (op.targetSlot.slot === null) {
  21516. throw new Error(`Expected a slot to be assigned`);
  21517. }
  21518. op.handlerFnName = sanitizeIdentifier(`${unit.fnName}_${op.tag.replace('-', '_')}_${op.name}_${op.targetSlot.slot}_listener`);
  21519. break;
  21520. case OpKind.Variable:
  21521. varNames.set(op.xref, getVariableName(unit, op.variable, state));
  21522. break;
  21523. case OpKind.RepeaterCreate:
  21524. if (!(unit instanceof ViewCompilationUnit)) {
  21525. throw new Error(`AssertionError: must be compiling a component`);
  21526. }
  21527. if (op.handle.slot === null) {
  21528. throw new Error(`Expected slot to be assigned`);
  21529. }
  21530. if (op.emptyView !== null) {
  21531. const emptyView = unit.job.views.get(op.emptyView);
  21532. // Repeater empty view function is at slot +2 (metadata is in the first slot).
  21533. addNamesToView(emptyView, `${baseName}_${op.functionNameSuffix}Empty_${op.handle.slot + 2}`, state, compatibility);
  21534. }
  21535. // Repeater primary view function is at slot +1 (metadata is in the first slot).
  21536. addNamesToView(unit.job.views.get(op.xref), `${baseName}_${op.functionNameSuffix}_${op.handle.slot + 1}`, state, compatibility);
  21537. break;
  21538. case OpKind.Projection:
  21539. if (!(unit instanceof ViewCompilationUnit)) {
  21540. throw new Error(`AssertionError: must be compiling a component`);
  21541. }
  21542. if (op.handle.slot === null) {
  21543. throw new Error(`Expected slot to be assigned`);
  21544. }
  21545. if (op.fallbackView !== null) {
  21546. const fallbackView = unit.job.views.get(op.fallbackView);
  21547. addNamesToView(fallbackView, `${baseName}_ProjectionFallback_${op.handle.slot}`, state, compatibility);
  21548. }
  21549. break;
  21550. case OpKind.Template:
  21551. if (!(unit instanceof ViewCompilationUnit)) {
  21552. throw new Error(`AssertionError: must be compiling a component`);
  21553. }
  21554. const childView = unit.job.views.get(op.xref);
  21555. if (op.handle.slot === null) {
  21556. throw new Error(`Expected slot to be assigned`);
  21557. }
  21558. const suffix = op.functionNameSuffix.length === 0 ? '' : `_${op.functionNameSuffix}`;
  21559. addNamesToView(childView, `${baseName}${suffix}_${op.handle.slot}`, state, compatibility);
  21560. break;
  21561. case OpKind.StyleProp:
  21562. op.name = normalizeStylePropName(op.name);
  21563. if (compatibility) {
  21564. op.name = stripImportant(op.name);
  21565. }
  21566. break;
  21567. case OpKind.ClassProp:
  21568. if (compatibility) {
  21569. op.name = stripImportant(op.name);
  21570. }
  21571. break;
  21572. }
  21573. }
  21574. // Having named all variables declared in the view, now we can push those names into the
  21575. // `ir.ReadVariableExpr` expressions which represent reads of those variables.
  21576. for (const op of unit.ops()) {
  21577. visitExpressionsInOp(op, (expr) => {
  21578. if (!(expr instanceof ReadVariableExpr) || expr.name !== null) {
  21579. return;
  21580. }
  21581. if (!varNames.has(expr.xref)) {
  21582. throw new Error(`Variable ${expr.xref} not yet named`);
  21583. }
  21584. expr.name = varNames.get(expr.xref);
  21585. });
  21586. }
  21587. }
  21588. function getVariableName(unit, variable, state) {
  21589. if (variable.name === null) {
  21590. switch (variable.kind) {
  21591. case SemanticVariableKind.Context:
  21592. variable.name = `ctx_r${state.index++}`;
  21593. break;
  21594. case SemanticVariableKind.Identifier:
  21595. if (unit.job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  21596. // TODO: Prefix increment and `_r` are for compatibility with the old naming scheme.
  21597. // This has the potential to cause collisions when `ctx` is the identifier, so we need a
  21598. // special check for that as well.
  21599. const compatPrefix = variable.identifier === 'ctx' ? 'i' : '';
  21600. variable.name = `${variable.identifier}_${compatPrefix}r${++state.index}`;
  21601. }
  21602. else {
  21603. variable.name = `${variable.identifier}_i${state.index++}`;
  21604. }
  21605. break;
  21606. default:
  21607. // TODO: Prefix increment for compatibility only.
  21608. variable.name = `_r${++state.index}`;
  21609. break;
  21610. }
  21611. }
  21612. return variable.name;
  21613. }
  21614. /**
  21615. * Normalizes a style prop name by hyphenating it (unless its a CSS variable).
  21616. */
  21617. function normalizeStylePropName(name) {
  21618. return name.startsWith('--') ? name : hyphenate(name);
  21619. }
  21620. /**
  21621. * Strips `!important` out of the given style or class name.
  21622. */
  21623. function stripImportant(name) {
  21624. const importantIndex = name.indexOf('!important');
  21625. if (importantIndex > -1) {
  21626. return name.substring(0, importantIndex);
  21627. }
  21628. return name;
  21629. }
  21630. /**
  21631. * Merges logically sequential `NextContextExpr` operations.
  21632. *
  21633. * `NextContextExpr` can be referenced repeatedly, "popping" the runtime's context stack each time.
  21634. * When two such expressions appear back-to-back, it's possible to merge them together into a single
  21635. * `NextContextExpr` that steps multiple contexts. This merging is possible if all conditions are
  21636. * met:
  21637. *
  21638. * * The result of the `NextContextExpr` that's folded into the subsequent one is not stored (that
  21639. * is, the call is purely side-effectful).
  21640. * * No operations in between them uses the implicit context.
  21641. */
  21642. function mergeNextContextExpressions(job) {
  21643. for (const unit of job.units) {
  21644. for (const op of unit.create) {
  21645. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  21646. mergeNextContextsInOps(op.handlerOps);
  21647. }
  21648. }
  21649. mergeNextContextsInOps(unit.update);
  21650. }
  21651. }
  21652. function mergeNextContextsInOps(ops) {
  21653. for (const op of ops) {
  21654. // Look for a candidate operation to maybe merge.
  21655. if (op.kind !== OpKind.Statement ||
  21656. !(op.statement instanceof ExpressionStatement) ||
  21657. !(op.statement.expr instanceof NextContextExpr)) {
  21658. continue;
  21659. }
  21660. const mergeSteps = op.statement.expr.steps;
  21661. // Try to merge this `ir.NextContextExpr`.
  21662. let tryToMerge = true;
  21663. for (let candidate = op.next; candidate.kind !== OpKind.ListEnd && tryToMerge; candidate = candidate.next) {
  21664. visitExpressionsInOp(candidate, (expr, flags) => {
  21665. if (!isIrExpression(expr)) {
  21666. return expr;
  21667. }
  21668. if (!tryToMerge) {
  21669. // Either we've already merged, or failed to merge.
  21670. return;
  21671. }
  21672. if (flags & VisitorContextFlag.InChildOperation) {
  21673. // We cannot merge into child operations.
  21674. return;
  21675. }
  21676. switch (expr.kind) {
  21677. case ExpressionKind.NextContext:
  21678. // Merge the previous `ir.NextContextExpr` into this one.
  21679. expr.steps += mergeSteps;
  21680. OpList.remove(op);
  21681. tryToMerge = false;
  21682. break;
  21683. case ExpressionKind.GetCurrentView:
  21684. case ExpressionKind.Reference:
  21685. case ExpressionKind.ContextLetReference:
  21686. // Can't merge past a dependency on the context.
  21687. tryToMerge = false;
  21688. break;
  21689. }
  21690. return;
  21691. });
  21692. }
  21693. }
  21694. }
  21695. const CONTAINER_TAG = 'ng-container';
  21696. /**
  21697. * Replace an `Element` or `ElementStart` whose tag is `ng-container` with a specific op.
  21698. */
  21699. function generateNgContainerOps(job) {
  21700. for (const unit of job.units) {
  21701. const updatedElementXrefs = new Set();
  21702. for (const op of unit.create) {
  21703. if (op.kind === OpKind.ElementStart && op.tag === CONTAINER_TAG) {
  21704. // Transmute the `ElementStart` instruction to `ContainerStart`.
  21705. op.kind = OpKind.ContainerStart;
  21706. updatedElementXrefs.add(op.xref);
  21707. }
  21708. if (op.kind === OpKind.ElementEnd && updatedElementXrefs.has(op.xref)) {
  21709. // This `ElementEnd` is associated with an `ElementStart` we already transmuted.
  21710. op.kind = OpKind.ContainerEnd;
  21711. }
  21712. }
  21713. }
  21714. }
  21715. /**
  21716. * Looks up an element in the given map by xref ID.
  21717. */
  21718. function lookupElement(elements, xref) {
  21719. const el = elements.get(xref);
  21720. if (el === undefined) {
  21721. throw new Error('All attributes should have an element-like target.');
  21722. }
  21723. return el;
  21724. }
  21725. /**
  21726. * When a container is marked with `ngNonBindable`, the non-bindable characteristic also applies to
  21727. * all descendants of that container. Therefore, we must emit `disableBindings` and `enableBindings`
  21728. * instructions for every such container.
  21729. */
  21730. function disableBindings$1(job) {
  21731. const elements = new Map();
  21732. for (const view of job.units) {
  21733. for (const op of view.create) {
  21734. if (!isElementOrContainerOp(op)) {
  21735. continue;
  21736. }
  21737. elements.set(op.xref, op);
  21738. }
  21739. }
  21740. for (const unit of job.units) {
  21741. for (const op of unit.create) {
  21742. if ((op.kind === OpKind.ElementStart || op.kind === OpKind.ContainerStart) &&
  21743. op.nonBindable) {
  21744. OpList.insertAfter(createDisableBindingsOp(op.xref), op);
  21745. }
  21746. if ((op.kind === OpKind.ElementEnd || op.kind === OpKind.ContainerEnd) &&
  21747. lookupElement(elements, op.xref).nonBindable) {
  21748. OpList.insertBefore(createEnableBindingsOp(op.xref), op);
  21749. }
  21750. }
  21751. }
  21752. }
  21753. /**
  21754. * Nullish coalescing expressions such as `a ?? b` have different semantics in Angular templates as
  21755. * compared to JavaScript. In particular, they default to `null` instead of `undefined`. Therefore,
  21756. * we replace them with ternary expressions, assigning temporaries as needed to avoid re-evaluating
  21757. * the same sub-expression multiple times.
  21758. */
  21759. function generateNullishCoalesceExpressions(job) {
  21760. for (const unit of job.units) {
  21761. for (const op of unit.ops()) {
  21762. transformExpressionsInOp(op, (expr) => {
  21763. if (!(expr instanceof BinaryOperatorExpr) ||
  21764. expr.operator !== BinaryOperator.NullishCoalesce) {
  21765. return expr;
  21766. }
  21767. const assignment = new AssignTemporaryExpr(expr.lhs.clone(), job.allocateXrefId());
  21768. const read = new ReadTemporaryExpr(assignment.xref);
  21769. // TODO: When not in compatibility mode for TemplateDefinitionBuilder, we can just emit
  21770. // `t != null` instead of including an undefined check as well.
  21771. return new ConditionalExpr(new BinaryOperatorExpr(BinaryOperator.And, new BinaryOperatorExpr(BinaryOperator.NotIdentical, assignment, NULL_EXPR), new BinaryOperatorExpr(BinaryOperator.NotIdentical, read, new LiteralExpr(undefined))), read.clone(), expr.rhs);
  21772. }, VisitorContextFlag.None);
  21773. }
  21774. }
  21775. }
  21776. function kindTest(kind) {
  21777. return (op) => op.kind === kind;
  21778. }
  21779. function kindWithInterpolationTest(kind, interpolation) {
  21780. return (op) => {
  21781. return op.kind === kind && interpolation === op.expression instanceof Interpolation;
  21782. };
  21783. }
  21784. function basicListenerKindTest(op) {
  21785. return ((op.kind === OpKind.Listener && !(op.hostListener && op.isAnimationListener)) ||
  21786. op.kind === OpKind.TwoWayListener);
  21787. }
  21788. function nonInterpolationPropertyKindTest(op) {
  21789. return ((op.kind === OpKind.Property || op.kind === OpKind.TwoWayProperty) &&
  21790. !(op.expression instanceof Interpolation));
  21791. }
  21792. /**
  21793. * Defines the groups based on `OpKind` that ops will be divided into, for the various create
  21794. * op kinds. Ops will be collected into groups, then optionally transformed, before recombining
  21795. * the groups in the order defined here.
  21796. */
  21797. const CREATE_ORDERING = [
  21798. { test: (op) => op.kind === OpKind.Listener && op.hostListener && op.isAnimationListener },
  21799. { test: basicListenerKindTest },
  21800. ];
  21801. /**
  21802. * Defines the groups based on `OpKind` that ops will be divided into, for the various update
  21803. * op kinds.
  21804. */
  21805. const UPDATE_ORDERING = [
  21806. { test: kindTest(OpKind.StyleMap), transform: keepLast },
  21807. { test: kindTest(OpKind.ClassMap), transform: keepLast },
  21808. { test: kindTest(OpKind.StyleProp) },
  21809. { test: kindTest(OpKind.ClassProp) },
  21810. { test: kindWithInterpolationTest(OpKind.Attribute, true) },
  21811. { test: kindWithInterpolationTest(OpKind.Property, true) },
  21812. { test: nonInterpolationPropertyKindTest },
  21813. { test: kindWithInterpolationTest(OpKind.Attribute, false) },
  21814. ];
  21815. /**
  21816. * Host bindings have their own update ordering.
  21817. */
  21818. const UPDATE_HOST_ORDERING = [
  21819. { test: kindWithInterpolationTest(OpKind.HostProperty, true) },
  21820. { test: kindWithInterpolationTest(OpKind.HostProperty, false) },
  21821. { test: kindTest(OpKind.Attribute) },
  21822. { test: kindTest(OpKind.StyleMap), transform: keepLast },
  21823. { test: kindTest(OpKind.ClassMap), transform: keepLast },
  21824. { test: kindTest(OpKind.StyleProp) },
  21825. { test: kindTest(OpKind.ClassProp) },
  21826. ];
  21827. /**
  21828. * The set of all op kinds we handle in the reordering phase.
  21829. */
  21830. const handledOpKinds = new Set([
  21831. OpKind.Listener,
  21832. OpKind.TwoWayListener,
  21833. OpKind.StyleMap,
  21834. OpKind.ClassMap,
  21835. OpKind.StyleProp,
  21836. OpKind.ClassProp,
  21837. OpKind.Property,
  21838. OpKind.TwoWayProperty,
  21839. OpKind.HostProperty,
  21840. OpKind.Attribute,
  21841. ]);
  21842. /**
  21843. * Many type of operations have ordering constraints that must be respected. For example, a
  21844. * `ClassMap` instruction must be ordered after a `StyleMap` instruction, in order to have
  21845. * predictable semantics that match TemplateDefinitionBuilder and don't break applications.
  21846. */
  21847. function orderOps(job) {
  21848. for (const unit of job.units) {
  21849. // First, we pull out ops that need to be ordered. Then, when we encounter an op that shouldn't
  21850. // be reordered, put the ones we've pulled so far back in the correct order. Finally, if we
  21851. // still have ops pulled at the end, put them back in the correct order.
  21852. // Create mode:
  21853. orderWithin(unit.create, CREATE_ORDERING);
  21854. // Update mode:
  21855. const ordering = unit.job.kind === CompilationJobKind.Host ? UPDATE_HOST_ORDERING : UPDATE_ORDERING;
  21856. orderWithin(unit.update, ordering);
  21857. }
  21858. }
  21859. /**
  21860. * Order all the ops within the specified group.
  21861. */
  21862. function orderWithin(opList, ordering) {
  21863. let opsToOrder = [];
  21864. // Only reorder ops that target the same xref; do not mix ops that target different xrefs.
  21865. let firstTargetInGroup = null;
  21866. for (const op of opList) {
  21867. const currentTarget = hasDependsOnSlotContextTrait(op) ? op.target : null;
  21868. if (!handledOpKinds.has(op.kind) ||
  21869. (currentTarget !== firstTargetInGroup &&
  21870. firstTargetInGroup !== null &&
  21871. currentTarget !== null)) {
  21872. OpList.insertBefore(reorder(opsToOrder, ordering), op);
  21873. opsToOrder = [];
  21874. firstTargetInGroup = null;
  21875. }
  21876. if (handledOpKinds.has(op.kind)) {
  21877. opsToOrder.push(op);
  21878. OpList.remove(op);
  21879. firstTargetInGroup = currentTarget ?? firstTargetInGroup;
  21880. }
  21881. }
  21882. opList.push(reorder(opsToOrder, ordering));
  21883. }
  21884. /**
  21885. * Reorders the given list of ops according to the ordering defined by `ORDERING`.
  21886. */
  21887. function reorder(ops, ordering) {
  21888. // Break the ops list into groups based on OpKind.
  21889. const groups = Array.from(ordering, () => new Array());
  21890. for (const op of ops) {
  21891. const groupIndex = ordering.findIndex((o) => o.test(op));
  21892. groups[groupIndex].push(op);
  21893. }
  21894. // Reassemble the groups into a single list, in the correct order.
  21895. return groups.flatMap((group, i) => {
  21896. const transform = ordering[i].transform;
  21897. return transform ? transform(group) : group;
  21898. });
  21899. }
  21900. /**
  21901. * Keeps only the last op in a list of ops.
  21902. */
  21903. function keepLast(ops) {
  21904. return ops.slice(ops.length - 1);
  21905. }
  21906. /**
  21907. * Attributes of `ng-content` named 'select' are specifically removed, because they control which
  21908. * content matches as a property of the `projection`, and are not a plain attribute.
  21909. */
  21910. function removeContentSelectors(job) {
  21911. for (const unit of job.units) {
  21912. const elements = createOpXrefMap(unit);
  21913. for (const op of unit.ops()) {
  21914. switch (op.kind) {
  21915. case OpKind.Binding:
  21916. const target = lookupInXrefMap(elements, op.target);
  21917. if (isSelectAttribute(op.name) && target.kind === OpKind.Projection) {
  21918. OpList.remove(op);
  21919. }
  21920. break;
  21921. }
  21922. }
  21923. }
  21924. }
  21925. function isSelectAttribute(name) {
  21926. return name.toLowerCase() === 'select';
  21927. }
  21928. /**
  21929. * Looks up an element in the given map by xref ID.
  21930. */
  21931. function lookupInXrefMap(map, xref) {
  21932. const el = map.get(xref);
  21933. if (el === undefined) {
  21934. throw new Error('All attributes should have an slottable target.');
  21935. }
  21936. return el;
  21937. }
  21938. /**
  21939. * This phase generates pipe creation instructions. We do this based on the pipe bindings found in
  21940. * the update block, in the order we see them.
  21941. *
  21942. * When not in compatibility mode, we can simply group all these creation instructions together, to
  21943. * maximize chaining opportunities.
  21944. */
  21945. function createPipes(job) {
  21946. for (const unit of job.units) {
  21947. processPipeBindingsInView(unit);
  21948. }
  21949. }
  21950. function processPipeBindingsInView(unit) {
  21951. for (const updateOp of unit.update) {
  21952. visitExpressionsInOp(updateOp, (expr, flags) => {
  21953. if (!isIrExpression(expr)) {
  21954. return;
  21955. }
  21956. if (expr.kind !== ExpressionKind.PipeBinding) {
  21957. return;
  21958. }
  21959. if (flags & VisitorContextFlag.InChildOperation) {
  21960. throw new Error(`AssertionError: pipe bindings should not appear in child expressions`);
  21961. }
  21962. if (unit.job.compatibility) {
  21963. // TODO: We can delete this cast and check once compatibility mode is removed.
  21964. const slotHandle = updateOp.target;
  21965. if (slotHandle == undefined) {
  21966. throw new Error(`AssertionError: expected slot handle to be assigned for pipe creation`);
  21967. }
  21968. addPipeToCreationBlock(unit, updateOp.target, expr);
  21969. }
  21970. else {
  21971. // When not in compatibility mode, we just add the pipe to the end of the create block. This
  21972. // is not only simpler and faster, but allows more chaining opportunities for other
  21973. // instructions.
  21974. unit.create.push(createPipeOp(expr.target, expr.targetSlot, expr.name));
  21975. }
  21976. });
  21977. }
  21978. }
  21979. function addPipeToCreationBlock(unit, afterTargetXref, binding) {
  21980. // Find the appropriate point to insert the Pipe creation operation.
  21981. // We're looking for `afterTargetXref` (and also want to insert after any other pipe operations
  21982. // which might be beyond it).
  21983. for (let op = unit.create.head.next; op.kind !== OpKind.ListEnd; op = op.next) {
  21984. if (!hasConsumesSlotTrait(op)) {
  21985. continue;
  21986. }
  21987. if (op.xref !== afterTargetXref) {
  21988. continue;
  21989. }
  21990. // We've found a tentative insertion point; however, we also want to skip past any _other_ pipe
  21991. // operations present.
  21992. while (op.next.kind === OpKind.Pipe) {
  21993. op = op.next;
  21994. }
  21995. const pipe = createPipeOp(binding.target, binding.targetSlot, binding.name);
  21996. OpList.insertBefore(pipe, op.next);
  21997. // This completes adding the pipe to the creation block.
  21998. return;
  21999. }
  22000. // At this point, we've failed to add the pipe to the creation block.
  22001. throw new Error(`AssertionError: unable to find insertion point for pipe ${binding.name}`);
  22002. }
  22003. /**
  22004. * Pipes that accept more than 4 arguments are variadic, and are handled with a different runtime
  22005. * instruction.
  22006. */
  22007. function createVariadicPipes(job) {
  22008. for (const unit of job.units) {
  22009. for (const op of unit.update) {
  22010. transformExpressionsInOp(op, (expr) => {
  22011. if (!(expr instanceof PipeBindingExpr)) {
  22012. return expr;
  22013. }
  22014. // Pipes are variadic if they have more than 4 arguments.
  22015. if (expr.args.length <= 4) {
  22016. return expr;
  22017. }
  22018. return new PipeBindingVariadicExpr(expr.target, expr.targetSlot, expr.name, literalArr(expr.args), expr.args.length);
  22019. }, VisitorContextFlag.None);
  22020. }
  22021. }
  22022. }
  22023. /**
  22024. * Propagate i18n blocks down through child templates that act as placeholders in the root i18n
  22025. * message. Specifically, perform an in-order traversal of all the views, and add i18nStart/i18nEnd
  22026. * op pairs into descending views. Also, assign an increasing sub-template index to each
  22027. * descending view.
  22028. */
  22029. function propagateI18nBlocks(job) {
  22030. propagateI18nBlocksToTemplates(job.root, 0);
  22031. }
  22032. /**
  22033. * Propagates i18n ops in the given view through to any child views recursively.
  22034. */
  22035. function propagateI18nBlocksToTemplates(unit, subTemplateIndex) {
  22036. let i18nBlock = null;
  22037. for (const op of unit.create) {
  22038. switch (op.kind) {
  22039. case OpKind.I18nStart:
  22040. op.subTemplateIndex = subTemplateIndex === 0 ? null : subTemplateIndex;
  22041. i18nBlock = op;
  22042. break;
  22043. case OpKind.I18nEnd:
  22044. // When we exit a root-level i18n block, reset the sub-template index counter.
  22045. if (i18nBlock.subTemplateIndex === null) {
  22046. subTemplateIndex = 0;
  22047. }
  22048. i18nBlock = null;
  22049. break;
  22050. case OpKind.Template:
  22051. subTemplateIndex = propagateI18nBlocksForView(unit.job.views.get(op.xref), i18nBlock, op.i18nPlaceholder, subTemplateIndex);
  22052. break;
  22053. case OpKind.RepeaterCreate:
  22054. // Propagate i18n blocks to the @for template.
  22055. const forView = unit.job.views.get(op.xref);
  22056. subTemplateIndex = propagateI18nBlocksForView(forView, i18nBlock, op.i18nPlaceholder, subTemplateIndex);
  22057. // Then if there's an @empty template, propagate the i18n blocks for it as well.
  22058. if (op.emptyView !== null) {
  22059. subTemplateIndex = propagateI18nBlocksForView(unit.job.views.get(op.emptyView), i18nBlock, op.emptyI18nPlaceholder, subTemplateIndex);
  22060. }
  22061. break;
  22062. }
  22063. }
  22064. return subTemplateIndex;
  22065. }
  22066. /**
  22067. * Propagate i18n blocks for a view.
  22068. */
  22069. function propagateI18nBlocksForView(view, i18nBlock, i18nPlaceholder, subTemplateIndex) {
  22070. // We found an <ng-template> inside an i18n block; increment the sub-template counter and
  22071. // wrap the template's view in a child i18n block.
  22072. if (i18nPlaceholder !== undefined) {
  22073. if (i18nBlock === null) {
  22074. throw Error('Expected template with i18n placeholder to be in an i18n block.');
  22075. }
  22076. subTemplateIndex++;
  22077. wrapTemplateWithI18n(view, i18nBlock);
  22078. }
  22079. // Continue traversing inside the template's view.
  22080. return propagateI18nBlocksToTemplates(view, subTemplateIndex);
  22081. }
  22082. /**
  22083. * Wraps a template view with i18n start and end ops.
  22084. */
  22085. function wrapTemplateWithI18n(unit, parentI18n) {
  22086. // Only add i18n ops if they have not already been propagated to this template.
  22087. if (unit.create.head.next?.kind !== OpKind.I18nStart) {
  22088. const id = unit.job.allocateXrefId();
  22089. OpList.insertAfter(
  22090. // Nested ng-template i18n start/end ops should not receive source spans.
  22091. createI18nStartOp(id, parentI18n.message, parentI18n.root, null), unit.create.head);
  22092. OpList.insertBefore(createI18nEndOp(id, null), unit.create.tail);
  22093. }
  22094. }
  22095. function extractPureFunctions(job) {
  22096. for (const view of job.units) {
  22097. for (const op of view.ops()) {
  22098. visitExpressionsInOp(op, (expr) => {
  22099. if (!(expr instanceof PureFunctionExpr) || expr.body === null) {
  22100. return;
  22101. }
  22102. const constantDef = new PureFunctionConstant(expr.args.length);
  22103. expr.fn = job.pool.getSharedConstant(constantDef, expr.body);
  22104. expr.body = null;
  22105. });
  22106. }
  22107. }
  22108. }
  22109. class PureFunctionConstant extends GenericKeyFn {
  22110. numArgs;
  22111. constructor(numArgs) {
  22112. super();
  22113. this.numArgs = numArgs;
  22114. }
  22115. keyOf(expr) {
  22116. if (expr instanceof PureFunctionParameterExpr) {
  22117. return `param(${expr.index})`;
  22118. }
  22119. else {
  22120. return super.keyOf(expr);
  22121. }
  22122. }
  22123. // TODO: Use the new pool method `getSharedFunctionReference`
  22124. toSharedConstantDeclaration(declName, keyExpr) {
  22125. const fnParams = [];
  22126. for (let idx = 0; idx < this.numArgs; idx++) {
  22127. fnParams.push(new FnParam('a' + idx));
  22128. }
  22129. // We will never visit `ir.PureFunctionParameterExpr`s that don't belong to us, because this
  22130. // transform runs inside another visitor which will visit nested pure functions before this one.
  22131. const returnExpr = transformExpressionsInExpression(keyExpr, (expr) => {
  22132. if (!(expr instanceof PureFunctionParameterExpr)) {
  22133. return expr;
  22134. }
  22135. return variable('a' + expr.index);
  22136. }, VisitorContextFlag.None);
  22137. return new DeclareVarStmt(declName, new ArrowFunctionExpr(fnParams, returnExpr), undefined, exports.StmtModifier.Final);
  22138. }
  22139. }
  22140. function generatePureLiteralStructures(job) {
  22141. for (const unit of job.units) {
  22142. for (const op of unit.update) {
  22143. transformExpressionsInOp(op, (expr, flags) => {
  22144. if (flags & VisitorContextFlag.InChildOperation) {
  22145. return expr;
  22146. }
  22147. if (expr instanceof LiteralArrayExpr) {
  22148. return transformLiteralArray(expr);
  22149. }
  22150. else if (expr instanceof LiteralMapExpr) {
  22151. return transformLiteralMap(expr);
  22152. }
  22153. return expr;
  22154. }, VisitorContextFlag.None);
  22155. }
  22156. }
  22157. }
  22158. function transformLiteralArray(expr) {
  22159. const derivedEntries = [];
  22160. const nonConstantArgs = [];
  22161. for (const entry of expr.entries) {
  22162. if (entry.isConstant()) {
  22163. derivedEntries.push(entry);
  22164. }
  22165. else {
  22166. const idx = nonConstantArgs.length;
  22167. nonConstantArgs.push(entry);
  22168. derivedEntries.push(new PureFunctionParameterExpr(idx));
  22169. }
  22170. }
  22171. return new PureFunctionExpr(literalArr(derivedEntries), nonConstantArgs);
  22172. }
  22173. function transformLiteralMap(expr) {
  22174. let derivedEntries = [];
  22175. const nonConstantArgs = [];
  22176. for (const entry of expr.entries) {
  22177. if (entry.value.isConstant()) {
  22178. derivedEntries.push(entry);
  22179. }
  22180. else {
  22181. const idx = nonConstantArgs.length;
  22182. nonConstantArgs.push(entry.value);
  22183. derivedEntries.push(new LiteralMapEntry(entry.key, new PureFunctionParameterExpr(idx), entry.quoted));
  22184. }
  22185. }
  22186. return new PureFunctionExpr(literalMap(derivedEntries), nonConstantArgs);
  22187. }
  22188. // This file contains helpers for generating calls to Ivy instructions. In particular, each
  22189. // instruction type is represented as a function, which may select a specific instruction variant
  22190. // depending on the exact arguments.
  22191. function element(slot, tag, constIndex, localRefIndex, sourceSpan) {
  22192. return elementOrContainerBase(Identifiers.element, slot, tag, constIndex, localRefIndex, sourceSpan);
  22193. }
  22194. function elementStart(slot, tag, constIndex, localRefIndex, sourceSpan) {
  22195. return elementOrContainerBase(Identifiers.elementStart, slot, tag, constIndex, localRefIndex, sourceSpan);
  22196. }
  22197. function elementOrContainerBase(instruction, slot, tag, constIndex, localRefIndex, sourceSpan) {
  22198. const args = [literal$1(slot)];
  22199. if (tag !== null) {
  22200. args.push(literal$1(tag));
  22201. }
  22202. if (localRefIndex !== null) {
  22203. args.push(literal$1(constIndex), // might be null, but that's okay.
  22204. literal$1(localRefIndex));
  22205. }
  22206. else if (constIndex !== null) {
  22207. args.push(literal$1(constIndex));
  22208. }
  22209. return call(instruction, args, sourceSpan);
  22210. }
  22211. function elementEnd(sourceSpan) {
  22212. return call(Identifiers.elementEnd, [], sourceSpan);
  22213. }
  22214. function elementContainerStart(slot, constIndex, localRefIndex, sourceSpan) {
  22215. return elementOrContainerBase(Identifiers.elementContainerStart, slot,
  22216. /* tag */ null, constIndex, localRefIndex, sourceSpan);
  22217. }
  22218. function elementContainer(slot, constIndex, localRefIndex, sourceSpan) {
  22219. return elementOrContainerBase(Identifiers.elementContainer, slot,
  22220. /* tag */ null, constIndex, localRefIndex, sourceSpan);
  22221. }
  22222. function elementContainerEnd() {
  22223. return call(Identifiers.elementContainerEnd, [], null);
  22224. }
  22225. function template(slot, templateFnRef, decls, vars, tag, constIndex, localRefs, sourceSpan) {
  22226. const args = [
  22227. literal$1(slot),
  22228. templateFnRef,
  22229. literal$1(decls),
  22230. literal$1(vars),
  22231. literal$1(tag),
  22232. literal$1(constIndex),
  22233. ];
  22234. if (localRefs !== null) {
  22235. args.push(literal$1(localRefs));
  22236. args.push(importExpr(Identifiers.templateRefExtractor));
  22237. }
  22238. while (args[args.length - 1].isEquivalent(NULL_EXPR)) {
  22239. args.pop();
  22240. }
  22241. return call(Identifiers.templateCreate, args, sourceSpan);
  22242. }
  22243. function disableBindings() {
  22244. return call(Identifiers.disableBindings, [], null);
  22245. }
  22246. function enableBindings() {
  22247. return call(Identifiers.enableBindings, [], null);
  22248. }
  22249. function listener(name, handlerFn, eventTargetResolver, syntheticHost, sourceSpan) {
  22250. const args = [literal$1(name), handlerFn];
  22251. if (eventTargetResolver !== null) {
  22252. args.push(literal$1(false)); // `useCapture` flag, defaults to `false`
  22253. args.push(importExpr(eventTargetResolver));
  22254. }
  22255. return call(syntheticHost ? Identifiers.syntheticHostListener : Identifiers.listener, args, sourceSpan);
  22256. }
  22257. function twoWayBindingSet(target, value) {
  22258. return importExpr(Identifiers.twoWayBindingSet).callFn([target, value]);
  22259. }
  22260. function twoWayListener(name, handlerFn, sourceSpan) {
  22261. return call(Identifiers.twoWayListener, [literal$1(name), handlerFn], sourceSpan);
  22262. }
  22263. function pipe(slot, name) {
  22264. return call(Identifiers.pipe, [literal$1(slot), literal$1(name)], null);
  22265. }
  22266. function namespaceHTML() {
  22267. return call(Identifiers.namespaceHTML, [], null);
  22268. }
  22269. function namespaceSVG() {
  22270. return call(Identifiers.namespaceSVG, [], null);
  22271. }
  22272. function namespaceMath() {
  22273. return call(Identifiers.namespaceMathML, [], null);
  22274. }
  22275. function advance(delta, sourceSpan) {
  22276. return call(Identifiers.advance, delta > 1 ? [literal$1(delta)] : [], sourceSpan);
  22277. }
  22278. function reference(slot) {
  22279. return importExpr(Identifiers.reference).callFn([literal$1(slot)]);
  22280. }
  22281. function nextContext(steps) {
  22282. return importExpr(Identifiers.nextContext).callFn(steps === 1 ? [] : [literal$1(steps)]);
  22283. }
  22284. function getCurrentView() {
  22285. return importExpr(Identifiers.getCurrentView).callFn([]);
  22286. }
  22287. function restoreView(savedView) {
  22288. return importExpr(Identifiers.restoreView).callFn([savedView]);
  22289. }
  22290. function resetView(returnValue) {
  22291. return importExpr(Identifiers.resetView).callFn([returnValue]);
  22292. }
  22293. function text(slot, initialValue, sourceSpan) {
  22294. const args = [literal$1(slot, null)];
  22295. if (initialValue !== '') {
  22296. args.push(literal$1(initialValue));
  22297. }
  22298. return call(Identifiers.text, args, sourceSpan);
  22299. }
  22300. function defer(selfSlot, primarySlot, dependencyResolverFn, loadingSlot, placeholderSlot, errorSlot, loadingConfig, placeholderConfig, enableTimerScheduling, sourceSpan, flags) {
  22301. const args = [
  22302. literal$1(selfSlot),
  22303. literal$1(primarySlot),
  22304. dependencyResolverFn ?? literal$1(null),
  22305. literal$1(loadingSlot),
  22306. literal$1(placeholderSlot),
  22307. literal$1(errorSlot),
  22308. loadingConfig ?? literal$1(null),
  22309. placeholderConfig ?? literal$1(null),
  22310. enableTimerScheduling ? importExpr(Identifiers.deferEnableTimerScheduling) : literal$1(null),
  22311. literal$1(flags),
  22312. ];
  22313. let expr;
  22314. while ((expr = args[args.length - 1]) !== null &&
  22315. expr instanceof LiteralExpr &&
  22316. expr.value === null) {
  22317. args.pop();
  22318. }
  22319. return call(Identifiers.defer, args, sourceSpan);
  22320. }
  22321. const deferTriggerToR3TriggerInstructionsMap = new Map([
  22322. [
  22323. DeferTriggerKind.Idle,
  22324. {
  22325. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnIdle,
  22326. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnIdle,
  22327. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnIdle,
  22328. },
  22329. ],
  22330. [
  22331. DeferTriggerKind.Immediate,
  22332. {
  22333. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnImmediate,
  22334. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnImmediate,
  22335. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnImmediate,
  22336. },
  22337. ],
  22338. [
  22339. DeferTriggerKind.Timer,
  22340. {
  22341. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnTimer,
  22342. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnTimer,
  22343. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnTimer,
  22344. },
  22345. ],
  22346. [
  22347. DeferTriggerKind.Hover,
  22348. {
  22349. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnHover,
  22350. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnHover,
  22351. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnHover,
  22352. },
  22353. ],
  22354. [
  22355. DeferTriggerKind.Interaction,
  22356. {
  22357. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnInteraction,
  22358. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnInteraction,
  22359. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnInteraction,
  22360. },
  22361. ],
  22362. [
  22363. DeferTriggerKind.Viewport,
  22364. {
  22365. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferOnViewport,
  22366. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferPrefetchOnViewport,
  22367. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateOnViewport,
  22368. },
  22369. ],
  22370. [
  22371. DeferTriggerKind.Never,
  22372. {
  22373. ["none" /* ir.DeferOpModifierKind.NONE */]: Identifiers.deferHydrateNever,
  22374. ["prefetch" /* ir.DeferOpModifierKind.PREFETCH */]: Identifiers.deferHydrateNever,
  22375. ["hydrate" /* ir.DeferOpModifierKind.HYDRATE */]: Identifiers.deferHydrateNever,
  22376. },
  22377. ],
  22378. ]);
  22379. function deferOn(trigger, args, modifier, sourceSpan) {
  22380. const instructionToCall = deferTriggerToR3TriggerInstructionsMap.get(trigger)?.[modifier];
  22381. if (instructionToCall === undefined) {
  22382. throw new Error(`Unable to determine instruction for trigger ${trigger}`);
  22383. }
  22384. return call(instructionToCall, args.map((a) => literal$1(a)), sourceSpan);
  22385. }
  22386. function projectionDef(def) {
  22387. return call(Identifiers.projectionDef, def ? [def] : [], null);
  22388. }
  22389. function projection(slot, projectionSlotIndex, attributes, fallbackFnName, fallbackDecls, fallbackVars, sourceSpan) {
  22390. const args = [literal$1(slot)];
  22391. if (projectionSlotIndex !== 0 || attributes !== null || fallbackFnName !== null) {
  22392. args.push(literal$1(projectionSlotIndex));
  22393. if (attributes !== null) {
  22394. args.push(attributes);
  22395. }
  22396. if (fallbackFnName !== null) {
  22397. if (attributes === null) {
  22398. args.push(literal$1(null));
  22399. }
  22400. args.push(variable(fallbackFnName), literal$1(fallbackDecls), literal$1(fallbackVars));
  22401. }
  22402. }
  22403. return call(Identifiers.projection, args, sourceSpan);
  22404. }
  22405. function i18nStart(slot, constIndex, subTemplateIndex, sourceSpan) {
  22406. const args = [literal$1(slot), literal$1(constIndex)];
  22407. if (subTemplateIndex !== null) {
  22408. args.push(literal$1(subTemplateIndex));
  22409. }
  22410. return call(Identifiers.i18nStart, args, sourceSpan);
  22411. }
  22412. function repeaterCreate(slot, viewFnName, decls, vars, tag, constIndex, trackByFn, trackByUsesComponentInstance, emptyViewFnName, emptyDecls, emptyVars, emptyTag, emptyConstIndex, sourceSpan) {
  22413. const args = [
  22414. literal$1(slot),
  22415. variable(viewFnName),
  22416. literal$1(decls),
  22417. literal$1(vars),
  22418. literal$1(tag),
  22419. literal$1(constIndex),
  22420. trackByFn,
  22421. ];
  22422. if (trackByUsesComponentInstance || emptyViewFnName !== null) {
  22423. args.push(literal$1(trackByUsesComponentInstance));
  22424. if (emptyViewFnName !== null) {
  22425. args.push(variable(emptyViewFnName), literal$1(emptyDecls), literal$1(emptyVars));
  22426. if (emptyTag !== null || emptyConstIndex !== null) {
  22427. args.push(literal$1(emptyTag));
  22428. }
  22429. if (emptyConstIndex !== null) {
  22430. args.push(literal$1(emptyConstIndex));
  22431. }
  22432. }
  22433. }
  22434. return call(Identifiers.repeaterCreate, args, sourceSpan);
  22435. }
  22436. function repeater(collection, sourceSpan) {
  22437. return call(Identifiers.repeater, [collection], sourceSpan);
  22438. }
  22439. function deferWhen(modifier, expr, sourceSpan) {
  22440. if (modifier === "prefetch" /* ir.DeferOpModifierKind.PREFETCH */) {
  22441. return call(Identifiers.deferPrefetchWhen, [expr], sourceSpan);
  22442. }
  22443. else if (modifier === "hydrate" /* ir.DeferOpModifierKind.HYDRATE */) {
  22444. return call(Identifiers.deferHydrateWhen, [expr], sourceSpan);
  22445. }
  22446. return call(Identifiers.deferWhen, [expr], sourceSpan);
  22447. }
  22448. function declareLet(slot, sourceSpan) {
  22449. return call(Identifiers.declareLet, [literal$1(slot)], sourceSpan);
  22450. }
  22451. function storeLet(value, sourceSpan) {
  22452. return importExpr(Identifiers.storeLet).callFn([value], sourceSpan);
  22453. }
  22454. function readContextLet(slot) {
  22455. return importExpr(Identifiers.readContextLet).callFn([literal$1(slot)]);
  22456. }
  22457. function i18n(slot, constIndex, subTemplateIndex, sourceSpan) {
  22458. const args = [literal$1(slot), literal$1(constIndex)];
  22459. if (subTemplateIndex) {
  22460. args.push(literal$1(subTemplateIndex));
  22461. }
  22462. return call(Identifiers.i18n, args, sourceSpan);
  22463. }
  22464. function i18nEnd(endSourceSpan) {
  22465. return call(Identifiers.i18nEnd, [], endSourceSpan);
  22466. }
  22467. function i18nAttributes(slot, i18nAttributesConfig) {
  22468. const args = [literal$1(slot), literal$1(i18nAttributesConfig)];
  22469. return call(Identifiers.i18nAttributes, args, null);
  22470. }
  22471. function property(name, expression, sanitizer, sourceSpan) {
  22472. const args = [literal$1(name), expression];
  22473. if (sanitizer !== null) {
  22474. args.push(sanitizer);
  22475. }
  22476. return call(Identifiers.property, args, sourceSpan);
  22477. }
  22478. function twoWayProperty(name, expression, sanitizer, sourceSpan) {
  22479. const args = [literal$1(name), expression];
  22480. if (sanitizer !== null) {
  22481. args.push(sanitizer);
  22482. }
  22483. return call(Identifiers.twoWayProperty, args, sourceSpan);
  22484. }
  22485. function attribute(name, expression, sanitizer, namespace) {
  22486. const args = [literal$1(name), expression];
  22487. if (sanitizer !== null || namespace !== null) {
  22488. args.push(sanitizer ?? literal$1(null));
  22489. }
  22490. if (namespace !== null) {
  22491. args.push(literal$1(namespace));
  22492. }
  22493. return call(Identifiers.attribute, args, null);
  22494. }
  22495. function styleProp(name, expression, unit, sourceSpan) {
  22496. const args = [literal$1(name), expression];
  22497. if (unit !== null) {
  22498. args.push(literal$1(unit));
  22499. }
  22500. return call(Identifiers.styleProp, args, sourceSpan);
  22501. }
  22502. function classProp(name, expression, sourceSpan) {
  22503. return call(Identifiers.classProp, [literal$1(name), expression], sourceSpan);
  22504. }
  22505. function styleMap(expression, sourceSpan) {
  22506. return call(Identifiers.styleMap, [expression], sourceSpan);
  22507. }
  22508. function classMap(expression, sourceSpan) {
  22509. return call(Identifiers.classMap, [expression], sourceSpan);
  22510. }
  22511. const PIPE_BINDINGS = [
  22512. Identifiers.pipeBind1,
  22513. Identifiers.pipeBind2,
  22514. Identifiers.pipeBind3,
  22515. Identifiers.pipeBind4,
  22516. ];
  22517. function pipeBind(slot, varOffset, args) {
  22518. if (args.length < 1 || args.length > PIPE_BINDINGS.length) {
  22519. throw new Error(`pipeBind() argument count out of bounds`);
  22520. }
  22521. const instruction = PIPE_BINDINGS[args.length - 1];
  22522. return importExpr(instruction).callFn([literal$1(slot), literal$1(varOffset), ...args]);
  22523. }
  22524. function pipeBindV(slot, varOffset, args) {
  22525. return importExpr(Identifiers.pipeBindV).callFn([literal$1(slot), literal$1(varOffset), args]);
  22526. }
  22527. function textInterpolate(strings, expressions, sourceSpan) {
  22528. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22529. return callVariadicInstruction(TEXT_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan);
  22530. }
  22531. function i18nExp(expr, sourceSpan) {
  22532. return call(Identifiers.i18nExp, [expr], sourceSpan);
  22533. }
  22534. function i18nApply(slot, sourceSpan) {
  22535. return call(Identifiers.i18nApply, [literal$1(slot)], sourceSpan);
  22536. }
  22537. function propertyInterpolate(name, strings, expressions, sanitizer, sourceSpan) {
  22538. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22539. const extraArgs = [];
  22540. if (sanitizer !== null) {
  22541. extraArgs.push(sanitizer);
  22542. }
  22543. return callVariadicInstruction(PROPERTY_INTERPOLATE_CONFIG, [literal$1(name)], interpolationArgs, extraArgs, sourceSpan);
  22544. }
  22545. function attributeInterpolate(name, strings, expressions, sanitizer, sourceSpan) {
  22546. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22547. const extraArgs = [];
  22548. if (sanitizer !== null) {
  22549. extraArgs.push(sanitizer);
  22550. }
  22551. return callVariadicInstruction(ATTRIBUTE_INTERPOLATE_CONFIG, [literal$1(name)], interpolationArgs, extraArgs, sourceSpan);
  22552. }
  22553. function stylePropInterpolate(name, strings, expressions, unit, sourceSpan) {
  22554. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22555. const extraArgs = [];
  22556. if (unit !== null) {
  22557. extraArgs.push(literal$1(unit));
  22558. }
  22559. return callVariadicInstruction(STYLE_PROP_INTERPOLATE_CONFIG, [literal$1(name)], interpolationArgs, extraArgs, sourceSpan);
  22560. }
  22561. function styleMapInterpolate(strings, expressions, sourceSpan) {
  22562. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22563. return callVariadicInstruction(STYLE_MAP_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan);
  22564. }
  22565. function classMapInterpolate(strings, expressions, sourceSpan) {
  22566. const interpolationArgs = collateInterpolationArgs(strings, expressions);
  22567. return callVariadicInstruction(CLASS_MAP_INTERPOLATE_CONFIG, [], interpolationArgs, [], sourceSpan);
  22568. }
  22569. function hostProperty(name, expression, sanitizer, sourceSpan) {
  22570. const args = [literal$1(name), expression];
  22571. if (sanitizer !== null) {
  22572. args.push(sanitizer);
  22573. }
  22574. return call(Identifiers.hostProperty, args, sourceSpan);
  22575. }
  22576. function syntheticHostProperty(name, expression, sourceSpan) {
  22577. return call(Identifiers.syntheticHostProperty, [literal$1(name), expression], sourceSpan);
  22578. }
  22579. function pureFunction(varOffset, fn, args) {
  22580. return callVariadicInstructionExpr(PURE_FUNCTION_CONFIG, [literal$1(varOffset), fn], args, [], null);
  22581. }
  22582. function attachSourceLocation(templatePath, locations) {
  22583. return call(Identifiers.attachSourceLocations, [literal$1(templatePath), locations], null);
  22584. }
  22585. /**
  22586. * Collates the string an expression arguments for an interpolation instruction.
  22587. */
  22588. function collateInterpolationArgs(strings, expressions) {
  22589. if (strings.length < 1 || expressions.length !== strings.length - 1) {
  22590. throw new Error(`AssertionError: expected specific shape of args for strings/expressions in interpolation`);
  22591. }
  22592. const interpolationArgs = [];
  22593. if (expressions.length === 1 && strings[0] === '' && strings[1] === '') {
  22594. interpolationArgs.push(expressions[0]);
  22595. }
  22596. else {
  22597. let idx;
  22598. for (idx = 0; idx < expressions.length; idx++) {
  22599. interpolationArgs.push(literal$1(strings[idx]), expressions[idx]);
  22600. }
  22601. // idx points at the last string.
  22602. interpolationArgs.push(literal$1(strings[idx]));
  22603. }
  22604. return interpolationArgs;
  22605. }
  22606. function call(instruction, args, sourceSpan) {
  22607. const expr = importExpr(instruction).callFn(args, sourceSpan);
  22608. return createStatementOp(new ExpressionStatement(expr, sourceSpan));
  22609. }
  22610. function conditional(condition, contextValue, sourceSpan) {
  22611. const args = [condition];
  22612. if (contextValue !== null) {
  22613. args.push(contextValue);
  22614. }
  22615. return call(Identifiers.conditional, args, sourceSpan);
  22616. }
  22617. /**
  22618. * `InterpolationConfig` for the `textInterpolate` instruction.
  22619. */
  22620. const TEXT_INTERPOLATE_CONFIG = {
  22621. constant: [
  22622. Identifiers.textInterpolate,
  22623. Identifiers.textInterpolate1,
  22624. Identifiers.textInterpolate2,
  22625. Identifiers.textInterpolate3,
  22626. Identifiers.textInterpolate4,
  22627. Identifiers.textInterpolate5,
  22628. Identifiers.textInterpolate6,
  22629. Identifiers.textInterpolate7,
  22630. Identifiers.textInterpolate8,
  22631. ],
  22632. variable: Identifiers.textInterpolateV,
  22633. mapping: (n) => {
  22634. if (n % 2 === 0) {
  22635. throw new Error(`Expected odd number of arguments`);
  22636. }
  22637. return (n - 1) / 2;
  22638. },
  22639. };
  22640. /**
  22641. * `InterpolationConfig` for the `propertyInterpolate` instruction.
  22642. */
  22643. const PROPERTY_INTERPOLATE_CONFIG = {
  22644. constant: [
  22645. Identifiers.propertyInterpolate,
  22646. Identifiers.propertyInterpolate1,
  22647. Identifiers.propertyInterpolate2,
  22648. Identifiers.propertyInterpolate3,
  22649. Identifiers.propertyInterpolate4,
  22650. Identifiers.propertyInterpolate5,
  22651. Identifiers.propertyInterpolate6,
  22652. Identifiers.propertyInterpolate7,
  22653. Identifiers.propertyInterpolate8,
  22654. ],
  22655. variable: Identifiers.propertyInterpolateV,
  22656. mapping: (n) => {
  22657. if (n % 2 === 0) {
  22658. throw new Error(`Expected odd number of arguments`);
  22659. }
  22660. return (n - 1) / 2;
  22661. },
  22662. };
  22663. /**
  22664. * `InterpolationConfig` for the `stylePropInterpolate` instruction.
  22665. */
  22666. const STYLE_PROP_INTERPOLATE_CONFIG = {
  22667. constant: [
  22668. Identifiers.styleProp,
  22669. Identifiers.stylePropInterpolate1,
  22670. Identifiers.stylePropInterpolate2,
  22671. Identifiers.stylePropInterpolate3,
  22672. Identifiers.stylePropInterpolate4,
  22673. Identifiers.stylePropInterpolate5,
  22674. Identifiers.stylePropInterpolate6,
  22675. Identifiers.stylePropInterpolate7,
  22676. Identifiers.stylePropInterpolate8,
  22677. ],
  22678. variable: Identifiers.stylePropInterpolateV,
  22679. mapping: (n) => {
  22680. if (n % 2 === 0) {
  22681. throw new Error(`Expected odd number of arguments`);
  22682. }
  22683. return (n - 1) / 2;
  22684. },
  22685. };
  22686. /**
  22687. * `InterpolationConfig` for the `attributeInterpolate` instruction.
  22688. */
  22689. const ATTRIBUTE_INTERPOLATE_CONFIG = {
  22690. constant: [
  22691. Identifiers.attribute,
  22692. Identifiers.attributeInterpolate1,
  22693. Identifiers.attributeInterpolate2,
  22694. Identifiers.attributeInterpolate3,
  22695. Identifiers.attributeInterpolate4,
  22696. Identifiers.attributeInterpolate5,
  22697. Identifiers.attributeInterpolate6,
  22698. Identifiers.attributeInterpolate7,
  22699. Identifiers.attributeInterpolate8,
  22700. ],
  22701. variable: Identifiers.attributeInterpolateV,
  22702. mapping: (n) => {
  22703. if (n % 2 === 0) {
  22704. throw new Error(`Expected odd number of arguments`);
  22705. }
  22706. return (n - 1) / 2;
  22707. },
  22708. };
  22709. /**
  22710. * `InterpolationConfig` for the `styleMapInterpolate` instruction.
  22711. */
  22712. const STYLE_MAP_INTERPOLATE_CONFIG = {
  22713. constant: [
  22714. Identifiers.styleMap,
  22715. Identifiers.styleMapInterpolate1,
  22716. Identifiers.styleMapInterpolate2,
  22717. Identifiers.styleMapInterpolate3,
  22718. Identifiers.styleMapInterpolate4,
  22719. Identifiers.styleMapInterpolate5,
  22720. Identifiers.styleMapInterpolate6,
  22721. Identifiers.styleMapInterpolate7,
  22722. Identifiers.styleMapInterpolate8,
  22723. ],
  22724. variable: Identifiers.styleMapInterpolateV,
  22725. mapping: (n) => {
  22726. if (n % 2 === 0) {
  22727. throw new Error(`Expected odd number of arguments`);
  22728. }
  22729. return (n - 1) / 2;
  22730. },
  22731. };
  22732. /**
  22733. * `InterpolationConfig` for the `classMapInterpolate` instruction.
  22734. */
  22735. const CLASS_MAP_INTERPOLATE_CONFIG = {
  22736. constant: [
  22737. Identifiers.classMap,
  22738. Identifiers.classMapInterpolate1,
  22739. Identifiers.classMapInterpolate2,
  22740. Identifiers.classMapInterpolate3,
  22741. Identifiers.classMapInterpolate4,
  22742. Identifiers.classMapInterpolate5,
  22743. Identifiers.classMapInterpolate6,
  22744. Identifiers.classMapInterpolate7,
  22745. Identifiers.classMapInterpolate8,
  22746. ],
  22747. variable: Identifiers.classMapInterpolateV,
  22748. mapping: (n) => {
  22749. if (n % 2 === 0) {
  22750. throw new Error(`Expected odd number of arguments`);
  22751. }
  22752. return (n - 1) / 2;
  22753. },
  22754. };
  22755. const PURE_FUNCTION_CONFIG = {
  22756. constant: [
  22757. Identifiers.pureFunction0,
  22758. Identifiers.pureFunction1,
  22759. Identifiers.pureFunction2,
  22760. Identifiers.pureFunction3,
  22761. Identifiers.pureFunction4,
  22762. Identifiers.pureFunction5,
  22763. Identifiers.pureFunction6,
  22764. Identifiers.pureFunction7,
  22765. Identifiers.pureFunction8,
  22766. ],
  22767. variable: Identifiers.pureFunctionV,
  22768. mapping: (n) => n,
  22769. };
  22770. function callVariadicInstructionExpr(config, baseArgs, interpolationArgs, extraArgs, sourceSpan) {
  22771. const n = config.mapping(interpolationArgs.length);
  22772. if (n < config.constant.length) {
  22773. // Constant calling pattern.
  22774. return importExpr(config.constant[n])
  22775. .callFn([...baseArgs, ...interpolationArgs, ...extraArgs], sourceSpan);
  22776. }
  22777. else if (config.variable !== null) {
  22778. // Variable calling pattern.
  22779. return importExpr(config.variable)
  22780. .callFn([...baseArgs, literalArr(interpolationArgs), ...extraArgs], sourceSpan);
  22781. }
  22782. else {
  22783. throw new Error(`AssertionError: unable to call variadic function`);
  22784. }
  22785. }
  22786. function callVariadicInstruction(config, baseArgs, interpolationArgs, extraArgs, sourceSpan) {
  22787. return createStatementOp(callVariadicInstructionExpr(config, baseArgs, interpolationArgs, extraArgs, sourceSpan).toStmt());
  22788. }
  22789. /**
  22790. * Map of target resolvers for event listeners.
  22791. */
  22792. const GLOBAL_TARGET_RESOLVERS = new Map([
  22793. ['window', Identifiers.resolveWindow],
  22794. ['document', Identifiers.resolveDocument],
  22795. ['body', Identifiers.resolveBody],
  22796. ]);
  22797. /**
  22798. * Compiles semantic operations across all views and generates output `o.Statement`s with actual
  22799. * runtime calls in their place.
  22800. *
  22801. * Reification replaces semantic operations with selected Ivy instructions and other generated code
  22802. * structures. After reification, the create/update operation lists of all views should only contain
  22803. * `ir.StatementOp`s (which wrap generated `o.Statement`s).
  22804. */
  22805. function reify(job) {
  22806. for (const unit of job.units) {
  22807. reifyCreateOperations(unit, unit.create);
  22808. reifyUpdateOperations(unit, unit.update);
  22809. }
  22810. }
  22811. function reifyCreateOperations(unit, ops) {
  22812. for (const op of ops) {
  22813. transformExpressionsInOp(op, reifyIrExpression, VisitorContextFlag.None);
  22814. switch (op.kind) {
  22815. case OpKind.Text:
  22816. OpList.replace(op, text(op.handle.slot, op.initialValue, op.sourceSpan));
  22817. break;
  22818. case OpKind.ElementStart:
  22819. OpList.replace(op, elementStart(op.handle.slot, op.tag, op.attributes, op.localRefs, op.startSourceSpan));
  22820. break;
  22821. case OpKind.Element:
  22822. OpList.replace(op, element(op.handle.slot, op.tag, op.attributes, op.localRefs, op.wholeSourceSpan));
  22823. break;
  22824. case OpKind.ElementEnd:
  22825. OpList.replace(op, elementEnd(op.sourceSpan));
  22826. break;
  22827. case OpKind.ContainerStart:
  22828. OpList.replace(op, elementContainerStart(op.handle.slot, op.attributes, op.localRefs, op.startSourceSpan));
  22829. break;
  22830. case OpKind.Container:
  22831. OpList.replace(op, elementContainer(op.handle.slot, op.attributes, op.localRefs, op.wholeSourceSpan));
  22832. break;
  22833. case OpKind.ContainerEnd:
  22834. OpList.replace(op, elementContainerEnd());
  22835. break;
  22836. case OpKind.I18nStart:
  22837. OpList.replace(op, i18nStart(op.handle.slot, op.messageIndex, op.subTemplateIndex, op.sourceSpan));
  22838. break;
  22839. case OpKind.I18nEnd:
  22840. OpList.replace(op, i18nEnd(op.sourceSpan));
  22841. break;
  22842. case OpKind.I18n:
  22843. OpList.replace(op, i18n(op.handle.slot, op.messageIndex, op.subTemplateIndex, op.sourceSpan));
  22844. break;
  22845. case OpKind.I18nAttributes:
  22846. if (op.i18nAttributesConfig === null) {
  22847. throw new Error(`AssertionError: i18nAttributesConfig was not set`);
  22848. }
  22849. OpList.replace(op, i18nAttributes(op.handle.slot, op.i18nAttributesConfig));
  22850. break;
  22851. case OpKind.Template:
  22852. if (!(unit instanceof ViewCompilationUnit)) {
  22853. throw new Error(`AssertionError: must be compiling a component`);
  22854. }
  22855. if (Array.isArray(op.localRefs)) {
  22856. throw new Error(`AssertionError: local refs array should have been extracted into a constant`);
  22857. }
  22858. const childView = unit.job.views.get(op.xref);
  22859. OpList.replace(op, template(op.handle.slot, variable(childView.fnName), childView.decls, childView.vars, op.tag, op.attributes, op.localRefs, op.startSourceSpan));
  22860. break;
  22861. case OpKind.DisableBindings:
  22862. OpList.replace(op, disableBindings());
  22863. break;
  22864. case OpKind.EnableBindings:
  22865. OpList.replace(op, enableBindings());
  22866. break;
  22867. case OpKind.Pipe:
  22868. OpList.replace(op, pipe(op.handle.slot, op.name));
  22869. break;
  22870. case OpKind.DeclareLet:
  22871. OpList.replace(op, declareLet(op.handle.slot, op.sourceSpan));
  22872. break;
  22873. case OpKind.Listener:
  22874. const listenerFn = reifyListenerHandler(unit, op.handlerFnName, op.handlerOps, op.consumesDollarEvent);
  22875. const eventTargetResolver = op.eventTarget
  22876. ? GLOBAL_TARGET_RESOLVERS.get(op.eventTarget)
  22877. : null;
  22878. if (eventTargetResolver === undefined) {
  22879. throw new Error(`Unexpected global target '${op.eventTarget}' defined for '${op.name}' event. Supported list of global targets: window,document,body.`);
  22880. }
  22881. OpList.replace(op, listener(op.name, listenerFn, eventTargetResolver, op.hostListener && op.isAnimationListener, op.sourceSpan));
  22882. break;
  22883. case OpKind.TwoWayListener:
  22884. OpList.replace(op, twoWayListener(op.name, reifyListenerHandler(unit, op.handlerFnName, op.handlerOps, true), op.sourceSpan));
  22885. break;
  22886. case OpKind.Variable:
  22887. if (op.variable.name === null) {
  22888. throw new Error(`AssertionError: unnamed variable ${op.xref}`);
  22889. }
  22890. OpList.replace(op, createStatementOp(new DeclareVarStmt(op.variable.name, op.initializer, undefined, exports.StmtModifier.Final)));
  22891. break;
  22892. case OpKind.Namespace:
  22893. switch (op.active) {
  22894. case Namespace.HTML:
  22895. OpList.replace(op, namespaceHTML());
  22896. break;
  22897. case Namespace.SVG:
  22898. OpList.replace(op, namespaceSVG());
  22899. break;
  22900. case Namespace.Math:
  22901. OpList.replace(op, namespaceMath());
  22902. break;
  22903. }
  22904. break;
  22905. case OpKind.Defer:
  22906. const timerScheduling = !!op.loadingMinimumTime || !!op.loadingAfterTime || !!op.placeholderMinimumTime;
  22907. OpList.replace(op, defer(op.handle.slot, op.mainSlot.slot, op.resolverFn, op.loadingSlot?.slot ?? null, op.placeholderSlot?.slot ?? null, op.errorSlot?.slot ?? null, op.loadingConfig, op.placeholderConfig, timerScheduling, op.sourceSpan, op.flags));
  22908. break;
  22909. case OpKind.DeferOn:
  22910. let args = [];
  22911. switch (op.trigger.kind) {
  22912. case DeferTriggerKind.Never:
  22913. case DeferTriggerKind.Idle:
  22914. case DeferTriggerKind.Immediate:
  22915. break;
  22916. case DeferTriggerKind.Timer:
  22917. args = [op.trigger.delay];
  22918. break;
  22919. case DeferTriggerKind.Interaction:
  22920. case DeferTriggerKind.Hover:
  22921. case DeferTriggerKind.Viewport:
  22922. // `hydrate` triggers don't support targets.
  22923. if (op.modifier === "hydrate" /* ir.DeferOpModifierKind.HYDRATE */) {
  22924. args = [];
  22925. }
  22926. else {
  22927. if (op.trigger.targetSlot?.slot == null || op.trigger.targetSlotViewSteps === null) {
  22928. throw new Error(`Slot or view steps not set in trigger reification for trigger kind ${op.trigger.kind}`);
  22929. }
  22930. args = [op.trigger.targetSlot.slot];
  22931. if (op.trigger.targetSlotViewSteps !== 0) {
  22932. args.push(op.trigger.targetSlotViewSteps);
  22933. }
  22934. }
  22935. break;
  22936. default:
  22937. throw new Error(`AssertionError: Unsupported reification of defer trigger kind ${op.trigger.kind}`);
  22938. }
  22939. OpList.replace(op, deferOn(op.trigger.kind, args, op.modifier, op.sourceSpan));
  22940. break;
  22941. case OpKind.ProjectionDef:
  22942. OpList.replace(op, projectionDef(op.def));
  22943. break;
  22944. case OpKind.Projection:
  22945. if (op.handle.slot === null) {
  22946. throw new Error('No slot was assigned for project instruction');
  22947. }
  22948. let fallbackViewFnName = null;
  22949. let fallbackDecls = null;
  22950. let fallbackVars = null;
  22951. if (op.fallbackView !== null) {
  22952. if (!(unit instanceof ViewCompilationUnit)) {
  22953. throw new Error(`AssertionError: must be compiling a component`);
  22954. }
  22955. const fallbackView = unit.job.views.get(op.fallbackView);
  22956. if (fallbackView === undefined) {
  22957. throw new Error('AssertionError: projection had fallback view xref, but fallback view was not found');
  22958. }
  22959. if (fallbackView.fnName === null ||
  22960. fallbackView.decls === null ||
  22961. fallbackView.vars === null) {
  22962. throw new Error(`AssertionError: expected projection fallback view to have been named and counted`);
  22963. }
  22964. fallbackViewFnName = fallbackView.fnName;
  22965. fallbackDecls = fallbackView.decls;
  22966. fallbackVars = fallbackView.vars;
  22967. }
  22968. OpList.replace(op, projection(op.handle.slot, op.projectionSlotIndex, op.attributes, fallbackViewFnName, fallbackDecls, fallbackVars, op.sourceSpan));
  22969. break;
  22970. case OpKind.RepeaterCreate:
  22971. if (op.handle.slot === null) {
  22972. throw new Error('No slot was assigned for repeater instruction');
  22973. }
  22974. if (!(unit instanceof ViewCompilationUnit)) {
  22975. throw new Error(`AssertionError: must be compiling a component`);
  22976. }
  22977. const repeaterView = unit.job.views.get(op.xref);
  22978. if (repeaterView.fnName === null) {
  22979. throw new Error(`AssertionError: expected repeater primary view to have been named`);
  22980. }
  22981. let emptyViewFnName = null;
  22982. let emptyDecls = null;
  22983. let emptyVars = null;
  22984. if (op.emptyView !== null) {
  22985. const emptyView = unit.job.views.get(op.emptyView);
  22986. if (emptyView === undefined) {
  22987. throw new Error('AssertionError: repeater had empty view xref, but empty view was not found');
  22988. }
  22989. if (emptyView.fnName === null || emptyView.decls === null || emptyView.vars === null) {
  22990. throw new Error(`AssertionError: expected repeater empty view to have been named and counted`);
  22991. }
  22992. emptyViewFnName = emptyView.fnName;
  22993. emptyDecls = emptyView.decls;
  22994. emptyVars = emptyView.vars;
  22995. }
  22996. OpList.replace(op, repeaterCreate(op.handle.slot, repeaterView.fnName, op.decls, op.vars, op.tag, op.attributes, reifyTrackBy(unit, op), op.usesComponentInstance, emptyViewFnName, emptyDecls, emptyVars, op.emptyTag, op.emptyAttributes, op.wholeSourceSpan));
  22997. break;
  22998. case OpKind.SourceLocation:
  22999. const locationsLiteral = literalArr(op.locations.map(({ targetSlot, offset, line, column }) => {
  23000. if (targetSlot.slot === null) {
  23001. throw new Error('No slot was assigned for source location');
  23002. }
  23003. return literalArr([
  23004. literal$1(targetSlot.slot),
  23005. literal$1(offset),
  23006. literal$1(line),
  23007. literal$1(column),
  23008. ]);
  23009. }));
  23010. OpList.replace(op, attachSourceLocation(op.templatePath, locationsLiteral));
  23011. break;
  23012. case OpKind.Statement:
  23013. // Pass statement operations directly through.
  23014. break;
  23015. default:
  23016. throw new Error(`AssertionError: Unsupported reification of create op ${OpKind[op.kind]}`);
  23017. }
  23018. }
  23019. }
  23020. function reifyUpdateOperations(_unit, ops) {
  23021. for (const op of ops) {
  23022. transformExpressionsInOp(op, reifyIrExpression, VisitorContextFlag.None);
  23023. switch (op.kind) {
  23024. case OpKind.Advance:
  23025. OpList.replace(op, advance(op.delta, op.sourceSpan));
  23026. break;
  23027. case OpKind.Property:
  23028. if (op.expression instanceof Interpolation) {
  23029. OpList.replace(op, propertyInterpolate(op.name, op.expression.strings, op.expression.expressions, op.sanitizer, op.sourceSpan));
  23030. }
  23031. else {
  23032. OpList.replace(op, property(op.name, op.expression, op.sanitizer, op.sourceSpan));
  23033. }
  23034. break;
  23035. case OpKind.TwoWayProperty:
  23036. OpList.replace(op, twoWayProperty(op.name, op.expression, op.sanitizer, op.sourceSpan));
  23037. break;
  23038. case OpKind.StyleProp:
  23039. if (op.expression instanceof Interpolation) {
  23040. OpList.replace(op, stylePropInterpolate(op.name, op.expression.strings, op.expression.expressions, op.unit, op.sourceSpan));
  23041. }
  23042. else {
  23043. OpList.replace(op, styleProp(op.name, op.expression, op.unit, op.sourceSpan));
  23044. }
  23045. break;
  23046. case OpKind.ClassProp:
  23047. OpList.replace(op, classProp(op.name, op.expression, op.sourceSpan));
  23048. break;
  23049. case OpKind.StyleMap:
  23050. if (op.expression instanceof Interpolation) {
  23051. OpList.replace(op, styleMapInterpolate(op.expression.strings, op.expression.expressions, op.sourceSpan));
  23052. }
  23053. else {
  23054. OpList.replace(op, styleMap(op.expression, op.sourceSpan));
  23055. }
  23056. break;
  23057. case OpKind.ClassMap:
  23058. if (op.expression instanceof Interpolation) {
  23059. OpList.replace(op, classMapInterpolate(op.expression.strings, op.expression.expressions, op.sourceSpan));
  23060. }
  23061. else {
  23062. OpList.replace(op, classMap(op.expression, op.sourceSpan));
  23063. }
  23064. break;
  23065. case OpKind.I18nExpression:
  23066. OpList.replace(op, i18nExp(op.expression, op.sourceSpan));
  23067. break;
  23068. case OpKind.I18nApply:
  23069. OpList.replace(op, i18nApply(op.handle.slot, op.sourceSpan));
  23070. break;
  23071. case OpKind.InterpolateText:
  23072. OpList.replace(op, textInterpolate(op.interpolation.strings, op.interpolation.expressions, op.sourceSpan));
  23073. break;
  23074. case OpKind.Attribute:
  23075. if (op.expression instanceof Interpolation) {
  23076. OpList.replace(op, attributeInterpolate(op.name, op.expression.strings, op.expression.expressions, op.sanitizer, op.sourceSpan));
  23077. }
  23078. else {
  23079. OpList.replace(op, attribute(op.name, op.expression, op.sanitizer, op.namespace));
  23080. }
  23081. break;
  23082. case OpKind.HostProperty:
  23083. if (op.expression instanceof Interpolation) {
  23084. throw new Error('not yet handled');
  23085. }
  23086. else {
  23087. if (op.isAnimationTrigger) {
  23088. OpList.replace(op, syntheticHostProperty(op.name, op.expression, op.sourceSpan));
  23089. }
  23090. else {
  23091. OpList.replace(op, hostProperty(op.name, op.expression, op.sanitizer, op.sourceSpan));
  23092. }
  23093. }
  23094. break;
  23095. case OpKind.Variable:
  23096. if (op.variable.name === null) {
  23097. throw new Error(`AssertionError: unnamed variable ${op.xref}`);
  23098. }
  23099. OpList.replace(op, createStatementOp(new DeclareVarStmt(op.variable.name, op.initializer, undefined, exports.StmtModifier.Final)));
  23100. break;
  23101. case OpKind.Conditional:
  23102. if (op.processed === null) {
  23103. throw new Error(`Conditional test was not set.`);
  23104. }
  23105. OpList.replace(op, conditional(op.processed, op.contextValue, op.sourceSpan));
  23106. break;
  23107. case OpKind.Repeater:
  23108. OpList.replace(op, repeater(op.collection, op.sourceSpan));
  23109. break;
  23110. case OpKind.DeferWhen:
  23111. OpList.replace(op, deferWhen(op.modifier, op.expr, op.sourceSpan));
  23112. break;
  23113. case OpKind.StoreLet:
  23114. throw new Error(`AssertionError: unexpected storeLet ${op.declaredName}`);
  23115. case OpKind.Statement:
  23116. // Pass statement operations directly through.
  23117. break;
  23118. default:
  23119. throw new Error(`AssertionError: Unsupported reification of update op ${OpKind[op.kind]}`);
  23120. }
  23121. }
  23122. }
  23123. function reifyIrExpression(expr) {
  23124. if (!isIrExpression(expr)) {
  23125. return expr;
  23126. }
  23127. switch (expr.kind) {
  23128. case ExpressionKind.NextContext:
  23129. return nextContext(expr.steps);
  23130. case ExpressionKind.Reference:
  23131. return reference(expr.targetSlot.slot + 1 + expr.offset);
  23132. case ExpressionKind.LexicalRead:
  23133. throw new Error(`AssertionError: unresolved LexicalRead of ${expr.name}`);
  23134. case ExpressionKind.TwoWayBindingSet:
  23135. throw new Error(`AssertionError: unresolved TwoWayBindingSet`);
  23136. case ExpressionKind.RestoreView:
  23137. if (typeof expr.view === 'number') {
  23138. throw new Error(`AssertionError: unresolved RestoreView`);
  23139. }
  23140. return restoreView(expr.view);
  23141. case ExpressionKind.ResetView:
  23142. return resetView(expr.expr);
  23143. case ExpressionKind.GetCurrentView:
  23144. return getCurrentView();
  23145. case ExpressionKind.ReadVariable:
  23146. if (expr.name === null) {
  23147. throw new Error(`Read of unnamed variable ${expr.xref}`);
  23148. }
  23149. return variable(expr.name);
  23150. case ExpressionKind.ReadTemporaryExpr:
  23151. if (expr.name === null) {
  23152. throw new Error(`Read of unnamed temporary ${expr.xref}`);
  23153. }
  23154. return variable(expr.name);
  23155. case ExpressionKind.AssignTemporaryExpr:
  23156. if (expr.name === null) {
  23157. throw new Error(`Assign of unnamed temporary ${expr.xref}`);
  23158. }
  23159. return variable(expr.name).set(expr.expr);
  23160. case ExpressionKind.PureFunctionExpr:
  23161. if (expr.fn === null) {
  23162. throw new Error(`AssertionError: expected PureFunctions to have been extracted`);
  23163. }
  23164. return pureFunction(expr.varOffset, expr.fn, expr.args);
  23165. case ExpressionKind.PureFunctionParameterExpr:
  23166. throw new Error(`AssertionError: expected PureFunctionParameterExpr to have been extracted`);
  23167. case ExpressionKind.PipeBinding:
  23168. return pipeBind(expr.targetSlot.slot, expr.varOffset, expr.args);
  23169. case ExpressionKind.PipeBindingVariadic:
  23170. return pipeBindV(expr.targetSlot.slot, expr.varOffset, expr.args);
  23171. case ExpressionKind.SlotLiteralExpr:
  23172. return literal$1(expr.slot.slot);
  23173. case ExpressionKind.ContextLetReference:
  23174. return readContextLet(expr.targetSlot.slot);
  23175. case ExpressionKind.StoreLet:
  23176. return storeLet(expr.value, expr.sourceSpan);
  23177. case ExpressionKind.TrackContext:
  23178. return variable('this');
  23179. default:
  23180. throw new Error(`AssertionError: Unsupported reification of ir.Expression kind: ${ExpressionKind[expr.kind]}`);
  23181. }
  23182. }
  23183. /**
  23184. * Listeners get turned into a function expression, which may or may not have the `$event`
  23185. * parameter defined.
  23186. */
  23187. function reifyListenerHandler(unit, name, handlerOps, consumesDollarEvent) {
  23188. // First, reify all instruction calls within `handlerOps`.
  23189. reifyUpdateOperations(unit, handlerOps);
  23190. // Next, extract all the `o.Statement`s from the reified operations. We can expect that at this
  23191. // point, all operations have been converted to statements.
  23192. const handlerStmts = [];
  23193. for (const op of handlerOps) {
  23194. if (op.kind !== OpKind.Statement) {
  23195. throw new Error(`AssertionError: expected reified statements, but found op ${OpKind[op.kind]}`);
  23196. }
  23197. handlerStmts.push(op.statement);
  23198. }
  23199. // If `$event` is referenced, we need to generate it as a parameter.
  23200. const params = [];
  23201. if (consumesDollarEvent) {
  23202. // We need the `$event` parameter.
  23203. params.push(new FnParam('$event'));
  23204. }
  23205. return fn(params, handlerStmts, undefined, undefined, name);
  23206. }
  23207. /** Reifies the tracking expression of a `RepeaterCreateOp`. */
  23208. function reifyTrackBy(unit, op) {
  23209. // If the tracking function was created already, there's nothing left to do.
  23210. if (op.trackByFn !== null) {
  23211. return op.trackByFn;
  23212. }
  23213. const params = [new FnParam('$index'), new FnParam('$item')];
  23214. let fn$1;
  23215. if (op.trackByOps === null) {
  23216. // If there are no additional ops related to the tracking function, we just need
  23217. // to turn it into a function that returns the result of the expression.
  23218. fn$1 = op.usesComponentInstance
  23219. ? fn(params, [new ReturnStatement(op.track)])
  23220. : arrowFn(params, op.track);
  23221. }
  23222. else {
  23223. // Otherwise first we need to reify the track-related ops.
  23224. reifyUpdateOperations(unit, op.trackByOps);
  23225. const statements = [];
  23226. for (const trackOp of op.trackByOps) {
  23227. if (trackOp.kind !== OpKind.Statement) {
  23228. throw new Error(`AssertionError: expected reified statements, but found op ${OpKind[trackOp.kind]}`);
  23229. }
  23230. statements.push(trackOp.statement);
  23231. }
  23232. // Afterwards we can create the function from those ops.
  23233. fn$1 =
  23234. op.usesComponentInstance ||
  23235. statements.length !== 1 ||
  23236. !(statements[0] instanceof ReturnStatement)
  23237. ? fn(params, statements)
  23238. : arrowFn(params, statements[0].value);
  23239. }
  23240. op.trackByFn = unit.job.pool.getSharedFunctionReference(fn$1, '_forTrack');
  23241. return op.trackByFn;
  23242. }
  23243. /**
  23244. * Binding with no content can be safely deleted.
  23245. */
  23246. function removeEmptyBindings(job) {
  23247. for (const unit of job.units) {
  23248. for (const op of unit.update) {
  23249. switch (op.kind) {
  23250. case OpKind.Attribute:
  23251. case OpKind.Binding:
  23252. case OpKind.ClassProp:
  23253. case OpKind.ClassMap:
  23254. case OpKind.Property:
  23255. case OpKind.StyleProp:
  23256. case OpKind.StyleMap:
  23257. if (op.expression instanceof EmptyExpr) {
  23258. OpList.remove(op);
  23259. }
  23260. break;
  23261. }
  23262. }
  23263. }
  23264. }
  23265. /**
  23266. * Remove the i18n context ops after they are no longer needed, and null out references to them to
  23267. * be safe.
  23268. */
  23269. function removeI18nContexts(job) {
  23270. for (const unit of job.units) {
  23271. for (const op of unit.create) {
  23272. switch (op.kind) {
  23273. case OpKind.I18nContext:
  23274. OpList.remove(op);
  23275. break;
  23276. case OpKind.I18nStart:
  23277. op.context = null;
  23278. break;
  23279. }
  23280. }
  23281. }
  23282. }
  23283. /**
  23284. * i18nAttributes ops will be generated for each i18n attribute. However, not all i18n attribues
  23285. * will contain dynamic content, and so some of these i18nAttributes ops may be unnecessary.
  23286. */
  23287. function removeUnusedI18nAttributesOps(job) {
  23288. for (const unit of job.units) {
  23289. const ownersWithI18nExpressions = new Set();
  23290. for (const op of unit.update) {
  23291. switch (op.kind) {
  23292. case OpKind.I18nExpression:
  23293. ownersWithI18nExpressions.add(op.i18nOwner);
  23294. }
  23295. }
  23296. for (const op of unit.create) {
  23297. switch (op.kind) {
  23298. case OpKind.I18nAttributes:
  23299. if (ownersWithI18nExpressions.has(op.xref)) {
  23300. continue;
  23301. }
  23302. OpList.remove(op);
  23303. }
  23304. }
  23305. }
  23306. }
  23307. /**
  23308. * Resolves `ir.ContextExpr` expressions (which represent embedded view or component contexts) to
  23309. * either the `ctx` parameter to component functions (for the current view context) or to variables
  23310. * that store those contexts (for contexts accessed via the `nextContext()` instruction).
  23311. */
  23312. function resolveContexts(job) {
  23313. for (const unit of job.units) {
  23314. processLexicalScope$1(unit, unit.create);
  23315. processLexicalScope$1(unit, unit.update);
  23316. }
  23317. }
  23318. function processLexicalScope$1(view, ops) {
  23319. // Track the expressions used to access all available contexts within the current view, by the
  23320. // view `ir.XrefId`.
  23321. const scope = new Map();
  23322. // The current view's context is accessible via the `ctx` parameter.
  23323. scope.set(view.xref, variable('ctx'));
  23324. for (const op of ops) {
  23325. switch (op.kind) {
  23326. case OpKind.Variable:
  23327. switch (op.variable.kind) {
  23328. case SemanticVariableKind.Context:
  23329. scope.set(op.variable.view, new ReadVariableExpr(op.xref));
  23330. break;
  23331. }
  23332. break;
  23333. case OpKind.Listener:
  23334. case OpKind.TwoWayListener:
  23335. processLexicalScope$1(view, op.handlerOps);
  23336. break;
  23337. case OpKind.RepeaterCreate:
  23338. if (op.trackByOps !== null) {
  23339. processLexicalScope$1(view, op.trackByOps);
  23340. }
  23341. break;
  23342. }
  23343. }
  23344. if (view === view.job.root) {
  23345. // Prefer `ctx` of the root view to any variables which happen to contain the root context.
  23346. scope.set(view.xref, variable('ctx'));
  23347. }
  23348. for (const op of ops) {
  23349. transformExpressionsInOp(op, (expr) => {
  23350. if (expr instanceof ContextExpr) {
  23351. if (!scope.has(expr.view)) {
  23352. throw new Error(`No context found for reference to view ${expr.view} from view ${view.xref}`);
  23353. }
  23354. return scope.get(expr.view);
  23355. }
  23356. else {
  23357. return expr;
  23358. }
  23359. }, VisitorContextFlag.None);
  23360. }
  23361. }
  23362. /**
  23363. * Any variable inside a listener with the name `$event` will be transformed into a output lexical
  23364. * read immediately, and does not participate in any of the normal logic for handling variables.
  23365. */
  23366. function resolveDollarEvent(job) {
  23367. for (const unit of job.units) {
  23368. transformDollarEvent(unit.create);
  23369. transformDollarEvent(unit.update);
  23370. }
  23371. }
  23372. function transformDollarEvent(ops) {
  23373. for (const op of ops) {
  23374. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  23375. transformExpressionsInOp(op, (expr) => {
  23376. if (expr instanceof LexicalReadExpr && expr.name === '$event') {
  23377. // Two-way listeners always consume `$event` so they omit this field.
  23378. if (op.kind === OpKind.Listener) {
  23379. op.consumesDollarEvent = true;
  23380. }
  23381. return new ReadVarExpr(expr.name);
  23382. }
  23383. return expr;
  23384. }, VisitorContextFlag.InChildOperation);
  23385. }
  23386. }
  23387. }
  23388. /**
  23389. * Resolve the element placeholders in i18n messages.
  23390. */
  23391. function resolveI18nElementPlaceholders(job) {
  23392. // Record all of the element and i18n context ops for use later.
  23393. const i18nContexts = new Map();
  23394. const elements = new Map();
  23395. for (const unit of job.units) {
  23396. for (const op of unit.create) {
  23397. switch (op.kind) {
  23398. case OpKind.I18nContext:
  23399. i18nContexts.set(op.xref, op);
  23400. break;
  23401. case OpKind.ElementStart:
  23402. elements.set(op.xref, op);
  23403. break;
  23404. }
  23405. }
  23406. }
  23407. resolvePlaceholdersForView(job, job.root, i18nContexts, elements);
  23408. }
  23409. /**
  23410. * Recursively resolves element and template tag placeholders in the given view.
  23411. */
  23412. function resolvePlaceholdersForView(job, unit, i18nContexts, elements, pendingStructuralDirective) {
  23413. // Track the current i18n op and corresponding i18n context op as we step through the creation
  23414. // IR.
  23415. let currentOps = null;
  23416. let pendingStructuralDirectiveCloses = new Map();
  23417. for (const op of unit.create) {
  23418. switch (op.kind) {
  23419. case OpKind.I18nStart:
  23420. if (!op.context) {
  23421. throw Error('Could not find i18n context for i18n op');
  23422. }
  23423. currentOps = { i18nBlock: op, i18nContext: i18nContexts.get(op.context) };
  23424. break;
  23425. case OpKind.I18nEnd:
  23426. currentOps = null;
  23427. break;
  23428. case OpKind.ElementStart:
  23429. // For elements with i18n placeholders, record its slot value in the params map under the
  23430. // corresponding tag start placeholder.
  23431. if (op.i18nPlaceholder !== undefined) {
  23432. if (currentOps === null) {
  23433. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23434. }
  23435. recordElementStart(op, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23436. // If there is a separate close tag placeholder for this element, save the pending
  23437. // structural directive so we can pass it to the closing tag as well.
  23438. if (pendingStructuralDirective && op.i18nPlaceholder.closeName) {
  23439. pendingStructuralDirectiveCloses.set(op.xref, pendingStructuralDirective);
  23440. }
  23441. // Clear out the pending structural directive now that its been accounted for.
  23442. pendingStructuralDirective = undefined;
  23443. }
  23444. break;
  23445. case OpKind.ElementEnd:
  23446. // For elements with i18n placeholders, record its slot value in the params map under the
  23447. // corresponding tag close placeholder.
  23448. const startOp = elements.get(op.xref);
  23449. if (startOp && startOp.i18nPlaceholder !== undefined) {
  23450. if (currentOps === null) {
  23451. throw Error('AssertionError: i18n tag placeholder should only occur inside an i18n block');
  23452. }
  23453. recordElementClose(startOp, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirectiveCloses.get(op.xref));
  23454. // Clear out the pending structural directive close that was accounted for.
  23455. pendingStructuralDirectiveCloses.delete(op.xref);
  23456. }
  23457. break;
  23458. case OpKind.Projection:
  23459. // For content projections with i18n placeholders, record its slot value in the params map
  23460. // under the corresponding tag start and close placeholders.
  23461. if (op.i18nPlaceholder !== undefined) {
  23462. if (currentOps === null) {
  23463. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23464. }
  23465. recordElementStart(op, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23466. recordElementClose(op, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23467. // Clear out the pending structural directive now that its been accounted for.
  23468. pendingStructuralDirective = undefined;
  23469. }
  23470. break;
  23471. case OpKind.Template:
  23472. const view = job.views.get(op.xref);
  23473. if (op.i18nPlaceholder === undefined) {
  23474. // If there is no i18n placeholder, just recurse into the view in case it contains i18n
  23475. // blocks.
  23476. resolvePlaceholdersForView(job, view, i18nContexts, elements);
  23477. }
  23478. else {
  23479. if (currentOps === null) {
  23480. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23481. }
  23482. if (op.templateKind === TemplateKind.Structural) {
  23483. // If this is a structural directive template, don't record anything yet. Instead pass
  23484. // the current template as a pending structural directive to be recorded when we find
  23485. // the element, content, or template it belongs to. This allows us to create combined
  23486. // values that represent, e.g. the start of a template and element at the same time.
  23487. resolvePlaceholdersForView(job, view, i18nContexts, elements, op);
  23488. }
  23489. else {
  23490. // If this is some other kind of template, we can record its start, recurse into its
  23491. // view, and then record its end.
  23492. recordTemplateStart(job, view, op.handle.slot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23493. resolvePlaceholdersForView(job, view, i18nContexts, elements);
  23494. recordTemplateClose(job, view, op.handle.slot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23495. pendingStructuralDirective = undefined;
  23496. }
  23497. }
  23498. break;
  23499. case OpKind.RepeaterCreate:
  23500. if (pendingStructuralDirective !== undefined) {
  23501. throw Error('AssertionError: Unexpected structural directive associated with @for block');
  23502. }
  23503. // RepeaterCreate has 3 slots: the first is for the op itself, the second is for the @for
  23504. // template and the (optional) third is for the @empty template.
  23505. const forSlot = op.handle.slot + 1;
  23506. const forView = job.views.get(op.xref);
  23507. // First record all of the placeholders for the @for template.
  23508. if (op.i18nPlaceholder === undefined) {
  23509. // If there is no i18n placeholder, just recurse into the view in case it contains i18n
  23510. // blocks.
  23511. resolvePlaceholdersForView(job, forView, i18nContexts, elements);
  23512. }
  23513. else {
  23514. if (currentOps === null) {
  23515. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23516. }
  23517. recordTemplateStart(job, forView, forSlot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23518. resolvePlaceholdersForView(job, forView, i18nContexts, elements);
  23519. recordTemplateClose(job, forView, forSlot, op.i18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23520. pendingStructuralDirective = undefined;
  23521. }
  23522. // Then if there's an @empty template, add its placeholders as well.
  23523. if (op.emptyView !== null) {
  23524. // RepeaterCreate has 3 slots: the first is for the op itself, the second is for the @for
  23525. // template and the (optional) third is for the @empty template.
  23526. const emptySlot = op.handle.slot + 2;
  23527. const emptyView = job.views.get(op.emptyView);
  23528. if (op.emptyI18nPlaceholder === undefined) {
  23529. // If there is no i18n placeholder, just recurse into the view in case it contains i18n
  23530. // blocks.
  23531. resolvePlaceholdersForView(job, emptyView, i18nContexts, elements);
  23532. }
  23533. else {
  23534. if (currentOps === null) {
  23535. throw Error('i18n tag placeholder should only occur inside an i18n block');
  23536. }
  23537. recordTemplateStart(job, emptyView, emptySlot, op.emptyI18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23538. resolvePlaceholdersForView(job, emptyView, i18nContexts, elements);
  23539. recordTemplateClose(job, emptyView, emptySlot, op.emptyI18nPlaceholder, currentOps.i18nContext, currentOps.i18nBlock, pendingStructuralDirective);
  23540. pendingStructuralDirective = undefined;
  23541. }
  23542. }
  23543. break;
  23544. }
  23545. }
  23546. }
  23547. /**
  23548. * Records an i18n param value for the start of an element.
  23549. */
  23550. function recordElementStart(op, i18nContext, i18nBlock, structuralDirective) {
  23551. const { startName, closeName } = op.i18nPlaceholder;
  23552. let flags = I18nParamValueFlags.ElementTag | I18nParamValueFlags.OpenTag;
  23553. let value = op.handle.slot;
  23554. // If the element is associated with a structural directive, start it as well.
  23555. if (structuralDirective !== undefined) {
  23556. flags |= I18nParamValueFlags.TemplateTag;
  23557. value = { element: value, template: structuralDirective.handle.slot };
  23558. }
  23559. // For self-closing tags, there is no close tag placeholder. Instead, the start tag
  23560. // placeholder accounts for the start and close of the element.
  23561. if (!closeName) {
  23562. flags |= I18nParamValueFlags.CloseTag;
  23563. }
  23564. addParam(i18nContext.params, startName, value, i18nBlock.subTemplateIndex, flags);
  23565. }
  23566. /**
  23567. * Records an i18n param value for the closing of an element.
  23568. */
  23569. function recordElementClose(op, i18nContext, i18nBlock, structuralDirective) {
  23570. const { closeName } = op.i18nPlaceholder;
  23571. // Self-closing tags don't have a closing tag placeholder, instead the element closing is
  23572. // recorded via an additional flag on the element start value.
  23573. if (closeName) {
  23574. let flags = I18nParamValueFlags.ElementTag | I18nParamValueFlags.CloseTag;
  23575. let value = op.handle.slot;
  23576. // If the element is associated with a structural directive, close it as well.
  23577. if (structuralDirective !== undefined) {
  23578. flags |= I18nParamValueFlags.TemplateTag;
  23579. value = { element: value, template: structuralDirective.handle.slot };
  23580. }
  23581. addParam(i18nContext.params, closeName, value, i18nBlock.subTemplateIndex, flags);
  23582. }
  23583. }
  23584. /**
  23585. * Records an i18n param value for the start of a template.
  23586. */
  23587. function recordTemplateStart(job, view, slot, i18nPlaceholder, i18nContext, i18nBlock, structuralDirective) {
  23588. let { startName, closeName } = i18nPlaceholder;
  23589. let flags = I18nParamValueFlags.TemplateTag | I18nParamValueFlags.OpenTag;
  23590. // For self-closing tags, there is no close tag placeholder. Instead, the start tag
  23591. // placeholder accounts for the start and close of the element.
  23592. if (!closeName) {
  23593. flags |= I18nParamValueFlags.CloseTag;
  23594. }
  23595. // If the template is associated with a structural directive, record the structural directive's
  23596. // start first. Since this template must be in the structural directive's view, we can just
  23597. // directly use the current i18n block's sub-template index.
  23598. if (structuralDirective !== undefined) {
  23599. addParam(i18nContext.params, startName, structuralDirective.handle.slot, i18nBlock.subTemplateIndex, flags);
  23600. }
  23601. // Record the start of the template. For the sub-template index, pass the index for the template's
  23602. // view, rather than the current i18n block's index.
  23603. addParam(i18nContext.params, startName, slot, getSubTemplateIndexForTemplateTag(job, i18nBlock, view), flags);
  23604. }
  23605. /**
  23606. * Records an i18n param value for the closing of a template.
  23607. */
  23608. function recordTemplateClose(job, view, slot, i18nPlaceholder, i18nContext, i18nBlock, structuralDirective) {
  23609. const { closeName } = i18nPlaceholder;
  23610. const flags = I18nParamValueFlags.TemplateTag | I18nParamValueFlags.CloseTag;
  23611. // Self-closing tags don't have a closing tag placeholder, instead the template's closing is
  23612. // recorded via an additional flag on the template start value.
  23613. if (closeName) {
  23614. // Record the closing of the template. For the sub-template index, pass the index for the
  23615. // template's view, rather than the current i18n block's index.
  23616. addParam(i18nContext.params, closeName, slot, getSubTemplateIndexForTemplateTag(job, i18nBlock, view), flags);
  23617. // If the template is associated with a structural directive, record the structural directive's
  23618. // closing after. Since this template must be in the structural directive's view, we can just
  23619. // directly use the current i18n block's sub-template index.
  23620. if (structuralDirective !== undefined) {
  23621. addParam(i18nContext.params, closeName, structuralDirective.handle.slot, i18nBlock.subTemplateIndex, flags);
  23622. }
  23623. }
  23624. }
  23625. /**
  23626. * Get the subTemplateIndex for the given template op. For template ops, use the subTemplateIndex of
  23627. * the child i18n block inside the template.
  23628. */
  23629. function getSubTemplateIndexForTemplateTag(job, i18nOp, view) {
  23630. for (const childOp of view.create) {
  23631. if (childOp.kind === OpKind.I18nStart) {
  23632. return childOp.subTemplateIndex;
  23633. }
  23634. }
  23635. return i18nOp.subTemplateIndex;
  23636. }
  23637. /**
  23638. * Add a param value to the given params map.
  23639. */
  23640. function addParam(params, placeholder, value, subTemplateIndex, flags) {
  23641. const values = params.get(placeholder) ?? [];
  23642. values.push({ value, subTemplateIndex, flags });
  23643. params.set(placeholder, values);
  23644. }
  23645. /**
  23646. * Resolve the i18n expression placeholders in i18n messages.
  23647. */
  23648. function resolveI18nExpressionPlaceholders(job) {
  23649. // Record all of the i18n context ops, and the sub-template index for each i18n op.
  23650. const subTemplateIndices = new Map();
  23651. const i18nContexts = new Map();
  23652. const icuPlaceholders = new Map();
  23653. for (const unit of job.units) {
  23654. for (const op of unit.create) {
  23655. switch (op.kind) {
  23656. case OpKind.I18nStart:
  23657. subTemplateIndices.set(op.xref, op.subTemplateIndex);
  23658. break;
  23659. case OpKind.I18nContext:
  23660. i18nContexts.set(op.xref, op);
  23661. break;
  23662. case OpKind.IcuPlaceholder:
  23663. icuPlaceholders.set(op.xref, op);
  23664. break;
  23665. }
  23666. }
  23667. }
  23668. // Keep track of the next available expression index for each i18n message.
  23669. const expressionIndices = new Map();
  23670. // Keep track of a reference index for each expression.
  23671. // We use different references for normal i18n expressio and attribute i18n expressions. This is
  23672. // because child i18n blocks in templates don't get their own context, since they're rolled into
  23673. // the translated message of the parent, but they may target a different slot.
  23674. const referenceIndex = (op) => op.usage === I18nExpressionFor.I18nText ? op.i18nOwner : op.context;
  23675. for (const unit of job.units) {
  23676. for (const op of unit.update) {
  23677. if (op.kind === OpKind.I18nExpression) {
  23678. const index = expressionIndices.get(referenceIndex(op)) || 0;
  23679. const subTemplateIndex = subTemplateIndices.get(op.i18nOwner) ?? null;
  23680. const value = {
  23681. value: index,
  23682. subTemplateIndex: subTemplateIndex,
  23683. flags: I18nParamValueFlags.ExpressionIndex,
  23684. };
  23685. updatePlaceholder(op, value, i18nContexts, icuPlaceholders);
  23686. expressionIndices.set(referenceIndex(op), index + 1);
  23687. }
  23688. }
  23689. }
  23690. }
  23691. function updatePlaceholder(op, value, i18nContexts, icuPlaceholders) {
  23692. if (op.i18nPlaceholder !== null) {
  23693. const i18nContext = i18nContexts.get(op.context);
  23694. const params = op.resolutionTime === I18nParamResolutionTime.Creation
  23695. ? i18nContext.params
  23696. : i18nContext.postprocessingParams;
  23697. const values = params.get(op.i18nPlaceholder) || [];
  23698. values.push(value);
  23699. params.set(op.i18nPlaceholder, values);
  23700. }
  23701. if (op.icuPlaceholder !== null) {
  23702. const icuPlaceholderOp = icuPlaceholders.get(op.icuPlaceholder);
  23703. icuPlaceholderOp?.expressionPlaceholders.push(value);
  23704. }
  23705. }
  23706. /**
  23707. * Resolves lexical references in views (`ir.LexicalReadExpr`) to either a target variable or to
  23708. * property reads on the top-level component context.
  23709. *
  23710. * Also matches `ir.RestoreViewExpr` expressions with the variables of their corresponding saved
  23711. * views.
  23712. */
  23713. function resolveNames(job) {
  23714. for (const unit of job.units) {
  23715. processLexicalScope(unit, unit.create, null);
  23716. processLexicalScope(unit, unit.update, null);
  23717. }
  23718. }
  23719. function processLexicalScope(unit, ops, savedView) {
  23720. // Maps names defined in the lexical scope of this template to the `ir.XrefId`s of the variable
  23721. // declarations which represent those values.
  23722. //
  23723. // Since variables are generated in each view for the entire lexical scope (including any
  23724. // identifiers from parent templates) only local variables need be considered here.
  23725. const scope = new Map();
  23726. // Symbols defined within the current scope. They take precedence over ones defined outside.
  23727. const localDefinitions = new Map();
  23728. // First, step through the operations list and:
  23729. // 1) build up the `scope` mapping
  23730. // 2) recurse into any listener functions
  23731. for (const op of ops) {
  23732. switch (op.kind) {
  23733. case OpKind.Variable:
  23734. switch (op.variable.kind) {
  23735. case SemanticVariableKind.Identifier:
  23736. if (op.variable.local) {
  23737. if (localDefinitions.has(op.variable.identifier)) {
  23738. continue;
  23739. }
  23740. localDefinitions.set(op.variable.identifier, op.xref);
  23741. }
  23742. else if (scope.has(op.variable.identifier)) {
  23743. continue;
  23744. }
  23745. scope.set(op.variable.identifier, op.xref);
  23746. break;
  23747. case SemanticVariableKind.Alias:
  23748. // This variable represents some kind of identifier which can be used in the template.
  23749. if (scope.has(op.variable.identifier)) {
  23750. continue;
  23751. }
  23752. scope.set(op.variable.identifier, op.xref);
  23753. break;
  23754. case SemanticVariableKind.SavedView:
  23755. // This variable represents a snapshot of the current view context, and can be used to
  23756. // restore that context within listener functions.
  23757. savedView = {
  23758. view: op.variable.view,
  23759. variable: op.xref,
  23760. };
  23761. break;
  23762. }
  23763. break;
  23764. case OpKind.Listener:
  23765. case OpKind.TwoWayListener:
  23766. // Listener functions have separate variable declarations, so process them as a separate
  23767. // lexical scope.
  23768. processLexicalScope(unit, op.handlerOps, savedView);
  23769. break;
  23770. case OpKind.RepeaterCreate:
  23771. if (op.trackByOps !== null) {
  23772. processLexicalScope(unit, op.trackByOps, savedView);
  23773. }
  23774. break;
  23775. }
  23776. }
  23777. // Next, use the `scope` mapping to match `ir.LexicalReadExpr` with defined names in the lexical
  23778. // scope. Also, look for `ir.RestoreViewExpr`s and match them with the snapshotted view context
  23779. // variable.
  23780. for (const op of ops) {
  23781. if (op.kind == OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  23782. // Listeners were already processed above with their own scopes.
  23783. continue;
  23784. }
  23785. transformExpressionsInOp(op, (expr) => {
  23786. if (expr instanceof LexicalReadExpr) {
  23787. // `expr` is a read of a name within the lexical scope of this view.
  23788. // Either that name is defined within the current view, or it represents a property from the
  23789. // main component context.
  23790. if (localDefinitions.has(expr.name)) {
  23791. return new ReadVariableExpr(localDefinitions.get(expr.name));
  23792. }
  23793. else if (scope.has(expr.name)) {
  23794. // This was a defined variable in the current scope.
  23795. return new ReadVariableExpr(scope.get(expr.name));
  23796. }
  23797. else {
  23798. // Reading from the component context.
  23799. return new ReadPropExpr(new ContextExpr(unit.job.root.xref), expr.name);
  23800. }
  23801. }
  23802. else if (expr instanceof RestoreViewExpr && typeof expr.view === 'number') {
  23803. // `ir.RestoreViewExpr` happens in listener functions and restores a saved view from the
  23804. // parent creation list. We expect to find that we captured the `savedView` previously, and
  23805. // that it matches the expected view to be restored.
  23806. if (savedView === null || savedView.view !== expr.view) {
  23807. throw new Error(`AssertionError: no saved view ${expr.view} from view ${unit.xref}`);
  23808. }
  23809. expr.view = new ReadVariableExpr(savedView.variable);
  23810. return expr;
  23811. }
  23812. else {
  23813. return expr;
  23814. }
  23815. }, VisitorContextFlag.None);
  23816. }
  23817. for (const op of ops) {
  23818. visitExpressionsInOp(op, (expr) => {
  23819. if (expr instanceof LexicalReadExpr) {
  23820. throw new Error(`AssertionError: no lexical reads should remain, but found read of ${expr.name}`);
  23821. }
  23822. });
  23823. }
  23824. }
  23825. /**
  23826. * Map of security contexts to their sanitizer function.
  23827. */
  23828. const sanitizerFns = new Map([
  23829. [SecurityContext.HTML, Identifiers.sanitizeHtml],
  23830. [SecurityContext.RESOURCE_URL, Identifiers.sanitizeResourceUrl],
  23831. [SecurityContext.SCRIPT, Identifiers.sanitizeScript],
  23832. [SecurityContext.STYLE, Identifiers.sanitizeStyle],
  23833. [SecurityContext.URL, Identifiers.sanitizeUrl],
  23834. ]);
  23835. /**
  23836. * Map of security contexts to their trusted value function.
  23837. */
  23838. const trustedValueFns = new Map([
  23839. [SecurityContext.HTML, Identifiers.trustConstantHtml],
  23840. [SecurityContext.RESOURCE_URL, Identifiers.trustConstantResourceUrl],
  23841. ]);
  23842. /**
  23843. * Resolves sanitization functions for ops that need them.
  23844. */
  23845. function resolveSanitizers(job) {
  23846. for (const unit of job.units) {
  23847. const elements = createOpXrefMap(unit);
  23848. // For normal element bindings we create trusted values for security sensitive constant
  23849. // attributes. However, for host bindings we skip this step (this matches what
  23850. // TemplateDefinitionBuilder does).
  23851. // TODO: Is the TDB behavior correct here?
  23852. if (job.kind !== CompilationJobKind.Host) {
  23853. for (const op of unit.create) {
  23854. if (op.kind === OpKind.ExtractedAttribute) {
  23855. const trustedValueFn = trustedValueFns.get(getOnlySecurityContext(op.securityContext)) ?? null;
  23856. op.trustedValueFn = trustedValueFn !== null ? importExpr(trustedValueFn) : null;
  23857. }
  23858. }
  23859. }
  23860. for (const op of unit.update) {
  23861. switch (op.kind) {
  23862. case OpKind.Property:
  23863. case OpKind.Attribute:
  23864. case OpKind.HostProperty:
  23865. let sanitizerFn = null;
  23866. if (Array.isArray(op.securityContext) &&
  23867. op.securityContext.length === 2 &&
  23868. op.securityContext.indexOf(SecurityContext.URL) > -1 &&
  23869. op.securityContext.indexOf(SecurityContext.RESOURCE_URL) > -1) {
  23870. // When the host element isn't known, some URL attributes (such as "src" and "href") may
  23871. // be part of multiple different security contexts. In this case we use special
  23872. // sanitization function and select the actual sanitizer at runtime based on a tag name
  23873. // that is provided while invoking sanitization function.
  23874. sanitizerFn = Identifiers.sanitizeUrlOrResourceUrl;
  23875. }
  23876. else {
  23877. sanitizerFn = sanitizerFns.get(getOnlySecurityContext(op.securityContext)) ?? null;
  23878. }
  23879. op.sanitizer = sanitizerFn !== null ? importExpr(sanitizerFn) : null;
  23880. // If there was no sanitization function found based on the security context of an
  23881. // attribute/property, check whether this attribute/property is one of the
  23882. // security-sensitive <iframe> attributes (and that the current element is actually an
  23883. // <iframe>).
  23884. if (op.sanitizer === null) {
  23885. let isIframe = false;
  23886. if (job.kind === CompilationJobKind.Host || op.kind === OpKind.HostProperty) {
  23887. // Note: for host bindings defined on a directive, we do not try to find all
  23888. // possible places where it can be matched, so we can not determine whether
  23889. // the host element is an <iframe>. In this case, we just assume it is and append a
  23890. // validation function, which is invoked at runtime and would have access to the
  23891. // underlying DOM element to check if it's an <iframe> and if so - run extra checks.
  23892. isIframe = true;
  23893. }
  23894. else {
  23895. // For a normal binding we can just check if the element its on is an iframe.
  23896. const ownerOp = elements.get(op.target);
  23897. if (ownerOp === undefined || !isElementOrContainerOp(ownerOp)) {
  23898. throw Error('Property should have an element-like owner');
  23899. }
  23900. isIframe = isIframeElement(ownerOp);
  23901. }
  23902. if (isIframe && isIframeSecuritySensitiveAttr(op.name)) {
  23903. op.sanitizer = importExpr(Identifiers.validateIframeAttribute);
  23904. }
  23905. }
  23906. break;
  23907. }
  23908. }
  23909. }
  23910. }
  23911. /**
  23912. * Checks whether the given op represents an iframe element.
  23913. */
  23914. function isIframeElement(op) {
  23915. return op.kind === OpKind.ElementStart && op.tag?.toLowerCase() === 'iframe';
  23916. }
  23917. /**
  23918. * Asserts that there is only a single security context and returns it.
  23919. */
  23920. function getOnlySecurityContext(securityContext) {
  23921. if (Array.isArray(securityContext)) {
  23922. if (securityContext.length > 1) {
  23923. // TODO: What should we do here? TDB just took the first one, but this feels like something we
  23924. // would want to know about and create a special case for like we did for Url/ResourceUrl. My
  23925. // guess is that, outside of the Url/ResourceUrl case, this never actually happens. If there
  23926. // do turn out to be other cases, throwing an error until we can address it feels safer.
  23927. throw Error(`AssertionError: Ambiguous security context`);
  23928. }
  23929. return securityContext[0] || SecurityContext.NONE;
  23930. }
  23931. return securityContext;
  23932. }
  23933. /**
  23934. * Transforms a `TwoWayBindingSet` expression into an expression that either
  23935. * sets a value through the `twoWayBindingSet` instruction or falls back to setting
  23936. * the value directly. E.g. the expression `TwoWayBindingSet(target, value)` becomes:
  23937. * `ng.twoWayBindingSet(target, value) || (target = value)`.
  23938. */
  23939. function transformTwoWayBindingSet(job) {
  23940. for (const unit of job.units) {
  23941. for (const op of unit.create) {
  23942. if (op.kind === OpKind.TwoWayListener) {
  23943. transformExpressionsInOp(op, (expr) => {
  23944. if (!(expr instanceof TwoWayBindingSetExpr)) {
  23945. return expr;
  23946. }
  23947. const { target, value } = expr;
  23948. if (target instanceof ReadPropExpr || target instanceof ReadKeyExpr) {
  23949. return twoWayBindingSet(target, value).or(target.set(value));
  23950. }
  23951. // ASSUMPTION: here we're assuming that `ReadVariableExpr` will be a reference
  23952. // to a local template variable. This appears to be the case at the time of writing.
  23953. // If the expression is targeting a variable read, we only emit the `twoWayBindingSet`
  23954. // since the fallback would be attempting to write into a constant. Invalid usages will be
  23955. // flagged during template type checking.
  23956. if (target instanceof ReadVariableExpr) {
  23957. return twoWayBindingSet(target, value);
  23958. }
  23959. throw new Error(`Unsupported expression in two-way action binding.`);
  23960. }, VisitorContextFlag.InChildOperation);
  23961. }
  23962. }
  23963. }
  23964. }
  23965. /**
  23966. * When inside of a listener, we may need access to one or more enclosing views. Therefore, each
  23967. * view should save the current view, and each listener must have the ability to restore the
  23968. * appropriate view. We eagerly generate all save view variables; they will be optimized away later.
  23969. */
  23970. function saveAndRestoreView(job) {
  23971. for (const unit of job.units) {
  23972. unit.create.prepend([
  23973. createVariableOp(unit.job.allocateXrefId(), {
  23974. kind: SemanticVariableKind.SavedView,
  23975. name: null,
  23976. view: unit.xref,
  23977. }, new GetCurrentViewExpr(), VariableFlags.None),
  23978. ]);
  23979. for (const op of unit.create) {
  23980. if (op.kind !== OpKind.Listener && op.kind !== OpKind.TwoWayListener) {
  23981. continue;
  23982. }
  23983. // Embedded views always need the save/restore view operation.
  23984. let needsRestoreView = unit !== job.root;
  23985. if (!needsRestoreView) {
  23986. for (const handlerOp of op.handlerOps) {
  23987. visitExpressionsInOp(handlerOp, (expr) => {
  23988. if (expr instanceof ReferenceExpr || expr instanceof ContextLetReferenceExpr) {
  23989. // Listeners that reference() a local ref need the save/restore view operation.
  23990. needsRestoreView = true;
  23991. }
  23992. });
  23993. }
  23994. }
  23995. if (needsRestoreView) {
  23996. addSaveRestoreViewOperationToListener(unit, op);
  23997. }
  23998. }
  23999. }
  24000. }
  24001. function addSaveRestoreViewOperationToListener(unit, op) {
  24002. op.handlerOps.prepend([
  24003. createVariableOp(unit.job.allocateXrefId(), {
  24004. kind: SemanticVariableKind.Context,
  24005. name: null,
  24006. view: unit.xref,
  24007. }, new RestoreViewExpr(unit.xref), VariableFlags.None),
  24008. ]);
  24009. // The "restore view" operation in listeners requires a call to `resetView` to reset the
  24010. // context prior to returning from the listener operation. Find any `return` statements in
  24011. // the listener body and wrap them in a call to reset the view.
  24012. for (const handlerOp of op.handlerOps) {
  24013. if (handlerOp.kind === OpKind.Statement &&
  24014. handlerOp.statement instanceof ReturnStatement) {
  24015. handlerOp.statement.value = new ResetViewExpr(handlerOp.statement.value);
  24016. }
  24017. }
  24018. }
  24019. /**
  24020. * Assign data slots for all operations which implement `ConsumesSlotOpTrait`, and propagate the
  24021. * assigned data slots of those operations to any expressions which reference them via
  24022. * `UsesSlotIndexTrait`.
  24023. *
  24024. * This phase is also responsible for counting the number of slots used for each view (its `decls`)
  24025. * and propagating that number into the `Template` operations which declare embedded views.
  24026. */
  24027. function allocateSlots(job) {
  24028. // Map of all declarations in all views within the component which require an assigned slot index.
  24029. // This map needs to be global (across all views within the component) since it's possible to
  24030. // reference a slot from one view from an expression within another (e.g. local references work
  24031. // this way).
  24032. const slotMap = new Map();
  24033. // Process all views in the component and assign slot indexes.
  24034. for (const unit of job.units) {
  24035. // Slot indices start at 0 for each view (and are not unique between views).
  24036. let slotCount = 0;
  24037. for (const op of unit.create) {
  24038. // Only consider declarations which consume data slots.
  24039. if (!hasConsumesSlotTrait(op)) {
  24040. continue;
  24041. }
  24042. // Assign slots to this declaration starting at the current `slotCount`.
  24043. op.handle.slot = slotCount;
  24044. // And track its assigned slot in the `slotMap`.
  24045. slotMap.set(op.xref, op.handle.slot);
  24046. // Each declaration may use more than 1 slot, so increment `slotCount` to reserve the number
  24047. // of slots required.
  24048. slotCount += op.numSlotsUsed;
  24049. }
  24050. // Record the total number of slots used on the view itself. This will later be propagated into
  24051. // `ir.TemplateOp`s which declare those views (except for the root view).
  24052. unit.decls = slotCount;
  24053. }
  24054. // After slot assignment, `slotMap` now contains slot assignments for every declaration in the
  24055. // whole template, across all views. Next, look for expressions which implement
  24056. // `UsesSlotIndexExprTrait` and propagate the assigned slot indexes into them.
  24057. // Additionally, this second scan allows us to find `ir.TemplateOp`s which declare views and
  24058. // propagate the number of slots used for each view into the operation which declares it.
  24059. for (const unit of job.units) {
  24060. for (const op of unit.ops()) {
  24061. if (op.kind === OpKind.Template || op.kind === OpKind.RepeaterCreate) {
  24062. // Record the number of slots used by the view this `ir.TemplateOp` declares in the
  24063. // operation itself, so it can be emitted later.
  24064. const childView = job.views.get(op.xref);
  24065. op.decls = childView.decls;
  24066. // TODO: currently we handle the decls for the RepeaterCreate empty template in the reify
  24067. // phase. We should handle that here instead.
  24068. }
  24069. }
  24070. }
  24071. }
  24072. /**
  24073. * Transforms special-case bindings with 'style' or 'class' in their names. Must run before the
  24074. * main binding specialization pass.
  24075. */
  24076. function specializeStyleBindings(job) {
  24077. for (const unit of job.units) {
  24078. for (const op of unit.update) {
  24079. if (op.kind !== OpKind.Binding) {
  24080. continue;
  24081. }
  24082. switch (op.bindingKind) {
  24083. case BindingKind.ClassName:
  24084. if (op.expression instanceof Interpolation) {
  24085. throw new Error(`Unexpected interpolation in ClassName binding`);
  24086. }
  24087. OpList.replace(op, createClassPropOp(op.target, op.name, op.expression, op.sourceSpan));
  24088. break;
  24089. case BindingKind.StyleProperty:
  24090. OpList.replace(op, createStylePropOp(op.target, op.name, op.expression, op.unit, op.sourceSpan));
  24091. break;
  24092. case BindingKind.Property:
  24093. case BindingKind.Template:
  24094. if (op.name === 'style') {
  24095. OpList.replace(op, createStyleMapOp(op.target, op.expression, op.sourceSpan));
  24096. }
  24097. else if (op.name === 'class') {
  24098. OpList.replace(op, createClassMapOp(op.target, op.expression, op.sourceSpan));
  24099. }
  24100. break;
  24101. }
  24102. }
  24103. }
  24104. }
  24105. /**
  24106. * Find all assignments and usages of temporary variables, which are linked to each other with cross
  24107. * references. Generate names for each cross-reference, and add a `DeclareVarStmt` to initialize
  24108. * them at the beginning of the update block.
  24109. *
  24110. * TODO: Sometimes, it will be possible to reuse names across different subexpressions. For example,
  24111. * in the double keyed read `a?.[f()]?.[f()]`, the two function calls have non-overlapping scopes.
  24112. * Implement an algorithm for reuse.
  24113. */
  24114. function generateTemporaryVariables(job) {
  24115. for (const unit of job.units) {
  24116. unit.create.prepend(generateTemporaries(unit.create));
  24117. unit.update.prepend(generateTemporaries(unit.update));
  24118. }
  24119. }
  24120. function generateTemporaries(ops) {
  24121. let opCount = 0;
  24122. let generatedStatements = [];
  24123. // For each op, search for any variables that are assigned or read. For each variable, generate a
  24124. // name and produce a `DeclareVarStmt` to the beginning of the block.
  24125. for (const op of ops) {
  24126. // Identify the final time each temp var is read.
  24127. const finalReads = new Map();
  24128. visitExpressionsInOp(op, (expr, flag) => {
  24129. if (flag & VisitorContextFlag.InChildOperation) {
  24130. return;
  24131. }
  24132. if (expr instanceof ReadTemporaryExpr) {
  24133. finalReads.set(expr.xref, expr);
  24134. }
  24135. });
  24136. // Name the temp vars, accounting for the fact that a name can be reused after it has been
  24137. // read for the final time.
  24138. let count = 0;
  24139. const assigned = new Set();
  24140. const released = new Set();
  24141. const defs = new Map();
  24142. visitExpressionsInOp(op, (expr, flag) => {
  24143. if (flag & VisitorContextFlag.InChildOperation) {
  24144. return;
  24145. }
  24146. if (expr instanceof AssignTemporaryExpr) {
  24147. if (!assigned.has(expr.xref)) {
  24148. assigned.add(expr.xref);
  24149. // TODO: Exactly replicate the naming scheme used by `TemplateDefinitionBuilder`.
  24150. // It seems to rely on an expression index instead of an op index.
  24151. defs.set(expr.xref, `tmp_${opCount}_${count++}`);
  24152. }
  24153. assignName(defs, expr);
  24154. }
  24155. else if (expr instanceof ReadTemporaryExpr) {
  24156. if (finalReads.get(expr.xref) === expr) {
  24157. released.add(expr.xref);
  24158. count--;
  24159. }
  24160. assignName(defs, expr);
  24161. }
  24162. });
  24163. // Add declarations for the temp vars.
  24164. generatedStatements.push(...Array.from(new Set(defs.values())).map((name) => createStatementOp(new DeclareVarStmt(name))));
  24165. opCount++;
  24166. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  24167. op.handlerOps.prepend(generateTemporaries(op.handlerOps));
  24168. }
  24169. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  24170. op.trackByOps.prepend(generateTemporaries(op.trackByOps));
  24171. }
  24172. }
  24173. return generatedStatements;
  24174. }
  24175. /**
  24176. * Assigns a name to the temporary variable in the given temporary variable expression.
  24177. */
  24178. function assignName(names, expr) {
  24179. const name = names.get(expr.xref);
  24180. if (name === undefined) {
  24181. throw new Error(`Found xref with unassigned name: ${expr.xref}`);
  24182. }
  24183. expr.name = name;
  24184. }
  24185. /**
  24186. * `track` functions in `for` repeaters can sometimes be "optimized," i.e. transformed into inline
  24187. * expressions, in lieu of an external function call. For example, tracking by `$index` can be be
  24188. * optimized into an inline `trackByIndex` reference. This phase checks track expressions for
  24189. * optimizable cases.
  24190. */
  24191. function optimizeTrackFns(job) {
  24192. for (const unit of job.units) {
  24193. for (const op of unit.create) {
  24194. if (op.kind !== OpKind.RepeaterCreate) {
  24195. continue;
  24196. }
  24197. if (op.track instanceof ReadVarExpr && op.track.name === '$index') {
  24198. // Top-level access of `$index` uses the built in `repeaterTrackByIndex`.
  24199. op.trackByFn = importExpr(Identifiers.repeaterTrackByIndex);
  24200. }
  24201. else if (op.track instanceof ReadVarExpr && op.track.name === '$item') {
  24202. // Top-level access of the item uses the built in `repeaterTrackByIdentity`.
  24203. op.trackByFn = importExpr(Identifiers.repeaterTrackByIdentity);
  24204. }
  24205. else if (isTrackByFunctionCall(job.root.xref, op.track)) {
  24206. // Mark the function as using the component instance to play it safe
  24207. // since the method might be using `this` internally (see #53628).
  24208. op.usesComponentInstance = true;
  24209. // Top-level method calls in the form of `fn($index, item)` can be passed in directly.
  24210. if (op.track.receiver.receiver.view === unit.xref) {
  24211. // TODO: this may be wrong
  24212. op.trackByFn = op.track.receiver;
  24213. }
  24214. else {
  24215. // This is a plain method call, but not in the component's root view.
  24216. // We need to get the component instance, and then call the method on it.
  24217. op.trackByFn = importExpr(Identifiers.componentInstance)
  24218. .callFn([])
  24219. .prop(op.track.receiver.name);
  24220. // Because the context is not avaiable (without a special function), we don't want to
  24221. // try to resolve it later. Let's get rid of it by overwriting the original track
  24222. // expression (which won't be used anyway).
  24223. op.track = op.trackByFn;
  24224. }
  24225. }
  24226. else {
  24227. // The track function could not be optimized.
  24228. // Replace context reads with a special IR expression, since context reads in a track
  24229. // function are emitted specially.
  24230. op.track = transformExpressionsInExpression(op.track, (expr) => {
  24231. if (expr instanceof PipeBindingExpr || expr instanceof PipeBindingVariadicExpr) {
  24232. throw new Error(`Illegal State: Pipes are not allowed in this context`);
  24233. }
  24234. else if (expr instanceof ContextExpr) {
  24235. op.usesComponentInstance = true;
  24236. return new TrackContextExpr(expr.view);
  24237. }
  24238. return expr;
  24239. }, VisitorContextFlag.None);
  24240. // Also create an OpList for the tracking expression since it may need
  24241. // additional ops when generating the final code (e.g. temporary variables).
  24242. const trackOpList = new OpList();
  24243. trackOpList.push(createStatementOp(new ReturnStatement(op.track, op.track.sourceSpan)));
  24244. op.trackByOps = trackOpList;
  24245. }
  24246. }
  24247. }
  24248. }
  24249. function isTrackByFunctionCall(rootView, expr) {
  24250. if (!(expr instanceof InvokeFunctionExpr) || expr.args.length === 0 || expr.args.length > 2) {
  24251. return false;
  24252. }
  24253. if (!(expr.receiver instanceof ReadPropExpr && expr.receiver.receiver instanceof ContextExpr) ||
  24254. expr.receiver.receiver.view !== rootView) {
  24255. return false;
  24256. }
  24257. const [arg0, arg1] = expr.args;
  24258. if (!(arg0 instanceof ReadVarExpr) || arg0.name !== '$index') {
  24259. return false;
  24260. }
  24261. else if (expr.args.length === 1) {
  24262. return true;
  24263. }
  24264. if (!(arg1 instanceof ReadVarExpr) || arg1.name !== '$item') {
  24265. return false;
  24266. }
  24267. return true;
  24268. }
  24269. /**
  24270. * Inside the `track` expression on a `for` repeater, the `$index` and `$item` variables are
  24271. * ambiently available. In this phase, we find those variable usages, and replace them with the
  24272. * appropriate output read.
  24273. */
  24274. function generateTrackVariables(job) {
  24275. for (const unit of job.units) {
  24276. for (const op of unit.create) {
  24277. if (op.kind !== OpKind.RepeaterCreate) {
  24278. continue;
  24279. }
  24280. op.track = transformExpressionsInExpression(op.track, (expr) => {
  24281. if (expr instanceof LexicalReadExpr) {
  24282. if (op.varNames.$index.has(expr.name)) {
  24283. return variable('$index');
  24284. }
  24285. else if (expr.name === op.varNames.$implicit) {
  24286. return variable('$item');
  24287. }
  24288. // TODO: handle prohibited context variables (emit as globals?)
  24289. }
  24290. return expr;
  24291. }, VisitorContextFlag.None);
  24292. }
  24293. }
  24294. }
  24295. /**
  24296. * Counts the number of variable slots used within each view, and stores that on the view itself, as
  24297. * well as propagates it to the `ir.TemplateOp` for embedded views.
  24298. */
  24299. function countVariables(job) {
  24300. // First, count the vars used in each view, and update the view-level counter.
  24301. for (const unit of job.units) {
  24302. let varCount = 0;
  24303. // Count variables on top-level ops first. Don't explore nested expressions just yet.
  24304. for (const op of unit.ops()) {
  24305. if (hasConsumesVarsTrait(op)) {
  24306. varCount += varsUsedByOp(op);
  24307. }
  24308. }
  24309. // Count variables on expressions inside ops. We do this later because some of these expressions
  24310. // might be conditional (e.g. `pipeBinding` inside of a ternary), and we don't want to interfere
  24311. // with indices for top-level binding slots (e.g. `property`).
  24312. for (const op of unit.ops()) {
  24313. visitExpressionsInOp(op, (expr) => {
  24314. if (!isIrExpression(expr)) {
  24315. return;
  24316. }
  24317. // TemplateDefinitionBuilder assigns variable offsets for everything but pure functions
  24318. // first, and then assigns offsets to pure functions lazily. We emulate that behavior by
  24319. // assigning offsets in two passes instead of one, only in compatibility mode.
  24320. if (job.compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  24321. expr instanceof PureFunctionExpr) {
  24322. return;
  24323. }
  24324. // Some expressions require knowledge of the number of variable slots consumed.
  24325. if (hasUsesVarOffsetTrait(expr)) {
  24326. expr.varOffset = varCount;
  24327. }
  24328. if (hasConsumesVarsTrait(expr)) {
  24329. varCount += varsUsedByIrExpression(expr);
  24330. }
  24331. });
  24332. }
  24333. // Compatibility mode pass for pure function offsets (as explained above).
  24334. if (job.compatibility === CompatibilityMode.TemplateDefinitionBuilder) {
  24335. for (const op of unit.ops()) {
  24336. visitExpressionsInOp(op, (expr) => {
  24337. if (!isIrExpression(expr) || !(expr instanceof PureFunctionExpr)) {
  24338. return;
  24339. }
  24340. // Some expressions require knowledge of the number of variable slots consumed.
  24341. if (hasUsesVarOffsetTrait(expr)) {
  24342. expr.varOffset = varCount;
  24343. }
  24344. if (hasConsumesVarsTrait(expr)) {
  24345. varCount += varsUsedByIrExpression(expr);
  24346. }
  24347. });
  24348. }
  24349. }
  24350. unit.vars = varCount;
  24351. }
  24352. if (job instanceof ComponentCompilationJob) {
  24353. // Add var counts for each view to the `ir.TemplateOp` which declares that view (if the view is
  24354. // an embedded view).
  24355. for (const unit of job.units) {
  24356. for (const op of unit.create) {
  24357. if (op.kind !== OpKind.Template && op.kind !== OpKind.RepeaterCreate) {
  24358. continue;
  24359. }
  24360. const childView = job.views.get(op.xref);
  24361. op.vars = childView.vars;
  24362. // TODO: currently we handle the vars for the RepeaterCreate empty template in the reify
  24363. // phase. We should handle that here instead.
  24364. }
  24365. }
  24366. }
  24367. }
  24368. /**
  24369. * Different operations that implement `ir.UsesVarsTrait` use different numbers of variables, so
  24370. * count the variables used by any particular `op`.
  24371. */
  24372. function varsUsedByOp(op) {
  24373. let slots;
  24374. switch (op.kind) {
  24375. case OpKind.Property:
  24376. case OpKind.HostProperty:
  24377. case OpKind.Attribute:
  24378. // All of these bindings use 1 variable slot, plus 1 slot for every interpolated expression,
  24379. // if any.
  24380. slots = 1;
  24381. if (op.expression instanceof Interpolation && !isSingletonInterpolation(op.expression)) {
  24382. slots += op.expression.expressions.length;
  24383. }
  24384. return slots;
  24385. case OpKind.TwoWayProperty:
  24386. // Two-way properties can only have expressions so they only need one variable slot.
  24387. return 1;
  24388. case OpKind.StyleProp:
  24389. case OpKind.ClassProp:
  24390. case OpKind.StyleMap:
  24391. case OpKind.ClassMap:
  24392. // Style & class bindings use 2 variable slots, plus 1 slot for every interpolated expression,
  24393. // if any.
  24394. slots = 2;
  24395. if (op.expression instanceof Interpolation) {
  24396. slots += op.expression.expressions.length;
  24397. }
  24398. return slots;
  24399. case OpKind.InterpolateText:
  24400. // `ir.InterpolateTextOp`s use a variable slot for each dynamic expression.
  24401. return op.interpolation.expressions.length;
  24402. case OpKind.I18nExpression:
  24403. case OpKind.Conditional:
  24404. case OpKind.DeferWhen:
  24405. case OpKind.StoreLet:
  24406. return 1;
  24407. case OpKind.RepeaterCreate:
  24408. // Repeaters may require an extra variable binding slot, if they have an empty view, for the
  24409. // empty block tracking.
  24410. // TODO: It's a bit odd to have a create mode instruction consume variable slots. Maybe we can
  24411. // find a way to use the Repeater update op instead.
  24412. return op.emptyView ? 1 : 0;
  24413. default:
  24414. throw new Error(`Unhandled op: ${OpKind[op.kind]}`);
  24415. }
  24416. }
  24417. function varsUsedByIrExpression(expr) {
  24418. switch (expr.kind) {
  24419. case ExpressionKind.PureFunctionExpr:
  24420. return 1 + expr.args.length;
  24421. case ExpressionKind.PipeBinding:
  24422. return 1 + expr.args.length;
  24423. case ExpressionKind.PipeBindingVariadic:
  24424. return 1 + expr.numArgs;
  24425. case ExpressionKind.StoreLet:
  24426. return 1;
  24427. default:
  24428. throw new Error(`AssertionError: unhandled ConsumesVarsTrait expression ${expr.constructor.name}`);
  24429. }
  24430. }
  24431. function isSingletonInterpolation(expr) {
  24432. if (expr.expressions.length !== 1 || expr.strings.length !== 2) {
  24433. return false;
  24434. }
  24435. if (expr.strings[0] !== '' || expr.strings[1] !== '') {
  24436. return false;
  24437. }
  24438. return true;
  24439. }
  24440. /**
  24441. * Optimize variables declared and used in the IR.
  24442. *
  24443. * Variables are eagerly generated by pipeline stages for all possible values that could be
  24444. * referenced. This stage processes the list of declared variables and all variable usages,
  24445. * and optimizes where possible. It performs 3 main optimizations:
  24446. *
  24447. * * It transforms variable declarations to side effectful expressions when the
  24448. * variable is not used, but its initializer has global effects which other
  24449. * operations rely upon.
  24450. * * It removes variable declarations if those variables are not referenced and
  24451. * either they do not have global effects, or nothing relies on them.
  24452. * * It inlines variable declarations when those variables are only used once
  24453. * and the inlining is semantically safe.
  24454. *
  24455. * To guarantee correctness, analysis of "fences" in the instruction lists is used to determine
  24456. * which optimizations are safe to perform.
  24457. */
  24458. function optimizeVariables(job) {
  24459. for (const unit of job.units) {
  24460. inlineAlwaysInlineVariables(unit.create);
  24461. inlineAlwaysInlineVariables(unit.update);
  24462. for (const op of unit.create) {
  24463. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  24464. inlineAlwaysInlineVariables(op.handlerOps);
  24465. }
  24466. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  24467. inlineAlwaysInlineVariables(op.trackByOps);
  24468. }
  24469. }
  24470. optimizeVariablesInOpList(unit.create, job.compatibility);
  24471. optimizeVariablesInOpList(unit.update, job.compatibility);
  24472. for (const op of unit.create) {
  24473. if (op.kind === OpKind.Listener || op.kind === OpKind.TwoWayListener) {
  24474. optimizeVariablesInOpList(op.handlerOps, job.compatibility);
  24475. }
  24476. else if (op.kind === OpKind.RepeaterCreate && op.trackByOps !== null) {
  24477. optimizeVariablesInOpList(op.trackByOps, job.compatibility);
  24478. }
  24479. }
  24480. }
  24481. }
  24482. /**
  24483. * A [fence](https://en.wikipedia.org/wiki/Memory_barrier) flag for an expression which indicates
  24484. * how that expression can be optimized in relation to other expressions or instructions.
  24485. *
  24486. * `Fence`s are a bitfield, so multiple flags may be set on a single expression.
  24487. */
  24488. var Fence;
  24489. (function (Fence) {
  24490. /**
  24491. * Empty flag (no fence exists).
  24492. */
  24493. Fence[Fence["None"] = 0] = "None";
  24494. /**
  24495. * A context read fence, meaning that the expression in question reads from the "current view"
  24496. * context of the runtime.
  24497. */
  24498. Fence[Fence["ViewContextRead"] = 1] = "ViewContextRead";
  24499. /**
  24500. * A context write fence, meaning that the expression in question writes to the "current view"
  24501. * context of the runtime.
  24502. *
  24503. * Note that all `ContextWrite` fences are implicitly `ContextRead` fences as operations which
  24504. * change the view context do so based on the current one.
  24505. */
  24506. Fence[Fence["ViewContextWrite"] = 2] = "ViewContextWrite";
  24507. /**
  24508. * Indicates that a call is required for its side-effects, even if nothing reads its result.
  24509. *
  24510. * This is also true of `ViewContextWrite` operations **if** they are followed by a
  24511. * `ViewContextRead`.
  24512. */
  24513. Fence[Fence["SideEffectful"] = 4] = "SideEffectful";
  24514. })(Fence || (Fence = {}));
  24515. function inlineAlwaysInlineVariables(ops) {
  24516. const vars = new Map();
  24517. for (const op of ops) {
  24518. if (op.kind === OpKind.Variable && op.flags & VariableFlags.AlwaysInline) {
  24519. visitExpressionsInOp(op, (expr) => {
  24520. if (isIrExpression(expr) && fencesForIrExpression(expr) !== Fence.None) {
  24521. throw new Error(`AssertionError: A context-sensitive variable was marked AlwaysInline`);
  24522. }
  24523. });
  24524. vars.set(op.xref, op);
  24525. }
  24526. transformExpressionsInOp(op, (expr) => {
  24527. if (expr instanceof ReadVariableExpr && vars.has(expr.xref)) {
  24528. const varOp = vars.get(expr.xref);
  24529. // Inline by cloning, because we might inline into multiple places.
  24530. return varOp.initializer.clone();
  24531. }
  24532. return expr;
  24533. }, VisitorContextFlag.None);
  24534. }
  24535. for (const op of vars.values()) {
  24536. OpList.remove(op);
  24537. }
  24538. }
  24539. /**
  24540. * Process a list of operations and optimize variables within that list.
  24541. */
  24542. function optimizeVariablesInOpList(ops, compatibility) {
  24543. const varDecls = new Map();
  24544. const varUsages = new Map();
  24545. // Track variables that are used outside of the immediate operation list. For example, within
  24546. // `ListenerOp` handler operations of listeners in the current operation list.
  24547. const varRemoteUsages = new Set();
  24548. const opMap = new Map();
  24549. // First, extract information about variables declared or used within the whole list.
  24550. for (const op of ops) {
  24551. if (op.kind === OpKind.Variable) {
  24552. if (varDecls.has(op.xref) || varUsages.has(op.xref)) {
  24553. throw new Error(`Should not see two declarations of the same variable: ${op.xref}`);
  24554. }
  24555. varDecls.set(op.xref, op);
  24556. varUsages.set(op.xref, 0);
  24557. }
  24558. opMap.set(op, collectOpInfo(op));
  24559. countVariableUsages(op, varUsages, varRemoteUsages);
  24560. }
  24561. // The next step is to remove any variable declarations for variables that aren't used. The
  24562. // variable initializer expressions may be side-effectful, so they may need to be retained as
  24563. // expression statements.
  24564. // Track whether we've seen an operation which reads from the view context yet. This is used to
  24565. // determine whether a write to the view context in a variable initializer can be observed.
  24566. let contextIsUsed = false;
  24567. // Note that iteration through the list happens in reverse, which guarantees that we'll process
  24568. // all reads of a variable prior to processing its declaration.
  24569. for (const op of ops.reversed()) {
  24570. const opInfo = opMap.get(op);
  24571. if (op.kind === OpKind.Variable && varUsages.get(op.xref) === 0) {
  24572. // This variable is unused and can be removed. We might need to keep the initializer around,
  24573. // though, if something depends on it running.
  24574. if ((contextIsUsed && opInfo.fences & Fence.ViewContextWrite) ||
  24575. opInfo.fences & Fence.SideEffectful) {
  24576. // This variable initializer has a side effect which must be retained. Either:
  24577. // * it writes to the view context, and we know there is a future operation which depends
  24578. // on that write, or
  24579. // * it's an operation which is inherently side-effectful.
  24580. // We can't remove the initializer, but we can remove the variable declaration itself and
  24581. // replace it with a side-effectful statement.
  24582. const stmtOp = createStatementOp(op.initializer.toStmt());
  24583. opMap.set(stmtOp, opInfo);
  24584. OpList.replace(op, stmtOp);
  24585. }
  24586. else {
  24587. // It's safe to delete this entire variable declaration as nothing depends on it, even
  24588. // side-effectfully. Note that doing this might make other variables unused. Since we're
  24589. // iterating in reverse order, we should always be processing usages before declarations
  24590. // and therefore by the time we get to a declaration, all removable usages will have been
  24591. // removed.
  24592. uncountVariableUsages(op, varUsages);
  24593. OpList.remove(op);
  24594. }
  24595. opMap.delete(op);
  24596. varDecls.delete(op.xref);
  24597. varUsages.delete(op.xref);
  24598. continue;
  24599. }
  24600. // Does this operation depend on the view context?
  24601. if (opInfo.fences & Fence.ViewContextRead) {
  24602. contextIsUsed = true;
  24603. }
  24604. }
  24605. // Next, inline any remaining variables with exactly one usage.
  24606. const toInline = [];
  24607. for (const [id, count] of varUsages) {
  24608. const decl = varDecls.get(id);
  24609. // We can inline variables that:
  24610. // - are used exactly once, and
  24611. // - are not used remotely
  24612. // OR
  24613. // - are marked for always inlining
  24614. const isAlwaysInline = !!(decl.flags & VariableFlags.AlwaysInline);
  24615. if (count !== 1 || isAlwaysInline) {
  24616. // We can't inline this variable as it's used more than once.
  24617. continue;
  24618. }
  24619. if (varRemoteUsages.has(id)) {
  24620. // This variable is used once, but across an operation boundary, so it can't be inlined.
  24621. continue;
  24622. }
  24623. toInline.push(id);
  24624. }
  24625. let candidate;
  24626. while ((candidate = toInline.pop())) {
  24627. // We will attempt to inline this variable. If inlining fails (due to fences for example),
  24628. // no future operation will make inlining legal.
  24629. const decl = varDecls.get(candidate);
  24630. const varInfo = opMap.get(decl);
  24631. const isAlwaysInline = !!(decl.flags & VariableFlags.AlwaysInline);
  24632. if (isAlwaysInline) {
  24633. throw new Error(`AssertionError: Found an 'AlwaysInline' variable after the always inlining pass.`);
  24634. }
  24635. // Scan operations following the variable declaration and look for the point where that variable
  24636. // is used. There should only be one usage given the precondition above.
  24637. for (let targetOp = decl.next; targetOp.kind !== OpKind.ListEnd; targetOp = targetOp.next) {
  24638. const opInfo = opMap.get(targetOp);
  24639. // Is the variable used in this operation?
  24640. if (opInfo.variablesUsed.has(candidate)) {
  24641. if (compatibility === CompatibilityMode.TemplateDefinitionBuilder &&
  24642. !allowConservativeInlining(decl, targetOp)) {
  24643. // We're in conservative mode, and this variable is not eligible for inlining into the
  24644. // target operation in this mode.
  24645. break;
  24646. }
  24647. // Yes, try to inline it. Inlining may not be successful if fences in this operation before
  24648. // the variable's usage cannot be safely crossed.
  24649. if (tryInlineVariableInitializer(candidate, decl.initializer, targetOp, varInfo.fences)) {
  24650. // Inlining was successful! Update the tracking structures to reflect the inlined
  24651. // variable.
  24652. opInfo.variablesUsed.delete(candidate);
  24653. // Add all variables used in the variable's initializer to its new usage site.
  24654. for (const id of varInfo.variablesUsed) {
  24655. opInfo.variablesUsed.add(id);
  24656. }
  24657. // Merge fences in the variable's initializer into its new usage site.
  24658. opInfo.fences |= varInfo.fences;
  24659. // Delete tracking info related to the declaration.
  24660. varDecls.delete(candidate);
  24661. varUsages.delete(candidate);
  24662. opMap.delete(decl);
  24663. // And finally, delete the original declaration from the operation list.
  24664. OpList.remove(decl);
  24665. }
  24666. // Whether inlining succeeded or failed, we're done processing this variable.
  24667. break;
  24668. }
  24669. // If the variable is not used in this operation, then we'd need to inline across it. Check if
  24670. // that's safe to do.
  24671. if (!safeToInlinePastFences(opInfo.fences, varInfo.fences)) {
  24672. // We can't safely inline this variable beyond this operation, so don't proceed with
  24673. // inlining this variable.
  24674. break;
  24675. }
  24676. }
  24677. }
  24678. }
  24679. /**
  24680. * Given an `ir.Expression`, returns the `Fence` flags for that expression type.
  24681. */
  24682. function fencesForIrExpression(expr) {
  24683. switch (expr.kind) {
  24684. case ExpressionKind.NextContext:
  24685. return Fence.ViewContextRead | Fence.ViewContextWrite;
  24686. case ExpressionKind.RestoreView:
  24687. return Fence.ViewContextRead | Fence.ViewContextWrite | Fence.SideEffectful;
  24688. case ExpressionKind.StoreLet:
  24689. return Fence.SideEffectful;
  24690. case ExpressionKind.Reference:
  24691. case ExpressionKind.ContextLetReference:
  24692. return Fence.ViewContextRead;
  24693. default:
  24694. return Fence.None;
  24695. }
  24696. }
  24697. /**
  24698. * Build the `OpInfo` structure for the given `op`. This performs two operations:
  24699. *
  24700. * * It tracks which variables are used in the operation's expressions.
  24701. * * It rolls up fence flags for expressions within the operation.
  24702. */
  24703. function collectOpInfo(op) {
  24704. let fences = Fence.None;
  24705. const variablesUsed = new Set();
  24706. visitExpressionsInOp(op, (expr) => {
  24707. if (!isIrExpression(expr)) {
  24708. return;
  24709. }
  24710. switch (expr.kind) {
  24711. case ExpressionKind.ReadVariable:
  24712. variablesUsed.add(expr.xref);
  24713. break;
  24714. default:
  24715. fences |= fencesForIrExpression(expr);
  24716. }
  24717. });
  24718. return { fences, variablesUsed };
  24719. }
  24720. /**
  24721. * Count the number of usages of each variable, being careful to track whether those usages are
  24722. * local or remote.
  24723. */
  24724. function countVariableUsages(op, varUsages, varRemoteUsage) {
  24725. visitExpressionsInOp(op, (expr, flags) => {
  24726. if (!isIrExpression(expr)) {
  24727. return;
  24728. }
  24729. if (expr.kind !== ExpressionKind.ReadVariable) {
  24730. return;
  24731. }
  24732. const count = varUsages.get(expr.xref);
  24733. if (count === undefined) {
  24734. // This variable is declared outside the current scope of optimization.
  24735. return;
  24736. }
  24737. varUsages.set(expr.xref, count + 1);
  24738. if (flags & VisitorContextFlag.InChildOperation) {
  24739. varRemoteUsage.add(expr.xref);
  24740. }
  24741. });
  24742. }
  24743. /**
  24744. * Remove usages of a variable in `op` from the `varUsages` tracking.
  24745. */
  24746. function uncountVariableUsages(op, varUsages) {
  24747. visitExpressionsInOp(op, (expr) => {
  24748. if (!isIrExpression(expr)) {
  24749. return;
  24750. }
  24751. if (expr.kind !== ExpressionKind.ReadVariable) {
  24752. return;
  24753. }
  24754. const count = varUsages.get(expr.xref);
  24755. if (count === undefined) {
  24756. // This variable is declared outside the current scope of optimization.
  24757. return;
  24758. }
  24759. else if (count === 0) {
  24760. throw new Error(`Inaccurate variable count: ${expr.xref} - found another read but count is already 0`);
  24761. }
  24762. varUsages.set(expr.xref, count - 1);
  24763. });
  24764. }
  24765. /**
  24766. * Checks whether it's safe to inline a variable across a particular operation.
  24767. *
  24768. * @param fences the fences of the operation which the inlining will cross
  24769. * @param declFences the fences of the variable being inlined.
  24770. */
  24771. function safeToInlinePastFences(fences, declFences) {
  24772. if (fences & Fence.ViewContextWrite) {
  24773. // It's not safe to inline context reads across context writes.
  24774. if (declFences & Fence.ViewContextRead) {
  24775. return false;
  24776. }
  24777. }
  24778. else if (fences & Fence.ViewContextRead) {
  24779. // It's not safe to inline context writes across context reads.
  24780. if (declFences & Fence.ViewContextWrite) {
  24781. return false;
  24782. }
  24783. }
  24784. return true;
  24785. }
  24786. /**
  24787. * Attempt to inline the initializer of a variable into a target operation's expressions.
  24788. *
  24789. * This may or may not be safe to do. For example, the variable could be read following the
  24790. * execution of an expression with fences that don't permit the variable to be inlined across them.
  24791. */
  24792. function tryInlineVariableInitializer(id, initializer, target, declFences) {
  24793. // We use `ir.transformExpressionsInOp` to walk the expressions and inline the variable if
  24794. // possible. Since this operation is callback-based, once inlining succeeds or fails we can't
  24795. // "stop" the expression processing, and have to keep track of whether inlining has succeeded or
  24796. // is no longer allowed.
  24797. let inlined = false;
  24798. let inliningAllowed = true;
  24799. transformExpressionsInOp(target, (expr, flags) => {
  24800. if (!isIrExpression(expr)) {
  24801. return expr;
  24802. }
  24803. if (inlined || !inliningAllowed) {
  24804. // Either the inlining has already succeeded, or we've passed a fence that disallows inlining
  24805. // at this point, so don't try.
  24806. return expr;
  24807. }
  24808. else if (flags & VisitorContextFlag.InChildOperation &&
  24809. declFences & Fence.ViewContextRead) {
  24810. // We cannot inline variables that are sensitive to the current context across operation
  24811. // boundaries.
  24812. return expr;
  24813. }
  24814. switch (expr.kind) {
  24815. case ExpressionKind.ReadVariable:
  24816. if (expr.xref === id) {
  24817. // This is the usage site of the variable. Since nothing has disallowed inlining, it's
  24818. // safe to inline the initializer here.
  24819. inlined = true;
  24820. return initializer;
  24821. }
  24822. break;
  24823. default:
  24824. // For other types of `ir.Expression`s, whether inlining is allowed depends on their fences.
  24825. const exprFences = fencesForIrExpression(expr);
  24826. inliningAllowed = inliningAllowed && safeToInlinePastFences(exprFences, declFences);
  24827. break;
  24828. }
  24829. return expr;
  24830. }, VisitorContextFlag.None);
  24831. return inlined;
  24832. }
  24833. /**
  24834. * Determines whether inlining of `decl` should be allowed in "conservative" mode.
  24835. *
  24836. * In conservative mode, inlining behavior is limited to those operations which the
  24837. * `TemplateDefinitionBuilder` supported, with the goal of producing equivalent output.
  24838. */
  24839. function allowConservativeInlining(decl, target) {
  24840. // TODO(alxhub): understand exactly how TemplateDefinitionBuilder approaches inlining, and record
  24841. // that behavior here.
  24842. switch (decl.variable.kind) {
  24843. case SemanticVariableKind.Identifier:
  24844. if (decl.initializer instanceof ReadVarExpr && decl.initializer.name === 'ctx') {
  24845. // Although TemplateDefinitionBuilder is cautious about inlining, we still want to do so
  24846. // when the variable is the context, to imitate its behavior with aliases in control flow
  24847. // blocks. This quirky behavior will become dead code once compatibility mode is no longer
  24848. // supported.
  24849. return true;
  24850. }
  24851. return false;
  24852. case SemanticVariableKind.Context:
  24853. // Context can only be inlined into other variables.
  24854. return target.kind === OpKind.Variable;
  24855. default:
  24856. return true;
  24857. }
  24858. }
  24859. /**
  24860. * Wraps ICUs that do not already belong to an i18n block in a new i18n block.
  24861. */
  24862. function wrapI18nIcus(job) {
  24863. for (const unit of job.units) {
  24864. let currentI18nOp = null;
  24865. let addedI18nId = null;
  24866. for (const op of unit.create) {
  24867. switch (op.kind) {
  24868. case OpKind.I18nStart:
  24869. currentI18nOp = op;
  24870. break;
  24871. case OpKind.I18nEnd:
  24872. currentI18nOp = null;
  24873. break;
  24874. case OpKind.IcuStart:
  24875. if (currentI18nOp === null) {
  24876. addedI18nId = job.allocateXrefId();
  24877. // ICU i18n start/end ops should not receive source spans.
  24878. OpList.insertBefore(createI18nStartOp(addedI18nId, op.message, undefined, null), op);
  24879. }
  24880. break;
  24881. case OpKind.IcuEnd:
  24882. if (addedI18nId !== null) {
  24883. OpList.insertAfter(createI18nEndOp(addedI18nId, null), op);
  24884. addedI18nId = null;
  24885. }
  24886. break;
  24887. }
  24888. }
  24889. }
  24890. }
  24891. /*!
  24892. * @license
  24893. * Copyright Google LLC All Rights Reserved.
  24894. *
  24895. * Use of this source code is governed by an MIT-style license that can be
  24896. * found in the LICENSE file at https://angular.dev/license
  24897. */
  24898. /**
  24899. * Removes any `storeLet` calls that aren't referenced outside of the current view.
  24900. */
  24901. function optimizeStoreLet(job) {
  24902. const letUsedExternally = new Set();
  24903. // Since `@let` declarations can be referenced in child views, both in
  24904. // the creation block (via listeners) and in the update block, we have
  24905. // to look through all the ops to find the references.
  24906. for (const unit of job.units) {
  24907. for (const op of unit.ops()) {
  24908. visitExpressionsInOp(op, (expr) => {
  24909. if (expr instanceof ContextLetReferenceExpr) {
  24910. letUsedExternally.add(expr.target);
  24911. }
  24912. });
  24913. }
  24914. }
  24915. // TODO(crisbeto): potentially remove the unused calls completely, pending discussion.
  24916. for (const unit of job.units) {
  24917. for (const op of unit.update) {
  24918. transformExpressionsInOp(op, (expression) => expression instanceof StoreLetExpr && !letUsedExternally.has(expression.target)
  24919. ? expression.value
  24920. : expression, VisitorContextFlag.None);
  24921. }
  24922. }
  24923. }
  24924. /**
  24925. * It's not allowed to access a `@let` declaration before it has been defined. This is enforced
  24926. * already via template type checking, however it can trip some of the assertions in the pipeline.
  24927. * E.g. the naming phase can fail because we resolved the variable here, but the variable doesn't
  24928. * exist anymore because the optimization phase removed it since it's invalid. To avoid surfacing
  24929. * confusing errors to users in the case where template type checking isn't running (e.g. in JIT
  24930. * mode) this phase detects illegal forward references and replaces them with `undefined`.
  24931. * Eventually users will see the proper error from the template type checker.
  24932. */
  24933. function removeIllegalLetReferences(job) {
  24934. for (const unit of job.units) {
  24935. for (const op of unit.update) {
  24936. if (op.kind !== OpKind.Variable ||
  24937. op.variable.kind !== SemanticVariableKind.Identifier ||
  24938. !(op.initializer instanceof StoreLetExpr)) {
  24939. continue;
  24940. }
  24941. const name = op.variable.identifier;
  24942. let current = op;
  24943. while (current && current.kind !== OpKind.ListEnd) {
  24944. transformExpressionsInOp(current, (expr) => expr instanceof LexicalReadExpr && expr.name === name ? literal$1(undefined) : expr, VisitorContextFlag.None);
  24945. current = current.prev;
  24946. }
  24947. }
  24948. }
  24949. }
  24950. /**
  24951. * Replaces the `storeLet` ops with variables that can be
  24952. * used to reference the value within the same view.
  24953. */
  24954. function generateLocalLetReferences(job) {
  24955. for (const unit of job.units) {
  24956. for (const op of unit.update) {
  24957. if (op.kind !== OpKind.StoreLet) {
  24958. continue;
  24959. }
  24960. const variable = {
  24961. kind: SemanticVariableKind.Identifier,
  24962. name: null,
  24963. identifier: op.declaredName,
  24964. local: true,
  24965. };
  24966. OpList.replace(op, createVariableOp(job.allocateXrefId(), variable, new StoreLetExpr(op.target, op.value, op.sourceSpan), VariableFlags.None));
  24967. }
  24968. }
  24969. }
  24970. /**
  24971. * Locates all of the elements defined in a creation block and outputs an op
  24972. * that will expose their definition location in the DOM.
  24973. */
  24974. function attachSourceLocations(job) {
  24975. if (!job.enableDebugLocations || job.relativeTemplatePath === null) {
  24976. return;
  24977. }
  24978. for (const unit of job.units) {
  24979. const locations = [];
  24980. for (const op of unit.create) {
  24981. if (op.kind === OpKind.ElementStart || op.kind === OpKind.Element) {
  24982. const start = op.startSourceSpan.start;
  24983. locations.push({
  24984. targetSlot: op.handle,
  24985. offset: start.offset,
  24986. line: start.line,
  24987. column: start.col,
  24988. });
  24989. }
  24990. }
  24991. if (locations.length > 0) {
  24992. unit.create.push(createSourceLocationOp(job.relativeTemplatePath, locations));
  24993. }
  24994. }
  24995. }
  24996. /**
  24997. *
  24998. * @license
  24999. * Copyright Google LLC All Rights Reserved.
  25000. *
  25001. * Use of this source code is governed by an MIT-style license that can be
  25002. * found in the LICENSE file at https://angular.dev/license
  25003. */
  25004. const phases = [
  25005. { kind: CompilationJobKind.Tmpl, fn: removeContentSelectors },
  25006. { kind: CompilationJobKind.Host, fn: parseHostStyleProperties },
  25007. { kind: CompilationJobKind.Tmpl, fn: emitNamespaceChanges },
  25008. { kind: CompilationJobKind.Tmpl, fn: propagateI18nBlocks },
  25009. { kind: CompilationJobKind.Tmpl, fn: wrapI18nIcus },
  25010. { kind: CompilationJobKind.Both, fn: deduplicateTextBindings },
  25011. { kind: CompilationJobKind.Both, fn: specializeStyleBindings },
  25012. { kind: CompilationJobKind.Both, fn: specializeBindings },
  25013. { kind: CompilationJobKind.Both, fn: extractAttributes },
  25014. { kind: CompilationJobKind.Tmpl, fn: createI18nContexts },
  25015. { kind: CompilationJobKind.Both, fn: parseExtractedStyles },
  25016. { kind: CompilationJobKind.Tmpl, fn: removeEmptyBindings },
  25017. { kind: CompilationJobKind.Both, fn: collapseSingletonInterpolations },
  25018. { kind: CompilationJobKind.Both, fn: orderOps },
  25019. { kind: CompilationJobKind.Tmpl, fn: generateConditionalExpressions },
  25020. { kind: CompilationJobKind.Tmpl, fn: createPipes },
  25021. { kind: CompilationJobKind.Tmpl, fn: configureDeferInstructions },
  25022. { kind: CompilationJobKind.Tmpl, fn: convertI18nText },
  25023. { kind: CompilationJobKind.Tmpl, fn: convertI18nBindings },
  25024. { kind: CompilationJobKind.Tmpl, fn: removeUnusedI18nAttributesOps },
  25025. { kind: CompilationJobKind.Tmpl, fn: assignI18nSlotDependencies },
  25026. { kind: CompilationJobKind.Tmpl, fn: applyI18nExpressions },
  25027. { kind: CompilationJobKind.Tmpl, fn: createVariadicPipes },
  25028. { kind: CompilationJobKind.Both, fn: generatePureLiteralStructures },
  25029. { kind: CompilationJobKind.Tmpl, fn: generateProjectionDefs },
  25030. { kind: CompilationJobKind.Tmpl, fn: generateLocalLetReferences },
  25031. { kind: CompilationJobKind.Tmpl, fn: generateVariables },
  25032. { kind: CompilationJobKind.Tmpl, fn: saveAndRestoreView },
  25033. { kind: CompilationJobKind.Both, fn: deleteAnyCasts },
  25034. { kind: CompilationJobKind.Both, fn: resolveDollarEvent },
  25035. { kind: CompilationJobKind.Tmpl, fn: generateTrackVariables },
  25036. { kind: CompilationJobKind.Tmpl, fn: removeIllegalLetReferences },
  25037. { kind: CompilationJobKind.Both, fn: resolveNames },
  25038. { kind: CompilationJobKind.Tmpl, fn: resolveDeferTargetNames },
  25039. { kind: CompilationJobKind.Tmpl, fn: transformTwoWayBindingSet },
  25040. { kind: CompilationJobKind.Tmpl, fn: optimizeTrackFns },
  25041. { kind: CompilationJobKind.Both, fn: resolveContexts },
  25042. { kind: CompilationJobKind.Both, fn: resolveSanitizers },
  25043. { kind: CompilationJobKind.Tmpl, fn: liftLocalRefs },
  25044. { kind: CompilationJobKind.Both, fn: generateNullishCoalesceExpressions },
  25045. { kind: CompilationJobKind.Both, fn: expandSafeReads },
  25046. { kind: CompilationJobKind.Both, fn: generateTemporaryVariables },
  25047. { kind: CompilationJobKind.Both, fn: optimizeVariables },
  25048. { kind: CompilationJobKind.Both, fn: optimizeStoreLet },
  25049. { kind: CompilationJobKind.Tmpl, fn: allocateSlots },
  25050. { kind: CompilationJobKind.Tmpl, fn: resolveI18nElementPlaceholders },
  25051. { kind: CompilationJobKind.Tmpl, fn: resolveI18nExpressionPlaceholders },
  25052. { kind: CompilationJobKind.Tmpl, fn: extractI18nMessages },
  25053. { kind: CompilationJobKind.Tmpl, fn: collectI18nConsts },
  25054. { kind: CompilationJobKind.Tmpl, fn: collectConstExpressions },
  25055. { kind: CompilationJobKind.Both, fn: collectElementConsts },
  25056. { kind: CompilationJobKind.Tmpl, fn: removeI18nContexts },
  25057. { kind: CompilationJobKind.Both, fn: countVariables },
  25058. { kind: CompilationJobKind.Tmpl, fn: generateAdvance },
  25059. { kind: CompilationJobKind.Both, fn: nameFunctionsAndVariables },
  25060. { kind: CompilationJobKind.Tmpl, fn: resolveDeferDepsFns },
  25061. { kind: CompilationJobKind.Tmpl, fn: mergeNextContextExpressions },
  25062. { kind: CompilationJobKind.Tmpl, fn: generateNgContainerOps },
  25063. { kind: CompilationJobKind.Tmpl, fn: collapseEmptyInstructions },
  25064. { kind: CompilationJobKind.Tmpl, fn: attachSourceLocations },
  25065. { kind: CompilationJobKind.Tmpl, fn: disableBindings$1 },
  25066. { kind: CompilationJobKind.Both, fn: extractPureFunctions },
  25067. { kind: CompilationJobKind.Both, fn: reify },
  25068. { kind: CompilationJobKind.Both, fn: chain },
  25069. ];
  25070. /**
  25071. * Run all transformation phases in the correct order against a compilation job. After this
  25072. * processing, the compilation should be in a state where it can be emitted.
  25073. */
  25074. function transform(job, kind) {
  25075. for (const phase of phases) {
  25076. if (phase.kind === kind || phase.kind === CompilationJobKind.Both) {
  25077. // The type of `Phase` above ensures it is impossible to call a phase that doesn't support the
  25078. // job kind.
  25079. phase.fn(job);
  25080. }
  25081. }
  25082. }
  25083. /**
  25084. * Compile all views in the given `ComponentCompilation` into the final template function, which may
  25085. * reference constants defined in a `ConstantPool`.
  25086. */
  25087. function emitTemplateFn(tpl, pool) {
  25088. const rootFn = emitView(tpl.root);
  25089. emitChildViews(tpl.root, pool);
  25090. return rootFn;
  25091. }
  25092. function emitChildViews(parent, pool) {
  25093. for (const unit of parent.job.units) {
  25094. if (unit.parent !== parent.xref) {
  25095. continue;
  25096. }
  25097. // Child views are emitted depth-first.
  25098. emitChildViews(unit, pool);
  25099. const viewFn = emitView(unit);
  25100. pool.statements.push(viewFn.toDeclStmt(viewFn.name));
  25101. }
  25102. }
  25103. /**
  25104. * Emit a template function for an individual `ViewCompilation` (which may be either the root view
  25105. * or an embedded view).
  25106. */
  25107. function emitView(view) {
  25108. if (view.fnName === null) {
  25109. throw new Error(`AssertionError: view ${view.xref} is unnamed`);
  25110. }
  25111. const createStatements = [];
  25112. for (const op of view.create) {
  25113. if (op.kind !== OpKind.Statement) {
  25114. throw new Error(`AssertionError: expected all create ops to have been compiled, but got ${OpKind[op.kind]}`);
  25115. }
  25116. createStatements.push(op.statement);
  25117. }
  25118. const updateStatements = [];
  25119. for (const op of view.update) {
  25120. if (op.kind !== OpKind.Statement) {
  25121. throw new Error(`AssertionError: expected all update ops to have been compiled, but got ${OpKind[op.kind]}`);
  25122. }
  25123. updateStatements.push(op.statement);
  25124. }
  25125. const createCond = maybeGenerateRfBlock(1, createStatements);
  25126. const updateCond = maybeGenerateRfBlock(2, updateStatements);
  25127. return fn([new FnParam('rf'), new FnParam('ctx')], [...createCond, ...updateCond],
  25128. /* type */ undefined,
  25129. /* sourceSpan */ undefined, view.fnName);
  25130. }
  25131. function maybeGenerateRfBlock(flag, statements) {
  25132. if (statements.length === 0) {
  25133. return [];
  25134. }
  25135. return [
  25136. ifStmt(new BinaryOperatorExpr(BinaryOperator.BitwiseAnd, variable('rf'), literal$1(flag)), statements),
  25137. ];
  25138. }
  25139. function emitHostBindingFunction(job) {
  25140. if (job.root.fnName === null) {
  25141. throw new Error(`AssertionError: host binding function is unnamed`);
  25142. }
  25143. const createStatements = [];
  25144. for (const op of job.root.create) {
  25145. if (op.kind !== OpKind.Statement) {
  25146. throw new Error(`AssertionError: expected all create ops to have been compiled, but got ${OpKind[op.kind]}`);
  25147. }
  25148. createStatements.push(op.statement);
  25149. }
  25150. const updateStatements = [];
  25151. for (const op of job.root.update) {
  25152. if (op.kind !== OpKind.Statement) {
  25153. throw new Error(`AssertionError: expected all update ops to have been compiled, but got ${OpKind[op.kind]}`);
  25154. }
  25155. updateStatements.push(op.statement);
  25156. }
  25157. if (createStatements.length === 0 && updateStatements.length === 0) {
  25158. return null;
  25159. }
  25160. const createCond = maybeGenerateRfBlock(1, createStatements);
  25161. const updateCond = maybeGenerateRfBlock(2, updateStatements);
  25162. return fn([new FnParam('rf'), new FnParam('ctx')], [...createCond, ...updateCond],
  25163. /* type */ undefined,
  25164. /* sourceSpan */ undefined, job.root.fnName);
  25165. }
  25166. const compatibilityMode = CompatibilityMode.TemplateDefinitionBuilder;
  25167. // Schema containing DOM elements and their properties.
  25168. const domSchema = new DomElementSchemaRegistry();
  25169. // Tag name of the `ng-template` element.
  25170. const NG_TEMPLATE_TAG_NAME = 'ng-template';
  25171. function isI18nRootNode(meta) {
  25172. return meta instanceof Message;
  25173. }
  25174. function isSingleI18nIcu(meta) {
  25175. return isI18nRootNode(meta) && meta.nodes.length === 1 && meta.nodes[0] instanceof Icu;
  25176. }
  25177. /**
  25178. * Process a template AST and convert it into a `ComponentCompilation` in the intermediate
  25179. * representation.
  25180. * TODO: Refactor more of the ingestion code into phases.
  25181. */
  25182. function ingestComponent(componentName, template, constantPool, relativeContextFilePath, i18nUseExternalIds, deferMeta, allDeferrableDepsFn, relativeTemplatePath, enableDebugLocations) {
  25183. const job = new ComponentCompilationJob(componentName, constantPool, compatibilityMode, relativeContextFilePath, i18nUseExternalIds, deferMeta, allDeferrableDepsFn, relativeTemplatePath, enableDebugLocations);
  25184. ingestNodes(job.root, template);
  25185. return job;
  25186. }
  25187. /**
  25188. * Process a host binding AST and convert it into a `HostBindingCompilationJob` in the intermediate
  25189. * representation.
  25190. */
  25191. function ingestHostBinding(input, bindingParser, constantPool) {
  25192. const job = new HostBindingCompilationJob(input.componentName, constantPool, compatibilityMode);
  25193. for (const property of input.properties ?? []) {
  25194. let bindingKind = BindingKind.Property;
  25195. // TODO: this should really be handled in the parser.
  25196. if (property.name.startsWith('attr.')) {
  25197. property.name = property.name.substring('attr.'.length);
  25198. bindingKind = BindingKind.Attribute;
  25199. }
  25200. if (property.isAnimation) {
  25201. bindingKind = BindingKind.Animation;
  25202. }
  25203. const securityContexts = bindingParser
  25204. .calcPossibleSecurityContexts(input.componentSelector, property.name, bindingKind === BindingKind.Attribute)
  25205. .filter((context) => context !== SecurityContext.NONE);
  25206. ingestHostProperty(job, property, bindingKind, securityContexts);
  25207. }
  25208. for (const [name, expr] of Object.entries(input.attributes) ?? []) {
  25209. const securityContexts = bindingParser
  25210. .calcPossibleSecurityContexts(input.componentSelector, name, true)
  25211. .filter((context) => context !== SecurityContext.NONE);
  25212. ingestHostAttribute(job, name, expr, securityContexts);
  25213. }
  25214. for (const event of input.events ?? []) {
  25215. ingestHostEvent(job, event);
  25216. }
  25217. return job;
  25218. }
  25219. // TODO: We should refactor the parser to use the same types and structures for host bindings as
  25220. // with ordinary components. This would allow us to share a lot more ingestion code.
  25221. function ingestHostProperty(job, property, bindingKind, securityContexts) {
  25222. let expression;
  25223. const ast = property.expression.ast;
  25224. if (ast instanceof Interpolation$1) {
  25225. expression = new Interpolation(ast.strings, ast.expressions.map((expr) => convertAst(expr, job, property.sourceSpan)), []);
  25226. }
  25227. else {
  25228. expression = convertAst(ast, job, property.sourceSpan);
  25229. }
  25230. job.root.update.push(createBindingOp(job.root.xref, bindingKind, property.name, expression, null, securityContexts, false, false, null,
  25231. /* TODO: How do Host bindings handle i18n attrs? */ null, property.sourceSpan));
  25232. }
  25233. function ingestHostAttribute(job, name, value, securityContexts) {
  25234. const attrBinding = createBindingOp(job.root.xref, BindingKind.Attribute, name, value, null, securityContexts,
  25235. /* Host attributes should always be extracted to const hostAttrs, even if they are not
  25236. *strictly* text literals */
  25237. true, false, null,
  25238. /* TODO */ null,
  25239. /** TODO: May be null? */ value.sourceSpan);
  25240. job.root.update.push(attrBinding);
  25241. }
  25242. function ingestHostEvent(job, event) {
  25243. const [phase, target] = event.type !== exports.ParsedEventType.Animation
  25244. ? [null, event.targetOrPhase]
  25245. : [event.targetOrPhase, null];
  25246. const eventBinding = createListenerOp(job.root.xref, new SlotHandle(), event.name, null, makeListenerHandlerOps(job.root, event.handler, event.handlerSpan), phase, target, true, event.sourceSpan);
  25247. job.root.create.push(eventBinding);
  25248. }
  25249. /**
  25250. * Ingest the nodes of a template AST into the given `ViewCompilation`.
  25251. */
  25252. function ingestNodes(unit, template) {
  25253. for (const node of template) {
  25254. if (node instanceof Element$1) {
  25255. ingestElement(unit, node);
  25256. }
  25257. else if (node instanceof Template) {
  25258. ingestTemplate(unit, node);
  25259. }
  25260. else if (node instanceof Content) {
  25261. ingestContent(unit, node);
  25262. }
  25263. else if (node instanceof Text$3) {
  25264. ingestText(unit, node, null);
  25265. }
  25266. else if (node instanceof BoundText) {
  25267. ingestBoundText(unit, node, null);
  25268. }
  25269. else if (node instanceof IfBlock) {
  25270. ingestIfBlock(unit, node);
  25271. }
  25272. else if (node instanceof SwitchBlock) {
  25273. ingestSwitchBlock(unit, node);
  25274. }
  25275. else if (node instanceof DeferredBlock) {
  25276. ingestDeferBlock(unit, node);
  25277. }
  25278. else if (node instanceof Icu$1) {
  25279. ingestIcu(unit, node);
  25280. }
  25281. else if (node instanceof ForLoopBlock) {
  25282. ingestForBlock(unit, node);
  25283. }
  25284. else if (node instanceof LetDeclaration$1) {
  25285. ingestLetDeclaration(unit, node);
  25286. }
  25287. else {
  25288. throw new Error(`Unsupported template node: ${node.constructor.name}`);
  25289. }
  25290. }
  25291. }
  25292. /**
  25293. * Ingest an element AST from the template into the given `ViewCompilation`.
  25294. */
  25295. function ingestElement(unit, element) {
  25296. if (element.i18n !== undefined &&
  25297. !(element.i18n instanceof Message || element.i18n instanceof TagPlaceholder)) {
  25298. throw Error(`Unhandled i18n metadata type for element: ${element.i18n.constructor.name}`);
  25299. }
  25300. const id = unit.job.allocateXrefId();
  25301. const [namespaceKey, elementName] = splitNsName(element.name);
  25302. const startOp = createElementStartOp(elementName, id, namespaceForKey(namespaceKey), element.i18n instanceof TagPlaceholder ? element.i18n : undefined, element.startSourceSpan, element.sourceSpan);
  25303. unit.create.push(startOp);
  25304. ingestElementBindings(unit, startOp, element);
  25305. ingestReferences(startOp, element);
  25306. // Start i18n, if needed, goes after the element create and bindings, but before the nodes
  25307. let i18nBlockId = null;
  25308. if (element.i18n instanceof Message) {
  25309. i18nBlockId = unit.job.allocateXrefId();
  25310. unit.create.push(createI18nStartOp(i18nBlockId, element.i18n, undefined, element.startSourceSpan));
  25311. }
  25312. ingestNodes(unit, element.children);
  25313. // The source span for the end op is typically the element closing tag. However, if no closing tag
  25314. // exists, such as in `<input>`, we use the start source span instead. Usually the start and end
  25315. // instructions will be collapsed into one `element` instruction, negating the purpose of this
  25316. // fallback, but in cases when it is not collapsed (such as an input with a binding), we still
  25317. // want to map the end instruction to the main element.
  25318. const endOp = createElementEndOp(id, element.endSourceSpan ?? element.startSourceSpan);
  25319. unit.create.push(endOp);
  25320. // If there is an i18n message associated with this element, insert i18n start and end ops.
  25321. if (i18nBlockId !== null) {
  25322. OpList.insertBefore(createI18nEndOp(i18nBlockId, element.endSourceSpan ?? element.startSourceSpan), endOp);
  25323. }
  25324. }
  25325. /**
  25326. * Ingest an `ng-template` node from the AST into the given `ViewCompilation`.
  25327. */
  25328. function ingestTemplate(unit, tmpl) {
  25329. if (tmpl.i18n !== undefined &&
  25330. !(tmpl.i18n instanceof Message || tmpl.i18n instanceof TagPlaceholder)) {
  25331. throw Error(`Unhandled i18n metadata type for template: ${tmpl.i18n.constructor.name}`);
  25332. }
  25333. const childView = unit.job.allocateView(unit.xref);
  25334. let tagNameWithoutNamespace = tmpl.tagName;
  25335. let namespacePrefix = '';
  25336. if (tmpl.tagName) {
  25337. [namespacePrefix, tagNameWithoutNamespace] = splitNsName(tmpl.tagName);
  25338. }
  25339. const i18nPlaceholder = tmpl.i18n instanceof TagPlaceholder ? tmpl.i18n : undefined;
  25340. const namespace = namespaceForKey(namespacePrefix);
  25341. const functionNameSuffix = tagNameWithoutNamespace === null ? '' : prefixWithNamespace(tagNameWithoutNamespace, namespace);
  25342. const templateKind = isPlainTemplate(tmpl)
  25343. ? TemplateKind.NgTemplate
  25344. : TemplateKind.Structural;
  25345. const templateOp = createTemplateOp(childView.xref, templateKind, tagNameWithoutNamespace, functionNameSuffix, namespace, i18nPlaceholder, tmpl.startSourceSpan, tmpl.sourceSpan);
  25346. unit.create.push(templateOp);
  25347. ingestTemplateBindings(unit, templateOp, tmpl, templateKind);
  25348. ingestReferences(templateOp, tmpl);
  25349. ingestNodes(childView, tmpl.children);
  25350. for (const { name, value } of tmpl.variables) {
  25351. childView.contextVariables.set(name, value !== '' ? value : '$implicit');
  25352. }
  25353. // If this is a plain template and there is an i18n message associated with it, insert i18n start
  25354. // and end ops. For structural directive templates, the i18n ops will be added when ingesting the
  25355. // element/template the directive is placed on.
  25356. if (templateKind === TemplateKind.NgTemplate && tmpl.i18n instanceof Message) {
  25357. const id = unit.job.allocateXrefId();
  25358. OpList.insertAfter(createI18nStartOp(id, tmpl.i18n, undefined, tmpl.startSourceSpan), childView.create.head);
  25359. OpList.insertBefore(createI18nEndOp(id, tmpl.endSourceSpan ?? tmpl.startSourceSpan), childView.create.tail);
  25360. }
  25361. }
  25362. /**
  25363. * Ingest a content node from the AST into the given `ViewCompilation`.
  25364. */
  25365. function ingestContent(unit, content) {
  25366. if (content.i18n !== undefined && !(content.i18n instanceof TagPlaceholder)) {
  25367. throw Error(`Unhandled i18n metadata type for element: ${content.i18n.constructor.name}`);
  25368. }
  25369. let fallbackView = null;
  25370. // Don't capture default content that's only made up of empty text nodes and comments.
  25371. // Note that we process the default content before the projection in order to match the
  25372. // insertion order at runtime.
  25373. if (content.children.some((child) => !(child instanceof Comment$1) &&
  25374. (!(child instanceof Text$3) || child.value.trim().length > 0))) {
  25375. fallbackView = unit.job.allocateView(unit.xref);
  25376. ingestNodes(fallbackView, content.children);
  25377. }
  25378. const id = unit.job.allocateXrefId();
  25379. const op = createProjectionOp(id, content.selector, content.i18n, fallbackView?.xref ?? null, content.sourceSpan);
  25380. for (const attr of content.attributes) {
  25381. const securityContext = domSchema.securityContext(content.name, attr.name, true);
  25382. unit.update.push(createBindingOp(op.xref, BindingKind.Attribute, attr.name, literal$1(attr.value), null, securityContext, true, false, null, asMessage(attr.i18n), attr.sourceSpan));
  25383. }
  25384. unit.create.push(op);
  25385. }
  25386. /**
  25387. * Ingest a literal text node from the AST into the given `ViewCompilation`.
  25388. */
  25389. function ingestText(unit, text, icuPlaceholder) {
  25390. unit.create.push(createTextOp(unit.job.allocateXrefId(), text.value, icuPlaceholder, text.sourceSpan));
  25391. }
  25392. /**
  25393. * Ingest an interpolated text node from the AST into the given `ViewCompilation`.
  25394. */
  25395. function ingestBoundText(unit, text, icuPlaceholder) {
  25396. let value = text.value;
  25397. if (value instanceof ASTWithSource) {
  25398. value = value.ast;
  25399. }
  25400. if (!(value instanceof Interpolation$1)) {
  25401. throw new Error(`AssertionError: expected Interpolation for BoundText node, got ${value.constructor.name}`);
  25402. }
  25403. if (text.i18n !== undefined && !(text.i18n instanceof Container)) {
  25404. throw Error(`Unhandled i18n metadata type for text interpolation: ${text.i18n?.constructor.name}`);
  25405. }
  25406. const i18nPlaceholders = text.i18n instanceof Container
  25407. ? text.i18n.children
  25408. .filter((node) => node instanceof Placeholder)
  25409. .map((placeholder) => placeholder.name)
  25410. : [];
  25411. if (i18nPlaceholders.length > 0 && i18nPlaceholders.length !== value.expressions.length) {
  25412. throw Error(`Unexpected number of i18n placeholders (${value.expressions.length}) for BoundText with ${value.expressions.length} expressions`);
  25413. }
  25414. const textXref = unit.job.allocateXrefId();
  25415. unit.create.push(createTextOp(textXref, '', icuPlaceholder, text.sourceSpan));
  25416. // TemplateDefinitionBuilder does not generate source maps for sub-expressions inside an
  25417. // interpolation. We copy that behavior in compatibility mode.
  25418. // TODO: is it actually correct to generate these extra maps in modern mode?
  25419. const baseSourceSpan = unit.job.compatibility ? null : text.sourceSpan;
  25420. unit.update.push(createInterpolateTextOp(textXref, new Interpolation(value.strings, value.expressions.map((expr) => convertAst(expr, unit.job, baseSourceSpan)), i18nPlaceholders), text.sourceSpan));
  25421. }
  25422. /**
  25423. * Ingest an `@if` block into the given `ViewCompilation`.
  25424. */
  25425. function ingestIfBlock(unit, ifBlock) {
  25426. let firstXref = null;
  25427. let conditions = [];
  25428. for (let i = 0; i < ifBlock.branches.length; i++) {
  25429. const ifCase = ifBlock.branches[i];
  25430. const cView = unit.job.allocateView(unit.xref);
  25431. const tagName = ingestControlFlowInsertionPoint(unit, cView.xref, ifCase);
  25432. if (ifCase.expressionAlias !== null) {
  25433. cView.contextVariables.set(ifCase.expressionAlias.name, CTX_REF);
  25434. }
  25435. let ifCaseI18nMeta = undefined;
  25436. if (ifCase.i18n !== undefined) {
  25437. if (!(ifCase.i18n instanceof BlockPlaceholder)) {
  25438. throw Error(`Unhandled i18n metadata type for if block: ${ifCase.i18n?.constructor.name}`);
  25439. }
  25440. ifCaseI18nMeta = ifCase.i18n;
  25441. }
  25442. const templateOp = createTemplateOp(cView.xref, TemplateKind.Block, tagName, 'Conditional', Namespace.HTML, ifCaseI18nMeta, ifCase.startSourceSpan, ifCase.sourceSpan);
  25443. unit.create.push(templateOp);
  25444. if (firstXref === null) {
  25445. firstXref = cView.xref;
  25446. }
  25447. const caseExpr = ifCase.expression ? convertAst(ifCase.expression, unit.job, null) : null;
  25448. const conditionalCaseExpr = new ConditionalCaseExpr(caseExpr, templateOp.xref, templateOp.handle, ifCase.expressionAlias);
  25449. conditions.push(conditionalCaseExpr);
  25450. ingestNodes(cView, ifCase.children);
  25451. }
  25452. unit.update.push(createConditionalOp(firstXref, null, conditions, ifBlock.sourceSpan));
  25453. }
  25454. /**
  25455. * Ingest an `@switch` block into the given `ViewCompilation`.
  25456. */
  25457. function ingestSwitchBlock(unit, switchBlock) {
  25458. // Don't ingest empty switches since they won't render anything.
  25459. if (switchBlock.cases.length === 0) {
  25460. return;
  25461. }
  25462. let firstXref = null;
  25463. let conditions = [];
  25464. for (const switchCase of switchBlock.cases) {
  25465. const cView = unit.job.allocateView(unit.xref);
  25466. const tagName = ingestControlFlowInsertionPoint(unit, cView.xref, switchCase);
  25467. let switchCaseI18nMeta = undefined;
  25468. if (switchCase.i18n !== undefined) {
  25469. if (!(switchCase.i18n instanceof BlockPlaceholder)) {
  25470. throw Error(`Unhandled i18n metadata type for switch block: ${switchCase.i18n?.constructor.name}`);
  25471. }
  25472. switchCaseI18nMeta = switchCase.i18n;
  25473. }
  25474. const templateOp = createTemplateOp(cView.xref, TemplateKind.Block, tagName, 'Case', Namespace.HTML, switchCaseI18nMeta, switchCase.startSourceSpan, switchCase.sourceSpan);
  25475. unit.create.push(templateOp);
  25476. if (firstXref === null) {
  25477. firstXref = cView.xref;
  25478. }
  25479. const caseExpr = switchCase.expression
  25480. ? convertAst(switchCase.expression, unit.job, switchBlock.startSourceSpan)
  25481. : null;
  25482. const conditionalCaseExpr = new ConditionalCaseExpr(caseExpr, templateOp.xref, templateOp.handle);
  25483. conditions.push(conditionalCaseExpr);
  25484. ingestNodes(cView, switchCase.children);
  25485. }
  25486. unit.update.push(createConditionalOp(firstXref, convertAst(switchBlock.expression, unit.job, null), conditions, switchBlock.sourceSpan));
  25487. }
  25488. function ingestDeferView(unit, suffix, i18nMeta, children, sourceSpan) {
  25489. if (i18nMeta !== undefined && !(i18nMeta instanceof BlockPlaceholder)) {
  25490. throw Error('Unhandled i18n metadata type for defer block');
  25491. }
  25492. if (children === undefined) {
  25493. return null;
  25494. }
  25495. const secondaryView = unit.job.allocateView(unit.xref);
  25496. ingestNodes(secondaryView, children);
  25497. const templateOp = createTemplateOp(secondaryView.xref, TemplateKind.Block, null, `Defer${suffix}`, Namespace.HTML, i18nMeta, sourceSpan, sourceSpan);
  25498. unit.create.push(templateOp);
  25499. return templateOp;
  25500. }
  25501. function ingestDeferBlock(unit, deferBlock) {
  25502. let ownResolverFn = null;
  25503. if (unit.job.deferMeta.mode === 0 /* DeferBlockDepsEmitMode.PerBlock */) {
  25504. if (!unit.job.deferMeta.blocks.has(deferBlock)) {
  25505. throw new Error(`AssertionError: unable to find a dependency function for this deferred block`);
  25506. }
  25507. ownResolverFn = unit.job.deferMeta.blocks.get(deferBlock) ?? null;
  25508. }
  25509. // Generate the defer main view and all secondary views.
  25510. const main = ingestDeferView(unit, '', deferBlock.i18n, deferBlock.children, deferBlock.sourceSpan);
  25511. const loading = ingestDeferView(unit, 'Loading', deferBlock.loading?.i18n, deferBlock.loading?.children, deferBlock.loading?.sourceSpan);
  25512. const placeholder = ingestDeferView(unit, 'Placeholder', deferBlock.placeholder?.i18n, deferBlock.placeholder?.children, deferBlock.placeholder?.sourceSpan);
  25513. const error = ingestDeferView(unit, 'Error', deferBlock.error?.i18n, deferBlock.error?.children, deferBlock.error?.sourceSpan);
  25514. // Create the main defer op, and ops for all secondary views.
  25515. const deferXref = unit.job.allocateXrefId();
  25516. const deferOp = createDeferOp(deferXref, main.xref, main.handle, ownResolverFn, unit.job.allDeferrableDepsFn, deferBlock.sourceSpan);
  25517. deferOp.placeholderView = placeholder?.xref ?? null;
  25518. deferOp.placeholderSlot = placeholder?.handle ?? null;
  25519. deferOp.loadingSlot = loading?.handle ?? null;
  25520. deferOp.errorSlot = error?.handle ?? null;
  25521. deferOp.placeholderMinimumTime = deferBlock.placeholder?.minimumTime ?? null;
  25522. deferOp.loadingMinimumTime = deferBlock.loading?.minimumTime ?? null;
  25523. deferOp.loadingAfterTime = deferBlock.loading?.afterTime ?? null;
  25524. deferOp.flags = calcDeferBlockFlags(deferBlock);
  25525. unit.create.push(deferOp);
  25526. // Configure all defer `on` conditions.
  25527. // TODO: refactor prefetch triggers to use a separate op type, with a shared superclass. This will
  25528. // make it easier to refactor prefetch behavior in the future.
  25529. const deferOnOps = [];
  25530. const deferWhenOps = [];
  25531. // Ingest the hydrate triggers first since they set up all the other triggers during SSR.
  25532. ingestDeferTriggers("hydrate" /* ir.DeferOpModifierKind.HYDRATE */, deferBlock.hydrateTriggers, deferOnOps, deferWhenOps, unit, deferXref);
  25533. ingestDeferTriggers("none" /* ir.DeferOpModifierKind.NONE */, deferBlock.triggers, deferOnOps, deferWhenOps, unit, deferXref);
  25534. ingestDeferTriggers("prefetch" /* ir.DeferOpModifierKind.PREFETCH */, deferBlock.prefetchTriggers, deferOnOps, deferWhenOps, unit, deferXref);
  25535. // If no (non-prefetching or hydrating) defer triggers were provided, default to `idle`.
  25536. const hasConcreteTrigger = deferOnOps.some((op) => op.modifier === "none" /* ir.DeferOpModifierKind.NONE */) ||
  25537. deferWhenOps.some((op) => op.modifier === "none" /* ir.DeferOpModifierKind.NONE */);
  25538. if (!hasConcreteTrigger) {
  25539. deferOnOps.push(createDeferOnOp(deferXref, { kind: DeferTriggerKind.Idle }, "none" /* ir.DeferOpModifierKind.NONE */, null));
  25540. }
  25541. unit.create.push(deferOnOps);
  25542. unit.update.push(deferWhenOps);
  25543. }
  25544. function calcDeferBlockFlags(deferBlockDetails) {
  25545. if (Object.keys(deferBlockDetails.hydrateTriggers).length > 0) {
  25546. return 1 /* ir.TDeferDetailsFlags.HasHydrateTriggers */;
  25547. }
  25548. return null;
  25549. }
  25550. function ingestDeferTriggers(modifier, triggers, onOps, whenOps, unit, deferXref) {
  25551. if (triggers.idle !== undefined) {
  25552. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Idle }, modifier, triggers.idle.sourceSpan);
  25553. onOps.push(deferOnOp);
  25554. }
  25555. if (triggers.immediate !== undefined) {
  25556. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Immediate }, modifier, triggers.immediate.sourceSpan);
  25557. onOps.push(deferOnOp);
  25558. }
  25559. if (triggers.timer !== undefined) {
  25560. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Timer, delay: triggers.timer.delay }, modifier, triggers.timer.sourceSpan);
  25561. onOps.push(deferOnOp);
  25562. }
  25563. if (triggers.hover !== undefined) {
  25564. const deferOnOp = createDeferOnOp(deferXref, {
  25565. kind: DeferTriggerKind.Hover,
  25566. targetName: triggers.hover.reference,
  25567. targetXref: null,
  25568. targetSlot: null,
  25569. targetView: null,
  25570. targetSlotViewSteps: null,
  25571. }, modifier, triggers.hover.sourceSpan);
  25572. onOps.push(deferOnOp);
  25573. }
  25574. if (triggers.interaction !== undefined) {
  25575. const deferOnOp = createDeferOnOp(deferXref, {
  25576. kind: DeferTriggerKind.Interaction,
  25577. targetName: triggers.interaction.reference,
  25578. targetXref: null,
  25579. targetSlot: null,
  25580. targetView: null,
  25581. targetSlotViewSteps: null,
  25582. }, modifier, triggers.interaction.sourceSpan);
  25583. onOps.push(deferOnOp);
  25584. }
  25585. if (triggers.viewport !== undefined) {
  25586. const deferOnOp = createDeferOnOp(deferXref, {
  25587. kind: DeferTriggerKind.Viewport,
  25588. targetName: triggers.viewport.reference,
  25589. targetXref: null,
  25590. targetSlot: null,
  25591. targetView: null,
  25592. targetSlotViewSteps: null,
  25593. }, modifier, triggers.viewport.sourceSpan);
  25594. onOps.push(deferOnOp);
  25595. }
  25596. if (triggers.never !== undefined) {
  25597. const deferOnOp = createDeferOnOp(deferXref, { kind: DeferTriggerKind.Never }, modifier, triggers.never.sourceSpan);
  25598. onOps.push(deferOnOp);
  25599. }
  25600. if (triggers.when !== undefined) {
  25601. if (triggers.when.value instanceof Interpolation$1) {
  25602. // TemplateDefinitionBuilder supports this case, but it's very strange to me. What would it
  25603. // even mean?
  25604. throw new Error(`Unexpected interpolation in defer block when trigger`);
  25605. }
  25606. const deferOnOp = createDeferWhenOp(deferXref, convertAst(triggers.when.value, unit.job, triggers.when.sourceSpan), modifier, triggers.when.sourceSpan);
  25607. whenOps.push(deferOnOp);
  25608. }
  25609. }
  25610. function ingestIcu(unit, icu) {
  25611. if (icu.i18n instanceof Message && isSingleI18nIcu(icu.i18n)) {
  25612. const xref = unit.job.allocateXrefId();
  25613. unit.create.push(createIcuStartOp(xref, icu.i18n, icuFromI18nMessage(icu.i18n).name, null));
  25614. for (const [placeholder, text] of Object.entries({ ...icu.vars, ...icu.placeholders })) {
  25615. if (text instanceof BoundText) {
  25616. ingestBoundText(unit, text, placeholder);
  25617. }
  25618. else {
  25619. ingestText(unit, text, placeholder);
  25620. }
  25621. }
  25622. unit.create.push(createIcuEndOp(xref));
  25623. }
  25624. else {
  25625. throw Error(`Unhandled i18n metadata type for ICU: ${icu.i18n?.constructor.name}`);
  25626. }
  25627. }
  25628. /**
  25629. * Ingest an `@for` block into the given `ViewCompilation`.
  25630. */
  25631. function ingestForBlock(unit, forBlock) {
  25632. const repeaterView = unit.job.allocateView(unit.xref);
  25633. // We copy TemplateDefinitionBuilder's scheme of creating names for `$count` and `$index`
  25634. // that are suffixed with special information, to disambiguate which level of nested loop
  25635. // the below aliases refer to.
  25636. // TODO: We should refactor Template Pipeline's variable phases to gracefully handle
  25637. // shadowing, and arbitrarily many levels of variables depending on each other.
  25638. const indexName = `ɵ$index_${repeaterView.xref}`;
  25639. const countName = `ɵ$count_${repeaterView.xref}`;
  25640. const indexVarNames = new Set();
  25641. // Set all the context variables and aliases available in the repeater.
  25642. repeaterView.contextVariables.set(forBlock.item.name, forBlock.item.value);
  25643. for (const variable of forBlock.contextVariables) {
  25644. if (variable.value === '$index') {
  25645. indexVarNames.add(variable.name);
  25646. }
  25647. if (variable.name === '$index') {
  25648. repeaterView.contextVariables.set('$index', variable.value).set(indexName, variable.value);
  25649. }
  25650. else if (variable.name === '$count') {
  25651. repeaterView.contextVariables.set('$count', variable.value).set(countName, variable.value);
  25652. }
  25653. else {
  25654. repeaterView.aliases.add({
  25655. kind: SemanticVariableKind.Alias,
  25656. name: null,
  25657. identifier: variable.name,
  25658. expression: getComputedForLoopVariableExpression(variable, indexName, countName),
  25659. });
  25660. }
  25661. }
  25662. const sourceSpan = convertSourceSpan(forBlock.trackBy.span, forBlock.sourceSpan);
  25663. const track = convertAst(forBlock.trackBy, unit.job, sourceSpan);
  25664. ingestNodes(repeaterView, forBlock.children);
  25665. let emptyView = null;
  25666. let emptyTagName = null;
  25667. if (forBlock.empty !== null) {
  25668. emptyView = unit.job.allocateView(unit.xref);
  25669. ingestNodes(emptyView, forBlock.empty.children);
  25670. emptyTagName = ingestControlFlowInsertionPoint(unit, emptyView.xref, forBlock.empty);
  25671. }
  25672. const varNames = {
  25673. $index: indexVarNames,
  25674. $implicit: forBlock.item.name,
  25675. };
  25676. if (forBlock.i18n !== undefined && !(forBlock.i18n instanceof BlockPlaceholder)) {
  25677. throw Error('AssertionError: Unhandled i18n metadata type or @for');
  25678. }
  25679. if (forBlock.empty?.i18n !== undefined &&
  25680. !(forBlock.empty.i18n instanceof BlockPlaceholder)) {
  25681. throw Error('AssertionError: Unhandled i18n metadata type or @empty');
  25682. }
  25683. const i18nPlaceholder = forBlock.i18n;
  25684. const emptyI18nPlaceholder = forBlock.empty?.i18n;
  25685. const tagName = ingestControlFlowInsertionPoint(unit, repeaterView.xref, forBlock);
  25686. const repeaterCreate = createRepeaterCreateOp(repeaterView.xref, emptyView?.xref ?? null, tagName, track, varNames, emptyTagName, i18nPlaceholder, emptyI18nPlaceholder, forBlock.startSourceSpan, forBlock.sourceSpan);
  25687. unit.create.push(repeaterCreate);
  25688. const expression = convertAst(forBlock.expression, unit.job, convertSourceSpan(forBlock.expression.span, forBlock.sourceSpan));
  25689. const repeater = createRepeaterOp(repeaterCreate.xref, repeaterCreate.handle, expression, forBlock.sourceSpan);
  25690. unit.update.push(repeater);
  25691. }
  25692. /**
  25693. * Gets an expression that represents a variable in an `@for` loop.
  25694. * @param variable AST representing the variable.
  25695. * @param indexName Loop-specific name for `$index`.
  25696. * @param countName Loop-specific name for `$count`.
  25697. */
  25698. function getComputedForLoopVariableExpression(variable, indexName, countName) {
  25699. switch (variable.value) {
  25700. case '$index':
  25701. return new LexicalReadExpr(indexName);
  25702. case '$count':
  25703. return new LexicalReadExpr(countName);
  25704. case '$first':
  25705. return new LexicalReadExpr(indexName).identical(literal$1(0));
  25706. case '$last':
  25707. return new LexicalReadExpr(indexName).identical(new LexicalReadExpr(countName).minus(literal$1(1)));
  25708. case '$even':
  25709. return new LexicalReadExpr(indexName).modulo(literal$1(2)).identical(literal$1(0));
  25710. case '$odd':
  25711. return new LexicalReadExpr(indexName).modulo(literal$1(2)).notIdentical(literal$1(0));
  25712. default:
  25713. throw new Error(`AssertionError: unknown @for loop variable ${variable.value}`);
  25714. }
  25715. }
  25716. function ingestLetDeclaration(unit, node) {
  25717. const target = unit.job.allocateXrefId();
  25718. unit.create.push(createDeclareLetOp(target, node.name, node.sourceSpan));
  25719. unit.update.push(createStoreLetOp(target, node.name, convertAst(node.value, unit.job, node.valueSpan), node.sourceSpan));
  25720. }
  25721. /**
  25722. * Convert a template AST expression into an output AST expression.
  25723. */
  25724. function convertAst(ast, job, baseSourceSpan) {
  25725. if (ast instanceof ASTWithSource) {
  25726. return convertAst(ast.ast, job, baseSourceSpan);
  25727. }
  25728. else if (ast instanceof PropertyRead) {
  25729. // Whether this is an implicit receiver, *excluding* explicit reads of `this`.
  25730. const isImplicitReceiver = ast.receiver instanceof ImplicitReceiver && !(ast.receiver instanceof ThisReceiver);
  25731. if (isImplicitReceiver) {
  25732. return new LexicalReadExpr(ast.name);
  25733. }
  25734. else {
  25735. return new ReadPropExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.name, null, convertSourceSpan(ast.span, baseSourceSpan));
  25736. }
  25737. }
  25738. else if (ast instanceof PropertyWrite) {
  25739. if (ast.receiver instanceof ImplicitReceiver) {
  25740. return new WritePropExpr(
  25741. // TODO: Is it correct to always use the root context in place of the implicit receiver?
  25742. new ContextExpr(job.root.xref), ast.name, convertAst(ast.value, job, baseSourceSpan), null, convertSourceSpan(ast.span, baseSourceSpan));
  25743. }
  25744. return new WritePropExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.name, convertAst(ast.value, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25745. }
  25746. else if (ast instanceof KeyedWrite) {
  25747. return new WriteKeyExpr(convertAst(ast.receiver, job, baseSourceSpan), convertAst(ast.key, job, baseSourceSpan), convertAst(ast.value, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25748. }
  25749. else if (ast instanceof Call) {
  25750. if (ast.receiver instanceof ImplicitReceiver) {
  25751. throw new Error(`Unexpected ImplicitReceiver`);
  25752. }
  25753. else {
  25754. return new InvokeFunctionExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25755. }
  25756. }
  25757. else if (ast instanceof LiteralPrimitive) {
  25758. return literal$1(ast.value, undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25759. }
  25760. else if (ast instanceof Unary) {
  25761. switch (ast.operator) {
  25762. case '+':
  25763. return new UnaryOperatorExpr(UnaryOperator.Plus, convertAst(ast.expr, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25764. case '-':
  25765. return new UnaryOperatorExpr(UnaryOperator.Minus, convertAst(ast.expr, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25766. default:
  25767. throw new Error(`AssertionError: unknown unary operator ${ast.operator}`);
  25768. }
  25769. }
  25770. else if (ast instanceof Binary) {
  25771. const operator = BINARY_OPERATORS$3.get(ast.operation);
  25772. if (operator === undefined) {
  25773. throw new Error(`AssertionError: unknown binary operator ${ast.operation}`);
  25774. }
  25775. return new BinaryOperatorExpr(operator, convertAst(ast.left, job, baseSourceSpan), convertAst(ast.right, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25776. }
  25777. else if (ast instanceof ThisReceiver) {
  25778. // TODO: should context expressions have source maps?
  25779. return new ContextExpr(job.root.xref);
  25780. }
  25781. else if (ast instanceof KeyedRead) {
  25782. return new ReadKeyExpr(convertAst(ast.receiver, job, baseSourceSpan), convertAst(ast.key, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25783. }
  25784. else if (ast instanceof Chain) {
  25785. throw new Error(`AssertionError: Chain in unknown context`);
  25786. }
  25787. else if (ast instanceof LiteralMap) {
  25788. const entries = ast.keys.map((key, idx) => {
  25789. const value = ast.values[idx];
  25790. // TODO: should literals have source maps, or do we just map the whole surrounding
  25791. // expression?
  25792. return new LiteralMapEntry(key.key, convertAst(value, job, baseSourceSpan), key.quoted);
  25793. });
  25794. return new LiteralMapExpr(entries, undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25795. }
  25796. else if (ast instanceof LiteralArray) {
  25797. // TODO: should literals have source maps, or do we just map the whole surrounding expression?
  25798. return new LiteralArrayExpr(ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan)));
  25799. }
  25800. else if (ast instanceof Conditional) {
  25801. return new ConditionalExpr(convertAst(ast.condition, job, baseSourceSpan), convertAst(ast.trueExp, job, baseSourceSpan), convertAst(ast.falseExp, job, baseSourceSpan), undefined, convertSourceSpan(ast.span, baseSourceSpan));
  25802. }
  25803. else if (ast instanceof NonNullAssert) {
  25804. // A non-null assertion shouldn't impact generated instructions, so we can just drop it.
  25805. return convertAst(ast.expression, job, baseSourceSpan);
  25806. }
  25807. else if (ast instanceof BindingPipe) {
  25808. // TODO: pipes should probably have source maps; figure out details.
  25809. return new PipeBindingExpr(job.allocateXrefId(), new SlotHandle(), ast.name, [
  25810. convertAst(ast.exp, job, baseSourceSpan),
  25811. ...ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)),
  25812. ]);
  25813. }
  25814. else if (ast instanceof SafeKeyedRead) {
  25815. return new SafeKeyedReadExpr(convertAst(ast.receiver, job, baseSourceSpan), convertAst(ast.key, job, baseSourceSpan), convertSourceSpan(ast.span, baseSourceSpan));
  25816. }
  25817. else if (ast instanceof SafePropertyRead) {
  25818. // TODO: source span
  25819. return new SafePropertyReadExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.name);
  25820. }
  25821. else if (ast instanceof SafeCall) {
  25822. // TODO: source span
  25823. return new SafeInvokeFunctionExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.args.map((a) => convertAst(a, job, baseSourceSpan)));
  25824. }
  25825. else if (ast instanceof EmptyExpr$1) {
  25826. return new EmptyExpr(convertSourceSpan(ast.span, baseSourceSpan));
  25827. }
  25828. else if (ast instanceof PrefixNot) {
  25829. return not(convertAst(ast.expression, job, baseSourceSpan), convertSourceSpan(ast.span, baseSourceSpan));
  25830. }
  25831. else if (ast instanceof TypeofExpression) {
  25832. return typeofExpr(convertAst(ast.expression, job, baseSourceSpan));
  25833. }
  25834. else if (ast instanceof TemplateLiteral) {
  25835. return new TemplateLiteralExpr(ast.elements.map((el) => {
  25836. return new TemplateLiteralElementExpr(el.text, convertSourceSpan(el.span, baseSourceSpan));
  25837. }), ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan)), convertSourceSpan(ast.span, baseSourceSpan));
  25838. }
  25839. else {
  25840. throw new Error(`Unhandled expression type "${ast.constructor.name}" in file "${baseSourceSpan?.start.file.url}"`);
  25841. }
  25842. }
  25843. function convertAstWithInterpolation(job, value, i18nMeta, sourceSpan) {
  25844. let expression;
  25845. if (value instanceof Interpolation$1) {
  25846. expression = new Interpolation(value.strings, value.expressions.map((e) => convertAst(e, job, null)), Object.keys(asMessage(i18nMeta)?.placeholders ?? {}));
  25847. }
  25848. else if (value instanceof AST) {
  25849. expression = convertAst(value, job, null);
  25850. }
  25851. else {
  25852. expression = literal$1(value);
  25853. }
  25854. return expression;
  25855. }
  25856. // TODO: Can we populate Template binding kinds in ingest?
  25857. const BINDING_KINDS = new Map([
  25858. [exports.BindingType.Property, BindingKind.Property],
  25859. [exports.BindingType.TwoWay, BindingKind.TwoWayProperty],
  25860. [exports.BindingType.Attribute, BindingKind.Attribute],
  25861. [exports.BindingType.Class, BindingKind.ClassName],
  25862. [exports.BindingType.Style, BindingKind.StyleProperty],
  25863. [exports.BindingType.Animation, BindingKind.Animation],
  25864. ]);
  25865. /**
  25866. * Checks whether the given template is a plain ng-template (as opposed to another kind of template
  25867. * such as a structural directive template or control flow template). This is checked based on the
  25868. * tagName. We can expect that only plain ng-templates will come through with a tagName of
  25869. * 'ng-template'.
  25870. *
  25871. * Here are some of the cases we expect:
  25872. *
  25873. * | Angular HTML | Template tagName |
  25874. * | ---------------------------------- | ------------------ |
  25875. * | `<ng-template>` | 'ng-template' |
  25876. * | `<div *ngIf="true">` | 'div' |
  25877. * | `<svg><ng-template>` | 'svg:ng-template' |
  25878. * | `@if (true) {` | 'Conditional' |
  25879. * | `<ng-template *ngIf>` (plain) | 'ng-template' |
  25880. * | `<ng-template *ngIf>` (structural) | null |
  25881. */
  25882. function isPlainTemplate(tmpl) {
  25883. return splitNsName(tmpl.tagName ?? '')[1] === NG_TEMPLATE_TAG_NAME;
  25884. }
  25885. /**
  25886. * Ensures that the i18nMeta, if provided, is an i18n.Message.
  25887. */
  25888. function asMessage(i18nMeta) {
  25889. if (i18nMeta == null) {
  25890. return null;
  25891. }
  25892. if (!(i18nMeta instanceof Message)) {
  25893. throw Error(`Expected i18n meta to be a Message, but got: ${i18nMeta.constructor.name}`);
  25894. }
  25895. return i18nMeta;
  25896. }
  25897. /**
  25898. * Process all of the bindings on an element in the template AST and convert them to their IR
  25899. * representation.
  25900. */
  25901. function ingestElementBindings(unit, op, element) {
  25902. let bindings = new Array();
  25903. let i18nAttributeBindingNames = new Set();
  25904. for (const attr of element.attributes) {
  25905. // Attribute literal bindings, such as `attr.foo="bar"`.
  25906. const securityContext = domSchema.securityContext(element.name, attr.name, true);
  25907. bindings.push(createBindingOp(op.xref, BindingKind.Attribute, attr.name, convertAstWithInterpolation(unit.job, attr.value, attr.i18n), null, securityContext, true, false, null, asMessage(attr.i18n), attr.sourceSpan));
  25908. if (attr.i18n) {
  25909. i18nAttributeBindingNames.add(attr.name);
  25910. }
  25911. }
  25912. for (const input of element.inputs) {
  25913. if (i18nAttributeBindingNames.has(input.name)) {
  25914. console.error(`On component ${unit.job.componentName}, the binding ${input.name} is both an i18n attribute and a property. You may want to remove the property binding. This will become a compilation error in future versions of Angular.`);
  25915. }
  25916. // All dynamic bindings (both attribute and property bindings).
  25917. bindings.push(createBindingOp(op.xref, BINDING_KINDS.get(input.type), input.name, convertAstWithInterpolation(unit.job, astOf(input.value), input.i18n), input.unit, input.securityContext, false, false, null, asMessage(input.i18n) ?? null, input.sourceSpan));
  25918. }
  25919. unit.create.push(bindings.filter((b) => b?.kind === OpKind.ExtractedAttribute));
  25920. unit.update.push(bindings.filter((b) => b?.kind === OpKind.Binding));
  25921. for (const output of element.outputs) {
  25922. if (output.type === exports.ParsedEventType.Animation && output.phase === null) {
  25923. throw Error('Animation listener should have a phase');
  25924. }
  25925. if (output.type === exports.ParsedEventType.TwoWay) {
  25926. unit.create.push(createTwoWayListenerOp(op.xref, op.handle, output.name, op.tag, makeTwoWayListenerHandlerOps(unit, output.handler, output.handlerSpan), output.sourceSpan));
  25927. }
  25928. else {
  25929. unit.create.push(createListenerOp(op.xref, op.handle, output.name, op.tag, makeListenerHandlerOps(unit, output.handler, output.handlerSpan), output.phase, output.target, false, output.sourceSpan));
  25930. }
  25931. }
  25932. // If any of the bindings on this element have an i18n message, then an i18n attrs configuration
  25933. // op is also required.
  25934. if (bindings.some((b) => b?.i18nMessage) !== null) {
  25935. unit.create.push(createI18nAttributesOp(unit.job.allocateXrefId(), new SlotHandle(), op.xref));
  25936. }
  25937. }
  25938. /**
  25939. * Process all of the bindings on a template in the template AST and convert them to their IR
  25940. * representation.
  25941. */
  25942. function ingestTemplateBindings(unit, op, template, templateKind) {
  25943. let bindings = new Array();
  25944. for (const attr of template.templateAttrs) {
  25945. if (attr instanceof TextAttribute) {
  25946. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  25947. bindings.push(createTemplateBinding(unit, op.xref, exports.BindingType.Attribute, attr.name, attr.value, null, securityContext, true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
  25948. }
  25949. else {
  25950. bindings.push(createTemplateBinding(unit, op.xref, attr.type, attr.name, astOf(attr.value), attr.unit, attr.securityContext, true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
  25951. }
  25952. }
  25953. for (const attr of template.attributes) {
  25954. // Attribute literal bindings, such as `attr.foo="bar"`.
  25955. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  25956. bindings.push(createTemplateBinding(unit, op.xref, exports.BindingType.Attribute, attr.name, attr.value, null, securityContext, false, templateKind, asMessage(attr.i18n), attr.sourceSpan));
  25957. }
  25958. for (const input of template.inputs) {
  25959. // Dynamic bindings (both attribute and property bindings).
  25960. bindings.push(createTemplateBinding(unit, op.xref, input.type, input.name, astOf(input.value), input.unit, input.securityContext, false, templateKind, asMessage(input.i18n), input.sourceSpan));
  25961. }
  25962. unit.create.push(bindings.filter((b) => b?.kind === OpKind.ExtractedAttribute));
  25963. unit.update.push(bindings.filter((b) => b?.kind === OpKind.Binding));
  25964. for (const output of template.outputs) {
  25965. if (output.type === exports.ParsedEventType.Animation && output.phase === null) {
  25966. throw Error('Animation listener should have a phase');
  25967. }
  25968. if (templateKind === TemplateKind.NgTemplate) {
  25969. if (output.type === exports.ParsedEventType.TwoWay) {
  25970. unit.create.push(createTwoWayListenerOp(op.xref, op.handle, output.name, op.tag, makeTwoWayListenerHandlerOps(unit, output.handler, output.handlerSpan), output.sourceSpan));
  25971. }
  25972. else {
  25973. unit.create.push(createListenerOp(op.xref, op.handle, output.name, op.tag, makeListenerHandlerOps(unit, output.handler, output.handlerSpan), output.phase, output.target, false, output.sourceSpan));
  25974. }
  25975. }
  25976. if (templateKind === TemplateKind.Structural &&
  25977. output.type !== exports.ParsedEventType.Animation) {
  25978. // Animation bindings are excluded from the structural template's const array.
  25979. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, output.name, false);
  25980. unit.create.push(createExtractedAttributeOp(op.xref, BindingKind.Property, null, output.name, null, null, null, securityContext));
  25981. }
  25982. }
  25983. // TODO: Perhaps we could do this in a phase? (It likely wouldn't change the slot indices.)
  25984. if (bindings.some((b) => b?.i18nMessage) !== null) {
  25985. unit.create.push(createI18nAttributesOp(unit.job.allocateXrefId(), new SlotHandle(), op.xref));
  25986. }
  25987. }
  25988. /**
  25989. * Helper to ingest an individual binding on a template, either an explicit `ng-template`, or an
  25990. * implicit template created via structural directive.
  25991. *
  25992. * Bindings on templates are *extremely* tricky. I have tried to isolate all of the confusing edge
  25993. * cases into this function, and to comment it well to document the behavior.
  25994. *
  25995. * Some of this behavior is intuitively incorrect, and we should consider changing it in the future.
  25996. *
  25997. * @param view The compilation unit for the view containing the template.
  25998. * @param xref The xref of the template op.
  25999. * @param type The binding type, according to the parser. This is fairly reasonable, e.g. both
  26000. * dynamic and static attributes have e.BindingType.Attribute.
  26001. * @param name The binding's name.
  26002. * @param value The bindings's value, which will either be an input AST expression, or a string
  26003. * literal. Note that the input AST expression may or may not be const -- it will only be a
  26004. * string literal if the parser considered it a text binding.
  26005. * @param unit If the binding has a unit (e.g. `px` for style bindings), then this is the unit.
  26006. * @param securityContext The security context of the binding.
  26007. * @param isStructuralTemplateAttribute Whether this binding actually applies to the structural
  26008. * ng-template. For example, an `ngFor` would actually apply to the structural template. (Most
  26009. * bindings on structural elements target the inner element, not the template.)
  26010. * @param templateKind Whether this is an explicit `ng-template` or an implicit template created by
  26011. * a structural directive. This should never be a block template.
  26012. * @param i18nMessage The i18n metadata for the binding, if any.
  26013. * @param sourceSpan The source span of the binding.
  26014. * @returns An IR binding op, or null if the binding should be skipped.
  26015. */
  26016. function createTemplateBinding(view, xref, type, name, value, unit, securityContext, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan) {
  26017. const isTextBinding = typeof value === 'string';
  26018. // If this is a structural template, then several kinds of bindings should not result in an
  26019. // update instruction.
  26020. if (templateKind === TemplateKind.Structural) {
  26021. if (!isStructuralTemplateAttribute) {
  26022. switch (type) {
  26023. case exports.BindingType.Property:
  26024. case exports.BindingType.Class:
  26025. case exports.BindingType.Style:
  26026. // Because this binding doesn't really target the ng-template, it must be a binding on an
  26027. // inner node of a structural template. We can't skip it entirely, because we still need
  26028. // it on the ng-template's consts (e.g. for the purposes of directive matching). However,
  26029. // we should not generate an update instruction for it.
  26030. return createExtractedAttributeOp(xref, BindingKind.Property, null, name, null, null, i18nMessage, securityContext);
  26031. case exports.BindingType.TwoWay:
  26032. return createExtractedAttributeOp(xref, BindingKind.TwoWayProperty, null, name, null, null, i18nMessage, securityContext);
  26033. }
  26034. }
  26035. if (!isTextBinding && (type === exports.BindingType.Attribute || type === exports.BindingType.Animation)) {
  26036. // Again, this binding doesn't really target the ng-template; it actually targets the element
  26037. // inside the structural template. In the case of non-text attribute or animation bindings,
  26038. // the binding doesn't even show up on the ng-template const array, so we just skip it
  26039. // entirely.
  26040. return null;
  26041. }
  26042. }
  26043. let bindingType = BINDING_KINDS.get(type);
  26044. if (templateKind === TemplateKind.NgTemplate) {
  26045. // We know we are dealing with bindings directly on an explicit ng-template.
  26046. // Static attribute bindings should be collected into the const array as k/v pairs. Property
  26047. // bindings should result in a `property` instruction, and `AttributeMarker.Bindings` const
  26048. // entries.
  26049. //
  26050. // The difficulty is with dynamic attribute, style, and class bindings. These don't really make
  26051. // sense on an `ng-template` and should probably be parser errors. However,
  26052. // TemplateDefinitionBuilder generates `property` instructions for them, and so we do that as
  26053. // well.
  26054. //
  26055. // Note that we do have a slight behavior difference with TemplateDefinitionBuilder: although
  26056. // TDB emits `property` instructions for dynamic attributes, styles, and classes, only styles
  26057. // and classes also get const collected into the `AttributeMarker.Bindings` field. Dynamic
  26058. // attribute bindings are missing from the consts entirely. We choose to emit them into the
  26059. // consts field anyway, to avoid creating special cases for something so arcane and nonsensical.
  26060. if (type === exports.BindingType.Class ||
  26061. type === exports.BindingType.Style ||
  26062. (type === exports.BindingType.Attribute && !isTextBinding)) {
  26063. // TODO: These cases should be parse errors.
  26064. bindingType = BindingKind.Property;
  26065. }
  26066. }
  26067. return createBindingOp(xref, bindingType, name, convertAstWithInterpolation(view.job, value, i18nMessage), unit, securityContext, isTextBinding, isStructuralTemplateAttribute, templateKind, i18nMessage, sourceSpan);
  26068. }
  26069. function makeListenerHandlerOps(unit, handler, handlerSpan) {
  26070. handler = astOf(handler);
  26071. const handlerOps = new Array();
  26072. let handlerExprs = handler instanceof Chain ? handler.expressions : [handler];
  26073. if (handlerExprs.length === 0) {
  26074. throw new Error('Expected listener to have non-empty expression list.');
  26075. }
  26076. const expressions = handlerExprs.map((expr) => convertAst(expr, unit.job, handlerSpan));
  26077. const returnExpr = expressions.pop();
  26078. handlerOps.push(...expressions.map((e) => createStatementOp(new ExpressionStatement(e, e.sourceSpan))));
  26079. handlerOps.push(createStatementOp(new ReturnStatement(returnExpr, returnExpr.sourceSpan)));
  26080. return handlerOps;
  26081. }
  26082. function makeTwoWayListenerHandlerOps(unit, handler, handlerSpan) {
  26083. handler = astOf(handler);
  26084. const handlerOps = new Array();
  26085. if (handler instanceof Chain) {
  26086. if (handler.expressions.length === 1) {
  26087. handler = handler.expressions[0];
  26088. }
  26089. else {
  26090. // This is validated during parsing already, but we do it here just in case.
  26091. throw new Error('Expected two-way listener to have a single expression.');
  26092. }
  26093. }
  26094. const handlerExpr = convertAst(handler, unit.job, handlerSpan);
  26095. const eventReference = new LexicalReadExpr('$event');
  26096. const twoWaySetExpr = new TwoWayBindingSetExpr(handlerExpr, eventReference);
  26097. handlerOps.push(createStatementOp(new ExpressionStatement(twoWaySetExpr)));
  26098. handlerOps.push(createStatementOp(new ReturnStatement(eventReference)));
  26099. return handlerOps;
  26100. }
  26101. function astOf(ast) {
  26102. return ast instanceof ASTWithSource ? ast.ast : ast;
  26103. }
  26104. /**
  26105. * Process all of the local references on an element-like structure in the template AST and
  26106. * convert them to their IR representation.
  26107. */
  26108. function ingestReferences(op, element) {
  26109. assertIsArray(op.localRefs);
  26110. for (const { name, value } of element.references) {
  26111. op.localRefs.push({
  26112. name,
  26113. target: value,
  26114. });
  26115. }
  26116. }
  26117. /**
  26118. * Assert that the given value is an array.
  26119. */
  26120. function assertIsArray(value) {
  26121. if (!Array.isArray(value)) {
  26122. throw new Error(`AssertionError: expected an array`);
  26123. }
  26124. }
  26125. /**
  26126. * Creates an absolute `ParseSourceSpan` from the relative `ParseSpan`.
  26127. *
  26128. * `ParseSpan` objects are relative to the start of the expression.
  26129. * This method converts these to full `ParseSourceSpan` objects that
  26130. * show where the span is within the overall source file.
  26131. *
  26132. * @param span the relative span to convert.
  26133. * @param baseSourceSpan a span corresponding to the base of the expression tree.
  26134. * @returns a `ParseSourceSpan` for the given span or null if no `baseSourceSpan` was provided.
  26135. */
  26136. function convertSourceSpan(span, baseSourceSpan) {
  26137. if (baseSourceSpan === null) {
  26138. return null;
  26139. }
  26140. const start = baseSourceSpan.start.moveBy(span.start);
  26141. const end = baseSourceSpan.start.moveBy(span.end);
  26142. const fullStart = baseSourceSpan.fullStart.moveBy(span.start);
  26143. return new ParseSourceSpan(start, end, fullStart);
  26144. }
  26145. /**
  26146. * With the directive-based control flow users were able to conditionally project content using
  26147. * the `*` syntax. E.g. `<div *ngIf="expr" projectMe></div>` will be projected into
  26148. * `<ng-content select="[projectMe]"/>`, because the attributes and tag name from the `div` are
  26149. * copied to the template via the template creation instruction. With `@if` and `@for` that is
  26150. * not the case, because the conditional is placed *around* elements, rather than *on* them.
  26151. * The result is that content projection won't work in the same way if a user converts from
  26152. * `*ngIf` to `@if`.
  26153. *
  26154. * This function aims to cover the most common case by doing the same copying when a control flow
  26155. * node has *one and only one* root element or template node.
  26156. *
  26157. * This approach comes with some caveats:
  26158. * 1. As soon as any other node is added to the root, the copying behavior won't work anymore.
  26159. * A diagnostic will be added to flag cases like this and to explain how to work around it.
  26160. * 2. If `preserveWhitespaces` is enabled, it's very likely that indentation will break this
  26161. * workaround, because it'll include an additional text node as the first child. We can work
  26162. * around it here, but in a discussion it was decided not to, because the user explicitly opted
  26163. * into preserving the whitespace and we would have to drop it from the generated code.
  26164. * The diagnostic mentioned point in #1 will flag such cases to users.
  26165. *
  26166. * @returns Tag name to be used for the control flow template.
  26167. */
  26168. function ingestControlFlowInsertionPoint(unit, xref, node) {
  26169. let root = null;
  26170. for (const child of node.children) {
  26171. // Skip over comment nodes and @let declarations since
  26172. // it doesn't matter where they end up in the DOM.
  26173. if (child instanceof Comment$1 || child instanceof LetDeclaration$1) {
  26174. continue;
  26175. }
  26176. // We can only infer the tag name/attributes if there's a single root node.
  26177. if (root !== null) {
  26178. return null;
  26179. }
  26180. // Root nodes can only elements or templates with a tag name (e.g. `<div *foo></div>`).
  26181. if (child instanceof Element$1 || (child instanceof Template && child.tagName !== null)) {
  26182. root = child;
  26183. }
  26184. else {
  26185. return null;
  26186. }
  26187. }
  26188. // If we've found a single root node, its tag name and attributes can be
  26189. // copied to the surrounding template to be used for content projection.
  26190. if (root !== null) {
  26191. // Collect the static attributes for content projection purposes.
  26192. for (const attr of root.attributes) {
  26193. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  26194. unit.update.push(createBindingOp(xref, BindingKind.Attribute, attr.name, literal$1(attr.value), null, securityContext, true, false, null, asMessage(attr.i18n), attr.sourceSpan));
  26195. }
  26196. // Also collect the inputs since they participate in content projection as well.
  26197. // Note that TDB used to collect the outputs as well, but it wasn't passing them into
  26198. // the template instruction. Here we just don't collect them.
  26199. for (const attr of root.inputs) {
  26200. if (attr.type !== exports.BindingType.Animation && attr.type !== exports.BindingType.Attribute) {
  26201. const securityContext = domSchema.securityContext(NG_TEMPLATE_TAG_NAME, attr.name, true);
  26202. unit.create.push(createExtractedAttributeOp(xref, BindingKind.Property, null, attr.name, null, null, null, securityContext));
  26203. }
  26204. }
  26205. const tagName = root instanceof Element$1 ? root.name : root.tagName;
  26206. // Don't pass along `ng-template` tag name since it enables directive matching.
  26207. return tagName === NG_TEMPLATE_TAG_NAME ? null : tagName;
  26208. }
  26209. return null;
  26210. }
  26211. /*!
  26212. * @license
  26213. * Copyright Google LLC All Rights Reserved.
  26214. *
  26215. * Use of this source code is governed by an MIT-style license that can be
  26216. * found in the LICENSE file at https://angular.dev/license
  26217. */
  26218. /**
  26219. * Whether to produce instructions that will attach the source location to each DOM node.
  26220. *
  26221. * !!!Important!!! at the time of writing this flag isn't exposed externally, but internal debug
  26222. * tools enable it via a local change. Any modifications to this flag need to update the
  26223. * internal tooling as well.
  26224. */
  26225. let ENABLE_TEMPLATE_SOURCE_LOCATIONS = false;
  26226. /** Gets whether template source locations are enabled. */
  26227. function getTemplateSourceLocationsEnabled() {
  26228. return ENABLE_TEMPLATE_SOURCE_LOCATIONS;
  26229. }
  26230. // if (rf & flags) { .. }
  26231. function renderFlagCheckIfStmt(flags, statements) {
  26232. return ifStmt(variable(RENDER_FLAGS).bitwiseAnd(literal$1(flags), null, false), statements);
  26233. }
  26234. /**
  26235. * Translates query flags into `TQueryFlags` type in
  26236. * packages/core/src/render3/interfaces/query.ts
  26237. * @param query
  26238. */
  26239. function toQueryFlags(query) {
  26240. return ((query.descendants ? 1 /* QueryFlags.descendants */ : 0 /* QueryFlags.none */) |
  26241. (query.static ? 2 /* QueryFlags.isStatic */ : 0 /* QueryFlags.none */) |
  26242. (query.emitDistinctChangesOnly ? 4 /* QueryFlags.emitDistinctChangesOnly */ : 0 /* QueryFlags.none */));
  26243. }
  26244. function getQueryPredicate(query, constantPool) {
  26245. if (Array.isArray(query.predicate)) {
  26246. let predicate = [];
  26247. query.predicate.forEach((selector) => {
  26248. // Each item in predicates array may contain strings with comma-separated refs
  26249. // (for ex. 'ref, ref1, ..., refN'), thus we extract individual refs and store them
  26250. // as separate array entities
  26251. const selectors = selector.split(',').map((token) => literal$1(token.trim()));
  26252. predicate.push(...selectors);
  26253. });
  26254. return constantPool.getConstLiteral(literalArr(predicate), true);
  26255. }
  26256. else {
  26257. // The original predicate may have been wrapped in a `forwardRef()` call.
  26258. switch (query.predicate.forwardRef) {
  26259. case 0 /* ForwardRefHandling.None */:
  26260. case 2 /* ForwardRefHandling.Unwrapped */:
  26261. return query.predicate.expression;
  26262. case 1 /* ForwardRefHandling.Wrapped */:
  26263. return importExpr(Identifiers.resolveForwardRef).callFn([query.predicate.expression]);
  26264. }
  26265. }
  26266. }
  26267. function createQueryCreateCall(query, constantPool, queryTypeFns, prependParams) {
  26268. const parameters = [];
  26269. if (prependParams !== undefined) {
  26270. parameters.push(...prependParams);
  26271. }
  26272. if (query.isSignal) {
  26273. parameters.push(new ReadPropExpr(variable(CONTEXT_NAME), query.propertyName));
  26274. }
  26275. parameters.push(getQueryPredicate(query, constantPool), literal$1(toQueryFlags(query)));
  26276. if (query.read) {
  26277. parameters.push(query.read);
  26278. }
  26279. const queryCreateFn = query.isSignal ? queryTypeFns.signalBased : queryTypeFns.nonSignal;
  26280. return importExpr(queryCreateFn).callFn(parameters);
  26281. }
  26282. const queryAdvancePlaceholder = Symbol('queryAdvancePlaceholder');
  26283. /**
  26284. * Collapses query advance placeholders in a list of statements.
  26285. *
  26286. * This allows for less generated code because multiple sibling query advance
  26287. * statements can be collapsed into a single call with the count as argument.
  26288. *
  26289. * e.g.
  26290. *
  26291. * ```ts
  26292. * bla();
  26293. * queryAdvance();
  26294. * queryAdvance();
  26295. * bla();
  26296. * ```
  26297. *
  26298. * --> will turn into
  26299. *
  26300. * ```ts
  26301. * bla();
  26302. * queryAdvance(2);
  26303. * bla();
  26304. * ```
  26305. */
  26306. function collapseAdvanceStatements(statements) {
  26307. const result = [];
  26308. let advanceCollapseCount = 0;
  26309. const flushAdvanceCount = () => {
  26310. if (advanceCollapseCount > 0) {
  26311. result.unshift(importExpr(Identifiers.queryAdvance)
  26312. .callFn(advanceCollapseCount === 1 ? [] : [literal$1(advanceCollapseCount)])
  26313. .toStmt());
  26314. advanceCollapseCount = 0;
  26315. }
  26316. };
  26317. // Iterate through statements in reverse and collapse advance placeholders.
  26318. for (let i = statements.length - 1; i >= 0; i--) {
  26319. const st = statements[i];
  26320. if (st === queryAdvancePlaceholder) {
  26321. advanceCollapseCount++;
  26322. }
  26323. else {
  26324. flushAdvanceCount();
  26325. result.unshift(st);
  26326. }
  26327. }
  26328. flushAdvanceCount();
  26329. return result;
  26330. }
  26331. // Define and update any view queries
  26332. function createViewQueriesFunction(viewQueries, constantPool, name) {
  26333. const createStatements = [];
  26334. const updateStatements = [];
  26335. const tempAllocator = temporaryAllocator((st) => updateStatements.push(st), TEMPORARY_NAME);
  26336. viewQueries.forEach((query) => {
  26337. // creation call, e.g. r3.viewQuery(somePredicate, true) or
  26338. // r3.viewQuerySignal(ctx.prop, somePredicate, true);
  26339. const queryDefinitionCall = createQueryCreateCall(query, constantPool, {
  26340. signalBased: Identifiers.viewQuerySignal,
  26341. nonSignal: Identifiers.viewQuery,
  26342. });
  26343. createStatements.push(queryDefinitionCall.toStmt());
  26344. // Signal queries update lazily and we just advance the index.
  26345. if (query.isSignal) {
  26346. updateStatements.push(queryAdvancePlaceholder);
  26347. return;
  26348. }
  26349. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  26350. const temporary = tempAllocator();
  26351. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  26352. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  26353. const updateDirective = variable(CONTEXT_NAME)
  26354. .prop(query.propertyName)
  26355. .set(query.first ? temporary.prop('first') : temporary);
  26356. updateStatements.push(refresh.and(updateDirective).toStmt());
  26357. });
  26358. const viewQueryFnName = name ? `${name}_Query` : null;
  26359. return fn([new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], [
  26360. renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, createStatements),
  26361. renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, collapseAdvanceStatements(updateStatements)),
  26362. ], INFERRED_TYPE, null, viewQueryFnName);
  26363. }
  26364. // Define and update any content queries
  26365. function createContentQueriesFunction(queries, constantPool, name) {
  26366. const createStatements = [];
  26367. const updateStatements = [];
  26368. const tempAllocator = temporaryAllocator((st) => updateStatements.push(st), TEMPORARY_NAME);
  26369. for (const query of queries) {
  26370. // creation, e.g. r3.contentQuery(dirIndex, somePredicate, true, null) or
  26371. // r3.contentQuerySignal(dirIndex, propName, somePredicate, <flags>, <read>).
  26372. createStatements.push(createQueryCreateCall(query, constantPool, { nonSignal: Identifiers.contentQuery, signalBased: Identifiers.contentQuerySignal },
  26373. /* prependParams */ [variable('dirIndex')]).toStmt());
  26374. // Signal queries update lazily and we just advance the index.
  26375. if (query.isSignal) {
  26376. updateStatements.push(queryAdvancePlaceholder);
  26377. continue;
  26378. }
  26379. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  26380. const temporary = tempAllocator();
  26381. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  26382. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  26383. const updateDirective = variable(CONTEXT_NAME)
  26384. .prop(query.propertyName)
  26385. .set(query.first ? temporary.prop('first') : temporary);
  26386. updateStatements.push(refresh.and(updateDirective).toStmt());
  26387. }
  26388. const contentQueriesFnName = name ? `${name}_ContentQueries` : null;
  26389. return fn([
  26390. new FnParam(RENDER_FLAGS, NUMBER_TYPE),
  26391. new FnParam(CONTEXT_NAME, null),
  26392. new FnParam('dirIndex', null),
  26393. ], [
  26394. renderFlagCheckIfStmt(1 /* core.RenderFlags.Create */, createStatements),
  26395. renderFlagCheckIfStmt(2 /* core.RenderFlags.Update */, collapseAdvanceStatements(updateStatements)),
  26396. ], INFERRED_TYPE, null, contentQueriesFnName);
  26397. }
  26398. class HtmlParser extends Parser$1 {
  26399. constructor() {
  26400. super(getHtmlTagDefinition);
  26401. }
  26402. parse(source, url, options) {
  26403. return super.parse(source, url, options);
  26404. }
  26405. }
  26406. const PROPERTY_PARTS_SEPARATOR = '.';
  26407. const ATTRIBUTE_PREFIX = 'attr';
  26408. const CLASS_PREFIX = 'class';
  26409. const STYLE_PREFIX = 'style';
  26410. const TEMPLATE_ATTR_PREFIX$1 = '*';
  26411. const ANIMATE_PROP_PREFIX = 'animate-';
  26412. /**
  26413. * Parses bindings in templates and in the directive host area.
  26414. */
  26415. class BindingParser {
  26416. _exprParser;
  26417. _interpolationConfig;
  26418. _schemaRegistry;
  26419. errors;
  26420. constructor(_exprParser, _interpolationConfig, _schemaRegistry, errors) {
  26421. this._exprParser = _exprParser;
  26422. this._interpolationConfig = _interpolationConfig;
  26423. this._schemaRegistry = _schemaRegistry;
  26424. this.errors = errors;
  26425. }
  26426. get interpolationConfig() {
  26427. return this._interpolationConfig;
  26428. }
  26429. createBoundHostProperties(properties, sourceSpan) {
  26430. const boundProps = [];
  26431. for (const propName of Object.keys(properties)) {
  26432. const expression = properties[propName];
  26433. if (typeof expression === 'string') {
  26434. this.parsePropertyBinding(propName, expression, true, false, sourceSpan, sourceSpan.start.offset, undefined, [],
  26435. // Use the `sourceSpan` for `keySpan`. This isn't really accurate, but neither is the
  26436. // sourceSpan, as it represents the sourceSpan of the host itself rather than the
  26437. // source of the host binding (which doesn't exist in the template). Regardless,
  26438. // neither of these values are used in Ivy but are only here to satisfy the function
  26439. // signature. This should likely be refactored in the future so that `sourceSpan`
  26440. // isn't being used inaccurately.
  26441. boundProps, sourceSpan);
  26442. }
  26443. else {
  26444. this._reportError(`Value of the host property binding "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  26445. }
  26446. }
  26447. return boundProps;
  26448. }
  26449. createDirectiveHostEventAsts(hostListeners, sourceSpan) {
  26450. const targetEvents = [];
  26451. for (const propName of Object.keys(hostListeners)) {
  26452. const expression = hostListeners[propName];
  26453. if (typeof expression === 'string') {
  26454. // Use the `sourceSpan` for `keySpan` and `handlerSpan`. This isn't really accurate, but
  26455. // neither is the `sourceSpan`, as it represents the `sourceSpan` of the host itself
  26456. // rather than the source of the host binding (which doesn't exist in the template).
  26457. // Regardless, neither of these values are used in Ivy but are only here to satisfy the
  26458. // function signature. This should likely be refactored in the future so that `sourceSpan`
  26459. // isn't being used inaccurately.
  26460. this.parseEvent(propName, expression,
  26461. /* isAssignmentEvent */ false, sourceSpan, sourceSpan, [], targetEvents, sourceSpan);
  26462. }
  26463. else {
  26464. this._reportError(`Value of the host listener "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  26465. }
  26466. }
  26467. return targetEvents;
  26468. }
  26469. parseInterpolation(value, sourceSpan, interpolatedTokens) {
  26470. const sourceInfo = sourceSpan.start.toString();
  26471. const absoluteOffset = sourceSpan.fullStart.offset;
  26472. try {
  26473. const ast = this._exprParser.parseInterpolation(value, sourceInfo, absoluteOffset, interpolatedTokens, this._interpolationConfig);
  26474. if (ast)
  26475. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26476. return ast;
  26477. }
  26478. catch (e) {
  26479. this._reportError(`${e}`, sourceSpan);
  26480. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26481. }
  26482. }
  26483. /**
  26484. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  26485. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  26486. * This is used for parsing the switch expression in ICUs.
  26487. */
  26488. parseInterpolationExpression(expression, sourceSpan) {
  26489. const sourceInfo = sourceSpan.start.toString();
  26490. const absoluteOffset = sourceSpan.start.offset;
  26491. try {
  26492. const ast = this._exprParser.parseInterpolationExpression(expression, sourceInfo, absoluteOffset);
  26493. if (ast)
  26494. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26495. return ast;
  26496. }
  26497. catch (e) {
  26498. this._reportError(`${e}`, sourceSpan);
  26499. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26500. }
  26501. }
  26502. /**
  26503. * Parses the bindings in a microsyntax expression, and converts them to
  26504. * `ParsedProperty` or `ParsedVariable`.
  26505. *
  26506. * @param tplKey template binding name
  26507. * @param tplValue template binding value
  26508. * @param sourceSpan span of template binding relative to entire the template
  26509. * @param absoluteValueOffset start of the tplValue relative to the entire template
  26510. * @param targetMatchableAttrs potential attributes to match in the template
  26511. * @param targetProps target property bindings in the template
  26512. * @param targetVars target variables in the template
  26513. */
  26514. parseInlineTemplateBinding(tplKey, tplValue, sourceSpan, absoluteValueOffset, targetMatchableAttrs, targetProps, targetVars, isIvyAst) {
  26515. const absoluteKeyOffset = sourceSpan.start.offset + TEMPLATE_ATTR_PREFIX$1.length;
  26516. const bindings = this._parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset);
  26517. for (const binding of bindings) {
  26518. // sourceSpan is for the entire HTML attribute. bindingSpan is for a particular
  26519. // binding within the microsyntax expression so it's more narrow than sourceSpan.
  26520. const bindingSpan = moveParseSourceSpan(sourceSpan, binding.sourceSpan);
  26521. const key = binding.key.source;
  26522. const keySpan = moveParseSourceSpan(sourceSpan, binding.key.span);
  26523. if (binding instanceof VariableBinding) {
  26524. const value = binding.value ? binding.value.source : '$implicit';
  26525. const valueSpan = binding.value
  26526. ? moveParseSourceSpan(sourceSpan, binding.value.span)
  26527. : undefined;
  26528. targetVars.push(new ParsedVariable(key, value, bindingSpan, keySpan, valueSpan));
  26529. }
  26530. else if (binding.value) {
  26531. const srcSpan = isIvyAst ? bindingSpan : sourceSpan;
  26532. const valueSpan = moveParseSourceSpan(sourceSpan, binding.value.ast.sourceSpan);
  26533. this._parsePropertyAst(key, binding.value, false, srcSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26534. }
  26535. else {
  26536. targetMatchableAttrs.push([key, '' /* value */]);
  26537. // Since this is a literal attribute with no RHS, source span should be
  26538. // just the key span.
  26539. this.parseLiteralAttr(key, null /* value */, keySpan, absoluteValueOffset, undefined /* valueSpan */, targetMatchableAttrs, targetProps, keySpan);
  26540. }
  26541. }
  26542. }
  26543. /**
  26544. * Parses the bindings in a microsyntax expression, e.g.
  26545. * ```html
  26546. * <tag *tplKey="let value1 = prop; let value2 = localVar">
  26547. * ```
  26548. *
  26549. * @param tplKey template binding name
  26550. * @param tplValue template binding value
  26551. * @param sourceSpan span of template binding relative to entire the template
  26552. * @param absoluteKeyOffset start of the `tplKey`
  26553. * @param absoluteValueOffset start of the `tplValue`
  26554. */
  26555. _parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset) {
  26556. const sourceInfo = sourceSpan.start.toString();
  26557. try {
  26558. const bindingsResult = this._exprParser.parseTemplateBindings(tplKey, tplValue, sourceInfo, absoluteKeyOffset, absoluteValueOffset);
  26559. this._reportExpressionParserErrors(bindingsResult.errors, sourceSpan);
  26560. bindingsResult.warnings.forEach((warning) => {
  26561. this._reportError(warning, sourceSpan, ParseErrorLevel.WARNING);
  26562. });
  26563. return bindingsResult.templateBindings;
  26564. }
  26565. catch (e) {
  26566. this._reportError(`${e}`, sourceSpan);
  26567. return [];
  26568. }
  26569. }
  26570. parseLiteralAttr(name, value, sourceSpan, absoluteOffset, valueSpan, targetMatchableAttrs, targetProps, keySpan) {
  26571. if (isAnimationLabel(name)) {
  26572. name = name.substring(1);
  26573. if (keySpan !== undefined) {
  26574. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  26575. }
  26576. if (value) {
  26577. this._reportError(`Assigning animation triggers via @prop="exp" attributes with an expression is invalid.` +
  26578. ` Use property bindings (e.g. [@prop]="exp") or use an attribute without a value (e.g. @prop) instead.`, sourceSpan, ParseErrorLevel.ERROR);
  26579. }
  26580. this._parseAnimation(name, value, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26581. }
  26582. else {
  26583. targetProps.push(new ParsedProperty(name, this._exprParser.wrapLiteralPrimitive(value, '', absoluteOffset), ParsedPropertyType.LITERAL_ATTR, sourceSpan, keySpan, valueSpan));
  26584. }
  26585. }
  26586. parsePropertyBinding(name, expression, isHost, isPartOfAssignmentBinding, sourceSpan, absoluteOffset, valueSpan, targetMatchableAttrs, targetProps, keySpan) {
  26587. if (name.length === 0) {
  26588. this._reportError(`Property name is missing in binding`, sourceSpan);
  26589. }
  26590. let isAnimationProp = false;
  26591. if (name.startsWith(ANIMATE_PROP_PREFIX)) {
  26592. isAnimationProp = true;
  26593. name = name.substring(ANIMATE_PROP_PREFIX.length);
  26594. if (keySpan !== undefined) {
  26595. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + ANIMATE_PROP_PREFIX.length, keySpan.end.offset));
  26596. }
  26597. }
  26598. else if (isAnimationLabel(name)) {
  26599. isAnimationProp = true;
  26600. name = name.substring(1);
  26601. if (keySpan !== undefined) {
  26602. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  26603. }
  26604. }
  26605. if (isAnimationProp) {
  26606. this._parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26607. }
  26608. else {
  26609. this._parsePropertyAst(name, this.parseBinding(expression, isHost, valueSpan || sourceSpan, absoluteOffset), isPartOfAssignmentBinding, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26610. }
  26611. }
  26612. parsePropertyInterpolation(name, value, sourceSpan, valueSpan, targetMatchableAttrs, targetProps, keySpan, interpolatedTokens) {
  26613. const expr = this.parseInterpolation(value, valueSpan || sourceSpan, interpolatedTokens);
  26614. if (expr) {
  26615. this._parsePropertyAst(name, expr, false, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  26616. return true;
  26617. }
  26618. return false;
  26619. }
  26620. _parsePropertyAst(name, ast, isPartOfAssignmentBinding, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  26621. targetMatchableAttrs.push([name, ast.source]);
  26622. targetProps.push(new ParsedProperty(name, ast, isPartOfAssignmentBinding ? ParsedPropertyType.TWO_WAY : ParsedPropertyType.DEFAULT, sourceSpan, keySpan, valueSpan));
  26623. }
  26624. _parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  26625. if (name.length === 0) {
  26626. this._reportError('Animation trigger is missing', sourceSpan);
  26627. }
  26628. // This will occur when a @trigger is not paired with an expression.
  26629. // For animations it is valid to not have an expression since */void
  26630. // states will be applied by angular when the element is attached/detached
  26631. const ast = this.parseBinding(expression || 'undefined', false, valueSpan || sourceSpan, absoluteOffset);
  26632. targetMatchableAttrs.push([name, ast.source]);
  26633. targetProps.push(new ParsedProperty(name, ast, ParsedPropertyType.ANIMATION, sourceSpan, keySpan, valueSpan));
  26634. }
  26635. parseBinding(value, isHostBinding, sourceSpan, absoluteOffset) {
  26636. const sourceInfo = ((sourceSpan && sourceSpan.start) || '(unknown)').toString();
  26637. try {
  26638. const ast = isHostBinding
  26639. ? this._exprParser.parseSimpleBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig)
  26640. : this._exprParser.parseBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  26641. if (ast)
  26642. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26643. return ast;
  26644. }
  26645. catch (e) {
  26646. this._reportError(`${e}`, sourceSpan);
  26647. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26648. }
  26649. }
  26650. createBoundElementProperty(elementSelector, boundProp, skipValidation = false, mapPropertyName = true) {
  26651. if (boundProp.isAnimation) {
  26652. return new BoundElementProperty(boundProp.name, exports.BindingType.Animation, SecurityContext.NONE, boundProp.expression, null, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  26653. }
  26654. let unit = null;
  26655. let bindingType = undefined;
  26656. let boundPropertyName = null;
  26657. const parts = boundProp.name.split(PROPERTY_PARTS_SEPARATOR);
  26658. let securityContexts = undefined;
  26659. // Check for special cases (prefix style, attr, class)
  26660. if (parts.length > 1) {
  26661. if (parts[0] == ATTRIBUTE_PREFIX) {
  26662. boundPropertyName = parts.slice(1).join(PROPERTY_PARTS_SEPARATOR);
  26663. if (!skipValidation) {
  26664. this._validatePropertyOrAttributeName(boundPropertyName, boundProp.sourceSpan, true);
  26665. }
  26666. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, boundPropertyName, true);
  26667. const nsSeparatorIdx = boundPropertyName.indexOf(':');
  26668. if (nsSeparatorIdx > -1) {
  26669. const ns = boundPropertyName.substring(0, nsSeparatorIdx);
  26670. const name = boundPropertyName.substring(nsSeparatorIdx + 1);
  26671. boundPropertyName = mergeNsAndName(ns, name);
  26672. }
  26673. bindingType = exports.BindingType.Attribute;
  26674. }
  26675. else if (parts[0] == CLASS_PREFIX) {
  26676. boundPropertyName = parts[1];
  26677. bindingType = exports.BindingType.Class;
  26678. securityContexts = [SecurityContext.NONE];
  26679. }
  26680. else if (parts[0] == STYLE_PREFIX) {
  26681. unit = parts.length > 2 ? parts[2] : null;
  26682. boundPropertyName = parts[1];
  26683. bindingType = exports.BindingType.Style;
  26684. securityContexts = [SecurityContext.STYLE];
  26685. }
  26686. }
  26687. // If not a special case, use the full property name
  26688. if (boundPropertyName === null) {
  26689. const mappedPropName = this._schemaRegistry.getMappedPropName(boundProp.name);
  26690. boundPropertyName = mapPropertyName ? mappedPropName : boundProp.name;
  26691. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, mappedPropName, false);
  26692. bindingType =
  26693. boundProp.type === ParsedPropertyType.TWO_WAY ? exports.BindingType.TwoWay : exports.BindingType.Property;
  26694. if (!skipValidation) {
  26695. this._validatePropertyOrAttributeName(mappedPropName, boundProp.sourceSpan, false);
  26696. }
  26697. }
  26698. return new BoundElementProperty(boundPropertyName, bindingType, securityContexts[0], boundProp.expression, unit, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  26699. }
  26700. // TODO: keySpan should be required but was made optional to avoid changing VE parser.
  26701. parseEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  26702. if (name.length === 0) {
  26703. this._reportError(`Event name is missing in binding`, sourceSpan);
  26704. }
  26705. if (isAnimationLabel(name)) {
  26706. name = name.slice(1);
  26707. if (keySpan !== undefined) {
  26708. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  26709. }
  26710. this._parseAnimationEvent(name, expression, sourceSpan, handlerSpan, targetEvents, keySpan);
  26711. }
  26712. else {
  26713. this._parseRegularEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan);
  26714. }
  26715. }
  26716. calcPossibleSecurityContexts(selector, propName, isAttribute) {
  26717. const prop = this._schemaRegistry.getMappedPropName(propName);
  26718. return calcPossibleSecurityContexts(this._schemaRegistry, selector, prop, isAttribute);
  26719. }
  26720. _parseAnimationEvent(name, expression, sourceSpan, handlerSpan, targetEvents, keySpan) {
  26721. const matches = splitAtPeriod(name, [name, '']);
  26722. const eventName = matches[0];
  26723. const phase = matches[1].toLowerCase();
  26724. const ast = this._parseAction(expression, handlerSpan);
  26725. targetEvents.push(new ParsedEvent(eventName, phase, exports.ParsedEventType.Animation, ast, sourceSpan, handlerSpan, keySpan));
  26726. if (eventName.length === 0) {
  26727. this._reportError(`Animation event name is missing in binding`, sourceSpan);
  26728. }
  26729. if (phase) {
  26730. if (phase !== 'start' && phase !== 'done') {
  26731. this._reportError(`The provided animation output phase value "${phase}" for "@${eventName}" is not supported (use start or done)`, sourceSpan);
  26732. }
  26733. }
  26734. else {
  26735. this._reportError(`The animation trigger output event (@${eventName}) is missing its phase value name (start or done are currently supported)`, sourceSpan);
  26736. }
  26737. }
  26738. _parseRegularEvent(name, expression, isAssignmentEvent, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  26739. // long format: 'target: eventName'
  26740. const [target, eventName] = splitAtColon(name, [null, name]);
  26741. const prevErrorCount = this.errors.length;
  26742. const ast = this._parseAction(expression, handlerSpan);
  26743. const isValid = this.errors.length === prevErrorCount;
  26744. targetMatchableAttrs.push([name, ast.source]);
  26745. // Don't try to validate assignment events if there were other
  26746. // parsing errors to avoid adding more noise to the error logs.
  26747. if (isAssignmentEvent && isValid && !this._isAllowedAssignmentEvent(ast)) {
  26748. this._reportError('Unsupported expression in a two-way binding', sourceSpan);
  26749. }
  26750. targetEvents.push(new ParsedEvent(eventName, target, isAssignmentEvent ? exports.ParsedEventType.TwoWay : exports.ParsedEventType.Regular, ast, sourceSpan, handlerSpan, keySpan));
  26751. // Don't detect directives for event names for now,
  26752. // so don't add the event name to the matchableAttrs
  26753. }
  26754. _parseAction(value, sourceSpan) {
  26755. const sourceInfo = ((sourceSpan && sourceSpan.start) || '(unknown').toString();
  26756. const absoluteOffset = sourceSpan && sourceSpan.start ? sourceSpan.start.offset : 0;
  26757. try {
  26758. const ast = this._exprParser.parseAction(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  26759. if (ast) {
  26760. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  26761. }
  26762. if (!ast || ast.ast instanceof EmptyExpr$1) {
  26763. this._reportError(`Empty expressions are not allowed`, sourceSpan);
  26764. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26765. }
  26766. return ast;
  26767. }
  26768. catch (e) {
  26769. this._reportError(`${e}`, sourceSpan);
  26770. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  26771. }
  26772. }
  26773. _reportError(message, sourceSpan, level = ParseErrorLevel.ERROR, relatedError) {
  26774. this.errors.push(new ParseError(sourceSpan, message, level, relatedError));
  26775. }
  26776. _reportExpressionParserErrors(errors, sourceSpan) {
  26777. for (const error of errors) {
  26778. this._reportError(error.message, sourceSpan, undefined, error);
  26779. }
  26780. }
  26781. /**
  26782. * @param propName the name of the property / attribute
  26783. * @param sourceSpan
  26784. * @param isAttr true when binding to an attribute
  26785. */
  26786. _validatePropertyOrAttributeName(propName, sourceSpan, isAttr) {
  26787. const report = isAttr
  26788. ? this._schemaRegistry.validateAttribute(propName)
  26789. : this._schemaRegistry.validateProperty(propName);
  26790. if (report.error) {
  26791. this._reportError(report.msg, sourceSpan, ParseErrorLevel.ERROR);
  26792. }
  26793. }
  26794. /**
  26795. * Returns whether a parsed AST is allowed to be used within the event side of a two-way binding.
  26796. * @param ast Parsed AST to be checked.
  26797. */
  26798. _isAllowedAssignmentEvent(ast) {
  26799. if (ast instanceof ASTWithSource) {
  26800. return this._isAllowedAssignmentEvent(ast.ast);
  26801. }
  26802. if (ast instanceof NonNullAssert) {
  26803. return this._isAllowedAssignmentEvent(ast.expression);
  26804. }
  26805. if (ast instanceof Call &&
  26806. ast.args.length === 1 &&
  26807. ast.receiver instanceof PropertyRead &&
  26808. ast.receiver.name === '$any' &&
  26809. ast.receiver.receiver instanceof ImplicitReceiver &&
  26810. !(ast.receiver.receiver instanceof ThisReceiver)) {
  26811. return this._isAllowedAssignmentEvent(ast.args[0]);
  26812. }
  26813. if (ast instanceof PropertyRead || ast instanceof KeyedRead) {
  26814. return true;
  26815. }
  26816. return false;
  26817. }
  26818. }
  26819. function isAnimationLabel(name) {
  26820. return name[0] == '@';
  26821. }
  26822. function calcPossibleSecurityContexts(registry, selector, propName, isAttribute) {
  26823. const ctxs = [];
  26824. CssSelector.parse(selector).forEach((selector) => {
  26825. const elementNames = selector.element ? [selector.element] : registry.allKnownElementNames();
  26826. const notElementNames = new Set(selector.notSelectors
  26827. .filter((selector) => selector.isElementSelector())
  26828. .map((selector) => selector.element));
  26829. const possibleElementNames = elementNames.filter((elementName) => !notElementNames.has(elementName));
  26830. ctxs.push(...possibleElementNames.map((elementName) => registry.securityContext(elementName, propName, isAttribute)));
  26831. });
  26832. return ctxs.length === 0 ? [SecurityContext.NONE] : Array.from(new Set(ctxs)).sort();
  26833. }
  26834. /**
  26835. * Compute a new ParseSourceSpan based off an original `sourceSpan` by using
  26836. * absolute offsets from the specified `absoluteSpan`.
  26837. *
  26838. * @param sourceSpan original source span
  26839. * @param absoluteSpan absolute source span to move to
  26840. */
  26841. function moveParseSourceSpan(sourceSpan, absoluteSpan) {
  26842. // The difference of two absolute offsets provide the relative offset
  26843. const startDiff = absoluteSpan.start - sourceSpan.start.offset;
  26844. const endDiff = absoluteSpan.end - sourceSpan.end.offset;
  26845. return new ParseSourceSpan(sourceSpan.start.moveBy(startDiff), sourceSpan.end.moveBy(endDiff), sourceSpan.fullStart.moveBy(startDiff), sourceSpan.details);
  26846. }
  26847. // Some of the code comes from WebComponents.JS
  26848. // https://github.com/webcomponents/webcomponentsjs/blob/master/src/HTMLImports/path.js
  26849. function isStyleUrlResolvable(url) {
  26850. if (url == null || url.length === 0 || url[0] == '/')
  26851. return false;
  26852. const schemeMatch = url.match(URL_WITH_SCHEMA_REGEXP);
  26853. return schemeMatch === null || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
  26854. }
  26855. const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/;
  26856. const NG_CONTENT_SELECT_ATTR = 'select';
  26857. const LINK_ELEMENT = 'link';
  26858. const LINK_STYLE_REL_ATTR = 'rel';
  26859. const LINK_STYLE_HREF_ATTR = 'href';
  26860. const LINK_STYLE_REL_VALUE = 'stylesheet';
  26861. const STYLE_ELEMENT = 'style';
  26862. const SCRIPT_ELEMENT = 'script';
  26863. const NG_NON_BINDABLE_ATTR = 'ngNonBindable';
  26864. const NG_PROJECT_AS = 'ngProjectAs';
  26865. function preparseElement(ast) {
  26866. let selectAttr = null;
  26867. let hrefAttr = null;
  26868. let relAttr = null;
  26869. let nonBindable = false;
  26870. let projectAs = '';
  26871. ast.attrs.forEach((attr) => {
  26872. const lcAttrName = attr.name.toLowerCase();
  26873. if (lcAttrName == NG_CONTENT_SELECT_ATTR) {
  26874. selectAttr = attr.value;
  26875. }
  26876. else if (lcAttrName == LINK_STYLE_HREF_ATTR) {
  26877. hrefAttr = attr.value;
  26878. }
  26879. else if (lcAttrName == LINK_STYLE_REL_ATTR) {
  26880. relAttr = attr.value;
  26881. }
  26882. else if (attr.name == NG_NON_BINDABLE_ATTR) {
  26883. nonBindable = true;
  26884. }
  26885. else if (attr.name == NG_PROJECT_AS) {
  26886. if (attr.value.length > 0) {
  26887. projectAs = attr.value;
  26888. }
  26889. }
  26890. });
  26891. selectAttr = normalizeNgContentSelect(selectAttr);
  26892. const nodeName = ast.name.toLowerCase();
  26893. let type = PreparsedElementType.OTHER;
  26894. if (isNgContent(nodeName)) {
  26895. type = PreparsedElementType.NG_CONTENT;
  26896. }
  26897. else if (nodeName == STYLE_ELEMENT) {
  26898. type = PreparsedElementType.STYLE;
  26899. }
  26900. else if (nodeName == SCRIPT_ELEMENT) {
  26901. type = PreparsedElementType.SCRIPT;
  26902. }
  26903. else if (nodeName == LINK_ELEMENT && relAttr == LINK_STYLE_REL_VALUE) {
  26904. type = PreparsedElementType.STYLESHEET;
  26905. }
  26906. return new PreparsedElement(type, selectAttr, hrefAttr, nonBindable, projectAs);
  26907. }
  26908. var PreparsedElementType;
  26909. (function (PreparsedElementType) {
  26910. PreparsedElementType[PreparsedElementType["NG_CONTENT"] = 0] = "NG_CONTENT";
  26911. PreparsedElementType[PreparsedElementType["STYLE"] = 1] = "STYLE";
  26912. PreparsedElementType[PreparsedElementType["STYLESHEET"] = 2] = "STYLESHEET";
  26913. PreparsedElementType[PreparsedElementType["SCRIPT"] = 3] = "SCRIPT";
  26914. PreparsedElementType[PreparsedElementType["OTHER"] = 4] = "OTHER";
  26915. })(PreparsedElementType || (PreparsedElementType = {}));
  26916. class PreparsedElement {
  26917. type;
  26918. selectAttr;
  26919. hrefAttr;
  26920. nonBindable;
  26921. projectAs;
  26922. constructor(type, selectAttr, hrefAttr, nonBindable, projectAs) {
  26923. this.type = type;
  26924. this.selectAttr = selectAttr;
  26925. this.hrefAttr = hrefAttr;
  26926. this.nonBindable = nonBindable;
  26927. this.projectAs = projectAs;
  26928. }
  26929. }
  26930. function normalizeNgContentSelect(selectAttr) {
  26931. if (selectAttr === null || selectAttr.length === 0) {
  26932. return '*';
  26933. }
  26934. return selectAttr;
  26935. }
  26936. /** Pattern for the expression in a for loop block. */
  26937. const FOR_LOOP_EXPRESSION_PATTERN = /^\s*([0-9A-Za-z_$]*)\s+of\s+([\S\s]*)/;
  26938. /** Pattern for the tracking expression in a for loop block. */
  26939. const FOR_LOOP_TRACK_PATTERN = /^track\s+([\S\s]*)/;
  26940. /** Pattern for the `as` expression in a conditional block. */
  26941. const CONDITIONAL_ALIAS_PATTERN = /^(as\s+)(.*)/;
  26942. /** Pattern used to identify an `else if` block. */
  26943. const ELSE_IF_PATTERN = /^else[^\S\r\n]+if/;
  26944. /** Pattern used to identify a `let` parameter. */
  26945. const FOR_LOOP_LET_PATTERN = /^let\s+([\S\s]*)/;
  26946. /** Pattern used to validate a JavaScript identifier. */
  26947. const IDENTIFIER_PATTERN = /^[$A-Z_][0-9A-Z_$]*$/i;
  26948. /**
  26949. * Pattern to group a string into leading whitespace, non whitespace, and trailing whitespace.
  26950. * Useful for getting the variable name span when a span can contain leading and trailing space.
  26951. */
  26952. const CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN = /(\s*)(\S+)(\s*)/;
  26953. /** Names of variables that are allowed to be used in the `let` expression of a `for` loop. */
  26954. const ALLOWED_FOR_LOOP_LET_VARIABLES = new Set([
  26955. '$index',
  26956. '$first',
  26957. '$last',
  26958. '$even',
  26959. '$odd',
  26960. '$count',
  26961. ]);
  26962. /**
  26963. * Predicate function that determines if a block with
  26964. * a specific name cam be connected to a `for` block.
  26965. */
  26966. function isConnectedForLoopBlock(name) {
  26967. return name === 'empty';
  26968. }
  26969. /**
  26970. * Predicate function that determines if a block with
  26971. * a specific name cam be connected to an `if` block.
  26972. */
  26973. function isConnectedIfLoopBlock(name) {
  26974. return name === 'else' || ELSE_IF_PATTERN.test(name);
  26975. }
  26976. /** Creates an `if` loop block from an HTML AST node. */
  26977. function createIfBlock(ast, connectedBlocks, visitor, bindingParser) {
  26978. const errors = validateIfConnectedBlocks(connectedBlocks);
  26979. const branches = [];
  26980. const mainBlockParams = parseConditionalBlockParameters(ast, errors, bindingParser);
  26981. if (mainBlockParams !== null) {
  26982. branches.push(new IfBlockBranch(mainBlockParams.expression, visitAll(visitor, ast.children, ast.children), mainBlockParams.expressionAlias, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.nameSpan, ast.i18n));
  26983. }
  26984. for (const block of connectedBlocks) {
  26985. if (ELSE_IF_PATTERN.test(block.name)) {
  26986. const params = parseConditionalBlockParameters(block, errors, bindingParser);
  26987. if (params !== null) {
  26988. const children = visitAll(visitor, block.children, block.children);
  26989. branches.push(new IfBlockBranch(params.expression, children, params.expressionAlias, block.sourceSpan, block.startSourceSpan, block.endSourceSpan, block.nameSpan, block.i18n));
  26990. }
  26991. }
  26992. else if (block.name === 'else') {
  26993. const children = visitAll(visitor, block.children, block.children);
  26994. branches.push(new IfBlockBranch(null, children, null, block.sourceSpan, block.startSourceSpan, block.endSourceSpan, block.nameSpan, block.i18n));
  26995. }
  26996. }
  26997. // The outer IfBlock should have a span that encapsulates all branches.
  26998. const ifBlockStartSourceSpan = branches.length > 0 ? branches[0].startSourceSpan : ast.startSourceSpan;
  26999. const ifBlockEndSourceSpan = branches.length > 0 ? branches[branches.length - 1].endSourceSpan : ast.endSourceSpan;
  27000. let wholeSourceSpan = ast.sourceSpan;
  27001. const lastBranch = branches[branches.length - 1];
  27002. if (lastBranch !== undefined) {
  27003. wholeSourceSpan = new ParseSourceSpan(ifBlockStartSourceSpan.start, lastBranch.sourceSpan.end);
  27004. }
  27005. return {
  27006. node: new IfBlock(branches, wholeSourceSpan, ast.startSourceSpan, ifBlockEndSourceSpan, ast.nameSpan),
  27007. errors,
  27008. };
  27009. }
  27010. /** Creates a `for` loop block from an HTML AST node. */
  27011. function createForLoop(ast, connectedBlocks, visitor, bindingParser) {
  27012. const errors = [];
  27013. const params = parseForLoopParameters(ast, errors, bindingParser);
  27014. let node = null;
  27015. let empty = null;
  27016. for (const block of connectedBlocks) {
  27017. if (block.name === 'empty') {
  27018. if (empty !== null) {
  27019. errors.push(new ParseError(block.sourceSpan, '@for loop can only have one @empty block'));
  27020. }
  27021. else if (block.parameters.length > 0) {
  27022. errors.push(new ParseError(block.sourceSpan, '@empty block cannot have parameters'));
  27023. }
  27024. else {
  27025. empty = new ForLoopBlockEmpty(visitAll(visitor, block.children, block.children), block.sourceSpan, block.startSourceSpan, block.endSourceSpan, block.nameSpan, block.i18n);
  27026. }
  27027. }
  27028. else {
  27029. errors.push(new ParseError(block.sourceSpan, `Unrecognized @for loop block "${block.name}"`));
  27030. }
  27031. }
  27032. if (params !== null) {
  27033. if (params.trackBy === null) {
  27034. // TODO: We should not fail here, and instead try to produce some AST for the language
  27035. // service.
  27036. errors.push(new ParseError(ast.startSourceSpan, '@for loop must have a "track" expression'));
  27037. }
  27038. else {
  27039. // The `for` block has a main span that includes the `empty` branch. For only the span of the
  27040. // main `for` body, use `mainSourceSpan`.
  27041. const endSpan = empty?.endSourceSpan ?? ast.endSourceSpan;
  27042. const sourceSpan = new ParseSourceSpan(ast.sourceSpan.start, endSpan?.end ?? ast.sourceSpan.end);
  27043. node = new ForLoopBlock(params.itemName, params.expression, params.trackBy.expression, params.trackBy.keywordSpan, params.context, visitAll(visitor, ast.children, ast.children), empty, sourceSpan, ast.sourceSpan, ast.startSourceSpan, endSpan, ast.nameSpan, ast.i18n);
  27044. }
  27045. }
  27046. return { node, errors };
  27047. }
  27048. /** Creates a switch block from an HTML AST node. */
  27049. function createSwitchBlock(ast, visitor, bindingParser) {
  27050. const errors = validateSwitchBlock(ast);
  27051. const primaryExpression = ast.parameters.length > 0
  27052. ? parseBlockParameterToBinding(ast.parameters[0], bindingParser)
  27053. : bindingParser.parseBinding('', false, ast.sourceSpan, 0);
  27054. const cases = [];
  27055. const unknownBlocks = [];
  27056. let defaultCase = null;
  27057. // Here we assume that all the blocks are valid given that we validated them above.
  27058. for (const node of ast.children) {
  27059. if (!(node instanceof Block)) {
  27060. continue;
  27061. }
  27062. if ((node.name !== 'case' || node.parameters.length === 0) && node.name !== 'default') {
  27063. unknownBlocks.push(new UnknownBlock(node.name, node.sourceSpan, node.nameSpan));
  27064. continue;
  27065. }
  27066. const expression = node.name === 'case' ? parseBlockParameterToBinding(node.parameters[0], bindingParser) : null;
  27067. const ast = new SwitchBlockCase(expression, visitAll(visitor, node.children, node.children), node.sourceSpan, node.startSourceSpan, node.endSourceSpan, node.nameSpan, node.i18n);
  27068. if (expression === null) {
  27069. defaultCase = ast;
  27070. }
  27071. else {
  27072. cases.push(ast);
  27073. }
  27074. }
  27075. // Ensure that the default case is last in the array.
  27076. if (defaultCase !== null) {
  27077. cases.push(defaultCase);
  27078. }
  27079. return {
  27080. node: new SwitchBlock(primaryExpression, cases, unknownBlocks, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.nameSpan),
  27081. errors,
  27082. };
  27083. }
  27084. /** Parses the parameters of a `for` loop block. */
  27085. function parseForLoopParameters(block, errors, bindingParser) {
  27086. if (block.parameters.length === 0) {
  27087. errors.push(new ParseError(block.startSourceSpan, '@for loop does not have an expression'));
  27088. return null;
  27089. }
  27090. const [expressionParam, ...secondaryParams] = block.parameters;
  27091. const match = stripOptionalParentheses(expressionParam, errors)?.match(FOR_LOOP_EXPRESSION_PATTERN);
  27092. if (!match || match[2].trim().length === 0) {
  27093. errors.push(new ParseError(expressionParam.sourceSpan, 'Cannot parse expression. @for loop expression must match the pattern "<identifier> of <expression>"'));
  27094. return null;
  27095. }
  27096. const [, itemName, rawExpression] = match;
  27097. if (ALLOWED_FOR_LOOP_LET_VARIABLES.has(itemName)) {
  27098. errors.push(new ParseError(expressionParam.sourceSpan, `@for loop item name cannot be one of ${Array.from(ALLOWED_FOR_LOOP_LET_VARIABLES).join(', ')}.`));
  27099. }
  27100. // `expressionParam.expression` contains the variable declaration and the expression of the
  27101. // for...of statement, i.e. 'user of users' The variable of a ForOfStatement is _only_ the "const
  27102. // user" part and does not include "of x".
  27103. const variableName = expressionParam.expression.split(' ')[0];
  27104. const variableSpan = new ParseSourceSpan(expressionParam.sourceSpan.start, expressionParam.sourceSpan.start.moveBy(variableName.length));
  27105. const result = {
  27106. itemName: new Variable(itemName, '$implicit', variableSpan, variableSpan),
  27107. trackBy: null,
  27108. expression: parseBlockParameterToBinding(expressionParam, bindingParser, rawExpression),
  27109. context: Array.from(ALLOWED_FOR_LOOP_LET_VARIABLES, (variableName) => {
  27110. // Give ambiently-available context variables empty spans at the end of
  27111. // the start of the `for` block, since they are not explicitly defined.
  27112. const emptySpanAfterForBlockStart = new ParseSourceSpan(block.startSourceSpan.end, block.startSourceSpan.end);
  27113. return new Variable(variableName, variableName, emptySpanAfterForBlockStart, emptySpanAfterForBlockStart);
  27114. }),
  27115. };
  27116. for (const param of secondaryParams) {
  27117. const letMatch = param.expression.match(FOR_LOOP_LET_PATTERN);
  27118. if (letMatch !== null) {
  27119. const variablesSpan = new ParseSourceSpan(param.sourceSpan.start.moveBy(letMatch[0].length - letMatch[1].length), param.sourceSpan.end);
  27120. parseLetParameter(param.sourceSpan, letMatch[1], variablesSpan, itemName, result.context, errors);
  27121. continue;
  27122. }
  27123. const trackMatch = param.expression.match(FOR_LOOP_TRACK_PATTERN);
  27124. if (trackMatch !== null) {
  27125. if (result.trackBy !== null) {
  27126. errors.push(new ParseError(param.sourceSpan, '@for loop can only have one "track" expression'));
  27127. }
  27128. else {
  27129. const expression = parseBlockParameterToBinding(param, bindingParser, trackMatch[1]);
  27130. if (expression.ast instanceof EmptyExpr$1) {
  27131. errors.push(new ParseError(block.startSourceSpan, '@for loop must have a "track" expression'));
  27132. }
  27133. const keywordSpan = new ParseSourceSpan(param.sourceSpan.start, param.sourceSpan.start.moveBy('track'.length));
  27134. result.trackBy = { expression, keywordSpan };
  27135. }
  27136. continue;
  27137. }
  27138. errors.push(new ParseError(param.sourceSpan, `Unrecognized @for loop parameter "${param.expression}"`));
  27139. }
  27140. return result;
  27141. }
  27142. /** Parses the `let` parameter of a `for` loop block. */
  27143. function parseLetParameter(sourceSpan, expression, span, loopItemName, context, errors) {
  27144. const parts = expression.split(',');
  27145. let startSpan = span.start;
  27146. for (const part of parts) {
  27147. const expressionParts = part.split('=');
  27148. const name = expressionParts.length === 2 ? expressionParts[0].trim() : '';
  27149. const variableName = expressionParts.length === 2 ? expressionParts[1].trim() : '';
  27150. if (name.length === 0 || variableName.length === 0) {
  27151. errors.push(new ParseError(sourceSpan, `Invalid @for loop "let" parameter. Parameter should match the pattern "<name> = <variable name>"`));
  27152. }
  27153. else if (!ALLOWED_FOR_LOOP_LET_VARIABLES.has(variableName)) {
  27154. errors.push(new ParseError(sourceSpan, `Unknown "let" parameter variable "${variableName}". The allowed variables are: ${Array.from(ALLOWED_FOR_LOOP_LET_VARIABLES).join(', ')}`));
  27155. }
  27156. else if (name === loopItemName) {
  27157. errors.push(new ParseError(sourceSpan, `Invalid @for loop "let" parameter. Variable cannot be called "${loopItemName}"`));
  27158. }
  27159. else if (context.some((v) => v.name === name)) {
  27160. errors.push(new ParseError(sourceSpan, `Duplicate "let" parameter variable "${variableName}"`));
  27161. }
  27162. else {
  27163. const [, keyLeadingWhitespace, keyName] = expressionParts[0].match(CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN) ?? [];
  27164. const keySpan = keyLeadingWhitespace !== undefined && expressionParts.length === 2
  27165. ? new ParseSourceSpan(
  27166. /* strip leading spaces */
  27167. startSpan.moveBy(keyLeadingWhitespace.length),
  27168. /* advance to end of the variable name */
  27169. startSpan.moveBy(keyLeadingWhitespace.length + keyName.length))
  27170. : span;
  27171. let valueSpan = undefined;
  27172. if (expressionParts.length === 2) {
  27173. const [, valueLeadingWhitespace, implicit] = expressionParts[1].match(CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN) ?? [];
  27174. valueSpan =
  27175. valueLeadingWhitespace !== undefined
  27176. ? new ParseSourceSpan(startSpan.moveBy(expressionParts[0].length + 1 + valueLeadingWhitespace.length), startSpan.moveBy(expressionParts[0].length + 1 + valueLeadingWhitespace.length + implicit.length))
  27177. : undefined;
  27178. }
  27179. const sourceSpan = new ParseSourceSpan(keySpan.start, valueSpan?.end ?? keySpan.end);
  27180. context.push(new Variable(name, variableName, sourceSpan, keySpan, valueSpan));
  27181. }
  27182. startSpan = startSpan.moveBy(part.length + 1 /* add 1 to move past the comma */);
  27183. }
  27184. }
  27185. /**
  27186. * Checks that the shape of the blocks connected to an
  27187. * `@if` block is correct. Returns an array of errors.
  27188. */
  27189. function validateIfConnectedBlocks(connectedBlocks) {
  27190. const errors = [];
  27191. let hasElse = false;
  27192. for (let i = 0; i < connectedBlocks.length; i++) {
  27193. const block = connectedBlocks[i];
  27194. if (block.name === 'else') {
  27195. if (hasElse) {
  27196. errors.push(new ParseError(block.startSourceSpan, 'Conditional can only have one @else block'));
  27197. }
  27198. else if (connectedBlocks.length > 1 && i < connectedBlocks.length - 1) {
  27199. errors.push(new ParseError(block.startSourceSpan, '@else block must be last inside the conditional'));
  27200. }
  27201. else if (block.parameters.length > 0) {
  27202. errors.push(new ParseError(block.startSourceSpan, '@else block cannot have parameters'));
  27203. }
  27204. hasElse = true;
  27205. }
  27206. else if (!ELSE_IF_PATTERN.test(block.name)) {
  27207. errors.push(new ParseError(block.startSourceSpan, `Unrecognized conditional block @${block.name}`));
  27208. }
  27209. }
  27210. return errors;
  27211. }
  27212. /** Checks that the shape of a `switch` block is valid. Returns an array of errors. */
  27213. function validateSwitchBlock(ast) {
  27214. const errors = [];
  27215. let hasDefault = false;
  27216. if (ast.parameters.length !== 1) {
  27217. errors.push(new ParseError(ast.startSourceSpan, '@switch block must have exactly one parameter'));
  27218. return errors;
  27219. }
  27220. for (const node of ast.children) {
  27221. // Skip over comments and empty text nodes inside the switch block.
  27222. // Empty text nodes can be used for formatting while comments don't affect the runtime.
  27223. if (node instanceof Comment ||
  27224. (node instanceof Text && node.value.trim().length === 0)) {
  27225. continue;
  27226. }
  27227. if (!(node instanceof Block) || (node.name !== 'case' && node.name !== 'default')) {
  27228. errors.push(new ParseError(node.sourceSpan, '@switch block can only contain @case and @default blocks'));
  27229. continue;
  27230. }
  27231. if (node.name === 'default') {
  27232. if (hasDefault) {
  27233. errors.push(new ParseError(node.startSourceSpan, '@switch block can only have one @default block'));
  27234. }
  27235. else if (node.parameters.length > 0) {
  27236. errors.push(new ParseError(node.startSourceSpan, '@default block cannot have parameters'));
  27237. }
  27238. hasDefault = true;
  27239. }
  27240. else if (node.name === 'case' && node.parameters.length !== 1) {
  27241. errors.push(new ParseError(node.startSourceSpan, '@case block must have exactly one parameter'));
  27242. }
  27243. }
  27244. return errors;
  27245. }
  27246. /**
  27247. * Parses a block parameter into a binding AST.
  27248. * @param ast Block parameter that should be parsed.
  27249. * @param bindingParser Parser that the expression should be parsed with.
  27250. * @param part Specific part of the expression that should be parsed.
  27251. */
  27252. function parseBlockParameterToBinding(ast, bindingParser, part) {
  27253. let start;
  27254. let end;
  27255. if (typeof part === 'string') {
  27256. // Note: `lastIndexOf` here should be enough to know the start index of the expression,
  27257. // because we know that it'll be at the end of the param. Ideally we could use the `d`
  27258. // flag when matching via regex and get the index from `match.indices`, but it's unclear
  27259. // if we can use it yet since it's a relatively new feature. See:
  27260. // https://github.com/tc39/proposal-regexp-match-indices
  27261. start = Math.max(0, ast.expression.lastIndexOf(part));
  27262. end = start + part.length;
  27263. }
  27264. else {
  27265. start = 0;
  27266. end = ast.expression.length;
  27267. }
  27268. return bindingParser.parseBinding(ast.expression.slice(start, end), false, ast.sourceSpan, ast.sourceSpan.start.offset + start);
  27269. }
  27270. /** Parses the parameter of a conditional block (`if` or `else if`). */
  27271. function parseConditionalBlockParameters(block, errors, bindingParser) {
  27272. if (block.parameters.length === 0) {
  27273. errors.push(new ParseError(block.startSourceSpan, 'Conditional block does not have an expression'));
  27274. return null;
  27275. }
  27276. const expression = parseBlockParameterToBinding(block.parameters[0], bindingParser);
  27277. let expressionAlias = null;
  27278. // Start from 1 since we processed the first parameter already.
  27279. for (let i = 1; i < block.parameters.length; i++) {
  27280. const param = block.parameters[i];
  27281. const aliasMatch = param.expression.match(CONDITIONAL_ALIAS_PATTERN);
  27282. // For now conditionals can only have an `as` parameter.
  27283. // We may want to rework this later if we add more.
  27284. if (aliasMatch === null) {
  27285. errors.push(new ParseError(param.sourceSpan, `Unrecognized conditional parameter "${param.expression}"`));
  27286. }
  27287. else if (block.name !== 'if') {
  27288. errors.push(new ParseError(param.sourceSpan, '"as" expression is only allowed on the primary @if block'));
  27289. }
  27290. else if (expressionAlias !== null) {
  27291. errors.push(new ParseError(param.sourceSpan, 'Conditional can only have one "as" expression'));
  27292. }
  27293. else {
  27294. const name = aliasMatch[2].trim();
  27295. if (IDENTIFIER_PATTERN.test(name)) {
  27296. const variableStart = param.sourceSpan.start.moveBy(aliasMatch[1].length);
  27297. const variableSpan = new ParseSourceSpan(variableStart, variableStart.moveBy(name.length));
  27298. expressionAlias = new Variable(name, name, variableSpan, variableSpan);
  27299. }
  27300. else {
  27301. errors.push(new ParseError(param.sourceSpan, '"as" expression must be a valid JavaScript identifier'));
  27302. }
  27303. }
  27304. }
  27305. return { expression, expressionAlias };
  27306. }
  27307. /** Strips optional parentheses around from a control from expression parameter. */
  27308. function stripOptionalParentheses(param, errors) {
  27309. const expression = param.expression;
  27310. const spaceRegex = /^\s$/;
  27311. let openParens = 0;
  27312. let start = 0;
  27313. let end = expression.length - 1;
  27314. for (let i = 0; i < expression.length; i++) {
  27315. const char = expression[i];
  27316. if (char === '(') {
  27317. start = i + 1;
  27318. openParens++;
  27319. }
  27320. else if (spaceRegex.test(char)) {
  27321. continue;
  27322. }
  27323. else {
  27324. break;
  27325. }
  27326. }
  27327. if (openParens === 0) {
  27328. return expression;
  27329. }
  27330. for (let i = expression.length - 1; i > -1; i--) {
  27331. const char = expression[i];
  27332. if (char === ')') {
  27333. end = i;
  27334. openParens--;
  27335. if (openParens === 0) {
  27336. break;
  27337. }
  27338. }
  27339. else if (spaceRegex.test(char)) {
  27340. continue;
  27341. }
  27342. else {
  27343. break;
  27344. }
  27345. }
  27346. if (openParens !== 0) {
  27347. errors.push(new ParseError(param.sourceSpan, 'Unclosed parentheses in expression'));
  27348. return null;
  27349. }
  27350. return expression.slice(start, end);
  27351. }
  27352. /** Pattern for a timing value in a trigger. */
  27353. const TIME_PATTERN = /^\d+\.?\d*(ms|s)?$/;
  27354. /** Pattern for a separator between keywords in a trigger expression. */
  27355. const SEPARATOR_PATTERN = /^\s$/;
  27356. /** Pairs of characters that form syntax that is comma-delimited. */
  27357. const COMMA_DELIMITED_SYNTAX = new Map([
  27358. [$LBRACE, $RBRACE], // Object literals
  27359. [$LBRACKET, $RBRACKET], // Array literals
  27360. [$LPAREN, $RPAREN], // Function calls
  27361. ]);
  27362. /** Possible types of `on` triggers. */
  27363. var OnTriggerType;
  27364. (function (OnTriggerType) {
  27365. OnTriggerType["IDLE"] = "idle";
  27366. OnTriggerType["TIMER"] = "timer";
  27367. OnTriggerType["INTERACTION"] = "interaction";
  27368. OnTriggerType["IMMEDIATE"] = "immediate";
  27369. OnTriggerType["HOVER"] = "hover";
  27370. OnTriggerType["VIEWPORT"] = "viewport";
  27371. OnTriggerType["NEVER"] = "never";
  27372. })(OnTriggerType || (OnTriggerType = {}));
  27373. /** Parses a `when` deferred trigger. */
  27374. function parseNeverTrigger({ expression, sourceSpan }, triggers, errors) {
  27375. const neverIndex = expression.indexOf('never');
  27376. const neverSourceSpan = new ParseSourceSpan(sourceSpan.start.moveBy(neverIndex), sourceSpan.start.moveBy(neverIndex + 'never'.length));
  27377. const prefetchSpan = getPrefetchSpan(expression, sourceSpan);
  27378. const hydrateSpan = getHydrateSpan(expression, sourceSpan);
  27379. // This is here just to be safe, we shouldn't enter this function
  27380. // in the first place if a block doesn't have the "on" keyword.
  27381. if (neverIndex === -1) {
  27382. errors.push(new ParseError(sourceSpan, `Could not find "never" keyword in expression`));
  27383. }
  27384. else {
  27385. trackTrigger('never', triggers, errors, new NeverDeferredTrigger(neverSourceSpan, sourceSpan, prefetchSpan, null, hydrateSpan));
  27386. }
  27387. }
  27388. /** Parses a `when` deferred trigger. */
  27389. function parseWhenTrigger({ expression, sourceSpan }, bindingParser, triggers, errors) {
  27390. const whenIndex = expression.indexOf('when');
  27391. const whenSourceSpan = new ParseSourceSpan(sourceSpan.start.moveBy(whenIndex), sourceSpan.start.moveBy(whenIndex + 'when'.length));
  27392. const prefetchSpan = getPrefetchSpan(expression, sourceSpan);
  27393. const hydrateSpan = getHydrateSpan(expression, sourceSpan);
  27394. // This is here just to be safe, we shouldn't enter this function
  27395. // in the first place if a block doesn't have the "when" keyword.
  27396. if (whenIndex === -1) {
  27397. errors.push(new ParseError(sourceSpan, `Could not find "when" keyword in expression`));
  27398. }
  27399. else {
  27400. const start = getTriggerParametersStart(expression, whenIndex + 1);
  27401. const parsed = bindingParser.parseBinding(expression.slice(start), false, sourceSpan, sourceSpan.start.offset + start);
  27402. trackTrigger('when', triggers, errors, new BoundDeferredTrigger(parsed, sourceSpan, prefetchSpan, whenSourceSpan, hydrateSpan));
  27403. }
  27404. }
  27405. /** Parses an `on` trigger */
  27406. function parseOnTrigger({ expression, sourceSpan }, triggers, errors, placeholder) {
  27407. const onIndex = expression.indexOf('on');
  27408. const onSourceSpan = new ParseSourceSpan(sourceSpan.start.moveBy(onIndex), sourceSpan.start.moveBy(onIndex + 'on'.length));
  27409. const prefetchSpan = getPrefetchSpan(expression, sourceSpan);
  27410. const hydrateSpan = getHydrateSpan(expression, sourceSpan);
  27411. // This is here just to be safe, we shouldn't enter this function
  27412. // in the first place if a block doesn't have the "on" keyword.
  27413. if (onIndex === -1) {
  27414. errors.push(new ParseError(sourceSpan, `Could not find "on" keyword in expression`));
  27415. }
  27416. else {
  27417. const start = getTriggerParametersStart(expression, onIndex + 1);
  27418. const parser = new OnTriggerParser(expression, start, sourceSpan, triggers, errors, expression.startsWith('hydrate')
  27419. ? validateHydrateReferenceBasedTrigger
  27420. : validatePlainReferenceBasedTrigger, placeholder, prefetchSpan, onSourceSpan, hydrateSpan);
  27421. parser.parse();
  27422. }
  27423. }
  27424. function getPrefetchSpan(expression, sourceSpan) {
  27425. if (!expression.startsWith('prefetch')) {
  27426. return null;
  27427. }
  27428. return new ParseSourceSpan(sourceSpan.start, sourceSpan.start.moveBy('prefetch'.length));
  27429. }
  27430. function getHydrateSpan(expression, sourceSpan) {
  27431. if (!expression.startsWith('hydrate')) {
  27432. return null;
  27433. }
  27434. return new ParseSourceSpan(sourceSpan.start, sourceSpan.start.moveBy('hydrate'.length));
  27435. }
  27436. class OnTriggerParser {
  27437. expression;
  27438. start;
  27439. span;
  27440. triggers;
  27441. errors;
  27442. validator;
  27443. placeholder;
  27444. prefetchSpan;
  27445. onSourceSpan;
  27446. hydrateSpan;
  27447. index = 0;
  27448. tokens;
  27449. constructor(expression, start, span, triggers, errors, validator, placeholder, prefetchSpan, onSourceSpan, hydrateSpan) {
  27450. this.expression = expression;
  27451. this.start = start;
  27452. this.span = span;
  27453. this.triggers = triggers;
  27454. this.errors = errors;
  27455. this.validator = validator;
  27456. this.placeholder = placeholder;
  27457. this.prefetchSpan = prefetchSpan;
  27458. this.onSourceSpan = onSourceSpan;
  27459. this.hydrateSpan = hydrateSpan;
  27460. this.tokens = new Lexer().tokenize(expression.slice(start));
  27461. }
  27462. parse() {
  27463. while (this.tokens.length > 0 && this.index < this.tokens.length) {
  27464. const token = this.token();
  27465. if (!token.isIdentifier()) {
  27466. this.unexpectedToken(token);
  27467. break;
  27468. }
  27469. // An identifier immediately followed by a comma or the end of
  27470. // the expression cannot have parameters so we can exit early.
  27471. if (this.isFollowedByOrLast($COMMA)) {
  27472. this.consumeTrigger(token, []);
  27473. this.advance();
  27474. }
  27475. else if (this.isFollowedByOrLast($LPAREN)) {
  27476. this.advance(); // Advance to the opening paren.
  27477. const prevErrors = this.errors.length;
  27478. const parameters = this.consumeParameters();
  27479. if (this.errors.length !== prevErrors) {
  27480. break;
  27481. }
  27482. this.consumeTrigger(token, parameters);
  27483. this.advance(); // Advance past the closing paren.
  27484. }
  27485. else if (this.index < this.tokens.length - 1) {
  27486. this.unexpectedToken(this.tokens[this.index + 1]);
  27487. }
  27488. this.advance();
  27489. }
  27490. }
  27491. advance() {
  27492. this.index++;
  27493. }
  27494. isFollowedByOrLast(char) {
  27495. if (this.index === this.tokens.length - 1) {
  27496. return true;
  27497. }
  27498. return this.tokens[this.index + 1].isCharacter(char);
  27499. }
  27500. token() {
  27501. return this.tokens[Math.min(this.index, this.tokens.length - 1)];
  27502. }
  27503. consumeTrigger(identifier, parameters) {
  27504. const triggerNameStartSpan = this.span.start.moveBy(this.start + identifier.index - this.tokens[0].index);
  27505. const nameSpan = new ParseSourceSpan(triggerNameStartSpan, triggerNameStartSpan.moveBy(identifier.strValue.length));
  27506. const endSpan = triggerNameStartSpan.moveBy(this.token().end - identifier.index);
  27507. // Put the prefetch and on spans with the first trigger
  27508. // This should maybe be refactored to have something like an outer OnGroup AST
  27509. // Since triggers can be grouped with commas "on hover(x), interaction(y)"
  27510. const isFirstTrigger = identifier.index === 0;
  27511. const onSourceSpan = isFirstTrigger ? this.onSourceSpan : null;
  27512. const prefetchSourceSpan = isFirstTrigger ? this.prefetchSpan : null;
  27513. const hydrateSourceSpan = isFirstTrigger ? this.hydrateSpan : null;
  27514. const sourceSpan = new ParseSourceSpan(isFirstTrigger ? this.span.start : triggerNameStartSpan, endSpan);
  27515. try {
  27516. switch (identifier.toString()) {
  27517. case OnTriggerType.IDLE:
  27518. this.trackTrigger('idle', createIdleTrigger(parameters, nameSpan, sourceSpan, prefetchSourceSpan, onSourceSpan, hydrateSourceSpan));
  27519. break;
  27520. case OnTriggerType.TIMER:
  27521. this.trackTrigger('timer', createTimerTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan));
  27522. break;
  27523. case OnTriggerType.INTERACTION:
  27524. this.trackTrigger('interaction', createInteractionTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan, this.placeholder, this.validator));
  27525. break;
  27526. case OnTriggerType.IMMEDIATE:
  27527. this.trackTrigger('immediate', createImmediateTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan));
  27528. break;
  27529. case OnTriggerType.HOVER:
  27530. this.trackTrigger('hover', createHoverTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan, this.placeholder, this.validator));
  27531. break;
  27532. case OnTriggerType.VIEWPORT:
  27533. this.trackTrigger('viewport', createViewportTrigger(parameters, nameSpan, sourceSpan, this.prefetchSpan, this.onSourceSpan, this.hydrateSpan, this.placeholder, this.validator));
  27534. break;
  27535. default:
  27536. throw new Error(`Unrecognized trigger type "${identifier}"`);
  27537. }
  27538. }
  27539. catch (e) {
  27540. this.error(identifier, e.message);
  27541. }
  27542. }
  27543. consumeParameters() {
  27544. const parameters = [];
  27545. if (!this.token().isCharacter($LPAREN)) {
  27546. this.unexpectedToken(this.token());
  27547. return parameters;
  27548. }
  27549. this.advance();
  27550. const commaDelimStack = [];
  27551. let current = '';
  27552. while (this.index < this.tokens.length) {
  27553. const token = this.token();
  27554. // Stop parsing if we've hit the end character and we're outside of a comma-delimited syntax.
  27555. // Note that we don't need to account for strings here since the lexer already parsed them
  27556. // into string tokens.
  27557. if (token.isCharacter($RPAREN) && commaDelimStack.length === 0) {
  27558. if (current.length) {
  27559. parameters.push(current);
  27560. }
  27561. break;
  27562. }
  27563. // In the `on` microsyntax "top-level" commas (e.g. ones outside of an parameters) separate
  27564. // the different triggers (e.g. `on idle,timer(500)`). This is problematic, because the
  27565. // function-like syntax also implies that multiple parameters can be passed into the
  27566. // individual trigger (e.g. `on foo(a, b)`). To avoid tripping up the parser with commas that
  27567. // are part of other sorts of syntax (object literals, arrays), we treat anything inside
  27568. // a comma-delimited syntax block as plain text.
  27569. if (token.type === TokenType.Character && COMMA_DELIMITED_SYNTAX.has(token.numValue)) {
  27570. commaDelimStack.push(COMMA_DELIMITED_SYNTAX.get(token.numValue));
  27571. }
  27572. if (commaDelimStack.length > 0 &&
  27573. token.isCharacter(commaDelimStack[commaDelimStack.length - 1])) {
  27574. commaDelimStack.pop();
  27575. }
  27576. // If we hit a comma outside of a comma-delimited syntax, it means
  27577. // that we're at the top level and we're starting a new parameter.
  27578. if (commaDelimStack.length === 0 && token.isCharacter($COMMA) && current.length > 0) {
  27579. parameters.push(current);
  27580. current = '';
  27581. this.advance();
  27582. continue;
  27583. }
  27584. // Otherwise treat the token as a plain text character in the current parameter.
  27585. current += this.tokenText();
  27586. this.advance();
  27587. }
  27588. if (!this.token().isCharacter($RPAREN) || commaDelimStack.length > 0) {
  27589. this.error(this.token(), 'Unexpected end of expression');
  27590. }
  27591. if (this.index < this.tokens.length - 1 &&
  27592. !this.tokens[this.index + 1].isCharacter($COMMA)) {
  27593. this.unexpectedToken(this.tokens[this.index + 1]);
  27594. }
  27595. return parameters;
  27596. }
  27597. tokenText() {
  27598. // Tokens have a toString already which we could use, but for string tokens it omits the quotes.
  27599. // Eventually we could expose this information on the token directly.
  27600. return this.expression.slice(this.start + this.token().index, this.start + this.token().end);
  27601. }
  27602. trackTrigger(name, trigger) {
  27603. trackTrigger(name, this.triggers, this.errors, trigger);
  27604. }
  27605. error(token, message) {
  27606. const newStart = this.span.start.moveBy(this.start + token.index);
  27607. const newEnd = newStart.moveBy(token.end - token.index);
  27608. this.errors.push(new ParseError(new ParseSourceSpan(newStart, newEnd), message));
  27609. }
  27610. unexpectedToken(token) {
  27611. this.error(token, `Unexpected token "${token}"`);
  27612. }
  27613. }
  27614. /** Adds a trigger to a map of triggers. */
  27615. function trackTrigger(name, allTriggers, errors, trigger) {
  27616. if (allTriggers[name]) {
  27617. errors.push(new ParseError(trigger.sourceSpan, `Duplicate "${name}" trigger is not allowed`));
  27618. }
  27619. else {
  27620. allTriggers[name] = trigger;
  27621. }
  27622. }
  27623. function createIdleTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  27624. if (parameters.length > 0) {
  27625. throw new Error(`"${OnTriggerType.IDLE}" trigger cannot have parameters`);
  27626. }
  27627. return new IdleDeferredTrigger(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27628. }
  27629. function createTimerTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  27630. if (parameters.length !== 1) {
  27631. throw new Error(`"${OnTriggerType.TIMER}" trigger must have exactly one parameter`);
  27632. }
  27633. const delay = parseDeferredTime(parameters[0]);
  27634. if (delay === null) {
  27635. throw new Error(`Could not parse time value of trigger "${OnTriggerType.TIMER}"`);
  27636. }
  27637. return new TimerDeferredTrigger(delay, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27638. }
  27639. function createImmediateTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan) {
  27640. if (parameters.length > 0) {
  27641. throw new Error(`"${OnTriggerType.IMMEDIATE}" trigger cannot have parameters`);
  27642. }
  27643. return new ImmediateDeferredTrigger(nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27644. }
  27645. function createHoverTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan, placeholder, validator) {
  27646. validator(OnTriggerType.HOVER, parameters, placeholder);
  27647. return new HoverDeferredTrigger(parameters[0] ?? null, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27648. }
  27649. function createInteractionTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan, placeholder, validator) {
  27650. validator(OnTriggerType.INTERACTION, parameters, placeholder);
  27651. return new InteractionDeferredTrigger(parameters[0] ?? null, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27652. }
  27653. function createViewportTrigger(parameters, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan, placeholder, validator) {
  27654. validator(OnTriggerType.VIEWPORT, parameters, placeholder);
  27655. return new ViewportDeferredTrigger(parameters[0] ?? null, nameSpan, sourceSpan, prefetchSpan, onSourceSpan, hydrateSpan);
  27656. }
  27657. /**
  27658. * Checks whether the structure of a non-hydrate reference-based trigger is valid.
  27659. * @param type Type of the trigger being validated.
  27660. * @param parameters Parameters of the trigger.
  27661. * @param placeholder Placeholder of the defer block.
  27662. */
  27663. function validatePlainReferenceBasedTrigger(type, parameters, placeholder) {
  27664. if (parameters.length > 1) {
  27665. throw new Error(`"${type}" trigger can only have zero or one parameters`);
  27666. }
  27667. if (parameters.length === 0) {
  27668. if (placeholder === null) {
  27669. throw new Error(`"${type}" trigger with no parameters can only be placed on an @defer that has a @placeholder block`);
  27670. }
  27671. if (placeholder.children.length !== 1 || !(placeholder.children[0] instanceof Element$1)) {
  27672. throw new Error(`"${type}" trigger with no parameters can only be placed on an @defer that has a ` +
  27673. `@placeholder block with exactly one root element node`);
  27674. }
  27675. }
  27676. }
  27677. /**
  27678. * Checks whether the structure of a hydrate trigger is valid.
  27679. * @param type Type of the trigger being validated.
  27680. * @param parameters Parameters of the trigger.
  27681. */
  27682. function validateHydrateReferenceBasedTrigger(type, parameters) {
  27683. if (parameters.length > 0) {
  27684. throw new Error(`Hydration trigger "${type}" cannot have parameters`);
  27685. }
  27686. }
  27687. /** Gets the index within an expression at which the trigger parameters start. */
  27688. function getTriggerParametersStart(value, startPosition = 0) {
  27689. let hasFoundSeparator = false;
  27690. for (let i = startPosition; i < value.length; i++) {
  27691. if (SEPARATOR_PATTERN.test(value[i])) {
  27692. hasFoundSeparator = true;
  27693. }
  27694. else if (hasFoundSeparator) {
  27695. return i;
  27696. }
  27697. }
  27698. return -1;
  27699. }
  27700. /**
  27701. * Parses a time expression from a deferred trigger to
  27702. * milliseconds. Returns null if it cannot be parsed.
  27703. */
  27704. function parseDeferredTime(value) {
  27705. const match = value.match(TIME_PATTERN);
  27706. if (!match) {
  27707. return null;
  27708. }
  27709. const [time, units] = match;
  27710. return parseFloat(time) * (units === 's' ? 1000 : 1);
  27711. }
  27712. /** Pattern to identify a `prefetch when` trigger. */
  27713. const PREFETCH_WHEN_PATTERN = /^prefetch\s+when\s/;
  27714. /** Pattern to identify a `prefetch on` trigger. */
  27715. const PREFETCH_ON_PATTERN = /^prefetch\s+on\s/;
  27716. /** Pattern to identify a `hydrate when` trigger. */
  27717. const HYDRATE_WHEN_PATTERN = /^hydrate\s+when\s/;
  27718. /** Pattern to identify a `hydrate on` trigger. */
  27719. const HYDRATE_ON_PATTERN = /^hydrate\s+on\s/;
  27720. /** Pattern to identify a `hydrate never` trigger. */
  27721. const HYDRATE_NEVER_PATTERN = /^hydrate\s+never(\s*)$/;
  27722. /** Pattern to identify a `minimum` parameter in a block. */
  27723. const MINIMUM_PARAMETER_PATTERN = /^minimum\s/;
  27724. /** Pattern to identify a `after` parameter in a block. */
  27725. const AFTER_PARAMETER_PATTERN = /^after\s/;
  27726. /** Pattern to identify a `when` parameter in a block. */
  27727. const WHEN_PARAMETER_PATTERN = /^when\s/;
  27728. /** Pattern to identify a `on` parameter in a block. */
  27729. const ON_PARAMETER_PATTERN = /^on\s/;
  27730. /**
  27731. * Predicate function that determines if a block with
  27732. * a specific name cam be connected to a `defer` block.
  27733. */
  27734. function isConnectedDeferLoopBlock(name) {
  27735. return name === 'placeholder' || name === 'loading' || name === 'error';
  27736. }
  27737. /** Creates a deferred block from an HTML AST node. */
  27738. function createDeferredBlock(ast, connectedBlocks, visitor, bindingParser) {
  27739. const errors = [];
  27740. const { placeholder, loading, error } = parseConnectedBlocks(connectedBlocks, errors, visitor);
  27741. const { triggers, prefetchTriggers, hydrateTriggers } = parsePrimaryTriggers(ast, bindingParser, errors, placeholder);
  27742. // The `defer` block has a main span encompassing all of the connected branches as well.
  27743. let lastEndSourceSpan = ast.endSourceSpan;
  27744. let endOfLastSourceSpan = ast.sourceSpan.end;
  27745. if (connectedBlocks.length > 0) {
  27746. const lastConnectedBlock = connectedBlocks[connectedBlocks.length - 1];
  27747. lastEndSourceSpan = lastConnectedBlock.endSourceSpan;
  27748. endOfLastSourceSpan = lastConnectedBlock.sourceSpan.end;
  27749. }
  27750. const sourceSpanWithConnectedBlocks = new ParseSourceSpan(ast.sourceSpan.start, endOfLastSourceSpan);
  27751. const node = new DeferredBlock(visitAll(visitor, ast.children, ast.children), triggers, prefetchTriggers, hydrateTriggers, placeholder, loading, error, ast.nameSpan, sourceSpanWithConnectedBlocks, ast.sourceSpan, ast.startSourceSpan, lastEndSourceSpan, ast.i18n);
  27752. return { node, errors };
  27753. }
  27754. function parseConnectedBlocks(connectedBlocks, errors, visitor) {
  27755. let placeholder = null;
  27756. let loading = null;
  27757. let error = null;
  27758. for (const block of connectedBlocks) {
  27759. try {
  27760. if (!isConnectedDeferLoopBlock(block.name)) {
  27761. errors.push(new ParseError(block.startSourceSpan, `Unrecognized block "@${block.name}"`));
  27762. break;
  27763. }
  27764. switch (block.name) {
  27765. case 'placeholder':
  27766. if (placeholder !== null) {
  27767. errors.push(new ParseError(block.startSourceSpan, `@defer block can only have one @placeholder block`));
  27768. }
  27769. else {
  27770. placeholder = parsePlaceholderBlock(block, visitor);
  27771. }
  27772. break;
  27773. case 'loading':
  27774. if (loading !== null) {
  27775. errors.push(new ParseError(block.startSourceSpan, `@defer block can only have one @loading block`));
  27776. }
  27777. else {
  27778. loading = parseLoadingBlock(block, visitor);
  27779. }
  27780. break;
  27781. case 'error':
  27782. if (error !== null) {
  27783. errors.push(new ParseError(block.startSourceSpan, `@defer block can only have one @error block`));
  27784. }
  27785. else {
  27786. error = parseErrorBlock(block, visitor);
  27787. }
  27788. break;
  27789. }
  27790. }
  27791. catch (e) {
  27792. errors.push(new ParseError(block.startSourceSpan, e.message));
  27793. }
  27794. }
  27795. return { placeholder, loading, error };
  27796. }
  27797. function parsePlaceholderBlock(ast, visitor) {
  27798. let minimumTime = null;
  27799. for (const param of ast.parameters) {
  27800. if (MINIMUM_PARAMETER_PATTERN.test(param.expression)) {
  27801. if (minimumTime != null) {
  27802. throw new Error(`@placeholder block can only have one "minimum" parameter`);
  27803. }
  27804. const parsedTime = parseDeferredTime(param.expression.slice(getTriggerParametersStart(param.expression)));
  27805. if (parsedTime === null) {
  27806. throw new Error(`Could not parse time value of parameter "minimum"`);
  27807. }
  27808. minimumTime = parsedTime;
  27809. }
  27810. else {
  27811. throw new Error(`Unrecognized parameter in @placeholder block: "${param.expression}"`);
  27812. }
  27813. }
  27814. return new DeferredBlockPlaceholder(visitAll(visitor, ast.children, ast.children), minimumTime, ast.nameSpan, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.i18n);
  27815. }
  27816. function parseLoadingBlock(ast, visitor) {
  27817. let afterTime = null;
  27818. let minimumTime = null;
  27819. for (const param of ast.parameters) {
  27820. if (AFTER_PARAMETER_PATTERN.test(param.expression)) {
  27821. if (afterTime != null) {
  27822. throw new Error(`@loading block can only have one "after" parameter`);
  27823. }
  27824. const parsedTime = parseDeferredTime(param.expression.slice(getTriggerParametersStart(param.expression)));
  27825. if (parsedTime === null) {
  27826. throw new Error(`Could not parse time value of parameter "after"`);
  27827. }
  27828. afterTime = parsedTime;
  27829. }
  27830. else if (MINIMUM_PARAMETER_PATTERN.test(param.expression)) {
  27831. if (minimumTime != null) {
  27832. throw new Error(`@loading block can only have one "minimum" parameter`);
  27833. }
  27834. const parsedTime = parseDeferredTime(param.expression.slice(getTriggerParametersStart(param.expression)));
  27835. if (parsedTime === null) {
  27836. throw new Error(`Could not parse time value of parameter "minimum"`);
  27837. }
  27838. minimumTime = parsedTime;
  27839. }
  27840. else {
  27841. throw new Error(`Unrecognized parameter in @loading block: "${param.expression}"`);
  27842. }
  27843. }
  27844. return new DeferredBlockLoading(visitAll(visitor, ast.children, ast.children), afterTime, minimumTime, ast.nameSpan, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.i18n);
  27845. }
  27846. function parseErrorBlock(ast, visitor) {
  27847. if (ast.parameters.length > 0) {
  27848. throw new Error(`@error block cannot have parameters`);
  27849. }
  27850. return new DeferredBlockError(visitAll(visitor, ast.children, ast.children), ast.nameSpan, ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan, ast.i18n);
  27851. }
  27852. function parsePrimaryTriggers(ast, bindingParser, errors, placeholder) {
  27853. const triggers = {};
  27854. const prefetchTriggers = {};
  27855. const hydrateTriggers = {};
  27856. for (const param of ast.parameters) {
  27857. // The lexer ignores the leading spaces so we can assume
  27858. // that the expression starts with a keyword.
  27859. if (WHEN_PARAMETER_PATTERN.test(param.expression)) {
  27860. parseWhenTrigger(param, bindingParser, triggers, errors);
  27861. }
  27862. else if (ON_PARAMETER_PATTERN.test(param.expression)) {
  27863. parseOnTrigger(param, triggers, errors, placeholder);
  27864. }
  27865. else if (PREFETCH_WHEN_PATTERN.test(param.expression)) {
  27866. parseWhenTrigger(param, bindingParser, prefetchTriggers, errors);
  27867. }
  27868. else if (PREFETCH_ON_PATTERN.test(param.expression)) {
  27869. parseOnTrigger(param, prefetchTriggers, errors, placeholder);
  27870. }
  27871. else if (HYDRATE_WHEN_PATTERN.test(param.expression)) {
  27872. parseWhenTrigger(param, bindingParser, hydrateTriggers, errors);
  27873. }
  27874. else if (HYDRATE_ON_PATTERN.test(param.expression)) {
  27875. parseOnTrigger(param, hydrateTriggers, errors, placeholder);
  27876. }
  27877. else if (HYDRATE_NEVER_PATTERN.test(param.expression)) {
  27878. parseNeverTrigger(param, hydrateTriggers, errors);
  27879. }
  27880. else {
  27881. errors.push(new ParseError(param.sourceSpan, 'Unrecognized trigger'));
  27882. }
  27883. }
  27884. if (hydrateTriggers.never && Object.keys(hydrateTriggers).length > 1) {
  27885. errors.push(new ParseError(ast.startSourceSpan, 'Cannot specify additional `hydrate` triggers if `hydrate never` is present'));
  27886. }
  27887. return { triggers, prefetchTriggers, hydrateTriggers };
  27888. }
  27889. const BIND_NAME_REGEXP = /^(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.*)$/;
  27890. // Group 1 = "bind-"
  27891. const KW_BIND_IDX = 1;
  27892. // Group 2 = "let-"
  27893. const KW_LET_IDX = 2;
  27894. // Group 3 = "ref-/#"
  27895. const KW_REF_IDX = 3;
  27896. // Group 4 = "on-"
  27897. const KW_ON_IDX = 4;
  27898. // Group 5 = "bindon-"
  27899. const KW_BINDON_IDX = 5;
  27900. // Group 6 = "@"
  27901. const KW_AT_IDX = 6;
  27902. // Group 7 = the identifier after "bind-", "let-", "ref-/#", "on-", "bindon-" or "@"
  27903. const IDENT_KW_IDX = 7;
  27904. const BINDING_DELIMS = {
  27905. BANANA_BOX: { start: '[(', end: ')]' },
  27906. PROPERTY: { start: '[', end: ']' },
  27907. EVENT: { start: '(', end: ')' },
  27908. };
  27909. const TEMPLATE_ATTR_PREFIX = '*';
  27910. function htmlAstToRender3Ast(htmlNodes, bindingParser, options) {
  27911. const transformer = new HtmlAstToIvyAst(bindingParser, options);
  27912. const ivyNodes = visitAll(transformer, htmlNodes, htmlNodes);
  27913. // Errors might originate in either the binding parser or the html to ivy transformer
  27914. const allErrors = bindingParser.errors.concat(transformer.errors);
  27915. const result = {
  27916. nodes: ivyNodes,
  27917. errors: allErrors,
  27918. styleUrls: transformer.styleUrls,
  27919. styles: transformer.styles,
  27920. ngContentSelectors: transformer.ngContentSelectors,
  27921. };
  27922. if (options.collectCommentNodes) {
  27923. result.commentNodes = transformer.commentNodes;
  27924. }
  27925. return result;
  27926. }
  27927. class HtmlAstToIvyAst {
  27928. bindingParser;
  27929. options;
  27930. errors = [];
  27931. styles = [];
  27932. styleUrls = [];
  27933. ngContentSelectors = [];
  27934. // This array will be populated if `Render3ParseOptions['collectCommentNodes']` is true
  27935. commentNodes = [];
  27936. inI18nBlock = false;
  27937. /**
  27938. * Keeps track of the nodes that have been processed already when previous nodes were visited.
  27939. * These are typically blocks connected to other blocks or text nodes between connected blocks.
  27940. */
  27941. processedNodes = new Set();
  27942. constructor(bindingParser, options) {
  27943. this.bindingParser = bindingParser;
  27944. this.options = options;
  27945. }
  27946. // HTML visitor
  27947. visitElement(element) {
  27948. const isI18nRootElement = isI18nRootNode(element.i18n);
  27949. if (isI18nRootElement) {
  27950. if (this.inI18nBlock) {
  27951. this.reportError('Cannot mark an element as translatable inside of a translatable section. Please remove the nested i18n marker.', element.sourceSpan);
  27952. }
  27953. this.inI18nBlock = true;
  27954. }
  27955. const preparsedElement = preparseElement(element);
  27956. if (preparsedElement.type === PreparsedElementType.SCRIPT) {
  27957. return null;
  27958. }
  27959. else if (preparsedElement.type === PreparsedElementType.STYLE) {
  27960. const contents = textContents(element);
  27961. if (contents !== null) {
  27962. this.styles.push(contents);
  27963. }
  27964. return null;
  27965. }
  27966. else if (preparsedElement.type === PreparsedElementType.STYLESHEET &&
  27967. isStyleUrlResolvable(preparsedElement.hrefAttr)) {
  27968. this.styleUrls.push(preparsedElement.hrefAttr);
  27969. return null;
  27970. }
  27971. // Whether the element is a `<ng-template>`
  27972. const isTemplateElement = isNgTemplate(element.name);
  27973. const parsedProperties = [];
  27974. const boundEvents = [];
  27975. const variables = [];
  27976. const references = [];
  27977. const attributes = [];
  27978. const i18nAttrsMeta = {};
  27979. const templateParsedProperties = [];
  27980. const templateVariables = [];
  27981. // Whether the element has any *-attribute
  27982. let elementHasInlineTemplate = false;
  27983. for (const attribute of element.attrs) {
  27984. let hasBinding = false;
  27985. const normalizedName = normalizeAttributeName(attribute.name);
  27986. // `*attr` defines template bindings
  27987. let isTemplateBinding = false;
  27988. if (attribute.i18n) {
  27989. i18nAttrsMeta[attribute.name] = attribute.i18n;
  27990. }
  27991. if (normalizedName.startsWith(TEMPLATE_ATTR_PREFIX)) {
  27992. // *-attributes
  27993. if (elementHasInlineTemplate) {
  27994. this.reportError(`Can't have multiple template bindings on one element. Use only one attribute prefixed with *`, attribute.sourceSpan);
  27995. }
  27996. isTemplateBinding = true;
  27997. elementHasInlineTemplate = true;
  27998. const templateValue = attribute.value;
  27999. const templateKey = normalizedName.substring(TEMPLATE_ATTR_PREFIX.length);
  28000. const parsedVariables = [];
  28001. const absoluteValueOffset = attribute.valueSpan
  28002. ? attribute.valueSpan.start.offset
  28003. : // If there is no value span the attribute does not have a value, like `attr` in
  28004. //`<div attr></div>`. In this case, point to one character beyond the last character of
  28005. // the attribute name.
  28006. attribute.sourceSpan.start.offset + attribute.name.length;
  28007. this.bindingParser.parseInlineTemplateBinding(templateKey, templateValue, attribute.sourceSpan, absoluteValueOffset, [], templateParsedProperties, parsedVariables, true /* isIvyAst */);
  28008. templateVariables.push(...parsedVariables.map((v) => new Variable(v.name, v.value, v.sourceSpan, v.keySpan, v.valueSpan)));
  28009. }
  28010. else {
  28011. // Check for variables, events, property bindings, interpolation
  28012. hasBinding = this.parseAttribute(isTemplateElement, attribute, [], parsedProperties, boundEvents, variables, references);
  28013. }
  28014. if (!hasBinding && !isTemplateBinding) {
  28015. // don't include the bindings as attributes as well in the AST
  28016. attributes.push(this.visitAttribute(attribute));
  28017. }
  28018. }
  28019. let children;
  28020. if (preparsedElement.nonBindable) {
  28021. // The `NonBindableVisitor` may need to return an array of nodes for blocks so we need
  28022. // to flatten the array here. Avoid doing this for the `HtmlAstToIvyAst` since `flat` creates
  28023. // a new array.
  28024. children = visitAll(NON_BINDABLE_VISITOR, element.children).flat(Infinity);
  28025. }
  28026. else {
  28027. children = visitAll(this, element.children, element.children);
  28028. }
  28029. let parsedElement;
  28030. if (preparsedElement.type === PreparsedElementType.NG_CONTENT) {
  28031. const selector = preparsedElement.selectAttr;
  28032. const attrs = element.attrs.map((attr) => this.visitAttribute(attr));
  28033. parsedElement = new Content(selector, attrs, children, element.sourceSpan, element.i18n);
  28034. this.ngContentSelectors.push(selector);
  28035. }
  28036. else if (isTemplateElement) {
  28037. // `<ng-template>`
  28038. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  28039. parsedElement = new Template(element.name, attributes, attrs.bound, boundEvents, [
  28040. /* no template attributes */
  28041. ], children, references, variables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  28042. }
  28043. else {
  28044. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  28045. parsedElement = new Element$1(element.name, attributes, attrs.bound, boundEvents, children, references, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  28046. }
  28047. if (elementHasInlineTemplate) {
  28048. // If this node is an inline-template (e.g. has *ngFor) then we need to create a template
  28049. // node that contains this node.
  28050. // Moreover, if the node is an element, then we need to hoist its attributes to the template
  28051. // node for matching against content projection selectors.
  28052. const attrs = this.extractAttributes('ng-template', templateParsedProperties, i18nAttrsMeta);
  28053. const templateAttrs = [];
  28054. attrs.literal.forEach((attr) => templateAttrs.push(attr));
  28055. attrs.bound.forEach((attr) => templateAttrs.push(attr));
  28056. const hoistedAttrs = parsedElement instanceof Element$1
  28057. ? {
  28058. attributes: parsedElement.attributes,
  28059. inputs: parsedElement.inputs,
  28060. outputs: parsedElement.outputs,
  28061. }
  28062. : { attributes: [], inputs: [], outputs: [] };
  28063. // For <ng-template>s with structural directives on them, avoid passing i18n information to
  28064. // the wrapping template to prevent unnecessary i18n instructions from being generated. The
  28065. // necessary i18n meta information will be extracted from child elements.
  28066. const i18n = isTemplateElement && isI18nRootElement ? undefined : element.i18n;
  28067. const name = parsedElement instanceof Template ? null : parsedElement.name;
  28068. parsedElement = new Template(name, hoistedAttrs.attributes, hoistedAttrs.inputs, hoistedAttrs.outputs, templateAttrs, [parsedElement], [
  28069. /* no references */
  28070. ], templateVariables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, i18n);
  28071. }
  28072. if (isI18nRootElement) {
  28073. this.inI18nBlock = false;
  28074. }
  28075. return parsedElement;
  28076. }
  28077. visitAttribute(attribute) {
  28078. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  28079. }
  28080. visitText(text) {
  28081. return this.processedNodes.has(text)
  28082. ? null
  28083. : this._visitTextWithInterpolation(text.value, text.sourceSpan, text.tokens, text.i18n);
  28084. }
  28085. visitExpansion(expansion) {
  28086. if (!expansion.i18n) {
  28087. // do not generate Icu in case it was created
  28088. // outside of i18n block in a template
  28089. return null;
  28090. }
  28091. if (!isI18nRootNode(expansion.i18n)) {
  28092. throw new Error(`Invalid type "${expansion.i18n.constructor}" for "i18n" property of ${expansion.sourceSpan.toString()}. Expected a "Message"`);
  28093. }
  28094. const message = expansion.i18n;
  28095. const vars = {};
  28096. const placeholders = {};
  28097. // extract VARs from ICUs - we process them separately while
  28098. // assembling resulting message via goog.getMsg function, since
  28099. // we need to pass them to top-level goog.getMsg call
  28100. Object.keys(message.placeholders).forEach((key) => {
  28101. const value = message.placeholders[key];
  28102. if (key.startsWith(I18N_ICU_VAR_PREFIX)) {
  28103. // Currently when the `plural` or `select` keywords in an ICU contain trailing spaces (e.g.
  28104. // `{count, select , ...}`), these spaces are also included into the key names in ICU vars
  28105. // (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be
  28106. // converted into `_` symbols while normalizing placeholder names, which might lead to
  28107. // mismatches at runtime (i.e. placeholder will not be replaced with the correct value).
  28108. const formattedKey = key.trim();
  28109. const ast = this.bindingParser.parseInterpolationExpression(value.text, value.sourceSpan);
  28110. vars[formattedKey] = new BoundText(ast, value.sourceSpan);
  28111. }
  28112. else {
  28113. placeholders[key] = this._visitTextWithInterpolation(value.text, value.sourceSpan, null);
  28114. }
  28115. });
  28116. return new Icu$1(vars, placeholders, expansion.sourceSpan, message);
  28117. }
  28118. visitExpansionCase(expansionCase) {
  28119. return null;
  28120. }
  28121. visitComment(comment) {
  28122. if (this.options.collectCommentNodes) {
  28123. this.commentNodes.push(new Comment$1(comment.value || '', comment.sourceSpan));
  28124. }
  28125. return null;
  28126. }
  28127. visitLetDeclaration(decl, context) {
  28128. const value = this.bindingParser.parseBinding(decl.value, false, decl.valueSpan, decl.valueSpan.start.offset);
  28129. if (value.errors.length === 0 && value.ast instanceof EmptyExpr$1) {
  28130. this.reportError('@let declaration value cannot be empty', decl.valueSpan);
  28131. }
  28132. return new LetDeclaration$1(decl.name, value, decl.sourceSpan, decl.nameSpan, decl.valueSpan);
  28133. }
  28134. visitBlockParameter() {
  28135. return null;
  28136. }
  28137. visitBlock(block, context) {
  28138. const index = Array.isArray(context) ? context.indexOf(block) : -1;
  28139. if (index === -1) {
  28140. throw new Error('Visitor invoked incorrectly. Expecting visitBlock to be invoked siblings array as its context');
  28141. }
  28142. // Connected blocks may have been processed as a part of the previous block.
  28143. if (this.processedNodes.has(block)) {
  28144. return null;
  28145. }
  28146. let result = null;
  28147. switch (block.name) {
  28148. case 'defer':
  28149. result = createDeferredBlock(block, this.findConnectedBlocks(index, context, isConnectedDeferLoopBlock), this, this.bindingParser);
  28150. break;
  28151. case 'switch':
  28152. result = createSwitchBlock(block, this, this.bindingParser);
  28153. break;
  28154. case 'for':
  28155. result = createForLoop(block, this.findConnectedBlocks(index, context, isConnectedForLoopBlock), this, this.bindingParser);
  28156. break;
  28157. case 'if':
  28158. result = createIfBlock(block, this.findConnectedBlocks(index, context, isConnectedIfLoopBlock), this, this.bindingParser);
  28159. break;
  28160. default:
  28161. let errorMessage;
  28162. if (isConnectedDeferLoopBlock(block.name)) {
  28163. errorMessage = `@${block.name} block can only be used after an @defer block.`;
  28164. this.processedNodes.add(block);
  28165. }
  28166. else if (isConnectedForLoopBlock(block.name)) {
  28167. errorMessage = `@${block.name} block can only be used after an @for block.`;
  28168. this.processedNodes.add(block);
  28169. }
  28170. else if (isConnectedIfLoopBlock(block.name)) {
  28171. errorMessage = `@${block.name} block can only be used after an @if or @else if block.`;
  28172. this.processedNodes.add(block);
  28173. }
  28174. else {
  28175. errorMessage = `Unrecognized block @${block.name}.`;
  28176. }
  28177. result = {
  28178. node: new UnknownBlock(block.name, block.sourceSpan, block.nameSpan),
  28179. errors: [new ParseError(block.sourceSpan, errorMessage)],
  28180. };
  28181. break;
  28182. }
  28183. this.errors.push(...result.errors);
  28184. return result.node;
  28185. }
  28186. findConnectedBlocks(primaryBlockIndex, siblings, predicate) {
  28187. const relatedBlocks = [];
  28188. for (let i = primaryBlockIndex + 1; i < siblings.length; i++) {
  28189. const node = siblings[i];
  28190. // Skip over comments.
  28191. if (node instanceof Comment) {
  28192. continue;
  28193. }
  28194. // Ignore empty text nodes between blocks.
  28195. if (node instanceof Text && node.value.trim().length === 0) {
  28196. // Add the text node to the processed nodes since we don't want
  28197. // it to be generated between the connected nodes.
  28198. this.processedNodes.add(node);
  28199. continue;
  28200. }
  28201. // Stop searching as soon as we hit a non-block node or a block that is unrelated.
  28202. if (!(node instanceof Block) || !predicate(node.name)) {
  28203. break;
  28204. }
  28205. relatedBlocks.push(node);
  28206. this.processedNodes.add(node);
  28207. }
  28208. return relatedBlocks;
  28209. }
  28210. // convert view engine `ParsedProperty` to a format suitable for IVY
  28211. extractAttributes(elementName, properties, i18nPropsMeta) {
  28212. const bound = [];
  28213. const literal = [];
  28214. properties.forEach((prop) => {
  28215. const i18n = i18nPropsMeta[prop.name];
  28216. if (prop.isLiteral) {
  28217. literal.push(new TextAttribute(prop.name, prop.expression.source || '', prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n));
  28218. }
  28219. else {
  28220. // Note that validation is skipped and property mapping is disabled
  28221. // due to the fact that we need to make sure a given prop is not an
  28222. // input of a directive and directive matching happens at runtime.
  28223. const bep = this.bindingParser.createBoundElementProperty(elementName, prop,
  28224. /* skipValidation */ true,
  28225. /* mapPropertyName */ false);
  28226. bound.push(BoundAttribute.fromBoundElementProperty(bep, i18n));
  28227. }
  28228. });
  28229. return { bound, literal };
  28230. }
  28231. parseAttribute(isTemplateElement, attribute, matchableAttributes, parsedProperties, boundEvents, variables, references) {
  28232. const name = normalizeAttributeName(attribute.name);
  28233. const value = attribute.value;
  28234. const srcSpan = attribute.sourceSpan;
  28235. const absoluteOffset = attribute.valueSpan
  28236. ? attribute.valueSpan.start.offset
  28237. : srcSpan.start.offset;
  28238. function createKeySpan(srcSpan, prefix, identifier) {
  28239. // We need to adjust the start location for the keySpan to account for the removed 'data-'
  28240. // prefix from `normalizeAttributeName`.
  28241. const normalizationAdjustment = attribute.name.length - name.length;
  28242. const keySpanStart = srcSpan.start.moveBy(prefix.length + normalizationAdjustment);
  28243. const keySpanEnd = keySpanStart.moveBy(identifier.length);
  28244. return new ParseSourceSpan(keySpanStart, keySpanEnd, keySpanStart, identifier);
  28245. }
  28246. const bindParts = name.match(BIND_NAME_REGEXP);
  28247. if (bindParts) {
  28248. if (bindParts[KW_BIND_IDX] != null) {
  28249. const identifier = bindParts[IDENT_KW_IDX];
  28250. const keySpan = createKeySpan(srcSpan, bindParts[KW_BIND_IDX], identifier);
  28251. this.bindingParser.parsePropertyBinding(identifier, value, false, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28252. }
  28253. else if (bindParts[KW_LET_IDX]) {
  28254. if (isTemplateElement) {
  28255. const identifier = bindParts[IDENT_KW_IDX];
  28256. const keySpan = createKeySpan(srcSpan, bindParts[KW_LET_IDX], identifier);
  28257. this.parseVariable(identifier, value, srcSpan, keySpan, attribute.valueSpan, variables);
  28258. }
  28259. else {
  28260. this.reportError(`"let-" is only supported on ng-template elements.`, srcSpan);
  28261. }
  28262. }
  28263. else if (bindParts[KW_REF_IDX]) {
  28264. const identifier = bindParts[IDENT_KW_IDX];
  28265. const keySpan = createKeySpan(srcSpan, bindParts[KW_REF_IDX], identifier);
  28266. this.parseReference(identifier, value, srcSpan, keySpan, attribute.valueSpan, references);
  28267. }
  28268. else if (bindParts[KW_ON_IDX]) {
  28269. const events = [];
  28270. const identifier = bindParts[IDENT_KW_IDX];
  28271. const keySpan = createKeySpan(srcSpan, bindParts[KW_ON_IDX], identifier);
  28272. this.bindingParser.parseEvent(identifier, value,
  28273. /* isAssignmentEvent */ false, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  28274. addEvents(events, boundEvents);
  28275. }
  28276. else if (bindParts[KW_BINDON_IDX]) {
  28277. const identifier = bindParts[IDENT_KW_IDX];
  28278. const keySpan = createKeySpan(srcSpan, bindParts[KW_BINDON_IDX], identifier);
  28279. this.bindingParser.parsePropertyBinding(identifier, value, false, true, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28280. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  28281. }
  28282. else if (bindParts[KW_AT_IDX]) {
  28283. const keySpan = createKeySpan(srcSpan, '', name);
  28284. this.bindingParser.parseLiteralAttr(name, value, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28285. }
  28286. return true;
  28287. }
  28288. // We didn't see a kw-prefixed property binding, but we have not yet checked
  28289. // for the []/()/[()] syntax.
  28290. let delims = null;
  28291. if (name.startsWith(BINDING_DELIMS.BANANA_BOX.start)) {
  28292. delims = BINDING_DELIMS.BANANA_BOX;
  28293. }
  28294. else if (name.startsWith(BINDING_DELIMS.PROPERTY.start)) {
  28295. delims = BINDING_DELIMS.PROPERTY;
  28296. }
  28297. else if (name.startsWith(BINDING_DELIMS.EVENT.start)) {
  28298. delims = BINDING_DELIMS.EVENT;
  28299. }
  28300. if (delims !== null &&
  28301. // NOTE: older versions of the parser would match a start/end delimited
  28302. // binding iff the property name was terminated by the ending delimiter
  28303. // and the identifier in the binding was non-empty.
  28304. // TODO(ayazhafiz): update this to handle malformed bindings.
  28305. name.endsWith(delims.end) &&
  28306. name.length > delims.start.length + delims.end.length) {
  28307. const identifier = name.substring(delims.start.length, name.length - delims.end.length);
  28308. const keySpan = createKeySpan(srcSpan, delims.start, identifier);
  28309. if (delims.start === BINDING_DELIMS.BANANA_BOX.start) {
  28310. this.bindingParser.parsePropertyBinding(identifier, value, false, true, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28311. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  28312. }
  28313. else if (delims.start === BINDING_DELIMS.PROPERTY.start) {
  28314. this.bindingParser.parsePropertyBinding(identifier, value, false, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  28315. }
  28316. else {
  28317. const events = [];
  28318. this.bindingParser.parseEvent(identifier, value,
  28319. /* isAssignmentEvent */ false, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  28320. addEvents(events, boundEvents);
  28321. }
  28322. return true;
  28323. }
  28324. // No explicit binding found.
  28325. const keySpan = createKeySpan(srcSpan, '' /* prefix */, name);
  28326. const hasBinding = this.bindingParser.parsePropertyInterpolation(name, value, srcSpan, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan, attribute.valueTokens ?? null);
  28327. return hasBinding;
  28328. }
  28329. _visitTextWithInterpolation(value, sourceSpan, interpolatedTokens, i18n) {
  28330. const valueNoNgsp = replaceNgsp(value);
  28331. const expr = this.bindingParser.parseInterpolation(valueNoNgsp, sourceSpan, interpolatedTokens);
  28332. return expr ? new BoundText(expr, sourceSpan, i18n) : new Text$3(valueNoNgsp, sourceSpan);
  28333. }
  28334. parseVariable(identifier, value, sourceSpan, keySpan, valueSpan, variables) {
  28335. if (identifier.indexOf('-') > -1) {
  28336. this.reportError(`"-" is not allowed in variable names`, sourceSpan);
  28337. }
  28338. else if (identifier.length === 0) {
  28339. this.reportError(`Variable does not have a name`, sourceSpan);
  28340. }
  28341. variables.push(new Variable(identifier, value, sourceSpan, keySpan, valueSpan));
  28342. }
  28343. parseReference(identifier, value, sourceSpan, keySpan, valueSpan, references) {
  28344. if (identifier.indexOf('-') > -1) {
  28345. this.reportError(`"-" is not allowed in reference names`, sourceSpan);
  28346. }
  28347. else if (identifier.length === 0) {
  28348. this.reportError(`Reference does not have a name`, sourceSpan);
  28349. }
  28350. else if (references.some((reference) => reference.name === identifier)) {
  28351. this.reportError(`Reference "#${identifier}" is defined more than once`, sourceSpan);
  28352. }
  28353. references.push(new Reference$1(identifier, value, sourceSpan, keySpan, valueSpan));
  28354. }
  28355. parseAssignmentEvent(name, expression, sourceSpan, valueSpan, targetMatchableAttrs, boundEvents, keySpan) {
  28356. const events = [];
  28357. this.bindingParser.parseEvent(`${name}Change`, expression,
  28358. /* isAssignmentEvent */ true, sourceSpan, valueSpan || sourceSpan, targetMatchableAttrs, events, keySpan);
  28359. addEvents(events, boundEvents);
  28360. }
  28361. reportError(message, sourceSpan, level = ParseErrorLevel.ERROR) {
  28362. this.errors.push(new ParseError(sourceSpan, message, level));
  28363. }
  28364. }
  28365. class NonBindableVisitor {
  28366. visitElement(ast) {
  28367. const preparsedElement = preparseElement(ast);
  28368. if (preparsedElement.type === PreparsedElementType.SCRIPT ||
  28369. preparsedElement.type === PreparsedElementType.STYLE ||
  28370. preparsedElement.type === PreparsedElementType.STYLESHEET) {
  28371. // Skipping <script> for security reasons
  28372. // Skipping <style> and stylesheets as we already processed them
  28373. // in the StyleCompiler
  28374. return null;
  28375. }
  28376. const children = visitAll(this, ast.children, null);
  28377. return new Element$1(ast.name, visitAll(this, ast.attrs),
  28378. /* inputs */ [],
  28379. /* outputs */ [], children,
  28380. /* references */ [], ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan);
  28381. }
  28382. visitComment(comment) {
  28383. return null;
  28384. }
  28385. visitAttribute(attribute) {
  28386. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  28387. }
  28388. visitText(text) {
  28389. return new Text$3(text.value, text.sourceSpan);
  28390. }
  28391. visitExpansion(expansion) {
  28392. return null;
  28393. }
  28394. visitExpansionCase(expansionCase) {
  28395. return null;
  28396. }
  28397. visitBlock(block, context) {
  28398. const nodes = [
  28399. // In an ngNonBindable context we treat the opening/closing tags of block as plain text.
  28400. // This is the as if the `tokenizeBlocks` option was disabled.
  28401. new Text$3(block.startSourceSpan.toString(), block.startSourceSpan),
  28402. ...visitAll(this, block.children),
  28403. ];
  28404. if (block.endSourceSpan !== null) {
  28405. nodes.push(new Text$3(block.endSourceSpan.toString(), block.endSourceSpan));
  28406. }
  28407. return nodes;
  28408. }
  28409. visitBlockParameter(parameter, context) {
  28410. return null;
  28411. }
  28412. visitLetDeclaration(decl, context) {
  28413. return new Text$3(`@let ${decl.name} = ${decl.value};`, decl.sourceSpan);
  28414. }
  28415. }
  28416. const NON_BINDABLE_VISITOR = new NonBindableVisitor();
  28417. function normalizeAttributeName(attrName) {
  28418. return /^data-/i.test(attrName) ? attrName.substring(5) : attrName;
  28419. }
  28420. function addEvents(events, boundEvents) {
  28421. boundEvents.push(...events.map((e) => BoundEvent.fromParsedEvent(e)));
  28422. }
  28423. function textContents(node) {
  28424. if (node.children.length !== 1 || !(node.children[0] instanceof Text)) {
  28425. return null;
  28426. }
  28427. else {
  28428. return node.children[0].value;
  28429. }
  28430. }
  28431. const LEADING_TRIVIA_CHARS = [' ', '\n', '\r', '\t'];
  28432. /**
  28433. * Parse a template into render3 `Node`s and additional metadata, with no other dependencies.
  28434. *
  28435. * @param template text of the template to parse
  28436. * @param templateUrl URL to use for source mapping of the parsed template
  28437. * @param options options to modify how the template is parsed
  28438. */
  28439. function parseTemplate(template, templateUrl, options = {}) {
  28440. const { interpolationConfig, preserveWhitespaces, enableI18nLegacyMessageIdFormat } = options;
  28441. const bindingParser = makeBindingParser(interpolationConfig);
  28442. const htmlParser = new HtmlParser();
  28443. const parseResult = htmlParser.parse(template, templateUrl, {
  28444. leadingTriviaChars: LEADING_TRIVIA_CHARS,
  28445. ...options,
  28446. tokenizeExpansionForms: true,
  28447. tokenizeBlocks: options.enableBlockSyntax ?? true,
  28448. tokenizeLet: options.enableLetSyntax ?? true,
  28449. });
  28450. if (!options.alwaysAttemptHtmlToR3AstConversion &&
  28451. parseResult.errors &&
  28452. parseResult.errors.length > 0) {
  28453. const parsedTemplate = {
  28454. interpolationConfig,
  28455. preserveWhitespaces,
  28456. errors: parseResult.errors,
  28457. nodes: [],
  28458. styleUrls: [],
  28459. styles: [],
  28460. ngContentSelectors: [],
  28461. };
  28462. if (options.collectCommentNodes) {
  28463. parsedTemplate.commentNodes = [];
  28464. }
  28465. return parsedTemplate;
  28466. }
  28467. let rootNodes = parseResult.rootNodes;
  28468. // We need to use the same `retainEmptyTokens` value for both parses to avoid
  28469. // causing a mismatch when reusing source spans, even if the
  28470. // `preserveSignificantWhitespace` behavior is different between the two
  28471. // parses.
  28472. const retainEmptyTokens = !(options.preserveSignificantWhitespace ?? true);
  28473. // process i18n meta information (scan attributes, generate ids)
  28474. // before we run whitespace removal process, because existing i18n
  28475. // extraction process (ng extract-i18n) relies on a raw content to generate
  28476. // message ids
  28477. const i18nMetaVisitor = new I18nMetaVisitor(interpolationConfig,
  28478. /* keepI18nAttrs */ !preserveWhitespaces, enableI18nLegacyMessageIdFormat,
  28479. /* containerBlocks */ undefined, options.preserveSignificantWhitespace, retainEmptyTokens);
  28480. const i18nMetaResult = i18nMetaVisitor.visitAllWithErrors(rootNodes);
  28481. if (!options.alwaysAttemptHtmlToR3AstConversion &&
  28482. i18nMetaResult.errors &&
  28483. i18nMetaResult.errors.length > 0) {
  28484. const parsedTemplate = {
  28485. interpolationConfig,
  28486. preserveWhitespaces,
  28487. errors: i18nMetaResult.errors,
  28488. nodes: [],
  28489. styleUrls: [],
  28490. styles: [],
  28491. ngContentSelectors: [],
  28492. };
  28493. if (options.collectCommentNodes) {
  28494. parsedTemplate.commentNodes = [];
  28495. }
  28496. return parsedTemplate;
  28497. }
  28498. rootNodes = i18nMetaResult.rootNodes;
  28499. if (!preserveWhitespaces) {
  28500. // Always preserve significant whitespace here because this is used to generate the `goog.getMsg`
  28501. // and `$localize` calls which should retain significant whitespace in order to render the
  28502. // correct output. We let this diverge from the message IDs generated earlier which might not
  28503. // have preserved significant whitespace.
  28504. //
  28505. // This should use `visitAllWithSiblings` to set `WhitespaceVisitor` context correctly, however
  28506. // there is an existing bug where significant whitespace is not properly retained in the JS
  28507. // output of leading/trailing whitespace for ICU messages due to the existing lack of context\
  28508. // in `WhitespaceVisitor`. Using `visitAllWithSiblings` here would fix that bug and retain the
  28509. // whitespace, however it would also change the runtime representation which we don't want to do
  28510. // right now.
  28511. rootNodes = visitAll(new WhitespaceVisitor(
  28512. /* preserveSignificantWhitespace */ true,
  28513. /* originalNodeMap */ undefined,
  28514. /* requireContext */ false), rootNodes);
  28515. // run i18n meta visitor again in case whitespaces are removed (because that might affect
  28516. // generated i18n message content) and first pass indicated that i18n content is present in a
  28517. // template. During this pass i18n IDs generated at the first pass will be preserved, so we can
  28518. // mimic existing extraction process (ng extract-i18n)
  28519. if (i18nMetaVisitor.hasI18nMeta) {
  28520. rootNodes = visitAll(new I18nMetaVisitor(interpolationConfig,
  28521. /* keepI18nAttrs */ false,
  28522. /* enableI18nLegacyMessageIdFormat */ undefined,
  28523. /* containerBlocks */ undefined,
  28524. /* preserveSignificantWhitespace */ true, retainEmptyTokens), rootNodes);
  28525. }
  28526. }
  28527. const { nodes, errors, styleUrls, styles, ngContentSelectors, commentNodes } = htmlAstToRender3Ast(rootNodes, bindingParser, { collectCommentNodes: !!options.collectCommentNodes });
  28528. errors.push(...parseResult.errors, ...i18nMetaResult.errors);
  28529. const parsedTemplate = {
  28530. interpolationConfig,
  28531. preserveWhitespaces,
  28532. errors: errors.length > 0 ? errors : null,
  28533. nodes,
  28534. styleUrls,
  28535. styles,
  28536. ngContentSelectors,
  28537. };
  28538. if (options.collectCommentNodes) {
  28539. parsedTemplate.commentNodes = commentNodes;
  28540. }
  28541. return parsedTemplate;
  28542. }
  28543. const elementRegistry = new DomElementSchemaRegistry();
  28544. /**
  28545. * Construct a `BindingParser` with a default configuration.
  28546. */
  28547. function makeBindingParser(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  28548. return new BindingParser(new Parser(new Lexer()), interpolationConfig, elementRegistry, []);
  28549. }
  28550. const COMPONENT_VARIABLE = '%COMP%';
  28551. const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
  28552. const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
  28553. function baseDirectiveFields(meta, constantPool, bindingParser) {
  28554. const definitionMap = new DefinitionMap();
  28555. const selectors = parseSelectorToR3Selector(meta.selector);
  28556. // e.g. `type: MyDirective`
  28557. definitionMap.set('type', meta.type.value);
  28558. // e.g. `selectors: [['', 'someDir', '']]`
  28559. if (selectors.length > 0) {
  28560. definitionMap.set('selectors', asLiteral(selectors));
  28561. }
  28562. if (meta.queries.length > 0) {
  28563. // e.g. `contentQueries: (rf, ctx, dirIndex) => { ... }
  28564. definitionMap.set('contentQueries', createContentQueriesFunction(meta.queries, constantPool, meta.name));
  28565. }
  28566. if (meta.viewQueries.length) {
  28567. definitionMap.set('viewQuery', createViewQueriesFunction(meta.viewQueries, constantPool, meta.name));
  28568. }
  28569. // e.g. `hostBindings: (rf, ctx) => { ... }
  28570. definitionMap.set('hostBindings', createHostBindingsFunction(meta.host, meta.typeSourceSpan, bindingParser, constantPool, meta.selector || '', meta.name, definitionMap));
  28571. // e.g 'inputs: {a: 'a'}`
  28572. definitionMap.set('inputs', conditionallyCreateDirectiveBindingLiteral(meta.inputs, true));
  28573. // e.g 'outputs: {a: 'a'}`
  28574. definitionMap.set('outputs', conditionallyCreateDirectiveBindingLiteral(meta.outputs));
  28575. if (meta.exportAs !== null) {
  28576. definitionMap.set('exportAs', literalArr(meta.exportAs.map((e) => literal$1(e))));
  28577. }
  28578. if (meta.isStandalone === false) {
  28579. definitionMap.set('standalone', literal$1(false));
  28580. }
  28581. if (meta.isSignal) {
  28582. definitionMap.set('signals', literal$1(true));
  28583. }
  28584. return definitionMap;
  28585. }
  28586. /**
  28587. * Add features to the definition map.
  28588. */
  28589. function addFeatures(definitionMap, meta) {
  28590. // e.g. `features: [NgOnChangesFeature]`
  28591. const features = [];
  28592. const providers = meta.providers;
  28593. const viewProviders = meta.viewProviders;
  28594. if (providers || viewProviders) {
  28595. const args = [providers || new LiteralArrayExpr([])];
  28596. if (viewProviders) {
  28597. args.push(viewProviders);
  28598. }
  28599. features.push(importExpr(Identifiers.ProvidersFeature).callFn(args));
  28600. }
  28601. // Note: host directives feature needs to be inserted before the
  28602. // inheritance feature to ensure the correct execution order.
  28603. if (meta.hostDirectives?.length) {
  28604. features.push(importExpr(Identifiers.HostDirectivesFeature)
  28605. .callFn([createHostDirectivesFeatureArg(meta.hostDirectives)]));
  28606. }
  28607. if (meta.usesInheritance) {
  28608. features.push(importExpr(Identifiers.InheritDefinitionFeature));
  28609. }
  28610. if (meta.fullInheritance) {
  28611. features.push(importExpr(Identifiers.CopyDefinitionFeature));
  28612. }
  28613. if (meta.lifecycle.usesOnChanges) {
  28614. features.push(importExpr(Identifiers.NgOnChangesFeature));
  28615. }
  28616. if ('externalStyles' in meta && meta.externalStyles?.length) {
  28617. const externalStyleNodes = meta.externalStyles.map((externalStyle) => literal$1(externalStyle));
  28618. features.push(importExpr(Identifiers.ExternalStylesFeature).callFn([literalArr(externalStyleNodes)]));
  28619. }
  28620. if (features.length) {
  28621. definitionMap.set('features', literalArr(features));
  28622. }
  28623. }
  28624. /**
  28625. * Compile a directive for the render3 runtime as defined by the `R3DirectiveMetadata`.
  28626. */
  28627. function compileDirectiveFromMetadata(meta, constantPool, bindingParser) {
  28628. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  28629. addFeatures(definitionMap, meta);
  28630. const expression = importExpr(Identifiers.defineDirective)
  28631. .callFn([definitionMap.toLiteralMap()], undefined, true);
  28632. const type = createDirectiveType(meta);
  28633. return { expression, type, statements: [] };
  28634. }
  28635. /**
  28636. * Compile a component for the render3 runtime as defined by the `R3ComponentMetadata`.
  28637. */
  28638. function compileComponentFromMetadata(meta, constantPool, bindingParser) {
  28639. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  28640. addFeatures(definitionMap, meta);
  28641. const selector = meta.selector && CssSelector.parse(meta.selector);
  28642. const firstSelector = selector && selector[0];
  28643. // e.g. `attr: ["class", ".my.app"]`
  28644. // This is optional an only included if the first selector of a component specifies attributes.
  28645. if (firstSelector) {
  28646. const selectorAttributes = firstSelector.getAttrs();
  28647. if (selectorAttributes.length) {
  28648. definitionMap.set('attrs', constantPool.getConstLiteral(literalArr(selectorAttributes.map((value) => value != null ? literal$1(value) : literal$1(undefined))),
  28649. /* forceShared */ true));
  28650. }
  28651. }
  28652. // e.g. `template: function MyComponent_Template(_ctx, _cm) {...}`
  28653. const templateTypeName = meta.name;
  28654. let allDeferrableDepsFn = null;
  28655. if (meta.defer.mode === 1 /* DeferBlockDepsEmitMode.PerComponent */ &&
  28656. meta.defer.dependenciesFn !== null) {
  28657. const fnName = `${templateTypeName}_DeferFn`;
  28658. constantPool.statements.push(new DeclareVarStmt(fnName, meta.defer.dependenciesFn, undefined, exports.StmtModifier.Final));
  28659. allDeferrableDepsFn = variable(fnName);
  28660. }
  28661. // First the template is ingested into IR:
  28662. const tpl = ingestComponent(meta.name, meta.template.nodes, constantPool, meta.relativeContextFilePath, meta.i18nUseExternalIds, meta.defer, allDeferrableDepsFn, meta.relativeTemplatePath, getTemplateSourceLocationsEnabled());
  28663. // Then the IR is transformed to prepare it for cod egeneration.
  28664. transform(tpl, CompilationJobKind.Tmpl);
  28665. // Finally we emit the template function:
  28666. const templateFn = emitTemplateFn(tpl, constantPool);
  28667. if (tpl.contentSelectors !== null) {
  28668. definitionMap.set('ngContentSelectors', tpl.contentSelectors);
  28669. }
  28670. definitionMap.set('decls', literal$1(tpl.root.decls));
  28671. definitionMap.set('vars', literal$1(tpl.root.vars));
  28672. if (tpl.consts.length > 0) {
  28673. if (tpl.constsInitializers.length > 0) {
  28674. definitionMap.set('consts', arrowFn([], [...tpl.constsInitializers, new ReturnStatement(literalArr(tpl.consts))]));
  28675. }
  28676. else {
  28677. definitionMap.set('consts', literalArr(tpl.consts));
  28678. }
  28679. }
  28680. definitionMap.set('template', templateFn);
  28681. if (meta.declarationListEmitMode !== 3 /* DeclarationListEmitMode.RuntimeResolved */ &&
  28682. meta.declarations.length > 0) {
  28683. definitionMap.set('dependencies', compileDeclarationList(literalArr(meta.declarations.map((decl) => decl.type)), meta.declarationListEmitMode));
  28684. }
  28685. else if (meta.declarationListEmitMode === 3 /* DeclarationListEmitMode.RuntimeResolved */) {
  28686. const args = [meta.type.value];
  28687. if (meta.rawImports) {
  28688. args.push(meta.rawImports);
  28689. }
  28690. definitionMap.set('dependencies', importExpr(Identifiers.getComponentDepsFactory).callFn(args));
  28691. }
  28692. if (meta.encapsulation === null) {
  28693. meta.encapsulation = exports.ViewEncapsulation.Emulated;
  28694. }
  28695. let hasStyles = !!meta.externalStyles?.length;
  28696. // e.g. `styles: [str1, str2]`
  28697. if (meta.styles && meta.styles.length) {
  28698. const styleValues = meta.encapsulation == exports.ViewEncapsulation.Emulated
  28699. ? compileStyles(meta.styles, CONTENT_ATTR, HOST_ATTR)
  28700. : meta.styles;
  28701. const styleNodes = styleValues.reduce((result, style) => {
  28702. if (style.trim().length > 0) {
  28703. result.push(constantPool.getConstLiteral(literal$1(style)));
  28704. }
  28705. return result;
  28706. }, []);
  28707. if (styleNodes.length > 0) {
  28708. hasStyles = true;
  28709. definitionMap.set('styles', literalArr(styleNodes));
  28710. }
  28711. }
  28712. if (!hasStyles && meta.encapsulation === exports.ViewEncapsulation.Emulated) {
  28713. // If there is no style, don't generate css selectors on elements
  28714. meta.encapsulation = exports.ViewEncapsulation.None;
  28715. }
  28716. // Only set view encapsulation if it's not the default value
  28717. if (meta.encapsulation !== exports.ViewEncapsulation.Emulated) {
  28718. definitionMap.set('encapsulation', literal$1(meta.encapsulation));
  28719. }
  28720. // e.g. `animation: [trigger('123', [])]`
  28721. if (meta.animations !== null) {
  28722. definitionMap.set('data', literalMap([{ key: 'animation', value: meta.animations, quoted: false }]));
  28723. }
  28724. // Setting change detection flag
  28725. if (meta.changeDetection !== null) {
  28726. if (typeof meta.changeDetection === 'number' &&
  28727. meta.changeDetection !== exports.ChangeDetectionStrategy.Default) {
  28728. // changeDetection is resolved during analysis. Only set it if not the default.
  28729. definitionMap.set('changeDetection', literal$1(meta.changeDetection));
  28730. }
  28731. else if (typeof meta.changeDetection === 'object') {
  28732. // changeDetection is not resolved during analysis (e.g., we are in local compilation mode).
  28733. // So place it as is.
  28734. definitionMap.set('changeDetection', meta.changeDetection);
  28735. }
  28736. }
  28737. const expression = importExpr(Identifiers.defineComponent)
  28738. .callFn([definitionMap.toLiteralMap()], undefined, true);
  28739. const type = createComponentType(meta);
  28740. return { expression, type, statements: [] };
  28741. }
  28742. /**
  28743. * Creates the type specification from the component meta. This type is inserted into .d.ts files
  28744. * to be consumed by upstream compilations.
  28745. */
  28746. function createComponentType(meta) {
  28747. const typeParams = createBaseDirectiveTypeParams(meta);
  28748. typeParams.push(stringArrayAsType(meta.template.ngContentSelectors));
  28749. typeParams.push(expressionType(literal$1(meta.isStandalone)));
  28750. typeParams.push(createHostDirectivesType(meta));
  28751. // TODO(signals): Always include this metadata starting with v17. Right
  28752. // now Angular v16.0.x does not support this field and library distributions
  28753. // would then be incompatible with v16.0.x framework users.
  28754. if (meta.isSignal) {
  28755. typeParams.push(expressionType(literal$1(meta.isSignal)));
  28756. }
  28757. return expressionType(importExpr(Identifiers.ComponentDeclaration, typeParams));
  28758. }
  28759. /**
  28760. * Compiles the array literal of declarations into an expression according to the provided emit
  28761. * mode.
  28762. */
  28763. function compileDeclarationList(list, mode) {
  28764. switch (mode) {
  28765. case 0 /* DeclarationListEmitMode.Direct */:
  28766. // directives: [MyDir],
  28767. return list;
  28768. case 1 /* DeclarationListEmitMode.Closure */:
  28769. // directives: function () { return [MyDir]; }
  28770. return arrowFn([], list);
  28771. case 2 /* DeclarationListEmitMode.ClosureResolved */:
  28772. // directives: function () { return [MyDir].map(ng.resolveForwardRef); }
  28773. const resolvedList = list.prop('map').callFn([importExpr(Identifiers.resolveForwardRef)]);
  28774. return arrowFn([], resolvedList);
  28775. case 3 /* DeclarationListEmitMode.RuntimeResolved */:
  28776. throw new Error(`Unsupported with an array of pre-resolved dependencies`);
  28777. }
  28778. }
  28779. function stringAsType(str) {
  28780. return expressionType(literal$1(str));
  28781. }
  28782. function stringMapAsLiteralExpression(map) {
  28783. const mapValues = Object.keys(map).map((key) => {
  28784. const value = Array.isArray(map[key]) ? map[key][0] : map[key];
  28785. return {
  28786. key,
  28787. value: literal$1(value),
  28788. quoted: true,
  28789. };
  28790. });
  28791. return literalMap(mapValues);
  28792. }
  28793. function stringArrayAsType(arr) {
  28794. return arr.length > 0
  28795. ? expressionType(literalArr(arr.map((value) => literal$1(value))))
  28796. : NONE_TYPE;
  28797. }
  28798. function createBaseDirectiveTypeParams(meta) {
  28799. // On the type side, remove newlines from the selector as it will need to fit into a TypeScript
  28800. // string literal, which must be on one line.
  28801. const selectorForType = meta.selector !== null ? meta.selector.replace(/\n/g, '') : null;
  28802. return [
  28803. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  28804. selectorForType !== null ? stringAsType(selectorForType) : NONE_TYPE,
  28805. meta.exportAs !== null ? stringArrayAsType(meta.exportAs) : NONE_TYPE,
  28806. expressionType(getInputsTypeExpression(meta)),
  28807. expressionType(stringMapAsLiteralExpression(meta.outputs)),
  28808. stringArrayAsType(meta.queries.map((q) => q.propertyName)),
  28809. ];
  28810. }
  28811. function getInputsTypeExpression(meta) {
  28812. return literalMap(Object.keys(meta.inputs).map((key) => {
  28813. const value = meta.inputs[key];
  28814. const values = [
  28815. { key: 'alias', value: literal$1(value.bindingPropertyName), quoted: true },
  28816. { key: 'required', value: literal$1(value.required), quoted: true },
  28817. ];
  28818. // TODO(legacy-partial-output-inputs): Consider always emitting this information,
  28819. // or leaving it as is.
  28820. if (value.isSignal) {
  28821. values.push({ key: 'isSignal', value: literal$1(value.isSignal), quoted: true });
  28822. }
  28823. return { key, value: literalMap(values), quoted: true };
  28824. }));
  28825. }
  28826. /**
  28827. * Creates the type specification from the directive meta. This type is inserted into .d.ts files
  28828. * to be consumed by upstream compilations.
  28829. */
  28830. function createDirectiveType(meta) {
  28831. const typeParams = createBaseDirectiveTypeParams(meta);
  28832. // Directives have no NgContentSelectors slot, but instead express a `never` type
  28833. // so that future fields align.
  28834. typeParams.push(NONE_TYPE);
  28835. typeParams.push(expressionType(literal$1(meta.isStandalone)));
  28836. typeParams.push(createHostDirectivesType(meta));
  28837. // TODO(signals): Always include this metadata starting with v17. Right
  28838. // now Angular v16.0.x does not support this field and library distributions
  28839. // would then be incompatible with v16.0.x framework users.
  28840. if (meta.isSignal) {
  28841. typeParams.push(expressionType(literal$1(meta.isSignal)));
  28842. }
  28843. return expressionType(importExpr(Identifiers.DirectiveDeclaration, typeParams));
  28844. }
  28845. // Return a host binding function or null if one is not necessary.
  28846. function createHostBindingsFunction(hostBindingsMetadata, typeSourceSpan, bindingParser, constantPool, selector, name, definitionMap) {
  28847. const bindings = bindingParser.createBoundHostProperties(hostBindingsMetadata.properties, typeSourceSpan);
  28848. // Calculate host event bindings
  28849. const eventBindings = bindingParser.createDirectiveHostEventAsts(hostBindingsMetadata.listeners, typeSourceSpan);
  28850. // The parser for host bindings treats class and style attributes specially -- they are
  28851. // extracted into these separate fields. This is not the case for templates, so the compiler can
  28852. // actually already handle these special attributes internally. Therefore, we just drop them
  28853. // into the attributes map.
  28854. if (hostBindingsMetadata.specialAttributes.styleAttr) {
  28855. hostBindingsMetadata.attributes['style'] = literal$1(hostBindingsMetadata.specialAttributes.styleAttr);
  28856. }
  28857. if (hostBindingsMetadata.specialAttributes.classAttr) {
  28858. hostBindingsMetadata.attributes['class'] = literal$1(hostBindingsMetadata.specialAttributes.classAttr);
  28859. }
  28860. const hostJob = ingestHostBinding({
  28861. componentName: name,
  28862. componentSelector: selector,
  28863. properties: bindings,
  28864. events: eventBindings,
  28865. attributes: hostBindingsMetadata.attributes,
  28866. }, bindingParser, constantPool);
  28867. transform(hostJob, CompilationJobKind.Host);
  28868. definitionMap.set('hostAttrs', hostJob.root.attributes);
  28869. const varCount = hostJob.root.vars;
  28870. if (varCount !== null && varCount > 0) {
  28871. definitionMap.set('hostVars', literal$1(varCount));
  28872. }
  28873. return emitHostBindingFunction(hostJob);
  28874. }
  28875. const HOST_REG_EXP = /^(?:\[([^\]]+)\])|(?:\(([^\)]+)\))$/;
  28876. function parseHostBindings(host) {
  28877. const attributes = {};
  28878. const listeners = {};
  28879. const properties = {};
  28880. const specialAttributes = {};
  28881. for (const key of Object.keys(host)) {
  28882. const value = host[key];
  28883. const matches = key.match(HOST_REG_EXP);
  28884. if (matches === null) {
  28885. switch (key) {
  28886. case 'class':
  28887. if (typeof value !== 'string') {
  28888. // TODO(alxhub): make this a diagnostic.
  28889. throw new Error(`Class binding must be string`);
  28890. }
  28891. specialAttributes.classAttr = value;
  28892. break;
  28893. case 'style':
  28894. if (typeof value !== 'string') {
  28895. // TODO(alxhub): make this a diagnostic.
  28896. throw new Error(`Style binding must be string`);
  28897. }
  28898. specialAttributes.styleAttr = value;
  28899. break;
  28900. default:
  28901. if (typeof value === 'string') {
  28902. attributes[key] = literal$1(value);
  28903. }
  28904. else {
  28905. attributes[key] = value;
  28906. }
  28907. }
  28908. }
  28909. else if (matches[1 /* HostBindingGroup.Binding */] != null) {
  28910. if (typeof value !== 'string') {
  28911. // TODO(alxhub): make this a diagnostic.
  28912. throw new Error(`Property binding must be string`);
  28913. }
  28914. // synthetic properties (the ones that have a `@` as a prefix)
  28915. // are still treated the same as regular properties. Therefore
  28916. // there is no point in storing them in a separate map.
  28917. properties[matches[1 /* HostBindingGroup.Binding */]] = value;
  28918. }
  28919. else if (matches[2 /* HostBindingGroup.Event */] != null) {
  28920. if (typeof value !== 'string') {
  28921. // TODO(alxhub): make this a diagnostic.
  28922. throw new Error(`Event binding must be string`);
  28923. }
  28924. listeners[matches[2 /* HostBindingGroup.Event */]] = value;
  28925. }
  28926. }
  28927. return { attributes, listeners, properties, specialAttributes };
  28928. }
  28929. /**
  28930. * Verifies host bindings and returns the list of errors (if any). Empty array indicates that a
  28931. * given set of host bindings has no errors.
  28932. *
  28933. * @param bindings set of host bindings to verify.
  28934. * @param sourceSpan source span where host bindings were defined.
  28935. * @returns array of errors associated with a given set of host bindings.
  28936. */
  28937. function verifyHostBindings(bindings, sourceSpan) {
  28938. // TODO: abstract out host bindings verification logic and use it instead of
  28939. // creating events and properties ASTs to detect errors (FW-996)
  28940. const bindingParser = makeBindingParser();
  28941. bindingParser.createDirectiveHostEventAsts(bindings.listeners, sourceSpan);
  28942. bindingParser.createBoundHostProperties(bindings.properties, sourceSpan);
  28943. return bindingParser.errors;
  28944. }
  28945. function compileStyles(styles, selector, hostSelector) {
  28946. const shadowCss = new ShadowCss();
  28947. return styles.map((style) => {
  28948. return shadowCss.shimCssText(style, selector, hostSelector);
  28949. });
  28950. }
  28951. function createHostDirectivesType(meta) {
  28952. if (!meta.hostDirectives?.length) {
  28953. return NONE_TYPE;
  28954. }
  28955. return expressionType(literalArr(meta.hostDirectives.map((hostMeta) => literalMap([
  28956. { key: 'directive', value: typeofExpr(hostMeta.directive.type), quoted: false },
  28957. {
  28958. key: 'inputs',
  28959. value: stringMapAsLiteralExpression(hostMeta.inputs || {}),
  28960. quoted: false,
  28961. },
  28962. {
  28963. key: 'outputs',
  28964. value: stringMapAsLiteralExpression(hostMeta.outputs || {}),
  28965. quoted: false,
  28966. },
  28967. ]))));
  28968. }
  28969. function createHostDirectivesFeatureArg(hostDirectives) {
  28970. const expressions = [];
  28971. let hasForwardRef = false;
  28972. for (const current of hostDirectives) {
  28973. // Use a shorthand if there are no inputs or outputs.
  28974. if (!current.inputs && !current.outputs) {
  28975. expressions.push(current.directive.type);
  28976. }
  28977. else {
  28978. const keys = [{ key: 'directive', value: current.directive.type, quoted: false }];
  28979. if (current.inputs) {
  28980. const inputsLiteral = createHostDirectivesMappingArray(current.inputs);
  28981. if (inputsLiteral) {
  28982. keys.push({ key: 'inputs', value: inputsLiteral, quoted: false });
  28983. }
  28984. }
  28985. if (current.outputs) {
  28986. const outputsLiteral = createHostDirectivesMappingArray(current.outputs);
  28987. if (outputsLiteral) {
  28988. keys.push({ key: 'outputs', value: outputsLiteral, quoted: false });
  28989. }
  28990. }
  28991. expressions.push(literalMap(keys));
  28992. }
  28993. if (current.isForwardReference) {
  28994. hasForwardRef = true;
  28995. }
  28996. }
  28997. // If there's a forward reference, we generate a `function() { return [HostDir] }`,
  28998. // otherwise we can save some bytes by using a plain array, e.g. `[HostDir]`.
  28999. return hasForwardRef
  29000. ? new FunctionExpr([], [new ReturnStatement(literalArr(expressions))])
  29001. : literalArr(expressions);
  29002. }
  29003. /**
  29004. * Converts an input/output mapping object literal into an array where the even keys are the
  29005. * public name of the binding and the odd ones are the name it was aliased to. E.g.
  29006. * `{inputOne: 'aliasOne', inputTwo: 'aliasTwo'}` will become
  29007. * `['inputOne', 'aliasOne', 'inputTwo', 'aliasTwo']`.
  29008. *
  29009. * This conversion is necessary, because hosts bind to the public name of the host directive and
  29010. * keeping the mapping in an object literal will break for apps using property renaming.
  29011. */
  29012. function createHostDirectivesMappingArray(mapping) {
  29013. const elements = [];
  29014. for (const publicName in mapping) {
  29015. if (mapping.hasOwnProperty(publicName)) {
  29016. elements.push(literal$1(publicName), literal$1(mapping[publicName]));
  29017. }
  29018. }
  29019. return elements.length > 0 ? literalArr(elements) : null;
  29020. }
  29021. /**
  29022. * Compiles the dependency resolver function for a defer block.
  29023. */
  29024. function compileDeferResolverFunction(meta) {
  29025. const depExpressions = [];
  29026. if (meta.mode === 0 /* DeferBlockDepsEmitMode.PerBlock */) {
  29027. for (const dep of meta.dependencies) {
  29028. if (dep.isDeferrable) {
  29029. // Callback function, e.g. `m () => m.MyCmp;`.
  29030. const innerFn = arrowFn(
  29031. // Default imports are always accessed through the `default` property.
  29032. [new FnParam('m', DYNAMIC_TYPE)], variable('m').prop(dep.isDefaultImport ? 'default' : dep.symbolName));
  29033. // Dynamic import, e.g. `import('./a').then(...)`.
  29034. const importExpr = new DynamicImportExpr(dep.importPath).prop('then').callFn([innerFn]);
  29035. depExpressions.push(importExpr);
  29036. }
  29037. else {
  29038. // Non-deferrable symbol, just use a reference to the type. Note that it's important to
  29039. // go through `typeReference`, rather than `symbolName` in order to preserve the
  29040. // original reference within the source file.
  29041. depExpressions.push(dep.typeReference);
  29042. }
  29043. }
  29044. }
  29045. else {
  29046. for (const { symbolName, importPath, isDefaultImport } of meta.dependencies) {
  29047. // Callback function, e.g. `m () => m.MyCmp;`.
  29048. const innerFn = arrowFn([new FnParam('m', DYNAMIC_TYPE)], variable('m').prop(isDefaultImport ? 'default' : symbolName));
  29049. // Dynamic import, e.g. `import('./a').then(...)`.
  29050. const importExpr = new DynamicImportExpr(importPath).prop('then').callFn([innerFn]);
  29051. depExpressions.push(importExpr);
  29052. }
  29053. }
  29054. return arrowFn([], literalArr(depExpressions));
  29055. }
  29056. /**
  29057. * Processes `Target`s with a given set of directives and performs a binding operation, which
  29058. * returns an object similar to TypeScript's `ts.TypeChecker` that contains knowledge about the
  29059. * target.
  29060. */
  29061. class R3TargetBinder {
  29062. directiveMatcher;
  29063. constructor(directiveMatcher) {
  29064. this.directiveMatcher = directiveMatcher;
  29065. }
  29066. /**
  29067. * Perform a binding operation on the given `Target` and return a `BoundTarget` which contains
  29068. * metadata about the types referenced in the template.
  29069. */
  29070. bind(target) {
  29071. if (!target.template) {
  29072. throw new Error('Empty bound targets are not supported');
  29073. }
  29074. const directives = new Map();
  29075. const eagerDirectives = [];
  29076. const bindings = new Map();
  29077. const references = new Map();
  29078. const scopedNodeEntities = new Map();
  29079. const expressions = new Map();
  29080. const symbols = new Map();
  29081. const nestingLevel = new Map();
  29082. const usedPipes = new Set();
  29083. const eagerPipes = new Set();
  29084. const deferBlocks = [];
  29085. if (target.template) {
  29086. // First, parse the template into a `Scope` structure. This operation captures the syntactic
  29087. // scopes in the template and makes them available for later use.
  29088. const scope = Scope$1.apply(target.template);
  29089. // Use the `Scope` to extract the entities present at every level of the template.
  29090. extractScopedNodeEntities(scope, scopedNodeEntities);
  29091. // Next, perform directive matching on the template using the `DirectiveBinder`. This returns:
  29092. // - directives: Map of nodes (elements & ng-templates) to the directives on them.
  29093. // - bindings: Map of inputs, outputs, and attributes to the directive/element that claims
  29094. // them. TODO(alxhub): handle multiple directives claiming an input/output/etc.
  29095. // - references: Map of #references to their targets.
  29096. DirectiveBinder.apply(target.template, this.directiveMatcher, directives, eagerDirectives, bindings, references);
  29097. // Finally, run the TemplateBinder to bind references, variables, and other entities within the
  29098. // template. This extracts all the metadata that doesn't depend on directive matching.
  29099. TemplateBinder.applyWithScope(target.template, scope, expressions, symbols, nestingLevel, usedPipes, eagerPipes, deferBlocks);
  29100. }
  29101. return new R3BoundTarget(target, directives, eagerDirectives, bindings, references, expressions, symbols, nestingLevel, scopedNodeEntities, usedPipes, eagerPipes, deferBlocks);
  29102. }
  29103. }
  29104. /**
  29105. * Represents a binding scope within a template.
  29106. *
  29107. * Any variables, references, or other named entities declared within the template will
  29108. * be captured and available by name in `namedEntities`. Additionally, child templates will
  29109. * be analyzed and have their child `Scope`s available in `childScopes`.
  29110. */
  29111. let Scope$1 = class Scope {
  29112. parentScope;
  29113. rootNode;
  29114. /**
  29115. * Named members of the `Scope`, such as `Reference`s or `Variable`s.
  29116. */
  29117. namedEntities = new Map();
  29118. /**
  29119. * Set of elements that belong to this scope.
  29120. */
  29121. elementsInScope = new Set();
  29122. /**
  29123. * Child `Scope`s for immediately nested `ScopedNode`s.
  29124. */
  29125. childScopes = new Map();
  29126. /** Whether this scope is deferred or if any of its ancestors are deferred. */
  29127. isDeferred;
  29128. constructor(parentScope, rootNode) {
  29129. this.parentScope = parentScope;
  29130. this.rootNode = rootNode;
  29131. this.isDeferred =
  29132. parentScope !== null && parentScope.isDeferred ? true : rootNode instanceof DeferredBlock;
  29133. }
  29134. static newRootScope() {
  29135. return new Scope(null, null);
  29136. }
  29137. /**
  29138. * Process a template (either as a `Template` sub-template with variables, or a plain array of
  29139. * template `Node`s) and construct its `Scope`.
  29140. */
  29141. static apply(template) {
  29142. const scope = Scope.newRootScope();
  29143. scope.ingest(template);
  29144. return scope;
  29145. }
  29146. /**
  29147. * Internal method to process the scoped node and populate the `Scope`.
  29148. */
  29149. ingest(nodeOrNodes) {
  29150. if (nodeOrNodes instanceof Template) {
  29151. // Variables on an <ng-template> are defined in the inner scope.
  29152. nodeOrNodes.variables.forEach((node) => this.visitVariable(node));
  29153. // Process the nodes of the template.
  29154. nodeOrNodes.children.forEach((node) => node.visit(this));
  29155. }
  29156. else if (nodeOrNodes instanceof IfBlockBranch) {
  29157. if (nodeOrNodes.expressionAlias !== null) {
  29158. this.visitVariable(nodeOrNodes.expressionAlias);
  29159. }
  29160. nodeOrNodes.children.forEach((node) => node.visit(this));
  29161. }
  29162. else if (nodeOrNodes instanceof ForLoopBlock) {
  29163. this.visitVariable(nodeOrNodes.item);
  29164. nodeOrNodes.contextVariables.forEach((v) => this.visitVariable(v));
  29165. nodeOrNodes.children.forEach((node) => node.visit(this));
  29166. }
  29167. else if (nodeOrNodes instanceof SwitchBlockCase ||
  29168. nodeOrNodes instanceof ForLoopBlockEmpty ||
  29169. nodeOrNodes instanceof DeferredBlock ||
  29170. nodeOrNodes instanceof DeferredBlockError ||
  29171. nodeOrNodes instanceof DeferredBlockPlaceholder ||
  29172. nodeOrNodes instanceof DeferredBlockLoading ||
  29173. nodeOrNodes instanceof Content) {
  29174. nodeOrNodes.children.forEach((node) => node.visit(this));
  29175. }
  29176. else {
  29177. // No overarching `Template` instance, so process the nodes directly.
  29178. nodeOrNodes.forEach((node) => node.visit(this));
  29179. }
  29180. }
  29181. visitElement(element) {
  29182. // `Element`s in the template may have `Reference`s which are captured in the scope.
  29183. element.references.forEach((node) => this.visitReference(node));
  29184. // Recurse into the `Element`'s children.
  29185. element.children.forEach((node) => node.visit(this));
  29186. this.elementsInScope.add(element);
  29187. }
  29188. visitTemplate(template) {
  29189. // References on a <ng-template> are defined in the outer scope, so capture them before
  29190. // processing the template's child scope.
  29191. template.references.forEach((node) => this.visitReference(node));
  29192. // Next, create an inner scope and process the template within it.
  29193. this.ingestScopedNode(template);
  29194. }
  29195. visitVariable(variable) {
  29196. // Declare the variable if it's not already.
  29197. this.maybeDeclare(variable);
  29198. }
  29199. visitReference(reference) {
  29200. // Declare the variable if it's not already.
  29201. this.maybeDeclare(reference);
  29202. }
  29203. visitDeferredBlock(deferred) {
  29204. this.ingestScopedNode(deferred);
  29205. deferred.placeholder?.visit(this);
  29206. deferred.loading?.visit(this);
  29207. deferred.error?.visit(this);
  29208. }
  29209. visitDeferredBlockPlaceholder(block) {
  29210. this.ingestScopedNode(block);
  29211. }
  29212. visitDeferredBlockError(block) {
  29213. this.ingestScopedNode(block);
  29214. }
  29215. visitDeferredBlockLoading(block) {
  29216. this.ingestScopedNode(block);
  29217. }
  29218. visitSwitchBlock(block) {
  29219. block.cases.forEach((node) => node.visit(this));
  29220. }
  29221. visitSwitchBlockCase(block) {
  29222. this.ingestScopedNode(block);
  29223. }
  29224. visitForLoopBlock(block) {
  29225. this.ingestScopedNode(block);
  29226. block.empty?.visit(this);
  29227. }
  29228. visitForLoopBlockEmpty(block) {
  29229. this.ingestScopedNode(block);
  29230. }
  29231. visitIfBlock(block) {
  29232. block.branches.forEach((node) => node.visit(this));
  29233. }
  29234. visitIfBlockBranch(block) {
  29235. this.ingestScopedNode(block);
  29236. }
  29237. visitContent(content) {
  29238. this.ingestScopedNode(content);
  29239. }
  29240. visitLetDeclaration(decl) {
  29241. this.maybeDeclare(decl);
  29242. }
  29243. // Unused visitors.
  29244. visitBoundAttribute(attr) { }
  29245. visitBoundEvent(event) { }
  29246. visitBoundText(text) { }
  29247. visitText(text) { }
  29248. visitTextAttribute(attr) { }
  29249. visitIcu(icu) { }
  29250. visitDeferredTrigger(trigger) { }
  29251. visitUnknownBlock(block) { }
  29252. maybeDeclare(thing) {
  29253. // Declare something with a name, as long as that name isn't taken.
  29254. if (!this.namedEntities.has(thing.name)) {
  29255. this.namedEntities.set(thing.name, thing);
  29256. }
  29257. }
  29258. /**
  29259. * Look up a variable within this `Scope`.
  29260. *
  29261. * This can recurse into a parent `Scope` if it's available.
  29262. */
  29263. lookup(name) {
  29264. if (this.namedEntities.has(name)) {
  29265. // Found in the local scope.
  29266. return this.namedEntities.get(name);
  29267. }
  29268. else if (this.parentScope !== null) {
  29269. // Not in the local scope, but there's a parent scope so check there.
  29270. return this.parentScope.lookup(name);
  29271. }
  29272. else {
  29273. // At the top level and it wasn't found.
  29274. return null;
  29275. }
  29276. }
  29277. /**
  29278. * Get the child scope for a `ScopedNode`.
  29279. *
  29280. * This should always be defined.
  29281. */
  29282. getChildScope(node) {
  29283. const res = this.childScopes.get(node);
  29284. if (res === undefined) {
  29285. throw new Error(`Assertion error: child scope for ${node} not found`);
  29286. }
  29287. return res;
  29288. }
  29289. ingestScopedNode(node) {
  29290. const scope = new Scope(this, node);
  29291. scope.ingest(node);
  29292. this.childScopes.set(node, scope);
  29293. }
  29294. };
  29295. /**
  29296. * Processes a template and matches directives on nodes (elements and templates).
  29297. *
  29298. * Usually used via the static `apply()` method.
  29299. */
  29300. class DirectiveBinder {
  29301. matcher;
  29302. directives;
  29303. eagerDirectives;
  29304. bindings;
  29305. references;
  29306. // Indicates whether we are visiting elements within a `defer` block
  29307. isInDeferBlock = false;
  29308. constructor(matcher, directives, eagerDirectives, bindings, references) {
  29309. this.matcher = matcher;
  29310. this.directives = directives;
  29311. this.eagerDirectives = eagerDirectives;
  29312. this.bindings = bindings;
  29313. this.references = references;
  29314. }
  29315. /**
  29316. * Process a template (list of `Node`s) and perform directive matching against each node.
  29317. *
  29318. * @param template the list of template `Node`s to match (recursively).
  29319. * @param selectorMatcher a `SelectorMatcher` containing the directives that are in scope for
  29320. * this template.
  29321. * @returns three maps which contain information about directives in the template: the
  29322. * `directives` map which lists directives matched on each node, the `bindings` map which
  29323. * indicates which directives claimed which bindings (inputs, outputs, etc), and the `references`
  29324. * map which resolves #references (`Reference`s) within the template to the named directive or
  29325. * template node.
  29326. */
  29327. static apply(template, selectorMatcher, directives, eagerDirectives, bindings, references) {
  29328. const matcher = new DirectiveBinder(selectorMatcher, directives, eagerDirectives, bindings, references);
  29329. matcher.ingest(template);
  29330. }
  29331. ingest(template) {
  29332. template.forEach((node) => node.visit(this));
  29333. }
  29334. visitElement(element) {
  29335. this.visitElementOrTemplate(element);
  29336. }
  29337. visitTemplate(template) {
  29338. this.visitElementOrTemplate(template);
  29339. }
  29340. visitElementOrTemplate(node) {
  29341. // First, determine the HTML shape of the node for the purpose of directive matching.
  29342. // Do this by building up a `CssSelector` for the node.
  29343. const cssSelector = createCssSelectorFromNode(node);
  29344. // Next, use the `SelectorMatcher` to get the list of directives on the node.
  29345. const directives = [];
  29346. this.matcher.match(cssSelector, (_selector, results) => directives.push(...results));
  29347. if (directives.length > 0) {
  29348. this.directives.set(node, directives);
  29349. if (!this.isInDeferBlock) {
  29350. this.eagerDirectives.push(...directives);
  29351. }
  29352. }
  29353. // Resolve any references that are created on this node.
  29354. node.references.forEach((ref) => {
  29355. let dirTarget = null;
  29356. // If the reference expression is empty, then it matches the "primary" directive on the node
  29357. // (if there is one). Otherwise it matches the host node itself (either an element or
  29358. // <ng-template> node).
  29359. if (ref.value.trim() === '') {
  29360. // This could be a reference to a component if there is one.
  29361. dirTarget = directives.find((dir) => dir.isComponent) || null;
  29362. }
  29363. else {
  29364. // This should be a reference to a directive exported via exportAs.
  29365. dirTarget =
  29366. directives.find((dir) => dir.exportAs !== null && dir.exportAs.some((value) => value === ref.value)) || null;
  29367. // Check if a matching directive was found.
  29368. if (dirTarget === null) {
  29369. // No matching directive was found - this reference points to an unknown target. Leave it
  29370. // unmapped.
  29371. return;
  29372. }
  29373. }
  29374. if (dirTarget !== null) {
  29375. // This reference points to a directive.
  29376. this.references.set(ref, { directive: dirTarget, node });
  29377. }
  29378. else {
  29379. // This reference points to the node itself.
  29380. this.references.set(ref, node);
  29381. }
  29382. });
  29383. const setAttributeBinding = (attribute, ioType) => {
  29384. const dir = directives.find((dir) => dir[ioType].hasBindingPropertyName(attribute.name));
  29385. const binding = dir !== undefined ? dir : node;
  29386. this.bindings.set(attribute, binding);
  29387. };
  29388. // Node inputs (bound attributes) and text attributes can be bound to an
  29389. // input on a directive.
  29390. node.inputs.forEach((input) => setAttributeBinding(input, 'inputs'));
  29391. node.attributes.forEach((attr) => setAttributeBinding(attr, 'inputs'));
  29392. if (node instanceof Template) {
  29393. node.templateAttrs.forEach((attr) => setAttributeBinding(attr, 'inputs'));
  29394. }
  29395. // Node outputs (bound events) can be bound to an output on a directive.
  29396. node.outputs.forEach((output) => setAttributeBinding(output, 'outputs'));
  29397. // Recurse into the node's children.
  29398. node.children.forEach((child) => child.visit(this));
  29399. }
  29400. visitDeferredBlock(deferred) {
  29401. const wasInDeferBlock = this.isInDeferBlock;
  29402. this.isInDeferBlock = true;
  29403. deferred.children.forEach((child) => child.visit(this));
  29404. this.isInDeferBlock = wasInDeferBlock;
  29405. deferred.placeholder?.visit(this);
  29406. deferred.loading?.visit(this);
  29407. deferred.error?.visit(this);
  29408. }
  29409. visitDeferredBlockPlaceholder(block) {
  29410. block.children.forEach((child) => child.visit(this));
  29411. }
  29412. visitDeferredBlockError(block) {
  29413. block.children.forEach((child) => child.visit(this));
  29414. }
  29415. visitDeferredBlockLoading(block) {
  29416. block.children.forEach((child) => child.visit(this));
  29417. }
  29418. visitSwitchBlock(block) {
  29419. block.cases.forEach((node) => node.visit(this));
  29420. }
  29421. visitSwitchBlockCase(block) {
  29422. block.children.forEach((node) => node.visit(this));
  29423. }
  29424. visitForLoopBlock(block) {
  29425. block.item.visit(this);
  29426. block.contextVariables.forEach((v) => v.visit(this));
  29427. block.children.forEach((node) => node.visit(this));
  29428. block.empty?.visit(this);
  29429. }
  29430. visitForLoopBlockEmpty(block) {
  29431. block.children.forEach((node) => node.visit(this));
  29432. }
  29433. visitIfBlock(block) {
  29434. block.branches.forEach((node) => node.visit(this));
  29435. }
  29436. visitIfBlockBranch(block) {
  29437. block.expressionAlias?.visit(this);
  29438. block.children.forEach((node) => node.visit(this));
  29439. }
  29440. visitContent(content) {
  29441. content.children.forEach((child) => child.visit(this));
  29442. }
  29443. // Unused visitors.
  29444. visitVariable(variable) { }
  29445. visitReference(reference) { }
  29446. visitTextAttribute(attribute) { }
  29447. visitBoundAttribute(attribute) { }
  29448. visitBoundEvent(attribute) { }
  29449. visitBoundAttributeOrEvent(node) { }
  29450. visitText(text) { }
  29451. visitBoundText(text) { }
  29452. visitIcu(icu) { }
  29453. visitDeferredTrigger(trigger) { }
  29454. visitUnknownBlock(block) { }
  29455. visitLetDeclaration(decl) { }
  29456. }
  29457. /**
  29458. * Processes a template and extract metadata about expressions and symbols within.
  29459. *
  29460. * This is a companion to the `DirectiveBinder` that doesn't require knowledge of directives matched
  29461. * within the template in order to operate.
  29462. *
  29463. * Expressions are visited by the superclass `RecursiveAstVisitor`, with custom logic provided
  29464. * by overridden methods from that visitor.
  29465. */
  29466. class TemplateBinder extends RecursiveAstVisitor {
  29467. bindings;
  29468. symbols;
  29469. usedPipes;
  29470. eagerPipes;
  29471. deferBlocks;
  29472. nestingLevel;
  29473. scope;
  29474. rootNode;
  29475. level;
  29476. visitNode;
  29477. constructor(bindings, symbols, usedPipes, eagerPipes, deferBlocks, nestingLevel, scope, rootNode, level) {
  29478. super();
  29479. this.bindings = bindings;
  29480. this.symbols = symbols;
  29481. this.usedPipes = usedPipes;
  29482. this.eagerPipes = eagerPipes;
  29483. this.deferBlocks = deferBlocks;
  29484. this.nestingLevel = nestingLevel;
  29485. this.scope = scope;
  29486. this.rootNode = rootNode;
  29487. this.level = level;
  29488. // Save a bit of processing time by constructing this closure in advance.
  29489. this.visitNode = (node) => node.visit(this);
  29490. }
  29491. // This method is defined to reconcile the type of TemplateBinder since both
  29492. // RecursiveAstVisitor and Visitor define the visit() method in their
  29493. // interfaces.
  29494. visit(node, context) {
  29495. if (node instanceof AST) {
  29496. node.visit(this, context);
  29497. }
  29498. else {
  29499. node.visit(this);
  29500. }
  29501. }
  29502. /**
  29503. * Process a template and extract metadata about expressions and symbols within.
  29504. *
  29505. * @param nodes the nodes of the template to process
  29506. * @param scope the `Scope` of the template being processed.
  29507. * @returns three maps which contain metadata about the template: `expressions` which interprets
  29508. * special `AST` nodes in expressions as pointing to references or variables declared within the
  29509. * template, `symbols` which maps those variables and references to the nested `Template` which
  29510. * declares them, if any, and `nestingLevel` which associates each `Template` with a integer
  29511. * nesting level (how many levels deep within the template structure the `Template` is), starting
  29512. * at 1.
  29513. */
  29514. static applyWithScope(nodes, scope, expressions, symbols, nestingLevel, usedPipes, eagerPipes, deferBlocks) {
  29515. const template = nodes instanceof Template ? nodes : null;
  29516. // The top-level template has nesting level 0.
  29517. const binder = new TemplateBinder(expressions, symbols, usedPipes, eagerPipes, deferBlocks, nestingLevel, scope, template, 0);
  29518. binder.ingest(nodes);
  29519. }
  29520. ingest(nodeOrNodes) {
  29521. if (nodeOrNodes instanceof Template) {
  29522. // For <ng-template>s, process only variables and child nodes. Inputs, outputs, templateAttrs,
  29523. // and references were all processed in the scope of the containing template.
  29524. nodeOrNodes.variables.forEach(this.visitNode);
  29525. nodeOrNodes.children.forEach(this.visitNode);
  29526. // Set the nesting level.
  29527. this.nestingLevel.set(nodeOrNodes, this.level);
  29528. }
  29529. else if (nodeOrNodes instanceof IfBlockBranch) {
  29530. if (nodeOrNodes.expressionAlias !== null) {
  29531. this.visitNode(nodeOrNodes.expressionAlias);
  29532. }
  29533. nodeOrNodes.children.forEach(this.visitNode);
  29534. this.nestingLevel.set(nodeOrNodes, this.level);
  29535. }
  29536. else if (nodeOrNodes instanceof ForLoopBlock) {
  29537. this.visitNode(nodeOrNodes.item);
  29538. nodeOrNodes.contextVariables.forEach((v) => this.visitNode(v));
  29539. nodeOrNodes.trackBy.visit(this);
  29540. nodeOrNodes.children.forEach(this.visitNode);
  29541. this.nestingLevel.set(nodeOrNodes, this.level);
  29542. }
  29543. else if (nodeOrNodes instanceof DeferredBlock) {
  29544. if (this.scope.rootNode !== nodeOrNodes) {
  29545. throw new Error(`Assertion error: resolved incorrect scope for deferred block ${nodeOrNodes}`);
  29546. }
  29547. this.deferBlocks.push([nodeOrNodes, this.scope]);
  29548. nodeOrNodes.children.forEach((node) => node.visit(this));
  29549. this.nestingLevel.set(nodeOrNodes, this.level);
  29550. }
  29551. else if (nodeOrNodes instanceof SwitchBlockCase ||
  29552. nodeOrNodes instanceof ForLoopBlockEmpty ||
  29553. nodeOrNodes instanceof DeferredBlockError ||
  29554. nodeOrNodes instanceof DeferredBlockPlaceholder ||
  29555. nodeOrNodes instanceof DeferredBlockLoading ||
  29556. nodeOrNodes instanceof Content) {
  29557. nodeOrNodes.children.forEach((node) => node.visit(this));
  29558. this.nestingLevel.set(nodeOrNodes, this.level);
  29559. }
  29560. else {
  29561. // Visit each node from the top-level template.
  29562. nodeOrNodes.forEach(this.visitNode);
  29563. }
  29564. }
  29565. visitElement(element) {
  29566. // Visit the inputs, outputs, and children of the element.
  29567. element.inputs.forEach(this.visitNode);
  29568. element.outputs.forEach(this.visitNode);
  29569. element.children.forEach(this.visitNode);
  29570. element.references.forEach(this.visitNode);
  29571. }
  29572. visitTemplate(template) {
  29573. // First, visit inputs, outputs and template attributes of the template node.
  29574. template.inputs.forEach(this.visitNode);
  29575. template.outputs.forEach(this.visitNode);
  29576. template.templateAttrs.forEach(this.visitNode);
  29577. template.references.forEach(this.visitNode);
  29578. // Next, recurse into the template.
  29579. this.ingestScopedNode(template);
  29580. }
  29581. visitVariable(variable) {
  29582. // Register the `Variable` as a symbol in the current `Template`.
  29583. if (this.rootNode !== null) {
  29584. this.symbols.set(variable, this.rootNode);
  29585. }
  29586. }
  29587. visitReference(reference) {
  29588. // Register the `Reference` as a symbol in the current `Template`.
  29589. if (this.rootNode !== null) {
  29590. this.symbols.set(reference, this.rootNode);
  29591. }
  29592. }
  29593. // Unused template visitors
  29594. visitText(text) { }
  29595. visitTextAttribute(attribute) { }
  29596. visitUnknownBlock(block) { }
  29597. visitDeferredTrigger() { }
  29598. visitIcu(icu) {
  29599. Object.keys(icu.vars).forEach((key) => icu.vars[key].visit(this));
  29600. Object.keys(icu.placeholders).forEach((key) => icu.placeholders[key].visit(this));
  29601. }
  29602. // The remaining visitors are concerned with processing AST expressions within template bindings
  29603. visitBoundAttribute(attribute) {
  29604. attribute.value.visit(this);
  29605. }
  29606. visitBoundEvent(event) {
  29607. event.handler.visit(this);
  29608. }
  29609. visitDeferredBlock(deferred) {
  29610. this.ingestScopedNode(deferred);
  29611. deferred.triggers.when?.value.visit(this);
  29612. deferred.prefetchTriggers.when?.value.visit(this);
  29613. deferred.hydrateTriggers.when?.value.visit(this);
  29614. deferred.hydrateTriggers.never?.visit(this);
  29615. deferred.placeholder && this.visitNode(deferred.placeholder);
  29616. deferred.loading && this.visitNode(deferred.loading);
  29617. deferred.error && this.visitNode(deferred.error);
  29618. }
  29619. visitDeferredBlockPlaceholder(block) {
  29620. this.ingestScopedNode(block);
  29621. }
  29622. visitDeferredBlockError(block) {
  29623. this.ingestScopedNode(block);
  29624. }
  29625. visitDeferredBlockLoading(block) {
  29626. this.ingestScopedNode(block);
  29627. }
  29628. visitSwitchBlock(block) {
  29629. block.expression.visit(this);
  29630. block.cases.forEach(this.visitNode);
  29631. }
  29632. visitSwitchBlockCase(block) {
  29633. block.expression?.visit(this);
  29634. this.ingestScopedNode(block);
  29635. }
  29636. visitForLoopBlock(block) {
  29637. block.expression.visit(this);
  29638. this.ingestScopedNode(block);
  29639. block.empty?.visit(this);
  29640. }
  29641. visitForLoopBlockEmpty(block) {
  29642. this.ingestScopedNode(block);
  29643. }
  29644. visitIfBlock(block) {
  29645. block.branches.forEach((node) => node.visit(this));
  29646. }
  29647. visitIfBlockBranch(block) {
  29648. block.expression?.visit(this);
  29649. this.ingestScopedNode(block);
  29650. }
  29651. visitContent(content) {
  29652. this.ingestScopedNode(content);
  29653. }
  29654. visitBoundText(text) {
  29655. text.value.visit(this);
  29656. }
  29657. visitLetDeclaration(decl) {
  29658. decl.value.visit(this);
  29659. if (this.rootNode !== null) {
  29660. this.symbols.set(decl, this.rootNode);
  29661. }
  29662. }
  29663. visitPipe(ast, context) {
  29664. this.usedPipes.add(ast.name);
  29665. if (!this.scope.isDeferred) {
  29666. this.eagerPipes.add(ast.name);
  29667. }
  29668. return super.visitPipe(ast, context);
  29669. }
  29670. // These five types of AST expressions can refer to expression roots, which could be variables
  29671. // or references in the current scope.
  29672. visitPropertyRead(ast, context) {
  29673. this.maybeMap(ast, ast.name);
  29674. return super.visitPropertyRead(ast, context);
  29675. }
  29676. visitSafePropertyRead(ast, context) {
  29677. this.maybeMap(ast, ast.name);
  29678. return super.visitSafePropertyRead(ast, context);
  29679. }
  29680. visitPropertyWrite(ast, context) {
  29681. this.maybeMap(ast, ast.name);
  29682. return super.visitPropertyWrite(ast, context);
  29683. }
  29684. ingestScopedNode(node) {
  29685. const childScope = this.scope.getChildScope(node);
  29686. const binder = new TemplateBinder(this.bindings, this.symbols, this.usedPipes, this.eagerPipes, this.deferBlocks, this.nestingLevel, childScope, node, this.level + 1);
  29687. binder.ingest(node);
  29688. }
  29689. maybeMap(ast, name) {
  29690. // If the receiver of the expression isn't the `ImplicitReceiver`, this isn't the root of an
  29691. // `AST` expression that maps to a `Variable` or `Reference`.
  29692. if (!(ast.receiver instanceof ImplicitReceiver) || ast.receiver instanceof ThisReceiver) {
  29693. return;
  29694. }
  29695. // Check whether the name exists in the current scope. If so, map it. Otherwise, the name is
  29696. // probably a property on the top-level component context.
  29697. const target = this.scope.lookup(name);
  29698. if (target !== null) {
  29699. this.bindings.set(ast, target);
  29700. }
  29701. }
  29702. }
  29703. /**
  29704. * Metadata container for a `Target` that allows queries for specific bits of metadata.
  29705. *
  29706. * See `BoundTarget` for documentation on the individual methods.
  29707. */
  29708. class R3BoundTarget {
  29709. target;
  29710. directives;
  29711. eagerDirectives;
  29712. bindings;
  29713. references;
  29714. exprTargets;
  29715. symbols;
  29716. nestingLevel;
  29717. scopedNodeEntities;
  29718. usedPipes;
  29719. eagerPipes;
  29720. /** Deferred blocks, ordered as they appear in the template. */
  29721. deferredBlocks;
  29722. /** Map of deferred blocks to their scope. */
  29723. deferredScopes;
  29724. constructor(target, directives, eagerDirectives, bindings, references, exprTargets, symbols, nestingLevel, scopedNodeEntities, usedPipes, eagerPipes, rawDeferred) {
  29725. this.target = target;
  29726. this.directives = directives;
  29727. this.eagerDirectives = eagerDirectives;
  29728. this.bindings = bindings;
  29729. this.references = references;
  29730. this.exprTargets = exprTargets;
  29731. this.symbols = symbols;
  29732. this.nestingLevel = nestingLevel;
  29733. this.scopedNodeEntities = scopedNodeEntities;
  29734. this.usedPipes = usedPipes;
  29735. this.eagerPipes = eagerPipes;
  29736. this.deferredBlocks = rawDeferred.map((current) => current[0]);
  29737. this.deferredScopes = new Map(rawDeferred);
  29738. }
  29739. getEntitiesInScope(node) {
  29740. return this.scopedNodeEntities.get(node) ?? new Set();
  29741. }
  29742. getDirectivesOfNode(node) {
  29743. return this.directives.get(node) || null;
  29744. }
  29745. getReferenceTarget(ref) {
  29746. return this.references.get(ref) || null;
  29747. }
  29748. getConsumerOfBinding(binding) {
  29749. return this.bindings.get(binding) || null;
  29750. }
  29751. getExpressionTarget(expr) {
  29752. return this.exprTargets.get(expr) || null;
  29753. }
  29754. getDefinitionNodeOfSymbol(symbol) {
  29755. return this.symbols.get(symbol) || null;
  29756. }
  29757. getNestingLevel(node) {
  29758. return this.nestingLevel.get(node) || 0;
  29759. }
  29760. getUsedDirectives() {
  29761. const set = new Set();
  29762. this.directives.forEach((dirs) => dirs.forEach((dir) => set.add(dir)));
  29763. return Array.from(set.values());
  29764. }
  29765. getEagerlyUsedDirectives() {
  29766. const set = new Set(this.eagerDirectives);
  29767. return Array.from(set.values());
  29768. }
  29769. getUsedPipes() {
  29770. return Array.from(this.usedPipes);
  29771. }
  29772. getEagerlyUsedPipes() {
  29773. return Array.from(this.eagerPipes);
  29774. }
  29775. getDeferBlocks() {
  29776. return this.deferredBlocks;
  29777. }
  29778. getDeferredTriggerTarget(block, trigger) {
  29779. // Only triggers that refer to DOM nodes can be resolved.
  29780. if (!(trigger instanceof InteractionDeferredTrigger) &&
  29781. !(trigger instanceof ViewportDeferredTrigger) &&
  29782. !(trigger instanceof HoverDeferredTrigger)) {
  29783. return null;
  29784. }
  29785. const name = trigger.reference;
  29786. if (name === null) {
  29787. let trigger = null;
  29788. if (block.placeholder !== null) {
  29789. for (const child of block.placeholder.children) {
  29790. // Skip over comment nodes. Currently by default the template parser doesn't capture
  29791. // comments, but we have a safeguard here just in case since it can be enabled.
  29792. if (child instanceof Comment$1) {
  29793. continue;
  29794. }
  29795. // We can only infer the trigger if there's one root element node. Any other
  29796. // nodes at the root make it so that we can't infer the trigger anymore.
  29797. if (trigger !== null) {
  29798. return null;
  29799. }
  29800. if (child instanceof Element$1) {
  29801. trigger = child;
  29802. }
  29803. }
  29804. }
  29805. return trigger;
  29806. }
  29807. const outsideRef = this.findEntityInScope(block, name);
  29808. // First try to resolve the target in the scope of the main deferred block. Note that we
  29809. // skip triggers defined inside the main block itself, because they might not exist yet.
  29810. if (outsideRef instanceof Reference$1 && this.getDefinitionNodeOfSymbol(outsideRef) !== block) {
  29811. const target = this.getReferenceTarget(outsideRef);
  29812. if (target !== null) {
  29813. return this.referenceTargetToElement(target);
  29814. }
  29815. }
  29816. // If the trigger couldn't be found in the main block, check the
  29817. // placeholder block which is shown before the main block has loaded.
  29818. if (block.placeholder !== null) {
  29819. const refInPlaceholder = this.findEntityInScope(block.placeholder, name);
  29820. const targetInPlaceholder = refInPlaceholder instanceof Reference$1 ? this.getReferenceTarget(refInPlaceholder) : null;
  29821. if (targetInPlaceholder !== null) {
  29822. return this.referenceTargetToElement(targetInPlaceholder);
  29823. }
  29824. }
  29825. return null;
  29826. }
  29827. isDeferred(element) {
  29828. for (const block of this.deferredBlocks) {
  29829. if (!this.deferredScopes.has(block)) {
  29830. continue;
  29831. }
  29832. const stack = [this.deferredScopes.get(block)];
  29833. while (stack.length > 0) {
  29834. const current = stack.pop();
  29835. if (current.elementsInScope.has(element)) {
  29836. return true;
  29837. }
  29838. stack.push(...current.childScopes.values());
  29839. }
  29840. }
  29841. return false;
  29842. }
  29843. /**
  29844. * Finds an entity with a specific name in a scope.
  29845. * @param rootNode Root node of the scope.
  29846. * @param name Name of the entity.
  29847. */
  29848. findEntityInScope(rootNode, name) {
  29849. const entities = this.getEntitiesInScope(rootNode);
  29850. for (const entity of entities) {
  29851. if (entity.name === name) {
  29852. return entity;
  29853. }
  29854. }
  29855. return null;
  29856. }
  29857. /** Coerces a `ReferenceTarget` to an `Element`, if possible. */
  29858. referenceTargetToElement(target) {
  29859. if (target instanceof Element$1) {
  29860. return target;
  29861. }
  29862. if (target instanceof Template) {
  29863. return null;
  29864. }
  29865. return this.referenceTargetToElement(target.node);
  29866. }
  29867. }
  29868. function extractScopedNodeEntities(rootScope, templateEntities) {
  29869. const entityMap = new Map();
  29870. function extractScopeEntities(scope) {
  29871. if (entityMap.has(scope.rootNode)) {
  29872. return entityMap.get(scope.rootNode);
  29873. }
  29874. const currentEntities = scope.namedEntities;
  29875. let entities;
  29876. if (scope.parentScope !== null) {
  29877. entities = new Map([...extractScopeEntities(scope.parentScope), ...currentEntities]);
  29878. }
  29879. else {
  29880. entities = new Map(currentEntities);
  29881. }
  29882. entityMap.set(scope.rootNode, entities);
  29883. return entities;
  29884. }
  29885. const scopesToProcess = [rootScope];
  29886. while (scopesToProcess.length > 0) {
  29887. const scope = scopesToProcess.pop();
  29888. for (const childScope of scope.childScopes.values()) {
  29889. scopesToProcess.push(childScope);
  29890. }
  29891. extractScopeEntities(scope);
  29892. }
  29893. for (const [template, entities] of entityMap) {
  29894. templateEntities.set(template, new Set(entities.values()));
  29895. }
  29896. }
  29897. /**
  29898. * An interface for retrieving documents by URL that the compiler uses to
  29899. * load templates.
  29900. *
  29901. * This is an abstract class, rather than an interface, so that it can be used
  29902. * as injection token.
  29903. */
  29904. class ResourceLoader {
  29905. }
  29906. class CompilerFacadeImpl {
  29907. jitEvaluator;
  29908. FactoryTarget = exports.FactoryTarget;
  29909. ResourceLoader = ResourceLoader;
  29910. elementSchemaRegistry = new DomElementSchemaRegistry();
  29911. constructor(jitEvaluator = new JitEvaluator()) {
  29912. this.jitEvaluator = jitEvaluator;
  29913. }
  29914. compilePipe(angularCoreEnv, sourceMapUrl, facade) {
  29915. const metadata = {
  29916. name: facade.name,
  29917. type: wrapReference(facade.type),
  29918. typeArgumentCount: 0,
  29919. pipeName: facade.pipeName,
  29920. pure: facade.pure,
  29921. isStandalone: facade.isStandalone,
  29922. };
  29923. const res = compilePipeFromMetadata(metadata);
  29924. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  29925. }
  29926. compilePipeDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  29927. const meta = convertDeclarePipeFacadeToMetadata(declaration);
  29928. const res = compilePipeFromMetadata(meta);
  29929. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  29930. }
  29931. compileInjectable(angularCoreEnv, sourceMapUrl, facade) {
  29932. const { expression, statements } = compileInjectable({
  29933. name: facade.name,
  29934. type: wrapReference(facade.type),
  29935. typeArgumentCount: facade.typeArgumentCount,
  29936. providedIn: computeProvidedIn(facade.providedIn),
  29937. useClass: convertToProviderExpression(facade, 'useClass'),
  29938. useFactory: wrapExpression(facade, 'useFactory'),
  29939. useValue: convertToProviderExpression(facade, 'useValue'),
  29940. useExisting: convertToProviderExpression(facade, 'useExisting'),
  29941. deps: facade.deps?.map(convertR3DependencyMetadata),
  29942. },
  29943. /* resolveForwardRefs */ true);
  29944. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  29945. }
  29946. compileInjectableDeclaration(angularCoreEnv, sourceMapUrl, facade) {
  29947. const { expression, statements } = compileInjectable({
  29948. name: facade.type.name,
  29949. type: wrapReference(facade.type),
  29950. typeArgumentCount: 0,
  29951. providedIn: computeProvidedIn(facade.providedIn),
  29952. useClass: convertToProviderExpression(facade, 'useClass'),
  29953. useFactory: wrapExpression(facade, 'useFactory'),
  29954. useValue: convertToProviderExpression(facade, 'useValue'),
  29955. useExisting: convertToProviderExpression(facade, 'useExisting'),
  29956. deps: facade.deps?.map(convertR3DeclareDependencyMetadata),
  29957. },
  29958. /* resolveForwardRefs */ true);
  29959. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  29960. }
  29961. compileInjector(angularCoreEnv, sourceMapUrl, facade) {
  29962. const meta = {
  29963. name: facade.name,
  29964. type: wrapReference(facade.type),
  29965. providers: facade.providers && facade.providers.length > 0
  29966. ? new WrappedNodeExpr(facade.providers)
  29967. : null,
  29968. imports: facade.imports.map((i) => new WrappedNodeExpr(i)),
  29969. };
  29970. const res = compileInjector(meta);
  29971. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  29972. }
  29973. compileInjectorDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  29974. const meta = convertDeclareInjectorFacadeToMetadata(declaration);
  29975. const res = compileInjector(meta);
  29976. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  29977. }
  29978. compileNgModule(angularCoreEnv, sourceMapUrl, facade) {
  29979. const meta = {
  29980. kind: exports.R3NgModuleMetadataKind.Global,
  29981. type: wrapReference(facade.type),
  29982. bootstrap: facade.bootstrap.map(wrapReference),
  29983. declarations: facade.declarations.map(wrapReference),
  29984. publicDeclarationTypes: null, // only needed for types in AOT
  29985. imports: facade.imports.map(wrapReference),
  29986. includeImportTypes: true,
  29987. exports: facade.exports.map(wrapReference),
  29988. selectorScopeMode: exports.R3SelectorScopeMode.Inline,
  29989. containsForwardDecls: false,
  29990. schemas: facade.schemas ? facade.schemas.map(wrapReference) : null,
  29991. id: facade.id ? new WrappedNodeExpr(facade.id) : null,
  29992. };
  29993. const res = compileNgModule(meta);
  29994. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  29995. }
  29996. compileNgModuleDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  29997. const expression = compileNgModuleDeclarationExpression(declaration);
  29998. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, []);
  29999. }
  30000. compileDirective(angularCoreEnv, sourceMapUrl, facade) {
  30001. const meta = convertDirectiveFacadeToMetadata(facade);
  30002. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  30003. }
  30004. compileDirectiveDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  30005. const typeSourceSpan = this.createParseSourceSpan('Directive', declaration.type.name, sourceMapUrl);
  30006. const meta = convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan);
  30007. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  30008. }
  30009. compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  30010. const constantPool = new ConstantPool();
  30011. const bindingParser = makeBindingParser();
  30012. const res = compileDirectiveFromMetadata(meta, constantPool, bindingParser);
  30013. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  30014. }
  30015. compileComponent(angularCoreEnv, sourceMapUrl, facade) {
  30016. // Parse the template and check for errors.
  30017. const { template, interpolation, defer } = parseJitTemplate(facade.template, facade.name, sourceMapUrl, facade.preserveWhitespaces, facade.interpolation, undefined);
  30018. // Compile the component metadata, including template, into an expression.
  30019. const meta = {
  30020. ...facade,
  30021. ...convertDirectiveFacadeToMetadata(facade),
  30022. selector: facade.selector || this.elementSchemaRegistry.getDefaultComponentElementName(),
  30023. template,
  30024. declarations: facade.declarations.map(convertDeclarationFacadeToMetadata),
  30025. declarationListEmitMode: 0 /* DeclarationListEmitMode.Direct */,
  30026. defer,
  30027. styles: [...facade.styles, ...template.styles],
  30028. encapsulation: facade.encapsulation,
  30029. interpolation,
  30030. changeDetection: facade.changeDetection ?? null,
  30031. animations: facade.animations != null ? new WrappedNodeExpr(facade.animations) : null,
  30032. viewProviders: facade.viewProviders != null ? new WrappedNodeExpr(facade.viewProviders) : null,
  30033. relativeContextFilePath: '',
  30034. i18nUseExternalIds: true,
  30035. relativeTemplatePath: null,
  30036. };
  30037. const jitExpressionSourceMap = `ng:///${facade.name}.js`;
  30038. return this.compileComponentFromMeta(angularCoreEnv, jitExpressionSourceMap, meta);
  30039. }
  30040. compileComponentDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  30041. const typeSourceSpan = this.createParseSourceSpan('Component', declaration.type.name, sourceMapUrl);
  30042. const meta = convertDeclareComponentFacadeToMetadata(declaration, typeSourceSpan, sourceMapUrl);
  30043. return this.compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta);
  30044. }
  30045. compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  30046. const constantPool = new ConstantPool();
  30047. const bindingParser = makeBindingParser(meta.interpolation);
  30048. const res = compileComponentFromMetadata(meta, constantPool, bindingParser);
  30049. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  30050. }
  30051. compileFactory(angularCoreEnv, sourceMapUrl, meta) {
  30052. const factoryRes = compileFactoryFunction({
  30053. name: meta.name,
  30054. type: wrapReference(meta.type),
  30055. typeArgumentCount: meta.typeArgumentCount,
  30056. deps: convertR3DependencyMetadataArray(meta.deps),
  30057. target: meta.target,
  30058. });
  30059. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  30060. }
  30061. compileFactoryDeclaration(angularCoreEnv, sourceMapUrl, meta) {
  30062. const factoryRes = compileFactoryFunction({
  30063. name: meta.type.name,
  30064. type: wrapReference(meta.type),
  30065. typeArgumentCount: 0,
  30066. deps: Array.isArray(meta.deps)
  30067. ? meta.deps.map(convertR3DeclareDependencyMetadata)
  30068. : meta.deps,
  30069. target: meta.target,
  30070. });
  30071. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  30072. }
  30073. createParseSourceSpan(kind, typeName, sourceUrl) {
  30074. return r3JitTypeSourceSpan(kind, typeName, sourceUrl);
  30075. }
  30076. /**
  30077. * JIT compiles an expression and returns the result of executing that expression.
  30078. *
  30079. * @param def the definition which will be compiled and executed to get the value to patch
  30080. * @param context an object map of @angular/core symbol names to symbols which will be available
  30081. * in the context of the compiled expression
  30082. * @param sourceUrl a URL to use for the source map of the compiled expression
  30083. * @param preStatements a collection of statements that should be evaluated before the expression.
  30084. */
  30085. jitExpression(def, context, sourceUrl, preStatements) {
  30086. // The ConstantPool may contain Statements which declare variables used in the final expression.
  30087. // Therefore, its statements need to precede the actual JIT operation. The final statement is a
  30088. // declaration of $def which is set to the expression being compiled.
  30089. const statements = [
  30090. ...preStatements,
  30091. new DeclareVarStmt('$def', def, undefined, exports.StmtModifier.Exported),
  30092. ];
  30093. const res = this.jitEvaluator.evaluateStatements(sourceUrl, statements, new R3JitReflector(context),
  30094. /* enableSourceMaps */ true);
  30095. return res['$def'];
  30096. }
  30097. }
  30098. function convertToR3QueryMetadata(facade) {
  30099. return {
  30100. ...facade,
  30101. isSignal: facade.isSignal,
  30102. predicate: convertQueryPredicate(facade.predicate),
  30103. read: facade.read ? new WrappedNodeExpr(facade.read) : null,
  30104. static: facade.static,
  30105. emitDistinctChangesOnly: facade.emitDistinctChangesOnly,
  30106. };
  30107. }
  30108. function convertQueryDeclarationToMetadata(declaration) {
  30109. return {
  30110. propertyName: declaration.propertyName,
  30111. first: declaration.first ?? false,
  30112. predicate: convertQueryPredicate(declaration.predicate),
  30113. descendants: declaration.descendants ?? false,
  30114. read: declaration.read ? new WrappedNodeExpr(declaration.read) : null,
  30115. static: declaration.static ?? false,
  30116. emitDistinctChangesOnly: declaration.emitDistinctChangesOnly ?? true,
  30117. isSignal: !!declaration.isSignal,
  30118. };
  30119. }
  30120. function convertQueryPredicate(predicate) {
  30121. return Array.isArray(predicate)
  30122. ? // The predicate is an array of strings so pass it through.
  30123. predicate
  30124. : // The predicate is a type - assume that we will need to unwrap any `forwardRef()` calls.
  30125. createMayBeForwardRefExpression(new WrappedNodeExpr(predicate), 1 /* ForwardRefHandling.Wrapped */);
  30126. }
  30127. function convertDirectiveFacadeToMetadata(facade) {
  30128. const inputsFromMetadata = parseInputsArray$1(facade.inputs || []);
  30129. const outputsFromMetadata = parseMappingStringArray$1(facade.outputs || []);
  30130. const propMetadata = facade.propMetadata;
  30131. const inputsFromType = {};
  30132. const outputsFromType = {};
  30133. for (const field in propMetadata) {
  30134. if (propMetadata.hasOwnProperty(field)) {
  30135. propMetadata[field].forEach((ann) => {
  30136. if (isInput(ann)) {
  30137. inputsFromType[field] = {
  30138. bindingPropertyName: ann.alias || field,
  30139. classPropertyName: field,
  30140. required: ann.required || false,
  30141. // For JIT, decorators are used to declare signal inputs. That is because of
  30142. // a technical limitation where it's not possible to statically reflect class
  30143. // members of a directive/component at runtime before instantiating the class.
  30144. isSignal: !!ann.isSignal,
  30145. transformFunction: ann.transform != null ? new WrappedNodeExpr(ann.transform) : null,
  30146. };
  30147. }
  30148. else if (isOutput(ann)) {
  30149. outputsFromType[field] = ann.alias || field;
  30150. }
  30151. });
  30152. }
  30153. }
  30154. const hostDirectives = facade.hostDirectives?.length
  30155. ? facade.hostDirectives.map((hostDirective) => {
  30156. return typeof hostDirective === 'function'
  30157. ? {
  30158. directive: wrapReference(hostDirective),
  30159. inputs: null,
  30160. outputs: null,
  30161. isForwardReference: false,
  30162. }
  30163. : {
  30164. directive: wrapReference(hostDirective.directive),
  30165. isForwardReference: false,
  30166. inputs: hostDirective.inputs ? parseMappingStringArray$1(hostDirective.inputs) : null,
  30167. outputs: hostDirective.outputs
  30168. ? parseMappingStringArray$1(hostDirective.outputs)
  30169. : null,
  30170. };
  30171. })
  30172. : null;
  30173. return {
  30174. ...facade,
  30175. typeArgumentCount: 0,
  30176. typeSourceSpan: facade.typeSourceSpan,
  30177. type: wrapReference(facade.type),
  30178. deps: null,
  30179. host: {
  30180. ...extractHostBindings$1(facade.propMetadata, facade.typeSourceSpan, facade.host),
  30181. },
  30182. inputs: { ...inputsFromMetadata, ...inputsFromType },
  30183. outputs: { ...outputsFromMetadata, ...outputsFromType },
  30184. queries: facade.queries.map(convertToR3QueryMetadata),
  30185. providers: facade.providers != null ? new WrappedNodeExpr(facade.providers) : null,
  30186. viewQueries: facade.viewQueries.map(convertToR3QueryMetadata),
  30187. fullInheritance: false,
  30188. hostDirectives,
  30189. };
  30190. }
  30191. function convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan) {
  30192. const hostDirectives = declaration.hostDirectives?.length
  30193. ? declaration.hostDirectives.map((dir) => ({
  30194. directive: wrapReference(dir.directive),
  30195. isForwardReference: false,
  30196. inputs: dir.inputs ? getHostDirectiveBindingMapping(dir.inputs) : null,
  30197. outputs: dir.outputs ? getHostDirectiveBindingMapping(dir.outputs) : null,
  30198. }))
  30199. : null;
  30200. return {
  30201. name: declaration.type.name,
  30202. type: wrapReference(declaration.type),
  30203. typeSourceSpan,
  30204. selector: declaration.selector ?? null,
  30205. inputs: declaration.inputs ? inputsPartialMetadataToInputMetadata(declaration.inputs) : {},
  30206. outputs: declaration.outputs ?? {},
  30207. host: convertHostDeclarationToMetadata(declaration.host),
  30208. queries: (declaration.queries ?? []).map(convertQueryDeclarationToMetadata),
  30209. viewQueries: (declaration.viewQueries ?? []).map(convertQueryDeclarationToMetadata),
  30210. providers: declaration.providers !== undefined ? new WrappedNodeExpr(declaration.providers) : null,
  30211. exportAs: declaration.exportAs ?? null,
  30212. usesInheritance: declaration.usesInheritance ?? false,
  30213. lifecycle: { usesOnChanges: declaration.usesOnChanges ?? false },
  30214. deps: null,
  30215. typeArgumentCount: 0,
  30216. fullInheritance: false,
  30217. isStandalone: declaration.isStandalone ?? getJitStandaloneDefaultForVersion(declaration.version),
  30218. isSignal: declaration.isSignal ?? false,
  30219. hostDirectives,
  30220. };
  30221. }
  30222. function convertHostDeclarationToMetadata(host = {}) {
  30223. return {
  30224. attributes: convertOpaqueValuesToExpressions(host.attributes ?? {}),
  30225. listeners: host.listeners ?? {},
  30226. properties: host.properties ?? {},
  30227. specialAttributes: {
  30228. classAttr: host.classAttribute,
  30229. styleAttr: host.styleAttribute,
  30230. },
  30231. };
  30232. }
  30233. /**
  30234. * Parses a host directive mapping where each odd array key is the name of an input/output
  30235. * and each even key is its public name, e.g. `['one', 'oneAlias', 'two', 'two']`.
  30236. */
  30237. function getHostDirectiveBindingMapping(array) {
  30238. let result = null;
  30239. for (let i = 1; i < array.length; i += 2) {
  30240. result = result || {};
  30241. result[array[i - 1]] = array[i];
  30242. }
  30243. return result;
  30244. }
  30245. function convertOpaqueValuesToExpressions(obj) {
  30246. const result = {};
  30247. for (const key of Object.keys(obj)) {
  30248. result[key] = new WrappedNodeExpr(obj[key]);
  30249. }
  30250. return result;
  30251. }
  30252. function convertDeclareComponentFacadeToMetadata(decl, typeSourceSpan, sourceMapUrl) {
  30253. const { template, interpolation, defer } = parseJitTemplate(decl.template, decl.type.name, sourceMapUrl, decl.preserveWhitespaces ?? false, decl.interpolation, decl.deferBlockDependencies);
  30254. const declarations = [];
  30255. if (decl.dependencies) {
  30256. for (const innerDep of decl.dependencies) {
  30257. switch (innerDep.kind) {
  30258. case 'directive':
  30259. case 'component':
  30260. declarations.push(convertDirectiveDeclarationToMetadata(innerDep));
  30261. break;
  30262. case 'pipe':
  30263. declarations.push(convertPipeDeclarationToMetadata(innerDep));
  30264. break;
  30265. }
  30266. }
  30267. }
  30268. else if (decl.components || decl.directives || decl.pipes) {
  30269. // Existing declarations on NPM may not be using the new `dependencies` merged field, and may
  30270. // have separate fields for dependencies instead. Unify them for JIT compilation.
  30271. decl.components &&
  30272. declarations.push(...decl.components.map((dir) => convertDirectiveDeclarationToMetadata(dir, /* isComponent */ true)));
  30273. decl.directives &&
  30274. declarations.push(...decl.directives.map((dir) => convertDirectiveDeclarationToMetadata(dir)));
  30275. decl.pipes && declarations.push(...convertPipeMapToMetadata(decl.pipes));
  30276. }
  30277. return {
  30278. ...convertDeclareDirectiveFacadeToMetadata(decl, typeSourceSpan),
  30279. template,
  30280. styles: decl.styles ?? [],
  30281. declarations,
  30282. viewProviders: decl.viewProviders !== undefined ? new WrappedNodeExpr(decl.viewProviders) : null,
  30283. animations: decl.animations !== undefined ? new WrappedNodeExpr(decl.animations) : null,
  30284. defer,
  30285. changeDetection: decl.changeDetection ?? exports.ChangeDetectionStrategy.Default,
  30286. encapsulation: decl.encapsulation ?? exports.ViewEncapsulation.Emulated,
  30287. interpolation,
  30288. declarationListEmitMode: 2 /* DeclarationListEmitMode.ClosureResolved */,
  30289. relativeContextFilePath: '',
  30290. i18nUseExternalIds: true,
  30291. relativeTemplatePath: null,
  30292. };
  30293. }
  30294. function convertDeclarationFacadeToMetadata(declaration) {
  30295. return {
  30296. ...declaration,
  30297. type: new WrappedNodeExpr(declaration.type),
  30298. };
  30299. }
  30300. function convertDirectiveDeclarationToMetadata(declaration, isComponent = null) {
  30301. return {
  30302. kind: exports.R3TemplateDependencyKind.Directive,
  30303. isComponent: isComponent || declaration.kind === 'component',
  30304. selector: declaration.selector,
  30305. type: new WrappedNodeExpr(declaration.type),
  30306. inputs: declaration.inputs ?? [],
  30307. outputs: declaration.outputs ?? [],
  30308. exportAs: declaration.exportAs ?? null,
  30309. };
  30310. }
  30311. function convertPipeMapToMetadata(pipes) {
  30312. if (!pipes) {
  30313. return [];
  30314. }
  30315. return Object.keys(pipes).map((name) => {
  30316. return {
  30317. kind: exports.R3TemplateDependencyKind.Pipe,
  30318. name,
  30319. type: new WrappedNodeExpr(pipes[name]),
  30320. };
  30321. });
  30322. }
  30323. function convertPipeDeclarationToMetadata(pipe) {
  30324. return {
  30325. kind: exports.R3TemplateDependencyKind.Pipe,
  30326. name: pipe.name,
  30327. type: new WrappedNodeExpr(pipe.type),
  30328. };
  30329. }
  30330. function parseJitTemplate(template, typeName, sourceMapUrl, preserveWhitespaces, interpolation, deferBlockDependencies) {
  30331. const interpolationConfig = interpolation
  30332. ? InterpolationConfig.fromArray(interpolation)
  30333. : DEFAULT_INTERPOLATION_CONFIG;
  30334. // Parse the template and check for errors.
  30335. const parsed = parseTemplate(template, sourceMapUrl, {
  30336. preserveWhitespaces,
  30337. interpolationConfig,
  30338. });
  30339. if (parsed.errors !== null) {
  30340. const errors = parsed.errors.map((err) => err.toString()).join(', ');
  30341. throw new Error(`Errors during JIT compilation of template for ${typeName}: ${errors}`);
  30342. }
  30343. const binder = new R3TargetBinder(new SelectorMatcher());
  30344. const boundTarget = binder.bind({ template: parsed.nodes });
  30345. return {
  30346. template: parsed,
  30347. interpolation: interpolationConfig,
  30348. defer: createR3ComponentDeferMetadata(boundTarget, deferBlockDependencies),
  30349. };
  30350. }
  30351. /**
  30352. * Convert the expression, if present to an `R3ProviderExpression`.
  30353. *
  30354. * In JIT mode we do not want the compiler to wrap the expression in a `forwardRef()` call because,
  30355. * if it is referencing a type that has not yet been defined, it will have already been wrapped in
  30356. * a `forwardRef()` - either by the application developer or during partial-compilation. Thus we can
  30357. * use `ForwardRefHandling.None`.
  30358. */
  30359. function convertToProviderExpression(obj, property) {
  30360. if (obj.hasOwnProperty(property)) {
  30361. return createMayBeForwardRefExpression(new WrappedNodeExpr(obj[property]), 0 /* ForwardRefHandling.None */);
  30362. }
  30363. else {
  30364. return undefined;
  30365. }
  30366. }
  30367. function wrapExpression(obj, property) {
  30368. if (obj.hasOwnProperty(property)) {
  30369. return new WrappedNodeExpr(obj[property]);
  30370. }
  30371. else {
  30372. return undefined;
  30373. }
  30374. }
  30375. function computeProvidedIn(providedIn) {
  30376. const expression = typeof providedIn === 'function'
  30377. ? new WrappedNodeExpr(providedIn)
  30378. : new LiteralExpr(providedIn ?? null);
  30379. // See `convertToProviderExpression()` for why this uses `ForwardRefHandling.None`.
  30380. return createMayBeForwardRefExpression(expression, 0 /* ForwardRefHandling.None */);
  30381. }
  30382. function convertR3DependencyMetadataArray(facades) {
  30383. return facades == null ? null : facades.map(convertR3DependencyMetadata);
  30384. }
  30385. function convertR3DependencyMetadata(facade) {
  30386. const isAttributeDep = facade.attribute != null; // both `null` and `undefined`
  30387. const rawToken = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  30388. // In JIT mode, if the dep is an `@Attribute()` then we use the attribute name given in
  30389. // `attribute` rather than the `token`.
  30390. const token = isAttributeDep ? new WrappedNodeExpr(facade.attribute) : rawToken;
  30391. return createR3DependencyMetadata(token, isAttributeDep, facade.host, facade.optional, facade.self, facade.skipSelf);
  30392. }
  30393. function convertR3DeclareDependencyMetadata(facade) {
  30394. const isAttributeDep = facade.attribute ?? false;
  30395. const token = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  30396. return createR3DependencyMetadata(token, isAttributeDep, facade.host ?? false, facade.optional ?? false, facade.self ?? false, facade.skipSelf ?? false);
  30397. }
  30398. function createR3DependencyMetadata(token, isAttributeDep, host, optional, self, skipSelf) {
  30399. // If the dep is an `@Attribute()` the `attributeNameType` ought to be the `unknown` type.
  30400. // But types are not available at runtime so we just use a literal `"<unknown>"` string as a dummy
  30401. // marker.
  30402. const attributeNameType = isAttributeDep ? literal$1('unknown') : null;
  30403. return { token, attributeNameType, host, optional, self, skipSelf };
  30404. }
  30405. function createR3ComponentDeferMetadata(boundTarget, deferBlockDependencies) {
  30406. const deferredBlocks = boundTarget.getDeferBlocks();
  30407. const blocks = new Map();
  30408. for (let i = 0; i < deferredBlocks.length; i++) {
  30409. const dependencyFn = deferBlockDependencies?.[i];
  30410. blocks.set(deferredBlocks[i], dependencyFn ? new WrappedNodeExpr(dependencyFn) : null);
  30411. }
  30412. return { mode: 0 /* DeferBlockDepsEmitMode.PerBlock */, blocks };
  30413. }
  30414. function extractHostBindings$1(propMetadata, sourceSpan, host) {
  30415. // First parse the declarations from the metadata.
  30416. const bindings = parseHostBindings(host || {});
  30417. // After that check host bindings for errors
  30418. const errors = verifyHostBindings(bindings, sourceSpan);
  30419. if (errors.length) {
  30420. throw new Error(errors.map((error) => error.msg).join('\n'));
  30421. }
  30422. // Next, loop over the properties of the object, looking for @HostBinding and @HostListener.
  30423. for (const field in propMetadata) {
  30424. if (propMetadata.hasOwnProperty(field)) {
  30425. propMetadata[field].forEach((ann) => {
  30426. if (isHostBinding(ann)) {
  30427. // Since this is a decorator, we know that the value is a class member. Always access it
  30428. // through `this` so that further down the line it can't be confused for a literal value
  30429. // (e.g. if there's a property called `true`).
  30430. bindings.properties[ann.hostPropertyName || field] = getSafePropertyAccessString('this', field);
  30431. }
  30432. else if (isHostListener(ann)) {
  30433. bindings.listeners[ann.eventName || field] = `${field}(${(ann.args || []).join(',')})`;
  30434. }
  30435. });
  30436. }
  30437. }
  30438. return bindings;
  30439. }
  30440. function isHostBinding(value) {
  30441. return value.ngMetadataName === 'HostBinding';
  30442. }
  30443. function isHostListener(value) {
  30444. return value.ngMetadataName === 'HostListener';
  30445. }
  30446. function isInput(value) {
  30447. return value.ngMetadataName === 'Input';
  30448. }
  30449. function isOutput(value) {
  30450. return value.ngMetadataName === 'Output';
  30451. }
  30452. function inputsPartialMetadataToInputMetadata(inputs) {
  30453. return Object.keys(inputs).reduce((result, minifiedClassName) => {
  30454. const value = inputs[minifiedClassName];
  30455. // Handle legacy partial input output.
  30456. if (typeof value === 'string' || Array.isArray(value)) {
  30457. result[minifiedClassName] = parseLegacyInputPartialOutput(value);
  30458. }
  30459. else {
  30460. result[minifiedClassName] = {
  30461. bindingPropertyName: value.publicName,
  30462. classPropertyName: minifiedClassName,
  30463. transformFunction: value.transformFunction !== null ? new WrappedNodeExpr(value.transformFunction) : null,
  30464. required: value.isRequired,
  30465. isSignal: value.isSignal,
  30466. };
  30467. }
  30468. return result;
  30469. }, {});
  30470. }
  30471. /**
  30472. * Parses the legacy input partial output. For more details see `partial/directive.ts`.
  30473. * TODO(legacy-partial-output-inputs): Remove in v18.
  30474. */
  30475. function parseLegacyInputPartialOutput(value) {
  30476. if (typeof value === 'string') {
  30477. return {
  30478. bindingPropertyName: value,
  30479. classPropertyName: value,
  30480. transformFunction: null,
  30481. required: false,
  30482. // legacy partial output does not capture signal inputs.
  30483. isSignal: false,
  30484. };
  30485. }
  30486. return {
  30487. bindingPropertyName: value[0],
  30488. classPropertyName: value[1],
  30489. transformFunction: value[2] ? new WrappedNodeExpr(value[2]) : null,
  30490. required: false,
  30491. // legacy partial output does not capture signal inputs.
  30492. isSignal: false,
  30493. };
  30494. }
  30495. function parseInputsArray$1(values) {
  30496. return values.reduce((results, value) => {
  30497. if (typeof value === 'string') {
  30498. const [bindingPropertyName, classPropertyName] = parseMappingString$1(value);
  30499. results[classPropertyName] = {
  30500. bindingPropertyName,
  30501. classPropertyName,
  30502. required: false,
  30503. // Signal inputs not supported for the inputs array.
  30504. isSignal: false,
  30505. transformFunction: null,
  30506. };
  30507. }
  30508. else {
  30509. results[value.name] = {
  30510. bindingPropertyName: value.alias || value.name,
  30511. classPropertyName: value.name,
  30512. required: value.required || false,
  30513. // Signal inputs not supported for the inputs array.
  30514. isSignal: false,
  30515. transformFunction: value.transform != null ? new WrappedNodeExpr(value.transform) : null,
  30516. };
  30517. }
  30518. return results;
  30519. }, {});
  30520. }
  30521. function parseMappingStringArray$1(values) {
  30522. return values.reduce((results, value) => {
  30523. const [alias, fieldName] = parseMappingString$1(value);
  30524. results[fieldName] = alias;
  30525. return results;
  30526. }, {});
  30527. }
  30528. function parseMappingString$1(value) {
  30529. // Either the value is 'field' or 'field: property'. In the first case, `property` will
  30530. // be undefined, in which case the field name should also be used as the property name.
  30531. const [fieldName, bindingPropertyName] = value.split(':', 2).map((str) => str.trim());
  30532. return [bindingPropertyName ?? fieldName, fieldName];
  30533. }
  30534. function convertDeclarePipeFacadeToMetadata(declaration) {
  30535. return {
  30536. name: declaration.type.name,
  30537. type: wrapReference(declaration.type),
  30538. typeArgumentCount: 0,
  30539. pipeName: declaration.name,
  30540. deps: null,
  30541. pure: declaration.pure ?? true,
  30542. isStandalone: declaration.isStandalone ?? getJitStandaloneDefaultForVersion(declaration.version),
  30543. };
  30544. }
  30545. function convertDeclareInjectorFacadeToMetadata(declaration) {
  30546. return {
  30547. name: declaration.type.name,
  30548. type: wrapReference(declaration.type),
  30549. providers: declaration.providers !== undefined && declaration.providers.length > 0
  30550. ? new WrappedNodeExpr(declaration.providers)
  30551. : null,
  30552. imports: declaration.imports !== undefined
  30553. ? declaration.imports.map((i) => new WrappedNodeExpr(i))
  30554. : [],
  30555. };
  30556. }
  30557. function publishFacade(global) {
  30558. const ng = global.ng || (global.ng = {});
  30559. ng.ɵcompilerFacade = new CompilerFacadeImpl();
  30560. }
  30561. /**
  30562. * @module
  30563. * @description
  30564. * Entry point for all public APIs of the compiler package.
  30565. */
  30566. new Version('19.2.13');
  30567. const _I18N_ATTR = 'i18n';
  30568. const _I18N_ATTR_PREFIX = 'i18n-';
  30569. const _I18N_COMMENT_PREFIX_REGEXP = /^i18n:?/;
  30570. const MEANING_SEPARATOR = '|';
  30571. const ID_SEPARATOR = '@@';
  30572. let i18nCommentsWarned = false;
  30573. /**
  30574. * Extract translatable messages from an html AST
  30575. */
  30576. function extractMessages(nodes, interpolationConfig, implicitTags, implicitAttrs, preserveSignificantWhitespace) {
  30577. const visitor = new _Visitor(implicitTags, implicitAttrs, preserveSignificantWhitespace);
  30578. return visitor.extract(nodes, interpolationConfig);
  30579. }
  30580. class ExtractionResult {
  30581. messages;
  30582. errors;
  30583. constructor(messages, errors) {
  30584. this.messages = messages;
  30585. this.errors = errors;
  30586. }
  30587. }
  30588. var _VisitorMode;
  30589. (function (_VisitorMode) {
  30590. _VisitorMode[_VisitorMode["Extract"] = 0] = "Extract";
  30591. _VisitorMode[_VisitorMode["Merge"] = 1] = "Merge";
  30592. })(_VisitorMode || (_VisitorMode = {}));
  30593. /**
  30594. * This Visitor is used:
  30595. * 1. to extract all the translatable strings from an html AST (see `extract()`),
  30596. * 2. to replace the translatable strings with the actual translations (see `merge()`)
  30597. *
  30598. * @internal
  30599. */
  30600. class _Visitor {
  30601. _implicitTags;
  30602. _implicitAttrs;
  30603. _preserveSignificantWhitespace;
  30604. // Using non-null assertions because all variables are (re)set in init()
  30605. _depth;
  30606. // <el i18n>...</el>
  30607. _inI18nNode;
  30608. _inImplicitNode;
  30609. // <!--i18n-->...<!--/i18n-->
  30610. _inI18nBlock;
  30611. _blockMeaningAndDesc;
  30612. _blockChildren;
  30613. _blockStartDepth;
  30614. // {<icu message>}
  30615. _inIcu;
  30616. // set to void 0 when not in a section
  30617. _msgCountAtSectionStart;
  30618. _errors;
  30619. _mode;
  30620. // _VisitorMode.Extract only
  30621. _messages;
  30622. // _VisitorMode.Merge only
  30623. _translations;
  30624. _createI18nMessage;
  30625. constructor(_implicitTags, _implicitAttrs, _preserveSignificantWhitespace = true) {
  30626. this._implicitTags = _implicitTags;
  30627. this._implicitAttrs = _implicitAttrs;
  30628. this._preserveSignificantWhitespace = _preserveSignificantWhitespace;
  30629. }
  30630. /**
  30631. * Extracts the messages from the tree
  30632. */
  30633. extract(nodes, interpolationConfig) {
  30634. this._init(_VisitorMode.Extract, interpolationConfig);
  30635. nodes.forEach((node) => node.visit(this, null));
  30636. if (this._inI18nBlock) {
  30637. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  30638. }
  30639. return new ExtractionResult(this._messages, this._errors);
  30640. }
  30641. /**
  30642. * Returns a tree where all translatable nodes are translated
  30643. */
  30644. merge(nodes, translations, interpolationConfig) {
  30645. this._init(_VisitorMode.Merge, interpolationConfig);
  30646. this._translations = translations;
  30647. // Construct a single fake root element
  30648. const wrapper = new Element('wrapper', [], nodes, undefined, undefined, undefined);
  30649. const translatedNode = wrapper.visit(this, null);
  30650. if (this._inI18nBlock) {
  30651. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  30652. }
  30653. return new ParseTreeResult(translatedNode.children, this._errors);
  30654. }
  30655. visitExpansionCase(icuCase, context) {
  30656. // Parse cases for translatable html attributes
  30657. const expression = visitAll(this, icuCase.expression, context);
  30658. if (this._mode === _VisitorMode.Merge) {
  30659. return new ExpansionCase(icuCase.value, expression, icuCase.sourceSpan, icuCase.valueSourceSpan, icuCase.expSourceSpan);
  30660. }
  30661. }
  30662. visitExpansion(icu, context) {
  30663. this._mayBeAddBlockChildren(icu);
  30664. const wasInIcu = this._inIcu;
  30665. if (!this._inIcu) {
  30666. // nested ICU messages should not be extracted but top-level translated as a whole
  30667. if (this._isInTranslatableSection) {
  30668. this._addMessage([icu]);
  30669. }
  30670. this._inIcu = true;
  30671. }
  30672. const cases = visitAll(this, icu.cases, context);
  30673. if (this._mode === _VisitorMode.Merge) {
  30674. icu = new Expansion(icu.switchValue, icu.type, cases, icu.sourceSpan, icu.switchValueSourceSpan);
  30675. }
  30676. this._inIcu = wasInIcu;
  30677. return icu;
  30678. }
  30679. visitComment(comment, context) {
  30680. const isOpening = _isOpeningComment(comment);
  30681. if (isOpening && this._isInTranslatableSection) {
  30682. this._reportError(comment, 'Could not start a block inside a translatable section');
  30683. return;
  30684. }
  30685. const isClosing = _isClosingComment(comment);
  30686. if (isClosing && !this._inI18nBlock) {
  30687. this._reportError(comment, 'Trying to close an unopened block');
  30688. return;
  30689. }
  30690. if (!this._inI18nNode && !this._inIcu) {
  30691. if (!this._inI18nBlock) {
  30692. if (isOpening) {
  30693. // deprecated from v5 you should use <ng-container i18n> instead of i18n comments
  30694. if (!i18nCommentsWarned && console && console.warn) {
  30695. i18nCommentsWarned = true;
  30696. const details = comment.sourceSpan.details ? `, ${comment.sourceSpan.details}` : '';
  30697. // TODO(ocombe): use a log service once there is a public one available
  30698. console.warn(`I18n comments are deprecated, use an <ng-container> element instead (${comment.sourceSpan.start}${details})`);
  30699. }
  30700. this._inI18nBlock = true;
  30701. this._blockStartDepth = this._depth;
  30702. this._blockChildren = [];
  30703. this._blockMeaningAndDesc = comment
  30704. .value.replace(_I18N_COMMENT_PREFIX_REGEXP, '')
  30705. .trim();
  30706. this._openTranslatableSection(comment);
  30707. }
  30708. }
  30709. else {
  30710. if (isClosing) {
  30711. if (this._depth == this._blockStartDepth) {
  30712. this._closeTranslatableSection(comment, this._blockChildren);
  30713. this._inI18nBlock = false;
  30714. const message = this._addMessage(this._blockChildren, this._blockMeaningAndDesc);
  30715. // merge attributes in sections
  30716. const nodes = this._translateMessage(comment, message);
  30717. return visitAll(this, nodes);
  30718. }
  30719. else {
  30720. this._reportError(comment, 'I18N blocks should not cross element boundaries');
  30721. return;
  30722. }
  30723. }
  30724. }
  30725. }
  30726. }
  30727. visitText(text, context) {
  30728. if (this._isInTranslatableSection) {
  30729. this._mayBeAddBlockChildren(text);
  30730. }
  30731. return text;
  30732. }
  30733. visitElement(el, context) {
  30734. this._mayBeAddBlockChildren(el);
  30735. this._depth++;
  30736. const wasInI18nNode = this._inI18nNode;
  30737. const wasInImplicitNode = this._inImplicitNode;
  30738. let childNodes = [];
  30739. let translatedChildNodes = undefined;
  30740. // Extract:
  30741. // - top level nodes with the (implicit) "i18n" attribute if not already in a section
  30742. // - ICU messages
  30743. const i18nAttr = _getI18nAttr(el);
  30744. const i18nMeta = i18nAttr ? i18nAttr.value : '';
  30745. const isImplicit = this._implicitTags.some((tag) => el.name === tag) &&
  30746. !this._inIcu &&
  30747. !this._isInTranslatableSection;
  30748. const isTopLevelImplicit = !wasInImplicitNode && isImplicit;
  30749. this._inImplicitNode = wasInImplicitNode || isImplicit;
  30750. if (!this._isInTranslatableSection && !this._inIcu) {
  30751. if (i18nAttr || isTopLevelImplicit) {
  30752. this._inI18nNode = true;
  30753. const message = this._addMessage(el.children, i18nMeta);
  30754. translatedChildNodes = this._translateMessage(el, message);
  30755. }
  30756. if (this._mode == _VisitorMode.Extract) {
  30757. const isTranslatable = i18nAttr || isTopLevelImplicit;
  30758. if (isTranslatable)
  30759. this._openTranslatableSection(el);
  30760. visitAll(this, el.children);
  30761. if (isTranslatable)
  30762. this._closeTranslatableSection(el, el.children);
  30763. }
  30764. }
  30765. else {
  30766. if (i18nAttr || isTopLevelImplicit) {
  30767. this._reportError(el, 'Could not mark an element as translatable inside a translatable section');
  30768. }
  30769. if (this._mode == _VisitorMode.Extract) {
  30770. // Descend into child nodes for extraction
  30771. visitAll(this, el.children);
  30772. }
  30773. }
  30774. if (this._mode === _VisitorMode.Merge) {
  30775. const visitNodes = translatedChildNodes || el.children;
  30776. visitNodes.forEach((child) => {
  30777. const visited = child.visit(this, context);
  30778. if (visited && !this._isInTranslatableSection) {
  30779. // Do not add the children from translatable sections (= i18n blocks here)
  30780. // They will be added later in this loop when the block closes (i.e. on `<!-- /i18n -->`)
  30781. childNodes = childNodes.concat(visited);
  30782. }
  30783. });
  30784. }
  30785. this._visitAttributesOf(el);
  30786. this._depth--;
  30787. this._inI18nNode = wasInI18nNode;
  30788. this._inImplicitNode = wasInImplicitNode;
  30789. if (this._mode === _VisitorMode.Merge) {
  30790. const translatedAttrs = this._translateAttributes(el);
  30791. return new Element(el.name, translatedAttrs, childNodes, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  30792. }
  30793. return null;
  30794. }
  30795. visitAttribute(attribute, context) {
  30796. throw new Error('unreachable code');
  30797. }
  30798. visitBlock(block, context) {
  30799. visitAll(this, block.children, context);
  30800. }
  30801. visitBlockParameter(parameter, context) { }
  30802. visitLetDeclaration(decl, context) { }
  30803. _init(mode, interpolationConfig) {
  30804. this._mode = mode;
  30805. this._inI18nBlock = false;
  30806. this._inI18nNode = false;
  30807. this._depth = 0;
  30808. this._inIcu = false;
  30809. this._msgCountAtSectionStart = undefined;
  30810. this._errors = [];
  30811. this._messages = [];
  30812. this._inImplicitNode = false;
  30813. this._createI18nMessage = createI18nMessageFactory(interpolationConfig, DEFAULT_CONTAINER_BLOCKS,
  30814. // When dropping significant whitespace we need to retain whitespace tokens or
  30815. // else we won't be able to reuse source spans because empty tokens would be
  30816. // removed and cause a mismatch.
  30817. /* retainEmptyTokens */ !this._preserveSignificantWhitespace,
  30818. /* preserveExpressionWhitespace */ this._preserveSignificantWhitespace);
  30819. }
  30820. // looks for translatable attributes
  30821. _visitAttributesOf(el) {
  30822. const explicitAttrNameToValue = {};
  30823. const implicitAttrNames = this._implicitAttrs[el.name] || [];
  30824. el.attrs
  30825. .filter((attr) => attr.name.startsWith(_I18N_ATTR_PREFIX))
  30826. .forEach((attr) => (explicitAttrNameToValue[attr.name.slice(_I18N_ATTR_PREFIX.length)] = attr.value));
  30827. el.attrs.forEach((attr) => {
  30828. if (attr.name in explicitAttrNameToValue) {
  30829. this._addMessage([attr], explicitAttrNameToValue[attr.name]);
  30830. }
  30831. else if (implicitAttrNames.some((name) => attr.name === name)) {
  30832. this._addMessage([attr]);
  30833. }
  30834. });
  30835. }
  30836. // add a translatable message
  30837. _addMessage(ast, msgMeta) {
  30838. if (ast.length == 0 ||
  30839. this._isEmptyAttributeValue(ast) ||
  30840. this._isPlaceholderOnlyAttributeValue(ast) ||
  30841. this._isPlaceholderOnlyMessage(ast)) {
  30842. // Do not create empty messages
  30843. return null;
  30844. }
  30845. const { meaning, description, id } = _parseMessageMeta(msgMeta);
  30846. const message = this._createI18nMessage(ast, meaning, description, id);
  30847. this._messages.push(message);
  30848. return message;
  30849. }
  30850. // Check for cases like `<div i18n-title title="">`.
  30851. _isEmptyAttributeValue(ast) {
  30852. if (!isAttrNode(ast))
  30853. return false;
  30854. const node = ast[0];
  30855. return node.value.trim() === '';
  30856. }
  30857. // Check for cases like `<div i18n-title title="{{ name }}">`.
  30858. _isPlaceholderOnlyAttributeValue(ast) {
  30859. if (!isAttrNode(ast))
  30860. return false;
  30861. const tokens = ast[0].valueTokens ?? [];
  30862. const interpolations = tokens.filter((token) => token.type === 17 /* TokenType.ATTR_VALUE_INTERPOLATION */);
  30863. const plainText = tokens
  30864. .filter((token) => token.type === 16 /* TokenType.ATTR_VALUE_TEXT */)
  30865. // `AttributeValueTextToken` always has exactly one part per its type.
  30866. .map((token) => token.parts[0].trim())
  30867. .join('');
  30868. // Check if there is a single interpolation and all text around it is empty.
  30869. return interpolations.length === 1 && plainText === '';
  30870. }
  30871. // Check for cases like `<div i18n>{{ name }}</div>`.
  30872. _isPlaceholderOnlyMessage(ast) {
  30873. if (!isTextNode(ast))
  30874. return false;
  30875. const tokens = ast[0].tokens;
  30876. const interpolations = tokens.filter((token) => token.type === 8 /* TokenType.INTERPOLATION */);
  30877. const plainText = tokens
  30878. .filter((token) => token.type === 5 /* TokenType.TEXT */)
  30879. // `TextToken` always has exactly one part per its type.
  30880. .map((token) => token.parts[0].trim())
  30881. .join('');
  30882. // Check if there is a single interpolation and all text around it is empty.
  30883. return interpolations.length === 1 && plainText === '';
  30884. }
  30885. // Translates the given message given the `TranslationBundle`
  30886. // This is used for translating elements / blocks - see `_translateAttributes` for attributes
  30887. // no-op when called in extraction mode (returns [])
  30888. _translateMessage(el, message) {
  30889. if (message && this._mode === _VisitorMode.Merge) {
  30890. const nodes = this._translations.get(message);
  30891. if (nodes) {
  30892. return nodes;
  30893. }
  30894. this._reportError(el, `Translation unavailable for message id="${this._translations.digest(message)}"`);
  30895. }
  30896. return [];
  30897. }
  30898. // translate the attributes of an element and remove i18n specific attributes
  30899. _translateAttributes(el) {
  30900. const attributes = el.attrs;
  30901. const i18nParsedMessageMeta = {};
  30902. attributes.forEach((attr) => {
  30903. if (attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  30904. i18nParsedMessageMeta[attr.name.slice(_I18N_ATTR_PREFIX.length)] = _parseMessageMeta(attr.value);
  30905. }
  30906. });
  30907. const translatedAttributes = [];
  30908. attributes.forEach((attr) => {
  30909. if (attr.name === _I18N_ATTR || attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  30910. // strip i18n specific attributes
  30911. return;
  30912. }
  30913. if (attr.value && attr.value != '' && i18nParsedMessageMeta.hasOwnProperty(attr.name)) {
  30914. const { meaning, description, id } = i18nParsedMessageMeta[attr.name];
  30915. const message = this._createI18nMessage([attr], meaning, description, id);
  30916. const nodes = this._translations.get(message);
  30917. if (nodes) {
  30918. if (nodes.length == 0) {
  30919. translatedAttributes.push(new Attribute(attr.name, '', attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* valueTokens */, undefined /* i18n */));
  30920. }
  30921. else if (nodes[0] instanceof Text) {
  30922. const value = nodes[0].value;
  30923. translatedAttributes.push(new Attribute(attr.name, value, attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* valueTokens */, undefined /* i18n */));
  30924. }
  30925. else {
  30926. this._reportError(el, `Unexpected translation for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  30927. }
  30928. }
  30929. else {
  30930. this._reportError(el, `Translation unavailable for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  30931. }
  30932. }
  30933. else {
  30934. translatedAttributes.push(attr);
  30935. }
  30936. });
  30937. return translatedAttributes;
  30938. }
  30939. /**
  30940. * Add the node as a child of the block when:
  30941. * - we are in a block,
  30942. * - we are not inside a ICU message (those are handled separately),
  30943. * - the node is a "direct child" of the block
  30944. */
  30945. _mayBeAddBlockChildren(node) {
  30946. if (this._inI18nBlock && !this._inIcu && this._depth == this._blockStartDepth) {
  30947. this._blockChildren.push(node);
  30948. }
  30949. }
  30950. /**
  30951. * Marks the start of a section, see `_closeTranslatableSection`
  30952. */
  30953. _openTranslatableSection(node) {
  30954. if (this._isInTranslatableSection) {
  30955. this._reportError(node, 'Unexpected section start');
  30956. }
  30957. else {
  30958. this._msgCountAtSectionStart = this._messages.length;
  30959. }
  30960. }
  30961. /**
  30962. * A translatable section could be:
  30963. * - the content of translatable element,
  30964. * - nodes between `<!-- i18n -->` and `<!-- /i18n -->` comments
  30965. */
  30966. get _isInTranslatableSection() {
  30967. return this._msgCountAtSectionStart !== void 0;
  30968. }
  30969. /**
  30970. * Terminates a section.
  30971. *
  30972. * If a section has only one significant children (comments not significant) then we should not
  30973. * keep the message from this children:
  30974. *
  30975. * `<p i18n="meaning|description">{ICU message}</p>` would produce two messages:
  30976. * - one for the <p> content with meaning and description,
  30977. * - another one for the ICU message.
  30978. *
  30979. * In this case the last message is discarded as it contains less information (the AST is
  30980. * otherwise identical).
  30981. *
  30982. * Note that we should still keep messages extracted from attributes inside the section (ie in the
  30983. * ICU message here)
  30984. */
  30985. _closeTranslatableSection(node, directChildren) {
  30986. if (!this._isInTranslatableSection) {
  30987. this._reportError(node, 'Unexpected section end');
  30988. return;
  30989. }
  30990. const startIndex = this._msgCountAtSectionStart;
  30991. const significantChildren = directChildren.reduce((count, node) => count + (node instanceof Comment ? 0 : 1), 0);
  30992. if (significantChildren == 1) {
  30993. for (let i = this._messages.length - 1; i >= startIndex; i--) {
  30994. const ast = this._messages[i].nodes;
  30995. if (!(ast.length == 1 && ast[0] instanceof Text$2)) {
  30996. this._messages.splice(i, 1);
  30997. break;
  30998. }
  30999. }
  31000. }
  31001. this._msgCountAtSectionStart = undefined;
  31002. }
  31003. _reportError(node, msg) {
  31004. this._errors.push(new I18nError(node.sourceSpan, msg));
  31005. }
  31006. }
  31007. function _isOpeningComment(n) {
  31008. return !!(n instanceof Comment && n.value && n.value.startsWith('i18n'));
  31009. }
  31010. function _isClosingComment(n) {
  31011. return !!(n instanceof Comment && n.value && n.value === '/i18n');
  31012. }
  31013. function _getI18nAttr(p) {
  31014. return p.attrs.find((attr) => attr.name === _I18N_ATTR) || null;
  31015. }
  31016. function _parseMessageMeta(i18n) {
  31017. if (!i18n)
  31018. return { meaning: '', description: '', id: '' };
  31019. const idIndex = i18n.indexOf(ID_SEPARATOR);
  31020. const descIndex = i18n.indexOf(MEANING_SEPARATOR);
  31021. const [meaningAndDesc, id] = idIndex > -1 ? [i18n.slice(0, idIndex), i18n.slice(idIndex + 2)] : [i18n, ''];
  31022. const [meaning, description] = descIndex > -1
  31023. ? [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)]
  31024. : ['', meaningAndDesc];
  31025. return { meaning, description, id: id.trim() };
  31026. }
  31027. function isTextNode(ast) {
  31028. return ast.length === 1 && ast[0] instanceof Text;
  31029. }
  31030. function isAttrNode(ast) {
  31031. return ast.length === 1 && ast[0] instanceof Attribute;
  31032. }
  31033. //////////////////////////////////////
  31034. // THIS FILE HAS GLOBAL SIDE EFFECT //
  31035. // (see bottom of file) //
  31036. //////////////////////////////////////
  31037. /**
  31038. * @module
  31039. * @description
  31040. * Entry point for all APIs of the compiler package.
  31041. *
  31042. * <div class="callout is-critical">
  31043. * <header>Unstable APIs</header>
  31044. * <p>
  31045. * All compiler apis are currently considered experimental and private!
  31046. * </p>
  31047. * <p>
  31048. * We expect the APIs in this package to keep on changing. Do not rely on them.
  31049. * </p>
  31050. * </div>
  31051. */
  31052. // This file only reexports content of the `src` folder. Keep it that way.
  31053. // This function call has a global side effects and publishes the compiler into global namespace for
  31054. // the late binding of the Compiler to the @angular/core for jit compilation.
  31055. publishFacade(_global);
  31056. /**
  31057. * @publicApi
  31058. */
  31059. exports.ErrorCode = void 0;
  31060. (function (ErrorCode) {
  31061. ErrorCode[ErrorCode["DECORATOR_ARG_NOT_LITERAL"] = 1001] = "DECORATOR_ARG_NOT_LITERAL";
  31062. ErrorCode[ErrorCode["DECORATOR_ARITY_WRONG"] = 1002] = "DECORATOR_ARITY_WRONG";
  31063. ErrorCode[ErrorCode["DECORATOR_NOT_CALLED"] = 1003] = "DECORATOR_NOT_CALLED";
  31064. ErrorCode[ErrorCode["DECORATOR_UNEXPECTED"] = 1005] = "DECORATOR_UNEXPECTED";
  31065. /**
  31066. * This error code indicates that there are incompatible decorators on a type or a class field.
  31067. */
  31068. ErrorCode[ErrorCode["DECORATOR_COLLISION"] = 1006] = "DECORATOR_COLLISION";
  31069. ErrorCode[ErrorCode["VALUE_HAS_WRONG_TYPE"] = 1010] = "VALUE_HAS_WRONG_TYPE";
  31070. ErrorCode[ErrorCode["VALUE_NOT_LITERAL"] = 1011] = "VALUE_NOT_LITERAL";
  31071. ErrorCode[ErrorCode["DUPLICATE_DECORATED_PROPERTIES"] = 1012] = "DUPLICATE_DECORATED_PROPERTIES";
  31072. /**
  31073. * Raised when an initializer API is annotated with an unexpected decorator.
  31074. *
  31075. * e.g. `@Input` is also applied on the class member using `input`.
  31076. */
  31077. ErrorCode[ErrorCode["INITIALIZER_API_WITH_DISALLOWED_DECORATOR"] = 1050] = "INITIALIZER_API_WITH_DISALLOWED_DECORATOR";
  31078. /**
  31079. * Raised when an initializer API feature (like signal inputs) are also
  31080. * declared in the class decorator metadata.
  31081. *
  31082. * e.g. a signal input is also declared in the `@Directive` `inputs` array.
  31083. */
  31084. ErrorCode[ErrorCode["INITIALIZER_API_DECORATOR_METADATA_COLLISION"] = 1051] = "INITIALIZER_API_DECORATOR_METADATA_COLLISION";
  31085. /**
  31086. * Raised whenever an initializer API does not support the `.required`
  31087. * function, but is still detected unexpectedly.
  31088. */
  31089. ErrorCode[ErrorCode["INITIALIZER_API_NO_REQUIRED_FUNCTION"] = 1052] = "INITIALIZER_API_NO_REQUIRED_FUNCTION";
  31090. /**
  31091. * Raised whenever an initializer API is used on a class member
  31092. * and the given access modifiers (e.g. `private`) are not allowed.
  31093. */
  31094. ErrorCode[ErrorCode["INITIALIZER_API_DISALLOWED_MEMBER_VISIBILITY"] = 1053] = "INITIALIZER_API_DISALLOWED_MEMBER_VISIBILITY";
  31095. /**
  31096. * An Angular feature, like inputs, outputs or queries is incorrectly
  31097. * declared on a static member.
  31098. */
  31099. ErrorCode[ErrorCode["INCORRECTLY_DECLARED_ON_STATIC_MEMBER"] = 1100] = "INCORRECTLY_DECLARED_ON_STATIC_MEMBER";
  31100. ErrorCode[ErrorCode["COMPONENT_MISSING_TEMPLATE"] = 2001] = "COMPONENT_MISSING_TEMPLATE";
  31101. ErrorCode[ErrorCode["PIPE_MISSING_NAME"] = 2002] = "PIPE_MISSING_NAME";
  31102. ErrorCode[ErrorCode["PARAM_MISSING_TOKEN"] = 2003] = "PARAM_MISSING_TOKEN";
  31103. ErrorCode[ErrorCode["DIRECTIVE_MISSING_SELECTOR"] = 2004] = "DIRECTIVE_MISSING_SELECTOR";
  31104. /** Raised when an undecorated class is passed in as a provider to a module or a directive. */
  31105. ErrorCode[ErrorCode["UNDECORATED_PROVIDER"] = 2005] = "UNDECORATED_PROVIDER";
  31106. /**
  31107. * Raised when a Directive inherits its constructor from a base class without an Angular
  31108. * decorator.
  31109. */
  31110. ErrorCode[ErrorCode["DIRECTIVE_INHERITS_UNDECORATED_CTOR"] = 2006] = "DIRECTIVE_INHERITS_UNDECORATED_CTOR";
  31111. /**
  31112. * Raised when an undecorated class that is using Angular features
  31113. * has been discovered.
  31114. */
  31115. ErrorCode[ErrorCode["UNDECORATED_CLASS_USING_ANGULAR_FEATURES"] = 2007] = "UNDECORATED_CLASS_USING_ANGULAR_FEATURES";
  31116. /**
  31117. * Raised when an component cannot resolve an external resource, such as a template or a style
  31118. * sheet.
  31119. */
  31120. ErrorCode[ErrorCode["COMPONENT_RESOURCE_NOT_FOUND"] = 2008] = "COMPONENT_RESOURCE_NOT_FOUND";
  31121. /**
  31122. * Raised when a component uses `ShadowDom` view encapsulation, but its selector
  31123. * does not match the shadow DOM tag name requirements.
  31124. */
  31125. ErrorCode[ErrorCode["COMPONENT_INVALID_SHADOW_DOM_SELECTOR"] = 2009] = "COMPONENT_INVALID_SHADOW_DOM_SELECTOR";
  31126. /**
  31127. * Raised when a component has `imports` but is not marked as `standalone: true`.
  31128. */
  31129. ErrorCode[ErrorCode["COMPONENT_NOT_STANDALONE"] = 2010] = "COMPONENT_NOT_STANDALONE";
  31130. /**
  31131. * Raised when a type in the `imports` of a component is a directive or pipe, but is not
  31132. * standalone.
  31133. */
  31134. ErrorCode[ErrorCode["COMPONENT_IMPORT_NOT_STANDALONE"] = 2011] = "COMPONENT_IMPORT_NOT_STANDALONE";
  31135. /**
  31136. * Raised when a type in the `imports` of a component is not a directive, pipe, or NgModule.
  31137. */
  31138. ErrorCode[ErrorCode["COMPONENT_UNKNOWN_IMPORT"] = 2012] = "COMPONENT_UNKNOWN_IMPORT";
  31139. /**
  31140. * Raised when the compiler wasn't able to resolve the metadata of a host directive.
  31141. */
  31142. ErrorCode[ErrorCode["HOST_DIRECTIVE_INVALID"] = 2013] = "HOST_DIRECTIVE_INVALID";
  31143. /**
  31144. * Raised when a host directive isn't standalone.
  31145. */
  31146. ErrorCode[ErrorCode["HOST_DIRECTIVE_NOT_STANDALONE"] = 2014] = "HOST_DIRECTIVE_NOT_STANDALONE";
  31147. /**
  31148. * Raised when a host directive is a component.
  31149. */
  31150. ErrorCode[ErrorCode["HOST_DIRECTIVE_COMPONENT"] = 2015] = "HOST_DIRECTIVE_COMPONENT";
  31151. /**
  31152. * Raised when a type with Angular decorator inherits its constructor from a base class
  31153. * which has a constructor that is incompatible with Angular DI.
  31154. */
  31155. ErrorCode[ErrorCode["INJECTABLE_INHERITS_INVALID_CONSTRUCTOR"] = 2016] = "INJECTABLE_INHERITS_INVALID_CONSTRUCTOR";
  31156. /** Raised when a host tries to alias a host directive binding that does not exist. */
  31157. ErrorCode[ErrorCode["HOST_DIRECTIVE_UNDEFINED_BINDING"] = 2017] = "HOST_DIRECTIVE_UNDEFINED_BINDING";
  31158. /**
  31159. * Raised when a host tries to alias a host directive
  31160. * binding to a pre-existing binding's public name.
  31161. */
  31162. ErrorCode[ErrorCode["HOST_DIRECTIVE_CONFLICTING_ALIAS"] = 2018] = "HOST_DIRECTIVE_CONFLICTING_ALIAS";
  31163. /**
  31164. * Raised when a host directive definition doesn't expose a
  31165. * required binding from the host directive.
  31166. */
  31167. ErrorCode[ErrorCode["HOST_DIRECTIVE_MISSING_REQUIRED_BINDING"] = 2019] = "HOST_DIRECTIVE_MISSING_REQUIRED_BINDING";
  31168. /**
  31169. * Raised when a component specifies both a `transform` function on an input
  31170. * and has a corresponding `ngAcceptInputType_` member for the same input.
  31171. */
  31172. ErrorCode[ErrorCode["CONFLICTING_INPUT_TRANSFORM"] = 2020] = "CONFLICTING_INPUT_TRANSFORM";
  31173. /** Raised when a component has both `styleUrls` and `styleUrl`. */
  31174. ErrorCode[ErrorCode["COMPONENT_INVALID_STYLE_URLS"] = 2021] = "COMPONENT_INVALID_STYLE_URLS";
  31175. /**
  31176. * Raised when a type in the `deferredImports` of a component is not a component, directive or
  31177. * pipe.
  31178. */
  31179. ErrorCode[ErrorCode["COMPONENT_UNKNOWN_DEFERRED_IMPORT"] = 2022] = "COMPONENT_UNKNOWN_DEFERRED_IMPORT";
  31180. /**
  31181. * Raised when a `standalone: false` component is declared but `strictStandalone` is set.
  31182. */
  31183. ErrorCode[ErrorCode["NON_STANDALONE_NOT_ALLOWED"] = 2023] = "NON_STANDALONE_NOT_ALLOWED";
  31184. ErrorCode[ErrorCode["SYMBOL_NOT_EXPORTED"] = 3001] = "SYMBOL_NOT_EXPORTED";
  31185. /**
  31186. * Raised when a relationship between directives and/or pipes would cause a cyclic import to be
  31187. * created that cannot be handled, such as in partial compilation mode.
  31188. */
  31189. ErrorCode[ErrorCode["IMPORT_CYCLE_DETECTED"] = 3003] = "IMPORT_CYCLE_DETECTED";
  31190. /**
  31191. * Raised when the compiler is unable to generate an import statement for a reference.
  31192. */
  31193. ErrorCode[ErrorCode["IMPORT_GENERATION_FAILURE"] = 3004] = "IMPORT_GENERATION_FAILURE";
  31194. ErrorCode[ErrorCode["CONFIG_FLAT_MODULE_NO_INDEX"] = 4001] = "CONFIG_FLAT_MODULE_NO_INDEX";
  31195. ErrorCode[ErrorCode["CONFIG_STRICT_TEMPLATES_IMPLIES_FULL_TEMPLATE_TYPECHECK"] = 4002] = "CONFIG_STRICT_TEMPLATES_IMPLIES_FULL_TEMPLATE_TYPECHECK";
  31196. ErrorCode[ErrorCode["CONFIG_EXTENDED_DIAGNOSTICS_IMPLIES_STRICT_TEMPLATES"] = 4003] = "CONFIG_EXTENDED_DIAGNOSTICS_IMPLIES_STRICT_TEMPLATES";
  31197. ErrorCode[ErrorCode["CONFIG_EXTENDED_DIAGNOSTICS_UNKNOWN_CATEGORY_LABEL"] = 4004] = "CONFIG_EXTENDED_DIAGNOSTICS_UNKNOWN_CATEGORY_LABEL";
  31198. ErrorCode[ErrorCode["CONFIG_EXTENDED_DIAGNOSTICS_UNKNOWN_CHECK"] = 4005] = "CONFIG_EXTENDED_DIAGNOSTICS_UNKNOWN_CHECK";
  31199. /**
  31200. * Raised when a host expression has a parse error, such as a host listener or host binding
  31201. * expression containing a pipe.
  31202. */
  31203. ErrorCode[ErrorCode["HOST_BINDING_PARSE_ERROR"] = 5001] = "HOST_BINDING_PARSE_ERROR";
  31204. /**
  31205. * Raised when the compiler cannot parse a component's template.
  31206. */
  31207. ErrorCode[ErrorCode["TEMPLATE_PARSE_ERROR"] = 5002] = "TEMPLATE_PARSE_ERROR";
  31208. /**
  31209. * Raised when an NgModule contains an invalid reference in `declarations`.
  31210. */
  31211. ErrorCode[ErrorCode["NGMODULE_INVALID_DECLARATION"] = 6001] = "NGMODULE_INVALID_DECLARATION";
  31212. /**
  31213. * Raised when an NgModule contains an invalid type in `imports`.
  31214. */
  31215. ErrorCode[ErrorCode["NGMODULE_INVALID_IMPORT"] = 6002] = "NGMODULE_INVALID_IMPORT";
  31216. /**
  31217. * Raised when an NgModule contains an invalid type in `exports`.
  31218. */
  31219. ErrorCode[ErrorCode["NGMODULE_INVALID_EXPORT"] = 6003] = "NGMODULE_INVALID_EXPORT";
  31220. /**
  31221. * Raised when an NgModule contains a type in `exports` which is neither in `declarations` nor
  31222. * otherwise imported.
  31223. */
  31224. ErrorCode[ErrorCode["NGMODULE_INVALID_REEXPORT"] = 6004] = "NGMODULE_INVALID_REEXPORT";
  31225. /**
  31226. * Raised when a `ModuleWithProviders` with a missing
  31227. * generic type argument is passed into an `NgModule`.
  31228. */
  31229. ErrorCode[ErrorCode["NGMODULE_MODULE_WITH_PROVIDERS_MISSING_GENERIC"] = 6005] = "NGMODULE_MODULE_WITH_PROVIDERS_MISSING_GENERIC";
  31230. /**
  31231. * Raised when an NgModule exports multiple directives/pipes of the same name and the compiler
  31232. * attempts to generate private re-exports within the NgModule file.
  31233. */
  31234. ErrorCode[ErrorCode["NGMODULE_REEXPORT_NAME_COLLISION"] = 6006] = "NGMODULE_REEXPORT_NAME_COLLISION";
  31235. /**
  31236. * Raised when a directive/pipe is part of the declarations of two or more NgModules.
  31237. */
  31238. ErrorCode[ErrorCode["NGMODULE_DECLARATION_NOT_UNIQUE"] = 6007] = "NGMODULE_DECLARATION_NOT_UNIQUE";
  31239. /**
  31240. * Raised when a standalone directive/pipe is part of the declarations of an NgModule.
  31241. */
  31242. ErrorCode[ErrorCode["NGMODULE_DECLARATION_IS_STANDALONE"] = 6008] = "NGMODULE_DECLARATION_IS_STANDALONE";
  31243. /**
  31244. * Raised when a standalone component is part of the bootstrap list of an NgModule.
  31245. */
  31246. ErrorCode[ErrorCode["NGMODULE_BOOTSTRAP_IS_STANDALONE"] = 6009] = "NGMODULE_BOOTSTRAP_IS_STANDALONE";
  31247. /**
  31248. * Indicates that an NgModule is declared with `id: module.id`. This is an anti-pattern that is
  31249. * disabled explicitly in the compiler, that was originally based on a misunderstanding of
  31250. * `NgModule.id`.
  31251. */
  31252. ErrorCode[ErrorCode["WARN_NGMODULE_ID_UNNECESSARY"] = 6100] = "WARN_NGMODULE_ID_UNNECESSARY";
  31253. /**
  31254. * 6999 was previously assigned to NGMODULE_VE_DEPENDENCY_ON_IVY_LIB
  31255. * To prevent any confusion, let's not reassign it.
  31256. */
  31257. /**
  31258. * An element name failed validation against the DOM schema.
  31259. */
  31260. ErrorCode[ErrorCode["SCHEMA_INVALID_ELEMENT"] = 8001] = "SCHEMA_INVALID_ELEMENT";
  31261. /**
  31262. * An element's attribute name failed validation against the DOM schema.
  31263. */
  31264. ErrorCode[ErrorCode["SCHEMA_INVALID_ATTRIBUTE"] = 8002] = "SCHEMA_INVALID_ATTRIBUTE";
  31265. /**
  31266. * No matching directive was found for a `#ref="target"` expression.
  31267. */
  31268. ErrorCode[ErrorCode["MISSING_REFERENCE_TARGET"] = 8003] = "MISSING_REFERENCE_TARGET";
  31269. /**
  31270. * No matching pipe was found for a
  31271. */
  31272. ErrorCode[ErrorCode["MISSING_PIPE"] = 8004] = "MISSING_PIPE";
  31273. /**
  31274. * The left-hand side of an assignment expression was a template variable. Effectively, the
  31275. * template looked like:
  31276. *
  31277. * ```html
  31278. * <ng-template let-something>
  31279. * <button (click)="something = ...">...</button>
  31280. * </ng-template>
  31281. * ```
  31282. *
  31283. * Template variables are read-only.
  31284. */
  31285. ErrorCode[ErrorCode["WRITE_TO_READ_ONLY_VARIABLE"] = 8005] = "WRITE_TO_READ_ONLY_VARIABLE";
  31286. /**
  31287. * A template variable was declared twice. For example:
  31288. *
  31289. * ```html
  31290. * <div *ngFor="let i of items; let i = index">
  31291. * </div>
  31292. * ```
  31293. */
  31294. ErrorCode[ErrorCode["DUPLICATE_VARIABLE_DECLARATION"] = 8006] = "DUPLICATE_VARIABLE_DECLARATION";
  31295. /**
  31296. * A template has a two way binding (two bindings created by a single syntactical element)
  31297. * in which the input and output are going to different places.
  31298. */
  31299. ErrorCode[ErrorCode["SPLIT_TWO_WAY_BINDING"] = 8007] = "SPLIT_TWO_WAY_BINDING";
  31300. /**
  31301. * A directive usage isn't binding to one or more required inputs.
  31302. */
  31303. ErrorCode[ErrorCode["MISSING_REQUIRED_INPUTS"] = 8008] = "MISSING_REQUIRED_INPUTS";
  31304. /**
  31305. * The tracking expression of a `for` loop block is accessing a variable that is unavailable,
  31306. * for example:
  31307. *
  31308. * ```angular-html
  31309. * <ng-template let-ref>
  31310. * @for (item of items; track ref) {}
  31311. * </ng-template>
  31312. * ```
  31313. */
  31314. ErrorCode[ErrorCode["ILLEGAL_FOR_LOOP_TRACK_ACCESS"] = 8009] = "ILLEGAL_FOR_LOOP_TRACK_ACCESS";
  31315. /**
  31316. * The trigger of a `defer` block cannot access its trigger element,
  31317. * either because it doesn't exist or it's in a different view.
  31318. *
  31319. * ```angular-html
  31320. * @defer (on interaction(trigger)) {...}
  31321. *
  31322. * <ng-template>
  31323. * <button #trigger></button>
  31324. * </ng-template>
  31325. * ```
  31326. */
  31327. ErrorCode[ErrorCode["INACCESSIBLE_DEFERRED_TRIGGER_ELEMENT"] = 8010] = "INACCESSIBLE_DEFERRED_TRIGGER_ELEMENT";
  31328. /**
  31329. * A control flow node is projected at the root of a component and is preventing its direct
  31330. * descendants from being projected, because it has more than one root node.
  31331. *
  31332. * ```angular-html
  31333. * <comp>
  31334. * @if (expr) {
  31335. * <div projectsIntoSlot></div>
  31336. * Text preventing the div from being projected
  31337. * }
  31338. * </comp>
  31339. * ```
  31340. */
  31341. ErrorCode[ErrorCode["CONTROL_FLOW_PREVENTING_CONTENT_PROJECTION"] = 8011] = "CONTROL_FLOW_PREVENTING_CONTENT_PROJECTION";
  31342. /**
  31343. * A pipe imported via `@Component.deferredImports` is
  31344. * used outside of a `@defer` block in a template.
  31345. */
  31346. ErrorCode[ErrorCode["DEFERRED_PIPE_USED_EAGERLY"] = 8012] = "DEFERRED_PIPE_USED_EAGERLY";
  31347. /**
  31348. * A directive/component imported via `@Component.deferredImports` is
  31349. * used outside of a `@defer` block in a template.
  31350. */
  31351. ErrorCode[ErrorCode["DEFERRED_DIRECTIVE_USED_EAGERLY"] = 8013] = "DEFERRED_DIRECTIVE_USED_EAGERLY";
  31352. /**
  31353. * A directive/component/pipe imported via `@Component.deferredImports` is
  31354. * also included into the `@Component.imports` list.
  31355. */
  31356. ErrorCode[ErrorCode["DEFERRED_DEPENDENCY_IMPORTED_EAGERLY"] = 8014] = "DEFERRED_DEPENDENCY_IMPORTED_EAGERLY";
  31357. /** An expression is trying to write to an `@let` declaration. */
  31358. ErrorCode[ErrorCode["ILLEGAL_LET_WRITE"] = 8015] = "ILLEGAL_LET_WRITE";
  31359. /** An expression is trying to read an `@let` before it has been defined. */
  31360. ErrorCode[ErrorCode["LET_USED_BEFORE_DEFINITION"] = 8016] = "LET_USED_BEFORE_DEFINITION";
  31361. /** A `@let` declaration conflicts with another symbol in the same scope. */
  31362. ErrorCode[ErrorCode["CONFLICTING_LET_DECLARATION"] = 8017] = "CONFLICTING_LET_DECLARATION";
  31363. /**
  31364. * A two way binding in a template has an incorrect syntax,
  31365. * parentheses outside brackets. For example:
  31366. *
  31367. * ```html
  31368. * <div ([foo])="bar" />
  31369. * ```
  31370. */
  31371. ErrorCode[ErrorCode["INVALID_BANANA_IN_BOX"] = 8101] = "INVALID_BANANA_IN_BOX";
  31372. /**
  31373. * The left side of a nullish coalescing operation is not nullable.
  31374. *
  31375. * ```html
  31376. * {{ foo ?? bar }}
  31377. * ```
  31378. * When the type of foo doesn't include `null` or `undefined`.
  31379. */
  31380. ErrorCode[ErrorCode["NULLISH_COALESCING_NOT_NULLABLE"] = 8102] = "NULLISH_COALESCING_NOT_NULLABLE";
  31381. /**
  31382. * A known control flow directive (e.g. `*ngIf`) is used in a template,
  31383. * but the `CommonModule` is not imported.
  31384. */
  31385. ErrorCode[ErrorCode["MISSING_CONTROL_FLOW_DIRECTIVE"] = 8103] = "MISSING_CONTROL_FLOW_DIRECTIVE";
  31386. /**
  31387. * A text attribute is not interpreted as a binding but likely intended to be.
  31388. *
  31389. * For example:
  31390. * ```html
  31391. * <div
  31392. * attr.x="value"
  31393. * class.blue="true"
  31394. * style.margin-right.px="5">
  31395. * </div>
  31396. * ```
  31397. *
  31398. * All of the above attributes will just be static text attributes and will not be interpreted as
  31399. * bindings by the compiler.
  31400. */
  31401. ErrorCode[ErrorCode["TEXT_ATTRIBUTE_NOT_BINDING"] = 8104] = "TEXT_ATTRIBUTE_NOT_BINDING";
  31402. /**
  31403. * NgForOf is used in a template, but the user forgot to include let
  31404. * in their statement.
  31405. *
  31406. * For example:
  31407. * ```html
  31408. * <ul><li *ngFor="item of items">{{item["name"]}};</li></ul>
  31409. * ```
  31410. */
  31411. ErrorCode[ErrorCode["MISSING_NGFOROF_LET"] = 8105] = "MISSING_NGFOROF_LET";
  31412. /**
  31413. * Indicates that the binding suffix is not supported
  31414. *
  31415. * Style bindings support suffixes like `style.width.px`, `.em`, and `.%`.
  31416. * These suffixes are _not_ supported for attribute bindings.
  31417. *
  31418. * For example `[attr.width.px]="5"` becomes `width.px="5"` when bound.
  31419. * This is almost certainly unintentional and this error is meant to
  31420. * surface this mistake to the developer.
  31421. */
  31422. ErrorCode[ErrorCode["SUFFIX_NOT_SUPPORTED"] = 8106] = "SUFFIX_NOT_SUPPORTED";
  31423. /**
  31424. * The left side of an optional chain operation is not nullable.
  31425. *
  31426. * ```html
  31427. * {{ foo?.bar }}
  31428. * {{ foo?.['bar'] }}
  31429. * {{ foo?.() }}
  31430. * ```
  31431. * When the type of foo doesn't include `null` or `undefined`.
  31432. */
  31433. ErrorCode[ErrorCode["OPTIONAL_CHAIN_NOT_NULLABLE"] = 8107] = "OPTIONAL_CHAIN_NOT_NULLABLE";
  31434. /**
  31435. * `ngSkipHydration` should not be a binding (it should be a static attribute).
  31436. *
  31437. * For example:
  31438. * ```html
  31439. * <my-cmp [ngSkipHydration]="someTruthyVar" />
  31440. * ```
  31441. *
  31442. * `ngSkipHydration` cannot be a binding and can not have values other than "true" or an empty
  31443. * value
  31444. */
  31445. ErrorCode[ErrorCode["SKIP_HYDRATION_NOT_STATIC"] = 8108] = "SKIP_HYDRATION_NOT_STATIC";
  31446. /**
  31447. * Signal functions should be invoked when interpolated in templates.
  31448. *
  31449. * For example:
  31450. * ```html
  31451. * {{ mySignal() }}
  31452. * ```
  31453. */
  31454. ErrorCode[ErrorCode["INTERPOLATED_SIGNAL_NOT_INVOKED"] = 8109] = "INTERPOLATED_SIGNAL_NOT_INVOKED";
  31455. /**
  31456. * Initializer-based APIs can only be invoked from inside of an initializer.
  31457. *
  31458. * ```ts
  31459. * // Allowed
  31460. * myInput = input();
  31461. *
  31462. * // Not allowed
  31463. * function myInput() {
  31464. * return input();
  31465. * }
  31466. * ```
  31467. */
  31468. ErrorCode[ErrorCode["UNSUPPORTED_INITIALIZER_API_USAGE"] = 8110] = "UNSUPPORTED_INITIALIZER_API_USAGE";
  31469. /**
  31470. * A function in an event binding is not called.
  31471. *
  31472. * For example:
  31473. * ```html
  31474. * <button (click)="myFunc"></button>
  31475. * ```
  31476. *
  31477. * This will not call `myFunc` when the button is clicked. Instead, it should be
  31478. * `<button (click)="myFunc()"></button>`.
  31479. */
  31480. ErrorCode[ErrorCode["UNINVOKED_FUNCTION_IN_EVENT_BINDING"] = 8111] = "UNINVOKED_FUNCTION_IN_EVENT_BINDING";
  31481. /**
  31482. * A `@let` declaration in a template isn't used.
  31483. *
  31484. * For example:
  31485. * ```angular-html
  31486. * @let used = 1; <!-- Not an error -->
  31487. * @let notUsed = 2; <!-- Error -->
  31488. *
  31489. * {{used}}
  31490. * ```
  31491. */
  31492. ErrorCode[ErrorCode["UNUSED_LET_DECLARATION"] = 8112] = "UNUSED_LET_DECLARATION";
  31493. /**
  31494. * A symbol referenced in `@Component.imports` isn't being used within the template.
  31495. */
  31496. ErrorCode[ErrorCode["UNUSED_STANDALONE_IMPORTS"] = 8113] = "UNUSED_STANDALONE_IMPORTS";
  31497. /**
  31498. * The template type-checking engine would need to generate an inline type check block for a
  31499. * component, but the current type-checking environment doesn't support it.
  31500. */
  31501. ErrorCode[ErrorCode["INLINE_TCB_REQUIRED"] = 8900] = "INLINE_TCB_REQUIRED";
  31502. /**
  31503. * The template type-checking engine would need to generate an inline type constructor for a
  31504. * directive or component, but the current type-checking environment doesn't support it.
  31505. */
  31506. ErrorCode[ErrorCode["INLINE_TYPE_CTOR_REQUIRED"] = 8901] = "INLINE_TYPE_CTOR_REQUIRED";
  31507. /**
  31508. * An injectable already has a `ɵprov` property.
  31509. */
  31510. ErrorCode[ErrorCode["INJECTABLE_DUPLICATE_PROV"] = 9001] = "INJECTABLE_DUPLICATE_PROV";
  31511. // 10XXX error codes are reserved for diagnostics with categories other than
  31512. // `ts.DiagnosticCategory.Error`. These diagnostics are generated by the compiler when configured
  31513. // to do so by a tool such as the Language Service, or by the Language Service itself.
  31514. /**
  31515. * Suggest users to enable `strictTemplates` to make use of full capabilities
  31516. * provided by Angular language service.
  31517. */
  31518. ErrorCode[ErrorCode["SUGGEST_STRICT_TEMPLATES"] = 10001] = "SUGGEST_STRICT_TEMPLATES";
  31519. /**
  31520. * Indicates that a particular structural directive provides advanced type narrowing
  31521. * functionality, but the current template type-checking configuration does not allow its usage in
  31522. * type inference.
  31523. */
  31524. ErrorCode[ErrorCode["SUGGEST_SUBOPTIMAL_TYPE_INFERENCE"] = 10002] = "SUGGEST_SUBOPTIMAL_TYPE_INFERENCE";
  31525. /**
  31526. * In local compilation mode a const is required to be resolved statically but cannot be so since
  31527. * it is imported from a file outside of the compilation unit. This usually happens with const
  31528. * being used as Angular decorators parameters such as `@Component.template`,
  31529. * `@HostListener.eventName`, etc.
  31530. */
  31531. ErrorCode[ErrorCode["LOCAL_COMPILATION_UNRESOLVED_CONST"] = 11001] = "LOCAL_COMPILATION_UNRESOLVED_CONST";
  31532. /**
  31533. * In local compilation mode a certain expression or syntax is not supported. This is usually
  31534. * because the expression/syntax is not very common and so we did not add support for it yet. This
  31535. * can be changed in the future and support for more expressions could be added if need be.
  31536. * Meanwhile, this error is thrown to indicate a current unavailability.
  31537. */
  31538. ErrorCode[ErrorCode["LOCAL_COMPILATION_UNSUPPORTED_EXPRESSION"] = 11003] = "LOCAL_COMPILATION_UNSUPPORTED_EXPRESSION";
  31539. })(exports.ErrorCode || (exports.ErrorCode = {}));
  31540. /**
  31541. * Contains a set of error messages that have detailed guides at angular.io.
  31542. * Full list of available error guides can be found at https://angular.dev/errors
  31543. */
  31544. const COMPILER_ERRORS_WITH_GUIDES = new Set([
  31545. exports.ErrorCode.DECORATOR_ARG_NOT_LITERAL,
  31546. exports.ErrorCode.IMPORT_CYCLE_DETECTED,
  31547. exports.ErrorCode.PARAM_MISSING_TOKEN,
  31548. exports.ErrorCode.SCHEMA_INVALID_ELEMENT,
  31549. exports.ErrorCode.SCHEMA_INVALID_ATTRIBUTE,
  31550. exports.ErrorCode.MISSING_REFERENCE_TARGET,
  31551. exports.ErrorCode.COMPONENT_INVALID_SHADOW_DOM_SELECTOR,
  31552. exports.ErrorCode.WARN_NGMODULE_ID_UNNECESSARY,
  31553. ]);
  31554. function ngErrorCode(code) {
  31555. return parseInt('-99' + code);
  31556. }
  31557. class FatalDiagnosticError extends Error {
  31558. code;
  31559. node;
  31560. diagnosticMessage;
  31561. relatedInformation;
  31562. constructor(code, node, diagnosticMessage, relatedInformation) {
  31563. super(`FatalDiagnosticError: Code: ${code}, Message: ${ts.flattenDiagnosticMessageText(diagnosticMessage, '\n')}`);
  31564. this.code = code;
  31565. this.node = node;
  31566. this.diagnosticMessage = diagnosticMessage;
  31567. this.relatedInformation = relatedInformation;
  31568. // Extending `Error` ends up breaking some internal tests. This appears to be a known issue
  31569. // when extending errors in TS and the workaround is to explicitly set the prototype.
  31570. // https://stackoverflow.com/questions/41102060/typescript-extending-error-class
  31571. Object.setPrototypeOf(this, new.target.prototype);
  31572. }
  31573. /**
  31574. * @internal
  31575. */
  31576. _isFatalDiagnosticError = true;
  31577. toDiagnostic() {
  31578. return makeDiagnostic(this.code, this.node, this.diagnosticMessage, this.relatedInformation);
  31579. }
  31580. }
  31581. function makeDiagnostic(code, node, messageText, relatedInformation, category = ts.DiagnosticCategory.Error) {
  31582. node = ts.getOriginalNode(node);
  31583. return {
  31584. category,
  31585. code: ngErrorCode(code),
  31586. file: ts.getOriginalNode(node).getSourceFile(),
  31587. start: node.getStart(undefined, false),
  31588. length: node.getWidth(),
  31589. messageText,
  31590. relatedInformation,
  31591. };
  31592. }
  31593. function makeDiagnosticChain(messageText, next) {
  31594. return {
  31595. category: ts.DiagnosticCategory.Message,
  31596. code: 0,
  31597. messageText,
  31598. next,
  31599. };
  31600. }
  31601. function makeRelatedInformation(node, messageText) {
  31602. node = ts.getOriginalNode(node);
  31603. return {
  31604. category: ts.DiagnosticCategory.Message,
  31605. code: 0,
  31606. file: node.getSourceFile(),
  31607. start: node.getStart(),
  31608. length: node.getWidth(),
  31609. messageText,
  31610. };
  31611. }
  31612. function addDiagnosticChain(messageText, add) {
  31613. if (typeof messageText === 'string') {
  31614. return makeDiagnosticChain(messageText, add);
  31615. }
  31616. if (messageText.next === undefined) {
  31617. messageText.next = add;
  31618. }
  31619. else {
  31620. messageText.next.push(...add);
  31621. }
  31622. return messageText;
  31623. }
  31624. function isFatalDiagnosticError(err) {
  31625. return err._isFatalDiagnosticError === true;
  31626. }
  31627. /**
  31628. * Enum holding the name of each extended template diagnostic. The name is used as a user-meaningful
  31629. * value for configuring the diagnostic in the project's options.
  31630. *
  31631. * See the corresponding `ErrorCode` for documentation about each specific error.
  31632. * packages/compiler-cli/src/ngtsc/diagnostics/src/error_code.ts
  31633. *
  31634. * @publicApi
  31635. */
  31636. exports.ExtendedTemplateDiagnosticName = void 0;
  31637. (function (ExtendedTemplateDiagnosticName) {
  31638. ExtendedTemplateDiagnosticName["INVALID_BANANA_IN_BOX"] = "invalidBananaInBox";
  31639. ExtendedTemplateDiagnosticName["NULLISH_COALESCING_NOT_NULLABLE"] = "nullishCoalescingNotNullable";
  31640. ExtendedTemplateDiagnosticName["OPTIONAL_CHAIN_NOT_NULLABLE"] = "optionalChainNotNullable";
  31641. ExtendedTemplateDiagnosticName["MISSING_CONTROL_FLOW_DIRECTIVE"] = "missingControlFlowDirective";
  31642. ExtendedTemplateDiagnosticName["TEXT_ATTRIBUTE_NOT_BINDING"] = "textAttributeNotBinding";
  31643. ExtendedTemplateDiagnosticName["UNINVOKED_FUNCTION_IN_EVENT_BINDING"] = "uninvokedFunctionInEventBinding";
  31644. ExtendedTemplateDiagnosticName["MISSING_NGFOROF_LET"] = "missingNgForOfLet";
  31645. ExtendedTemplateDiagnosticName["SUFFIX_NOT_SUPPORTED"] = "suffixNotSupported";
  31646. ExtendedTemplateDiagnosticName["SKIP_HYDRATION_NOT_STATIC"] = "skipHydrationNotStatic";
  31647. ExtendedTemplateDiagnosticName["INTERPOLATED_SIGNAL_NOT_INVOKED"] = "interpolatedSignalNotInvoked";
  31648. ExtendedTemplateDiagnosticName["CONTROL_FLOW_PREVENTING_CONTENT_PROJECTION"] = "controlFlowPreventingContentProjection";
  31649. ExtendedTemplateDiagnosticName["UNUSED_LET_DECLARATION"] = "unusedLetDeclaration";
  31650. ExtendedTemplateDiagnosticName["UNUSED_STANDALONE_IMPORTS"] = "unusedStandaloneImports";
  31651. })(exports.ExtendedTemplateDiagnosticName || (exports.ExtendedTemplateDiagnosticName = {}));
  31652. /**
  31653. * The default `FileSystem` that will always fail.
  31654. *
  31655. * This is a way of ensuring that the developer consciously chooses and
  31656. * configures the `FileSystem` before using it; particularly important when
  31657. * considering static functions like `absoluteFrom()` which rely on
  31658. * the `FileSystem` under the hood.
  31659. */
  31660. class InvalidFileSystem {
  31661. exists(path) {
  31662. throw makeError();
  31663. }
  31664. readFile(path) {
  31665. throw makeError();
  31666. }
  31667. readFileBuffer(path) {
  31668. throw makeError();
  31669. }
  31670. writeFile(path, data, exclusive) {
  31671. throw makeError();
  31672. }
  31673. removeFile(path) {
  31674. throw makeError();
  31675. }
  31676. symlink(target, path) {
  31677. throw makeError();
  31678. }
  31679. readdir(path) {
  31680. throw makeError();
  31681. }
  31682. lstat(path) {
  31683. throw makeError();
  31684. }
  31685. stat(path) {
  31686. throw makeError();
  31687. }
  31688. pwd() {
  31689. throw makeError();
  31690. }
  31691. chdir(path) {
  31692. throw makeError();
  31693. }
  31694. extname(path) {
  31695. throw makeError();
  31696. }
  31697. copyFile(from, to) {
  31698. throw makeError();
  31699. }
  31700. moveFile(from, to) {
  31701. throw makeError();
  31702. }
  31703. ensureDir(path) {
  31704. throw makeError();
  31705. }
  31706. removeDeep(path) {
  31707. throw makeError();
  31708. }
  31709. isCaseSensitive() {
  31710. throw makeError();
  31711. }
  31712. resolve(...paths) {
  31713. throw makeError();
  31714. }
  31715. dirname(file) {
  31716. throw makeError();
  31717. }
  31718. join(basePath, ...paths) {
  31719. throw makeError();
  31720. }
  31721. isRoot(path) {
  31722. throw makeError();
  31723. }
  31724. isRooted(path) {
  31725. throw makeError();
  31726. }
  31727. relative(from, to) {
  31728. throw makeError();
  31729. }
  31730. basename(filePath, extension) {
  31731. throw makeError();
  31732. }
  31733. realpath(filePath) {
  31734. throw makeError();
  31735. }
  31736. getDefaultLibLocation() {
  31737. throw makeError();
  31738. }
  31739. normalize(path) {
  31740. throw makeError();
  31741. }
  31742. }
  31743. function makeError() {
  31744. return new Error('FileSystem has not been configured. Please call `setFileSystem()` before calling this method.');
  31745. }
  31746. const TS_DTS_JS_EXTENSION = /(?:\.d)?\.ts$|\.js$/;
  31747. /**
  31748. * Remove a .ts, .d.ts, or .js extension from a file name.
  31749. */
  31750. function stripExtension(path) {
  31751. return path.replace(TS_DTS_JS_EXTENSION, '');
  31752. }
  31753. function getSourceFileOrError(program, fileName) {
  31754. const sf = program.getSourceFile(fileName);
  31755. if (sf === undefined) {
  31756. throw new Error(`Program does not contain "${fileName}" - available files are ${program
  31757. .getSourceFiles()
  31758. .map((sf) => sf.fileName)
  31759. .join(', ')}`);
  31760. }
  31761. return sf;
  31762. }
  31763. let fs = new InvalidFileSystem();
  31764. function getFileSystem() {
  31765. return fs;
  31766. }
  31767. function setFileSystem(fileSystem) {
  31768. fs = fileSystem;
  31769. }
  31770. /**
  31771. * Convert the path `path` to an `AbsoluteFsPath`, throwing an error if it's not an absolute path.
  31772. */
  31773. function absoluteFrom(path) {
  31774. if (!fs.isRooted(path)) {
  31775. throw new Error(`Internal Error: absoluteFrom(${path}): path is not absolute`);
  31776. }
  31777. return fs.resolve(path);
  31778. }
  31779. const ABSOLUTE_PATH = Symbol('AbsolutePath');
  31780. /**
  31781. * Extract an `AbsoluteFsPath` from a `ts.SourceFile`-like object.
  31782. */
  31783. function absoluteFromSourceFile(sf) {
  31784. const sfWithPatch = sf;
  31785. if (sfWithPatch[ABSOLUTE_PATH] === undefined) {
  31786. sfWithPatch[ABSOLUTE_PATH] = fs.resolve(sfWithPatch.fileName);
  31787. }
  31788. // Non-null assertion needed since TS doesn't narrow the type of fields that use a symbol as a key
  31789. // apparently.
  31790. return sfWithPatch[ABSOLUTE_PATH];
  31791. }
  31792. /**
  31793. * Static access to `dirname`.
  31794. */
  31795. function dirname(file) {
  31796. return fs.dirname(file);
  31797. }
  31798. /**
  31799. * Static access to `join`.
  31800. */
  31801. function join(basePath, ...paths) {
  31802. return fs.join(basePath, ...paths);
  31803. }
  31804. /**
  31805. * Static access to `resolve`s.
  31806. */
  31807. function resolve(basePath, ...paths) {
  31808. return fs.resolve(basePath, ...paths);
  31809. }
  31810. /**
  31811. * Static access to `isRooted`.
  31812. */
  31813. function isRooted(path) {
  31814. return fs.isRooted(path);
  31815. }
  31816. /**
  31817. * Static access to `relative`.
  31818. */
  31819. function relative(from, to) {
  31820. return fs.relative(from, to);
  31821. }
  31822. /**
  31823. * Returns true if the given path is locally relative.
  31824. *
  31825. * This is used to work out if the given path is relative (i.e. not absolute) but also is not
  31826. * escaping the current directory.
  31827. */
  31828. function isLocalRelativePath(relativePath) {
  31829. return !isRooted(relativePath) && !relativePath.startsWith('..');
  31830. }
  31831. /**
  31832. * Converts a path to a form suitable for use as a relative module import specifier.
  31833. *
  31834. * In other words it adds the `./` to the path if it is locally relative.
  31835. */
  31836. function toRelativeImport(relativePath) {
  31837. return isLocalRelativePath(relativePath) ? `./${relativePath}` : relativePath;
  31838. }
  31839. const LogicalProjectPath = {
  31840. /**
  31841. * Get the relative path between two `LogicalProjectPath`s.
  31842. *
  31843. * This will return a `PathSegment` which would be a valid module specifier to use in `from` when
  31844. * importing from `to`.
  31845. */
  31846. relativePathBetween: function (from, to) {
  31847. const relativePath = relative(dirname(resolve(from)), resolve(to));
  31848. return toRelativeImport(relativePath);
  31849. },
  31850. };
  31851. /**
  31852. * A utility class which can translate absolute paths to source files into logical paths in
  31853. * TypeScript's logical file system, based on the root directories of the project.
  31854. */
  31855. class LogicalFileSystem {
  31856. compilerHost;
  31857. /**
  31858. * The root directories of the project, sorted with the longest path first.
  31859. */
  31860. rootDirs;
  31861. /**
  31862. * The same root directories as `rootDirs` but with each one converted to its
  31863. * canonical form for matching in case-insensitive file-systems.
  31864. */
  31865. canonicalRootDirs;
  31866. /**
  31867. * A cache of file paths to project paths, because computation of these paths is slightly
  31868. * expensive.
  31869. */
  31870. cache = new Map();
  31871. constructor(rootDirs, compilerHost) {
  31872. this.compilerHost = compilerHost;
  31873. // Make a copy and sort it by length in reverse order (longest first). This speeds up lookups,
  31874. // since there's no need to keep going through the array once a match is found.
  31875. this.rootDirs = rootDirs.concat([]).sort((a, b) => b.length - a.length);
  31876. this.canonicalRootDirs = this.rootDirs.map((dir) => this.compilerHost.getCanonicalFileName(dir));
  31877. }
  31878. /**
  31879. * Get the logical path in the project of a `ts.SourceFile`.
  31880. *
  31881. * This method is provided as a convenient alternative to calling
  31882. * `logicalPathOfFile(absoluteFromSourceFile(sf))`.
  31883. */
  31884. logicalPathOfSf(sf) {
  31885. return this.logicalPathOfFile(absoluteFromSourceFile(sf));
  31886. }
  31887. /**
  31888. * Get the logical path in the project of a source file.
  31889. *
  31890. * @returns A `LogicalProjectPath` to the source file, or `null` if the source file is not in any
  31891. * of the TS project's root directories.
  31892. */
  31893. logicalPathOfFile(physicalFile) {
  31894. if (!this.cache.has(physicalFile)) {
  31895. const canonicalFilePath = this.compilerHost.getCanonicalFileName(physicalFile);
  31896. let logicalFile = null;
  31897. for (let i = 0; i < this.rootDirs.length; i++) {
  31898. const rootDir = this.rootDirs[i];
  31899. const canonicalRootDir = this.canonicalRootDirs[i];
  31900. if (isWithinBasePath(canonicalRootDir, canonicalFilePath)) {
  31901. // Note that we match against canonical paths but then create the logical path from
  31902. // original paths.
  31903. logicalFile = this.createLogicalProjectPath(physicalFile, rootDir);
  31904. // The logical project does not include any special "node_modules" nested directories.
  31905. if (logicalFile.indexOf('/node_modules/') !== -1) {
  31906. logicalFile = null;
  31907. }
  31908. else {
  31909. break;
  31910. }
  31911. }
  31912. }
  31913. this.cache.set(physicalFile, logicalFile);
  31914. }
  31915. return this.cache.get(physicalFile);
  31916. }
  31917. createLogicalProjectPath(file, rootDir) {
  31918. const logicalPath = stripExtension(file.slice(rootDir.length));
  31919. return (logicalPath.startsWith('/') ? logicalPath : '/' + logicalPath);
  31920. }
  31921. }
  31922. /**
  31923. * Is the `path` a descendant of the `base`?
  31924. * E.g. `foo/bar/zee` is within `foo/bar` but not within `foo/car`.
  31925. */
  31926. function isWithinBasePath(base, path) {
  31927. return isLocalRelativePath(relative(base, path));
  31928. }
  31929. /// <reference types="node" />
  31930. /**
  31931. * A wrapper around the Node.js file-system that supports path manipulation.
  31932. */
  31933. class NodeJSPathManipulation {
  31934. pwd() {
  31935. return this.normalize(process.cwd());
  31936. }
  31937. chdir(dir) {
  31938. process.chdir(dir);
  31939. }
  31940. resolve(...paths) {
  31941. return this.normalize(p__namespace.resolve(...paths));
  31942. }
  31943. dirname(file) {
  31944. return this.normalize(p__namespace.dirname(file));
  31945. }
  31946. join(basePath, ...paths) {
  31947. return this.normalize(p__namespace.join(basePath, ...paths));
  31948. }
  31949. isRoot(path) {
  31950. return this.dirname(path) === this.normalize(path);
  31951. }
  31952. isRooted(path) {
  31953. return p__namespace.isAbsolute(path);
  31954. }
  31955. relative(from, to) {
  31956. return this.normalize(p__namespace.relative(from, to));
  31957. }
  31958. basename(filePath, extension) {
  31959. return p__namespace.basename(filePath, extension);
  31960. }
  31961. extname(path) {
  31962. return p__namespace.extname(path);
  31963. }
  31964. normalize(path) {
  31965. // Convert backslashes to forward slashes
  31966. return path.replace(/\\/g, '/');
  31967. }
  31968. }
  31969. // G3-ESM-MARKER: G3 uses CommonJS, but externally everything in ESM.
  31970. // CommonJS/ESM interop for determining the current file name and containing dir.
  31971. const isCommonJS = typeof __filename !== 'undefined';
  31972. const currentFileUrl = isCommonJS ? null : (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('checker-5pyJrZ9G.cjs', document.baseURI).href));
  31973. const currentFileName = isCommonJS ? __filename : url.fileURLToPath(currentFileUrl);
  31974. /**
  31975. * A wrapper around the Node.js file-system that supports readonly operations and path manipulation.
  31976. */
  31977. class NodeJSReadonlyFileSystem extends NodeJSPathManipulation {
  31978. _caseSensitive = undefined;
  31979. isCaseSensitive() {
  31980. if (this._caseSensitive === undefined) {
  31981. // Note the use of the real file-system is intentional:
  31982. // `this.exists()` relies upon `isCaseSensitive()` so that would cause an infinite recursion.
  31983. this._caseSensitive = !fs$1.existsSync(this.normalize(toggleCase(currentFileName)));
  31984. }
  31985. return this._caseSensitive;
  31986. }
  31987. exists(path) {
  31988. return fs$1.existsSync(path);
  31989. }
  31990. readFile(path) {
  31991. return fs$1.readFileSync(path, 'utf8');
  31992. }
  31993. readFileBuffer(path) {
  31994. return fs$1.readFileSync(path);
  31995. }
  31996. readdir(path) {
  31997. return fs$1.readdirSync(path);
  31998. }
  31999. lstat(path) {
  32000. return fs$1.lstatSync(path);
  32001. }
  32002. stat(path) {
  32003. return fs$1.statSync(path);
  32004. }
  32005. realpath(path) {
  32006. return this.resolve(fs$1.realpathSync(path));
  32007. }
  32008. getDefaultLibLocation() {
  32009. // G3-ESM-MARKER: G3 uses CommonJS, but externally everything in ESM.
  32010. const requireFn = isCommonJS ? require : module$1.createRequire(currentFileUrl);
  32011. return this.resolve(requireFn.resolve('typescript'), '..');
  32012. }
  32013. }
  32014. /**
  32015. * A wrapper around the Node.js file-system (i.e. the `fs` package).
  32016. */
  32017. class NodeJSFileSystem extends NodeJSReadonlyFileSystem {
  32018. writeFile(path, data, exclusive = false) {
  32019. fs$1.writeFileSync(path, data, exclusive ? { flag: 'wx' } : undefined);
  32020. }
  32021. removeFile(path) {
  32022. fs$1.unlinkSync(path);
  32023. }
  32024. symlink(target, path) {
  32025. fs$1.symlinkSync(target, path);
  32026. }
  32027. copyFile(from, to) {
  32028. fs$1.copyFileSync(from, to);
  32029. }
  32030. moveFile(from, to) {
  32031. fs$1.renameSync(from, to);
  32032. }
  32033. ensureDir(path) {
  32034. fs$1.mkdirSync(path, { recursive: true });
  32035. }
  32036. removeDeep(path) {
  32037. fs$1.rmdirSync(path, { recursive: true });
  32038. }
  32039. }
  32040. /**
  32041. * Toggle the case of each character in a string.
  32042. */
  32043. function toggleCase(str) {
  32044. return str.replace(/\w/g, (ch) => ch.toUpperCase() === ch ? ch.toLowerCase() : ch.toUpperCase());
  32045. }
  32046. const TS = /\.tsx?$/i;
  32047. const D_TS = /\.d\.ts$/i;
  32048. function isSymbolWithValueDeclaration(symbol) {
  32049. // If there is a value declaration set, then the `declarations` property is never undefined. We
  32050. // still check for the property to exist as this matches with the type that `symbol` is narrowed
  32051. // to.
  32052. return (symbol != null && symbol.valueDeclaration !== undefined && symbol.declarations !== undefined);
  32053. }
  32054. function isDtsPath(filePath) {
  32055. return D_TS.test(filePath);
  32056. }
  32057. function isNonDeclarationTsPath(filePath) {
  32058. return TS.test(filePath) && !D_TS.test(filePath);
  32059. }
  32060. function isFromDtsFile(node) {
  32061. let sf = node.getSourceFile();
  32062. if (sf === undefined) {
  32063. sf = ts.getOriginalNode(node).getSourceFile();
  32064. }
  32065. return sf !== undefined && sf.isDeclarationFile;
  32066. }
  32067. function nodeNameForError(node) {
  32068. if (node.name !== undefined && ts.isIdentifier(node.name)) {
  32069. return node.name.text;
  32070. }
  32071. else {
  32072. const kind = ts.SyntaxKind[node.kind];
  32073. const { line, character } = ts.getLineAndCharacterOfPosition(node.getSourceFile(), node.getStart());
  32074. return `${kind}@${line}:${character}`;
  32075. }
  32076. }
  32077. function getSourceFile(node) {
  32078. // In certain transformation contexts, `ts.Node.getSourceFile()` can actually return `undefined`,
  32079. // despite the type signature not allowing it. In that event, get the `ts.SourceFile` via the
  32080. // original node instead (which works).
  32081. const directSf = node.getSourceFile();
  32082. return directSf !== undefined ? directSf : ts.getOriginalNode(node).getSourceFile();
  32083. }
  32084. function getSourceFileOrNull(program, fileName) {
  32085. return program.getSourceFile(fileName) || null;
  32086. }
  32087. function getTokenAtPosition(sf, pos) {
  32088. // getTokenAtPosition is part of TypeScript's private API.
  32089. return ts.getTokenAtPosition(sf, pos);
  32090. }
  32091. function identifierOfNode(decl) {
  32092. if (decl.name !== undefined && ts.isIdentifier(decl.name)) {
  32093. return decl.name;
  32094. }
  32095. else {
  32096. return null;
  32097. }
  32098. }
  32099. function isDeclaration(node) {
  32100. return isValueDeclaration(node) || isTypeDeclaration(node);
  32101. }
  32102. function isValueDeclaration(node) {
  32103. return (ts.isClassDeclaration(node) || ts.isFunctionDeclaration(node) || ts.isVariableDeclaration(node));
  32104. }
  32105. function isTypeDeclaration(node) {
  32106. return (ts.isEnumDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isInterfaceDeclaration(node));
  32107. }
  32108. function isNamedDeclaration(node) {
  32109. const namedNode = node;
  32110. return namedNode.name !== undefined && ts.isIdentifier(namedNode.name);
  32111. }
  32112. function getRootDirs(host, options) {
  32113. const rootDirs = [];
  32114. const cwd = host.getCurrentDirectory();
  32115. const fs = getFileSystem();
  32116. if (options.rootDirs !== undefined) {
  32117. rootDirs.push(...options.rootDirs);
  32118. }
  32119. else if (options.rootDir !== undefined) {
  32120. rootDirs.push(options.rootDir);
  32121. }
  32122. else {
  32123. rootDirs.push(cwd);
  32124. }
  32125. // In Windows the above might not always return posix separated paths
  32126. // See:
  32127. // https://github.com/Microsoft/TypeScript/blob/3f7357d37f66c842d70d835bc925ec2a873ecfec/src/compiler/sys.ts#L650
  32128. // Also compiler options might be set via an API which doesn't normalize paths
  32129. return rootDirs.map((rootDir) => fs.resolve(cwd, host.getCanonicalFileName(rootDir)));
  32130. }
  32131. function nodeDebugInfo(node) {
  32132. const sf = getSourceFile(node);
  32133. const { line, character } = ts.getLineAndCharacterOfPosition(sf, node.pos);
  32134. return `[${sf.fileName}: ${ts.SyntaxKind[node.kind]} @ ${line}:${character}]`;
  32135. }
  32136. /**
  32137. * Resolve the specified `moduleName` using the given `compilerOptions` and `compilerHost`.
  32138. *
  32139. * This helper will attempt to use the `CompilerHost.resolveModuleNames()` method if available.
  32140. * Otherwise it will fallback on the `ts.ResolveModuleName()` function.
  32141. */
  32142. function resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost, moduleResolutionCache) {
  32143. if (compilerHost.resolveModuleNames) {
  32144. return compilerHost.resolveModuleNames([moduleName], containingFile, undefined, // reusedNames
  32145. undefined, // redirectedReference
  32146. compilerOptions)[0];
  32147. }
  32148. else {
  32149. return ts.resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost, moduleResolutionCache !== null ? moduleResolutionCache : undefined).resolvedModule;
  32150. }
  32151. }
  32152. /** Returns true if the node is an assignment expression. */
  32153. function isAssignment(node) {
  32154. return ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.EqualsToken;
  32155. }
  32156. /**
  32157. * Obtains the non-redirected source file for `sf`.
  32158. */
  32159. function toUnredirectedSourceFile(sf) {
  32160. const redirectInfo = sf.redirectInfo;
  32161. if (redirectInfo === undefined) {
  32162. return sf;
  32163. }
  32164. return redirectInfo.unredirected;
  32165. }
  32166. /**
  32167. * Find the name, if any, by which a node is exported from a given file.
  32168. */
  32169. function findExportedNameOfNode(target, file, reflector) {
  32170. const exports = reflector.getExportsOfModule(file);
  32171. if (exports === null) {
  32172. return null;
  32173. }
  32174. const declaredName = isNamedDeclaration(target) ? target.name.text : null;
  32175. // Look for the export which declares the node.
  32176. let foundExportName = null;
  32177. for (const [exportName, declaration] of exports) {
  32178. if (declaration.node !== target) {
  32179. continue;
  32180. }
  32181. if (exportName === declaredName) {
  32182. // A non-alias export exists which is always preferred, so use that one.
  32183. return exportName;
  32184. }
  32185. foundExportName = exportName;
  32186. }
  32187. return foundExportName;
  32188. }
  32189. /**
  32190. * Flags which alter the imports generated by the `ReferenceEmitter`.
  32191. */
  32192. exports.ImportFlags = void 0;
  32193. (function (ImportFlags) {
  32194. ImportFlags[ImportFlags["None"] = 0] = "None";
  32195. /**
  32196. * Force the generation of a new import when generating a reference, even if an identifier already
  32197. * exists in the target file which could be used instead.
  32198. *
  32199. * This is sometimes required if there's a risk TypeScript might remove imports during emit.
  32200. */
  32201. ImportFlags[ImportFlags["ForceNewImport"] = 1] = "ForceNewImport";
  32202. /**
  32203. * Don't make use of any aliasing information when emitting a reference.
  32204. *
  32205. * This is sometimes required if emitting into a context where generated references will be fed
  32206. * into TypeScript and type-checked (such as in template type-checking).
  32207. */
  32208. ImportFlags[ImportFlags["NoAliasing"] = 2] = "NoAliasing";
  32209. /**
  32210. * Indicates that an import to a type-only declaration is allowed.
  32211. *
  32212. * For references that occur in type-positions, the referred declaration may be a type-only
  32213. * declaration that is not retained during emit. Including this flag allows to emit references to
  32214. * type-only declarations as used in e.g. template type-checking.
  32215. */
  32216. ImportFlags[ImportFlags["AllowTypeImports"] = 4] = "AllowTypeImports";
  32217. /**
  32218. * Indicates that importing from a declaration file using a relative import path is allowed.
  32219. *
  32220. * The generated imports should normally use module specifiers that are valid for use in
  32221. * production code, where arbitrary relative imports into e.g. node_modules are not allowed. For
  32222. * template type-checking code it is however acceptable to use relative imports, as such files are
  32223. * never emitted to JS code.
  32224. *
  32225. * Non-declaration files have to be contained within a configured `rootDir` so using relative
  32226. * paths may not be possible for those, hence this flag only applies when importing from a
  32227. * declaration file.
  32228. */
  32229. ImportFlags[ImportFlags["AllowRelativeDtsImports"] = 8] = "AllowRelativeDtsImports";
  32230. /**
  32231. * Indicates that references coming from ambient imports are allowed.
  32232. */
  32233. ImportFlags[ImportFlags["AllowAmbientReferences"] = 16] = "AllowAmbientReferences";
  32234. })(exports.ImportFlags || (exports.ImportFlags = {}));
  32235. exports.ReferenceEmitKind = void 0;
  32236. (function (ReferenceEmitKind) {
  32237. ReferenceEmitKind[ReferenceEmitKind["Success"] = 0] = "Success";
  32238. ReferenceEmitKind[ReferenceEmitKind["Failed"] = 1] = "Failed";
  32239. })(exports.ReferenceEmitKind || (exports.ReferenceEmitKind = {}));
  32240. /**
  32241. * Verifies that a reference was emitted successfully, or raises a `FatalDiagnosticError` otherwise.
  32242. * @param result The emit result that should have been successful.
  32243. * @param origin The node that is used to report the failure diagnostic.
  32244. * @param typeKind The kind of the symbol that the reference represents, e.g. 'component' or
  32245. * 'class'.
  32246. */
  32247. function assertSuccessfulReferenceEmit(result, origin, typeKind) {
  32248. if (result.kind === exports.ReferenceEmitKind.Success) {
  32249. return;
  32250. }
  32251. const message = makeDiagnosticChain(`Unable to import ${typeKind} ${nodeNameForError(result.ref.node)}.`, [makeDiagnosticChain(result.reason)]);
  32252. throw new FatalDiagnosticError(exports.ErrorCode.IMPORT_GENERATION_FAILURE, origin, message, [
  32253. makeRelatedInformation(result.ref.node, `The ${typeKind} is declared here.`),
  32254. ]);
  32255. }
  32256. /**
  32257. * Generates `Expression`s which refer to `Reference`s in a given context.
  32258. *
  32259. * A `ReferenceEmitter` uses one or more `ReferenceEmitStrategy` implementations to produce an
  32260. * `Expression` which refers to a `Reference` in the context of a particular file.
  32261. */
  32262. class ReferenceEmitter {
  32263. strategies;
  32264. constructor(strategies) {
  32265. this.strategies = strategies;
  32266. }
  32267. emit(ref, context, importFlags = exports.ImportFlags.None) {
  32268. for (const strategy of this.strategies) {
  32269. const emitted = strategy.emit(ref, context, importFlags);
  32270. if (emitted !== null) {
  32271. return emitted;
  32272. }
  32273. }
  32274. return {
  32275. kind: exports.ReferenceEmitKind.Failed,
  32276. ref,
  32277. context,
  32278. reason: `Unable to write a reference to ${nodeNameForError(ref.node)}.`,
  32279. };
  32280. }
  32281. }
  32282. /**
  32283. * A `ReferenceEmitStrategy` which will refer to declarations by any local `ts.Identifier`s, if
  32284. * such identifiers are available.
  32285. */
  32286. class LocalIdentifierStrategy {
  32287. emit(ref, context, importFlags) {
  32288. const refSf = getSourceFile(ref.node);
  32289. // If the emitter has specified ForceNewImport, then LocalIdentifierStrategy should not use a
  32290. // local identifier at all, *except* in the source file where the node is actually declared.
  32291. if (importFlags & exports.ImportFlags.ForceNewImport && refSf !== context) {
  32292. return null;
  32293. }
  32294. // If referenced node is not an actual TS declaration (e.g. `class Foo` or `function foo() {}`,
  32295. // etc) and it is in the current file then just use it directly.
  32296. // This is important because the reference could be a property access (e.g. `exports.foo`). In
  32297. // such a case, the reference's `identities` property would be `[foo]`, which would result in an
  32298. // invalid emission of a free-standing `foo` identifier, rather than `exports.foo`.
  32299. if (!isDeclaration(ref.node) && refSf === context) {
  32300. return {
  32301. kind: exports.ReferenceEmitKind.Success,
  32302. expression: new WrappedNodeExpr(ref.node),
  32303. importedFile: null,
  32304. };
  32305. }
  32306. // If the reference is to an ambient type, it can be referenced directly.
  32307. if (ref.isAmbient && importFlags & exports.ImportFlags.AllowAmbientReferences) {
  32308. const identifier = identifierOfNode(ref.node);
  32309. if (identifier !== null) {
  32310. return {
  32311. kind: exports.ReferenceEmitKind.Success,
  32312. expression: new WrappedNodeExpr(identifier),
  32313. importedFile: null,
  32314. };
  32315. }
  32316. else {
  32317. return null;
  32318. }
  32319. }
  32320. // A Reference can have multiple identities in different files, so it may already have an
  32321. // Identifier in the requested context file.
  32322. const identifier = ref.getIdentityIn(context);
  32323. if (identifier !== null) {
  32324. return {
  32325. kind: exports.ReferenceEmitKind.Success,
  32326. expression: new WrappedNodeExpr(identifier),
  32327. importedFile: null,
  32328. };
  32329. }
  32330. else {
  32331. return null;
  32332. }
  32333. }
  32334. }
  32335. /**
  32336. * A `ReferenceEmitStrategy` which will refer to declarations that come from `node_modules` using
  32337. * an absolute import.
  32338. *
  32339. * Part of this strategy involves looking at the target entry point and identifying the exported
  32340. * name of the targeted declaration, as it might be different from the declared name (e.g. a
  32341. * directive might be declared as FooDirImpl, but exported as FooDir). If no export can be found
  32342. * which maps back to the original directive, an error is thrown.
  32343. */
  32344. class AbsoluteModuleStrategy {
  32345. program;
  32346. checker;
  32347. moduleResolver;
  32348. reflectionHost;
  32349. /**
  32350. * A cache of the exports of specific modules, because resolving a module to its exports is a
  32351. * costly operation.
  32352. */
  32353. moduleExportsCache = new Map();
  32354. constructor(program, checker, moduleResolver, reflectionHost) {
  32355. this.program = program;
  32356. this.checker = checker;
  32357. this.moduleResolver = moduleResolver;
  32358. this.reflectionHost = reflectionHost;
  32359. }
  32360. emit(ref, context, importFlags) {
  32361. if (ref.bestGuessOwningModule === null) {
  32362. // There is no module name available for this Reference, meaning it was arrived at via a
  32363. // relative path.
  32364. return null;
  32365. }
  32366. else if (!isDeclaration(ref.node)) {
  32367. // It's not possible to import something which isn't a declaration.
  32368. throw new Error(`Debug assert: unable to import a Reference to non-declaration of type ${ts.SyntaxKind[ref.node.kind]}.`);
  32369. }
  32370. else if ((importFlags & exports.ImportFlags.AllowTypeImports) === 0 && isTypeDeclaration(ref.node)) {
  32371. throw new Error(`Importing a type-only declaration of type ${ts.SyntaxKind[ref.node.kind]} in a value position is not allowed.`);
  32372. }
  32373. // Try to find the exported name of the declaration, if one is available.
  32374. const { specifier, resolutionContext } = ref.bestGuessOwningModule;
  32375. const exports$1 = this.getExportsOfModule(specifier, resolutionContext);
  32376. if (exports$1.module === null) {
  32377. return {
  32378. kind: exports.ReferenceEmitKind.Failed,
  32379. ref,
  32380. context,
  32381. reason: `The module '${specifier}' could not be found.`,
  32382. };
  32383. }
  32384. else if (exports$1.exportMap === null || !exports$1.exportMap.has(ref.node)) {
  32385. return {
  32386. kind: exports.ReferenceEmitKind.Failed,
  32387. ref,
  32388. context,
  32389. reason: `The symbol is not exported from ${exports$1.module.fileName} (module '${specifier}').`,
  32390. };
  32391. }
  32392. const symbolName = exports$1.exportMap.get(ref.node);
  32393. return {
  32394. kind: exports.ReferenceEmitKind.Success,
  32395. expression: new ExternalExpr(new ExternalReference(specifier, symbolName)),
  32396. importedFile: exports$1.module,
  32397. };
  32398. }
  32399. getExportsOfModule(moduleName, fromFile) {
  32400. if (!this.moduleExportsCache.has(moduleName)) {
  32401. this.moduleExportsCache.set(moduleName, this.enumerateExportsOfModule(moduleName, fromFile));
  32402. }
  32403. return this.moduleExportsCache.get(moduleName);
  32404. }
  32405. enumerateExportsOfModule(specifier, fromFile) {
  32406. // First, resolve the module specifier to its entry point, and get the ts.Symbol for it.
  32407. const entryPointFile = this.moduleResolver.resolveModule(specifier, fromFile);
  32408. if (entryPointFile === null) {
  32409. return { module: null, exportMap: null };
  32410. }
  32411. const exports = this.reflectionHost.getExportsOfModule(entryPointFile);
  32412. if (exports === null) {
  32413. return { module: entryPointFile, exportMap: null };
  32414. }
  32415. const exportMap = new Map();
  32416. for (const [name, declaration] of exports) {
  32417. if (exportMap.has(declaration.node)) {
  32418. // An export for this declaration has already been registered. We prefer an export that
  32419. // has the same name as the declared name, i.e. is not an aliased export. This is relevant
  32420. // for partial compilations where emitted references should import symbols using a stable
  32421. // name. This is particularly relevant for declarations inside VE-generated libraries, as
  32422. // such libraries contain private, unstable reexports of symbols.
  32423. const existingExport = exportMap.get(declaration.node);
  32424. if (isNamedDeclaration(declaration.node) && declaration.node.name.text === existingExport) {
  32425. continue;
  32426. }
  32427. }
  32428. exportMap.set(declaration.node, name);
  32429. }
  32430. return { module: entryPointFile, exportMap };
  32431. }
  32432. }
  32433. /**
  32434. * A `ReferenceEmitStrategy` which will refer to declarations via relative paths, provided they're
  32435. * both in the logical project "space" of paths.
  32436. *
  32437. * This is trickier than it sounds, as the two files may be in different root directories in the
  32438. * project. Simply calculating a file system relative path between the two is not sufficient.
  32439. * Instead, `LogicalProjectPath`s are used.
  32440. */
  32441. class LogicalProjectStrategy {
  32442. reflector;
  32443. logicalFs;
  32444. relativePathStrategy;
  32445. constructor(reflector, logicalFs) {
  32446. this.reflector = reflector;
  32447. this.logicalFs = logicalFs;
  32448. this.relativePathStrategy = new RelativePathStrategy(this.reflector);
  32449. }
  32450. emit(ref, context, importFlags) {
  32451. const destSf = getSourceFile(ref.node);
  32452. // Compute the relative path from the importing file to the file being imported. This is done
  32453. // as a logical path computation, because the two files might be in different rootDirs.
  32454. const destPath = this.logicalFs.logicalPathOfSf(destSf);
  32455. if (destPath === null) {
  32456. // The imported file is not within the logical project filesystem. An import into a
  32457. // declaration file is exempt from `TS6059: File is not under 'rootDir'` so we choose to allow
  32458. // using a filesystem relative path as fallback, if allowed per the provided import flags.
  32459. if (destSf.isDeclarationFile && importFlags & exports.ImportFlags.AllowRelativeDtsImports) {
  32460. return this.relativePathStrategy.emit(ref, context);
  32461. }
  32462. // Note: this error is analogous to `TS6059: File is not under 'rootDir'` that TypeScript
  32463. // reports.
  32464. return {
  32465. kind: exports.ReferenceEmitKind.Failed,
  32466. ref,
  32467. context,
  32468. reason: `The file ${destSf.fileName} is outside of the configured 'rootDir'.`,
  32469. };
  32470. }
  32471. const originPath = this.logicalFs.logicalPathOfSf(context);
  32472. if (originPath === null) {
  32473. throw new Error(`Debug assert: attempt to import from ${context.fileName} but it's outside the program?`);
  32474. }
  32475. // There's no way to emit a relative reference from a file to itself.
  32476. if (destPath === originPath) {
  32477. return null;
  32478. }
  32479. const name = findExportedNameOfNode(ref.node, destSf, this.reflector);
  32480. if (name === null) {
  32481. // The target declaration isn't exported from the file it's declared in. This is an issue!
  32482. return {
  32483. kind: exports.ReferenceEmitKind.Failed,
  32484. ref,
  32485. context,
  32486. reason: `The symbol is not exported from ${destSf.fileName}.`,
  32487. };
  32488. }
  32489. // With both files expressed as LogicalProjectPaths, getting the module specifier as a relative
  32490. // path is now straightforward.
  32491. const moduleName = LogicalProjectPath.relativePathBetween(originPath, destPath);
  32492. return {
  32493. kind: exports.ReferenceEmitKind.Success,
  32494. expression: new ExternalExpr({ moduleName, name }),
  32495. importedFile: destSf,
  32496. };
  32497. }
  32498. }
  32499. /**
  32500. * A `ReferenceEmitStrategy` which constructs relatives paths between `ts.SourceFile`s.
  32501. *
  32502. * This strategy can be used if there is no `rootDir`/`rootDirs` structure for the project which
  32503. * necessitates the stronger logic of `LogicalProjectStrategy`.
  32504. */
  32505. class RelativePathStrategy {
  32506. reflector;
  32507. constructor(reflector) {
  32508. this.reflector = reflector;
  32509. }
  32510. emit(ref, context) {
  32511. const destSf = getSourceFile(ref.node);
  32512. const relativePath = relative(dirname(absoluteFromSourceFile(context)), absoluteFromSourceFile(destSf));
  32513. const moduleName = toRelativeImport(stripExtension(relativePath));
  32514. const name = findExportedNameOfNode(ref.node, destSf, this.reflector);
  32515. if (name === null) {
  32516. return {
  32517. kind: exports.ReferenceEmitKind.Failed,
  32518. ref,
  32519. context,
  32520. reason: `The symbol is not exported from ${destSf.fileName}.`,
  32521. };
  32522. }
  32523. return {
  32524. kind: exports.ReferenceEmitKind.Success,
  32525. expression: new ExternalExpr({ moduleName, name }),
  32526. importedFile: destSf,
  32527. };
  32528. }
  32529. }
  32530. /**
  32531. * A `ReferenceEmitStrategy` which uses a `UnifiedModulesHost` to generate absolute import
  32532. * references.
  32533. */
  32534. class UnifiedModulesStrategy {
  32535. reflector;
  32536. unifiedModulesHost;
  32537. constructor(reflector, unifiedModulesHost) {
  32538. this.reflector = reflector;
  32539. this.unifiedModulesHost = unifiedModulesHost;
  32540. }
  32541. emit(ref, context) {
  32542. const destSf = getSourceFile(ref.node);
  32543. const name = findExportedNameOfNode(ref.node, destSf, this.reflector);
  32544. if (name === null) {
  32545. return null;
  32546. }
  32547. const moduleName = this.unifiedModulesHost.fileNameToModuleName(destSf.fileName, context.fileName);
  32548. return {
  32549. kind: exports.ReferenceEmitKind.Success,
  32550. expression: new ExternalExpr({ moduleName, name }),
  32551. importedFile: destSf,
  32552. };
  32553. }
  32554. }
  32555. const patchedReferencedAliasesSymbol = Symbol('patchedReferencedAliases');
  32556. /**
  32557. * Patches the alias declaration reference resolution for a given transformation context
  32558. * so that TypeScript knows about the specified alias declarations being referenced.
  32559. *
  32560. * This exists because TypeScript performs analysis of import usage before transformers
  32561. * run and doesn't refresh its state after transformations. This means that imports
  32562. * for symbols used as constructor types are elided due to their original type-only usage.
  32563. *
  32564. * In reality though, since we downlevel decorators and constructor parameters, we want
  32565. * these symbols to be retained in the JavaScript output as they will be used as values
  32566. * at runtime. We can instruct TypeScript to preserve imports for such identifiers by
  32567. * creating a mutable clone of a given import specifier/clause or namespace, but that
  32568. * has the downside of preserving the full import in the JS output. See:
  32569. * https://github.com/microsoft/TypeScript/blob/3eaa7c65f6f076a08a5f7f1946fd0df7c7430259/src/compiler/transformers/ts.ts#L242-L250.
  32570. *
  32571. * This is a trick the CLI used in the past for constructor parameter downleveling in JIT:
  32572. * https://github.com/angular/angular-cli/blob/b3f84cc5184337666ce61c07b7b9df418030106f/packages/ngtools/webpack/src/transformers/ctor-parameters.ts#L323-L325
  32573. * The trick is not ideal though as it preserves the full import (as outlined before), and it
  32574. * results in a slow-down due to the type checker being involved multiple times. The CLI worked
  32575. * around this import preserving issue by having another complex post-process step that detects and
  32576. * elides unused imports. Note that these unused imports could cause unused chunks being generated
  32577. * by webpack if the application or library is not marked as side-effect free.
  32578. *
  32579. * This is not ideal though, as we basically re-implement the complex import usage resolution
  32580. * from TypeScript. We can do better by letting TypeScript do the import eliding, but providing
  32581. * information about the alias declarations (e.g. import specifiers) that should not be elided
  32582. * because they are actually referenced (as they will now appear in static properties).
  32583. *
  32584. * More information about these limitations with transformers can be found in:
  32585. * 1. https://github.com/Microsoft/TypeScript/issues/17552.
  32586. * 2. https://github.com/microsoft/TypeScript/issues/17516.
  32587. * 3. https://github.com/angular/tsickle/issues/635.
  32588. *
  32589. * The patch we apply to tell TypeScript about actual referenced aliases (i.e. imported symbols),
  32590. * matches conceptually with the logic that runs internally in TypeScript when the
  32591. * `emitDecoratorMetadata` flag is enabled. TypeScript basically surfaces the same problem and
  32592. * solves it conceptually the same way, but obviously doesn't need to access an internal API.
  32593. *
  32594. * The set that is returned by this function is meant to be filled with import declaration nodes
  32595. * that have been referenced in a value-position by the transform, such the installed patch can
  32596. * ensure that those import declarations are not elided.
  32597. *
  32598. * If `null` is returned then the transform operates in an isolated context, i.e. using the
  32599. * `ts.transform` API. In such scenario there is no information whether an alias declaration
  32600. * is referenced, so all alias declarations are naturally preserved and explicitly registering
  32601. * an alias declaration as used isn't necessary.
  32602. *
  32603. * See below. Note that this uses sourcegraph as the TypeScript checker file doesn't display on
  32604. * Github.
  32605. * https://sourcegraph.com/github.com/microsoft/TypeScript@3eaa7c65f6f076a08a5f7f1946fd0df7c7430259/-/blob/src/compiler/checker.ts#L31219-31257
  32606. */
  32607. function loadIsReferencedAliasDeclarationPatch(context) {
  32608. // If the `getEmitResolver` method is not available, TS most likely changed the
  32609. // internal structure of the transformation context. We will abort gracefully.
  32610. if (!isTransformationContextWithEmitResolver(context)) {
  32611. throwIncompatibleTransformationContextError();
  32612. }
  32613. const emitResolver = context.getEmitResolver();
  32614. if (emitResolver === undefined) {
  32615. // In isolated `ts.transform` operations no emit resolver is present, return null as `isReferencedAliasDeclaration`
  32616. // will never be invoked.
  32617. return null;
  32618. }
  32619. // The emit resolver may have been patched already, in which case we return the set of referenced
  32620. // aliases that was created when the patch was first applied.
  32621. // See https://github.com/angular/angular/issues/40276.
  32622. const existingReferencedAliases = emitResolver[patchedReferencedAliasesSymbol];
  32623. if (existingReferencedAliases !== undefined) {
  32624. return existingReferencedAliases;
  32625. }
  32626. const originalIsReferencedAliasDeclaration = emitResolver.isReferencedAliasDeclaration;
  32627. // If the emit resolver does not have a function called `isReferencedAliasDeclaration`, then
  32628. // we abort gracefully as most likely TS changed the internal structure of the emit resolver.
  32629. if (originalIsReferencedAliasDeclaration === undefined) {
  32630. throwIncompatibleTransformationContextError();
  32631. }
  32632. const referencedAliases = new Set();
  32633. emitResolver.isReferencedAliasDeclaration = function (node, ...args) {
  32634. if (isAliasImportDeclaration(node) && referencedAliases.has(node)) {
  32635. return true;
  32636. }
  32637. return originalIsReferencedAliasDeclaration.call(emitResolver, node, ...args);
  32638. };
  32639. return (emitResolver[patchedReferencedAliasesSymbol] = referencedAliases);
  32640. }
  32641. /**
  32642. * Gets whether a given node corresponds to an import alias declaration. Alias
  32643. * declarations can be import specifiers, namespace imports or import clauses
  32644. * as these do not declare an actual symbol but just point to a target declaration.
  32645. */
  32646. function isAliasImportDeclaration(node) {
  32647. return ts.isImportSpecifier(node) || ts.isNamespaceImport(node) || ts.isImportClause(node);
  32648. }
  32649. /** Whether the transformation context exposes its emit resolver. */
  32650. function isTransformationContextWithEmitResolver(context) {
  32651. return context.getEmitResolver !== undefined;
  32652. }
  32653. /**
  32654. * Throws an error about an incompatible TypeScript version for which the alias
  32655. * declaration reference resolution could not be monkey-patched. The error will
  32656. * also propose potential solutions that can be applied by developers.
  32657. */
  32658. function throwIncompatibleTransformationContextError() {
  32659. throw Error('Angular compiler is incompatible with this version of the TypeScript compiler.\n\n' +
  32660. 'If you recently updated TypeScript and this issue surfaces now, consider downgrading.\n\n' +
  32661. 'Please report an issue on the Angular repositories when this issue ' +
  32662. 'surfaces and you are using a supposedly compatible TypeScript version.');
  32663. }
  32664. const DefaultImportDeclaration = Symbol('DefaultImportDeclaration');
  32665. /**
  32666. * Attaches a default import declaration to `expr` to indicate the dependency of `expr` on the
  32667. * default import.
  32668. */
  32669. function attachDefaultImportDeclaration(expr, importDecl) {
  32670. expr[DefaultImportDeclaration] = importDecl;
  32671. }
  32672. /**
  32673. * Obtains the default import declaration that `expr` depends on, or `null` if there is no such
  32674. * dependency.
  32675. */
  32676. function getDefaultImportDeclaration(expr) {
  32677. return expr[DefaultImportDeclaration] ?? null;
  32678. }
  32679. /**
  32680. * TypeScript has trouble with generating default imports inside of transformers for some module
  32681. * formats. The issue is that for the statement:
  32682. *
  32683. * import X from 'some/module';
  32684. * console.log(X);
  32685. *
  32686. * TypeScript will not use the "X" name in generated code. For normal user code, this is fine
  32687. * because references to X will also be renamed. However, if both the import and any references are
  32688. * added in a transformer, TypeScript does not associate the two, and will leave the "X" references
  32689. * dangling while renaming the import variable. The generated code looks something like:
  32690. *
  32691. * const module_1 = require('some/module');
  32692. * console.log(X); // now X is a dangling reference.
  32693. *
  32694. * Therefore, we cannot synthetically add default imports, and must reuse the imports that users
  32695. * include. Doing this poses a challenge for imports that are only consumed in the type position in
  32696. * the user's code. If Angular reuses the imported symbol in a value position (for example, we
  32697. * see a constructor parameter of type Foo and try to write "inject(Foo)") we will also end up with
  32698. * a dangling reference, as TS will elide the import because it was only used in the type position
  32699. * originally.
  32700. *
  32701. * To avoid this, the compiler must patch the emit resolver, and should only do this for imports
  32702. * which are actually consumed. The `DefaultImportTracker` keeps track of these imports as they're
  32703. * encountered and emitted, and implements a transform which can correctly flag the imports as
  32704. * required.
  32705. *
  32706. * This problem does not exist for non-default imports as the compiler can easily insert
  32707. * "import * as X" style imports for those, and the "X" identifier survives transformation.
  32708. */
  32709. class DefaultImportTracker {
  32710. /**
  32711. * A `Map` which tracks the `Set` of `ts.ImportClause`s for default imports that were used in
  32712. * a given file name.
  32713. */
  32714. sourceFileToUsedImports = new Map();
  32715. recordUsedImport(importDecl) {
  32716. if (importDecl.importClause) {
  32717. const sf = getSourceFile(importDecl);
  32718. // Add the default import declaration to the set of used import declarations for the file.
  32719. if (!this.sourceFileToUsedImports.has(sf.fileName)) {
  32720. this.sourceFileToUsedImports.set(sf.fileName, new Set());
  32721. }
  32722. this.sourceFileToUsedImports.get(sf.fileName).add(importDecl.importClause);
  32723. }
  32724. }
  32725. /**
  32726. * Get a `ts.TransformerFactory` which will preserve default imports that were previously marked
  32727. * as used.
  32728. *
  32729. * This transformer must run after any other transformers which call `recordUsedImport`.
  32730. */
  32731. importPreservingTransformer() {
  32732. return (context) => {
  32733. let clausesToPreserve = null;
  32734. return (sourceFile) => {
  32735. const clausesForFile = this.sourceFileToUsedImports.get(sourceFile.fileName);
  32736. if (clausesForFile !== undefined) {
  32737. for (const clause of clausesForFile) {
  32738. // Initialize the patch lazily so that apps that
  32739. // don't use default imports aren't patched.
  32740. if (clausesToPreserve === null) {
  32741. clausesToPreserve = loadIsReferencedAliasDeclarationPatch(context);
  32742. }
  32743. clausesToPreserve?.add(clause);
  32744. }
  32745. }
  32746. return sourceFile;
  32747. };
  32748. };
  32749. }
  32750. }
  32751. function isDecoratorIdentifier(exp) {
  32752. return (ts.isIdentifier(exp) ||
  32753. (ts.isPropertyAccessExpression(exp) &&
  32754. ts.isIdentifier(exp.expression) &&
  32755. ts.isIdentifier(exp.name)));
  32756. }
  32757. /**
  32758. * An enumeration of possible kinds of class members.
  32759. */
  32760. exports.ClassMemberKind = void 0;
  32761. (function (ClassMemberKind) {
  32762. ClassMemberKind[ClassMemberKind["Constructor"] = 0] = "Constructor";
  32763. ClassMemberKind[ClassMemberKind["Getter"] = 1] = "Getter";
  32764. ClassMemberKind[ClassMemberKind["Setter"] = 2] = "Setter";
  32765. ClassMemberKind[ClassMemberKind["Property"] = 3] = "Property";
  32766. ClassMemberKind[ClassMemberKind["Method"] = 4] = "Method";
  32767. })(exports.ClassMemberKind || (exports.ClassMemberKind = {}));
  32768. /** Possible access levels of a class member. */
  32769. exports.ClassMemberAccessLevel = void 0;
  32770. (function (ClassMemberAccessLevel) {
  32771. ClassMemberAccessLevel[ClassMemberAccessLevel["PublicWritable"] = 0] = "PublicWritable";
  32772. ClassMemberAccessLevel[ClassMemberAccessLevel["PublicReadonly"] = 1] = "PublicReadonly";
  32773. ClassMemberAccessLevel[ClassMemberAccessLevel["Protected"] = 2] = "Protected";
  32774. ClassMemberAccessLevel[ClassMemberAccessLevel["Private"] = 3] = "Private";
  32775. ClassMemberAccessLevel[ClassMemberAccessLevel["EcmaScriptPrivate"] = 4] = "EcmaScriptPrivate";
  32776. })(exports.ClassMemberAccessLevel || (exports.ClassMemberAccessLevel = {}));
  32777. /** Indicates that a declaration is referenced through an ambient type. */
  32778. const AmbientImport = {};
  32779. /**
  32780. * Potentially convert a `ts.TypeNode` to a `TypeValueReference`, which indicates how to use the
  32781. * type given in the `ts.TypeNode` in a value position.
  32782. *
  32783. * This can return `null` if the `typeNode` is `null`, if it does not refer to a symbol with a value
  32784. * declaration, or if it is not possible to statically understand.
  32785. */
  32786. function typeToValue(typeNode, checker, isLocalCompilation) {
  32787. // It's not possible to get a value expression if the parameter doesn't even have a type.
  32788. if (typeNode === null) {
  32789. return missingType();
  32790. }
  32791. if (!ts.isTypeReferenceNode(typeNode)) {
  32792. return unsupportedType(typeNode);
  32793. }
  32794. const symbols = resolveTypeSymbols(typeNode, checker);
  32795. if (symbols === null) {
  32796. return unknownReference(typeNode);
  32797. }
  32798. const { local, decl } = symbols;
  32799. // It's only valid to convert a type reference to a value reference if the type actually
  32800. // has a value declaration associated with it. Note that const enums are an exception,
  32801. // because while they do have a value declaration, they don't exist at runtime.
  32802. if (decl.valueDeclaration === undefined || decl.flags & ts.SymbolFlags.ConstEnum) {
  32803. let typeOnlyDecl = null;
  32804. if (decl.declarations !== undefined && decl.declarations.length > 0) {
  32805. typeOnlyDecl = decl.declarations[0];
  32806. }
  32807. // In local compilation mode a declaration is considered invalid only if it is a type related
  32808. // declaration.
  32809. if (!isLocalCompilation ||
  32810. (typeOnlyDecl &&
  32811. [
  32812. ts.SyntaxKind.TypeParameter,
  32813. ts.SyntaxKind.TypeAliasDeclaration,
  32814. ts.SyntaxKind.InterfaceDeclaration,
  32815. ].includes(typeOnlyDecl.kind))) {
  32816. return noValueDeclaration(typeNode, typeOnlyDecl);
  32817. }
  32818. }
  32819. // The type points to a valid value declaration. Rewrite the TypeReference into an
  32820. // Expression which references the value pointed to by the TypeReference, if possible.
  32821. // Look at the local `ts.Symbol`'s declarations and see if it comes from an import
  32822. // statement. If so, extract the module specifier and the name of the imported type.
  32823. const firstDecl = local.declarations && local.declarations[0];
  32824. if (firstDecl !== undefined) {
  32825. if (ts.isImportClause(firstDecl) && firstDecl.name !== undefined) {
  32826. // This is a default import.
  32827. // import Foo from 'foo';
  32828. if (firstDecl.isTypeOnly) {
  32829. // Type-only imports cannot be represented as value.
  32830. return typeOnlyImport(typeNode, firstDecl);
  32831. }
  32832. if (!ts.isImportDeclaration(firstDecl.parent)) {
  32833. return unsupportedType(typeNode);
  32834. }
  32835. return {
  32836. kind: 0 /* TypeValueReferenceKind.LOCAL */,
  32837. expression: firstDecl.name,
  32838. defaultImportStatement: firstDecl.parent,
  32839. };
  32840. }
  32841. else if (ts.isImportSpecifier(firstDecl)) {
  32842. // The symbol was imported by name
  32843. // import {Foo} from 'foo';
  32844. // or
  32845. // import {Foo as Bar} from 'foo';
  32846. if (firstDecl.isTypeOnly) {
  32847. // The import specifier can't be type-only (e.g. `import {type Foo} from '...')`.
  32848. return typeOnlyImport(typeNode, firstDecl);
  32849. }
  32850. if (firstDecl.parent.parent.isTypeOnly) {
  32851. // The import specifier can't be inside a type-only import clause
  32852. // (e.g. `import type {Foo} from '...')`.
  32853. return typeOnlyImport(typeNode, firstDecl.parent.parent);
  32854. }
  32855. // Determine the name to import (`Foo`) from the import specifier, as the symbol names of
  32856. // the imported type could refer to a local alias (like `Bar` in the example above).
  32857. const importedName = (firstDecl.propertyName || firstDecl.name).text;
  32858. // The first symbol name refers to the local name, which is replaced by `importedName` above.
  32859. // Any remaining symbol names make up the complete path to the value.
  32860. const [_localName, ...nestedPath] = symbols.symbolNames;
  32861. const importDeclaration = firstDecl.parent.parent.parent;
  32862. if (!ts.isImportDeclaration(importDeclaration)) {
  32863. return unsupportedType(typeNode);
  32864. }
  32865. const moduleName = extractModuleName(importDeclaration);
  32866. return {
  32867. kind: 1 /* TypeValueReferenceKind.IMPORTED */,
  32868. valueDeclaration: decl.valueDeclaration ?? null,
  32869. moduleName,
  32870. importedName,
  32871. nestedPath,
  32872. };
  32873. }
  32874. else if (ts.isNamespaceImport(firstDecl)) {
  32875. // The import is a namespace import
  32876. // import * as Foo from 'foo';
  32877. if (firstDecl.parent.isTypeOnly) {
  32878. // Type-only imports cannot be represented as value.
  32879. return typeOnlyImport(typeNode, firstDecl.parent);
  32880. }
  32881. if (symbols.symbolNames.length === 1) {
  32882. // The type refers to the namespace itself, which cannot be represented as a value.
  32883. return namespaceImport(typeNode, firstDecl.parent);
  32884. }
  32885. // The first symbol name refers to the local name of the namespace, which is is discarded
  32886. // as a new namespace import will be generated. This is followed by the symbol name that needs
  32887. // to be imported and any remaining names that constitute the complete path to the value.
  32888. const [_ns, importedName, ...nestedPath] = symbols.symbolNames;
  32889. const importDeclaration = firstDecl.parent.parent;
  32890. if (!ts.isImportDeclaration(importDeclaration)) {
  32891. return unsupportedType(typeNode);
  32892. }
  32893. const moduleName = extractModuleName(importDeclaration);
  32894. return {
  32895. kind: 1 /* TypeValueReferenceKind.IMPORTED */,
  32896. valueDeclaration: decl.valueDeclaration ?? null,
  32897. moduleName,
  32898. importedName,
  32899. nestedPath,
  32900. };
  32901. }
  32902. }
  32903. // If the type is not imported, the type reference can be converted into an expression as is.
  32904. const expression = typeNodeToValueExpr(typeNode);
  32905. if (expression !== null) {
  32906. return {
  32907. kind: 0 /* TypeValueReferenceKind.LOCAL */,
  32908. expression,
  32909. defaultImportStatement: null,
  32910. };
  32911. }
  32912. else {
  32913. return unsupportedType(typeNode);
  32914. }
  32915. }
  32916. function unsupportedType(typeNode) {
  32917. return {
  32918. kind: 2 /* TypeValueReferenceKind.UNAVAILABLE */,
  32919. reason: { kind: 5 /* ValueUnavailableKind.UNSUPPORTED */, typeNode },
  32920. };
  32921. }
  32922. function noValueDeclaration(typeNode, decl) {
  32923. return {
  32924. kind: 2 /* TypeValueReferenceKind.UNAVAILABLE */,
  32925. reason: { kind: 1 /* ValueUnavailableKind.NO_VALUE_DECLARATION */, typeNode, decl },
  32926. };
  32927. }
  32928. function typeOnlyImport(typeNode, node) {
  32929. return {
  32930. kind: 2 /* TypeValueReferenceKind.UNAVAILABLE */,
  32931. reason: { kind: 2 /* ValueUnavailableKind.TYPE_ONLY_IMPORT */, typeNode, node },
  32932. };
  32933. }
  32934. function unknownReference(typeNode) {
  32935. return {
  32936. kind: 2 /* TypeValueReferenceKind.UNAVAILABLE */,
  32937. reason: { kind: 3 /* ValueUnavailableKind.UNKNOWN_REFERENCE */, typeNode },
  32938. };
  32939. }
  32940. function namespaceImport(typeNode, importClause) {
  32941. return {
  32942. kind: 2 /* TypeValueReferenceKind.UNAVAILABLE */,
  32943. reason: { kind: 4 /* ValueUnavailableKind.NAMESPACE */, typeNode, importClause },
  32944. };
  32945. }
  32946. function missingType() {
  32947. return {
  32948. kind: 2 /* TypeValueReferenceKind.UNAVAILABLE */,
  32949. reason: { kind: 0 /* ValueUnavailableKind.MISSING_TYPE */ },
  32950. };
  32951. }
  32952. /**
  32953. * Attempt to extract a `ts.Expression` that's equivalent to a `ts.TypeNode`, as the two have
  32954. * different AST shapes but can reference the same symbols.
  32955. *
  32956. * This will return `null` if an equivalent expression cannot be constructed.
  32957. */
  32958. function typeNodeToValueExpr(node) {
  32959. if (ts.isTypeReferenceNode(node)) {
  32960. return entityNameToValue(node.typeName);
  32961. }
  32962. else {
  32963. return null;
  32964. }
  32965. }
  32966. /**
  32967. * Resolve a `TypeReference` node to the `ts.Symbol`s for both its declaration and its local source.
  32968. *
  32969. * In the event that the `TypeReference` refers to a locally declared symbol, these will be the
  32970. * same. If the `TypeReference` refers to an imported symbol, then `decl` will be the fully resolved
  32971. * `ts.Symbol` of the referenced symbol. `local` will be the `ts.Symbol` of the `ts.Identifier`
  32972. * which points to the import statement by which the symbol was imported.
  32973. *
  32974. * All symbol names that make up the type reference are returned left-to-right into the
  32975. * `symbolNames` array, which is guaranteed to include at least one entry.
  32976. */
  32977. function resolveTypeSymbols(typeRef, checker) {
  32978. const typeName = typeRef.typeName;
  32979. // typeRefSymbol is the ts.Symbol of the entire type reference.
  32980. const typeRefSymbol = checker.getSymbolAtLocation(typeName);
  32981. if (typeRefSymbol === undefined) {
  32982. return null;
  32983. }
  32984. // `local` is the `ts.Symbol` for the local `ts.Identifier` for the type.
  32985. // If the type is actually locally declared or is imported by name, for example:
  32986. // import {Foo} from './foo';
  32987. // then it'll be the same as `typeRefSymbol`.
  32988. //
  32989. // If the type is imported via a namespace import, for example:
  32990. // import * as foo from './foo';
  32991. // and then referenced as:
  32992. // constructor(f: foo.Foo)
  32993. // then `local` will be the `ts.Symbol` of `foo`, whereas `typeRefSymbol` will be the `ts.Symbol`
  32994. // of `foo.Foo`. This allows tracking of the import behind whatever type reference exists.
  32995. let local = typeRefSymbol;
  32996. // Destructure a name like `foo.X.Y.Z` as follows:
  32997. // - in `leftMost`, the `ts.Identifier` of the left-most name (`foo`) in the qualified name.
  32998. // This identifier is used to resolve the `ts.Symbol` for `local`.
  32999. // - in `symbolNames`, all names involved in the qualified path, or a single symbol name if the
  33000. // type is not qualified.
  33001. let leftMost = typeName;
  33002. const symbolNames = [];
  33003. while (ts.isQualifiedName(leftMost)) {
  33004. symbolNames.unshift(leftMost.right.text);
  33005. leftMost = leftMost.left;
  33006. }
  33007. symbolNames.unshift(leftMost.text);
  33008. if (leftMost !== typeName) {
  33009. const localTmp = checker.getSymbolAtLocation(leftMost);
  33010. if (localTmp !== undefined) {
  33011. local = localTmp;
  33012. }
  33013. }
  33014. // De-alias the top-level type reference symbol to get the symbol of the actual declaration.
  33015. let decl = typeRefSymbol;
  33016. if (typeRefSymbol.flags & ts.SymbolFlags.Alias) {
  33017. decl = checker.getAliasedSymbol(typeRefSymbol);
  33018. }
  33019. return { local, decl, symbolNames };
  33020. }
  33021. function entityNameToValue(node) {
  33022. if (ts.isQualifiedName(node)) {
  33023. const left = entityNameToValue(node.left);
  33024. return left !== null ? ts.factory.createPropertyAccessExpression(left, node.right) : null;
  33025. }
  33026. else if (ts.isIdentifier(node)) {
  33027. const clone = ts.setOriginalNode(ts.factory.createIdentifier(node.text), node);
  33028. clone.parent = node.parent;
  33029. return clone;
  33030. }
  33031. else {
  33032. return null;
  33033. }
  33034. }
  33035. function extractModuleName(node) {
  33036. if (!ts.isStringLiteral(node.moduleSpecifier)) {
  33037. throw new Error('not a module specifier');
  33038. }
  33039. return node.moduleSpecifier.text;
  33040. }
  33041. function isNamedClassDeclaration(node) {
  33042. return ts.isClassDeclaration(node) && isIdentifier(node.name);
  33043. }
  33044. function isIdentifier(node) {
  33045. return node !== undefined && ts.isIdentifier(node);
  33046. }
  33047. /**
  33048. * Converts the given class member access level to a string.
  33049. * Useful fo error messages.
  33050. */
  33051. function classMemberAccessLevelToString(level) {
  33052. switch (level) {
  33053. case exports.ClassMemberAccessLevel.EcmaScriptPrivate:
  33054. return 'ES private';
  33055. case exports.ClassMemberAccessLevel.Private:
  33056. return 'private';
  33057. case exports.ClassMemberAccessLevel.Protected:
  33058. return 'protected';
  33059. case exports.ClassMemberAccessLevel.PublicReadonly:
  33060. return 'public readonly';
  33061. case exports.ClassMemberAccessLevel.PublicWritable:
  33062. default:
  33063. return 'public';
  33064. }
  33065. }
  33066. /**
  33067. * reflector.ts implements static reflection of declarations using the TypeScript `ts.TypeChecker`.
  33068. */
  33069. class TypeScriptReflectionHost {
  33070. checker;
  33071. isLocalCompilation;
  33072. skipPrivateValueDeclarationTypes;
  33073. /**
  33074. * @param skipPrivateValueDeclarationTypes Avoids using a value declaration that is considered private (using a ɵ-prefix),
  33075. * instead using the first available declaration. This is needed for the {@link FormControl} API of
  33076. * which the type declaration documents the type and the value declaration corresponds with an implementation detail.
  33077. */
  33078. constructor(checker, isLocalCompilation = false, skipPrivateValueDeclarationTypes = false) {
  33079. this.checker = checker;
  33080. this.isLocalCompilation = isLocalCompilation;
  33081. this.skipPrivateValueDeclarationTypes = skipPrivateValueDeclarationTypes;
  33082. }
  33083. getDecoratorsOfDeclaration(declaration) {
  33084. const decorators = ts.canHaveDecorators(declaration)
  33085. ? ts.getDecorators(declaration)
  33086. : undefined;
  33087. return decorators !== undefined && decorators.length
  33088. ? decorators
  33089. .map((decorator) => this._reflectDecorator(decorator))
  33090. .filter((dec) => dec !== null)
  33091. : null;
  33092. }
  33093. getMembersOfClass(clazz) {
  33094. const tsClazz = castDeclarationToClassOrDie(clazz);
  33095. return tsClazz.members
  33096. .map((member) => {
  33097. const result = reflectClassMember(member);
  33098. if (result === null) {
  33099. return null;
  33100. }
  33101. return {
  33102. ...result,
  33103. decorators: this.getDecoratorsOfDeclaration(member),
  33104. };
  33105. })
  33106. .filter((member) => member !== null);
  33107. }
  33108. getConstructorParameters(clazz) {
  33109. const tsClazz = castDeclarationToClassOrDie(clazz);
  33110. const isDeclaration = tsClazz.getSourceFile().isDeclarationFile;
  33111. // For non-declaration files, we want to find the constructor with a `body`. The constructors
  33112. // without a `body` are overloads whereas we want the implementation since it's the one that'll
  33113. // be executed and which can have decorators. For declaration files, we take the first one that
  33114. // we get.
  33115. const ctor = tsClazz.members.find((member) => ts.isConstructorDeclaration(member) && (isDeclaration || member.body !== undefined));
  33116. if (ctor === undefined) {
  33117. return null;
  33118. }
  33119. return ctor.parameters.map((node) => {
  33120. // The name of the parameter is easy.
  33121. const name = parameterName(node.name);
  33122. const decorators = this.getDecoratorsOfDeclaration(node);
  33123. // It may or may not be possible to write an expression that refers to the value side of the
  33124. // type named for the parameter.
  33125. let originalTypeNode = node.type || null;
  33126. let typeNode = originalTypeNode;
  33127. // Check if we are dealing with a simple nullable union type e.g. `foo: Foo|null`
  33128. // and extract the type. More complex union types e.g. `foo: Foo|Bar` are not supported.
  33129. // We also don't need to support `foo: Foo|undefined` because Angular's DI injects `null` for
  33130. // optional tokes that don't have providers.
  33131. if (typeNode && ts.isUnionTypeNode(typeNode)) {
  33132. let childTypeNodes = typeNode.types.filter((childTypeNode) => !(ts.isLiteralTypeNode(childTypeNode) &&
  33133. childTypeNode.literal.kind === ts.SyntaxKind.NullKeyword));
  33134. if (childTypeNodes.length === 1) {
  33135. typeNode = childTypeNodes[0];
  33136. }
  33137. }
  33138. const typeValueReference = typeToValue(typeNode, this.checker, this.isLocalCompilation);
  33139. return {
  33140. name,
  33141. nameNode: node.name,
  33142. typeValueReference,
  33143. typeNode: originalTypeNode,
  33144. decorators,
  33145. };
  33146. });
  33147. }
  33148. getImportOfIdentifier(id) {
  33149. const directImport = this.getDirectImportOfIdentifier(id);
  33150. if (directImport !== null) {
  33151. return directImport;
  33152. }
  33153. else if (ts.isQualifiedName(id.parent) && id.parent.right === id) {
  33154. return this.getImportOfNamespacedIdentifier(id, getQualifiedNameRoot(id.parent));
  33155. }
  33156. else if (ts.isPropertyAccessExpression(id.parent) && id.parent.name === id) {
  33157. return this.getImportOfNamespacedIdentifier(id, getFarLeftIdentifier(id.parent));
  33158. }
  33159. else {
  33160. return null;
  33161. }
  33162. }
  33163. getExportsOfModule(node) {
  33164. // In TypeScript code, modules are only ts.SourceFiles. Throw if the node isn't a module.
  33165. if (!ts.isSourceFile(node)) {
  33166. throw new Error(`getExportsOfModule() called on non-SourceFile in TS code`);
  33167. }
  33168. // Reflect the module to a Symbol, and use getExportsOfModule() to get a list of exported
  33169. // Symbols.
  33170. const symbol = this.checker.getSymbolAtLocation(node);
  33171. if (symbol === undefined) {
  33172. return null;
  33173. }
  33174. const map = new Map();
  33175. this.checker.getExportsOfModule(symbol).forEach((exportSymbol) => {
  33176. // Map each exported Symbol to a Declaration and add it to the map.
  33177. const decl = this.getDeclarationOfSymbol(exportSymbol, null);
  33178. if (decl !== null) {
  33179. map.set(exportSymbol.name, decl);
  33180. }
  33181. });
  33182. return map;
  33183. }
  33184. isClass(node) {
  33185. // For our purposes, classes are "named" ts.ClassDeclarations;
  33186. // (`node.name` can be undefined in unnamed default exports: `default export class { ... }`).
  33187. return isNamedClassDeclaration(node);
  33188. }
  33189. hasBaseClass(clazz) {
  33190. return this.getBaseClassExpression(clazz) !== null;
  33191. }
  33192. getBaseClassExpression(clazz) {
  33193. if (!(ts.isClassDeclaration(clazz) || ts.isClassExpression(clazz)) ||
  33194. clazz.heritageClauses === undefined) {
  33195. return null;
  33196. }
  33197. const extendsClause = clazz.heritageClauses.find((clause) => clause.token === ts.SyntaxKind.ExtendsKeyword);
  33198. if (extendsClause === undefined) {
  33199. return null;
  33200. }
  33201. const extendsType = extendsClause.types[0];
  33202. if (extendsType === undefined) {
  33203. return null;
  33204. }
  33205. return extendsType.expression;
  33206. }
  33207. getDeclarationOfIdentifier(id) {
  33208. // Resolve the identifier to a Symbol, and return the declaration of that.
  33209. let symbol = this.checker.getSymbolAtLocation(id);
  33210. if (symbol === undefined) {
  33211. return null;
  33212. }
  33213. return this.getDeclarationOfSymbol(symbol, id);
  33214. }
  33215. getDefinitionOfFunction(node) {
  33216. if (!ts.isFunctionDeclaration(node) &&
  33217. !ts.isMethodDeclaration(node) &&
  33218. !ts.isFunctionExpression(node) &&
  33219. !ts.isArrowFunction(node)) {
  33220. return null;
  33221. }
  33222. let body = null;
  33223. if (node.body !== undefined) {
  33224. // The body might be an expression if the node is an arrow function.
  33225. body = ts.isBlock(node.body)
  33226. ? Array.from(node.body.statements)
  33227. : [ts.factory.createReturnStatement(node.body)];
  33228. }
  33229. const type = this.checker.getTypeAtLocation(node);
  33230. const signatures = this.checker.getSignaturesOfType(type, ts.SignatureKind.Call);
  33231. return {
  33232. node,
  33233. body,
  33234. signatureCount: signatures.length,
  33235. typeParameters: node.typeParameters === undefined ? null : Array.from(node.typeParameters),
  33236. parameters: node.parameters.map((param) => {
  33237. const name = parameterName(param.name);
  33238. const initializer = param.initializer || null;
  33239. return { name, node: param, initializer, type: param.type || null };
  33240. }),
  33241. };
  33242. }
  33243. getGenericArityOfClass(clazz) {
  33244. if (!ts.isClassDeclaration(clazz)) {
  33245. return null;
  33246. }
  33247. return clazz.typeParameters !== undefined ? clazz.typeParameters.length : 0;
  33248. }
  33249. getVariableValue(declaration) {
  33250. return declaration.initializer || null;
  33251. }
  33252. isStaticallyExported(decl) {
  33253. // First check if there's an `export` modifier directly on the declaration.
  33254. let topLevel = decl;
  33255. if (ts.isVariableDeclaration(decl) && ts.isVariableDeclarationList(decl.parent)) {
  33256. topLevel = decl.parent.parent;
  33257. }
  33258. const modifiers = ts.canHaveModifiers(topLevel) ? ts.getModifiers(topLevel) : undefined;
  33259. if (modifiers !== undefined &&
  33260. modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
  33261. // The node is part of a declaration that's directly exported.
  33262. return true;
  33263. }
  33264. // If `topLevel` is not directly exported via a modifier, then it might be indirectly exported,
  33265. // e.g.:
  33266. //
  33267. // class Foo {}
  33268. // export {Foo};
  33269. //
  33270. // The only way to check this is to look at the module level for exports of the class. As a
  33271. // performance optimization, this check is only performed if the class is actually declared at
  33272. // the top level of the file and thus eligible for exporting in the first place.
  33273. if (topLevel.parent === undefined || !ts.isSourceFile(topLevel.parent)) {
  33274. return false;
  33275. }
  33276. const localExports = this.getLocalExportedDeclarationsOfSourceFile(decl.getSourceFile());
  33277. return localExports.has(decl);
  33278. }
  33279. getDirectImportOfIdentifier(id) {
  33280. const symbol = this.checker.getSymbolAtLocation(id);
  33281. if (symbol === undefined ||
  33282. symbol.declarations === undefined ||
  33283. symbol.declarations.length !== 1) {
  33284. return null;
  33285. }
  33286. const decl = symbol.declarations[0];
  33287. const importDecl = getContainingImportDeclaration(decl);
  33288. // Ignore declarations that are defined locally (not imported).
  33289. if (importDecl === null) {
  33290. return null;
  33291. }
  33292. // The module specifier is guaranteed to be a string literal, so this should always pass.
  33293. if (!ts.isStringLiteral(importDecl.moduleSpecifier)) {
  33294. // Not allowed to happen in TypeScript ASTs.
  33295. return null;
  33296. }
  33297. return {
  33298. from: importDecl.moduleSpecifier.text,
  33299. name: getExportedName(decl, id),
  33300. node: importDecl,
  33301. };
  33302. }
  33303. /**
  33304. * Try to get the import info for this identifier as though it is a namespaced import.
  33305. *
  33306. * For example, if the identifier is the `Directive` part of a qualified type chain like:
  33307. *
  33308. * ```ts
  33309. * core.Directive
  33310. * ```
  33311. *
  33312. * then it might be that `core` is a namespace import such as:
  33313. *
  33314. * ```ts
  33315. * import * as core from 'tslib';
  33316. * ```
  33317. *
  33318. * @param id the TypeScript identifier to find the import info for.
  33319. * @returns The import info if this is a namespaced import or `null`.
  33320. */
  33321. getImportOfNamespacedIdentifier(id, namespaceIdentifier) {
  33322. if (namespaceIdentifier === null) {
  33323. return null;
  33324. }
  33325. const namespaceSymbol = this.checker.getSymbolAtLocation(namespaceIdentifier);
  33326. if (!namespaceSymbol || namespaceSymbol.declarations === undefined) {
  33327. return null;
  33328. }
  33329. const declaration = namespaceSymbol.declarations.length === 1 ? namespaceSymbol.declarations[0] : null;
  33330. if (!declaration) {
  33331. return null;
  33332. }
  33333. const namespaceDeclaration = ts.isNamespaceImport(declaration) ? declaration : null;
  33334. if (!namespaceDeclaration) {
  33335. return null;
  33336. }
  33337. const importDeclaration = namespaceDeclaration.parent.parent;
  33338. if (!ts.isImportDeclaration(importDeclaration) ||
  33339. !ts.isStringLiteral(importDeclaration.moduleSpecifier)) {
  33340. // Should not happen as this would be invalid TypesScript
  33341. return null;
  33342. }
  33343. return {
  33344. from: importDeclaration.moduleSpecifier.text,
  33345. name: id.text,
  33346. node: importDeclaration,
  33347. };
  33348. }
  33349. /**
  33350. * Resolve a `ts.Symbol` to its declaration, keeping track of the `viaModule` along the way.
  33351. */
  33352. getDeclarationOfSymbol(symbol, originalId) {
  33353. // If the symbol points to a ShorthandPropertyAssignment, resolve it.
  33354. let valueDeclaration = undefined;
  33355. if (symbol.valueDeclaration !== undefined) {
  33356. valueDeclaration = symbol.valueDeclaration;
  33357. }
  33358. else if (symbol.declarations !== undefined && symbol.declarations.length > 0) {
  33359. valueDeclaration = symbol.declarations[0];
  33360. }
  33361. if (valueDeclaration !== undefined && ts.isShorthandPropertyAssignment(valueDeclaration)) {
  33362. const shorthandSymbol = this.checker.getShorthandAssignmentValueSymbol(valueDeclaration);
  33363. if (shorthandSymbol === undefined) {
  33364. return null;
  33365. }
  33366. return this.getDeclarationOfSymbol(shorthandSymbol, originalId);
  33367. }
  33368. else if (valueDeclaration !== undefined && ts.isExportSpecifier(valueDeclaration)) {
  33369. const targetSymbol = this.checker.getExportSpecifierLocalTargetSymbol(valueDeclaration);
  33370. if (targetSymbol === undefined) {
  33371. return null;
  33372. }
  33373. return this.getDeclarationOfSymbol(targetSymbol, originalId);
  33374. }
  33375. const importInfo = originalId && this.getImportOfIdentifier(originalId);
  33376. // Now, resolve the Symbol to its declaration by following any and all aliases.
  33377. while (symbol.flags & ts.SymbolFlags.Alias) {
  33378. symbol = this.checker.getAliasedSymbol(symbol);
  33379. }
  33380. // Look at the resolved Symbol's declarations and pick one of them to return.
  33381. // Value declarations are given precedence over type declarations if not specified otherwise
  33382. if (symbol.valueDeclaration !== undefined &&
  33383. (!this.skipPrivateValueDeclarationTypes || !isPrivateSymbol(this.checker, symbol))) {
  33384. return {
  33385. node: symbol.valueDeclaration,
  33386. viaModule: this._viaModule(symbol.valueDeclaration, originalId, importInfo),
  33387. };
  33388. }
  33389. else if (symbol.declarations !== undefined && symbol.declarations.length > 0) {
  33390. return {
  33391. node: symbol.declarations[0],
  33392. viaModule: this._viaModule(symbol.declarations[0], originalId, importInfo),
  33393. };
  33394. }
  33395. else {
  33396. return null;
  33397. }
  33398. }
  33399. _reflectDecorator(node) {
  33400. // Attempt to resolve the decorator expression into a reference to a concrete Identifier. The
  33401. // expression may contain a call to a function which returns the decorator function, in which
  33402. // case we want to return the arguments.
  33403. let decoratorExpr = node.expression;
  33404. let args = null;
  33405. // Check for call expressions.
  33406. if (ts.isCallExpression(decoratorExpr)) {
  33407. args = Array.from(decoratorExpr.arguments);
  33408. decoratorExpr = decoratorExpr.expression;
  33409. }
  33410. // The final resolved decorator should be a `ts.Identifier` - if it's not, then something is
  33411. // wrong and the decorator can't be resolved statically.
  33412. if (!isDecoratorIdentifier(decoratorExpr)) {
  33413. return null;
  33414. }
  33415. const decoratorIdentifier = ts.isIdentifier(decoratorExpr) ? decoratorExpr : decoratorExpr.name;
  33416. const importDecl = this.getImportOfIdentifier(decoratorIdentifier);
  33417. return {
  33418. name: decoratorIdentifier.text,
  33419. identifier: decoratorExpr,
  33420. import: importDecl,
  33421. node,
  33422. args,
  33423. };
  33424. }
  33425. /**
  33426. * Get the set of declarations declared in `file` which are exported.
  33427. */
  33428. getLocalExportedDeclarationsOfSourceFile(file) {
  33429. const cacheSf = file;
  33430. if (cacheSf[LocalExportedDeclarations] !== undefined) {
  33431. // TS does not currently narrow symbol-keyed fields, hence the non-null assert is needed.
  33432. return cacheSf[LocalExportedDeclarations];
  33433. }
  33434. const exportSet = new Set();
  33435. cacheSf[LocalExportedDeclarations] = exportSet;
  33436. const sfSymbol = this.checker.getSymbolAtLocation(cacheSf);
  33437. if (sfSymbol === undefined || sfSymbol.exports === undefined) {
  33438. return exportSet;
  33439. }
  33440. // Scan the exported symbol of the `ts.SourceFile` for the original `symbol` of the class
  33441. // declaration.
  33442. //
  33443. // Note: when checking multiple classes declared in the same file, this repeats some operations.
  33444. // In theory, this could be expensive if run in the context of a massive input file. If
  33445. // performance does become an issue here, it should be possible to create a `Set<>`
  33446. // Unfortunately, `ts.Iterator` doesn't implement the iterator protocol, so iteration here is
  33447. // done manually.
  33448. const iter = sfSymbol.exports.values();
  33449. let item = iter.next();
  33450. while (item.done !== true) {
  33451. let exportedSymbol = item.value;
  33452. // If this exported symbol comes from an `export {Foo}` statement, then the symbol is actually
  33453. // for the export declaration, not the original declaration. Such a symbol will be an alias,
  33454. // so unwrap aliasing if necessary.
  33455. if (exportedSymbol.flags & ts.SymbolFlags.Alias) {
  33456. exportedSymbol = this.checker.getAliasedSymbol(exportedSymbol);
  33457. }
  33458. if (exportedSymbol.valueDeclaration !== undefined &&
  33459. exportedSymbol.valueDeclaration.getSourceFile() === file) {
  33460. exportSet.add(exportedSymbol.valueDeclaration);
  33461. }
  33462. item = iter.next();
  33463. }
  33464. return exportSet;
  33465. }
  33466. _viaModule(declaration, originalId, importInfo) {
  33467. if (importInfo === null &&
  33468. originalId !== null &&
  33469. declaration.getSourceFile() !== originalId.getSourceFile()) {
  33470. return AmbientImport;
  33471. }
  33472. return importInfo !== null && importInfo.from !== null && !importInfo.from.startsWith('.')
  33473. ? importInfo.from
  33474. : null;
  33475. }
  33476. }
  33477. class TypeEntityToDeclarationError extends Error {
  33478. constructor(message) {
  33479. super(message);
  33480. // Extending `Error` ends up breaking some internal tests. This appears to be a known issue
  33481. // when extending errors in TS and the workaround is to explicitly set the prototype.
  33482. // https://stackoverflow.com/questions/41102060/typescript-extending-error-class
  33483. Object.setPrototypeOf(this, new.target.prototype);
  33484. }
  33485. }
  33486. /**
  33487. * @throws {TypeEntityToDeclarationError} if the type cannot be converted
  33488. * to a declaration.
  33489. */
  33490. function reflectTypeEntityToDeclaration(type, checker) {
  33491. let realSymbol = checker.getSymbolAtLocation(type);
  33492. if (realSymbol === undefined) {
  33493. throw new TypeEntityToDeclarationError(`Cannot resolve type entity ${type.getText()} to symbol`);
  33494. }
  33495. while (realSymbol.flags & ts.SymbolFlags.Alias) {
  33496. realSymbol = checker.getAliasedSymbol(realSymbol);
  33497. }
  33498. let node = null;
  33499. if (realSymbol.valueDeclaration !== undefined) {
  33500. node = realSymbol.valueDeclaration;
  33501. }
  33502. else if (realSymbol.declarations !== undefined && realSymbol.declarations.length === 1) {
  33503. node = realSymbol.declarations[0];
  33504. }
  33505. else {
  33506. throw new TypeEntityToDeclarationError(`Cannot resolve type entity symbol to declaration`);
  33507. }
  33508. if (ts.isQualifiedName(type)) {
  33509. if (!ts.isIdentifier(type.left)) {
  33510. throw new TypeEntityToDeclarationError(`Cannot handle qualified name with non-identifier lhs`);
  33511. }
  33512. const symbol = checker.getSymbolAtLocation(type.left);
  33513. if (symbol === undefined ||
  33514. symbol.declarations === undefined ||
  33515. symbol.declarations.length !== 1) {
  33516. throw new TypeEntityToDeclarationError(`Cannot resolve qualified type entity lhs to symbol`);
  33517. }
  33518. const decl = symbol.declarations[0];
  33519. if (ts.isNamespaceImport(decl)) {
  33520. const clause = decl.parent;
  33521. const importDecl = clause.parent;
  33522. if (!ts.isStringLiteral(importDecl.moduleSpecifier)) {
  33523. throw new TypeEntityToDeclarationError(`Module specifier is not a string`);
  33524. }
  33525. return { node, from: importDecl.moduleSpecifier.text };
  33526. }
  33527. else if (ts.isModuleDeclaration(decl)) {
  33528. return { node, from: null };
  33529. }
  33530. else {
  33531. throw new TypeEntityToDeclarationError(`Unknown import type?`);
  33532. }
  33533. }
  33534. else {
  33535. return { node, from: null };
  33536. }
  33537. }
  33538. function filterToMembersWithDecorator(members, name, module) {
  33539. return members
  33540. .filter((member) => !member.isStatic)
  33541. .map((member) => {
  33542. if (member.decorators === null) {
  33543. return null;
  33544. }
  33545. const decorators = member.decorators.filter((dec) => {
  33546. if (dec.import !== null) {
  33547. return dec.import.name === name && (module === undefined || dec.import.from === module);
  33548. }
  33549. else {
  33550. return dec.name === name && module === undefined;
  33551. }
  33552. });
  33553. if (decorators.length === 0) {
  33554. return null;
  33555. }
  33556. return { member, decorators };
  33557. })
  33558. .filter((value) => value !== null);
  33559. }
  33560. function extractModifiersOfMember(node) {
  33561. const modifiers = ts.getModifiers(node);
  33562. let isStatic = false;
  33563. let isReadonly = false;
  33564. let accessLevel = exports.ClassMemberAccessLevel.PublicWritable;
  33565. if (modifiers !== undefined) {
  33566. for (const modifier of modifiers) {
  33567. switch (modifier.kind) {
  33568. case ts.SyntaxKind.StaticKeyword:
  33569. isStatic = true;
  33570. break;
  33571. case ts.SyntaxKind.PrivateKeyword:
  33572. accessLevel = exports.ClassMemberAccessLevel.Private;
  33573. break;
  33574. case ts.SyntaxKind.ProtectedKeyword:
  33575. accessLevel = exports.ClassMemberAccessLevel.Protected;
  33576. break;
  33577. case ts.SyntaxKind.ReadonlyKeyword:
  33578. isReadonly = true;
  33579. break;
  33580. }
  33581. }
  33582. }
  33583. if (isReadonly && accessLevel === exports.ClassMemberAccessLevel.PublicWritable) {
  33584. accessLevel = exports.ClassMemberAccessLevel.PublicReadonly;
  33585. }
  33586. if (node.name !== undefined && ts.isPrivateIdentifier(node.name)) {
  33587. accessLevel = exports.ClassMemberAccessLevel.EcmaScriptPrivate;
  33588. }
  33589. return { accessLevel, isStatic };
  33590. }
  33591. /**
  33592. * Reflects a class element and returns static information about the
  33593. * class member.
  33594. *
  33595. * Note: Decorator information is not included in this helper as it relies
  33596. * on type checking to resolve originating import.
  33597. */
  33598. function reflectClassMember(node) {
  33599. let kind = null;
  33600. let value = null;
  33601. let name = null;
  33602. let nameNode = null;
  33603. if (ts.isPropertyDeclaration(node)) {
  33604. kind = exports.ClassMemberKind.Property;
  33605. value = node.initializer || null;
  33606. }
  33607. else if (ts.isGetAccessorDeclaration(node)) {
  33608. kind = exports.ClassMemberKind.Getter;
  33609. }
  33610. else if (ts.isSetAccessorDeclaration(node)) {
  33611. kind = exports.ClassMemberKind.Setter;
  33612. }
  33613. else if (ts.isMethodDeclaration(node)) {
  33614. kind = exports.ClassMemberKind.Method;
  33615. }
  33616. else if (ts.isConstructorDeclaration(node)) {
  33617. kind = exports.ClassMemberKind.Constructor;
  33618. }
  33619. else {
  33620. return null;
  33621. }
  33622. if (ts.isConstructorDeclaration(node)) {
  33623. name = 'constructor';
  33624. }
  33625. else if (ts.isIdentifier(node.name)) {
  33626. name = node.name.text;
  33627. nameNode = node.name;
  33628. }
  33629. else if (ts.isStringLiteral(node.name)) {
  33630. name = node.name.text;
  33631. nameNode = node.name;
  33632. }
  33633. else if (ts.isPrivateIdentifier(node.name)) {
  33634. name = node.name.text;
  33635. nameNode = node.name;
  33636. }
  33637. else {
  33638. return null;
  33639. }
  33640. const { accessLevel, isStatic } = extractModifiersOfMember(node);
  33641. return {
  33642. node,
  33643. implementation: node,
  33644. kind,
  33645. type: node.type || null,
  33646. accessLevel,
  33647. name,
  33648. nameNode,
  33649. value,
  33650. isStatic,
  33651. };
  33652. }
  33653. function reflectObjectLiteral(node) {
  33654. const map = new Map();
  33655. node.properties.forEach((prop) => {
  33656. if (ts.isPropertyAssignment(prop)) {
  33657. const name = propertyNameToString(prop.name);
  33658. if (name === null) {
  33659. return;
  33660. }
  33661. map.set(name, prop.initializer);
  33662. }
  33663. else if (ts.isShorthandPropertyAssignment(prop)) {
  33664. map.set(prop.name.text, prop.name);
  33665. }
  33666. else {
  33667. return;
  33668. }
  33669. });
  33670. return map;
  33671. }
  33672. function castDeclarationToClassOrDie(declaration) {
  33673. if (!ts.isClassDeclaration(declaration)) {
  33674. throw new Error(`Reflecting on a ${ts.SyntaxKind[declaration.kind]} instead of a ClassDeclaration.`);
  33675. }
  33676. return declaration;
  33677. }
  33678. function parameterName(name) {
  33679. if (ts.isIdentifier(name)) {
  33680. return name.text;
  33681. }
  33682. else {
  33683. return null;
  33684. }
  33685. }
  33686. function propertyNameToString(node) {
  33687. if (ts.isIdentifier(node) || ts.isStringLiteral(node) || ts.isNumericLiteral(node)) {
  33688. return node.text;
  33689. }
  33690. else {
  33691. return null;
  33692. }
  33693. }
  33694. /** Determines whether a given symbol represents a private API (symbols with names that start with `ɵ`) */
  33695. function isPrivateSymbol(typeChecker, symbol) {
  33696. if (symbol.valueDeclaration !== undefined) {
  33697. const symbolType = typeChecker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration);
  33698. return symbolType?.symbol?.name.startsWith('ɵ') === true;
  33699. }
  33700. return false;
  33701. }
  33702. /**
  33703. * Compute the left most identifier in a qualified type chain. E.g. the `a` of `a.b.c.SomeType`.
  33704. * @param qualifiedName The starting property access expression from which we want to compute
  33705. * the left most identifier.
  33706. * @returns the left most identifier in the chain or `null` if it is not an identifier.
  33707. */
  33708. function getQualifiedNameRoot(qualifiedName) {
  33709. while (ts.isQualifiedName(qualifiedName.left)) {
  33710. qualifiedName = qualifiedName.left;
  33711. }
  33712. return ts.isIdentifier(qualifiedName.left) ? qualifiedName.left : null;
  33713. }
  33714. /**
  33715. * Compute the left most identifier in a property access chain. E.g. the `a` of `a.b.c.d`.
  33716. * @param propertyAccess The starting property access expression from which we want to compute
  33717. * the left most identifier.
  33718. * @returns the left most identifier in the chain or `null` if it is not an identifier.
  33719. */
  33720. function getFarLeftIdentifier(propertyAccess) {
  33721. while (ts.isPropertyAccessExpression(propertyAccess.expression)) {
  33722. propertyAccess = propertyAccess.expression;
  33723. }
  33724. return ts.isIdentifier(propertyAccess.expression) ? propertyAccess.expression : null;
  33725. }
  33726. /**
  33727. * Gets the closest ancestor `ImportDeclaration` to a node.
  33728. */
  33729. function getContainingImportDeclaration(node) {
  33730. let parent = node.parent;
  33731. while (parent && !ts.isSourceFile(parent)) {
  33732. if (ts.isImportDeclaration(parent)) {
  33733. return parent;
  33734. }
  33735. parent = parent.parent;
  33736. }
  33737. return null;
  33738. }
  33739. /**
  33740. * Compute the name by which the `decl` was exported, not imported.
  33741. * If no such declaration can be found (e.g. it is a namespace import)
  33742. * then fallback to the `originalId`.
  33743. */
  33744. function getExportedName(decl, originalId) {
  33745. return ts.isImportSpecifier(decl)
  33746. ? (decl.propertyName !== undefined ? decl.propertyName : decl.name).text
  33747. : originalId.text;
  33748. }
  33749. const LocalExportedDeclarations = Symbol('LocalExportedDeclarations');
  33750. /**
  33751. * A `ts.Node` plus the context in which it was discovered.
  33752. *
  33753. * A `Reference` is a pointer to a `ts.Node` that was extracted from the program somehow. It
  33754. * contains not only the node itself, but the information regarding how the node was located. In
  33755. * particular, it might track different identifiers by which the node is exposed, as well as
  33756. * potentially a module specifier which might expose the node.
  33757. *
  33758. * The Angular compiler uses `Reference`s instead of `ts.Node`s when tracking classes or generating
  33759. * imports.
  33760. */
  33761. class Reference {
  33762. node;
  33763. /**
  33764. * The compiler's best guess at an absolute module specifier which owns this `Reference`.
  33765. *
  33766. * This is usually determined by tracking the import statements which led the compiler to a given
  33767. * node. If any of these imports are absolute, it's an indication that the node being imported
  33768. * might come from that module.
  33769. *
  33770. * It is not _guaranteed_ that the node in question is exported from its `bestGuessOwningModule` -
  33771. * that is mostly a convention that applies in certain package formats.
  33772. *
  33773. * If `bestGuessOwningModule` is `null`, then it's likely the node came from the current program.
  33774. */
  33775. bestGuessOwningModule;
  33776. identifiers = [];
  33777. /**
  33778. * Indicates that the Reference was created synthetically, not as a result of natural value
  33779. * resolution.
  33780. *
  33781. * This is used to avoid misinterpreting the Reference in certain contexts.
  33782. */
  33783. synthetic = false;
  33784. _alias = null;
  33785. isAmbient;
  33786. constructor(node, bestGuessOwningModule = null) {
  33787. this.node = node;
  33788. if (bestGuessOwningModule === AmbientImport) {
  33789. this.isAmbient = true;
  33790. this.bestGuessOwningModule = null;
  33791. }
  33792. else {
  33793. this.isAmbient = false;
  33794. this.bestGuessOwningModule = bestGuessOwningModule;
  33795. }
  33796. const id = identifierOfNode(node);
  33797. if (id !== null) {
  33798. this.identifiers.push(id);
  33799. }
  33800. }
  33801. /**
  33802. * The best guess at which module specifier owns this particular reference, or `null` if there
  33803. * isn't one.
  33804. */
  33805. get ownedByModuleGuess() {
  33806. if (this.bestGuessOwningModule !== null) {
  33807. return this.bestGuessOwningModule.specifier;
  33808. }
  33809. else {
  33810. return null;
  33811. }
  33812. }
  33813. /**
  33814. * Whether this reference has a potential owning module or not.
  33815. *
  33816. * See `bestGuessOwningModule`.
  33817. */
  33818. get hasOwningModuleGuess() {
  33819. return this.bestGuessOwningModule !== null;
  33820. }
  33821. /**
  33822. * A name for the node, if one is available.
  33823. *
  33824. * This is only suited for debugging. Any actual references to this node should be made with
  33825. * `ts.Identifier`s (see `getIdentityIn`).
  33826. */
  33827. get debugName() {
  33828. const id = identifierOfNode(this.node);
  33829. return id !== null ? id.text : null;
  33830. }
  33831. get alias() {
  33832. return this._alias;
  33833. }
  33834. /**
  33835. * Record a `ts.Identifier` by which it's valid to refer to this node, within the context of this
  33836. * `Reference`.
  33837. */
  33838. addIdentifier(identifier) {
  33839. this.identifiers.push(identifier);
  33840. }
  33841. /**
  33842. * Get a `ts.Identifier` within this `Reference` that can be used to refer within the context of a
  33843. * given `ts.SourceFile`, if any.
  33844. */
  33845. getIdentityIn(context) {
  33846. return this.identifiers.find((id) => id.getSourceFile() === context) || null;
  33847. }
  33848. /**
  33849. * Get a `ts.Identifier` for this `Reference` that exists within the given expression.
  33850. *
  33851. * This is very useful for producing `ts.Diagnostic`s that reference `Reference`s that were
  33852. * extracted from some larger expression, as it can be used to pinpoint the `ts.Identifier` within
  33853. * the expression from which the `Reference` originated.
  33854. */
  33855. getIdentityInExpression(expr) {
  33856. const sf = expr.getSourceFile();
  33857. return (this.identifiers.find((id) => {
  33858. if (id.getSourceFile() !== sf) {
  33859. return false;
  33860. }
  33861. // This identifier is a match if its position lies within the given expression.
  33862. return id.pos >= expr.pos && id.end <= expr.end;
  33863. }) || null);
  33864. }
  33865. /**
  33866. * Given the 'container' expression from which this `Reference` was extracted, produce a
  33867. * `ts.Expression` to use in a diagnostic which best indicates the position within the container
  33868. * expression that generated the `Reference`.
  33869. *
  33870. * For example, given a `Reference` to the class 'Bar' and the containing expression:
  33871. * `[Foo, Bar, Baz]`, this function would attempt to return the `ts.Identifier` for `Bar` within
  33872. * the array. This could be used to produce a nice diagnostic context:
  33873. *
  33874. * ```text
  33875. * [Foo, Bar, Baz]
  33876. * ~~~
  33877. * ```
  33878. *
  33879. * If no specific node can be found, then the `fallback` expression is used, which defaults to the
  33880. * entire containing expression.
  33881. */
  33882. getOriginForDiagnostics(container, fallback = container) {
  33883. const id = this.getIdentityInExpression(container);
  33884. return id !== null ? id : fallback;
  33885. }
  33886. cloneWithAlias(alias) {
  33887. const ref = new Reference(this.node, this.isAmbient ? AmbientImport : this.bestGuessOwningModule);
  33888. ref.identifiers = [...this.identifiers];
  33889. ref._alias = alias;
  33890. return ref;
  33891. }
  33892. cloneWithNoIdentifiers() {
  33893. const ref = new Reference(this.node, this.isAmbient ? AmbientImport : this.bestGuessOwningModule);
  33894. ref._alias = this._alias;
  33895. ref.identifiers = [];
  33896. return ref;
  33897. }
  33898. }
  33899. /** Module name of the framework core. */
  33900. const CORE_MODULE = '@angular/core';
  33901. function valueReferenceToExpression(valueRef) {
  33902. if (valueRef.kind === 2 /* TypeValueReferenceKind.UNAVAILABLE */) {
  33903. return null;
  33904. }
  33905. else if (valueRef.kind === 0 /* TypeValueReferenceKind.LOCAL */) {
  33906. const expr = new WrappedNodeExpr(valueRef.expression);
  33907. if (valueRef.defaultImportStatement !== null) {
  33908. attachDefaultImportDeclaration(expr, valueRef.defaultImportStatement);
  33909. }
  33910. return expr;
  33911. }
  33912. else {
  33913. let importExpr = new ExternalExpr({
  33914. moduleName: valueRef.moduleName,
  33915. name: valueRef.importedName,
  33916. });
  33917. if (valueRef.nestedPath !== null) {
  33918. for (const property of valueRef.nestedPath) {
  33919. importExpr = new ReadPropExpr(importExpr, property);
  33920. }
  33921. }
  33922. return importExpr;
  33923. }
  33924. }
  33925. function toR3Reference(origin, ref, context, refEmitter) {
  33926. const emittedValueRef = refEmitter.emit(ref, context);
  33927. assertSuccessfulReferenceEmit(emittedValueRef, origin, 'class');
  33928. const emittedTypeRef = refEmitter.emit(ref, context, exports.ImportFlags.ForceNewImport | exports.ImportFlags.AllowTypeImports);
  33929. assertSuccessfulReferenceEmit(emittedTypeRef, origin, 'class');
  33930. return {
  33931. value: emittedValueRef.expression,
  33932. type: emittedTypeRef.expression,
  33933. };
  33934. }
  33935. function isAngularCore(decorator) {
  33936. return decorator.import !== null && decorator.import.from === CORE_MODULE;
  33937. }
  33938. /**
  33939. * This function is used for verifying that a given reference is declared
  33940. * inside `@angular/core` and corresponds to the given symbol name.
  33941. *
  33942. * In some cases, due to the compiler face duplicating many symbols as
  33943. * an independent bridge between core and the compiler, the dts bundler may
  33944. * decide to alias declarations in the `.d.ts`, to avoid conflicts.
  33945. *
  33946. * e.g.
  33947. *
  33948. * ```
  33949. * declare enum ViewEncapsulation {} // from the facade
  33950. * declare enum ViewEncapsulation$1 {} // the real one exported to users.
  33951. * ```
  33952. *
  33953. * This function accounts for such potential re-namings.
  33954. */
  33955. function isAngularCoreReferenceWithPotentialAliasing(reference, symbolName, isCore) {
  33956. return ((reference.ownedByModuleGuess === CORE_MODULE || isCore) &&
  33957. reference.debugName?.replace(/\$\d+$/, '') === symbolName);
  33958. }
  33959. function findAngularDecorator(decorators, name, isCore) {
  33960. return decorators.find((decorator) => isAngularDecorator(decorator, name, isCore));
  33961. }
  33962. function isAngularDecorator(decorator, name, isCore) {
  33963. if (isCore) {
  33964. return decorator.name === name;
  33965. }
  33966. else if (isAngularCore(decorator)) {
  33967. return decorator.import.name === name;
  33968. }
  33969. return false;
  33970. }
  33971. function getAngularDecorators(decorators, names, isCore) {
  33972. return decorators.filter((decorator) => {
  33973. const name = isCore ? decorator.name : decorator.import?.name;
  33974. if (name === undefined || !names.includes(name)) {
  33975. return false;
  33976. }
  33977. return isCore || isAngularCore(decorator);
  33978. });
  33979. }
  33980. /**
  33981. * Unwrap a `ts.Expression`, removing outer type-casts or parentheses until the expression is in its
  33982. * lowest level form.
  33983. *
  33984. * For example, the expression "(foo as Type)" unwraps to "foo".
  33985. */
  33986. function unwrapExpression(node) {
  33987. while (ts.isAsExpression(node) || ts.isParenthesizedExpression(node)) {
  33988. node = node.expression;
  33989. }
  33990. return node;
  33991. }
  33992. function expandForwardRef(arg) {
  33993. arg = unwrapExpression(arg);
  33994. if (!ts.isArrowFunction(arg) && !ts.isFunctionExpression(arg)) {
  33995. return null;
  33996. }
  33997. const body = arg.body;
  33998. // Either the body is a ts.Expression directly, or a block with a single return statement.
  33999. if (ts.isBlock(body)) {
  34000. // Block body - look for a single return statement.
  34001. if (body.statements.length !== 1) {
  34002. return null;
  34003. }
  34004. const stmt = body.statements[0];
  34005. if (!ts.isReturnStatement(stmt) || stmt.expression === undefined) {
  34006. return null;
  34007. }
  34008. return stmt.expression;
  34009. }
  34010. else {
  34011. // Shorthand body - return as an expression.
  34012. return body;
  34013. }
  34014. }
  34015. /**
  34016. * If the given `node` is a forwardRef() expression then resolve its inner value, otherwise return
  34017. * `null`.
  34018. *
  34019. * @param node the forwardRef() expression to resolve
  34020. * @param reflector a ReflectionHost
  34021. * @returns the resolved expression, if the original expression was a forwardRef(), or `null`
  34022. * otherwise.
  34023. */
  34024. function tryUnwrapForwardRef(node, reflector) {
  34025. node = unwrapExpression(node);
  34026. if (!ts.isCallExpression(node) || node.arguments.length !== 1) {
  34027. return null;
  34028. }
  34029. const fn = ts.isPropertyAccessExpression(node.expression)
  34030. ? node.expression.name
  34031. : node.expression;
  34032. if (!ts.isIdentifier(fn)) {
  34033. return null;
  34034. }
  34035. const expr = expandForwardRef(node.arguments[0]);
  34036. if (expr === null) {
  34037. return null;
  34038. }
  34039. const imp = reflector.getImportOfIdentifier(fn);
  34040. if (imp === null || imp.from !== '@angular/core' || imp.name !== 'forwardRef') {
  34041. return null;
  34042. }
  34043. return expr;
  34044. }
  34045. /**
  34046. * A foreign function resolver for `staticallyResolve` which unwraps forwardRef() expressions.
  34047. *
  34048. * @param ref a Reference to the declaration of the function being called (which might be
  34049. * forwardRef)
  34050. * @param args the arguments to the invocation of the forwardRef expression
  34051. * @returns an unwrapped argument if `ref` pointed to forwardRef, or null otherwise
  34052. */
  34053. function createForwardRefResolver(isCore) {
  34054. return (fn, callExpr, resolve, unresolvable) => {
  34055. if (!isAngularCoreReferenceWithPotentialAliasing(fn, 'forwardRef', isCore) ||
  34056. callExpr.arguments.length !== 1) {
  34057. return unresolvable;
  34058. }
  34059. const expanded = expandForwardRef(callExpr.arguments[0]);
  34060. if (expanded !== null) {
  34061. return resolve(expanded);
  34062. }
  34063. else {
  34064. return unresolvable;
  34065. }
  34066. };
  34067. }
  34068. /**
  34069. * Combines an array of resolver functions into a one.
  34070. * @param resolvers Resolvers to be combined.
  34071. */
  34072. function combineResolvers(resolvers) {
  34073. return (fn, callExpr, resolve, unresolvable) => {
  34074. for (const resolver of resolvers) {
  34075. const resolved = resolver(fn, callExpr, resolve, unresolvable);
  34076. if (resolved !== unresolvable) {
  34077. return resolved;
  34078. }
  34079. }
  34080. return unresolvable;
  34081. };
  34082. }
  34083. function isExpressionForwardReference(expr, context, contextSource) {
  34084. if (isWrappedTsNodeExpr(expr)) {
  34085. const node = ts.getOriginalNode(expr.node);
  34086. return node.getSourceFile() === contextSource && context.pos < node.pos;
  34087. }
  34088. else {
  34089. return false;
  34090. }
  34091. }
  34092. function isWrappedTsNodeExpr(expr) {
  34093. return expr instanceof WrappedNodeExpr;
  34094. }
  34095. function readBaseClass(node, reflector, evaluator) {
  34096. const baseExpression = reflector.getBaseClassExpression(node);
  34097. if (baseExpression !== null) {
  34098. const baseClass = evaluator.evaluate(baseExpression);
  34099. if (baseClass instanceof Reference && reflector.isClass(baseClass.node)) {
  34100. return baseClass;
  34101. }
  34102. else {
  34103. return 'dynamic';
  34104. }
  34105. }
  34106. return null;
  34107. }
  34108. const parensWrapperTransformerFactory = (context) => {
  34109. const visitor = (node) => {
  34110. const visited = ts.visitEachChild(node, visitor, context);
  34111. if (ts.isArrowFunction(visited) || ts.isFunctionExpression(visited)) {
  34112. return ts.factory.createParenthesizedExpression(visited);
  34113. }
  34114. return visited;
  34115. };
  34116. return (node) => ts.visitEachChild(node, visitor, context);
  34117. };
  34118. /**
  34119. * Wraps all functions in a given expression in parentheses. This is needed to avoid problems
  34120. * where Tsickle annotations added between analyse and transform phases in Angular may trigger
  34121. * automatic semicolon insertion, e.g. if a function is the expression in a `return` statement.
  34122. * More
  34123. * info can be found in Tsickle source code here:
  34124. * https://github.com/angular/tsickle/blob/d7974262571c8a17d684e5ba07680e1b1993afdd/src/jsdoc_transformer.ts#L1021
  34125. *
  34126. * @param expression Expression where functions should be wrapped in parentheses
  34127. */
  34128. function wrapFunctionExpressionsInParens(expression) {
  34129. return ts.transform(expression, [parensWrapperTransformerFactory]).transformed[0];
  34130. }
  34131. /**
  34132. * Resolves the given `rawProviders` into `ClassDeclarations` and returns
  34133. * a set containing those that are known to require a factory definition.
  34134. * @param rawProviders Expression that declared the providers array in the source.
  34135. */
  34136. function resolveProvidersRequiringFactory(rawProviders, reflector, evaluator) {
  34137. const providers = new Set();
  34138. const resolvedProviders = evaluator.evaluate(rawProviders);
  34139. if (!Array.isArray(resolvedProviders)) {
  34140. return providers;
  34141. }
  34142. resolvedProviders.forEach(function processProviders(provider) {
  34143. let tokenClass = null;
  34144. if (Array.isArray(provider)) {
  34145. // If we ran into an array, recurse into it until we've resolve all the classes.
  34146. provider.forEach(processProviders);
  34147. }
  34148. else if (provider instanceof Reference) {
  34149. tokenClass = provider;
  34150. }
  34151. else if (provider instanceof Map && provider.has('useClass') && !provider.has('deps')) {
  34152. const useExisting = provider.get('useClass');
  34153. if (useExisting instanceof Reference) {
  34154. tokenClass = useExisting;
  34155. }
  34156. }
  34157. // TODO(alxhub): there was a bug where `getConstructorParameters` would return `null` for a
  34158. // class in a .d.ts file, always, even if the class had a constructor. This was fixed for
  34159. // `getConstructorParameters`, but that fix causes more classes to be recognized here as needing
  34160. // provider checks, which is a breaking change in g3. Avoid this breakage for now by skipping
  34161. // classes from .d.ts files here directly, until g3 can be cleaned up.
  34162. if (tokenClass !== null &&
  34163. !tokenClass.node.getSourceFile().isDeclarationFile &&
  34164. reflector.isClass(tokenClass.node)) {
  34165. const constructorParameters = reflector.getConstructorParameters(tokenClass.node);
  34166. // Note that we only want to capture providers with a non-trivial constructor,
  34167. // because they're the ones that might be using DI and need to be decorated.
  34168. if (constructorParameters !== null && constructorParameters.length > 0) {
  34169. providers.add(tokenClass);
  34170. }
  34171. }
  34172. });
  34173. return providers;
  34174. }
  34175. /**
  34176. * Create an R3Reference for a class.
  34177. *
  34178. * The `value` is the exported declaration of the class from its source file.
  34179. * The `type` is an expression that would be used in the typings (.d.ts) files.
  34180. */
  34181. function wrapTypeReference(reflector, clazz) {
  34182. const value = new WrappedNodeExpr(clazz.name);
  34183. const type = value;
  34184. return { value, type };
  34185. }
  34186. /** Creates a ParseSourceSpan for a TypeScript node. */
  34187. function createSourceSpan(node) {
  34188. const sf = node.getSourceFile();
  34189. const [startOffset, endOffset] = [node.getStart(), node.getEnd()];
  34190. const { line: startLine, character: startCol } = sf.getLineAndCharacterOfPosition(startOffset);
  34191. const { line: endLine, character: endCol } = sf.getLineAndCharacterOfPosition(endOffset);
  34192. const parseSf = new ParseSourceFile(sf.getFullText(), sf.fileName);
  34193. // +1 because values are zero-indexed.
  34194. return new ParseSourceSpan(new ParseLocation(parseSf, startOffset, startLine + 1, startCol + 1), new ParseLocation(parseSf, endOffset, endLine + 1, endCol + 1));
  34195. }
  34196. /**
  34197. * Collate the factory and definition compiled results into an array of CompileResult objects.
  34198. */
  34199. function compileResults(fac, def, metadataStmt, propName, additionalFields, deferrableImports, debugInfo = null, hmrInitializer = null) {
  34200. const statements = def.statements;
  34201. if (metadataStmt !== null) {
  34202. statements.push(metadataStmt);
  34203. }
  34204. if (debugInfo !== null) {
  34205. statements.push(debugInfo);
  34206. }
  34207. if (hmrInitializer !== null) {
  34208. statements.push(hmrInitializer);
  34209. }
  34210. const results = [
  34211. fac,
  34212. {
  34213. name: propName,
  34214. initializer: def.expression,
  34215. statements: def.statements,
  34216. type: def.type,
  34217. deferrableImports,
  34218. },
  34219. ];
  34220. if (additionalFields !== null) {
  34221. results.push(...additionalFields);
  34222. }
  34223. return results;
  34224. }
  34225. function toFactoryMetadata(meta, target) {
  34226. return {
  34227. name: meta.name,
  34228. type: meta.type,
  34229. typeArgumentCount: meta.typeArgumentCount,
  34230. deps: meta.deps,
  34231. target,
  34232. };
  34233. }
  34234. function resolveImportedFile(moduleResolver, importedFile, expr, origin) {
  34235. // If `importedFile` is not 'unknown' then it accurately reflects the source file that is
  34236. // being imported.
  34237. if (importedFile !== 'unknown') {
  34238. return importedFile;
  34239. }
  34240. // Otherwise `expr` has to be inspected to determine the file that is being imported. If `expr`
  34241. // is not an `ExternalExpr` then it does not correspond with an import, so return null in that
  34242. // case.
  34243. if (!(expr instanceof ExternalExpr)) {
  34244. return null;
  34245. }
  34246. // Figure out what file is being imported.
  34247. return moduleResolver.resolveModule(expr.value.moduleName, origin.fileName);
  34248. }
  34249. /**
  34250. * Determines the most appropriate expression for diagnostic reporting purposes. If `expr` is
  34251. * contained within `container` then `expr` is used as origin node, otherwise `container` itself is
  34252. * used.
  34253. */
  34254. function getOriginNodeForDiagnostics(expr, container) {
  34255. const nodeSf = expr.getSourceFile();
  34256. const exprSf = container.getSourceFile();
  34257. if (nodeSf === exprSf && expr.pos >= container.pos && expr.end <= container.end) {
  34258. // `expr` occurs within the same source file as `container` and is contained within it, so
  34259. // `expr` is appropriate to use as origin node for diagnostics.
  34260. return expr;
  34261. }
  34262. else {
  34263. return container;
  34264. }
  34265. }
  34266. function isAbstractClassDeclaration(clazz) {
  34267. return ts.canHaveModifiers(clazz) && clazz.modifiers !== undefined
  34268. ? clazz.modifiers.some((mod) => mod.kind === ts.SyntaxKind.AbstractKeyword)
  34269. : false;
  34270. }
  34271. function getConstructorDependencies(clazz, reflector, isCore) {
  34272. const deps = [];
  34273. const errors = [];
  34274. let ctorParams = reflector.getConstructorParameters(clazz);
  34275. if (ctorParams === null) {
  34276. if (reflector.hasBaseClass(clazz)) {
  34277. return null;
  34278. }
  34279. else {
  34280. ctorParams = [];
  34281. }
  34282. }
  34283. ctorParams.forEach((param, idx) => {
  34284. let token = valueReferenceToExpression(param.typeValueReference);
  34285. let attributeNameType = null;
  34286. let optional = false, self = false, skipSelf = false, host = false;
  34287. (param.decorators || [])
  34288. .filter((dec) => isCore || isAngularCore(dec))
  34289. .forEach((dec) => {
  34290. const name = isCore || dec.import === null ? dec.name : dec.import.name;
  34291. if (name === 'Inject') {
  34292. if (dec.args === null || dec.args.length !== 1) {
  34293. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, dec.node, `Unexpected number of arguments to @Inject().`);
  34294. }
  34295. token = new WrappedNodeExpr(dec.args[0]);
  34296. }
  34297. else if (name === 'Optional') {
  34298. optional = true;
  34299. }
  34300. else if (name === 'SkipSelf') {
  34301. skipSelf = true;
  34302. }
  34303. else if (name === 'Self') {
  34304. self = true;
  34305. }
  34306. else if (name === 'Host') {
  34307. host = true;
  34308. }
  34309. else if (name === 'Attribute') {
  34310. if (dec.args === null || dec.args.length !== 1) {
  34311. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, dec.node, `Unexpected number of arguments to @Attribute().`);
  34312. }
  34313. const attributeName = dec.args[0];
  34314. token = new WrappedNodeExpr(attributeName);
  34315. if (ts.isStringLiteralLike(attributeName)) {
  34316. attributeNameType = new LiteralExpr(attributeName.text);
  34317. }
  34318. else {
  34319. attributeNameType = new WrappedNodeExpr(ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword));
  34320. }
  34321. }
  34322. else {
  34323. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_UNEXPECTED, dec.node, `Unexpected decorator ${name} on parameter.`);
  34324. }
  34325. });
  34326. if (token === null) {
  34327. if (param.typeValueReference.kind !== 2 /* TypeValueReferenceKind.UNAVAILABLE */) {
  34328. throw new Error('Illegal state: expected value reference to be unavailable if no token is present');
  34329. }
  34330. errors.push({
  34331. index: idx,
  34332. param,
  34333. reason: param.typeValueReference.reason,
  34334. });
  34335. }
  34336. else {
  34337. deps.push({ token, attributeNameType, optional, self, skipSelf, host });
  34338. }
  34339. });
  34340. if (errors.length === 0) {
  34341. return { deps };
  34342. }
  34343. else {
  34344. return { deps: null, errors };
  34345. }
  34346. }
  34347. /**
  34348. * Convert `ConstructorDeps` into the `R3DependencyMetadata` array for those deps if they're valid,
  34349. * or into an `'invalid'` signal if they're not.
  34350. *
  34351. * This is a companion function to `validateConstructorDependencies` which accepts invalid deps.
  34352. */
  34353. function unwrapConstructorDependencies(deps) {
  34354. if (deps === null) {
  34355. return null;
  34356. }
  34357. else if (deps.deps !== null) {
  34358. // These constructor dependencies are valid.
  34359. return deps.deps;
  34360. }
  34361. else {
  34362. // These deps are invalid.
  34363. return 'invalid';
  34364. }
  34365. }
  34366. function getValidConstructorDependencies(clazz, reflector, isCore) {
  34367. return validateConstructorDependencies(clazz, getConstructorDependencies(clazz, reflector, isCore));
  34368. }
  34369. /**
  34370. * Validate that `ConstructorDeps` does not have any invalid dependencies and convert them into the
  34371. * `R3DependencyMetadata` array if so, or raise a diagnostic if some deps are invalid.
  34372. *
  34373. * This is a companion function to `unwrapConstructorDependencies` which does not accept invalid
  34374. * deps.
  34375. */
  34376. function validateConstructorDependencies(clazz, deps) {
  34377. if (deps === null) {
  34378. return null;
  34379. }
  34380. else if (deps.deps !== null) {
  34381. return deps.deps;
  34382. }
  34383. else {
  34384. // There is at least one error.
  34385. const error = deps.errors[0];
  34386. throw createUnsuitableInjectionTokenError(clazz, error);
  34387. }
  34388. }
  34389. /**
  34390. * Creates a fatal error with diagnostic for an invalid injection token.
  34391. * @param clazz The class for which the injection token was unavailable.
  34392. * @param error The reason why no valid injection token is available.
  34393. */
  34394. function createUnsuitableInjectionTokenError(clazz, error) {
  34395. const { param, index, reason } = error;
  34396. let chainMessage = undefined;
  34397. let hints = undefined;
  34398. switch (reason.kind) {
  34399. case 5 /* ValueUnavailableKind.UNSUPPORTED */:
  34400. chainMessage = 'Consider using the @Inject decorator to specify an injection token.';
  34401. hints = [
  34402. makeRelatedInformation(reason.typeNode, 'This type is not supported as injection token.'),
  34403. ];
  34404. break;
  34405. case 1 /* ValueUnavailableKind.NO_VALUE_DECLARATION */:
  34406. chainMessage = 'Consider using the @Inject decorator to specify an injection token.';
  34407. hints = [
  34408. makeRelatedInformation(reason.typeNode, 'This type does not have a value, so it cannot be used as injection token.'),
  34409. ];
  34410. if (reason.decl !== null) {
  34411. hints.push(makeRelatedInformation(reason.decl, 'The type is declared here.'));
  34412. }
  34413. break;
  34414. case 2 /* ValueUnavailableKind.TYPE_ONLY_IMPORT */:
  34415. chainMessage =
  34416. 'Consider changing the type-only import to a regular import, or use the @Inject decorator to specify an injection token.';
  34417. hints = [
  34418. makeRelatedInformation(reason.typeNode, 'This type is imported using a type-only import, which prevents it from being usable as an injection token.'),
  34419. makeRelatedInformation(reason.node, 'The type-only import occurs here.'),
  34420. ];
  34421. break;
  34422. case 4 /* ValueUnavailableKind.NAMESPACE */:
  34423. chainMessage = 'Consider using the @Inject decorator to specify an injection token.';
  34424. hints = [
  34425. makeRelatedInformation(reason.typeNode, 'This type corresponds with a namespace, which cannot be used as injection token.'),
  34426. makeRelatedInformation(reason.importClause, 'The namespace import occurs here.'),
  34427. ];
  34428. break;
  34429. case 3 /* ValueUnavailableKind.UNKNOWN_REFERENCE */:
  34430. chainMessage = 'The type should reference a known declaration.';
  34431. hints = [makeRelatedInformation(reason.typeNode, 'This type could not be resolved.')];
  34432. break;
  34433. case 0 /* ValueUnavailableKind.MISSING_TYPE */:
  34434. chainMessage =
  34435. 'Consider adding a type to the parameter or use the @Inject decorator to specify an injection token.';
  34436. break;
  34437. }
  34438. const chain = {
  34439. messageText: `No suitable injection token for parameter '${param.name || index}' of class '${clazz.name.text}'.`,
  34440. category: ts.DiagnosticCategory.Error,
  34441. code: 0,
  34442. next: [
  34443. {
  34444. messageText: chainMessage,
  34445. category: ts.DiagnosticCategory.Message,
  34446. code: 0,
  34447. },
  34448. ],
  34449. };
  34450. return new FatalDiagnosticError(exports.ErrorCode.PARAM_MISSING_TOKEN, param.nameNode, chain, hints);
  34451. }
  34452. /**
  34453. * Disambiguates different kinds of compiler metadata objects.
  34454. */
  34455. exports.MetaKind = void 0;
  34456. (function (MetaKind) {
  34457. MetaKind[MetaKind["Directive"] = 0] = "Directive";
  34458. MetaKind[MetaKind["Pipe"] = 1] = "Pipe";
  34459. MetaKind[MetaKind["NgModule"] = 2] = "NgModule";
  34460. })(exports.MetaKind || (exports.MetaKind = {}));
  34461. /**
  34462. * Possible ways that a directive can be matched.
  34463. */
  34464. exports.MatchSource = void 0;
  34465. (function (MatchSource) {
  34466. /** The directive was matched by its selector. */
  34467. MatchSource[MatchSource["Selector"] = 0] = "Selector";
  34468. /** The directive was applied as a host directive. */
  34469. MatchSource[MatchSource["HostDirective"] = 1] = "HostDirective";
  34470. })(exports.MatchSource || (exports.MatchSource = {}));
  34471. /**
  34472. * A mapping of component property and template binding property names, for example containing the
  34473. * inputs of a particular directive or component.
  34474. *
  34475. * A single component property has exactly one input/output annotation (and therefore one binding
  34476. * property name) associated with it, but the same binding property name may be shared across many
  34477. * component property names.
  34478. *
  34479. * Allows bidirectional querying of the mapping - looking up all inputs/outputs with a given
  34480. * property name, or mapping from a specific class property to its binding property name.
  34481. */
  34482. class ClassPropertyMapping {
  34483. /**
  34484. * Mapping from class property names to the single `InputOrOutput` for that class property.
  34485. */
  34486. forwardMap;
  34487. /**
  34488. * Mapping from property names to one or more `InputOrOutput`s which share that name.
  34489. */
  34490. reverseMap;
  34491. constructor(forwardMap) {
  34492. this.forwardMap = forwardMap;
  34493. this.reverseMap = reverseMapFromForwardMap(forwardMap);
  34494. }
  34495. /**
  34496. * Construct a `ClassPropertyMapping` with no entries.
  34497. */
  34498. static empty() {
  34499. return new ClassPropertyMapping(new Map());
  34500. }
  34501. /**
  34502. * Construct a `ClassPropertyMapping` from a primitive JS object which maps class property names
  34503. * to either binding property names or an array that contains both names, which is used in on-disk
  34504. * metadata formats (e.g. in .d.ts files).
  34505. */
  34506. static fromMappedObject(obj) {
  34507. const forwardMap = new Map();
  34508. for (const classPropertyName of Object.keys(obj)) {
  34509. const value = obj[classPropertyName];
  34510. let inputOrOutput;
  34511. if (typeof value === 'string') {
  34512. inputOrOutput = {
  34513. classPropertyName,
  34514. bindingPropertyName: value,
  34515. // Inputs/outputs not captured via an explicit `InputOrOutput` mapping
  34516. // value are always considered non-signal. This is the string shorthand.
  34517. isSignal: false,
  34518. };
  34519. }
  34520. else {
  34521. inputOrOutput = value;
  34522. }
  34523. forwardMap.set(classPropertyName, inputOrOutput);
  34524. }
  34525. return new ClassPropertyMapping(forwardMap);
  34526. }
  34527. /**
  34528. * Merge two mappings into one, with class properties from `b` taking precedence over class
  34529. * properties from `a`.
  34530. */
  34531. static merge(a, b) {
  34532. const forwardMap = new Map(a.forwardMap.entries());
  34533. for (const [classPropertyName, inputOrOutput] of b.forwardMap) {
  34534. forwardMap.set(classPropertyName, inputOrOutput);
  34535. }
  34536. return new ClassPropertyMapping(forwardMap);
  34537. }
  34538. /**
  34539. * All class property names mapped in this mapping.
  34540. */
  34541. get classPropertyNames() {
  34542. return Array.from(this.forwardMap.keys());
  34543. }
  34544. /**
  34545. * All binding property names mapped in this mapping.
  34546. */
  34547. get propertyNames() {
  34548. return Array.from(this.reverseMap.keys());
  34549. }
  34550. /**
  34551. * Check whether a mapping for the given property name exists.
  34552. */
  34553. hasBindingPropertyName(propertyName) {
  34554. return this.reverseMap.has(propertyName);
  34555. }
  34556. /**
  34557. * Lookup all `InputOrOutput`s that use this `propertyName`.
  34558. */
  34559. getByBindingPropertyName(propertyName) {
  34560. return this.reverseMap.has(propertyName) ? this.reverseMap.get(propertyName) : null;
  34561. }
  34562. /**
  34563. * Lookup the `InputOrOutput` associated with a `classPropertyName`.
  34564. */
  34565. getByClassPropertyName(classPropertyName) {
  34566. return this.forwardMap.has(classPropertyName) ? this.forwardMap.get(classPropertyName) : null;
  34567. }
  34568. /**
  34569. * Convert this mapping to a primitive JS object which maps each class property directly to the
  34570. * binding property name associated with it.
  34571. */
  34572. toDirectMappedObject() {
  34573. const obj = {};
  34574. for (const [classPropertyName, inputOrOutput] of this.forwardMap) {
  34575. obj[classPropertyName] = inputOrOutput.bindingPropertyName;
  34576. }
  34577. return obj;
  34578. }
  34579. /**
  34580. * Convert this mapping to a primitive JS object which maps each class property either to itself
  34581. * (for cases where the binding property name is the same) or to an array which contains both
  34582. * names if they differ.
  34583. *
  34584. * This object format is used when mappings are serialized (for example into .d.ts files).
  34585. * @param transform Function used to transform the values of the generated map.
  34586. */
  34587. toJointMappedObject(transform) {
  34588. const obj = {};
  34589. for (const [classPropertyName, inputOrOutput] of this.forwardMap) {
  34590. obj[classPropertyName] = transform(inputOrOutput);
  34591. }
  34592. return obj;
  34593. }
  34594. /**
  34595. * Implement the iterator protocol and return entry objects which contain the class and binding
  34596. * property names (and are useful for destructuring).
  34597. */
  34598. *[Symbol.iterator]() {
  34599. for (const inputOrOutput of this.forwardMap.values()) {
  34600. yield inputOrOutput;
  34601. }
  34602. }
  34603. }
  34604. function reverseMapFromForwardMap(forwardMap) {
  34605. const reverseMap = new Map();
  34606. for (const [_, inputOrOutput] of forwardMap) {
  34607. if (!reverseMap.has(inputOrOutput.bindingPropertyName)) {
  34608. reverseMap.set(inputOrOutput.bindingPropertyName, []);
  34609. }
  34610. reverseMap.get(inputOrOutput.bindingPropertyName).push(inputOrOutput);
  34611. }
  34612. return reverseMap;
  34613. }
  34614. function extractReferencesFromType(checker, def, bestGuessOwningModule) {
  34615. if (!ts.isTupleTypeNode(def)) {
  34616. return { result: [], isIncomplete: false };
  34617. }
  34618. const result = [];
  34619. let isIncomplete = false;
  34620. for (const element of def.elements) {
  34621. if (!ts.isTypeQueryNode(element)) {
  34622. throw new Error(`Expected TypeQueryNode: ${nodeDebugInfo(element)}`);
  34623. }
  34624. const ref = extraReferenceFromTypeQuery(checker, element, def, bestGuessOwningModule);
  34625. // Note: Sometimes a reference inside the type tuple/array
  34626. // may not be resolvable/existent. We proceed with incomplete data.
  34627. if (ref === null) {
  34628. isIncomplete = true;
  34629. }
  34630. else {
  34631. result.push(ref);
  34632. }
  34633. }
  34634. return { result, isIncomplete };
  34635. }
  34636. function extraReferenceFromTypeQuery(checker, typeNode, origin, bestGuessOwningModule) {
  34637. const type = typeNode.exprName;
  34638. let node;
  34639. let from;
  34640. // Gracefully handle when the type entity could not be converted or
  34641. // resolved to its declaration node.
  34642. try {
  34643. const result = reflectTypeEntityToDeclaration(type, checker);
  34644. node = result.node;
  34645. from = result.from;
  34646. }
  34647. catch (e) {
  34648. if (e instanceof TypeEntityToDeclarationError) {
  34649. return null;
  34650. }
  34651. throw e;
  34652. }
  34653. if (!isNamedClassDeclaration(node)) {
  34654. throw new Error(`Expected named ClassDeclaration: ${nodeDebugInfo(node)}`);
  34655. }
  34656. if (from !== null && !from.startsWith('.')) {
  34657. // The symbol was imported using an absolute module specifier so return a reference that
  34658. // uses that absolute module specifier as its best guess owning module.
  34659. return new Reference(node, {
  34660. specifier: from,
  34661. resolutionContext: origin.getSourceFile().fileName,
  34662. });
  34663. }
  34664. // For local symbols or symbols that were imported using a relative module import it is
  34665. // assumed that the symbol is exported from the provided best guess owning module.
  34666. return new Reference(node, bestGuessOwningModule);
  34667. }
  34668. function readBooleanType(type) {
  34669. if (!ts.isLiteralTypeNode(type)) {
  34670. return null;
  34671. }
  34672. switch (type.literal.kind) {
  34673. case ts.SyntaxKind.TrueKeyword:
  34674. return true;
  34675. case ts.SyntaxKind.FalseKeyword:
  34676. return false;
  34677. default:
  34678. return null;
  34679. }
  34680. }
  34681. function readStringType(type) {
  34682. if (!ts.isLiteralTypeNode(type) || !ts.isStringLiteral(type.literal)) {
  34683. return null;
  34684. }
  34685. return type.literal.text;
  34686. }
  34687. function readMapType(type, valueTransform) {
  34688. if (!ts.isTypeLiteralNode(type)) {
  34689. return {};
  34690. }
  34691. const obj = {};
  34692. type.members.forEach((member) => {
  34693. if (!ts.isPropertySignature(member) ||
  34694. member.type === undefined ||
  34695. member.name === undefined ||
  34696. (!ts.isStringLiteral(member.name) && !ts.isIdentifier(member.name))) {
  34697. return;
  34698. }
  34699. const value = valueTransform(member.type);
  34700. if (value !== null) {
  34701. obj[member.name.text] = value;
  34702. }
  34703. });
  34704. return obj;
  34705. }
  34706. function readStringArrayType(type) {
  34707. if (!ts.isTupleTypeNode(type)) {
  34708. return [];
  34709. }
  34710. const res = [];
  34711. type.elements.forEach((el) => {
  34712. if (!ts.isLiteralTypeNode(el) || !ts.isStringLiteral(el.literal)) {
  34713. return;
  34714. }
  34715. res.push(el.literal.text);
  34716. });
  34717. return res;
  34718. }
  34719. /**
  34720. * Inspects the class' members and extracts the metadata that is used when type-checking templates
  34721. * that use the directive. This metadata does not contain information from a base class, if any,
  34722. * making this metadata invariant to changes of inherited classes.
  34723. */
  34724. function extractDirectiveTypeCheckMeta(node, inputs, reflector) {
  34725. const members = reflector.getMembersOfClass(node);
  34726. const staticMembers = members.filter((member) => member.isStatic);
  34727. const ngTemplateGuards = staticMembers
  34728. .map(extractTemplateGuard)
  34729. .filter((guard) => guard !== null);
  34730. const hasNgTemplateContextGuard = staticMembers.some((member) => member.kind === exports.ClassMemberKind.Method && member.name === 'ngTemplateContextGuard');
  34731. const coercedInputFields = new Set(staticMembers.map(extractCoercedInput).filter((inputName) => {
  34732. // If the input refers to a signal input, we will not respect coercion members.
  34733. // A transform function should be used instead.
  34734. if (inputName === null || inputs.getByClassPropertyName(inputName)?.isSignal) {
  34735. return false;
  34736. }
  34737. return true;
  34738. }));
  34739. const restrictedInputFields = new Set();
  34740. const stringLiteralInputFields = new Set();
  34741. const undeclaredInputFields = new Set();
  34742. for (const { classPropertyName, transform } of inputs) {
  34743. const field = members.find((member) => member.name === classPropertyName);
  34744. if (field === undefined || field.node === null) {
  34745. undeclaredInputFields.add(classPropertyName);
  34746. continue;
  34747. }
  34748. if (isRestricted(field.node)) {
  34749. restrictedInputFields.add(classPropertyName);
  34750. }
  34751. if (field.nameNode !== null && ts.isStringLiteral(field.nameNode)) {
  34752. stringLiteralInputFields.add(classPropertyName);
  34753. }
  34754. if (transform !== null) {
  34755. coercedInputFields.add(classPropertyName);
  34756. }
  34757. }
  34758. const arity = reflector.getGenericArityOfClass(node);
  34759. return {
  34760. hasNgTemplateContextGuard,
  34761. ngTemplateGuards,
  34762. coercedInputFields,
  34763. restrictedInputFields,
  34764. stringLiteralInputFields,
  34765. undeclaredInputFields,
  34766. isGeneric: arity !== null && arity > 0,
  34767. };
  34768. }
  34769. function isRestricted(node) {
  34770. const modifiers = ts.canHaveModifiers(node) ? ts.getModifiers(node) : undefined;
  34771. return (modifiers !== undefined &&
  34772. modifiers.some(({ kind }) => {
  34773. return (kind === ts.SyntaxKind.PrivateKeyword ||
  34774. kind === ts.SyntaxKind.ProtectedKeyword ||
  34775. kind === ts.SyntaxKind.ReadonlyKeyword);
  34776. }));
  34777. }
  34778. function extractTemplateGuard(member) {
  34779. if (!member.name.startsWith('ngTemplateGuard_')) {
  34780. return null;
  34781. }
  34782. const inputName = afterUnderscore(member.name);
  34783. if (member.kind === exports.ClassMemberKind.Property) {
  34784. let type = null;
  34785. if (member.type !== null &&
  34786. ts.isLiteralTypeNode(member.type) &&
  34787. ts.isStringLiteral(member.type.literal)) {
  34788. type = member.type.literal.text;
  34789. }
  34790. // Only property members with string literal type 'binding' are considered as template guard.
  34791. if (type !== 'binding') {
  34792. return null;
  34793. }
  34794. return { inputName, type };
  34795. }
  34796. else if (member.kind === exports.ClassMemberKind.Method) {
  34797. return { inputName, type: 'invocation' };
  34798. }
  34799. else {
  34800. return null;
  34801. }
  34802. }
  34803. function extractCoercedInput(member) {
  34804. if (member.kind !== exports.ClassMemberKind.Property || !member.name.startsWith('ngAcceptInputType_')) {
  34805. return null;
  34806. }
  34807. return afterUnderscore(member.name);
  34808. }
  34809. /**
  34810. * A `MetadataReader` that reads from an ordered set of child readers until it obtains the requested
  34811. * metadata.
  34812. *
  34813. * This is used to combine `MetadataReader`s that read from different sources (e.g. from a registry
  34814. * and from .d.ts files).
  34815. */
  34816. class CompoundMetadataReader {
  34817. readers;
  34818. constructor(readers) {
  34819. this.readers = readers;
  34820. }
  34821. getDirectiveMetadata(node) {
  34822. for (const reader of this.readers) {
  34823. const meta = reader.getDirectiveMetadata(node);
  34824. if (meta !== null) {
  34825. return meta;
  34826. }
  34827. }
  34828. return null;
  34829. }
  34830. getNgModuleMetadata(node) {
  34831. for (const reader of this.readers) {
  34832. const meta = reader.getNgModuleMetadata(node);
  34833. if (meta !== null) {
  34834. return meta;
  34835. }
  34836. }
  34837. return null;
  34838. }
  34839. getPipeMetadata(node) {
  34840. for (const reader of this.readers) {
  34841. const meta = reader.getPipeMetadata(node);
  34842. if (meta !== null) {
  34843. return meta;
  34844. }
  34845. }
  34846. return null;
  34847. }
  34848. }
  34849. function afterUnderscore(str) {
  34850. const pos = str.indexOf('_');
  34851. if (pos === -1) {
  34852. throw new Error(`Expected '${str}' to contain '_'`);
  34853. }
  34854. return str.slice(pos + 1);
  34855. }
  34856. /** Returns whether a class declaration has the necessary class fields to make it injectable. */
  34857. function hasInjectableFields(clazz, host) {
  34858. const members = host.getMembersOfClass(clazz);
  34859. return members.some(({ isStatic, name }) => isStatic && (name === 'ɵprov' || name === 'ɵfac'));
  34860. }
  34861. function isHostDirectiveMetaForGlobalMode(hostDirectiveMeta) {
  34862. return hostDirectiveMeta.directive instanceof Reference;
  34863. }
  34864. /**
  34865. * Given a reference to a directive, return a flattened version of its `DirectiveMeta` metadata
  34866. * which includes metadata from its entire inheritance chain.
  34867. *
  34868. * The returned `DirectiveMeta` will either have `baseClass: null` if the inheritance chain could be
  34869. * fully resolved, or `baseClass: 'dynamic'` if the inheritance chain could not be completely
  34870. * followed.
  34871. */
  34872. function flattenInheritedDirectiveMetadata(reader, dir) {
  34873. const topMeta = reader.getDirectiveMetadata(dir);
  34874. if (topMeta === null) {
  34875. return null;
  34876. }
  34877. if (topMeta.baseClass === null) {
  34878. return topMeta;
  34879. }
  34880. const coercedInputFields = new Set();
  34881. const undeclaredInputFields = new Set();
  34882. const restrictedInputFields = new Set();
  34883. const stringLiteralInputFields = new Set();
  34884. let hostDirectives = null;
  34885. let isDynamic = false;
  34886. let inputs = ClassPropertyMapping.empty();
  34887. let outputs = ClassPropertyMapping.empty();
  34888. let isStructural = false;
  34889. const addMetadata = (meta) => {
  34890. if (meta.baseClass === 'dynamic') {
  34891. isDynamic = true;
  34892. }
  34893. else if (meta.baseClass !== null) {
  34894. const baseMeta = reader.getDirectiveMetadata(meta.baseClass);
  34895. if (baseMeta !== null) {
  34896. addMetadata(baseMeta);
  34897. }
  34898. else {
  34899. // Missing metadata for the base class means it's effectively dynamic.
  34900. isDynamic = true;
  34901. }
  34902. }
  34903. isStructural = isStructural || meta.isStructural;
  34904. inputs = ClassPropertyMapping.merge(inputs, meta.inputs);
  34905. outputs = ClassPropertyMapping.merge(outputs, meta.outputs);
  34906. for (const coercedInputField of meta.coercedInputFields) {
  34907. coercedInputFields.add(coercedInputField);
  34908. }
  34909. for (const undeclaredInputField of meta.undeclaredInputFields) {
  34910. undeclaredInputFields.add(undeclaredInputField);
  34911. }
  34912. for (const restrictedInputField of meta.restrictedInputFields) {
  34913. restrictedInputFields.add(restrictedInputField);
  34914. }
  34915. for (const field of meta.stringLiteralInputFields) {
  34916. stringLiteralInputFields.add(field);
  34917. }
  34918. if (meta.hostDirectives !== null && meta.hostDirectives.length > 0) {
  34919. hostDirectives ??= [];
  34920. hostDirectives.push(...meta.hostDirectives);
  34921. }
  34922. };
  34923. addMetadata(topMeta);
  34924. return {
  34925. ...topMeta,
  34926. inputs,
  34927. outputs,
  34928. coercedInputFields,
  34929. undeclaredInputFields,
  34930. restrictedInputFields,
  34931. stringLiteralInputFields,
  34932. baseClass: isDynamic ? 'dynamic' : null,
  34933. isStructural,
  34934. hostDirectives,
  34935. };
  34936. }
  34937. /**
  34938. * Represents a value which cannot be determined statically.
  34939. */
  34940. class DynamicValue {
  34941. node;
  34942. reason;
  34943. code;
  34944. constructor(node, reason, code) {
  34945. this.node = node;
  34946. this.reason = reason;
  34947. this.code = code;
  34948. }
  34949. static fromDynamicInput(node, input) {
  34950. return new DynamicValue(node, input, 0 /* DynamicValueReason.DYNAMIC_INPUT */);
  34951. }
  34952. static fromDynamicString(node) {
  34953. return new DynamicValue(node, undefined, 1 /* DynamicValueReason.DYNAMIC_STRING */);
  34954. }
  34955. static fromExternalReference(node, ref) {
  34956. return new DynamicValue(node, ref, 2 /* DynamicValueReason.EXTERNAL_REFERENCE */);
  34957. }
  34958. static fromUnsupportedSyntax(node) {
  34959. return new DynamicValue(node, undefined, 3 /* DynamicValueReason.UNSUPPORTED_SYNTAX */);
  34960. }
  34961. static fromUnknownIdentifier(node) {
  34962. return new DynamicValue(node, undefined, 4 /* DynamicValueReason.UNKNOWN_IDENTIFIER */);
  34963. }
  34964. static fromInvalidExpressionType(node, value) {
  34965. return new DynamicValue(node, value, 5 /* DynamicValueReason.INVALID_EXPRESSION_TYPE */);
  34966. }
  34967. static fromComplexFunctionCall(node, fn) {
  34968. return new DynamicValue(node, fn, 6 /* DynamicValueReason.COMPLEX_FUNCTION_CALL */);
  34969. }
  34970. static fromDynamicType(node) {
  34971. return new DynamicValue(node, undefined, 7 /* DynamicValueReason.DYNAMIC_TYPE */);
  34972. }
  34973. static fromSyntheticInput(node, value) {
  34974. return new DynamicValue(node, value, 8 /* DynamicValueReason.SYNTHETIC_INPUT */);
  34975. }
  34976. static fromUnknown(node) {
  34977. return new DynamicValue(node, undefined, 9 /* DynamicValueReason.UNKNOWN */);
  34978. }
  34979. isFromDynamicInput() {
  34980. return this.code === 0 /* DynamicValueReason.DYNAMIC_INPUT */;
  34981. }
  34982. isFromDynamicString() {
  34983. return this.code === 1 /* DynamicValueReason.DYNAMIC_STRING */;
  34984. }
  34985. isFromExternalReference() {
  34986. return this.code === 2 /* DynamicValueReason.EXTERNAL_REFERENCE */;
  34987. }
  34988. isFromUnsupportedSyntax() {
  34989. return this.code === 3 /* DynamicValueReason.UNSUPPORTED_SYNTAX */;
  34990. }
  34991. isFromUnknownIdentifier() {
  34992. return this.code === 4 /* DynamicValueReason.UNKNOWN_IDENTIFIER */;
  34993. }
  34994. isFromInvalidExpressionType() {
  34995. return this.code === 5 /* DynamicValueReason.INVALID_EXPRESSION_TYPE */;
  34996. }
  34997. isFromComplexFunctionCall() {
  34998. return this.code === 6 /* DynamicValueReason.COMPLEX_FUNCTION_CALL */;
  34999. }
  35000. isFromDynamicType() {
  35001. return this.code === 7 /* DynamicValueReason.DYNAMIC_TYPE */;
  35002. }
  35003. isFromUnknown() {
  35004. return this.code === 9 /* DynamicValueReason.UNKNOWN */;
  35005. }
  35006. accept(visitor) {
  35007. switch (this.code) {
  35008. case 0 /* DynamicValueReason.DYNAMIC_INPUT */:
  35009. return visitor.visitDynamicInput(this);
  35010. case 1 /* DynamicValueReason.DYNAMIC_STRING */:
  35011. return visitor.visitDynamicString(this);
  35012. case 2 /* DynamicValueReason.EXTERNAL_REFERENCE */:
  35013. return visitor.visitExternalReference(this);
  35014. case 3 /* DynamicValueReason.UNSUPPORTED_SYNTAX */:
  35015. return visitor.visitUnsupportedSyntax(this);
  35016. case 4 /* DynamicValueReason.UNKNOWN_IDENTIFIER */:
  35017. return visitor.visitUnknownIdentifier(this);
  35018. case 5 /* DynamicValueReason.INVALID_EXPRESSION_TYPE */:
  35019. return visitor.visitInvalidExpressionType(this);
  35020. case 6 /* DynamicValueReason.COMPLEX_FUNCTION_CALL */:
  35021. return visitor.visitComplexFunctionCall(this);
  35022. case 7 /* DynamicValueReason.DYNAMIC_TYPE */:
  35023. return visitor.visitDynamicType(this);
  35024. case 8 /* DynamicValueReason.SYNTHETIC_INPUT */:
  35025. return visitor.visitSyntheticInput(this);
  35026. case 9 /* DynamicValueReason.UNKNOWN */:
  35027. return visitor.visitUnknown(this);
  35028. }
  35029. }
  35030. }
  35031. /**
  35032. * A collection of publicly exported declarations from a module. Each declaration is evaluated
  35033. * lazily upon request.
  35034. */
  35035. class ResolvedModule {
  35036. exports;
  35037. evaluate;
  35038. constructor(exports, evaluate) {
  35039. this.exports = exports;
  35040. this.evaluate = evaluate;
  35041. }
  35042. getExport(name) {
  35043. if (!this.exports.has(name)) {
  35044. return undefined;
  35045. }
  35046. return this.evaluate(this.exports.get(name));
  35047. }
  35048. getExports() {
  35049. const map = new Map();
  35050. this.exports.forEach((decl, name) => {
  35051. map.set(name, this.evaluate(decl));
  35052. });
  35053. return map;
  35054. }
  35055. }
  35056. /**
  35057. * A value member of an enumeration.
  35058. *
  35059. * Contains a `Reference` to the enumeration itself, and the name of the referenced member.
  35060. */
  35061. class EnumValue {
  35062. enumRef;
  35063. name;
  35064. resolved;
  35065. constructor(enumRef, name, resolved) {
  35066. this.enumRef = enumRef;
  35067. this.name = name;
  35068. this.resolved = resolved;
  35069. }
  35070. }
  35071. /**
  35072. * An implementation of a known function that can be statically evaluated.
  35073. * It could be a built-in function or method (such as `Array.prototype.slice`) or a TypeScript
  35074. * helper (such as `__spread`).
  35075. */
  35076. class KnownFn {
  35077. }
  35078. /**
  35079. * Derives a type representation from a resolved value to be reported in a diagnostic.
  35080. *
  35081. * @param value The resolved value for which a type representation should be derived.
  35082. * @param maxDepth The maximum nesting depth of objects and arrays, defaults to 1 level.
  35083. */
  35084. function describeResolvedType(value, maxDepth = 1) {
  35085. if (value === null) {
  35086. return 'null';
  35087. }
  35088. else if (value === undefined) {
  35089. return 'undefined';
  35090. }
  35091. else if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'string') {
  35092. return typeof value;
  35093. }
  35094. else if (value instanceof Map) {
  35095. if (maxDepth === 0) {
  35096. return 'object';
  35097. }
  35098. const entries = Array.from(value.entries()).map(([key, v]) => {
  35099. return `${quoteKey(key)}: ${describeResolvedType(v, maxDepth - 1)}`;
  35100. });
  35101. return entries.length > 0 ? `{ ${entries.join('; ')} }` : '{}';
  35102. }
  35103. else if (value instanceof ResolvedModule) {
  35104. return '(module)';
  35105. }
  35106. else if (value instanceof EnumValue) {
  35107. return value.enumRef.debugName ?? '(anonymous)';
  35108. }
  35109. else if (value instanceof Reference) {
  35110. return value.debugName ?? '(anonymous)';
  35111. }
  35112. else if (Array.isArray(value)) {
  35113. if (maxDepth === 0) {
  35114. return 'Array';
  35115. }
  35116. return `[${value.map((v) => describeResolvedType(v, maxDepth - 1)).join(', ')}]`;
  35117. }
  35118. else if (value instanceof DynamicValue) {
  35119. return '(not statically analyzable)';
  35120. }
  35121. else if (value instanceof KnownFn) {
  35122. return 'Function';
  35123. }
  35124. else {
  35125. return 'unknown';
  35126. }
  35127. }
  35128. function quoteKey(key) {
  35129. if (/^[a-z0-9_]+$/i.test(key)) {
  35130. return key;
  35131. }
  35132. else {
  35133. return `'${key.replace(/'/g, "\\'")}'`;
  35134. }
  35135. }
  35136. /**
  35137. * Creates an array of related information diagnostics for a `DynamicValue` that describe the trace
  35138. * of why an expression was evaluated as dynamic.
  35139. *
  35140. * @param node The node for which a `ts.Diagnostic` is to be created with the trace.
  35141. * @param value The dynamic value for which a trace should be created.
  35142. */
  35143. function traceDynamicValue(node, value) {
  35144. return value.accept(new TraceDynamicValueVisitor(node));
  35145. }
  35146. class TraceDynamicValueVisitor {
  35147. node;
  35148. currentContainerNode = null;
  35149. constructor(node) {
  35150. this.node = node;
  35151. }
  35152. visitDynamicInput(value) {
  35153. const trace = value.reason.accept(this);
  35154. if (this.shouldTrace(value.node)) {
  35155. const info = makeRelatedInformation(value.node, 'Unable to evaluate this expression statically.');
  35156. trace.unshift(info);
  35157. }
  35158. return trace;
  35159. }
  35160. visitSyntheticInput(value) {
  35161. return [makeRelatedInformation(value.node, 'Unable to evaluate this expression further.')];
  35162. }
  35163. visitDynamicString(value) {
  35164. return [
  35165. makeRelatedInformation(value.node, 'A string value could not be determined statically.'),
  35166. ];
  35167. }
  35168. visitExternalReference(value) {
  35169. const name = value.reason.debugName;
  35170. const description = name !== null ? `'${name}'` : 'an anonymous declaration';
  35171. return [
  35172. makeRelatedInformation(value.node, `A value for ${description} cannot be determined statically, as it is an external declaration.`),
  35173. ];
  35174. }
  35175. visitComplexFunctionCall(value) {
  35176. return [
  35177. makeRelatedInformation(value.node, 'Unable to evaluate function call of complex function. A function must have exactly one return statement.'),
  35178. makeRelatedInformation(value.reason.node, 'Function is declared here.'),
  35179. ];
  35180. }
  35181. visitInvalidExpressionType(value) {
  35182. return [makeRelatedInformation(value.node, 'Unable to evaluate an invalid expression.')];
  35183. }
  35184. visitUnknown(value) {
  35185. return [makeRelatedInformation(value.node, 'Unable to evaluate statically.')];
  35186. }
  35187. visitUnknownIdentifier(value) {
  35188. return [makeRelatedInformation(value.node, 'Unknown reference.')];
  35189. }
  35190. visitDynamicType(value) {
  35191. return [makeRelatedInformation(value.node, 'Dynamic type.')];
  35192. }
  35193. visitUnsupportedSyntax(value) {
  35194. return [makeRelatedInformation(value.node, 'This syntax is not supported.')];
  35195. }
  35196. /**
  35197. * Determines whether the dynamic value reported for the node should be traced, i.e. if it is not
  35198. * part of the container for which the most recent trace was created.
  35199. */
  35200. shouldTrace(node) {
  35201. if (node === this.node) {
  35202. // Do not include a dynamic value for the origin node, as the main diagnostic is already
  35203. // reported on that node.
  35204. return false;
  35205. }
  35206. const container = getContainerNode(node);
  35207. if (container === this.currentContainerNode) {
  35208. // The node is part of the same container as the previous trace entry, so this dynamic value
  35209. // should not become part of the trace.
  35210. return false;
  35211. }
  35212. this.currentContainerNode = container;
  35213. return true;
  35214. }
  35215. }
  35216. /**
  35217. * Determines the closest parent node that is to be considered as container, which is used to reduce
  35218. * the granularity of tracing the dynamic values to a single entry per container. Currently, full
  35219. * statements and destructuring patterns are considered as container.
  35220. */
  35221. function getContainerNode(node) {
  35222. let currentNode = node;
  35223. while (currentNode !== undefined) {
  35224. switch (currentNode.kind) {
  35225. case ts.SyntaxKind.ExpressionStatement:
  35226. case ts.SyntaxKind.VariableStatement:
  35227. case ts.SyntaxKind.ReturnStatement:
  35228. case ts.SyntaxKind.IfStatement:
  35229. case ts.SyntaxKind.SwitchStatement:
  35230. case ts.SyntaxKind.DoStatement:
  35231. case ts.SyntaxKind.WhileStatement:
  35232. case ts.SyntaxKind.ForStatement:
  35233. case ts.SyntaxKind.ForInStatement:
  35234. case ts.SyntaxKind.ForOfStatement:
  35235. case ts.SyntaxKind.ContinueStatement:
  35236. case ts.SyntaxKind.BreakStatement:
  35237. case ts.SyntaxKind.ThrowStatement:
  35238. case ts.SyntaxKind.ObjectBindingPattern:
  35239. case ts.SyntaxKind.ArrayBindingPattern:
  35240. return currentNode;
  35241. }
  35242. currentNode = currentNode.parent;
  35243. }
  35244. return node.getSourceFile();
  35245. }
  35246. class ArraySliceBuiltinFn extends KnownFn {
  35247. lhs;
  35248. constructor(lhs) {
  35249. super();
  35250. this.lhs = lhs;
  35251. }
  35252. evaluate(node, args) {
  35253. if (args.length === 0) {
  35254. return this.lhs;
  35255. }
  35256. else {
  35257. return DynamicValue.fromUnknown(node);
  35258. }
  35259. }
  35260. }
  35261. class ArrayConcatBuiltinFn extends KnownFn {
  35262. lhs;
  35263. constructor(lhs) {
  35264. super();
  35265. this.lhs = lhs;
  35266. }
  35267. evaluate(node, args) {
  35268. const result = [...this.lhs];
  35269. for (const arg of args) {
  35270. if (arg instanceof DynamicValue) {
  35271. result.push(DynamicValue.fromDynamicInput(node, arg));
  35272. }
  35273. else if (Array.isArray(arg)) {
  35274. result.push(...arg);
  35275. }
  35276. else {
  35277. result.push(arg);
  35278. }
  35279. }
  35280. return result;
  35281. }
  35282. }
  35283. class StringConcatBuiltinFn extends KnownFn {
  35284. lhs;
  35285. constructor(lhs) {
  35286. super();
  35287. this.lhs = lhs;
  35288. }
  35289. evaluate(node, args) {
  35290. let result = this.lhs;
  35291. for (const arg of args) {
  35292. const resolved = arg instanceof EnumValue ? arg.resolved : arg;
  35293. if (typeof resolved === 'string' ||
  35294. typeof resolved === 'number' ||
  35295. typeof resolved === 'boolean' ||
  35296. resolved == null) {
  35297. // Cast to `any`, because `concat` will convert
  35298. // anything to a string, but TS only allows strings.
  35299. result = result.concat(resolved);
  35300. }
  35301. else {
  35302. return DynamicValue.fromUnknown(node);
  35303. }
  35304. }
  35305. return result;
  35306. }
  35307. }
  35308. /**
  35309. * A value produced which originated in a `ForeignFunctionResolver` and doesn't come from the
  35310. * template itself.
  35311. *
  35312. * Synthetic values cannot be further evaluated, and attempts to do so produce `DynamicValue`s
  35313. * instead.
  35314. */
  35315. class SyntheticValue {
  35316. value;
  35317. constructor(value) {
  35318. this.value = value;
  35319. }
  35320. }
  35321. function literalBinaryOp(op) {
  35322. return { op, literal: true };
  35323. }
  35324. function referenceBinaryOp(op) {
  35325. return { op, literal: false };
  35326. }
  35327. const BINARY_OPERATORS$2 = new Map([
  35328. [ts.SyntaxKind.PlusToken, literalBinaryOp((a, b) => a + b)],
  35329. [ts.SyntaxKind.MinusToken, literalBinaryOp((a, b) => a - b)],
  35330. [ts.SyntaxKind.AsteriskToken, literalBinaryOp((a, b) => a * b)],
  35331. [ts.SyntaxKind.SlashToken, literalBinaryOp((a, b) => a / b)],
  35332. [ts.SyntaxKind.PercentToken, literalBinaryOp((a, b) => a % b)],
  35333. [ts.SyntaxKind.AmpersandToken, literalBinaryOp((a, b) => a & b)],
  35334. [ts.SyntaxKind.BarToken, literalBinaryOp((a, b) => a | b)],
  35335. [ts.SyntaxKind.CaretToken, literalBinaryOp((a, b) => a ^ b)],
  35336. [ts.SyntaxKind.LessThanToken, literalBinaryOp((a, b) => a < b)],
  35337. [ts.SyntaxKind.LessThanEqualsToken, literalBinaryOp((a, b) => a <= b)],
  35338. [ts.SyntaxKind.GreaterThanToken, literalBinaryOp((a, b) => a > b)],
  35339. [ts.SyntaxKind.GreaterThanEqualsToken, literalBinaryOp((a, b) => a >= b)],
  35340. [ts.SyntaxKind.EqualsEqualsToken, literalBinaryOp((a, b) => a == b)],
  35341. [ts.SyntaxKind.EqualsEqualsEqualsToken, literalBinaryOp((a, b) => a === b)],
  35342. [ts.SyntaxKind.ExclamationEqualsToken, literalBinaryOp((a, b) => a != b)],
  35343. [ts.SyntaxKind.ExclamationEqualsEqualsToken, literalBinaryOp((a, b) => a !== b)],
  35344. [ts.SyntaxKind.LessThanLessThanToken, literalBinaryOp((a, b) => a << b)],
  35345. [ts.SyntaxKind.GreaterThanGreaterThanToken, literalBinaryOp((a, b) => a >> b)],
  35346. [ts.SyntaxKind.GreaterThanGreaterThanGreaterThanToken, literalBinaryOp((a, b) => a >>> b)],
  35347. [ts.SyntaxKind.AsteriskAsteriskToken, literalBinaryOp((a, b) => Math.pow(a, b))],
  35348. [ts.SyntaxKind.AmpersandAmpersandToken, referenceBinaryOp((a, b) => a && b)],
  35349. [ts.SyntaxKind.BarBarToken, referenceBinaryOp((a, b) => a || b)],
  35350. ]);
  35351. const UNARY_OPERATORS$2 = new Map([
  35352. [ts.SyntaxKind.TildeToken, (a) => ~a],
  35353. [ts.SyntaxKind.MinusToken, (a) => -a],
  35354. [ts.SyntaxKind.PlusToken, (a) => +a],
  35355. [ts.SyntaxKind.ExclamationToken, (a) => !a],
  35356. ]);
  35357. class StaticInterpreter {
  35358. host;
  35359. checker;
  35360. dependencyTracker;
  35361. constructor(host, checker, dependencyTracker) {
  35362. this.host = host;
  35363. this.checker = checker;
  35364. this.dependencyTracker = dependencyTracker;
  35365. }
  35366. visit(node, context) {
  35367. return this.visitExpression(node, context);
  35368. }
  35369. visitExpression(node, context) {
  35370. let result;
  35371. if (node.kind === ts.SyntaxKind.TrueKeyword) {
  35372. return true;
  35373. }
  35374. else if (node.kind === ts.SyntaxKind.FalseKeyword) {
  35375. return false;
  35376. }
  35377. else if (node.kind === ts.SyntaxKind.NullKeyword) {
  35378. return null;
  35379. }
  35380. else if (ts.isStringLiteral(node)) {
  35381. return node.text;
  35382. }
  35383. else if (ts.isNoSubstitutionTemplateLiteral(node)) {
  35384. return node.text;
  35385. }
  35386. else if (ts.isTemplateExpression(node)) {
  35387. result = this.visitTemplateExpression(node, context);
  35388. }
  35389. else if (ts.isNumericLiteral(node)) {
  35390. return parseFloat(node.text);
  35391. }
  35392. else if (ts.isObjectLiteralExpression(node)) {
  35393. result = this.visitObjectLiteralExpression(node, context);
  35394. }
  35395. else if (ts.isIdentifier(node)) {
  35396. result = this.visitIdentifier(node, context);
  35397. }
  35398. else if (ts.isPropertyAccessExpression(node)) {
  35399. result = this.visitPropertyAccessExpression(node, context);
  35400. }
  35401. else if (ts.isCallExpression(node)) {
  35402. result = this.visitCallExpression(node, context);
  35403. }
  35404. else if (ts.isConditionalExpression(node)) {
  35405. result = this.visitConditionalExpression(node, context);
  35406. }
  35407. else if (ts.isPrefixUnaryExpression(node)) {
  35408. result = this.visitPrefixUnaryExpression(node, context);
  35409. }
  35410. else if (ts.isBinaryExpression(node)) {
  35411. result = this.visitBinaryExpression(node, context);
  35412. }
  35413. else if (ts.isArrayLiteralExpression(node)) {
  35414. result = this.visitArrayLiteralExpression(node, context);
  35415. }
  35416. else if (ts.isParenthesizedExpression(node)) {
  35417. result = this.visitParenthesizedExpression(node, context);
  35418. }
  35419. else if (ts.isElementAccessExpression(node)) {
  35420. result = this.visitElementAccessExpression(node, context);
  35421. }
  35422. else if (ts.isAsExpression(node)) {
  35423. result = this.visitExpression(node.expression, context);
  35424. }
  35425. else if (ts.isNonNullExpression(node)) {
  35426. result = this.visitExpression(node.expression, context);
  35427. }
  35428. else if (this.host.isClass(node)) {
  35429. result = this.visitDeclaration(node, context);
  35430. }
  35431. else {
  35432. return DynamicValue.fromUnsupportedSyntax(node);
  35433. }
  35434. if (result instanceof DynamicValue && result.node !== node) {
  35435. return DynamicValue.fromDynamicInput(node, result);
  35436. }
  35437. return result;
  35438. }
  35439. visitArrayLiteralExpression(node, context) {
  35440. const array = [];
  35441. for (let i = 0; i < node.elements.length; i++) {
  35442. const element = node.elements[i];
  35443. if (ts.isSpreadElement(element)) {
  35444. array.push(...this.visitSpreadElement(element, context));
  35445. }
  35446. else {
  35447. array.push(this.visitExpression(element, context));
  35448. }
  35449. }
  35450. return array;
  35451. }
  35452. visitObjectLiteralExpression(node, context) {
  35453. const map = new Map();
  35454. for (let i = 0; i < node.properties.length; i++) {
  35455. const property = node.properties[i];
  35456. if (ts.isPropertyAssignment(property)) {
  35457. const name = this.stringNameFromPropertyName(property.name, context);
  35458. // Check whether the name can be determined statically.
  35459. if (name === undefined) {
  35460. return DynamicValue.fromDynamicInput(node, DynamicValue.fromDynamicString(property.name));
  35461. }
  35462. map.set(name, this.visitExpression(property.initializer, context));
  35463. }
  35464. else if (ts.isShorthandPropertyAssignment(property)) {
  35465. const symbol = this.checker.getShorthandAssignmentValueSymbol(property);
  35466. if (symbol === undefined || symbol.valueDeclaration === undefined) {
  35467. map.set(property.name.text, DynamicValue.fromUnknown(property));
  35468. }
  35469. else {
  35470. map.set(property.name.text, this.visitDeclaration(symbol.valueDeclaration, context));
  35471. }
  35472. }
  35473. else if (ts.isSpreadAssignment(property)) {
  35474. const spread = this.visitExpression(property.expression, context);
  35475. if (spread instanceof DynamicValue) {
  35476. return DynamicValue.fromDynamicInput(node, spread);
  35477. }
  35478. else if (spread instanceof Map) {
  35479. spread.forEach((value, key) => map.set(key, value));
  35480. }
  35481. else if (spread instanceof ResolvedModule) {
  35482. spread.getExports().forEach((value, key) => map.set(key, value));
  35483. }
  35484. else {
  35485. return DynamicValue.fromDynamicInput(node, DynamicValue.fromInvalidExpressionType(property, spread));
  35486. }
  35487. }
  35488. else {
  35489. return DynamicValue.fromUnknown(node);
  35490. }
  35491. }
  35492. return map;
  35493. }
  35494. visitTemplateExpression(node, context) {
  35495. const pieces = [node.head.text];
  35496. for (let i = 0; i < node.templateSpans.length; i++) {
  35497. const span = node.templateSpans[i];
  35498. const value = literal(this.visit(span.expression, context), () => DynamicValue.fromDynamicString(span.expression));
  35499. if (value instanceof DynamicValue) {
  35500. return DynamicValue.fromDynamicInput(node, value);
  35501. }
  35502. pieces.push(`${value}`, span.literal.text);
  35503. }
  35504. return pieces.join('');
  35505. }
  35506. visitIdentifier(node, context) {
  35507. const decl = this.host.getDeclarationOfIdentifier(node);
  35508. if (decl === null) {
  35509. if (ts.identifierToKeywordKind(node) === ts.SyntaxKind.UndefinedKeyword) {
  35510. return undefined;
  35511. }
  35512. else {
  35513. // Check if the symbol here is imported.
  35514. if (this.dependencyTracker !== null && this.host.getImportOfIdentifier(node) !== null) {
  35515. // It was, but no declaration for the node could be found. This means that the dependency
  35516. // graph for the current file cannot be properly updated to account for this (broken)
  35517. // import. Instead, the originating file is reported as failing dependency analysis,
  35518. // ensuring that future compilations will always attempt to re-resolve the previously
  35519. // broken identifier.
  35520. this.dependencyTracker.recordDependencyAnalysisFailure(context.originatingFile);
  35521. }
  35522. return DynamicValue.fromUnknownIdentifier(node);
  35523. }
  35524. }
  35525. const declContext = { ...context, ...joinModuleContext(context, node, decl) };
  35526. const result = this.visitDeclaration(decl.node, declContext);
  35527. if (result instanceof Reference) {
  35528. // Only record identifiers to non-synthetic references. Synthetic references may not have the
  35529. // same value at runtime as they do at compile time, so it's not legal to refer to them by the
  35530. // identifier here.
  35531. if (!result.synthetic) {
  35532. result.addIdentifier(node);
  35533. }
  35534. }
  35535. else if (result instanceof DynamicValue) {
  35536. return DynamicValue.fromDynamicInput(node, result);
  35537. }
  35538. return result;
  35539. }
  35540. visitDeclaration(node, context) {
  35541. if (this.dependencyTracker !== null) {
  35542. this.dependencyTracker.addDependency(context.originatingFile, node.getSourceFile());
  35543. }
  35544. if (this.host.isClass(node)) {
  35545. return this.getReference(node, context);
  35546. }
  35547. else if (ts.isVariableDeclaration(node)) {
  35548. return this.visitVariableDeclaration(node, context);
  35549. }
  35550. else if (ts.isParameter(node) && context.scope.has(node)) {
  35551. return context.scope.get(node);
  35552. }
  35553. else if (ts.isExportAssignment(node)) {
  35554. return this.visitExpression(node.expression, context);
  35555. }
  35556. else if (ts.isEnumDeclaration(node)) {
  35557. return this.visitEnumDeclaration(node, context);
  35558. }
  35559. else if (ts.isSourceFile(node)) {
  35560. return this.visitSourceFile(node, context);
  35561. }
  35562. else if (ts.isBindingElement(node)) {
  35563. return this.visitBindingElement(node, context);
  35564. }
  35565. else {
  35566. return this.getReference(node, context);
  35567. }
  35568. }
  35569. visitVariableDeclaration(node, context) {
  35570. const value = this.host.getVariableValue(node);
  35571. if (value !== null) {
  35572. return this.visitExpression(value, context);
  35573. }
  35574. else if (isVariableDeclarationDeclared(node)) {
  35575. // If the declaration has a literal type that can be statically reduced to a value, resolve to
  35576. // that value. If not, the historical behavior for variable declarations is to return a
  35577. // `Reference` to the variable, as the consumer could use it in a context where knowing its
  35578. // static value is not necessary.
  35579. //
  35580. // Arguably, since the value cannot be statically determined, we should return a
  35581. // `DynamicValue`. This returns a `Reference` because it's the same behavior as before
  35582. // `visitType` was introduced.
  35583. //
  35584. // TODO(zarend): investigate switching to a `DynamicValue` and verify this won't break any
  35585. // use cases, especially in ngcc
  35586. if (node.type !== undefined) {
  35587. const evaluatedType = this.visitType(node.type, context);
  35588. if (!(evaluatedType instanceof DynamicValue)) {
  35589. return evaluatedType;
  35590. }
  35591. }
  35592. return this.getReference(node, context);
  35593. }
  35594. else {
  35595. return undefined;
  35596. }
  35597. }
  35598. visitEnumDeclaration(node, context) {
  35599. const enumRef = this.getReference(node, context);
  35600. const map = new Map();
  35601. node.members.forEach((member, index) => {
  35602. const name = this.stringNameFromPropertyName(member.name, context);
  35603. if (name !== undefined) {
  35604. const resolved = member.initializer ? this.visit(member.initializer, context) : index;
  35605. map.set(name, new EnumValue(enumRef, name, resolved));
  35606. }
  35607. });
  35608. return map;
  35609. }
  35610. visitElementAccessExpression(node, context) {
  35611. const lhs = this.visitExpression(node.expression, context);
  35612. if (lhs instanceof DynamicValue) {
  35613. return DynamicValue.fromDynamicInput(node, lhs);
  35614. }
  35615. const rhs = this.visitExpression(node.argumentExpression, context);
  35616. if (rhs instanceof DynamicValue) {
  35617. return DynamicValue.fromDynamicInput(node, rhs);
  35618. }
  35619. if (typeof rhs !== 'string' && typeof rhs !== 'number') {
  35620. return DynamicValue.fromInvalidExpressionType(node, rhs);
  35621. }
  35622. return this.accessHelper(node, lhs, rhs, context);
  35623. }
  35624. visitPropertyAccessExpression(node, context) {
  35625. const lhs = this.visitExpression(node.expression, context);
  35626. const rhs = node.name.text;
  35627. // TODO: handle reference to class declaration.
  35628. if (lhs instanceof DynamicValue) {
  35629. return DynamicValue.fromDynamicInput(node, lhs);
  35630. }
  35631. return this.accessHelper(node, lhs, rhs, context);
  35632. }
  35633. visitSourceFile(node, context) {
  35634. const declarations = this.host.getExportsOfModule(node);
  35635. if (declarations === null) {
  35636. return DynamicValue.fromUnknown(node);
  35637. }
  35638. return new ResolvedModule(declarations, (decl) => {
  35639. const declContext = {
  35640. ...context,
  35641. ...joinModuleContext(context, node, decl),
  35642. };
  35643. // Visit both concrete and inline declarations.
  35644. return this.visitDeclaration(decl.node, declContext);
  35645. });
  35646. }
  35647. accessHelper(node, lhs, rhs, context) {
  35648. const strIndex = `${rhs}`;
  35649. if (lhs instanceof Map) {
  35650. if (lhs.has(strIndex)) {
  35651. return lhs.get(strIndex);
  35652. }
  35653. else {
  35654. return undefined;
  35655. }
  35656. }
  35657. else if (lhs instanceof ResolvedModule) {
  35658. return lhs.getExport(strIndex);
  35659. }
  35660. else if (Array.isArray(lhs)) {
  35661. if (rhs === 'length') {
  35662. return lhs.length;
  35663. }
  35664. else if (rhs === 'slice') {
  35665. return new ArraySliceBuiltinFn(lhs);
  35666. }
  35667. else if (rhs === 'concat') {
  35668. return new ArrayConcatBuiltinFn(lhs);
  35669. }
  35670. if (typeof rhs !== 'number' || !Number.isInteger(rhs)) {
  35671. return DynamicValue.fromInvalidExpressionType(node, rhs);
  35672. }
  35673. return lhs[rhs];
  35674. }
  35675. else if (typeof lhs === 'string' && rhs === 'concat') {
  35676. return new StringConcatBuiltinFn(lhs);
  35677. }
  35678. else if (lhs instanceof Reference) {
  35679. const ref = lhs.node;
  35680. if (this.host.isClass(ref)) {
  35681. const module = owningModule(context, lhs.bestGuessOwningModule);
  35682. let value = undefined;
  35683. const member = this.host
  35684. .getMembersOfClass(ref)
  35685. .find((member) => member.isStatic && member.name === strIndex);
  35686. if (member !== undefined) {
  35687. if (member.value !== null) {
  35688. value = this.visitExpression(member.value, context);
  35689. }
  35690. else if (member.implementation !== null) {
  35691. value = new Reference(member.implementation, module);
  35692. }
  35693. else if (member.node) {
  35694. value = new Reference(member.node, module);
  35695. }
  35696. }
  35697. return value;
  35698. }
  35699. else if (isDeclaration(ref)) {
  35700. return DynamicValue.fromDynamicInput(node, DynamicValue.fromExternalReference(ref, lhs));
  35701. }
  35702. }
  35703. else if (lhs instanceof DynamicValue) {
  35704. return DynamicValue.fromDynamicInput(node, lhs);
  35705. }
  35706. else if (lhs instanceof SyntheticValue) {
  35707. return DynamicValue.fromSyntheticInput(node, lhs);
  35708. }
  35709. return DynamicValue.fromUnknown(node);
  35710. }
  35711. visitCallExpression(node, context) {
  35712. const lhs = this.visitExpression(node.expression, context);
  35713. if (lhs instanceof DynamicValue) {
  35714. return DynamicValue.fromDynamicInput(node, lhs);
  35715. }
  35716. // If the call refers to a builtin function, attempt to evaluate the function.
  35717. if (lhs instanceof KnownFn) {
  35718. return lhs.evaluate(node, this.evaluateFunctionArguments(node, context));
  35719. }
  35720. if (!(lhs instanceof Reference)) {
  35721. return DynamicValue.fromInvalidExpressionType(node.expression, lhs);
  35722. }
  35723. const fn = this.host.getDefinitionOfFunction(lhs.node);
  35724. if (fn === null) {
  35725. return DynamicValue.fromInvalidExpressionType(node.expression, lhs);
  35726. }
  35727. if (!isFunctionOrMethodReference(lhs)) {
  35728. return DynamicValue.fromInvalidExpressionType(node.expression, lhs);
  35729. }
  35730. const resolveFfrExpr = (expr) => {
  35731. let contextExtension = {};
  35732. // TODO(alxhub): the condition `fn.body === null` here is vestigial - we probably _do_ want to
  35733. // change the context like this even for non-null function bodies. But, this is being
  35734. // redesigned as a refactoring with no behavior changes so that should be done as a follow-up.
  35735. if (fn.body === null &&
  35736. expr.getSourceFile() !== node.expression.getSourceFile() &&
  35737. lhs.bestGuessOwningModule !== null) {
  35738. contextExtension = {
  35739. absoluteModuleName: lhs.bestGuessOwningModule.specifier,
  35740. resolutionContext: lhs.bestGuessOwningModule.resolutionContext,
  35741. };
  35742. }
  35743. return this.visitFfrExpression(expr, { ...context, ...contextExtension });
  35744. };
  35745. // If the function is foreign (declared through a d.ts file), attempt to resolve it with the
  35746. // foreignFunctionResolver, if one is specified.
  35747. if (fn.body === null && context.foreignFunctionResolver !== undefined) {
  35748. const unresolvable = DynamicValue.fromDynamicInput(node, DynamicValue.fromExternalReference(node.expression, lhs));
  35749. return context.foreignFunctionResolver(lhs, node, resolveFfrExpr, unresolvable);
  35750. }
  35751. const res = this.visitFunctionBody(node, fn, context);
  35752. // If the result of attempting to resolve the function body was a DynamicValue, attempt to use
  35753. // the foreignFunctionResolver if one is present. This could still potentially yield a usable
  35754. // value.
  35755. if (res instanceof DynamicValue && context.foreignFunctionResolver !== undefined) {
  35756. const unresolvable = DynamicValue.fromComplexFunctionCall(node, fn);
  35757. return context.foreignFunctionResolver(lhs, node, resolveFfrExpr, unresolvable);
  35758. }
  35759. return res;
  35760. }
  35761. /**
  35762. * Visit an expression which was extracted from a foreign-function resolver.
  35763. *
  35764. * This will process the result and ensure it's correct for FFR-resolved values, including marking
  35765. * `Reference`s as synthetic.
  35766. */
  35767. visitFfrExpression(expr, context) {
  35768. const res = this.visitExpression(expr, context);
  35769. if (res instanceof Reference) {
  35770. // This Reference was created synthetically, via a foreign function resolver. The real
  35771. // runtime value of the function expression may be different than the foreign function
  35772. // resolved value, so mark the Reference as synthetic to avoid it being misinterpreted.
  35773. res.synthetic = true;
  35774. }
  35775. return res;
  35776. }
  35777. visitFunctionBody(node, fn, context) {
  35778. if (fn.body === null) {
  35779. return DynamicValue.fromUnknown(node);
  35780. }
  35781. else if (fn.body.length !== 1 || !ts.isReturnStatement(fn.body[0])) {
  35782. return DynamicValue.fromComplexFunctionCall(node, fn);
  35783. }
  35784. const ret = fn.body[0];
  35785. const args = this.evaluateFunctionArguments(node, context);
  35786. const newScope = new Map();
  35787. const calleeContext = { ...context, scope: newScope };
  35788. fn.parameters.forEach((param, index) => {
  35789. let arg = args[index];
  35790. if (param.node.dotDotDotToken !== undefined) {
  35791. arg = args.slice(index);
  35792. }
  35793. if (arg === undefined && param.initializer !== null) {
  35794. arg = this.visitExpression(param.initializer, calleeContext);
  35795. }
  35796. newScope.set(param.node, arg);
  35797. });
  35798. return ret.expression !== undefined
  35799. ? this.visitExpression(ret.expression, calleeContext)
  35800. : undefined;
  35801. }
  35802. visitConditionalExpression(node, context) {
  35803. const condition = this.visitExpression(node.condition, context);
  35804. if (condition instanceof DynamicValue) {
  35805. return DynamicValue.fromDynamicInput(node, condition);
  35806. }
  35807. if (condition) {
  35808. return this.visitExpression(node.whenTrue, context);
  35809. }
  35810. else {
  35811. return this.visitExpression(node.whenFalse, context);
  35812. }
  35813. }
  35814. visitPrefixUnaryExpression(node, context) {
  35815. const operatorKind = node.operator;
  35816. if (!UNARY_OPERATORS$2.has(operatorKind)) {
  35817. return DynamicValue.fromUnsupportedSyntax(node);
  35818. }
  35819. const op = UNARY_OPERATORS$2.get(operatorKind);
  35820. const value = this.visitExpression(node.operand, context);
  35821. if (value instanceof DynamicValue) {
  35822. return DynamicValue.fromDynamicInput(node, value);
  35823. }
  35824. else {
  35825. return op(value);
  35826. }
  35827. }
  35828. visitBinaryExpression(node, context) {
  35829. const tokenKind = node.operatorToken.kind;
  35830. if (!BINARY_OPERATORS$2.has(tokenKind)) {
  35831. return DynamicValue.fromUnsupportedSyntax(node);
  35832. }
  35833. const opRecord = BINARY_OPERATORS$2.get(tokenKind);
  35834. let lhs, rhs;
  35835. if (opRecord.literal) {
  35836. lhs = literal(this.visitExpression(node.left, context), (value) => DynamicValue.fromInvalidExpressionType(node.left, value));
  35837. rhs = literal(this.visitExpression(node.right, context), (value) => DynamicValue.fromInvalidExpressionType(node.right, value));
  35838. }
  35839. else {
  35840. lhs = this.visitExpression(node.left, context);
  35841. rhs = this.visitExpression(node.right, context);
  35842. }
  35843. if (lhs instanceof DynamicValue) {
  35844. return DynamicValue.fromDynamicInput(node, lhs);
  35845. }
  35846. else if (rhs instanceof DynamicValue) {
  35847. return DynamicValue.fromDynamicInput(node, rhs);
  35848. }
  35849. else {
  35850. return opRecord.op(lhs, rhs);
  35851. }
  35852. }
  35853. visitParenthesizedExpression(node, context) {
  35854. return this.visitExpression(node.expression, context);
  35855. }
  35856. evaluateFunctionArguments(node, context) {
  35857. const args = [];
  35858. for (const arg of node.arguments) {
  35859. if (ts.isSpreadElement(arg)) {
  35860. args.push(...this.visitSpreadElement(arg, context));
  35861. }
  35862. else {
  35863. args.push(this.visitExpression(arg, context));
  35864. }
  35865. }
  35866. return args;
  35867. }
  35868. visitSpreadElement(node, context) {
  35869. const spread = this.visitExpression(node.expression, context);
  35870. if (spread instanceof DynamicValue) {
  35871. return [DynamicValue.fromDynamicInput(node, spread)];
  35872. }
  35873. else if (!Array.isArray(spread)) {
  35874. return [DynamicValue.fromInvalidExpressionType(node, spread)];
  35875. }
  35876. else {
  35877. return spread;
  35878. }
  35879. }
  35880. visitBindingElement(node, context) {
  35881. const path = [];
  35882. let closestDeclaration = node;
  35883. while (ts.isBindingElement(closestDeclaration) ||
  35884. ts.isArrayBindingPattern(closestDeclaration) ||
  35885. ts.isObjectBindingPattern(closestDeclaration)) {
  35886. if (ts.isBindingElement(closestDeclaration)) {
  35887. path.unshift(closestDeclaration);
  35888. }
  35889. closestDeclaration = closestDeclaration.parent;
  35890. }
  35891. if (!ts.isVariableDeclaration(closestDeclaration) ||
  35892. closestDeclaration.initializer === undefined) {
  35893. return DynamicValue.fromUnknown(node);
  35894. }
  35895. let value = this.visit(closestDeclaration.initializer, context);
  35896. for (const element of path) {
  35897. let key;
  35898. if (ts.isArrayBindingPattern(element.parent)) {
  35899. key = element.parent.elements.indexOf(element);
  35900. }
  35901. else {
  35902. const name = element.propertyName || element.name;
  35903. if (ts.isIdentifier(name)) {
  35904. key = name.text;
  35905. }
  35906. else {
  35907. return DynamicValue.fromUnknown(element);
  35908. }
  35909. }
  35910. value = this.accessHelper(element, value, key, context);
  35911. if (value instanceof DynamicValue) {
  35912. return value;
  35913. }
  35914. }
  35915. return value;
  35916. }
  35917. stringNameFromPropertyName(node, context) {
  35918. if (ts.isIdentifier(node) || ts.isStringLiteral(node) || ts.isNumericLiteral(node)) {
  35919. return node.text;
  35920. }
  35921. else if (ts.isComputedPropertyName(node)) {
  35922. const literal = this.visitExpression(node.expression, context);
  35923. return typeof literal === 'string' ? literal : undefined;
  35924. }
  35925. else {
  35926. return undefined;
  35927. }
  35928. }
  35929. getReference(node, context) {
  35930. return new Reference(node, owningModule(context));
  35931. }
  35932. visitType(node, context) {
  35933. if (ts.isLiteralTypeNode(node)) {
  35934. return this.visitExpression(node.literal, context);
  35935. }
  35936. else if (ts.isTupleTypeNode(node)) {
  35937. return this.visitTupleType(node, context);
  35938. }
  35939. else if (ts.isNamedTupleMember(node)) {
  35940. return this.visitType(node.type, context);
  35941. }
  35942. else if (ts.isTypeOperatorNode(node) && node.operator === ts.SyntaxKind.ReadonlyKeyword) {
  35943. return this.visitType(node.type, context);
  35944. }
  35945. else if (ts.isTypeQueryNode(node)) {
  35946. return this.visitTypeQuery(node, context);
  35947. }
  35948. return DynamicValue.fromDynamicType(node);
  35949. }
  35950. visitTupleType(node, context) {
  35951. const res = [];
  35952. for (const elem of node.elements) {
  35953. res.push(this.visitType(elem, context));
  35954. }
  35955. return res;
  35956. }
  35957. visitTypeQuery(node, context) {
  35958. if (!ts.isIdentifier(node.exprName)) {
  35959. return DynamicValue.fromUnknown(node);
  35960. }
  35961. const decl = this.host.getDeclarationOfIdentifier(node.exprName);
  35962. if (decl === null) {
  35963. return DynamicValue.fromUnknownIdentifier(node.exprName);
  35964. }
  35965. const declContext = { ...context, ...joinModuleContext(context, node, decl) };
  35966. return this.visitDeclaration(decl.node, declContext);
  35967. }
  35968. }
  35969. function isFunctionOrMethodReference(ref) {
  35970. return (ts.isFunctionDeclaration(ref.node) ||
  35971. ts.isMethodDeclaration(ref.node) ||
  35972. ts.isFunctionExpression(ref.node));
  35973. }
  35974. function literal(value, reject) {
  35975. if (value instanceof EnumValue) {
  35976. value = value.resolved;
  35977. }
  35978. if (value instanceof DynamicValue ||
  35979. value === null ||
  35980. value === undefined ||
  35981. typeof value === 'string' ||
  35982. typeof value === 'number' ||
  35983. typeof value === 'boolean') {
  35984. return value;
  35985. }
  35986. return reject(value);
  35987. }
  35988. function isVariableDeclarationDeclared(node) {
  35989. if (node.parent === undefined || !ts.isVariableDeclarationList(node.parent)) {
  35990. return false;
  35991. }
  35992. const declList = node.parent;
  35993. if (declList.parent === undefined || !ts.isVariableStatement(declList.parent)) {
  35994. return false;
  35995. }
  35996. const varStmt = declList.parent;
  35997. const modifiers = ts.getModifiers(varStmt);
  35998. return (modifiers !== undefined && modifiers.some((mod) => mod.kind === ts.SyntaxKind.DeclareKeyword));
  35999. }
  36000. const EMPTY = {};
  36001. function joinModuleContext(existing, node, decl) {
  36002. if (typeof decl.viaModule === 'string' && decl.viaModule !== existing.absoluteModuleName) {
  36003. return {
  36004. absoluteModuleName: decl.viaModule,
  36005. resolutionContext: node.getSourceFile().fileName,
  36006. };
  36007. }
  36008. else {
  36009. return EMPTY;
  36010. }
  36011. }
  36012. function owningModule(context, override = null) {
  36013. let specifier = context.absoluteModuleName;
  36014. if (override !== null) {
  36015. specifier = override.specifier;
  36016. }
  36017. if (specifier !== null) {
  36018. return {
  36019. specifier,
  36020. resolutionContext: context.resolutionContext,
  36021. };
  36022. }
  36023. else {
  36024. return null;
  36025. }
  36026. }
  36027. /**
  36028. * Specifies the compilation mode that is used for the compilation.
  36029. */
  36030. exports.CompilationMode = void 0;
  36031. (function (CompilationMode) {
  36032. /**
  36033. * Generates fully AOT compiled code using Ivy instructions.
  36034. */
  36035. CompilationMode[CompilationMode["FULL"] = 0] = "FULL";
  36036. /**
  36037. * Generates code using a stable, but intermediate format suitable to be published to NPM.
  36038. */
  36039. CompilationMode[CompilationMode["PARTIAL"] = 1] = "PARTIAL";
  36040. /**
  36041. * Generates code based on each individual source file without using its
  36042. * dependencies (suitable for local dev edit/refresh workflow).
  36043. */
  36044. CompilationMode[CompilationMode["LOCAL"] = 2] = "LOCAL";
  36045. })(exports.CompilationMode || (exports.CompilationMode = {}));
  36046. exports.HandlerPrecedence = void 0;
  36047. (function (HandlerPrecedence) {
  36048. /**
  36049. * Handler with PRIMARY precedence cannot overlap - there can only be one on a given class.
  36050. *
  36051. * If more than one PRIMARY handler matches a class, an error is produced.
  36052. */
  36053. HandlerPrecedence[HandlerPrecedence["PRIMARY"] = 0] = "PRIMARY";
  36054. /**
  36055. * Handlers with SHARED precedence can match any class, possibly in addition to a single PRIMARY
  36056. * handler.
  36057. *
  36058. * It is not an error for a class to have any number of SHARED handlers.
  36059. */
  36060. HandlerPrecedence[HandlerPrecedence["SHARED"] = 1] = "SHARED";
  36061. /**
  36062. * Handlers with WEAK precedence that match a class are ignored if any handlers with stronger
  36063. * precedence match a class.
  36064. */
  36065. HandlerPrecedence[HandlerPrecedence["WEAK"] = 2] = "WEAK";
  36066. })(exports.HandlerPrecedence || (exports.HandlerPrecedence = {}));
  36067. /**
  36068. * A phase of compilation for which time is tracked in a distinct bucket.
  36069. */
  36070. exports.PerfPhase = void 0;
  36071. (function (PerfPhase) {
  36072. /**
  36073. * The "default" phase which tracks time not spent in any other phase.
  36074. */
  36075. PerfPhase[PerfPhase["Unaccounted"] = 0] = "Unaccounted";
  36076. /**
  36077. * Time spent setting up the compiler, before a TypeScript program is created.
  36078. *
  36079. * This includes operations like configuring the `ts.CompilerHost` and any wrappers.
  36080. */
  36081. PerfPhase[PerfPhase["Setup"] = 1] = "Setup";
  36082. /**
  36083. * Time spent in `ts.createProgram`, including reading and parsing `ts.SourceFile`s in the
  36084. * `ts.CompilerHost`.
  36085. *
  36086. * This might be an incremental program creation operation.
  36087. */
  36088. PerfPhase[PerfPhase["TypeScriptProgramCreate"] = 2] = "TypeScriptProgramCreate";
  36089. /**
  36090. * Time spent reconciling the contents of an old `ts.Program` with the new incremental one.
  36091. *
  36092. * Only present in incremental compilations.
  36093. */
  36094. PerfPhase[PerfPhase["Reconciliation"] = 3] = "Reconciliation";
  36095. /**
  36096. * Time spent updating an `NgCompiler` instance with a resource-only change.
  36097. *
  36098. * Only present in incremental compilations where the change was resource-only.
  36099. */
  36100. PerfPhase[PerfPhase["ResourceUpdate"] = 4] = "ResourceUpdate";
  36101. /**
  36102. * Time spent calculating the plain TypeScript diagnostics (structural and semantic).
  36103. */
  36104. PerfPhase[PerfPhase["TypeScriptDiagnostics"] = 5] = "TypeScriptDiagnostics";
  36105. /**
  36106. * Time spent in Angular analysis of individual classes in the program.
  36107. */
  36108. PerfPhase[PerfPhase["Analysis"] = 6] = "Analysis";
  36109. /**
  36110. * Time spent in Angular global analysis (synthesis of analysis information into a complete
  36111. * understanding of the program).
  36112. */
  36113. PerfPhase[PerfPhase["Resolve"] = 7] = "Resolve";
  36114. /**
  36115. * Time spent building the import graph of the program in order to perform cycle detection.
  36116. */
  36117. PerfPhase[PerfPhase["CycleDetection"] = 8] = "CycleDetection";
  36118. /**
  36119. * Time spent generating the text of Type Check Blocks in order to perform template type checking.
  36120. */
  36121. PerfPhase[PerfPhase["TcbGeneration"] = 9] = "TcbGeneration";
  36122. /**
  36123. * Time spent updating the `ts.Program` with new Type Check Block code.
  36124. */
  36125. PerfPhase[PerfPhase["TcbUpdateProgram"] = 10] = "TcbUpdateProgram";
  36126. /**
  36127. * Time spent by TypeScript performing its emit operations, including downleveling and writing
  36128. * output files.
  36129. */
  36130. PerfPhase[PerfPhase["TypeScriptEmit"] = 11] = "TypeScriptEmit";
  36131. /**
  36132. * Time spent by Angular performing code transformations of ASTs as they're about to be emitted.
  36133. *
  36134. * This includes the actual code generation step for templates, and occurs during the emit phase
  36135. * (but is tracked separately from `TypeScriptEmit` time).
  36136. */
  36137. PerfPhase[PerfPhase["Compile"] = 12] = "Compile";
  36138. /**
  36139. * Time spent performing a `TemplateTypeChecker` autocompletion operation.
  36140. */
  36141. PerfPhase[PerfPhase["TtcAutocompletion"] = 13] = "TtcAutocompletion";
  36142. /**
  36143. * Time spent computing template type-checking diagnostics.
  36144. */
  36145. PerfPhase[PerfPhase["TtcDiagnostics"] = 14] = "TtcDiagnostics";
  36146. /**
  36147. * Time spent getting a `Symbol` from the `TemplateTypeChecker`.
  36148. */
  36149. PerfPhase[PerfPhase["TtcSymbol"] = 15] = "TtcSymbol";
  36150. /**
  36151. * Time spent by the Angular Language Service calculating a "get references" or a renaming
  36152. * operation.
  36153. */
  36154. PerfPhase[PerfPhase["LsReferencesAndRenames"] = 16] = "LsReferencesAndRenames";
  36155. /**
  36156. * Time spent by the Angular Language Service calculating a "quick info" operation.
  36157. */
  36158. PerfPhase[PerfPhase["LsQuickInfo"] = 17] = "LsQuickInfo";
  36159. /**
  36160. * Time spent by the Angular Language Service calculating a "get type definition" or "get
  36161. * definition" operation.
  36162. */
  36163. PerfPhase[PerfPhase["LsDefinition"] = 18] = "LsDefinition";
  36164. /**
  36165. * Time spent by the Angular Language Service calculating a "get completions" (AKA autocomplete)
  36166. * operation.
  36167. */
  36168. PerfPhase[PerfPhase["LsCompletions"] = 19] = "LsCompletions";
  36169. /**
  36170. * Time spent by the Angular Language Service calculating a "view template typecheck block"
  36171. * operation.
  36172. */
  36173. PerfPhase[PerfPhase["LsTcb"] = 20] = "LsTcb";
  36174. /**
  36175. * Time spent by the Angular Language Service calculating diagnostics.
  36176. */
  36177. PerfPhase[PerfPhase["LsDiagnostics"] = 21] = "LsDiagnostics";
  36178. /**
  36179. * Time spent by the Angular Language Service calculating a "get component locations for template"
  36180. * operation.
  36181. */
  36182. PerfPhase[PerfPhase["LsComponentLocations"] = 22] = "LsComponentLocations";
  36183. /**
  36184. * Time spent by the Angular Language Service calculating signature help.
  36185. */
  36186. PerfPhase[PerfPhase["LsSignatureHelp"] = 23] = "LsSignatureHelp";
  36187. /**
  36188. * Time spent by the Angular Language Service calculating outlining spans.
  36189. */
  36190. PerfPhase[PerfPhase["OutliningSpans"] = 24] = "OutliningSpans";
  36191. /**
  36192. * Tracks the number of `PerfPhase`s, and must appear at the end of the list.
  36193. */
  36194. PerfPhase[PerfPhase["LAST"] = 25] = "LAST";
  36195. /**
  36196. * Time spent by the Angular Language Service calculating code fixes.
  36197. */
  36198. PerfPhase[PerfPhase["LsCodeFixes"] = 26] = "LsCodeFixes";
  36199. /**
  36200. * Time spent by the Angular Language Service to fix all detected same type errors.
  36201. */
  36202. PerfPhase[PerfPhase["LsCodeFixesAll"] = 27] = "LsCodeFixesAll";
  36203. /**
  36204. * Time spent computing possible Angular refactorings.
  36205. */
  36206. PerfPhase[PerfPhase["LSComputeApplicableRefactorings"] = 28] = "LSComputeApplicableRefactorings";
  36207. /**
  36208. * Time spent computing changes for applying a given refactoring.
  36209. */
  36210. PerfPhase[PerfPhase["LSApplyRefactoring"] = 29] = "LSApplyRefactoring";
  36211. })(exports.PerfPhase || (exports.PerfPhase = {}));
  36212. /**
  36213. * Represents some occurrence during compilation, and is tracked with a counter.
  36214. */
  36215. exports.PerfEvent = void 0;
  36216. (function (PerfEvent) {
  36217. /**
  36218. * Counts the number of `.d.ts` files in the program.
  36219. */
  36220. PerfEvent[PerfEvent["InputDtsFile"] = 0] = "InputDtsFile";
  36221. /**
  36222. * Counts the number of non-`.d.ts` files in the program.
  36223. */
  36224. PerfEvent[PerfEvent["InputTsFile"] = 1] = "InputTsFile";
  36225. /**
  36226. * An `@Component` class was analyzed.
  36227. */
  36228. PerfEvent[PerfEvent["AnalyzeComponent"] = 2] = "AnalyzeComponent";
  36229. /**
  36230. * An `@Directive` class was analyzed.
  36231. */
  36232. PerfEvent[PerfEvent["AnalyzeDirective"] = 3] = "AnalyzeDirective";
  36233. /**
  36234. * An `@Injectable` class was analyzed.
  36235. */
  36236. PerfEvent[PerfEvent["AnalyzeInjectable"] = 4] = "AnalyzeInjectable";
  36237. /**
  36238. * An `@NgModule` class was analyzed.
  36239. */
  36240. PerfEvent[PerfEvent["AnalyzeNgModule"] = 5] = "AnalyzeNgModule";
  36241. /**
  36242. * An `@Pipe` class was analyzed.
  36243. */
  36244. PerfEvent[PerfEvent["AnalyzePipe"] = 6] = "AnalyzePipe";
  36245. /**
  36246. * A trait was analyzed.
  36247. *
  36248. * In theory, this should be the sum of the `Analyze` counters for each decorator type.
  36249. */
  36250. PerfEvent[PerfEvent["TraitAnalyze"] = 7] = "TraitAnalyze";
  36251. /**
  36252. * A trait had a prior analysis available from an incremental program, and did not need to be
  36253. * re-analyzed.
  36254. */
  36255. PerfEvent[PerfEvent["TraitReuseAnalysis"] = 8] = "TraitReuseAnalysis";
  36256. /**
  36257. * A `ts.SourceFile` directly changed between the prior program and a new incremental compilation.
  36258. */
  36259. PerfEvent[PerfEvent["SourceFilePhysicalChange"] = 9] = "SourceFilePhysicalChange";
  36260. /**
  36261. * A `ts.SourceFile` did not physically changed, but according to the file dependency graph, has
  36262. * logically changed between the prior program and a new incremental compilation.
  36263. */
  36264. PerfEvent[PerfEvent["SourceFileLogicalChange"] = 10] = "SourceFileLogicalChange";
  36265. /**
  36266. * A `ts.SourceFile` has not logically changed and all of its analysis results were thus available
  36267. * for reuse.
  36268. */
  36269. PerfEvent[PerfEvent["SourceFileReuseAnalysis"] = 11] = "SourceFileReuseAnalysis";
  36270. /**
  36271. * A Type Check Block (TCB) was generated.
  36272. */
  36273. PerfEvent[PerfEvent["GenerateTcb"] = 12] = "GenerateTcb";
  36274. /**
  36275. * A Type Check Block (TCB) could not be generated because inlining was disabled, and the block
  36276. * would've required inlining.
  36277. */
  36278. PerfEvent[PerfEvent["SkipGenerateTcbNoInline"] = 13] = "SkipGenerateTcbNoInline";
  36279. /**
  36280. * A `.ngtypecheck.ts` file could be reused from the previous program and did not need to be
  36281. * regenerated.
  36282. */
  36283. PerfEvent[PerfEvent["ReuseTypeCheckFile"] = 14] = "ReuseTypeCheckFile";
  36284. /**
  36285. * The template type-checking program required changes and had to be updated in an incremental
  36286. * step.
  36287. */
  36288. PerfEvent[PerfEvent["UpdateTypeCheckProgram"] = 15] = "UpdateTypeCheckProgram";
  36289. /**
  36290. * The compiler was able to prove that a `ts.SourceFile` did not need to be re-emitted.
  36291. */
  36292. PerfEvent[PerfEvent["EmitSkipSourceFile"] = 16] = "EmitSkipSourceFile";
  36293. /**
  36294. * A `ts.SourceFile` was emitted.
  36295. */
  36296. PerfEvent[PerfEvent["EmitSourceFile"] = 17] = "EmitSourceFile";
  36297. /**
  36298. * Tracks the number of `PrefEvent`s, and must appear at the end of the list.
  36299. */
  36300. PerfEvent[PerfEvent["LAST"] = 18] = "LAST";
  36301. })(exports.PerfEvent || (exports.PerfEvent = {}));
  36302. /**
  36303. * Represents a checkpoint during compilation at which the memory usage of the compiler should be
  36304. * recorded.
  36305. */
  36306. exports.PerfCheckpoint = void 0;
  36307. (function (PerfCheckpoint) {
  36308. /**
  36309. * The point at which the `PerfRecorder` was created, and ideally tracks memory used before any
  36310. * compilation structures are created.
  36311. */
  36312. PerfCheckpoint[PerfCheckpoint["Initial"] = 0] = "Initial";
  36313. /**
  36314. * The point just after the `ts.Program` has been created.
  36315. */
  36316. PerfCheckpoint[PerfCheckpoint["TypeScriptProgramCreate"] = 1] = "TypeScriptProgramCreate";
  36317. /**
  36318. * The point just before Angular analysis starts.
  36319. *
  36320. * In the main usage pattern for the compiler, TypeScript diagnostics have been calculated at this
  36321. * point, so the `ts.TypeChecker` has fully ingested the current program, all `ts.Type` structures
  36322. * and `ts.Symbol`s have been created.
  36323. */
  36324. PerfCheckpoint[PerfCheckpoint["PreAnalysis"] = 2] = "PreAnalysis";
  36325. /**
  36326. * The point just after Angular analysis completes.
  36327. */
  36328. PerfCheckpoint[PerfCheckpoint["Analysis"] = 3] = "Analysis";
  36329. /**
  36330. * The point just after Angular resolution is complete.
  36331. */
  36332. PerfCheckpoint[PerfCheckpoint["Resolve"] = 4] = "Resolve";
  36333. /**
  36334. * The point just after Type Check Blocks (TCBs) have been generated.
  36335. */
  36336. PerfCheckpoint[PerfCheckpoint["TtcGeneration"] = 5] = "TtcGeneration";
  36337. /**
  36338. * The point just after the template type-checking program has been updated with any new TCBs.
  36339. */
  36340. PerfCheckpoint[PerfCheckpoint["TtcUpdateProgram"] = 6] = "TtcUpdateProgram";
  36341. /**
  36342. * The point just before emit begins.
  36343. *
  36344. * In the main usage pattern for the compiler, all template type-checking diagnostics have been
  36345. * requested at this point.
  36346. */
  36347. PerfCheckpoint[PerfCheckpoint["PreEmit"] = 7] = "PreEmit";
  36348. /**
  36349. * The point just after the program has been fully emitted.
  36350. */
  36351. PerfCheckpoint[PerfCheckpoint["Emit"] = 8] = "Emit";
  36352. /**
  36353. * Tracks the number of `PerfCheckpoint`s, and must appear at the end of the list.
  36354. */
  36355. PerfCheckpoint[PerfCheckpoint["LAST"] = 9] = "LAST";
  36356. })(exports.PerfCheckpoint || (exports.PerfCheckpoint = {}));
  36357. exports.TraitState = void 0;
  36358. (function (TraitState) {
  36359. /**
  36360. * Pending traits are freshly created and have never been analyzed.
  36361. */
  36362. TraitState[TraitState["Pending"] = 0] = "Pending";
  36363. /**
  36364. * Analyzed traits have successfully been analyzed, but are pending resolution.
  36365. */
  36366. TraitState[TraitState["Analyzed"] = 1] = "Analyzed";
  36367. /**
  36368. * Resolved traits have successfully been analyzed and resolved and are ready for compilation.
  36369. */
  36370. TraitState[TraitState["Resolved"] = 2] = "Resolved";
  36371. /**
  36372. * Skipped traits are no longer considered for compilation.
  36373. */
  36374. TraitState[TraitState["Skipped"] = 3] = "Skipped";
  36375. })(exports.TraitState || (exports.TraitState = {}));
  36376. /**
  36377. * The value side of `Trait` exposes a helper to create a `Trait` in a pending state (by delegating
  36378. * to `TraitImpl`).
  36379. */
  36380. const Trait = {
  36381. pending: (handler, detected) => TraitImpl.pending(handler, detected),
  36382. };
  36383. /**
  36384. * An implementation of the `Trait` type which transitions safely between the various
  36385. * `TraitState`s.
  36386. */
  36387. class TraitImpl {
  36388. state = exports.TraitState.Pending;
  36389. handler;
  36390. detected;
  36391. analysis = null;
  36392. symbol = null;
  36393. resolution = null;
  36394. analysisDiagnostics = null;
  36395. resolveDiagnostics = null;
  36396. typeCheckDiagnostics = null;
  36397. constructor(handler, detected) {
  36398. this.handler = handler;
  36399. this.detected = detected;
  36400. }
  36401. toAnalyzed(analysis, diagnostics, symbol) {
  36402. // Only pending traits can be analyzed.
  36403. this.assertTransitionLegal(exports.TraitState.Pending, exports.TraitState.Analyzed);
  36404. this.analysis = analysis;
  36405. this.analysisDiagnostics = diagnostics;
  36406. this.symbol = symbol;
  36407. this.state = exports.TraitState.Analyzed;
  36408. return this;
  36409. }
  36410. toResolved(resolution, diagnostics) {
  36411. // Only analyzed traits can be resolved.
  36412. this.assertTransitionLegal(exports.TraitState.Analyzed, exports.TraitState.Resolved);
  36413. if (this.analysis === null) {
  36414. throw new Error(`Cannot transition an Analyzed trait with a null analysis to Resolved`);
  36415. }
  36416. this.resolution = resolution;
  36417. this.state = exports.TraitState.Resolved;
  36418. this.resolveDiagnostics = diagnostics;
  36419. this.typeCheckDiagnostics = null;
  36420. return this;
  36421. }
  36422. toSkipped() {
  36423. // Only pending traits can be skipped.
  36424. this.assertTransitionLegal(exports.TraitState.Pending, exports.TraitState.Skipped);
  36425. this.state = exports.TraitState.Skipped;
  36426. return this;
  36427. }
  36428. /**
  36429. * Verifies that the trait is currently in one of the `allowedState`s.
  36430. *
  36431. * If correctly used, the `Trait` type and transition methods prevent illegal transitions from
  36432. * occurring. However, if a reference to the `TraitImpl` instance typed with the previous
  36433. * interface is retained after calling one of its transition methods, it will allow for illegal
  36434. * transitions to take place. Hence, this assertion provides a little extra runtime protection.
  36435. */
  36436. assertTransitionLegal(allowedState, transitionTo) {
  36437. if (!(this.state === allowedState)) {
  36438. throw new Error(`Assertion failure: cannot transition from ${exports.TraitState[this.state]} to ${exports.TraitState[transitionTo]}.`);
  36439. }
  36440. }
  36441. /**
  36442. * Construct a new `TraitImpl` in the pending state.
  36443. */
  36444. static pending(handler, detected) {
  36445. return new TraitImpl(handler, detected);
  36446. }
  36447. }
  36448. /**
  36449. * The current context of a translator visitor as it traverses the AST tree.
  36450. *
  36451. * It tracks whether we are in the process of outputting a statement or an expression.
  36452. */
  36453. let Context$1 = class Context {
  36454. isStatement;
  36455. constructor(isStatement) {
  36456. this.isStatement = isStatement;
  36457. }
  36458. get withExpressionMode() {
  36459. return this.isStatement ? new Context(false) : this;
  36460. }
  36461. get withStatementMode() {
  36462. return !this.isStatement ? new Context(true) : this;
  36463. }
  36464. };
  36465. /**
  36466. * Generates a helper for `ImportManagerConfig` to generate unique identifiers
  36467. * for a given source file.
  36468. */
  36469. function createGenerateUniqueIdentifierHelper() {
  36470. const generatedIdentifiers = new Set();
  36471. const isGeneratedIdentifier = (sf, identifierName) => generatedIdentifiers.has(`${sf.fileName}@@${identifierName}`);
  36472. const markIdentifierAsGenerated = (sf, identifierName) => generatedIdentifiers.add(`${sf.fileName}@@${identifierName}`);
  36473. return (sourceFile, symbolName) => {
  36474. const sf = sourceFile;
  36475. if (sf.identifiers === undefined) {
  36476. throw new Error('Source file unexpectedly lacks map of parsed `identifiers`.');
  36477. }
  36478. const isUniqueIdentifier = (name) => !sf.identifiers.has(name) && !isGeneratedIdentifier(sf, name);
  36479. if (isUniqueIdentifier(symbolName)) {
  36480. markIdentifierAsGenerated(sf, symbolName);
  36481. return null;
  36482. }
  36483. let name = null;
  36484. let counter = 1;
  36485. do {
  36486. name = `${symbolName}_${counter++}`;
  36487. } while (!isUniqueIdentifier(name));
  36488. markIdentifierAsGenerated(sf, name);
  36489. return ts.factory.createUniqueName(name, ts.GeneratedIdentifierFlags.Optimistic);
  36490. };
  36491. }
  36492. /**
  36493. * Creates a TypeScript transform for the given import manager.
  36494. *
  36495. * - The transform updates existing imports with new symbols to be added.
  36496. * - The transform adds new necessary imports.
  36497. * - The transform inserts additional optional statements after imports.
  36498. * - The transform deletes any nodes that are marked for deletion by the manager.
  36499. */
  36500. function createTsTransformForImportManager(manager, extraStatementsForFiles) {
  36501. return (ctx) => {
  36502. const { affectedFiles, newImports, updatedImports, reusedOriginalAliasDeclarations, deletedImports, } = manager.finalize();
  36503. // If we re-used existing source file alias declarations, mark those as referenced so TypeScript
  36504. // doesn't drop these thinking they are unused.
  36505. if (reusedOriginalAliasDeclarations.size > 0) {
  36506. const referencedAliasDeclarations = loadIsReferencedAliasDeclarationPatch(ctx);
  36507. if (referencedAliasDeclarations !== null) {
  36508. reusedOriginalAliasDeclarations.forEach((aliasDecl) => referencedAliasDeclarations.add(aliasDecl));
  36509. }
  36510. }
  36511. // Update the set of affected files to include files that need extra statements to be inserted.
  36512. if (extraStatementsForFiles !== undefined) {
  36513. for (const [fileName, statements] of extraStatementsForFiles.entries()) {
  36514. if (statements.length > 0) {
  36515. affectedFiles.add(fileName);
  36516. }
  36517. }
  36518. }
  36519. const visitStatement = (node) => {
  36520. if (!ts.isImportDeclaration(node)) {
  36521. return node;
  36522. }
  36523. if (deletedImports.has(node)) {
  36524. return undefined;
  36525. }
  36526. if (node.importClause === undefined || !ts.isImportClause(node.importClause)) {
  36527. return node;
  36528. }
  36529. const clause = node.importClause;
  36530. if (clause.namedBindings === undefined ||
  36531. !ts.isNamedImports(clause.namedBindings) ||
  36532. !updatedImports.has(clause.namedBindings)) {
  36533. return node;
  36534. }
  36535. const newClause = ctx.factory.updateImportClause(clause, clause.isTypeOnly, clause.name, updatedImports.get(clause.namedBindings));
  36536. const newImport = ctx.factory.updateImportDeclaration(node, node.modifiers, newClause, node.moduleSpecifier, node.attributes);
  36537. // This tricks TypeScript into thinking that the `importClause` is still optimizable.
  36538. // By default, TS assumes, no specifiers are elide-able if the clause of the "original
  36539. // node" has changed. google3:
  36540. // typescript/unstable/src/compiler/transformers/ts.ts;l=456;rcl=611254538.
  36541. ts.setOriginalNode(newImport, {
  36542. importClause: newClause,
  36543. kind: newImport.kind,
  36544. });
  36545. return newImport;
  36546. };
  36547. return (sourceFile) => {
  36548. if (!affectedFiles.has(sourceFile.fileName)) {
  36549. return sourceFile;
  36550. }
  36551. sourceFile = ts.visitEachChild(sourceFile, visitStatement, ctx);
  36552. // Filter out the existing imports and the source file body.
  36553. // All new statements will be inserted between them.
  36554. const extraStatements = extraStatementsForFiles?.get(sourceFile.fileName) ?? [];
  36555. const existingImports = [];
  36556. const body = [];
  36557. for (const statement of sourceFile.statements) {
  36558. if (isImportStatement(statement)) {
  36559. existingImports.push(statement);
  36560. }
  36561. else {
  36562. body.push(statement);
  36563. }
  36564. }
  36565. return ctx.factory.updateSourceFile(sourceFile, [
  36566. ...existingImports,
  36567. ...(newImports.get(sourceFile.fileName) ?? []),
  36568. ...extraStatements,
  36569. ...body,
  36570. ], sourceFile.isDeclarationFile, sourceFile.referencedFiles, sourceFile.typeReferenceDirectives, sourceFile.hasNoDefaultLib, sourceFile.libReferenceDirectives);
  36571. };
  36572. };
  36573. }
  36574. /** Whether the given statement is an import statement. */
  36575. function isImportStatement(stmt) {
  36576. return (ts.isImportDeclaration(stmt) || ts.isImportEqualsDeclaration(stmt) || ts.isNamespaceImport(stmt));
  36577. }
  36578. /** Attempts to efficiently re-use previous generated import requests. */
  36579. function attemptToReuseGeneratedImports(tracker, request) {
  36580. const requestHash = hashImportRequest(request);
  36581. // In case the given import has been already generated previously, we just return
  36582. // the previous generated identifier in order to avoid duplicate generated imports.
  36583. const existingExactImport = tracker.directReuseCache.get(requestHash);
  36584. if (existingExactImport !== undefined) {
  36585. return existingExactImport;
  36586. }
  36587. const potentialNamespaceImport = tracker.namespaceImportReuseCache.get(request.exportModuleSpecifier);
  36588. if (potentialNamespaceImport === undefined) {
  36589. return null;
  36590. }
  36591. if (request.exportSymbolName === null) {
  36592. return potentialNamespaceImport;
  36593. }
  36594. return [potentialNamespaceImport, ts.factory.createIdentifier(request.exportSymbolName)];
  36595. }
  36596. /** Captures the given import request and its generated reference node/path for future re-use. */
  36597. function captureGeneratedImport(request, tracker, referenceNode) {
  36598. tracker.directReuseCache.set(hashImportRequest(request), referenceNode);
  36599. if (request.exportSymbolName === null && !Array.isArray(referenceNode)) {
  36600. tracker.namespaceImportReuseCache.set(request.exportModuleSpecifier, referenceNode);
  36601. }
  36602. }
  36603. /** Generates a unique hash for the given import request. */
  36604. function hashImportRequest(req) {
  36605. return `${req.requestedFile.fileName}:${req.exportModuleSpecifier}:${req.exportSymbolName}${req.unsafeAliasOverride ? ':' + req.unsafeAliasOverride : ''}`;
  36606. }
  36607. /** Attempts to re-use original source file imports for the given request. */
  36608. function attemptToReuseExistingSourceFileImports(tracker, sourceFile, request) {
  36609. // Walk through all source-file top-level statements and search for import declarations
  36610. // that already match the specified "moduleName" and can be updated to import the
  36611. // given symbol. If no matching import can be found, the last import in the source-file
  36612. // will be used as starting point for a new import that will be generated.
  36613. let candidateImportToBeUpdated = null;
  36614. for (let i = sourceFile.statements.length - 1; i >= 0; i--) {
  36615. const statement = sourceFile.statements[i];
  36616. if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier)) {
  36617. continue;
  36618. }
  36619. // Side-effect imports are ignored, or type-only imports.
  36620. // TODO: Consider re-using type-only imports efficiently.
  36621. if (!statement.importClause || statement.importClause.isTypeOnly) {
  36622. continue;
  36623. }
  36624. const moduleSpecifier = statement.moduleSpecifier.text;
  36625. // If the import does not match the module name, or requested target file, continue.
  36626. // Note: In the future, we may consider performing better analysis here. E.g. resolve paths,
  36627. // or try to detect re-usable symbols via type-checking.
  36628. if (moduleSpecifier !== request.exportModuleSpecifier) {
  36629. continue;
  36630. }
  36631. if (statement.importClause.namedBindings) {
  36632. const namedBindings = statement.importClause.namedBindings;
  36633. // A namespace import can be reused.
  36634. if (ts.isNamespaceImport(namedBindings)) {
  36635. tracker.reusedAliasDeclarations.add(namedBindings);
  36636. if (request.exportSymbolName === null) {
  36637. return namedBindings.name;
  36638. }
  36639. return [namedBindings.name, ts.factory.createIdentifier(request.exportSymbolName)];
  36640. }
  36641. // Named imports can be re-used if a specific symbol is requested.
  36642. if (ts.isNamedImports(namedBindings) && request.exportSymbolName !== null) {
  36643. const existingElement = namedBindings.elements.find((e) => {
  36644. // TODO: Consider re-using type-only imports efficiently.
  36645. let nameMatches;
  36646. if (request.unsafeAliasOverride) {
  36647. // If a specific alias is passed, both the original name and alias have to match.
  36648. nameMatches =
  36649. e.propertyName?.text === request.exportSymbolName &&
  36650. e.name.text === request.unsafeAliasOverride;
  36651. }
  36652. else {
  36653. nameMatches = e.propertyName
  36654. ? e.propertyName.text === request.exportSymbolName
  36655. : e.name.text === request.exportSymbolName;
  36656. }
  36657. return !e.isTypeOnly && nameMatches;
  36658. });
  36659. if (existingElement !== undefined) {
  36660. tracker.reusedAliasDeclarations.add(existingElement);
  36661. return existingElement.name;
  36662. }
  36663. // In case the symbol could not be found in an existing import, we
  36664. // keep track of the import declaration as it can be updated to include
  36665. // the specified symbol name without having to create a new import.
  36666. candidateImportToBeUpdated = statement;
  36667. }
  36668. }
  36669. }
  36670. if (candidateImportToBeUpdated === null || request.exportSymbolName === null) {
  36671. return null;
  36672. }
  36673. // We have a candidate import. Update it to import what we need.
  36674. if (!tracker.updatedImports.has(candidateImportToBeUpdated)) {
  36675. tracker.updatedImports.set(candidateImportToBeUpdated, []);
  36676. }
  36677. const symbolsToBeImported = tracker.updatedImports.get(candidateImportToBeUpdated);
  36678. const propertyName = ts.factory.createIdentifier(request.exportSymbolName);
  36679. const fileUniqueAlias = request.unsafeAliasOverride
  36680. ? ts.factory.createIdentifier(request.unsafeAliasOverride)
  36681. : tracker.generateUniqueIdentifier(sourceFile, request.exportSymbolName);
  36682. // Since it can happen that multiple classes need to be imported within the
  36683. // specified source file and we want to add the identifiers to the existing
  36684. // import declaration, we need to keep track of the updated import declarations.
  36685. // We can't directly update the import declaration for each identifier as this
  36686. // would not be reflected in the AST— or would throw of update recording offsets.
  36687. symbolsToBeImported.push({
  36688. propertyName,
  36689. fileUniqueAlias,
  36690. });
  36691. return fileUniqueAlias ?? propertyName;
  36692. }
  36693. /**
  36694. * Preset configuration for forcing namespace imports.
  36695. *
  36696. * This preset is commonly used to avoid test differences to previous
  36697. * versions of the `ImportManager`.
  36698. */
  36699. const presetImportManagerForceNamespaceImports = {
  36700. // Forcing namespace imports also means no-reuse.
  36701. // Re-using would otherwise become more complicated and we don't
  36702. // expect re-usable namespace imports.
  36703. disableOriginalSourceFileReuse: true,
  36704. forceGenerateNamespacesForNewImports: true,
  36705. };
  36706. /**
  36707. * Import manager that can be used to conveniently and efficiently generate
  36708. * imports It efficiently re-uses existing source file imports, or previous
  36709. * generated imports.
  36710. *
  36711. * These capabilities are important for efficient TypeScript transforms that
  36712. * minimize structural changes to the dependency graph of source files, enabling
  36713. * as much incremental re-use as possible.
  36714. *
  36715. * Those imports may be inserted via a TypeScript transform, or via manual string
  36716. * manipulation using e.g. `magic-string`.
  36717. */
  36718. class ImportManager {
  36719. /** List of new imports that will be inserted into given source files. */
  36720. newImports = new Map();
  36721. /**
  36722. * Keeps track of imports marked for removal. The root-level key is the file from which the
  36723. * import should be removed, the inner map key is the name of the module from which the symbol
  36724. * is being imported. The value of the inner map is a set of symbol names that should be removed.
  36725. * Note! the inner map tracks the original names of the imported symbols, not their local aliases.
  36726. */
  36727. removedImports = new Map();
  36728. nextUniqueIndex = 0;
  36729. config;
  36730. reuseSourceFileImportsTracker;
  36731. reuseGeneratedImportsTracker = {
  36732. directReuseCache: new Map(),
  36733. namespaceImportReuseCache: new Map(),
  36734. };
  36735. constructor(config = {}) {
  36736. this.config = {
  36737. shouldUseSingleQuotes: config.shouldUseSingleQuotes ?? (() => false),
  36738. rewriter: config.rewriter ?? null,
  36739. disableOriginalSourceFileReuse: config.disableOriginalSourceFileReuse ?? false,
  36740. forceGenerateNamespacesForNewImports: config.forceGenerateNamespacesForNewImports ?? false,
  36741. namespaceImportPrefix: config.namespaceImportPrefix ?? 'i',
  36742. generateUniqueIdentifier: config.generateUniqueIdentifier ?? createGenerateUniqueIdentifierHelper(),
  36743. };
  36744. this.reuseSourceFileImportsTracker = {
  36745. generateUniqueIdentifier: this.config.generateUniqueIdentifier,
  36746. reusedAliasDeclarations: new Set(),
  36747. updatedImports: new Map(),
  36748. };
  36749. }
  36750. /** Adds a side-effect import for the given module. */
  36751. addSideEffectImport(requestedFile, moduleSpecifier) {
  36752. if (this.config.rewriter !== null) {
  36753. moduleSpecifier = this.config.rewriter.rewriteSpecifier(moduleSpecifier, requestedFile.fileName);
  36754. }
  36755. this._getNewImportsTrackerForFile(requestedFile).sideEffectImports.add(moduleSpecifier);
  36756. }
  36757. addImport(request) {
  36758. if (this.config.rewriter !== null) {
  36759. if (request.exportSymbolName !== null) {
  36760. request.exportSymbolName = this.config.rewriter.rewriteSymbol(request.exportSymbolName, request.exportModuleSpecifier);
  36761. }
  36762. request.exportModuleSpecifier = this.config.rewriter.rewriteSpecifier(request.exportModuleSpecifier, request.requestedFile.fileName);
  36763. }
  36764. // Remove the newly-added import from the set of removed imports.
  36765. if (request.exportSymbolName !== null && !request.asTypeReference) {
  36766. this.removedImports
  36767. .get(request.requestedFile)
  36768. ?.get(request.exportModuleSpecifier)
  36769. ?.delete(request.exportSymbolName);
  36770. }
  36771. // Attempt to re-use previous identical import requests.
  36772. const previousGeneratedImportRef = attemptToReuseGeneratedImports(this.reuseGeneratedImportsTracker, request);
  36773. if (previousGeneratedImportRef !== null) {
  36774. return createImportReference(!!request.asTypeReference, previousGeneratedImportRef);
  36775. }
  36776. // Generate a new one, and cache it.
  36777. const resultImportRef = this._generateNewImport(request);
  36778. captureGeneratedImport(request, this.reuseGeneratedImportsTracker, resultImportRef);
  36779. return createImportReference(!!request.asTypeReference, resultImportRef);
  36780. }
  36781. /**
  36782. * Marks all imported symbols with a specific name for removal.
  36783. * Call `addImport` to undo this operation.
  36784. * @param requestedFile File from which to remove the imports.
  36785. * @param exportSymbolName Declared name of the symbol being removed.
  36786. * @param moduleSpecifier Module from which the symbol is being imported.
  36787. */
  36788. removeImport(requestedFile, exportSymbolName, moduleSpecifier) {
  36789. let moduleMap = this.removedImports.get(requestedFile);
  36790. if (!moduleMap) {
  36791. moduleMap = new Map();
  36792. this.removedImports.set(requestedFile, moduleMap);
  36793. }
  36794. let removedSymbols = moduleMap.get(moduleSpecifier);
  36795. if (!removedSymbols) {
  36796. removedSymbols = new Set();
  36797. moduleMap.set(moduleSpecifier, removedSymbols);
  36798. }
  36799. removedSymbols.add(exportSymbolName);
  36800. }
  36801. _generateNewImport(request) {
  36802. const { requestedFile: sourceFile } = request;
  36803. const disableOriginalSourceFileReuse = this.config.disableOriginalSourceFileReuse;
  36804. const forceGenerateNamespacesForNewImports = this.config.forceGenerateNamespacesForNewImports;
  36805. // If desired, attempt to re-use original source file imports as a base, or as much as possible.
  36806. // This may involve updates to existing import named bindings.
  36807. if (!disableOriginalSourceFileReuse) {
  36808. const reuseResult = attemptToReuseExistingSourceFileImports(this.reuseSourceFileImportsTracker, sourceFile, request);
  36809. if (reuseResult !== null) {
  36810. return reuseResult;
  36811. }
  36812. }
  36813. // A new import needs to be generated.
  36814. // No candidate existing import was found.
  36815. const { namedImports, namespaceImports } = this._getNewImportsTrackerForFile(sourceFile);
  36816. // If a namespace import is requested, or the symbol should be forcibly
  36817. // imported through namespace imports:
  36818. if (request.exportSymbolName === null || forceGenerateNamespacesForNewImports) {
  36819. let namespaceImportName = `${this.config.namespaceImportPrefix}${this.nextUniqueIndex++}`;
  36820. if (this.config.rewriter) {
  36821. namespaceImportName = this.config.rewriter.rewriteNamespaceImportIdentifier(namespaceImportName, request.exportModuleSpecifier);
  36822. }
  36823. const namespaceImport = ts.factory.createNamespaceImport(this.config.generateUniqueIdentifier(sourceFile, namespaceImportName) ??
  36824. ts.factory.createIdentifier(namespaceImportName));
  36825. namespaceImports.set(request.exportModuleSpecifier, namespaceImport);
  36826. // Capture the generated namespace import alone, to allow re-use.
  36827. captureGeneratedImport({ ...request, exportSymbolName: null }, this.reuseGeneratedImportsTracker, namespaceImport.name);
  36828. if (request.exportSymbolName !== null) {
  36829. return [namespaceImport.name, ts.factory.createIdentifier(request.exportSymbolName)];
  36830. }
  36831. return namespaceImport.name;
  36832. }
  36833. // Otherwise, an individual named import is requested.
  36834. if (!namedImports.has(request.exportModuleSpecifier)) {
  36835. namedImports.set(request.exportModuleSpecifier, []);
  36836. }
  36837. const exportSymbolName = ts.factory.createIdentifier(request.exportSymbolName);
  36838. const fileUniqueName = request.unsafeAliasOverride
  36839. ? null
  36840. : this.config.generateUniqueIdentifier(sourceFile, request.exportSymbolName);
  36841. let needsAlias;
  36842. let specifierName;
  36843. if (request.unsafeAliasOverride) {
  36844. needsAlias = true;
  36845. specifierName = ts.factory.createIdentifier(request.unsafeAliasOverride);
  36846. }
  36847. else if (fileUniqueName !== null) {
  36848. needsAlias = true;
  36849. specifierName = fileUniqueName;
  36850. }
  36851. else {
  36852. needsAlias = false;
  36853. specifierName = exportSymbolName;
  36854. }
  36855. namedImports
  36856. .get(request.exportModuleSpecifier)
  36857. .push(ts.factory.createImportSpecifier(false, needsAlias ? exportSymbolName : undefined, specifierName));
  36858. return specifierName;
  36859. }
  36860. /**
  36861. * Finalizes the import manager by computing all necessary import changes
  36862. * and returning them.
  36863. *
  36864. * Changes are collected once at the end, after all imports are requested,
  36865. * because this simplifies building up changes to existing imports that need
  36866. * to be updated, and allows more trivial re-use of previous generated imports.
  36867. */
  36868. finalize() {
  36869. const affectedFiles = new Set();
  36870. const updatedImportsResult = new Map();
  36871. const newImportsResult = new Map();
  36872. const deletedImports = new Set();
  36873. const importDeclarationsPerFile = new Map();
  36874. const addNewImport = (fileName, importDecl) => {
  36875. affectedFiles.add(fileName);
  36876. if (newImportsResult.has(fileName)) {
  36877. newImportsResult.get(fileName).push(importDecl);
  36878. }
  36879. else {
  36880. newImportsResult.set(fileName, [importDecl]);
  36881. }
  36882. };
  36883. // Collect original source file imports that need to be updated.
  36884. this.reuseSourceFileImportsTracker.updatedImports.forEach((expressions, importDecl) => {
  36885. const sourceFile = importDecl.getSourceFile();
  36886. const namedBindings = importDecl.importClause.namedBindings;
  36887. const moduleName = importDecl.moduleSpecifier.text;
  36888. const newElements = namedBindings.elements
  36889. .concat(expressions.map(({ propertyName, fileUniqueAlias }) => ts.factory.createImportSpecifier(false, fileUniqueAlias !== null ? propertyName : undefined, fileUniqueAlias ?? propertyName)))
  36890. .filter((specifier) => this._canAddSpecifier(sourceFile, moduleName, specifier));
  36891. affectedFiles.add(sourceFile.fileName);
  36892. if (newElements.length === 0) {
  36893. deletedImports.add(importDecl);
  36894. }
  36895. else {
  36896. updatedImportsResult.set(namedBindings, ts.factory.updateNamedImports(namedBindings, newElements));
  36897. }
  36898. });
  36899. this.removedImports.forEach((removeMap, sourceFile) => {
  36900. if (removeMap.size === 0) {
  36901. return;
  36902. }
  36903. let allImports = importDeclarationsPerFile.get(sourceFile);
  36904. if (!allImports) {
  36905. allImports = sourceFile.statements.filter(ts.isImportDeclaration);
  36906. importDeclarationsPerFile.set(sourceFile, allImports);
  36907. }
  36908. for (const node of allImports) {
  36909. if (!node.importClause?.namedBindings ||
  36910. !ts.isNamedImports(node.importClause.namedBindings) ||
  36911. this.reuseSourceFileImportsTracker.updatedImports.has(node) ||
  36912. deletedImports.has(node)) {
  36913. continue;
  36914. }
  36915. const namedBindings = node.importClause.namedBindings;
  36916. const moduleName = node.moduleSpecifier.text;
  36917. const newImports = namedBindings.elements.filter((specifier) => this._canAddSpecifier(sourceFile, moduleName, specifier));
  36918. if (newImports.length === 0) {
  36919. affectedFiles.add(sourceFile.fileName);
  36920. deletedImports.add(node);
  36921. }
  36922. else if (newImports.length !== namedBindings.elements.length) {
  36923. affectedFiles.add(sourceFile.fileName);
  36924. updatedImportsResult.set(namedBindings, ts.factory.updateNamedImports(namedBindings, newImports));
  36925. }
  36926. }
  36927. });
  36928. // Collect all new imports to be added. Named imports, namespace imports or side-effects.
  36929. this.newImports.forEach(({ namedImports, namespaceImports, sideEffectImports }, sourceFile) => {
  36930. const useSingleQuotes = this.config.shouldUseSingleQuotes(sourceFile);
  36931. const fileName = sourceFile.fileName;
  36932. sideEffectImports.forEach((moduleName) => {
  36933. addNewImport(fileName, ts.factory.createImportDeclaration(undefined, undefined, ts.factory.createStringLiteral(moduleName)));
  36934. });
  36935. namespaceImports.forEach((namespaceImport, moduleName) => {
  36936. const newImport = ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(false, undefined, namespaceImport), ts.factory.createStringLiteral(moduleName, useSingleQuotes));
  36937. // IMPORTANT: Set the original TS node to the `ts.ImportDeclaration`. This allows
  36938. // downstream transforms such as tsickle to properly process references to this import.
  36939. //
  36940. // This operation is load-bearing in g3 as some imported modules contain special metadata
  36941. // generated by clutz, which tsickle uses to transform imports and references to those
  36942. // imports. See: `google3: node_modules/tsickle/src/googmodule.ts;l=637-640;rcl=615418148`
  36943. ts.setOriginalNode(namespaceImport.name, newImport);
  36944. addNewImport(fileName, newImport);
  36945. });
  36946. namedImports.forEach((specifiers, moduleName) => {
  36947. const filteredSpecifiers = specifiers.filter((specifier) => this._canAddSpecifier(sourceFile, moduleName, specifier));
  36948. if (filteredSpecifiers.length > 0) {
  36949. const newImport = ts.factory.createImportDeclaration(undefined, ts.factory.createImportClause(false, undefined, ts.factory.createNamedImports(filteredSpecifiers)), ts.factory.createStringLiteral(moduleName, useSingleQuotes));
  36950. addNewImport(fileName, newImport);
  36951. }
  36952. });
  36953. });
  36954. return {
  36955. affectedFiles,
  36956. newImports: newImportsResult,
  36957. updatedImports: updatedImportsResult,
  36958. reusedOriginalAliasDeclarations: this.reuseSourceFileImportsTracker.reusedAliasDeclarations,
  36959. deletedImports,
  36960. };
  36961. }
  36962. /**
  36963. * Gets a TypeScript transform for the import manager.
  36964. *
  36965. * @param extraStatementsMap Additional set of statements to be inserted
  36966. * for given source files after their imports. E.g. top-level constants.
  36967. */
  36968. toTsTransform(extraStatementsMap) {
  36969. return createTsTransformForImportManager(this, extraStatementsMap);
  36970. }
  36971. /**
  36972. * Transforms a single file as a shorthand, using {@link toTsTransform}.
  36973. *
  36974. * @param extraStatementsMap Additional set of statements to be inserted
  36975. * for given source files after their imports. E.g. top-level constants.
  36976. */
  36977. transformTsFile(ctx, file, extraStatementsAfterImports) {
  36978. const extraStatementsMap = extraStatementsAfterImports
  36979. ? new Map([[file.fileName, extraStatementsAfterImports]])
  36980. : undefined;
  36981. return this.toTsTransform(extraStatementsMap)(ctx)(file);
  36982. }
  36983. _getNewImportsTrackerForFile(file) {
  36984. if (!this.newImports.has(file)) {
  36985. this.newImports.set(file, {
  36986. namespaceImports: new Map(),
  36987. namedImports: new Map(),
  36988. sideEffectImports: new Set(),
  36989. });
  36990. }
  36991. return this.newImports.get(file);
  36992. }
  36993. _canAddSpecifier(sourceFile, moduleSpecifier, specifier) {
  36994. return !this.removedImports
  36995. .get(sourceFile)
  36996. ?.get(moduleSpecifier)
  36997. ?.has((specifier.propertyName || specifier.name).text);
  36998. }
  36999. }
  37000. /** Creates an import reference based on the given identifier, or nested access. */
  37001. function createImportReference(asTypeReference, ref) {
  37002. if (asTypeReference) {
  37003. return Array.isArray(ref) ? ts.factory.createQualifiedName(ref[0], ref[1]) : ref;
  37004. }
  37005. else {
  37006. return Array.isArray(ref) ? ts.factory.createPropertyAccessExpression(ref[0], ref[1]) : ref;
  37007. }
  37008. }
  37009. const UNARY_OPERATORS$1 = new Map([
  37010. [UnaryOperator.Minus, '-'],
  37011. [UnaryOperator.Plus, '+'],
  37012. ]);
  37013. const BINARY_OPERATORS$1 = new Map([
  37014. [BinaryOperator.And, '&&'],
  37015. [BinaryOperator.Bigger, '>'],
  37016. [BinaryOperator.BiggerEquals, '>='],
  37017. [BinaryOperator.BitwiseAnd, '&'],
  37018. [BinaryOperator.BitwiseOr, '|'],
  37019. [BinaryOperator.Divide, '/'],
  37020. [BinaryOperator.Equals, '=='],
  37021. [BinaryOperator.Identical, '==='],
  37022. [BinaryOperator.Lower, '<'],
  37023. [BinaryOperator.LowerEquals, '<='],
  37024. [BinaryOperator.Minus, '-'],
  37025. [BinaryOperator.Modulo, '%'],
  37026. [BinaryOperator.Multiply, '*'],
  37027. [BinaryOperator.NotEquals, '!='],
  37028. [BinaryOperator.NotIdentical, '!=='],
  37029. [BinaryOperator.Or, '||'],
  37030. [BinaryOperator.Plus, '+'],
  37031. [BinaryOperator.NullishCoalesce, '??'],
  37032. ]);
  37033. class ExpressionTranslatorVisitor {
  37034. factory;
  37035. imports;
  37036. contextFile;
  37037. downlevelTaggedTemplates;
  37038. downlevelVariableDeclarations;
  37039. recordWrappedNode;
  37040. constructor(factory, imports, contextFile, options) {
  37041. this.factory = factory;
  37042. this.imports = imports;
  37043. this.contextFile = contextFile;
  37044. this.downlevelTaggedTemplates = options.downlevelTaggedTemplates === true;
  37045. this.downlevelVariableDeclarations = options.downlevelVariableDeclarations === true;
  37046. this.recordWrappedNode = options.recordWrappedNode || (() => { });
  37047. }
  37048. visitDeclareVarStmt(stmt, context) {
  37049. const varType = this.downlevelVariableDeclarations
  37050. ? 'var'
  37051. : stmt.hasModifier(exports.StmtModifier.Final)
  37052. ? 'const'
  37053. : 'let';
  37054. return this.attachComments(this.factory.createVariableDeclaration(stmt.name, stmt.value?.visitExpression(this, context.withExpressionMode), varType), stmt.leadingComments);
  37055. }
  37056. visitDeclareFunctionStmt(stmt, context) {
  37057. return this.attachComments(this.factory.createFunctionDeclaration(stmt.name, stmt.params.map((param) => param.name), this.factory.createBlock(this.visitStatements(stmt.statements, context.withStatementMode))), stmt.leadingComments);
  37058. }
  37059. visitExpressionStmt(stmt, context) {
  37060. return this.attachComments(this.factory.createExpressionStatement(stmt.expr.visitExpression(this, context.withStatementMode)), stmt.leadingComments);
  37061. }
  37062. visitReturnStmt(stmt, context) {
  37063. return this.attachComments(this.factory.createReturnStatement(stmt.value.visitExpression(this, context.withExpressionMode)), stmt.leadingComments);
  37064. }
  37065. visitIfStmt(stmt, context) {
  37066. return this.attachComments(this.factory.createIfStatement(stmt.condition.visitExpression(this, context), this.factory.createBlock(this.visitStatements(stmt.trueCase, context.withStatementMode)), stmt.falseCase.length > 0
  37067. ? this.factory.createBlock(this.visitStatements(stmt.falseCase, context.withStatementMode))
  37068. : null), stmt.leadingComments);
  37069. }
  37070. visitReadVarExpr(ast, _context) {
  37071. const identifier = this.factory.createIdentifier(ast.name);
  37072. this.setSourceMapRange(identifier, ast.sourceSpan);
  37073. return identifier;
  37074. }
  37075. visitWriteVarExpr(expr, context) {
  37076. const assignment = this.factory.createAssignment(this.setSourceMapRange(this.factory.createIdentifier(expr.name), expr.sourceSpan), expr.value.visitExpression(this, context));
  37077. return context.isStatement
  37078. ? assignment
  37079. : this.factory.createParenthesizedExpression(assignment);
  37080. }
  37081. visitWriteKeyExpr(expr, context) {
  37082. const exprContext = context.withExpressionMode;
  37083. const target = this.factory.createElementAccess(expr.receiver.visitExpression(this, exprContext), expr.index.visitExpression(this, exprContext));
  37084. const assignment = this.factory.createAssignment(target, expr.value.visitExpression(this, exprContext));
  37085. return context.isStatement
  37086. ? assignment
  37087. : this.factory.createParenthesizedExpression(assignment);
  37088. }
  37089. visitWritePropExpr(expr, context) {
  37090. const target = this.factory.createPropertyAccess(expr.receiver.visitExpression(this, context), expr.name);
  37091. return this.factory.createAssignment(target, expr.value.visitExpression(this, context));
  37092. }
  37093. visitInvokeFunctionExpr(ast, context) {
  37094. return this.setSourceMapRange(this.factory.createCallExpression(ast.fn.visitExpression(this, context), ast.args.map((arg) => arg.visitExpression(this, context)), ast.pure), ast.sourceSpan);
  37095. }
  37096. visitTaggedTemplateLiteralExpr(ast, context) {
  37097. return this.setSourceMapRange(this.createTaggedTemplateExpression(ast.tag.visitExpression(this, context), this.getTemplateLiteralFromAst(ast.template, context)), ast.sourceSpan);
  37098. }
  37099. visitTemplateLiteralExpr(ast, context) {
  37100. return this.setSourceMapRange(this.factory.createTemplateLiteral(this.getTemplateLiteralFromAst(ast, context)), ast.sourceSpan);
  37101. }
  37102. visitInstantiateExpr(ast, context) {
  37103. return this.factory.createNewExpression(ast.classExpr.visitExpression(this, context), ast.args.map((arg) => arg.visitExpression(this, context)));
  37104. }
  37105. visitLiteralExpr(ast, _context) {
  37106. return this.setSourceMapRange(this.factory.createLiteral(ast.value), ast.sourceSpan);
  37107. }
  37108. visitLocalizedString(ast, context) {
  37109. // A `$localize` message consists of `messageParts` and `expressions`, which get interleaved
  37110. // together. The interleaved pieces look like:
  37111. // `[messagePart0, expression0, messagePart1, expression1, messagePart2]`
  37112. //
  37113. // Note that there is always a message part at the start and end, and so therefore
  37114. // `messageParts.length === expressions.length + 1`.
  37115. //
  37116. // Each message part may be prefixed with "metadata", which is wrapped in colons (:) delimiters.
  37117. // The metadata is attached to the first and subsequent message parts by calls to
  37118. // `serializeI18nHead()` and `serializeI18nTemplatePart()` respectively.
  37119. //
  37120. // The first message part (i.e. `ast.messageParts[0]`) is used to initialize `messageParts`
  37121. // array.
  37122. const elements = [createTemplateElement(ast.serializeI18nHead())];
  37123. const expressions = [];
  37124. for (let i = 0; i < ast.expressions.length; i++) {
  37125. const placeholder = this.setSourceMapRange(ast.expressions[i].visitExpression(this, context), ast.getPlaceholderSourceSpan(i));
  37126. expressions.push(placeholder);
  37127. elements.push(createTemplateElement(ast.serializeI18nTemplatePart(i + 1)));
  37128. }
  37129. const localizeTag = this.factory.createIdentifier('$localize');
  37130. return this.setSourceMapRange(this.createTaggedTemplateExpression(localizeTag, { elements, expressions }), ast.sourceSpan);
  37131. }
  37132. createTaggedTemplateExpression(tag, template) {
  37133. return this.downlevelTaggedTemplates
  37134. ? this.createES5TaggedTemplateFunctionCall(tag, template)
  37135. : this.factory.createTaggedTemplate(tag, template);
  37136. }
  37137. /**
  37138. * Translate the tagged template literal into a call that is compatible with ES5, using the
  37139. * imported `__makeTemplateObject` helper for ES5 formatted output.
  37140. */
  37141. createES5TaggedTemplateFunctionCall(tagHandler, { elements, expressions }) {
  37142. // Ensure that the `__makeTemplateObject()` helper has been imported.
  37143. const __makeTemplateObjectHelper = this.imports.addImport({
  37144. exportModuleSpecifier: 'tslib',
  37145. exportSymbolName: '__makeTemplateObject',
  37146. requestedFile: this.contextFile,
  37147. });
  37148. // Collect up the cooked and raw strings into two separate arrays.
  37149. const cooked = [];
  37150. const raw = [];
  37151. for (const element of elements) {
  37152. cooked.push(this.factory.setSourceMapRange(this.factory.createLiteral(element.cooked), element.range));
  37153. raw.push(this.factory.setSourceMapRange(this.factory.createLiteral(element.raw), element.range));
  37154. }
  37155. // Generate the helper call in the form: `__makeTemplateObject([cooked], [raw]);`
  37156. const templateHelperCall = this.factory.createCallExpression(__makeTemplateObjectHelper, [this.factory.createArrayLiteral(cooked), this.factory.createArrayLiteral(raw)],
  37157. /* pure */ false);
  37158. // Finally create the tagged handler call in the form:
  37159. // `tag(__makeTemplateObject([cooked], [raw]), ...expressions);`
  37160. return this.factory.createCallExpression(tagHandler, [templateHelperCall, ...expressions],
  37161. /* pure */ false);
  37162. }
  37163. visitExternalExpr(ast, _context) {
  37164. if (ast.value.name === null) {
  37165. if (ast.value.moduleName === null) {
  37166. throw new Error('Invalid import without name nor moduleName');
  37167. }
  37168. return this.imports.addImport({
  37169. exportModuleSpecifier: ast.value.moduleName,
  37170. exportSymbolName: null,
  37171. requestedFile: this.contextFile,
  37172. });
  37173. }
  37174. // If a moduleName is specified, this is a normal import. If there's no module name, it's a
  37175. // reference to a global/ambient symbol.
  37176. if (ast.value.moduleName !== null) {
  37177. // This is a normal import. Find the imported module.
  37178. return this.imports.addImport({
  37179. exportModuleSpecifier: ast.value.moduleName,
  37180. exportSymbolName: ast.value.name,
  37181. requestedFile: this.contextFile,
  37182. });
  37183. }
  37184. else {
  37185. // The symbol is ambient, so just reference it.
  37186. return this.factory.createIdentifier(ast.value.name);
  37187. }
  37188. }
  37189. visitConditionalExpr(ast, context) {
  37190. let cond = ast.condition.visitExpression(this, context);
  37191. // Ordinarily the ternary operator is right-associative. The following are equivalent:
  37192. // `a ? b : c ? d : e` => `a ? b : (c ? d : e)`
  37193. //
  37194. // However, occasionally Angular needs to produce a left-associative conditional, such as in
  37195. // the case of a null-safe navigation production: `{{a?.b ? c : d}}`. This template produces
  37196. // a ternary of the form:
  37197. // `a == null ? null : rest of expression`
  37198. // If the rest of the expression is also a ternary though, this would produce the form:
  37199. // `a == null ? null : a.b ? c : d`
  37200. // which, if left as right-associative, would be incorrectly associated as:
  37201. // `a == null ? null : (a.b ? c : d)`
  37202. //
  37203. // In such cases, the left-associativity needs to be enforced with parentheses:
  37204. // `(a == null ? null : a.b) ? c : d`
  37205. //
  37206. // Such parentheses could always be included in the condition (guaranteeing correct behavior) in
  37207. // all cases, but this has a code size cost. Instead, parentheses are added only when a
  37208. // conditional expression is directly used as the condition of another.
  37209. //
  37210. // TODO(alxhub): investigate better logic for precendence of conditional operators
  37211. if (ast.condition instanceof ConditionalExpr) {
  37212. // The condition of this ternary needs to be wrapped in parentheses to maintain
  37213. // left-associativity.
  37214. cond = this.factory.createParenthesizedExpression(cond);
  37215. }
  37216. return this.factory.createConditional(cond, ast.trueCase.visitExpression(this, context), ast.falseCase.visitExpression(this, context));
  37217. }
  37218. visitDynamicImportExpr(ast, context) {
  37219. const urlExpression = typeof ast.url === 'string'
  37220. ? this.factory.createLiteral(ast.url)
  37221. : ast.url.visitExpression(this, context);
  37222. if (ast.urlComment) {
  37223. this.factory.attachComments(urlExpression, [leadingComment(ast.urlComment, true)]);
  37224. }
  37225. return this.factory.createDynamicImport(urlExpression);
  37226. }
  37227. visitNotExpr(ast, context) {
  37228. return this.factory.createUnaryExpression('!', ast.condition.visitExpression(this, context));
  37229. }
  37230. visitFunctionExpr(ast, context) {
  37231. return this.factory.createFunctionExpression(ast.name ?? null, ast.params.map((param) => param.name), this.factory.createBlock(this.visitStatements(ast.statements, context)));
  37232. }
  37233. visitArrowFunctionExpr(ast, context) {
  37234. return this.factory.createArrowFunctionExpression(ast.params.map((param) => param.name), Array.isArray(ast.body)
  37235. ? this.factory.createBlock(this.visitStatements(ast.body, context))
  37236. : ast.body.visitExpression(this, context));
  37237. }
  37238. visitBinaryOperatorExpr(ast, context) {
  37239. if (!BINARY_OPERATORS$1.has(ast.operator)) {
  37240. throw new Error(`Unknown binary operator: ${BinaryOperator[ast.operator]}`);
  37241. }
  37242. return this.factory.createBinaryExpression(ast.lhs.visitExpression(this, context), BINARY_OPERATORS$1.get(ast.operator), ast.rhs.visitExpression(this, context));
  37243. }
  37244. visitReadPropExpr(ast, context) {
  37245. return this.factory.createPropertyAccess(ast.receiver.visitExpression(this, context), ast.name);
  37246. }
  37247. visitReadKeyExpr(ast, context) {
  37248. return this.factory.createElementAccess(ast.receiver.visitExpression(this, context), ast.index.visitExpression(this, context));
  37249. }
  37250. visitLiteralArrayExpr(ast, context) {
  37251. return this.factory.createArrayLiteral(ast.entries.map((expr) => this.setSourceMapRange(expr.visitExpression(this, context), ast.sourceSpan)));
  37252. }
  37253. visitLiteralMapExpr(ast, context) {
  37254. const properties = ast.entries.map((entry) => {
  37255. return {
  37256. propertyName: entry.key,
  37257. quoted: entry.quoted,
  37258. value: entry.value.visitExpression(this, context),
  37259. };
  37260. });
  37261. return this.setSourceMapRange(this.factory.createObjectLiteral(properties), ast.sourceSpan);
  37262. }
  37263. visitCommaExpr(ast, context) {
  37264. throw new Error('Method not implemented.');
  37265. }
  37266. visitTemplateLiteralElementExpr(ast, context) {
  37267. throw new Error('Method not implemented');
  37268. }
  37269. visitWrappedNodeExpr(ast, _context) {
  37270. this.recordWrappedNode(ast);
  37271. return ast.node;
  37272. }
  37273. visitTypeofExpr(ast, context) {
  37274. return this.factory.createTypeOfExpression(ast.expr.visitExpression(this, context));
  37275. }
  37276. visitUnaryOperatorExpr(ast, context) {
  37277. if (!UNARY_OPERATORS$1.has(ast.operator)) {
  37278. throw new Error(`Unknown unary operator: ${UnaryOperator[ast.operator]}`);
  37279. }
  37280. return this.factory.createUnaryExpression(UNARY_OPERATORS$1.get(ast.operator), ast.expr.visitExpression(this, context));
  37281. }
  37282. visitStatements(statements, context) {
  37283. return statements
  37284. .map((stmt) => stmt.visitStatement(this, context))
  37285. .filter((stmt) => stmt !== undefined);
  37286. }
  37287. setSourceMapRange(ast, span) {
  37288. return this.factory.setSourceMapRange(ast, createRange(span));
  37289. }
  37290. attachComments(statement, leadingComments) {
  37291. if (leadingComments !== undefined) {
  37292. this.factory.attachComments(statement, leadingComments);
  37293. }
  37294. return statement;
  37295. }
  37296. getTemplateLiteralFromAst(ast, context) {
  37297. return {
  37298. elements: ast.elements.map((e) => createTemplateElement({
  37299. cooked: e.text,
  37300. raw: e.rawText,
  37301. range: e.sourceSpan ?? ast.sourceSpan,
  37302. })),
  37303. expressions: ast.expressions.map((e) => e.visitExpression(this, context)),
  37304. };
  37305. }
  37306. }
  37307. /**
  37308. * Convert a cooked-raw string object into one that can be used by the AST factories.
  37309. */
  37310. function createTemplateElement({ cooked, raw, range, }) {
  37311. return { cooked, raw, range: createRange(range) };
  37312. }
  37313. /**
  37314. * Convert an OutputAST source-span into a range that can be used by the AST factories.
  37315. */
  37316. function createRange(span) {
  37317. if (span === null) {
  37318. return null;
  37319. }
  37320. const { start, end } = span;
  37321. const { url, content } = start.file;
  37322. if (!url) {
  37323. return null;
  37324. }
  37325. return {
  37326. url,
  37327. content,
  37328. start: { offset: start.offset, line: start.line, column: start.col },
  37329. end: { offset: end.offset, line: end.line, column: end.col },
  37330. };
  37331. }
  37332. const INELIGIBLE = {};
  37333. /**
  37334. * Determines whether the provided type can be emitted, which means that it can be safely emitted
  37335. * into a different location.
  37336. *
  37337. * If this function returns true, a `TypeEmitter` should be able to succeed. Vice versa, if this
  37338. * function returns false, then using the `TypeEmitter` should not be attempted as it is known to
  37339. * fail.
  37340. */
  37341. function canEmitType(type, canEmit) {
  37342. return canEmitTypeWorker(type);
  37343. function canEmitTypeWorker(type) {
  37344. return visitNode(type) !== INELIGIBLE;
  37345. }
  37346. // To determine whether a type can be emitted, we have to recursively look through all type nodes.
  37347. // If an unsupported type node is found at any position within the type, then the `INELIGIBLE`
  37348. // constant is returned to stop the recursive walk as the type as a whole cannot be emitted in
  37349. // that case. Otherwise, the result of visiting all child nodes determines the result. If no
  37350. // ineligible type reference node is found then the walk returns `undefined`, indicating that
  37351. // no type node was visited that could not be emitted.
  37352. function visitNode(node) {
  37353. // `import('module')` type nodes are not supported, as it may require rewriting the module
  37354. // specifier which is currently not done.
  37355. if (ts.isImportTypeNode(node)) {
  37356. return INELIGIBLE;
  37357. }
  37358. // Emitting a type reference node in a different context requires that an import for the type
  37359. // can be created. If a type reference node cannot be emitted, `INELIGIBLE` is returned to stop
  37360. // the walk.
  37361. if (ts.isTypeReferenceNode(node) && !canEmitTypeReference(node)) {
  37362. return INELIGIBLE;
  37363. }
  37364. else {
  37365. return ts.forEachChild(node, visitNode);
  37366. }
  37367. }
  37368. function canEmitTypeReference(type) {
  37369. if (!canEmit(type)) {
  37370. return false;
  37371. }
  37372. // The type can be emitted if either it does not have any type arguments, or all of them can be
  37373. // emitted.
  37374. return type.typeArguments === undefined || type.typeArguments.every(canEmitTypeWorker);
  37375. }
  37376. }
  37377. /**
  37378. * Given a `ts.TypeNode`, this class derives an equivalent `ts.TypeNode` that has been emitted into
  37379. * a different context.
  37380. *
  37381. * For example, consider the following code:
  37382. *
  37383. * ```ts
  37384. * import {NgIterable} from '@angular/core';
  37385. *
  37386. * class NgForOf<T, U extends NgIterable<T>> {}
  37387. * ```
  37388. *
  37389. * Here, the generic type parameters `T` and `U` can be emitted into a different context, as the
  37390. * type reference to `NgIterable` originates from an absolute module import so that it can be
  37391. * emitted anywhere, using that same module import. The process of emitting translates the
  37392. * `NgIterable` type reference to a type reference that is valid in the context in which it is
  37393. * emitted, for example:
  37394. *
  37395. * ```ts
  37396. * import * as i0 from '@angular/core';
  37397. * import * as i1 from '@angular/common';
  37398. *
  37399. * const _ctor1: <T, U extends i0.NgIterable<T>>(o: Pick<i1.NgForOf<T, U>, 'ngForOf'>):
  37400. * i1.NgForOf<T, U>;
  37401. * ```
  37402. *
  37403. * Notice how the type reference for `NgIterable` has been translated into a qualified name,
  37404. * referring to the namespace import that was created.
  37405. */
  37406. class TypeEmitter {
  37407. translator;
  37408. constructor(translator) {
  37409. this.translator = translator;
  37410. }
  37411. emitType(type) {
  37412. const typeReferenceTransformer = (context) => {
  37413. const visitNode = (node) => {
  37414. if (ts.isImportTypeNode(node)) {
  37415. throw new Error('Unable to emit import type');
  37416. }
  37417. if (ts.isTypeReferenceNode(node)) {
  37418. return this.emitTypeReference(node);
  37419. }
  37420. else if (ts.isLiteralExpression(node)) {
  37421. // TypeScript would typically take the emit text for a literal expression from the source
  37422. // file itself. As the type node is being emitted into a different file, however,
  37423. // TypeScript would extract the literal text from the wrong source file. To mitigate this
  37424. // issue the literal is cloned and explicitly marked as synthesized by setting its text
  37425. // range to a negative range, forcing TypeScript to determine the node's literal text from
  37426. // the synthesized node's text instead of the incorrect source file.
  37427. let clone;
  37428. if (ts.isStringLiteral(node)) {
  37429. clone = ts.factory.createStringLiteral(node.text);
  37430. }
  37431. else if (ts.isNumericLiteral(node)) {
  37432. clone = ts.factory.createNumericLiteral(node.text);
  37433. }
  37434. else if (ts.isBigIntLiteral(node)) {
  37435. clone = ts.factory.createBigIntLiteral(node.text);
  37436. }
  37437. else if (ts.isNoSubstitutionTemplateLiteral(node)) {
  37438. clone = ts.factory.createNoSubstitutionTemplateLiteral(node.text, node.rawText);
  37439. }
  37440. else if (ts.isRegularExpressionLiteral(node)) {
  37441. clone = ts.factory.createRegularExpressionLiteral(node.text);
  37442. }
  37443. else {
  37444. throw new Error(`Unsupported literal kind ${ts.SyntaxKind[node.kind]}`);
  37445. }
  37446. ts.setTextRange(clone, { pos: -1, end: -1 });
  37447. return clone;
  37448. }
  37449. else {
  37450. return ts.visitEachChild(node, visitNode, context);
  37451. }
  37452. };
  37453. return (node) => ts.visitNode(node, visitNode, ts.isTypeNode);
  37454. };
  37455. return ts.transform(type, [typeReferenceTransformer]).transformed[0];
  37456. }
  37457. emitTypeReference(type) {
  37458. // Determine the reference that the type corresponds with.
  37459. const translatedType = this.translator(type);
  37460. if (translatedType === null) {
  37461. throw new Error('Unable to emit an unresolved reference');
  37462. }
  37463. // Emit the type arguments, if any.
  37464. let typeArguments = undefined;
  37465. if (type.typeArguments !== undefined) {
  37466. typeArguments = ts.factory.createNodeArray(type.typeArguments.map((typeArg) => this.emitType(typeArg)));
  37467. }
  37468. return ts.factory.updateTypeReferenceNode(type, translatedType.typeName, typeArguments);
  37469. }
  37470. }
  37471. /*!
  37472. * @license
  37473. * Copyright Google LLC All Rights Reserved.
  37474. *
  37475. * Use of this source code is governed by an MIT-style license that can be
  37476. * found in the LICENSE file at https://angular.dev/license
  37477. */
  37478. /**
  37479. * Creates a TypeScript node representing a numeric value.
  37480. */
  37481. function tsNumericExpression$1(value) {
  37482. // As of TypeScript 5.3 negative numbers are represented as `prefixUnaryOperator` and passing a
  37483. // negative number (even as a string) into `createNumericLiteral` will result in an error.
  37484. if (value < 0) {
  37485. const operand = ts.factory.createNumericLiteral(Math.abs(value));
  37486. return ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, operand);
  37487. }
  37488. return ts.factory.createNumericLiteral(value);
  37489. }
  37490. function translateType(type, contextFile, reflector, refEmitter, imports) {
  37491. return type.visitType(new TypeTranslatorVisitor(imports, contextFile, reflector, refEmitter), new Context$1(false));
  37492. }
  37493. class TypeTranslatorVisitor {
  37494. imports;
  37495. contextFile;
  37496. reflector;
  37497. refEmitter;
  37498. constructor(imports, contextFile, reflector, refEmitter) {
  37499. this.imports = imports;
  37500. this.contextFile = contextFile;
  37501. this.reflector = reflector;
  37502. this.refEmitter = refEmitter;
  37503. }
  37504. visitBuiltinType(type, context) {
  37505. switch (type.name) {
  37506. case BuiltinTypeName.Bool:
  37507. return ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword);
  37508. case BuiltinTypeName.Dynamic:
  37509. return ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
  37510. case BuiltinTypeName.Int:
  37511. case BuiltinTypeName.Number:
  37512. return ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword);
  37513. case BuiltinTypeName.String:
  37514. return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
  37515. case BuiltinTypeName.None:
  37516. return ts.factory.createKeywordTypeNode(ts.SyntaxKind.NeverKeyword);
  37517. default:
  37518. throw new Error(`Unsupported builtin type: ${BuiltinTypeName[type.name]}`);
  37519. }
  37520. }
  37521. visitExpressionType(type, context) {
  37522. const typeNode = this.translateExpression(type.value, context);
  37523. if (type.typeParams === null) {
  37524. return typeNode;
  37525. }
  37526. if (!ts.isTypeReferenceNode(typeNode)) {
  37527. throw new Error('An ExpressionType with type arguments must translate into a TypeReferenceNode');
  37528. }
  37529. else if (typeNode.typeArguments !== undefined) {
  37530. throw new Error(`An ExpressionType with type arguments cannot have multiple levels of type arguments`);
  37531. }
  37532. const typeArgs = type.typeParams.map((param) => this.translateType(param, context));
  37533. return ts.factory.createTypeReferenceNode(typeNode.typeName, typeArgs);
  37534. }
  37535. visitArrayType(type, context) {
  37536. return ts.factory.createArrayTypeNode(this.translateType(type.of, context));
  37537. }
  37538. visitMapType(type, context) {
  37539. const parameter = ts.factory.createParameterDeclaration(undefined, undefined, 'key', undefined, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword));
  37540. const typeArgs = type.valueType !== null
  37541. ? this.translateType(type.valueType, context)
  37542. : ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword);
  37543. const indexSignature = ts.factory.createIndexSignature(undefined, [parameter], typeArgs);
  37544. return ts.factory.createTypeLiteralNode([indexSignature]);
  37545. }
  37546. visitTransplantedType(ast, context) {
  37547. const node = ast.type instanceof Reference ? ast.type.node : ast.type;
  37548. if (!ts.isTypeNode(node)) {
  37549. throw new Error(`A TransplantedType must wrap a TypeNode`);
  37550. }
  37551. const viaModule = ast.type instanceof Reference ? ast.type.bestGuessOwningModule : null;
  37552. const emitter = new TypeEmitter((typeRef) => this.translateTypeReference(typeRef, context, viaModule));
  37553. return emitter.emitType(node);
  37554. }
  37555. visitReadVarExpr(ast, context) {
  37556. if (ast.name === null) {
  37557. throw new Error(`ReadVarExpr with no variable name in type`);
  37558. }
  37559. return ts.factory.createTypeQueryNode(ts.factory.createIdentifier(ast.name));
  37560. }
  37561. visitWriteVarExpr(expr, context) {
  37562. throw new Error('Method not implemented.');
  37563. }
  37564. visitWriteKeyExpr(expr, context) {
  37565. throw new Error('Method not implemented.');
  37566. }
  37567. visitWritePropExpr(expr, context) {
  37568. throw new Error('Method not implemented.');
  37569. }
  37570. visitInvokeFunctionExpr(ast, context) {
  37571. throw new Error('Method not implemented.');
  37572. }
  37573. visitTaggedTemplateLiteralExpr(ast, context) {
  37574. throw new Error('Method not implemented.');
  37575. }
  37576. visitTemplateLiteralExpr(ast, context) {
  37577. throw new Error('Method not implemented.');
  37578. }
  37579. visitTemplateLiteralElementExpr(ast, context) {
  37580. throw new Error('Method not implemented.');
  37581. }
  37582. visitInstantiateExpr(ast, context) {
  37583. throw new Error('Method not implemented.');
  37584. }
  37585. visitLiteralExpr(ast, context) {
  37586. if (ast.value === null) {
  37587. return ts.factory.createLiteralTypeNode(ts.factory.createNull());
  37588. }
  37589. else if (ast.value === undefined) {
  37590. return ts.factory.createKeywordTypeNode(ts.SyntaxKind.UndefinedKeyword);
  37591. }
  37592. else if (typeof ast.value === 'boolean') {
  37593. return ts.factory.createLiteralTypeNode(ast.value ? ts.factory.createTrue() : ts.factory.createFalse());
  37594. }
  37595. else if (typeof ast.value === 'number') {
  37596. return ts.factory.createLiteralTypeNode(tsNumericExpression$1(ast.value));
  37597. }
  37598. else {
  37599. return ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(ast.value));
  37600. }
  37601. }
  37602. visitLocalizedString(ast, context) {
  37603. throw new Error('Method not implemented.');
  37604. }
  37605. visitExternalExpr(ast, context) {
  37606. if (ast.value.moduleName === null || ast.value.name === null) {
  37607. throw new Error(`Import unknown module or symbol`);
  37608. }
  37609. const typeName = this.imports.addImport({
  37610. exportModuleSpecifier: ast.value.moduleName,
  37611. exportSymbolName: ast.value.name,
  37612. requestedFile: this.contextFile,
  37613. asTypeReference: true,
  37614. });
  37615. const typeArguments = ast.typeParams !== null
  37616. ? ast.typeParams.map((type) => this.translateType(type, context))
  37617. : undefined;
  37618. return ts.factory.createTypeReferenceNode(typeName, typeArguments);
  37619. }
  37620. visitConditionalExpr(ast, context) {
  37621. throw new Error('Method not implemented.');
  37622. }
  37623. visitDynamicImportExpr(ast, context) {
  37624. throw new Error('Method not implemented.');
  37625. }
  37626. visitNotExpr(ast, context) {
  37627. throw new Error('Method not implemented.');
  37628. }
  37629. visitFunctionExpr(ast, context) {
  37630. throw new Error('Method not implemented.');
  37631. }
  37632. visitArrowFunctionExpr(ast, context) {
  37633. throw new Error('Method not implemented.');
  37634. }
  37635. visitUnaryOperatorExpr(ast, context) {
  37636. throw new Error('Method not implemented.');
  37637. }
  37638. visitBinaryOperatorExpr(ast, context) {
  37639. throw new Error('Method not implemented.');
  37640. }
  37641. visitReadPropExpr(ast, context) {
  37642. throw new Error('Method not implemented.');
  37643. }
  37644. visitReadKeyExpr(ast, context) {
  37645. throw new Error('Method not implemented.');
  37646. }
  37647. visitLiteralArrayExpr(ast, context) {
  37648. const values = ast.entries.map((expr) => this.translateExpression(expr, context));
  37649. return ts.factory.createTupleTypeNode(values);
  37650. }
  37651. visitLiteralMapExpr(ast, context) {
  37652. const entries = ast.entries.map((entry) => {
  37653. const { key, quoted } = entry;
  37654. const type = this.translateExpression(entry.value, context);
  37655. return ts.factory.createPropertySignature(
  37656. /* modifiers */ undefined,
  37657. /* name */ quoted ? ts.factory.createStringLiteral(key) : key,
  37658. /* questionToken */ undefined,
  37659. /* type */ type);
  37660. });
  37661. return ts.factory.createTypeLiteralNode(entries);
  37662. }
  37663. visitCommaExpr(ast, context) {
  37664. throw new Error('Method not implemented.');
  37665. }
  37666. visitWrappedNodeExpr(ast, context) {
  37667. const node = ast.node;
  37668. if (ts.isEntityName(node)) {
  37669. return ts.factory.createTypeReferenceNode(node, /* typeArguments */ undefined);
  37670. }
  37671. else if (ts.isTypeNode(node)) {
  37672. return node;
  37673. }
  37674. else if (ts.isLiteralExpression(node)) {
  37675. return ts.factory.createLiteralTypeNode(node);
  37676. }
  37677. else {
  37678. throw new Error(`Unsupported WrappedNodeExpr in TypeTranslatorVisitor: ${ts.SyntaxKind[node.kind]}`);
  37679. }
  37680. }
  37681. visitTypeofExpr(ast, context) {
  37682. const typeNode = this.translateExpression(ast.expr, context);
  37683. if (!ts.isTypeReferenceNode(typeNode)) {
  37684. throw new Error(`The target of a typeof expression must be a type reference, but it was
  37685. ${ts.SyntaxKind[typeNode.kind]}`);
  37686. }
  37687. return ts.factory.createTypeQueryNode(typeNode.typeName);
  37688. }
  37689. translateType(type, context) {
  37690. const typeNode = type.visitType(this, context);
  37691. if (!ts.isTypeNode(typeNode)) {
  37692. throw new Error(`A Type must translate to a TypeNode, but was ${ts.SyntaxKind[typeNode.kind]}`);
  37693. }
  37694. return typeNode;
  37695. }
  37696. translateExpression(expr, context) {
  37697. const typeNode = expr.visitExpression(this, context);
  37698. if (!ts.isTypeNode(typeNode)) {
  37699. throw new Error(`An Expression must translate to a TypeNode, but was ${ts.SyntaxKind[typeNode.kind]}`);
  37700. }
  37701. return typeNode;
  37702. }
  37703. translateTypeReference(type, context, viaModule) {
  37704. const target = ts.isIdentifier(type.typeName) ? type.typeName : type.typeName.right;
  37705. const declaration = this.reflector.getDeclarationOfIdentifier(target);
  37706. if (declaration === null) {
  37707. throw new Error(`Unable to statically determine the declaration file of type node ${target.text}`);
  37708. }
  37709. let owningModule = viaModule;
  37710. if (typeof declaration.viaModule === 'string') {
  37711. owningModule = {
  37712. specifier: declaration.viaModule,
  37713. resolutionContext: type.getSourceFile().fileName,
  37714. };
  37715. }
  37716. const reference = new Reference(declaration.node, declaration.viaModule === AmbientImport ? AmbientImport : owningModule);
  37717. const emittedType = this.refEmitter.emit(reference, this.contextFile, exports.ImportFlags.NoAliasing | exports.ImportFlags.AllowTypeImports | exports.ImportFlags.AllowAmbientReferences);
  37718. assertSuccessfulReferenceEmit(emittedType, target, 'type');
  37719. const typeNode = this.translateExpression(emittedType.expression, context);
  37720. if (!ts.isTypeReferenceNode(typeNode)) {
  37721. throw new Error(`Expected TypeReferenceNode for emitted reference, got ${ts.SyntaxKind[typeNode.kind]}.`);
  37722. }
  37723. return typeNode;
  37724. }
  37725. }
  37726. /**
  37727. * Different optimizers use different annotations on a function or method call to indicate its pure
  37728. * status.
  37729. */
  37730. var PureAnnotation;
  37731. (function (PureAnnotation) {
  37732. /**
  37733. * Closure's annotation for purity is `@pureOrBreakMyCode`, but this needs to be in a semantic
  37734. * (jsdoc) enabled comment. Thus, the actual comment text for Closure must include the `*` that
  37735. * turns a `/*` comment into a `/**` comment, as well as surrounding whitespace.
  37736. */
  37737. PureAnnotation["CLOSURE"] = "* @pureOrBreakMyCode ";
  37738. PureAnnotation["TERSER"] = "@__PURE__";
  37739. })(PureAnnotation || (PureAnnotation = {}));
  37740. const UNARY_OPERATORS = {
  37741. '+': ts.SyntaxKind.PlusToken,
  37742. '-': ts.SyntaxKind.MinusToken,
  37743. '!': ts.SyntaxKind.ExclamationToken,
  37744. };
  37745. const BINARY_OPERATORS = {
  37746. '&&': ts.SyntaxKind.AmpersandAmpersandToken,
  37747. '>': ts.SyntaxKind.GreaterThanToken,
  37748. '>=': ts.SyntaxKind.GreaterThanEqualsToken,
  37749. '&': ts.SyntaxKind.AmpersandToken,
  37750. '|': ts.SyntaxKind.BarToken,
  37751. '/': ts.SyntaxKind.SlashToken,
  37752. '==': ts.SyntaxKind.EqualsEqualsToken,
  37753. '===': ts.SyntaxKind.EqualsEqualsEqualsToken,
  37754. '<': ts.SyntaxKind.LessThanToken,
  37755. '<=': ts.SyntaxKind.LessThanEqualsToken,
  37756. '-': ts.SyntaxKind.MinusToken,
  37757. '%': ts.SyntaxKind.PercentToken,
  37758. '*': ts.SyntaxKind.AsteriskToken,
  37759. '!=': ts.SyntaxKind.ExclamationEqualsToken,
  37760. '!==': ts.SyntaxKind.ExclamationEqualsEqualsToken,
  37761. '||': ts.SyntaxKind.BarBarToken,
  37762. '+': ts.SyntaxKind.PlusToken,
  37763. '??': ts.SyntaxKind.QuestionQuestionToken,
  37764. };
  37765. const VAR_TYPES = {
  37766. 'const': ts.NodeFlags.Const,
  37767. 'let': ts.NodeFlags.Let,
  37768. 'var': ts.NodeFlags.None,
  37769. };
  37770. /**
  37771. * A TypeScript flavoured implementation of the AstFactory.
  37772. */
  37773. class TypeScriptAstFactory {
  37774. annotateForClosureCompiler;
  37775. externalSourceFiles = new Map();
  37776. constructor(annotateForClosureCompiler) {
  37777. this.annotateForClosureCompiler = annotateForClosureCompiler;
  37778. }
  37779. attachComments = attachComments;
  37780. createArrayLiteral = ts.factory.createArrayLiteralExpression;
  37781. createAssignment(target, value) {
  37782. return ts.factory.createBinaryExpression(target, ts.SyntaxKind.EqualsToken, value);
  37783. }
  37784. createBinaryExpression(leftOperand, operator, rightOperand) {
  37785. return ts.factory.createBinaryExpression(leftOperand, BINARY_OPERATORS[operator], rightOperand);
  37786. }
  37787. createBlock(body) {
  37788. return ts.factory.createBlock(body);
  37789. }
  37790. createCallExpression(callee, args, pure) {
  37791. const call = ts.factory.createCallExpression(callee, undefined, args);
  37792. if (pure) {
  37793. ts.addSyntheticLeadingComment(call, ts.SyntaxKind.MultiLineCommentTrivia, this.annotateForClosureCompiler ? PureAnnotation.CLOSURE : PureAnnotation.TERSER,
  37794. /* trailing newline */ false);
  37795. }
  37796. return call;
  37797. }
  37798. createConditional(condition, whenTrue, whenFalse) {
  37799. return ts.factory.createConditionalExpression(condition, undefined, whenTrue, undefined, whenFalse);
  37800. }
  37801. createElementAccess = ts.factory.createElementAccessExpression;
  37802. createExpressionStatement = ts.factory.createExpressionStatement;
  37803. createDynamicImport(url) {
  37804. return ts.factory.createCallExpression(ts.factory.createToken(ts.SyntaxKind.ImportKeyword),
  37805. /* type */ undefined, [typeof url === 'string' ? ts.factory.createStringLiteral(url) : url]);
  37806. }
  37807. createFunctionDeclaration(functionName, parameters, body) {
  37808. if (!ts.isBlock(body)) {
  37809. throw new Error(`Invalid syntax, expected a block, but got ${ts.SyntaxKind[body.kind]}.`);
  37810. }
  37811. return ts.factory.createFunctionDeclaration(undefined, undefined, functionName, undefined, parameters.map((param) => ts.factory.createParameterDeclaration(undefined, undefined, param)), undefined, body);
  37812. }
  37813. createFunctionExpression(functionName, parameters, body) {
  37814. if (!ts.isBlock(body)) {
  37815. throw new Error(`Invalid syntax, expected a block, but got ${ts.SyntaxKind[body.kind]}.`);
  37816. }
  37817. return ts.factory.createFunctionExpression(undefined, undefined, functionName ?? undefined, undefined, parameters.map((param) => ts.factory.createParameterDeclaration(undefined, undefined, param)), undefined, body);
  37818. }
  37819. createArrowFunctionExpression(parameters, body) {
  37820. if (ts.isStatement(body) && !ts.isBlock(body)) {
  37821. throw new Error(`Invalid syntax, expected a block, but got ${ts.SyntaxKind[body.kind]}.`);
  37822. }
  37823. return ts.factory.createArrowFunction(undefined, undefined, parameters.map((param) => ts.factory.createParameterDeclaration(undefined, undefined, param)), undefined, undefined, body);
  37824. }
  37825. createIdentifier = ts.factory.createIdentifier;
  37826. createIfStatement(condition, thenStatement, elseStatement) {
  37827. return ts.factory.createIfStatement(condition, thenStatement, elseStatement ?? undefined);
  37828. }
  37829. createLiteral(value) {
  37830. if (value === undefined) {
  37831. return ts.factory.createIdentifier('undefined');
  37832. }
  37833. else if (value === null) {
  37834. return ts.factory.createNull();
  37835. }
  37836. else if (typeof value === 'boolean') {
  37837. return value ? ts.factory.createTrue() : ts.factory.createFalse();
  37838. }
  37839. else if (typeof value === 'number') {
  37840. return tsNumericExpression$1(value);
  37841. }
  37842. else {
  37843. return ts.factory.createStringLiteral(value);
  37844. }
  37845. }
  37846. createNewExpression(expression, args) {
  37847. return ts.factory.createNewExpression(expression, undefined, args);
  37848. }
  37849. createObjectLiteral(properties) {
  37850. return ts.factory.createObjectLiteralExpression(properties.map((prop) => ts.factory.createPropertyAssignment(prop.quoted
  37851. ? ts.factory.createStringLiteral(prop.propertyName)
  37852. : ts.factory.createIdentifier(prop.propertyName), prop.value)));
  37853. }
  37854. createParenthesizedExpression = ts.factory.createParenthesizedExpression;
  37855. createPropertyAccess = ts.factory.createPropertyAccessExpression;
  37856. createReturnStatement(expression) {
  37857. return ts.factory.createReturnStatement(expression ?? undefined);
  37858. }
  37859. createTaggedTemplate(tag, template) {
  37860. return ts.factory.createTaggedTemplateExpression(tag, undefined, this.createTemplateLiteral(template));
  37861. }
  37862. createTemplateLiteral(template) {
  37863. let templateLiteral;
  37864. const length = template.elements.length;
  37865. const head = template.elements[0];
  37866. if (length === 1) {
  37867. templateLiteral = ts.factory.createNoSubstitutionTemplateLiteral(head.cooked, head.raw);
  37868. }
  37869. else {
  37870. const spans = [];
  37871. // Create the middle parts
  37872. for (let i = 1; i < length - 1; i++) {
  37873. const { cooked, raw, range } = template.elements[i];
  37874. const middle = createTemplateMiddle(cooked, raw);
  37875. if (range !== null) {
  37876. this.setSourceMapRange(middle, range);
  37877. }
  37878. spans.push(ts.factory.createTemplateSpan(template.expressions[i - 1], middle));
  37879. }
  37880. // Create the tail part
  37881. const resolvedExpression = template.expressions[length - 2];
  37882. const templatePart = template.elements[length - 1];
  37883. const templateTail = createTemplateTail(templatePart.cooked, templatePart.raw);
  37884. if (templatePart.range !== null) {
  37885. this.setSourceMapRange(templateTail, templatePart.range);
  37886. }
  37887. spans.push(ts.factory.createTemplateSpan(resolvedExpression, templateTail));
  37888. // Put it all together
  37889. templateLiteral = ts.factory.createTemplateExpression(ts.factory.createTemplateHead(head.cooked, head.raw), spans);
  37890. }
  37891. if (head.range !== null) {
  37892. this.setSourceMapRange(templateLiteral, head.range);
  37893. }
  37894. return templateLiteral;
  37895. }
  37896. createThrowStatement = ts.factory.createThrowStatement;
  37897. createTypeOfExpression = ts.factory.createTypeOfExpression;
  37898. createUnaryExpression(operator, operand) {
  37899. return ts.factory.createPrefixUnaryExpression(UNARY_OPERATORS[operator], operand);
  37900. }
  37901. createVariableDeclaration(variableName, initializer, type) {
  37902. return ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList([
  37903. ts.factory.createVariableDeclaration(variableName, undefined, undefined, initializer ?? undefined),
  37904. ], VAR_TYPES[type]));
  37905. }
  37906. setSourceMapRange(node, sourceMapRange) {
  37907. if (sourceMapRange === null) {
  37908. return node;
  37909. }
  37910. const url = sourceMapRange.url;
  37911. if (!this.externalSourceFiles.has(url)) {
  37912. this.externalSourceFiles.set(url, ts.createSourceMapSource(url, sourceMapRange.content, (pos) => pos));
  37913. }
  37914. const source = this.externalSourceFiles.get(url);
  37915. ts.setSourceMapRange(node, {
  37916. pos: sourceMapRange.start.offset,
  37917. end: sourceMapRange.end.offset,
  37918. source,
  37919. });
  37920. return node;
  37921. }
  37922. }
  37923. // HACK: Use this in place of `ts.createTemplateMiddle()`.
  37924. // Revert once https://github.com/microsoft/TypeScript/issues/35374 is fixed.
  37925. function createTemplateMiddle(cooked, raw) {
  37926. const node = ts.factory.createTemplateHead(cooked, raw);
  37927. node.kind = ts.SyntaxKind.TemplateMiddle;
  37928. return node;
  37929. }
  37930. // HACK: Use this in place of `ts.createTemplateTail()`.
  37931. // Revert once https://github.com/microsoft/TypeScript/issues/35374 is fixed.
  37932. function createTemplateTail(cooked, raw) {
  37933. const node = ts.factory.createTemplateHead(cooked, raw);
  37934. node.kind = ts.SyntaxKind.TemplateTail;
  37935. return node;
  37936. }
  37937. /**
  37938. * Attach the given `leadingComments` to the `statement` node.
  37939. *
  37940. * @param statement The statement that will have comments attached.
  37941. * @param leadingComments The comments to attach to the statement.
  37942. */
  37943. function attachComments(statement, leadingComments) {
  37944. for (const comment of leadingComments) {
  37945. const commentKind = comment.multiline
  37946. ? ts.SyntaxKind.MultiLineCommentTrivia
  37947. : ts.SyntaxKind.SingleLineCommentTrivia;
  37948. if (comment.multiline) {
  37949. ts.addSyntheticLeadingComment(statement, commentKind, comment.toString(), comment.trailingNewline);
  37950. }
  37951. else {
  37952. for (const line of comment.toString().split('\n')) {
  37953. ts.addSyntheticLeadingComment(statement, commentKind, line, comment.trailingNewline);
  37954. }
  37955. }
  37956. }
  37957. }
  37958. function translateExpression(contextFile, expression, imports, options = {}) {
  37959. return expression.visitExpression(new ExpressionTranslatorVisitor(new TypeScriptAstFactory(options.annotateForClosureCompiler === true), imports, contextFile, options), new Context$1(false));
  37960. }
  37961. function translateStatement(contextFile, statement, imports, options = {}) {
  37962. return statement.visitStatement(new ExpressionTranslatorVisitor(new TypeScriptAstFactory(options.annotateForClosureCompiler === true), imports, contextFile, options), new Context$1(true));
  37963. }
  37964. /**
  37965. * Create a `ts.Diagnostic` which indicates the given class is part of the declarations of two or
  37966. * more NgModules.
  37967. *
  37968. * The resulting `ts.Diagnostic` will have a context entry for each NgModule showing the point where
  37969. * the directive/pipe exists in its `declarations` (if possible).
  37970. */
  37971. function makeDuplicateDeclarationError(node, data, kind) {
  37972. const context = [];
  37973. for (const decl of data) {
  37974. if (decl.rawDeclarations === null) {
  37975. continue;
  37976. }
  37977. // Try to find the reference to the declaration within the declarations array, to hang the
  37978. // error there. If it can't be found, fall back on using the NgModule's name.
  37979. const contextNode = decl.ref.getOriginForDiagnostics(decl.rawDeclarations, decl.ngModule.name);
  37980. context.push(makeRelatedInformation(contextNode, `'${node.name.text}' is listed in the declarations of the NgModule '${decl.ngModule.name.text}'.`));
  37981. }
  37982. // Finally, produce the diagnostic.
  37983. return makeDiagnostic(exports.ErrorCode.NGMODULE_DECLARATION_NOT_UNIQUE, node.name, `The ${kind} '${node.name.text}' is declared by more than one NgModule.`, context);
  37984. }
  37985. /**
  37986. * Creates a `FatalDiagnosticError` for a node that did not evaluate to the expected type. The
  37987. * diagnostic that is created will include details on why the value is incorrect, i.e. it includes
  37988. * a representation of the actual type that was unsupported, or in the case of a dynamic value the
  37989. * trace to the node where the dynamic value originated.
  37990. *
  37991. * @param node The node for which the diagnostic should be produced.
  37992. * @param value The evaluated value that has the wrong type.
  37993. * @param messageText The message text of the error.
  37994. */
  37995. function createValueHasWrongTypeError(node, value, messageText) {
  37996. let chainedMessage;
  37997. let relatedInformation;
  37998. if (value instanceof DynamicValue) {
  37999. chainedMessage = 'Value could not be determined statically.';
  38000. relatedInformation = traceDynamicValue(node, value);
  38001. }
  38002. else if (value instanceof Reference) {
  38003. const target = value.debugName !== null ? `'${value.debugName}'` : 'an anonymous declaration';
  38004. chainedMessage = `Value is a reference to ${target}.`;
  38005. const referenceNode = identifierOfNode(value.node) ?? value.node;
  38006. relatedInformation = [makeRelatedInformation(referenceNode, 'Reference is declared here.')];
  38007. }
  38008. else {
  38009. chainedMessage = `Value is of type '${describeResolvedType(value)}'.`;
  38010. }
  38011. const chain = {
  38012. messageText,
  38013. category: ts.DiagnosticCategory.Error,
  38014. code: 0,
  38015. next: [
  38016. {
  38017. messageText: chainedMessage,
  38018. category: ts.DiagnosticCategory.Message,
  38019. code: 0,
  38020. },
  38021. ],
  38022. };
  38023. return new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, node, chain, relatedInformation);
  38024. }
  38025. /**
  38026. * Gets the diagnostics for a set of provider classes.
  38027. * @param providerClasses Classes that should be checked.
  38028. * @param providersDeclaration Node that declares the providers array.
  38029. * @param registry Registry that keeps track of the registered injectable classes.
  38030. */
  38031. function getProviderDiagnostics(providerClasses, providersDeclaration, registry) {
  38032. const diagnostics = [];
  38033. for (const provider of providerClasses) {
  38034. const injectableMeta = registry.getInjectableMeta(provider.node);
  38035. if (injectableMeta !== null) {
  38036. // The provided type is recognized as injectable, so we don't report a diagnostic for this
  38037. // provider.
  38038. continue;
  38039. }
  38040. const contextNode = provider.getOriginForDiagnostics(providersDeclaration);
  38041. diagnostics.push(makeDiagnostic(exports.ErrorCode.UNDECORATED_PROVIDER, contextNode, `The class '${provider.node.name.text}' cannot be created via dependency injection, as it does not have an Angular decorator. This will result in an error at runtime.
  38042. Either add the @Injectable() decorator to '${provider.node.name.text}', or configure a different provider (such as a provider with 'useFactory').
  38043. `, [makeRelatedInformation(provider.node, `'${provider.node.name.text}' is declared here.`)]));
  38044. }
  38045. return diagnostics;
  38046. }
  38047. function getDirectiveDiagnostics(node, injectableRegistry, evaluator, reflector, scopeRegistry, strictInjectionParameters, kind) {
  38048. let diagnostics = [];
  38049. const addDiagnostics = (more) => {
  38050. if (more === null) {
  38051. return;
  38052. }
  38053. else if (diagnostics === null) {
  38054. diagnostics = Array.isArray(more) ? more : [more];
  38055. }
  38056. else if (Array.isArray(more)) {
  38057. diagnostics.push(...more);
  38058. }
  38059. else {
  38060. diagnostics.push(more);
  38061. }
  38062. };
  38063. const duplicateDeclarations = scopeRegistry.getDuplicateDeclarations(node);
  38064. if (duplicateDeclarations !== null) {
  38065. addDiagnostics(makeDuplicateDeclarationError(node, duplicateDeclarations, kind));
  38066. }
  38067. addDiagnostics(checkInheritanceOfInjectable(node, injectableRegistry, reflector, evaluator, strictInjectionParameters, kind));
  38068. return diagnostics;
  38069. }
  38070. function validateHostDirectives(origin, hostDirectives, metaReader) {
  38071. const diagnostics = [];
  38072. for (const current of hostDirectives) {
  38073. if (!isHostDirectiveMetaForGlobalMode(current)) {
  38074. throw new Error('Impossible state: diagnostics code path for local compilation');
  38075. }
  38076. const hostMeta = flattenInheritedDirectiveMetadata(metaReader, current.directive);
  38077. if (hostMeta === null) {
  38078. diagnostics.push(makeDiagnostic(exports.ErrorCode.HOST_DIRECTIVE_INVALID, current.directive.getOriginForDiagnostics(origin), `${current.directive.debugName} must be a standalone directive to be used as a host directive`));
  38079. continue;
  38080. }
  38081. if (!hostMeta.isStandalone) {
  38082. diagnostics.push(makeDiagnostic(exports.ErrorCode.HOST_DIRECTIVE_NOT_STANDALONE, current.directive.getOriginForDiagnostics(origin), `Host directive ${hostMeta.name} must be standalone`));
  38083. }
  38084. if (hostMeta.isComponent) {
  38085. diagnostics.push(makeDiagnostic(exports.ErrorCode.HOST_DIRECTIVE_COMPONENT, current.directive.getOriginForDiagnostics(origin), `Host directive ${hostMeta.name} cannot be a component`));
  38086. }
  38087. const requiredInputNames = Array.from(hostMeta.inputs)
  38088. .filter((input) => input.required)
  38089. .map((input) => input.classPropertyName);
  38090. validateHostDirectiveMappings('input', current, hostMeta, origin, diagnostics, requiredInputNames.length > 0 ? new Set(requiredInputNames) : null);
  38091. validateHostDirectiveMappings('output', current, hostMeta, origin, diagnostics, null);
  38092. }
  38093. return diagnostics;
  38094. }
  38095. function validateHostDirectiveMappings(bindingType, hostDirectiveMeta, meta, origin, diagnostics, requiredBindings) {
  38096. if (!isHostDirectiveMetaForGlobalMode(hostDirectiveMeta)) {
  38097. throw new Error('Impossible state: diagnostics code path for local compilation');
  38098. }
  38099. const className = meta.name;
  38100. const hostDirectiveMappings = bindingType === 'input' ? hostDirectiveMeta.inputs : hostDirectiveMeta.outputs;
  38101. const existingBindings = bindingType === 'input' ? meta.inputs : meta.outputs;
  38102. const exposedRequiredBindings = new Set();
  38103. for (const publicName in hostDirectiveMappings) {
  38104. if (hostDirectiveMappings.hasOwnProperty(publicName)) {
  38105. const bindings = existingBindings.getByBindingPropertyName(publicName);
  38106. if (bindings === null) {
  38107. diagnostics.push(makeDiagnostic(exports.ErrorCode.HOST_DIRECTIVE_UNDEFINED_BINDING, hostDirectiveMeta.directive.getOriginForDiagnostics(origin), `Directive ${className} does not have an ${bindingType} with a public name of ${publicName}.`));
  38108. }
  38109. else if (requiredBindings !== null) {
  38110. for (const field of bindings) {
  38111. if (requiredBindings.has(field.classPropertyName)) {
  38112. exposedRequiredBindings.add(field.classPropertyName);
  38113. }
  38114. }
  38115. }
  38116. const remappedPublicName = hostDirectiveMappings[publicName];
  38117. const bindingsForPublicName = existingBindings.getByBindingPropertyName(remappedPublicName);
  38118. if (bindingsForPublicName !== null) {
  38119. for (const binding of bindingsForPublicName) {
  38120. if (binding.bindingPropertyName !== publicName) {
  38121. diagnostics.push(makeDiagnostic(exports.ErrorCode.HOST_DIRECTIVE_CONFLICTING_ALIAS, hostDirectiveMeta.directive.getOriginForDiagnostics(origin), `Cannot alias ${bindingType} ${publicName} of host directive ${className} to ${remappedPublicName}, because it already has a different ${bindingType} with the same public name.`));
  38122. }
  38123. }
  38124. }
  38125. }
  38126. }
  38127. if (requiredBindings !== null && requiredBindings.size !== exposedRequiredBindings.size) {
  38128. const missingBindings = [];
  38129. for (const publicName of requiredBindings) {
  38130. if (!exposedRequiredBindings.has(publicName)) {
  38131. const name = existingBindings.getByClassPropertyName(publicName);
  38132. if (name) {
  38133. missingBindings.push(`'${name.bindingPropertyName}'`);
  38134. }
  38135. }
  38136. }
  38137. diagnostics.push(makeDiagnostic(exports.ErrorCode.HOST_DIRECTIVE_MISSING_REQUIRED_BINDING, hostDirectiveMeta.directive.getOriginForDiagnostics(origin), `Required ${bindingType}${missingBindings.length === 1 ? '' : 's'} ${missingBindings.join(', ')} from host directive ${className} must be exposed.`));
  38138. }
  38139. }
  38140. function getUndecoratedClassWithAngularFeaturesDiagnostic(node) {
  38141. return makeDiagnostic(exports.ErrorCode.UNDECORATED_CLASS_USING_ANGULAR_FEATURES, node.name, `Class is using Angular features but is not decorated. Please add an explicit ` +
  38142. `Angular decorator.`);
  38143. }
  38144. function checkInheritanceOfInjectable(node, injectableRegistry, reflector, evaluator, strictInjectionParameters, kind) {
  38145. const classWithCtor = findInheritedCtor(node, injectableRegistry, reflector, evaluator);
  38146. if (classWithCtor === null || classWithCtor.isCtorValid) {
  38147. // The class does not inherit a constructor, or the inherited constructor is compatible
  38148. // with DI; no need to report a diagnostic.
  38149. return null;
  38150. }
  38151. if (!classWithCtor.isDecorated) {
  38152. // The inherited constructor exists in a class that does not have an Angular decorator.
  38153. // This is an error, as there won't be a factory definition available for DI to invoke
  38154. // the constructor.
  38155. return getInheritedUndecoratedCtorDiagnostic(node, classWithCtor.ref, kind);
  38156. }
  38157. if (isFromDtsFile(classWithCtor.ref.node)) {
  38158. // The inherited class is declared in a declaration file, in which case there is not enough
  38159. // information to detect invalid constructors as `@Inject()` metadata is not present in the
  38160. // declaration file. Consequently, we have to accept such occurrences, although they might
  38161. // still fail at runtime.
  38162. return null;
  38163. }
  38164. if (!strictInjectionParameters || isAbstractClassDeclaration(node)) {
  38165. // An invalid constructor is only reported as error under `strictInjectionParameters` and
  38166. // only for concrete classes; follow the same exclusions for derived types.
  38167. return null;
  38168. }
  38169. return getInheritedInvalidCtorDiagnostic(node, classWithCtor.ref, kind);
  38170. }
  38171. function findInheritedCtor(node, injectableRegistry, reflector, evaluator) {
  38172. if (!reflector.isClass(node) || reflector.getConstructorParameters(node) !== null) {
  38173. // We should skip nodes that aren't classes. If a constructor exists, then no base class
  38174. // definition is required on the runtime side - it's legal to inherit from any class.
  38175. return null;
  38176. }
  38177. // The extends clause is an expression which can be as dynamic as the user wants. Try to
  38178. // evaluate it, but fall back on ignoring the clause if it can't be understood. This is a View
  38179. // Engine compatibility hack: View Engine ignores 'extends' expressions that it cannot understand.
  38180. let baseClass = readBaseClass(node, reflector, evaluator);
  38181. while (baseClass !== null) {
  38182. if (baseClass === 'dynamic') {
  38183. return null;
  38184. }
  38185. const injectableMeta = injectableRegistry.getInjectableMeta(baseClass.node);
  38186. if (injectableMeta !== null) {
  38187. if (injectableMeta.ctorDeps !== null) {
  38188. // The class has an Angular decorator with a constructor.
  38189. return {
  38190. ref: baseClass,
  38191. isCtorValid: injectableMeta.ctorDeps !== 'invalid',
  38192. isDecorated: true,
  38193. };
  38194. }
  38195. }
  38196. else {
  38197. const baseClassConstructorParams = reflector.getConstructorParameters(baseClass.node);
  38198. if (baseClassConstructorParams !== null) {
  38199. // The class is not decorated, but it does have constructor. An undecorated class is only
  38200. // allowed to have a constructor without parameters, otherwise it is invalid.
  38201. return {
  38202. ref: baseClass,
  38203. isCtorValid: baseClassConstructorParams.length === 0,
  38204. isDecorated: false,
  38205. };
  38206. }
  38207. }
  38208. // Go up the chain and continue
  38209. baseClass = readBaseClass(baseClass.node, reflector, evaluator);
  38210. }
  38211. return null;
  38212. }
  38213. function getInheritedInvalidCtorDiagnostic(node, baseClass, kind) {
  38214. const baseClassName = baseClass.debugName;
  38215. return makeDiagnostic(exports.ErrorCode.INJECTABLE_INHERITS_INVALID_CONSTRUCTOR, node.name, `The ${kind.toLowerCase()} ${node.name.text} inherits its constructor from ${baseClassName}, ` +
  38216. `but the latter has a constructor parameter that is not compatible with dependency injection. ` +
  38217. `Either add an explicit constructor to ${node.name.text} or change ${baseClassName}'s constructor to ` +
  38218. `use parameters that are valid for DI.`);
  38219. }
  38220. function getInheritedUndecoratedCtorDiagnostic(node, baseClass, kind) {
  38221. const baseClassName = baseClass.debugName;
  38222. const baseNeedsDecorator = kind === 'Component' || kind === 'Directive' ? 'Directive' : 'Injectable';
  38223. return makeDiagnostic(exports.ErrorCode.DIRECTIVE_INHERITS_UNDECORATED_CTOR, node.name, `The ${kind.toLowerCase()} ${node.name.text} inherits its constructor from ${baseClassName}, ` +
  38224. `but the latter does not have an Angular decorator of its own. Dependency injection will not be able to ` +
  38225. `resolve the parameters of ${baseClassName}'s constructor. Either add a @${baseNeedsDecorator} decorator ` +
  38226. `to ${baseClassName}, or add an explicit constructor to ${node.name.text}.`);
  38227. }
  38228. /**
  38229. * Throws `FatalDiagnosticError` with error code `LOCAL_COMPILATION_UNRESOLVED_CONST`
  38230. * if the compilation mode is local and the value is not resolved due to being imported
  38231. * from external files. This is a common scenario for errors in local compilation mode,
  38232. * and so this helper can be used to quickly generate the relevant errors.
  38233. *
  38234. * @param nodeToHighlight Node to be highlighted in teh error message.
  38235. * Will default to value.node if not provided.
  38236. */
  38237. function assertLocalCompilationUnresolvedConst(compilationMode, value, nodeToHighlight, errorMessage) {
  38238. if (compilationMode === exports.CompilationMode.LOCAL &&
  38239. value instanceof DynamicValue &&
  38240. value.isFromUnknownIdentifier()) {
  38241. throw new FatalDiagnosticError(exports.ErrorCode.LOCAL_COMPILATION_UNRESOLVED_CONST, nodeToHighlight ?? value.node, errorMessage);
  38242. }
  38243. }
  38244. exports.ComponentScopeKind = void 0;
  38245. (function (ComponentScopeKind) {
  38246. ComponentScopeKind[ComponentScopeKind["NgModule"] = 0] = "NgModule";
  38247. ComponentScopeKind[ComponentScopeKind["Standalone"] = 1] = "Standalone";
  38248. })(exports.ComponentScopeKind || (exports.ComponentScopeKind = {}));
  38249. /**
  38250. * Validates that the initializer member is compatible with the given class
  38251. * member in terms of field access and visibility.
  38252. *
  38253. * @throws {FatalDiagnosticError} If the recognized initializer API is
  38254. * incompatible.
  38255. */
  38256. function validateAccessOfInitializerApiMember({ api, call }, member) {
  38257. if (!api.allowedAccessLevels.includes(member.accessLevel)) {
  38258. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_DISALLOWED_MEMBER_VISIBILITY, call, makeDiagnosticChain(`Cannot use "${api.functionName}" on a class member that is declared as ${classMemberAccessLevelToString(member.accessLevel)}.`, [
  38259. makeDiagnosticChain(`Update the class field to be either: ` +
  38260. api.allowedAccessLevels.map((l) => classMemberAccessLevelToString(l)).join(', ')),
  38261. ]));
  38262. }
  38263. }
  38264. /**
  38265. * Attempts to identify an Angular initializer function call.
  38266. *
  38267. * Note that multiple possible initializer API function names can be specified,
  38268. * allowing for checking multiple types in one pass.
  38269. *
  38270. * @returns The parsed initializer API, or null if none was found.
  38271. */
  38272. function tryParseInitializerApi(functions, expression, reflector, importTracker) {
  38273. if (!ts.isCallExpression(expression)) {
  38274. return null;
  38275. }
  38276. const staticResult = parseTopLevelCall(expression, functions, importTracker) ||
  38277. parseTopLevelRequiredCall(expression, functions, importTracker) ||
  38278. parseTopLevelCallFromNamespace(expression, functions, importTracker);
  38279. if (staticResult === null) {
  38280. return null;
  38281. }
  38282. const { api, apiReference, isRequired } = staticResult;
  38283. // Once we've statically determined that the initializer is one of the APIs we're looking for, we
  38284. // need to verify it using the type checker which accounts for things like shadowed variables.
  38285. // This should be done as the absolute last step since using the type check can be expensive.
  38286. const resolvedImport = reflector.getImportOfIdentifier(apiReference);
  38287. if (resolvedImport === null ||
  38288. api.functionName !== resolvedImport.name ||
  38289. api.owningModule !== resolvedImport.from) {
  38290. return null;
  38291. }
  38292. return {
  38293. api,
  38294. call: expression,
  38295. isRequired,
  38296. };
  38297. }
  38298. /**
  38299. * Attempts to parse a top-level call to an initializer function,
  38300. * e.g. `prop = input()`. Returns null if it can't be parsed.
  38301. */
  38302. function parseTopLevelCall(call, functions, importTracker) {
  38303. const node = call.expression;
  38304. if (!ts.isIdentifier(node)) {
  38305. return null;
  38306. }
  38307. const matchingApi = functions.find((fn) => importTracker.isPotentialReferenceToNamedImport(node, fn.functionName, fn.owningModule));
  38308. if (matchingApi === undefined) {
  38309. return null;
  38310. }
  38311. return { api: matchingApi, apiReference: node, isRequired: false };
  38312. }
  38313. /**
  38314. * Attempts to parse a top-level call to a required initializer,
  38315. * e.g. `prop = input.required()`. Returns null if it can't be parsed.
  38316. */
  38317. function parseTopLevelRequiredCall(call, functions, importTracker) {
  38318. const node = call.expression;
  38319. if (!ts.isPropertyAccessExpression(node) ||
  38320. !ts.isIdentifier(node.expression) ||
  38321. node.name.text !== 'required') {
  38322. return null;
  38323. }
  38324. const expression = node.expression;
  38325. const matchingApi = functions.find((fn) => importTracker.isPotentialReferenceToNamedImport(expression, fn.functionName, fn.owningModule));
  38326. if (matchingApi === undefined) {
  38327. return null;
  38328. }
  38329. return { api: matchingApi, apiReference: expression, isRequired: true };
  38330. }
  38331. /**
  38332. * Attempts to parse a top-level call to a function referenced via a namespace import,
  38333. * e.g. `prop = core.input.required()`. Returns null if it can't be parsed.
  38334. */
  38335. function parseTopLevelCallFromNamespace(call, functions, importTracker) {
  38336. const node = call.expression;
  38337. if (!ts.isPropertyAccessExpression(node)) {
  38338. return null;
  38339. }
  38340. let apiReference = null;
  38341. let matchingApi = undefined;
  38342. let isRequired = false;
  38343. // `prop = core.input()`
  38344. if (ts.isIdentifier(node.expression) && ts.isIdentifier(node.name)) {
  38345. const namespaceRef = node.expression;
  38346. apiReference = node.name;
  38347. matchingApi = functions.find((fn) => node.name.text === fn.functionName &&
  38348. importTracker.isPotentialReferenceToNamespaceImport(namespaceRef, fn.owningModule));
  38349. }
  38350. else if (
  38351. // `prop = core.input.required()`
  38352. ts.isPropertyAccessExpression(node.expression) &&
  38353. ts.isIdentifier(node.expression.expression) &&
  38354. ts.isIdentifier(node.expression.name) &&
  38355. node.name.text === 'required') {
  38356. const potentialName = node.expression.name.text;
  38357. const namespaceRef = node.expression.expression;
  38358. apiReference = node.expression.name;
  38359. matchingApi = functions.find((fn) => fn.functionName === potentialName &&
  38360. importTracker.isPotentialReferenceToNamespaceImport(namespaceRef, fn.owningModule));
  38361. isRequired = true;
  38362. }
  38363. if (matchingApi === undefined || apiReference === null) {
  38364. return null;
  38365. }
  38366. return { api: matchingApi, apiReference, isRequired };
  38367. }
  38368. /**
  38369. * Parses and validates input and output initializer function options.
  38370. *
  38371. * This currently only parses the `alias` option and returns it. The other
  38372. * options for signal inputs are runtime constructs that aren't relevant at
  38373. * compile time.
  38374. */
  38375. function parseAndValidateInputAndOutputOptions(optionsNode) {
  38376. if (!ts.isObjectLiteralExpression(optionsNode)) {
  38377. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, optionsNode, 'Argument needs to be an object literal that is statically analyzable.');
  38378. }
  38379. const options = reflectObjectLiteral(optionsNode);
  38380. let alias = undefined;
  38381. if (options.has('alias')) {
  38382. const aliasExpr = options.get('alias');
  38383. if (!ts.isStringLiteralLike(aliasExpr)) {
  38384. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, aliasExpr, 'Alias needs to be a string that is statically analyzable.');
  38385. }
  38386. alias = aliasExpr.text;
  38387. }
  38388. return { alias };
  38389. }
  38390. /** Represents a function that can declare an input. */
  38391. const INPUT_INITIALIZER_FN = {
  38392. functionName: 'input',
  38393. owningModule: '@angular/core',
  38394. // Inputs are accessed from parents, via the `property` instruction.
  38395. // Conceptually, the fields need to be publicly readable, but in practice,
  38396. // accessing `protected` or `private` members works at runtime, so we can allow
  38397. // cases where the input is intentionally not part of the public API, programmatically.
  38398. // Note: `private` is omitted intentionally as this would be a conceptual confusion point.
  38399. allowedAccessLevels: [
  38400. exports.ClassMemberAccessLevel.PublicWritable,
  38401. exports.ClassMemberAccessLevel.PublicReadonly,
  38402. exports.ClassMemberAccessLevel.Protected,
  38403. ],
  38404. };
  38405. /**
  38406. * Attempts to parse a signal input class member. Returns the parsed
  38407. * input mapping if possible.
  38408. */
  38409. function tryParseSignalInputMapping(member, reflector, importTracker) {
  38410. if (member.value === null) {
  38411. return null;
  38412. }
  38413. const signalInput = tryParseInitializerApi([INPUT_INITIALIZER_FN], member.value, reflector, importTracker);
  38414. if (signalInput === null) {
  38415. return null;
  38416. }
  38417. validateAccessOfInitializerApiMember(signalInput, member);
  38418. const optionsNode = (signalInput.isRequired ? signalInput.call.arguments[0] : signalInput.call.arguments[1]);
  38419. const options = optionsNode !== undefined ? parseAndValidateInputAndOutputOptions(optionsNode) : null;
  38420. const classPropertyName = member.name;
  38421. return {
  38422. isSignal: true,
  38423. classPropertyName,
  38424. bindingPropertyName: options?.alias ?? classPropertyName,
  38425. required: signalInput.isRequired,
  38426. // Signal inputs do not capture complex transform metadata.
  38427. // See more details in the `transform` type of `InputMapping`.
  38428. transform: null,
  38429. };
  38430. }
  38431. /** Represents a function that can declare a model. */
  38432. const MODEL_INITIALIZER_FN = {
  38433. functionName: 'model',
  38434. owningModule: '@angular/core',
  38435. // Inputs are accessed from parents, via the `property` instruction.
  38436. // Conceptually, the fields need to be publicly readable, but in practice,
  38437. // accessing `protected` or `private` members works at runtime, so we can allow
  38438. // cases where the input is intentionally not part of the public API, programmatically.
  38439. allowedAccessLevels: [
  38440. exports.ClassMemberAccessLevel.PublicWritable,
  38441. exports.ClassMemberAccessLevel.PublicReadonly,
  38442. exports.ClassMemberAccessLevel.Protected,
  38443. ],
  38444. };
  38445. /**
  38446. * Attempts to parse a model class member. Returns the parsed model mapping if possible.
  38447. */
  38448. function tryParseSignalModelMapping(member, reflector, importTracker) {
  38449. if (member.value === null) {
  38450. return null;
  38451. }
  38452. const model = tryParseInitializerApi([MODEL_INITIALIZER_FN], member.value, reflector, importTracker);
  38453. if (model === null) {
  38454. return null;
  38455. }
  38456. validateAccessOfInitializerApiMember(model, member);
  38457. const optionsNode = (model.isRequired ? model.call.arguments[0] : model.call.arguments[1]);
  38458. const options = optionsNode !== undefined ? parseAndValidateInputAndOutputOptions(optionsNode) : null;
  38459. const classPropertyName = member.name;
  38460. const bindingPropertyName = options?.alias ?? classPropertyName;
  38461. return {
  38462. call: model.call,
  38463. input: {
  38464. isSignal: true,
  38465. transform: null,
  38466. classPropertyName,
  38467. bindingPropertyName,
  38468. required: model.isRequired,
  38469. },
  38470. output: {
  38471. isSignal: false,
  38472. classPropertyName,
  38473. bindingPropertyName: bindingPropertyName + 'Change',
  38474. },
  38475. };
  38476. }
  38477. // Outputs are accessed from parents, via the `listener` instruction.
  38478. // Conceptually, the fields need to be publicly readable, but in practice,
  38479. // accessing `protected` or `private` members works at runtime, so we can allow
  38480. // such outputs that may not want to expose the `OutputRef` as part of the
  38481. // component API, programmatically.
  38482. // Note: `private` is omitted intentionally as this would be a conceptual confusion point.
  38483. const allowedAccessLevels = [
  38484. exports.ClassMemberAccessLevel.PublicWritable,
  38485. exports.ClassMemberAccessLevel.PublicReadonly,
  38486. exports.ClassMemberAccessLevel.Protected,
  38487. ];
  38488. /** Possible functions that can declare an output. */
  38489. const OUTPUT_INITIALIZER_FNS = [
  38490. {
  38491. functionName: 'output',
  38492. owningModule: '@angular/core',
  38493. allowedAccessLevels,
  38494. },
  38495. {
  38496. functionName: 'outputFromObservable',
  38497. owningModule: '@angular/core/rxjs-interop',
  38498. allowedAccessLevels,
  38499. },
  38500. ];
  38501. /**
  38502. * Attempts to parse a signal output class member. Returns the parsed
  38503. * input mapping if possible.
  38504. */
  38505. function tryParseInitializerBasedOutput(member, reflector, importTracker) {
  38506. if (member.value === null) {
  38507. return null;
  38508. }
  38509. const output = tryParseInitializerApi(OUTPUT_INITIALIZER_FNS, member.value, reflector, importTracker);
  38510. if (output === null) {
  38511. return null;
  38512. }
  38513. if (output.isRequired) {
  38514. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_NO_REQUIRED_FUNCTION, output.call, `Output does not support ".required()".`);
  38515. }
  38516. validateAccessOfInitializerApiMember(output, member);
  38517. // Options are the first parameter for `output()`, while for
  38518. // the interop `outputFromObservable()` they are the second argument.
  38519. const optionsNode = (output.api.functionName === 'output' ? output.call.arguments[0] : output.call.arguments[1]);
  38520. const options = optionsNode !== undefined ? parseAndValidateInputAndOutputOptions(optionsNode) : null;
  38521. const classPropertyName = member.name;
  38522. return {
  38523. call: output.call,
  38524. metadata: {
  38525. // Outputs are not signal-based.
  38526. isSignal: false,
  38527. classPropertyName,
  38528. bindingPropertyName: options?.alias ?? classPropertyName,
  38529. },
  38530. };
  38531. }
  38532. /** Possible names of query initializer APIs. */
  38533. const queryFunctionNames = [
  38534. 'viewChild',
  38535. 'viewChildren',
  38536. 'contentChild',
  38537. 'contentChildren',
  38538. ];
  38539. /** Possible query initializer API functions. */
  38540. const QUERY_INITIALIZER_FNS = queryFunctionNames.map((fnName) => ({
  38541. functionName: fnName,
  38542. owningModule: '@angular/core',
  38543. // Queries are accessed from within static blocks, via the query definition functions.
  38544. // Conceptually, the fields could access private members— even ES private fields.
  38545. // Support for ES private fields requires special caution and complexity when partial
  38546. // output is linked— hence not supported. TS private members are allowed in static blocks.
  38547. allowedAccessLevels: [
  38548. exports.ClassMemberAccessLevel.PublicWritable,
  38549. exports.ClassMemberAccessLevel.PublicReadonly,
  38550. exports.ClassMemberAccessLevel.Protected,
  38551. exports.ClassMemberAccessLevel.Private,
  38552. ],
  38553. }));
  38554. // The `descendants` option is enabled by default, except for content children.
  38555. const defaultDescendantsValue = (type) => type !== 'contentChildren';
  38556. /**
  38557. * Attempts to detect a possible query definition for the given class member.
  38558. *
  38559. * This function checks for all possible variants of queries and matches the
  38560. * first one. The query is then analyzed and its resolved metadata is returned.
  38561. *
  38562. * @returns Resolved query metadata, or null if no query is declared.
  38563. */
  38564. function tryParseSignalQueryFromInitializer(member, reflector, importTracker) {
  38565. if (member.value === null) {
  38566. return null;
  38567. }
  38568. const query = tryParseInitializerApi(QUERY_INITIALIZER_FNS, member.value, reflector, importTracker);
  38569. if (query === null) {
  38570. return null;
  38571. }
  38572. validateAccessOfInitializerApiMember(query, member);
  38573. const { functionName } = query.api;
  38574. const isSingleQuery = functionName === 'viewChild' || functionName === 'contentChild';
  38575. const predicateNode = query.call.arguments[0];
  38576. if (predicateNode === undefined) {
  38577. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, query.call, 'No locator specified.');
  38578. }
  38579. const optionsNode = query.call.arguments[1];
  38580. if (optionsNode !== undefined && !ts.isObjectLiteralExpression(optionsNode)) {
  38581. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, optionsNode, 'Argument needs to be an object literal.');
  38582. }
  38583. const options = optionsNode && reflectObjectLiteral(optionsNode);
  38584. const read = options?.has('read') ? parseReadOption(options.get('read')) : null;
  38585. const descendants = options?.has('descendants')
  38586. ? parseDescendantsOption(options.get('descendants'))
  38587. : defaultDescendantsValue(functionName);
  38588. return {
  38589. name: functionName,
  38590. call: query.call,
  38591. metadata: {
  38592. isSignal: true,
  38593. propertyName: member.name,
  38594. static: false,
  38595. emitDistinctChangesOnly: true,
  38596. predicate: parseLocator(predicateNode, reflector),
  38597. first: isSingleQuery,
  38598. read,
  38599. descendants,
  38600. },
  38601. };
  38602. }
  38603. /** Parses the locator/predicate of the query. */
  38604. function parseLocator(expression, reflector) {
  38605. // Attempt to unwrap `forwardRef` calls.
  38606. const unwrappedExpression = tryUnwrapForwardRef(expression, reflector);
  38607. if (unwrappedExpression !== null) {
  38608. expression = unwrappedExpression;
  38609. }
  38610. if (ts.isStringLiteralLike(expression)) {
  38611. return [expression.text];
  38612. }
  38613. return createMayBeForwardRefExpression(new WrappedNodeExpr(expression), unwrappedExpression !== null ? 2 /* ForwardRefHandling.Unwrapped */ : 0 /* ForwardRefHandling.None */);
  38614. }
  38615. /**
  38616. * Parses the `read` option of a query.
  38617. *
  38618. * We only support the following patterns for the `read` option:
  38619. * - `read: someImport.BLA`,
  38620. * - `read: BLA`
  38621. *
  38622. * That is because we cannot trivially support complex expressions,
  38623. * especially those referencing `this`. The read provider token will
  38624. * live outside of the class in the static class definition.
  38625. */
  38626. function parseReadOption(value) {
  38627. if (ts.isExpressionWithTypeArguments(value) ||
  38628. ts.isParenthesizedExpression(value) ||
  38629. ts.isAsExpression(value)) {
  38630. return parseReadOption(value.expression);
  38631. }
  38632. if ((ts.isPropertyAccessExpression(value) && ts.isIdentifier(value.expression)) ||
  38633. ts.isIdentifier(value)) {
  38634. return new WrappedNodeExpr(value);
  38635. }
  38636. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_NOT_LITERAL, value, `Query "read" option expected a literal class reference.`);
  38637. }
  38638. /** Parses the `descendants` option of a query. */
  38639. function parseDescendantsOption(value) {
  38640. if (value.kind === ts.SyntaxKind.TrueKeyword) {
  38641. return true;
  38642. }
  38643. else if (value.kind === ts.SyntaxKind.FalseKeyword) {
  38644. return false;
  38645. }
  38646. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, value, `Expected "descendants" option to be a boolean literal.`);
  38647. }
  38648. const EMPTY_OBJECT = {};
  38649. const queryDecoratorNames = [
  38650. 'ViewChild',
  38651. 'ViewChildren',
  38652. 'ContentChild',
  38653. 'ContentChildren',
  38654. ];
  38655. const QUERY_TYPES = new Set(queryDecoratorNames);
  38656. /**
  38657. * Helper function to extract metadata from a `Directive` or `Component`. `Directive`s without a
  38658. * selector are allowed to be used for abstract base classes. These abstract directives should not
  38659. * appear in the declarations of an `NgModule` and additional verification is done when processing
  38660. * the module.
  38661. */
  38662. function extractDirectiveMetadata(clazz, decorator, reflector, importTracker, evaluator, refEmitter, referencesRegistry, isCore, annotateForClosureCompiler, compilationMode, defaultSelector, strictStandalone, implicitStandaloneValue) {
  38663. let directive;
  38664. if (decorator.args === null || decorator.args.length === 0) {
  38665. directive = new Map();
  38666. }
  38667. else if (decorator.args.length !== 1) {
  38668. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, decorator.node, `Incorrect number of arguments to @${decorator.name} decorator`);
  38669. }
  38670. else {
  38671. const meta = unwrapExpression(decorator.args[0]);
  38672. if (!ts.isObjectLiteralExpression(meta)) {
  38673. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARG_NOT_LITERAL, meta, `@${decorator.name} argument must be an object literal`);
  38674. }
  38675. directive = reflectObjectLiteral(meta);
  38676. }
  38677. if (directive.has('jit')) {
  38678. // The only allowed value is true, so there's no need to expand further.
  38679. return { jitForced: true };
  38680. }
  38681. const members = reflector.getMembersOfClass(clazz);
  38682. // Precompute a list of ts.ClassElements that have decorators. This includes things like @Input,
  38683. // @Output, @HostBinding, etc.
  38684. const decoratedElements = members.filter((member) => !member.isStatic && member.decorators !== null);
  38685. const coreModule = isCore ? undefined : '@angular/core';
  38686. // Construct the map of inputs both from the @Directive/@Component
  38687. // decorator, and the decorated fields.
  38688. const inputsFromMeta = parseInputsArray(clazz, directive, evaluator, reflector, refEmitter, compilationMode);
  38689. const inputsFromFields = parseInputFields(clazz, members, evaluator, reflector, importTracker, refEmitter, isCore, compilationMode, inputsFromMeta, decorator);
  38690. const inputs = ClassPropertyMapping.fromMappedObject({ ...inputsFromMeta, ...inputsFromFields });
  38691. // And outputs.
  38692. const outputsFromMeta = parseOutputsArray(directive, evaluator);
  38693. const outputsFromFields = parseOutputFields(clazz, decorator, members, isCore, reflector, importTracker, evaluator, outputsFromMeta);
  38694. const outputs = ClassPropertyMapping.fromMappedObject({ ...outputsFromMeta, ...outputsFromFields });
  38695. // Parse queries of fields.
  38696. const { viewQueries, contentQueries } = parseQueriesOfClassFields(members, reflector, importTracker, evaluator, isCore);
  38697. if (directive.has('queries')) {
  38698. const signalQueryFields = new Set([...viewQueries, ...contentQueries].filter((q) => q.isSignal).map((q) => q.propertyName));
  38699. const queriesFromDecorator = extractQueriesFromDecorator(directive.get('queries'), reflector, evaluator, isCore);
  38700. // Checks if the query is already declared/reserved via class members declaration.
  38701. // If so, we throw a fatal diagnostic error to prevent this unintentional pattern.
  38702. const checkAndUnwrapQuery = (q) => {
  38703. if (signalQueryFields.has(q.metadata.propertyName)) {
  38704. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_DECORATOR_METADATA_COLLISION, q.expr, `Query is declared multiple times. "@${decorator.name}" declares a query for the same property.`);
  38705. }
  38706. return q.metadata;
  38707. };
  38708. contentQueries.push(...queriesFromDecorator.content.map((q) => checkAndUnwrapQuery(q)));
  38709. viewQueries.push(...queriesFromDecorator.view.map((q) => checkAndUnwrapQuery(q)));
  38710. }
  38711. // Parse the selector.
  38712. let selector = defaultSelector;
  38713. if (directive.has('selector')) {
  38714. const expr = directive.get('selector');
  38715. const resolved = evaluator.evaluate(expr);
  38716. assertLocalCompilationUnresolvedConst(compilationMode, resolved, null, 'Unresolved identifier found for @Component.selector field! Did you ' +
  38717. 'import this identifier from a file outside of the compilation unit? ' +
  38718. 'This is not allowed when Angular compiler runs in local mode. Possible ' +
  38719. 'solutions: 1) Move the declarations into a file within the compilation ' +
  38720. 'unit, 2) Inline the selector');
  38721. if (typeof resolved !== 'string') {
  38722. throw createValueHasWrongTypeError(expr, resolved, `selector must be a string`);
  38723. }
  38724. // use default selector in case selector is an empty string
  38725. selector = resolved === '' ? defaultSelector : resolved;
  38726. if (!selector) {
  38727. throw new FatalDiagnosticError(exports.ErrorCode.DIRECTIVE_MISSING_SELECTOR, expr, `Directive ${clazz.name.text} has no selector, please add it!`);
  38728. }
  38729. }
  38730. const host = extractHostBindings(decoratedElements, evaluator, coreModule, compilationMode, directive);
  38731. const providers = directive.has('providers')
  38732. ? new WrappedNodeExpr(annotateForClosureCompiler
  38733. ? wrapFunctionExpressionsInParens(directive.get('providers'))
  38734. : directive.get('providers'))
  38735. : null;
  38736. // Determine if `ngOnChanges` is a lifecycle hook defined on the component.
  38737. const usesOnChanges = members.some((member) => !member.isStatic && member.kind === exports.ClassMemberKind.Method && member.name === 'ngOnChanges');
  38738. // Parse exportAs.
  38739. let exportAs = null;
  38740. if (directive.has('exportAs')) {
  38741. const expr = directive.get('exportAs');
  38742. const resolved = evaluator.evaluate(expr);
  38743. assertLocalCompilationUnresolvedConst(compilationMode, resolved, null, 'Unresolved identifier found for exportAs field! Did you import this ' +
  38744. 'identifier from a file outside of the compilation unit? This is not ' +
  38745. 'allowed when Angular compiler runs in local mode. Possible solutions: ' +
  38746. '1) Move the declarations into a file within the compilation unit, ' +
  38747. '2) Inline the selector');
  38748. if (typeof resolved !== 'string') {
  38749. throw createValueHasWrongTypeError(expr, resolved, `exportAs must be a string`);
  38750. }
  38751. exportAs = resolved.split(',').map((part) => part.trim());
  38752. }
  38753. const rawCtorDeps = getConstructorDependencies(clazz, reflector, isCore);
  38754. // Non-abstract directives (those with a selector) require valid constructor dependencies, whereas
  38755. // abstract directives are allowed to have invalid dependencies, given that a subclass may call
  38756. // the constructor explicitly.
  38757. const ctorDeps = selector !== null
  38758. ? validateConstructorDependencies(clazz, rawCtorDeps)
  38759. : unwrapConstructorDependencies(rawCtorDeps);
  38760. // Structural directives must have a `TemplateRef` dependency.
  38761. const isStructural = ctorDeps !== null &&
  38762. ctorDeps !== 'invalid' &&
  38763. ctorDeps.some((dep) => dep.token instanceof ExternalExpr &&
  38764. dep.token.value.moduleName === '@angular/core' &&
  38765. dep.token.value.name === 'TemplateRef');
  38766. let isStandalone = implicitStandaloneValue;
  38767. if (directive.has('standalone')) {
  38768. const expr = directive.get('standalone');
  38769. const resolved = evaluator.evaluate(expr);
  38770. if (typeof resolved !== 'boolean') {
  38771. throw createValueHasWrongTypeError(expr, resolved, `standalone flag must be a boolean`);
  38772. }
  38773. isStandalone = resolved;
  38774. if (!isStandalone && strictStandalone) {
  38775. throw new FatalDiagnosticError(exports.ErrorCode.NON_STANDALONE_NOT_ALLOWED, expr, `Only standalone components/directives are allowed when 'strictStandalone' is enabled.`);
  38776. }
  38777. }
  38778. let isSignal = false;
  38779. if (directive.has('signals')) {
  38780. const expr = directive.get('signals');
  38781. const resolved = evaluator.evaluate(expr);
  38782. if (typeof resolved !== 'boolean') {
  38783. throw createValueHasWrongTypeError(expr, resolved, `signals flag must be a boolean`);
  38784. }
  38785. isSignal = resolved;
  38786. }
  38787. // Detect if the component inherits from another class
  38788. const usesInheritance = reflector.hasBaseClass(clazz);
  38789. const sourceFile = clazz.getSourceFile();
  38790. const type = wrapTypeReference(reflector, clazz);
  38791. const rawHostDirectives = directive.get('hostDirectives') || null;
  38792. const hostDirectives = rawHostDirectives === null
  38793. ? null
  38794. : extractHostDirectives(rawHostDirectives, evaluator, compilationMode, createForwardRefResolver(isCore));
  38795. if (compilationMode !== exports.CompilationMode.LOCAL && hostDirectives !== null) {
  38796. // In global compilation mode where we do type checking, the template type-checker will need to
  38797. // import host directive types, so add them as referenced by `clazz`. This will ensure that
  38798. // libraries are required to export host directives which are visible from publicly exported
  38799. // components.
  38800. referencesRegistry.add(clazz, ...hostDirectives.map((hostDir) => {
  38801. if (!isHostDirectiveMetaForGlobalMode(hostDir)) {
  38802. throw new Error('Impossible state');
  38803. }
  38804. return hostDir.directive;
  38805. }));
  38806. }
  38807. const metadata = {
  38808. name: clazz.name.text,
  38809. deps: ctorDeps,
  38810. host: {
  38811. ...host,
  38812. },
  38813. lifecycle: {
  38814. usesOnChanges,
  38815. },
  38816. inputs: inputs.toJointMappedObject(toR3InputMetadata),
  38817. outputs: outputs.toDirectMappedObject(),
  38818. queries: contentQueries,
  38819. viewQueries,
  38820. selector,
  38821. fullInheritance: false,
  38822. type,
  38823. typeArgumentCount: reflector.getGenericArityOfClass(clazz) || 0,
  38824. typeSourceSpan: createSourceSpan(clazz.name),
  38825. usesInheritance,
  38826. exportAs,
  38827. providers,
  38828. isStandalone,
  38829. isSignal,
  38830. hostDirectives: hostDirectives?.map((hostDir) => toHostDirectiveMetadata(hostDir, sourceFile, refEmitter)) ||
  38831. null,
  38832. };
  38833. return {
  38834. jitForced: false,
  38835. decorator: directive,
  38836. metadata,
  38837. inputs,
  38838. outputs,
  38839. isStructural,
  38840. hostDirectives,
  38841. rawHostDirectives,
  38842. // Track inputs from class metadata. This is useful for migration efforts.
  38843. inputFieldNamesFromMetadataArray: new Set(Object.values(inputsFromMeta).map((i) => i.classPropertyName)),
  38844. };
  38845. }
  38846. function extractDecoratorQueryMetadata(exprNode, name, args, propertyName, reflector, evaluator) {
  38847. if (args.length === 0) {
  38848. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, exprNode, `@${name} must have arguments`);
  38849. }
  38850. const first = name === 'ViewChild' || name === 'ContentChild';
  38851. const forwardReferenceTarget = tryUnwrapForwardRef(args[0], reflector);
  38852. const node = forwardReferenceTarget ?? args[0];
  38853. const arg = evaluator.evaluate(node);
  38854. /** Whether or not this query should collect only static results (see view/api.ts) */
  38855. let isStatic = false;
  38856. // Extract the predicate
  38857. let predicate = null;
  38858. if (arg instanceof Reference || arg instanceof DynamicValue) {
  38859. // References and predicates that could not be evaluated statically are emitted as is.
  38860. predicate = createMayBeForwardRefExpression(new WrappedNodeExpr(node), forwardReferenceTarget !== null ? 2 /* ForwardRefHandling.Unwrapped */ : 0 /* ForwardRefHandling.None */);
  38861. }
  38862. else if (typeof arg === 'string') {
  38863. predicate = [arg];
  38864. }
  38865. else if (isStringArrayOrDie(arg, `@${name} predicate`, node)) {
  38866. predicate = arg;
  38867. }
  38868. else {
  38869. throw createValueHasWrongTypeError(node, arg, `@${name} predicate cannot be interpreted`);
  38870. }
  38871. // Extract the read and descendants options.
  38872. let read = null;
  38873. // The default value for descendants is true for every decorator except @ContentChildren.
  38874. let descendants = name !== 'ContentChildren';
  38875. let emitDistinctChangesOnly = emitDistinctChangesOnlyDefaultValue;
  38876. if (args.length === 2) {
  38877. const optionsExpr = unwrapExpression(args[1]);
  38878. if (!ts.isObjectLiteralExpression(optionsExpr)) {
  38879. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARG_NOT_LITERAL, optionsExpr, `@${name} options must be an object literal`);
  38880. }
  38881. const options = reflectObjectLiteral(optionsExpr);
  38882. if (options.has('read')) {
  38883. read = new WrappedNodeExpr(options.get('read'));
  38884. }
  38885. if (options.has('descendants')) {
  38886. const descendantsExpr = options.get('descendants');
  38887. const descendantsValue = evaluator.evaluate(descendantsExpr);
  38888. if (typeof descendantsValue !== 'boolean') {
  38889. throw createValueHasWrongTypeError(descendantsExpr, descendantsValue, `@${name} options.descendants must be a boolean`);
  38890. }
  38891. descendants = descendantsValue;
  38892. }
  38893. if (options.has('emitDistinctChangesOnly')) {
  38894. const emitDistinctChangesOnlyExpr = options.get('emitDistinctChangesOnly');
  38895. const emitDistinctChangesOnlyValue = evaluator.evaluate(emitDistinctChangesOnlyExpr);
  38896. if (typeof emitDistinctChangesOnlyValue !== 'boolean') {
  38897. throw createValueHasWrongTypeError(emitDistinctChangesOnlyExpr, emitDistinctChangesOnlyValue, `@${name} options.emitDistinctChangesOnly must be a boolean`);
  38898. }
  38899. emitDistinctChangesOnly = emitDistinctChangesOnlyValue;
  38900. }
  38901. if (options.has('static')) {
  38902. const staticValue = evaluator.evaluate(options.get('static'));
  38903. if (typeof staticValue !== 'boolean') {
  38904. throw createValueHasWrongTypeError(node, staticValue, `@${name} options.static must be a boolean`);
  38905. }
  38906. isStatic = staticValue;
  38907. }
  38908. }
  38909. else if (args.length > 2) {
  38910. // Too many arguments.
  38911. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, node, `@${name} has too many arguments`);
  38912. }
  38913. return {
  38914. isSignal: false,
  38915. propertyName,
  38916. predicate,
  38917. first,
  38918. descendants,
  38919. read,
  38920. static: isStatic,
  38921. emitDistinctChangesOnly,
  38922. };
  38923. }
  38924. function extractHostBindings(members, evaluator, coreModule, compilationMode, metadata) {
  38925. let bindings;
  38926. if (metadata && metadata.has('host')) {
  38927. bindings = evaluateHostExpressionBindings(metadata.get('host'), evaluator);
  38928. }
  38929. else {
  38930. bindings = parseHostBindings({});
  38931. }
  38932. filterToMembersWithDecorator(members, 'HostBinding', coreModule).forEach(({ member, decorators }) => {
  38933. decorators.forEach((decorator) => {
  38934. let hostPropertyName = member.name;
  38935. if (decorator.args !== null && decorator.args.length > 0) {
  38936. if (decorator.args.length !== 1) {
  38937. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, decorator.node, `@HostBinding can have at most one argument, got ${decorator.args.length} argument(s)`);
  38938. }
  38939. const resolved = evaluator.evaluate(decorator.args[0]);
  38940. // Specific error for local compilation mode if the argument cannot be resolved
  38941. assertLocalCompilationUnresolvedConst(compilationMode, resolved, null, "Unresolved identifier found for @HostBinding's argument! Did " +
  38942. 'you import this identifier from a file outside of the compilation ' +
  38943. 'unit? This is not allowed when Angular compiler runs in local mode. ' +
  38944. 'Possible solutions: 1) Move the declaration into a file within ' +
  38945. 'the compilation unit, 2) Inline the argument');
  38946. if (typeof resolved !== 'string') {
  38947. throw createValueHasWrongTypeError(decorator.node, resolved, `@HostBinding's argument must be a string`);
  38948. }
  38949. hostPropertyName = resolved;
  38950. }
  38951. // Since this is a decorator, we know that the value is a class member. Always access it
  38952. // through `this` so that further down the line it can't be confused for a literal value
  38953. // (e.g. if there's a property called `true`). There is no size penalty, because all
  38954. // values (except literals) are converted to `ctx.propName` eventually.
  38955. bindings.properties[hostPropertyName] = getSafePropertyAccessString('this', member.name);
  38956. });
  38957. });
  38958. filterToMembersWithDecorator(members, 'HostListener', coreModule).forEach(({ member, decorators }) => {
  38959. decorators.forEach((decorator) => {
  38960. let eventName = member.name;
  38961. let args = [];
  38962. if (decorator.args !== null && decorator.args.length > 0) {
  38963. if (decorator.args.length > 2) {
  38964. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, decorator.args[2], `@HostListener can have at most two arguments`);
  38965. }
  38966. const resolved = evaluator.evaluate(decorator.args[0]);
  38967. // Specific error for local compilation mode if the event name cannot be resolved
  38968. assertLocalCompilationUnresolvedConst(compilationMode, resolved, null, "Unresolved identifier found for @HostListener's event name " +
  38969. 'argument! Did you import this identifier from a file outside of ' +
  38970. 'the compilation unit? This is not allowed when Angular compiler ' +
  38971. 'runs in local mode. Possible solutions: 1) Move the declaration ' +
  38972. 'into a file within the compilation unit, 2) Inline the argument');
  38973. if (typeof resolved !== 'string') {
  38974. throw createValueHasWrongTypeError(decorator.args[0], resolved, `@HostListener's event name argument must be a string`);
  38975. }
  38976. eventName = resolved;
  38977. if (decorator.args.length === 2) {
  38978. const expression = decorator.args[1];
  38979. const resolvedArgs = evaluator.evaluate(decorator.args[1]);
  38980. if (!isStringArrayOrDie(resolvedArgs, '@HostListener.args', expression)) {
  38981. throw createValueHasWrongTypeError(decorator.args[1], resolvedArgs, `@HostListener's second argument must be a string array`);
  38982. }
  38983. args = resolvedArgs;
  38984. }
  38985. }
  38986. bindings.listeners[eventName] = `${member.name}(${args.join(',')})`;
  38987. });
  38988. });
  38989. return bindings;
  38990. }
  38991. function extractQueriesFromDecorator(queryData, reflector, evaluator, isCore) {
  38992. const content = [];
  38993. const view = [];
  38994. if (!ts.isObjectLiteralExpression(queryData)) {
  38995. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, queryData, 'Decorator queries metadata must be an object literal');
  38996. }
  38997. reflectObjectLiteral(queryData).forEach((queryExpr, propertyName) => {
  38998. queryExpr = unwrapExpression(queryExpr);
  38999. if (!ts.isNewExpression(queryExpr)) {
  39000. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, queryData, 'Decorator query metadata must be an instance of a query type');
  39001. }
  39002. const queryType = ts.isPropertyAccessExpression(queryExpr.expression)
  39003. ? queryExpr.expression.name
  39004. : queryExpr.expression;
  39005. if (!ts.isIdentifier(queryType)) {
  39006. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, queryData, 'Decorator query metadata must be an instance of a query type');
  39007. }
  39008. const type = reflector.getImportOfIdentifier(queryType);
  39009. if (type === null ||
  39010. (!isCore && type.from !== '@angular/core') ||
  39011. !QUERY_TYPES.has(type.name)) {
  39012. throw new FatalDiagnosticError(exports.ErrorCode.VALUE_HAS_WRONG_TYPE, queryData, 'Decorator query metadata must be an instance of a query type');
  39013. }
  39014. const query = extractDecoratorQueryMetadata(queryExpr, type.name, queryExpr.arguments || [], propertyName, reflector, evaluator);
  39015. if (type.name.startsWith('Content')) {
  39016. content.push({ expr: queryExpr, metadata: query });
  39017. }
  39018. else {
  39019. view.push({ expr: queryExpr, metadata: query });
  39020. }
  39021. });
  39022. return { content, view };
  39023. }
  39024. function parseDirectiveStyles(directive, evaluator, compilationMode) {
  39025. const expression = directive.get('styles');
  39026. if (!expression) {
  39027. return null;
  39028. }
  39029. const evaluated = evaluator.evaluate(expression);
  39030. const value = typeof evaluated === 'string' ? [evaluated] : evaluated;
  39031. // Check if the identifier used for @Component.styles cannot be resolved in local compilation
  39032. // mode. if the case, an error specific to this situation is generated.
  39033. if (compilationMode === exports.CompilationMode.LOCAL) {
  39034. let unresolvedNode = null;
  39035. if (Array.isArray(value)) {
  39036. const entry = value.find((e) => e instanceof DynamicValue && e.isFromUnknownIdentifier());
  39037. unresolvedNode = entry?.node ?? null;
  39038. }
  39039. else if (value instanceof DynamicValue && value.isFromUnknownIdentifier()) {
  39040. unresolvedNode = value.node;
  39041. }
  39042. if (unresolvedNode !== null) {
  39043. throw new FatalDiagnosticError(exports.ErrorCode.LOCAL_COMPILATION_UNRESOLVED_CONST, unresolvedNode, 'Unresolved identifier found for @Component.styles field! Did you import ' +
  39044. 'this identifier from a file outside of the compilation unit? This is ' +
  39045. 'not allowed when Angular compiler runs in local mode. Possible ' +
  39046. 'solutions: 1) Move the declarations into a file within the compilation ' +
  39047. 'unit, 2) Inline the styles, 3) Move the styles into separate files and ' +
  39048. 'include it using @Component.styleUrls');
  39049. }
  39050. }
  39051. if (!isStringArrayOrDie(value, 'styles', expression)) {
  39052. throw createValueHasWrongTypeError(expression, value, `Failed to resolve @Component.styles to a string or an array of strings`);
  39053. }
  39054. return value;
  39055. }
  39056. function parseFieldStringArrayValue(directive, field, evaluator) {
  39057. if (!directive.has(field)) {
  39058. return null;
  39059. }
  39060. // Resolve the field of interest from the directive metadata to a string[].
  39061. const expression = directive.get(field);
  39062. const value = evaluator.evaluate(expression);
  39063. if (!isStringArrayOrDie(value, field, expression)) {
  39064. throw createValueHasWrongTypeError(expression, value, `Failed to resolve @Directive.${field} to a string array`);
  39065. }
  39066. return value;
  39067. }
  39068. function isStringArrayOrDie(value, name, node) {
  39069. if (!Array.isArray(value)) {
  39070. return false;
  39071. }
  39072. for (let i = 0; i < value.length; i++) {
  39073. if (typeof value[i] !== 'string') {
  39074. throw createValueHasWrongTypeError(node, value[i], `Failed to resolve ${name} at position ${i} to a string`);
  39075. }
  39076. }
  39077. return true;
  39078. }
  39079. function tryGetQueryFromFieldDecorator(member, reflector, evaluator, isCore) {
  39080. const decorators = member.decorators;
  39081. if (decorators === null) {
  39082. return null;
  39083. }
  39084. const queryDecorators = getAngularDecorators(decorators, queryDecoratorNames, isCore);
  39085. if (queryDecorators.length === 0) {
  39086. return null;
  39087. }
  39088. if (queryDecorators.length !== 1) {
  39089. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_COLLISION, member.node ?? queryDecorators[0].node, 'Cannot combine multiple query decorators.');
  39090. }
  39091. const decorator = queryDecorators[0];
  39092. const node = member.node || decorator.node;
  39093. // Throw in case of `@Input() @ContentChild('foo') foo: any`, which is not supported in Ivy
  39094. if (decorators.some((v) => v.name === 'Input')) {
  39095. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_COLLISION, node, 'Cannot combine @Input decorators with query decorators');
  39096. }
  39097. if (!isPropertyTypeMember(member)) {
  39098. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_UNEXPECTED, node, 'Query decorator must go on a property-type member');
  39099. }
  39100. // Either the decorator was aliased, or is referenced directly with
  39101. // the proper query name.
  39102. const name = (decorator.import?.name ?? decorator.name);
  39103. return {
  39104. name,
  39105. decorator,
  39106. metadata: extractDecoratorQueryMetadata(node, name, decorator.args || [], member.name, reflector, evaluator),
  39107. };
  39108. }
  39109. function isPropertyTypeMember(member) {
  39110. return (member.kind === exports.ClassMemberKind.Getter ||
  39111. member.kind === exports.ClassMemberKind.Setter ||
  39112. member.kind === exports.ClassMemberKind.Property);
  39113. }
  39114. function parseMappingStringArray(values) {
  39115. return values.reduce((results, value) => {
  39116. if (typeof value !== 'string') {
  39117. throw new Error('Mapping value must be a string');
  39118. }
  39119. const [bindingPropertyName, fieldName] = parseMappingString(value);
  39120. results[fieldName] = bindingPropertyName;
  39121. return results;
  39122. }, {});
  39123. }
  39124. function parseMappingString(value) {
  39125. // Either the value is 'field' or 'field: property'. In the first case, `property` will
  39126. // be undefined, in which case the field name should also be used as the property name.
  39127. const [fieldName, bindingPropertyName] = value.split(':', 2).map((str) => str.trim());
  39128. return [bindingPropertyName ?? fieldName, fieldName];
  39129. }
  39130. /** Parses the `inputs` array of a directive/component decorator. */
  39131. function parseInputsArray(clazz, decoratorMetadata, evaluator, reflector, refEmitter, compilationMode) {
  39132. const inputsField = decoratorMetadata.get('inputs');
  39133. if (inputsField === undefined) {
  39134. return {};
  39135. }
  39136. const inputs = {};
  39137. const inputsArray = evaluator.evaluate(inputsField);
  39138. if (!Array.isArray(inputsArray)) {
  39139. throw createValueHasWrongTypeError(inputsField, inputsArray, `Failed to resolve @Directive.inputs to an array`);
  39140. }
  39141. for (let i = 0; i < inputsArray.length; i++) {
  39142. const value = inputsArray[i];
  39143. if (typeof value === 'string') {
  39144. // If the value is a string, we treat it as a mapping string.
  39145. const [bindingPropertyName, classPropertyName] = parseMappingString(value);
  39146. inputs[classPropertyName] = {
  39147. bindingPropertyName,
  39148. classPropertyName,
  39149. required: false,
  39150. transform: null,
  39151. // Note: Signal inputs are not allowed with the array form.
  39152. isSignal: false,
  39153. };
  39154. }
  39155. else if (value instanceof Map) {
  39156. // If it's a map, we treat it as a config object.
  39157. const name = value.get('name');
  39158. const alias = value.get('alias');
  39159. const required = value.get('required');
  39160. let transform = null;
  39161. if (typeof name !== 'string') {
  39162. throw createValueHasWrongTypeError(inputsField, name, `Value at position ${i} of @Directive.inputs array must have a "name" property`);
  39163. }
  39164. if (value.has('transform')) {
  39165. const transformValue = value.get('transform');
  39166. if (!(transformValue instanceof DynamicValue) && !(transformValue instanceof Reference)) {
  39167. throw createValueHasWrongTypeError(inputsField, transformValue, `Transform of value at position ${i} of @Directive.inputs array must be a function`);
  39168. }
  39169. transform = parseDecoratorInputTransformFunction(clazz, name, transformValue, reflector, refEmitter, compilationMode);
  39170. }
  39171. inputs[name] = {
  39172. classPropertyName: name,
  39173. bindingPropertyName: typeof alias === 'string' ? alias : name,
  39174. required: required === true,
  39175. // Note: Signal inputs are not allowed with the array form.
  39176. isSignal: false,
  39177. transform,
  39178. };
  39179. }
  39180. else {
  39181. throw createValueHasWrongTypeError(inputsField, value, `@Directive.inputs array can only contain strings or object literals`);
  39182. }
  39183. }
  39184. return inputs;
  39185. }
  39186. /** Attempts to find a given Angular decorator on the class member. */
  39187. function tryGetDecoratorOnMember(member, decoratorName, isCore) {
  39188. if (member.decorators === null) {
  39189. return null;
  39190. }
  39191. for (const decorator of member.decorators) {
  39192. if (isAngularDecorator(decorator, decoratorName, isCore)) {
  39193. return decorator;
  39194. }
  39195. }
  39196. return null;
  39197. }
  39198. function tryParseInputFieldMapping(clazz, member, evaluator, reflector, importTracker, isCore, refEmitter, compilationMode) {
  39199. const classPropertyName = member.name;
  39200. const decorator = tryGetDecoratorOnMember(member, 'Input', isCore);
  39201. const signalInputMapping = tryParseSignalInputMapping(member, reflector, importTracker);
  39202. const modelInputMapping = tryParseSignalModelMapping(member, reflector, importTracker);
  39203. if (decorator !== null && signalInputMapping !== null) {
  39204. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_WITH_DISALLOWED_DECORATOR, decorator.node, `Using @Input with a signal input is not allowed.`);
  39205. }
  39206. if (decorator !== null && modelInputMapping !== null) {
  39207. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_WITH_DISALLOWED_DECORATOR, decorator.node, `Using @Input with a model input is not allowed.`);
  39208. }
  39209. // Check `@Input` case.
  39210. if (decorator !== null) {
  39211. if (decorator.args !== null && decorator.args.length > 1) {
  39212. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, decorator.node, `@${decorator.name} can have at most one argument, got ${decorator.args.length} argument(s)`);
  39213. }
  39214. const optionsNode = decorator.args !== null && decorator.args.length === 1 ? decorator.args[0] : undefined;
  39215. const options = optionsNode !== undefined ? evaluator.evaluate(optionsNode) : null;
  39216. const required = options instanceof Map ? options.get('required') === true : false;
  39217. // To preserve old behavior: Even though TypeScript types ensure proper options are
  39218. // passed, we sanity check for unsupported values here again.
  39219. if (options !== null && typeof options !== 'string' && !(options instanceof Map)) {
  39220. throw createValueHasWrongTypeError(decorator.node, options, `@${decorator.name} decorator argument must resolve to a string or an object literal`);
  39221. }
  39222. let alias = null;
  39223. if (typeof options === 'string') {
  39224. alias = options;
  39225. }
  39226. else if (options instanceof Map && typeof options.get('alias') === 'string') {
  39227. alias = options.get('alias');
  39228. }
  39229. const publicInputName = alias ?? classPropertyName;
  39230. let transform = null;
  39231. if (options instanceof Map && options.has('transform')) {
  39232. const transformValue = options.get('transform');
  39233. if (!(transformValue instanceof DynamicValue) && !(transformValue instanceof Reference)) {
  39234. throw createValueHasWrongTypeError(optionsNode, transformValue, `Input transform must be a function`);
  39235. }
  39236. transform = parseDecoratorInputTransformFunction(clazz, classPropertyName, transformValue, reflector, refEmitter, compilationMode);
  39237. }
  39238. return {
  39239. isSignal: false,
  39240. classPropertyName,
  39241. bindingPropertyName: publicInputName,
  39242. transform,
  39243. required,
  39244. };
  39245. }
  39246. // Look for signal inputs. e.g. `memberName = input()`
  39247. if (signalInputMapping !== null) {
  39248. return signalInputMapping;
  39249. }
  39250. if (modelInputMapping !== null) {
  39251. return modelInputMapping.input;
  39252. }
  39253. return null;
  39254. }
  39255. /** Parses the class members that declare inputs (via decorator or initializer). */
  39256. function parseInputFields(clazz, members, evaluator, reflector, importTracker, refEmitter, isCore, compilationMode, inputsFromClassDecorator, classDecorator) {
  39257. const inputs = {};
  39258. for (const member of members) {
  39259. const classPropertyName = member.name;
  39260. const inputMapping = tryParseInputFieldMapping(clazz, member, evaluator, reflector, importTracker, isCore, refEmitter, compilationMode);
  39261. if (inputMapping === null) {
  39262. continue;
  39263. }
  39264. if (member.isStatic) {
  39265. throw new FatalDiagnosticError(exports.ErrorCode.INCORRECTLY_DECLARED_ON_STATIC_MEMBER, member.node ?? clazz, `Input "${member.name}" is incorrectly declared as static member of "${clazz.name.text}".`);
  39266. }
  39267. // Validate that signal inputs are not accidentally declared in the `inputs` metadata.
  39268. if (inputMapping.isSignal && inputsFromClassDecorator.hasOwnProperty(classPropertyName)) {
  39269. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_DECORATOR_METADATA_COLLISION, member.node ?? clazz, `Input "${member.name}" is also declared as non-signal in @${classDecorator.name}.`);
  39270. }
  39271. inputs[classPropertyName] = inputMapping;
  39272. }
  39273. return inputs;
  39274. }
  39275. /**
  39276. * Parses the `transform` function and its type for a decorator `@Input`.
  39277. *
  39278. * This logic verifies feasibility of extracting the transform write type
  39279. * into a different place, so that the input write type can be captured at
  39280. * a later point in a static acceptance member.
  39281. *
  39282. * Note: This is not needed for signal inputs where the transform type is
  39283. * automatically captured in the type of the `InputSignal`.
  39284. *
  39285. */
  39286. function parseDecoratorInputTransformFunction(clazz, classPropertyName, value, reflector, refEmitter, compilationMode) {
  39287. // In local compilation mode we can skip type checking the function args. This is because usually
  39288. // the type check is done in a separate build which runs in full compilation mode. So here we skip
  39289. // all the diagnostics.
  39290. if (compilationMode === exports.CompilationMode.LOCAL) {
  39291. const node = value instanceof Reference ? value.getIdentityIn(clazz.getSourceFile()) : value.node;
  39292. // This should never be null since we know the reference originates
  39293. // from the same file, but we null check it just in case.
  39294. if (node === null) {
  39295. throw createValueHasWrongTypeError(value.node, value, 'Input transform function could not be referenced');
  39296. }
  39297. return {
  39298. node,
  39299. type: new Reference(ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)),
  39300. };
  39301. }
  39302. const definition = reflector.getDefinitionOfFunction(value.node);
  39303. if (definition === null) {
  39304. throw createValueHasWrongTypeError(value.node, value, 'Input transform must be a function');
  39305. }
  39306. if (definition.typeParameters !== null && definition.typeParameters.length > 0) {
  39307. throw createValueHasWrongTypeError(value.node, value, 'Input transform function cannot be generic');
  39308. }
  39309. if (definition.signatureCount > 1) {
  39310. throw createValueHasWrongTypeError(value.node, value, 'Input transform function cannot have multiple signatures');
  39311. }
  39312. const members = reflector.getMembersOfClass(clazz);
  39313. for (const member of members) {
  39314. const conflictingName = `ngAcceptInputType_${classPropertyName}`;
  39315. if (member.name === conflictingName && member.isStatic) {
  39316. throw new FatalDiagnosticError(exports.ErrorCode.CONFLICTING_INPUT_TRANSFORM, value.node, `Class cannot have both a transform function on Input ${classPropertyName} and a static member called ${conflictingName}`);
  39317. }
  39318. }
  39319. const node = value instanceof Reference ? value.getIdentityIn(clazz.getSourceFile()) : value.node;
  39320. // This should never be null since we know the reference originates
  39321. // from the same file, but we null check it just in case.
  39322. if (node === null) {
  39323. throw createValueHasWrongTypeError(value.node, value, 'Input transform function could not be referenced');
  39324. }
  39325. // Skip over `this` parameters since they're typing the context, not the actual parameter.
  39326. // `this` parameters are guaranteed to be first if they exist, and the only to distinguish them
  39327. // is using the name, TS doesn't have a special AST for them.
  39328. const firstParam = definition.parameters[0]?.name === 'this' ? definition.parameters[1] : definition.parameters[0];
  39329. // Treat functions with no arguments as `unknown` since returning
  39330. // the same value from the transform function is valid.
  39331. if (!firstParam) {
  39332. return {
  39333. node,
  39334. type: new Reference(ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword)),
  39335. };
  39336. }
  39337. // This should be caught by `noImplicitAny` already, but null check it just in case.
  39338. if (!firstParam.type) {
  39339. throw createValueHasWrongTypeError(value.node, value, 'Input transform function first parameter must have a type');
  39340. }
  39341. if (firstParam.node.dotDotDotToken) {
  39342. throw createValueHasWrongTypeError(value.node, value, 'Input transform function first parameter cannot be a spread parameter');
  39343. }
  39344. assertEmittableInputType(firstParam.type, clazz.getSourceFile(), reflector, refEmitter);
  39345. const viaModule = value instanceof Reference ? value.bestGuessOwningModule : null;
  39346. return { node, type: new Reference(firstParam.type, viaModule) };
  39347. }
  39348. /**
  39349. * Verifies that a type and all types contained within
  39350. * it can be referenced in a specific context file.
  39351. */
  39352. function assertEmittableInputType(type, contextFile, reflector, refEmitter) {
  39353. (function walk(node) {
  39354. if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName)) {
  39355. const declaration = reflector.getDeclarationOfIdentifier(node.typeName);
  39356. if (declaration !== null) {
  39357. // If the type is declared in a different file, we have to check that it can be imported
  39358. // into the context file. If they're in the same file, we need to verify that they're
  39359. // exported, otherwise TS won't emit it to the .d.ts.
  39360. if (declaration.node.getSourceFile() !== contextFile) {
  39361. const emittedType = refEmitter.emit(new Reference(declaration.node, declaration.viaModule === AmbientImport ? AmbientImport : null), contextFile, exports.ImportFlags.NoAliasing |
  39362. exports.ImportFlags.AllowTypeImports |
  39363. exports.ImportFlags.AllowRelativeDtsImports |
  39364. exports.ImportFlags.AllowAmbientReferences);
  39365. assertSuccessfulReferenceEmit(emittedType, node, 'type');
  39366. }
  39367. else if (!reflector.isStaticallyExported(declaration.node)) {
  39368. throw new FatalDiagnosticError(exports.ErrorCode.SYMBOL_NOT_EXPORTED, type, `Symbol must be exported in order to be used as the type of an Input transform function`, [makeRelatedInformation(declaration.node, `The symbol is declared here.`)]);
  39369. }
  39370. }
  39371. }
  39372. node.forEachChild(walk);
  39373. })(type);
  39374. }
  39375. /**
  39376. * Iterates through all specified class members and attempts to detect
  39377. * view and content queries defined.
  39378. *
  39379. * Queries may be either defined via decorators, or through class member
  39380. * initializers for signal-based queries.
  39381. */
  39382. function parseQueriesOfClassFields(members, reflector, importTracker, evaluator, isCore) {
  39383. const viewQueries = [];
  39384. const contentQueries = [];
  39385. // For backwards compatibility, decorator-based queries are grouped and
  39386. // ordered in a specific way. The order needs to match with what we had in:
  39387. // https://github.com/angular/angular/blob/8737544d6963bf664f752de273e919575cca08ac/packages/compiler-cli/src/ngtsc/annotations/directive/src/shared.ts#L94-L111.
  39388. const decoratorViewChild = [];
  39389. const decoratorViewChildren = [];
  39390. const decoratorContentChild = [];
  39391. const decoratorContentChildren = [];
  39392. for (const member of members) {
  39393. const decoratorQuery = tryGetQueryFromFieldDecorator(member, reflector, evaluator, isCore);
  39394. const signalQuery = tryParseSignalQueryFromInitializer(member, reflector, importTracker);
  39395. if (decoratorQuery !== null && signalQuery !== null) {
  39396. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_WITH_DISALLOWED_DECORATOR, decoratorQuery.decorator.node, `Using @${decoratorQuery.name} with a signal-based query is not allowed.`);
  39397. }
  39398. const queryNode = decoratorQuery?.decorator.node ?? signalQuery?.call;
  39399. if (queryNode !== undefined && member.isStatic) {
  39400. throw new FatalDiagnosticError(exports.ErrorCode.INCORRECTLY_DECLARED_ON_STATIC_MEMBER, queryNode, `Query is incorrectly declared on a static class member.`);
  39401. }
  39402. if (decoratorQuery !== null) {
  39403. switch (decoratorQuery.name) {
  39404. case 'ViewChild':
  39405. decoratorViewChild.push(decoratorQuery.metadata);
  39406. break;
  39407. case 'ViewChildren':
  39408. decoratorViewChildren.push(decoratorQuery.metadata);
  39409. break;
  39410. case 'ContentChild':
  39411. decoratorContentChild.push(decoratorQuery.metadata);
  39412. break;
  39413. case 'ContentChildren':
  39414. decoratorContentChildren.push(decoratorQuery.metadata);
  39415. break;
  39416. }
  39417. }
  39418. else if (signalQuery !== null) {
  39419. switch (signalQuery.name) {
  39420. case 'viewChild':
  39421. case 'viewChildren':
  39422. viewQueries.push(signalQuery.metadata);
  39423. break;
  39424. case 'contentChild':
  39425. case 'contentChildren':
  39426. contentQueries.push(signalQuery.metadata);
  39427. break;
  39428. }
  39429. }
  39430. }
  39431. return {
  39432. viewQueries: [...viewQueries, ...decoratorViewChild, ...decoratorViewChildren],
  39433. contentQueries: [...contentQueries, ...decoratorContentChild, ...decoratorContentChildren],
  39434. };
  39435. }
  39436. /** Parses the `outputs` array of a directive/component. */
  39437. function parseOutputsArray(directive, evaluator) {
  39438. const metaValues = parseFieldStringArrayValue(directive, 'outputs', evaluator);
  39439. return metaValues ? parseMappingStringArray(metaValues) : EMPTY_OBJECT;
  39440. }
  39441. /** Parses the class members that are outputs. */
  39442. function parseOutputFields(clazz, classDecorator, members, isCore, reflector, importTracker, evaluator, outputsFromMeta) {
  39443. const outputs = {};
  39444. for (const member of members) {
  39445. const decoratorOutput = tryParseDecoratorOutput(member, evaluator, isCore);
  39446. const initializerOutput = tryParseInitializerBasedOutput(member, reflector, importTracker);
  39447. const modelMapping = tryParseSignalModelMapping(member, reflector, importTracker);
  39448. if (decoratorOutput !== null && initializerOutput !== null) {
  39449. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_WITH_DISALLOWED_DECORATOR, decoratorOutput.decorator.node, `Using "@Output" with "output()" is not allowed.`);
  39450. }
  39451. if (decoratorOutput !== null && modelMapping !== null) {
  39452. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_WITH_DISALLOWED_DECORATOR, decoratorOutput.decorator.node, `Using @Output with a model input is not allowed.`);
  39453. }
  39454. const queryNode = decoratorOutput?.decorator.node ?? initializerOutput?.call ?? modelMapping?.call;
  39455. if (queryNode !== undefined && member.isStatic) {
  39456. throw new FatalDiagnosticError(exports.ErrorCode.INCORRECTLY_DECLARED_ON_STATIC_MEMBER, queryNode, `Output is incorrectly declared on a static class member.`);
  39457. }
  39458. let bindingPropertyName;
  39459. if (decoratorOutput !== null) {
  39460. bindingPropertyName = decoratorOutput.metadata.bindingPropertyName;
  39461. }
  39462. else if (initializerOutput !== null) {
  39463. bindingPropertyName = initializerOutput.metadata.bindingPropertyName;
  39464. }
  39465. else if (modelMapping !== null) {
  39466. bindingPropertyName = modelMapping.output.bindingPropertyName;
  39467. }
  39468. else {
  39469. continue;
  39470. }
  39471. // Validate that initializer-based outputs are not accidentally declared
  39472. // in the `outputs` class metadata.
  39473. if ((initializerOutput !== null || modelMapping !== null) &&
  39474. outputsFromMeta.hasOwnProperty(member.name)) {
  39475. throw new FatalDiagnosticError(exports.ErrorCode.INITIALIZER_API_DECORATOR_METADATA_COLLISION, member.node ?? clazz, `Output "${member.name}" is unexpectedly declared in @${classDecorator.name} as well.`);
  39476. }
  39477. outputs[member.name] = bindingPropertyName;
  39478. }
  39479. return outputs;
  39480. }
  39481. /** Attempts to parse a decorator-based @Output. */
  39482. function tryParseDecoratorOutput(member, evaluator, isCore) {
  39483. const decorator = tryGetDecoratorOnMember(member, 'Output', isCore);
  39484. if (decorator === null) {
  39485. return null;
  39486. }
  39487. if (decorator.args !== null && decorator.args.length > 1) {
  39488. throw new FatalDiagnosticError(exports.ErrorCode.DECORATOR_ARITY_WRONG, decorator.node, `@Output can have at most one argument, got ${decorator.args.length} argument(s)`);
  39489. }
  39490. const classPropertyName = member.name;
  39491. let alias = null;
  39492. if (decorator.args?.length === 1) {
  39493. const resolvedAlias = evaluator.evaluate(decorator.args[0]);
  39494. if (typeof resolvedAlias !== 'string') {
  39495. throw createValueHasWrongTypeError(decorator.node, resolvedAlias, `@Output decorator argument must resolve to a string`);
  39496. }
  39497. alias = resolvedAlias;
  39498. }
  39499. return {
  39500. decorator,
  39501. metadata: {
  39502. isSignal: false,
  39503. classPropertyName,
  39504. bindingPropertyName: alias ?? classPropertyName,
  39505. },
  39506. };
  39507. }
  39508. function evaluateHostExpressionBindings(hostExpr, evaluator) {
  39509. const hostMetaMap = evaluator.evaluate(hostExpr);
  39510. if (!(hostMetaMap instanceof Map)) {
  39511. throw createValueHasWrongTypeError(hostExpr, hostMetaMap, `Decorator host metadata must be an object`);
  39512. }
  39513. const hostMetadata = {};
  39514. hostMetaMap.forEach((value, key) => {
  39515. // Resolve Enum references to their declared value.
  39516. if (value instanceof EnumValue) {
  39517. value = value.resolved;
  39518. }
  39519. if (typeof key !== 'string') {
  39520. throw createValueHasWrongTypeError(hostExpr, key, `Decorator host metadata must be a string -> string object, but found unparseable key`);
  39521. }
  39522. if (typeof value == 'string') {
  39523. hostMetadata[key] = value;
  39524. }
  39525. else if (value instanceof DynamicValue) {
  39526. hostMetadata[key] = new WrappedNodeExpr(value.node);
  39527. }
  39528. else {
  39529. throw createValueHasWrongTypeError(hostExpr, value, `Decorator host metadata must be a string -> string object, but found unparseable value`);
  39530. }
  39531. });
  39532. const bindings = parseHostBindings(hostMetadata);
  39533. const errors = verifyHostBindings(bindings, createSourceSpan(hostExpr));
  39534. if (errors.length > 0) {
  39535. throw new FatalDiagnosticError(exports.ErrorCode.HOST_BINDING_PARSE_ERROR, getHostBindingErrorNode(errors[0], hostExpr), errors.map((error) => error.msg).join('\n'));
  39536. }
  39537. return bindings;
  39538. }
  39539. /**
  39540. * Attempts to match a parser error to the host binding expression that caused it.
  39541. * @param error Error to match.
  39542. * @param hostExpr Expression declaring the host bindings.
  39543. */
  39544. function getHostBindingErrorNode(error, hostExpr) {
  39545. // In the most common case the `host` object is an object literal with string values. We can
  39546. // confidently match the error to its expression by looking at the string value that the parser
  39547. // failed to parse and the initializers for each of the properties. If we fail to match, we fall
  39548. // back to the old behavior where the error is reported on the entire `host` object.
  39549. if (ts.isObjectLiteralExpression(hostExpr) && error.relatedError instanceof ParserError) {
  39550. for (const prop of hostExpr.properties) {
  39551. if (ts.isPropertyAssignment(prop) &&
  39552. ts.isStringLiteralLike(prop.initializer) &&
  39553. prop.initializer.text === error.relatedError.input) {
  39554. return prop.initializer;
  39555. }
  39556. }
  39557. }
  39558. return hostExpr;
  39559. }
  39560. /**
  39561. * Extracts and prepares the host directives metadata from an array literal expression.
  39562. * @param rawHostDirectives Expression that defined the `hostDirectives`.
  39563. */
  39564. function extractHostDirectives(rawHostDirectives, evaluator, compilationMode, forwardRefResolver) {
  39565. const resolved = evaluator.evaluate(rawHostDirectives, forwardRefResolver);
  39566. if (!Array.isArray(resolved)) {
  39567. throw createValueHasWrongTypeError(rawHostDirectives, resolved, 'hostDirectives must be an array');
  39568. }
  39569. return resolved.map((value) => {
  39570. const hostReference = value instanceof Map ? value.get('directive') : value;
  39571. // Diagnostics
  39572. if (compilationMode !== exports.CompilationMode.LOCAL) {
  39573. if (!(hostReference instanceof Reference)) {
  39574. throw createValueHasWrongTypeError(rawHostDirectives, hostReference, 'Host directive must be a reference');
  39575. }
  39576. if (!isNamedClassDeclaration(hostReference.node)) {
  39577. throw createValueHasWrongTypeError(rawHostDirectives, hostReference, 'Host directive reference must be a class');
  39578. }
  39579. }
  39580. let directive;
  39581. let nameForErrors = (fieldName) => '@Directive.hostDirectives';
  39582. if (compilationMode === exports.CompilationMode.LOCAL && hostReference instanceof DynamicValue) {
  39583. // At the moment in local compilation we only support simple array for host directives, i.e.,
  39584. // an array consisting of the directive identifiers. We don't support forward refs or other
  39585. // expressions applied on externally imported directives. The main reason is simplicity, and
  39586. // that almost nobody wants to use host directives this way (e.g., what would be the point of
  39587. // forward ref for imported symbols?!)
  39588. if (!ts.isIdentifier(hostReference.node) &&
  39589. !ts.isPropertyAccessExpression(hostReference.node)) {
  39590. throw new FatalDiagnosticError(exports.ErrorCode.LOCAL_COMPILATION_UNSUPPORTED_EXPRESSION, hostReference.node, `In local compilation mode, host directive cannot be an expression. Use an identifier instead`);
  39591. }
  39592. directive = new WrappedNodeExpr(hostReference.node);
  39593. }
  39594. else if (hostReference instanceof Reference) {
  39595. directive = hostReference;
  39596. nameForErrors = (fieldName) => `@Directive.hostDirectives.${directive.node.name.text}.${fieldName}`;
  39597. }
  39598. else {
  39599. throw new Error('Impossible state');
  39600. }
  39601. const meta = {
  39602. directive,
  39603. isForwardReference: hostReference instanceof Reference && hostReference.synthetic,
  39604. inputs: parseHostDirectivesMapping('inputs', value, nameForErrors('input'), rawHostDirectives),
  39605. outputs: parseHostDirectivesMapping('outputs', value, nameForErrors('output'), rawHostDirectives),
  39606. };
  39607. return meta;
  39608. });
  39609. }
  39610. /**
  39611. * Parses the expression that defines the `inputs` or `outputs` of a host directive.
  39612. * @param field Name of the field that is being parsed.
  39613. * @param resolvedValue Evaluated value of the expression that defined the field.
  39614. * @param classReference Reference to the host directive class.
  39615. * @param sourceExpression Expression that the host directive is referenced in.
  39616. */
  39617. function parseHostDirectivesMapping(field, resolvedValue, nameForErrors, sourceExpression) {
  39618. if (resolvedValue instanceof Map && resolvedValue.has(field)) {
  39619. const rawInputs = resolvedValue.get(field);
  39620. if (isStringArrayOrDie(rawInputs, nameForErrors, sourceExpression)) {
  39621. return parseMappingStringArray(rawInputs);
  39622. }
  39623. }
  39624. return null;
  39625. }
  39626. /** Converts the parsed host directive information into metadata. */
  39627. function toHostDirectiveMetadata(hostDirective, context, refEmitter) {
  39628. let directive;
  39629. if (hostDirective.directive instanceof Reference) {
  39630. directive = toR3Reference(hostDirective.directive.node, hostDirective.directive, context, refEmitter);
  39631. }
  39632. else {
  39633. directive = {
  39634. value: hostDirective.directive,
  39635. type: hostDirective.directive,
  39636. };
  39637. }
  39638. return {
  39639. directive,
  39640. isForwardReference: hostDirective.isForwardReference,
  39641. inputs: hostDirective.inputs || null,
  39642. outputs: hostDirective.outputs || null,
  39643. };
  39644. }
  39645. /** Converts the parsed input information into metadata. */
  39646. function toR3InputMetadata(mapping) {
  39647. return {
  39648. classPropertyName: mapping.classPropertyName,
  39649. bindingPropertyName: mapping.bindingPropertyName,
  39650. required: mapping.required,
  39651. transformFunction: mapping.transform !== null ? new WrappedNodeExpr(mapping.transform.node) : null,
  39652. isSignal: mapping.isSignal,
  39653. };
  39654. }
  39655. const NgOriginalFile = Symbol('NgOriginalFile');
  39656. exports.UpdateMode = void 0;
  39657. (function (UpdateMode) {
  39658. /**
  39659. * A complete update creates a completely new overlay of type-checking code on top of the user's
  39660. * original program, which doesn't include type-checking code from previous calls to
  39661. * `updateFiles`.
  39662. */
  39663. UpdateMode[UpdateMode["Complete"] = 0] = "Complete";
  39664. /**
  39665. * An incremental update changes the contents of some files in the type-checking program without
  39666. * reverting any prior changes.
  39667. */
  39668. UpdateMode[UpdateMode["Incremental"] = 1] = "Incremental";
  39669. })(exports.UpdateMode || (exports.UpdateMode = {}));
  39670. /**
  39671. * A `Symbol` which is used to patch extension data onto `ts.SourceFile`s.
  39672. */
  39673. const NgExtension = Symbol('NgExtension');
  39674. /**
  39675. * Narrows a `ts.SourceFile` if it has an `NgExtension` property.
  39676. */
  39677. function isExtended(sf) {
  39678. return sf[NgExtension] !== undefined;
  39679. }
  39680. /**
  39681. * Returns the `NgExtensionData` for a given `ts.SourceFile`, adding it if none exists.
  39682. */
  39683. function sfExtensionData(sf) {
  39684. const extSf = sf;
  39685. if (extSf[NgExtension] !== undefined) {
  39686. // The file already has extension data, so return it directly.
  39687. return extSf[NgExtension];
  39688. }
  39689. // The file has no existing extension data, so add it and return it.
  39690. const extension = {
  39691. isTopLevelShim: false,
  39692. fileShim: null,
  39693. originalReferencedFiles: null,
  39694. taggedReferenceFiles: null,
  39695. };
  39696. extSf[NgExtension] = extension;
  39697. return extension;
  39698. }
  39699. /**
  39700. * Check whether `sf` is a per-file shim `ts.SourceFile`.
  39701. */
  39702. function isFileShimSourceFile(sf) {
  39703. return isExtended(sf) && sf[NgExtension].fileShim !== null;
  39704. }
  39705. /**
  39706. * Check whether `sf` is a shim `ts.SourceFile` (either a per-file shim or a top-level shim).
  39707. */
  39708. function isShim(sf) {
  39709. return isExtended(sf) && (sf[NgExtension].fileShim !== null || sf[NgExtension].isTopLevelShim);
  39710. }
  39711. /**
  39712. * Copy any shim data from one `ts.SourceFile` to another.
  39713. */
  39714. function copyFileShimData(from, to) {
  39715. if (!isFileShimSourceFile(from)) {
  39716. return;
  39717. }
  39718. sfExtensionData(to).fileShim = sfExtensionData(from).fileShim;
  39719. }
  39720. /**
  39721. * For those `ts.SourceFile`s in the `program` which have previously been tagged by a
  39722. * `ShimReferenceTagger`, restore the original `referencedFiles` array that does not have shim tags.
  39723. */
  39724. function untagAllTsFiles(program) {
  39725. for (const sf of program.getSourceFiles()) {
  39726. untagTsFile(sf);
  39727. }
  39728. }
  39729. /**
  39730. * For those `ts.SourceFile`s in the `program` which have previously been tagged by a
  39731. * `ShimReferenceTagger`, re-apply the effects of tagging by updating the `referencedFiles` array to
  39732. * the tagged version produced previously.
  39733. */
  39734. function retagAllTsFiles(program) {
  39735. for (const sf of program.getSourceFiles()) {
  39736. retagTsFile(sf);
  39737. }
  39738. }
  39739. /**
  39740. * Restore the original `referencedFiles` for the given `ts.SourceFile`.
  39741. */
  39742. function untagTsFile(sf) {
  39743. if (sf.isDeclarationFile || !isExtended(sf)) {
  39744. return;
  39745. }
  39746. const ext = sfExtensionData(sf);
  39747. if (ext.originalReferencedFiles !== null) {
  39748. sf.referencedFiles = ext.originalReferencedFiles;
  39749. }
  39750. }
  39751. /**
  39752. * Apply the previously tagged `referencedFiles` to the given `ts.SourceFile`, if it was previously
  39753. * tagged.
  39754. */
  39755. function retagTsFile(sf) {
  39756. if (sf.isDeclarationFile || !isExtended(sf)) {
  39757. return;
  39758. }
  39759. const ext = sfExtensionData(sf);
  39760. if (ext.taggedReferenceFiles !== null) {
  39761. sf.referencedFiles = ext.taggedReferenceFiles;
  39762. }
  39763. }
  39764. /**
  39765. * Describes the scope of the caller's interest in template type-checking results.
  39766. */
  39767. exports.OptimizeFor = void 0;
  39768. (function (OptimizeFor) {
  39769. /**
  39770. * Indicates that a consumer of a `TemplateTypeChecker` is only interested in results for a
  39771. * given file, and wants them as fast as possible.
  39772. *
  39773. * Calling `TemplateTypeChecker` methods successively for multiple files while specifying
  39774. * `OptimizeFor.SingleFile` can result in significant unnecessary overhead overall.
  39775. */
  39776. OptimizeFor[OptimizeFor["SingleFile"] = 0] = "SingleFile";
  39777. /**
  39778. * Indicates that a consumer of a `TemplateTypeChecker` intends to query for results pertaining
  39779. * to the entire user program, and so the type-checker should internally optimize for this case.
  39780. *
  39781. * Initial calls to retrieve type-checking information may take longer, but repeated calls to
  39782. * gather information for the whole user program will be significantly faster with this mode of
  39783. * optimization.
  39784. */
  39785. OptimizeFor[OptimizeFor["WholeProgram"] = 1] = "WholeProgram";
  39786. })(exports.OptimizeFor || (exports.OptimizeFor = {}));
  39787. /**
  39788. * Discriminant of an autocompletion source (a `Completion`).
  39789. */
  39790. var CompletionKind;
  39791. (function (CompletionKind) {
  39792. CompletionKind[CompletionKind["Reference"] = 0] = "Reference";
  39793. CompletionKind[CompletionKind["Variable"] = 1] = "Variable";
  39794. CompletionKind[CompletionKind["LetDeclaration"] = 2] = "LetDeclaration";
  39795. })(CompletionKind || (CompletionKind = {}));
  39796. /**
  39797. * Which kind of Angular Trait the import targets.
  39798. */
  39799. exports.PotentialImportKind = void 0;
  39800. (function (PotentialImportKind) {
  39801. PotentialImportKind[PotentialImportKind["NgModule"] = 0] = "NgModule";
  39802. PotentialImportKind[PotentialImportKind["Standalone"] = 1] = "Standalone";
  39803. })(exports.PotentialImportKind || (exports.PotentialImportKind = {}));
  39804. /**
  39805. * Possible modes in which to look up a potential import.
  39806. */
  39807. exports.PotentialImportMode = void 0;
  39808. (function (PotentialImportMode) {
  39809. /** Whether an import is standalone is inferred based on its metadata. */
  39810. PotentialImportMode[PotentialImportMode["Normal"] = 0] = "Normal";
  39811. /**
  39812. * An import is assumed to be standalone and is imported directly. This is useful for migrations
  39813. * where a declaration wasn't standalone when the program was created, but will become standalone
  39814. * as a part of the migration.
  39815. */
  39816. PotentialImportMode[PotentialImportMode["ForceDirect"] = 1] = "ForceDirect";
  39817. })(exports.PotentialImportMode || (exports.PotentialImportMode = {}));
  39818. exports.SymbolKind = void 0;
  39819. (function (SymbolKind) {
  39820. SymbolKind[SymbolKind["Input"] = 0] = "Input";
  39821. SymbolKind[SymbolKind["Output"] = 1] = "Output";
  39822. SymbolKind[SymbolKind["Binding"] = 2] = "Binding";
  39823. SymbolKind[SymbolKind["Reference"] = 3] = "Reference";
  39824. SymbolKind[SymbolKind["Variable"] = 4] = "Variable";
  39825. SymbolKind[SymbolKind["Directive"] = 5] = "Directive";
  39826. SymbolKind[SymbolKind["Element"] = 6] = "Element";
  39827. SymbolKind[SymbolKind["Template"] = 7] = "Template";
  39828. SymbolKind[SymbolKind["Expression"] = 8] = "Expression";
  39829. SymbolKind[SymbolKind["DomBinding"] = 9] = "DomBinding";
  39830. SymbolKind[SymbolKind["Pipe"] = 10] = "Pipe";
  39831. SymbolKind[SymbolKind["LetDeclaration"] = 11] = "LetDeclaration";
  39832. })(exports.SymbolKind || (exports.SymbolKind = {}));
  39833. /**
  39834. * Constructs a `ts.Diagnostic` for a given `ParseSourceSpan` within a template.
  39835. */
  39836. function makeTemplateDiagnostic(id, mapping, span, category, code, messageText, relatedMessages) {
  39837. if (mapping.type === 'direct') {
  39838. let relatedInformation = undefined;
  39839. if (relatedMessages !== undefined) {
  39840. relatedInformation = [];
  39841. for (const relatedMessage of relatedMessages) {
  39842. relatedInformation.push({
  39843. category: ts.DiagnosticCategory.Message,
  39844. code: 0,
  39845. file: relatedMessage.sourceFile,
  39846. start: relatedMessage.start,
  39847. length: relatedMessage.end - relatedMessage.start,
  39848. messageText: relatedMessage.text,
  39849. });
  39850. }
  39851. }
  39852. // For direct mappings, the error is shown inline as ngtsc was able to pinpoint a string
  39853. // constant within the `@Component` decorator for the template. This allows us to map the error
  39854. // directly into the bytes of the source file.
  39855. return {
  39856. source: 'ngtsc',
  39857. code,
  39858. category,
  39859. messageText,
  39860. file: mapping.node.getSourceFile(),
  39861. sourceFile: mapping.node.getSourceFile(),
  39862. typeCheckId: id,
  39863. start: span.start.offset,
  39864. length: span.end.offset - span.start.offset,
  39865. relatedInformation,
  39866. };
  39867. }
  39868. else if (mapping.type === 'indirect' || mapping.type === 'external') {
  39869. // For indirect mappings (template was declared inline, but ngtsc couldn't map it directly
  39870. // to a string constant in the decorator), the component's file name is given with a suffix
  39871. // indicating it's not the TS file being displayed, but a template.
  39872. // For external temoplates, the HTML filename is used.
  39873. const componentSf = mapping.componentClass.getSourceFile();
  39874. const componentName = mapping.componentClass.name.text;
  39875. const fileName = mapping.type === 'indirect'
  39876. ? `${componentSf.fileName} (${componentName} template)`
  39877. : mapping.templateUrl;
  39878. let relatedInformation = [];
  39879. if (relatedMessages !== undefined) {
  39880. for (const relatedMessage of relatedMessages) {
  39881. relatedInformation.push({
  39882. category: ts.DiagnosticCategory.Message,
  39883. code: 0,
  39884. file: relatedMessage.sourceFile,
  39885. start: relatedMessage.start,
  39886. length: relatedMessage.end - relatedMessage.start,
  39887. messageText: relatedMessage.text,
  39888. });
  39889. }
  39890. }
  39891. let sf;
  39892. try {
  39893. sf = getParsedTemplateSourceFile(fileName, mapping);
  39894. }
  39895. catch (e) {
  39896. const failureChain = makeDiagnosticChain(`Failed to report an error in '${fileName}' at ${span.start.line + 1}:${span.start.col + 1}`, [makeDiagnosticChain(e?.stack ?? `${e}`)]);
  39897. return {
  39898. source: 'ngtsc',
  39899. category,
  39900. code,
  39901. messageText: addDiagnosticChain(messageText, [failureChain]),
  39902. file: componentSf,
  39903. sourceFile: componentSf,
  39904. typeCheckId: id,
  39905. // mapping.node represents either the 'template' or 'templateUrl' expression. getStart()
  39906. // and getEnd() are used because they don't include surrounding whitespace.
  39907. start: mapping.node.getStart(),
  39908. length: mapping.node.getEnd() - mapping.node.getStart(),
  39909. relatedInformation,
  39910. };
  39911. }
  39912. relatedInformation.push({
  39913. category: ts.DiagnosticCategory.Message,
  39914. code: 0,
  39915. file: componentSf,
  39916. // mapping.node represents either the 'template' or 'templateUrl' expression. getStart()
  39917. // and getEnd() are used because they don't include surrounding whitespace.
  39918. start: mapping.node.getStart(),
  39919. length: mapping.node.getEnd() - mapping.node.getStart(),
  39920. messageText: `Error occurs in the template of component ${componentName}.`,
  39921. });
  39922. return {
  39923. source: 'ngtsc',
  39924. category,
  39925. code,
  39926. messageText,
  39927. file: sf,
  39928. sourceFile: componentSf,
  39929. typeCheckId: id,
  39930. start: span.start.offset,
  39931. length: span.end.offset - span.start.offset,
  39932. // Show a secondary message indicating the component whose template contains the error.
  39933. relatedInformation,
  39934. };
  39935. }
  39936. else {
  39937. throw new Error(`Unexpected source mapping type: ${mapping.type}`);
  39938. }
  39939. }
  39940. const TemplateSourceFile = Symbol('TemplateSourceFile');
  39941. function getParsedTemplateSourceFile(fileName, mapping) {
  39942. if (mapping[TemplateSourceFile] === undefined) {
  39943. mapping[TemplateSourceFile] = parseTemplateAsSourceFile(fileName, mapping.template);
  39944. }
  39945. return mapping[TemplateSourceFile];
  39946. }
  39947. function parseTemplateAsSourceFile(fileName, template) {
  39948. // TODO(alxhub): investigate creating a fake `ts.SourceFile` here instead of invoking the TS
  39949. // parser against the template (HTML is just really syntactically invalid TypeScript code ;).
  39950. return ts.createSourceFile(fileName, template, ts.ScriptTarget.Latest,
  39951. /* setParentNodes */ false, ts.ScriptKind.JSX);
  39952. }
  39953. const TYPE_CHECK_ID_MAP = Symbol('TypeCheckId');
  39954. function getTypeCheckId(clazz) {
  39955. const sf = clazz.getSourceFile();
  39956. if (sf[TYPE_CHECK_ID_MAP] === undefined) {
  39957. sf[TYPE_CHECK_ID_MAP] = new Map();
  39958. }
  39959. if (sf[TYPE_CHECK_ID_MAP].get(clazz) === undefined) {
  39960. sf[TYPE_CHECK_ID_MAP].set(clazz, `tcb${sf[TYPE_CHECK_ID_MAP].size + 1}`);
  39961. }
  39962. return sf[TYPE_CHECK_ID_MAP].get(clazz);
  39963. }
  39964. const parseSpanComment = /^(\d+),(\d+)$/;
  39965. /**
  39966. * Reads the trailing comments and finds the first match which is a span comment (i.e. 4,10) on a
  39967. * node and returns it as an `AbsoluteSourceSpan`.
  39968. *
  39969. * Will return `null` if no trailing comments on the node match the expected form of a source span.
  39970. */
  39971. function readSpanComment(node, sourceFile = node.getSourceFile()) {
  39972. return (ts.forEachTrailingCommentRange(sourceFile.text, node.getEnd(), (pos, end, kind) => {
  39973. if (kind !== ts.SyntaxKind.MultiLineCommentTrivia) {
  39974. return null;
  39975. }
  39976. const commentText = sourceFile.text.substring(pos + 2, end - 2);
  39977. const match = commentText.match(parseSpanComment);
  39978. if (match === null) {
  39979. return null;
  39980. }
  39981. return new AbsoluteSourceSpan(+match[1], +match[2]);
  39982. }) || null);
  39983. }
  39984. /** Used to identify what type the comment is. */
  39985. var CommentTriviaType;
  39986. (function (CommentTriviaType) {
  39987. CommentTriviaType["DIAGNOSTIC"] = "D";
  39988. CommentTriviaType["EXPRESSION_TYPE_IDENTIFIER"] = "T";
  39989. })(CommentTriviaType || (CommentTriviaType = {}));
  39990. /** Identifies what the TCB expression is for (for example, a directive declaration). */
  39991. var ExpressionIdentifier;
  39992. (function (ExpressionIdentifier) {
  39993. ExpressionIdentifier["DIRECTIVE"] = "DIR";
  39994. ExpressionIdentifier["COMPONENT_COMPLETION"] = "COMPCOMP";
  39995. ExpressionIdentifier["EVENT_PARAMETER"] = "EP";
  39996. ExpressionIdentifier["VARIABLE_AS_EXPRESSION"] = "VAE";
  39997. })(ExpressionIdentifier || (ExpressionIdentifier = {}));
  39998. /** Tags the node with the given expression identifier. */
  39999. function addExpressionIdentifier(node, identifier) {
  40000. ts.addSyntheticTrailingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, `${CommentTriviaType.EXPRESSION_TYPE_IDENTIFIER}:${identifier}`,
  40001. /* hasTrailingNewLine */ false);
  40002. }
  40003. const IGNORE_FOR_DIAGNOSTICS_MARKER = `${CommentTriviaType.DIAGNOSTIC}:ignore`;
  40004. /**
  40005. * Tag the `ts.Node` with an indication that any errors arising from the evaluation of the node
  40006. * should be ignored.
  40007. */
  40008. function markIgnoreDiagnostics(node) {
  40009. ts.addSyntheticTrailingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, IGNORE_FOR_DIAGNOSTICS_MARKER,
  40010. /* hasTrailingNewLine */ false);
  40011. }
  40012. /** Returns true if the node has a marker that indicates diagnostics errors should be ignored. */
  40013. function hasIgnoreForDiagnosticsMarker(node, sourceFile) {
  40014. return (ts.forEachTrailingCommentRange(sourceFile.text, node.getEnd(), (pos, end, kind) => {
  40015. if (kind !== ts.SyntaxKind.MultiLineCommentTrivia) {
  40016. return null;
  40017. }
  40018. const commentText = sourceFile.text.substring(pos + 2, end - 2);
  40019. return commentText === IGNORE_FOR_DIAGNOSTICS_MARKER;
  40020. }) === true);
  40021. }
  40022. function makeRecursiveVisitor(visitor) {
  40023. function recursiveVisitor(node) {
  40024. const res = visitor(node);
  40025. return res !== null ? res : node.forEachChild(recursiveVisitor);
  40026. }
  40027. return recursiveVisitor;
  40028. }
  40029. function getSpanFromOptions(opts) {
  40030. let withSpan = null;
  40031. if (opts.withSpan !== undefined) {
  40032. if (opts.withSpan instanceof AbsoluteSourceSpan) {
  40033. withSpan = opts.withSpan;
  40034. }
  40035. else {
  40036. withSpan = { start: opts.withSpan.start.offset, end: opts.withSpan.end.offset };
  40037. }
  40038. }
  40039. return withSpan;
  40040. }
  40041. /**
  40042. * Given a `ts.Node` with finds the first node whose matching the criteria specified
  40043. * by the `FindOptions`.
  40044. *
  40045. * Returns `null` when no `ts.Node` matches the given conditions.
  40046. */
  40047. function findFirstMatchingNode(tcb, opts) {
  40048. const withSpan = getSpanFromOptions(opts);
  40049. const withExpressionIdentifier = opts.withExpressionIdentifier;
  40050. const sf = tcb.getSourceFile();
  40051. const visitor = makeRecursiveVisitor((node) => {
  40052. if (!opts.filter(node)) {
  40053. return null;
  40054. }
  40055. if (withSpan !== null) {
  40056. const comment = readSpanComment(node, sf);
  40057. if (comment === null || withSpan.start !== comment.start || withSpan.end !== comment.end) {
  40058. return null;
  40059. }
  40060. }
  40061. if (withExpressionIdentifier !== undefined &&
  40062. !hasExpressionIdentifier(sf, node, withExpressionIdentifier)) {
  40063. return null;
  40064. }
  40065. return node;
  40066. });
  40067. return tcb.forEachChild(visitor) ?? null;
  40068. }
  40069. /**
  40070. * Given a `ts.Node` with source span comments, finds the first node whose source span comment
  40071. * matches the given `sourceSpan`. Additionally, the `filter` function allows matching only
  40072. * `ts.Nodes` of a given type, which provides the ability to select only matches of a given type
  40073. * when there may be more than one.
  40074. *
  40075. * Returns `null` when no `ts.Node` matches the given conditions.
  40076. */
  40077. function findAllMatchingNodes(tcb, opts) {
  40078. const withSpan = getSpanFromOptions(opts);
  40079. const withExpressionIdentifier = opts.withExpressionIdentifier;
  40080. const results = [];
  40081. const stack = [tcb];
  40082. const sf = tcb.getSourceFile();
  40083. while (stack.length > 0) {
  40084. const node = stack.pop();
  40085. if (!opts.filter(node)) {
  40086. stack.push(...node.getChildren());
  40087. continue;
  40088. }
  40089. if (withSpan !== null) {
  40090. const comment = readSpanComment(node, sf);
  40091. if (comment === null || withSpan.start !== comment.start || withSpan.end !== comment.end) {
  40092. stack.push(...node.getChildren());
  40093. continue;
  40094. }
  40095. }
  40096. if (withExpressionIdentifier !== undefined &&
  40097. !hasExpressionIdentifier(sf, node, withExpressionIdentifier)) {
  40098. continue;
  40099. }
  40100. results.push(node);
  40101. }
  40102. return results;
  40103. }
  40104. function hasExpressionIdentifier(sourceFile, node, identifier) {
  40105. return (ts.forEachTrailingCommentRange(sourceFile.text, node.getEnd(), (pos, end, kind) => {
  40106. if (kind !== ts.SyntaxKind.MultiLineCommentTrivia) {
  40107. return false;
  40108. }
  40109. const commentText = sourceFile.text.substring(pos + 2, end - 2);
  40110. return commentText === `${CommentTriviaType.EXPRESSION_TYPE_IDENTIFIER}:${identifier}`;
  40111. }) || false);
  40112. }
  40113. /**
  40114. * Powers autocompletion for a specific component.
  40115. *
  40116. * Internally caches autocompletion results, and must be discarded if the component template or
  40117. * surrounding TS program have changed.
  40118. */
  40119. class CompletionEngine {
  40120. tcb;
  40121. data;
  40122. tcbPath;
  40123. tcbIsShim;
  40124. componentContext;
  40125. /**
  40126. * Cache of completions for various levels of the template, including the root template (`null`).
  40127. * Memoizes `getTemplateContextCompletions`.
  40128. */
  40129. templateContextCache = new Map();
  40130. expressionCompletionCache = new Map();
  40131. constructor(tcb, data, tcbPath, tcbIsShim) {
  40132. this.tcb = tcb;
  40133. this.data = data;
  40134. this.tcbPath = tcbPath;
  40135. this.tcbIsShim = tcbIsShim;
  40136. // Find the component completion expression within the TCB. This looks like: `ctx. /* ... */;`
  40137. const globalRead = findFirstMatchingNode(this.tcb, {
  40138. filter: ts.isPropertyAccessExpression,
  40139. withExpressionIdentifier: ExpressionIdentifier.COMPONENT_COMPLETION,
  40140. });
  40141. if (globalRead !== null) {
  40142. this.componentContext = {
  40143. tcbPath: this.tcbPath,
  40144. isShimFile: this.tcbIsShim,
  40145. // `globalRead.name` is an empty `ts.Identifier`, so its start position immediately follows
  40146. // the `.` in `ctx.`. TS autocompletion APIs can then be used to access completion results
  40147. // for the component context.
  40148. positionInFile: globalRead.name.getStart(),
  40149. };
  40150. }
  40151. else {
  40152. this.componentContext = null;
  40153. }
  40154. }
  40155. /**
  40156. * Get global completions within the given template context and AST node.
  40157. *
  40158. * @param context the given template context - either a `TmplAstTemplate` embedded view, or `null`
  40159. * for the root
  40160. * template context.
  40161. * @param node the given AST node
  40162. */
  40163. getGlobalCompletions(context, node) {
  40164. if (this.componentContext === null) {
  40165. return null;
  40166. }
  40167. const templateContext = this.getTemplateContextCompletions(context);
  40168. if (templateContext === null) {
  40169. return null;
  40170. }
  40171. let nodeContext = null;
  40172. if (node instanceof EmptyExpr$1) {
  40173. const nodeLocation = findFirstMatchingNode(this.tcb, {
  40174. filter: ts.isIdentifier,
  40175. withSpan: node.sourceSpan,
  40176. });
  40177. if (nodeLocation !== null) {
  40178. nodeContext = {
  40179. tcbPath: this.tcbPath,
  40180. isShimFile: this.tcbIsShim,
  40181. positionInFile: nodeLocation.getStart(),
  40182. };
  40183. }
  40184. }
  40185. if (node instanceof PropertyRead && node.receiver instanceof ImplicitReceiver) {
  40186. const nodeLocation = findFirstMatchingNode(this.tcb, {
  40187. filter: ts.isPropertyAccessExpression,
  40188. withSpan: node.sourceSpan,
  40189. });
  40190. if (nodeLocation) {
  40191. nodeContext = {
  40192. tcbPath: this.tcbPath,
  40193. isShimFile: this.tcbIsShim,
  40194. positionInFile: nodeLocation.getStart(),
  40195. };
  40196. }
  40197. }
  40198. return {
  40199. componentContext: this.componentContext,
  40200. templateContext,
  40201. nodeContext,
  40202. };
  40203. }
  40204. getExpressionCompletionLocation(expr) {
  40205. if (this.expressionCompletionCache.has(expr)) {
  40206. return this.expressionCompletionCache.get(expr);
  40207. }
  40208. // Completion works inside property reads and method calls.
  40209. let tsExpr = null;
  40210. if (expr instanceof PropertyRead || expr instanceof PropertyWrite) {
  40211. // Non-safe navigation operations are trivial: `foo.bar` or `foo.bar()`
  40212. tsExpr = findFirstMatchingNode(this.tcb, {
  40213. filter: ts.isPropertyAccessExpression,
  40214. withSpan: expr.nameSpan,
  40215. });
  40216. }
  40217. else if (expr instanceof SafePropertyRead) {
  40218. // Safe navigation operations are a little more complex, and involve a ternary. Completion
  40219. // happens in the "true" case of the ternary.
  40220. const ternaryExpr = findFirstMatchingNode(this.tcb, {
  40221. filter: ts.isParenthesizedExpression,
  40222. withSpan: expr.sourceSpan,
  40223. });
  40224. if (ternaryExpr === null || !ts.isConditionalExpression(ternaryExpr.expression)) {
  40225. return null;
  40226. }
  40227. const whenTrue = ternaryExpr.expression.whenTrue;
  40228. if (ts.isPropertyAccessExpression(whenTrue)) {
  40229. tsExpr = whenTrue;
  40230. }
  40231. else if (ts.isCallExpression(whenTrue) &&
  40232. ts.isPropertyAccessExpression(whenTrue.expression)) {
  40233. tsExpr = whenTrue.expression;
  40234. }
  40235. }
  40236. if (tsExpr === null) {
  40237. return null;
  40238. }
  40239. const res = {
  40240. tcbPath: this.tcbPath,
  40241. isShimFile: this.tcbIsShim,
  40242. positionInFile: tsExpr.name.getEnd(),
  40243. };
  40244. this.expressionCompletionCache.set(expr, res);
  40245. return res;
  40246. }
  40247. getLiteralCompletionLocation(expr) {
  40248. if (this.expressionCompletionCache.has(expr)) {
  40249. return this.expressionCompletionCache.get(expr);
  40250. }
  40251. let tsExpr = null;
  40252. if (expr instanceof TextAttribute) {
  40253. const strNode = findFirstMatchingNode(this.tcb, {
  40254. filter: ts.isParenthesizedExpression,
  40255. withSpan: expr.sourceSpan,
  40256. });
  40257. if (strNode !== null && ts.isStringLiteral(strNode.expression)) {
  40258. tsExpr = strNode.expression;
  40259. }
  40260. }
  40261. else {
  40262. tsExpr = findFirstMatchingNode(this.tcb, {
  40263. filter: (n) => ts.isStringLiteral(n) || ts.isNumericLiteral(n),
  40264. withSpan: expr.sourceSpan,
  40265. });
  40266. }
  40267. if (tsExpr === null) {
  40268. return null;
  40269. }
  40270. let positionInShimFile = tsExpr.getEnd();
  40271. if (ts.isStringLiteral(tsExpr)) {
  40272. // In the shimFile, if `tsExpr` is a string, the position should be in the quotes.
  40273. positionInShimFile -= 1;
  40274. }
  40275. const res = {
  40276. tcbPath: this.tcbPath,
  40277. isShimFile: this.tcbIsShim,
  40278. positionInFile: positionInShimFile,
  40279. };
  40280. this.expressionCompletionCache.set(expr, res);
  40281. return res;
  40282. }
  40283. /**
  40284. * Get global completions within the given template context - either a `TmplAstTemplate` embedded
  40285. * view, or `null` for the root context.
  40286. */
  40287. getTemplateContextCompletions(context) {
  40288. if (this.templateContextCache.has(context)) {
  40289. return this.templateContextCache.get(context);
  40290. }
  40291. const templateContext = new Map();
  40292. // The bound template already has details about the references and variables in scope in the
  40293. // `context` template - they just need to be converted to `Completion`s.
  40294. for (const node of this.data.boundTarget.getEntitiesInScope(context)) {
  40295. if (node instanceof Reference$1) {
  40296. templateContext.set(node.name, {
  40297. kind: CompletionKind.Reference,
  40298. node,
  40299. });
  40300. }
  40301. else if (node instanceof LetDeclaration$1) {
  40302. templateContext.set(node.name, {
  40303. kind: CompletionKind.LetDeclaration,
  40304. node,
  40305. });
  40306. }
  40307. else {
  40308. templateContext.set(node.name, {
  40309. kind: CompletionKind.Variable,
  40310. node,
  40311. });
  40312. }
  40313. }
  40314. this.templateContextCache.set(context, templateContext);
  40315. return templateContext;
  40316. }
  40317. }
  40318. const comma = ','.charCodeAt(0);
  40319. const semicolon = ';'.charCodeAt(0);
  40320. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  40321. const intToChar = new Uint8Array(64); // 64 possible chars.
  40322. const charToInt = new Uint8Array(128); // z is 122 in ASCII
  40323. for (let i = 0; i < chars.length; i++) {
  40324. const c = chars.charCodeAt(i);
  40325. intToChar[i] = c;
  40326. charToInt[c] = i;
  40327. }
  40328. function encodeInteger(builder, num, relative) {
  40329. let delta = num - relative;
  40330. delta = delta < 0 ? (-delta << 1) | 1 : delta << 1;
  40331. do {
  40332. let clamped = delta & 0b011111;
  40333. delta >>>= 5;
  40334. if (delta > 0)
  40335. clamped |= 0b100000;
  40336. builder.write(intToChar[clamped]);
  40337. } while (delta > 0);
  40338. return num;
  40339. }
  40340. const bufLength = 1024 * 16;
  40341. // Provide a fallback for older environments.
  40342. const td = typeof TextDecoder !== 'undefined'
  40343. ? /* #__PURE__ */ new TextDecoder()
  40344. : typeof Buffer !== 'undefined'
  40345. ? {
  40346. decode(buf) {
  40347. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  40348. return out.toString();
  40349. },
  40350. }
  40351. : {
  40352. decode(buf) {
  40353. let out = '';
  40354. for (let i = 0; i < buf.length; i++) {
  40355. out += String.fromCharCode(buf[i]);
  40356. }
  40357. return out;
  40358. },
  40359. };
  40360. class StringWriter {
  40361. constructor() {
  40362. this.pos = 0;
  40363. this.out = '';
  40364. this.buffer = new Uint8Array(bufLength);
  40365. }
  40366. write(v) {
  40367. const { buffer } = this;
  40368. buffer[this.pos++] = v;
  40369. if (this.pos === bufLength) {
  40370. this.out += td.decode(buffer);
  40371. this.pos = 0;
  40372. }
  40373. }
  40374. flush() {
  40375. const { buffer, out, pos } = this;
  40376. return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
  40377. }
  40378. }
  40379. function encode(decoded) {
  40380. const writer = new StringWriter();
  40381. let sourcesIndex = 0;
  40382. let sourceLine = 0;
  40383. let sourceColumn = 0;
  40384. let namesIndex = 0;
  40385. for (let i = 0; i < decoded.length; i++) {
  40386. const line = decoded[i];
  40387. if (i > 0)
  40388. writer.write(semicolon);
  40389. if (line.length === 0)
  40390. continue;
  40391. let genColumn = 0;
  40392. for (let j = 0; j < line.length; j++) {
  40393. const segment = line[j];
  40394. if (j > 0)
  40395. writer.write(comma);
  40396. genColumn = encodeInteger(writer, segment[0], genColumn);
  40397. if (segment.length === 1)
  40398. continue;
  40399. sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
  40400. sourceLine = encodeInteger(writer, segment[2], sourceLine);
  40401. sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
  40402. if (segment.length === 4)
  40403. continue;
  40404. namesIndex = encodeInteger(writer, segment[4], namesIndex);
  40405. }
  40406. }
  40407. return writer.flush();
  40408. }
  40409. class BitSet {
  40410. constructor(arg) {
  40411. this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
  40412. }
  40413. add(n) {
  40414. this.bits[n >> 5] |= 1 << (n & 31);
  40415. }
  40416. has(n) {
  40417. return !!(this.bits[n >> 5] & (1 << (n & 31)));
  40418. }
  40419. }
  40420. class Chunk {
  40421. constructor(start, end, content) {
  40422. this.start = start;
  40423. this.end = end;
  40424. this.original = content;
  40425. this.intro = '';
  40426. this.outro = '';
  40427. this.content = content;
  40428. this.storeName = false;
  40429. this.edited = false;
  40430. {
  40431. this.previous = null;
  40432. this.next = null;
  40433. }
  40434. }
  40435. appendLeft(content) {
  40436. this.outro += content;
  40437. }
  40438. appendRight(content) {
  40439. this.intro = this.intro + content;
  40440. }
  40441. clone() {
  40442. const chunk = new Chunk(this.start, this.end, this.original);
  40443. chunk.intro = this.intro;
  40444. chunk.outro = this.outro;
  40445. chunk.content = this.content;
  40446. chunk.storeName = this.storeName;
  40447. chunk.edited = this.edited;
  40448. return chunk;
  40449. }
  40450. contains(index) {
  40451. return this.start < index && index < this.end;
  40452. }
  40453. eachNext(fn) {
  40454. let chunk = this;
  40455. while (chunk) {
  40456. fn(chunk);
  40457. chunk = chunk.next;
  40458. }
  40459. }
  40460. eachPrevious(fn) {
  40461. let chunk = this;
  40462. while (chunk) {
  40463. fn(chunk);
  40464. chunk = chunk.previous;
  40465. }
  40466. }
  40467. edit(content, storeName, contentOnly) {
  40468. this.content = content;
  40469. if (!contentOnly) {
  40470. this.intro = '';
  40471. this.outro = '';
  40472. }
  40473. this.storeName = storeName;
  40474. this.edited = true;
  40475. return this;
  40476. }
  40477. prependLeft(content) {
  40478. this.outro = content + this.outro;
  40479. }
  40480. prependRight(content) {
  40481. this.intro = content + this.intro;
  40482. }
  40483. reset() {
  40484. this.intro = '';
  40485. this.outro = '';
  40486. if (this.edited) {
  40487. this.content = this.original;
  40488. this.storeName = false;
  40489. this.edited = false;
  40490. }
  40491. }
  40492. split(index) {
  40493. const sliceIndex = index - this.start;
  40494. const originalBefore = this.original.slice(0, sliceIndex);
  40495. const originalAfter = this.original.slice(sliceIndex);
  40496. this.original = originalBefore;
  40497. const newChunk = new Chunk(index, this.end, originalAfter);
  40498. newChunk.outro = this.outro;
  40499. this.outro = '';
  40500. this.end = index;
  40501. if (this.edited) {
  40502. // after split we should save the edit content record into the correct chunk
  40503. // to make sure sourcemap correct
  40504. // For example:
  40505. // ' test'.trim()
  40506. // split -> ' ' + 'test'
  40507. // ✔️ edit -> '' + 'test'
  40508. // ✖️ edit -> 'test' + ''
  40509. // TODO is this block necessary?...
  40510. newChunk.edit('', false);
  40511. this.content = '';
  40512. } else {
  40513. this.content = originalBefore;
  40514. }
  40515. newChunk.next = this.next;
  40516. if (newChunk.next) newChunk.next.previous = newChunk;
  40517. newChunk.previous = this;
  40518. this.next = newChunk;
  40519. return newChunk;
  40520. }
  40521. toString() {
  40522. return this.intro + this.content + this.outro;
  40523. }
  40524. trimEnd(rx) {
  40525. this.outro = this.outro.replace(rx, '');
  40526. if (this.outro.length) return true;
  40527. const trimmed = this.content.replace(rx, '');
  40528. if (trimmed.length) {
  40529. if (trimmed !== this.content) {
  40530. this.split(this.start + trimmed.length).edit('', undefined, true);
  40531. if (this.edited) {
  40532. // save the change, if it has been edited
  40533. this.edit(trimmed, this.storeName, true);
  40534. }
  40535. }
  40536. return true;
  40537. } else {
  40538. this.edit('', undefined, true);
  40539. this.intro = this.intro.replace(rx, '');
  40540. if (this.intro.length) return true;
  40541. }
  40542. }
  40543. trimStart(rx) {
  40544. this.intro = this.intro.replace(rx, '');
  40545. if (this.intro.length) return true;
  40546. const trimmed = this.content.replace(rx, '');
  40547. if (trimmed.length) {
  40548. if (trimmed !== this.content) {
  40549. const newChunk = this.split(this.end - trimmed.length);
  40550. if (this.edited) {
  40551. // save the change, if it has been edited
  40552. newChunk.edit(trimmed, this.storeName, true);
  40553. }
  40554. this.edit('', undefined, true);
  40555. }
  40556. return true;
  40557. } else {
  40558. this.edit('', undefined, true);
  40559. this.outro = this.outro.replace(rx, '');
  40560. if (this.outro.length) return true;
  40561. }
  40562. }
  40563. }
  40564. function getBtoa() {
  40565. if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
  40566. return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
  40567. } else if (typeof Buffer === 'function') {
  40568. return (str) => Buffer.from(str, 'utf-8').toString('base64');
  40569. } else {
  40570. return () => {
  40571. throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
  40572. };
  40573. }
  40574. }
  40575. const btoa = /*#__PURE__*/ getBtoa();
  40576. class SourceMap {
  40577. constructor(properties) {
  40578. this.version = 3;
  40579. this.file = properties.file;
  40580. this.sources = properties.sources;
  40581. this.sourcesContent = properties.sourcesContent;
  40582. this.names = properties.names;
  40583. this.mappings = encode(properties.mappings);
  40584. if (typeof properties.x_google_ignoreList !== 'undefined') {
  40585. this.x_google_ignoreList = properties.x_google_ignoreList;
  40586. }
  40587. if (typeof properties.debugId !== 'undefined') {
  40588. this.debugId = properties.debugId;
  40589. }
  40590. }
  40591. toString() {
  40592. return JSON.stringify(this);
  40593. }
  40594. toUrl() {
  40595. return 'data:application/json;charset=utf-8;base64,' + btoa(this.toString());
  40596. }
  40597. }
  40598. function guessIndent(code) {
  40599. const lines = code.split('\n');
  40600. const tabbed = lines.filter((line) => /^\t+/.test(line));
  40601. const spaced = lines.filter((line) => /^ {2,}/.test(line));
  40602. if (tabbed.length === 0 && spaced.length === 0) {
  40603. return null;
  40604. }
  40605. // More lines tabbed than spaced? Assume tabs, and
  40606. // default to tabs in the case of a tie (or nothing
  40607. // to go on)
  40608. if (tabbed.length >= spaced.length) {
  40609. return '\t';
  40610. }
  40611. // Otherwise, we need to guess the multiple
  40612. const min = spaced.reduce((previous, current) => {
  40613. const numSpaces = /^ +/.exec(current)[0].length;
  40614. return Math.min(numSpaces, previous);
  40615. }, Infinity);
  40616. return new Array(min + 1).join(' ');
  40617. }
  40618. function getRelativePath(from, to) {
  40619. const fromParts = from.split(/[/\\]/);
  40620. const toParts = to.split(/[/\\]/);
  40621. fromParts.pop(); // get dirname
  40622. while (fromParts[0] === toParts[0]) {
  40623. fromParts.shift();
  40624. toParts.shift();
  40625. }
  40626. if (fromParts.length) {
  40627. let i = fromParts.length;
  40628. while (i--) fromParts[i] = '..';
  40629. }
  40630. return fromParts.concat(toParts).join('/');
  40631. }
  40632. const toString = Object.prototype.toString;
  40633. function isObject(thing) {
  40634. return toString.call(thing) === '[object Object]';
  40635. }
  40636. function getLocator(source) {
  40637. const originalLines = source.split('\n');
  40638. const lineOffsets = [];
  40639. for (let i = 0, pos = 0; i < originalLines.length; i++) {
  40640. lineOffsets.push(pos);
  40641. pos += originalLines[i].length + 1;
  40642. }
  40643. return function locate(index) {
  40644. let i = 0;
  40645. let j = lineOffsets.length;
  40646. while (i < j) {
  40647. const m = (i + j) >> 1;
  40648. if (index < lineOffsets[m]) {
  40649. j = m;
  40650. } else {
  40651. i = m + 1;
  40652. }
  40653. }
  40654. const line = i - 1;
  40655. const column = index - lineOffsets[line];
  40656. return { line, column };
  40657. };
  40658. }
  40659. const wordRegex = /\w/;
  40660. class Mappings {
  40661. constructor(hires) {
  40662. this.hires = hires;
  40663. this.generatedCodeLine = 0;
  40664. this.generatedCodeColumn = 0;
  40665. this.raw = [];
  40666. this.rawSegments = this.raw[this.generatedCodeLine] = [];
  40667. this.pending = null;
  40668. }
  40669. addEdit(sourceIndex, content, loc, nameIndex) {
  40670. if (content.length) {
  40671. const contentLengthMinusOne = content.length - 1;
  40672. let contentLineEnd = content.indexOf('\n', 0);
  40673. let previousContentLineEnd = -1;
  40674. // Loop through each line in the content and add a segment, but stop if the last line is empty,
  40675. // else code afterwards would fill one line too many
  40676. while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
  40677. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  40678. if (nameIndex >= 0) {
  40679. segment.push(nameIndex);
  40680. }
  40681. this.rawSegments.push(segment);
  40682. this.generatedCodeLine += 1;
  40683. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  40684. this.generatedCodeColumn = 0;
  40685. previousContentLineEnd = contentLineEnd;
  40686. contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
  40687. }
  40688. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  40689. if (nameIndex >= 0) {
  40690. segment.push(nameIndex);
  40691. }
  40692. this.rawSegments.push(segment);
  40693. this.advance(content.slice(previousContentLineEnd + 1));
  40694. } else if (this.pending) {
  40695. this.rawSegments.push(this.pending);
  40696. this.advance(content);
  40697. }
  40698. this.pending = null;
  40699. }
  40700. addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
  40701. let originalCharIndex = chunk.start;
  40702. let first = true;
  40703. // when iterating each char, check if it's in a word boundary
  40704. let charInHiresBoundary = false;
  40705. while (originalCharIndex < chunk.end) {
  40706. if (original[originalCharIndex] === '\n') {
  40707. loc.line += 1;
  40708. loc.column = 0;
  40709. this.generatedCodeLine += 1;
  40710. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  40711. this.generatedCodeColumn = 0;
  40712. first = true;
  40713. charInHiresBoundary = false;
  40714. } else {
  40715. if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
  40716. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  40717. if (this.hires === 'boundary') {
  40718. // in hires "boundary", group segments per word boundary than per char
  40719. if (wordRegex.test(original[originalCharIndex])) {
  40720. // for first char in the boundary found, start the boundary by pushing a segment
  40721. if (!charInHiresBoundary) {
  40722. this.rawSegments.push(segment);
  40723. charInHiresBoundary = true;
  40724. }
  40725. } else {
  40726. // for non-word char, end the boundary by pushing a segment
  40727. this.rawSegments.push(segment);
  40728. charInHiresBoundary = false;
  40729. }
  40730. } else {
  40731. this.rawSegments.push(segment);
  40732. }
  40733. }
  40734. loc.column += 1;
  40735. this.generatedCodeColumn += 1;
  40736. first = false;
  40737. }
  40738. originalCharIndex += 1;
  40739. }
  40740. this.pending = null;
  40741. }
  40742. advance(str) {
  40743. if (!str) return;
  40744. const lines = str.split('\n');
  40745. if (lines.length > 1) {
  40746. for (let i = 0; i < lines.length - 1; i++) {
  40747. this.generatedCodeLine++;
  40748. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  40749. }
  40750. this.generatedCodeColumn = 0;
  40751. }
  40752. this.generatedCodeColumn += lines[lines.length - 1].length;
  40753. }
  40754. }
  40755. const n = '\n';
  40756. const warned = {
  40757. insertLeft: false,
  40758. insertRight: false,
  40759. storeName: false,
  40760. };
  40761. class MagicString {
  40762. constructor(string, options = {}) {
  40763. const chunk = new Chunk(0, string.length, string);
  40764. Object.defineProperties(this, {
  40765. original: { writable: true, value: string },
  40766. outro: { writable: true, value: '' },
  40767. intro: { writable: true, value: '' },
  40768. firstChunk: { writable: true, value: chunk },
  40769. lastChunk: { writable: true, value: chunk },
  40770. lastSearchedChunk: { writable: true, value: chunk },
  40771. byStart: { writable: true, value: {} },
  40772. byEnd: { writable: true, value: {} },
  40773. filename: { writable: true, value: options.filename },
  40774. indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
  40775. sourcemapLocations: { writable: true, value: new BitSet() },
  40776. storedNames: { writable: true, value: {} },
  40777. indentStr: { writable: true, value: undefined },
  40778. ignoreList: { writable: true, value: options.ignoreList },
  40779. offset: { writable: true, value: options.offset || 0 },
  40780. });
  40781. this.byStart[0] = chunk;
  40782. this.byEnd[string.length] = chunk;
  40783. }
  40784. addSourcemapLocation(char) {
  40785. this.sourcemapLocations.add(char);
  40786. }
  40787. append(content) {
  40788. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  40789. this.outro += content;
  40790. return this;
  40791. }
  40792. appendLeft(index, content) {
  40793. index = index + this.offset;
  40794. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  40795. this._split(index);
  40796. const chunk = this.byEnd[index];
  40797. if (chunk) {
  40798. chunk.appendLeft(content);
  40799. } else {
  40800. this.intro += content;
  40801. }
  40802. return this;
  40803. }
  40804. appendRight(index, content) {
  40805. index = index + this.offset;
  40806. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  40807. this._split(index);
  40808. const chunk = this.byStart[index];
  40809. if (chunk) {
  40810. chunk.appendRight(content);
  40811. } else {
  40812. this.outro += content;
  40813. }
  40814. return this;
  40815. }
  40816. clone() {
  40817. const cloned = new MagicString(this.original, { filename: this.filename, offset: this.offset });
  40818. let originalChunk = this.firstChunk;
  40819. let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
  40820. while (originalChunk) {
  40821. cloned.byStart[clonedChunk.start] = clonedChunk;
  40822. cloned.byEnd[clonedChunk.end] = clonedChunk;
  40823. const nextOriginalChunk = originalChunk.next;
  40824. const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
  40825. if (nextClonedChunk) {
  40826. clonedChunk.next = nextClonedChunk;
  40827. nextClonedChunk.previous = clonedChunk;
  40828. clonedChunk = nextClonedChunk;
  40829. }
  40830. originalChunk = nextOriginalChunk;
  40831. }
  40832. cloned.lastChunk = clonedChunk;
  40833. if (this.indentExclusionRanges) {
  40834. cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
  40835. }
  40836. cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
  40837. cloned.intro = this.intro;
  40838. cloned.outro = this.outro;
  40839. return cloned;
  40840. }
  40841. generateDecodedMap(options) {
  40842. options = options || {};
  40843. const sourceIndex = 0;
  40844. const names = Object.keys(this.storedNames);
  40845. const mappings = new Mappings(options.hires);
  40846. const locate = getLocator(this.original);
  40847. if (this.intro) {
  40848. mappings.advance(this.intro);
  40849. }
  40850. this.firstChunk.eachNext((chunk) => {
  40851. const loc = locate(chunk.start);
  40852. if (chunk.intro.length) mappings.advance(chunk.intro);
  40853. if (chunk.edited) {
  40854. mappings.addEdit(
  40855. sourceIndex,
  40856. chunk.content,
  40857. loc,
  40858. chunk.storeName ? names.indexOf(chunk.original) : -1,
  40859. );
  40860. } else {
  40861. mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
  40862. }
  40863. if (chunk.outro.length) mappings.advance(chunk.outro);
  40864. });
  40865. return {
  40866. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  40867. sources: [
  40868. options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
  40869. ],
  40870. sourcesContent: options.includeContent ? [this.original] : undefined,
  40871. names,
  40872. mappings: mappings.raw,
  40873. x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
  40874. };
  40875. }
  40876. generateMap(options) {
  40877. return new SourceMap(this.generateDecodedMap(options));
  40878. }
  40879. _ensureindentStr() {
  40880. if (this.indentStr === undefined) {
  40881. this.indentStr = guessIndent(this.original);
  40882. }
  40883. }
  40884. _getRawIndentString() {
  40885. this._ensureindentStr();
  40886. return this.indentStr;
  40887. }
  40888. getIndentString() {
  40889. this._ensureindentStr();
  40890. return this.indentStr === null ? '\t' : this.indentStr;
  40891. }
  40892. indent(indentStr, options) {
  40893. const pattern = /^[^\r\n]/gm;
  40894. if (isObject(indentStr)) {
  40895. options = indentStr;
  40896. indentStr = undefined;
  40897. }
  40898. if (indentStr === undefined) {
  40899. this._ensureindentStr();
  40900. indentStr = this.indentStr || '\t';
  40901. }
  40902. if (indentStr === '') return this; // noop
  40903. options = options || {};
  40904. // Process exclusion ranges
  40905. const isExcluded = {};
  40906. if (options.exclude) {
  40907. const exclusions =
  40908. typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
  40909. exclusions.forEach((exclusion) => {
  40910. for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
  40911. isExcluded[i] = true;
  40912. }
  40913. });
  40914. }
  40915. let shouldIndentNextCharacter = options.indentStart !== false;
  40916. const replacer = (match) => {
  40917. if (shouldIndentNextCharacter) return `${indentStr}${match}`;
  40918. shouldIndentNextCharacter = true;
  40919. return match;
  40920. };
  40921. this.intro = this.intro.replace(pattern, replacer);
  40922. let charIndex = 0;
  40923. let chunk = this.firstChunk;
  40924. while (chunk) {
  40925. const end = chunk.end;
  40926. if (chunk.edited) {
  40927. if (!isExcluded[charIndex]) {
  40928. chunk.content = chunk.content.replace(pattern, replacer);
  40929. if (chunk.content.length) {
  40930. shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
  40931. }
  40932. }
  40933. } else {
  40934. charIndex = chunk.start;
  40935. while (charIndex < end) {
  40936. if (!isExcluded[charIndex]) {
  40937. const char = this.original[charIndex];
  40938. if (char === '\n') {
  40939. shouldIndentNextCharacter = true;
  40940. } else if (char !== '\r' && shouldIndentNextCharacter) {
  40941. shouldIndentNextCharacter = false;
  40942. if (charIndex === chunk.start) {
  40943. chunk.prependRight(indentStr);
  40944. } else {
  40945. this._splitChunk(chunk, charIndex);
  40946. chunk = chunk.next;
  40947. chunk.prependRight(indentStr);
  40948. }
  40949. }
  40950. }
  40951. charIndex += 1;
  40952. }
  40953. }
  40954. charIndex = chunk.end;
  40955. chunk = chunk.next;
  40956. }
  40957. this.outro = this.outro.replace(pattern, replacer);
  40958. return this;
  40959. }
  40960. insert() {
  40961. throw new Error(
  40962. 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
  40963. );
  40964. }
  40965. insertLeft(index, content) {
  40966. if (!warned.insertLeft) {
  40967. console.warn(
  40968. 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
  40969. );
  40970. warned.insertLeft = true;
  40971. }
  40972. return this.appendLeft(index, content);
  40973. }
  40974. insertRight(index, content) {
  40975. if (!warned.insertRight) {
  40976. console.warn(
  40977. 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
  40978. );
  40979. warned.insertRight = true;
  40980. }
  40981. return this.prependRight(index, content);
  40982. }
  40983. move(start, end, index) {
  40984. start = start + this.offset;
  40985. end = end + this.offset;
  40986. index = index + this.offset;
  40987. if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
  40988. this._split(start);
  40989. this._split(end);
  40990. this._split(index);
  40991. const first = this.byStart[start];
  40992. const last = this.byEnd[end];
  40993. const oldLeft = first.previous;
  40994. const oldRight = last.next;
  40995. const newRight = this.byStart[index];
  40996. if (!newRight && last === this.lastChunk) return this;
  40997. const newLeft = newRight ? newRight.previous : this.lastChunk;
  40998. if (oldLeft) oldLeft.next = oldRight;
  40999. if (oldRight) oldRight.previous = oldLeft;
  41000. if (newLeft) newLeft.next = first;
  41001. if (newRight) newRight.previous = last;
  41002. if (!first.previous) this.firstChunk = last.next;
  41003. if (!last.next) {
  41004. this.lastChunk = first.previous;
  41005. this.lastChunk.next = null;
  41006. }
  41007. first.previous = newLeft;
  41008. last.next = newRight || null;
  41009. if (!newLeft) this.firstChunk = first;
  41010. if (!newRight) this.lastChunk = last;
  41011. return this;
  41012. }
  41013. overwrite(start, end, content, options) {
  41014. options = options || {};
  41015. return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
  41016. }
  41017. update(start, end, content, options) {
  41018. start = start + this.offset;
  41019. end = end + this.offset;
  41020. if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
  41021. if (this.original.length !== 0) {
  41022. while (start < 0) start += this.original.length;
  41023. while (end < 0) end += this.original.length;
  41024. }
  41025. if (end > this.original.length) throw new Error('end is out of bounds');
  41026. if (start === end)
  41027. throw new Error(
  41028. 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
  41029. );
  41030. this._split(start);
  41031. this._split(end);
  41032. if (options === true) {
  41033. if (!warned.storeName) {
  41034. console.warn(
  41035. 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
  41036. );
  41037. warned.storeName = true;
  41038. }
  41039. options = { storeName: true };
  41040. }
  41041. const storeName = options !== undefined ? options.storeName : false;
  41042. const overwrite = options !== undefined ? options.overwrite : false;
  41043. if (storeName) {
  41044. const original = this.original.slice(start, end);
  41045. Object.defineProperty(this.storedNames, original, {
  41046. writable: true,
  41047. value: true,
  41048. enumerable: true,
  41049. });
  41050. }
  41051. const first = this.byStart[start];
  41052. const last = this.byEnd[end];
  41053. if (first) {
  41054. let chunk = first;
  41055. while (chunk !== last) {
  41056. if (chunk.next !== this.byStart[chunk.end]) {
  41057. throw new Error('Cannot overwrite across a split point');
  41058. }
  41059. chunk = chunk.next;
  41060. chunk.edit('', false);
  41061. }
  41062. first.edit(content, storeName, !overwrite);
  41063. } else {
  41064. // must be inserting at the end
  41065. const newChunk = new Chunk(start, end, '').edit(content, storeName);
  41066. // TODO last chunk in the array may not be the last chunk, if it's moved...
  41067. last.next = newChunk;
  41068. newChunk.previous = last;
  41069. }
  41070. return this;
  41071. }
  41072. prepend(content) {
  41073. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  41074. this.intro = content + this.intro;
  41075. return this;
  41076. }
  41077. prependLeft(index, content) {
  41078. index = index + this.offset;
  41079. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  41080. this._split(index);
  41081. const chunk = this.byEnd[index];
  41082. if (chunk) {
  41083. chunk.prependLeft(content);
  41084. } else {
  41085. this.intro = content + this.intro;
  41086. }
  41087. return this;
  41088. }
  41089. prependRight(index, content) {
  41090. index = index + this.offset;
  41091. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  41092. this._split(index);
  41093. const chunk = this.byStart[index];
  41094. if (chunk) {
  41095. chunk.prependRight(content);
  41096. } else {
  41097. this.outro = content + this.outro;
  41098. }
  41099. return this;
  41100. }
  41101. remove(start, end) {
  41102. start = start + this.offset;
  41103. end = end + this.offset;
  41104. if (this.original.length !== 0) {
  41105. while (start < 0) start += this.original.length;
  41106. while (end < 0) end += this.original.length;
  41107. }
  41108. if (start === end) return this;
  41109. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  41110. if (start > end) throw new Error('end must be greater than start');
  41111. this._split(start);
  41112. this._split(end);
  41113. let chunk = this.byStart[start];
  41114. while (chunk) {
  41115. chunk.intro = '';
  41116. chunk.outro = '';
  41117. chunk.edit('');
  41118. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  41119. }
  41120. return this;
  41121. }
  41122. reset(start, end) {
  41123. start = start + this.offset;
  41124. end = end + this.offset;
  41125. if (this.original.length !== 0) {
  41126. while (start < 0) start += this.original.length;
  41127. while (end < 0) end += this.original.length;
  41128. }
  41129. if (start === end) return this;
  41130. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  41131. if (start > end) throw new Error('end must be greater than start');
  41132. this._split(start);
  41133. this._split(end);
  41134. let chunk = this.byStart[start];
  41135. while (chunk) {
  41136. chunk.reset();
  41137. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  41138. }
  41139. return this;
  41140. }
  41141. lastChar() {
  41142. if (this.outro.length) return this.outro[this.outro.length - 1];
  41143. let chunk = this.lastChunk;
  41144. do {
  41145. if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
  41146. if (chunk.content.length) return chunk.content[chunk.content.length - 1];
  41147. if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
  41148. } while ((chunk = chunk.previous));
  41149. if (this.intro.length) return this.intro[this.intro.length - 1];
  41150. return '';
  41151. }
  41152. lastLine() {
  41153. let lineIndex = this.outro.lastIndexOf(n);
  41154. if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
  41155. let lineStr = this.outro;
  41156. let chunk = this.lastChunk;
  41157. do {
  41158. if (chunk.outro.length > 0) {
  41159. lineIndex = chunk.outro.lastIndexOf(n);
  41160. if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
  41161. lineStr = chunk.outro + lineStr;
  41162. }
  41163. if (chunk.content.length > 0) {
  41164. lineIndex = chunk.content.lastIndexOf(n);
  41165. if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
  41166. lineStr = chunk.content + lineStr;
  41167. }
  41168. if (chunk.intro.length > 0) {
  41169. lineIndex = chunk.intro.lastIndexOf(n);
  41170. if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
  41171. lineStr = chunk.intro + lineStr;
  41172. }
  41173. } while ((chunk = chunk.previous));
  41174. lineIndex = this.intro.lastIndexOf(n);
  41175. if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
  41176. return this.intro + lineStr;
  41177. }
  41178. slice(start = 0, end = this.original.length - this.offset) {
  41179. start = start + this.offset;
  41180. end = end + this.offset;
  41181. if (this.original.length !== 0) {
  41182. while (start < 0) start += this.original.length;
  41183. while (end < 0) end += this.original.length;
  41184. }
  41185. let result = '';
  41186. // find start chunk
  41187. let chunk = this.firstChunk;
  41188. while (chunk && (chunk.start > start || chunk.end <= start)) {
  41189. // found end chunk before start
  41190. if (chunk.start < end && chunk.end >= end) {
  41191. return result;
  41192. }
  41193. chunk = chunk.next;
  41194. }
  41195. if (chunk && chunk.edited && chunk.start !== start)
  41196. throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
  41197. const startChunk = chunk;
  41198. while (chunk) {
  41199. if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
  41200. result += chunk.intro;
  41201. }
  41202. const containsEnd = chunk.start < end && chunk.end >= end;
  41203. if (containsEnd && chunk.edited && chunk.end !== end)
  41204. throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
  41205. const sliceStart = startChunk === chunk ? start - chunk.start : 0;
  41206. const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  41207. result += chunk.content.slice(sliceStart, sliceEnd);
  41208. if (chunk.outro && (!containsEnd || chunk.end === end)) {
  41209. result += chunk.outro;
  41210. }
  41211. if (containsEnd) {
  41212. break;
  41213. }
  41214. chunk = chunk.next;
  41215. }
  41216. return result;
  41217. }
  41218. // TODO deprecate this? not really very useful
  41219. snip(start, end) {
  41220. const clone = this.clone();
  41221. clone.remove(0, start);
  41222. clone.remove(end, clone.original.length);
  41223. return clone;
  41224. }
  41225. _split(index) {
  41226. if (this.byStart[index] || this.byEnd[index]) return;
  41227. let chunk = this.lastSearchedChunk;
  41228. const searchForward = index > chunk.end;
  41229. while (chunk) {
  41230. if (chunk.contains(index)) return this._splitChunk(chunk, index);
  41231. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  41232. }
  41233. }
  41234. _splitChunk(chunk, index) {
  41235. if (chunk.edited && chunk.content.length) {
  41236. // zero-length edited chunks are a special case (overlapping replacements)
  41237. const loc = getLocator(this.original)(index);
  41238. throw new Error(
  41239. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
  41240. );
  41241. }
  41242. const newChunk = chunk.split(index);
  41243. this.byEnd[index] = chunk;
  41244. this.byStart[index] = newChunk;
  41245. this.byEnd[newChunk.end] = newChunk;
  41246. if (chunk === this.lastChunk) this.lastChunk = newChunk;
  41247. this.lastSearchedChunk = chunk;
  41248. return true;
  41249. }
  41250. toString() {
  41251. let str = this.intro;
  41252. let chunk = this.firstChunk;
  41253. while (chunk) {
  41254. str += chunk.toString();
  41255. chunk = chunk.next;
  41256. }
  41257. return str + this.outro;
  41258. }
  41259. isEmpty() {
  41260. let chunk = this.firstChunk;
  41261. do {
  41262. if (
  41263. (chunk.intro.length && chunk.intro.trim()) ||
  41264. (chunk.content.length && chunk.content.trim()) ||
  41265. (chunk.outro.length && chunk.outro.trim())
  41266. )
  41267. return false;
  41268. } while ((chunk = chunk.next));
  41269. return true;
  41270. }
  41271. length() {
  41272. let chunk = this.firstChunk;
  41273. let length = 0;
  41274. do {
  41275. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  41276. } while ((chunk = chunk.next));
  41277. return length;
  41278. }
  41279. trimLines() {
  41280. return this.trim('[\\r\\n]');
  41281. }
  41282. trim(charType) {
  41283. return this.trimStart(charType).trimEnd(charType);
  41284. }
  41285. trimEndAborted(charType) {
  41286. const rx = new RegExp((charType || '\\s') + '+$');
  41287. this.outro = this.outro.replace(rx, '');
  41288. if (this.outro.length) return true;
  41289. let chunk = this.lastChunk;
  41290. do {
  41291. const end = chunk.end;
  41292. const aborted = chunk.trimEnd(rx);
  41293. // if chunk was trimmed, we have a new lastChunk
  41294. if (chunk.end !== end) {
  41295. if (this.lastChunk === chunk) {
  41296. this.lastChunk = chunk.next;
  41297. }
  41298. this.byEnd[chunk.end] = chunk;
  41299. this.byStart[chunk.next.start] = chunk.next;
  41300. this.byEnd[chunk.next.end] = chunk.next;
  41301. }
  41302. if (aborted) return true;
  41303. chunk = chunk.previous;
  41304. } while (chunk);
  41305. return false;
  41306. }
  41307. trimEnd(charType) {
  41308. this.trimEndAborted(charType);
  41309. return this;
  41310. }
  41311. trimStartAborted(charType) {
  41312. const rx = new RegExp('^' + (charType || '\\s') + '+');
  41313. this.intro = this.intro.replace(rx, '');
  41314. if (this.intro.length) return true;
  41315. let chunk = this.firstChunk;
  41316. do {
  41317. const end = chunk.end;
  41318. const aborted = chunk.trimStart(rx);
  41319. if (chunk.end !== end) {
  41320. // special case...
  41321. if (chunk === this.lastChunk) this.lastChunk = chunk.next;
  41322. this.byEnd[chunk.end] = chunk;
  41323. this.byStart[chunk.next.start] = chunk.next;
  41324. this.byEnd[chunk.next.end] = chunk.next;
  41325. }
  41326. if (aborted) return true;
  41327. chunk = chunk.next;
  41328. } while (chunk);
  41329. return false;
  41330. }
  41331. trimStart(charType) {
  41332. this.trimStartAborted(charType);
  41333. return this;
  41334. }
  41335. hasChanged() {
  41336. return this.original !== this.toString();
  41337. }
  41338. _replaceRegexp(searchValue, replacement) {
  41339. function getReplacement(match, str) {
  41340. if (typeof replacement === 'string') {
  41341. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  41342. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
  41343. if (i === '$') return '$';
  41344. if (i === '&') return match[0];
  41345. const num = +i;
  41346. if (num < match.length) return match[+i];
  41347. return `$${i}`;
  41348. });
  41349. } else {
  41350. return replacement(...match, match.index, str, match.groups);
  41351. }
  41352. }
  41353. function matchAll(re, str) {
  41354. let match;
  41355. const matches = [];
  41356. while ((match = re.exec(str))) {
  41357. matches.push(match);
  41358. }
  41359. return matches;
  41360. }
  41361. if (searchValue.global) {
  41362. const matches = matchAll(searchValue, this.original);
  41363. matches.forEach((match) => {
  41364. if (match.index != null) {
  41365. const replacement = getReplacement(match, this.original);
  41366. if (replacement !== match[0]) {
  41367. this.overwrite(match.index, match.index + match[0].length, replacement);
  41368. }
  41369. }
  41370. });
  41371. } else {
  41372. const match = this.original.match(searchValue);
  41373. if (match && match.index != null) {
  41374. const replacement = getReplacement(match, this.original);
  41375. if (replacement !== match[0]) {
  41376. this.overwrite(match.index, match.index + match[0].length, replacement);
  41377. }
  41378. }
  41379. }
  41380. return this;
  41381. }
  41382. _replaceString(string, replacement) {
  41383. const { original } = this;
  41384. const index = original.indexOf(string);
  41385. if (index !== -1) {
  41386. this.overwrite(index, index + string.length, replacement);
  41387. }
  41388. return this;
  41389. }
  41390. replace(searchValue, replacement) {
  41391. if (typeof searchValue === 'string') {
  41392. return this._replaceString(searchValue, replacement);
  41393. }
  41394. return this._replaceRegexp(searchValue, replacement);
  41395. }
  41396. _replaceAllString(string, replacement) {
  41397. const { original } = this;
  41398. const stringLength = string.length;
  41399. for (
  41400. let index = original.indexOf(string);
  41401. index !== -1;
  41402. index = original.indexOf(string, index + stringLength)
  41403. ) {
  41404. const previous = original.slice(index, index + stringLength);
  41405. if (previous !== replacement) this.overwrite(index, index + stringLength, replacement);
  41406. }
  41407. return this;
  41408. }
  41409. replaceAll(searchValue, replacement) {
  41410. if (typeof searchValue === 'string') {
  41411. return this._replaceAllString(searchValue, replacement);
  41412. }
  41413. if (!searchValue.global) {
  41414. throw new TypeError(
  41415. 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
  41416. );
  41417. }
  41418. return this._replaceRegexp(searchValue, replacement);
  41419. }
  41420. }
  41421. const REGISTRY$1 = new DomElementSchemaRegistry();
  41422. const REMOVE_XHTML_REGEX = /^:xhtml:/;
  41423. /**
  41424. * Checks non-Angular elements and properties against the `DomElementSchemaRegistry`, a schema
  41425. * maintained by the Angular team via extraction from a browser IDL.
  41426. */
  41427. class RegistryDomSchemaChecker {
  41428. resolver;
  41429. _diagnostics = [];
  41430. get diagnostics() {
  41431. return this._diagnostics;
  41432. }
  41433. constructor(resolver) {
  41434. this.resolver = resolver;
  41435. }
  41436. checkElement(id, element, schemas, hostIsStandalone) {
  41437. // HTML elements inside an SVG `foreignObject` are declared in the `xhtml` namespace.
  41438. // We need to strip it before handing it over to the registry because all HTML tag names
  41439. // in the registry are without a namespace.
  41440. const name = element.name.replace(REMOVE_XHTML_REGEX, '');
  41441. if (!REGISTRY$1.hasElement(name, schemas)) {
  41442. const mapping = this.resolver.getTemplateSourceMapping(id);
  41443. const schemas = `'${hostIsStandalone ? '@Component' : '@NgModule'}.schemas'`;
  41444. let errorMsg = `'${name}' is not a known element:\n`;
  41445. errorMsg += `1. If '${name}' is an Angular component, then verify that it is ${hostIsStandalone
  41446. ? "included in the '@Component.imports' of this component"
  41447. : 'part of this module'}.\n`;
  41448. if (name.indexOf('-') > -1) {
  41449. errorMsg += `2. If '${name}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the ${schemas} of this component to suppress this message.`;
  41450. }
  41451. else {
  41452. errorMsg += `2. To allow any element add 'NO_ERRORS_SCHEMA' to the ${schemas} of this component.`;
  41453. }
  41454. const diag = makeTemplateDiagnostic(id, mapping, element.startSourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.SCHEMA_INVALID_ELEMENT), errorMsg);
  41455. this._diagnostics.push(diag);
  41456. }
  41457. }
  41458. checkProperty(id, element, name, span, schemas, hostIsStandalone) {
  41459. if (!REGISTRY$1.hasProperty(element.name, name, schemas)) {
  41460. const mapping = this.resolver.getTemplateSourceMapping(id);
  41461. const decorator = hostIsStandalone ? '@Component' : '@NgModule';
  41462. const schemas = `'${decorator}.schemas'`;
  41463. let errorMsg = `Can't bind to '${name}' since it isn't a known property of '${element.name}'.`;
  41464. if (element.name.startsWith('ng-')) {
  41465. errorMsg +=
  41466. `\n1. If '${name}' is an Angular directive, then add 'CommonModule' to the '${decorator}.imports' of this component.` +
  41467. `\n2. To allow any property add 'NO_ERRORS_SCHEMA' to the ${schemas} of this component.`;
  41468. }
  41469. else if (element.name.indexOf('-') > -1) {
  41470. errorMsg +=
  41471. `\n1. If '${element.name}' is an Angular component and it has '${name}' input, then verify that it is ${hostIsStandalone
  41472. ? "included in the '@Component.imports' of this component"
  41473. : 'part of this module'}.` +
  41474. `\n2. If '${element.name}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the ${schemas} of this component to suppress this message.` +
  41475. `\n3. To allow any property add 'NO_ERRORS_SCHEMA' to the ${schemas} of this component.`;
  41476. }
  41477. const diag = makeTemplateDiagnostic(id, mapping, span, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.SCHEMA_INVALID_ATTRIBUTE), errorMsg);
  41478. this._diagnostics.push(diag);
  41479. }
  41480. }
  41481. }
  41482. /**
  41483. * An environment for a given source file that can be used to emit references.
  41484. *
  41485. * This can be used by the type-checking block, or constructor logic to generate
  41486. * references to directives or other symbols or types.
  41487. */
  41488. class ReferenceEmitEnvironment {
  41489. importManager;
  41490. refEmitter;
  41491. reflector;
  41492. contextFile;
  41493. constructor(importManager, refEmitter, reflector, contextFile) {
  41494. this.importManager = importManager;
  41495. this.refEmitter = refEmitter;
  41496. this.reflector = reflector;
  41497. this.contextFile = contextFile;
  41498. }
  41499. canReferenceType(ref, flags = exports.ImportFlags.NoAliasing |
  41500. exports.ImportFlags.AllowTypeImports |
  41501. exports.ImportFlags.AllowRelativeDtsImports) {
  41502. const result = this.refEmitter.emit(ref, this.contextFile, flags);
  41503. return result.kind === exports.ReferenceEmitKind.Success;
  41504. }
  41505. /**
  41506. * Generate a `ts.TypeNode` that references the given node as a type.
  41507. *
  41508. * This may involve importing the node into the file if it's not declared there already.
  41509. */
  41510. referenceType(ref, flags = exports.ImportFlags.NoAliasing |
  41511. exports.ImportFlags.AllowTypeImports |
  41512. exports.ImportFlags.AllowRelativeDtsImports) {
  41513. const ngExpr = this.refEmitter.emit(ref, this.contextFile, flags);
  41514. assertSuccessfulReferenceEmit(ngExpr, this.contextFile, 'symbol');
  41515. // Create an `ExpressionType` from the `Expression` and translate it via `translateType`.
  41516. // TODO(alxhub): support references to types with generic arguments in a clean way.
  41517. return translateType(new ExpressionType(ngExpr.expression), this.contextFile, this.reflector, this.refEmitter, this.importManager);
  41518. }
  41519. /**
  41520. * Generate a `ts.Expression` that refers to the external symbol. This
  41521. * may result in new imports being generated.
  41522. */
  41523. referenceExternalSymbol(moduleName, name) {
  41524. const external = new ExternalExpr({ moduleName, name });
  41525. return translateExpression(this.contextFile, external, this.importManager);
  41526. }
  41527. /**
  41528. * Generate a `ts.TypeNode` that references a given type from the provided module.
  41529. *
  41530. * This will involve importing the type into the file, and will also add type parameters if
  41531. * provided.
  41532. */
  41533. referenceExternalType(moduleName, name, typeParams) {
  41534. const external = new ExternalExpr({ moduleName, name });
  41535. return translateType(new ExpressionType(external, TypeModifier.None, typeParams), this.contextFile, this.reflector, this.refEmitter, this.importManager);
  41536. }
  41537. /**
  41538. * Generates a `ts.TypeNode` representing a type that is being referenced from a different place
  41539. * in the program. Any type references inside the transplanted type will be rewritten so that
  41540. * they can be imported in the context file.
  41541. */
  41542. referenceTransplantedType(type) {
  41543. return translateType(type, this.contextFile, this.reflector, this.refEmitter, this.importManager);
  41544. }
  41545. }
  41546. /**
  41547. * A `Set` of `ts.SyntaxKind`s of `ts.Expression` which are safe to wrap in a `ts.AsExpression`
  41548. * without needing to be wrapped in parentheses.
  41549. *
  41550. * For example, `foo.bar()` is a `ts.CallExpression`, and can be safely cast to `any` with
  41551. * `foo.bar() as any`. however, `foo !== bar` is a `ts.BinaryExpression`, and attempting to cast
  41552. * without the parentheses yields the expression `foo !== bar as any`. This is semantically
  41553. * equivalent to `foo !== (bar as any)`, which is not what was intended. Thus,
  41554. * `ts.BinaryExpression`s need to be wrapped in parentheses before casting.
  41555. */
  41556. //
  41557. const SAFE_TO_CAST_WITHOUT_PARENS = new Set([
  41558. // Expressions which are already parenthesized can be cast without further wrapping.
  41559. ts.SyntaxKind.ParenthesizedExpression,
  41560. // Expressions which form a single lexical unit leave no room for precedence issues with the cast.
  41561. ts.SyntaxKind.Identifier,
  41562. ts.SyntaxKind.CallExpression,
  41563. ts.SyntaxKind.NonNullExpression,
  41564. ts.SyntaxKind.ElementAccessExpression,
  41565. ts.SyntaxKind.PropertyAccessExpression,
  41566. ts.SyntaxKind.ArrayLiteralExpression,
  41567. ts.SyntaxKind.ObjectLiteralExpression,
  41568. // The same goes for various literals.
  41569. ts.SyntaxKind.StringLiteral,
  41570. ts.SyntaxKind.NumericLiteral,
  41571. ts.SyntaxKind.TrueKeyword,
  41572. ts.SyntaxKind.FalseKeyword,
  41573. ts.SyntaxKind.NullKeyword,
  41574. ts.SyntaxKind.UndefinedKeyword,
  41575. ]);
  41576. function tsCastToAny(expr) {
  41577. // Wrap `expr` in parentheses if needed (see `SAFE_TO_CAST_WITHOUT_PARENS` above).
  41578. if (!SAFE_TO_CAST_WITHOUT_PARENS.has(expr.kind)) {
  41579. expr = ts.factory.createParenthesizedExpression(expr);
  41580. }
  41581. // The outer expression is always wrapped in parentheses.
  41582. return ts.factory.createParenthesizedExpression(ts.factory.createAsExpression(expr, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)));
  41583. }
  41584. /**
  41585. * Create an expression which instantiates an element by its HTML tagName.
  41586. *
  41587. * Thanks to narrowing of `document.createElement()`, this expression will have its type inferred
  41588. * based on the tag name, including for custom elements that have appropriate .d.ts definitions.
  41589. */
  41590. function tsCreateElement(tagName) {
  41591. const createElement = ts.factory.createPropertyAccessExpression(
  41592. /* expression */ ts.factory.createIdentifier('document'), 'createElement');
  41593. return ts.factory.createCallExpression(
  41594. /* expression */ createElement,
  41595. /* typeArguments */ undefined,
  41596. /* argumentsArray */ [ts.factory.createStringLiteral(tagName)]);
  41597. }
  41598. /**
  41599. * Create a `ts.VariableStatement` which declares a variable without explicit initialization.
  41600. *
  41601. * The initializer `null!` is used to bypass strict variable initialization checks.
  41602. *
  41603. * Unlike with `tsCreateVariable`, the type of the variable is explicitly specified.
  41604. */
  41605. function tsDeclareVariable(id, type) {
  41606. // When we create a variable like `var _t1: boolean = null!`, TypeScript actually infers `_t1`
  41607. // to be `never`, instead of a `boolean`. To work around it, we cast the value
  41608. // in the initializer, e.g. `var _t1 = null! as boolean;`.
  41609. addExpressionIdentifier(type, ExpressionIdentifier.VARIABLE_AS_EXPRESSION);
  41610. const initializer = ts.factory.createAsExpression(ts.factory.createNonNullExpression(ts.factory.createNull()), type);
  41611. const decl = ts.factory.createVariableDeclaration(
  41612. /* name */ id,
  41613. /* exclamationToken */ undefined,
  41614. /* type */ undefined,
  41615. /* initializer */ initializer);
  41616. return ts.factory.createVariableStatement(
  41617. /* modifiers */ undefined,
  41618. /* declarationList */ [decl]);
  41619. }
  41620. /**
  41621. * Creates a `ts.TypeQueryNode` for a coerced input.
  41622. *
  41623. * For example: `typeof MatInput.ngAcceptInputType_value`, where MatInput is `typeName` and `value`
  41624. * is the `coercedInputName`.
  41625. *
  41626. * @param typeName The `EntityName` of the Directive where the static coerced input is defined.
  41627. * @param coercedInputName The field name of the coerced input.
  41628. */
  41629. function tsCreateTypeQueryForCoercedInput(typeName, coercedInputName) {
  41630. return ts.factory.createTypeQueryNode(ts.factory.createQualifiedName(typeName, `ngAcceptInputType_${coercedInputName}`));
  41631. }
  41632. /**
  41633. * Create a `ts.VariableStatement` that initializes a variable with a given expression.
  41634. *
  41635. * Unlike with `tsDeclareVariable`, the type of the variable is inferred from the initializer
  41636. * expression.
  41637. */
  41638. function tsCreateVariable(id, initializer, flags = null) {
  41639. const decl = ts.factory.createVariableDeclaration(
  41640. /* name */ id,
  41641. /* exclamationToken */ undefined,
  41642. /* type */ undefined,
  41643. /* initializer */ initializer);
  41644. return ts.factory.createVariableStatement(
  41645. /* modifiers */ undefined,
  41646. /* declarationList */ flags === null
  41647. ? [decl]
  41648. : ts.factory.createVariableDeclarationList([decl], flags));
  41649. }
  41650. /**
  41651. * Construct a `ts.CallExpression` that calls a method on a receiver.
  41652. */
  41653. function tsCallMethod(receiver, methodName, args = []) {
  41654. const methodAccess = ts.factory.createPropertyAccessExpression(receiver, methodName);
  41655. return ts.factory.createCallExpression(
  41656. /* expression */ methodAccess,
  41657. /* typeArguments */ undefined,
  41658. /* argumentsArray */ args);
  41659. }
  41660. function isAccessExpression(node) {
  41661. return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node);
  41662. }
  41663. /**
  41664. * Creates a TypeScript node representing a numeric value.
  41665. */
  41666. function tsNumericExpression(value) {
  41667. // As of TypeScript 5.3 negative numbers are represented as `prefixUnaryOperator` and passing a
  41668. // negative number (even as a string) into `createNumericLiteral` will result in an error.
  41669. if (value < 0) {
  41670. const operand = ts.factory.createNumericLiteral(Math.abs(value));
  41671. return ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, operand);
  41672. }
  41673. return ts.factory.createNumericLiteral(value);
  41674. }
  41675. /**
  41676. * See `TypeEmitter` for more information on the emitting process.
  41677. */
  41678. class TypeParameterEmitter {
  41679. typeParameters;
  41680. reflector;
  41681. constructor(typeParameters, reflector) {
  41682. this.typeParameters = typeParameters;
  41683. this.reflector = reflector;
  41684. }
  41685. /**
  41686. * Determines whether the type parameters can be emitted. If this returns true, then a call to
  41687. * `emit` is known to succeed. Vice versa, if false is returned then `emit` should not be
  41688. * called, as it would fail.
  41689. */
  41690. canEmit(canEmitReference) {
  41691. if (this.typeParameters === undefined) {
  41692. return true;
  41693. }
  41694. return this.typeParameters.every((typeParam) => {
  41695. return (this.canEmitType(typeParam.constraint, canEmitReference) &&
  41696. this.canEmitType(typeParam.default, canEmitReference));
  41697. });
  41698. }
  41699. canEmitType(type, canEmitReference) {
  41700. if (type === undefined) {
  41701. return true;
  41702. }
  41703. return canEmitType(type, (typeReference) => {
  41704. const reference = this.resolveTypeReference(typeReference);
  41705. if (reference === null) {
  41706. return false;
  41707. }
  41708. if (reference instanceof Reference) {
  41709. return canEmitReference(reference);
  41710. }
  41711. return true;
  41712. });
  41713. }
  41714. /**
  41715. * Emits the type parameters using the provided emitter function for `Reference`s.
  41716. */
  41717. emit(emitReference) {
  41718. if (this.typeParameters === undefined) {
  41719. return undefined;
  41720. }
  41721. const emitter = new TypeEmitter((type) => this.translateTypeReference(type, emitReference));
  41722. return this.typeParameters.map((typeParam) => {
  41723. const constraint = typeParam.constraint !== undefined ? emitter.emitType(typeParam.constraint) : undefined;
  41724. const defaultType = typeParam.default !== undefined ? emitter.emitType(typeParam.default) : undefined;
  41725. return ts.factory.updateTypeParameterDeclaration(typeParam, typeParam.modifiers, typeParam.name, constraint, defaultType);
  41726. });
  41727. }
  41728. resolveTypeReference(type) {
  41729. const target = ts.isIdentifier(type.typeName) ? type.typeName : type.typeName.right;
  41730. const declaration = this.reflector.getDeclarationOfIdentifier(target);
  41731. // If no declaration could be resolved or does not have a `ts.Declaration`, the type cannot be
  41732. // resolved.
  41733. if (declaration === null || declaration.node === null) {
  41734. return null;
  41735. }
  41736. // If the declaration corresponds with a local type parameter, the type reference can be used
  41737. // as is.
  41738. if (this.isLocalTypeParameter(declaration.node)) {
  41739. return type;
  41740. }
  41741. let owningModule = null;
  41742. if (typeof declaration.viaModule === 'string') {
  41743. owningModule = {
  41744. specifier: declaration.viaModule,
  41745. resolutionContext: type.getSourceFile().fileName,
  41746. };
  41747. }
  41748. return new Reference(declaration.node, declaration.viaModule === AmbientImport ? AmbientImport : owningModule);
  41749. }
  41750. translateTypeReference(type, emitReference) {
  41751. const reference = this.resolveTypeReference(type);
  41752. if (!(reference instanceof Reference)) {
  41753. return reference;
  41754. }
  41755. const typeNode = emitReference(reference);
  41756. if (typeNode === null) {
  41757. return null;
  41758. }
  41759. if (!ts.isTypeReferenceNode(typeNode)) {
  41760. throw new Error(`Expected TypeReferenceNode for emitted reference, got ${ts.SyntaxKind[typeNode.kind]}.`);
  41761. }
  41762. return typeNode;
  41763. }
  41764. isLocalTypeParameter(decl) {
  41765. // Checking for local type parameters only occurs during resolution of type parameters, so it is
  41766. // guaranteed that type parameters are present.
  41767. return this.typeParameters.some((param) => param === decl);
  41768. }
  41769. }
  41770. /**
  41771. * External modules/identifiers that always should exist for type check
  41772. * block files.
  41773. *
  41774. * Importing the modules in preparation helps ensuring a stable import graph
  41775. * that would not degrade TypeScript's incremental program structure re-use.
  41776. *
  41777. * Note: For inline type check blocks, or type constructors, we cannot add preparation
  41778. * imports, but ideally the required modules are already imported and can be re-used
  41779. * to not incur a structural TypeScript program re-use discarding.
  41780. */
  41781. const TCB_FILE_IMPORT_GRAPH_PREPARE_IDENTIFIERS = [
  41782. // Imports may be added for signal input checking. We wouldn't want to change the
  41783. // import graph for incremental compilations when suddenly a signal input is used,
  41784. // or removed.
  41785. Identifiers.InputSignalBrandWriteType,
  41786. ];
  41787. /**
  41788. * Indicates whether a particular component requires an inline type check block.
  41789. *
  41790. * This is not a boolean state as inlining might only be required to get the best possible
  41791. * type-checking, but the component could theoretically still be checked without it.
  41792. */
  41793. var TcbInliningRequirement;
  41794. (function (TcbInliningRequirement) {
  41795. /**
  41796. * There is no way to type check this component without inlining.
  41797. */
  41798. TcbInliningRequirement[TcbInliningRequirement["MustInline"] = 0] = "MustInline";
  41799. /**
  41800. * Inlining should be used due to the component's generic bounds, but a non-inlining fallback
  41801. * method can be used if that's not possible.
  41802. */
  41803. TcbInliningRequirement[TcbInliningRequirement["ShouldInlineForGenericBounds"] = 1] = "ShouldInlineForGenericBounds";
  41804. /**
  41805. * There is no requirement for this component's TCB to be inlined.
  41806. */
  41807. TcbInliningRequirement[TcbInliningRequirement["None"] = 2] = "None";
  41808. })(TcbInliningRequirement || (TcbInliningRequirement = {}));
  41809. function requiresInlineTypeCheckBlock(ref, env, usedPipes, reflector) {
  41810. // In order to qualify for a declared TCB (not inline) two conditions must be met:
  41811. // 1) the class must be suitable to be referenced from `env` (e.g. it must be exported)
  41812. // 2) it must not have contextual generic type bounds
  41813. if (!env.canReferenceType(ref)) {
  41814. // Condition 1 is false, the class is not exported.
  41815. return TcbInliningRequirement.MustInline;
  41816. }
  41817. else if (!checkIfGenericTypeBoundsCanBeEmitted(ref.node, reflector, env)) {
  41818. // Condition 2 is false, the class has constrained generic types. It should be checked with an
  41819. // inline TCB if possible, but can potentially use fallbacks to avoid inlining if not.
  41820. return TcbInliningRequirement.ShouldInlineForGenericBounds;
  41821. }
  41822. else if (usedPipes.some((pipeRef) => !env.canReferenceType(pipeRef))) {
  41823. // If one of the pipes used by the component is not exported, a non-inline TCB will not be able
  41824. // to import it, so this requires an inline TCB.
  41825. return TcbInliningRequirement.MustInline;
  41826. }
  41827. else {
  41828. return TcbInliningRequirement.None;
  41829. }
  41830. }
  41831. /** Maps a shim position back to a source code location. */
  41832. function getSourceMapping(shimSf, position, resolver, isDiagnosticRequest) {
  41833. const node = getTokenAtPosition(shimSf, position);
  41834. const sourceLocation = findSourceLocation(node, shimSf, isDiagnosticRequest);
  41835. if (sourceLocation === null) {
  41836. return null;
  41837. }
  41838. const mapping = resolver.getTemplateSourceMapping(sourceLocation.id);
  41839. const span = resolver.toTemplateParseSourceSpan(sourceLocation.id, sourceLocation.span);
  41840. if (span === null) {
  41841. return null;
  41842. }
  41843. // TODO(atscott): Consider adding a context span by walking up from `node` until we get a
  41844. // different span.
  41845. return { sourceLocation, sourceMapping: mapping, span };
  41846. }
  41847. function findTypeCheckBlock(file, id, isDiagnosticRequest) {
  41848. for (const stmt of file.statements) {
  41849. if (ts.isFunctionDeclaration(stmt) && getTemplateId(stmt, file, isDiagnosticRequest) === id) {
  41850. return stmt;
  41851. }
  41852. }
  41853. return null;
  41854. }
  41855. /**
  41856. * Traverses up the AST starting from the given node to extract the source location from comments
  41857. * that have been emitted into the TCB. If the node does not exist within a TCB, or if an ignore
  41858. * marker comment is found up the tree (and this is part of a diagnostic request), this function
  41859. * returns null.
  41860. */
  41861. function findSourceLocation(node, sourceFile, isDiagnosticsRequest) {
  41862. // Search for comments until the TCB's function declaration is encountered.
  41863. while (node !== undefined && !ts.isFunctionDeclaration(node)) {
  41864. if (hasIgnoreForDiagnosticsMarker(node, sourceFile) && isDiagnosticsRequest) {
  41865. // There's an ignore marker on this node, so the diagnostic should not be reported.
  41866. return null;
  41867. }
  41868. const span = readSpanComment(node, sourceFile);
  41869. if (span !== null) {
  41870. // Once the positional information has been extracted, search further up the TCB to extract
  41871. // the unique id that is attached with the TCB's function declaration.
  41872. const id = getTemplateId(node, sourceFile, isDiagnosticsRequest);
  41873. if (id === null) {
  41874. return null;
  41875. }
  41876. return { id, span };
  41877. }
  41878. node = node.parent;
  41879. }
  41880. return null;
  41881. }
  41882. function getTemplateId(node, sourceFile, isDiagnosticRequest) {
  41883. // Walk up to the function declaration of the TCB, the file information is attached there.
  41884. while (!ts.isFunctionDeclaration(node)) {
  41885. if (hasIgnoreForDiagnosticsMarker(node, sourceFile) && isDiagnosticRequest) {
  41886. // There's an ignore marker on this node, so the diagnostic should not be reported.
  41887. return null;
  41888. }
  41889. node = node.parent;
  41890. // Bail once we have reached the root.
  41891. if (node === undefined) {
  41892. return null;
  41893. }
  41894. }
  41895. const start = node.getFullStart();
  41896. return (ts.forEachLeadingCommentRange(sourceFile.text, start, (pos, end, kind) => {
  41897. if (kind !== ts.SyntaxKind.MultiLineCommentTrivia) {
  41898. return null;
  41899. }
  41900. const commentText = sourceFile.text.substring(pos + 2, end - 2);
  41901. return commentText;
  41902. }) || null);
  41903. }
  41904. /**
  41905. * Ensure imports for certain external modules that should always
  41906. * exist are generated. These are ensured to exist to avoid frequent
  41907. * import graph changes whenever e.g. a signal input is introduced in user code.
  41908. */
  41909. function ensureTypeCheckFilePreparationImports(env) {
  41910. for (const identifier of TCB_FILE_IMPORT_GRAPH_PREPARE_IDENTIFIERS) {
  41911. env.importManager.addImport({
  41912. exportModuleSpecifier: identifier.moduleName,
  41913. exportSymbolName: identifier.name,
  41914. requestedFile: env.contextFile,
  41915. });
  41916. }
  41917. }
  41918. function checkIfGenericTypeBoundsCanBeEmitted(node, reflector, env) {
  41919. // Generic type parameters are considered context free if they can be emitted into any context.
  41920. const emitter = new TypeParameterEmitter(node.typeParameters, reflector);
  41921. return emitter.canEmit((ref) => env.canReferenceType(ref));
  41922. }
  41923. function generateTypeCtorDeclarationFn(env, meta, nodeTypeRef, typeParams) {
  41924. const rawTypeArgs = typeParams !== undefined ? generateGenericArgs(typeParams) : undefined;
  41925. const rawType = ts.factory.createTypeReferenceNode(nodeTypeRef, rawTypeArgs);
  41926. const initParam = constructTypeCtorParameter(env, meta, rawType);
  41927. const typeParameters = typeParametersWithDefaultTypes(typeParams);
  41928. if (meta.body) {
  41929. const fnType = ts.factory.createFunctionTypeNode(
  41930. /* typeParameters */ typeParameters,
  41931. /* parameters */ [initParam],
  41932. /* type */ rawType);
  41933. const decl = ts.factory.createVariableDeclaration(
  41934. /* name */ meta.fnName,
  41935. /* exclamationToken */ undefined,
  41936. /* type */ fnType,
  41937. /* body */ ts.factory.createNonNullExpression(ts.factory.createNull()));
  41938. const declList = ts.factory.createVariableDeclarationList([decl], ts.NodeFlags.Const);
  41939. return ts.factory.createVariableStatement(
  41940. /* modifiers */ undefined,
  41941. /* declarationList */ declList);
  41942. }
  41943. else {
  41944. return ts.factory.createFunctionDeclaration(
  41945. /* modifiers */ [ts.factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
  41946. /* asteriskToken */ undefined,
  41947. /* name */ meta.fnName,
  41948. /* typeParameters */ typeParameters,
  41949. /* parameters */ [initParam],
  41950. /* type */ rawType,
  41951. /* body */ undefined);
  41952. }
  41953. }
  41954. /**
  41955. * Generate an inline type constructor for the given class and metadata.
  41956. *
  41957. * An inline type constructor is a specially shaped TypeScript static method, intended to be placed
  41958. * within a directive class itself, that permits type inference of any generic type parameters of
  41959. * the class from the types of expressions bound to inputs or outputs, and the types of elements
  41960. * that match queries performed by the directive. It also catches any errors in the types of these
  41961. * expressions. This method is never called at runtime, but is used in type-check blocks to
  41962. * construct directive types.
  41963. *
  41964. * An inline type constructor for NgFor looks like:
  41965. *
  41966. * static ngTypeCtor<T>(init: Pick<NgForOf<T>, 'ngForOf'|'ngForTrackBy'|'ngForTemplate'>):
  41967. * NgForOf<T>;
  41968. *
  41969. * A typical constructor would be:
  41970. *
  41971. * NgForOf.ngTypeCtor(init: {
  41972. * ngForOf: ['foo', 'bar'],
  41973. * ngForTrackBy: null as any,
  41974. * ngForTemplate: null as any,
  41975. * }); // Infers a type of NgForOf<string>.
  41976. *
  41977. * Any inputs declared on the type for which no property binding is present are assigned a value of
  41978. * type `any`, to avoid producing any type errors for unset inputs.
  41979. *
  41980. * Inline type constructors are used when the type being created has bounded generic types which
  41981. * make writing a declared type constructor (via `generateTypeCtorDeclarationFn`) difficult or
  41982. * impossible.
  41983. *
  41984. * @param node the `ClassDeclaration<ts.ClassDeclaration>` for which a type constructor will be
  41985. * generated.
  41986. * @param meta additional metadata required to generate the type constructor.
  41987. * @returns a `ts.MethodDeclaration` for the type constructor.
  41988. */
  41989. function generateInlineTypeCtor(env, node, meta) {
  41990. // Build rawType, a `ts.TypeNode` of the class with its generic parameters passed through from
  41991. // the definition without any type bounds. For example, if the class is
  41992. // `FooDirective<T extends Bar>`, its rawType would be `FooDirective<T>`.
  41993. const rawTypeArgs = node.typeParameters !== undefined ? generateGenericArgs(node.typeParameters) : undefined;
  41994. const rawType = ts.factory.createTypeReferenceNode(node.name, rawTypeArgs);
  41995. const initParam = constructTypeCtorParameter(env, meta, rawType);
  41996. // If this constructor is being generated into a .ts file, then it needs a fake body. The body
  41997. // is set to a return of `null!`. If the type constructor is being generated into a .d.ts file,
  41998. // it needs no body.
  41999. let body = undefined;
  42000. if (meta.body) {
  42001. body = ts.factory.createBlock([
  42002. ts.factory.createReturnStatement(ts.factory.createNonNullExpression(ts.factory.createNull())),
  42003. ]);
  42004. }
  42005. // Create the type constructor method declaration.
  42006. return ts.factory.createMethodDeclaration(
  42007. /* modifiers */ [ts.factory.createModifier(ts.SyntaxKind.StaticKeyword)],
  42008. /* asteriskToken */ undefined,
  42009. /* name */ meta.fnName,
  42010. /* questionToken */ undefined,
  42011. /* typeParameters */ typeParametersWithDefaultTypes(node.typeParameters),
  42012. /* parameters */ [initParam],
  42013. /* type */ rawType,
  42014. /* body */ body);
  42015. }
  42016. function constructTypeCtorParameter(env, meta, rawType) {
  42017. // initType is the type of 'init', the single argument to the type constructor method.
  42018. // If the Directive has any inputs, its initType will be:
  42019. //
  42020. // Pick<rawType, 'inputA'|'inputB'>
  42021. //
  42022. // Pick here is used to select only those fields from which the generic type parameters of the
  42023. // directive will be inferred.
  42024. //
  42025. // In the special case there are no inputs, initType is set to {}.
  42026. let initType = null;
  42027. const plainKeys = [];
  42028. const coercedKeys = [];
  42029. const signalInputKeys = [];
  42030. for (const { classPropertyName, transform, isSignal } of meta.fields.inputs) {
  42031. if (isSignal) {
  42032. signalInputKeys.push(ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(classPropertyName)));
  42033. }
  42034. else if (!meta.coercedInputFields.has(classPropertyName)) {
  42035. plainKeys.push(ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(classPropertyName)));
  42036. }
  42037. else {
  42038. const coercionType = transform != null
  42039. ? transform.type.node
  42040. : tsCreateTypeQueryForCoercedInput(rawType.typeName, classPropertyName);
  42041. coercedKeys.push(ts.factory.createPropertySignature(
  42042. /* modifiers */ undefined,
  42043. /* name */ classPropertyName,
  42044. /* questionToken */ undefined,
  42045. /* type */ coercionType));
  42046. }
  42047. }
  42048. if (plainKeys.length > 0) {
  42049. // Construct a union of all the field names.
  42050. const keyTypeUnion = ts.factory.createUnionTypeNode(plainKeys);
  42051. // Construct the Pick<rawType, keyTypeUnion>.
  42052. initType = ts.factory.createTypeReferenceNode('Pick', [rawType, keyTypeUnion]);
  42053. }
  42054. if (coercedKeys.length > 0) {
  42055. const coercedLiteral = ts.factory.createTypeLiteralNode(coercedKeys);
  42056. initType =
  42057. initType !== null
  42058. ? ts.factory.createIntersectionTypeNode([initType, coercedLiteral])
  42059. : coercedLiteral;
  42060. }
  42061. if (signalInputKeys.length > 0) {
  42062. const keyTypeUnion = ts.factory.createUnionTypeNode(signalInputKeys);
  42063. // Construct the UnwrapDirectiveSignalInputs<rawType, keyTypeUnion>.
  42064. const unwrapDirectiveSignalInputsExpr = env.referenceExternalType(Identifiers.UnwrapDirectiveSignalInputs.moduleName, Identifiers.UnwrapDirectiveSignalInputs.name, [
  42065. // TODO:
  42066. new ExpressionType(new WrappedNodeExpr(rawType)),
  42067. new ExpressionType(new WrappedNodeExpr(keyTypeUnion)),
  42068. ]);
  42069. initType =
  42070. initType !== null
  42071. ? ts.factory.createIntersectionTypeNode([initType, unwrapDirectiveSignalInputsExpr])
  42072. : unwrapDirectiveSignalInputsExpr;
  42073. }
  42074. if (initType === null) {
  42075. // Special case - no inputs, outputs, or other fields which could influence the result type.
  42076. initType = ts.factory.createTypeLiteralNode([]);
  42077. }
  42078. // Create the 'init' parameter itself.
  42079. return ts.factory.createParameterDeclaration(
  42080. /* modifiers */ undefined,
  42081. /* dotDotDotToken */ undefined,
  42082. /* name */ 'init',
  42083. /* questionToken */ undefined,
  42084. /* type */ initType,
  42085. /* initializer */ undefined);
  42086. }
  42087. function generateGenericArgs(params) {
  42088. return params.map((param) => ts.factory.createTypeReferenceNode(param.name, undefined));
  42089. }
  42090. function requiresInlineTypeCtor(node, host, env) {
  42091. // The class requires an inline type constructor if it has generic type bounds that can not be
  42092. // emitted into the provided type-check environment.
  42093. return !checkIfGenericTypeBoundsCanBeEmitted(node, host, env);
  42094. }
  42095. /**
  42096. * Add a default `= any` to type parameters that don't have a default value already.
  42097. *
  42098. * TypeScript uses the default type of a type parameter whenever inference of that parameter
  42099. * fails. This can happen when inferring a complex type from 'any'. For example, if `NgFor`'s
  42100. * inference is done with the TCB code:
  42101. *
  42102. * ```ts
  42103. * class NgFor<T> {
  42104. * ngForOf: T[];
  42105. * }
  42106. *
  42107. * declare function ctor<T>(o: Pick<NgFor<T>, 'ngForOf'|'ngForTrackBy'|'ngForTemplate'>):
  42108. * NgFor<T>;
  42109. * ```
  42110. *
  42111. * An invocation looks like:
  42112. *
  42113. * ```ts
  42114. * var _t1 = ctor({ngForOf: [1, 2], ngForTrackBy: null as any, ngForTemplate: null as any});
  42115. * ```
  42116. *
  42117. * This correctly infers the type `NgFor<number>` for `_t1`, since `T` is inferred from the
  42118. * assignment of type `number[]` to `ngForOf`'s type `T[]`. However, if `any` is passed instead:
  42119. *
  42120. * ```ts
  42121. * var _t2 = ctor({ngForOf: [1, 2] as any, ngForTrackBy: null as any, ngForTemplate: null as
  42122. * any});
  42123. * ```
  42124. *
  42125. * then inference for `T` fails (it cannot be inferred from `T[] = any`). In this case, `T`
  42126. * takes the type `{}`, and so `_t2` is inferred as `NgFor<{}>`. This is obviously wrong.
  42127. *
  42128. * Adding a default type to the generic declaration in the constructor solves this problem, as
  42129. * the default type will be used in the event that inference fails.
  42130. *
  42131. * ```ts
  42132. * declare function ctor<T = any>(o: Pick<NgFor<T>, 'ngForOf'>): NgFor<T>;
  42133. *
  42134. * var _t3 = ctor({ngForOf: [1, 2] as any});
  42135. * ```
  42136. *
  42137. * This correctly infers `T` as `any`, and therefore `_t3` as `NgFor<any>`.
  42138. */
  42139. function typeParametersWithDefaultTypes(params) {
  42140. if (params === undefined) {
  42141. return undefined;
  42142. }
  42143. return params.map((param) => {
  42144. if (param.default === undefined) {
  42145. return ts.factory.updateTypeParameterDeclaration(param, param.modifiers, param.name, param.constraint, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  42146. }
  42147. else {
  42148. return param;
  42149. }
  42150. });
  42151. }
  42152. /**
  42153. * A context which hosts one or more Type Check Blocks (TCBs).
  42154. *
  42155. * An `Environment` supports the generation of TCBs by tracking necessary imports, declarations of
  42156. * type constructors, and other statements beyond the type-checking code within the TCB itself.
  42157. * Through method calls on `Environment`, the TCB generator can request `ts.Expression`s which
  42158. * reference declarations in the `Environment` for these artifacts`.
  42159. *
  42160. * `Environment` can be used in a standalone fashion, or can be extended to support more specialized
  42161. * usage.
  42162. */
  42163. class Environment extends ReferenceEmitEnvironment {
  42164. config;
  42165. nextIds = {
  42166. pipeInst: 1,
  42167. typeCtor: 1,
  42168. };
  42169. typeCtors = new Map();
  42170. typeCtorStatements = [];
  42171. pipeInsts = new Map();
  42172. pipeInstStatements = [];
  42173. constructor(config, importManager, refEmitter, reflector, contextFile) {
  42174. super(importManager, refEmitter, reflector, contextFile);
  42175. this.config = config;
  42176. }
  42177. /**
  42178. * Get an expression referring to a type constructor for the given directive.
  42179. *
  42180. * Depending on the shape of the directive itself, this could be either a reference to a declared
  42181. * type constructor, or to an inline type constructor.
  42182. */
  42183. typeCtorFor(dir) {
  42184. const dirRef = dir.ref;
  42185. const node = dirRef.node;
  42186. if (this.typeCtors.has(node)) {
  42187. return this.typeCtors.get(node);
  42188. }
  42189. if (requiresInlineTypeCtor(node, this.reflector, this)) {
  42190. // The constructor has already been created inline, we just need to construct a reference to
  42191. // it.
  42192. const ref = this.reference(dirRef);
  42193. const typeCtorExpr = ts.factory.createPropertyAccessExpression(ref, 'ngTypeCtor');
  42194. this.typeCtors.set(node, typeCtorExpr);
  42195. return typeCtorExpr;
  42196. }
  42197. else {
  42198. const fnName = `_ctor${this.nextIds.typeCtor++}`;
  42199. const nodeTypeRef = this.referenceType(dirRef);
  42200. if (!ts.isTypeReferenceNode(nodeTypeRef)) {
  42201. throw new Error(`Expected TypeReferenceNode from reference to ${dirRef.debugName}`);
  42202. }
  42203. const meta = {
  42204. fnName,
  42205. body: true,
  42206. fields: {
  42207. inputs: dir.inputs,
  42208. // TODO: support queries
  42209. queries: dir.queries,
  42210. },
  42211. coercedInputFields: dir.coercedInputFields,
  42212. };
  42213. const typeParams = this.emitTypeParameters(node);
  42214. const typeCtor = generateTypeCtorDeclarationFn(this, meta, nodeTypeRef.typeName, typeParams);
  42215. this.typeCtorStatements.push(typeCtor);
  42216. const fnId = ts.factory.createIdentifier(fnName);
  42217. this.typeCtors.set(node, fnId);
  42218. return fnId;
  42219. }
  42220. }
  42221. /*
  42222. * Get an expression referring to an instance of the given pipe.
  42223. */
  42224. pipeInst(ref) {
  42225. if (this.pipeInsts.has(ref.node)) {
  42226. return this.pipeInsts.get(ref.node);
  42227. }
  42228. const pipeType = this.referenceType(ref);
  42229. const pipeInstId = ts.factory.createIdentifier(`_pipe${this.nextIds.pipeInst++}`);
  42230. this.pipeInstStatements.push(tsDeclareVariable(pipeInstId, pipeType));
  42231. this.pipeInsts.set(ref.node, pipeInstId);
  42232. return pipeInstId;
  42233. }
  42234. /**
  42235. * Generate a `ts.Expression` that references the given node.
  42236. *
  42237. * This may involve importing the node into the file if it's not declared there already.
  42238. */
  42239. reference(ref) {
  42240. // Disable aliasing for imports generated in a template type-checking context, as there is no
  42241. // guarantee that any alias re-exports exist in the .d.ts files. It's safe to use direct imports
  42242. // in these cases as there is no strict dependency checking during the template type-checking
  42243. // pass.
  42244. const ngExpr = this.refEmitter.emit(ref, this.contextFile, exports.ImportFlags.NoAliasing);
  42245. assertSuccessfulReferenceEmit(ngExpr, this.contextFile, 'class');
  42246. // Use `translateExpression` to convert the `Expression` into a `ts.Expression`.
  42247. return translateExpression(this.contextFile, ngExpr.expression, this.importManager);
  42248. }
  42249. emitTypeParameters(declaration) {
  42250. const emitter = new TypeParameterEmitter(declaration.typeParameters, this.reflector);
  42251. return emitter.emit((ref) => this.referenceType(ref));
  42252. }
  42253. getPreludeStatements() {
  42254. return [...this.pipeInstStatements, ...this.typeCtorStatements];
  42255. }
  42256. }
  42257. class OutOfBandDiagnosticRecorderImpl {
  42258. resolver;
  42259. _diagnostics = [];
  42260. /**
  42261. * Tracks which `BindingPipe` nodes have already been recorded as invalid, so only one diagnostic
  42262. * is ever produced per node.
  42263. */
  42264. recordedPipes = new Set();
  42265. constructor(resolver) {
  42266. this.resolver = resolver;
  42267. }
  42268. get diagnostics() {
  42269. return this._diagnostics;
  42270. }
  42271. missingReferenceTarget(id, ref) {
  42272. const mapping = this.resolver.getTemplateSourceMapping(id);
  42273. const value = ref.value.trim();
  42274. const errorMsg = `No directive found with exportAs '${value}'.`;
  42275. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, ref.valueSpan || ref.sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.MISSING_REFERENCE_TARGET), errorMsg));
  42276. }
  42277. missingPipe(id, ast) {
  42278. if (this.recordedPipes.has(ast)) {
  42279. return;
  42280. }
  42281. const mapping = this.resolver.getTemplateSourceMapping(id);
  42282. const errorMsg = `No pipe found with name '${ast.name}'.`;
  42283. const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, ast.nameSpan);
  42284. if (sourceSpan === null) {
  42285. throw new Error(`Assertion failure: no SourceLocation found for usage of pipe '${ast.name}'.`);
  42286. }
  42287. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.MISSING_PIPE), errorMsg));
  42288. this.recordedPipes.add(ast);
  42289. }
  42290. deferredPipeUsedEagerly(id, ast) {
  42291. if (this.recordedPipes.has(ast)) {
  42292. return;
  42293. }
  42294. const mapping = this.resolver.getTemplateSourceMapping(id);
  42295. const errorMsg = `Pipe '${ast.name}' was imported via \`@Component.deferredImports\`, ` +
  42296. `but was used outside of a \`@defer\` block in a template. To fix this, either ` +
  42297. `use the '${ast.name}' pipe inside of a \`@defer\` block or import this dependency ` +
  42298. `using the \`@Component.imports\` field.`;
  42299. const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, ast.nameSpan);
  42300. if (sourceSpan === null) {
  42301. throw new Error(`Assertion failure: no SourceLocation found for usage of pipe '${ast.name}'.`);
  42302. }
  42303. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.DEFERRED_PIPE_USED_EAGERLY), errorMsg));
  42304. this.recordedPipes.add(ast);
  42305. }
  42306. deferredComponentUsedEagerly(id, element) {
  42307. const mapping = this.resolver.getTemplateSourceMapping(id);
  42308. const errorMsg = `Element '${element.name}' contains a component or a directive that ` +
  42309. `was imported via \`@Component.deferredImports\`, but the element itself is located ` +
  42310. `outside of a \`@defer\` block in a template. To fix this, either ` +
  42311. `use the '${element.name}' element inside of a \`@defer\` block or ` +
  42312. `import referenced component/directive dependency using the \`@Component.imports\` field.`;
  42313. const { start, end } = element.startSourceSpan;
  42314. const absoluteSourceSpan = new AbsoluteSourceSpan(start.offset, end.offset);
  42315. const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, absoluteSourceSpan);
  42316. if (sourceSpan === null) {
  42317. throw new Error(`Assertion failure: no SourceLocation found for usage of pipe '${element.name}'.`);
  42318. }
  42319. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.DEFERRED_DIRECTIVE_USED_EAGERLY), errorMsg));
  42320. }
  42321. duplicateTemplateVar(id, variable, firstDecl) {
  42322. const mapping = this.resolver.getTemplateSourceMapping(id);
  42323. const errorMsg = `Cannot redeclare variable '${variable.name}' as it was previously declared elsewhere for the same template.`;
  42324. // The allocation of the error here is pretty useless for variables declared in microsyntax,
  42325. // since the sourceSpan refers to the entire microsyntax property, not a span for the specific
  42326. // variable in question.
  42327. //
  42328. // TODO(alxhub): allocate to a tighter span once one is available.
  42329. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, variable.sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.DUPLICATE_VARIABLE_DECLARATION), errorMsg, [
  42330. {
  42331. text: `The variable '${firstDecl.name}' was first declared here.`,
  42332. start: firstDecl.sourceSpan.start.offset,
  42333. end: firstDecl.sourceSpan.end.offset,
  42334. sourceFile: mapping.node.getSourceFile(),
  42335. },
  42336. ]));
  42337. }
  42338. requiresInlineTcb(id, node) {
  42339. this._diagnostics.push(makeInlineDiagnostic(id, exports.ErrorCode.INLINE_TCB_REQUIRED, node.name, `This component requires inline template type-checking, which is not supported by the current environment.`));
  42340. }
  42341. requiresInlineTypeConstructors(id, node, directives) {
  42342. let message;
  42343. if (directives.length > 1) {
  42344. message = `This component uses directives which require inline type constructors, which are not supported by the current environment.`;
  42345. }
  42346. else {
  42347. message = `This component uses a directive which requires an inline type constructor, which is not supported by the current environment.`;
  42348. }
  42349. this._diagnostics.push(makeInlineDiagnostic(id, exports.ErrorCode.INLINE_TYPE_CTOR_REQUIRED, node.name, message, directives.map((dir) => makeRelatedInformation(dir.name, `Requires an inline type constructor.`))));
  42350. }
  42351. suboptimalTypeInference(id, variables) {
  42352. const mapping = this.resolver.getTemplateSourceMapping(id);
  42353. // Select one of the template variables that's most suitable for reporting the diagnostic. Any
  42354. // variable will do, but prefer one bound to the context's $implicit if present.
  42355. let diagnosticVar = null;
  42356. for (const variable of variables) {
  42357. if (diagnosticVar === null || variable.value === '' || variable.value === '$implicit') {
  42358. diagnosticVar = variable;
  42359. }
  42360. }
  42361. if (diagnosticVar === null) {
  42362. // There is no variable on which to report the diagnostic.
  42363. return;
  42364. }
  42365. let varIdentification = `'${diagnosticVar.name}'`;
  42366. if (variables.length === 2) {
  42367. varIdentification += ` (and 1 other)`;
  42368. }
  42369. else if (variables.length > 2) {
  42370. varIdentification += ` (and ${variables.length - 1} others)`;
  42371. }
  42372. const message = `This structural directive supports advanced type inference, but the current compiler configuration prevents its usage. The variable ${varIdentification} will have type 'any' as a result.\n\nConsider enabling the 'strictTemplates' option in your tsconfig.json for better type inference within this template.`;
  42373. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, diagnosticVar.keySpan, ts.DiagnosticCategory.Suggestion, ngErrorCode(exports.ErrorCode.SUGGEST_SUBOPTIMAL_TYPE_INFERENCE), message));
  42374. }
  42375. splitTwoWayBinding(id, input, output, inputConsumer, outputConsumer) {
  42376. const mapping = this.resolver.getTemplateSourceMapping(id);
  42377. const errorMsg = `The property and event halves of the two-way binding '${input.name}' are not bound to the same target.
  42378. Find more at https://angular.dev/guide/templates/two-way-binding#how-two-way-binding-works`;
  42379. const relatedMessages = [];
  42380. relatedMessages.push({
  42381. text: `The property half of the binding is to the '${inputConsumer.name.text}' component.`,
  42382. start: inputConsumer.name.getStart(),
  42383. end: inputConsumer.name.getEnd(),
  42384. sourceFile: inputConsumer.name.getSourceFile(),
  42385. });
  42386. if (outputConsumer instanceof Element$1) {
  42387. let message = `The event half of the binding is to a native event called '${input.name}' on the <${outputConsumer.name}> DOM element.`;
  42388. if (!mapping.node.getSourceFile().isDeclarationFile) {
  42389. message += `\n \n Are you missing an output declaration called '${output.name}'?`;
  42390. }
  42391. relatedMessages.push({
  42392. text: message,
  42393. start: outputConsumer.sourceSpan.start.offset + 1,
  42394. end: outputConsumer.sourceSpan.start.offset + outputConsumer.name.length + 1,
  42395. sourceFile: mapping.node.getSourceFile(),
  42396. });
  42397. }
  42398. else {
  42399. relatedMessages.push({
  42400. text: `The event half of the binding is to the '${outputConsumer.name.text}' component.`,
  42401. start: outputConsumer.name.getStart(),
  42402. end: outputConsumer.name.getEnd(),
  42403. sourceFile: outputConsumer.name.getSourceFile(),
  42404. });
  42405. }
  42406. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, input.keySpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.SPLIT_TWO_WAY_BINDING), errorMsg, relatedMessages));
  42407. }
  42408. missingRequiredInputs(id, element, directiveName, isComponent, inputAliases) {
  42409. const message = `Required input${inputAliases.length === 1 ? '' : 's'} ${inputAliases
  42410. .map((n) => `'${n}'`)
  42411. .join(', ')} from ${isComponent ? 'component' : 'directive'} ${directiveName} must be specified.`;
  42412. this._diagnostics.push(makeTemplateDiagnostic(id, this.resolver.getTemplateSourceMapping(id), element.startSourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.MISSING_REQUIRED_INPUTS), message));
  42413. }
  42414. illegalForLoopTrackAccess(id, block, access) {
  42415. const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, access.sourceSpan);
  42416. if (sourceSpan === null) {
  42417. throw new Error(`Assertion failure: no SourceLocation found for property read.`);
  42418. }
  42419. const messageVars = [block.item, ...block.contextVariables.filter((v) => v.value === '$index')]
  42420. .map((v) => `'${v.name}'`)
  42421. .join(', ');
  42422. const message = `Cannot access '${access.name}' inside of a track expression. ` +
  42423. `Only ${messageVars} and properties on the containing component are available to this expression.`;
  42424. this._diagnostics.push(makeTemplateDiagnostic(id, this.resolver.getTemplateSourceMapping(id), sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.ILLEGAL_FOR_LOOP_TRACK_ACCESS), message));
  42425. }
  42426. inaccessibleDeferredTriggerElement(id, trigger) {
  42427. let message;
  42428. if (trigger.reference === null) {
  42429. message =
  42430. `Trigger cannot find reference. Make sure that the @defer block has a ` +
  42431. `@placeholder with at least one root element node.`;
  42432. }
  42433. else {
  42434. message =
  42435. `Trigger cannot find reference "${trigger.reference}".\nCheck that an element with #${trigger.reference} exists in the same template and it's accessible from the ` +
  42436. `@defer block.\nDeferred blocks can only access triggers in same view, a parent ` +
  42437. `embedded view or the root view of the @placeholder block.`;
  42438. }
  42439. this._diagnostics.push(makeTemplateDiagnostic(id, this.resolver.getTemplateSourceMapping(id), trigger.sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.INACCESSIBLE_DEFERRED_TRIGGER_ELEMENT), message));
  42440. }
  42441. controlFlowPreventingContentProjection(id, category, projectionNode, componentName, slotSelector, controlFlowNode, preservesWhitespaces) {
  42442. const blockName = controlFlowNode.nameSpan.toString().trim();
  42443. const lines = [
  42444. `Node matches the "${slotSelector}" slot of the "${componentName}" component, but will not be projected into the specific slot because the surrounding ${blockName} has more than one node at its root. To project the node in the right slot, you can:\n`,
  42445. `1. Wrap the content of the ${blockName} block in an <ng-container/> that matches the "${slotSelector}" selector.`,
  42446. `2. Split the content of the ${blockName} block across multiple ${blockName} blocks such that each one only has a single projectable node at its root.`,
  42447. `3. Remove all content from the ${blockName} block, except for the node being projected.`,
  42448. ];
  42449. if (preservesWhitespaces) {
  42450. lines.push('Note: the host component has `preserveWhitespaces: true` which may ' +
  42451. 'cause whitespace to affect content projection.');
  42452. }
  42453. lines.push('', 'This check can be disabled using the `extendedDiagnostics.checks.' +
  42454. 'controlFlowPreventingContentProjection = "suppress" compiler option.`');
  42455. this._diagnostics.push(makeTemplateDiagnostic(id, this.resolver.getTemplateSourceMapping(id), projectionNode.startSourceSpan, category, ngErrorCode(exports.ErrorCode.CONTROL_FLOW_PREVENTING_CONTENT_PROJECTION), lines.join('\n')));
  42456. }
  42457. illegalWriteToLetDeclaration(id, node, target) {
  42458. const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, node.sourceSpan);
  42459. if (sourceSpan === null) {
  42460. throw new Error(`Assertion failure: no SourceLocation found for property write.`);
  42461. }
  42462. this._diagnostics.push(makeTemplateDiagnostic(id, this.resolver.getTemplateSourceMapping(id), sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.ILLEGAL_LET_WRITE), `Cannot assign to @let declaration '${target.name}'.`));
  42463. }
  42464. letUsedBeforeDefinition(id, node, target) {
  42465. const sourceSpan = this.resolver.toTemplateParseSourceSpan(id, node.sourceSpan);
  42466. if (sourceSpan === null) {
  42467. throw new Error(`Assertion failure: no SourceLocation found for property read.`);
  42468. }
  42469. this._diagnostics.push(makeTemplateDiagnostic(id, this.resolver.getTemplateSourceMapping(id), sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.LET_USED_BEFORE_DEFINITION), `Cannot read @let declaration '${target.name}' before it has been defined.`));
  42470. }
  42471. conflictingDeclaration(id, decl) {
  42472. const mapping = this.resolver.getTemplateSourceMapping(id);
  42473. const errorMsg = `Cannot declare @let called '${decl.name}' as there is another symbol in the template with the same name.`;
  42474. this._diagnostics.push(makeTemplateDiagnostic(id, mapping, decl.sourceSpan, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.CONFLICTING_LET_DECLARATION), errorMsg));
  42475. }
  42476. }
  42477. function makeInlineDiagnostic(id, code, node, messageText, relatedInformation) {
  42478. return {
  42479. ...makeDiagnostic(code, node, messageText, relatedInformation),
  42480. sourceFile: node.getSourceFile(),
  42481. typeCheckId: id,
  42482. };
  42483. }
  42484. /**
  42485. * A `ShimGenerator` which adds type-checking files to the `ts.Program`.
  42486. *
  42487. * This is a requirement for performant template type-checking, as TypeScript will only reuse
  42488. * information in the main program when creating the type-checking program if the set of files in
  42489. * each are exactly the same. Thus, the main program also needs the synthetic type-checking files.
  42490. */
  42491. class TypeCheckShimGenerator {
  42492. extensionPrefix = 'ngtypecheck';
  42493. shouldEmit = false;
  42494. generateShimForFile(sf, genFilePath, priorShimSf) {
  42495. if (priorShimSf !== null) {
  42496. // If this shim existed in the previous program, reuse it now. It might not be correct, but
  42497. // reusing it in the main program allows the shape of its imports to potentially remain the
  42498. // same and TS can then use the fastest path for incremental program creation. Later during
  42499. // the type-checking phase it's going to either be reused, or replaced anyways. Thus there's
  42500. // no harm in reuse here even if it's out of date.
  42501. return priorShimSf;
  42502. }
  42503. return ts.createSourceFile(genFilePath, 'export const USED_FOR_NG_TYPE_CHECKING = true;', ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
  42504. }
  42505. static shimFor(fileName) {
  42506. return absoluteFrom(fileName.replace(/\.tsx?$/, '.ngtypecheck.ts'));
  42507. }
  42508. }
  42509. /**
  42510. * Wraps the node in parenthesis such that inserted span comments become attached to the proper
  42511. * node. This is an alias for `ts.factory.createParenthesizedExpression` with the benefit that it
  42512. * signifies that the inserted parenthesis are for diagnostic purposes, not for correctness of the
  42513. * rendered TCB code.
  42514. *
  42515. * Note that it is important that nodes and its attached comment are not wrapped into parenthesis
  42516. * by default, as it prevents correct translation of e.g. diagnostics produced for incorrect method
  42517. * arguments. Such diagnostics would then be produced for the parenthesised node whereas the
  42518. * positional comment would be located within that node, resulting in a mismatch.
  42519. */
  42520. function wrapForDiagnostics(expr) {
  42521. return ts.factory.createParenthesizedExpression(expr);
  42522. }
  42523. /**
  42524. * Wraps the node in parenthesis such that inserted span comments become attached to the proper
  42525. * node. This is an alias for `ts.factory.createParenthesizedExpression` with the benefit that it
  42526. * signifies that the inserted parenthesis are for use by the type checker, not for correctness of
  42527. * the rendered TCB code.
  42528. */
  42529. function wrapForTypeChecker(expr) {
  42530. return ts.factory.createParenthesizedExpression(expr);
  42531. }
  42532. /**
  42533. * Adds a synthetic comment to the expression that represents the parse span of the provided node.
  42534. * This comment can later be retrieved as trivia of a node to recover original source locations.
  42535. */
  42536. function addParseSpanInfo(node, span) {
  42537. let commentText;
  42538. if (span instanceof AbsoluteSourceSpan) {
  42539. commentText = `${span.start},${span.end}`;
  42540. }
  42541. else {
  42542. commentText = `${span.start.offset},${span.end.offset}`;
  42543. }
  42544. ts.addSyntheticTrailingComment(node, ts.SyntaxKind.MultiLineCommentTrivia, commentText,
  42545. /* hasTrailingNewLine */ false);
  42546. }
  42547. /**
  42548. * Adds a synthetic comment to the function declaration that contains the type checking ID
  42549. * of the class declaration.
  42550. */
  42551. function addTypeCheckId(tcb, id) {
  42552. ts.addSyntheticLeadingComment(tcb, ts.SyntaxKind.MultiLineCommentTrivia, id, true);
  42553. }
  42554. /**
  42555. * Determines if the diagnostic should be reported. Some diagnostics are produced because of the
  42556. * way TCBs are generated; those diagnostics should not be reported as type check errors of the
  42557. * template.
  42558. */
  42559. function shouldReportDiagnostic(diagnostic) {
  42560. const { code } = diagnostic;
  42561. if (code === 6133 /* $var is declared but its value is never read. */) {
  42562. return false;
  42563. }
  42564. else if (code === 6199 /* All variables are unused. */) {
  42565. return false;
  42566. }
  42567. else if (code === 2695 /* Left side of comma operator is unused and has no side effects. */) {
  42568. return false;
  42569. }
  42570. else if (code === 7006 /* Parameter '$event' implicitly has an 'any' type. */) {
  42571. return false;
  42572. }
  42573. return true;
  42574. }
  42575. /**
  42576. * Attempts to translate a TypeScript diagnostic produced during template type-checking to their
  42577. * location of origin, based on the comments that are emitted in the TCB code.
  42578. *
  42579. * If the diagnostic could not be translated, `null` is returned to indicate that the diagnostic
  42580. * should not be reported at all. This prevents diagnostics from non-TCB code in a user's source
  42581. * file from being reported as type-check errors.
  42582. */
  42583. function translateDiagnostic(diagnostic, resolver) {
  42584. if (diagnostic.file === undefined || diagnostic.start === undefined) {
  42585. return null;
  42586. }
  42587. const fullMapping = getSourceMapping(diagnostic.file, diagnostic.start, resolver,
  42588. /*isDiagnosticsRequest*/ true);
  42589. if (fullMapping === null) {
  42590. return null;
  42591. }
  42592. const { sourceLocation, sourceMapping: templateSourceMapping, span } = fullMapping;
  42593. return makeTemplateDiagnostic(sourceLocation.id, templateSourceMapping, span, diagnostic.category, diagnostic.code, diagnostic.messageText);
  42594. }
  42595. /**
  42596. * Expression that is cast to any. Currently represented as `0 as any`.
  42597. *
  42598. * Historically this expression was using `null as any`, but a newly-added check in TypeScript 5.6
  42599. * (https://devblogs.microsoft.com/typescript/announcing-typescript-5-6-beta/#disallowed-nullish-and-truthy-checks)
  42600. * started flagging it as always being nullish. Other options that were considered:
  42601. * - `NaN as any` or `Infinity as any` - not used, because they don't work if the `noLib` compiler
  42602. * option is enabled. Also they require more characters.
  42603. * - Some flavor of function call, like `isNan(0) as any` - requires even more characters than the
  42604. * NaN option and has the same issue with `noLib`.
  42605. */
  42606. const ANY_EXPRESSION = ts.factory.createAsExpression(ts.factory.createNumericLiteral('0'), ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  42607. const UNDEFINED = ts.factory.createIdentifier('undefined');
  42608. const UNARY_OPS = new Map([
  42609. ['+', ts.SyntaxKind.PlusToken],
  42610. ['-', ts.SyntaxKind.MinusToken],
  42611. ]);
  42612. const BINARY_OPS = new Map([
  42613. ['+', ts.SyntaxKind.PlusToken],
  42614. ['-', ts.SyntaxKind.MinusToken],
  42615. ['<', ts.SyntaxKind.LessThanToken],
  42616. ['>', ts.SyntaxKind.GreaterThanToken],
  42617. ['<=', ts.SyntaxKind.LessThanEqualsToken],
  42618. ['>=', ts.SyntaxKind.GreaterThanEqualsToken],
  42619. ['==', ts.SyntaxKind.EqualsEqualsToken],
  42620. ['===', ts.SyntaxKind.EqualsEqualsEqualsToken],
  42621. ['*', ts.SyntaxKind.AsteriskToken],
  42622. ['/', ts.SyntaxKind.SlashToken],
  42623. ['%', ts.SyntaxKind.PercentToken],
  42624. ['!=', ts.SyntaxKind.ExclamationEqualsToken],
  42625. ['!==', ts.SyntaxKind.ExclamationEqualsEqualsToken],
  42626. ['||', ts.SyntaxKind.BarBarToken],
  42627. ['&&', ts.SyntaxKind.AmpersandAmpersandToken],
  42628. ['&', ts.SyntaxKind.AmpersandToken],
  42629. ['|', ts.SyntaxKind.BarToken],
  42630. ['??', ts.SyntaxKind.QuestionQuestionToken],
  42631. ]);
  42632. /**
  42633. * Convert an `AST` to TypeScript code directly, without going through an intermediate `Expression`
  42634. * AST.
  42635. */
  42636. function astToTypescript(ast, maybeResolve, config) {
  42637. const translator = new AstTranslator(maybeResolve, config);
  42638. return translator.translate(ast);
  42639. }
  42640. class AstTranslator {
  42641. maybeResolve;
  42642. config;
  42643. constructor(maybeResolve, config) {
  42644. this.maybeResolve = maybeResolve;
  42645. this.config = config;
  42646. }
  42647. translate(ast) {
  42648. // Skip over an `ASTWithSource` as its `visit` method calls directly into its ast's `visit`,
  42649. // which would prevent any custom resolution through `maybeResolve` for that node.
  42650. if (ast instanceof ASTWithSource) {
  42651. ast = ast.ast;
  42652. }
  42653. // The `EmptyExpr` doesn't have a dedicated method on `AstVisitor`, so it's special cased here.
  42654. if (ast instanceof EmptyExpr$1) {
  42655. const res = ts.factory.createIdentifier('undefined');
  42656. addParseSpanInfo(res, ast.sourceSpan);
  42657. return res;
  42658. }
  42659. // First attempt to let any custom resolution logic provide a translation for the given node.
  42660. const resolved = this.maybeResolve(ast);
  42661. if (resolved !== null) {
  42662. return resolved;
  42663. }
  42664. return ast.visit(this);
  42665. }
  42666. visitUnary(ast) {
  42667. const expr = this.translate(ast.expr);
  42668. const op = UNARY_OPS.get(ast.operator);
  42669. if (op === undefined) {
  42670. throw new Error(`Unsupported Unary.operator: ${ast.operator}`);
  42671. }
  42672. const node = wrapForDiagnostics(ts.factory.createPrefixUnaryExpression(op, expr));
  42673. addParseSpanInfo(node, ast.sourceSpan);
  42674. return node;
  42675. }
  42676. visitBinary(ast) {
  42677. const lhs = wrapForDiagnostics(this.translate(ast.left));
  42678. const rhs = wrapForDiagnostics(this.translate(ast.right));
  42679. const op = BINARY_OPS.get(ast.operation);
  42680. if (op === undefined) {
  42681. throw new Error(`Unsupported Binary.operation: ${ast.operation}`);
  42682. }
  42683. const node = ts.factory.createBinaryExpression(lhs, op, rhs);
  42684. addParseSpanInfo(node, ast.sourceSpan);
  42685. return node;
  42686. }
  42687. visitChain(ast) {
  42688. const elements = ast.expressions.map((expr) => this.translate(expr));
  42689. const node = wrapForDiagnostics(ts.factory.createCommaListExpression(elements));
  42690. addParseSpanInfo(node, ast.sourceSpan);
  42691. return node;
  42692. }
  42693. visitConditional(ast) {
  42694. const condExpr = this.translate(ast.condition);
  42695. const trueExpr = this.translate(ast.trueExp);
  42696. // Wrap `falseExpr` in parens so that the trailing parse span info is not attributed to the
  42697. // whole conditional.
  42698. // In the following example, the last source span comment (5,6) could be seen as the
  42699. // trailing comment for _either_ the whole conditional expression _or_ just the `falseExpr` that
  42700. // is immediately before it:
  42701. // `conditional /*1,2*/ ? trueExpr /*3,4*/ : falseExpr /*5,6*/`
  42702. // This should be instead be `conditional /*1,2*/ ? trueExpr /*3,4*/ : (falseExpr /*5,6*/)`
  42703. const falseExpr = wrapForTypeChecker(this.translate(ast.falseExp));
  42704. const node = ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression(condExpr, undefined, trueExpr, undefined, falseExpr));
  42705. addParseSpanInfo(node, ast.sourceSpan);
  42706. return node;
  42707. }
  42708. visitImplicitReceiver(ast) {
  42709. throw new Error('Method not implemented.');
  42710. }
  42711. visitThisReceiver(ast) {
  42712. throw new Error('Method not implemented.');
  42713. }
  42714. visitInterpolation(ast) {
  42715. // Build up a chain of binary + operations to simulate the string concatenation of the
  42716. // interpolation's expressions. The chain is started using an actual string literal to ensure
  42717. // the type is inferred as 'string'.
  42718. return ast.expressions.reduce((lhs, ast) => ts.factory.createBinaryExpression(lhs, ts.SyntaxKind.PlusToken, wrapForTypeChecker(this.translate(ast))), ts.factory.createStringLiteral(''));
  42719. }
  42720. visitKeyedRead(ast) {
  42721. const receiver = wrapForDiagnostics(this.translate(ast.receiver));
  42722. const key = this.translate(ast.key);
  42723. const node = ts.factory.createElementAccessExpression(receiver, key);
  42724. addParseSpanInfo(node, ast.sourceSpan);
  42725. return node;
  42726. }
  42727. visitKeyedWrite(ast) {
  42728. const receiver = wrapForDiagnostics(this.translate(ast.receiver));
  42729. const left = ts.factory.createElementAccessExpression(receiver, this.translate(ast.key));
  42730. // TODO(joost): annotate `left` with the span of the element access, which is not currently
  42731. // available on `ast`.
  42732. const right = wrapForTypeChecker(this.translate(ast.value));
  42733. const node = wrapForDiagnostics(ts.factory.createBinaryExpression(left, ts.SyntaxKind.EqualsToken, right));
  42734. addParseSpanInfo(node, ast.sourceSpan);
  42735. return node;
  42736. }
  42737. visitLiteralArray(ast) {
  42738. const elements = ast.expressions.map((expr) => this.translate(expr));
  42739. const literal = ts.factory.createArrayLiteralExpression(elements);
  42740. // If strictLiteralTypes is disabled, array literals are cast to `any`.
  42741. const node = this.config.strictLiteralTypes ? literal : tsCastToAny(literal);
  42742. addParseSpanInfo(node, ast.sourceSpan);
  42743. return node;
  42744. }
  42745. visitLiteralMap(ast) {
  42746. const properties = ast.keys.map(({ key }, idx) => {
  42747. const value = this.translate(ast.values[idx]);
  42748. return ts.factory.createPropertyAssignment(ts.factory.createStringLiteral(key), value);
  42749. });
  42750. const literal = ts.factory.createObjectLiteralExpression(properties, true);
  42751. // If strictLiteralTypes is disabled, object literals are cast to `any`.
  42752. const node = this.config.strictLiteralTypes ? literal : tsCastToAny(literal);
  42753. addParseSpanInfo(node, ast.sourceSpan);
  42754. return node;
  42755. }
  42756. visitLiteralPrimitive(ast) {
  42757. let node;
  42758. if (ast.value === undefined) {
  42759. node = ts.factory.createIdentifier('undefined');
  42760. }
  42761. else if (ast.value === null) {
  42762. node = ts.factory.createNull();
  42763. }
  42764. else if (typeof ast.value === 'string') {
  42765. node = ts.factory.createStringLiteral(ast.value);
  42766. }
  42767. else if (typeof ast.value === 'number') {
  42768. node = tsNumericExpression(ast.value);
  42769. }
  42770. else if (typeof ast.value === 'boolean') {
  42771. node = ast.value ? ts.factory.createTrue() : ts.factory.createFalse();
  42772. }
  42773. else {
  42774. throw Error(`Unsupported AST value of type ${typeof ast.value}`);
  42775. }
  42776. addParseSpanInfo(node, ast.sourceSpan);
  42777. return node;
  42778. }
  42779. visitNonNullAssert(ast) {
  42780. const expr = wrapForDiagnostics(this.translate(ast.expression));
  42781. const node = ts.factory.createNonNullExpression(expr);
  42782. addParseSpanInfo(node, ast.sourceSpan);
  42783. return node;
  42784. }
  42785. visitPipe(ast) {
  42786. throw new Error('Method not implemented.');
  42787. }
  42788. visitPrefixNot(ast) {
  42789. const expression = wrapForDiagnostics(this.translate(ast.expression));
  42790. const node = ts.factory.createLogicalNot(expression);
  42791. addParseSpanInfo(node, ast.sourceSpan);
  42792. return node;
  42793. }
  42794. visitTypeofExpression(ast) {
  42795. const expression = wrapForDiagnostics(this.translate(ast.expression));
  42796. const node = ts.factory.createTypeOfExpression(expression);
  42797. addParseSpanInfo(node, ast.sourceSpan);
  42798. return node;
  42799. }
  42800. visitPropertyRead(ast) {
  42801. // This is a normal property read - convert the receiver to an expression and emit the correct
  42802. // TypeScript expression to read the property.
  42803. const receiver = wrapForDiagnostics(this.translate(ast.receiver));
  42804. const name = ts.factory.createPropertyAccessExpression(receiver, ast.name);
  42805. addParseSpanInfo(name, ast.nameSpan);
  42806. const node = wrapForDiagnostics(name);
  42807. addParseSpanInfo(node, ast.sourceSpan);
  42808. return node;
  42809. }
  42810. visitPropertyWrite(ast) {
  42811. const receiver = wrapForDiagnostics(this.translate(ast.receiver));
  42812. const left = ts.factory.createPropertyAccessExpression(receiver, ast.name);
  42813. addParseSpanInfo(left, ast.nameSpan);
  42814. // TypeScript reports assignment errors on the entire lvalue expression. Annotate the lvalue of
  42815. // the assignment with the sourceSpan, which includes receivers, rather than nameSpan for
  42816. // consistency of the diagnostic location.
  42817. // a.b.c = 1
  42818. // ^^^^^^^^^ sourceSpan
  42819. // ^ nameSpan
  42820. const leftWithPath = wrapForDiagnostics(left);
  42821. addParseSpanInfo(leftWithPath, ast.sourceSpan);
  42822. // The right needs to be wrapped in parens as well or we cannot accurately match its
  42823. // span to just the RHS. For example, the span in `e = $event /*0,10*/` is ambiguous.
  42824. // It could refer to either the whole binary expression or just the RHS.
  42825. // We should instead generate `e = ($event /*0,10*/)` so we know the span 0,10 matches RHS.
  42826. const right = wrapForTypeChecker(this.translate(ast.value));
  42827. const node = wrapForDiagnostics(ts.factory.createBinaryExpression(leftWithPath, ts.SyntaxKind.EqualsToken, right));
  42828. addParseSpanInfo(node, ast.sourceSpan);
  42829. return node;
  42830. }
  42831. visitSafePropertyRead(ast) {
  42832. let node;
  42833. const receiver = wrapForDiagnostics(this.translate(ast.receiver));
  42834. // The form of safe property reads depends on whether strictness is in use.
  42835. if (this.config.strictSafeNavigationTypes) {
  42836. // Basically, the return here is either the type of the complete expression with a null-safe
  42837. // property read, or `undefined`. So a ternary is used to create an "or" type:
  42838. // "a?.b" becomes (0 as any ? a!.b : undefined)
  42839. // The type of this expression is (typeof a!.b) | undefined, which is exactly as desired.
  42840. const expr = ts.factory.createPropertyAccessExpression(ts.factory.createNonNullExpression(receiver), ast.name);
  42841. addParseSpanInfo(expr, ast.nameSpan);
  42842. node = ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression(ANY_EXPRESSION, undefined, expr, undefined, UNDEFINED));
  42843. }
  42844. else if (VeSafeLhsInferenceBugDetector.veWillInferAnyFor(ast)) {
  42845. // Emulate a View Engine bug where 'any' is inferred for the left-hand side of the safe
  42846. // navigation operation. With this bug, the type of the left-hand side is regarded as any.
  42847. // Therefore, the left-hand side only needs repeating in the output (to validate it), and then
  42848. // 'any' is used for the rest of the expression. This is done using a comma operator:
  42849. // "a?.b" becomes (a as any).b, which will of course have type 'any'.
  42850. node = ts.factory.createPropertyAccessExpression(tsCastToAny(receiver), ast.name);
  42851. }
  42852. else {
  42853. // The View Engine bug isn't active, so check the entire type of the expression, but the final
  42854. // result is still inferred as `any`.
  42855. // "a?.b" becomes (a!.b as any)
  42856. const expr = ts.factory.createPropertyAccessExpression(ts.factory.createNonNullExpression(receiver), ast.name);
  42857. addParseSpanInfo(expr, ast.nameSpan);
  42858. node = tsCastToAny(expr);
  42859. }
  42860. addParseSpanInfo(node, ast.sourceSpan);
  42861. return node;
  42862. }
  42863. visitSafeKeyedRead(ast) {
  42864. const receiver = wrapForDiagnostics(this.translate(ast.receiver));
  42865. const key = this.translate(ast.key);
  42866. let node;
  42867. // The form of safe property reads depends on whether strictness is in use.
  42868. if (this.config.strictSafeNavigationTypes) {
  42869. // "a?.[...]" becomes (0 as any ? a![...] : undefined)
  42870. const expr = ts.factory.createElementAccessExpression(ts.factory.createNonNullExpression(receiver), key);
  42871. addParseSpanInfo(expr, ast.sourceSpan);
  42872. node = ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression(ANY_EXPRESSION, undefined, expr, undefined, UNDEFINED));
  42873. }
  42874. else if (VeSafeLhsInferenceBugDetector.veWillInferAnyFor(ast)) {
  42875. // "a?.[...]" becomes (a as any)[...]
  42876. node = ts.factory.createElementAccessExpression(tsCastToAny(receiver), key);
  42877. }
  42878. else {
  42879. // "a?.[...]" becomes (a!.[...] as any)
  42880. const expr = ts.factory.createElementAccessExpression(ts.factory.createNonNullExpression(receiver), key);
  42881. addParseSpanInfo(expr, ast.sourceSpan);
  42882. node = tsCastToAny(expr);
  42883. }
  42884. addParseSpanInfo(node, ast.sourceSpan);
  42885. return node;
  42886. }
  42887. visitCall(ast) {
  42888. const args = ast.args.map((expr) => this.translate(expr));
  42889. let expr;
  42890. const receiver = ast.receiver;
  42891. // For calls that have a property read as receiver, we have to special-case their emit to avoid
  42892. // inserting superfluous parenthesis as they prevent TypeScript from applying a narrowing effect
  42893. // if the method acts as a type guard.
  42894. if (receiver instanceof PropertyRead) {
  42895. const resolved = this.maybeResolve(receiver);
  42896. if (resolved !== null) {
  42897. expr = resolved;
  42898. }
  42899. else {
  42900. const propertyReceiver = wrapForDiagnostics(this.translate(receiver.receiver));
  42901. expr = ts.factory.createPropertyAccessExpression(propertyReceiver, receiver.name);
  42902. addParseSpanInfo(expr, receiver.nameSpan);
  42903. }
  42904. }
  42905. else {
  42906. expr = this.translate(receiver);
  42907. }
  42908. let node;
  42909. // Safe property/keyed reads will produce a ternary whose value is nullable.
  42910. // We have to generate a similar ternary around the call.
  42911. if (ast.receiver instanceof SafePropertyRead || ast.receiver instanceof SafeKeyedRead) {
  42912. node = this.convertToSafeCall(ast, expr, args);
  42913. }
  42914. else {
  42915. node = ts.factory.createCallExpression(expr, undefined, args);
  42916. }
  42917. addParseSpanInfo(node, ast.sourceSpan);
  42918. return node;
  42919. }
  42920. visitSafeCall(ast) {
  42921. const args = ast.args.map((expr) => this.translate(expr));
  42922. const expr = wrapForDiagnostics(this.translate(ast.receiver));
  42923. const node = this.convertToSafeCall(ast, expr, args);
  42924. addParseSpanInfo(node, ast.sourceSpan);
  42925. return node;
  42926. }
  42927. visitTemplateLiteral(ast) {
  42928. const length = ast.elements.length;
  42929. const head = ast.elements[0];
  42930. let result;
  42931. if (length === 1) {
  42932. result = ts.factory.createNoSubstitutionTemplateLiteral(head.text);
  42933. }
  42934. else {
  42935. const spans = [];
  42936. const tailIndex = length - 1;
  42937. for (let i = 1; i < tailIndex; i++) {
  42938. const middle = ts.factory.createTemplateMiddle(ast.elements[i].text);
  42939. spans.push(ts.factory.createTemplateSpan(this.translate(ast.expressions[i - 1]), middle));
  42940. }
  42941. const resolvedExpression = this.translate(ast.expressions[tailIndex - 1]);
  42942. const templateTail = ts.factory.createTemplateTail(ast.elements[tailIndex].text);
  42943. spans.push(ts.factory.createTemplateSpan(resolvedExpression, templateTail));
  42944. result = ts.factory.createTemplateExpression(ts.factory.createTemplateHead(head.text), spans);
  42945. }
  42946. return result;
  42947. }
  42948. visitTemplateLiteralElement(ast, context) {
  42949. throw new Error('Method not implemented');
  42950. }
  42951. convertToSafeCall(ast, expr, args) {
  42952. if (this.config.strictSafeNavigationTypes) {
  42953. // "a?.method(...)" becomes (0 as any ? a!.method(...) : undefined)
  42954. const call = ts.factory.createCallExpression(ts.factory.createNonNullExpression(expr), undefined, args);
  42955. return ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression(ANY_EXPRESSION, undefined, call, undefined, UNDEFINED));
  42956. }
  42957. if (VeSafeLhsInferenceBugDetector.veWillInferAnyFor(ast)) {
  42958. // "a?.method(...)" becomes (a as any).method(...)
  42959. return ts.factory.createCallExpression(tsCastToAny(expr), undefined, args);
  42960. }
  42961. // "a?.method(...)" becomes (a!.method(...) as any)
  42962. return tsCastToAny(ts.factory.createCallExpression(ts.factory.createNonNullExpression(expr), undefined, args));
  42963. }
  42964. }
  42965. /**
  42966. * Checks whether View Engine will infer a type of 'any' for the left-hand side of a safe navigation
  42967. * operation.
  42968. *
  42969. * In View Engine's template type-checker, certain receivers of safe navigation operations will
  42970. * cause a temporary variable to be allocated as part of the checking expression, to save the value
  42971. * of the receiver and use it more than once in the expression. This temporary variable has type
  42972. * 'any'. In practice, this means certain receivers cause View Engine to not check the full
  42973. * expression, and other receivers will receive more complete checking.
  42974. *
  42975. * For compatibility, this logic is adapted from View Engine's expression_converter.ts so that the
  42976. * Ivy checker can emulate this bug when needed.
  42977. */
  42978. class VeSafeLhsInferenceBugDetector {
  42979. static SINGLETON = new VeSafeLhsInferenceBugDetector();
  42980. static veWillInferAnyFor(ast) {
  42981. const visitor = VeSafeLhsInferenceBugDetector.SINGLETON;
  42982. return ast instanceof Call ? ast.visit(visitor) : ast.receiver.visit(visitor);
  42983. }
  42984. visitUnary(ast) {
  42985. return ast.expr.visit(this);
  42986. }
  42987. visitBinary(ast) {
  42988. return ast.left.visit(this) || ast.right.visit(this);
  42989. }
  42990. visitChain(ast) {
  42991. return false;
  42992. }
  42993. visitConditional(ast) {
  42994. return ast.condition.visit(this) || ast.trueExp.visit(this) || ast.falseExp.visit(this);
  42995. }
  42996. visitCall(ast) {
  42997. return true;
  42998. }
  42999. visitSafeCall(ast) {
  43000. return false;
  43001. }
  43002. visitImplicitReceiver(ast) {
  43003. return false;
  43004. }
  43005. visitThisReceiver(ast) {
  43006. return false;
  43007. }
  43008. visitInterpolation(ast) {
  43009. return ast.expressions.some((exp) => exp.visit(this));
  43010. }
  43011. visitKeyedRead(ast) {
  43012. return false;
  43013. }
  43014. visitKeyedWrite(ast) {
  43015. return false;
  43016. }
  43017. visitLiteralArray(ast) {
  43018. return true;
  43019. }
  43020. visitLiteralMap(ast) {
  43021. return true;
  43022. }
  43023. visitLiteralPrimitive(ast) {
  43024. return false;
  43025. }
  43026. visitPipe(ast) {
  43027. return true;
  43028. }
  43029. visitPrefixNot(ast) {
  43030. return ast.expression.visit(this);
  43031. }
  43032. visitTypeofExpression(ast) {
  43033. return ast.expression.visit(this);
  43034. }
  43035. visitNonNullAssert(ast) {
  43036. return ast.expression.visit(this);
  43037. }
  43038. visitPropertyRead(ast) {
  43039. return false;
  43040. }
  43041. visitPropertyWrite(ast) {
  43042. return false;
  43043. }
  43044. visitSafePropertyRead(ast) {
  43045. return false;
  43046. }
  43047. visitSafeKeyedRead(ast) {
  43048. return false;
  43049. }
  43050. visitTemplateLiteral(ast, context) {
  43051. return false;
  43052. }
  43053. visitTemplateLiteralElement(ast, context) {
  43054. return false;
  43055. }
  43056. }
  43057. /**
  43058. * Controls how generics for the component context class will be handled during TCB generation.
  43059. */
  43060. var TcbGenericContextBehavior;
  43061. (function (TcbGenericContextBehavior) {
  43062. /**
  43063. * References to generic parameter bounds will be emitted via the `TypeParameterEmitter`.
  43064. *
  43065. * The caller must verify that all parameter bounds are emittable in order to use this mode.
  43066. */
  43067. TcbGenericContextBehavior[TcbGenericContextBehavior["UseEmitter"] = 0] = "UseEmitter";
  43068. /**
  43069. * Generic parameter declarations will be copied directly from the `ts.ClassDeclaration` of the
  43070. * component class.
  43071. *
  43072. * The caller must only use the generated TCB code in a context where such copies will still be
  43073. * valid, such as an inline type check block.
  43074. */
  43075. TcbGenericContextBehavior[TcbGenericContextBehavior["CopyClassNodes"] = 1] = "CopyClassNodes";
  43076. /**
  43077. * Any generic parameters for the component context class will be set to `any`.
  43078. *
  43079. * Produces a less useful type, but is always safe to use.
  43080. */
  43081. TcbGenericContextBehavior[TcbGenericContextBehavior["FallbackToAny"] = 2] = "FallbackToAny";
  43082. })(TcbGenericContextBehavior || (TcbGenericContextBehavior = {}));
  43083. /**
  43084. * Given a `ts.ClassDeclaration` for a component, and metadata regarding that component, compose a
  43085. * "type check block" function.
  43086. *
  43087. * When passed through TypeScript's TypeChecker, type errors that arise within the type check block
  43088. * function indicate issues in the template itself.
  43089. *
  43090. * As a side effect of generating a TCB for the component, `ts.Diagnostic`s may also be produced
  43091. * directly for issues within the template which are identified during generation. These issues are
  43092. * recorded in either the `domSchemaChecker` (which checks usage of DOM elements and bindings) as
  43093. * well as the `oobRecorder` (which records errors when the type-checking code generator is unable
  43094. * to sufficiently understand a template).
  43095. *
  43096. * @param env an `Environment` into which type-checking code will be generated.
  43097. * @param ref a `Reference` to the component class which should be type-checked.
  43098. * @param name a `ts.Identifier` to use for the generated `ts.FunctionDeclaration`.
  43099. * @param meta metadata about the component's template and the function being generated.
  43100. * @param domSchemaChecker used to check and record errors regarding improper usage of DOM elements
  43101. * and bindings.
  43102. * @param oobRecorder used to record errors regarding template elements which could not be correctly
  43103. * translated into types during TCB generation.
  43104. * @param genericContextBehavior controls how generic parameters (especially parameters with generic
  43105. * bounds) will be referenced from the generated TCB code.
  43106. */
  43107. function generateTypeCheckBlock(env, ref, name, meta, domSchemaChecker, oobRecorder, genericContextBehavior) {
  43108. const tcb = new Context(env, domSchemaChecker, oobRecorder, meta.id, meta.boundTarget, meta.pipes, meta.schemas, meta.isStandalone, meta.preserveWhitespaces);
  43109. const scope = Scope.forNodes(tcb, null, null, tcb.boundTarget.target.template, /* guard */ null);
  43110. const ctxRawType = env.referenceType(ref);
  43111. if (!ts.isTypeReferenceNode(ctxRawType)) {
  43112. throw new Error(`Expected TypeReferenceNode when referencing the ctx param for ${ref.debugName}`);
  43113. }
  43114. let typeParameters = undefined;
  43115. let typeArguments = undefined;
  43116. if (ref.node.typeParameters !== undefined) {
  43117. if (!env.config.useContextGenericType) {
  43118. genericContextBehavior = TcbGenericContextBehavior.FallbackToAny;
  43119. }
  43120. switch (genericContextBehavior) {
  43121. case TcbGenericContextBehavior.UseEmitter:
  43122. // Guaranteed to emit type parameters since we checked that the class has them above.
  43123. typeParameters = new TypeParameterEmitter(ref.node.typeParameters, env.reflector).emit((typeRef) => env.referenceType(typeRef));
  43124. typeArguments = typeParameters.map((param) => ts.factory.createTypeReferenceNode(param.name));
  43125. break;
  43126. case TcbGenericContextBehavior.CopyClassNodes:
  43127. typeParameters = [...ref.node.typeParameters];
  43128. typeArguments = typeParameters.map((param) => ts.factory.createTypeReferenceNode(param.name));
  43129. break;
  43130. case TcbGenericContextBehavior.FallbackToAny:
  43131. typeArguments = ref.node.typeParameters.map(() => ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  43132. break;
  43133. }
  43134. }
  43135. const paramList = [tcbThisParam(ctxRawType.typeName, typeArguments)];
  43136. const scopeStatements = scope.render();
  43137. const innerBody = ts.factory.createBlock([...env.getPreludeStatements(), ...scopeStatements]);
  43138. // Wrap the body in an "if (true)" expression. This is unnecessary but has the effect of causing
  43139. // the `ts.Printer` to format the type-check block nicely.
  43140. const body = ts.factory.createBlock([
  43141. ts.factory.createIfStatement(ts.factory.createTrue(), innerBody, undefined),
  43142. ]);
  43143. const fnDecl = ts.factory.createFunctionDeclaration(
  43144. /* modifiers */ undefined,
  43145. /* asteriskToken */ undefined,
  43146. /* name */ name,
  43147. /* typeParameters */ env.config.useContextGenericType ? typeParameters : undefined,
  43148. /* parameters */ paramList,
  43149. /* type */ undefined,
  43150. /* body */ body);
  43151. addTypeCheckId(fnDecl, meta.id);
  43152. return fnDecl;
  43153. }
  43154. /**
  43155. * A code generation operation that's involved in the construction of a Type Check Block.
  43156. *
  43157. * The generation of a TCB is non-linear. Bindings within a template may result in the need to
  43158. * construct certain types earlier than they otherwise would be constructed. That is, if the
  43159. * generation of a TCB for a template is broken down into specific operations (constructing a
  43160. * directive, extracting a variable from a let- operation, etc), then it's possible for operations
  43161. * earlier in the sequence to depend on operations which occur later in the sequence.
  43162. *
  43163. * `TcbOp` abstracts the different types of operations which are required to convert a template into
  43164. * a TCB. This allows for two phases of processing for the template, where 1) a linear sequence of
  43165. * `TcbOp`s is generated, and then 2) these operations are executed, not necessarily in linear
  43166. * order.
  43167. *
  43168. * Each `TcbOp` may insert statements into the body of the TCB, and also optionally return a
  43169. * `ts.Expression` which can be used to reference the operation's result.
  43170. */
  43171. class TcbOp {
  43172. /**
  43173. * Replacement value or operation used while this `TcbOp` is executing (i.e. to resolve circular
  43174. * references during its execution).
  43175. *
  43176. * This is usually a `null!` expression (which asks TS to infer an appropriate type), but another
  43177. * `TcbOp` can be returned in cases where additional code generation is necessary to deal with
  43178. * circular references.
  43179. */
  43180. circularFallback() {
  43181. return INFER_TYPE_FOR_CIRCULAR_OP_EXPR;
  43182. }
  43183. }
  43184. /**
  43185. * A `TcbOp` which creates an expression for a native DOM element (or web component) from a
  43186. * `TmplAstElement`.
  43187. *
  43188. * Executing this operation returns a reference to the element variable.
  43189. */
  43190. class TcbElementOp extends TcbOp {
  43191. tcb;
  43192. scope;
  43193. element;
  43194. constructor(tcb, scope, element) {
  43195. super();
  43196. this.tcb = tcb;
  43197. this.scope = scope;
  43198. this.element = element;
  43199. }
  43200. get optional() {
  43201. // The statement generated by this operation is only used for type-inference of the DOM
  43202. // element's type and won't report diagnostics by itself, so the operation is marked as optional
  43203. // to avoid generating statements for DOM elements that are never referenced.
  43204. return true;
  43205. }
  43206. execute() {
  43207. const id = this.tcb.allocateId();
  43208. // Add the declaration of the element using document.createElement.
  43209. const initializer = tsCreateElement(this.element.name);
  43210. addParseSpanInfo(initializer, this.element.startSourceSpan || this.element.sourceSpan);
  43211. this.scope.addStatement(tsCreateVariable(id, initializer));
  43212. return id;
  43213. }
  43214. }
  43215. /**
  43216. * A `TcbOp` which creates an expression for particular let- `TmplAstVariable` on a
  43217. * `TmplAstTemplate`'s context.
  43218. *
  43219. * Executing this operation returns a reference to the variable variable (lol).
  43220. */
  43221. class TcbTemplateVariableOp extends TcbOp {
  43222. tcb;
  43223. scope;
  43224. template;
  43225. variable;
  43226. constructor(tcb, scope, template, variable) {
  43227. super();
  43228. this.tcb = tcb;
  43229. this.scope = scope;
  43230. this.template = template;
  43231. this.variable = variable;
  43232. }
  43233. get optional() {
  43234. return false;
  43235. }
  43236. execute() {
  43237. // Look for a context variable for the template.
  43238. const ctx = this.scope.resolve(this.template);
  43239. // Allocate an identifier for the TmplAstVariable, and initialize it to a read of the variable
  43240. // on the template context.
  43241. const id = this.tcb.allocateId();
  43242. const initializer = ts.factory.createPropertyAccessExpression(
  43243. /* expression */ ctx,
  43244. /* name */ this.variable.value || '$implicit');
  43245. addParseSpanInfo(id, this.variable.keySpan);
  43246. // Declare the variable, and return its identifier.
  43247. let variable;
  43248. if (this.variable.valueSpan !== undefined) {
  43249. addParseSpanInfo(initializer, this.variable.valueSpan);
  43250. variable = tsCreateVariable(id, wrapForTypeChecker(initializer));
  43251. }
  43252. else {
  43253. variable = tsCreateVariable(id, initializer);
  43254. }
  43255. addParseSpanInfo(variable.declarationList.declarations[0], this.variable.sourceSpan);
  43256. this.scope.addStatement(variable);
  43257. return id;
  43258. }
  43259. }
  43260. /**
  43261. * A `TcbOp` which generates a variable for a `TmplAstTemplate`'s context.
  43262. *
  43263. * Executing this operation returns a reference to the template's context variable.
  43264. */
  43265. class TcbTemplateContextOp extends TcbOp {
  43266. tcb;
  43267. scope;
  43268. constructor(tcb, scope) {
  43269. super();
  43270. this.tcb = tcb;
  43271. this.scope = scope;
  43272. }
  43273. // The declaration of the context variable is only needed when the context is actually referenced.
  43274. optional = true;
  43275. execute() {
  43276. // Allocate a template ctx variable and declare it with an 'any' type. The type of this variable
  43277. // may be narrowed as a result of template guard conditions.
  43278. const ctx = this.tcb.allocateId();
  43279. const type = ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
  43280. this.scope.addStatement(tsDeclareVariable(ctx, type));
  43281. return ctx;
  43282. }
  43283. }
  43284. /**
  43285. * A `TcbOp` which generates a constant for a `TmplAstLetDeclaration`.
  43286. *
  43287. * Executing this operation returns a reference to the `@let` declaration.
  43288. */
  43289. class TcbLetDeclarationOp extends TcbOp {
  43290. tcb;
  43291. scope;
  43292. node;
  43293. constructor(tcb, scope, node) {
  43294. super();
  43295. this.tcb = tcb;
  43296. this.scope = scope;
  43297. this.node = node;
  43298. }
  43299. /**
  43300. * `@let` declarations are mandatory, because their expressions
  43301. * should be checked even if they aren't referenced anywhere.
  43302. */
  43303. optional = false;
  43304. execute() {
  43305. const id = this.tcb.allocateId();
  43306. addParseSpanInfo(id, this.node.nameSpan);
  43307. const value = tcbExpression(this.node.value, this.tcb, this.scope);
  43308. // Value needs to be wrapped, because spans for the expressions inside of it can
  43309. // be picked up incorrectly as belonging to the full variable declaration.
  43310. const varStatement = tsCreateVariable(id, wrapForTypeChecker(value), ts.NodeFlags.Const);
  43311. addParseSpanInfo(varStatement.declarationList.declarations[0], this.node.sourceSpan);
  43312. this.scope.addStatement(varStatement);
  43313. return id;
  43314. }
  43315. }
  43316. /**
  43317. * A `TcbOp` which descends into a `TmplAstTemplate`'s children and generates type-checking code for
  43318. * them.
  43319. *
  43320. * This operation wraps the children's type-checking code in an `if` block, which may include one
  43321. * or more type guard conditions that narrow types within the template body.
  43322. */
  43323. class TcbTemplateBodyOp extends TcbOp {
  43324. tcb;
  43325. scope;
  43326. template;
  43327. constructor(tcb, scope, template) {
  43328. super();
  43329. this.tcb = tcb;
  43330. this.scope = scope;
  43331. this.template = template;
  43332. }
  43333. get optional() {
  43334. return false;
  43335. }
  43336. execute() {
  43337. // An `if` will be constructed, within which the template's children will be type checked. The
  43338. // `if` is used for two reasons: it creates a new syntactic scope, isolating variables declared
  43339. // in the template's TCB from the outer context, and it allows any directives on the templates
  43340. // to perform type narrowing of either expressions or the template's context.
  43341. //
  43342. // The guard is the `if` block's condition. It's usually set to `true` but directives that exist
  43343. // on the template can trigger extra guard expressions that serve to narrow types within the
  43344. // `if`. `guard` is calculated by starting with `true` and adding other conditions as needed.
  43345. // Collect these into `guards` by processing the directives.
  43346. const directiveGuards = [];
  43347. const directives = this.tcb.boundTarget.getDirectivesOfNode(this.template);
  43348. if (directives !== null) {
  43349. for (const dir of directives) {
  43350. const dirInstId = this.scope.resolve(this.template, dir);
  43351. const dirId = this.tcb.env.reference(dir.ref);
  43352. // There are two kinds of guards. Template guards (ngTemplateGuards) allow type narrowing of
  43353. // the expression passed to an @Input of the directive. Scan the directive to see if it has
  43354. // any template guards, and generate them if needed.
  43355. dir.ngTemplateGuards.forEach((guard) => {
  43356. // For each template guard function on the directive, look for a binding to that input.
  43357. const boundInput = this.template.inputs.find((i) => i.name === guard.inputName) ||
  43358. this.template.templateAttrs.find((i) => i instanceof BoundAttribute && i.name === guard.inputName);
  43359. if (boundInput !== undefined) {
  43360. // If there is such a binding, generate an expression for it.
  43361. const expr = tcbExpression(boundInput.value, this.tcb, this.scope);
  43362. // The expression has already been checked in the type constructor invocation, so
  43363. // it should be ignored when used within a template guard.
  43364. markIgnoreDiagnostics(expr);
  43365. if (guard.type === 'binding') {
  43366. // Use the binding expression itself as guard.
  43367. directiveGuards.push(expr);
  43368. }
  43369. else {
  43370. // Call the guard function on the directive with the directive instance and that
  43371. // expression.
  43372. const guardInvoke = tsCallMethod(dirId, `ngTemplateGuard_${guard.inputName}`, [
  43373. dirInstId,
  43374. expr,
  43375. ]);
  43376. addParseSpanInfo(guardInvoke, boundInput.value.sourceSpan);
  43377. directiveGuards.push(guardInvoke);
  43378. }
  43379. }
  43380. });
  43381. // The second kind of guard is a template context guard. This guard narrows the template
  43382. // rendering context variable `ctx`.
  43383. if (dir.hasNgTemplateContextGuard) {
  43384. if (this.tcb.env.config.applyTemplateContextGuards) {
  43385. const ctx = this.scope.resolve(this.template);
  43386. const guardInvoke = tsCallMethod(dirId, 'ngTemplateContextGuard', [dirInstId, ctx]);
  43387. addParseSpanInfo(guardInvoke, this.template.sourceSpan);
  43388. directiveGuards.push(guardInvoke);
  43389. }
  43390. else if (this.template.variables.length > 0 &&
  43391. this.tcb.env.config.suggestionsForSuboptimalTypeInference) {
  43392. // The compiler could have inferred a better type for the variables in this template,
  43393. // but was prevented from doing so by the type-checking configuration. Issue a warning
  43394. // diagnostic.
  43395. this.tcb.oobRecorder.suboptimalTypeInference(this.tcb.id, this.template.variables);
  43396. }
  43397. }
  43398. }
  43399. }
  43400. // By default the guard is simply `true`.
  43401. let guard = null;
  43402. // If there are any guards from directives, use them instead.
  43403. if (directiveGuards.length > 0) {
  43404. // Pop the first value and use it as the initializer to reduce(). This way, a single guard
  43405. // will be used on its own, but two or more will be combined into binary AND expressions.
  43406. guard = directiveGuards.reduce((expr, dirGuard) => ts.factory.createBinaryExpression(expr, ts.SyntaxKind.AmpersandAmpersandToken, dirGuard), directiveGuards.pop());
  43407. }
  43408. // Create a new Scope for the template. This constructs the list of operations for the template
  43409. // children, as well as tracks bindings within the template.
  43410. const tmplScope = Scope.forNodes(this.tcb, this.scope, this.template, this.template.children, guard);
  43411. // Render the template's `Scope` into its statements.
  43412. const statements = tmplScope.render();
  43413. if (statements.length === 0) {
  43414. // As an optimization, don't generate the scope's block if it has no statements. This is
  43415. // beneficial for templates that contain for example `<span *ngIf="first"></span>`, in which
  43416. // case there's no need to render the `NgIf` guard expression. This seems like a minor
  43417. // improvement, however it reduces the number of flow-node antecedents that TypeScript needs
  43418. // to keep into account for such cases, resulting in an overall reduction of
  43419. // type-checking time.
  43420. return null;
  43421. }
  43422. let tmplBlock = ts.factory.createBlock(statements);
  43423. if (guard !== null) {
  43424. // The scope has a guard that needs to be applied, so wrap the template block into an `if`
  43425. // statement containing the guard expression.
  43426. tmplBlock = ts.factory.createIfStatement(
  43427. /* expression */ guard,
  43428. /* thenStatement */ tmplBlock);
  43429. }
  43430. this.scope.addStatement(tmplBlock);
  43431. return null;
  43432. }
  43433. }
  43434. /**
  43435. * A `TcbOp` which renders an Angular expression (e.g. `{{foo() && bar.baz}}`).
  43436. *
  43437. * Executing this operation returns nothing.
  43438. */
  43439. class TcbExpressionOp extends TcbOp {
  43440. tcb;
  43441. scope;
  43442. expression;
  43443. constructor(tcb, scope, expression) {
  43444. super();
  43445. this.tcb = tcb;
  43446. this.scope = scope;
  43447. this.expression = expression;
  43448. }
  43449. get optional() {
  43450. return false;
  43451. }
  43452. execute() {
  43453. const expr = tcbExpression(this.expression, this.tcb, this.scope);
  43454. this.scope.addStatement(ts.factory.createExpressionStatement(expr));
  43455. return null;
  43456. }
  43457. }
  43458. /**
  43459. * A `TcbOp` which constructs an instance of a directive. For generic directives, generic
  43460. * parameters are set to `any` type.
  43461. */
  43462. class TcbDirectiveTypeOpBase extends TcbOp {
  43463. tcb;
  43464. scope;
  43465. node;
  43466. dir;
  43467. constructor(tcb, scope, node, dir) {
  43468. super();
  43469. this.tcb = tcb;
  43470. this.scope = scope;
  43471. this.node = node;
  43472. this.dir = dir;
  43473. }
  43474. get optional() {
  43475. // The statement generated by this operation is only used to declare the directive's type and
  43476. // won't report diagnostics by itself, so the operation is marked as optional to avoid
  43477. // generating declarations for directives that don't have any inputs/outputs.
  43478. return true;
  43479. }
  43480. execute() {
  43481. const dirRef = this.dir.ref;
  43482. const rawType = this.tcb.env.referenceType(this.dir.ref);
  43483. let type;
  43484. if (this.dir.isGeneric === false || dirRef.node.typeParameters === undefined) {
  43485. type = rawType;
  43486. }
  43487. else {
  43488. if (!ts.isTypeReferenceNode(rawType)) {
  43489. throw new Error(`Expected TypeReferenceNode when referencing the type for ${this.dir.ref.debugName}`);
  43490. }
  43491. const typeArguments = dirRef.node.typeParameters.map(() => ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  43492. type = ts.factory.createTypeReferenceNode(rawType.typeName, typeArguments);
  43493. }
  43494. const id = this.tcb.allocateId();
  43495. addExpressionIdentifier(id, ExpressionIdentifier.DIRECTIVE);
  43496. addParseSpanInfo(id, this.node.startSourceSpan || this.node.sourceSpan);
  43497. this.scope.addStatement(tsDeclareVariable(id, type));
  43498. return id;
  43499. }
  43500. }
  43501. /**
  43502. * A `TcbOp` which constructs an instance of a non-generic directive _without_ setting any of its
  43503. * inputs. Inputs are later set in the `TcbDirectiveInputsOp`. Type checking was found to be
  43504. * faster when done in this way as opposed to `TcbDirectiveCtorOp` which is only necessary when the
  43505. * directive is generic.
  43506. *
  43507. * Executing this operation returns a reference to the directive instance variable with its inferred
  43508. * type.
  43509. */
  43510. class TcbNonGenericDirectiveTypeOp extends TcbDirectiveTypeOpBase {
  43511. /**
  43512. * Creates a variable declaration for this op's directive of the argument type. Returns the id of
  43513. * the newly created variable.
  43514. */
  43515. execute() {
  43516. const dirRef = this.dir.ref;
  43517. if (this.dir.isGeneric) {
  43518. throw new Error(`Assertion Error: expected ${dirRef.debugName} not to be generic.`);
  43519. }
  43520. return super.execute();
  43521. }
  43522. }
  43523. /**
  43524. * A `TcbOp` which constructs an instance of a generic directive with its generic parameters set
  43525. * to `any` type. This op is like `TcbDirectiveTypeOp`, except that generic parameters are set to
  43526. * `any` type. This is used for situations where we want to avoid inlining.
  43527. *
  43528. * Executing this operation returns a reference to the directive instance variable with its generic
  43529. * type parameters set to `any`.
  43530. */
  43531. class TcbGenericDirectiveTypeWithAnyParamsOp extends TcbDirectiveTypeOpBase {
  43532. execute() {
  43533. const dirRef = this.dir.ref;
  43534. if (dirRef.node.typeParameters === undefined) {
  43535. throw new Error(`Assertion Error: expected typeParameters when creating a declaration for ${dirRef.debugName}`);
  43536. }
  43537. return super.execute();
  43538. }
  43539. }
  43540. /**
  43541. * A `TcbOp` which creates a variable for a local ref in a template.
  43542. * The initializer for the variable is the variable expression for the directive, template, or
  43543. * element the ref refers to. When the reference is used in the template, those TCB statements will
  43544. * access this variable as well. For example:
  43545. * ```ts
  43546. * var _t1 = document.createElement('div');
  43547. * var _t2 = _t1;
  43548. * _t2.value
  43549. * ```
  43550. * This operation supports more fluent lookups for the `TemplateTypeChecker` when getting a symbol
  43551. * for a reference. In most cases, this isn't essential; that is, the information for the symbol
  43552. * could be gathered without this operation using the `BoundTarget`. However, for the case of
  43553. * ng-template references, we will need this reference variable to not only provide a location in
  43554. * the shim file, but also to narrow the variable to the correct `TemplateRef<T>` type rather than
  43555. * `TemplateRef<any>` (this work is still TODO).
  43556. *
  43557. * Executing this operation returns a reference to the directive instance variable with its inferred
  43558. * type.
  43559. */
  43560. class TcbReferenceOp extends TcbOp {
  43561. tcb;
  43562. scope;
  43563. node;
  43564. host;
  43565. target;
  43566. constructor(tcb, scope, node, host, target) {
  43567. super();
  43568. this.tcb = tcb;
  43569. this.scope = scope;
  43570. this.node = node;
  43571. this.host = host;
  43572. this.target = target;
  43573. }
  43574. // The statement generated by this operation is only used to for the Type Checker
  43575. // so it can map a reference variable in the template directly to a node in the TCB.
  43576. optional = true;
  43577. execute() {
  43578. const id = this.tcb.allocateId();
  43579. let initializer = this.target instanceof Template || this.target instanceof Element$1
  43580. ? this.scope.resolve(this.target)
  43581. : this.scope.resolve(this.host, this.target);
  43582. // The reference is either to an element, an <ng-template> node, or to a directive on an
  43583. // element or template.
  43584. if ((this.target instanceof Element$1 && !this.tcb.env.config.checkTypeOfDomReferences) ||
  43585. !this.tcb.env.config.checkTypeOfNonDomReferences) {
  43586. // References to DOM nodes are pinned to 'any' when `checkTypeOfDomReferences` is `false`.
  43587. // References to `TemplateRef`s and directives are pinned to 'any' when
  43588. // `checkTypeOfNonDomReferences` is `false`.
  43589. initializer = ts.factory.createAsExpression(initializer, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  43590. }
  43591. else if (this.target instanceof Template) {
  43592. // Direct references to an <ng-template> node simply require a value of type
  43593. // `TemplateRef<any>`. To get this, an expression of the form
  43594. // `(_t1 as any as TemplateRef<any>)` is constructed.
  43595. initializer = ts.factory.createAsExpression(initializer, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  43596. initializer = ts.factory.createAsExpression(initializer, this.tcb.env.referenceExternalType('@angular/core', 'TemplateRef', [DYNAMIC_TYPE]));
  43597. initializer = ts.factory.createParenthesizedExpression(initializer);
  43598. }
  43599. addParseSpanInfo(initializer, this.node.sourceSpan);
  43600. addParseSpanInfo(id, this.node.keySpan);
  43601. this.scope.addStatement(tsCreateVariable(id, initializer));
  43602. return id;
  43603. }
  43604. }
  43605. /**
  43606. * A `TcbOp` which is used when the target of a reference is missing. This operation generates a
  43607. * variable of type any for usages of the invalid reference to resolve to. The invalid reference
  43608. * itself is recorded out-of-band.
  43609. */
  43610. class TcbInvalidReferenceOp extends TcbOp {
  43611. tcb;
  43612. scope;
  43613. constructor(tcb, scope) {
  43614. super();
  43615. this.tcb = tcb;
  43616. this.scope = scope;
  43617. }
  43618. // The declaration of a missing reference is only needed when the reference is resolved.
  43619. optional = true;
  43620. execute() {
  43621. const id = this.tcb.allocateId();
  43622. this.scope.addStatement(tsCreateVariable(id, ANY_EXPRESSION));
  43623. return id;
  43624. }
  43625. }
  43626. /**
  43627. * A `TcbOp` which constructs an instance of a directive with types inferred from its inputs. The
  43628. * inputs themselves are not checked here; checking of inputs is achieved in `TcbDirectiveInputsOp`.
  43629. * Any errors reported in this statement are ignored, as the type constructor call is only present
  43630. * for type-inference.
  43631. *
  43632. * When a Directive is generic, it is required that the TCB generates the instance using this method
  43633. * in order to infer the type information correctly.
  43634. *
  43635. * Executing this operation returns a reference to the directive instance variable with its inferred
  43636. * type.
  43637. */
  43638. class TcbDirectiveCtorOp extends TcbOp {
  43639. tcb;
  43640. scope;
  43641. node;
  43642. dir;
  43643. constructor(tcb, scope, node, dir) {
  43644. super();
  43645. this.tcb = tcb;
  43646. this.scope = scope;
  43647. this.node = node;
  43648. this.dir = dir;
  43649. }
  43650. get optional() {
  43651. // The statement generated by this operation is only used to infer the directive's type and
  43652. // won't report diagnostics by itself, so the operation is marked as optional.
  43653. return true;
  43654. }
  43655. execute() {
  43656. const id = this.tcb.allocateId();
  43657. addExpressionIdentifier(id, ExpressionIdentifier.DIRECTIVE);
  43658. addParseSpanInfo(id, this.node.startSourceSpan || this.node.sourceSpan);
  43659. const genericInputs = new Map();
  43660. const boundAttrs = getBoundAttributes(this.dir, this.node);
  43661. for (const attr of boundAttrs) {
  43662. // Skip text attributes if configured to do so.
  43663. if (!this.tcb.env.config.checkTypeOfAttributes &&
  43664. attr.attribute instanceof TextAttribute) {
  43665. continue;
  43666. }
  43667. for (const { fieldName, isTwoWayBinding } of attr.inputs) {
  43668. // Skip the field if an attribute has already been bound to it; we can't have a duplicate
  43669. // key in the type constructor call.
  43670. if (genericInputs.has(fieldName)) {
  43671. continue;
  43672. }
  43673. const expression = translateInput(attr.attribute, this.tcb, this.scope);
  43674. genericInputs.set(fieldName, {
  43675. type: 'binding',
  43676. field: fieldName,
  43677. expression,
  43678. sourceSpan: attr.attribute.sourceSpan,
  43679. isTwoWayBinding,
  43680. });
  43681. }
  43682. }
  43683. // Add unset directive inputs for each of the remaining unset fields.
  43684. for (const { classPropertyName } of this.dir.inputs) {
  43685. if (!genericInputs.has(classPropertyName)) {
  43686. genericInputs.set(classPropertyName, { type: 'unset', field: classPropertyName });
  43687. }
  43688. }
  43689. // Call the type constructor of the directive to infer a type, and assign the directive
  43690. // instance.
  43691. const typeCtor = tcbCallTypeCtor(this.dir, this.tcb, Array.from(genericInputs.values()));
  43692. markIgnoreDiagnostics(typeCtor);
  43693. this.scope.addStatement(tsCreateVariable(id, typeCtor));
  43694. return id;
  43695. }
  43696. circularFallback() {
  43697. return new TcbDirectiveCtorCircularFallbackOp(this.tcb, this.scope, this.node, this.dir);
  43698. }
  43699. }
  43700. /**
  43701. * A `TcbOp` which generates code to check input bindings on an element that correspond with the
  43702. * members of a directive.
  43703. *
  43704. * Executing this operation returns nothing.
  43705. */
  43706. class TcbDirectiveInputsOp extends TcbOp {
  43707. tcb;
  43708. scope;
  43709. node;
  43710. dir;
  43711. constructor(tcb, scope, node, dir) {
  43712. super();
  43713. this.tcb = tcb;
  43714. this.scope = scope;
  43715. this.node = node;
  43716. this.dir = dir;
  43717. }
  43718. get optional() {
  43719. return false;
  43720. }
  43721. execute() {
  43722. let dirId = null;
  43723. // TODO(joost): report duplicate properties
  43724. const boundAttrs = getBoundAttributes(this.dir, this.node);
  43725. const seenRequiredInputs = new Set();
  43726. for (const attr of boundAttrs) {
  43727. // For bound inputs, the property is assigned the binding expression.
  43728. const expr = widenBinding(translateInput(attr.attribute, this.tcb, this.scope), this.tcb);
  43729. let assignment = wrapForDiagnostics(expr);
  43730. for (const { fieldName, required, transformType, isSignal, isTwoWayBinding } of attr.inputs) {
  43731. let target;
  43732. if (required) {
  43733. seenRequiredInputs.add(fieldName);
  43734. }
  43735. // Note: There is no special logic for transforms/coercion with signal inputs.
  43736. // For signal inputs, a `transformType` will never be set as we do not capture
  43737. // the transform in the compiler metadata. Signal inputs incorporate their
  43738. // transform write type into their member type, and we extract it below when
  43739. // setting the `WriteT` of such `InputSignalWithTransform<_, WriteT>`.
  43740. if (this.dir.coercedInputFields.has(fieldName)) {
  43741. let type;
  43742. if (transformType !== null) {
  43743. type = this.tcb.env.referenceTransplantedType(new TransplantedType(transformType));
  43744. }
  43745. else {
  43746. // The input has a coercion declaration which should be used instead of assigning the
  43747. // expression into the input field directly. To achieve this, a variable is declared
  43748. // with a type of `typeof Directive.ngAcceptInputType_fieldName` which is then used as
  43749. // target of the assignment.
  43750. const dirTypeRef = this.tcb.env.referenceType(this.dir.ref);
  43751. if (!ts.isTypeReferenceNode(dirTypeRef)) {
  43752. throw new Error(`Expected TypeReferenceNode from reference to ${this.dir.ref.debugName}`);
  43753. }
  43754. type = tsCreateTypeQueryForCoercedInput(dirTypeRef.typeName, fieldName);
  43755. }
  43756. const id = this.tcb.allocateId();
  43757. this.scope.addStatement(tsDeclareVariable(id, type));
  43758. target = id;
  43759. }
  43760. else if (this.dir.undeclaredInputFields.has(fieldName)) {
  43761. // If no coercion declaration is present nor is the field declared (i.e. the input is
  43762. // declared in a `@Directive` or `@Component` decorator's `inputs` property) there is no
  43763. // assignment target available, so this field is skipped.
  43764. continue;
  43765. }
  43766. else if (!this.tcb.env.config.honorAccessModifiersForInputBindings &&
  43767. this.dir.restrictedInputFields.has(fieldName)) {
  43768. // If strict checking of access modifiers is disabled and the field is restricted
  43769. // (i.e. private/protected/readonly), generate an assignment into a temporary variable
  43770. // that has the type of the field. This achieves type-checking but circumvents the access
  43771. // modifiers.
  43772. if (dirId === null) {
  43773. dirId = this.scope.resolve(this.node, this.dir);
  43774. }
  43775. const id = this.tcb.allocateId();
  43776. const dirTypeRef = this.tcb.env.referenceType(this.dir.ref);
  43777. if (!ts.isTypeReferenceNode(dirTypeRef)) {
  43778. throw new Error(`Expected TypeReferenceNode from reference to ${this.dir.ref.debugName}`);
  43779. }
  43780. const type = ts.factory.createIndexedAccessTypeNode(ts.factory.createTypeQueryNode(dirId), ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(fieldName)));
  43781. const temp = tsDeclareVariable(id, type);
  43782. this.scope.addStatement(temp);
  43783. target = id;
  43784. }
  43785. else {
  43786. if (dirId === null) {
  43787. dirId = this.scope.resolve(this.node, this.dir);
  43788. }
  43789. // To get errors assign directly to the fields on the instance, using property access
  43790. // when possible. String literal fields may not be valid JS identifiers so we use
  43791. // literal element access instead for those cases.
  43792. target = this.dir.stringLiteralInputFields.has(fieldName)
  43793. ? ts.factory.createElementAccessExpression(dirId, ts.factory.createStringLiteral(fieldName))
  43794. : ts.factory.createPropertyAccessExpression(dirId, ts.factory.createIdentifier(fieldName));
  43795. }
  43796. // For signal inputs, we unwrap the target `InputSignal`. Note that
  43797. // we intentionally do the following things:
  43798. // 1. keep the direct access to `dir.[field]` so that modifiers are honored.
  43799. // 2. follow the existing pattern where multiple targets assign a single expression.
  43800. // This is a significant requirement for language service auto-completion.
  43801. if (isSignal) {
  43802. const inputSignalBrandWriteSymbol = this.tcb.env.referenceExternalSymbol(Identifiers.InputSignalBrandWriteType.moduleName, Identifiers.InputSignalBrandWriteType.name);
  43803. if (!ts.isIdentifier(inputSignalBrandWriteSymbol) &&
  43804. !ts.isPropertyAccessExpression(inputSignalBrandWriteSymbol)) {
  43805. throw new Error(`Expected identifier or property access for reference to ${Identifiers.InputSignalBrandWriteType.name}`);
  43806. }
  43807. target = ts.factory.createElementAccessExpression(target, inputSignalBrandWriteSymbol);
  43808. }
  43809. if (attr.attribute.keySpan !== undefined) {
  43810. addParseSpanInfo(target, attr.attribute.keySpan);
  43811. }
  43812. // Two-way bindings accept `T | WritableSignal<T>` so we have to unwrap the value.
  43813. if (isTwoWayBinding && this.tcb.env.config.allowSignalsInTwoWayBindings) {
  43814. assignment = unwrapWritableSignal(assignment, this.tcb);
  43815. }
  43816. // Finally the assignment is extended by assigning it into the target expression.
  43817. assignment = ts.factory.createBinaryExpression(target, ts.SyntaxKind.EqualsToken, assignment);
  43818. }
  43819. addParseSpanInfo(assignment, attr.attribute.sourceSpan);
  43820. // Ignore diagnostics for text attributes if configured to do so.
  43821. if (!this.tcb.env.config.checkTypeOfAttributes &&
  43822. attr.attribute instanceof TextAttribute) {
  43823. markIgnoreDiagnostics(assignment);
  43824. }
  43825. this.scope.addStatement(ts.factory.createExpressionStatement(assignment));
  43826. }
  43827. this.checkRequiredInputs(seenRequiredInputs);
  43828. return null;
  43829. }
  43830. checkRequiredInputs(seenRequiredInputs) {
  43831. const missing = [];
  43832. for (const input of this.dir.inputs) {
  43833. if (input.required && !seenRequiredInputs.has(input.classPropertyName)) {
  43834. missing.push(input.bindingPropertyName);
  43835. }
  43836. }
  43837. if (missing.length > 0) {
  43838. this.tcb.oobRecorder.missingRequiredInputs(this.tcb.id, this.node, this.dir.name, this.dir.isComponent, missing);
  43839. }
  43840. }
  43841. }
  43842. /**
  43843. * A `TcbOp` which is used to generate a fallback expression if the inference of a directive type
  43844. * via `TcbDirectiveCtorOp` requires a reference to its own type. This can happen using a template
  43845. * reference:
  43846. *
  43847. * ```html
  43848. * <some-cmp #ref [prop]="ref.foo"></some-cmp>
  43849. * ```
  43850. *
  43851. * In this case, `TcbDirectiveCtorCircularFallbackOp` will add a second inference of the directive
  43852. * type to the type-check block, this time calling the directive's type constructor without any
  43853. * input expressions. This infers the widest possible supertype for the directive, which is used to
  43854. * resolve any recursive references required to infer the real type.
  43855. */
  43856. class TcbDirectiveCtorCircularFallbackOp extends TcbOp {
  43857. tcb;
  43858. scope;
  43859. node;
  43860. dir;
  43861. constructor(tcb, scope, node, dir) {
  43862. super();
  43863. this.tcb = tcb;
  43864. this.scope = scope;
  43865. this.node = node;
  43866. this.dir = dir;
  43867. }
  43868. get optional() {
  43869. return false;
  43870. }
  43871. execute() {
  43872. const id = this.tcb.allocateId();
  43873. const typeCtor = this.tcb.env.typeCtorFor(this.dir);
  43874. const circularPlaceholder = ts.factory.createCallExpression(typeCtor,
  43875. /* typeArguments */ undefined, [ts.factory.createNonNullExpression(ts.factory.createNull())]);
  43876. this.scope.addStatement(tsCreateVariable(id, circularPlaceholder));
  43877. return id;
  43878. }
  43879. }
  43880. /**
  43881. * A `TcbOp` which feeds elements and unclaimed properties to the `DomSchemaChecker`.
  43882. *
  43883. * The DOM schema is not checked via TCB code generation. Instead, the `DomSchemaChecker` ingests
  43884. * elements and property bindings and accumulates synthetic `ts.Diagnostic`s out-of-band. These are
  43885. * later merged with the diagnostics generated from the TCB.
  43886. *
  43887. * For convenience, the TCB iteration of the template is used to drive the `DomSchemaChecker` via
  43888. * the `TcbDomSchemaCheckerOp`.
  43889. */
  43890. class TcbDomSchemaCheckerOp extends TcbOp {
  43891. tcb;
  43892. element;
  43893. checkElement;
  43894. claimedInputs;
  43895. constructor(tcb, element, checkElement, claimedInputs) {
  43896. super();
  43897. this.tcb = tcb;
  43898. this.element = element;
  43899. this.checkElement = checkElement;
  43900. this.claimedInputs = claimedInputs;
  43901. }
  43902. get optional() {
  43903. return false;
  43904. }
  43905. execute() {
  43906. if (this.checkElement) {
  43907. this.tcb.domSchemaChecker.checkElement(this.tcb.id, this.element, this.tcb.schemas, this.tcb.hostIsStandalone);
  43908. }
  43909. // TODO(alxhub): this could be more efficient.
  43910. for (const binding of this.element.inputs) {
  43911. const isPropertyBinding = binding.type === exports.BindingType.Property || binding.type === exports.BindingType.TwoWay;
  43912. if (isPropertyBinding && this.claimedInputs.has(binding.name)) {
  43913. // Skip this binding as it was claimed by a directive.
  43914. continue;
  43915. }
  43916. if (isPropertyBinding && binding.name !== 'style' && binding.name !== 'class') {
  43917. // A direct binding to a property.
  43918. const propertyName = ATTR_TO_PROP.get(binding.name) ?? binding.name;
  43919. this.tcb.domSchemaChecker.checkProperty(this.tcb.id, this.element, propertyName, binding.sourceSpan, this.tcb.schemas, this.tcb.hostIsStandalone);
  43920. }
  43921. }
  43922. return null;
  43923. }
  43924. }
  43925. /**
  43926. * A `TcbOp` that finds and flags control flow nodes that interfere with content projection.
  43927. *
  43928. * Context:
  43929. * Control flow blocks try to emulate the content projection behavior of `*ngIf` and `*ngFor`
  43930. * in order to reduce breakages when moving from one syntax to the other (see #52414), however the
  43931. * approach only works if there's only one element at the root of the control flow expression.
  43932. * This means that a stray sibling node (e.g. text) can prevent an element from being projected
  43933. * into the right slot. The purpose of the `TcbOp` is to find any places where a node at the root
  43934. * of a control flow expression *would have been projected* into a specific slot, if the control
  43935. * flow node didn't exist.
  43936. */
  43937. class TcbControlFlowContentProjectionOp extends TcbOp {
  43938. tcb;
  43939. element;
  43940. ngContentSelectors;
  43941. componentName;
  43942. category;
  43943. constructor(tcb, element, ngContentSelectors, componentName) {
  43944. super();
  43945. this.tcb = tcb;
  43946. this.element = element;
  43947. this.ngContentSelectors = ngContentSelectors;
  43948. this.componentName = componentName;
  43949. // We only need to account for `error` and `warning` since
  43950. // this check won't be enabled for `suppress`.
  43951. this.category =
  43952. tcb.env.config.controlFlowPreventingContentProjection === 'error'
  43953. ? ts.DiagnosticCategory.Error
  43954. : ts.DiagnosticCategory.Warning;
  43955. }
  43956. optional = false;
  43957. execute() {
  43958. const controlFlowToCheck = this.findPotentialControlFlowNodes();
  43959. if (controlFlowToCheck.length > 0) {
  43960. const matcher = new SelectorMatcher();
  43961. for (const selector of this.ngContentSelectors) {
  43962. // `*` is a special selector for the catch-all slot.
  43963. if (selector !== '*') {
  43964. matcher.addSelectables(CssSelector.parse(selector), selector);
  43965. }
  43966. }
  43967. for (const root of controlFlowToCheck) {
  43968. for (const child of root.children) {
  43969. if (child instanceof Element$1 || child instanceof Template) {
  43970. matcher.match(createCssSelectorFromNode(child), (_, originalSelector) => {
  43971. this.tcb.oobRecorder.controlFlowPreventingContentProjection(this.tcb.id, this.category, child, this.componentName, originalSelector, root, this.tcb.hostPreserveWhitespaces);
  43972. });
  43973. }
  43974. }
  43975. }
  43976. }
  43977. return null;
  43978. }
  43979. findPotentialControlFlowNodes() {
  43980. const result = [];
  43981. for (const child of this.element.children) {
  43982. if (child instanceof ForLoopBlock) {
  43983. if (this.shouldCheck(child)) {
  43984. result.push(child);
  43985. }
  43986. if (child.empty !== null && this.shouldCheck(child.empty)) {
  43987. result.push(child.empty);
  43988. }
  43989. }
  43990. else if (child instanceof IfBlock) {
  43991. for (const branch of child.branches) {
  43992. if (this.shouldCheck(branch)) {
  43993. result.push(branch);
  43994. }
  43995. }
  43996. }
  43997. else if (child instanceof SwitchBlock) {
  43998. for (const current of child.cases) {
  43999. if (this.shouldCheck(current)) {
  44000. result.push(current);
  44001. }
  44002. }
  44003. }
  44004. }
  44005. return result;
  44006. }
  44007. shouldCheck(node) {
  44008. // Skip nodes with less than two children since it's impossible
  44009. // for them to run into the issue that we're checking for.
  44010. if (node.children.length < 2) {
  44011. return false;
  44012. }
  44013. let hasSeenRootNode = false;
  44014. // Check the number of root nodes while skipping empty text where relevant.
  44015. for (const child of node.children) {
  44016. // Normally `preserveWhitspaces` would have been accounted for during parsing, however
  44017. // in `ngtsc/annotations/component/src/resources.ts#parseExtractedTemplate` we enable
  44018. // `preserveWhitespaces` to preserve the accuracy of source maps diagnostics. This means
  44019. // that we have to account for it here since the presence of text nodes affects the
  44020. // content projection behavior.
  44021. if (!(child instanceof Text$3) ||
  44022. this.tcb.hostPreserveWhitespaces ||
  44023. child.value.trim().length > 0) {
  44024. // Content projection will be affected if there's more than one root node.
  44025. if (hasSeenRootNode) {
  44026. return true;
  44027. }
  44028. hasSeenRootNode = true;
  44029. }
  44030. }
  44031. return false;
  44032. }
  44033. }
  44034. /**
  44035. * Mapping between attributes names that don't correspond to their element property names.
  44036. * Note: this mapping has to be kept in sync with the equally named mapping in the runtime.
  44037. */
  44038. const ATTR_TO_PROP = new Map(Object.entries({
  44039. 'class': 'className',
  44040. 'for': 'htmlFor',
  44041. 'formaction': 'formAction',
  44042. 'innerHtml': 'innerHTML',
  44043. 'readonly': 'readOnly',
  44044. 'tabindex': 'tabIndex',
  44045. }));
  44046. /**
  44047. * A `TcbOp` which generates code to check "unclaimed inputs" - bindings on an element which were
  44048. * not attributed to any directive or component, and are instead processed against the HTML element
  44049. * itself.
  44050. *
  44051. * Currently, only the expressions of these bindings are checked. The targets of the bindings are
  44052. * checked against the DOM schema via a `TcbDomSchemaCheckerOp`.
  44053. *
  44054. * Executing this operation returns nothing.
  44055. */
  44056. class TcbUnclaimedInputsOp extends TcbOp {
  44057. tcb;
  44058. scope;
  44059. element;
  44060. claimedInputs;
  44061. constructor(tcb, scope, element, claimedInputs) {
  44062. super();
  44063. this.tcb = tcb;
  44064. this.scope = scope;
  44065. this.element = element;
  44066. this.claimedInputs = claimedInputs;
  44067. }
  44068. get optional() {
  44069. return false;
  44070. }
  44071. execute() {
  44072. // `this.inputs` contains only those bindings not matched by any directive. These bindings go to
  44073. // the element itself.
  44074. let elId = null;
  44075. // TODO(alxhub): this could be more efficient.
  44076. for (const binding of this.element.inputs) {
  44077. const isPropertyBinding = binding.type === exports.BindingType.Property || binding.type === exports.BindingType.TwoWay;
  44078. if (isPropertyBinding && this.claimedInputs.has(binding.name)) {
  44079. // Skip this binding as it was claimed by a directive.
  44080. continue;
  44081. }
  44082. const expr = widenBinding(tcbExpression(binding.value, this.tcb, this.scope), this.tcb);
  44083. if (this.tcb.env.config.checkTypeOfDomBindings && isPropertyBinding) {
  44084. if (binding.name !== 'style' && binding.name !== 'class') {
  44085. if (elId === null) {
  44086. elId = this.scope.resolve(this.element);
  44087. }
  44088. // A direct binding to a property.
  44089. const propertyName = ATTR_TO_PROP.get(binding.name) ?? binding.name;
  44090. const prop = ts.factory.createElementAccessExpression(elId, ts.factory.createStringLiteral(propertyName));
  44091. const stmt = ts.factory.createBinaryExpression(prop, ts.SyntaxKind.EqualsToken, wrapForDiagnostics(expr));
  44092. addParseSpanInfo(stmt, binding.sourceSpan);
  44093. this.scope.addStatement(ts.factory.createExpressionStatement(stmt));
  44094. }
  44095. else {
  44096. this.scope.addStatement(ts.factory.createExpressionStatement(expr));
  44097. }
  44098. }
  44099. else {
  44100. // A binding to an animation, attribute, class or style. For now, only validate the right-
  44101. // hand side of the expression.
  44102. // TODO: properly check class and style bindings.
  44103. this.scope.addStatement(ts.factory.createExpressionStatement(expr));
  44104. }
  44105. }
  44106. return null;
  44107. }
  44108. }
  44109. /**
  44110. * A `TcbOp` which generates code to check event bindings on an element that correspond with the
  44111. * outputs of a directive.
  44112. *
  44113. * Executing this operation returns nothing.
  44114. */
  44115. class TcbDirectiveOutputsOp extends TcbOp {
  44116. tcb;
  44117. scope;
  44118. node;
  44119. dir;
  44120. constructor(tcb, scope, node, dir) {
  44121. super();
  44122. this.tcb = tcb;
  44123. this.scope = scope;
  44124. this.node = node;
  44125. this.dir = dir;
  44126. }
  44127. get optional() {
  44128. return false;
  44129. }
  44130. execute() {
  44131. let dirId = null;
  44132. const outputs = this.dir.outputs;
  44133. for (const output of this.node.outputs) {
  44134. if (output.type === exports.ParsedEventType.Animation ||
  44135. !outputs.hasBindingPropertyName(output.name)) {
  44136. continue;
  44137. }
  44138. if (this.tcb.env.config.checkTypeOfOutputEvents && output.name.endsWith('Change')) {
  44139. const inputName = output.name.slice(0, -6);
  44140. checkSplitTwoWayBinding(inputName, output, this.node.inputs, this.tcb);
  44141. }
  44142. // TODO(alxhub): consider supporting multiple fields with the same property name for outputs.
  44143. const field = outputs.getByBindingPropertyName(output.name)[0].classPropertyName;
  44144. if (dirId === null) {
  44145. dirId = this.scope.resolve(this.node, this.dir);
  44146. }
  44147. const outputField = ts.factory.createElementAccessExpression(dirId, ts.factory.createStringLiteral(field));
  44148. addParseSpanInfo(outputField, output.keySpan);
  44149. if (this.tcb.env.config.checkTypeOfOutputEvents) {
  44150. // For strict checking of directive events, generate a call to the `subscribe` method
  44151. // on the directive's output field to let type information flow into the handler function's
  44152. // `$event` parameter.
  44153. const handler = tcbCreateEventHandler(output, this.tcb, this.scope, 0 /* EventParamType.Infer */);
  44154. const subscribeFn = ts.factory.createPropertyAccessExpression(outputField, 'subscribe');
  44155. const call = ts.factory.createCallExpression(subscribeFn, /* typeArguments */ undefined, [
  44156. handler,
  44157. ]);
  44158. addParseSpanInfo(call, output.sourceSpan);
  44159. this.scope.addStatement(ts.factory.createExpressionStatement(call));
  44160. }
  44161. else {
  44162. // If strict checking of directive events is disabled:
  44163. //
  44164. // * We still generate the access to the output field as a statement in the TCB so consumers
  44165. // of the `TemplateTypeChecker` can still find the node for the class member for the
  44166. // output.
  44167. // * Emit a handler function where the `$event` parameter has an explicit `any` type.
  44168. this.scope.addStatement(ts.factory.createExpressionStatement(outputField));
  44169. const handler = tcbCreateEventHandler(output, this.tcb, this.scope, 1 /* EventParamType.Any */);
  44170. this.scope.addStatement(ts.factory.createExpressionStatement(handler));
  44171. }
  44172. }
  44173. return null;
  44174. }
  44175. }
  44176. /**
  44177. * A `TcbOp` which generates code to check "unclaimed outputs" - event bindings on an element which
  44178. * were not attributed to any directive or component, and are instead processed against the HTML
  44179. * element itself.
  44180. *
  44181. * Executing this operation returns nothing.
  44182. */
  44183. class TcbUnclaimedOutputsOp extends TcbOp {
  44184. tcb;
  44185. scope;
  44186. element;
  44187. claimedOutputs;
  44188. constructor(tcb, scope, element, claimedOutputs) {
  44189. super();
  44190. this.tcb = tcb;
  44191. this.scope = scope;
  44192. this.element = element;
  44193. this.claimedOutputs = claimedOutputs;
  44194. }
  44195. get optional() {
  44196. return false;
  44197. }
  44198. execute() {
  44199. let elId = null;
  44200. // TODO(alxhub): this could be more efficient.
  44201. for (const output of this.element.outputs) {
  44202. if (this.claimedOutputs.has(output.name)) {
  44203. // Skip this event handler as it was claimed by a directive.
  44204. continue;
  44205. }
  44206. if (this.tcb.env.config.checkTypeOfOutputEvents && output.name.endsWith('Change')) {
  44207. const inputName = output.name.slice(0, -6);
  44208. if (checkSplitTwoWayBinding(inputName, output, this.element.inputs, this.tcb)) {
  44209. // Skip this event handler as the error was already handled.
  44210. continue;
  44211. }
  44212. }
  44213. if (output.type === exports.ParsedEventType.Animation) {
  44214. // Animation output bindings always have an `$event` parameter of type `AnimationEvent`.
  44215. const eventType = this.tcb.env.config.checkTypeOfAnimationEvents
  44216. ? this.tcb.env.referenceExternalType('@angular/animations', 'AnimationEvent')
  44217. : 1 /* EventParamType.Any */;
  44218. const handler = tcbCreateEventHandler(output, this.tcb, this.scope, eventType);
  44219. this.scope.addStatement(ts.factory.createExpressionStatement(handler));
  44220. }
  44221. else if (this.tcb.env.config.checkTypeOfDomEvents) {
  44222. // If strict checking of DOM events is enabled, generate a call to `addEventListener` on
  44223. // the element instance so that TypeScript's type inference for
  44224. // `HTMLElement.addEventListener` using `HTMLElementEventMap` to infer an accurate type for
  44225. // `$event` depending on the event name. For unknown event names, TypeScript resorts to the
  44226. // base `Event` type.
  44227. const handler = tcbCreateEventHandler(output, this.tcb, this.scope, 0 /* EventParamType.Infer */);
  44228. if (elId === null) {
  44229. elId = this.scope.resolve(this.element);
  44230. }
  44231. const propertyAccess = ts.factory.createPropertyAccessExpression(elId, 'addEventListener');
  44232. addParseSpanInfo(propertyAccess, output.keySpan);
  44233. const call = ts.factory.createCallExpression(
  44234. /* expression */ propertyAccess,
  44235. /* typeArguments */ undefined,
  44236. /* arguments */ [ts.factory.createStringLiteral(output.name), handler]);
  44237. addParseSpanInfo(call, output.sourceSpan);
  44238. this.scope.addStatement(ts.factory.createExpressionStatement(call));
  44239. }
  44240. else {
  44241. // If strict checking of DOM inputs is disabled, emit a handler function where the `$event`
  44242. // parameter has an explicit `any` type.
  44243. const handler = tcbCreateEventHandler(output, this.tcb, this.scope, 1 /* EventParamType.Any */);
  44244. this.scope.addStatement(ts.factory.createExpressionStatement(handler));
  44245. }
  44246. }
  44247. return null;
  44248. }
  44249. }
  44250. /**
  44251. * A `TcbOp` which generates a completion point for the component context.
  44252. *
  44253. * This completion point looks like `this. ;` in the TCB output, and does not produce diagnostics.
  44254. * TypeScript autocompletion APIs can be used at this completion point (after the '.') to produce
  44255. * autocompletion results of properties and methods from the template's component context.
  44256. */
  44257. class TcbComponentContextCompletionOp extends TcbOp {
  44258. scope;
  44259. constructor(scope) {
  44260. super();
  44261. this.scope = scope;
  44262. }
  44263. optional = false;
  44264. execute() {
  44265. const ctx = ts.factory.createThis();
  44266. const ctxDot = ts.factory.createPropertyAccessExpression(ctx, '');
  44267. markIgnoreDiagnostics(ctxDot);
  44268. addExpressionIdentifier(ctxDot, ExpressionIdentifier.COMPONENT_COMPLETION);
  44269. this.scope.addStatement(ts.factory.createExpressionStatement(ctxDot));
  44270. return null;
  44271. }
  44272. }
  44273. /**
  44274. * A `TcbOp` which renders a variable defined inside of block syntax (e.g. `@if (expr; as var) {}`).
  44275. *
  44276. * Executing this operation returns the identifier which can be used to refer to the variable.
  44277. */
  44278. class TcbBlockVariableOp extends TcbOp {
  44279. tcb;
  44280. scope;
  44281. initializer;
  44282. variable;
  44283. constructor(tcb, scope, initializer, variable) {
  44284. super();
  44285. this.tcb = tcb;
  44286. this.scope = scope;
  44287. this.initializer = initializer;
  44288. this.variable = variable;
  44289. }
  44290. get optional() {
  44291. return false;
  44292. }
  44293. execute() {
  44294. const id = this.tcb.allocateId();
  44295. addParseSpanInfo(id, this.variable.keySpan);
  44296. const variable = tsCreateVariable(id, wrapForTypeChecker(this.initializer));
  44297. addParseSpanInfo(variable.declarationList.declarations[0], this.variable.sourceSpan);
  44298. this.scope.addStatement(variable);
  44299. return id;
  44300. }
  44301. }
  44302. /**
  44303. * A `TcbOp` which renders a variable that is implicitly available within a block (e.g. `$count`
  44304. * in a `@for` block).
  44305. *
  44306. * Executing this operation returns the identifier which can be used to refer to the variable.
  44307. */
  44308. class TcbBlockImplicitVariableOp extends TcbOp {
  44309. tcb;
  44310. scope;
  44311. type;
  44312. variable;
  44313. constructor(tcb, scope, type, variable) {
  44314. super();
  44315. this.tcb = tcb;
  44316. this.scope = scope;
  44317. this.type = type;
  44318. this.variable = variable;
  44319. }
  44320. optional = true;
  44321. execute() {
  44322. const id = this.tcb.allocateId();
  44323. addParseSpanInfo(id, this.variable.keySpan);
  44324. const variable = tsDeclareVariable(id, this.type);
  44325. addParseSpanInfo(variable.declarationList.declarations[0], this.variable.sourceSpan);
  44326. this.scope.addStatement(variable);
  44327. return id;
  44328. }
  44329. }
  44330. /**
  44331. * A `TcbOp` which renders an `if` template block as a TypeScript `if` statement.
  44332. *
  44333. * Executing this operation returns nothing.
  44334. */
  44335. class TcbIfOp extends TcbOp {
  44336. tcb;
  44337. scope;
  44338. block;
  44339. expressionScopes = new Map();
  44340. constructor(tcb, scope, block) {
  44341. super();
  44342. this.tcb = tcb;
  44343. this.scope = scope;
  44344. this.block = block;
  44345. }
  44346. get optional() {
  44347. return false;
  44348. }
  44349. execute() {
  44350. const root = this.generateBranch(0);
  44351. root && this.scope.addStatement(root);
  44352. return null;
  44353. }
  44354. generateBranch(index) {
  44355. const branch = this.block.branches[index];
  44356. if (!branch) {
  44357. return undefined;
  44358. }
  44359. // If the expression is null, it means that it's an `else` statement.
  44360. if (branch.expression === null) {
  44361. const branchScope = this.getBranchScope(this.scope, branch, index);
  44362. return ts.factory.createBlock(branchScope.render());
  44363. }
  44364. // We process the expression first in the parent scope, but create a scope around the block
  44365. // that the body will inherit from. We do this, because we need to declare a separate variable
  44366. // for the case where the expression has an alias _and_ because we need the processed
  44367. // expression when generating the guard for the body.
  44368. const outerScope = Scope.forNodes(this.tcb, this.scope, branch, [], null);
  44369. outerScope.render().forEach((stmt) => this.scope.addStatement(stmt));
  44370. this.expressionScopes.set(branch, outerScope);
  44371. let expression = tcbExpression(branch.expression, this.tcb, this.scope);
  44372. if (branch.expressionAlias !== null) {
  44373. expression = ts.factory.createBinaryExpression(ts.factory.createParenthesizedExpression(expression), ts.SyntaxKind.AmpersandAmpersandToken, outerScope.resolve(branch.expressionAlias));
  44374. }
  44375. const bodyScope = this.getBranchScope(outerScope, branch, index);
  44376. return ts.factory.createIfStatement(expression, ts.factory.createBlock(bodyScope.render()), this.generateBranch(index + 1));
  44377. }
  44378. getBranchScope(parentScope, branch, index) {
  44379. const checkBody = this.tcb.env.config.checkControlFlowBodies;
  44380. return Scope.forNodes(this.tcb, parentScope, null, checkBody ? branch.children : [], checkBody ? this.generateBranchGuard(index) : null);
  44381. }
  44382. generateBranchGuard(index) {
  44383. let guard = null;
  44384. // Since event listeners are inside callbacks, type narrowing doesn't apply to them anymore.
  44385. // To recreate the behavior, we generate an expression that negates all the values of the
  44386. // branches _before_ the current one, and then we add the current branch's expression on top.
  44387. // For example `@if (expr === 1) {} @else if (expr === 2) {} @else if (expr === 3)`, the guard
  44388. // for the last expression will be `!(expr === 1) && !(expr === 2) && expr === 3`.
  44389. for (let i = 0; i <= index; i++) {
  44390. const branch = this.block.branches[i];
  44391. // Skip over branches without an expression.
  44392. if (branch.expression === null) {
  44393. continue;
  44394. }
  44395. // This shouldn't happen since all the state is handled
  44396. // internally, but we have the check just in case.
  44397. if (!this.expressionScopes.has(branch)) {
  44398. throw new Error(`Could not determine expression scope of branch at index ${i}`);
  44399. }
  44400. const expressionScope = this.expressionScopes.get(branch);
  44401. let expression;
  44402. // We need to recreate the expression and mark it to be ignored for diagnostics,
  44403. // because it was already checked as a part of the block's condition and we don't
  44404. // want it to produce a duplicate diagnostic.
  44405. expression = tcbExpression(branch.expression, this.tcb, expressionScope);
  44406. if (branch.expressionAlias !== null) {
  44407. expression = ts.factory.createBinaryExpression(ts.factory.createParenthesizedExpression(expression), ts.SyntaxKind.AmpersandAmpersandToken, expressionScope.resolve(branch.expressionAlias));
  44408. }
  44409. markIgnoreDiagnostics(expression);
  44410. // The expressions of the preceding branches have to be negated
  44411. // (e.g. `expr` becomes `!(expr)`) when comparing in the guard, except
  44412. // for the branch's own expression which is preserved as is.
  44413. const comparisonExpression = i === index
  44414. ? expression
  44415. : ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.ExclamationToken, ts.factory.createParenthesizedExpression(expression));
  44416. // Finally add the expression to the guard with an && operator.
  44417. guard =
  44418. guard === null
  44419. ? comparisonExpression
  44420. : ts.factory.createBinaryExpression(guard, ts.SyntaxKind.AmpersandAmpersandToken, comparisonExpression);
  44421. }
  44422. return guard;
  44423. }
  44424. }
  44425. /**
  44426. * A `TcbOp` which renders a `switch` block as a TypeScript `switch` statement.
  44427. *
  44428. * Executing this operation returns nothing.
  44429. */
  44430. class TcbSwitchOp extends TcbOp {
  44431. tcb;
  44432. scope;
  44433. block;
  44434. constructor(tcb, scope, block) {
  44435. super();
  44436. this.tcb = tcb;
  44437. this.scope = scope;
  44438. this.block = block;
  44439. }
  44440. get optional() {
  44441. return false;
  44442. }
  44443. execute() {
  44444. const switchExpression = tcbExpression(this.block.expression, this.tcb, this.scope);
  44445. const clauses = this.block.cases.map((current) => {
  44446. const checkBody = this.tcb.env.config.checkControlFlowBodies;
  44447. const clauseScope = Scope.forNodes(this.tcb, this.scope, null, checkBody ? current.children : [], checkBody ? this.generateGuard(current, switchExpression) : null);
  44448. const statements = [...clauseScope.render(), ts.factory.createBreakStatement()];
  44449. return current.expression === null
  44450. ? ts.factory.createDefaultClause(statements)
  44451. : ts.factory.createCaseClause(tcbExpression(current.expression, this.tcb, clauseScope), statements);
  44452. });
  44453. this.scope.addStatement(ts.factory.createSwitchStatement(switchExpression, ts.factory.createCaseBlock(clauses)));
  44454. return null;
  44455. }
  44456. generateGuard(node, switchValue) {
  44457. // For non-default cases, the guard needs to compare against the case value, e.g.
  44458. // `switchExpression === caseExpression`.
  44459. if (node.expression !== null) {
  44460. // The expression needs to be ignored for diagnostics since it has been checked already.
  44461. const expression = tcbExpression(node.expression, this.tcb, this.scope);
  44462. markIgnoreDiagnostics(expression);
  44463. return ts.factory.createBinaryExpression(switchValue, ts.SyntaxKind.EqualsEqualsEqualsToken, expression);
  44464. }
  44465. // To fully narrow the type in the default case, we need to generate an expression that negates
  44466. // the values of all of the other expressions. For example:
  44467. // @switch (expr) {
  44468. // @case (1) {}
  44469. // @case (2) {}
  44470. // @default {}
  44471. // }
  44472. // Will produce the guard `expr !== 1 && expr !== 2`.
  44473. let guard = null;
  44474. for (const current of this.block.cases) {
  44475. if (current.expression === null) {
  44476. continue;
  44477. }
  44478. // The expression needs to be ignored for diagnostics since it has been checked already.
  44479. const expression = tcbExpression(current.expression, this.tcb, this.scope);
  44480. markIgnoreDiagnostics(expression);
  44481. const comparison = ts.factory.createBinaryExpression(switchValue, ts.SyntaxKind.ExclamationEqualsEqualsToken, expression);
  44482. if (guard === null) {
  44483. guard = comparison;
  44484. }
  44485. else {
  44486. guard = ts.factory.createBinaryExpression(guard, ts.SyntaxKind.AmpersandAmpersandToken, comparison);
  44487. }
  44488. }
  44489. return guard;
  44490. }
  44491. }
  44492. /**
  44493. * A `TcbOp` which renders a `for` block as a TypeScript `for...of` loop.
  44494. *
  44495. * Executing this operation returns nothing.
  44496. */
  44497. class TcbForOfOp extends TcbOp {
  44498. tcb;
  44499. scope;
  44500. block;
  44501. constructor(tcb, scope, block) {
  44502. super();
  44503. this.tcb = tcb;
  44504. this.scope = scope;
  44505. this.block = block;
  44506. }
  44507. get optional() {
  44508. return false;
  44509. }
  44510. execute() {
  44511. const loopScope = Scope.forNodes(this.tcb, this.scope, this.block, this.tcb.env.config.checkControlFlowBodies ? this.block.children : [], null);
  44512. const initializerId = loopScope.resolve(this.block.item);
  44513. if (!ts.isIdentifier(initializerId)) {
  44514. throw new Error(`Could not resolve for loop variable ${this.block.item.name} to an identifier`);
  44515. }
  44516. const initializer = ts.factory.createVariableDeclarationList([ts.factory.createVariableDeclaration(initializerId)], ts.NodeFlags.Const);
  44517. addParseSpanInfo(initializer, this.block.item.keySpan);
  44518. // It's common to have a for loop over a nullable value (e.g. produced by the `async` pipe).
  44519. // Add a non-null expression to allow such values to be assigned.
  44520. const expression = ts.factory.createNonNullExpression(tcbExpression(this.block.expression, this.tcb, this.scope));
  44521. const trackTranslator = new TcbForLoopTrackTranslator(this.tcb, loopScope, this.block);
  44522. const trackExpression = trackTranslator.translate(this.block.trackBy);
  44523. const statements = [
  44524. ...loopScope.render(),
  44525. ts.factory.createExpressionStatement(trackExpression),
  44526. ];
  44527. this.scope.addStatement(ts.factory.createForOfStatement(undefined, initializer, expression, ts.factory.createBlock(statements)));
  44528. return null;
  44529. }
  44530. }
  44531. /**
  44532. * Value used to break a circular reference between `TcbOp`s.
  44533. *
  44534. * This value is returned whenever `TcbOp`s have a circular dependency. The expression is a non-null
  44535. * assertion of the null value (in TypeScript, the expression `null!`). This construction will infer
  44536. * the least narrow type for whatever it's assigned to.
  44537. */
  44538. const INFER_TYPE_FOR_CIRCULAR_OP_EXPR = ts.factory.createNonNullExpression(ts.factory.createNull());
  44539. /**
  44540. * Overall generation context for the type check block.
  44541. *
  44542. * `Context` handles operations during code generation which are global with respect to the whole
  44543. * block. It's responsible for variable name allocation and management of any imports needed. It
  44544. * also contains the template metadata itself.
  44545. */
  44546. class Context {
  44547. env;
  44548. domSchemaChecker;
  44549. oobRecorder;
  44550. id;
  44551. boundTarget;
  44552. pipes;
  44553. schemas;
  44554. hostIsStandalone;
  44555. hostPreserveWhitespaces;
  44556. nextId = 1;
  44557. constructor(env, domSchemaChecker, oobRecorder, id, boundTarget, pipes, schemas, hostIsStandalone, hostPreserveWhitespaces) {
  44558. this.env = env;
  44559. this.domSchemaChecker = domSchemaChecker;
  44560. this.oobRecorder = oobRecorder;
  44561. this.id = id;
  44562. this.boundTarget = boundTarget;
  44563. this.pipes = pipes;
  44564. this.schemas = schemas;
  44565. this.hostIsStandalone = hostIsStandalone;
  44566. this.hostPreserveWhitespaces = hostPreserveWhitespaces;
  44567. }
  44568. /**
  44569. * Allocate a new variable name for use within the `Context`.
  44570. *
  44571. * Currently this uses a monotonically increasing counter, but in the future the variable name
  44572. * might change depending on the type of data being stored.
  44573. */
  44574. allocateId() {
  44575. return ts.factory.createIdentifier(`_t${this.nextId++}`);
  44576. }
  44577. getPipeByName(name) {
  44578. if (this.pipes === null || !this.pipes.has(name)) {
  44579. return null;
  44580. }
  44581. return this.pipes.get(name);
  44582. }
  44583. }
  44584. /**
  44585. * Local scope within the type check block for a particular template.
  44586. *
  44587. * The top-level template and each nested `<ng-template>` have their own `Scope`, which exist in a
  44588. * hierarchy. The structure of this hierarchy mirrors the syntactic scopes in the generated type
  44589. * check block, where each nested template is encased in an `if` structure.
  44590. *
  44591. * As a template's `TcbOp`s are executed in a given `Scope`, statements are added via
  44592. * `addStatement()`. When this processing is complete, the `Scope` can be turned into a `ts.Block`
  44593. * via `renderToBlock()`.
  44594. *
  44595. * If a `TcbOp` requires the output of another, it can call `resolve()`.
  44596. */
  44597. class Scope {
  44598. tcb;
  44599. parent;
  44600. guard;
  44601. /**
  44602. * A queue of operations which need to be performed to generate the TCB code for this scope.
  44603. *
  44604. * This array can contain either a `TcbOp` which has yet to be executed, or a `ts.Expression|null`
  44605. * representing the memoized result of executing the operation. As operations are executed, their
  44606. * results are written into the `opQueue`, overwriting the original operation.
  44607. *
  44608. * If an operation is in the process of being executed, it is temporarily overwritten here with
  44609. * `INFER_TYPE_FOR_CIRCULAR_OP_EXPR`. This way, if a cycle is encountered where an operation
  44610. * depends transitively on its own result, the inner operation will infer the least narrow type
  44611. * that fits instead. This has the same semantics as TypeScript itself when types are referenced
  44612. * circularly.
  44613. */
  44614. opQueue = [];
  44615. /**
  44616. * A map of `TmplAstElement`s to the index of their `TcbElementOp` in the `opQueue`
  44617. */
  44618. elementOpMap = new Map();
  44619. /**
  44620. * A map of maps which tracks the index of `TcbDirectiveCtorOp`s in the `opQueue` for each
  44621. * directive on a `TmplAstElement` or `TmplAstTemplate` node.
  44622. */
  44623. directiveOpMap = new Map();
  44624. /**
  44625. * A map of `TmplAstReference`s to the index of their `TcbReferenceOp` in the `opQueue`
  44626. */
  44627. referenceOpMap = new Map();
  44628. /**
  44629. * Map of immediately nested <ng-template>s (within this `Scope`) represented by `TmplAstTemplate`
  44630. * nodes to the index of their `TcbTemplateContextOp`s in the `opQueue`.
  44631. */
  44632. templateCtxOpMap = new Map();
  44633. /**
  44634. * Map of variables declared on the template that created this `Scope` (represented by
  44635. * `TmplAstVariable` nodes) to the index of their `TcbVariableOp`s in the `opQueue`, or to
  44636. * pre-resolved variable identifiers.
  44637. */
  44638. varMap = new Map();
  44639. /**
  44640. * A map of the names of `TmplAstLetDeclaration`s to the index of their op in the `opQueue`.
  44641. *
  44642. * Assumes that there won't be duplicated `@let` declarations within the same scope.
  44643. */
  44644. letDeclOpMap = new Map();
  44645. /**
  44646. * Statements for this template.
  44647. *
  44648. * Executing the `TcbOp`s in the `opQueue` populates this array.
  44649. */
  44650. statements = [];
  44651. /**
  44652. * Names of the for loop context variables and their types.
  44653. */
  44654. static forLoopContextVariableTypes = new Map([
  44655. ['$first', ts.SyntaxKind.BooleanKeyword],
  44656. ['$last', ts.SyntaxKind.BooleanKeyword],
  44657. ['$even', ts.SyntaxKind.BooleanKeyword],
  44658. ['$odd', ts.SyntaxKind.BooleanKeyword],
  44659. ['$index', ts.SyntaxKind.NumberKeyword],
  44660. ['$count', ts.SyntaxKind.NumberKeyword],
  44661. ]);
  44662. constructor(tcb, parent = null, guard = null) {
  44663. this.tcb = tcb;
  44664. this.parent = parent;
  44665. this.guard = guard;
  44666. }
  44667. /**
  44668. * Constructs a `Scope` given either a `TmplAstTemplate` or a list of `TmplAstNode`s.
  44669. *
  44670. * @param tcb the overall context of TCB generation.
  44671. * @param parentScope the `Scope` of the parent template (if any) or `null` if this is the root
  44672. * `Scope`.
  44673. * @param scopedNode Node that provides the scope around the child nodes (e.g. a
  44674. * `TmplAstTemplate` node exposing variables to its children).
  44675. * @param children Child nodes that should be appended to the TCB.
  44676. * @param guard an expression that is applied to this scope for type narrowing purposes.
  44677. */
  44678. static forNodes(tcb, parentScope, scopedNode, children, guard) {
  44679. const scope = new Scope(tcb, parentScope, guard);
  44680. if (parentScope === null && tcb.env.config.enableTemplateTypeChecker) {
  44681. // Add an autocompletion point for the component context.
  44682. scope.opQueue.push(new TcbComponentContextCompletionOp(scope));
  44683. }
  44684. // If given an actual `TmplAstTemplate` instance, then process any additional information it
  44685. // has.
  44686. if (scopedNode instanceof Template) {
  44687. // The template's variable declarations need to be added as `TcbVariableOp`s.
  44688. const varMap = new Map();
  44689. for (const v of scopedNode.variables) {
  44690. // Validate that variables on the `TmplAstTemplate` are only declared once.
  44691. if (!varMap.has(v.name)) {
  44692. varMap.set(v.name, v);
  44693. }
  44694. else {
  44695. const firstDecl = varMap.get(v.name);
  44696. tcb.oobRecorder.duplicateTemplateVar(tcb.id, v, firstDecl);
  44697. }
  44698. this.registerVariable(scope, v, new TcbTemplateVariableOp(tcb, scope, scopedNode, v));
  44699. }
  44700. }
  44701. else if (scopedNode instanceof IfBlockBranch) {
  44702. const { expression, expressionAlias } = scopedNode;
  44703. if (expression !== null && expressionAlias !== null) {
  44704. this.registerVariable(scope, expressionAlias, new TcbBlockVariableOp(tcb, scope, tcbExpression(expression, tcb, scope), expressionAlias));
  44705. }
  44706. }
  44707. else if (scopedNode instanceof ForLoopBlock) {
  44708. // Register the variable for the loop so it can be resolved by
  44709. // children. It'll be declared once the loop is created.
  44710. const loopInitializer = tcb.allocateId();
  44711. addParseSpanInfo(loopInitializer, scopedNode.item.sourceSpan);
  44712. scope.varMap.set(scopedNode.item, loopInitializer);
  44713. for (const variable of scopedNode.contextVariables) {
  44714. if (!this.forLoopContextVariableTypes.has(variable.value)) {
  44715. throw new Error(`Unrecognized for loop context variable ${variable.name}`);
  44716. }
  44717. const type = ts.factory.createKeywordTypeNode(this.forLoopContextVariableTypes.get(variable.value));
  44718. this.registerVariable(scope, variable, new TcbBlockImplicitVariableOp(tcb, scope, type, variable));
  44719. }
  44720. }
  44721. for (const node of children) {
  44722. scope.appendNode(node);
  44723. }
  44724. // Once everything is registered, we need to check if there are `@let`
  44725. // declarations that conflict with other local symbols defined after them.
  44726. for (const variable of scope.varMap.keys()) {
  44727. Scope.checkConflictingLet(scope, variable);
  44728. }
  44729. for (const ref of scope.referenceOpMap.keys()) {
  44730. Scope.checkConflictingLet(scope, ref);
  44731. }
  44732. return scope;
  44733. }
  44734. /** Registers a local variable with a scope. */
  44735. static registerVariable(scope, variable, op) {
  44736. const opIndex = scope.opQueue.push(op) - 1;
  44737. scope.varMap.set(variable, opIndex);
  44738. }
  44739. /**
  44740. * Look up a `ts.Expression` representing the value of some operation in the current `Scope`,
  44741. * including any parent scope(s). This method always returns a mutable clone of the
  44742. * `ts.Expression` with the comments cleared.
  44743. *
  44744. * @param node a `TmplAstNode` of the operation in question. The lookup performed will depend on
  44745. * the type of this node:
  44746. *
  44747. * Assuming `directive` is not present, then `resolve` will return:
  44748. *
  44749. * * `TmplAstElement` - retrieve the expression for the element DOM node
  44750. * * `TmplAstTemplate` - retrieve the template context variable
  44751. * * `TmplAstVariable` - retrieve a template let- variable
  44752. * * `TmplAstLetDeclaration` - retrieve a template `@let` declaration
  44753. * * `TmplAstReference` - retrieve variable created for the local ref
  44754. *
  44755. * @param directive if present, a directive type on a `TmplAstElement` or `TmplAstTemplate` to
  44756. * look up instead of the default for an element or template node.
  44757. */
  44758. resolve(node, directive) {
  44759. // Attempt to resolve the operation locally.
  44760. const res = this.resolveLocal(node, directive);
  44761. if (res !== null) {
  44762. // We want to get a clone of the resolved expression and clear the trailing comments
  44763. // so they don't continue to appear in every place the expression is used.
  44764. // As an example, this would otherwise produce:
  44765. // var _t1 /**T:DIR*/ /*1,2*/ = _ctor1();
  44766. // _t1 /**T:DIR*/ /*1,2*/.input = 'value';
  44767. //
  44768. // In addition, returning a clone prevents the consumer of `Scope#resolve` from
  44769. // attaching comments at the declaration site.
  44770. let clone;
  44771. if (ts.isIdentifier(res)) {
  44772. clone = ts.factory.createIdentifier(res.text);
  44773. }
  44774. else if (ts.isNonNullExpression(res)) {
  44775. clone = ts.factory.createNonNullExpression(res.expression);
  44776. }
  44777. else {
  44778. throw new Error(`Could not resolve ${node} to an Identifier or a NonNullExpression`);
  44779. }
  44780. ts.setOriginalNode(clone, res);
  44781. clone.parent = clone.parent;
  44782. return ts.setSyntheticTrailingComments(clone, []);
  44783. }
  44784. else if (this.parent !== null) {
  44785. // Check with the parent.
  44786. return this.parent.resolve(node, directive);
  44787. }
  44788. else {
  44789. throw new Error(`Could not resolve ${node} / ${directive}`);
  44790. }
  44791. }
  44792. /**
  44793. * Add a statement to this scope.
  44794. */
  44795. addStatement(stmt) {
  44796. this.statements.push(stmt);
  44797. }
  44798. /**
  44799. * Get the statements.
  44800. */
  44801. render() {
  44802. for (let i = 0; i < this.opQueue.length; i++) {
  44803. // Optional statements cannot be skipped when we are generating the TCB for use
  44804. // by the TemplateTypeChecker.
  44805. const skipOptional = !this.tcb.env.config.enableTemplateTypeChecker;
  44806. this.executeOp(i, skipOptional);
  44807. }
  44808. return this.statements;
  44809. }
  44810. /**
  44811. * Returns an expression of all template guards that apply to this scope, including those of
  44812. * parent scopes. If no guards have been applied, null is returned.
  44813. */
  44814. guards() {
  44815. let parentGuards = null;
  44816. if (this.parent !== null) {
  44817. // Start with the guards from the parent scope, if present.
  44818. parentGuards = this.parent.guards();
  44819. }
  44820. if (this.guard === null) {
  44821. // This scope does not have a guard, so return the parent's guards as is.
  44822. return parentGuards;
  44823. }
  44824. else if (parentGuards === null) {
  44825. // There's no guards from the parent scope, so this scope's guard represents all available
  44826. // guards.
  44827. return this.guard;
  44828. }
  44829. else {
  44830. // Both the parent scope and this scope provide a guard, so create a combination of the two.
  44831. // It is important that the parent guard is used as left operand, given that it may provide
  44832. // narrowing that is required for this scope's guard to be valid.
  44833. return ts.factory.createBinaryExpression(parentGuards, ts.SyntaxKind.AmpersandAmpersandToken, this.guard);
  44834. }
  44835. }
  44836. /** Returns whether a template symbol is defined locally within the current scope. */
  44837. isLocal(node) {
  44838. if (node instanceof Variable) {
  44839. return this.varMap.has(node);
  44840. }
  44841. if (node instanceof LetDeclaration$1) {
  44842. return this.letDeclOpMap.has(node.name);
  44843. }
  44844. return this.referenceOpMap.has(node);
  44845. }
  44846. resolveLocal(ref, directive) {
  44847. if (ref instanceof Reference$1 && this.referenceOpMap.has(ref)) {
  44848. return this.resolveOp(this.referenceOpMap.get(ref));
  44849. }
  44850. else if (ref instanceof LetDeclaration$1 && this.letDeclOpMap.has(ref.name)) {
  44851. return this.resolveOp(this.letDeclOpMap.get(ref.name).opIndex);
  44852. }
  44853. else if (ref instanceof Variable && this.varMap.has(ref)) {
  44854. // Resolving a context variable for this template.
  44855. // Execute the `TcbVariableOp` associated with the `TmplAstVariable`.
  44856. const opIndexOrNode = this.varMap.get(ref);
  44857. return typeof opIndexOrNode === 'number' ? this.resolveOp(opIndexOrNode) : opIndexOrNode;
  44858. }
  44859. else if (ref instanceof Template &&
  44860. directive === undefined &&
  44861. this.templateCtxOpMap.has(ref)) {
  44862. // Resolving the context of the given sub-template.
  44863. // Execute the `TcbTemplateContextOp` for the template.
  44864. return this.resolveOp(this.templateCtxOpMap.get(ref));
  44865. }
  44866. else if ((ref instanceof Element$1 || ref instanceof Template) &&
  44867. directive !== undefined &&
  44868. this.directiveOpMap.has(ref)) {
  44869. // Resolving a directive on an element or sub-template.
  44870. const dirMap = this.directiveOpMap.get(ref);
  44871. if (dirMap.has(directive)) {
  44872. return this.resolveOp(dirMap.get(directive));
  44873. }
  44874. else {
  44875. return null;
  44876. }
  44877. }
  44878. else if (ref instanceof Element$1 && this.elementOpMap.has(ref)) {
  44879. // Resolving the DOM node of an element in this template.
  44880. return this.resolveOp(this.elementOpMap.get(ref));
  44881. }
  44882. else {
  44883. return null;
  44884. }
  44885. }
  44886. /**
  44887. * Like `executeOp`, but assert that the operation actually returned `ts.Expression`.
  44888. */
  44889. resolveOp(opIndex) {
  44890. const res = this.executeOp(opIndex, /* skipOptional */ false);
  44891. if (res === null) {
  44892. throw new Error(`Error resolving operation, got null`);
  44893. }
  44894. return res;
  44895. }
  44896. /**
  44897. * Execute a particular `TcbOp` in the `opQueue`.
  44898. *
  44899. * This method replaces the operation in the `opQueue` with the result of execution (once done)
  44900. * and also protects against a circular dependency from the operation to itself by temporarily
  44901. * setting the operation's result to a special expression.
  44902. */
  44903. executeOp(opIndex, skipOptional) {
  44904. const op = this.opQueue[opIndex];
  44905. if (!(op instanceof TcbOp)) {
  44906. return op;
  44907. }
  44908. if (skipOptional && op.optional) {
  44909. return null;
  44910. }
  44911. // Set the result of the operation in the queue to its circular fallback. If executing this
  44912. // operation results in a circular dependency, this will prevent an infinite loop and allow for
  44913. // the resolution of such cycles.
  44914. this.opQueue[opIndex] = op.circularFallback();
  44915. const res = op.execute();
  44916. // Once the operation has finished executing, it's safe to cache the real result.
  44917. this.opQueue[opIndex] = res;
  44918. return res;
  44919. }
  44920. appendNode(node) {
  44921. if (node instanceof Element$1) {
  44922. const opIndex = this.opQueue.push(new TcbElementOp(this.tcb, this, node)) - 1;
  44923. this.elementOpMap.set(node, opIndex);
  44924. if (this.tcb.env.config.controlFlowPreventingContentProjection !== 'suppress') {
  44925. this.appendContentProjectionCheckOp(node);
  44926. }
  44927. this.appendDirectivesAndInputsOfNode(node);
  44928. this.appendOutputsOfNode(node);
  44929. this.appendChildren(node);
  44930. this.checkAndAppendReferencesOfNode(node);
  44931. }
  44932. else if (node instanceof Template) {
  44933. // Template children are rendered in a child scope.
  44934. this.appendDirectivesAndInputsOfNode(node);
  44935. this.appendOutputsOfNode(node);
  44936. const ctxIndex = this.opQueue.push(new TcbTemplateContextOp(this.tcb, this)) - 1;
  44937. this.templateCtxOpMap.set(node, ctxIndex);
  44938. if (this.tcb.env.config.checkTemplateBodies) {
  44939. this.opQueue.push(new TcbTemplateBodyOp(this.tcb, this, node));
  44940. }
  44941. else if (this.tcb.env.config.alwaysCheckSchemaInTemplateBodies) {
  44942. this.appendDeepSchemaChecks(node.children);
  44943. }
  44944. this.checkAndAppendReferencesOfNode(node);
  44945. }
  44946. else if (node instanceof DeferredBlock) {
  44947. this.appendDeferredBlock(node);
  44948. }
  44949. else if (node instanceof IfBlock) {
  44950. this.opQueue.push(new TcbIfOp(this.tcb, this, node));
  44951. }
  44952. else if (node instanceof SwitchBlock) {
  44953. this.opQueue.push(new TcbSwitchOp(this.tcb, this, node));
  44954. }
  44955. else if (node instanceof ForLoopBlock) {
  44956. this.opQueue.push(new TcbForOfOp(this.tcb, this, node));
  44957. node.empty && this.tcb.env.config.checkControlFlowBodies && this.appendChildren(node.empty);
  44958. }
  44959. else if (node instanceof BoundText) {
  44960. this.opQueue.push(new TcbExpressionOp(this.tcb, this, node.value));
  44961. }
  44962. else if (node instanceof Icu$1) {
  44963. this.appendIcuExpressions(node);
  44964. }
  44965. else if (node instanceof Content) {
  44966. this.appendChildren(node);
  44967. }
  44968. else if (node instanceof LetDeclaration$1) {
  44969. const opIndex = this.opQueue.push(new TcbLetDeclarationOp(this.tcb, this, node)) - 1;
  44970. if (this.isLocal(node)) {
  44971. this.tcb.oobRecorder.conflictingDeclaration(this.tcb.id, node);
  44972. }
  44973. else {
  44974. this.letDeclOpMap.set(node.name, { opIndex, node });
  44975. }
  44976. }
  44977. }
  44978. appendChildren(node) {
  44979. for (const child of node.children) {
  44980. this.appendNode(child);
  44981. }
  44982. }
  44983. checkAndAppendReferencesOfNode(node) {
  44984. for (const ref of node.references) {
  44985. const target = this.tcb.boundTarget.getReferenceTarget(ref);
  44986. let ctxIndex;
  44987. if (target === null) {
  44988. // The reference is invalid if it doesn't have a target, so report it as an error.
  44989. this.tcb.oobRecorder.missingReferenceTarget(this.tcb.id, ref);
  44990. // Any usages of the invalid reference will be resolved to a variable of type any.
  44991. ctxIndex = this.opQueue.push(new TcbInvalidReferenceOp(this.tcb, this)) - 1;
  44992. }
  44993. else if (target instanceof Template || target instanceof Element$1) {
  44994. ctxIndex = this.opQueue.push(new TcbReferenceOp(this.tcb, this, ref, node, target)) - 1;
  44995. }
  44996. else {
  44997. ctxIndex =
  44998. this.opQueue.push(new TcbReferenceOp(this.tcb, this, ref, node, target.directive)) - 1;
  44999. }
  45000. this.referenceOpMap.set(ref, ctxIndex);
  45001. }
  45002. }
  45003. appendDirectivesAndInputsOfNode(node) {
  45004. // Collect all the inputs on the element.
  45005. const claimedInputs = new Set();
  45006. const directives = this.tcb.boundTarget.getDirectivesOfNode(node);
  45007. if (directives === null || directives.length === 0) {
  45008. // If there are no directives, then all inputs are unclaimed inputs, so queue an operation
  45009. // to add them if needed.
  45010. if (node instanceof Element$1) {
  45011. this.opQueue.push(new TcbUnclaimedInputsOp(this.tcb, this, node, claimedInputs));
  45012. this.opQueue.push(new TcbDomSchemaCheckerOp(this.tcb, node, /* checkElement */ true, claimedInputs));
  45013. }
  45014. return;
  45015. }
  45016. else {
  45017. if (node instanceof Element$1) {
  45018. const isDeferred = this.tcb.boundTarget.isDeferred(node);
  45019. if (!isDeferred && directives.some((dirMeta) => dirMeta.isExplicitlyDeferred)) {
  45020. // This node has directives/components that were defer-loaded (included into
  45021. // `@Component.deferredImports`), but the node itself was used outside of a
  45022. // `@defer` block, which is the error.
  45023. this.tcb.oobRecorder.deferredComponentUsedEagerly(this.tcb.id, node);
  45024. }
  45025. }
  45026. }
  45027. const dirMap = new Map();
  45028. for (const dir of directives) {
  45029. let directiveOp;
  45030. const host = this.tcb.env.reflector;
  45031. const dirRef = dir.ref;
  45032. if (!dir.isGeneric) {
  45033. // The most common case is that when a directive is not generic, we use the normal
  45034. // `TcbNonDirectiveTypeOp`.
  45035. directiveOp = new TcbNonGenericDirectiveTypeOp(this.tcb, this, node, dir);
  45036. }
  45037. else if (!requiresInlineTypeCtor(dirRef.node, host, this.tcb.env) ||
  45038. this.tcb.env.config.useInlineTypeConstructors) {
  45039. // For generic directives, we use a type constructor to infer types. If a directive requires
  45040. // an inline type constructor, then inlining must be available to use the
  45041. // `TcbDirectiveCtorOp`. If not we, we fallback to using `any` – see below.
  45042. directiveOp = new TcbDirectiveCtorOp(this.tcb, this, node, dir);
  45043. }
  45044. else {
  45045. // If inlining is not available, then we give up on inferring the generic params, and use
  45046. // `any` type for the directive's generic parameters.
  45047. directiveOp = new TcbGenericDirectiveTypeWithAnyParamsOp(this.tcb, this, node, dir);
  45048. }
  45049. const dirIndex = this.opQueue.push(directiveOp) - 1;
  45050. dirMap.set(dir, dirIndex);
  45051. this.opQueue.push(new TcbDirectiveInputsOp(this.tcb, this, node, dir));
  45052. }
  45053. this.directiveOpMap.set(node, dirMap);
  45054. // After expanding the directives, we might need to queue an operation to check any unclaimed
  45055. // inputs.
  45056. if (node instanceof Element$1) {
  45057. // Go through the directives and remove any inputs that it claims from `elementInputs`.
  45058. for (const dir of directives) {
  45059. for (const propertyName of dir.inputs.propertyNames) {
  45060. claimedInputs.add(propertyName);
  45061. }
  45062. }
  45063. this.opQueue.push(new TcbUnclaimedInputsOp(this.tcb, this, node, claimedInputs));
  45064. // If there are no directives which match this element, then it's a "plain" DOM element (or a
  45065. // web component), and should be checked against the DOM schema. If any directives match,
  45066. // we must assume that the element could be custom (either a component, or a directive like
  45067. // <router-outlet>) and shouldn't validate the element name itself.
  45068. const checkElement = directives.length === 0;
  45069. this.opQueue.push(new TcbDomSchemaCheckerOp(this.tcb, node, checkElement, claimedInputs));
  45070. }
  45071. }
  45072. appendOutputsOfNode(node) {
  45073. // Collect all the outputs on the element.
  45074. const claimedOutputs = new Set();
  45075. const directives = this.tcb.boundTarget.getDirectivesOfNode(node);
  45076. if (directives === null || directives.length === 0) {
  45077. // If there are no directives, then all outputs are unclaimed outputs, so queue an operation
  45078. // to add them if needed.
  45079. if (node instanceof Element$1) {
  45080. this.opQueue.push(new TcbUnclaimedOutputsOp(this.tcb, this, node, claimedOutputs));
  45081. }
  45082. return;
  45083. }
  45084. // Queue operations for all directives to check the relevant outputs for a directive.
  45085. for (const dir of directives) {
  45086. this.opQueue.push(new TcbDirectiveOutputsOp(this.tcb, this, node, dir));
  45087. }
  45088. // After expanding the directives, we might need to queue an operation to check any unclaimed
  45089. // outputs.
  45090. if (node instanceof Element$1) {
  45091. // Go through the directives and register any outputs that it claims in `claimedOutputs`.
  45092. for (const dir of directives) {
  45093. for (const outputProperty of dir.outputs.propertyNames) {
  45094. claimedOutputs.add(outputProperty);
  45095. }
  45096. }
  45097. this.opQueue.push(new TcbUnclaimedOutputsOp(this.tcb, this, node, claimedOutputs));
  45098. }
  45099. }
  45100. appendDeepSchemaChecks(nodes) {
  45101. for (const node of nodes) {
  45102. if (!(node instanceof Element$1 || node instanceof Template)) {
  45103. continue;
  45104. }
  45105. if (node instanceof Element$1) {
  45106. const claimedInputs = new Set();
  45107. const directives = this.tcb.boundTarget.getDirectivesOfNode(node);
  45108. let hasDirectives;
  45109. if (directives === null || directives.length === 0) {
  45110. hasDirectives = false;
  45111. }
  45112. else {
  45113. hasDirectives = true;
  45114. for (const dir of directives) {
  45115. for (const propertyName of dir.inputs.propertyNames) {
  45116. claimedInputs.add(propertyName);
  45117. }
  45118. }
  45119. }
  45120. this.opQueue.push(new TcbDomSchemaCheckerOp(this.tcb, node, !hasDirectives, claimedInputs));
  45121. }
  45122. this.appendDeepSchemaChecks(node.children);
  45123. }
  45124. }
  45125. appendIcuExpressions(node) {
  45126. for (const variable of Object.values(node.vars)) {
  45127. this.opQueue.push(new TcbExpressionOp(this.tcb, this, variable.value));
  45128. }
  45129. for (const placeholder of Object.values(node.placeholders)) {
  45130. if (placeholder instanceof BoundText) {
  45131. this.opQueue.push(new TcbExpressionOp(this.tcb, this, placeholder.value));
  45132. }
  45133. }
  45134. }
  45135. appendContentProjectionCheckOp(root) {
  45136. const meta = this.tcb.boundTarget.getDirectivesOfNode(root)?.find((meta) => meta.isComponent) || null;
  45137. if (meta !== null && meta.ngContentSelectors !== null && meta.ngContentSelectors.length > 0) {
  45138. const selectors = meta.ngContentSelectors;
  45139. // We don't need to generate anything for components that don't have projection
  45140. // slots, or they only have one catch-all slot (represented by `*`).
  45141. if (selectors.length > 1 || (selectors.length === 1 && selectors[0] !== '*')) {
  45142. this.opQueue.push(new TcbControlFlowContentProjectionOp(this.tcb, root, selectors, meta.name));
  45143. }
  45144. }
  45145. }
  45146. appendDeferredBlock(block) {
  45147. this.appendDeferredTriggers(block, block.triggers);
  45148. this.appendDeferredTriggers(block, block.prefetchTriggers);
  45149. // Only the `when` hydration trigger needs to be checked.
  45150. if (block.hydrateTriggers.when) {
  45151. this.opQueue.push(new TcbExpressionOp(this.tcb, this, block.hydrateTriggers.when.value));
  45152. }
  45153. this.appendChildren(block);
  45154. if (block.placeholder !== null) {
  45155. this.appendChildren(block.placeholder);
  45156. }
  45157. if (block.loading !== null) {
  45158. this.appendChildren(block.loading);
  45159. }
  45160. if (block.error !== null) {
  45161. this.appendChildren(block.error);
  45162. }
  45163. }
  45164. appendDeferredTriggers(block, triggers) {
  45165. if (triggers.when !== undefined) {
  45166. this.opQueue.push(new TcbExpressionOp(this.tcb, this, triggers.when.value));
  45167. }
  45168. if (triggers.hover !== undefined) {
  45169. this.appendReferenceBasedDeferredTrigger(block, triggers.hover);
  45170. }
  45171. if (triggers.interaction !== undefined) {
  45172. this.appendReferenceBasedDeferredTrigger(block, triggers.interaction);
  45173. }
  45174. if (triggers.viewport !== undefined) {
  45175. this.appendReferenceBasedDeferredTrigger(block, triggers.viewport);
  45176. }
  45177. }
  45178. appendReferenceBasedDeferredTrigger(block, trigger) {
  45179. if (this.tcb.boundTarget.getDeferredTriggerTarget(block, trigger) === null) {
  45180. this.tcb.oobRecorder.inaccessibleDeferredTriggerElement(this.tcb.id, trigger);
  45181. }
  45182. }
  45183. /** Reports a diagnostic if there are any `@let` declarations that conflict with a node. */
  45184. static checkConflictingLet(scope, node) {
  45185. if (scope.letDeclOpMap.has(node.name)) {
  45186. scope.tcb.oobRecorder.conflictingDeclaration(scope.tcb.id, scope.letDeclOpMap.get(node.name).node);
  45187. }
  45188. }
  45189. }
  45190. /**
  45191. * Create the `this` parameter to the top-level TCB function, with the given generic type
  45192. * arguments.
  45193. */
  45194. function tcbThisParam(name, typeArguments) {
  45195. return ts.factory.createParameterDeclaration(
  45196. /* modifiers */ undefined,
  45197. /* dotDotDotToken */ undefined,
  45198. /* name */ 'this',
  45199. /* questionToken */ undefined,
  45200. /* type */ ts.factory.createTypeReferenceNode(name, typeArguments),
  45201. /* initializer */ undefined);
  45202. }
  45203. /**
  45204. * Process an `AST` expression and convert it into a `ts.Expression`, generating references to the
  45205. * correct identifiers in the current scope.
  45206. */
  45207. function tcbExpression(ast, tcb, scope) {
  45208. const translator = new TcbExpressionTranslator(tcb, scope);
  45209. return translator.translate(ast);
  45210. }
  45211. class TcbExpressionTranslator {
  45212. tcb;
  45213. scope;
  45214. constructor(tcb, scope) {
  45215. this.tcb = tcb;
  45216. this.scope = scope;
  45217. }
  45218. translate(ast) {
  45219. // `astToTypescript` actually does the conversion. A special resolver `tcbResolve` is passed
  45220. // which interprets specific expression nodes that interact with the `ImplicitReceiver`. These
  45221. // nodes actually refer to identifiers within the current scope.
  45222. return astToTypescript(ast, (ast) => this.resolve(ast), this.tcb.env.config);
  45223. }
  45224. /**
  45225. * Resolve an `AST` expression within the given scope.
  45226. *
  45227. * Some `AST` expressions refer to top-level concepts (references, variables, the component
  45228. * context). This method assists in resolving those.
  45229. */
  45230. resolve(ast) {
  45231. if (ast instanceof PropertyRead &&
  45232. ast.receiver instanceof ImplicitReceiver &&
  45233. !(ast.receiver instanceof ThisReceiver)) {
  45234. // Try to resolve a bound target for this expression. If no such target is available, then
  45235. // the expression is referencing the top-level component context. In that case, `null` is
  45236. // returned here to let it fall through resolution so it will be caught when the
  45237. // `ImplicitReceiver` is resolved in the branch below.
  45238. const target = this.tcb.boundTarget.getExpressionTarget(ast);
  45239. const targetExpression = target === null ? null : this.getTargetNodeExpression(target, ast);
  45240. if (target instanceof LetDeclaration$1 &&
  45241. !this.isValidLetDeclarationAccess(target, ast)) {
  45242. this.tcb.oobRecorder.letUsedBeforeDefinition(this.tcb.id, ast, target);
  45243. // Cast the expression to `any` so we don't produce additional diagnostics.
  45244. // We don't use `markIgnoreForDiagnostics` here, because it won't prevent duplicate
  45245. // diagnostics for nested accesses in cases like `@let value = value.foo.bar.baz`.
  45246. if (targetExpression !== null) {
  45247. return ts.factory.createAsExpression(targetExpression, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  45248. }
  45249. }
  45250. return targetExpression;
  45251. }
  45252. else if (ast instanceof PropertyWrite && ast.receiver instanceof ImplicitReceiver) {
  45253. const target = this.tcb.boundTarget.getExpressionTarget(ast);
  45254. if (target === null) {
  45255. return null;
  45256. }
  45257. const targetExpression = this.getTargetNodeExpression(target, ast);
  45258. const expr = this.translate(ast.value);
  45259. const result = ts.factory.createParenthesizedExpression(ts.factory.createBinaryExpression(targetExpression, ts.SyntaxKind.EqualsToken, expr));
  45260. addParseSpanInfo(result, ast.sourceSpan);
  45261. // Ignore diagnostics from TS produced for writes to `@let` and re-report them using
  45262. // our own infrastructure. We can't rely on the TS reporting, because it includes
  45263. // the name of the auto-generated TCB variable name.
  45264. if (target instanceof LetDeclaration$1) {
  45265. markIgnoreDiagnostics(result);
  45266. this.tcb.oobRecorder.illegalWriteToLetDeclaration(this.tcb.id, ast, target);
  45267. }
  45268. return result;
  45269. }
  45270. else if (ast instanceof ImplicitReceiver) {
  45271. // AST instances representing variables and references look very similar to property reads
  45272. // or method calls from the component context: both have the shape
  45273. // PropertyRead(ImplicitReceiver, 'propName') or Call(ImplicitReceiver, 'methodName').
  45274. //
  45275. // `translate` will first try to `resolve` the outer PropertyRead/Call. If this works,
  45276. // it's because the `BoundTarget` found an expression target for the whole expression, and
  45277. // therefore `translate` will never attempt to `resolve` the ImplicitReceiver of that
  45278. // PropertyRead/Call.
  45279. //
  45280. // Therefore if `resolve` is called on an `ImplicitReceiver`, it's because no outer
  45281. // PropertyRead/Call resolved to a variable or reference, and therefore this is a
  45282. // property read or method call on the component context itself.
  45283. return ts.factory.createThis();
  45284. }
  45285. else if (ast instanceof BindingPipe) {
  45286. const expr = this.translate(ast.exp);
  45287. const pipeMeta = this.tcb.getPipeByName(ast.name);
  45288. let pipe;
  45289. if (pipeMeta === null) {
  45290. // No pipe by that name exists in scope. Record this as an error.
  45291. this.tcb.oobRecorder.missingPipe(this.tcb.id, ast);
  45292. // Use an 'any' value to at least allow the rest of the expression to be checked.
  45293. pipe = ANY_EXPRESSION;
  45294. }
  45295. else if (pipeMeta.isExplicitlyDeferred &&
  45296. this.tcb.boundTarget.getEagerlyUsedPipes().includes(ast.name)) {
  45297. // This pipe was defer-loaded (included into `@Component.deferredImports`),
  45298. // but was used outside of a `@defer` block, which is the error.
  45299. this.tcb.oobRecorder.deferredPipeUsedEagerly(this.tcb.id, ast);
  45300. // Use an 'any' value to at least allow the rest of the expression to be checked.
  45301. pipe = ANY_EXPRESSION;
  45302. }
  45303. else {
  45304. // Use a variable declared as the pipe's type.
  45305. pipe = this.tcb.env.pipeInst(pipeMeta.ref);
  45306. }
  45307. const args = ast.args.map((arg) => this.translate(arg));
  45308. let methodAccess = ts.factory.createPropertyAccessExpression(pipe, 'transform');
  45309. addParseSpanInfo(methodAccess, ast.nameSpan);
  45310. if (!this.tcb.env.config.checkTypeOfPipes) {
  45311. methodAccess = ts.factory.createAsExpression(methodAccess, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  45312. }
  45313. const result = ts.factory.createCallExpression(
  45314. /* expression */ methodAccess,
  45315. /* typeArguments */ undefined,
  45316. /* argumentsArray */ [expr, ...args]);
  45317. addParseSpanInfo(result, ast.sourceSpan);
  45318. return result;
  45319. }
  45320. else if ((ast instanceof Call || ast instanceof SafeCall) &&
  45321. (ast.receiver instanceof PropertyRead || ast.receiver instanceof SafePropertyRead)) {
  45322. // Resolve the special `$any(expr)` syntax to insert a cast of the argument to type `any`.
  45323. // `$any(expr)` -> `expr as any`
  45324. if (ast.receiver.receiver instanceof ImplicitReceiver &&
  45325. !(ast.receiver.receiver instanceof ThisReceiver) &&
  45326. ast.receiver.name === '$any' &&
  45327. ast.args.length === 1) {
  45328. const expr = this.translate(ast.args[0]);
  45329. const exprAsAny = ts.factory.createAsExpression(expr, ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
  45330. const result = ts.factory.createParenthesizedExpression(exprAsAny);
  45331. addParseSpanInfo(result, ast.sourceSpan);
  45332. return result;
  45333. }
  45334. // Attempt to resolve a bound target for the method, and generate the method call if a target
  45335. // could be resolved. If no target is available, then the method is referencing the top-level
  45336. // component context, in which case `null` is returned to let the `ImplicitReceiver` being
  45337. // resolved to the component context.
  45338. const target = this.tcb.boundTarget.getExpressionTarget(ast);
  45339. if (target === null) {
  45340. return null;
  45341. }
  45342. const receiver = this.getTargetNodeExpression(target, ast);
  45343. const method = wrapForDiagnostics(receiver);
  45344. addParseSpanInfo(method, ast.receiver.nameSpan);
  45345. const args = ast.args.map((arg) => this.translate(arg));
  45346. const node = ts.factory.createCallExpression(method, undefined, args);
  45347. addParseSpanInfo(node, ast.sourceSpan);
  45348. return node;
  45349. }
  45350. else {
  45351. // This AST isn't special after all.
  45352. return null;
  45353. }
  45354. }
  45355. getTargetNodeExpression(targetNode, expressionNode) {
  45356. const expr = this.scope.resolve(targetNode);
  45357. addParseSpanInfo(expr, expressionNode.sourceSpan);
  45358. return expr;
  45359. }
  45360. isValidLetDeclarationAccess(target, ast) {
  45361. const targetStart = target.sourceSpan.start.offset;
  45362. const targetEnd = target.sourceSpan.end.offset;
  45363. const astStart = ast.sourceSpan.start;
  45364. // We only flag local references that occur before the declaration, because embedded views
  45365. // are updated before the child views. In practice this means that something like
  45366. // `<ng-template [ngIf]="true">{{value}}</ng-template> @let value = 1;` is valid.
  45367. return (targetStart < astStart && astStart > targetEnd) || !this.scope.isLocal(target);
  45368. }
  45369. }
  45370. /**
  45371. * Call the type constructor of a directive instance on a given template node, inferring a type for
  45372. * the directive instance from any bound inputs.
  45373. */
  45374. function tcbCallTypeCtor(dir, tcb, inputs) {
  45375. const typeCtor = tcb.env.typeCtorFor(dir);
  45376. // Construct an array of `ts.PropertyAssignment`s for each of the directive's inputs.
  45377. const members = inputs.map((input) => {
  45378. const propertyName = ts.factory.createStringLiteral(input.field);
  45379. if (input.type === 'binding') {
  45380. // For bound inputs, the property is assigned the binding expression.
  45381. let expr = widenBinding(input.expression, tcb);
  45382. if (input.isTwoWayBinding && tcb.env.config.allowSignalsInTwoWayBindings) {
  45383. expr = unwrapWritableSignal(expr, tcb);
  45384. }
  45385. const assignment = ts.factory.createPropertyAssignment(propertyName, wrapForDiagnostics(expr));
  45386. addParseSpanInfo(assignment, input.sourceSpan);
  45387. return assignment;
  45388. }
  45389. else {
  45390. // A type constructor is required to be called with all input properties, so any unset
  45391. // inputs are simply assigned a value of type `any` to ignore them.
  45392. return ts.factory.createPropertyAssignment(propertyName, ANY_EXPRESSION);
  45393. }
  45394. });
  45395. // Call the `ngTypeCtor` method on the directive class, with an object literal argument created
  45396. // from the matched inputs.
  45397. return ts.factory.createCallExpression(
  45398. /* expression */ typeCtor,
  45399. /* typeArguments */ undefined,
  45400. /* argumentsArray */ [ts.factory.createObjectLiteralExpression(members)]);
  45401. }
  45402. function getBoundAttributes(directive, node) {
  45403. const boundInputs = [];
  45404. const processAttribute = (attr) => {
  45405. // Skip non-property bindings.
  45406. if (attr instanceof BoundAttribute &&
  45407. attr.type !== exports.BindingType.Property &&
  45408. attr.type !== exports.BindingType.TwoWay) {
  45409. return;
  45410. }
  45411. // Skip the attribute if the directive does not have an input for it.
  45412. const inputs = directive.inputs.getByBindingPropertyName(attr.name);
  45413. if (inputs !== null) {
  45414. boundInputs.push({
  45415. attribute: attr,
  45416. inputs: inputs.map((input) => {
  45417. return {
  45418. fieldName: input.classPropertyName,
  45419. required: input.required,
  45420. transformType: input.transform?.type || null,
  45421. isSignal: input.isSignal,
  45422. isTwoWayBinding: attr instanceof BoundAttribute && attr.type === exports.BindingType.TwoWay,
  45423. };
  45424. }),
  45425. });
  45426. }
  45427. };
  45428. node.inputs.forEach(processAttribute);
  45429. node.attributes.forEach(processAttribute);
  45430. if (node instanceof Template) {
  45431. node.templateAttrs.forEach(processAttribute);
  45432. }
  45433. return boundInputs;
  45434. }
  45435. /**
  45436. * Translates the given attribute binding to a `ts.Expression`.
  45437. */
  45438. function translateInput(attr, tcb, scope) {
  45439. if (attr instanceof BoundAttribute) {
  45440. // Produce an expression representing the value of the binding.
  45441. return tcbExpression(attr.value, tcb, scope);
  45442. }
  45443. else {
  45444. // For regular attributes with a static string value, use the represented string literal.
  45445. return ts.factory.createStringLiteral(attr.value);
  45446. }
  45447. }
  45448. /**
  45449. * Potentially widens the type of `expr` according to the type-checking configuration.
  45450. */
  45451. function widenBinding(expr, tcb) {
  45452. if (!tcb.env.config.checkTypeOfInputBindings) {
  45453. // If checking the type of bindings is disabled, cast the resulting expression to 'any'
  45454. // before the assignment.
  45455. return tsCastToAny(expr);
  45456. }
  45457. else if (!tcb.env.config.strictNullInputBindings) {
  45458. if (ts.isObjectLiteralExpression(expr) || ts.isArrayLiteralExpression(expr)) {
  45459. // Object literals and array literals should not be wrapped in non-null assertions as that
  45460. // would cause literals to be prematurely widened, resulting in type errors when assigning
  45461. // into a literal type.
  45462. return expr;
  45463. }
  45464. else {
  45465. // If strict null checks are disabled, erase `null` and `undefined` from the type by
  45466. // wrapping the expression in a non-null assertion.
  45467. return ts.factory.createNonNullExpression(expr);
  45468. }
  45469. }
  45470. else {
  45471. // No widening is requested, use the expression as is.
  45472. return expr;
  45473. }
  45474. }
  45475. /**
  45476. * Wraps an expression in an `unwrapSignal` call which extracts the signal's value.
  45477. */
  45478. function unwrapWritableSignal(expression, tcb) {
  45479. const unwrapRef = tcb.env.referenceExternalSymbol(Identifiers.unwrapWritableSignal.moduleName, Identifiers.unwrapWritableSignal.name);
  45480. return ts.factory.createCallExpression(unwrapRef, undefined, [expression]);
  45481. }
  45482. const EVENT_PARAMETER = '$event';
  45483. /**
  45484. * Creates an arrow function to be used as handler function for event bindings. The handler
  45485. * function has a single parameter `$event` and the bound event's handler `AST` represented as a
  45486. * TypeScript expression as its body.
  45487. *
  45488. * When `eventType` is set to `Infer`, the `$event` parameter will not have an explicit type. This
  45489. * allows for the created handler function to have its `$event` parameter's type inferred based on
  45490. * how it's used, to enable strict type checking of event bindings. When set to `Any`, the `$event`
  45491. * parameter will have an explicit `any` type, effectively disabling strict type checking of event
  45492. * bindings. Alternatively, an explicit type can be passed for the `$event` parameter.
  45493. */
  45494. function tcbCreateEventHandler(event, tcb, scope, eventType) {
  45495. const handler = tcbEventHandlerExpression(event.handler, tcb, scope);
  45496. const statements = [];
  45497. // TODO(crisbeto): remove the `checkTwoWayBoundEvents` check in v20.
  45498. if (event.type === exports.ParsedEventType.TwoWay && tcb.env.config.checkTwoWayBoundEvents) {
  45499. // If we're dealing with a two-way event, we create a variable initialized to the unwrapped
  45500. // signal value of the expression and then we assign `$event` to it. Note that in most cases
  45501. // this will already be covered by the corresponding input binding, however it allows us to
  45502. // handle the case where the input has a wider type than the output (see #58971).
  45503. const target = tcb.allocateId();
  45504. const assignment = ts.factory.createBinaryExpression(target, ts.SyntaxKind.EqualsToken, ts.factory.createIdentifier(EVENT_PARAMETER));
  45505. statements.push(tsCreateVariable(target, tcb.env.config.allowSignalsInTwoWayBindings ? unwrapWritableSignal(handler, tcb) : handler), ts.factory.createExpressionStatement(assignment));
  45506. }
  45507. else {
  45508. statements.push(ts.factory.createExpressionStatement(handler));
  45509. }
  45510. let eventParamType;
  45511. if (eventType === 0 /* EventParamType.Infer */) {
  45512. eventParamType = undefined;
  45513. }
  45514. else if (eventType === 1 /* EventParamType.Any */) {
  45515. eventParamType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword);
  45516. }
  45517. else {
  45518. eventParamType = eventType;
  45519. }
  45520. // Obtain all guards that have been applied to the scope and its parents, as they have to be
  45521. // repeated within the handler function for their narrowing to be in effect within the handler.
  45522. const guards = scope.guards();
  45523. let body = ts.factory.createBlock(statements);
  45524. if (guards !== null) {
  45525. // Wrap the body in an `if` statement containing all guards that have to be applied.
  45526. body = ts.factory.createBlock([ts.factory.createIfStatement(guards, body)]);
  45527. }
  45528. const eventParam = ts.factory.createParameterDeclaration(
  45529. /* modifiers */ undefined,
  45530. /* dotDotDotToken */ undefined,
  45531. /* name */ EVENT_PARAMETER,
  45532. /* questionToken */ undefined,
  45533. /* type */ eventParamType);
  45534. addExpressionIdentifier(eventParam, ExpressionIdentifier.EVENT_PARAMETER);
  45535. // Return an arrow function instead of a function expression to preserve the `this` context.
  45536. return ts.factory.createArrowFunction(
  45537. /* modifiers */ undefined,
  45538. /* typeParameters */ undefined,
  45539. /* parameters */ [eventParam],
  45540. /* type */ ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword),
  45541. /* equalsGreaterThanToken */ undefined,
  45542. /* body */ body);
  45543. }
  45544. /**
  45545. * Similar to `tcbExpression`, this function converts the provided `AST` expression into a
  45546. * `ts.Expression`, with special handling of the `$event` variable that can be used within event
  45547. * bindings.
  45548. */
  45549. function tcbEventHandlerExpression(ast, tcb, scope) {
  45550. const translator = new TcbEventHandlerTranslator(tcb, scope);
  45551. return translator.translate(ast);
  45552. }
  45553. function checkSplitTwoWayBinding(inputName, output, inputs, tcb) {
  45554. const input = inputs.find((input) => input.name === inputName);
  45555. if (input === undefined || input.sourceSpan !== output.sourceSpan) {
  45556. return false;
  45557. }
  45558. // Input consumer should be a directive because it's claimed
  45559. const inputConsumer = tcb.boundTarget.getConsumerOfBinding(input);
  45560. const outputConsumer = tcb.boundTarget.getConsumerOfBinding(output);
  45561. if (outputConsumer === null ||
  45562. inputConsumer.ref === undefined ||
  45563. outputConsumer instanceof Template) {
  45564. return false;
  45565. }
  45566. if (outputConsumer instanceof Element$1) {
  45567. tcb.oobRecorder.splitTwoWayBinding(tcb.id, input, output, inputConsumer.ref.node, outputConsumer);
  45568. return true;
  45569. }
  45570. else if (outputConsumer.ref !== inputConsumer.ref) {
  45571. tcb.oobRecorder.splitTwoWayBinding(tcb.id, input, output, inputConsumer.ref.node, outputConsumer.ref.node);
  45572. return true;
  45573. }
  45574. return false;
  45575. }
  45576. class TcbEventHandlerTranslator extends TcbExpressionTranslator {
  45577. resolve(ast) {
  45578. // Recognize a property read on the implicit receiver corresponding with the event parameter
  45579. // that is available in event bindings. Since this variable is a parameter of the handler
  45580. // function that the converted expression becomes a child of, just create a reference to the
  45581. // parameter by its name.
  45582. if (ast instanceof PropertyRead &&
  45583. ast.receiver instanceof ImplicitReceiver &&
  45584. !(ast.receiver instanceof ThisReceiver) &&
  45585. ast.name === EVENT_PARAMETER) {
  45586. const event = ts.factory.createIdentifier(EVENT_PARAMETER);
  45587. addParseSpanInfo(event, ast.nameSpan);
  45588. return event;
  45589. }
  45590. return super.resolve(ast);
  45591. }
  45592. isValidLetDeclarationAccess() {
  45593. // Event listeners are allowed to read `@let` declarations before
  45594. // they're declared since the callback won't be executed immediately.
  45595. return true;
  45596. }
  45597. }
  45598. class TcbForLoopTrackTranslator extends TcbExpressionTranslator {
  45599. block;
  45600. allowedVariables;
  45601. constructor(tcb, scope, block) {
  45602. super(tcb, scope);
  45603. this.block = block;
  45604. // Tracking expressions are only allowed to read the `$index`,
  45605. // the item and properties off the component instance.
  45606. this.allowedVariables = new Set([block.item]);
  45607. for (const variable of block.contextVariables) {
  45608. if (variable.value === '$index') {
  45609. this.allowedVariables.add(variable);
  45610. }
  45611. }
  45612. }
  45613. resolve(ast) {
  45614. if (ast instanceof PropertyRead && ast.receiver instanceof ImplicitReceiver) {
  45615. const target = this.tcb.boundTarget.getExpressionTarget(ast);
  45616. if (target !== null &&
  45617. (!(target instanceof Variable) || !this.allowedVariables.has(target))) {
  45618. this.tcb.oobRecorder.illegalForLoopTrackAccess(this.tcb.id, this.block, ast);
  45619. }
  45620. }
  45621. return super.resolve(ast);
  45622. }
  45623. }
  45624. /**
  45625. * An `Environment` representing the single type-checking file into which most (if not all) Type
  45626. * Check Blocks (TCBs) will be generated.
  45627. *
  45628. * The `TypeCheckFile` hosts multiple TCBs and allows the sharing of declarations (e.g. type
  45629. * constructors) between them. Rather than return such declarations via `getPreludeStatements()`, it
  45630. * hoists them to the top of the generated `ts.SourceFile`.
  45631. */
  45632. class TypeCheckFile extends Environment {
  45633. fileName;
  45634. nextTcbId = 1;
  45635. tcbStatements = [];
  45636. constructor(fileName, config, refEmitter, reflector, compilerHost) {
  45637. super(config, new ImportManager({
  45638. // This minimizes noticeable changes with older versions of `ImportManager`.
  45639. forceGenerateNamespacesForNewImports: true,
  45640. // Type check block code affects code completion and fix suggestions.
  45641. // We want to encourage single quotes for now, like we always did.
  45642. shouldUseSingleQuotes: () => true,
  45643. }), refEmitter, reflector, ts.createSourceFile(compilerHost.getCanonicalFileName(fileName), '', ts.ScriptTarget.Latest, true));
  45644. this.fileName = fileName;
  45645. }
  45646. addTypeCheckBlock(ref, meta, domSchemaChecker, oobRecorder, genericContextBehavior) {
  45647. const fnId = ts.factory.createIdentifier(`_tcb${this.nextTcbId++}`);
  45648. const fn = generateTypeCheckBlock(this, ref, fnId, meta, domSchemaChecker, oobRecorder, genericContextBehavior);
  45649. this.tcbStatements.push(fn);
  45650. }
  45651. render(removeComments) {
  45652. // NOTE: We are conditionally adding imports whenever we discover signal inputs. This has a
  45653. // risk of changing the import graph of the TypeScript program, degrading incremental program
  45654. // re-use due to program structure changes. For type check block files, we are ensuring an
  45655. // import to e.g. `@angular/core` always exists to guarantee a stable graph.
  45656. ensureTypeCheckFilePreparationImports(this);
  45657. const importChanges = this.importManager.finalize();
  45658. if (importChanges.updatedImports.size > 0) {
  45659. throw new Error('AssertionError: Expected no imports to be updated for a new type check file.');
  45660. }
  45661. const printer = ts.createPrinter({ removeComments });
  45662. let source = '';
  45663. const newImports = importChanges.newImports.get(this.contextFile.fileName);
  45664. if (newImports !== undefined) {
  45665. source += newImports
  45666. .map((i) => printer.printNode(ts.EmitHint.Unspecified, i, this.contextFile))
  45667. .join('\n');
  45668. }
  45669. source += '\n';
  45670. for (const stmt of this.pipeInstStatements) {
  45671. source += printer.printNode(ts.EmitHint.Unspecified, stmt, this.contextFile) + '\n';
  45672. }
  45673. for (const stmt of this.typeCtorStatements) {
  45674. source += printer.printNode(ts.EmitHint.Unspecified, stmt, this.contextFile) + '\n';
  45675. }
  45676. source += '\n';
  45677. for (const stmt of this.tcbStatements) {
  45678. source += printer.printNode(ts.EmitHint.Unspecified, stmt, this.contextFile) + '\n';
  45679. }
  45680. // Ensure the template type-checking file is an ES module. Otherwise, it's interpreted as some
  45681. // kind of global namespace in TS, which forces a full re-typecheck of the user's program that
  45682. // is somehow more expensive than the initial parse.
  45683. source += '\nexport const IS_A_MODULE = true;\n';
  45684. return source;
  45685. }
  45686. getPreludeStatements() {
  45687. return [];
  45688. }
  45689. }
  45690. /**
  45691. * How a type-checking context should handle operations which would require inlining.
  45692. */
  45693. var InliningMode;
  45694. (function (InliningMode) {
  45695. /**
  45696. * Use inlining operations when required.
  45697. */
  45698. InliningMode[InliningMode["InlineOps"] = 0] = "InlineOps";
  45699. /**
  45700. * Produce diagnostics if an operation would require inlining.
  45701. */
  45702. InliningMode[InliningMode["Error"] = 1] = "Error";
  45703. })(InliningMode || (InliningMode = {}));
  45704. /**
  45705. * A template type checking context for a program.
  45706. *
  45707. * The `TypeCheckContext` allows registration of directives to be type checked.
  45708. */
  45709. class TypeCheckContextImpl {
  45710. config;
  45711. compilerHost;
  45712. refEmitter;
  45713. reflector;
  45714. host;
  45715. inlining;
  45716. perf;
  45717. fileMap = new Map();
  45718. constructor(config, compilerHost, refEmitter, reflector, host, inlining, perf) {
  45719. this.config = config;
  45720. this.compilerHost = compilerHost;
  45721. this.refEmitter = refEmitter;
  45722. this.reflector = reflector;
  45723. this.host = host;
  45724. this.inlining = inlining;
  45725. this.perf = perf;
  45726. if (inlining === InliningMode.Error && config.useInlineTypeConstructors) {
  45727. // We cannot use inlining for type checking since this environment does not support it.
  45728. throw new Error(`AssertionError: invalid inlining configuration.`);
  45729. }
  45730. }
  45731. /**
  45732. * A `Map` of `ts.SourceFile`s that the context has seen to the operations (additions of methods
  45733. * or type-check blocks) that need to be eventually performed on that file.
  45734. */
  45735. opMap = new Map();
  45736. /**
  45737. * Tracks when an a particular class has a pending type constructor patching operation already
  45738. * queued.
  45739. */
  45740. typeCtorPending = new Set();
  45741. /**
  45742. * Register a template to potentially be type-checked.
  45743. *
  45744. * Implements `TypeCheckContext.addTemplate`.
  45745. */
  45746. addDirective(ref, binder, schemas, templateContext, isStandalone) {
  45747. if (!this.host.shouldCheckClass(ref.node)) {
  45748. return;
  45749. }
  45750. const fileData = this.dataForFile(ref.node.getSourceFile());
  45751. const shimData = this.pendingShimForClass(ref.node);
  45752. const id = fileData.sourceManager.getTypeCheckId(ref.node);
  45753. const templateParsingDiagnostics = [];
  45754. if (templateContext !== null && templateContext.parseErrors !== null) {
  45755. templateParsingDiagnostics.push(...getTemplateDiagnostics(templateContext.parseErrors, id, templateContext.sourceMapping));
  45756. }
  45757. const boundTarget = binder.bind({ template: templateContext?.nodes });
  45758. if (this.inlining === InliningMode.InlineOps) {
  45759. // Get all of the directives used in the template and record inline type constructors when
  45760. // required.
  45761. for (const dir of boundTarget.getUsedDirectives()) {
  45762. const dirRef = dir.ref;
  45763. const dirNode = dirRef.node;
  45764. if (!dir.isGeneric || !requiresInlineTypeCtor(dirNode, this.reflector, shimData.file)) {
  45765. // inlining not required
  45766. continue;
  45767. }
  45768. // Add an inline type constructor operation for the directive.
  45769. this.addInlineTypeCtor(fileData, dirNode.getSourceFile(), dirRef, {
  45770. fnName: 'ngTypeCtor',
  45771. // The constructor should have a body if the directive comes from a .ts file, but not if
  45772. // it comes from a .d.ts file. .d.ts declarations don't have bodies.
  45773. body: !dirNode.getSourceFile().isDeclarationFile,
  45774. fields: {
  45775. inputs: dir.inputs,
  45776. // TODO(alxhub): support queries
  45777. queries: dir.queries,
  45778. },
  45779. coercedInputFields: dir.coercedInputFields,
  45780. });
  45781. }
  45782. }
  45783. shimData.data.set(id, {
  45784. template: templateContext?.nodes || null,
  45785. boundTarget,
  45786. templateParsingDiagnostics,
  45787. });
  45788. const usedPipes = [];
  45789. if (templateContext !== null) {
  45790. for (const name of boundTarget.getUsedPipes()) {
  45791. if (templateContext.pipes.has(name)) {
  45792. usedPipes.push(templateContext.pipes.get(name).ref);
  45793. }
  45794. }
  45795. }
  45796. const inliningRequirement = requiresInlineTypeCheckBlock(ref, shimData.file, usedPipes, this.reflector);
  45797. // If inlining is not supported, but is required for either the TCB or one of its directive
  45798. // dependencies, then exit here with an error.
  45799. if (this.inlining === InliningMode.Error &&
  45800. inliningRequirement === TcbInliningRequirement.MustInline) {
  45801. // This template cannot be supported because the underlying strategy does not support inlining
  45802. // and inlining would be required.
  45803. // Record diagnostics to indicate the issues with this template.
  45804. shimData.oobRecorder.requiresInlineTcb(id, ref.node);
  45805. // Checking this template would be unsupported, so don't try.
  45806. this.perf.eventCount(exports.PerfEvent.SkipGenerateTcbNoInline);
  45807. return;
  45808. }
  45809. if (templateContext !== null) {
  45810. fileData.sourceManager.captureTemplateSource(id, templateContext.sourceMapping, templateContext.file);
  45811. }
  45812. const meta = {
  45813. id,
  45814. boundTarget,
  45815. pipes: templateContext?.pipes || null,
  45816. schemas,
  45817. isStandalone,
  45818. preserveWhitespaces: templateContext?.preserveWhitespaces ?? false,
  45819. };
  45820. this.perf.eventCount(exports.PerfEvent.GenerateTcb);
  45821. if (inliningRequirement !== TcbInliningRequirement.None &&
  45822. this.inlining === InliningMode.InlineOps) {
  45823. // This class didn't meet the requirements for external type checking, so generate an inline
  45824. // TCB for the class.
  45825. this.addInlineTypeCheckBlock(fileData, shimData, ref, meta);
  45826. }
  45827. else if (inliningRequirement === TcbInliningRequirement.ShouldInlineForGenericBounds &&
  45828. this.inlining === InliningMode.Error) {
  45829. // It's suggested that this TCB should be generated inline due to the class' generic
  45830. // bounds, but inlining is not supported by the current environment. Use a non-inline type
  45831. // check block, but fall back to `any` generic parameters since the generic bounds can't be
  45832. // referenced in that context. This will infer a less useful type for the class, but allow
  45833. // for type-checking it in an environment where that would not be possible otherwise.
  45834. shimData.file.addTypeCheckBlock(ref, meta, shimData.domSchemaChecker, shimData.oobRecorder, TcbGenericContextBehavior.FallbackToAny);
  45835. }
  45836. else {
  45837. shimData.file.addTypeCheckBlock(ref, meta, shimData.domSchemaChecker, shimData.oobRecorder, TcbGenericContextBehavior.UseEmitter);
  45838. }
  45839. }
  45840. /**
  45841. * Record a type constructor for the given `node` with the given `ctorMetadata`.
  45842. */
  45843. addInlineTypeCtor(fileData, sf, ref, ctorMeta) {
  45844. if (this.typeCtorPending.has(ref.node)) {
  45845. return;
  45846. }
  45847. this.typeCtorPending.add(ref.node);
  45848. // Lazily construct the operation map.
  45849. if (!this.opMap.has(sf)) {
  45850. this.opMap.set(sf, []);
  45851. }
  45852. const ops = this.opMap.get(sf);
  45853. // Push a `TypeCtorOp` into the operation queue for the source file.
  45854. ops.push(new TypeCtorOp(ref, this.reflector, ctorMeta));
  45855. fileData.hasInlines = true;
  45856. }
  45857. /**
  45858. * Transform a `ts.SourceFile` into a version that includes type checking code.
  45859. *
  45860. * If this particular `ts.SourceFile` requires changes, the text representing its new contents
  45861. * will be returned. Otherwise, a `null` return indicates no changes were necessary.
  45862. */
  45863. transform(sf) {
  45864. // If there are no operations pending for this particular file, return `null` to indicate no
  45865. // changes.
  45866. if (!this.opMap.has(sf)) {
  45867. return null;
  45868. }
  45869. // Use a `ts.Printer` to generate source code.
  45870. const printer = ts.createPrinter({ omitTrailingSemicolon: true });
  45871. // Imports may need to be added to the file to support type-checking of directives
  45872. // used in the template within it.
  45873. const importManager = new ImportManager({
  45874. // This minimizes noticeable changes with older versions of `ImportManager`.
  45875. forceGenerateNamespacesForNewImports: true,
  45876. // Type check block code affects code completion and fix suggestions.
  45877. // We want to encourage single quotes for now, like we always did.
  45878. shouldUseSingleQuotes: () => true,
  45879. });
  45880. // Execute ops.
  45881. // Each Op has a splitPoint index into the text where it needs to be inserted.
  45882. const updates = this.opMap
  45883. .get(sf)
  45884. .map((op) => {
  45885. return {
  45886. pos: op.splitPoint,
  45887. text: op.execute(importManager, sf, this.refEmitter, printer),
  45888. };
  45889. });
  45890. const { newImports, updatedImports } = importManager.finalize();
  45891. // Capture new imports
  45892. if (newImports.has(sf.fileName)) {
  45893. newImports.get(sf.fileName).forEach((newImport) => {
  45894. updates.push({
  45895. pos: 0,
  45896. text: printer.printNode(ts.EmitHint.Unspecified, newImport, sf),
  45897. });
  45898. });
  45899. }
  45900. // Capture updated imports
  45901. for (const [oldBindings, newBindings] of updatedImports.entries()) {
  45902. if (oldBindings.getSourceFile() !== sf) {
  45903. throw new Error('Unexpected updates to unrelated source files.');
  45904. }
  45905. updates.push({
  45906. pos: oldBindings.getStart(),
  45907. deletePos: oldBindings.getEnd(),
  45908. text: printer.printNode(ts.EmitHint.Unspecified, newBindings, sf),
  45909. });
  45910. }
  45911. const result = new MagicString(sf.text, { filename: sf.fileName });
  45912. for (const update of updates) {
  45913. if (update.deletePos !== undefined) {
  45914. result.remove(update.pos, update.deletePos);
  45915. }
  45916. result.appendLeft(update.pos, update.text);
  45917. }
  45918. return result.toString();
  45919. }
  45920. finalize() {
  45921. // First, build the map of updates to source files.
  45922. const updates = new Map();
  45923. for (const originalSf of this.opMap.keys()) {
  45924. const newText = this.transform(originalSf);
  45925. if (newText !== null) {
  45926. updates.set(absoluteFromSourceFile(originalSf), {
  45927. newText,
  45928. originalFile: originalSf,
  45929. });
  45930. }
  45931. }
  45932. // Then go through each input file that has pending code generation operations.
  45933. for (const [sfPath, pendingFileData] of this.fileMap) {
  45934. // For each input file, consider generation operations for each of its shims.
  45935. for (const pendingShimData of pendingFileData.shimData.values()) {
  45936. this.host.recordShimData(sfPath, {
  45937. genesisDiagnostics: [
  45938. ...pendingShimData.domSchemaChecker.diagnostics,
  45939. ...pendingShimData.oobRecorder.diagnostics,
  45940. ],
  45941. hasInlines: pendingFileData.hasInlines,
  45942. path: pendingShimData.file.fileName,
  45943. data: pendingShimData.data,
  45944. });
  45945. const sfText = pendingShimData.file.render(false /* removeComments */);
  45946. updates.set(pendingShimData.file.fileName, {
  45947. newText: sfText,
  45948. // Shim files do not have an associated original file.
  45949. originalFile: null,
  45950. });
  45951. }
  45952. }
  45953. return updates;
  45954. }
  45955. addInlineTypeCheckBlock(fileData, shimData, ref, tcbMeta) {
  45956. const sf = ref.node.getSourceFile();
  45957. if (!this.opMap.has(sf)) {
  45958. this.opMap.set(sf, []);
  45959. }
  45960. const ops = this.opMap.get(sf);
  45961. ops.push(new InlineTcbOp(ref, tcbMeta, this.config, this.reflector, shimData.domSchemaChecker, shimData.oobRecorder));
  45962. fileData.hasInlines = true;
  45963. }
  45964. pendingShimForClass(node) {
  45965. const fileData = this.dataForFile(node.getSourceFile());
  45966. const shimPath = TypeCheckShimGenerator.shimFor(absoluteFromSourceFile(node.getSourceFile()));
  45967. if (!fileData.shimData.has(shimPath)) {
  45968. fileData.shimData.set(shimPath, {
  45969. domSchemaChecker: new RegistryDomSchemaChecker(fileData.sourceManager),
  45970. oobRecorder: new OutOfBandDiagnosticRecorderImpl(fileData.sourceManager),
  45971. file: new TypeCheckFile(shimPath, this.config, this.refEmitter, this.reflector, this.compilerHost),
  45972. data: new Map(),
  45973. });
  45974. }
  45975. return fileData.shimData.get(shimPath);
  45976. }
  45977. dataForFile(sf) {
  45978. const sfPath = absoluteFromSourceFile(sf);
  45979. if (!this.fileMap.has(sfPath)) {
  45980. const data = {
  45981. hasInlines: false,
  45982. sourceManager: this.host.getSourceManager(sfPath),
  45983. shimData: new Map(),
  45984. };
  45985. this.fileMap.set(sfPath, data);
  45986. }
  45987. return this.fileMap.get(sfPath);
  45988. }
  45989. }
  45990. function getTemplateDiagnostics(parseErrors, templateId, sourceMapping) {
  45991. return parseErrors.map((error) => {
  45992. const span = error.span;
  45993. if (span.start.offset === span.end.offset) {
  45994. // Template errors can contain zero-length spans, if the error occurs at a single point.
  45995. // However, TypeScript does not handle displaying a zero-length diagnostic very well, so
  45996. // increase the ending offset by 1 for such errors, to ensure the position is shown in the
  45997. // diagnostic.
  45998. span.end.offset++;
  45999. }
  46000. return makeTemplateDiagnostic(templateId, sourceMapping, span, ts.DiagnosticCategory.Error, ngErrorCode(exports.ErrorCode.TEMPLATE_PARSE_ERROR), error.msg);
  46001. });
  46002. }
  46003. /**
  46004. * A type check block operation which produces inline type check code for a particular directive.
  46005. */
  46006. class InlineTcbOp {
  46007. ref;
  46008. meta;
  46009. config;
  46010. reflector;
  46011. domSchemaChecker;
  46012. oobRecorder;
  46013. constructor(ref, meta, config, reflector, domSchemaChecker, oobRecorder) {
  46014. this.ref = ref;
  46015. this.meta = meta;
  46016. this.config = config;
  46017. this.reflector = reflector;
  46018. this.domSchemaChecker = domSchemaChecker;
  46019. this.oobRecorder = oobRecorder;
  46020. }
  46021. /**
  46022. * Type check blocks are inserted immediately after the end of the directve class.
  46023. */
  46024. get splitPoint() {
  46025. return this.ref.node.end + 1;
  46026. }
  46027. execute(im, sf, refEmitter, printer) {
  46028. const env = new Environment(this.config, im, refEmitter, this.reflector, sf);
  46029. const fnName = ts.factory.createIdentifier(`_tcb_${this.ref.node.pos}`);
  46030. // Inline TCBs should copy any generic type parameter nodes directly, as the TCB code is
  46031. // inlined into the class in a context where that will always be legal.
  46032. const fn = generateTypeCheckBlock(env, this.ref, fnName, this.meta, this.domSchemaChecker, this.oobRecorder, TcbGenericContextBehavior.CopyClassNodes);
  46033. return printer.printNode(ts.EmitHint.Unspecified, fn, sf);
  46034. }
  46035. }
  46036. /**
  46037. * A type constructor operation which produces type constructor code for a particular directive.
  46038. */
  46039. class TypeCtorOp {
  46040. ref;
  46041. reflector;
  46042. meta;
  46043. constructor(ref, reflector, meta) {
  46044. this.ref = ref;
  46045. this.reflector = reflector;
  46046. this.meta = meta;
  46047. }
  46048. /**
  46049. * Type constructor operations are inserted immediately before the end of the directive class.
  46050. */
  46051. get splitPoint() {
  46052. return this.ref.node.end - 1;
  46053. }
  46054. execute(im, sf, refEmitter, printer) {
  46055. const emitEnv = new ReferenceEmitEnvironment(im, refEmitter, this.reflector, sf);
  46056. const tcb = generateInlineTypeCtor(emitEnv, this.ref.node, this.meta);
  46057. return printer.printNode(ts.EmitHint.Unspecified, tcb, sf);
  46058. }
  46059. }
  46060. const LF_CHAR = 10;
  46061. const CR_CHAR = 13;
  46062. const LINE_SEP_CHAR = 8232;
  46063. const PARAGRAPH_CHAR = 8233;
  46064. /** Gets the line and character for the given position from the line starts map. */
  46065. function getLineAndCharacterFromPosition(lineStartsMap, position) {
  46066. const lineIndex = findClosestLineStartPosition(lineStartsMap, position);
  46067. return { character: position - lineStartsMap[lineIndex], line: lineIndex };
  46068. }
  46069. /**
  46070. * Computes the line start map of the given text. This can be used in order to
  46071. * retrieve the line and character of a given text position index.
  46072. */
  46073. function computeLineStartsMap(text) {
  46074. const result = [0];
  46075. let pos = 0;
  46076. while (pos < text.length) {
  46077. const char = text.charCodeAt(pos++);
  46078. // Handles the "CRLF" line break. In that case we peek the character
  46079. // after the "CR" and check if it is a line feed.
  46080. if (char === CR_CHAR) {
  46081. if (text.charCodeAt(pos) === LF_CHAR) {
  46082. pos++;
  46083. }
  46084. result.push(pos);
  46085. }
  46086. else if (char === LF_CHAR || char === LINE_SEP_CHAR || char === PARAGRAPH_CHAR) {
  46087. result.push(pos);
  46088. }
  46089. }
  46090. result.push(pos);
  46091. return result;
  46092. }
  46093. /** Finds the closest line start for the given position. */
  46094. function findClosestLineStartPosition(linesMap, position, low = 0, high = linesMap.length - 1) {
  46095. while (low <= high) {
  46096. const pivotIdx = Math.floor((low + high) / 2);
  46097. const pivotEl = linesMap[pivotIdx];
  46098. if (pivotEl === position) {
  46099. return pivotIdx;
  46100. }
  46101. else if (position > pivotEl) {
  46102. low = pivotIdx + 1;
  46103. }
  46104. else {
  46105. high = pivotIdx - 1;
  46106. }
  46107. }
  46108. // In case there was no exact match, return the closest "lower" line index. We also
  46109. // subtract the index by one because want the index of the previous line start.
  46110. return low - 1;
  46111. }
  46112. /**
  46113. * Represents the source of a template that was processed during type-checking. This information is
  46114. * used when translating parse offsets in diagnostics back to their original line/column location.
  46115. */
  46116. class TemplateSource {
  46117. mapping;
  46118. file;
  46119. lineStarts = null;
  46120. constructor(mapping, file) {
  46121. this.mapping = mapping;
  46122. this.file = file;
  46123. }
  46124. toParseSourceSpan(start, end) {
  46125. const startLoc = this.toParseLocation(start);
  46126. const endLoc = this.toParseLocation(end);
  46127. return new ParseSourceSpan(startLoc, endLoc);
  46128. }
  46129. toParseLocation(position) {
  46130. const lineStarts = this.acquireLineStarts();
  46131. const { line, character } = getLineAndCharacterFromPosition(lineStarts, position);
  46132. return new ParseLocation(this.file, position, line, character);
  46133. }
  46134. acquireLineStarts() {
  46135. if (this.lineStarts === null) {
  46136. this.lineStarts = computeLineStartsMap(this.file.content);
  46137. }
  46138. return this.lineStarts;
  46139. }
  46140. }
  46141. /**
  46142. * Assigns IDs for type checking and keeps track of their origins.
  46143. *
  46144. * Implements `TypeCheckSourceResolver` to resolve the source of a template based on these IDs.
  46145. */
  46146. class DirectiveSourceManager {
  46147. /**
  46148. * This map keeps track of all template sources that have been type-checked by the id that is
  46149. * attached to a TCB's function declaration as leading trivia. This enables translation of
  46150. * diagnostics produced for TCB code to their source location in the template.
  46151. */
  46152. templateSources = new Map();
  46153. getTypeCheckId(node) {
  46154. return getTypeCheckId(node);
  46155. }
  46156. captureTemplateSource(id, mapping, file) {
  46157. this.templateSources.set(id, new TemplateSource(mapping, file));
  46158. }
  46159. getTemplateSourceMapping(id) {
  46160. if (!this.templateSources.has(id)) {
  46161. throw new Error(`Unexpected unknown type check ID: ${id}`);
  46162. }
  46163. return this.templateSources.get(id).mapping;
  46164. }
  46165. toTemplateParseSourceSpan(id, span) {
  46166. if (!this.templateSources.has(id)) {
  46167. return null;
  46168. }
  46169. const templateSource = this.templateSources.get(id);
  46170. return templateSource.toParseSourceSpan(span.start, span.end);
  46171. }
  46172. }
  46173. /**
  46174. * Generates and caches `Symbol`s for various template structures for a given component.
  46175. *
  46176. * The `SymbolBuilder` internally caches the `Symbol`s it creates, and must be destroyed and
  46177. * replaced if the component's template changes.
  46178. */
  46179. class SymbolBuilder {
  46180. tcbPath;
  46181. tcbIsShim;
  46182. typeCheckBlock;
  46183. typeCheckData;
  46184. componentScopeReader;
  46185. getTypeChecker;
  46186. symbolCache = new Map();
  46187. constructor(tcbPath, tcbIsShim, typeCheckBlock, typeCheckData, componentScopeReader,
  46188. // The `ts.TypeChecker` depends on the current type-checking program, and so must be requested
  46189. // on-demand instead of cached.
  46190. getTypeChecker) {
  46191. this.tcbPath = tcbPath;
  46192. this.tcbIsShim = tcbIsShim;
  46193. this.typeCheckBlock = typeCheckBlock;
  46194. this.typeCheckData = typeCheckData;
  46195. this.componentScopeReader = componentScopeReader;
  46196. this.getTypeChecker = getTypeChecker;
  46197. }
  46198. getSymbol(node) {
  46199. if (this.symbolCache.has(node)) {
  46200. return this.symbolCache.get(node);
  46201. }
  46202. let symbol = null;
  46203. if (node instanceof BoundAttribute || node instanceof TextAttribute) {
  46204. // TODO(atscott): input and output bindings only return the first directive match but should
  46205. // return a list of bindings for all of them.
  46206. symbol = this.getSymbolOfInputBinding(node);
  46207. }
  46208. else if (node instanceof BoundEvent) {
  46209. symbol = this.getSymbolOfBoundEvent(node);
  46210. }
  46211. else if (node instanceof Element$1) {
  46212. symbol = this.getSymbolOfElement(node);
  46213. }
  46214. else if (node instanceof Template) {
  46215. symbol = this.getSymbolOfAstTemplate(node);
  46216. }
  46217. else if (node instanceof Variable) {
  46218. symbol = this.getSymbolOfVariable(node);
  46219. }
  46220. else if (node instanceof LetDeclaration$1) {
  46221. symbol = this.getSymbolOfLetDeclaration(node);
  46222. }
  46223. else if (node instanceof Reference$1) {
  46224. symbol = this.getSymbolOfReference(node);
  46225. }
  46226. else if (node instanceof BindingPipe) {
  46227. symbol = this.getSymbolOfPipe(node);
  46228. }
  46229. else if (node instanceof AST) {
  46230. symbol = this.getSymbolOfTemplateExpression(node);
  46231. }
  46232. else ;
  46233. this.symbolCache.set(node, symbol);
  46234. return symbol;
  46235. }
  46236. getSymbolOfAstTemplate(template) {
  46237. const directives = this.getDirectivesOfNode(template);
  46238. return { kind: exports.SymbolKind.Template, directives, templateNode: template };
  46239. }
  46240. getSymbolOfElement(element) {
  46241. const elementSourceSpan = element.startSourceSpan ?? element.sourceSpan;
  46242. const node = findFirstMatchingNode(this.typeCheckBlock, {
  46243. withSpan: elementSourceSpan,
  46244. filter: ts.isVariableDeclaration,
  46245. });
  46246. if (node === null) {
  46247. return null;
  46248. }
  46249. const symbolFromDeclaration = this.getSymbolOfTsNode(node);
  46250. if (symbolFromDeclaration === null || symbolFromDeclaration.tsSymbol === null) {
  46251. return null;
  46252. }
  46253. const directives = this.getDirectivesOfNode(element);
  46254. // All statements in the TCB are `Expression`s that optionally include more information.
  46255. // An `ElementSymbol` uses the information returned for the variable declaration expression,
  46256. // adds the directives for the element, and updates the `kind` to be `SymbolKind.Element`.
  46257. return {
  46258. ...symbolFromDeclaration,
  46259. kind: exports.SymbolKind.Element,
  46260. directives,
  46261. templateNode: element,
  46262. };
  46263. }
  46264. getDirectivesOfNode(element) {
  46265. const elementSourceSpan = element.startSourceSpan ?? element.sourceSpan;
  46266. const tcbSourceFile = this.typeCheckBlock.getSourceFile();
  46267. // directives could be either:
  46268. // - var _t1: TestDir /*T:D*/ = null! as TestDir;
  46269. // - var _t1 /*T:D*/ = _ctor1({});
  46270. const isDirectiveDeclaration = (node) => (ts.isTypeNode(node) || ts.isIdentifier(node)) &&
  46271. ts.isVariableDeclaration(node.parent) &&
  46272. hasExpressionIdentifier(tcbSourceFile, node, ExpressionIdentifier.DIRECTIVE);
  46273. const nodes = findAllMatchingNodes(this.typeCheckBlock, {
  46274. withSpan: elementSourceSpan,
  46275. filter: isDirectiveDeclaration,
  46276. });
  46277. const symbols = [];
  46278. for (const node of nodes) {
  46279. const symbol = this.getSymbolOfTsNode(node.parent);
  46280. if (symbol === null ||
  46281. !isSymbolWithValueDeclaration(symbol.tsSymbol) ||
  46282. !ts.isClassDeclaration(symbol.tsSymbol.valueDeclaration)) {
  46283. continue;
  46284. }
  46285. const meta = this.getDirectiveMeta(element, symbol.tsSymbol.valueDeclaration);
  46286. if (meta !== null && meta.selector !== null) {
  46287. const ref = new Reference(symbol.tsSymbol.valueDeclaration);
  46288. if (meta.hostDirectives !== null) {
  46289. this.addHostDirectiveSymbols(element, meta.hostDirectives, symbols);
  46290. }
  46291. const directiveSymbol = {
  46292. ...symbol,
  46293. ref,
  46294. tsSymbol: symbol.tsSymbol,
  46295. selector: meta.selector,
  46296. isComponent: meta.isComponent,
  46297. ngModule: this.getDirectiveModule(symbol.tsSymbol.valueDeclaration),
  46298. kind: exports.SymbolKind.Directive,
  46299. isStructural: meta.isStructural,
  46300. isInScope: true,
  46301. isHostDirective: false,
  46302. };
  46303. symbols.push(directiveSymbol);
  46304. }
  46305. }
  46306. return symbols;
  46307. }
  46308. addHostDirectiveSymbols(host, hostDirectives, symbols) {
  46309. for (const current of hostDirectives) {
  46310. if (!isHostDirectiveMetaForGlobalMode(current)) {
  46311. throw new Error('Impossible state: typecheck code path in local compilation mode.');
  46312. }
  46313. if (!ts.isClassDeclaration(current.directive.node)) {
  46314. continue;
  46315. }
  46316. const symbol = this.getSymbolOfTsNode(current.directive.node);
  46317. const meta = this.getDirectiveMeta(host, current.directive.node);
  46318. if (meta !== null && symbol !== null && isSymbolWithValueDeclaration(symbol.tsSymbol)) {
  46319. if (meta.hostDirectives !== null) {
  46320. this.addHostDirectiveSymbols(host, meta.hostDirectives, symbols);
  46321. }
  46322. const directiveSymbol = {
  46323. ...symbol,
  46324. isHostDirective: true,
  46325. ref: current.directive,
  46326. tsSymbol: symbol.tsSymbol,
  46327. exposedInputs: current.inputs,
  46328. exposedOutputs: current.outputs,
  46329. selector: meta.selector,
  46330. isComponent: meta.isComponent,
  46331. ngModule: this.getDirectiveModule(current.directive.node),
  46332. kind: exports.SymbolKind.Directive,
  46333. isStructural: meta.isStructural,
  46334. isInScope: true,
  46335. };
  46336. symbols.push(directiveSymbol);
  46337. }
  46338. }
  46339. }
  46340. getDirectiveMeta(host, directiveDeclaration) {
  46341. let directives = this.typeCheckData.boundTarget.getDirectivesOfNode(host);
  46342. // `getDirectivesOfNode` will not return the directives intended for an element
  46343. // on a microsyntax template, for example `<div *ngFor="let user of users;" dir>`,
  46344. // the `dir` will be skipped, but it's needed in language service.
  46345. const firstChild = host.children[0];
  46346. if (firstChild instanceof Element$1) {
  46347. const isMicrosyntaxTemplate = host instanceof Template && sourceSpanEqual(firstChild.sourceSpan, host.sourceSpan);
  46348. if (isMicrosyntaxTemplate) {
  46349. const firstChildDirectives = this.typeCheckData.boundTarget.getDirectivesOfNode(firstChild);
  46350. if (firstChildDirectives !== null && directives !== null) {
  46351. directives = directives.concat(firstChildDirectives);
  46352. }
  46353. else {
  46354. directives = directives ?? firstChildDirectives;
  46355. }
  46356. }
  46357. }
  46358. if (directives === null) {
  46359. return null;
  46360. }
  46361. return directives.find((m) => m.ref.node === directiveDeclaration) ?? null;
  46362. }
  46363. getDirectiveModule(declaration) {
  46364. const scope = this.componentScopeReader.getScopeForComponent(declaration);
  46365. if (scope === null || scope.kind !== exports.ComponentScopeKind.NgModule) {
  46366. return null;
  46367. }
  46368. return scope.ngModule;
  46369. }
  46370. getSymbolOfBoundEvent(eventBinding) {
  46371. const consumer = this.typeCheckData.boundTarget.getConsumerOfBinding(eventBinding);
  46372. if (consumer === null) {
  46373. return null;
  46374. }
  46375. // Outputs in the TCB look like one of the two:
  46376. // * _t1["outputField"].subscribe(handler);
  46377. // * _t1.addEventListener(handler);
  46378. // Even with strict null checks disabled, we still produce the access as a separate statement
  46379. // so that it can be found here.
  46380. let expectedAccess;
  46381. if (consumer instanceof Template || consumer instanceof Element$1) {
  46382. expectedAccess = 'addEventListener';
  46383. }
  46384. else {
  46385. const bindingPropertyNames = consumer.outputs.getByBindingPropertyName(eventBinding.name);
  46386. if (bindingPropertyNames === null || bindingPropertyNames.length === 0) {
  46387. return null;
  46388. }
  46389. // Note that we only get the expectedAccess text from a single consumer of the binding. If
  46390. // there are multiple consumers (not supported in the `boundTarget` API) and one of them has
  46391. // an alias, it will not get matched here.
  46392. expectedAccess = bindingPropertyNames[0].classPropertyName;
  46393. }
  46394. function filter(n) {
  46395. if (!isAccessExpression(n)) {
  46396. return false;
  46397. }
  46398. if (ts.isPropertyAccessExpression(n)) {
  46399. return n.name.getText() === expectedAccess;
  46400. }
  46401. else {
  46402. return (ts.isStringLiteral(n.argumentExpression) && n.argumentExpression.text === expectedAccess);
  46403. }
  46404. }
  46405. const outputFieldAccesses = findAllMatchingNodes(this.typeCheckBlock, {
  46406. withSpan: eventBinding.keySpan,
  46407. filter,
  46408. });
  46409. const bindings = [];
  46410. for (const outputFieldAccess of outputFieldAccesses) {
  46411. if (consumer instanceof Template || consumer instanceof Element$1) {
  46412. if (!ts.isPropertyAccessExpression(outputFieldAccess)) {
  46413. continue;
  46414. }
  46415. const addEventListener = outputFieldAccess.name;
  46416. const tsSymbol = this.getTypeChecker().getSymbolAtLocation(addEventListener);
  46417. const tsType = this.getTypeChecker().getTypeAtLocation(addEventListener);
  46418. const positionInFile = this.getTcbPositionForNode(addEventListener);
  46419. const target = this.getSymbol(consumer);
  46420. if (target === null || tsSymbol === undefined) {
  46421. continue;
  46422. }
  46423. bindings.push({
  46424. kind: exports.SymbolKind.Binding,
  46425. tsSymbol,
  46426. tsType,
  46427. target,
  46428. tcbLocation: {
  46429. tcbPath: this.tcbPath,
  46430. isShimFile: this.tcbIsShim,
  46431. positionInFile,
  46432. },
  46433. });
  46434. }
  46435. else {
  46436. if (!ts.isElementAccessExpression(outputFieldAccess)) {
  46437. continue;
  46438. }
  46439. const tsSymbol = this.getTypeChecker().getSymbolAtLocation(outputFieldAccess.argumentExpression);
  46440. if (tsSymbol === undefined) {
  46441. continue;
  46442. }
  46443. const target = this.getDirectiveSymbolForAccessExpression(outputFieldAccess, consumer);
  46444. if (target === null) {
  46445. continue;
  46446. }
  46447. const positionInFile = this.getTcbPositionForNode(outputFieldAccess);
  46448. const tsType = this.getTypeChecker().getTypeAtLocation(outputFieldAccess);
  46449. bindings.push({
  46450. kind: exports.SymbolKind.Binding,
  46451. tsSymbol,
  46452. tsType,
  46453. target,
  46454. tcbLocation: {
  46455. tcbPath: this.tcbPath,
  46456. isShimFile: this.tcbIsShim,
  46457. positionInFile,
  46458. },
  46459. });
  46460. }
  46461. }
  46462. if (bindings.length === 0) {
  46463. return null;
  46464. }
  46465. return { kind: exports.SymbolKind.Output, bindings };
  46466. }
  46467. getSymbolOfInputBinding(binding) {
  46468. const consumer = this.typeCheckData.boundTarget.getConsumerOfBinding(binding);
  46469. if (consumer === null) {
  46470. return null;
  46471. }
  46472. if (consumer instanceof Element$1 || consumer instanceof Template) {
  46473. const host = this.getSymbol(consumer);
  46474. return host !== null ? { kind: exports.SymbolKind.DomBinding, host } : null;
  46475. }
  46476. const nodes = findAllMatchingNodes(this.typeCheckBlock, {
  46477. withSpan: binding.sourceSpan,
  46478. filter: isAssignment,
  46479. });
  46480. const bindings = [];
  46481. for (const node of nodes) {
  46482. if (!isAccessExpression(node.left)) {
  46483. continue;
  46484. }
  46485. const signalInputAssignment = unwrapSignalInputWriteTAccessor(node.left);
  46486. let fieldAccessExpr;
  46487. let symbolInfo = null;
  46488. // Signal inputs need special treatment because they are generated with an extra keyed
  46489. // access. E.g. `_t1.prop[WriteT_ACCESSOR_SYMBOL]`. Observations:
  46490. // - The keyed access for the write type needs to be resolved for the "input type".
  46491. // - The definition symbol of the input should be the input class member, and not the
  46492. // internal write accessor. Symbol should resolve `_t1.prop`.
  46493. if (signalInputAssignment !== null) {
  46494. // Note: If the field expression for the input binding refers to just an identifier,
  46495. // then we are handling the case of a temporary variable being used for the input field.
  46496. // This is the case with `honorAccessModifiersForInputBindings = false` and in those cases
  46497. // we cannot resolve the owning directive, similar to how we guard above with `isAccessExpression`.
  46498. if (ts.isIdentifier(signalInputAssignment.fieldExpr)) {
  46499. continue;
  46500. }
  46501. const fieldSymbol = this.getSymbolOfTsNode(signalInputAssignment.fieldExpr);
  46502. const typeSymbol = this.getSymbolOfTsNode(signalInputAssignment.typeExpr);
  46503. fieldAccessExpr = signalInputAssignment.fieldExpr;
  46504. symbolInfo =
  46505. fieldSymbol === null || typeSymbol === null
  46506. ? null
  46507. : {
  46508. tcbLocation: fieldSymbol.tcbLocation,
  46509. tsSymbol: fieldSymbol.tsSymbol,
  46510. tsType: typeSymbol.tsType,
  46511. };
  46512. }
  46513. else {
  46514. fieldAccessExpr = node.left;
  46515. symbolInfo = this.getSymbolOfTsNode(node.left);
  46516. }
  46517. if (symbolInfo === null || symbolInfo.tsSymbol === null) {
  46518. continue;
  46519. }
  46520. const target = this.getDirectiveSymbolForAccessExpression(fieldAccessExpr, consumer);
  46521. if (target === null) {
  46522. continue;
  46523. }
  46524. bindings.push({
  46525. ...symbolInfo,
  46526. tsSymbol: symbolInfo.tsSymbol,
  46527. kind: exports.SymbolKind.Binding,
  46528. target,
  46529. });
  46530. }
  46531. if (bindings.length === 0) {
  46532. return null;
  46533. }
  46534. return { kind: exports.SymbolKind.Input, bindings };
  46535. }
  46536. getDirectiveSymbolForAccessExpression(fieldAccessExpr, { isComponent, selector, isStructural }) {
  46537. // In all cases, `_t1["index"]` or `_t1.index`, `node.expression` is _t1.
  46538. const tsSymbol = this.getTypeChecker().getSymbolAtLocation(fieldAccessExpr.expression);
  46539. if (tsSymbol?.declarations === undefined ||
  46540. tsSymbol.declarations.length === 0 ||
  46541. selector === null) {
  46542. return null;
  46543. }
  46544. const [declaration] = tsSymbol.declarations;
  46545. if (!ts.isVariableDeclaration(declaration) ||
  46546. !hasExpressionIdentifier(
  46547. // The expression identifier could be on the type (for regular directives) or the name
  46548. // (for generic directives and the ctor op).
  46549. declaration.getSourceFile(), declaration.type ?? declaration.name, ExpressionIdentifier.DIRECTIVE)) {
  46550. return null;
  46551. }
  46552. const symbol = this.getSymbolOfTsNode(declaration);
  46553. if (symbol === null ||
  46554. !isSymbolWithValueDeclaration(symbol.tsSymbol) ||
  46555. !ts.isClassDeclaration(symbol.tsSymbol.valueDeclaration)) {
  46556. return null;
  46557. }
  46558. const ref = new Reference(symbol.tsSymbol.valueDeclaration);
  46559. const ngModule = this.getDirectiveModule(symbol.tsSymbol.valueDeclaration);
  46560. return {
  46561. ref,
  46562. kind: exports.SymbolKind.Directive,
  46563. tsSymbol: symbol.tsSymbol,
  46564. tsType: symbol.tsType,
  46565. tcbLocation: symbol.tcbLocation,
  46566. isComponent,
  46567. isStructural,
  46568. selector,
  46569. ngModule,
  46570. isHostDirective: false,
  46571. isInScope: true, // TODO: this should always be in scope in this context, right?
  46572. };
  46573. }
  46574. getSymbolOfVariable(variable) {
  46575. const node = findFirstMatchingNode(this.typeCheckBlock, {
  46576. withSpan: variable.sourceSpan,
  46577. filter: ts.isVariableDeclaration,
  46578. });
  46579. if (node === null) {
  46580. return null;
  46581. }
  46582. let nodeValueSymbol = null;
  46583. if (ts.isForOfStatement(node.parent.parent)) {
  46584. nodeValueSymbol = this.getSymbolOfTsNode(node);
  46585. }
  46586. else if (node.initializer !== undefined) {
  46587. nodeValueSymbol = this.getSymbolOfTsNode(node.initializer);
  46588. }
  46589. if (nodeValueSymbol === null) {
  46590. return null;
  46591. }
  46592. return {
  46593. tsType: nodeValueSymbol.tsType,
  46594. tsSymbol: nodeValueSymbol.tsSymbol,
  46595. initializerLocation: nodeValueSymbol.tcbLocation,
  46596. kind: exports.SymbolKind.Variable,
  46597. declaration: variable,
  46598. localVarLocation: {
  46599. tcbPath: this.tcbPath,
  46600. isShimFile: this.tcbIsShim,
  46601. positionInFile: this.getTcbPositionForNode(node.name),
  46602. },
  46603. };
  46604. }
  46605. getSymbolOfReference(ref) {
  46606. const target = this.typeCheckData.boundTarget.getReferenceTarget(ref);
  46607. // Find the node for the reference declaration, i.e. `var _t2 = _t1;`
  46608. let node = findFirstMatchingNode(this.typeCheckBlock, {
  46609. withSpan: ref.sourceSpan,
  46610. filter: ts.isVariableDeclaration,
  46611. });
  46612. if (node === null || target === null || node.initializer === undefined) {
  46613. return null;
  46614. }
  46615. // Get the original declaration for the references variable, with the exception of template refs
  46616. // which are of the form var _t3 = (_t2 as any as i2.TemplateRef<any>)
  46617. // TODO(atscott): Consider adding an `ExpressionIdentifier` to tag variable declaration
  46618. // initializers as invalid for symbol retrieval.
  46619. const originalDeclaration = ts.isParenthesizedExpression(node.initializer) &&
  46620. ts.isAsExpression(node.initializer.expression)
  46621. ? this.getTypeChecker().getSymbolAtLocation(node.name)
  46622. : this.getTypeChecker().getSymbolAtLocation(node.initializer);
  46623. if (originalDeclaration === undefined || originalDeclaration.valueDeclaration === undefined) {
  46624. return null;
  46625. }
  46626. const symbol = this.getSymbolOfTsNode(originalDeclaration.valueDeclaration);
  46627. if (symbol === null || symbol.tsSymbol === null) {
  46628. return null;
  46629. }
  46630. const referenceVarTcbLocation = {
  46631. tcbPath: this.tcbPath,
  46632. isShimFile: this.tcbIsShim,
  46633. positionInFile: this.getTcbPositionForNode(node),
  46634. };
  46635. if (target instanceof Template || target instanceof Element$1) {
  46636. return {
  46637. kind: exports.SymbolKind.Reference,
  46638. tsSymbol: symbol.tsSymbol,
  46639. tsType: symbol.tsType,
  46640. target,
  46641. declaration: ref,
  46642. targetLocation: symbol.tcbLocation,
  46643. referenceVarLocation: referenceVarTcbLocation,
  46644. };
  46645. }
  46646. else {
  46647. if (!ts.isClassDeclaration(target.directive.ref.node)) {
  46648. return null;
  46649. }
  46650. return {
  46651. kind: exports.SymbolKind.Reference,
  46652. tsSymbol: symbol.tsSymbol,
  46653. tsType: symbol.tsType,
  46654. declaration: ref,
  46655. target: target.directive.ref.node,
  46656. targetLocation: symbol.tcbLocation,
  46657. referenceVarLocation: referenceVarTcbLocation,
  46658. };
  46659. }
  46660. }
  46661. getSymbolOfLetDeclaration(decl) {
  46662. const node = findFirstMatchingNode(this.typeCheckBlock, {
  46663. withSpan: decl.sourceSpan,
  46664. filter: ts.isVariableDeclaration,
  46665. });
  46666. if (node === null) {
  46667. return null;
  46668. }
  46669. const nodeValueSymbol = this.getSymbolOfTsNode(node.initializer);
  46670. if (nodeValueSymbol === null) {
  46671. return null;
  46672. }
  46673. return {
  46674. tsType: nodeValueSymbol.tsType,
  46675. tsSymbol: nodeValueSymbol.tsSymbol,
  46676. initializerLocation: nodeValueSymbol.tcbLocation,
  46677. kind: exports.SymbolKind.LetDeclaration,
  46678. declaration: decl,
  46679. localVarLocation: {
  46680. tcbPath: this.tcbPath,
  46681. isShimFile: this.tcbIsShim,
  46682. positionInFile: this.getTcbPositionForNode(node.name),
  46683. },
  46684. };
  46685. }
  46686. getSymbolOfPipe(expression) {
  46687. const methodAccess = findFirstMatchingNode(this.typeCheckBlock, {
  46688. withSpan: expression.nameSpan,
  46689. filter: ts.isPropertyAccessExpression,
  46690. });
  46691. if (methodAccess === null) {
  46692. return null;
  46693. }
  46694. const pipeVariableNode = methodAccess.expression;
  46695. const pipeDeclaration = this.getTypeChecker().getSymbolAtLocation(pipeVariableNode);
  46696. if (pipeDeclaration === undefined || pipeDeclaration.valueDeclaration === undefined) {
  46697. return null;
  46698. }
  46699. const pipeInstance = this.getSymbolOfTsNode(pipeDeclaration.valueDeclaration);
  46700. // The instance should never be null, nor should the symbol lack a value declaration. This
  46701. // is because the node used to look for the `pipeInstance` symbol info is a value
  46702. // declaration of another symbol (i.e. the `pipeDeclaration` symbol).
  46703. if (pipeInstance === null || !isSymbolWithValueDeclaration(pipeInstance.tsSymbol)) {
  46704. return null;
  46705. }
  46706. const symbolInfo = this.getSymbolOfTsNode(methodAccess);
  46707. if (symbolInfo === null) {
  46708. return null;
  46709. }
  46710. return {
  46711. kind: exports.SymbolKind.Pipe,
  46712. ...symbolInfo,
  46713. classSymbol: {
  46714. ...pipeInstance,
  46715. tsSymbol: pipeInstance.tsSymbol,
  46716. },
  46717. };
  46718. }
  46719. getSymbolOfTemplateExpression(expression) {
  46720. if (expression instanceof ASTWithSource) {
  46721. expression = expression.ast;
  46722. }
  46723. const expressionTarget = this.typeCheckData.boundTarget.getExpressionTarget(expression);
  46724. if (expressionTarget !== null) {
  46725. return this.getSymbol(expressionTarget);
  46726. }
  46727. let withSpan = expression.sourceSpan;
  46728. // The `name` part of a `PropertyWrite` and `ASTWithName` do not have their own
  46729. // AST so there is no way to retrieve a `Symbol` for just the `name` via a specific node.
  46730. // Also skipping SafePropertyReads as it breaks nullish coalescing not nullable extended diagnostic
  46731. if (expression instanceof PropertyWrite ||
  46732. (expression instanceof ASTWithName && !(expression instanceof SafePropertyRead))) {
  46733. withSpan = expression.nameSpan;
  46734. }
  46735. let node = null;
  46736. // Property reads in templates usually map to a `PropertyAccessExpression`
  46737. // (e.g. `ctx.foo`) so try looking for one first.
  46738. if (expression instanceof PropertyRead) {
  46739. node = findFirstMatchingNode(this.typeCheckBlock, {
  46740. withSpan,
  46741. filter: ts.isPropertyAccessExpression,
  46742. });
  46743. }
  46744. // Otherwise fall back to searching for any AST node.
  46745. if (node === null) {
  46746. node = findFirstMatchingNode(this.typeCheckBlock, { withSpan, filter: anyNodeFilter });
  46747. }
  46748. if (node === null) {
  46749. return null;
  46750. }
  46751. while (ts.isParenthesizedExpression(node)) {
  46752. node = node.expression;
  46753. }
  46754. // - If we have safe property read ("a?.b") we want to get the Symbol for b, the `whenTrue`
  46755. // expression.
  46756. // - If our expression is a pipe binding ("a | test:b:c"), we want the Symbol for the
  46757. // `transform` on the pipe.
  46758. // - Otherwise, we retrieve the symbol for the node itself with no special considerations
  46759. if (expression instanceof SafePropertyRead && ts.isConditionalExpression(node)) {
  46760. const whenTrueSymbol = this.getSymbolOfTsNode(node.whenTrue);
  46761. if (whenTrueSymbol === null) {
  46762. return null;
  46763. }
  46764. return {
  46765. ...whenTrueSymbol,
  46766. kind: exports.SymbolKind.Expression,
  46767. // Rather than using the type of only the `whenTrue` part of the expression, we should
  46768. // still get the type of the whole conditional expression to include `|undefined`.
  46769. tsType: this.getTypeChecker().getTypeAtLocation(node),
  46770. };
  46771. }
  46772. else {
  46773. const symbolInfo = this.getSymbolOfTsNode(node);
  46774. return symbolInfo === null ? null : { ...symbolInfo, kind: exports.SymbolKind.Expression };
  46775. }
  46776. }
  46777. getSymbolOfTsNode(node) {
  46778. while (ts.isParenthesizedExpression(node)) {
  46779. node = node.expression;
  46780. }
  46781. let tsSymbol;
  46782. if (ts.isPropertyAccessExpression(node)) {
  46783. tsSymbol = this.getTypeChecker().getSymbolAtLocation(node.name);
  46784. }
  46785. else if (ts.isCallExpression(node)) {
  46786. tsSymbol = this.getTypeChecker().getSymbolAtLocation(node.expression);
  46787. }
  46788. else {
  46789. tsSymbol = this.getTypeChecker().getSymbolAtLocation(node);
  46790. }
  46791. const positionInFile = this.getTcbPositionForNode(node);
  46792. const type = this.getTypeChecker().getTypeAtLocation(node);
  46793. return {
  46794. // If we could not find a symbol, fall back to the symbol on the type for the node.
  46795. // Some nodes won't have a "symbol at location" but will have a symbol for the type.
  46796. // Examples of this would be literals and `document.createElement('div')`.
  46797. tsSymbol: tsSymbol ?? type.symbol ?? null,
  46798. tsType: type,
  46799. tcbLocation: {
  46800. tcbPath: this.tcbPath,
  46801. isShimFile: this.tcbIsShim,
  46802. positionInFile,
  46803. },
  46804. };
  46805. }
  46806. getTcbPositionForNode(node) {
  46807. if (ts.isTypeReferenceNode(node)) {
  46808. return this.getTcbPositionForNode(node.typeName);
  46809. }
  46810. else if (ts.isQualifiedName(node)) {
  46811. return node.right.getStart();
  46812. }
  46813. else if (ts.isPropertyAccessExpression(node)) {
  46814. return node.name.getStart();
  46815. }
  46816. else if (ts.isElementAccessExpression(node)) {
  46817. return node.argumentExpression.getStart();
  46818. }
  46819. else {
  46820. return node.getStart();
  46821. }
  46822. }
  46823. }
  46824. /** Filter predicate function that matches any AST node. */
  46825. function anyNodeFilter(n) {
  46826. return true;
  46827. }
  46828. function sourceSpanEqual(a, b) {
  46829. return a.start.offset === b.start.offset && a.end.offset === b.end.offset;
  46830. }
  46831. function unwrapSignalInputWriteTAccessor(expr) {
  46832. // e.g. `_t2.inputA[i2.ɵINPUT_SIGNAL_BRAND_WRITE_TYPE]`
  46833. // 1. Assert that we are dealing with an element access expression.
  46834. // 2. Assert that we are dealing with a signal brand symbol access in the argument expression.
  46835. if (!ts.isElementAccessExpression(expr) ||
  46836. !ts.isPropertyAccessExpression(expr.argumentExpression)) {
  46837. return null;
  46838. }
  46839. // Assert that the property access in the element access is a simple identifier and
  46840. // refers to `ɵINPUT_SIGNAL_BRAND_WRITE_TYPE`.
  46841. if (!ts.isIdentifier(expr.argumentExpression.name) ||
  46842. expr.argumentExpression.name.text !== Identifiers.InputSignalBrandWriteType.name) {
  46843. return null;
  46844. }
  46845. // Assert that the expression is either:
  46846. // - `_t2.inputA[ɵINPUT_SIGNAL_BRAND_WRITE_TYPE]` or (common case)
  46847. // - or `_t2['input-A'][ɵINPUT_SIGNAL_BRAND_WRITE_TYPE]` (non-identifier input field names)
  46848. // - or `_dirInput[ɵINPUT_SIGNAL_BRAND_WRITE_TYPE` (honorAccessModifiersForInputBindings=false)
  46849. // This is checked for type safety and to catch unexpected cases.
  46850. if (!ts.isPropertyAccessExpression(expr.expression) &&
  46851. !ts.isElementAccessExpression(expr.expression) &&
  46852. !ts.isIdentifier(expr.expression)) {
  46853. throw new Error('Unexpected expression for signal input write type.');
  46854. }
  46855. return {
  46856. fieldExpr: expr.expression,
  46857. typeExpr: expr,
  46858. };
  46859. }
  46860. const REGISTRY = new DomElementSchemaRegistry();
  46861. /**
  46862. * Primary template type-checking engine, which performs type-checking using a
  46863. * `TypeCheckingProgramStrategy` for type-checking program maintenance, and the
  46864. * `ProgramTypeCheckAdapter` for generation of template type-checking code.
  46865. */
  46866. class TemplateTypeCheckerImpl {
  46867. originalProgram;
  46868. programDriver;
  46869. typeCheckAdapter;
  46870. config;
  46871. refEmitter;
  46872. reflector;
  46873. compilerHost;
  46874. priorBuild;
  46875. metaReader;
  46876. localMetaReader;
  46877. ngModuleIndex;
  46878. componentScopeReader;
  46879. typeCheckScopeRegistry;
  46880. perf;
  46881. state = new Map();
  46882. /**
  46883. * Stores the `CompletionEngine` which powers autocompletion for each component class.
  46884. *
  46885. * Must be invalidated whenever the component's template or the `ts.Program` changes. Invalidation
  46886. * on template changes is performed within this `TemplateTypeCheckerImpl` instance. When the
  46887. * `ts.Program` changes, the `TemplateTypeCheckerImpl` as a whole is destroyed and replaced.
  46888. */
  46889. completionCache = new Map();
  46890. /**
  46891. * Stores the `SymbolBuilder` which creates symbols for each component class.
  46892. *
  46893. * Must be invalidated whenever the component's template or the `ts.Program` changes. Invalidation
  46894. * on template changes is performed within this `TemplateTypeCheckerImpl` instance. When the
  46895. * `ts.Program` changes, the `TemplateTypeCheckerImpl` as a whole is destroyed and replaced.
  46896. */
  46897. symbolBuilderCache = new Map();
  46898. /**
  46899. * Stores directives and pipes that are in scope for each component.
  46900. *
  46901. * Unlike other caches, the scope of a component is not affected by its template. It will be
  46902. * destroyed when the `ts.Program` changes and the `TemplateTypeCheckerImpl` as a whole is
  46903. * destroyed and replaced.
  46904. */
  46905. scopeCache = new Map();
  46906. /**
  46907. * Stores potential element tags for each component (a union of DOM tags as well as directive
  46908. * tags).
  46909. *
  46910. * Unlike other caches, the scope of a component is not affected by its template. It will be
  46911. * destroyed when the `ts.Program` changes and the `TemplateTypeCheckerImpl` as a whole is
  46912. * destroyed and replaced.
  46913. */
  46914. elementTagCache = new Map();
  46915. isComplete = false;
  46916. priorResultsAdopted = false;
  46917. constructor(originalProgram, programDriver, typeCheckAdapter, config, refEmitter, reflector, compilerHost, priorBuild, metaReader, localMetaReader, ngModuleIndex, componentScopeReader, typeCheckScopeRegistry, perf) {
  46918. this.originalProgram = originalProgram;
  46919. this.programDriver = programDriver;
  46920. this.typeCheckAdapter = typeCheckAdapter;
  46921. this.config = config;
  46922. this.refEmitter = refEmitter;
  46923. this.reflector = reflector;
  46924. this.compilerHost = compilerHost;
  46925. this.priorBuild = priorBuild;
  46926. this.metaReader = metaReader;
  46927. this.localMetaReader = localMetaReader;
  46928. this.ngModuleIndex = ngModuleIndex;
  46929. this.componentScopeReader = componentScopeReader;
  46930. this.typeCheckScopeRegistry = typeCheckScopeRegistry;
  46931. this.perf = perf;
  46932. }
  46933. getTemplate(component, optimizeFor) {
  46934. const { data } = this.getLatestComponentState(component, optimizeFor);
  46935. if (data === null) {
  46936. return null;
  46937. }
  46938. return data.template;
  46939. }
  46940. getUsedDirectives(component) {
  46941. return this.getLatestComponentState(component).data?.boundTarget.getUsedDirectives() || null;
  46942. }
  46943. getUsedPipes(component) {
  46944. return this.getLatestComponentState(component).data?.boundTarget.getUsedPipes() || null;
  46945. }
  46946. getLatestComponentState(component, optimizeFor = exports.OptimizeFor.SingleFile) {
  46947. switch (optimizeFor) {
  46948. case exports.OptimizeFor.WholeProgram:
  46949. this.ensureAllShimsForAllFiles();
  46950. break;
  46951. case exports.OptimizeFor.SingleFile:
  46952. this.ensureShimForComponent(component);
  46953. break;
  46954. }
  46955. const sf = component.getSourceFile();
  46956. const sfPath = absoluteFromSourceFile(sf);
  46957. const shimPath = TypeCheckShimGenerator.shimFor(sfPath);
  46958. const fileRecord = this.getFileData(sfPath);
  46959. if (!fileRecord.shimData.has(shimPath)) {
  46960. return { data: null, tcb: null, tcbPath: shimPath, tcbIsShim: true };
  46961. }
  46962. const id = fileRecord.sourceManager.getTypeCheckId(component);
  46963. const shimRecord = fileRecord.shimData.get(shimPath);
  46964. const program = this.programDriver.getProgram();
  46965. const shimSf = getSourceFileOrNull(program, shimPath);
  46966. if (shimSf === null || !fileRecord.shimData.has(shimPath)) {
  46967. throw new Error(`Error: no shim file in program: ${shimPath}`);
  46968. }
  46969. let tcb = findTypeCheckBlock(shimSf, id, /*isDiagnosticsRequest*/ false);
  46970. let tcbPath = shimPath;
  46971. if (tcb === null) {
  46972. // Try for an inline block.
  46973. const inlineSf = getSourceFileOrError(program, sfPath);
  46974. tcb = findTypeCheckBlock(inlineSf, id, /*isDiagnosticsRequest*/ false);
  46975. if (tcb !== null) {
  46976. tcbPath = sfPath;
  46977. }
  46978. }
  46979. let data = null;
  46980. if (shimRecord.data.has(id)) {
  46981. data = shimRecord.data.get(id);
  46982. }
  46983. return { data, tcb, tcbPath, tcbIsShim: tcbPath === shimPath };
  46984. }
  46985. isTrackedTypeCheckFile(filePath) {
  46986. return this.getFileAndShimRecordsForPath(filePath) !== null;
  46987. }
  46988. getFileRecordForTcbLocation({ tcbPath, isShimFile, }) {
  46989. if (!isShimFile) {
  46990. // The location is not within a shim file but corresponds with an inline TCB in an original
  46991. // source file; we can obtain the record directly by its path.
  46992. if (this.state.has(tcbPath)) {
  46993. return this.state.get(tcbPath);
  46994. }
  46995. else {
  46996. return null;
  46997. }
  46998. }
  46999. // The location is within a type-checking shim file; find the type-checking data that owns this
  47000. // shim path.
  47001. const records = this.getFileAndShimRecordsForPath(tcbPath);
  47002. if (records !== null) {
  47003. return records.fileRecord;
  47004. }
  47005. else {
  47006. return null;
  47007. }
  47008. }
  47009. getFileAndShimRecordsForPath(shimPath) {
  47010. for (const fileRecord of this.state.values()) {
  47011. if (fileRecord.shimData.has(shimPath)) {
  47012. return { fileRecord, shimRecord: fileRecord.shimData.get(shimPath) };
  47013. }
  47014. }
  47015. return null;
  47016. }
  47017. getSourceMappingAtTcbLocation(tcbLocation) {
  47018. const fileRecord = this.getFileRecordForTcbLocation(tcbLocation);
  47019. if (fileRecord === null) {
  47020. return null;
  47021. }
  47022. const shimSf = this.programDriver.getProgram().getSourceFile(tcbLocation.tcbPath);
  47023. if (shimSf === undefined) {
  47024. return null;
  47025. }
  47026. return getSourceMapping(shimSf, tcbLocation.positionInFile, fileRecord.sourceManager,
  47027. /*isDiagnosticsRequest*/ false);
  47028. }
  47029. generateAllTypeCheckBlocks() {
  47030. this.ensureAllShimsForAllFiles();
  47031. }
  47032. /**
  47033. * Retrieve type-checking and template parse diagnostics from the given `ts.SourceFile` using the
  47034. * most recent type-checking program.
  47035. */
  47036. getDiagnosticsForFile(sf, optimizeFor) {
  47037. switch (optimizeFor) {
  47038. case exports.OptimizeFor.WholeProgram:
  47039. this.ensureAllShimsForAllFiles();
  47040. break;
  47041. case exports.OptimizeFor.SingleFile:
  47042. this.ensureAllShimsForOneFile(sf);
  47043. break;
  47044. }
  47045. return this.perf.inPhase(exports.PerfPhase.TtcDiagnostics, () => {
  47046. const sfPath = absoluteFromSourceFile(sf);
  47047. const fileRecord = this.state.get(sfPath);
  47048. const typeCheckProgram = this.programDriver.getProgram();
  47049. const diagnostics = [];
  47050. if (fileRecord.hasInlines) {
  47051. const inlineSf = getSourceFileOrError(typeCheckProgram, sfPath);
  47052. diagnostics.push(...typeCheckProgram
  47053. .getSemanticDiagnostics(inlineSf)
  47054. .map((diag) => convertDiagnostic(diag, fileRecord.sourceManager)));
  47055. }
  47056. for (const [shimPath, shimRecord] of fileRecord.shimData) {
  47057. const shimSf = getSourceFileOrError(typeCheckProgram, shimPath);
  47058. diagnostics.push(...typeCheckProgram
  47059. .getSemanticDiagnostics(shimSf)
  47060. .map((diag) => convertDiagnostic(diag, fileRecord.sourceManager)));
  47061. diagnostics.push(...shimRecord.genesisDiagnostics);
  47062. for (const templateData of shimRecord.data.values()) {
  47063. diagnostics.push(...templateData.templateParsingDiagnostics);
  47064. }
  47065. }
  47066. return diagnostics.filter((diag) => diag !== null);
  47067. });
  47068. }
  47069. getDiagnosticsForComponent(component) {
  47070. this.ensureShimForComponent(component);
  47071. return this.perf.inPhase(exports.PerfPhase.TtcDiagnostics, () => {
  47072. const sf = component.getSourceFile();
  47073. const sfPath = absoluteFromSourceFile(sf);
  47074. const shimPath = TypeCheckShimGenerator.shimFor(sfPath);
  47075. const fileRecord = this.getFileData(sfPath);
  47076. if (!fileRecord.shimData.has(shimPath)) {
  47077. return [];
  47078. }
  47079. const id = fileRecord.sourceManager.getTypeCheckId(component);
  47080. const shimRecord = fileRecord.shimData.get(shimPath);
  47081. const typeCheckProgram = this.programDriver.getProgram();
  47082. const diagnostics = [];
  47083. if (shimRecord.hasInlines) {
  47084. const inlineSf = getSourceFileOrError(typeCheckProgram, sfPath);
  47085. diagnostics.push(...typeCheckProgram
  47086. .getSemanticDiagnostics(inlineSf)
  47087. .map((diag) => convertDiagnostic(diag, fileRecord.sourceManager)));
  47088. }
  47089. const shimSf = getSourceFileOrError(typeCheckProgram, shimPath);
  47090. diagnostics.push(...typeCheckProgram
  47091. .getSemanticDiagnostics(shimSf)
  47092. .map((diag) => convertDiagnostic(diag, fileRecord.sourceManager)));
  47093. diagnostics.push(...shimRecord.genesisDiagnostics);
  47094. for (const templateData of shimRecord.data.values()) {
  47095. diagnostics.push(...templateData.templateParsingDiagnostics);
  47096. }
  47097. return diagnostics.filter((diag) => diag !== null && diag.typeCheckId === id);
  47098. });
  47099. }
  47100. getTypeCheckBlock(component) {
  47101. return this.getLatestComponentState(component).tcb;
  47102. }
  47103. getGlobalCompletions(context, component, node) {
  47104. const engine = this.getOrCreateCompletionEngine(component);
  47105. if (engine === null) {
  47106. return null;
  47107. }
  47108. return this.perf.inPhase(exports.PerfPhase.TtcAutocompletion, () => engine.getGlobalCompletions(context, node));
  47109. }
  47110. getExpressionCompletionLocation(ast, component) {
  47111. const engine = this.getOrCreateCompletionEngine(component);
  47112. if (engine === null) {
  47113. return null;
  47114. }
  47115. return this.perf.inPhase(exports.PerfPhase.TtcAutocompletion, () => engine.getExpressionCompletionLocation(ast));
  47116. }
  47117. getLiteralCompletionLocation(node, component) {
  47118. const engine = this.getOrCreateCompletionEngine(component);
  47119. if (engine === null) {
  47120. return null;
  47121. }
  47122. return this.perf.inPhase(exports.PerfPhase.TtcAutocompletion, () => engine.getLiteralCompletionLocation(node));
  47123. }
  47124. invalidateClass(clazz) {
  47125. this.completionCache.delete(clazz);
  47126. this.symbolBuilderCache.delete(clazz);
  47127. this.scopeCache.delete(clazz);
  47128. this.elementTagCache.delete(clazz);
  47129. const sf = clazz.getSourceFile();
  47130. const sfPath = absoluteFromSourceFile(sf);
  47131. const shimPath = TypeCheckShimGenerator.shimFor(sfPath);
  47132. const fileData = this.getFileData(sfPath);
  47133. fileData.sourceManager.getTypeCheckId(clazz);
  47134. fileData.shimData.delete(shimPath);
  47135. fileData.isComplete = false;
  47136. this.isComplete = false;
  47137. }
  47138. getExpressionTarget(expression, clazz) {
  47139. return (this.getLatestComponentState(clazz).data?.boundTarget.getExpressionTarget(expression) || null);
  47140. }
  47141. makeTemplateDiagnostic(clazz, sourceSpan, category, errorCode, message, relatedInformation) {
  47142. const sfPath = absoluteFromSourceFile(clazz.getSourceFile());
  47143. const fileRecord = this.state.get(sfPath);
  47144. const id = fileRecord.sourceManager.getTypeCheckId(clazz);
  47145. const mapping = fileRecord.sourceManager.getTemplateSourceMapping(id);
  47146. return {
  47147. ...makeTemplateDiagnostic(id, mapping, sourceSpan, category, ngErrorCode(errorCode), message, relatedInformation),
  47148. __ngCode: errorCode,
  47149. };
  47150. }
  47151. getOrCreateCompletionEngine(component) {
  47152. if (this.completionCache.has(component)) {
  47153. return this.completionCache.get(component);
  47154. }
  47155. const { tcb, data, tcbPath, tcbIsShim } = this.getLatestComponentState(component);
  47156. if (tcb === null || data === null) {
  47157. return null;
  47158. }
  47159. const engine = new CompletionEngine(tcb, data, tcbPath, tcbIsShim);
  47160. this.completionCache.set(component, engine);
  47161. return engine;
  47162. }
  47163. maybeAdoptPriorResults() {
  47164. if (this.priorResultsAdopted) {
  47165. return;
  47166. }
  47167. for (const sf of this.originalProgram.getSourceFiles()) {
  47168. if (sf.isDeclarationFile || isShim(sf)) {
  47169. continue;
  47170. }
  47171. const sfPath = absoluteFromSourceFile(sf);
  47172. if (this.state.has(sfPath)) {
  47173. const existingResults = this.state.get(sfPath);
  47174. if (existingResults.isComplete) {
  47175. // All data for this file has already been generated, so no need to adopt anything.
  47176. continue;
  47177. }
  47178. }
  47179. const previousResults = this.priorBuild.priorTypeCheckingResultsFor(sf);
  47180. if (previousResults === null || !previousResults.isComplete) {
  47181. continue;
  47182. }
  47183. this.perf.eventCount(exports.PerfEvent.ReuseTypeCheckFile);
  47184. this.state.set(sfPath, previousResults);
  47185. }
  47186. this.priorResultsAdopted = true;
  47187. }
  47188. ensureAllShimsForAllFiles() {
  47189. if (this.isComplete) {
  47190. return;
  47191. }
  47192. this.maybeAdoptPriorResults();
  47193. this.perf.inPhase(exports.PerfPhase.TcbGeneration, () => {
  47194. const host = new WholeProgramTypeCheckingHost(this);
  47195. const ctx = this.newContext(host);
  47196. for (const sf of this.originalProgram.getSourceFiles()) {
  47197. if (sf.isDeclarationFile || isShim(sf)) {
  47198. continue;
  47199. }
  47200. const sfPath = absoluteFromSourceFile(sf);
  47201. const fileData = this.getFileData(sfPath);
  47202. if (fileData.isComplete) {
  47203. continue;
  47204. }
  47205. this.typeCheckAdapter.typeCheck(sf, ctx);
  47206. fileData.isComplete = true;
  47207. }
  47208. this.updateFromContext(ctx);
  47209. this.isComplete = true;
  47210. });
  47211. }
  47212. ensureAllShimsForOneFile(sf) {
  47213. this.maybeAdoptPriorResults();
  47214. this.perf.inPhase(exports.PerfPhase.TcbGeneration, () => {
  47215. const sfPath = absoluteFromSourceFile(sf);
  47216. const fileData = this.getFileData(sfPath);
  47217. if (fileData.isComplete) {
  47218. // All data for this file is present and accounted for already.
  47219. return;
  47220. }
  47221. const host = new SingleFileTypeCheckingHost(sfPath, fileData, this);
  47222. const ctx = this.newContext(host);
  47223. this.typeCheckAdapter.typeCheck(sf, ctx);
  47224. fileData.isComplete = true;
  47225. this.updateFromContext(ctx);
  47226. });
  47227. }
  47228. ensureShimForComponent(component) {
  47229. this.maybeAdoptPriorResults();
  47230. const sf = component.getSourceFile();
  47231. const sfPath = absoluteFromSourceFile(sf);
  47232. const shimPath = TypeCheckShimGenerator.shimFor(sfPath);
  47233. const fileData = this.getFileData(sfPath);
  47234. if (fileData.shimData.has(shimPath)) {
  47235. // All data for this component is available.
  47236. return;
  47237. }
  47238. const host = new SingleShimTypeCheckingHost(sfPath, fileData, this, shimPath);
  47239. const ctx = this.newContext(host);
  47240. this.typeCheckAdapter.typeCheck(sf, ctx);
  47241. this.updateFromContext(ctx);
  47242. }
  47243. newContext(host) {
  47244. const inlining = this.programDriver.supportsInlineOperations
  47245. ? InliningMode.InlineOps
  47246. : InliningMode.Error;
  47247. return new TypeCheckContextImpl(this.config, this.compilerHost, this.refEmitter, this.reflector, host, inlining, this.perf);
  47248. }
  47249. /**
  47250. * Remove any shim data that depends on inline operations applied to the type-checking program.
  47251. *
  47252. * This can be useful if new inlines need to be applied, and it's not possible to guarantee that
  47253. * they won't overwrite or corrupt existing inlines that are used by such shims.
  47254. */
  47255. clearAllShimDataUsingInlines() {
  47256. for (const fileData of this.state.values()) {
  47257. if (!fileData.hasInlines) {
  47258. continue;
  47259. }
  47260. for (const [shimFile, shimData] of fileData.shimData.entries()) {
  47261. if (shimData.hasInlines) {
  47262. fileData.shimData.delete(shimFile);
  47263. }
  47264. }
  47265. fileData.hasInlines = false;
  47266. fileData.isComplete = false;
  47267. this.isComplete = false;
  47268. }
  47269. }
  47270. updateFromContext(ctx) {
  47271. const updates = ctx.finalize();
  47272. return this.perf.inPhase(exports.PerfPhase.TcbUpdateProgram, () => {
  47273. if (updates.size > 0) {
  47274. this.perf.eventCount(exports.PerfEvent.UpdateTypeCheckProgram);
  47275. }
  47276. this.programDriver.updateFiles(updates, exports.UpdateMode.Incremental);
  47277. this.priorBuild.recordSuccessfulTypeCheck(this.state);
  47278. this.perf.memory(exports.PerfCheckpoint.TtcUpdateProgram);
  47279. });
  47280. }
  47281. getFileData(path) {
  47282. if (!this.state.has(path)) {
  47283. this.state.set(path, {
  47284. hasInlines: false,
  47285. sourceManager: new DirectiveSourceManager(),
  47286. isComplete: false,
  47287. shimData: new Map(),
  47288. });
  47289. }
  47290. return this.state.get(path);
  47291. }
  47292. getSymbolOfNode(node, component) {
  47293. const builder = this.getOrCreateSymbolBuilder(component);
  47294. if (builder === null) {
  47295. return null;
  47296. }
  47297. return this.perf.inPhase(exports.PerfPhase.TtcSymbol, () => builder.getSymbol(node));
  47298. }
  47299. getOrCreateSymbolBuilder(component) {
  47300. if (this.symbolBuilderCache.has(component)) {
  47301. return this.symbolBuilderCache.get(component);
  47302. }
  47303. const { tcb, data, tcbPath, tcbIsShim } = this.getLatestComponentState(component);
  47304. if (tcb === null || data === null) {
  47305. return null;
  47306. }
  47307. const builder = new SymbolBuilder(tcbPath, tcbIsShim, tcb, data, this.componentScopeReader, () => this.programDriver.getProgram().getTypeChecker());
  47308. this.symbolBuilderCache.set(component, builder);
  47309. return builder;
  47310. }
  47311. getPotentialTemplateDirectives(component) {
  47312. const typeChecker = this.programDriver.getProgram().getTypeChecker();
  47313. const inScopeDirectives = this.getScopeData(component)?.directives ?? [];
  47314. const resultingDirectives = new Map();
  47315. // First, all in scope directives can be used.
  47316. for (const d of inScopeDirectives) {
  47317. resultingDirectives.set(d.ref.node, d);
  47318. }
  47319. // Any additional directives found from the global registry can be used, but are not in scope.
  47320. // In the future, we can also walk other registries for .d.ts files, or traverse the
  47321. // import/export graph.
  47322. for (const directiveClass of this.localMetaReader.getKnown(exports.MetaKind.Directive)) {
  47323. const directiveMeta = this.metaReader.getDirectiveMetadata(new Reference(directiveClass));
  47324. if (directiveMeta === null)
  47325. continue;
  47326. if (resultingDirectives.has(directiveClass))
  47327. continue;
  47328. const withScope = this.scopeDataOfDirectiveMeta(typeChecker, directiveMeta);
  47329. if (withScope === null)
  47330. continue;
  47331. resultingDirectives.set(directiveClass, { ...withScope, isInScope: false });
  47332. }
  47333. return Array.from(resultingDirectives.values());
  47334. }
  47335. getPotentialPipes(component) {
  47336. // Very similar to the above `getPotentialTemplateDirectives`, but on pipes.
  47337. const typeChecker = this.programDriver.getProgram().getTypeChecker();
  47338. const inScopePipes = this.getScopeData(component)?.pipes ?? [];
  47339. const resultingPipes = new Map();
  47340. for (const p of inScopePipes) {
  47341. resultingPipes.set(p.ref.node, p);
  47342. }
  47343. for (const pipeClass of this.localMetaReader.getKnown(exports.MetaKind.Pipe)) {
  47344. const pipeMeta = this.metaReader.getPipeMetadata(new Reference(pipeClass));
  47345. if (pipeMeta === null)
  47346. continue;
  47347. if (resultingPipes.has(pipeClass))
  47348. continue;
  47349. const withScope = this.scopeDataOfPipeMeta(typeChecker, pipeMeta);
  47350. if (withScope === null)
  47351. continue;
  47352. resultingPipes.set(pipeClass, { ...withScope, isInScope: false });
  47353. }
  47354. return Array.from(resultingPipes.values());
  47355. }
  47356. getDirectiveMetadata(dir) {
  47357. if (!isNamedClassDeclaration(dir)) {
  47358. return null;
  47359. }
  47360. return this.typeCheckScopeRegistry.getTypeCheckDirectiveMetadata(new Reference(dir));
  47361. }
  47362. getNgModuleMetadata(module) {
  47363. if (!isNamedClassDeclaration(module)) {
  47364. return null;
  47365. }
  47366. return this.metaReader.getNgModuleMetadata(new Reference(module));
  47367. }
  47368. getPipeMetadata(pipe) {
  47369. if (!isNamedClassDeclaration(pipe)) {
  47370. return null;
  47371. }
  47372. return this.metaReader.getPipeMetadata(new Reference(pipe));
  47373. }
  47374. getPotentialElementTags(component) {
  47375. if (this.elementTagCache.has(component)) {
  47376. return this.elementTagCache.get(component);
  47377. }
  47378. const tagMap = new Map();
  47379. for (const tag of REGISTRY.allKnownElementNames()) {
  47380. tagMap.set(tag, null);
  47381. }
  47382. const potentialDirectives = this.getPotentialTemplateDirectives(component);
  47383. for (const directive of potentialDirectives) {
  47384. if (directive.selector === null) {
  47385. continue;
  47386. }
  47387. for (const selector of CssSelector.parse(directive.selector)) {
  47388. if (selector.element === null || tagMap.has(selector.element)) {
  47389. // Skip this directive if it doesn't match an element tag, or if another directive has
  47390. // already been included with the same element name.
  47391. continue;
  47392. }
  47393. tagMap.set(selector.element, directive);
  47394. }
  47395. }
  47396. this.elementTagCache.set(component, tagMap);
  47397. return tagMap;
  47398. }
  47399. getPotentialDomBindings(tagName) {
  47400. const attributes = REGISTRY.allKnownAttributesOfElement(tagName);
  47401. return attributes.map((attribute) => ({
  47402. attribute,
  47403. property: REGISTRY.getMappedPropName(attribute),
  47404. }));
  47405. }
  47406. getPotentialDomEvents(tagName) {
  47407. return REGISTRY.allKnownEventsOfElement(tagName);
  47408. }
  47409. getPrimaryAngularDecorator(target) {
  47410. this.ensureAllShimsForOneFile(target.getSourceFile());
  47411. if (!isNamedClassDeclaration(target)) {
  47412. return null;
  47413. }
  47414. const ref = new Reference(target);
  47415. const dirMeta = this.metaReader.getDirectiveMetadata(ref);
  47416. if (dirMeta !== null) {
  47417. return dirMeta.decorator;
  47418. }
  47419. const pipeMeta = this.metaReader.getPipeMetadata(ref);
  47420. if (pipeMeta !== null) {
  47421. return pipeMeta.decorator;
  47422. }
  47423. const ngModuleMeta = this.metaReader.getNgModuleMetadata(ref);
  47424. if (ngModuleMeta !== null) {
  47425. return ngModuleMeta.decorator;
  47426. }
  47427. return null;
  47428. }
  47429. getOwningNgModule(component) {
  47430. if (!isNamedClassDeclaration(component)) {
  47431. return null;
  47432. }
  47433. const dirMeta = this.metaReader.getDirectiveMetadata(new Reference(component));
  47434. if (dirMeta !== null && dirMeta.isStandalone) {
  47435. return null;
  47436. }
  47437. const scope = this.componentScopeReader.getScopeForComponent(component);
  47438. if (scope === null ||
  47439. scope.kind !== exports.ComponentScopeKind.NgModule ||
  47440. !isNamedClassDeclaration(scope.ngModule)) {
  47441. return null;
  47442. }
  47443. return scope.ngModule;
  47444. }
  47445. emit(kind, refTo, inContext) {
  47446. const emittedRef = this.refEmitter.emit(refTo, inContext.getSourceFile());
  47447. if (emittedRef.kind === exports.ReferenceEmitKind.Failed) {
  47448. return null;
  47449. }
  47450. const emitted = emittedRef.expression;
  47451. if (emitted instanceof WrappedNodeExpr) {
  47452. if (refTo.node === inContext) {
  47453. // Suppress self-imports since components do not have to import themselves.
  47454. return null;
  47455. }
  47456. let isForwardReference = false;
  47457. if (emitted.node.getStart() > inContext.getStart()) {
  47458. const declaration = this.programDriver
  47459. .getProgram()
  47460. .getTypeChecker()
  47461. .getTypeAtLocation(emitted.node)
  47462. .getSymbol()?.declarations?.[0];
  47463. if (declaration && declaration.getSourceFile() === inContext.getSourceFile()) {
  47464. isForwardReference = true;
  47465. }
  47466. }
  47467. // An appropriate identifier is already in scope.
  47468. return { kind, symbolName: emitted.node.text, isForwardReference };
  47469. }
  47470. else if (emitted instanceof ExternalExpr &&
  47471. emitted.value.moduleName !== null &&
  47472. emitted.value.name !== null) {
  47473. return {
  47474. kind,
  47475. moduleSpecifier: emitted.value.moduleName,
  47476. symbolName: emitted.value.name,
  47477. isForwardReference: false,
  47478. };
  47479. }
  47480. return null;
  47481. }
  47482. getPotentialImportsFor(toImport, inContext, importMode) {
  47483. const imports = [];
  47484. const meta = this.metaReader.getDirectiveMetadata(toImport) ?? this.metaReader.getPipeMetadata(toImport);
  47485. if (meta === null) {
  47486. return imports;
  47487. }
  47488. if (meta.isStandalone || importMode === exports.PotentialImportMode.ForceDirect) {
  47489. const emitted = this.emit(exports.PotentialImportKind.Standalone, toImport, inContext);
  47490. if (emitted !== null) {
  47491. imports.push(emitted);
  47492. }
  47493. }
  47494. const exportingNgModules = this.ngModuleIndex.getNgModulesExporting(meta.ref.node);
  47495. if (exportingNgModules !== null) {
  47496. for (const exporter of exportingNgModules) {
  47497. const emittedRef = this.emit(exports.PotentialImportKind.NgModule, exporter, inContext);
  47498. if (emittedRef !== null) {
  47499. imports.push(emittedRef);
  47500. }
  47501. }
  47502. }
  47503. return imports;
  47504. }
  47505. getScopeData(component) {
  47506. if (this.scopeCache.has(component)) {
  47507. return this.scopeCache.get(component);
  47508. }
  47509. if (!isNamedClassDeclaration(component)) {
  47510. throw new Error(`AssertionError: components must have names`);
  47511. }
  47512. const scope = this.componentScopeReader.getScopeForComponent(component);
  47513. if (scope === null) {
  47514. return null;
  47515. }
  47516. const dependencies = scope.kind === exports.ComponentScopeKind.NgModule
  47517. ? scope.compilation.dependencies
  47518. : scope.dependencies;
  47519. const data = {
  47520. directives: [],
  47521. pipes: [],
  47522. isPoisoned: scope.kind === exports.ComponentScopeKind.NgModule
  47523. ? scope.compilation.isPoisoned
  47524. : scope.isPoisoned,
  47525. };
  47526. const typeChecker = this.programDriver.getProgram().getTypeChecker();
  47527. for (const dep of dependencies) {
  47528. if (dep.kind === exports.MetaKind.Directive) {
  47529. const dirScope = this.scopeDataOfDirectiveMeta(typeChecker, dep);
  47530. if (dirScope === null)
  47531. continue;
  47532. data.directives.push({ ...dirScope, isInScope: true });
  47533. }
  47534. else if (dep.kind === exports.MetaKind.Pipe) {
  47535. const pipeScope = this.scopeDataOfPipeMeta(typeChecker, dep);
  47536. if (pipeScope === null)
  47537. continue;
  47538. data.pipes.push({ ...pipeScope, isInScope: true });
  47539. }
  47540. }
  47541. this.scopeCache.set(component, data);
  47542. return data;
  47543. }
  47544. scopeDataOfDirectiveMeta(typeChecker, dep) {
  47545. if (dep.selector === null) {
  47546. // Skip this directive, it can't be added to a template anyway.
  47547. return null;
  47548. }
  47549. const tsSymbol = typeChecker.getSymbolAtLocation(dep.ref.node.name);
  47550. if (!isSymbolWithValueDeclaration(tsSymbol)) {
  47551. return null;
  47552. }
  47553. let ngModule = null;
  47554. const moduleScopeOfDir = this.componentScopeReader.getScopeForComponent(dep.ref.node);
  47555. if (moduleScopeOfDir !== null && moduleScopeOfDir.kind === exports.ComponentScopeKind.NgModule) {
  47556. ngModule = moduleScopeOfDir.ngModule;
  47557. }
  47558. return {
  47559. ref: dep.ref,
  47560. isComponent: dep.isComponent,
  47561. isStructural: dep.isStructural,
  47562. selector: dep.selector,
  47563. tsSymbol,
  47564. ngModule,
  47565. };
  47566. }
  47567. scopeDataOfPipeMeta(typeChecker, dep) {
  47568. const tsSymbol = typeChecker.getSymbolAtLocation(dep.ref.node.name);
  47569. if (tsSymbol === undefined) {
  47570. return null;
  47571. }
  47572. return {
  47573. ref: dep.ref,
  47574. name: dep.name,
  47575. tsSymbol,
  47576. };
  47577. }
  47578. }
  47579. function convertDiagnostic(diag, sourceResolver) {
  47580. if (!shouldReportDiagnostic(diag)) {
  47581. return null;
  47582. }
  47583. return translateDiagnostic(diag, sourceResolver);
  47584. }
  47585. /**
  47586. * Drives a `TypeCheckContext` to generate type-checking code for every component in the program.
  47587. */
  47588. class WholeProgramTypeCheckingHost {
  47589. impl;
  47590. constructor(impl) {
  47591. this.impl = impl;
  47592. }
  47593. getSourceManager(sfPath) {
  47594. return this.impl.getFileData(sfPath).sourceManager;
  47595. }
  47596. shouldCheckClass(node) {
  47597. const sfPath = absoluteFromSourceFile(node.getSourceFile());
  47598. const shimPath = TypeCheckShimGenerator.shimFor(sfPath);
  47599. const fileData = this.impl.getFileData(sfPath);
  47600. // The component needs to be checked unless the shim which would contain it already exists.
  47601. return !fileData.shimData.has(shimPath);
  47602. }
  47603. recordShimData(sfPath, data) {
  47604. const fileData = this.impl.getFileData(sfPath);
  47605. fileData.shimData.set(data.path, data);
  47606. if (data.hasInlines) {
  47607. fileData.hasInlines = true;
  47608. }
  47609. }
  47610. recordComplete(sfPath) {
  47611. this.impl.getFileData(sfPath).isComplete = true;
  47612. }
  47613. }
  47614. /**
  47615. * Drives a `TypeCheckContext` to generate type-checking code efficiently for a single input file.
  47616. */
  47617. class SingleFileTypeCheckingHost {
  47618. sfPath;
  47619. fileData;
  47620. impl;
  47621. seenInlines = false;
  47622. constructor(sfPath, fileData, impl) {
  47623. this.sfPath = sfPath;
  47624. this.fileData = fileData;
  47625. this.impl = impl;
  47626. }
  47627. assertPath(sfPath) {
  47628. if (this.sfPath !== sfPath) {
  47629. throw new Error(`AssertionError: querying TypeCheckingHost outside of assigned file`);
  47630. }
  47631. }
  47632. getSourceManager(sfPath) {
  47633. this.assertPath(sfPath);
  47634. return this.fileData.sourceManager;
  47635. }
  47636. shouldCheckClass(node) {
  47637. if (this.sfPath !== absoluteFromSourceFile(node.getSourceFile())) {
  47638. return false;
  47639. }
  47640. const shimPath = TypeCheckShimGenerator.shimFor(this.sfPath);
  47641. // Only need to generate a TCB for the class if no shim exists for it currently.
  47642. return !this.fileData.shimData.has(shimPath);
  47643. }
  47644. recordShimData(sfPath, data) {
  47645. this.assertPath(sfPath);
  47646. // Previous type-checking state may have required the use of inlines (assuming they were
  47647. // supported). If the current operation also requires inlines, this presents a problem:
  47648. // generating new inlines may invalidate any old inlines that old state depends on.
  47649. //
  47650. // Rather than resolve this issue by tracking specific dependencies on inlines, if the new state
  47651. // relies on inlines, any old state that relied on them is simply cleared. This happens when the
  47652. // first new state that uses inlines is encountered.
  47653. if (data.hasInlines && !this.seenInlines) {
  47654. this.impl.clearAllShimDataUsingInlines();
  47655. this.seenInlines = true;
  47656. }
  47657. this.fileData.shimData.set(data.path, data);
  47658. if (data.hasInlines) {
  47659. this.fileData.hasInlines = true;
  47660. }
  47661. }
  47662. recordComplete(sfPath) {
  47663. this.assertPath(sfPath);
  47664. this.fileData.isComplete = true;
  47665. }
  47666. }
  47667. /**
  47668. * Drives a `TypeCheckContext` to generate type-checking code efficiently for only those components
  47669. * which map to a single shim of a single input file.
  47670. */
  47671. class SingleShimTypeCheckingHost extends SingleFileTypeCheckingHost {
  47672. shimPath;
  47673. constructor(sfPath, fileData, impl, shimPath) {
  47674. super(sfPath, fileData, impl);
  47675. this.shimPath = shimPath;
  47676. }
  47677. shouldCheckNode(node) {
  47678. if (this.sfPath !== absoluteFromSourceFile(node.getSourceFile())) {
  47679. return false;
  47680. }
  47681. // Only generate a TCB for the component if it maps to the requested shim file.
  47682. const shimPath = TypeCheckShimGenerator.shimFor(this.sfPath);
  47683. if (shimPath !== this.shimPath) {
  47684. return false;
  47685. }
  47686. // Only need to generate a TCB for the class if no shim exists for it currently.
  47687. return !this.fileData.shimData.has(shimPath);
  47688. }
  47689. }
  47690. exports.AST = AST;
  47691. exports.ASTWithSource = ASTWithSource;
  47692. exports.AbsoluteModuleStrategy = AbsoluteModuleStrategy;
  47693. exports.ArrowFunctionExpr = ArrowFunctionExpr;
  47694. exports.Binary = Binary;
  47695. exports.BlockPlaceholder = BlockPlaceholder;
  47696. exports.BoundAttribute = BoundAttribute;
  47697. exports.BoundDeferredTrigger = BoundDeferredTrigger;
  47698. exports.BoundEvent = BoundEvent;
  47699. exports.COMPILER_ERRORS_WITH_GUIDES = COMPILER_ERRORS_WITH_GUIDES;
  47700. exports.CR = CR;
  47701. exports.CUSTOM_ELEMENTS_SCHEMA = CUSTOM_ELEMENTS_SCHEMA;
  47702. exports.Call = Call;
  47703. exports.Chain = Chain;
  47704. exports.ClassPropertyMapping = ClassPropertyMapping;
  47705. exports.CloneVisitor = CloneVisitor;
  47706. exports.CompoundMetadataReader = CompoundMetadataReader;
  47707. exports.Conditional = Conditional;
  47708. exports.ConstantPool = ConstantPool;
  47709. exports.Container = Container;
  47710. exports.CssSelector = CssSelector;
  47711. exports.DEFAULT_INTERPOLATION_CONFIG = DEFAULT_INTERPOLATION_CONFIG;
  47712. exports.DYNAMIC_TYPE = DYNAMIC_TYPE;
  47713. exports.Declaration = Declaration;
  47714. exports.DeclareFunctionStmt = DeclareFunctionStmt;
  47715. exports.DeclareVarStmt = DeclareVarStmt;
  47716. exports.DefaultImportTracker = DefaultImportTracker;
  47717. exports.DefinitionMap = DefinitionMap;
  47718. exports.DomElementSchemaRegistry = DomElementSchemaRegistry;
  47719. exports.DynamicImportExpr = DynamicImportExpr;
  47720. exports.DynamicValue = DynamicValue;
  47721. exports.Element = Element;
  47722. exports.Element$1 = Element$1;
  47723. exports.EnumValue = EnumValue;
  47724. exports.ExternalExpr = ExternalExpr;
  47725. exports.FatalDiagnosticError = FatalDiagnosticError;
  47726. exports.FnParam = FnParam;
  47727. exports.FunctionExpr = FunctionExpr;
  47728. exports.HtmlParser = HtmlParser;
  47729. exports.I18nError = I18nError;
  47730. exports.INPUT_INITIALIZER_FN = INPUT_INITIALIZER_FN;
  47731. exports.Icu = Icu;
  47732. exports.IcuPlaceholder = IcuPlaceholder;
  47733. exports.Identifiers = Identifiers;
  47734. exports.ImplicitReceiver = ImplicitReceiver;
  47735. exports.ImportManager = ImportManager;
  47736. exports.Interpolation = Interpolation$1;
  47737. exports.InterpolationConfig = InterpolationConfig;
  47738. exports.InvokeFunctionExpr = InvokeFunctionExpr;
  47739. exports.LetDeclaration = LetDeclaration$1;
  47740. exports.LiteralArrayExpr = LiteralArrayExpr;
  47741. exports.LiteralExpr = LiteralExpr;
  47742. exports.LocalIdentifierStrategy = LocalIdentifierStrategy;
  47743. exports.LogicalFileSystem = LogicalFileSystem;
  47744. exports.LogicalProjectStrategy = LogicalProjectStrategy;
  47745. exports.MODEL_INITIALIZER_FN = MODEL_INITIALIZER_FN;
  47746. exports.Message = Message;
  47747. exports.NO_ERRORS_SCHEMA = NO_ERRORS_SCHEMA;
  47748. exports.NULL_EXPR = NULL_EXPR;
  47749. exports.NgOriginalFile = NgOriginalFile;
  47750. exports.NodeJSFileSystem = NodeJSFileSystem;
  47751. exports.OUTPUT_INITIALIZER_FNS = OUTPUT_INITIALIZER_FNS;
  47752. exports.ParseLocation = ParseLocation;
  47753. exports.ParseSourceFile = ParseSourceFile;
  47754. exports.ParseSourceSpan = ParseSourceSpan;
  47755. exports.Parser = Parser$1;
  47756. exports.Placeholder = Placeholder;
  47757. exports.PropertyRead = PropertyRead;
  47758. exports.PropertyWrite = PropertyWrite;
  47759. exports.QUERY_INITIALIZER_FNS = QUERY_INITIALIZER_FNS;
  47760. exports.R3TargetBinder = R3TargetBinder;
  47761. exports.ReadVarExpr = ReadVarExpr;
  47762. exports.RecursiveAstVisitor = RecursiveAstVisitor;
  47763. exports.RecursiveAstVisitor$1 = RecursiveAstVisitor$1;
  47764. exports.RecursiveVisitor = RecursiveVisitor;
  47765. exports.RecursiveVisitor$1 = RecursiveVisitor$1;
  47766. exports.Reference = Reference;
  47767. exports.Reference$1 = Reference$1;
  47768. exports.ReferenceEmitter = ReferenceEmitter;
  47769. exports.RelativePathStrategy = RelativePathStrategy;
  47770. exports.ReturnStatement = ReturnStatement;
  47771. exports.SafeCall = SafeCall;
  47772. exports.SafeKeyedRead = SafeKeyedRead;
  47773. exports.SafePropertyRead = SafePropertyRead;
  47774. exports.SelectorMatcher = SelectorMatcher;
  47775. exports.Serializer = Serializer;
  47776. exports.StaticInterpreter = StaticInterpreter;
  47777. exports.SyntheticValue = SyntheticValue;
  47778. exports.Tag = Tag;
  47779. exports.TagPlaceholder = TagPlaceholder;
  47780. exports.Template = Template;
  47781. exports.TemplateTypeCheckerImpl = TemplateTypeCheckerImpl;
  47782. exports.Text = Text;
  47783. exports.Text$1 = Text$1;
  47784. exports.Text$2 = Text$2;
  47785. exports.TextAttribute = TextAttribute;
  47786. exports.ThisReceiver = ThisReceiver;
  47787. exports.Trait = Trait;
  47788. exports.TypeCheckShimGenerator = TypeCheckShimGenerator;
  47789. exports.TypeScriptReflectionHost = TypeScriptReflectionHost;
  47790. exports.UNSAFE_OBJECT_KEY_NAME_REGEXP = UNSAFE_OBJECT_KEY_NAME_REGEXP;
  47791. exports.UnifiedModulesStrategy = UnifiedModulesStrategy;
  47792. exports.Variable = Variable;
  47793. exports.Version = Version;
  47794. exports.WhitespaceVisitor = WhitespaceVisitor;
  47795. exports.WrappedNodeExpr = WrappedNodeExpr;
  47796. exports.Xmb = Xmb;
  47797. exports.absoluteFrom = absoluteFrom;
  47798. exports.absoluteFromSourceFile = absoluteFromSourceFile;
  47799. exports.arrowFn = arrowFn;
  47800. exports.asLiteral = asLiteral;
  47801. exports.assertLocalCompilationUnresolvedConst = assertLocalCompilationUnresolvedConst;
  47802. exports.assertSuccessfulReferenceEmit = assertSuccessfulReferenceEmit;
  47803. exports.checkInheritanceOfInjectable = checkInheritanceOfInjectable;
  47804. exports.combineResolvers = combineResolvers;
  47805. exports.compileComponentFromMetadata = compileComponentFromMetadata;
  47806. exports.compileDeferResolverFunction = compileDeferResolverFunction;
  47807. exports.compileDirectiveFromMetadata = compileDirectiveFromMetadata;
  47808. exports.compileFactoryFunction = compileFactoryFunction;
  47809. exports.compileInjectable = compileInjectable;
  47810. exports.compileInjector = compileInjector;
  47811. exports.compileNgModule = compileNgModule;
  47812. exports.compilePipeFromMetadata = compilePipeFromMetadata;
  47813. exports.compileResults = compileResults;
  47814. exports.conditionallyCreateDirectiveBindingLiteral = conditionallyCreateDirectiveBindingLiteral;
  47815. exports.convertFromMaybeForwardRefExpression = convertFromMaybeForwardRefExpression;
  47816. exports.copyFileShimData = copyFileShimData;
  47817. exports.createComponentType = createComponentType;
  47818. exports.createDirectiveType = createDirectiveType;
  47819. exports.createFactoryType = createFactoryType;
  47820. exports.createForwardRefResolver = createForwardRefResolver;
  47821. exports.createHostDirectivesMappingArray = createHostDirectivesMappingArray;
  47822. exports.createInjectableType = createInjectableType;
  47823. exports.createInjectorType = createInjectorType;
  47824. exports.createMayBeForwardRefExpression = createMayBeForwardRefExpression;
  47825. exports.createNgModuleType = createNgModuleType;
  47826. exports.createPipeType = createPipeType;
  47827. exports.createValueHasWrongTypeError = createValueHasWrongTypeError;
  47828. exports.decimalDigest = decimalDigest;
  47829. exports.devOnlyGuardedExpression = devOnlyGuardedExpression;
  47830. exports.digest = digest$1;
  47831. exports.dirname = dirname;
  47832. exports.entityNameToValue = entityNameToValue;
  47833. exports.extraReferenceFromTypeQuery = extraReferenceFromTypeQuery;
  47834. exports.extractDecoratorQueryMetadata = extractDecoratorQueryMetadata;
  47835. exports.extractDirectiveMetadata = extractDirectiveMetadata;
  47836. exports.extractDirectiveTypeCheckMeta = extractDirectiveTypeCheckMeta;
  47837. exports.extractMessages = extractMessages;
  47838. exports.extractReferencesFromType = extractReferencesFromType;
  47839. exports.findAngularDecorator = findAngularDecorator;
  47840. exports.flattenInheritedDirectiveMetadata = flattenInheritedDirectiveMetadata;
  47841. exports.generateForwardRef = generateForwardRef;
  47842. exports.getAngularDecorators = getAngularDecorators;
  47843. exports.getConstructorDependencies = getConstructorDependencies;
  47844. exports.getContainingImportDeclaration = getContainingImportDeclaration;
  47845. exports.getDefaultImportDeclaration = getDefaultImportDeclaration;
  47846. exports.getDirectiveDiagnostics = getDirectiveDiagnostics;
  47847. exports.getFileSystem = getFileSystem;
  47848. exports.getOriginNodeForDiagnostics = getOriginNodeForDiagnostics;
  47849. exports.getProviderDiagnostics = getProviderDiagnostics;
  47850. exports.getRootDirs = getRootDirs;
  47851. exports.getSourceFile = getSourceFile;
  47852. exports.getSourceFileOrNull = getSourceFileOrNull;
  47853. exports.getTemplateDiagnostics = getTemplateDiagnostics;
  47854. exports.getUndecoratedClassWithAngularFeaturesDiagnostic = getUndecoratedClassWithAngularFeaturesDiagnostic;
  47855. exports.getValidConstructorDependencies = getValidConstructorDependencies;
  47856. exports.hasInjectableFields = hasInjectableFields;
  47857. exports.identifierOfNode = identifierOfNode;
  47858. exports.importExpr = importExpr;
  47859. exports.isAbstractClassDeclaration = isAbstractClassDeclaration;
  47860. exports.isAliasImportDeclaration = isAliasImportDeclaration;
  47861. exports.isAngularCore = isAngularCore;
  47862. exports.isAngularCoreReferenceWithPotentialAliasing = isAngularCoreReferenceWithPotentialAliasing;
  47863. exports.isAngularDecorator = isAngularDecorator;
  47864. exports.isDtsPath = isDtsPath;
  47865. exports.isExpressionForwardReference = isExpressionForwardReference;
  47866. exports.isFatalDiagnosticError = isFatalDiagnosticError;
  47867. exports.isFileShimSourceFile = isFileShimSourceFile;
  47868. exports.isHostDirectiveMetaForGlobalMode = isHostDirectiveMetaForGlobalMode;
  47869. exports.isLocalRelativePath = isLocalRelativePath;
  47870. exports.isNamedClassDeclaration = isNamedClassDeclaration;
  47871. exports.isNonDeclarationTsPath = isNonDeclarationTsPath;
  47872. exports.isShim = isShim;
  47873. exports.join = join;
  47874. exports.literal = literal$1;
  47875. exports.literalArr = literalArr;
  47876. exports.literalMap = literalMap;
  47877. exports.loadIsReferencedAliasDeclarationPatch = loadIsReferencedAliasDeclarationPatch;
  47878. exports.makeBindingParser = makeBindingParser;
  47879. exports.makeDiagnostic = makeDiagnostic;
  47880. exports.makeDuplicateDeclarationError = makeDuplicateDeclarationError;
  47881. exports.makeRelatedInformation = makeRelatedInformation;
  47882. exports.mapLiteral = mapLiteral;
  47883. exports.ngErrorCode = ngErrorCode;
  47884. exports.nodeDebugInfo = nodeDebugInfo;
  47885. exports.nodeNameForError = nodeNameForError;
  47886. exports.parseDecoratorInputTransformFunction = parseDecoratorInputTransformFunction;
  47887. exports.parseDirectiveStyles = parseDirectiveStyles;
  47888. exports.parseTemplate = parseTemplate;
  47889. exports.presetImportManagerForceNamespaceImports = presetImportManagerForceNamespaceImports;
  47890. exports.queryDecoratorNames = queryDecoratorNames;
  47891. exports.readBaseClass = readBaseClass;
  47892. exports.readBooleanType = readBooleanType;
  47893. exports.readMapType = readMapType;
  47894. exports.readStringArrayType = readStringArrayType;
  47895. exports.readStringType = readStringType;
  47896. exports.reflectClassMember = reflectClassMember;
  47897. exports.reflectObjectLiteral = reflectObjectLiteral;
  47898. exports.refsToArray = refsToArray;
  47899. exports.relative = relative;
  47900. exports.resolve = resolve;
  47901. exports.resolveImportedFile = resolveImportedFile;
  47902. exports.resolveModuleName = resolveModuleName;
  47903. exports.resolveProvidersRequiringFactory = resolveProvidersRequiringFactory;
  47904. exports.retagAllTsFiles = retagAllTsFiles;
  47905. exports.serialize = serialize$1;
  47906. exports.setFileSystem = setFileSystem;
  47907. exports.sfExtensionData = sfExtensionData;
  47908. exports.stripExtension = stripExtension;
  47909. exports.toFactoryMetadata = toFactoryMetadata;
  47910. exports.toR3Reference = toR3Reference;
  47911. exports.toRelativeImport = toRelativeImport;
  47912. exports.toUnredirectedSourceFile = toUnredirectedSourceFile;
  47913. exports.translateExpression = translateExpression;
  47914. exports.translateStatement = translateStatement;
  47915. exports.translateType = translateType;
  47916. exports.transplantedType = transplantedType;
  47917. exports.tryParseInitializerApi = tryParseInitializerApi;
  47918. exports.tryParseInitializerBasedOutput = tryParseInitializerBasedOutput;
  47919. exports.tryParseSignalInputMapping = tryParseSignalInputMapping;
  47920. exports.tryParseSignalModelMapping = tryParseSignalModelMapping;
  47921. exports.tryParseSignalQueryFromInitializer = tryParseSignalQueryFromInitializer;
  47922. exports.tryUnwrapForwardRef = tryUnwrapForwardRef;
  47923. exports.typeNodeToValueExpr = typeNodeToValueExpr;
  47924. exports.untagAllTsFiles = untagAllTsFiles;
  47925. exports.unwrapConstructorDependencies = unwrapConstructorDependencies;
  47926. exports.unwrapExpression = unwrapExpression;
  47927. exports.validateConstructorDependencies = validateConstructorDependencies;
  47928. exports.validateHostDirectives = validateHostDirectives;
  47929. exports.valueReferenceToExpression = valueReferenceToExpression;
  47930. exports.variable = variable;
  47931. exports.visitAll = visitAll;
  47932. exports.visitAll$1 = visitAll$1;
  47933. exports.visitAllWithSiblings = visitAllWithSiblings;
  47934. exports.wrapFunctionExpressionsInParens = wrapFunctionExpressionsInParens;
  47935. exports.wrapTypeReference = wrapTypeReference;