亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Babel自動(dòng)生成Attribute文檔實(shí)現(xiàn)詳解

 更新時(shí)間:2022年11月03日 16:36:16   作者:小鑫同學(xué)  
這篇文章主要為大家介紹了Babel自動(dòng)生成Attribute文檔實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

1. 前言

利用Babel自動(dòng)解析源碼屬性上的注釋生成對(duì)應(yīng)Markdown文檔,這個(gè)場(chǎng)景的應(yīng)用主要包括在組件庫(kù)文檔對(duì)組件屬性的介紹中,這一篇就通過(guò)編寫一個(gè)Babel插件來(lái)實(shí)現(xiàn)這個(gè)功能~

2. 開(kāi)發(fā)自動(dòng)生成屬性文檔插件

2.1 生成Babel插件模板:

  • 2.1.1 創(chuàng)建babel-plugin-auto-attr-doc文件夾;
  • 2.1.2 安裝npm i -g yo generator-babel-plugin-x;
  • 2.1.3 在新建目錄下執(zhí)行 yo babel-plugin-x:v7-ts;

生成的插件模板如下:

babel-plugin-auto-attr-doc  
├─ lib                      
│  └─ index.js              
├─ src                      
│  └─ index.ts              
├─ __tests__                
│  ├─ fixtures              
│  │  └─ example            
│  │     ├─ actual.ts       
│  │     └─ expected.ts     
│  └─ index.js              
├─ package-lock.json        
├─ package.json             
├─ README.md                
└─ tsconfig.json            

2.2 轉(zhuǎn)換思路詳解:

轉(zhuǎn)換過(guò)程:利用Babel將Typescript腳本解析為AST,通過(guò)對(duì)AST結(jié)構(gòu)分析抽離對(duì)應(yīng)的注釋部分,再拼接Markdown表格風(fēng)格的語(yǔ)法;

源碼要求:**我們應(yīng)該將組件涉及到對(duì)外提供的屬性統(tǒng)一到對(duì)應(yīng)的types.ts文件管理,分別導(dǎo)出對(duì)應(yīng)的type字段;

注釋要求:**分別定義字段描述、類型、可選項(xiàng)、默認(rèn)值4項(xiàng),由于解析器關(guān)鍵詞沖突原因,我們應(yīng)該盡量避免;

/**
  * @cDescribe 類型
  * @cType string
  * @cOptions 
  * @cDefault 
  */
 export type IType = "primary" | "success" | "warning" | "danger" | "info";
 /**
  * @cDescribe 圖標(biāo)組件
  * @cType string
  * @cOptions 
  * @cDefault 
  */
 export type IIcon = string;
 /**
  * @cDescribe 是否為樸素按鈕
  * @cType boolean
  * @cOptions 
  * @cDefault false
  */
 export type IPlain = boolean;

 Markdown表格:**展示組件的屬性、描述、類型、可選值和默認(rèn)值這幾項(xiàng);

2.3 單元測(cè)試用例:

  • 準(zhǔn)備插件待解析源碼文件source-code.ts
  • 準(zhǔn)備實(shí)際生成MD后應(yīng)該顯示的內(nèi)容文件actual.md;
| 屬性名 | 說(shuō)明 | 類型 | 可選值	| 默認(rèn)值 |
| ------ | ---- | ---- | ----- | ----- |
| type | 類型 | string |  |  |
| icon | 圖標(biāo)組件 | string |  |  |
| plain | 是否為樸素按鈕 | boolean |  | false |
  • 調(diào)整單元測(cè)試文件讀?。?/li>
it(`should ${caseName.split("-").join(" ")}`, () => {
  const actualPath = path.join(fixtureDir, "source-code.ts");
  // 對(duì)源碼進(jìn)行加載解析
  transformFileSync(actualPath);
  // 讀取我們準(zhǔn)備好的md文件
  const actual = fs
    .readFileSync(path.join(fixtureDir, "actual.md"))
    .toString();
  // 讀取插件解析生成的md文件
  const expected = fs
    .readFileSync(path.join(fixtureDir, "api-doc.md"))
    .toString();
  // diff
  const diff = diffChars(actual, expected);
  diff.length > 1 && _print(diff);
  expect(diff.length).toBe(1);
});

