[{"data":1,"prerenderedAt":1097},["ShallowReactive",2],{"page:\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext":3},{"id":4,"title":5,"body":6,"description":1079,"extension":1080,"frontmatter":1081,"heading":1086,"meta":1088,"navigation":1090,"order":1084,"path":1091,"seo":1092,"sitemap":1093,"source":1094,"stem":1095,"__hash__":1096},"docs\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext.md","Context - this, ctx and useKubb() in every hook",{"type":7,"value":8,"toc":1066},"minimark",[9,34,39,48,217,226,294,298,307,454,462,761,766,777,783,811,885,888,967,971,990],[10,11,12,13,17,18,21,22,25,26,29,30,33],"p",{},"Kubb runs every plugin hook with one context object. In each hook, ",[14,15,16],"code",{},"this",", the ",[14,19,20],{},"ctx"," argument and ",[14,23,24],{},"useKubb()"," are that same object, also after an ",[14,27,28],{},"await",". Composables read it, so they work in every hook, not only in ",[14,31,32],{},"setup",".",[35,36,38],"h2",{"id":37},"where-code-runs","Where code runs",[10,40,41,42,44,45,47],{},"The context depends on where your code runs. Printer handlers and macro callbacks are the exceptions: they get their own small context as ",[14,43,16],{}," and as their second argument, while ",[14,46,24],{}," keeps returning the context of the hook that runs them.",[49,50,51,73],"table",{},[52,53,54],"thead",{},[55,56,57,61,68],"tr",{},[58,59,60],"th",{},"Code",[58,62,63,65,66],{},[14,64,16],{}," and ",[14,67,20],{},[58,69,70,72],{},[14,71,24],{}," returns",[74,75,76,91,117,131,153,180,200],"tbody",{},[55,77,78,86,89],{},[79,80,81,65,83],"td",{},[14,82,32],{},[14,84,85],{},"kubb:plugin:setup",[79,87,88],{},"The setup context",[79,90,88],{},[55,92,93,112,115],{},[79,94,95,96,99,100,99,103,99,106,99,109],{},"Events Kubb fires: ",[14,97,98],{},"kubb:build:start",", ",[14,101,102],{},"kubb:plugin:start",[14,104,105],{},"kubb:plugin:end",[14,107,108],{},"kubb:plugins:end",[14,110,111],{},"kubb:build:end",[79,113,114],{},"The plugin's own visit context",[79,116,114],{},[55,118,119,125,128],{},[79,120,121,122],{},"Events fired inside a hook, with ",[14,123,124],{},"ctx.hooks.callHook(...)",[79,126,127],{},"The context of the hook that fired the event",[79,129,130],{},"The context of the hook that fired it",[55,132,133,148,151],{},[79,134,135,136,99,139,99,142,99,145],{},"Visit hooks: ",[14,137,138],{},"kubb:plugin:visit:start",[14,140,141],{},":schema",[14,143,144],{},":operation",[14,146,147],{},":end",[79,149,150],{},"The plugin's visit context",[79,152,150],{},[55,154,155,175,178],{},[79,156,157,158,99,161,99,164,99,167,170,171,174],{},"Generator ",[14,159,160],{},"match",[14,162,163],{},"schema",[14,165,166],{},"operation",[14,168,169],{},"operations",", the printer factory and the ",[14,172,173],{},"kubb:generate:*"," events",[79,176,177],{},"The generator context",[79,179,177],{},[55,181,182,189,198],{},[79,183,184],{},[185,186,188],"a",{"href":187},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fast#printer-handlers-and-context","Printer handlers",[79,190,191,192,65,195],{},"The printer context, with ",[14,193,194],{},"transform",[14,196,197],{},"base",[79,199,177],{},[55,201,202,208,214],{},[79,203,204],{},[185,205,207],{"href":206},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fast#macro-options-and-callbacks","Macro callbacks",[79,209,210,211],{},"The walk context, with the node's ",[14,212,213],{},"parent",[79,215,216],{},"The visit context of the running hook",[10,218,219,220,222,223,33],{},"A plugin has one visit context per build. Its bus hooks and its visit hooks get the same object, so a value you set on it in ",[14,221,98],{}," is there in ",[14,224,225],{},"kubb:plugin:visit:schema",[227,228,229,248,275],"ul",{},[230,231,232,233,237,238,99,241,99,244,247],"li",{},"The ",[185,234,236],{"href":235},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fplugins#setup-context","setup context"," holds ",[14,239,240],{},"options",[14,242,243],{},"setOptions",[14,245,246],{},"injectFile"," and the members every context has.",[230,249,250,251,99,254,99,257,99,260,99,263,99,266,269,270,274],{},"The visit context holds ",[14,252,253],{},"config",[14,255,256],{},"plugin",[14,258,259],{},"memo",[14,261,262],{},"hooks",[14,264,265],{},"requirePlugin",[14,267,268],{},"upsertFile"," and the other ",[185,271,273],{"href":272},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fhooks#visit-hooks","visit hook"," members.",[230,276,232,277,281,282,99,285,288,289,65,291,33],{},[185,278,280],{"href":279},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fgenerators#generatorcontext","generator context"," is the visit context plus ",[14,283,284],{},"resolver",[14,286,287],{},"getResolver(name)",", per-node ",[14,290,240],{},[14,292,293],{},"printer",[35,295,297],{"id":296},"composables","Composables",[10,299,300,301,303,304,306],{},"Use composables for plugin-level concepts in ",[14,302,32],{},", and read ",[14,305,20],{}," for the rest.",[49,308,309,319],{},[52,310,311],{},[55,312,313,316],{},[58,314,315],{},"Composable",[58,317,318],{},"Works in",[74,320,321,340,353,372,382,395,418],{},[55,322,323,330],{},[79,324,325,99,327],{},[14,326,24],{},[14,328,329],{},"tryUseKubb()",[79,331,332,333,335,336,339],{},"Any hook. ",[14,334,329],{}," returns ",[14,337,338],{},"undefined"," outside one instead of throwing.",[55,341,342,350],{},[79,343,344,99,347],{},[14,345,346],{},"useConfig()",[14,348,349],{},"requirePlugin(name)",[79,351,352],{},"Any hook.",[55,354,355,369],{},[79,356,357,99,360,99,363,99,366],{},[14,358,359],{},"memo(key, fn)",[14,361,362],{},"memo.get",[14,364,365],{},"memo.set",[14,367,368],{},"memo.has",[79,370,371],{},"Any hook. They read the build memo of the running hook.",[55,373,374,379],{},[79,375,376],{},[14,377,378],{},"memo(store, key, fn)",[79,380,381],{},"Anywhere. It caches in the store you pass and needs no hook.",[55,383,384,388],{},[79,385,386],{},[14,387,287],{},[79,389,390,391,394],{},"Any hook. Pass a memo as the second argument, ",[14,392,393],{},"getResolver(name, memo)",", outside a hook.",[55,396,397,408],{},[79,398,399,99,402,99,405],{},[14,400,401],{},"setOptions()",[14,403,404],{},"useOptions()",[14,406,407],{},"injectFile()",[79,409,410,65,412,414,415,33],{},[14,411,32],{},[14,413,85],{},". Elsewhere they throw ",[14,416,417],{},"KUBB_NO_CONTEXT",[55,419,420,440],{},[79,421,422,99,425,99,428,99,431,99,434,99,437],{},[14,423,424],{},"addGenerator()",[14,426,427],{},"setResolver()",[14,429,430],{},"addMacro()",[14,432,433],{},"setMacros()",[14,435,436],{},"setPrinter()",[14,438,439],{},"provide()",[79,441,442,65,444,446,447,450,451,33],{},[14,443,32],{},[14,445,85],{}," of a plugin defined with ",[14,448,449],{},"defineExtension"," from ",[14,452,453],{},"kubb\u002Fkit",[10,455,456,457,33],{},"Outside every hook, a composable that needs a context throws ",[185,458,460],{"href":459},"\u002Fdocs\u002F5.x\u002Freference\u002Fdiagnostics#kubb-no-context",[14,461,417],{},[463,464,469],"pre",{"language":465,"filename":466,"code":467,"class":468},"typescript","plugin.ts","import { defineExtension, getResolver, memo, useConfig } from 'kubb\u002Fkit'\n\nexport const pluginExample = defineExtension({\n  kind: 'plugin',\n  meta: { name: 'plugin-example' },\n  hooks: {\n    'kubb:plugin:visit:schema'(node) {\n      const names = memo('plugin-example:names', () => new Set\u003Cstring>())\n      if (node.name) names.add(getResolver('plugin-ts').name(node.name))\n      console.log(useConfig().root, this.plugin.name)\n    },\n  },\n})","highlights",[14,470,472,512,513,512,515,512,535,512,552,512,576,512,587,512,604,512,649,512,702,512,741,512,748,512,756],{"class":471},"language-typescript",[473,474,477,481,485,488,490,493,495,497,499,502,505,508],"span",{"class":475,"style":476},"line","display: inline",[473,478,480],{"style":479},"color:#d73a49;--hls-dark:#f97583","import ",[473,482,484],{"style":483},"color:#005cc5;--hls-dark:#79b8ff","{ ",[473,486,449],{"style":487},"color:#24292e;--hls-dark:#e1e4e8",[473,489,99],{"style":479},[473,491,492],{"style":487},"getResolver",[473,494,99],{"style":479},[473,496,259],{"style":487},[473,498,99],{"style":479},[473,500,501],{"style":487},"useConfig ",[473,503,504],{"style":483},"} ",[473,506,507],{"style":479},"from ",[473,509,511],{"style":510},"color:#032f62;--hls-dark:#9ecbff","'kubb\u002Fkit'","\n",[473,514],{"class":475,"style":476},[473,516,517,520,523,526,529,532],{"class":475,"style":476},[473,518,519],{"style":479},"export ",[473,521,522],{"style":479},"const ",[473,524,525],{"style":487},"pluginExample ",[473,527,528],{"style":479},"= ",[473,530,449],{"style":531},"color:#6f42c1;--hls-dark:#b392f0",[473,533,534],{"style":483},"({",[473,536,537,540,543,546,549],{"class":475,"style":476},[473,538,539],{"style":483},"  ",[473,541,542],{"style":483},"kind",[473,544,545],{"style":479},": ",[473,547,548],{"style":510},"'plugin'",[473,550,551],{"style":479},",",[473,553,554,556,559,561,563,566,568,571,574],{"class":475,"style":476},[473,555,539],{"style":479},[473,557,558],{"style":483},"meta",[473,560,545],{"style":479},[473,562,484],{"style":483},[473,564,565],{"style":483},"name",[473,567,545],{"style":479},[473,569,570],{"style":510},"'plugin-example' ",[473,572,573],{"style":483},"}",[473,575,551],{"style":479},[473,577,578,580,582,584],{"class":475,"style":476},[473,579,539],{"style":479},[473,581,262],{"style":483},[473,583,545],{"style":479},[473,585,586],{"style":483},"{",[473,588,589,592,595,598,601],{"class":475,"style":476},[473,590,591],{"style":483},"    ",[473,593,594],{"style":510},"'kubb:plugin:visit:schema'",[473,596,597],{"style":483},"(",[473,599,600],{"style":487},"node",[473,602,603],{"style":483},") {",[473,605,606,609,611,614,616,618,620,623,625,628,631,634,637,640,643,646],{"class":475,"style":476},[473,607,608],{"style":483},"      ",[473,610,522],{"style":479},[473,612,613],{"style":487},"names ",[473,615,528],{"style":479},[473,617,259],{"style":531},[473,619,597],{"style":483},[473,621,622],{"style":510},"'plugin-example:names'",[473,624,99],{"style":479},[473,626,627],{"style":483},"() ",[473,629,630],{"style":479},"=> ",[473,632,633],{"style":479},"new ",[473,635,636],{"style":531},"Set",[473,638,639],{"style":479},"\u003C",[473,641,642],{"style":487},"string",[473,644,645],{"style":479},">",[473,647,648],{"style":483},"())",[473,650,651,653,656,658,660,662,664,667,670,672,675,677,679,681,684,687,689,691,693,695,697,699],{"class":475,"style":476},[473,652,608],{"style":483},[473,654,655],{"style":479},"if ",[473,657,597],{"style":483},[473,659,600],{"style":487},[473,661,33],{"style":479},[473,663,565],{"style":483},[473,665,666],{"style":483},") ",[473,668,669],{"style":487},"names",[473,671,33],{"style":479},[473,673,674],{"style":531},"add",[473,676,597],{"style":483},[473,678,492],{"style":531},[473,680,597],{"style":483},[473,682,683],{"style":510},"'plugin-ts'",[473,685,686],{"style":483},")",[473,688,33],{"style":479},[473,690,565],{"style":531},[473,692,597],{"style":483},[473,694,600],{"style":487},[473,696,33],{"style":479},[473,698,565],{"style":483},[473,700,701],{"style":483},"))",[473,703,704,706,709,711,714,716,719,722,724,727,729,731,733,735,737,739],{"class":475,"style":476},[473,705,608],{"style":483},[473,707,708],{"style":487},"console",[473,710,33],{"style":479},[473,712,713],{"style":531},"log",[473,715,597],{"style":483},[473,717,718],{"style":531},"useConfig",[473,720,721],{"style":483},"()",[473,723,33],{"style":479},[473,725,726],{"style":483},"root",[473,728,99],{"style":479},[473,730,16],{"style":487},[473,732,33],{"style":479},[473,734,256],{"style":483},[473,736,33],{"style":479},[473,738,565],{"style":483},[473,740,686],{"style":483},[473,742,743,746],{"class":475,"style":476},[473,744,745],{"style":483},"    }",[473,747,551],{"style":479},[473,749,750,752,754],{"class":475,"style":476},[473,751,539],{"style":479},[473,753,573],{"style":483},[473,755,551],{"style":479},[473,757,758],{"class":475,"style":476},[473,759,760],{"style":483},"})",[762,763,765],"h3",{"id":764},"setup-methods","Setup methods",[10,767,768,769,450,771,773,774,33],{},"In a plugin defined with ",[14,770,449],{},[14,772,453],{},", the setup context also has the setup composables as methods. See ",[185,775,776],{"href":235},"Setup context",[35,778,780],{"id":779},"withcontext",[14,781,782],{},"withContext",[10,784,785,788,789,792,793,795,796,798,799,801,802,804,805,807,808,810],{},[14,786,787],{},"withContext(ctx, fn)"," runs ",[14,790,791],{},"fn"," with ",[14,794,20],{}," as its ",[14,797,16],{},", its argument and the value ",[14,800,24],{}," returns, also after an ",[14,803,28],{}," in ",[14,806,791],{},". It returns what ",[14,809,791],{}," returns. Kubb runs every hook through it.",[463,812,815],{"language":465,"filename":813,"code":814,"class":468},"Type definition","function withContext\u003CTContext extends object, TResult>(ctx: TContext, fn: (this: TContext, ctx: TContext) => TResult): TResult",[14,816,817],{"class":471},[473,818,819,822,824,826,829,832,835,837,840,842,844,846,848,851,853,855,857,859,861,863,865,867,869,871,873,875,877,879,881,883],{"class":475,"style":476},[473,820,821],{"style":479},"function ",[473,823,782],{"style":531},[473,825,639],{"style":479},[473,827,828],{"style":531},"TContext ",[473,830,831],{"style":479},"extends ",[473,833,834],{"style":531},"object",[473,836,99],{"style":479},[473,838,839],{"style":531},"TResult",[473,841,645],{"style":479},[473,843,597],{"style":483},[473,845,20],{"style":487},[473,847,545],{"style":487},[473,849,850],{"style":531},"TContext",[473,852,99],{"style":479},[473,854,791],{"style":487},[473,856,545],{"style":479},[473,858,597],{"style":483},[473,860,16],{"style":487},[473,862,545],{"style":487},[473,864,850],{"style":531},[473,866,99],{"style":479},[473,868,20],{"style":483},[473,870,545],{"style":487},[473,872,850],{"style":531},[473,874,666],{"style":483},[473,876,630],{"style":479},[473,878,839],{"style":531},[473,880,686],{"style":483},[473,882,545],{"style":487},[473,884,839],{"style":531},[10,886,887],{},"Extension authors use it to run their own callbacks with a context, so composables work there too:",[463,889,891],{"language":465,"code":890,"class":468},"import { useKubb, withContext } from 'kubb\u002Fkit'\n\nawait withContext(ctx, async function () {\n  console.log(this === useKubb()) \u002F\u002F true\n})",[14,892,893,512,913,512,915,512,936,512,963],{"class":471},[473,894,895,897,899,902,904,907,909,911],{"class":475,"style":476},[473,896,480],{"style":479},[473,898,484],{"style":483},[473,900,901],{"style":487},"useKubb",[473,903,99],{"style":479},[473,905,906],{"style":487},"withContext ",[473,908,504],{"style":483},[473,910,507],{"style":479},[473,912,511],{"style":510},[473,914],{"class":475,"style":476},[473,916,917,920,922,924,926,928,931,933],{"class":475,"style":476},[473,918,919],{"style":479},"await ",[473,921,782],{"style":531},[473,923,597],{"style":483},[473,925,20],{"style":487},[473,927,99],{"style":479},[473,929,930],{"style":479},"async ",[473,932,821],{"style":479},[473,934,935],{"style":483},"() {",[473,937,938,940,942,944,946,948,951,954,956,959],{"class":475,"style":476},[473,939,539],{"style":483},[473,941,708],{"style":487},[473,943,33],{"style":479},[473,945,713],{"style":531},[473,947,597],{"style":483},[473,949,950],{"style":487},"this ",[473,952,953],{"style":479},"=== ",[473,955,901],{"style":531},[473,957,958],{"style":483},"()) ",[473,960,962],{"style":961},"color:#6a737d;--hls-dark:#6a737d","\u002F\u002F true",[473,964,965],{"class":475,"style":476},[473,966,760],{"style":483},[762,968,970],{"id":969},"in-tests","In tests",[10,972,973,974,976,977,980,981,983,984,986,987,989],{},"A plugin test can call a generator or hook through ",[14,975,782],{}," instead of ",[14,978,979],{},"kubbContext.run",". It also sets ",[14,982,16],{},", so code that reads ",[14,985,16],{}," or ",[14,988,24],{}," sees your test context:",[463,991,994],{"language":465,"filename":992,"code":993,"class":468},"generator.test.ts","import { withContext } from 'kubb\u002Fkit'\n\nconst files = await withContext(ctx, (ctx) => typeGenerator.schema?.call(ctx, node, ctx))",[14,995,996,512,1010,512,1012],{"class":471},[473,997,998,1000,1002,1004,1006,1008],{"class":475,"style":476},[473,999,480],{"style":479},[473,1001,484],{"style":483},[473,1003,906],{"style":487},[473,1005,504],{"style":483},[473,1007,507],{"style":479},[473,1009,511],{"style":510},[473,1011],{"class":475,"style":476},[473,1013,1014,1016,1019,1021,1023,1025,1027,1029,1031,1033,1035,1037,1039,1042,1044,1046,1049,1052,1054,1056,1058,1060,1062,1064],{"class":475,"style":476},[473,1015,522],{"style":479},[473,1017,1018],{"style":487},"files ",[473,1020,528],{"style":479},[473,1022,919],{"style":479},[473,1024,782],{"style":531},[473,1026,597],{"style":483},[473,1028,20],{"style":487},[473,1030,99],{"style":479},[473,1032,597],{"style":483},[473,1034,20],{"style":487},[473,1036,666],{"style":483},[473,1038,630],{"style":479},[473,1040,1041],{"style":487},"typeGenerator",[473,1043,33],{"style":479},[473,1045,163],{"style":483},[473,1047,1048],{"style":479},"?.",[473,1050,1051],{"style":531},"call",[473,1053,597],{"style":483},[473,1055,20],{"style":487},[473,1057,99],{"style":479},[473,1059,600],{"style":487},[473,1061,99],{"style":479},[473,1063,20],{"style":487},[473,1065,701],{"style":483},{"title":1067,"searchDepth":1068,"depth":1068,"links":1069},"",5,[1070,1072,1076],{"id":37,"depth":1071,"text":38},2,{"id":296,"depth":1071,"text":297,"children":1073},[1074],{"id":764,"depth":1075,"text":765},3,{"id":779,"depth":1071,"text":782,"children":1077},[1078],{"id":969,"depth":1075,"text":970},"Reference for the context Kubb passes to plugin code: what this, the ctx argument and useKubb() are in each hook, which composables work where, and withContext.","md",{"layout":1082,"title":5,"description":1079,"outline":1083,"order":1084,"navigation":1085},"doc",[1071,1075],12,{"title":1086,"icon":1087},"Context","i-iconoir-mirror",{"layout":1082,"outline":1089},[1071,1075],{"title":1086,"icon":1087},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext",{"title":5,"description":1079},{"loc":1091},"kubb-labs\u002Fkubb\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fcontext.md","docs\u002F5.x\u002Freference\u002Fkit\u002Fcontext","_x9Dv_k9begbP4GQmKWvfcYyJN0kPC7_CtZxaretHUc",1795383540370]