parse-statements.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict"
  2. // external tooling
  3. const valueParser = require("postcss-value-parser")
  4. // extended tooling
  5. const stringify = valueParser.stringify
  6. function split(params, start) {
  7. const list = []
  8. const last = params.reduce((item, node, index) => {
  9. if (index < start) return ""
  10. if (node.type === "div" && node.value === ",") {
  11. list.push(item)
  12. return ""
  13. }
  14. return item + stringify(node)
  15. }, "")
  16. list.push(last)
  17. return list
  18. }
  19. module.exports = function(result, styles) {
  20. const statements = []
  21. let nodes = []
  22. styles.each(node => {
  23. let stmt
  24. if (node.type === "atrule") {
  25. if (node.name === "import") stmt = parseImport(result, node)
  26. else if (node.name === "media") stmt = parseMedia(result, node)
  27. }
  28. if (stmt) {
  29. if (nodes.length) {
  30. statements.push({
  31. type: "nodes",
  32. nodes: nodes,
  33. media: [],
  34. })
  35. nodes = []
  36. }
  37. statements.push(stmt)
  38. } else nodes.push(node)
  39. })
  40. if (nodes.length) {
  41. statements.push({
  42. type: "nodes",
  43. nodes: nodes,
  44. media: [],
  45. })
  46. }
  47. return statements
  48. }
  49. function parseMedia(result, atRule) {
  50. const params = valueParser(atRule.params).nodes
  51. return {
  52. type: "media",
  53. node: atRule,
  54. media: split(params, 0),
  55. }
  56. }
  57. function parseImport(result, atRule) {
  58. let prev = getPrev(atRule)
  59. if (prev) {
  60. do {
  61. if (
  62. prev.type !== "atrule" ||
  63. (prev.name !== "import" && prev.name !== "charset")
  64. ) {
  65. return result.warn(
  66. "@import must precede all other statements (besides @charset)",
  67. { node: atRule }
  68. )
  69. } else prev = getPrev(prev)
  70. } while (prev)
  71. }
  72. if (atRule.nodes) {
  73. return result.warn(
  74. "It looks like you didn't end your @import statement correctly. " +
  75. "Child nodes are attached to it.",
  76. { node: atRule }
  77. )
  78. }
  79. const params = valueParser(atRule.params).nodes
  80. const stmt = {
  81. type: "import",
  82. node: atRule,
  83. media: [],
  84. }
  85. // prettier-ignore
  86. if (
  87. !params.length ||
  88. (
  89. params[0].type !== "string" ||
  90. !params[0].value
  91. ) &&
  92. (
  93. params[0].type !== "function" ||
  94. params[0].value !== "url" ||
  95. !params[0].nodes.length ||
  96. !params[0].nodes[0].value
  97. )
  98. ) {
  99. return result.warn(`Unable to find uri in '${ atRule.toString() }'`, {
  100. node: atRule,
  101. })
  102. }
  103. if (params[0].type === "string") stmt.uri = params[0].value
  104. else stmt.uri = params[0].nodes[0].value
  105. stmt.fullUri = stringify(params[0])
  106. if (params.length > 2) {
  107. if (params[1].type !== "space") {
  108. return result.warn("Invalid import media statement", { node: atRule })
  109. }
  110. stmt.media = split(params, 2)
  111. }
  112. return stmt
  113. }
  114. function getPrev(item) {
  115. let prev = item.prev()
  116. while (prev && prev.type === "comment") {
  117. prev = prev.prev()
  118. }
  119. return prev
  120. }