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