settings.js 2.7 KB

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