Skip to content

Commit

Permalink
Move to objects in jsonify()
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Aug 15, 2023
1 parent d3d762e commit 442f6dc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 57 deletions.
6 changes: 3 additions & 3 deletions each-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let { readFileSync, readdirSync } = require('fs')
let { join, extname, basename } = require('path')
let { readdirSync, readFileSync } = require('fs')
let { basename, extname, join } = require('path')

let extra = require('./extra-cases')

Expand All @@ -10,7 +10,7 @@ function read(file) {
module.exports = function eachTest(callback) {
readdirSync(join(__dirname, 'cases')).forEach(i => {
if (extname(i) !== '.json') return
let json = read(i).toString().trim()
let json = JSON.parse(read(i).toString().trim())
let name = basename(i, '.json')
let css = extra[name]
if (!css) {
Expand Down
10 changes: 5 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export function jsonify (node: object): string
export function jsonify(node: object): object

export function testPath (name: string): string
export function testPath(name: string): string

export function eachTest (
callback: (name: string, css: string, json: string) => void
export function eachTest(
callback: (name: string, css: string, json: object) => void
): void

export function testOnReal (
export function testOnReal(
callback: (css: string) => { css: string },
extra: string[]
): void
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ let eachTest = require('./each-test')
let testPath = require('./test-path')
let jsonify = require('./jsonify')

module.exports = { jsonify, testPath, eachTest, testOnReal }
module.exports = { eachTest, jsonify, testOnReal, testPath }
2 changes: 1 addition & 1 deletion jsonify.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ function clean(node) {

module.exports = function jsonify(node) {
let cleaned = clean(node.toJSON())
return JSON.stringify(cleaned, null, 2)
return JSON.parse(JSON.stringify(cleaned, null, 2))
}
85 changes: 38 additions & 47 deletions test/jsonify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ let jsonify = require('../jsonify')

test('converts to JSON string', () => {
let node = postcss.rule()
equal(
jsonify(node),
'{\n' + ' "raws": {},\n' + ' "type": "rule",\n' + ' "nodes": []\n' + '}'
)
equal(jsonify(node), { nodes: [], raws: {}, type: 'rule' })
})

test('converts source.input', () => {
Expand All @@ -19,62 +16,56 @@ test('converts source.input', () => {
}
})
node.each(() => {})
equal(
jsonify(node),
'{\n' +
' "raws": {},\n' +
' "source": {},\n' +
' "type": "rule",\n' +
' "nodes": []\n' +
'}'
)
equal(jsonify(node), {
nodes: [],
raws: {},
source: {},
type: 'rule'
})
})

test('converts source.input recursively', () => {
let rule = postcss.rule({
source: {
start: {
offset: 0,
line: 1,
column: 1
},
end: {
offset: 14,
column: 15,
line: 1,
column: 15
offset: 14
},
input: new postcss.Input('test', { from: '/a.css' })
input: new postcss.Input('test', { from: '/a.css' }),
start: {
column: 1,
line: 1,
offset: 0
}
}
})
let root = postcss.root()
root.append(rule)

equal(
jsonify(root),
'{\n' +
' "raws": {},\n' +
' "type": "root",\n' +
' "nodes": [\n' +
' {\n' +
' "raws": {},\n' +
' "source": {\n' +
' "start": {\n' +
' "offset": 0,\n' +
' "line": 1,\n' +
' "column": 1\n' +
' },\n' +
' "end": {\n' +
' "offset": 14,\n' +
' "line": 1,\n' +
' "column": 15\n' +
' }\n' +
' },\n' +
' "type": "rule",\n' +
' "nodes": []\n' +
' }\n' +
' ]\n' +
'}'
)
equal(jsonify(root), {
nodes: [
{
nodes: [],
raws: {},
source: {
end: {
column: 15,
line: 1,
offset: 14
},
start: {
column: 1,
line: 1,
offset: 0
}
},
type: 'rule'
}
],
raws: {},
type: 'root'
})
})

test.run()

0 comments on commit 442f6dc

Please sign in to comment.