MCPcopy
hub / github.com/postcss/postcss / fromJSON

Function fromJSON

lib/fromJSON.js:11–65  ·  view source on GitHub ↗
(json, inputs)

Source from the content-addressed store, hash-verified

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 } = json
15 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.prototype
23 }
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 insertion
30 // spacing normalization and overwrite each child's own `raws.before`.
31 let nodes
32 if (defaults.nodes) {
33 nodes = json.nodes.map(n => fromJSON(n, inputs))
34 delete defaults.nodes
35 }
36 if (defaults.source) {
37 let { inputId, ...source } = defaults.source
38 defaults.source = source
39 if (inputId != null) {
40 defaults.source.input = inputs[inputId]
41 }
42 }
43
44 let node
45 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 = nodes
61 for (let child of nodes) child.parent = node
62 }
63
64 return node
65}
66
67module.exports = fromJSON
68fromJSON.default = fromJSON

Callers

nothing calls this directly

Calls 2

pushMethod · 0.80
mapMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…