@@ -13,7 +13,7 @@ function addTest (description, test) {
13
13
}
14
14
}
15
15
16
- var tokens = [ // '/* start */{ "a":1, // c\n"0b": [ 2,3 ]}'
16
+ var mixedTokens = [ // '/* start */{ "a":1, // c\n"0b": [ 2,3 ]}'
17
17
{ type : 'comment' , raw : '/* start */' } ,
18
18
{ type : 'symbol' , raw : '{' , value : '{' } ,
19
19
{ type : 'whitespace' , raw : ' ' } ,
@@ -37,73 +37,92 @@ var tokens = [ // '/* start */{ "a":1, // c\n"0b": [ 2,3 ]}'
37
37
{ type : 'symbol' , raw : '}' , value : '}' }
38
38
]
39
39
40
+ // `// test
41
+ // {// test
42
+ // // test
43
+ // a:/* test */1,// test
44
+ // b:2// test
45
+ // // test
46
+ // }// test
47
+ // // test`
48
+ var commentTokens = [
49
+ { type : 'comment' , raw : '// test' } ,
50
+ { type : 'whitespace' , raw : '\n' } ,
51
+ { type : 'symbol' , raw : '{' , value : '{' } ,
52
+ { type : 'comment' , raw : '// test' } ,
53
+ { type : 'whitespace' , raw : '\n' } ,
54
+ { type : 'comment' , raw : '// test' } ,
55
+ { type : 'whitespace' , raw : '\n' } ,
56
+ { type : 'literal' , raw : 'a' , value : 'a' } ,
57
+ { type : 'symbol' , raw : ':' , value : ':' } ,
58
+ { type : 'comment' , raw : '/* test */' } ,
59
+ { type : 'literal' , raw : '1' , value : 1 } ,
60
+ { type : 'symbol' , raw : ',' , value : ',' } ,
61
+ { type : 'comment' , raw : '// test' } ,
62
+ { type : 'whitespace' , raw : '\n' } ,
63
+ { type : 'literal' , raw : 'b' , value : 'b' } ,
64
+ { type : 'symbol' , raw : ':' , value : ':' } ,
65
+ { type : 'literal' , raw : '2' , value : 2 } ,
66
+ { type : 'comment' , raw : '// test' } ,
67
+ { type : 'whitespace' , raw : '\n' } ,
68
+ { type : 'comment' , raw : '// test' } ,
69
+ { type : 'whitespace' , raw : '\n' } ,
70
+ { type : 'symbol' , raw : '}' , value : '}' } ,
71
+ { type : 'comment' , raw : '// test' } ,
72
+ { type : 'whitespace' , raw : '\n' } ,
73
+ { type : 'comment' , raw : '// test' }
74
+ ]
75
+
76
+ // `{
77
+ // // String parameter
78
+ // "key": 'value',
79
+ // }`
80
+ var stringTokens = [
81
+ { type : 'symbol' , raw : '{' , value : '{' } ,
82
+ { type : 'whitespace' , raw : '\n' } ,
83
+ { type : 'comment' , raw : '// String parameter' } ,
84
+ { type : 'whitespace' , raw : '\n' } ,
85
+ { type : 'literal' , raw : '"key"' , value : 'key' } ,
86
+ { type : 'symbol' , raw : ':' , value : ':' } ,
87
+ { type : 'whitespace' , raw : ' ' } ,
88
+ { type : 'literal' , raw : '\'value\'' , value : 'value' } ,
89
+ { type : 'symbol' , raw : ',' , value : ',' } ,
90
+ { type : 'whitespace' , raw : '\n' } ,
91
+ { type : 'symbol' , raw : '}' , value : '}' }
92
+ ]
93
+
40
94
addTest ( 'concatenate tokens' , function ( ) {
41
- var output = print ( tokens )
95
+ var output = print ( mixedTokens )
42
96
assert . equal ( output , '/* start */{ "a":1, // c\n"0b": [ 2,3 ]}' )
43
97
} )
44
98
45
99
addTest ( 'omit whitespace' , function ( ) {
46
- var output = print ( tokens , { } )
100
+ var output = print ( mixedTokens , { } )
47
101
assert . equal ( output , '/* start */{"a":1,/* c */"0b":[2,3]}' )
48
102
} )
49
103
50
104
addTest ( 'introduce line breaks' , function ( ) {
51
- var output = print ( tokens , { indent : '' } )
105
+ var output = print ( mixedTokens , { indent : '' } )
52
106
assert . equal ( output , '/* start */\n{\n"a": 1, // c\n"0b": [\n2,\n3\n]\n}' )
53
107
} )
54
108
55
109
addTest ( 'apply indent' , function ( ) {
56
- var output = print ( tokens , { indent : 2 } )
110
+ var output = print ( mixedTokens , { indent : 2 } )
57
111
assert . equal ( output , '/* start */\n{\n "a": 1, // c\n "0b": [\n 2,\n 3\n ]\n}' )
58
112
} )
59
113
60
114
addTest ( 'omit comments' , function ( ) {
61
- var output = print ( tokens , { pruneComments : true } )
115
+ var output = print ( mixedTokens , { pruneComments : true } )
62
116
assert . equal ( output , '{"a":1,"0b":[2,3]}' )
63
117
} )
64
118
65
119
addTest ( 'strip quotes from object keys' , function ( ) {
66
- var output = print ( tokens , { stripObjectKeys : true } )
120
+ var output = print ( mixedTokens , { stripObjectKeys : true } )
67
121
assert . equal ( output , '/* start */{a:1,/* c */"0b":[2,3]}' )
68
122
} )
69
123
70
124
addTest ( 'keep comment locations' , function ( ) {
71
- // `// test
72
- // {// test
73
- // // test
74
- // a:/* test */1,// test
75
- // b:2// test
76
- // // test
77
- // }// test
78
- // // test`
79
- var tokens = [
80
- { type : 'comment' , raw : '// test' } ,
81
- { type : 'whitespace' , raw : '\n' } ,
82
- { type : 'symbol' , raw : '{' , value : '{' } ,
83
- { type : 'comment' , raw : '// test' } ,
84
- { type : 'whitespace' , raw : '\n' } ,
85
- { type : 'comment' , raw : '// test' } ,
86
- { type : 'whitespace' , raw : '\n' } ,
87
- { type : 'literal' , raw : 'a' , value : 'a' } ,
88
- { type : 'symbol' , raw : ':' , value : ':' } ,
89
- { type : 'comment' , raw : '/* test */' } ,
90
- { type : 'literal' , raw : '1' , value : 1 } ,
91
- { type : 'symbol' , raw : ',' , value : ',' } ,
92
- { type : 'comment' , raw : '// test' } ,
93
- { type : 'whitespace' , raw : '\n' } ,
94
- { type : 'literal' , raw : 'b' , value : 'b' } ,
95
- { type : 'symbol' , raw : ':' , value : ':' } ,
96
- { type : 'literal' , raw : '2' , value : 2 } ,
97
- { type : 'comment' , raw : '// test' } ,
98
- { type : 'whitespace' , raw : '\n' } ,
99
- { type : 'comment' , raw : '// test' } ,
100
- { type : 'whitespace' , raw : '\n' } ,
101
- { type : 'symbol' , raw : '}' , value : '}' } ,
102
- { type : 'comment' , raw : '// test' } ,
103
- { type : 'whitespace' , raw : '\n' } ,
104
- { type : 'comment' , raw : '// test' }
105
- ]
106
- var output = print ( tokens , { indent : ' ' } )
125
+ var output = print ( commentTokens , { indent : ' ' } )
107
126
assert . equal ( output , '// test\n{ // test\n // test\n a: /* test */ 1, // test\n b: 2 // test\n // test\n} // test\n// test' )
108
127
// `// test
109
128
// { // test
@@ -116,29 +135,30 @@ addTest('keep comment locations', function () {
116
135
} )
117
136
118
137
addTest ( 'keep comment after opening an object scope indented' , function ( ) {
119
- // `{
120
- // // String parameter
121
- // "key": 'value',
122
- // }`
123
- var tokens = [
124
- { type : 'symbol' , raw : '{' , value : '{' } ,
125
- { type : 'whitespace' , raw : '\n' } ,
126
- { type : 'comment' , raw : '// String parameter' } ,
127
- { type : 'whitespace' , raw : '\n' } ,
128
- { type : 'literal' , raw : '"key"' , value : 'key' } ,
129
- { type : 'symbol' , raw : ':' , value : ':' } ,
130
- { type : 'whitespace' , raw : ' ' } ,
131
- { type : 'literal' , raw : '\'value\'' , value : 'value' } ,
132
- { type : 'symbol' , raw : ',' , value : ',' } ,
133
- { type : 'whitespace' , raw : '\n' } ,
134
- { type : 'symbol' , raw : '}' , value : '}' }
135
- ]
136
- var output = print ( tokens , { indent : ' ' } )
138
+ var output = print ( stringTokens , { indent : ' ' } )
137
139
assert . equal ( output , '{\n // String parameter\n "key": \'value\',\n \n}' )
138
140
// `{
139
141
// // String parameter
140
142
// "key": 'value',
141
143
// }`
142
144
} )
143
145
146
+ addTest ( 'enforce double quotes' , function ( ) {
147
+ var output = print ( stringTokens , { enforceDoubleQuotes : true } )
148
+ assert . equal ( output , '{/* String parameter */"key":"value",}' )
149
+ } )
150
+
151
+ addTest ( 'enforce single quotes' , function ( ) {
152
+ var output = print ( stringTokens , { enforceSingleQuotes : true } )
153
+ assert . equal ( output , '{/* String parameter */\'key\':\'value\',}' )
154
+ } )
155
+
156
+ addTest ( 'enforce double quotes, but strip quotes from object keys' , function ( ) {
157
+ var output = print ( stringTokens , {
158
+ stripObjectKeys : true ,
159
+ enforceDoubleQuotes : true
160
+ } )
161
+ assert . equal ( output , '{/* String parameter */key:"value",}' )
162
+ } )
163
+
144
164
if ( require . main === module ) { require ( 'test' ) . run ( exports ) }
0 commit comments