Sha256: 5087cfda6c555ac560cfec7f6c5904c07835da403e398e6b03f078f8107c129f

Contents?: true

Size: 1.47 KB

Versions: 26

Compression:

Stored size: 1.47 KB

Contents

'use strict'

let AtRule = require('./at-rule')
let Comment = require('./comment')
let Declaration = require('./declaration')
let Input = require('./input')
let PreviousMap = require('./previous-map')
let Root = require('./root')
let Rule = require('./rule')

function fromJSON(json, inputs) {
  if (Array.isArray(json)) return json.map(n => fromJSON(n))

  let { inputs: ownInputs, ...defaults } = json
  if (ownInputs) {
    inputs = []
    for (let input of ownInputs) {
      let inputHydrated = { ...input, __proto__: Input.prototype }
      if (inputHydrated.map) {
        inputHydrated.map = {
          ...inputHydrated.map,
          __proto__: PreviousMap.prototype
        }
      }
      inputs.push(inputHydrated)
    }
  }
  if (defaults.nodes) {
    defaults.nodes = json.nodes.map(n => fromJSON(n, inputs))
  }
  if (defaults.source) {
    let { inputId, ...source } = defaults.source
    defaults.source = source
    if (inputId != null) {
      defaults.source.input = inputs[inputId]
    }
  }
  if (defaults.type === 'root') {
    return new Root(defaults)
  } else if (defaults.type === 'decl') {
    return new Declaration(defaults)
  } else if (defaults.type === 'rule') {
    return new Rule(defaults)
  } else if (defaults.type === 'comment') {
    return new Comment(defaults)
  } else if (defaults.type === 'atrule') {
    return new AtRule(defaults)
  } else {
    throw new Error('Unknown node type: ' + json.type)
  }
}

module.exports = fromJSON
fromJSON.default = fromJSON

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.25 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.24 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.23 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.22 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.21 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.20 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.19 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.18 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.17 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.16 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.15 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.14 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.13 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.12 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.11 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.10 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.9 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.8 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js
clapton-0.0.7 lib/clapton/javascripts/node_modules/postcss/lib/fromJSON.js