Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fromJSON and toJSON #1484

Merged
merged 3 commits into from Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions lib/fromJSON.js
@@ -0,0 +1,46 @@
'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) {
mischnic marked this conversation as resolved.
Show resolved Hide resolved
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)
mischnic marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = fromJSON
74 changes: 44 additions & 30 deletions lib/input.js
Expand Up @@ -49,42 +49,41 @@ class Input {
}

fromOffset (offset) {
let lines = this.css.split('\n')
let lineToIndex = new Array(lines.length)
let prevIndex = 0
if (!this.lineToIndex) {
let lines = this.css.split('\n')
this.lineToIndex = new Array(lines.length)
let prevIndex = 0

for (let i = 0, l = lines.length; i < l; i++) {
this.lineToIndex[i] = prevIndex
prevIndex += lines[i].length + 1
}

for (let i = 0, l = lines.length; i < l; i++) {
lineToIndex[i] = prevIndex
prevIndex += lines[i].length + 1
this.lastLine = this.lineToIndex[this.lineToIndex.length - 1]
}
mischnic marked this conversation as resolved.
Show resolved Hide resolved

let lastLine = lineToIndex[lineToIndex.length - 1]

this.fromOffset = index => {
let min = 0
if (index >= lastLine) {
min = lineToIndex.length - 1
} else {
let max = lineToIndex.length - 2
let mid
while (min < max) {
mid = min + ((max - min) >> 1)
if (index < lineToIndex[mid]) {
max = mid - 1
} else if (index >= lineToIndex[mid + 1]) {
min = mid + 1
} else {
min = mid
break
}
let min = 0
if (offset >= this.lastLine) {
min = this.lineToIndex.length - 1
} else {
let max = this.lineToIndex.length - 2
let mid
while (min < max) {
mid = min + ((max - min) >> 1)
if (offset < this.lineToIndex[mid]) {
max = mid - 1
} else if (offset >= this.lineToIndex[mid + 1]) {
min = mid + 1
} else {
min = mid
break
}
}
return {
line: min + 1,
col: index - lineToIndex[min] + 1
}
}
return this.fromOffset(offset)
return {
line: min + 1,
col: offset - this.lineToIndex[min] + 1
}
}

error (message, line, column, opts = {}) {
Expand Down Expand Up @@ -168,6 +167,21 @@ class Input {
get from () {
return this.file || this.id
}

toJSON () {
let json = {}
if (this.hasBOM != null) json.hasBOM = this.hasBOM
mischnic marked this conversation as resolved.
Show resolved Hide resolved
if (this.css != null) json.css = this.css
if (this.file != null) json.file = this.file
if (this.id != null) json.id = this.id
if (this.map) {
json.map = { ...this.map }
if (json.map.consumerCache) {
json.map.consumerCache = undefined
}
}
return json
}
}

module.exports = Input
Expand Down
5 changes: 5 additions & 0 deletions lib/node.js
Expand Up @@ -187,6 +187,11 @@ class Node {
})
} else if (typeof value === 'object' && value.toJSON) {
fixed[name] = value.toJSON()
} else if (this.type === 'root' && name === 'source') {
fixed[name] = {
input: value.input.toJSON(),
start: value.start
}
} else {
fixed[name] = value
}
Expand Down
7 changes: 7 additions & 0 deletions lib/postcss.d.ts
Expand Up @@ -397,6 +397,12 @@ export interface Postcss {
*/
root(defaults?: RootProps): Root

/**
* Rehydrate a JSON AST (from Node#toJSON) back into the corresponding node.
* @param data A JSON AST.
*/
fromJSON(data: object): Node

CssSyntaxError: typeof CssSyntaxError
Declaration: typeof Declaration
Container: typeof Container
Expand All @@ -412,6 +418,7 @@ export interface Postcss {

export const stringify: Stringifier
export const parse: Parser
export const fromJSON: Postcss['fromJSON']

export const comment: Postcss['comment']
export const atRule: Postcss['atRule']
Expand Down
2 changes: 2 additions & 0 deletions lib/postcss.js
Expand Up @@ -12,6 +12,7 @@ let AtRule = require('./at-rule')
let Result = require('./result.js')
let Input = require('./input')
let parse = require('./parse')
let fromJSON = require('./fromJSON')
mischnic marked this conversation as resolved.
Show resolved Hide resolved
let list = require('./list')
let Rule = require('./rule')
let Root = require('./root')
Expand Down Expand Up @@ -62,6 +63,7 @@ postcss.plugin = function plugin (name, initializer) {

postcss.stringify = stringify
postcss.parse = parse
postcss.fromJSON = fromJSON
postcss.list = list

postcss.comment = defaults => new Comment(defaults)
Expand Down
1 change: 1 addition & 0 deletions lib/postcss.mjs
Expand Up @@ -5,6 +5,7 @@ export default postcss
export const stringify = postcss.stringify
export const plugin = postcss.plugin
export const parse = postcss.parse
export const fromJSON = postcss.fromJSON
export const list = postcss.list

export const comment = postcss.comment
Expand Down
28 changes: 28 additions & 0 deletions test/fromJSON.test.ts
@@ -0,0 +1,28 @@
import v8 from 'v8'

import postcss, { Root } from '../lib/postcss.js'

it('can rehydrate a JSON AST', () => {
mischnic marked this conversation as resolved.
Show resolved Hide resolved
let cssWithMap = postcss().process(
'.foo { color: red; font-size: 12pt; } /* abc */ @media (width: 60em) { }',
{
from: undefined,
map: {
inline: true
},
stringifier: postcss.stringify
ai marked this conversation as resolved.
Show resolved Hide resolved
}
).css

let root = postcss.parse(cssWithMap)

let json = root.toJSON()
let serialized = v8.serialize(json)
let deserialized = v8.deserialize(serialized)
// @ts-ignore
mischnic marked this conversation as resolved.
Show resolved Hide resolved
let rehydrated: Root = postcss.fromJSON(deserialized)

rehydrated.nodes[0].remove()

expect(rehydrated.nodes).toHaveLength(3)
})