AK-21/Graphite-Industrial-Intelligence
0
1'use strict'2 3let AtRule = require('./at-rule')4let Comment = require('./comment')5let Declaration = require('./declaration')6let Input = require('./input')7let PreviousMap = require('./previous-map')8let Root = require('./root')9let Rule = require('./rule')10 11function fromJSON(json, inputs) {12 if (Array.isArray(json)) return json.map(n => fromJSON(n))13 14 let { inputs: ownInputs, ...defaults } = json15 if (ownInputs) {16 inputs = []17 for (let input of ownInputs) {18 let inputHydrated = { ...input, __proto__: Input.prototype }19 if (inputHydrated.map) {20 inputHydrated.map = {21 ...inputHydrated.map,22 __proto__: PreviousMap.prototype23 }24 }25 inputs.push(inputHydrated)26 }27 }28 // Rehydrate children separately and attach them after construction.29 // Passing them through the container constructor would re-run insertion30 // spacing normalization and overwrite each child's own `raws.before`.31 let nodes32 if (defaults.nodes) {33 nodes = json.nodes.map(n => fromJSON(n, inputs))34 delete defaults.nodes35 }36 if (defaults.source) {37 let { inputId, ...source } = defaults.source38 defaults.source = source39 if (inputId != null) {40 defaults.source.input = inputs[inputId]41 }42 }43 44 let node45 if (defaults.type === 'root') {46 node = new Root(defaults)47 } else if (defaults.type === 'decl') {48 node = new Declaration(defaults)49 } else if (defaults.type === 'rule') {50 node = new Rule(defaults)51 } else if (defaults.type === 'comment') {52 node = new Comment(defaults)53 } else if (defaults.type === 'atrule') {54 node = new AtRule(defaults)55 } else {56 throw new Error('Unknown node type: ' + json.type)57 }58 59 if (nodes) {60 node.nodes = nodes61 for (let child of nodes) child.parent = node62 }63 64 return node65}66 67module.exports = fromJSON68fromJSON.default = fromJSON69 