All files parser.js

99.15% Statements 233/235
96.27% Branches 129/134
95.65% Functions 22/23
99.54% Lines 218/219

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 3913x   3x   32x   32x 32x 32x 32x   32x 32x 32x   32x         32x 183x   183x 16x 167x 24x 143x 77x   77x 22x   55x 55x 42x 13x 8x 5x 5x     66x 23x   43x     174x     23x 45x 23x 23x       23x           16x 16x 16x 15x 15x       24x 24x   24x 24x 24x   22x   22x 2x 2x 2x 2x     22x 22x 22x 21x 20x       69x 69x   69x 69x 69x 69x 69x 143x 143x 68x 68x 68x 68x 75x 2x 73x 1x   72x       68x 67x   67x   67x           7x 7x 7x 7x     67x 67x 2x 2x 2x 2x         2x 2x 2x 2x   2x     67x 69x 69x 4x 4x 2x 2x   2x 2x   4x 65x 63x       67x 67x 66x       51x 51x   51x 51x   51x 5x 5x 5x 5x     51x 51x 50x           160x 160x   160x   159x 17x 17x     159x 96x 96x 64x 1x 63x 61x   2x 2x 4x     32x 1x 1x   31x 42x         157x       160x   157x 157x   157x 157x 2x 2x   157x             72x 157x 2x           89x 172x 2x           73x 73x 2x 2x         89x 89x 161x 81x 81x   80x     89x         136x 136x 136x 136x 269x 269x 269x 10x   259x     136x 42x 6x 42x 4x   38x     6x 6x   136x     136x 129x 127x 127x     136x   136x             77x   77x 83x 83x 83x   77x       17x 17x 13x 13x   4x     17x 17x 15x 15x 15x   2x 2x 2x             9x               1x       1x       2x 2x       1x      
let { Declaration, Comment, AtRule, Rule, Root } = require('postcss')
 
