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