[{"data":1,"prerenderedAt":1012},["ShallowReactive",2],{"page:\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fparsers":3},{"id":4,"title":5,"body":6,"description":994,"extension":995,"frontmatter":996,"heading":5,"meta":1002,"navigation":1004,"order":999,"path":1005,"seo":1006,"sitemap":1007,"source":1008,"stem":1009,"__hash__":1010,"hasPopups":1011},"docs\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fparsers.md","Parsers",{"type":7,"value":8,"toc":983},"minimark",[9,18,39,46,63,517,535,543,555,572,575,677,681,703,858,866,869,873,884,952,957],[10,11,12,13,17],"p",{},"A parser turns a ",[14,15,16],"code",{},"FileNode"," into source code.",[19,20,22,23,30,31,34,35,38],"blockquote",{"as":21},"tip","For TypeScript and JavaScript output use the built-in ",[24,25,27],"a",{"href":26},"\u002Fextensions\u002Fkubb-labs\u002Fparser-ts",[14,28,29],{},"@kubb\u002Fparser-ts",". It is added by default when you import ",[14,32,33],{},"defineConfig"," from the ",[14,36,37],{},"kubb"," package. Build a custom parser only when you target a different language, such as Python, Kotlin, or Rust.",[40,41,43],"h2",{"id":42},"defineextension",[14,44,45],{},"defineExtension",[10,47,48,50,51,54,55,58,59,62],{},[14,49,45],{}," with ",[14,52,53],{},"kind: 'parser'"," takes a definition object and returns a factory. ",[14,56,57],{},"build"," receives the caller's options, or an empty object when omitted, and returns the parser methods. Declare extensions with ",[14,60,61],{},"extNames",":",[64,65,71],"pre",{"language":66,"meta":67,"code":68,"filename":69,"class":70},"typescript","twoslash","import { defineExtension } from 'kubb\u002Fkit'\n\nexport const parserText = defineExtension({\n  kind: 'parser',\n  meta: { name: 'text', version: '1.0.0' },\n  build() {\n    return {\n      extNames: ['.txt'],\n      parse(file) {\n        return file.sources\n          .flatMap((source) => source.nodes ?? [])\n          .map((node) => (node.kind === 'Text' ? node.value : ''))\n          .join('\\n')\n      },\n      print(...nodes) {\n        return nodes.map(String).join('\\n')\n      },\n    }\n  },\n})","parserText.ts","shiki shiki-themes github-light github-dark twoslash lsp dark:github-dark",[14,72,74,103,104,103,106,103,138,103,158,103,199,103,211,103,219,103,238,103,261,103,282,103,331,103,404,103,427,103,432,103,454,103,498,103,502,103,507,103,512],{"class":73},"language-typescript",[75,76,79,83,87,93,96,99],"span",{"class":77,"style":78},"line","display: inline",[75,80,82],{"style":81},"color:#D73A49;--shiki-dark:#F97583","import",[75,84,86],{"style":85},"color:#24292E;--shiki-dark:#E1E4E8"," { ",[75,88,89],{"style":85},[75,90,45],{"class":91,"data-popup":92},"twoslash-hover","0",[75,94,95],{"style":85}," } ",[75,97,98],{"style":81},"from",[75,100,102],{"style":101},"color:#032F62;--shiki-dark:#9ECBFF"," 'kubb\u002Fkit'","\n",[75,105],{"class":77,"style":78},[75,107,108,111,114,118,124,127,130,135],{"class":77,"style":78},[75,109,110],{"style":81},"export",[75,112,113],{"style":81}," const",[75,115,117],{"style":116},"color:#005CC5;--shiki-dark:#79B8FF"," ",[75,119,120],{"style":116},[75,121,123],{"class":91,"data-popup":122},"1","parserText",[75,125,126],{"style":81}," =",[75,128,117],{"style":129},"color:#6F42C1;--shiki-dark:#B392F0",[75,131,132],{"style":129},[75,133,45],{"class":91,"data-popup":134},"2",[75,136,137],{"style":85},"({",[75,139,140,143,149,152,155],{"class":77,"style":78},[75,141,142],{"style":85},"  ",[75,144,145],{"style":85},[75,146,148],{"class":91,"data-popup":147},"3","kind",[75,150,151],{"style":85},": ",[75,153,154],{"style":101},"'parser'",[75,156,157],{"style":85},",",[75,159,160,162,168,171,177,179,182,185,191,193,196],{"class":77,"style":78},[75,161,142],{"style":85},[75,163,164],{"style":85},[75,165,167],{"class":91,"data-popup":166},"4","meta",[75,169,170],{"style":85},": { ",[75,172,173],{"style":85},[75,174,176],{"class":91,"data-popup":175},"5","name",[75,178,151],{"style":85},[75,180,181],{"style":101},"'text'",[75,183,184],{"style":85},", ",[75,186,187],{"style":85},[75,188,190],{"class":91,"data-popup":189},"6","version",[75,192,151],{"style":85},[75,194,195],{"style":101},"'1.0.0'",[75,197,198],{"style":85}," },",[75,200,201,203,208],{"class":77,"style":78},[75,202,142],{"style":129},[75,204,205],{"style":129},[75,206,57],{"class":91,"data-popup":207},"7",[75,209,210],{"style":85},"() {",[75,212,213,216],{"class":77,"style":78},[75,214,215],{"style":81},"    return",[75,217,218],{"style":85}," {",[75,220,221,224,229,232,235],{"class":77,"style":78},[75,222,223],{"style":85},"      ",[75,225,226],{"style":85},[75,227,61],{"class":91,"data-popup":228},"8",[75,230,231],{"style":85},": [",[75,233,234],{"style":101},"'.txt'",[75,236,237],{"style":85},"],",[75,239,240,242,248,251,258],{"class":77,"style":78},[75,241,223],{"style":129},[75,243,244],{"style":129},[75,245,247],{"class":91,"data-popup":246},"9","parse",[75,249,250],{"style":85},"(",[75,252,254],{"style":253},"color:#E36209;--shiki-dark:#FFAB70",[75,255,257],{"class":91,"data-popup":256},"10","file",[75,259,260],{"style":85},") {",[75,262,263,266,268,273,276],{"class":77,"style":78},[75,264,265],{"style":81},"        return",[75,267,117],{"style":85},[75,269,270],{"style":85},[75,271,257],{"class":91,"data-popup":272},"11",[75,274,275],{"style":85},".",[75,277,278],{"style":85},[75,279,281],{"class":91,"data-popup":280},"12","sources",[75,283,284,287,293,296,302,305,308,310,315,317,323,325,328],{"class":77,"style":78},[75,285,286],{"style":85},"          .",[75,288,289],{"style":129},[75,290,292],{"class":91,"data-popup":291},"13","flatMap",[75,294,295],{"style":85},"((",[75,297,298],{"style":253},[75,299,301],{"class":91,"data-popup":300},"14","source",[75,303,304],{"style":85},") ",[75,306,307],{"style":81},"=>",[75,309,117],{"style":85},[75,311,312],{"style":85},[75,313,301],{"class":91,"data-popup":314},"15",[75,316,275],{"style":85},[75,318,319],{"style":85},[75,320,322],{"class":91,"data-popup":321},"16","nodes",[75,324,117],{"style":85},[75,326,327],{"style":81},"??",[75,329,330],{"style":85}," [])",[75,332,333,335,341,343,349,351,353,356,361,363,368,370,373,376,379,381,386,388,394,396,398,401],{"class":77,"style":78},[75,334,286],{"style":85},[75,336,337],{"style":129},[75,338,340],{"class":91,"data-popup":339},"17","map",[75,342,295],{"style":85},[75,344,345],{"style":253},[75,346,348],{"class":91,"data-popup":347},"18","node",[75,350,304],{"style":85},[75,352,307],{"style":81},[75,354,355],{"style":85}," (",[75,357,358],{"style":85},[75,359,348],{"class":91,"data-popup":360},"19",[75,362,275],{"style":85},[75,364,365],{"style":85},[75,366,148],{"class":91,"data-popup":367},"20",[75,369,117],{"style":85},[75,371,372],{"style":81},"===",[75,374,375],{"style":101}," 'Text'",[75,377,378],{"style":81}," ?",[75,380,117],{"style":85},[75,382,383],{"style":85},[75,384,348],{"class":91,"data-popup":385},"21",[75,387,275],{"style":85},[75,389,390],{"style":85},[75,391,393],{"class":91,"data-popup":392},"22","value",[75,395,117],{"style":85},[75,397,62],{"style":81},[75,399,400],{"style":101}," ''",[75,402,403],{"style":85},"))",[75,405,406,408,414,416,419,422,424],{"class":77,"style":78},[75,407,286],{"style":85},[75,409,410],{"style":129},[75,411,413],{"class":91,"data-popup":412},"23","join",[75,415,250],{"style":85},[75,417,418],{"style":101},"'",[75,420,421],{"style":116},"\\n",[75,423,418],{"style":101},[75,425,426],{"style":85},")",[75,428,429],{"class":77,"style":78},[75,430,431],{"style":85},"      },",[75,433,434,436,442,444,447,452],{"class":77,"style":78},[75,435,223],{"style":129},[75,437,438],{"style":129},[75,439,441],{"class":91,"data-popup":440},"24","print",[75,443,250],{"style":85},[75,445,446],{"style":81},"...",[75,448,449],{"style":253},[75,450,322],{"class":91,"data-popup":451},"25",[75,453,260],{"style":85},[75,455,456,458,460,465,467,472,474,480,483,488,490,492,494,496],{"class":77,"style":78},[75,457,265],{"style":81},[75,459,117],{"style":85},[75,461,462],{"style":85},[75,463,322],{"class":91,"data-popup":464},"26",[75,466,275],{"style":85},[75,468,469],{"style":129},[75,470,340],{"class":91,"data-popup":471},"27",[75,473,250],{"style":85},[75,475,476],{"style":85},[75,477,479],{"class":91,"data-popup":478},"28","String",[75,481,482],{"style":85},").",[75,484,485],{"style":129},[75,486,413],{"class":91,"data-popup":487},"29",[75,489,250],{"style":85},[75,491,418],{"style":101},[75,493,421],{"style":116},[75,495,418],{"style":101},[75,497,426],{"style":85},[75,499,500],{"class":77,"style":78},[75,501,431],{"style":85},[75,503,504],{"class":77,"style":78},[75,505,506],{"style":85},"    }",[75,508,509],{"class":77,"style":78},[75,510,511],{"style":85},"  },",[75,513,514],{"class":77,"style":78},[75,515,516],{"style":85},"})",[10,518,519,520,522,523,526,527,530,531,275],{},"The parser's ",[14,521,176],{}," comes from ",[14,524,525],{},"meta.name",". Kubb checks ",[14,528,529],{},"meta.compatibility"," before the build, the same way it does for plugins. See ",[24,532,534],{"href":533},".\u002Fextensions","Extensions",[536,537,539,542],"h3",{"id":538},"defineparser-deprecated",[14,540,541],{},"defineParser"," (deprecated)",[19,544,546,547,549,550,50,552,554],{"as":545},"warning","",[14,548,541],{}," is deprecated. Use ",[14,551,45],{},[14,553,53],{}," instead.",[10,556,557,559,560,562,563,565,566,568,569,275],{},[14,558,541],{}," takes the same definition object as ",[14,561,45],{},", without ",[14,564,148],{},". To move over, rename the call and add ",[14,567,53],{},". See ",[24,570,534],{"href":571},".\u002Fextensions#define-extension",[10,573,574],{},"Wire it into your config:",[64,576,580],{"language":66,"filename":577,"code":578,"class":579},"kubb.config.ts","import { defineConfig } from 'kubb\u002Fconfig'\nimport { parserTs, parserTsx } from '@kubb\u002Fparser-ts'\nimport { parserText } from '.\u002FparserText.ts'\n\nexport default defineConfig({\n  input: '.\u002FpetStore.yaml',\n  output: { path: '.\u002Fsrc\u002Fgen' },\n  parsers: [parserTs(), parserTsx(), parserText()],\n})","shiki shiki-themes github-light github-dark dark:github-dark",[14,581,582,103,594,103,606,103,618,103,620,103,632,103,642,103,652,103,673],{"class":73},[75,583,584,586,589,591],{"class":77,"style":78},[75,585,82],{"style":81},[75,587,588],{"style":85}," { defineConfig } ",[75,590,98],{"style":81},[75,592,593],{"style":101}," 'kubb\u002Fconfig'",[75,595,596,598,601,603],{"class":77,"style":78},[75,597,82],{"style":81},[75,599,600],{"style":85}," { parserTs, parserTsx } ",[75,602,98],{"style":81},[75,604,605],{"style":101}," '@kubb\u002Fparser-ts'",[75,607,608,610,613,615],{"class":77,"style":78},[75,609,82],{"style":81},[75,611,612],{"style":85}," { parserText } ",[75,614,98],{"style":81},[75,616,617],{"style":101}," '.\u002FparserText.ts'",[75,619],{"class":77,"style":78},[75,621,622,624,627,630],{"class":77,"style":78},[75,623,110],{"style":81},[75,625,626],{"style":81}," default",[75,628,629],{"style":129}," defineConfig",[75,631,137],{"style":85},[75,633,634,637,640],{"class":77,"style":78},[75,635,636],{"style":85},"  input: ",[75,638,639],{"style":101},"'.\u002FpetStore.yaml'",[75,641,157],{"style":85},[75,643,644,647,650],{"class":77,"style":78},[75,645,646],{"style":85},"  output: { path: ",[75,648,649],{"style":101},"'.\u002Fsrc\u002Fgen'",[75,651,198],{"style":85},[75,653,654,657,660,663,666,668,670],{"class":77,"style":78},[75,655,656],{"style":85},"  parsers: [",[75,658,659],{"style":129},"parserTs",[75,661,662],{"style":85},"(), ",[75,664,665],{"style":129},"parserTsx",[75,667,662],{"style":85},[75,669,123],{"style":129},[75,671,672],{"style":85},"()],",[75,674,675],{"class":77,"style":78},[75,676,516],{"style":85},[40,678,680],{"id":679},"parser-anatomy","Parser anatomy",[10,682,683,684,687,688,694,695,697,698,700,701,62],{},"Every parser the factory returns matches the ",[14,685,686],{},"Parser"," interface from ",[24,689,691],{"href":690},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit",[14,692,693],{},"kubb\u002Fkit",". ",[14,696,57],{}," returns every field except ",[14,699,176],{}," and ",[14,702,167],{},[704,705,706,728],"table",{},[707,708,709],"thead",{},[710,711,712,716,719,722,725],"tr",{},[713,714,715],"th",{},"Property",[713,717,718],{},"Type",[713,720,721],{},"Required",[713,723,724],{},"When called",[713,726,727],{},"Purpose",[729,730,731,757,779,798,823],"tbody",{},[710,732,733,738,743,746,748],{},[734,735,736],"td",{},[14,737,176],{},[734,739,740],{},[14,741,742],{},"string",[734,744,745],{},"Yes",[734,747],{},[734,749,750,751,753,754,275],{},"Unique parser identifier, filled from ",[14,752,525],{},". Use the output language or format, such as ",[14,755,756],{},"'typescript'",[710,758,759,763,768,770,772],{},[734,760,761],{},[14,762,61],{},[734,764,765],{},[14,766,767],{},"Array\u003CFileNode['extname']> | undefined",[734,769,745],{},[734,771],{},[734,773,774,775,778],{},"File extensions this parser handles. Set to ",[14,776,777],{},"undefined"," to register a catch-all fallback.",[710,780,781,785,790,792,795],{},[734,782,783],{},[14,784,247],{},[734,786,787],{},[14,788,789],{},"(file: FileNode) => string",[734,791,745],{},[734,793,794],{},"After all plugins run",[734,796,797],{},"Serializes the file's sources into the final output string. Must return synchronously.",[710,799,800,804,809,811,814],{},[734,801,802],{},[14,803,441],{},[734,805,806],{},[14,807,808],{},"(...nodes: TNode[]) => string",[734,810,745],{},[734,812,813],{},"By plugins, while they generate files",[734,815,816,817,820,821,275],{},"Renders compiler AST nodes to source text. The node type is parser-specific, for example ",[14,818,819],{},"ts.Node"," for ",[14,822,659],{},[710,824,825,830,835,838,844],{},[734,826,827],{},[14,828,829],{},"copy",[734,831,832],{},[14,833,834],{},"(file: FileNode, source: string) => UserFileNode",[734,836,837],{},"No",[734,839,840,841,843],{},"For each ",[14,842,829],{}," file",[734,845,846,847,850,851,854,855,857],{},"Describes a copied template's raw content as nodes, for example its imports as ",[14,848,849],{},"ImportNode","s, in the same shape ",[14,852,853],{},"injectFile"," takes. Kubb prints the result with ",[14,856,247],{},". Omit it to write copied files verbatim.",[19,859,861,862,865],{"as":860},"important","If two parsers register the same extension, the last one in the ",[14,863,864],{},"parsers"," array wins. Order matters.",[10,867,868],{},"When no parser matches a file's extension, Kubb writes the file's source strings joined together.",[40,870,872],{"id":871},"parser-naming-convention","Parser naming convention",[10,874,875,876,700,880,62],{},"Parsers share the layout of ",[24,877,879],{"href":878},"\u002Fdocs\u002F5.x\u002Fexplanation\u002Fextensions#plugins","plugins",[24,881,883],{"href":882},"\u002Fdocs\u002F5.x\u002Fexplanation\u002Farchitecture#adapters","adapters",[704,885,886,899],{},[707,887,888],{},[710,889,890,893,896],{},[713,891,892],{},"Surface",[713,894,895],{},"Pattern",[713,897,898],{},"Example",[729,900,901,919,934],{},[710,902,903,906,915],{},[734,904,905],{},"npm package",[734,907,908,911,912],{},[14,909,910],{},"@\u003Cscope>\u002Fparser-\u003Cname>"," or ",[14,913,914],{},"kubb-parser-\u003Cname>",[734,916,917],{},[14,918,29],{},[710,920,921,924,927],{},[734,922,923],{},"Parser runtime name",[734,925,926],{},"The output language or format (lowercase)",[734,928,929,184,931],{},[14,930,756],{},[14,932,933],{},"'markdown'",[710,935,936,939,945],{},[734,937,938],{},"Factory export",[734,940,941,944],{},[14,942,943],{},"parser\u003CName>"," (camelCase)",[734,946,947,184,949],{},[14,948,659],{},[14,950,951],{},"parserMd",[10,953,954,955,275],{},"Call the parser factory when registering it in ",[14,956,864],{},[10,958,959,960,355,962,184,965,968,969,355,971,184,974,977,978,982],{},"Parsers compose by extension. ",[14,961,659],{},[14,963,964],{},".ts",[14,966,967],{},".js",") and ",[14,970,665],{},[14,972,973],{},".tsx",[14,975,976],{},".jsx",") ship in the same ",[24,979,980],{"href":26},[14,981,29],{}," package and register side by side.",{"title":546,"searchDepth":984,"depth":984,"links":985},5,[986,992,993],{"id":42,"depth":987,"text":45,"children":988},2,[989],{"id":538,"depth":990,"text":991},3,"defineParser (deprecated)",{"id":679,"depth":987,"text":680},{"id":871,"depth":987,"text":872},"defineExtension with kind parser defines a parser from one object with meta and a build function. A parser converts a generated file AST into the source string written to disk. Covers the Parser interface and adding your own.","md",{"layout":997,"title":5,"description":994,"outline":998,"order":999,"navigation":1000},"doc",[987,990],6,{"title":5,"icon":1001},"i-iconoir-code",{"layout":997,"outline":1003},[987,990],{"title":5,"icon":1001},"\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fparsers",{"title":5,"description":994},{"loc":1005},"kubb-labs\u002Fkubb\u002Fdocs\u002F5.x\u002Freference\u002Fkit\u002Fparsers.md","docs\u002F5.x\u002Freference\u002Fkit\u002Fparsers","Hm2IUWNymkci_hEzkU0pofqjE13JGByMELuaUciLrNE",true,1792352128383]