UNPKG

1.21 kBJavaScriptView Raw
1'use strict'
2
3const debug = require('debug')('elint:walker:filetree')
4const path = require('path')
5const { linterSuffix } = require('../config')
6
7/**
8 * 获取 fileTree
9 *
10 * @returns {object} empty file tree
11 */
12function getFileTree () {
13 const fileTree = {}
14
15 debug('config linterSuffix: %o', linterSuffix)
16
17 Object.keys(linterSuffix).forEach(linterName => {
18 fileTree[linterName] = []
19 })
20
21 return fileTree
22}
23
24/**
25 * 填充 fileTree
26 *
27 * @param {object} fileTree file tree
28 * @param {Array<string>} fileList file list
29 * @returns {object} filled file tree
30 */
31function fillFileTree (fileTree, fileList) {
32 let extname
33
34 fileList.forEach(filePath => {
35 extname = path.extname(filePath)
36 const linters = Object.keys(linterSuffix)
37
38 const match = linters.some(linterName => {
39 if (linterSuffix[linterName].includes(extname)) {
40 fileTree[linterName].push(filePath)
41 return true
42 }
43
44 return false
45 })
46
47 // 没有匹配到特定的 linter,则分配给所有 linter
48 if (!match) {
49 linters.forEach(linterName => {
50 fileTree[linterName].push(filePath)
51 })
52 }
53 })
54
55 return fileTree
56}
57
58module.exports = {
59 getFileTree,
60 fillFileTree
61}