自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊Eslint 的 Disble、Enable 的注釋配置是怎么實(shí)現(xiàn)的

開發(fā) 前端
注釋中的配置在 eslint、webpack、terser 等工具中都有應(yīng)用,分別叫 inline config、magic comment、annotation,但都指的同一個東西。

[[424924]]

不知道大家有沒有用過 eslint 的注釋的配置方式:

  1. /* eslint-disable no-alert, no-console */ 
  2. alert('foo'); 
  3. console.log('bar'); 
  4. /* eslint-enable no-alert, no-console */ 
  5.  
  6. // eslint-disable-next-line 
  7. alert('foo'); 

eslint 支持 eslint-disable、eslint-enable、eslint-disable-next-line 等指定某個 rule 是否生效的行內(nèi)配置,叫做 inline config。

webpack 中也有這種配置方式,可以在動態(tài)引入一個模塊的時候配置代碼分割的方式,叫做 magic comment。

  1. import( 
  2.   /* webpackChunkName: "my-chunk-name" */ 
  3.   /* webpackMode: "lazy" */ 
  4.   /* webpackExports: ["default""named"] */ 
  5.   'module' 
  6. ); 

類似的,terser 也有這種機(jī)制,叫做 annotation,可以指定某個 api 是否是純的,純函數(shù)的話如果沒用到可以直接刪除。

  1. var a = /*#__PURE__*/React.createElement("div"null); 

可以看到,很多庫都用到了這種通過注釋來配置的方式,不管是叫 annotation 也好、magic comment 也好,或者 inline config 也好,都指的同一個東西。

既然是這么常見的配置方式,那么他們是怎么實(shí)現(xiàn)的呢?

注釋中配置的實(shí)現(xiàn)原理

我們拿 eslint 的 inline config 的實(shí)現(xiàn)來看一下。

eslint 會把源碼 parse 成 AST,然后對把 AST 傳入一系列 rule 來做檢查,檢查結(jié)果會用 formatter 格式化后輸出。

注釋的配置是在哪一步生效的呢?