module.exports = class Parser {
  constructor(input) {
    this.input = input
 
    this.pos = 0
    this.root = new Root()
    this.current = this.root
    this.spaces = ''
 
    this.extraIndent = false
    this.prevIndent = undefined
    this.step = undefined
 
    this.root.source = { input, start: { line: 1, column: 1 } }
  }
 
  loop() {
    let part
    while (this.pos < this.parts.length) {
      part = this.parts[this.pos]
 
      if (part.comment) {
        this.comment(part)
      } else if (part.atrule) {
        this.atrule(part)
      } else if (part.colon) {
        let next = this.nextNonComment(this.pos)
 
        if (next.end || next.atrule) {
          this.decl(part)
        } else {
          let moreIndent = next.indent.length > part.indent.length
          if (!moreIndent) {
            this.decl(part)
          } else if (moreIndent && next.colon) {
            this.rule(part)
          } else Eif (moreIndent && !next.colon) {
            this.decl(part)
          }
        }
      } else if (part.end) {
        this.root.raws.after = part.before
      } else {
        this.rule(part)
      }
 
      this.pos += 1
    }
 
    for (let i = this.tokens.length - 1; i >= 0; i--) {
      if (this.tokens[i].length > 3) {
        let last = this.tokens[i]
        this.root.source.end = {
          line: last[4] || last[2],
          column: last[5] || last[3]
        }
        break
      }
    }
  }
 
  comment(part) {
    let token = part.tokens[0]
    let node = new Comment()
    this.init(node, part)
    node.source.end = { line: token[4], column: token[5] }
    this.commentText(node, token)
  }
 
  atrule(part) {
    let atword = part.tokens[0]
    let params = part.tokens.slice(1)
 
    let node = new AtRule()
    node.name = atword[1].slice(1)
    this.init(node, part)
 
    Iif (node.name === '') this.unnamedAtrule(atword)
 
    while (!part.end && part.lastComma) {
      this.pos += 1
      part = this.parts[this.pos]
      params.push(['space', part.before + part.indent])
      params = params.concat(part.tokens)
    }
 
    node.raws.afterName = this.firstSpaces(params)
    this.keepTrailingSpace(node, params)
    this.checkSemicolon(params)
    this.checkCurly(params)
    this.raw(node, 'params', params, atword)
  }
 
  decl(part) {
    let node = new Declaration()
    this.init(node, part)
 
    let between = ''
    let colon = 0
    let value = []
    let prop = ''
    for (let i = 0; i < part.tokens.length; i++) {
      let token = part.tokens[i]
      if (token[0] === ':') {
        between += token[1]
        colon = token
        value = part.tokens.slice(i + 1)
        break
      } else if (token[0] === 'comment' || token[0] === 'space') {
        between += token[1]
      } else if (between !== '') {
        this.badProp(token)
      } else {
        prop += token[1]
      }
    }
 
    if (prop === '') this.unnamedDecl(part.tokens[0])
    node.prop = prop
 
    let next = this.parts[this.pos + 1]
 
    while (
      !next.end &&
      !next.atrule &&
      !next.colon &&
      next.indent.length > part.indent.length
    ) {
      value.push(['space', next.before + next.indent])
      value = value.concat(next.tokens)
      this.pos += 1
      next = this.parts[this.pos + 1]
    }
 
    let last = value[value.length - 1]
    if (last && last[0] === 'comment') {
      value.pop()
      let comment = new Comment()
      this.current.push(comment)
      comment.source = {
        input: this.input,
        start: { line: last[2], column: last[3] },
        end: { line: last[4], column: last[5] }
      }
      let prev = value[value.length - 1]
      Eif (prev && prev[0] === 'space') {
        value.pop()
        comment.raws.before = prev[1]
      }
      this.commentText(comment, last)
    }
 
    for (let i = value.length - 1; i > 0; i--) {
      let t = value[i][0]
      if (t === 'word' && value[i][1] === '!important') {
        node.important = true
        if (i > 0 && value[i - 1][0] === 'space') {
          node.raws.important = value[i - 1][1] + '!important'
          value.splice(i - 1, 2)
        } else {
          node.raws.important = '!important'
          value.splice(i, 1)
        }
        break
      } else if (t !== 'space' && t !== 'newline' && t !== 'comment') {
        break
      }
    }
 
    node.raws.between = between + this.firstSpaces(value)
    this.checkSemicolon(value)
    this.raw(node, 'value', value, colon)
  }
 
  rule(part) {
    let node = new Rule()
    this.init(node, part)
 
    let selector = part.tokens
    let next = this.parts[this.pos + 1]
 
    while (!next.end && next.indent.length === part.indent.length) {
      selector.push(['space', next.before + next.indent])
      selector = selector.concat(next.tokens)
      this.pos += 1
      next = this.parts[this.pos + 1]
    }
 
    this.keepTrailingSpace(node, selector)
    this.checkCurly(selector)
    this.raw(node, 'selector', selector)
  }
 
  /* Helpers */
 
  indent(part) {
    let indent = part.indent.length
    let isPrev = typeof this.prevIndent !== 'undefined'
 
    if (!isPrev && indent) this.indentedFirstLine(part)
 
    if (!this.step && indent) {
      this.step = indent
      this.root.raws.indent = part.indent
    }
 
    if (isPrev && this.prevIndent !== indent) {
      let diff = indent - this.prevIndent
      if (diff > 0) {
        if (diff !== this.step) {
          this.wrongIndent(this.prevIndent + this.step, indent, part)
        } else if (this.current.last.push) {
          this.current = this.current.last
        } else {
          this.extraIndent = ''
          for (let i = 0; i < diff; i++) {
            this.extraIndent += ' '
          }
        }
      } else if (diff % this.step !== 0) {
        let m = indent + (diff % this.step)
        this.wrongIndent(`${m} or ${m + this.step}`, indent, part)
      } else {
        for (let i = 0; i < -diff / this.step; i++) {
          this.current = this.current.parent
        }
      }
    }
 
    this.prevIndent = indent
  }
 
  init(node, part) {
    this.indent(part)
 
    if (!this.current.nodes) this.current.nodes = []
    this.current.push(node)
 
    node.raws.before = part.before + part.indent
    if (this.extraIndent) {
      node.raws.extraIndent = this.extraIndent
      this.extraIndent = false
    }
    node.source = {
      start: { line: part.tokens[0][2], column: part.tokens[0][3] },
      input: this.input
    }
  }
 
  checkCurly(tokens) {
    for (let token of tokens) {
      if (token[0] === '{') {
        this.error('Unnecessary curly bracket', token[2], token[3])
      }
    }
  }
 
  checkSemicolon(tokens) {
    for (let token of tokens) {
      if (token[0] === ';') {
        this.error('Unnecessary semicolon', token[2], token[3])
      }
    }
  }
 
  keepTrailingSpace(node, tokens) {
    let lastSpace = tokens[tokens.length - 1]
    if (lastSpace && lastSpace[0] === 'space') {
      tokens.pop()
      node.raws.sssBetween = lastSpace[1]
    }
  }
 
  firstSpaces(tokens) {
    let result = ''
    for (let i = 0; i < tokens.length; i++) {
      if (tokens[i][0] === 'space' || tokens[i][0] === 'newline') {
        result += tokens.shift()[1]
        i -= 1
      } else {
        break
      }
    }
    return result
  }
 
  raw(node, prop, tokens, altLast) {
    let token, type
    let length = tokens.length
    let value = ''
    let clean = true
    for (let i = 0; i < length; i += 1) {
      token = tokens[i]
      type = token[0]
      if (type === 'comment' || (type === 'space' && i === length - 1)) {
        clean = false
      } else {
        value += token[1]
      }
    }
    if (!clean) {
      let sss = tokens.reduce((all, i) => all + i[1], '')
      let raw = tokens.reduce((all, i) => {
        if (i[0] === 'comment' && i[6] === 'inline') {
          return all + '/* ' + i[1].slice(2).trim() + ' */'
        } else {
          return all + i[1]
        }
      }, '')
      node.raws[prop] = { value, raw }
      if (sss !== raw) node.raws[prop].sss = sss
    }
    node[prop] = value
 
    let last
    for (let i = tokens.length - 1; i >= 0; i--) {
      if (tokens[i].length > 2) {
        last = tokens[i]
        break
      }
    }
    if (!last) last = altLast
 
    node.source.end = {
      line: last[4] || last[2],
      column: last[5] || last[3]
    }
  }
 
  nextNonComment(pos) {
    let next = pos
    let part
    while (next < this.parts.length) {
      next += 1
      part = this.parts[next]
      if (part.end || !part.comment) break
    }
    return part
  }
 
  commentText(node, token) {
    let text = token[1]
    if (token[6] === 'inline') {
      node.raws.inline = true
      text = text.slice(2)
    } else {
      text = text.slice(2, -2)
    }
 
    let match = text.match(/^(\s*)([^]*\S)(\s*)\n?$/)
    if (match) {
      node.text = match[2]
      node.raws.left = match[1]
      node.raws.inlineRight = match[3]
    } else {
      node.text = ''
      node.raws.left = ''
      node.raws.inlineRight = ''
    }
  }
 
  // Errors
 
  error(msg, line, column) {
    throw this.input.error(msg, line, column)
  }
 
  unnamedAtrule(token) {
    this.error('At-rule without name', token[2], token[3])
  }
 
  unnamedDecl(token) {
    this.error('Declaration without name', token[2], token[3])
  }
 
  indentedFirstLine(part) {
    this.error('First line should not have indent', part.number, 1)
  }
 
  wrongIndent(expected, real, part) {
    let msg = `Expected ${expected} indent, but get ${real}`
    this.error(msg, part.number, 1)
  }
 
  badProp(token) {
    this.error('Unexpected separator in property', token[2], token[3])
  }
}