compiler.js 1.2 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780
  1. /**
  2. * @license Angular v12.0.5
  3. * (c) 2010-2021 Google LLC. https://angular.io/
  4. * License: MIT
  5. */
  6. /**
  7. * @license
  8. * Copyright Google LLC All Rights Reserved.
  9. *
  10. * Use of this source code is governed by an MIT-style license that can be
  11. * found in the LICENSE file at https://angular.io/license
  12. */
  13. var TagContentType;
  14. (function (TagContentType) {
  15. TagContentType[TagContentType["RAW_TEXT"] = 0] = "RAW_TEXT";
  16. TagContentType[TagContentType["ESCAPABLE_RAW_TEXT"] = 1] = "ESCAPABLE_RAW_TEXT";
  17. TagContentType[TagContentType["PARSABLE_DATA"] = 2] = "PARSABLE_DATA";
  18. })(TagContentType || (TagContentType = {}));
  19. function splitNsName(elementName) {
  20. if (elementName[0] != ':') {
  21. return [null, elementName];
  22. }
  23. const colonIndex = elementName.indexOf(':', 1);
  24. if (colonIndex == -1) {
  25. throw new Error(`Unsupported format "${elementName}" expecting ":namespace:name"`);
  26. }
  27. return [elementName.slice(1, colonIndex), elementName.slice(colonIndex + 1)];
  28. }
  29. // `<ng-container>` tags work the same regardless the namespace
  30. function isNgContainer(tagName) {
  31. return splitNsName(tagName)[1] === 'ng-container';
  32. }
  33. // `<ng-content>` tags work the same regardless the namespace
  34. function isNgContent(tagName) {
  35. return splitNsName(tagName)[1] === 'ng-content';
  36. }
  37. // `<ng-template>` tags work the same regardless the namespace
  38. function isNgTemplate(tagName) {
  39. return splitNsName(tagName)[1] === 'ng-template';
  40. }
  41. function getNsPrefix(fullName) {
  42. return fullName === null ? null : splitNsName(fullName)[0];
  43. }
  44. function mergeNsAndName(prefix, localName) {
  45. return prefix ? `:${prefix}:${localName}` : localName;
  46. }
  47. // see https://www.w3.org/TR/html51/syntax.html#named-character-references
  48. // see https://html.spec.whatwg.org/multipage/entities.json
  49. // This list is not exhaustive to keep the compiler footprint low.
  50. // The `&#123;` / `&#x1ab;` syntax should be used when the named character reference does not
  51. // exist.
  52. const NAMED_ENTITIES = {
  53. 'Aacute': '\u00C1',
  54. 'aacute': '\u00E1',
  55. 'Acirc': '\u00C2',
  56. 'acirc': '\u00E2',
  57. 'acute': '\u00B4',
  58. 'AElig': '\u00C6',
  59. 'aelig': '\u00E6',
  60. 'Agrave': '\u00C0',
  61. 'agrave': '\u00E0',
  62. 'alefsym': '\u2135',
  63. 'Alpha': '\u0391',
  64. 'alpha': '\u03B1',
  65. 'amp': '&',
  66. 'and': '\u2227',
  67. 'ang': '\u2220',
  68. 'apos': '\u0027',
  69. 'Aring': '\u00C5',
  70. 'aring': '\u00E5',
  71. 'asymp': '\u2248',
  72. 'Atilde': '\u00C3',
  73. 'atilde': '\u00E3',
  74. 'Auml': '\u00C4',
  75. 'auml': '\u00E4',
  76. 'bdquo': '\u201E',
  77. 'Beta': '\u0392',
  78. 'beta': '\u03B2',
  79. 'brvbar': '\u00A6',
  80. 'bull': '\u2022',
  81. 'cap': '\u2229',
  82. 'Ccedil': '\u00C7',
  83. 'ccedil': '\u00E7',
  84. 'cedil': '\u00B8',
  85. 'cent': '\u00A2',
  86. 'Chi': '\u03A7',
  87. 'chi': '\u03C7',
  88. 'circ': '\u02C6',
  89. 'clubs': '\u2663',
  90. 'cong': '\u2245',
  91. 'copy': '\u00A9',
  92. 'crarr': '\u21B5',
  93. 'cup': '\u222A',
  94. 'curren': '\u00A4',
  95. 'dagger': '\u2020',
  96. 'Dagger': '\u2021',
  97. 'darr': '\u2193',
  98. 'dArr': '\u21D3',
  99. 'deg': '\u00B0',
  100. 'Delta': '\u0394',
  101. 'delta': '\u03B4',
  102. 'diams': '\u2666',
  103. 'divide': '\u00F7',
  104. 'Eacute': '\u00C9',
  105. 'eacute': '\u00E9',
  106. 'Ecirc': '\u00CA',
  107. 'ecirc': '\u00EA',
  108. 'Egrave': '\u00C8',
  109. 'egrave': '\u00E8',
  110. 'empty': '\u2205',
  111. 'emsp': '\u2003',
  112. 'ensp': '\u2002',
  113. 'Epsilon': '\u0395',
  114. 'epsilon': '\u03B5',
  115. 'equiv': '\u2261',
  116. 'Eta': '\u0397',
  117. 'eta': '\u03B7',
  118. 'ETH': '\u00D0',
  119. 'eth': '\u00F0',
  120. 'Euml': '\u00CB',
  121. 'euml': '\u00EB',
  122. 'euro': '\u20AC',
  123. 'exist': '\u2203',
  124. 'fnof': '\u0192',
  125. 'forall': '\u2200',
  126. 'frac12': '\u00BD',
  127. 'frac14': '\u00BC',
  128. 'frac34': '\u00BE',
  129. 'frasl': '\u2044',
  130. 'Gamma': '\u0393',
  131. 'gamma': '\u03B3',
  132. 'ge': '\u2265',
  133. 'gt': '>',
  134. 'harr': '\u2194',
  135. 'hArr': '\u21D4',
  136. 'hearts': '\u2665',
  137. 'hellip': '\u2026',
  138. 'Iacute': '\u00CD',
  139. 'iacute': '\u00ED',
  140. 'Icirc': '\u00CE',
  141. 'icirc': '\u00EE',
  142. 'iexcl': '\u00A1',
  143. 'Igrave': '\u00CC',
  144. 'igrave': '\u00EC',
  145. 'image': '\u2111',
  146. 'infin': '\u221E',
  147. 'int': '\u222B',
  148. 'Iota': '\u0399',
  149. 'iota': '\u03B9',
  150. 'iquest': '\u00BF',
  151. 'isin': '\u2208',
  152. 'Iuml': '\u00CF',
  153. 'iuml': '\u00EF',
  154. 'Kappa': '\u039A',
  155. 'kappa': '\u03BA',
  156. 'Lambda': '\u039B',
  157. 'lambda': '\u03BB',
  158. 'lang': '\u27E8',
  159. 'laquo': '\u00AB',
  160. 'larr': '\u2190',
  161. 'lArr': '\u21D0',
  162. 'lceil': '\u2308',
  163. 'ldquo': '\u201C',
  164. 'le': '\u2264',
  165. 'lfloor': '\u230A',
  166. 'lowast': '\u2217',
  167. 'loz': '\u25CA',
  168. 'lrm': '\u200E',
  169. 'lsaquo': '\u2039',
  170. 'lsquo': '\u2018',
  171. 'lt': '<',
  172. 'macr': '\u00AF',
  173. 'mdash': '\u2014',
  174. 'micro': '\u00B5',
  175. 'middot': '\u00B7',
  176. 'minus': '\u2212',
  177. 'Mu': '\u039C',
  178. 'mu': '\u03BC',
  179. 'nabla': '\u2207',
  180. 'nbsp': '\u00A0',
  181. 'ndash': '\u2013',
  182. 'ne': '\u2260',
  183. 'ni': '\u220B',
  184. 'not': '\u00AC',
  185. 'notin': '\u2209',
  186. 'nsub': '\u2284',
  187. 'Ntilde': '\u00D1',
  188. 'ntilde': '\u00F1',
  189. 'Nu': '\u039D',
  190. 'nu': '\u03BD',
  191. 'Oacute': '\u00D3',
  192. 'oacute': '\u00F3',
  193. 'Ocirc': '\u00D4',
  194. 'ocirc': '\u00F4',
  195. 'OElig': '\u0152',
  196. 'oelig': '\u0153',
  197. 'Ograve': '\u00D2',
  198. 'ograve': '\u00F2',
  199. 'oline': '\u203E',
  200. 'Omega': '\u03A9',
  201. 'omega': '\u03C9',
  202. 'Omicron': '\u039F',
  203. 'omicron': '\u03BF',
  204. 'oplus': '\u2295',
  205. 'or': '\u2228',
  206. 'ordf': '\u00AA',
  207. 'ordm': '\u00BA',
  208. 'Oslash': '\u00D8',
  209. 'oslash': '\u00F8',
  210. 'Otilde': '\u00D5',
  211. 'otilde': '\u00F5',
  212. 'otimes': '\u2297',
  213. 'Ouml': '\u00D6',
  214. 'ouml': '\u00F6',
  215. 'para': '\u00B6',
  216. 'permil': '\u2030',
  217. 'perp': '\u22A5',
  218. 'Phi': '\u03A6',
  219. 'phi': '\u03C6',
  220. 'Pi': '\u03A0',
  221. 'pi': '\u03C0',
  222. 'piv': '\u03D6',
  223. 'plusmn': '\u00B1',
  224. 'pound': '\u00A3',
  225. 'prime': '\u2032',
  226. 'Prime': '\u2033',
  227. 'prod': '\u220F',
  228. 'prop': '\u221D',
  229. 'Psi': '\u03A8',
  230. 'psi': '\u03C8',
  231. 'quot': '\u0022',
  232. 'radic': '\u221A',
  233. 'rang': '\u27E9',
  234. 'raquo': '\u00BB',
  235. 'rarr': '\u2192',
  236. 'rArr': '\u21D2',
  237. 'rceil': '\u2309',
  238. 'rdquo': '\u201D',
  239. 'real': '\u211C',
  240. 'reg': '\u00AE',
  241. 'rfloor': '\u230B',
  242. 'Rho': '\u03A1',
  243. 'rho': '\u03C1',
  244. 'rlm': '\u200F',
  245. 'rsaquo': '\u203A',
  246. 'rsquo': '\u2019',
  247. 'sbquo': '\u201A',
  248. 'Scaron': '\u0160',
  249. 'scaron': '\u0161',
  250. 'sdot': '\u22C5',
  251. 'sect': '\u00A7',
  252. 'shy': '\u00AD',
  253. 'Sigma': '\u03A3',
  254. 'sigma': '\u03C3',
  255. 'sigmaf': '\u03C2',
  256. 'sim': '\u223C',
  257. 'spades': '\u2660',
  258. 'sub': '\u2282',
  259. 'sube': '\u2286',
  260. 'sum': '\u2211',
  261. 'sup': '\u2283',
  262. 'sup1': '\u00B9',
  263. 'sup2': '\u00B2',
  264. 'sup3': '\u00B3',
  265. 'supe': '\u2287',
  266. 'szlig': '\u00DF',
  267. 'Tau': '\u03A4',
  268. 'tau': '\u03C4',
  269. 'there4': '\u2234',
  270. 'Theta': '\u0398',
  271. 'theta': '\u03B8',
  272. 'thetasym': '\u03D1',
  273. 'thinsp': '\u2009',
  274. 'THORN': '\u00DE',
  275. 'thorn': '\u00FE',
  276. 'tilde': '\u02DC',
  277. 'times': '\u00D7',
  278. 'trade': '\u2122',
  279. 'Uacute': '\u00DA',
  280. 'uacute': '\u00FA',
  281. 'uarr': '\u2191',
  282. 'uArr': '\u21D1',
  283. 'Ucirc': '\u00DB',
  284. 'ucirc': '\u00FB',
  285. 'Ugrave': '\u00D9',
  286. 'ugrave': '\u00F9',
  287. 'uml': '\u00A8',
  288. 'upsih': '\u03D2',
  289. 'Upsilon': '\u03A5',
  290. 'upsilon': '\u03C5',
  291. 'Uuml': '\u00DC',
  292. 'uuml': '\u00FC',
  293. 'weierp': '\u2118',
  294. 'Xi': '\u039E',
  295. 'xi': '\u03BE',
  296. 'Yacute': '\u00DD',
  297. 'yacute': '\u00FD',
  298. 'yen': '\u00A5',
  299. 'yuml': '\u00FF',
  300. 'Yuml': '\u0178',
  301. 'Zeta': '\u0396',
  302. 'zeta': '\u03B6',
  303. 'zwj': '\u200D',
  304. 'zwnj': '\u200C',
  305. };
  306. // The &ngsp; pseudo-entity is denoting a space. see:
  307. // https://github.com/dart-lang/angular/blob/0bb611387d29d65b5af7f9d2515ab571fd3fbee4/_tests/test/compiler/preserve_whitespace_test.dart
  308. const NGSP_UNICODE = '\uE500';
  309. NAMED_ENTITIES['ngsp'] = NGSP_UNICODE;
  310. /**
  311. * @license
  312. * Copyright Google LLC All Rights Reserved.
  313. *
  314. * Use of this source code is governed by an MIT-style license that can be
  315. * found in the LICENSE file at https://angular.io/license
  316. */
  317. class HtmlTagDefinition {
  318. constructor({ closedByChildren, implicitNamespacePrefix, contentType = TagContentType.PARSABLE_DATA, closedByParent = false, isVoid = false, ignoreFirstLf = false, preventNamespaceInheritance = false } = {}) {
  319. this.closedByChildren = {};
  320. this.closedByParent = false;
  321. this.canSelfClose = false;
  322. if (closedByChildren && closedByChildren.length > 0) {
  323. closedByChildren.forEach(tagName => this.closedByChildren[tagName] = true);
  324. }
  325. this.isVoid = isVoid;
  326. this.closedByParent = closedByParent || isVoid;
  327. this.implicitNamespacePrefix = implicitNamespacePrefix || null;
  328. this.contentType = contentType;
  329. this.ignoreFirstLf = ignoreFirstLf;
  330. this.preventNamespaceInheritance = preventNamespaceInheritance;
  331. }
  332. isClosedByChild(name) {
  333. return this.isVoid || name.toLowerCase() in this.closedByChildren;
  334. }
  335. getContentType(prefix) {
  336. if (typeof this.contentType === 'object') {
  337. const overrideType = prefix == null ? undefined : this.contentType[prefix];
  338. return overrideType !== null && overrideType !== void 0 ? overrideType : this.contentType.default;
  339. }
  340. return this.contentType;
  341. }
  342. }
  343. let _DEFAULT_TAG_DEFINITION;
  344. // see https://www.w3.org/TR/html51/syntax.html#optional-tags
  345. // This implementation does not fully conform to the HTML5 spec.
  346. let TAG_DEFINITIONS;
  347. function getHtmlTagDefinition(tagName) {
  348. var _a, _b;
  349. if (!TAG_DEFINITIONS) {
  350. _DEFAULT_TAG_DEFINITION = new HtmlTagDefinition();
  351. TAG_DEFINITIONS = {
  352. 'base': new HtmlTagDefinition({ isVoid: true }),
  353. 'meta': new HtmlTagDefinition({ isVoid: true }),
  354. 'area': new HtmlTagDefinition({ isVoid: true }),
  355. 'embed': new HtmlTagDefinition({ isVoid: true }),
  356. 'link': new HtmlTagDefinition({ isVoid: true }),
  357. 'img': new HtmlTagDefinition({ isVoid: true }),
  358. 'input': new HtmlTagDefinition({ isVoid: true }),
  359. 'param': new HtmlTagDefinition({ isVoid: true }),
  360. 'hr': new HtmlTagDefinition({ isVoid: true }),
  361. 'br': new HtmlTagDefinition({ isVoid: true }),
  362. 'source': new HtmlTagDefinition({ isVoid: true }),
  363. 'track': new HtmlTagDefinition({ isVoid: true }),
  364. 'wbr': new HtmlTagDefinition({ isVoid: true }),
  365. 'p': new HtmlTagDefinition({
  366. closedByChildren: [
  367. 'address', 'article', 'aside', 'blockquote', 'div', 'dl', 'fieldset',
  368. 'footer', 'form', 'h1', 'h2', 'h3', 'h4', 'h5',
  369. 'h6', 'header', 'hgroup', 'hr', 'main', 'nav', 'ol',
  370. 'p', 'pre', 'section', 'table', 'ul'
  371. ],
  372. closedByParent: true
  373. }),
  374. 'thead': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'] }),
  375. 'tbody': new HtmlTagDefinition({ closedByChildren: ['tbody', 'tfoot'], closedByParent: true }),
  376. 'tfoot': new HtmlTagDefinition({ closedByChildren: ['tbody'], closedByParent: true }),
  377. 'tr': new HtmlTagDefinition({ closedByChildren: ['tr'], closedByParent: true }),
  378. 'td': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  379. 'th': new HtmlTagDefinition({ closedByChildren: ['td', 'th'], closedByParent: true }),
  380. 'col': new HtmlTagDefinition({ isVoid: true }),
  381. 'svg': new HtmlTagDefinition({ implicitNamespacePrefix: 'svg' }),
  382. 'foreignObject': new HtmlTagDefinition({
  383. // Usually the implicit namespace here would be redundant since it will be inherited from
  384. // the parent `svg`, but we have to do it for `foreignObject`, because the way the parser
  385. // works is that the parent node of an end tag is its own start tag which means that
  386. // the `preventNamespaceInheritance` on `foreignObject` would have it default to the
  387. // implicit namespace which is `html`, unless specified otherwise.
  388. implicitNamespacePrefix: 'svg',
  389. // We want to prevent children of foreignObject from inheriting its namespace, because
  390. // the point of the element is to allow nodes from other namespaces to be inserted.
  391. preventNamespaceInheritance: true,
  392. }),
  393. 'math': new HtmlTagDefinition({ implicitNamespacePrefix: 'math' }),
  394. 'li': new HtmlTagDefinition({ closedByChildren: ['li'], closedByParent: true }),
  395. 'dt': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'] }),
  396. 'dd': new HtmlTagDefinition({ closedByChildren: ['dt', 'dd'], closedByParent: true }),
  397. 'rb': new HtmlTagDefinition({ closedByChildren: ['rb', 'rt', 'rtc', 'rp'], closedByParent: true }),
  398. 'rt': new HtmlTagDefinition({ closedByChildren: ['rb', 'rt', 'rtc', 'rp'], closedByParent: true }),
  399. 'rtc': new HtmlTagDefinition({ closedByChildren: ['rb', 'rtc', 'rp'], closedByParent: true }),
  400. 'rp': new HtmlTagDefinition({ closedByChildren: ['rb', 'rt', 'rtc', 'rp'], closedByParent: true }),
  401. 'optgroup': new HtmlTagDefinition({ closedByChildren: ['optgroup'], closedByParent: true }),
  402. 'option': new HtmlTagDefinition({ closedByChildren: ['option', 'optgroup'], closedByParent: true }),
  403. 'pre': new HtmlTagDefinition({ ignoreFirstLf: true }),
  404. 'listing': new HtmlTagDefinition({ ignoreFirstLf: true }),
  405. 'style': new HtmlTagDefinition({ contentType: TagContentType.RAW_TEXT }),
  406. 'script': new HtmlTagDefinition({ contentType: TagContentType.RAW_TEXT }),
  407. 'title': new HtmlTagDefinition({
  408. // The browser supports two separate `title` tags which have to use
  409. // a different content type: `HTMLTitleElement` and `SVGTitleElement`
  410. contentType: { default: TagContentType.ESCAPABLE_RAW_TEXT, svg: TagContentType.PARSABLE_DATA }
  411. }),
  412. 'textarea': new HtmlTagDefinition({ contentType: TagContentType.ESCAPABLE_RAW_TEXT, ignoreFirstLf: true }),
  413. };
  414. }
  415. // We have to make both a case-sensitive and a case-insesitive lookup, because
  416. // HTML tag names are case insensitive, whereas some SVG tags are case sensitive.
  417. return (_b = (_a = TAG_DEFINITIONS[tagName]) !== null && _a !== void 0 ? _a : TAG_DEFINITIONS[tagName.toLowerCase()]) !== null && _b !== void 0 ? _b : _DEFAULT_TAG_DEFINITION;
  418. }
  419. /**
  420. * @license
  421. * Copyright Google LLC All Rights Reserved.
  422. *
  423. * Use of this source code is governed by an MIT-style license that can be
  424. * found in the LICENSE file at https://angular.io/license
  425. */
  426. const _SELECTOR_REGEXP = new RegExp('(\\:not\\()|' + // 1: ":not("
  427. '(([\\.\\#]?)[-\\w]+)|' + // 2: "tag"; 3: "."/"#";
  428. // "-" should appear first in the regexp below as FF31 parses "[.-\w]" as a range
  429. // 4: attribute; 5: attribute_string; 6: attribute_value
  430. '(?:\\[([-.\\w*]+)(?:=([\"\']?)([^\\]\"\']*)\\5)?\\])|' + // "[name]", "[name=value]",
  431. // "[name="value"]",
  432. // "[name='value']"
  433. '(\\))|' + // 7: ")"
  434. '(\\s*,\\s*)', // 8: ","
  435. 'g');
  436. /**
  437. * A css selector contains an element name,
  438. * css classes and attribute/value pairs with the purpose
  439. * of selecting subsets out of them.
  440. */
  441. class CssSelector {
  442. constructor() {
  443. this.element = null;
  444. this.classNames = [];
  445. /**
  446. * The selectors are encoded in pairs where:
  447. * - even locations are attribute names
  448. * - odd locations are attribute values.
  449. *
  450. * Example:
  451. * Selector: `[key1=value1][key2]` would parse to:
  452. * ```
  453. * ['key1', 'value1', 'key2', '']
  454. * ```
  455. */
  456. this.attrs = [];
  457. this.notSelectors = [];
  458. }
  459. static parse(selector) {
  460. const results = [];
  461. const _addResult = (res, cssSel) => {
  462. if (cssSel.notSelectors.length > 0 && !cssSel.element && cssSel.classNames.length == 0 &&
  463. cssSel.attrs.length == 0) {
  464. cssSel.element = '*';
  465. }
  466. res.push(cssSel);
  467. };
  468. let cssSelector = new CssSelector();
  469. let match;
  470. let current = cssSelector;
  471. let inNot = false;
  472. _SELECTOR_REGEXP.lastIndex = 0;
  473. while (match = _SELECTOR_REGEXP.exec(selector)) {
  474. if (match[1 /* NOT */]) {
  475. if (inNot) {
  476. throw new Error('Nesting :not in a selector is not allowed');
  477. }
  478. inNot = true;
  479. current = new CssSelector();
  480. cssSelector.notSelectors.push(current);
  481. }
  482. const tag = match[2 /* TAG */];
  483. if (tag) {
  484. const prefix = match[3 /* PREFIX */];
  485. if (prefix === '#') {
  486. // #hash
  487. current.addAttribute('id', tag.substr(1));
  488. }
  489. else if (prefix === '.') {
  490. // Class
  491. current.addClassName(tag.substr(1));
  492. }
  493. else {
  494. // Element
  495. current.setElement(tag);
  496. }
  497. }
  498. const attribute = match[4 /* ATTRIBUTE */];
  499. if (attribute) {
  500. current.addAttribute(attribute, match[6 /* ATTRIBUTE_VALUE */]);
  501. }
  502. if (match[7 /* NOT_END */]) {
  503. inNot = false;
  504. current = cssSelector;
  505. }
  506. if (match[8 /* SEPARATOR */]) {
  507. if (inNot) {
  508. throw new Error('Multiple selectors in :not are not supported');
  509. }
  510. _addResult(results, cssSelector);
  511. cssSelector = current = new CssSelector();
  512. }
  513. }
  514. _addResult(results, cssSelector);
  515. return results;
  516. }
  517. isElementSelector() {
  518. return this.hasElementSelector() && this.classNames.length == 0 && this.attrs.length == 0 &&
  519. this.notSelectors.length === 0;
  520. }
  521. hasElementSelector() {
  522. return !!this.element;
  523. }
  524. setElement(element = null) {
  525. this.element = element;
  526. }
  527. /** Gets a template string for an element that matches the selector. */
  528. getMatchingElementTemplate() {
  529. const tagName = this.element || 'div';
  530. const classAttr = this.classNames.length > 0 ? ` class="${this.classNames.join(' ')}"` : '';
  531. let attrs = '';
  532. for (let i = 0; i < this.attrs.length; i += 2) {
  533. const attrName = this.attrs[i];
  534. const attrValue = this.attrs[i + 1] !== '' ? `="${this.attrs[i + 1]}"` : '';
  535. attrs += ` ${attrName}${attrValue}`;
  536. }
  537. return getHtmlTagDefinition(tagName).isVoid ? `<${tagName}${classAttr}${attrs}/>` :
  538. `<${tagName}${classAttr}${attrs}></${tagName}>`;
  539. }
  540. getAttrs() {
  541. const result = [];
  542. if (this.classNames.length > 0) {
  543. result.push('class', this.classNames.join(' '));
  544. }
  545. return result.concat(this.attrs);
  546. }
  547. addAttribute(name, value = '') {
  548. this.attrs.push(name, value && value.toLowerCase() || '');
  549. }
  550. addClassName(name) {
  551. this.classNames.push(name.toLowerCase());
  552. }
  553. toString() {
  554. let res = this.element || '';
  555. if (this.classNames) {
  556. this.classNames.forEach(klass => res += `.${klass}`);
  557. }
  558. if (this.attrs) {
  559. for (let i = 0; i < this.attrs.length; i += 2) {
  560. const name = this.attrs[i];
  561. const value = this.attrs[i + 1];
  562. res += `[${name}${value ? '=' + value : ''}]`;
  563. }
  564. }
  565. this.notSelectors.forEach(notSelector => res += `:not(${notSelector})`);
  566. return res;
  567. }
  568. }
  569. /**
  570. * Reads a list of CssSelectors and allows to calculate which ones
  571. * are contained in a given CssSelector.
  572. */
  573. class SelectorMatcher {
  574. constructor() {
  575. this._elementMap = new Map();
  576. this._elementPartialMap = new Map();
  577. this._classMap = new Map();
  578. this._classPartialMap = new Map();
  579. this._attrValueMap = new Map();
  580. this._attrValuePartialMap = new Map();
  581. this._listContexts = [];
  582. }
  583. static createNotMatcher(notSelectors) {
  584. const notMatcher = new SelectorMatcher();
  585. notMatcher.addSelectables(notSelectors, null);
  586. return notMatcher;
  587. }
  588. addSelectables(cssSelectors, callbackCtxt) {
  589. let listContext = null;
  590. if (cssSelectors.length > 1) {
  591. listContext = new SelectorListContext(cssSelectors);
  592. this._listContexts.push(listContext);
  593. }
  594. for (let i = 0; i < cssSelectors.length; i++) {
  595. this._addSelectable(cssSelectors[i], callbackCtxt, listContext);
  596. }
  597. }
  598. /**
  599. * Add an object that can be found later on by calling `match`.
  600. * @param cssSelector A css selector
  601. * @param callbackCtxt An opaque object that will be given to the callback of the `match` function
  602. */
  603. _addSelectable(cssSelector, callbackCtxt, listContext) {
  604. let matcher = this;
  605. const element = cssSelector.element;
  606. const classNames = cssSelector.classNames;
  607. const attrs = cssSelector.attrs;
  608. const selectable = new SelectorContext(cssSelector, callbackCtxt, listContext);
  609. if (element) {
  610. const isTerminal = attrs.length === 0 && classNames.length === 0;
  611. if (isTerminal) {
  612. this._addTerminal(matcher._elementMap, element, selectable);
  613. }
  614. else {
  615. matcher = this._addPartial(matcher._elementPartialMap, element);
  616. }
  617. }
  618. if (classNames) {
  619. for (let i = 0; i < classNames.length; i++) {
  620. const isTerminal = attrs.length === 0 && i === classNames.length - 1;
  621. const className = classNames[i];
  622. if (isTerminal) {
  623. this._addTerminal(matcher._classMap, className, selectable);
  624. }
  625. else {
  626. matcher = this._addPartial(matcher._classPartialMap, className);
  627. }
  628. }
  629. }
  630. if (attrs) {
  631. for (let i = 0; i < attrs.length; i += 2) {
  632. const isTerminal = i === attrs.length - 2;
  633. const name = attrs[i];
  634. const value = attrs[i + 1];
  635. if (isTerminal) {
  636. const terminalMap = matcher._attrValueMap;
  637. let terminalValuesMap = terminalMap.get(name);
  638. if (!terminalValuesMap) {
  639. terminalValuesMap = new Map();
  640. terminalMap.set(name, terminalValuesMap);
  641. }
  642. this._addTerminal(terminalValuesMap, value, selectable);
  643. }
  644. else {
  645. const partialMap = matcher._attrValuePartialMap;
  646. let partialValuesMap = partialMap.get(name);
  647. if (!partialValuesMap) {
  648. partialValuesMap = new Map();
  649. partialMap.set(name, partialValuesMap);
  650. }
  651. matcher = this._addPartial(partialValuesMap, value);
  652. }
  653. }
  654. }
  655. }
  656. _addTerminal(map, name, selectable) {
  657. let terminalList = map.get(name);
  658. if (!terminalList) {
  659. terminalList = [];
  660. map.set(name, terminalList);
  661. }
  662. terminalList.push(selectable);
  663. }
  664. _addPartial(map, name) {
  665. let matcher = map.get(name);
  666. if (!matcher) {
  667. matcher = new SelectorMatcher();
  668. map.set(name, matcher);
  669. }
  670. return matcher;
  671. }
  672. /**
  673. * Find the objects that have been added via `addSelectable`
  674. * whose css selector is contained in the given css selector.
  675. * @param cssSelector A css selector
  676. * @param matchedCallback This callback will be called with the object handed into `addSelectable`
  677. * @return boolean true if a match was found
  678. */
  679. match(cssSelector, matchedCallback) {
  680. let result = false;
  681. const element = cssSelector.element;
  682. const classNames = cssSelector.classNames;
  683. const attrs = cssSelector.attrs;
  684. for (let i = 0; i < this._listContexts.length; i++) {
  685. this._listContexts[i].alreadyMatched = false;
  686. }
  687. result = this._matchTerminal(this._elementMap, element, cssSelector, matchedCallback) || result;
  688. result = this._matchPartial(this._elementPartialMap, element, cssSelector, matchedCallback) ||
  689. result;
  690. if (classNames) {
  691. for (let i = 0; i < classNames.length; i++) {
  692. const className = classNames[i];
  693. result =
  694. this._matchTerminal(this._classMap, className, cssSelector, matchedCallback) || result;
  695. result =
  696. this._matchPartial(this._classPartialMap, className, cssSelector, matchedCallback) ||
  697. result;
  698. }
  699. }
  700. if (attrs) {
  701. for (let i = 0; i < attrs.length; i += 2) {
  702. const name = attrs[i];
  703. const value = attrs[i + 1];
  704. const terminalValuesMap = this._attrValueMap.get(name);
  705. if (value) {
  706. result =
  707. this._matchTerminal(terminalValuesMap, '', cssSelector, matchedCallback) || result;
  708. }
  709. result =
  710. this._matchTerminal(terminalValuesMap, value, cssSelector, matchedCallback) || result;
  711. const partialValuesMap = this._attrValuePartialMap.get(name);
  712. if (value) {
  713. result = this._matchPartial(partialValuesMap, '', cssSelector, matchedCallback) || result;
  714. }
  715. result =
  716. this._matchPartial(partialValuesMap, value, cssSelector, matchedCallback) || result;
  717. }
  718. }
  719. return result;
  720. }
  721. /** @internal */
  722. _matchTerminal(map, name, cssSelector, matchedCallback) {
  723. if (!map || typeof name !== 'string') {
  724. return false;
  725. }
  726. let selectables = map.get(name) || [];
  727. const starSelectables = map.get('*');
  728. if (starSelectables) {
  729. selectables = selectables.concat(starSelectables);
  730. }
  731. if (selectables.length === 0) {
  732. return false;
  733. }
  734. let selectable;
  735. let result = false;
  736. for (let i = 0; i < selectables.length; i++) {
  737. selectable = selectables[i];
  738. result = selectable.finalize(cssSelector, matchedCallback) || result;
  739. }
  740. return result;
  741. }
  742. /** @internal */
  743. _matchPartial(map, name, cssSelector, matchedCallback) {
  744. if (!map || typeof name !== 'string') {
  745. return false;
  746. }
  747. const nestedSelector = map.get(name);
  748. if (!nestedSelector) {
  749. return false;
  750. }
  751. // TODO(perf): get rid of recursion and measure again
  752. // TODO(perf): don't pass the whole selector into the recursion,
  753. // but only the not processed parts
  754. return nestedSelector.match(cssSelector, matchedCallback);
  755. }
  756. }
  757. class SelectorListContext {
  758. constructor(selectors) {
  759. this.selectors = selectors;
  760. this.alreadyMatched = false;
  761. }
  762. }
  763. // Store context to pass back selector and context when a selector is matched
  764. class SelectorContext {
  765. constructor(selector, cbContext, listContext) {
  766. this.selector = selector;
  767. this.cbContext = cbContext;
  768. this.listContext = listContext;
  769. this.notSelectors = selector.notSelectors;
  770. }
  771. finalize(cssSelector, callback) {
  772. let result = true;
  773. if (this.notSelectors.length > 0 && (!this.listContext || !this.listContext.alreadyMatched)) {
  774. const notMatcher = SelectorMatcher.createNotMatcher(this.notSelectors);
  775. result = !notMatcher.match(cssSelector, null);
  776. }
  777. if (result && callback && (!this.listContext || !this.listContext.alreadyMatched)) {
  778. if (this.listContext) {
  779. this.listContext.alreadyMatched = true;
  780. }
  781. callback(this.selector, this.cbContext);
  782. }
  783. return result;
  784. }
  785. }
  786. /**
  787. * @license
  788. * Copyright Google LLC All Rights Reserved.
  789. *
  790. * Use of this source code is governed by an MIT-style license that can be
  791. * found in the LICENSE file at https://angular.io/license
  792. */
  793. const createInject = makeMetadataFactory('Inject', (token) => ({ token }));
  794. const createInjectionToken = makeMetadataFactory('InjectionToken', (desc) => ({ _desc: desc, ɵprov: undefined }));
  795. const createAttribute = makeMetadataFactory('Attribute', (attributeName) => ({ attributeName }));
  796. // Stores the default value of `emitDistinctChangesOnly` when the `emitDistinctChangesOnly` is not
  797. // explicitly set.
  798. const emitDistinctChangesOnlyDefaultValue = true;
  799. const createContentChildren = makeMetadataFactory('ContentChildren', (selector, data = {}) => (Object.assign({ selector, first: false, isViewQuery: false, descendants: false, emitDistinctChangesOnly: emitDistinctChangesOnlyDefaultValue }, data)));
  800. const createContentChild = makeMetadataFactory('ContentChild', (selector, data = {}) => (Object.assign({ selector, first: true, isViewQuery: false, descendants: true }, data)));
  801. const createViewChildren = makeMetadataFactory('ViewChildren', (selector, data = {}) => (Object.assign({ selector, first: false, isViewQuery: true, descendants: true, emitDistinctChangesOnly: emitDistinctChangesOnlyDefaultValue }, data)));
  802. const createViewChild = makeMetadataFactory('ViewChild', (selector, data) => (Object.assign({ selector, first: true, isViewQuery: true, descendants: true }, data)));
  803. const createDirective = makeMetadataFactory('Directive', (dir = {}) => dir);
  804. var ViewEncapsulation;
  805. (function (ViewEncapsulation) {
  806. ViewEncapsulation[ViewEncapsulation["Emulated"] = 0] = "Emulated";
  807. // Historically the 1 value was for `Native` encapsulation which has been removed as of v11.
  808. ViewEncapsulation[ViewEncapsulation["None"] = 2] = "None";
  809. ViewEncapsulation[ViewEncapsulation["ShadowDom"] = 3] = "ShadowDom";
  810. })(ViewEncapsulation || (ViewEncapsulation = {}));
  811. var ChangeDetectionStrategy;
  812. (function (ChangeDetectionStrategy) {
  813. ChangeDetectionStrategy[ChangeDetectionStrategy["OnPush"] = 0] = "OnPush";
  814. ChangeDetectionStrategy[ChangeDetectionStrategy["Default"] = 1] = "Default";
  815. })(ChangeDetectionStrategy || (ChangeDetectionStrategy = {}));
  816. const createComponent = makeMetadataFactory('Component', (c = {}) => (Object.assign({ changeDetection: ChangeDetectionStrategy.Default }, c)));
  817. const createPipe = makeMetadataFactory('Pipe', (p) => (Object.assign({ pure: true }, p)));
  818. const createInput = makeMetadataFactory('Input', (bindingPropertyName) => ({ bindingPropertyName }));
  819. const createOutput = makeMetadataFactory('Output', (bindingPropertyName) => ({ bindingPropertyName }));
  820. const createHostBinding = makeMetadataFactory('HostBinding', (hostPropertyName) => ({ hostPropertyName }));
  821. const createHostListener = makeMetadataFactory('HostListener', (eventName, args) => ({ eventName, args }));
  822. const createNgModule = makeMetadataFactory('NgModule', (ngModule) => ngModule);
  823. const createInjectable = makeMetadataFactory('Injectable', (injectable = {}) => injectable);
  824. const CUSTOM_ELEMENTS_SCHEMA = {
  825. name: 'custom-elements'
  826. };
  827. const NO_ERRORS_SCHEMA = {
  828. name: 'no-errors-schema'
  829. };
  830. const createOptional = makeMetadataFactory('Optional');
  831. const createSelf = makeMetadataFactory('Self');
  832. const createSkipSelf = makeMetadataFactory('SkipSelf');
  833. const createHost = makeMetadataFactory('Host');
  834. const Type = Function;
  835. var SecurityContext;
  836. (function (SecurityContext) {
  837. SecurityContext[SecurityContext["NONE"] = 0] = "NONE";
  838. SecurityContext[SecurityContext["HTML"] = 1] = "HTML";
  839. SecurityContext[SecurityContext["STYLE"] = 2] = "STYLE";
  840. SecurityContext[SecurityContext["SCRIPT"] = 3] = "SCRIPT";
  841. SecurityContext[SecurityContext["URL"] = 4] = "URL";
  842. SecurityContext[SecurityContext["RESOURCE_URL"] = 5] = "RESOURCE_URL";
  843. })(SecurityContext || (SecurityContext = {}));
  844. var MissingTranslationStrategy;
  845. (function (MissingTranslationStrategy) {
  846. MissingTranslationStrategy[MissingTranslationStrategy["Error"] = 0] = "Error";
  847. MissingTranslationStrategy[MissingTranslationStrategy["Warning"] = 1] = "Warning";
  848. MissingTranslationStrategy[MissingTranslationStrategy["Ignore"] = 2] = "Ignore";
  849. })(MissingTranslationStrategy || (MissingTranslationStrategy = {}));
  850. function makeMetadataFactory(name, props) {
  851. // This must be declared as a function, not a fat arrow, so that ES2015 devmode produces code
  852. // that works with the static_reflector.ts in the ViewEngine compiler.
  853. // In particular, `_registerDecoratorOrConstructor` assumes that the value returned here can be
  854. // new'ed.
  855. function factory(...args) {
  856. const values = props ? props(...args) : {};
  857. return Object.assign({ ngMetadataName: name }, values);
  858. }
  859. factory.isTypeOf = (obj) => obj && obj.ngMetadataName === name;
  860. factory.ngMetadataName = name;
  861. return factory;
  862. }
  863. function parserSelectorToSimpleSelector(selector) {
  864. const classes = selector.classNames && selector.classNames.length ?
  865. [8 /* CLASS */, ...selector.classNames] :
  866. [];
  867. const elementName = selector.element && selector.element !== '*' ? selector.element : '';
  868. return [elementName, ...selector.attrs, ...classes];
  869. }
  870. function parserSelectorToNegativeSelector(selector) {
  871. const classes = selector.classNames && selector.classNames.length ?
  872. [8 /* CLASS */, ...selector.classNames] :
  873. [];
  874. if (selector.element) {
  875. return [
  876. 1 /* NOT */ | 4 /* ELEMENT */, selector.element, ...selector.attrs, ...classes
  877. ];
  878. }
  879. else if (selector.attrs.length) {
  880. return [1 /* NOT */ | 2 /* ATTRIBUTE */, ...selector.attrs, ...classes];
  881. }
  882. else {
  883. return selector.classNames && selector.classNames.length ?
  884. [1 /* NOT */ | 8 /* CLASS */, ...selector.classNames] :
  885. [];
  886. }
  887. }
  888. function parserSelectorToR3Selector(selector) {
  889. const positive = parserSelectorToSimpleSelector(selector);
  890. const negative = selector.notSelectors && selector.notSelectors.length ?
  891. selector.notSelectors.map(notSelector => parserSelectorToNegativeSelector(notSelector)) :
  892. [];
  893. return positive.concat(...negative);
  894. }
  895. function parseSelectorToR3Selector(selector) {
  896. return selector ? CssSelector.parse(selector).map(parserSelectorToR3Selector) : [];
  897. }
  898. var core = /*#__PURE__*/Object.freeze({
  899. __proto__: null,
  900. createInject: createInject,
  901. createInjectionToken: createInjectionToken,
  902. createAttribute: createAttribute,
  903. emitDistinctChangesOnlyDefaultValue: emitDistinctChangesOnlyDefaultValue,
  904. createContentChildren: createContentChildren,
  905. createContentChild: createContentChild,
  906. createViewChildren: createViewChildren,
  907. createViewChild: createViewChild,
  908. createDirective: createDirective,
  909. get ViewEncapsulation () { return ViewEncapsulation; },
  910. get ChangeDetectionStrategy () { return ChangeDetectionStrategy; },
  911. createComponent: createComponent,
  912. createPipe: createPipe,
  913. createInput: createInput,
  914. createOutput: createOutput,
  915. createHostBinding: createHostBinding,
  916. createHostListener: createHostListener,
  917. createNgModule: createNgModule,
  918. createInjectable: createInjectable,
  919. CUSTOM_ELEMENTS_SCHEMA: CUSTOM_ELEMENTS_SCHEMA,
  920. NO_ERRORS_SCHEMA: NO_ERRORS_SCHEMA,
  921. createOptional: createOptional,
  922. createSelf: createSelf,
  923. createSkipSelf: createSkipSelf,
  924. createHost: createHost,
  925. Type: Type,
  926. get SecurityContext () { return SecurityContext; },
  927. get MissingTranslationStrategy () { return MissingTranslationStrategy; },
  928. parseSelectorToR3Selector: parseSelectorToR3Selector
  929. });
  930. /**
  931. * @license
  932. * Copyright Google LLC All Rights Reserved.
  933. *
  934. * Use of this source code is governed by an MIT-style license that can be
  935. * found in the LICENSE file at https://angular.io/license
  936. */
  937. //// Types
  938. var TypeModifier;
  939. (function (TypeModifier) {
  940. TypeModifier[TypeModifier["Const"] = 0] = "Const";
  941. })(TypeModifier || (TypeModifier = {}));
  942. class Type$1 {
  943. constructor(modifiers = []) {
  944. this.modifiers = modifiers;
  945. }
  946. hasModifier(modifier) {
  947. return this.modifiers.indexOf(modifier) !== -1;
  948. }
  949. }
  950. var BuiltinTypeName;
  951. (function (BuiltinTypeName) {
  952. BuiltinTypeName[BuiltinTypeName["Dynamic"] = 0] = "Dynamic";
  953. BuiltinTypeName[BuiltinTypeName["Bool"] = 1] = "Bool";
  954. BuiltinTypeName[BuiltinTypeName["String"] = 2] = "String";
  955. BuiltinTypeName[BuiltinTypeName["Int"] = 3] = "Int";
  956. BuiltinTypeName[BuiltinTypeName["Number"] = 4] = "Number";
  957. BuiltinTypeName[BuiltinTypeName["Function"] = 5] = "Function";
  958. BuiltinTypeName[BuiltinTypeName["Inferred"] = 6] = "Inferred";
  959. BuiltinTypeName[BuiltinTypeName["None"] = 7] = "None";
  960. })(BuiltinTypeName || (BuiltinTypeName = {}));
  961. class BuiltinType extends Type$1 {
  962. constructor(name, modifiers) {
  963. super(modifiers);
  964. this.name = name;
  965. }
  966. visitType(visitor, context) {
  967. return visitor.visitBuiltinType(this, context);
  968. }
  969. }
  970. class ExpressionType extends Type$1 {
  971. constructor(value, modifiers, typeParams = null) {
  972. super(modifiers);
  973. this.value = value;
  974. this.typeParams = typeParams;
  975. }
  976. visitType(visitor, context) {
  977. return visitor.visitExpressionType(this, context);
  978. }
  979. }
  980. class ArrayType extends Type$1 {
  981. constructor(of, modifiers) {
  982. super(modifiers);
  983. this.of = of;
  984. }
  985. visitType(visitor, context) {
  986. return visitor.visitArrayType(this, context);
  987. }
  988. }
  989. class MapType extends Type$1 {
  990. constructor(valueType, modifiers) {
  991. super(modifiers);
  992. this.valueType = valueType || null;
  993. }
  994. visitType(visitor, context) {
  995. return visitor.visitMapType(this, context);
  996. }
  997. }
  998. const DYNAMIC_TYPE = new BuiltinType(BuiltinTypeName.Dynamic);
  999. const INFERRED_TYPE = new BuiltinType(BuiltinTypeName.Inferred);
  1000. const BOOL_TYPE = new BuiltinType(BuiltinTypeName.Bool);
  1001. const INT_TYPE = new BuiltinType(BuiltinTypeName.Int);
  1002. const NUMBER_TYPE = new BuiltinType(BuiltinTypeName.Number);
  1003. const STRING_TYPE = new BuiltinType(BuiltinTypeName.String);
  1004. const FUNCTION_TYPE = new BuiltinType(BuiltinTypeName.Function);
  1005. const NONE_TYPE = new BuiltinType(BuiltinTypeName.None);
  1006. ///// Expressions
  1007. var UnaryOperator;
  1008. (function (UnaryOperator) {
  1009. UnaryOperator[UnaryOperator["Minus"] = 0] = "Minus";
  1010. UnaryOperator[UnaryOperator["Plus"] = 1] = "Plus";
  1011. })(UnaryOperator || (UnaryOperator = {}));
  1012. var BinaryOperator;
  1013. (function (BinaryOperator) {
  1014. BinaryOperator[BinaryOperator["Equals"] = 0] = "Equals";
  1015. BinaryOperator[BinaryOperator["NotEquals"] = 1] = "NotEquals";
  1016. BinaryOperator[BinaryOperator["Identical"] = 2] = "Identical";
  1017. BinaryOperator[BinaryOperator["NotIdentical"] = 3] = "NotIdentical";
  1018. BinaryOperator[BinaryOperator["Minus"] = 4] = "Minus";
  1019. BinaryOperator[BinaryOperator["Plus"] = 5] = "Plus";
  1020. BinaryOperator[BinaryOperator["Divide"] = 6] = "Divide";
  1021. BinaryOperator[BinaryOperator["Multiply"] = 7] = "Multiply";
  1022. BinaryOperator[BinaryOperator["Modulo"] = 8] = "Modulo";
  1023. BinaryOperator[BinaryOperator["And"] = 9] = "And";
  1024. BinaryOperator[BinaryOperator["Or"] = 10] = "Or";
  1025. BinaryOperator[BinaryOperator["BitwiseAnd"] = 11] = "BitwiseAnd";
  1026. BinaryOperator[BinaryOperator["Lower"] = 12] = "Lower";
  1027. BinaryOperator[BinaryOperator["LowerEquals"] = 13] = "LowerEquals";
  1028. BinaryOperator[BinaryOperator["Bigger"] = 14] = "Bigger";
  1029. BinaryOperator[BinaryOperator["BiggerEquals"] = 15] = "BiggerEquals";
  1030. BinaryOperator[BinaryOperator["NullishCoalesce"] = 16] = "NullishCoalesce";
  1031. })(BinaryOperator || (BinaryOperator = {}));
  1032. function nullSafeIsEquivalent(base, other) {
  1033. if (base == null || other == null) {
  1034. return base == other;
  1035. }
  1036. return base.isEquivalent(other);
  1037. }
  1038. function areAllEquivalentPredicate(base, other, equivalentPredicate) {
  1039. const len = base.length;
  1040. if (len !== other.length) {
  1041. return false;
  1042. }
  1043. for (let i = 0; i < len; i++) {
  1044. if (!equivalentPredicate(base[i], other[i])) {
  1045. return false;
  1046. }
  1047. }
  1048. return true;
  1049. }
  1050. function areAllEquivalent(base, other) {
  1051. return areAllEquivalentPredicate(base, other, (baseElement, otherElement) => baseElement.isEquivalent(otherElement));
  1052. }
  1053. class Expression {
  1054. constructor(type, sourceSpan) {
  1055. this.type = type || null;
  1056. this.sourceSpan = sourceSpan || null;
  1057. }
  1058. prop(name, sourceSpan) {
  1059. return new ReadPropExpr(this, name, null, sourceSpan);
  1060. }
  1061. key(index, type, sourceSpan) {
  1062. return new ReadKeyExpr(this, index, type, sourceSpan);
  1063. }
  1064. callMethod(name, params, sourceSpan) {
  1065. return new InvokeMethodExpr(this, name, params, null, sourceSpan);
  1066. }
  1067. callFn(params, sourceSpan, pure) {
  1068. return new InvokeFunctionExpr(this, params, null, sourceSpan, pure);
  1069. }
  1070. instantiate(params, type, sourceSpan) {
  1071. return new InstantiateExpr(this, params, type, sourceSpan);
  1072. }
  1073. conditional(trueCase, falseCase = null, sourceSpan) {
  1074. return new ConditionalExpr(this, trueCase, falseCase, null, sourceSpan);
  1075. }
  1076. equals(rhs, sourceSpan) {
  1077. return new BinaryOperatorExpr(BinaryOperator.Equals, this, rhs, null, sourceSpan);
  1078. }
  1079. notEquals(rhs, sourceSpan) {
  1080. return new BinaryOperatorExpr(BinaryOperator.NotEquals, this, rhs, null, sourceSpan);
  1081. }
  1082. identical(rhs, sourceSpan) {
  1083. return new BinaryOperatorExpr(BinaryOperator.Identical, this, rhs, null, sourceSpan);
  1084. }
  1085. notIdentical(rhs, sourceSpan) {
  1086. return new BinaryOperatorExpr(BinaryOperator.NotIdentical, this, rhs, null, sourceSpan);
  1087. }
  1088. minus(rhs, sourceSpan) {
  1089. return new BinaryOperatorExpr(BinaryOperator.Minus, this, rhs, null, sourceSpan);
  1090. }
  1091. plus(rhs, sourceSpan) {
  1092. return new BinaryOperatorExpr(BinaryOperator.Plus, this, rhs, null, sourceSpan);
  1093. }
  1094. divide(rhs, sourceSpan) {
  1095. return new BinaryOperatorExpr(BinaryOperator.Divide, this, rhs, null, sourceSpan);
  1096. }
  1097. multiply(rhs, sourceSpan) {
  1098. return new BinaryOperatorExpr(BinaryOperator.Multiply, this, rhs, null, sourceSpan);
  1099. }
  1100. modulo(rhs, sourceSpan) {
  1101. return new BinaryOperatorExpr(BinaryOperator.Modulo, this, rhs, null, sourceSpan);
  1102. }
  1103. and(rhs, sourceSpan) {
  1104. return new BinaryOperatorExpr(BinaryOperator.And, this, rhs, null, sourceSpan);
  1105. }
  1106. bitwiseAnd(rhs, sourceSpan, parens = true) {
  1107. return new BinaryOperatorExpr(BinaryOperator.BitwiseAnd, this, rhs, null, sourceSpan, parens);
  1108. }
  1109. or(rhs, sourceSpan) {
  1110. return new BinaryOperatorExpr(BinaryOperator.Or, this, rhs, null, sourceSpan);
  1111. }
  1112. lower(rhs, sourceSpan) {
  1113. return new BinaryOperatorExpr(BinaryOperator.Lower, this, rhs, null, sourceSpan);
  1114. }
  1115. lowerEquals(rhs, sourceSpan) {
  1116. return new BinaryOperatorExpr(BinaryOperator.LowerEquals, this, rhs, null, sourceSpan);
  1117. }
  1118. bigger(rhs, sourceSpan) {
  1119. return new BinaryOperatorExpr(BinaryOperator.Bigger, this, rhs, null, sourceSpan);
  1120. }
  1121. biggerEquals(rhs, sourceSpan) {
  1122. return new BinaryOperatorExpr(BinaryOperator.BiggerEquals, this, rhs, null, sourceSpan);
  1123. }
  1124. isBlank(sourceSpan) {
  1125. // Note: We use equals by purpose here to compare to null and undefined in JS.
  1126. // We use the typed null to allow strictNullChecks to narrow types.
  1127. return this.equals(TYPED_NULL_EXPR, sourceSpan);
  1128. }
  1129. cast(type, sourceSpan) {
  1130. return new CastExpr(this, type, sourceSpan);
  1131. }
  1132. nullishCoalesce(rhs, sourceSpan) {
  1133. return new BinaryOperatorExpr(BinaryOperator.NullishCoalesce, this, rhs, null, sourceSpan);
  1134. }
  1135. toStmt() {
  1136. return new ExpressionStatement(this, null);
  1137. }
  1138. }
  1139. var BuiltinVar;
  1140. (function (BuiltinVar) {
  1141. BuiltinVar[BuiltinVar["This"] = 0] = "This";
  1142. BuiltinVar[BuiltinVar["Super"] = 1] = "Super";
  1143. BuiltinVar[BuiltinVar["CatchError"] = 2] = "CatchError";
  1144. BuiltinVar[BuiltinVar["CatchStack"] = 3] = "CatchStack";
  1145. })(BuiltinVar || (BuiltinVar = {}));
  1146. class ReadVarExpr extends Expression {
  1147. constructor(name, type, sourceSpan) {
  1148. super(type, sourceSpan);
  1149. if (typeof name === 'string') {
  1150. this.name = name;
  1151. this.builtin = null;
  1152. }
  1153. else {
  1154. this.name = null;
  1155. this.builtin = name;
  1156. }
  1157. }
  1158. isEquivalent(e) {
  1159. return e instanceof ReadVarExpr && this.name === e.name && this.builtin === e.builtin;
  1160. }
  1161. isConstant() {
  1162. return false;
  1163. }
  1164. visitExpression(visitor, context) {
  1165. return visitor.visitReadVarExpr(this, context);
  1166. }
  1167. set(value) {
  1168. if (!this.name) {
  1169. throw new Error(`Built in variable ${this.builtin} can not be assigned to.`);
  1170. }
  1171. return new WriteVarExpr(this.name, value, null, this.sourceSpan);
  1172. }
  1173. }
  1174. class TypeofExpr extends Expression {
  1175. constructor(expr, type, sourceSpan) {
  1176. super(type, sourceSpan);
  1177. this.expr = expr;
  1178. }
  1179. visitExpression(visitor, context) {
  1180. return visitor.visitTypeofExpr(this, context);
  1181. }
  1182. isEquivalent(e) {
  1183. return e instanceof TypeofExpr && e.expr.isEquivalent(this.expr);
  1184. }
  1185. isConstant() {
  1186. return this.expr.isConstant();
  1187. }
  1188. }
  1189. class WrappedNodeExpr extends Expression {
  1190. constructor(node, type, sourceSpan) {
  1191. super(type, sourceSpan);
  1192. this.node = node;
  1193. }
  1194. isEquivalent(e) {
  1195. return e instanceof WrappedNodeExpr && this.node === e.node;
  1196. }
  1197. isConstant() {
  1198. return false;
  1199. }
  1200. visitExpression(visitor, context) {
  1201. return visitor.visitWrappedNodeExpr(this, context);
  1202. }
  1203. }
  1204. class WriteVarExpr extends Expression {
  1205. constructor(name, value, type, sourceSpan) {
  1206. super(type || value.type, sourceSpan);
  1207. this.name = name;
  1208. this.value = value;
  1209. }
  1210. isEquivalent(e) {
  1211. return e instanceof WriteVarExpr && this.name === e.name && this.value.isEquivalent(e.value);
  1212. }
  1213. isConstant() {
  1214. return false;
  1215. }
  1216. visitExpression(visitor, context) {
  1217. return visitor.visitWriteVarExpr(this, context);
  1218. }
  1219. toDeclStmt(type, modifiers) {
  1220. return new DeclareVarStmt(this.name, this.value, type, modifiers, this.sourceSpan);
  1221. }
  1222. toConstDecl() {
  1223. return this.toDeclStmt(INFERRED_TYPE, [StmtModifier.Final]);
  1224. }
  1225. }
  1226. class WriteKeyExpr extends Expression {
  1227. constructor(receiver, index, value, type, sourceSpan) {
  1228. super(type || value.type, sourceSpan);
  1229. this.receiver = receiver;
  1230. this.index = index;
  1231. this.value = value;
  1232. }
  1233. isEquivalent(e) {
  1234. return e instanceof WriteKeyExpr && this.receiver.isEquivalent(e.receiver) &&
  1235. this.index.isEquivalent(e.index) && this.value.isEquivalent(e.value);
  1236. }
  1237. isConstant() {
  1238. return false;
  1239. }
  1240. visitExpression(visitor, context) {
  1241. return visitor.visitWriteKeyExpr(this, context);
  1242. }
  1243. }
  1244. class WritePropExpr extends Expression {
  1245. constructor(receiver, name, value, type, sourceSpan) {
  1246. super(type || value.type, sourceSpan);
  1247. this.receiver = receiver;
  1248. this.name = name;
  1249. this.value = value;
  1250. }
  1251. isEquivalent(e) {
  1252. return e instanceof WritePropExpr && this.receiver.isEquivalent(e.receiver) &&
  1253. this.name === e.name && this.value.isEquivalent(e.value);
  1254. }
  1255. isConstant() {
  1256. return false;
  1257. }
  1258. visitExpression(visitor, context) {
  1259. return visitor.visitWritePropExpr(this, context);
  1260. }
  1261. }
  1262. var BuiltinMethod;
  1263. (function (BuiltinMethod) {
  1264. BuiltinMethod[BuiltinMethod["ConcatArray"] = 0] = "ConcatArray";
  1265. BuiltinMethod[BuiltinMethod["SubscribeObservable"] = 1] = "SubscribeObservable";
  1266. BuiltinMethod[BuiltinMethod["Bind"] = 2] = "Bind";
  1267. })(BuiltinMethod || (BuiltinMethod = {}));
  1268. class InvokeMethodExpr extends Expression {
  1269. constructor(receiver, method, args, type, sourceSpan) {
  1270. super(type, sourceSpan);
  1271. this.receiver = receiver;
  1272. this.args = args;
  1273. if (typeof method === 'string') {
  1274. this.name = method;
  1275. this.builtin = null;
  1276. }
  1277. else {
  1278. this.name = null;
  1279. this.builtin = method;
  1280. }
  1281. }
  1282. isEquivalent(e) {
  1283. return e instanceof InvokeMethodExpr && this.receiver.isEquivalent(e.receiver) &&
  1284. this.name === e.name && this.builtin === e.builtin && areAllEquivalent(this.args, e.args);
  1285. }
  1286. isConstant() {
  1287. return false;
  1288. }
  1289. visitExpression(visitor, context) {
  1290. return visitor.visitInvokeMethodExpr(this, context);
  1291. }
  1292. }
  1293. class InvokeFunctionExpr extends Expression {
  1294. constructor(fn, args, type, sourceSpan, pure = false) {
  1295. super(type, sourceSpan);
  1296. this.fn = fn;
  1297. this.args = args;
  1298. this.pure = pure;
  1299. }
  1300. isEquivalent(e) {
  1301. return e instanceof InvokeFunctionExpr && this.fn.isEquivalent(e.fn) &&
  1302. areAllEquivalent(this.args, e.args) && this.pure === e.pure;
  1303. }
  1304. isConstant() {
  1305. return false;
  1306. }
  1307. visitExpression(visitor, context) {
  1308. return visitor.visitInvokeFunctionExpr(this, context);
  1309. }
  1310. }
  1311. class TaggedTemplateExpr extends Expression {
  1312. constructor(tag, template, type, sourceSpan) {
  1313. super(type, sourceSpan);
  1314. this.tag = tag;
  1315. this.template = template;
  1316. }
  1317. isEquivalent(e) {
  1318. return e instanceof TaggedTemplateExpr && this.tag.isEquivalent(e.tag) &&
  1319. areAllEquivalentPredicate(this.template.elements, e.template.elements, (a, b) => a.text === b.text) &&
  1320. areAllEquivalent(this.template.expressions, e.template.expressions);
  1321. }
  1322. isConstant() {
  1323. return false;
  1324. }
  1325. visitExpression(visitor, context) {
  1326. return visitor.visitTaggedTemplateExpr(this, context);
  1327. }
  1328. }
  1329. class InstantiateExpr extends Expression {
  1330. constructor(classExpr, args, type, sourceSpan) {
  1331. super(type, sourceSpan);
  1332. this.classExpr = classExpr;
  1333. this.args = args;
  1334. }
  1335. isEquivalent(e) {
  1336. return e instanceof InstantiateExpr && this.classExpr.isEquivalent(e.classExpr) &&
  1337. areAllEquivalent(this.args, e.args);
  1338. }
  1339. isConstant() {
  1340. return false;
  1341. }
  1342. visitExpression(visitor, context) {
  1343. return visitor.visitInstantiateExpr(this, context);
  1344. }
  1345. }
  1346. class LiteralExpr extends Expression {
  1347. constructor(value, type, sourceSpan) {
  1348. super(type, sourceSpan);
  1349. this.value = value;
  1350. }
  1351. isEquivalent(e) {
  1352. return e instanceof LiteralExpr && this.value === e.value;
  1353. }
  1354. isConstant() {
  1355. return true;
  1356. }
  1357. visitExpression(visitor, context) {
  1358. return visitor.visitLiteralExpr(this, context);
  1359. }
  1360. }
  1361. class TemplateLiteral {
  1362. constructor(elements, expressions) {
  1363. this.elements = elements;
  1364. this.expressions = expressions;
  1365. }
  1366. }
  1367. class TemplateLiteralElement {
  1368. constructor(text, sourceSpan, rawText) {
  1369. var _a;
  1370. this.text = text;
  1371. this.sourceSpan = sourceSpan;
  1372. // If `rawText` is not provided, try to extract the raw string from its
  1373. // associated `sourceSpan`. If that is also not available, "fake" the raw
  1374. // string instead by escaping the following control sequences:
  1375. // - "\" would otherwise indicate that the next character is a control character.
  1376. // - "`" and "${" are template string control sequences that would otherwise prematurely
  1377. // indicate the end of the template literal element.
  1378. this.rawText =
  1379. (_a = rawText !== null && rawText !== void 0 ? rawText : sourceSpan === null || sourceSpan === void 0 ? void 0 : sourceSpan.toString()) !== null && _a !== void 0 ? _a : escapeForTemplateLiteral(escapeSlashes(text));
  1380. }
  1381. }
  1382. class MessagePiece {
  1383. constructor(text, sourceSpan) {
  1384. this.text = text;
  1385. this.sourceSpan = sourceSpan;
  1386. }
  1387. }
  1388. class LiteralPiece extends MessagePiece {
  1389. }
  1390. class PlaceholderPiece extends MessagePiece {
  1391. }
  1392. class LocalizedString extends Expression {
  1393. constructor(metaBlock, messageParts, placeHolderNames, expressions, sourceSpan) {
  1394. super(STRING_TYPE, sourceSpan);
  1395. this.metaBlock = metaBlock;
  1396. this.messageParts = messageParts;
  1397. this.placeHolderNames = placeHolderNames;
  1398. this.expressions = expressions;
  1399. }
  1400. isEquivalent(e) {
  1401. // return e instanceof LocalizedString && this.message === e.message;
  1402. return false;
  1403. }
  1404. isConstant() {
  1405. return false;
  1406. }
  1407. visitExpression(visitor, context) {
  1408. return visitor.visitLocalizedString(this, context);
  1409. }
  1410. /**
  1411. * Serialize the given `meta` and `messagePart` into "cooked" and "raw" strings that can be used
  1412. * in a `$localize` tagged string. The format of the metadata is the same as that parsed by
  1413. * `parseI18nMeta()`.
  1414. *
  1415. * @param meta The metadata to serialize
  1416. * @param messagePart The first part of the tagged string
  1417. */
  1418. serializeI18nHead() {
  1419. const MEANING_SEPARATOR = '|';
  1420. const ID_SEPARATOR = '@@';
  1421. const LEGACY_ID_INDICATOR = '␟';
  1422. let metaBlock = this.metaBlock.description || '';
  1423. if (this.metaBlock.meaning) {
  1424. metaBlock = `${this.metaBlock.meaning}${MEANING_SEPARATOR}${metaBlock}`;
  1425. }
  1426. if (this.metaBlock.customId) {
  1427. metaBlock = `${metaBlock}${ID_SEPARATOR}${this.metaBlock.customId}`;
  1428. }
  1429. if (this.metaBlock.legacyIds) {
  1430. this.metaBlock.legacyIds.forEach(legacyId => {
  1431. metaBlock = `${metaBlock}${LEGACY_ID_INDICATOR}${legacyId}`;
  1432. });
  1433. }
  1434. return createCookedRawString(metaBlock, this.messageParts[0].text, this.getMessagePartSourceSpan(0));
  1435. }
  1436. getMessagePartSourceSpan(i) {
  1437. var _a, _b;
  1438. return (_b = (_a = this.messageParts[i]) === null || _a === void 0 ? void 0 : _a.sourceSpan) !== null && _b !== void 0 ? _b : this.sourceSpan;
  1439. }
  1440. getPlaceholderSourceSpan(i) {
  1441. var _a, _b, _c, _d;
  1442. return (_d = (_b = (_a = this.placeHolderNames[i]) === null || _a === void 0 ? void 0 : _a.sourceSpan) !== null && _b !== void 0 ? _b : (_c = this.expressions[i]) === null || _c === void 0 ? void 0 : _c.sourceSpan) !== null && _d !== void 0 ? _d : this.sourceSpan;
  1443. }
  1444. /**
  1445. * Serialize the given `placeholderName` and `messagePart` into "cooked" and "raw" strings that
  1446. * can be used in a `$localize` tagged string.
  1447. *
  1448. * @param placeholderName The placeholder name to serialize
  1449. * @param messagePart The following message string after this placeholder
  1450. */
  1451. serializeI18nTemplatePart(partIndex) {
  1452. const placeholderName = this.placeHolderNames[partIndex - 1].text;
  1453. const messagePart = this.messageParts[partIndex];
  1454. return createCookedRawString(placeholderName, messagePart.text, this.getMessagePartSourceSpan(partIndex));
  1455. }
  1456. }
  1457. const escapeSlashes = (str) => str.replace(/\\/g, '\\\\');
  1458. const escapeStartingColon = (str) => str.replace(/^:/, '\\:');
  1459. const escapeColons = (str) => str.replace(/:/g, '\\:');
  1460. const escapeForTemplateLiteral = (str) => str.replace(/`/g, '\\`').replace(/\${/g, '$\\{');
  1461. /**
  1462. * Creates a `{cooked, raw}` object from the `metaBlock` and `messagePart`.
  1463. *
  1464. * The `raw` text must have various character sequences escaped:
  1465. * * "\" would otherwise indicate that the next character is a control character.
  1466. * * "`" and "${" are template string control sequences that would otherwise prematurely indicate
  1467. * the end of a message part.
  1468. * * ":" inside a metablock would prematurely indicate the end of the metablock.
  1469. * * ":" at the start of a messagePart with no metablock would erroneously indicate the start of a
  1470. * metablock.
  1471. *
  1472. * @param metaBlock Any metadata that should be prepended to the string
  1473. * @param messagePart The message part of the string
  1474. */
  1475. function createCookedRawString(metaBlock, messagePart, range) {
  1476. if (metaBlock === '') {
  1477. return {
  1478. cooked: messagePart,
  1479. raw: escapeForTemplateLiteral(escapeStartingColon(escapeSlashes(messagePart))),
  1480. range,
  1481. };
  1482. }
  1483. else {
  1484. return {
  1485. cooked: `:${metaBlock}:${messagePart}`,
  1486. raw: escapeForTemplateLiteral(`:${escapeColons(escapeSlashes(metaBlock))}:${escapeSlashes(messagePart)}`),
  1487. range,
  1488. };
  1489. }
  1490. }
  1491. class ExternalExpr extends Expression {
  1492. constructor(value, type, typeParams = null, sourceSpan) {
  1493. super(type, sourceSpan);
  1494. this.value = value;
  1495. this.typeParams = typeParams;
  1496. }
  1497. isEquivalent(e) {
  1498. return e instanceof ExternalExpr && this.value.name === e.value.name &&
  1499. this.value.moduleName === e.value.moduleName && this.value.runtime === e.value.runtime;
  1500. }
  1501. isConstant() {
  1502. return false;
  1503. }
  1504. visitExpression(visitor, context) {
  1505. return visitor.visitExternalExpr(this, context);
  1506. }
  1507. }
  1508. class ExternalReference {
  1509. constructor(moduleName, name, runtime) {
  1510. this.moduleName = moduleName;
  1511. this.name = name;
  1512. this.runtime = runtime;
  1513. }
  1514. }
  1515. class ConditionalExpr extends Expression {
  1516. constructor(condition, trueCase, falseCase = null, type, sourceSpan) {
  1517. super(type || trueCase.type, sourceSpan);
  1518. this.condition = condition;
  1519. this.falseCase = falseCase;
  1520. this.trueCase = trueCase;
  1521. }
  1522. isEquivalent(e) {
  1523. return e instanceof ConditionalExpr && this.condition.isEquivalent(e.condition) &&
  1524. this.trueCase.isEquivalent(e.trueCase) && nullSafeIsEquivalent(this.falseCase, e.falseCase);
  1525. }
  1526. isConstant() {
  1527. return false;
  1528. }
  1529. visitExpression(visitor, context) {
  1530. return visitor.visitConditionalExpr(this, context);
  1531. }
  1532. }
  1533. class NotExpr extends Expression {
  1534. constructor(condition, sourceSpan) {
  1535. super(BOOL_TYPE, sourceSpan);
  1536. this.condition = condition;
  1537. }
  1538. isEquivalent(e) {
  1539. return e instanceof NotExpr && this.condition.isEquivalent(e.condition);
  1540. }
  1541. isConstant() {
  1542. return false;
  1543. }
  1544. visitExpression(visitor, context) {
  1545. return visitor.visitNotExpr(this, context);
  1546. }
  1547. }
  1548. class AssertNotNull extends Expression {
  1549. constructor(condition, sourceSpan) {
  1550. super(condition.type, sourceSpan);
  1551. this.condition = condition;
  1552. }
  1553. isEquivalent(e) {
  1554. return e instanceof AssertNotNull && this.condition.isEquivalent(e.condition);
  1555. }
  1556. isConstant() {
  1557. return false;
  1558. }
  1559. visitExpression(visitor, context) {
  1560. return visitor.visitAssertNotNullExpr(this, context);
  1561. }
  1562. }
  1563. class CastExpr extends Expression {
  1564. constructor(value, type, sourceSpan) {
  1565. super(type, sourceSpan);
  1566. this.value = value;
  1567. }
  1568. isEquivalent(e) {
  1569. return e instanceof CastExpr && this.value.isEquivalent(e.value);
  1570. }
  1571. isConstant() {
  1572. return false;
  1573. }
  1574. visitExpression(visitor, context) {
  1575. return visitor.visitCastExpr(this, context);
  1576. }
  1577. }
  1578. class FnParam {
  1579. constructor(name, type = null) {
  1580. this.name = name;
  1581. this.type = type;
  1582. }
  1583. isEquivalent(param) {
  1584. return this.name === param.name;
  1585. }
  1586. }
  1587. class FunctionExpr extends Expression {
  1588. constructor(params, statements, type, sourceSpan, name) {
  1589. super(type, sourceSpan);
  1590. this.params = params;
  1591. this.statements = statements;
  1592. this.name = name;
  1593. }
  1594. isEquivalent(e) {
  1595. return e instanceof FunctionExpr && areAllEquivalent(this.params, e.params) &&
  1596. areAllEquivalent(this.statements, e.statements);
  1597. }
  1598. isConstant() {
  1599. return false;
  1600. }
  1601. visitExpression(visitor, context) {
  1602. return visitor.visitFunctionExpr(this, context);
  1603. }
  1604. toDeclStmt(name, modifiers) {
  1605. return new DeclareFunctionStmt(name, this.params, this.statements, this.type, modifiers, this.sourceSpan);
  1606. }
  1607. }
  1608. class UnaryOperatorExpr extends Expression {
  1609. constructor(operator, expr, type, sourceSpan, parens = true) {
  1610. super(type || NUMBER_TYPE, sourceSpan);
  1611. this.operator = operator;
  1612. this.expr = expr;
  1613. this.parens = parens;
  1614. }
  1615. isEquivalent(e) {
  1616. return e instanceof UnaryOperatorExpr && this.operator === e.operator &&
  1617. this.expr.isEquivalent(e.expr);
  1618. }
  1619. isConstant() {
  1620. return false;
  1621. }
  1622. visitExpression(visitor, context) {
  1623. return visitor.visitUnaryOperatorExpr(this, context);
  1624. }
  1625. }
  1626. class BinaryOperatorExpr extends Expression {
  1627. constructor(operator, lhs, rhs, type, sourceSpan, parens = true) {
  1628. super(type || lhs.type, sourceSpan);
  1629. this.operator = operator;
  1630. this.rhs = rhs;
  1631. this.parens = parens;
  1632. this.lhs = lhs;
  1633. }
  1634. isEquivalent(e) {
  1635. return e instanceof BinaryOperatorExpr && this.operator === e.operator &&
  1636. this.lhs.isEquivalent(e.lhs) && this.rhs.isEquivalent(e.rhs);
  1637. }
  1638. isConstant() {
  1639. return false;
  1640. }
  1641. visitExpression(visitor, context) {
  1642. return visitor.visitBinaryOperatorExpr(this, context);
  1643. }
  1644. }
  1645. class ReadPropExpr extends Expression {
  1646. constructor(receiver, name, type, sourceSpan) {
  1647. super(type, sourceSpan);
  1648. this.receiver = receiver;
  1649. this.name = name;
  1650. }
  1651. isEquivalent(e) {
  1652. return e instanceof ReadPropExpr && this.receiver.isEquivalent(e.receiver) &&
  1653. this.name === e.name;
  1654. }
  1655. isConstant() {
  1656. return false;
  1657. }
  1658. visitExpression(visitor, context) {
  1659. return visitor.visitReadPropExpr(this, context);
  1660. }
  1661. set(value) {
  1662. return new WritePropExpr(this.receiver, this.name, value, null, this.sourceSpan);
  1663. }
  1664. }
  1665. class ReadKeyExpr extends Expression {
  1666. constructor(receiver, index, type, sourceSpan) {
  1667. super(type, sourceSpan);
  1668. this.receiver = receiver;
  1669. this.index = index;
  1670. }
  1671. isEquivalent(e) {
  1672. return e instanceof ReadKeyExpr && this.receiver.isEquivalent(e.receiver) &&
  1673. this.index.isEquivalent(e.index);
  1674. }
  1675. isConstant() {
  1676. return false;
  1677. }
  1678. visitExpression(visitor, context) {
  1679. return visitor.visitReadKeyExpr(this, context);
  1680. }
  1681. set(value) {
  1682. return new WriteKeyExpr(this.receiver, this.index, value, null, this.sourceSpan);
  1683. }
  1684. }
  1685. class LiteralArrayExpr extends Expression {
  1686. constructor(entries, type, sourceSpan) {
  1687. super(type, sourceSpan);
  1688. this.entries = entries;
  1689. }
  1690. isConstant() {
  1691. return this.entries.every(e => e.isConstant());
  1692. }
  1693. isEquivalent(e) {
  1694. return e instanceof LiteralArrayExpr && areAllEquivalent(this.entries, e.entries);
  1695. }
  1696. visitExpression(visitor, context) {
  1697. return visitor.visitLiteralArrayExpr(this, context);
  1698. }
  1699. }
  1700. class LiteralMapEntry {
  1701. constructor(key, value, quoted) {
  1702. this.key = key;
  1703. this.value = value;
  1704. this.quoted = quoted;
  1705. }
  1706. isEquivalent(e) {
  1707. return this.key === e.key && this.value.isEquivalent(e.value);
  1708. }
  1709. }
  1710. class LiteralMapExpr extends Expression {
  1711. constructor(entries, type, sourceSpan) {
  1712. super(type, sourceSpan);
  1713. this.entries = entries;
  1714. this.valueType = null;
  1715. if (type) {
  1716. this.valueType = type.valueType;
  1717. }
  1718. }
  1719. isEquivalent(e) {
  1720. return e instanceof LiteralMapExpr && areAllEquivalent(this.entries, e.entries);
  1721. }
  1722. isConstant() {
  1723. return this.entries.every(e => e.value.isConstant());
  1724. }
  1725. visitExpression(visitor, context) {
  1726. return visitor.visitLiteralMapExpr(this, context);
  1727. }
  1728. }
  1729. class CommaExpr extends Expression {
  1730. constructor(parts, sourceSpan) {
  1731. super(parts[parts.length - 1].type, sourceSpan);
  1732. this.parts = parts;
  1733. }
  1734. isEquivalent(e) {
  1735. return e instanceof CommaExpr && areAllEquivalent(this.parts, e.parts);
  1736. }
  1737. isConstant() {
  1738. return false;
  1739. }
  1740. visitExpression(visitor, context) {
  1741. return visitor.visitCommaExpr(this, context);
  1742. }
  1743. }
  1744. const THIS_EXPR = new ReadVarExpr(BuiltinVar.This, null, null);
  1745. const SUPER_EXPR = new ReadVarExpr(BuiltinVar.Super, null, null);
  1746. const CATCH_ERROR_VAR = new ReadVarExpr(BuiltinVar.CatchError, null, null);
  1747. const CATCH_STACK_VAR = new ReadVarExpr(BuiltinVar.CatchStack, null, null);
  1748. const NULL_EXPR = new LiteralExpr(null, null, null);
  1749. const TYPED_NULL_EXPR = new LiteralExpr(null, INFERRED_TYPE, null);
  1750. //// Statements
  1751. var StmtModifier;
  1752. (function (StmtModifier) {
  1753. StmtModifier[StmtModifier["Final"] = 0] = "Final";
  1754. StmtModifier[StmtModifier["Private"] = 1] = "Private";
  1755. StmtModifier[StmtModifier["Exported"] = 2] = "Exported";
  1756. StmtModifier[StmtModifier["Static"] = 3] = "Static";
  1757. })(StmtModifier || (StmtModifier = {}));
  1758. class LeadingComment {
  1759. constructor(text, multiline, trailingNewline) {
  1760. this.text = text;
  1761. this.multiline = multiline;
  1762. this.trailingNewline = trailingNewline;
  1763. }
  1764. toString() {
  1765. return this.multiline ? ` ${this.text} ` : this.text;
  1766. }
  1767. }
  1768. class JSDocComment extends LeadingComment {
  1769. constructor(tags) {
  1770. super('', /* multiline */ true, /* trailingNewline */ true);
  1771. this.tags = tags;
  1772. }
  1773. toString() {
  1774. return serializeTags(this.tags);
  1775. }
  1776. }
  1777. class Statement {
  1778. constructor(modifiers = [], sourceSpan = null, leadingComments) {
  1779. this.modifiers = modifiers;
  1780. this.sourceSpan = sourceSpan;
  1781. this.leadingComments = leadingComments;
  1782. }
  1783. hasModifier(modifier) {
  1784. return this.modifiers.indexOf(modifier) !== -1;
  1785. }
  1786. addLeadingComment(leadingComment) {
  1787. var _a;
  1788. this.leadingComments = (_a = this.leadingComments) !== null && _a !== void 0 ? _a : [];
  1789. this.leadingComments.push(leadingComment);
  1790. }
  1791. }
  1792. class DeclareVarStmt extends Statement {
  1793. constructor(name, value, type, modifiers, sourceSpan, leadingComments) {
  1794. super(modifiers, sourceSpan, leadingComments);
  1795. this.name = name;
  1796. this.value = value;
  1797. this.type = type || (value && value.type) || null;
  1798. }
  1799. isEquivalent(stmt) {
  1800. return stmt instanceof DeclareVarStmt && this.name === stmt.name &&
  1801. (this.value ? !!stmt.value && this.value.isEquivalent(stmt.value) : !stmt.value);
  1802. }
  1803. visitStatement(visitor, context) {
  1804. return visitor.visitDeclareVarStmt(this, context);
  1805. }
  1806. }
  1807. class DeclareFunctionStmt extends Statement {
  1808. constructor(name, params, statements, type, modifiers, sourceSpan, leadingComments) {
  1809. super(modifiers, sourceSpan, leadingComments);
  1810. this.name = name;
  1811. this.params = params;
  1812. this.statements = statements;
  1813. this.type = type || null;
  1814. }
  1815. isEquivalent(stmt) {
  1816. return stmt instanceof DeclareFunctionStmt && areAllEquivalent(this.params, stmt.params) &&
  1817. areAllEquivalent(this.statements, stmt.statements);
  1818. }
  1819. visitStatement(visitor, context) {
  1820. return visitor.visitDeclareFunctionStmt(this, context);
  1821. }
  1822. }
  1823. class ExpressionStatement extends Statement {
  1824. constructor(expr, sourceSpan, leadingComments) {
  1825. super([], sourceSpan, leadingComments);
  1826. this.expr = expr;
  1827. }
  1828. isEquivalent(stmt) {
  1829. return stmt instanceof ExpressionStatement && this.expr.isEquivalent(stmt.expr);
  1830. }
  1831. visitStatement(visitor, context) {
  1832. return visitor.visitExpressionStmt(this, context);
  1833. }
  1834. }
  1835. class ReturnStatement extends Statement {
  1836. constructor(value, sourceSpan = null, leadingComments) {
  1837. super([], sourceSpan, leadingComments);
  1838. this.value = value;
  1839. }
  1840. isEquivalent(stmt) {
  1841. return stmt instanceof ReturnStatement && this.value.isEquivalent(stmt.value);
  1842. }
  1843. visitStatement(visitor, context) {
  1844. return visitor.visitReturnStmt(this, context);
  1845. }
  1846. }
  1847. class AbstractClassPart {
  1848. constructor(type = null, modifiers = []) {
  1849. this.type = type;
  1850. this.modifiers = modifiers;
  1851. }
  1852. hasModifier(modifier) {
  1853. return this.modifiers.indexOf(modifier) !== -1;
  1854. }
  1855. }
  1856. class ClassField extends AbstractClassPart {
  1857. constructor(name, type, modifiers, initializer) {
  1858. super(type, modifiers);
  1859. this.name = name;
  1860. this.initializer = initializer;
  1861. }
  1862. isEquivalent(f) {
  1863. return this.name === f.name;
  1864. }
  1865. }
  1866. class ClassMethod extends AbstractClassPart {
  1867. constructor(name, params, body, type, modifiers) {
  1868. super(type, modifiers);
  1869. this.name = name;
  1870. this.params = params;
  1871. this.body = body;
  1872. }
  1873. isEquivalent(m) {
  1874. return this.name === m.name && areAllEquivalent(this.body, m.body);
  1875. }
  1876. }
  1877. class ClassGetter extends AbstractClassPart {
  1878. constructor(name, body, type, modifiers) {
  1879. super(type, modifiers);
  1880. this.name = name;
  1881. this.body = body;
  1882. }
  1883. isEquivalent(m) {
  1884. return this.name === m.name && areAllEquivalent(this.body, m.body);
  1885. }
  1886. }
  1887. class ClassStmt extends Statement {
  1888. constructor(name, parent, fields, getters, constructorMethod, methods, modifiers, sourceSpan, leadingComments) {
  1889. super(modifiers, sourceSpan, leadingComments);
  1890. this.name = name;
  1891. this.parent = parent;
  1892. this.fields = fields;
  1893. this.getters = getters;
  1894. this.constructorMethod = constructorMethod;
  1895. this.methods = methods;
  1896. }
  1897. isEquivalent(stmt) {
  1898. return stmt instanceof ClassStmt && this.name === stmt.name &&
  1899. nullSafeIsEquivalent(this.parent, stmt.parent) &&
  1900. areAllEquivalent(this.fields, stmt.fields) &&
  1901. areAllEquivalent(this.getters, stmt.getters) &&
  1902. this.constructorMethod.isEquivalent(stmt.constructorMethod) &&
  1903. areAllEquivalent(this.methods, stmt.methods);
  1904. }
  1905. visitStatement(visitor, context) {
  1906. return visitor.visitDeclareClassStmt(this, context);
  1907. }
  1908. }
  1909. class IfStmt extends Statement {
  1910. constructor(condition, trueCase, falseCase = [], sourceSpan, leadingComments) {
  1911. super([], sourceSpan, leadingComments);
  1912. this.condition = condition;
  1913. this.trueCase = trueCase;
  1914. this.falseCase = falseCase;
  1915. }
  1916. isEquivalent(stmt) {
  1917. return stmt instanceof IfStmt && this.condition.isEquivalent(stmt.condition) &&
  1918. areAllEquivalent(this.trueCase, stmt.trueCase) &&
  1919. areAllEquivalent(this.falseCase, stmt.falseCase);
  1920. }
  1921. visitStatement(visitor, context) {
  1922. return visitor.visitIfStmt(this, context);
  1923. }
  1924. }
  1925. class TryCatchStmt extends Statement {
  1926. constructor(bodyStmts, catchStmts, sourceSpan = null, leadingComments) {
  1927. super([], sourceSpan, leadingComments);
  1928. this.bodyStmts = bodyStmts;
  1929. this.catchStmts = catchStmts;
  1930. }
  1931. isEquivalent(stmt) {
  1932. return stmt instanceof TryCatchStmt && areAllEquivalent(this.bodyStmts, stmt.bodyStmts) &&
  1933. areAllEquivalent(this.catchStmts, stmt.catchStmts);
  1934. }
  1935. visitStatement(visitor, context) {
  1936. return visitor.visitTryCatchStmt(this, context);
  1937. }
  1938. }
  1939. class ThrowStmt extends Statement {
  1940. constructor(error, sourceSpan = null, leadingComments) {
  1941. super([], sourceSpan, leadingComments);
  1942. this.error = error;
  1943. }
  1944. isEquivalent(stmt) {
  1945. return stmt instanceof TryCatchStmt && this.error.isEquivalent(stmt.error);
  1946. }
  1947. visitStatement(visitor, context) {
  1948. return visitor.visitThrowStmt(this, context);
  1949. }
  1950. }
  1951. class AstTransformer {
  1952. transformExpr(expr, context) {
  1953. return expr;
  1954. }
  1955. transformStmt(stmt, context) {
  1956. return stmt;
  1957. }
  1958. visitReadVarExpr(ast, context) {
  1959. return this.transformExpr(ast, context);
  1960. }
  1961. visitWrappedNodeExpr(ast, context) {
  1962. return this.transformExpr(ast, context);
  1963. }
  1964. visitTypeofExpr(expr, context) {
  1965. return this.transformExpr(new TypeofExpr(expr.expr.visitExpression(this, context), expr.type, expr.sourceSpan), context);
  1966. }
  1967. visitWriteVarExpr(expr, context) {
  1968. return this.transformExpr(new WriteVarExpr(expr.name, expr.value.visitExpression(this, context), expr.type, expr.sourceSpan), context);
  1969. }
  1970. visitWriteKeyExpr(expr, context) {
  1971. return this.transformExpr(new WriteKeyExpr(expr.receiver.visitExpression(this, context), expr.index.visitExpression(this, context), expr.value.visitExpression(this, context), expr.type, expr.sourceSpan), context);
  1972. }
  1973. visitWritePropExpr(expr, context) {
  1974. return this.transformExpr(new WritePropExpr(expr.receiver.visitExpression(this, context), expr.name, expr.value.visitExpression(this, context), expr.type, expr.sourceSpan), context);
  1975. }
  1976. visitInvokeMethodExpr(ast, context) {
  1977. const method = ast.builtin || ast.name;
  1978. return this.transformExpr(new InvokeMethodExpr(ast.receiver.visitExpression(this, context), method, this.visitAllExpressions(ast.args, context), ast.type, ast.sourceSpan), context);
  1979. }
  1980. visitInvokeFunctionExpr(ast, context) {
  1981. return this.transformExpr(new InvokeFunctionExpr(ast.fn.visitExpression(this, context), this.visitAllExpressions(ast.args, context), ast.type, ast.sourceSpan), context);
  1982. }
  1983. visitTaggedTemplateExpr(ast, context) {
  1984. return this.transformExpr(new TaggedTemplateExpr(ast.tag.visitExpression(this, context), new TemplateLiteral(ast.template.elements, ast.template.expressions.map((e) => e.visitExpression(this, context))), ast.type, ast.sourceSpan), context);
  1985. }
  1986. visitInstantiateExpr(ast, context) {
  1987. return this.transformExpr(new InstantiateExpr(ast.classExpr.visitExpression(this, context), this.visitAllExpressions(ast.args, context), ast.type, ast.sourceSpan), context);
  1988. }
  1989. visitLiteralExpr(ast, context) {
  1990. return this.transformExpr(ast, context);
  1991. }
  1992. visitLocalizedString(ast, context) {
  1993. return this.transformExpr(new LocalizedString(ast.metaBlock, ast.messageParts, ast.placeHolderNames, this.visitAllExpressions(ast.expressions, context), ast.sourceSpan), context);
  1994. }
  1995. visitExternalExpr(ast, context) {
  1996. return this.transformExpr(ast, context);
  1997. }
  1998. visitConditionalExpr(ast, context) {
  1999. return this.transformExpr(new ConditionalExpr(ast.condition.visitExpression(this, context), ast.trueCase.visitExpression(this, context), ast.falseCase.visitExpression(this, context), ast.type, ast.sourceSpan), context);
  2000. }
  2001. visitNotExpr(ast, context) {
  2002. return this.transformExpr(new NotExpr(ast.condition.visitExpression(this, context), ast.sourceSpan), context);
  2003. }
  2004. visitAssertNotNullExpr(ast, context) {
  2005. return this.transformExpr(new AssertNotNull(ast.condition.visitExpression(this, context), ast.sourceSpan), context);
  2006. }
  2007. visitCastExpr(ast, context) {
  2008. return this.transformExpr(new CastExpr(ast.value.visitExpression(this, context), ast.type, ast.sourceSpan), context);
  2009. }
  2010. visitFunctionExpr(ast, context) {
  2011. return this.transformExpr(new FunctionExpr(ast.params, this.visitAllStatements(ast.statements, context), ast.type, ast.sourceSpan), context);
  2012. }
  2013. visitUnaryOperatorExpr(ast, context) {
  2014. return this.transformExpr(new UnaryOperatorExpr(ast.operator, ast.expr.visitExpression(this, context), ast.type, ast.sourceSpan), context);
  2015. }
  2016. visitBinaryOperatorExpr(ast, context) {
  2017. return this.transformExpr(new BinaryOperatorExpr(ast.operator, ast.lhs.visitExpression(this, context), ast.rhs.visitExpression(this, context), ast.type, ast.sourceSpan), context);
  2018. }
  2019. visitReadPropExpr(ast, context) {
  2020. return this.transformExpr(new ReadPropExpr(ast.receiver.visitExpression(this, context), ast.name, ast.type, ast.sourceSpan), context);
  2021. }
  2022. visitReadKeyExpr(ast, context) {
  2023. return this.transformExpr(new ReadKeyExpr(ast.receiver.visitExpression(this, context), ast.index.visitExpression(this, context), ast.type, ast.sourceSpan), context);
  2024. }
  2025. visitLiteralArrayExpr(ast, context) {
  2026. return this.transformExpr(new LiteralArrayExpr(this.visitAllExpressions(ast.entries, context), ast.type, ast.sourceSpan), context);
  2027. }
  2028. visitLiteralMapExpr(ast, context) {
  2029. const entries = ast.entries.map((entry) => new LiteralMapEntry(entry.key, entry.value.visitExpression(this, context), entry.quoted));
  2030. const mapType = new MapType(ast.valueType);
  2031. return this.transformExpr(new LiteralMapExpr(entries, mapType, ast.sourceSpan), context);
  2032. }
  2033. visitCommaExpr(ast, context) {
  2034. return this.transformExpr(new CommaExpr(this.visitAllExpressions(ast.parts, context), ast.sourceSpan), context);
  2035. }
  2036. visitAllExpressions(exprs, context) {
  2037. return exprs.map(expr => expr.visitExpression(this, context));
  2038. }
  2039. visitDeclareVarStmt(stmt, context) {
  2040. const value = stmt.value && stmt.value.visitExpression(this, context);
  2041. return this.transformStmt(new DeclareVarStmt(stmt.name, value, stmt.type, stmt.modifiers, stmt.sourceSpan, stmt.leadingComments), context);
  2042. }
  2043. visitDeclareFunctionStmt(stmt, context) {
  2044. return this.transformStmt(new DeclareFunctionStmt(stmt.name, stmt.params, this.visitAllStatements(stmt.statements, context), stmt.type, stmt.modifiers, stmt.sourceSpan, stmt.leadingComments), context);
  2045. }
  2046. visitExpressionStmt(stmt, context) {
  2047. return this.transformStmt(new ExpressionStatement(stmt.expr.visitExpression(this, context), stmt.sourceSpan, stmt.leadingComments), context);
  2048. }
  2049. visitReturnStmt(stmt, context) {
  2050. return this.transformStmt(new ReturnStatement(stmt.value.visitExpression(this, context), stmt.sourceSpan, stmt.leadingComments), context);
  2051. }
  2052. visitDeclareClassStmt(stmt, context) {
  2053. const parent = stmt.parent.visitExpression(this, context);
  2054. const getters = stmt.getters.map(getter => new ClassGetter(getter.name, this.visitAllStatements(getter.body, context), getter.type, getter.modifiers));
  2055. const ctorMethod = stmt.constructorMethod &&
  2056. new ClassMethod(stmt.constructorMethod.name, stmt.constructorMethod.params, this.visitAllStatements(stmt.constructorMethod.body, context), stmt.constructorMethod.type, stmt.constructorMethod.modifiers);
  2057. const methods = stmt.methods.map(method => new ClassMethod(method.name, method.params, this.visitAllStatements(method.body, context), method.type, method.modifiers));
  2058. return this.transformStmt(new ClassStmt(stmt.name, parent, stmt.fields, getters, ctorMethod, methods, stmt.modifiers, stmt.sourceSpan), context);
  2059. }
  2060. visitIfStmt(stmt, context) {
  2061. return this.transformStmt(new IfStmt(stmt.condition.visitExpression(this, context), this.visitAllStatements(stmt.trueCase, context), this.visitAllStatements(stmt.falseCase, context), stmt.sourceSpan, stmt.leadingComments), context);
  2062. }
  2063. visitTryCatchStmt(stmt, context) {
  2064. return this.transformStmt(new TryCatchStmt(this.visitAllStatements(stmt.bodyStmts, context), this.visitAllStatements(stmt.catchStmts, context), stmt.sourceSpan, stmt.leadingComments), context);
  2065. }
  2066. visitThrowStmt(stmt, context) {
  2067. return this.transformStmt(new ThrowStmt(stmt.error.visitExpression(this, context), stmt.sourceSpan, stmt.leadingComments), context);
  2068. }
  2069. visitAllStatements(stmts, context) {
  2070. return stmts.map(stmt => stmt.visitStatement(this, context));
  2071. }
  2072. }
  2073. class RecursiveAstVisitor {
  2074. visitType(ast, context) {
  2075. return ast;
  2076. }
  2077. visitExpression(ast, context) {
  2078. if (ast.type) {
  2079. ast.type.visitType(this, context);
  2080. }
  2081. return ast;
  2082. }
  2083. visitBuiltinType(type, context) {
  2084. return this.visitType(type, context);
  2085. }
  2086. visitExpressionType(type, context) {
  2087. type.value.visitExpression(this, context);
  2088. if (type.typeParams !== null) {
  2089. type.typeParams.forEach(param => this.visitType(param, context));
  2090. }
  2091. return this.visitType(type, context);
  2092. }
  2093. visitArrayType(type, context) {
  2094. return this.visitType(type, context);
  2095. }
  2096. visitMapType(type, context) {
  2097. return this.visitType(type, context);
  2098. }
  2099. visitWrappedNodeExpr(ast, context) {
  2100. return ast;
  2101. }
  2102. visitTypeofExpr(ast, context) {
  2103. return this.visitExpression(ast, context);
  2104. }
  2105. visitReadVarExpr(ast, context) {
  2106. return this.visitExpression(ast, context);
  2107. }
  2108. visitWriteVarExpr(ast, context) {
  2109. ast.value.visitExpression(this, context);
  2110. return this.visitExpression(ast, context);
  2111. }
  2112. visitWriteKeyExpr(ast, context) {
  2113. ast.receiver.visitExpression(this, context);
  2114. ast.index.visitExpression(this, context);
  2115. ast.value.visitExpression(this, context);
  2116. return this.visitExpression(ast, context);
  2117. }
  2118. visitWritePropExpr(ast, context) {
  2119. ast.receiver.visitExpression(this, context);
  2120. ast.value.visitExpression(this, context);
  2121. return this.visitExpression(ast, context);
  2122. }
  2123. visitInvokeMethodExpr(ast, context) {
  2124. ast.receiver.visitExpression(this, context);
  2125. this.visitAllExpressions(ast.args, context);
  2126. return this.visitExpression(ast, context);
  2127. }
  2128. visitInvokeFunctionExpr(ast, context) {
  2129. ast.fn.visitExpression(this, context);
  2130. this.visitAllExpressions(ast.args, context);
  2131. return this.visitExpression(ast, context);
  2132. }
  2133. visitTaggedTemplateExpr(ast, context) {
  2134. ast.tag.visitExpression(this, context);
  2135. this.visitAllExpressions(ast.template.expressions, context);
  2136. return this.visitExpression(ast, context);
  2137. }
  2138. visitInstantiateExpr(ast, context) {
  2139. ast.classExpr.visitExpression(this, context);
  2140. this.visitAllExpressions(ast.args, context);
  2141. return this.visitExpression(ast, context);
  2142. }
  2143. visitLiteralExpr(ast, context) {
  2144. return this.visitExpression(ast, context);
  2145. }
  2146. visitLocalizedString(ast, context) {
  2147. return this.visitExpression(ast, context);
  2148. }
  2149. visitExternalExpr(ast, context) {
  2150. if (ast.typeParams) {
  2151. ast.typeParams.forEach(type => type.visitType(this, context));
  2152. }
  2153. return this.visitExpression(ast, context);
  2154. }
  2155. visitConditionalExpr(ast, context) {
  2156. ast.condition.visitExpression(this, context);
  2157. ast.trueCase.visitExpression(this, context);
  2158. ast.falseCase.visitExpression(this, context);
  2159. return this.visitExpression(ast, context);
  2160. }
  2161. visitNotExpr(ast, context) {
  2162. ast.condition.visitExpression(this, context);
  2163. return this.visitExpression(ast, context);
  2164. }
  2165. visitAssertNotNullExpr(ast, context) {
  2166. ast.condition.visitExpression(this, context);
  2167. return this.visitExpression(ast, context);
  2168. }
  2169. visitCastExpr(ast, context) {
  2170. ast.value.visitExpression(this, context);
  2171. return this.visitExpression(ast, context);
  2172. }
  2173. visitFunctionExpr(ast, context) {
  2174. this.visitAllStatements(ast.statements, context);
  2175. return this.visitExpression(ast, context);
  2176. }
  2177. visitUnaryOperatorExpr(ast, context) {
  2178. ast.expr.visitExpression(this, context);
  2179. return this.visitExpression(ast, context);
  2180. }
  2181. visitBinaryOperatorExpr(ast, context) {
  2182. ast.lhs.visitExpression(this, context);
  2183. ast.rhs.visitExpression(this, context);
  2184. return this.visitExpression(ast, context);
  2185. }
  2186. visitReadPropExpr(ast, context) {
  2187. ast.receiver.visitExpression(this, context);
  2188. return this.visitExpression(ast, context);
  2189. }
  2190. visitReadKeyExpr(ast, context) {
  2191. ast.receiver.visitExpression(this, context);
  2192. ast.index.visitExpression(this, context);
  2193. return this.visitExpression(ast, context);
  2194. }
  2195. visitLiteralArrayExpr(ast, context) {
  2196. this.visitAllExpressions(ast.entries, context);
  2197. return this.visitExpression(ast, context);
  2198. }
  2199. visitLiteralMapExpr(ast, context) {
  2200. ast.entries.forEach((entry) => entry.value.visitExpression(this, context));
  2201. return this.visitExpression(ast, context);
  2202. }
  2203. visitCommaExpr(ast, context) {
  2204. this.visitAllExpressions(ast.parts, context);
  2205. return this.visitExpression(ast, context);
  2206. }
  2207. visitAllExpressions(exprs, context) {
  2208. exprs.forEach(expr => expr.visitExpression(this, context));
  2209. }
  2210. visitDeclareVarStmt(stmt, context) {
  2211. if (stmt.value) {
  2212. stmt.value.visitExpression(this, context);
  2213. }
  2214. if (stmt.type) {
  2215. stmt.type.visitType(this, context);
  2216. }
  2217. return stmt;
  2218. }
  2219. visitDeclareFunctionStmt(stmt, context) {
  2220. this.visitAllStatements(stmt.statements, context);
  2221. if (stmt.type) {
  2222. stmt.type.visitType(this, context);
  2223. }
  2224. return stmt;
  2225. }
  2226. visitExpressionStmt(stmt, context) {
  2227. stmt.expr.visitExpression(this, context);
  2228. return stmt;
  2229. }
  2230. visitReturnStmt(stmt, context) {
  2231. stmt.value.visitExpression(this, context);
  2232. return stmt;
  2233. }
  2234. visitDeclareClassStmt(stmt, context) {
  2235. stmt.parent.visitExpression(this, context);
  2236. stmt.getters.forEach(getter => this.visitAllStatements(getter.body, context));
  2237. if (stmt.constructorMethod) {
  2238. this.visitAllStatements(stmt.constructorMethod.body, context);
  2239. }
  2240. stmt.methods.forEach(method => this.visitAllStatements(method.body, context));
  2241. return stmt;
  2242. }
  2243. visitIfStmt(stmt, context) {
  2244. stmt.condition.visitExpression(this, context);
  2245. this.visitAllStatements(stmt.trueCase, context);
  2246. this.visitAllStatements(stmt.falseCase, context);
  2247. return stmt;
  2248. }
  2249. visitTryCatchStmt(stmt, context) {
  2250. this.visitAllStatements(stmt.bodyStmts, context);
  2251. this.visitAllStatements(stmt.catchStmts, context);
  2252. return stmt;
  2253. }
  2254. visitThrowStmt(stmt, context) {
  2255. stmt.error.visitExpression(this, context);
  2256. return stmt;
  2257. }
  2258. visitAllStatements(stmts, context) {
  2259. stmts.forEach(stmt => stmt.visitStatement(this, context));
  2260. }
  2261. }
  2262. function findReadVarNames(stmts) {
  2263. const visitor = new _ReadVarVisitor();
  2264. visitor.visitAllStatements(stmts, null);
  2265. return visitor.varNames;
  2266. }
  2267. class _ReadVarVisitor extends RecursiveAstVisitor {
  2268. constructor() {
  2269. super(...arguments);
  2270. this.varNames = new Set();
  2271. }
  2272. visitDeclareFunctionStmt(stmt, context) {
  2273. // Don't descend into nested functions
  2274. return stmt;
  2275. }
  2276. visitDeclareClassStmt(stmt, context) {
  2277. // Don't descend into nested classes
  2278. return stmt;
  2279. }
  2280. visitReadVarExpr(ast, context) {
  2281. if (ast.name) {
  2282. this.varNames.add(ast.name);
  2283. }
  2284. return null;
  2285. }
  2286. }
  2287. function collectExternalReferences(stmts) {
  2288. const visitor = new _FindExternalReferencesVisitor();
  2289. visitor.visitAllStatements(stmts, null);
  2290. return visitor.externalReferences;
  2291. }
  2292. class _FindExternalReferencesVisitor extends RecursiveAstVisitor {
  2293. constructor() {
  2294. super(...arguments);
  2295. this.externalReferences = [];
  2296. }
  2297. visitExternalExpr(e, context) {
  2298. this.externalReferences.push(e.value);
  2299. return super.visitExternalExpr(e, context);
  2300. }
  2301. }
  2302. function applySourceSpanToStatementIfNeeded(stmt, sourceSpan) {
  2303. if (!sourceSpan) {
  2304. return stmt;
  2305. }
  2306. const transformer = new _ApplySourceSpanTransformer(sourceSpan);
  2307. return stmt.visitStatement(transformer, null);
  2308. }
  2309. function applySourceSpanToExpressionIfNeeded(expr, sourceSpan) {
  2310. if (!sourceSpan) {
  2311. return expr;
  2312. }
  2313. const transformer = new _ApplySourceSpanTransformer(sourceSpan);
  2314. return expr.visitExpression(transformer, null);
  2315. }
  2316. class _ApplySourceSpanTransformer extends AstTransformer {
  2317. constructor(sourceSpan) {
  2318. super();
  2319. this.sourceSpan = sourceSpan;
  2320. }
  2321. _clone(obj) {
  2322. const clone = Object.create(obj.constructor.prototype);
  2323. for (let prop of Object.keys(obj)) {
  2324. clone[prop] = obj[prop];
  2325. }
  2326. return clone;
  2327. }
  2328. transformExpr(expr, context) {
  2329. if (!expr.sourceSpan) {
  2330. expr = this._clone(expr);
  2331. expr.sourceSpan = this.sourceSpan;
  2332. }
  2333. return expr;
  2334. }
  2335. transformStmt(stmt, context) {
  2336. if (!stmt.sourceSpan) {
  2337. stmt = this._clone(stmt);
  2338. stmt.sourceSpan = this.sourceSpan;
  2339. }
  2340. return stmt;
  2341. }
  2342. }
  2343. function leadingComment(text, multiline = false, trailingNewline = true) {
  2344. return new LeadingComment(text, multiline, trailingNewline);
  2345. }
  2346. function jsDocComment(tags = []) {
  2347. return new JSDocComment(tags);
  2348. }
  2349. function variable(name, type, sourceSpan) {
  2350. return new ReadVarExpr(name, type, sourceSpan);
  2351. }
  2352. function importExpr(id, typeParams = null, sourceSpan) {
  2353. return new ExternalExpr(id, null, typeParams, sourceSpan);
  2354. }
  2355. function importType(id, typeParams, typeModifiers) {
  2356. return id != null ? expressionType(importExpr(id, typeParams, null), typeModifiers) : null;
  2357. }
  2358. function expressionType(expr, typeModifiers, typeParams) {
  2359. return new ExpressionType(expr, typeModifiers, typeParams);
  2360. }
  2361. function typeofExpr(expr) {
  2362. return new TypeofExpr(expr);
  2363. }
  2364. function literalArr(values, type, sourceSpan) {
  2365. return new LiteralArrayExpr(values, type, sourceSpan);
  2366. }
  2367. function literalMap(values, type = null) {
  2368. return new LiteralMapExpr(values.map(e => new LiteralMapEntry(e.key, e.value, e.quoted)), type, null);
  2369. }
  2370. function unary(operator, expr, type, sourceSpan) {
  2371. return new UnaryOperatorExpr(operator, expr, type, sourceSpan);
  2372. }
  2373. function not(expr, sourceSpan) {
  2374. return new NotExpr(expr, sourceSpan);
  2375. }
  2376. function assertNotNull(expr, sourceSpan) {
  2377. return new AssertNotNull(expr, sourceSpan);
  2378. }
  2379. function fn(params, body, type, sourceSpan, name) {
  2380. return new FunctionExpr(params, body, type, sourceSpan, name);
  2381. }
  2382. function ifStmt(condition, thenClause, elseClause, sourceSpan, leadingComments) {
  2383. return new IfStmt(condition, thenClause, elseClause, sourceSpan, leadingComments);
  2384. }
  2385. function taggedTemplate(tag, template, type, sourceSpan) {
  2386. return new TaggedTemplateExpr(tag, template, type, sourceSpan);
  2387. }
  2388. function literal(value, type, sourceSpan) {
  2389. return new LiteralExpr(value, type, sourceSpan);
  2390. }
  2391. function localizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan) {
  2392. return new LocalizedString(metaBlock, messageParts, placeholderNames, expressions, sourceSpan);
  2393. }
  2394. function isNull(exp) {
  2395. return exp instanceof LiteralExpr && exp.value === null;
  2396. }
  2397. /*
  2398. * Serializes a `Tag` into a string.
  2399. * Returns a string like " @foo {bar} baz" (note the leading whitespace before `@foo`).
  2400. */
  2401. function tagToString(tag) {
  2402. let out = '';
  2403. if (tag.tagName) {
  2404. out += ` @${tag.tagName}`;
  2405. }
  2406. if (tag.text) {
  2407. if (tag.text.match(/\/\*|\*\//)) {
  2408. throw new Error('JSDoc text cannot contain "/*" and "*/"');
  2409. }
  2410. out += ' ' + tag.text.replace(/@/g, '\\@');
  2411. }
  2412. return out;
  2413. }
  2414. function serializeTags(tags) {
  2415. if (tags.length === 0)
  2416. return '';
  2417. if (tags.length === 1 && tags[0].tagName && !tags[0].text) {
  2418. // The JSDOC comment is a single simple tag: e.g `/** @tagname */`.
  2419. return `*${tagToString(tags[0])} `;
  2420. }
  2421. let out = '*\n';
  2422. for (const tag of tags) {
  2423. out += ' *';
  2424. // If the tagToString is multi-line, insert " * " prefixes on lines.
  2425. out += tagToString(tag).replace(/\n/g, '\n * ');
  2426. out += '\n';
  2427. }
  2428. out += ' ';
  2429. return out;
  2430. }
  2431. /**
  2432. * @license
  2433. * Copyright Google LLC All Rights Reserved.
  2434. *
  2435. * Use of this source code is governed by an MIT-style license that can be
  2436. * found in the LICENSE file at https://angular.io/license
  2437. */
  2438. const CONSTANT_PREFIX = '_c';
  2439. /**
  2440. * `ConstantPool` tries to reuse literal factories when two or more literals are identical.
  2441. * We determine whether literals are identical by creating a key out of their AST using the
  2442. * `KeyVisitor`. This constant is used to replace dynamic expressions which can't be safely
  2443. * converted into a key. E.g. given an expression `{foo: bar()}`, since we don't know what
  2444. * the result of `bar` will be, we create a key that looks like `{foo: <unknown>}`. Note
  2445. * that we use a variable, rather than something like `null` in order to avoid collisions.
  2446. */
  2447. const UNKNOWN_VALUE_KEY = variable('<unknown>');
  2448. /**
  2449. * Context to use when producing a key.
  2450. *
  2451. * This ensures we see the constant not the reference variable when producing
  2452. * a key.
  2453. */
  2454. const KEY_CONTEXT = {};
  2455. /**
  2456. * Generally all primitive values are excluded from the `ConstantPool`, but there is an exclusion
  2457. * for strings that reach a certain length threshold. This constant defines the length threshold for
  2458. * strings.
  2459. */
  2460. const POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS = 50;
  2461. /**
  2462. * A node that is a place-holder that allows the node to be replaced when the actual
  2463. * node is known.
  2464. *
  2465. * This allows the constant pool to change an expression from a direct reference to
  2466. * a constant to a shared constant. It returns a fix-up node that is later allowed to
  2467. * change the referenced expression.
  2468. */
  2469. class FixupExpression extends Expression {
  2470. constructor(resolved) {
  2471. super(resolved.type);
  2472. this.resolved = resolved;
  2473. this.original = resolved;
  2474. }
  2475. visitExpression(visitor, context) {
  2476. if (context === KEY_CONTEXT) {
  2477. // When producing a key we want to traverse the constant not the
  2478. // variable used to refer to it.
  2479. return this.original.visitExpression(visitor, context);
  2480. }
  2481. else {
  2482. return this.resolved.visitExpression(visitor, context);
  2483. }
  2484. }
  2485. isEquivalent(e) {
  2486. return e instanceof FixupExpression && this.resolved.isEquivalent(e.resolved);
  2487. }
  2488. isConstant() {
  2489. return true;
  2490. }
  2491. fixup(expression) {
  2492. this.resolved = expression;
  2493. this.shared = true;
  2494. }
  2495. }
  2496. /**
  2497. * A constant pool allows a code emitter to share constant in an output context.
  2498. *
  2499. * The constant pool also supports sharing access to ivy definitions references.
  2500. */
  2501. class ConstantPool {
  2502. constructor(isClosureCompilerEnabled = false) {
  2503. this.isClosureCompilerEnabled = isClosureCompilerEnabled;
  2504. this.statements = [];
  2505. this.literals = new Map();
  2506. this.literalFactories = new Map();
  2507. this.injectorDefinitions = new Map();
  2508. this.directiveDefinitions = new Map();
  2509. this.componentDefinitions = new Map();
  2510. this.pipeDefinitions = new Map();
  2511. this.nextNameIndex = 0;
  2512. }
  2513. getConstLiteral(literal, forceShared) {
  2514. if ((literal instanceof LiteralExpr && !isLongStringLiteral(literal)) ||
  2515. literal instanceof FixupExpression) {
  2516. // Do no put simple literals into the constant pool or try to produce a constant for a
  2517. // reference to a constant.
  2518. return literal;
  2519. }
  2520. const key = this.keyOf(literal);
  2521. let fixup = this.literals.get(key);
  2522. let newValue = false;
  2523. if (!fixup) {
  2524. fixup = new FixupExpression(literal);
  2525. this.literals.set(key, fixup);
  2526. newValue = true;
  2527. }
  2528. if ((!newValue && !fixup.shared) || (newValue && forceShared)) {
  2529. // Replace the expression with a variable
  2530. const name = this.freshName();
  2531. let definition;
  2532. let usage;
  2533. if (this.isClosureCompilerEnabled && isLongStringLiteral(literal)) {
  2534. // For string literals, Closure will **always** inline the string at
  2535. // **all** usages, duplicating it each time. For large strings, this
  2536. // unnecessarily bloats bundle size. To work around this restriction, we
  2537. // wrap the string in a function, and call that function for each usage.
  2538. // This tricks Closure into using inline logic for functions instead of
  2539. // string literals. Function calls are only inlined if the body is small
  2540. // enough to be worth it. By doing this, very large strings will be
  2541. // shared across multiple usages, rather than duplicating the string at
  2542. // each usage site.
  2543. //
  2544. // const myStr = function() { return "very very very long string"; };
  2545. // const usage1 = myStr();
  2546. // const usage2 = myStr();
  2547. definition = variable(name).set(new FunctionExpr([], // Params.
  2548. [
  2549. // Statements.
  2550. new ReturnStatement(literal),
  2551. ]));
  2552. usage = variable(name).callFn([]);
  2553. }
  2554. else {
  2555. // Just declare and use the variable directly, without a function call
  2556. // indirection. This saves a few bytes and avoids an unncessary call.
  2557. definition = variable(name).set(literal);
  2558. usage = variable(name);
  2559. }
  2560. this.statements.push(definition.toDeclStmt(INFERRED_TYPE, [StmtModifier.Final]));
  2561. fixup.fixup(usage);
  2562. }
  2563. return fixup;
  2564. }
  2565. getDefinition(type, kind, ctx, forceShared = false) {
  2566. const definitions = this.definitionsOf(kind);
  2567. let fixup = definitions.get(type);
  2568. let newValue = false;
  2569. if (!fixup) {
  2570. const property = this.propertyNameOf(kind);
  2571. fixup = new FixupExpression(ctx.importExpr(type).prop(property));
  2572. definitions.set(type, fixup);
  2573. newValue = true;
  2574. }
  2575. if ((!newValue && !fixup.shared) || (newValue && forceShared)) {
  2576. const name = this.freshName();
  2577. this.statements.push(variable(name).set(fixup.resolved).toDeclStmt(INFERRED_TYPE, [StmtModifier.Final]));
  2578. fixup.fixup(variable(name));
  2579. }
  2580. return fixup;
  2581. }
  2582. getLiteralFactory(literal) {
  2583. // Create a pure function that builds an array of a mix of constant and variable expressions
  2584. if (literal instanceof LiteralArrayExpr) {
  2585. const argumentsForKey = literal.entries.map(e => e.isConstant() ? e : UNKNOWN_VALUE_KEY);
  2586. const key = this.keyOf(literalArr(argumentsForKey));
  2587. return this._getLiteralFactory(key, literal.entries, entries => literalArr(entries));
  2588. }
  2589. else {
  2590. const expressionForKey = literalMap(literal.entries.map(e => ({
  2591. key: e.key,
  2592. value: e.value.isConstant() ? e.value : UNKNOWN_VALUE_KEY,
  2593. quoted: e.quoted
  2594. })));
  2595. const key = this.keyOf(expressionForKey);
  2596. return this._getLiteralFactory(key, literal.entries.map(e => e.value), entries => literalMap(entries.map((value, index) => ({
  2597. key: literal.entries[index].key,
  2598. value,
  2599. quoted: literal.entries[index].quoted
  2600. }))));
  2601. }
  2602. }
  2603. _getLiteralFactory(key, values, resultMap) {
  2604. let literalFactory = this.literalFactories.get(key);
  2605. const literalFactoryArguments = values.filter((e => !e.isConstant()));
  2606. if (!literalFactory) {
  2607. const resultExpressions = values.map((e, index) => e.isConstant() ? this.getConstLiteral(e, true) : variable(`a${index}`));
  2608. const parameters = resultExpressions.filter(isVariable).map(e => new FnParam(e.name, DYNAMIC_TYPE));
  2609. const pureFunctionDeclaration = fn(parameters, [new ReturnStatement(resultMap(resultExpressions))], INFERRED_TYPE);
  2610. const name = this.freshName();
  2611. this.statements.push(variable(name).set(pureFunctionDeclaration).toDeclStmt(INFERRED_TYPE, [
  2612. StmtModifier.Final
  2613. ]));
  2614. literalFactory = variable(name);
  2615. this.literalFactories.set(key, literalFactory);
  2616. }
  2617. return { literalFactory, literalFactoryArguments };
  2618. }
  2619. /**
  2620. * Produce a unique name.
  2621. *
  2622. * The name might be unique among different prefixes if any of the prefixes end in
  2623. * a digit so the prefix should be a constant string (not based on user input) and
  2624. * must not end in a digit.
  2625. */
  2626. uniqueName(prefix) {
  2627. return `${prefix}${this.nextNameIndex++}`;
  2628. }
  2629. definitionsOf(kind) {
  2630. switch (kind) {
  2631. case 2 /* Component */:
  2632. return this.componentDefinitions;
  2633. case 1 /* Directive */:
  2634. return this.directiveDefinitions;
  2635. case 0 /* Injector */:
  2636. return this.injectorDefinitions;
  2637. case 3 /* Pipe */:
  2638. return this.pipeDefinitions;
  2639. }
  2640. }
  2641. propertyNameOf(kind) {
  2642. switch (kind) {
  2643. case 2 /* Component */:
  2644. return 'ɵcmp';
  2645. case 1 /* Directive */:
  2646. return 'ɵdir';
  2647. case 0 /* Injector */:
  2648. return 'ɵinj';
  2649. case 3 /* Pipe */:
  2650. return 'ɵpipe';
  2651. }
  2652. }
  2653. freshName() {
  2654. return this.uniqueName(CONSTANT_PREFIX);
  2655. }
  2656. keyOf(expression) {
  2657. return expression.visitExpression(new KeyVisitor(), KEY_CONTEXT);
  2658. }
  2659. }
  2660. /**
  2661. * Visitor used to determine if 2 expressions are equivalent and can be shared in the
  2662. * `ConstantPool`.
  2663. *
  2664. * When the id (string) generated by the visitor is equal, expressions are considered equivalent.
  2665. */
  2666. class KeyVisitor {
  2667. constructor() {
  2668. this.visitWrappedNodeExpr = invalid;
  2669. this.visitWriteVarExpr = invalid;
  2670. this.visitWriteKeyExpr = invalid;
  2671. this.visitWritePropExpr = invalid;
  2672. this.visitInvokeMethodExpr = invalid;
  2673. this.visitInvokeFunctionExpr = invalid;
  2674. this.visitTaggedTemplateExpr = invalid;
  2675. this.visitInstantiateExpr = invalid;
  2676. this.visitConditionalExpr = invalid;
  2677. this.visitNotExpr = invalid;
  2678. this.visitAssertNotNullExpr = invalid;
  2679. this.visitCastExpr = invalid;
  2680. this.visitFunctionExpr = invalid;
  2681. this.visitUnaryOperatorExpr = invalid;
  2682. this.visitBinaryOperatorExpr = invalid;
  2683. this.visitReadPropExpr = invalid;
  2684. this.visitReadKeyExpr = invalid;
  2685. this.visitCommaExpr = invalid;
  2686. this.visitLocalizedString = invalid;
  2687. }
  2688. visitLiteralExpr(ast) {
  2689. return `${typeof ast.value === 'string' ? '"' + ast.value + '"' : ast.value}`;
  2690. }
  2691. visitLiteralArrayExpr(ast, context) {
  2692. return `[${ast.entries.map(entry => entry.visitExpression(this, context)).join(',')}]`;
  2693. }
  2694. visitLiteralMapExpr(ast, context) {
  2695. const mapKey = (entry) => {
  2696. const quote = entry.quoted ? '"' : '';
  2697. return `${quote}${entry.key}${quote}`;
  2698. };
  2699. const mapEntry = (entry) => `${mapKey(entry)}:${entry.value.visitExpression(this, context)}`;
  2700. return `{${ast.entries.map(mapEntry).join(',')}`;
  2701. }
  2702. visitExternalExpr(ast) {
  2703. return ast.value.moduleName ? `EX:${ast.value.moduleName}:${ast.value.name}` :
  2704. `EX:${ast.value.runtime.name}`;
  2705. }
  2706. visitReadVarExpr(node) {
  2707. return `VAR:${node.name}`;
  2708. }
  2709. visitTypeofExpr(node, context) {
  2710. return `TYPEOF:${node.expr.visitExpression(this, context)}`;
  2711. }
  2712. }
  2713. function invalid(arg) {
  2714. throw new Error(`Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`);
  2715. }
  2716. function isVariable(e) {
  2717. return e instanceof ReadVarExpr;
  2718. }
  2719. function isLongStringLiteral(expr) {
  2720. return expr instanceof LiteralExpr && typeof expr.value === 'string' &&
  2721. expr.value.length >= POOL_INCLUSION_LENGTH_THRESHOLD_FOR_STRINGS;
  2722. }
  2723. /**
  2724. * @license
  2725. * Copyright Google LLC All Rights Reserved.
  2726. *
  2727. * Use of this source code is governed by an MIT-style license that can be
  2728. * found in the LICENSE file at https://angular.io/license
  2729. */
  2730. const CORE = '@angular/core';
  2731. class Identifiers {
  2732. }
  2733. /* Methods */
  2734. Identifiers.NEW_METHOD = 'factory';
  2735. Identifiers.TRANSFORM_METHOD = 'transform';
  2736. Identifiers.PATCH_DEPS = 'patchedDeps';
  2737. Identifiers.core = { name: null, moduleName: CORE };
  2738. /* Instructions */
  2739. Identifiers.namespaceHTML = { name: 'ɵɵnamespaceHTML', moduleName: CORE };
  2740. Identifiers.namespaceMathML = { name: 'ɵɵnamespaceMathML', moduleName: CORE };
  2741. Identifiers.namespaceSVG = { name: 'ɵɵnamespaceSVG', moduleName: CORE };
  2742. Identifiers.element = { name: 'ɵɵelement', moduleName: CORE };
  2743. Identifiers.elementStart = { name: 'ɵɵelementStart', moduleName: CORE };
  2744. Identifiers.elementEnd = { name: 'ɵɵelementEnd', moduleName: CORE };
  2745. Identifiers.advance = { name: 'ɵɵadvance', moduleName: CORE };
  2746. Identifiers.syntheticHostProperty = { name: 'ɵɵsyntheticHostProperty', moduleName: CORE };
  2747. Identifiers.syntheticHostListener = { name: 'ɵɵsyntheticHostListener', moduleName: CORE };
  2748. Identifiers.attribute = { name: 'ɵɵattribute', moduleName: CORE };
  2749. Identifiers.attributeInterpolate1 = { name: 'ɵɵattributeInterpolate1', moduleName: CORE };
  2750. Identifiers.attributeInterpolate2 = { name: 'ɵɵattributeInterpolate2', moduleName: CORE };
  2751. Identifiers.attributeInterpolate3 = { name: 'ɵɵattributeInterpolate3', moduleName: CORE };
  2752. Identifiers.attributeInterpolate4 = { name: 'ɵɵattributeInterpolate4', moduleName: CORE };
  2753. Identifiers.attributeInterpolate5 = { name: 'ɵɵattributeInterpolate5', moduleName: CORE };
  2754. Identifiers.attributeInterpolate6 = { name: 'ɵɵattributeInterpolate6', moduleName: CORE };
  2755. Identifiers.attributeInterpolate7 = { name: 'ɵɵattributeInterpolate7', moduleName: CORE };
  2756. Identifiers.attributeInterpolate8 = { name: 'ɵɵattributeInterpolate8', moduleName: CORE };
  2757. Identifiers.attributeInterpolateV = { name: 'ɵɵattributeInterpolateV', moduleName: CORE };
  2758. Identifiers.classProp = { name: 'ɵɵclassProp', moduleName: CORE };
  2759. Identifiers.elementContainerStart = { name: 'ɵɵelementContainerStart', moduleName: CORE };
  2760. Identifiers.elementContainerEnd = { name: 'ɵɵelementContainerEnd', moduleName: CORE };
  2761. Identifiers.elementContainer = { name: 'ɵɵelementContainer', moduleName: CORE };
  2762. Identifiers.styleMap = { name: 'ɵɵstyleMap', moduleName: CORE };
  2763. Identifiers.styleMapInterpolate1 = { name: 'ɵɵstyleMapInterpolate1', moduleName: CORE };
  2764. Identifiers.styleMapInterpolate2 = { name: 'ɵɵstyleMapInterpolate2', moduleName: CORE };
  2765. Identifiers.styleMapInterpolate3 = { name: 'ɵɵstyleMapInterpolate3', moduleName: CORE };
  2766. Identifiers.styleMapInterpolate4 = { name: 'ɵɵstyleMapInterpolate4', moduleName: CORE };
  2767. Identifiers.styleMapInterpolate5 = { name: 'ɵɵstyleMapInterpolate5', moduleName: CORE };
  2768. Identifiers.styleMapInterpolate6 = { name: 'ɵɵstyleMapInterpolate6', moduleName: CORE };
  2769. Identifiers.styleMapInterpolate7 = { name: 'ɵɵstyleMapInterpolate7', moduleName: CORE };
  2770. Identifiers.styleMapInterpolate8 = { name: 'ɵɵstyleMapInterpolate8', moduleName: CORE };
  2771. Identifiers.styleMapInterpolateV = { name: 'ɵɵstyleMapInterpolateV', moduleName: CORE };
  2772. Identifiers.classMap = { name: 'ɵɵclassMap', moduleName: CORE };
  2773. Identifiers.classMapInterpolate1 = { name: 'ɵɵclassMapInterpolate1', moduleName: CORE };
  2774. Identifiers.classMapInterpolate2 = { name: 'ɵɵclassMapInterpolate2', moduleName: CORE };
  2775. Identifiers.classMapInterpolate3 = { name: 'ɵɵclassMapInterpolate3', moduleName: CORE };
  2776. Identifiers.classMapInterpolate4 = { name: 'ɵɵclassMapInterpolate4', moduleName: CORE };
  2777. Identifiers.classMapInterpolate5 = { name: 'ɵɵclassMapInterpolate5', moduleName: CORE };
  2778. Identifiers.classMapInterpolate6 = { name: 'ɵɵclassMapInterpolate6', moduleName: CORE };
  2779. Identifiers.classMapInterpolate7 = { name: 'ɵɵclassMapInterpolate7', moduleName: CORE };
  2780. Identifiers.classMapInterpolate8 = { name: 'ɵɵclassMapInterpolate8', moduleName: CORE };
  2781. Identifiers.classMapInterpolateV = { name: 'ɵɵclassMapInterpolateV', moduleName: CORE };
  2782. Identifiers.styleProp = { name: 'ɵɵstyleProp', moduleName: CORE };
  2783. Identifiers.stylePropInterpolate1 = { name: 'ɵɵstylePropInterpolate1', moduleName: CORE };
  2784. Identifiers.stylePropInterpolate2 = { name: 'ɵɵstylePropInterpolate2', moduleName: CORE };
  2785. Identifiers.stylePropInterpolate3 = { name: 'ɵɵstylePropInterpolate3', moduleName: CORE };
  2786. Identifiers.stylePropInterpolate4 = { name: 'ɵɵstylePropInterpolate4', moduleName: CORE };
  2787. Identifiers.stylePropInterpolate5 = { name: 'ɵɵstylePropInterpolate5', moduleName: CORE };
  2788. Identifiers.stylePropInterpolate6 = { name: 'ɵɵstylePropInterpolate6', moduleName: CORE };
  2789. Identifiers.stylePropInterpolate7 = { name: 'ɵɵstylePropInterpolate7', moduleName: CORE };
  2790. Identifiers.stylePropInterpolate8 = { name: 'ɵɵstylePropInterpolate8', moduleName: CORE };
  2791. Identifiers.stylePropInterpolateV = { name: 'ɵɵstylePropInterpolateV', moduleName: CORE };
  2792. Identifiers.nextContext = { name: 'ɵɵnextContext', moduleName: CORE };
  2793. Identifiers.templateCreate = { name: 'ɵɵtemplate', moduleName: CORE };
  2794. Identifiers.text = { name: 'ɵɵtext', moduleName: CORE };
  2795. Identifiers.enableBindings = { name: 'ɵɵenableBindings', moduleName: CORE };
  2796. Identifiers.disableBindings = { name: 'ɵɵdisableBindings', moduleName: CORE };
  2797. Identifiers.getCurrentView = { name: 'ɵɵgetCurrentView', moduleName: CORE };
  2798. Identifiers.textInterpolate = { name: 'ɵɵtextInterpolate', moduleName: CORE };
  2799. Identifiers.textInterpolate1 = { name: 'ɵɵtextInterpolate1', moduleName: CORE };
  2800. Identifiers.textInterpolate2 = { name: 'ɵɵtextInterpolate2', moduleName: CORE };
  2801. Identifiers.textInterpolate3 = { name: 'ɵɵtextInterpolate3', moduleName: CORE };
  2802. Identifiers.textInterpolate4 = { name: 'ɵɵtextInterpolate4', moduleName: CORE };
  2803. Identifiers.textInterpolate5 = { name: 'ɵɵtextInterpolate5', moduleName: CORE };
  2804. Identifiers.textInterpolate6 = { name: 'ɵɵtextInterpolate6', moduleName: CORE };
  2805. Identifiers.textInterpolate7 = { name: 'ɵɵtextInterpolate7', moduleName: CORE };
  2806. Identifiers.textInterpolate8 = { name: 'ɵɵtextInterpolate8', moduleName: CORE };
  2807. Identifiers.textInterpolateV = { name: 'ɵɵtextInterpolateV', moduleName: CORE };
  2808. Identifiers.restoreView = { name: 'ɵɵrestoreView', moduleName: CORE };
  2809. Identifiers.pureFunction0 = { name: 'ɵɵpureFunction0', moduleName: CORE };
  2810. Identifiers.pureFunction1 = { name: 'ɵɵpureFunction1', moduleName: CORE };
  2811. Identifiers.pureFunction2 = { name: 'ɵɵpureFunction2', moduleName: CORE };
  2812. Identifiers.pureFunction3 = { name: 'ɵɵpureFunction3', moduleName: CORE };
  2813. Identifiers.pureFunction4 = { name: 'ɵɵpureFunction4', moduleName: CORE };
  2814. Identifiers.pureFunction5 = { name: 'ɵɵpureFunction5', moduleName: CORE };
  2815. Identifiers.pureFunction6 = { name: 'ɵɵpureFunction6', moduleName: CORE };
  2816. Identifiers.pureFunction7 = { name: 'ɵɵpureFunction7', moduleName: CORE };
  2817. Identifiers.pureFunction8 = { name: 'ɵɵpureFunction8', moduleName: CORE };
  2818. Identifiers.pureFunctionV = { name: 'ɵɵpureFunctionV', moduleName: CORE };
  2819. Identifiers.pipeBind1 = { name: 'ɵɵpipeBind1', moduleName: CORE };
  2820. Identifiers.pipeBind2 = { name: 'ɵɵpipeBind2', moduleName: CORE };
  2821. Identifiers.pipeBind3 = { name: 'ɵɵpipeBind3', moduleName: CORE };
  2822. Identifiers.pipeBind4 = { name: 'ɵɵpipeBind4', moduleName: CORE };
  2823. Identifiers.pipeBindV = { name: 'ɵɵpipeBindV', moduleName: CORE };
  2824. Identifiers.hostProperty = { name: 'ɵɵhostProperty', moduleName: CORE };
  2825. Identifiers.property = { name: 'ɵɵproperty', moduleName: CORE };
  2826. Identifiers.propertyInterpolate = { name: 'ɵɵpropertyInterpolate', moduleName: CORE };
  2827. Identifiers.propertyInterpolate1 = { name: 'ɵɵpropertyInterpolate1', moduleName: CORE };
  2828. Identifiers.propertyInterpolate2 = { name: 'ɵɵpropertyInterpolate2', moduleName: CORE };
  2829. Identifiers.propertyInterpolate3 = { name: 'ɵɵpropertyInterpolate3', moduleName: CORE };
  2830. Identifiers.propertyInterpolate4 = { name: 'ɵɵpropertyInterpolate4', moduleName: CORE };
  2831. Identifiers.propertyInterpolate5 = { name: 'ɵɵpropertyInterpolate5', moduleName: CORE };
  2832. Identifiers.propertyInterpolate6 = { name: 'ɵɵpropertyInterpolate6', moduleName: CORE };
  2833. Identifiers.propertyInterpolate7 = { name: 'ɵɵpropertyInterpolate7', moduleName: CORE };
  2834. Identifiers.propertyInterpolate8 = { name: 'ɵɵpropertyInterpolate8', moduleName: CORE };
  2835. Identifiers.propertyInterpolateV = { name: 'ɵɵpropertyInterpolateV', moduleName: CORE };
  2836. Identifiers.i18n = { name: 'ɵɵi18n', moduleName: CORE };
  2837. Identifiers.i18nAttributes = { name: 'ɵɵi18nAttributes', moduleName: CORE };
  2838. Identifiers.i18nExp = { name: 'ɵɵi18nExp', moduleName: CORE };
  2839. Identifiers.i18nStart = { name: 'ɵɵi18nStart', moduleName: CORE };
  2840. Identifiers.i18nEnd = { name: 'ɵɵi18nEnd', moduleName: CORE };
  2841. Identifiers.i18nApply = { name: 'ɵɵi18nApply', moduleName: CORE };
  2842. Identifiers.i18nPostprocess = { name: 'ɵɵi18nPostprocess', moduleName: CORE };
  2843. Identifiers.pipe = { name: 'ɵɵpipe', moduleName: CORE };
  2844. Identifiers.projection = { name: 'ɵɵprojection', moduleName: CORE };
  2845. Identifiers.projectionDef = { name: 'ɵɵprojectionDef', moduleName: CORE };
  2846. Identifiers.reference = { name: 'ɵɵreference', moduleName: CORE };
  2847. Identifiers.inject = { name: 'ɵɵinject', moduleName: CORE };
  2848. Identifiers.injectAttribute = { name: 'ɵɵinjectAttribute', moduleName: CORE };
  2849. Identifiers.directiveInject = { name: 'ɵɵdirectiveInject', moduleName: CORE };
  2850. Identifiers.invalidFactory = { name: 'ɵɵinvalidFactory', moduleName: CORE };
  2851. Identifiers.invalidFactoryDep = { name: 'ɵɵinvalidFactoryDep', moduleName: CORE };
  2852. Identifiers.templateRefExtractor = { name: 'ɵɵtemplateRefExtractor', moduleName: CORE };
  2853. Identifiers.forwardRef = { name: 'forwardRef', moduleName: CORE };
  2854. Identifiers.resolveForwardRef = { name: 'resolveForwardRef', moduleName: CORE };
  2855. Identifiers.ɵɵdefineInjectable = { name: 'ɵɵdefineInjectable', moduleName: CORE };
  2856. Identifiers.declareInjectable = { name: 'ɵɵngDeclareInjectable', moduleName: CORE };
  2857. Identifiers.InjectableDeclaration = { name: 'ɵɵInjectableDeclaration', moduleName: CORE };
  2858. Identifiers.resolveWindow = { name: 'ɵɵresolveWindow', moduleName: CORE };
  2859. Identifiers.resolveDocument = { name: 'ɵɵresolveDocument', moduleName: CORE };
  2860. Identifiers.resolveBody = { name: 'ɵɵresolveBody', moduleName: CORE };
  2861. Identifiers.defineComponent = { name: 'ɵɵdefineComponent', moduleName: CORE };
  2862. Identifiers.declareComponent = { name: 'ɵɵngDeclareComponent', moduleName: CORE };
  2863. Identifiers.setComponentScope = { name: 'ɵɵsetComponentScope', moduleName: CORE };
  2864. Identifiers.ChangeDetectionStrategy = {
  2865. name: 'ChangeDetectionStrategy',
  2866. moduleName: CORE,
  2867. };
  2868. Identifiers.ViewEncapsulation = {
  2869. name: 'ViewEncapsulation',
  2870. moduleName: CORE,
  2871. };
  2872. Identifiers.ComponentDeclaration = {
  2873. name: 'ɵɵComponentDeclaration',
  2874. moduleName: CORE,
  2875. };
  2876. Identifiers.FactoryDeclaration = {
  2877. name: 'ɵɵFactoryDeclaration',
  2878. moduleName: CORE,
  2879. };
  2880. Identifiers.declareFactory = { name: 'ɵɵngDeclareFactory', moduleName: CORE };
  2881. Identifiers.FactoryTarget = { name: 'ɵɵFactoryTarget', moduleName: CORE };
  2882. Identifiers.defineDirective = { name: 'ɵɵdefineDirective', moduleName: CORE };
  2883. Identifiers.declareDirective = { name: 'ɵɵngDeclareDirective', moduleName: CORE };
  2884. Identifiers.DirectiveDeclaration = {
  2885. name: 'ɵɵDirectiveDeclaration',
  2886. moduleName: CORE,
  2887. };
  2888. Identifiers.InjectorDef = { name: 'ɵɵInjectorDef', moduleName: CORE };
  2889. Identifiers.InjectorDeclaration = { name: 'ɵɵInjectorDeclaration', moduleName: CORE };
  2890. Identifiers.defineInjector = { name: 'ɵɵdefineInjector', moduleName: CORE };
  2891. Identifiers.declareInjector = { name: 'ɵɵngDeclareInjector', moduleName: CORE };
  2892. Identifiers.NgModuleDeclaration = {
  2893. name: 'ɵɵNgModuleDeclaration',
  2894. moduleName: CORE,
  2895. };
  2896. Identifiers.ModuleWithProviders = {
  2897. name: 'ModuleWithProviders',
  2898. moduleName: CORE,
  2899. };
  2900. Identifiers.defineNgModule = { name: 'ɵɵdefineNgModule', moduleName: CORE };
  2901. Identifiers.declareNgModule = { name: 'ɵɵngDeclareNgModule', moduleName: CORE };
  2902. Identifiers.setNgModuleScope = { name: 'ɵɵsetNgModuleScope', moduleName: CORE };
  2903. Identifiers.PipeDeclaration = { name: 'ɵɵPipeDeclaration', moduleName: CORE };
  2904. Identifiers.definePipe = { name: 'ɵɵdefinePipe', moduleName: CORE };
  2905. Identifiers.declarePipe = { name: 'ɵɵngDeclarePipe', moduleName: CORE };
  2906. Identifiers.declareClassMetadata = { name: 'ɵɵngDeclareClassMetadata', moduleName: CORE };
  2907. Identifiers.setClassMetadata = { name: 'ɵsetClassMetadata', moduleName: CORE };
  2908. Identifiers.queryRefresh = { name: 'ɵɵqueryRefresh', moduleName: CORE };
  2909. Identifiers.viewQuery = { name: 'ɵɵviewQuery', moduleName: CORE };
  2910. Identifiers.loadQuery = { name: 'ɵɵloadQuery', moduleName: CORE };
  2911. Identifiers.contentQuery = { name: 'ɵɵcontentQuery', moduleName: CORE };
  2912. Identifiers.NgOnChangesFeature = { name: 'ɵɵNgOnChangesFeature', moduleName: CORE };
  2913. Identifiers.InheritDefinitionFeature = { name: 'ɵɵInheritDefinitionFeature', moduleName: CORE };
  2914. Identifiers.CopyDefinitionFeature = { name: 'ɵɵCopyDefinitionFeature', moduleName: CORE };
  2915. Identifiers.ProvidersFeature = { name: 'ɵɵProvidersFeature', moduleName: CORE };
  2916. Identifiers.listener = { name: 'ɵɵlistener', moduleName: CORE };
  2917. Identifiers.getInheritedFactory = {
  2918. name: 'ɵɵgetInheritedFactory',
  2919. moduleName: CORE,
  2920. };
  2921. // sanitization-related functions
  2922. Identifiers.sanitizeHtml = { name: 'ɵɵsanitizeHtml', moduleName: CORE };
  2923. Identifiers.sanitizeStyle = { name: 'ɵɵsanitizeStyle', moduleName: CORE };
  2924. Identifiers.sanitizeResourceUrl = { name: 'ɵɵsanitizeResourceUrl', moduleName: CORE };
  2925. Identifiers.sanitizeScript = { name: 'ɵɵsanitizeScript', moduleName: CORE };
  2926. Identifiers.sanitizeUrl = { name: 'ɵɵsanitizeUrl', moduleName: CORE };
  2927. Identifiers.sanitizeUrlOrResourceUrl = { name: 'ɵɵsanitizeUrlOrResourceUrl', moduleName: CORE };
  2928. Identifiers.trustConstantHtml = { name: 'ɵɵtrustConstantHtml', moduleName: CORE };
  2929. Identifiers.trustConstantResourceUrl = { name: 'ɵɵtrustConstantResourceUrl', moduleName: CORE };
  2930. /**
  2931. * @license
  2932. * Copyright Google LLC All Rights Reserved.
  2933. *
  2934. * Use of this source code is governed by an MIT-style license that can be
  2935. * found in the LICENSE file at https://angular.io/license
  2936. */
  2937. const DASH_CASE_REGEXP = /-+([a-z0-9])/g;
  2938. function dashCaseToCamelCase(input) {
  2939. return input.replace(DASH_CASE_REGEXP, (...m) => m[1].toUpperCase());
  2940. }
  2941. function splitAtColon(input, defaultValues) {
  2942. return _splitAt(input, ':', defaultValues);
  2943. }
  2944. function splitAtPeriod(input, defaultValues) {
  2945. return _splitAt(input, '.', defaultValues);
  2946. }
  2947. function _splitAt(input, character, defaultValues) {
  2948. const characterIndex = input.indexOf(character);
  2949. if (characterIndex == -1)
  2950. return defaultValues;
  2951. return [input.slice(0, characterIndex).trim(), input.slice(characterIndex + 1).trim()];
  2952. }
  2953. function visitValue(value, visitor, context) {
  2954. if (Array.isArray(value)) {
  2955. return visitor.visitArray(value, context);
  2956. }
  2957. if (isStrictStringMap(value)) {
  2958. return visitor.visitStringMap(value, context);
  2959. }
  2960. if (value == null || typeof value == 'string' || typeof value == 'number' ||
  2961. typeof value == 'boolean') {
  2962. return visitor.visitPrimitive(value, context);
  2963. }
  2964. return visitor.visitOther(value, context);
  2965. }
  2966. function isDefined(val) {
  2967. return val !== null && val !== undefined;
  2968. }
  2969. function noUndefined(val) {
  2970. return val === undefined ? null : val;
  2971. }
  2972. class ValueTransformer {
  2973. visitArray(arr, context) {
  2974. return arr.map(value => visitValue(value, this, context));
  2975. }
  2976. visitStringMap(map, context) {
  2977. const result = {};
  2978. Object.keys(map).forEach(key => {
  2979. result[key] = visitValue(map[key], this, context);
  2980. });
  2981. return result;
  2982. }
  2983. visitPrimitive(value, context) {
  2984. return value;
  2985. }
  2986. visitOther(value, context) {
  2987. return value;
  2988. }
  2989. }
  2990. const SyncAsync = {
  2991. assertSync: (value) => {
  2992. if (isPromise(value)) {
  2993. throw new Error(`Illegal state: value cannot be a promise`);
  2994. }
  2995. return value;
  2996. },
  2997. then: (value, cb) => {
  2998. return isPromise(value) ? value.then(cb) : cb(value);
  2999. },
  3000. all: (syncAsyncValues) => {
  3001. return syncAsyncValues.some(isPromise) ? Promise.all(syncAsyncValues) : syncAsyncValues;
  3002. }
  3003. };
  3004. function error(msg) {
  3005. throw new Error(`Internal Error: ${msg}`);
  3006. }
  3007. function syntaxError(msg, parseErrors) {
  3008. const error = Error(msg);
  3009. error[ERROR_SYNTAX_ERROR] = true;
  3010. if (parseErrors)
  3011. error[ERROR_PARSE_ERRORS] = parseErrors;
  3012. return error;
  3013. }
  3014. const ERROR_SYNTAX_ERROR = 'ngSyntaxError';
  3015. const ERROR_PARSE_ERRORS = 'ngParseErrors';
  3016. function isSyntaxError(error) {
  3017. return error[ERROR_SYNTAX_ERROR];
  3018. }
  3019. function getParseErrors(error) {
  3020. return error[ERROR_PARSE_ERRORS] || [];
  3021. }
  3022. // Escape characters that have a special meaning in Regular Expressions
  3023. function escapeRegExp(s) {
  3024. return s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
  3025. }
  3026. const STRING_MAP_PROTO = Object.getPrototypeOf({});
  3027. function isStrictStringMap(obj) {
  3028. return typeof obj === 'object' && obj !== null && Object.getPrototypeOf(obj) === STRING_MAP_PROTO;
  3029. }
  3030. function utf8Encode(str) {
  3031. let encoded = [];
  3032. for (let index = 0; index < str.length; index++) {
  3033. let codePoint = str.charCodeAt(index);
  3034. // decode surrogate
  3035. // see https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  3036. if (codePoint >= 0xd800 && codePoint <= 0xdbff && str.length > (index + 1)) {
  3037. const low = str.charCodeAt(index + 1);
  3038. if (low >= 0xdc00 && low <= 0xdfff) {
  3039. index++;
  3040. codePoint = ((codePoint - 0xd800) << 10) + low - 0xdc00 + 0x10000;
  3041. }
  3042. }
  3043. if (codePoint <= 0x7f) {
  3044. encoded.push(codePoint);
  3045. }
  3046. else if (codePoint <= 0x7ff) {
  3047. encoded.push(((codePoint >> 6) & 0x1F) | 0xc0, (codePoint & 0x3f) | 0x80);
  3048. }
  3049. else if (codePoint <= 0xffff) {
  3050. encoded.push((codePoint >> 12) | 0xe0, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  3051. }
  3052. else if (codePoint <= 0x1fffff) {
  3053. encoded.push(((codePoint >> 18) & 0x07) | 0xf0, ((codePoint >> 12) & 0x3f) | 0x80, ((codePoint >> 6) & 0x3f) | 0x80, (codePoint & 0x3f) | 0x80);
  3054. }
  3055. }
  3056. return encoded;
  3057. }
  3058. function stringify(token) {
  3059. if (typeof token === 'string') {
  3060. return token;
  3061. }
  3062. if (Array.isArray(token)) {
  3063. return '[' + token.map(stringify).join(', ') + ']';
  3064. }
  3065. if (token == null) {
  3066. return '' + token;
  3067. }
  3068. if (token.overriddenName) {
  3069. return `${token.overriddenName}`;
  3070. }
  3071. if (token.name) {
  3072. return `${token.name}`;
  3073. }
  3074. if (!token.toString) {
  3075. return 'object';
  3076. }
  3077. // WARNING: do not try to `JSON.stringify(token)` here
  3078. // see https://github.com/angular/angular/issues/23440
  3079. const res = token.toString();
  3080. if (res == null) {
  3081. return '' + res;
  3082. }
  3083. const newLineIndex = res.indexOf('\n');
  3084. return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
  3085. }
  3086. /**
  3087. * Lazily retrieves the reference value from a forwardRef.
  3088. */
  3089. function resolveForwardRef(type) {
  3090. if (typeof type === 'function' && type.hasOwnProperty('__forward_ref__')) {
  3091. return type();
  3092. }
  3093. else {
  3094. return type;
  3095. }
  3096. }
  3097. /**
  3098. * Determine if the argument is shaped like a Promise
  3099. */
  3100. function isPromise(obj) {
  3101. // allow any Promise/A+ compliant thenable.
  3102. // It's up to the caller to ensure that obj.then conforms to the spec
  3103. return !!obj && typeof obj.then === 'function';
  3104. }
  3105. class Version {
  3106. constructor(full) {
  3107. this.full = full;
  3108. const splits = full.split('.');
  3109. this.major = splits[0];
  3110. this.minor = splits[1];
  3111. this.patch = splits.slice(2).join('.');
  3112. }
  3113. }
  3114. const __window = typeof window !== 'undefined' && window;
  3115. const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' &&
  3116. self instanceof WorkerGlobalScope && self;
  3117. const __global = typeof global !== 'undefined' && global;
  3118. // Check __global first, because in Node tests both __global and __window may be defined and _global
  3119. // should be __global in that case.
  3120. const _global = __global || __window || __self;
  3121. function newArray(size, value) {
  3122. const list = [];
  3123. for (let i = 0; i < size; i++) {
  3124. list.push(value);
  3125. }
  3126. return list;
  3127. }
  3128. /**
  3129. * Partitions a given array into 2 arrays, based on a boolean value returned by the condition
  3130. * function.
  3131. *
  3132. * @param arr Input array that should be partitioned
  3133. * @param conditionFn Condition function that is called for each item in a given array and returns a
  3134. * boolean value.
  3135. */
  3136. function partitionArray(arr, conditionFn) {
  3137. const truthy = [];
  3138. const falsy = [];
  3139. for (const item of arr) {
  3140. (conditionFn(item) ? truthy : falsy).push(item);
  3141. }
  3142. return [truthy, falsy];
  3143. }
  3144. /**
  3145. * @license
  3146. * Copyright Google LLC All Rights Reserved.
  3147. *
  3148. * Use of this source code is governed by an MIT-style license that can be
  3149. * found in the LICENSE file at https://angular.io/license
  3150. */
  3151. /**
  3152. * This is an R3 `Node`-like wrapper for a raw `html.Comment` node. We do not currently
  3153. * require the implementation of a visitor for Comments as they are only collected at
  3154. * the top-level of the R3 AST, and only if `Render3ParseOptions['collectCommentNodes']`
  3155. * is true.
  3156. */
  3157. class Comment {
  3158. constructor(value, sourceSpan) {
  3159. this.value = value;
  3160. this.sourceSpan = sourceSpan;
  3161. }
  3162. visit(_visitor) {
  3163. throw new Error('visit() not implemented for Comment');
  3164. }
  3165. }
  3166. class Text {
  3167. constructor(value, sourceSpan) {
  3168. this.value = value;
  3169. this.sourceSpan = sourceSpan;
  3170. }
  3171. visit(visitor) {
  3172. return visitor.visitText(this);
  3173. }
  3174. }
  3175. class BoundText {
  3176. constructor(value, sourceSpan, i18n) {
  3177. this.value = value;
  3178. this.sourceSpan = sourceSpan;
  3179. this.i18n = i18n;
  3180. }
  3181. visit(visitor) {
  3182. return visitor.visitBoundText(this);
  3183. }
  3184. }
  3185. /**
  3186. * Represents a text attribute in the template.
  3187. *
  3188. * `valueSpan` may not be present in cases where there is no value `<div a></div>`.
  3189. * `keySpan` may also not be present for synthetic attributes from ICU expansions.
  3190. */
  3191. class TextAttribute {
  3192. constructor(name, value, sourceSpan, keySpan, valueSpan, i18n) {
  3193. this.name = name;
  3194. this.value = value;
  3195. this.sourceSpan = sourceSpan;
  3196. this.keySpan = keySpan;
  3197. this.valueSpan = valueSpan;
  3198. this.i18n = i18n;
  3199. }
  3200. visit(visitor) {
  3201. return visitor.visitTextAttribute(this);
  3202. }
  3203. }
  3204. class BoundAttribute {
  3205. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan, i18n) {
  3206. this.name = name;
  3207. this.type = type;
  3208. this.securityContext = securityContext;
  3209. this.value = value;
  3210. this.unit = unit;
  3211. this.sourceSpan = sourceSpan;
  3212. this.keySpan = keySpan;
  3213. this.valueSpan = valueSpan;
  3214. this.i18n = i18n;
  3215. }
  3216. static fromBoundElementProperty(prop, i18n) {
  3217. if (prop.keySpan === undefined) {
  3218. throw new Error(`Unexpected state: keySpan must be defined for bound attributes but was not for ${prop.name}: ${prop.sourceSpan}`);
  3219. }
  3220. return new BoundAttribute(prop.name, prop.type, prop.securityContext, prop.value, prop.unit, prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n);
  3221. }
  3222. visit(visitor) {
  3223. return visitor.visitBoundAttribute(this);
  3224. }
  3225. }
  3226. class BoundEvent {
  3227. constructor(name, type, handler, target, phase, sourceSpan, handlerSpan, keySpan) {
  3228. this.name = name;
  3229. this.type = type;
  3230. this.handler = handler;
  3231. this.target = target;
  3232. this.phase = phase;
  3233. this.sourceSpan = sourceSpan;
  3234. this.handlerSpan = handlerSpan;
  3235. this.keySpan = keySpan;
  3236. }
  3237. static fromParsedEvent(event) {
  3238. const target = event.type === 0 /* Regular */ ? event.targetOrPhase : null;
  3239. const phase = event.type === 1 /* Animation */ ? event.targetOrPhase : null;
  3240. if (event.keySpan === undefined) {
  3241. throw new Error(`Unexpected state: keySpan must be defined for bound event but was not for ${event.name}: ${event.sourceSpan}`);
  3242. }
  3243. return new BoundEvent(event.name, event.type, event.handler, target, phase, event.sourceSpan, event.handlerSpan, event.keySpan);
  3244. }
  3245. visit(visitor) {
  3246. return visitor.visitBoundEvent(this);
  3247. }
  3248. }
  3249. class Element {
  3250. constructor(name, attributes, inputs, outputs, children, references, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  3251. this.name = name;
  3252. this.attributes = attributes;
  3253. this.inputs = inputs;
  3254. this.outputs = outputs;
  3255. this.children = children;
  3256. this.references = references;
  3257. this.sourceSpan = sourceSpan;
  3258. this.startSourceSpan = startSourceSpan;
  3259. this.endSourceSpan = endSourceSpan;
  3260. this.i18n = i18n;
  3261. }
  3262. visit(visitor) {
  3263. return visitor.visitElement(this);
  3264. }
  3265. }
  3266. class Template {
  3267. constructor(tagName, attributes, inputs, outputs, templateAttrs, children, references, variables, sourceSpan, startSourceSpan, endSourceSpan, i18n) {
  3268. this.tagName = tagName;
  3269. this.attributes = attributes;
  3270. this.inputs = inputs;
  3271. this.outputs = outputs;
  3272. this.templateAttrs = templateAttrs;
  3273. this.children = children;
  3274. this.references = references;
  3275. this.variables = variables;
  3276. this.sourceSpan = sourceSpan;
  3277. this.startSourceSpan = startSourceSpan;
  3278. this.endSourceSpan = endSourceSpan;
  3279. this.i18n = i18n;
  3280. }
  3281. visit(visitor) {
  3282. return visitor.visitTemplate(this);
  3283. }
  3284. }
  3285. class Content {
  3286. constructor(selector, attributes, sourceSpan, i18n) {
  3287. this.selector = selector;
  3288. this.attributes = attributes;
  3289. this.sourceSpan = sourceSpan;
  3290. this.i18n = i18n;
  3291. this.name = 'ng-content';
  3292. }
  3293. visit(visitor) {
  3294. return visitor.visitContent(this);
  3295. }
  3296. }
  3297. class Variable {
  3298. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  3299. this.name = name;
  3300. this.value = value;
  3301. this.sourceSpan = sourceSpan;
  3302. this.keySpan = keySpan;
  3303. this.valueSpan = valueSpan;
  3304. }
  3305. visit(visitor) {
  3306. return visitor.visitVariable(this);
  3307. }
  3308. }
  3309. class Reference {
  3310. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  3311. this.name = name;
  3312. this.value = value;
  3313. this.sourceSpan = sourceSpan;
  3314. this.keySpan = keySpan;
  3315. this.valueSpan = valueSpan;
  3316. }
  3317. visit(visitor) {
  3318. return visitor.visitReference(this);
  3319. }
  3320. }
  3321. class Icu {
  3322. constructor(vars, placeholders, sourceSpan, i18n) {
  3323. this.vars = vars;
  3324. this.placeholders = placeholders;
  3325. this.sourceSpan = sourceSpan;
  3326. this.i18n = i18n;
  3327. }
  3328. visit(visitor) {
  3329. return visitor.visitIcu(this);
  3330. }
  3331. }
  3332. class NullVisitor {
  3333. visitElement(element) { }
  3334. visitTemplate(template) { }
  3335. visitContent(content) { }
  3336. visitVariable(variable) { }
  3337. visitReference(reference) { }
  3338. visitTextAttribute(attribute) { }
  3339. visitBoundAttribute(attribute) { }
  3340. visitBoundEvent(attribute) { }
  3341. visitText(text) { }
  3342. visitBoundText(text) { }
  3343. visitIcu(icu) { }
  3344. }
  3345. class RecursiveVisitor {
  3346. visitElement(element) {
  3347. visitAll(this, element.attributes);
  3348. visitAll(this, element.children);
  3349. visitAll(this, element.references);
  3350. }
  3351. visitTemplate(template) {
  3352. visitAll(this, template.attributes);
  3353. visitAll(this, template.children);
  3354. visitAll(this, template.references);
  3355. visitAll(this, template.variables);
  3356. }
  3357. visitContent(content) { }
  3358. visitVariable(variable) { }
  3359. visitReference(reference) { }
  3360. visitTextAttribute(attribute) { }
  3361. visitBoundAttribute(attribute) { }
  3362. visitBoundEvent(attribute) { }
  3363. visitText(text) { }
  3364. visitBoundText(text) { }
  3365. visitIcu(icu) { }
  3366. }
  3367. class TransformVisitor {
  3368. visitElement(element) {
  3369. const newAttributes = transformAll(this, element.attributes);
  3370. const newInputs = transformAll(this, element.inputs);
  3371. const newOutputs = transformAll(this, element.outputs);
  3372. const newChildren = transformAll(this, element.children);
  3373. const newReferences = transformAll(this, element.references);
  3374. if (newAttributes != element.attributes || newInputs != element.inputs ||
  3375. newOutputs != element.outputs || newChildren != element.children ||
  3376. newReferences != element.references) {
  3377. return new Element(element.name, newAttributes, newInputs, newOutputs, newChildren, newReferences, element.sourceSpan, element.startSourceSpan, element.endSourceSpan);
  3378. }
  3379. return element;
  3380. }
  3381. visitTemplate(template) {
  3382. const newAttributes = transformAll(this, template.attributes);
  3383. const newInputs = transformAll(this, template.inputs);
  3384. const newOutputs = transformAll(this, template.outputs);
  3385. const newTemplateAttrs = transformAll(this, template.templateAttrs);
  3386. const newChildren = transformAll(this, template.children);
  3387. const newReferences = transformAll(this, template.references);
  3388. const newVariables = transformAll(this, template.variables);
  3389. if (newAttributes != template.attributes || newInputs != template.inputs ||
  3390. newOutputs != template.outputs || newTemplateAttrs != template.templateAttrs ||
  3391. newChildren != template.children || newReferences != template.references ||
  3392. newVariables != template.variables) {
  3393. return new Template(template.tagName, newAttributes, newInputs, newOutputs, newTemplateAttrs, newChildren, newReferences, newVariables, template.sourceSpan, template.startSourceSpan, template.endSourceSpan);
  3394. }
  3395. return template;
  3396. }
  3397. visitContent(content) {
  3398. return content;
  3399. }
  3400. visitVariable(variable) {
  3401. return variable;
  3402. }
  3403. visitReference(reference) {
  3404. return reference;
  3405. }
  3406. visitTextAttribute(attribute) {
  3407. return attribute;
  3408. }
  3409. visitBoundAttribute(attribute) {
  3410. return attribute;
  3411. }
  3412. visitBoundEvent(attribute) {
  3413. return attribute;
  3414. }
  3415. visitText(text) {
  3416. return text;
  3417. }
  3418. visitBoundText(text) {
  3419. return text;
  3420. }
  3421. visitIcu(icu) {
  3422. return icu;
  3423. }
  3424. }
  3425. function visitAll(visitor, nodes) {
  3426. const result = [];
  3427. if (visitor.visit) {
  3428. for (const node of nodes) {
  3429. const newNode = visitor.visit(node) || node.visit(visitor);
  3430. }
  3431. }
  3432. else {
  3433. for (const node of nodes) {
  3434. const newNode = node.visit(visitor);
  3435. if (newNode) {
  3436. result.push(newNode);
  3437. }
  3438. }
  3439. }
  3440. return result;
  3441. }
  3442. function transformAll(visitor, nodes) {
  3443. const result = [];
  3444. let changed = false;
  3445. for (const node of nodes) {
  3446. const newNode = node.visit(visitor);
  3447. if (newNode) {
  3448. result.push(newNode);
  3449. }
  3450. changed = changed || newNode != node;
  3451. }
  3452. return changed ? result : nodes;
  3453. }
  3454. /**
  3455. * @license
  3456. * Copyright Google LLC All Rights Reserved.
  3457. *
  3458. * Use of this source code is governed by an MIT-style license that can be
  3459. * found in the LICENSE file at https://angular.io/license
  3460. */
  3461. class Message {
  3462. /**
  3463. * @param nodes message AST
  3464. * @param placeholders maps placeholder names to static content and their source spans
  3465. * @param placeholderToMessage maps placeholder names to messages (used for nested ICU messages)
  3466. * @param meaning
  3467. * @param description
  3468. * @param customId
  3469. */
  3470. constructor(nodes, placeholders, placeholderToMessage, meaning, description, customId) {
  3471. this.nodes = nodes;
  3472. this.placeholders = placeholders;
  3473. this.placeholderToMessage = placeholderToMessage;
  3474. this.meaning = meaning;
  3475. this.description = description;
  3476. this.customId = customId;
  3477. this.id = this.customId;
  3478. /** The ids to use if there are no custom id and if `i18nLegacyMessageIdFormat` is not empty */
  3479. this.legacyIds = [];
  3480. if (nodes.length) {
  3481. this.sources = [{
  3482. filePath: nodes[0].sourceSpan.start.file.url,
  3483. startLine: nodes[0].sourceSpan.start.line + 1,
  3484. startCol: nodes[0].sourceSpan.start.col + 1,
  3485. endLine: nodes[nodes.length - 1].sourceSpan.end.line + 1,
  3486. endCol: nodes[0].sourceSpan.start.col + 1
  3487. }];
  3488. }
  3489. else {
  3490. this.sources = [];
  3491. }
  3492. }
  3493. }
  3494. class Text$1 {
  3495. constructor(value, sourceSpan) {
  3496. this.value = value;
  3497. this.sourceSpan = sourceSpan;
  3498. }
  3499. visit(visitor, context) {
  3500. return visitor.visitText(this, context);
  3501. }
  3502. }
  3503. // TODO(vicb): do we really need this node (vs an array) ?
  3504. class Container {
  3505. constructor(children, sourceSpan) {
  3506. this.children = children;
  3507. this.sourceSpan = sourceSpan;
  3508. }
  3509. visit(visitor, context) {
  3510. return visitor.visitContainer(this, context);
  3511. }
  3512. }
  3513. class Icu$1 {
  3514. constructor(expression, type, cases, sourceSpan) {
  3515. this.expression = expression;
  3516. this.type = type;
  3517. this.cases = cases;
  3518. this.sourceSpan = sourceSpan;
  3519. }
  3520. visit(visitor, context) {
  3521. return visitor.visitIcu(this, context);
  3522. }
  3523. }
  3524. class TagPlaceholder {
  3525. constructor(tag, attrs, startName, closeName, children, isVoid,
  3526. // TODO sourceSpan should cover all (we need a startSourceSpan and endSourceSpan)
  3527. sourceSpan, startSourceSpan, endSourceSpan) {
  3528. this.tag = tag;
  3529. this.attrs = attrs;
  3530. this.startName = startName;
  3531. this.closeName = closeName;
  3532. this.children = children;
  3533. this.isVoid = isVoid;
  3534. this.sourceSpan = sourceSpan;
  3535. this.startSourceSpan = startSourceSpan;
  3536. this.endSourceSpan = endSourceSpan;
  3537. }
  3538. visit(visitor, context) {
  3539. return visitor.visitTagPlaceholder(this, context);
  3540. }
  3541. }
  3542. class Placeholder {
  3543. constructor(value, name, sourceSpan) {
  3544. this.value = value;
  3545. this.name = name;
  3546. this.sourceSpan = sourceSpan;
  3547. }
  3548. visit(visitor, context) {
  3549. return visitor.visitPlaceholder(this, context);
  3550. }
  3551. }
  3552. class IcuPlaceholder {
  3553. constructor(value, name, sourceSpan) {
  3554. this.value = value;
  3555. this.name = name;
  3556. this.sourceSpan = sourceSpan;
  3557. }
  3558. visit(visitor, context) {
  3559. return visitor.visitIcuPlaceholder(this, context);
  3560. }
  3561. }
  3562. // Clone the AST
  3563. class CloneVisitor {
  3564. visitText(text, context) {
  3565. return new Text$1(text.value, text.sourceSpan);
  3566. }
  3567. visitContainer(container, context) {
  3568. const children = container.children.map(n => n.visit(this, context));
  3569. return new Container(children, container.sourceSpan);
  3570. }
  3571. visitIcu(icu, context) {
  3572. const cases = {};
  3573. Object.keys(icu.cases).forEach(key => cases[key] = icu.cases[key].visit(this, context));
  3574. const msg = new Icu$1(icu.expression, icu.type, cases, icu.sourceSpan);
  3575. msg.expressionPlaceholder = icu.expressionPlaceholder;
  3576. return msg;
  3577. }
  3578. visitTagPlaceholder(ph, context) {
  3579. const children = ph.children.map(n => n.visit(this, context));
  3580. return new TagPlaceholder(ph.tag, ph.attrs, ph.startName, ph.closeName, children, ph.isVoid, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  3581. }
  3582. visitPlaceholder(ph, context) {
  3583. return new Placeholder(ph.value, ph.name, ph.sourceSpan);
  3584. }
  3585. visitIcuPlaceholder(ph, context) {
  3586. return new IcuPlaceholder(ph.value, ph.name, ph.sourceSpan);
  3587. }
  3588. }
  3589. // Visit all the nodes recursively
  3590. class RecurseVisitor {
  3591. visitText(text, context) { }
  3592. visitContainer(container, context) {
  3593. container.children.forEach(child => child.visit(this));
  3594. }
  3595. visitIcu(icu, context) {
  3596. Object.keys(icu.cases).forEach(k => {
  3597. icu.cases[k].visit(this);
  3598. });
  3599. }
  3600. visitTagPlaceholder(ph, context) {
  3601. ph.children.forEach(child => child.visit(this));
  3602. }
  3603. visitPlaceholder(ph, context) { }
  3604. visitIcuPlaceholder(ph, context) { }
  3605. }
  3606. /**
  3607. * @license
  3608. * Copyright Google LLC All Rights Reserved.
  3609. *
  3610. * Use of this source code is governed by an MIT-style license that can be
  3611. * found in the LICENSE file at https://angular.io/license
  3612. */
  3613. /**
  3614. * Represents a big integer using a buffer of its individual digits, with the least significant
  3615. * digit stored at the beginning of the array (little endian).
  3616. *
  3617. * For performance reasons, each instance is mutable. The addition operation can be done in-place
  3618. * to reduce memory pressure of allocation for the digits array.
  3619. */
  3620. class BigInteger {
  3621. /**
  3622. * Creates a big integer using its individual digits in little endian storage.
  3623. */
  3624. constructor(digits) {
  3625. this.digits = digits;
  3626. }
  3627. static zero() {
  3628. return new BigInteger([0]);
  3629. }
  3630. static one() {
  3631. return new BigInteger([1]);
  3632. }
  3633. /**
  3634. * Creates a clone of this instance.
  3635. */
  3636. clone() {
  3637. return new BigInteger(this.digits.slice());
  3638. }
  3639. /**
  3640. * Returns a new big integer with the sum of `this` and `other` as its value. This does not mutate
  3641. * `this` but instead returns a new instance, unlike `addToSelf`.
  3642. */
  3643. add(other) {
  3644. const result = this.clone();
  3645. result.addToSelf(other);
  3646. return result;
  3647. }
  3648. /**
  3649. * Adds `other` to the instance itself, thereby mutating its value.
  3650. */
  3651. addToSelf(other) {
  3652. const maxNrOfDigits = Math.max(this.digits.length, other.digits.length);
  3653. let carry = 0;
  3654. for (let i = 0; i < maxNrOfDigits; i++) {
  3655. let digitSum = carry;
  3656. if (i < this.digits.length) {
  3657. digitSum += this.digits[i];
  3658. }
  3659. if (i < other.digits.length) {
  3660. digitSum += other.digits[i];
  3661. }
  3662. if (digitSum >= 10) {
  3663. this.digits[i] = digitSum - 10;
  3664. carry = 1;
  3665. }
  3666. else {
  3667. this.digits[i] = digitSum;
  3668. carry = 0;
  3669. }
  3670. }
  3671. // Apply a remaining carry if needed.
  3672. if (carry > 0) {
  3673. this.digits[maxNrOfDigits] = 1;
  3674. }
  3675. }
  3676. /**
  3677. * Builds the decimal string representation of the big integer. As this is stored in
  3678. * little endian, the digits are concatenated in reverse order.
  3679. */
  3680. toString() {
  3681. let res = '';
  3682. for (let i = this.digits.length - 1; i >= 0; i--) {
  3683. res += this.digits[i];
  3684. }
  3685. return res;
  3686. }
  3687. }
  3688. /**
  3689. * Represents a big integer which is optimized for multiplication operations, as its power-of-twos
  3690. * are memoized. See `multiplyBy()` for details on the multiplication algorithm.
  3691. */
  3692. class BigIntForMultiplication {
  3693. constructor(value) {
  3694. this.powerOfTwos = [value];
  3695. }
  3696. /**
  3697. * Returns the big integer itself.
  3698. */
  3699. getValue() {
  3700. return this.powerOfTwos[0];
  3701. }
  3702. /**
  3703. * Computes the value for `num * b`, where `num` is a JS number and `b` is a big integer. The
  3704. * value for `b` is represented by a storage model that is optimized for this computation.
  3705. *
  3706. * This operation is implemented in N(log2(num)) by continuous halving of the number, where the
  3707. * least-significant bit (LSB) is tested in each iteration. If the bit is set, the bit's index is
  3708. * used as exponent into the power-of-two multiplication of `b`.
  3709. *
  3710. * As an example, consider the multiplication num=42, b=1337. In binary 42 is 0b00101010 and the
  3711. * algorithm unrolls into the following iterations:
  3712. *
  3713. * Iteration | num | LSB | b * 2^iter | Add? | product
  3714. * -----------|------------|------|------------|------|--------
  3715. * 0 | 0b00101010 | 0 | 1337 | No | 0
  3716. * 1 | 0b00010101 | 1 | 2674 | Yes | 2674
  3717. * 2 | 0b00001010 | 0 | 5348 | No | 2674
  3718. * 3 | 0b00000101 | 1 | 10696 | Yes | 13370
  3719. * 4 | 0b00000010 | 0 | 21392 | No | 13370
  3720. * 5 | 0b00000001 | 1 | 42784 | Yes | 56154
  3721. * 6 | 0b00000000 | 0 | 85568 | No | 56154
  3722. *
  3723. * The computed product of 56154 is indeed the correct result.
  3724. *
  3725. * The `BigIntForMultiplication` representation for a big integer provides memoized access to the
  3726. * power-of-two values to reduce the workload in computing those values.
  3727. */
  3728. multiplyBy(num) {
  3729. const product = BigInteger.zero();
  3730. this.multiplyByAndAddTo(num, product);
  3731. return product;
  3732. }
  3733. /**
  3734. * See `multiplyBy()` for details. This function allows for the computed product to be added
  3735. * directly to the provided result big integer.
  3736. */
  3737. multiplyByAndAddTo(num, result) {
  3738. for (let exponent = 0; num !== 0; num = num >>> 1, exponent++) {
  3739. if (num & 1) {
  3740. const value = this.getMultipliedByPowerOfTwo(exponent);
  3741. result.addToSelf(value);
  3742. }
  3743. }
  3744. }
  3745. /**
  3746. * Computes and memoizes the big integer value for `this.number * 2^exponent`.
  3747. */
  3748. getMultipliedByPowerOfTwo(exponent) {
  3749. // Compute the powers up until the requested exponent, where each value is computed from its
  3750. // predecessor. This is simple as `this.number * 2^(exponent - 1)` only has to be doubled (i.e.
  3751. // added to itself) to reach `this.number * 2^exponent`.
  3752. for (let i = this.powerOfTwos.length; i <= exponent; i++) {
  3753. const previousPower = this.powerOfTwos[i - 1];
  3754. this.powerOfTwos[i] = previousPower.add(previousPower);
  3755. }
  3756. return this.powerOfTwos[exponent];
  3757. }
  3758. }
  3759. /**
  3760. * Represents an exponentiation operation for the provided base, of which exponents are computed and
  3761. * memoized. The results are represented by a `BigIntForMultiplication` which is tailored for
  3762. * multiplication operations by memoizing the power-of-twos. This effectively results in a matrix
  3763. * representation that is lazily computed upon request.
  3764. */
  3765. class BigIntExponentiation {
  3766. constructor(base) {
  3767. this.base = base;
  3768. this.exponents = [new BigIntForMultiplication(BigInteger.one())];
  3769. }
  3770. /**
  3771. * Compute the value for `this.base^exponent`, resulting in a big integer that is optimized for
  3772. * further multiplication operations.
  3773. */
  3774. toThePowerOf(exponent) {
  3775. // Compute the results up until the requested exponent, where every value is computed from its
  3776. // predecessor. This is because `this.base^(exponent - 1)` only has to be multiplied by `base`
  3777. // to reach `this.base^exponent`.
  3778. for (let i = this.exponents.length; i <= exponent; i++) {
  3779. const value = this.exponents[i - 1].multiplyBy(this.base);
  3780. this.exponents[i] = new BigIntForMultiplication(value);
  3781. }
  3782. return this.exponents[exponent];
  3783. }
  3784. }
  3785. /**
  3786. * @license
  3787. * Copyright Google LLC All Rights Reserved.
  3788. *
  3789. * Use of this source code is governed by an MIT-style license that can be
  3790. * found in the LICENSE file at https://angular.io/license
  3791. */
  3792. /**
  3793. * Return the message id or compute it using the XLIFF1 digest.
  3794. */
  3795. function digest(message) {
  3796. return message.id || computeDigest(message);
  3797. }
  3798. /**
  3799. * Compute the message id using the XLIFF1 digest.
  3800. */
  3801. function computeDigest(message) {
  3802. return sha1(serializeNodes(message.nodes).join('') + `[${message.meaning}]`);
  3803. }
  3804. /**
  3805. * Return the message id or compute it using the XLIFF2/XMB/$localize digest.
  3806. */
  3807. function decimalDigest(message) {
  3808. return message.id || computeDecimalDigest(message);
  3809. }
  3810. /**
  3811. * Compute the message id using the XLIFF2/XMB/$localize digest.
  3812. */
  3813. function computeDecimalDigest(message) {
  3814. const visitor = new _SerializerIgnoreIcuExpVisitor();
  3815. const parts = message.nodes.map(a => a.visit(visitor, null));
  3816. return computeMsgId(parts.join(''), message.meaning);
  3817. }
  3818. /**
  3819. * Serialize the i18n ast to something xml-like in order to generate an UID.
  3820. *
  3821. * The visitor is also used in the i18n parser tests
  3822. *
  3823. * @internal
  3824. */
  3825. class _SerializerVisitor {
  3826. visitText(text, context) {
  3827. return text.value;
  3828. }
  3829. visitContainer(container, context) {
  3830. return `[${container.children.map(child => child.visit(this)).join(', ')}]`;
  3831. }
  3832. visitIcu(icu, context) {
  3833. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  3834. return `{${icu.expression}, ${icu.type}, ${strCases.join(', ')}}`;
  3835. }
  3836. visitTagPlaceholder(ph, context) {
  3837. return ph.isVoid ?
  3838. `<ph tag name="${ph.startName}"/>` :
  3839. `<ph tag name="${ph.startName}">${ph.children.map(child => child.visit(this)).join(', ')}</ph name="${ph.closeName}">`;
  3840. }
  3841. visitPlaceholder(ph, context) {
  3842. return ph.value ? `<ph name="${ph.name}">${ph.value}</ph>` : `<ph name="${ph.name}"/>`;
  3843. }
  3844. visitIcuPlaceholder(ph, context) {
  3845. return `<ph icu name="${ph.name}">${ph.value.visit(this)}</ph>`;
  3846. }
  3847. }
  3848. const serializerVisitor = new _SerializerVisitor();
  3849. function serializeNodes(nodes) {
  3850. return nodes.map(a => a.visit(serializerVisitor, null));
  3851. }
  3852. /**
  3853. * Serialize the i18n ast to something xml-like in order to generate an UID.
  3854. *
  3855. * Ignore the ICU expressions so that message IDs stays identical if only the expression changes.
  3856. *
  3857. * @internal
  3858. */
  3859. class _SerializerIgnoreIcuExpVisitor extends _SerializerVisitor {
  3860. visitIcu(icu, context) {
  3861. let strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  3862. // Do not take the expression into account
  3863. return `{${icu.type}, ${strCases.join(', ')}}`;
  3864. }
  3865. }
  3866. /**
  3867. * Compute the SHA1 of the given string
  3868. *
  3869. * see https://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  3870. *
  3871. * WARNING: this function has not been designed not tested with security in mind.
  3872. * DO NOT USE IT IN A SECURITY SENSITIVE CONTEXT.
  3873. */
  3874. function sha1(str) {
  3875. const utf8 = utf8Encode(str);
  3876. const words32 = bytesToWords32(utf8, Endian.Big);
  3877. const len = utf8.length * 8;
  3878. const w = newArray(80);
  3879. let a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476, e = 0xc3d2e1f0;
  3880. words32[len >> 5] |= 0x80 << (24 - len % 32);
  3881. words32[((len + 64 >> 9) << 4) + 15] = len;
  3882. for (let i = 0; i < words32.length; i += 16) {
  3883. const h0 = a, h1 = b, h2 = c, h3 = d, h4 = e;
  3884. for (let j = 0; j < 80; j++) {
  3885. if (j < 16) {
  3886. w[j] = words32[i + j];
  3887. }
  3888. else {
  3889. w[j] = rol32(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
  3890. }
  3891. const fkVal = fk(j, b, c, d);
  3892. const f = fkVal[0];
  3893. const k = fkVal[1];
  3894. const temp = [rol32(a, 5), f, e, k, w[j]].reduce(add32);
  3895. e = d;
  3896. d = c;
  3897. c = rol32(b, 30);
  3898. b = a;
  3899. a = temp;
  3900. }
  3901. a = add32(a, h0);
  3902. b = add32(b, h1);
  3903. c = add32(c, h2);
  3904. d = add32(d, h3);
  3905. e = add32(e, h4);
  3906. }
  3907. return bytesToHexString(words32ToByteString([a, b, c, d, e]));
  3908. }
  3909. function fk(index, b, c, d) {
  3910. if (index < 20) {
  3911. return [(b & c) | (~b & d), 0x5a827999];
  3912. }
  3913. if (index < 40) {
  3914. return [b ^ c ^ d, 0x6ed9eba1];
  3915. }
  3916. if (index < 60) {
  3917. return [(b & c) | (b & d) | (c & d), 0x8f1bbcdc];
  3918. }
  3919. return [b ^ c ^ d, 0xca62c1d6];
  3920. }
  3921. /**
  3922. * Compute the fingerprint of the given string
  3923. *
  3924. * The output is 64 bit number encoded as a decimal string
  3925. *
  3926. * based on:
  3927. * https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/GoogleJsMessageIdGenerator.java
  3928. */
  3929. function fingerprint(str) {
  3930. const utf8 = utf8Encode(str);
  3931. let hi = hash32(utf8, 0);
  3932. let lo = hash32(utf8, 102072);
  3933. if (hi == 0 && (lo == 0 || lo == 1)) {
  3934. hi = hi ^ 0x130f9bef;
  3935. lo = lo ^ -0x6b5f56d8;
  3936. }
  3937. return [hi, lo];
  3938. }
  3939. function computeMsgId(msg, meaning = '') {
  3940. let msgFingerprint = fingerprint(msg);
  3941. if (meaning) {
  3942. const meaningFingerprint = fingerprint(meaning);
  3943. msgFingerprint = add64(rol64(msgFingerprint, 1), meaningFingerprint);
  3944. }
  3945. const hi = msgFingerprint[0];
  3946. const lo = msgFingerprint[1];
  3947. return wordsToDecimalString(hi & 0x7fffffff, lo);
  3948. }
  3949. function hash32(bytes, c) {
  3950. let a = 0x9e3779b9, b = 0x9e3779b9;
  3951. let i;
  3952. const len = bytes.length;
  3953. for (i = 0; i + 12 <= len; i += 12) {
  3954. a = add32(a, wordAt(bytes, i, Endian.Little));
  3955. b = add32(b, wordAt(bytes, i + 4, Endian.Little));
  3956. c = add32(c, wordAt(bytes, i + 8, Endian.Little));
  3957. const res = mix(a, b, c);
  3958. a = res[0], b = res[1], c = res[2];
  3959. }
  3960. a = add32(a, wordAt(bytes, i, Endian.Little));
  3961. b = add32(b, wordAt(bytes, i + 4, Endian.Little));
  3962. // the first byte of c is reserved for the length
  3963. c = add32(c, len);
  3964. c = add32(c, wordAt(bytes, i + 8, Endian.Little) << 8);
  3965. return mix(a, b, c)[2];
  3966. }
  3967. // clang-format off
  3968. function mix(a, b, c) {
  3969. a = sub32(a, b);
  3970. a = sub32(a, c);
  3971. a ^= c >>> 13;
  3972. b = sub32(b, c);
  3973. b = sub32(b, a);
  3974. b ^= a << 8;
  3975. c = sub32(c, a);
  3976. c = sub32(c, b);
  3977. c ^= b >>> 13;
  3978. a = sub32(a, b);
  3979. a = sub32(a, c);
  3980. a ^= c >>> 12;
  3981. b = sub32(b, c);
  3982. b = sub32(b, a);
  3983. b ^= a << 16;
  3984. c = sub32(c, a);
  3985. c = sub32(c, b);
  3986. c ^= b >>> 5;
  3987. a = sub32(a, b);
  3988. a = sub32(a, c);
  3989. a ^= c >>> 3;
  3990. b = sub32(b, c);
  3991. b = sub32(b, a);
  3992. b ^= a << 10;
  3993. c = sub32(c, a);
  3994. c = sub32(c, b);
  3995. c ^= b >>> 15;
  3996. return [a, b, c];
  3997. }
  3998. // clang-format on
  3999. // Utils
  4000. var Endian;
  4001. (function (Endian) {
  4002. Endian[Endian["Little"] = 0] = "Little";
  4003. Endian[Endian["Big"] = 1] = "Big";
  4004. })(Endian || (Endian = {}));
  4005. function add32(a, b) {
  4006. return add32to64(a, b)[1];
  4007. }
  4008. function add32to64(a, b) {
  4009. const low = (a & 0xffff) + (b & 0xffff);
  4010. const high = (a >>> 16) + (b >>> 16) + (low >>> 16);
  4011. return [high >>> 16, (high << 16) | (low & 0xffff)];
  4012. }
  4013. function add64(a, b) {
  4014. const ah = a[0], al = a[1];
  4015. const bh = b[0], bl = b[1];
  4016. const result = add32to64(al, bl);
  4017. const carry = result[0];
  4018. const l = result[1];
  4019. const h = add32(add32(ah, bh), carry);
  4020. return [h, l];
  4021. }
  4022. function sub32(a, b) {
  4023. const low = (a & 0xffff) - (b & 0xffff);
  4024. const high = (a >> 16) - (b >> 16) + (low >> 16);
  4025. return (high << 16) | (low & 0xffff);
  4026. }
  4027. // Rotate a 32b number left `count` position
  4028. function rol32(a, count) {
  4029. return (a << count) | (a >>> (32 - count));
  4030. }
  4031. // Rotate a 64b number left `count` position
  4032. function rol64(num, count) {
  4033. const hi = num[0], lo = num[1];
  4034. const h = (hi << count) | (lo >>> (32 - count));
  4035. const l = (lo << count) | (hi >>> (32 - count));
  4036. return [h, l];
  4037. }
  4038. function bytesToWords32(bytes, endian) {
  4039. const size = (bytes.length + 3) >>> 2;
  4040. const words32 = [];
  4041. for (let i = 0; i < size; i++) {
  4042. words32[i] = wordAt(bytes, i * 4, endian);
  4043. }
  4044. return words32;
  4045. }
  4046. function byteAt(bytes, index) {
  4047. return index >= bytes.length ? 0 : bytes[index];
  4048. }
  4049. function wordAt(bytes, index, endian) {
  4050. let word = 0;
  4051. if (endian === Endian.Big) {
  4052. for (let i = 0; i < 4; i++) {
  4053. word += byteAt(bytes, index + i) << (24 - 8 * i);
  4054. }
  4055. }
  4056. else {
  4057. for (let i = 0; i < 4; i++) {
  4058. word += byteAt(bytes, index + i) << 8 * i;
  4059. }
  4060. }
  4061. return word;
  4062. }
  4063. function words32ToByteString(words32) {
  4064. return words32.reduce((bytes, word) => bytes.concat(word32ToByteString(word)), []);
  4065. }
  4066. function word32ToByteString(word) {
  4067. let bytes = [];
  4068. for (let i = 0; i < 4; i++) {
  4069. bytes.push((word >>> 8 * (3 - i)) & 0xff);
  4070. }
  4071. return bytes;
  4072. }
  4073. function bytesToHexString(bytes) {
  4074. let hex = '';
  4075. for (let i = 0; i < bytes.length; i++) {
  4076. const b = byteAt(bytes, i);
  4077. hex += (b >>> 4).toString(16) + (b & 0x0f).toString(16);
  4078. }
  4079. return hex.toLowerCase();
  4080. }
  4081. /**
  4082. * Create a shared exponentiation pool for base-256 computations. This shared pool provides memoized
  4083. * power-of-256 results with memoized power-of-two computations for efficient multiplication.
  4084. *
  4085. * For our purposes, this can be safely stored as a global without memory concerns. The reason is
  4086. * that we encode two words, so only need the 0th (for the low word) and 4th (for the high word)
  4087. * exponent.
  4088. */
  4089. const base256 = new BigIntExponentiation(256);
  4090. /**
  4091. * Represents two 32-bit words as a single decimal number. This requires a big integer storage
  4092. * model as JS numbers are not accurate enough to represent the 64-bit number.
  4093. *
  4094. * Based on https://www.danvk.org/hex2dec.html
  4095. */
  4096. function wordsToDecimalString(hi, lo) {
  4097. // Encode the four bytes in lo in the lower digits of the decimal number.
  4098. // Note: the multiplication results in lo itself but represented by a big integer using its
  4099. // decimal digits.
  4100. const decimal = base256.toThePowerOf(0).multiplyBy(lo);
  4101. // Encode the four bytes in hi above the four lo bytes. lo is a maximum of (2^8)^4, which is why
  4102. // this multiplication factor is applied.
  4103. base256.toThePowerOf(4).multiplyByAndAddTo(hi, decimal);
  4104. return decimal.toString();
  4105. }
  4106. /**
  4107. * @license
  4108. * Copyright Google LLC All Rights Reserved.
  4109. *
  4110. * Use of this source code is governed by an MIT-style license that can be
  4111. * found in the LICENSE file at https://angular.io/license
  4112. */
  4113. class Serializer {
  4114. // Creates a name mapper, see `PlaceholderMapper`
  4115. // Returning `null` means that no name mapping is used.
  4116. createNameMapper(message) {
  4117. return null;
  4118. }
  4119. }
  4120. /**
  4121. * A simple mapper that take a function to transform an internal name to a public name
  4122. */
  4123. class SimplePlaceholderMapper extends RecurseVisitor {
  4124. // create a mapping from the message
  4125. constructor(message, mapName) {
  4126. super();
  4127. this.mapName = mapName;
  4128. this.internalToPublic = {};
  4129. this.publicToNextId = {};
  4130. this.publicToInternal = {};
  4131. message.nodes.forEach(node => node.visit(this));
  4132. }
  4133. toPublicName(internalName) {
  4134. return this.internalToPublic.hasOwnProperty(internalName) ?
  4135. this.internalToPublic[internalName] :
  4136. null;
  4137. }
  4138. toInternalName(publicName) {
  4139. return this.publicToInternal.hasOwnProperty(publicName) ? this.publicToInternal[publicName] :
  4140. null;
  4141. }
  4142. visitText(text, context) {
  4143. return null;
  4144. }
  4145. visitTagPlaceholder(ph, context) {
  4146. this.visitPlaceholderName(ph.startName);
  4147. super.visitTagPlaceholder(ph, context);
  4148. this.visitPlaceholderName(ph.closeName);
  4149. }
  4150. visitPlaceholder(ph, context) {
  4151. this.visitPlaceholderName(ph.name);
  4152. }
  4153. visitIcuPlaceholder(ph, context) {
  4154. this.visitPlaceholderName(ph.name);
  4155. }
  4156. // XMB placeholders could only contains A-Z, 0-9 and _
  4157. visitPlaceholderName(internalName) {
  4158. if (!internalName || this.internalToPublic.hasOwnProperty(internalName)) {
  4159. return;
  4160. }
  4161. let publicName = this.mapName(internalName);
  4162. if (this.publicToInternal.hasOwnProperty(publicName)) {
  4163. // Create a new XMB when it has already been used
  4164. const nextId = this.publicToNextId[publicName];
  4165. this.publicToNextId[publicName] = nextId + 1;
  4166. publicName = `${publicName}_${nextId}`;
  4167. }
  4168. else {
  4169. this.publicToNextId[publicName] = 1;
  4170. }
  4171. this.internalToPublic[internalName] = publicName;
  4172. this.publicToInternal[publicName] = internalName;
  4173. }
  4174. }
  4175. /**
  4176. * @license
  4177. * Copyright Google LLC All Rights Reserved.
  4178. *
  4179. * Use of this source code is governed by an MIT-style license that can be
  4180. * found in the LICENSE file at https://angular.io/license
  4181. */
  4182. class _Visitor {
  4183. visitTag(tag) {
  4184. const strAttrs = this._serializeAttributes(tag.attrs);
  4185. if (tag.children.length == 0) {
  4186. return `<${tag.name}${strAttrs}/>`;
  4187. }
  4188. const strChildren = tag.children.map(node => node.visit(this));
  4189. return `<${tag.name}${strAttrs}>${strChildren.join('')}</${tag.name}>`;
  4190. }
  4191. visitText(text) {
  4192. return text.value;
  4193. }
  4194. visitDeclaration(decl) {
  4195. return `<?xml${this._serializeAttributes(decl.attrs)} ?>`;
  4196. }
  4197. _serializeAttributes(attrs) {
  4198. const strAttrs = Object.keys(attrs).map((name) => `${name}="${attrs[name]}"`).join(' ');
  4199. return strAttrs.length > 0 ? ' ' + strAttrs : '';
  4200. }
  4201. visitDoctype(doctype) {
  4202. return `<!DOCTYPE ${doctype.rootTag} [\n${doctype.dtd}\n]>`;
  4203. }
  4204. }
  4205. const _visitor = new _Visitor();
  4206. function serialize(nodes) {
  4207. return nodes.map((node) => node.visit(_visitor)).join('');
  4208. }
  4209. class Declaration {
  4210. constructor(unescapedAttrs) {
  4211. this.attrs = {};
  4212. Object.keys(unescapedAttrs).forEach((k) => {
  4213. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  4214. });
  4215. }
  4216. visit(visitor) {
  4217. return visitor.visitDeclaration(this);
  4218. }
  4219. }
  4220. class Doctype {
  4221. constructor(rootTag, dtd) {
  4222. this.rootTag = rootTag;
  4223. this.dtd = dtd;
  4224. }
  4225. visit(visitor) {
  4226. return visitor.visitDoctype(this);
  4227. }
  4228. }
  4229. class Tag {
  4230. constructor(name, unescapedAttrs = {}, children = []) {
  4231. this.name = name;
  4232. this.children = children;
  4233. this.attrs = {};
  4234. Object.keys(unescapedAttrs).forEach((k) => {
  4235. this.attrs[k] = escapeXml(unescapedAttrs[k]);
  4236. });
  4237. }
  4238. visit(visitor) {
  4239. return visitor.visitTag(this);
  4240. }
  4241. }
  4242. class Text$2 {
  4243. constructor(unescapedValue) {
  4244. this.value = escapeXml(unescapedValue);
  4245. }
  4246. visit(visitor) {
  4247. return visitor.visitText(this);
  4248. }
  4249. }
  4250. class CR extends Text$2 {
  4251. constructor(ws = 0) {
  4252. super(`\n${new Array(ws + 1).join(' ')}`);
  4253. }
  4254. }
  4255. const _ESCAPED_CHARS = [
  4256. [/&/g, '&amp;'],
  4257. [/"/g, '&quot;'],
  4258. [/'/g, '&apos;'],
  4259. [/</g, '&lt;'],
  4260. [/>/g, '&gt;'],
  4261. ];
  4262. // Escape `_ESCAPED_CHARS` characters in the given text with encoded entities
  4263. function escapeXml(text) {
  4264. return _ESCAPED_CHARS.reduce((text, entry) => text.replace(entry[0], entry[1]), text);
  4265. }
  4266. /**
  4267. * @license
  4268. * Copyright Google LLC All Rights Reserved.
  4269. *
  4270. * Use of this source code is governed by an MIT-style license that can be
  4271. * found in the LICENSE file at https://angular.io/license
  4272. */
  4273. const _MESSAGES_TAG = 'messagebundle';
  4274. const _MESSAGE_TAG = 'msg';
  4275. const _PLACEHOLDER_TAG = 'ph';
  4276. const _EXAMPLE_TAG = 'ex';
  4277. const _SOURCE_TAG = 'source';
  4278. const _DOCTYPE = `<!ELEMENT messagebundle (msg)*>
  4279. <!ATTLIST messagebundle class CDATA #IMPLIED>
  4280. <!ELEMENT msg (#PCDATA|ph|source)*>
  4281. <!ATTLIST msg id CDATA #IMPLIED>
  4282. <!ATTLIST msg seq CDATA #IMPLIED>
  4283. <!ATTLIST msg name CDATA #IMPLIED>
  4284. <!ATTLIST msg desc CDATA #IMPLIED>
  4285. <!ATTLIST msg meaning CDATA #IMPLIED>
  4286. <!ATTLIST msg obsolete (obsolete) #IMPLIED>
  4287. <!ATTLIST msg xml:space (default|preserve) "default">
  4288. <!ATTLIST msg is_hidden CDATA #IMPLIED>
  4289. <!ELEMENT source (#PCDATA)>
  4290. <!ELEMENT ph (#PCDATA|ex)*>
  4291. <!ATTLIST ph name CDATA #REQUIRED>
  4292. <!ELEMENT ex (#PCDATA)>`;
  4293. class Xmb extends Serializer {
  4294. write(messages, locale) {
  4295. const exampleVisitor = new ExampleVisitor();
  4296. const visitor = new _Visitor$1();
  4297. let rootNode = new Tag(_MESSAGES_TAG);
  4298. messages.forEach(message => {
  4299. const attrs = { id: message.id };
  4300. if (message.description) {
  4301. attrs['desc'] = message.description;
  4302. }
  4303. if (message.meaning) {
  4304. attrs['meaning'] = message.meaning;
  4305. }
  4306. let sourceTags = [];
  4307. message.sources.forEach((source) => {
  4308. sourceTags.push(new Tag(_SOURCE_TAG, {}, [new Text$2(`${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`)]));
  4309. });
  4310. rootNode.children.push(new CR(2), new Tag(_MESSAGE_TAG, attrs, [...sourceTags, ...visitor.serialize(message.nodes)]));
  4311. });
  4312. rootNode.children.push(new CR());
  4313. return serialize([
  4314. new Declaration({ version: '1.0', encoding: 'UTF-8' }),
  4315. new CR(),
  4316. new Doctype(_MESSAGES_TAG, _DOCTYPE),
  4317. new CR(),
  4318. exampleVisitor.addDefaultExamples(rootNode),
  4319. new CR(),
  4320. ]);
  4321. }
  4322. load(content, url) {
  4323. throw new Error('Unsupported');
  4324. }
  4325. digest(message) {
  4326. return digest$1(message);
  4327. }
  4328. createNameMapper(message) {
  4329. return new SimplePlaceholderMapper(message, toPublicName);
  4330. }
  4331. }
  4332. class _Visitor$1 {
  4333. visitText(text, context) {
  4334. return [new Text$2(text.value)];
  4335. }
  4336. visitContainer(container, context) {
  4337. const nodes = [];
  4338. container.children.forEach((node) => nodes.push(...node.visit(this)));
  4339. return nodes;
  4340. }
  4341. visitIcu(icu, context) {
  4342. const nodes = [new Text$2(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  4343. Object.keys(icu.cases).forEach((c) => {
  4344. nodes.push(new Text$2(`${c} {`), ...icu.cases[c].visit(this), new Text$2(`} `));
  4345. });
  4346. nodes.push(new Text$2(`}`));
  4347. return nodes;
  4348. }
  4349. visitTagPlaceholder(ph, context) {
  4350. const startTagAsText = new Text$2(`<${ph.tag}>`);
  4351. const startEx = new Tag(_EXAMPLE_TAG, {}, [startTagAsText]);
  4352. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4353. const startTagPh = new Tag(_PLACEHOLDER_TAG, { name: ph.startName }, [startEx, startTagAsText]);
  4354. if (ph.isVoid) {
  4355. // void tags have no children nor closing tags
  4356. return [startTagPh];
  4357. }
  4358. const closeTagAsText = new Text$2(`</${ph.tag}>`);
  4359. const closeEx = new Tag(_EXAMPLE_TAG, {}, [closeTagAsText]);
  4360. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4361. const closeTagPh = new Tag(_PLACEHOLDER_TAG, { name: ph.closeName }, [closeEx, closeTagAsText]);
  4362. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  4363. }
  4364. visitPlaceholder(ph, context) {
  4365. const interpolationAsText = new Text$2(`{{${ph.value}}}`);
  4366. // Example tag needs to be not-empty for TC.
  4367. const exTag = new Tag(_EXAMPLE_TAG, {}, [interpolationAsText]);
  4368. return [
  4369. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4370. new Tag(_PLACEHOLDER_TAG, { name: ph.name }, [exTag, interpolationAsText])
  4371. ];
  4372. }
  4373. visitIcuPlaceholder(ph, context) {
  4374. const icuExpression = ph.value.expression;
  4375. const icuType = ph.value.type;
  4376. const icuCases = Object.keys(ph.value.cases).map((value) => value + ' {...}').join(' ');
  4377. const icuAsText = new Text$2(`{${icuExpression}, ${icuType}, ${icuCases}}`);
  4378. const exTag = new Tag(_EXAMPLE_TAG, {}, [icuAsText]);
  4379. return [
  4380. // TC requires PH to have a non empty EX, and uses the text node to show the "original" value.
  4381. new Tag(_PLACEHOLDER_TAG, { name: ph.name }, [exTag, icuAsText])
  4382. ];
  4383. }
  4384. serialize(nodes) {
  4385. return [].concat(...nodes.map(node => node.visit(this)));
  4386. }
  4387. }
  4388. function digest$1(message) {
  4389. return decimalDigest(message);
  4390. }
  4391. // TC requires at least one non-empty example on placeholders
  4392. class ExampleVisitor {
  4393. addDefaultExamples(node) {
  4394. node.visit(this);
  4395. return node;
  4396. }
  4397. visitTag(tag) {
  4398. if (tag.name === _PLACEHOLDER_TAG) {
  4399. if (!tag.children || tag.children.length == 0) {
  4400. const exText = new Text$2(tag.attrs['name'] || '...');
  4401. tag.children = [new Tag(_EXAMPLE_TAG, {}, [exText])];
  4402. }
  4403. }
  4404. else if (tag.children) {
  4405. tag.children.forEach(node => node.visit(this));
  4406. }
  4407. }
  4408. visitText(text) { }
  4409. visitDeclaration(decl) { }
  4410. visitDoctype(doctype) { }
  4411. }
  4412. // XMB/XTB placeholders can only contain A-Z, 0-9 and _
  4413. function toPublicName(internalName) {
  4414. return internalName.toUpperCase().replace(/[^A-Z0-9_]/g, '_');
  4415. }
  4416. /**
  4417. * @license
  4418. * Copyright Google LLC All Rights Reserved.
  4419. *
  4420. * Use of this source code is governed by an MIT-style license that can be
  4421. * found in the LICENSE file at https://angular.io/license
  4422. */
  4423. /* Closure variables holding messages must be named `MSG_[A-Z0-9]+` */
  4424. const CLOSURE_TRANSLATION_VAR_PREFIX = 'MSG_';
  4425. /**
  4426. * Prefix for non-`goog.getMsg` i18n-related vars.
  4427. * Note: the prefix uses lowercase characters intentionally due to a Closure behavior that
  4428. * considers variables like `I18N_0` as constants and throws an error when their value changes.
  4429. */
  4430. const TRANSLATION_VAR_PREFIX = 'i18n_';
  4431. /** Name of the i18n attributes **/
  4432. const I18N_ATTR = 'i18n';
  4433. const I18N_ATTR_PREFIX = 'i18n-';
  4434. /** Prefix of var expressions used in ICUs */
  4435. const I18N_ICU_VAR_PREFIX = 'VAR_';
  4436. /** Prefix of ICU expressions for post processing */
  4437. const I18N_ICU_MAPPING_PREFIX = 'I18N_EXP_';
  4438. /** Placeholder wrapper for i18n expressions **/
  4439. const I18N_PLACEHOLDER_SYMBOL = '�';
  4440. function isI18nAttribute(name) {
  4441. return name === I18N_ATTR || name.startsWith(I18N_ATTR_PREFIX);
  4442. }
  4443. function isI18nRootNode(meta) {
  4444. return meta instanceof Message;
  4445. }
  4446. function isSingleI18nIcu(meta) {
  4447. return isI18nRootNode(meta) && meta.nodes.length === 1 && meta.nodes[0] instanceof Icu$1;
  4448. }
  4449. function hasI18nMeta(node) {
  4450. return !!node.i18n;
  4451. }
  4452. function hasI18nAttrs(element) {
  4453. return element.attrs.some((attr) => isI18nAttribute(attr.name));
  4454. }
  4455. function icuFromI18nMessage(message) {
  4456. return message.nodes[0];
  4457. }
  4458. function wrapI18nPlaceholder(content, contextId = 0) {
  4459. const blockId = contextId > 0 ? `:${contextId}` : '';
  4460. return `${I18N_PLACEHOLDER_SYMBOL}${content}${blockId}${I18N_PLACEHOLDER_SYMBOL}`;
  4461. }
  4462. function assembleI18nBoundString(strings, bindingStartIndex = 0, contextId = 0) {
  4463. if (!strings.length)
  4464. return '';
  4465. let acc = '';
  4466. const lastIdx = strings.length - 1;
  4467. for (let i = 0; i < lastIdx; i++) {
  4468. acc += `${strings[i]}${wrapI18nPlaceholder(bindingStartIndex + i, contextId)}`;
  4469. }
  4470. acc += strings[lastIdx];
  4471. return acc;
  4472. }
  4473. function getSeqNumberGenerator(startsAt = 0) {
  4474. let current = startsAt;
  4475. return () => current++;
  4476. }
  4477. function placeholdersToParams(placeholders) {
  4478. const params = {};
  4479. placeholders.forEach((values, key) => {
  4480. params[key] = literal(values.length > 1 ? `[${values.join('|')}]` : values[0]);
  4481. });
  4482. return params;
  4483. }
  4484. function updatePlaceholderMap(map, name, ...values) {
  4485. const current = map.get(name) || [];
  4486. current.push(...values);
  4487. map.set(name, current);
  4488. }
  4489. function assembleBoundTextPlaceholders(meta, bindingStartIndex = 0, contextId = 0) {
  4490. const startIdx = bindingStartIndex;
  4491. const placeholders = new Map();
  4492. const node = meta instanceof Message ? meta.nodes.find(node => node instanceof Container) : meta;
  4493. if (node) {
  4494. node
  4495. .children
  4496. .filter((child) => child instanceof Placeholder)
  4497. .forEach((child, idx) => {
  4498. const content = wrapI18nPlaceholder(startIdx + idx, contextId);
  4499. updatePlaceholderMap(placeholders, child.name, content);
  4500. });
  4501. }
  4502. return placeholders;
  4503. }
  4504. /**
  4505. * Format the placeholder names in a map of placeholders to expressions.
  4506. *
  4507. * The placeholder names are converted from "internal" format (e.g. `START_TAG_DIV_1`) to "external"
  4508. * format (e.g. `startTagDiv_1`).
  4509. *
  4510. * @param params A map of placeholder names to expressions.
  4511. * @param useCamelCase whether to camelCase the placeholder name when formatting.
  4512. * @returns A new map of formatted placeholder names to expressions.
  4513. */
  4514. function i18nFormatPlaceholderNames(params = {}, useCamelCase) {
  4515. const _params = {};
  4516. if (params && Object.keys(params).length) {
  4517. Object.keys(params).forEach(key => _params[formatI18nPlaceholderName(key, useCamelCase)] = params[key]);
  4518. }
  4519. return _params;
  4520. }
  4521. /**
  4522. * Converts internal placeholder names to public-facing format
  4523. * (for example to use in goog.getMsg call).
  4524. * Example: `START_TAG_DIV_1` is converted to `startTagDiv_1`.
  4525. *
  4526. * @param name The placeholder name that should be formatted
  4527. * @returns Formatted placeholder name
  4528. */
  4529. function formatI18nPlaceholderName(name, useCamelCase = true) {
  4530. const publicName = toPublicName(name);
  4531. if (!useCamelCase) {
  4532. return publicName;
  4533. }
  4534. const chunks = publicName.split('_');
  4535. if (chunks.length === 1) {
  4536. // if no "_" found - just lowercase the value
  4537. return name.toLowerCase();
  4538. }
  4539. let postfix;
  4540. // eject last element if it's a number
  4541. if (/^\d+$/.test(chunks[chunks.length - 1])) {
  4542. postfix = chunks.pop();
  4543. }
  4544. let raw = chunks.shift().toLowerCase();
  4545. if (chunks.length) {
  4546. raw += chunks.map(c => c.charAt(0).toUpperCase() + c.slice(1).toLowerCase()).join('');
  4547. }
  4548. return postfix ? `${raw}_${postfix}` : raw;
  4549. }
  4550. /**
  4551. * Generates a prefix for translation const name.
  4552. *
  4553. * @param extra Additional local prefix that should be injected into translation var name
  4554. * @returns Complete translation const prefix
  4555. */
  4556. function getTranslationConstPrefix(extra) {
  4557. return `${CLOSURE_TRANSLATION_VAR_PREFIX}${extra}`.toUpperCase();
  4558. }
  4559. /**
  4560. * Generate AST to declare a variable. E.g. `var I18N_1;`.
  4561. * @param variable the name of the variable to declare.
  4562. */
  4563. function declareI18nVariable(variable) {
  4564. return new DeclareVarStmt(variable.name, undefined, INFERRED_TYPE, undefined, variable.sourceSpan);
  4565. }
  4566. /**
  4567. * @license
  4568. * Copyright Google LLC All Rights Reserved.
  4569. *
  4570. * Use of this source code is governed by an MIT-style license that can be
  4571. * found in the LICENSE file at https://angular.io/license
  4572. */
  4573. /**
  4574. * Checks whether an object key contains potentially unsafe chars, thus the key should be wrapped in
  4575. * quotes. Note: we do not wrap all keys into quotes, as it may have impact on minification and may
  4576. * bot work in some cases when object keys are mangled by minifier.
  4577. *
  4578. * TODO(FW-1136): this is a temporary solution, we need to come up with a better way of working with
  4579. * inputs that contain potentially unsafe chars.
  4580. */
  4581. const UNSAFE_OBJECT_KEY_NAME_REGEXP = /[-.]/;
  4582. /** Name of the temporary to use during data binding */
  4583. const TEMPORARY_NAME = '_t';
  4584. /** Name of the context parameter passed into a template function */
  4585. const CONTEXT_NAME = 'ctx';
  4586. /** Name of the RenderFlag passed into a template function */
  4587. const RENDER_FLAGS = 'rf';
  4588. /** The prefix reference variables */
  4589. const REFERENCE_PREFIX = '_r';
  4590. /** The name of the implicit context reference */
  4591. const IMPLICIT_REFERENCE = '$implicit';
  4592. /** Non bindable attribute name **/
  4593. const NON_BINDABLE_ATTR = 'ngNonBindable';
  4594. /** Name for the variable keeping track of the context returned by `ɵɵrestoreView`. */
  4595. const RESTORED_VIEW_CONTEXT_NAME = 'restoredCtx';
  4596. /**
  4597. * Creates an allocator for a temporary variable.
  4598. *
  4599. * A variable declaration is added to the statements the first time the allocator is invoked.
  4600. */
  4601. function temporaryAllocator(statements, name) {
  4602. let temp = null;
  4603. return () => {
  4604. if (!temp) {
  4605. statements.push(new DeclareVarStmt(TEMPORARY_NAME, undefined, DYNAMIC_TYPE));
  4606. temp = variable(name);
  4607. }
  4608. return temp;
  4609. };
  4610. }
  4611. function unsupported(feature) {
  4612. if (this) {
  4613. throw new Error(`Builder ${this.constructor.name} doesn't support ${feature} yet`);
  4614. }
  4615. throw new Error(`Feature ${feature} is not supported yet`);
  4616. }
  4617. function invalid$1(arg) {
  4618. throw new Error(`Invalid state: Visitor ${this.constructor.name} doesn't handle ${arg.constructor.name}`);
  4619. }
  4620. function asLiteral(value) {
  4621. if (Array.isArray(value)) {
  4622. return literalArr(value.map(asLiteral));
  4623. }
  4624. return literal(value, INFERRED_TYPE);
  4625. }
  4626. function conditionallyCreateMapObjectLiteral(keys, keepDeclared) {
  4627. if (Object.getOwnPropertyNames(keys).length > 0) {
  4628. return mapToExpression(keys, keepDeclared);
  4629. }
  4630. return null;
  4631. }
  4632. function mapToExpression(map, keepDeclared) {
  4633. return literalMap(Object.getOwnPropertyNames(map).map(key => {
  4634. // canonical syntax: `dirProp: publicProp`
  4635. // if there is no `:`, use dirProp = elProp
  4636. const value = map[key];
  4637. let declaredName;
  4638. let publicName;
  4639. let minifiedName;
  4640. let needsDeclaredName;
  4641. if (Array.isArray(value)) {
  4642. [publicName, declaredName] = value;
  4643. minifiedName = key;
  4644. needsDeclaredName = publicName !== declaredName;
  4645. }
  4646. else {
  4647. [declaredName, publicName] = splitAtColon(key, [key, value]);
  4648. minifiedName = declaredName;
  4649. // Only include the declared name if extracted from the key, i.e. the key contains a colon.
  4650. // Otherwise the declared name should be omitted even if it is different from the public name,
  4651. // as it may have already been minified.
  4652. needsDeclaredName = publicName !== declaredName && key.includes(':');
  4653. }
  4654. return {
  4655. key: minifiedName,
  4656. // put quotes around keys that contain potentially unsafe characters
  4657. quoted: UNSAFE_OBJECT_KEY_NAME_REGEXP.test(minifiedName),
  4658. value: (keepDeclared && needsDeclaredName) ?
  4659. literalArr([asLiteral(publicName), asLiteral(declaredName)]) :
  4660. asLiteral(publicName)
  4661. };
  4662. }));
  4663. }
  4664. /**
  4665. * Remove trailing null nodes as they are implied.
  4666. */
  4667. function trimTrailingNulls(parameters) {
  4668. while (isNull(parameters[parameters.length - 1])) {
  4669. parameters.pop();
  4670. }
  4671. return parameters;
  4672. }
  4673. function getQueryPredicate(query, constantPool) {
  4674. if (Array.isArray(query.predicate)) {
  4675. let predicate = [];
  4676. query.predicate.forEach((selector) => {
  4677. // Each item in predicates array may contain strings with comma-separated refs
  4678. // (for ex. 'ref, ref1, ..., refN'), thus we extract individual refs and store them
  4679. // as separate array entities
  4680. const selectors = selector.split(',').map(token => literal(token.trim()));
  4681. predicate.push(...selectors);
  4682. });
  4683. return constantPool.getConstLiteral(literalArr(predicate), true);
  4684. }
  4685. else {
  4686. return query.predicate;
  4687. }
  4688. }
  4689. /**
  4690. * A representation for an object literal used during codegen of definition objects. The generic
  4691. * type `T` allows to reference a documented type of the generated structure, such that the
  4692. * property names that are set can be resolved to their documented declaration.
  4693. */
  4694. class DefinitionMap {
  4695. constructor() {
  4696. this.values = [];
  4697. }
  4698. set(key, value) {
  4699. if (value) {
  4700. this.values.push({ key: key, value, quoted: false });
  4701. }
  4702. }
  4703. toLiteralMap() {
  4704. return literalMap(this.values);
  4705. }
  4706. }
  4707. /**
  4708. * Extract a map of properties to values for a given element or template node, which can be used
  4709. * by the directive matching machinery.
  4710. *
  4711. * @param elOrTpl the element or template in question
  4712. * @return an object set up for directive matching. For attributes on the element/template, this
  4713. * object maps a property name to its (static) value. For any bindings, this map simply maps the
  4714. * property name to an empty string.
  4715. */
  4716. function getAttrsForDirectiveMatching(elOrTpl) {
  4717. const attributesMap = {};
  4718. if (elOrTpl instanceof Template && elOrTpl.tagName !== 'ng-template') {
  4719. elOrTpl.templateAttrs.forEach(a => attributesMap[a.name] = '');
  4720. }
  4721. else {
  4722. elOrTpl.attributes.forEach(a => {
  4723. if (!isI18nAttribute(a.name)) {
  4724. attributesMap[a.name] = a.value;
  4725. }
  4726. });
  4727. elOrTpl.inputs.forEach(i => {
  4728. attributesMap[i.name] = '';
  4729. });
  4730. elOrTpl.outputs.forEach(o => {
  4731. attributesMap[o.name] = '';
  4732. });
  4733. }
  4734. return attributesMap;
  4735. }
  4736. /** Returns a call expression to a chained instruction, e.g. `property(params[0])(params[1])`. */
  4737. function chainedInstruction(reference, calls, span) {
  4738. let expression = importExpr(reference, null, span);
  4739. if (calls.length > 0) {
  4740. for (let i = 0; i < calls.length; i++) {
  4741. expression = expression.callFn(calls[i], span);
  4742. }
  4743. }
  4744. else {
  4745. // Add a blank invocation, in case the `calls` array is empty.
  4746. expression = expression.callFn([], span);
  4747. }
  4748. return expression;
  4749. }
  4750. /**
  4751. * Gets the number of arguments expected to be passed to a generated instruction in the case of
  4752. * interpolation instructions.
  4753. * @param interpolation An interpolation ast
  4754. */
  4755. function getInterpolationArgsLength(interpolation) {
  4756. const { expressions, strings } = interpolation;
  4757. if (expressions.length === 1 && strings.length === 2 && strings[0] === '' && strings[1] === '') {
  4758. // If the interpolation has one interpolated value, but the prefix and suffix are both empty
  4759. // strings, we only pass one argument, to a special instruction like `propertyInterpolate` or
  4760. // `textInterpolate`.
  4761. return 1;
  4762. }
  4763. else {
  4764. return expressions.length + strings.length;
  4765. }
  4766. }
  4767. /**
  4768. * @license
  4769. * Copyright Google LLC All Rights Reserved.
  4770. *
  4771. * Use of this source code is governed by an MIT-style license that can be
  4772. * found in the LICENSE file at https://angular.io/license
  4773. */
  4774. /**
  4775. * Creates an array literal expression from the given array, mapping all values to an expression
  4776. * using the provided mapping function. If the array is empty or null, then null is returned.
  4777. *
  4778. * @param values The array to transfer into literal array expression.
  4779. * @param mapper The logic to use for creating an expression for the array's values.
  4780. * @returns An array literal expression representing `values`, or null if `values` is empty or
  4781. * is itself null.
  4782. */
  4783. function toOptionalLiteralArray(values, mapper) {
  4784. if (values === null || values.length === 0) {
  4785. return null;
  4786. }
  4787. return literalArr(values.map(value => mapper(value)));
  4788. }
  4789. /**
  4790. * Creates an object literal expression from the given object, mapping all values to an expression
  4791. * using the provided mapping function. If the object has no keys, then null is returned.
  4792. *
  4793. * @param object The object to transfer into an object literal expression.
  4794. * @param mapper The logic to use for creating an expression for the object's values.
  4795. * @returns An object literal expression representing `object`, or null if `object` does not have
  4796. * any keys.
  4797. */
  4798. function toOptionalLiteralMap(object, mapper) {
  4799. const entries = Object.keys(object).map(key => {
  4800. const value = object[key];
  4801. return { key, value: mapper(value), quoted: true };
  4802. });
  4803. if (entries.length > 0) {
  4804. return literalMap(entries);
  4805. }
  4806. else {
  4807. return null;
  4808. }
  4809. }
  4810. function compileDependencies(deps) {
  4811. if (deps === 'invalid') {
  4812. // The `deps` can be set to the string "invalid" by the `unwrapConstructorDependencies()`
  4813. // function, which tries to convert `ConstructorDeps` into `R3DependencyMetadata[]`.
  4814. return literal('invalid');
  4815. }
  4816. else if (deps === null) {
  4817. return literal(null);
  4818. }
  4819. else {
  4820. return literalArr(deps.map(compileDependency));
  4821. }
  4822. }
  4823. function compileDependency(dep) {
  4824. const depMeta = new DefinitionMap();
  4825. depMeta.set('token', dep.token);
  4826. if (dep.attributeNameType !== null) {
  4827. depMeta.set('attribute', literal(true));
  4828. }
  4829. if (dep.host) {
  4830. depMeta.set('host', literal(true));
  4831. }
  4832. if (dep.optional) {
  4833. depMeta.set('optional', literal(true));
  4834. }
  4835. if (dep.self) {
  4836. depMeta.set('self', literal(true));
  4837. }
  4838. if (dep.skipSelf) {
  4839. depMeta.set('skipSelf', literal(true));
  4840. }
  4841. return depMeta.toLiteralMap();
  4842. }
  4843. /**
  4844. * Generate an expression that has the given `expr` wrapped in the following form:
  4845. *
  4846. * ```
  4847. * forwardRef(() => expr)
  4848. * ```
  4849. */
  4850. function generateForwardRef(expr) {
  4851. return importExpr(Identifiers.forwardRef).callFn([fn([], [new ReturnStatement(expr)])]);
  4852. }
  4853. /**
  4854. * @license
  4855. * Copyright Google LLC All Rights Reserved.
  4856. *
  4857. * Use of this source code is governed by an MIT-style license that can be
  4858. * found in the LICENSE file at https://angular.io/license
  4859. */
  4860. // https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit
  4861. const VERSION = 3;
  4862. const JS_B64_PREFIX = '# sourceMappingURL=data:application/json;base64,';
  4863. class SourceMapGenerator {
  4864. constructor(file = null) {
  4865. this.file = file;
  4866. this.sourcesContent = new Map();
  4867. this.lines = [];
  4868. this.lastCol0 = 0;
  4869. this.hasMappings = false;
  4870. }
  4871. // The content is `null` when the content is expected to be loaded using the URL
  4872. addSource(url, content = null) {
  4873. if (!this.sourcesContent.has(url)) {
  4874. this.sourcesContent.set(url, content);
  4875. }
  4876. return this;
  4877. }
  4878. addLine() {
  4879. this.lines.push([]);
  4880. this.lastCol0 = 0;
  4881. return this;
  4882. }
  4883. addMapping(col0, sourceUrl, sourceLine0, sourceCol0) {
  4884. if (!this.currentLine) {
  4885. throw new Error(`A line must be added before mappings can be added`);
  4886. }
  4887. if (sourceUrl != null && !this.sourcesContent.has(sourceUrl)) {
  4888. throw new Error(`Unknown source file "${sourceUrl}"`);
  4889. }
  4890. if (col0 == null) {
  4891. throw new Error(`The column in the generated code must be provided`);
  4892. }
  4893. if (col0 < this.lastCol0) {
  4894. throw new Error(`Mapping should be added in output order`);
  4895. }
  4896. if (sourceUrl && (sourceLine0 == null || sourceCol0 == null)) {
  4897. throw new Error(`The source location must be provided when a source url is provided`);
  4898. }
  4899. this.hasMappings = true;
  4900. this.lastCol0 = col0;
  4901. this.currentLine.push({ col0, sourceUrl, sourceLine0, sourceCol0 });
  4902. return this;
  4903. }
  4904. /**
  4905. * @internal strip this from published d.ts files due to
  4906. * https://github.com/microsoft/TypeScript/issues/36216
  4907. */
  4908. get currentLine() {
  4909. return this.lines.slice(-1)[0];
  4910. }
  4911. toJSON() {
  4912. if (!this.hasMappings) {
  4913. return null;
  4914. }
  4915. const sourcesIndex = new Map();
  4916. const sources = [];
  4917. const sourcesContent = [];
  4918. Array.from(this.sourcesContent.keys()).forEach((url, i) => {
  4919. sourcesIndex.set(url, i);
  4920. sources.push(url);
  4921. sourcesContent.push(this.sourcesContent.get(url) || null);
  4922. });
  4923. let mappings = '';
  4924. let lastCol0 = 0;
  4925. let lastSourceIndex = 0;
  4926. let lastSourceLine0 = 0;
  4927. let lastSourceCol0 = 0;
  4928. this.lines.forEach(segments => {
  4929. lastCol0 = 0;
  4930. mappings += segments
  4931. .map(segment => {
  4932. // zero-based starting column of the line in the generated code
  4933. let segAsStr = toBase64VLQ(segment.col0 - lastCol0);
  4934. lastCol0 = segment.col0;
  4935. if (segment.sourceUrl != null) {
  4936. // zero-based index into the “sources” list
  4937. segAsStr +=
  4938. toBase64VLQ(sourcesIndex.get(segment.sourceUrl) - lastSourceIndex);
  4939. lastSourceIndex = sourcesIndex.get(segment.sourceUrl);
  4940. // the zero-based starting line in the original source
  4941. segAsStr += toBase64VLQ(segment.sourceLine0 - lastSourceLine0);
  4942. lastSourceLine0 = segment.sourceLine0;
  4943. // the zero-based starting column in the original source
  4944. segAsStr += toBase64VLQ(segment.sourceCol0 - lastSourceCol0);
  4945. lastSourceCol0 = segment.sourceCol0;
  4946. }
  4947. return segAsStr;
  4948. })
  4949. .join(',');
  4950. mappings += ';';
  4951. });
  4952. mappings = mappings.slice(0, -1);
  4953. return {
  4954. 'file': this.file || '',
  4955. 'version': VERSION,
  4956. 'sourceRoot': '',
  4957. 'sources': sources,
  4958. 'sourcesContent': sourcesContent,
  4959. 'mappings': mappings,
  4960. };
  4961. }
  4962. toJsComment() {
  4963. return this.hasMappings ? '//' + JS_B64_PREFIX + toBase64String(JSON.stringify(this, null, 0)) :
  4964. '';
  4965. }
  4966. }
  4967. function toBase64String(value) {
  4968. let b64 = '';
  4969. const encoded = utf8Encode(value);
  4970. for (let i = 0; i < encoded.length;) {
  4971. const i1 = encoded[i++];
  4972. const i2 = i < encoded.length ? encoded[i++] : null;
  4973. const i3 = i < encoded.length ? encoded[i++] : null;
  4974. b64 += toBase64Digit(i1 >> 2);
  4975. b64 += toBase64Digit(((i1 & 3) << 4) | (i2 === null ? 0 : i2 >> 4));
  4976. b64 += i2 === null ? '=' : toBase64Digit(((i2 & 15) << 2) | (i3 === null ? 0 : i3 >> 6));
  4977. b64 += i2 === null || i3 === null ? '=' : toBase64Digit(i3 & 63);
  4978. }
  4979. return b64;
  4980. }
  4981. function toBase64VLQ(value) {
  4982. value = value < 0 ? ((-value) << 1) + 1 : value << 1;
  4983. let out = '';
  4984. do {
  4985. let digit = value & 31;
  4986. value = value >> 5;
  4987. if (value > 0) {
  4988. digit = digit | 32;
  4989. }
  4990. out += toBase64Digit(digit);
  4991. } while (value > 0);
  4992. return out;
  4993. }
  4994. const B64_DIGITS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  4995. function toBase64Digit(value) {
  4996. if (value < 0 || value >= 64) {
  4997. throw new Error(`Can only encode value in the range [0, 63]`);
  4998. }
  4999. return B64_DIGITS[value];
  5000. }
  5001. /**
  5002. * @license
  5003. * Copyright Google LLC All Rights Reserved.
  5004. *
  5005. * Use of this source code is governed by an MIT-style license that can be
  5006. * found in the LICENSE file at https://angular.io/license
  5007. */
  5008. const _SINGLE_QUOTE_ESCAPE_STRING_RE = /'|\\|\n|\r|\$/g;
  5009. const _LEGAL_IDENTIFIER_RE = /^[$A-Z_][0-9A-Z_$]*$/i;
  5010. const _INDENT_WITH = ' ';
  5011. const CATCH_ERROR_VAR$1 = variable('error', null, null);
  5012. const CATCH_STACK_VAR$1 = variable('stack', null, null);
  5013. class _EmittedLine {
  5014. constructor(indent) {
  5015. this.indent = indent;
  5016. this.partsLength = 0;
  5017. this.parts = [];
  5018. this.srcSpans = [];
  5019. }
  5020. }
  5021. class EmitterVisitorContext {
  5022. constructor(_indent) {
  5023. this._indent = _indent;
  5024. this._classes = [];
  5025. this._preambleLineCount = 0;
  5026. this._lines = [new _EmittedLine(_indent)];
  5027. }
  5028. static createRoot() {
  5029. return new EmitterVisitorContext(0);
  5030. }
  5031. /**
  5032. * @internal strip this from published d.ts files due to
  5033. * https://github.com/microsoft/TypeScript/issues/36216
  5034. */
  5035. get _currentLine() {
  5036. return this._lines[this._lines.length - 1];
  5037. }
  5038. println(from, lastPart = '') {
  5039. this.print(from || null, lastPart, true);
  5040. }
  5041. lineIsEmpty() {
  5042. return this._currentLine.parts.length === 0;
  5043. }
  5044. lineLength() {
  5045. return this._currentLine.indent * _INDENT_WITH.length + this._currentLine.partsLength;
  5046. }
  5047. print(from, part, newLine = false) {
  5048. if (part.length > 0) {
  5049. this._currentLine.parts.push(part);
  5050. this._currentLine.partsLength += part.length;
  5051. this._currentLine.srcSpans.push(from && from.sourceSpan || null);
  5052. }
  5053. if (newLine) {
  5054. this._lines.push(new _EmittedLine(this._indent));
  5055. }
  5056. }
  5057. removeEmptyLastLine() {
  5058. if (this.lineIsEmpty()) {
  5059. this._lines.pop();
  5060. }
  5061. }
  5062. incIndent() {
  5063. this._indent++;
  5064. if (this.lineIsEmpty()) {
  5065. this._currentLine.indent = this._indent;
  5066. }
  5067. }
  5068. decIndent() {
  5069. this._indent--;
  5070. if (this.lineIsEmpty()) {
  5071. this._currentLine.indent = this._indent;
  5072. }
  5073. }
  5074. pushClass(clazz) {
  5075. this._classes.push(clazz);
  5076. }
  5077. popClass() {
  5078. return this._classes.pop();
  5079. }
  5080. get currentClass() {
  5081. return this._classes.length > 0 ? this._classes[this._classes.length - 1] : null;
  5082. }
  5083. toSource() {
  5084. return this.sourceLines
  5085. .map(l => l.parts.length > 0 ? _createIndent(l.indent) + l.parts.join('') : '')
  5086. .join('\n');
  5087. }
  5088. toSourceMapGenerator(genFilePath, startsAtLine = 0) {
  5089. const map = new SourceMapGenerator(genFilePath);
  5090. let firstOffsetMapped = false;
  5091. const mapFirstOffsetIfNeeded = () => {
  5092. if (!firstOffsetMapped) {
  5093. // Add a single space so that tools won't try to load the file from disk.
  5094. // Note: We are using virtual urls like `ng:///`, so we have to
  5095. // provide a content here.
  5096. map.addSource(genFilePath, ' ').addMapping(0, genFilePath, 0, 0);
  5097. firstOffsetMapped = true;
  5098. }
  5099. };
  5100. for (let i = 0; i < startsAtLine; i++) {
  5101. map.addLine();
  5102. mapFirstOffsetIfNeeded();
  5103. }
  5104. this.sourceLines.forEach((line, lineIdx) => {
  5105. map.addLine();
  5106. const spans = line.srcSpans;
  5107. const parts = line.parts;
  5108. let col0 = line.indent * _INDENT_WITH.length;
  5109. let spanIdx = 0;
  5110. // skip leading parts without source spans
  5111. while (spanIdx < spans.length && !spans[spanIdx]) {
  5112. col0 += parts[spanIdx].length;
  5113. spanIdx++;
  5114. }
  5115. if (spanIdx < spans.length && lineIdx === 0 && col0 === 0) {
  5116. firstOffsetMapped = true;
  5117. }
  5118. else {
  5119. mapFirstOffsetIfNeeded();
  5120. }
  5121. while (spanIdx < spans.length) {
  5122. const span = spans[spanIdx];
  5123. const source = span.start.file;
  5124. const sourceLine = span.start.line;
  5125. const sourceCol = span.start.col;
  5126. map.addSource(source.url, source.content)
  5127. .addMapping(col0, source.url, sourceLine, sourceCol);
  5128. col0 += parts[spanIdx].length;
  5129. spanIdx++;
  5130. // assign parts without span or the same span to the previous segment
  5131. while (spanIdx < spans.length && (span === spans[spanIdx] || !spans[spanIdx])) {
  5132. col0 += parts[spanIdx].length;
  5133. spanIdx++;
  5134. }
  5135. }
  5136. });
  5137. return map;
  5138. }
  5139. setPreambleLineCount(count) {
  5140. return this._preambleLineCount = count;
  5141. }
  5142. spanOf(line, column) {
  5143. const emittedLine = this._lines[line - this._preambleLineCount];
  5144. if (emittedLine) {
  5145. let columnsLeft = column - _createIndent(emittedLine.indent).length;
  5146. for (let partIndex = 0; partIndex < emittedLine.parts.length; partIndex++) {
  5147. const part = emittedLine.parts[partIndex];
  5148. if (part.length > columnsLeft) {
  5149. return emittedLine.srcSpans[partIndex];
  5150. }
  5151. columnsLeft -= part.length;
  5152. }
  5153. }
  5154. return null;
  5155. }
  5156. /**
  5157. * @internal strip this from published d.ts files due to
  5158. * https://github.com/microsoft/TypeScript/issues/36216
  5159. */
  5160. get sourceLines() {
  5161. if (this._lines.length && this._lines[this._lines.length - 1].parts.length === 0) {
  5162. return this._lines.slice(0, -1);
  5163. }
  5164. return this._lines;
  5165. }
  5166. }
  5167. class AbstractEmitterVisitor {
  5168. constructor(_escapeDollarInStrings) {
  5169. this._escapeDollarInStrings = _escapeDollarInStrings;
  5170. }
  5171. printLeadingComments(stmt, ctx) {
  5172. if (stmt.leadingComments === undefined) {
  5173. return;
  5174. }
  5175. for (const comment of stmt.leadingComments) {
  5176. if (comment instanceof JSDocComment) {
  5177. ctx.print(stmt, `/*${comment.toString()}*/`, comment.trailingNewline);
  5178. }
  5179. else {
  5180. if (comment.multiline) {
  5181. ctx.print(stmt, `/* ${comment.text} */`, comment.trailingNewline);
  5182. }
  5183. else {
  5184. comment.text.split('\n').forEach((line) => {
  5185. ctx.println(stmt, `// ${line}`);
  5186. });
  5187. }
  5188. }
  5189. }
  5190. }
  5191. visitExpressionStmt(stmt, ctx) {
  5192. this.printLeadingComments(stmt, ctx);
  5193. stmt.expr.visitExpression(this, ctx);
  5194. ctx.println(stmt, ';');
  5195. return null;
  5196. }
  5197. visitReturnStmt(stmt, ctx) {
  5198. this.printLeadingComments(stmt, ctx);
  5199. ctx.print(stmt, `return `);
  5200. stmt.value.visitExpression(this, ctx);
  5201. ctx.println(stmt, ';');
  5202. return null;
  5203. }
  5204. visitIfStmt(stmt, ctx) {
  5205. this.printLeadingComments(stmt, ctx);
  5206. ctx.print(stmt, `if (`);
  5207. stmt.condition.visitExpression(this, ctx);
  5208. ctx.print(stmt, `) {`);
  5209. const hasElseCase = stmt.falseCase != null && stmt.falseCase.length > 0;
  5210. if (stmt.trueCase.length <= 1 && !hasElseCase) {
  5211. ctx.print(stmt, ` `);
  5212. this.visitAllStatements(stmt.trueCase, ctx);
  5213. ctx.removeEmptyLastLine();
  5214. ctx.print(stmt, ` `);
  5215. }
  5216. else {
  5217. ctx.println();
  5218. ctx.incIndent();
  5219. this.visitAllStatements(stmt.trueCase, ctx);
  5220. ctx.decIndent();
  5221. if (hasElseCase) {
  5222. ctx.println(stmt, `} else {`);
  5223. ctx.incIndent();
  5224. this.visitAllStatements(stmt.falseCase, ctx);
  5225. ctx.decIndent();
  5226. }
  5227. }
  5228. ctx.println(stmt, `}`);
  5229. return null;
  5230. }
  5231. visitThrowStmt(stmt, ctx) {
  5232. this.printLeadingComments(stmt, ctx);
  5233. ctx.print(stmt, `throw `);
  5234. stmt.error.visitExpression(this, ctx);
  5235. ctx.println(stmt, `;`);
  5236. return null;
  5237. }
  5238. visitWriteVarExpr(expr, ctx) {
  5239. const lineWasEmpty = ctx.lineIsEmpty();
  5240. if (!lineWasEmpty) {
  5241. ctx.print(expr, '(');
  5242. }
  5243. ctx.print(expr, `${expr.name} = `);
  5244. expr.value.visitExpression(this, ctx);
  5245. if (!lineWasEmpty) {
  5246. ctx.print(expr, ')');
  5247. }
  5248. return null;
  5249. }
  5250. visitWriteKeyExpr(expr, ctx) {
  5251. const lineWasEmpty = ctx.lineIsEmpty();
  5252. if (!lineWasEmpty) {
  5253. ctx.print(expr, '(');
  5254. }
  5255. expr.receiver.visitExpression(this, ctx);
  5256. ctx.print(expr, `[`);
  5257. expr.index.visitExpression(this, ctx);
  5258. ctx.print(expr, `] = `);
  5259. expr.value.visitExpression(this, ctx);
  5260. if (!lineWasEmpty) {
  5261. ctx.print(expr, ')');
  5262. }
  5263. return null;
  5264. }
  5265. visitWritePropExpr(expr, ctx) {
  5266. const lineWasEmpty = ctx.lineIsEmpty();
  5267. if (!lineWasEmpty) {
  5268. ctx.print(expr, '(');
  5269. }
  5270. expr.receiver.visitExpression(this, ctx);
  5271. ctx.print(expr, `.${expr.name} = `);
  5272. expr.value.visitExpression(this, ctx);
  5273. if (!lineWasEmpty) {
  5274. ctx.print(expr, ')');
  5275. }
  5276. return null;
  5277. }
  5278. visitInvokeMethodExpr(expr, ctx) {
  5279. expr.receiver.visitExpression(this, ctx);
  5280. let name = expr.name;
  5281. if (expr.builtin != null) {
  5282. name = this.getBuiltinMethodName(expr.builtin);
  5283. if (name == null) {
  5284. // some builtins just mean to skip the call.
  5285. return null;
  5286. }
  5287. }
  5288. ctx.print(expr, `.${name}(`);
  5289. this.visitAllExpressions(expr.args, ctx, `,`);
  5290. ctx.print(expr, `)`);
  5291. return null;
  5292. }
  5293. visitInvokeFunctionExpr(expr, ctx) {
  5294. expr.fn.visitExpression(this, ctx);
  5295. ctx.print(expr, `(`);
  5296. this.visitAllExpressions(expr.args, ctx, ',');
  5297. ctx.print(expr, `)`);
  5298. return null;
  5299. }
  5300. visitTaggedTemplateExpr(expr, ctx) {
  5301. expr.tag.visitExpression(this, ctx);
  5302. ctx.print(expr, '`' + expr.template.elements[0].rawText);
  5303. for (let i = 1; i < expr.template.elements.length; i++) {
  5304. ctx.print(expr, '${');
  5305. expr.template.expressions[i - 1].visitExpression(this, ctx);
  5306. ctx.print(expr, `}${expr.template.elements[i].rawText}`);
  5307. }
  5308. ctx.print(expr, '`');
  5309. return null;
  5310. }
  5311. visitWrappedNodeExpr(ast, ctx) {
  5312. throw new Error('Abstract emitter cannot visit WrappedNodeExpr.');
  5313. }
  5314. visitTypeofExpr(expr, ctx) {
  5315. ctx.print(expr, 'typeof ');
  5316. expr.expr.visitExpression(this, ctx);
  5317. }
  5318. visitReadVarExpr(ast, ctx) {
  5319. let varName = ast.name;
  5320. if (ast.builtin != null) {
  5321. switch (ast.builtin) {
  5322. case BuiltinVar.Super:
  5323. varName = 'super';
  5324. break;
  5325. case BuiltinVar.This:
  5326. varName = 'this';
  5327. break;
  5328. case BuiltinVar.CatchError:
  5329. varName = CATCH_ERROR_VAR$1.name;
  5330. break;
  5331. case BuiltinVar.CatchStack:
  5332. varName = CATCH_STACK_VAR$1.name;
  5333. break;
  5334. default:
  5335. throw new Error(`Unknown builtin variable ${ast.builtin}`);
  5336. }
  5337. }
  5338. ctx.print(ast, varName);
  5339. return null;
  5340. }
  5341. visitInstantiateExpr(ast, ctx) {
  5342. ctx.print(ast, `new `);
  5343. ast.classExpr.visitExpression(this, ctx);
  5344. ctx.print(ast, `(`);
  5345. this.visitAllExpressions(ast.args, ctx, ',');
  5346. ctx.print(ast, `)`);
  5347. return null;
  5348. }
  5349. visitLiteralExpr(ast, ctx) {
  5350. const value = ast.value;
  5351. if (typeof value === 'string') {
  5352. ctx.print(ast, escapeIdentifier(value, this._escapeDollarInStrings));
  5353. }
  5354. else {
  5355. ctx.print(ast, `${value}`);
  5356. }
  5357. return null;
  5358. }
  5359. visitLocalizedString(ast, ctx) {
  5360. const head = ast.serializeI18nHead();
  5361. ctx.print(ast, '$localize `' + head.raw);
  5362. for (let i = 1; i < ast.messageParts.length; i++) {
  5363. ctx.print(ast, '${');
  5364. ast.expressions[i - 1].visitExpression(this, ctx);
  5365. ctx.print(ast, `}${ast.serializeI18nTemplatePart(i).raw}`);
  5366. }
  5367. ctx.print(ast, '`');
  5368. return null;
  5369. }
  5370. visitConditionalExpr(ast, ctx) {
  5371. ctx.print(ast, `(`);
  5372. ast.condition.visitExpression(this, ctx);
  5373. ctx.print(ast, '? ');
  5374. ast.trueCase.visitExpression(this, ctx);
  5375. ctx.print(ast, ': ');
  5376. ast.falseCase.visitExpression(this, ctx);
  5377. ctx.print(ast, `)`);
  5378. return null;
  5379. }
  5380. visitNotExpr(ast, ctx) {
  5381. ctx.print(ast, '!');
  5382. ast.condition.visitExpression(this, ctx);
  5383. return null;
  5384. }
  5385. visitAssertNotNullExpr(ast, ctx) {
  5386. ast.condition.visitExpression(this, ctx);
  5387. return null;
  5388. }
  5389. visitUnaryOperatorExpr(ast, ctx) {
  5390. let opStr;
  5391. switch (ast.operator) {
  5392. case UnaryOperator.Plus:
  5393. opStr = '+';
  5394. break;
  5395. case UnaryOperator.Minus:
  5396. opStr = '-';
  5397. break;
  5398. default:
  5399. throw new Error(`Unknown operator ${ast.operator}`);
  5400. }
  5401. if (ast.parens)
  5402. ctx.print(ast, `(`);
  5403. ctx.print(ast, opStr);
  5404. ast.expr.visitExpression(this, ctx);
  5405. if (ast.parens)
  5406. ctx.print(ast, `)`);
  5407. return null;
  5408. }
  5409. visitBinaryOperatorExpr(ast, ctx) {
  5410. let opStr;
  5411. switch (ast.operator) {
  5412. case BinaryOperator.Equals:
  5413. opStr = '==';
  5414. break;
  5415. case BinaryOperator.Identical:
  5416. opStr = '===';
  5417. break;
  5418. case BinaryOperator.NotEquals:
  5419. opStr = '!=';
  5420. break;
  5421. case BinaryOperator.NotIdentical:
  5422. opStr = '!==';
  5423. break;
  5424. case BinaryOperator.And:
  5425. opStr = '&&';
  5426. break;
  5427. case BinaryOperator.BitwiseAnd:
  5428. opStr = '&';
  5429. break;
  5430. case BinaryOperator.Or:
  5431. opStr = '||';
  5432. break;
  5433. case BinaryOperator.Plus:
  5434. opStr = '+';
  5435. break;
  5436. case BinaryOperator.Minus:
  5437. opStr = '-';
  5438. break;
  5439. case BinaryOperator.Divide:
  5440. opStr = '/';
  5441. break;
  5442. case BinaryOperator.Multiply:
  5443. opStr = '*';
  5444. break;
  5445. case BinaryOperator.Modulo:
  5446. opStr = '%';
  5447. break;
  5448. case BinaryOperator.Lower:
  5449. opStr = '<';
  5450. break;
  5451. case BinaryOperator.LowerEquals:
  5452. opStr = '<=';
  5453. break;
  5454. case BinaryOperator.Bigger:
  5455. opStr = '>';
  5456. break;
  5457. case BinaryOperator.BiggerEquals:
  5458. opStr = '>=';
  5459. break;
  5460. case BinaryOperator.NullishCoalesce:
  5461. opStr = '??';
  5462. break;
  5463. default:
  5464. throw new Error(`Unknown operator ${ast.operator}`);
  5465. }
  5466. if (ast.parens)
  5467. ctx.print(ast, `(`);
  5468. ast.lhs.visitExpression(this, ctx);
  5469. ctx.print(ast, ` ${opStr} `);
  5470. ast.rhs.visitExpression(this, ctx);
  5471. if (ast.parens)
  5472. ctx.print(ast, `)`);
  5473. return null;
  5474. }
  5475. visitReadPropExpr(ast, ctx) {
  5476. ast.receiver.visitExpression(this, ctx);
  5477. ctx.print(ast, `.`);
  5478. ctx.print(ast, ast.name);
  5479. return null;
  5480. }
  5481. visitReadKeyExpr(ast, ctx) {
  5482. ast.receiver.visitExpression(this, ctx);
  5483. ctx.print(ast, `[`);
  5484. ast.index.visitExpression(this, ctx);
  5485. ctx.print(ast, `]`);
  5486. return null;
  5487. }
  5488. visitLiteralArrayExpr(ast, ctx) {
  5489. ctx.print(ast, `[`);
  5490. this.visitAllExpressions(ast.entries, ctx, ',');
  5491. ctx.print(ast, `]`);
  5492. return null;
  5493. }
  5494. visitLiteralMapExpr(ast, ctx) {
  5495. ctx.print(ast, `{`);
  5496. this.visitAllObjects(entry => {
  5497. ctx.print(ast, `${escapeIdentifier(entry.key, this._escapeDollarInStrings, entry.quoted)}:`);
  5498. entry.value.visitExpression(this, ctx);
  5499. }, ast.entries, ctx, ',');
  5500. ctx.print(ast, `}`);
  5501. return null;
  5502. }
  5503. visitCommaExpr(ast, ctx) {
  5504. ctx.print(ast, '(');
  5505. this.visitAllExpressions(ast.parts, ctx, ',');
  5506. ctx.print(ast, ')');
  5507. return null;
  5508. }
  5509. visitAllExpressions(expressions, ctx, separator) {
  5510. this.visitAllObjects(expr => expr.visitExpression(this, ctx), expressions, ctx, separator);
  5511. }
  5512. visitAllObjects(handler, expressions, ctx, separator) {
  5513. let incrementedIndent = false;
  5514. for (let i = 0; i < expressions.length; i++) {
  5515. if (i > 0) {
  5516. if (ctx.lineLength() > 80) {
  5517. ctx.print(null, separator, true);
  5518. if (!incrementedIndent) {
  5519. // continuation are marked with double indent.
  5520. ctx.incIndent();
  5521. ctx.incIndent();
  5522. incrementedIndent = true;
  5523. }
  5524. }
  5525. else {
  5526. ctx.print(null, separator, false);
  5527. }
  5528. }
  5529. handler(expressions[i]);
  5530. }
  5531. if (incrementedIndent) {
  5532. // continuation are marked with double indent.
  5533. ctx.decIndent();
  5534. ctx.decIndent();
  5535. }
  5536. }
  5537. visitAllStatements(statements, ctx) {
  5538. statements.forEach((stmt) => stmt.visitStatement(this, ctx));
  5539. }
  5540. }
  5541. function escapeIdentifier(input, escapeDollar, alwaysQuote = true) {
  5542. if (input == null) {
  5543. return null;
  5544. }
  5545. const body = input.replace(_SINGLE_QUOTE_ESCAPE_STRING_RE, (...match) => {
  5546. if (match[0] == '$') {
  5547. return escapeDollar ? '\\$' : '$';
  5548. }
  5549. else if (match[0] == '\n') {
  5550. return '\\n';
  5551. }
  5552. else if (match[0] == '\r') {
  5553. return '\\r';
  5554. }
  5555. else {
  5556. return `\\${match[0]}`;
  5557. }
  5558. });
  5559. const requiresQuotes = alwaysQuote || !_LEGAL_IDENTIFIER_RE.test(body);
  5560. return requiresQuotes ? `'${body}'` : body;
  5561. }
  5562. function _createIndent(count) {
  5563. let res = '';
  5564. for (let i = 0; i < count; i++) {
  5565. res += _INDENT_WITH;
  5566. }
  5567. return res;
  5568. }
  5569. /**
  5570. * @license
  5571. * Copyright Google LLC All Rights Reserved.
  5572. *
  5573. * Use of this source code is governed by an MIT-style license that can be
  5574. * found in the LICENSE file at https://angular.io/license
  5575. */
  5576. function typeWithParameters(type, numParams) {
  5577. if (numParams === 0) {
  5578. return expressionType(type);
  5579. }
  5580. const params = [];
  5581. for (let i = 0; i < numParams; i++) {
  5582. params.push(DYNAMIC_TYPE);
  5583. }
  5584. return expressionType(type, undefined, params);
  5585. }
  5586. const ANIMATE_SYMBOL_PREFIX = '@';
  5587. function prepareSyntheticPropertyName(name) {
  5588. return `${ANIMATE_SYMBOL_PREFIX}${name}`;
  5589. }
  5590. function prepareSyntheticListenerName(name, phase) {
  5591. return `${ANIMATE_SYMBOL_PREFIX}${name}.${phase}`;
  5592. }
  5593. function getSafePropertyAccessString(accessor, name) {
  5594. const escapedName = escapeIdentifier(name, false, false);
  5595. return escapedName !== name ? `${accessor}[${escapedName}]` : `${accessor}.${name}`;
  5596. }
  5597. function prepareSyntheticListenerFunctionName(name, phase) {
  5598. return `animation_${name}_${phase}`;
  5599. }
  5600. function jitOnlyGuardedExpression(expr) {
  5601. return guardedExpression('ngJitMode', expr);
  5602. }
  5603. function devOnlyGuardedExpression(expr) {
  5604. return guardedExpression('ngDevMode', expr);
  5605. }
  5606. function guardedExpression(guard, expr) {
  5607. const guardExpr = new ExternalExpr({ name: guard, moduleName: null });
  5608. const guardNotDefined = new BinaryOperatorExpr(BinaryOperator.Identical, new TypeofExpr(guardExpr), literal('undefined'));
  5609. const guardUndefinedOrTrue = new BinaryOperatorExpr(BinaryOperator.Or, guardNotDefined, guardExpr, /* type */ undefined,
  5610. /* sourceSpan */ undefined, true);
  5611. return new BinaryOperatorExpr(BinaryOperator.And, guardUndefinedOrTrue, expr);
  5612. }
  5613. function wrapReference(value) {
  5614. const wrapped = new WrappedNodeExpr(value);
  5615. return { value: wrapped, type: wrapped };
  5616. }
  5617. function refsToArray(refs, shouldForwardDeclare) {
  5618. const values = literalArr(refs.map(ref => ref.value));
  5619. return shouldForwardDeclare ? fn([], [new ReturnStatement(values)]) : values;
  5620. }
  5621. /**
  5622. * @license
  5623. * Copyright Google LLC All Rights Reserved.
  5624. *
  5625. * Use of this source code is governed by an MIT-style license that can be
  5626. * found in the LICENSE file at https://angular.io/license
  5627. */
  5628. var R3FactoryDelegateType;
  5629. (function (R3FactoryDelegateType) {
  5630. R3FactoryDelegateType[R3FactoryDelegateType["Class"] = 0] = "Class";
  5631. R3FactoryDelegateType[R3FactoryDelegateType["Function"] = 1] = "Function";
  5632. })(R3FactoryDelegateType || (R3FactoryDelegateType = {}));
  5633. var FactoryTarget;
  5634. (function (FactoryTarget) {
  5635. FactoryTarget[FactoryTarget["Directive"] = 0] = "Directive";
  5636. FactoryTarget[FactoryTarget["Component"] = 1] = "Component";
  5637. FactoryTarget[FactoryTarget["Injectable"] = 2] = "Injectable";
  5638. FactoryTarget[FactoryTarget["Pipe"] = 3] = "Pipe";
  5639. FactoryTarget[FactoryTarget["NgModule"] = 4] = "NgModule";
  5640. })(FactoryTarget || (FactoryTarget = {}));
  5641. /**
  5642. * Construct a factory function expression for the given `R3FactoryMetadata`.
  5643. */
  5644. function compileFactoryFunction(meta) {
  5645. const t = variable('t');
  5646. let baseFactoryVar = null;
  5647. // The type to instantiate via constructor invocation. If there is no delegated factory, meaning
  5648. // this type is always created by constructor invocation, then this is the type-to-create
  5649. // parameter provided by the user (t) if specified, or the current type if not. If there is a
  5650. // delegated factory (which is used to create the current type) then this is only the type-to-
  5651. // create parameter (t).
  5652. const typeForCtor = !isDelegatedFactoryMetadata(meta) ?
  5653. new BinaryOperatorExpr(BinaryOperator.Or, t, meta.internalType) :
  5654. t;
  5655. let ctorExpr = null;
  5656. if (meta.deps !== null) {
  5657. // There is a constructor (either explicitly or implicitly defined).
  5658. if (meta.deps !== 'invalid') {
  5659. ctorExpr = new InstantiateExpr(typeForCtor, injectDependencies(meta.deps, meta.target));
  5660. }
  5661. }
  5662. else {
  5663. // There is no constructor, use the base class' factory to construct typeForCtor.
  5664. baseFactoryVar = variable(`ɵ${meta.name}_BaseFactory`);
  5665. ctorExpr = baseFactoryVar.callFn([typeForCtor]);
  5666. }
  5667. const body = [];
  5668. let retExpr = null;
  5669. function makeConditionalFactory(nonCtorExpr) {
  5670. const r = variable('r');
  5671. body.push(r.set(NULL_EXPR).toDeclStmt());
  5672. const ctorStmt = ctorExpr !== null ? r.set(ctorExpr).toStmt() :
  5673. importExpr(Identifiers.invalidFactory).callFn([]).toStmt();
  5674. body.push(ifStmt(t, [ctorStmt], [r.set(nonCtorExpr).toStmt()]));
  5675. return r;
  5676. }
  5677. if (isDelegatedFactoryMetadata(meta)) {
  5678. // This type is created with a delegated factory. If a type parameter is not specified, call
  5679. // the factory instead.
  5680. const delegateArgs = injectDependencies(meta.delegateDeps, meta.target);
  5681. // Either call `new delegate(...)` or `delegate(...)` depending on meta.delegateType.
  5682. const factoryExpr = new (meta.delegateType === R3FactoryDelegateType.Class ?
  5683. InstantiateExpr :
  5684. InvokeFunctionExpr)(meta.delegate, delegateArgs);
  5685. retExpr = makeConditionalFactory(factoryExpr);
  5686. }
  5687. else if (isExpressionFactoryMetadata(meta)) {
  5688. // TODO(alxhub): decide whether to lower the value here or in the caller
  5689. retExpr = makeConditionalFactory(meta.expression);
  5690. }
  5691. else {
  5692. retExpr = ctorExpr;
  5693. }
  5694. if (retExpr === null) {
  5695. // The expression cannot be formed so render an `ɵɵinvalidFactory()` call.
  5696. body.push(importExpr(Identifiers.invalidFactory).callFn([]).toStmt());
  5697. }
  5698. else if (baseFactoryVar !== null) {
  5699. // This factory uses a base factory, so call `ɵɵgetInheritedFactory()` to compute it.
  5700. const getInheritedFactoryCall = importExpr(Identifiers.getInheritedFactory).callFn([meta.internalType]);
  5701. // Memoize the base factoryFn: `baseFactory || (baseFactory = ɵɵgetInheritedFactory(...))`
  5702. const baseFactory = new BinaryOperatorExpr(BinaryOperator.Or, baseFactoryVar, baseFactoryVar.set(getInheritedFactoryCall));
  5703. body.push(new ReturnStatement(baseFactory.callFn([typeForCtor])));
  5704. }
  5705. else {
  5706. // This is straightforward factory, just return it.
  5707. body.push(new ReturnStatement(retExpr));
  5708. }
  5709. let factoryFn = fn([new FnParam('t', DYNAMIC_TYPE)], body, INFERRED_TYPE, undefined, `${meta.name}_Factory`);
  5710. if (baseFactoryVar !== null) {
  5711. // There is a base factory variable so wrap its declaration along with the factory function into
  5712. // an IIFE.
  5713. factoryFn = fn([], [
  5714. new DeclareVarStmt(baseFactoryVar.name),
  5715. new ReturnStatement(factoryFn)
  5716. ]).callFn([], /* sourceSpan */ undefined, /* pure */ true);
  5717. }
  5718. return {
  5719. expression: factoryFn,
  5720. statements: [],
  5721. type: createFactoryType(meta),
  5722. };
  5723. }
  5724. function createFactoryType(meta) {
  5725. const ctorDepsType = meta.deps !== null && meta.deps !== 'invalid' ? createCtorDepsType(meta.deps) : NONE_TYPE;
  5726. return expressionType(importExpr(Identifiers.FactoryDeclaration, [typeWithParameters(meta.type.type, meta.typeArgumentCount), ctorDepsType]));
  5727. }
  5728. function injectDependencies(deps, target) {
  5729. return deps.map((dep, index) => compileInjectDependency(dep, target, index));
  5730. }
  5731. function compileInjectDependency(dep, target, index) {
  5732. // Interpret the dependency according to its resolved type.
  5733. if (dep.token === null) {
  5734. return importExpr(Identifiers.invalidFactoryDep).callFn([literal(index)]);
  5735. }
  5736. else if (dep.attributeNameType === null) {
  5737. // Build up the injection flags according to the metadata.
  5738. const flags = 0 /* Default */ | (dep.self ? 2 /* Self */ : 0) |
  5739. (dep.skipSelf ? 4 /* SkipSelf */ : 0) | (dep.host ? 1 /* Host */ : 0) |
  5740. (dep.optional ? 8 /* Optional */ : 0) |
  5741. (target === FactoryTarget.Pipe ? 16 /* ForPipe */ : 0);
  5742. // If this dependency is optional or otherwise has non-default flags, then additional
  5743. // parameters describing how to inject the dependency must be passed to the inject function
  5744. // that's being used.
  5745. let flagsParam = (flags !== 0 /* Default */ || dep.optional) ? literal(flags) : null;
  5746. // Build up the arguments to the injectFn call.
  5747. const injectArgs = [dep.token];
  5748. if (flagsParam) {
  5749. injectArgs.push(flagsParam);
  5750. }
  5751. const injectFn = getInjectFn(target);
  5752. return importExpr(injectFn).callFn(injectArgs);
  5753. }
  5754. else {
  5755. // The `dep.attributeTypeName` value is defined, which indicates that this is an `@Attribute()`
  5756. // type dependency. For the generated JS we still want to use the `dep.token` value in case the
  5757. // name given for the attribute is not a string literal. For example given `@Attribute(foo())`,
  5758. // we want to generate `ɵɵinjectAttribute(foo())`.
  5759. //
  5760. // The `dep.attributeTypeName` is only actually used (in `createCtorDepType()`) to generate
  5761. // typings.
  5762. return importExpr(Identifiers.injectAttribute).callFn([dep.token]);
  5763. }
  5764. }
  5765. function createCtorDepsType(deps) {
  5766. let hasTypes = false;
  5767. const attributeTypes = deps.map(dep => {
  5768. const type = createCtorDepType(dep);
  5769. if (type !== null) {
  5770. hasTypes = true;
  5771. return type;
  5772. }
  5773. else {
  5774. return literal(null);
  5775. }
  5776. });
  5777. if (hasTypes) {
  5778. return expressionType(literalArr(attributeTypes));
  5779. }
  5780. else {
  5781. return NONE_TYPE;
  5782. }
  5783. }
  5784. function createCtorDepType(dep) {
  5785. const entries = [];
  5786. if (dep.attributeNameType !== null) {
  5787. entries.push({ key: 'attribute', value: dep.attributeNameType, quoted: false });
  5788. }
  5789. if (dep.optional) {
  5790. entries.push({ key: 'optional', value: literal(true), quoted: false });
  5791. }
  5792. if (dep.host) {
  5793. entries.push({ key: 'host', value: literal(true), quoted: false });
  5794. }
  5795. if (dep.self) {
  5796. entries.push({ key: 'self', value: literal(true), quoted: false });
  5797. }
  5798. if (dep.skipSelf) {
  5799. entries.push({ key: 'skipSelf', value: literal(true), quoted: false });
  5800. }
  5801. return entries.length > 0 ? literalMap(entries) : null;
  5802. }
  5803. function isDelegatedFactoryMetadata(meta) {
  5804. return meta.delegateType !== undefined;
  5805. }
  5806. function isExpressionFactoryMetadata(meta) {
  5807. return meta.expression !== undefined;
  5808. }
  5809. function getInjectFn(target) {
  5810. switch (target) {
  5811. case FactoryTarget.Component:
  5812. case FactoryTarget.Directive:
  5813. case FactoryTarget.Pipe:
  5814. return Identifiers.directiveInject;
  5815. case FactoryTarget.NgModule:
  5816. case FactoryTarget.Injectable:
  5817. default:
  5818. return Identifiers.inject;
  5819. }
  5820. }
  5821. /**
  5822. * @license
  5823. * Copyright Google LLC All Rights Reserved.
  5824. *
  5825. * Use of this source code is governed by an MIT-style license that can be
  5826. * found in the LICENSE file at https://angular.io/license
  5827. */
  5828. function createR3ProviderExpression(expression, isForwardRef) {
  5829. return { expression, isForwardRef };
  5830. }
  5831. function compileInjectable(meta, resolveForwardRefs) {
  5832. let result = null;
  5833. const factoryMeta = {
  5834. name: meta.name,
  5835. type: meta.type,
  5836. internalType: meta.internalType,
  5837. typeArgumentCount: meta.typeArgumentCount,
  5838. deps: [],
  5839. target: FactoryTarget.Injectable,
  5840. };
  5841. if (meta.useClass !== undefined) {
  5842. // meta.useClass has two modes of operation. Either deps are specified, in which case `new` is
  5843. // used to instantiate the class with dependencies injected, or deps are not specified and
  5844. // the factory of the class is used to instantiate it.
  5845. //
  5846. // A special case exists for useClass: Type where Type is the injectable type itself and no
  5847. // deps are specified, in which case 'useClass' is effectively ignored.
  5848. const useClassOnSelf = meta.useClass.expression.isEquivalent(meta.internalType);
  5849. let deps = undefined;
  5850. if (meta.deps !== undefined) {
  5851. deps = meta.deps;
  5852. }
  5853. if (deps !== undefined) {
  5854. // factory: () => new meta.useClass(...deps)
  5855. result = compileFactoryFunction(Object.assign(Object.assign({}, factoryMeta), { delegate: meta.useClass.expression, delegateDeps: deps, delegateType: R3FactoryDelegateType.Class }));
  5856. }
  5857. else if (useClassOnSelf) {
  5858. result = compileFactoryFunction(factoryMeta);
  5859. }
  5860. else {
  5861. result = {
  5862. statements: [],
  5863. expression: delegateToFactory(meta.type.value, meta.useClass.expression, resolveForwardRefs)
  5864. };
  5865. }
  5866. }
  5867. else if (meta.useFactory !== undefined) {
  5868. if (meta.deps !== undefined) {
  5869. result = compileFactoryFunction(Object.assign(Object.assign({}, factoryMeta), { delegate: meta.useFactory, delegateDeps: meta.deps || [], delegateType: R3FactoryDelegateType.Function }));
  5870. }
  5871. else {
  5872. result = {
  5873. statements: [],
  5874. expression: fn([], [new ReturnStatement(meta.useFactory.callFn([]))])
  5875. };
  5876. }
  5877. }
  5878. else if (meta.useValue !== undefined) {
  5879. // Note: it's safe to use `meta.useValue` instead of the `USE_VALUE in meta` check used for
  5880. // client code because meta.useValue is an Expression which will be defined even if the actual
  5881. // value is undefined.
  5882. result = compileFactoryFunction(Object.assign(Object.assign({}, factoryMeta), { expression: meta.useValue.expression }));
  5883. }
  5884. else if (meta.useExisting !== undefined) {
  5885. // useExisting is an `inject` call on the existing token.
  5886. result = compileFactoryFunction(Object.assign(Object.assign({}, factoryMeta), { expression: importExpr(Identifiers.inject).callFn([meta.useExisting.expression]) }));
  5887. }
  5888. else {
  5889. result = {
  5890. statements: [],
  5891. expression: delegateToFactory(meta.type.value, meta.internalType, resolveForwardRefs)
  5892. };
  5893. }
  5894. const token = meta.internalType;
  5895. const injectableProps = new DefinitionMap();
  5896. injectableProps.set('token', token);
  5897. injectableProps.set('factory', result.expression);
  5898. // Only generate providedIn property if it has a non-null value
  5899. if (meta.providedIn.expression.value !== null) {
  5900. injectableProps.set('providedIn', meta.providedIn.isForwardRef ? generateForwardRef(meta.providedIn.expression) :
  5901. meta.providedIn.expression);
  5902. }
  5903. const expression = importExpr(Identifiers.ɵɵdefineInjectable)
  5904. .callFn([injectableProps.toLiteralMap()], undefined, true);
  5905. return {
  5906. expression,
  5907. type: createInjectableType(meta),
  5908. statements: result.statements,
  5909. };
  5910. }
  5911. function createInjectableType(meta) {
  5912. return new ExpressionType(importExpr(Identifiers.InjectableDeclaration, [typeWithParameters(meta.type.type, meta.typeArgumentCount)]));
  5913. }
  5914. function delegateToFactory(type, internalType, unwrapForwardRefs) {
  5915. if (type.node === internalType.node) {
  5916. // The types are the same, so we can simply delegate directly to the type's factory.
  5917. // ```
  5918. // factory: type.ɵfac
  5919. // ```
  5920. return internalType.prop('ɵfac');
  5921. }
  5922. if (!unwrapForwardRefs) {
  5923. // The type is not wrapped in a `forwardRef()`, so we create a simple factory function that
  5924. // accepts a sub-type as an argument.
  5925. // ```
  5926. // factory: function(t) { return internalType.ɵfac(t); }
  5927. // ```
  5928. return createFactoryFunction(internalType);
  5929. }
  5930. // The internalType is actually wrapped in a `forwardRef()` so we need to resolve that before
  5931. // calling its factory.
  5932. // ```
  5933. // factory: function(t) { return core.resolveForwardRef(type).ɵfac(t); }
  5934. // ```
  5935. const unwrappedType = importExpr(Identifiers.resolveForwardRef).callFn([internalType]);
  5936. return createFactoryFunction(unwrappedType);
  5937. }
  5938. function createFactoryFunction(type) {
  5939. return fn([new FnParam('t', DYNAMIC_TYPE)], [new ReturnStatement(type.callMethod('ɵfac', [variable('t')]))]);
  5940. }
  5941. /**
  5942. * @license
  5943. * Copyright Google LLC All Rights Reserved.
  5944. *
  5945. * Use of this source code is governed by an MIT-style license that can be
  5946. * found in the LICENSE file at https://angular.io/license
  5947. */
  5948. function assertArrayOfStrings(identifier, value) {
  5949. if (value == null) {
  5950. return;
  5951. }
  5952. if (!Array.isArray(value)) {
  5953. throw new Error(`Expected '${identifier}' to be an array of strings.`);
  5954. }
  5955. for (let i = 0; i < value.length; i += 1) {
  5956. if (typeof value[i] !== 'string') {
  5957. throw new Error(`Expected '${identifier}' to be an array of strings.`);
  5958. }
  5959. }
  5960. }
  5961. const UNUSABLE_INTERPOLATION_REGEXPS = [
  5962. /^\s*$/,
  5963. /[<>]/,
  5964. /^[{}]$/,
  5965. /&(#|[a-z])/i,
  5966. /^\/\//, // comment
  5967. ];
  5968. function assertInterpolationSymbols(identifier, value) {
  5969. if (value != null && !(Array.isArray(value) && value.length == 2)) {
  5970. throw new Error(`Expected '${identifier}' to be an array, [start, end].`);
  5971. }
  5972. else if (value != null) {
  5973. const start = value[0];
  5974. const end = value[1];
  5975. // Check for unusable interpolation symbols
  5976. UNUSABLE_INTERPOLATION_REGEXPS.forEach(regexp => {
  5977. if (regexp.test(start) || regexp.test(end)) {
  5978. throw new Error(`['${start}', '${end}'] contains unusable interpolation symbol.`);
  5979. }
  5980. });
  5981. }
  5982. }
  5983. /**
  5984. * @license
  5985. * Copyright Google LLC All Rights Reserved.
  5986. *
  5987. * Use of this source code is governed by an MIT-style license that can be
  5988. * found in the LICENSE file at https://angular.io/license
  5989. */
  5990. class InterpolationConfig {
  5991. constructor(start, end) {
  5992. this.start = start;
  5993. this.end = end;
  5994. }
  5995. static fromArray(markers) {
  5996. if (!markers) {
  5997. return DEFAULT_INTERPOLATION_CONFIG;
  5998. }
  5999. assertInterpolationSymbols('interpolation', markers);
  6000. return new InterpolationConfig(markers[0], markers[1]);
  6001. }
  6002. }
  6003. const DEFAULT_INTERPOLATION_CONFIG = new InterpolationConfig('{{', '}}');
  6004. /**
  6005. * @license
  6006. * Copyright Google LLC All Rights Reserved.
  6007. *
  6008. * Use of this source code is governed by an MIT-style license that can be
  6009. * found in the LICENSE file at https://angular.io/license
  6010. */
  6011. /**
  6012. * A token representing the a reference to a static type.
  6013. *
  6014. * This token is unique for a filePath and name and can be used as a hash table key.
  6015. */
  6016. class StaticSymbol {
  6017. constructor(filePath, name, members) {
  6018. this.filePath = filePath;
  6019. this.name = name;
  6020. this.members = members;
  6021. }
  6022. assertNoMembers() {
  6023. if (this.members.length) {
  6024. throw new Error(`Illegal state: symbol without members expected, but got ${JSON.stringify(this)}.`);
  6025. }
  6026. }
  6027. }
  6028. /**
  6029. * A cache of static symbol used by the StaticReflector to return the same symbol for the
  6030. * same symbol values.
  6031. */
  6032. class StaticSymbolCache {
  6033. constructor() {
  6034. this.cache = new Map();
  6035. }
  6036. get(declarationFile, name, members) {
  6037. members = members || [];
  6038. const memberSuffix = members.length ? `.${members.join('.')}` : '';
  6039. const key = `"${declarationFile}".${name}${memberSuffix}`;
  6040. let result = this.cache.get(key);
  6041. if (!result) {
  6042. result = new StaticSymbol(declarationFile, name, members);
  6043. this.cache.set(key, result);
  6044. }
  6045. return result;
  6046. }
  6047. }
  6048. /**
  6049. * @license
  6050. * Copyright Google LLC All Rights Reserved.
  6051. *
  6052. * Use of this source code is governed by an MIT-style license that can be
  6053. * found in the LICENSE file at https://angular.io/license
  6054. */
  6055. // group 0: "[prop] or (event) or @trigger"
  6056. // group 1: "prop" from "[prop]"
  6057. // group 2: "event" from "(event)"
  6058. // group 3: "@trigger" from "@trigger"
  6059. const HOST_REG_EXP = /^(?:(?:\[([^\]]+)\])|(?:\(([^\)]+)\)))|(\@[-\w]+)$/;
  6060. function sanitizeIdentifier(name) {
  6061. return name.replace(/\W/g, '_');
  6062. }
  6063. let _anonymousTypeIndex = 0;
  6064. function identifierName(compileIdentifier) {
  6065. if (!compileIdentifier || !compileIdentifier.reference) {
  6066. return null;
  6067. }
  6068. const ref = compileIdentifier.reference;
  6069. if (ref instanceof StaticSymbol) {
  6070. return ref.name;
  6071. }
  6072. if (ref['__anonymousType']) {
  6073. return ref['__anonymousType'];
  6074. }
  6075. if (ref['__forward_ref__']) {
  6076. // We do not want to try to stringify a `forwardRef()` function because that would cause the
  6077. // inner function to be evaluated too early, defeating the whole point of the `forwardRef`.
  6078. return '__forward_ref__';
  6079. }
  6080. let identifier = stringify(ref);
  6081. if (identifier.indexOf('(') >= 0) {
  6082. // case: anonymous functions!
  6083. identifier = `anonymous_${_anonymousTypeIndex++}`;
  6084. ref['__anonymousType'] = identifier;
  6085. }
  6086. else {
  6087. identifier = sanitizeIdentifier(identifier);
  6088. }
  6089. return identifier;
  6090. }
  6091. function identifierModuleUrl(compileIdentifier) {
  6092. const ref = compileIdentifier.reference;
  6093. if (ref instanceof StaticSymbol) {
  6094. return ref.filePath;
  6095. }
  6096. // Runtime type
  6097. return `./${stringify(ref)}`;
  6098. }
  6099. function viewClassName(compType, embeddedTemplateIndex) {
  6100. return `View_${identifierName({ reference: compType })}_${embeddedTemplateIndex}`;
  6101. }
  6102. function rendererTypeName(compType) {
  6103. return `RenderType_${identifierName({ reference: compType })}`;
  6104. }
  6105. function hostViewClassName(compType) {
  6106. return `HostView_${identifierName({ reference: compType })}`;
  6107. }
  6108. function componentFactoryName(compType) {
  6109. return `${identifierName({ reference: compType })}NgFactory`;
  6110. }
  6111. var CompileSummaryKind;
  6112. (function (CompileSummaryKind) {
  6113. CompileSummaryKind[CompileSummaryKind["Pipe"] = 0] = "Pipe";
  6114. CompileSummaryKind[CompileSummaryKind["Directive"] = 1] = "Directive";
  6115. CompileSummaryKind[CompileSummaryKind["NgModule"] = 2] = "NgModule";
  6116. CompileSummaryKind[CompileSummaryKind["Injectable"] = 3] = "Injectable";
  6117. })(CompileSummaryKind || (CompileSummaryKind = {}));
  6118. function tokenName(token) {
  6119. return token.value != null ? sanitizeIdentifier(token.value) : identifierName(token.identifier);
  6120. }
  6121. function tokenReference(token) {
  6122. if (token.identifier != null) {
  6123. return token.identifier.reference;
  6124. }
  6125. else {
  6126. return token.value;
  6127. }
  6128. }
  6129. /**
  6130. * Metadata about a stylesheet
  6131. */
  6132. class CompileStylesheetMetadata {
  6133. constructor({ moduleUrl, styles, styleUrls } = {}) {
  6134. this.moduleUrl = moduleUrl || null;
  6135. this.styles = _normalizeArray(styles);
  6136. this.styleUrls = _normalizeArray(styleUrls);
  6137. }
  6138. }
  6139. /**
  6140. * Metadata regarding compilation of a template.
  6141. */
  6142. class CompileTemplateMetadata {
  6143. constructor({ encapsulation, template, templateUrl, htmlAst, styles, styleUrls, externalStylesheets, animations, ngContentSelectors, interpolation, isInline, preserveWhitespaces }) {
  6144. this.encapsulation = encapsulation;
  6145. this.template = template;
  6146. this.templateUrl = templateUrl;
  6147. this.htmlAst = htmlAst;
  6148. this.styles = _normalizeArray(styles);
  6149. this.styleUrls = _normalizeArray(styleUrls);
  6150. this.externalStylesheets = _normalizeArray(externalStylesheets);
  6151. this.animations = animations ? flatten(animations) : [];
  6152. this.ngContentSelectors = ngContentSelectors || [];
  6153. if (interpolation && interpolation.length != 2) {
  6154. throw new Error(`'interpolation' should have a start and an end symbol.`);
  6155. }
  6156. this.interpolation = interpolation;
  6157. this.isInline = isInline;
  6158. this.preserveWhitespaces = preserveWhitespaces;
  6159. }
  6160. toSummary() {
  6161. return {
  6162. ngContentSelectors: this.ngContentSelectors,
  6163. encapsulation: this.encapsulation,
  6164. styles: this.styles,
  6165. animations: this.animations
  6166. };
  6167. }
  6168. }
  6169. /**
  6170. * Metadata regarding compilation of a directive.
  6171. */
  6172. class CompileDirectiveMetadata {
  6173. constructor({ isHost, type, isComponent, selector, exportAs, changeDetection, inputs, outputs, hostListeners, hostProperties, hostAttributes, providers, viewProviders, queries, guards, viewQueries, entryComponents, template, componentViewType, rendererType, componentFactory }) {
  6174. this.isHost = !!isHost;
  6175. this.type = type;
  6176. this.isComponent = isComponent;
  6177. this.selector = selector;
  6178. this.exportAs = exportAs;
  6179. this.changeDetection = changeDetection;
  6180. this.inputs = inputs;
  6181. this.outputs = outputs;
  6182. this.hostListeners = hostListeners;
  6183. this.hostProperties = hostProperties;
  6184. this.hostAttributes = hostAttributes;
  6185. this.providers = _normalizeArray(providers);
  6186. this.viewProviders = _normalizeArray(viewProviders);
  6187. this.queries = _normalizeArray(queries);
  6188. this.guards = guards;
  6189. this.viewQueries = _normalizeArray(viewQueries);
  6190. this.entryComponents = _normalizeArray(entryComponents);
  6191. this.template = template;
  6192. this.componentViewType = componentViewType;
  6193. this.rendererType = rendererType;
  6194. this.componentFactory = componentFactory;
  6195. }
  6196. static create({ isHost, type, isComponent, selector, exportAs, changeDetection, inputs, outputs, host, providers, viewProviders, queries, guards, viewQueries, entryComponents, template, componentViewType, rendererType, componentFactory }) {
  6197. const hostListeners = {};
  6198. const hostProperties = {};
  6199. const hostAttributes = {};
  6200. if (host != null) {
  6201. Object.keys(host).forEach(key => {
  6202. const value = host[key];
  6203. const matches = key.match(HOST_REG_EXP);
  6204. if (matches === null) {
  6205. hostAttributes[key] = value;
  6206. }
  6207. else if (matches[1] != null) {
  6208. hostProperties[matches[1]] = value;
  6209. }
  6210. else if (matches[2] != null) {
  6211. hostListeners[matches[2]] = value;
  6212. }
  6213. });
  6214. }
  6215. const inputsMap = {};
  6216. if (inputs != null) {
  6217. inputs.forEach((bindConfig) => {
  6218. // canonical syntax: `dirProp: elProp`
  6219. // if there is no `:`, use dirProp = elProp
  6220. const parts = splitAtColon(bindConfig, [bindConfig, bindConfig]);
  6221. inputsMap[parts[0]] = parts[1];
  6222. });
  6223. }
  6224. const outputsMap = {};
  6225. if (outputs != null) {
  6226. outputs.forEach((bindConfig) => {
  6227. // canonical syntax: `dirProp: elProp`
  6228. // if there is no `:`, use dirProp = elProp
  6229. const parts = splitAtColon(bindConfig, [bindConfig, bindConfig]);
  6230. outputsMap[parts[0]] = parts[1];
  6231. });
  6232. }
  6233. return new CompileDirectiveMetadata({
  6234. isHost,
  6235. type,
  6236. isComponent: !!isComponent,
  6237. selector,
  6238. exportAs,
  6239. changeDetection,
  6240. inputs: inputsMap,
  6241. outputs: outputsMap,
  6242. hostListeners,
  6243. hostProperties,
  6244. hostAttributes,
  6245. providers,
  6246. viewProviders,
  6247. queries,
  6248. guards,
  6249. viewQueries,
  6250. entryComponents,
  6251. template,
  6252. componentViewType,
  6253. rendererType,
  6254. componentFactory,
  6255. });
  6256. }
  6257. toSummary() {
  6258. return {
  6259. summaryKind: CompileSummaryKind.Directive,
  6260. type: this.type,
  6261. isComponent: this.isComponent,
  6262. selector: this.selector,
  6263. exportAs: this.exportAs,
  6264. inputs: this.inputs,
  6265. outputs: this.outputs,
  6266. hostListeners: this.hostListeners,
  6267. hostProperties: this.hostProperties,
  6268. hostAttributes: this.hostAttributes,
  6269. providers: this.providers,
  6270. viewProviders: this.viewProviders,
  6271. queries: this.queries,
  6272. guards: this.guards,
  6273. viewQueries: this.viewQueries,
  6274. entryComponents: this.entryComponents,
  6275. changeDetection: this.changeDetection,
  6276. template: this.template && this.template.toSummary(),
  6277. componentViewType: this.componentViewType,
  6278. rendererType: this.rendererType,
  6279. componentFactory: this.componentFactory
  6280. };
  6281. }
  6282. }
  6283. class CompilePipeMetadata {
  6284. constructor({ type, name, pure }) {
  6285. this.type = type;
  6286. this.name = name;
  6287. this.pure = !!pure;
  6288. }
  6289. toSummary() {
  6290. return {
  6291. summaryKind: CompileSummaryKind.Pipe,
  6292. type: this.type,
  6293. name: this.name,
  6294. pure: this.pure
  6295. };
  6296. }
  6297. }
  6298. class CompileShallowModuleMetadata {
  6299. }
  6300. /**
  6301. * Metadata regarding compilation of a module.
  6302. */
  6303. class CompileNgModuleMetadata {
  6304. constructor({ type, providers, declaredDirectives, exportedDirectives, declaredPipes, exportedPipes, entryComponents, bootstrapComponents, importedModules, exportedModules, schemas, transitiveModule, id }) {
  6305. this.type = type || null;
  6306. this.declaredDirectives = _normalizeArray(declaredDirectives);
  6307. this.exportedDirectives = _normalizeArray(exportedDirectives);
  6308. this.declaredPipes = _normalizeArray(declaredPipes);
  6309. this.exportedPipes = _normalizeArray(exportedPipes);
  6310. this.providers = _normalizeArray(providers);
  6311. this.entryComponents = _normalizeArray(entryComponents);
  6312. this.bootstrapComponents = _normalizeArray(bootstrapComponents);
  6313. this.importedModules = _normalizeArray(importedModules);
  6314. this.exportedModules = _normalizeArray(exportedModules);
  6315. this.schemas = _normalizeArray(schemas);
  6316. this.id = id || null;
  6317. this.transitiveModule = transitiveModule || null;
  6318. }
  6319. toSummary() {
  6320. const module = this.transitiveModule;
  6321. return {
  6322. summaryKind: CompileSummaryKind.NgModule,
  6323. type: this.type,
  6324. entryComponents: module.entryComponents,
  6325. providers: module.providers,
  6326. modules: module.modules,
  6327. exportedDirectives: module.exportedDirectives,
  6328. exportedPipes: module.exportedPipes
  6329. };
  6330. }
  6331. }
  6332. class TransitiveCompileNgModuleMetadata {
  6333. constructor() {
  6334. this.directivesSet = new Set();
  6335. this.directives = [];
  6336. this.exportedDirectivesSet = new Set();
  6337. this.exportedDirectives = [];
  6338. this.pipesSet = new Set();
  6339. this.pipes = [];
  6340. this.exportedPipesSet = new Set();
  6341. this.exportedPipes = [];
  6342. this.modulesSet = new Set();
  6343. this.modules = [];
  6344. this.entryComponentsSet = new Set();
  6345. this.entryComponents = [];
  6346. this.providers = [];
  6347. }
  6348. addProvider(provider, module) {
  6349. this.providers.push({ provider: provider, module: module });
  6350. }
  6351. addDirective(id) {
  6352. if (!this.directivesSet.has(id.reference)) {
  6353. this.directivesSet.add(id.reference);
  6354. this.directives.push(id);
  6355. }
  6356. }
  6357. addExportedDirective(id) {
  6358. if (!this.exportedDirectivesSet.has(id.reference)) {
  6359. this.exportedDirectivesSet.add(id.reference);
  6360. this.exportedDirectives.push(id);
  6361. }
  6362. }
  6363. addPipe(id) {
  6364. if (!this.pipesSet.has(id.reference)) {
  6365. this.pipesSet.add(id.reference);
  6366. this.pipes.push(id);
  6367. }
  6368. }
  6369. addExportedPipe(id) {
  6370. if (!this.exportedPipesSet.has(id.reference)) {
  6371. this.exportedPipesSet.add(id.reference);
  6372. this.exportedPipes.push(id);
  6373. }
  6374. }
  6375. addModule(id) {
  6376. if (!this.modulesSet.has(id.reference)) {
  6377. this.modulesSet.add(id.reference);
  6378. this.modules.push(id);
  6379. }
  6380. }
  6381. addEntryComponent(ec) {
  6382. if (!this.entryComponentsSet.has(ec.componentType)) {
  6383. this.entryComponentsSet.add(ec.componentType);
  6384. this.entryComponents.push(ec);
  6385. }
  6386. }
  6387. }
  6388. function _normalizeArray(obj) {
  6389. return obj || [];
  6390. }
  6391. class ProviderMeta {
  6392. constructor(token, { useClass, useValue, useExisting, useFactory, deps, multi }) {
  6393. this.token = token;
  6394. this.useClass = useClass || null;
  6395. this.useValue = useValue;
  6396. this.useExisting = useExisting;
  6397. this.useFactory = useFactory || null;
  6398. this.dependencies = deps || null;
  6399. this.multi = !!multi;
  6400. }
  6401. }
  6402. function flatten(list) {
  6403. return list.reduce((flat, item) => {
  6404. const flatItem = Array.isArray(item) ? flatten(item) : item;
  6405. return flat.concat(flatItem);
  6406. }, []);
  6407. }
  6408. function jitSourceUrl(url) {
  6409. // Note: We need 3 "/" so that ng shows up as a separate domain
  6410. // in the chrome dev tools.
  6411. return url.replace(/(\w+:\/\/[\w:-]+)?(\/+)?/, 'ng:///');
  6412. }
  6413. function templateSourceUrl(ngModuleType, compMeta, templateMeta) {
  6414. let url;
  6415. if (templateMeta.isInline) {
  6416. if (compMeta.type.reference instanceof StaticSymbol) {
  6417. // Note: a .ts file might contain multiple components with inline templates,
  6418. // so we need to give them unique urls, as these will be used for sourcemaps.
  6419. url = `${compMeta.type.reference.filePath}.${compMeta.type.reference.name}.html`;
  6420. }
  6421. else {
  6422. url = `${identifierName(ngModuleType)}/${identifierName(compMeta.type)}.html`;
  6423. }
  6424. }
  6425. else {
  6426. url = templateMeta.templateUrl;
  6427. }
  6428. return compMeta.type.reference instanceof StaticSymbol ? url : jitSourceUrl(url);
  6429. }
  6430. function sharedStylesheetJitUrl(meta, id) {
  6431. const pathParts = meta.moduleUrl.split(/\/\\/g);
  6432. const baseName = pathParts[pathParts.length - 1];
  6433. return jitSourceUrl(`css/${id}${baseName}.ngstyle.js`);
  6434. }
  6435. function ngModuleJitUrl(moduleMeta) {
  6436. return jitSourceUrl(`${identifierName(moduleMeta.type)}/module.ngfactory.js`);
  6437. }
  6438. function templateJitUrl(ngModuleType, compMeta) {
  6439. return jitSourceUrl(`${identifierName(ngModuleType)}/${identifierName(compMeta.type)}.ngfactory.js`);
  6440. }
  6441. /**
  6442. * @license
  6443. * Copyright Google LLC All Rights Reserved.
  6444. *
  6445. * Use of this source code is governed by an MIT-style license that can be
  6446. * found in the LICENSE file at https://angular.io/license
  6447. */
  6448. /**
  6449. * In TypeScript, tagged template functions expect a "template object", which is an array of
  6450. * "cooked" strings plus a `raw` property that contains an array of "raw" strings. This is
  6451. * typically constructed with a function called `__makeTemplateObject(cooked, raw)`, but it may not
  6452. * be available in all environments.
  6453. *
  6454. * This is a JavaScript polyfill that uses __makeTemplateObject when it's available, but otherwise
  6455. * creates an inline helper with the same functionality.
  6456. *
  6457. * In the inline function, if `Object.defineProperty` is available we use that to attach the `raw`
  6458. * array.
  6459. */
  6460. const makeTemplateObjectPolyfill = '(this&&this.__makeTemplateObject||function(e,t){return Object.defineProperty?Object.defineProperty(e,"raw",{value:t}):e.raw=t,e})';
  6461. class AbstractJsEmitterVisitor extends AbstractEmitterVisitor {
  6462. constructor() {
  6463. super(false);
  6464. }
  6465. visitDeclareClassStmt(stmt, ctx) {
  6466. ctx.pushClass(stmt);
  6467. this._visitClassConstructor(stmt, ctx);
  6468. if (stmt.parent != null) {
  6469. ctx.print(stmt, `${stmt.name}.prototype = Object.create(`);
  6470. stmt.parent.visitExpression(this, ctx);
  6471. ctx.println(stmt, `.prototype);`);
  6472. }
  6473. stmt.getters.forEach((getter) => this._visitClassGetter(stmt, getter, ctx));
  6474. stmt.methods.forEach((method) => this._visitClassMethod(stmt, method, ctx));
  6475. ctx.popClass();
  6476. return null;
  6477. }
  6478. _visitClassConstructor(stmt, ctx) {
  6479. ctx.print(stmt, `function ${stmt.name}(`);
  6480. if (stmt.constructorMethod != null) {
  6481. this._visitParams(stmt.constructorMethod.params, ctx);
  6482. }
  6483. ctx.println(stmt, `) {`);
  6484. ctx.incIndent();
  6485. if (stmt.constructorMethod != null) {
  6486. if (stmt.constructorMethod.body.length > 0) {
  6487. ctx.println(stmt, `var self = this;`);
  6488. this.visitAllStatements(stmt.constructorMethod.body, ctx);
  6489. }
  6490. }
  6491. ctx.decIndent();
  6492. ctx.println(stmt, `}`);
  6493. }
  6494. _visitClassGetter(stmt, getter, ctx) {
  6495. ctx.println(stmt, `Object.defineProperty(${stmt.name}.prototype, '${getter.name}', { get: function() {`);
  6496. ctx.incIndent();
  6497. if (getter.body.length > 0) {
  6498. ctx.println(stmt, `var self = this;`);
  6499. this.visitAllStatements(getter.body, ctx);
  6500. }
  6501. ctx.decIndent();
  6502. ctx.println(stmt, `}});`);
  6503. }
  6504. _visitClassMethod(stmt, method, ctx) {
  6505. ctx.print(stmt, `${stmt.name}.prototype.${method.name} = function(`);
  6506. this._visitParams(method.params, ctx);
  6507. ctx.println(stmt, `) {`);
  6508. ctx.incIndent();
  6509. if (method.body.length > 0) {
  6510. ctx.println(stmt, `var self = this;`);
  6511. this.visitAllStatements(method.body, ctx);
  6512. }
  6513. ctx.decIndent();
  6514. ctx.println(stmt, `};`);
  6515. }
  6516. visitWrappedNodeExpr(ast, ctx) {
  6517. throw new Error('Cannot emit a WrappedNodeExpr in Javascript.');
  6518. }
  6519. visitReadVarExpr(ast, ctx) {
  6520. if (ast.builtin === BuiltinVar.This) {
  6521. ctx.print(ast, 'self');
  6522. }
  6523. else if (ast.builtin === BuiltinVar.Super) {
  6524. throw new Error(`'super' needs to be handled at a parent ast node, not at the variable level!`);
  6525. }
  6526. else {
  6527. super.visitReadVarExpr(ast, ctx);
  6528. }
  6529. return null;
  6530. }
  6531. visitDeclareVarStmt(stmt, ctx) {
  6532. ctx.print(stmt, `var ${stmt.name}`);
  6533. if (stmt.value) {
  6534. ctx.print(stmt, ' = ');
  6535. stmt.value.visitExpression(this, ctx);
  6536. }
  6537. ctx.println(stmt, `;`);
  6538. return null;
  6539. }
  6540. visitCastExpr(ast, ctx) {
  6541. ast.value.visitExpression(this, ctx);
  6542. return null;
  6543. }
  6544. visitInvokeFunctionExpr(expr, ctx) {
  6545. const fnExpr = expr.fn;
  6546. if (fnExpr instanceof ReadVarExpr && fnExpr.builtin === BuiltinVar.Super) {
  6547. ctx.currentClass.parent.visitExpression(this, ctx);
  6548. ctx.print(expr, `.call(this`);
  6549. if (expr.args.length > 0) {
  6550. ctx.print(expr, `, `);
  6551. this.visitAllExpressions(expr.args, ctx, ',');
  6552. }
  6553. ctx.print(expr, `)`);
  6554. }
  6555. else {
  6556. super.visitInvokeFunctionExpr(expr, ctx);
  6557. }
  6558. return null;
  6559. }
  6560. visitTaggedTemplateExpr(ast, ctx) {
  6561. // The following convoluted piece of code is effectively the downlevelled equivalent of
  6562. // ```
  6563. // tag`...`
  6564. // ```
  6565. // which is effectively like:
  6566. // ```
  6567. // tag(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  6568. // ```
  6569. const elements = ast.template.elements;
  6570. ast.tag.visitExpression(this, ctx);
  6571. ctx.print(ast, `(${makeTemplateObjectPolyfill}(`);
  6572. ctx.print(ast, `[${elements.map(part => escapeIdentifier(part.text, false)).join(', ')}], `);
  6573. ctx.print(ast, `[${elements.map(part => escapeIdentifier(part.rawText, false)).join(', ')}])`);
  6574. ast.template.expressions.forEach(expression => {
  6575. ctx.print(ast, ', ');
  6576. expression.visitExpression(this, ctx);
  6577. });
  6578. ctx.print(ast, ')');
  6579. return null;
  6580. }
  6581. visitFunctionExpr(ast, ctx) {
  6582. ctx.print(ast, `function${ast.name ? ' ' + ast.name : ''}(`);
  6583. this._visitParams(ast.params, ctx);
  6584. ctx.println(ast, `) {`);
  6585. ctx.incIndent();
  6586. this.visitAllStatements(ast.statements, ctx);
  6587. ctx.decIndent();
  6588. ctx.print(ast, `}`);
  6589. return null;
  6590. }
  6591. visitDeclareFunctionStmt(stmt, ctx) {
  6592. ctx.print(stmt, `function ${stmt.name}(`);
  6593. this._visitParams(stmt.params, ctx);
  6594. ctx.println(stmt, `) {`);
  6595. ctx.incIndent();
  6596. this.visitAllStatements(stmt.statements, ctx);
  6597. ctx.decIndent();
  6598. ctx.println(stmt, `}`);
  6599. return null;
  6600. }
  6601. visitTryCatchStmt(stmt, ctx) {
  6602. ctx.println(stmt, `try {`);
  6603. ctx.incIndent();
  6604. this.visitAllStatements(stmt.bodyStmts, ctx);
  6605. ctx.decIndent();
  6606. ctx.println(stmt, `} catch (${CATCH_ERROR_VAR$1.name}) {`);
  6607. ctx.incIndent();
  6608. const catchStmts = [CATCH_STACK_VAR$1.set(CATCH_ERROR_VAR$1.prop('stack')).toDeclStmt(null, [
  6609. StmtModifier.Final
  6610. ])].concat(stmt.catchStmts);
  6611. this.visitAllStatements(catchStmts, ctx);
  6612. ctx.decIndent();
  6613. ctx.println(stmt, `}`);
  6614. return null;
  6615. }
  6616. visitLocalizedString(ast, ctx) {
  6617. // The following convoluted piece of code is effectively the downlevelled equivalent of
  6618. // ```
  6619. // $localize `...`
  6620. // ```
  6621. // which is effectively like:
  6622. // ```
  6623. // $localize(__makeTemplateObject(cooked, raw), expression1, expression2, ...);
  6624. // ```
  6625. ctx.print(ast, `$localize(${makeTemplateObjectPolyfill}(`);
  6626. const parts = [ast.serializeI18nHead()];
  6627. for (let i = 1; i < ast.messageParts.length; i++) {
  6628. parts.push(ast.serializeI18nTemplatePart(i));
  6629. }
  6630. ctx.print(ast, `[${parts.map(part => escapeIdentifier(part.cooked, false)).join(', ')}], `);
  6631. ctx.print(ast, `[${parts.map(part => escapeIdentifier(part.raw, false)).join(', ')}])`);
  6632. ast.expressions.forEach(expression => {
  6633. ctx.print(ast, ', ');
  6634. expression.visitExpression(this, ctx);
  6635. });
  6636. ctx.print(ast, ')');
  6637. return null;
  6638. }
  6639. _visitParams(params, ctx) {
  6640. this.visitAllObjects(param => ctx.print(null, param.name), params, ctx, ',');
  6641. }
  6642. getBuiltinMethodName(method) {
  6643. let name;
  6644. switch (method) {
  6645. case BuiltinMethod.ConcatArray:
  6646. name = 'concat';
  6647. break;
  6648. case BuiltinMethod.SubscribeObservable:
  6649. name = 'subscribe';
  6650. break;
  6651. case BuiltinMethod.Bind:
  6652. name = 'bind';
  6653. break;
  6654. default:
  6655. throw new Error(`Unknown builtin method: ${method}`);
  6656. }
  6657. return name;
  6658. }
  6659. }
  6660. /**
  6661. * @license
  6662. * Copyright Google LLC All Rights Reserved.
  6663. *
  6664. * Use of this source code is governed by an MIT-style license that can be
  6665. * found in the LICENSE file at https://angular.io/license
  6666. */
  6667. /**
  6668. * The Trusted Types policy, or null if Trusted Types are not
  6669. * enabled/supported, or undefined if the policy has not been created yet.
  6670. */
  6671. let policy;
  6672. /**
  6673. * Returns the Trusted Types policy, or null if Trusted Types are not
  6674. * enabled/supported. The first call to this function will create the policy.
  6675. */
  6676. function getPolicy() {
  6677. if (policy === undefined) {
  6678. policy = null;
  6679. if (_global.trustedTypes) {
  6680. try {
  6681. policy =
  6682. _global.trustedTypes.createPolicy('angular#unsafe-jit', {
  6683. createScript: (s) => s,
  6684. });
  6685. }
  6686. catch (_a) {
  6687. // trustedTypes.createPolicy throws if called with a name that is
  6688. // already registered, even in report-only mode. Until the API changes,
  6689. // catch the error not to break the applications functionally. In such
  6690. // cases, the code will fall back to using strings.
  6691. }
  6692. }
  6693. }
  6694. return policy;
  6695. }
  6696. /**
  6697. * Unsafely promote a string to a TrustedScript, falling back to strings when
  6698. * Trusted Types are not available.
  6699. * @security In particular, it must be assured that the provided string will
  6700. * never cause an XSS vulnerability if used in a context that will be
  6701. * interpreted and executed as a script by a browser, e.g. when calling eval.
  6702. */
  6703. function trustedScriptFromString(script) {
  6704. var _a;
  6705. return ((_a = getPolicy()) === null || _a === void 0 ? void 0 : _a.createScript(script)) || script;
  6706. }
  6707. /**
  6708. * Unsafely call the Function constructor with the given string arguments.
  6709. * @security This is a security-sensitive function; any use of this function
  6710. * must go through security review. In particular, it must be assured that it
  6711. * is only called from the JIT compiler, as use in other code can lead to XSS
  6712. * vulnerabilities.
  6713. */
  6714. function newTrustedFunctionForJIT(...args) {
  6715. if (!_global.trustedTypes) {
  6716. // In environments that don't support Trusted Types, fall back to the most
  6717. // straightforward implementation:
  6718. return new Function(...args);
  6719. }
  6720. // Chrome currently does not support passing TrustedScript to the Function
  6721. // constructor. The following implements the workaround proposed on the page
  6722. // below, where the Chromium bug is also referenced:
  6723. // https://github.com/w3c/webappsec-trusted-types/wiki/Trusted-Types-for-function-constructor
  6724. const fnArgs = args.slice(0, -1).join(',');
  6725. const fnBody = args[args.length - 1];
  6726. const body = `(function anonymous(${fnArgs}
  6727. ) { ${fnBody}
  6728. })`;
  6729. // Using eval directly confuses the compiler and prevents this module from
  6730. // being stripped out of JS binaries even if not used. The global['eval']
  6731. // indirection fixes that.
  6732. const fn = _global['eval'](trustedScriptFromString(body));
  6733. if (fn.bind === undefined) {
  6734. // Workaround for a browser bug that only exists in Chrome 83, where passing
  6735. // a TrustedScript to eval just returns the TrustedScript back without
  6736. // evaluating it. In that case, fall back to the most straightforward
  6737. // implementation:
  6738. return new Function(...args);
  6739. }
  6740. // To completely mimic the behavior of calling "new Function", two more
  6741. // things need to happen:
  6742. // 1. Stringifying the resulting function should return its source code
  6743. fn.toString = () => body;
  6744. // 2. When calling the resulting function, `this` should refer to `global`
  6745. return fn.bind(_global);
  6746. // When Trusted Types support in Function constructors is widely available,
  6747. // the implementation of this function can be simplified to:
  6748. // return new Function(...args.map(a => trustedScriptFromString(a)));
  6749. }
  6750. /**
  6751. * @license
  6752. * Copyright Google LLC All Rights Reserved.
  6753. *
  6754. * Use of this source code is governed by an MIT-style license that can be
  6755. * found in the LICENSE file at https://angular.io/license
  6756. */
  6757. /**
  6758. * A helper class to manage the evaluation of JIT generated code.
  6759. */
  6760. class JitEvaluator {
  6761. /**
  6762. *
  6763. * @param sourceUrl The URL of the generated code.
  6764. * @param statements An array of Angular statement AST nodes to be evaluated.
  6765. * @param reflector A helper used when converting the statements to executable code.
  6766. * @param createSourceMaps If true then create a source-map for the generated code and include it
  6767. * inline as a source-map comment.
  6768. * @returns A map of all the variables in the generated code.
  6769. */
  6770. evaluateStatements(sourceUrl, statements, reflector, createSourceMaps) {
  6771. const converter = new JitEmitterVisitor(reflector);
  6772. const ctx = EmitterVisitorContext.createRoot();
  6773. // Ensure generated code is in strict mode
  6774. if (statements.length > 0 && !isUseStrictStatement(statements[0])) {
  6775. statements = [
  6776. literal('use strict').toStmt(),
  6777. ...statements,
  6778. ];
  6779. }
  6780. converter.visitAllStatements(statements, ctx);
  6781. converter.createReturnStmt(ctx);
  6782. return this.evaluateCode(sourceUrl, ctx, converter.getArgs(), createSourceMaps);
  6783. }
  6784. /**
  6785. * Evaluate a piece of JIT generated code.
  6786. * @param sourceUrl The URL of this generated code.
  6787. * @param ctx A context object that contains an AST of the code to be evaluated.
  6788. * @param vars A map containing the names and values of variables that the evaluated code might
  6789. * reference.
  6790. * @param createSourceMap If true then create a source-map for the generated code and include it
  6791. * inline as a source-map comment.
  6792. * @returns The result of evaluating the code.
  6793. */
  6794. evaluateCode(sourceUrl, ctx, vars, createSourceMap) {
  6795. let fnBody = `"use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl}`;
  6796. const fnArgNames = [];
  6797. const fnArgValues = [];
  6798. for (const argName in vars) {
  6799. fnArgValues.push(vars[argName]);
  6800. fnArgNames.push(argName);
  6801. }
  6802. if (createSourceMap) {
  6803. // using `new Function(...)` generates a header, 1 line of no arguments, 2 lines otherwise
  6804. // E.g. ```
  6805. // function anonymous(a,b,c
  6806. // /**/) { ... }```
  6807. // We don't want to hard code this fact, so we auto detect it via an empty function first.
  6808. const emptyFn = newTrustedFunctionForJIT(...fnArgNames.concat('return null;')).toString();
  6809. const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1;
  6810. fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()}`;
  6811. }
  6812. const fn = newTrustedFunctionForJIT(...fnArgNames.concat(fnBody));
  6813. return this.executeFunction(fn, fnArgValues);
  6814. }
  6815. /**
  6816. * Execute a JIT generated function by calling it.
  6817. *
  6818. * This method can be overridden in tests to capture the functions that are generated
  6819. * by this `JitEvaluator` class.
  6820. *
  6821. * @param fn A function to execute.
  6822. * @param args The arguments to pass to the function being executed.
  6823. * @returns The return value of the executed function.
  6824. */
  6825. executeFunction(fn, args) {
  6826. return fn(...args);
  6827. }
  6828. }
  6829. /**
  6830. * An Angular AST visitor that converts AST nodes into executable JavaScript code.
  6831. */
  6832. class JitEmitterVisitor extends AbstractJsEmitterVisitor {
  6833. constructor(reflector) {
  6834. super();
  6835. this.reflector = reflector;
  6836. this._evalArgNames = [];
  6837. this._evalArgValues = [];
  6838. this._evalExportedVars = [];
  6839. }
  6840. createReturnStmt(ctx) {
  6841. const stmt = new ReturnStatement(new LiteralMapExpr(this._evalExportedVars.map(resultVar => new LiteralMapEntry(resultVar, variable(resultVar), false))));
  6842. stmt.visitStatement(this, ctx);
  6843. }
  6844. getArgs() {
  6845. const result = {};
  6846. for (let i = 0; i < this._evalArgNames.length; i++) {
  6847. result[this._evalArgNames[i]] = this._evalArgValues[i];
  6848. }
  6849. return result;
  6850. }
  6851. visitExternalExpr(ast, ctx) {
  6852. this._emitReferenceToExternal(ast, this.reflector.resolveExternalReference(ast.value), ctx);
  6853. return null;
  6854. }
  6855. visitWrappedNodeExpr(ast, ctx) {
  6856. this._emitReferenceToExternal(ast, ast.node, ctx);
  6857. return null;
  6858. }
  6859. visitDeclareVarStmt(stmt, ctx) {
  6860. if (stmt.hasModifier(StmtModifier.Exported)) {
  6861. this._evalExportedVars.push(stmt.name);
  6862. }
  6863. return super.visitDeclareVarStmt(stmt, ctx);
  6864. }
  6865. visitDeclareFunctionStmt(stmt, ctx) {
  6866. if (stmt.hasModifier(StmtModifier.Exported)) {
  6867. this._evalExportedVars.push(stmt.name);
  6868. }
  6869. return super.visitDeclareFunctionStmt(stmt, ctx);
  6870. }
  6871. visitDeclareClassStmt(stmt, ctx) {
  6872. if (stmt.hasModifier(StmtModifier.Exported)) {
  6873. this._evalExportedVars.push(stmt.name);
  6874. }
  6875. return super.visitDeclareClassStmt(stmt, ctx);
  6876. }
  6877. _emitReferenceToExternal(ast, value, ctx) {
  6878. let id = this._evalArgValues.indexOf(value);
  6879. if (id === -1) {
  6880. id = this._evalArgValues.length;
  6881. this._evalArgValues.push(value);
  6882. const name = identifierName({ reference: value }) || 'val';
  6883. this._evalArgNames.push(`jit_${name}_${id}`);
  6884. }
  6885. ctx.print(ast, this._evalArgNames[id]);
  6886. }
  6887. }
  6888. function isUseStrictStatement(statement) {
  6889. return statement.isEquivalent(literal('use strict').toStmt());
  6890. }
  6891. /**
  6892. * @license
  6893. * Copyright Google LLC All Rights Reserved.
  6894. *
  6895. * Use of this source code is governed by an MIT-style license that can be
  6896. * found in the LICENSE file at https://angular.io/license
  6897. */
  6898. const $EOF = 0;
  6899. const $BSPACE = 8;
  6900. const $TAB = 9;
  6901. const $LF = 10;
  6902. const $VTAB = 11;
  6903. const $FF = 12;
  6904. const $CR = 13;
  6905. const $SPACE = 32;
  6906. const $BANG = 33;
  6907. const $DQ = 34;
  6908. const $HASH = 35;
  6909. const $$ = 36;
  6910. const $PERCENT = 37;
  6911. const $AMPERSAND = 38;
  6912. const $SQ = 39;
  6913. const $LPAREN = 40;
  6914. const $RPAREN = 41;
  6915. const $STAR = 42;
  6916. const $PLUS = 43;
  6917. const $COMMA = 44;
  6918. const $MINUS = 45;
  6919. const $PERIOD = 46;
  6920. const $SLASH = 47;
  6921. const $COLON = 58;
  6922. const $SEMICOLON = 59;
  6923. const $LT = 60;
  6924. const $EQ = 61;
  6925. const $GT = 62;
  6926. const $QUESTION = 63;
  6927. const $0 = 48;
  6928. const $7 = 55;
  6929. const $9 = 57;
  6930. const $A = 65;
  6931. const $E = 69;
  6932. const $F = 70;
  6933. const $X = 88;
  6934. const $Z = 90;
  6935. const $LBRACKET = 91;
  6936. const $BACKSLASH = 92;
  6937. const $RBRACKET = 93;
  6938. const $CARET = 94;
  6939. const $_ = 95;
  6940. const $a = 97;
  6941. const $b = 98;
  6942. const $e = 101;
  6943. const $f = 102;
  6944. const $n = 110;
  6945. const $r = 114;
  6946. const $t = 116;
  6947. const $u = 117;
  6948. const $v = 118;
  6949. const $x = 120;
  6950. const $z = 122;
  6951. const $LBRACE = 123;
  6952. const $BAR = 124;
  6953. const $RBRACE = 125;
  6954. const $NBSP = 160;
  6955. const $PIPE = 124;
  6956. const $TILDA = 126;
  6957. const $AT = 64;
  6958. const $BT = 96;
  6959. function isWhitespace(code) {
  6960. return (code >= $TAB && code <= $SPACE) || (code == $NBSP);
  6961. }
  6962. function isDigit(code) {
  6963. return $0 <= code && code <= $9;
  6964. }
  6965. function isAsciiLetter(code) {
  6966. return code >= $a && code <= $z || code >= $A && code <= $Z;
  6967. }
  6968. function isAsciiHexDigit(code) {
  6969. return code >= $a && code <= $f || code >= $A && code <= $F || isDigit(code);
  6970. }
  6971. function isNewLine(code) {
  6972. return code === $LF || code === $CR;
  6973. }
  6974. function isOctalDigit(code) {
  6975. return $0 <= code && code <= $7;
  6976. }
  6977. /**
  6978. * @license
  6979. * Copyright Google LLC All Rights Reserved.
  6980. *
  6981. * Use of this source code is governed by an MIT-style license that can be
  6982. * found in the LICENSE file at https://angular.io/license
  6983. */
  6984. class ParseLocation {
  6985. constructor(file, offset, line, col) {
  6986. this.file = file;
  6987. this.offset = offset;
  6988. this.line = line;
  6989. this.col = col;
  6990. }
  6991. toString() {
  6992. return this.offset != null ? `${this.file.url}@${this.line}:${this.col}` : this.file.url;
  6993. }
  6994. moveBy(delta) {
  6995. const source = this.file.content;
  6996. const len = source.length;
  6997. let offset = this.offset;
  6998. let line = this.line;
  6999. let col = this.col;
  7000. while (offset > 0 && delta < 0) {
  7001. offset--;
  7002. delta++;
  7003. const ch = source.charCodeAt(offset);
  7004. if (ch == $LF) {
  7005. line--;
  7006. const priorLine = source.substr(0, offset - 1).lastIndexOf(String.fromCharCode($LF));
  7007. col = priorLine > 0 ? offset - priorLine : offset;
  7008. }
  7009. else {
  7010. col--;
  7011. }
  7012. }
  7013. while (offset < len && delta > 0) {
  7014. const ch = source.charCodeAt(offset);
  7015. offset++;
  7016. delta--;
  7017. if (ch == $LF) {
  7018. line++;
  7019. col = 0;
  7020. }
  7021. else {
  7022. col++;
  7023. }
  7024. }
  7025. return new ParseLocation(this.file, offset, line, col);
  7026. }
  7027. // Return the source around the location
  7028. // Up to `maxChars` or `maxLines` on each side of the location
  7029. getContext(maxChars, maxLines) {
  7030. const content = this.file.content;
  7031. let startOffset = this.offset;
  7032. if (startOffset != null) {
  7033. if (startOffset > content.length - 1) {
  7034. startOffset = content.length - 1;
  7035. }
  7036. let endOffset = startOffset;
  7037. let ctxChars = 0;
  7038. let ctxLines = 0;
  7039. while (ctxChars < maxChars && startOffset > 0) {
  7040. startOffset--;
  7041. ctxChars++;
  7042. if (content[startOffset] == '\n') {
  7043. if (++ctxLines == maxLines) {
  7044. break;
  7045. }
  7046. }
  7047. }
  7048. ctxChars = 0;
  7049. ctxLines = 0;
  7050. while (ctxChars < maxChars && endOffset < content.length - 1) {
  7051. endOffset++;
  7052. ctxChars++;
  7053. if (content[endOffset] == '\n') {
  7054. if (++ctxLines == maxLines) {
  7055. break;
  7056. }
  7057. }
  7058. }
  7059. return {
  7060. before: content.substring(startOffset, this.offset),
  7061. after: content.substring(this.offset, endOffset + 1),
  7062. };
  7063. }
  7064. return null;
  7065. }
  7066. }
  7067. class ParseSourceFile {
  7068. constructor(content, url) {
  7069. this.content = content;
  7070. this.url = url;
  7071. }
  7072. }
  7073. class ParseSourceSpan {
  7074. /**
  7075. * Create an object that holds information about spans of tokens/nodes captured during
  7076. * lexing/parsing of text.
  7077. *
  7078. * @param start
  7079. * The location of the start of the span (having skipped leading trivia).
  7080. * Skipping leading trivia makes source-spans more "user friendly", since things like HTML
  7081. * elements will appear to begin at the start of the opening tag, rather than at the start of any
  7082. * leading trivia, which could include newlines.
  7083. *
  7084. * @param end
  7085. * The location of the end of the span.
  7086. *
  7087. * @param fullStart
  7088. * The start of the token without skipping the leading trivia.
  7089. * This is used by tooling that splits tokens further, such as extracting Angular interpolations
  7090. * from text tokens. Such tooling creates new source-spans relative to the original token's
  7091. * source-span. If leading trivia characters have been skipped then the new source-spans may be
  7092. * incorrectly offset.
  7093. *
  7094. * @param details
  7095. * Additional information (such as identifier names) that should be associated with the span.
  7096. */
  7097. constructor(start, end, fullStart = start, details = null) {
  7098. this.start = start;
  7099. this.end = end;
  7100. this.fullStart = fullStart;
  7101. this.details = details;
  7102. }
  7103. toString() {
  7104. return this.start.file.content.substring(this.start.offset, this.end.offset);
  7105. }
  7106. }
  7107. var ParseErrorLevel;
  7108. (function (ParseErrorLevel) {
  7109. ParseErrorLevel[ParseErrorLevel["WARNING"] = 0] = "WARNING";
  7110. ParseErrorLevel[ParseErrorLevel["ERROR"] = 1] = "ERROR";
  7111. })(ParseErrorLevel || (ParseErrorLevel = {}));
  7112. class ParseError {
  7113. constructor(span, msg, level = ParseErrorLevel.ERROR) {
  7114. this.span = span;
  7115. this.msg = msg;
  7116. this.level = level;
  7117. }
  7118. contextualMessage() {
  7119. const ctx = this.span.start.getContext(100, 3);
  7120. return ctx ? `${this.msg} ("${ctx.before}[${ParseErrorLevel[this.level]} ->]${ctx.after}")` :
  7121. this.msg;
  7122. }
  7123. toString() {
  7124. const details = this.span.details ? `, ${this.span.details}` : '';
  7125. return `${this.contextualMessage()}: ${this.span.start}${details}`;
  7126. }
  7127. }
  7128. function typeSourceSpan(kind, type) {
  7129. const moduleUrl = identifierModuleUrl(type);
  7130. const sourceFileName = moduleUrl != null ? `in ${kind} ${identifierName(type)} in ${moduleUrl}` :
  7131. `in ${kind} ${identifierName(type)}`;
  7132. const sourceFile = new ParseSourceFile('', sourceFileName);
  7133. return new ParseSourceSpan(new ParseLocation(sourceFile, -1, -1, -1), new ParseLocation(sourceFile, -1, -1, -1));
  7134. }
  7135. /**
  7136. * Generates Source Span object for a given R3 Type for JIT mode.
  7137. *
  7138. * @param kind Component or Directive.
  7139. * @param typeName name of the Component or Directive.
  7140. * @param sourceUrl reference to Component or Directive source.
  7141. * @returns instance of ParseSourceSpan that represent a given Component or Directive.
  7142. */
  7143. function r3JitTypeSourceSpan(kind, typeName, sourceUrl) {
  7144. const sourceFileName = `in ${kind} ${typeName} in ${sourceUrl}`;
  7145. const sourceFile = new ParseSourceFile('', sourceFileName);
  7146. return new ParseSourceSpan(new ParseLocation(sourceFile, -1, -1, -1), new ParseLocation(sourceFile, -1, -1, -1));
  7147. }
  7148. /**
  7149. * @license
  7150. * Copyright Google LLC All Rights Reserved.
  7151. *
  7152. * Use of this source code is governed by an MIT-style license that can be
  7153. * found in the LICENSE file at https://angular.io/license
  7154. */
  7155. function compileInjector(meta) {
  7156. const definitionMap = new DefinitionMap();
  7157. if (meta.providers !== null) {
  7158. definitionMap.set('providers', meta.providers);
  7159. }
  7160. if (meta.imports.length > 0) {
  7161. definitionMap.set('imports', literalArr(meta.imports));
  7162. }
  7163. const expression = importExpr(Identifiers.defineInjector).callFn([definitionMap.toLiteralMap()], undefined, true);
  7164. const type = createInjectorType(meta);
  7165. return { expression, type, statements: [] };
  7166. }
  7167. function createInjectorType(meta) {
  7168. return new ExpressionType(importExpr(Identifiers.InjectorDeclaration, [new ExpressionType(meta.type.type)]));
  7169. }
  7170. /**
  7171. * @license
  7172. * Copyright Google LLC All Rights Reserved.
  7173. *
  7174. * Use of this source code is governed by an MIT-style license that can be
  7175. * found in the LICENSE file at https://angular.io/license
  7176. */
  7177. /**
  7178. * Implementation of `CompileReflector` which resolves references to @angular/core
  7179. * symbols at runtime, according to a consumer-provided mapping.
  7180. *
  7181. * Only supports `resolveExternalReference`, all other methods throw.
  7182. */
  7183. class R3JitReflector {
  7184. constructor(context) {
  7185. this.context = context;
  7186. }
  7187. resolveExternalReference(ref) {
  7188. // This reflector only handles @angular/core imports.
  7189. if (ref.moduleName !== '@angular/core') {
  7190. throw new Error(`Cannot resolve external reference to ${ref.moduleName}, only references to @angular/core are supported.`);
  7191. }
  7192. if (!this.context.hasOwnProperty(ref.name)) {
  7193. throw new Error(`No value provided for @angular/core symbol '${ref.name}'.`);
  7194. }
  7195. return this.context[ref.name];
  7196. }
  7197. parameters(typeOrFunc) {
  7198. throw new Error('Not implemented.');
  7199. }
  7200. annotations(typeOrFunc) {
  7201. throw new Error('Not implemented.');
  7202. }
  7203. shallowAnnotations(typeOrFunc) {
  7204. throw new Error('Not implemented.');
  7205. }
  7206. tryAnnotations(typeOrFunc) {
  7207. throw new Error('Not implemented.');
  7208. }
  7209. propMetadata(typeOrFunc) {
  7210. throw new Error('Not implemented.');
  7211. }
  7212. hasLifecycleHook(type, lcProperty) {
  7213. throw new Error('Not implemented.');
  7214. }
  7215. guards(typeOrFunc) {
  7216. throw new Error('Not implemented.');
  7217. }
  7218. componentModuleUrl(type, cmpMetadata) {
  7219. throw new Error('Not implemented.');
  7220. }
  7221. }
  7222. /**
  7223. * @license
  7224. * Copyright Google LLC All Rights Reserved.
  7225. *
  7226. * Use of this source code is governed by an MIT-style license that can be
  7227. * found in the LICENSE file at https://angular.io/license
  7228. */
  7229. /**
  7230. * Construct an `R3NgModuleDef` for the given `R3NgModuleMetadata`.
  7231. */
  7232. function compileNgModule(meta) {
  7233. const { internalType, bootstrap, declarations, imports, exports, schemas, containsForwardDecls, emitInline, id } = meta;
  7234. const statements = [];
  7235. const definitionMap = new DefinitionMap();
  7236. definitionMap.set('type', internalType);
  7237. if (bootstrap.length > 0) {
  7238. definitionMap.set('bootstrap', refsToArray(bootstrap, containsForwardDecls));
  7239. }
  7240. // If requested to emit scope information inline, pass the `declarations`, `imports` and `exports`
  7241. // to the `ɵɵdefineNgModule()` call. The JIT compilation uses this.
  7242. if (emitInline) {
  7243. if (declarations.length > 0) {
  7244. definitionMap.set('declarations', refsToArray(declarations, containsForwardDecls));
  7245. }
  7246. if (imports.length > 0) {
  7247. definitionMap.set('imports', refsToArray(imports, containsForwardDecls));
  7248. }
  7249. if (exports.length > 0) {
  7250. definitionMap.set('exports', refsToArray(exports, containsForwardDecls));
  7251. }
  7252. }
  7253. // If not emitting inline, the scope information is not passed into `ɵɵdefineNgModule` as it would
  7254. // prevent tree-shaking of the declarations, imports and exports references.
  7255. else {
  7256. const setNgModuleScopeCall = generateSetNgModuleScopeCall(meta);
  7257. if (setNgModuleScopeCall !== null) {
  7258. statements.push(setNgModuleScopeCall);
  7259. }
  7260. }
  7261. if (schemas !== null && schemas.length > 0) {
  7262. definitionMap.set('schemas', literalArr(schemas.map(ref => ref.value)));
  7263. }
  7264. if (id !== null) {
  7265. definitionMap.set('id', id);
  7266. }
  7267. const expression = importExpr(Identifiers.defineNgModule).callFn([definitionMap.toLiteralMap()], undefined, true);
  7268. const type = createNgModuleType(meta);
  7269. return { expression, type, statements };
  7270. }
  7271. /**
  7272. * This function is used in JIT mode to generate the call to `ɵɵdefineNgModule()` from a call to
  7273. * `ɵɵngDeclareNgModule()`.
  7274. */
  7275. function compileNgModuleDeclarationExpression(meta) {
  7276. const definitionMap = new DefinitionMap();
  7277. definitionMap.set('type', new WrappedNodeExpr(meta.type));
  7278. if (meta.bootstrap !== undefined) {
  7279. definitionMap.set('bootstrap', new WrappedNodeExpr(meta.bootstrap));
  7280. }
  7281. if (meta.declarations !== undefined) {
  7282. definitionMap.set('declarations', new WrappedNodeExpr(meta.declarations));
  7283. }
  7284. if (meta.imports !== undefined) {
  7285. definitionMap.set('imports', new WrappedNodeExpr(meta.imports));
  7286. }
  7287. if (meta.exports !== undefined) {
  7288. definitionMap.set('exports', new WrappedNodeExpr(meta.exports));
  7289. }
  7290. if (meta.schemas !== undefined) {
  7291. definitionMap.set('schemas', new WrappedNodeExpr(meta.schemas));
  7292. }
  7293. if (meta.id !== undefined) {
  7294. definitionMap.set('id', new WrappedNodeExpr(meta.id));
  7295. }
  7296. return importExpr(Identifiers.defineNgModule).callFn([definitionMap.toLiteralMap()]);
  7297. }
  7298. function createNgModuleType({ type: moduleType, declarations, imports, exports }) {
  7299. return new ExpressionType(importExpr(Identifiers.NgModuleDeclaration, [
  7300. new ExpressionType(moduleType.type), tupleTypeOf(declarations), tupleTypeOf(imports),
  7301. tupleTypeOf(exports)
  7302. ]));
  7303. }
  7304. /**
  7305. * Generates a function call to `ɵɵsetNgModuleScope` with all necessary information so that the
  7306. * transitive module scope can be computed during runtime in JIT mode. This call is marked pure
  7307. * such that the references to declarations, imports and exports may be elided causing these
  7308. * symbols to become tree-shakeable.
  7309. */
  7310. function generateSetNgModuleScopeCall(meta) {
  7311. const { adjacentType: moduleType, declarations, imports, exports, containsForwardDecls } = meta;
  7312. const scopeMap = new DefinitionMap();
  7313. if (declarations.length > 0) {
  7314. scopeMap.set('declarations', refsToArray(declarations, containsForwardDecls));
  7315. }
  7316. if (imports.length > 0) {
  7317. scopeMap.set('imports', refsToArray(imports, containsForwardDecls));
  7318. }
  7319. if (exports.length > 0) {
  7320. scopeMap.set('exports', refsToArray(exports, containsForwardDecls));
  7321. }
  7322. if (Object.keys(scopeMap.values).length === 0) {
  7323. return null;
  7324. }
  7325. // setNgModuleScope(...)
  7326. const fnCall = new InvokeFunctionExpr(
  7327. /* fn */ importExpr(Identifiers.setNgModuleScope),
  7328. /* args */ [moduleType, scopeMap.toLiteralMap()]);
  7329. // (ngJitMode guard) && setNgModuleScope(...)
  7330. const guardedCall = jitOnlyGuardedExpression(fnCall);
  7331. // function() { (ngJitMode guard) && setNgModuleScope(...); }
  7332. const iife = new FunctionExpr(
  7333. /* params */ [],
  7334. /* statements */ [guardedCall.toStmt()]);
  7335. // (function() { (ngJitMode guard) && setNgModuleScope(...); })()
  7336. const iifeCall = new InvokeFunctionExpr(
  7337. /* fn */ iife,
  7338. /* args */ []);
  7339. return iifeCall.toStmt();
  7340. }
  7341. function tupleTypeOf(exp) {
  7342. const types = exp.map(ref => typeofExpr(ref.type));
  7343. return exp.length > 0 ? expressionType(literalArr(types)) : NONE_TYPE;
  7344. }
  7345. /**
  7346. * @license
  7347. * Copyright Google LLC All Rights Reserved.
  7348. *
  7349. * Use of this source code is governed by an MIT-style license that can be
  7350. * found in the LICENSE file at https://angular.io/license
  7351. */
  7352. function compilePipeFromMetadata(metadata) {
  7353. const definitionMapValues = [];
  7354. // e.g. `name: 'myPipe'`
  7355. definitionMapValues.push({ key: 'name', value: literal(metadata.pipeName), quoted: false });
  7356. // e.g. `type: MyPipe`
  7357. definitionMapValues.push({ key: 'type', value: metadata.type.value, quoted: false });
  7358. // e.g. `pure: true`
  7359. definitionMapValues.push({ key: 'pure', value: literal(metadata.pure), quoted: false });
  7360. const expression = importExpr(Identifiers.definePipe).callFn([literalMap(definitionMapValues)], undefined, true);
  7361. const type = createPipeType(metadata);
  7362. return { expression, type, statements: [] };
  7363. }
  7364. function createPipeType(metadata) {
  7365. return new ExpressionType(importExpr(Identifiers.PipeDeclaration, [
  7366. typeWithParameters(metadata.type.type, metadata.typeArgumentCount),
  7367. new ExpressionType(new LiteralExpr(metadata.pipeName)),
  7368. ]));
  7369. }
  7370. /**
  7371. * @license
  7372. * Copyright Google LLC All Rights Reserved.
  7373. *
  7374. * Use of this source code is governed by an MIT-style license that can be
  7375. * found in the LICENSE file at https://angular.io/license
  7376. */
  7377. class ParserError {
  7378. constructor(message, input, errLocation, ctxLocation) {
  7379. this.input = input;
  7380. this.errLocation = errLocation;
  7381. this.ctxLocation = ctxLocation;
  7382. this.message = `Parser Error: ${message} ${errLocation} [${input}] in ${ctxLocation}`;
  7383. }
  7384. }
  7385. class ParseSpan {
  7386. constructor(start, end) {
  7387. this.start = start;
  7388. this.end = end;
  7389. }
  7390. toAbsolute(absoluteOffset) {
  7391. return new AbsoluteSourceSpan(absoluteOffset + this.start, absoluteOffset + this.end);
  7392. }
  7393. }
  7394. class AST {
  7395. constructor(span,
  7396. /**
  7397. * Absolute location of the expression AST in a source code file.
  7398. */
  7399. sourceSpan) {
  7400. this.span = span;
  7401. this.sourceSpan = sourceSpan;
  7402. }
  7403. visit(visitor, context = null) {
  7404. return null;
  7405. }
  7406. toString() {
  7407. return 'AST';
  7408. }
  7409. }
  7410. class ASTWithName extends AST {
  7411. constructor(span, sourceSpan, nameSpan) {
  7412. super(span, sourceSpan);
  7413. this.nameSpan = nameSpan;
  7414. }
  7415. }
  7416. /**
  7417. * Represents a quoted expression of the form:
  7418. *
  7419. * quote = prefix `:` uninterpretedExpression
  7420. * prefix = identifier
  7421. * uninterpretedExpression = arbitrary string
  7422. *
  7423. * A quoted expression is meant to be pre-processed by an AST transformer that
  7424. * converts it into another AST that no longer contains quoted expressions.
  7425. * It is meant to allow third-party developers to extend Angular template
  7426. * expression language. The `uninterpretedExpression` part of the quote is
  7427. * therefore not interpreted by the Angular's own expression parser.
  7428. */
  7429. class Quote extends AST {
  7430. constructor(span, sourceSpan, prefix, uninterpretedExpression, location) {
  7431. super(span, sourceSpan);
  7432. this.prefix = prefix;
  7433. this.uninterpretedExpression = uninterpretedExpression;
  7434. this.location = location;
  7435. }
  7436. visit(visitor, context = null) {
  7437. return visitor.visitQuote(this, context);
  7438. }
  7439. toString() {
  7440. return 'Quote';
  7441. }
  7442. }
  7443. class EmptyExpr extends AST {
  7444. visit(visitor, context = null) {
  7445. // do nothing
  7446. }
  7447. }
  7448. class ImplicitReceiver extends AST {
  7449. visit(visitor, context = null) {
  7450. return visitor.visitImplicitReceiver(this, context);
  7451. }
  7452. }
  7453. /**
  7454. * Receiver when something is accessed through `this` (e.g. `this.foo`). Note that this class
  7455. * inherits from `ImplicitReceiver`, because accessing something through `this` is treated the
  7456. * same as accessing it implicitly inside of an Angular template (e.g. `[attr.title]="this.title"`
  7457. * is the same as `[attr.title]="title"`.). Inheriting allows for the `this` accesses to be treated
  7458. * the same as implicit ones, except for a couple of exceptions like `$event` and `$any`.
  7459. * TODO: we should find a way for this class not to extend from `ImplicitReceiver` in the future.
  7460. */
  7461. class ThisReceiver extends ImplicitReceiver {
  7462. visit(visitor, context = null) {
  7463. var _a;
  7464. return (_a = visitor.visitThisReceiver) === null || _a === void 0 ? void 0 : _a.call(visitor, this, context);
  7465. }
  7466. }
  7467. /**
  7468. * Multiple expressions separated by a semicolon.
  7469. */
  7470. class Chain extends AST {
  7471. constructor(span, sourceSpan, expressions) {
  7472. super(span, sourceSpan);
  7473. this.expressions = expressions;
  7474. }
  7475. visit(visitor, context = null) {
  7476. return visitor.visitChain(this, context);
  7477. }
  7478. }
  7479. class Conditional extends AST {
  7480. constructor(span, sourceSpan, condition, trueExp, falseExp) {
  7481. super(span, sourceSpan);
  7482. this.condition = condition;
  7483. this.trueExp = trueExp;
  7484. this.falseExp = falseExp;
  7485. }
  7486. visit(visitor, context = null) {
  7487. return visitor.visitConditional(this, context);
  7488. }
  7489. }
  7490. class PropertyRead extends ASTWithName {
  7491. constructor(span, sourceSpan, nameSpan, receiver, name) {
  7492. super(span, sourceSpan, nameSpan);
  7493. this.receiver = receiver;
  7494. this.name = name;
  7495. }
  7496. visit(visitor, context = null) {
  7497. return visitor.visitPropertyRead(this, context);
  7498. }
  7499. }
  7500. class PropertyWrite extends ASTWithName {
  7501. constructor(span, sourceSpan, nameSpan, receiver, name, value) {
  7502. super(span, sourceSpan, nameSpan);
  7503. this.receiver = receiver;
  7504. this.name = name;
  7505. this.value = value;
  7506. }
  7507. visit(visitor, context = null) {
  7508. return visitor.visitPropertyWrite(this, context);
  7509. }
  7510. }
  7511. class SafePropertyRead extends ASTWithName {
  7512. constructor(span, sourceSpan, nameSpan, receiver, name) {
  7513. super(span, sourceSpan, nameSpan);
  7514. this.receiver = receiver;
  7515. this.name = name;
  7516. }
  7517. visit(visitor, context = null) {
  7518. return visitor.visitSafePropertyRead(this, context);
  7519. }
  7520. }
  7521. class KeyedRead extends AST {
  7522. constructor(span, sourceSpan, obj, key) {
  7523. super(span, sourceSpan);
  7524. this.obj = obj;
  7525. this.key = key;
  7526. }
  7527. visit(visitor, context = null) {
  7528. return visitor.visitKeyedRead(this, context);
  7529. }
  7530. }
  7531. class KeyedWrite extends AST {
  7532. constructor(span, sourceSpan, obj, key, value) {
  7533. super(span, sourceSpan);
  7534. this.obj = obj;
  7535. this.key = key;
  7536. this.value = value;
  7537. }
  7538. visit(visitor, context = null) {
  7539. return visitor.visitKeyedWrite(this, context);
  7540. }
  7541. }
  7542. class BindingPipe extends ASTWithName {
  7543. constructor(span, sourceSpan, exp, name, args, nameSpan) {
  7544. super(span, sourceSpan, nameSpan);
  7545. this.exp = exp;
  7546. this.name = name;
  7547. this.args = args;
  7548. }
  7549. visit(visitor, context = null) {
  7550. return visitor.visitPipe(this, context);
  7551. }
  7552. }
  7553. class LiteralPrimitive extends AST {
  7554. constructor(span, sourceSpan, value) {
  7555. super(span, sourceSpan);
  7556. this.value = value;
  7557. }
  7558. visit(visitor, context = null) {
  7559. return visitor.visitLiteralPrimitive(this, context);
  7560. }
  7561. }
  7562. class LiteralArray extends AST {
  7563. constructor(span, sourceSpan, expressions) {
  7564. super(span, sourceSpan);
  7565. this.expressions = expressions;
  7566. }
  7567. visit(visitor, context = null) {
  7568. return visitor.visitLiteralArray(this, context);
  7569. }
  7570. }
  7571. class LiteralMap extends AST {
  7572. constructor(span, sourceSpan, keys, values) {
  7573. super(span, sourceSpan);
  7574. this.keys = keys;
  7575. this.values = values;
  7576. }
  7577. visit(visitor, context = null) {
  7578. return visitor.visitLiteralMap(this, context);
  7579. }
  7580. }
  7581. class Interpolation extends AST {
  7582. constructor(span, sourceSpan, strings, expressions) {
  7583. super(span, sourceSpan);
  7584. this.strings = strings;
  7585. this.expressions = expressions;
  7586. }
  7587. visit(visitor, context = null) {
  7588. return visitor.visitInterpolation(this, context);
  7589. }
  7590. }
  7591. class Binary extends AST {
  7592. constructor(span, sourceSpan, operation, left, right) {
  7593. super(span, sourceSpan);
  7594. this.operation = operation;
  7595. this.left = left;
  7596. this.right = right;
  7597. }
  7598. visit(visitor, context = null) {
  7599. return visitor.visitBinary(this, context);
  7600. }
  7601. }
  7602. /**
  7603. * For backwards compatibility reasons, `Unary` inherits from `Binary` and mimics the binary AST
  7604. * node that was originally used. This inheritance relation can be deleted in some future major,
  7605. * after consumers have been given a chance to fully support Unary.
  7606. */
  7607. class Unary extends Binary {
  7608. /**
  7609. * During the deprecation period this constructor is private, to avoid consumers from creating
  7610. * a `Unary` with the fallback properties for `Binary`.
  7611. */
  7612. constructor(span, sourceSpan, operator, expr, binaryOp, binaryLeft, binaryRight) {
  7613. super(span, sourceSpan, binaryOp, binaryLeft, binaryRight);
  7614. this.operator = operator;
  7615. this.expr = expr;
  7616. }
  7617. /**
  7618. * Creates a unary minus expression "-x", represented as `Binary` using "0 - x".
  7619. */
  7620. static createMinus(span, sourceSpan, expr) {
  7621. return new Unary(span, sourceSpan, '-', expr, '-', new LiteralPrimitive(span, sourceSpan, 0), expr);
  7622. }
  7623. /**
  7624. * Creates a unary plus expression "+x", represented as `Binary` using "x - 0".
  7625. */
  7626. static createPlus(span, sourceSpan, expr) {
  7627. return new Unary(span, sourceSpan, '+', expr, '-', expr, new LiteralPrimitive(span, sourceSpan, 0));
  7628. }
  7629. visit(visitor, context = null) {
  7630. if (visitor.visitUnary !== undefined) {
  7631. return visitor.visitUnary(this, context);
  7632. }
  7633. return visitor.visitBinary(this, context);
  7634. }
  7635. }
  7636. class PrefixNot extends AST {
  7637. constructor(span, sourceSpan, expression) {
  7638. super(span, sourceSpan);
  7639. this.expression = expression;
  7640. }
  7641. visit(visitor, context = null) {
  7642. return visitor.visitPrefixNot(this, context);
  7643. }
  7644. }
  7645. class NonNullAssert extends AST {
  7646. constructor(span, sourceSpan, expression) {
  7647. super(span, sourceSpan);
  7648. this.expression = expression;
  7649. }
  7650. visit(visitor, context = null) {
  7651. return visitor.visitNonNullAssert(this, context);
  7652. }
  7653. }
  7654. class MethodCall extends ASTWithName {
  7655. constructor(span, sourceSpan, nameSpan, receiver, name, args, argumentSpan) {
  7656. super(span, sourceSpan, nameSpan);
  7657. this.receiver = receiver;
  7658. this.name = name;
  7659. this.args = args;
  7660. this.argumentSpan = argumentSpan;
  7661. }
  7662. visit(visitor, context = null) {
  7663. return visitor.visitMethodCall(this, context);
  7664. }
  7665. }
  7666. class SafeMethodCall extends ASTWithName {
  7667. constructor(span, sourceSpan, nameSpan, receiver, name, args, argumentSpan) {
  7668. super(span, sourceSpan, nameSpan);
  7669. this.receiver = receiver;
  7670. this.name = name;
  7671. this.args = args;
  7672. this.argumentSpan = argumentSpan;
  7673. }
  7674. visit(visitor, context = null) {
  7675. return visitor.visitSafeMethodCall(this, context);
  7676. }
  7677. }
  7678. class FunctionCall extends AST {
  7679. constructor(span, sourceSpan, target, args) {
  7680. super(span, sourceSpan);
  7681. this.target = target;
  7682. this.args = args;
  7683. }
  7684. visit(visitor, context = null) {
  7685. return visitor.visitFunctionCall(this, context);
  7686. }
  7687. }
  7688. /**
  7689. * Records the absolute position of a text span in a source file, where `start` and `end` are the
  7690. * starting and ending byte offsets, respectively, of the text span in a source file.
  7691. */
  7692. class AbsoluteSourceSpan {
  7693. constructor(start, end) {
  7694. this.start = start;
  7695. this.end = end;
  7696. }
  7697. }
  7698. class ASTWithSource extends AST {
  7699. constructor(ast, source, location, absoluteOffset, errors) {
  7700. super(new ParseSpan(0, source === null ? 0 : source.length), new AbsoluteSourceSpan(absoluteOffset, source === null ? absoluteOffset : absoluteOffset + source.length));
  7701. this.ast = ast;
  7702. this.source = source;
  7703. this.location = location;
  7704. this.errors = errors;
  7705. }
  7706. visit(visitor, context = null) {
  7707. if (visitor.visitASTWithSource) {
  7708. return visitor.visitASTWithSource(this, context);
  7709. }
  7710. return this.ast.visit(visitor, context);
  7711. }
  7712. toString() {
  7713. return `${this.source} in ${this.location}`;
  7714. }
  7715. }
  7716. class VariableBinding {
  7717. /**
  7718. * @param sourceSpan entire span of the binding.
  7719. * @param key name of the LHS along with its span.
  7720. * @param value optional value for the RHS along with its span.
  7721. */
  7722. constructor(sourceSpan, key, value) {
  7723. this.sourceSpan = sourceSpan;
  7724. this.key = key;
  7725. this.value = value;
  7726. }
  7727. }
  7728. class ExpressionBinding {
  7729. /**
  7730. * @param sourceSpan entire span of the binding.
  7731. * @param key binding name, like ngForOf, ngForTrackBy, ngIf, along with its
  7732. * span. Note that the length of the span may not be the same as
  7733. * `key.source.length`. For example,
  7734. * 1. key.source = ngFor, key.span is for "ngFor"
  7735. * 2. key.source = ngForOf, key.span is for "of"
  7736. * 3. key.source = ngForTrackBy, key.span is for "trackBy"
  7737. * @param value optional expression for the RHS.
  7738. */
  7739. constructor(sourceSpan, key, value) {
  7740. this.sourceSpan = sourceSpan;
  7741. this.key = key;
  7742. this.value = value;
  7743. }
  7744. }
  7745. class RecursiveAstVisitor$1 {
  7746. visit(ast, context) {
  7747. // The default implementation just visits every node.
  7748. // Classes that extend RecursiveAstVisitor should override this function
  7749. // to selectively visit the specified node.
  7750. ast.visit(this, context);
  7751. }
  7752. visitUnary(ast, context) {
  7753. this.visit(ast.expr, context);
  7754. }
  7755. visitBinary(ast, context) {
  7756. this.visit(ast.left, context);
  7757. this.visit(ast.right, context);
  7758. }
  7759. visitChain(ast, context) {
  7760. this.visitAll(ast.expressions, context);
  7761. }
  7762. visitConditional(ast, context) {
  7763. this.visit(ast.condition, context);
  7764. this.visit(ast.trueExp, context);
  7765. this.visit(ast.falseExp, context);
  7766. }
  7767. visitPipe(ast, context) {
  7768. this.visit(ast.exp, context);
  7769. this.visitAll(ast.args, context);
  7770. }
  7771. visitFunctionCall(ast, context) {
  7772. if (ast.target) {
  7773. this.visit(ast.target, context);
  7774. }
  7775. this.visitAll(ast.args, context);
  7776. }
  7777. visitImplicitReceiver(ast, context) { }
  7778. visitThisReceiver(ast, context) { }
  7779. visitInterpolation(ast, context) {
  7780. this.visitAll(ast.expressions, context);
  7781. }
  7782. visitKeyedRead(ast, context) {
  7783. this.visit(ast.obj, context);
  7784. this.visit(ast.key, context);
  7785. }
  7786. visitKeyedWrite(ast, context) {
  7787. this.visit(ast.obj, context);
  7788. this.visit(ast.key, context);
  7789. this.visit(ast.value, context);
  7790. }
  7791. visitLiteralArray(ast, context) {
  7792. this.visitAll(ast.expressions, context);
  7793. }
  7794. visitLiteralMap(ast, context) {
  7795. this.visitAll(ast.values, context);
  7796. }
  7797. visitLiteralPrimitive(ast, context) { }
  7798. visitMethodCall(ast, context) {
  7799. this.visit(ast.receiver, context);
  7800. this.visitAll(ast.args, context);
  7801. }
  7802. visitPrefixNot(ast, context) {
  7803. this.visit(ast.expression, context);
  7804. }
  7805. visitNonNullAssert(ast, context) {
  7806. this.visit(ast.expression, context);
  7807. }
  7808. visitPropertyRead(ast, context) {
  7809. this.visit(ast.receiver, context);
  7810. }
  7811. visitPropertyWrite(ast, context) {
  7812. this.visit(ast.receiver, context);
  7813. this.visit(ast.value, context);
  7814. }
  7815. visitSafePropertyRead(ast, context) {
  7816. this.visit(ast.receiver, context);
  7817. }
  7818. visitSafeMethodCall(ast, context) {
  7819. this.visit(ast.receiver, context);
  7820. this.visitAll(ast.args, context);
  7821. }
  7822. visitQuote(ast, context) { }
  7823. // This is not part of the AstVisitor interface, just a helper method
  7824. visitAll(asts, context) {
  7825. for (const ast of asts) {
  7826. this.visit(ast, context);
  7827. }
  7828. }
  7829. }
  7830. class AstTransformer$1 {
  7831. visitImplicitReceiver(ast, context) {
  7832. return ast;
  7833. }
  7834. visitThisReceiver(ast, context) {
  7835. return ast;
  7836. }
  7837. visitInterpolation(ast, context) {
  7838. return new Interpolation(ast.span, ast.sourceSpan, ast.strings, this.visitAll(ast.expressions));
  7839. }
  7840. visitLiteralPrimitive(ast, context) {
  7841. return new LiteralPrimitive(ast.span, ast.sourceSpan, ast.value);
  7842. }
  7843. visitPropertyRead(ast, context) {
  7844. return new PropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name);
  7845. }
  7846. visitPropertyWrite(ast, context) {
  7847. return new PropertyWrite(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name, ast.value.visit(this));
  7848. }
  7849. visitSafePropertyRead(ast, context) {
  7850. return new SafePropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name);
  7851. }
  7852. visitMethodCall(ast, context) {
  7853. return new MethodCall(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name, this.visitAll(ast.args), ast.argumentSpan);
  7854. }
  7855. visitSafeMethodCall(ast, context) {
  7856. return new SafeMethodCall(ast.span, ast.sourceSpan, ast.nameSpan, ast.receiver.visit(this), ast.name, this.visitAll(ast.args), ast.argumentSpan);
  7857. }
  7858. visitFunctionCall(ast, context) {
  7859. return new FunctionCall(ast.span, ast.sourceSpan, ast.target.visit(this), this.visitAll(ast.args));
  7860. }
  7861. visitLiteralArray(ast, context) {
  7862. return new LiteralArray(ast.span, ast.sourceSpan, this.visitAll(ast.expressions));
  7863. }
  7864. visitLiteralMap(ast, context) {
  7865. return new LiteralMap(ast.span, ast.sourceSpan, ast.keys, this.visitAll(ast.values));
  7866. }
  7867. visitUnary(ast, context) {
  7868. switch (ast.operator) {
  7869. case '+':
  7870. return Unary.createPlus(ast.span, ast.sourceSpan, ast.expr.visit(this));
  7871. case '-':
  7872. return Unary.createMinus(ast.span, ast.sourceSpan, ast.expr.visit(this));
  7873. default:
  7874. throw new Error(`Unknown unary operator ${ast.operator}`);
  7875. }
  7876. }
  7877. visitBinary(ast, context) {
  7878. return new Binary(ast.span, ast.sourceSpan, ast.operation, ast.left.visit(this), ast.right.visit(this));
  7879. }
  7880. visitPrefixNot(ast, context) {
  7881. return new PrefixNot(ast.span, ast.sourceSpan, ast.expression.visit(this));
  7882. }
  7883. visitNonNullAssert(ast, context) {
  7884. return new NonNullAssert(ast.span, ast.sourceSpan, ast.expression.visit(this));
  7885. }
  7886. visitConditional(ast, context) {
  7887. return new Conditional(ast.span, ast.sourceSpan, ast.condition.visit(this), ast.trueExp.visit(this), ast.falseExp.visit(this));
  7888. }
  7889. visitPipe(ast, context) {
  7890. return new BindingPipe(ast.span, ast.sourceSpan, ast.exp.visit(this), ast.name, this.visitAll(ast.args), ast.nameSpan);
  7891. }
  7892. visitKeyedRead(ast, context) {
  7893. return new KeyedRead(ast.span, ast.sourceSpan, ast.obj.visit(this), ast.key.visit(this));
  7894. }
  7895. visitKeyedWrite(ast, context) {
  7896. return new KeyedWrite(ast.span, ast.sourceSpan, ast.obj.visit(this), ast.key.visit(this), ast.value.visit(this));
  7897. }
  7898. visitAll(asts) {
  7899. const res = [];
  7900. for (let i = 0; i < asts.length; ++i) {
  7901. res[i] = asts[i].visit(this);
  7902. }
  7903. return res;
  7904. }
  7905. visitChain(ast, context) {
  7906. return new Chain(ast.span, ast.sourceSpan, this.visitAll(ast.expressions));
  7907. }
  7908. visitQuote(ast, context) {
  7909. return new Quote(ast.span, ast.sourceSpan, ast.prefix, ast.uninterpretedExpression, ast.location);
  7910. }
  7911. }
  7912. // A transformer that only creates new nodes if the transformer makes a change or
  7913. // a change is made a child node.
  7914. class AstMemoryEfficientTransformer {
  7915. visitImplicitReceiver(ast, context) {
  7916. return ast;
  7917. }
  7918. visitThisReceiver(ast, context) {
  7919. return ast;
  7920. }
  7921. visitInterpolation(ast, context) {
  7922. const expressions = this.visitAll(ast.expressions);
  7923. if (expressions !== ast.expressions)
  7924. return new Interpolation(ast.span, ast.sourceSpan, ast.strings, expressions);
  7925. return ast;
  7926. }
  7927. visitLiteralPrimitive(ast, context) {
  7928. return ast;
  7929. }
  7930. visitPropertyRead(ast, context) {
  7931. const receiver = ast.receiver.visit(this);
  7932. if (receiver !== ast.receiver) {
  7933. return new PropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name);
  7934. }
  7935. return ast;
  7936. }
  7937. visitPropertyWrite(ast, context) {
  7938. const receiver = ast.receiver.visit(this);
  7939. const value = ast.value.visit(this);
  7940. if (receiver !== ast.receiver || value !== ast.value) {
  7941. return new PropertyWrite(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name, value);
  7942. }
  7943. return ast;
  7944. }
  7945. visitSafePropertyRead(ast, context) {
  7946. const receiver = ast.receiver.visit(this);
  7947. if (receiver !== ast.receiver) {
  7948. return new SafePropertyRead(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name);
  7949. }
  7950. return ast;
  7951. }
  7952. visitMethodCall(ast, context) {
  7953. const receiver = ast.receiver.visit(this);
  7954. const args = this.visitAll(ast.args);
  7955. if (receiver !== ast.receiver || args !== ast.args) {
  7956. return new MethodCall(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name, args, ast.argumentSpan);
  7957. }
  7958. return ast;
  7959. }
  7960. visitSafeMethodCall(ast, context) {
  7961. const receiver = ast.receiver.visit(this);
  7962. const args = this.visitAll(ast.args);
  7963. if (receiver !== ast.receiver || args !== ast.args) {
  7964. return new SafeMethodCall(ast.span, ast.sourceSpan, ast.nameSpan, receiver, ast.name, args, ast.argumentSpan);
  7965. }
  7966. return ast;
  7967. }
  7968. visitFunctionCall(ast, context) {
  7969. const target = ast.target && ast.target.visit(this);
  7970. const args = this.visitAll(ast.args);
  7971. if (target !== ast.target || args !== ast.args) {
  7972. return new FunctionCall(ast.span, ast.sourceSpan, target, args);
  7973. }
  7974. return ast;
  7975. }
  7976. visitLiteralArray(ast, context) {
  7977. const expressions = this.visitAll(ast.expressions);
  7978. if (expressions !== ast.expressions) {
  7979. return new LiteralArray(ast.span, ast.sourceSpan, expressions);
  7980. }
  7981. return ast;
  7982. }
  7983. visitLiteralMap(ast, context) {
  7984. const values = this.visitAll(ast.values);
  7985. if (values !== ast.values) {
  7986. return new LiteralMap(ast.span, ast.sourceSpan, ast.keys, values);
  7987. }
  7988. return ast;
  7989. }
  7990. visitUnary(ast, context) {
  7991. const expr = ast.expr.visit(this);
  7992. if (expr !== ast.expr) {
  7993. switch (ast.operator) {
  7994. case '+':
  7995. return Unary.createPlus(ast.span, ast.sourceSpan, expr);
  7996. case '-':
  7997. return Unary.createMinus(ast.span, ast.sourceSpan, expr);
  7998. default:
  7999. throw new Error(`Unknown unary operator ${ast.operator}`);
  8000. }
  8001. }
  8002. return ast;
  8003. }
  8004. visitBinary(ast, context) {
  8005. const left = ast.left.visit(this);
  8006. const right = ast.right.visit(this);
  8007. if (left !== ast.left || right !== ast.right) {
  8008. return new Binary(ast.span, ast.sourceSpan, ast.operation, left, right);
  8009. }
  8010. return ast;
  8011. }
  8012. visitPrefixNot(ast, context) {
  8013. const expression = ast.expression.visit(this);
  8014. if (expression !== ast.expression) {
  8015. return new PrefixNot(ast.span, ast.sourceSpan, expression);
  8016. }
  8017. return ast;
  8018. }
  8019. visitNonNullAssert(ast, context) {
  8020. const expression = ast.expression.visit(this);
  8021. if (expression !== ast.expression) {
  8022. return new NonNullAssert(ast.span, ast.sourceSpan, expression);
  8023. }
  8024. return ast;
  8025. }
  8026. visitConditional(ast, context) {
  8027. const condition = ast.condition.visit(this);
  8028. const trueExp = ast.trueExp.visit(this);
  8029. const falseExp = ast.falseExp.visit(this);
  8030. if (condition !== ast.condition || trueExp !== ast.trueExp || falseExp !== ast.falseExp) {
  8031. return new Conditional(ast.span, ast.sourceSpan, condition, trueExp, falseExp);
  8032. }
  8033. return ast;
  8034. }
  8035. visitPipe(ast, context) {
  8036. const exp = ast.exp.visit(this);
  8037. const args = this.visitAll(ast.args);
  8038. if (exp !== ast.exp || args !== ast.args) {
  8039. return new BindingPipe(ast.span, ast.sourceSpan, exp, ast.name, args, ast.nameSpan);
  8040. }
  8041. return ast;
  8042. }
  8043. visitKeyedRead(ast, context) {
  8044. const obj = ast.obj.visit(this);
  8045. const key = ast.key.visit(this);
  8046. if (obj !== ast.obj || key !== ast.key) {
  8047. return new KeyedRead(ast.span, ast.sourceSpan, obj, key);
  8048. }
  8049. return ast;
  8050. }
  8051. visitKeyedWrite(ast, context) {
  8052. const obj = ast.obj.visit(this);
  8053. const key = ast.key.visit(this);
  8054. const value = ast.value.visit(this);
  8055. if (obj !== ast.obj || key !== ast.key || value !== ast.value) {
  8056. return new KeyedWrite(ast.span, ast.sourceSpan, obj, key, value);
  8057. }
  8058. return ast;
  8059. }
  8060. visitAll(asts) {
  8061. const res = [];
  8062. let modified = false;
  8063. for (let i = 0; i < asts.length; ++i) {
  8064. const original = asts[i];
  8065. const value = original.visit(this);
  8066. res[i] = value;
  8067. modified = modified || value !== original;
  8068. }
  8069. return modified ? res : asts;
  8070. }
  8071. visitChain(ast, context) {
  8072. const expressions = this.visitAll(ast.expressions);
  8073. if (expressions !== ast.expressions) {
  8074. return new Chain(ast.span, ast.sourceSpan, expressions);
  8075. }
  8076. return ast;
  8077. }
  8078. visitQuote(ast, context) {
  8079. return ast;
  8080. }
  8081. }
  8082. // Bindings
  8083. class ParsedProperty {
  8084. constructor(name, expression, type,
  8085. // TODO(FW-2095): `keySpan` should really be required but allows `undefined` so VE does
  8086. // not need to be updated. Make `keySpan` required when VE is removed.
  8087. sourceSpan, keySpan, valueSpan) {
  8088. this.name = name;
  8089. this.expression = expression;
  8090. this.type = type;
  8091. this.sourceSpan = sourceSpan;
  8092. this.keySpan = keySpan;
  8093. this.valueSpan = valueSpan;
  8094. this.isLiteral = this.type === ParsedPropertyType.LITERAL_ATTR;
  8095. this.isAnimation = this.type === ParsedPropertyType.ANIMATION;
  8096. }
  8097. }
  8098. var ParsedPropertyType;
  8099. (function (ParsedPropertyType) {
  8100. ParsedPropertyType[ParsedPropertyType["DEFAULT"] = 0] = "DEFAULT";
  8101. ParsedPropertyType[ParsedPropertyType["LITERAL_ATTR"] = 1] = "LITERAL_ATTR";
  8102. ParsedPropertyType[ParsedPropertyType["ANIMATION"] = 2] = "ANIMATION";
  8103. })(ParsedPropertyType || (ParsedPropertyType = {}));
  8104. class ParsedEvent {
  8105. // Regular events have a target
  8106. // Animation events have a phase
  8107. constructor(name, targetOrPhase, type, handler, sourceSpan,
  8108. // TODO(FW-2095): keySpan should be required but was made optional to avoid changing VE
  8109. handlerSpan, keySpan) {
  8110. this.name = name;
  8111. this.targetOrPhase = targetOrPhase;
  8112. this.type = type;
  8113. this.handler = handler;
  8114. this.sourceSpan = sourceSpan;
  8115. this.handlerSpan = handlerSpan;
  8116. this.keySpan = keySpan;
  8117. }
  8118. }
  8119. /**
  8120. * ParsedVariable represents a variable declaration in a microsyntax expression.
  8121. */
  8122. class ParsedVariable {
  8123. constructor(name, value, sourceSpan, keySpan, valueSpan) {
  8124. this.name = name;
  8125. this.value = value;
  8126. this.sourceSpan = sourceSpan;
  8127. this.keySpan = keySpan;
  8128. this.valueSpan = valueSpan;
  8129. }
  8130. }
  8131. class BoundElementProperty {
  8132. constructor(name, type, securityContext, value, unit, sourceSpan, keySpan, valueSpan) {
  8133. this.name = name;
  8134. this.type = type;
  8135. this.securityContext = securityContext;
  8136. this.value = value;
  8137. this.unit = unit;
  8138. this.sourceSpan = sourceSpan;
  8139. this.keySpan = keySpan;
  8140. this.valueSpan = valueSpan;
  8141. }
  8142. }
  8143. /**
  8144. * @license
  8145. * Copyright Google LLC All Rights Reserved.
  8146. *
  8147. * Use of this source code is governed by an MIT-style license that can be
  8148. * found in the LICENSE file at https://angular.io/license
  8149. */
  8150. const CORE$1 = '@angular/core';
  8151. class Identifiers$1 {
  8152. }
  8153. Identifiers$1.ANALYZE_FOR_ENTRY_COMPONENTS = {
  8154. name: 'ANALYZE_FOR_ENTRY_COMPONENTS',
  8155. moduleName: CORE$1,
  8156. };
  8157. Identifiers$1.ElementRef = { name: 'ElementRef', moduleName: CORE$1 };
  8158. Identifiers$1.NgModuleRef = { name: 'NgModuleRef', moduleName: CORE$1 };
  8159. Identifiers$1.ViewContainerRef = { name: 'ViewContainerRef', moduleName: CORE$1 };
  8160. Identifiers$1.ChangeDetectorRef = {
  8161. name: 'ChangeDetectorRef',
  8162. moduleName: CORE$1,
  8163. };
  8164. Identifiers$1.QueryList = { name: 'QueryList', moduleName: CORE$1 };
  8165. Identifiers$1.TemplateRef = { name: 'TemplateRef', moduleName: CORE$1 };
  8166. Identifiers$1.Renderer2 = { name: 'Renderer2', moduleName: CORE$1 };
  8167. Identifiers$1.CodegenComponentFactoryResolver = {
  8168. name: 'ɵCodegenComponentFactoryResolver',
  8169. moduleName: CORE$1,
  8170. };
  8171. Identifiers$1.ComponentFactoryResolver = {
  8172. name: 'ComponentFactoryResolver',
  8173. moduleName: CORE$1,
  8174. };
  8175. Identifiers$1.ComponentFactory = { name: 'ComponentFactory', moduleName: CORE$1 };
  8176. Identifiers$1.ComponentRef = { name: 'ComponentRef', moduleName: CORE$1 };
  8177. Identifiers$1.NgModuleFactory = { name: 'NgModuleFactory', moduleName: CORE$1 };
  8178. Identifiers$1.createModuleFactory = {
  8179. name: 'ɵcmf',
  8180. moduleName: CORE$1,
  8181. };
  8182. Identifiers$1.moduleDef = {
  8183. name: 'ɵmod',
  8184. moduleName: CORE$1,
  8185. };
  8186. Identifiers$1.moduleProviderDef = {
  8187. name: 'ɵmpd',
  8188. moduleName: CORE$1,
  8189. };
  8190. Identifiers$1.RegisterModuleFactoryFn = {
  8191. name: 'ɵregisterModuleFactory',
  8192. moduleName: CORE$1,
  8193. };
  8194. Identifiers$1.inject = { name: 'ɵɵinject', moduleName: CORE$1 };
  8195. Identifiers$1.directiveInject = { name: 'ɵɵdirectiveInject', moduleName: CORE$1 };
  8196. Identifiers$1.INJECTOR = { name: 'INJECTOR', moduleName: CORE$1 };
  8197. Identifiers$1.Injector = { name: 'Injector', moduleName: CORE$1 };
  8198. Identifiers$1.ViewEncapsulation = {
  8199. name: 'ViewEncapsulation',
  8200. moduleName: CORE$1,
  8201. };
  8202. Identifiers$1.ChangeDetectionStrategy = {
  8203. name: 'ChangeDetectionStrategy',
  8204. moduleName: CORE$1,
  8205. };
  8206. Identifiers$1.SecurityContext = {
  8207. name: 'SecurityContext',
  8208. moduleName: CORE$1,
  8209. };
  8210. Identifiers$1.LOCALE_ID = { name: 'LOCALE_ID', moduleName: CORE$1 };
  8211. Identifiers$1.TRANSLATIONS_FORMAT = {
  8212. name: 'TRANSLATIONS_FORMAT',
  8213. moduleName: CORE$1,
  8214. };
  8215. Identifiers$1.inlineInterpolate = {
  8216. name: 'ɵinlineInterpolate',
  8217. moduleName: CORE$1,
  8218. };
  8219. Identifiers$1.interpolate = { name: 'ɵinterpolate', moduleName: CORE$1 };
  8220. Identifiers$1.EMPTY_ARRAY = { name: 'ɵEMPTY_ARRAY', moduleName: CORE$1 };
  8221. Identifiers$1.EMPTY_MAP = { name: 'ɵEMPTY_MAP', moduleName: CORE$1 };
  8222. Identifiers$1.Renderer = { name: 'Renderer', moduleName: CORE$1 };
  8223. Identifiers$1.viewDef = { name: 'ɵvid', moduleName: CORE$1 };
  8224. Identifiers$1.elementDef = { name: 'ɵeld', moduleName: CORE$1 };
  8225. Identifiers$1.anchorDef = { name: 'ɵand', moduleName: CORE$1 };
  8226. Identifiers$1.textDef = { name: 'ɵted', moduleName: CORE$1 };
  8227. Identifiers$1.directiveDef = { name: 'ɵdid', moduleName: CORE$1 };
  8228. Identifiers$1.providerDef = { name: 'ɵprd', moduleName: CORE$1 };
  8229. Identifiers$1.queryDef = { name: 'ɵqud', moduleName: CORE$1 };
  8230. Identifiers$1.pureArrayDef = { name: 'ɵpad', moduleName: CORE$1 };
  8231. Identifiers$1.pureObjectDef = { name: 'ɵpod', moduleName: CORE$1 };
  8232. Identifiers$1.purePipeDef = { name: 'ɵppd', moduleName: CORE$1 };
  8233. Identifiers$1.pipeDef = { name: 'ɵpid', moduleName: CORE$1 };
  8234. Identifiers$1.nodeValue = { name: 'ɵnov', moduleName: CORE$1 };
  8235. Identifiers$1.ngContentDef = { name: 'ɵncd', moduleName: CORE$1 };
  8236. Identifiers$1.unwrapValue = { name: 'ɵunv', moduleName: CORE$1 };
  8237. Identifiers$1.createRendererType2 = { name: 'ɵcrt', moduleName: CORE$1 };
  8238. // type only
  8239. Identifiers$1.RendererType2 = {
  8240. name: 'RendererType2',
  8241. moduleName: CORE$1,
  8242. };
  8243. // type only
  8244. Identifiers$1.ViewDefinition = {
  8245. name: 'ɵViewDefinition',
  8246. moduleName: CORE$1,
  8247. };
  8248. Identifiers$1.createComponentFactory = { name: 'ɵccf', moduleName: CORE$1 };
  8249. function createTokenForReference(reference) {
  8250. return { identifier: { reference: reference } };
  8251. }
  8252. function createTokenForExternalReference(reflector, reference) {
  8253. return createTokenForReference(reflector.resolveExternalReference(reference));
  8254. }
  8255. /**
  8256. * @license
  8257. * Copyright Google LLC All Rights Reserved.
  8258. *
  8259. * Use of this source code is governed by an MIT-style license that can be
  8260. * found in the LICENSE file at https://angular.io/license
  8261. */
  8262. class EventHandlerVars {
  8263. }
  8264. EventHandlerVars.event = variable('$event');
  8265. class ConvertActionBindingResult {
  8266. constructor(
  8267. /**
  8268. * Render2 compatible statements,
  8269. */
  8270. stmts,
  8271. /**
  8272. * Variable name used with render2 compatible statements.
  8273. */
  8274. allowDefault) {
  8275. this.stmts = stmts;
  8276. this.allowDefault = allowDefault;
  8277. /**
  8278. * This is bit of a hack. It converts statements which render2 expects to statements which are
  8279. * expected by render3.
  8280. *
  8281. * Example: `<div click="doSomething($event)">` will generate:
  8282. *
  8283. * Render3:
  8284. * ```
  8285. * const pd_b:any = ((<any>ctx.doSomething($event)) !== false);
  8286. * return pd_b;
  8287. * ```
  8288. *
  8289. * but render2 expects:
  8290. * ```
  8291. * return ctx.doSomething($event);
  8292. * ```
  8293. */
  8294. // TODO(misko): remove this hack once we no longer support ViewEngine.
  8295. this.render3Stmts = stmts.map((statement) => {
  8296. if (statement instanceof DeclareVarStmt && statement.name == allowDefault.name &&
  8297. statement.value instanceof BinaryOperatorExpr) {
  8298. const lhs = statement.value.lhs;
  8299. return new ReturnStatement(lhs.value);
  8300. }
  8301. return statement;
  8302. });
  8303. }
  8304. }
  8305. /**
  8306. * Converts the given expression AST into an executable output AST, assuming the expression is
  8307. * used in an action binding (e.g. an event handler).
  8308. */
  8309. function convertActionBinding(localResolver, implicitReceiver, action, bindingId, interpolationFunction, baseSourceSpan, implicitReceiverAccesses, globals) {
  8310. if (!localResolver) {
  8311. localResolver = new DefaultLocalResolver(globals);
  8312. }
  8313. const actionWithoutBuiltins = convertPropertyBindingBuiltins({
  8314. createLiteralArrayConverter: (argCount) => {
  8315. // Note: no caching for literal arrays in actions.
  8316. return (args) => literalArr(args);
  8317. },
  8318. createLiteralMapConverter: (keys) => {
  8319. // Note: no caching for literal maps in actions.
  8320. return (values) => {
  8321. const entries = keys.map((k, i) => ({
  8322. key: k.key,
  8323. value: values[i],
  8324. quoted: k.quoted,
  8325. }));
  8326. return literalMap(entries);
  8327. };
  8328. },
  8329. createPipeConverter: (name) => {
  8330. throw new Error(`Illegal State: Actions are not allowed to contain pipes. Pipe: ${name}`);
  8331. }
  8332. }, action);
  8333. const visitor = new _AstToIrVisitor(localResolver, implicitReceiver, bindingId, interpolationFunction, baseSourceSpan, implicitReceiverAccesses);
  8334. const actionStmts = [];
  8335. flattenStatements(actionWithoutBuiltins.visit(visitor, _Mode.Statement), actionStmts);
  8336. prependTemporaryDecls(visitor.temporaryCount, bindingId, actionStmts);
  8337. if (visitor.usesImplicitReceiver) {
  8338. localResolver.notifyImplicitReceiverUse();
  8339. }
  8340. const lastIndex = actionStmts.length - 1;
  8341. let preventDefaultVar = null;
  8342. if (lastIndex >= 0) {
  8343. const lastStatement = actionStmts[lastIndex];
  8344. const returnExpr = convertStmtIntoExpression(lastStatement);
  8345. if (returnExpr) {
  8346. // Note: We need to cast the result of the method call to dynamic,
  8347. // as it might be a void method!
  8348. preventDefaultVar = createPreventDefaultVar(bindingId);
  8349. actionStmts[lastIndex] =
  8350. preventDefaultVar.set(returnExpr.cast(DYNAMIC_TYPE).notIdentical(literal(false)))
  8351. .toDeclStmt(null, [StmtModifier.Final]);
  8352. }
  8353. }
  8354. return new ConvertActionBindingResult(actionStmts, preventDefaultVar);
  8355. }
  8356. function convertPropertyBindingBuiltins(converterFactory, ast) {
  8357. return convertBuiltins(converterFactory, ast);
  8358. }
  8359. class ConvertPropertyBindingResult {
  8360. constructor(stmts, currValExpr) {
  8361. this.stmts = stmts;
  8362. this.currValExpr = currValExpr;
  8363. }
  8364. }
  8365. var BindingForm;
  8366. (function (BindingForm) {
  8367. // The general form of binding expression, supports all expressions.
  8368. BindingForm[BindingForm["General"] = 0] = "General";
  8369. // Try to generate a simple binding (no temporaries or statements)
  8370. // otherwise generate a general binding
  8371. BindingForm[BindingForm["TrySimple"] = 1] = "TrySimple";
  8372. // Inlines assignment of temporaries into the generated expression. The result may still
  8373. // have statements attached for declarations of temporary variables.
  8374. // This is the only relevant form for Ivy, the other forms are only used in ViewEngine.
  8375. BindingForm[BindingForm["Expression"] = 2] = "Expression";
  8376. })(BindingForm || (BindingForm = {}));
  8377. /**
  8378. * Converts the given expression AST into an executable output AST, assuming the expression
  8379. * is used in property binding. The expression has to be preprocessed via
  8380. * `convertPropertyBindingBuiltins`.
  8381. */
  8382. function convertPropertyBinding(localResolver, implicitReceiver, expressionWithoutBuiltins, bindingId, form, interpolationFunction) {
  8383. if (!localResolver) {
  8384. localResolver = new DefaultLocalResolver();
  8385. }
  8386. const visitor = new _AstToIrVisitor(localResolver, implicitReceiver, bindingId, interpolationFunction);
  8387. const outputExpr = expressionWithoutBuiltins.visit(visitor, _Mode.Expression);
  8388. const stmts = getStatementsFromVisitor(visitor, bindingId);
  8389. if (visitor.usesImplicitReceiver) {
  8390. localResolver.notifyImplicitReceiverUse();
  8391. }
  8392. if (visitor.temporaryCount === 0 && form == BindingForm.TrySimple) {
  8393. return new ConvertPropertyBindingResult([], outputExpr);
  8394. }
  8395. else if (form === BindingForm.Expression) {
  8396. return new ConvertPropertyBindingResult(stmts, outputExpr);
  8397. }
  8398. const currValExpr = createCurrValueExpr(bindingId);
  8399. stmts.push(currValExpr.set(outputExpr).toDeclStmt(DYNAMIC_TYPE, [StmtModifier.Final]));
  8400. return new ConvertPropertyBindingResult(stmts, currValExpr);
  8401. }
  8402. /**
  8403. * Given some expression, such as a binding or interpolation expression, and a context expression to
  8404. * look values up on, visit each facet of the given expression resolving values from the context
  8405. * expression such that a list of arguments can be derived from the found values that can be used as
  8406. * arguments to an external update instruction.
  8407. *
  8408. * @param localResolver The resolver to use to look up expressions by name appropriately
  8409. * @param contextVariableExpression The expression representing the context variable used to create
  8410. * the final argument expressions
  8411. * @param expressionWithArgumentsToExtract The expression to visit to figure out what values need to
  8412. * be resolved and what arguments list to build.
  8413. * @param bindingId A name prefix used to create temporary variable names if they're needed for the
  8414. * arguments generated
  8415. * @returns An array of expressions that can be passed as arguments to instruction expressions like
  8416. * `o.importExpr(R3.propertyInterpolate).callFn(result)`
  8417. */
  8418. function convertUpdateArguments(localResolver, contextVariableExpression, expressionWithArgumentsToExtract, bindingId) {
  8419. const visitor = new _AstToIrVisitor(localResolver, contextVariableExpression, bindingId, undefined);
  8420. const outputExpr = expressionWithArgumentsToExtract.visit(visitor, _Mode.Expression);
  8421. if (visitor.usesImplicitReceiver) {
  8422. localResolver.notifyImplicitReceiverUse();
  8423. }
  8424. const stmts = getStatementsFromVisitor(visitor, bindingId);
  8425. // Removing the first argument, because it was a length for ViewEngine, not Ivy.
  8426. let args = outputExpr.args.slice(1);
  8427. if (expressionWithArgumentsToExtract instanceof Interpolation) {
  8428. // If we're dealing with an interpolation of 1 value with an empty prefix and suffix, reduce the
  8429. // args returned to just the value, because we're going to pass it to a special instruction.
  8430. const strings = expressionWithArgumentsToExtract.strings;
  8431. if (args.length === 3 && strings[0] === '' && strings[1] === '') {
  8432. // Single argument interpolate instructions.
  8433. args = [args[1]];
  8434. }
  8435. else if (args.length >= 19) {
  8436. // 19 or more arguments must be passed to the `interpolateV`-style instructions, which accept
  8437. // an array of arguments
  8438. args = [literalArr(args)];
  8439. }
  8440. }
  8441. return { stmts, args };
  8442. }
  8443. function getStatementsFromVisitor(visitor, bindingId) {
  8444. const stmts = [];
  8445. for (let i = 0; i < visitor.temporaryCount; i++) {
  8446. stmts.push(temporaryDeclaration(bindingId, i));
  8447. }
  8448. return stmts;
  8449. }
  8450. function convertBuiltins(converterFactory, ast) {
  8451. const visitor = new _BuiltinAstConverter(converterFactory);
  8452. return ast.visit(visitor);
  8453. }
  8454. function temporaryName(bindingId, temporaryNumber) {
  8455. return `tmp_${bindingId}_${temporaryNumber}`;
  8456. }
  8457. function temporaryDeclaration(bindingId, temporaryNumber) {
  8458. return new DeclareVarStmt(temporaryName(bindingId, temporaryNumber));
  8459. }
  8460. function prependTemporaryDecls(temporaryCount, bindingId, statements) {
  8461. for (let i = temporaryCount - 1; i >= 0; i--) {
  8462. statements.unshift(temporaryDeclaration(bindingId, i));
  8463. }
  8464. }
  8465. var _Mode;
  8466. (function (_Mode) {
  8467. _Mode[_Mode["Statement"] = 0] = "Statement";
  8468. _Mode[_Mode["Expression"] = 1] = "Expression";
  8469. })(_Mode || (_Mode = {}));
  8470. function ensureStatementMode(mode, ast) {
  8471. if (mode !== _Mode.Statement) {
  8472. throw new Error(`Expected a statement, but saw ${ast}`);
  8473. }
  8474. }
  8475. function ensureExpressionMode(mode, ast) {
  8476. if (mode !== _Mode.Expression) {
  8477. throw new Error(`Expected an expression, but saw ${ast}`);
  8478. }
  8479. }
  8480. function convertToStatementIfNeeded(mode, expr) {
  8481. if (mode === _Mode.Statement) {
  8482. return expr.toStmt();
  8483. }
  8484. else {
  8485. return expr;
  8486. }
  8487. }
  8488. class _BuiltinAstConverter extends AstTransformer$1 {
  8489. constructor(_converterFactory) {
  8490. super();
  8491. this._converterFactory = _converterFactory;
  8492. }
  8493. visitPipe(ast, context) {
  8494. const args = [ast.exp, ...ast.args].map(ast => ast.visit(this, context));
  8495. return new BuiltinFunctionCall(ast.span, ast.sourceSpan, args, this._converterFactory.createPipeConverter(ast.name, args.length));
  8496. }
  8497. visitLiteralArray(ast, context) {
  8498. const args = ast.expressions.map(ast => ast.visit(this, context));
  8499. return new BuiltinFunctionCall(ast.span, ast.sourceSpan, args, this._converterFactory.createLiteralArrayConverter(ast.expressions.length));
  8500. }
  8501. visitLiteralMap(ast, context) {
  8502. const args = ast.values.map(ast => ast.visit(this, context));
  8503. return new BuiltinFunctionCall(ast.span, ast.sourceSpan, args, this._converterFactory.createLiteralMapConverter(ast.keys));
  8504. }
  8505. }
  8506. class _AstToIrVisitor {
  8507. constructor(_localResolver, _implicitReceiver, bindingId, interpolationFunction, baseSourceSpan, implicitReceiverAccesses) {
  8508. this._localResolver = _localResolver;
  8509. this._implicitReceiver = _implicitReceiver;
  8510. this.bindingId = bindingId;
  8511. this.interpolationFunction = interpolationFunction;
  8512. this.baseSourceSpan = baseSourceSpan;
  8513. this.implicitReceiverAccesses = implicitReceiverAccesses;
  8514. this._nodeMap = new Map();
  8515. this._resultMap = new Map();
  8516. this._currentTemporary = 0;
  8517. this.temporaryCount = 0;
  8518. this.usesImplicitReceiver = false;
  8519. }
  8520. visitUnary(ast, mode) {
  8521. let op;
  8522. switch (ast.operator) {
  8523. case '+':
  8524. op = UnaryOperator.Plus;
  8525. break;
  8526. case '-':
  8527. op = UnaryOperator.Minus;
  8528. break;
  8529. default:
  8530. throw new Error(`Unsupported operator ${ast.operator}`);
  8531. }
  8532. return convertToStatementIfNeeded(mode, new UnaryOperatorExpr(op, this._visit(ast.expr, _Mode.Expression), undefined, this.convertSourceSpan(ast.span)));
  8533. }
  8534. visitBinary(ast, mode) {
  8535. let op;
  8536. switch (ast.operation) {
  8537. case '+':
  8538. op = BinaryOperator.Plus;
  8539. break;
  8540. case '-':
  8541. op = BinaryOperator.Minus;
  8542. break;
  8543. case '*':
  8544. op = BinaryOperator.Multiply;
  8545. break;
  8546. case '/':
  8547. op = BinaryOperator.Divide;
  8548. break;
  8549. case '%':
  8550. op = BinaryOperator.Modulo;
  8551. break;
  8552. case '&&':
  8553. op = BinaryOperator.And;
  8554. break;
  8555. case '||':
  8556. op = BinaryOperator.Or;
  8557. break;
  8558. case '==':
  8559. op = BinaryOperator.Equals;
  8560. break;
  8561. case '!=':
  8562. op = BinaryOperator.NotEquals;
  8563. break;
  8564. case '===':
  8565. op = BinaryOperator.Identical;
  8566. break;
  8567. case '!==':
  8568. op = BinaryOperator.NotIdentical;
  8569. break;
  8570. case '<':
  8571. op = BinaryOperator.Lower;
  8572. break;
  8573. case '>':
  8574. op = BinaryOperator.Bigger;
  8575. break;
  8576. case '<=':
  8577. op = BinaryOperator.LowerEquals;
  8578. break;
  8579. case '>=':
  8580. op = BinaryOperator.BiggerEquals;
  8581. break;
  8582. case '??':
  8583. return this.convertNullishCoalesce(ast, mode);
  8584. default:
  8585. throw new Error(`Unsupported operation ${ast.operation}`);
  8586. }
  8587. return convertToStatementIfNeeded(mode, new BinaryOperatorExpr(op, this._visit(ast.left, _Mode.Expression), this._visit(ast.right, _Mode.Expression), undefined, this.convertSourceSpan(ast.span)));
  8588. }
  8589. visitChain(ast, mode) {
  8590. ensureStatementMode(mode, ast);
  8591. return this.visitAll(ast.expressions, mode);
  8592. }
  8593. visitConditional(ast, mode) {
  8594. const value = this._visit(ast.condition, _Mode.Expression);
  8595. return convertToStatementIfNeeded(mode, value.conditional(this._visit(ast.trueExp, _Mode.Expression), this._visit(ast.falseExp, _Mode.Expression), this.convertSourceSpan(ast.span)));
  8596. }
  8597. visitPipe(ast, mode) {
  8598. throw new Error(`Illegal state: Pipes should have been converted into functions. Pipe: ${ast.name}`);
  8599. }
  8600. visitFunctionCall(ast, mode) {
  8601. const convertedArgs = this.visitAll(ast.args, _Mode.Expression);
  8602. let fnResult;
  8603. if (ast instanceof BuiltinFunctionCall) {
  8604. fnResult = ast.converter(convertedArgs);
  8605. }
  8606. else {
  8607. fnResult = this._visit(ast.target, _Mode.Expression)
  8608. .callFn(convertedArgs, this.convertSourceSpan(ast.span));
  8609. }
  8610. return convertToStatementIfNeeded(mode, fnResult);
  8611. }
  8612. visitImplicitReceiver(ast, mode) {
  8613. ensureExpressionMode(mode, ast);
  8614. this.usesImplicitReceiver = true;
  8615. return this._implicitReceiver;
  8616. }
  8617. visitThisReceiver(ast, mode) {
  8618. return this.visitImplicitReceiver(ast, mode);
  8619. }
  8620. visitInterpolation(ast, mode) {
  8621. ensureExpressionMode(mode, ast);
  8622. const args = [literal(ast.expressions.length)];
  8623. for (let i = 0; i < ast.strings.length - 1; i++) {
  8624. args.push(literal(ast.strings[i]));
  8625. args.push(this._visit(ast.expressions[i], _Mode.Expression));
  8626. }
  8627. args.push(literal(ast.strings[ast.strings.length - 1]));
  8628. if (this.interpolationFunction) {
  8629. return this.interpolationFunction(args);
  8630. }
  8631. return ast.expressions.length <= 9 ?
  8632. importExpr(Identifiers$1.inlineInterpolate).callFn(args) :
  8633. importExpr(Identifiers$1.interpolate).callFn([
  8634. args[0], literalArr(args.slice(1), undefined, this.convertSourceSpan(ast.span))
  8635. ]);
  8636. }
  8637. visitKeyedRead(ast, mode) {
  8638. const leftMostSafe = this.leftMostSafeNode(ast);
  8639. if (leftMostSafe) {
  8640. return this.convertSafeAccess(ast, leftMostSafe, mode);
  8641. }
  8642. else {
  8643. return convertToStatementIfNeeded(mode, this._visit(ast.obj, _Mode.Expression).key(this._visit(ast.key, _Mode.Expression)));
  8644. }
  8645. }
  8646. visitKeyedWrite(ast, mode) {
  8647. const obj = this._visit(ast.obj, _Mode.Expression);
  8648. const key = this._visit(ast.key, _Mode.Expression);
  8649. const value = this._visit(ast.value, _Mode.Expression);
  8650. return convertToStatementIfNeeded(mode, obj.key(key).set(value));
  8651. }
  8652. visitLiteralArray(ast, mode) {
  8653. throw new Error(`Illegal State: literal arrays should have been converted into functions`);
  8654. }
  8655. visitLiteralMap(ast, mode) {
  8656. throw new Error(`Illegal State: literal maps should have been converted into functions`);
  8657. }
  8658. visitLiteralPrimitive(ast, mode) {
  8659. // For literal values of null, undefined, true, or false allow type interference
  8660. // to infer the type.
  8661. const type = ast.value === null || ast.value === undefined || ast.value === true || ast.value === true ?
  8662. INFERRED_TYPE :
  8663. undefined;
  8664. return convertToStatementIfNeeded(mode, literal(ast.value, type, this.convertSourceSpan(ast.span)));
  8665. }
  8666. _getLocal(name, receiver) {
  8667. var _a;
  8668. if (((_a = this._localResolver.globals) === null || _a === void 0 ? void 0 : _a.has(name)) && receiver instanceof ThisReceiver) {
  8669. return null;
  8670. }
  8671. return this._localResolver.getLocal(name);
  8672. }
  8673. visitMethodCall(ast, mode) {
  8674. if (ast.receiver instanceof ImplicitReceiver &&
  8675. !(ast.receiver instanceof ThisReceiver) && ast.name === '$any') {
  8676. const args = this.visitAll(ast.args, _Mode.Expression);
  8677. if (args.length != 1) {
  8678. throw new Error(`Invalid call to $any, expected 1 argument but received ${args.length || 'none'}`);
  8679. }
  8680. return args[0].cast(DYNAMIC_TYPE, this.convertSourceSpan(ast.span));
  8681. }
  8682. const leftMostSafe = this.leftMostSafeNode(ast);
  8683. if (leftMostSafe) {
  8684. return this.convertSafeAccess(ast, leftMostSafe, mode);
  8685. }
  8686. else {
  8687. const args = this.visitAll(ast.args, _Mode.Expression);
  8688. const prevUsesImplicitReceiver = this.usesImplicitReceiver;
  8689. let result = null;
  8690. const receiver = this._visit(ast.receiver, _Mode.Expression);
  8691. if (receiver === this._implicitReceiver) {
  8692. const varExpr = this._getLocal(ast.name, ast.receiver);
  8693. if (varExpr) {
  8694. // Restore the previous "usesImplicitReceiver" state since the implicit
  8695. // receiver has been replaced with a resolved local expression.
  8696. this.usesImplicitReceiver = prevUsesImplicitReceiver;
  8697. result = varExpr.callFn(args);
  8698. this.addImplicitReceiverAccess(ast.name);
  8699. }
  8700. }
  8701. if (result == null) {
  8702. result = receiver.callMethod(ast.name, args, this.convertSourceSpan(ast.span));
  8703. }
  8704. return convertToStatementIfNeeded(mode, result);
  8705. }
  8706. }
  8707. visitPrefixNot(ast, mode) {
  8708. return convertToStatementIfNeeded(mode, not(this._visit(ast.expression, _Mode.Expression)));
  8709. }
  8710. visitNonNullAssert(ast, mode) {
  8711. return convertToStatementIfNeeded(mode, assertNotNull(this._visit(ast.expression, _Mode.Expression)));
  8712. }
  8713. visitPropertyRead(ast, mode) {
  8714. const leftMostSafe = this.leftMostSafeNode(ast);
  8715. if (leftMostSafe) {
  8716. return this.convertSafeAccess(ast, leftMostSafe, mode);
  8717. }
  8718. else {
  8719. let result = null;
  8720. const prevUsesImplicitReceiver = this.usesImplicitReceiver;
  8721. const receiver = this._visit(ast.receiver, _Mode.Expression);
  8722. if (receiver === this._implicitReceiver) {
  8723. result = this._getLocal(ast.name, ast.receiver);
  8724. if (result) {
  8725. // Restore the previous "usesImplicitReceiver" state since the implicit
  8726. // receiver has been replaced with a resolved local expression.
  8727. this.usesImplicitReceiver = prevUsesImplicitReceiver;
  8728. this.addImplicitReceiverAccess(ast.name);
  8729. }
  8730. }
  8731. if (result == null) {
  8732. result = receiver.prop(ast.name);
  8733. }
  8734. return convertToStatementIfNeeded(mode, result);
  8735. }
  8736. }
  8737. visitPropertyWrite(ast, mode) {
  8738. const receiver = this._visit(ast.receiver, _Mode.Expression);
  8739. const prevUsesImplicitReceiver = this.usesImplicitReceiver;
  8740. let varExpr = null;
  8741. if (receiver === this._implicitReceiver) {
  8742. const localExpr = this._getLocal(ast.name, ast.receiver);
  8743. if (localExpr) {
  8744. if (localExpr instanceof ReadPropExpr) {
  8745. // If the local variable is a property read expression, it's a reference
  8746. // to a 'context.property' value and will be used as the target of the
  8747. // write expression.
  8748. varExpr = localExpr;
  8749. // Restore the previous "usesImplicitReceiver" state since the implicit
  8750. // receiver has been replaced with a resolved local expression.
  8751. this.usesImplicitReceiver = prevUsesImplicitReceiver;
  8752. this.addImplicitReceiverAccess(ast.name);
  8753. }
  8754. else {
  8755. // Otherwise it's an error.
  8756. const receiver = ast.name;
  8757. const value = (ast.value instanceof PropertyRead) ? ast.value.name : undefined;
  8758. throw new Error(`Cannot assign value "${value}" to template variable "${receiver}". Template variables are read-only.`);
  8759. }
  8760. }
  8761. }
  8762. // If no local expression could be produced, use the original receiver's
  8763. // property as the target.
  8764. if (varExpr === null) {
  8765. varExpr = receiver.prop(ast.name);
  8766. }
  8767. return convertToStatementIfNeeded(mode, varExpr.set(this._visit(ast.value, _Mode.Expression)));
  8768. }
  8769. visitSafePropertyRead(ast, mode) {
  8770. return this.convertSafeAccess(ast, this.leftMostSafeNode(ast), mode);
  8771. }
  8772. visitSafeMethodCall(ast, mode) {
  8773. return this.convertSafeAccess(ast, this.leftMostSafeNode(ast), mode);
  8774. }
  8775. visitAll(asts, mode) {
  8776. return asts.map(ast => this._visit(ast, mode));
  8777. }
  8778. visitQuote(ast, mode) {
  8779. throw new Error(`Quotes are not supported for evaluation!
  8780. Statement: ${ast.uninterpretedExpression} located at ${ast.location}`);
  8781. }
  8782. _visit(ast, mode) {
  8783. const result = this._resultMap.get(ast);
  8784. if (result)
  8785. return result;
  8786. return (this._nodeMap.get(ast) || ast).visit(this, mode);
  8787. }
  8788. convertSafeAccess(ast, leftMostSafe, mode) {
  8789. // If the expression contains a safe access node on the left it needs to be converted to
  8790. // an expression that guards the access to the member by checking the receiver for blank. As
  8791. // execution proceeds from left to right, the left most part of the expression must be guarded
  8792. // first but, because member access is left associative, the right side of the expression is at
  8793. // the top of the AST. The desired result requires lifting a copy of the left part of the
  8794. // expression up to test it for blank before generating the unguarded version.
  8795. // Consider, for example the following expression: a?.b.c?.d.e
  8796. // This results in the ast:
  8797. // .
  8798. // / \
  8799. // ?. e
  8800. // / \
  8801. // . d
  8802. // / \
  8803. // ?. c
  8804. // / \
  8805. // a b
  8806. // The following tree should be generated:
  8807. //
  8808. // /---- ? ----\
  8809. // / | \
  8810. // a /--- ? ---\ null
  8811. // / | \
  8812. // . . null
  8813. // / \ / \
  8814. // . c . e
  8815. // / \ / \
  8816. // a b . d
  8817. // / \
  8818. // . c
  8819. // / \
  8820. // a b
  8821. //
  8822. // Notice that the first guard condition is the left hand of the left most safe access node
  8823. // which comes in as leftMostSafe to this routine.
  8824. let guardedExpression = this._visit(leftMostSafe.receiver, _Mode.Expression);
  8825. let temporary = undefined;
  8826. if (this.needsTemporaryInSafeAccess(leftMostSafe.receiver)) {
  8827. // If the expression has method calls or pipes then we need to save the result into a
  8828. // temporary variable to avoid calling stateful or impure code more than once.
  8829. temporary = this.allocateTemporary();
  8830. // Preserve the result in the temporary variable
  8831. guardedExpression = temporary.set(guardedExpression);
  8832. // Ensure all further references to the guarded expression refer to the temporary instead.
  8833. this._resultMap.set(leftMostSafe.receiver, temporary);
  8834. }
  8835. const condition = guardedExpression.isBlank();
  8836. // Convert the ast to an unguarded access to the receiver's member. The map will substitute
  8837. // leftMostNode with its unguarded version in the call to `this.visit()`.
  8838. if (leftMostSafe instanceof SafeMethodCall) {
  8839. this._nodeMap.set(leftMostSafe, new MethodCall(leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.nameSpan, leftMostSafe.receiver, leftMostSafe.name, leftMostSafe.args, leftMostSafe.argumentSpan));
  8840. }
  8841. else {
  8842. this._nodeMap.set(leftMostSafe, new PropertyRead(leftMostSafe.span, leftMostSafe.sourceSpan, leftMostSafe.nameSpan, leftMostSafe.receiver, leftMostSafe.name));
  8843. }
  8844. // Recursively convert the node now without the guarded member access.
  8845. const access = this._visit(ast, _Mode.Expression);
  8846. // Remove the mapping. This is not strictly required as the converter only traverses each node
  8847. // once but is safer if the conversion is changed to traverse the nodes more than once.
  8848. this._nodeMap.delete(leftMostSafe);
  8849. // If we allocated a temporary, release it.
  8850. if (temporary) {
  8851. this.releaseTemporary(temporary);
  8852. }
  8853. // Produce the conditional
  8854. return convertToStatementIfNeeded(mode, condition.conditional(NULL_EXPR, access));
  8855. }
  8856. convertNullishCoalesce(ast, mode) {
  8857. const left = this._visit(ast.left, _Mode.Expression);
  8858. const right = this._visit(ast.right, _Mode.Expression);
  8859. const temporary = this.allocateTemporary();
  8860. this.releaseTemporary(temporary);
  8861. // Generate the following expression. It is identical to how TS
  8862. // transpiles binary expressions with a nullish coalescing operator.
  8863. // let temp;
  8864. // (temp = a) !== null && temp !== undefined ? temp : b;
  8865. return convertToStatementIfNeeded(mode, temporary.set(left)
  8866. .notIdentical(NULL_EXPR)
  8867. .and(temporary.notIdentical(literal(undefined)))
  8868. .conditional(temporary, right));
  8869. }
  8870. // Given an expression of the form a?.b.c?.d.e then the left most safe node is
  8871. // the (a?.b). The . and ?. are left associative thus can be rewritten as:
  8872. // ((((a?.c).b).c)?.d).e. This returns the most deeply nested safe read or
  8873. // safe method call as this needs to be transformed initially to:
  8874. // a == null ? null : a.c.b.c?.d.e
  8875. // then to:
  8876. // a == null ? null : a.b.c == null ? null : a.b.c.d.e
  8877. leftMostSafeNode(ast) {
  8878. const visit = (visitor, ast) => {
  8879. return (this._nodeMap.get(ast) || ast).visit(visitor);
  8880. };
  8881. return ast.visit({
  8882. visitUnary(ast) {
  8883. return null;
  8884. },
  8885. visitBinary(ast) {
  8886. return null;
  8887. },
  8888. visitChain(ast) {
  8889. return null;
  8890. },
  8891. visitConditional(ast) {
  8892. return null;
  8893. },
  8894. visitFunctionCall(ast) {
  8895. return null;
  8896. },
  8897. visitImplicitReceiver(ast) {
  8898. return null;
  8899. },
  8900. visitThisReceiver(ast) {
  8901. return null;
  8902. },
  8903. visitInterpolation(ast) {
  8904. return null;
  8905. },
  8906. visitKeyedRead(ast) {
  8907. return visit(this, ast.obj);
  8908. },
  8909. visitKeyedWrite(ast) {
  8910. return null;
  8911. },
  8912. visitLiteralArray(ast) {
  8913. return null;
  8914. },
  8915. visitLiteralMap(ast) {
  8916. return null;
  8917. },
  8918. visitLiteralPrimitive(ast) {
  8919. return null;
  8920. },
  8921. visitMethodCall(ast) {
  8922. return visit(this, ast.receiver);
  8923. },
  8924. visitPipe(ast) {
  8925. return null;
  8926. },
  8927. visitPrefixNot(ast) {
  8928. return null;
  8929. },
  8930. visitNonNullAssert(ast) {
  8931. return null;
  8932. },
  8933. visitPropertyRead(ast) {
  8934. return visit(this, ast.receiver);
  8935. },
  8936. visitPropertyWrite(ast) {
  8937. return null;
  8938. },
  8939. visitQuote(ast) {
  8940. return null;
  8941. },
  8942. visitSafeMethodCall(ast) {
  8943. return visit(this, ast.receiver) || ast;
  8944. },
  8945. visitSafePropertyRead(ast) {
  8946. return visit(this, ast.receiver) || ast;
  8947. }
  8948. });
  8949. }
  8950. // Returns true of the AST includes a method or a pipe indicating that, if the
  8951. // expression is used as the target of a safe property or method access then
  8952. // the expression should be stored into a temporary variable.
  8953. needsTemporaryInSafeAccess(ast) {
  8954. const visit = (visitor, ast) => {
  8955. return ast && (this._nodeMap.get(ast) || ast).visit(visitor);
  8956. };
  8957. const visitSome = (visitor, ast) => {
  8958. return ast.some(ast => visit(visitor, ast));
  8959. };
  8960. return ast.visit({
  8961. visitUnary(ast) {
  8962. return visit(this, ast.expr);
  8963. },
  8964. visitBinary(ast) {
  8965. return visit(this, ast.left) || visit(this, ast.right);
  8966. },
  8967. visitChain(ast) {
  8968. return false;
  8969. },
  8970. visitConditional(ast) {
  8971. return visit(this, ast.condition) || visit(this, ast.trueExp) || visit(this, ast.falseExp);
  8972. },
  8973. visitFunctionCall(ast) {
  8974. return true;
  8975. },
  8976. visitImplicitReceiver(ast) {
  8977. return false;
  8978. },
  8979. visitThisReceiver(ast) {
  8980. return false;
  8981. },
  8982. visitInterpolation(ast) {
  8983. return visitSome(this, ast.expressions);
  8984. },
  8985. visitKeyedRead(ast) {
  8986. return false;
  8987. },
  8988. visitKeyedWrite(ast) {
  8989. return false;
  8990. },
  8991. visitLiteralArray(ast) {
  8992. return true;
  8993. },
  8994. visitLiteralMap(ast) {
  8995. return true;
  8996. },
  8997. visitLiteralPrimitive(ast) {
  8998. return false;
  8999. },
  9000. visitMethodCall(ast) {
  9001. return true;
  9002. },
  9003. visitPipe(ast) {
  9004. return true;
  9005. },
  9006. visitPrefixNot(ast) {
  9007. return visit(this, ast.expression);
  9008. },
  9009. visitNonNullAssert(ast) {
  9010. return visit(this, ast.expression);
  9011. },
  9012. visitPropertyRead(ast) {
  9013. return false;
  9014. },
  9015. visitPropertyWrite(ast) {
  9016. return false;
  9017. },
  9018. visitQuote(ast) {
  9019. return false;
  9020. },
  9021. visitSafeMethodCall(ast) {
  9022. return true;
  9023. },
  9024. visitSafePropertyRead(ast) {
  9025. return false;
  9026. }
  9027. });
  9028. }
  9029. allocateTemporary() {
  9030. const tempNumber = this._currentTemporary++;
  9031. this.temporaryCount = Math.max(this._currentTemporary, this.temporaryCount);
  9032. return new ReadVarExpr(temporaryName(this.bindingId, tempNumber));
  9033. }
  9034. releaseTemporary(temporary) {
  9035. this._currentTemporary--;
  9036. if (temporary.name != temporaryName(this.bindingId, this._currentTemporary)) {
  9037. throw new Error(`Temporary ${temporary.name} released out of order`);
  9038. }
  9039. }
  9040. /**
  9041. * Creates an absolute `ParseSourceSpan` from the relative `ParseSpan`.
  9042. *
  9043. * `ParseSpan` objects are relative to the start of the expression.
  9044. * This method converts these to full `ParseSourceSpan` objects that
  9045. * show where the span is within the overall source file.
  9046. *
  9047. * @param span the relative span to convert.
  9048. * @returns a `ParseSourceSpan` for the given span or null if no
  9049. * `baseSourceSpan` was provided to this class.
  9050. */
  9051. convertSourceSpan(span) {
  9052. if (this.baseSourceSpan) {
  9053. const start = this.baseSourceSpan.start.moveBy(span.start);
  9054. const end = this.baseSourceSpan.start.moveBy(span.end);
  9055. const fullStart = this.baseSourceSpan.fullStart.moveBy(span.start);
  9056. return new ParseSourceSpan(start, end, fullStart);
  9057. }
  9058. else {
  9059. return null;
  9060. }
  9061. }
  9062. /** Adds the name of an AST to the list of implicit receiver accesses. */
  9063. addImplicitReceiverAccess(name) {
  9064. if (this.implicitReceiverAccesses) {
  9065. this.implicitReceiverAccesses.add(name);
  9066. }
  9067. }
  9068. }
  9069. function flattenStatements(arg, output) {
  9070. if (Array.isArray(arg)) {
  9071. arg.forEach((entry) => flattenStatements(entry, output));
  9072. }
  9073. else {
  9074. output.push(arg);
  9075. }
  9076. }
  9077. class DefaultLocalResolver {
  9078. constructor(globals) {
  9079. this.globals = globals;
  9080. }
  9081. notifyImplicitReceiverUse() { }
  9082. getLocal(name) {
  9083. if (name === EventHandlerVars.event.name) {
  9084. return EventHandlerVars.event;
  9085. }
  9086. return null;
  9087. }
  9088. }
  9089. function createCurrValueExpr(bindingId) {
  9090. return variable(`currVal_${bindingId}`); // fix syntax highlighting: `
  9091. }
  9092. function createPreventDefaultVar(bindingId) {
  9093. return variable(`pd_${bindingId}`);
  9094. }
  9095. function convertStmtIntoExpression(stmt) {
  9096. if (stmt instanceof ExpressionStatement) {
  9097. return stmt.expr;
  9098. }
  9099. else if (stmt instanceof ReturnStatement) {
  9100. return stmt.value;
  9101. }
  9102. return null;
  9103. }
  9104. class BuiltinFunctionCall extends FunctionCall {
  9105. constructor(span, sourceSpan, args, converter) {
  9106. super(span, sourceSpan, null, args);
  9107. this.args = args;
  9108. this.converter = converter;
  9109. }
  9110. }
  9111. /**
  9112. * @license
  9113. * Copyright Google LLC All Rights Reserved.
  9114. *
  9115. * Use of this source code is governed by an MIT-style license that can be
  9116. * found in the LICENSE file at https://angular.io/license
  9117. */
  9118. /**
  9119. * This file is a port of shadowCSS from webcomponents.js to TypeScript.
  9120. *
  9121. * Please make sure to keep to edits in sync with the source file.
  9122. *
  9123. * Source:
  9124. * https://github.com/webcomponents/webcomponentsjs/blob/4efecd7e0e/src/ShadowCSS/ShadowCSS.js
  9125. *
  9126. * The original file level comment is reproduced below
  9127. */
  9128. /*
  9129. This is a limited shim for ShadowDOM css styling.
  9130. https://dvcs.w3.org/hg/webcomponents/raw-file/tip/spec/shadow/index.html#styles
  9131. The intention here is to support only the styling features which can be
  9132. relatively simply implemented. The goal is to allow users to avoid the
  9133. most obvious pitfalls and do so without compromising performance significantly.
  9134. For ShadowDOM styling that's not covered here, a set of best practices
  9135. can be provided that should allow users to accomplish more complex styling.
  9136. The following is a list of specific ShadowDOM styling features and a brief
  9137. discussion of the approach used to shim.
  9138. Shimmed features:
  9139. * :host, :host-context: ShadowDOM allows styling of the shadowRoot's host
  9140. element using the :host rule. To shim this feature, the :host styles are
  9141. reformatted and prefixed with a given scope name and promoted to a
  9142. document level stylesheet.
  9143. For example, given a scope name of .foo, a rule like this:
  9144. :host {
  9145. background: red;
  9146. }
  9147. }
  9148. becomes:
  9149. .foo {
  9150. background: red;
  9151. }
  9152. * encapsulation: Styles defined within ShadowDOM, apply only to
  9153. dom inside the ShadowDOM. Polymer uses one of two techniques to implement
  9154. this feature.
  9155. By default, rules are prefixed with the host element tag name
  9156. as a descendant selector. This ensures styling does not leak out of the 'top'
  9157. of the element's ShadowDOM. For example,
  9158. div {
  9159. font-weight: bold;
  9160. }
  9161. becomes:
  9162. x-foo div {
  9163. font-weight: bold;
  9164. }
  9165. becomes:
  9166. Alternatively, if WebComponents.ShadowCSS.strictStyling is set to true then
  9167. selectors are scoped by adding an attribute selector suffix to each
  9168. simple selector that contains the host element tag name. Each element
  9169. in the element's ShadowDOM template is also given the scope attribute.
  9170. Thus, these rules match only elements that have the scope attribute.
  9171. For example, given a scope name of x-foo, a rule like this:
  9172. div {
  9173. font-weight: bold;
  9174. }
  9175. becomes:
  9176. div[x-foo] {
  9177. font-weight: bold;
  9178. }
  9179. Note that elements that are dynamically added to a scope must have the scope
  9180. selector added to them manually.
  9181. * upper/lower bound encapsulation: Styles which are defined outside a
  9182. shadowRoot should not cross the ShadowDOM boundary and should not apply
  9183. inside a shadowRoot.
  9184. This styling behavior is not emulated. Some possible ways to do this that
  9185. were rejected due to complexity and/or performance concerns include: (1) reset
  9186. every possible property for every possible selector for a given scope name;
  9187. (2) re-implement css in javascript.
  9188. As an alternative, users should make sure to use selectors
  9189. specific to the scope in which they are working.
  9190. * ::distributed: This behavior is not emulated. It's often not necessary
  9191. to style the contents of a specific insertion point and instead, descendants
  9192. of the host element can be styled selectively. Users can also create an
  9193. extra node around an insertion point and style that node's contents
  9194. via descendent selectors. For example, with a shadowRoot like this:
  9195. <style>
  9196. ::content(div) {
  9197. background: red;
  9198. }
  9199. </style>
  9200. <content></content>
  9201. could become:
  9202. <style>
  9203. / *@polyfill .content-container div * /
  9204. ::content(div) {
  9205. background: red;
  9206. }
  9207. </style>
  9208. <div class="content-container">
  9209. <content></content>
  9210. </div>
  9211. Note the use of @polyfill in the comment above a ShadowDOM specific style
  9212. declaration. This is a directive to the styling shim to use the selector
  9213. in comments in lieu of the next selector when running under polyfill.
  9214. */
  9215. class ShadowCss {
  9216. constructor() {
  9217. this.strictStyling = true;
  9218. }
  9219. /*
  9220. * Shim some cssText with the given selector. Returns cssText that can
  9221. * be included in the document via WebComponents.ShadowCSS.addCssToDocument(css).
  9222. *
  9223. * When strictStyling is true:
  9224. * - selector is the attribute added to all elements inside the host,
  9225. * - hostSelector is the attribute added to the host itself.
  9226. */
  9227. shimCssText(cssText, selector, hostSelector = '') {
  9228. const commentsWithHash = extractCommentsWithHash(cssText);
  9229. cssText = stripComments(cssText);
  9230. cssText = this._insertDirectives(cssText);
  9231. const scopedCssText = this._scopeCssText(cssText, selector, hostSelector);
  9232. return [scopedCssText, ...commentsWithHash].join('\n');
  9233. }
  9234. _insertDirectives(cssText) {
  9235. cssText = this._insertPolyfillDirectivesInCssText(cssText);
  9236. return this._insertPolyfillRulesInCssText(cssText);
  9237. }
  9238. /*
  9239. * Process styles to convert native ShadowDOM rules that will trip
  9240. * up the css parser; we rely on decorating the stylesheet with inert rules.
  9241. *
  9242. * For example, we convert this rule:
  9243. *
  9244. * polyfill-next-selector { content: ':host menu-item'; }
  9245. * ::content menu-item {
  9246. *
  9247. * to this:
  9248. *
  9249. * scopeName menu-item {
  9250. *
  9251. **/
  9252. _insertPolyfillDirectivesInCssText(cssText) {
  9253. // Difference with webcomponents.js: does not handle comments
  9254. return cssText.replace(_cssContentNextSelectorRe, function (...m) {
  9255. return m[2] + '{';
  9256. });
  9257. }
  9258. /*
  9259. * Process styles to add rules which will only apply under the polyfill
  9260. *
  9261. * For example, we convert this rule:
  9262. *
  9263. * polyfill-rule {
  9264. * content: ':host menu-item';
  9265. * ...
  9266. * }
  9267. *
  9268. * to this:
  9269. *
  9270. * scopeName menu-item {...}
  9271. *
  9272. **/
  9273. _insertPolyfillRulesInCssText(cssText) {
  9274. // Difference with webcomponents.js: does not handle comments
  9275. return cssText.replace(_cssContentRuleRe, (...m) => {
  9276. const rule = m[0].replace(m[1], '').replace(m[2], '');
  9277. return m[4] + rule;
  9278. });
  9279. }
  9280. /* Ensure styles are scoped. Pseudo-scoping takes a rule like:
  9281. *
  9282. * .foo {... }
  9283. *
  9284. * and converts this to
  9285. *
  9286. * scopeName .foo { ... }
  9287. */
  9288. _scopeCssText(cssText, scopeSelector, hostSelector) {
  9289. const unscopedRules = this._extractUnscopedRulesFromCssText(cssText);
  9290. // replace :host and :host-context -shadowcsshost and -shadowcsshost respectively
  9291. cssText = this._insertPolyfillHostInCssText(cssText);
  9292. cssText = this._convertColonHost(cssText);
  9293. cssText = this._convertColonHostContext(cssText);
  9294. cssText = this._convertShadowDOMSelectors(cssText);
  9295. if (scopeSelector) {
  9296. cssText = this._scopeSelectors(cssText, scopeSelector, hostSelector);
  9297. }
  9298. cssText = cssText + '\n' + unscopedRules;
  9299. return cssText.trim();
  9300. }
  9301. /*
  9302. * Process styles to add rules which will only apply under the polyfill
  9303. * and do not process via CSSOM. (CSSOM is destructive to rules on rare
  9304. * occasions, e.g. -webkit-calc on Safari.)
  9305. * For example, we convert this rule:
  9306. *
  9307. * @polyfill-unscoped-rule {
  9308. * content: 'menu-item';
  9309. * ... }
  9310. *
  9311. * to this:
  9312. *
  9313. * menu-item {...}
  9314. *
  9315. **/
  9316. _extractUnscopedRulesFromCssText(cssText) {
  9317. // Difference with webcomponents.js: does not handle comments
  9318. let r = '';
  9319. let m;
  9320. _cssContentUnscopedRuleRe.lastIndex = 0;
  9321. while ((m = _cssContentUnscopedRuleRe.exec(cssText)) !== null) {
  9322. const rule = m[0].replace(m[2], '').replace(m[1], m[4]);
  9323. r += rule + '\n\n';
  9324. }
  9325. return r;
  9326. }
  9327. /*
  9328. * convert a rule like :host(.foo) > .bar { }
  9329. *
  9330. * to
  9331. *
  9332. * .foo<scopeName> > .bar
  9333. */
  9334. _convertColonHost(cssText) {
  9335. return cssText.replace(_cssColonHostRe, (_, hostSelectors, otherSelectors) => {
  9336. if (hostSelectors) {
  9337. const convertedSelectors = [];
  9338. const hostSelectorArray = hostSelectors.split(',').map(p => p.trim());
  9339. for (const hostSelector of hostSelectorArray) {
  9340. if (!hostSelector)
  9341. break;
  9342. const convertedSelector = _polyfillHostNoCombinator + hostSelector.replace(_polyfillHost, '') + otherSelectors;
  9343. convertedSelectors.push(convertedSelector);
  9344. }
  9345. return convertedSelectors.join(',');
  9346. }
  9347. else {
  9348. return _polyfillHostNoCombinator + otherSelectors;
  9349. }
  9350. });
  9351. }
  9352. /*
  9353. * convert a rule like :host-context(.foo) > .bar { }
  9354. *
  9355. * to
  9356. *
  9357. * .foo<scopeName> > .bar, .foo <scopeName> > .bar { }
  9358. *
  9359. * and
  9360. *
  9361. * :host-context(.foo:host) .bar { ... }
  9362. *
  9363. * to
  9364. *
  9365. * .foo<scopeName> .bar { ... }
  9366. */
  9367. _convertColonHostContext(cssText) {
  9368. return cssText.replace(_cssColonHostContextReGlobal, selectorText => {
  9369. // We have captured a selector that contains a `:host-context` rule.
  9370. var _a;
  9371. // For backward compatibility `:host-context` may contain a comma separated list of selectors.
  9372. // Each context selector group will contain a list of host-context selectors that must match
  9373. // an ancestor of the host.
  9374. // (Normally `contextSelectorGroups` will only contain a single array of context selectors.)
  9375. const contextSelectorGroups = [[]];
  9376. // There may be more than `:host-context` in this selector so `selectorText` could look like:
  9377. // `:host-context(.one):host-context(.two)`.
  9378. // Execute `_cssColonHostContextRe` over and over until we have extracted all the
  9379. // `:host-context` selectors from this selector.
  9380. let match;
  9381. while (match = _cssColonHostContextRe.exec(selectorText)) {
  9382. // `match` = [':host-context(<selectors>)<rest>', <selectors>, <rest>]
  9383. // The `<selectors>` could actually be a comma separated list: `:host-context(.one, .two)`.
  9384. const newContextSelectors = ((_a = match[1]) !== null && _a !== void 0 ? _a : '').trim().split(',').map(m => m.trim()).filter(m => m !== '');
  9385. // We must duplicate the current selector group for each of these new selectors.
  9386. // For example if the current groups are:
  9387. // ```
  9388. // [
  9389. // ['a', 'b', 'c'],
  9390. // ['x', 'y', 'z'],
  9391. // ]
  9392. // ```
  9393. // And we have a new set of comma separated selectors: `:host-context(m,n)` then the new
  9394. // groups are:
  9395. // ```
  9396. // [
  9397. // ['a', 'b', 'c', 'm'],
  9398. // ['x', 'y', 'z', 'm'],
  9399. // ['a', 'b', 'c', 'n'],
  9400. // ['x', 'y', 'z', 'n'],
  9401. // ]
  9402. // ```
  9403. const contextSelectorGroupsLength = contextSelectorGroups.length;
  9404. repeatGroups(contextSelectorGroups, newContextSelectors.length);
  9405. for (let i = 0; i < newContextSelectors.length; i++) {
  9406. for (let j = 0; j < contextSelectorGroupsLength; j++) {
  9407. contextSelectorGroups[j + (i * contextSelectorGroupsLength)].push(newContextSelectors[i]);
  9408. }
  9409. }
  9410. // Update the `selectorText` and see repeat to see if there are more `:host-context`s.
  9411. selectorText = match[2];
  9412. }
  9413. // The context selectors now must be combined with each other to capture all the possible
  9414. // selectors that `:host-context` can match. See `combineHostContextSelectors()` for more
  9415. // info about how this is done.
  9416. return contextSelectorGroups
  9417. .map(contextSelectors => combineHostContextSelectors(contextSelectors, selectorText))
  9418. .join(', ');
  9419. });
  9420. }
  9421. /*
  9422. * Convert combinators like ::shadow and pseudo-elements like ::content
  9423. * by replacing with space.
  9424. */
  9425. _convertShadowDOMSelectors(cssText) {
  9426. return _shadowDOMSelectorsRe.reduce((result, pattern) => result.replace(pattern, ' '), cssText);
  9427. }
  9428. // change a selector like 'div' to 'name div'
  9429. _scopeSelectors(cssText, scopeSelector, hostSelector) {
  9430. return processRules(cssText, (rule) => {
  9431. let selector = rule.selector;
  9432. let content = rule.content;
  9433. if (rule.selector[0] !== '@') {
  9434. selector =
  9435. this._scopeSelector(rule.selector, scopeSelector, hostSelector, this.strictStyling);
  9436. }
  9437. else if (rule.selector.startsWith('@media') || rule.selector.startsWith('@supports') ||
  9438. rule.selector.startsWith('@document')) {
  9439. content = this._scopeSelectors(rule.content, scopeSelector, hostSelector);
  9440. }
  9441. else if (rule.selector.startsWith('@font-face') || rule.selector.startsWith('@page')) {
  9442. content = this._stripScopingSelectors(rule.content);
  9443. }
  9444. return new CssRule(selector, content);
  9445. });
  9446. }
  9447. /**
  9448. * Handle a css text that is within a rule that should not contain scope selectors by simply
  9449. * removing them! An example of such a rule is `@font-face`.
  9450. *
  9451. * `@font-face` rules cannot contain nested selectors. Nor can they be nested under a selector.
  9452. * Normally this would be a syntax error by the author of the styles. But in some rare cases, such
  9453. * as importing styles from a library, and applying `:host ::ng-deep` to the imported styles, we
  9454. * can end up with broken css if the imported styles happen to contain @font-face rules.
  9455. *
  9456. * For example:
  9457. *
  9458. * ```
  9459. * :host ::ng-deep {
  9460. * import 'some/lib/containing/font-face';
  9461. * }
  9462. *
  9463. * Similar logic applies to `@page` rules which can contain a particular set of properties,
  9464. * as well as some specific at-rules. Since they can't be encapsulated, we have to strip
  9465. * any scoping selectors from them. For more information: https://www.w3.org/TR/css-page-3
  9466. * ```
  9467. */
  9468. _stripScopingSelectors(cssText) {
  9469. return processRules(cssText, rule => {
  9470. const selector = rule.selector.replace(_shadowDeepSelectors, ' ')
  9471. .replace(_polyfillHostNoCombinatorRe, ' ');
  9472. return new CssRule(selector, rule.content);
  9473. });
  9474. }
  9475. _scopeSelector(selector, scopeSelector, hostSelector, strict) {
  9476. return selector.split(',')
  9477. .map(part => part.trim().split(_shadowDeepSelectors))
  9478. .map((deepParts) => {
  9479. const [shallowPart, ...otherParts] = deepParts;
  9480. const applyScope = (shallowPart) => {
  9481. if (this._selectorNeedsScoping(shallowPart, scopeSelector)) {
  9482. return strict ?
  9483. this._applyStrictSelectorScope(shallowPart, scopeSelector, hostSelector) :
  9484. this._applySelectorScope(shallowPart, scopeSelector, hostSelector);
  9485. }
  9486. else {
  9487. return shallowPart;
  9488. }
  9489. };
  9490. return [applyScope(shallowPart), ...otherParts].join(' ');
  9491. })
  9492. .join(', ');
  9493. }
  9494. _selectorNeedsScoping(selector, scopeSelector) {
  9495. const re = this._makeScopeMatcher(scopeSelector);
  9496. return !re.test(selector);
  9497. }
  9498. _makeScopeMatcher(scopeSelector) {
  9499. const lre = /\[/g;
  9500. const rre = /\]/g;
  9501. scopeSelector = scopeSelector.replace(lre, '\\[').replace(rre, '\\]');
  9502. return new RegExp('^(' + scopeSelector + ')' + _selectorReSuffix, 'm');
  9503. }
  9504. _applySelectorScope(selector, scopeSelector, hostSelector) {
  9505. // Difference from webcomponents.js: scopeSelector could not be an array
  9506. return this._applySimpleSelectorScope(selector, scopeSelector, hostSelector);
  9507. }
  9508. // scope via name and [is=name]
  9509. _applySimpleSelectorScope(selector, scopeSelector, hostSelector) {
  9510. // In Android browser, the lastIndex is not reset when the regex is used in String.replace()
  9511. _polyfillHostRe.lastIndex = 0;
  9512. if (_polyfillHostRe.test(selector)) {
  9513. const replaceBy = this.strictStyling ? `[${hostSelector}]` : scopeSelector;
  9514. return selector
  9515. .replace(_polyfillHostNoCombinatorRe, (hnc, selector) => {
  9516. return selector.replace(/([^:]*)(:*)(.*)/, (_, before, colon, after) => {
  9517. return before + replaceBy + colon + after;
  9518. });
  9519. })
  9520. .replace(_polyfillHostRe, replaceBy + ' ');
  9521. }
  9522. return scopeSelector + ' ' + selector;
  9523. }
  9524. // return a selector with [name] suffix on each simple selector
  9525. // e.g. .foo.bar > .zot becomes .foo[name].bar[name] > .zot[name] /** @internal */
  9526. _applyStrictSelectorScope(selector, scopeSelector, hostSelector) {
  9527. const isRe = /\[is=([^\]]*)\]/g;
  9528. scopeSelector = scopeSelector.replace(isRe, (_, ...parts) => parts[0]);
  9529. const attrName = '[' + scopeSelector + ']';
  9530. const _scopeSelectorPart = (p) => {
  9531. let scopedP = p.trim();
  9532. if (!scopedP) {
  9533. return '';
  9534. }
  9535. if (p.indexOf(_polyfillHostNoCombinator) > -1) {
  9536. scopedP = this._applySimpleSelectorScope(p, scopeSelector, hostSelector);
  9537. }
  9538. else {
  9539. // remove :host since it should be unnecessary
  9540. const t = p.replace(_polyfillHostRe, '');
  9541. if (t.length > 0) {
  9542. const matches = t.match(/([^:]*)(:*)(.*)/);
  9543. if (matches) {
  9544. scopedP = matches[1] + attrName + matches[2] + matches[3];
  9545. }
  9546. }
  9547. }
  9548. return scopedP;
  9549. };
  9550. const safeContent = new SafeSelector(selector);
  9551. selector = safeContent.content();
  9552. let scopedSelector = '';
  9553. let startIndex = 0;
  9554. let res;
  9555. const sep = /( |>|\+|~(?!=))\s*/g;
  9556. // If a selector appears before :host it should not be shimmed as it
  9557. // matches on ancestor elements and not on elements in the host's shadow
  9558. // `:host-context(div)` is transformed to
  9559. // `-shadowcsshost-no-combinatordiv, div -shadowcsshost-no-combinator`
  9560. // the `div` is not part of the component in the 2nd selectors and should not be scoped.
  9561. // Historically `component-tag:host` was matching the component so we also want to preserve
  9562. // this behavior to avoid breaking legacy apps (it should not match).
  9563. // The behavior should be:
  9564. // - `tag:host` -> `tag[h]` (this is to avoid breaking legacy apps, should not match anything)
  9565. // - `tag :host` -> `tag [h]` (`tag` is not scoped because it's considered part of a
  9566. // `:host-context(tag)`)
  9567. const hasHost = selector.indexOf(_polyfillHostNoCombinator) > -1;
  9568. // Only scope parts after the first `-shadowcsshost-no-combinator` when it is present
  9569. let shouldScope = !hasHost;
  9570. while ((res = sep.exec(selector)) !== null) {
  9571. const separator = res[1];
  9572. const part = selector.slice(startIndex, res.index).trim();
  9573. shouldScope = shouldScope || part.indexOf(_polyfillHostNoCombinator) > -1;
  9574. const scopedPart = shouldScope ? _scopeSelectorPart(part) : part;
  9575. scopedSelector += `${scopedPart} ${separator} `;
  9576. startIndex = sep.lastIndex;
  9577. }
  9578. const part = selector.substring(startIndex);
  9579. shouldScope = shouldScope || part.indexOf(_polyfillHostNoCombinator) > -1;
  9580. scopedSelector += shouldScope ? _scopeSelectorPart(part) : part;
  9581. // replace the placeholders with their original values
  9582. return safeContent.restore(scopedSelector);
  9583. }
  9584. _insertPolyfillHostInCssText(selector) {
  9585. return selector.replace(_colonHostContextRe, _polyfillHostContext)
  9586. .replace(_colonHostRe, _polyfillHost);
  9587. }
  9588. }
  9589. class SafeSelector {
  9590. constructor(selector) {
  9591. this.placeholders = [];
  9592. this.index = 0;
  9593. // Replaces attribute selectors with placeholders.
  9594. // The WS in [attr="va lue"] would otherwise be interpreted as a selector separator.
  9595. selector = this._escapeRegexMatches(selector, /(\[[^\]]*\])/g);
  9596. // CSS allows for certain special characters to be used in selectors if they're escaped.
  9597. // E.g. `.foo:blue` won't match a class called `foo:blue`, because the colon denotes a
  9598. // pseudo-class, but writing `.foo\:blue` will match, because the colon was escaped.
  9599. // Replace all escape sequences (`\` followed by a character) with a placeholder so
  9600. // that our handling of pseudo-selectors doesn't mess with them.
  9601. selector = this._escapeRegexMatches(selector, /(\\.)/g);
  9602. // Replaces the expression in `:nth-child(2n + 1)` with a placeholder.
  9603. // WS and "+" would otherwise be interpreted as selector separators.
  9604. this._content = selector.replace(/(:nth-[-\w]+)(\([^)]+\))/g, (_, pseudo, exp) => {
  9605. const replaceBy = `__ph-${this.index}__`;
  9606. this.placeholders.push(exp);
  9607. this.index++;
  9608. return pseudo + replaceBy;
  9609. });
  9610. }
  9611. restore(content) {
  9612. return content.replace(/__ph-(\d+)__/g, (_ph, index) => this.placeholders[+index]);
  9613. }
  9614. content() {
  9615. return this._content;
  9616. }
  9617. /**
  9618. * Replaces all of the substrings that match a regex within a
  9619. * special string (e.g. `__ph-0__`, `__ph-1__`, etc).
  9620. */
  9621. _escapeRegexMatches(content, pattern) {
  9622. return content.replace(pattern, (_, keep) => {
  9623. const replaceBy = `__ph-${this.index}__`;
  9624. this.placeholders.push(keep);
  9625. this.index++;
  9626. return replaceBy;
  9627. });
  9628. }
  9629. }
  9630. const _cssContentNextSelectorRe = /polyfill-next-selector[^}]*content:[\s]*?(['"])(.*?)\1[;\s]*}([^{]*?){/gim;
  9631. const _cssContentRuleRe = /(polyfill-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  9632. const _cssContentUnscopedRuleRe = /(polyfill-unscoped-rule)[^}]*(content:[\s]*(['"])(.*?)\3)[;\s]*[^}]*}/gim;
  9633. const _polyfillHost = '-shadowcsshost';
  9634. // note: :host-context pre-processed to -shadowcsshostcontext.
  9635. const _polyfillHostContext = '-shadowcsscontext';
  9636. const _parenSuffix = '(?:\\((' +
  9637. '(?:\\([^)(]*\\)|[^)(]*)+?' +
  9638. ')\\))?([^,{]*)';
  9639. const _cssColonHostRe = new RegExp(_polyfillHost + _parenSuffix, 'gim');
  9640. const _cssColonHostContextReGlobal = new RegExp(_polyfillHostContext + _parenSuffix, 'gim');
  9641. const _cssColonHostContextRe = new RegExp(_polyfillHostContext + _parenSuffix, 'im');
  9642. const _polyfillHostNoCombinator = _polyfillHost + '-no-combinator';
  9643. const _polyfillHostNoCombinatorRe = /-shadowcsshost-no-combinator([^\s]*)/;
  9644. const _shadowDOMSelectorsRe = [
  9645. /::shadow/g,
  9646. /::content/g,
  9647. // Deprecated selectors
  9648. /\/shadow-deep\//g,
  9649. /\/shadow\//g,
  9650. ];
  9651. // The deep combinator is deprecated in the CSS spec
  9652. // Support for `>>>`, `deep`, `::ng-deep` is then also deprecated and will be removed in the future.
  9653. // see https://github.com/angular/angular/pull/17677
  9654. const _shadowDeepSelectors = /(?:>>>)|(?:\/deep\/)|(?:::ng-deep)/g;
  9655. const _selectorReSuffix = '([>\\s~+\[.,{:][\\s\\S]*)?$';
  9656. const _polyfillHostRe = /-shadowcsshost/gim;
  9657. const _colonHostRe = /:host/gim;
  9658. const _colonHostContextRe = /:host-context/gim;
  9659. const _commentRe = /\/\*\s*[\s\S]*?\*\//g;
  9660. function stripComments(input) {
  9661. return input.replace(_commentRe, '');
  9662. }
  9663. const _commentWithHashRe = /\/\*\s*#\s*source(Mapping)?URL=[\s\S]+?\*\//g;
  9664. function extractCommentsWithHash(input) {
  9665. return input.match(_commentWithHashRe) || [];
  9666. }
  9667. const BLOCK_PLACEHOLDER = '%BLOCK%';
  9668. const QUOTE_PLACEHOLDER = '%QUOTED%';
  9669. const _ruleRe = /(\s*)([^;\{\}]+?)(\s*)((?:{%BLOCK%}?\s*;?)|(?:\s*;))/g;
  9670. const _quotedRe = /%QUOTED%/g;
  9671. const CONTENT_PAIRS = new Map([['{', '}']]);
  9672. const QUOTE_PAIRS = new Map([[`"`, `"`], [`'`, `'`]]);
  9673. class CssRule {
  9674. constructor(selector, content) {
  9675. this.selector = selector;
  9676. this.content = content;
  9677. }
  9678. }
  9679. function processRules(input, ruleCallback) {
  9680. const inputWithEscapedQuotes = escapeBlocks(input, QUOTE_PAIRS, QUOTE_PLACEHOLDER);
  9681. const inputWithEscapedBlocks = escapeBlocks(inputWithEscapedQuotes.escapedString, CONTENT_PAIRS, BLOCK_PLACEHOLDER);
  9682. let nextBlockIndex = 0;
  9683. let nextQuoteIndex = 0;
  9684. return inputWithEscapedBlocks.escapedString
  9685. .replace(_ruleRe, (...m) => {
  9686. const selector = m[2];
  9687. let content = '';
  9688. let suffix = m[4];
  9689. let contentPrefix = '';
  9690. if (suffix && suffix.startsWith('{' + BLOCK_PLACEHOLDER)) {
  9691. content = inputWithEscapedBlocks.blocks[nextBlockIndex++];
  9692. suffix = suffix.substring(BLOCK_PLACEHOLDER.length + 1);
  9693. contentPrefix = '{';
  9694. }
  9695. const rule = ruleCallback(new CssRule(selector, content));
  9696. return `${m[1]}${rule.selector}${m[3]}${contentPrefix}${rule.content}${suffix}`;
  9697. })
  9698. .replace(_quotedRe, () => inputWithEscapedQuotes.blocks[nextQuoteIndex++]);
  9699. }
  9700. class StringWithEscapedBlocks {
  9701. constructor(escapedString, blocks) {
  9702. this.escapedString = escapedString;
  9703. this.blocks = blocks;
  9704. }
  9705. }
  9706. function escapeBlocks(input, charPairs, placeholder) {
  9707. const resultParts = [];
  9708. const escapedBlocks = [];
  9709. let openCharCount = 0;
  9710. let nonBlockStartIndex = 0;
  9711. let blockStartIndex = -1;
  9712. let openChar;
  9713. let closeChar;
  9714. for (let i = 0; i < input.length; i++) {
  9715. const char = input[i];
  9716. if (char === '\\') {
  9717. i++;
  9718. }
  9719. else if (char === closeChar) {
  9720. openCharCount--;
  9721. if (openCharCount === 0) {
  9722. escapedBlocks.push(input.substring(blockStartIndex, i));
  9723. resultParts.push(placeholder);
  9724. nonBlockStartIndex = i;
  9725. blockStartIndex = -1;
  9726. openChar = closeChar = undefined;
  9727. }
  9728. }
  9729. else if (char === openChar) {
  9730. openCharCount++;
  9731. }
  9732. else if (openCharCount === 0 && charPairs.has(char)) {
  9733. openChar = char;
  9734. closeChar = charPairs.get(char);
  9735. openCharCount = 1;
  9736. blockStartIndex = i + 1;
  9737. resultParts.push(input.substring(nonBlockStartIndex, blockStartIndex));
  9738. }
  9739. }
  9740. if (blockStartIndex !== -1) {
  9741. escapedBlocks.push(input.substring(blockStartIndex));
  9742. resultParts.push(placeholder);
  9743. }
  9744. else {
  9745. resultParts.push(input.substring(nonBlockStartIndex));
  9746. }
  9747. return new StringWithEscapedBlocks(resultParts.join(''), escapedBlocks);
  9748. }
  9749. /**
  9750. * Combine the `contextSelectors` with the `hostMarker` and the `otherSelectors`
  9751. * to create a selector that matches the same as `:host-context()`.
  9752. *
  9753. * Given a single context selector `A` we need to output selectors that match on the host and as an
  9754. * ancestor of the host:
  9755. *
  9756. * ```
  9757. * A <hostMarker>, A<hostMarker> {}
  9758. * ```
  9759. *
  9760. * When there is more than one context selector we also have to create combinations of those
  9761. * selectors with each other. For example if there are `A` and `B` selectors the output is:
  9762. *
  9763. * ```
  9764. * AB<hostMarker>, AB <hostMarker>, A B<hostMarker>,
  9765. * B A<hostMarker>, A B <hostMarker>, B A <hostMarker> {}
  9766. * ```
  9767. *
  9768. * And so on...
  9769. *
  9770. * @param hostMarker the string that selects the host element.
  9771. * @param contextSelectors an array of context selectors that will be combined.
  9772. * @param otherSelectors the rest of the selectors that are not context selectors.
  9773. */
  9774. function combineHostContextSelectors(contextSelectors, otherSelectors) {
  9775. const hostMarker = _polyfillHostNoCombinator;
  9776. _polyfillHostRe.lastIndex = 0; // reset the regex to ensure we get an accurate test
  9777. const otherSelectorsHasHost = _polyfillHostRe.test(otherSelectors);
  9778. // If there are no context selectors then just output a host marker
  9779. if (contextSelectors.length === 0) {
  9780. return hostMarker + otherSelectors;
  9781. }
  9782. const combined = [contextSelectors.pop() || ''];
  9783. while (contextSelectors.length > 0) {
  9784. const length = combined.length;
  9785. const contextSelector = contextSelectors.pop();
  9786. for (let i = 0; i < length; i++) {
  9787. const previousSelectors = combined[i];
  9788. // Add the new selector as a descendant of the previous selectors
  9789. combined[length * 2 + i] = previousSelectors + ' ' + contextSelector;
  9790. // Add the new selector as an ancestor of the previous selectors
  9791. combined[length + i] = contextSelector + ' ' + previousSelectors;
  9792. // Add the new selector to act on the same element as the previous selectors
  9793. combined[i] = contextSelector + previousSelectors;
  9794. }
  9795. }
  9796. // Finally connect the selector to the `hostMarker`s: either acting directly on the host
  9797. // (A<hostMarker>) or as an ancestor (A <hostMarker>).
  9798. return combined
  9799. .map(s => otherSelectorsHasHost ?
  9800. `${s}${otherSelectors}` :
  9801. `${s}${hostMarker}${otherSelectors}, ${s} ${hostMarker}${otherSelectors}`)
  9802. .join(',');
  9803. }
  9804. /**
  9805. * Mutate the given `groups` array so that there are `multiples` clones of the original array
  9806. * stored.
  9807. *
  9808. * For example `repeatGroups([a, b], 3)` will result in `[a, b, a, b, a, b]` - but importantly the
  9809. * newly added groups will be clones of the original.
  9810. *
  9811. * @param groups An array of groups of strings that will be repeated. This array is mutated
  9812. * in-place.
  9813. * @param multiples The number of times the current groups should appear.
  9814. */
  9815. function repeatGroups(groups, multiples) {
  9816. const length = groups.length;
  9817. for (let i = 1; i < multiples; i++) {
  9818. for (let j = 0; j < length; j++) {
  9819. groups[j + (i * length)] = groups[j].slice(0);
  9820. }
  9821. }
  9822. }
  9823. /**
  9824. * @license
  9825. * Copyright Google LLC All Rights Reserved.
  9826. *
  9827. * Use of this source code is governed by an MIT-style license that can be
  9828. * found in the LICENSE file at https://angular.io/license
  9829. */
  9830. const COMPONENT_VARIABLE = '%COMP%';
  9831. const HOST_ATTR = `_nghost-${COMPONENT_VARIABLE}`;
  9832. const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
  9833. class StylesCompileDependency {
  9834. constructor(name, moduleUrl, setValue) {
  9835. this.name = name;
  9836. this.moduleUrl = moduleUrl;
  9837. this.setValue = setValue;
  9838. }
  9839. }
  9840. class CompiledStylesheet {
  9841. constructor(outputCtx, stylesVar, dependencies, isShimmed, meta) {
  9842. this.outputCtx = outputCtx;
  9843. this.stylesVar = stylesVar;
  9844. this.dependencies = dependencies;
  9845. this.isShimmed = isShimmed;
  9846. this.meta = meta;
  9847. }
  9848. }
  9849. class StyleCompiler {
  9850. constructor(_urlResolver) {
  9851. this._urlResolver = _urlResolver;
  9852. this._shadowCss = new ShadowCss();
  9853. }
  9854. compileComponent(outputCtx, comp) {
  9855. const template = comp.template;
  9856. return this._compileStyles(outputCtx, comp, new CompileStylesheetMetadata({
  9857. styles: template.styles,
  9858. styleUrls: template.styleUrls,
  9859. moduleUrl: identifierModuleUrl(comp.type)
  9860. }), this.needsStyleShim(comp), true);
  9861. }
  9862. compileStyles(outputCtx, comp, stylesheet, shim = this.needsStyleShim(comp)) {
  9863. return this._compileStyles(outputCtx, comp, stylesheet, shim, false);
  9864. }
  9865. needsStyleShim(comp) {
  9866. return comp.template.encapsulation === ViewEncapsulation.Emulated;
  9867. }
  9868. _compileStyles(outputCtx, comp, stylesheet, shim, isComponentStylesheet) {
  9869. const styleExpressions = stylesheet.styles.map(plainStyle => literal(this._shimIfNeeded(plainStyle, shim)));
  9870. const dependencies = [];
  9871. stylesheet.styleUrls.forEach((styleUrl) => {
  9872. const exprIndex = styleExpressions.length;
  9873. // Note: This placeholder will be filled later.
  9874. styleExpressions.push(null);
  9875. dependencies.push(new StylesCompileDependency(getStylesVarName(null), styleUrl, (value) => styleExpressions[exprIndex] = outputCtx.importExpr(value)));
  9876. });
  9877. // styles variable contains plain strings and arrays of other styles arrays (recursive),
  9878. // so we set its type to dynamic.
  9879. const stylesVar = getStylesVarName(isComponentStylesheet ? comp : null);
  9880. const stmt = variable(stylesVar)
  9881. .set(literalArr(styleExpressions, new ArrayType(DYNAMIC_TYPE, [TypeModifier.Const])))
  9882. .toDeclStmt(null, isComponentStylesheet ? [StmtModifier.Final] : [
  9883. StmtModifier.Final, StmtModifier.Exported
  9884. ]);
  9885. outputCtx.statements.push(stmt);
  9886. return new CompiledStylesheet(outputCtx, stylesVar, dependencies, shim, stylesheet);
  9887. }
  9888. _shimIfNeeded(style, shim) {
  9889. return shim ? this._shadowCss.shimCssText(style, CONTENT_ATTR, HOST_ATTR) : style;
  9890. }
  9891. }
  9892. function getStylesVarName(component) {
  9893. let result = `styles`;
  9894. if (component) {
  9895. result += `_${identifierName(component.type)}`;
  9896. }
  9897. return result;
  9898. }
  9899. /**
  9900. * @license
  9901. * Copyright Google LLC All Rights Reserved.
  9902. *
  9903. * Use of this source code is governed by an MIT-style license that can be
  9904. * found in the LICENSE file at https://angular.io/license
  9905. */
  9906. /**
  9907. * A path is an ordered set of elements. Typically a path is to a
  9908. * particular offset in a source file. The head of the list is the top
  9909. * most node. The tail is the node that contains the offset directly.
  9910. *
  9911. * For example, the expression `a + b + c` might have an ast that looks
  9912. * like:
  9913. * +
  9914. * / \
  9915. * a +
  9916. * / \
  9917. * b c
  9918. *
  9919. * The path to the node at offset 9 would be `['+' at 1-10, '+' at 7-10,
  9920. * 'c' at 9-10]` and the path the node at offset 1 would be
  9921. * `['+' at 1-10, 'a' at 1-2]`.
  9922. */
  9923. class AstPath {
  9924. constructor(path, position = -1) {
  9925. this.path = path;
  9926. this.position = position;
  9927. }
  9928. get empty() {
  9929. return !this.path || !this.path.length;
  9930. }
  9931. get head() {
  9932. return this.path[0];
  9933. }
  9934. get tail() {
  9935. return this.path[this.path.length - 1];
  9936. }
  9937. parentOf(node) {
  9938. return node && this.path[this.path.indexOf(node) - 1];
  9939. }
  9940. childOf(node) {
  9941. return this.path[this.path.indexOf(node) + 1];
  9942. }
  9943. first(ctor) {
  9944. for (let i = this.path.length - 1; i >= 0; i--) {
  9945. let item = this.path[i];
  9946. if (item instanceof ctor)
  9947. return item;
  9948. }
  9949. }
  9950. push(node) {
  9951. this.path.push(node);
  9952. }
  9953. pop() {
  9954. return this.path.pop();
  9955. }
  9956. }
  9957. /**
  9958. * @license
  9959. * Copyright Google LLC All Rights Reserved.
  9960. *
  9961. * Use of this source code is governed by an MIT-style license that can be
  9962. * found in the LICENSE file at https://angular.io/license
  9963. */
  9964. class NodeWithI18n {
  9965. constructor(sourceSpan, i18n) {
  9966. this.sourceSpan = sourceSpan;
  9967. this.i18n = i18n;
  9968. }
  9969. }
  9970. class Text$3 extends NodeWithI18n {
  9971. constructor(value, sourceSpan, i18n) {
  9972. super(sourceSpan, i18n);
  9973. this.value = value;
  9974. }
  9975. visit(visitor, context) {
  9976. return visitor.visitText(this, context);
  9977. }
  9978. }
  9979. class Expansion extends NodeWithI18n {
  9980. constructor(switchValue, type, cases, sourceSpan, switchValueSourceSpan, i18n) {
  9981. super(sourceSpan, i18n);
  9982. this.switchValue = switchValue;
  9983. this.type = type;
  9984. this.cases = cases;
  9985. this.switchValueSourceSpan = switchValueSourceSpan;
  9986. }
  9987. visit(visitor, context) {
  9988. return visitor.visitExpansion(this, context);
  9989. }
  9990. }
  9991. class ExpansionCase {
  9992. constructor(value, expression, sourceSpan, valueSourceSpan, expSourceSpan) {
  9993. this.value = value;
  9994. this.expression = expression;
  9995. this.sourceSpan = sourceSpan;
  9996. this.valueSourceSpan = valueSourceSpan;
  9997. this.expSourceSpan = expSourceSpan;
  9998. }
  9999. visit(visitor, context) {
  10000. return visitor.visitExpansionCase(this, context);
  10001. }
  10002. }
  10003. class Attribute extends NodeWithI18n {
  10004. constructor(name, value, sourceSpan, keySpan, valueSpan, i18n) {
  10005. super(sourceSpan, i18n);
  10006. this.name = name;
  10007. this.value = value;
  10008. this.keySpan = keySpan;
  10009. this.valueSpan = valueSpan;
  10010. }
  10011. visit(visitor, context) {
  10012. return visitor.visitAttribute(this, context);
  10013. }
  10014. }
  10015. class Element$1 extends NodeWithI18n {
  10016. constructor(name, attrs, children, sourceSpan, startSourceSpan, endSourceSpan = null, i18n) {
  10017. super(sourceSpan, i18n);
  10018. this.name = name;
  10019. this.attrs = attrs;
  10020. this.children = children;
  10021. this.startSourceSpan = startSourceSpan;
  10022. this.endSourceSpan = endSourceSpan;
  10023. }
  10024. visit(visitor, context) {
  10025. return visitor.visitElement(this, context);
  10026. }
  10027. }
  10028. class Comment$1 {
  10029. constructor(value, sourceSpan) {
  10030. this.value = value;
  10031. this.sourceSpan = sourceSpan;
  10032. }
  10033. visit(visitor, context) {
  10034. return visitor.visitComment(this, context);
  10035. }
  10036. }
  10037. function visitAll$1(visitor, nodes, context = null) {
  10038. const result = [];
  10039. const visit = visitor.visit ?
  10040. (ast) => visitor.visit(ast, context) || ast.visit(visitor, context) :
  10041. (ast) => ast.visit(visitor, context);
  10042. nodes.forEach(ast => {
  10043. const astResult = visit(ast);
  10044. if (astResult) {
  10045. result.push(astResult);
  10046. }
  10047. });
  10048. return result;
  10049. }
  10050. class RecursiveVisitor$1 {
  10051. constructor() { }
  10052. visitElement(ast, context) {
  10053. this.visitChildren(context, visit => {
  10054. visit(ast.attrs);
  10055. visit(ast.children);
  10056. });
  10057. }
  10058. visitAttribute(ast, context) { }
  10059. visitText(ast, context) { }
  10060. visitComment(ast, context) { }
  10061. visitExpansion(ast, context) {
  10062. return this.visitChildren(context, visit => {
  10063. visit(ast.cases);
  10064. });
  10065. }
  10066. visitExpansionCase(ast, context) { }
  10067. visitChildren(context, cb) {
  10068. let results = [];
  10069. let t = this;
  10070. function visit(children) {
  10071. if (children)
  10072. results.push(visitAll$1(t, children, context));
  10073. }
  10074. cb(visit);
  10075. return Array.prototype.concat.apply([], results);
  10076. }
  10077. }
  10078. function spanOf(ast) {
  10079. const start = ast.sourceSpan.start.offset;
  10080. let end = ast.sourceSpan.end.offset;
  10081. if (ast instanceof Element$1) {
  10082. if (ast.endSourceSpan) {
  10083. end = ast.endSourceSpan.end.offset;
  10084. }
  10085. else if (ast.children && ast.children.length) {
  10086. end = spanOf(ast.children[ast.children.length - 1]).end;
  10087. }
  10088. }
  10089. return { start, end };
  10090. }
  10091. function findNode(nodes, position) {
  10092. const path = [];
  10093. const visitor = new class extends RecursiveVisitor$1 {
  10094. visit(ast, context) {
  10095. const span = spanOf(ast);
  10096. if (span.start <= position && position < span.end) {
  10097. path.push(ast);
  10098. }
  10099. else {
  10100. // Returning a value here will result in the children being skipped.
  10101. return true;
  10102. }
  10103. }
  10104. };
  10105. visitAll$1(visitor, nodes);
  10106. return new AstPath(path, position);
  10107. }
  10108. /**
  10109. * @license
  10110. * Copyright Google LLC All Rights Reserved.
  10111. *
  10112. * Use of this source code is governed by an MIT-style license that can be
  10113. * found in the LICENSE file at https://angular.io/license
  10114. */
  10115. var TokenType;
  10116. (function (TokenType) {
  10117. TokenType[TokenType["TAG_OPEN_START"] = 0] = "TAG_OPEN_START";
  10118. TokenType[TokenType["TAG_OPEN_END"] = 1] = "TAG_OPEN_END";
  10119. TokenType[TokenType["TAG_OPEN_END_VOID"] = 2] = "TAG_OPEN_END_VOID";
  10120. TokenType[TokenType["TAG_CLOSE"] = 3] = "TAG_CLOSE";
  10121. TokenType[TokenType["INCOMPLETE_TAG_OPEN"] = 4] = "INCOMPLETE_TAG_OPEN";
  10122. TokenType[TokenType["TEXT"] = 5] = "TEXT";
  10123. TokenType[TokenType["ESCAPABLE_RAW_TEXT"] = 6] = "ESCAPABLE_RAW_TEXT";
  10124. TokenType[TokenType["RAW_TEXT"] = 7] = "RAW_TEXT";
  10125. TokenType[TokenType["COMMENT_START"] = 8] = "COMMENT_START";
  10126. TokenType[TokenType["COMMENT_END"] = 9] = "COMMENT_END";
  10127. TokenType[TokenType["CDATA_START"] = 10] = "CDATA_START";
  10128. TokenType[TokenType["CDATA_END"] = 11] = "CDATA_END";
  10129. TokenType[TokenType["ATTR_NAME"] = 12] = "ATTR_NAME";
  10130. TokenType[TokenType["ATTR_QUOTE"] = 13] = "ATTR_QUOTE";
  10131. TokenType[TokenType["ATTR_VALUE"] = 14] = "ATTR_VALUE";
  10132. TokenType[TokenType["DOC_TYPE"] = 15] = "DOC_TYPE";
  10133. TokenType[TokenType["EXPANSION_FORM_START"] = 16] = "EXPANSION_FORM_START";
  10134. TokenType[TokenType["EXPANSION_CASE_VALUE"] = 17] = "EXPANSION_CASE_VALUE";
  10135. TokenType[TokenType["EXPANSION_CASE_EXP_START"] = 18] = "EXPANSION_CASE_EXP_START";
  10136. TokenType[TokenType["EXPANSION_CASE_EXP_END"] = 19] = "EXPANSION_CASE_EXP_END";
  10137. TokenType[TokenType["EXPANSION_FORM_END"] = 20] = "EXPANSION_FORM_END";
  10138. TokenType[TokenType["EOF"] = 21] = "EOF";
  10139. })(TokenType || (TokenType = {}));
  10140. class Token {
  10141. constructor(type, parts, sourceSpan) {
  10142. this.type = type;
  10143. this.parts = parts;
  10144. this.sourceSpan = sourceSpan;
  10145. }
  10146. }
  10147. class TokenError extends ParseError {
  10148. constructor(errorMsg, tokenType, span) {
  10149. super(span, errorMsg);
  10150. this.tokenType = tokenType;
  10151. }
  10152. }
  10153. class TokenizeResult {
  10154. constructor(tokens, errors, nonNormalizedIcuExpressions) {
  10155. this.tokens = tokens;
  10156. this.errors = errors;
  10157. this.nonNormalizedIcuExpressions = nonNormalizedIcuExpressions;
  10158. }
  10159. }
  10160. function tokenize(source, url, getTagDefinition, options = {}) {
  10161. const tokenizer = new _Tokenizer(new ParseSourceFile(source, url), getTagDefinition, options);
  10162. tokenizer.tokenize();
  10163. return new TokenizeResult(mergeTextTokens(tokenizer.tokens), tokenizer.errors, tokenizer.nonNormalizedIcuExpressions);
  10164. }
  10165. const _CR_OR_CRLF_REGEXP = /\r\n?/g;
  10166. function _unexpectedCharacterErrorMsg(charCode) {
  10167. const char = charCode === $EOF ? 'EOF' : String.fromCharCode(charCode);
  10168. return `Unexpected character "${char}"`;
  10169. }
  10170. function _unknownEntityErrorMsg(entitySrc) {
  10171. return `Unknown entity "${entitySrc}" - use the "&#<decimal>;" or "&#x<hex>;" syntax`;
  10172. }
  10173. function _unparsableEntityErrorMsg(type, entityStr) {
  10174. return `Unable to parse entity "${entityStr}" - ${type} character reference entities must end with ";"`;
  10175. }
  10176. var CharacterReferenceType;
  10177. (function (CharacterReferenceType) {
  10178. CharacterReferenceType["HEX"] = "hexadecimal";
  10179. CharacterReferenceType["DEC"] = "decimal";
  10180. })(CharacterReferenceType || (CharacterReferenceType = {}));
  10181. class _ControlFlowError {
  10182. constructor(error) {
  10183. this.error = error;
  10184. }
  10185. }
  10186. // See https://www.w3.org/TR/html51/syntax.html#writing-html-documents
  10187. class _Tokenizer {
  10188. /**
  10189. * @param _file The html source file being tokenized.
  10190. * @param _getTagDefinition A function that will retrieve a tag definition for a given tag name.
  10191. * @param options Configuration of the tokenization.
  10192. */
  10193. constructor(_file, _getTagDefinition, options) {
  10194. this._getTagDefinition = _getTagDefinition;
  10195. this._currentTokenStart = null;
  10196. this._currentTokenType = null;
  10197. this._expansionCaseStack = [];
  10198. this._inInterpolation = false;
  10199. this.tokens = [];
  10200. this.errors = [];
  10201. this.nonNormalizedIcuExpressions = [];
  10202. this._tokenizeIcu = options.tokenizeExpansionForms || false;
  10203. this._interpolationConfig = options.interpolationConfig || DEFAULT_INTERPOLATION_CONFIG;
  10204. this._leadingTriviaCodePoints =
  10205. options.leadingTriviaChars && options.leadingTriviaChars.map(c => c.codePointAt(0) || 0);
  10206. const range = options.range || { endPos: _file.content.length, startPos: 0, startLine: 0, startCol: 0 };
  10207. this._cursor = options.escapedString ? new EscapedCharacterCursor(_file, range) :
  10208. new PlainCharacterCursor(_file, range);
  10209. this._preserveLineEndings = options.preserveLineEndings || false;
  10210. this._escapedString = options.escapedString || false;
  10211. this._i18nNormalizeLineEndingsInICUs = options.i18nNormalizeLineEndingsInICUs || false;
  10212. try {
  10213. this._cursor.init();
  10214. }
  10215. catch (e) {
  10216. this.handleError(e);
  10217. }
  10218. }
  10219. _processCarriageReturns(content) {
  10220. if (this._preserveLineEndings) {
  10221. return content;
  10222. }
  10223. // https://www.w3.org/TR/html51/syntax.html#preprocessing-the-input-stream
  10224. // In order to keep the original position in the source, we can not
  10225. // pre-process it.
  10226. // Instead CRs are processed right before instantiating the tokens.
  10227. return content.replace(_CR_OR_CRLF_REGEXP, '\n');
  10228. }
  10229. tokenize() {
  10230. while (this._cursor.peek() !== $EOF) {
  10231. const start = this._cursor.clone();
  10232. try {
  10233. if (this._attemptCharCode($LT)) {
  10234. if (this._attemptCharCode($BANG)) {
  10235. if (this._attemptCharCode($LBRACKET)) {
  10236. this._consumeCdata(start);
  10237. }
  10238. else if (this._attemptCharCode($MINUS)) {
  10239. this._consumeComment(start);
  10240. }
  10241. else {
  10242. this._consumeDocType(start);
  10243. }
  10244. }
  10245. else if (this._attemptCharCode($SLASH)) {
  10246. this._consumeTagClose(start);
  10247. }
  10248. else {
  10249. this._consumeTagOpen(start);
  10250. }
  10251. }
  10252. else if (!(this._tokenizeIcu && this._tokenizeExpansionForm())) {
  10253. this._consumeText();
  10254. }
  10255. }
  10256. catch (e) {
  10257. this.handleError(e);
  10258. }
  10259. }
  10260. this._beginToken(TokenType.EOF);
  10261. this._endToken([]);
  10262. }
  10263. /**
  10264. * @returns whether an ICU token has been created
  10265. * @internal
  10266. */
  10267. _tokenizeExpansionForm() {
  10268. if (this.isExpansionFormStart()) {
  10269. this._consumeExpansionFormStart();
  10270. return true;
  10271. }
  10272. if (isExpansionCaseStart(this._cursor.peek()) && this._isInExpansionForm()) {
  10273. this._consumeExpansionCaseStart();
  10274. return true;
  10275. }
  10276. if (this._cursor.peek() === $RBRACE) {
  10277. if (this._isInExpansionCase()) {
  10278. this._consumeExpansionCaseEnd();
  10279. return true;
  10280. }
  10281. if (this._isInExpansionForm()) {
  10282. this._consumeExpansionFormEnd();
  10283. return true;
  10284. }
  10285. }
  10286. return false;
  10287. }
  10288. _beginToken(type, start = this._cursor.clone()) {
  10289. this._currentTokenStart = start;
  10290. this._currentTokenType = type;
  10291. }
  10292. _endToken(parts, end) {
  10293. if (this._currentTokenStart === null) {
  10294. throw new TokenError('Programming error - attempted to end a token when there was no start to the token', this._currentTokenType, this._cursor.getSpan(end));
  10295. }
  10296. if (this._currentTokenType === null) {
  10297. throw new TokenError('Programming error - attempted to end a token which has no token type', null, this._cursor.getSpan(this._currentTokenStart));
  10298. }
  10299. const token = new Token(this._currentTokenType, parts, this._cursor.getSpan(this._currentTokenStart, this._leadingTriviaCodePoints));
  10300. this.tokens.push(token);
  10301. this._currentTokenStart = null;
  10302. this._currentTokenType = null;
  10303. return token;
  10304. }
  10305. _createError(msg, span) {
  10306. if (this._isInExpansionForm()) {
  10307. msg += ` (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)`;
  10308. }
  10309. const error = new TokenError(msg, this._currentTokenType, span);
  10310. this._currentTokenStart = null;
  10311. this._currentTokenType = null;
  10312. return new _ControlFlowError(error);
  10313. }
  10314. handleError(e) {
  10315. if (e instanceof CursorError) {
  10316. e = this._createError(e.msg, this._cursor.getSpan(e.cursor));
  10317. }
  10318. if (e instanceof _ControlFlowError) {
  10319. this.errors.push(e.error);
  10320. }
  10321. else {
  10322. throw e;
  10323. }
  10324. }
  10325. _attemptCharCode(charCode) {
  10326. if (this._cursor.peek() === charCode) {
  10327. this._cursor.advance();
  10328. return true;
  10329. }
  10330. return false;
  10331. }
  10332. _attemptCharCodeCaseInsensitive(charCode) {
  10333. if (compareCharCodeCaseInsensitive(this._cursor.peek(), charCode)) {
  10334. this._cursor.advance();
  10335. return true;
  10336. }
  10337. return false;
  10338. }
  10339. _requireCharCode(charCode) {
  10340. const location = this._cursor.clone();
  10341. if (!this._attemptCharCode(charCode)) {
  10342. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  10343. }
  10344. }
  10345. _attemptStr(chars) {
  10346. const len = chars.length;
  10347. if (this._cursor.charsLeft() < len) {
  10348. return false;
  10349. }
  10350. const initialPosition = this._cursor.clone();
  10351. for (let i = 0; i < len; i++) {
  10352. if (!this._attemptCharCode(chars.charCodeAt(i))) {
  10353. // If attempting to parse the string fails, we want to reset the parser
  10354. // to where it was before the attempt
  10355. this._cursor = initialPosition;
  10356. return false;
  10357. }
  10358. }
  10359. return true;
  10360. }
  10361. _attemptStrCaseInsensitive(chars) {
  10362. for (let i = 0; i < chars.length; i++) {
  10363. if (!this._attemptCharCodeCaseInsensitive(chars.charCodeAt(i))) {
  10364. return false;
  10365. }
  10366. }
  10367. return true;
  10368. }
  10369. _requireStr(chars) {
  10370. const location = this._cursor.clone();
  10371. if (!this._attemptStr(chars)) {
  10372. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(location));
  10373. }
  10374. }
  10375. _attemptCharCodeUntilFn(predicate) {
  10376. while (!predicate(this._cursor.peek())) {
  10377. this._cursor.advance();
  10378. }
  10379. }
  10380. _requireCharCodeUntilFn(predicate, len) {
  10381. const start = this._cursor.clone();
  10382. this._attemptCharCodeUntilFn(predicate);
  10383. if (this._cursor.diff(start) < len) {
  10384. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  10385. }
  10386. }
  10387. _attemptUntilChar(char) {
  10388. while (this._cursor.peek() !== char) {
  10389. this._cursor.advance();
  10390. }
  10391. }
  10392. _readChar(decodeEntities) {
  10393. if (decodeEntities && this._cursor.peek() === $AMPERSAND) {
  10394. return this._decodeEntity();
  10395. }
  10396. else {
  10397. // Don't rely upon reading directly from `_input` as the actual char value
  10398. // may have been generated from an escape sequence.
  10399. const char = String.fromCodePoint(this._cursor.peek());
  10400. this._cursor.advance();
  10401. return char;
  10402. }
  10403. }
  10404. _decodeEntity() {
  10405. const start = this._cursor.clone();
  10406. this._cursor.advance();
  10407. if (this._attemptCharCode($HASH)) {
  10408. const isHex = this._attemptCharCode($x) || this._attemptCharCode($X);
  10409. const codeStart = this._cursor.clone();
  10410. this._attemptCharCodeUntilFn(isDigitEntityEnd);
  10411. if (this._cursor.peek() != $SEMICOLON) {
  10412. // Advance cursor to include the peeked character in the string provided to the error
  10413. // message.
  10414. this._cursor.advance();
  10415. const entityType = isHex ? CharacterReferenceType.HEX : CharacterReferenceType.DEC;
  10416. throw this._createError(_unparsableEntityErrorMsg(entityType, this._cursor.getChars(start)), this._cursor.getSpan());
  10417. }
  10418. const strNum = this._cursor.getChars(codeStart);
  10419. this._cursor.advance();
  10420. try {
  10421. const charCode = parseInt(strNum, isHex ? 16 : 10);
  10422. return String.fromCharCode(charCode);
  10423. }
  10424. catch (_a) {
  10425. throw this._createError(_unknownEntityErrorMsg(this._cursor.getChars(start)), this._cursor.getSpan());
  10426. }
  10427. }
  10428. else {
  10429. const nameStart = this._cursor.clone();
  10430. this._attemptCharCodeUntilFn(isNamedEntityEnd);
  10431. if (this._cursor.peek() != $SEMICOLON) {
  10432. this._cursor = nameStart;
  10433. return '&';
  10434. }
  10435. const name = this._cursor.getChars(nameStart);
  10436. this._cursor.advance();
  10437. const char = NAMED_ENTITIES[name];
  10438. if (!char) {
  10439. throw this._createError(_unknownEntityErrorMsg(name), this._cursor.getSpan(start));
  10440. }
  10441. return char;
  10442. }
  10443. }
  10444. _consumeRawText(decodeEntities, endMarkerPredicate) {
  10445. this._beginToken(decodeEntities ? TokenType.ESCAPABLE_RAW_TEXT : TokenType.RAW_TEXT);
  10446. const parts = [];
  10447. while (true) {
  10448. const tagCloseStart = this._cursor.clone();
  10449. const foundEndMarker = endMarkerPredicate();
  10450. this._cursor = tagCloseStart;
  10451. if (foundEndMarker) {
  10452. break;
  10453. }
  10454. parts.push(this._readChar(decodeEntities));
  10455. }
  10456. return this._endToken([this._processCarriageReturns(parts.join(''))]);
  10457. }
  10458. _consumeComment(start) {
  10459. this._beginToken(TokenType.COMMENT_START, start);
  10460. this._requireCharCode($MINUS);
  10461. this._endToken([]);
  10462. this._consumeRawText(false, () => this._attemptStr('-->'));
  10463. this._beginToken(TokenType.COMMENT_END);
  10464. this._requireStr('-->');
  10465. this._endToken([]);
  10466. }
  10467. _consumeCdata(start) {
  10468. this._beginToken(TokenType.CDATA_START, start);
  10469. this._requireStr('CDATA[');
  10470. this._endToken([]);
  10471. this._consumeRawText(false, () => this._attemptStr(']]>'));
  10472. this._beginToken(TokenType.CDATA_END);
  10473. this._requireStr(']]>');
  10474. this._endToken([]);
  10475. }
  10476. _consumeDocType(start) {
  10477. this._beginToken(TokenType.DOC_TYPE, start);
  10478. const contentStart = this._cursor.clone();
  10479. this._attemptUntilChar($GT);
  10480. const content = this._cursor.getChars(contentStart);
  10481. this._cursor.advance();
  10482. this._endToken([content]);
  10483. }
  10484. _consumePrefixAndName() {
  10485. const nameOrPrefixStart = this._cursor.clone();
  10486. let prefix = '';
  10487. while (this._cursor.peek() !== $COLON && !isPrefixEnd(this._cursor.peek())) {
  10488. this._cursor.advance();
  10489. }
  10490. let nameStart;
  10491. if (this._cursor.peek() === $COLON) {
  10492. prefix = this._cursor.getChars(nameOrPrefixStart);
  10493. this._cursor.advance();
  10494. nameStart = this._cursor.clone();
  10495. }
  10496. else {
  10497. nameStart = nameOrPrefixStart;
  10498. }
  10499. this._requireCharCodeUntilFn(isNameEnd, prefix === '' ? 0 : 1);
  10500. const name = this._cursor.getChars(nameStart);
  10501. return [prefix, name];
  10502. }
  10503. _consumeTagOpen(start) {
  10504. let tagName;
  10505. let prefix;
  10506. let openTagToken;
  10507. try {
  10508. if (!isAsciiLetter(this._cursor.peek())) {
  10509. throw this._createError(_unexpectedCharacterErrorMsg(this._cursor.peek()), this._cursor.getSpan(start));
  10510. }
  10511. openTagToken = this._consumeTagOpenStart(start);
  10512. prefix = openTagToken.parts[0];
  10513. tagName = openTagToken.parts[1];
  10514. this._attemptCharCodeUntilFn(isNotWhitespace);
  10515. while (this._cursor.peek() !== $SLASH && this._cursor.peek() !== $GT &&
  10516. this._cursor.peek() !== $LT && this._cursor.peek() !== $EOF) {
  10517. this._consumeAttributeName();
  10518. this._attemptCharCodeUntilFn(isNotWhitespace);
  10519. if (this._attemptCharCode($EQ)) {
  10520. this._attemptCharCodeUntilFn(isNotWhitespace);
  10521. this._consumeAttributeValue();
  10522. }
  10523. this._attemptCharCodeUntilFn(isNotWhitespace);
  10524. }
  10525. this._consumeTagOpenEnd();
  10526. }
  10527. catch (e) {
  10528. if (e instanceof _ControlFlowError) {
  10529. if (openTagToken) {
  10530. // We errored before we could close the opening tag, so it is incomplete.
  10531. openTagToken.type = TokenType.INCOMPLETE_TAG_OPEN;
  10532. }
  10533. else {
  10534. // When the start tag is invalid, assume we want a "<" as text.
  10535. // Back to back text tokens are merged at the end.
  10536. this._beginToken(TokenType.TEXT, start);
  10537. this._endToken(['<']);
  10538. }
  10539. return;
  10540. }
  10541. throw e;
  10542. }
  10543. const contentTokenType = this._getTagDefinition(tagName).getContentType(prefix);
  10544. if (contentTokenType === TagContentType.RAW_TEXT) {
  10545. this._consumeRawTextWithTagClose(prefix, tagName, false);
  10546. }
  10547. else if (contentTokenType === TagContentType.ESCAPABLE_RAW_TEXT) {
  10548. this._consumeRawTextWithTagClose(prefix, tagName, true);
  10549. }
  10550. }
  10551. _consumeRawTextWithTagClose(prefix, tagName, decodeEntities) {
  10552. this._consumeRawText(decodeEntities, () => {
  10553. if (!this._attemptCharCode($LT))
  10554. return false;
  10555. if (!this._attemptCharCode($SLASH))
  10556. return false;
  10557. this._attemptCharCodeUntilFn(isNotWhitespace);
  10558. if (!this._attemptStrCaseInsensitive(tagName))
  10559. return false;
  10560. this._attemptCharCodeUntilFn(isNotWhitespace);
  10561. return this._attemptCharCode($GT);
  10562. });
  10563. this._beginToken(TokenType.TAG_CLOSE);
  10564. this._requireCharCodeUntilFn(code => code === $GT, 3);
  10565. this._cursor.advance(); // Consume the `>`
  10566. this._endToken([prefix, tagName]);
  10567. }
  10568. _consumeTagOpenStart(start) {
  10569. this._beginToken(TokenType.TAG_OPEN_START, start);
  10570. const parts = this._consumePrefixAndName();
  10571. return this._endToken(parts);
  10572. }
  10573. _consumeAttributeName() {
  10574. const attrNameStart = this._cursor.peek();
  10575. if (attrNameStart === $SQ || attrNameStart === $DQ) {
  10576. throw this._createError(_unexpectedCharacterErrorMsg(attrNameStart), this._cursor.getSpan());
  10577. }
  10578. this._beginToken(TokenType.ATTR_NAME);
  10579. const prefixAndName = this._consumePrefixAndName();
  10580. this._endToken(prefixAndName);
  10581. }
  10582. _consumeAttributeValue() {
  10583. let value;
  10584. if (this._cursor.peek() === $SQ || this._cursor.peek() === $DQ) {
  10585. this._beginToken(TokenType.ATTR_QUOTE);
  10586. const quoteChar = this._cursor.peek();
  10587. this._cursor.advance();
  10588. this._endToken([String.fromCodePoint(quoteChar)]);
  10589. this._beginToken(TokenType.ATTR_VALUE);
  10590. const parts = [];
  10591. while (this._cursor.peek() !== quoteChar) {
  10592. parts.push(this._readChar(true));
  10593. }
  10594. value = parts.join('');
  10595. this._endToken([this._processCarriageReturns(value)]);
  10596. this._beginToken(TokenType.ATTR_QUOTE);
  10597. this._cursor.advance();
  10598. this._endToken([String.fromCodePoint(quoteChar)]);
  10599. }
  10600. else {
  10601. this._beginToken(TokenType.ATTR_VALUE);
  10602. const valueStart = this._cursor.clone();
  10603. this._requireCharCodeUntilFn(isNameEnd, 1);
  10604. value = this._cursor.getChars(valueStart);
  10605. this._endToken([this._processCarriageReturns(value)]);
  10606. }
  10607. }
  10608. _consumeTagOpenEnd() {
  10609. const tokenType = this._attemptCharCode($SLASH) ? TokenType.TAG_OPEN_END_VOID : TokenType.TAG_OPEN_END;
  10610. this._beginToken(tokenType);
  10611. this._requireCharCode($GT);
  10612. this._endToken([]);
  10613. }
  10614. _consumeTagClose(start) {
  10615. this._beginToken(TokenType.TAG_CLOSE, start);
  10616. this._attemptCharCodeUntilFn(isNotWhitespace);
  10617. const prefixAndName = this._consumePrefixAndName();
  10618. this._attemptCharCodeUntilFn(isNotWhitespace);
  10619. this._requireCharCode($GT);
  10620. this._endToken(prefixAndName);
  10621. }
  10622. _consumeExpansionFormStart() {
  10623. this._beginToken(TokenType.EXPANSION_FORM_START);
  10624. this._requireCharCode($LBRACE);
  10625. this._endToken([]);
  10626. this._expansionCaseStack.push(TokenType.EXPANSION_FORM_START);
  10627. this._beginToken(TokenType.RAW_TEXT);
  10628. const condition = this._readUntil($COMMA);
  10629. const normalizedCondition = this._processCarriageReturns(condition);
  10630. if (this._i18nNormalizeLineEndingsInICUs) {
  10631. // We explicitly want to normalize line endings for this text.
  10632. this._endToken([normalizedCondition]);
  10633. }
  10634. else {
  10635. // We are not normalizing line endings.
  10636. const conditionToken = this._endToken([condition]);
  10637. if (normalizedCondition !== condition) {
  10638. this.nonNormalizedIcuExpressions.push(conditionToken);
  10639. }
  10640. }
  10641. this._requireCharCode($COMMA);
  10642. this._attemptCharCodeUntilFn(isNotWhitespace);
  10643. this._beginToken(TokenType.RAW_TEXT);
  10644. const type = this._readUntil($COMMA);
  10645. this._endToken([type]);
  10646. this._requireCharCode($COMMA);
  10647. this._attemptCharCodeUntilFn(isNotWhitespace);
  10648. }
  10649. _consumeExpansionCaseStart() {
  10650. this._beginToken(TokenType.EXPANSION_CASE_VALUE);
  10651. const value = this._readUntil($LBRACE).trim();
  10652. this._endToken([value]);
  10653. this._attemptCharCodeUntilFn(isNotWhitespace);
  10654. this._beginToken(TokenType.EXPANSION_CASE_EXP_START);
  10655. this._requireCharCode($LBRACE);
  10656. this._endToken([]);
  10657. this._attemptCharCodeUntilFn(isNotWhitespace);
  10658. this._expansionCaseStack.push(TokenType.EXPANSION_CASE_EXP_START);
  10659. }
  10660. _consumeExpansionCaseEnd() {
  10661. this._beginToken(TokenType.EXPANSION_CASE_EXP_END);
  10662. this._requireCharCode($RBRACE);
  10663. this._endToken([]);
  10664. this._attemptCharCodeUntilFn(isNotWhitespace);
  10665. this._expansionCaseStack.pop();
  10666. }
  10667. _consumeExpansionFormEnd() {
  10668. this._beginToken(TokenType.EXPANSION_FORM_END);
  10669. this._requireCharCode($RBRACE);
  10670. this._endToken([]);
  10671. this._expansionCaseStack.pop();
  10672. }
  10673. _consumeText() {
  10674. const start = this._cursor.clone();
  10675. this._beginToken(TokenType.TEXT, start);
  10676. const parts = [];
  10677. do {
  10678. if (this._interpolationConfig && this._attemptStr(this._interpolationConfig.start)) {
  10679. parts.push(this._interpolationConfig.start);
  10680. this._inInterpolation = true;
  10681. }
  10682. else if (this._interpolationConfig && this._inInterpolation &&
  10683. this._attemptStr(this._interpolationConfig.end)) {
  10684. parts.push(this._interpolationConfig.end);
  10685. this._inInterpolation = false;
  10686. }
  10687. else {
  10688. parts.push(this._readChar(true));
  10689. }
  10690. } while (!this._isTextEnd());
  10691. this._endToken([this._processCarriageReturns(parts.join(''))]);
  10692. }
  10693. _isTextEnd() {
  10694. if (this._cursor.peek() === $LT || this._cursor.peek() === $EOF) {
  10695. return true;
  10696. }
  10697. if (this._tokenizeIcu && !this._inInterpolation) {
  10698. if (this.isExpansionFormStart()) {
  10699. // start of an expansion form
  10700. return true;
  10701. }
  10702. if (this._cursor.peek() === $RBRACE && this._isInExpansionCase()) {
  10703. // end of and expansion case
  10704. return true;
  10705. }
  10706. }
  10707. return false;
  10708. }
  10709. _readUntil(char) {
  10710. const start = this._cursor.clone();
  10711. this._attemptUntilChar(char);
  10712. return this._cursor.getChars(start);
  10713. }
  10714. _isInExpansionCase() {
  10715. return this._expansionCaseStack.length > 0 &&
  10716. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  10717. TokenType.EXPANSION_CASE_EXP_START;
  10718. }
  10719. _isInExpansionForm() {
  10720. return this._expansionCaseStack.length > 0 &&
  10721. this._expansionCaseStack[this._expansionCaseStack.length - 1] ===
  10722. TokenType.EXPANSION_FORM_START;
  10723. }
  10724. isExpansionFormStart() {
  10725. if (this._cursor.peek() !== $LBRACE) {
  10726. return false;
  10727. }
  10728. if (this._interpolationConfig) {
  10729. const start = this._cursor.clone();
  10730. const isInterpolation = this._attemptStr(this._interpolationConfig.start);
  10731. this._cursor = start;
  10732. return !isInterpolation;
  10733. }
  10734. return true;
  10735. }
  10736. }
  10737. function isNotWhitespace(code) {
  10738. return !isWhitespace(code) || code === $EOF;
  10739. }
  10740. function isNameEnd(code) {
  10741. return isWhitespace(code) || code === $GT || code === $LT ||
  10742. code === $SLASH || code === $SQ || code === $DQ || code === $EQ ||
  10743. code === $EOF;
  10744. }
  10745. function isPrefixEnd(code) {
  10746. return (code < $a || $z < code) && (code < $A || $Z < code) &&
  10747. (code < $0 || code > $9);
  10748. }
  10749. function isDigitEntityEnd(code) {
  10750. return code == $SEMICOLON || code == $EOF || !isAsciiHexDigit(code);
  10751. }
  10752. function isNamedEntityEnd(code) {
  10753. return code == $SEMICOLON || code == $EOF || !isAsciiLetter(code);
  10754. }
  10755. function isExpansionCaseStart(peek) {
  10756. return peek !== $RBRACE;
  10757. }
  10758. function compareCharCodeCaseInsensitive(code1, code2) {
  10759. return toUpperCaseCharCode(code1) == toUpperCaseCharCode(code2);
  10760. }
  10761. function toUpperCaseCharCode(code) {
  10762. return code >= $a && code <= $z ? code - $a + $A : code;
  10763. }
  10764. function mergeTextTokens(srcTokens) {
  10765. const dstTokens = [];
  10766. let lastDstToken = undefined;
  10767. for (let i = 0; i < srcTokens.length; i++) {
  10768. const token = srcTokens[i];
  10769. if (lastDstToken && lastDstToken.type == TokenType.TEXT && token.type == TokenType.TEXT) {
  10770. lastDstToken.parts[0] += token.parts[0];
  10771. lastDstToken.sourceSpan.end = token.sourceSpan.end;
  10772. }
  10773. else {
  10774. lastDstToken = token;
  10775. dstTokens.push(lastDstToken);
  10776. }
  10777. }
  10778. return dstTokens;
  10779. }
  10780. class PlainCharacterCursor {
  10781. constructor(fileOrCursor, range) {
  10782. if (fileOrCursor instanceof PlainCharacterCursor) {
  10783. this.file = fileOrCursor.file;
  10784. this.input = fileOrCursor.input;
  10785. this.end = fileOrCursor.end;
  10786. const state = fileOrCursor.state;
  10787. // Note: avoid using `{...fileOrCursor.state}` here as that has a severe performance penalty.
  10788. // In ES5 bundles the object spread operator is translated into the `__assign` helper, which
  10789. // is not optimized by VMs as efficiently as a raw object literal. Since this constructor is
  10790. // called in tight loops, this difference matters.
  10791. this.state = {
  10792. peek: state.peek,
  10793. offset: state.offset,
  10794. line: state.line,
  10795. column: state.column,
  10796. };
  10797. }
  10798. else {
  10799. if (!range) {
  10800. throw new Error('Programming error: the range argument must be provided with a file argument.');
  10801. }
  10802. this.file = fileOrCursor;
  10803. this.input = fileOrCursor.content;
  10804. this.end = range.endPos;
  10805. this.state = {
  10806. peek: -1,
  10807. offset: range.startPos,
  10808. line: range.startLine,
  10809. column: range.startCol,
  10810. };
  10811. }
  10812. }
  10813. clone() {
  10814. return new PlainCharacterCursor(this);
  10815. }
  10816. peek() {
  10817. return this.state.peek;
  10818. }
  10819. charsLeft() {
  10820. return this.end - this.state.offset;
  10821. }
  10822. diff(other) {
  10823. return this.state.offset - other.state.offset;
  10824. }
  10825. advance() {
  10826. this.advanceState(this.state);
  10827. }
  10828. init() {
  10829. this.updatePeek(this.state);
  10830. }
  10831. getSpan(start, leadingTriviaCodePoints) {
  10832. start = start || this;
  10833. let fullStart = start;
  10834. if (leadingTriviaCodePoints) {
  10835. while (this.diff(start) > 0 && leadingTriviaCodePoints.indexOf(start.peek()) !== -1) {
  10836. if (fullStart === start) {
  10837. start = start.clone();
  10838. }
  10839. start.advance();
  10840. }
  10841. }
  10842. const startLocation = this.locationFromCursor(start);
  10843. const endLocation = this.locationFromCursor(this);
  10844. const fullStartLocation = fullStart !== start ? this.locationFromCursor(fullStart) : startLocation;
  10845. return new ParseSourceSpan(startLocation, endLocation, fullStartLocation);
  10846. }
  10847. getChars(start) {
  10848. return this.input.substring(start.state.offset, this.state.offset);
  10849. }
  10850. charAt(pos) {
  10851. return this.input.charCodeAt(pos);
  10852. }
  10853. advanceState(state) {
  10854. if (state.offset >= this.end) {
  10855. this.state = state;
  10856. throw new CursorError('Unexpected character "EOF"', this);
  10857. }
  10858. const currentChar = this.charAt(state.offset);
  10859. if (currentChar === $LF) {
  10860. state.line++;
  10861. state.column = 0;
  10862. }
  10863. else if (!isNewLine(currentChar)) {
  10864. state.column++;
  10865. }
  10866. state.offset++;
  10867. this.updatePeek(state);
  10868. }
  10869. updatePeek(state) {
  10870. state.peek = state.offset >= this.end ? $EOF : this.charAt(state.offset);
  10871. }
  10872. locationFromCursor(cursor) {
  10873. return new ParseLocation(cursor.file, cursor.state.offset, cursor.state.line, cursor.state.column);
  10874. }
  10875. }
  10876. class EscapedCharacterCursor extends PlainCharacterCursor {
  10877. constructor(fileOrCursor, range) {
  10878. if (fileOrCursor instanceof EscapedCharacterCursor) {
  10879. super(fileOrCursor);
  10880. this.internalState = Object.assign({}, fileOrCursor.internalState);
  10881. }
  10882. else {
  10883. super(fileOrCursor, range);
  10884. this.internalState = this.state;
  10885. }
  10886. }
  10887. advance() {
  10888. this.state = this.internalState;
  10889. super.advance();
  10890. this.processEscapeSequence();
  10891. }
  10892. init() {
  10893. super.init();
  10894. this.processEscapeSequence();
  10895. }
  10896. clone() {
  10897. return new EscapedCharacterCursor(this);
  10898. }
  10899. getChars(start) {
  10900. const cursor = start.clone();
  10901. let chars = '';
  10902. while (cursor.internalState.offset < this.internalState.offset) {
  10903. chars += String.fromCodePoint(cursor.peek());
  10904. cursor.advance();
  10905. }
  10906. return chars;
  10907. }
  10908. /**
  10909. * Process the escape sequence that starts at the current position in the text.
  10910. *
  10911. * This method is called to ensure that `peek` has the unescaped value of escape sequences.
  10912. */
  10913. processEscapeSequence() {
  10914. const peek = () => this.internalState.peek;
  10915. if (peek() === $BACKSLASH) {
  10916. // We have hit an escape sequence so we need the internal state to become independent
  10917. // of the external state.
  10918. this.internalState = Object.assign({}, this.state);
  10919. // Move past the backslash
  10920. this.advanceState(this.internalState);
  10921. // First check for standard control char sequences
  10922. if (peek() === $n) {
  10923. this.state.peek = $LF;
  10924. }
  10925. else if (peek() === $r) {
  10926. this.state.peek = $CR;
  10927. }
  10928. else if (peek() === $v) {
  10929. this.state.peek = $VTAB;
  10930. }
  10931. else if (peek() === $t) {
  10932. this.state.peek = $TAB;
  10933. }
  10934. else if (peek() === $b) {
  10935. this.state.peek = $BSPACE;
  10936. }
  10937. else if (peek() === $f) {
  10938. this.state.peek = $FF;
  10939. }
  10940. // Now consider more complex sequences
  10941. else if (peek() === $u) {
  10942. // Unicode code-point sequence
  10943. this.advanceState(this.internalState); // advance past the `u` char
  10944. if (peek() === $LBRACE) {
  10945. // Variable length Unicode, e.g. `\x{123}`
  10946. this.advanceState(this.internalState); // advance past the `{` char
  10947. // Advance past the variable number of hex digits until we hit a `}` char
  10948. const digitStart = this.clone();
  10949. let length = 0;
  10950. while (peek() !== $RBRACE) {
  10951. this.advanceState(this.internalState);
  10952. length++;
  10953. }
  10954. this.state.peek = this.decodeHexDigits(digitStart, length);
  10955. }
  10956. else {
  10957. // Fixed length Unicode, e.g. `\u1234`
  10958. const digitStart = this.clone();
  10959. this.advanceState(this.internalState);
  10960. this.advanceState(this.internalState);
  10961. this.advanceState(this.internalState);
  10962. this.state.peek = this.decodeHexDigits(digitStart, 4);
  10963. }
  10964. }
  10965. else if (peek() === $x) {
  10966. // Hex char code, e.g. `\x2F`
  10967. this.advanceState(this.internalState); // advance past the `x` char
  10968. const digitStart = this.clone();
  10969. this.advanceState(this.internalState);
  10970. this.state.peek = this.decodeHexDigits(digitStart, 2);
  10971. }
  10972. else if (isOctalDigit(peek())) {
  10973. // Octal char code, e.g. `\012`,
  10974. let octal = '';
  10975. let length = 0;
  10976. let previous = this.clone();
  10977. while (isOctalDigit(peek()) && length < 3) {
  10978. previous = this.clone();
  10979. octal += String.fromCodePoint(peek());
  10980. this.advanceState(this.internalState);
  10981. length++;
  10982. }
  10983. this.state.peek = parseInt(octal, 8);
  10984. // Backup one char
  10985. this.internalState = previous.internalState;
  10986. }
  10987. else if (isNewLine(this.internalState.peek)) {
  10988. // Line continuation `\` followed by a new line
  10989. this.advanceState(this.internalState); // advance over the newline
  10990. this.state = this.internalState;
  10991. }
  10992. else {
  10993. // If none of the `if` blocks were executed then we just have an escaped normal character.
  10994. // In that case we just, effectively, skip the backslash from the character.
  10995. this.state.peek = this.internalState.peek;
  10996. }
  10997. }
  10998. }
  10999. decodeHexDigits(start, length) {
  11000. const hex = this.input.substr(start.internalState.offset, length);
  11001. const charCode = parseInt(hex, 16);
  11002. if (!isNaN(charCode)) {
  11003. return charCode;
  11004. }
  11005. else {
  11006. start.state = start.internalState;
  11007. throw new CursorError('Invalid hexadecimal escape sequence', start);
  11008. }
  11009. }
  11010. }
  11011. class CursorError {
  11012. constructor(msg, cursor) {
  11013. this.msg = msg;
  11014. this.cursor = cursor;
  11015. }
  11016. }
  11017. /**
  11018. * @license
  11019. * Copyright Google LLC All Rights Reserved.
  11020. *
  11021. * Use of this source code is governed by an MIT-style license that can be
  11022. * found in the LICENSE file at https://angular.io/license
  11023. */
  11024. class TreeError extends ParseError {
  11025. constructor(elementName, span, msg) {
  11026. super(span, msg);
  11027. this.elementName = elementName;
  11028. }
  11029. static create(elementName, span, msg) {
  11030. return new TreeError(elementName, span, msg);
  11031. }
  11032. }
  11033. class ParseTreeResult {
  11034. constructor(rootNodes, errors) {
  11035. this.rootNodes = rootNodes;
  11036. this.errors = errors;
  11037. }
  11038. }
  11039. class Parser {
  11040. constructor(getTagDefinition) {
  11041. this.getTagDefinition = getTagDefinition;
  11042. }
  11043. parse(source, url, options) {
  11044. const tokenizeResult = tokenize(source, url, this.getTagDefinition, options);
  11045. const parser = new _TreeBuilder(tokenizeResult.tokens, this.getTagDefinition);
  11046. parser.build();
  11047. return new ParseTreeResult(parser.rootNodes, tokenizeResult.errors.concat(parser.errors));
  11048. }
  11049. }
  11050. class _TreeBuilder {
  11051. constructor(tokens, getTagDefinition) {
  11052. this.tokens = tokens;
  11053. this.getTagDefinition = getTagDefinition;
  11054. this._index = -1;
  11055. this._elementStack = [];
  11056. this.rootNodes = [];
  11057. this.errors = [];
  11058. this._advance();
  11059. }
  11060. build() {
  11061. while (this._peek.type !== TokenType.EOF) {
  11062. if (this._peek.type === TokenType.TAG_OPEN_START ||
  11063. this._peek.type === TokenType.INCOMPLETE_TAG_OPEN) {
  11064. this._consumeStartTag(this._advance());
  11065. }
  11066. else if (this._peek.type === TokenType.TAG_CLOSE) {
  11067. this._consumeEndTag(this._advance());
  11068. }
  11069. else if (this._peek.type === TokenType.CDATA_START) {
  11070. this._closeVoidElement();
  11071. this._consumeCdata(this._advance());
  11072. }
  11073. else if (this._peek.type === TokenType.COMMENT_START) {
  11074. this._closeVoidElement();
  11075. this._consumeComment(this._advance());
  11076. }
  11077. else if (this._peek.type === TokenType.TEXT || this._peek.type === TokenType.RAW_TEXT ||
  11078. this._peek.type === TokenType.ESCAPABLE_RAW_TEXT) {
  11079. this._closeVoidElement();
  11080. this._consumeText(this._advance());
  11081. }
  11082. else if (this._peek.type === TokenType.EXPANSION_FORM_START) {
  11083. this._consumeExpansion(this._advance());
  11084. }
  11085. else {
  11086. // Skip all other tokens...
  11087. this._advance();
  11088. }
  11089. }
  11090. }
  11091. _advance() {
  11092. const prev = this._peek;
  11093. if (this._index < this.tokens.length - 1) {
  11094. // Note: there is always an EOF token at the end
  11095. this._index++;
  11096. }
  11097. this._peek = this.tokens[this._index];
  11098. return prev;
  11099. }
  11100. _advanceIf(type) {
  11101. if (this._peek.type === type) {
  11102. return this._advance();
  11103. }
  11104. return null;
  11105. }
  11106. _consumeCdata(_startToken) {
  11107. this._consumeText(this._advance());
  11108. this._advanceIf(TokenType.CDATA_END);
  11109. }
  11110. _consumeComment(token) {
  11111. const text = this._advanceIf(TokenType.RAW_TEXT);
  11112. this._advanceIf(TokenType.COMMENT_END);
  11113. const value = text != null ? text.parts[0].trim() : null;
  11114. this._addToParent(new Comment$1(value, token.sourceSpan));
  11115. }
  11116. _consumeExpansion(token) {
  11117. const switchValue = this._advance();
  11118. const type = this._advance();
  11119. const cases = [];
  11120. // read =
  11121. while (this._peek.type === TokenType.EXPANSION_CASE_VALUE) {
  11122. const expCase = this._parseExpansionCase();
  11123. if (!expCase)
  11124. return; // error
  11125. cases.push(expCase);
  11126. }
  11127. // read the final }
  11128. if (this._peek.type !== TokenType.EXPANSION_FORM_END) {
  11129. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '}'.`));
  11130. return;
  11131. }
  11132. const sourceSpan = new ParseSourceSpan(token.sourceSpan.start, this._peek.sourceSpan.end, token.sourceSpan.fullStart);
  11133. this._addToParent(new Expansion(switchValue.parts[0], type.parts[0], cases, sourceSpan, switchValue.sourceSpan));
  11134. this._advance();
  11135. }
  11136. _parseExpansionCase() {
  11137. const value = this._advance();
  11138. // read {
  11139. if (this._peek.type !== TokenType.EXPANSION_CASE_EXP_START) {
  11140. this.errors.push(TreeError.create(null, this._peek.sourceSpan, `Invalid ICU message. Missing '{'.`));
  11141. return null;
  11142. }
  11143. // read until }
  11144. const start = this._advance();
  11145. const exp = this._collectExpansionExpTokens(start);
  11146. if (!exp)
  11147. return null;
  11148. const end = this._advance();
  11149. exp.push(new Token(TokenType.EOF, [], end.sourceSpan));
  11150. // parse everything in between { and }
  11151. const expansionCaseParser = new _TreeBuilder(exp, this.getTagDefinition);
  11152. expansionCaseParser.build();
  11153. if (expansionCaseParser.errors.length > 0) {
  11154. this.errors = this.errors.concat(expansionCaseParser.errors);
  11155. return null;
  11156. }
  11157. const sourceSpan = new ParseSourceSpan(value.sourceSpan.start, end.sourceSpan.end, value.sourceSpan.fullStart);
  11158. const expSourceSpan = new ParseSourceSpan(start.sourceSpan.start, end.sourceSpan.end, start.sourceSpan.fullStart);
  11159. return new ExpansionCase(value.parts[0], expansionCaseParser.rootNodes, sourceSpan, value.sourceSpan, expSourceSpan);
  11160. }
  11161. _collectExpansionExpTokens(start) {
  11162. const exp = [];
  11163. const expansionFormStack = [TokenType.EXPANSION_CASE_EXP_START];
  11164. while (true) {
  11165. if (this._peek.type === TokenType.EXPANSION_FORM_START ||
  11166. this._peek.type === TokenType.EXPANSION_CASE_EXP_START) {
  11167. expansionFormStack.push(this._peek.type);
  11168. }
  11169. if (this._peek.type === TokenType.EXPANSION_CASE_EXP_END) {
  11170. if (lastOnStack(expansionFormStack, TokenType.EXPANSION_CASE_EXP_START)) {
  11171. expansionFormStack.pop();
  11172. if (expansionFormStack.length == 0)
  11173. return exp;
  11174. }
  11175. else {
  11176. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  11177. return null;
  11178. }
  11179. }
  11180. if (this._peek.type === TokenType.EXPANSION_FORM_END) {
  11181. if (lastOnStack(expansionFormStack, TokenType.EXPANSION_FORM_START)) {
  11182. expansionFormStack.pop();
  11183. }
  11184. else {
  11185. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  11186. return null;
  11187. }
  11188. }
  11189. if (this._peek.type === TokenType.EOF) {
  11190. this.errors.push(TreeError.create(null, start.sourceSpan, `Invalid ICU message. Missing '}'.`));
  11191. return null;
  11192. }
  11193. exp.push(this._advance());
  11194. }
  11195. }
  11196. _consumeText(token) {
  11197. let text = token.parts[0];
  11198. if (text.length > 0 && text[0] == '\n') {
  11199. const parent = this._getParentElement();
  11200. if (parent != null && parent.children.length == 0 &&
  11201. this.getTagDefinition(parent.name).ignoreFirstLf) {
  11202. text = text.substring(1);
  11203. }
  11204. }
  11205. if (text.length > 0) {
  11206. this._addToParent(new Text$3(text, token.sourceSpan));
  11207. }
  11208. }
  11209. _closeVoidElement() {
  11210. const el = this._getParentElement();
  11211. if (el && this.getTagDefinition(el.name).isVoid) {
  11212. this._elementStack.pop();
  11213. }
  11214. }
  11215. _consumeStartTag(startTagToken) {
  11216. const [prefix, name] = startTagToken.parts;
  11217. const attrs = [];
  11218. while (this._peek.type === TokenType.ATTR_NAME) {
  11219. attrs.push(this._consumeAttr(this._advance()));
  11220. }
  11221. const fullName = this._getElementFullName(prefix, name, this._getParentElement());
  11222. let selfClosing = false;
  11223. // Note: There could have been a tokenizer error
  11224. // so that we don't get a token for the end tag...
  11225. if (this._peek.type === TokenType.TAG_OPEN_END_VOID) {
  11226. this._advance();
  11227. selfClosing = true;
  11228. const tagDef = this.getTagDefinition(fullName);
  11229. if (!(tagDef.canSelfClose || getNsPrefix(fullName) !== null || tagDef.isVoid)) {
  11230. this.errors.push(TreeError.create(fullName, startTagToken.sourceSpan, `Only void and foreign elements can be self closed "${startTagToken.parts[1]}"`));
  11231. }
  11232. }
  11233. else if (this._peek.type === TokenType.TAG_OPEN_END) {
  11234. this._advance();
  11235. selfClosing = false;
  11236. }
  11237. const end = this._peek.sourceSpan.fullStart;
  11238. const span = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  11239. // Create a separate `startSpan` because `span` will be modified when there is an `end` span.
  11240. const startSpan = new ParseSourceSpan(startTagToken.sourceSpan.start, end, startTagToken.sourceSpan.fullStart);
  11241. const el = new Element$1(fullName, attrs, [], span, startSpan, undefined);
  11242. this._pushElement(el);
  11243. if (selfClosing) {
  11244. // Elements that are self-closed have their `endSourceSpan` set to the full span, as the
  11245. // element start tag also represents the end tag.
  11246. this._popElement(fullName, span);
  11247. }
  11248. else if (startTagToken.type === TokenType.INCOMPLETE_TAG_OPEN) {
  11249. // We already know the opening tag is not complete, so it is unlikely it has a corresponding
  11250. // close tag. Let's optimistically parse it as a full element and emit an error.
  11251. this._popElement(fullName, null);
  11252. this.errors.push(TreeError.create(fullName, span, `Opening tag "${fullName}" not terminated.`));
  11253. }
  11254. }
  11255. _pushElement(el) {
  11256. const parentEl = this._getParentElement();
  11257. if (parentEl && this.getTagDefinition(parentEl.name).isClosedByChild(el.name)) {
  11258. this._elementStack.pop();
  11259. }
  11260. this._addToParent(el);
  11261. this._elementStack.push(el);
  11262. }
  11263. _consumeEndTag(endTagToken) {
  11264. const fullName = this._getElementFullName(endTagToken.parts[0], endTagToken.parts[1], this._getParentElement());
  11265. if (this.getTagDefinition(fullName).isVoid) {
  11266. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, `Void elements do not have end tags "${endTagToken.parts[1]}"`));
  11267. }
  11268. else if (!this._popElement(fullName, endTagToken.sourceSpan)) {
  11269. 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`;
  11270. this.errors.push(TreeError.create(fullName, endTagToken.sourceSpan, errMsg));
  11271. }
  11272. }
  11273. /**
  11274. * Closes the nearest element with the tag name `fullName` in the parse tree.
  11275. * `endSourceSpan` is the span of the closing tag, or null if the element does
  11276. * not have a closing tag (for example, this happens when an incomplete
  11277. * opening tag is recovered).
  11278. */
  11279. _popElement(fullName, endSourceSpan) {
  11280. let unexpectedCloseTagDetected = false;
  11281. for (let stackIndex = this._elementStack.length - 1; stackIndex >= 0; stackIndex--) {
  11282. const el = this._elementStack[stackIndex];
  11283. if (el.name == fullName) {
  11284. // Record the parse span with the element that is being closed. Any elements that are
  11285. // removed from the element stack at this point are closed implicitly, so they won't get
  11286. // an end source span (as there is no explicit closing element).
  11287. el.endSourceSpan = endSourceSpan;
  11288. el.sourceSpan.end = endSourceSpan !== null ? endSourceSpan.end : el.sourceSpan.end;
  11289. this._elementStack.splice(stackIndex, this._elementStack.length - stackIndex);
  11290. return !unexpectedCloseTagDetected;
  11291. }
  11292. if (!this.getTagDefinition(el.name).closedByParent) {
  11293. // Note that we encountered an unexpected close tag but continue processing the element
  11294. // stack so we can assign an `endSourceSpan` if there is a corresponding start tag for this
  11295. // end tag in the stack.
  11296. unexpectedCloseTagDetected = true;
  11297. }
  11298. }
  11299. return false;
  11300. }
  11301. _consumeAttr(attrName) {
  11302. const fullName = mergeNsAndName(attrName.parts[0], attrName.parts[1]);
  11303. let end = attrName.sourceSpan.end;
  11304. let value = '';
  11305. let valueSpan = undefined;
  11306. if (this._peek.type === TokenType.ATTR_QUOTE) {
  11307. this._advance();
  11308. }
  11309. if (this._peek.type === TokenType.ATTR_VALUE) {
  11310. const valueToken = this._advance();
  11311. value = valueToken.parts[0];
  11312. end = valueToken.sourceSpan.end;
  11313. valueSpan = valueToken.sourceSpan;
  11314. }
  11315. if (this._peek.type === TokenType.ATTR_QUOTE) {
  11316. const quoteToken = this._advance();
  11317. end = quoteToken.sourceSpan.end;
  11318. }
  11319. const keySpan = new ParseSourceSpan(attrName.sourceSpan.start, attrName.sourceSpan.end);
  11320. return new Attribute(fullName, value, new ParseSourceSpan(attrName.sourceSpan.start, end, attrName.sourceSpan.fullStart), keySpan, valueSpan);
  11321. }
  11322. _getParentElement() {
  11323. return this._elementStack.length > 0 ? this._elementStack[this._elementStack.length - 1] : null;
  11324. }
  11325. _addToParent(node) {
  11326. const parent = this._getParentElement();
  11327. if (parent != null) {
  11328. parent.children.push(node);
  11329. }
  11330. else {
  11331. this.rootNodes.push(node);
  11332. }
  11333. }
  11334. _getElementFullName(prefix, localName, parentElement) {
  11335. if (prefix === '') {
  11336. prefix = this.getTagDefinition(localName).implicitNamespacePrefix || '';
  11337. if (prefix === '' && parentElement != null) {
  11338. const parentTagName = splitNsName(parentElement.name)[1];
  11339. const parentTagDefinition = this.getTagDefinition(parentTagName);
  11340. if (!parentTagDefinition.preventNamespaceInheritance) {
  11341. prefix = getNsPrefix(parentElement.name);
  11342. }
  11343. }
  11344. }
  11345. return mergeNsAndName(prefix, localName);
  11346. }
  11347. }
  11348. function lastOnStack(stack, element) {
  11349. return stack.length > 0 && stack[stack.length - 1] === element;
  11350. }
  11351. /**
  11352. * @license
  11353. * Copyright Google LLC All Rights Reserved.
  11354. *
  11355. * Use of this source code is governed by an MIT-style license that can be
  11356. * found in the LICENSE file at https://angular.io/license
  11357. */
  11358. class HtmlParser extends Parser {
  11359. constructor() {
  11360. super(getHtmlTagDefinition);
  11361. }
  11362. parse(source, url, options) {
  11363. return super.parse(source, url, options);
  11364. }
  11365. }
  11366. /**
  11367. * @license
  11368. * Copyright Google LLC All Rights Reserved.
  11369. *
  11370. * Use of this source code is governed by an MIT-style license that can be
  11371. * found in the LICENSE file at https://angular.io/license
  11372. */
  11373. const PRESERVE_WS_ATTR_NAME = 'ngPreserveWhitespaces';
  11374. const SKIP_WS_TRIM_TAGS = new Set(['pre', 'template', 'textarea', 'script', 'style']);
  11375. // Equivalent to \s with \u00a0 (non-breaking space) excluded.
  11376. // Based on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
  11377. const WS_CHARS = ' \f\n\r\t\v\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff';
  11378. const NO_WS_REGEXP = new RegExp(`[^${WS_CHARS}]`);
  11379. const WS_REPLACE_REGEXP = new RegExp(`[${WS_CHARS}]{2,}`, 'g');
  11380. function hasPreserveWhitespacesAttr(attrs) {
  11381. return attrs.some((attr) => attr.name === PRESERVE_WS_ATTR_NAME);
  11382. }
  11383. /**
  11384. * Angular Dart introduced &ngsp; as a placeholder for non-removable space, see:
  11385. * https://github.com/dart-lang/angular/blob/0bb611387d29d65b5af7f9d2515ab571fd3fbee4/_tests/test/compiler/preserve_whitespace_test.dart#L25-L32
  11386. * In Angular Dart &ngsp; is converted to the 0xE500 PUA (Private Use Areas) unicode character
  11387. * and later on replaced by a space. We are re-implementing the same idea here.
  11388. */
  11389. function replaceNgsp(value) {
  11390. // lexer is replacing the &ngsp; pseudo-entity with NGSP_UNICODE
  11391. return value.replace(new RegExp(NGSP_UNICODE, 'g'), ' ');
  11392. }
  11393. /**
  11394. * This visitor can walk HTML parse tree and remove / trim text nodes using the following rules:
  11395. * - consider spaces, tabs and new lines as whitespace characters;
  11396. * - drop text nodes consisting of whitespace characters only;
  11397. * - for all other text nodes replace consecutive whitespace characters with one space;
  11398. * - convert &ngsp; pseudo-entity to a single space;
  11399. *
  11400. * Removal and trimming of whitespaces have positive performance impact (less code to generate
  11401. * while compiling templates, faster view creation). At the same time it can be "destructive"
  11402. * in some cases (whitespaces can influence layout). Because of the potential of breaking layout
  11403. * this visitor is not activated by default in Angular 5 and people need to explicitly opt-in for
  11404. * whitespace removal. The default option for whitespace removal will be revisited in Angular 6
  11405. * and might be changed to "on" by default.
  11406. */
  11407. class WhitespaceVisitor {
  11408. visitElement(element, context) {
  11409. if (SKIP_WS_TRIM_TAGS.has(element.name) || hasPreserveWhitespacesAttr(element.attrs)) {
  11410. // don't descent into elements where we need to preserve whitespaces
  11411. // but still visit all attributes to eliminate one used as a market to preserve WS
  11412. return new Element$1(element.name, visitAll$1(this, element.attrs), element.children, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  11413. }
  11414. return new Element$1(element.name, element.attrs, visitAllWithSiblings(this, element.children), element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  11415. }
  11416. visitAttribute(attribute, context) {
  11417. return attribute.name !== PRESERVE_WS_ATTR_NAME ? attribute : null;
  11418. }
  11419. visitText(text, context) {
  11420. const isNotBlank = text.value.match(NO_WS_REGEXP);
  11421. const hasExpansionSibling = context &&
  11422. (context.prev instanceof Expansion || context.next instanceof Expansion);
  11423. if (isNotBlank || hasExpansionSibling) {
  11424. return new Text$3(replaceNgsp(text.value).replace(WS_REPLACE_REGEXP, ' '), text.sourceSpan, text.i18n);
  11425. }
  11426. return null;
  11427. }
  11428. visitComment(comment, context) {
  11429. return comment;
  11430. }
  11431. visitExpansion(expansion, context) {
  11432. return expansion;
  11433. }
  11434. visitExpansionCase(expansionCase, context) {
  11435. return expansionCase;
  11436. }
  11437. }
  11438. function removeWhitespaces(htmlAstWithErrors) {
  11439. return new ParseTreeResult(visitAll$1(new WhitespaceVisitor(), htmlAstWithErrors.rootNodes), htmlAstWithErrors.errors);
  11440. }
  11441. function visitAllWithSiblings(visitor, nodes) {
  11442. const result = [];
  11443. nodes.forEach((ast, i) => {
  11444. const context = { prev: nodes[i - 1], next: nodes[i + 1] };
  11445. const astResult = ast.visit(visitor, context);
  11446. if (astResult) {
  11447. result.push(astResult);
  11448. }
  11449. });
  11450. return result;
  11451. }
  11452. /**
  11453. * @license
  11454. * Copyright Google LLC All Rights Reserved.
  11455. *
  11456. * Use of this source code is governed by an MIT-style license that can be
  11457. * found in the LICENSE file at https://angular.io/license
  11458. */
  11459. // http://cldr.unicode.org/index/cldr-spec/plural-rules
  11460. const PLURAL_CASES = ['zero', 'one', 'two', 'few', 'many', 'other'];
  11461. /**
  11462. * Expands special forms into elements.
  11463. *
  11464. * For example,
  11465. *
  11466. * ```
  11467. * { messages.length, plural,
  11468. * =0 {zero}
  11469. * =1 {one}
  11470. * other {more than one}
  11471. * }
  11472. * ```
  11473. *
  11474. * will be expanded into
  11475. *
  11476. * ```
  11477. * <ng-container [ngPlural]="messages.length">
  11478. * <ng-template ngPluralCase="=0">zero</ng-template>
  11479. * <ng-template ngPluralCase="=1">one</ng-template>
  11480. * <ng-template ngPluralCase="other">more than one</ng-template>
  11481. * </ng-container>
  11482. * ```
  11483. */
  11484. function expandNodes(nodes) {
  11485. const expander = new _Expander();
  11486. return new ExpansionResult(visitAll$1(expander, nodes), expander.isExpanded, expander.errors);
  11487. }
  11488. class ExpansionResult {
  11489. constructor(nodes, expanded, errors) {
  11490. this.nodes = nodes;
  11491. this.expanded = expanded;
  11492. this.errors = errors;
  11493. }
  11494. }
  11495. class ExpansionError extends ParseError {
  11496. constructor(span, errorMsg) {
  11497. super(span, errorMsg);
  11498. }
  11499. }
  11500. /**
  11501. * Expand expansion forms (plural, select) to directives
  11502. *
  11503. * @internal
  11504. */
  11505. class _Expander {
  11506. constructor() {
  11507. this.isExpanded = false;
  11508. this.errors = [];
  11509. }
  11510. visitElement(element, context) {
  11511. return new Element$1(element.name, element.attrs, visitAll$1(this, element.children), element.sourceSpan, element.startSourceSpan, element.endSourceSpan);
  11512. }
  11513. visitAttribute(attribute, context) {
  11514. return attribute;
  11515. }
  11516. visitText(text, context) {
  11517. return text;
  11518. }
  11519. visitComment(comment, context) {
  11520. return comment;
  11521. }
  11522. visitExpansion(icu, context) {
  11523. this.isExpanded = true;
  11524. return icu.type == 'plural' ? _expandPluralForm(icu, this.errors) :
  11525. _expandDefaultForm(icu, this.errors);
  11526. }
  11527. visitExpansionCase(icuCase, context) {
  11528. throw new Error('Should not be reached');
  11529. }
  11530. }
  11531. // Plural forms are expanded to `NgPlural` and `NgPluralCase`s
  11532. function _expandPluralForm(ast, errors) {
  11533. const children = ast.cases.map(c => {
  11534. if (PLURAL_CASES.indexOf(c.value) == -1 && !c.value.match(/^=\d+$/)) {
  11535. errors.push(new ExpansionError(c.valueSourceSpan, `Plural cases should be "=<number>" or one of ${PLURAL_CASES.join(', ')}`));
  11536. }
  11537. const expansionResult = expandNodes(c.expression);
  11538. errors.push(...expansionResult.errors);
  11539. return new Element$1(`ng-template`, [new Attribute('ngPluralCase', `${c.value}`, c.valueSourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* i18n */)], expansionResult.nodes, c.sourceSpan, c.sourceSpan, c.sourceSpan);
  11540. });
  11541. const switchAttr = new Attribute('[ngPlural]', ast.switchValue, ast.switchValueSourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* i18n */);
  11542. return new Element$1('ng-container', [switchAttr], children, ast.sourceSpan, ast.sourceSpan, ast.sourceSpan);
  11543. }
  11544. // ICU messages (excluding plural form) are expanded to `NgSwitch` and `NgSwitchCase`s
  11545. function _expandDefaultForm(ast, errors) {
  11546. const children = ast.cases.map(c => {
  11547. const expansionResult = expandNodes(c.expression);
  11548. errors.push(...expansionResult.errors);
  11549. if (c.value === 'other') {
  11550. // other is the default case when no values match
  11551. return new Element$1(`ng-template`, [new Attribute('ngSwitchDefault', '', c.valueSourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* i18n */)], expansionResult.nodes, c.sourceSpan, c.sourceSpan, c.sourceSpan);
  11552. }
  11553. return new Element$1(`ng-template`, [new Attribute('ngSwitchCase', `${c.value}`, c.valueSourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* i18n */)], expansionResult.nodes, c.sourceSpan, c.sourceSpan, c.sourceSpan);
  11554. });
  11555. const switchAttr = new Attribute('[ngSwitch]', ast.switchValue, ast.switchValueSourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* i18n */);
  11556. return new Element$1('ng-container', [switchAttr], children, ast.sourceSpan, ast.sourceSpan, ast.sourceSpan);
  11557. }
  11558. /**
  11559. * @license
  11560. * Copyright Google LLC All Rights Reserved.
  11561. *
  11562. * Use of this source code is governed by an MIT-style license that can be
  11563. * found in the LICENSE file at https://angular.io/license
  11564. */
  11565. /**
  11566. * A segment of text within the template.
  11567. */
  11568. class TextAst {
  11569. constructor(value, ngContentIndex, sourceSpan) {
  11570. this.value = value;
  11571. this.ngContentIndex = ngContentIndex;
  11572. this.sourceSpan = sourceSpan;
  11573. }
  11574. visit(visitor, context) {
  11575. return visitor.visitText(this, context);
  11576. }
  11577. }
  11578. /**
  11579. * A bound expression within the text of a template.
  11580. */
  11581. class BoundTextAst {
  11582. constructor(value, ngContentIndex, sourceSpan) {
  11583. this.value = value;
  11584. this.ngContentIndex = ngContentIndex;
  11585. this.sourceSpan = sourceSpan;
  11586. }
  11587. visit(visitor, context) {
  11588. return visitor.visitBoundText(this, context);
  11589. }
  11590. }
  11591. /**
  11592. * A plain attribute on an element.
  11593. */
  11594. class AttrAst {
  11595. constructor(name, value, sourceSpan) {
  11596. this.name = name;
  11597. this.value = value;
  11598. this.sourceSpan = sourceSpan;
  11599. }
  11600. visit(visitor, context) {
  11601. return visitor.visitAttr(this, context);
  11602. }
  11603. }
  11604. const BoundPropertyMapping = {
  11605. [4 /* Animation */]: 4 /* Animation */,
  11606. [1 /* Attribute */]: 1 /* Attribute */,
  11607. [2 /* Class */]: 2 /* Class */,
  11608. [0 /* Property */]: 0 /* Property */,
  11609. [3 /* Style */]: 3 /* Style */,
  11610. };
  11611. /**
  11612. * A binding for an element property (e.g. `[property]="expression"`) or an animation trigger (e.g.
  11613. * `[@trigger]="stateExp"`)
  11614. */
  11615. class BoundElementPropertyAst {
  11616. constructor(name, type, securityContext, value, unit, sourceSpan) {
  11617. this.name = name;
  11618. this.type = type;
  11619. this.securityContext = securityContext;
  11620. this.value = value;
  11621. this.unit = unit;
  11622. this.sourceSpan = sourceSpan;
  11623. this.isAnimation = this.type === 4 /* Animation */;
  11624. }
  11625. static fromBoundProperty(prop) {
  11626. const type = BoundPropertyMapping[prop.type];
  11627. return new BoundElementPropertyAst(prop.name, type, prop.securityContext, prop.value, prop.unit, prop.sourceSpan);
  11628. }
  11629. visit(visitor, context) {
  11630. return visitor.visitElementProperty(this, context);
  11631. }
  11632. }
  11633. /**
  11634. * A binding for an element event (e.g. `(event)="handler()"`) or an animation trigger event (e.g.
  11635. * `(@trigger.phase)="callback($event)"`).
  11636. */
  11637. class BoundEventAst {
  11638. constructor(name, target, phase, handler, sourceSpan, handlerSpan) {
  11639. this.name = name;
  11640. this.target = target;
  11641. this.phase = phase;
  11642. this.handler = handler;
  11643. this.sourceSpan = sourceSpan;
  11644. this.handlerSpan = handlerSpan;
  11645. this.fullName = BoundEventAst.calcFullName(this.name, this.target, this.phase);
  11646. this.isAnimation = !!this.phase;
  11647. }
  11648. static calcFullName(name, target, phase) {
  11649. if (target) {
  11650. return `${target}:${name}`;
  11651. }
  11652. if (phase) {
  11653. return `@${name}.${phase}`;
  11654. }
  11655. return name;
  11656. }
  11657. static fromParsedEvent(event) {
  11658. const target = event.type === 0 /* Regular */ ? event.targetOrPhase : null;
  11659. const phase = event.type === 1 /* Animation */ ? event.targetOrPhase : null;
  11660. return new BoundEventAst(event.name, target, phase, event.handler, event.sourceSpan, event.handlerSpan);
  11661. }
  11662. visit(visitor, context) {
  11663. return visitor.visitEvent(this, context);
  11664. }
  11665. }
  11666. /**
  11667. * A reference declaration on an element (e.g. `let someName="expression"`).
  11668. */
  11669. class ReferenceAst {
  11670. constructor(name, value, originalValue, sourceSpan) {
  11671. this.name = name;
  11672. this.value = value;
  11673. this.originalValue = originalValue;
  11674. this.sourceSpan = sourceSpan;
  11675. }
  11676. visit(visitor, context) {
  11677. return visitor.visitReference(this, context);
  11678. }
  11679. }
  11680. /**
  11681. * A variable declaration on a <ng-template> (e.g. `var-someName="someLocalName"`).
  11682. */
  11683. class VariableAst {
  11684. constructor(name, value, sourceSpan, valueSpan) {
  11685. this.name = name;
  11686. this.value = value;
  11687. this.sourceSpan = sourceSpan;
  11688. this.valueSpan = valueSpan;
  11689. }
  11690. static fromParsedVariable(v) {
  11691. return new VariableAst(v.name, v.value, v.sourceSpan, v.valueSpan);
  11692. }
  11693. visit(visitor, context) {
  11694. return visitor.visitVariable(this, context);
  11695. }
  11696. }
  11697. /**
  11698. * An element declaration in a template.
  11699. */
  11700. class ElementAst {
  11701. constructor(name, attrs, inputs, outputs, references, directives, providers, hasViewContainer, queryMatches, children, ngContentIndex, sourceSpan, endSourceSpan) {
  11702. this.name = name;
  11703. this.attrs = attrs;
  11704. this.inputs = inputs;
  11705. this.outputs = outputs;
  11706. this.references = references;
  11707. this.directives = directives;
  11708. this.providers = providers;
  11709. this.hasViewContainer = hasViewContainer;
  11710. this.queryMatches = queryMatches;
  11711. this.children = children;
  11712. this.ngContentIndex = ngContentIndex;
  11713. this.sourceSpan = sourceSpan;
  11714. this.endSourceSpan = endSourceSpan;
  11715. }
  11716. visit(visitor, context) {
  11717. return visitor.visitElement(this, context);
  11718. }
  11719. }
  11720. /**
  11721. * A `<ng-template>` element included in an Angular template.
  11722. */
  11723. class EmbeddedTemplateAst {
  11724. constructor(attrs, outputs, references, variables, directives, providers, hasViewContainer, queryMatches, children, ngContentIndex, sourceSpan) {
  11725. this.attrs = attrs;
  11726. this.outputs = outputs;
  11727. this.references = references;
  11728. this.variables = variables;
  11729. this.directives = directives;
  11730. this.providers = providers;
  11731. this.hasViewContainer = hasViewContainer;
  11732. this.queryMatches = queryMatches;
  11733. this.children = children;
  11734. this.ngContentIndex = ngContentIndex;
  11735. this.sourceSpan = sourceSpan;
  11736. }
  11737. visit(visitor, context) {
  11738. return visitor.visitEmbeddedTemplate(this, context);
  11739. }
  11740. }
  11741. /**
  11742. * A directive property with a bound value (e.g. `*ngIf="condition").
  11743. */
  11744. class BoundDirectivePropertyAst {
  11745. constructor(directiveName, templateName, value, sourceSpan) {
  11746. this.directiveName = directiveName;
  11747. this.templateName = templateName;
  11748. this.value = value;
  11749. this.sourceSpan = sourceSpan;
  11750. }
  11751. visit(visitor, context) {
  11752. return visitor.visitDirectiveProperty(this, context);
  11753. }
  11754. }
  11755. /**
  11756. * A directive declared on an element.
  11757. */
  11758. class DirectiveAst {
  11759. constructor(directive, inputs, hostProperties, hostEvents, contentQueryStartId, sourceSpan) {
  11760. this.directive = directive;
  11761. this.inputs = inputs;
  11762. this.hostProperties = hostProperties;
  11763. this.hostEvents = hostEvents;
  11764. this.contentQueryStartId = contentQueryStartId;
  11765. this.sourceSpan = sourceSpan;
  11766. }
  11767. visit(visitor, context) {
  11768. return visitor.visitDirective(this, context);
  11769. }
  11770. }
  11771. /**
  11772. * A provider declared on an element
  11773. */
  11774. class ProviderAst {
  11775. constructor(token, multiProvider, eager, providers, providerType, lifecycleHooks, sourceSpan, isModule) {
  11776. this.token = token;
  11777. this.multiProvider = multiProvider;
  11778. this.eager = eager;
  11779. this.providers = providers;
  11780. this.providerType = providerType;
  11781. this.lifecycleHooks = lifecycleHooks;
  11782. this.sourceSpan = sourceSpan;
  11783. this.isModule = isModule;
  11784. }
  11785. visit(visitor, context) {
  11786. // No visit method in the visitor for now...
  11787. return null;
  11788. }
  11789. }
  11790. var ProviderAstType;
  11791. (function (ProviderAstType) {
  11792. ProviderAstType[ProviderAstType["PublicService"] = 0] = "PublicService";
  11793. ProviderAstType[ProviderAstType["PrivateService"] = 1] = "PrivateService";
  11794. ProviderAstType[ProviderAstType["Component"] = 2] = "Component";
  11795. ProviderAstType[ProviderAstType["Directive"] = 3] = "Directive";
  11796. ProviderAstType[ProviderAstType["Builtin"] = 4] = "Builtin";
  11797. })(ProviderAstType || (ProviderAstType = {}));
  11798. /**
  11799. * Position where content is to be projected (instance of `<ng-content>` in a template).
  11800. */
  11801. class NgContentAst {
  11802. constructor(index, ngContentIndex, sourceSpan) {
  11803. this.index = index;
  11804. this.ngContentIndex = ngContentIndex;
  11805. this.sourceSpan = sourceSpan;
  11806. }
  11807. visit(visitor, context) {
  11808. return visitor.visitNgContent(this, context);
  11809. }
  11810. }
  11811. /**
  11812. * A visitor that accepts each node but doesn't do anything. It is intended to be used
  11813. * as the base class for a visitor that is only interested in a subset of the node types.
  11814. */
  11815. class NullTemplateVisitor {
  11816. visitNgContent(ast, context) { }
  11817. visitEmbeddedTemplate(ast, context) { }
  11818. visitElement(ast, context) { }
  11819. visitReference(ast, context) { }
  11820. visitVariable(ast, context) { }
  11821. visitEvent(ast, context) { }
  11822. visitElementProperty(ast, context) { }
  11823. visitAttr(ast, context) { }
  11824. visitBoundText(ast, context) { }
  11825. visitText(ast, context) { }
  11826. visitDirective(ast, context) { }
  11827. visitDirectiveProperty(ast, context) { }
  11828. }
  11829. /**
  11830. * Base class that can be used to build a visitor that visits each node
  11831. * in an template ast recursively.
  11832. */
  11833. class RecursiveTemplateAstVisitor extends NullTemplateVisitor {
  11834. constructor() {
  11835. super();
  11836. }
  11837. // Nodes with children
  11838. visitEmbeddedTemplate(ast, context) {
  11839. return this.visitChildren(context, visit => {
  11840. visit(ast.attrs);
  11841. visit(ast.references);
  11842. visit(ast.variables);
  11843. visit(ast.directives);
  11844. visit(ast.providers);
  11845. visit(ast.children);
  11846. });
  11847. }
  11848. visitElement(ast, context) {
  11849. return this.visitChildren(context, visit => {
  11850. visit(ast.attrs);
  11851. visit(ast.inputs);
  11852. visit(ast.outputs);
  11853. visit(ast.references);
  11854. visit(ast.directives);
  11855. visit(ast.providers);
  11856. visit(ast.children);
  11857. });
  11858. }
  11859. visitDirective(ast, context) {
  11860. return this.visitChildren(context, visit => {
  11861. visit(ast.inputs);
  11862. visit(ast.hostProperties);
  11863. visit(ast.hostEvents);
  11864. });
  11865. }
  11866. visitChildren(context, cb) {
  11867. let results = [];
  11868. let t = this;
  11869. function visit(children) {
  11870. if (children && children.length)
  11871. results.push(templateVisitAll(t, children, context));
  11872. }
  11873. cb(visit);
  11874. return Array.prototype.concat.apply([], results);
  11875. }
  11876. }
  11877. /**
  11878. * Visit every node in a list of {@link TemplateAst}s with the given {@link TemplateAstVisitor}.
  11879. */
  11880. function templateVisitAll(visitor, asts, context = null) {
  11881. const result = [];
  11882. const visit = visitor.visit ?
  11883. (ast) => visitor.visit(ast, context) || ast.visit(visitor, context) :
  11884. (ast) => ast.visit(visitor, context);
  11885. asts.forEach(ast => {
  11886. const astResult = visit(ast);
  11887. if (astResult) {
  11888. result.push(astResult);
  11889. }
  11890. });
  11891. return result;
  11892. }
  11893. /**
  11894. * @license
  11895. * Copyright Google LLC All Rights Reserved.
  11896. *
  11897. * Use of this source code is governed by an MIT-style license that can be
  11898. * found in the LICENSE file at https://angular.io/license
  11899. */
  11900. class ProviderError extends ParseError {
  11901. constructor(message, span) {
  11902. super(span, message);
  11903. }
  11904. }
  11905. class ProviderViewContext {
  11906. constructor(reflector, component) {
  11907. this.reflector = reflector;
  11908. this.component = component;
  11909. this.errors = [];
  11910. this.viewQueries = _getViewQueries(component);
  11911. this.viewProviders = new Map();
  11912. component.viewProviders.forEach((provider) => {
  11913. if (this.viewProviders.get(tokenReference(provider.token)) == null) {
  11914. this.viewProviders.set(tokenReference(provider.token), true);
  11915. }
  11916. });
  11917. }
  11918. }
  11919. class ProviderElementContext {
  11920. constructor(viewContext, _parent, _isViewRoot, _directiveAsts, attrs, refs, isTemplate, contentQueryStartId, _sourceSpan) {
  11921. this.viewContext = viewContext;
  11922. this._parent = _parent;
  11923. this._isViewRoot = _isViewRoot;
  11924. this._directiveAsts = _directiveAsts;
  11925. this._sourceSpan = _sourceSpan;
  11926. this._transformedProviders = new Map();
  11927. this._seenProviders = new Map();
  11928. this._queriedTokens = new Map();
  11929. this.transformedHasViewContainer = false;
  11930. this._attrs = {};
  11931. attrs.forEach((attrAst) => this._attrs[attrAst.name] = attrAst.value);
  11932. const directivesMeta = _directiveAsts.map(directiveAst => directiveAst.directive);
  11933. this._allProviders =
  11934. _resolveProvidersFromDirectives(directivesMeta, _sourceSpan, viewContext.errors);
  11935. this._contentQueries = _getContentQueries(contentQueryStartId, directivesMeta);
  11936. Array.from(this._allProviders.values()).forEach((provider) => {
  11937. this._addQueryReadsTo(provider.token, provider.token, this._queriedTokens);
  11938. });
  11939. if (isTemplate) {
  11940. const templateRefId = createTokenForExternalReference(this.viewContext.reflector, Identifiers$1.TemplateRef);
  11941. this._addQueryReadsTo(templateRefId, templateRefId, this._queriedTokens);
  11942. }
  11943. refs.forEach((refAst) => {
  11944. let defaultQueryValue = refAst.value ||
  11945. createTokenForExternalReference(this.viewContext.reflector, Identifiers$1.ElementRef);
  11946. this._addQueryReadsTo({ value: refAst.name }, defaultQueryValue, this._queriedTokens);
  11947. });
  11948. if (this._queriedTokens.get(this.viewContext.reflector.resolveExternalReference(Identifiers$1.ViewContainerRef))) {
  11949. this.transformedHasViewContainer = true;
  11950. }
  11951. // create the providers that we know are eager first
  11952. Array.from(this._allProviders.values()).forEach((provider) => {
  11953. const eager = provider.eager || this._queriedTokens.get(tokenReference(provider.token));
  11954. if (eager) {
  11955. this._getOrCreateLocalProvider(provider.providerType, provider.token, true);
  11956. }
  11957. });
  11958. }
  11959. afterElement() {
  11960. // collect lazy providers
  11961. Array.from(this._allProviders.values()).forEach((provider) => {
  11962. this._getOrCreateLocalProvider(provider.providerType, provider.token, false);
  11963. });
  11964. }
  11965. get transformProviders() {
  11966. // Note: Maps keep their insertion order.
  11967. const lazyProviders = [];
  11968. const eagerProviders = [];
  11969. this._transformedProviders.forEach(provider => {
  11970. if (provider.eager) {
  11971. eagerProviders.push(provider);
  11972. }
  11973. else {
  11974. lazyProviders.push(provider);
  11975. }
  11976. });
  11977. return lazyProviders.concat(eagerProviders);
  11978. }
  11979. get transformedDirectiveAsts() {
  11980. const sortedProviderTypes = this.transformProviders.map(provider => provider.token.identifier);
  11981. const sortedDirectives = this._directiveAsts.slice();
  11982. sortedDirectives.sort((dir1, dir2) => sortedProviderTypes.indexOf(dir1.directive.type) -
  11983. sortedProviderTypes.indexOf(dir2.directive.type));
  11984. return sortedDirectives;
  11985. }
  11986. get queryMatches() {
  11987. const allMatches = [];
  11988. this._queriedTokens.forEach((matches) => {
  11989. allMatches.push(...matches);
  11990. });
  11991. return allMatches;
  11992. }
  11993. _addQueryReadsTo(token, defaultValue, queryReadTokens) {
  11994. this._getQueriesFor(token).forEach((query) => {
  11995. const queryValue = query.meta.read || defaultValue;
  11996. const tokenRef = tokenReference(queryValue);
  11997. let queryMatches = queryReadTokens.get(tokenRef);
  11998. if (!queryMatches) {
  11999. queryMatches = [];
  12000. queryReadTokens.set(tokenRef, queryMatches);
  12001. }
  12002. queryMatches.push({ queryId: query.queryId, value: queryValue });
  12003. });
  12004. }
  12005. _getQueriesFor(token) {
  12006. const result = [];
  12007. let currentEl = this;
  12008. let distance = 0;
  12009. let queries;
  12010. while (currentEl !== null) {
  12011. queries = currentEl._contentQueries.get(tokenReference(token));
  12012. if (queries) {
  12013. result.push(...queries.filter((query) => query.meta.descendants || distance <= 1));
  12014. }
  12015. if (currentEl._directiveAsts.length > 0) {
  12016. distance++;
  12017. }
  12018. currentEl = currentEl._parent;
  12019. }
  12020. queries = this.viewContext.viewQueries.get(tokenReference(token));
  12021. if (queries) {
  12022. result.push(...queries);
  12023. }
  12024. return result;
  12025. }
  12026. _getOrCreateLocalProvider(requestingProviderType, token, eager) {
  12027. const resolvedProvider = this._allProviders.get(tokenReference(token));
  12028. if (!resolvedProvider ||
  12029. ((requestingProviderType === ProviderAstType.Directive ||
  12030. requestingProviderType === ProviderAstType.PublicService) &&
  12031. resolvedProvider.providerType === ProviderAstType.PrivateService) ||
  12032. ((requestingProviderType === ProviderAstType.PrivateService ||
  12033. requestingProviderType === ProviderAstType.PublicService) &&
  12034. resolvedProvider.providerType === ProviderAstType.Builtin)) {
  12035. return null;
  12036. }
  12037. let transformedProviderAst = this._transformedProviders.get(tokenReference(token));
  12038. if (transformedProviderAst) {
  12039. return transformedProviderAst;
  12040. }
  12041. if (this._seenProviders.get(tokenReference(token)) != null) {
  12042. this.viewContext.errors.push(new ProviderError(`Cannot instantiate cyclic dependency! ${tokenName(token)}`, this._sourceSpan));
  12043. return null;
  12044. }
  12045. this._seenProviders.set(tokenReference(token), true);
  12046. const transformedProviders = resolvedProvider.providers.map((provider) => {
  12047. let transformedUseValue = provider.useValue;
  12048. let transformedUseExisting = provider.useExisting;
  12049. let transformedDeps = undefined;
  12050. if (provider.useExisting != null) {
  12051. const existingDiDep = this._getDependency(resolvedProvider.providerType, { token: provider.useExisting }, eager);
  12052. if (existingDiDep.token != null) {
  12053. transformedUseExisting = existingDiDep.token;
  12054. }
  12055. else {
  12056. transformedUseExisting = null;
  12057. transformedUseValue = existingDiDep.value;
  12058. }
  12059. }
  12060. else if (provider.useFactory) {
  12061. const deps = provider.deps || provider.useFactory.diDeps;
  12062. transformedDeps =
  12063. deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager));
  12064. }
  12065. else if (provider.useClass) {
  12066. const deps = provider.deps || provider.useClass.diDeps;
  12067. transformedDeps =
  12068. deps.map((dep) => this._getDependency(resolvedProvider.providerType, dep, eager));
  12069. }
  12070. return _transformProvider(provider, {
  12071. useExisting: transformedUseExisting,
  12072. useValue: transformedUseValue,
  12073. deps: transformedDeps
  12074. });
  12075. });
  12076. transformedProviderAst =
  12077. _transformProviderAst(resolvedProvider, { eager: eager, providers: transformedProviders });
  12078. this._transformedProviders.set(tokenReference(token), transformedProviderAst);
  12079. return transformedProviderAst;
  12080. }
  12081. _getLocalDependency(requestingProviderType, dep, eager = false) {
  12082. if (dep.isAttribute) {
  12083. const attrValue = this._attrs[dep.token.value];
  12084. return { isValue: true, value: attrValue == null ? null : attrValue };
  12085. }
  12086. if (dep.token != null) {
  12087. // access builtints
  12088. if ((requestingProviderType === ProviderAstType.Directive ||
  12089. requestingProviderType === ProviderAstType.Component)) {
  12090. if (tokenReference(dep.token) ===
  12091. this.viewContext.reflector.resolveExternalReference(Identifiers$1.Renderer) ||
  12092. tokenReference(dep.token) ===
  12093. this.viewContext.reflector.resolveExternalReference(Identifiers$1.ElementRef) ||
  12094. tokenReference(dep.token) ===
  12095. this.viewContext.reflector.resolveExternalReference(Identifiers$1.ChangeDetectorRef) ||
  12096. tokenReference(dep.token) ===
  12097. this.viewContext.reflector.resolveExternalReference(Identifiers$1.TemplateRef)) {
  12098. return dep;
  12099. }
  12100. if (tokenReference(dep.token) ===
  12101. this.viewContext.reflector.resolveExternalReference(Identifiers$1.ViewContainerRef)) {
  12102. this.transformedHasViewContainer = true;
  12103. }
  12104. }
  12105. // access the injector
  12106. if (tokenReference(dep.token) ===
  12107. this.viewContext.reflector.resolveExternalReference(Identifiers$1.Injector)) {
  12108. return dep;
  12109. }
  12110. // access providers
  12111. if (this._getOrCreateLocalProvider(requestingProviderType, dep.token, eager) != null) {
  12112. return dep;
  12113. }
  12114. }
  12115. return null;
  12116. }
  12117. _getDependency(requestingProviderType, dep, eager = false) {
  12118. let currElement = this;
  12119. let currEager = eager;
  12120. let result = null;
  12121. if (!dep.isSkipSelf) {
  12122. result = this._getLocalDependency(requestingProviderType, dep, eager);
  12123. }
  12124. if (dep.isSelf) {
  12125. if (!result && dep.isOptional) {
  12126. result = { isValue: true, value: null };
  12127. }
  12128. }
  12129. else {
  12130. // check parent elements
  12131. while (!result && currElement._parent) {
  12132. const prevElement = currElement;
  12133. currElement = currElement._parent;
  12134. if (prevElement._isViewRoot) {
  12135. currEager = false;
  12136. }
  12137. result = currElement._getLocalDependency(ProviderAstType.PublicService, dep, currEager);
  12138. }
  12139. // check @Host restriction
  12140. if (!result) {
  12141. if (!dep.isHost || this.viewContext.component.isHost ||
  12142. this.viewContext.component.type.reference === tokenReference(dep.token) ||
  12143. this.viewContext.viewProviders.get(tokenReference(dep.token)) != null) {
  12144. result = dep;
  12145. }
  12146. else {
  12147. result = dep.isOptional ? { isValue: true, value: null } : null;
  12148. }
  12149. }
  12150. }
  12151. if (!result) {
  12152. this.viewContext.errors.push(new ProviderError(`No provider for ${tokenName(dep.token)}`, this._sourceSpan));
  12153. }
  12154. return result;
  12155. }
  12156. }
  12157. class NgModuleProviderAnalyzer {
  12158. constructor(reflector, ngModule, extraProviders, sourceSpan) {
  12159. this.reflector = reflector;
  12160. this._transformedProviders = new Map();
  12161. this._seenProviders = new Map();
  12162. this._errors = [];
  12163. this._allProviders = new Map();
  12164. ngModule.transitiveModule.modules.forEach((ngModuleType) => {
  12165. const ngModuleProvider = { token: { identifier: ngModuleType }, useClass: ngModuleType };
  12166. _resolveProviders([ngModuleProvider], ProviderAstType.PublicService, true, sourceSpan, this._errors, this._allProviders, /* isModule */ true);
  12167. });
  12168. _resolveProviders(ngModule.transitiveModule.providers.map(entry => entry.provider).concat(extraProviders), ProviderAstType.PublicService, false, sourceSpan, this._errors, this._allProviders,
  12169. /* isModule */ false);
  12170. }
  12171. parse() {
  12172. Array.from(this._allProviders.values()).forEach((provider) => {
  12173. this._getOrCreateLocalProvider(provider.token, provider.eager);
  12174. });
  12175. if (this._errors.length > 0) {
  12176. const errorString = this._errors.join('\n');
  12177. throw new Error(`Provider parse errors:\n${errorString}`);
  12178. }
  12179. // Note: Maps keep their insertion order.
  12180. const lazyProviders = [];
  12181. const eagerProviders = [];
  12182. this._transformedProviders.forEach(provider => {
  12183. if (provider.eager) {
  12184. eagerProviders.push(provider);
  12185. }
  12186. else {
  12187. lazyProviders.push(provider);
  12188. }
  12189. });
  12190. return lazyProviders.concat(eagerProviders);
  12191. }
  12192. _getOrCreateLocalProvider(token, eager) {
  12193. const resolvedProvider = this._allProviders.get(tokenReference(token));
  12194. if (!resolvedProvider) {
  12195. return null;
  12196. }
  12197. let transformedProviderAst = this._transformedProviders.get(tokenReference(token));
  12198. if (transformedProviderAst) {
  12199. return transformedProviderAst;
  12200. }
  12201. if (this._seenProviders.get(tokenReference(token)) != null) {
  12202. this._errors.push(new ProviderError(`Cannot instantiate cyclic dependency! ${tokenName(token)}`, resolvedProvider.sourceSpan));
  12203. return null;
  12204. }
  12205. this._seenProviders.set(tokenReference(token), true);
  12206. const transformedProviders = resolvedProvider.providers.map((provider) => {
  12207. let transformedUseValue = provider.useValue;
  12208. let transformedUseExisting = provider.useExisting;
  12209. let transformedDeps = undefined;
  12210. if (provider.useExisting != null) {
  12211. const existingDiDep = this._getDependency({ token: provider.useExisting }, eager, resolvedProvider.sourceSpan);
  12212. if (existingDiDep.token != null) {
  12213. transformedUseExisting = existingDiDep.token;
  12214. }
  12215. else {
  12216. transformedUseExisting = null;
  12217. transformedUseValue = existingDiDep.value;
  12218. }
  12219. }
  12220. else if (provider.useFactory) {
  12221. const deps = provider.deps || provider.useFactory.diDeps;
  12222. transformedDeps =
  12223. deps.map((dep) => this._getDependency(dep, eager, resolvedProvider.sourceSpan));
  12224. }
  12225. else if (provider.useClass) {
  12226. const deps = provider.deps || provider.useClass.diDeps;
  12227. transformedDeps =
  12228. deps.map((dep) => this._getDependency(dep, eager, resolvedProvider.sourceSpan));
  12229. }
  12230. return _transformProvider(provider, {
  12231. useExisting: transformedUseExisting,
  12232. useValue: transformedUseValue,
  12233. deps: transformedDeps
  12234. });
  12235. });
  12236. transformedProviderAst =
  12237. _transformProviderAst(resolvedProvider, { eager: eager, providers: transformedProviders });
  12238. this._transformedProviders.set(tokenReference(token), transformedProviderAst);
  12239. return transformedProviderAst;
  12240. }
  12241. _getDependency(dep, eager = false, requestorSourceSpan) {
  12242. let foundLocal = false;
  12243. if (!dep.isSkipSelf && dep.token != null) {
  12244. // access the injector
  12245. if (tokenReference(dep.token) ===
  12246. this.reflector.resolveExternalReference(Identifiers$1.Injector) ||
  12247. tokenReference(dep.token) ===
  12248. this.reflector.resolveExternalReference(Identifiers$1.ComponentFactoryResolver)) {
  12249. foundLocal = true;
  12250. // access providers
  12251. }
  12252. else if (this._getOrCreateLocalProvider(dep.token, eager) != null) {
  12253. foundLocal = true;
  12254. }
  12255. }
  12256. return dep;
  12257. }
  12258. }
  12259. function _transformProvider(provider, { useExisting, useValue, deps }) {
  12260. return {
  12261. token: provider.token,
  12262. useClass: provider.useClass,
  12263. useExisting: useExisting,
  12264. useFactory: provider.useFactory,
  12265. useValue: useValue,
  12266. deps: deps,
  12267. multi: provider.multi
  12268. };
  12269. }
  12270. function _transformProviderAst(provider, { eager, providers }) {
  12271. return new ProviderAst(provider.token, provider.multiProvider, provider.eager || eager, providers, provider.providerType, provider.lifecycleHooks, provider.sourceSpan, provider.isModule);
  12272. }
  12273. function _resolveProvidersFromDirectives(directives, sourceSpan, targetErrors) {
  12274. const providersByToken = new Map();
  12275. directives.forEach((directive) => {
  12276. const dirProvider = { token: { identifier: directive.type }, useClass: directive.type };
  12277. _resolveProviders([dirProvider], directive.isComponent ? ProviderAstType.Component : ProviderAstType.Directive, true, sourceSpan, targetErrors, providersByToken, /* isModule */ false);
  12278. });
  12279. // Note: directives need to be able to overwrite providers of a component!
  12280. const directivesWithComponentFirst = directives.filter(dir => dir.isComponent).concat(directives.filter(dir => !dir.isComponent));
  12281. directivesWithComponentFirst.forEach((directive) => {
  12282. _resolveProviders(directive.providers, ProviderAstType.PublicService, false, sourceSpan, targetErrors, providersByToken, /* isModule */ false);
  12283. _resolveProviders(directive.viewProviders, ProviderAstType.PrivateService, false, sourceSpan, targetErrors, providersByToken, /* isModule */ false);
  12284. });
  12285. return providersByToken;
  12286. }
  12287. function _resolveProviders(providers, providerType, eager, sourceSpan, targetErrors, targetProvidersByToken, isModule) {
  12288. providers.forEach((provider) => {
  12289. let resolvedProvider = targetProvidersByToken.get(tokenReference(provider.token));
  12290. if (resolvedProvider != null && !!resolvedProvider.multiProvider !== !!provider.multi) {
  12291. targetErrors.push(new ProviderError(`Mixing multi and non multi provider is not possible for token ${tokenName(resolvedProvider.token)}`, sourceSpan));
  12292. }
  12293. if (!resolvedProvider) {
  12294. const lifecycleHooks = provider.token.identifier &&
  12295. provider.token.identifier.lifecycleHooks ?
  12296. provider.token.identifier.lifecycleHooks :
  12297. [];
  12298. const isUseValue = !(provider.useClass || provider.useExisting || provider.useFactory);
  12299. resolvedProvider = new ProviderAst(provider.token, !!provider.multi, eager || isUseValue, [provider], providerType, lifecycleHooks, sourceSpan, isModule);
  12300. targetProvidersByToken.set(tokenReference(provider.token), resolvedProvider);
  12301. }
  12302. else {
  12303. if (!provider.multi) {
  12304. resolvedProvider.providers.length = 0;
  12305. }
  12306. resolvedProvider.providers.push(provider);
  12307. }
  12308. });
  12309. }
  12310. function _getViewQueries(component) {
  12311. // Note: queries start with id 1 so we can use the number in a Bloom filter!
  12312. let viewQueryId = 1;
  12313. const viewQueries = new Map();
  12314. if (component.viewQueries) {
  12315. component.viewQueries.forEach((query) => _addQueryToTokenMap(viewQueries, { meta: query, queryId: viewQueryId++ }));
  12316. }
  12317. return viewQueries;
  12318. }
  12319. function _getContentQueries(contentQueryStartId, directives) {
  12320. let contentQueryId = contentQueryStartId;
  12321. const contentQueries = new Map();
  12322. directives.forEach((directive, directiveIndex) => {
  12323. if (directive.queries) {
  12324. directive.queries.forEach((query) => _addQueryToTokenMap(contentQueries, { meta: query, queryId: contentQueryId++ }));
  12325. }
  12326. });
  12327. return contentQueries;
  12328. }
  12329. function _addQueryToTokenMap(map, query) {
  12330. query.meta.selectors.forEach((token) => {
  12331. let entry = map.get(tokenReference(token));
  12332. if (!entry) {
  12333. entry = [];
  12334. map.set(tokenReference(token), entry);
  12335. }
  12336. entry.push(query);
  12337. });
  12338. }
  12339. /**
  12340. * @license
  12341. * Copyright Google LLC All Rights Reserved.
  12342. *
  12343. * Use of this source code is governed by an MIT-style license that can be
  12344. * found in the LICENSE file at https://angular.io/license
  12345. */
  12346. class StyleWithImports {
  12347. constructor(style, styleUrls) {
  12348. this.style = style;
  12349. this.styleUrls = styleUrls;
  12350. }
  12351. }
  12352. function isStyleUrlResolvable(url) {
  12353. if (url == null || url.length === 0 || url[0] == '/')
  12354. return false;
  12355. const schemeMatch = url.match(URL_WITH_SCHEMA_REGEXP);
  12356. return schemeMatch === null || schemeMatch[1] == 'package' || schemeMatch[1] == 'asset';
  12357. }
  12358. /**
  12359. * Rewrites stylesheets by resolving and removing the @import urls that
  12360. * are either relative or don't have a `package:` scheme
  12361. */
  12362. function extractStyleUrls(resolver, baseUrl, cssText) {
  12363. const foundUrls = [];
  12364. const modifiedCssText = cssText.replace(CSS_STRIPPABLE_COMMENT_REGEXP, '')
  12365. .replace(CSS_IMPORT_REGEXP, (...m) => {
  12366. const url = m[1] || m[2];
  12367. if (!isStyleUrlResolvable(url)) {
  12368. // Do not attempt to resolve non-package absolute URLs with URI
  12369. // scheme
  12370. return m[0];
  12371. }
  12372. foundUrls.push(resolver.resolve(baseUrl, url));
  12373. return '';
  12374. });
  12375. return new StyleWithImports(modifiedCssText, foundUrls);
  12376. }
  12377. const CSS_IMPORT_REGEXP = /@import\s+(?:url\()?\s*(?:(?:['"]([^'"]*))|([^;\)\s]*))[^;]*;?/g;
  12378. const CSS_STRIPPABLE_COMMENT_REGEXP = /\/\*(?!#\s*(?:sourceURL|sourceMappingURL)=)[\s\S]+?\*\//g;
  12379. const URL_WITH_SCHEMA_REGEXP = /^([^:/?#]+):/;
  12380. /**
  12381. * @license
  12382. * Copyright Google LLC All Rights Reserved.
  12383. *
  12384. * Use of this source code is governed by an MIT-style license that can be
  12385. * found in the LICENSE file at https://angular.io/license
  12386. */
  12387. const PROPERTY_PARTS_SEPARATOR = '.';
  12388. const ATTRIBUTE_PREFIX = 'attr';
  12389. const CLASS_PREFIX = 'class';
  12390. const STYLE_PREFIX = 'style';
  12391. const TEMPLATE_ATTR_PREFIX = '*';
  12392. const ANIMATE_PROP_PREFIX = 'animate-';
  12393. /**
  12394. * Parses bindings in templates and in the directive host area.
  12395. */
  12396. class BindingParser {
  12397. constructor(_exprParser, _interpolationConfig, _schemaRegistry, pipes, errors) {
  12398. this._exprParser = _exprParser;
  12399. this._interpolationConfig = _interpolationConfig;
  12400. this._schemaRegistry = _schemaRegistry;
  12401. this.errors = errors;
  12402. this.pipesByName = null;
  12403. this._usedPipes = new Map();
  12404. // When the `pipes` parameter is `null`, do not check for used pipes
  12405. // This is used in IVY when we might not know the available pipes at compile time
  12406. if (pipes) {
  12407. const pipesByName = new Map();
  12408. pipes.forEach(pipe => pipesByName.set(pipe.name, pipe));
  12409. this.pipesByName = pipesByName;
  12410. }
  12411. }
  12412. get interpolationConfig() {
  12413. return this._interpolationConfig;
  12414. }
  12415. getUsedPipes() {
  12416. return Array.from(this._usedPipes.values());
  12417. }
  12418. createBoundHostProperties(dirMeta, sourceSpan) {
  12419. if (dirMeta.hostProperties) {
  12420. const boundProps = [];
  12421. Object.keys(dirMeta.hostProperties).forEach(propName => {
  12422. const expression = dirMeta.hostProperties[propName];
  12423. if (typeof expression === 'string') {
  12424. this.parsePropertyBinding(propName, expression, true, sourceSpan, sourceSpan.start.offset, undefined, [],
  12425. // Use the `sourceSpan` for `keySpan`. This isn't really accurate, but neither is the
  12426. // sourceSpan, as it represents the sourceSpan of the host itself rather than the
  12427. // source of the host binding (which doesn't exist in the template). Regardless,
  12428. // neither of these values are used in Ivy but are only here to satisfy the function
  12429. // signature. This should likely be refactored in the future so that `sourceSpan`
  12430. // isn't being used inaccurately.
  12431. boundProps, sourceSpan);
  12432. }
  12433. else {
  12434. this._reportError(`Value of the host property binding "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  12435. }
  12436. });
  12437. return boundProps;
  12438. }
  12439. return null;
  12440. }
  12441. createDirectiveHostPropertyAsts(dirMeta, elementSelector, sourceSpan) {
  12442. const boundProps = this.createBoundHostProperties(dirMeta, sourceSpan);
  12443. return boundProps &&
  12444. boundProps.map((prop) => this.createBoundElementProperty(elementSelector, prop));
  12445. }
  12446. createDirectiveHostEventAsts(dirMeta, sourceSpan) {
  12447. if (dirMeta.hostListeners) {
  12448. const targetEvents = [];
  12449. Object.keys(dirMeta.hostListeners).forEach(propName => {
  12450. const expression = dirMeta.hostListeners[propName];
  12451. if (typeof expression === 'string') {
  12452. // Use the `sourceSpan` for `keySpan` and `handlerSpan`. This isn't really accurate, but
  12453. // neither is the `sourceSpan`, as it represents the `sourceSpan` of the host itself
  12454. // rather than the source of the host binding (which doesn't exist in the template).
  12455. // Regardless, neither of these values are used in Ivy but are only here to satisfy the
  12456. // function signature. This should likely be refactored in the future so that `sourceSpan`
  12457. // isn't being used inaccurately.
  12458. this.parseEvent(propName, expression, sourceSpan, sourceSpan, [], targetEvents, sourceSpan);
  12459. }
  12460. else {
  12461. this._reportError(`Value of the host listener "${propName}" needs to be a string representing an expression but got "${expression}" (${typeof expression})`, sourceSpan);
  12462. }
  12463. });
  12464. return targetEvents;
  12465. }
  12466. return null;
  12467. }
  12468. parseInterpolation(value, sourceSpan) {
  12469. const sourceInfo = sourceSpan.start.toString();
  12470. const absoluteOffset = sourceSpan.fullStart.offset;
  12471. try {
  12472. const ast = this._exprParser.parseInterpolation(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  12473. if (ast)
  12474. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  12475. this._checkPipes(ast, sourceSpan);
  12476. return ast;
  12477. }
  12478. catch (e) {
  12479. this._reportError(`${e}`, sourceSpan);
  12480. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  12481. }
  12482. }
  12483. /**
  12484. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  12485. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  12486. * This is used for parsing the switch expression in ICUs.
  12487. */
  12488. parseInterpolationExpression(expression, sourceSpan) {
  12489. const sourceInfo = sourceSpan.start.toString();
  12490. const absoluteOffset = sourceSpan.start.offset;
  12491. try {
  12492. const ast = this._exprParser.parseInterpolationExpression(expression, sourceInfo, absoluteOffset);
  12493. if (ast)
  12494. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  12495. this._checkPipes(ast, sourceSpan);
  12496. return ast;
  12497. }
  12498. catch (e) {
  12499. this._reportError(`${e}`, sourceSpan);
  12500. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  12501. }
  12502. }
  12503. /**
  12504. * Parses the bindings in a microsyntax expression, and converts them to
  12505. * `ParsedProperty` or `ParsedVariable`.
  12506. *
  12507. * @param tplKey template binding name
  12508. * @param tplValue template binding value
  12509. * @param sourceSpan span of template binding relative to entire the template
  12510. * @param absoluteValueOffset start of the tplValue relative to the entire template
  12511. * @param targetMatchableAttrs potential attributes to match in the template
  12512. * @param targetProps target property bindings in the template
  12513. * @param targetVars target variables in the template
  12514. */
  12515. parseInlineTemplateBinding(tplKey, tplValue, sourceSpan, absoluteValueOffset, targetMatchableAttrs, targetProps, targetVars, isIvyAst) {
  12516. const absoluteKeyOffset = sourceSpan.start.offset + TEMPLATE_ATTR_PREFIX.length;
  12517. const bindings = this._parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset);
  12518. for (const binding of bindings) {
  12519. // sourceSpan is for the entire HTML attribute. bindingSpan is for a particular
  12520. // binding within the microsyntax expression so it's more narrow than sourceSpan.
  12521. const bindingSpan = moveParseSourceSpan(sourceSpan, binding.sourceSpan);
  12522. const key = binding.key.source;
  12523. const keySpan = moveParseSourceSpan(sourceSpan, binding.key.span);
  12524. if (binding instanceof VariableBinding) {
  12525. const value = binding.value ? binding.value.source : '$implicit';
  12526. const valueSpan = binding.value ? moveParseSourceSpan(sourceSpan, binding.value.span) : undefined;
  12527. targetVars.push(new ParsedVariable(key, value, bindingSpan, keySpan, valueSpan));
  12528. }
  12529. else if (binding.value) {
  12530. const srcSpan = isIvyAst ? bindingSpan : sourceSpan;
  12531. const valueSpan = moveParseSourceSpan(sourceSpan, binding.value.ast.sourceSpan);
  12532. this._parsePropertyAst(key, binding.value, srcSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  12533. }
  12534. else {
  12535. targetMatchableAttrs.push([key, '' /* value */]);
  12536. // Since this is a literal attribute with no RHS, source span should be
  12537. // just the key span.
  12538. this.parseLiteralAttr(key, null /* value */, keySpan, absoluteValueOffset, undefined /* valueSpan */, targetMatchableAttrs, targetProps, keySpan);
  12539. }
  12540. }
  12541. }
  12542. /**
  12543. * Parses the bindings in a microsyntax expression, e.g.
  12544. * ```
  12545. * <tag *tplKey="let value1 = prop; let value2 = localVar">
  12546. * ```
  12547. *
  12548. * @param tplKey template binding name
  12549. * @param tplValue template binding value
  12550. * @param sourceSpan span of template binding relative to entire the template
  12551. * @param absoluteKeyOffset start of the `tplKey`
  12552. * @param absoluteValueOffset start of the `tplValue`
  12553. */
  12554. _parseTemplateBindings(tplKey, tplValue, sourceSpan, absoluteKeyOffset, absoluteValueOffset) {
  12555. const sourceInfo = sourceSpan.start.toString();
  12556. try {
  12557. const bindingsResult = this._exprParser.parseTemplateBindings(tplKey, tplValue, sourceInfo, absoluteKeyOffset, absoluteValueOffset);
  12558. this._reportExpressionParserErrors(bindingsResult.errors, sourceSpan);
  12559. bindingsResult.templateBindings.forEach((binding) => {
  12560. if (binding.value instanceof ASTWithSource) {
  12561. this._checkPipes(binding.value, sourceSpan);
  12562. }
  12563. });
  12564. bindingsResult.warnings.forEach((warning) => {
  12565. this._reportError(warning, sourceSpan, ParseErrorLevel.WARNING);
  12566. });
  12567. return bindingsResult.templateBindings;
  12568. }
  12569. catch (e) {
  12570. this._reportError(`${e}`, sourceSpan);
  12571. return [];
  12572. }
  12573. }
  12574. parseLiteralAttr(name, value, sourceSpan, absoluteOffset, valueSpan, targetMatchableAttrs,
  12575. // TODO(atscott): keySpan is only optional here so VE template parser implementation does not
  12576. // have to change This should be required when VE is removed.
  12577. targetProps, keySpan) {
  12578. if (isAnimationLabel(name)) {
  12579. name = name.substring(1);
  12580. if (keySpan !== undefined) {
  12581. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  12582. }
  12583. if (value) {
  12584. this._reportError(`Assigning animation triggers via @prop="exp" attributes with an expression is invalid.` +
  12585. ` Use property bindings (e.g. [@prop]="exp") or use an attribute without a value (e.g. @prop) instead.`, sourceSpan, ParseErrorLevel.ERROR);
  12586. }
  12587. this._parseAnimation(name, value, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  12588. }
  12589. else {
  12590. targetProps.push(new ParsedProperty(name, this._exprParser.wrapLiteralPrimitive(value, '', absoluteOffset), ParsedPropertyType.LITERAL_ATTR, sourceSpan, keySpan, valueSpan));
  12591. }
  12592. }
  12593. parsePropertyBinding(name, expression, isHost, sourceSpan, absoluteOffset, valueSpan,
  12594. // TODO(atscott): keySpan is only optional here so VE template parser implementation does not
  12595. // have to change This should be required when VE is removed.
  12596. targetMatchableAttrs, targetProps, keySpan) {
  12597. if (name.length === 0) {
  12598. this._reportError(`Property name is missing in binding`, sourceSpan);
  12599. }
  12600. let isAnimationProp = false;
  12601. if (name.startsWith(ANIMATE_PROP_PREFIX)) {
  12602. isAnimationProp = true;
  12603. name = name.substring(ANIMATE_PROP_PREFIX.length);
  12604. if (keySpan !== undefined) {
  12605. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + ANIMATE_PROP_PREFIX.length, keySpan.end.offset));
  12606. }
  12607. }
  12608. else if (isAnimationLabel(name)) {
  12609. isAnimationProp = true;
  12610. name = name.substring(1);
  12611. if (keySpan !== undefined) {
  12612. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  12613. }
  12614. }
  12615. if (isAnimationProp) {
  12616. this._parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  12617. }
  12618. else {
  12619. this._parsePropertyAst(name, this._parseBinding(expression, isHost, valueSpan || sourceSpan, absoluteOffset), sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  12620. }
  12621. }
  12622. parsePropertyInterpolation(name, value, sourceSpan, valueSpan, targetMatchableAttrs,
  12623. // TODO(atscott): keySpan is only optional here so VE template parser implementation does not
  12624. // have to change This should be required when VE is removed.
  12625. targetProps, keySpan) {
  12626. const expr = this.parseInterpolation(value, valueSpan || sourceSpan);
  12627. if (expr) {
  12628. this._parsePropertyAst(name, expr, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps);
  12629. return true;
  12630. }
  12631. return false;
  12632. }
  12633. _parsePropertyAst(name, ast, sourceSpan, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  12634. targetMatchableAttrs.push([name, ast.source]);
  12635. targetProps.push(new ParsedProperty(name, ast, ParsedPropertyType.DEFAULT, sourceSpan, keySpan, valueSpan));
  12636. }
  12637. _parseAnimation(name, expression, sourceSpan, absoluteOffset, keySpan, valueSpan, targetMatchableAttrs, targetProps) {
  12638. if (name.length === 0) {
  12639. this._reportError('Animation trigger is missing', sourceSpan);
  12640. }
  12641. // This will occur when a @trigger is not paired with an expression.
  12642. // For animations it is valid to not have an expression since */void
  12643. // states will be applied by angular when the element is attached/detached
  12644. const ast = this._parseBinding(expression || 'undefined', false, valueSpan || sourceSpan, absoluteOffset);
  12645. targetMatchableAttrs.push([name, ast.source]);
  12646. targetProps.push(new ParsedProperty(name, ast, ParsedPropertyType.ANIMATION, sourceSpan, keySpan, valueSpan));
  12647. }
  12648. _parseBinding(value, isHostBinding, sourceSpan, absoluteOffset) {
  12649. const sourceInfo = (sourceSpan && sourceSpan.start || '(unknown)').toString();
  12650. try {
  12651. const ast = isHostBinding ?
  12652. this._exprParser.parseSimpleBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig) :
  12653. this._exprParser.parseBinding(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  12654. if (ast)
  12655. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  12656. this._checkPipes(ast, sourceSpan);
  12657. return ast;
  12658. }
  12659. catch (e) {
  12660. this._reportError(`${e}`, sourceSpan);
  12661. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  12662. }
  12663. }
  12664. createBoundElementProperty(elementSelector, boundProp, skipValidation = false, mapPropertyName = true) {
  12665. if (boundProp.isAnimation) {
  12666. return new BoundElementProperty(boundProp.name, 4 /* Animation */, SecurityContext.NONE, boundProp.expression, null, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  12667. }
  12668. let unit = null;
  12669. let bindingType = undefined;
  12670. let boundPropertyName = null;
  12671. const parts = boundProp.name.split(PROPERTY_PARTS_SEPARATOR);
  12672. let securityContexts = undefined;
  12673. // Check for special cases (prefix style, attr, class)
  12674. if (parts.length > 1) {
  12675. if (parts[0] == ATTRIBUTE_PREFIX) {
  12676. boundPropertyName = parts.slice(1).join(PROPERTY_PARTS_SEPARATOR);
  12677. if (!skipValidation) {
  12678. this._validatePropertyOrAttributeName(boundPropertyName, boundProp.sourceSpan, true);
  12679. }
  12680. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, boundPropertyName, true);
  12681. const nsSeparatorIdx = boundPropertyName.indexOf(':');
  12682. if (nsSeparatorIdx > -1) {
  12683. const ns = boundPropertyName.substring(0, nsSeparatorIdx);
  12684. const name = boundPropertyName.substring(nsSeparatorIdx + 1);
  12685. boundPropertyName = mergeNsAndName(ns, name);
  12686. }
  12687. bindingType = 1 /* Attribute */;
  12688. }
  12689. else if (parts[0] == CLASS_PREFIX) {
  12690. boundPropertyName = parts[1];
  12691. bindingType = 2 /* Class */;
  12692. securityContexts = [SecurityContext.NONE];
  12693. }
  12694. else if (parts[0] == STYLE_PREFIX) {
  12695. unit = parts.length > 2 ? parts[2] : null;
  12696. boundPropertyName = parts[1];
  12697. bindingType = 3 /* Style */;
  12698. securityContexts = [SecurityContext.STYLE];
  12699. }
  12700. }
  12701. // If not a special case, use the full property name
  12702. if (boundPropertyName === null) {
  12703. const mappedPropName = this._schemaRegistry.getMappedPropName(boundProp.name);
  12704. boundPropertyName = mapPropertyName ? mappedPropName : boundProp.name;
  12705. securityContexts = calcPossibleSecurityContexts(this._schemaRegistry, elementSelector, mappedPropName, false);
  12706. bindingType = 0 /* Property */;
  12707. if (!skipValidation) {
  12708. this._validatePropertyOrAttributeName(mappedPropName, boundProp.sourceSpan, false);
  12709. }
  12710. }
  12711. return new BoundElementProperty(boundPropertyName, bindingType, securityContexts[0], boundProp.expression, unit, boundProp.sourceSpan, boundProp.keySpan, boundProp.valueSpan);
  12712. }
  12713. // TODO: keySpan should be required but was made optional to avoid changing VE parser.
  12714. parseEvent(name, expression, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  12715. if (name.length === 0) {
  12716. this._reportError(`Event name is missing in binding`, sourceSpan);
  12717. }
  12718. if (isAnimationLabel(name)) {
  12719. name = name.substr(1);
  12720. if (keySpan !== undefined) {
  12721. keySpan = moveParseSourceSpan(keySpan, new AbsoluteSourceSpan(keySpan.start.offset + 1, keySpan.end.offset));
  12722. }
  12723. this._parseAnimationEvent(name, expression, sourceSpan, handlerSpan, targetEvents, keySpan);
  12724. }
  12725. else {
  12726. this._parseRegularEvent(name, expression, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan);
  12727. }
  12728. }
  12729. calcPossibleSecurityContexts(selector, propName, isAttribute) {
  12730. const prop = this._schemaRegistry.getMappedPropName(propName);
  12731. return calcPossibleSecurityContexts(this._schemaRegistry, selector, prop, isAttribute);
  12732. }
  12733. _parseAnimationEvent(name, expression, sourceSpan, handlerSpan, targetEvents, keySpan) {
  12734. const matches = splitAtPeriod(name, [name, '']);
  12735. const eventName = matches[0];
  12736. const phase = matches[1].toLowerCase();
  12737. const ast = this._parseAction(expression, handlerSpan);
  12738. targetEvents.push(new ParsedEvent(eventName, phase, 1 /* Animation */, ast, sourceSpan, handlerSpan, keySpan));
  12739. if (eventName.length === 0) {
  12740. this._reportError(`Animation event name is missing in binding`, sourceSpan);
  12741. }
  12742. if (phase) {
  12743. if (phase !== 'start' && phase !== 'done') {
  12744. this._reportError(`The provided animation output phase value "${phase}" for "@${eventName}" is not supported (use start or done)`, sourceSpan);
  12745. }
  12746. }
  12747. else {
  12748. this._reportError(`The animation trigger output event (@${eventName}) is missing its phase value name (start or done are currently supported)`, sourceSpan);
  12749. }
  12750. }
  12751. _parseRegularEvent(name, expression, sourceSpan, handlerSpan, targetMatchableAttrs, targetEvents, keySpan) {
  12752. // long format: 'target: eventName'
  12753. const [target, eventName] = splitAtColon(name, [null, name]);
  12754. const ast = this._parseAction(expression, handlerSpan);
  12755. targetMatchableAttrs.push([name, ast.source]);
  12756. targetEvents.push(new ParsedEvent(eventName, target, 0 /* Regular */, ast, sourceSpan, handlerSpan, keySpan));
  12757. // Don't detect directives for event names for now,
  12758. // so don't add the event name to the matchableAttrs
  12759. }
  12760. _parseAction(value, sourceSpan) {
  12761. const sourceInfo = (sourceSpan && sourceSpan.start || '(unknown').toString();
  12762. const absoluteOffset = (sourceSpan && sourceSpan.start) ? sourceSpan.start.offset : 0;
  12763. try {
  12764. const ast = this._exprParser.parseAction(value, sourceInfo, absoluteOffset, this._interpolationConfig);
  12765. if (ast) {
  12766. this._reportExpressionParserErrors(ast.errors, sourceSpan);
  12767. }
  12768. if (!ast || ast.ast instanceof EmptyExpr) {
  12769. this._reportError(`Empty expressions are not allowed`, sourceSpan);
  12770. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  12771. }
  12772. this._checkPipes(ast, sourceSpan);
  12773. return ast;
  12774. }
  12775. catch (e) {
  12776. this._reportError(`${e}`, sourceSpan);
  12777. return this._exprParser.wrapLiteralPrimitive('ERROR', sourceInfo, absoluteOffset);
  12778. }
  12779. }
  12780. _reportError(message, sourceSpan, level = ParseErrorLevel.ERROR) {
  12781. this.errors.push(new ParseError(sourceSpan, message, level));
  12782. }
  12783. _reportExpressionParserErrors(errors, sourceSpan) {
  12784. for (const error of errors) {
  12785. this._reportError(error.message, sourceSpan);
  12786. }
  12787. }
  12788. // Make sure all the used pipes are known in `this.pipesByName`
  12789. _checkPipes(ast, sourceSpan) {
  12790. if (ast && this.pipesByName) {
  12791. const collector = new PipeCollector();
  12792. ast.visit(collector);
  12793. collector.pipes.forEach((ast, pipeName) => {
  12794. const pipeMeta = this.pipesByName.get(pipeName);
  12795. if (!pipeMeta) {
  12796. this._reportError(`The pipe '${pipeName}' could not be found`, new ParseSourceSpan(sourceSpan.start.moveBy(ast.span.start), sourceSpan.start.moveBy(ast.span.end)));
  12797. }
  12798. else {
  12799. this._usedPipes.set(pipeName, pipeMeta);
  12800. }
  12801. });
  12802. }
  12803. }
  12804. /**
  12805. * @param propName the name of the property / attribute
  12806. * @param sourceSpan
  12807. * @param isAttr true when binding to an attribute
  12808. */
  12809. _validatePropertyOrAttributeName(propName, sourceSpan, isAttr) {
  12810. const report = isAttr ? this._schemaRegistry.validateAttribute(propName) :
  12811. this._schemaRegistry.validateProperty(propName);
  12812. if (report.error) {
  12813. this._reportError(report.msg, sourceSpan, ParseErrorLevel.ERROR);
  12814. }
  12815. }
  12816. }
  12817. class PipeCollector extends RecursiveAstVisitor$1 {
  12818. constructor() {
  12819. super(...arguments);
  12820. this.pipes = new Map();
  12821. }
  12822. visitPipe(ast, context) {
  12823. this.pipes.set(ast.name, ast);
  12824. ast.exp.visit(this);
  12825. this.visitAll(ast.args, context);
  12826. return null;
  12827. }
  12828. }
  12829. function isAnimationLabel(name) {
  12830. return name[0] == '@';
  12831. }
  12832. function calcPossibleSecurityContexts(registry, selector, propName, isAttribute) {
  12833. const ctxs = [];
  12834. CssSelector.parse(selector).forEach((selector) => {
  12835. const elementNames = selector.element ? [selector.element] : registry.allKnownElementNames();
  12836. const notElementNames = new Set(selector.notSelectors.filter(selector => selector.isElementSelector())
  12837. .map((selector) => selector.element));
  12838. const possibleElementNames = elementNames.filter(elementName => !notElementNames.has(elementName));
  12839. ctxs.push(...possibleElementNames.map(elementName => registry.securityContext(elementName, propName, isAttribute)));
  12840. });
  12841. return ctxs.length === 0 ? [SecurityContext.NONE] : Array.from(new Set(ctxs)).sort();
  12842. }
  12843. /**
  12844. * Compute a new ParseSourceSpan based off an original `sourceSpan` by using
  12845. * absolute offsets from the specified `absoluteSpan`.
  12846. *
  12847. * @param sourceSpan original source span
  12848. * @param absoluteSpan absolute source span to move to
  12849. */
  12850. function moveParseSourceSpan(sourceSpan, absoluteSpan) {
  12851. // The difference of two absolute offsets provide the relative offset
  12852. const startDiff = absoluteSpan.start - sourceSpan.start.offset;
  12853. const endDiff = absoluteSpan.end - sourceSpan.end.offset;
  12854. return new ParseSourceSpan(sourceSpan.start.moveBy(startDiff), sourceSpan.end.moveBy(endDiff), sourceSpan.fullStart.moveBy(startDiff), sourceSpan.details);
  12855. }
  12856. /**
  12857. * @license
  12858. * Copyright Google LLC All Rights Reserved.
  12859. *
  12860. * Use of this source code is governed by an MIT-style license that can be
  12861. * found in the LICENSE file at https://angular.io/license
  12862. */
  12863. const NG_CONTENT_SELECT_ATTR = 'select';
  12864. const LINK_ELEMENT = 'link';
  12865. const LINK_STYLE_REL_ATTR = 'rel';
  12866. const LINK_STYLE_HREF_ATTR = 'href';
  12867. const LINK_STYLE_REL_VALUE = 'stylesheet';
  12868. const STYLE_ELEMENT = 'style';
  12869. const SCRIPT_ELEMENT = 'script';
  12870. const NG_NON_BINDABLE_ATTR = 'ngNonBindable';
  12871. const NG_PROJECT_AS = 'ngProjectAs';
  12872. function preparseElement(ast) {
  12873. let selectAttr = null;
  12874. let hrefAttr = null;
  12875. let relAttr = null;
  12876. let nonBindable = false;
  12877. let projectAs = '';
  12878. ast.attrs.forEach(attr => {
  12879. const lcAttrName = attr.name.toLowerCase();
  12880. if (lcAttrName == NG_CONTENT_SELECT_ATTR) {
  12881. selectAttr = attr.value;
  12882. }
  12883. else if (lcAttrName == LINK_STYLE_HREF_ATTR) {
  12884. hrefAttr = attr.value;
  12885. }
  12886. else if (lcAttrName == LINK_STYLE_REL_ATTR) {
  12887. relAttr = attr.value;
  12888. }
  12889. else if (attr.name == NG_NON_BINDABLE_ATTR) {
  12890. nonBindable = true;
  12891. }
  12892. else if (attr.name == NG_PROJECT_AS) {
  12893. if (attr.value.length > 0) {
  12894. projectAs = attr.value;
  12895. }
  12896. }
  12897. });
  12898. selectAttr = normalizeNgContentSelect(selectAttr);
  12899. const nodeName = ast.name.toLowerCase();
  12900. let type = PreparsedElementType.OTHER;
  12901. if (isNgContent(nodeName)) {
  12902. type = PreparsedElementType.NG_CONTENT;
  12903. }
  12904. else if (nodeName == STYLE_ELEMENT) {
  12905. type = PreparsedElementType.STYLE;
  12906. }
  12907. else if (nodeName == SCRIPT_ELEMENT) {
  12908. type = PreparsedElementType.SCRIPT;
  12909. }
  12910. else if (nodeName == LINK_ELEMENT && relAttr == LINK_STYLE_REL_VALUE) {
  12911. type = PreparsedElementType.STYLESHEET;
  12912. }
  12913. return new PreparsedElement(type, selectAttr, hrefAttr, nonBindable, projectAs);
  12914. }
  12915. var PreparsedElementType;
  12916. (function (PreparsedElementType) {
  12917. PreparsedElementType[PreparsedElementType["NG_CONTENT"] = 0] = "NG_CONTENT";
  12918. PreparsedElementType[PreparsedElementType["STYLE"] = 1] = "STYLE";
  12919. PreparsedElementType[PreparsedElementType["STYLESHEET"] = 2] = "STYLESHEET";
  12920. PreparsedElementType[PreparsedElementType["SCRIPT"] = 3] = "SCRIPT";
  12921. PreparsedElementType[PreparsedElementType["OTHER"] = 4] = "OTHER";
  12922. })(PreparsedElementType || (PreparsedElementType = {}));
  12923. class PreparsedElement {
  12924. constructor(type, selectAttr, hrefAttr, nonBindable, projectAs) {
  12925. this.type = type;
  12926. this.selectAttr = selectAttr;
  12927. this.hrefAttr = hrefAttr;
  12928. this.nonBindable = nonBindable;
  12929. this.projectAs = projectAs;
  12930. }
  12931. }
  12932. function normalizeNgContentSelect(selectAttr) {
  12933. if (selectAttr === null || selectAttr.length === 0) {
  12934. return '*';
  12935. }
  12936. return selectAttr;
  12937. }
  12938. /**
  12939. * @license
  12940. * Copyright Google LLC All Rights Reserved.
  12941. *
  12942. * Use of this source code is governed by an MIT-style license that can be
  12943. * found in the LICENSE file at https://angular.io/license
  12944. */
  12945. const BIND_NAME_REGEXP = /^(?:(?:(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.*))|\[\(([^\)]+)\)\]|\[([^\]]+)\]|\(([^\)]+)\))$/;
  12946. // Group 1 = "bind-"
  12947. const KW_BIND_IDX = 1;
  12948. // Group 2 = "let-"
  12949. const KW_LET_IDX = 2;
  12950. // Group 3 = "ref-/#"
  12951. const KW_REF_IDX = 3;
  12952. // Group 4 = "on-"
  12953. const KW_ON_IDX = 4;
  12954. // Group 5 = "bindon-"
  12955. const KW_BINDON_IDX = 5;
  12956. // Group 6 = "@"
  12957. const KW_AT_IDX = 6;
  12958. // Group 7 = the identifier after "bind-", "let-", "ref-/#", "on-", "bindon-" or "@"
  12959. const IDENT_KW_IDX = 7;
  12960. // Group 8 = identifier inside [()]
  12961. const IDENT_BANANA_BOX_IDX = 8;
  12962. // Group 9 = identifier inside []
  12963. const IDENT_PROPERTY_IDX = 9;
  12964. // Group 10 = identifier inside ()
  12965. const IDENT_EVENT_IDX = 10;
  12966. const TEMPLATE_ATTR_PREFIX$1 = '*';
  12967. const CLASS_ATTR = 'class';
  12968. let _TEXT_CSS_SELECTOR;
  12969. function TEXT_CSS_SELECTOR() {
  12970. if (!_TEXT_CSS_SELECTOR) {
  12971. _TEXT_CSS_SELECTOR = CssSelector.parse('*')[0];
  12972. }
  12973. return _TEXT_CSS_SELECTOR;
  12974. }
  12975. class TemplateParseError extends ParseError {
  12976. constructor(message, span, level) {
  12977. super(span, message, level);
  12978. }
  12979. }
  12980. class TemplateParseResult {
  12981. constructor(templateAst, usedPipes, errors) {
  12982. this.templateAst = templateAst;
  12983. this.usedPipes = usedPipes;
  12984. this.errors = errors;
  12985. }
  12986. }
  12987. class TemplateParser {
  12988. constructor(_config, _reflector, _exprParser, _schemaRegistry, _htmlParser, _console, transforms) {
  12989. this._config = _config;
  12990. this._reflector = _reflector;
  12991. this._exprParser = _exprParser;
  12992. this._schemaRegistry = _schemaRegistry;
  12993. this._htmlParser = _htmlParser;
  12994. this._console = _console;
  12995. this.transforms = transforms;
  12996. }
  12997. get expressionParser() {
  12998. return this._exprParser;
  12999. }
  13000. parse(component, template, directives, pipes, schemas, templateUrl, preserveWhitespaces) {
  13001. var _a;
  13002. const result = this.tryParse(component, template, directives, pipes, schemas, templateUrl, preserveWhitespaces);
  13003. const warnings = result.errors.filter(error => error.level === ParseErrorLevel.WARNING);
  13004. const errors = result.errors.filter(error => error.level === ParseErrorLevel.ERROR);
  13005. if (warnings.length > 0) {
  13006. (_a = this._console) === null || _a === void 0 ? void 0 : _a.warn(`Template parse warnings:\n${warnings.join('\n')}`);
  13007. }
  13008. if (errors.length > 0) {
  13009. const errorString = errors.join('\n');
  13010. throw syntaxError(`Template parse errors:\n${errorString}`, errors);
  13011. }
  13012. return { template: result.templateAst, pipes: result.usedPipes };
  13013. }
  13014. tryParse(component, template, directives, pipes, schemas, templateUrl, preserveWhitespaces) {
  13015. let htmlParseResult = typeof template === 'string' ?
  13016. this._htmlParser.parse(template, templateUrl, {
  13017. tokenizeExpansionForms: true,
  13018. interpolationConfig: this.getInterpolationConfig(component)
  13019. }) :
  13020. template;
  13021. if (!preserveWhitespaces) {
  13022. htmlParseResult = removeWhitespaces(htmlParseResult);
  13023. }
  13024. return this.tryParseHtml(this.expandHtml(htmlParseResult), component, directives, pipes, schemas);
  13025. }
  13026. tryParseHtml(htmlAstWithErrors, component, directives, pipes, schemas) {
  13027. let result;
  13028. const errors = htmlAstWithErrors.errors;
  13029. const usedPipes = [];
  13030. if (htmlAstWithErrors.rootNodes.length > 0) {
  13031. const uniqDirectives = removeSummaryDuplicates(directives);
  13032. const uniqPipes = removeSummaryDuplicates(pipes);
  13033. const providerViewContext = new ProviderViewContext(this._reflector, component);
  13034. let interpolationConfig = undefined;
  13035. if (component.template && component.template.interpolation) {
  13036. interpolationConfig = {
  13037. start: component.template.interpolation[0],
  13038. end: component.template.interpolation[1]
  13039. };
  13040. }
  13041. const bindingParser = new BindingParser(this._exprParser, interpolationConfig, this._schemaRegistry, uniqPipes, errors);
  13042. const parseVisitor = new TemplateParseVisitor(this._reflector, this._config, providerViewContext, uniqDirectives, bindingParser, this._schemaRegistry, schemas, errors);
  13043. result = visitAll$1(parseVisitor, htmlAstWithErrors.rootNodes, EMPTY_ELEMENT_CONTEXT);
  13044. errors.push(...providerViewContext.errors);
  13045. usedPipes.push(...bindingParser.getUsedPipes());
  13046. }
  13047. else {
  13048. result = [];
  13049. }
  13050. this._assertNoReferenceDuplicationOnTemplate(result, errors);
  13051. if (errors.length > 0) {
  13052. return new TemplateParseResult(result, usedPipes, errors);
  13053. }
  13054. if (this.transforms) {
  13055. this.transforms.forEach((transform) => {
  13056. result = templateVisitAll(transform, result);
  13057. });
  13058. }
  13059. return new TemplateParseResult(result, usedPipes, errors);
  13060. }
  13061. expandHtml(htmlAstWithErrors, forced = false) {
  13062. const errors = htmlAstWithErrors.errors;
  13063. if (errors.length == 0 || forced) {
  13064. // Transform ICU messages to angular directives
  13065. const expandedHtmlAst = expandNodes(htmlAstWithErrors.rootNodes);
  13066. errors.push(...expandedHtmlAst.errors);
  13067. htmlAstWithErrors = new ParseTreeResult(expandedHtmlAst.nodes, errors);
  13068. }
  13069. return htmlAstWithErrors;
  13070. }
  13071. getInterpolationConfig(component) {
  13072. if (component.template) {
  13073. return InterpolationConfig.fromArray(component.template.interpolation);
  13074. }
  13075. return undefined;
  13076. }
  13077. /** @internal */
  13078. _assertNoReferenceDuplicationOnTemplate(result, errors) {
  13079. const existingReferences = [];
  13080. result.filter(element => !!element.references)
  13081. .forEach(element => element.references.forEach((reference) => {
  13082. const name = reference.name;
  13083. if (existingReferences.indexOf(name) < 0) {
  13084. existingReferences.push(name);
  13085. }
  13086. else {
  13087. const error = new TemplateParseError(`Reference "#${name}" is defined several times`, reference.sourceSpan, ParseErrorLevel.ERROR);
  13088. errors.push(error);
  13089. }
  13090. }));
  13091. }
  13092. }
  13093. class TemplateParseVisitor {
  13094. constructor(reflector, config, providerViewContext, directives, _bindingParser, _schemaRegistry, _schemas, _targetErrors) {
  13095. this.reflector = reflector;
  13096. this.config = config;
  13097. this.providerViewContext = providerViewContext;
  13098. this._bindingParser = _bindingParser;
  13099. this._schemaRegistry = _schemaRegistry;
  13100. this._schemas = _schemas;
  13101. this._targetErrors = _targetErrors;
  13102. this.selectorMatcher = new SelectorMatcher();
  13103. this.directivesIndex = new Map();
  13104. this.ngContentCount = 0;
  13105. // Note: queries start with id 1 so we can use the number in a Bloom filter!
  13106. this.contentQueryStartId = providerViewContext.component.viewQueries.length + 1;
  13107. directives.forEach((directive, index) => {
  13108. const selector = CssSelector.parse(directive.selector);
  13109. this.selectorMatcher.addSelectables(selector, directive);
  13110. this.directivesIndex.set(directive, index);
  13111. });
  13112. }
  13113. visitExpansion(expansion, context) {
  13114. return null;
  13115. }
  13116. visitExpansionCase(expansionCase, context) {
  13117. return null;
  13118. }
  13119. visitText(text, parent) {
  13120. const ngContentIndex = parent.findNgContentIndex(TEXT_CSS_SELECTOR());
  13121. const valueNoNgsp = replaceNgsp(text.value);
  13122. const expr = this._bindingParser.parseInterpolation(valueNoNgsp, text.sourceSpan);
  13123. return expr ? new BoundTextAst(expr, ngContentIndex, text.sourceSpan) :
  13124. new TextAst(valueNoNgsp, ngContentIndex, text.sourceSpan);
  13125. }
  13126. visitAttribute(attribute, context) {
  13127. return new AttrAst(attribute.name, attribute.value, attribute.sourceSpan);
  13128. }
  13129. visitComment(comment, context) {
  13130. return null;
  13131. }
  13132. visitElement(element, parent) {
  13133. const queryStartIndex = this.contentQueryStartId;
  13134. const elName = element.name;
  13135. const preparsedElement = preparseElement(element);
  13136. if (preparsedElement.type === PreparsedElementType.SCRIPT ||
  13137. preparsedElement.type === PreparsedElementType.STYLE) {
  13138. // Skipping <script> for security reasons
  13139. // Skipping <style> as we already processed them
  13140. // in the StyleCompiler
  13141. return null;
  13142. }
  13143. if (preparsedElement.type === PreparsedElementType.STYLESHEET &&
  13144. isStyleUrlResolvable(preparsedElement.hrefAttr)) {
  13145. // Skipping stylesheets with either relative urls or package scheme as we already processed
  13146. // them in the StyleCompiler
  13147. return null;
  13148. }
  13149. const matchableAttrs = [];
  13150. const elementOrDirectiveProps = [];
  13151. const elementOrDirectiveRefs = [];
  13152. const elementVars = [];
  13153. const events = [];
  13154. const templateElementOrDirectiveProps = [];
  13155. const templateMatchableAttrs = [];
  13156. const templateElementVars = [];
  13157. let hasInlineTemplates = false;
  13158. const attrs = [];
  13159. const isTemplateElement = isNgTemplate(element.name);
  13160. element.attrs.forEach(attr => {
  13161. const parsedVariables = [];
  13162. const hasBinding = this._parseAttr(isTemplateElement, attr, matchableAttrs, elementOrDirectiveProps, events, elementOrDirectiveRefs, elementVars);
  13163. elementVars.push(...parsedVariables.map(v => VariableAst.fromParsedVariable(v)));
  13164. let templateValue;
  13165. let templateKey;
  13166. const normalizedName = this._normalizeAttributeName(attr.name);
  13167. if (normalizedName.startsWith(TEMPLATE_ATTR_PREFIX$1)) {
  13168. templateValue = attr.value;
  13169. templateKey = normalizedName.substring(TEMPLATE_ATTR_PREFIX$1.length);
  13170. }
  13171. const hasTemplateBinding = templateValue != null;
  13172. if (hasTemplateBinding) {
  13173. if (hasInlineTemplates) {
  13174. this._reportError(`Can't have multiple template bindings on one element. Use only one attribute prefixed with *`, attr.sourceSpan);
  13175. }
  13176. hasInlineTemplates = true;
  13177. const parsedVariables = [];
  13178. const absoluteOffset = (attr.valueSpan || attr.sourceSpan).start.offset;
  13179. this._bindingParser.parseInlineTemplateBinding(templateKey, templateValue, attr.sourceSpan, absoluteOffset, templateMatchableAttrs, templateElementOrDirectiveProps, parsedVariables, false /* isIvyAst */);
  13180. templateElementVars.push(...parsedVariables.map(v => VariableAst.fromParsedVariable(v)));
  13181. }
  13182. if (!hasBinding && !hasTemplateBinding) {
  13183. // don't include the bindings as attributes as well in the AST
  13184. attrs.push(this.visitAttribute(attr, null));
  13185. matchableAttrs.push([attr.name, attr.value]);
  13186. }
  13187. });
  13188. const elementCssSelector = createElementCssSelector(elName, matchableAttrs);
  13189. const { directives: directiveMetas, matchElement } = this._parseDirectives(this.selectorMatcher, elementCssSelector);
  13190. const references = [];
  13191. const boundDirectivePropNames = new Set();
  13192. const directiveAsts = this._createDirectiveAsts(isTemplateElement, element.name, directiveMetas, elementOrDirectiveProps, elementOrDirectiveRefs, element.sourceSpan, references, boundDirectivePropNames);
  13193. const elementProps = this._createElementPropertyAsts(element.name, elementOrDirectiveProps, boundDirectivePropNames);
  13194. const isViewRoot = parent.isTemplateElement || hasInlineTemplates;
  13195. const providerContext = new ProviderElementContext(this.providerViewContext, parent.providerContext, isViewRoot, directiveAsts, attrs, references, isTemplateElement, queryStartIndex, element.sourceSpan);
  13196. const children = visitAll$1(preparsedElement.nonBindable ? NON_BINDABLE_VISITOR : this, element.children, ElementContext.create(isTemplateElement, directiveAsts, isTemplateElement ? parent.providerContext : providerContext));
  13197. providerContext.afterElement();
  13198. // Override the actual selector when the `ngProjectAs` attribute is provided
  13199. const projectionSelector = preparsedElement.projectAs != '' ?
  13200. CssSelector.parse(preparsedElement.projectAs)[0] :
  13201. elementCssSelector;
  13202. const ngContentIndex = parent.findNgContentIndex(projectionSelector);
  13203. let parsedElement;
  13204. if (preparsedElement.type === PreparsedElementType.NG_CONTENT) {
  13205. // `<ng-content>` element
  13206. if (element.children && !element.children.every(_isEmptyTextNode)) {
  13207. this._reportError(`<ng-content> element cannot have content.`, element.sourceSpan);
  13208. }
  13209. parsedElement = new NgContentAst(this.ngContentCount++, hasInlineTemplates ? null : ngContentIndex, element.sourceSpan);
  13210. }
  13211. else if (isTemplateElement) {
  13212. // `<ng-template>` element
  13213. this._assertAllEventsPublishedByDirectives(directiveAsts, events);
  13214. this._assertNoComponentsNorElementBindingsOnTemplate(directiveAsts, elementProps, element.sourceSpan);
  13215. parsedElement = new EmbeddedTemplateAst(attrs, events, references, elementVars, providerContext.transformedDirectiveAsts, providerContext.transformProviders, providerContext.transformedHasViewContainer, providerContext.queryMatches, children, hasInlineTemplates ? null : ngContentIndex, element.sourceSpan);
  13216. }
  13217. else {
  13218. // element other than `<ng-content>` and `<ng-template>`
  13219. this._assertElementExists(matchElement, element);
  13220. this._assertOnlyOneComponent(directiveAsts, element.sourceSpan);
  13221. const ngContentIndex = hasInlineTemplates ? null : parent.findNgContentIndex(projectionSelector);
  13222. parsedElement = new ElementAst(elName, attrs, elementProps, events, references, providerContext.transformedDirectiveAsts, providerContext.transformProviders, providerContext.transformedHasViewContainer, providerContext.queryMatches, children, hasInlineTemplates ? null : ngContentIndex, element.sourceSpan, element.endSourceSpan || null);
  13223. }
  13224. if (hasInlineTemplates) {
  13225. // The element as a *-attribute
  13226. const templateQueryStartIndex = this.contentQueryStartId;
  13227. const templateSelector = createElementCssSelector('ng-template', templateMatchableAttrs);
  13228. const { directives } = this._parseDirectives(this.selectorMatcher, templateSelector);
  13229. const templateBoundDirectivePropNames = new Set();
  13230. const templateDirectiveAsts = this._createDirectiveAsts(true, elName, directives, templateElementOrDirectiveProps, [], element.sourceSpan, [], templateBoundDirectivePropNames);
  13231. const templateElementProps = this._createElementPropertyAsts(elName, templateElementOrDirectiveProps, templateBoundDirectivePropNames);
  13232. this._assertNoComponentsNorElementBindingsOnTemplate(templateDirectiveAsts, templateElementProps, element.sourceSpan);
  13233. const templateProviderContext = new ProviderElementContext(this.providerViewContext, parent.providerContext, parent.isTemplateElement, templateDirectiveAsts, [], [], true, templateQueryStartIndex, element.sourceSpan);
  13234. templateProviderContext.afterElement();
  13235. parsedElement = new EmbeddedTemplateAst([], [], [], templateElementVars, templateProviderContext.transformedDirectiveAsts, templateProviderContext.transformProviders, templateProviderContext.transformedHasViewContainer, templateProviderContext.queryMatches, [parsedElement], ngContentIndex, element.sourceSpan);
  13236. }
  13237. return parsedElement;
  13238. }
  13239. _parseAttr(isTemplateElement, attr, targetMatchableAttrs, targetProps, targetEvents, targetRefs, targetVars) {
  13240. const name = this._normalizeAttributeName(attr.name);
  13241. const value = attr.value;
  13242. const srcSpan = attr.sourceSpan;
  13243. const absoluteOffset = attr.valueSpan ? attr.valueSpan.start.offset : srcSpan.start.offset;
  13244. const boundEvents = [];
  13245. const bindParts = name.match(BIND_NAME_REGEXP);
  13246. let hasBinding = false;
  13247. if (bindParts !== null) {
  13248. hasBinding = true;
  13249. if (bindParts[KW_BIND_IDX] != null) {
  13250. this._bindingParser.parsePropertyBinding(bindParts[IDENT_KW_IDX], value, false, srcSpan, absoluteOffset, attr.valueSpan, targetMatchableAttrs, targetProps);
  13251. }
  13252. else if (bindParts[KW_LET_IDX]) {
  13253. if (isTemplateElement) {
  13254. const identifier = bindParts[IDENT_KW_IDX];
  13255. this._parseVariable(identifier, value, srcSpan, targetVars);
  13256. }
  13257. else {
  13258. this._reportError(`"let-" is only supported on ng-template elements.`, srcSpan);
  13259. }
  13260. }
  13261. else if (bindParts[KW_REF_IDX]) {
  13262. const identifier = bindParts[IDENT_KW_IDX];
  13263. this._parseReference(identifier, value, srcSpan, targetRefs);
  13264. }
  13265. else if (bindParts[KW_ON_IDX]) {
  13266. this._bindingParser.parseEvent(bindParts[IDENT_KW_IDX], value, srcSpan, attr.valueSpan || srcSpan, targetMatchableAttrs, boundEvents);
  13267. }
  13268. else if (bindParts[KW_BINDON_IDX]) {
  13269. this._bindingParser.parsePropertyBinding(bindParts[IDENT_KW_IDX], value, false, srcSpan, absoluteOffset, attr.valueSpan, targetMatchableAttrs, targetProps);
  13270. this._parseAssignmentEvent(bindParts[IDENT_KW_IDX], value, srcSpan, attr.valueSpan || srcSpan, targetMatchableAttrs, boundEvents);
  13271. }
  13272. else if (bindParts[KW_AT_IDX]) {
  13273. this._bindingParser.parseLiteralAttr(name, value, srcSpan, absoluteOffset, attr.valueSpan, targetMatchableAttrs, targetProps);
  13274. }
  13275. else if (bindParts[IDENT_BANANA_BOX_IDX]) {
  13276. this._bindingParser.parsePropertyBinding(bindParts[IDENT_BANANA_BOX_IDX], value, false, srcSpan, absoluteOffset, attr.valueSpan, targetMatchableAttrs, targetProps);
  13277. this._parseAssignmentEvent(bindParts[IDENT_BANANA_BOX_IDX], value, srcSpan, attr.valueSpan || srcSpan, targetMatchableAttrs, boundEvents);
  13278. }
  13279. else if (bindParts[IDENT_PROPERTY_IDX]) {
  13280. this._bindingParser.parsePropertyBinding(bindParts[IDENT_PROPERTY_IDX], value, false, srcSpan, absoluteOffset, attr.valueSpan, targetMatchableAttrs, targetProps);
  13281. }
  13282. else if (bindParts[IDENT_EVENT_IDX]) {
  13283. this._bindingParser.parseEvent(bindParts[IDENT_EVENT_IDX], value, srcSpan, attr.valueSpan || srcSpan, targetMatchableAttrs, boundEvents);
  13284. }
  13285. }
  13286. else {
  13287. hasBinding = this._bindingParser.parsePropertyInterpolation(name, value, srcSpan, attr.valueSpan, targetMatchableAttrs, targetProps);
  13288. }
  13289. if (!hasBinding) {
  13290. this._bindingParser.parseLiteralAttr(name, value, srcSpan, absoluteOffset, attr.valueSpan, targetMatchableAttrs, targetProps);
  13291. }
  13292. targetEvents.push(...boundEvents.map(e => BoundEventAst.fromParsedEvent(e)));
  13293. return hasBinding;
  13294. }
  13295. _normalizeAttributeName(attrName) {
  13296. return /^data-/i.test(attrName) ? attrName.substring(5) : attrName;
  13297. }
  13298. _parseVariable(identifier, value, sourceSpan, targetVars) {
  13299. if (identifier.indexOf('-') > -1) {
  13300. this._reportError(`"-" is not allowed in variable names`, sourceSpan);
  13301. }
  13302. else if (identifier.length === 0) {
  13303. this._reportError(`Variable does not have a name`, sourceSpan);
  13304. }
  13305. targetVars.push(new VariableAst(identifier, value, sourceSpan));
  13306. }
  13307. _parseReference(identifier, value, sourceSpan, targetRefs) {
  13308. if (identifier.indexOf('-') > -1) {
  13309. this._reportError(`"-" is not allowed in reference names`, sourceSpan);
  13310. }
  13311. else if (identifier.length === 0) {
  13312. this._reportError(`Reference does not have a name`, sourceSpan);
  13313. }
  13314. targetRefs.push(new ElementOrDirectiveRef(identifier, value, sourceSpan));
  13315. }
  13316. _parseAssignmentEvent(name, expression, sourceSpan, valueSpan, targetMatchableAttrs, targetEvents) {
  13317. this._bindingParser.parseEvent(`${name}Change`, `${expression}=$event`, sourceSpan, valueSpan, targetMatchableAttrs, targetEvents);
  13318. }
  13319. _parseDirectives(selectorMatcher, elementCssSelector) {
  13320. // Need to sort the directives so that we get consistent results throughout,
  13321. // as selectorMatcher uses Maps inside.
  13322. // Also deduplicate directives as they might match more than one time!
  13323. const directives = newArray(this.directivesIndex.size);
  13324. // Whether any directive selector matches on the element name
  13325. let matchElement = false;
  13326. selectorMatcher.match(elementCssSelector, (selector, directive) => {
  13327. directives[this.directivesIndex.get(directive)] = directive;
  13328. matchElement = matchElement || selector.hasElementSelector();
  13329. });
  13330. return {
  13331. directives: directives.filter(dir => !!dir),
  13332. matchElement,
  13333. };
  13334. }
  13335. _createDirectiveAsts(isTemplateElement, elementName, directives, props, elementOrDirectiveRefs, elementSourceSpan, targetReferences, targetBoundDirectivePropNames) {
  13336. const matchedReferences = new Set();
  13337. let component = null;
  13338. const directiveAsts = directives.map((directive) => {
  13339. const sourceSpan = new ParseSourceSpan(elementSourceSpan.start, elementSourceSpan.end, elementSourceSpan.fullStart, `Directive ${identifierName(directive.type)}`);
  13340. if (directive.isComponent) {
  13341. component = directive;
  13342. }
  13343. const directiveProperties = [];
  13344. const boundProperties = this._bindingParser.createDirectiveHostPropertyAsts(directive, elementName, sourceSpan);
  13345. let hostProperties = boundProperties.map(prop => BoundElementPropertyAst.fromBoundProperty(prop));
  13346. // Note: We need to check the host properties here as well,
  13347. // as we don't know the element name in the DirectiveWrapperCompiler yet.
  13348. hostProperties = this._checkPropertiesInSchema(elementName, hostProperties);
  13349. const parsedEvents = this._bindingParser.createDirectiveHostEventAsts(directive, sourceSpan);
  13350. this._createDirectivePropertyAsts(directive.inputs, props, directiveProperties, targetBoundDirectivePropNames);
  13351. elementOrDirectiveRefs.forEach((elOrDirRef) => {
  13352. if ((elOrDirRef.value.length === 0 && directive.isComponent) ||
  13353. (elOrDirRef.isReferenceToDirective(directive))) {
  13354. targetReferences.push(new ReferenceAst(elOrDirRef.name, createTokenForReference(directive.type.reference), elOrDirRef.value, elOrDirRef.sourceSpan));
  13355. matchedReferences.add(elOrDirRef.name);
  13356. }
  13357. });
  13358. const hostEvents = parsedEvents.map(e => BoundEventAst.fromParsedEvent(e));
  13359. const contentQueryStartId = this.contentQueryStartId;
  13360. this.contentQueryStartId += directive.queries.length;
  13361. return new DirectiveAst(directive, directiveProperties, hostProperties, hostEvents, contentQueryStartId, sourceSpan);
  13362. });
  13363. elementOrDirectiveRefs.forEach((elOrDirRef) => {
  13364. if (elOrDirRef.value.length > 0) {
  13365. if (!matchedReferences.has(elOrDirRef.name)) {
  13366. this._reportError(`There is no directive with "exportAs" set to "${elOrDirRef.value}"`, elOrDirRef.sourceSpan);
  13367. }
  13368. }
  13369. else if (!component) {
  13370. let refToken = null;
  13371. if (isTemplateElement) {
  13372. refToken = createTokenForExternalReference(this.reflector, Identifiers$1.TemplateRef);
  13373. }
  13374. targetReferences.push(new ReferenceAst(elOrDirRef.name, refToken, elOrDirRef.value, elOrDirRef.sourceSpan));
  13375. }
  13376. });
  13377. return directiveAsts;
  13378. }
  13379. _createDirectivePropertyAsts(directiveProperties, boundProps, targetBoundDirectiveProps, targetBoundDirectivePropNames) {
  13380. if (directiveProperties) {
  13381. const boundPropsByName = new Map();
  13382. boundProps.forEach(boundProp => {
  13383. const prevValue = boundPropsByName.get(boundProp.name);
  13384. if (!prevValue || prevValue.isLiteral) {
  13385. // give [a]="b" a higher precedence than a="b" on the same element
  13386. boundPropsByName.set(boundProp.name, boundProp);
  13387. }
  13388. });
  13389. Object.keys(directiveProperties).forEach(dirProp => {
  13390. const elProp = directiveProperties[dirProp];
  13391. const boundProp = boundPropsByName.get(elProp);
  13392. // Bindings are optional, so this binding only needs to be set up if an expression is given.
  13393. if (boundProp) {
  13394. targetBoundDirectivePropNames.add(boundProp.name);
  13395. if (!isEmptyExpression(boundProp.expression)) {
  13396. targetBoundDirectiveProps.push(new BoundDirectivePropertyAst(dirProp, boundProp.name, boundProp.expression, boundProp.sourceSpan));
  13397. }
  13398. }
  13399. });
  13400. }
  13401. }
  13402. _createElementPropertyAsts(elementName, props, boundDirectivePropNames) {
  13403. const boundElementProps = [];
  13404. props.forEach((prop) => {
  13405. if (!prop.isLiteral && !boundDirectivePropNames.has(prop.name)) {
  13406. const boundProp = this._bindingParser.createBoundElementProperty(elementName, prop);
  13407. boundElementProps.push(BoundElementPropertyAst.fromBoundProperty(boundProp));
  13408. }
  13409. });
  13410. return this._checkPropertiesInSchema(elementName, boundElementProps);
  13411. }
  13412. _findComponentDirectives(directives) {
  13413. return directives.filter(directive => directive.directive.isComponent);
  13414. }
  13415. _findComponentDirectiveNames(directives) {
  13416. return this._findComponentDirectives(directives)
  13417. .map(directive => identifierName(directive.directive.type));
  13418. }
  13419. _assertOnlyOneComponent(directives, sourceSpan) {
  13420. const componentTypeNames = this._findComponentDirectiveNames(directives);
  13421. if (componentTypeNames.length > 1) {
  13422. this._reportError(`More than one component matched on this element.\n` +
  13423. `Make sure that only one component's selector can match a given element.\n` +
  13424. `Conflicting components: ${componentTypeNames.join(',')}`, sourceSpan);
  13425. }
  13426. }
  13427. /**
  13428. * Make sure that non-angular tags conform to the schemas.
  13429. *
  13430. * Note: An element is considered an angular tag when at least one directive selector matches the
  13431. * tag name.
  13432. *
  13433. * @param matchElement Whether any directive has matched on the tag name
  13434. * @param element the html element
  13435. */
  13436. _assertElementExists(matchElement, element) {
  13437. const elName = element.name.replace(/^:xhtml:/, '');
  13438. if (!matchElement && !this._schemaRegistry.hasElement(elName, this._schemas)) {
  13439. let errorMsg = `'${elName}' is not a known element:\n`;
  13440. errorMsg += `1. If '${elName}' is an Angular component, then verify that it is part of this module.\n`;
  13441. if (elName.indexOf('-') > -1) {
  13442. errorMsg += `2. If '${elName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.`;
  13443. }
  13444. else {
  13445. errorMsg +=
  13446. `2. To allow any element add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`;
  13447. }
  13448. this._reportError(errorMsg, element.sourceSpan);
  13449. }
  13450. }
  13451. _assertNoComponentsNorElementBindingsOnTemplate(directives, elementProps, sourceSpan) {
  13452. const componentTypeNames = this._findComponentDirectiveNames(directives);
  13453. if (componentTypeNames.length > 0) {
  13454. this._reportError(`Components on an embedded template: ${componentTypeNames.join(',')}`, sourceSpan);
  13455. }
  13456. elementProps.forEach(prop => {
  13457. this._reportError(`Property binding ${prop.name} not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations".`, sourceSpan);
  13458. });
  13459. }
  13460. _assertAllEventsPublishedByDirectives(directives, events) {
  13461. const allDirectiveEvents = new Set();
  13462. directives.forEach(directive => {
  13463. Object.keys(directive.directive.outputs).forEach(k => {
  13464. const eventName = directive.directive.outputs[k];
  13465. allDirectiveEvents.add(eventName);
  13466. });
  13467. });
  13468. events.forEach(event => {
  13469. if (event.target != null || !allDirectiveEvents.has(event.name)) {
  13470. this._reportError(`Event binding ${event
  13471. .fullName} not emitted by any directive on an embedded template. Make sure that the event name is spelled correctly and all directives are listed in the "@NgModule.declarations".`, event.sourceSpan);
  13472. }
  13473. });
  13474. }
  13475. _checkPropertiesInSchema(elementName, boundProps) {
  13476. // Note: We can't filter out empty expressions before this method,
  13477. // as we still want to validate them!
  13478. return boundProps.filter((boundProp) => {
  13479. if (boundProp.type === 0 /* Property */ &&
  13480. !this._schemaRegistry.hasProperty(elementName, boundProp.name, this._schemas)) {
  13481. let errorMsg = `Can't bind to '${boundProp.name}' since it isn't a known property of '${elementName}'.`;
  13482. if (elementName.startsWith('ng-')) {
  13483. errorMsg +=
  13484. `\n1. If '${boundProp
  13485. .name}' is an Angular directive, then add 'CommonModule' to the '@NgModule.imports' of this component.` +
  13486. `\n2. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`;
  13487. }
  13488. else if (elementName.indexOf('-') > -1) {
  13489. errorMsg +=
  13490. `\n1. If '${elementName}' is an Angular component and it has '${boundProp.name}' input, then verify that it is part of this module.` +
  13491. `\n2. If '${elementName}' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.` +
  13492. `\n3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component.`;
  13493. }
  13494. this._reportError(errorMsg, boundProp.sourceSpan);
  13495. }
  13496. return !isEmptyExpression(boundProp.value);
  13497. });
  13498. }
  13499. _reportError(message, sourceSpan, level = ParseErrorLevel.ERROR) {
  13500. this._targetErrors.push(new ParseError(sourceSpan, message, level));
  13501. }
  13502. }
  13503. class NonBindableVisitor {
  13504. visitElement(ast, parent) {
  13505. const preparsedElement = preparseElement(ast);
  13506. if (preparsedElement.type === PreparsedElementType.SCRIPT ||
  13507. preparsedElement.type === PreparsedElementType.STYLE ||
  13508. preparsedElement.type === PreparsedElementType.STYLESHEET) {
  13509. // Skipping <script> for security reasons
  13510. // Skipping <style> and stylesheets as we already processed them
  13511. // in the StyleCompiler
  13512. return null;
  13513. }
  13514. const attrNameAndValues = ast.attrs.map((attr) => [attr.name, attr.value]);
  13515. const selector = createElementCssSelector(ast.name, attrNameAndValues);
  13516. const ngContentIndex = parent.findNgContentIndex(selector);
  13517. const children = visitAll$1(this, ast.children, EMPTY_ELEMENT_CONTEXT);
  13518. return new ElementAst(ast.name, visitAll$1(this, ast.attrs), [], [], [], [], [], false, [], children, ngContentIndex, ast.sourceSpan, ast.endSourceSpan);
  13519. }
  13520. visitComment(comment, context) {
  13521. return null;
  13522. }
  13523. visitAttribute(attribute, context) {
  13524. return new AttrAst(attribute.name, attribute.value, attribute.sourceSpan);
  13525. }
  13526. visitText(text, parent) {
  13527. const ngContentIndex = parent.findNgContentIndex(TEXT_CSS_SELECTOR());
  13528. return new TextAst(text.value, ngContentIndex, text.sourceSpan);
  13529. }
  13530. visitExpansion(expansion, context) {
  13531. return expansion;
  13532. }
  13533. visitExpansionCase(expansionCase, context) {
  13534. return expansionCase;
  13535. }
  13536. }
  13537. /**
  13538. * A reference to an element or directive in a template. E.g., the reference in this template:
  13539. *
  13540. * <div #myMenu="coolMenu">
  13541. *
  13542. * would be {name: 'myMenu', value: 'coolMenu', sourceSpan: ...}
  13543. */
  13544. class ElementOrDirectiveRef {
  13545. constructor(name, value, sourceSpan) {
  13546. this.name = name;
  13547. this.value = value;
  13548. this.sourceSpan = sourceSpan;
  13549. }
  13550. /** Gets whether this is a reference to the given directive. */
  13551. isReferenceToDirective(directive) {
  13552. return splitExportAs(directive.exportAs).indexOf(this.value) !== -1;
  13553. }
  13554. }
  13555. /** Splits a raw, potentially comma-delimited `exportAs` value into an array of names. */
  13556. function splitExportAs(exportAs) {
  13557. return exportAs ? exportAs.split(',').map(e => e.trim()) : [];
  13558. }
  13559. function splitClasses(classAttrValue) {
  13560. return classAttrValue.trim().split(/\s+/g);
  13561. }
  13562. class ElementContext {
  13563. constructor(isTemplateElement, _ngContentIndexMatcher, _wildcardNgContentIndex, providerContext) {
  13564. this.isTemplateElement = isTemplateElement;
  13565. this._ngContentIndexMatcher = _ngContentIndexMatcher;
  13566. this._wildcardNgContentIndex = _wildcardNgContentIndex;
  13567. this.providerContext = providerContext;
  13568. }
  13569. static create(isTemplateElement, directives, providerContext) {
  13570. const matcher = new SelectorMatcher();
  13571. let wildcardNgContentIndex = null;
  13572. const component = directives.find(directive => directive.directive.isComponent);
  13573. if (component) {
  13574. const ngContentSelectors = component.directive.template.ngContentSelectors;
  13575. for (let i = 0; i < ngContentSelectors.length; i++) {
  13576. const selector = ngContentSelectors[i];
  13577. if (selector === '*') {
  13578. wildcardNgContentIndex = i;
  13579. }
  13580. else {
  13581. matcher.addSelectables(CssSelector.parse(ngContentSelectors[i]), i);
  13582. }
  13583. }
  13584. }
  13585. return new ElementContext(isTemplateElement, matcher, wildcardNgContentIndex, providerContext);
  13586. }
  13587. findNgContentIndex(selector) {
  13588. const ngContentIndices = [];
  13589. this._ngContentIndexMatcher.match(selector, (selector, ngContentIndex) => {
  13590. ngContentIndices.push(ngContentIndex);
  13591. });
  13592. ngContentIndices.sort();
  13593. if (this._wildcardNgContentIndex != null) {
  13594. ngContentIndices.push(this._wildcardNgContentIndex);
  13595. }
  13596. return ngContentIndices.length > 0 ? ngContentIndices[0] : null;
  13597. }
  13598. }
  13599. function createElementCssSelector(elementName, attributes) {
  13600. const cssSelector = new CssSelector();
  13601. const elNameNoNs = splitNsName(elementName)[1];
  13602. cssSelector.setElement(elNameNoNs);
  13603. for (let i = 0; i < attributes.length; i++) {
  13604. const attrName = attributes[i][0];
  13605. const attrNameNoNs = splitNsName(attrName)[1];
  13606. const attrValue = attributes[i][1];
  13607. cssSelector.addAttribute(attrNameNoNs, attrValue);
  13608. if (attrName.toLowerCase() == CLASS_ATTR) {
  13609. const classes = splitClasses(attrValue);
  13610. classes.forEach(className => cssSelector.addClassName(className));
  13611. }
  13612. }
  13613. return cssSelector;
  13614. }
  13615. const EMPTY_ELEMENT_CONTEXT = new ElementContext(true, new SelectorMatcher(), null, null);
  13616. const NON_BINDABLE_VISITOR = new NonBindableVisitor();
  13617. function _isEmptyTextNode(node) {
  13618. return node instanceof Text$3 && node.value.trim().length == 0;
  13619. }
  13620. function removeSummaryDuplicates(items) {
  13621. const map = new Map();
  13622. items.forEach((item) => {
  13623. if (!map.get(item.type.reference)) {
  13624. map.set(item.type.reference, item);
  13625. }
  13626. });
  13627. return Array.from(map.values());
  13628. }
  13629. function isEmptyExpression(ast) {
  13630. if (ast instanceof ASTWithSource) {
  13631. ast = ast.ast;
  13632. }
  13633. return ast instanceof EmptyExpr;
  13634. }
  13635. /**
  13636. * @license
  13637. * Copyright Google LLC All Rights Reserved.
  13638. *
  13639. * Use of this source code is governed by an MIT-style license that can be
  13640. * found in the LICENSE file at https://angular.io/license
  13641. */
  13642. /**
  13643. * Parses string representation of a style and converts it into object literal.
  13644. *
  13645. * @param value string representation of style as used in the `style` attribute in HTML.
  13646. * Example: `color: red; height: auto`.
  13647. * @returns An array of style property name and value pairs, e.g. `['color', 'red', 'height',
  13648. * 'auto']`
  13649. */
  13650. function parse(value) {
  13651. // we use a string array here instead of a string map
  13652. // because a string-map is not guaranteed to retain the
  13653. // order of the entries whereas a string array can be
  13654. // constructed in a [key, value, key, value] format.
  13655. const styles = [];
  13656. let i = 0;
  13657. let parenDepth = 0;
  13658. let quote = 0 /* QuoteNone */;
  13659. let valueStart = 0;
  13660. let propStart = 0;
  13661. let currentProp = null;
  13662. let valueHasQuotes = false;
  13663. while (i < value.length) {
  13664. const token = value.charCodeAt(i++);
  13665. switch (token) {
  13666. case 40 /* OpenParen */:
  13667. parenDepth++;
  13668. break;
  13669. case 41 /* CloseParen */:
  13670. parenDepth--;
  13671. break;
  13672. case 39 /* QuoteSingle */:
  13673. // valueStart needs to be there since prop values don't
  13674. // have quotes in CSS
  13675. valueHasQuotes = valueHasQuotes || valueStart > 0;
  13676. if (quote === 0 /* QuoteNone */) {
  13677. quote = 39 /* QuoteSingle */;
  13678. }
  13679. else if (quote === 39 /* QuoteSingle */ && value.charCodeAt(i - 1) !== 92 /* BackSlash */) {
  13680. quote = 0 /* QuoteNone */;
  13681. }
  13682. break;
  13683. case 34 /* QuoteDouble */:
  13684. // same logic as above
  13685. valueHasQuotes = valueHasQuotes || valueStart > 0;
  13686. if (quote === 0 /* QuoteNone */) {
  13687. quote = 34 /* QuoteDouble */;
  13688. }
  13689. else if (quote === 34 /* QuoteDouble */ && value.charCodeAt(i - 1) !== 92 /* BackSlash */) {
  13690. quote = 0 /* QuoteNone */;
  13691. }
  13692. break;
  13693. case 58 /* Colon */:
  13694. if (!currentProp && parenDepth === 0 && quote === 0 /* QuoteNone */) {
  13695. currentProp = hyphenate(value.substring(propStart, i - 1).trim());
  13696. valueStart = i;
  13697. }
  13698. break;
  13699. case 59 /* Semicolon */:
  13700. if (currentProp && valueStart > 0 && parenDepth === 0 && quote === 0 /* QuoteNone */) {
  13701. const styleVal = value.substring(valueStart, i - 1).trim();
  13702. styles.push(currentProp, valueHasQuotes ? stripUnnecessaryQuotes(styleVal) : styleVal);
  13703. propStart = i;
  13704. valueStart = 0;
  13705. currentProp = null;
  13706. valueHasQuotes = false;
  13707. }
  13708. break;
  13709. }
  13710. }
  13711. if (currentProp && valueStart) {
  13712. const styleVal = value.substr(valueStart).trim();
  13713. styles.push(currentProp, valueHasQuotes ? stripUnnecessaryQuotes(styleVal) : styleVal);
  13714. }
  13715. return styles;
  13716. }
  13717. function stripUnnecessaryQuotes(value) {
  13718. const qS = value.charCodeAt(0);
  13719. const qE = value.charCodeAt(value.length - 1);
  13720. if (qS == qE && (qS == 39 /* QuoteSingle */ || qS == 34 /* QuoteDouble */)) {
  13721. const tempValue = value.substring(1, value.length - 1);
  13722. // special case to avoid using a multi-quoted string that was just chomped
  13723. // (e.g. `font-family: "Verdana", "sans-serif"`)
  13724. if (tempValue.indexOf('\'') == -1 && tempValue.indexOf('"') == -1) {
  13725. value = tempValue;
  13726. }
  13727. }
  13728. return value;
  13729. }
  13730. function hyphenate(value) {
  13731. return value
  13732. .replace(/[a-z][A-Z]/g, v => {
  13733. return v.charAt(0) + '-' + v.charAt(1);
  13734. })
  13735. .toLowerCase();
  13736. }
  13737. const IMPORTANT_FLAG = '!important';
  13738. /**
  13739. * Minimum amount of binding slots required in the runtime for style/class bindings.
  13740. *
  13741. * Styling in Angular uses up two slots in the runtime LView/TData data structures to
  13742. * record binding data, property information and metadata.
  13743. *
  13744. * When a binding is registered it will place the following information in the `LView`:
  13745. *
  13746. * slot 1) binding value
  13747. * slot 2) cached value (all other values collected before it in string form)
  13748. *
  13749. * When a binding is registered it will place the following information in the `TData`:
  13750. *
  13751. * slot 1) prop name
  13752. * slot 2) binding index that points to the previous style/class binding (and some extra config
  13753. * values)
  13754. *
  13755. * Let's imagine we have a binding that looks like so:
  13756. *
  13757. * ```
  13758. * <div [style.width]="x" [style.height]="y">
  13759. * ```
  13760. *
  13761. * Our `LView` and `TData` data-structures look like so:
  13762. *
  13763. * ```typescript
  13764. * LView = [
  13765. * // ...
  13766. * x, // value of x
  13767. * "width: x",
  13768. *
  13769. * y, // value of y
  13770. * "width: x; height: y",
  13771. * // ...
  13772. * ];
  13773. *
  13774. * TData = [
  13775. * // ...
  13776. * "width", // binding slot 20
  13777. * 0,
  13778. *
  13779. * "height",
  13780. * 20,
  13781. * // ...
  13782. * ];
  13783. * ```
  13784. *
  13785. * */
  13786. const MIN_STYLING_BINDING_SLOTS_REQUIRED = 2;
  13787. /**
  13788. * Produces creation/update instructions for all styling bindings (class and style)
  13789. *
  13790. * It also produces the creation instruction to register all initial styling values
  13791. * (which are all the static class="..." and style="..." attribute values that exist
  13792. * on an element within a template).
  13793. *
  13794. * The builder class below handles producing instructions for the following cases:
  13795. *
  13796. * - Static style/class attributes (style="..." and class="...")
  13797. * - Dynamic style/class map bindings ([style]="map" and [class]="map|string")
  13798. * - Dynamic style/class property bindings ([style.prop]="exp" and [class.name]="exp")
  13799. *
  13800. * Due to the complex relationship of all of these cases, the instructions generated
  13801. * for these attributes/properties/bindings must be done so in the correct order. The
  13802. * order which these must be generated is as follows:
  13803. *
  13804. * if (createMode) {
  13805. * styling(...)
  13806. * }
  13807. * if (updateMode) {
  13808. * styleMap(...)
  13809. * classMap(...)
  13810. * styleProp(...)
  13811. * classProp(...)
  13812. * }
  13813. *
  13814. * The creation/update methods within the builder class produce these instructions.
  13815. */
  13816. class StylingBuilder {
  13817. constructor(_directiveExpr) {
  13818. this._directiveExpr = _directiveExpr;
  13819. /** Whether or not there are any static styling values present */
  13820. this._hasInitialValues = false;
  13821. /**
  13822. * Whether or not there are any styling bindings present
  13823. * (i.e. `[style]`, `[class]`, `[style.prop]` or `[class.name]`)
  13824. */
  13825. this.hasBindings = false;
  13826. this.hasBindingsWithPipes = false;
  13827. /** the input for [class] (if it exists) */
  13828. this._classMapInput = null;
  13829. /** the input for [style] (if it exists) */
  13830. this._styleMapInput = null;
  13831. /** an array of each [style.prop] input */
  13832. this._singleStyleInputs = null;
  13833. /** an array of each [class.name] input */
  13834. this._singleClassInputs = null;
  13835. this._lastStylingInput = null;
  13836. this._firstStylingInput = null;
  13837. // maps are used instead of hash maps because a Map will
  13838. // retain the ordering of the keys
  13839. /**
  13840. * Represents the location of each style binding in the template
  13841. * (e.g. `<div [style.width]="w" [style.height]="h">` implies
  13842. * that `width=0` and `height=1`)
  13843. */
  13844. this._stylesIndex = new Map();
  13845. /**
  13846. * Represents the location of each class binding in the template
  13847. * (e.g. `<div [class.big]="b" [class.hidden]="h">` implies
  13848. * that `big=0` and `hidden=1`)
  13849. */
  13850. this._classesIndex = new Map();
  13851. this._initialStyleValues = [];
  13852. this._initialClassValues = [];
  13853. }
  13854. /**
  13855. * Registers a given input to the styling builder to be later used when producing AOT code.
  13856. *
  13857. * The code below will only accept the input if it is somehow tied to styling (whether it be
  13858. * style/class bindings or static style/class attributes).
  13859. */
  13860. registerBoundInput(input) {
  13861. // [attr.style] or [attr.class] are skipped in the code below,
  13862. // they should not be treated as styling-based bindings since
  13863. // they are intended to be written directly to the attr and
  13864. // will therefore skip all style/class resolution that is present
  13865. // with style="", [style]="" and [style.prop]="", class="",
  13866. // [class.prop]="". [class]="" assignments
  13867. let binding = null;
  13868. let name = input.name;
  13869. switch (input.type) {
  13870. case 0 /* Property */:
  13871. binding = this.registerInputBasedOnName(name, input.value, input.sourceSpan);
  13872. break;
  13873. case 3 /* Style */:
  13874. binding = this.registerStyleInput(name, false, input.value, input.sourceSpan, input.unit);
  13875. break;
  13876. case 2 /* Class */:
  13877. binding = this.registerClassInput(name, false, input.value, input.sourceSpan);
  13878. break;
  13879. }
  13880. return binding ? true : false;
  13881. }
  13882. registerInputBasedOnName(name, expression, sourceSpan) {
  13883. let binding = null;
  13884. const prefix = name.substring(0, 6);
  13885. const isStyle = name === 'style' || prefix === 'style.' || prefix === 'style!';
  13886. const isClass = !isStyle && (name === 'class' || prefix === 'class.' || prefix === 'class!');
  13887. if (isStyle || isClass) {
  13888. const isMapBased = name.charAt(5) !== '.'; // style.prop or class.prop makes this a no
  13889. const property = name.substr(isMapBased ? 5 : 6); // the dot explains why there's a +1
  13890. if (isStyle) {
  13891. binding = this.registerStyleInput(property, isMapBased, expression, sourceSpan);
  13892. }
  13893. else {
  13894. binding = this.registerClassInput(property, isMapBased, expression, sourceSpan);
  13895. }
  13896. }
  13897. return binding;
  13898. }
  13899. registerStyleInput(name, isMapBased, value, sourceSpan, suffix) {
  13900. if (isEmptyExpression(value)) {
  13901. return null;
  13902. }
  13903. // CSS custom properties are case-sensitive so we shouldn't normalize them.
  13904. // See: https://www.w3.org/TR/css-variables-1/#defining-variables
  13905. if (!isCssCustomProperty(name)) {
  13906. name = hyphenate(name);
  13907. }
  13908. const { property, hasOverrideFlag, suffix: bindingSuffix } = parseProperty(name);
  13909. suffix = typeof suffix === 'string' && suffix.length !== 0 ? suffix : bindingSuffix;
  13910. const entry = { name: property, suffix: suffix, value, sourceSpan, hasOverrideFlag };
  13911. if (isMapBased) {
  13912. this._styleMapInput = entry;
  13913. }
  13914. else {
  13915. (this._singleStyleInputs = this._singleStyleInputs || []).push(entry);
  13916. registerIntoMap(this._stylesIndex, property);
  13917. }
  13918. this._lastStylingInput = entry;
  13919. this._firstStylingInput = this._firstStylingInput || entry;
  13920. this._checkForPipes(value);
  13921. this.hasBindings = true;
  13922. return entry;
  13923. }
  13924. registerClassInput(name, isMapBased, value, sourceSpan) {
  13925. if (isEmptyExpression(value)) {
  13926. return null;
  13927. }
  13928. const { property, hasOverrideFlag } = parseProperty(name);
  13929. const entry = { name: property, value, sourceSpan, hasOverrideFlag, suffix: null };
  13930. if (isMapBased) {
  13931. this._classMapInput = entry;
  13932. }
  13933. else {
  13934. (this._singleClassInputs = this._singleClassInputs || []).push(entry);
  13935. registerIntoMap(this._classesIndex, property);
  13936. }
  13937. this._lastStylingInput = entry;
  13938. this._firstStylingInput = this._firstStylingInput || entry;
  13939. this._checkForPipes(value);
  13940. this.hasBindings = true;
  13941. return entry;
  13942. }
  13943. _checkForPipes(value) {
  13944. if ((value instanceof ASTWithSource) && (value.ast instanceof BindingPipe)) {
  13945. this.hasBindingsWithPipes = true;
  13946. }
  13947. }
  13948. /**
  13949. * Registers the element's static style string value to the builder.
  13950. *
  13951. * @param value the style string (e.g. `width:100px; height:200px;`)
  13952. */
  13953. registerStyleAttr(value) {
  13954. this._initialStyleValues = parse(value);
  13955. this._hasInitialValues = true;
  13956. }
  13957. /**
  13958. * Registers the element's static class string value to the builder.
  13959. *
  13960. * @param value the className string (e.g. `disabled gold zoom`)
  13961. */
  13962. registerClassAttr(value) {
  13963. this._initialClassValues = value.trim().split(/\s+/g);
  13964. this._hasInitialValues = true;
  13965. }
  13966. /**
  13967. * Appends all styling-related expressions to the provided attrs array.
  13968. *
  13969. * @param attrs an existing array where each of the styling expressions
  13970. * will be inserted into.
  13971. */
  13972. populateInitialStylingAttrs(attrs) {
  13973. // [CLASS_MARKER, 'foo', 'bar', 'baz' ...]
  13974. if (this._initialClassValues.length) {
  13975. attrs.push(literal(1 /* Classes */));
  13976. for (let i = 0; i < this._initialClassValues.length; i++) {
  13977. attrs.push(literal(this._initialClassValues[i]));
  13978. }
  13979. }
  13980. // [STYLE_MARKER, 'width', '200px', 'height', '100px', ...]
  13981. if (this._initialStyleValues.length) {
  13982. attrs.push(literal(2 /* Styles */));
  13983. for (let i = 0; i < this._initialStyleValues.length; i += 2) {
  13984. attrs.push(literal(this._initialStyleValues[i]), literal(this._initialStyleValues[i + 1]));
  13985. }
  13986. }
  13987. }
  13988. /**
  13989. * Builds an instruction with all the expressions and parameters for `elementHostAttrs`.
  13990. *
  13991. * The instruction generation code below is used for producing the AOT statement code which is
  13992. * responsible for registering initial styles (within a directive hostBindings' creation block),
  13993. * as well as any of the provided attribute values, to the directive host element.
  13994. */
  13995. assignHostAttrs(attrs, definitionMap) {
  13996. if (this._directiveExpr && (attrs.length || this._hasInitialValues)) {
  13997. this.populateInitialStylingAttrs(attrs);
  13998. definitionMap.set('hostAttrs', literalArr(attrs));
  13999. }
  14000. }
  14001. /**
  14002. * Builds an instruction with all the expressions and parameters for `classMap`.
  14003. *
  14004. * The instruction data will contain all expressions for `classMap` to function
  14005. * which includes the `[class]` expression params.
  14006. */
  14007. buildClassMapInstruction(valueConverter) {
  14008. if (this._classMapInput) {
  14009. return this._buildMapBasedInstruction(valueConverter, true, this._classMapInput);
  14010. }
  14011. return null;
  14012. }
  14013. /**
  14014. * Builds an instruction with all the expressions and parameters for `styleMap`.
  14015. *
  14016. * The instruction data will contain all expressions for `styleMap` to function
  14017. * which includes the `[style]` expression params.
  14018. */
  14019. buildStyleMapInstruction(valueConverter) {
  14020. if (this._styleMapInput) {
  14021. return this._buildMapBasedInstruction(valueConverter, false, this._styleMapInput);
  14022. }
  14023. return null;
  14024. }
  14025. _buildMapBasedInstruction(valueConverter, isClassBased, stylingInput) {
  14026. // each styling binding value is stored in the LView
  14027. // map-based bindings allocate two slots: one for the
  14028. // previous binding value and another for the previous
  14029. // className or style attribute value.
  14030. let totalBindingSlotsRequired = MIN_STYLING_BINDING_SLOTS_REQUIRED;
  14031. // these values must be outside of the update block so that they can
  14032. // be evaluated (the AST visit call) during creation time so that any
  14033. // pipes can be picked up in time before the template is built
  14034. const mapValue = stylingInput.value.visit(valueConverter);
  14035. let reference;
  14036. if (mapValue instanceof Interpolation) {
  14037. totalBindingSlotsRequired += mapValue.expressions.length;
  14038. reference = isClassBased ? getClassMapInterpolationExpression(mapValue) :
  14039. getStyleMapInterpolationExpression(mapValue);
  14040. }
  14041. else {
  14042. reference = isClassBased ? Identifiers.classMap : Identifiers.styleMap;
  14043. }
  14044. return {
  14045. reference,
  14046. calls: [{
  14047. supportsInterpolation: true,
  14048. sourceSpan: stylingInput.sourceSpan,
  14049. allocateBindingSlots: totalBindingSlotsRequired,
  14050. params: (convertFn) => {
  14051. const convertResult = convertFn(mapValue);
  14052. const params = Array.isArray(convertResult) ? convertResult : [convertResult];
  14053. return params;
  14054. }
  14055. }]
  14056. };
  14057. }
  14058. _buildSingleInputs(reference, inputs, valueConverter, getInterpolationExpressionFn, isClassBased) {
  14059. const instructions = [];
  14060. inputs.forEach(input => {
  14061. const previousInstruction = instructions[instructions.length - 1];
  14062. const value = input.value.visit(valueConverter);
  14063. let referenceForCall = reference;
  14064. // each styling binding value is stored in the LView
  14065. // but there are two values stored for each binding:
  14066. // 1) the value itself
  14067. // 2) an intermediate value (concatenation of style up to this point).
  14068. // We need to store the intermediate value so that we don't allocate
  14069. // the strings on each CD.
  14070. let totalBindingSlotsRequired = MIN_STYLING_BINDING_SLOTS_REQUIRED;
  14071. if (value instanceof Interpolation) {
  14072. totalBindingSlotsRequired += value.expressions.length;
  14073. if (getInterpolationExpressionFn) {
  14074. referenceForCall = getInterpolationExpressionFn(value);
  14075. }
  14076. }
  14077. const call = {
  14078. sourceSpan: input.sourceSpan,
  14079. allocateBindingSlots: totalBindingSlotsRequired,
  14080. supportsInterpolation: !!getInterpolationExpressionFn,
  14081. params: (convertFn) => {
  14082. // params => stylingProp(propName, value, suffix)
  14083. const params = [];
  14084. params.push(literal(input.name));
  14085. const convertResult = convertFn(value);
  14086. if (Array.isArray(convertResult)) {
  14087. params.push(...convertResult);
  14088. }
  14089. else {
  14090. params.push(convertResult);
  14091. }
  14092. // [style.prop] bindings may use suffix values (e.g. px, em, etc...), therefore,
  14093. // if that is detected then we need to pass that in as an optional param.
  14094. if (!isClassBased && input.suffix !== null) {
  14095. params.push(literal(input.suffix));
  14096. }
  14097. return params;
  14098. }
  14099. };
  14100. // If we ended up generating a call to the same instruction as the previous styling property
  14101. // we can chain the calls together safely to save some bytes, otherwise we have to generate
  14102. // a separate instruction call. This is primarily a concern with interpolation instructions
  14103. // where we may start off with one `reference`, but end up using another based on the
  14104. // number of interpolations.
  14105. if (previousInstruction && previousInstruction.reference === referenceForCall) {
  14106. previousInstruction.calls.push(call);
  14107. }
  14108. else {
  14109. instructions.push({ reference: referenceForCall, calls: [call] });
  14110. }
  14111. });
  14112. return instructions;
  14113. }
  14114. _buildClassInputs(valueConverter) {
  14115. if (this._singleClassInputs) {
  14116. return this._buildSingleInputs(Identifiers.classProp, this._singleClassInputs, valueConverter, null, true);
  14117. }
  14118. return [];
  14119. }
  14120. _buildStyleInputs(valueConverter) {
  14121. if (this._singleStyleInputs) {
  14122. return this._buildSingleInputs(Identifiers.styleProp, this._singleStyleInputs, valueConverter, getStylePropInterpolationExpression, false);
  14123. }
  14124. return [];
  14125. }
  14126. /**
  14127. * Constructs all instructions which contain the expressions that will be placed
  14128. * into the update block of a template function or a directive hostBindings function.
  14129. */
  14130. buildUpdateLevelInstructions(valueConverter) {
  14131. const instructions = [];
  14132. if (this.hasBindings) {
  14133. const styleMapInstruction = this.buildStyleMapInstruction(valueConverter);
  14134. if (styleMapInstruction) {
  14135. instructions.push(styleMapInstruction);
  14136. }
  14137. const classMapInstruction = this.buildClassMapInstruction(valueConverter);
  14138. if (classMapInstruction) {
  14139. instructions.push(classMapInstruction);
  14140. }
  14141. instructions.push(...this._buildStyleInputs(valueConverter));
  14142. instructions.push(...this._buildClassInputs(valueConverter));
  14143. }
  14144. return instructions;
  14145. }
  14146. }
  14147. function registerIntoMap(map, key) {
  14148. if (!map.has(key)) {
  14149. map.set(key, map.size);
  14150. }
  14151. }
  14152. function parseProperty(name) {
  14153. let hasOverrideFlag = false;
  14154. const overrideIndex = name.indexOf(IMPORTANT_FLAG);
  14155. if (overrideIndex !== -1) {
  14156. name = overrideIndex > 0 ? name.substring(0, overrideIndex) : '';
  14157. hasOverrideFlag = true;
  14158. }
  14159. let suffix = null;
  14160. let property = name;
  14161. const unitIndex = name.lastIndexOf('.');
  14162. if (unitIndex > 0) {
  14163. suffix = name.substr(unitIndex + 1);
  14164. property = name.substring(0, unitIndex);
  14165. }
  14166. return { property, suffix, hasOverrideFlag };
  14167. }
  14168. /**
  14169. * Gets the instruction to generate for an interpolated class map.
  14170. * @param interpolation An Interpolation AST
  14171. */
  14172. function getClassMapInterpolationExpression(interpolation) {
  14173. switch (getInterpolationArgsLength(interpolation)) {
  14174. case 1:
  14175. return Identifiers.classMap;
  14176. case 3:
  14177. return Identifiers.classMapInterpolate1;
  14178. case 5:
  14179. return Identifiers.classMapInterpolate2;
  14180. case 7:
  14181. return Identifiers.classMapInterpolate3;
  14182. case 9:
  14183. return Identifiers.classMapInterpolate4;
  14184. case 11:
  14185. return Identifiers.classMapInterpolate5;
  14186. case 13:
  14187. return Identifiers.classMapInterpolate6;
  14188. case 15:
  14189. return Identifiers.classMapInterpolate7;
  14190. case 17:
  14191. return Identifiers.classMapInterpolate8;
  14192. default:
  14193. return Identifiers.classMapInterpolateV;
  14194. }
  14195. }
  14196. /**
  14197. * Gets the instruction to generate for an interpolated style map.
  14198. * @param interpolation An Interpolation AST
  14199. */
  14200. function getStyleMapInterpolationExpression(interpolation) {
  14201. switch (getInterpolationArgsLength(interpolation)) {
  14202. case 1:
  14203. return Identifiers.styleMap;
  14204. case 3:
  14205. return Identifiers.styleMapInterpolate1;
  14206. case 5:
  14207. return Identifiers.styleMapInterpolate2;
  14208. case 7:
  14209. return Identifiers.styleMapInterpolate3;
  14210. case 9:
  14211. return Identifiers.styleMapInterpolate4;
  14212. case 11:
  14213. return Identifiers.styleMapInterpolate5;
  14214. case 13:
  14215. return Identifiers.styleMapInterpolate6;
  14216. case 15:
  14217. return Identifiers.styleMapInterpolate7;
  14218. case 17:
  14219. return Identifiers.styleMapInterpolate8;
  14220. default:
  14221. return Identifiers.styleMapInterpolateV;
  14222. }
  14223. }
  14224. /**
  14225. * Gets the instruction to generate for an interpolated style prop.
  14226. * @param interpolation An Interpolation AST
  14227. */
  14228. function getStylePropInterpolationExpression(interpolation) {
  14229. switch (getInterpolationArgsLength(interpolation)) {
  14230. case 1:
  14231. return Identifiers.styleProp;
  14232. case 3:
  14233. return Identifiers.stylePropInterpolate1;
  14234. case 5:
  14235. return Identifiers.stylePropInterpolate2;
  14236. case 7:
  14237. return Identifiers.stylePropInterpolate3;
  14238. case 9:
  14239. return Identifiers.stylePropInterpolate4;
  14240. case 11:
  14241. return Identifiers.stylePropInterpolate5;
  14242. case 13:
  14243. return Identifiers.stylePropInterpolate6;
  14244. case 15:
  14245. return Identifiers.stylePropInterpolate7;
  14246. case 17:
  14247. return Identifiers.stylePropInterpolate8;
  14248. default:
  14249. return Identifiers.stylePropInterpolateV;
  14250. }
  14251. }
  14252. /**
  14253. * Checks whether property name is a custom CSS property.
  14254. * See: https://www.w3.org/TR/css-variables-1
  14255. */
  14256. function isCssCustomProperty(name) {
  14257. return name.startsWith('--');
  14258. }
  14259. /**
  14260. * @license
  14261. * Copyright Google LLC All Rights Reserved.
  14262. *
  14263. * Use of this source code is governed by an MIT-style license that can be
  14264. * found in the LICENSE file at https://angular.io/license
  14265. */
  14266. var TokenType$1;
  14267. (function (TokenType) {
  14268. TokenType[TokenType["Character"] = 0] = "Character";
  14269. TokenType[TokenType["Identifier"] = 1] = "Identifier";
  14270. TokenType[TokenType["PrivateIdentifier"] = 2] = "PrivateIdentifier";
  14271. TokenType[TokenType["Keyword"] = 3] = "Keyword";
  14272. TokenType[TokenType["String"] = 4] = "String";
  14273. TokenType[TokenType["Operator"] = 5] = "Operator";
  14274. TokenType[TokenType["Number"] = 6] = "Number";
  14275. TokenType[TokenType["Error"] = 7] = "Error";
  14276. })(TokenType$1 || (TokenType$1 = {}));
  14277. const KEYWORDS = ['var', 'let', 'as', 'null', 'undefined', 'true', 'false', 'if', 'else', 'this'];
  14278. class Lexer {
  14279. tokenize(text) {
  14280. const scanner = new _Scanner(text);
  14281. const tokens = [];
  14282. let token = scanner.scanToken();
  14283. while (token != null) {
  14284. tokens.push(token);
  14285. token = scanner.scanToken();
  14286. }
  14287. return tokens;
  14288. }
  14289. }
  14290. class Token$1 {
  14291. constructor(index, end, type, numValue, strValue) {
  14292. this.index = index;
  14293. this.end = end;
  14294. this.type = type;
  14295. this.numValue = numValue;
  14296. this.strValue = strValue;
  14297. }
  14298. isCharacter(code) {
  14299. return this.type == TokenType$1.Character && this.numValue == code;
  14300. }
  14301. isNumber() {
  14302. return this.type == TokenType$1.Number;
  14303. }
  14304. isString() {
  14305. return this.type == TokenType$1.String;
  14306. }
  14307. isOperator(operator) {
  14308. return this.type == TokenType$1.Operator && this.strValue == operator;
  14309. }
  14310. isIdentifier() {
  14311. return this.type == TokenType$1.Identifier;
  14312. }
  14313. isPrivateIdentifier() {
  14314. return this.type == TokenType$1.PrivateIdentifier;
  14315. }
  14316. isKeyword() {
  14317. return this.type == TokenType$1.Keyword;
  14318. }
  14319. isKeywordLet() {
  14320. return this.type == TokenType$1.Keyword && this.strValue == 'let';
  14321. }
  14322. isKeywordAs() {
  14323. return this.type == TokenType$1.Keyword && this.strValue == 'as';
  14324. }
  14325. isKeywordNull() {
  14326. return this.type == TokenType$1.Keyword && this.strValue == 'null';
  14327. }
  14328. isKeywordUndefined() {
  14329. return this.type == TokenType$1.Keyword && this.strValue == 'undefined';
  14330. }
  14331. isKeywordTrue() {
  14332. return this.type == TokenType$1.Keyword && this.strValue == 'true';
  14333. }
  14334. isKeywordFalse() {
  14335. return this.type == TokenType$1.Keyword && this.strValue == 'false';
  14336. }
  14337. isKeywordThis() {
  14338. return this.type == TokenType$1.Keyword && this.strValue == 'this';
  14339. }
  14340. isError() {
  14341. return this.type == TokenType$1.Error;
  14342. }
  14343. toNumber() {
  14344. return this.type == TokenType$1.Number ? this.numValue : -1;
  14345. }
  14346. toString() {
  14347. switch (this.type) {
  14348. case TokenType$1.Character:
  14349. case TokenType$1.Identifier:
  14350. case TokenType$1.Keyword:
  14351. case TokenType$1.Operator:
  14352. case TokenType$1.PrivateIdentifier:
  14353. case TokenType$1.String:
  14354. case TokenType$1.Error:
  14355. return this.strValue;
  14356. case TokenType$1.Number:
  14357. return this.numValue.toString();
  14358. default:
  14359. return null;
  14360. }
  14361. }
  14362. }
  14363. function newCharacterToken(index, end, code) {
  14364. return new Token$1(index, end, TokenType$1.Character, code, String.fromCharCode(code));
  14365. }
  14366. function newIdentifierToken(index, end, text) {
  14367. return new Token$1(index, end, TokenType$1.Identifier, 0, text);
  14368. }
  14369. function newPrivateIdentifierToken(index, end, text) {
  14370. return new Token$1(index, end, TokenType$1.PrivateIdentifier, 0, text);
  14371. }
  14372. function newKeywordToken(index, end, text) {
  14373. return new Token$1(index, end, TokenType$1.Keyword, 0, text);
  14374. }
  14375. function newOperatorToken(index, end, text) {
  14376. return new Token$1(index, end, TokenType$1.Operator, 0, text);
  14377. }
  14378. function newStringToken(index, end, text) {
  14379. return new Token$1(index, end, TokenType$1.String, 0, text);
  14380. }
  14381. function newNumberToken(index, end, n) {
  14382. return new Token$1(index, end, TokenType$1.Number, n, '');
  14383. }
  14384. function newErrorToken(index, end, message) {
  14385. return new Token$1(index, end, TokenType$1.Error, 0, message);
  14386. }
  14387. const EOF = new Token$1(-1, -1, TokenType$1.Character, 0, '');
  14388. class _Scanner {
  14389. constructor(input) {
  14390. this.input = input;
  14391. this.peek = 0;
  14392. this.index = -1;
  14393. this.length = input.length;
  14394. this.advance();
  14395. }
  14396. advance() {
  14397. this.peek = ++this.index >= this.length ? $EOF : this.input.charCodeAt(this.index);
  14398. }
  14399. scanToken() {
  14400. const input = this.input, length = this.length;
  14401. let peek = this.peek, index = this.index;
  14402. // Skip whitespace.
  14403. while (peek <= $SPACE) {
  14404. if (++index >= length) {
  14405. peek = $EOF;
  14406. break;
  14407. }
  14408. else {
  14409. peek = input.charCodeAt(index);
  14410. }
  14411. }
  14412. this.peek = peek;
  14413. this.index = index;
  14414. if (index >= length) {
  14415. return null;
  14416. }
  14417. // Handle identifiers and numbers.
  14418. if (isIdentifierStart(peek))
  14419. return this.scanIdentifier();
  14420. if (isDigit(peek))
  14421. return this.scanNumber(index);
  14422. const start = index;
  14423. switch (peek) {
  14424. case $PERIOD:
  14425. this.advance();
  14426. return isDigit(this.peek) ? this.scanNumber(start) :
  14427. newCharacterToken(start, this.index, $PERIOD);
  14428. case $LPAREN:
  14429. case $RPAREN:
  14430. case $LBRACE:
  14431. case $RBRACE:
  14432. case $LBRACKET:
  14433. case $RBRACKET:
  14434. case $COMMA:
  14435. case $COLON:
  14436. case $SEMICOLON:
  14437. return this.scanCharacter(start, peek);
  14438. case $SQ:
  14439. case $DQ:
  14440. return this.scanString();
  14441. case $HASH:
  14442. return this.scanPrivateIdentifier();
  14443. case $PLUS:
  14444. case $MINUS:
  14445. case $STAR:
  14446. case $SLASH:
  14447. case $PERCENT:
  14448. case $CARET:
  14449. return this.scanOperator(start, String.fromCharCode(peek));
  14450. case $QUESTION:
  14451. return this.scanQuestion(start);
  14452. case $LT:
  14453. case $GT:
  14454. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=');
  14455. case $BANG:
  14456. case $EQ:
  14457. return this.scanComplexOperator(start, String.fromCharCode(peek), $EQ, '=', $EQ, '=');
  14458. case $AMPERSAND:
  14459. return this.scanComplexOperator(start, '&', $AMPERSAND, '&');
  14460. case $BAR:
  14461. return this.scanComplexOperator(start, '|', $BAR, '|');
  14462. case $NBSP:
  14463. while (isWhitespace(this.peek))
  14464. this.advance();
  14465. return this.scanToken();
  14466. }
  14467. this.advance();
  14468. return this.error(`Unexpected character [${String.fromCharCode(peek)}]`, 0);
  14469. }
  14470. scanCharacter(start, code) {
  14471. this.advance();
  14472. return newCharacterToken(start, this.index, code);
  14473. }
  14474. scanOperator(start, str) {
  14475. this.advance();
  14476. return newOperatorToken(start, this.index, str);
  14477. }
  14478. /**
  14479. * Tokenize a 2/3 char long operator
  14480. *
  14481. * @param start start index in the expression
  14482. * @param one first symbol (always part of the operator)
  14483. * @param twoCode code point for the second symbol
  14484. * @param two second symbol (part of the operator when the second code point matches)
  14485. * @param threeCode code point for the third symbol
  14486. * @param three third symbol (part of the operator when provided and matches source expression)
  14487. */
  14488. scanComplexOperator(start, one, twoCode, two, threeCode, three) {
  14489. this.advance();
  14490. let str = one;
  14491. if (this.peek == twoCode) {
  14492. this.advance();
  14493. str += two;
  14494. }
  14495. if (threeCode != null && this.peek == threeCode) {
  14496. this.advance();
  14497. str += three;
  14498. }
  14499. return newOperatorToken(start, this.index, str);
  14500. }
  14501. scanIdentifier() {
  14502. const start = this.index;
  14503. this.advance();
  14504. while (isIdentifierPart(this.peek))
  14505. this.advance();
  14506. const str = this.input.substring(start, this.index);
  14507. return KEYWORDS.indexOf(str) > -1 ? newKeywordToken(start, this.index, str) :
  14508. newIdentifierToken(start, this.index, str);
  14509. }
  14510. /** Scans an ECMAScript private identifier. */
  14511. scanPrivateIdentifier() {
  14512. const start = this.index;
  14513. this.advance();
  14514. if (!isIdentifierStart(this.peek)) {
  14515. return this.error('Invalid character [#]', -1);
  14516. }
  14517. while (isIdentifierPart(this.peek))
  14518. this.advance();
  14519. const identifierName = this.input.substring(start, this.index);
  14520. return newPrivateIdentifierToken(start, this.index, identifierName);
  14521. }
  14522. scanNumber(start) {
  14523. let simple = (this.index === start);
  14524. this.advance(); // Skip initial digit.
  14525. while (true) {
  14526. if (isDigit(this.peek)) {
  14527. // Do nothing.
  14528. }
  14529. else if (this.peek == $PERIOD) {
  14530. simple = false;
  14531. }
  14532. else if (isExponentStart(this.peek)) {
  14533. this.advance();
  14534. if (isExponentSign(this.peek))
  14535. this.advance();
  14536. if (!isDigit(this.peek))
  14537. return this.error('Invalid exponent', -1);
  14538. simple = false;
  14539. }
  14540. else {
  14541. break;
  14542. }
  14543. this.advance();
  14544. }
  14545. const str = this.input.substring(start, this.index);
  14546. const value = simple ? parseIntAutoRadix(str) : parseFloat(str);
  14547. return newNumberToken(start, this.index, value);
  14548. }
  14549. scanString() {
  14550. const start = this.index;
  14551. const quote = this.peek;
  14552. this.advance(); // Skip initial quote.
  14553. let buffer = '';
  14554. let marker = this.index;
  14555. const input = this.input;
  14556. while (this.peek != quote) {
  14557. if (this.peek == $BACKSLASH) {
  14558. buffer += input.substring(marker, this.index);
  14559. this.advance();
  14560. let unescapedCode;
  14561. // Workaround for TS2.1-introduced type strictness
  14562. this.peek = this.peek;
  14563. if (this.peek == $u) {
  14564. // 4 character hex code for unicode character.
  14565. const hex = input.substring(this.index + 1, this.index + 5);
  14566. if (/^[0-9a-f]+$/i.test(hex)) {
  14567. unescapedCode = parseInt(hex, 16);
  14568. }
  14569. else {
  14570. return this.error(`Invalid unicode escape [\\u${hex}]`, 0);
  14571. }
  14572. for (let i = 0; i < 5; i++) {
  14573. this.advance();
  14574. }
  14575. }
  14576. else {
  14577. unescapedCode = unescape(this.peek);
  14578. this.advance();
  14579. }
  14580. buffer += String.fromCharCode(unescapedCode);
  14581. marker = this.index;
  14582. }
  14583. else if (this.peek == $EOF) {
  14584. return this.error('Unterminated quote', 0);
  14585. }
  14586. else {
  14587. this.advance();
  14588. }
  14589. }
  14590. const last = input.substring(marker, this.index);
  14591. this.advance(); // Skip terminating quote.
  14592. return newStringToken(start, this.index, buffer + last);
  14593. }
  14594. scanQuestion(start) {
  14595. this.advance();
  14596. let str = '?';
  14597. // Either `a ?? b` or 'a?.b'.
  14598. if (this.peek === $QUESTION || this.peek === $PERIOD) {
  14599. str += this.peek === $PERIOD ? '.' : '?';
  14600. this.advance();
  14601. }
  14602. return newOperatorToken(start, this.index, str);
  14603. }
  14604. error(message, offset) {
  14605. const position = this.index + offset;
  14606. return newErrorToken(position, this.index, `Lexer Error: ${message} at column ${position} in expression [${this.input}]`);
  14607. }
  14608. }
  14609. function isIdentifierStart(code) {
  14610. return ($a <= code && code <= $z) || ($A <= code && code <= $Z) ||
  14611. (code == $_) || (code == $$);
  14612. }
  14613. function isIdentifier(input) {
  14614. if (input.length == 0)
  14615. return false;
  14616. const scanner = new _Scanner(input);
  14617. if (!isIdentifierStart(scanner.peek))
  14618. return false;
  14619. scanner.advance();
  14620. while (scanner.peek !== $EOF) {
  14621. if (!isIdentifierPart(scanner.peek))
  14622. return false;
  14623. scanner.advance();
  14624. }
  14625. return true;
  14626. }
  14627. function isIdentifierPart(code) {
  14628. return isAsciiLetter(code) || isDigit(code) || (code == $_) ||
  14629. (code == $$);
  14630. }
  14631. function isExponentStart(code) {
  14632. return code == $e || code == $E;
  14633. }
  14634. function isExponentSign(code) {
  14635. return code == $MINUS || code == $PLUS;
  14636. }
  14637. function isQuote(code) {
  14638. return code === $SQ || code === $DQ || code === $BT;
  14639. }
  14640. function unescape(code) {
  14641. switch (code) {
  14642. case $n:
  14643. return $LF;
  14644. case $f:
  14645. return $FF;
  14646. case $r:
  14647. return $CR;
  14648. case $t:
  14649. return $TAB;
  14650. case $v:
  14651. return $VTAB;
  14652. default:
  14653. return code;
  14654. }
  14655. }
  14656. function parseIntAutoRadix(text) {
  14657. const result = parseInt(text);
  14658. if (isNaN(result)) {
  14659. throw new Error('Invalid integer literal when parsing ' + text);
  14660. }
  14661. return result;
  14662. }
  14663. /**
  14664. * @license
  14665. * Copyright Google LLC All Rights Reserved.
  14666. *
  14667. * Use of this source code is governed by an MIT-style license that can be
  14668. * found in the LICENSE file at https://angular.io/license
  14669. */
  14670. class SplitInterpolation {
  14671. constructor(strings, expressions, offsets) {
  14672. this.strings = strings;
  14673. this.expressions = expressions;
  14674. this.offsets = offsets;
  14675. }
  14676. }
  14677. class TemplateBindingParseResult {
  14678. constructor(templateBindings, warnings, errors) {
  14679. this.templateBindings = templateBindings;
  14680. this.warnings = warnings;
  14681. this.errors = errors;
  14682. }
  14683. }
  14684. class Parser$1 {
  14685. constructor(_lexer) {
  14686. this._lexer = _lexer;
  14687. this.errors = [];
  14688. this.simpleExpressionChecker = SimpleExpressionChecker;
  14689. }
  14690. parseAction(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  14691. this._checkNoInterpolation(input, location, interpolationConfig);
  14692. const sourceToLex = this._stripComments(input);
  14693. const tokens = this._lexer.tokenize(this._stripComments(input));
  14694. const ast = new _ParseAST(input, location, absoluteOffset, tokens, sourceToLex.length, true, this.errors, input.length - sourceToLex.length)
  14695. .parseChain();
  14696. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  14697. }
  14698. parseBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  14699. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  14700. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  14701. }
  14702. checkSimpleExpression(ast) {
  14703. const checker = new this.simpleExpressionChecker();
  14704. ast.visit(checker);
  14705. return checker.errors;
  14706. }
  14707. parseSimpleBinding(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  14708. const ast = this._parseBindingAst(input, location, absoluteOffset, interpolationConfig);
  14709. const errors = this.checkSimpleExpression(ast);
  14710. if (errors.length > 0) {
  14711. this._reportError(`Host binding expression cannot contain ${errors.join(' ')}`, input, location);
  14712. }
  14713. return new ASTWithSource(ast, input, location, absoluteOffset, this.errors);
  14714. }
  14715. _reportError(message, input, errLocation, ctxLocation) {
  14716. this.errors.push(new ParserError(message, input, errLocation, ctxLocation));
  14717. }
  14718. _parseBindingAst(input, location, absoluteOffset, interpolationConfig) {
  14719. // Quotes expressions use 3rd-party expression language. We don't want to use
  14720. // our lexer or parser for that, so we check for that ahead of time.
  14721. const quote = this._parseQuote(input, location, absoluteOffset);
  14722. if (quote != null) {
  14723. return quote;
  14724. }
  14725. this._checkNoInterpolation(input, location, interpolationConfig);
  14726. const sourceToLex = this._stripComments(input);
  14727. const tokens = this._lexer.tokenize(sourceToLex);
  14728. return new _ParseAST(input, location, absoluteOffset, tokens, sourceToLex.length, false, this.errors, input.length - sourceToLex.length)
  14729. .parseChain();
  14730. }
  14731. _parseQuote(input, location, absoluteOffset) {
  14732. if (input == null)
  14733. return null;
  14734. const prefixSeparatorIndex = input.indexOf(':');
  14735. if (prefixSeparatorIndex == -1)
  14736. return null;
  14737. const prefix = input.substring(0, prefixSeparatorIndex).trim();
  14738. if (!isIdentifier(prefix))
  14739. return null;
  14740. const uninterpretedExpression = input.substring(prefixSeparatorIndex + 1);
  14741. const span = new ParseSpan(0, input.length);
  14742. return new Quote(span, span.toAbsolute(absoluteOffset), prefix, uninterpretedExpression, location);
  14743. }
  14744. /**
  14745. * Parse microsyntax template expression and return a list of bindings or
  14746. * parsing errors in case the given expression is invalid.
  14747. *
  14748. * For example,
  14749. * ```
  14750. * <div *ngFor="let item of items">
  14751. * ^ ^ absoluteValueOffset for `templateValue`
  14752. * absoluteKeyOffset for `templateKey`
  14753. * ```
  14754. * contains three bindings:
  14755. * 1. ngFor -> null
  14756. * 2. item -> NgForOfContext.$implicit
  14757. * 3. ngForOf -> items
  14758. *
  14759. * This is apparent from the de-sugared template:
  14760. * ```
  14761. * <ng-template ngFor let-item [ngForOf]="items">
  14762. * ```
  14763. *
  14764. * @param templateKey name of directive, without the * prefix. For example: ngIf, ngFor
  14765. * @param templateValue RHS of the microsyntax attribute
  14766. * @param templateUrl template filename if it's external, component filename if it's inline
  14767. * @param absoluteKeyOffset start of the `templateKey`
  14768. * @param absoluteValueOffset start of the `templateValue`
  14769. */
  14770. parseTemplateBindings(templateKey, templateValue, templateUrl, absoluteKeyOffset, absoluteValueOffset) {
  14771. const tokens = this._lexer.tokenize(templateValue);
  14772. const parser = new _ParseAST(templateValue, templateUrl, absoluteValueOffset, tokens, templateValue.length, false /* parseAction */, this.errors, 0 /* relative offset */);
  14773. return parser.parseTemplateBindings({
  14774. source: templateKey,
  14775. span: new AbsoluteSourceSpan(absoluteKeyOffset, absoluteKeyOffset + templateKey.length),
  14776. });
  14777. }
  14778. parseInterpolation(input, location, absoluteOffset, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  14779. const { strings, expressions, offsets } = this.splitInterpolation(input, location, interpolationConfig);
  14780. if (expressions.length === 0)
  14781. return null;
  14782. const expressionNodes = [];
  14783. for (let i = 0; i < expressions.length; ++i) {
  14784. const expressionText = expressions[i].text;
  14785. const sourceToLex = this._stripComments(expressionText);
  14786. const tokens = this._lexer.tokenize(sourceToLex);
  14787. const ast = new _ParseAST(input, location, absoluteOffset, tokens, sourceToLex.length, false, this.errors, offsets[i] + (expressionText.length - sourceToLex.length))
  14788. .parseChain();
  14789. expressionNodes.push(ast);
  14790. }
  14791. return this.createInterpolationAst(strings.map(s => s.text), expressionNodes, input, location, absoluteOffset);
  14792. }
  14793. /**
  14794. * Similar to `parseInterpolation`, but treats the provided string as a single expression
  14795. * element that would normally appear within the interpolation prefix and suffix (`{{` and `}}`).
  14796. * This is used for parsing the switch expression in ICUs.
  14797. */
  14798. parseInterpolationExpression(expression, location, absoluteOffset) {
  14799. const sourceToLex = this._stripComments(expression);
  14800. const tokens = this._lexer.tokenize(sourceToLex);
  14801. const ast = new _ParseAST(expression, location, absoluteOffset, tokens, sourceToLex.length,
  14802. /* parseAction */ false, this.errors, 0)
  14803. .parseChain();
  14804. const strings = ['', '']; // The prefix and suffix strings are both empty
  14805. return this.createInterpolationAst(strings, [ast], expression, location, absoluteOffset);
  14806. }
  14807. createInterpolationAst(strings, expressions, input, location, absoluteOffset) {
  14808. const span = new ParseSpan(0, input.length);
  14809. const interpolation = new Interpolation(span, span.toAbsolute(absoluteOffset), strings, expressions);
  14810. return new ASTWithSource(interpolation, input, location, absoluteOffset, this.errors);
  14811. }
  14812. /**
  14813. * Splits a string of text into "raw" text segments and expressions present in interpolations in
  14814. * the string.
  14815. * Returns `null` if there are no interpolations, otherwise a
  14816. * `SplitInterpolation` with splits that look like
  14817. * <raw text> <expression> <raw text> ... <raw text> <expression> <raw text>
  14818. */
  14819. splitInterpolation(input, location, interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  14820. const strings = [];
  14821. const expressions = [];
  14822. const offsets = [];
  14823. let i = 0;
  14824. let atInterpolation = false;
  14825. let extendLastString = false;
  14826. let { start: interpStart, end: interpEnd } = interpolationConfig;
  14827. while (i < input.length) {
  14828. if (!atInterpolation) {
  14829. // parse until starting {{
  14830. const start = i;
  14831. i = input.indexOf(interpStart, i);
  14832. if (i === -1) {
  14833. i = input.length;
  14834. }
  14835. const text = input.substring(start, i);
  14836. strings.push({ text, start, end: i });
  14837. atInterpolation = true;
  14838. }
  14839. else {
  14840. // parse from starting {{ to ending }} while ignoring content inside quotes.
  14841. const fullStart = i;
  14842. const exprStart = fullStart + interpStart.length;
  14843. const exprEnd = this._getInterpolationEndIndex(input, interpEnd, exprStart);
  14844. if (exprEnd === -1) {
  14845. // Could not find the end of the interpolation; do not parse an expression.
  14846. // Instead we should extend the content on the last raw string.
  14847. atInterpolation = false;
  14848. extendLastString = true;
  14849. break;
  14850. }
  14851. const fullEnd = exprEnd + interpEnd.length;
  14852. const text = input.substring(exprStart, exprEnd);
  14853. if (text.trim().length === 0) {
  14854. this._reportError('Blank expressions are not allowed in interpolated strings', input, `at column ${i} in`, location);
  14855. }
  14856. expressions.push({ text, start: fullStart, end: fullEnd });
  14857. offsets.push(exprStart);
  14858. i = fullEnd;
  14859. atInterpolation = false;
  14860. }
  14861. }
  14862. if (!atInterpolation) {
  14863. // If we are now at a text section, add the remaining content as a raw string.
  14864. if (extendLastString) {
  14865. const piece = strings[strings.length - 1];
  14866. piece.text += input.substring(i);
  14867. piece.end = input.length;
  14868. }
  14869. else {
  14870. strings.push({ text: input.substring(i), start: i, end: input.length });
  14871. }
  14872. }
  14873. return new SplitInterpolation(strings, expressions, offsets);
  14874. }
  14875. wrapLiteralPrimitive(input, location, absoluteOffset) {
  14876. const span = new ParseSpan(0, input == null ? 0 : input.length);
  14877. return new ASTWithSource(new LiteralPrimitive(span, span.toAbsolute(absoluteOffset), input), input, location, absoluteOffset, this.errors);
  14878. }
  14879. _stripComments(input) {
  14880. const i = this._commentStart(input);
  14881. return i != null ? input.substring(0, i).trim() : input;
  14882. }
  14883. _commentStart(input) {
  14884. let outerQuote = null;
  14885. for (let i = 0; i < input.length - 1; i++) {
  14886. const char = input.charCodeAt(i);
  14887. const nextChar = input.charCodeAt(i + 1);
  14888. if (char === $SLASH && nextChar == $SLASH && outerQuote == null)
  14889. return i;
  14890. if (outerQuote === char) {
  14891. outerQuote = null;
  14892. }
  14893. else if (outerQuote == null && isQuote(char)) {
  14894. outerQuote = char;
  14895. }
  14896. }
  14897. return null;
  14898. }
  14899. _checkNoInterpolation(input, location, { start, end }) {
  14900. let startIndex = -1;
  14901. let endIndex = -1;
  14902. for (const charIndex of this._forEachUnquotedChar(input, 0)) {
  14903. if (startIndex === -1) {
  14904. if (input.startsWith(start)) {
  14905. startIndex = charIndex;
  14906. }
  14907. }
  14908. else {
  14909. endIndex = this._getInterpolationEndIndex(input, end, charIndex);
  14910. if (endIndex > -1) {
  14911. break;
  14912. }
  14913. }
  14914. }
  14915. if (startIndex > -1 && endIndex > -1) {
  14916. this._reportError(`Got interpolation (${start}${end}) where expression was expected`, input, `at column ${startIndex} in`, location);
  14917. }
  14918. }
  14919. /**
  14920. * Finds the index of the end of an interpolation expression
  14921. * while ignoring comments and quoted content.
  14922. */
  14923. _getInterpolationEndIndex(input, expressionEnd, start) {
  14924. for (const charIndex of this._forEachUnquotedChar(input, start)) {
  14925. if (input.startsWith(expressionEnd, charIndex)) {
  14926. return charIndex;
  14927. }
  14928. // Nothing else in the expression matters after we've
  14929. // hit a comment so look directly for the end token.
  14930. if (input.startsWith('//', charIndex)) {
  14931. return input.indexOf(expressionEnd, charIndex);
  14932. }
  14933. }
  14934. return -1;
  14935. }
  14936. /**
  14937. * Generator used to iterate over the character indexes of a string that are outside of quotes.
  14938. * @param input String to loop through.
  14939. * @param start Index within the string at which to start.
  14940. */
  14941. *_forEachUnquotedChar(input, start) {
  14942. let currentQuote = null;
  14943. let escapeCount = 0;
  14944. for (let i = start; i < input.length; i++) {
  14945. const char = input[i];
  14946. // Skip the characters inside quotes. Note that we only care about the outer-most
  14947. // quotes matching up and we need to account for escape characters.
  14948. if (isQuote(input.charCodeAt(i)) && (currentQuote === null || currentQuote === char) &&
  14949. escapeCount % 2 === 0) {
  14950. currentQuote = currentQuote === null ? char : null;
  14951. }
  14952. else if (currentQuote === null) {
  14953. yield i;
  14954. }
  14955. escapeCount = char === '\\' ? escapeCount + 1 : 0;
  14956. }
  14957. }
  14958. }
  14959. class IvyParser extends Parser$1 {
  14960. constructor() {
  14961. super(...arguments);
  14962. this.simpleExpressionChecker = IvySimpleExpressionChecker;
  14963. }
  14964. }
  14965. /** Describes a stateful context an expression parser is in. */
  14966. var ParseContextFlags;
  14967. (function (ParseContextFlags) {
  14968. ParseContextFlags[ParseContextFlags["None"] = 0] = "None";
  14969. /**
  14970. * A Writable context is one in which a value may be written to an lvalue.
  14971. * For example, after we see a property access, we may expect a write to the
  14972. * property via the "=" operator.
  14973. * prop
  14974. * ^ possible "=" after
  14975. */
  14976. ParseContextFlags[ParseContextFlags["Writable"] = 1] = "Writable";
  14977. })(ParseContextFlags || (ParseContextFlags = {}));
  14978. class _ParseAST {
  14979. constructor(input, location, absoluteOffset, tokens, inputLength, parseAction, errors, offset) {
  14980. this.input = input;
  14981. this.location = location;
  14982. this.absoluteOffset = absoluteOffset;
  14983. this.tokens = tokens;
  14984. this.inputLength = inputLength;
  14985. this.parseAction = parseAction;
  14986. this.errors = errors;
  14987. this.offset = offset;
  14988. this.rparensExpected = 0;
  14989. this.rbracketsExpected = 0;
  14990. this.rbracesExpected = 0;
  14991. this.context = ParseContextFlags.None;
  14992. // Cache of expression start and input indeces to the absolute source span they map to, used to
  14993. // prevent creating superfluous source spans in `sourceSpan`.
  14994. // A serial of the expression start and input index is used for mapping because both are stateful
  14995. // and may change for subsequent expressions visited by the parser.
  14996. this.sourceSpanCache = new Map();
  14997. this.index = 0;
  14998. }
  14999. peek(offset) {
  15000. const i = this.index + offset;
  15001. return i < this.tokens.length ? this.tokens[i] : EOF;
  15002. }
  15003. get next() {
  15004. return this.peek(0);
  15005. }
  15006. /** Whether all the parser input has been processed. */
  15007. get atEOF() {
  15008. return this.index >= this.tokens.length;
  15009. }
  15010. /**
  15011. * Index of the next token to be processed, or the end of the last token if all have been
  15012. * processed.
  15013. */
  15014. get inputIndex() {
  15015. return this.atEOF ? this.currentEndIndex : this.next.index + this.offset;
  15016. }
  15017. /**
  15018. * End index of the last processed token, or the start of the first token if none have been
  15019. * processed.
  15020. */
  15021. get currentEndIndex() {
  15022. if (this.index > 0) {
  15023. const curToken = this.peek(-1);
  15024. return curToken.end + this.offset;
  15025. }
  15026. // No tokens have been processed yet; return the next token's start or the length of the input
  15027. // if there is no token.
  15028. if (this.tokens.length === 0) {
  15029. return this.inputLength + this.offset;
  15030. }
  15031. return this.next.index + this.offset;
  15032. }
  15033. /**
  15034. * Returns the absolute offset of the start of the current token.
  15035. */
  15036. get currentAbsoluteOffset() {
  15037. return this.absoluteOffset + this.inputIndex;
  15038. }
  15039. /**
  15040. * Retrieve a `ParseSpan` from `start` to the current position (or to `artificialEndIndex` if
  15041. * provided).
  15042. *
  15043. * @param start Position from which the `ParseSpan` will start.
  15044. * @param artificialEndIndex Optional ending index to be used if provided (and if greater than the
  15045. * natural ending index)
  15046. */
  15047. span(start, artificialEndIndex) {
  15048. let endIndex = this.currentEndIndex;
  15049. if (artificialEndIndex !== undefined && artificialEndIndex > this.currentEndIndex) {
  15050. endIndex = artificialEndIndex;
  15051. }
  15052. // In some unusual parsing scenarios (like when certain tokens are missing and an `EmptyExpr` is
  15053. // being created), the current token may already be advanced beyond the `currentEndIndex`. This
  15054. // appears to be a deep-seated parser bug.
  15055. //
  15056. // As a workaround for now, swap the start and end indices to ensure a valid `ParseSpan`.
  15057. // TODO(alxhub): fix the bug upstream in the parser state, and remove this workaround.
  15058. if (start > endIndex) {
  15059. const tmp = endIndex;
  15060. endIndex = start;
  15061. start = tmp;
  15062. }
  15063. return new ParseSpan(start, endIndex);
  15064. }
  15065. sourceSpan(start, artificialEndIndex) {
  15066. const serial = `${start}@${this.inputIndex}:${artificialEndIndex}`;
  15067. if (!this.sourceSpanCache.has(serial)) {
  15068. this.sourceSpanCache.set(serial, this.span(start, artificialEndIndex).toAbsolute(this.absoluteOffset));
  15069. }
  15070. return this.sourceSpanCache.get(serial);
  15071. }
  15072. advance() {
  15073. this.index++;
  15074. }
  15075. /**
  15076. * Executes a callback in the provided context.
  15077. */
  15078. withContext(context, cb) {
  15079. this.context |= context;
  15080. const ret = cb();
  15081. this.context ^= context;
  15082. return ret;
  15083. }
  15084. consumeOptionalCharacter(code) {
  15085. if (this.next.isCharacter(code)) {
  15086. this.advance();
  15087. return true;
  15088. }
  15089. else {
  15090. return false;
  15091. }
  15092. }
  15093. peekKeywordLet() {
  15094. return this.next.isKeywordLet();
  15095. }
  15096. peekKeywordAs() {
  15097. return this.next.isKeywordAs();
  15098. }
  15099. /**
  15100. * Consumes an expected character, otherwise emits an error about the missing expected character
  15101. * and skips over the token stream until reaching a recoverable point.
  15102. *
  15103. * See `this.error` and `this.skip` for more details.
  15104. */
  15105. expectCharacter(code) {
  15106. if (this.consumeOptionalCharacter(code))
  15107. return;
  15108. this.error(`Missing expected ${String.fromCharCode(code)}`);
  15109. }
  15110. consumeOptionalOperator(op) {
  15111. if (this.next.isOperator(op)) {
  15112. this.advance();
  15113. return true;
  15114. }
  15115. else {
  15116. return false;
  15117. }
  15118. }
  15119. expectOperator(operator) {
  15120. if (this.consumeOptionalOperator(operator))
  15121. return;
  15122. this.error(`Missing expected operator ${operator}`);
  15123. }
  15124. prettyPrintToken(tok) {
  15125. return tok === EOF ? 'end of input' : `token ${tok}`;
  15126. }
  15127. expectIdentifierOrKeyword() {
  15128. const n = this.next;
  15129. if (!n.isIdentifier() && !n.isKeyword()) {
  15130. if (n.isPrivateIdentifier()) {
  15131. this._reportErrorForPrivateIdentifier(n, 'expected identifier or keyword');
  15132. }
  15133. else {
  15134. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier or keyword`);
  15135. }
  15136. return null;
  15137. }
  15138. this.advance();
  15139. return n.toString();
  15140. }
  15141. expectIdentifierOrKeywordOrString() {
  15142. const n = this.next;
  15143. if (!n.isIdentifier() && !n.isKeyword() && !n.isString()) {
  15144. if (n.isPrivateIdentifier()) {
  15145. this._reportErrorForPrivateIdentifier(n, 'expected identifier, keyword or string');
  15146. }
  15147. else {
  15148. this.error(`Unexpected ${this.prettyPrintToken(n)}, expected identifier, keyword, or string`);
  15149. }
  15150. return '';
  15151. }
  15152. this.advance();
  15153. return n.toString();
  15154. }
  15155. parseChain() {
  15156. const exprs = [];
  15157. const start = this.inputIndex;
  15158. while (this.index < this.tokens.length) {
  15159. const expr = this.parsePipe();
  15160. exprs.push(expr);
  15161. if (this.consumeOptionalCharacter($SEMICOLON)) {
  15162. if (!this.parseAction) {
  15163. this.error('Binding expression cannot contain chained expression');
  15164. }
  15165. while (this.consumeOptionalCharacter($SEMICOLON)) {
  15166. } // read all semicolons
  15167. }
  15168. else if (this.index < this.tokens.length) {
  15169. this.error(`Unexpected token '${this.next}'`);
  15170. }
  15171. }
  15172. if (exprs.length == 0) {
  15173. // We have no expressions so create an empty expression that spans the entire input length
  15174. const artificialStart = this.offset;
  15175. const artificialEnd = this.offset + this.inputLength;
  15176. return new EmptyExpr(this.span(artificialStart, artificialEnd), this.sourceSpan(artificialStart, artificialEnd));
  15177. }
  15178. if (exprs.length == 1)
  15179. return exprs[0];
  15180. return new Chain(this.span(start), this.sourceSpan(start), exprs);
  15181. }
  15182. parsePipe() {
  15183. const start = this.inputIndex;
  15184. let result = this.parseExpression();
  15185. if (this.consumeOptionalOperator('|')) {
  15186. if (this.parseAction) {
  15187. this.error('Cannot have a pipe in an action expression');
  15188. }
  15189. do {
  15190. const nameStart = this.inputIndex;
  15191. let nameId = this.expectIdentifierOrKeyword();
  15192. let nameSpan;
  15193. let fullSpanEnd = undefined;
  15194. if (nameId !== null) {
  15195. nameSpan = this.sourceSpan(nameStart);
  15196. }
  15197. else {
  15198. // No valid identifier was found, so we'll assume an empty pipe name ('').
  15199. nameId = '';
  15200. // However, there may have been whitespace present between the pipe character and the next
  15201. // token in the sequence (or the end of input). We want to track this whitespace so that
  15202. // the `BindingPipe` we produce covers not just the pipe character, but any trailing
  15203. // whitespace beyond it. Another way of thinking about this is that the zero-length name
  15204. // is assumed to be at the end of any whitespace beyond the pipe character.
  15205. //
  15206. // Therefore, we push the end of the `ParseSpan` for this pipe all the way up to the
  15207. // beginning of the next token, or until the end of input if the next token is EOF.
  15208. fullSpanEnd = this.next.index !== -1 ? this.next.index : this.inputLength + this.offset;
  15209. // The `nameSpan` for an empty pipe name is zero-length at the end of any whitespace
  15210. // beyond the pipe character.
  15211. nameSpan = new ParseSpan(fullSpanEnd, fullSpanEnd).toAbsolute(this.absoluteOffset);
  15212. }
  15213. const args = [];
  15214. while (this.consumeOptionalCharacter($COLON)) {
  15215. args.push(this.parseExpression());
  15216. // If there are additional expressions beyond the name, then the artificial end for the
  15217. // name is no longer relevant.
  15218. }
  15219. result = new BindingPipe(this.span(start), this.sourceSpan(start, fullSpanEnd), result, nameId, args, nameSpan);
  15220. } while (this.consumeOptionalOperator('|'));
  15221. }
  15222. return result;
  15223. }
  15224. parseExpression() {
  15225. return this.parseConditional();
  15226. }
  15227. parseConditional() {
  15228. const start = this.inputIndex;
  15229. const result = this.parseLogicalOr();
  15230. if (this.consumeOptionalOperator('?')) {
  15231. const yes = this.parsePipe();
  15232. let no;
  15233. if (!this.consumeOptionalCharacter($COLON)) {
  15234. const end = this.inputIndex;
  15235. const expression = this.input.substring(start, end);
  15236. this.error(`Conditional expression ${expression} requires all 3 expressions`);
  15237. no = new EmptyExpr(this.span(start), this.sourceSpan(start));
  15238. }
  15239. else {
  15240. no = this.parsePipe();
  15241. }
  15242. return new Conditional(this.span(start), this.sourceSpan(start), result, yes, no);
  15243. }
  15244. else {
  15245. return result;
  15246. }
  15247. }
  15248. parseLogicalOr() {
  15249. // '||'
  15250. const start = this.inputIndex;
  15251. let result = this.parseLogicalAnd();
  15252. while (this.consumeOptionalOperator('||')) {
  15253. const right = this.parseLogicalAnd();
  15254. result = new Binary(this.span(start), this.sourceSpan(start), '||', result, right);
  15255. }
  15256. return result;
  15257. }
  15258. parseLogicalAnd() {
  15259. // '&&'
  15260. const start = this.inputIndex;
  15261. let result = this.parseNullishCoalescing();
  15262. while (this.consumeOptionalOperator('&&')) {
  15263. const right = this.parseNullishCoalescing();
  15264. result = new Binary(this.span(start), this.sourceSpan(start), '&&', result, right);
  15265. }
  15266. return result;
  15267. }
  15268. parseNullishCoalescing() {
  15269. // '??'
  15270. const start = this.inputIndex;
  15271. let result = this.parseEquality();
  15272. while (this.consumeOptionalOperator('??')) {
  15273. const right = this.parseEquality();
  15274. result = new Binary(this.span(start), this.sourceSpan(start), '??', result, right);
  15275. }
  15276. return result;
  15277. }
  15278. parseEquality() {
  15279. // '==','!=','===','!=='
  15280. const start = this.inputIndex;
  15281. let result = this.parseRelational();
  15282. while (this.next.type == TokenType$1.Operator) {
  15283. const operator = this.next.strValue;
  15284. switch (operator) {
  15285. case '==':
  15286. case '===':
  15287. case '!=':
  15288. case '!==':
  15289. this.advance();
  15290. const right = this.parseRelational();
  15291. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  15292. continue;
  15293. }
  15294. break;
  15295. }
  15296. return result;
  15297. }
  15298. parseRelational() {
  15299. // '<', '>', '<=', '>='
  15300. const start = this.inputIndex;
  15301. let result = this.parseAdditive();
  15302. while (this.next.type == TokenType$1.Operator) {
  15303. const operator = this.next.strValue;
  15304. switch (operator) {
  15305. case '<':
  15306. case '>':
  15307. case '<=':
  15308. case '>=':
  15309. this.advance();
  15310. const right = this.parseAdditive();
  15311. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  15312. continue;
  15313. }
  15314. break;
  15315. }
  15316. return result;
  15317. }
  15318. parseAdditive() {
  15319. // '+', '-'
  15320. const start = this.inputIndex;
  15321. let result = this.parseMultiplicative();
  15322. while (this.next.type == TokenType$1.Operator) {
  15323. const operator = this.next.strValue;
  15324. switch (operator) {
  15325. case '+':
  15326. case '-':
  15327. this.advance();
  15328. let right = this.parseMultiplicative();
  15329. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  15330. continue;
  15331. }
  15332. break;
  15333. }
  15334. return result;
  15335. }
  15336. parseMultiplicative() {
  15337. // '*', '%', '/'
  15338. const start = this.inputIndex;
  15339. let result = this.parsePrefix();
  15340. while (this.next.type == TokenType$1.Operator) {
  15341. const operator = this.next.strValue;
  15342. switch (operator) {
  15343. case '*':
  15344. case '%':
  15345. case '/':
  15346. this.advance();
  15347. let right = this.parsePrefix();
  15348. result = new Binary(this.span(start), this.sourceSpan(start), operator, result, right);
  15349. continue;
  15350. }
  15351. break;
  15352. }
  15353. return result;
  15354. }
  15355. parsePrefix() {
  15356. if (this.next.type == TokenType$1.Operator) {
  15357. const start = this.inputIndex;
  15358. const operator = this.next.strValue;
  15359. let result;
  15360. switch (operator) {
  15361. case '+':
  15362. this.advance();
  15363. result = this.parsePrefix();
  15364. return Unary.createPlus(this.span(start), this.sourceSpan(start), result);
  15365. case '-':
  15366. this.advance();
  15367. result = this.parsePrefix();
  15368. return Unary.createMinus(this.span(start), this.sourceSpan(start), result);
  15369. case '!':
  15370. this.advance();
  15371. result = this.parsePrefix();
  15372. return new PrefixNot(this.span(start), this.sourceSpan(start), result);
  15373. }
  15374. }
  15375. return this.parseCallChain();
  15376. }
  15377. parseCallChain() {
  15378. const start = this.inputIndex;
  15379. let result = this.parsePrimary();
  15380. while (true) {
  15381. if (this.consumeOptionalCharacter($PERIOD)) {
  15382. result = this.parseAccessMemberOrMethodCall(result, start, false);
  15383. }
  15384. else if (this.consumeOptionalOperator('?.')) {
  15385. result = this.parseAccessMemberOrMethodCall(result, start, true);
  15386. }
  15387. else if (this.consumeOptionalCharacter($LBRACKET)) {
  15388. this.withContext(ParseContextFlags.Writable, () => {
  15389. this.rbracketsExpected++;
  15390. const key = this.parsePipe();
  15391. if (key instanceof EmptyExpr) {
  15392. this.error(`Key access cannot be empty`);
  15393. }
  15394. this.rbracketsExpected--;
  15395. this.expectCharacter($RBRACKET);
  15396. if (this.consumeOptionalOperator('=')) {
  15397. const value = this.parseConditional();
  15398. result = new KeyedWrite(this.span(start), this.sourceSpan(start), result, key, value);
  15399. }
  15400. else {
  15401. result = new KeyedRead(this.span(start), this.sourceSpan(start), result, key);
  15402. }
  15403. });
  15404. }
  15405. else if (this.consumeOptionalCharacter($LPAREN)) {
  15406. this.rparensExpected++;
  15407. const args = this.parseCallArguments();
  15408. this.rparensExpected--;
  15409. this.expectCharacter($RPAREN);
  15410. result = new FunctionCall(this.span(start), this.sourceSpan(start), result, args);
  15411. }
  15412. else if (this.consumeOptionalOperator('!')) {
  15413. result = new NonNullAssert(this.span(start), this.sourceSpan(start), result);
  15414. }
  15415. else {
  15416. return result;
  15417. }
  15418. }
  15419. }
  15420. parsePrimary() {
  15421. const start = this.inputIndex;
  15422. if (this.consumeOptionalCharacter($LPAREN)) {
  15423. this.rparensExpected++;
  15424. const result = this.parsePipe();
  15425. this.rparensExpected--;
  15426. this.expectCharacter($RPAREN);
  15427. return result;
  15428. }
  15429. else if (this.next.isKeywordNull()) {
  15430. this.advance();
  15431. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), null);
  15432. }
  15433. else if (this.next.isKeywordUndefined()) {
  15434. this.advance();
  15435. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), void 0);
  15436. }
  15437. else if (this.next.isKeywordTrue()) {
  15438. this.advance();
  15439. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), true);
  15440. }
  15441. else if (this.next.isKeywordFalse()) {
  15442. this.advance();
  15443. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), false);
  15444. }
  15445. else if (this.next.isKeywordThis()) {
  15446. this.advance();
  15447. return new ThisReceiver(this.span(start), this.sourceSpan(start));
  15448. }
  15449. else if (this.consumeOptionalCharacter($LBRACKET)) {
  15450. this.rbracketsExpected++;
  15451. const elements = this.parseExpressionList($RBRACKET);
  15452. this.rbracketsExpected--;
  15453. this.expectCharacter($RBRACKET);
  15454. return new LiteralArray(this.span(start), this.sourceSpan(start), elements);
  15455. }
  15456. else if (this.next.isCharacter($LBRACE)) {
  15457. return this.parseLiteralMap();
  15458. }
  15459. else if (this.next.isIdentifier()) {
  15460. return this.parseAccessMemberOrMethodCall(new ImplicitReceiver(this.span(start), this.sourceSpan(start)), start, false);
  15461. }
  15462. else if (this.next.isNumber()) {
  15463. const value = this.next.toNumber();
  15464. this.advance();
  15465. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), value);
  15466. }
  15467. else if (this.next.isString()) {
  15468. const literalValue = this.next.toString();
  15469. this.advance();
  15470. return new LiteralPrimitive(this.span(start), this.sourceSpan(start), literalValue);
  15471. }
  15472. else if (this.next.isPrivateIdentifier()) {
  15473. this._reportErrorForPrivateIdentifier(this.next, null);
  15474. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  15475. }
  15476. else if (this.index >= this.tokens.length) {
  15477. this.error(`Unexpected end of expression: ${this.input}`);
  15478. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  15479. }
  15480. else {
  15481. this.error(`Unexpected token ${this.next}`);
  15482. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  15483. }
  15484. }
  15485. parseExpressionList(terminator) {
  15486. const result = [];
  15487. do {
  15488. if (!this.next.isCharacter(terminator)) {
  15489. result.push(this.parsePipe());
  15490. }
  15491. else {
  15492. break;
  15493. }
  15494. } while (this.consumeOptionalCharacter($COMMA));
  15495. return result;
  15496. }
  15497. parseLiteralMap() {
  15498. const keys = [];
  15499. const values = [];
  15500. const start = this.inputIndex;
  15501. this.expectCharacter($LBRACE);
  15502. if (!this.consumeOptionalCharacter($RBRACE)) {
  15503. this.rbracesExpected++;
  15504. do {
  15505. const quoted = this.next.isString();
  15506. const key = this.expectIdentifierOrKeywordOrString();
  15507. keys.push({ key, quoted });
  15508. this.expectCharacter($COLON);
  15509. values.push(this.parsePipe());
  15510. } while (this.consumeOptionalCharacter($COMMA));
  15511. this.rbracesExpected--;
  15512. this.expectCharacter($RBRACE);
  15513. }
  15514. return new LiteralMap(this.span(start), this.sourceSpan(start), keys, values);
  15515. }
  15516. parseAccessMemberOrMethodCall(receiver, start, isSafe = false) {
  15517. const nameStart = this.inputIndex;
  15518. const id = this.withContext(ParseContextFlags.Writable, () => {
  15519. var _a;
  15520. const id = (_a = this.expectIdentifierOrKeyword()) !== null && _a !== void 0 ? _a : '';
  15521. if (id.length === 0) {
  15522. this.error(`Expected identifier for property access`, receiver.span.end);
  15523. }
  15524. return id;
  15525. });
  15526. const nameSpan = this.sourceSpan(nameStart);
  15527. if (this.consumeOptionalCharacter($LPAREN)) {
  15528. const argumentStart = this.inputIndex;
  15529. this.rparensExpected++;
  15530. const args = this.parseCallArguments();
  15531. const argumentSpan = this.span(argumentStart, this.inputIndex).toAbsolute(this.absoluteOffset);
  15532. this.expectCharacter($RPAREN);
  15533. this.rparensExpected--;
  15534. const span = this.span(start);
  15535. const sourceSpan = this.sourceSpan(start);
  15536. return isSafe ?
  15537. new SafeMethodCall(span, sourceSpan, nameSpan, receiver, id, args, argumentSpan) :
  15538. new MethodCall(span, sourceSpan, nameSpan, receiver, id, args, argumentSpan);
  15539. }
  15540. else {
  15541. if (isSafe) {
  15542. if (this.consumeOptionalOperator('=')) {
  15543. this.error('The \'?.\' operator cannot be used in the assignment');
  15544. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  15545. }
  15546. else {
  15547. return new SafePropertyRead(this.span(start), this.sourceSpan(start), nameSpan, receiver, id);
  15548. }
  15549. }
  15550. else {
  15551. if (this.consumeOptionalOperator('=')) {
  15552. if (!this.parseAction) {
  15553. this.error('Bindings cannot contain assignments');
  15554. return new EmptyExpr(this.span(start), this.sourceSpan(start));
  15555. }
  15556. const value = this.parseConditional();
  15557. return new PropertyWrite(this.span(start), this.sourceSpan(start), nameSpan, receiver, id, value);
  15558. }
  15559. else {
  15560. return new PropertyRead(this.span(start), this.sourceSpan(start), nameSpan, receiver, id);
  15561. }
  15562. }
  15563. }
  15564. }
  15565. parseCallArguments() {
  15566. if (this.next.isCharacter($RPAREN))
  15567. return [];
  15568. const positionals = [];
  15569. do {
  15570. positionals.push(this.parsePipe());
  15571. } while (this.consumeOptionalCharacter($COMMA));
  15572. return positionals;
  15573. }
  15574. /**
  15575. * Parses an identifier, a keyword, a string with an optional `-` in between,
  15576. * and returns the string along with its absolute source span.
  15577. */
  15578. expectTemplateBindingKey() {
  15579. let result = '';
  15580. let operatorFound = false;
  15581. const start = this.currentAbsoluteOffset;
  15582. do {
  15583. result += this.expectIdentifierOrKeywordOrString();
  15584. operatorFound = this.consumeOptionalOperator('-');
  15585. if (operatorFound) {
  15586. result += '-';
  15587. }
  15588. } while (operatorFound);
  15589. return {
  15590. source: result,
  15591. span: new AbsoluteSourceSpan(start, start + result.length),
  15592. };
  15593. }
  15594. /**
  15595. * Parse microsyntax template expression and return a list of bindings or
  15596. * parsing errors in case the given expression is invalid.
  15597. *
  15598. * For example,
  15599. * ```
  15600. * <div *ngFor="let item of items; index as i; trackBy: func">
  15601. * ```
  15602. * contains five bindings:
  15603. * 1. ngFor -> null
  15604. * 2. item -> NgForOfContext.$implicit
  15605. * 3. ngForOf -> items
  15606. * 4. i -> NgForOfContext.index
  15607. * 5. ngForTrackBy -> func
  15608. *
  15609. * For a full description of the microsyntax grammar, see
  15610. * https://gist.github.com/mhevery/d3530294cff2e4a1b3fe15ff75d08855
  15611. *
  15612. * @param templateKey name of the microsyntax directive, like ngIf, ngFor,
  15613. * without the *, along with its absolute span.
  15614. */
  15615. parseTemplateBindings(templateKey) {
  15616. const bindings = [];
  15617. // The first binding is for the template key itself
  15618. // In *ngFor="let item of items", key = "ngFor", value = null
  15619. // In *ngIf="cond | pipe", key = "ngIf", value = "cond | pipe"
  15620. bindings.push(...this.parseDirectiveKeywordBindings(templateKey));
  15621. while (this.index < this.tokens.length) {
  15622. // If it starts with 'let', then this must be variable declaration
  15623. const letBinding = this.parseLetBinding();
  15624. if (letBinding) {
  15625. bindings.push(letBinding);
  15626. }
  15627. else {
  15628. // Two possible cases here, either `value "as" key` or
  15629. // "directive-keyword expression". We don't know which case, but both
  15630. // "value" and "directive-keyword" are template binding key, so consume
  15631. // the key first.
  15632. const key = this.expectTemplateBindingKey();
  15633. // Peek at the next token, if it is "as" then this must be variable
  15634. // declaration.
  15635. const binding = this.parseAsBinding(key);
  15636. if (binding) {
  15637. bindings.push(binding);
  15638. }
  15639. else {
  15640. // Otherwise the key must be a directive keyword, like "of". Transform
  15641. // the key to actual key. Eg. of -> ngForOf, trackBy -> ngForTrackBy
  15642. key.source =
  15643. templateKey.source + key.source.charAt(0).toUpperCase() + key.source.substring(1);
  15644. bindings.push(...this.parseDirectiveKeywordBindings(key));
  15645. }
  15646. }
  15647. this.consumeStatementTerminator();
  15648. }
  15649. return new TemplateBindingParseResult(bindings, [] /* warnings */, this.errors);
  15650. }
  15651. /**
  15652. * Parse a directive keyword, followed by a mandatory expression.
  15653. * For example, "of items", "trackBy: func".
  15654. * The bindings are: ngForOf -> items, ngForTrackBy -> func
  15655. * There could be an optional "as" binding that follows the expression.
  15656. * For example,
  15657. * ```
  15658. * *ngFor="let item of items | slice:0:1 as collection".
  15659. * ^^ ^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
  15660. * keyword bound target optional 'as' binding
  15661. * ```
  15662. *
  15663. * @param key binding key, for example, ngFor, ngIf, ngForOf, along with its
  15664. * absolute span.
  15665. */
  15666. parseDirectiveKeywordBindings(key) {
  15667. const bindings = [];
  15668. this.consumeOptionalCharacter($COLON); // trackBy: trackByFunction
  15669. const value = this.getDirectiveBoundTarget();
  15670. let spanEnd = this.currentAbsoluteOffset;
  15671. // The binding could optionally be followed by "as". For example,
  15672. // *ngIf="cond | pipe as x". In this case, the key in the "as" binding
  15673. // is "x" and the value is the template key itself ("ngIf"). Note that the
  15674. // 'key' in the current context now becomes the "value" in the next binding.
  15675. const asBinding = this.parseAsBinding(key);
  15676. if (!asBinding) {
  15677. this.consumeStatementTerminator();
  15678. spanEnd = this.currentAbsoluteOffset;
  15679. }
  15680. const sourceSpan = new AbsoluteSourceSpan(key.span.start, spanEnd);
  15681. bindings.push(new ExpressionBinding(sourceSpan, key, value));
  15682. if (asBinding) {
  15683. bindings.push(asBinding);
  15684. }
  15685. return bindings;
  15686. }
  15687. /**
  15688. * Return the expression AST for the bound target of a directive keyword
  15689. * binding. For example,
  15690. * ```
  15691. * *ngIf="condition | pipe"
  15692. * ^^^^^^^^^^^^^^^^ bound target for "ngIf"
  15693. * *ngFor="let item of items"
  15694. * ^^^^^ bound target for "ngForOf"
  15695. * ```
  15696. */
  15697. getDirectiveBoundTarget() {
  15698. if (this.next === EOF || this.peekKeywordAs() || this.peekKeywordLet()) {
  15699. return null;
  15700. }
  15701. const ast = this.parsePipe(); // example: "condition | async"
  15702. const { start, end } = ast.span;
  15703. const value = this.input.substring(start, end);
  15704. return new ASTWithSource(ast, value, this.location, this.absoluteOffset + start, this.errors);
  15705. }
  15706. /**
  15707. * Return the binding for a variable declared using `as`. Note that the order
  15708. * of the key-value pair in this declaration is reversed. For example,
  15709. * ```
  15710. * *ngFor="let item of items; index as i"
  15711. * ^^^^^ ^
  15712. * value key
  15713. * ```
  15714. *
  15715. * @param value name of the value in the declaration, "ngIf" in the example
  15716. * above, along with its absolute span.
  15717. */
  15718. parseAsBinding(value) {
  15719. if (!this.peekKeywordAs()) {
  15720. return null;
  15721. }
  15722. this.advance(); // consume the 'as' keyword
  15723. const key = this.expectTemplateBindingKey();
  15724. this.consumeStatementTerminator();
  15725. const sourceSpan = new AbsoluteSourceSpan(value.span.start, this.currentAbsoluteOffset);
  15726. return new VariableBinding(sourceSpan, key, value);
  15727. }
  15728. /**
  15729. * Return the binding for a variable declared using `let`. For example,
  15730. * ```
  15731. * *ngFor="let item of items; let i=index;"
  15732. * ^^^^^^^^ ^^^^^^^^^^^
  15733. * ```
  15734. * In the first binding, `item` is bound to `NgForOfContext.$implicit`.
  15735. * In the second binding, `i` is bound to `NgForOfContext.index`.
  15736. */
  15737. parseLetBinding() {
  15738. if (!this.peekKeywordLet()) {
  15739. return null;
  15740. }
  15741. const spanStart = this.currentAbsoluteOffset;
  15742. this.advance(); // consume the 'let' keyword
  15743. const key = this.expectTemplateBindingKey();
  15744. let value = null;
  15745. if (this.consumeOptionalOperator('=')) {
  15746. value = this.expectTemplateBindingKey();
  15747. }
  15748. this.consumeStatementTerminator();
  15749. const sourceSpan = new AbsoluteSourceSpan(spanStart, this.currentAbsoluteOffset);
  15750. return new VariableBinding(sourceSpan, key, value);
  15751. }
  15752. /**
  15753. * Consume the optional statement terminator: semicolon or comma.
  15754. */
  15755. consumeStatementTerminator() {
  15756. this.consumeOptionalCharacter($SEMICOLON) || this.consumeOptionalCharacter($COMMA);
  15757. }
  15758. /**
  15759. * Records an error and skips over the token stream until reaching a recoverable point. See
  15760. * `this.skip` for more details on token skipping.
  15761. */
  15762. error(message, index = null) {
  15763. this.errors.push(new ParserError(message, this.input, this.locationText(index), this.location));
  15764. this.skip();
  15765. }
  15766. locationText(index = null) {
  15767. if (index == null)
  15768. index = this.index;
  15769. return (index < this.tokens.length) ? `at column ${this.tokens[index].index + 1} in` :
  15770. `at the end of the expression`;
  15771. }
  15772. /**
  15773. * Records an error for an unexpected private identifier being discovered.
  15774. * @param token Token representing a private identifier.
  15775. * @param extraMessage Optional additional message being appended to the error.
  15776. */
  15777. _reportErrorForPrivateIdentifier(token, extraMessage) {
  15778. let errorMessage = `Private identifiers are not supported. Unexpected private identifier: ${token}`;
  15779. if (extraMessage !== null) {
  15780. errorMessage += `, ${extraMessage}`;
  15781. }
  15782. this.error(errorMessage);
  15783. }
  15784. /**
  15785. * Error recovery should skip tokens until it encounters a recovery point.
  15786. *
  15787. * The following are treated as unconditional recovery points:
  15788. * - end of input
  15789. * - ';' (parseChain() is always the root production, and it expects a ';')
  15790. * - '|' (since pipes may be chained and each pipe expression may be treated independently)
  15791. *
  15792. * The following are conditional recovery points:
  15793. * - ')', '}', ']' if one of calling productions is expecting one of these symbols
  15794. * - This allows skip() to recover from errors such as '(a.) + 1' allowing more of the AST to
  15795. * be retained (it doesn't skip any tokens as the ')' is retained because of the '(' begins
  15796. * an '(' <expr> ')' production).
  15797. * The recovery points of grouping symbols must be conditional as they must be skipped if
  15798. * none of the calling productions are not expecting the closing token else we will never
  15799. * make progress in the case of an extraneous group closing symbol (such as a stray ')').
  15800. * That is, we skip a closing symbol if we are not in a grouping production.
  15801. * - '=' in a `Writable` context
  15802. * - In this context, we are able to recover after seeing the `=` operator, which
  15803. * signals the presence of an independent rvalue expression following the `=` operator.
  15804. *
  15805. * If a production expects one of these token it increments the corresponding nesting count,
  15806. * and then decrements it just prior to checking if the token is in the input.
  15807. */
  15808. skip() {
  15809. let n = this.next;
  15810. while (this.index < this.tokens.length && !n.isCharacter($SEMICOLON) &&
  15811. !n.isOperator('|') && (this.rparensExpected <= 0 || !n.isCharacter($RPAREN)) &&
  15812. (this.rbracesExpected <= 0 || !n.isCharacter($RBRACE)) &&
  15813. (this.rbracketsExpected <= 0 || !n.isCharacter($RBRACKET)) &&
  15814. (!(this.context & ParseContextFlags.Writable) || !n.isOperator('='))) {
  15815. if (this.next.isError()) {
  15816. this.errors.push(new ParserError(this.next.toString(), this.input, this.locationText(), this.location));
  15817. }
  15818. this.advance();
  15819. n = this.next;
  15820. }
  15821. }
  15822. }
  15823. class SimpleExpressionChecker {
  15824. constructor() {
  15825. this.errors = [];
  15826. }
  15827. visitImplicitReceiver(ast, context) { }
  15828. visitThisReceiver(ast, context) { }
  15829. visitInterpolation(ast, context) { }
  15830. visitLiteralPrimitive(ast, context) { }
  15831. visitPropertyRead(ast, context) { }
  15832. visitPropertyWrite(ast, context) { }
  15833. visitSafePropertyRead(ast, context) { }
  15834. visitMethodCall(ast, context) { }
  15835. visitSafeMethodCall(ast, context) { }
  15836. visitFunctionCall(ast, context) { }
  15837. visitLiteralArray(ast, context) {
  15838. this.visitAll(ast.expressions, context);
  15839. }
  15840. visitLiteralMap(ast, context) {
  15841. this.visitAll(ast.values, context);
  15842. }
  15843. visitUnary(ast, context) { }
  15844. visitBinary(ast, context) { }
  15845. visitPrefixNot(ast, context) { }
  15846. visitNonNullAssert(ast, context) { }
  15847. visitConditional(ast, context) { }
  15848. visitPipe(ast, context) {
  15849. this.errors.push('pipes');
  15850. }
  15851. visitKeyedRead(ast, context) { }
  15852. visitKeyedWrite(ast, context) { }
  15853. visitAll(asts, context) {
  15854. return asts.map(node => node.visit(this, context));
  15855. }
  15856. visitChain(ast, context) { }
  15857. visitQuote(ast, context) { }
  15858. }
  15859. /**
  15860. * This class implements SimpleExpressionChecker used in View Engine and performs more strict checks
  15861. * to make sure host bindings do not contain pipes. In View Engine, having pipes in host bindings is
  15862. * not supported as well, but in some cases (like `!(value | async)`) the error is not triggered at
  15863. * compile time. In order to preserve View Engine behavior, more strict checks are introduced for
  15864. * Ivy mode only.
  15865. */
  15866. class IvySimpleExpressionChecker extends RecursiveAstVisitor$1 {
  15867. constructor() {
  15868. super(...arguments);
  15869. this.errors = [];
  15870. }
  15871. visitPipe() {
  15872. this.errors.push('pipes');
  15873. }
  15874. }
  15875. /**
  15876. * @license
  15877. * Copyright Google LLC All Rights Reserved.
  15878. *
  15879. * Use of this source code is governed by an MIT-style license that can be
  15880. * found in the LICENSE file at https://angular.io/license
  15881. */
  15882. function mapEntry(key, value) {
  15883. return { key, value, quoted: false };
  15884. }
  15885. function mapLiteral(obj, quoted = false) {
  15886. return literalMap(Object.keys(obj).map(key => ({
  15887. key,
  15888. quoted,
  15889. value: obj[key],
  15890. })));
  15891. }
  15892. /**
  15893. * @license
  15894. * Copyright Google LLC All Rights Reserved.
  15895. *
  15896. * Use of this source code is governed by an MIT-style license that can be
  15897. * found in the LICENSE file at https://angular.io/license
  15898. */
  15899. // =================================================================================================
  15900. // =================================================================================================
  15901. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  15902. // =================================================================================================
  15903. // =================================================================================================
  15904. //
  15905. // DO NOT EDIT THIS LIST OF SECURITY SENSITIVE PROPERTIES WITHOUT A SECURITY REVIEW!
  15906. // Reach out to mprobst for details.
  15907. //
  15908. // =================================================================================================
  15909. /** Map from tagName|propertyName to SecurityContext. Properties applying to all tags use '*'. */
  15910. let _SECURITY_SCHEMA;
  15911. function SECURITY_SCHEMA() {
  15912. if (!_SECURITY_SCHEMA) {
  15913. _SECURITY_SCHEMA = {};
  15914. // Case is insignificant below, all element and attribute names are lower-cased for lookup.
  15915. registerContext(SecurityContext.HTML, [
  15916. 'iframe|srcdoc',
  15917. '*|innerHTML',
  15918. '*|outerHTML',
  15919. ]);
  15920. registerContext(SecurityContext.STYLE, ['*|style']);
  15921. // NB: no SCRIPT contexts here, they are never allowed due to the parser stripping them.
  15922. registerContext(SecurityContext.URL, [
  15923. '*|formAction', 'area|href', 'area|ping', 'audio|src', 'a|href',
  15924. 'a|ping', 'blockquote|cite', 'body|background', 'del|cite', 'form|action',
  15925. 'img|src', 'img|srcset', 'input|src', 'ins|cite', 'q|cite',
  15926. 'source|src', 'source|srcset', 'track|src', 'video|poster', 'video|src',
  15927. ]);
  15928. registerContext(SecurityContext.RESOURCE_URL, [
  15929. 'applet|code',
  15930. 'applet|codebase',
  15931. 'base|href',
  15932. 'embed|src',
  15933. 'frame|src',
  15934. 'head|profile',
  15935. 'html|manifest',
  15936. 'iframe|src',
  15937. 'link|href',
  15938. 'media|src',
  15939. 'object|codebase',
  15940. 'object|data',
  15941. 'script|src',
  15942. ]);
  15943. }
  15944. return _SECURITY_SCHEMA;
  15945. }
  15946. function registerContext(ctx, specs) {
  15947. for (const spec of specs)
  15948. _SECURITY_SCHEMA[spec.toLowerCase()] = ctx;
  15949. }
  15950. /**
  15951. * @license
  15952. * Copyright Google LLC All Rights Reserved.
  15953. *
  15954. * Use of this source code is governed by an MIT-style license that can be
  15955. * found in the LICENSE file at https://angular.io/license
  15956. */
  15957. class ElementSchemaRegistry {
  15958. }
  15959. /**
  15960. * @license
  15961. * Copyright Google LLC All Rights Reserved.
  15962. *
  15963. * Use of this source code is governed by an MIT-style license that can be
  15964. * found in the LICENSE file at https://angular.io/license
  15965. */
  15966. const BOOLEAN = 'boolean';
  15967. const NUMBER = 'number';
  15968. const STRING = 'string';
  15969. const OBJECT = 'object';
  15970. /**
  15971. * This array represents the DOM schema. It encodes inheritance, properties, and events.
  15972. *
  15973. * ## Overview
  15974. *
  15975. * Each line represents one kind of element. The `element_inheritance` and properties are joined
  15976. * using `element_inheritance|properties` syntax.
  15977. *
  15978. * ## Element Inheritance
  15979. *
  15980. * The `element_inheritance` can be further subdivided as `element1,element2,...^parentElement`.
  15981. * Here the individual elements are separated by `,` (commas). Every element in the list
  15982. * has identical properties.
  15983. *
  15984. * An `element` may inherit additional properties from `parentElement` If no `^parentElement` is
  15985. * specified then `""` (blank) element is assumed.
  15986. *
  15987. * NOTE: The blank element inherits from root `[Element]` element, the super element of all
  15988. * elements.
  15989. *
  15990. * NOTE an element prefix such as `:svg:` has no special meaning to the schema.
  15991. *
  15992. * ## Properties
  15993. *
  15994. * Each element has a set of properties separated by `,` (commas). Each property can be prefixed
  15995. * by a special character designating its type:
  15996. *
  15997. * - (no prefix): property is a string.
  15998. * - `*`: property represents an event.
  15999. * - `!`: property is a boolean.
  16000. * - `#`: property is a number.
  16001. * - `%`: property is an object.
  16002. *
  16003. * ## Query
  16004. *
  16005. * The class creates an internal squas representation which allows to easily answer the query of
  16006. * if a given property exist on a given element.
  16007. *
  16008. * NOTE: We don't yet support querying for types or events.
  16009. * NOTE: This schema is auto extracted from `schema_extractor.ts` located in the test folder,
  16010. * see dom_element_schema_registry_spec.ts
  16011. */
  16012. // =================================================================================================
  16013. // =================================================================================================
  16014. // =========== S T O P - S T O P - S T O P - S T O P - S T O P - S T O P ===========
  16015. // =================================================================================================
  16016. // =================================================================================================
  16017. //
  16018. // DO NOT EDIT THIS DOM SCHEMA WITHOUT A SECURITY REVIEW!
  16019. //
  16020. // Newly added properties must be security reviewed and assigned an appropriate SecurityContext in
  16021. // dom_security_schema.ts. Reach out to mprobst & rjamet for details.
  16022. //
  16023. // =================================================================================================
  16024. const SCHEMA = [
  16025. '[Element]|textContent,%classList,className,id,innerHTML,*beforecopy,*beforecut,*beforepaste,*copy,*cut,*paste,*search,*selectstart,*webkitfullscreenchange,*webkitfullscreenerror,*wheel,outerHTML,#scrollLeft,#scrollTop,slot' +
  16026. /* added manually to avoid breaking changes */
  16027. ',*message,*mozfullscreenchange,*mozfullscreenerror,*mozpointerlockchange,*mozpointerlockerror,*webglcontextcreationerror,*webglcontextlost,*webglcontextrestored',
  16028. '[HTMLElement]^[Element]|accessKey,contentEditable,dir,!draggable,!hidden,innerText,lang,*abort,*auxclick,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*cuechange,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*seeked,*seeking,*select,*show,*stalled,*submit,*suspend,*timeupdate,*toggle,*volumechange,*waiting,outerText,!spellcheck,%style,#tabIndex,title,!translate',
  16029. 'abbr,address,article,aside,b,bdi,bdo,cite,code,dd,dfn,dt,em,figcaption,figure,footer,header,i,kbd,main,mark,nav,noscript,rb,rp,rt,rtc,ruby,s,samp,section,small,strong,sub,sup,u,var,wbr^[HTMLElement]|accessKey,contentEditable,dir,!draggable,!hidden,innerText,lang,*abort,*auxclick,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*cuechange,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*seeked,*seeking,*select,*show,*stalled,*submit,*suspend,*timeupdate,*toggle,*volumechange,*waiting,outerText,!spellcheck,%style,#tabIndex,title,!translate',
  16030. 'media^[HTMLElement]|!autoplay,!controls,%controlsList,%crossOrigin,#currentTime,!defaultMuted,#defaultPlaybackRate,!disableRemotePlayback,!loop,!muted,*encrypted,*waitingforkey,#playbackRate,preload,src,%srcObject,#volume',
  16031. ':svg:^[HTMLElement]|*abort,*auxclick,*blur,*cancel,*canplay,*canplaythrough,*change,*click,*close,*contextmenu,*cuechange,*dblclick,*drag,*dragend,*dragenter,*dragleave,*dragover,*dragstart,*drop,*durationchange,*emptied,*ended,*error,*focus,*gotpointercapture,*input,*invalid,*keydown,*keypress,*keyup,*load,*loadeddata,*loadedmetadata,*loadstart,*lostpointercapture,*mousedown,*mouseenter,*mouseleave,*mousemove,*mouseout,*mouseover,*mouseup,*mousewheel,*pause,*play,*playing,*pointercancel,*pointerdown,*pointerenter,*pointerleave,*pointermove,*pointerout,*pointerover,*pointerup,*progress,*ratechange,*reset,*resize,*scroll,*seeked,*seeking,*select,*show,*stalled,*submit,*suspend,*timeupdate,*toggle,*volumechange,*waiting,%style,#tabIndex',
  16032. ':svg:graphics^:svg:|',
  16033. ':svg:animation^:svg:|*begin,*end,*repeat',
  16034. ':svg:geometry^:svg:|',
  16035. ':svg:componentTransferFunction^:svg:|',
  16036. ':svg:gradient^:svg:|',
  16037. ':svg:textContent^:svg:graphics|',
  16038. ':svg:textPositioning^:svg:textContent|',
  16039. 'a^[HTMLElement]|charset,coords,download,hash,host,hostname,href,hreflang,name,password,pathname,ping,port,protocol,referrerPolicy,rel,rev,search,shape,target,text,type,username',
  16040. 'area^[HTMLElement]|alt,coords,download,hash,host,hostname,href,!noHref,password,pathname,ping,port,protocol,referrerPolicy,rel,search,shape,target,username',
  16041. 'audio^media|',
  16042. 'br^[HTMLElement]|clear',
  16043. 'base^[HTMLElement]|href,target',
  16044. 'body^[HTMLElement]|aLink,background,bgColor,link,*beforeunload,*blur,*error,*focus,*hashchange,*languagechange,*load,*message,*offline,*online,*pagehide,*pageshow,*popstate,*rejectionhandled,*resize,*scroll,*storage,*unhandledrejection,*unload,text,vLink',
  16045. 'button^[HTMLElement]|!autofocus,!disabled,formAction,formEnctype,formMethod,!formNoValidate,formTarget,name,type,value',
  16046. 'canvas^[HTMLElement]|#height,#width',
  16047. 'content^[HTMLElement]|select',
  16048. 'dl^[HTMLElement]|!compact',
  16049. 'datalist^[HTMLElement]|',
  16050. 'details^[HTMLElement]|!open',
  16051. 'dialog^[HTMLElement]|!open,returnValue',
  16052. 'dir^[HTMLElement]|!compact',
  16053. 'div^[HTMLElement]|align',
  16054. 'embed^[HTMLElement]|align,height,name,src,type,width',
  16055. 'fieldset^[HTMLElement]|!disabled,name',
  16056. 'font^[HTMLElement]|color,face,size',
  16057. 'form^[HTMLElement]|acceptCharset,action,autocomplete,encoding,enctype,method,name,!noValidate,target',
  16058. 'frame^[HTMLElement]|frameBorder,longDesc,marginHeight,marginWidth,name,!noResize,scrolling,src',
  16059. 'frameset^[HTMLElement]|cols,*beforeunload,*blur,*error,*focus,*hashchange,*languagechange,*load,*message,*offline,*online,*pagehide,*pageshow,*popstate,*rejectionhandled,*resize,*scroll,*storage,*unhandledrejection,*unload,rows',
  16060. 'hr^[HTMLElement]|align,color,!noShade,size,width',
  16061. 'head^[HTMLElement]|',
  16062. 'h1,h2,h3,h4,h5,h6^[HTMLElement]|align',
  16063. 'html^[HTMLElement]|version',
  16064. 'iframe^[HTMLElement]|align,!allowFullscreen,frameBorder,height,longDesc,marginHeight,marginWidth,name,referrerPolicy,%sandbox,scrolling,src,srcdoc,width',
  16065. 'img^[HTMLElement]|align,alt,border,%crossOrigin,#height,#hspace,!isMap,longDesc,lowsrc,name,referrerPolicy,sizes,src,srcset,useMap,#vspace,#width',
  16066. 'input^[HTMLElement]|accept,align,alt,autocapitalize,autocomplete,!autofocus,!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',
  16067. 'li^[HTMLElement]|type,#value',
  16068. 'label^[HTMLElement]|htmlFor',
  16069. 'legend^[HTMLElement]|align',
  16070. 'link^[HTMLElement]|as,charset,%crossOrigin,!disabled,href,hreflang,integrity,media,referrerPolicy,rel,%relList,rev,%sizes,target,type',
  16071. 'map^[HTMLElement]|name',
  16072. 'marquee^[HTMLElement]|behavior,bgColor,direction,height,#hspace,#loop,#scrollAmount,#scrollDelay,!trueSpeed,#vspace,width',
  16073. 'menu^[HTMLElement]|!compact',
  16074. 'meta^[HTMLElement]|content,httpEquiv,name,scheme',
  16075. 'meter^[HTMLElement]|#high,#low,#max,#min,#optimum,#value',
  16076. 'ins,del^[HTMLElement]|cite,dateTime',
  16077. 'ol^[HTMLElement]|!compact,!reversed,#start,type',
  16078. 'object^[HTMLElement]|align,archive,border,code,codeBase,codeType,data,!declare,height,#hspace,name,standby,type,useMap,#vspace,width',
  16079. 'optgroup^[HTMLElement]|!disabled,label',
  16080. 'option^[HTMLElement]|!defaultSelected,!disabled,label,!selected,text,value',
  16081. 'output^[HTMLElement]|defaultValue,%htmlFor,name,value',
  16082. 'p^[HTMLElement]|align',
  16083. 'param^[HTMLElement]|name,type,value,valueType',
  16084. 'picture^[HTMLElement]|',
  16085. 'pre^[HTMLElement]|#width',
  16086. 'progress^[HTMLElement]|#max,#value',
  16087. 'q,blockquote,cite^[HTMLElement]|',
  16088. 'script^[HTMLElement]|!async,charset,%crossOrigin,!defer,event,htmlFor,integrity,src,text,type',
  16089. 'select^[HTMLElement]|autocomplete,!autofocus,!disabled,#length,!multiple,name,!required,#selectedIndex,#size,value',
  16090. 'shadow^[HTMLElement]|',
  16091. 'slot^[HTMLElement]|name',
  16092. 'source^[HTMLElement]|media,sizes,src,srcset,type',
  16093. 'span^[HTMLElement]|',
  16094. 'style^[HTMLElement]|!disabled,media,type',
  16095. 'caption^[HTMLElement]|align',
  16096. 'th,td^[HTMLElement]|abbr,align,axis,bgColor,ch,chOff,#colSpan,headers,height,!noWrap,#rowSpan,scope,vAlign,width',
  16097. 'col,colgroup^[HTMLElement]|align,ch,chOff,#span,vAlign,width',
  16098. 'table^[HTMLElement]|align,bgColor,border,%caption,cellPadding,cellSpacing,frame,rules,summary,%tFoot,%tHead,width',
  16099. 'tr^[HTMLElement]|align,bgColor,ch,chOff,vAlign',
  16100. 'tfoot,thead,tbody^[HTMLElement]|align,ch,chOff,vAlign',
  16101. 'template^[HTMLElement]|',
  16102. 'textarea^[HTMLElement]|autocapitalize,autocomplete,!autofocus,#cols,defaultValue,dirName,!disabled,#maxLength,#minLength,name,placeholder,!readOnly,!required,#rows,selectionDirection,#selectionEnd,#selectionStart,value,wrap',
  16103. 'title^[HTMLElement]|text',
  16104. 'track^[HTMLElement]|!default,kind,label,src,srclang',
  16105. 'ul^[HTMLElement]|!compact,type',
  16106. 'unknown^[HTMLElement]|',
  16107. 'video^media|#height,poster,#width',
  16108. ':svg:a^:svg:graphics|',
  16109. ':svg:animate^:svg:animation|',
  16110. ':svg:animateMotion^:svg:animation|',
  16111. ':svg:animateTransform^:svg:animation|',
  16112. ':svg:circle^:svg:geometry|',
  16113. ':svg:clipPath^:svg:graphics|',
  16114. ':svg:defs^:svg:graphics|',
  16115. ':svg:desc^:svg:|',
  16116. ':svg:discard^:svg:|',
  16117. ':svg:ellipse^:svg:geometry|',
  16118. ':svg:feBlend^:svg:|',
  16119. ':svg:feColorMatrix^:svg:|',
  16120. ':svg:feComponentTransfer^:svg:|',
  16121. ':svg:feComposite^:svg:|',
  16122. ':svg:feConvolveMatrix^:svg:|',
  16123. ':svg:feDiffuseLighting^:svg:|',
  16124. ':svg:feDisplacementMap^:svg:|',
  16125. ':svg:feDistantLight^:svg:|',
  16126. ':svg:feDropShadow^:svg:|',
  16127. ':svg:feFlood^:svg:|',
  16128. ':svg:feFuncA^:svg:componentTransferFunction|',
  16129. ':svg:feFuncB^:svg:componentTransferFunction|',
  16130. ':svg:feFuncG^:svg:componentTransferFunction|',
  16131. ':svg:feFuncR^:svg:componentTransferFunction|',
  16132. ':svg:feGaussianBlur^:svg:|',
  16133. ':svg:feImage^:svg:|',
  16134. ':svg:feMerge^:svg:|',
  16135. ':svg:feMergeNode^:svg:|',
  16136. ':svg:feMorphology^:svg:|',
  16137. ':svg:feOffset^:svg:|',
  16138. ':svg:fePointLight^:svg:|',
  16139. ':svg:feSpecularLighting^:svg:|',
  16140. ':svg:feSpotLight^:svg:|',
  16141. ':svg:feTile^:svg:|',
  16142. ':svg:feTurbulence^:svg:|',
  16143. ':svg:filter^:svg:|',
  16144. ':svg:foreignObject^:svg:graphics|',
  16145. ':svg:g^:svg:graphics|',
  16146. ':svg:image^:svg:graphics|',
  16147. ':svg:line^:svg:geometry|',
  16148. ':svg:linearGradient^:svg:gradient|',
  16149. ':svg:mpath^:svg:|',
  16150. ':svg:marker^:svg:|',
  16151. ':svg:mask^:svg:|',
  16152. ':svg:metadata^:svg:|',
  16153. ':svg:path^:svg:geometry|',
  16154. ':svg:pattern^:svg:|',
  16155. ':svg:polygon^:svg:geometry|',
  16156. ':svg:polyline^:svg:geometry|',
  16157. ':svg:radialGradient^:svg:gradient|',
  16158. ':svg:rect^:svg:geometry|',
  16159. ':svg:svg^:svg:graphics|#currentScale,#zoomAndPan',
  16160. ':svg:script^:svg:|type',
  16161. ':svg:set^:svg:animation|',
  16162. ':svg:stop^:svg:|',
  16163. ':svg:style^:svg:|!disabled,media,title,type',
  16164. ':svg:switch^:svg:graphics|',
  16165. ':svg:symbol^:svg:|',
  16166. ':svg:tspan^:svg:textPositioning|',
  16167. ':svg:text^:svg:textPositioning|',
  16168. ':svg:textPath^:svg:textContent|',
  16169. ':svg:title^:svg:|',
  16170. ':svg:use^:svg:graphics|',
  16171. ':svg:view^:svg:|#zoomAndPan',
  16172. 'data^[HTMLElement]|value',
  16173. 'keygen^[HTMLElement]|!autofocus,challenge,!disabled,form,keytype,name',
  16174. 'menuitem^[HTMLElement]|type,label,icon,!disabled,!checked,radiogroup,!default',
  16175. 'summary^[HTMLElement]|',
  16176. 'time^[HTMLElement]|dateTime',
  16177. ':svg:cursor^:svg:|',
  16178. ];
  16179. const _ATTR_TO_PROP = {
  16180. 'class': 'className',
  16181. 'for': 'htmlFor',
  16182. 'formaction': 'formAction',
  16183. 'innerHtml': 'innerHTML',
  16184. 'readonly': 'readOnly',
  16185. 'tabindex': 'tabIndex',
  16186. };
  16187. // Invert _ATTR_TO_PROP.
  16188. const _PROP_TO_ATTR = Object.keys(_ATTR_TO_PROP).reduce((inverted, attr) => {
  16189. inverted[_ATTR_TO_PROP[attr]] = attr;
  16190. return inverted;
  16191. }, {});
  16192. class DomElementSchemaRegistry extends ElementSchemaRegistry {
  16193. constructor() {
  16194. super();
  16195. this._schema = {};
  16196. SCHEMA.forEach(encodedType => {
  16197. const type = {};
  16198. const [strType, strProperties] = encodedType.split('|');
  16199. const properties = strProperties.split(',');
  16200. const [typeNames, superName] = strType.split('^');
  16201. typeNames.split(',').forEach(tag => this._schema[tag.toLowerCase()] = type);
  16202. const superType = superName && this._schema[superName.toLowerCase()];
  16203. if (superType) {
  16204. Object.keys(superType).forEach((prop) => {
  16205. type[prop] = superType[prop];
  16206. });
  16207. }
  16208. properties.forEach((property) => {
  16209. if (property.length > 0) {
  16210. switch (property[0]) {
  16211. case '*':
  16212. // We don't yet support events.
  16213. // If ever allowing to bind to events, GO THROUGH A SECURITY REVIEW, allowing events
  16214. // will
  16215. // almost certainly introduce bad XSS vulnerabilities.
  16216. // type[property.substring(1)] = EVENT;
  16217. break;
  16218. case '!':
  16219. type[property.substring(1)] = BOOLEAN;
  16220. break;
  16221. case '#':
  16222. type[property.substring(1)] = NUMBER;
  16223. break;
  16224. case '%':
  16225. type[property.substring(1)] = OBJECT;
  16226. break;
  16227. default:
  16228. type[property] = STRING;
  16229. }
  16230. }
  16231. });
  16232. });
  16233. }
  16234. hasProperty(tagName, propName, schemaMetas) {
  16235. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  16236. return true;
  16237. }
  16238. if (tagName.indexOf('-') > -1) {
  16239. if (isNgContainer(tagName) || isNgContent(tagName)) {
  16240. return false;
  16241. }
  16242. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  16243. // Can't tell now as we don't know which properties a custom element will get
  16244. // once it is instantiated
  16245. return true;
  16246. }
  16247. }
  16248. const elementProperties = this._schema[tagName.toLowerCase()] || this._schema['unknown'];
  16249. return !!elementProperties[propName];
  16250. }
  16251. hasElement(tagName, schemaMetas) {
  16252. if (schemaMetas.some((schema) => schema.name === NO_ERRORS_SCHEMA.name)) {
  16253. return true;
  16254. }
  16255. if (tagName.indexOf('-') > -1) {
  16256. if (isNgContainer(tagName) || isNgContent(tagName)) {
  16257. return true;
  16258. }
  16259. if (schemaMetas.some((schema) => schema.name === CUSTOM_ELEMENTS_SCHEMA.name)) {
  16260. // Allow any custom elements
  16261. return true;
  16262. }
  16263. }
  16264. return !!this._schema[tagName.toLowerCase()];
  16265. }
  16266. /**
  16267. * securityContext returns the security context for the given property on the given DOM tag.
  16268. *
  16269. * Tag and property name are statically known and cannot change at runtime, i.e. it is not
  16270. * possible to bind a value into a changing attribute or tag name.
  16271. *
  16272. * The filtering is based on a list of allowed tags|attributes. All attributes in the schema
  16273. * above are assumed to have the 'NONE' security context, i.e. that they are safe inert
  16274. * string values. Only specific well known attack vectors are assigned their appropriate context.
  16275. */
  16276. securityContext(tagName, propName, isAttribute) {
  16277. if (isAttribute) {
  16278. // NB: For security purposes, use the mapped property name, not the attribute name.
  16279. propName = this.getMappedPropName(propName);
  16280. }
  16281. // Make sure comparisons are case insensitive, so that case differences between attribute and
  16282. // property names do not have a security impact.
  16283. tagName = tagName.toLowerCase();
  16284. propName = propName.toLowerCase();
  16285. let ctx = SECURITY_SCHEMA()[tagName + '|' + propName];
  16286. if (ctx) {
  16287. return ctx;
  16288. }
  16289. ctx = SECURITY_SCHEMA()['*|' + propName];
  16290. return ctx ? ctx : SecurityContext.NONE;
  16291. }
  16292. getMappedPropName(propName) {
  16293. return _ATTR_TO_PROP[propName] || propName;
  16294. }
  16295. getDefaultComponentElementName() {
  16296. return 'ng-component';
  16297. }
  16298. validateProperty(name) {
  16299. if (name.toLowerCase().startsWith('on')) {
  16300. const msg = `Binding to event property '${name}' is disallowed for security reasons, ` +
  16301. `please use (${name.slice(2)})=...` +
  16302. `\nIf '${name}' is a directive input, make sure the directive is imported by the` +
  16303. ` current module.`;
  16304. return { error: true, msg: msg };
  16305. }
  16306. else {
  16307. return { error: false };
  16308. }
  16309. }
  16310. validateAttribute(name) {
  16311. if (name.toLowerCase().startsWith('on')) {
  16312. const msg = `Binding to event attribute '${name}' is disallowed for security reasons, ` +
  16313. `please use (${name.slice(2)})=...`;
  16314. return { error: true, msg: msg };
  16315. }
  16316. else {
  16317. return { error: false };
  16318. }
  16319. }
  16320. allKnownElementNames() {
  16321. return Object.keys(this._schema);
  16322. }
  16323. allKnownAttributesOfElement(tagName) {
  16324. const elementProperties = this._schema[tagName.toLowerCase()] || this._schema['unknown'];
  16325. // Convert properties to attributes.
  16326. return Object.keys(elementProperties).map(prop => { var _a; return (_a = _PROP_TO_ATTR[prop]) !== null && _a !== void 0 ? _a : prop; });
  16327. }
  16328. normalizeAnimationStyleProperty(propName) {
  16329. return dashCaseToCamelCase(propName);
  16330. }
  16331. normalizeAnimationStyleValue(camelCaseProp, userProvidedProp, val) {
  16332. let unit = '';
  16333. const strVal = val.toString().trim();
  16334. let errorMsg = null;
  16335. if (_isPixelDimensionStyle(camelCaseProp) && val !== 0 && val !== '0') {
  16336. if (typeof val === 'number') {
  16337. unit = 'px';
  16338. }
  16339. else {
  16340. const valAndSuffixMatch = val.match(/^[+-]?[\d\.]+([a-z]*)$/);
  16341. if (valAndSuffixMatch && valAndSuffixMatch[1].length == 0) {
  16342. errorMsg = `Please provide a CSS unit value for ${userProvidedProp}:${val}`;
  16343. }
  16344. }
  16345. }
  16346. return { error: errorMsg, value: strVal + unit };
  16347. }
  16348. }
  16349. function _isPixelDimensionStyle(prop) {
  16350. switch (prop) {
  16351. case 'width':
  16352. case 'height':
  16353. case 'minWidth':
  16354. case 'minHeight':
  16355. case 'maxWidth':
  16356. case 'maxHeight':
  16357. case 'left':
  16358. case 'top':
  16359. case 'bottom':
  16360. case 'right':
  16361. case 'fontSize':
  16362. case 'outlineWidth':
  16363. case 'outlineOffset':
  16364. case 'paddingTop':
  16365. case 'paddingLeft':
  16366. case 'paddingBottom':
  16367. case 'paddingRight':
  16368. case 'marginTop':
  16369. case 'marginLeft':
  16370. case 'marginBottom':
  16371. case 'marginRight':
  16372. case 'borderRadius':
  16373. case 'borderWidth':
  16374. case 'borderTopWidth':
  16375. case 'borderLeftWidth':
  16376. case 'borderRightWidth':
  16377. case 'borderBottomWidth':
  16378. case 'textIndent':
  16379. return true;
  16380. default:
  16381. return false;
  16382. }
  16383. }
  16384. /**
  16385. * @license
  16386. * Copyright Google LLC All Rights Reserved.
  16387. *
  16388. * Use of this source code is governed by an MIT-style license that can be
  16389. * found in the LICENSE file at https://angular.io/license
  16390. */
  16391. /**
  16392. * Set of tagName|propertyName corresponding to Trusted Types sinks. Properties applying to all
  16393. * tags use '*'.
  16394. *
  16395. * Extracted from, and should be kept in sync with
  16396. * https://w3c.github.io/webappsec-trusted-types/dist/spec/#integrations
  16397. */
  16398. const TRUSTED_TYPES_SINKS = new Set([
  16399. // NOTE: All strings in this set *must* be lowercase!
  16400. // TrustedHTML
  16401. 'iframe|srcdoc',
  16402. '*|innerhtml',
  16403. '*|outerhtml',
  16404. // NB: no TrustedScript here, as the corresponding tags are stripped by the compiler.
  16405. // TrustedScriptURL
  16406. 'embed|src',
  16407. 'object|codebase',
  16408. 'object|data',
  16409. ]);
  16410. /**
  16411. * isTrustedTypesSink returns true if the given property on the given DOM tag is a Trusted Types
  16412. * sink. In that case, use `ElementSchemaRegistry.securityContext` to determine which particular
  16413. * Trusted Type is required for values passed to the sink:
  16414. * - SecurityContext.HTML corresponds to TrustedHTML
  16415. * - SecurityContext.RESOURCE_URL corresponds to TrustedScriptURL
  16416. */
  16417. function isTrustedTypesSink(tagName, propName) {
  16418. // Make sure comparisons are case insensitive, so that case differences between attribute and
  16419. // property names do not have a security impact.
  16420. tagName = tagName.toLowerCase();
  16421. propName = propName.toLowerCase();
  16422. return TRUSTED_TYPES_SINKS.has(tagName + '|' + propName) ||
  16423. TRUSTED_TYPES_SINKS.has('*|' + propName);
  16424. }
  16425. /**
  16426. * @license
  16427. * Copyright Google LLC All Rights Reserved.
  16428. *
  16429. * Use of this source code is governed by an MIT-style license that can be
  16430. * found in the LICENSE file at https://angular.io/license
  16431. */
  16432. const BIND_NAME_REGEXP$1 = /^(?:(bind-)|(let-)|(ref-|#)|(on-)|(bindon-)|(@))(.*)$/;
  16433. // Group 1 = "bind-"
  16434. const KW_BIND_IDX$1 = 1;
  16435. // Group 2 = "let-"
  16436. const KW_LET_IDX$1 = 2;
  16437. // Group 3 = "ref-/#"
  16438. const KW_REF_IDX$1 = 3;
  16439. // Group 4 = "on-"
  16440. const KW_ON_IDX$1 = 4;
  16441. // Group 5 = "bindon-"
  16442. const KW_BINDON_IDX$1 = 5;
  16443. // Group 6 = "@"
  16444. const KW_AT_IDX$1 = 6;
  16445. // Group 7 = the identifier after "bind-", "let-", "ref-/#", "on-", "bindon-" or "@"
  16446. const IDENT_KW_IDX$1 = 7;
  16447. const BINDING_DELIMS = {
  16448. BANANA_BOX: { start: '[(', end: ')]' },
  16449. PROPERTY: { start: '[', end: ']' },
  16450. EVENT: { start: '(', end: ')' },
  16451. };
  16452. const TEMPLATE_ATTR_PREFIX$2 = '*';
  16453. function htmlAstToRender3Ast(htmlNodes, bindingParser, options) {
  16454. const transformer = new HtmlAstToIvyAst(bindingParser, options);
  16455. const ivyNodes = visitAll$1(transformer, htmlNodes);
  16456. // Errors might originate in either the binding parser or the html to ivy transformer
  16457. const allErrors = bindingParser.errors.concat(transformer.errors);
  16458. const result = {
  16459. nodes: ivyNodes,
  16460. errors: allErrors,
  16461. styleUrls: transformer.styleUrls,
  16462. styles: transformer.styles,
  16463. ngContentSelectors: transformer.ngContentSelectors
  16464. };
  16465. if (options.collectCommentNodes) {
  16466. result.commentNodes = transformer.commentNodes;
  16467. }
  16468. return result;
  16469. }
  16470. class HtmlAstToIvyAst {
  16471. constructor(bindingParser, options) {
  16472. this.bindingParser = bindingParser;
  16473. this.options = options;
  16474. this.errors = [];
  16475. this.styles = [];
  16476. this.styleUrls = [];
  16477. this.ngContentSelectors = [];
  16478. // This array will be populated if `Render3ParseOptions['collectCommentNodes']` is true
  16479. this.commentNodes = [];
  16480. this.inI18nBlock = false;
  16481. }
  16482. // HTML visitor
  16483. visitElement(element) {
  16484. const isI18nRootElement = isI18nRootNode(element.i18n);
  16485. if (isI18nRootElement) {
  16486. if (this.inI18nBlock) {
  16487. this.reportError('Cannot mark an element as translatable inside of a translatable section. Please remove the nested i18n marker.', element.sourceSpan);
  16488. }
  16489. this.inI18nBlock = true;
  16490. }
  16491. const preparsedElement = preparseElement(element);
  16492. if (preparsedElement.type === PreparsedElementType.SCRIPT) {
  16493. return null;
  16494. }
  16495. else if (preparsedElement.type === PreparsedElementType.STYLE) {
  16496. const contents = textContents(element);
  16497. if (contents !== null) {
  16498. this.styles.push(contents);
  16499. }
  16500. return null;
  16501. }
  16502. else if (preparsedElement.type === PreparsedElementType.STYLESHEET &&
  16503. isStyleUrlResolvable(preparsedElement.hrefAttr)) {
  16504. this.styleUrls.push(preparsedElement.hrefAttr);
  16505. return null;
  16506. }
  16507. // Whether the element is a `<ng-template>`
  16508. const isTemplateElement = isNgTemplate(element.name);
  16509. const parsedProperties = [];
  16510. const boundEvents = [];
  16511. const variables = [];
  16512. const references = [];
  16513. const attributes = [];
  16514. const i18nAttrsMeta = {};
  16515. const templateParsedProperties = [];
  16516. const templateVariables = [];
  16517. // Whether the element has any *-attribute
  16518. let elementHasInlineTemplate = false;
  16519. for (const attribute of element.attrs) {
  16520. let hasBinding = false;
  16521. const normalizedName = normalizeAttributeName(attribute.name);
  16522. // `*attr` defines template bindings
  16523. let isTemplateBinding = false;
  16524. if (attribute.i18n) {
  16525. i18nAttrsMeta[attribute.name] = attribute.i18n;
  16526. }
  16527. if (normalizedName.startsWith(TEMPLATE_ATTR_PREFIX$2)) {
  16528. // *-attributes
  16529. if (elementHasInlineTemplate) {
  16530. this.reportError(`Can't have multiple template bindings on one element. Use only one attribute prefixed with *`, attribute.sourceSpan);
  16531. }
  16532. isTemplateBinding = true;
  16533. elementHasInlineTemplate = true;
  16534. const templateValue = attribute.value;
  16535. const templateKey = normalizedName.substring(TEMPLATE_ATTR_PREFIX$2.length);
  16536. const parsedVariables = [];
  16537. const absoluteValueOffset = attribute.valueSpan ?
  16538. attribute.valueSpan.start.offset :
  16539. // If there is no value span the attribute does not have a value, like `attr` in
  16540. //`<div attr></div>`. In this case, point to one character beyond the last character of
  16541. // the attribute name.
  16542. attribute.sourceSpan.start.offset + attribute.name.length;
  16543. this.bindingParser.parseInlineTemplateBinding(templateKey, templateValue, attribute.sourceSpan, absoluteValueOffset, [], templateParsedProperties, parsedVariables, true /* isIvyAst */);
  16544. templateVariables.push(...parsedVariables.map(v => new Variable(v.name, v.value, v.sourceSpan, v.keySpan, v.valueSpan)));
  16545. }
  16546. else {
  16547. // Check for variables, events, property bindings, interpolation
  16548. hasBinding = this.parseAttribute(isTemplateElement, attribute, [], parsedProperties, boundEvents, variables, references);
  16549. }
  16550. if (!hasBinding && !isTemplateBinding) {
  16551. // don't include the bindings as attributes as well in the AST
  16552. attributes.push(this.visitAttribute(attribute));
  16553. }
  16554. }
  16555. const children = visitAll$1(preparsedElement.nonBindable ? NON_BINDABLE_VISITOR$1 : this, element.children);
  16556. let parsedElement;
  16557. if (preparsedElement.type === PreparsedElementType.NG_CONTENT) {
  16558. // `<ng-content>`
  16559. if (element.children &&
  16560. !element.children.every((node) => isEmptyTextNode(node) || isCommentNode(node))) {
  16561. this.reportError(`<ng-content> element cannot have content.`, element.sourceSpan);
  16562. }
  16563. const selector = preparsedElement.selectAttr;
  16564. const attrs = element.attrs.map(attr => this.visitAttribute(attr));
  16565. parsedElement = new Content(selector, attrs, element.sourceSpan, element.i18n);
  16566. this.ngContentSelectors.push(selector);
  16567. }
  16568. else if (isTemplateElement) {
  16569. // `<ng-template>`
  16570. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  16571. parsedElement = new Template(element.name, attributes, attrs.bound, boundEvents, [ /* no template attributes */], children, references, variables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  16572. }
  16573. else {
  16574. const attrs = this.extractAttributes(element.name, parsedProperties, i18nAttrsMeta);
  16575. parsedElement = new Element(element.name, attributes, attrs.bound, boundEvents, children, references, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, element.i18n);
  16576. }
  16577. if (elementHasInlineTemplate) {
  16578. // If this node is an inline-template (e.g. has *ngFor) then we need to create a template
  16579. // node that contains this node.
  16580. // Moreover, if the node is an element, then we need to hoist its attributes to the template
  16581. // node for matching against content projection selectors.
  16582. const attrs = this.extractAttributes('ng-template', templateParsedProperties, i18nAttrsMeta);
  16583. const templateAttrs = [];
  16584. attrs.literal.forEach(attr => templateAttrs.push(attr));
  16585. attrs.bound.forEach(attr => templateAttrs.push(attr));
  16586. const hoistedAttrs = parsedElement instanceof Element ?
  16587. {
  16588. attributes: parsedElement.attributes,
  16589. inputs: parsedElement.inputs,
  16590. outputs: parsedElement.outputs,
  16591. } :
  16592. { attributes: [], inputs: [], outputs: [] };
  16593. // For <ng-template>s with structural directives on them, avoid passing i18n information to
  16594. // the wrapping template to prevent unnecessary i18n instructions from being generated. The
  16595. // necessary i18n meta information will be extracted from child elements.
  16596. const i18n = isTemplateElement && isI18nRootElement ? undefined : element.i18n;
  16597. // TODO(pk): test for this case
  16598. parsedElement = new Template(parsedElement.name, hoistedAttrs.attributes, hoistedAttrs.inputs, hoistedAttrs.outputs, templateAttrs, [parsedElement], [ /* no references */], templateVariables, element.sourceSpan, element.startSourceSpan, element.endSourceSpan, i18n);
  16599. }
  16600. if (isI18nRootElement) {
  16601. this.inI18nBlock = false;
  16602. }
  16603. return parsedElement;
  16604. }
  16605. visitAttribute(attribute) {
  16606. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  16607. }
  16608. visitText(text) {
  16609. return this._visitTextWithInterpolation(text.value, text.sourceSpan, text.i18n);
  16610. }
  16611. visitExpansion(expansion) {
  16612. if (!expansion.i18n) {
  16613. // do not generate Icu in case it was created
  16614. // outside of i18n block in a template
  16615. return null;
  16616. }
  16617. if (!isI18nRootNode(expansion.i18n)) {
  16618. throw new Error(`Invalid type "${expansion.i18n.constructor}" for "i18n" property of ${expansion.sourceSpan.toString()}. Expected a "Message"`);
  16619. }
  16620. const message = expansion.i18n;
  16621. const vars = {};
  16622. const placeholders = {};
  16623. // extract VARs from ICUs - we process them separately while
  16624. // assembling resulting message via goog.getMsg function, since
  16625. // we need to pass them to top-level goog.getMsg call
  16626. Object.keys(message.placeholders).forEach(key => {
  16627. const value = message.placeholders[key];
  16628. if (key.startsWith(I18N_ICU_VAR_PREFIX)) {
  16629. // Currently when the `plural` or `select` keywords in an ICU contain trailing spaces (e.g.
  16630. // `{count, select , ...}`), these spaces are also included into the key names in ICU vars
  16631. // (e.g. "VAR_SELECT "). These trailing spaces are not desirable, since they will later be
  16632. // converted into `_` symbols while normalizing placeholder names, which might lead to
  16633. // mismatches at runtime (i.e. placeholder will not be replaced with the correct value).
  16634. const formattedKey = key.trim();
  16635. const ast = this.bindingParser.parseInterpolationExpression(value.text, value.sourceSpan);
  16636. vars[formattedKey] = new BoundText(ast, value.sourceSpan);
  16637. }
  16638. else {
  16639. placeholders[key] = this._visitTextWithInterpolation(value.text, value.sourceSpan);
  16640. }
  16641. });
  16642. return new Icu(vars, placeholders, expansion.sourceSpan, message);
  16643. }
  16644. visitExpansionCase(expansionCase) {
  16645. return null;
  16646. }
  16647. visitComment(comment) {
  16648. if (this.options.collectCommentNodes) {
  16649. this.commentNodes.push(new Comment(comment.value || '', comment.sourceSpan));
  16650. }
  16651. return null;
  16652. }
  16653. // convert view engine `ParsedProperty` to a format suitable for IVY
  16654. extractAttributes(elementName, properties, i18nPropsMeta) {
  16655. const bound = [];
  16656. const literal = [];
  16657. properties.forEach(prop => {
  16658. const i18n = i18nPropsMeta[prop.name];
  16659. if (prop.isLiteral) {
  16660. literal.push(new TextAttribute(prop.name, prop.expression.source || '', prop.sourceSpan, prop.keySpan, prop.valueSpan, i18n));
  16661. }
  16662. else {
  16663. // Note that validation is skipped and property mapping is disabled
  16664. // due to the fact that we need to make sure a given prop is not an
  16665. // input of a directive and directive matching happens at runtime.
  16666. const bep = this.bindingParser.createBoundElementProperty(elementName, prop, /* skipValidation */ true, /* mapPropertyName */ false);
  16667. bound.push(BoundAttribute.fromBoundElementProperty(bep, i18n));
  16668. }
  16669. });
  16670. return { bound, literal };
  16671. }
  16672. parseAttribute(isTemplateElement, attribute, matchableAttributes, parsedProperties, boundEvents, variables, references) {
  16673. const name = normalizeAttributeName(attribute.name);
  16674. const value = attribute.value;
  16675. const srcSpan = attribute.sourceSpan;
  16676. const absoluteOffset = attribute.valueSpan ? attribute.valueSpan.start.offset : srcSpan.start.offset;
  16677. function createKeySpan(srcSpan, prefix, identifier) {
  16678. // We need to adjust the start location for the keySpan to account for the removed 'data-'
  16679. // prefix from `normalizeAttributeName`.
  16680. const normalizationAdjustment = attribute.name.length - name.length;
  16681. const keySpanStart = srcSpan.start.moveBy(prefix.length + normalizationAdjustment);
  16682. const keySpanEnd = keySpanStart.moveBy(identifier.length);
  16683. return new ParseSourceSpan(keySpanStart, keySpanEnd, keySpanStart, identifier);
  16684. }
  16685. const bindParts = name.match(BIND_NAME_REGEXP$1);
  16686. if (bindParts) {
  16687. if (bindParts[KW_BIND_IDX$1] != null) {
  16688. const identifier = bindParts[IDENT_KW_IDX$1];
  16689. const keySpan = createKeySpan(srcSpan, bindParts[KW_BIND_IDX$1], identifier);
  16690. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  16691. }
  16692. else if (bindParts[KW_LET_IDX$1]) {
  16693. if (isTemplateElement) {
  16694. const identifier = bindParts[IDENT_KW_IDX$1];
  16695. const keySpan = createKeySpan(srcSpan, bindParts[KW_LET_IDX$1], identifier);
  16696. this.parseVariable(identifier, value, srcSpan, keySpan, attribute.valueSpan, variables);
  16697. }
  16698. else {
  16699. this.reportError(`"let-" is only supported on ng-template elements.`, srcSpan);
  16700. }
  16701. }
  16702. else if (bindParts[KW_REF_IDX$1]) {
  16703. const identifier = bindParts[IDENT_KW_IDX$1];
  16704. const keySpan = createKeySpan(srcSpan, bindParts[KW_REF_IDX$1], identifier);
  16705. this.parseReference(identifier, value, srcSpan, keySpan, attribute.valueSpan, references);
  16706. }
  16707. else if (bindParts[KW_ON_IDX$1]) {
  16708. const events = [];
  16709. const identifier = bindParts[IDENT_KW_IDX$1];
  16710. const keySpan = createKeySpan(srcSpan, bindParts[KW_ON_IDX$1], identifier);
  16711. this.bindingParser.parseEvent(identifier, value, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  16712. addEvents(events, boundEvents);
  16713. }
  16714. else if (bindParts[KW_BINDON_IDX$1]) {
  16715. const identifier = bindParts[IDENT_KW_IDX$1];
  16716. const keySpan = createKeySpan(srcSpan, bindParts[KW_BINDON_IDX$1], identifier);
  16717. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  16718. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  16719. }
  16720. else if (bindParts[KW_AT_IDX$1]) {
  16721. const keySpan = createKeySpan(srcSpan, '', name);
  16722. this.bindingParser.parseLiteralAttr(name, value, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  16723. }
  16724. return true;
  16725. }
  16726. // We didn't see a kw-prefixed property binding, but we have not yet checked
  16727. // for the []/()/[()] syntax.
  16728. let delims = null;
  16729. if (name.startsWith(BINDING_DELIMS.BANANA_BOX.start)) {
  16730. delims = BINDING_DELIMS.BANANA_BOX;
  16731. }
  16732. else if (name.startsWith(BINDING_DELIMS.PROPERTY.start)) {
  16733. delims = BINDING_DELIMS.PROPERTY;
  16734. }
  16735. else if (name.startsWith(BINDING_DELIMS.EVENT.start)) {
  16736. delims = BINDING_DELIMS.EVENT;
  16737. }
  16738. if (delims !== null &&
  16739. // NOTE: older versions of the parser would match a start/end delimited
  16740. // binding iff the property name was terminated by the ending delimiter
  16741. // and the identifier in the binding was non-empty.
  16742. // TODO(ayazhafiz): update this to handle malformed bindings.
  16743. name.endsWith(delims.end) && name.length > delims.start.length + delims.end.length) {
  16744. const identifier = name.substring(delims.start.length, name.length - delims.end.length);
  16745. const keySpan = createKeySpan(srcSpan, delims.start, identifier);
  16746. if (delims.start === BINDING_DELIMS.BANANA_BOX.start) {
  16747. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  16748. this.parseAssignmentEvent(identifier, value, srcSpan, attribute.valueSpan, matchableAttributes, boundEvents, keySpan);
  16749. }
  16750. else if (delims.start === BINDING_DELIMS.PROPERTY.start) {
  16751. this.bindingParser.parsePropertyBinding(identifier, value, false, srcSpan, absoluteOffset, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  16752. }
  16753. else {
  16754. const events = [];
  16755. this.bindingParser.parseEvent(identifier, value, srcSpan, attribute.valueSpan || srcSpan, matchableAttributes, events, keySpan);
  16756. addEvents(events, boundEvents);
  16757. }
  16758. return true;
  16759. }
  16760. // No explicit binding found.
  16761. const keySpan = createKeySpan(srcSpan, '' /* prefix */, name);
  16762. const hasBinding = this.bindingParser.parsePropertyInterpolation(name, value, srcSpan, attribute.valueSpan, matchableAttributes, parsedProperties, keySpan);
  16763. return hasBinding;
  16764. }
  16765. _visitTextWithInterpolation(value, sourceSpan, i18n) {
  16766. const valueNoNgsp = replaceNgsp(value);
  16767. const expr = this.bindingParser.parseInterpolation(valueNoNgsp, sourceSpan);
  16768. return expr ? new BoundText(expr, sourceSpan, i18n) : new Text(valueNoNgsp, sourceSpan);
  16769. }
  16770. parseVariable(identifier, value, sourceSpan, keySpan, valueSpan, variables) {
  16771. if (identifier.indexOf('-') > -1) {
  16772. this.reportError(`"-" is not allowed in variable names`, sourceSpan);
  16773. }
  16774. else if (identifier.length === 0) {
  16775. this.reportError(`Variable does not have a name`, sourceSpan);
  16776. }
  16777. variables.push(new Variable(identifier, value, sourceSpan, keySpan, valueSpan));
  16778. }
  16779. parseReference(identifier, value, sourceSpan, keySpan, valueSpan, references) {
  16780. if (identifier.indexOf('-') > -1) {
  16781. this.reportError(`"-" is not allowed in reference names`, sourceSpan);
  16782. }
  16783. else if (identifier.length === 0) {
  16784. this.reportError(`Reference does not have a name`, sourceSpan);
  16785. }
  16786. else if (references.some(reference => reference.name === identifier)) {
  16787. this.reportError(`Reference "#${identifier}" is defined more than once`, sourceSpan);
  16788. }
  16789. references.push(new Reference(identifier, value, sourceSpan, keySpan, valueSpan));
  16790. }
  16791. parseAssignmentEvent(name, expression, sourceSpan, valueSpan, targetMatchableAttrs, boundEvents, keySpan) {
  16792. const events = [];
  16793. this.bindingParser.parseEvent(`${name}Change`, `${expression}=$event`, sourceSpan, valueSpan || sourceSpan, targetMatchableAttrs, events, keySpan);
  16794. addEvents(events, boundEvents);
  16795. }
  16796. reportError(message, sourceSpan, level = ParseErrorLevel.ERROR) {
  16797. this.errors.push(new ParseError(sourceSpan, message, level));
  16798. }
  16799. }
  16800. class NonBindableVisitor$1 {
  16801. visitElement(ast) {
  16802. const preparsedElement = preparseElement(ast);
  16803. if (preparsedElement.type === PreparsedElementType.SCRIPT ||
  16804. preparsedElement.type === PreparsedElementType.STYLE ||
  16805. preparsedElement.type === PreparsedElementType.STYLESHEET) {
  16806. // Skipping <script> for security reasons
  16807. // Skipping <style> and stylesheets as we already processed them
  16808. // in the StyleCompiler
  16809. return null;
  16810. }
  16811. const children = visitAll$1(this, ast.children, null);
  16812. return new Element(ast.name, visitAll$1(this, ast.attrs),
  16813. /* inputs */ [], /* outputs */ [], children, /* references */ [], ast.sourceSpan, ast.startSourceSpan, ast.endSourceSpan);
  16814. }
  16815. visitComment(comment) {
  16816. return null;
  16817. }
  16818. visitAttribute(attribute) {
  16819. return new TextAttribute(attribute.name, attribute.value, attribute.sourceSpan, attribute.keySpan, attribute.valueSpan, attribute.i18n);
  16820. }
  16821. visitText(text) {
  16822. return new Text(text.value, text.sourceSpan);
  16823. }
  16824. visitExpansion(expansion) {
  16825. return null;
  16826. }
  16827. visitExpansionCase(expansionCase) {
  16828. return null;
  16829. }
  16830. }
  16831. const NON_BINDABLE_VISITOR$1 = new NonBindableVisitor$1();
  16832. function normalizeAttributeName(attrName) {
  16833. return /^data-/i.test(attrName) ? attrName.substring(5) : attrName;
  16834. }
  16835. function addEvents(events, boundEvents) {
  16836. boundEvents.push(...events.map(e => BoundEvent.fromParsedEvent(e)));
  16837. }
  16838. function isEmptyTextNode(node) {
  16839. return node instanceof Text$3 && node.value.trim().length == 0;
  16840. }
  16841. function isCommentNode(node) {
  16842. return node instanceof Comment$1;
  16843. }
  16844. function textContents(node) {
  16845. if (node.children.length !== 1 || !(node.children[0] instanceof Text$3)) {
  16846. return null;
  16847. }
  16848. else {
  16849. return node.children[0].value;
  16850. }
  16851. }
  16852. /**
  16853. * @license
  16854. * Copyright Google LLC All Rights Reserved.
  16855. *
  16856. * Use of this source code is governed by an MIT-style license that can be
  16857. * found in the LICENSE file at https://angular.io/license
  16858. */
  16859. var TagType;
  16860. (function (TagType) {
  16861. TagType[TagType["ELEMENT"] = 0] = "ELEMENT";
  16862. TagType[TagType["TEMPLATE"] = 1] = "TEMPLATE";
  16863. })(TagType || (TagType = {}));
  16864. /**
  16865. * Generates an object that is used as a shared state between parent and all child contexts.
  16866. */
  16867. function setupRegistry() {
  16868. return { getUniqueId: getSeqNumberGenerator(), icus: new Map() };
  16869. }
  16870. /**
  16871. * I18nContext is a helper class which keeps track of all i18n-related aspects
  16872. * (accumulates placeholders, bindings, etc) between i18nStart and i18nEnd instructions.
  16873. *
  16874. * When we enter a nested template, the top-level context is being passed down
  16875. * to the nested component, which uses this context to generate a child instance
  16876. * of I18nContext class (to handle nested template) and at the end, reconciles it back
  16877. * with the parent context.
  16878. *
  16879. * @param index Instruction index of i18nStart, which initiates this context
  16880. * @param ref Reference to a translation const that represents the content if thus context
  16881. * @param level Nestng level defined for child contexts
  16882. * @param templateIndex Instruction index of a template which this context belongs to
  16883. * @param meta Meta information (id, meaning, description, etc) associated with this context
  16884. */
  16885. class I18nContext {
  16886. constructor(index, ref, level = 0, templateIndex = null, meta, registry) {
  16887. this.index = index;
  16888. this.ref = ref;
  16889. this.level = level;
  16890. this.templateIndex = templateIndex;
  16891. this.meta = meta;
  16892. this.registry = registry;
  16893. this.bindings = new Set();
  16894. this.placeholders = new Map();
  16895. this.isEmitted = false;
  16896. this._unresolvedCtxCount = 0;
  16897. this._registry = registry || setupRegistry();
  16898. this.id = this._registry.getUniqueId();
  16899. }
  16900. appendTag(type, node, index, closed) {
  16901. if (node.isVoid && closed) {
  16902. return; // ignore "close" for void tags
  16903. }
  16904. const ph = node.isVoid || !closed ? node.startName : node.closeName;
  16905. const content = { type, index, ctx: this.id, isVoid: node.isVoid, closed };
  16906. updatePlaceholderMap(this.placeholders, ph, content);
  16907. }
  16908. get icus() {
  16909. return this._registry.icus;
  16910. }
  16911. get isRoot() {
  16912. return this.level === 0;
  16913. }
  16914. get isResolved() {
  16915. return this._unresolvedCtxCount === 0;
  16916. }
  16917. getSerializedPlaceholders() {
  16918. const result = new Map();
  16919. this.placeholders.forEach((values, key) => result.set(key, values.map(serializePlaceholderValue)));
  16920. return result;
  16921. }
  16922. // public API to accumulate i18n-related content
  16923. appendBinding(binding) {
  16924. this.bindings.add(binding);
  16925. }
  16926. appendIcu(name, ref) {
  16927. updatePlaceholderMap(this._registry.icus, name, ref);
  16928. }
  16929. appendBoundText(node) {
  16930. const phs = assembleBoundTextPlaceholders(node, this.bindings.size, this.id);
  16931. phs.forEach((values, key) => updatePlaceholderMap(this.placeholders, key, ...values));
  16932. }
  16933. appendTemplate(node, index) {
  16934. // add open and close tags at the same time,
  16935. // since we process nested templates separately
  16936. this.appendTag(TagType.TEMPLATE, node, index, false);
  16937. this.appendTag(TagType.TEMPLATE, node, index, true);
  16938. this._unresolvedCtxCount++;
  16939. }
  16940. appendElement(node, index, closed) {
  16941. this.appendTag(TagType.ELEMENT, node, index, closed);
  16942. }
  16943. appendProjection(node, index) {
  16944. // Add open and close tags at the same time, since `<ng-content>` has no content,
  16945. // so when we come across `<ng-content>` we can register both open and close tags.
  16946. // Note: runtime i18n logic doesn't distinguish `<ng-content>` tag placeholders and
  16947. // regular element tag placeholders, so we generate element placeholders for both types.
  16948. this.appendTag(TagType.ELEMENT, node, index, false);
  16949. this.appendTag(TagType.ELEMENT, node, index, true);
  16950. }
  16951. /**
  16952. * Generates an instance of a child context based on the root one,
  16953. * when we enter a nested template within I18n section.
  16954. *
  16955. * @param index Instruction index of corresponding i18nStart, which initiates this context
  16956. * @param templateIndex Instruction index of a template which this context belongs to
  16957. * @param meta Meta information (id, meaning, description, etc) associated with this context
  16958. *
  16959. * @returns I18nContext instance
  16960. */
  16961. forkChildContext(index, templateIndex, meta) {
  16962. return new I18nContext(index, this.ref, this.level + 1, templateIndex, meta, this._registry);
  16963. }
  16964. /**
  16965. * Reconciles child context into parent one once the end of the i18n block is reached (i18nEnd).
  16966. *
  16967. * @param context Child I18nContext instance to be reconciled with parent context.
  16968. */
  16969. reconcileChildContext(context) {
  16970. // set the right context id for open and close
  16971. // template tags, so we can use it as sub-block ids
  16972. ['start', 'close'].forEach((op) => {
  16973. const key = context.meta[`${op}Name`];
  16974. const phs = this.placeholders.get(key) || [];
  16975. const tag = phs.find(findTemplateFn(this.id, context.templateIndex));
  16976. if (tag) {
  16977. tag.ctx = context.id;
  16978. }
  16979. });
  16980. // reconcile placeholders
  16981. const childPhs = context.placeholders;
  16982. childPhs.forEach((values, key) => {
  16983. const phs = this.placeholders.get(key);
  16984. if (!phs) {
  16985. this.placeholders.set(key, values);
  16986. return;
  16987. }
  16988. // try to find matching template...
  16989. const tmplIdx = phs.findIndex(findTemplateFn(context.id, context.templateIndex));
  16990. if (tmplIdx >= 0) {
  16991. // ... if found - replace it with nested template content
  16992. const isCloseTag = key.startsWith('CLOSE');
  16993. const isTemplateTag = key.endsWith('NG-TEMPLATE');
  16994. if (isTemplateTag) {
  16995. // current template's content is placed before or after
  16996. // parent template tag, depending on the open/close atrribute
  16997. phs.splice(tmplIdx + (isCloseTag ? 0 : 1), 0, ...values);
  16998. }
  16999. else {
  17000. const idx = isCloseTag ? values.length - 1 : 0;
  17001. values[idx].tmpl = phs[tmplIdx];
  17002. phs.splice(tmplIdx, 1, ...values);
  17003. }
  17004. }
  17005. else {
  17006. // ... otherwise just append content to placeholder value
  17007. phs.push(...values);
  17008. }
  17009. this.placeholders.set(key, phs);
  17010. });
  17011. this._unresolvedCtxCount--;
  17012. }
  17013. }
  17014. //
  17015. // Helper methods
  17016. //
  17017. function wrap(symbol, index, contextId, closed) {
  17018. const state = closed ? '/' : '';
  17019. return wrapI18nPlaceholder(`${state}${symbol}${index}`, contextId);
  17020. }
  17021. function wrapTag(symbol, { index, ctx, isVoid }, closed) {
  17022. return isVoid ? wrap(symbol, index, ctx) + wrap(symbol, index, ctx, true) :
  17023. wrap(symbol, index, ctx, closed);
  17024. }
  17025. function findTemplateFn(ctx, templateIndex) {
  17026. return (token) => typeof token === 'object' && token.type === TagType.TEMPLATE &&
  17027. token.index === templateIndex && token.ctx === ctx;
  17028. }
  17029. function serializePlaceholderValue(value) {
  17030. const element = (data, closed) => wrapTag('#', data, closed);
  17031. const template = (data, closed) => wrapTag('*', data, closed);
  17032. const projection = (data, closed) => wrapTag('!', data, closed);
  17033. switch (value.type) {
  17034. case TagType.ELEMENT:
  17035. // close element tag
  17036. if (value.closed) {
  17037. return element(value, true) + (value.tmpl ? template(value.tmpl, true) : '');
  17038. }
  17039. // open element tag that also initiates a template
  17040. if (value.tmpl) {
  17041. return template(value.tmpl) + element(value) +
  17042. (value.isVoid ? template(value.tmpl, true) : '');
  17043. }
  17044. return element(value);
  17045. case TagType.TEMPLATE:
  17046. return template(value, value.closed);
  17047. default:
  17048. return value;
  17049. }
  17050. }
  17051. /**
  17052. * @license
  17053. * Copyright Google LLC All Rights Reserved.
  17054. *
  17055. * Use of this source code is governed by an MIT-style license that can be
  17056. * found in the LICENSE file at https://angular.io/license
  17057. */
  17058. class IcuSerializerVisitor {
  17059. visitText(text) {
  17060. return text.value;
  17061. }
  17062. visitContainer(container) {
  17063. return container.children.map(child => child.visit(this)).join('');
  17064. }
  17065. visitIcu(icu) {
  17066. const strCases = Object.keys(icu.cases).map((k) => `${k} {${icu.cases[k].visit(this)}}`);
  17067. const result = `{${icu.expressionPlaceholder}, ${icu.type}, ${strCases.join(' ')}}`;
  17068. return result;
  17069. }
  17070. visitTagPlaceholder(ph) {
  17071. return ph.isVoid ?
  17072. this.formatPh(ph.startName) :
  17073. `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${this.formatPh(ph.closeName)}`;
  17074. }
  17075. visitPlaceholder(ph) {
  17076. return this.formatPh(ph.name);
  17077. }
  17078. visitIcuPlaceholder(ph, context) {
  17079. return this.formatPh(ph.name);
  17080. }
  17081. formatPh(value) {
  17082. return `{${formatI18nPlaceholderName(value, /* useCamelCase */ false)}}`;
  17083. }
  17084. }
  17085. const serializer = new IcuSerializerVisitor();
  17086. function serializeIcuNode(icu) {
  17087. return icu.visit(serializer);
  17088. }
  17089. /**
  17090. * @license
  17091. * Copyright Google LLC All Rights Reserved.
  17092. *
  17093. * Use of this source code is governed by an MIT-style license that can be
  17094. * found in the LICENSE file at https://angular.io/license
  17095. */
  17096. const TAG_TO_PLACEHOLDER_NAMES = {
  17097. 'A': 'LINK',
  17098. 'B': 'BOLD_TEXT',
  17099. 'BR': 'LINE_BREAK',
  17100. 'EM': 'EMPHASISED_TEXT',
  17101. 'H1': 'HEADING_LEVEL1',
  17102. 'H2': 'HEADING_LEVEL2',
  17103. 'H3': 'HEADING_LEVEL3',
  17104. 'H4': 'HEADING_LEVEL4',
  17105. 'H5': 'HEADING_LEVEL5',
  17106. 'H6': 'HEADING_LEVEL6',
  17107. 'HR': 'HORIZONTAL_RULE',
  17108. 'I': 'ITALIC_TEXT',
  17109. 'LI': 'LIST_ITEM',
  17110. 'LINK': 'MEDIA_LINK',
  17111. 'OL': 'ORDERED_LIST',
  17112. 'P': 'PARAGRAPH',
  17113. 'Q': 'QUOTATION',
  17114. 'S': 'STRIKETHROUGH_TEXT',
  17115. 'SMALL': 'SMALL_TEXT',
  17116. 'SUB': 'SUBSTRIPT',
  17117. 'SUP': 'SUPERSCRIPT',
  17118. 'TBODY': 'TABLE_BODY',
  17119. 'TD': 'TABLE_CELL',
  17120. 'TFOOT': 'TABLE_FOOTER',
  17121. 'TH': 'TABLE_HEADER_CELL',
  17122. 'THEAD': 'TABLE_HEADER',
  17123. 'TR': 'TABLE_ROW',
  17124. 'TT': 'MONOSPACED_TEXT',
  17125. 'U': 'UNDERLINED_TEXT',
  17126. 'UL': 'UNORDERED_LIST',
  17127. };
  17128. /**
  17129. * Creates unique names for placeholder with different content.
  17130. *
  17131. * Returns the same placeholder name when the content is identical.
  17132. */
  17133. class PlaceholderRegistry {
  17134. constructor() {
  17135. // Count the occurrence of the base name top generate a unique name
  17136. this._placeHolderNameCounts = {};
  17137. // Maps signature to placeholder names
  17138. this._signatureToName = {};
  17139. }
  17140. getStartTagPlaceholderName(tag, attrs, isVoid) {
  17141. const signature = this._hashTag(tag, attrs, isVoid);
  17142. if (this._signatureToName[signature]) {
  17143. return this._signatureToName[signature];
  17144. }
  17145. const upperTag = tag.toUpperCase();
  17146. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  17147. const name = this._generateUniqueName(isVoid ? baseName : `START_${baseName}`);
  17148. this._signatureToName[signature] = name;
  17149. return name;
  17150. }
  17151. getCloseTagPlaceholderName(tag) {
  17152. const signature = this._hashClosingTag(tag);
  17153. if (this._signatureToName[signature]) {
  17154. return this._signatureToName[signature];
  17155. }
  17156. const upperTag = tag.toUpperCase();
  17157. const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
  17158. const name = this._generateUniqueName(`CLOSE_${baseName}`);
  17159. this._signatureToName[signature] = name;
  17160. return name;
  17161. }
  17162. getPlaceholderName(name, content) {
  17163. const upperName = name.toUpperCase();
  17164. const signature = `PH: ${upperName}=${content}`;
  17165. if (this._signatureToName[signature]) {
  17166. return this._signatureToName[signature];
  17167. }
  17168. const uniqueName = this._generateUniqueName(upperName);
  17169. this._signatureToName[signature] = uniqueName;
  17170. return uniqueName;
  17171. }
  17172. getUniquePlaceholder(name) {
  17173. return this._generateUniqueName(name.toUpperCase());
  17174. }
  17175. // Generate a hash for a tag - does not take attribute order into account
  17176. _hashTag(tag, attrs, isVoid) {
  17177. const start = `<${tag}`;
  17178. const strAttrs = Object.keys(attrs).sort().map((name) => ` ${name}=${attrs[name]}`).join('');
  17179. const end = isVoid ? '/>' : `></${tag}>`;
  17180. return start + strAttrs + end;
  17181. }
  17182. _hashClosingTag(tag) {
  17183. return this._hashTag(`/${tag}`, {}, false);
  17184. }
  17185. _generateUniqueName(base) {
  17186. const seen = this._placeHolderNameCounts.hasOwnProperty(base);
  17187. if (!seen) {
  17188. this._placeHolderNameCounts[base] = 1;
  17189. return base;
  17190. }
  17191. const id = this._placeHolderNameCounts[base];
  17192. this._placeHolderNameCounts[base] = id + 1;
  17193. return `${base}_${id}`;
  17194. }
  17195. }
  17196. /**
  17197. * @license
  17198. * Copyright Google LLC All Rights Reserved.
  17199. *
  17200. * Use of this source code is governed by an MIT-style license that can be
  17201. * found in the LICENSE file at https://angular.io/license
  17202. */
  17203. const _expParser = new Parser$1(new Lexer());
  17204. /**
  17205. * Returns a function converting html nodes to an i18n Message given an interpolationConfig
  17206. */
  17207. function createI18nMessageFactory(interpolationConfig) {
  17208. const visitor = new _I18nVisitor(_expParser, interpolationConfig);
  17209. return (nodes, meaning, description, customId, visitNodeFn) => visitor.toI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  17210. }
  17211. function noopVisitNodeFn(_html, i18n) {
  17212. return i18n;
  17213. }
  17214. class _I18nVisitor {
  17215. constructor(_expressionParser, _interpolationConfig) {
  17216. this._expressionParser = _expressionParser;
  17217. this._interpolationConfig = _interpolationConfig;
  17218. }
  17219. toI18nMessage(nodes, meaning = '', description = '', customId = '', visitNodeFn) {
  17220. const context = {
  17221. isIcu: nodes.length == 1 && nodes[0] instanceof Expansion,
  17222. icuDepth: 0,
  17223. placeholderRegistry: new PlaceholderRegistry(),
  17224. placeholderToContent: {},
  17225. placeholderToMessage: {},
  17226. visitNodeFn: visitNodeFn || noopVisitNodeFn,
  17227. };
  17228. const i18nodes = visitAll$1(this, nodes, context);
  17229. return new Message(i18nodes, context.placeholderToContent, context.placeholderToMessage, meaning, description, customId);
  17230. }
  17231. visitElement(el, context) {
  17232. var _a;
  17233. const children = visitAll$1(this, el.children, context);
  17234. const attrs = {};
  17235. el.attrs.forEach(attr => {
  17236. // Do not visit the attributes, translatable ones are top-level ASTs
  17237. attrs[attr.name] = attr.value;
  17238. });
  17239. const isVoid = getHtmlTagDefinition(el.name).isVoid;
  17240. const startPhName = context.placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid);
  17241. context.placeholderToContent[startPhName] = {
  17242. text: el.startSourceSpan.toString(),
  17243. sourceSpan: el.startSourceSpan,
  17244. };
  17245. let closePhName = '';
  17246. if (!isVoid) {
  17247. closePhName = context.placeholderRegistry.getCloseTagPlaceholderName(el.name);
  17248. context.placeholderToContent[closePhName] = {
  17249. text: `</${el.name}>`,
  17250. sourceSpan: (_a = el.endSourceSpan) !== null && _a !== void 0 ? _a : el.sourceSpan,
  17251. };
  17252. }
  17253. const node = new TagPlaceholder(el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  17254. return context.visitNodeFn(el, node);
  17255. }
  17256. visitAttribute(attribute, context) {
  17257. const node = this._visitTextWithInterpolation(attribute.value, attribute.valueSpan || attribute.sourceSpan, context, attribute.i18n);
  17258. return context.visitNodeFn(attribute, node);
  17259. }
  17260. visitText(text, context) {
  17261. const node = this._visitTextWithInterpolation(text.value, text.sourceSpan, context, text.i18n);
  17262. return context.visitNodeFn(text, node);
  17263. }
  17264. visitComment(comment, context) {
  17265. return null;
  17266. }
  17267. visitExpansion(icu, context) {
  17268. context.icuDepth++;
  17269. const i18nIcuCases = {};
  17270. const i18nIcu = new Icu$1(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
  17271. icu.cases.forEach((caze) => {
  17272. i18nIcuCases[caze.value] = new Container(caze.expression.map((node) => node.visit(this, context)), caze.expSourceSpan);
  17273. });
  17274. context.icuDepth--;
  17275. if (context.isIcu || context.icuDepth > 0) {
  17276. // Returns an ICU node when:
  17277. // - the message (vs a part of the message) is an ICU message, or
  17278. // - the ICU message is nested.
  17279. const expPh = context.placeholderRegistry.getUniquePlaceholder(`VAR_${icu.type}`);
  17280. i18nIcu.expressionPlaceholder = expPh;
  17281. context.placeholderToContent[expPh] = {
  17282. text: icu.switchValue,
  17283. sourceSpan: icu.switchValueSourceSpan,
  17284. };
  17285. return context.visitNodeFn(icu, i18nIcu);
  17286. }
  17287. // Else returns a placeholder
  17288. // ICU placeholders should not be replaced with their original content but with the their
  17289. // translations.
  17290. // TODO(vicb): add a html.Node -> i18n.Message cache to avoid having to re-create the msg
  17291. const phName = context.placeholderRegistry.getPlaceholderName('ICU', icu.sourceSpan.toString());
  17292. context.placeholderToMessage[phName] = this.toI18nMessage([icu], '', '', '', undefined);
  17293. const node = new IcuPlaceholder(i18nIcu, phName, icu.sourceSpan);
  17294. return context.visitNodeFn(icu, node);
  17295. }
  17296. visitExpansionCase(_icuCase, _context) {
  17297. throw new Error('Unreachable code');
  17298. }
  17299. /**
  17300. * Split the, potentially interpolated, text up into text and placeholder pieces.
  17301. *
  17302. * @param text The potentially interpolated string to be split.
  17303. * @param sourceSpan The span of the whole of the `text` string.
  17304. * @param context The current context of the visitor, used to compute and store placeholders.
  17305. * @param previousI18n Any i18n metadata associated with this `text` from a previous pass.
  17306. */
  17307. _visitTextWithInterpolation(text, sourceSpan, context, previousI18n) {
  17308. const { strings, expressions } = this._expressionParser.splitInterpolation(text, sourceSpan.start.toString(), this._interpolationConfig);
  17309. // No expressions, return a single text.
  17310. if (expressions.length === 0) {
  17311. return new Text$1(text, sourceSpan);
  17312. }
  17313. // Return a sequence of `Text` and `Placeholder` nodes grouped in a `Container`.
  17314. const nodes = [];
  17315. for (let i = 0; i < strings.length - 1; i++) {
  17316. this._addText(nodes, strings[i], sourceSpan);
  17317. this._addPlaceholder(nodes, context, expressions[i], sourceSpan);
  17318. }
  17319. // The last index contains no expression
  17320. this._addText(nodes, strings[strings.length - 1], sourceSpan);
  17321. // Whitespace removal may have invalidated the interpolation source-spans.
  17322. reusePreviousSourceSpans(nodes, previousI18n);
  17323. return new Container(nodes, sourceSpan);
  17324. }
  17325. /**
  17326. * Create a new `Text` node from the `textPiece` and add it to the `nodes` collection.
  17327. *
  17328. * @param nodes The nodes to which the created `Text` node should be added.
  17329. * @param textPiece The text and relative span information for this `Text` node.
  17330. * @param interpolationSpan The span of the whole interpolated text.
  17331. */
  17332. _addText(nodes, textPiece, interpolationSpan) {
  17333. if (textPiece.text.length > 0) {
  17334. // No need to add empty strings
  17335. const stringSpan = getOffsetSourceSpan(interpolationSpan, textPiece);
  17336. nodes.push(new Text$1(textPiece.text, stringSpan));
  17337. }
  17338. }
  17339. /**
  17340. * Create a new `Placeholder` node from the `expression` and add it to the `nodes` collection.
  17341. *
  17342. * @param nodes The nodes to which the created `Text` node should be added.
  17343. * @param context The current context of the visitor, used to compute and store placeholders.
  17344. * @param expression The expression text and relative span information for this `Placeholder`
  17345. * node.
  17346. * @param interpolationSpan The span of the whole interpolated text.
  17347. */
  17348. _addPlaceholder(nodes, context, expression, interpolationSpan) {
  17349. const sourceSpan = getOffsetSourceSpan(interpolationSpan, expression);
  17350. const baseName = extractPlaceholderName(expression.text) || 'INTERPOLATION';
  17351. const phName = context.placeholderRegistry.getPlaceholderName(baseName, expression.text);
  17352. const text = this._interpolationConfig.start + expression.text + this._interpolationConfig.end;
  17353. context.placeholderToContent[phName] = { text, sourceSpan };
  17354. nodes.push(new Placeholder(expression.text, phName, sourceSpan));
  17355. }
  17356. }
  17357. /**
  17358. * Re-use the source-spans from `previousI18n` metadata for the `nodes`.
  17359. *
  17360. * Whitespace removal can invalidate the source-spans of interpolation nodes, so we
  17361. * reuse the source-span stored from a previous pass before the whitespace was removed.
  17362. *
  17363. * @param nodes The `Text` and `Placeholder` nodes to be processed.
  17364. * @param previousI18n Any i18n metadata for these `nodes` stored from a previous pass.
  17365. */
  17366. function reusePreviousSourceSpans(nodes, previousI18n) {
  17367. if (previousI18n instanceof Message) {
  17368. // The `previousI18n` is an i18n `Message`, so we are processing an `Attribute` with i18n
  17369. // metadata. The `Message` should consist only of a single `Container` that contains the
  17370. // parts (`Text` and `Placeholder`) to process.
  17371. assertSingleContainerMessage(previousI18n);
  17372. previousI18n = previousI18n.nodes[0];
  17373. }
  17374. if (previousI18n instanceof Container) {
  17375. // The `previousI18n` is a `Container`, which means that this is a second i18n extraction pass
  17376. // after whitespace has been removed from the AST ndoes.
  17377. assertEquivalentNodes(previousI18n.children, nodes);
  17378. // Reuse the source-spans from the first pass.
  17379. for (let i = 0; i < nodes.length; i++) {
  17380. nodes[i].sourceSpan = previousI18n.children[i].sourceSpan;
  17381. }
  17382. }
  17383. }
  17384. /**
  17385. * Asserts that the `message` contains exactly one `Container` node.
  17386. */
  17387. function assertSingleContainerMessage(message) {
  17388. const nodes = message.nodes;
  17389. if (nodes.length !== 1 || !(nodes[0] instanceof Container)) {
  17390. throw new Error('Unexpected previous i18n message - expected it to consist of only a single `Container` node.');
  17391. }
  17392. }
  17393. /**
  17394. * Asserts that the `previousNodes` and `node` collections have the same number of elements and
  17395. * corresponding elements have the same node type.
  17396. */
  17397. function assertEquivalentNodes(previousNodes, nodes) {
  17398. if (previousNodes.length !== nodes.length) {
  17399. throw new Error('The number of i18n message children changed between first and second pass.');
  17400. }
  17401. if (previousNodes.some((node, i) => nodes[i].constructor !== node.constructor)) {
  17402. throw new Error('The types of the i18n message children changed between first and second pass.');
  17403. }
  17404. }
  17405. /**
  17406. * Create a new `ParseSourceSpan` from the `sourceSpan`, offset by the `start` and `end` values.
  17407. */
  17408. function getOffsetSourceSpan(sourceSpan, { start, end }) {
  17409. return new ParseSourceSpan(sourceSpan.fullStart.moveBy(start), sourceSpan.fullStart.moveBy(end));
  17410. }
  17411. const _CUSTOM_PH_EXP = /\/\/[\s\S]*i18n[\s\S]*\([\s\S]*ph[\s\S]*=[\s\S]*("|')([\s\S]*?)\1[\s\S]*\)/g;
  17412. function extractPlaceholderName(input) {
  17413. return input.split(_CUSTOM_PH_EXP)[2];
  17414. }
  17415. /**
  17416. * @license
  17417. * Copyright Google LLC All Rights Reserved.
  17418. *
  17419. * Use of this source code is governed by an MIT-style license that can be
  17420. * found in the LICENSE file at https://angular.io/license
  17421. */
  17422. /**
  17423. * An i18n error.
  17424. */
  17425. class I18nError extends ParseError {
  17426. constructor(span, msg) {
  17427. super(span, msg);
  17428. }
  17429. }
  17430. /**
  17431. * @license
  17432. * Copyright Google LLC All Rights Reserved.
  17433. *
  17434. * Use of this source code is governed by an MIT-style license that can be
  17435. * found in the LICENSE file at https://angular.io/license
  17436. */
  17437. const setI18nRefs = (htmlNode, i18nNode) => {
  17438. if (htmlNode instanceof NodeWithI18n) {
  17439. if (i18nNode instanceof IcuPlaceholder && htmlNode.i18n instanceof Message) {
  17440. // This html node represents an ICU but this is a second processing pass, and the legacy id
  17441. // was computed in the previous pass and stored in the `i18n` property as a message.
  17442. // We are about to wipe out that property so capture the previous message to be reused when
  17443. // generating the message for this ICU later. See `_generateI18nMessage()`.
  17444. i18nNode.previousMessage = htmlNode.i18n;
  17445. }
  17446. htmlNode.i18n = i18nNode;
  17447. }
  17448. return i18nNode;
  17449. };
  17450. /**
  17451. * This visitor walks over HTML parse tree and converts information stored in
  17452. * i18n-related attributes ("i18n" and "i18n-*") into i18n meta object that is
  17453. * stored with other element's and attribute's information.
  17454. */
  17455. class I18nMetaVisitor {
  17456. constructor(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG, keepI18nAttrs = false, enableI18nLegacyMessageIdFormat = false) {
  17457. this.interpolationConfig = interpolationConfig;
  17458. this.keepI18nAttrs = keepI18nAttrs;
  17459. this.enableI18nLegacyMessageIdFormat = enableI18nLegacyMessageIdFormat;
  17460. // whether visited nodes contain i18n information
  17461. this.hasI18nMeta = false;
  17462. this._errors = [];
  17463. // i18n message generation factory
  17464. this._createI18nMessage = createI18nMessageFactory(this.interpolationConfig);
  17465. }
  17466. _generateI18nMessage(nodes, meta = '', visitNodeFn) {
  17467. const { meaning, description, customId } = this._parseMetadata(meta);
  17468. const message = this._createI18nMessage(nodes, meaning, description, customId, visitNodeFn);
  17469. this._setMessageId(message, meta);
  17470. this._setLegacyIds(message, meta);
  17471. return message;
  17472. }
  17473. visitAllWithErrors(nodes) {
  17474. const result = nodes.map(node => node.visit(this, null));
  17475. return new ParseTreeResult(result, this._errors);
  17476. }
  17477. visitElement(element) {
  17478. if (hasI18nAttrs(element)) {
  17479. this.hasI18nMeta = true;
  17480. const attrs = [];
  17481. const attrsMeta = {};
  17482. for (const attr of element.attrs) {
  17483. if (attr.name === I18N_ATTR) {
  17484. // root 'i18n' node attribute
  17485. const i18n = element.i18n || attr.value;
  17486. const message = this._generateI18nMessage(element.children, i18n, setI18nRefs);
  17487. // do not assign empty i18n meta
  17488. if (message.nodes.length) {
  17489. element.i18n = message;
  17490. }
  17491. }
  17492. else if (attr.name.startsWith(I18N_ATTR_PREFIX)) {
  17493. // 'i18n-*' attributes
  17494. const name = attr.name.slice(I18N_ATTR_PREFIX.length);
  17495. if (isTrustedTypesSink(element.name, name)) {
  17496. this._reportError(attr, `Translating attribute '${name}' is disallowed for security reasons.`);
  17497. }
  17498. else {
  17499. attrsMeta[name] = attr.value;
  17500. }
  17501. }
  17502. else {
  17503. // non-i18n attributes
  17504. attrs.push(attr);
  17505. }
  17506. }
  17507. // set i18n meta for attributes
  17508. if (Object.keys(attrsMeta).length) {
  17509. for (const attr of attrs) {
  17510. const meta = attrsMeta[attr.name];
  17511. // do not create translation for empty attributes
  17512. if (meta !== undefined && attr.value) {
  17513. attr.i18n = this._generateI18nMessage([attr], attr.i18n || meta);
  17514. }
  17515. }
  17516. }
  17517. if (!this.keepI18nAttrs) {
  17518. // update element's attributes,
  17519. // keeping only non-i18n related ones
  17520. element.attrs = attrs;
  17521. }
  17522. }
  17523. visitAll$1(this, element.children, element.i18n);
  17524. return element;
  17525. }
  17526. visitExpansion(expansion, currentMessage) {
  17527. let message;
  17528. const meta = expansion.i18n;
  17529. this.hasI18nMeta = true;
  17530. if (meta instanceof IcuPlaceholder) {
  17531. // set ICU placeholder name (e.g. "ICU_1"),
  17532. // generated while processing root element contents,
  17533. // so we can reference it when we output translation
  17534. const name = meta.name;
  17535. message = this._generateI18nMessage([expansion], meta);
  17536. const icu = icuFromI18nMessage(message);
  17537. icu.name = name;
  17538. }
  17539. else {
  17540. // ICU is a top level message, try to use metadata from container element if provided via
  17541. // `context` argument. Note: context may not be available for standalone ICUs (without
  17542. // wrapping element), so fallback to ICU metadata in this case.
  17543. message = this._generateI18nMessage([expansion], currentMessage || meta);
  17544. }
  17545. expansion.i18n = message;
  17546. return expansion;
  17547. }
  17548. visitText(text) {
  17549. return text;
  17550. }
  17551. visitAttribute(attribute) {
  17552. return attribute;
  17553. }
  17554. visitComment(comment) {
  17555. return comment;
  17556. }
  17557. visitExpansionCase(expansionCase) {
  17558. return expansionCase;
  17559. }
  17560. /**
  17561. * Parse the general form `meta` passed into extract the explicit metadata needed to create a
  17562. * `Message`.
  17563. *
  17564. * There are three possibilities for the `meta` variable
  17565. * 1) a string from an `i18n` template attribute: parse it to extract the metadata values.
  17566. * 2) a `Message` from a previous processing pass: reuse the metadata values in the message.
  17567. * 4) other: ignore this and just process the message metadata as normal
  17568. *
  17569. * @param meta the bucket that holds information about the message
  17570. * @returns the parsed metadata.
  17571. */
  17572. _parseMetadata(meta) {
  17573. return typeof meta === 'string' ? parseI18nMeta(meta) :
  17574. meta instanceof Message ? meta : {};
  17575. }
  17576. /**
  17577. * Generate (or restore) message id if not specified already.
  17578. */
  17579. _setMessageId(message, meta) {
  17580. if (!message.id) {
  17581. message.id = meta instanceof Message && meta.id || decimalDigest(message);
  17582. }
  17583. }
  17584. /**
  17585. * Update the `message` with a `legacyId` if necessary.
  17586. *
  17587. * @param message the message whose legacy id should be set
  17588. * @param meta information about the message being processed
  17589. */
  17590. _setLegacyIds(message, meta) {
  17591. if (this.enableI18nLegacyMessageIdFormat) {
  17592. message.legacyIds = [computeDigest(message), computeDecimalDigest(message)];
  17593. }
  17594. else if (typeof meta !== 'string') {
  17595. // This occurs if we are doing the 2nd pass after whitespace removal (see `parseTemplate()` in
  17596. // `packages/compiler/src/render3/view/template.ts`).
  17597. // In that case we want to reuse the legacy message generated in the 1st pass (see
  17598. // `setI18nRefs()`).
  17599. const previousMessage = meta instanceof Message ?
  17600. meta :
  17601. meta instanceof IcuPlaceholder ? meta.previousMessage : undefined;
  17602. message.legacyIds = previousMessage ? previousMessage.legacyIds : [];
  17603. }
  17604. }
  17605. _reportError(node, msg) {
  17606. this._errors.push(new I18nError(node.sourceSpan, msg));
  17607. }
  17608. }
  17609. /** I18n separators for metadata **/
  17610. const I18N_MEANING_SEPARATOR = '|';
  17611. const I18N_ID_SEPARATOR = '@@';
  17612. /**
  17613. * Parses i18n metas like:
  17614. * - "@@id",
  17615. * - "description[@@id]",
  17616. * - "meaning|description[@@id]"
  17617. * and returns an object with parsed output.
  17618. *
  17619. * @param meta String that represents i18n meta
  17620. * @returns Object with id, meaning and description fields
  17621. */
  17622. function parseI18nMeta(meta = '') {
  17623. let customId;
  17624. let meaning;
  17625. let description;
  17626. meta = meta.trim();
  17627. if (meta) {
  17628. const idIndex = meta.indexOf(I18N_ID_SEPARATOR);
  17629. const descIndex = meta.indexOf(I18N_MEANING_SEPARATOR);
  17630. let meaningAndDesc;
  17631. [meaningAndDesc, customId] =
  17632. (idIndex > -1) ? [meta.slice(0, idIndex), meta.slice(idIndex + 2)] : [meta, ''];
  17633. [meaning, description] = (descIndex > -1) ?
  17634. [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)] :
  17635. ['', meaningAndDesc];
  17636. }
  17637. return { customId, meaning, description };
  17638. }
  17639. // Converts i18n meta information for a message (id, description, meaning)
  17640. // to a JsDoc statement formatted as expected by the Closure compiler.
  17641. function i18nMetaToJSDoc(meta) {
  17642. const tags = [];
  17643. if (meta.description) {
  17644. tags.push({ tagName: "desc" /* Desc */, text: meta.description });
  17645. }
  17646. if (meta.meaning) {
  17647. tags.push({ tagName: "meaning" /* Meaning */, text: meta.meaning });
  17648. }
  17649. return tags.length == 0 ? null : jsDocComment(tags);
  17650. }
  17651. /** Closure uses `goog.getMsg(message)` to lookup translations */
  17652. const GOOG_GET_MSG = 'goog.getMsg';
  17653. function createGoogleGetMsgStatements(variable$1, message, closureVar, params) {
  17654. const messageString = serializeI18nMessageForGetMsg(message);
  17655. const args = [literal(messageString)];
  17656. if (Object.keys(params).length) {
  17657. args.push(mapLiteral(params, true));
  17658. }
  17659. // /**
  17660. // * @desc description of message
  17661. // * @meaning meaning of message
  17662. // */
  17663. // const MSG_... = goog.getMsg(..);
  17664. // I18N_X = MSG_...;
  17665. const googGetMsgStmt = closureVar.set(variable(GOOG_GET_MSG).callFn(args)).toConstDecl();
  17666. const metaComment = i18nMetaToJSDoc(message);
  17667. if (metaComment !== null) {
  17668. googGetMsgStmt.addLeadingComment(metaComment);
  17669. }
  17670. const i18nAssignmentStmt = new ExpressionStatement(variable$1.set(closureVar));
  17671. return [googGetMsgStmt, i18nAssignmentStmt];
  17672. }
  17673. /**
  17674. * This visitor walks over i18n tree and generates its string representation, including ICUs and
  17675. * placeholders in `{$placeholder}` (for plain messages) or `{PLACEHOLDER}` (inside ICUs) format.
  17676. */
  17677. class GetMsgSerializerVisitor {
  17678. formatPh(value) {
  17679. return `{$${formatI18nPlaceholderName(value)}}`;
  17680. }
  17681. visitText(text) {
  17682. return text.value;
  17683. }
  17684. visitContainer(container) {
  17685. return container.children.map(child => child.visit(this)).join('');
  17686. }
  17687. visitIcu(icu) {
  17688. return serializeIcuNode(icu);
  17689. }
  17690. visitTagPlaceholder(ph) {
  17691. return ph.isVoid ?
  17692. this.formatPh(ph.startName) :
  17693. `${this.formatPh(ph.startName)}${ph.children.map(child => child.visit(this)).join('')}${this.formatPh(ph.closeName)}`;
  17694. }
  17695. visitPlaceholder(ph) {
  17696. return this.formatPh(ph.name);
  17697. }
  17698. visitIcuPlaceholder(ph, context) {
  17699. return this.formatPh(ph.name);
  17700. }
  17701. }
  17702. const serializerVisitor$1 = new GetMsgSerializerVisitor();
  17703. function serializeI18nMessageForGetMsg(message) {
  17704. return message.nodes.map(node => node.visit(serializerVisitor$1, null)).join('');
  17705. }
  17706. function createLocalizeStatements(variable, message, params) {
  17707. const { messageParts, placeHolders } = serializeI18nMessageForLocalize(message);
  17708. const sourceSpan = getSourceSpan(message);
  17709. const expressions = placeHolders.map(ph => params[ph.text]);
  17710. const localizedString$1 = localizedString(message, messageParts, placeHolders, expressions, sourceSpan);
  17711. const variableInitialization = variable.set(localizedString$1);
  17712. return [new ExpressionStatement(variableInitialization)];
  17713. }
  17714. /**
  17715. * This visitor walks over an i18n tree, capturing literal strings and placeholders.
  17716. *
  17717. * The result can be used for generating the `$localize` tagged template literals.
  17718. */
  17719. class LocalizeSerializerVisitor {
  17720. visitText(text, context) {
  17721. if (context[context.length - 1] instanceof LiteralPiece) {
  17722. // Two literal pieces in a row means that there was some comment node in-between.
  17723. context[context.length - 1].text += text.value;
  17724. }
  17725. else {
  17726. context.push(new LiteralPiece(text.value, text.sourceSpan));
  17727. }
  17728. }
  17729. visitContainer(container, context) {
  17730. container.children.forEach(child => child.visit(this, context));
  17731. }
  17732. visitIcu(icu, context) {
  17733. context.push(new LiteralPiece(serializeIcuNode(icu), icu.sourceSpan));
  17734. }
  17735. visitTagPlaceholder(ph, context) {
  17736. var _a, _b;
  17737. context.push(this.createPlaceholderPiece(ph.startName, (_a = ph.startSourceSpan) !== null && _a !== void 0 ? _a : ph.sourceSpan));
  17738. if (!ph.isVoid) {
  17739. ph.children.forEach(child => child.visit(this, context));
  17740. context.push(this.createPlaceholderPiece(ph.closeName, (_b = ph.endSourceSpan) !== null && _b !== void 0 ? _b : ph.sourceSpan));
  17741. }
  17742. }
  17743. visitPlaceholder(ph, context) {
  17744. context.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan));
  17745. }
  17746. visitIcuPlaceholder(ph, context) {
  17747. context.push(this.createPlaceholderPiece(ph.name, ph.sourceSpan));
  17748. }
  17749. createPlaceholderPiece(name, sourceSpan) {
  17750. return new PlaceholderPiece(formatI18nPlaceholderName(name, /* useCamelCase */ false), sourceSpan);
  17751. }
  17752. }
  17753. const serializerVisitor$2 = new LocalizeSerializerVisitor();
  17754. /**
  17755. * Serialize an i18n message into two arrays: messageParts and placeholders.
  17756. *
  17757. * These arrays will be used to generate `$localize` tagged template literals.
  17758. *
  17759. * @param message The message to be serialized.
  17760. * @returns an object containing the messageParts and placeholders.
  17761. */
  17762. function serializeI18nMessageForLocalize(message) {
  17763. const pieces = [];
  17764. message.nodes.forEach(node => node.visit(serializerVisitor$2, pieces));
  17765. return processMessagePieces(pieces);
  17766. }
  17767. function getSourceSpan(message) {
  17768. const startNode = message.nodes[0];
  17769. const endNode = message.nodes[message.nodes.length - 1];
  17770. return new ParseSourceSpan(startNode.sourceSpan.start, endNode.sourceSpan.end, startNode.sourceSpan.fullStart, startNode.sourceSpan.details);
  17771. }
  17772. /**
  17773. * Convert the list of serialized MessagePieces into two arrays.
  17774. *
  17775. * One contains the literal string pieces and the other the placeholders that will be replaced by
  17776. * expressions when rendering `$localize` tagged template literals.
  17777. *
  17778. * @param pieces The pieces to process.
  17779. * @returns an object containing the messageParts and placeholders.
  17780. */
  17781. function processMessagePieces(pieces) {
  17782. const messageParts = [];
  17783. const placeHolders = [];
  17784. if (pieces[0] instanceof PlaceholderPiece) {
  17785. // The first piece was a placeholder so we need to add an initial empty message part.
  17786. messageParts.push(createEmptyMessagePart(pieces[0].sourceSpan.start));
  17787. }
  17788. for (let i = 0; i < pieces.length; i++) {
  17789. const part = pieces[i];
  17790. if (part instanceof LiteralPiece) {
  17791. messageParts.push(part);
  17792. }
  17793. else {
  17794. placeHolders.push(part);
  17795. if (pieces[i - 1] instanceof PlaceholderPiece) {
  17796. // There were two placeholders in a row, so we need to add an empty message part.
  17797. messageParts.push(createEmptyMessagePart(pieces[i - 1].sourceSpan.end));
  17798. }
  17799. }
  17800. }
  17801. if (pieces[pieces.length - 1] instanceof PlaceholderPiece) {
  17802. // The last piece was a placeholder so we need to add a final empty message part.
  17803. messageParts.push(createEmptyMessagePart(pieces[pieces.length - 1].sourceSpan.end));
  17804. }
  17805. return { messageParts, placeHolders };
  17806. }
  17807. function createEmptyMessagePart(location) {
  17808. return new LiteralPiece('', new ParseSourceSpan(location, location));
  17809. }
  17810. /**
  17811. * @license
  17812. * Copyright Google LLC All Rights Reserved.
  17813. *
  17814. * Use of this source code is governed by an MIT-style license that can be
  17815. * found in the LICENSE file at https://angular.io/license
  17816. */
  17817. // Selector attribute name of `<ng-content>`
  17818. const NG_CONTENT_SELECT_ATTR$1 = 'select';
  17819. // Attribute name of `ngProjectAs`.
  17820. const NG_PROJECT_AS_ATTR_NAME = 'ngProjectAs';
  17821. // Global symbols available only inside event bindings.
  17822. const EVENT_BINDING_SCOPE_GLOBALS = new Set(['$event']);
  17823. // List of supported global targets for event listeners
  17824. const GLOBAL_TARGET_RESOLVERS = new Map([['window', Identifiers.resolveWindow], ['document', Identifiers.resolveDocument], ['body', Identifiers.resolveBody]]);
  17825. const LEADING_TRIVIA_CHARS = [' ', '\n', '\r', '\t'];
  17826. // if (rf & flags) { .. }
  17827. function renderFlagCheckIfStmt(flags, statements) {
  17828. return ifStmt(variable(RENDER_FLAGS).bitwiseAnd(literal(flags), null, false), statements);
  17829. }
  17830. function prepareEventListenerParameters(eventAst, handlerName = null, scope = null) {
  17831. const { type, name, target, phase, handler } = eventAst;
  17832. if (target && !GLOBAL_TARGET_RESOLVERS.has(target)) {
  17833. throw new Error(`Unexpected global target '${target}' defined for '${name}' event.
  17834. Supported list of global targets: ${Array.from(GLOBAL_TARGET_RESOLVERS.keys())}.`);
  17835. }
  17836. const eventArgumentName = '$event';
  17837. const implicitReceiverAccesses = new Set();
  17838. const implicitReceiverExpr = (scope === null || scope.bindingLevel === 0) ?
  17839. variable(CONTEXT_NAME) :
  17840. scope.getOrCreateSharedContextVar(0);
  17841. const bindingExpr = convertActionBinding(scope, implicitReceiverExpr, handler, 'b', () => error('Unexpected interpolation'), eventAst.handlerSpan, implicitReceiverAccesses, EVENT_BINDING_SCOPE_GLOBALS);
  17842. const statements = [];
  17843. if (scope) {
  17844. // `variableDeclarations` needs to run first, because
  17845. // `restoreViewStatement` depends on the result.
  17846. statements.push(...scope.variableDeclarations());
  17847. statements.unshift(...scope.restoreViewStatement());
  17848. }
  17849. statements.push(...bindingExpr.render3Stmts);
  17850. const eventName = type === 1 /* Animation */ ? prepareSyntheticListenerName(name, phase) : name;
  17851. const fnName = handlerName && sanitizeIdentifier(handlerName);
  17852. const fnArgs = [];
  17853. if (implicitReceiverAccesses.has(eventArgumentName)) {
  17854. fnArgs.push(new FnParam(eventArgumentName, DYNAMIC_TYPE));
  17855. }
  17856. const handlerFn = fn(fnArgs, statements, INFERRED_TYPE, null, fnName);
  17857. const params = [literal(eventName), handlerFn];
  17858. if (target) {
  17859. params.push(literal(false), // `useCapture` flag, defaults to `false`
  17860. importExpr(GLOBAL_TARGET_RESOLVERS.get(target)));
  17861. }
  17862. return params;
  17863. }
  17864. function createComponentDefConsts() {
  17865. return {
  17866. prepareStatements: [],
  17867. constExpressions: [],
  17868. i18nVarRefsCache: new Map(),
  17869. };
  17870. }
  17871. class TemplateDefinitionBuilder {
  17872. constructor(constantPool, parentBindingScope, level = 0, contextName, i18nContext, templateIndex, templateName, directiveMatcher, directives, pipeTypeByName, pipes, _namespace, relativeContextFilePath, i18nUseExternalIds, _constants = createComponentDefConsts()) {
  17873. this.constantPool = constantPool;
  17874. this.level = level;
  17875. this.contextName = contextName;
  17876. this.i18nContext = i18nContext;
  17877. this.templateIndex = templateIndex;
  17878. this.templateName = templateName;
  17879. this.directiveMatcher = directiveMatcher;
  17880. this.directives = directives;
  17881. this.pipeTypeByName = pipeTypeByName;
  17882. this.pipes = pipes;
  17883. this._namespace = _namespace;
  17884. this.i18nUseExternalIds = i18nUseExternalIds;
  17885. this._constants = _constants;
  17886. this._dataIndex = 0;
  17887. this._bindingContext = 0;
  17888. this._prefixCode = [];
  17889. /**
  17890. * List of callbacks to generate creation mode instructions. We store them here as we process
  17891. * the template so bindings in listeners are resolved only once all nodes have been visited.
  17892. * This ensures all local refs and context variables are available for matching.
  17893. */
  17894. this._creationCodeFns = [];
  17895. /**
  17896. * List of callbacks to generate update mode instructions. We store them here as we process
  17897. * the template so bindings are resolved only once all nodes have been visited. This ensures
  17898. * all local refs and context variables are available for matching.
  17899. */
  17900. this._updateCodeFns = [];
  17901. /** Index of the currently-selected node. */
  17902. this._currentIndex = 0;
  17903. /** Temporary variable declarations generated from visiting pipes, literals, etc. */
  17904. this._tempVariables = [];
  17905. /**
  17906. * List of callbacks to build nested templates. Nested templates must not be visited until
  17907. * after the parent template has finished visiting all of its nodes. This ensures that all
  17908. * local ref bindings in nested templates are able to find local ref values if the refs
  17909. * are defined after the template declaration.
  17910. */
  17911. this._nestedTemplateFns = [];
  17912. this._unsupported = unsupported;
  17913. // i18n context local to this template
  17914. this.i18n = null;
  17915. // Number of slots to reserve for pureFunctions
  17916. this._pureFunctionSlots = 0;
  17917. // Number of binding slots
  17918. this._bindingSlots = 0;
  17919. // Projection slots found in the template. Projection slots can distribute projected
  17920. // nodes based on a selector, or can just use the wildcard selector to match
  17921. // all nodes which aren't matching any selector.
  17922. this._ngContentReservedSlots = [];
  17923. // Number of non-default selectors found in all parent templates of this template. We need to
  17924. // track it to properly adjust projection slot index in the `projection` instruction.
  17925. this._ngContentSelectorsOffset = 0;
  17926. // Expression that should be used as implicit receiver when converting template
  17927. // expressions to output AST.
  17928. this._implicitReceiverExpr = null;
  17929. // These should be handled in the template or element directly.
  17930. this.visitReference = invalid$1;
  17931. this.visitVariable = invalid$1;
  17932. this.visitTextAttribute = invalid$1;
  17933. this.visitBoundAttribute = invalid$1;
  17934. this.visitBoundEvent = invalid$1;
  17935. this._bindingScope = parentBindingScope.nestedScope(level);
  17936. // Turn the relative context file path into an identifier by replacing non-alphanumeric
  17937. // characters with underscores.
  17938. this.fileBasedI18nSuffix = relativeContextFilePath.replace(/[^A-Za-z0-9]/g, '_') + '_';
  17939. this._valueConverter = new ValueConverter(constantPool, () => this.allocateDataSlot(), (numSlots) => this.allocatePureFunctionSlots(numSlots), (name, localName, slot, value) => {
  17940. const pipeType = pipeTypeByName.get(name);
  17941. if (pipeType) {
  17942. this.pipes.add(pipeType);
  17943. }
  17944. this._bindingScope.set(this.level, localName, value);
  17945. this.creationInstruction(null, Identifiers.pipe, [literal(slot), literal(name)]);
  17946. });
  17947. }
  17948. buildTemplateFunction(nodes, variables, ngContentSelectorsOffset = 0, i18n) {
  17949. this._ngContentSelectorsOffset = ngContentSelectorsOffset;
  17950. if (this._namespace !== Identifiers.namespaceHTML) {
  17951. this.creationInstruction(null, this._namespace);
  17952. }
  17953. // Create variable bindings
  17954. variables.forEach(v => this.registerContextVariables(v));
  17955. // Initiate i18n context in case:
  17956. // - this template has parent i18n context
  17957. // - or the template has i18n meta associated with it,
  17958. // but it's not initiated by the Element (e.g. <ng-template i18n>)
  17959. const initI18nContext = this.i18nContext ||
  17960. (isI18nRootNode(i18n) && !isSingleI18nIcu(i18n) &&
  17961. !(isSingleElementTemplate(nodes) && nodes[0].i18n === i18n));
  17962. const selfClosingI18nInstruction = hasTextChildrenOnly(nodes);
  17963. if (initI18nContext) {
  17964. this.i18nStart(null, i18n, selfClosingI18nInstruction);
  17965. }
  17966. // This is the initial pass through the nodes of this template. In this pass, we
  17967. // queue all creation mode and update mode instructions for generation in the second
  17968. // pass. It's necessary to separate the passes to ensure local refs are defined before
  17969. // resolving bindings. We also count bindings in this pass as we walk bound expressions.
  17970. visitAll(this, nodes);
  17971. // Add total binding count to pure function count so pure function instructions are
  17972. // generated with the correct slot offset when update instructions are processed.
  17973. this._pureFunctionSlots += this._bindingSlots;
  17974. // Pipes are walked in the first pass (to enqueue `pipe()` creation instructions and
  17975. // `pipeBind` update instructions), so we have to update the slot offsets manually
  17976. // to account for bindings.
  17977. this._valueConverter.updatePipeSlotOffsets(this._bindingSlots);
  17978. // Nested templates must be processed before creation instructions so template()
  17979. // instructions can be generated with the correct internal const count.
  17980. this._nestedTemplateFns.forEach(buildTemplateFn => buildTemplateFn());
  17981. // Output the `projectionDef` instruction when some `<ng-content>` tags are present.
  17982. // The `projectionDef` instruction is only emitted for the component template and
  17983. // is skipped for nested templates (<ng-template> tags).
  17984. if (this.level === 0 && this._ngContentReservedSlots.length) {
  17985. const parameters = [];
  17986. // By default the `projectionDef` instructions creates one slot for the wildcard
  17987. // selector if no parameters are passed. Therefore we only want to allocate a new
  17988. // array for the projection slots if the default projection slot is not sufficient.
  17989. if (this._ngContentReservedSlots.length > 1 || this._ngContentReservedSlots[0] !== '*') {
  17990. const r3ReservedSlots = this._ngContentReservedSlots.map(s => s !== '*' ? parseSelectorToR3Selector(s) : s);
  17991. parameters.push(this.constantPool.getConstLiteral(asLiteral(r3ReservedSlots), true));
  17992. }
  17993. // Since we accumulate ngContent selectors while processing template elements,
  17994. // we *prepend* `projectionDef` to creation instructions block, to put it before
  17995. // any `projection` instructions
  17996. this.creationInstruction(null, Identifiers.projectionDef, parameters, /* prepend */ true);
  17997. }
  17998. if (initI18nContext) {
  17999. this.i18nEnd(null, selfClosingI18nInstruction);
  18000. }
  18001. // Generate all the creation mode instructions (e.g. resolve bindings in listeners)
  18002. const creationStatements = this._creationCodeFns.map((fn) => fn());
  18003. // Generate all the update mode instructions (e.g. resolve property or text bindings)
  18004. const updateStatements = this._updateCodeFns.map((fn) => fn());
  18005. // Variable declaration must occur after binding resolution so we can generate context
  18006. // instructions that build on each other.
  18007. // e.g. const b = nextContext().$implicit(); const b = nextContext();
  18008. const creationVariables = this._bindingScope.viewSnapshotStatements();
  18009. const updateVariables = this._bindingScope.variableDeclarations().concat(this._tempVariables);
  18010. const creationBlock = creationStatements.length > 0 ?
  18011. [renderFlagCheckIfStmt(1 /* Create */, creationVariables.concat(creationStatements))] :
  18012. [];
  18013. const updateBlock = updateStatements.length > 0 ?
  18014. [renderFlagCheckIfStmt(2 /* Update */, updateVariables.concat(updateStatements))] :
  18015. [];
  18016. return fn(
  18017. // i.e. (rf: RenderFlags, ctx: any)
  18018. [new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], [
  18019. // Temporary variable declarations for query refresh (i.e. let _t: any;)
  18020. ...this._prefixCode,
  18021. // Creating mode (i.e. if (rf & RenderFlags.Create) { ... })
  18022. ...creationBlock,
  18023. // Binding and refresh mode (i.e. if (rf & RenderFlags.Update) {...})
  18024. ...updateBlock,
  18025. ], INFERRED_TYPE, null, this.templateName);
  18026. }
  18027. // LocalResolver
  18028. getLocal(name) {
  18029. return this._bindingScope.get(name);
  18030. }
  18031. // LocalResolver
  18032. notifyImplicitReceiverUse() {
  18033. this._bindingScope.notifyImplicitReceiverUse();
  18034. }
  18035. i18nTranslate(message, params = {}, ref, transformFn) {
  18036. const _ref = ref || this.i18nGenerateMainBlockVar();
  18037. // Closure Compiler requires const names to start with `MSG_` but disallows any other const to
  18038. // start with `MSG_`. We define a variable starting with `MSG_` just for the `goog.getMsg` call
  18039. const closureVar = this.i18nGenerateClosureVar(message.id);
  18040. const statements = getTranslationDeclStmts(message, _ref, closureVar, params, transformFn);
  18041. this._constants.prepareStatements.push(...statements);
  18042. return _ref;
  18043. }
  18044. registerContextVariables(variable$1) {
  18045. const scopedName = this._bindingScope.freshReferenceName();
  18046. const retrievalLevel = this.level;
  18047. const lhs = variable(variable$1.name + scopedName);
  18048. this._bindingScope.set(retrievalLevel, variable$1.name, lhs, 1 /* CONTEXT */, (scope, relativeLevel) => {
  18049. let rhs;
  18050. if (scope.bindingLevel === retrievalLevel) {
  18051. if (scope.isListenerScope() && scope.hasRestoreViewVariable()) {
  18052. // e.g. restoredCtx.
  18053. // We have to get the context from a view reference, if one is available, because
  18054. // the context that was passed in during creation may not be correct anymore.
  18055. // For more information see: https://github.com/angular/angular/pull/40360.
  18056. rhs = variable(RESTORED_VIEW_CONTEXT_NAME);
  18057. scope.notifyRestoredViewContextUse();
  18058. }
  18059. else {
  18060. // e.g. ctx
  18061. rhs = variable(CONTEXT_NAME);
  18062. }
  18063. }
  18064. else {
  18065. const sharedCtxVar = scope.getSharedContextName(retrievalLevel);
  18066. // e.g. ctx_r0 OR x(2);
  18067. rhs = sharedCtxVar ? sharedCtxVar : generateNextContextExpr(relativeLevel);
  18068. }
  18069. // e.g. const $item$ = x(2).$implicit;
  18070. return [lhs.set(rhs.prop(variable$1.value || IMPLICIT_REFERENCE)).toConstDecl()];
  18071. });
  18072. }
  18073. i18nAppendBindings(expressions) {
  18074. if (expressions.length > 0) {
  18075. expressions.forEach(expression => this.i18n.appendBinding(expression));
  18076. }
  18077. }
  18078. i18nBindProps(props) {
  18079. const bound = {};
  18080. Object.keys(props).forEach(key => {
  18081. const prop = props[key];
  18082. if (prop instanceof Text) {
  18083. bound[key] = literal(prop.value);
  18084. }
  18085. else {
  18086. const value = prop.value.visit(this._valueConverter);
  18087. this.allocateBindingSlots(value);
  18088. if (value instanceof Interpolation) {
  18089. const { strings, expressions } = value;
  18090. const { id, bindings } = this.i18n;
  18091. const label = assembleI18nBoundString(strings, bindings.size, id);
  18092. this.i18nAppendBindings(expressions);
  18093. bound[key] = literal(label);
  18094. }
  18095. }
  18096. });
  18097. return bound;
  18098. }
  18099. // Generates top level vars for i18n blocks (i.e. `i18n_N`).
  18100. i18nGenerateMainBlockVar() {
  18101. return variable(this.constantPool.uniqueName(TRANSLATION_VAR_PREFIX));
  18102. }
  18103. // Generates vars with Closure-specific names for i18n blocks (i.e. `MSG_XXX`).
  18104. i18nGenerateClosureVar(messageId) {
  18105. let name;
  18106. const suffix = this.fileBasedI18nSuffix.toUpperCase();
  18107. if (this.i18nUseExternalIds) {
  18108. const prefix = getTranslationConstPrefix(`EXTERNAL_`);
  18109. const uniqueSuffix = this.constantPool.uniqueName(suffix);
  18110. name = `${prefix}${sanitizeIdentifier(messageId)}$$${uniqueSuffix}`;
  18111. }
  18112. else {
  18113. const prefix = getTranslationConstPrefix(suffix);
  18114. name = this.constantPool.uniqueName(prefix);
  18115. }
  18116. return variable(name);
  18117. }
  18118. i18nUpdateRef(context) {
  18119. const { icus, meta, isRoot, isResolved, isEmitted } = context;
  18120. if (isRoot && isResolved && !isEmitted && !isSingleI18nIcu(meta)) {
  18121. context.isEmitted = true;
  18122. const placeholders = context.getSerializedPlaceholders();
  18123. let icuMapping = {};
  18124. let params = placeholders.size ? placeholdersToParams(placeholders) : {};
  18125. if (icus.size) {
  18126. icus.forEach((refs, key) => {
  18127. if (refs.length === 1) {
  18128. // if we have one ICU defined for a given
  18129. // placeholder - just output its reference
  18130. params[key] = refs[0];
  18131. }
  18132. else {
  18133. // ... otherwise we need to activate post-processing
  18134. // to replace ICU placeholders with proper values
  18135. const placeholder = wrapI18nPlaceholder(`${I18N_ICU_MAPPING_PREFIX}${key}`);
  18136. params[key] = literal(placeholder);
  18137. icuMapping[key] = literalArr(refs);
  18138. }
  18139. });
  18140. }
  18141. // translation requires post processing in 2 cases:
  18142. // - if we have placeholders with multiple values (ex. `START_DIV`: [�#1�, �#2�, ...])
  18143. // - if we have multiple ICUs that refer to the same placeholder name
  18144. const needsPostprocessing = Array.from(placeholders.values()).some((value) => value.length > 1) ||
  18145. Object.keys(icuMapping).length;
  18146. let transformFn;
  18147. if (needsPostprocessing) {
  18148. transformFn = (raw) => {
  18149. const args = [raw];
  18150. if (Object.keys(icuMapping).length) {
  18151. args.push(mapLiteral(icuMapping, true));
  18152. }
  18153. return instruction(null, Identifiers.i18nPostprocess, args);
  18154. };
  18155. }
  18156. this.i18nTranslate(meta, params, context.ref, transformFn);
  18157. }
  18158. }
  18159. i18nStart(span = null, meta, selfClosing) {
  18160. const index = this.allocateDataSlot();
  18161. this.i18n = this.i18nContext ?
  18162. this.i18nContext.forkChildContext(index, this.templateIndex, meta) :
  18163. new I18nContext(index, this.i18nGenerateMainBlockVar(), 0, this.templateIndex, meta);
  18164. // generate i18nStart instruction
  18165. const { id, ref } = this.i18n;
  18166. const params = [literal(index), this.addToConsts(ref)];
  18167. if (id > 0) {
  18168. // do not push 3rd argument (sub-block id)
  18169. // into i18nStart call for top level i18n context
  18170. params.push(literal(id));
  18171. }
  18172. this.creationInstruction(span, selfClosing ? Identifiers.i18n : Identifiers.i18nStart, params);
  18173. }
  18174. i18nEnd(span = null, selfClosing) {
  18175. if (!this.i18n) {
  18176. throw new Error('i18nEnd is executed with no i18n context present');
  18177. }
  18178. if (this.i18nContext) {
  18179. this.i18nContext.reconcileChildContext(this.i18n);
  18180. this.i18nUpdateRef(this.i18nContext);
  18181. }
  18182. else {
  18183. this.i18nUpdateRef(this.i18n);
  18184. }
  18185. // setup accumulated bindings
  18186. const { index, bindings } = this.i18n;
  18187. if (bindings.size) {
  18188. const chainBindings = [];
  18189. bindings.forEach(binding => {
  18190. chainBindings.push({ sourceSpan: span, value: () => this.convertPropertyBinding(binding) });
  18191. });
  18192. // for i18n block, advance to the most recent element index (by taking the current number of
  18193. // elements and subtracting one) before invoking `i18nExp` instructions, to make sure the
  18194. // necessary lifecycle hooks of components/directives are properly flushed.
  18195. this.updateInstructionChainWithAdvance(this.getConstCount() - 1, Identifiers.i18nExp, chainBindings);
  18196. this.updateInstruction(span, Identifiers.i18nApply, [literal(index)]);
  18197. }
  18198. if (!selfClosing) {
  18199. this.creationInstruction(span, Identifiers.i18nEnd);
  18200. }
  18201. this.i18n = null; // reset local i18n context
  18202. }
  18203. i18nAttributesInstruction(nodeIndex, attrs, sourceSpan) {
  18204. let hasBindings = false;
  18205. const i18nAttrArgs = [];
  18206. const bindings = [];
  18207. attrs.forEach(attr => {
  18208. const message = attr.i18n;
  18209. const converted = attr.value.visit(this._valueConverter);
  18210. this.allocateBindingSlots(converted);
  18211. if (converted instanceof Interpolation) {
  18212. const placeholders = assembleBoundTextPlaceholders(message);
  18213. const params = placeholdersToParams(placeholders);
  18214. i18nAttrArgs.push(literal(attr.name), this.i18nTranslate(message, params));
  18215. converted.expressions.forEach(expression => {
  18216. hasBindings = true;
  18217. bindings.push({
  18218. sourceSpan,
  18219. value: () => this.convertPropertyBinding(expression),
  18220. });
  18221. });
  18222. }
  18223. });
  18224. if (bindings.length > 0) {
  18225. this.updateInstructionChainWithAdvance(nodeIndex, Identifiers.i18nExp, bindings);
  18226. }
  18227. if (i18nAttrArgs.length > 0) {
  18228. const index = literal(this.allocateDataSlot());
  18229. const constIndex = this.addToConsts(literalArr(i18nAttrArgs));
  18230. this.creationInstruction(sourceSpan, Identifiers.i18nAttributes, [index, constIndex]);
  18231. if (hasBindings) {
  18232. this.updateInstruction(sourceSpan, Identifiers.i18nApply, [index]);
  18233. }
  18234. }
  18235. }
  18236. getNamespaceInstruction(namespaceKey) {
  18237. switch (namespaceKey) {
  18238. case 'math':
  18239. return Identifiers.namespaceMathML;
  18240. case 'svg':
  18241. return Identifiers.namespaceSVG;
  18242. default:
  18243. return Identifiers.namespaceHTML;
  18244. }
  18245. }
  18246. addNamespaceInstruction(nsInstruction, element) {
  18247. this._namespace = nsInstruction;
  18248. this.creationInstruction(element.startSourceSpan, nsInstruction);
  18249. }
  18250. /**
  18251. * Adds an update instruction for an interpolated property or attribute, such as
  18252. * `prop="{{value}}"` or `attr.title="{{value}}"`
  18253. */
  18254. interpolatedUpdateInstruction(instruction, elementIndex, attrName, input, value, params) {
  18255. this.updateInstructionWithAdvance(elementIndex, input.sourceSpan, instruction, () => [literal(attrName), ...this.getUpdateInstructionArguments(value), ...params]);
  18256. }
  18257. visitContent(ngContent) {
  18258. const slot = this.allocateDataSlot();
  18259. const projectionSlotIdx = this._ngContentSelectorsOffset + this._ngContentReservedSlots.length;
  18260. const parameters = [literal(slot)];
  18261. this._ngContentReservedSlots.push(ngContent.selector);
  18262. const nonContentSelectAttributes = ngContent.attributes.filter(attr => attr.name.toLowerCase() !== NG_CONTENT_SELECT_ATTR$1);
  18263. const attributes = this.getAttributeExpressions(ngContent.name, nonContentSelectAttributes, [], []);
  18264. if (attributes.length > 0) {
  18265. parameters.push(literal(projectionSlotIdx), literalArr(attributes));
  18266. }
  18267. else if (projectionSlotIdx !== 0) {
  18268. parameters.push(literal(projectionSlotIdx));
  18269. }
  18270. this.creationInstruction(ngContent.sourceSpan, Identifiers.projection, parameters);
  18271. if (this.i18n) {
  18272. this.i18n.appendProjection(ngContent.i18n, slot);
  18273. }
  18274. }
  18275. visitElement(element) {
  18276. var _a, _b;
  18277. const elementIndex = this.allocateDataSlot();
  18278. const stylingBuilder = new StylingBuilder(null);
  18279. let isNonBindableMode = false;
  18280. const isI18nRootElement = isI18nRootNode(element.i18n) && !isSingleI18nIcu(element.i18n);
  18281. const outputAttrs = [];
  18282. const [namespaceKey, elementName] = splitNsName(element.name);
  18283. const isNgContainer$1 = isNgContainer(element.name);
  18284. // Handle styling, i18n, ngNonBindable attributes
  18285. for (const attr of element.attributes) {
  18286. const { name, value } = attr;
  18287. if (name === NON_BINDABLE_ATTR) {
  18288. isNonBindableMode = true;
  18289. }
  18290. else if (name === 'style') {
  18291. stylingBuilder.registerStyleAttr(value);
  18292. }
  18293. else if (name === 'class') {
  18294. stylingBuilder.registerClassAttr(value);
  18295. }
  18296. else {
  18297. outputAttrs.push(attr);
  18298. }
  18299. }
  18300. // Match directives on non i18n attributes
  18301. this.matchDirectives(element.name, element);
  18302. // Regular element or ng-container creation mode
  18303. const parameters = [literal(elementIndex)];
  18304. if (!isNgContainer$1) {
  18305. parameters.push(literal(elementName));
  18306. }
  18307. // Add the attributes
  18308. const allOtherInputs = [];
  18309. const boundI18nAttrs = [];
  18310. element.inputs.forEach(input => {
  18311. const stylingInputWasSet = stylingBuilder.registerBoundInput(input);
  18312. if (!stylingInputWasSet) {
  18313. if (input.type === 0 /* Property */ && input.i18n) {
  18314. boundI18nAttrs.push(input);
  18315. }
  18316. else {
  18317. allOtherInputs.push(input);
  18318. }
  18319. }
  18320. });
  18321. // add attributes for directive and projection matching purposes
  18322. const attributes = this.getAttributeExpressions(element.name, outputAttrs, allOtherInputs, element.outputs, stylingBuilder, [], boundI18nAttrs);
  18323. parameters.push(this.addAttrsToConsts(attributes));
  18324. // local refs (ex.: <div #foo #bar="baz">)
  18325. const refs = this.prepareRefsArray(element.references);
  18326. parameters.push(this.addToConsts(refs));
  18327. const wasInNamespace = this._namespace;
  18328. const currentNamespace = this.getNamespaceInstruction(namespaceKey);
  18329. // If the namespace is changing now, include an instruction to change it
  18330. // during element creation.
  18331. if (currentNamespace !== wasInNamespace) {
  18332. this.addNamespaceInstruction(currentNamespace, element);
  18333. }
  18334. if (this.i18n) {
  18335. this.i18n.appendElement(element.i18n, elementIndex);
  18336. }
  18337. // Note that we do not append text node instructions and ICUs inside i18n section,
  18338. // so we exclude them while calculating whether current element has children
  18339. const hasChildren = (!isI18nRootElement && this.i18n) ? !hasTextChildrenOnly(element.children) :
  18340. element.children.length > 0;
  18341. const createSelfClosingInstruction = !stylingBuilder.hasBindingsWithPipes &&
  18342. element.outputs.length === 0 && boundI18nAttrs.length === 0 && !hasChildren;
  18343. const createSelfClosingI18nInstruction = !createSelfClosingInstruction && hasTextChildrenOnly(element.children);
  18344. if (createSelfClosingInstruction) {
  18345. this.creationInstruction(element.sourceSpan, isNgContainer$1 ? Identifiers.elementContainer : Identifiers.element, trimTrailingNulls(parameters));
  18346. }
  18347. else {
  18348. this.creationInstruction(element.startSourceSpan, isNgContainer$1 ? Identifiers.elementContainerStart : Identifiers.elementStart, trimTrailingNulls(parameters));
  18349. if (isNonBindableMode) {
  18350. this.creationInstruction(element.startSourceSpan, Identifiers.disableBindings);
  18351. }
  18352. if (boundI18nAttrs.length > 0) {
  18353. this.i18nAttributesInstruction(elementIndex, boundI18nAttrs, (_a = element.startSourceSpan) !== null && _a !== void 0 ? _a : element.sourceSpan);
  18354. }
  18355. // Generate Listeners (outputs)
  18356. if (element.outputs.length > 0) {
  18357. const listeners = element.outputs.map((outputAst) => ({
  18358. sourceSpan: outputAst.sourceSpan,
  18359. params: this.prepareListenerParameter(element.name, outputAst, elementIndex)
  18360. }));
  18361. this.creationInstructionChain(Identifiers.listener, listeners);
  18362. }
  18363. // Note: it's important to keep i18n/i18nStart instructions after i18nAttributes and
  18364. // listeners, to make sure i18nAttributes instruction targets current element at runtime.
  18365. if (isI18nRootElement) {
  18366. this.i18nStart(element.startSourceSpan, element.i18n, createSelfClosingI18nInstruction);
  18367. }
  18368. }
  18369. // the code here will collect all update-level styling instructions and add them to the
  18370. // update block of the template function AOT code. Instructions like `styleProp`,
  18371. // `styleMap`, `classMap`, `classProp`
  18372. // are all generated and assigned in the code below.
  18373. const stylingInstructions = stylingBuilder.buildUpdateLevelInstructions(this._valueConverter);
  18374. const limit = stylingInstructions.length - 1;
  18375. for (let i = 0; i <= limit; i++) {
  18376. const instruction = stylingInstructions[i];
  18377. this._bindingSlots += this.processStylingUpdateInstruction(elementIndex, instruction);
  18378. }
  18379. // the reason why `undefined` is used is because the renderer understands this as a
  18380. // special value to symbolize that there is no RHS to this binding
  18381. // TODO (matsko): revisit this once FW-959 is approached
  18382. const emptyValueBindInstruction = literal(undefined);
  18383. const propertyBindings = [];
  18384. const attributeBindings = [];
  18385. // Generate element input bindings
  18386. allOtherInputs.forEach(input => {
  18387. const inputType = input.type;
  18388. if (inputType === 4 /* Animation */) {
  18389. const value = input.value.visit(this._valueConverter);
  18390. // animation bindings can be presented in the following formats:
  18391. // 1. [@binding]="fooExp"
  18392. // 2. [@binding]="{value:fooExp, params:{...}}"
  18393. // 3. [@binding]
  18394. // 4. @binding
  18395. // All formats will be valid for when a synthetic binding is created.
  18396. // The reasoning for this is because the renderer should get each
  18397. // synthetic binding value in the order of the array that they are
  18398. // defined in...
  18399. const hasValue = value instanceof LiteralPrimitive ? !!value.value : true;
  18400. this.allocateBindingSlots(value);
  18401. propertyBindings.push({
  18402. name: prepareSyntheticPropertyName(input.name),
  18403. sourceSpan: input.sourceSpan,
  18404. value: () => hasValue ? this.convertPropertyBinding(value) : emptyValueBindInstruction
  18405. });
  18406. }
  18407. else {
  18408. // we must skip attributes with associated i18n context, since these attributes are handled
  18409. // separately and corresponding `i18nExp` and `i18nApply` instructions will be generated
  18410. if (input.i18n)
  18411. return;
  18412. const value = input.value.visit(this._valueConverter);
  18413. if (value !== undefined) {
  18414. const params = [];
  18415. const [attrNamespace, attrName] = splitNsName(input.name);
  18416. const isAttributeBinding = inputType === 1 /* Attribute */;
  18417. const sanitizationRef = resolveSanitizationFn(input.securityContext, isAttributeBinding);
  18418. if (sanitizationRef)
  18419. params.push(sanitizationRef);
  18420. if (attrNamespace) {
  18421. const namespaceLiteral = literal(attrNamespace);
  18422. if (sanitizationRef) {
  18423. params.push(namespaceLiteral);
  18424. }
  18425. else {
  18426. // If there wasn't a sanitization ref, we need to add
  18427. // an extra param so that we can pass in the namespace.
  18428. params.push(literal(null), namespaceLiteral);
  18429. }
  18430. }
  18431. this.allocateBindingSlots(value);
  18432. if (inputType === 0 /* Property */) {
  18433. if (value instanceof Interpolation) {
  18434. // prop="{{value}}" and friends
  18435. this.interpolatedUpdateInstruction(getPropertyInterpolationExpression(value), elementIndex, attrName, input, value, params);
  18436. }
  18437. else {
  18438. // [prop]="value"
  18439. // Collect all the properties so that we can chain into a single function at the end.
  18440. propertyBindings.push({
  18441. name: attrName,
  18442. sourceSpan: input.sourceSpan,
  18443. value: () => this.convertPropertyBinding(value),
  18444. params
  18445. });
  18446. }
  18447. }
  18448. else if (inputType === 1 /* Attribute */) {
  18449. if (value instanceof Interpolation && getInterpolationArgsLength(value) > 1) {
  18450. // attr.name="text{{value}}" and friends
  18451. this.interpolatedUpdateInstruction(getAttributeInterpolationExpression(value), elementIndex, attrName, input, value, params);
  18452. }
  18453. else {
  18454. const boundValue = value instanceof Interpolation ? value.expressions[0] : value;
  18455. // [attr.name]="value" or attr.name="{{value}}"
  18456. // Collect the attribute bindings so that they can be chained at the end.
  18457. attributeBindings.push({
  18458. name: attrName,
  18459. sourceSpan: input.sourceSpan,
  18460. value: () => this.convertPropertyBinding(boundValue),
  18461. params
  18462. });
  18463. }
  18464. }
  18465. else {
  18466. // class prop
  18467. this.updateInstructionWithAdvance(elementIndex, input.sourceSpan, Identifiers.classProp, () => {
  18468. return [
  18469. literal(elementIndex), literal(attrName), this.convertPropertyBinding(value),
  18470. ...params
  18471. ];
  18472. });
  18473. }
  18474. }
  18475. }
  18476. });
  18477. if (propertyBindings.length > 0) {
  18478. this.updateInstructionChainWithAdvance(elementIndex, Identifiers.property, propertyBindings);
  18479. }
  18480. if (attributeBindings.length > 0) {
  18481. this.updateInstructionChainWithAdvance(elementIndex, Identifiers.attribute, attributeBindings);
  18482. }
  18483. // Traverse element child nodes
  18484. visitAll(this, element.children);
  18485. if (!isI18nRootElement && this.i18n) {
  18486. this.i18n.appendElement(element.i18n, elementIndex, true);
  18487. }
  18488. if (!createSelfClosingInstruction) {
  18489. // Finish element construction mode.
  18490. const span = (_b = element.endSourceSpan) !== null && _b !== void 0 ? _b : element.sourceSpan;
  18491. if (isI18nRootElement) {
  18492. this.i18nEnd(span, createSelfClosingI18nInstruction);
  18493. }
  18494. if (isNonBindableMode) {
  18495. this.creationInstruction(span, Identifiers.enableBindings);
  18496. }
  18497. this.creationInstruction(span, isNgContainer$1 ? Identifiers.elementContainerEnd : Identifiers.elementEnd);
  18498. }
  18499. }
  18500. visitTemplate(template) {
  18501. var _a;
  18502. const NG_TEMPLATE_TAG_NAME = 'ng-template';
  18503. const templateIndex = this.allocateDataSlot();
  18504. if (this.i18n) {
  18505. this.i18n.appendTemplate(template.i18n, templateIndex);
  18506. }
  18507. const tagNameWithoutNamespace = template.tagName ? splitNsName(template.tagName)[1] : template.tagName;
  18508. const contextName = `${this.contextName}${template.tagName ? '_' + sanitizeIdentifier(template.tagName) : ''}_${templateIndex}`;
  18509. const templateName = `${contextName}_Template`;
  18510. const parameters = [
  18511. literal(templateIndex),
  18512. variable(templateName),
  18513. // We don't care about the tag's namespace here, because we infer
  18514. // it based on the parent nodes inside the template instruction.
  18515. literal(tagNameWithoutNamespace),
  18516. ];
  18517. // find directives matching on a given <ng-template> node
  18518. this.matchDirectives(NG_TEMPLATE_TAG_NAME, template);
  18519. // prepare attributes parameter (including attributes used for directive matching)
  18520. const attrsExprs = this.getAttributeExpressions(NG_TEMPLATE_TAG_NAME, template.attributes, template.inputs, template.outputs, undefined /* styles */, template.templateAttrs);
  18521. parameters.push(this.addAttrsToConsts(attrsExprs));
  18522. // local refs (ex.: <ng-template #foo>)
  18523. if (template.references && template.references.length) {
  18524. const refs = this.prepareRefsArray(template.references);
  18525. parameters.push(this.addToConsts(refs));
  18526. parameters.push(importExpr(Identifiers.templateRefExtractor));
  18527. }
  18528. // Create the template function
  18529. const templateVisitor = new TemplateDefinitionBuilder(this.constantPool, this._bindingScope, this.level + 1, contextName, this.i18n, templateIndex, templateName, this.directiveMatcher, this.directives, this.pipeTypeByName, this.pipes, this._namespace, this.fileBasedI18nSuffix, this.i18nUseExternalIds, this._constants);
  18530. // Nested templates must not be visited until after their parent templates have completed
  18531. // processing, so they are queued here until after the initial pass. Otherwise, we wouldn't
  18532. // be able to support bindings in nested templates to local refs that occur after the
  18533. // template definition. e.g. <div *ngIf="showing">{{ foo }}</div> <div #foo></div>
  18534. this._nestedTemplateFns.push(() => {
  18535. const templateFunctionExpr = templateVisitor.buildTemplateFunction(template.children, template.variables, this._ngContentReservedSlots.length + this._ngContentSelectorsOffset, template.i18n);
  18536. this.constantPool.statements.push(templateFunctionExpr.toDeclStmt(templateName));
  18537. if (templateVisitor._ngContentReservedSlots.length) {
  18538. this._ngContentReservedSlots.push(...templateVisitor._ngContentReservedSlots);
  18539. }
  18540. });
  18541. // e.g. template(1, MyComp_Template_1)
  18542. this.creationInstruction(template.sourceSpan, Identifiers.templateCreate, () => {
  18543. parameters.splice(2, 0, literal(templateVisitor.getConstCount()), literal(templateVisitor.getVarCount()));
  18544. return trimTrailingNulls(parameters);
  18545. });
  18546. // handle property bindings e.g. ɵɵproperty('ngForOf', ctx.items), et al;
  18547. this.templatePropertyBindings(templateIndex, template.templateAttrs);
  18548. // Only add normal input/output binding instructions on explicit <ng-template> elements.
  18549. if (tagNameWithoutNamespace === NG_TEMPLATE_TAG_NAME) {
  18550. const [i18nInputs, inputs] = partitionArray(template.inputs, hasI18nMeta);
  18551. // Add i18n attributes that may act as inputs to directives. If such attributes are present,
  18552. // generate `i18nAttributes` instruction. Note: we generate it only for explicit <ng-template>
  18553. // elements, in case of inline templates, corresponding instructions will be generated in the
  18554. // nested template function.
  18555. if (i18nInputs.length > 0) {
  18556. this.i18nAttributesInstruction(templateIndex, i18nInputs, (_a = template.startSourceSpan) !== null && _a !== void 0 ? _a : template.sourceSpan);
  18557. }
  18558. // Add the input bindings
  18559. if (inputs.length > 0) {
  18560. this.templatePropertyBindings(templateIndex, inputs);
  18561. }
  18562. // Generate listeners for directive output
  18563. if (template.outputs.length > 0) {
  18564. const listeners = template.outputs.map((outputAst) => ({
  18565. sourceSpan: outputAst.sourceSpan,
  18566. params: this.prepareListenerParameter('ng_template', outputAst, templateIndex)
  18567. }));
  18568. this.creationInstructionChain(Identifiers.listener, listeners);
  18569. }
  18570. }
  18571. }
  18572. visitBoundText(text) {
  18573. if (this.i18n) {
  18574. const value = text.value.visit(this._valueConverter);
  18575. this.allocateBindingSlots(value);
  18576. if (value instanceof Interpolation) {
  18577. this.i18n.appendBoundText(text.i18n);
  18578. this.i18nAppendBindings(value.expressions);
  18579. }
  18580. return;
  18581. }
  18582. const nodeIndex = this.allocateDataSlot();
  18583. this.creationInstruction(text.sourceSpan, Identifiers.text, [literal(nodeIndex)]);
  18584. const value = text.value.visit(this._valueConverter);
  18585. this.allocateBindingSlots(value);
  18586. if (value instanceof Interpolation) {
  18587. this.updateInstructionWithAdvance(nodeIndex, text.sourceSpan, getTextInterpolationExpression(value), () => this.getUpdateInstructionArguments(value));
  18588. }
  18589. else {
  18590. error('Text nodes should be interpolated and never bound directly.');
  18591. }
  18592. }
  18593. visitText(text) {
  18594. // when a text element is located within a translatable
  18595. // block, we exclude this text element from instructions set,
  18596. // since it will be captured in i18n content and processed at runtime
  18597. if (!this.i18n) {
  18598. this.creationInstruction(text.sourceSpan, Identifiers.text, [literal(this.allocateDataSlot()), literal(text.value)]);
  18599. }
  18600. }
  18601. visitIcu(icu) {
  18602. let initWasInvoked = false;
  18603. // if an ICU was created outside of i18n block, we still treat
  18604. // it as a translatable entity and invoke i18nStart and i18nEnd
  18605. // to generate i18n context and the necessary instructions
  18606. if (!this.i18n) {
  18607. initWasInvoked = true;
  18608. this.i18nStart(null, icu.i18n, true);
  18609. }
  18610. const i18n = this.i18n;
  18611. const vars = this.i18nBindProps(icu.vars);
  18612. const placeholders = this.i18nBindProps(icu.placeholders);
  18613. // output ICU directly and keep ICU reference in context
  18614. const message = icu.i18n;
  18615. // we always need post-processing function for ICUs, to make sure that:
  18616. // - all placeholders in a form of {PLACEHOLDER} are replaced with actual values (note:
  18617. // `goog.getMsg` does not process ICUs and uses the `{PLACEHOLDER}` format for placeholders
  18618. // inside ICUs)
  18619. // - all ICU vars (such as `VAR_SELECT` or `VAR_PLURAL`) are replaced with correct values
  18620. const transformFn = (raw) => {
  18621. const params = Object.assign(Object.assign({}, vars), placeholders);
  18622. const formatted = i18nFormatPlaceholderNames(params, /* useCamelCase */ false);
  18623. return instruction(null, Identifiers.i18nPostprocess, [raw, mapLiteral(formatted, true)]);
  18624. };
  18625. // in case the whole i18n message is a single ICU - we do not need to
  18626. // create a separate top-level translation, we can use the root ref instead
  18627. // and make this ICU a top-level translation
  18628. // note: ICU placeholders are replaced with actual values in `i18nPostprocess` function
  18629. // separately, so we do not pass placeholders into `i18nTranslate` function.
  18630. if (isSingleI18nIcu(i18n.meta)) {
  18631. this.i18nTranslate(message, /* placeholders */ {}, i18n.ref, transformFn);
  18632. }
  18633. else {
  18634. // output ICU directly and keep ICU reference in context
  18635. const ref = this.i18nTranslate(message, /* placeholders */ {}, /* ref */ undefined, transformFn);
  18636. i18n.appendIcu(icuFromI18nMessage(message).name, ref);
  18637. }
  18638. if (initWasInvoked) {
  18639. this.i18nEnd(null, true);
  18640. }
  18641. return null;
  18642. }
  18643. allocateDataSlot() {
  18644. return this._dataIndex++;
  18645. }
  18646. getConstCount() {
  18647. return this._dataIndex;
  18648. }
  18649. getVarCount() {
  18650. return this._pureFunctionSlots;
  18651. }
  18652. getConsts() {
  18653. return this._constants;
  18654. }
  18655. getNgContentSelectors() {
  18656. return this._ngContentReservedSlots.length ?
  18657. this.constantPool.getConstLiteral(asLiteral(this._ngContentReservedSlots), true) :
  18658. null;
  18659. }
  18660. bindingContext() {
  18661. return `${this._bindingContext++}`;
  18662. }
  18663. templatePropertyBindings(templateIndex, attrs) {
  18664. const propertyBindings = [];
  18665. attrs.forEach(input => {
  18666. if (input instanceof BoundAttribute) {
  18667. const value = input.value.visit(this._valueConverter);
  18668. if (value !== undefined) {
  18669. this.allocateBindingSlots(value);
  18670. if (value instanceof Interpolation) {
  18671. // Params typically contain attribute namespace and value sanitizer, which is applicable
  18672. // for regular HTML elements, but not applicable for <ng-template> (since props act as
  18673. // inputs to directives), so keep params array empty.
  18674. const params = [];
  18675. // prop="{{value}}" case
  18676. this.interpolatedUpdateInstruction(getPropertyInterpolationExpression(value), templateIndex, input.name, input, value, params);
  18677. }
  18678. else {
  18679. // [prop]="value" case
  18680. propertyBindings.push({
  18681. name: input.name,
  18682. sourceSpan: input.sourceSpan,
  18683. value: () => this.convertPropertyBinding(value)
  18684. });
  18685. }
  18686. }
  18687. }
  18688. });
  18689. if (propertyBindings.length > 0) {
  18690. this.updateInstructionChainWithAdvance(templateIndex, Identifiers.property, propertyBindings);
  18691. }
  18692. }
  18693. // Bindings must only be resolved after all local refs have been visited, so all
  18694. // instructions are queued in callbacks that execute once the initial pass has completed.
  18695. // Otherwise, we wouldn't be able to support local refs that are defined after their
  18696. // bindings. e.g. {{ foo }} <div #foo></div>
  18697. instructionFn(fns, span, reference, paramsOrFn, prepend = false) {
  18698. fns[prepend ? 'unshift' : 'push'](() => {
  18699. const params = Array.isArray(paramsOrFn) ? paramsOrFn : paramsOrFn();
  18700. return instruction(span, reference, params).toStmt();
  18701. });
  18702. }
  18703. processStylingUpdateInstruction(elementIndex, instruction) {
  18704. let allocateBindingSlots = 0;
  18705. if (instruction) {
  18706. const calls = [];
  18707. instruction.calls.forEach(call => {
  18708. allocateBindingSlots += call.allocateBindingSlots;
  18709. calls.push({
  18710. sourceSpan: call.sourceSpan,
  18711. value: () => {
  18712. return call.params(value => (call.supportsInterpolation && value instanceof Interpolation) ?
  18713. this.getUpdateInstructionArguments(value) :
  18714. this.convertPropertyBinding(value));
  18715. }
  18716. });
  18717. });
  18718. this.updateInstructionChainWithAdvance(elementIndex, instruction.reference, calls);
  18719. }
  18720. return allocateBindingSlots;
  18721. }
  18722. creationInstruction(span, reference, paramsOrFn, prepend) {
  18723. this.instructionFn(this._creationCodeFns, span, reference, paramsOrFn || [], prepend);
  18724. }
  18725. creationInstructionChain(reference, calls) {
  18726. const span = calls.length ? calls[0].sourceSpan : null;
  18727. this._creationCodeFns.push(() => {
  18728. return chainedInstruction(reference, calls.map(call => call.params()), span).toStmt();
  18729. });
  18730. }
  18731. updateInstructionWithAdvance(nodeIndex, span, reference, paramsOrFn) {
  18732. this.addAdvanceInstructionIfNecessary(nodeIndex, span);
  18733. this.updateInstruction(span, reference, paramsOrFn);
  18734. }
  18735. updateInstruction(span, reference, paramsOrFn) {
  18736. this.instructionFn(this._updateCodeFns, span, reference, paramsOrFn || []);
  18737. }
  18738. updateInstructionChain(reference, bindings) {
  18739. const span = bindings.length ? bindings[0].sourceSpan : null;
  18740. this._updateCodeFns.push(() => {
  18741. const calls = bindings.map(property => {
  18742. const value = property.value();
  18743. const fnParams = Array.isArray(value) ? value : [value];
  18744. if (property.params) {
  18745. fnParams.push(...property.params);
  18746. }
  18747. if (property.name) {
  18748. // We want the property name to always be the first function parameter.
  18749. fnParams.unshift(literal(property.name));
  18750. }
  18751. return fnParams;
  18752. });
  18753. return chainedInstruction(reference, calls, span).toStmt();
  18754. });
  18755. }
  18756. updateInstructionChainWithAdvance(nodeIndex, reference, bindings) {
  18757. this.addAdvanceInstructionIfNecessary(nodeIndex, bindings.length ? bindings[0].sourceSpan : null);
  18758. this.updateInstructionChain(reference, bindings);
  18759. }
  18760. addAdvanceInstructionIfNecessary(nodeIndex, span) {
  18761. if (nodeIndex !== this._currentIndex) {
  18762. const delta = nodeIndex - this._currentIndex;
  18763. if (delta < 1) {
  18764. throw new Error('advance instruction can only go forwards');
  18765. }
  18766. this.instructionFn(this._updateCodeFns, span, Identifiers.advance, [literal(delta)]);
  18767. this._currentIndex = nodeIndex;
  18768. }
  18769. }
  18770. allocatePureFunctionSlots(numSlots) {
  18771. const originalSlots = this._pureFunctionSlots;
  18772. this._pureFunctionSlots += numSlots;
  18773. return originalSlots;
  18774. }
  18775. allocateBindingSlots(value) {
  18776. this._bindingSlots += value instanceof Interpolation ? value.expressions.length : 1;
  18777. }
  18778. /**
  18779. * Gets an expression that refers to the implicit receiver. The implicit
  18780. * receiver is always the root level context.
  18781. */
  18782. getImplicitReceiverExpr() {
  18783. if (this._implicitReceiverExpr) {
  18784. return this._implicitReceiverExpr;
  18785. }
  18786. return this._implicitReceiverExpr = this.level === 0 ?
  18787. variable(CONTEXT_NAME) :
  18788. this._bindingScope.getOrCreateSharedContextVar(0);
  18789. }
  18790. convertPropertyBinding(value) {
  18791. const convertedPropertyBinding = convertPropertyBinding(this, this.getImplicitReceiverExpr(), value, this.bindingContext(), BindingForm.Expression, () => error('Unexpected interpolation'));
  18792. const valExpr = convertedPropertyBinding.currValExpr;
  18793. this._tempVariables.push(...convertedPropertyBinding.stmts);
  18794. return valExpr;
  18795. }
  18796. /**
  18797. * Gets a list of argument expressions to pass to an update instruction expression. Also updates
  18798. * the temp variables state with temp variables that were identified as needing to be created
  18799. * while visiting the arguments.
  18800. * @param value The original expression we will be resolving an arguments list from.
  18801. */
  18802. getUpdateInstructionArguments(value) {
  18803. const { args, stmts } = convertUpdateArguments(this, this.getImplicitReceiverExpr(), value, this.bindingContext());
  18804. this._tempVariables.push(...stmts);
  18805. return args;
  18806. }
  18807. matchDirectives(elementName, elOrTpl) {
  18808. if (this.directiveMatcher) {
  18809. const selector = createCssSelector(elementName, getAttrsForDirectiveMatching(elOrTpl));
  18810. this.directiveMatcher.match(selector, (cssSelector, staticType) => {
  18811. this.directives.add(staticType);
  18812. });
  18813. }
  18814. }
  18815. /**
  18816. * Prepares all attribute expression values for the `TAttributes` array.
  18817. *
  18818. * The purpose of this function is to properly construct an attributes array that
  18819. * is passed into the `elementStart` (or just `element`) functions. Because there
  18820. * are many different types of attributes, the array needs to be constructed in a
  18821. * special way so that `elementStart` can properly evaluate them.
  18822. *
  18823. * The format looks like this:
  18824. *
  18825. * ```
  18826. * attrs = [prop, value, prop2, value2,
  18827. * PROJECT_AS, selector,
  18828. * CLASSES, class1, class2,
  18829. * STYLES, style1, value1, style2, value2,
  18830. * BINDINGS, name1, name2, name3,
  18831. * TEMPLATE, name4, name5, name6,
  18832. * I18N, name7, name8, ...]
  18833. * ```
  18834. *
  18835. * Note that this function will fully ignore all synthetic (@foo) attribute values
  18836. * because those values are intended to always be generated as property instructions.
  18837. */
  18838. getAttributeExpressions(elementName, renderAttributes, inputs, outputs, styles, templateAttrs = [], boundI18nAttrs = []) {
  18839. const alreadySeen = new Set();
  18840. const attrExprs = [];
  18841. let ngProjectAsAttr;
  18842. for (const attr of renderAttributes) {
  18843. if (attr.name === NG_PROJECT_AS_ATTR_NAME) {
  18844. ngProjectAsAttr = attr;
  18845. }
  18846. // Note that static i18n attributes aren't in the i18n array,
  18847. // because they're treated in the same way as regular attributes.
  18848. if (attr.i18n) {
  18849. // When i18n attributes are present on elements with structural directives
  18850. // (e.g. `<div *ngIf title="Hello" i18n-title>`), we want to avoid generating
  18851. // duplicate i18n translation blocks for `ɵɵtemplate` and `ɵɵelement` instruction
  18852. // attributes. So we do a cache lookup to see if suitable i18n translation block
  18853. // already exists.
  18854. const { i18nVarRefsCache } = this._constants;
  18855. let i18nVarRef;
  18856. if (i18nVarRefsCache.has(attr.i18n)) {
  18857. i18nVarRef = i18nVarRefsCache.get(attr.i18n);
  18858. }
  18859. else {
  18860. i18nVarRef = this.i18nTranslate(attr.i18n);
  18861. i18nVarRefsCache.set(attr.i18n, i18nVarRef);
  18862. }
  18863. attrExprs.push(literal(attr.name), i18nVarRef);
  18864. }
  18865. else {
  18866. attrExprs.push(...getAttributeNameLiterals(attr.name), trustedConstAttribute(elementName, attr));
  18867. }
  18868. }
  18869. // Keep ngProjectAs next to the other name, value pairs so we can verify that we match
  18870. // ngProjectAs marker in the attribute name slot.
  18871. if (ngProjectAsAttr) {
  18872. attrExprs.push(...getNgProjectAsLiteral(ngProjectAsAttr));
  18873. }
  18874. function addAttrExpr(key, value) {
  18875. if (typeof key === 'string') {
  18876. if (!alreadySeen.has(key)) {
  18877. attrExprs.push(...getAttributeNameLiterals(key));
  18878. value !== undefined && attrExprs.push(value);
  18879. alreadySeen.add(key);
  18880. }
  18881. }
  18882. else {
  18883. attrExprs.push(literal(key));
  18884. }
  18885. }
  18886. // it's important that this occurs before BINDINGS and TEMPLATE because once `elementStart`
  18887. // comes across the BINDINGS or TEMPLATE markers then it will continue reading each value as
  18888. // as single property value cell by cell.
  18889. if (styles) {
  18890. styles.populateInitialStylingAttrs(attrExprs);
  18891. }
  18892. if (inputs.length || outputs.length) {
  18893. const attrsLengthBeforeInputs = attrExprs.length;
  18894. for (let i = 0; i < inputs.length; i++) {
  18895. const input = inputs[i];
  18896. // We don't want the animation and attribute bindings in the
  18897. // attributes array since they aren't used for directive matching.
  18898. if (input.type !== 4 /* Animation */ && input.type !== 1 /* Attribute */) {
  18899. addAttrExpr(input.name);
  18900. }
  18901. }
  18902. for (let i = 0; i < outputs.length; i++) {
  18903. const output = outputs[i];
  18904. if (output.type !== 1 /* Animation */) {
  18905. addAttrExpr(output.name);
  18906. }
  18907. }
  18908. // this is a cheap way of adding the marker only after all the input/output
  18909. // values have been filtered (by not including the animation ones) and added
  18910. // to the expressions. The marker is important because it tells the runtime
  18911. // code that this is where attributes without values start...
  18912. if (attrExprs.length !== attrsLengthBeforeInputs) {
  18913. attrExprs.splice(attrsLengthBeforeInputs, 0, literal(3 /* Bindings */));
  18914. }
  18915. }
  18916. if (templateAttrs.length) {
  18917. attrExprs.push(literal(4 /* Template */));
  18918. templateAttrs.forEach(attr => addAttrExpr(attr.name));
  18919. }
  18920. if (boundI18nAttrs.length) {
  18921. attrExprs.push(literal(6 /* I18n */));
  18922. boundI18nAttrs.forEach(attr => addAttrExpr(attr.name));
  18923. }
  18924. return attrExprs;
  18925. }
  18926. addToConsts(expression) {
  18927. if (isNull(expression)) {
  18928. return TYPED_NULL_EXPR;
  18929. }
  18930. const consts = this._constants.constExpressions;
  18931. // Try to reuse a literal that's already in the array, if possible.
  18932. for (let i = 0; i < consts.length; i++) {
  18933. if (consts[i].isEquivalent(expression)) {
  18934. return literal(i);
  18935. }
  18936. }
  18937. return literal(consts.push(expression) - 1);
  18938. }
  18939. addAttrsToConsts(attrs) {
  18940. return attrs.length > 0 ? this.addToConsts(literalArr(attrs)) : TYPED_NULL_EXPR;
  18941. }
  18942. prepareRefsArray(references) {
  18943. if (!references || references.length === 0) {
  18944. return TYPED_NULL_EXPR;
  18945. }
  18946. const refsParam = flatten(references.map(reference => {
  18947. const slot = this.allocateDataSlot();
  18948. // Generate the update temporary.
  18949. const variableName = this._bindingScope.freshReferenceName();
  18950. const retrievalLevel = this.level;
  18951. const lhs = variable(variableName);
  18952. this._bindingScope.set(retrievalLevel, reference.name, lhs, 0 /* DEFAULT */, (scope, relativeLevel) => {
  18953. // e.g. nextContext(2);
  18954. const nextContextStmt = relativeLevel > 0 ? [generateNextContextExpr(relativeLevel).toStmt()] : [];
  18955. // e.g. const $foo$ = reference(1);
  18956. const refExpr = lhs.set(importExpr(Identifiers.reference).callFn([literal(slot)]));
  18957. return nextContextStmt.concat(refExpr.toConstDecl());
  18958. }, true);
  18959. return [reference.name, reference.value];
  18960. }));
  18961. return asLiteral(refsParam);
  18962. }
  18963. prepareListenerParameter(tagName, outputAst, index) {
  18964. return () => {
  18965. const eventName = outputAst.name;
  18966. const bindingFnName = outputAst.type === 1 /* Animation */ ?
  18967. // synthetic @listener.foo values are treated the exact same as are standard listeners
  18968. prepareSyntheticListenerFunctionName(eventName, outputAst.phase) :
  18969. sanitizeIdentifier(eventName);
  18970. const handlerName = `${this.templateName}_${tagName}_${bindingFnName}_${index}_listener`;
  18971. const scope = this._bindingScope.nestedScope(this._bindingScope.bindingLevel, EVENT_BINDING_SCOPE_GLOBALS);
  18972. return prepareEventListenerParameters(outputAst, handlerName, scope);
  18973. };
  18974. }
  18975. }
  18976. class ValueConverter extends AstMemoryEfficientTransformer {
  18977. constructor(constantPool, allocateSlot, allocatePureFunctionSlots, definePipe) {
  18978. super();
  18979. this.constantPool = constantPool;
  18980. this.allocateSlot = allocateSlot;
  18981. this.allocatePureFunctionSlots = allocatePureFunctionSlots;
  18982. this.definePipe = definePipe;
  18983. this._pipeBindExprs = [];
  18984. }
  18985. // AstMemoryEfficientTransformer
  18986. visitPipe(pipe, context) {
  18987. // Allocate a slot to create the pipe
  18988. const slot = this.allocateSlot();
  18989. const slotPseudoLocal = `PIPE:${slot}`;
  18990. // Allocate one slot for the result plus one slot per pipe argument
  18991. const pureFunctionSlot = this.allocatePureFunctionSlots(2 + pipe.args.length);
  18992. const target = new PropertyRead(pipe.span, pipe.sourceSpan, pipe.nameSpan, new ImplicitReceiver(pipe.span, pipe.sourceSpan), slotPseudoLocal);
  18993. const { identifier, isVarLength } = pipeBindingCallInfo(pipe.args);
  18994. this.definePipe(pipe.name, slotPseudoLocal, slot, importExpr(identifier));
  18995. const args = [pipe.exp, ...pipe.args];
  18996. const convertedArgs = isVarLength ?
  18997. this.visitAll([new LiteralArray(pipe.span, pipe.sourceSpan, args)]) :
  18998. this.visitAll(args);
  18999. const pipeBindExpr = new FunctionCall(pipe.span, pipe.sourceSpan, target, [
  19000. new LiteralPrimitive(pipe.span, pipe.sourceSpan, slot),
  19001. new LiteralPrimitive(pipe.span, pipe.sourceSpan, pureFunctionSlot),
  19002. ...convertedArgs,
  19003. ]);
  19004. this._pipeBindExprs.push(pipeBindExpr);
  19005. return pipeBindExpr;
  19006. }
  19007. updatePipeSlotOffsets(bindingSlots) {
  19008. this._pipeBindExprs.forEach((pipe) => {
  19009. // update the slot offset arg (index 1) to account for binding slots
  19010. const slotOffset = pipe.args[1];
  19011. slotOffset.value += bindingSlots;
  19012. });
  19013. }
  19014. visitLiteralArray(array, context) {
  19015. return new BuiltinFunctionCall(array.span, array.sourceSpan, this.visitAll(array.expressions), values => {
  19016. // If the literal has calculated (non-literal) elements transform it into
  19017. // calls to literal factories that compose the literal and will cache intermediate
  19018. // values.
  19019. const literal = literalArr(values);
  19020. return getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
  19021. });
  19022. }
  19023. visitLiteralMap(map, context) {
  19024. return new BuiltinFunctionCall(map.span, map.sourceSpan, this.visitAll(map.values), values => {
  19025. // If the literal has calculated (non-literal) elements transform it into
  19026. // calls to literal factories that compose the literal and will cache intermediate
  19027. // values.
  19028. const literal = literalMap(values.map((value, index) => ({ key: map.keys[index].key, value, quoted: map.keys[index].quoted })));
  19029. return getLiteralFactory(this.constantPool, literal, this.allocatePureFunctionSlots);
  19030. });
  19031. }
  19032. }
  19033. // Pipes always have at least one parameter, the value they operate on
  19034. const pipeBindingIdentifiers = [Identifiers.pipeBind1, Identifiers.pipeBind2, Identifiers.pipeBind3, Identifiers.pipeBind4];
  19035. function pipeBindingCallInfo(args) {
  19036. const identifier = pipeBindingIdentifiers[args.length];
  19037. return {
  19038. identifier: identifier || Identifiers.pipeBindV,
  19039. isVarLength: !identifier,
  19040. };
  19041. }
  19042. const pureFunctionIdentifiers = [
  19043. Identifiers.pureFunction0, Identifiers.pureFunction1, Identifiers.pureFunction2, Identifiers.pureFunction3, Identifiers.pureFunction4,
  19044. Identifiers.pureFunction5, Identifiers.pureFunction6, Identifiers.pureFunction7, Identifiers.pureFunction8
  19045. ];
  19046. function pureFunctionCallInfo(args) {
  19047. const identifier = pureFunctionIdentifiers[args.length];
  19048. return {
  19049. identifier: identifier || Identifiers.pureFunctionV,
  19050. isVarLength: !identifier,
  19051. };
  19052. }
  19053. function instruction(span, reference, params) {
  19054. return importExpr(reference, null, span).callFn(params, span);
  19055. }
  19056. // e.g. x(2);
  19057. function generateNextContextExpr(relativeLevelDiff) {
  19058. return importExpr(Identifiers.nextContext)
  19059. .callFn(relativeLevelDiff > 1 ? [literal(relativeLevelDiff)] : []);
  19060. }
  19061. function getLiteralFactory(constantPool, literal$1, allocateSlots) {
  19062. const { literalFactory, literalFactoryArguments } = constantPool.getLiteralFactory(literal$1);
  19063. // Allocate 1 slot for the result plus 1 per argument
  19064. const startSlot = allocateSlots(1 + literalFactoryArguments.length);
  19065. const { identifier, isVarLength } = pureFunctionCallInfo(literalFactoryArguments);
  19066. // Literal factories are pure functions that only need to be re-invoked when the parameters
  19067. // change.
  19068. const args = [literal(startSlot), literalFactory];
  19069. if (isVarLength) {
  19070. args.push(literalArr(literalFactoryArguments));
  19071. }
  19072. else {
  19073. args.push(...literalFactoryArguments);
  19074. }
  19075. return importExpr(identifier).callFn(args);
  19076. }
  19077. /**
  19078. * Gets an array of literals that can be added to an expression
  19079. * to represent the name and namespace of an attribute. E.g.
  19080. * `:xlink:href` turns into `[AttributeMarker.NamespaceURI, 'xlink', 'href']`.
  19081. *
  19082. * @param name Name of the attribute, including the namespace.
  19083. */
  19084. function getAttributeNameLiterals(name) {
  19085. const [attributeNamespace, attributeName] = splitNsName(name);
  19086. const nameLiteral = literal(attributeName);
  19087. if (attributeNamespace) {
  19088. return [
  19089. literal(0 /* NamespaceURI */), literal(attributeNamespace), nameLiteral
  19090. ];
  19091. }
  19092. return [nameLiteral];
  19093. }
  19094. /** The prefix used to get a shared context in BindingScope's map. */
  19095. const SHARED_CONTEXT_KEY = '$$shared_ctx$$';
  19096. class BindingScope {
  19097. constructor(bindingLevel = 0, parent = null, globals) {
  19098. this.bindingLevel = bindingLevel;
  19099. this.parent = parent;
  19100. this.globals = globals;
  19101. /** Keeps a map from local variables to their BindingData. */
  19102. this.map = new Map();
  19103. this.referenceNameIndex = 0;
  19104. this.restoreViewVariable = null;
  19105. this.usesRestoredViewContext = false;
  19106. if (globals !== undefined) {
  19107. for (const name of globals) {
  19108. this.set(0, name, variable(name));
  19109. }
  19110. }
  19111. }
  19112. static createRootScope() {
  19113. return new BindingScope();
  19114. }
  19115. get(name) {
  19116. let current = this;
  19117. while (current) {
  19118. let value = current.map.get(name);
  19119. if (value != null) {
  19120. if (current !== this) {
  19121. // make a local copy and reset the `declare` state
  19122. value = {
  19123. retrievalLevel: value.retrievalLevel,
  19124. lhs: value.lhs,
  19125. declareLocalCallback: value.declareLocalCallback,
  19126. declare: false,
  19127. priority: value.priority,
  19128. localRef: value.localRef
  19129. };
  19130. // Cache the value locally.
  19131. this.map.set(name, value);
  19132. // Possibly generate a shared context var
  19133. this.maybeGenerateSharedContextVar(value);
  19134. this.maybeRestoreView(value.retrievalLevel, value.localRef);
  19135. }
  19136. if (value.declareLocalCallback && !value.declare) {
  19137. value.declare = true;
  19138. }
  19139. return value.lhs;
  19140. }
  19141. current = current.parent;
  19142. }
  19143. // If we get to this point, we are looking for a property on the top level component
  19144. // - If level === 0, we are on the top and don't need to re-declare `ctx`.
  19145. // - If level > 0, we are in an embedded view. We need to retrieve the name of the
  19146. // local var we used to store the component context, e.g. const $comp$ = x();
  19147. return this.bindingLevel === 0 ? null : this.getComponentProperty(name);
  19148. }
  19149. /**
  19150. * Create a local variable for later reference.
  19151. *
  19152. * @param retrievalLevel The level from which this value can be retrieved
  19153. * @param name Name of the variable.
  19154. * @param lhs AST representing the left hand side of the `let lhs = rhs;`.
  19155. * @param priority The sorting priority of this var
  19156. * @param declareLocalCallback The callback to invoke when declaring this local var
  19157. * @param localRef Whether or not this is a local ref
  19158. */
  19159. set(retrievalLevel, name, lhs, priority = 0 /* DEFAULT */, declareLocalCallback, localRef) {
  19160. if (this.map.has(name)) {
  19161. if (localRef) {
  19162. // Do not throw an error if it's a local ref and do not update existing value,
  19163. // so the first defined ref is always returned.
  19164. return this;
  19165. }
  19166. error(`The name ${name} is already defined in scope to be ${this.map.get(name)}`);
  19167. }
  19168. this.map.set(name, {
  19169. retrievalLevel: retrievalLevel,
  19170. lhs: lhs,
  19171. declare: false,
  19172. declareLocalCallback: declareLocalCallback,
  19173. priority: priority,
  19174. localRef: localRef || false
  19175. });
  19176. return this;
  19177. }
  19178. // Implemented as part of LocalResolver.
  19179. getLocal(name) {
  19180. return this.get(name);
  19181. }
  19182. // Implemented as part of LocalResolver.
  19183. notifyImplicitReceiverUse() {
  19184. if (this.bindingLevel !== 0) {
  19185. // Since the implicit receiver is accessed in an embedded view, we need to
  19186. // ensure that we declare a shared context variable for the current template
  19187. // in the update variables.
  19188. this.map.get(SHARED_CONTEXT_KEY + 0).declare = true;
  19189. }
  19190. }
  19191. nestedScope(level, globals) {
  19192. const newScope = new BindingScope(level, this, globals);
  19193. if (level > 0)
  19194. newScope.generateSharedContextVar(0);
  19195. return newScope;
  19196. }
  19197. /**
  19198. * Gets or creates a shared context variable and returns its expression. Note that
  19199. * this does not mean that the shared variable will be declared. Variables in the
  19200. * binding scope will be only declared if they are used.
  19201. */
  19202. getOrCreateSharedContextVar(retrievalLevel) {
  19203. const bindingKey = SHARED_CONTEXT_KEY + retrievalLevel;
  19204. if (!this.map.has(bindingKey)) {
  19205. this.generateSharedContextVar(retrievalLevel);
  19206. }
  19207. // Shared context variables are always generated as "ReadVarExpr".
  19208. return this.map.get(bindingKey).lhs;
  19209. }
  19210. getSharedContextName(retrievalLevel) {
  19211. const sharedCtxObj = this.map.get(SHARED_CONTEXT_KEY + retrievalLevel);
  19212. // Shared context variables are always generated as "ReadVarExpr".
  19213. return sharedCtxObj && sharedCtxObj.declare ? sharedCtxObj.lhs : null;
  19214. }
  19215. maybeGenerateSharedContextVar(value) {
  19216. if (value.priority === 1 /* CONTEXT */ &&
  19217. value.retrievalLevel < this.bindingLevel) {
  19218. const sharedCtxObj = this.map.get(SHARED_CONTEXT_KEY + value.retrievalLevel);
  19219. if (sharedCtxObj) {
  19220. sharedCtxObj.declare = true;
  19221. }
  19222. else {
  19223. this.generateSharedContextVar(value.retrievalLevel);
  19224. }
  19225. }
  19226. }
  19227. generateSharedContextVar(retrievalLevel) {
  19228. const lhs = variable(CONTEXT_NAME + this.freshReferenceName());
  19229. this.map.set(SHARED_CONTEXT_KEY + retrievalLevel, {
  19230. retrievalLevel: retrievalLevel,
  19231. lhs: lhs,
  19232. declareLocalCallback: (scope, relativeLevel) => {
  19233. // const ctx_r0 = nextContext(2);
  19234. return [lhs.set(generateNextContextExpr(relativeLevel)).toConstDecl()];
  19235. },
  19236. declare: false,
  19237. priority: 2 /* SHARED_CONTEXT */,
  19238. localRef: false
  19239. });
  19240. }
  19241. getComponentProperty(name) {
  19242. const componentValue = this.map.get(SHARED_CONTEXT_KEY + 0);
  19243. componentValue.declare = true;
  19244. this.maybeRestoreView(0, false);
  19245. return componentValue.lhs.prop(name);
  19246. }
  19247. maybeRestoreView(retrievalLevel, localRefLookup) {
  19248. // We want to restore the current view in listener fns if:
  19249. // 1 - we are accessing a value in a parent view, which requires walking the view tree rather
  19250. // than using the ctx arg. In this case, the retrieval and binding level will be different.
  19251. // 2 - we are looking up a local ref, which requires restoring the view where the local
  19252. // ref is stored
  19253. if (this.isListenerScope() && (retrievalLevel < this.bindingLevel || localRefLookup)) {
  19254. if (!this.parent.restoreViewVariable) {
  19255. // parent saves variable to generate a shared `const $s$ = getCurrentView();` instruction
  19256. this.parent.restoreViewVariable = variable(this.parent.freshReferenceName());
  19257. }
  19258. this.restoreViewVariable = this.parent.restoreViewVariable;
  19259. }
  19260. }
  19261. restoreViewStatement() {
  19262. const statements = [];
  19263. if (this.restoreViewVariable) {
  19264. const restoreCall = instruction(null, Identifiers.restoreView, [this.restoreViewVariable]);
  19265. // Either `const restoredCtx = restoreView($state$);` or `restoreView($state$);`
  19266. // depending on whether it is being used.
  19267. statements.push(this.usesRestoredViewContext ?
  19268. variable(RESTORED_VIEW_CONTEXT_NAME).set(restoreCall).toConstDecl() :
  19269. restoreCall.toStmt());
  19270. }
  19271. return statements;
  19272. }
  19273. viewSnapshotStatements() {
  19274. // const $state$ = getCurrentView();
  19275. return this.restoreViewVariable ?
  19276. [this.restoreViewVariable.set(instruction(null, Identifiers.getCurrentView, [])).toConstDecl()] :
  19277. [];
  19278. }
  19279. isListenerScope() {
  19280. return this.parent && this.parent.bindingLevel === this.bindingLevel;
  19281. }
  19282. variableDeclarations() {
  19283. let currentContextLevel = 0;
  19284. return Array.from(this.map.values())
  19285. .filter(value => value.declare)
  19286. .sort((a, b) => b.retrievalLevel - a.retrievalLevel || b.priority - a.priority)
  19287. .reduce((stmts, value) => {
  19288. const levelDiff = this.bindingLevel - value.retrievalLevel;
  19289. const currStmts = value.declareLocalCallback(this, levelDiff - currentContextLevel);
  19290. currentContextLevel = levelDiff;
  19291. return stmts.concat(currStmts);
  19292. }, []);
  19293. }
  19294. freshReferenceName() {
  19295. let current = this;
  19296. // Find the top scope as it maintains the global reference count
  19297. while (current.parent)
  19298. current = current.parent;
  19299. const ref = `${REFERENCE_PREFIX}${current.referenceNameIndex++}`;
  19300. return ref;
  19301. }
  19302. hasRestoreViewVariable() {
  19303. return !!this.restoreViewVariable;
  19304. }
  19305. notifyRestoredViewContextUse() {
  19306. this.usesRestoredViewContext = true;
  19307. }
  19308. }
  19309. /**
  19310. * Creates a `CssSelector` given a tag name and a map of attributes
  19311. */
  19312. function createCssSelector(elementName, attributes) {
  19313. const cssSelector = new CssSelector();
  19314. const elementNameNoNs = splitNsName(elementName)[1];
  19315. cssSelector.setElement(elementNameNoNs);
  19316. Object.getOwnPropertyNames(attributes).forEach((name) => {
  19317. const nameNoNs = splitNsName(name)[1];
  19318. const value = attributes[name];
  19319. cssSelector.addAttribute(nameNoNs, value);
  19320. if (name.toLowerCase() === 'class') {
  19321. const classes = value.trim().split(/\s+/);
  19322. classes.forEach(className => cssSelector.addClassName(className));
  19323. }
  19324. });
  19325. return cssSelector;
  19326. }
  19327. /**
  19328. * Creates an array of expressions out of an `ngProjectAs` attributes
  19329. * which can be added to the instruction parameters.
  19330. */
  19331. function getNgProjectAsLiteral(attribute) {
  19332. // Parse the attribute value into a CssSelectorList. Note that we only take the
  19333. // first selector, because we don't support multiple selectors in ngProjectAs.
  19334. const parsedR3Selector = parseSelectorToR3Selector(attribute.value)[0];
  19335. return [literal(5 /* ProjectAs */), asLiteral(parsedR3Selector)];
  19336. }
  19337. /**
  19338. * Gets the instruction to generate for an interpolated property
  19339. * @param interpolation An Interpolation AST
  19340. */
  19341. function getPropertyInterpolationExpression(interpolation) {
  19342. switch (getInterpolationArgsLength(interpolation)) {
  19343. case 1:
  19344. return Identifiers.propertyInterpolate;
  19345. case 3:
  19346. return Identifiers.propertyInterpolate1;
  19347. case 5:
  19348. return Identifiers.propertyInterpolate2;
  19349. case 7:
  19350. return Identifiers.propertyInterpolate3;
  19351. case 9:
  19352. return Identifiers.propertyInterpolate4;
  19353. case 11:
  19354. return Identifiers.propertyInterpolate5;
  19355. case 13:
  19356. return Identifiers.propertyInterpolate6;
  19357. case 15:
  19358. return Identifiers.propertyInterpolate7;
  19359. case 17:
  19360. return Identifiers.propertyInterpolate8;
  19361. default:
  19362. return Identifiers.propertyInterpolateV;
  19363. }
  19364. }
  19365. /**
  19366. * Gets the instruction to generate for an interpolated attribute
  19367. * @param interpolation An Interpolation AST
  19368. */
  19369. function getAttributeInterpolationExpression(interpolation) {
  19370. switch (getInterpolationArgsLength(interpolation)) {
  19371. case 3:
  19372. return Identifiers.attributeInterpolate1;
  19373. case 5:
  19374. return Identifiers.attributeInterpolate2;
  19375. case 7:
  19376. return Identifiers.attributeInterpolate3;
  19377. case 9:
  19378. return Identifiers.attributeInterpolate4;
  19379. case 11:
  19380. return Identifiers.attributeInterpolate5;
  19381. case 13:
  19382. return Identifiers.attributeInterpolate6;
  19383. case 15:
  19384. return Identifiers.attributeInterpolate7;
  19385. case 17:
  19386. return Identifiers.attributeInterpolate8;
  19387. default:
  19388. return Identifiers.attributeInterpolateV;
  19389. }
  19390. }
  19391. /**
  19392. * Gets the instruction to generate for interpolated text.
  19393. * @param interpolation An Interpolation AST
  19394. */
  19395. function getTextInterpolationExpression(interpolation) {
  19396. switch (getInterpolationArgsLength(interpolation)) {
  19397. case 1:
  19398. return Identifiers.textInterpolate;
  19399. case 3:
  19400. return Identifiers.textInterpolate1;
  19401. case 5:
  19402. return Identifiers.textInterpolate2;
  19403. case 7:
  19404. return Identifiers.textInterpolate3;
  19405. case 9:
  19406. return Identifiers.textInterpolate4;
  19407. case 11:
  19408. return Identifiers.textInterpolate5;
  19409. case 13:
  19410. return Identifiers.textInterpolate6;
  19411. case 15:
  19412. return Identifiers.textInterpolate7;
  19413. case 17:
  19414. return Identifiers.textInterpolate8;
  19415. default:
  19416. return Identifiers.textInterpolateV;
  19417. }
  19418. }
  19419. /**
  19420. * Parse a template into render3 `Node`s and additional metadata, with no other dependencies.
  19421. *
  19422. * @param template text of the template to parse
  19423. * @param templateUrl URL to use for source mapping of the parsed template
  19424. * @param options options to modify how the template is parsed
  19425. */
  19426. function parseTemplate(template, templateUrl, options = {}) {
  19427. const { interpolationConfig, preserveWhitespaces, enableI18nLegacyMessageIdFormat } = options;
  19428. const bindingParser = makeBindingParser(interpolationConfig);
  19429. const htmlParser = new HtmlParser();
  19430. const parseResult = htmlParser.parse(template, templateUrl, Object.assign(Object.assign({ leadingTriviaChars: LEADING_TRIVIA_CHARS }, options), { tokenizeExpansionForms: true }));
  19431. if (!options.alwaysAttemptHtmlToR3AstConversion && parseResult.errors &&
  19432. parseResult.errors.length > 0) {
  19433. const parsedTemplate = {
  19434. interpolationConfig,
  19435. preserveWhitespaces,
  19436. errors: parseResult.errors,
  19437. nodes: [],
  19438. styleUrls: [],
  19439. styles: [],
  19440. ngContentSelectors: []
  19441. };
  19442. if (options.collectCommentNodes) {
  19443. parsedTemplate.commentNodes = [];
  19444. }
  19445. return parsedTemplate;
  19446. }
  19447. let rootNodes = parseResult.rootNodes;
  19448. // process i18n meta information (scan attributes, generate ids)
  19449. // before we run whitespace removal process, because existing i18n
  19450. // extraction process (ng extract-i18n) relies on a raw content to generate
  19451. // message ids
  19452. const i18nMetaVisitor = new I18nMetaVisitor(interpolationConfig, /* keepI18nAttrs */ !preserveWhitespaces, enableI18nLegacyMessageIdFormat);
  19453. const i18nMetaResult = i18nMetaVisitor.visitAllWithErrors(rootNodes);
  19454. if (!options.alwaysAttemptHtmlToR3AstConversion && i18nMetaResult.errors &&
  19455. i18nMetaResult.errors.length > 0) {
  19456. const parsedTemplate = {
  19457. interpolationConfig,
  19458. preserveWhitespaces,
  19459. errors: i18nMetaResult.errors,
  19460. nodes: [],
  19461. styleUrls: [],
  19462. styles: [],
  19463. ngContentSelectors: []
  19464. };
  19465. if (options.collectCommentNodes) {
  19466. parsedTemplate.commentNodes = [];
  19467. }
  19468. return parsedTemplate;
  19469. }
  19470. rootNodes = i18nMetaResult.rootNodes;
  19471. if (!preserveWhitespaces) {
  19472. rootNodes = visitAll$1(new WhitespaceVisitor(), rootNodes);
  19473. // run i18n meta visitor again in case whitespaces are removed (because that might affect
  19474. // generated i18n message content) and first pass indicated that i18n content is present in a
  19475. // template. During this pass i18n IDs generated at the first pass will be preserved, so we can
  19476. // mimic existing extraction process (ng extract-i18n)
  19477. if (i18nMetaVisitor.hasI18nMeta) {
  19478. rootNodes = visitAll$1(new I18nMetaVisitor(interpolationConfig, /* keepI18nAttrs */ false), rootNodes);
  19479. }
  19480. }
  19481. const { nodes, errors, styleUrls, styles, ngContentSelectors, commentNodes } = htmlAstToRender3Ast(rootNodes, bindingParser, { collectCommentNodes: !!options.collectCommentNodes });
  19482. errors.push(...parseResult.errors, ...i18nMetaResult.errors);
  19483. const parsedTemplate = {
  19484. interpolationConfig,
  19485. preserveWhitespaces,
  19486. errors: errors.length > 0 ? errors : null,
  19487. nodes,
  19488. styleUrls,
  19489. styles,
  19490. ngContentSelectors
  19491. };
  19492. if (options.collectCommentNodes) {
  19493. parsedTemplate.commentNodes = commentNodes;
  19494. }
  19495. return parsedTemplate;
  19496. }
  19497. const elementRegistry = new DomElementSchemaRegistry();
  19498. /**
  19499. * Construct a `BindingParser` with a default configuration.
  19500. */
  19501. function makeBindingParser(interpolationConfig = DEFAULT_INTERPOLATION_CONFIG) {
  19502. return new BindingParser(new IvyParser(new Lexer()), interpolationConfig, elementRegistry, null, []);
  19503. }
  19504. function resolveSanitizationFn(context, isAttribute) {
  19505. switch (context) {
  19506. case SecurityContext.HTML:
  19507. return importExpr(Identifiers.sanitizeHtml);
  19508. case SecurityContext.SCRIPT:
  19509. return importExpr(Identifiers.sanitizeScript);
  19510. case SecurityContext.STYLE:
  19511. // the compiler does not fill in an instruction for [style.prop?] binding
  19512. // values because the style algorithm knows internally what props are subject
  19513. // to sanitization (only [attr.style] values are explicitly sanitized)
  19514. return isAttribute ? importExpr(Identifiers.sanitizeStyle) : null;
  19515. case SecurityContext.URL:
  19516. return importExpr(Identifiers.sanitizeUrl);
  19517. case SecurityContext.RESOURCE_URL:
  19518. return importExpr(Identifiers.sanitizeResourceUrl);
  19519. default:
  19520. return null;
  19521. }
  19522. }
  19523. function trustedConstAttribute(tagName, attr) {
  19524. const value = asLiteral(attr.value);
  19525. if (isTrustedTypesSink(tagName, attr.name)) {
  19526. switch (elementRegistry.securityContext(tagName, attr.name, /* isAttribute */ true)) {
  19527. case SecurityContext.HTML:
  19528. return taggedTemplate(importExpr(Identifiers.trustConstantHtml), new TemplateLiteral([new TemplateLiteralElement(attr.value)], []), undefined, attr.valueSpan);
  19529. // NB: no SecurityContext.SCRIPT here, as the corresponding tags are stripped by the compiler.
  19530. case SecurityContext.RESOURCE_URL:
  19531. return taggedTemplate(importExpr(Identifiers.trustConstantResourceUrl), new TemplateLiteral([new TemplateLiteralElement(attr.value)], []), undefined, attr.valueSpan);
  19532. default:
  19533. return value;
  19534. }
  19535. }
  19536. else {
  19537. return value;
  19538. }
  19539. }
  19540. function isSingleElementTemplate(children) {
  19541. return children.length === 1 && children[0] instanceof Element;
  19542. }
  19543. function isTextNode(node) {
  19544. return node instanceof Text || node instanceof BoundText || node instanceof Icu;
  19545. }
  19546. function hasTextChildrenOnly(children) {
  19547. return children.every(isTextNode);
  19548. }
  19549. /** Name of the global variable that is used to determine if we use Closure translations or not */
  19550. const NG_I18N_CLOSURE_MODE = 'ngI18nClosureMode';
  19551. /**
  19552. * Generate statements that define a given translation message.
  19553. *
  19554. * ```
  19555. * var I18N_1;
  19556. * if (typeof ngI18nClosureMode !== undefined && ngI18nClosureMode) {
  19557. * var MSG_EXTERNAL_XXX = goog.getMsg(
  19558. * "Some message with {$interpolation}!",
  19559. * { "interpolation": "\uFFFD0\uFFFD" }
  19560. * );
  19561. * I18N_1 = MSG_EXTERNAL_XXX;
  19562. * }
  19563. * else {
  19564. * I18N_1 = $localize`Some message with ${'\uFFFD0\uFFFD'}!`;
  19565. * }
  19566. * ```
  19567. *
  19568. * @param message The original i18n AST message node
  19569. * @param variable The variable that will be assigned the translation, e.g. `I18N_1`.
  19570. * @param closureVar The variable for Closure `goog.getMsg` calls, e.g. `MSG_EXTERNAL_XXX`.
  19571. * @param params Object mapping placeholder names to their values (e.g.
  19572. * `{ "interpolation": "\uFFFD0\uFFFD" }`).
  19573. * @param transformFn Optional transformation function that will be applied to the translation (e.g.
  19574. * post-processing).
  19575. * @returns An array of statements that defined a given translation.
  19576. */
  19577. function getTranslationDeclStmts(message, variable, closureVar, params = {}, transformFn) {
  19578. const statements = [
  19579. declareI18nVariable(variable),
  19580. ifStmt(createClosureModeGuard(), createGoogleGetMsgStatements(variable, message, closureVar, i18nFormatPlaceholderNames(params, /* useCamelCase */ true)), createLocalizeStatements(variable, message, i18nFormatPlaceholderNames(params, /* useCamelCase */ false))),
  19581. ];
  19582. if (transformFn) {
  19583. statements.push(new ExpressionStatement(variable.set(transformFn(variable))));
  19584. }
  19585. return statements;
  19586. }
  19587. /**
  19588. * Create the expression that will be used to guard the closure mode block
  19589. * It is equivalent to:
  19590. *
  19591. * ```
  19592. * typeof ngI18nClosureMode !== undefined && ngI18nClosureMode
  19593. * ```
  19594. */
  19595. function createClosureModeGuard() {
  19596. return typeofExpr(variable(NG_I18N_CLOSURE_MODE))
  19597. .notIdentical(literal('undefined', STRING_TYPE))
  19598. .and(variable(NG_I18N_CLOSURE_MODE));
  19599. }
  19600. /**
  19601. * @license
  19602. * Copyright Google LLC All Rights Reserved.
  19603. *
  19604. * Use of this source code is governed by an MIT-style license that can be
  19605. * found in the LICENSE file at https://angular.io/license
  19606. */
  19607. // This regex matches any binding names that contain the "attr." prefix, e.g. "attr.required"
  19608. // If there is a match, the first matching group will contain the attribute name to bind.
  19609. const ATTR_REGEX = /attr\.([^\]]+)/;
  19610. function baseDirectiveFields(meta, constantPool, bindingParser) {
  19611. const definitionMap = new DefinitionMap();
  19612. const selectors = parseSelectorToR3Selector(meta.selector);
  19613. // e.g. `type: MyDirective`
  19614. definitionMap.set('type', meta.internalType);
  19615. // e.g. `selectors: [['', 'someDir', '']]`
  19616. if (selectors.length > 0) {
  19617. definitionMap.set('selectors', asLiteral(selectors));
  19618. }
  19619. if (meta.queries.length > 0) {
  19620. // e.g. `contentQueries: (rf, ctx, dirIndex) => { ... }
  19621. definitionMap.set('contentQueries', createContentQueriesFunction(meta.queries, constantPool, meta.name));
  19622. }
  19623. if (meta.viewQueries.length) {
  19624. definitionMap.set('viewQuery', createViewQueriesFunction(meta.viewQueries, constantPool, meta.name));
  19625. }
  19626. // e.g. `hostBindings: (rf, ctx) => { ... }
  19627. definitionMap.set('hostBindings', createHostBindingsFunction(meta.host, meta.typeSourceSpan, bindingParser, constantPool, meta.selector || '', meta.name, definitionMap));
  19628. // e.g 'inputs: {a: 'a'}`
  19629. definitionMap.set('inputs', conditionallyCreateMapObjectLiteral(meta.inputs, true));
  19630. // e.g 'outputs: {a: 'a'}`
  19631. definitionMap.set('outputs', conditionallyCreateMapObjectLiteral(meta.outputs));
  19632. if (meta.exportAs !== null) {
  19633. definitionMap.set('exportAs', literalArr(meta.exportAs.map(e => literal(e))));
  19634. }
  19635. return definitionMap;
  19636. }
  19637. /**
  19638. * Add features to the definition map.
  19639. */
  19640. function addFeatures(definitionMap, meta) {
  19641. // e.g. `features: [NgOnChangesFeature]`
  19642. const features = [];
  19643. const providers = meta.providers;
  19644. const viewProviders = meta.viewProviders;
  19645. if (providers || viewProviders) {
  19646. const args = [providers || new LiteralArrayExpr([])];
  19647. if (viewProviders) {
  19648. args.push(viewProviders);
  19649. }
  19650. features.push(importExpr(Identifiers.ProvidersFeature).callFn(args));
  19651. }
  19652. if (meta.usesInheritance) {
  19653. features.push(importExpr(Identifiers.InheritDefinitionFeature));
  19654. }
  19655. if (meta.fullInheritance) {
  19656. features.push(importExpr(Identifiers.CopyDefinitionFeature));
  19657. }
  19658. if (meta.lifecycle.usesOnChanges) {
  19659. features.push(importExpr(Identifiers.NgOnChangesFeature));
  19660. }
  19661. if (features.length) {
  19662. definitionMap.set('features', literalArr(features));
  19663. }
  19664. }
  19665. /**
  19666. * Compile a directive for the render3 runtime as defined by the `R3DirectiveMetadata`.
  19667. */
  19668. function compileDirectiveFromMetadata(meta, constantPool, bindingParser) {
  19669. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  19670. addFeatures(definitionMap, meta);
  19671. const expression = importExpr(Identifiers.defineDirective).callFn([definitionMap.toLiteralMap()], undefined, true);
  19672. const type = createDirectiveType(meta);
  19673. return { expression, type, statements: [] };
  19674. }
  19675. /**
  19676. * Compile a component for the render3 runtime as defined by the `R3ComponentMetadata`.
  19677. */
  19678. function compileComponentFromMetadata(meta, constantPool, bindingParser) {
  19679. const definitionMap = baseDirectiveFields(meta, constantPool, bindingParser);
  19680. addFeatures(definitionMap, meta);
  19681. const selector = meta.selector && CssSelector.parse(meta.selector);
  19682. const firstSelector = selector && selector[0];
  19683. // e.g. `attr: ["class", ".my.app"]`
  19684. // This is optional an only included if the first selector of a component specifies attributes.
  19685. if (firstSelector) {
  19686. const selectorAttributes = firstSelector.getAttrs();
  19687. if (selectorAttributes.length) {
  19688. definitionMap.set('attrs', constantPool.getConstLiteral(literalArr(selectorAttributes.map(value => value != null ? literal(value) : literal(undefined))),
  19689. /* forceShared */ true));
  19690. }
  19691. }
  19692. // Generate the CSS matcher that recognize directive
  19693. let directiveMatcher = null;
  19694. if (meta.directives.length > 0) {
  19695. const matcher = new SelectorMatcher();
  19696. for (const { selector, type } of meta.directives) {
  19697. matcher.addSelectables(CssSelector.parse(selector), type);
  19698. }
  19699. directiveMatcher = matcher;
  19700. }
  19701. // e.g. `template: function MyComponent_Template(_ctx, _cm) {...}`
  19702. const templateTypeName = meta.name;
  19703. const templateName = templateTypeName ? `${templateTypeName}_Template` : null;
  19704. const directivesUsed = new Set();
  19705. const pipesUsed = new Set();
  19706. const changeDetection = meta.changeDetection;
  19707. const template = meta.template;
  19708. const templateBuilder = new TemplateDefinitionBuilder(constantPool, BindingScope.createRootScope(), 0, templateTypeName, null, null, templateName, directiveMatcher, directivesUsed, meta.pipes, pipesUsed, Identifiers.namespaceHTML, meta.relativeContextFilePath, meta.i18nUseExternalIds);
  19709. const templateFunctionExpression = templateBuilder.buildTemplateFunction(template.nodes, []);
  19710. // We need to provide this so that dynamically generated components know what
  19711. // projected content blocks to pass through to the component when it is instantiated.
  19712. const ngContentSelectors = templateBuilder.getNgContentSelectors();
  19713. if (ngContentSelectors) {
  19714. definitionMap.set('ngContentSelectors', ngContentSelectors);
  19715. }
  19716. // e.g. `decls: 2`
  19717. definitionMap.set('decls', literal(templateBuilder.getConstCount()));
  19718. // e.g. `vars: 2`
  19719. definitionMap.set('vars', literal(templateBuilder.getVarCount()));
  19720. // Generate `consts` section of ComponentDef:
  19721. // - either as an array:
  19722. // `consts: [['one', 'two'], ['three', 'four']]`
  19723. // - or as a factory function in case additional statements are present (to support i18n):
  19724. // `consts: function() { var i18n_0; if (ngI18nClosureMode) {...} else {...} return [i18n_0]; }`
  19725. const { constExpressions, prepareStatements } = templateBuilder.getConsts();
  19726. if (constExpressions.length > 0) {
  19727. let constsExpr = literalArr(constExpressions);
  19728. // Prepare statements are present - turn `consts` into a function.
  19729. if (prepareStatements.length > 0) {
  19730. constsExpr = fn([], [...prepareStatements, new ReturnStatement(constsExpr)]);
  19731. }
  19732. definitionMap.set('consts', constsExpr);
  19733. }
  19734. definitionMap.set('template', templateFunctionExpression);
  19735. // e.g. `directives: [MyDirective]`
  19736. if (directivesUsed.size) {
  19737. const directivesList = literalArr(Array.from(directivesUsed));
  19738. const directivesExpr = compileDeclarationList(directivesList, meta.declarationListEmitMode);
  19739. definitionMap.set('directives', directivesExpr);
  19740. }
  19741. // e.g. `pipes: [MyPipe]`
  19742. if (pipesUsed.size) {
  19743. const pipesList = literalArr(Array.from(pipesUsed));
  19744. const pipesExpr = compileDeclarationList(pipesList, meta.declarationListEmitMode);
  19745. definitionMap.set('pipes', pipesExpr);
  19746. }
  19747. if (meta.encapsulation === null) {
  19748. meta.encapsulation = ViewEncapsulation.Emulated;
  19749. }
  19750. // e.g. `styles: [str1, str2]`
  19751. if (meta.styles && meta.styles.length) {
  19752. const styleValues = meta.encapsulation == ViewEncapsulation.Emulated ?
  19753. compileStyles(meta.styles, CONTENT_ATTR, HOST_ATTR) :
  19754. meta.styles;
  19755. const strings = styleValues.map(str => constantPool.getConstLiteral(literal(str)));
  19756. definitionMap.set('styles', literalArr(strings));
  19757. }
  19758. else if (meta.encapsulation === ViewEncapsulation.Emulated) {
  19759. // If there is no style, don't generate css selectors on elements
  19760. meta.encapsulation = ViewEncapsulation.None;
  19761. }
  19762. // Only set view encapsulation if it's not the default value
  19763. if (meta.encapsulation !== ViewEncapsulation.Emulated) {
  19764. definitionMap.set('encapsulation', literal(meta.encapsulation));
  19765. }
  19766. // e.g. `animation: [trigger('123', [])]`
  19767. if (meta.animations !== null) {
  19768. definitionMap.set('data', literalMap([{ key: 'animation', value: meta.animations, quoted: false }]));
  19769. }
  19770. // Only set the change detection flag if it's defined and it's not the default.
  19771. if (changeDetection != null && changeDetection !== ChangeDetectionStrategy.Default) {
  19772. definitionMap.set('changeDetection', literal(changeDetection));
  19773. }
  19774. const expression = importExpr(Identifiers.defineComponent).callFn([definitionMap.toLiteralMap()], undefined, true);
  19775. const type = createComponentType(meta);
  19776. return { expression, type, statements: [] };
  19777. }
  19778. /**
  19779. * Creates the type specification from the component meta. This type is inserted into .d.ts files
  19780. * to be consumed by upstream compilations.
  19781. */
  19782. function createComponentType(meta) {
  19783. const typeParams = createDirectiveTypeParams(meta);
  19784. typeParams.push(stringArrayAsType(meta.template.ngContentSelectors));
  19785. return expressionType(importExpr(Identifiers.ComponentDeclaration, typeParams));
  19786. }
  19787. /**
  19788. * Compiles the array literal of declarations into an expression according to the provided emit
  19789. * mode.
  19790. */
  19791. function compileDeclarationList(list, mode) {
  19792. switch (mode) {
  19793. case 0 /* Direct */:
  19794. // directives: [MyDir],
  19795. return list;
  19796. case 1 /* Closure */:
  19797. // directives: function () { return [MyDir]; }
  19798. return fn([], [new ReturnStatement(list)]);
  19799. case 2 /* ClosureResolved */:
  19800. // directives: function () { return [MyDir].map(ng.resolveForwardRef); }
  19801. const resolvedList = list.callMethod('map', [importExpr(Identifiers.resolveForwardRef)]);
  19802. return fn([], [new ReturnStatement(resolvedList)]);
  19803. }
  19804. }
  19805. function prepareQueryParams(query, constantPool) {
  19806. const parameters = [getQueryPredicate(query, constantPool), literal(toQueryFlags(query))];
  19807. if (query.read) {
  19808. parameters.push(query.read);
  19809. }
  19810. return parameters;
  19811. }
  19812. /**
  19813. * Translates query flags into `TQueryFlags` type in packages/core/src/render3/interfaces/query.ts
  19814. * @param query
  19815. */
  19816. function toQueryFlags(query) {
  19817. return (query.descendants ? 1 /* descendants */ : 0 /* none */) |
  19818. (query.static ? 2 /* isStatic */ : 0 /* none */) |
  19819. (query.emitDistinctChangesOnly ? 4 /* emitDistinctChangesOnly */ : 0 /* none */);
  19820. }
  19821. function convertAttributesToExpressions(attributes) {
  19822. const values = [];
  19823. for (let key of Object.getOwnPropertyNames(attributes)) {
  19824. const value = attributes[key];
  19825. values.push(literal(key), value);
  19826. }
  19827. return values;
  19828. }
  19829. // Define and update any content queries
  19830. function createContentQueriesFunction(queries, constantPool, name) {
  19831. const createStatements = [];
  19832. const updateStatements = [];
  19833. const tempAllocator = temporaryAllocator(updateStatements, TEMPORARY_NAME);
  19834. for (const query of queries) {
  19835. // creation, e.g. r3.contentQuery(dirIndex, somePredicate, true, null);
  19836. createStatements.push(importExpr(Identifiers.contentQuery)
  19837. .callFn([variable('dirIndex'), ...prepareQueryParams(query, constantPool)])
  19838. .toStmt());
  19839. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  19840. const temporary = tempAllocator();
  19841. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  19842. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  19843. const updateDirective = variable(CONTEXT_NAME)
  19844. .prop(query.propertyName)
  19845. .set(query.first ? temporary.prop('first') : temporary);
  19846. updateStatements.push(refresh.and(updateDirective).toStmt());
  19847. }
  19848. const contentQueriesFnName = name ? `${name}_ContentQueries` : null;
  19849. return fn([
  19850. new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null),
  19851. new FnParam('dirIndex', null)
  19852. ], [
  19853. renderFlagCheckIfStmt(1 /* Create */, createStatements),
  19854. renderFlagCheckIfStmt(2 /* Update */, updateStatements)
  19855. ], INFERRED_TYPE, null, contentQueriesFnName);
  19856. }
  19857. function stringAsType(str) {
  19858. return expressionType(literal(str));
  19859. }
  19860. function stringMapAsType(map) {
  19861. const mapValues = Object.keys(map).map(key => {
  19862. const value = Array.isArray(map[key]) ? map[key][0] : map[key];
  19863. return {
  19864. key,
  19865. value: literal(value),
  19866. quoted: true,
  19867. };
  19868. });
  19869. return expressionType(literalMap(mapValues));
  19870. }
  19871. function stringArrayAsType(arr) {
  19872. return arr.length > 0 ? expressionType(literalArr(arr.map(value => literal(value)))) :
  19873. NONE_TYPE;
  19874. }
  19875. function createDirectiveTypeParams(meta) {
  19876. // On the type side, remove newlines from the selector as it will need to fit into a TypeScript
  19877. // string literal, which must be on one line.
  19878. const selectorForType = meta.selector !== null ? meta.selector.replace(/\n/g, '') : null;
  19879. return [
  19880. typeWithParameters(meta.type.type, meta.typeArgumentCount),
  19881. selectorForType !== null ? stringAsType(selectorForType) : NONE_TYPE,
  19882. meta.exportAs !== null ? stringArrayAsType(meta.exportAs) : NONE_TYPE,
  19883. stringMapAsType(meta.inputs),
  19884. stringMapAsType(meta.outputs),
  19885. stringArrayAsType(meta.queries.map(q => q.propertyName)),
  19886. ];
  19887. }
  19888. /**
  19889. * Creates the type specification from the directive meta. This type is inserted into .d.ts files
  19890. * to be consumed by upstream compilations.
  19891. */
  19892. function createDirectiveType(meta) {
  19893. const typeParams = createDirectiveTypeParams(meta);
  19894. return expressionType(importExpr(Identifiers.DirectiveDeclaration, typeParams));
  19895. }
  19896. // Define and update any view queries
  19897. function createViewQueriesFunction(viewQueries, constantPool, name) {
  19898. const createStatements = [];
  19899. const updateStatements = [];
  19900. const tempAllocator = temporaryAllocator(updateStatements, TEMPORARY_NAME);
  19901. viewQueries.forEach((query) => {
  19902. // creation, e.g. r3.viewQuery(somePredicate, true);
  19903. const queryDefinition = importExpr(Identifiers.viewQuery).callFn(prepareQueryParams(query, constantPool));
  19904. createStatements.push(queryDefinition.toStmt());
  19905. // update, e.g. (r3.queryRefresh(tmp = r3.loadQuery()) && (ctx.someDir = tmp));
  19906. const temporary = tempAllocator();
  19907. const getQueryList = importExpr(Identifiers.loadQuery).callFn([]);
  19908. const refresh = importExpr(Identifiers.queryRefresh).callFn([temporary.set(getQueryList)]);
  19909. const updateDirective = variable(CONTEXT_NAME)
  19910. .prop(query.propertyName)
  19911. .set(query.first ? temporary.prop('first') : temporary);
  19912. updateStatements.push(refresh.and(updateDirective).toStmt());
  19913. });
  19914. const viewQueryFnName = name ? `${name}_Query` : null;
  19915. return fn([new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], [
  19916. renderFlagCheckIfStmt(1 /* Create */, createStatements),
  19917. renderFlagCheckIfStmt(2 /* Update */, updateStatements)
  19918. ], INFERRED_TYPE, null, viewQueryFnName);
  19919. }
  19920. // Return a host binding function or null if one is not necessary.
  19921. function createHostBindingsFunction(hostBindingsMetadata, typeSourceSpan, bindingParser, constantPool, selector, name, definitionMap) {
  19922. const bindingContext = variable(CONTEXT_NAME);
  19923. const styleBuilder = new StylingBuilder(bindingContext);
  19924. const { styleAttr, classAttr } = hostBindingsMetadata.specialAttributes;
  19925. if (styleAttr !== undefined) {
  19926. styleBuilder.registerStyleAttr(styleAttr);
  19927. }
  19928. if (classAttr !== undefined) {
  19929. styleBuilder.registerClassAttr(classAttr);
  19930. }
  19931. const createStatements = [];
  19932. const updateStatements = [];
  19933. const hostBindingSourceSpan = typeSourceSpan;
  19934. const directiveSummary = metadataAsSummary(hostBindingsMetadata);
  19935. // Calculate host event bindings
  19936. const eventBindings = bindingParser.createDirectiveHostEventAsts(directiveSummary, hostBindingSourceSpan);
  19937. if (eventBindings && eventBindings.length) {
  19938. const listeners = createHostListeners(eventBindings, name);
  19939. createStatements.push(...listeners);
  19940. }
  19941. // Calculate the host property bindings
  19942. const bindings = bindingParser.createBoundHostProperties(directiveSummary, hostBindingSourceSpan);
  19943. const allOtherBindings = [];
  19944. // We need to calculate the total amount of binding slots required by
  19945. // all the instructions together before any value conversions happen.
  19946. // Value conversions may require additional slots for interpolation and
  19947. // bindings with pipes. These calculates happen after this block.
  19948. let totalHostVarsCount = 0;
  19949. bindings && bindings.forEach((binding) => {
  19950. const stylingInputWasSet = styleBuilder.registerInputBasedOnName(binding.name, binding.expression, hostBindingSourceSpan);
  19951. if (stylingInputWasSet) {
  19952. totalHostVarsCount += MIN_STYLING_BINDING_SLOTS_REQUIRED;
  19953. }
  19954. else {
  19955. allOtherBindings.push(binding);
  19956. totalHostVarsCount++;
  19957. }
  19958. });
  19959. let valueConverter;
  19960. const getValueConverter = () => {
  19961. if (!valueConverter) {
  19962. const hostVarsCountFn = (numSlots) => {
  19963. const originalVarsCount = totalHostVarsCount;
  19964. totalHostVarsCount += numSlots;
  19965. return originalVarsCount;
  19966. };
  19967. valueConverter = new ValueConverter(constantPool, () => error('Unexpected node'), // new nodes are illegal here
  19968. hostVarsCountFn, () => error('Unexpected pipe')); // pipes are illegal here
  19969. }
  19970. return valueConverter;
  19971. };
  19972. const propertyBindings = [];
  19973. const attributeBindings = [];
  19974. const syntheticHostBindings = [];
  19975. allOtherBindings.forEach((binding) => {
  19976. // resolve literal arrays and literal objects
  19977. const value = binding.expression.visit(getValueConverter());
  19978. const bindingExpr = bindingFn(bindingContext, value);
  19979. const { bindingName, instruction, isAttribute } = getBindingNameAndInstruction(binding);
  19980. const securityContexts = bindingParser.calcPossibleSecurityContexts(selector, bindingName, isAttribute)
  19981. .filter(context => context !== SecurityContext.NONE);
  19982. let sanitizerFn = null;
  19983. if (securityContexts.length) {
  19984. if (securityContexts.length === 2 &&
  19985. securityContexts.indexOf(SecurityContext.URL) > -1 &&
  19986. securityContexts.indexOf(SecurityContext.RESOURCE_URL) > -1) {
  19987. // Special case for some URL attributes (such as "src" and "href") that may be a part
  19988. // of different security contexts. In this case we use special sanitization function and
  19989. // select the actual sanitizer at runtime based on a tag name that is provided while
  19990. // invoking sanitization function.
  19991. sanitizerFn = importExpr(Identifiers.sanitizeUrlOrResourceUrl);
  19992. }
  19993. else {
  19994. sanitizerFn = resolveSanitizationFn(securityContexts[0], isAttribute);
  19995. }
  19996. }
  19997. const instructionParams = [literal(bindingName), bindingExpr.currValExpr];
  19998. if (sanitizerFn) {
  19999. instructionParams.push(sanitizerFn);
  20000. }
  20001. updateStatements.push(...bindingExpr.stmts);
  20002. if (instruction === Identifiers.hostProperty) {
  20003. propertyBindings.push(instructionParams);
  20004. }
  20005. else if (instruction === Identifiers.attribute) {
  20006. attributeBindings.push(instructionParams);
  20007. }
  20008. else if (instruction === Identifiers.syntheticHostProperty) {
  20009. syntheticHostBindings.push(instructionParams);
  20010. }
  20011. else {
  20012. updateStatements.push(importExpr(instruction).callFn(instructionParams).toStmt());
  20013. }
  20014. });
  20015. if (propertyBindings.length > 0) {
  20016. updateStatements.push(chainedInstruction(Identifiers.hostProperty, propertyBindings).toStmt());
  20017. }
  20018. if (attributeBindings.length > 0) {
  20019. updateStatements.push(chainedInstruction(Identifiers.attribute, attributeBindings).toStmt());
  20020. }
  20021. if (syntheticHostBindings.length > 0) {
  20022. updateStatements.push(chainedInstruction(Identifiers.syntheticHostProperty, syntheticHostBindings).toStmt());
  20023. }
  20024. // since we're dealing with directives/components and both have hostBinding
  20025. // functions, we need to generate a special hostAttrs instruction that deals
  20026. // with both the assignment of styling as well as static attributes to the host
  20027. // element. The instruction below will instruct all initial styling (styling
  20028. // that is inside of a host binding within a directive/component) to be attached
  20029. // to the host element alongside any of the provided host attributes that were
  20030. // collected earlier.
  20031. const hostAttrs = convertAttributesToExpressions(hostBindingsMetadata.attributes);
  20032. styleBuilder.assignHostAttrs(hostAttrs, definitionMap);
  20033. if (styleBuilder.hasBindings) {
  20034. // finally each binding that was registered in the statement above will need to be added to
  20035. // the update block of a component/directive templateFn/hostBindingsFn so that the bindings
  20036. // are evaluated and updated for the element.
  20037. styleBuilder.buildUpdateLevelInstructions(getValueConverter()).forEach(instruction => {
  20038. if (instruction.calls.length > 0) {
  20039. const calls = [];
  20040. instruction.calls.forEach(call => {
  20041. // we subtract a value of `1` here because the binding slot was already allocated
  20042. // at the top of this method when all the input bindings were counted.
  20043. totalHostVarsCount +=
  20044. Math.max(call.allocateBindingSlots - MIN_STYLING_BINDING_SLOTS_REQUIRED, 0);
  20045. calls.push(convertStylingCall(call, bindingContext, bindingFn));
  20046. });
  20047. updateStatements.push(chainedInstruction(instruction.reference, calls).toStmt());
  20048. }
  20049. });
  20050. }
  20051. if (totalHostVarsCount) {
  20052. definitionMap.set('hostVars', literal(totalHostVarsCount));
  20053. }
  20054. if (createStatements.length > 0 || updateStatements.length > 0) {
  20055. const hostBindingsFnName = name ? `${name}_HostBindings` : null;
  20056. const statements = [];
  20057. if (createStatements.length > 0) {
  20058. statements.push(renderFlagCheckIfStmt(1 /* Create */, createStatements));
  20059. }
  20060. if (updateStatements.length > 0) {
  20061. statements.push(renderFlagCheckIfStmt(2 /* Update */, updateStatements));
  20062. }
  20063. return fn([new FnParam(RENDER_FLAGS, NUMBER_TYPE), new FnParam(CONTEXT_NAME, null)], statements, INFERRED_TYPE, null, hostBindingsFnName);
  20064. }
  20065. return null;
  20066. }
  20067. function bindingFn(implicit, value) {
  20068. return convertPropertyBinding(null, implicit, value, 'b', BindingForm.Expression, () => error('Unexpected interpolation'));
  20069. }
  20070. function convertStylingCall(call, bindingContext, bindingFn) {
  20071. return call.params(value => bindingFn(bindingContext, value).currValExpr);
  20072. }
  20073. function getBindingNameAndInstruction(binding) {
  20074. let bindingName = binding.name;
  20075. let instruction;
  20076. // Check to see if this is an attr binding or a property binding
  20077. const attrMatches = bindingName.match(ATTR_REGEX);
  20078. if (attrMatches) {
  20079. bindingName = attrMatches[1];
  20080. instruction = Identifiers.attribute;
  20081. }
  20082. else {
  20083. if (binding.isAnimation) {
  20084. bindingName = prepareSyntheticPropertyName(bindingName);
  20085. // host bindings that have a synthetic property (e.g. @foo) should always be rendered
  20086. // in the context of the component and not the parent. Therefore there is a special
  20087. // compatibility instruction available for this purpose.
  20088. instruction = Identifiers.syntheticHostProperty;
  20089. }
  20090. else {
  20091. instruction = Identifiers.hostProperty;
  20092. }
  20093. }
  20094. return { bindingName, instruction, isAttribute: !!attrMatches };
  20095. }
  20096. function createHostListeners(eventBindings, name) {
  20097. const listeners = [];
  20098. const syntheticListeners = [];
  20099. const instructions = [];
  20100. eventBindings.forEach(binding => {
  20101. let bindingName = binding.name && sanitizeIdentifier(binding.name);
  20102. const bindingFnName = binding.type === 1 /* Animation */ ?
  20103. prepareSyntheticListenerFunctionName(bindingName, binding.targetOrPhase) :
  20104. bindingName;
  20105. const handlerName = name && bindingName ? `${name}_${bindingFnName}_HostBindingHandler` : null;
  20106. const params = prepareEventListenerParameters(BoundEvent.fromParsedEvent(binding), handlerName);
  20107. if (binding.type == 1 /* Animation */) {
  20108. syntheticListeners.push(params);
  20109. }
  20110. else {
  20111. listeners.push(params);
  20112. }
  20113. });
  20114. if (syntheticListeners.length > 0) {
  20115. instructions.push(chainedInstruction(Identifiers.syntheticHostListener, syntheticListeners).toStmt());
  20116. }
  20117. if (listeners.length > 0) {
  20118. instructions.push(chainedInstruction(Identifiers.listener, listeners).toStmt());
  20119. }
  20120. return instructions;
  20121. }
  20122. function metadataAsSummary(meta) {
  20123. // clang-format off
  20124. return {
  20125. // This is used by the BindingParser, which only deals with listeners and properties. There's no
  20126. // need to pass attributes to it.
  20127. hostAttributes: {},
  20128. hostListeners: meta.listeners,
  20129. hostProperties: meta.properties,
  20130. };
  20131. // clang-format on
  20132. }
  20133. const HOST_REG_EXP$1 = /^(?:\[([^\]]+)\])|(?:\(([^\)]+)\))$/;
  20134. function parseHostBindings(host) {
  20135. const attributes = {};
  20136. const listeners = {};
  20137. const properties = {};
  20138. const specialAttributes = {};
  20139. for (const key of Object.keys(host)) {
  20140. const value = host[key];
  20141. const matches = key.match(HOST_REG_EXP$1);
  20142. if (matches === null) {
  20143. switch (key) {
  20144. case 'class':
  20145. if (typeof value !== 'string') {
  20146. // TODO(alxhub): make this a diagnostic.
  20147. throw new Error(`Class binding must be string`);
  20148. }
  20149. specialAttributes.classAttr = value;
  20150. break;
  20151. case 'style':
  20152. if (typeof value !== 'string') {
  20153. // TODO(alxhub): make this a diagnostic.
  20154. throw new Error(`Style binding must be string`);
  20155. }
  20156. specialAttributes.styleAttr = value;
  20157. break;
  20158. default:
  20159. if (typeof value === 'string') {
  20160. attributes[key] = literal(value);
  20161. }
  20162. else {
  20163. attributes[key] = value;
  20164. }
  20165. }
  20166. }
  20167. else if (matches[1 /* Binding */] != null) {
  20168. if (typeof value !== 'string') {
  20169. // TODO(alxhub): make this a diagnostic.
  20170. throw new Error(`Property binding must be string`);
  20171. }
  20172. // synthetic properties (the ones that have a `@` as a prefix)
  20173. // are still treated the same as regular properties. Therefore
  20174. // there is no point in storing them in a separate map.
  20175. properties[matches[1 /* Binding */]] = value;
  20176. }
  20177. else if (matches[2 /* Event */] != null) {
  20178. if (typeof value !== 'string') {
  20179. // TODO(alxhub): make this a diagnostic.
  20180. throw new Error(`Event binding must be string`);
  20181. }
  20182. listeners[matches[2 /* Event */]] = value;
  20183. }
  20184. }
  20185. return { attributes, listeners, properties, specialAttributes };
  20186. }
  20187. /**
  20188. * Verifies host bindings and returns the list of errors (if any). Empty array indicates that a
  20189. * given set of host bindings has no errors.
  20190. *
  20191. * @param bindings set of host bindings to verify.
  20192. * @param sourceSpan source span where host bindings were defined.
  20193. * @returns array of errors associated with a given set of host bindings.
  20194. */
  20195. function verifyHostBindings(bindings, sourceSpan) {
  20196. const summary = metadataAsSummary(bindings);
  20197. // TODO: abstract out host bindings verification logic and use it instead of
  20198. // creating events and properties ASTs to detect errors (FW-996)
  20199. const bindingParser = makeBindingParser();
  20200. bindingParser.createDirectiveHostEventAsts(summary, sourceSpan);
  20201. bindingParser.createBoundHostProperties(summary, sourceSpan);
  20202. return bindingParser.errors;
  20203. }
  20204. function compileStyles(styles, selector, hostSelector) {
  20205. const shadowCss = new ShadowCss();
  20206. return styles.map(style => {
  20207. return shadowCss.shimCssText(style, selector, hostSelector);
  20208. });
  20209. }
  20210. /**
  20211. * @license
  20212. * Copyright Google LLC All Rights Reserved.
  20213. *
  20214. * Use of this source code is governed by an MIT-style license that can be
  20215. * found in the LICENSE file at https://angular.io/license
  20216. */
  20217. /**
  20218. * An interface for retrieving documents by URL that the compiler uses
  20219. * to load templates.
  20220. */
  20221. class ResourceLoader {
  20222. get(url) {
  20223. return '';
  20224. }
  20225. }
  20226. /**
  20227. * @license
  20228. * Copyright Google LLC All Rights Reserved.
  20229. *
  20230. * Use of this source code is governed by an MIT-style license that can be
  20231. * found in the LICENSE file at https://angular.io/license
  20232. */
  20233. class CompilerFacadeImpl {
  20234. constructor(jitEvaluator = new JitEvaluator()) {
  20235. this.jitEvaluator = jitEvaluator;
  20236. this.FactoryTarget = FactoryTarget;
  20237. this.ResourceLoader = ResourceLoader;
  20238. this.elementSchemaRegistry = new DomElementSchemaRegistry();
  20239. }
  20240. compilePipe(angularCoreEnv, sourceMapUrl, facade) {
  20241. const metadata = {
  20242. name: facade.name,
  20243. type: wrapReference(facade.type),
  20244. internalType: new WrappedNodeExpr(facade.type),
  20245. typeArgumentCount: 0,
  20246. deps: null,
  20247. pipeName: facade.pipeName,
  20248. pure: facade.pure,
  20249. };
  20250. const res = compilePipeFromMetadata(metadata);
  20251. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  20252. }
  20253. compilePipeDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  20254. const meta = convertDeclarePipeFacadeToMetadata(declaration);
  20255. const res = compilePipeFromMetadata(meta);
  20256. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  20257. }
  20258. compileInjectable(angularCoreEnv, sourceMapUrl, facade) {
  20259. var _a;
  20260. const { expression, statements } = compileInjectable({
  20261. name: facade.name,
  20262. type: wrapReference(facade.type),
  20263. internalType: new WrappedNodeExpr(facade.type),
  20264. typeArgumentCount: facade.typeArgumentCount,
  20265. providedIn: computeProvidedIn(facade.providedIn),
  20266. useClass: convertToProviderExpression(facade, USE_CLASS),
  20267. useFactory: wrapExpression(facade, USE_FACTORY),
  20268. useValue: convertToProviderExpression(facade, USE_VALUE),
  20269. useExisting: convertToProviderExpression(facade, USE_EXISTING),
  20270. deps: (_a = facade.deps) === null || _a === void 0 ? void 0 : _a.map(convertR3DependencyMetadata),
  20271. },
  20272. /* resolveForwardRefs */ true);
  20273. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  20274. }
  20275. compileInjectableDeclaration(angularCoreEnv, sourceMapUrl, facade) {
  20276. var _a;
  20277. const { expression, statements } = compileInjectable({
  20278. name: facade.type.name,
  20279. type: wrapReference(facade.type),
  20280. internalType: new WrappedNodeExpr(facade.type),
  20281. typeArgumentCount: 0,
  20282. providedIn: computeProvidedIn(facade.providedIn),
  20283. useClass: convertToProviderExpression(facade, USE_CLASS),
  20284. useFactory: wrapExpression(facade, USE_FACTORY),
  20285. useValue: convertToProviderExpression(facade, USE_VALUE),
  20286. useExisting: convertToProviderExpression(facade, USE_EXISTING),
  20287. deps: (_a = facade.deps) === null || _a === void 0 ? void 0 : _a.map(convertR3DeclareDependencyMetadata),
  20288. },
  20289. /* resolveForwardRefs */ true);
  20290. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, statements);
  20291. }
  20292. compileInjector(angularCoreEnv, sourceMapUrl, facade) {
  20293. const meta = {
  20294. name: facade.name,
  20295. type: wrapReference(facade.type),
  20296. internalType: new WrappedNodeExpr(facade.type),
  20297. providers: new WrappedNodeExpr(facade.providers),
  20298. imports: facade.imports.map(i => new WrappedNodeExpr(i)),
  20299. };
  20300. const res = compileInjector(meta);
  20301. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  20302. }
  20303. compileInjectorDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  20304. const meta = convertDeclareInjectorFacadeToMetadata(declaration);
  20305. const res = compileInjector(meta);
  20306. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  20307. }
  20308. compileNgModule(angularCoreEnv, sourceMapUrl, facade) {
  20309. const meta = {
  20310. type: wrapReference(facade.type),
  20311. internalType: new WrappedNodeExpr(facade.type),
  20312. adjacentType: new WrappedNodeExpr(facade.type),
  20313. bootstrap: facade.bootstrap.map(wrapReference),
  20314. declarations: facade.declarations.map(wrapReference),
  20315. imports: facade.imports.map(wrapReference),
  20316. exports: facade.exports.map(wrapReference),
  20317. emitInline: true,
  20318. containsForwardDecls: false,
  20319. schemas: facade.schemas ? facade.schemas.map(wrapReference) : null,
  20320. id: facade.id ? new WrappedNodeExpr(facade.id) : null,
  20321. };
  20322. const res = compileNgModule(meta);
  20323. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, []);
  20324. }
  20325. compileNgModuleDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  20326. const expression = compileNgModuleDeclarationExpression(declaration);
  20327. return this.jitExpression(expression, angularCoreEnv, sourceMapUrl, []);
  20328. }
  20329. compileDirective(angularCoreEnv, sourceMapUrl, facade) {
  20330. const meta = convertDirectiveFacadeToMetadata(facade);
  20331. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  20332. }
  20333. compileDirectiveDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  20334. const typeSourceSpan = this.createParseSourceSpan('Directive', declaration.type.name, sourceMapUrl);
  20335. const meta = convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan);
  20336. return this.compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta);
  20337. }
  20338. compileDirectiveFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  20339. const constantPool = new ConstantPool();
  20340. const bindingParser = makeBindingParser();
  20341. const res = compileDirectiveFromMetadata(meta, constantPool, bindingParser);
  20342. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  20343. }
  20344. compileComponent(angularCoreEnv, sourceMapUrl, facade) {
  20345. // Parse the template and check for errors.
  20346. const { template, interpolation } = parseJitTemplate(facade.template, facade.name, sourceMapUrl, facade.preserveWhitespaces, facade.interpolation);
  20347. // Compile the component metadata, including template, into an expression.
  20348. const meta = Object.assign(Object.assign(Object.assign({}, facade), convertDirectiveFacadeToMetadata(facade)), { selector: facade.selector || this.elementSchemaRegistry.getDefaultComponentElementName(), template, declarationListEmitMode: 0 /* Direct */, styles: [...facade.styles, ...template.styles], encapsulation: facade.encapsulation, interpolation, changeDetection: facade.changeDetection, animations: facade.animations != null ? new WrappedNodeExpr(facade.animations) : null, viewProviders: facade.viewProviders != null ? new WrappedNodeExpr(facade.viewProviders) :
  20349. null, relativeContextFilePath: '', i18nUseExternalIds: true });
  20350. const jitExpressionSourceMap = `ng:///${facade.name}.js`;
  20351. return this.compileComponentFromMeta(angularCoreEnv, jitExpressionSourceMap, meta);
  20352. }
  20353. compileComponentDeclaration(angularCoreEnv, sourceMapUrl, declaration) {
  20354. const typeSourceSpan = this.createParseSourceSpan('Component', declaration.type.name, sourceMapUrl);
  20355. const meta = convertDeclareComponentFacadeToMetadata(declaration, typeSourceSpan, sourceMapUrl);
  20356. return this.compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta);
  20357. }
  20358. compileComponentFromMeta(angularCoreEnv, sourceMapUrl, meta) {
  20359. const constantPool = new ConstantPool();
  20360. const bindingParser = makeBindingParser(meta.interpolation);
  20361. const res = compileComponentFromMetadata(meta, constantPool, bindingParser);
  20362. return this.jitExpression(res.expression, angularCoreEnv, sourceMapUrl, constantPool.statements);
  20363. }
  20364. compileFactory(angularCoreEnv, sourceMapUrl, meta) {
  20365. const factoryRes = compileFactoryFunction({
  20366. name: meta.name,
  20367. type: wrapReference(meta.type),
  20368. internalType: new WrappedNodeExpr(meta.type),
  20369. typeArgumentCount: meta.typeArgumentCount,
  20370. deps: convertR3DependencyMetadataArray(meta.deps),
  20371. target: meta.target,
  20372. });
  20373. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  20374. }
  20375. compileFactoryDeclaration(angularCoreEnv, sourceMapUrl, meta) {
  20376. const factoryRes = compileFactoryFunction({
  20377. name: meta.type.name,
  20378. type: wrapReference(meta.type),
  20379. internalType: new WrappedNodeExpr(meta.type),
  20380. typeArgumentCount: 0,
  20381. deps: meta.deps && meta.deps.map(convertR3DeclareDependencyMetadata),
  20382. target: meta.target,
  20383. });
  20384. return this.jitExpression(factoryRes.expression, angularCoreEnv, sourceMapUrl, factoryRes.statements);
  20385. }
  20386. createParseSourceSpan(kind, typeName, sourceUrl) {
  20387. return r3JitTypeSourceSpan(kind, typeName, sourceUrl);
  20388. }
  20389. /**
  20390. * JIT compiles an expression and returns the result of executing that expression.
  20391. *
  20392. * @param def the definition which will be compiled and executed to get the value to patch
  20393. * @param context an object map of @angular/core symbol names to symbols which will be available
  20394. * in the context of the compiled expression
  20395. * @param sourceUrl a URL to use for the source map of the compiled expression
  20396. * @param preStatements a collection of statements that should be evaluated before the expression.
  20397. */
  20398. jitExpression(def, context, sourceUrl, preStatements) {
  20399. // The ConstantPool may contain Statements which declare variables used in the final expression.
  20400. // Therefore, its statements need to precede the actual JIT operation. The final statement is a
  20401. // declaration of $def which is set to the expression being compiled.
  20402. const statements = [
  20403. ...preStatements,
  20404. new DeclareVarStmt('$def', def, undefined, [StmtModifier.Exported]),
  20405. ];
  20406. const res = this.jitEvaluator.evaluateStatements(sourceUrl, statements, new R3JitReflector(context), /* enableSourceMaps */ true);
  20407. return res['$def'];
  20408. }
  20409. }
  20410. const USE_CLASS = Object.keys({ useClass: null })[0];
  20411. const USE_FACTORY = Object.keys({ useFactory: null })[0];
  20412. const USE_VALUE = Object.keys({ useValue: null })[0];
  20413. const USE_EXISTING = Object.keys({ useExisting: null })[0];
  20414. function convertToR3QueryMetadata(facade) {
  20415. return Object.assign(Object.assign({}, facade), { predicate: Array.isArray(facade.predicate) ? facade.predicate :
  20416. new WrappedNodeExpr(facade.predicate), read: facade.read ? new WrappedNodeExpr(facade.read) : null, static: facade.static, emitDistinctChangesOnly: facade.emitDistinctChangesOnly });
  20417. }
  20418. function convertQueryDeclarationToMetadata(declaration) {
  20419. var _a, _b, _c, _d;
  20420. return {
  20421. propertyName: declaration.propertyName,
  20422. first: (_a = declaration.first) !== null && _a !== void 0 ? _a : false,
  20423. predicate: Array.isArray(declaration.predicate) ? declaration.predicate :
  20424. new WrappedNodeExpr(declaration.predicate),
  20425. descendants: (_b = declaration.descendants) !== null && _b !== void 0 ? _b : false,
  20426. read: declaration.read ? new WrappedNodeExpr(declaration.read) : null,
  20427. static: (_c = declaration.static) !== null && _c !== void 0 ? _c : false,
  20428. emitDistinctChangesOnly: (_d = declaration.emitDistinctChangesOnly) !== null && _d !== void 0 ? _d : true,
  20429. };
  20430. }
  20431. function convertDirectiveFacadeToMetadata(facade) {
  20432. const inputsFromMetadata = parseInputOutputs(facade.inputs || []);
  20433. const outputsFromMetadata = parseInputOutputs(facade.outputs || []);
  20434. const propMetadata = facade.propMetadata;
  20435. const inputsFromType = {};
  20436. const outputsFromType = {};
  20437. for (const field in propMetadata) {
  20438. if (propMetadata.hasOwnProperty(field)) {
  20439. propMetadata[field].forEach(ann => {
  20440. if (isInput(ann)) {
  20441. inputsFromType[field] =
  20442. ann.bindingPropertyName ? [ann.bindingPropertyName, field] : field;
  20443. }
  20444. else if (isOutput(ann)) {
  20445. outputsFromType[field] = ann.bindingPropertyName || field;
  20446. }
  20447. });
  20448. }
  20449. }
  20450. return Object.assign(Object.assign({}, facade), { typeArgumentCount: 0, typeSourceSpan: facade.typeSourceSpan, type: wrapReference(facade.type), internalType: new WrappedNodeExpr(facade.type), deps: null, host: extractHostBindings(facade.propMetadata, facade.typeSourceSpan, facade.host), inputs: Object.assign(Object.assign({}, inputsFromMetadata), inputsFromType), outputs: Object.assign(Object.assign({}, outputsFromMetadata), outputsFromType), queries: facade.queries.map(convertToR3QueryMetadata), providers: facade.providers != null ? new WrappedNodeExpr(facade.providers) : null, viewQueries: facade.viewQueries.map(convertToR3QueryMetadata), fullInheritance: false });
  20451. }
  20452. function convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan) {
  20453. var _a, _b, _c, _d, _e, _f, _g, _h;
  20454. return {
  20455. name: declaration.type.name,
  20456. type: wrapReference(declaration.type),
  20457. typeSourceSpan,
  20458. internalType: new WrappedNodeExpr(declaration.type),
  20459. selector: (_a = declaration.selector) !== null && _a !== void 0 ? _a : null,
  20460. inputs: (_b = declaration.inputs) !== null && _b !== void 0 ? _b : {},
  20461. outputs: (_c = declaration.outputs) !== null && _c !== void 0 ? _c : {},
  20462. host: convertHostDeclarationToMetadata(declaration.host),
  20463. queries: ((_d = declaration.queries) !== null && _d !== void 0 ? _d : []).map(convertQueryDeclarationToMetadata),
  20464. viewQueries: ((_e = declaration.viewQueries) !== null && _e !== void 0 ? _e : []).map(convertQueryDeclarationToMetadata),
  20465. providers: declaration.providers !== undefined ? new WrappedNodeExpr(declaration.providers) :
  20466. null,
  20467. exportAs: (_f = declaration.exportAs) !== null && _f !== void 0 ? _f : null,
  20468. usesInheritance: (_g = declaration.usesInheritance) !== null && _g !== void 0 ? _g : false,
  20469. lifecycle: { usesOnChanges: (_h = declaration.usesOnChanges) !== null && _h !== void 0 ? _h : false },
  20470. deps: null,
  20471. typeArgumentCount: 0,
  20472. fullInheritance: false,
  20473. };
  20474. }
  20475. function convertHostDeclarationToMetadata(host = {}) {
  20476. var _a, _b, _c;
  20477. return {
  20478. attributes: convertOpaqueValuesToExpressions((_a = host.attributes) !== null && _a !== void 0 ? _a : {}),
  20479. listeners: (_b = host.listeners) !== null && _b !== void 0 ? _b : {},
  20480. properties: (_c = host.properties) !== null && _c !== void 0 ? _c : {},
  20481. specialAttributes: {
  20482. classAttr: host.classAttribute,
  20483. styleAttr: host.styleAttribute,
  20484. },
  20485. };
  20486. }
  20487. function convertOpaqueValuesToExpressions(obj) {
  20488. const result = {};
  20489. for (const key of Object.keys(obj)) {
  20490. result[key] = new WrappedNodeExpr(obj[key]);
  20491. }
  20492. return result;
  20493. }
  20494. function convertDeclareComponentFacadeToMetadata(declaration, typeSourceSpan, sourceMapUrl) {
  20495. var _a, _b, _c, _d, _e, _f;
  20496. const { template, interpolation } = parseJitTemplate(declaration.template, declaration.type.name, sourceMapUrl, (_a = declaration.preserveWhitespaces) !== null && _a !== void 0 ? _a : false, declaration.interpolation);
  20497. return Object.assign(Object.assign({}, convertDeclareDirectiveFacadeToMetadata(declaration, typeSourceSpan)), { template, styles: (_b = declaration.styles) !== null && _b !== void 0 ? _b : [], directives: ((_c = declaration.components) !== null && _c !== void 0 ? _c : [])
  20498. .concat((_d = declaration.directives) !== null && _d !== void 0 ? _d : [])
  20499. .map(convertUsedDirectiveDeclarationToMetadata), pipes: convertUsedPipesToMetadata(declaration.pipes), viewProviders: declaration.viewProviders !== undefined ?
  20500. new WrappedNodeExpr(declaration.viewProviders) :
  20501. null, animations: declaration.animations !== undefined ? new WrappedNodeExpr(declaration.animations) :
  20502. null, changeDetection: (_e = declaration.changeDetection) !== null && _e !== void 0 ? _e : ChangeDetectionStrategy.Default, encapsulation: (_f = declaration.encapsulation) !== null && _f !== void 0 ? _f : ViewEncapsulation.Emulated, interpolation, declarationListEmitMode: 2 /* ClosureResolved */, relativeContextFilePath: '', i18nUseExternalIds: true });
  20503. }
  20504. function convertUsedDirectiveDeclarationToMetadata(declaration) {
  20505. var _a, _b, _c;
  20506. return {
  20507. selector: declaration.selector,
  20508. type: new WrappedNodeExpr(declaration.type),
  20509. inputs: (_a = declaration.inputs) !== null && _a !== void 0 ? _a : [],
  20510. outputs: (_b = declaration.outputs) !== null && _b !== void 0 ? _b : [],
  20511. exportAs: (_c = declaration.exportAs) !== null && _c !== void 0 ? _c : null,
  20512. };
  20513. }
  20514. function convertUsedPipesToMetadata(declaredPipes) {
  20515. const pipes = new Map();
  20516. if (declaredPipes === undefined) {
  20517. return pipes;
  20518. }
  20519. for (const pipeName of Object.keys(declaredPipes)) {
  20520. const pipeType = declaredPipes[pipeName];
  20521. pipes.set(pipeName, new WrappedNodeExpr(pipeType));
  20522. }
  20523. return pipes;
  20524. }
  20525. function parseJitTemplate(template, typeName, sourceMapUrl, preserveWhitespaces, interpolation) {
  20526. const interpolationConfig = interpolation ? InterpolationConfig.fromArray(interpolation) : DEFAULT_INTERPOLATION_CONFIG;
  20527. // Parse the template and check for errors.
  20528. const parsed = parseTemplate(template, sourceMapUrl, { preserveWhitespaces: preserveWhitespaces, interpolationConfig });
  20529. if (parsed.errors !== null) {
  20530. const errors = parsed.errors.map(err => err.toString()).join(', ');
  20531. throw new Error(`Errors during JIT compilation of template for ${typeName}: ${errors}`);
  20532. }
  20533. return { template: parsed, interpolation: interpolationConfig };
  20534. }
  20535. /**
  20536. * Convert the expression, if present to an `R3ProviderExpression`.
  20537. *
  20538. * In JIT mode we do not want the compiler to wrap the expression in a `forwardRef()` call because,
  20539. * if it is referencing a type that has not yet been defined, it will have already been wrapped in
  20540. * a `forwardRef()` - either by the application developer or during partial-compilation. Thus we can
  20541. * set `isForwardRef` to `false`.
  20542. */
  20543. function convertToProviderExpression(obj, property) {
  20544. if (obj.hasOwnProperty(property)) {
  20545. return createR3ProviderExpression(new WrappedNodeExpr(obj[property]), /* isForwardRef */ false);
  20546. }
  20547. else {
  20548. return undefined;
  20549. }
  20550. }
  20551. function wrapExpression(obj, property) {
  20552. if (obj.hasOwnProperty(property)) {
  20553. return new WrappedNodeExpr(obj[property]);
  20554. }
  20555. else {
  20556. return undefined;
  20557. }
  20558. }
  20559. function computeProvidedIn(providedIn) {
  20560. const expression = (providedIn == null || typeof providedIn === 'string') ?
  20561. new LiteralExpr(providedIn !== null && providedIn !== void 0 ? providedIn : null) :
  20562. new WrappedNodeExpr(providedIn);
  20563. // See `convertToProviderExpression()` for why `isForwardRef` is false.
  20564. return createR3ProviderExpression(expression, /* isForwardRef */ false);
  20565. }
  20566. function convertR3DependencyMetadataArray(facades) {
  20567. return facades == null ? null : facades.map(convertR3DependencyMetadata);
  20568. }
  20569. function convertR3DependencyMetadata(facade) {
  20570. const isAttributeDep = facade.attribute != null; // both `null` and `undefined`
  20571. const rawToken = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  20572. // In JIT mode, if the dep is an `@Attribute()` then we use the attribute name given in
  20573. // `attribute` rather than the `token`.
  20574. const token = isAttributeDep ? new WrappedNodeExpr(facade.attribute) : rawToken;
  20575. return createR3DependencyMetadata(token, isAttributeDep, facade.host, facade.optional, facade.self, facade.skipSelf);
  20576. }
  20577. function convertR3DeclareDependencyMetadata(facade) {
  20578. var _a, _b, _c, _d, _e;
  20579. const isAttributeDep = (_a = facade.attribute) !== null && _a !== void 0 ? _a : false;
  20580. const token = facade.token === null ? null : new WrappedNodeExpr(facade.token);
  20581. return createR3DependencyMetadata(token, isAttributeDep, (_b = facade.host) !== null && _b !== void 0 ? _b : false, (_c = facade.optional) !== null && _c !== void 0 ? _c : false, (_d = facade.self) !== null && _d !== void 0 ? _d : false, (_e = facade.skipSelf) !== null && _e !== void 0 ? _e : false);
  20582. }
  20583. function createR3DependencyMetadata(token, isAttributeDep, host, optional, self, skipSelf) {
  20584. // If the dep is an `@Attribute()` the `attributeNameType` ought to be the `unknown` type.
  20585. // But types are not available at runtime so we just use a literal `"<unknown>"` string as a dummy
  20586. // marker.
  20587. const attributeNameType = isAttributeDep ? literal('unknown') : null;
  20588. return { token, attributeNameType, host, optional, self, skipSelf };
  20589. }
  20590. function extractHostBindings(propMetadata, sourceSpan, host) {
  20591. // First parse the declarations from the metadata.
  20592. const bindings = parseHostBindings(host || {});
  20593. // After that check host bindings for errors
  20594. const errors = verifyHostBindings(bindings, sourceSpan);
  20595. if (errors.length) {
  20596. throw new Error(errors.map((error) => error.msg).join('\n'));
  20597. }
  20598. // Next, loop over the properties of the object, looking for @HostBinding and @HostListener.
  20599. for (const field in propMetadata) {
  20600. if (propMetadata.hasOwnProperty(field)) {
  20601. propMetadata[field].forEach(ann => {
  20602. if (isHostBinding(ann)) {
  20603. // Since this is a decorator, we know that the value is a class member. Always access it
  20604. // through `this` so that further down the line it can't be confused for a literal value
  20605. // (e.g. if there's a property called `true`).
  20606. bindings.properties[ann.hostPropertyName || field] =
  20607. getSafePropertyAccessString('this', field);
  20608. }
  20609. else if (isHostListener(ann)) {
  20610. bindings.listeners[ann.eventName || field] = `${field}(${(ann.args || []).join(',')})`;
  20611. }
  20612. });
  20613. }
  20614. }
  20615. return bindings;
  20616. }
  20617. function isHostBinding(value) {
  20618. return value.ngMetadataName === 'HostBinding';
  20619. }
  20620. function isHostListener(value) {
  20621. return value.ngMetadataName === 'HostListener';
  20622. }
  20623. function isInput(value) {
  20624. return value.ngMetadataName === 'Input';
  20625. }
  20626. function isOutput(value) {
  20627. return value.ngMetadataName === 'Output';
  20628. }
  20629. function parseInputOutputs(values) {
  20630. return values.reduce((map, value) => {
  20631. const [field, property] = value.split(',').map(piece => piece.trim());
  20632. map[field] = property || field;
  20633. return map;
  20634. }, {});
  20635. }
  20636. function convertDeclarePipeFacadeToMetadata(declaration) {
  20637. var _a;
  20638. return {
  20639. name: declaration.type.name,
  20640. type: wrapReference(declaration.type),
  20641. internalType: new WrappedNodeExpr(declaration.type),
  20642. typeArgumentCount: 0,
  20643. pipeName: declaration.name,
  20644. deps: null,
  20645. pure: (_a = declaration.pure) !== null && _a !== void 0 ? _a : true,
  20646. };
  20647. }
  20648. function convertDeclareInjectorFacadeToMetadata(declaration) {
  20649. return {
  20650. name: declaration.type.name,
  20651. type: wrapReference(declaration.type),
  20652. internalType: new WrappedNodeExpr(declaration.type),
  20653. providers: declaration.providers !== undefined ? new WrappedNodeExpr(declaration.providers) :
  20654. null,
  20655. imports: declaration.imports !== undefined ?
  20656. declaration.imports.map(i => new WrappedNodeExpr(i)) :
  20657. [],
  20658. };
  20659. }
  20660. function publishFacade(global) {
  20661. const ng = global.ng || (global.ng = {});
  20662. ng.ɵcompilerFacade = new CompilerFacadeImpl();
  20663. }
  20664. /**
  20665. * @license
  20666. * Copyright Google LLC All Rights Reserved.
  20667. *
  20668. * Use of this source code is governed by an MIT-style license that can be
  20669. * found in the LICENSE file at https://angular.io/license
  20670. */
  20671. const VERSION$1 = new Version('12.0.5');
  20672. /**
  20673. * @license
  20674. * Copyright Google LLC All Rights Reserved.
  20675. *
  20676. * Use of this source code is governed by an MIT-style license that can be
  20677. * found in the LICENSE file at https://angular.io/license
  20678. */
  20679. class CompilerConfig {
  20680. constructor({ defaultEncapsulation = ViewEncapsulation.Emulated, useJit = true, jitDevMode = false, missingTranslation = null, preserveWhitespaces, strictInjectionParameters } = {}) {
  20681. this.defaultEncapsulation = defaultEncapsulation;
  20682. this.useJit = !!useJit;
  20683. this.jitDevMode = !!jitDevMode;
  20684. this.missingTranslation = missingTranslation;
  20685. this.preserveWhitespaces = preserveWhitespacesDefault(noUndefined(preserveWhitespaces));
  20686. this.strictInjectionParameters = strictInjectionParameters === true;
  20687. }
  20688. }
  20689. function preserveWhitespacesDefault(preserveWhitespacesOption, defaultSetting = false) {
  20690. return preserveWhitespacesOption === null ? defaultSetting : preserveWhitespacesOption;
  20691. }
  20692. /**
  20693. * @license
  20694. * Copyright Google LLC All Rights Reserved.
  20695. *
  20696. * Use of this source code is governed by an MIT-style license that can be
  20697. * found in the LICENSE file at https://angular.io/license
  20698. */
  20699. class DirectiveNormalizer {
  20700. constructor(_resourceLoader, _urlResolver, _htmlParser, _config) {
  20701. this._resourceLoader = _resourceLoader;
  20702. this._urlResolver = _urlResolver;
  20703. this._htmlParser = _htmlParser;
  20704. this._config = _config;
  20705. this._resourceLoaderCache = new Map();
  20706. }
  20707. clearCache() {
  20708. this._resourceLoaderCache.clear();
  20709. }
  20710. clearCacheFor(normalizedDirective) {
  20711. if (!normalizedDirective.isComponent) {
  20712. return;
  20713. }
  20714. const template = normalizedDirective.template;
  20715. this._resourceLoaderCache.delete(template.templateUrl);
  20716. template.externalStylesheets.forEach((stylesheet) => {
  20717. this._resourceLoaderCache.delete(stylesheet.moduleUrl);
  20718. });
  20719. }
  20720. _fetch(url) {
  20721. let result = this._resourceLoaderCache.get(url);
  20722. if (!result) {
  20723. result = this._resourceLoader.get(url);
  20724. this._resourceLoaderCache.set(url, result);
  20725. }
  20726. return result;
  20727. }
  20728. normalizeTemplate(prenormData) {
  20729. if (isDefined(prenormData.template)) {
  20730. if (isDefined(prenormData.templateUrl)) {
  20731. throw syntaxError(`'${stringify(prenormData
  20732. .componentType)}' component cannot define both template and templateUrl`);
  20733. }
  20734. if (typeof prenormData.template !== 'string') {
  20735. throw syntaxError(`The template specified for component ${stringify(prenormData.componentType)} is not a string`);
  20736. }
  20737. }
  20738. else if (isDefined(prenormData.templateUrl)) {
  20739. if (typeof prenormData.templateUrl !== 'string') {
  20740. throw syntaxError(`The templateUrl specified for component ${stringify(prenormData.componentType)} is not a string`);
  20741. }
  20742. }
  20743. else {
  20744. throw syntaxError(`No template specified for component ${stringify(prenormData.componentType)}`);
  20745. }
  20746. if (isDefined(prenormData.preserveWhitespaces) &&
  20747. typeof prenormData.preserveWhitespaces !== 'boolean') {
  20748. throw syntaxError(`The preserveWhitespaces option for component ${stringify(prenormData.componentType)} must be a boolean`);
  20749. }
  20750. return SyncAsync.then(this._preParseTemplate(prenormData), (preparsedTemplate) => this._normalizeTemplateMetadata(prenormData, preparsedTemplate));
  20751. }
  20752. _preParseTemplate(prenomData) {
  20753. let template;
  20754. let templateUrl;
  20755. if (prenomData.template != null) {
  20756. template = prenomData.template;
  20757. templateUrl = prenomData.moduleUrl;
  20758. }
  20759. else {
  20760. templateUrl = this._urlResolver.resolve(prenomData.moduleUrl, prenomData.templateUrl);
  20761. template = this._fetch(templateUrl);
  20762. }
  20763. return SyncAsync.then(template, (template) => this._preparseLoadedTemplate(prenomData, template, templateUrl));
  20764. }
  20765. _preparseLoadedTemplate(prenormData, template, templateAbsUrl) {
  20766. const isInline = !!prenormData.template;
  20767. const interpolationConfig = InterpolationConfig.fromArray(prenormData.interpolation);
  20768. const templateUrl = templateSourceUrl({ reference: prenormData.ngModuleType }, { type: { reference: prenormData.componentType } }, { isInline, templateUrl: templateAbsUrl });
  20769. const rootNodesAndErrors = this._htmlParser.parse(template, templateUrl, { tokenizeExpansionForms: true, interpolationConfig });
  20770. if (rootNodesAndErrors.errors.length > 0) {
  20771. const errorString = rootNodesAndErrors.errors.join('\n');
  20772. throw syntaxError(`Template parse errors:\n${errorString}`);
  20773. }
  20774. const templateMetadataStyles = this._normalizeStylesheet(new CompileStylesheetMetadata({ styles: prenormData.styles, moduleUrl: prenormData.moduleUrl }));
  20775. const visitor = new TemplatePreparseVisitor();
  20776. visitAll$1(visitor, rootNodesAndErrors.rootNodes);
  20777. const templateStyles = this._normalizeStylesheet(new CompileStylesheetMetadata({ styles: visitor.styles, styleUrls: visitor.styleUrls, moduleUrl: templateAbsUrl }));
  20778. const styles = templateMetadataStyles.styles.concat(templateStyles.styles);
  20779. const inlineStyleUrls = templateMetadataStyles.styleUrls.concat(templateStyles.styleUrls);
  20780. const styleUrls = this
  20781. ._normalizeStylesheet(new CompileStylesheetMetadata({ styleUrls: prenormData.styleUrls, moduleUrl: prenormData.moduleUrl }))
  20782. .styleUrls;
  20783. return {
  20784. template,
  20785. templateUrl: templateAbsUrl,
  20786. isInline,
  20787. htmlAst: rootNodesAndErrors,
  20788. styles,
  20789. inlineStyleUrls,
  20790. styleUrls,
  20791. ngContentSelectors: visitor.ngContentSelectors,
  20792. };
  20793. }
  20794. _normalizeTemplateMetadata(prenormData, preparsedTemplate) {
  20795. return SyncAsync.then(this._loadMissingExternalStylesheets(preparsedTemplate.styleUrls.concat(preparsedTemplate.inlineStyleUrls)), (externalStylesheets) => this._normalizeLoadedTemplateMetadata(prenormData, preparsedTemplate, externalStylesheets));
  20796. }
  20797. _normalizeLoadedTemplateMetadata(prenormData, preparsedTemplate, stylesheets) {
  20798. // Algorithm:
  20799. // - produce exactly 1 entry per original styleUrl in
  20800. // CompileTemplateMetadata.externalStylesheets with all styles inlined
  20801. // - inline all styles that are referenced by the template into CompileTemplateMetadata.styles.
  20802. // Reason: be able to determine how many stylesheets there are even without loading
  20803. // the template nor the stylesheets, so we can create a stub for TypeScript always synchronously
  20804. // (as resource loading may be async)
  20805. const styles = [...preparsedTemplate.styles];
  20806. this._inlineStyles(preparsedTemplate.inlineStyleUrls, stylesheets, styles);
  20807. const styleUrls = preparsedTemplate.styleUrls;
  20808. const externalStylesheets = styleUrls.map(styleUrl => {
  20809. const stylesheet = stylesheets.get(styleUrl);
  20810. const styles = [...stylesheet.styles];
  20811. this._inlineStyles(stylesheet.styleUrls, stylesheets, styles);
  20812. return new CompileStylesheetMetadata({ moduleUrl: styleUrl, styles: styles });
  20813. });
  20814. let encapsulation = prenormData.encapsulation;
  20815. if (encapsulation == null) {
  20816. encapsulation = this._config.defaultEncapsulation;
  20817. }
  20818. if (encapsulation === ViewEncapsulation.Emulated && styles.length === 0 &&
  20819. styleUrls.length === 0) {
  20820. encapsulation = ViewEncapsulation.None;
  20821. }
  20822. return new CompileTemplateMetadata({
  20823. encapsulation,
  20824. template: preparsedTemplate.template,
  20825. templateUrl: preparsedTemplate.templateUrl,
  20826. htmlAst: preparsedTemplate.htmlAst,
  20827. styles,
  20828. styleUrls,
  20829. ngContentSelectors: preparsedTemplate.ngContentSelectors,
  20830. animations: prenormData.animations,
  20831. interpolation: prenormData.interpolation,
  20832. isInline: preparsedTemplate.isInline,
  20833. externalStylesheets,
  20834. preserveWhitespaces: preserveWhitespacesDefault(prenormData.preserveWhitespaces, this._config.preserveWhitespaces),
  20835. });
  20836. }
  20837. _inlineStyles(styleUrls, stylesheets, targetStyles) {
  20838. styleUrls.forEach(styleUrl => {
  20839. const stylesheet = stylesheets.get(styleUrl);
  20840. stylesheet.styles.forEach(style => targetStyles.push(style));
  20841. this._inlineStyles(stylesheet.styleUrls, stylesheets, targetStyles);
  20842. });
  20843. }
  20844. _loadMissingExternalStylesheets(styleUrls, loadedStylesheets = new Map()) {
  20845. return SyncAsync.then(SyncAsync.all(styleUrls.filter((styleUrl) => !loadedStylesheets.has(styleUrl))
  20846. .map(styleUrl => SyncAsync.then(this._fetch(styleUrl), (loadedStyle) => {
  20847. const stylesheet = this._normalizeStylesheet(new CompileStylesheetMetadata({ styles: [loadedStyle], moduleUrl: styleUrl }));
  20848. loadedStylesheets.set(styleUrl, stylesheet);
  20849. return this._loadMissingExternalStylesheets(stylesheet.styleUrls, loadedStylesheets);
  20850. }))), (_) => loadedStylesheets);
  20851. }
  20852. _normalizeStylesheet(stylesheet) {
  20853. const moduleUrl = stylesheet.moduleUrl;
  20854. const allStyleUrls = stylesheet.styleUrls.filter(isStyleUrlResolvable)
  20855. .map(url => this._urlResolver.resolve(moduleUrl, url));
  20856. const allStyles = stylesheet.styles.map(style => {
  20857. const styleWithImports = extractStyleUrls(this._urlResolver, moduleUrl, style);
  20858. allStyleUrls.push(...styleWithImports.styleUrls);
  20859. return styleWithImports.style;
  20860. });
  20861. return new CompileStylesheetMetadata({ styles: allStyles, styleUrls: allStyleUrls, moduleUrl: moduleUrl });
  20862. }
  20863. }
  20864. class TemplatePreparseVisitor {
  20865. constructor() {
  20866. this.ngContentSelectors = [];
  20867. this.styles = [];
  20868. this.styleUrls = [];
  20869. this.ngNonBindableStackCount = 0;
  20870. }
  20871. visitElement(ast, context) {
  20872. const preparsedElement = preparseElement(ast);
  20873. switch (preparsedElement.type) {
  20874. case PreparsedElementType.NG_CONTENT:
  20875. if (this.ngNonBindableStackCount === 0) {
  20876. this.ngContentSelectors.push(preparsedElement.selectAttr);
  20877. }
  20878. break;
  20879. case PreparsedElementType.STYLE:
  20880. let textContent = '';
  20881. ast.children.forEach(child => {
  20882. if (child instanceof Text$3) {
  20883. textContent += child.value;
  20884. }
  20885. });
  20886. this.styles.push(textContent);
  20887. break;
  20888. case PreparsedElementType.STYLESHEET:
  20889. this.styleUrls.push(preparsedElement.hrefAttr);
  20890. break;
  20891. default:
  20892. break;
  20893. }
  20894. if (preparsedElement.nonBindable) {
  20895. this.ngNonBindableStackCount++;
  20896. }
  20897. visitAll$1(this, ast.children);
  20898. if (preparsedElement.nonBindable) {
  20899. this.ngNonBindableStackCount--;
  20900. }
  20901. return null;
  20902. }
  20903. visitExpansion(ast, context) {
  20904. visitAll$1(this, ast.cases);
  20905. }
  20906. visitExpansionCase(ast, context) {
  20907. visitAll$1(this, ast.expression);
  20908. }
  20909. visitComment(ast, context) {
  20910. return null;
  20911. }
  20912. visitAttribute(ast, context) {
  20913. return null;
  20914. }
  20915. visitText(ast, context) {
  20916. return null;
  20917. }
  20918. }
  20919. /**
  20920. * @license
  20921. * Copyright Google LLC All Rights Reserved.
  20922. *
  20923. * Use of this source code is governed by an MIT-style license that can be
  20924. * found in the LICENSE file at https://angular.io/license
  20925. */
  20926. const QUERY_METADATA_IDENTIFIERS = [
  20927. createViewChild,
  20928. createViewChildren,
  20929. createContentChild,
  20930. createContentChildren,
  20931. ];
  20932. /*
  20933. * Resolve a `Type` for {@link Directive}.
  20934. *
  20935. * This interface can be overridden by the application developer to create custom behavior.
  20936. *
  20937. * See {@link Compiler}
  20938. */
  20939. class DirectiveResolver {
  20940. constructor(_reflector) {
  20941. this._reflector = _reflector;
  20942. }
  20943. isDirective(type) {
  20944. const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
  20945. return typeMetadata && typeMetadata.some(isDirectiveMetadata);
  20946. }
  20947. resolve(type, throwIfNotFound = true) {
  20948. const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
  20949. if (typeMetadata) {
  20950. const metadata = findLast(typeMetadata, isDirectiveMetadata);
  20951. if (metadata) {
  20952. const propertyMetadata = this._reflector.propMetadata(type);
  20953. const guards = this._reflector.guards(type);
  20954. return this._mergeWithPropertyMetadata(metadata, propertyMetadata, guards, type);
  20955. }
  20956. }
  20957. if (throwIfNotFound) {
  20958. throw new Error(`No Directive annotation found on ${stringify(type)}`);
  20959. }
  20960. return null;
  20961. }
  20962. _mergeWithPropertyMetadata(dm, propertyMetadata, guards, directiveType) {
  20963. const inputs = [];
  20964. const outputs = [];
  20965. const host = {};
  20966. const queries = {};
  20967. Object.keys(propertyMetadata).forEach((propName) => {
  20968. const input = findLast(propertyMetadata[propName], (a) => createInput.isTypeOf(a));
  20969. if (input) {
  20970. if (input.bindingPropertyName) {
  20971. inputs.push(`${propName}: ${input.bindingPropertyName}`);
  20972. }
  20973. else {
  20974. inputs.push(propName);
  20975. }
  20976. }
  20977. const output = findLast(propertyMetadata[propName], (a) => createOutput.isTypeOf(a));
  20978. if (output) {
  20979. if (output.bindingPropertyName) {
  20980. outputs.push(`${propName}: ${output.bindingPropertyName}`);
  20981. }
  20982. else {
  20983. outputs.push(propName);
  20984. }
  20985. }
  20986. const hostBindings = propertyMetadata[propName].filter(a => createHostBinding.isTypeOf(a));
  20987. hostBindings.forEach(hostBinding => {
  20988. if (hostBinding.hostPropertyName) {
  20989. const startWith = hostBinding.hostPropertyName[0];
  20990. if (startWith === '(') {
  20991. throw new Error(`@HostBinding can not bind to events. Use @HostListener instead.`);
  20992. }
  20993. else if (startWith === '[') {
  20994. throw new Error(`@HostBinding parameter should be a property name, 'class.<name>', or 'attr.<name>'.`);
  20995. }
  20996. host[`[${hostBinding.hostPropertyName}]`] = propName;
  20997. }
  20998. else {
  20999. host[`[${propName}]`] = propName;
  21000. }
  21001. });
  21002. const hostListeners = propertyMetadata[propName].filter(a => createHostListener.isTypeOf(a));
  21003. hostListeners.forEach(hostListener => {
  21004. const args = hostListener.args || [];
  21005. host[`(${hostListener.eventName})`] = `${propName}(${args.join(',')})`;
  21006. });
  21007. const query = findLast(propertyMetadata[propName], (a) => QUERY_METADATA_IDENTIFIERS.some(i => i.isTypeOf(a)));
  21008. if (query) {
  21009. queries[propName] = query;
  21010. }
  21011. });
  21012. return this._merge(dm, inputs, outputs, host, queries, guards, directiveType);
  21013. }
  21014. _extractPublicName(def) {
  21015. return splitAtColon(def, [null, def])[1].trim();
  21016. }
  21017. _dedupeBindings(bindings) {
  21018. const names = new Set();
  21019. const publicNames = new Set();
  21020. const reversedResult = [];
  21021. // go last to first to allow later entries to overwrite previous entries
  21022. for (let i = bindings.length - 1; i >= 0; i--) {
  21023. const binding = bindings[i];
  21024. const name = this._extractPublicName(binding);
  21025. publicNames.add(name);
  21026. if (!names.has(name)) {
  21027. names.add(name);
  21028. reversedResult.push(binding);
  21029. }
  21030. }
  21031. return reversedResult.reverse();
  21032. }
  21033. _merge(directive, inputs, outputs, host, queries, guards, directiveType) {
  21034. const mergedInputs = this._dedupeBindings(directive.inputs ? directive.inputs.concat(inputs) : inputs);
  21035. const mergedOutputs = this._dedupeBindings(directive.outputs ? directive.outputs.concat(outputs) : outputs);
  21036. const mergedHost = directive.host ? Object.assign(Object.assign({}, directive.host), host) : host;
  21037. const mergedQueries = directive.queries ? Object.assign(Object.assign({}, directive.queries), queries) : queries;
  21038. if (createComponent.isTypeOf(directive)) {
  21039. const comp = directive;
  21040. return createComponent({
  21041. selector: comp.selector,
  21042. inputs: mergedInputs,
  21043. outputs: mergedOutputs,
  21044. host: mergedHost,
  21045. exportAs: comp.exportAs,
  21046. moduleId: comp.moduleId,
  21047. queries: mergedQueries,
  21048. changeDetection: comp.changeDetection,
  21049. providers: comp.providers,
  21050. viewProviders: comp.viewProviders,
  21051. entryComponents: comp.entryComponents,
  21052. template: comp.template,
  21053. templateUrl: comp.templateUrl,
  21054. styles: comp.styles,
  21055. styleUrls: comp.styleUrls,
  21056. encapsulation: comp.encapsulation,
  21057. animations: comp.animations,
  21058. interpolation: comp.interpolation,
  21059. preserveWhitespaces: directive.preserveWhitespaces,
  21060. });
  21061. }
  21062. else {
  21063. return createDirective({
  21064. selector: directive.selector,
  21065. inputs: mergedInputs,
  21066. outputs: mergedOutputs,
  21067. host: mergedHost,
  21068. exportAs: directive.exportAs,
  21069. queries: mergedQueries,
  21070. providers: directive.providers,
  21071. guards
  21072. });
  21073. }
  21074. }
  21075. }
  21076. function isDirectiveMetadata(type) {
  21077. return createDirective.isTypeOf(type) || createComponent.isTypeOf(type);
  21078. }
  21079. function findLast(arr, condition) {
  21080. for (let i = arr.length - 1; i >= 0; i--) {
  21081. if (condition(arr[i])) {
  21082. return arr[i];
  21083. }
  21084. }
  21085. return null;
  21086. }
  21087. /**
  21088. * @license
  21089. * Copyright Google LLC All Rights Reserved.
  21090. *
  21091. * Use of this source code is governed by an MIT-style license that can be
  21092. * found in the LICENSE file at https://angular.io/license
  21093. */
  21094. const _I18N_ATTR = 'i18n';
  21095. const _I18N_ATTR_PREFIX = 'i18n-';
  21096. const _I18N_COMMENT_PREFIX_REGEXP = /^i18n:?/;
  21097. const MEANING_SEPARATOR = '|';
  21098. const ID_SEPARATOR = '@@';
  21099. let i18nCommentsWarned = false;
  21100. /**
  21101. * Extract translatable messages from an html AST
  21102. */
  21103. function extractMessages(nodes, interpolationConfig, implicitTags, implicitAttrs) {
  21104. const visitor = new _Visitor$2(implicitTags, implicitAttrs);
  21105. return visitor.extract(nodes, interpolationConfig);
  21106. }
  21107. function mergeTranslations(nodes, translations, interpolationConfig, implicitTags, implicitAttrs) {
  21108. const visitor = new _Visitor$2(implicitTags, implicitAttrs);
  21109. return visitor.merge(nodes, translations, interpolationConfig);
  21110. }
  21111. class ExtractionResult {
  21112. constructor(messages, errors) {
  21113. this.messages = messages;
  21114. this.errors = errors;
  21115. }
  21116. }
  21117. var _VisitorMode;
  21118. (function (_VisitorMode) {
  21119. _VisitorMode[_VisitorMode["Extract"] = 0] = "Extract";
  21120. _VisitorMode[_VisitorMode["Merge"] = 1] = "Merge";
  21121. })(_VisitorMode || (_VisitorMode = {}));
  21122. /**
  21123. * This Visitor is used:
  21124. * 1. to extract all the translatable strings from an html AST (see `extract()`),
  21125. * 2. to replace the translatable strings with the actual translations (see `merge()`)
  21126. *
  21127. * @internal
  21128. */
  21129. class _Visitor$2 {
  21130. constructor(_implicitTags, _implicitAttrs) {
  21131. this._implicitTags = _implicitTags;
  21132. this._implicitAttrs = _implicitAttrs;
  21133. }
  21134. /**
  21135. * Extracts the messages from the tree
  21136. */
  21137. extract(nodes, interpolationConfig) {
  21138. this._init(_VisitorMode.Extract, interpolationConfig);
  21139. nodes.forEach(node => node.visit(this, null));
  21140. if (this._inI18nBlock) {
  21141. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  21142. }
  21143. return new ExtractionResult(this._messages, this._errors);
  21144. }
  21145. /**
  21146. * Returns a tree where all translatable nodes are translated
  21147. */
  21148. merge(nodes, translations, interpolationConfig) {
  21149. this._init(_VisitorMode.Merge, interpolationConfig);
  21150. this._translations = translations;
  21151. // Construct a single fake root element
  21152. const wrapper = new Element$1('wrapper', [], nodes, undefined, undefined, undefined);
  21153. const translatedNode = wrapper.visit(this, null);
  21154. if (this._inI18nBlock) {
  21155. this._reportError(nodes[nodes.length - 1], 'Unclosed block');
  21156. }
  21157. return new ParseTreeResult(translatedNode.children, this._errors);
  21158. }
  21159. visitExpansionCase(icuCase, context) {
  21160. // Parse cases for translatable html attributes
  21161. const expression = visitAll$1(this, icuCase.expression, context);
  21162. if (this._mode === _VisitorMode.Merge) {
  21163. return new ExpansionCase(icuCase.value, expression, icuCase.sourceSpan, icuCase.valueSourceSpan, icuCase.expSourceSpan);
  21164. }
  21165. }
  21166. visitExpansion(icu, context) {
  21167. this._mayBeAddBlockChildren(icu);
  21168. const wasInIcu = this._inIcu;
  21169. if (!this._inIcu) {
  21170. // nested ICU messages should not be extracted but top-level translated as a whole
  21171. if (this._isInTranslatableSection) {
  21172. this._addMessage([icu]);
  21173. }
  21174. this._inIcu = true;
  21175. }
  21176. const cases = visitAll$1(this, icu.cases, context);
  21177. if (this._mode === _VisitorMode.Merge) {
  21178. icu = new Expansion(icu.switchValue, icu.type, cases, icu.sourceSpan, icu.switchValueSourceSpan);
  21179. }
  21180. this._inIcu = wasInIcu;
  21181. return icu;
  21182. }
  21183. visitComment(comment, context) {
  21184. const isOpening = _isOpeningComment(comment);
  21185. if (isOpening && this._isInTranslatableSection) {
  21186. this._reportError(comment, 'Could not start a block inside a translatable section');
  21187. return;
  21188. }
  21189. const isClosing = _isClosingComment(comment);
  21190. if (isClosing && !this._inI18nBlock) {
  21191. this._reportError(comment, 'Trying to close an unopened block');
  21192. return;
  21193. }
  21194. if (!this._inI18nNode && !this._inIcu) {
  21195. if (!this._inI18nBlock) {
  21196. if (isOpening) {
  21197. // deprecated from v5 you should use <ng-container i18n> instead of i18n comments
  21198. if (!i18nCommentsWarned && console && console.warn) {
  21199. i18nCommentsWarned = true;
  21200. const details = comment.sourceSpan.details ? `, ${comment.sourceSpan.details}` : '';
  21201. // TODO(ocombe): use a log service once there is a public one available
  21202. console.warn(`I18n comments are deprecated, use an <ng-container> element instead (${comment.sourceSpan.start}${details})`);
  21203. }
  21204. this._inI18nBlock = true;
  21205. this._blockStartDepth = this._depth;
  21206. this._blockChildren = [];
  21207. this._blockMeaningAndDesc =
  21208. comment.value.replace(_I18N_COMMENT_PREFIX_REGEXP, '').trim();
  21209. this._openTranslatableSection(comment);
  21210. }
  21211. }
  21212. else {
  21213. if (isClosing) {
  21214. if (this._depth == this._blockStartDepth) {
  21215. this._closeTranslatableSection(comment, this._blockChildren);
  21216. this._inI18nBlock = false;
  21217. const message = this._addMessage(this._blockChildren, this._blockMeaningAndDesc);
  21218. // merge attributes in sections
  21219. const nodes = this._translateMessage(comment, message);
  21220. return visitAll$1(this, nodes);
  21221. }
  21222. else {
  21223. this._reportError(comment, 'I18N blocks should not cross element boundaries');
  21224. return;
  21225. }
  21226. }
  21227. }
  21228. }
  21229. }
  21230. visitText(text, context) {
  21231. if (this._isInTranslatableSection) {
  21232. this._mayBeAddBlockChildren(text);
  21233. }
  21234. return text;
  21235. }
  21236. visitElement(el, context) {
  21237. this._mayBeAddBlockChildren(el);
  21238. this._depth++;
  21239. const wasInI18nNode = this._inI18nNode;
  21240. const wasInImplicitNode = this._inImplicitNode;
  21241. let childNodes = [];
  21242. let translatedChildNodes = undefined;
  21243. // Extract:
  21244. // - top level nodes with the (implicit) "i18n" attribute if not already in a section
  21245. // - ICU messages
  21246. const i18nAttr = _getI18nAttr(el);
  21247. const i18nMeta = i18nAttr ? i18nAttr.value : '';
  21248. const isImplicit = this._implicitTags.some(tag => el.name === tag) && !this._inIcu &&
  21249. !this._isInTranslatableSection;
  21250. const isTopLevelImplicit = !wasInImplicitNode && isImplicit;
  21251. this._inImplicitNode = wasInImplicitNode || isImplicit;
  21252. if (!this._isInTranslatableSection && !this._inIcu) {
  21253. if (i18nAttr || isTopLevelImplicit) {
  21254. this._inI18nNode = true;
  21255. const message = this._addMessage(el.children, i18nMeta);
  21256. translatedChildNodes = this._translateMessage(el, message);
  21257. }
  21258. if (this._mode == _VisitorMode.Extract) {
  21259. const isTranslatable = i18nAttr || isTopLevelImplicit;
  21260. if (isTranslatable)
  21261. this._openTranslatableSection(el);
  21262. visitAll$1(this, el.children);
  21263. if (isTranslatable)
  21264. this._closeTranslatableSection(el, el.children);
  21265. }
  21266. }
  21267. else {
  21268. if (i18nAttr || isTopLevelImplicit) {
  21269. this._reportError(el, 'Could not mark an element as translatable inside a translatable section');
  21270. }
  21271. if (this._mode == _VisitorMode.Extract) {
  21272. // Descend into child nodes for extraction
  21273. visitAll$1(this, el.children);
  21274. }
  21275. }
  21276. if (this._mode === _VisitorMode.Merge) {
  21277. const visitNodes = translatedChildNodes || el.children;
  21278. visitNodes.forEach(child => {
  21279. const visited = child.visit(this, context);
  21280. if (visited && !this._isInTranslatableSection) {
  21281. // Do not add the children from translatable sections (= i18n blocks here)
  21282. // They will be added later in this loop when the block closes (i.e. on `<!-- /i18n -->`)
  21283. childNodes = childNodes.concat(visited);
  21284. }
  21285. });
  21286. }
  21287. this._visitAttributesOf(el);
  21288. this._depth--;
  21289. this._inI18nNode = wasInI18nNode;
  21290. this._inImplicitNode = wasInImplicitNode;
  21291. if (this._mode === _VisitorMode.Merge) {
  21292. const translatedAttrs = this._translateAttributes(el);
  21293. return new Element$1(el.name, translatedAttrs, childNodes, el.sourceSpan, el.startSourceSpan, el.endSourceSpan);
  21294. }
  21295. return null;
  21296. }
  21297. visitAttribute(attribute, context) {
  21298. throw new Error('unreachable code');
  21299. }
  21300. _init(mode, interpolationConfig) {
  21301. this._mode = mode;
  21302. this._inI18nBlock = false;
  21303. this._inI18nNode = false;
  21304. this._depth = 0;
  21305. this._inIcu = false;
  21306. this._msgCountAtSectionStart = undefined;
  21307. this._errors = [];
  21308. this._messages = [];
  21309. this._inImplicitNode = false;
  21310. this._createI18nMessage = createI18nMessageFactory(interpolationConfig);
  21311. }
  21312. // looks for translatable attributes
  21313. _visitAttributesOf(el) {
  21314. const explicitAttrNameToValue = {};
  21315. const implicitAttrNames = this._implicitAttrs[el.name] || [];
  21316. el.attrs.filter(attr => attr.name.startsWith(_I18N_ATTR_PREFIX))
  21317. .forEach(attr => explicitAttrNameToValue[attr.name.slice(_I18N_ATTR_PREFIX.length)] =
  21318. attr.value);
  21319. el.attrs.forEach(attr => {
  21320. if (attr.name in explicitAttrNameToValue) {
  21321. this._addMessage([attr], explicitAttrNameToValue[attr.name]);
  21322. }
  21323. else if (implicitAttrNames.some(name => attr.name === name)) {
  21324. this._addMessage([attr]);
  21325. }
  21326. });
  21327. }
  21328. // add a translatable message
  21329. _addMessage(ast, msgMeta) {
  21330. if (ast.length == 0 ||
  21331. ast.length == 1 && ast[0] instanceof Attribute && !ast[0].value) {
  21332. // Do not create empty messages
  21333. return null;
  21334. }
  21335. const { meaning, description, id } = _parseMessageMeta(msgMeta);
  21336. const message = this._createI18nMessage(ast, meaning, description, id);
  21337. this._messages.push(message);
  21338. return message;
  21339. }
  21340. // Translates the given message given the `TranslationBundle`
  21341. // This is used for translating elements / blocks - see `_translateAttributes` for attributes
  21342. // no-op when called in extraction mode (returns [])
  21343. _translateMessage(el, message) {
  21344. if (message && this._mode === _VisitorMode.Merge) {
  21345. const nodes = this._translations.get(message);
  21346. if (nodes) {
  21347. return nodes;
  21348. }
  21349. this._reportError(el, `Translation unavailable for message id="${this._translations.digest(message)}"`);
  21350. }
  21351. return [];
  21352. }
  21353. // translate the attributes of an element and remove i18n specific attributes
  21354. _translateAttributes(el) {
  21355. const attributes = el.attrs;
  21356. const i18nParsedMessageMeta = {};
  21357. attributes.forEach(attr => {
  21358. if (attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  21359. i18nParsedMessageMeta[attr.name.slice(_I18N_ATTR_PREFIX.length)] =
  21360. _parseMessageMeta(attr.value);
  21361. }
  21362. });
  21363. const translatedAttributes = [];
  21364. attributes.forEach((attr) => {
  21365. if (attr.name === _I18N_ATTR || attr.name.startsWith(_I18N_ATTR_PREFIX)) {
  21366. // strip i18n specific attributes
  21367. return;
  21368. }
  21369. if (attr.value && attr.value != '' && i18nParsedMessageMeta.hasOwnProperty(attr.name)) {
  21370. const { meaning, description, id } = i18nParsedMessageMeta[attr.name];
  21371. const message = this._createI18nMessage([attr], meaning, description, id);
  21372. const nodes = this._translations.get(message);
  21373. if (nodes) {
  21374. if (nodes.length == 0) {
  21375. translatedAttributes.push(new Attribute(attr.name, '', attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* i18n */));
  21376. }
  21377. else if (nodes[0] instanceof Text$3) {
  21378. const value = nodes[0].value;
  21379. translatedAttributes.push(new Attribute(attr.name, value, attr.sourceSpan, undefined /* keySpan */, undefined /* valueSpan */, undefined /* i18n */));
  21380. }
  21381. else {
  21382. this._reportError(el, `Unexpected translation for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  21383. }
  21384. }
  21385. else {
  21386. this._reportError(el, `Translation unavailable for attribute "${attr.name}" (id="${id || this._translations.digest(message)}")`);
  21387. }
  21388. }
  21389. else {
  21390. translatedAttributes.push(attr);
  21391. }
  21392. });
  21393. return translatedAttributes;
  21394. }
  21395. /**
  21396. * Add the node as a child of the block when:
  21397. * - we are in a block,
  21398. * - we are not inside a ICU message (those are handled separately),
  21399. * - the node is a "direct child" of the block
  21400. */
  21401. _mayBeAddBlockChildren(node) {
  21402. if (this._inI18nBlock && !this._inIcu && this._depth == this._blockStartDepth) {
  21403. this._blockChildren.push(node);
  21404. }
  21405. }
  21406. /**
  21407. * Marks the start of a section, see `_closeTranslatableSection`
  21408. */
  21409. _openTranslatableSection(node) {
  21410. if (this._isInTranslatableSection) {
  21411. this._reportError(node, 'Unexpected section start');
  21412. }
  21413. else {
  21414. this._msgCountAtSectionStart = this._messages.length;
  21415. }
  21416. }
  21417. /**
  21418. * A translatable section could be:
  21419. * - the content of translatable element,
  21420. * - nodes between `<!-- i18n -->` and `<!-- /i18n -->` comments
  21421. */
  21422. get _isInTranslatableSection() {
  21423. return this._msgCountAtSectionStart !== void 0;
  21424. }
  21425. /**
  21426. * Terminates a section.
  21427. *
  21428. * If a section has only one significant children (comments not significant) then we should not
  21429. * keep the message from this children:
  21430. *
  21431. * `<p i18n="meaning|description">{ICU message}</p>` would produce two messages:
  21432. * - one for the <p> content with meaning and description,
  21433. * - another one for the ICU message.
  21434. *
  21435. * In this case the last message is discarded as it contains less information (the AST is
  21436. * otherwise identical).
  21437. *
  21438. * Note that we should still keep messages extracted from attributes inside the section (ie in the
  21439. * ICU message here)
  21440. */
  21441. _closeTranslatableSection(node, directChildren) {
  21442. if (!this._isInTranslatableSection) {
  21443. this._reportError(node, 'Unexpected section end');
  21444. return;
  21445. }
  21446. const startIndex = this._msgCountAtSectionStart;
  21447. const significantChildren = directChildren.reduce((count, node) => count + (node instanceof Comment$1 ? 0 : 1), 0);
  21448. if (significantChildren == 1) {
  21449. for (let i = this._messages.length - 1; i >= startIndex; i--) {
  21450. const ast = this._messages[i].nodes;
  21451. if (!(ast.length == 1 && ast[0] instanceof Text$1)) {
  21452. this._messages.splice(i, 1);
  21453. break;
  21454. }
  21455. }
  21456. }
  21457. this._msgCountAtSectionStart = undefined;
  21458. }
  21459. _reportError(node, msg) {
  21460. this._errors.push(new I18nError(node.sourceSpan, msg));
  21461. }
  21462. }
  21463. function _isOpeningComment(n) {
  21464. return !!(n instanceof Comment$1 && n.value && n.value.startsWith('i18n'));
  21465. }
  21466. function _isClosingComment(n) {
  21467. return !!(n instanceof Comment$1 && n.value && n.value === '/i18n');
  21468. }
  21469. function _getI18nAttr(p) {
  21470. return p.attrs.find(attr => attr.name === _I18N_ATTR) || null;
  21471. }
  21472. function _parseMessageMeta(i18n) {
  21473. if (!i18n)
  21474. return { meaning: '', description: '', id: '' };
  21475. const idIndex = i18n.indexOf(ID_SEPARATOR);
  21476. const descIndex = i18n.indexOf(MEANING_SEPARATOR);
  21477. const [meaningAndDesc, id] = (idIndex > -1) ? [i18n.slice(0, idIndex), i18n.slice(idIndex + 2)] : [i18n, ''];
  21478. const [meaning, description] = (descIndex > -1) ?
  21479. [meaningAndDesc.slice(0, descIndex), meaningAndDesc.slice(descIndex + 1)] :
  21480. ['', meaningAndDesc];
  21481. return { meaning, description, id: id.trim() };
  21482. }
  21483. /**
  21484. * @license
  21485. * Copyright Google LLC All Rights Reserved.
  21486. *
  21487. * Use of this source code is governed by an MIT-style license that can be
  21488. * found in the LICENSE file at https://angular.io/license
  21489. */
  21490. class XmlTagDefinition {
  21491. constructor() {
  21492. this.closedByParent = false;
  21493. this.isVoid = false;
  21494. this.ignoreFirstLf = false;
  21495. this.canSelfClose = true;
  21496. this.preventNamespaceInheritance = false;
  21497. }
  21498. requireExtraParent(currentParent) {
  21499. return false;
  21500. }
  21501. isClosedByChild(name) {
  21502. return false;
  21503. }
  21504. getContentType() {
  21505. return TagContentType.PARSABLE_DATA;
  21506. }
  21507. }
  21508. const _TAG_DEFINITION = new XmlTagDefinition();
  21509. function getXmlTagDefinition(tagName) {
  21510. return _TAG_DEFINITION;
  21511. }
  21512. /**
  21513. * @license
  21514. * Copyright Google LLC All Rights Reserved.
  21515. *
  21516. * Use of this source code is governed by an MIT-style license that can be
  21517. * found in the LICENSE file at https://angular.io/license
  21518. */
  21519. class XmlParser extends Parser {
  21520. constructor() {
  21521. super(getXmlTagDefinition);
  21522. }
  21523. parse(source, url, options) {
  21524. return super.parse(source, url, options);
  21525. }
  21526. }
  21527. /**
  21528. * @license
  21529. * Copyright Google LLC All Rights Reserved.
  21530. *
  21531. * Use of this source code is governed by an MIT-style license that can be
  21532. * found in the LICENSE file at https://angular.io/license
  21533. */
  21534. const _VERSION = '1.2';
  21535. const _XMLNS = 'urn:oasis:names:tc:xliff:document:1.2';
  21536. // TODO(vicb): make this a param (s/_/-/)
  21537. const _DEFAULT_SOURCE_LANG = 'en';
  21538. const _PLACEHOLDER_TAG$1 = 'x';
  21539. const _MARKER_TAG = 'mrk';
  21540. const _FILE_TAG = 'file';
  21541. const _SOURCE_TAG$1 = 'source';
  21542. const _SEGMENT_SOURCE_TAG = 'seg-source';
  21543. const _ALT_TRANS_TAG = 'alt-trans';
  21544. const _TARGET_TAG = 'target';
  21545. const _UNIT_TAG = 'trans-unit';
  21546. const _CONTEXT_GROUP_TAG = 'context-group';
  21547. const _CONTEXT_TAG = 'context';
  21548. // https://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html
  21549. // https://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html
  21550. class Xliff extends Serializer {
  21551. write(messages, locale) {
  21552. const visitor = new _WriteVisitor();
  21553. const transUnits = [];
  21554. messages.forEach(message => {
  21555. let contextTags = [];
  21556. message.sources.forEach((source) => {
  21557. let contextGroupTag = new Tag(_CONTEXT_GROUP_TAG, { purpose: 'location' });
  21558. contextGroupTag.children.push(new CR(10), new Tag(_CONTEXT_TAG, { 'context-type': 'sourcefile' }, [new Text$2(source.filePath)]), new CR(10), new Tag(_CONTEXT_TAG, { 'context-type': 'linenumber' }, [new Text$2(`${source.startLine}`)]), new CR(8));
  21559. contextTags.push(new CR(8), contextGroupTag);
  21560. });
  21561. const transUnit = new Tag(_UNIT_TAG, { id: message.id, datatype: 'html' });
  21562. transUnit.children.push(new CR(8), new Tag(_SOURCE_TAG$1, {}, visitor.serialize(message.nodes)), ...contextTags);
  21563. if (message.description) {
  21564. transUnit.children.push(new CR(8), new Tag('note', { priority: '1', from: 'description' }, [new Text$2(message.description)]));
  21565. }
  21566. if (message.meaning) {
  21567. transUnit.children.push(new CR(8), new Tag('note', { priority: '1', from: 'meaning' }, [new Text$2(message.meaning)]));
  21568. }
  21569. transUnit.children.push(new CR(6));
  21570. transUnits.push(new CR(6), transUnit);
  21571. });
  21572. const body = new Tag('body', {}, [...transUnits, new CR(4)]);
  21573. const file = new Tag('file', {
  21574. 'source-language': locale || _DEFAULT_SOURCE_LANG,
  21575. datatype: 'plaintext',
  21576. original: 'ng2.template',
  21577. }, [new CR(4), body, new CR(2)]);
  21578. const xliff = new Tag('xliff', { version: _VERSION, xmlns: _XMLNS }, [new CR(2), file, new CR()]);
  21579. return serialize([
  21580. new Declaration({ version: '1.0', encoding: 'UTF-8' }), new CR(), xliff, new CR()
  21581. ]);
  21582. }
  21583. load(content, url) {
  21584. // xliff to xml nodes
  21585. const xliffParser = new XliffParser();
  21586. const { locale, msgIdToHtml, errors } = xliffParser.parse(content, url);
  21587. // xml nodes to i18n nodes
  21588. const i18nNodesByMsgId = {};
  21589. const converter = new XmlToI18n();
  21590. Object.keys(msgIdToHtml).forEach(msgId => {
  21591. const { i18nNodes, errors: e } = converter.convert(msgIdToHtml[msgId], url);
  21592. errors.push(...e);
  21593. i18nNodesByMsgId[msgId] = i18nNodes;
  21594. });
  21595. if (errors.length) {
  21596. throw new Error(`xliff parse errors:\n${errors.join('\n')}`);
  21597. }
  21598. return { locale: locale, i18nNodesByMsgId };
  21599. }
  21600. digest(message) {
  21601. return digest(message);
  21602. }
  21603. }
  21604. class _WriteVisitor {
  21605. visitText(text, context) {
  21606. return [new Text$2(text.value)];
  21607. }
  21608. visitContainer(container, context) {
  21609. const nodes = [];
  21610. container.children.forEach((node) => nodes.push(...node.visit(this)));
  21611. return nodes;
  21612. }
  21613. visitIcu(icu, context) {
  21614. const nodes = [new Text$2(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  21615. Object.keys(icu.cases).forEach((c) => {
  21616. nodes.push(new Text$2(`${c} {`), ...icu.cases[c].visit(this), new Text$2(`} `));
  21617. });
  21618. nodes.push(new Text$2(`}`));
  21619. return nodes;
  21620. }
  21621. visitTagPlaceholder(ph, context) {
  21622. const ctype = getCtypeForTag(ph.tag);
  21623. if (ph.isVoid) {
  21624. // void tags have no children nor closing tags
  21625. return [new Tag(_PLACEHOLDER_TAG$1, { id: ph.startName, ctype, 'equiv-text': `<${ph.tag}/>` })];
  21626. }
  21627. const startTagPh = new Tag(_PLACEHOLDER_TAG$1, { id: ph.startName, ctype, 'equiv-text': `<${ph.tag}>` });
  21628. const closeTagPh = new Tag(_PLACEHOLDER_TAG$1, { id: ph.closeName, ctype, 'equiv-text': `</${ph.tag}>` });
  21629. return [startTagPh, ...this.serialize(ph.children), closeTagPh];
  21630. }
  21631. visitPlaceholder(ph, context) {
  21632. return [new Tag(_PLACEHOLDER_TAG$1, { id: ph.name, 'equiv-text': `{{${ph.value}}}` })];
  21633. }
  21634. visitIcuPlaceholder(ph, context) {
  21635. const equivText = `{${ph.value.expression}, ${ph.value.type}, ${Object.keys(ph.value.cases).map((value) => value + ' {...}').join(' ')}}`;
  21636. return [new Tag(_PLACEHOLDER_TAG$1, { id: ph.name, 'equiv-text': equivText })];
  21637. }
  21638. serialize(nodes) {
  21639. return [].concat(...nodes.map(node => node.visit(this)));
  21640. }
  21641. }
  21642. // TODO(vicb): add error management (structure)
  21643. // Extract messages as xml nodes from the xliff file
  21644. class XliffParser {
  21645. constructor() {
  21646. this._locale = null;
  21647. }
  21648. parse(xliff, url) {
  21649. this._unitMlString = null;
  21650. this._msgIdToHtml = {};
  21651. const xml = new XmlParser().parse(xliff, url);
  21652. this._errors = xml.errors;
  21653. visitAll$1(this, xml.rootNodes, null);
  21654. return {
  21655. msgIdToHtml: this._msgIdToHtml,
  21656. errors: this._errors,
  21657. locale: this._locale,
  21658. };
  21659. }
  21660. visitElement(element, context) {
  21661. switch (element.name) {
  21662. case _UNIT_TAG:
  21663. this._unitMlString = null;
  21664. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  21665. if (!idAttr) {
  21666. this._addError(element, `<${_UNIT_TAG}> misses the "id" attribute`);
  21667. }
  21668. else {
  21669. const id = idAttr.value;
  21670. if (this._msgIdToHtml.hasOwnProperty(id)) {
  21671. this._addError(element, `Duplicated translations for msg ${id}`);
  21672. }
  21673. else {
  21674. visitAll$1(this, element.children, null);
  21675. if (typeof this._unitMlString === 'string') {
  21676. this._msgIdToHtml[id] = this._unitMlString;
  21677. }
  21678. else {
  21679. this._addError(element, `Message ${id} misses a translation`);
  21680. }
  21681. }
  21682. }
  21683. break;
  21684. // ignore those tags
  21685. case _SOURCE_TAG$1:
  21686. case _SEGMENT_SOURCE_TAG:
  21687. case _ALT_TRANS_TAG:
  21688. break;
  21689. case _TARGET_TAG:
  21690. const innerTextStart = element.startSourceSpan.end.offset;
  21691. const innerTextEnd = element.endSourceSpan.start.offset;
  21692. const content = element.startSourceSpan.start.file.content;
  21693. const innerText = content.slice(innerTextStart, innerTextEnd);
  21694. this._unitMlString = innerText;
  21695. break;
  21696. case _FILE_TAG:
  21697. const localeAttr = element.attrs.find((attr) => attr.name === 'target-language');
  21698. if (localeAttr) {
  21699. this._locale = localeAttr.value;
  21700. }
  21701. visitAll$1(this, element.children, null);
  21702. break;
  21703. default:
  21704. // TODO(vicb): assert file structure, xliff version
  21705. // For now only recurse on unhandled nodes
  21706. visitAll$1(this, element.children, null);
  21707. }
  21708. }
  21709. visitAttribute(attribute, context) { }
  21710. visitText(text, context) { }
  21711. visitComment(comment, context) { }
  21712. visitExpansion(expansion, context) { }
  21713. visitExpansionCase(expansionCase, context) { }
  21714. _addError(node, message) {
  21715. this._errors.push(new I18nError(node.sourceSpan, message));
  21716. }
  21717. }
  21718. // Convert ml nodes (xliff syntax) to i18n nodes
  21719. class XmlToI18n {
  21720. convert(message, url) {
  21721. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  21722. this._errors = xmlIcu.errors;
  21723. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0 ?
  21724. [] :
  21725. [].concat(...visitAll$1(this, xmlIcu.rootNodes));
  21726. return {
  21727. i18nNodes: i18nNodes,
  21728. errors: this._errors,
  21729. };
  21730. }
  21731. visitText(text, context) {
  21732. return new Text$1(text.value, text.sourceSpan);
  21733. }
  21734. visitElement(el, context) {
  21735. if (el.name === _PLACEHOLDER_TAG$1) {
  21736. const nameAttr = el.attrs.find((attr) => attr.name === 'id');
  21737. if (nameAttr) {
  21738. return new Placeholder('', nameAttr.value, el.sourceSpan);
  21739. }
  21740. this._addError(el, `<${_PLACEHOLDER_TAG$1}> misses the "id" attribute`);
  21741. return null;
  21742. }
  21743. if (el.name === _MARKER_TAG) {
  21744. return [].concat(...visitAll$1(this, el.children));
  21745. }
  21746. this._addError(el, `Unexpected tag`);
  21747. return null;
  21748. }
  21749. visitExpansion(icu, context) {
  21750. const caseMap = {};
  21751. visitAll$1(this, icu.cases).forEach((c) => {
  21752. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  21753. });
  21754. return new Icu$1(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  21755. }
  21756. visitExpansionCase(icuCase, context) {
  21757. return {
  21758. value: icuCase.value,
  21759. nodes: visitAll$1(this, icuCase.expression),
  21760. };
  21761. }
  21762. visitComment(comment, context) { }
  21763. visitAttribute(attribute, context) { }
  21764. _addError(node, message) {
  21765. this._errors.push(new I18nError(node.sourceSpan, message));
  21766. }
  21767. }
  21768. function getCtypeForTag(tag) {
  21769. switch (tag.toLowerCase()) {
  21770. case 'br':
  21771. return 'lb';
  21772. case 'img':
  21773. return 'image';
  21774. default:
  21775. return `x-${tag}`;
  21776. }
  21777. }
  21778. /**
  21779. * @license
  21780. * Copyright Google LLC All Rights Reserved.
  21781. *
  21782. * Use of this source code is governed by an MIT-style license that can be
  21783. * found in the LICENSE file at https://angular.io/license
  21784. */
  21785. const _VERSION$1 = '2.0';
  21786. const _XMLNS$1 = 'urn:oasis:names:tc:xliff:document:2.0';
  21787. // TODO(vicb): make this a param (s/_/-/)
  21788. const _DEFAULT_SOURCE_LANG$1 = 'en';
  21789. const _PLACEHOLDER_TAG$2 = 'ph';
  21790. const _PLACEHOLDER_SPANNING_TAG = 'pc';
  21791. const _MARKER_TAG$1 = 'mrk';
  21792. const _XLIFF_TAG = 'xliff';
  21793. const _SOURCE_TAG$2 = 'source';
  21794. const _TARGET_TAG$1 = 'target';
  21795. const _UNIT_TAG$1 = 'unit';
  21796. // https://docs.oasis-open.org/xliff/xliff-core/v2.0/os/xliff-core-v2.0-os.html
  21797. class Xliff2 extends Serializer {
  21798. write(messages, locale) {
  21799. const visitor = new _WriteVisitor$1();
  21800. const units = [];
  21801. messages.forEach(message => {
  21802. const unit = new Tag(_UNIT_TAG$1, { id: message.id });
  21803. const notes = new Tag('notes');
  21804. if (message.description || message.meaning) {
  21805. if (message.description) {
  21806. notes.children.push(new CR(8), new Tag('note', { category: 'description' }, [new Text$2(message.description)]));
  21807. }
  21808. if (message.meaning) {
  21809. notes.children.push(new CR(8), new Tag('note', { category: 'meaning' }, [new Text$2(message.meaning)]));
  21810. }
  21811. }
  21812. message.sources.forEach((source) => {
  21813. notes.children.push(new CR(8), new Tag('note', { category: 'location' }, [
  21814. new Text$2(`${source.filePath}:${source.startLine}${source.endLine !== source.startLine ? ',' + source.endLine : ''}`)
  21815. ]));
  21816. });
  21817. notes.children.push(new CR(6));
  21818. unit.children.push(new CR(6), notes);
  21819. const segment = new Tag('segment');
  21820. segment.children.push(new CR(8), new Tag(_SOURCE_TAG$2, {}, visitor.serialize(message.nodes)), new CR(6));
  21821. unit.children.push(new CR(6), segment, new CR(4));
  21822. units.push(new CR(4), unit);
  21823. });
  21824. const file = new Tag('file', { 'original': 'ng.template', id: 'ngi18n' }, [...units, new CR(2)]);
  21825. const xliff = new Tag(_XLIFF_TAG, { version: _VERSION$1, xmlns: _XMLNS$1, srcLang: locale || _DEFAULT_SOURCE_LANG$1 }, [new CR(2), file, new CR()]);
  21826. return serialize([
  21827. new Declaration({ version: '1.0', encoding: 'UTF-8' }), new CR(), xliff, new CR()
  21828. ]);
  21829. }
  21830. load(content, url) {
  21831. // xliff to xml nodes
  21832. const xliff2Parser = new Xliff2Parser();
  21833. const { locale, msgIdToHtml, errors } = xliff2Parser.parse(content, url);
  21834. // xml nodes to i18n nodes
  21835. const i18nNodesByMsgId = {};
  21836. const converter = new XmlToI18n$1();
  21837. Object.keys(msgIdToHtml).forEach(msgId => {
  21838. const { i18nNodes, errors: e } = converter.convert(msgIdToHtml[msgId], url);
  21839. errors.push(...e);
  21840. i18nNodesByMsgId[msgId] = i18nNodes;
  21841. });
  21842. if (errors.length) {
  21843. throw new Error(`xliff2 parse errors:\n${errors.join('\n')}`);
  21844. }
  21845. return { locale: locale, i18nNodesByMsgId };
  21846. }
  21847. digest(message) {
  21848. return decimalDigest(message);
  21849. }
  21850. }
  21851. class _WriteVisitor$1 {
  21852. visitText(text, context) {
  21853. return [new Text$2(text.value)];
  21854. }
  21855. visitContainer(container, context) {
  21856. const nodes = [];
  21857. container.children.forEach((node) => nodes.push(...node.visit(this)));
  21858. return nodes;
  21859. }
  21860. visitIcu(icu, context) {
  21861. const nodes = [new Text$2(`{${icu.expressionPlaceholder}, ${icu.type}, `)];
  21862. Object.keys(icu.cases).forEach((c) => {
  21863. nodes.push(new Text$2(`${c} {`), ...icu.cases[c].visit(this), new Text$2(`} `));
  21864. });
  21865. nodes.push(new Text$2(`}`));
  21866. return nodes;
  21867. }
  21868. visitTagPlaceholder(ph, context) {
  21869. const type = getTypeForTag(ph.tag);
  21870. if (ph.isVoid) {
  21871. const tagPh = new Tag(_PLACEHOLDER_TAG$2, {
  21872. id: (this._nextPlaceholderId++).toString(),
  21873. equiv: ph.startName,
  21874. type: type,
  21875. disp: `<${ph.tag}/>`,
  21876. });
  21877. return [tagPh];
  21878. }
  21879. const tagPc = new Tag(_PLACEHOLDER_SPANNING_TAG, {
  21880. id: (this._nextPlaceholderId++).toString(),
  21881. equivStart: ph.startName,
  21882. equivEnd: ph.closeName,
  21883. type: type,
  21884. dispStart: `<${ph.tag}>`,
  21885. dispEnd: `</${ph.tag}>`,
  21886. });
  21887. const nodes = [].concat(...ph.children.map(node => node.visit(this)));
  21888. if (nodes.length) {
  21889. nodes.forEach((node) => tagPc.children.push(node));
  21890. }
  21891. else {
  21892. tagPc.children.push(new Text$2(''));
  21893. }
  21894. return [tagPc];
  21895. }
  21896. visitPlaceholder(ph, context) {
  21897. const idStr = (this._nextPlaceholderId++).toString();
  21898. return [new Tag(_PLACEHOLDER_TAG$2, {
  21899. id: idStr,
  21900. equiv: ph.name,
  21901. disp: `{{${ph.value}}}`,
  21902. })];
  21903. }
  21904. visitIcuPlaceholder(ph, context) {
  21905. const cases = Object.keys(ph.value.cases).map((value) => value + ' {...}').join(' ');
  21906. const idStr = (this._nextPlaceholderId++).toString();
  21907. return [new Tag(_PLACEHOLDER_TAG$2, { id: idStr, equiv: ph.name, disp: `{${ph.value.expression}, ${ph.value.type}, ${cases}}` })];
  21908. }
  21909. serialize(nodes) {
  21910. this._nextPlaceholderId = 0;
  21911. return [].concat(...nodes.map(node => node.visit(this)));
  21912. }
  21913. }
  21914. // Extract messages as xml nodes from the xliff file
  21915. class Xliff2Parser {
  21916. constructor() {
  21917. this._locale = null;
  21918. }
  21919. parse(xliff, url) {
  21920. this._unitMlString = null;
  21921. this._msgIdToHtml = {};
  21922. const xml = new XmlParser().parse(xliff, url);
  21923. this._errors = xml.errors;
  21924. visitAll$1(this, xml.rootNodes, null);
  21925. return {
  21926. msgIdToHtml: this._msgIdToHtml,
  21927. errors: this._errors,
  21928. locale: this._locale,
  21929. };
  21930. }
  21931. visitElement(element, context) {
  21932. switch (element.name) {
  21933. case _UNIT_TAG$1:
  21934. this._unitMlString = null;
  21935. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  21936. if (!idAttr) {
  21937. this._addError(element, `<${_UNIT_TAG$1}> misses the "id" attribute`);
  21938. }
  21939. else {
  21940. const id = idAttr.value;
  21941. if (this._msgIdToHtml.hasOwnProperty(id)) {
  21942. this._addError(element, `Duplicated translations for msg ${id}`);
  21943. }
  21944. else {
  21945. visitAll$1(this, element.children, null);
  21946. if (typeof this._unitMlString === 'string') {
  21947. this._msgIdToHtml[id] = this._unitMlString;
  21948. }
  21949. else {
  21950. this._addError(element, `Message ${id} misses a translation`);
  21951. }
  21952. }
  21953. }
  21954. break;
  21955. case _SOURCE_TAG$2:
  21956. // ignore source message
  21957. break;
  21958. case _TARGET_TAG$1:
  21959. const innerTextStart = element.startSourceSpan.end.offset;
  21960. const innerTextEnd = element.endSourceSpan.start.offset;
  21961. const content = element.startSourceSpan.start.file.content;
  21962. const innerText = content.slice(innerTextStart, innerTextEnd);
  21963. this._unitMlString = innerText;
  21964. break;
  21965. case _XLIFF_TAG:
  21966. const localeAttr = element.attrs.find((attr) => attr.name === 'trgLang');
  21967. if (localeAttr) {
  21968. this._locale = localeAttr.value;
  21969. }
  21970. const versionAttr = element.attrs.find((attr) => attr.name === 'version');
  21971. if (versionAttr) {
  21972. const version = versionAttr.value;
  21973. if (version !== '2.0') {
  21974. this._addError(element, `The XLIFF file version ${version} is not compatible with XLIFF 2.0 serializer`);
  21975. }
  21976. else {
  21977. visitAll$1(this, element.children, null);
  21978. }
  21979. }
  21980. break;
  21981. default:
  21982. visitAll$1(this, element.children, null);
  21983. }
  21984. }
  21985. visitAttribute(attribute, context) { }
  21986. visitText(text, context) { }
  21987. visitComment(comment, context) { }
  21988. visitExpansion(expansion, context) { }
  21989. visitExpansionCase(expansionCase, context) { }
  21990. _addError(node, message) {
  21991. this._errors.push(new I18nError(node.sourceSpan, message));
  21992. }
  21993. }
  21994. // Convert ml nodes (xliff syntax) to i18n nodes
  21995. class XmlToI18n$1 {
  21996. convert(message, url) {
  21997. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  21998. this._errors = xmlIcu.errors;
  21999. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0 ?
  22000. [] :
  22001. [].concat(...visitAll$1(this, xmlIcu.rootNodes));
  22002. return {
  22003. i18nNodes,
  22004. errors: this._errors,
  22005. };
  22006. }
  22007. visitText(text, context) {
  22008. return new Text$1(text.value, text.sourceSpan);
  22009. }
  22010. visitElement(el, context) {
  22011. switch (el.name) {
  22012. case _PLACEHOLDER_TAG$2:
  22013. const nameAttr = el.attrs.find((attr) => attr.name === 'equiv');
  22014. if (nameAttr) {
  22015. return [new Placeholder('', nameAttr.value, el.sourceSpan)];
  22016. }
  22017. this._addError(el, `<${_PLACEHOLDER_TAG$2}> misses the "equiv" attribute`);
  22018. break;
  22019. case _PLACEHOLDER_SPANNING_TAG:
  22020. const startAttr = el.attrs.find((attr) => attr.name === 'equivStart');
  22021. const endAttr = el.attrs.find((attr) => attr.name === 'equivEnd');
  22022. if (!startAttr) {
  22023. this._addError(el, `<${_PLACEHOLDER_TAG$2}> misses the "equivStart" attribute`);
  22024. }
  22025. else if (!endAttr) {
  22026. this._addError(el, `<${_PLACEHOLDER_TAG$2}> misses the "equivEnd" attribute`);
  22027. }
  22028. else {
  22029. const startId = startAttr.value;
  22030. const endId = endAttr.value;
  22031. const nodes = [];
  22032. return nodes.concat(new Placeholder('', startId, el.sourceSpan), ...el.children.map(node => node.visit(this, null)), new Placeholder('', endId, el.sourceSpan));
  22033. }
  22034. break;
  22035. case _MARKER_TAG$1:
  22036. return [].concat(...visitAll$1(this, el.children));
  22037. default:
  22038. this._addError(el, `Unexpected tag`);
  22039. }
  22040. return null;
  22041. }
  22042. visitExpansion(icu, context) {
  22043. const caseMap = {};
  22044. visitAll$1(this, icu.cases).forEach((c) => {
  22045. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  22046. });
  22047. return new Icu$1(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  22048. }
  22049. visitExpansionCase(icuCase, context) {
  22050. return {
  22051. value: icuCase.value,
  22052. nodes: [].concat(...visitAll$1(this, icuCase.expression)),
  22053. };
  22054. }
  22055. visitComment(comment, context) { }
  22056. visitAttribute(attribute, context) { }
  22057. _addError(node, message) {
  22058. this._errors.push(new I18nError(node.sourceSpan, message));
  22059. }
  22060. }
  22061. function getTypeForTag(tag) {
  22062. switch (tag.toLowerCase()) {
  22063. case 'br':
  22064. case 'b':
  22065. case 'i':
  22066. case 'u':
  22067. return 'fmt';
  22068. case 'img':
  22069. return 'image';
  22070. case 'a':
  22071. return 'link';
  22072. default:
  22073. return 'other';
  22074. }
  22075. }
  22076. /**
  22077. * @license
  22078. * Copyright Google LLC All Rights Reserved.
  22079. *
  22080. * Use of this source code is governed by an MIT-style license that can be
  22081. * found in the LICENSE file at https://angular.io/license
  22082. */
  22083. const _TRANSLATIONS_TAG = 'translationbundle';
  22084. const _TRANSLATION_TAG = 'translation';
  22085. const _PLACEHOLDER_TAG$3 = 'ph';
  22086. class Xtb extends Serializer {
  22087. write(messages, locale) {
  22088. throw new Error('Unsupported');
  22089. }
  22090. load(content, url) {
  22091. // xtb to xml nodes
  22092. const xtbParser = new XtbParser();
  22093. const { locale, msgIdToHtml, errors } = xtbParser.parse(content, url);
  22094. // xml nodes to i18n nodes
  22095. const i18nNodesByMsgId = {};
  22096. const converter = new XmlToI18n$2();
  22097. // Because we should be able to load xtb files that rely on features not supported by angular,
  22098. // we need to delay the conversion of html to i18n nodes so that non angular messages are not
  22099. // converted
  22100. Object.keys(msgIdToHtml).forEach(msgId => {
  22101. const valueFn = function () {
  22102. const { i18nNodes, errors } = converter.convert(msgIdToHtml[msgId], url);
  22103. if (errors.length) {
  22104. throw new Error(`xtb parse errors:\n${errors.join('\n')}`);
  22105. }
  22106. return i18nNodes;
  22107. };
  22108. createLazyProperty(i18nNodesByMsgId, msgId, valueFn);
  22109. });
  22110. if (errors.length) {
  22111. throw new Error(`xtb parse errors:\n${errors.join('\n')}`);
  22112. }
  22113. return { locale: locale, i18nNodesByMsgId };
  22114. }
  22115. digest(message) {
  22116. return digest$1(message);
  22117. }
  22118. createNameMapper(message) {
  22119. return new SimplePlaceholderMapper(message, toPublicName);
  22120. }
  22121. }
  22122. function createLazyProperty(messages, id, valueFn) {
  22123. Object.defineProperty(messages, id, {
  22124. configurable: true,
  22125. enumerable: true,
  22126. get: function () {
  22127. const value = valueFn();
  22128. Object.defineProperty(messages, id, { enumerable: true, value });
  22129. return value;
  22130. },
  22131. set: _ => {
  22132. throw new Error('Could not overwrite an XTB translation');
  22133. },
  22134. });
  22135. }
  22136. // Extract messages as xml nodes from the xtb file
  22137. class XtbParser {
  22138. constructor() {
  22139. this._locale = null;
  22140. }
  22141. parse(xtb, url) {
  22142. this._bundleDepth = 0;
  22143. this._msgIdToHtml = {};
  22144. // We can not parse the ICU messages at this point as some messages might not originate
  22145. // from Angular that could not be lex'd.
  22146. const xml = new XmlParser().parse(xtb, url);
  22147. this._errors = xml.errors;
  22148. visitAll$1(this, xml.rootNodes);
  22149. return {
  22150. msgIdToHtml: this._msgIdToHtml,
  22151. errors: this._errors,
  22152. locale: this._locale,
  22153. };
  22154. }
  22155. visitElement(element, context) {
  22156. switch (element.name) {
  22157. case _TRANSLATIONS_TAG:
  22158. this._bundleDepth++;
  22159. if (this._bundleDepth > 1) {
  22160. this._addError(element, `<${_TRANSLATIONS_TAG}> elements can not be nested`);
  22161. }
  22162. const langAttr = element.attrs.find((attr) => attr.name === 'lang');
  22163. if (langAttr) {
  22164. this._locale = langAttr.value;
  22165. }
  22166. visitAll$1(this, element.children, null);
  22167. this._bundleDepth--;
  22168. break;
  22169. case _TRANSLATION_TAG:
  22170. const idAttr = element.attrs.find((attr) => attr.name === 'id');
  22171. if (!idAttr) {
  22172. this._addError(element, `<${_TRANSLATION_TAG}> misses the "id" attribute`);
  22173. }
  22174. else {
  22175. const id = idAttr.value;
  22176. if (this._msgIdToHtml.hasOwnProperty(id)) {
  22177. this._addError(element, `Duplicated translations for msg ${id}`);
  22178. }
  22179. else {
  22180. const innerTextStart = element.startSourceSpan.end.offset;
  22181. const innerTextEnd = element.endSourceSpan.start.offset;
  22182. const content = element.startSourceSpan.start.file.content;
  22183. const innerText = content.slice(innerTextStart, innerTextEnd);
  22184. this._msgIdToHtml[id] = innerText;
  22185. }
  22186. }
  22187. break;
  22188. default:
  22189. this._addError(element, 'Unexpected tag');
  22190. }
  22191. }
  22192. visitAttribute(attribute, context) { }
  22193. visitText(text, context) { }
  22194. visitComment(comment, context) { }
  22195. visitExpansion(expansion, context) { }
  22196. visitExpansionCase(expansionCase, context) { }
  22197. _addError(node, message) {
  22198. this._errors.push(new I18nError(node.sourceSpan, message));
  22199. }
  22200. }
  22201. // Convert ml nodes (xtb syntax) to i18n nodes
  22202. class XmlToI18n$2 {
  22203. convert(message, url) {
  22204. const xmlIcu = new XmlParser().parse(message, url, { tokenizeExpansionForms: true });
  22205. this._errors = xmlIcu.errors;
  22206. const i18nNodes = this._errors.length > 0 || xmlIcu.rootNodes.length == 0 ?
  22207. [] :
  22208. visitAll$1(this, xmlIcu.rootNodes);
  22209. return {
  22210. i18nNodes,
  22211. errors: this._errors,
  22212. };
  22213. }
  22214. visitText(text, context) {
  22215. return new Text$1(text.value, text.sourceSpan);
  22216. }
  22217. visitExpansion(icu, context) {
  22218. const caseMap = {};
  22219. visitAll$1(this, icu.cases).forEach(c => {
  22220. caseMap[c.value] = new Container(c.nodes, icu.sourceSpan);
  22221. });
  22222. return new Icu$1(icu.switchValue, icu.type, caseMap, icu.sourceSpan);
  22223. }
  22224. visitExpansionCase(icuCase, context) {
  22225. return {
  22226. value: icuCase.value,
  22227. nodes: visitAll$1(this, icuCase.expression),
  22228. };
  22229. }
  22230. visitElement(el, context) {
  22231. if (el.name === _PLACEHOLDER_TAG$3) {
  22232. const nameAttr = el.attrs.find((attr) => attr.name === 'name');
  22233. if (nameAttr) {
  22234. return new Placeholder('', nameAttr.value, el.sourceSpan);
  22235. }
  22236. this._addError(el, `<${_PLACEHOLDER_TAG$3}> misses the "name" attribute`);
  22237. }
  22238. else {
  22239. this._addError(el, `Unexpected tag`);
  22240. }
  22241. return null;
  22242. }
  22243. visitComment(comment, context) { }
  22244. visitAttribute(attribute, context) { }
  22245. _addError(node, message) {
  22246. this._errors.push(new I18nError(node.sourceSpan, message));
  22247. }
  22248. }
  22249. /**
  22250. * @license
  22251. * Copyright Google LLC All Rights Reserved.
  22252. *
  22253. * Use of this source code is governed by an MIT-style license that can be
  22254. * found in the LICENSE file at https://angular.io/license
  22255. */
  22256. /**
  22257. * A container for translated messages
  22258. */
  22259. class TranslationBundle {
  22260. constructor(_i18nNodesByMsgId = {}, locale, digest, mapperFactory, missingTranslationStrategy = MissingTranslationStrategy.Warning, console) {
  22261. this._i18nNodesByMsgId = _i18nNodesByMsgId;
  22262. this.digest = digest;
  22263. this.mapperFactory = mapperFactory;
  22264. this._i18nToHtml = new I18nToHtmlVisitor(_i18nNodesByMsgId, locale, digest, mapperFactory, missingTranslationStrategy, console);
  22265. }
  22266. // Creates a `TranslationBundle` by parsing the given `content` with the `serializer`.
  22267. static load(content, url, serializer, missingTranslationStrategy, console) {
  22268. const { locale, i18nNodesByMsgId } = serializer.load(content, url);
  22269. const digestFn = (m) => serializer.digest(m);
  22270. const mapperFactory = (m) => serializer.createNameMapper(m);
  22271. return new TranslationBundle(i18nNodesByMsgId, locale, digestFn, mapperFactory, missingTranslationStrategy, console);
  22272. }
  22273. // Returns the translation as HTML nodes from the given source message.
  22274. get(srcMsg) {
  22275. const html = this._i18nToHtml.convert(srcMsg);
  22276. if (html.errors.length) {
  22277. throw new Error(html.errors.join('\n'));
  22278. }
  22279. return html.nodes;
  22280. }
  22281. has(srcMsg) {
  22282. return this.digest(srcMsg) in this._i18nNodesByMsgId;
  22283. }
  22284. }
  22285. class I18nToHtmlVisitor {
  22286. constructor(_i18nNodesByMsgId = {}, _locale, _digest, _mapperFactory, _missingTranslationStrategy, _console) {
  22287. this._i18nNodesByMsgId = _i18nNodesByMsgId;
  22288. this._locale = _locale;
  22289. this._digest = _digest;
  22290. this._mapperFactory = _mapperFactory;
  22291. this._missingTranslationStrategy = _missingTranslationStrategy;
  22292. this._console = _console;
  22293. this._contextStack = [];
  22294. this._errors = [];
  22295. }
  22296. convert(srcMsg) {
  22297. this._contextStack.length = 0;
  22298. this._errors.length = 0;
  22299. // i18n to text
  22300. const text = this._convertToText(srcMsg);
  22301. // text to html
  22302. const url = srcMsg.nodes[0].sourceSpan.start.file.url;
  22303. const html = new HtmlParser().parse(text, url, { tokenizeExpansionForms: true });
  22304. return {
  22305. nodes: html.rootNodes,
  22306. errors: [...this._errors, ...html.errors],
  22307. };
  22308. }
  22309. visitText(text, context) {
  22310. // `convert()` uses an `HtmlParser` to return `html.Node`s
  22311. // we should then make sure that any special characters are escaped
  22312. return escapeXml(text.value);
  22313. }
  22314. visitContainer(container, context) {
  22315. return container.children.map(n => n.visit(this)).join('');
  22316. }
  22317. visitIcu(icu, context) {
  22318. const cases = Object.keys(icu.cases).map(k => `${k} {${icu.cases[k].visit(this)}}`);
  22319. // TODO(vicb): Once all format switch to using expression placeholders
  22320. // we should throw when the placeholder is not in the source message
  22321. const exp = this._srcMsg.placeholders.hasOwnProperty(icu.expression) ?
  22322. this._srcMsg.placeholders[icu.expression].text :
  22323. icu.expression;
  22324. return `{${exp}, ${icu.type}, ${cases.join(' ')}}`;
  22325. }
  22326. visitPlaceholder(ph, context) {
  22327. const phName = this._mapper(ph.name);
  22328. if (this._srcMsg.placeholders.hasOwnProperty(phName)) {
  22329. return this._srcMsg.placeholders[phName].text;
  22330. }
  22331. if (this._srcMsg.placeholderToMessage.hasOwnProperty(phName)) {
  22332. return this._convertToText(this._srcMsg.placeholderToMessage[phName]);
  22333. }
  22334. this._addError(ph, `Unknown placeholder "${ph.name}"`);
  22335. return '';
  22336. }
  22337. // Loaded message contains only placeholders (vs tag and icu placeholders).
  22338. // However when a translation can not be found, we need to serialize the source message
  22339. // which can contain tag placeholders
  22340. visitTagPlaceholder(ph, context) {
  22341. const tag = `${ph.tag}`;
  22342. const attrs = Object.keys(ph.attrs).map(name => `${name}="${ph.attrs[name]}"`).join(' ');
  22343. if (ph.isVoid) {
  22344. return `<${tag} ${attrs}/>`;
  22345. }
  22346. const children = ph.children.map((c) => c.visit(this)).join('');
  22347. return `<${tag} ${attrs}>${children}</${tag}>`;
  22348. }
  22349. // Loaded message contains only placeholders (vs tag and icu placeholders).
  22350. // However when a translation can not be found, we need to serialize the source message
  22351. // which can contain tag placeholders
  22352. visitIcuPlaceholder(ph, context) {
  22353. // An ICU placeholder references the source message to be serialized
  22354. return this._convertToText(this._srcMsg.placeholderToMessage[ph.name]);
  22355. }
  22356. /**
  22357. * Convert a source message to a translated text string:
  22358. * - text nodes are replaced with their translation,
  22359. * - placeholders are replaced with their content,
  22360. * - ICU nodes are converted to ICU expressions.
  22361. */
  22362. _convertToText(srcMsg) {
  22363. const id = this._digest(srcMsg);
  22364. const mapper = this._mapperFactory ? this._mapperFactory(srcMsg) : null;
  22365. let nodes;
  22366. this._contextStack.push({ msg: this._srcMsg, mapper: this._mapper });
  22367. this._srcMsg = srcMsg;
  22368. if (this._i18nNodesByMsgId.hasOwnProperty(id)) {
  22369. // When there is a translation use its nodes as the source
  22370. // And create a mapper to convert serialized placeholder names to internal names
  22371. nodes = this._i18nNodesByMsgId[id];
  22372. this._mapper = (name) => mapper ? mapper.toInternalName(name) : name;
  22373. }
  22374. else {
  22375. // When no translation has been found
  22376. // - report an error / a warning / nothing,
  22377. // - use the nodes from the original message
  22378. // - placeholders are already internal and need no mapper
  22379. if (this._missingTranslationStrategy === MissingTranslationStrategy.Error) {
  22380. const ctx = this._locale ? ` for locale "${this._locale}"` : '';
  22381. this._addError(srcMsg.nodes[0], `Missing translation for message "${id}"${ctx}`);
  22382. }
  22383. else if (this._console &&
  22384. this._missingTranslationStrategy === MissingTranslationStrategy.Warning) {
  22385. const ctx = this._locale ? ` for locale "${this._locale}"` : '';
  22386. this._console.warn(`Missing translation for message "${id}"${ctx}`);
  22387. }
  22388. nodes = srcMsg.nodes;
  22389. this._mapper = (name) => name;
  22390. }
  22391. const text = nodes.map(node => node.visit(this)).join('');
  22392. const context = this._contextStack.pop();
  22393. this._srcMsg = context.msg;
  22394. this._mapper = context.mapper;
  22395. return text;
  22396. }
  22397. _addError(el, msg) {
  22398. this._errors.push(new I18nError(el.sourceSpan, msg));
  22399. }
  22400. }
  22401. /**
  22402. * @license
  22403. * Copyright Google LLC All Rights Reserved.
  22404. *
  22405. * Use of this source code is governed by an MIT-style license that can be
  22406. * found in the LICENSE file at https://angular.io/license
  22407. */
  22408. class I18NHtmlParser {
  22409. constructor(_htmlParser, translations, translationsFormat, missingTranslation = MissingTranslationStrategy.Warning, console) {
  22410. this._htmlParser = _htmlParser;
  22411. if (translations) {
  22412. const serializer = createSerializer(translationsFormat);
  22413. this._translationBundle =
  22414. TranslationBundle.load(translations, 'i18n', serializer, missingTranslation, console);
  22415. }
  22416. else {
  22417. this._translationBundle =
  22418. new TranslationBundle({}, null, digest, undefined, missingTranslation, console);
  22419. }
  22420. }
  22421. parse(source, url, options = {}) {
  22422. const interpolationConfig = options.interpolationConfig || DEFAULT_INTERPOLATION_CONFIG;
  22423. const parseResult = this._htmlParser.parse(source, url, Object.assign({ interpolationConfig }, options));
  22424. if (parseResult.errors.length) {
  22425. return new ParseTreeResult(parseResult.rootNodes, parseResult.errors);
  22426. }
  22427. return mergeTranslations(parseResult.rootNodes, this._translationBundle, interpolationConfig, [], {});
  22428. }
  22429. }
  22430. function createSerializer(format) {
  22431. format = (format || 'xlf').toLowerCase();
  22432. switch (format) {
  22433. case 'xmb':
  22434. return new Xmb();
  22435. case 'xtb':
  22436. return new Xtb();
  22437. case 'xliff2':
  22438. case 'xlf2':
  22439. return new Xliff2();
  22440. case 'xliff':
  22441. case 'xlf':
  22442. default:
  22443. return new Xliff();
  22444. }
  22445. }
  22446. /**
  22447. * @license
  22448. * Copyright Google LLC All Rights Reserved.
  22449. *
  22450. * Use of this source code is governed by an MIT-style license that can be
  22451. * found in the LICENSE file at https://angular.io/license
  22452. */
  22453. const QUOTED_KEYS = '$quoted$';
  22454. function convertValueToOutputAst(ctx, value, type = null) {
  22455. return visitValue(value, new _ValueOutputAstTransformer(ctx), type);
  22456. }
  22457. class _ValueOutputAstTransformer {
  22458. constructor(ctx) {
  22459. this.ctx = ctx;
  22460. }
  22461. visitArray(arr, type) {
  22462. const values = [];
  22463. // Note Array.map() must not be used to convert the values because it will
  22464. // skip over empty elements in arrays constructed using `new Array(length)`,
  22465. // resulting in `undefined` elements. This breaks the type guarantee that
  22466. // all values in `o.LiteralArrayExpr` are of type `o.Expression`.
  22467. // See test case in `value_util_spec.ts`.
  22468. for (let i = 0; i < arr.length; ++i) {
  22469. values.push(visitValue(arr[i], this, null /* context */));
  22470. }
  22471. return literalArr(values, type);
  22472. }
  22473. visitStringMap(map, type) {
  22474. const entries = [];
  22475. const quotedSet = new Set(map && map[QUOTED_KEYS]);
  22476. Object.keys(map).forEach(key => {
  22477. entries.push(new LiteralMapEntry(key, visitValue(map[key], this, null), quotedSet.has(key)));
  22478. });
  22479. return new LiteralMapExpr(entries, type);
  22480. }
  22481. visitPrimitive(value, type) {
  22482. return literal(value, type);
  22483. }
  22484. visitOther(value, type) {
  22485. if (value instanceof Expression) {
  22486. return value;
  22487. }
  22488. else {
  22489. return this.ctx.importExpr(value);
  22490. }
  22491. }
  22492. }
  22493. /**
  22494. * @license
  22495. * Copyright Google LLC All Rights Reserved.
  22496. *
  22497. * Use of this source code is governed by an MIT-style license that can be
  22498. * found in the LICENSE file at https://angular.io/license
  22499. */
  22500. function mapEntry$1(key, value) {
  22501. return { key, value, quoted: false };
  22502. }
  22503. class InjectableCompiler {
  22504. constructor(reflector, alwaysGenerateDef) {
  22505. this.reflector = reflector;
  22506. this.alwaysGenerateDef = alwaysGenerateDef;
  22507. this.tokenInjector = reflector.resolveExternalReference(Identifiers$1.Injector);
  22508. }
  22509. depsArray(deps, ctx) {
  22510. return deps.map(dep => {
  22511. let token = dep;
  22512. let args = [token];
  22513. let flags = 0 /* Default */;
  22514. if (Array.isArray(dep)) {
  22515. for (let i = 0; i < dep.length; i++) {
  22516. const v = dep[i];
  22517. if (v) {
  22518. if (v.ngMetadataName === 'Optional') {
  22519. flags |= 8 /* Optional */;
  22520. }
  22521. else if (v.ngMetadataName === 'SkipSelf') {
  22522. flags |= 4 /* SkipSelf */;
  22523. }
  22524. else if (v.ngMetadataName === 'Self') {
  22525. flags |= 2 /* Self */;
  22526. }
  22527. else if (v.ngMetadataName === 'Inject') {
  22528. token = v.token;
  22529. }
  22530. else {
  22531. token = v;
  22532. }
  22533. }
  22534. }
  22535. }
  22536. let tokenExpr;
  22537. if (typeof token === 'string') {
  22538. tokenExpr = literal(token);
  22539. }
  22540. else if (token === this.tokenInjector) {
  22541. tokenExpr = importExpr(Identifiers$1.INJECTOR);
  22542. }
  22543. else {
  22544. tokenExpr = ctx.importExpr(token);
  22545. }
  22546. if (flags !== 0 /* Default */) {
  22547. args = [tokenExpr, literal(flags)];
  22548. }
  22549. else {
  22550. args = [tokenExpr];
  22551. }
  22552. return importExpr(Identifiers$1.inject).callFn(args);
  22553. });
  22554. }
  22555. factoryFor(injectable, ctx) {
  22556. let retValue;
  22557. if (injectable.useExisting) {
  22558. retValue = importExpr(Identifiers$1.inject).callFn([ctx.importExpr(injectable.useExisting)]);
  22559. }
  22560. else if (injectable.useFactory) {
  22561. const deps = injectable.deps || [];
  22562. if (deps.length > 0) {
  22563. retValue = ctx.importExpr(injectable.useFactory).callFn(this.depsArray(deps, ctx));
  22564. }
  22565. else {
  22566. return ctx.importExpr(injectable.useFactory);
  22567. }
  22568. }
  22569. else if (injectable.useValue) {
  22570. retValue = convertValueToOutputAst(ctx, injectable.useValue);
  22571. }
  22572. else {
  22573. const clazz = injectable.useClass || injectable.symbol;
  22574. const depArgs = this.depsArray(this.reflector.parameters(clazz), ctx);
  22575. retValue = new InstantiateExpr(ctx.importExpr(clazz), depArgs);
  22576. }
  22577. return fn([], [new ReturnStatement(retValue)], undefined, undefined, injectable.symbol.name + '_Factory');
  22578. }
  22579. injectableDef(injectable, ctx) {
  22580. let providedIn = NULL_EXPR;
  22581. if (injectable.providedIn !== undefined) {
  22582. if (injectable.providedIn === null) {
  22583. providedIn = NULL_EXPR;
  22584. }
  22585. else if (typeof injectable.providedIn === 'string') {
  22586. providedIn = literal(injectable.providedIn);
  22587. }
  22588. else {
  22589. providedIn = ctx.importExpr(injectable.providedIn);
  22590. }
  22591. }
  22592. const def = [
  22593. mapEntry$1('factory', this.factoryFor(injectable, ctx)),
  22594. mapEntry$1('token', ctx.importExpr(injectable.type.reference)),
  22595. mapEntry$1('providedIn', providedIn),
  22596. ];
  22597. return importExpr(Identifiers.ɵɵdefineInjectable).callFn([literalMap(def)], undefined, true);
  22598. }
  22599. compile(injectable, ctx) {
  22600. if (this.alwaysGenerateDef || injectable.providedIn !== undefined) {
  22601. const className = identifierName(injectable.type);
  22602. const clazz = new ClassStmt(className, null, [
  22603. new ClassField('ɵprov', INFERRED_TYPE, [StmtModifier.Static], this.injectableDef(injectable, ctx)),
  22604. ], [], new ClassMethod(null, [], []), []);
  22605. ctx.statements.push(clazz);
  22606. }
  22607. }
  22608. }
  22609. /**
  22610. * @license
  22611. * Copyright Google LLC All Rights Reserved.
  22612. *
  22613. * Use of this source code is governed by an MIT-style license that can be
  22614. * found in the LICENSE file at https://angular.io/license
  22615. */
  22616. const STRIP_SRC_FILE_SUFFIXES = /(\.ts|\.d\.ts|\.js|\.jsx|\.tsx)$/;
  22617. const GENERATED_FILE = /\.ngfactory\.|\.ngsummary\./;
  22618. const JIT_SUMMARY_FILE = /\.ngsummary\./;
  22619. const JIT_SUMMARY_NAME = /NgSummary$/;
  22620. function ngfactoryFilePath(filePath, forceSourceFile = false) {
  22621. const urlWithSuffix = splitTypescriptSuffix(filePath, forceSourceFile);
  22622. return `${urlWithSuffix[0]}.ngfactory${normalizeGenFileSuffix(urlWithSuffix[1])}`;
  22623. }
  22624. function stripGeneratedFileSuffix(filePath) {
  22625. return filePath.replace(GENERATED_FILE, '.');
  22626. }
  22627. function isGeneratedFile(filePath) {
  22628. return GENERATED_FILE.test(filePath);
  22629. }
  22630. function splitTypescriptSuffix(path, forceSourceFile = false) {
  22631. if (path.endsWith('.d.ts')) {
  22632. return [path.slice(0, -5), forceSourceFile ? '.ts' : '.d.ts'];
  22633. }
  22634. const lastDot = path.lastIndexOf('.');
  22635. if (lastDot !== -1) {
  22636. return [path.substring(0, lastDot), path.substring(lastDot)];
  22637. }
  22638. return [path, ''];
  22639. }
  22640. function normalizeGenFileSuffix(srcFileSuffix) {
  22641. return srcFileSuffix === '.tsx' ? '.ts' : srcFileSuffix;
  22642. }
  22643. function summaryFileName(fileName) {
  22644. const fileNameWithoutSuffix = fileName.replace(STRIP_SRC_FILE_SUFFIXES, '');
  22645. return `${fileNameWithoutSuffix}.ngsummary.json`;
  22646. }
  22647. function summaryForJitFileName(fileName, forceSourceFile = false) {
  22648. const urlWithSuffix = splitTypescriptSuffix(stripGeneratedFileSuffix(fileName), forceSourceFile);
  22649. return `${urlWithSuffix[0]}.ngsummary${urlWithSuffix[1]}`;
  22650. }
  22651. function stripSummaryForJitFileSuffix(filePath) {
  22652. return filePath.replace(JIT_SUMMARY_FILE, '.');
  22653. }
  22654. function summaryForJitName(symbolName) {
  22655. return `${symbolName}NgSummary`;
  22656. }
  22657. function stripSummaryForJitNameSuffix(symbolName) {
  22658. return symbolName.replace(JIT_SUMMARY_NAME, '');
  22659. }
  22660. const LOWERED_SYMBOL = /\u0275\d+/;
  22661. function isLoweredSymbol(name) {
  22662. return LOWERED_SYMBOL.test(name);
  22663. }
  22664. function createLoweredSymbol(id) {
  22665. return `\u0275${id}`;
  22666. }
  22667. /**
  22668. * @license
  22669. * Copyright Google LLC All Rights Reserved.
  22670. *
  22671. * Use of this source code is governed by an MIT-style license that can be
  22672. * found in the LICENSE file at https://angular.io/license
  22673. */
  22674. var LifecycleHooks;
  22675. (function (LifecycleHooks) {
  22676. LifecycleHooks[LifecycleHooks["OnInit"] = 0] = "OnInit";
  22677. LifecycleHooks[LifecycleHooks["OnDestroy"] = 1] = "OnDestroy";
  22678. LifecycleHooks[LifecycleHooks["DoCheck"] = 2] = "DoCheck";
  22679. LifecycleHooks[LifecycleHooks["OnChanges"] = 3] = "OnChanges";
  22680. LifecycleHooks[LifecycleHooks["AfterContentInit"] = 4] = "AfterContentInit";
  22681. LifecycleHooks[LifecycleHooks["AfterContentChecked"] = 5] = "AfterContentChecked";
  22682. LifecycleHooks[LifecycleHooks["AfterViewInit"] = 6] = "AfterViewInit";
  22683. LifecycleHooks[LifecycleHooks["AfterViewChecked"] = 7] = "AfterViewChecked";
  22684. })(LifecycleHooks || (LifecycleHooks = {}));
  22685. const LIFECYCLE_HOOKS_VALUES = [
  22686. LifecycleHooks.OnInit, LifecycleHooks.OnDestroy, LifecycleHooks.DoCheck, LifecycleHooks.OnChanges,
  22687. LifecycleHooks.AfterContentInit, LifecycleHooks.AfterContentChecked, LifecycleHooks.AfterViewInit,
  22688. LifecycleHooks.AfterViewChecked
  22689. ];
  22690. function hasLifecycleHook(reflector, hook, token) {
  22691. return reflector.hasLifecycleHook(token, getHookName(hook));
  22692. }
  22693. function getAllLifecycleHooks(reflector, token) {
  22694. return LIFECYCLE_HOOKS_VALUES.filter(hook => hasLifecycleHook(reflector, hook, token));
  22695. }
  22696. function getHookName(hook) {
  22697. switch (hook) {
  22698. case LifecycleHooks.OnInit:
  22699. return 'ngOnInit';
  22700. case LifecycleHooks.OnDestroy:
  22701. return 'ngOnDestroy';
  22702. case LifecycleHooks.DoCheck:
  22703. return 'ngDoCheck';
  22704. case LifecycleHooks.OnChanges:
  22705. return 'ngOnChanges';
  22706. case LifecycleHooks.AfterContentInit:
  22707. return 'ngAfterContentInit';
  22708. case LifecycleHooks.AfterContentChecked:
  22709. return 'ngAfterContentChecked';
  22710. case LifecycleHooks.AfterViewInit:
  22711. return 'ngAfterViewInit';
  22712. case LifecycleHooks.AfterViewChecked:
  22713. return 'ngAfterViewChecked';
  22714. default:
  22715. // This default case is not needed by TypeScript compiler, as the switch is exhaustive.
  22716. // However Closure Compiler does not understand that and reports an error in typed mode.
  22717. // The `throw new Error` below works around the problem, and the unexpected: never variable
  22718. // makes sure tsc still checks this code is unreachable.
  22719. const unexpected = hook;
  22720. throw new Error(`unexpected ${unexpected}`);
  22721. }
  22722. }
  22723. /**
  22724. * @license
  22725. * Copyright Google LLC All Rights Reserved.
  22726. *
  22727. * Use of this source code is governed by an MIT-style license that can be
  22728. * found in the LICENSE file at https://angular.io/license
  22729. */
  22730. const ERROR_COMPONENT_TYPE = 'ngComponentType';
  22731. const MISSING_NG_MODULE_METADATA_ERROR_DATA = 'ngMissingNgModuleMetadataErrorData';
  22732. function getMissingNgModuleMetadataErrorData(error) {
  22733. var _a;
  22734. return (_a = error[MISSING_NG_MODULE_METADATA_ERROR_DATA]) !== null && _a !== void 0 ? _a : null;
  22735. }
  22736. // Design notes:
  22737. // - don't lazily create metadata:
  22738. // For some metadata, we need to do async work sometimes,
  22739. // so the user has to kick off this loading.
  22740. // But we want to report errors even when the async work is
  22741. // not required to check that the user would have been able
  22742. // to wait correctly.
  22743. class CompileMetadataResolver {
  22744. constructor(_config, _htmlParser, _ngModuleResolver, _directiveResolver, _pipeResolver, _summaryResolver, _schemaRegistry, _directiveNormalizer, _console, _staticSymbolCache, _reflector, _errorCollector) {
  22745. this._config = _config;
  22746. this._htmlParser = _htmlParser;
  22747. this._ngModuleResolver = _ngModuleResolver;
  22748. this._directiveResolver = _directiveResolver;
  22749. this._pipeResolver = _pipeResolver;
  22750. this._summaryResolver = _summaryResolver;
  22751. this._schemaRegistry = _schemaRegistry;
  22752. this._directiveNormalizer = _directiveNormalizer;
  22753. this._console = _console;
  22754. this._staticSymbolCache = _staticSymbolCache;
  22755. this._reflector = _reflector;
  22756. this._errorCollector = _errorCollector;
  22757. this._nonNormalizedDirectiveCache = new Map();
  22758. this._directiveCache = new Map();
  22759. this._summaryCache = new Map();
  22760. this._pipeCache = new Map();
  22761. this._ngModuleCache = new Map();
  22762. this._ngModuleOfTypes = new Map();
  22763. this._shallowModuleCache = new Map();
  22764. }
  22765. getReflector() {
  22766. return this._reflector;
  22767. }
  22768. clearCacheFor(type) {
  22769. const dirMeta = this._directiveCache.get(type);
  22770. this._directiveCache.delete(type);
  22771. this._nonNormalizedDirectiveCache.delete(type);
  22772. this._summaryCache.delete(type);
  22773. this._pipeCache.delete(type);
  22774. this._ngModuleOfTypes.delete(type);
  22775. // Clear all of the NgModule as they contain transitive information!
  22776. this._ngModuleCache.clear();
  22777. if (dirMeta) {
  22778. this._directiveNormalizer.clearCacheFor(dirMeta);
  22779. }
  22780. }
  22781. clearCache() {
  22782. this._directiveCache.clear();
  22783. this._nonNormalizedDirectiveCache.clear();
  22784. this._summaryCache.clear();
  22785. this._pipeCache.clear();
  22786. this._ngModuleCache.clear();
  22787. this._ngModuleOfTypes.clear();
  22788. this._directiveNormalizer.clearCache();
  22789. }
  22790. _createProxyClass(baseType, name) {
  22791. let delegate = null;
  22792. const proxyClass = function () {
  22793. if (!delegate) {
  22794. throw new Error(`Illegal state: Class ${name} for type ${stringify(baseType)} is not compiled yet!`);
  22795. }
  22796. return delegate.apply(this, arguments);
  22797. };
  22798. proxyClass.setDelegate = (d) => {
  22799. delegate = d;
  22800. proxyClass.prototype = d.prototype;
  22801. };
  22802. // Make stringify work correctly
  22803. proxyClass.overriddenName = name;
  22804. return proxyClass;
  22805. }
  22806. getGeneratedClass(dirType, name) {
  22807. if (dirType instanceof StaticSymbol) {
  22808. return this._staticSymbolCache.get(ngfactoryFilePath(dirType.filePath), name);
  22809. }
  22810. else {
  22811. return this._createProxyClass(dirType, name);
  22812. }
  22813. }
  22814. getComponentViewClass(dirType) {
  22815. return this.getGeneratedClass(dirType, viewClassName(dirType, 0));
  22816. }
  22817. getHostComponentViewClass(dirType) {
  22818. return this.getGeneratedClass(dirType, hostViewClassName(dirType));
  22819. }
  22820. getHostComponentType(dirType) {
  22821. const name = `${identifierName({ reference: dirType })}_Host`;
  22822. if (dirType instanceof StaticSymbol) {
  22823. return this._staticSymbolCache.get(dirType.filePath, name);
  22824. }
  22825. return this._createProxyClass(dirType, name);
  22826. }
  22827. getRendererType(dirType) {
  22828. if (dirType instanceof StaticSymbol) {
  22829. return this._staticSymbolCache.get(ngfactoryFilePath(dirType.filePath), rendererTypeName(dirType));
  22830. }
  22831. else {
  22832. // returning an object as proxy,
  22833. // that we fill later during runtime compilation.
  22834. return {};
  22835. }
  22836. }
  22837. getComponentFactory(selector, dirType, inputs, outputs) {
  22838. if (dirType instanceof StaticSymbol) {
  22839. return this._staticSymbolCache.get(ngfactoryFilePath(dirType.filePath), componentFactoryName(dirType));
  22840. }
  22841. else {
  22842. const hostView = this.getHostComponentViewClass(dirType);
  22843. // Note: ngContentSelectors will be filled later once the template is
  22844. // loaded.
  22845. const createComponentFactory = this._reflector.resolveExternalReference(Identifiers$1.createComponentFactory);
  22846. return createComponentFactory(selector, dirType, hostView, inputs, outputs, []);
  22847. }
  22848. }
  22849. initComponentFactory(factory, ngContentSelectors) {
  22850. if (!(factory instanceof StaticSymbol)) {
  22851. factory.ngContentSelectors.push(...ngContentSelectors);
  22852. }
  22853. }
  22854. _loadSummary(type, kind) {
  22855. let typeSummary = this._summaryCache.get(type);
  22856. if (!typeSummary) {
  22857. const summary = this._summaryResolver.resolveSummary(type);
  22858. typeSummary = summary ? summary.type : null;
  22859. this._summaryCache.set(type, typeSummary || null);
  22860. }
  22861. return typeSummary && typeSummary.summaryKind === kind ? typeSummary : null;
  22862. }
  22863. getHostComponentMetadata(compMeta, hostViewType) {
  22864. const hostType = this.getHostComponentType(compMeta.type.reference);
  22865. if (!hostViewType) {
  22866. hostViewType = this.getHostComponentViewClass(hostType);
  22867. }
  22868. // Note: ! is ok here as this method should only be called with normalized directive
  22869. // metadata, which always fills in the selector.
  22870. const template = CssSelector.parse(compMeta.selector)[0].getMatchingElementTemplate();
  22871. const templateUrl = '';
  22872. const htmlAst = this._htmlParser.parse(template, templateUrl);
  22873. return CompileDirectiveMetadata.create({
  22874. isHost: true,
  22875. type: { reference: hostType, diDeps: [], lifecycleHooks: [] },
  22876. template: new CompileTemplateMetadata({
  22877. encapsulation: ViewEncapsulation.None,
  22878. template,
  22879. templateUrl,
  22880. htmlAst,
  22881. styles: [],
  22882. styleUrls: [],
  22883. ngContentSelectors: [],
  22884. animations: [],
  22885. isInline: true,
  22886. externalStylesheets: [],
  22887. interpolation: null,
  22888. preserveWhitespaces: false,
  22889. }),
  22890. exportAs: null,
  22891. changeDetection: ChangeDetectionStrategy.Default,
  22892. inputs: [],
  22893. outputs: [],
  22894. host: {},
  22895. isComponent: true,
  22896. selector: '*',
  22897. providers: [],
  22898. viewProviders: [],
  22899. queries: [],
  22900. guards: {},
  22901. viewQueries: [],
  22902. componentViewType: hostViewType,
  22903. rendererType: { id: '__Host__', encapsulation: ViewEncapsulation.None, styles: [], data: {} },
  22904. entryComponents: [],
  22905. componentFactory: null
  22906. });
  22907. }
  22908. loadDirectiveMetadata(ngModuleType, directiveType, isSync) {
  22909. if (this._directiveCache.has(directiveType)) {
  22910. return null;
  22911. }
  22912. directiveType = resolveForwardRef(directiveType);
  22913. const { annotation, metadata } = this.getNonNormalizedDirectiveMetadata(directiveType);
  22914. const createDirectiveMetadata = (templateMetadata) => {
  22915. const normalizedDirMeta = new CompileDirectiveMetadata({
  22916. isHost: false,
  22917. type: metadata.type,
  22918. isComponent: metadata.isComponent,
  22919. selector: metadata.selector,
  22920. exportAs: metadata.exportAs,
  22921. changeDetection: metadata.changeDetection,
  22922. inputs: metadata.inputs,
  22923. outputs: metadata.outputs,
  22924. hostListeners: metadata.hostListeners,
  22925. hostProperties: metadata.hostProperties,
  22926. hostAttributes: metadata.hostAttributes,
  22927. providers: metadata.providers,
  22928. viewProviders: metadata.viewProviders,
  22929. queries: metadata.queries,
  22930. guards: metadata.guards,
  22931. viewQueries: metadata.viewQueries,
  22932. entryComponents: metadata.entryComponents,
  22933. componentViewType: metadata.componentViewType,
  22934. rendererType: metadata.rendererType,
  22935. componentFactory: metadata.componentFactory,
  22936. template: templateMetadata
  22937. });
  22938. if (templateMetadata) {
  22939. this.initComponentFactory(metadata.componentFactory, templateMetadata.ngContentSelectors);
  22940. }
  22941. this._directiveCache.set(directiveType, normalizedDirMeta);
  22942. this._summaryCache.set(directiveType, normalizedDirMeta.toSummary());
  22943. return null;
  22944. };
  22945. if (metadata.isComponent) {
  22946. const template = metadata.template;
  22947. const templateMeta = this._directiveNormalizer.normalizeTemplate({
  22948. ngModuleType,
  22949. componentType: directiveType,
  22950. moduleUrl: this._reflector.componentModuleUrl(directiveType, annotation),
  22951. encapsulation: template.encapsulation,
  22952. template: template.template,
  22953. templateUrl: template.templateUrl,
  22954. styles: template.styles,
  22955. styleUrls: template.styleUrls,
  22956. animations: template.animations,
  22957. interpolation: template.interpolation,
  22958. preserveWhitespaces: template.preserveWhitespaces
  22959. });
  22960. if (isPromise(templateMeta) && isSync) {
  22961. this._reportError(componentStillLoadingError(directiveType), directiveType);
  22962. return null;
  22963. }
  22964. return SyncAsync.then(templateMeta, createDirectiveMetadata);
  22965. }
  22966. else {
  22967. // directive
  22968. createDirectiveMetadata(null);
  22969. return null;
  22970. }
  22971. }
  22972. getNonNormalizedDirectiveMetadata(directiveType) {
  22973. directiveType = resolveForwardRef(directiveType);
  22974. if (!directiveType) {
  22975. return null;
  22976. }
  22977. let cacheEntry = this._nonNormalizedDirectiveCache.get(directiveType);
  22978. if (cacheEntry) {
  22979. return cacheEntry;
  22980. }
  22981. const dirMeta = this._directiveResolver.resolve(directiveType, false);
  22982. if (!dirMeta) {
  22983. return null;
  22984. }
  22985. let nonNormalizedTemplateMetadata = undefined;
  22986. if (createComponent.isTypeOf(dirMeta)) {
  22987. // component
  22988. const compMeta = dirMeta;
  22989. assertArrayOfStrings('styles', compMeta.styles);
  22990. assertArrayOfStrings('styleUrls', compMeta.styleUrls);
  22991. assertInterpolationSymbols('interpolation', compMeta.interpolation);
  22992. const animations = compMeta.animations;
  22993. nonNormalizedTemplateMetadata = new CompileTemplateMetadata({
  22994. encapsulation: noUndefined(compMeta.encapsulation),
  22995. template: noUndefined(compMeta.template),
  22996. templateUrl: noUndefined(compMeta.templateUrl),
  22997. htmlAst: null,
  22998. styles: compMeta.styles || [],
  22999. styleUrls: compMeta.styleUrls || [],
  23000. animations: animations || [],
  23001. interpolation: noUndefined(compMeta.interpolation),
  23002. isInline: !!compMeta.template,
  23003. externalStylesheets: [],
  23004. ngContentSelectors: [],
  23005. preserveWhitespaces: noUndefined(dirMeta.preserveWhitespaces),
  23006. });
  23007. }
  23008. let changeDetectionStrategy = null;
  23009. let viewProviders = [];
  23010. let entryComponentMetadata = [];
  23011. let selector = dirMeta.selector;
  23012. if (createComponent.isTypeOf(dirMeta)) {
  23013. // Component
  23014. const compMeta = dirMeta;
  23015. changeDetectionStrategy = compMeta.changeDetection;
  23016. if (compMeta.viewProviders) {
  23017. viewProviders = this._getProvidersMetadata(compMeta.viewProviders, entryComponentMetadata, `viewProviders for "${stringifyType(directiveType)}"`, [], directiveType);
  23018. }
  23019. if (compMeta.entryComponents) {
  23020. entryComponentMetadata = flattenAndDedupeArray(compMeta.entryComponents)
  23021. .map((type) => this._getEntryComponentMetadata(type))
  23022. .concat(entryComponentMetadata);
  23023. }
  23024. if (!selector) {
  23025. selector = this._schemaRegistry.getDefaultComponentElementName();
  23026. }
  23027. }
  23028. else {
  23029. // Directive
  23030. if (!selector) {
  23031. selector = null;
  23032. }
  23033. }
  23034. let providers = [];
  23035. if (dirMeta.providers != null) {
  23036. providers = this._getProvidersMetadata(dirMeta.providers, entryComponentMetadata, `providers for "${stringifyType(directiveType)}"`, [], directiveType);
  23037. }
  23038. let queries = [];
  23039. let viewQueries = [];
  23040. if (dirMeta.queries != null) {
  23041. queries = this._getQueriesMetadata(dirMeta.queries, false, directiveType);
  23042. viewQueries = this._getQueriesMetadata(dirMeta.queries, true, directiveType);
  23043. }
  23044. const metadata = CompileDirectiveMetadata.create({
  23045. isHost: false,
  23046. selector: selector,
  23047. exportAs: noUndefined(dirMeta.exportAs),
  23048. isComponent: !!nonNormalizedTemplateMetadata,
  23049. type: this._getTypeMetadata(directiveType),
  23050. template: nonNormalizedTemplateMetadata,
  23051. changeDetection: changeDetectionStrategy,
  23052. inputs: dirMeta.inputs || [],
  23053. outputs: dirMeta.outputs || [],
  23054. host: dirMeta.host || {},
  23055. providers: providers || [],
  23056. viewProviders: viewProviders || [],
  23057. queries: queries || [],
  23058. guards: dirMeta.guards || {},
  23059. viewQueries: viewQueries || [],
  23060. entryComponents: entryComponentMetadata,
  23061. componentViewType: nonNormalizedTemplateMetadata ? this.getComponentViewClass(directiveType) :
  23062. null,
  23063. rendererType: nonNormalizedTemplateMetadata ? this.getRendererType(directiveType) : null,
  23064. componentFactory: null
  23065. });
  23066. if (nonNormalizedTemplateMetadata) {
  23067. metadata.componentFactory =
  23068. this.getComponentFactory(selector, directiveType, metadata.inputs, metadata.outputs);
  23069. }
  23070. cacheEntry = { metadata, annotation: dirMeta };
  23071. this._nonNormalizedDirectiveCache.set(directiveType, cacheEntry);
  23072. return cacheEntry;
  23073. }
  23074. /**
  23075. * Gets the metadata for the given directive.
  23076. * This assumes `loadNgModuleDirectiveAndPipeMetadata` has been called first.
  23077. */
  23078. getDirectiveMetadata(directiveType) {
  23079. const dirMeta = this._directiveCache.get(directiveType);
  23080. if (!dirMeta) {
  23081. this._reportError(syntaxError(`Illegal state: getDirectiveMetadata can only be called after loadNgModuleDirectiveAndPipeMetadata for a module that declares it. Directive ${stringifyType(directiveType)}.`), directiveType);
  23082. }
  23083. return dirMeta;
  23084. }
  23085. getDirectiveSummary(dirType) {
  23086. const dirSummary = this._loadSummary(dirType, CompileSummaryKind.Directive);
  23087. if (!dirSummary) {
  23088. this._reportError(syntaxError(`Illegal state: Could not load the summary for directive ${stringifyType(dirType)}.`), dirType);
  23089. }
  23090. return dirSummary;
  23091. }
  23092. isDirective(type) {
  23093. return !!this._loadSummary(type, CompileSummaryKind.Directive) ||
  23094. this._directiveResolver.isDirective(type);
  23095. }
  23096. isAbstractDirective(type) {
  23097. const summary = this._loadSummary(type, CompileSummaryKind.Directive);
  23098. if (summary && !summary.isComponent) {
  23099. return !summary.selector;
  23100. }
  23101. const meta = this._directiveResolver.resolve(type, false);
  23102. if (meta && !createComponent.isTypeOf(meta)) {
  23103. return !meta.selector;
  23104. }
  23105. return false;
  23106. }
  23107. isPipe(type) {
  23108. return !!this._loadSummary(type, CompileSummaryKind.Pipe) ||
  23109. this._pipeResolver.isPipe(type);
  23110. }
  23111. isNgModule(type) {
  23112. return !!this._loadSummary(type, CompileSummaryKind.NgModule) ||
  23113. this._ngModuleResolver.isNgModule(type);
  23114. }
  23115. getNgModuleSummary(moduleType, alreadyCollecting = null) {
  23116. let moduleSummary = this._loadSummary(moduleType, CompileSummaryKind.NgModule);
  23117. if (!moduleSummary) {
  23118. const moduleMeta = this.getNgModuleMetadata(moduleType, false, alreadyCollecting);
  23119. moduleSummary = moduleMeta ? moduleMeta.toSummary() : null;
  23120. if (moduleSummary) {
  23121. this._summaryCache.set(moduleType, moduleSummary);
  23122. }
  23123. }
  23124. return moduleSummary;
  23125. }
  23126. /**
  23127. * Loads the declared directives and pipes of an NgModule.
  23128. */
  23129. loadNgModuleDirectiveAndPipeMetadata(moduleType, isSync, throwIfNotFound = true) {
  23130. const ngModule = this.getNgModuleMetadata(moduleType, throwIfNotFound);
  23131. const loading = [];
  23132. if (ngModule) {
  23133. ngModule.declaredDirectives.forEach((id) => {
  23134. const promise = this.loadDirectiveMetadata(moduleType, id.reference, isSync);
  23135. if (promise) {
  23136. loading.push(promise);
  23137. }
  23138. });
  23139. ngModule.declaredPipes.forEach((id) => this._loadPipeMetadata(id.reference));
  23140. }
  23141. return Promise.all(loading);
  23142. }
  23143. getShallowModuleMetadata(moduleType) {
  23144. let compileMeta = this._shallowModuleCache.get(moduleType);
  23145. if (compileMeta) {
  23146. return compileMeta;
  23147. }
  23148. const ngModuleMeta = findLast(this._reflector.shallowAnnotations(moduleType), createNgModule.isTypeOf);
  23149. compileMeta = {
  23150. type: this._getTypeMetadata(moduleType),
  23151. rawExports: ngModuleMeta.exports,
  23152. rawImports: ngModuleMeta.imports,
  23153. rawProviders: ngModuleMeta.providers,
  23154. };
  23155. this._shallowModuleCache.set(moduleType, compileMeta);
  23156. return compileMeta;
  23157. }
  23158. getNgModuleMetadata(moduleType, throwIfNotFound = true, alreadyCollecting = null) {
  23159. moduleType = resolveForwardRef(moduleType);
  23160. let compileMeta = this._ngModuleCache.get(moduleType);
  23161. if (compileMeta) {
  23162. return compileMeta;
  23163. }
  23164. const meta = this._ngModuleResolver.resolve(moduleType, throwIfNotFound);
  23165. if (!meta) {
  23166. return null;
  23167. }
  23168. const declaredDirectives = [];
  23169. const exportedNonModuleIdentifiers = [];
  23170. const declaredPipes = [];
  23171. const importedModules = [];
  23172. const exportedModules = [];
  23173. const providers = [];
  23174. const entryComponents = [];
  23175. const bootstrapComponents = [];
  23176. const schemas = [];
  23177. if (meta.imports) {
  23178. flattenAndDedupeArray(meta.imports).forEach((importedType) => {
  23179. let importedModuleType = undefined;
  23180. if (isValidType(importedType)) {
  23181. importedModuleType = importedType;
  23182. }
  23183. else if (importedType && importedType.ngModule) {
  23184. const moduleWithProviders = importedType;
  23185. importedModuleType = moduleWithProviders.ngModule;
  23186. if (moduleWithProviders.providers) {
  23187. providers.push(...this._getProvidersMetadata(moduleWithProviders.providers, entryComponents, `provider for the NgModule '${stringifyType(importedModuleType)}'`, [], importedType));
  23188. }
  23189. }
  23190. if (importedModuleType) {
  23191. if (this._checkSelfImport(moduleType, importedModuleType))
  23192. return;
  23193. if (!alreadyCollecting)
  23194. alreadyCollecting = new Set();
  23195. if (alreadyCollecting.has(importedModuleType)) {
  23196. this._reportError(syntaxError(`${this._getTypeDescriptor(importedModuleType)} '${stringifyType(importedType)}' is imported recursively by the module '${stringifyType(moduleType)}'.`), moduleType);
  23197. return;
  23198. }
  23199. alreadyCollecting.add(importedModuleType);
  23200. const importedModuleSummary = this.getNgModuleSummary(importedModuleType, alreadyCollecting);
  23201. alreadyCollecting.delete(importedModuleType);
  23202. if (!importedModuleSummary) {
  23203. const err = syntaxError(`Unexpected ${this._getTypeDescriptor(importedType)} '${stringifyType(importedType)}' imported by the module '${stringifyType(moduleType)}'. Please add a @NgModule annotation.`);
  23204. // If possible, record additional context for this error to enable more useful
  23205. // diagnostics on the compiler side.
  23206. if (importedType instanceof StaticSymbol) {
  23207. err[MISSING_NG_MODULE_METADATA_ERROR_DATA] = {
  23208. fileName: importedType.filePath,
  23209. className: importedType.name,
  23210. };
  23211. }
  23212. this._reportError(err, moduleType);
  23213. return;
  23214. }
  23215. importedModules.push(importedModuleSummary);
  23216. }
  23217. else {
  23218. this._reportError(syntaxError(`Unexpected value '${stringifyType(importedType)}' imported by the module '${stringifyType(moduleType)}'`), moduleType);
  23219. return;
  23220. }
  23221. });
  23222. }
  23223. if (meta.exports) {
  23224. flattenAndDedupeArray(meta.exports).forEach((exportedType) => {
  23225. if (!isValidType(exportedType)) {
  23226. this._reportError(syntaxError(`Unexpected value '${stringifyType(exportedType)}' exported by the module '${stringifyType(moduleType)}'`), moduleType);
  23227. return;
  23228. }
  23229. if (!alreadyCollecting)
  23230. alreadyCollecting = new Set();
  23231. if (alreadyCollecting.has(exportedType)) {
  23232. this._reportError(syntaxError(`${this._getTypeDescriptor(exportedType)} '${stringify(exportedType)}' is exported recursively by the module '${stringifyType(moduleType)}'`), moduleType);
  23233. return;
  23234. }
  23235. alreadyCollecting.add(exportedType);
  23236. const exportedModuleSummary = this.getNgModuleSummary(exportedType, alreadyCollecting);
  23237. alreadyCollecting.delete(exportedType);
  23238. if (exportedModuleSummary) {
  23239. exportedModules.push(exportedModuleSummary);
  23240. }
  23241. else {
  23242. exportedNonModuleIdentifiers.push(this._getIdentifierMetadata(exportedType));
  23243. }
  23244. });
  23245. }
  23246. // Note: This will be modified later, so we rely on
  23247. // getting a new instance every time!
  23248. const transitiveModule = this._getTransitiveNgModuleMetadata(importedModules, exportedModules);
  23249. if (meta.declarations) {
  23250. flattenAndDedupeArray(meta.declarations).forEach((declaredType) => {
  23251. if (!isValidType(declaredType)) {
  23252. this._reportError(syntaxError(`Unexpected value '${stringifyType(declaredType)}' declared by the module '${stringifyType(moduleType)}'`), moduleType);
  23253. return;
  23254. }
  23255. const declaredIdentifier = this._getIdentifierMetadata(declaredType);
  23256. if (this.isDirective(declaredType)) {
  23257. if (this.isAbstractDirective(declaredType)) {
  23258. this._reportError(syntaxError(`Directive ${stringifyType(declaredType)} has no selector, please add it!`), declaredType);
  23259. }
  23260. transitiveModule.addDirective(declaredIdentifier);
  23261. declaredDirectives.push(declaredIdentifier);
  23262. this._addTypeToModule(declaredType, moduleType);
  23263. }
  23264. else if (this.isPipe(declaredType)) {
  23265. transitiveModule.addPipe(declaredIdentifier);
  23266. transitiveModule.pipes.push(declaredIdentifier);
  23267. declaredPipes.push(declaredIdentifier);
  23268. this._addTypeToModule(declaredType, moduleType);
  23269. }
  23270. else {
  23271. this._reportError(syntaxError(`Unexpected ${this._getTypeDescriptor(declaredType)} '${stringifyType(declaredType)}' declared by the module '${stringifyType(moduleType)}'. Please add a @Pipe/@Directive/@Component annotation.`), moduleType);
  23272. return;
  23273. }
  23274. });
  23275. }
  23276. const exportedDirectives = [];
  23277. const exportedPipes = [];
  23278. exportedNonModuleIdentifiers.forEach((exportedId) => {
  23279. if (transitiveModule.directivesSet.has(exportedId.reference)) {
  23280. exportedDirectives.push(exportedId);
  23281. transitiveModule.addExportedDirective(exportedId);
  23282. }
  23283. else if (transitiveModule.pipesSet.has(exportedId.reference)) {
  23284. exportedPipes.push(exportedId);
  23285. transitiveModule.addExportedPipe(exportedId);
  23286. }
  23287. else {
  23288. this._reportError(syntaxError(`Can't export ${this._getTypeDescriptor(exportedId.reference)} ${stringifyType(exportedId.reference)} from ${stringifyType(moduleType)} as it was neither declared nor imported!`), moduleType);
  23289. return;
  23290. }
  23291. });
  23292. // The providers of the module have to go last
  23293. // so that they overwrite any other provider we already added.
  23294. if (meta.providers) {
  23295. providers.push(...this._getProvidersMetadata(meta.providers, entryComponents, `provider for the NgModule '${stringifyType(moduleType)}'`, [], moduleType));
  23296. }
  23297. if (meta.entryComponents) {
  23298. entryComponents.push(...flattenAndDedupeArray(meta.entryComponents)
  23299. .map(type => this._getEntryComponentMetadata(type)));
  23300. }
  23301. if (meta.bootstrap) {
  23302. flattenAndDedupeArray(meta.bootstrap).forEach(type => {
  23303. if (!isValidType(type)) {
  23304. this._reportError(syntaxError(`Unexpected value '${stringifyType(type)}' used in the bootstrap property of module '${stringifyType(moduleType)}'`), moduleType);
  23305. return;
  23306. }
  23307. bootstrapComponents.push(this._getIdentifierMetadata(type));
  23308. });
  23309. }
  23310. entryComponents.push(...bootstrapComponents.map(type => this._getEntryComponentMetadata(type.reference)));
  23311. if (meta.schemas) {
  23312. schemas.push(...flattenAndDedupeArray(meta.schemas));
  23313. }
  23314. compileMeta = new CompileNgModuleMetadata({
  23315. type: this._getTypeMetadata(moduleType),
  23316. providers,
  23317. entryComponents,
  23318. bootstrapComponents,
  23319. schemas,
  23320. declaredDirectives,
  23321. exportedDirectives,
  23322. declaredPipes,
  23323. exportedPipes,
  23324. importedModules,
  23325. exportedModules,
  23326. transitiveModule,
  23327. id: meta.id || null,
  23328. });
  23329. entryComponents.forEach((id) => transitiveModule.addEntryComponent(id));
  23330. providers.forEach((provider) => transitiveModule.addProvider(provider, compileMeta.type));
  23331. transitiveModule.addModule(compileMeta.type);
  23332. this._ngModuleCache.set(moduleType, compileMeta);
  23333. return compileMeta;
  23334. }
  23335. _checkSelfImport(moduleType, importedModuleType) {
  23336. if (moduleType === importedModuleType) {
  23337. this._reportError(syntaxError(`'${stringifyType(moduleType)}' module can't import itself`), moduleType);
  23338. return true;
  23339. }
  23340. return false;
  23341. }
  23342. _getTypeDescriptor(type) {
  23343. if (isValidType(type)) {
  23344. if (this.isDirective(type)) {
  23345. return 'directive';
  23346. }
  23347. if (this.isPipe(type)) {
  23348. return 'pipe';
  23349. }
  23350. if (this.isNgModule(type)) {
  23351. return 'module';
  23352. }
  23353. }
  23354. if (type.provide) {
  23355. return 'provider';
  23356. }
  23357. return 'value';
  23358. }
  23359. _addTypeToModule(type, moduleType) {
  23360. const oldModule = this._ngModuleOfTypes.get(type);
  23361. if (oldModule && oldModule !== moduleType) {
  23362. this._reportError(syntaxError(`Type ${stringifyType(type)} is part of the declarations of 2 modules: ${stringifyType(oldModule)} and ${stringifyType(moduleType)}! ` +
  23363. `Please consider moving ${stringifyType(type)} to a higher module that imports ${stringifyType(oldModule)} and ${stringifyType(moduleType)}. ` +
  23364. `You can also create a new NgModule that exports and includes ${stringifyType(type)} then import that NgModule in ${stringifyType(oldModule)} and ${stringifyType(moduleType)}.`), moduleType);
  23365. return;
  23366. }
  23367. this._ngModuleOfTypes.set(type, moduleType);
  23368. }
  23369. _getTransitiveNgModuleMetadata(importedModules, exportedModules) {
  23370. // collect `providers` / `entryComponents` from all imported and all exported modules
  23371. const result = new TransitiveCompileNgModuleMetadata();
  23372. const modulesByToken = new Map();
  23373. importedModules.concat(exportedModules).forEach((modSummary) => {
  23374. modSummary.modules.forEach((mod) => result.addModule(mod));
  23375. modSummary.entryComponents.forEach((comp) => result.addEntryComponent(comp));
  23376. const addedTokens = new Set();
  23377. modSummary.providers.forEach((entry) => {
  23378. const tokenRef = tokenReference(entry.provider.token);
  23379. let prevModules = modulesByToken.get(tokenRef);
  23380. if (!prevModules) {
  23381. prevModules = new Set();
  23382. modulesByToken.set(tokenRef, prevModules);
  23383. }
  23384. const moduleRef = entry.module.reference;
  23385. // Note: the providers of one module may still contain multiple providers
  23386. // per token (e.g. for multi providers), and we need to preserve these.
  23387. if (addedTokens.has(tokenRef) || !prevModules.has(moduleRef)) {
  23388. prevModules.add(moduleRef);
  23389. addedTokens.add(tokenRef);
  23390. result.addProvider(entry.provider, entry.module);
  23391. }
  23392. });
  23393. });
  23394. exportedModules.forEach((modSummary) => {
  23395. modSummary.exportedDirectives.forEach((id) => result.addExportedDirective(id));
  23396. modSummary.exportedPipes.forEach((id) => result.addExportedPipe(id));
  23397. });
  23398. importedModules.forEach((modSummary) => {
  23399. modSummary.exportedDirectives.forEach((id) => result.addDirective(id));
  23400. modSummary.exportedPipes.forEach((id) => result.addPipe(id));
  23401. });
  23402. return result;
  23403. }
  23404. _getIdentifierMetadata(type) {
  23405. type = resolveForwardRef(type);
  23406. return { reference: type };
  23407. }
  23408. isInjectable(type) {
  23409. const annotations = this._reflector.tryAnnotations(type);
  23410. return annotations.some(ann => createInjectable.isTypeOf(ann));
  23411. }
  23412. getInjectableSummary(type) {
  23413. return {
  23414. summaryKind: CompileSummaryKind.Injectable,
  23415. type: this._getTypeMetadata(type, null, false)
  23416. };
  23417. }
  23418. getInjectableMetadata(type, dependencies = null, throwOnUnknownDeps = true) {
  23419. const typeSummary = this._loadSummary(type, CompileSummaryKind.Injectable);
  23420. const typeMetadata = typeSummary ?
  23421. typeSummary.type :
  23422. this._getTypeMetadata(type, dependencies, throwOnUnknownDeps);
  23423. const annotations = this._reflector.annotations(type).filter(ann => createInjectable.isTypeOf(ann));
  23424. if (annotations.length === 0) {
  23425. return null;
  23426. }
  23427. const meta = annotations[annotations.length - 1];
  23428. return {
  23429. symbol: type,
  23430. type: typeMetadata,
  23431. providedIn: meta.providedIn,
  23432. useValue: meta.useValue,
  23433. useClass: meta.useClass,
  23434. useExisting: meta.useExisting,
  23435. useFactory: meta.useFactory,
  23436. deps: meta.deps,
  23437. };
  23438. }
  23439. _getTypeMetadata(type, dependencies = null, throwOnUnknownDeps = true) {
  23440. const identifier = this._getIdentifierMetadata(type);
  23441. return {
  23442. reference: identifier.reference,
  23443. diDeps: this._getDependenciesMetadata(identifier.reference, dependencies, throwOnUnknownDeps),
  23444. lifecycleHooks: getAllLifecycleHooks(this._reflector, identifier.reference),
  23445. };
  23446. }
  23447. _getFactoryMetadata(factory, dependencies = null) {
  23448. factory = resolveForwardRef(factory);
  23449. return { reference: factory, diDeps: this._getDependenciesMetadata(factory, dependencies) };
  23450. }
  23451. /**
  23452. * Gets the metadata for the given pipe.
  23453. * This assumes `loadNgModuleDirectiveAndPipeMetadata` has been called first.
  23454. */
  23455. getPipeMetadata(pipeType) {
  23456. const pipeMeta = this._pipeCache.get(pipeType);
  23457. if (!pipeMeta) {
  23458. this._reportError(syntaxError(`Illegal state: getPipeMetadata can only be called after loadNgModuleDirectiveAndPipeMetadata for a module that declares it. Pipe ${stringifyType(pipeType)}.`), pipeType);
  23459. }
  23460. return pipeMeta || null;
  23461. }
  23462. getPipeSummary(pipeType) {
  23463. const pipeSummary = this._loadSummary(pipeType, CompileSummaryKind.Pipe);
  23464. if (!pipeSummary) {
  23465. this._reportError(syntaxError(`Illegal state: Could not load the summary for pipe ${stringifyType(pipeType)}.`), pipeType);
  23466. }
  23467. return pipeSummary;
  23468. }
  23469. getOrLoadPipeMetadata(pipeType) {
  23470. let pipeMeta = this._pipeCache.get(pipeType);
  23471. if (!pipeMeta) {
  23472. pipeMeta = this._loadPipeMetadata(pipeType);
  23473. }
  23474. return pipeMeta;
  23475. }
  23476. _loadPipeMetadata(pipeType) {
  23477. pipeType = resolveForwardRef(pipeType);
  23478. const pipeAnnotation = this._pipeResolver.resolve(pipeType);
  23479. const pipeMeta = new CompilePipeMetadata({
  23480. type: this._getTypeMetadata(pipeType),
  23481. name: pipeAnnotation.name,
  23482. pure: !!pipeAnnotation.pure
  23483. });
  23484. this._pipeCache.set(pipeType, pipeMeta);
  23485. this._summaryCache.set(pipeType, pipeMeta.toSummary());
  23486. return pipeMeta;
  23487. }
  23488. _getDependenciesMetadata(typeOrFunc, dependencies, throwOnUnknownDeps = true) {
  23489. let hasUnknownDeps = false;
  23490. const params = dependencies || this._reflector.parameters(typeOrFunc) || [];
  23491. const dependenciesMetadata = params.map((param) => {
  23492. let isAttribute = false;
  23493. let isHost = false;
  23494. let isSelf = false;
  23495. let isSkipSelf = false;
  23496. let isOptional = false;
  23497. let token = null;
  23498. if (Array.isArray(param)) {
  23499. param.forEach((paramEntry) => {
  23500. if (createHost.isTypeOf(paramEntry)) {
  23501. isHost = true;
  23502. }
  23503. else if (createSelf.isTypeOf(paramEntry)) {
  23504. isSelf = true;
  23505. }
  23506. else if (createSkipSelf.isTypeOf(paramEntry)) {
  23507. isSkipSelf = true;
  23508. }
  23509. else if (createOptional.isTypeOf(paramEntry)) {
  23510. isOptional = true;
  23511. }
  23512. else if (createAttribute.isTypeOf(paramEntry)) {
  23513. isAttribute = true;
  23514. token = paramEntry.attributeName;
  23515. }
  23516. else if (createInject.isTypeOf(paramEntry)) {
  23517. token = paramEntry.token;
  23518. }
  23519. else if (createInjectionToken.isTypeOf(paramEntry) ||
  23520. paramEntry instanceof StaticSymbol) {
  23521. token = paramEntry;
  23522. }
  23523. else if (isValidType(paramEntry) && token == null) {
  23524. token = paramEntry;
  23525. }
  23526. });
  23527. }
  23528. else {
  23529. token = param;
  23530. }
  23531. if (token == null) {
  23532. hasUnknownDeps = true;
  23533. return {};
  23534. }
  23535. return {
  23536. isAttribute,
  23537. isHost,
  23538. isSelf,
  23539. isSkipSelf,
  23540. isOptional,
  23541. token: this._getTokenMetadata(token)
  23542. };
  23543. });
  23544. if (hasUnknownDeps) {
  23545. const depsTokens = dependenciesMetadata.map((dep) => dep.token ? stringifyType(dep.token) : '?').join(', ');
  23546. const message = `Can't resolve all parameters for ${stringifyType(typeOrFunc)}: (${depsTokens}).`;
  23547. if (throwOnUnknownDeps || this._config.strictInjectionParameters) {
  23548. this._reportError(syntaxError(message), typeOrFunc);
  23549. }
  23550. }
  23551. return dependenciesMetadata;
  23552. }
  23553. _getTokenMetadata(token) {
  23554. token = resolveForwardRef(token);
  23555. let compileToken;
  23556. if (typeof token === 'string') {
  23557. compileToken = { value: token };
  23558. }
  23559. else {
  23560. compileToken = { identifier: { reference: token } };
  23561. }
  23562. return compileToken;
  23563. }
  23564. _getProvidersMetadata(providers, targetEntryComponents, debugInfo, compileProviders = [], type) {
  23565. providers.forEach((provider, providerIdx) => {
  23566. if (Array.isArray(provider)) {
  23567. this._getProvidersMetadata(provider, targetEntryComponents, debugInfo, compileProviders);
  23568. }
  23569. else {
  23570. provider = resolveForwardRef(provider);
  23571. let providerMeta = undefined;
  23572. if (provider && typeof provider === 'object' && provider.hasOwnProperty('provide')) {
  23573. this._validateProvider(provider);
  23574. providerMeta = new ProviderMeta(provider.provide, provider);
  23575. }
  23576. else if (isValidType(provider)) {
  23577. providerMeta = new ProviderMeta(provider, { useClass: provider });
  23578. }
  23579. else if (provider === void 0) {
  23580. this._reportError(syntaxError(`Encountered undefined provider! Usually this means you have a circular dependencies. This might be caused by using 'barrel' index.ts files.`));
  23581. return;
  23582. }
  23583. else {
  23584. const providersInfo = providers
  23585. .reduce((soFar, seenProvider, seenProviderIdx) => {
  23586. if (seenProviderIdx < providerIdx) {
  23587. soFar.push(`${stringifyType(seenProvider)}`);
  23588. }
  23589. else if (seenProviderIdx == providerIdx) {
  23590. soFar.push(`?${stringifyType(seenProvider)}?`);
  23591. }
  23592. else if (seenProviderIdx == providerIdx + 1) {
  23593. soFar.push('...');
  23594. }
  23595. return soFar;
  23596. }, [])
  23597. .join(', ');
  23598. this._reportError(syntaxError(`Invalid ${debugInfo ?
  23599. debugInfo :
  23600. 'provider'} - only instances of Provider and Type are allowed, got: [${providersInfo}]`), type);
  23601. return;
  23602. }
  23603. if (providerMeta.token ===
  23604. this._reflector.resolveExternalReference(Identifiers$1.ANALYZE_FOR_ENTRY_COMPONENTS)) {
  23605. targetEntryComponents.push(...this._getEntryComponentsFromProvider(providerMeta, type));
  23606. }
  23607. else {
  23608. compileProviders.push(this.getProviderMetadata(providerMeta));
  23609. }
  23610. }
  23611. });
  23612. return compileProviders;
  23613. }
  23614. _validateProvider(provider) {
  23615. if (provider.hasOwnProperty('useClass') && provider.useClass == null) {
  23616. this._reportError(syntaxError(`Invalid provider for ${stringifyType(provider.provide)}. useClass cannot be ${provider.useClass}.
  23617. Usually it happens when:
  23618. 1. There's a circular dependency (might be caused by using index.ts (barrel) files).
  23619. 2. Class was used before it was declared. Use forwardRef in this case.`));
  23620. }
  23621. }
  23622. _getEntryComponentsFromProvider(provider, type) {
  23623. const components = [];
  23624. const collectedIdentifiers = [];
  23625. if (provider.useFactory || provider.useExisting || provider.useClass) {
  23626. this._reportError(syntaxError(`The ANALYZE_FOR_ENTRY_COMPONENTS token only supports useValue!`), type);
  23627. return [];
  23628. }
  23629. if (!provider.multi) {
  23630. this._reportError(syntaxError(`The ANALYZE_FOR_ENTRY_COMPONENTS token only supports 'multi = true'!`), type);
  23631. return [];
  23632. }
  23633. extractIdentifiers(provider.useValue, collectedIdentifiers);
  23634. collectedIdentifiers.forEach((identifier) => {
  23635. const entry = this._getEntryComponentMetadata(identifier.reference, false);
  23636. if (entry) {
  23637. components.push(entry);
  23638. }
  23639. });
  23640. return components;
  23641. }
  23642. _getEntryComponentMetadata(dirType, throwIfNotFound = true) {
  23643. const dirMeta = this.getNonNormalizedDirectiveMetadata(dirType);
  23644. if (dirMeta && dirMeta.metadata.isComponent) {
  23645. return { componentType: dirType, componentFactory: dirMeta.metadata.componentFactory };
  23646. }
  23647. const dirSummary = this._loadSummary(dirType, CompileSummaryKind.Directive);
  23648. if (dirSummary && dirSummary.isComponent) {
  23649. return { componentType: dirType, componentFactory: dirSummary.componentFactory };
  23650. }
  23651. if (throwIfNotFound) {
  23652. throw syntaxError(`${dirType.name} cannot be used as an entry component.`);
  23653. }
  23654. return null;
  23655. }
  23656. _getInjectableTypeMetadata(type, dependencies = null) {
  23657. const typeSummary = this._loadSummary(type, CompileSummaryKind.Injectable);
  23658. if (typeSummary) {
  23659. return typeSummary.type;
  23660. }
  23661. return this._getTypeMetadata(type, dependencies);
  23662. }
  23663. getProviderMetadata(provider) {
  23664. let compileDeps = undefined;
  23665. let compileTypeMetadata = null;
  23666. let compileFactoryMetadata = null;
  23667. let token = this._getTokenMetadata(provider.token);
  23668. if (provider.useClass) {
  23669. compileTypeMetadata =
  23670. this._getInjectableTypeMetadata(provider.useClass, provider.dependencies);
  23671. compileDeps = compileTypeMetadata.diDeps;
  23672. if (provider.token === provider.useClass) {
  23673. // use the compileTypeMetadata as it contains information about lifecycleHooks...
  23674. token = { identifier: compileTypeMetadata };
  23675. }
  23676. }
  23677. else if (provider.useFactory) {
  23678. compileFactoryMetadata = this._getFactoryMetadata(provider.useFactory, provider.dependencies);
  23679. compileDeps = compileFactoryMetadata.diDeps;
  23680. }
  23681. return {
  23682. token: token,
  23683. useClass: compileTypeMetadata,
  23684. useValue: provider.useValue,
  23685. useFactory: compileFactoryMetadata,
  23686. useExisting: provider.useExisting ? this._getTokenMetadata(provider.useExisting) : undefined,
  23687. deps: compileDeps,
  23688. multi: provider.multi
  23689. };
  23690. }
  23691. _getQueriesMetadata(queries, isViewQuery, directiveType) {
  23692. const res = [];
  23693. Object.keys(queries).forEach((propertyName) => {
  23694. const query = queries[propertyName];
  23695. if (query.isViewQuery === isViewQuery) {
  23696. res.push(this._getQueryMetadata(query, propertyName, directiveType));
  23697. }
  23698. });
  23699. return res;
  23700. }
  23701. _queryVarBindings(selector) {
  23702. return selector.split(/\s*,\s*/);
  23703. }
  23704. _getQueryMetadata(q, propertyName, typeOrFunc) {
  23705. let selectors;
  23706. if (typeof q.selector === 'string') {
  23707. selectors =
  23708. this._queryVarBindings(q.selector).map(varName => this._getTokenMetadata(varName));
  23709. }
  23710. else {
  23711. if (!q.selector) {
  23712. this._reportError(syntaxError(`Can't construct a query for the property "${propertyName}" of "${stringifyType(typeOrFunc)}" since the query selector wasn't defined.`), typeOrFunc);
  23713. selectors = [];
  23714. }
  23715. else {
  23716. selectors = [this._getTokenMetadata(q.selector)];
  23717. }
  23718. }
  23719. return {
  23720. selectors,
  23721. first: q.first,
  23722. descendants: q.descendants,
  23723. emitDistinctChangesOnly: q.emitDistinctChangesOnly,
  23724. propertyName,
  23725. read: q.read ? this._getTokenMetadata(q.read) : null,
  23726. static: q.static
  23727. };
  23728. }
  23729. _reportError(error, type, otherType) {
  23730. if (this._errorCollector) {
  23731. this._errorCollector(error, type);
  23732. if (otherType) {
  23733. this._errorCollector(error, otherType);
  23734. }
  23735. }
  23736. else {
  23737. throw error;
  23738. }
  23739. }
  23740. }
  23741. function flattenArray(tree, out = []) {
  23742. if (tree) {
  23743. for (let i = 0; i < tree.length; i++) {
  23744. const item = resolveForwardRef(tree[i]);
  23745. if (Array.isArray(item)) {
  23746. flattenArray(item, out);
  23747. }
  23748. else {
  23749. out.push(item);
  23750. }
  23751. }
  23752. }
  23753. return out;
  23754. }
  23755. function dedupeArray(array) {
  23756. if (array) {
  23757. return Array.from(new Set(array));
  23758. }
  23759. return [];
  23760. }
  23761. function flattenAndDedupeArray(tree) {
  23762. return dedupeArray(flattenArray(tree));
  23763. }
  23764. function isValidType(value) {
  23765. return (value instanceof StaticSymbol) || (value instanceof Type);
  23766. }
  23767. function extractIdentifiers(value, targetIdentifiers) {
  23768. visitValue(value, new _CompileValueConverter(), targetIdentifiers);
  23769. }
  23770. class _CompileValueConverter extends ValueTransformer {
  23771. visitOther(value, targetIdentifiers) {
  23772. targetIdentifiers.push({ reference: value });
  23773. }
  23774. }
  23775. function stringifyType(type) {
  23776. if (type instanceof StaticSymbol) {
  23777. return `${type.name} in ${type.filePath}`;
  23778. }
  23779. else {
  23780. return stringify(type);
  23781. }
  23782. }
  23783. /**
  23784. * Indicates that a component is still being loaded in a synchronous compile.
  23785. */
  23786. function componentStillLoadingError(compType) {
  23787. const error = Error(`Can't compile synchronously as ${stringify(compType)} is still being loaded!`);
  23788. error[ERROR_COMPONENT_TYPE] = compType;
  23789. return error;
  23790. }
  23791. /**
  23792. * @license
  23793. * Copyright Google LLC All Rights Reserved.
  23794. *
  23795. * Use of this source code is governed by an MIT-style license that can be
  23796. * found in the LICENSE file at https://angular.io/license
  23797. */
  23798. function providerDef(ctx, providerAst) {
  23799. let flags = 0 /* None */;
  23800. if (!providerAst.eager) {
  23801. flags |= 4096 /* LazyProvider */;
  23802. }
  23803. if (providerAst.providerType === ProviderAstType.PrivateService) {
  23804. flags |= 8192 /* PrivateProvider */;
  23805. }
  23806. if (providerAst.isModule) {
  23807. flags |= 1073741824 /* TypeModuleProvider */;
  23808. }
  23809. providerAst.lifecycleHooks.forEach((lifecycleHook) => {
  23810. // for regular providers, we only support ngOnDestroy
  23811. if (lifecycleHook === LifecycleHooks.OnDestroy ||
  23812. providerAst.providerType === ProviderAstType.Directive ||
  23813. providerAst.providerType === ProviderAstType.Component) {
  23814. flags |= lifecycleHookToNodeFlag(lifecycleHook);
  23815. }
  23816. });
  23817. const { providerExpr, flags: providerFlags, depsExpr } = providerAst.multiProvider ?
  23818. multiProviderDef(ctx, flags, providerAst.providers) :
  23819. singleProviderDef(ctx, flags, providerAst.providerType, providerAst.providers[0]);
  23820. return {
  23821. providerExpr,
  23822. flags: providerFlags,
  23823. depsExpr,
  23824. tokenExpr: tokenExpr(ctx, providerAst.token),
  23825. };
  23826. }
  23827. function multiProviderDef(ctx, flags, providers) {
  23828. const allDepDefs = [];
  23829. const allParams = [];
  23830. const exprs = providers.map((provider, providerIndex) => {
  23831. let expr;
  23832. if (provider.useClass) {
  23833. const depExprs = convertDeps(providerIndex, provider.deps || provider.useClass.diDeps);
  23834. expr = ctx.importExpr(provider.useClass.reference).instantiate(depExprs);
  23835. }
  23836. else if (provider.useFactory) {
  23837. const depExprs = convertDeps(providerIndex, provider.deps || provider.useFactory.diDeps);
  23838. expr = ctx.importExpr(provider.useFactory.reference).callFn(depExprs);
  23839. }
  23840. else if (provider.useExisting) {
  23841. const depExprs = convertDeps(providerIndex, [{ token: provider.useExisting }]);
  23842. expr = depExprs[0];
  23843. }
  23844. else {
  23845. expr = convertValueToOutputAst(ctx, provider.useValue);
  23846. }
  23847. return expr;
  23848. });
  23849. const providerExpr = fn(allParams, [new ReturnStatement(literalArr(exprs))], INFERRED_TYPE);
  23850. return {
  23851. providerExpr,
  23852. flags: flags | 1024 /* TypeFactoryProvider */,
  23853. depsExpr: literalArr(allDepDefs)
  23854. };
  23855. function convertDeps(providerIndex, deps) {
  23856. return deps.map((dep, depIndex) => {
  23857. const paramName = `p${providerIndex}_${depIndex}`;
  23858. allParams.push(new FnParam(paramName, DYNAMIC_TYPE));
  23859. allDepDefs.push(depDef(ctx, dep));
  23860. return variable(paramName);
  23861. });
  23862. }
  23863. }
  23864. function singleProviderDef(ctx, flags, providerType, providerMeta) {
  23865. let providerExpr;
  23866. let deps;
  23867. if (providerType === ProviderAstType.Directive || providerType === ProviderAstType.Component) {
  23868. providerExpr = ctx.importExpr(providerMeta.useClass.reference);
  23869. flags |= 16384 /* TypeDirective */;
  23870. deps = providerMeta.deps || providerMeta.useClass.diDeps;
  23871. }
  23872. else {
  23873. if (providerMeta.useClass) {
  23874. providerExpr = ctx.importExpr(providerMeta.useClass.reference);
  23875. flags |= 512 /* TypeClassProvider */;
  23876. deps = providerMeta.deps || providerMeta.useClass.diDeps;
  23877. }
  23878. else if (providerMeta.useFactory) {
  23879. providerExpr = ctx.importExpr(providerMeta.useFactory.reference);
  23880. flags |= 1024 /* TypeFactoryProvider */;
  23881. deps = providerMeta.deps || providerMeta.useFactory.diDeps;
  23882. }
  23883. else if (providerMeta.useExisting) {
  23884. providerExpr = NULL_EXPR;
  23885. flags |= 2048 /* TypeUseExistingProvider */;
  23886. deps = [{ token: providerMeta.useExisting }];
  23887. }
  23888. else {
  23889. providerExpr = convertValueToOutputAst(ctx, providerMeta.useValue);
  23890. flags |= 256 /* TypeValueProvider */;
  23891. deps = [];
  23892. }
  23893. }
  23894. const depsExpr = literalArr(deps.map(dep => depDef(ctx, dep)));
  23895. return { providerExpr, flags, depsExpr };
  23896. }
  23897. function tokenExpr(ctx, tokenMeta) {
  23898. return tokenMeta.identifier ? ctx.importExpr(tokenMeta.identifier.reference) :
  23899. literal(tokenMeta.value);
  23900. }
  23901. function depDef(ctx, dep) {
  23902. // Note: the following fields have already been normalized out by provider_analyzer:
  23903. // - isAttribute, isHost
  23904. const expr = dep.isValue ? convertValueToOutputAst(ctx, dep.value) : tokenExpr(ctx, dep.token);
  23905. let flags = 0 /* None */;
  23906. if (dep.isSkipSelf) {
  23907. flags |= 1 /* SkipSelf */;
  23908. }
  23909. if (dep.isOptional) {
  23910. flags |= 2 /* Optional */;
  23911. }
  23912. if (dep.isSelf) {
  23913. flags |= 4 /* Self */;
  23914. }
  23915. if (dep.isValue) {
  23916. flags |= 8 /* Value */;
  23917. }
  23918. return flags === 0 /* None */ ? expr : literalArr([literal(flags), expr]);
  23919. }
  23920. function lifecycleHookToNodeFlag(lifecycleHook) {
  23921. let nodeFlag = 0 /* None */;
  23922. switch (lifecycleHook) {
  23923. case LifecycleHooks.AfterContentChecked:
  23924. nodeFlag = 2097152 /* AfterContentChecked */;
  23925. break;
  23926. case LifecycleHooks.AfterContentInit:
  23927. nodeFlag = 1048576 /* AfterContentInit */;
  23928. break;
  23929. case LifecycleHooks.AfterViewChecked:
  23930. nodeFlag = 8388608 /* AfterViewChecked */;
  23931. break;
  23932. case LifecycleHooks.AfterViewInit:
  23933. nodeFlag = 4194304 /* AfterViewInit */;
  23934. break;
  23935. case LifecycleHooks.DoCheck:
  23936. nodeFlag = 262144 /* DoCheck */;
  23937. break;
  23938. case LifecycleHooks.OnChanges:
  23939. nodeFlag = 524288 /* OnChanges */;
  23940. break;
  23941. case LifecycleHooks.OnDestroy:
  23942. nodeFlag = 131072 /* OnDestroy */;
  23943. break;
  23944. case LifecycleHooks.OnInit:
  23945. nodeFlag = 65536 /* OnInit */;
  23946. break;
  23947. }
  23948. return nodeFlag;
  23949. }
  23950. function componentFactoryResolverProviderDef(reflector, ctx, flags, entryComponents) {
  23951. const entryComponentFactories = entryComponents.map((entryComponent) => ctx.importExpr(entryComponent.componentFactory));
  23952. const token = createTokenForExternalReference(reflector, Identifiers$1.ComponentFactoryResolver);
  23953. const classMeta = {
  23954. diDeps: [
  23955. { isValue: true, value: literalArr(entryComponentFactories) },
  23956. { token: token, isSkipSelf: true, isOptional: true },
  23957. { token: createTokenForExternalReference(reflector, Identifiers$1.NgModuleRef) },
  23958. ],
  23959. lifecycleHooks: [],
  23960. reference: reflector.resolveExternalReference(Identifiers$1.CodegenComponentFactoryResolver)
  23961. };
  23962. const { providerExpr, flags: providerFlags, depsExpr } = singleProviderDef(ctx, flags, ProviderAstType.PrivateService, {
  23963. token,
  23964. multi: false,
  23965. useClass: classMeta,
  23966. });
  23967. return { providerExpr, flags: providerFlags, depsExpr, tokenExpr: tokenExpr(ctx, token) };
  23968. }
  23969. /**
  23970. * @license
  23971. * Copyright Google LLC All Rights Reserved.
  23972. *
  23973. * Use of this source code is governed by an MIT-style license that can be
  23974. * found in the LICENSE file at https://angular.io/license
  23975. */
  23976. class NgModuleCompileResult {
  23977. constructor(ngModuleFactoryVar) {
  23978. this.ngModuleFactoryVar = ngModuleFactoryVar;
  23979. }
  23980. }
  23981. const LOG_VAR = variable('_l');
  23982. class NgModuleCompiler {
  23983. constructor(reflector) {
  23984. this.reflector = reflector;
  23985. }
  23986. compile(ctx, ngModuleMeta, extraProviders) {
  23987. const sourceSpan = typeSourceSpan('NgModule', ngModuleMeta.type);
  23988. const entryComponentFactories = ngModuleMeta.transitiveModule.entryComponents;
  23989. const bootstrapComponents = ngModuleMeta.bootstrapComponents;
  23990. const providerParser = new NgModuleProviderAnalyzer(this.reflector, ngModuleMeta, extraProviders, sourceSpan);
  23991. const providerDefs = [componentFactoryResolverProviderDef(this.reflector, ctx, 0 /* None */, entryComponentFactories)]
  23992. .concat(providerParser.parse().map((provider) => providerDef(ctx, provider)))
  23993. .map(({ providerExpr, depsExpr, flags, tokenExpr }) => {
  23994. return importExpr(Identifiers$1.moduleProviderDef).callFn([
  23995. literal(flags), tokenExpr, providerExpr, depsExpr
  23996. ]);
  23997. });
  23998. const ngModuleDef = importExpr(Identifiers$1.moduleDef).callFn([literalArr(providerDefs)]);
  23999. const ngModuleDefFactory = fn([new FnParam(LOG_VAR.name)], [new ReturnStatement(ngModuleDef)], INFERRED_TYPE);
  24000. const ngModuleFactoryVar = `${identifierName(ngModuleMeta.type)}NgFactory`;
  24001. this._createNgModuleFactory(ctx, ngModuleMeta.type.reference, importExpr(Identifiers$1.createModuleFactory).callFn([
  24002. ctx.importExpr(ngModuleMeta.type.reference),
  24003. literalArr(bootstrapComponents.map(id => ctx.importExpr(id.reference))),
  24004. ngModuleDefFactory
  24005. ]));
  24006. if (ngModuleMeta.id) {
  24007. const id = typeof ngModuleMeta.id === 'string' ? literal(ngModuleMeta.id) :
  24008. ctx.importExpr(ngModuleMeta.id);
  24009. const registerFactoryStmt = importExpr(Identifiers$1.RegisterModuleFactoryFn)
  24010. .callFn([id, variable(ngModuleFactoryVar)])
  24011. .toStmt();
  24012. ctx.statements.push(registerFactoryStmt);
  24013. }
  24014. return new NgModuleCompileResult(ngModuleFactoryVar);
  24015. }
  24016. createStub(ctx, ngModuleReference) {
  24017. this._createNgModuleFactory(ctx, ngModuleReference, NULL_EXPR);
  24018. }
  24019. _createNgModuleFactory(ctx, reference, value) {
  24020. const ngModuleFactoryVar = `${identifierName({ reference: reference })}NgFactory`;
  24021. const ngModuleFactoryStmt = variable(ngModuleFactoryVar)
  24022. .set(value)
  24023. .toDeclStmt(importType(Identifiers$1.NgModuleFactory, [expressionType(ctx.importExpr(reference))], [TypeModifier.Const]), [StmtModifier.Final, StmtModifier.Exported]);
  24024. ctx.statements.push(ngModuleFactoryStmt);
  24025. }
  24026. }
  24027. /**
  24028. * @license
  24029. * Copyright Google LLC All Rights Reserved.
  24030. *
  24031. * Use of this source code is governed by an MIT-style license that can be
  24032. * found in the LICENSE file at https://angular.io/license
  24033. */
  24034. /**
  24035. * Resolves types to {@link NgModule}.
  24036. */
  24037. class NgModuleResolver {
  24038. constructor(_reflector) {
  24039. this._reflector = _reflector;
  24040. }
  24041. isNgModule(type) {
  24042. return this._reflector.annotations(type).some(createNgModule.isTypeOf);
  24043. }
  24044. resolve(type, throwIfNotFound = true) {
  24045. const ngModuleMeta = findLast(this._reflector.annotations(type), createNgModule.isTypeOf);
  24046. if (ngModuleMeta) {
  24047. return ngModuleMeta;
  24048. }
  24049. else {
  24050. if (throwIfNotFound) {
  24051. throw new Error(`No NgModule metadata found for '${stringify(type)}'.`);
  24052. }
  24053. return null;
  24054. }
  24055. }
  24056. }
  24057. /**
  24058. * @license
  24059. * Copyright Google LLC All Rights Reserved.
  24060. *
  24061. * Use of this source code is governed by an MIT-style license that can be
  24062. * found in the LICENSE file at https://angular.io/license
  24063. */
  24064. function debugOutputAstAsTypeScript(ast) {
  24065. const converter = new _TsEmitterVisitor();
  24066. const ctx = EmitterVisitorContext.createRoot();
  24067. const asts = Array.isArray(ast) ? ast : [ast];
  24068. asts.forEach((ast) => {
  24069. if (ast instanceof Statement) {
  24070. ast.visitStatement(converter, ctx);
  24071. }
  24072. else if (ast instanceof Expression) {
  24073. ast.visitExpression(converter, ctx);
  24074. }
  24075. else if (ast instanceof Type$1) {
  24076. ast.visitType(converter, ctx);
  24077. }
  24078. else {
  24079. throw new Error(`Don't know how to print debug info for ${ast}`);
  24080. }
  24081. });
  24082. return ctx.toSource();
  24083. }
  24084. class TypeScriptEmitter {
  24085. emitStatementsAndContext(genFilePath, stmts, preamble = '', emitSourceMaps = true, referenceFilter, importFilter) {
  24086. const converter = new _TsEmitterVisitor(referenceFilter, importFilter);
  24087. const ctx = EmitterVisitorContext.createRoot();
  24088. converter.visitAllStatements(stmts, ctx);
  24089. const preambleLines = preamble ? preamble.split('\n') : [];
  24090. converter.reexports.forEach((reexports, exportedModuleName) => {
  24091. const reexportsCode = reexports.map(reexport => `${reexport.name} as ${reexport.as}`).join(',');
  24092. preambleLines.push(`export {${reexportsCode}} from '${exportedModuleName}';`);
  24093. });
  24094. converter.importsWithPrefixes.forEach((prefix, importedModuleName) => {
  24095. // Note: can't write the real word for import as it screws up system.js auto detection...
  24096. preambleLines.push(`imp` +
  24097. `ort * as ${prefix} from '${importedModuleName}';`);
  24098. });
  24099. const sm = emitSourceMaps ?
  24100. ctx.toSourceMapGenerator(genFilePath, preambleLines.length).toJsComment() :
  24101. '';
  24102. const lines = [...preambleLines, ctx.toSource(), sm];
  24103. if (sm) {
  24104. // always add a newline at the end, as some tools have bugs without it.
  24105. lines.push('');
  24106. }
  24107. ctx.setPreambleLineCount(preambleLines.length);
  24108. return { sourceText: lines.join('\n'), context: ctx };
  24109. }
  24110. emitStatements(genFilePath, stmts, preamble = '') {
  24111. return this.emitStatementsAndContext(genFilePath, stmts, preamble).sourceText;
  24112. }
  24113. }
  24114. class _TsEmitterVisitor extends AbstractEmitterVisitor {
  24115. constructor(referenceFilter, importFilter) {
  24116. super(false);
  24117. this.referenceFilter = referenceFilter;
  24118. this.importFilter = importFilter;
  24119. this.typeExpression = 0;
  24120. this.importsWithPrefixes = new Map();
  24121. this.reexports = new Map();
  24122. }
  24123. visitType(t, ctx, defaultType = 'any') {
  24124. if (t) {
  24125. this.typeExpression++;
  24126. t.visitType(this, ctx);
  24127. this.typeExpression--;
  24128. }
  24129. else {
  24130. ctx.print(null, defaultType);
  24131. }
  24132. }
  24133. visitLiteralExpr(ast, ctx) {
  24134. const value = ast.value;
  24135. if (value == null && ast.type != INFERRED_TYPE) {
  24136. ctx.print(ast, `(${value} as any)`);
  24137. return null;
  24138. }
  24139. return super.visitLiteralExpr(ast, ctx);
  24140. }
  24141. // Temporary workaround to support strictNullCheck enabled consumers of ngc emit.
  24142. // In SNC mode, [] have the type never[], so we cast here to any[].
  24143. // TODO: narrow the cast to a more explicit type, or use a pattern that does not
  24144. // start with [].concat. see https://github.com/angular/angular/pull/11846
  24145. visitLiteralArrayExpr(ast, ctx) {
  24146. if (ast.entries.length === 0) {
  24147. ctx.print(ast, '(');
  24148. }
  24149. const result = super.visitLiteralArrayExpr(ast, ctx);
  24150. if (ast.entries.length === 0) {
  24151. ctx.print(ast, ' as any[])');
  24152. }
  24153. return result;
  24154. }
  24155. visitExternalExpr(ast, ctx) {
  24156. this._visitIdentifier(ast.value, ast.typeParams, ctx);
  24157. return null;
  24158. }
  24159. visitAssertNotNullExpr(ast, ctx) {
  24160. const result = super.visitAssertNotNullExpr(ast, ctx);
  24161. ctx.print(ast, '!');
  24162. return result;
  24163. }
  24164. visitDeclareVarStmt(stmt, ctx) {
  24165. if (stmt.hasModifier(StmtModifier.Exported) && stmt.value instanceof ExternalExpr &&
  24166. !stmt.type) {
  24167. // check for a reexport
  24168. const { name, moduleName } = stmt.value.value;
  24169. if (moduleName) {
  24170. let reexports = this.reexports.get(moduleName);
  24171. if (!reexports) {
  24172. reexports = [];
  24173. this.reexports.set(moduleName, reexports);
  24174. }
  24175. reexports.push({ name: name, as: stmt.name });
  24176. return null;
  24177. }
  24178. }
  24179. if (stmt.hasModifier(StmtModifier.Exported)) {
  24180. ctx.print(stmt, `export `);
  24181. }
  24182. if (stmt.hasModifier(StmtModifier.Final)) {
  24183. ctx.print(stmt, `const`);
  24184. }
  24185. else {
  24186. ctx.print(stmt, `var`);
  24187. }
  24188. ctx.print(stmt, ` ${stmt.name}`);
  24189. this._printColonType(stmt.type, ctx);
  24190. if (stmt.value) {
  24191. ctx.print(stmt, ` = `);
  24192. stmt.value.visitExpression(this, ctx);
  24193. }
  24194. ctx.println(stmt, `;`);
  24195. return null;
  24196. }
  24197. visitWrappedNodeExpr(ast, ctx) {
  24198. throw new Error('Cannot visit a WrappedNodeExpr when outputting Typescript.');
  24199. }
  24200. visitCastExpr(ast, ctx) {
  24201. ctx.print(ast, `(<`);
  24202. ast.type.visitType(this, ctx);
  24203. ctx.print(ast, `>`);
  24204. ast.value.visitExpression(this, ctx);
  24205. ctx.print(ast, `)`);
  24206. return null;
  24207. }
  24208. visitInstantiateExpr(ast, ctx) {
  24209. ctx.print(ast, `new `);
  24210. this.typeExpression++;
  24211. ast.classExpr.visitExpression(this, ctx);
  24212. this.typeExpression--;
  24213. ctx.print(ast, `(`);
  24214. this.visitAllExpressions(ast.args, ctx, ',');
  24215. ctx.print(ast, `)`);
  24216. return null;
  24217. }
  24218. visitDeclareClassStmt(stmt, ctx) {
  24219. ctx.pushClass(stmt);
  24220. if (stmt.hasModifier(StmtModifier.Exported)) {
  24221. ctx.print(stmt, `export `);
  24222. }
  24223. ctx.print(stmt, `class ${stmt.name}`);
  24224. if (stmt.parent != null) {
  24225. ctx.print(stmt, ` extends `);
  24226. this.typeExpression++;
  24227. stmt.parent.visitExpression(this, ctx);
  24228. this.typeExpression--;
  24229. }
  24230. ctx.println(stmt, ` {`);
  24231. ctx.incIndent();
  24232. stmt.fields.forEach((field) => this._visitClassField(field, ctx));
  24233. if (stmt.constructorMethod != null) {
  24234. this._visitClassConstructor(stmt, ctx);
  24235. }
  24236. stmt.getters.forEach((getter) => this._visitClassGetter(getter, ctx));
  24237. stmt.methods.forEach((method) => this._visitClassMethod(method, ctx));
  24238. ctx.decIndent();
  24239. ctx.println(stmt, `}`);
  24240. ctx.popClass();
  24241. return null;
  24242. }
  24243. _visitClassField(field, ctx) {
  24244. if (field.hasModifier(StmtModifier.Private)) {
  24245. // comment out as a workaround for #10967
  24246. ctx.print(null, `/*private*/ `);
  24247. }
  24248. if (field.hasModifier(StmtModifier.Static)) {
  24249. ctx.print(null, 'static ');
  24250. }
  24251. ctx.print(null, field.name);
  24252. this._printColonType(field.type, ctx);
  24253. if (field.initializer) {
  24254. ctx.print(null, ' = ');
  24255. field.initializer.visitExpression(this, ctx);
  24256. }
  24257. ctx.println(null, `;`);
  24258. }
  24259. _visitClassGetter(getter, ctx) {
  24260. if (getter.hasModifier(StmtModifier.Private)) {
  24261. ctx.print(null, `private `);
  24262. }
  24263. ctx.print(null, `get ${getter.name}()`);
  24264. this._printColonType(getter.type, ctx);
  24265. ctx.println(null, ` {`);
  24266. ctx.incIndent();
  24267. this.visitAllStatements(getter.body, ctx);
  24268. ctx.decIndent();
  24269. ctx.println(null, `}`);
  24270. }
  24271. _visitClassConstructor(stmt, ctx) {
  24272. ctx.print(stmt, `constructor(`);
  24273. this._visitParams(stmt.constructorMethod.params, ctx);
  24274. ctx.println(stmt, `) {`);
  24275. ctx.incIndent();
  24276. this.visitAllStatements(stmt.constructorMethod.body, ctx);
  24277. ctx.decIndent();
  24278. ctx.println(stmt, `}`);
  24279. }
  24280. _visitClassMethod(method, ctx) {
  24281. if (method.hasModifier(StmtModifier.Private)) {
  24282. ctx.print(null, `private `);
  24283. }
  24284. ctx.print(null, `${method.name}(`);
  24285. this._visitParams(method.params, ctx);
  24286. ctx.print(null, `)`);
  24287. this._printColonType(method.type, ctx, 'void');
  24288. ctx.println(null, ` {`);
  24289. ctx.incIndent();
  24290. this.visitAllStatements(method.body, ctx);
  24291. ctx.decIndent();
  24292. ctx.println(null, `}`);
  24293. }
  24294. visitFunctionExpr(ast, ctx) {
  24295. if (ast.name) {
  24296. ctx.print(ast, 'function ');
  24297. ctx.print(ast, ast.name);
  24298. }
  24299. ctx.print(ast, `(`);
  24300. this._visitParams(ast.params, ctx);
  24301. ctx.print(ast, `)`);
  24302. this._printColonType(ast.type, ctx, 'void');
  24303. if (!ast.name) {
  24304. ctx.print(ast, ` => `);
  24305. }
  24306. ctx.println(ast, '{');
  24307. ctx.incIndent();
  24308. this.visitAllStatements(ast.statements, ctx);
  24309. ctx.decIndent();
  24310. ctx.print(ast, `}`);
  24311. return null;
  24312. }
  24313. visitDeclareFunctionStmt(stmt, ctx) {
  24314. if (stmt.hasModifier(StmtModifier.Exported)) {
  24315. ctx.print(stmt, `export `);
  24316. }
  24317. ctx.print(stmt, `function ${stmt.name}(`);
  24318. this._visitParams(stmt.params, ctx);
  24319. ctx.print(stmt, `)`);
  24320. this._printColonType(stmt.type, ctx, 'void');
  24321. ctx.println(stmt, ` {`);
  24322. ctx.incIndent();
  24323. this.visitAllStatements(stmt.statements, ctx);
  24324. ctx.decIndent();
  24325. ctx.println(stmt, `}`);
  24326. return null;
  24327. }
  24328. visitTryCatchStmt(stmt, ctx) {
  24329. ctx.println(stmt, `try {`);
  24330. ctx.incIndent();
  24331. this.visitAllStatements(stmt.bodyStmts, ctx);
  24332. ctx.decIndent();
  24333. ctx.println(stmt, `} catch (${CATCH_ERROR_VAR$1.name}) {`);
  24334. ctx.incIndent();
  24335. const catchStmts = [CATCH_STACK_VAR$1.set(CATCH_ERROR_VAR$1.prop('stack', null)).toDeclStmt(null, [
  24336. StmtModifier.Final
  24337. ])].concat(stmt.catchStmts);
  24338. this.visitAllStatements(catchStmts, ctx);
  24339. ctx.decIndent();
  24340. ctx.println(stmt, `}`);
  24341. return null;
  24342. }
  24343. visitBuiltinType(type, ctx) {
  24344. let typeStr;
  24345. switch (type.name) {
  24346. case BuiltinTypeName.Bool:
  24347. typeStr = 'boolean';
  24348. break;
  24349. case BuiltinTypeName.Dynamic:
  24350. typeStr = 'any';
  24351. break;
  24352. case BuiltinTypeName.Function:
  24353. typeStr = 'Function';
  24354. break;
  24355. case BuiltinTypeName.Number:
  24356. typeStr = 'number';
  24357. break;
  24358. case BuiltinTypeName.Int:
  24359. typeStr = 'number';
  24360. break;
  24361. case BuiltinTypeName.String:
  24362. typeStr = 'string';
  24363. break;
  24364. case BuiltinTypeName.None:
  24365. typeStr = 'never';
  24366. break;
  24367. default:
  24368. throw new Error(`Unsupported builtin type ${type.name}`);
  24369. }
  24370. ctx.print(null, typeStr);
  24371. return null;
  24372. }
  24373. visitExpressionType(ast, ctx) {
  24374. ast.value.visitExpression(this, ctx);
  24375. if (ast.typeParams !== null) {
  24376. ctx.print(null, '<');
  24377. this.visitAllObjects(type => this.visitType(type, ctx), ast.typeParams, ctx, ',');
  24378. ctx.print(null, '>');
  24379. }
  24380. return null;
  24381. }
  24382. visitArrayType(type, ctx) {
  24383. this.visitType(type.of, ctx);
  24384. ctx.print(null, `[]`);
  24385. return null;
  24386. }
  24387. visitMapType(type, ctx) {
  24388. ctx.print(null, `{[key: string]:`);
  24389. this.visitType(type.valueType, ctx);
  24390. ctx.print(null, `}`);
  24391. return null;
  24392. }
  24393. getBuiltinMethodName(method) {
  24394. let name;
  24395. switch (method) {
  24396. case BuiltinMethod.ConcatArray:
  24397. name = 'concat';
  24398. break;
  24399. case BuiltinMethod.SubscribeObservable:
  24400. name = 'subscribe';
  24401. break;
  24402. case BuiltinMethod.Bind:
  24403. name = 'bind';
  24404. break;
  24405. default:
  24406. throw new Error(`Unknown builtin method: ${method}`);
  24407. }
  24408. return name;
  24409. }
  24410. _visitParams(params, ctx) {
  24411. this.visitAllObjects(param => {
  24412. ctx.print(null, param.name);
  24413. this._printColonType(param.type, ctx);
  24414. }, params, ctx, ',');
  24415. }
  24416. _visitIdentifier(value, typeParams, ctx) {
  24417. const { name, moduleName } = value;
  24418. if (this.referenceFilter && this.referenceFilter(value)) {
  24419. ctx.print(null, '(null as any)');
  24420. return;
  24421. }
  24422. if (moduleName && (!this.importFilter || !this.importFilter(value))) {
  24423. let prefix = this.importsWithPrefixes.get(moduleName);
  24424. if (prefix == null) {
  24425. prefix = `i${this.importsWithPrefixes.size}`;
  24426. this.importsWithPrefixes.set(moduleName, prefix);
  24427. }
  24428. ctx.print(null, `${prefix}.`);
  24429. }
  24430. ctx.print(null, name);
  24431. if (this.typeExpression > 0) {
  24432. // If we are in a type expression that refers to a generic type then supply
  24433. // the required type parameters. If there were not enough type parameters
  24434. // supplied, supply any as the type. Outside a type expression the reference
  24435. // should not supply type parameters and be treated as a simple value reference
  24436. // to the constructor function itself.
  24437. const suppliedParameters = typeParams || [];
  24438. if (suppliedParameters.length > 0) {
  24439. ctx.print(null, `<`);
  24440. this.visitAllObjects(type => type.visitType(this, ctx), typeParams, ctx, ',');
  24441. ctx.print(null, `>`);
  24442. }
  24443. }
  24444. }
  24445. _printColonType(type, ctx, defaultType) {
  24446. if (type !== INFERRED_TYPE) {
  24447. ctx.print(null, ':');
  24448. this.visitType(type, ctx, defaultType);
  24449. }
  24450. }
  24451. }
  24452. /**
  24453. * @license
  24454. * Copyright Google LLC All Rights Reserved.
  24455. *
  24456. * Use of this source code is governed by an MIT-style license that can be
  24457. * found in the LICENSE file at https://angular.io/license
  24458. */
  24459. /**
  24460. * Resolve a `Type` for {@link Pipe}.
  24461. *
  24462. * This interface can be overridden by the application developer to create custom behavior.
  24463. *
  24464. * See {@link Compiler}
  24465. */
  24466. class PipeResolver {
  24467. constructor(_reflector) {
  24468. this._reflector = _reflector;
  24469. }
  24470. isPipe(type) {
  24471. const typeMetadata = this._reflector.annotations(resolveForwardRef(type));
  24472. return typeMetadata && typeMetadata.some(createPipe.isTypeOf);
  24473. }
  24474. /**
  24475. * Return {@link Pipe} for a given `Type`.
  24476. */
  24477. resolve(type, throwIfNotFound = true) {
  24478. const metas = this._reflector.annotations(resolveForwardRef(type));
  24479. if (metas) {
  24480. const annotation = findLast(metas, createPipe.isTypeOf);
  24481. if (annotation) {
  24482. return annotation;
  24483. }
  24484. }
  24485. if (throwIfNotFound) {
  24486. throw new Error(`No Pipe decorator found on ${stringify(type)}`);
  24487. }
  24488. return null;
  24489. }
  24490. }
  24491. /**
  24492. * @license
  24493. * Copyright Google LLC All Rights Reserved.
  24494. *
  24495. * Use of this source code is governed by an MIT-style license that can be
  24496. * found in the LICENSE file at https://angular.io/license
  24497. */
  24498. /**
  24499. * Generates code that is used to type check templates.
  24500. */
  24501. class TypeCheckCompiler {
  24502. constructor(options, reflector) {
  24503. this.options = options;
  24504. this.reflector = reflector;
  24505. }
  24506. /**
  24507. * Important notes:
  24508. * - This must not produce new `import` statements, but only refer to types outside
  24509. * of the file via the variables provided via externalReferenceVars.
  24510. * This allows Typescript to reuse the old program's structure as no imports have changed.
  24511. * - This must not produce any exports, as this would pollute the .d.ts file
  24512. * and also violate the point above.
  24513. */
  24514. compileComponent(componentId, component, template, usedPipes, externalReferenceVars, ctx) {
  24515. const pipes = new Map();
  24516. usedPipes.forEach(p => pipes.set(p.name, p.type.reference));
  24517. let embeddedViewCount = 0;
  24518. const viewBuilderFactory = (parent, guards) => {
  24519. const embeddedViewIndex = embeddedViewCount++;
  24520. return new ViewBuilder(this.options, this.reflector, externalReferenceVars, parent, component.type.reference, component.isHost, embeddedViewIndex, pipes, guards, ctx, viewBuilderFactory);
  24521. };
  24522. const visitor = viewBuilderFactory(null, []);
  24523. visitor.visitAll([], template);
  24524. return visitor.build(componentId);
  24525. }
  24526. }
  24527. const DYNAMIC_VAR_NAME = '_any';
  24528. class TypeCheckLocalResolver {
  24529. notifyImplicitReceiverUse() { }
  24530. getLocal(name) {
  24531. if (name === EventHandlerVars.event.name) {
  24532. // References to the event should not be type-checked.
  24533. // TODO(chuckj): determine a better type for the event.
  24534. return variable(DYNAMIC_VAR_NAME);
  24535. }
  24536. return null;
  24537. }
  24538. }
  24539. const defaultResolver = new TypeCheckLocalResolver();
  24540. class ViewBuilder {
  24541. constructor(options, reflector, externalReferenceVars, parent, component, isHostComponent, embeddedViewIndex, pipes, guards, ctx, viewBuilderFactory) {
  24542. this.options = options;
  24543. this.reflector = reflector;
  24544. this.externalReferenceVars = externalReferenceVars;
  24545. this.parent = parent;
  24546. this.component = component;
  24547. this.isHostComponent = isHostComponent;
  24548. this.embeddedViewIndex = embeddedViewIndex;
  24549. this.pipes = pipes;
  24550. this.guards = guards;
  24551. this.ctx = ctx;
  24552. this.viewBuilderFactory = viewBuilderFactory;
  24553. this.refOutputVars = new Map();
  24554. this.variables = [];
  24555. this.children = [];
  24556. this.updates = [];
  24557. this.actions = [];
  24558. }
  24559. getOutputVar(type) {
  24560. let varName;
  24561. if (type === this.component && this.isHostComponent) {
  24562. varName = DYNAMIC_VAR_NAME;
  24563. }
  24564. else if (type instanceof StaticSymbol) {
  24565. varName = this.externalReferenceVars.get(type);
  24566. }
  24567. else {
  24568. varName = DYNAMIC_VAR_NAME;
  24569. }
  24570. if (!varName) {
  24571. throw new Error(`Illegal State: referring to a type without a variable ${JSON.stringify(type)}`);
  24572. }
  24573. return varName;
  24574. }
  24575. getTypeGuardExpressions(ast) {
  24576. const result = [...this.guards];
  24577. for (let directive of ast.directives) {
  24578. for (let input of directive.inputs) {
  24579. const guard = directive.directive.guards[input.directiveName];
  24580. if (guard) {
  24581. const useIf = guard === 'UseIf';
  24582. result.push({
  24583. guard,
  24584. useIf,
  24585. expression: {
  24586. context: this.component,
  24587. value: input.value,
  24588. sourceSpan: input.sourceSpan,
  24589. },
  24590. });
  24591. }
  24592. }
  24593. }
  24594. return result;
  24595. }
  24596. visitAll(variables, astNodes) {
  24597. this.variables = variables;
  24598. templateVisitAll(this, astNodes);
  24599. }
  24600. build(componentId, targetStatements = []) {
  24601. this.children.forEach((child) => child.build(componentId, targetStatements));
  24602. let viewStmts = [variable(DYNAMIC_VAR_NAME).set(NULL_EXPR).toDeclStmt(DYNAMIC_TYPE)];
  24603. let bindingCount = 0;
  24604. this.updates.forEach((expression) => {
  24605. const { sourceSpan, context, value } = this.preprocessUpdateExpression(expression);
  24606. const bindingId = `${bindingCount++}`;
  24607. const nameResolver = context === this.component ? this : defaultResolver;
  24608. const { stmts, currValExpr } = convertPropertyBinding(nameResolver, variable(this.getOutputVar(context)), value, bindingId, BindingForm.General);
  24609. stmts.push(new ExpressionStatement(currValExpr));
  24610. viewStmts.push(...stmts.map((stmt) => applySourceSpanToStatementIfNeeded(stmt, sourceSpan)));
  24611. });
  24612. this.actions.forEach(({ sourceSpan, context, value }) => {
  24613. const bindingId = `${bindingCount++}`;
  24614. const nameResolver = context === this.component ? this : defaultResolver;
  24615. const { stmts } = convertActionBinding(nameResolver, variable(this.getOutputVar(context)), value, bindingId);
  24616. viewStmts.push(...stmts.map((stmt) => applySourceSpanToStatementIfNeeded(stmt, sourceSpan)));
  24617. });
  24618. if (this.guards.length) {
  24619. let guardExpression = undefined;
  24620. for (const guard of this.guards) {
  24621. const { context, value } = this.preprocessUpdateExpression(guard.expression);
  24622. const bindingId = `${bindingCount++}`;
  24623. const nameResolver = context === this.component ? this : defaultResolver;
  24624. // We only support support simple expressions and ignore others as they
  24625. // are unlikely to affect type narrowing.
  24626. const { stmts, currValExpr } = convertPropertyBinding(nameResolver, variable(this.getOutputVar(context)), value, bindingId, BindingForm.TrySimple);
  24627. if (stmts.length == 0) {
  24628. const guardClause = guard.useIf ? currValExpr : this.ctx.importExpr(guard.guard).callFn([currValExpr]);
  24629. guardExpression = guardExpression ? guardExpression.and(guardClause) : guardClause;
  24630. }
  24631. }
  24632. if (guardExpression) {
  24633. viewStmts = [new IfStmt(guardExpression, viewStmts)];
  24634. }
  24635. }
  24636. const viewName = `_View_${componentId}_${this.embeddedViewIndex}`;
  24637. const viewFactory = new DeclareFunctionStmt(viewName, [], viewStmts);
  24638. targetStatements.push(viewFactory);
  24639. return targetStatements;
  24640. }
  24641. visitBoundText(ast, context) {
  24642. const astWithSource = ast.value;
  24643. const inter = astWithSource.ast;
  24644. inter.expressions.forEach((expr) => this.updates.push({ context: this.component, value: expr, sourceSpan: ast.sourceSpan }));
  24645. }
  24646. visitEmbeddedTemplate(ast, context) {
  24647. this.visitElementOrTemplate(ast);
  24648. // Note: The old view compiler used to use an `any` type
  24649. // for the context in any embedded view.
  24650. // We keep this behaivor behind a flag for now.
  24651. if (this.options.fullTemplateTypeCheck) {
  24652. // Find any applicable type guards. For example, NgIf has a type guard on ngIf
  24653. // (see NgIf.ngIfTypeGuard) that can be used to indicate that a template is only
  24654. // stamped out if ngIf is truthy so any bindings in the template can assume that,
  24655. // if a nullable type is used for ngIf, that expression is not null or undefined.
  24656. const guards = this.getTypeGuardExpressions(ast);
  24657. const childVisitor = this.viewBuilderFactory(this, guards);
  24658. this.children.push(childVisitor);
  24659. childVisitor.visitAll(ast.variables, ast.children);
  24660. }
  24661. }
  24662. visitElement(ast, context) {
  24663. this.visitElementOrTemplate(ast);
  24664. let inputDefs = [];
  24665. let updateRendererExpressions = [];
  24666. let outputDefs = [];
  24667. ast.inputs.forEach((inputAst) => {
  24668. this.updates.push({ context: this.component, value: inputAst.value, sourceSpan: inputAst.sourceSpan });
  24669. });
  24670. templateVisitAll(this, ast.children);
  24671. }
  24672. visitElementOrTemplate(ast) {
  24673. ast.directives.forEach((dirAst) => {
  24674. this.visitDirective(dirAst);
  24675. });
  24676. ast.references.forEach((ref) => {
  24677. let outputVarType = null;
  24678. // Note: The old view compiler used to use an `any` type
  24679. // for directives exposed via `exportAs`.
  24680. // We keep this behaivor behind a flag for now.
  24681. if (ref.value && ref.value.identifier && this.options.fullTemplateTypeCheck) {
  24682. outputVarType = ref.value.identifier.reference;
  24683. }
  24684. else {
  24685. outputVarType = BuiltinTypeName.Dynamic;
  24686. }
  24687. this.refOutputVars.set(ref.name, outputVarType);
  24688. });
  24689. ast.outputs.forEach((outputAst) => {
  24690. this.actions.push({ context: this.component, value: outputAst.handler, sourceSpan: outputAst.sourceSpan });
  24691. });
  24692. }
  24693. visitDirective(dirAst) {
  24694. const dirType = dirAst.directive.type.reference;
  24695. dirAst.inputs.forEach((input) => this.updates.push({ context: this.component, value: input.value, sourceSpan: input.sourceSpan }));
  24696. // Note: The old view compiler used to use an `any` type
  24697. // for expressions in host properties / events.
  24698. // We keep this behaivor behind a flag for now.
  24699. if (this.options.fullTemplateTypeCheck) {
  24700. dirAst.hostProperties.forEach((inputAst) => this.updates.push({ context: dirType, value: inputAst.value, sourceSpan: inputAst.sourceSpan }));
  24701. dirAst.hostEvents.forEach((hostEventAst) => this.actions.push({
  24702. context: dirType,
  24703. value: hostEventAst.handler,
  24704. sourceSpan: hostEventAst.sourceSpan
  24705. }));
  24706. }
  24707. }
  24708. notifyImplicitReceiverUse() { }
  24709. getLocal(name) {
  24710. if (name == EventHandlerVars.event.name) {
  24711. return variable(this.getOutputVar(BuiltinTypeName.Dynamic));
  24712. }
  24713. for (let currBuilder = this; currBuilder; currBuilder = currBuilder.parent) {
  24714. let outputVarType;
  24715. // check references
  24716. outputVarType = currBuilder.refOutputVars.get(name);
  24717. if (outputVarType == null) {
  24718. // check variables
  24719. const varAst = currBuilder.variables.find((varAst) => varAst.name === name);
  24720. if (varAst) {
  24721. outputVarType = BuiltinTypeName.Dynamic;
  24722. }
  24723. }
  24724. if (outputVarType != null) {
  24725. return variable(this.getOutputVar(outputVarType));
  24726. }
  24727. }
  24728. return null;
  24729. }
  24730. pipeOutputVar(name) {
  24731. const pipe = this.pipes.get(name);
  24732. if (!pipe) {
  24733. throw new Error(`Illegal State: Could not find pipe ${name} in template of ${this.component}`);
  24734. }
  24735. return this.getOutputVar(pipe);
  24736. }
  24737. preprocessUpdateExpression(expression) {
  24738. return {
  24739. sourceSpan: expression.sourceSpan,
  24740. context: expression.context,
  24741. value: convertPropertyBindingBuiltins({
  24742. createLiteralArrayConverter: (argCount) => (args) => {
  24743. const arr = literalArr(args);
  24744. // Note: The old view compiler used to use an `any` type
  24745. // for arrays.
  24746. return this.options.fullTemplateTypeCheck ? arr : arr.cast(DYNAMIC_TYPE);
  24747. },
  24748. createLiteralMapConverter: (keys) => (values) => {
  24749. const entries = keys.map((k, i) => ({
  24750. key: k.key,
  24751. value: values[i],
  24752. quoted: k.quoted,
  24753. }));
  24754. const map = literalMap(entries);
  24755. // Note: The old view compiler used to use an `any` type
  24756. // for maps.
  24757. return this.options.fullTemplateTypeCheck ? map : map.cast(DYNAMIC_TYPE);
  24758. },
  24759. createPipeConverter: (name, argCount) => (args) => {
  24760. // Note: The old view compiler used to use an `any` type
  24761. // for pipes.
  24762. const pipeExpr = this.options.fullTemplateTypeCheck ?
  24763. variable(this.pipeOutputVar(name)) :
  24764. variable(this.getOutputVar(BuiltinTypeName.Dynamic));
  24765. return pipeExpr.callMethod('transform', args);
  24766. },
  24767. }, expression.value)
  24768. };
  24769. }
  24770. visitNgContent(ast, context) { }
  24771. visitText(ast, context) { }
  24772. visitDirectiveProperty(ast, context) { }
  24773. visitReference(ast, context) { }
  24774. visitVariable(ast, context) { }
  24775. visitEvent(ast, context) { }
  24776. visitElementProperty(ast, context) { }
  24777. visitAttr(ast, context) { }
  24778. }
  24779. /**
  24780. * @license
  24781. * Copyright Google LLC All Rights Reserved.
  24782. *
  24783. * Use of this source code is governed by an MIT-style license that can be
  24784. * found in the LICENSE file at https://angular.io/license
  24785. */
  24786. const CLASS_ATTR$1 = 'class';
  24787. const STYLE_ATTR = 'style';
  24788. const IMPLICIT_TEMPLATE_VAR = '\$implicit';
  24789. class ViewCompileResult {
  24790. constructor(viewClassVar, rendererTypeVar) {
  24791. this.viewClassVar = viewClassVar;
  24792. this.rendererTypeVar = rendererTypeVar;
  24793. }
  24794. }
  24795. class ViewCompiler {
  24796. constructor(_reflector) {
  24797. this._reflector = _reflector;
  24798. }
  24799. compileComponent(outputCtx, component, template, styles, usedPipes) {
  24800. let embeddedViewCount = 0;
  24801. let renderComponentVarName = undefined;
  24802. if (!component.isHost) {
  24803. const template = component.template;
  24804. const customRenderData = [];
  24805. if (template.animations && template.animations.length) {
  24806. customRenderData.push(new LiteralMapEntry('animation', convertValueToOutputAst(outputCtx, template.animations), true));
  24807. }
  24808. const renderComponentVar = variable(rendererTypeName(component.type.reference));
  24809. renderComponentVarName = renderComponentVar.name;
  24810. outputCtx.statements.push(renderComponentVar
  24811. .set(importExpr(Identifiers$1.createRendererType2).callFn([new LiteralMapExpr([
  24812. new LiteralMapEntry('encapsulation', literal(template.encapsulation), false),
  24813. new LiteralMapEntry('styles', styles, false),
  24814. new LiteralMapEntry('data', new LiteralMapExpr(customRenderData), false)
  24815. ])]))
  24816. .toDeclStmt(importType(Identifiers$1.RendererType2), [StmtModifier.Final, StmtModifier.Exported]));
  24817. }
  24818. const viewBuilderFactory = (parent) => {
  24819. const embeddedViewIndex = embeddedViewCount++;
  24820. return new ViewBuilder$1(this._reflector, outputCtx, parent, component, embeddedViewIndex, usedPipes, viewBuilderFactory);
  24821. };
  24822. const visitor = viewBuilderFactory(null);
  24823. visitor.visitAll([], template);
  24824. outputCtx.statements.push(...visitor.build());
  24825. return new ViewCompileResult(visitor.viewName, renderComponentVarName);
  24826. }
  24827. }
  24828. const LOG_VAR$1 = variable('_l');
  24829. const VIEW_VAR = variable('_v');
  24830. const CHECK_VAR = variable('_ck');
  24831. const COMP_VAR = variable('_co');
  24832. const EVENT_NAME_VAR = variable('en');
  24833. const ALLOW_DEFAULT_VAR = variable(`ad`);
  24834. class ViewBuilder$1 {
  24835. constructor(reflector, outputCtx, parent, component, embeddedViewIndex, usedPipes, viewBuilderFactory) {
  24836. this.reflector = reflector;
  24837. this.outputCtx = outputCtx;
  24838. this.parent = parent;
  24839. this.component = component;
  24840. this.embeddedViewIndex = embeddedViewIndex;
  24841. this.usedPipes = usedPipes;
  24842. this.viewBuilderFactory = viewBuilderFactory;
  24843. this.nodes = [];
  24844. this.purePipeNodeIndices = Object.create(null);
  24845. // Need Object.create so that we don't have builtin values...
  24846. this.refNodeIndices = Object.create(null);
  24847. this.variables = [];
  24848. this.children = [];
  24849. // TODO(tbosch): The old view compiler used to use an `any` type
  24850. // for the context in any embedded view. We keep this behaivor for now
  24851. // to be able to introduce the new view compiler without too many errors.
  24852. this.compType = this.embeddedViewIndex > 0 ?
  24853. DYNAMIC_TYPE :
  24854. expressionType(outputCtx.importExpr(this.component.type.reference));
  24855. this.viewName = viewClassName(this.component.type.reference, this.embeddedViewIndex);
  24856. }
  24857. visitAll(variables, astNodes) {
  24858. this.variables = variables;
  24859. // create the pipes for the pure pipes immediately, so that we know their indices.
  24860. if (!this.parent) {
  24861. this.usedPipes.forEach((pipe) => {
  24862. if (pipe.pure) {
  24863. this.purePipeNodeIndices[pipe.name] = this._createPipe(null, pipe);
  24864. }
  24865. });
  24866. }
  24867. if (!this.parent) {
  24868. this.component.viewQueries.forEach((query, queryIndex) => {
  24869. // Note: queries start with id 1 so we can use the number in a Bloom filter!
  24870. const queryId = queryIndex + 1;
  24871. const bindingType = query.first ? 0 /* First */ : 1 /* All */;
  24872. const flags = 134217728 /* TypeViewQuery */ | calcQueryFlags(query);
  24873. this.nodes.push(() => ({
  24874. sourceSpan: null,
  24875. nodeFlags: flags,
  24876. nodeDef: importExpr(Identifiers$1.queryDef).callFn([
  24877. literal(flags), literal(queryId),
  24878. new LiteralMapExpr([new LiteralMapEntry(query.propertyName, literal(bindingType), false)])
  24879. ])
  24880. }));
  24881. });
  24882. }
  24883. templateVisitAll(this, astNodes);
  24884. if (this.parent && (astNodes.length === 0 || needsAdditionalRootNode(astNodes))) {
  24885. // if the view is an embedded view, then we need to add an additional root node in some cases
  24886. this.nodes.push(() => ({
  24887. sourceSpan: null,
  24888. nodeFlags: 1 /* TypeElement */,
  24889. nodeDef: importExpr(Identifiers$1.anchorDef).callFn([
  24890. literal(0 /* None */), NULL_EXPR, NULL_EXPR, literal(0)
  24891. ])
  24892. }));
  24893. }
  24894. }
  24895. build(targetStatements = []) {
  24896. this.children.forEach((child) => child.build(targetStatements));
  24897. const { updateRendererStmts, updateDirectivesStmts, nodeDefExprs } = this._createNodeExpressions();
  24898. const updateRendererFn = this._createUpdateFn(updateRendererStmts);
  24899. const updateDirectivesFn = this._createUpdateFn(updateDirectivesStmts);
  24900. let viewFlags = 0 /* None */;
  24901. if (!this.parent && this.component.changeDetection === ChangeDetectionStrategy.OnPush) {
  24902. viewFlags |= 2 /* OnPush */;
  24903. }
  24904. const viewFactory = new DeclareFunctionStmt(this.viewName, [new FnParam(LOG_VAR$1.name)], [new ReturnStatement(importExpr(Identifiers$1.viewDef).callFn([
  24905. literal(viewFlags),
  24906. literalArr(nodeDefExprs),
  24907. updateDirectivesFn,
  24908. updateRendererFn,
  24909. ]))], importType(Identifiers$1.ViewDefinition), this.embeddedViewIndex === 0 ? [StmtModifier.Exported] : []);
  24910. targetStatements.push(viewFactory);
  24911. return targetStatements;
  24912. }
  24913. _createUpdateFn(updateStmts) {
  24914. let updateFn;
  24915. if (updateStmts.length > 0) {
  24916. const preStmts = [];
  24917. if (!this.component.isHost && findReadVarNames(updateStmts).has(COMP_VAR.name)) {
  24918. preStmts.push(COMP_VAR.set(VIEW_VAR.prop('component')).toDeclStmt(this.compType));
  24919. }
  24920. updateFn = fn([
  24921. new FnParam(CHECK_VAR.name, INFERRED_TYPE),
  24922. new FnParam(VIEW_VAR.name, INFERRED_TYPE)
  24923. ], [...preStmts, ...updateStmts], INFERRED_TYPE);
  24924. }
  24925. else {
  24926. updateFn = NULL_EXPR;
  24927. }
  24928. return updateFn;
  24929. }
  24930. visitNgContent(ast, context) {
  24931. // ngContentDef(ngContentIndex: number, index: number): NodeDef;
  24932. this.nodes.push(() => ({
  24933. sourceSpan: ast.sourceSpan,
  24934. nodeFlags: 8 /* TypeNgContent */,
  24935. nodeDef: importExpr(Identifiers$1.ngContentDef)
  24936. .callFn([literal(ast.ngContentIndex), literal(ast.index)])
  24937. }));
  24938. }
  24939. visitText(ast, context) {
  24940. // Static text nodes have no check function
  24941. const checkIndex = -1;
  24942. this.nodes.push(() => ({
  24943. sourceSpan: ast.sourceSpan,
  24944. nodeFlags: 2 /* TypeText */,
  24945. nodeDef: importExpr(Identifiers$1.textDef).callFn([
  24946. literal(checkIndex),
  24947. literal(ast.ngContentIndex),
  24948. literalArr([literal(ast.value)]),
  24949. ])
  24950. }));
  24951. }
  24952. visitBoundText(ast, context) {
  24953. const nodeIndex = this.nodes.length;
  24954. // reserve the space in the nodeDefs array
  24955. this.nodes.push(null);
  24956. const astWithSource = ast.value;
  24957. const inter = astWithSource.ast;
  24958. const updateRendererExpressions = inter.expressions.map((expr, bindingIndex) => this._preprocessUpdateExpression({ nodeIndex, bindingIndex, sourceSpan: ast.sourceSpan, context: COMP_VAR, value: expr }));
  24959. // Check index is the same as the node index during compilation
  24960. // They might only differ at runtime
  24961. const checkIndex = nodeIndex;
  24962. this.nodes[nodeIndex] = () => ({
  24963. sourceSpan: ast.sourceSpan,
  24964. nodeFlags: 2 /* TypeText */,
  24965. nodeDef: importExpr(Identifiers$1.textDef).callFn([
  24966. literal(checkIndex),
  24967. literal(ast.ngContentIndex),
  24968. literalArr(inter.strings.map(s => literal(s))),
  24969. ]),
  24970. updateRenderer: updateRendererExpressions
  24971. });
  24972. }
  24973. visitEmbeddedTemplate(ast, context) {
  24974. const nodeIndex = this.nodes.length;
  24975. // reserve the space in the nodeDefs array
  24976. this.nodes.push(null);
  24977. const { flags, queryMatchesExpr, hostEvents } = this._visitElementOrTemplate(nodeIndex, ast);
  24978. const childVisitor = this.viewBuilderFactory(this);
  24979. this.children.push(childVisitor);
  24980. childVisitor.visitAll(ast.variables, ast.children);
  24981. const childCount = this.nodes.length - nodeIndex - 1;
  24982. // anchorDef(
  24983. // flags: NodeFlags, matchedQueries: [string, QueryValueType][], ngContentIndex: number,
  24984. // childCount: number, handleEventFn?: ElementHandleEventFn, templateFactory?:
  24985. // ViewDefinitionFactory): NodeDef;
  24986. this.nodes[nodeIndex] = () => ({
  24987. sourceSpan: ast.sourceSpan,
  24988. nodeFlags: 1 /* TypeElement */ | flags,
  24989. nodeDef: importExpr(Identifiers$1.anchorDef).callFn([
  24990. literal(flags),
  24991. queryMatchesExpr,
  24992. literal(ast.ngContentIndex),
  24993. literal(childCount),
  24994. this._createElementHandleEventFn(nodeIndex, hostEvents),
  24995. variable(childVisitor.viewName),
  24996. ])
  24997. });
  24998. }
  24999. visitElement(ast, context) {
  25000. const nodeIndex = this.nodes.length;
  25001. // reserve the space in the nodeDefs array so we can add children
  25002. this.nodes.push(null);
  25003. // Using a null element name creates an anchor.
  25004. const elName = isNgContainer(ast.name) ? null : ast.name;
  25005. const { flags, usedEvents, queryMatchesExpr, hostBindings: dirHostBindings, hostEvents } = this._visitElementOrTemplate(nodeIndex, ast);
  25006. let inputDefs = [];
  25007. let updateRendererExpressions = [];
  25008. let outputDefs = [];
  25009. if (elName) {
  25010. const hostBindings = ast.inputs
  25011. .map((inputAst) => ({
  25012. context: COMP_VAR,
  25013. inputAst,
  25014. dirAst: null,
  25015. }))
  25016. .concat(dirHostBindings);
  25017. if (hostBindings.length) {
  25018. updateRendererExpressions =
  25019. hostBindings.map((hostBinding, bindingIndex) => this._preprocessUpdateExpression({
  25020. context: hostBinding.context,
  25021. nodeIndex,
  25022. bindingIndex,
  25023. sourceSpan: hostBinding.inputAst.sourceSpan,
  25024. value: hostBinding.inputAst.value
  25025. }));
  25026. inputDefs = hostBindings.map(hostBinding => elementBindingDef(hostBinding.inputAst, hostBinding.dirAst));
  25027. }
  25028. outputDefs = usedEvents.map(([target, eventName]) => literalArr([literal(target), literal(eventName)]));
  25029. }
  25030. templateVisitAll(this, ast.children);
  25031. const childCount = this.nodes.length - nodeIndex - 1;
  25032. const compAst = ast.directives.find(dirAst => dirAst.directive.isComponent);
  25033. let compRendererType = NULL_EXPR;
  25034. let compView = NULL_EXPR;
  25035. if (compAst) {
  25036. compView = this.outputCtx.importExpr(compAst.directive.componentViewType);
  25037. compRendererType = this.outputCtx.importExpr(compAst.directive.rendererType);
  25038. }
  25039. // Check index is the same as the node index during compilation
  25040. // They might only differ at runtime
  25041. const checkIndex = nodeIndex;
  25042. this.nodes[nodeIndex] = () => ({
  25043. sourceSpan: ast.sourceSpan,
  25044. nodeFlags: 1 /* TypeElement */ | flags,
  25045. nodeDef: importExpr(Identifiers$1.elementDef).callFn([
  25046. literal(checkIndex),
  25047. literal(flags),
  25048. queryMatchesExpr,
  25049. literal(ast.ngContentIndex),
  25050. literal(childCount),
  25051. literal(elName),
  25052. elName ? fixedAttrsDef(ast) : NULL_EXPR,
  25053. inputDefs.length ? literalArr(inputDefs) : NULL_EXPR,
  25054. outputDefs.length ? literalArr(outputDefs) : NULL_EXPR,
  25055. this._createElementHandleEventFn(nodeIndex, hostEvents),
  25056. compView,
  25057. compRendererType,
  25058. ]),
  25059. updateRenderer: updateRendererExpressions
  25060. });
  25061. }
  25062. _visitElementOrTemplate(nodeIndex, ast) {
  25063. let flags = 0 /* None */;
  25064. if (ast.hasViewContainer) {
  25065. flags |= 16777216 /* EmbeddedViews */;
  25066. }
  25067. const usedEvents = new Map();
  25068. ast.outputs.forEach((event) => {
  25069. const { name, target } = elementEventNameAndTarget(event, null);
  25070. usedEvents.set(elementEventFullName(target, name), [target, name]);
  25071. });
  25072. ast.directives.forEach((dirAst) => {
  25073. dirAst.hostEvents.forEach((event) => {
  25074. const { name, target } = elementEventNameAndTarget(event, dirAst);
  25075. usedEvents.set(elementEventFullName(target, name), [target, name]);
  25076. });
  25077. });
  25078. const hostBindings = [];
  25079. const hostEvents = [];
  25080. this._visitComponentFactoryResolverProvider(ast.directives);
  25081. ast.providers.forEach(providerAst => {
  25082. let dirAst = undefined;
  25083. ast.directives.forEach(localDirAst => {
  25084. if (localDirAst.directive.type.reference === tokenReference(providerAst.token)) {
  25085. dirAst = localDirAst;
  25086. }
  25087. });
  25088. if (dirAst) {
  25089. const { hostBindings: dirHostBindings, hostEvents: dirHostEvents } = this._visitDirective(providerAst, dirAst, ast.references, ast.queryMatches, usedEvents);
  25090. hostBindings.push(...dirHostBindings);
  25091. hostEvents.push(...dirHostEvents);
  25092. }
  25093. else {
  25094. this._visitProvider(providerAst, ast.queryMatches);
  25095. }
  25096. });
  25097. let queryMatchExprs = [];
  25098. ast.queryMatches.forEach((match) => {
  25099. let valueType = undefined;
  25100. if (tokenReference(match.value) ===
  25101. this.reflector.resolveExternalReference(Identifiers$1.ElementRef)) {
  25102. valueType = 0 /* ElementRef */;
  25103. }
  25104. else if (tokenReference(match.value) ===
  25105. this.reflector.resolveExternalReference(Identifiers$1.ViewContainerRef)) {
  25106. valueType = 3 /* ViewContainerRef */;
  25107. }
  25108. else if (tokenReference(match.value) ===
  25109. this.reflector.resolveExternalReference(Identifiers$1.TemplateRef)) {
  25110. valueType = 2 /* TemplateRef */;
  25111. }
  25112. if (valueType != null) {
  25113. queryMatchExprs.push(literalArr([literal(match.queryId), literal(valueType)]));
  25114. }
  25115. });
  25116. ast.references.forEach((ref) => {
  25117. let valueType = undefined;
  25118. if (!ref.value) {
  25119. valueType = 1 /* RenderElement */;
  25120. }
  25121. else if (tokenReference(ref.value) ===
  25122. this.reflector.resolveExternalReference(Identifiers$1.TemplateRef)) {
  25123. valueType = 2 /* TemplateRef */;
  25124. }
  25125. if (valueType != null) {
  25126. this.refNodeIndices[ref.name] = nodeIndex;
  25127. queryMatchExprs.push(literalArr([literal(ref.name), literal(valueType)]));
  25128. }
  25129. });
  25130. ast.outputs.forEach((outputAst) => {
  25131. hostEvents.push({ context: COMP_VAR, eventAst: outputAst, dirAst: null });
  25132. });
  25133. return {
  25134. flags,
  25135. usedEvents: Array.from(usedEvents.values()),
  25136. queryMatchesExpr: queryMatchExprs.length ? literalArr(queryMatchExprs) : NULL_EXPR,
  25137. hostBindings,
  25138. hostEvents: hostEvents
  25139. };
  25140. }
  25141. _visitDirective(providerAst, dirAst, refs, queryMatches, usedEvents) {
  25142. const nodeIndex = this.nodes.length;
  25143. // reserve the space in the nodeDefs array so we can add children
  25144. this.nodes.push(null);
  25145. dirAst.directive.queries.forEach((query, queryIndex) => {
  25146. const queryId = dirAst.contentQueryStartId + queryIndex;
  25147. const flags = 67108864 /* TypeContentQuery */ | calcQueryFlags(query);
  25148. const bindingType = query.first ? 0 /* First */ : 1 /* All */;
  25149. this.nodes.push(() => ({
  25150. sourceSpan: dirAst.sourceSpan,
  25151. nodeFlags: flags,
  25152. nodeDef: importExpr(Identifiers$1.queryDef).callFn([
  25153. literal(flags), literal(queryId),
  25154. new LiteralMapExpr([new LiteralMapEntry(query.propertyName, literal(bindingType), false)])
  25155. ]),
  25156. }));
  25157. });
  25158. // Note: the operation below might also create new nodeDefs,
  25159. // but we don't want them to be a child of a directive,
  25160. // as they might be a provider/pipe on their own.
  25161. // I.e. we only allow queries as children of directives nodes.
  25162. const childCount = this.nodes.length - nodeIndex - 1;
  25163. let { flags, queryMatchExprs, providerExpr, depsExpr } = this._visitProviderOrDirective(providerAst, queryMatches);
  25164. refs.forEach((ref) => {
  25165. if (ref.value && tokenReference(ref.value) === tokenReference(providerAst.token)) {
  25166. this.refNodeIndices[ref.name] = nodeIndex;
  25167. queryMatchExprs.push(literalArr([literal(ref.name), literal(4 /* Provider */)]));
  25168. }
  25169. });
  25170. if (dirAst.directive.isComponent) {
  25171. flags |= 32768 /* Component */;
  25172. }
  25173. const inputDefs = dirAst.inputs.map((inputAst, inputIndex) => {
  25174. const mapValue = literalArr([literal(inputIndex), literal(inputAst.directiveName)]);
  25175. // Note: it's important to not quote the key so that we can capture renames by minifiers!
  25176. return new LiteralMapEntry(inputAst.directiveName, mapValue, false);
  25177. });
  25178. const outputDefs = [];
  25179. const dirMeta = dirAst.directive;
  25180. Object.keys(dirMeta.outputs).forEach((propName) => {
  25181. const eventName = dirMeta.outputs[propName];
  25182. if (usedEvents.has(eventName)) {
  25183. // Note: it's important to not quote the key so that we can capture renames by minifiers!
  25184. outputDefs.push(new LiteralMapEntry(propName, literal(eventName), false));
  25185. }
  25186. });
  25187. let updateDirectiveExpressions = [];
  25188. if (dirAst.inputs.length || (flags & (262144 /* DoCheck */ | 65536 /* OnInit */)) > 0) {
  25189. updateDirectiveExpressions =
  25190. dirAst.inputs.map((input, bindingIndex) => this._preprocessUpdateExpression({
  25191. nodeIndex,
  25192. bindingIndex,
  25193. sourceSpan: input.sourceSpan,
  25194. context: COMP_VAR,
  25195. value: input.value
  25196. }));
  25197. }
  25198. const dirContextExpr = importExpr(Identifiers$1.nodeValue).callFn([VIEW_VAR, literal(nodeIndex)]);
  25199. const hostBindings = dirAst.hostProperties.map((inputAst) => ({
  25200. context: dirContextExpr,
  25201. dirAst,
  25202. inputAst,
  25203. }));
  25204. const hostEvents = dirAst.hostEvents.map((hostEventAst) => ({
  25205. context: dirContextExpr,
  25206. eventAst: hostEventAst,
  25207. dirAst,
  25208. }));
  25209. // Check index is the same as the node index during compilation
  25210. // They might only differ at runtime
  25211. const checkIndex = nodeIndex;
  25212. this.nodes[nodeIndex] = () => ({
  25213. sourceSpan: dirAst.sourceSpan,
  25214. nodeFlags: 16384 /* TypeDirective */ | flags,
  25215. nodeDef: importExpr(Identifiers$1.directiveDef).callFn([
  25216. literal(checkIndex),
  25217. literal(flags),
  25218. queryMatchExprs.length ? literalArr(queryMatchExprs) : NULL_EXPR,
  25219. literal(childCount),
  25220. providerExpr,
  25221. depsExpr,
  25222. inputDefs.length ? new LiteralMapExpr(inputDefs) : NULL_EXPR,
  25223. outputDefs.length ? new LiteralMapExpr(outputDefs) : NULL_EXPR,
  25224. ]),
  25225. updateDirectives: updateDirectiveExpressions,
  25226. directive: dirAst.directive.type,
  25227. });
  25228. return { hostBindings, hostEvents };
  25229. }
  25230. _visitProvider(providerAst, queryMatches) {
  25231. this._addProviderNode(this._visitProviderOrDirective(providerAst, queryMatches));
  25232. }
  25233. _visitComponentFactoryResolverProvider(directives) {
  25234. const componentDirMeta = directives.find(dirAst => dirAst.directive.isComponent);
  25235. if (componentDirMeta && componentDirMeta.directive.entryComponents.length) {
  25236. const { providerExpr, depsExpr, flags, tokenExpr } = componentFactoryResolverProviderDef(this.reflector, this.outputCtx, 8192 /* PrivateProvider */, componentDirMeta.directive.entryComponents);
  25237. this._addProviderNode({
  25238. providerExpr,
  25239. depsExpr,
  25240. flags,
  25241. tokenExpr,
  25242. queryMatchExprs: [],
  25243. sourceSpan: componentDirMeta.sourceSpan
  25244. });
  25245. }
  25246. }
  25247. _addProviderNode(data) {
  25248. // providerDef(
  25249. // flags: NodeFlags, matchedQueries: [string, QueryValueType][], token:any,
  25250. // value: any, deps: ([DepFlags, any] | any)[]): NodeDef;
  25251. this.nodes.push(() => ({
  25252. sourceSpan: data.sourceSpan,
  25253. nodeFlags: data.flags,
  25254. nodeDef: importExpr(Identifiers$1.providerDef).callFn([
  25255. literal(data.flags),
  25256. data.queryMatchExprs.length ? literalArr(data.queryMatchExprs) : NULL_EXPR,
  25257. data.tokenExpr, data.providerExpr, data.depsExpr
  25258. ])
  25259. }));
  25260. }
  25261. _visitProviderOrDirective(providerAst, queryMatches) {
  25262. let flags = 0 /* None */;
  25263. let queryMatchExprs = [];
  25264. queryMatches.forEach((match) => {
  25265. if (tokenReference(match.value) === tokenReference(providerAst.token)) {
  25266. queryMatchExprs.push(literalArr([literal(match.queryId), literal(4 /* Provider */)]));
  25267. }
  25268. });
  25269. const { providerExpr, depsExpr, flags: providerFlags, tokenExpr } = providerDef(this.outputCtx, providerAst);
  25270. return {
  25271. flags: flags | providerFlags,
  25272. queryMatchExprs,
  25273. providerExpr,
  25274. depsExpr,
  25275. tokenExpr,
  25276. sourceSpan: providerAst.sourceSpan
  25277. };
  25278. }
  25279. getLocal(name) {
  25280. if (name == EventHandlerVars.event.name) {
  25281. return EventHandlerVars.event;
  25282. }
  25283. let currViewExpr = VIEW_VAR;
  25284. for (let currBuilder = this; currBuilder; currBuilder = currBuilder.parent,
  25285. currViewExpr = currViewExpr.prop('parent').cast(DYNAMIC_TYPE)) {
  25286. // check references
  25287. const refNodeIndex = currBuilder.refNodeIndices[name];
  25288. if (refNodeIndex != null) {
  25289. return importExpr(Identifiers$1.nodeValue).callFn([currViewExpr, literal(refNodeIndex)]);
  25290. }
  25291. // check variables
  25292. const varAst = currBuilder.variables.find((varAst) => varAst.name === name);
  25293. if (varAst) {
  25294. const varValue = varAst.value || IMPLICIT_TEMPLATE_VAR;
  25295. return currViewExpr.prop('context').prop(varValue);
  25296. }
  25297. }
  25298. return null;
  25299. }
  25300. notifyImplicitReceiverUse() {
  25301. // Not needed in View Engine as View Engine walks through the generated
  25302. // expressions to figure out if the implicit receiver is used and needs
  25303. // to be generated as part of the pre-update statements.
  25304. }
  25305. _createLiteralArrayConverter(sourceSpan, argCount) {
  25306. if (argCount === 0) {
  25307. const valueExpr = importExpr(Identifiers$1.EMPTY_ARRAY);
  25308. return () => valueExpr;
  25309. }
  25310. const checkIndex = this.nodes.length;
  25311. this.nodes.push(() => ({
  25312. sourceSpan,
  25313. nodeFlags: 32 /* TypePureArray */,
  25314. nodeDef: importExpr(Identifiers$1.pureArrayDef).callFn([
  25315. literal(checkIndex),
  25316. literal(argCount),
  25317. ])
  25318. }));
  25319. return (args) => callCheckStmt(checkIndex, args);
  25320. }
  25321. _createLiteralMapConverter(sourceSpan, keys) {
  25322. if (keys.length === 0) {
  25323. const valueExpr = importExpr(Identifiers$1.EMPTY_MAP);
  25324. return () => valueExpr;
  25325. }
  25326. const map = literalMap(keys.map((e, i) => (Object.assign(Object.assign({}, e), { value: literal(i) }))));
  25327. const checkIndex = this.nodes.length;
  25328. this.nodes.push(() => ({
  25329. sourceSpan,
  25330. nodeFlags: 64 /* TypePureObject */,
  25331. nodeDef: importExpr(Identifiers$1.pureObjectDef).callFn([
  25332. literal(checkIndex),
  25333. map,
  25334. ])
  25335. }));
  25336. return (args) => callCheckStmt(checkIndex, args);
  25337. }
  25338. _createPipeConverter(expression, name, argCount) {
  25339. const pipe = this.usedPipes.find((pipeSummary) => pipeSummary.name === name);
  25340. if (pipe.pure) {
  25341. const checkIndex = this.nodes.length;
  25342. this.nodes.push(() => ({
  25343. sourceSpan: expression.sourceSpan,
  25344. nodeFlags: 128 /* TypePurePipe */,
  25345. nodeDef: importExpr(Identifiers$1.purePipeDef).callFn([
  25346. literal(checkIndex),
  25347. literal(argCount),
  25348. ])
  25349. }));
  25350. // find underlying pipe in the component view
  25351. let compViewExpr = VIEW_VAR;
  25352. let compBuilder = this;
  25353. while (compBuilder.parent) {
  25354. compBuilder = compBuilder.parent;
  25355. compViewExpr = compViewExpr.prop('parent').cast(DYNAMIC_TYPE);
  25356. }
  25357. const pipeNodeIndex = compBuilder.purePipeNodeIndices[name];
  25358. const pipeValueExpr = importExpr(Identifiers$1.nodeValue).callFn([compViewExpr, literal(pipeNodeIndex)]);
  25359. return (args) => callUnwrapValue(expression.nodeIndex, expression.bindingIndex, callCheckStmt(checkIndex, [pipeValueExpr].concat(args)));
  25360. }
  25361. else {
  25362. const nodeIndex = this._createPipe(expression.sourceSpan, pipe);
  25363. const nodeValueExpr = importExpr(Identifiers$1.nodeValue).callFn([VIEW_VAR, literal(nodeIndex)]);
  25364. return (args) => callUnwrapValue(expression.nodeIndex, expression.bindingIndex, nodeValueExpr.callMethod('transform', args));
  25365. }
  25366. }
  25367. _createPipe(sourceSpan, pipe) {
  25368. const nodeIndex = this.nodes.length;
  25369. let flags = 0 /* None */;
  25370. pipe.type.lifecycleHooks.forEach((lifecycleHook) => {
  25371. // for pipes, we only support ngOnDestroy
  25372. if (lifecycleHook === LifecycleHooks.OnDestroy) {
  25373. flags |= lifecycleHookToNodeFlag(lifecycleHook);
  25374. }
  25375. });
  25376. const depExprs = pipe.type.diDeps.map((diDep) => depDef(this.outputCtx, diDep));
  25377. // function pipeDef(
  25378. // flags: NodeFlags, ctor: any, deps: ([DepFlags, any] | any)[]): NodeDef
  25379. this.nodes.push(() => ({
  25380. sourceSpan,
  25381. nodeFlags: 16 /* TypePipe */,
  25382. nodeDef: importExpr(Identifiers$1.pipeDef).callFn([
  25383. literal(flags), this.outputCtx.importExpr(pipe.type.reference), literalArr(depExprs)
  25384. ])
  25385. }));
  25386. return nodeIndex;
  25387. }
  25388. /**
  25389. * For the AST in `UpdateExpression.value`:
  25390. * - create nodes for pipes, literal arrays and, literal maps,
  25391. * - update the AST to replace pipes, literal arrays and, literal maps with calls to check fn.
  25392. *
  25393. * WARNING: This might create new nodeDefs (for pipes and literal arrays and literal maps)!
  25394. */
  25395. _preprocessUpdateExpression(expression) {
  25396. return {
  25397. nodeIndex: expression.nodeIndex,
  25398. bindingIndex: expression.bindingIndex,
  25399. sourceSpan: expression.sourceSpan,
  25400. context: expression.context,
  25401. value: convertPropertyBindingBuiltins({
  25402. createLiteralArrayConverter: (argCount) => this._createLiteralArrayConverter(expression.sourceSpan, argCount),
  25403. createLiteralMapConverter: (keys) => this._createLiteralMapConverter(expression.sourceSpan, keys),
  25404. createPipeConverter: (name, argCount) => this._createPipeConverter(expression, name, argCount)
  25405. }, expression.value)
  25406. };
  25407. }
  25408. _createNodeExpressions() {
  25409. const self = this;
  25410. let updateBindingCount = 0;
  25411. const updateRendererStmts = [];
  25412. const updateDirectivesStmts = [];
  25413. const nodeDefExprs = this.nodes.map((factory, nodeIndex) => {
  25414. const { nodeDef, nodeFlags, updateDirectives, updateRenderer, sourceSpan } = factory();
  25415. if (updateRenderer) {
  25416. updateRendererStmts.push(...createUpdateStatements(nodeIndex, sourceSpan, updateRenderer, false));
  25417. }
  25418. if (updateDirectives) {
  25419. updateDirectivesStmts.push(...createUpdateStatements(nodeIndex, sourceSpan, updateDirectives, (nodeFlags & (262144 /* DoCheck */ | 65536 /* OnInit */)) > 0));
  25420. }
  25421. // We use a comma expression to call the log function before
  25422. // the nodeDef function, but still use the result of the nodeDef function
  25423. // as the value.
  25424. // Note: We only add the logger to elements / text nodes,
  25425. // so we don't generate too much code.
  25426. const logWithNodeDef = nodeFlags & 3 /* CatRenderNode */ ?
  25427. new CommaExpr([LOG_VAR$1.callFn([]).callFn([]), nodeDef]) :
  25428. nodeDef;
  25429. return applySourceSpanToExpressionIfNeeded(logWithNodeDef, sourceSpan);
  25430. });
  25431. return { updateRendererStmts, updateDirectivesStmts, nodeDefExprs };
  25432. function createUpdateStatements(nodeIndex, sourceSpan, expressions, allowEmptyExprs) {
  25433. const updateStmts = [];
  25434. const exprs = expressions.map(({ sourceSpan, context, value }) => {
  25435. const bindingId = `${updateBindingCount++}`;
  25436. const nameResolver = context === COMP_VAR ? self : null;
  25437. const { stmts, currValExpr } = convertPropertyBinding(nameResolver, context, value, bindingId, BindingForm.General);
  25438. updateStmts.push(...stmts.map((stmt) => applySourceSpanToStatementIfNeeded(stmt, sourceSpan)));
  25439. return applySourceSpanToExpressionIfNeeded(currValExpr, sourceSpan);
  25440. });
  25441. if (expressions.length || allowEmptyExprs) {
  25442. updateStmts.push(applySourceSpanToStatementIfNeeded(callCheckStmt(nodeIndex, exprs).toStmt(), sourceSpan));
  25443. }
  25444. return updateStmts;
  25445. }
  25446. }
  25447. _createElementHandleEventFn(nodeIndex, handlers) {
  25448. const handleEventStmts = [];
  25449. let handleEventBindingCount = 0;
  25450. handlers.forEach(({ context, eventAst, dirAst }) => {
  25451. const bindingId = `${handleEventBindingCount++}`;
  25452. const nameResolver = context === COMP_VAR ? this : null;
  25453. const { stmts, allowDefault } = convertActionBinding(nameResolver, context, eventAst.handler, bindingId);
  25454. const trueStmts = stmts;
  25455. if (allowDefault) {
  25456. trueStmts.push(ALLOW_DEFAULT_VAR.set(allowDefault.and(ALLOW_DEFAULT_VAR)).toStmt());
  25457. }
  25458. const { target: eventTarget, name: eventName } = elementEventNameAndTarget(eventAst, dirAst);
  25459. const fullEventName = elementEventFullName(eventTarget, eventName);
  25460. handleEventStmts.push(applySourceSpanToStatementIfNeeded(new IfStmt(literal(fullEventName).identical(EVENT_NAME_VAR), trueStmts), eventAst.sourceSpan));
  25461. });
  25462. let handleEventFn;
  25463. if (handleEventStmts.length > 0) {
  25464. const preStmts = [ALLOW_DEFAULT_VAR.set(literal(true)).toDeclStmt(BOOL_TYPE)];
  25465. if (!this.component.isHost && findReadVarNames(handleEventStmts).has(COMP_VAR.name)) {
  25466. preStmts.push(COMP_VAR.set(VIEW_VAR.prop('component')).toDeclStmt(this.compType));
  25467. }
  25468. handleEventFn = fn([
  25469. new FnParam(VIEW_VAR.name, INFERRED_TYPE),
  25470. new FnParam(EVENT_NAME_VAR.name, INFERRED_TYPE),
  25471. new FnParam(EventHandlerVars.event.name, INFERRED_TYPE)
  25472. ], [...preStmts, ...handleEventStmts, new ReturnStatement(ALLOW_DEFAULT_VAR)], INFERRED_TYPE);
  25473. }
  25474. else {
  25475. handleEventFn = NULL_EXPR;
  25476. }
  25477. return handleEventFn;
  25478. }
  25479. visitDirective(ast, context) { }
  25480. visitDirectiveProperty(ast, context) { }
  25481. visitReference(ast, context) { }
  25482. visitVariable(ast, context) { }
  25483. visitEvent(ast, context) { }
  25484. visitElementProperty(ast, context) { }
  25485. visitAttr(ast, context) { }
  25486. }
  25487. function needsAdditionalRootNode(astNodes) {
  25488. const lastAstNode = astNodes[astNodes.length - 1];
  25489. if (lastAstNode instanceof EmbeddedTemplateAst) {
  25490. return lastAstNode.hasViewContainer;
  25491. }
  25492. if (lastAstNode instanceof ElementAst) {
  25493. if (isNgContainer(lastAstNode.name) && lastAstNode.children.length) {
  25494. return needsAdditionalRootNode(lastAstNode.children);
  25495. }
  25496. return lastAstNode.hasViewContainer;
  25497. }
  25498. return lastAstNode instanceof NgContentAst;
  25499. }
  25500. function elementBindingDef(inputAst, dirAst) {
  25501. const inputType = inputAst.type;
  25502. switch (inputType) {
  25503. case 1 /* Attribute */:
  25504. return literalArr([
  25505. literal(1 /* TypeElementAttribute */), literal(inputAst.name),
  25506. literal(inputAst.securityContext)
  25507. ]);
  25508. case 0 /* Property */:
  25509. return literalArr([
  25510. literal(8 /* TypeProperty */), literal(inputAst.name),
  25511. literal(inputAst.securityContext)
  25512. ]);
  25513. case 4 /* Animation */:
  25514. const bindingType = 8 /* TypeProperty */ |
  25515. (dirAst && dirAst.directive.isComponent ? 32 /* SyntheticHostProperty */ :
  25516. 16 /* SyntheticProperty */);
  25517. return literalArr([
  25518. literal(bindingType), literal('@' + inputAst.name), literal(inputAst.securityContext)
  25519. ]);
  25520. case 2 /* Class */:
  25521. return literalArr([literal(2 /* TypeElementClass */), literal(inputAst.name), NULL_EXPR]);
  25522. case 3 /* Style */:
  25523. return literalArr([
  25524. literal(4 /* TypeElementStyle */), literal(inputAst.name), literal(inputAst.unit)
  25525. ]);
  25526. default:
  25527. // This default case is not needed by TypeScript compiler, as the switch is exhaustive.
  25528. // However Closure Compiler does not understand that and reports an error in typed mode.
  25529. // The `throw new Error` below works around the problem, and the unexpected: never variable
  25530. // makes sure tsc still checks this code is unreachable.
  25531. const unexpected = inputType;
  25532. throw new Error(`unexpected ${unexpected}`);
  25533. }
  25534. }
  25535. function fixedAttrsDef(elementAst) {
  25536. const mapResult = Object.create(null);
  25537. elementAst.attrs.forEach(attrAst => {
  25538. mapResult[attrAst.name] = attrAst.value;
  25539. });
  25540. elementAst.directives.forEach(dirAst => {
  25541. Object.keys(dirAst.directive.hostAttributes).forEach(name => {
  25542. const value = dirAst.directive.hostAttributes[name];
  25543. const prevValue = mapResult[name];
  25544. mapResult[name] = prevValue != null ? mergeAttributeValue(name, prevValue, value) : value;
  25545. });
  25546. });
  25547. // Note: We need to sort to get a defined output order
  25548. // for tests and for caching generated artifacts...
  25549. return literalArr(Object.keys(mapResult).sort().map((attrName) => literalArr([literal(attrName), literal(mapResult[attrName])])));
  25550. }
  25551. function mergeAttributeValue(attrName, attrValue1, attrValue2) {
  25552. if (attrName == CLASS_ATTR$1 || attrName == STYLE_ATTR) {
  25553. return `${attrValue1} ${attrValue2}`;
  25554. }
  25555. else {
  25556. return attrValue2;
  25557. }
  25558. }
  25559. function callCheckStmt(nodeIndex, exprs) {
  25560. if (exprs.length > 10) {
  25561. return CHECK_VAR.callFn([VIEW_VAR, literal(nodeIndex), literal(1 /* Dynamic */), literalArr(exprs)]);
  25562. }
  25563. else {
  25564. return CHECK_VAR.callFn([VIEW_VAR, literal(nodeIndex), literal(0 /* Inline */), ...exprs]);
  25565. }
  25566. }
  25567. function callUnwrapValue(nodeIndex, bindingIdx, expr) {
  25568. return importExpr(Identifiers$1.unwrapValue).callFn([
  25569. VIEW_VAR, literal(nodeIndex), literal(bindingIdx), expr
  25570. ]);
  25571. }
  25572. function elementEventNameAndTarget(eventAst, dirAst) {
  25573. if (eventAst.isAnimation) {
  25574. return {
  25575. name: `@${eventAst.name}.${eventAst.phase}`,
  25576. target: dirAst && dirAst.directive.isComponent ? 'component' : null
  25577. };
  25578. }
  25579. else {
  25580. return eventAst;
  25581. }
  25582. }
  25583. function calcQueryFlags(query) {
  25584. let flags = 0 /* None */;
  25585. // Note: We only make queries static that query for a single item and the user specifically
  25586. // set the to be static. This is because of backwards compatibility with the old view compiler...
  25587. if (query.first && query.static) {
  25588. flags |= 268435456 /* StaticQuery */;
  25589. }
  25590. else {
  25591. flags |= 536870912 /* DynamicQuery */;
  25592. }
  25593. if (query.emitDistinctChangesOnly) {
  25594. flags |= -2147483648 /* EmitDistinctChangesOnly */;
  25595. }
  25596. return flags;
  25597. }
  25598. function elementEventFullName(target, name) {
  25599. return target ? `${target}:${name}` : name;
  25600. }
  25601. /**
  25602. * @license
  25603. * Copyright Google LLC All Rights Reserved.
  25604. *
  25605. * Use of this source code is governed by an MIT-style license that can be
  25606. * found in the LICENSE file at https://angular.io/license
  25607. */
  25608. /**
  25609. * A container for message extracted from the templates.
  25610. */
  25611. class MessageBundle {
  25612. constructor(_htmlParser, _implicitTags, _implicitAttrs, _locale = null) {
  25613. this._htmlParser = _htmlParser;
  25614. this._implicitTags = _implicitTags;
  25615. this._implicitAttrs = _implicitAttrs;
  25616. this._locale = _locale;
  25617. this._messages = [];
  25618. }
  25619. updateFromTemplate(html, url, interpolationConfig) {
  25620. const htmlParserResult = this._htmlParser.parse(html, url, { tokenizeExpansionForms: true, interpolationConfig });
  25621. if (htmlParserResult.errors.length) {
  25622. return htmlParserResult.errors;
  25623. }
  25624. const i18nParserResult = extractMessages(htmlParserResult.rootNodes, interpolationConfig, this._implicitTags, this._implicitAttrs);
  25625. if (i18nParserResult.errors.length) {
  25626. return i18nParserResult.errors;
  25627. }
  25628. this._messages.push(...i18nParserResult.messages);
  25629. return [];
  25630. }
  25631. // Return the message in the internal format
  25632. // The public (serialized) format might be different, see the `write` method.
  25633. getMessages() {
  25634. return this._messages;
  25635. }
  25636. write(serializer, filterSources) {
  25637. const messages = {};
  25638. const mapperVisitor = new MapPlaceholderNames();
  25639. // Deduplicate messages based on their ID
  25640. this._messages.forEach(message => {
  25641. const id = serializer.digest(message);
  25642. if (!messages.hasOwnProperty(id)) {
  25643. messages[id] = message;
  25644. }
  25645. else {
  25646. messages[id].sources.push(...message.sources);
  25647. }
  25648. });
  25649. // Transform placeholder names using the serializer mapping
  25650. const msgList = Object.keys(messages).map(id => {
  25651. const mapper = serializer.createNameMapper(messages[id]);
  25652. const src = messages[id];
  25653. const nodes = mapper ? mapperVisitor.convert(src.nodes, mapper) : src.nodes;
  25654. let transformedMessage = new Message(nodes, {}, {}, src.meaning, src.description, id);
  25655. transformedMessage.sources = src.sources;
  25656. if (filterSources) {
  25657. transformedMessage.sources.forEach((source) => source.filePath = filterSources(source.filePath));
  25658. }
  25659. return transformedMessage;
  25660. });
  25661. return serializer.write(msgList, this._locale);
  25662. }
  25663. }
  25664. // Transform an i18n AST by renaming the placeholder nodes with the given mapper
  25665. class MapPlaceholderNames extends CloneVisitor {
  25666. convert(nodes, mapper) {
  25667. return mapper ? nodes.map(n => n.visit(this, mapper)) : nodes;
  25668. }
  25669. visitTagPlaceholder(ph, mapper) {
  25670. const startName = mapper.toPublicName(ph.startName);
  25671. const closeName = ph.closeName ? mapper.toPublicName(ph.closeName) : ph.closeName;
  25672. const children = ph.children.map(n => n.visit(this, mapper));
  25673. return new TagPlaceholder(ph.tag, ph.attrs, startName, closeName, children, ph.isVoid, ph.sourceSpan, ph.startSourceSpan, ph.endSourceSpan);
  25674. }
  25675. visitPlaceholder(ph, mapper) {
  25676. return new Placeholder(ph.value, mapper.toPublicName(ph.name), ph.sourceSpan);
  25677. }
  25678. visitIcuPlaceholder(ph, mapper) {
  25679. return new IcuPlaceholder(ph.value, mapper.toPublicName(ph.name), ph.sourceSpan);
  25680. }
  25681. }
  25682. /**
  25683. * @license
  25684. * Copyright Google LLC All Rights Reserved.
  25685. *
  25686. * Use of this source code is governed by an MIT-style license that can be
  25687. * found in the LICENSE file at https://angular.io/license
  25688. */
  25689. class GeneratedFile {
  25690. constructor(srcFileUrl, genFileUrl, sourceOrStmts) {
  25691. this.srcFileUrl = srcFileUrl;
  25692. this.genFileUrl = genFileUrl;
  25693. if (typeof sourceOrStmts === 'string') {
  25694. this.source = sourceOrStmts;
  25695. this.stmts = null;
  25696. }
  25697. else {
  25698. this.source = null;
  25699. this.stmts = sourceOrStmts;
  25700. }
  25701. }
  25702. isEquivalent(other) {
  25703. if (this.genFileUrl !== other.genFileUrl) {
  25704. return false;
  25705. }
  25706. if (this.source) {
  25707. return this.source === other.source;
  25708. }
  25709. if (other.stmts == null) {
  25710. return false;
  25711. }
  25712. // Note: the constructor guarantees that if this.source is not filled,
  25713. // then this.stmts is.
  25714. return areAllEquivalent(this.stmts, other.stmts);
  25715. }
  25716. }
  25717. function toTypeScript(file, preamble = '') {
  25718. if (!file.stmts) {
  25719. throw new Error(`Illegal state: No stmts present on GeneratedFile ${file.genFileUrl}`);
  25720. }
  25721. return new TypeScriptEmitter().emitStatements(file.genFileUrl, file.stmts, preamble);
  25722. }
  25723. /**
  25724. * @license
  25725. * Copyright Google LLC All Rights Reserved.
  25726. *
  25727. * Use of this source code is governed by an MIT-style license that can be
  25728. * found in the LICENSE file at https://angular.io/license
  25729. */
  25730. function listLazyRoutes(moduleMeta, reflector) {
  25731. const allLazyRoutes = [];
  25732. for (const { provider, module } of moduleMeta.transitiveModule.providers) {
  25733. if (tokenReference(provider.token) === reflector.ROUTES) {
  25734. const loadChildren = _collectLoadChildren(provider.useValue);
  25735. for (const route of loadChildren) {
  25736. allLazyRoutes.push(parseLazyRoute(route, reflector, module.reference));
  25737. }
  25738. }
  25739. }
  25740. return allLazyRoutes;
  25741. }
  25742. function _collectLoadChildren(routes, target = []) {
  25743. if (typeof routes === 'string') {
  25744. target.push(routes);
  25745. }
  25746. else if (Array.isArray(routes)) {
  25747. for (const route of routes) {
  25748. _collectLoadChildren(route, target);
  25749. }
  25750. }
  25751. else if (routes.loadChildren) {
  25752. _collectLoadChildren(routes.loadChildren, target);
  25753. }
  25754. else if (routes.children) {
  25755. _collectLoadChildren(routes.children, target);
  25756. }
  25757. return target;
  25758. }
  25759. function parseLazyRoute(route, reflector, module) {
  25760. const [routePath, routeName] = route.split('#');
  25761. const referencedModule = reflector.resolveExternalReference({
  25762. moduleName: routePath,
  25763. name: routeName,
  25764. }, module ? module.filePath : undefined);
  25765. return { route: route, module: module || referencedModule, referencedModule };
  25766. }
  25767. /**
  25768. * @license
  25769. * Copyright Google LLC All Rights Reserved.
  25770. *
  25771. * Use of this source code is governed by an MIT-style license that can be
  25772. * found in the LICENSE file at https://angular.io/license
  25773. */
  25774. const TS = /^(?!.*\.d\.ts$).*\.ts$/;
  25775. class ResolvedStaticSymbol {
  25776. constructor(symbol, metadata) {
  25777. this.symbol = symbol;
  25778. this.metadata = metadata;
  25779. }
  25780. }
  25781. const SUPPORTED_SCHEMA_VERSION = 4;
  25782. /**
  25783. * This class is responsible for loading metadata per symbol,
  25784. * and normalizing references between symbols.
  25785. *
  25786. * Internally, it only uses symbols without members,
  25787. * and deduces the values for symbols with members based
  25788. * on these symbols.
  25789. */
  25790. class StaticSymbolResolver {
  25791. constructor(host, staticSymbolCache, summaryResolver, errorRecorder) {
  25792. this.host = host;
  25793. this.staticSymbolCache = staticSymbolCache;
  25794. this.summaryResolver = summaryResolver;
  25795. this.errorRecorder = errorRecorder;
  25796. this.metadataCache = new Map();
  25797. // Note: this will only contain StaticSymbols without members!
  25798. this.resolvedSymbols = new Map();
  25799. // Note: this will only contain StaticSymbols without members!
  25800. this.importAs = new Map();
  25801. this.symbolResourcePaths = new Map();
  25802. this.symbolFromFile = new Map();
  25803. this.knownFileNameToModuleNames = new Map();
  25804. }
  25805. resolveSymbol(staticSymbol) {
  25806. if (staticSymbol.members.length > 0) {
  25807. return this._resolveSymbolMembers(staticSymbol);
  25808. }
  25809. // Note: always ask for a summary first,
  25810. // as we might have read shallow metadata via a .d.ts file
  25811. // for the symbol.
  25812. const resultFromSummary = this._resolveSymbolFromSummary(staticSymbol);
  25813. if (resultFromSummary) {
  25814. return resultFromSummary;
  25815. }
  25816. const resultFromCache = this.resolvedSymbols.get(staticSymbol);
  25817. if (resultFromCache) {
  25818. return resultFromCache;
  25819. }
  25820. // Note: Some users use libraries that were not compiled with ngc, i.e. they don't
  25821. // have summaries, only .d.ts files. So we always need to check both, the summary
  25822. // and metadata.
  25823. this._createSymbolsOf(staticSymbol.filePath);
  25824. return this.resolvedSymbols.get(staticSymbol);
  25825. }
  25826. /**
  25827. * getImportAs produces a symbol that can be used to import the given symbol.
  25828. * The import might be different than the symbol if the symbol is exported from
  25829. * a library with a summary; in which case we want to import the symbol from the
  25830. * ngfactory re-export instead of directly to avoid introducing a direct dependency
  25831. * on an otherwise indirect dependency.
  25832. *
  25833. * @param staticSymbol the symbol for which to generate a import symbol
  25834. */
  25835. getImportAs(staticSymbol, useSummaries = true) {
  25836. if (staticSymbol.members.length) {
  25837. const baseSymbol = this.getStaticSymbol(staticSymbol.filePath, staticSymbol.name);
  25838. const baseImportAs = this.getImportAs(baseSymbol, useSummaries);
  25839. return baseImportAs ?
  25840. this.getStaticSymbol(baseImportAs.filePath, baseImportAs.name, staticSymbol.members) :
  25841. null;
  25842. }
  25843. const summarizedFileName = stripSummaryForJitFileSuffix(staticSymbol.filePath);
  25844. if (summarizedFileName !== staticSymbol.filePath) {
  25845. const summarizedName = stripSummaryForJitNameSuffix(staticSymbol.name);
  25846. const baseSymbol = this.getStaticSymbol(summarizedFileName, summarizedName, staticSymbol.members);
  25847. const baseImportAs = this.getImportAs(baseSymbol, useSummaries);
  25848. return baseImportAs ? this.getStaticSymbol(summaryForJitFileName(baseImportAs.filePath), summaryForJitName(baseImportAs.name), baseSymbol.members) :
  25849. null;
  25850. }
  25851. let result = (useSummaries && this.summaryResolver.getImportAs(staticSymbol)) || null;
  25852. if (!result) {
  25853. result = this.importAs.get(staticSymbol);
  25854. }
  25855. return result;
  25856. }
  25857. /**
  25858. * getResourcePath produces the path to the original location of the symbol and should
  25859. * be used to determine the relative location of resource references recorded in
  25860. * symbol metadata.
  25861. */
  25862. getResourcePath(staticSymbol) {
  25863. return this.symbolResourcePaths.get(staticSymbol) || staticSymbol.filePath;
  25864. }
  25865. /**
  25866. * getTypeArity returns the number of generic type parameters the given symbol
  25867. * has. If the symbol is not a type the result is null.
  25868. */
  25869. getTypeArity(staticSymbol) {
  25870. // If the file is a factory/ngsummary file, don't resolve the symbol as doing so would
  25871. // cause the metadata for an factory/ngsummary file to be loaded which doesn't exist.
  25872. // All references to generated classes must include the correct arity whenever
  25873. // generating code.
  25874. if (isGeneratedFile(staticSymbol.filePath)) {
  25875. return null;
  25876. }
  25877. let resolvedSymbol = unwrapResolvedMetadata(this.resolveSymbol(staticSymbol));
  25878. while (resolvedSymbol && resolvedSymbol.metadata instanceof StaticSymbol) {
  25879. resolvedSymbol = unwrapResolvedMetadata(this.resolveSymbol(resolvedSymbol.metadata));
  25880. }
  25881. return (resolvedSymbol && resolvedSymbol.metadata && resolvedSymbol.metadata.arity) || null;
  25882. }
  25883. getKnownModuleName(filePath) {
  25884. return this.knownFileNameToModuleNames.get(filePath) || null;
  25885. }
  25886. recordImportAs(sourceSymbol, targetSymbol) {
  25887. sourceSymbol.assertNoMembers();
  25888. targetSymbol.assertNoMembers();
  25889. this.importAs.set(sourceSymbol, targetSymbol);
  25890. }
  25891. recordModuleNameForFileName(fileName, moduleName) {
  25892. this.knownFileNameToModuleNames.set(fileName, moduleName);
  25893. }
  25894. /**
  25895. * Invalidate all information derived from the given file and return the
  25896. * static symbols contained in the file.
  25897. *
  25898. * @param fileName the file to invalidate
  25899. */
  25900. invalidateFile(fileName) {
  25901. this.metadataCache.delete(fileName);
  25902. const symbols = this.symbolFromFile.get(fileName);
  25903. if (!symbols) {
  25904. return [];
  25905. }
  25906. this.symbolFromFile.delete(fileName);
  25907. for (const symbol of symbols) {
  25908. this.resolvedSymbols.delete(symbol);
  25909. this.importAs.delete(symbol);
  25910. this.symbolResourcePaths.delete(symbol);
  25911. }
  25912. return symbols;
  25913. }
  25914. /** @internal */
  25915. ignoreErrorsFor(cb) {
  25916. const recorder = this.errorRecorder;
  25917. this.errorRecorder = () => { };
  25918. try {
  25919. return cb();
  25920. }
  25921. finally {
  25922. this.errorRecorder = recorder;
  25923. }
  25924. }
  25925. _resolveSymbolMembers(staticSymbol) {
  25926. const members = staticSymbol.members;
  25927. const baseResolvedSymbol = this.resolveSymbol(this.getStaticSymbol(staticSymbol.filePath, staticSymbol.name));
  25928. if (!baseResolvedSymbol) {
  25929. return null;
  25930. }
  25931. let baseMetadata = unwrapResolvedMetadata(baseResolvedSymbol.metadata);
  25932. if (baseMetadata instanceof StaticSymbol) {
  25933. return new ResolvedStaticSymbol(staticSymbol, this.getStaticSymbol(baseMetadata.filePath, baseMetadata.name, members));
  25934. }
  25935. else if (baseMetadata && baseMetadata.__symbolic === 'class') {
  25936. if (baseMetadata.statics && members.length === 1) {
  25937. return new ResolvedStaticSymbol(staticSymbol, baseMetadata.statics[members[0]]);
  25938. }
  25939. }
  25940. else {
  25941. let value = baseMetadata;
  25942. for (let i = 0; i < members.length && value; i++) {
  25943. value = value[members[i]];
  25944. }
  25945. return new ResolvedStaticSymbol(staticSymbol, value);
  25946. }
  25947. return null;
  25948. }
  25949. _resolveSymbolFromSummary(staticSymbol) {
  25950. const summary = this.summaryResolver.resolveSummary(staticSymbol);
  25951. return summary ? new ResolvedStaticSymbol(staticSymbol, summary.metadata) : null;
  25952. }
  25953. /**
  25954. * getStaticSymbol produces a Type whose metadata is known but whose implementation is not loaded.
  25955. * All types passed to the StaticResolver should be pseudo-types returned by this method.
  25956. *
  25957. * @param declarationFile the absolute path of the file where the symbol is declared
  25958. * @param name the name of the type.
  25959. * @param members a symbol for a static member of the named type
  25960. */
  25961. getStaticSymbol(declarationFile, name, members) {
  25962. return this.staticSymbolCache.get(declarationFile, name, members);
  25963. }
  25964. /**
  25965. * hasDecorators checks a file's metadata for the presence of decorators without evaluating the
  25966. * metadata.
  25967. *
  25968. * @param filePath the absolute path to examine for decorators.
  25969. * @returns true if any class in the file has a decorator.
  25970. */
  25971. hasDecorators(filePath) {
  25972. const metadata = this.getModuleMetadata(filePath);
  25973. if (metadata['metadata']) {
  25974. return Object.keys(metadata['metadata']).some((metadataKey) => {
  25975. const entry = metadata['metadata'][metadataKey];
  25976. return entry && entry.__symbolic === 'class' && entry.decorators;
  25977. });
  25978. }
  25979. return false;
  25980. }
  25981. getSymbolsOf(filePath) {
  25982. const summarySymbols = this.summaryResolver.getSymbolsOf(filePath);
  25983. if (summarySymbols) {
  25984. return summarySymbols;
  25985. }
  25986. // Note: Some users use libraries that were not compiled with ngc, i.e. they don't
  25987. // have summaries, only .d.ts files, but `summaryResolver.isLibraryFile` returns true.
  25988. this._createSymbolsOf(filePath);
  25989. return this.symbolFromFile.get(filePath) || [];
  25990. }
  25991. _createSymbolsOf(filePath) {
  25992. if (this.symbolFromFile.has(filePath)) {
  25993. return;
  25994. }
  25995. const resolvedSymbols = [];
  25996. const metadata = this.getModuleMetadata(filePath);
  25997. if (metadata['importAs']) {
  25998. // Index bundle indices should use the importAs module name defined
  25999. // in the bundle.
  26000. this.knownFileNameToModuleNames.set(filePath, metadata['importAs']);
  26001. }
  26002. // handle the symbols in one of the re-export location
  26003. if (metadata['exports']) {
  26004. for (const moduleExport of metadata['exports']) {
  26005. // handle the symbols in the list of explicitly re-exported symbols.
  26006. if (moduleExport.export) {
  26007. moduleExport.export.forEach((exportSymbol) => {
  26008. let symbolName;
  26009. if (typeof exportSymbol === 'string') {
  26010. symbolName = exportSymbol;
  26011. }
  26012. else {
  26013. symbolName = exportSymbol.as;
  26014. }
  26015. symbolName = unescapeIdentifier(symbolName);
  26016. let symName = symbolName;
  26017. if (typeof exportSymbol !== 'string') {
  26018. symName = unescapeIdentifier(exportSymbol.name);
  26019. }
  26020. const resolvedModule = this.resolveModule(moduleExport.from, filePath);
  26021. if (resolvedModule) {
  26022. const targetSymbol = this.getStaticSymbol(resolvedModule, symName);
  26023. const sourceSymbol = this.getStaticSymbol(filePath, symbolName);
  26024. resolvedSymbols.push(this.createExport(sourceSymbol, targetSymbol));
  26025. }
  26026. });
  26027. }
  26028. else {
  26029. // Handle the symbols loaded by 'export *' directives.
  26030. const resolvedModule = this.resolveModule(moduleExport.from, filePath);
  26031. if (resolvedModule && resolvedModule !== filePath) {
  26032. const nestedExports = this.getSymbolsOf(resolvedModule);
  26033. nestedExports.forEach((targetSymbol) => {
  26034. const sourceSymbol = this.getStaticSymbol(filePath, targetSymbol.name);
  26035. resolvedSymbols.push(this.createExport(sourceSymbol, targetSymbol));
  26036. });
  26037. }
  26038. }
  26039. }
  26040. }
  26041. // handle the actual metadata. Has to be after the exports
  26042. // as there might be collisions in the names, and we want the symbols
  26043. // of the current module to win ofter reexports.
  26044. if (metadata['metadata']) {
  26045. // handle direct declarations of the symbol
  26046. const topLevelSymbolNames = new Set(Object.keys(metadata['metadata']).map(unescapeIdentifier));
  26047. const origins = metadata['origins'] || {};
  26048. Object.keys(metadata['metadata']).forEach((metadataKey) => {
  26049. const symbolMeta = metadata['metadata'][metadataKey];
  26050. const name = unescapeIdentifier(metadataKey);
  26051. const symbol = this.getStaticSymbol(filePath, name);
  26052. const origin = origins.hasOwnProperty(metadataKey) && origins[metadataKey];
  26053. if (origin) {
  26054. // If the symbol is from a bundled index, use the declaration location of the
  26055. // symbol so relative references (such as './my.html') will be calculated
  26056. // correctly.
  26057. const originFilePath = this.resolveModule(origin, filePath);
  26058. if (!originFilePath) {
  26059. this.reportError(new Error(`Couldn't resolve original symbol for ${origin} from ${this.host.getOutputName(filePath)}`));
  26060. }
  26061. else {
  26062. this.symbolResourcePaths.set(symbol, originFilePath);
  26063. }
  26064. }
  26065. resolvedSymbols.push(this.createResolvedSymbol(symbol, filePath, topLevelSymbolNames, symbolMeta));
  26066. });
  26067. }
  26068. const uniqueSymbols = new Set();
  26069. for (const resolvedSymbol of resolvedSymbols) {
  26070. this.resolvedSymbols.set(resolvedSymbol.symbol, resolvedSymbol);
  26071. uniqueSymbols.add(resolvedSymbol.symbol);
  26072. }
  26073. this.symbolFromFile.set(filePath, Array.from(uniqueSymbols));
  26074. }
  26075. createResolvedSymbol(sourceSymbol, topLevelPath, topLevelSymbolNames, metadata) {
  26076. // For classes that don't have Angular summaries / metadata,
  26077. // we only keep their arity, but nothing else
  26078. // (e.g. their constructor parameters).
  26079. // We do this to prevent introducing deep imports
  26080. // as we didn't generate .ngfactory.ts files with proper reexports.
  26081. const isTsFile = TS.test(sourceSymbol.filePath);
  26082. if (this.summaryResolver.isLibraryFile(sourceSymbol.filePath) && !isTsFile && metadata &&
  26083. metadata['__symbolic'] === 'class') {
  26084. const transformedMeta = { __symbolic: 'class', arity: metadata.arity };
  26085. return new ResolvedStaticSymbol(sourceSymbol, transformedMeta);
  26086. }
  26087. let _originalFileMemo;
  26088. const getOriginalName = () => {
  26089. if (!_originalFileMemo) {
  26090. // Guess what the original file name is from the reference. If it has a `.d.ts` extension
  26091. // replace it with `.ts`. If it already has `.ts` just leave it in place. If it doesn't have
  26092. // .ts or .d.ts, append `.ts'. Also, if it is in `node_modules`, trim the `node_module`
  26093. // location as it is not important to finding the file.
  26094. _originalFileMemo =
  26095. this.host.getOutputName(topLevelPath.replace(/((\.ts)|(\.d\.ts)|)$/, '.ts')
  26096. .replace(/^.*node_modules[/\\]/, ''));
  26097. }
  26098. return _originalFileMemo;
  26099. };
  26100. const self = this;
  26101. class ReferenceTransformer extends ValueTransformer {
  26102. visitStringMap(map, functionParams) {
  26103. const symbolic = map['__symbolic'];
  26104. if (symbolic === 'function') {
  26105. const oldLen = functionParams.length;
  26106. functionParams.push(...(map['parameters'] || []));
  26107. const result = super.visitStringMap(map, functionParams);
  26108. functionParams.length = oldLen;
  26109. return result;
  26110. }
  26111. else if (symbolic === 'reference') {
  26112. const module = map['module'];
  26113. const name = map['name'] ? unescapeIdentifier(map['name']) : map['name'];
  26114. if (!name) {
  26115. return null;
  26116. }
  26117. let filePath;
  26118. if (module) {
  26119. filePath = self.resolveModule(module, sourceSymbol.filePath);
  26120. if (!filePath) {
  26121. return {
  26122. __symbolic: 'error',
  26123. message: `Could not resolve ${module} relative to ${self.host.getMetadataFor(sourceSymbol.filePath)}.`,
  26124. line: map['line'],
  26125. character: map['character'],
  26126. fileName: getOriginalName()
  26127. };
  26128. }
  26129. return {
  26130. __symbolic: 'resolved',
  26131. symbol: self.getStaticSymbol(filePath, name),
  26132. line: map['line'],
  26133. character: map['character'],
  26134. fileName: getOriginalName()
  26135. };
  26136. }
  26137. else if (functionParams.indexOf(name) >= 0) {
  26138. // reference to a function parameter
  26139. return { __symbolic: 'reference', name: name };
  26140. }
  26141. else {
  26142. if (topLevelSymbolNames.has(name)) {
  26143. return self.getStaticSymbol(topLevelPath, name);
  26144. }
  26145. // ambient value
  26146. null;
  26147. }
  26148. }
  26149. else if (symbolic === 'error') {
  26150. return Object.assign(Object.assign({}, map), { fileName: getOriginalName() });
  26151. }
  26152. else {
  26153. return super.visitStringMap(map, functionParams);
  26154. }
  26155. }
  26156. }
  26157. const transformedMeta = visitValue(metadata, new ReferenceTransformer(), []);
  26158. let unwrappedTransformedMeta = unwrapResolvedMetadata(transformedMeta);
  26159. if (unwrappedTransformedMeta instanceof StaticSymbol) {
  26160. return this.createExport(sourceSymbol, unwrappedTransformedMeta);
  26161. }
  26162. return new ResolvedStaticSymbol(sourceSymbol, transformedMeta);
  26163. }
  26164. createExport(sourceSymbol, targetSymbol) {
  26165. sourceSymbol.assertNoMembers();
  26166. targetSymbol.assertNoMembers();
  26167. if (this.summaryResolver.isLibraryFile(sourceSymbol.filePath) &&
  26168. this.summaryResolver.isLibraryFile(targetSymbol.filePath)) {
  26169. // This case is for an ng library importing symbols from a plain ts library
  26170. // transitively.
  26171. // Note: We rely on the fact that we discover symbols in the direction
  26172. // from source files to library files
  26173. this.importAs.set(targetSymbol, this.getImportAs(sourceSymbol) || sourceSymbol);
  26174. }
  26175. return new ResolvedStaticSymbol(sourceSymbol, targetSymbol);
  26176. }
  26177. reportError(error, context, path) {
  26178. if (this.errorRecorder) {
  26179. this.errorRecorder(error, (context && context.filePath) || path);
  26180. }
  26181. else {
  26182. throw error;
  26183. }
  26184. }
  26185. /**
  26186. * @param module an absolute path to a module file.
  26187. */
  26188. getModuleMetadata(module) {
  26189. let moduleMetadata = this.metadataCache.get(module);
  26190. if (!moduleMetadata) {
  26191. const moduleMetadatas = this.host.getMetadataFor(module);
  26192. if (moduleMetadatas) {
  26193. let maxVersion = -1;
  26194. moduleMetadatas.forEach((md) => {
  26195. if (md && md['version'] > maxVersion) {
  26196. maxVersion = md['version'];
  26197. moduleMetadata = md;
  26198. }
  26199. });
  26200. }
  26201. if (!moduleMetadata) {
  26202. moduleMetadata =
  26203. { __symbolic: 'module', version: SUPPORTED_SCHEMA_VERSION, module: module, metadata: {} };
  26204. }
  26205. if (moduleMetadata['version'] != SUPPORTED_SCHEMA_VERSION) {
  26206. const errorMessage = moduleMetadata['version'] == 2 ?
  26207. `Unsupported metadata version ${moduleMetadata['version']} for module ${module}. This module should be compiled with a newer version of ngc` :
  26208. `Metadata version mismatch for module ${this.host.getOutputName(module)}, found version ${moduleMetadata['version']}, expected ${SUPPORTED_SCHEMA_VERSION}`;
  26209. this.reportError(new Error(errorMessage));
  26210. }
  26211. this.metadataCache.set(module, moduleMetadata);
  26212. }
  26213. return moduleMetadata;
  26214. }
  26215. getSymbolByModule(module, symbolName, containingFile) {
  26216. const filePath = this.resolveModule(module, containingFile);
  26217. if (!filePath) {
  26218. this.reportError(new Error(`Could not resolve module ${module}${containingFile ? ' relative to ' + this.host.getOutputName(containingFile) : ''}`));
  26219. return this.getStaticSymbol(`ERROR:${module}`, symbolName);
  26220. }
  26221. return this.getStaticSymbol(filePath, symbolName);
  26222. }
  26223. resolveModule(module, containingFile) {
  26224. try {
  26225. return this.host.moduleNameToFileName(module, containingFile);
  26226. }
  26227. catch (e) {
  26228. console.error(`Could not resolve module '${module}' relative to file ${containingFile}`);
  26229. this.reportError(e, undefined, containingFile);
  26230. }
  26231. return null;
  26232. }
  26233. }
  26234. // Remove extra underscore from escaped identifier.
  26235. // See https://github.com/Microsoft/TypeScript/blob/master/src/compiler/utilities.ts
  26236. function unescapeIdentifier(identifier) {
  26237. return identifier.startsWith('___') ? identifier.substr(1) : identifier;
  26238. }
  26239. function unwrapResolvedMetadata(metadata) {
  26240. if (metadata && metadata.__symbolic === 'resolved') {
  26241. return metadata.symbol;
  26242. }
  26243. return metadata;
  26244. }
  26245. /**
  26246. * @license
  26247. * Copyright Google LLC All Rights Reserved.
  26248. *
  26249. * Use of this source code is governed by an MIT-style license that can be
  26250. * found in the LICENSE file at https://angular.io/license
  26251. */
  26252. function serializeSummaries(srcFileName, forJitCtx, summaryResolver, symbolResolver, symbols, types, createExternalSymbolReexports = false) {
  26253. const toJsonSerializer = new ToJsonSerializer(symbolResolver, summaryResolver, srcFileName);
  26254. // for symbols, we use everything except for the class metadata itself
  26255. // (we keep the statics though), as the class metadata is contained in the
  26256. // CompileTypeSummary.
  26257. symbols.forEach((resolvedSymbol) => toJsonSerializer.addSummary({ symbol: resolvedSymbol.symbol, metadata: resolvedSymbol.metadata }));
  26258. // Add type summaries.
  26259. types.forEach(({ summary, metadata }) => {
  26260. toJsonSerializer.addSummary({ symbol: summary.type.reference, metadata: undefined, type: summary });
  26261. });
  26262. const { json, exportAs } = toJsonSerializer.serialize(createExternalSymbolReexports);
  26263. if (forJitCtx) {
  26264. const forJitSerializer = new ForJitSerializer(forJitCtx, symbolResolver, summaryResolver);
  26265. types.forEach(({ summary, metadata }) => {
  26266. forJitSerializer.addSourceType(summary, metadata);
  26267. });
  26268. toJsonSerializer.unprocessedSymbolSummariesBySymbol.forEach((summary) => {
  26269. if (summaryResolver.isLibraryFile(summary.symbol.filePath) && summary.type) {
  26270. forJitSerializer.addLibType(summary.type);
  26271. }
  26272. });
  26273. forJitSerializer.serialize(exportAs);
  26274. }
  26275. return { json, exportAs };
  26276. }
  26277. function deserializeSummaries(symbolCache, summaryResolver, libraryFileName, json) {
  26278. const deserializer = new FromJsonDeserializer(symbolCache, summaryResolver);
  26279. return deserializer.deserialize(libraryFileName, json);
  26280. }
  26281. function createForJitStub(outputCtx, reference) {
  26282. return createSummaryForJitFunction(outputCtx, reference, NULL_EXPR);
  26283. }
  26284. function createSummaryForJitFunction(outputCtx, reference, value) {
  26285. const fnName = summaryForJitName(reference.name);
  26286. outputCtx.statements.push(fn([], [new ReturnStatement(value)], new ArrayType(DYNAMIC_TYPE)).toDeclStmt(fnName, [
  26287. StmtModifier.Final, StmtModifier.Exported
  26288. ]));
  26289. }
  26290. class ToJsonSerializer extends ValueTransformer {
  26291. constructor(symbolResolver, summaryResolver, srcFileName) {
  26292. super();
  26293. this.symbolResolver = symbolResolver;
  26294. this.summaryResolver = summaryResolver;
  26295. this.srcFileName = srcFileName;
  26296. // Note: This only contains symbols without members.
  26297. this.symbols = [];
  26298. this.indexBySymbol = new Map();
  26299. this.reexportedBy = new Map();
  26300. // This now contains a `__symbol: number` in the place of
  26301. // StaticSymbols, but otherwise has the same shape as the original objects.
  26302. this.processedSummaryBySymbol = new Map();
  26303. this.processedSummaries = [];
  26304. this.unprocessedSymbolSummariesBySymbol = new Map();
  26305. this.moduleName = symbolResolver.getKnownModuleName(srcFileName);
  26306. }
  26307. addSummary(summary) {
  26308. let unprocessedSummary = this.unprocessedSymbolSummariesBySymbol.get(summary.symbol);
  26309. let processedSummary = this.processedSummaryBySymbol.get(summary.symbol);
  26310. if (!unprocessedSummary) {
  26311. unprocessedSummary = { symbol: summary.symbol, metadata: undefined };
  26312. this.unprocessedSymbolSummariesBySymbol.set(summary.symbol, unprocessedSummary);
  26313. processedSummary = { symbol: this.processValue(summary.symbol, 0 /* None */) };
  26314. this.processedSummaries.push(processedSummary);
  26315. this.processedSummaryBySymbol.set(summary.symbol, processedSummary);
  26316. }
  26317. if (!unprocessedSummary.metadata && summary.metadata) {
  26318. let metadata = summary.metadata || {};
  26319. if (metadata.__symbolic === 'class') {
  26320. // For classes, we keep everything except their class decorators.
  26321. // We need to keep e.g. the ctor args, method names, method decorators
  26322. // so that the class can be extended in another compilation unit.
  26323. // We don't keep the class decorators as
  26324. // 1) they refer to data
  26325. // that should not cause a rebuild of downstream compilation units
  26326. // (e.g. inline templates of @Component, or @NgModule.declarations)
  26327. // 2) their data is already captured in TypeSummaries, e.g. DirectiveSummary.
  26328. const clone = {};
  26329. Object.keys(metadata).forEach((propName) => {
  26330. if (propName !== 'decorators') {
  26331. clone[propName] = metadata[propName];
  26332. }
  26333. });
  26334. metadata = clone;
  26335. }
  26336. else if (isCall(metadata)) {
  26337. if (!isFunctionCall(metadata) && !isMethodCallOnVariable(metadata)) {
  26338. // Don't store complex calls as we won't be able to simplify them anyways later on.
  26339. metadata = {
  26340. __symbolic: 'error',
  26341. message: 'Complex function calls are not supported.',
  26342. };
  26343. }
  26344. }
  26345. // Note: We need to keep storing ctor calls for e.g.
  26346. // `export const x = new InjectionToken(...)`
  26347. unprocessedSummary.metadata = metadata;
  26348. processedSummary.metadata = this.processValue(metadata, 1 /* ResolveValue */);
  26349. if (metadata instanceof StaticSymbol &&
  26350. this.summaryResolver.isLibraryFile(metadata.filePath)) {
  26351. const declarationSymbol = this.symbols[this.indexBySymbol.get(metadata)];
  26352. if (!isLoweredSymbol(declarationSymbol.name)) {
  26353. // Note: symbols that were introduced during codegen in the user file can have a reexport
  26354. // if a user used `export *`. However, we can't rely on this as tsickle will change
  26355. // `export *` into named exports, using only the information from the typechecker.
  26356. // As we introduce the new symbols after typecheck, Tsickle does not know about them,
  26357. // and omits them when expanding `export *`.
  26358. // So we have to keep reexporting these symbols manually via .ngfactory files.
  26359. this.reexportedBy.set(declarationSymbol, summary.symbol);
  26360. }
  26361. }
  26362. }
  26363. if (!unprocessedSummary.type && summary.type) {
  26364. unprocessedSummary.type = summary.type;
  26365. // Note: We don't add the summaries of all referenced symbols as for the ResolvedSymbols,
  26366. // as the type summaries already contain the transitive data that they require
  26367. // (in a minimal way).
  26368. processedSummary.type = this.processValue(summary.type, 0 /* None */);
  26369. // except for reexported directives / pipes, so we need to store
  26370. // their summaries explicitly.
  26371. if (summary.type.summaryKind === CompileSummaryKind.NgModule) {
  26372. const ngModuleSummary = summary.type;
  26373. ngModuleSummary.exportedDirectives.concat(ngModuleSummary.exportedPipes).forEach((id) => {
  26374. const symbol = id.reference;
  26375. if (this.summaryResolver.isLibraryFile(symbol.filePath) &&
  26376. !this.unprocessedSymbolSummariesBySymbol.has(symbol)) {
  26377. const summary = this.summaryResolver.resolveSummary(symbol);
  26378. if (summary) {
  26379. this.addSummary(summary);
  26380. }
  26381. }
  26382. });
  26383. }
  26384. }
  26385. }
  26386. /**
  26387. * @param createExternalSymbolReexports Whether external static symbols should be re-exported.
  26388. * This can be enabled if external symbols should be re-exported by the current module in
  26389. * order to avoid dynamically generated module dependencies which can break strict dependency
  26390. * enforcements (as in Google3). Read more here: https://github.com/angular/angular/issues/25644
  26391. */
  26392. serialize(createExternalSymbolReexports) {
  26393. const exportAs = [];
  26394. const json = JSON.stringify({
  26395. moduleName: this.moduleName,
  26396. summaries: this.processedSummaries,
  26397. symbols: this.symbols.map((symbol, index) => {
  26398. symbol.assertNoMembers();
  26399. let importAs = undefined;
  26400. if (this.summaryResolver.isLibraryFile(symbol.filePath)) {
  26401. const reexportSymbol = this.reexportedBy.get(symbol);
  26402. if (reexportSymbol) {
  26403. // In case the given external static symbol is already manually exported by the
  26404. // user, we just proxy the external static symbol reference to the manual export.
  26405. // This ensures that the AOT compiler imports the external symbol through the
  26406. // user export and does not introduce another dependency which is not needed.
  26407. importAs = this.indexBySymbol.get(reexportSymbol);
  26408. }
  26409. else if (createExternalSymbolReexports) {
  26410. // In this case, the given external static symbol is *not* manually exported by
  26411. // the user, and we manually create a re-export in the factory file so that we
  26412. // don't introduce another module dependency. This is useful when running within
  26413. // Bazel so that the AOT compiler does not introduce any module dependencies
  26414. // which can break the strict dependency enforcement. (e.g. as in Google3)
  26415. // Read more about this here: https://github.com/angular/angular/issues/25644
  26416. const summary = this.unprocessedSymbolSummariesBySymbol.get(symbol);
  26417. if (!summary || !summary.metadata || summary.metadata.__symbolic !== 'interface') {
  26418. importAs = `${symbol.name}_${index}`;
  26419. exportAs.push({ symbol, exportAs: importAs });
  26420. }
  26421. }
  26422. }
  26423. return {
  26424. __symbol: index,
  26425. name: symbol.name,
  26426. filePath: this.summaryResolver.toSummaryFileName(symbol.filePath, this.srcFileName),
  26427. importAs: importAs
  26428. };
  26429. })
  26430. });
  26431. return { json, exportAs };
  26432. }
  26433. processValue(value, flags) {
  26434. return visitValue(value, this, flags);
  26435. }
  26436. visitOther(value, context) {
  26437. if (value instanceof StaticSymbol) {
  26438. let baseSymbol = this.symbolResolver.getStaticSymbol(value.filePath, value.name);
  26439. const index = this.visitStaticSymbol(baseSymbol, context);
  26440. return { __symbol: index, members: value.members };
  26441. }
  26442. }
  26443. /**
  26444. * Strip line and character numbers from ngsummaries.
  26445. * Emitting them causes white spaces changes to retrigger upstream
  26446. * recompilations in bazel.
  26447. * TODO: find out a way to have line and character numbers in errors without
  26448. * excessive recompilation in bazel.
  26449. */
  26450. visitStringMap(map, context) {
  26451. if (map['__symbolic'] === 'resolved') {
  26452. return visitValue(map['symbol'], this, context);
  26453. }
  26454. if (map['__symbolic'] === 'error') {
  26455. delete map['line'];
  26456. delete map['character'];
  26457. }
  26458. return super.visitStringMap(map, context);
  26459. }
  26460. /**
  26461. * Returns null if the options.resolveValue is true, and the summary for the symbol
  26462. * resolved to a type or could not be resolved.
  26463. */
  26464. visitStaticSymbol(baseSymbol, flags) {
  26465. let index = this.indexBySymbol.get(baseSymbol);
  26466. let summary = null;
  26467. if (flags & 1 /* ResolveValue */ &&
  26468. this.summaryResolver.isLibraryFile(baseSymbol.filePath)) {
  26469. if (this.unprocessedSymbolSummariesBySymbol.has(baseSymbol)) {
  26470. // the summary for this symbol was already added
  26471. // -> nothing to do.
  26472. return index;
  26473. }
  26474. summary = this.loadSummary(baseSymbol);
  26475. if (summary && summary.metadata instanceof StaticSymbol) {
  26476. // The summary is a reexport
  26477. index = this.visitStaticSymbol(summary.metadata, flags);
  26478. // reset the summary as it is just a reexport, so we don't want to store it.
  26479. summary = null;
  26480. }
  26481. }
  26482. else if (index != null) {
  26483. // Note: == on purpose to compare with undefined!
  26484. // No summary and the symbol is already added -> nothing to do.
  26485. return index;
  26486. }
  26487. // Note: == on purpose to compare with undefined!
  26488. if (index == null) {
  26489. index = this.symbols.length;
  26490. this.symbols.push(baseSymbol);
  26491. }
  26492. this.indexBySymbol.set(baseSymbol, index);
  26493. if (summary) {
  26494. this.addSummary(summary);
  26495. }
  26496. return index;
  26497. }
  26498. loadSummary(symbol) {
  26499. let summary = this.summaryResolver.resolveSummary(symbol);
  26500. if (!summary) {
  26501. // some symbols might originate from a plain typescript library
  26502. // that just exported .d.ts and .metadata.json files, i.e. where no summary
  26503. // files were created.
  26504. const resolvedSymbol = this.symbolResolver.resolveSymbol(symbol);
  26505. if (resolvedSymbol) {
  26506. summary = { symbol: resolvedSymbol.symbol, metadata: resolvedSymbol.metadata };
  26507. }
  26508. }
  26509. return summary;
  26510. }
  26511. }
  26512. class ForJitSerializer {
  26513. constructor(outputCtx, symbolResolver, summaryResolver) {
  26514. this.outputCtx = outputCtx;
  26515. this.symbolResolver = symbolResolver;
  26516. this.summaryResolver = summaryResolver;
  26517. this.data = [];
  26518. }
  26519. addSourceType(summary, metadata) {
  26520. this.data.push({ summary, metadata, isLibrary: false });
  26521. }
  26522. addLibType(summary) {
  26523. this.data.push({ summary, metadata: null, isLibrary: true });
  26524. }
  26525. serialize(exportAsArr) {
  26526. const exportAsBySymbol = new Map();
  26527. for (const { symbol, exportAs } of exportAsArr) {
  26528. exportAsBySymbol.set(symbol, exportAs);
  26529. }
  26530. const ngModuleSymbols = new Set();
  26531. for (const { summary, metadata, isLibrary } of this.data) {
  26532. if (summary.summaryKind === CompileSummaryKind.NgModule) {
  26533. // collect the symbols that refer to NgModule classes.
  26534. // Note: we can't just rely on `summary.type.summaryKind` to determine this as
  26535. // we don't add the summaries of all referenced symbols when we serialize type summaries.
  26536. // See serializeSummaries for details.
  26537. ngModuleSymbols.add(summary.type.reference);
  26538. const modSummary = summary;
  26539. for (const mod of modSummary.modules) {
  26540. ngModuleSymbols.add(mod.reference);
  26541. }
  26542. }
  26543. if (!isLibrary) {
  26544. const fnName = summaryForJitName(summary.type.reference.name);
  26545. createSummaryForJitFunction(this.outputCtx, summary.type.reference, this.serializeSummaryWithDeps(summary, metadata));
  26546. }
  26547. }
  26548. ngModuleSymbols.forEach((ngModuleSymbol) => {
  26549. if (this.summaryResolver.isLibraryFile(ngModuleSymbol.filePath)) {
  26550. let exportAs = exportAsBySymbol.get(ngModuleSymbol) || ngModuleSymbol.name;
  26551. const jitExportAsName = summaryForJitName(exportAs);
  26552. this.outputCtx.statements.push(variable(jitExportAsName)
  26553. .set(this.serializeSummaryRef(ngModuleSymbol))
  26554. .toDeclStmt(null, [StmtModifier.Exported]));
  26555. }
  26556. });
  26557. }
  26558. serializeSummaryWithDeps(summary, metadata) {
  26559. const expressions = [this.serializeSummary(summary)];
  26560. let providers = [];
  26561. if (metadata instanceof CompileNgModuleMetadata) {
  26562. expressions.push(...
  26563. // For directives / pipes, we only add the declared ones,
  26564. // and rely on transitively importing NgModules to get the transitive
  26565. // summaries.
  26566. metadata.declaredDirectives.concat(metadata.declaredPipes)
  26567. .map(type => type.reference)
  26568. // For modules,
  26569. // we also add the summaries for modules
  26570. // from libraries.
  26571. // This is ok as we produce reexports for all transitive modules.
  26572. .concat(metadata.transitiveModule.modules.map(type => type.reference)
  26573. .filter(ref => ref !== metadata.type.reference))
  26574. .map((ref) => this.serializeSummaryRef(ref)));
  26575. // Note: We don't use `NgModuleSummary.providers`, as that one is transitive,
  26576. // and we already have transitive modules.
  26577. providers = metadata.providers;
  26578. }
  26579. else if (summary.summaryKind === CompileSummaryKind.Directive) {
  26580. const dirSummary = summary;
  26581. providers = dirSummary.providers.concat(dirSummary.viewProviders);
  26582. }
  26583. // Note: We can't just refer to the `ngsummary.ts` files for `useClass` providers (as we do for
  26584. // declaredDirectives / declaredPipes), as we allow
  26585. // providers without ctor arguments to skip the `@Injectable` decorator,
  26586. // i.e. we didn't generate .ngsummary.ts files for these.
  26587. expressions.push(...providers.filter(provider => !!provider.useClass).map(provider => this.serializeSummary({
  26588. summaryKind: CompileSummaryKind.Injectable,
  26589. type: provider.useClass
  26590. })));
  26591. return literalArr(expressions);
  26592. }
  26593. serializeSummaryRef(typeSymbol) {
  26594. const jitImportedSymbol = this.symbolResolver.getStaticSymbol(summaryForJitFileName(typeSymbol.filePath), summaryForJitName(typeSymbol.name));
  26595. return this.outputCtx.importExpr(jitImportedSymbol);
  26596. }
  26597. serializeSummary(data) {
  26598. const outputCtx = this.outputCtx;
  26599. class Transformer {
  26600. visitArray(arr, context) {
  26601. return literalArr(arr.map(entry => visitValue(entry, this, context)));
  26602. }
  26603. visitStringMap(map, context) {
  26604. return new LiteralMapExpr(Object.keys(map).map((key) => new LiteralMapEntry(key, visitValue(map[key], this, context), false)));
  26605. }
  26606. visitPrimitive(value, context) {
  26607. return literal(value);
  26608. }
  26609. visitOther(value, context) {
  26610. if (value instanceof StaticSymbol) {
  26611. return outputCtx.importExpr(value);
  26612. }
  26613. else {
  26614. throw new Error(`Illegal State: Encountered value ${value}`);
  26615. }
  26616. }
  26617. }
  26618. return visitValue(data, new Transformer(), null);
  26619. }
  26620. }
  26621. class FromJsonDeserializer extends ValueTransformer {
  26622. constructor(symbolCache, summaryResolver) {
  26623. super();
  26624. this.symbolCache = symbolCache;
  26625. this.summaryResolver = summaryResolver;
  26626. }
  26627. deserialize(libraryFileName, json) {
  26628. const data = JSON.parse(json);
  26629. const allImportAs = [];
  26630. this.symbols = data.symbols.map((serializedSymbol) => this.symbolCache.get(this.summaryResolver.fromSummaryFileName(serializedSymbol.filePath, libraryFileName), serializedSymbol.name));
  26631. data.symbols.forEach((serializedSymbol, index) => {
  26632. const symbol = this.symbols[index];
  26633. const importAs = serializedSymbol.importAs;
  26634. if (typeof importAs === 'number') {
  26635. allImportAs.push({ symbol, importAs: this.symbols[importAs] });
  26636. }
  26637. else if (typeof importAs === 'string') {
  26638. allImportAs.push({ symbol, importAs: this.symbolCache.get(ngfactoryFilePath(libraryFileName), importAs) });
  26639. }
  26640. });
  26641. const summaries = visitValue(data.summaries, this, null);
  26642. return { moduleName: data.moduleName, summaries, importAs: allImportAs };
  26643. }
  26644. visitStringMap(map, context) {
  26645. if ('__symbol' in map) {
  26646. const baseSymbol = this.symbols[map['__symbol']];
  26647. const members = map['members'];
  26648. return members.length ? this.symbolCache.get(baseSymbol.filePath, baseSymbol.name, members) :
  26649. baseSymbol;
  26650. }
  26651. else {
  26652. return super.visitStringMap(map, context);
  26653. }
  26654. }
  26655. }
  26656. function isCall(metadata) {
  26657. return metadata && metadata.__symbolic === 'call';
  26658. }
  26659. function isFunctionCall(metadata) {
  26660. return isCall(metadata) && unwrapResolvedMetadata(metadata.expression) instanceof StaticSymbol;
  26661. }
  26662. function isMethodCallOnVariable(metadata) {
  26663. return isCall(metadata) && metadata.expression && metadata.expression.__symbolic === 'select' &&
  26664. unwrapResolvedMetadata(metadata.expression.expression) instanceof StaticSymbol;
  26665. }
  26666. /**
  26667. * @license
  26668. * Copyright Google LLC All Rights Reserved.
  26669. *
  26670. * Use of this source code is governed by an MIT-style license that can be
  26671. * found in the LICENSE file at https://angular.io/license
  26672. */
  26673. class AotCompiler {
  26674. constructor(_config, _options, _host, reflector, _metadataResolver, _templateParser, _styleCompiler, _viewCompiler, _typeCheckCompiler, _ngModuleCompiler, _injectableCompiler, _outputEmitter, _summaryResolver, _symbolResolver) {
  26675. this._config = _config;
  26676. this._options = _options;
  26677. this._host = _host;
  26678. this.reflector = reflector;
  26679. this._metadataResolver = _metadataResolver;
  26680. this._templateParser = _templateParser;
  26681. this._styleCompiler = _styleCompiler;
  26682. this._viewCompiler = _viewCompiler;
  26683. this._typeCheckCompiler = _typeCheckCompiler;
  26684. this._ngModuleCompiler = _ngModuleCompiler;
  26685. this._injectableCompiler = _injectableCompiler;
  26686. this._outputEmitter = _outputEmitter;
  26687. this._summaryResolver = _summaryResolver;
  26688. this._symbolResolver = _symbolResolver;
  26689. this._templateAstCache = new Map();
  26690. this._analyzedFiles = new Map();
  26691. this._analyzedFilesForInjectables = new Map();
  26692. }
  26693. clearCache() {
  26694. this._metadataResolver.clearCache();
  26695. }
  26696. analyzeModulesSync(rootFiles) {
  26697. const analyzeResult = analyzeAndValidateNgModules(rootFiles, this._host, this._symbolResolver, this._metadataResolver);
  26698. analyzeResult.ngModules.forEach(ngModule => this._metadataResolver.loadNgModuleDirectiveAndPipeMetadata(ngModule.type.reference, true));
  26699. return analyzeResult;
  26700. }
  26701. analyzeModulesAsync(rootFiles) {
  26702. const analyzeResult = analyzeAndValidateNgModules(rootFiles, this._host, this._symbolResolver, this._metadataResolver);
  26703. return Promise
  26704. .all(analyzeResult.ngModules.map(ngModule => this._metadataResolver.loadNgModuleDirectiveAndPipeMetadata(ngModule.type.reference, false)))
  26705. .then(() => analyzeResult);
  26706. }
  26707. _analyzeFile(fileName) {
  26708. let analyzedFile = this._analyzedFiles.get(fileName);
  26709. if (!analyzedFile) {
  26710. analyzedFile =
  26711. analyzeFile(this._host, this._symbolResolver, this._metadataResolver, fileName);
  26712. this._analyzedFiles.set(fileName, analyzedFile);
  26713. }
  26714. return analyzedFile;
  26715. }
  26716. _analyzeFileForInjectables(fileName) {
  26717. let analyzedFile = this._analyzedFilesForInjectables.get(fileName);
  26718. if (!analyzedFile) {
  26719. analyzedFile = analyzeFileForInjectables(this._host, this._symbolResolver, this._metadataResolver, fileName);
  26720. this._analyzedFilesForInjectables.set(fileName, analyzedFile);
  26721. }
  26722. return analyzedFile;
  26723. }
  26724. findGeneratedFileNames(fileName) {
  26725. const genFileNames = [];
  26726. const file = this._analyzeFile(fileName);
  26727. // Make sure we create a .ngfactory if we have a injectable/directive/pipe/NgModule
  26728. // or a reference to a non source file.
  26729. // Note: This is overestimating the required .ngfactory files as the real calculation is harder.
  26730. // Only do this for StubEmitFlags.Basic, as adding a type check block
  26731. // does not change this file (as we generate type check blocks based on NgModules).
  26732. if (this._options.allowEmptyCodegenFiles || file.directives.length || file.pipes.length ||
  26733. file.injectables.length || file.ngModules.length || file.exportsNonSourceFiles) {
  26734. genFileNames.push(ngfactoryFilePath(file.fileName, true));
  26735. if (this._options.enableSummariesForJit) {
  26736. genFileNames.push(summaryForJitFileName(file.fileName, true));
  26737. }
  26738. }
  26739. const fileSuffix = normalizeGenFileSuffix(splitTypescriptSuffix(file.fileName, true)[1]);
  26740. file.directives.forEach((dirSymbol) => {
  26741. const compMeta = this._metadataResolver.getNonNormalizedDirectiveMetadata(dirSymbol).metadata;
  26742. if (!compMeta.isComponent) {
  26743. return;
  26744. }
  26745. // Note: compMeta is a component and therefore template is non null.
  26746. compMeta.template.styleUrls.forEach((styleUrl) => {
  26747. const normalizedUrl = this._host.resourceNameToFileName(styleUrl, file.fileName);
  26748. if (!normalizedUrl) {
  26749. throw syntaxError(`Couldn't resolve resource ${styleUrl} relative to ${file.fileName}`);
  26750. }
  26751. const needsShim = (compMeta.template.encapsulation ||
  26752. this._config.defaultEncapsulation) === ViewEncapsulation.Emulated;
  26753. genFileNames.push(_stylesModuleUrl(normalizedUrl, needsShim, fileSuffix));
  26754. if (this._options.allowEmptyCodegenFiles) {
  26755. genFileNames.push(_stylesModuleUrl(normalizedUrl, !needsShim, fileSuffix));
  26756. }
  26757. });
  26758. });
  26759. return genFileNames;
  26760. }
  26761. emitBasicStub(genFileName, originalFileName) {
  26762. const outputCtx = this._createOutputContext(genFileName);
  26763. if (genFileName.endsWith('.ngfactory.ts')) {
  26764. if (!originalFileName) {
  26765. throw new Error(`Assertion error: require the original file for .ngfactory.ts stubs. File: ${genFileName}`);
  26766. }
  26767. const originalFile = this._analyzeFile(originalFileName);
  26768. this._createNgFactoryStub(outputCtx, originalFile, 1 /* Basic */);
  26769. }
  26770. else if (genFileName.endsWith('.ngsummary.ts')) {
  26771. if (this._options.enableSummariesForJit) {
  26772. if (!originalFileName) {
  26773. throw new Error(`Assertion error: require the original file for .ngsummary.ts stubs. File: ${genFileName}`);
  26774. }
  26775. const originalFile = this._analyzeFile(originalFileName);
  26776. _createEmptyStub(outputCtx);
  26777. originalFile.ngModules.forEach(ngModule => {
  26778. // create exports that user code can reference
  26779. createForJitStub(outputCtx, ngModule.type.reference);
  26780. });
  26781. }
  26782. }
  26783. else if (genFileName.endsWith('.ngstyle.ts')) {
  26784. _createEmptyStub(outputCtx);
  26785. }
  26786. // Note: for the stubs, we don't need a property srcFileUrl,
  26787. // as later on in emitAllImpls we will create the proper GeneratedFiles with the
  26788. // correct srcFileUrl.
  26789. // This is good as e.g. for .ngstyle.ts files we can't derive
  26790. // the url of components based on the genFileUrl.
  26791. return this._codegenSourceModule('unknown', outputCtx);
  26792. }
  26793. emitTypeCheckStub(genFileName, originalFileName) {
  26794. const originalFile = this._analyzeFile(originalFileName);
  26795. const outputCtx = this._createOutputContext(genFileName);
  26796. if (genFileName.endsWith('.ngfactory.ts')) {
  26797. this._createNgFactoryStub(outputCtx, originalFile, 2 /* TypeCheck */);
  26798. }
  26799. return outputCtx.statements.length > 0 ?
  26800. this._codegenSourceModule(originalFile.fileName, outputCtx) :
  26801. null;
  26802. }
  26803. loadFilesAsync(fileNames, tsFiles) {
  26804. const files = fileNames.map(fileName => this._analyzeFile(fileName));
  26805. const loadingPromises = [];
  26806. files.forEach(file => file.ngModules.forEach(ngModule => loadingPromises.push(this._metadataResolver.loadNgModuleDirectiveAndPipeMetadata(ngModule.type.reference, false))));
  26807. const analyzedInjectables = tsFiles.map(tsFile => this._analyzeFileForInjectables(tsFile));
  26808. return Promise.all(loadingPromises).then(_ => ({
  26809. analyzedModules: mergeAndValidateNgFiles(files),
  26810. analyzedInjectables: analyzedInjectables,
  26811. }));
  26812. }
  26813. loadFilesSync(fileNames, tsFiles) {
  26814. const files = fileNames.map(fileName => this._analyzeFile(fileName));
  26815. files.forEach(file => file.ngModules.forEach(ngModule => this._metadataResolver.loadNgModuleDirectiveAndPipeMetadata(ngModule.type.reference, true)));
  26816. const analyzedInjectables = tsFiles.map(tsFile => this._analyzeFileForInjectables(tsFile));
  26817. return {
  26818. analyzedModules: mergeAndValidateNgFiles(files),
  26819. analyzedInjectables: analyzedInjectables,
  26820. };
  26821. }
  26822. _createNgFactoryStub(outputCtx, file, emitFlags) {
  26823. let componentId = 0;
  26824. file.ngModules.forEach((ngModuleMeta, ngModuleIndex) => {
  26825. // Note: the code below needs to executed for StubEmitFlags.Basic and StubEmitFlags.TypeCheck,
  26826. // so we don't change the .ngfactory file too much when adding the type-check block.
  26827. // create exports that user code can reference
  26828. this._ngModuleCompiler.createStub(outputCtx, ngModuleMeta.type.reference);
  26829. // add references to the symbols from the metadata.
  26830. // These can be used by the type check block for components,
  26831. // and they also cause TypeScript to include these files into the program too,
  26832. // which will make them part of the analyzedFiles.
  26833. const externalReferences = [
  26834. // Add references that are available from all the modules and imports.
  26835. ...ngModuleMeta.transitiveModule.directives.map(d => d.reference),
  26836. ...ngModuleMeta.transitiveModule.pipes.map(d => d.reference),
  26837. ...ngModuleMeta.importedModules.map(m => m.type.reference),
  26838. ...ngModuleMeta.exportedModules.map(m => m.type.reference),
  26839. // Add references that might be inserted by the template compiler.
  26840. ...this._externalIdentifierReferences([Identifiers$1.TemplateRef, Identifiers$1.ElementRef]),
  26841. ];
  26842. const externalReferenceVars = new Map();
  26843. externalReferences.forEach((ref, typeIndex) => {
  26844. externalReferenceVars.set(ref, `_decl${ngModuleIndex}_${typeIndex}`);
  26845. });
  26846. externalReferenceVars.forEach((varName, reference) => {
  26847. outputCtx.statements.push(variable(varName)
  26848. .set(NULL_EXPR.cast(DYNAMIC_TYPE))
  26849. .toDeclStmt(expressionType(outputCtx.importExpr(reference, /* typeParams */ null, /* useSummaries */ false))));
  26850. });
  26851. if (emitFlags & 2 /* TypeCheck */) {
  26852. // add the type-check block for all components of the NgModule
  26853. ngModuleMeta.declaredDirectives.forEach((dirId) => {
  26854. const compMeta = this._metadataResolver.getDirectiveMetadata(dirId.reference);
  26855. if (!compMeta.isComponent) {
  26856. return;
  26857. }
  26858. componentId++;
  26859. this._createTypeCheckBlock(outputCtx, `${compMeta.type.reference.name}_Host_${componentId}`, ngModuleMeta, this._metadataResolver.getHostComponentMetadata(compMeta), [compMeta.type], externalReferenceVars);
  26860. this._createTypeCheckBlock(outputCtx, `${compMeta.type.reference.name}_${componentId}`, ngModuleMeta, compMeta, ngModuleMeta.transitiveModule.directives, externalReferenceVars);
  26861. });
  26862. }
  26863. });
  26864. if (outputCtx.statements.length === 0) {
  26865. _createEmptyStub(outputCtx);
  26866. }
  26867. }
  26868. _externalIdentifierReferences(references) {
  26869. const result = [];
  26870. for (let reference of references) {
  26871. const token = createTokenForExternalReference(this.reflector, reference);
  26872. if (token.identifier) {
  26873. result.push(token.identifier.reference);
  26874. }
  26875. }
  26876. return result;
  26877. }
  26878. _createTypeCheckBlock(ctx, componentId, moduleMeta, compMeta, directives, externalReferenceVars) {
  26879. const { template: parsedTemplate, pipes: usedPipes } = this._parseTemplate(compMeta, moduleMeta, directives);
  26880. ctx.statements.push(...this._typeCheckCompiler.compileComponent(componentId, compMeta, parsedTemplate, usedPipes, externalReferenceVars, ctx));
  26881. }
  26882. emitMessageBundle(analyzeResult, locale) {
  26883. const errors = [];
  26884. const htmlParser = new HtmlParser();
  26885. // TODO(vicb): implicit tags & attributes
  26886. const messageBundle = new MessageBundle(htmlParser, [], {}, locale);
  26887. analyzeResult.files.forEach(file => {
  26888. const compMetas = [];
  26889. file.directives.forEach(directiveType => {
  26890. const dirMeta = this._metadataResolver.getDirectiveMetadata(directiveType);
  26891. if (dirMeta && dirMeta.isComponent) {
  26892. compMetas.push(dirMeta);
  26893. }
  26894. });
  26895. compMetas.forEach(compMeta => {
  26896. const html = compMeta.template.template;
  26897. // Template URL points to either an HTML or TS file depending on whether
  26898. // the file is used with `templateUrl:` or `template:`, respectively.
  26899. const templateUrl = compMeta.template.templateUrl;
  26900. const interpolationConfig = InterpolationConfig.fromArray(compMeta.template.interpolation);
  26901. errors.push(...messageBundle.updateFromTemplate(html, templateUrl, interpolationConfig));
  26902. });
  26903. });
  26904. if (errors.length) {
  26905. throw new Error(errors.map(e => e.toString()).join('\n'));
  26906. }
  26907. return messageBundle;
  26908. }
  26909. emitAllPartialModules2(files) {
  26910. // Using reduce like this is a select many pattern (where map is a select pattern)
  26911. return files.reduce((r, file) => {
  26912. r.push(...this._emitPartialModule2(file.fileName, file.injectables));
  26913. return r;
  26914. }, []);
  26915. }
  26916. _emitPartialModule2(fileName, injectables) {
  26917. const context = this._createOutputContext(fileName);
  26918. injectables.forEach(injectable => this._injectableCompiler.compile(injectable, context));
  26919. if (context.statements && context.statements.length > 0) {
  26920. return [{ fileName, statements: [...context.constantPool.statements, ...context.statements] }];
  26921. }
  26922. return [];
  26923. }
  26924. emitAllImpls(analyzeResult) {
  26925. const { ngModuleByPipeOrDirective, files } = analyzeResult;
  26926. const sourceModules = files.map(file => this._compileImplFile(file.fileName, ngModuleByPipeOrDirective, file.directives, file.pipes, file.ngModules, file.injectables));
  26927. return flatten(sourceModules);
  26928. }
  26929. _compileImplFile(srcFileUrl, ngModuleByPipeOrDirective, directives, pipes, ngModules, injectables) {
  26930. const fileSuffix = normalizeGenFileSuffix(splitTypescriptSuffix(srcFileUrl, true)[1]);
  26931. const generatedFiles = [];
  26932. const outputCtx = this._createOutputContext(ngfactoryFilePath(srcFileUrl, true));
  26933. generatedFiles.push(...this._createSummary(srcFileUrl, directives, pipes, ngModules, injectables, outputCtx));
  26934. // compile all ng modules
  26935. ngModules.forEach((ngModuleMeta) => this._compileModule(outputCtx, ngModuleMeta));
  26936. // compile components
  26937. directives.forEach((dirType) => {
  26938. const compMeta = this._metadataResolver.getDirectiveMetadata(dirType);
  26939. if (!compMeta.isComponent) {
  26940. return;
  26941. }
  26942. const ngModule = ngModuleByPipeOrDirective.get(dirType);
  26943. if (!ngModule) {
  26944. throw new Error(`Internal Error: cannot determine the module for component ${identifierName(compMeta.type)}!`);
  26945. }
  26946. // compile styles
  26947. const componentStylesheet = this._styleCompiler.compileComponent(outputCtx, compMeta);
  26948. // Note: compMeta is a component and therefore template is non null.
  26949. compMeta.template.externalStylesheets.forEach((stylesheetMeta) => {
  26950. // Note: fill non shim and shim style files as they might
  26951. // be shared by component with and without ViewEncapsulation.
  26952. const shim = this._styleCompiler.needsStyleShim(compMeta);
  26953. generatedFiles.push(this._codegenStyles(srcFileUrl, compMeta, stylesheetMeta, shim, fileSuffix));
  26954. if (this._options.allowEmptyCodegenFiles) {
  26955. generatedFiles.push(this._codegenStyles(srcFileUrl, compMeta, stylesheetMeta, !shim, fileSuffix));
  26956. }
  26957. });
  26958. // compile components
  26959. const compViewVars = this._compileComponent(outputCtx, compMeta, ngModule, ngModule.transitiveModule.directives, componentStylesheet, fileSuffix);
  26960. this._compileComponentFactory(outputCtx, compMeta, ngModule, fileSuffix);
  26961. });
  26962. if (outputCtx.statements.length > 0 || this._options.allowEmptyCodegenFiles) {
  26963. const srcModule = this._codegenSourceModule(srcFileUrl, outputCtx);
  26964. generatedFiles.unshift(srcModule);
  26965. }
  26966. return generatedFiles;
  26967. }
  26968. _createSummary(srcFileName, directives, pipes, ngModules, injectables, ngFactoryCtx) {
  26969. const symbolSummaries = this._symbolResolver.getSymbolsOf(srcFileName)
  26970. .map(symbol => this._symbolResolver.resolveSymbol(symbol));
  26971. const typeData = [
  26972. ...ngModules.map(meta => ({
  26973. summary: this._metadataResolver.getNgModuleSummary(meta.type.reference),
  26974. metadata: this._metadataResolver.getNgModuleMetadata(meta.type.reference)
  26975. })),
  26976. ...directives.map(ref => ({
  26977. summary: this._metadataResolver.getDirectiveSummary(ref),
  26978. metadata: this._metadataResolver.getDirectiveMetadata(ref)
  26979. })),
  26980. ...pipes.map(ref => ({
  26981. summary: this._metadataResolver.getPipeSummary(ref),
  26982. metadata: this._metadataResolver.getPipeMetadata(ref)
  26983. })),
  26984. ...injectables.map(ref => ({
  26985. summary: this._metadataResolver.getInjectableSummary(ref.symbol),
  26986. metadata: this._metadataResolver.getInjectableSummary(ref.symbol).type
  26987. }))
  26988. ];
  26989. const forJitOutputCtx = this._options.enableSummariesForJit ?
  26990. this._createOutputContext(summaryForJitFileName(srcFileName, true)) :
  26991. null;
  26992. const { json, exportAs } = serializeSummaries(srcFileName, forJitOutputCtx, this._summaryResolver, this._symbolResolver, symbolSummaries, typeData, this._options.createExternalSymbolFactoryReexports);
  26993. exportAs.forEach((entry) => {
  26994. ngFactoryCtx.statements.push(variable(entry.exportAs).set(ngFactoryCtx.importExpr(entry.symbol)).toDeclStmt(null, [
  26995. StmtModifier.Exported
  26996. ]));
  26997. });
  26998. const summaryJson = new GeneratedFile(srcFileName, summaryFileName(srcFileName), json);
  26999. const result = [summaryJson];
  27000. if (forJitOutputCtx) {
  27001. result.push(this._codegenSourceModule(srcFileName, forJitOutputCtx));
  27002. }
  27003. return result;
  27004. }
  27005. _compileModule(outputCtx, ngModule) {
  27006. const providers = [];
  27007. if (this._options.locale) {
  27008. const normalizedLocale = this._options.locale.replace(/_/g, '-');
  27009. providers.push({
  27010. token: createTokenForExternalReference(this.reflector, Identifiers$1.LOCALE_ID),
  27011. useValue: normalizedLocale,
  27012. });
  27013. }
  27014. if (this._options.i18nFormat) {
  27015. providers.push({
  27016. token: createTokenForExternalReference(this.reflector, Identifiers$1.TRANSLATIONS_FORMAT),
  27017. useValue: this._options.i18nFormat
  27018. });
  27019. }
  27020. this._ngModuleCompiler.compile(outputCtx, ngModule, providers);
  27021. }
  27022. _compileComponentFactory(outputCtx, compMeta, ngModule, fileSuffix) {
  27023. const hostMeta = this._metadataResolver.getHostComponentMetadata(compMeta);
  27024. const hostViewFactoryVar = this._compileComponent(outputCtx, hostMeta, ngModule, [compMeta.type], null, fileSuffix)
  27025. .viewClassVar;
  27026. const compFactoryVar = componentFactoryName(compMeta.type.reference);
  27027. const inputsExprs = [];
  27028. for (let propName in compMeta.inputs) {
  27029. const templateName = compMeta.inputs[propName];
  27030. // Don't quote so that the key gets minified...
  27031. inputsExprs.push(new LiteralMapEntry(propName, literal(templateName), false));
  27032. }
  27033. const outputsExprs = [];
  27034. for (let propName in compMeta.outputs) {
  27035. const templateName = compMeta.outputs[propName];
  27036. // Don't quote so that the key gets minified...
  27037. outputsExprs.push(new LiteralMapEntry(propName, literal(templateName), false));
  27038. }
  27039. outputCtx.statements.push(variable(compFactoryVar)
  27040. .set(importExpr(Identifiers$1.createComponentFactory).callFn([
  27041. literal(compMeta.selector), outputCtx.importExpr(compMeta.type.reference),
  27042. variable(hostViewFactoryVar), new LiteralMapExpr(inputsExprs),
  27043. new LiteralMapExpr(outputsExprs),
  27044. literalArr(compMeta.template.ngContentSelectors.map(selector => literal(selector)))
  27045. ]))
  27046. .toDeclStmt(importType(Identifiers$1.ComponentFactory, [expressionType(outputCtx.importExpr(compMeta.type.reference))], [TypeModifier.Const]), [StmtModifier.Final, StmtModifier.Exported]));
  27047. }
  27048. _compileComponent(outputCtx, compMeta, ngModule, directiveIdentifiers, componentStyles, fileSuffix) {
  27049. const { template: parsedTemplate, pipes: usedPipes } = this._parseTemplate(compMeta, ngModule, directiveIdentifiers);
  27050. const stylesExpr = componentStyles ? variable(componentStyles.stylesVar) : literalArr([]);
  27051. const viewResult = this._viewCompiler.compileComponent(outputCtx, compMeta, parsedTemplate, stylesExpr, usedPipes);
  27052. if (componentStyles) {
  27053. _resolveStyleStatements(this._symbolResolver, componentStyles, this._styleCompiler.needsStyleShim(compMeta), fileSuffix);
  27054. }
  27055. return viewResult;
  27056. }
  27057. _parseTemplate(compMeta, ngModule, directiveIdentifiers) {
  27058. if (this._templateAstCache.has(compMeta.type.reference)) {
  27059. return this._templateAstCache.get(compMeta.type.reference);
  27060. }
  27061. const preserveWhitespaces = compMeta.template.preserveWhitespaces;
  27062. const directives = directiveIdentifiers.map(dir => this._metadataResolver.getDirectiveSummary(dir.reference));
  27063. const pipes = ngModule.transitiveModule.pipes.map(pipe => this._metadataResolver.getPipeSummary(pipe.reference));
  27064. const result = this._templateParser.parse(compMeta, compMeta.template.htmlAst, directives, pipes, ngModule.schemas, templateSourceUrl(ngModule.type, compMeta, compMeta.template), preserveWhitespaces);
  27065. this._templateAstCache.set(compMeta.type.reference, result);
  27066. return result;
  27067. }
  27068. _createOutputContext(genFilePath) {
  27069. const importExpr$1 = (symbol, typeParams = null, useSummaries = true) => {
  27070. if (!(symbol instanceof StaticSymbol)) {
  27071. throw new Error(`Internal error: unknown identifier ${JSON.stringify(symbol)}`);
  27072. }
  27073. const arity = this._symbolResolver.getTypeArity(symbol) || 0;
  27074. const { filePath, name, members } = this._symbolResolver.getImportAs(symbol, useSummaries) || symbol;
  27075. const importModule = this._fileNameToModuleName(filePath, genFilePath);
  27076. // It should be good enough to compare filePath to genFilePath and if they are equal
  27077. // there is a self reference. However, ngfactory files generate to .ts but their
  27078. // symbols have .d.ts so a simple compare is insufficient. They should be canonical
  27079. // and is tracked by #17705.
  27080. const selfReference = this._fileNameToModuleName(genFilePath, genFilePath);
  27081. const moduleName = importModule === selfReference ? null : importModule;
  27082. // If we are in a type expression that refers to a generic type then supply
  27083. // the required type parameters. If there were not enough type parameters
  27084. // supplied, supply any as the type. Outside a type expression the reference
  27085. // should not supply type parameters and be treated as a simple value reference
  27086. // to the constructor function itself.
  27087. const suppliedTypeParams = typeParams || [];
  27088. const missingTypeParamsCount = arity - suppliedTypeParams.length;
  27089. const allTypeParams = suppliedTypeParams.concat(newArray(missingTypeParamsCount, DYNAMIC_TYPE));
  27090. return members.reduce((expr, memberName) => expr.prop(memberName), importExpr(new ExternalReference(moduleName, name, null), allTypeParams));
  27091. };
  27092. return { statements: [], genFilePath, importExpr: importExpr$1, constantPool: new ConstantPool() };
  27093. }
  27094. _fileNameToModuleName(importedFilePath, containingFilePath) {
  27095. return this._summaryResolver.getKnownModuleName(importedFilePath) ||
  27096. this._symbolResolver.getKnownModuleName(importedFilePath) ||
  27097. this._host.fileNameToModuleName(importedFilePath, containingFilePath);
  27098. }
  27099. _codegenStyles(srcFileUrl, compMeta, stylesheetMetadata, isShimmed, fileSuffix) {
  27100. const outputCtx = this._createOutputContext(_stylesModuleUrl(stylesheetMetadata.moduleUrl, isShimmed, fileSuffix));
  27101. const compiledStylesheet = this._styleCompiler.compileStyles(outputCtx, compMeta, stylesheetMetadata, isShimmed);
  27102. _resolveStyleStatements(this._symbolResolver, compiledStylesheet, isShimmed, fileSuffix);
  27103. return this._codegenSourceModule(srcFileUrl, outputCtx);
  27104. }
  27105. _codegenSourceModule(srcFileUrl, ctx) {
  27106. return new GeneratedFile(srcFileUrl, ctx.genFilePath, ctx.statements);
  27107. }
  27108. listLazyRoutes(entryRoute, analyzedModules) {
  27109. const self = this;
  27110. if (entryRoute) {
  27111. const symbol = parseLazyRoute(entryRoute, this.reflector).referencedModule;
  27112. return visitLazyRoute(symbol);
  27113. }
  27114. else if (analyzedModules) {
  27115. const allLazyRoutes = [];
  27116. for (const ngModule of analyzedModules.ngModules) {
  27117. const lazyRoutes = listLazyRoutes(ngModule, this.reflector);
  27118. for (const lazyRoute of lazyRoutes) {
  27119. allLazyRoutes.push(lazyRoute);
  27120. }
  27121. }
  27122. return allLazyRoutes;
  27123. }
  27124. else {
  27125. throw new Error(`Either route or analyzedModules has to be specified!`);
  27126. }
  27127. function visitLazyRoute(symbol, seenRoutes = new Set(), allLazyRoutes = []) {
  27128. // Support pointing to default exports, but stop recursing there,
  27129. // as the StaticReflector does not yet support default exports.
  27130. if (seenRoutes.has(symbol) || !symbol.name) {
  27131. return allLazyRoutes;
  27132. }
  27133. seenRoutes.add(symbol);
  27134. const lazyRoutes = listLazyRoutes(self._metadataResolver.getNgModuleMetadata(symbol, true), self.reflector);
  27135. for (const lazyRoute of lazyRoutes) {
  27136. allLazyRoutes.push(lazyRoute);
  27137. visitLazyRoute(lazyRoute.referencedModule, seenRoutes, allLazyRoutes);
  27138. }
  27139. return allLazyRoutes;
  27140. }
  27141. }
  27142. }
  27143. function _createEmptyStub(outputCtx) {
  27144. // Note: We need to produce at least one import statement so that
  27145. // TypeScript knows that the file is an es6 module. Otherwise our generated
  27146. // exports / imports won't be emitted properly by TypeScript.
  27147. outputCtx.statements.push(importExpr(Identifiers$1.ComponentFactory).toStmt());
  27148. }
  27149. function _resolveStyleStatements(symbolResolver, compileResult, needsShim, fileSuffix) {
  27150. compileResult.dependencies.forEach((dep) => {
  27151. dep.setValue(symbolResolver.getStaticSymbol(_stylesModuleUrl(dep.moduleUrl, needsShim, fileSuffix), dep.name));
  27152. });
  27153. }
  27154. function _stylesModuleUrl(stylesheetUrl, shim, suffix) {
  27155. return `${stylesheetUrl}${shim ? '.shim' : ''}.ngstyle${suffix}`;
  27156. }
  27157. function analyzeNgModules(fileNames, host, staticSymbolResolver, metadataResolver) {
  27158. const files = _analyzeFilesIncludingNonProgramFiles(fileNames, host, staticSymbolResolver, metadataResolver);
  27159. return mergeAnalyzedFiles(files);
  27160. }
  27161. function analyzeAndValidateNgModules(fileNames, host, staticSymbolResolver, metadataResolver) {
  27162. return validateAnalyzedModules(analyzeNgModules(fileNames, host, staticSymbolResolver, metadataResolver));
  27163. }
  27164. function validateAnalyzedModules(analyzedModules) {
  27165. if (analyzedModules.symbolsMissingModule && analyzedModules.symbolsMissingModule.length) {
  27166. const messages = analyzedModules.symbolsMissingModule.map(s => `Cannot determine the module for class ${s.name} in ${s.filePath}! Add ${s.name} to the NgModule to fix it.`);
  27167. throw syntaxError(messages.join('\n'));
  27168. }
  27169. return analyzedModules;
  27170. }
  27171. // Analyzes all of the program files,
  27172. // including files that are not part of the program
  27173. // but are referenced by an NgModule.
  27174. function _analyzeFilesIncludingNonProgramFiles(fileNames, host, staticSymbolResolver, metadataResolver) {
  27175. const seenFiles = new Set();
  27176. const files = [];
  27177. const visitFile = (fileName) => {
  27178. if (seenFiles.has(fileName) || !host.isSourceFile(fileName)) {
  27179. return false;
  27180. }
  27181. seenFiles.add(fileName);
  27182. const analyzedFile = analyzeFile(host, staticSymbolResolver, metadataResolver, fileName);
  27183. files.push(analyzedFile);
  27184. analyzedFile.ngModules.forEach(ngModule => {
  27185. ngModule.transitiveModule.modules.forEach(modMeta => visitFile(modMeta.reference.filePath));
  27186. });
  27187. };
  27188. fileNames.forEach((fileName) => visitFile(fileName));
  27189. return files;
  27190. }
  27191. function analyzeFile(host, staticSymbolResolver, metadataResolver, fileName) {
  27192. const abstractDirectives = [];
  27193. const directives = [];
  27194. const pipes = [];
  27195. const injectables = [];
  27196. const ngModules = [];
  27197. const hasDecorators = staticSymbolResolver.hasDecorators(fileName);
  27198. let exportsNonSourceFiles = false;
  27199. const isDeclarationFile = fileName.endsWith('.d.ts');
  27200. // Don't analyze .d.ts files that have no decorators as a shortcut
  27201. // to speed up the analysis. This prevents us from
  27202. // resolving the references in these files.
  27203. // Note: exportsNonSourceFiles is only needed when compiling with summaries,
  27204. // which is not the case when .d.ts files are treated as input files.
  27205. if (!isDeclarationFile || hasDecorators) {
  27206. staticSymbolResolver.getSymbolsOf(fileName).forEach((symbol) => {
  27207. const resolvedSymbol = staticSymbolResolver.resolveSymbol(symbol);
  27208. const symbolMeta = resolvedSymbol.metadata;
  27209. if (!symbolMeta || symbolMeta.__symbolic === 'error') {
  27210. return;
  27211. }
  27212. let isNgSymbol = false;
  27213. if (symbolMeta.__symbolic === 'class') {
  27214. if (metadataResolver.isDirective(symbol)) {
  27215. isNgSymbol = true;
  27216. // This directive either has a selector or doesn't. Selector-less directives get tracked
  27217. // in abstractDirectives, not directives. The compiler doesn't deal with selector-less
  27218. // directives at all, really, other than to persist their metadata. This is done so that
  27219. // apps will have an easier time migrating to Ivy, which requires the selector-less
  27220. // annotations to be applied.
  27221. if (!metadataResolver.isAbstractDirective(symbol)) {
  27222. // The directive is an ordinary directive.
  27223. directives.push(symbol);
  27224. }
  27225. else {
  27226. // The directive has no selector and is an "abstract" directive, so track it
  27227. // accordingly.
  27228. abstractDirectives.push(symbol);
  27229. }
  27230. }
  27231. else if (metadataResolver.isPipe(symbol)) {
  27232. isNgSymbol = true;
  27233. pipes.push(symbol);
  27234. }
  27235. else if (metadataResolver.isNgModule(symbol)) {
  27236. const ngModule = metadataResolver.getNgModuleMetadata(symbol, false);
  27237. if (ngModule) {
  27238. isNgSymbol = true;
  27239. ngModules.push(ngModule);
  27240. }
  27241. }
  27242. else if (metadataResolver.isInjectable(symbol)) {
  27243. isNgSymbol = true;
  27244. const injectable = metadataResolver.getInjectableMetadata(symbol, null, false);
  27245. if (injectable) {
  27246. injectables.push(injectable);
  27247. }
  27248. }
  27249. }
  27250. if (!isNgSymbol) {
  27251. exportsNonSourceFiles =
  27252. exportsNonSourceFiles || isValueExportingNonSourceFile(host, symbolMeta);
  27253. }
  27254. });
  27255. }
  27256. return {
  27257. fileName,
  27258. directives,
  27259. abstractDirectives,
  27260. pipes,
  27261. ngModules,
  27262. injectables,
  27263. exportsNonSourceFiles,
  27264. };
  27265. }
  27266. function analyzeFileForInjectables(host, staticSymbolResolver, metadataResolver, fileName) {
  27267. const injectables = [];
  27268. const shallowModules = [];
  27269. if (staticSymbolResolver.hasDecorators(fileName)) {
  27270. staticSymbolResolver.getSymbolsOf(fileName).forEach((symbol) => {
  27271. const resolvedSymbol = staticSymbolResolver.resolveSymbol(symbol);
  27272. const symbolMeta = resolvedSymbol.metadata;
  27273. if (!symbolMeta || symbolMeta.__symbolic === 'error') {
  27274. return;
  27275. }
  27276. if (symbolMeta.__symbolic === 'class') {
  27277. if (metadataResolver.isInjectable(symbol)) {
  27278. const injectable = metadataResolver.getInjectableMetadata(symbol, null, false);
  27279. if (injectable) {
  27280. injectables.push(injectable);
  27281. }
  27282. }
  27283. else if (metadataResolver.isNgModule(symbol)) {
  27284. const module = metadataResolver.getShallowModuleMetadata(symbol);
  27285. if (module) {
  27286. shallowModules.push(module);
  27287. }
  27288. }
  27289. }
  27290. });
  27291. }
  27292. return { fileName, injectables, shallowModules };
  27293. }
  27294. function isValueExportingNonSourceFile(host, metadata) {
  27295. let exportsNonSourceFiles = false;
  27296. class Visitor {
  27297. visitArray(arr, context) {
  27298. arr.forEach(v => visitValue(v, this, context));
  27299. }
  27300. visitStringMap(map, context) {
  27301. Object.keys(map).forEach((key) => visitValue(map[key], this, context));
  27302. }
  27303. visitPrimitive(value, context) { }
  27304. visitOther(value, context) {
  27305. if (value instanceof StaticSymbol && !host.isSourceFile(value.filePath)) {
  27306. exportsNonSourceFiles = true;
  27307. }
  27308. }
  27309. }
  27310. visitValue(metadata, new Visitor(), null);
  27311. return exportsNonSourceFiles;
  27312. }
  27313. function mergeAnalyzedFiles(analyzedFiles) {
  27314. const allNgModules = [];
  27315. const ngModuleByPipeOrDirective = new Map();
  27316. const allPipesAndDirectives = new Set();
  27317. analyzedFiles.forEach(af => {
  27318. af.ngModules.forEach(ngModule => {
  27319. allNgModules.push(ngModule);
  27320. ngModule.declaredDirectives.forEach(d => ngModuleByPipeOrDirective.set(d.reference, ngModule));
  27321. ngModule.declaredPipes.forEach(p => ngModuleByPipeOrDirective.set(p.reference, ngModule));
  27322. });
  27323. af.directives.forEach(d => allPipesAndDirectives.add(d));
  27324. af.pipes.forEach(p => allPipesAndDirectives.add(p));
  27325. });
  27326. const symbolsMissingModule = [];
  27327. allPipesAndDirectives.forEach(ref => {
  27328. if (!ngModuleByPipeOrDirective.has(ref)) {
  27329. symbolsMissingModule.push(ref);
  27330. }
  27331. });
  27332. return {
  27333. ngModules: allNgModules,
  27334. ngModuleByPipeOrDirective,
  27335. symbolsMissingModule,
  27336. files: analyzedFiles
  27337. };
  27338. }
  27339. function mergeAndValidateNgFiles(files) {
  27340. return validateAnalyzedModules(mergeAnalyzedFiles(files));
  27341. }
  27342. /**
  27343. * @license
  27344. * Copyright Google LLC All Rights Reserved.
  27345. *
  27346. * Use of this source code is governed by an MIT-style license that can be
  27347. * found in the LICENSE file at https://angular.io/license
  27348. */
  27349. const FORMATTED_MESSAGE = 'ngFormattedMessage';
  27350. function indentStr(level) {
  27351. if (level <= 0)
  27352. return '';
  27353. if (level < 6)
  27354. return ['', ' ', ' ', ' ', ' ', ' '][level];
  27355. const half = indentStr(Math.floor(level / 2));
  27356. return half + half + (level % 2 === 1 ? ' ' : '');
  27357. }
  27358. function formatChain(chain, indent = 0) {
  27359. if (!chain)
  27360. return '';
  27361. const position = chain.position ?
  27362. `${chain.position.fileName}(${chain.position.line + 1},${chain.position.column + 1})` :
  27363. '';
  27364. const prefix = position && indent === 0 ? `${position}: ` : '';
  27365. const postfix = position && indent !== 0 ? ` at ${position}` : '';
  27366. let message = `${prefix}${chain.message}${postfix}`;
  27367. if (chain.next) {
  27368. for (const kid of chain.next) {
  27369. message += '\n' + formatChain(kid, indent + 2);
  27370. }
  27371. }
  27372. return `${indentStr(indent)}${message}`;
  27373. }
  27374. function formattedError(chain) {
  27375. const message = formatChain(chain) + '.';
  27376. const error = syntaxError(message);
  27377. error[FORMATTED_MESSAGE] = true;
  27378. error.chain = chain;
  27379. error.position = chain.position;
  27380. return error;
  27381. }
  27382. function isFormattedError(error) {
  27383. return !!error[FORMATTED_MESSAGE];
  27384. }
  27385. /**
  27386. * @license
  27387. * Copyright Google LLC All Rights Reserved.
  27388. *
  27389. * Use of this source code is governed by an MIT-style license that can be
  27390. * found in the LICENSE file at https://angular.io/license
  27391. */
  27392. const ANGULAR_CORE = '@angular/core';
  27393. const ANGULAR_ROUTER = '@angular/router';
  27394. const HIDDEN_KEY = /^\$.*\$$/;
  27395. const IGNORE = {
  27396. __symbolic: 'ignore'
  27397. };
  27398. const USE_VALUE$1 = 'useValue';
  27399. const PROVIDE = 'provide';
  27400. const REFERENCE_SET = new Set([USE_VALUE$1, 'useFactory', 'data', 'id', 'loadChildren']);
  27401. const TYPEGUARD_POSTFIX = 'TypeGuard';
  27402. const USE_IF = 'UseIf';
  27403. function shouldIgnore(value) {
  27404. return value && value.__symbolic == 'ignore';
  27405. }
  27406. /**
  27407. * A static reflector implements enough of the Reflector API that is necessary to compile
  27408. * templates statically.
  27409. */
  27410. class StaticReflector {
  27411. constructor(summaryResolver, symbolResolver, knownMetadataClasses = [], knownMetadataFunctions = [], errorRecorder) {
  27412. this.summaryResolver = summaryResolver;
  27413. this.symbolResolver = symbolResolver;
  27414. this.errorRecorder = errorRecorder;
  27415. this.annotationCache = new Map();
  27416. this.shallowAnnotationCache = new Map();
  27417. this.propertyCache = new Map();
  27418. this.parameterCache = new Map();
  27419. this.methodCache = new Map();
  27420. this.staticCache = new Map();
  27421. this.conversionMap = new Map();
  27422. this.resolvedExternalReferences = new Map();
  27423. this.annotationForParentClassWithSummaryKind = new Map();
  27424. this.initializeConversionMap();
  27425. knownMetadataClasses.forEach((kc) => this._registerDecoratorOrConstructor(this.getStaticSymbol(kc.filePath, kc.name), kc.ctor));
  27426. knownMetadataFunctions.forEach((kf) => this._registerFunction(this.getStaticSymbol(kf.filePath, kf.name), kf.fn));
  27427. this.annotationForParentClassWithSummaryKind.set(CompileSummaryKind.Directive, [createDirective, createComponent]);
  27428. this.annotationForParentClassWithSummaryKind.set(CompileSummaryKind.Pipe, [createPipe]);
  27429. this.annotationForParentClassWithSummaryKind.set(CompileSummaryKind.NgModule, [createNgModule]);
  27430. this.annotationForParentClassWithSummaryKind.set(CompileSummaryKind.Injectable, [createInjectable, createPipe, createDirective, createComponent, createNgModule]);
  27431. }
  27432. componentModuleUrl(typeOrFunc) {
  27433. const staticSymbol = this.findSymbolDeclaration(typeOrFunc);
  27434. return this.symbolResolver.getResourcePath(staticSymbol);
  27435. }
  27436. /**
  27437. * Invalidate the specified `symbols` on program change.
  27438. * @param symbols
  27439. */
  27440. invalidateSymbols(symbols) {
  27441. for (const symbol of symbols) {
  27442. this.annotationCache.delete(symbol);
  27443. this.shallowAnnotationCache.delete(symbol);
  27444. this.propertyCache.delete(symbol);
  27445. this.parameterCache.delete(symbol);
  27446. this.methodCache.delete(symbol);
  27447. this.staticCache.delete(symbol);
  27448. this.conversionMap.delete(symbol);
  27449. }
  27450. }
  27451. resolveExternalReference(ref, containingFile) {
  27452. let key = undefined;
  27453. if (!containingFile) {
  27454. key = `${ref.moduleName}:${ref.name}`;
  27455. const declarationSymbol = this.resolvedExternalReferences.get(key);
  27456. if (declarationSymbol)
  27457. return declarationSymbol;
  27458. }
  27459. const refSymbol = this.symbolResolver.getSymbolByModule(ref.moduleName, ref.name, containingFile);
  27460. const declarationSymbol = this.findSymbolDeclaration(refSymbol);
  27461. if (!containingFile) {
  27462. this.symbolResolver.recordModuleNameForFileName(refSymbol.filePath, ref.moduleName);
  27463. this.symbolResolver.recordImportAs(declarationSymbol, refSymbol);
  27464. }
  27465. if (key) {
  27466. this.resolvedExternalReferences.set(key, declarationSymbol);
  27467. }
  27468. return declarationSymbol;
  27469. }
  27470. findDeclaration(moduleUrl, name, containingFile) {
  27471. return this.findSymbolDeclaration(this.symbolResolver.getSymbolByModule(moduleUrl, name, containingFile));
  27472. }
  27473. tryFindDeclaration(moduleUrl, name, containingFile) {
  27474. return this.symbolResolver.ignoreErrorsFor(() => this.findDeclaration(moduleUrl, name, containingFile));
  27475. }
  27476. findSymbolDeclaration(symbol) {
  27477. const resolvedSymbol = this.symbolResolver.resolveSymbol(symbol);
  27478. if (resolvedSymbol) {
  27479. let resolvedMetadata = resolvedSymbol.metadata;
  27480. if (resolvedMetadata && resolvedMetadata.__symbolic === 'resolved') {
  27481. resolvedMetadata = resolvedMetadata.symbol;
  27482. }
  27483. if (resolvedMetadata instanceof StaticSymbol) {
  27484. return this.findSymbolDeclaration(resolvedSymbol.metadata);
  27485. }
  27486. }
  27487. return symbol;
  27488. }
  27489. tryAnnotations(type) {
  27490. const originalRecorder = this.errorRecorder;
  27491. this.errorRecorder = (error, fileName) => { };
  27492. try {
  27493. return this.annotations(type);
  27494. }
  27495. finally {
  27496. this.errorRecorder = originalRecorder;
  27497. }
  27498. }
  27499. annotations(type) {
  27500. return this._annotations(type, (type, decorators) => this.simplify(type, decorators), this.annotationCache);
  27501. }
  27502. shallowAnnotations(type) {
  27503. return this._annotations(type, (type, decorators) => this.simplify(type, decorators, true), this.shallowAnnotationCache);
  27504. }
  27505. _annotations(type, simplify, annotationCache) {
  27506. let annotations = annotationCache.get(type);
  27507. if (!annotations) {
  27508. annotations = [];
  27509. const classMetadata = this.getTypeMetadata(type);
  27510. const parentType = this.findParentType(type, classMetadata);
  27511. if (parentType) {
  27512. const parentAnnotations = this.annotations(parentType);
  27513. annotations.push(...parentAnnotations);
  27514. }
  27515. let ownAnnotations = [];
  27516. if (classMetadata['decorators']) {
  27517. ownAnnotations = simplify(type, classMetadata['decorators']);
  27518. if (ownAnnotations) {
  27519. annotations.push(...ownAnnotations);
  27520. }
  27521. }
  27522. if (parentType && !this.summaryResolver.isLibraryFile(type.filePath) &&
  27523. this.summaryResolver.isLibraryFile(parentType.filePath)) {
  27524. const summary = this.summaryResolver.resolveSummary(parentType);
  27525. if (summary && summary.type) {
  27526. const requiredAnnotationTypes = this.annotationForParentClassWithSummaryKind.get(summary.type.summaryKind);
  27527. const typeHasRequiredAnnotation = requiredAnnotationTypes.some((requiredType) => ownAnnotations.some(ann => requiredType.isTypeOf(ann)));
  27528. if (!typeHasRequiredAnnotation) {
  27529. this.reportError(formatMetadataError(metadataError(`Class ${type.name} in ${type.filePath} extends from a ${CompileSummaryKind[summary.type.summaryKind]} in another compilation unit without duplicating the decorator`,
  27530. /* summary */ undefined, `Please add a ${requiredAnnotationTypes.map((type) => type.ngMetadataName)
  27531. .join(' or ')} decorator to the class`), type), type);
  27532. }
  27533. }
  27534. }
  27535. annotationCache.set(type, annotations.filter(ann => !!ann));
  27536. }
  27537. return annotations;
  27538. }
  27539. propMetadata(type) {
  27540. let propMetadata = this.propertyCache.get(type);
  27541. if (!propMetadata) {
  27542. const classMetadata = this.getTypeMetadata(type);
  27543. propMetadata = {};
  27544. const parentType = this.findParentType(type, classMetadata);
  27545. if (parentType) {
  27546. const parentPropMetadata = this.propMetadata(parentType);
  27547. Object.keys(parentPropMetadata).forEach((parentProp) => {
  27548. propMetadata[parentProp] = parentPropMetadata[parentProp];
  27549. });
  27550. }
  27551. const members = classMetadata['members'] || {};
  27552. Object.keys(members).forEach((propName) => {
  27553. const propData = members[propName];
  27554. const prop = propData
  27555. .find(a => a['__symbolic'] == 'property' || a['__symbolic'] == 'method');
  27556. const decorators = [];
  27557. // hasOwnProperty() is used here to make sure we do not look up methods
  27558. // on `Object.prototype`.
  27559. if (propMetadata === null || propMetadata === void 0 ? void 0 : propMetadata.hasOwnProperty(propName)) {
  27560. decorators.push(...propMetadata[propName]);
  27561. }
  27562. propMetadata[propName] = decorators;
  27563. if (prop && prop['decorators']) {
  27564. decorators.push(...this.simplify(type, prop['decorators']));
  27565. }
  27566. });
  27567. this.propertyCache.set(type, propMetadata);
  27568. }
  27569. return propMetadata;
  27570. }
  27571. parameters(type) {
  27572. if (!(type instanceof StaticSymbol)) {
  27573. this.reportError(new Error(`parameters received ${JSON.stringify(type)} which is not a StaticSymbol`), type);
  27574. return [];
  27575. }
  27576. try {
  27577. let parameters = this.parameterCache.get(type);
  27578. if (!parameters) {
  27579. const classMetadata = this.getTypeMetadata(type);
  27580. const parentType = this.findParentType(type, classMetadata);
  27581. const members = classMetadata ? classMetadata['members'] : null;
  27582. const ctorData = members ? members['__ctor__'] : null;
  27583. if (ctorData) {
  27584. const ctor = ctorData.find(a => a['__symbolic'] == 'constructor');
  27585. const rawParameterTypes = ctor['parameters'] || [];
  27586. const parameterDecorators = this.simplify(type, ctor['parameterDecorators'] || []);
  27587. parameters = [];
  27588. rawParameterTypes.forEach((rawParamType, index) => {
  27589. const nestedResult = [];
  27590. const paramType = this.trySimplify(type, rawParamType);
  27591. if (paramType)
  27592. nestedResult.push(paramType);
  27593. const decorators = parameterDecorators ? parameterDecorators[index] : null;
  27594. if (decorators) {
  27595. nestedResult.push(...decorators);
  27596. }
  27597. parameters.push(nestedResult);
  27598. });
  27599. }
  27600. else if (parentType) {
  27601. parameters = this.parameters(parentType);
  27602. }
  27603. if (!parameters) {
  27604. parameters = [];
  27605. }
  27606. this.parameterCache.set(type, parameters);
  27607. }
  27608. return parameters;
  27609. }
  27610. catch (e) {
  27611. console.error(`Failed on type ${JSON.stringify(type)} with error ${e}`);
  27612. throw e;
  27613. }
  27614. }
  27615. _methodNames(type) {
  27616. let methodNames = this.methodCache.get(type);
  27617. if (!methodNames) {
  27618. const classMetadata = this.getTypeMetadata(type);
  27619. methodNames = {};
  27620. const parentType = this.findParentType(type, classMetadata);
  27621. if (parentType) {
  27622. const parentMethodNames = this._methodNames(parentType);
  27623. Object.keys(parentMethodNames).forEach((parentProp) => {
  27624. methodNames[parentProp] = parentMethodNames[parentProp];
  27625. });
  27626. }
  27627. const members = classMetadata['members'] || {};
  27628. Object.keys(members).forEach((propName) => {
  27629. const propData = members[propName];
  27630. const isMethod = propData.some(a => a['__symbolic'] == 'method');
  27631. methodNames[propName] = methodNames[propName] || isMethod;
  27632. });
  27633. this.methodCache.set(type, methodNames);
  27634. }
  27635. return methodNames;
  27636. }
  27637. _staticMembers(type) {
  27638. let staticMembers = this.staticCache.get(type);
  27639. if (!staticMembers) {
  27640. const classMetadata = this.getTypeMetadata(type);
  27641. const staticMemberData = classMetadata['statics'] || {};
  27642. staticMembers = Object.keys(staticMemberData);
  27643. this.staticCache.set(type, staticMembers);
  27644. }
  27645. return staticMembers;
  27646. }
  27647. findParentType(type, classMetadata) {
  27648. const parentType = this.trySimplify(type, classMetadata['extends']);
  27649. if (parentType instanceof StaticSymbol) {
  27650. return parentType;
  27651. }
  27652. }
  27653. hasLifecycleHook(type, lcProperty) {
  27654. if (!(type instanceof StaticSymbol)) {
  27655. this.reportError(new Error(`hasLifecycleHook received ${JSON.stringify(type)} which is not a StaticSymbol`), type);
  27656. }
  27657. try {
  27658. return !!this._methodNames(type)[lcProperty];
  27659. }
  27660. catch (e) {
  27661. console.error(`Failed on type ${JSON.stringify(type)} with error ${e}`);
  27662. throw e;
  27663. }
  27664. }
  27665. guards(type) {
  27666. if (!(type instanceof StaticSymbol)) {
  27667. this.reportError(new Error(`guards received ${JSON.stringify(type)} which is not a StaticSymbol`), type);
  27668. return {};
  27669. }
  27670. const staticMembers = this._staticMembers(type);
  27671. const result = {};
  27672. for (let name of staticMembers) {
  27673. if (name.endsWith(TYPEGUARD_POSTFIX)) {
  27674. let property = name.substr(0, name.length - TYPEGUARD_POSTFIX.length);
  27675. let value;
  27676. if (property.endsWith(USE_IF)) {
  27677. property = name.substr(0, property.length - USE_IF.length);
  27678. value = USE_IF;
  27679. }
  27680. else {
  27681. value = this.getStaticSymbol(type.filePath, type.name, [name]);
  27682. }
  27683. result[property] = value;
  27684. }
  27685. }
  27686. return result;
  27687. }
  27688. _registerDecoratorOrConstructor(type, ctor) {
  27689. this.conversionMap.set(type, (context, args) => new ctor(...args));
  27690. }
  27691. _registerFunction(type, fn) {
  27692. this.conversionMap.set(type, (context, args) => fn.apply(undefined, args));
  27693. }
  27694. initializeConversionMap() {
  27695. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Injectable'), createInjectable);
  27696. this.injectionToken = this.findDeclaration(ANGULAR_CORE, 'InjectionToken');
  27697. this.opaqueToken = this.findDeclaration(ANGULAR_CORE, 'OpaqueToken');
  27698. this.ROUTES = this.tryFindDeclaration(ANGULAR_ROUTER, 'ROUTES');
  27699. this.ANALYZE_FOR_ENTRY_COMPONENTS =
  27700. this.findDeclaration(ANGULAR_CORE, 'ANALYZE_FOR_ENTRY_COMPONENTS');
  27701. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Host'), createHost);
  27702. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Self'), createSelf);
  27703. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'SkipSelf'), createSkipSelf);
  27704. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Inject'), createInject);
  27705. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Optional'), createOptional);
  27706. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Attribute'), createAttribute);
  27707. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'ContentChild'), createContentChild);
  27708. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'ContentChildren'), createContentChildren);
  27709. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'ViewChild'), createViewChild);
  27710. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'ViewChildren'), createViewChildren);
  27711. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Input'), createInput);
  27712. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Output'), createOutput);
  27713. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Pipe'), createPipe);
  27714. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'HostBinding'), createHostBinding);
  27715. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'HostListener'), createHostListener);
  27716. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Directive'), createDirective);
  27717. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Component'), createComponent);
  27718. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'NgModule'), createNgModule);
  27719. // Note: Some metadata classes can be used directly with Provider.deps.
  27720. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Host'), createHost);
  27721. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Self'), createSelf);
  27722. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'SkipSelf'), createSkipSelf);
  27723. this._registerDecoratorOrConstructor(this.findDeclaration(ANGULAR_CORE, 'Optional'), createOptional);
  27724. }
  27725. /**
  27726. * getStaticSymbol produces a Type whose metadata is known but whose implementation is not loaded.
  27727. * All types passed to the StaticResolver should be pseudo-types returned by this method.
  27728. *
  27729. * @param declarationFile the absolute path of the file where the symbol is declared
  27730. * @param name the name of the type.
  27731. */
  27732. getStaticSymbol(declarationFile, name, members) {
  27733. return this.symbolResolver.getStaticSymbol(declarationFile, name, members);
  27734. }
  27735. /**
  27736. * Simplify but discard any errors
  27737. */
  27738. trySimplify(context, value) {
  27739. const originalRecorder = this.errorRecorder;
  27740. this.errorRecorder = (error, fileName) => { };
  27741. const result = this.simplify(context, value);
  27742. this.errorRecorder = originalRecorder;
  27743. return result;
  27744. }
  27745. /** @internal */
  27746. simplify(context, value, lazy = false) {
  27747. const self = this;
  27748. let scope = BindingScope$1.empty;
  27749. const calling = new Map();
  27750. const rootContext = context;
  27751. function simplifyInContext(context, value, depth, references) {
  27752. function resolveReferenceValue(staticSymbol) {
  27753. const resolvedSymbol = self.symbolResolver.resolveSymbol(staticSymbol);
  27754. return resolvedSymbol ? resolvedSymbol.metadata : null;
  27755. }
  27756. function simplifyEagerly(value) {
  27757. return simplifyInContext(context, value, depth, 0);
  27758. }
  27759. function simplifyLazily(value) {
  27760. return simplifyInContext(context, value, depth, references + 1);
  27761. }
  27762. function simplifyNested(nestedContext, value) {
  27763. if (nestedContext === context) {
  27764. // If the context hasn't changed let the exception propagate unmodified.
  27765. return simplifyInContext(nestedContext, value, depth + 1, references);
  27766. }
  27767. try {
  27768. return simplifyInContext(nestedContext, value, depth + 1, references);
  27769. }
  27770. catch (e) {
  27771. if (isMetadataError(e)) {
  27772. // Propagate the message text up but add a message to the chain that explains how we got
  27773. // here.
  27774. // e.chain implies e.symbol
  27775. const summaryMsg = e.chain ? 'references \'' + e.symbol.name + '\'' : errorSummary(e);
  27776. const summary = `'${nestedContext.name}' ${summaryMsg}`;
  27777. const chain = { message: summary, position: e.position, next: e.chain };
  27778. // TODO(chuckj): retrieve the position information indirectly from the collectors node
  27779. // map if the metadata is from a .ts file.
  27780. self.error({
  27781. message: e.message,
  27782. advise: e.advise,
  27783. context: e.context,
  27784. chain,
  27785. symbol: nestedContext
  27786. }, context);
  27787. }
  27788. else {
  27789. // It is probably an internal error.
  27790. throw e;
  27791. }
  27792. }
  27793. }
  27794. function simplifyCall(functionSymbol, targetFunction, args, targetExpression) {
  27795. if (targetFunction && targetFunction['__symbolic'] == 'function') {
  27796. if (calling.get(functionSymbol)) {
  27797. self.error({
  27798. message: 'Recursion is not supported',
  27799. summary: `called '${functionSymbol.name}' recursively`,
  27800. value: targetFunction
  27801. }, functionSymbol);
  27802. }
  27803. try {
  27804. const value = targetFunction['value'];
  27805. if (value && (depth != 0 || value.__symbolic != 'error')) {
  27806. const parameters = targetFunction['parameters'];
  27807. const defaults = targetFunction.defaults;
  27808. args = args.map(arg => simplifyNested(context, arg))
  27809. .map(arg => shouldIgnore(arg) ? undefined : arg);
  27810. if (defaults && defaults.length > args.length) {
  27811. args.push(...defaults.slice(args.length).map((value) => simplify(value)));
  27812. }
  27813. calling.set(functionSymbol, true);
  27814. const functionScope = BindingScope$1.build();
  27815. for (let i = 0; i < parameters.length; i++) {
  27816. functionScope.define(parameters[i], args[i]);
  27817. }
  27818. const oldScope = scope;
  27819. let result;
  27820. try {
  27821. scope = functionScope.done();
  27822. result = simplifyNested(functionSymbol, value);
  27823. }
  27824. finally {
  27825. scope = oldScope;
  27826. }
  27827. return result;
  27828. }
  27829. }
  27830. finally {
  27831. calling.delete(functionSymbol);
  27832. }
  27833. }
  27834. if (depth === 0) {
  27835. // If depth is 0 we are evaluating the top level expression that is describing element
  27836. // decorator. In this case, it is a decorator we don't understand, such as a custom
  27837. // non-angular decorator, and we should just ignore it.
  27838. return IGNORE;
  27839. }
  27840. let position = undefined;
  27841. if (targetExpression && targetExpression.__symbolic == 'resolved') {
  27842. const line = targetExpression.line;
  27843. const character = targetExpression.character;
  27844. const fileName = targetExpression.fileName;
  27845. if (fileName != null && line != null && character != null) {
  27846. position = { fileName, line, column: character };
  27847. }
  27848. }
  27849. self.error({
  27850. message: FUNCTION_CALL_NOT_SUPPORTED,
  27851. context: functionSymbol,
  27852. value: targetFunction,
  27853. position
  27854. }, context);
  27855. }
  27856. function simplify(expression) {
  27857. if (isPrimitive(expression)) {
  27858. return expression;
  27859. }
  27860. if (Array.isArray(expression)) {
  27861. const result = [];
  27862. for (const item of expression) {
  27863. // Check for a spread expression
  27864. if (item && item.__symbolic === 'spread') {
  27865. // We call with references as 0 because we require the actual value and cannot
  27866. // tolerate a reference here.
  27867. const spreadArray = simplifyEagerly(item.expression);
  27868. if (Array.isArray(spreadArray)) {
  27869. for (const spreadItem of spreadArray) {
  27870. result.push(spreadItem);
  27871. }
  27872. continue;
  27873. }
  27874. }
  27875. const value = simplify(item);
  27876. if (shouldIgnore(value)) {
  27877. continue;
  27878. }
  27879. result.push(value);
  27880. }
  27881. return result;
  27882. }
  27883. if (expression instanceof StaticSymbol) {
  27884. // Stop simplification at builtin symbols or if we are in a reference context and
  27885. // the symbol doesn't have members.
  27886. if (expression === self.injectionToken || self.conversionMap.has(expression) ||
  27887. (references > 0 && !expression.members.length)) {
  27888. return expression;
  27889. }
  27890. else {
  27891. const staticSymbol = expression;
  27892. const declarationValue = resolveReferenceValue(staticSymbol);
  27893. if (declarationValue != null) {
  27894. return simplifyNested(staticSymbol, declarationValue);
  27895. }
  27896. else {
  27897. return staticSymbol;
  27898. }
  27899. }
  27900. }
  27901. if (expression) {
  27902. if (expression['__symbolic']) {
  27903. let staticSymbol;
  27904. switch (expression['__symbolic']) {
  27905. case 'binop':
  27906. let left = simplify(expression['left']);
  27907. if (shouldIgnore(left))
  27908. return left;
  27909. let right = simplify(expression['right']);
  27910. if (shouldIgnore(right))
  27911. return right;
  27912. switch (expression['operator']) {
  27913. case '&&':
  27914. return left && right;
  27915. case '||':
  27916. return left || right;
  27917. case '|':
  27918. return left | right;
  27919. case '^':
  27920. return left ^ right;
  27921. case '&':
  27922. return left & right;
  27923. case '==':
  27924. return left == right;
  27925. case '!=':
  27926. return left != right;
  27927. case '===':
  27928. return left === right;
  27929. case '!==':
  27930. return left !== right;
  27931. case '<':
  27932. return left < right;
  27933. case '>':
  27934. return left > right;
  27935. case '<=':
  27936. return left <= right;
  27937. case '>=':
  27938. return left >= right;
  27939. case '<<':
  27940. return left << right;
  27941. case '>>':
  27942. return left >> right;
  27943. case '+':
  27944. return left + right;
  27945. case '-':
  27946. return left - right;
  27947. case '*':
  27948. return left * right;
  27949. case '/':
  27950. return left / right;
  27951. case '%':
  27952. return left % right;
  27953. case '??':
  27954. return left !== null && left !== void 0 ? left : right;
  27955. }
  27956. return null;
  27957. case 'if':
  27958. let condition = simplify(expression['condition']);
  27959. return condition ? simplify(expression['thenExpression']) :
  27960. simplify(expression['elseExpression']);
  27961. case 'pre':
  27962. let operand = simplify(expression['operand']);
  27963. if (shouldIgnore(operand))
  27964. return operand;
  27965. switch (expression['operator']) {
  27966. case '+':
  27967. return operand;
  27968. case '-':
  27969. return -operand;
  27970. case '!':
  27971. return !operand;
  27972. case '~':
  27973. return ~operand;
  27974. }
  27975. return null;
  27976. case 'index':
  27977. let indexTarget = simplifyEagerly(expression['expression']);
  27978. let index = simplifyEagerly(expression['index']);
  27979. if (indexTarget && isPrimitive(index))
  27980. return indexTarget[index];
  27981. return null;
  27982. case 'select':
  27983. const member = expression['member'];
  27984. let selectContext = context;
  27985. let selectTarget = simplify(expression['expression']);
  27986. if (selectTarget instanceof StaticSymbol) {
  27987. const members = selectTarget.members.concat(member);
  27988. selectContext =
  27989. self.getStaticSymbol(selectTarget.filePath, selectTarget.name, members);
  27990. const declarationValue = resolveReferenceValue(selectContext);
  27991. if (declarationValue != null) {
  27992. return simplifyNested(selectContext, declarationValue);
  27993. }
  27994. else {
  27995. return selectContext;
  27996. }
  27997. }
  27998. if (selectTarget && isPrimitive(member))
  27999. return simplifyNested(selectContext, selectTarget[member]);
  28000. return null;
  28001. case 'reference':
  28002. // Note: This only has to deal with variable references, as symbol references have
  28003. // been converted into 'resolved'
  28004. // in the StaticSymbolResolver.
  28005. const name = expression['name'];
  28006. const localValue = scope.resolve(name);
  28007. if (localValue != BindingScope$1.missing) {
  28008. return localValue;
  28009. }
  28010. break;
  28011. case 'resolved':
  28012. try {
  28013. return simplify(expression.symbol);
  28014. }
  28015. catch (e) {
  28016. // If an error is reported evaluating the symbol record the position of the
  28017. // reference in the error so it can
  28018. // be reported in the error message generated from the exception.
  28019. if (isMetadataError(e) && expression.fileName != null &&
  28020. expression.line != null && expression.character != null) {
  28021. e.position = {
  28022. fileName: expression.fileName,
  28023. line: expression.line,
  28024. column: expression.character
  28025. };
  28026. }
  28027. throw e;
  28028. }
  28029. case 'class':
  28030. return context;
  28031. case 'function':
  28032. return context;
  28033. case 'new':
  28034. case 'call':
  28035. // Determine if the function is a built-in conversion
  28036. staticSymbol = simplifyInContext(context, expression['expression'], depth + 1, /* references */ 0);
  28037. if (staticSymbol instanceof StaticSymbol) {
  28038. if (staticSymbol === self.injectionToken || staticSymbol === self.opaqueToken) {
  28039. // if somebody calls new InjectionToken, don't create an InjectionToken,
  28040. // but rather return the symbol to which the InjectionToken is assigned to.
  28041. // OpaqueToken is supported too as it is required by the language service to
  28042. // support v4 and prior versions of Angular.
  28043. return context;
  28044. }
  28045. const argExpressions = expression['arguments'] || [];
  28046. let converter = self.conversionMap.get(staticSymbol);
  28047. if (converter) {
  28048. const args = argExpressions.map(arg => simplifyNested(context, arg))
  28049. .map(arg => shouldIgnore(arg) ? undefined : arg);
  28050. return converter(context, args);
  28051. }
  28052. else {
  28053. // Determine if the function is one we can simplify.
  28054. const targetFunction = resolveReferenceValue(staticSymbol);
  28055. return simplifyCall(staticSymbol, targetFunction, argExpressions, expression['expression']);
  28056. }
  28057. }
  28058. return IGNORE;
  28059. case 'error':
  28060. let message = expression.message;
  28061. if (expression['line'] != null) {
  28062. self.error({
  28063. message,
  28064. context: expression.context,
  28065. value: expression,
  28066. position: {
  28067. fileName: expression['fileName'],
  28068. line: expression['line'],
  28069. column: expression['character']
  28070. }
  28071. }, context);
  28072. }
  28073. else {
  28074. self.error({ message, context: expression.context }, context);
  28075. }
  28076. return IGNORE;
  28077. case 'ignore':
  28078. return expression;
  28079. }
  28080. return null;
  28081. }
  28082. return mapStringMap(expression, (value, name) => {
  28083. if (REFERENCE_SET.has(name)) {
  28084. if (name === USE_VALUE$1 && PROVIDE in expression) {
  28085. // If this is a provider expression, check for special tokens that need the value
  28086. // during analysis.
  28087. const provide = simplify(expression.provide);
  28088. if (provide === self.ROUTES || provide == self.ANALYZE_FOR_ENTRY_COMPONENTS) {
  28089. return simplify(value);
  28090. }
  28091. }
  28092. return simplifyLazily(value);
  28093. }
  28094. return simplify(value);
  28095. });
  28096. }
  28097. return IGNORE;
  28098. }
  28099. return simplify(value);
  28100. }
  28101. let result;
  28102. try {
  28103. result = simplifyInContext(context, value, 0, lazy ? 1 : 0);
  28104. }
  28105. catch (e) {
  28106. if (this.errorRecorder) {
  28107. this.reportError(e, context);
  28108. }
  28109. else {
  28110. throw formatMetadataError(e, context);
  28111. }
  28112. }
  28113. if (shouldIgnore(result)) {
  28114. return undefined;
  28115. }
  28116. return result;
  28117. }
  28118. getTypeMetadata(type) {
  28119. const resolvedSymbol = this.symbolResolver.resolveSymbol(type);
  28120. return resolvedSymbol && resolvedSymbol.metadata ? resolvedSymbol.metadata :
  28121. { __symbolic: 'class' };
  28122. }
  28123. reportError(error, context, path) {
  28124. if (this.errorRecorder) {
  28125. this.errorRecorder(formatMetadataError(error, context), (context && context.filePath) || path);
  28126. }
  28127. else {
  28128. throw error;
  28129. }
  28130. }
  28131. error({ message, summary, advise, position, context, value, symbol, chain }, reportingContext) {
  28132. this.reportError(metadataError(message, summary, advise, position, symbol, context, chain), reportingContext);
  28133. }
  28134. }
  28135. const METADATA_ERROR = 'ngMetadataError';
  28136. function metadataError(message, summary, advise, position, symbol, context, chain) {
  28137. const error = syntaxError(message);
  28138. error[METADATA_ERROR] = true;
  28139. if (advise)
  28140. error.advise = advise;
  28141. if (position)
  28142. error.position = position;
  28143. if (summary)
  28144. error.summary = summary;
  28145. if (context)
  28146. error.context = context;
  28147. if (chain)
  28148. error.chain = chain;
  28149. if (symbol)
  28150. error.symbol = symbol;
  28151. return error;
  28152. }
  28153. function isMetadataError(error) {
  28154. return !!error[METADATA_ERROR];
  28155. }
  28156. const REFERENCE_TO_NONEXPORTED_CLASS = 'Reference to non-exported class';
  28157. const VARIABLE_NOT_INITIALIZED = 'Variable not initialized';
  28158. const DESTRUCTURE_NOT_SUPPORTED = 'Destructuring not supported';
  28159. const COULD_NOT_RESOLVE_TYPE = 'Could not resolve type';
  28160. const FUNCTION_CALL_NOT_SUPPORTED = 'Function call not supported';
  28161. const REFERENCE_TO_LOCAL_SYMBOL = 'Reference to a local symbol';
  28162. const LAMBDA_NOT_SUPPORTED = 'Lambda not supported';
  28163. function expandedMessage(message, context) {
  28164. switch (message) {
  28165. case REFERENCE_TO_NONEXPORTED_CLASS:
  28166. if (context && context.className) {
  28167. return `References to a non-exported class are not supported in decorators but ${context.className} was referenced.`;
  28168. }
  28169. break;
  28170. case VARIABLE_NOT_INITIALIZED:
  28171. return 'Only initialized variables and constants can be referenced in decorators because the value of this variable is needed by the template compiler';
  28172. case DESTRUCTURE_NOT_SUPPORTED:
  28173. return 'Referencing an exported destructured variable or constant is not supported in decorators and this value is needed by the template compiler';
  28174. case COULD_NOT_RESOLVE_TYPE:
  28175. if (context && context.typeName) {
  28176. return `Could not resolve type ${context.typeName}`;
  28177. }
  28178. break;
  28179. case FUNCTION_CALL_NOT_SUPPORTED:
  28180. if (context && context.name) {
  28181. return `Function calls are not supported in decorators but '${context.name}' was called`;
  28182. }
  28183. return 'Function calls are not supported in decorators';
  28184. case REFERENCE_TO_LOCAL_SYMBOL:
  28185. if (context && context.name) {
  28186. return `Reference to a local (non-exported) symbols are not supported in decorators but '${context.name}' was referenced`;
  28187. }
  28188. break;
  28189. case LAMBDA_NOT_SUPPORTED:
  28190. return `Function expressions are not supported in decorators`;
  28191. }
  28192. return message;
  28193. }
  28194. function messageAdvise(message, context) {
  28195. switch (message) {
  28196. case REFERENCE_TO_NONEXPORTED_CLASS:
  28197. if (context && context.className) {
  28198. return `Consider exporting '${context.className}'`;
  28199. }
  28200. break;
  28201. case DESTRUCTURE_NOT_SUPPORTED:
  28202. return 'Consider simplifying to avoid destructuring';
  28203. case REFERENCE_TO_LOCAL_SYMBOL:
  28204. if (context && context.name) {
  28205. return `Consider exporting '${context.name}'`;
  28206. }
  28207. break;
  28208. case LAMBDA_NOT_SUPPORTED:
  28209. return `Consider changing the function expression into an exported function`;
  28210. }
  28211. return undefined;
  28212. }
  28213. function errorSummary(error) {
  28214. if (error.summary) {
  28215. return error.summary;
  28216. }
  28217. switch (error.message) {
  28218. case REFERENCE_TO_NONEXPORTED_CLASS:
  28219. if (error.context && error.context.className) {
  28220. return `references non-exported class ${error.context.className}`;
  28221. }
  28222. break;
  28223. case VARIABLE_NOT_INITIALIZED:
  28224. return 'is not initialized';
  28225. case DESTRUCTURE_NOT_SUPPORTED:
  28226. return 'is a destructured variable';
  28227. case COULD_NOT_RESOLVE_TYPE:
  28228. return 'could not be resolved';
  28229. case FUNCTION_CALL_NOT_SUPPORTED:
  28230. if (error.context && error.context.name) {
  28231. return `calls '${error.context.name}'`;
  28232. }
  28233. return `calls a function`;
  28234. case REFERENCE_TO_LOCAL_SYMBOL:
  28235. if (error.context && error.context.name) {
  28236. return `references local variable ${error.context.name}`;
  28237. }
  28238. return `references a local variable`;
  28239. }
  28240. return 'contains the error';
  28241. }
  28242. function mapStringMap(input, transform) {
  28243. if (!input)
  28244. return {};
  28245. const result = {};
  28246. Object.keys(input).forEach((key) => {
  28247. const value = transform(input[key], key);
  28248. if (!shouldIgnore(value)) {
  28249. if (HIDDEN_KEY.test(key)) {
  28250. Object.defineProperty(result, key, { enumerable: false, configurable: true, value: value });
  28251. }
  28252. else {
  28253. result[key] = value;
  28254. }
  28255. }
  28256. });
  28257. return result;
  28258. }
  28259. function isPrimitive(o) {
  28260. return o === null || (typeof o !== 'function' && typeof o !== 'object');
  28261. }
  28262. class BindingScope$1 {
  28263. static build() {
  28264. const current = new Map();
  28265. return {
  28266. define: function (name, value) {
  28267. current.set(name, value);
  28268. return this;
  28269. },
  28270. done: function () {
  28271. return current.size > 0 ? new PopulatedScope(current) : BindingScope$1.empty;
  28272. }
  28273. };
  28274. }
  28275. }
  28276. BindingScope$1.missing = {};
  28277. BindingScope$1.empty = { resolve: name => BindingScope$1.missing };
  28278. class PopulatedScope extends BindingScope$1 {
  28279. constructor(bindings) {
  28280. super();
  28281. this.bindings = bindings;
  28282. }
  28283. resolve(name) {
  28284. return this.bindings.has(name) ? this.bindings.get(name) : BindingScope$1.missing;
  28285. }
  28286. }
  28287. function formatMetadataMessageChain(chain, advise) {
  28288. const expanded = expandedMessage(chain.message, chain.context);
  28289. const nesting = chain.symbol ? ` in '${chain.symbol.name}'` : '';
  28290. const message = `${expanded}${nesting}`;
  28291. const position = chain.position;
  28292. const next = chain.next ?
  28293. formatMetadataMessageChain(chain.next, advise) :
  28294. advise ? { message: advise } : undefined;
  28295. return { message, position, next: next ? [next] : undefined };
  28296. }
  28297. function formatMetadataError(e, context) {
  28298. if (isMetadataError(e)) {
  28299. // Produce a formatted version of the and leaving enough information in the original error
  28300. // to recover the formatting information to eventually produce a diagnostic error message.
  28301. const position = e.position;
  28302. const chain = {
  28303. message: `Error during template compile of '${context.name}'`,
  28304. position: position,
  28305. next: { message: e.message, next: e.chain, context: e.context, symbol: e.symbol }
  28306. };
  28307. const advise = e.advise || messageAdvise(e.message, e.context);
  28308. return formattedError(formatMetadataMessageChain(chain, advise));
  28309. }
  28310. return e;
  28311. }
  28312. /**
  28313. * @license
  28314. * Copyright Google LLC All Rights Reserved.
  28315. *
  28316. * Use of this source code is governed by an MIT-style license that can be
  28317. * found in the LICENSE file at https://angular.io/license
  28318. */
  28319. class AotSummaryResolver {
  28320. constructor(host, staticSymbolCache) {
  28321. this.host = host;
  28322. this.staticSymbolCache = staticSymbolCache;
  28323. // Note: this will only contain StaticSymbols without members!
  28324. this.summaryCache = new Map();
  28325. this.loadedFilePaths = new Map();
  28326. // Note: this will only contain StaticSymbols without members!
  28327. this.importAs = new Map();
  28328. this.knownFileNameToModuleNames = new Map();
  28329. }
  28330. isLibraryFile(filePath) {
  28331. // Note: We need to strip the .ngfactory. file path,
  28332. // so this method also works for generated files
  28333. // (for which host.isSourceFile will always return false).
  28334. return !this.host.isSourceFile(stripGeneratedFileSuffix(filePath));
  28335. }
  28336. toSummaryFileName(filePath, referringSrcFileName) {
  28337. return this.host.toSummaryFileName(filePath, referringSrcFileName);
  28338. }
  28339. fromSummaryFileName(fileName, referringLibFileName) {
  28340. return this.host.fromSummaryFileName(fileName, referringLibFileName);
  28341. }
  28342. resolveSummary(staticSymbol) {
  28343. const rootSymbol = staticSymbol.members.length ?
  28344. this.staticSymbolCache.get(staticSymbol.filePath, staticSymbol.name) :
  28345. staticSymbol;
  28346. let summary = this.summaryCache.get(rootSymbol);
  28347. if (!summary) {
  28348. this._loadSummaryFile(staticSymbol.filePath);
  28349. summary = this.summaryCache.get(staticSymbol);
  28350. }
  28351. return (rootSymbol === staticSymbol && summary) || null;
  28352. }
  28353. getSymbolsOf(filePath) {
  28354. if (this._loadSummaryFile(filePath)) {
  28355. return Array.from(this.summaryCache.keys()).filter((symbol) => symbol.filePath === filePath);
  28356. }
  28357. return null;
  28358. }
  28359. getImportAs(staticSymbol) {
  28360. staticSymbol.assertNoMembers();
  28361. return this.importAs.get(staticSymbol);
  28362. }
  28363. /**
  28364. * Converts a file path to a module name that can be used as an `import`.
  28365. */
  28366. getKnownModuleName(importedFilePath) {
  28367. return this.knownFileNameToModuleNames.get(importedFilePath) || null;
  28368. }
  28369. addSummary(summary) {
  28370. this.summaryCache.set(summary.symbol, summary);
  28371. }
  28372. _loadSummaryFile(filePath) {
  28373. let hasSummary = this.loadedFilePaths.get(filePath);
  28374. if (hasSummary != null) {
  28375. return hasSummary;
  28376. }
  28377. let json = null;
  28378. if (this.isLibraryFile(filePath)) {
  28379. const summaryFilePath = summaryFileName(filePath);
  28380. try {
  28381. json = this.host.loadSummary(summaryFilePath);
  28382. }
  28383. catch (e) {
  28384. console.error(`Error loading summary file ${summaryFilePath}`);
  28385. throw e;
  28386. }
  28387. }
  28388. hasSummary = json != null;
  28389. this.loadedFilePaths.set(filePath, hasSummary);
  28390. if (json) {
  28391. const { moduleName, summaries, importAs } = deserializeSummaries(this.staticSymbolCache, this, filePath, json);
  28392. summaries.forEach((summary) => this.summaryCache.set(summary.symbol, summary));
  28393. if (moduleName) {
  28394. this.knownFileNameToModuleNames.set(filePath, moduleName);
  28395. }
  28396. importAs.forEach((importAs) => {
  28397. this.importAs.set(importAs.symbol, importAs.importAs);
  28398. });
  28399. }
  28400. return hasSummary;
  28401. }
  28402. }
  28403. /**
  28404. * @license
  28405. * Copyright Google LLC All Rights Reserved.
  28406. *
  28407. * Use of this source code is governed by an MIT-style license that can be
  28408. * found in the LICENSE file at https://angular.io/license
  28409. */
  28410. function createAotUrlResolver(host) {
  28411. return {
  28412. resolve: (basePath, url) => {
  28413. const filePath = host.resourceNameToFileName(url, basePath);
  28414. if (!filePath) {
  28415. throw syntaxError(`Couldn't resolve resource ${url} from ${basePath}`);
  28416. }
  28417. return filePath;
  28418. }
  28419. };
  28420. }
  28421. /**
  28422. * Creates a new AotCompiler based on options and a host.
  28423. */
  28424. function createAotCompiler(compilerHost, options, errorCollector) {
  28425. let translations = options.translations || '';
  28426. const urlResolver = createAotUrlResolver(compilerHost);
  28427. const symbolCache = new StaticSymbolCache();
  28428. const summaryResolver = new AotSummaryResolver(compilerHost, symbolCache);
  28429. const symbolResolver = new StaticSymbolResolver(compilerHost, symbolCache, summaryResolver);
  28430. const staticReflector = new StaticReflector(summaryResolver, symbolResolver, [], [], errorCollector);
  28431. let htmlParser;
  28432. if (!!options.enableIvy) {
  28433. // Ivy handles i18n at the compiler level so we must use a regular parser
  28434. htmlParser = new HtmlParser();
  28435. }
  28436. else {
  28437. htmlParser = new I18NHtmlParser(new HtmlParser(), translations, options.i18nFormat, options.missingTranslation, console);
  28438. }
  28439. const config = new CompilerConfig({
  28440. defaultEncapsulation: ViewEncapsulation.Emulated,
  28441. useJit: false,
  28442. missingTranslation: options.missingTranslation,
  28443. preserveWhitespaces: options.preserveWhitespaces,
  28444. strictInjectionParameters: options.strictInjectionParameters,
  28445. });
  28446. const normalizer = new DirectiveNormalizer({ get: (url) => compilerHost.loadResource(url) }, urlResolver, htmlParser, config);
  28447. const expressionParser = new Parser$1(new Lexer());
  28448. const elementSchemaRegistry = new DomElementSchemaRegistry();
  28449. const tmplParser = new TemplateParser(config, staticReflector, expressionParser, elementSchemaRegistry, htmlParser, console, []);
  28450. const resolver = new CompileMetadataResolver(config, htmlParser, new NgModuleResolver(staticReflector), new DirectiveResolver(staticReflector), new PipeResolver(staticReflector), summaryResolver, elementSchemaRegistry, normalizer, console, symbolCache, staticReflector, errorCollector);
  28451. // TODO(vicb): do not pass options.i18nFormat here
  28452. const viewCompiler = new ViewCompiler(staticReflector);
  28453. const typeCheckCompiler = new TypeCheckCompiler(options, staticReflector);
  28454. const compiler = new AotCompiler(config, options, compilerHost, staticReflector, resolver, tmplParser, new StyleCompiler(urlResolver), viewCompiler, typeCheckCompiler, new NgModuleCompiler(staticReflector), new InjectableCompiler(staticReflector, !!options.enableIvy), new TypeScriptEmitter(), summaryResolver, symbolResolver);
  28455. return { compiler, reflector: staticReflector };
  28456. }
  28457. /**
  28458. * @license
  28459. * Copyright Google LLC All Rights Reserved.
  28460. *
  28461. * Use of this source code is governed by an MIT-style license that can be
  28462. * found in the LICENSE file at https://angular.io/license
  28463. */
  28464. /**
  28465. * @license
  28466. * Copyright Google LLC All Rights Reserved.
  28467. *
  28468. * Use of this source code is governed by an MIT-style license that can be
  28469. * found in the LICENSE file at https://angular.io/license
  28470. */
  28471. /**
  28472. * @license
  28473. * Copyright Google LLC All Rights Reserved.
  28474. *
  28475. * Use of this source code is governed by an MIT-style license that can be
  28476. * found in the LICENSE file at https://angular.io/license
  28477. */
  28478. class SummaryResolver {
  28479. }
  28480. class JitSummaryResolver {
  28481. constructor() {
  28482. this._summaries = new Map();
  28483. }
  28484. isLibraryFile() {
  28485. return false;
  28486. }
  28487. toSummaryFileName(fileName) {
  28488. return fileName;
  28489. }
  28490. fromSummaryFileName(fileName) {
  28491. return fileName;
  28492. }
  28493. resolveSummary(reference) {
  28494. return this._summaries.get(reference) || null;
  28495. }
  28496. getSymbolsOf() {
  28497. return [];
  28498. }
  28499. getImportAs(reference) {
  28500. return reference;
  28501. }
  28502. getKnownModuleName(fileName) {
  28503. return null;
  28504. }
  28505. addSummary(summary) {
  28506. this._summaries.set(summary.symbol, summary);
  28507. }
  28508. }
  28509. function interpretStatements(statements, reflector) {
  28510. const ctx = new _ExecutionContext(null, null, null, new Map());
  28511. const visitor = new StatementInterpreter(reflector);
  28512. visitor.visitAllStatements(statements, ctx);
  28513. const result = {};
  28514. ctx.exports.forEach((exportName) => {
  28515. result[exportName] = ctx.vars.get(exportName);
  28516. });
  28517. return result;
  28518. }
  28519. function _executeFunctionStatements(varNames, varValues, statements, ctx, visitor) {
  28520. const childCtx = ctx.createChildWihtLocalVars();
  28521. for (let i = 0; i < varNames.length; i++) {
  28522. childCtx.vars.set(varNames[i], varValues[i]);
  28523. }
  28524. const result = visitor.visitAllStatements(statements, childCtx);
  28525. return result ? result.value : null;
  28526. }
  28527. class _ExecutionContext {
  28528. constructor(parent, instance, className, vars) {
  28529. this.parent = parent;
  28530. this.instance = instance;
  28531. this.className = className;
  28532. this.vars = vars;
  28533. this.exports = [];
  28534. }
  28535. createChildWihtLocalVars() {
  28536. return new _ExecutionContext(this, this.instance, this.className, new Map());
  28537. }
  28538. }
  28539. class ReturnValue {
  28540. constructor(value) {
  28541. this.value = value;
  28542. }
  28543. }
  28544. function createDynamicClass(_classStmt, _ctx, _visitor) {
  28545. const propertyDescriptors = {};
  28546. _classStmt.getters.forEach((getter) => {
  28547. // Note: use `function` instead of arrow function to capture `this`
  28548. propertyDescriptors[getter.name] = {
  28549. configurable: false,
  28550. get: function () {
  28551. const instanceCtx = new _ExecutionContext(_ctx, this, _classStmt.name, _ctx.vars);
  28552. return _executeFunctionStatements([], [], getter.body, instanceCtx, _visitor);
  28553. }
  28554. };
  28555. });
  28556. _classStmt.methods.forEach(function (method) {
  28557. const paramNames = method.params.map(param => param.name);
  28558. // Note: use `function` instead of arrow function to capture `this`
  28559. propertyDescriptors[method.name] = {
  28560. writable: false,
  28561. configurable: false,
  28562. value: function (...args) {
  28563. const instanceCtx = new _ExecutionContext(_ctx, this, _classStmt.name, _ctx.vars);
  28564. return _executeFunctionStatements(paramNames, args, method.body, instanceCtx, _visitor);
  28565. }
  28566. };
  28567. });
  28568. const ctorParamNames = _classStmt.constructorMethod.params.map(param => param.name);
  28569. // Note: use `function` instead of arrow function to capture `this`
  28570. const ctor = function (...args) {
  28571. const instanceCtx = new _ExecutionContext(_ctx, this, _classStmt.name, _ctx.vars);
  28572. _classStmt.fields.forEach((field) => {
  28573. this[field.name] = undefined;
  28574. });
  28575. _executeFunctionStatements(ctorParamNames, args, _classStmt.constructorMethod.body, instanceCtx, _visitor);
  28576. };
  28577. const superClass = _classStmt.parent ? _classStmt.parent.visitExpression(_visitor, _ctx) : Object;
  28578. ctor.prototype = Object.create(superClass.prototype, propertyDescriptors);
  28579. return ctor;
  28580. }
  28581. class StatementInterpreter {
  28582. constructor(reflector) {
  28583. this.reflector = reflector;
  28584. }
  28585. debugAst(ast) {
  28586. return debugOutputAstAsTypeScript(ast);
  28587. }
  28588. visitDeclareVarStmt(stmt, ctx) {
  28589. const initialValue = stmt.value ? stmt.value.visitExpression(this, ctx) : undefined;
  28590. ctx.vars.set(stmt.name, initialValue);
  28591. if (stmt.hasModifier(StmtModifier.Exported)) {
  28592. ctx.exports.push(stmt.name);
  28593. }
  28594. return null;
  28595. }
  28596. visitWriteVarExpr(expr, ctx) {
  28597. const value = expr.value.visitExpression(this, ctx);
  28598. let currCtx = ctx;
  28599. while (currCtx != null) {
  28600. if (currCtx.vars.has(expr.name)) {
  28601. currCtx.vars.set(expr.name, value);
  28602. return value;
  28603. }
  28604. currCtx = currCtx.parent;
  28605. }
  28606. throw new Error(`Not declared variable ${expr.name}`);
  28607. }
  28608. visitWrappedNodeExpr(ast, ctx) {
  28609. throw new Error('Cannot interpret a WrappedNodeExpr.');
  28610. }
  28611. visitTypeofExpr(ast, ctx) {
  28612. throw new Error('Cannot interpret a TypeofExpr');
  28613. }
  28614. visitReadVarExpr(ast, ctx) {
  28615. let varName = ast.name;
  28616. if (ast.builtin != null) {
  28617. switch (ast.builtin) {
  28618. case BuiltinVar.Super:
  28619. return Object.getPrototypeOf(ctx.instance);
  28620. case BuiltinVar.This:
  28621. return ctx.instance;
  28622. case BuiltinVar.CatchError:
  28623. varName = CATCH_ERROR_VAR$2;
  28624. break;
  28625. case BuiltinVar.CatchStack:
  28626. varName = CATCH_STACK_VAR$2;
  28627. break;
  28628. default:
  28629. throw new Error(`Unknown builtin variable ${ast.builtin}`);
  28630. }
  28631. }
  28632. let currCtx = ctx;
  28633. while (currCtx != null) {
  28634. if (currCtx.vars.has(varName)) {
  28635. return currCtx.vars.get(varName);
  28636. }
  28637. currCtx = currCtx.parent;
  28638. }
  28639. throw new Error(`Not declared variable ${varName}`);
  28640. }
  28641. visitWriteKeyExpr(expr, ctx) {
  28642. const receiver = expr.receiver.visitExpression(this, ctx);
  28643. const index = expr.index.visitExpression(this, ctx);
  28644. const value = expr.value.visitExpression(this, ctx);
  28645. receiver[index] = value;
  28646. return value;
  28647. }
  28648. visitWritePropExpr(expr, ctx) {
  28649. const receiver = expr.receiver.visitExpression(this, ctx);
  28650. const value = expr.value.visitExpression(this, ctx);
  28651. receiver[expr.name] = value;
  28652. return value;
  28653. }
  28654. visitInvokeMethodExpr(expr, ctx) {
  28655. const receiver = expr.receiver.visitExpression(this, ctx);
  28656. const args = this.visitAllExpressions(expr.args, ctx);
  28657. let result;
  28658. if (expr.builtin != null) {
  28659. switch (expr.builtin) {
  28660. case BuiltinMethod.ConcatArray:
  28661. result = receiver.concat(...args);
  28662. break;
  28663. case BuiltinMethod.SubscribeObservable:
  28664. result = receiver.subscribe({ next: args[0] });
  28665. break;
  28666. case BuiltinMethod.Bind:
  28667. result = receiver.bind(...args);
  28668. break;
  28669. default:
  28670. throw new Error(`Unknown builtin method ${expr.builtin}`);
  28671. }
  28672. }
  28673. else {
  28674. result = receiver[expr.name].apply(receiver, args);
  28675. }
  28676. return result;
  28677. }
  28678. visitInvokeFunctionExpr(stmt, ctx) {
  28679. const args = this.visitAllExpressions(stmt.args, ctx);
  28680. const fnExpr = stmt.fn;
  28681. if (fnExpr instanceof ReadVarExpr && fnExpr.builtin === BuiltinVar.Super) {
  28682. ctx.instance.constructor.prototype.constructor.apply(ctx.instance, args);
  28683. return null;
  28684. }
  28685. else {
  28686. const fn = stmt.fn.visitExpression(this, ctx);
  28687. return fn.apply(null, args);
  28688. }
  28689. }
  28690. visitTaggedTemplateExpr(expr, ctx) {
  28691. const templateElements = expr.template.elements.map((e) => e.text);
  28692. Object.defineProperty(templateElements, 'raw', { value: expr.template.elements.map((e) => e.rawText) });
  28693. const args = this.visitAllExpressions(expr.template.expressions, ctx);
  28694. args.unshift(templateElements);
  28695. const tag = expr.tag.visitExpression(this, ctx);
  28696. return tag.apply(null, args);
  28697. }
  28698. visitReturnStmt(stmt, ctx) {
  28699. return new ReturnValue(stmt.value.visitExpression(this, ctx));
  28700. }
  28701. visitDeclareClassStmt(stmt, ctx) {
  28702. const clazz = createDynamicClass(stmt, ctx, this);
  28703. ctx.vars.set(stmt.name, clazz);
  28704. if (stmt.hasModifier(StmtModifier.Exported)) {
  28705. ctx.exports.push(stmt.name);
  28706. }
  28707. return null;
  28708. }
  28709. visitExpressionStmt(stmt, ctx) {
  28710. return stmt.expr.visitExpression(this, ctx);
  28711. }
  28712. visitIfStmt(stmt, ctx) {
  28713. const condition = stmt.condition.visitExpression(this, ctx);
  28714. if (condition) {
  28715. return this.visitAllStatements(stmt.trueCase, ctx);
  28716. }
  28717. else if (stmt.falseCase != null) {
  28718. return this.visitAllStatements(stmt.falseCase, ctx);
  28719. }
  28720. return null;
  28721. }
  28722. visitTryCatchStmt(stmt, ctx) {
  28723. try {
  28724. return this.visitAllStatements(stmt.bodyStmts, ctx);
  28725. }
  28726. catch (e) {
  28727. const childCtx = ctx.createChildWihtLocalVars();
  28728. childCtx.vars.set(CATCH_ERROR_VAR$2, e);
  28729. childCtx.vars.set(CATCH_STACK_VAR$2, e.stack);
  28730. return this.visitAllStatements(stmt.catchStmts, childCtx);
  28731. }
  28732. }
  28733. visitThrowStmt(stmt, ctx) {
  28734. throw stmt.error.visitExpression(this, ctx);
  28735. }
  28736. visitInstantiateExpr(ast, ctx) {
  28737. const args = this.visitAllExpressions(ast.args, ctx);
  28738. const clazz = ast.classExpr.visitExpression(this, ctx);
  28739. return new clazz(...args);
  28740. }
  28741. visitLiteralExpr(ast, ctx) {
  28742. return ast.value;
  28743. }
  28744. visitLocalizedString(ast, context) {
  28745. return null;
  28746. }
  28747. visitExternalExpr(ast, ctx) {
  28748. return this.reflector.resolveExternalReference(ast.value);
  28749. }
  28750. visitConditionalExpr(ast, ctx) {
  28751. if (ast.condition.visitExpression(this, ctx)) {
  28752. return ast.trueCase.visitExpression(this, ctx);
  28753. }
  28754. else if (ast.falseCase != null) {
  28755. return ast.falseCase.visitExpression(this, ctx);
  28756. }
  28757. return null;
  28758. }
  28759. visitNotExpr(ast, ctx) {
  28760. return !ast.condition.visitExpression(this, ctx);
  28761. }
  28762. visitAssertNotNullExpr(ast, ctx) {
  28763. return ast.condition.visitExpression(this, ctx);
  28764. }
  28765. visitCastExpr(ast, ctx) {
  28766. return ast.value.visitExpression(this, ctx);
  28767. }
  28768. visitFunctionExpr(ast, ctx) {
  28769. const paramNames = ast.params.map((param) => param.name);
  28770. return _declareFn(paramNames, ast.statements, ctx, this);
  28771. }
  28772. visitDeclareFunctionStmt(stmt, ctx) {
  28773. const paramNames = stmt.params.map((param) => param.name);
  28774. ctx.vars.set(stmt.name, _declareFn(paramNames, stmt.statements, ctx, this));
  28775. if (stmt.hasModifier(StmtModifier.Exported)) {
  28776. ctx.exports.push(stmt.name);
  28777. }
  28778. return null;
  28779. }
  28780. visitUnaryOperatorExpr(ast, ctx) {
  28781. const rhs = () => ast.expr.visitExpression(this, ctx);
  28782. switch (ast.operator) {
  28783. case UnaryOperator.Plus:
  28784. return +rhs();
  28785. case UnaryOperator.Minus:
  28786. return -rhs();
  28787. default:
  28788. throw new Error(`Unknown operator ${ast.operator}`);
  28789. }
  28790. }
  28791. visitBinaryOperatorExpr(ast, ctx) {
  28792. var _a;
  28793. const lhs = () => ast.lhs.visitExpression(this, ctx);
  28794. const rhs = () => ast.rhs.visitExpression(this, ctx);
  28795. switch (ast.operator) {
  28796. case BinaryOperator.Equals:
  28797. return lhs() == rhs();
  28798. case BinaryOperator.Identical:
  28799. return lhs() === rhs();
  28800. case BinaryOperator.NotEquals:
  28801. return lhs() != rhs();
  28802. case BinaryOperator.NotIdentical:
  28803. return lhs() !== rhs();
  28804. case BinaryOperator.And:
  28805. return lhs() && rhs();
  28806. case BinaryOperator.Or:
  28807. return lhs() || rhs();
  28808. case BinaryOperator.Plus:
  28809. return lhs() + rhs();
  28810. case BinaryOperator.Minus:
  28811. return lhs() - rhs();
  28812. case BinaryOperator.Divide:
  28813. return lhs() / rhs();
  28814. case BinaryOperator.Multiply:
  28815. return lhs() * rhs();
  28816. case BinaryOperator.Modulo:
  28817. return lhs() % rhs();
  28818. case BinaryOperator.Lower:
  28819. return lhs() < rhs();
  28820. case BinaryOperator.LowerEquals:
  28821. return lhs() <= rhs();
  28822. case BinaryOperator.Bigger:
  28823. return lhs() > rhs();
  28824. case BinaryOperator.BiggerEquals:
  28825. return lhs() >= rhs();
  28826. case BinaryOperator.NullishCoalesce:
  28827. return (_a = lhs()) !== null && _a !== void 0 ? _a : rhs();
  28828. default:
  28829. throw new Error(`Unknown operator ${ast.operator}`);
  28830. }
  28831. }
  28832. visitReadPropExpr(ast, ctx) {
  28833. let result;
  28834. const receiver = ast.receiver.visitExpression(this, ctx);
  28835. result = receiver[ast.name];
  28836. return result;
  28837. }
  28838. visitReadKeyExpr(ast, ctx) {
  28839. const receiver = ast.receiver.visitExpression(this, ctx);
  28840. const prop = ast.index.visitExpression(this, ctx);
  28841. return receiver[prop];
  28842. }
  28843. visitLiteralArrayExpr(ast, ctx) {
  28844. return this.visitAllExpressions(ast.entries, ctx);
  28845. }
  28846. visitLiteralMapExpr(ast, ctx) {
  28847. const result = {};
  28848. ast.entries.forEach(entry => result[entry.key] = entry.value.visitExpression(this, ctx));
  28849. return result;
  28850. }
  28851. visitCommaExpr(ast, context) {
  28852. const values = this.visitAllExpressions(ast.parts, context);
  28853. return values[values.length - 1];
  28854. }
  28855. visitAllExpressions(expressions, ctx) {
  28856. return expressions.map((expr) => expr.visitExpression(this, ctx));
  28857. }
  28858. visitAllStatements(statements, ctx) {
  28859. for (let i = 0; i < statements.length; i++) {
  28860. const stmt = statements[i];
  28861. const val = stmt.visitStatement(this, ctx);
  28862. if (val instanceof ReturnValue) {
  28863. return val;
  28864. }
  28865. }
  28866. return null;
  28867. }
  28868. }
  28869. function _declareFn(varNames, statements, ctx, visitor) {
  28870. return (...args) => _executeFunctionStatements(varNames, args, statements, ctx, visitor);
  28871. }
  28872. const CATCH_ERROR_VAR$2 = 'error';
  28873. const CATCH_STACK_VAR$2 = 'stack';
  28874. /**
  28875. * @license
  28876. * Copyright Google LLC All Rights Reserved.
  28877. *
  28878. * Use of this source code is governed by an MIT-style license that can be
  28879. * found in the LICENSE file at https://angular.io/license
  28880. */
  28881. /**
  28882. * An internal module of the Angular compiler that begins with component types,
  28883. * extracts templates, and eventually produces a compiled version of the component
  28884. * ready for linking into an application.
  28885. *
  28886. * @security When compiling templates at runtime, you must ensure that the entire template comes
  28887. * from a trusted source. Attacker-controlled data introduced by a template could expose your
  28888. * application to XSS risks. For more detail, see the [Security Guide](https://g.co/ng/security).
  28889. */
  28890. class JitCompiler {
  28891. constructor(_metadataResolver, _templateParser, _styleCompiler, _viewCompiler, _ngModuleCompiler, _summaryResolver, _reflector, _jitEvaluator, _compilerConfig, _console, getExtraNgModuleProviders) {
  28892. this._metadataResolver = _metadataResolver;
  28893. this._templateParser = _templateParser;
  28894. this._styleCompiler = _styleCompiler;
  28895. this._viewCompiler = _viewCompiler;
  28896. this._ngModuleCompiler = _ngModuleCompiler;
  28897. this._summaryResolver = _summaryResolver;
  28898. this._reflector = _reflector;
  28899. this._jitEvaluator = _jitEvaluator;
  28900. this._compilerConfig = _compilerConfig;
  28901. this._console = _console;
  28902. this.getExtraNgModuleProviders = getExtraNgModuleProviders;
  28903. this._compiledTemplateCache = new Map();
  28904. this._compiledHostTemplateCache = new Map();
  28905. this._compiledDirectiveWrapperCache = new Map();
  28906. this._compiledNgModuleCache = new Map();
  28907. this._sharedStylesheetCount = 0;
  28908. this._addedAotSummaries = new Set();
  28909. }
  28910. compileModuleSync(moduleType) {
  28911. return SyncAsync.assertSync(this._compileModuleAndComponents(moduleType, true));
  28912. }
  28913. compileModuleAsync(moduleType) {
  28914. return Promise.resolve(this._compileModuleAndComponents(moduleType, false));
  28915. }
  28916. compileModuleAndAllComponentsSync(moduleType) {
  28917. return SyncAsync.assertSync(this._compileModuleAndAllComponents(moduleType, true));
  28918. }
  28919. compileModuleAndAllComponentsAsync(moduleType) {
  28920. return Promise.resolve(this._compileModuleAndAllComponents(moduleType, false));
  28921. }
  28922. getComponentFactory(component) {
  28923. const summary = this._metadataResolver.getDirectiveSummary(component);
  28924. return summary.componentFactory;
  28925. }
  28926. loadAotSummaries(summaries) {
  28927. this.clearCache();
  28928. this._addAotSummaries(summaries);
  28929. }
  28930. _addAotSummaries(fn) {
  28931. if (this._addedAotSummaries.has(fn)) {
  28932. return;
  28933. }
  28934. this._addedAotSummaries.add(fn);
  28935. const summaries = fn();
  28936. for (let i = 0; i < summaries.length; i++) {
  28937. const entry = summaries[i];
  28938. if (typeof entry === 'function') {
  28939. this._addAotSummaries(entry);
  28940. }
  28941. else {
  28942. const summary = entry;
  28943. this._summaryResolver.addSummary({ symbol: summary.type.reference, metadata: null, type: summary });
  28944. }
  28945. }
  28946. }
  28947. hasAotSummary(ref) {
  28948. return !!this._summaryResolver.resolveSummary(ref);
  28949. }
  28950. _filterJitIdentifiers(ids) {
  28951. return ids.map(mod => mod.reference).filter((ref) => !this.hasAotSummary(ref));
  28952. }
  28953. _compileModuleAndComponents(moduleType, isSync) {
  28954. return SyncAsync.then(this._loadModules(moduleType, isSync), () => {
  28955. this._compileComponents(moduleType, null);
  28956. return this._compileModule(moduleType);
  28957. });
  28958. }
  28959. _compileModuleAndAllComponents(moduleType, isSync) {
  28960. return SyncAsync.then(this._loadModules(moduleType, isSync), () => {
  28961. const componentFactories = [];
  28962. this._compileComponents(moduleType, componentFactories);
  28963. return {
  28964. ngModuleFactory: this._compileModule(moduleType),
  28965. componentFactories: componentFactories
  28966. };
  28967. });
  28968. }
  28969. _loadModules(mainModule, isSync) {
  28970. const loading = [];
  28971. const mainNgModule = this._metadataResolver.getNgModuleMetadata(mainModule);
  28972. // Note: for runtime compilation, we want to transitively compile all modules,
  28973. // so we also need to load the declared directives / pipes for all nested modules.
  28974. this._filterJitIdentifiers(mainNgModule.transitiveModule.modules).forEach((nestedNgModule) => {
  28975. // getNgModuleMetadata only returns null if the value passed in is not an NgModule
  28976. const moduleMeta = this._metadataResolver.getNgModuleMetadata(nestedNgModule);
  28977. this._filterJitIdentifiers(moduleMeta.declaredDirectives).forEach((ref) => {
  28978. const promise = this._metadataResolver.loadDirectiveMetadata(moduleMeta.type.reference, ref, isSync);
  28979. if (promise) {
  28980. loading.push(promise);
  28981. }
  28982. });
  28983. this._filterJitIdentifiers(moduleMeta.declaredPipes)
  28984. .forEach((ref) => this._metadataResolver.getOrLoadPipeMetadata(ref));
  28985. });
  28986. return SyncAsync.all(loading);
  28987. }
  28988. _compileModule(moduleType) {
  28989. let ngModuleFactory = this._compiledNgModuleCache.get(moduleType);
  28990. if (!ngModuleFactory) {
  28991. const moduleMeta = this._metadataResolver.getNgModuleMetadata(moduleType);
  28992. // Always provide a bound Compiler
  28993. const extraProviders = this.getExtraNgModuleProviders(moduleMeta.type.reference);
  28994. const outputCtx = createOutputContext();
  28995. const compileResult = this._ngModuleCompiler.compile(outputCtx, moduleMeta, extraProviders);
  28996. ngModuleFactory = this._interpretOrJit(ngModuleJitUrl(moduleMeta), outputCtx.statements)[compileResult.ngModuleFactoryVar];
  28997. this._compiledNgModuleCache.set(moduleMeta.type.reference, ngModuleFactory);
  28998. }
  28999. return ngModuleFactory;
  29000. }
  29001. /**
  29002. * @internal
  29003. */
  29004. _compileComponents(mainModule, allComponentFactories) {
  29005. const ngModule = this._metadataResolver.getNgModuleMetadata(mainModule);
  29006. const moduleByJitDirective = new Map();
  29007. const templates = new Set();
  29008. const transJitModules = this._filterJitIdentifiers(ngModule.transitiveModule.modules);
  29009. transJitModules.forEach((localMod) => {
  29010. const localModuleMeta = this._metadataResolver.getNgModuleMetadata(localMod);
  29011. this._filterJitIdentifiers(localModuleMeta.declaredDirectives).forEach((dirRef) => {
  29012. moduleByJitDirective.set(dirRef, localModuleMeta);
  29013. const dirMeta = this._metadataResolver.getDirectiveMetadata(dirRef);
  29014. if (dirMeta.isComponent) {
  29015. templates.add(this._createCompiledTemplate(dirMeta, localModuleMeta));
  29016. if (allComponentFactories) {
  29017. const template = this._createCompiledHostTemplate(dirMeta.type.reference, localModuleMeta);
  29018. templates.add(template);
  29019. allComponentFactories.push(dirMeta.componentFactory);
  29020. }
  29021. }
  29022. });
  29023. });
  29024. transJitModules.forEach((localMod) => {
  29025. const localModuleMeta = this._metadataResolver.getNgModuleMetadata(localMod);
  29026. this._filterJitIdentifiers(localModuleMeta.declaredDirectives).forEach((dirRef) => {
  29027. const dirMeta = this._metadataResolver.getDirectiveMetadata(dirRef);
  29028. if (dirMeta.isComponent) {
  29029. dirMeta.entryComponents.forEach((entryComponentType) => {
  29030. const moduleMeta = moduleByJitDirective.get(entryComponentType.componentType);
  29031. templates.add(this._createCompiledHostTemplate(entryComponentType.componentType, moduleMeta));
  29032. });
  29033. }
  29034. });
  29035. localModuleMeta.entryComponents.forEach((entryComponentType) => {
  29036. if (!this.hasAotSummary(entryComponentType.componentType)) {
  29037. const moduleMeta = moduleByJitDirective.get(entryComponentType.componentType);
  29038. templates.add(this._createCompiledHostTemplate(entryComponentType.componentType, moduleMeta));
  29039. }
  29040. });
  29041. });
  29042. templates.forEach((template) => this._compileTemplate(template));
  29043. }
  29044. clearCacheFor(type) {
  29045. this._compiledNgModuleCache.delete(type);
  29046. this._metadataResolver.clearCacheFor(type);
  29047. this._compiledHostTemplateCache.delete(type);
  29048. const compiledTemplate = this._compiledTemplateCache.get(type);
  29049. if (compiledTemplate) {
  29050. this._compiledTemplateCache.delete(type);
  29051. }
  29052. }
  29053. clearCache() {
  29054. // Note: don't clear the _addedAotSummaries, as they don't change!
  29055. this._metadataResolver.clearCache();
  29056. this._compiledTemplateCache.clear();
  29057. this._compiledHostTemplateCache.clear();
  29058. this._compiledNgModuleCache.clear();
  29059. }
  29060. _createCompiledHostTemplate(compType, ngModule) {
  29061. if (!ngModule) {
  29062. throw new Error(`Component ${stringify(compType)} is not part of any NgModule or the module has not been imported into your module.`);
  29063. }
  29064. let compiledTemplate = this._compiledHostTemplateCache.get(compType);
  29065. if (!compiledTemplate) {
  29066. const compMeta = this._metadataResolver.getDirectiveMetadata(compType);
  29067. assertComponent(compMeta);
  29068. const hostMeta = this._metadataResolver.getHostComponentMetadata(compMeta, compMeta.componentFactory.viewDefFactory);
  29069. compiledTemplate =
  29070. new CompiledTemplate(true, compMeta.type, hostMeta, ngModule, [compMeta.type]);
  29071. this._compiledHostTemplateCache.set(compType, compiledTemplate);
  29072. }
  29073. return compiledTemplate;
  29074. }
  29075. _createCompiledTemplate(compMeta, ngModule) {
  29076. let compiledTemplate = this._compiledTemplateCache.get(compMeta.type.reference);
  29077. if (!compiledTemplate) {
  29078. assertComponent(compMeta);
  29079. compiledTemplate = new CompiledTemplate(false, compMeta.type, compMeta, ngModule, ngModule.transitiveModule.directives);
  29080. this._compiledTemplateCache.set(compMeta.type.reference, compiledTemplate);
  29081. }
  29082. return compiledTemplate;
  29083. }
  29084. _compileTemplate(template) {
  29085. if (template.isCompiled) {
  29086. return;
  29087. }
  29088. const compMeta = template.compMeta;
  29089. const externalStylesheetsByModuleUrl = new Map();
  29090. const outputContext = createOutputContext();
  29091. const componentStylesheet = this._styleCompiler.compileComponent(outputContext, compMeta);
  29092. compMeta.template.externalStylesheets.forEach((stylesheetMeta) => {
  29093. const compiledStylesheet = this._styleCompiler.compileStyles(createOutputContext(), compMeta, stylesheetMeta);
  29094. externalStylesheetsByModuleUrl.set(stylesheetMeta.moduleUrl, compiledStylesheet);
  29095. });
  29096. this._resolveStylesCompileResult(componentStylesheet, externalStylesheetsByModuleUrl);
  29097. const pipes = template.ngModule.transitiveModule.pipes.map(pipe => this._metadataResolver.getPipeSummary(pipe.reference));
  29098. const { template: parsedTemplate, pipes: usedPipes } = this._parseTemplate(compMeta, template.ngModule, template.directives);
  29099. const compileResult = this._viewCompiler.compileComponent(outputContext, compMeta, parsedTemplate, variable(componentStylesheet.stylesVar), usedPipes);
  29100. const evalResult = this._interpretOrJit(templateJitUrl(template.ngModule.type, template.compMeta), outputContext.statements);
  29101. const viewClass = evalResult[compileResult.viewClassVar];
  29102. const rendererType = evalResult[compileResult.rendererTypeVar];
  29103. template.compiled(viewClass, rendererType);
  29104. }
  29105. _parseTemplate(compMeta, ngModule, directiveIdentifiers) {
  29106. // Note: ! is ok here as components always have a template.
  29107. const preserveWhitespaces = compMeta.template.preserveWhitespaces;
  29108. const directives = directiveIdentifiers.map(dir => this._metadataResolver.getDirectiveSummary(dir.reference));
  29109. const pipes = ngModule.transitiveModule.pipes.map(pipe => this._metadataResolver.getPipeSummary(pipe.reference));
  29110. return this._templateParser.parse(compMeta, compMeta.template.htmlAst, directives, pipes, ngModule.schemas, templateSourceUrl(ngModule.type, compMeta, compMeta.template), preserveWhitespaces);
  29111. }
  29112. _resolveStylesCompileResult(result, externalStylesheetsByModuleUrl) {
  29113. result.dependencies.forEach((dep, i) => {
  29114. const nestedCompileResult = externalStylesheetsByModuleUrl.get(dep.moduleUrl);
  29115. const nestedStylesArr = this._resolveAndEvalStylesCompileResult(nestedCompileResult, externalStylesheetsByModuleUrl);
  29116. dep.setValue(nestedStylesArr);
  29117. });
  29118. }
  29119. _resolveAndEvalStylesCompileResult(result, externalStylesheetsByModuleUrl) {
  29120. this._resolveStylesCompileResult(result, externalStylesheetsByModuleUrl);
  29121. return this._interpretOrJit(sharedStylesheetJitUrl(result.meta, this._sharedStylesheetCount++), result.outputCtx.statements)[result.stylesVar];
  29122. }
  29123. _interpretOrJit(sourceUrl, statements) {
  29124. if (!this._compilerConfig.useJit) {
  29125. return interpretStatements(statements, this._reflector);
  29126. }
  29127. else {
  29128. return this._jitEvaluator.evaluateStatements(sourceUrl, statements, this._reflector, this._compilerConfig.jitDevMode);
  29129. }
  29130. }
  29131. }
  29132. class CompiledTemplate {
  29133. constructor(isHost, compType, compMeta, ngModule, directives) {
  29134. this.isHost = isHost;
  29135. this.compType = compType;
  29136. this.compMeta = compMeta;
  29137. this.ngModule = ngModule;
  29138. this.directives = directives;
  29139. this._viewClass = null;
  29140. this.isCompiled = false;
  29141. }
  29142. compiled(viewClass, rendererType) {
  29143. this._viewClass = viewClass;
  29144. this.compMeta.componentViewType.setDelegate(viewClass);
  29145. for (let prop in rendererType) {
  29146. this.compMeta.rendererType[prop] = rendererType[prop];
  29147. }
  29148. this.isCompiled = true;
  29149. }
  29150. }
  29151. function assertComponent(meta) {
  29152. if (!meta.isComponent) {
  29153. throw new Error(`Could not compile '${identifierName(meta.type)}' because it is not a component.`);
  29154. }
  29155. }
  29156. function createOutputContext() {
  29157. const importExpr$1 = (symbol) => importExpr({ name: identifierName(symbol), moduleName: null, runtime: symbol });
  29158. return { statements: [], genFilePath: '', importExpr: importExpr$1, constantPool: new ConstantPool() };
  29159. }
  29160. /**
  29161. * @license
  29162. * Copyright Google LLC All Rights Reserved.
  29163. *
  29164. * Use of this source code is governed by an MIT-style license that can be
  29165. * found in the LICENSE file at https://angular.io/license
  29166. */
  29167. /**
  29168. * Provides access to reflection data about symbols that the compiler needs.
  29169. */
  29170. class CompileReflector {
  29171. }
  29172. /**
  29173. * @license
  29174. * Copyright Google LLC All Rights Reserved.
  29175. *
  29176. * Use of this source code is governed by an MIT-style license that can be
  29177. * found in the LICENSE file at https://angular.io/license
  29178. */
  29179. /**
  29180. * Create a {@link UrlResolver} with no package prefix.
  29181. */
  29182. function createUrlResolverWithoutPackagePrefix() {
  29183. return new UrlResolver();
  29184. }
  29185. function createOfflineCompileUrlResolver() {
  29186. return new UrlResolver('.');
  29187. }
  29188. const UrlResolver = class UrlResolverImpl {
  29189. constructor(_packagePrefix = null) {
  29190. this._packagePrefix = _packagePrefix;
  29191. }
  29192. /**
  29193. * Resolves the `url` given the `baseUrl`:
  29194. * - when the `url` is null, the `baseUrl` is returned,
  29195. * - if `url` is relative ('path/to/here', './path/to/here'), the resolved url is a combination of
  29196. * `baseUrl` and `url`,
  29197. * - if `url` is absolute (it has a scheme: 'http://', 'https://' or start with '/'), the `url` is
  29198. * returned as is (ignoring the `baseUrl`)
  29199. */
  29200. resolve(baseUrl, url) {
  29201. let resolvedUrl = url;
  29202. if (baseUrl != null && baseUrl.length > 0) {
  29203. resolvedUrl = _resolveUrl(baseUrl, resolvedUrl);
  29204. }
  29205. const resolvedParts = _split(resolvedUrl);
  29206. let prefix = this._packagePrefix;
  29207. if (prefix != null && resolvedParts != null &&
  29208. resolvedParts[_ComponentIndex.Scheme] == 'package') {
  29209. let path = resolvedParts[_ComponentIndex.Path];
  29210. prefix = prefix.replace(/\/+$/, '');
  29211. path = path.replace(/^\/+/, '');
  29212. return `${prefix}/${path}`;
  29213. }
  29214. return resolvedUrl;
  29215. }
  29216. };
  29217. /**
  29218. * Extract the scheme of a URL.
  29219. */
  29220. function getUrlScheme(url) {
  29221. const match = _split(url);
  29222. return (match && match[_ComponentIndex.Scheme]) || '';
  29223. }
  29224. // The code below is adapted from Traceur:
  29225. // https://github.com/google/traceur-compiler/blob/9511c1dafa972bf0de1202a8a863bad02f0f95a8/src/runtime/url.js
  29226. /**
  29227. * Builds a URI string from already-encoded parts.
  29228. *
  29229. * No encoding is performed. Any component may be omitted as either null or
  29230. * undefined.
  29231. *
  29232. * @param opt_scheme The scheme such as 'http'.
  29233. * @param opt_userInfo The user name before the '@'.
  29234. * @param opt_domain The domain such as 'www.google.com', already
  29235. * URI-encoded.
  29236. * @param opt_port The port number.
  29237. * @param opt_path The path, already URI-encoded. If it is not
  29238. * empty, it must begin with a slash.
  29239. * @param opt_queryData The URI-encoded query data.
  29240. * @param opt_fragment The URI-encoded fragment identifier.
  29241. * @return The fully combined URI.
  29242. */
  29243. function _buildFromEncodedParts(opt_scheme, opt_userInfo, opt_domain, opt_port, opt_path, opt_queryData, opt_fragment) {
  29244. const out = [];
  29245. if (opt_scheme != null) {
  29246. out.push(opt_scheme + ':');
  29247. }
  29248. if (opt_domain != null) {
  29249. out.push('//');
  29250. if (opt_userInfo != null) {
  29251. out.push(opt_userInfo + '@');
  29252. }
  29253. out.push(opt_domain);
  29254. if (opt_port != null) {
  29255. out.push(':' + opt_port);
  29256. }
  29257. }
  29258. if (opt_path != null) {
  29259. out.push(opt_path);
  29260. }
  29261. if (opt_queryData != null) {
  29262. out.push('?' + opt_queryData);
  29263. }
  29264. if (opt_fragment != null) {
  29265. out.push('#' + opt_fragment);
  29266. }
  29267. return out.join('');
  29268. }
  29269. /**
  29270. * A regular expression for breaking a URI into its component parts.
  29271. *
  29272. * {@link https://tools.ietf.org/html/rfc3986#appendix-B} says
  29273. * As the "first-match-wins" algorithm is identical to the "greedy"
  29274. * disambiguation method used by POSIX regular expressions, it is natural and
  29275. * commonplace to use a regular expression for parsing the potential five
  29276. * components of a URI reference.
  29277. *
  29278. * The following line is the regular expression for breaking-down a
  29279. * well-formed URI reference into its components.
  29280. *
  29281. * <pre>
  29282. * ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
  29283. * 12 3 4 5 6 7 8 9
  29284. * </pre>
  29285. *
  29286. * The numbers in the second line above are only to assist readability; they
  29287. * indicate the reference points for each subexpression (i.e., each paired
  29288. * parenthesis). We refer to the value matched for subexpression <n> as $<n>.
  29289. * For example, matching the above expression to
  29290. * <pre>
  29291. * http://www.ics.uci.edu/pub/ietf/uri/#Related
  29292. * </pre>
  29293. * results in the following subexpression matches:
  29294. * <pre>
  29295. * $1 = http:
  29296. * $2 = http
  29297. * $3 = //www.ics.uci.edu
  29298. * $4 = www.ics.uci.edu
  29299. * $5 = /pub/ietf/uri/
  29300. * $6 = <undefined>
  29301. * $7 = <undefined>
  29302. * $8 = #Related
  29303. * $9 = Related
  29304. * </pre>
  29305. * where <undefined> indicates that the component is not present, as is the
  29306. * case for the query component in the above example. Therefore, we can
  29307. * determine the value of the five components as
  29308. * <pre>
  29309. * scheme = $2
  29310. * authority = $4
  29311. * path = $5
  29312. * query = $7
  29313. * fragment = $9
  29314. * </pre>
  29315. *
  29316. * The regular expression has been modified slightly to expose the
  29317. * userInfo, domain, and port separately from the authority.
  29318. * The modified version yields
  29319. * <pre>
  29320. * $1 = http scheme
  29321. * $2 = <undefined> userInfo -\
  29322. * $3 = www.ics.uci.edu domain | authority
  29323. * $4 = <undefined> port -/
  29324. * $5 = /pub/ietf/uri/ path
  29325. * $6 = <undefined> query without ?
  29326. * $7 = Related fragment without #
  29327. * </pre>
  29328. * @internal
  29329. */
  29330. const _splitRe = new RegExp('^' +
  29331. '(?:' +
  29332. '([^:/?#.]+)' + // scheme - ignore special characters
  29333. // used by other URL parts such as :,
  29334. // ?, /, #, and .
  29335. ':)?' +
  29336. '(?://' +
  29337. '(?:([^/?#]*)@)?' + // userInfo
  29338. '([\\w\\d\\-\\u0100-\\uffff.%]*)' + // domain - restrict to letters,
  29339. // digits, dashes, dots, percent
  29340. // escapes, and unicode characters.
  29341. '(?::([0-9]+))?' + // port
  29342. ')?' +
  29343. '([^?#]+)?' + // path
  29344. '(?:\\?([^#]*))?' + // query
  29345. '(?:#(.*))?' + // fragment
  29346. '$');
  29347. /**
  29348. * The index of each URI component in the return value of goog.uri.utils.split.
  29349. * @enum {number}
  29350. */
  29351. var _ComponentIndex;
  29352. (function (_ComponentIndex) {
  29353. _ComponentIndex[_ComponentIndex["Scheme"] = 1] = "Scheme";
  29354. _ComponentIndex[_ComponentIndex["UserInfo"] = 2] = "UserInfo";
  29355. _ComponentIndex[_ComponentIndex["Domain"] = 3] = "Domain";
  29356. _ComponentIndex[_ComponentIndex["Port"] = 4] = "Port";
  29357. _ComponentIndex[_ComponentIndex["Path"] = 5] = "Path";
  29358. _ComponentIndex[_ComponentIndex["QueryData"] = 6] = "QueryData";
  29359. _ComponentIndex[_ComponentIndex["Fragment"] = 7] = "Fragment";
  29360. })(_ComponentIndex || (_ComponentIndex = {}));
  29361. /**
  29362. * Splits a URI into its component parts.
  29363. *
  29364. * Each component can be accessed via the component indices; for example:
  29365. * <pre>
  29366. * goog.uri.utils.split(someStr)[goog.uri.utils.CompontentIndex.QUERY_DATA];
  29367. * </pre>
  29368. *
  29369. * @param uri The URI string to examine.
  29370. * @return Each component still URI-encoded.
  29371. * Each component that is present will contain the encoded value, whereas
  29372. * components that are not present will be undefined or empty, depending
  29373. * on the browser's regular expression implementation. Never null, since
  29374. * arbitrary strings may still look like path names.
  29375. */
  29376. function _split(uri) {
  29377. return uri.match(_splitRe);
  29378. }
  29379. /**
  29380. * Removes dot segments in given path component, as described in
  29381. * RFC 3986, section 5.2.4.
  29382. *
  29383. * @param path A non-empty path component.
  29384. * @return Path component with removed dot segments.
  29385. */
  29386. function _removeDotSegments(path) {
  29387. if (path == '/')
  29388. return '/';
  29389. const leadingSlash = path[0] == '/' ? '/' : '';
  29390. const trailingSlash = path[path.length - 1] === '/' ? '/' : '';
  29391. const segments = path.split('/');
  29392. const out = [];
  29393. let up = 0;
  29394. for (let pos = 0; pos < segments.length; pos++) {
  29395. const segment = segments[pos];
  29396. switch (segment) {
  29397. case '':
  29398. case '.':
  29399. break;
  29400. case '..':
  29401. if (out.length > 0) {
  29402. out.pop();
  29403. }
  29404. else {
  29405. up++;
  29406. }
  29407. break;
  29408. default:
  29409. out.push(segment);
  29410. }
  29411. }
  29412. if (leadingSlash == '') {
  29413. while (up-- > 0) {
  29414. out.unshift('..');
  29415. }
  29416. if (out.length === 0)
  29417. out.push('.');
  29418. }
  29419. return leadingSlash + out.join('/') + trailingSlash;
  29420. }
  29421. /**
  29422. * Takes an array of the parts from split and canonicalizes the path part
  29423. * and then joins all the parts.
  29424. */
  29425. function _joinAndCanonicalizePath(parts) {
  29426. let path = parts[_ComponentIndex.Path];
  29427. path = path == null ? '' : _removeDotSegments(path);
  29428. parts[_ComponentIndex.Path] = path;
  29429. return _buildFromEncodedParts(parts[_ComponentIndex.Scheme], parts[_ComponentIndex.UserInfo], parts[_ComponentIndex.Domain], parts[_ComponentIndex.Port], path, parts[_ComponentIndex.QueryData], parts[_ComponentIndex.Fragment]);
  29430. }
  29431. /**
  29432. * Resolves a URL.
  29433. * @param base The URL acting as the base URL.
  29434. * @param to The URL to resolve.
  29435. */
  29436. function _resolveUrl(base, url) {
  29437. const parts = _split(encodeURI(url));
  29438. const baseParts = _split(base);
  29439. if (parts[_ComponentIndex.Scheme] != null) {
  29440. return _joinAndCanonicalizePath(parts);
  29441. }
  29442. else {
  29443. parts[_ComponentIndex.Scheme] = baseParts[_ComponentIndex.Scheme];
  29444. }
  29445. for (let i = _ComponentIndex.Scheme; i <= _ComponentIndex.Port; i++) {
  29446. if (parts[i] == null) {
  29447. parts[i] = baseParts[i];
  29448. }
  29449. }
  29450. if (parts[_ComponentIndex.Path][0] == '/') {
  29451. return _joinAndCanonicalizePath(parts);
  29452. }
  29453. let path = baseParts[_ComponentIndex.Path];
  29454. if (path == null)
  29455. path = '/';
  29456. const index = path.lastIndexOf('/');
  29457. path = path.substring(0, index + 1) + parts[_ComponentIndex.Path];
  29458. parts[_ComponentIndex.Path] = path;
  29459. return _joinAndCanonicalizePath(parts);
  29460. }
  29461. /**
  29462. * @license
  29463. * Copyright Google LLC All Rights Reserved.
  29464. *
  29465. * Use of this source code is governed by an MIT-style license that can be
  29466. * found in the LICENSE file at https://angular.io/license
  29467. */
  29468. class Extractor {
  29469. constructor(host, staticSymbolResolver, messageBundle, metadataResolver) {
  29470. this.host = host;
  29471. this.staticSymbolResolver = staticSymbolResolver;
  29472. this.messageBundle = messageBundle;
  29473. this.metadataResolver = metadataResolver;
  29474. }
  29475. extract(rootFiles) {
  29476. const { files, ngModules } = analyzeAndValidateNgModules(rootFiles, this.host, this.staticSymbolResolver, this.metadataResolver);
  29477. return Promise
  29478. .all(ngModules.map(ngModule => this.metadataResolver.loadNgModuleDirectiveAndPipeMetadata(ngModule.type.reference, false)))
  29479. .then(() => {
  29480. const errors = [];
  29481. files.forEach(file => {
  29482. const compMetas = [];
  29483. file.directives.forEach(directiveType => {
  29484. const dirMeta = this.metadataResolver.getDirectiveMetadata(directiveType);
  29485. if (dirMeta && dirMeta.isComponent) {
  29486. compMetas.push(dirMeta);
  29487. }
  29488. });
  29489. compMetas.forEach(compMeta => {
  29490. const html = compMeta.template.template;
  29491. // Template URL points to either an HTML or TS file depending on
  29492. // whether the file is used with `templateUrl:` or `template:`,
  29493. // respectively.
  29494. const templateUrl = compMeta.template.templateUrl;
  29495. const interpolationConfig = InterpolationConfig.fromArray(compMeta.template.interpolation);
  29496. errors.push(...this.messageBundle.updateFromTemplate(html, templateUrl, interpolationConfig));
  29497. });
  29498. });
  29499. if (errors.length) {
  29500. throw new Error(errors.map(e => e.toString()).join('\n'));
  29501. }
  29502. return this.messageBundle;
  29503. });
  29504. }
  29505. static create(host, locale) {
  29506. const htmlParser = new HtmlParser();
  29507. const urlResolver = createAotUrlResolver(host);
  29508. const symbolCache = new StaticSymbolCache();
  29509. const summaryResolver = new AotSummaryResolver(host, symbolCache);
  29510. const staticSymbolResolver = new StaticSymbolResolver(host, symbolCache, summaryResolver);
  29511. const staticReflector = new StaticReflector(summaryResolver, staticSymbolResolver);
  29512. const config = new CompilerConfig({ defaultEncapsulation: ViewEncapsulation.Emulated, useJit: false });
  29513. const normalizer = new DirectiveNormalizer({ get: (url) => host.loadResource(url) }, urlResolver, htmlParser, config);
  29514. const elementSchemaRegistry = new DomElementSchemaRegistry();
  29515. const resolver = new CompileMetadataResolver(config, htmlParser, new NgModuleResolver(staticReflector), new DirectiveResolver(staticReflector), new PipeResolver(staticReflector), summaryResolver, elementSchemaRegistry, normalizer, console, symbolCache, staticReflector);
  29516. // TODO(vicb): implicit tags & attributes
  29517. const messageBundle = new MessageBundle(htmlParser, [], {}, locale);
  29518. const extractor = new Extractor(host, staticSymbolResolver, messageBundle, resolver);
  29519. return { extractor, staticReflector };
  29520. }
  29521. }
  29522. /**
  29523. * @license
  29524. * Copyright Google LLC All Rights Reserved.
  29525. *
  29526. * Use of this source code is governed by an MIT-style license that can be
  29527. * found in the LICENSE file at https://angular.io/license
  29528. */
  29529. var FactoryTarget$1;
  29530. (function (FactoryTarget) {
  29531. FactoryTarget[FactoryTarget["Directive"] = 0] = "Directive";
  29532. FactoryTarget[FactoryTarget["Component"] = 1] = "Component";
  29533. FactoryTarget[FactoryTarget["Injectable"] = 2] = "Injectable";
  29534. FactoryTarget[FactoryTarget["Pipe"] = 3] = "Pipe";
  29535. FactoryTarget[FactoryTarget["NgModule"] = 4] = "NgModule";
  29536. })(FactoryTarget$1 || (FactoryTarget$1 = {}));
  29537. /**
  29538. * @license
  29539. * Copyright Google LLC All Rights Reserved.
  29540. *
  29541. * Use of this source code is governed by an MIT-style license that can be
  29542. * found in the LICENSE file at https://angular.io/license
  29543. */
  29544. /**
  29545. * @license
  29546. * Copyright Google LLC All Rights Reserved.
  29547. *
  29548. * Use of this source code is governed by an MIT-style license that can be
  29549. * found in the LICENSE file at https://angular.io/license
  29550. */
  29551. /**
  29552. * @license
  29553. * Copyright Google LLC All Rights Reserved.
  29554. *
  29555. * Use of this source code is governed by an MIT-style license that can be
  29556. * found in the LICENSE file at https://angular.io/license
  29557. */
  29558. /**
  29559. * Processes `Target`s with a given set of directives and performs a binding operation, which
  29560. * returns an object similar to TypeScript's `ts.TypeChecker` that contains knowledge about the
  29561. * target.
  29562. */
  29563. class R3TargetBinder {
  29564. constructor(directiveMatcher) {
  29565. this.directiveMatcher = directiveMatcher;
  29566. }
  29567. /**
  29568. * Perform a binding operation on the given `Target` and return a `BoundTarget` which contains
  29569. * metadata about the types referenced in the template.
  29570. */
  29571. bind(target) {
  29572. if (!target.template) {
  29573. // TODO(alxhub): handle targets which contain things like HostBindings, etc.
  29574. throw new Error('Binding without a template not yet supported');
  29575. }
  29576. // First, parse the template into a `Scope` structure. This operation captures the syntactic
  29577. // scopes in the template and makes them available for later use.
  29578. const scope = Scope.apply(target.template);
  29579. // Use the `Scope` to extract the entities present at every level of the template.
  29580. const templateEntities = extractTemplateEntities(scope);
  29581. // Next, perform directive matching on the template using the `DirectiveBinder`. This returns:
  29582. // - directives: Map of nodes (elements & ng-templates) to the directives on them.
  29583. // - bindings: Map of inputs, outputs, and attributes to the directive/element that claims
  29584. // them. TODO(alxhub): handle multiple directives claiming an input/output/etc.
  29585. // - references: Map of #references to their targets.
  29586. const { directives, bindings, references } = DirectiveBinder.apply(target.template, this.directiveMatcher);
  29587. // Finally, run the TemplateBinder to bind references, variables, and other entities within the
  29588. // template. This extracts all the metadata that doesn't depend on directive matching.
  29589. const { expressions, symbols, nestingLevel, usedPipes } = TemplateBinder.apply(target.template, scope);
  29590. return new R3BoundTarget(target, directives, bindings, references, expressions, symbols, nestingLevel, templateEntities, usedPipes);
  29591. }
  29592. }
  29593. /**
  29594. * Represents a binding scope within a template.
  29595. *
  29596. * Any variables, references, or other named entities declared within the template will
  29597. * be captured and available by name in `namedEntities`. Additionally, child templates will
  29598. * be analyzed and have their child `Scope`s available in `childScopes`.
  29599. */
  29600. class Scope {
  29601. constructor(parentScope, template) {
  29602. this.parentScope = parentScope;
  29603. this.template = template;
  29604. /**
  29605. * Named members of the `Scope`, such as `Reference`s or `Variable`s.
  29606. */
  29607. this.namedEntities = new Map();
  29608. /**
  29609. * Child `Scope`s for immediately nested `Template`s.
  29610. */
  29611. this.childScopes = new Map();
  29612. }
  29613. static newRootScope() {
  29614. return new Scope(null, null);
  29615. }
  29616. /**
  29617. * Process a template (either as a `Template` sub-template with variables, or a plain array of
  29618. * template `Node`s) and construct its `Scope`.
  29619. */
  29620. static apply(template) {
  29621. const scope = Scope.newRootScope();
  29622. scope.ingest(template);
  29623. return scope;
  29624. }
  29625. /**
  29626. * Internal method to process the template and populate the `Scope`.
  29627. */
  29628. ingest(template) {
  29629. if (template instanceof Template) {
  29630. // Variables on an <ng-template> are defined in the inner scope.
  29631. template.variables.forEach(node => this.visitVariable(node));
  29632. // Process the nodes of the template.
  29633. template.children.forEach(node => node.visit(this));
  29634. }
  29635. else {
  29636. // No overarching `Template` instance, so process the nodes directly.
  29637. template.forEach(node => node.visit(this));
  29638. }
  29639. }
  29640. visitElement(element) {
  29641. // `Element`s in the template may have `Reference`s which are captured in the scope.
  29642. element.references.forEach(node => this.visitReference(node));
  29643. // Recurse into the `Element`'s children.
  29644. element.children.forEach(node => node.visit(this));
  29645. }
  29646. visitTemplate(template) {
  29647. // References on a <ng-template> are defined in the outer scope, so capture them before
  29648. // processing the template's child scope.
  29649. template.references.forEach(node => this.visitReference(node));
  29650. // Next, create an inner scope and process the template within it.
  29651. const scope = new Scope(this, template);
  29652. scope.ingest(template);
  29653. this.childScopes.set(template, scope);
  29654. }
  29655. visitVariable(variable) {
  29656. // Declare the variable if it's not already.
  29657. this.maybeDeclare(variable);
  29658. }
  29659. visitReference(reference) {
  29660. // Declare the variable if it's not already.
  29661. this.maybeDeclare(reference);
  29662. }
  29663. // Unused visitors.
  29664. visitContent(content) { }
  29665. visitBoundAttribute(attr) { }
  29666. visitBoundEvent(event) { }
  29667. visitBoundText(text) { }
  29668. visitText(text) { }
  29669. visitTextAttribute(attr) { }
  29670. visitIcu(icu) { }
  29671. maybeDeclare(thing) {
  29672. // Declare something with a name, as long as that name isn't taken.
  29673. if (!this.namedEntities.has(thing.name)) {
  29674. this.namedEntities.set(thing.name, thing);
  29675. }
  29676. }
  29677. /**
  29678. * Look up a variable within this `Scope`.
  29679. *
  29680. * This can recurse into a parent `Scope` if it's available.
  29681. */
  29682. lookup(name) {
  29683. if (this.namedEntities.has(name)) {
  29684. // Found in the local scope.
  29685. return this.namedEntities.get(name);
  29686. }
  29687. else if (this.parentScope !== null) {
  29688. // Not in the local scope, but there's a parent scope so check there.
  29689. return this.parentScope.lookup(name);
  29690. }
  29691. else {
  29692. // At the top level and it wasn't found.
  29693. return null;
  29694. }
  29695. }
  29696. /**
  29697. * Get the child scope for a `Template`.
  29698. *
  29699. * This should always be defined.
  29700. */
  29701. getChildScope(template) {
  29702. const res = this.childScopes.get(template);
  29703. if (res === undefined) {
  29704. throw new Error(`Assertion error: child scope for ${template} not found`);
  29705. }
  29706. return res;
  29707. }
  29708. }
  29709. /**
  29710. * Processes a template and matches directives on nodes (elements and templates).
  29711. *
  29712. * Usually used via the static `apply()` method.
  29713. */
  29714. class DirectiveBinder {
  29715. constructor(matcher, directives, bindings, references) {
  29716. this.matcher = matcher;
  29717. this.directives = directives;
  29718. this.bindings = bindings;
  29719. this.references = references;
  29720. }
  29721. /**
  29722. * Process a template (list of `Node`s) and perform directive matching against each node.
  29723. *
  29724. * @param template the list of template `Node`s to match (recursively).
  29725. * @param selectorMatcher a `SelectorMatcher` containing the directives that are in scope for
  29726. * this template.
  29727. * @returns three maps which contain information about directives in the template: the
  29728. * `directives` map which lists directives matched on each node, the `bindings` map which
  29729. * indicates which directives claimed which bindings (inputs, outputs, etc), and the `references`
  29730. * map which resolves #references (`Reference`s) within the template to the named directive or
  29731. * template node.
  29732. */
  29733. static apply(template, selectorMatcher) {
  29734. const directives = new Map();
  29735. const bindings = new Map();
  29736. const references = new Map();
  29737. const matcher = new DirectiveBinder(selectorMatcher, directives, bindings, references);
  29738. matcher.ingest(template);
  29739. return { directives, bindings, references };
  29740. }
  29741. ingest(template) {
  29742. template.forEach(node => node.visit(this));
  29743. }
  29744. visitElement(element) {
  29745. this.visitElementOrTemplate(element.name, element);
  29746. }
  29747. visitTemplate(template) {
  29748. this.visitElementOrTemplate('ng-template', template);
  29749. }
  29750. visitElementOrTemplate(elementName, node) {
  29751. // First, determine the HTML shape of the node for the purpose of directive matching.
  29752. // Do this by building up a `CssSelector` for the node.
  29753. const cssSelector = createCssSelector(elementName, getAttrsForDirectiveMatching(node));
  29754. // Next, use the `SelectorMatcher` to get the list of directives on the node.
  29755. const directives = [];
  29756. this.matcher.match(cssSelector, (_, directive) => directives.push(directive));
  29757. if (directives.length > 0) {
  29758. this.directives.set(node, directives);
  29759. }
  29760. // Resolve any references that are created on this node.
  29761. node.references.forEach(ref => {
  29762. let dirTarget = null;
  29763. // If the reference expression is empty, then it matches the "primary" directive on the node
  29764. // (if there is one). Otherwise it matches the host node itself (either an element or
  29765. // <ng-template> node).
  29766. if (ref.value.trim() === '') {
  29767. // This could be a reference to a component if there is one.
  29768. dirTarget = directives.find(dir => dir.isComponent) || null;
  29769. }
  29770. else {
  29771. // This should be a reference to a directive exported via exportAs.
  29772. dirTarget =
  29773. directives.find(dir => dir.exportAs !== null && dir.exportAs.some(value => value === ref.value)) ||
  29774. null;
  29775. // Check if a matching directive was found.
  29776. if (dirTarget === null) {
  29777. // No matching directive was found - this reference points to an unknown target. Leave it
  29778. // unmapped.
  29779. return;
  29780. }
  29781. }
  29782. if (dirTarget !== null) {
  29783. // This reference points to a directive.
  29784. this.references.set(ref, { directive: dirTarget, node });
  29785. }
  29786. else {
  29787. // This reference points to the node itself.
  29788. this.references.set(ref, node);
  29789. }
  29790. });
  29791. const setAttributeBinding = (attribute, ioType) => {
  29792. const dir = directives.find(dir => dir[ioType].hasBindingPropertyName(attribute.name));
  29793. const binding = dir !== undefined ? dir : node;
  29794. this.bindings.set(attribute, binding);
  29795. };
  29796. // Node inputs (bound attributes) and text attributes can be bound to an
  29797. // input on a directive.
  29798. node.inputs.forEach(input => setAttributeBinding(input, 'inputs'));
  29799. node.attributes.forEach(attr => setAttributeBinding(attr, 'inputs'));
  29800. if (node instanceof Template) {
  29801. node.templateAttrs.forEach(attr => setAttributeBinding(attr, 'inputs'));
  29802. }
  29803. // Node outputs (bound events) can be bound to an output on a directive.
  29804. node.outputs.forEach(output => setAttributeBinding(output, 'outputs'));
  29805. // Recurse into the node's children.
  29806. node.children.forEach(child => child.visit(this));
  29807. }
  29808. // Unused visitors.
  29809. visitContent(content) { }
  29810. visitVariable(variable) { }
  29811. visitReference(reference) { }
  29812. visitTextAttribute(attribute) { }
  29813. visitBoundAttribute(attribute) { }
  29814. visitBoundEvent(attribute) { }
  29815. visitBoundAttributeOrEvent(node) { }
  29816. visitText(text) { }
  29817. visitBoundText(text) { }
  29818. visitIcu(icu) { }
  29819. }
  29820. /**
  29821. * Processes a template and extract metadata about expressions and symbols within.
  29822. *
  29823. * This is a companion to the `DirectiveBinder` that doesn't require knowledge of directives matched
  29824. * within the template in order to operate.
  29825. *
  29826. * Expressions are visited by the superclass `RecursiveAstVisitor`, with custom logic provided
  29827. * by overridden methods from that visitor.
  29828. */
  29829. class TemplateBinder extends RecursiveAstVisitor$1 {
  29830. constructor(bindings, symbols, usedPipes, nestingLevel, scope, template, level) {
  29831. super();
  29832. this.bindings = bindings;
  29833. this.symbols = symbols;
  29834. this.usedPipes = usedPipes;
  29835. this.nestingLevel = nestingLevel;
  29836. this.scope = scope;
  29837. this.template = template;
  29838. this.level = level;
  29839. this.pipesUsed = [];
  29840. // Save a bit of processing time by constructing this closure in advance.
  29841. this.visitNode = (node) => node.visit(this);
  29842. }
  29843. // This method is defined to reconcile the type of TemplateBinder since both
  29844. // RecursiveAstVisitor and Visitor define the visit() method in their
  29845. // interfaces.
  29846. visit(node, context) {
  29847. if (node instanceof AST) {
  29848. node.visit(this, context);
  29849. }
  29850. else {
  29851. node.visit(this);
  29852. }
  29853. }
  29854. /**
  29855. * Process a template and extract metadata about expressions and symbols within.
  29856. *
  29857. * @param template the nodes of the template to process
  29858. * @param scope the `Scope` of the template being processed.
  29859. * @returns three maps which contain metadata about the template: `expressions` which interprets
  29860. * special `AST` nodes in expressions as pointing to references or variables declared within the
  29861. * template, `symbols` which maps those variables and references to the nested `Template` which
  29862. * declares them, if any, and `nestingLevel` which associates each `Template` with a integer
  29863. * nesting level (how many levels deep within the template structure the `Template` is), starting
  29864. * at 1.
  29865. */
  29866. static apply(template, scope) {
  29867. const expressions = new Map();
  29868. const symbols = new Map();
  29869. const nestingLevel = new Map();
  29870. const usedPipes = new Set();
  29871. // The top-level template has nesting level 0.
  29872. const binder = new TemplateBinder(expressions, symbols, usedPipes, nestingLevel, scope, template instanceof Template ? template : null, 0);
  29873. binder.ingest(template);
  29874. return { expressions, symbols, nestingLevel, usedPipes };
  29875. }
  29876. ingest(template) {
  29877. if (template instanceof Template) {
  29878. // For <ng-template>s, process only variables and child nodes. Inputs, outputs, templateAttrs,
  29879. // and references were all processed in the scope of the containing template.
  29880. template.variables.forEach(this.visitNode);
  29881. template.children.forEach(this.visitNode);
  29882. // Set the nesting level.
  29883. this.nestingLevel.set(template, this.level);
  29884. }
  29885. else {
  29886. // Visit each node from the top-level template.
  29887. template.forEach(this.visitNode);
  29888. }
  29889. }
  29890. visitElement(element) {
  29891. // Visit the inputs, outputs, and children of the element.
  29892. element.inputs.forEach(this.visitNode);
  29893. element.outputs.forEach(this.visitNode);
  29894. element.children.forEach(this.visitNode);
  29895. }
  29896. visitTemplate(template) {
  29897. // First, visit inputs, outputs and template attributes of the template node.
  29898. template.inputs.forEach(this.visitNode);
  29899. template.outputs.forEach(this.visitNode);
  29900. template.templateAttrs.forEach(this.visitNode);
  29901. // References are also evaluated in the outer context.
  29902. template.references.forEach(this.visitNode);
  29903. // Next, recurse into the template using its scope, and bumping the nesting level up by one.
  29904. const childScope = this.scope.getChildScope(template);
  29905. const binder = new TemplateBinder(this.bindings, this.symbols, this.usedPipes, this.nestingLevel, childScope, template, this.level + 1);
  29906. binder.ingest(template);
  29907. }
  29908. visitVariable(variable) {
  29909. // Register the `Variable` as a symbol in the current `Template`.
  29910. if (this.template !== null) {
  29911. this.symbols.set(variable, this.template);
  29912. }
  29913. }
  29914. visitReference(reference) {
  29915. // Register the `Reference` as a symbol in the current `Template`.
  29916. if (this.template !== null) {
  29917. this.symbols.set(reference, this.template);
  29918. }
  29919. }
  29920. // Unused template visitors
  29921. visitText(text) { }
  29922. visitContent(content) { }
  29923. visitTextAttribute(attribute) { }
  29924. visitIcu(icu) {
  29925. Object.keys(icu.vars).forEach(key => icu.vars[key].visit(this));
  29926. Object.keys(icu.placeholders).forEach(key => icu.placeholders[key].visit(this));
  29927. }
  29928. // The remaining visitors are concerned with processing AST expressions within template bindings
  29929. visitBoundAttribute(attribute) {
  29930. attribute.value.visit(this);
  29931. }
  29932. visitBoundEvent(event) {
  29933. event.handler.visit(this);
  29934. }
  29935. visitBoundText(text) {
  29936. text.value.visit(this);
  29937. }
  29938. visitPipe(ast, context) {
  29939. this.usedPipes.add(ast.name);
  29940. return super.visitPipe(ast, context);
  29941. }
  29942. // These five types of AST expressions can refer to expression roots, which could be variables
  29943. // or references in the current scope.
  29944. visitPropertyRead(ast, context) {
  29945. this.maybeMap(context, ast, ast.name);
  29946. return super.visitPropertyRead(ast, context);
  29947. }
  29948. visitSafePropertyRead(ast, context) {
  29949. this.maybeMap(context, ast, ast.name);
  29950. return super.visitSafePropertyRead(ast, context);
  29951. }
  29952. visitPropertyWrite(ast, context) {
  29953. this.maybeMap(context, ast, ast.name);
  29954. return super.visitPropertyWrite(ast, context);
  29955. }
  29956. visitMethodCall(ast, context) {
  29957. this.maybeMap(context, ast, ast.name);
  29958. return super.visitMethodCall(ast, context);
  29959. }
  29960. visitSafeMethodCall(ast, context) {
  29961. this.maybeMap(context, ast, ast.name);
  29962. return super.visitSafeMethodCall(ast, context);
  29963. }
  29964. maybeMap(scope, ast, name) {
  29965. // If the receiver of the expression isn't the `ImplicitReceiver`, this isn't the root of an
  29966. // `AST` expression that maps to a `Variable` or `Reference`.
  29967. if (!(ast.receiver instanceof ImplicitReceiver)) {
  29968. return;
  29969. }
  29970. // Check whether the name exists in the current scope. If so, map it. Otherwise, the name is
  29971. // probably a property on the top-level component context.
  29972. let target = this.scope.lookup(name);
  29973. if (target !== null) {
  29974. this.bindings.set(ast, target);
  29975. }
  29976. }
  29977. }
  29978. /**
  29979. * Metadata container for a `Target` that allows queries for specific bits of metadata.
  29980. *
  29981. * See `BoundTarget` for documentation on the individual methods.
  29982. */
  29983. class R3BoundTarget {
  29984. constructor(target, directives, bindings, references, exprTargets, symbols, nestingLevel, templateEntities, usedPipes) {
  29985. this.target = target;
  29986. this.directives = directives;
  29987. this.bindings = bindings;
  29988. this.references = references;
  29989. this.exprTargets = exprTargets;
  29990. this.symbols = symbols;
  29991. this.nestingLevel = nestingLevel;
  29992. this.templateEntities = templateEntities;
  29993. this.usedPipes = usedPipes;
  29994. }
  29995. getEntitiesInTemplateScope(template) {
  29996. var _a;
  29997. return (_a = this.templateEntities.get(template)) !== null && _a !== void 0 ? _a : new Set();
  29998. }
  29999. getDirectivesOfNode(node) {
  30000. return this.directives.get(node) || null;
  30001. }
  30002. getReferenceTarget(ref) {
  30003. return this.references.get(ref) || null;
  30004. }
  30005. getConsumerOfBinding(binding) {
  30006. return this.bindings.get(binding) || null;
  30007. }
  30008. getExpressionTarget(expr) {
  30009. return this.exprTargets.get(expr) || null;
  30010. }
  30011. getTemplateOfSymbol(symbol) {
  30012. return this.symbols.get(symbol) || null;
  30013. }
  30014. getNestingLevel(template) {
  30015. return this.nestingLevel.get(template) || 0;
  30016. }
  30017. getUsedDirectives() {
  30018. const set = new Set();
  30019. this.directives.forEach(dirs => dirs.forEach(dir => set.add(dir)));
  30020. return Array.from(set.values());
  30021. }
  30022. getUsedPipes() {
  30023. return Array.from(this.usedPipes);
  30024. }
  30025. }
  30026. function extractTemplateEntities(rootScope) {
  30027. const entityMap = new Map();
  30028. function extractScopeEntities(scope) {
  30029. if (entityMap.has(scope.template)) {
  30030. return entityMap.get(scope.template);
  30031. }
  30032. const currentEntities = scope.namedEntities;
  30033. let templateEntities;
  30034. if (scope.parentScope !== null) {
  30035. templateEntities = new Map([...extractScopeEntities(scope.parentScope), ...currentEntities]);
  30036. }
  30037. else {
  30038. templateEntities = new Map(currentEntities);
  30039. }
  30040. entityMap.set(scope.template, templateEntities);
  30041. return templateEntities;
  30042. }
  30043. const scopesToProcess = [rootScope];
  30044. while (scopesToProcess.length > 0) {
  30045. const scope = scopesToProcess.pop();
  30046. for (const childScope of scope.childScopes.values()) {
  30047. scopesToProcess.push(childScope);
  30048. }
  30049. extractScopeEntities(scope);
  30050. }
  30051. const templateEntities = new Map();
  30052. for (const [template, entities] of entityMap) {
  30053. templateEntities.set(template, new Set(entities.values()));
  30054. }
  30055. return templateEntities;
  30056. }
  30057. /**
  30058. * @license
  30059. * Copyright Google LLC All Rights Reserved.
  30060. *
  30061. * Use of this source code is governed by an MIT-style license that can be
  30062. * found in the LICENSE file at https://angular.io/license
  30063. */
  30064. function compileClassMetadata(metadata) {
  30065. var _a, _b;
  30066. // Generate an ngDevMode guarded call to setClassMetadata with the class identifier and its
  30067. // metadata.
  30068. const fnCall = importExpr(Identifiers.setClassMetadata).callFn([
  30069. metadata.type,
  30070. metadata.decorators,
  30071. (_a = metadata.ctorParameters) !== null && _a !== void 0 ? _a : literal(null),
  30072. (_b = metadata.propDecorators) !== null && _b !== void 0 ? _b : literal(null),
  30073. ]);
  30074. const iife = fn([], [devOnlyGuardedExpression(fnCall).toStmt()]);
  30075. return iife.callFn([]);
  30076. }
  30077. /**
  30078. * @license
  30079. * Copyright Google LLC All Rights Reserved.
  30080. *
  30081. * Use of this source code is governed by an MIT-style license that can be
  30082. * found in the LICENSE file at https://angular.io/license
  30083. */
  30084. /**
  30085. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  30086. * must update this constant to prevent old partial-linkers from incorrectly processing the
  30087. * declaration.
  30088. *
  30089. * Do not include any prerelease in these versions as they are ignored.
  30090. */
  30091. const MINIMUM_PARTIAL_LINKER_VERSION = '12.0.0';
  30092. function compileDeclareClassMetadata(metadata) {
  30093. const definitionMap = new DefinitionMap();
  30094. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION));
  30095. definitionMap.set('version', literal('12.0.5'));
  30096. definitionMap.set('ngImport', importExpr(Identifiers.core));
  30097. definitionMap.set('type', metadata.type);
  30098. definitionMap.set('decorators', metadata.decorators);
  30099. definitionMap.set('ctorParameters', metadata.ctorParameters);
  30100. definitionMap.set('propDecorators', metadata.propDecorators);
  30101. return importExpr(Identifiers.declareClassMetadata).callFn([definitionMap.toLiteralMap()]);
  30102. }
  30103. /**
  30104. * @license
  30105. * Copyright Google LLC All Rights Reserved.
  30106. *
  30107. * Use of this source code is governed by an MIT-style license that can be
  30108. * found in the LICENSE file at https://angular.io/license
  30109. */
  30110. /**
  30111. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  30112. * must update this constant to prevent old partial-linkers from incorrectly processing the
  30113. * declaration.
  30114. *
  30115. * Do not include any prerelease in these versions as they are ignored.
  30116. */
  30117. const MINIMUM_PARTIAL_LINKER_VERSION$1 = '12.0.0';
  30118. /**
  30119. * Compile a directive declaration defined by the `R3DirectiveMetadata`.
  30120. */
  30121. function compileDeclareDirectiveFromMetadata(meta) {
  30122. const definitionMap = createDirectiveDefinitionMap(meta);
  30123. const expression = importExpr(Identifiers.declareDirective).callFn([definitionMap.toLiteralMap()]);
  30124. const type = createDirectiveType(meta);
  30125. return { expression, type, statements: [] };
  30126. }
  30127. /**
  30128. * Gathers the declaration fields for a directive into a `DefinitionMap`. This allows for reusing
  30129. * this logic for components, as they extend the directive metadata.
  30130. */
  30131. function createDirectiveDefinitionMap(meta) {
  30132. const definitionMap = new DefinitionMap();
  30133. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$1));
  30134. definitionMap.set('version', literal('12.0.5'));
  30135. // e.g. `type: MyDirective`
  30136. definitionMap.set('type', meta.internalType);
  30137. // e.g. `selector: 'some-dir'`
  30138. if (meta.selector !== null) {
  30139. definitionMap.set('selector', literal(meta.selector));
  30140. }
  30141. definitionMap.set('inputs', conditionallyCreateMapObjectLiteral(meta.inputs, true));
  30142. definitionMap.set('outputs', conditionallyCreateMapObjectLiteral(meta.outputs));
  30143. definitionMap.set('host', compileHostMetadata(meta.host));
  30144. definitionMap.set('providers', meta.providers);
  30145. if (meta.queries.length > 0) {
  30146. definitionMap.set('queries', literalArr(meta.queries.map(compileQuery)));
  30147. }
  30148. if (meta.viewQueries.length > 0) {
  30149. definitionMap.set('viewQueries', literalArr(meta.viewQueries.map(compileQuery)));
  30150. }
  30151. if (meta.exportAs !== null) {
  30152. definitionMap.set('exportAs', asLiteral(meta.exportAs));
  30153. }
  30154. if (meta.usesInheritance) {
  30155. definitionMap.set('usesInheritance', literal(true));
  30156. }
  30157. if (meta.lifecycle.usesOnChanges) {
  30158. definitionMap.set('usesOnChanges', literal(true));
  30159. }
  30160. definitionMap.set('ngImport', importExpr(Identifiers.core));
  30161. return definitionMap;
  30162. }
  30163. /**
  30164. * Compiles the metadata of a single query into its partial declaration form as declared
  30165. * by `R3DeclareQueryMetadata`.
  30166. */
  30167. function compileQuery(query) {
  30168. const meta = new DefinitionMap();
  30169. meta.set('propertyName', literal(query.propertyName));
  30170. if (query.first) {
  30171. meta.set('first', literal(true));
  30172. }
  30173. meta.set('predicate', Array.isArray(query.predicate) ? asLiteral(query.predicate) : query.predicate);
  30174. if (!query.emitDistinctChangesOnly) {
  30175. // `emitDistinctChangesOnly` is special because we expect it to be `true`.
  30176. // Therefore we explicitly emit the field, and explicitly place it only when it's `false`.
  30177. meta.set('emitDistinctChangesOnly', literal(false));
  30178. }
  30179. else {
  30180. // The linker will assume that an absent `emitDistinctChangesOnly` flag is by default `true`.
  30181. }
  30182. if (query.descendants) {
  30183. meta.set('descendants', literal(true));
  30184. }
  30185. meta.set('read', query.read);
  30186. if (query.static) {
  30187. meta.set('static', literal(true));
  30188. }
  30189. return meta.toLiteralMap();
  30190. }
  30191. /**
  30192. * Compiles the host metadata into its partial declaration form as declared
  30193. * in `R3DeclareDirectiveMetadata['host']`
  30194. */
  30195. function compileHostMetadata(meta) {
  30196. const hostMetadata = new DefinitionMap();
  30197. hostMetadata.set('attributes', toOptionalLiteralMap(meta.attributes, expression => expression));
  30198. hostMetadata.set('listeners', toOptionalLiteralMap(meta.listeners, literal));
  30199. hostMetadata.set('properties', toOptionalLiteralMap(meta.properties, literal));
  30200. if (meta.specialAttributes.styleAttr) {
  30201. hostMetadata.set('styleAttribute', literal(meta.specialAttributes.styleAttr));
  30202. }
  30203. if (meta.specialAttributes.classAttr) {
  30204. hostMetadata.set('classAttribute', literal(meta.specialAttributes.classAttr));
  30205. }
  30206. if (hostMetadata.values.length > 0) {
  30207. return hostMetadata.toLiteralMap();
  30208. }
  30209. else {
  30210. return null;
  30211. }
  30212. }
  30213. /**
  30214. * @license
  30215. * Copyright Google LLC All Rights Reserved.
  30216. *
  30217. * Use of this source code is governed by an MIT-style license that can be
  30218. * found in the LICENSE file at https://angular.io/license
  30219. */
  30220. /**
  30221. * Compile a component declaration defined by the `R3ComponentMetadata`.
  30222. */
  30223. function compileDeclareComponentFromMetadata(meta, template, additionalTemplateInfo) {
  30224. const definitionMap = createComponentDefinitionMap(meta, template, additionalTemplateInfo);
  30225. const expression = importExpr(Identifiers.declareComponent).callFn([definitionMap.toLiteralMap()]);
  30226. const type = createComponentType(meta);
  30227. return { expression, type, statements: [] };
  30228. }
  30229. /**
  30230. * Gathers the declaration fields for a component into a `DefinitionMap`.
  30231. */
  30232. function createComponentDefinitionMap(meta, template, templateInfo) {
  30233. const definitionMap = createDirectiveDefinitionMap(meta);
  30234. definitionMap.set('template', getTemplateExpression(template, templateInfo));
  30235. if (templateInfo.isInline) {
  30236. definitionMap.set('isInline', literal(true));
  30237. }
  30238. definitionMap.set('styles', toOptionalLiteralArray(meta.styles, literal));
  30239. definitionMap.set('components', compileUsedDirectiveMetadata(meta, directive => directive.isComponent === true));
  30240. definitionMap.set('directives', compileUsedDirectiveMetadata(meta, directive => directive.isComponent !== true));
  30241. definitionMap.set('pipes', compileUsedPipeMetadata(meta));
  30242. definitionMap.set('viewProviders', meta.viewProviders);
  30243. definitionMap.set('animations', meta.animations);
  30244. if (meta.changeDetection !== undefined) {
  30245. definitionMap.set('changeDetection', importExpr(Identifiers.ChangeDetectionStrategy)
  30246. .prop(ChangeDetectionStrategy[meta.changeDetection]));
  30247. }
  30248. if (meta.encapsulation !== ViewEncapsulation.Emulated) {
  30249. definitionMap.set('encapsulation', importExpr(Identifiers.ViewEncapsulation).prop(ViewEncapsulation[meta.encapsulation]));
  30250. }
  30251. if (meta.interpolation !== DEFAULT_INTERPOLATION_CONFIG) {
  30252. definitionMap.set('interpolation', literalArr([literal(meta.interpolation.start), literal(meta.interpolation.end)]));
  30253. }
  30254. if (template.preserveWhitespaces === true) {
  30255. definitionMap.set('preserveWhitespaces', literal(true));
  30256. }
  30257. return definitionMap;
  30258. }
  30259. function getTemplateExpression(template, templateInfo) {
  30260. // If the template has been defined using a direct literal, we use that expression directly
  30261. // without any modifications. This is ensures proper source mapping from the partially
  30262. // compiled code to the source file declaring the template. Note that this does not capture
  30263. // template literals referenced indirectly through an identifier.
  30264. if (templateInfo.inlineTemplateLiteralExpression !== null) {
  30265. return templateInfo.inlineTemplateLiteralExpression;
  30266. }
  30267. // If the template is defined inline but not through a literal, the template has been resolved
  30268. // through static interpretation. We create a literal but cannot provide any source span. Note
  30269. // that we cannot use the expression defining the template because the linker expects the template
  30270. // to be defined as a literal in the declaration.
  30271. if (templateInfo.isInline) {
  30272. return literal(templateInfo.content, null, null);
  30273. }
  30274. // The template is external so we must synthesize an expression node with
  30275. // the appropriate source-span.
  30276. const contents = templateInfo.content;
  30277. const file = new ParseSourceFile(contents, templateInfo.sourceUrl);
  30278. const start = new ParseLocation(file, 0, 0, 0);
  30279. const end = computeEndLocation(file, contents);
  30280. const span = new ParseSourceSpan(start, end);
  30281. return literal(contents, null, span);
  30282. }
  30283. function computeEndLocation(file, contents) {
  30284. const length = contents.length;
  30285. let lineStart = 0;
  30286. let lastLineStart = 0;
  30287. let line = 0;
  30288. do {
  30289. lineStart = contents.indexOf('\n', lastLineStart);
  30290. if (lineStart !== -1) {
  30291. lastLineStart = lineStart + 1;
  30292. line++;
  30293. }
  30294. } while (lineStart !== -1);
  30295. return new ParseLocation(file, length, line, length - lastLineStart);
  30296. }
  30297. /**
  30298. * Compiles the directives as registered in the component metadata into an array literal of the
  30299. * individual directives. If the component does not use any directives, then null is returned.
  30300. */
  30301. function compileUsedDirectiveMetadata(meta, predicate) {
  30302. const wrapType = meta.declarationListEmitMode !== 0 /* Direct */ ?
  30303. generateForwardRef :
  30304. (expr) => expr;
  30305. const directives = meta.directives.filter(predicate);
  30306. return toOptionalLiteralArray(directives, directive => {
  30307. const dirMeta = new DefinitionMap();
  30308. dirMeta.set('type', wrapType(directive.type));
  30309. dirMeta.set('selector', literal(directive.selector));
  30310. dirMeta.set('inputs', toOptionalLiteralArray(directive.inputs, literal));
  30311. dirMeta.set('outputs', toOptionalLiteralArray(directive.outputs, literal));
  30312. dirMeta.set('exportAs', toOptionalLiteralArray(directive.exportAs, literal));
  30313. return dirMeta.toLiteralMap();
  30314. });
  30315. }
  30316. /**
  30317. * Compiles the pipes as registered in the component metadata into an object literal, where the
  30318. * pipe's name is used as key and a reference to its type as value. If the component does not use
  30319. * any pipes, then null is returned.
  30320. */
  30321. function compileUsedPipeMetadata(meta) {
  30322. if (meta.pipes.size === 0) {
  30323. return null;
  30324. }
  30325. const wrapType = meta.declarationListEmitMode !== 0 /* Direct */ ?
  30326. generateForwardRef :
  30327. (expr) => expr;
  30328. const entries = [];
  30329. for (const [name, pipe] of meta.pipes) {
  30330. entries.push({ key: name, value: wrapType(pipe), quoted: true });
  30331. }
  30332. return literalMap(entries);
  30333. }
  30334. /**
  30335. * @license
  30336. * Copyright Google LLC All Rights Reserved.
  30337. *
  30338. * Use of this source code is governed by an MIT-style license that can be
  30339. * found in the LICENSE file at https://angular.io/license
  30340. */
  30341. /**
  30342. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  30343. * must update this constant to prevent old partial-linkers from incorrectly processing the
  30344. * declaration.
  30345. *
  30346. * Do not include any prerelease in these versions as they are ignored.
  30347. */
  30348. const MINIMUM_PARTIAL_LINKER_VERSION$2 = '12.0.0';
  30349. function compileDeclareFactoryFunction(meta) {
  30350. const definitionMap = new DefinitionMap();
  30351. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$2));
  30352. definitionMap.set('version', literal('12.0.5'));
  30353. definitionMap.set('ngImport', importExpr(Identifiers.core));
  30354. definitionMap.set('type', meta.internalType);
  30355. definitionMap.set('deps', compileDependencies(meta.deps));
  30356. definitionMap.set('target', importExpr(Identifiers.FactoryTarget).prop(FactoryTarget[meta.target]));
  30357. return {
  30358. expression: importExpr(Identifiers.declareFactory).callFn([definitionMap.toLiteralMap()]),
  30359. statements: [],
  30360. type: createFactoryType(meta),
  30361. };
  30362. }
  30363. /**
  30364. * @license
  30365. * Copyright Google LLC All Rights Reserved.
  30366. *
  30367. * Use of this source code is governed by an MIT-style license that can be
  30368. * found in the LICENSE file at https://angular.io/license
  30369. */
  30370. /**
  30371. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  30372. * must update this constant to prevent old partial-linkers from incorrectly processing the
  30373. * declaration.
  30374. *
  30375. * Do not include any prerelease in these versions as they are ignored.
  30376. */
  30377. const MINIMUM_PARTIAL_LINKER_VERSION$3 = '12.0.0';
  30378. /**
  30379. * Compile a Injectable declaration defined by the `R3InjectableMetadata`.
  30380. */
  30381. function compileDeclareInjectableFromMetadata(meta) {
  30382. const definitionMap = createInjectableDefinitionMap(meta);
  30383. const expression = importExpr(Identifiers.declareInjectable).callFn([definitionMap.toLiteralMap()]);
  30384. const type = createInjectableType(meta);
  30385. return { expression, type, statements: [] };
  30386. }
  30387. /**
  30388. * Gathers the declaration fields for a Injectable into a `DefinitionMap`.
  30389. */
  30390. function createInjectableDefinitionMap(meta) {
  30391. const definitionMap = new DefinitionMap();
  30392. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$3));
  30393. definitionMap.set('version', literal('12.0.5'));
  30394. definitionMap.set('ngImport', importExpr(Identifiers.core));
  30395. definitionMap.set('type', meta.internalType);
  30396. // Only generate providedIn property if it has a non-null value
  30397. if (meta.providedIn !== undefined) {
  30398. const providedIn = convertFromProviderExpression(meta.providedIn);
  30399. if (providedIn.value !== null) {
  30400. definitionMap.set('providedIn', providedIn);
  30401. }
  30402. }
  30403. if (meta.useClass !== undefined) {
  30404. definitionMap.set('useClass', convertFromProviderExpression(meta.useClass));
  30405. }
  30406. if (meta.useExisting !== undefined) {
  30407. definitionMap.set('useExisting', convertFromProviderExpression(meta.useExisting));
  30408. }
  30409. if (meta.useValue !== undefined) {
  30410. definitionMap.set('useValue', convertFromProviderExpression(meta.useValue));
  30411. }
  30412. // Factories do not contain `ForwardRef`s since any types are already wrapped in a function call
  30413. // so the types will not be eagerly evaluated. Therefore we do not need to process this expression
  30414. // with `convertFromProviderExpression()`.
  30415. if (meta.useFactory !== undefined) {
  30416. definitionMap.set('useFactory', meta.useFactory);
  30417. }
  30418. if (meta.deps !== undefined) {
  30419. definitionMap.set('deps', literalArr(meta.deps.map(compileDependency)));
  30420. }
  30421. return definitionMap;
  30422. }
  30423. /**
  30424. * Convert an `R3ProviderExpression` to an `Expression`, possibly wrapping its expression in a
  30425. * `forwardRef()` call.
  30426. *
  30427. * If `R3ProviderExpression.isForwardRef` is true then the expression was originally wrapped in a
  30428. * `forwardRef()` call to prevent the value from being eagerly evaluated in the code.
  30429. *
  30430. * Normally, the linker will statically process the code, putting the `expression` inside a factory
  30431. * function so the `forwardRef()` wrapper is not evaluated before it has been defined. But if the
  30432. * partial declaration is evaluated by the JIT compiler the `forwardRef()` call is still needed to
  30433. * prevent eager evaluation of the `expression`.
  30434. *
  30435. * So in partial declarations, expressions that could be forward-refs are wrapped in `forwardRef()`
  30436. * calls, and this is then unwrapped in the linker as necessary.
  30437. *
  30438. * See `packages/compiler-cli/src/ngtsc/annotations/src/injectable.ts` and
  30439. * `packages/compiler/src/jit_compiler_facade.ts` for more information.
  30440. */
  30441. function convertFromProviderExpression({ expression, isForwardRef }) {
  30442. return isForwardRef ? generateForwardRef(expression) : expression;
  30443. }
  30444. /**
  30445. * @license
  30446. * Copyright Google LLC All Rights Reserved.
  30447. *
  30448. * Use of this source code is governed by an MIT-style license that can be
  30449. * found in the LICENSE file at https://angular.io/license
  30450. */
  30451. /**
  30452. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  30453. * must update this constant to prevent old partial-linkers from incorrectly processing the
  30454. * declaration.
  30455. *
  30456. * Do not include any prerelease in these versions as they are ignored.
  30457. */
  30458. const MINIMUM_PARTIAL_LINKER_VERSION$4 = '12.0.0';
  30459. function compileDeclareInjectorFromMetadata(meta) {
  30460. const definitionMap = createInjectorDefinitionMap(meta);
  30461. const expression = importExpr(Identifiers.declareInjector).callFn([definitionMap.toLiteralMap()]);
  30462. const type = createInjectorType(meta);
  30463. return { expression, type, statements: [] };
  30464. }
  30465. /**
  30466. * Gathers the declaration fields for an Injector into a `DefinitionMap`.
  30467. */
  30468. function createInjectorDefinitionMap(meta) {
  30469. const definitionMap = new DefinitionMap();
  30470. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$4));
  30471. definitionMap.set('version', literal('12.0.5'));
  30472. definitionMap.set('ngImport', importExpr(Identifiers.core));
  30473. definitionMap.set('type', meta.internalType);
  30474. definitionMap.set('providers', meta.providers);
  30475. if (meta.imports.length > 0) {
  30476. definitionMap.set('imports', literalArr(meta.imports));
  30477. }
  30478. return definitionMap;
  30479. }
  30480. /**
  30481. * @license
  30482. * Copyright Google LLC All Rights Reserved.
  30483. *
  30484. * Use of this source code is governed by an MIT-style license that can be
  30485. * found in the LICENSE file at https://angular.io/license
  30486. */
  30487. /**
  30488. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  30489. * must update this constant to prevent old partial-linkers from incorrectly processing the
  30490. * declaration.
  30491. *
  30492. * Do not include any prerelease in these versions as they are ignored.
  30493. */
  30494. const MINIMUM_PARTIAL_LINKER_VERSION$5 = '12.0.0';
  30495. function compileDeclareNgModuleFromMetadata(meta) {
  30496. const definitionMap = createNgModuleDefinitionMap(meta);
  30497. const expression = importExpr(Identifiers.declareNgModule).callFn([definitionMap.toLiteralMap()]);
  30498. const type = createNgModuleType(meta);
  30499. return { expression, type, statements: [] };
  30500. }
  30501. /**
  30502. * Gathers the declaration fields for an NgModule into a `DefinitionMap`.
  30503. */
  30504. function createNgModuleDefinitionMap(meta) {
  30505. const definitionMap = new DefinitionMap();
  30506. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$5));
  30507. definitionMap.set('version', literal('12.0.5'));
  30508. definitionMap.set('ngImport', importExpr(Identifiers.core));
  30509. definitionMap.set('type', meta.internalType);
  30510. // We only generate the keys in the metadata if the arrays contain values.
  30511. // We must wrap the arrays inside a function if any of the values are a forward reference to a
  30512. // not-yet-declared class. This is to support JIT execution of the `ɵɵngDeclareNgModule()` call.
  30513. // In the linker these wrappers are stripped and then reapplied for the `ɵɵdefineNgModule()` call.
  30514. if (meta.bootstrap.length > 0) {
  30515. definitionMap.set('bootstrap', refsToArray(meta.bootstrap, meta.containsForwardDecls));
  30516. }
  30517. if (meta.declarations.length > 0) {
  30518. definitionMap.set('declarations', refsToArray(meta.declarations, meta.containsForwardDecls));
  30519. }
  30520. if (meta.imports.length > 0) {
  30521. definitionMap.set('imports', refsToArray(meta.imports, meta.containsForwardDecls));
  30522. }
  30523. if (meta.exports.length > 0) {
  30524. definitionMap.set('exports', refsToArray(meta.exports, meta.containsForwardDecls));
  30525. }
  30526. if (meta.schemas !== null && meta.schemas.length > 0) {
  30527. definitionMap.set('schemas', literalArr(meta.schemas.map(ref => ref.value)));
  30528. }
  30529. if (meta.id !== null) {
  30530. definitionMap.set('id', meta.id);
  30531. }
  30532. return definitionMap;
  30533. }
  30534. /**
  30535. * @license
  30536. * Copyright Google LLC All Rights Reserved.
  30537. *
  30538. * Use of this source code is governed by an MIT-style license that can be
  30539. * found in the LICENSE file at https://angular.io/license
  30540. */
  30541. /**
  30542. * Every time we make a breaking change to the declaration interface or partial-linker behavior, we
  30543. * must update this constant to prevent old partial-linkers from incorrectly processing the
  30544. * declaration.
  30545. *
  30546. * Do not include any prerelease in these versions as they are ignored.
  30547. */
  30548. const MINIMUM_PARTIAL_LINKER_VERSION$6 = '12.0.0';
  30549. /**
  30550. * Compile a Pipe declaration defined by the `R3PipeMetadata`.
  30551. */
  30552. function compileDeclarePipeFromMetadata(meta) {
  30553. const definitionMap = createPipeDefinitionMap(meta);
  30554. const expression = importExpr(Identifiers.declarePipe).callFn([definitionMap.toLiteralMap()]);
  30555. const type = createPipeType(meta);
  30556. return { expression, type, statements: [] };
  30557. }
  30558. /**
  30559. * Gathers the declaration fields for a Pipe into a `DefinitionMap`.
  30560. */
  30561. function createPipeDefinitionMap(meta) {
  30562. const definitionMap = new DefinitionMap();
  30563. definitionMap.set('minVersion', literal(MINIMUM_PARTIAL_LINKER_VERSION$6));
  30564. definitionMap.set('version', literal('12.0.5'));
  30565. definitionMap.set('ngImport', importExpr(Identifiers.core));
  30566. // e.g. `type: MyPipe`
  30567. definitionMap.set('type', meta.internalType);
  30568. // e.g. `name: "myPipe"`
  30569. definitionMap.set('name', literal(meta.pipeName));
  30570. if (meta.pure === false) {
  30571. // e.g. `pure: false`
  30572. definitionMap.set('pure', literal(meta.pure));
  30573. }
  30574. return definitionMap;
  30575. }
  30576. /**
  30577. * @license
  30578. * Copyright Google LLC All Rights Reserved.
  30579. *
  30580. * Use of this source code is governed by an MIT-style license that can be
  30581. * found in the LICENSE file at https://angular.io/license
  30582. */
  30583. // This file only reexports content of the `src` folder. Keep it that way.
  30584. // This function call has a global side effects and publishes the compiler into global namespace for
  30585. // the late binding of the Compiler to the @angular/core for jit compilation.
  30586. publishFacade(_global);
  30587. /**
  30588. * @license
  30589. * Copyright Google LLC All Rights Reserved.
  30590. *
  30591. * Use of this source code is governed by an MIT-style license that can be
  30592. * found in the LICENSE file at https://angular.io/license
  30593. */
  30594. // This file only reexports content of the `src` folder. Keep it that way.
  30595. /**
  30596. * @license
  30597. * Copyright Google LLC All Rights Reserved.
  30598. *
  30599. * Use of this source code is governed by an MIT-style license that can be
  30600. * found in the LICENSE file at https://angular.io/license
  30601. */
  30602. /**
  30603. * @license
  30604. * Copyright Google LLC All Rights Reserved.
  30605. *
  30606. * Use of this source code is governed by an MIT-style license that can be
  30607. * found in the LICENSE file at https://angular.io/license
  30608. */
  30609. export { AST, ASTWithName, ASTWithSource, AbsoluteSourceSpan, AotCompiler, AotSummaryResolver, ArrayType, AssertNotNull, AstMemoryEfficientTransformer, AstPath, AstTransformer$1 as AstTransformer, AttrAst, Attribute, Binary, BinaryOperator, BinaryOperatorExpr, BindingPipe, BoundDirectivePropertyAst, BoundElementProperty, BoundElementPropertyAst, BoundEventAst, BoundTextAst, BuiltinMethod, BuiltinType, BuiltinTypeName, BuiltinVar, CONTENT_ATTR, CUSTOM_ELEMENTS_SCHEMA, CastExpr, Chain, ClassField, ClassMethod, ClassStmt, CommaExpr, Comment$1 as Comment, CompileDirectiveMetadata, CompileMetadataResolver, CompileNgModuleMetadata, CompilePipeMetadata, CompileReflector, CompileShallowModuleMetadata, CompileStylesheetMetadata, CompileSummaryKind, CompileTemplateMetadata, CompiledStylesheet, CompilerConfig, Conditional, ConditionalExpr, ConstantPool, CssSelector, DEFAULT_INTERPOLATION_CONFIG, DYNAMIC_TYPE, DeclareFunctionStmt, DeclareVarStmt, DirectiveAst, DirectiveNormalizer, DirectiveResolver, DomElementSchemaRegistry, EOF, ERROR_COMPONENT_TYPE, Element$1 as Element, ElementAst, ElementSchemaRegistry, EmbeddedTemplateAst, EmitterVisitorContext, EmptyExpr, Expansion, ExpansionCase, Expression, ExpressionBinding, ExpressionStatement, ExpressionType, ExternalExpr, ExternalReference, Extractor, FactoryTarget, FunctionCall, FunctionExpr, GeneratedFile, HOST_ATTR, HtmlParser, HtmlTagDefinition, I18NHtmlParser, Identifiers$1 as Identifiers, IfStmt, ImplicitReceiver, InstantiateExpr, Interpolation, InterpolationConfig, InvokeFunctionExpr, InvokeMethodExpr, IvyParser, JSDocComment, JitCompiler, JitEvaluator, JitSummaryResolver, KeyedRead, KeyedWrite, LeadingComment, Lexer, LiteralArray, LiteralArrayExpr, LiteralExpr, LiteralMap, LiteralMapExpr, LiteralPrimitive, LocalizedString, MapType, MessageBundle, MethodCall, NAMED_ENTITIES, NGSP_UNICODE, NONE_TYPE, NO_ERRORS_SCHEMA, NgContentAst, NgModuleCompiler, NgModuleResolver, NodeWithI18n, NonNullAssert, NotExpr, NullTemplateVisitor, ParseError, ParseErrorLevel, ParseLocation, ParseSourceFile, ParseSourceSpan, ParseSpan, ParseTreeResult, ParsedEvent, ParsedProperty, ParsedPropertyType, ParsedVariable, Parser$1 as Parser, ParserError, PipeResolver, PrefixNot, PropertyRead, PropertyWrite, ProviderAst, ProviderAstType, ProviderMeta, Quote, R3BoundTarget, Identifiers as R3Identifiers, R3TargetBinder, ReadKeyExpr, ReadPropExpr, ReadVarExpr, RecursiveAstVisitor$1 as RecursiveAstVisitor, RecursiveTemplateAstVisitor, RecursiveVisitor$1 as RecursiveVisitor, ReferenceAst, ResolvedStaticSymbol, ResourceLoader, ReturnStatement, STRING_TYPE, SafeMethodCall, SafePropertyRead, SelectorContext, SelectorListContext, SelectorMatcher, Serializer, SplitInterpolation, Statement, StaticReflector, StaticSymbol, StaticSymbolCache, StaticSymbolResolver, StmtModifier, StyleCompiler, StylesCompileDependency, SummaryResolver, TagContentType, TaggedTemplateExpr, TemplateBindingParseResult, TemplateLiteral, TemplateLiteralElement, TemplateParseError, TemplateParseResult, TemplateParser, Text$3 as Text, TextAst, ThisReceiver, ThrowStmt, BoundAttribute as TmplAstBoundAttribute, BoundEvent as TmplAstBoundEvent, BoundText as TmplAstBoundText, Content as TmplAstContent, Element as TmplAstElement, Icu as TmplAstIcu, RecursiveVisitor as TmplAstRecursiveVisitor, Reference as TmplAstReference, Template as TmplAstTemplate, Text as TmplAstText, TextAttribute as TmplAstTextAttribute, Variable as TmplAstVariable, Token$1 as Token, TokenType$1 as TokenType, TransitiveCompileNgModuleMetadata, TreeError, TryCatchStmt, Type$1 as Type, TypeScriptEmitter, TypeofExpr, Unary, UnaryOperator, UnaryOperatorExpr, UrlResolver, VERSION$1 as VERSION, VariableAst, VariableBinding, Version, ViewCompiler, WrappedNodeExpr, WriteKeyExpr, WritePropExpr, WriteVarExpr, Xliff, Xliff2, Xmb, XmlParser, Xtb, _ParseAST, analyzeAndValidateNgModules, analyzeFile, analyzeFileForInjectables, analyzeNgModules, collectExternalReferences, compileClassMetadata, compileComponentFromMetadata, compileDeclareClassMetadata, compileDeclareComponentFromMetadata, compileDeclareDirectiveFromMetadata, compileDeclareFactoryFunction, compileDeclareInjectableFromMetadata, compileDeclareInjectorFromMetadata, compileDeclareNgModuleFromMetadata, compileDeclarePipeFromMetadata, compileDirectiveFromMetadata, compileFactoryFunction, compileInjectable, compileInjector, compileNgModule, compilePipeFromMetadata, componentFactoryName, computeMsgId, core, createAotCompiler, createAotUrlResolver, createElementCssSelector, createInjectableType, createLoweredSymbol, createOfflineCompileUrlResolver, createR3ProviderExpression, createUrlResolverWithoutPackagePrefix, debugOutputAstAsTypeScript, devOnlyGuardedExpression, findNode, flatten, formattedError, getHtmlTagDefinition, getMissingNgModuleMetadataErrorData, getNsPrefix, getParseErrors, getSafePropertyAccessString, getUrlScheme, hostViewClassName, identifierModuleUrl, identifierName, isEmptyExpression, isFormattedError, isIdentifier, isLoweredSymbol, isNgContainer, isNgContent, isNgTemplate, isQuote, isSyntaxError, jsDocComment, leadingComment, literalMap, makeBindingParser, mergeAnalyzedFiles, mergeNsAndName, ngModuleJitUrl, parseHostBindings, parseTemplate, preserveWhitespacesDefault, publishFacade, r3JitTypeSourceSpan, removeSummaryDuplicates, rendererTypeName, sanitizeIdentifier, sharedStylesheetJitUrl, splitClasses, splitNsName, syntaxError, templateJitUrl, templateSourceUrl, templateVisitAll, toTypeScript, tokenName, tokenReference, typeSourceSpan, unescapeIdentifier, unwrapResolvedMetadata, verifyHostBindings, viewClassName, visitAll$1 as visitAll };
  30610. //# sourceMappingURL=compiler.js.map