This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
forked from gucong3000/postcss-jsx
-
Notifications
You must be signed in to change notification settings - Fork 18
/
object-stringifier.js
150 lines (117 loc) · 3.34 KB
/
object-stringifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
const camelCase = require('./camel-case');
const Stringifier = require('postcss/lib/stringifier');
class ObjectStringifier extends Stringifier {
object(node) {
this.builder('{', node, 'start');
let after;
if (node.nodes && node.nodes.length) {
this.body(node);
after = this.raw(node, 'after');
} else {
after = this.raw(node, 'after', 'emptyBody');
}
if (after) this.builder(after);
this.builder('}', node, 'end');
}
literal(node, semicolon) {
this.builder(node.text + (semicolon ? ',' : ''), node);
}
decl(node, semicolon) {
let prop = this.rawValue(node, 'prop');
let string = prop;
const isObjectShorthand = node.raws.node && node.raws.node.shorthand;
if (!isObjectShorthand) {
const between = this.raw(node, 'between', 'colon');
const value = this.rawValue(node, 'value');
string += between + value;
}
if (semicolon) string += ',';
this.builder(string, node);
}
rule(node, semicolon) {
this.block(node, this.rawValue(node, 'selector'), semicolon);
}
atrule(node, semicolon) {
const name = this.rawValue(node, 'name');
const params = this.rawValue(node, 'params');
if (node.nodes) {
let string;
if (params) {
const afterName = this.raw(node, 'afterName');
string = name + afterName + params;
} else {
string = name;
}
this.block(node, string, semicolon);
} else {
const between = this.raw(node, 'between', 'colon');
let string = name + between + params;
if (semicolon) string += ',';
this.builder(string, node);
}
}
block(node, start, semicolon) {
super.block(node, start);
if (semicolon) {
this.builder(',', node);
}
}
comment(node) {
const left = this.raw(node, 'left', 'commentLeft');
const right = this.raw(node, 'right', 'commentRight');
if (node.raws.inline) {
const text = node.raws.text || node.text;
this.builder(`//${left}${text}${right}`, node);
} else {
this.builder(`/*${left}${node.text}${right}*/`, node);
}
}
raw(node, own, detect) {
let value = super.raw(node, own, detect);
if (
(own === 'between' || (own === 'afterName' && node.type === 'atrule' && !node.nodes)) &&
!/:/.test(value)
) {
value = `:${value}`;
} else if (own === 'before' && /^(?:decl|rule)$/.test(node.type)) {
value = value.replace(/\S+$/, '');
}
return value;
}
rawValue(node, prop) {
const raw = node.raws[prop];
if (raw) {
const descriptor = Object.getOwnPropertyDescriptor(raw, 'raw');
if (descriptor && descriptor.get) {
return raw.prefix + raw.raw + raw.suffix;
}
}
let value = super.rawValue(node, prop);
if (value === null || value === undefined) {
return value;
}
if (/^(?:prop|selector)$/i.test(prop)) {
value = camelCase(value);
// eslint-disable-next-line regexp/no-unused-capturing-group -- TODO: fix
if (node.raws.before && /(\S+)$/.test(node.raws.before)) {
value = RegExp.$1 + value; // eslint-disable-line regexp/no-legacy-features -- TODO: fix
} else if (value && !/\W/.test(value)) {
return value;
}
} else if (node.type === 'atrule') {
if (prop === 'name') {
value = `@${value}`;
} else if (node.nodes) {
return;
}
if (node.nodes) {
value += this.raw(node, 'afterName');
value += super.rawValue(node, 'params');
}
}
value = JSON.stringify(value);
return value;
}
}
module.exports = ObjectStringifier;