我簡化了一下源碼,是這樣的:

  1. verify(text) { 
  2.     // parse 源碼 
  3.     const ast = parse(text); 
  4.     // 調(diào)用 rule,拿到 lint 的問題 
  5.     const lintingProblems = runRules(ast); 
  6.     // 通過 AST 拿到注釋中的配置 
  7.     const commentDirectives = getDirectiveComments(ast); 
  8.     // 根據(jù)注釋中的配置過濾問題 
  9.     return applyDisableDirectives(lintingProblems, commentDirectives); 

可以看到,整體流程是:

  • 把源碼 parse 成 AST
  • 調(diào)用 rule 對 AST 做檢查,拿到 lint 的 problems
  • 通過 AST 拿到注釋中的 diectives
  • 通過 directives 過濾 problems,就是最終需要報出的問題

也就是說 eslint 的 inline config 是在 lint 完 AST,拿到各種 problems 之后生效的,對 problems 做一次過濾。

那怎么從 AST 中取出 directives 的呢?又是怎么過濾 problems 的呢?

我們分別看一下。

從 AST 取出 directives 的源碼簡化以后是這樣的:

  1. function getDirectiveComments(ast){ 
  2.     const directives = []; 
  3.     ast.comments.forEach(comment => { 
  4.         const match = /^[#@](eslint(?:-env|-enable|-disable(?:(?:-next)?-line)?)?|exported|globals?)(?:\s|$)/u.exec(comment.trim()); 
  5.         if (match) { 
  6.             const directiveText = match[1]; 
  7.             ... 
  8.             directives.push({ type: xxx, line: loc.start.line, column: loc.start.column + 1, ruleId }); 
  9.         } 
  10.     } 
  11.     return directives; 

其實(shí)就是對 AST 中所有的 comments 的內(nèi)容做一下正則的匹配,如果是支持的 directive,就把它收集起來,并且記錄下對應(yīng)的行列號。

之后就是對 problems 的過濾了。

簡化后的源碼是這樣的:

  1. function applyDisableDirectives(problems, disableDirectives) { 
  2.     const filteredProblems = []; 
  3.  
  4.     const disabledRuleMap = new Map(); 
  5.  
  6.     let nextIndex = 0; 
  7.     for (const problem of problems) { 
  8.         // 對每一個 probelm,都要找到當(dāng)前被禁用的 rule 
  9.         while ( 
  10.             nextIndex < disableDirectives.length && 
  11.             compareLocations(disableDirectives[nextIndex], problem) <= 0 
  12.         ) { 
  13.             const directive = disableDirectives[nextIndex++]; 
  14.  
  15.             switch (directive.type) { 
  16.                 case "disable"
  17.                     disabledRuleMap.set(directive.ruleId, directive); 
  18.                     break; 
  19.                 case "enable"
  20.                     disabledRuleMap.delete(directive.ruleId); 
  21.                     break; 
  22.             }        
  23.         } 
  24.         //如果 problem 對應(yīng)的 rule 沒有被禁用,則返回 
  25.         if (!disabledRuleMap.has(problem.ruleId)) { 
  26.             filteredProblems.push(problem); 
  27.         } 
  28.     } 
  29.     return filteredProblems; 
  30.  
  31. function compareLocations(itemA, itemB) { 
  32.     return itemA.line - itemB.line || itemA.column - itemB.column

我們理下思路:

我們要過濾掉 problems 中被 disabled 的 rule 報出的 problem,返回過濾后的 problems。

可以維護(hù)一個 disabledRuleMap,表示禁用的 rule。

對每一個 problem,都根據(jù)行列號來從 disableDirectives 中取出 directive 的信息,把對應(yīng)的 rule 放入 disabledRuleMap。

然后看下該 problem 的 rule 是否是被禁用了,也就是是否在 disabledRuleMap 中,如果是,就過濾掉。

這樣處理完一遍,返回的 problem 就是可以報出的了。

這就是 eslint 的 eslint-disable、eslint-enable、eslint-disable-next-line 等注釋可以配置 rule 是否生效的原理。

eslint 是根據(jù)行列號找到對應(yīng)的 comment 的,其實(shí)很多 AST 中會記錄每個節(jié)點(diǎn)關(guān)聯(lián)的 comment。

比如 babel 的 AST:

這樣可以根據(jù) AST 來取出注釋,之后通過正則來判斷是否是 directive。

通過行列號來查找 comment,通過 AST 找到關(guān)聯(lián)的 comment,這是兩種查找注釋的方式。

總結(jié)

注釋中的配置在 eslint、webpack、terser 等工具中都有應(yīng)用,分別叫 inline config、magic comment、annotation,但都指的同一個東西。

它們都是找到 AST 中的 comments,通過正則匹配下是否是支持的 directive(指令),然后取出對應(yīng)的信息。

找到 directive 之后,還要找到 directive 生效的地方,可以用兩種方式來查找:一種是根據(jù)行列號的比較,一種是根據(jù)關(guān)聯(lián)的 AST 來查找。

找到 directive 和對應(yīng)生效的地方之后,就可以根據(jù) directive 中的信息做各種處理了。

注釋中的配置是一種比較常見的配置方式,適合一些局部的配置。理解了它們的實(shí)現(xiàn)原理,能夠讓我們更好的掌握這種機(jī)制。

 

責(zé)任編輯:姜華 來源: 神光的編程秘籍
相關(guān)推薦

2024-05-31 09:31:00

2009-06-11 17:37:32

EJB注釋

2021-07-02 07:06:20

調(diào)試代碼crash

2022-03-29 10:32:32

SpringBoot@Enable

2021-10-31 23:57:33

Eslint原理

2021-01-20 05:39:50

NacosConfig服務(wù)端

2022-09-30 00:03:03

JS斷點(diǎn)線程

2022-10-08 00:07:00

JSV8調(diào)用棧

2024-12-23 15:05:29

2021-08-30 22:38:47

VscodeMarkdown預(yù)覽

2022-02-18 08:26:12

TopK數(shù)組面試題

2021-07-14 14:05:24

Fragment項目結(jié)構(gòu)

2022-05-30 16:19:26

C#多態(tài)底層虛方法

2021-08-23 08:27:43

innodb數(shù)據(jù)庫存儲引擎

2022-02-11 09:31:23

IPV4IP地址IANA

2009-02-17 18:52:06

網(wǎng)絡(luò)虛擬化路由系統(tǒng)數(shù)據(jù)中心

2024-09-18 13:49:42

2023-11-23 19:30:35

Python編程語言

2022-05-13 09:05:37

JSObject無序

2021-05-19 07:35:00

MySQL數(shù)據(jù)庫COUNT
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號