Blame view

node_modules/eslint-plugin-html/src/settings.js 2.57 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  "use strict"
  
  const oneLine = require("./utils").oneLine
  
  const defaultHTMLExtensions = [
    ".erb",
    ".handlebars",
    ".hbs",
    ".htm",
    ".html",
    ".mustache",
    ".nunjucks",
    ".php",
    ".tag",
    ".twig",
    ".vue",
    ".we",
  ]
  
  const defaultXMLExtensions = [".xhtml", ".xml"]
  
  function filterOut(array, excludeArray) {
    if (!excludeArray) return array
    return array.filter(item => excludeArray.indexOf(item) < 0)
  }
  
  function compileRegExp(re) {
    const tokens = re.split("/")
    if (tokens.shift()) {
      // Ignore first token
      throw new Error(`Invalid regexp: ${re}`)
    }
    const flags = tokens.pop()
    return new RegExp(tokens.join("/"), flags)
  }
  
  function getSetting(settings, name) {
    if (typeof settings.html === "object" && name in settings.html) {
      return settings.html[name]
    }
    return settings[`html/${name}`]
  }
  
  function getSettings(settings) {
    const htmlExtensions =
      getSetting(settings, "html-extensions") ||
      filterOut(defaultHTMLExtensions, getSetting(settings, "xml-extensions"))
  
    const xmlExtensions =
      getSetting(settings, "xml-extensions") ||
      filterOut(defaultXMLExtensions, getSetting(settings, "html-extensions"))
  
    let reportBadIndent
    switch (getSetting(settings, "report-bad-indent")) {
      case undefined:
      case false:
      case 0:
      case "off":
        reportBadIndent = 0
        break
      case true:
      case 1:
      case "warn":
        reportBadIndent = 1
        break
      case 2:
      case "error":
        reportBadIndent = 2
        break
      default:
        throw new Error(
          oneLine`
          Invalid value for html/report-bad-indent,
          expected one of 0, 1, 2, "off", "warn" or "error"
        `
        )
    }
  
    const parsedIndent = /^(\+)?(tab|\d+)$/.exec(getSetting(settings, "indent"))
    const indent = parsedIndent && {
      relative: parsedIndent[1] === "+",
      spaces: parsedIndent[2] === "tab" ? "\t" : " ".repeat(parsedIndent[2]),
    }
  
    const rawJavaScriptMIMETypes = getSetting(settings, "javascript-mime-types")
    const javaScriptMIMETypes = rawJavaScriptMIMETypes
      ? (Array.isArray(rawJavaScriptMIMETypes)
          ? rawJavaScriptMIMETypes
          : [rawJavaScriptMIMETypes]
        ).map(s => (s.startsWith("/") ? compileRegExp(s) : s))
      : [
          /^(application|text)\/(x-)?(javascript|babel|ecmascript-6)$/i,
          /^module$/i,
        ]
  
    function isJavaScriptMIMEType(type) {
      return javaScriptMIMETypes.some(
        o => (typeof o === "string" ? type === o : o.test(type))
      )
    }
  
    return {
      htmlExtensions,
      xmlExtensions,
      indent,
      reportBadIndent,
      isJavaScriptMIMEType,
    }
  }
  
  module.exports = {
    getSettings,
  }