Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1484 from mischnic/ast-json
fromJSON and toJSON
- Loading branch information
Showing
8 changed files
with
163 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { JSONHydrator } from './postcss.js' | ||
|
||
declare const fromJSON: JSONHydrator | ||
|
||
export default fromJSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict' | ||
|
||
let Declaration = require('./declaration') | ||
let Comment = require('./comment') | ||
let AtRule = require('./at-rule') | ||
let Root = require('./root') | ||
let Rule = require('./rule') | ||
let Input = require('./input') | ||
let PreviousMap = require('./previous-map') | ||
|
||
function fromJSON (json) { | ||
let defaults = { ...json } | ||
if (json.nodes) { | ||
defaults.nodes = json.nodes.map(n => fromJSON(n)) | ||
} | ||
if (json.type === 'root') { | ||
if (defaults.source) { | ||
defaults.source = { ...defaults.source } | ||
if (defaults.source.input) { | ||
defaults.source.input = { | ||
...defaults.source.input, | ||
__proto__: Input.prototype | ||
} | ||
if (defaults.source.input.map) { | ||
defaults.source.input.map = { | ||
...defaults.source.input.map, | ||
__proto__: PreviousMap.prototype | ||
} | ||
} | ||
} | ||
} | ||
return new Root(defaults) | ||
} else if (json.type === 'decl') { | ||
return new Declaration(defaults) | ||
} else if (json.type === 'rule') { | ||
return new Rule(defaults) | ||
} else if (json.type === 'comment') { | ||
return new Comment(defaults) | ||
} else if (json.type === 'atrule') { | ||
return new AtRule(defaults) | ||
} else { | ||
throw new Error('Unknown node type: ' + json.type) | ||
} | ||
} | ||
|
||
module.exports = fromJSON | ||
fromJSON.default = fromJSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import v8 from 'v8' | ||
|
||
import postcss, { Root } from '../lib/postcss.js' | ||
|
||
it('rehydrates a JSON AST', () => { | ||
let cssWithMap = postcss().process( | ||
'.foo { color: red; font-size: 12pt; } /* abc */ @media (width: 60em) { }', | ||
{ | ||
from: undefined, | ||
map: { | ||
inline: true | ||
}, | ||
stringifier: postcss.stringify | ||
} | ||
).css | ||
|
||
let root = postcss.parse(cssWithMap) | ||
|
||
let json = root.toJSON() | ||
let serialized = v8.serialize(json) | ||
let deserialized = v8.deserialize(serialized) | ||
let rehydrated = postcss.fromJSON(deserialized) as Root | ||
|
||
rehydrated.nodes[0].remove() | ||
|
||
expect(rehydrated.nodes).toHaveLength(3) | ||
}) | ||
|
||
it('throws when rehydrating an invalid JSON AST', () => { | ||
expect(() => { | ||
postcss.fromJSON({ type: 'not-a-node-type' }) | ||
}).toThrow('Unknown node type: not-a-node-type') | ||
}) |