2.4 AST分析詳解:

  • 通過(guò)在AST explorer的源碼分析,我們?cè)贐abel中可以通過(guò)遍歷ExportNamedDeclaration(命名導(dǎo)出聲明);
  • leadingComments數(shù)組中可以取出所有注釋文本的集合,在Babel處理時(shí)我們需要依次處理每一塊注釋后增加標(biāo)記來(lái)避免重復(fù)處理;
  • (path.node.declaration as t.TypeAlias).id.name中取屬性名稱;

將注釋文本通過(guò)doctrine模塊解析為對(duì)象后和屬性名合并對(duì)轉(zhuǎn)換Markdown所需要的所有數(shù)據(jù)~

2.5 插件開(kāi)發(fā)過(guò)程:

2.5.1 定義Comment、ApiTable類型對(duì)象:

type Comment =
  | {
      describe: string;
      type: any;
      options?: any;
      default?: any;
    }
  | undefined;
type ApiTable = {
  attributeName: any;
  attributeDescribe: any;
  attributeType: any;
  attributeOptions: any;
  attributeDefault: any;
};

2.5.2 插件主邏輯分析:

  • pre:初始化存放apidoc容器,避免在存放時(shí)找不到容器;
  • visitor:解析源碼并獲取組織MD內(nèi)容數(shù)據(jù)暫存到apidoc中;
  • post:取出所有的apidoc內(nèi)容解析并輸出到本地文件中;
export default declare(
  (api: BabelAPI, options: Record<string, any>, dirname: string) => {
    api.assertVersion(7);
    return {
      name: "auto-attr-doc",
      pre(this: PluginPass, file: BabelFile) {
        this.set("api-doc", []);
      },
      visitor: {
        ExportNamedDeclaration(
          path: NodePath<t.ExportNamedDeclaration>,
          state: PluginPass
        ) {
          const apidoc = state.get("api-doc");
          // 處理 path.node.leadingComments 中未處理的數(shù)據(jù)后塞到apidoc中
          state.set("api-doc", apidoc);
        },
      },
      post(this: PluginPass, file: BabelFile) {
        const apidoc = this.get("api-doc");
        const output = generateMD(apidoc);
        const root = path.parse(file.opts.filename || "./").dir;
        fs.writeFileSync(path.join(root, "api-doc.md"), output, {
          encoding: "utf-8",
        });
      },
    } as PluginObj<PluginPass>;
  }
);

2.5.3 主邏輯實(shí)現(xiàn):

leadingComments數(shù)組會(huì)在依次訪問(wèn)ExportNamedDeclaration時(shí)不停增加,我們?cè)谔幚淼舢?dāng)前索引的對(duì)象后增加一個(gè)處理過(guò)的標(biāo)記skip,下次循環(huán)直接跳過(guò);

通過(guò)parseComment函數(shù)解析后的對(duì)象可以通過(guò)tags數(shù)組獲取到所有的注釋項(xiàng)目,通過(guò)對(duì)應(yīng)的title得到對(duì)應(yīng)description內(nèi)容;

在往apidoc存放數(shù)據(jù)時(shí)需要處理屬性名稱符合一定的規(guī)則,并將apidoc對(duì)象存放到原容器中;

{
  ExportNamedDeclaration(
    path: NodePath<t.ExportNamedDeclaration>,
    state: PluginPass
  ) {
    const apidoc = state.get("api-doc");
    let _comment: Comment = undefined;
    path.node.leadingComments?.forEach((comment) => {
      if (!Reflect.has(comment, "skip")) {
        const tags = parseComment(comment.value)?.tags;
        _comment = {
          describe:
            tags?.find((v) => v.title === "cDescribe")?.description || "",
          type: tags?.find((v) => v.title === "cType")?.description || "",
          options:
            tags?.find((v) => v.title === "cOptions")?.description || "",
          default:
            tags?.find((v) => v.title === "cDefault")?.description || "",
        };
        Reflect.set(comment, "skip", true);
      }
    });
    apidoc.push({
      attributeName: (path.node.declaration as t.TypeAlias).id.name.substr(1).toLocaleLowerCase(),
      attributeDescribe: _comment!.describe,
      attributeType: _comment!.type,
      attributeOptions: _comment!.options,
      attributeDefault: _comment!.default,
    } as ApiTable);
    state.set("api-doc", apidoc);
  },
}

2.5.4 注釋解析函數(shù):

const parseComment = (comment: string) => {
  if (!comment) {
    return;
  }
  return doctrine.parse(comment, {
    unwrap: true,
  });
};

2.5.5 Markdown表格拼裝:

const generateMD = (apidoc: Array<ApiTable>) => {
  let raw = `| 屬性名 | 說(shuō)明 | 類型 | 可選值	| 默認(rèn)值 |\n| ------ | ---- | ---- | ----- | ----- |\n`;
  apidoc.forEach((item) => {
    raw += `| ${item.attributeName} | ${item.attributeDescribe} | ${item.attributeType} | ${item.attributeOptions} | ${item.attributeDefault} |\n`;
  });
  return raw;
};

2.5.6生成結(jié)果展示~

3. 總結(jié)

插件生成目前基本功能完成,注釋解析可以通過(guò)Babel的插件選項(xiàng)來(lái)定義作為一個(gè)擴(kuò)展方向,MD文件的生成可以通過(guò)對(duì)應(yīng)工具轉(zhuǎn)換,更多的輸出文件類型也可以作為擴(kuò)展方向,歡迎喜歡玩轉(zhuǎn)Babel的小伙伴一起交流交流~

已推送至GitHub https://github.com/OSpoon/awesome-examples

以上就是Babel自動(dòng)生成Attribute文檔實(shí)現(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于Babel生成Attribute文檔的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Vue組件中的父子組件使用

    Vue組件中的父子組件使用

    這篇文章主要介紹了Vue組件中的父子組件使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • vue實(shí)現(xiàn)下拉菜單效果

    vue實(shí)現(xiàn)下拉菜單效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)下拉菜單效果,運(yùn)用了hover顯示與隱藏以及定位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • 使用 vue.js 構(gòu)建大型單頁(yè)應(yīng)用

    使用 vue.js 構(gòu)建大型單頁(yè)應(yīng)用

    本文給大家詳細(xì)介紹了如何使用使用 vue.js腳手架工具vue-cli構(gòu)建大型單頁(yè)應(yīng)用的方法,非常的實(shí)用,有需要的小伙伴可以參考下
    2018-02-02
  • 基于Vue.js實(shí)現(xiàn)tab滑塊效果

    基于Vue.js實(shí)現(xiàn)tab滑塊效果

    這篇文章主要為大家詳細(xì)介紹了基于Vue.js實(shí)現(xiàn)tab滑塊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Vue 綁定style和class樣式的寫法

    Vue 綁定style和class樣式的寫法

    class 與 style 綁定就是專門用來(lái)實(shí)現(xiàn)動(dòng)態(tài)樣式效果的技術(shù),如果需要?jiǎng)討B(tài)綁定 class 或 style 樣式,可以使用 v-bind 綁定,本文給大家講解Vue 綁定style和class樣式,感興趣的朋友一起看看吧
    2023-10-10
  • el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)

    el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)

    在開(kāi)發(fā)業(yè)務(wù)場(chǎng)景中我們通常遇到一些奇怪的需求,下面這篇文章主要給大家介紹了關(guān)于el-select如何獲取當(dāng)前選中的對(duì)象所有(item)數(shù)據(jù)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題

    vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題

    這篇文章主要介紹了vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • vue element input如何讓瀏覽器不保存密碼

    vue element input如何讓瀏覽器不保存密碼

    這篇文章主要介紹了vue element input如何讓瀏覽器不保存密碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • buildAdmin開(kāi)源項(xiàng)目引入四種圖標(biāo)方式詳解

    buildAdmin開(kāi)源項(xiàng)目引入四種圖標(biāo)方式詳解

    這篇文章主要為大家介紹了buildAdmin開(kāi)源項(xiàng)目引入四種圖標(biāo)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • 手寫可拖動(dòng)穿梭框組件CustormTransfer vue實(shí)現(xiàn)示例

    手寫可拖動(dòng)穿梭框組件CustormTransfer vue實(shí)現(xiàn)示例

    這篇文章主要為大家介紹了手寫可拖動(dòng)穿梭框組件CustormTransfer vue實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11

最新評(píng)論