liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
(function (global, factory) {
  // eslint-disable-next-line no-unused-expressions
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports)
  // eslint-disable-next-line no-undef
    : typeof define === 'function' && define.amd ? define('jsonlint-printer', ['exports'], factory)
    // eslint-disable-next-line no-undef
      : (global = global || self, factory(global.jsonlintPrinter = {}))
}(this, function (exports) {
  'use strict'
 
  function noop () {}
 
  function isIdentifierName (value) {
    return /^[a-zA-Z$_][a-zA-Z0-9$_]*$/.test(value)
  }
 
  function concatenateTokens (tokens) {
    var outputString = ''
    var tokenCount = tokens.length
    var tokenIndex
    for (tokenIndex = 0; tokenIndex < tokenCount; ++tokenIndex) {
      outputString += tokens[tokenIndex].raw
    }
    return outputString
  }
 
  function print (tokens, options) {
    if (!(tokens && tokens.length)) {
      throw new Error('JSON tokens missing.')
    }
    // Whitespace and comments are available only as raw token content.
    if (!(tokens[0] && tokens[0].raw)) {
      throw new Error('JSON tokens lack raw values.')
    }
 
    if (!options) {
      // If no options, not even an empty object is passed, just concatenate
      // the raw tokens with neither minification, nor pretty-printing.
      return concatenateTokens(tokens)
    }
 
    var indentString = options.indent
    if (typeof indentString === 'number') {
      indentString = new Array(indentString + 1).join(' ')
    }
    // Setting the indent to an empty string enables pretty-printing too.
    // It will just insert line breaks without any indentation.
    var prettyPrint = indentString !== undefined
    var pruneComments = options.pruneComments
    var stripObjectKeys = options.stripObjectKeys
    var enforceDoubleQuotes = options.enforceDoubleQuotes
    var enforceSingleQuotes = options.enforceSingleQuotes
    var trimTrailingCommas = options.trimTrailingCommas
 
    var outputString = ''
    var foundLineBreak, addedLineBreak, needsLineBreak
    var addedSpace, needsSpace
    var indentLevel = 0
    var scopes = []
    var scopeType
    var isValue
    var tokenCount = tokens.length
    var tokenIndex, token, tokenType, tokenContent
 
    function peekAtNextToken () {
      var nextTokenIndex = tokenIndex
      var nextToken
      do {
        nextToken = tokens[++nextTokenIndex]
      } while (nextToken && (nextToken.type === 'whitespace' ||
                             nextToken.type === 'comment'))
      return nextToken
    }
 
    var addIndent
    if (prettyPrint && indentString) {
      addIndent = function () {
        for (var i = 0; i < indentLevel; ++i) {
          outputString += indentString
        }
      }
    } else {
      addIndent = noop
    }
 
    var addLineBreak, addDelayedSpaceOrLineBreak
    if (prettyPrint) {
      addLineBreak = function () {
        outputString += '\n'
      }
 
      addDelayedSpaceOrLineBreak = function () {
        // A line break is more important than a space.
        if (needsLineBreak) {
          addLineBreak()
          addIndent()
        } else if (needsSpace) {
          outputString += ' '
        }
        needsSpace = needsLineBreak = false
      }
    } else {
      addLineBreak = addDelayedSpaceOrLineBreak = noop
    }
 
    var addStandaloneComment, tryAddingInlineComment
    if (pruneComments) {
      addStandaloneComment = tryAddingInlineComment = noop
    } else {
      if (prettyPrint) {
        addStandaloneComment = function () {
          // If a comment is not appended to the end of a line, it will start
          // on a new line with the current indentation.
          if (!addedLineBreak && tokenIndex > 0) {
            addLineBreak()
            addIndent()
          }
          outputString += tokenContent
          foundLineBreak = false
          addedLineBreak = false
          // If a comment is not appended to the end of a line, it will take
          // the whole line and has to end by a line break.
          needsLineBreak = true
        }
 
        tryAddingInlineComment = function () {
          // This function is called after printing a non-line-break character.
          foundLineBreak = false
          addedLineBreak = false
          addedSpace = false
 
          // Start with the character after the just processed one.
          var tryTokenIndex = tokenIndex + 1
 
          function skipWhitespace () {
            var token = tokens[tryTokenIndex]
            if (token && token.type === 'whitespace') {
              foundLineBreak = token.raw.indexOf('\n') >= 0
              token = tokens[++tryTokenIndex]
            }
            return token
          }
 
          var token = skipWhitespace()
          // If line break followed the previous token, leave the comment
          // to be handled by the next usual token processing.
          if (!foundLineBreak && token && token.type === 'comment') {
            if (needsLineBreak) {
              // If the previous non-whitespace token was ended by a line
              // break, retain it. Print the comment after the line break too.
              if (!addedLineBreak) {
                addLineBreak()
                addIndent()
              }
            } else {
              // If the previous non-whitespace token was not ended by a line
              // break, ensure that the comment is separated from it.
              if (!addedSpace) {
                outputString += ' '
              }
            }
            outputString += token.raw
            // Set the current token to the just processed comment.
            tokenIndex = tryTokenIndex++
            // Check the whitespace after the comment to give a hint
            // about the next whitespace to the further processing.
            skipWhitespace()
            if (foundLineBreak) {
              needsSpace = false
              needsLineBreak = true
            } else {
              needsSpace = true
              needsLineBreak = false
            }
          }
        }
      } else {
        // If all whitespace is omitted, convert single-line comments
        // to multi-line ones, which include a comment-closing token.
        addStandaloneComment = function () {
          if (tokenContent[1] === '/') {
            outputString += '/*'
            outputString += tokenContent.substr(2, tokenContent.length - 2)
            outputString += ' */'
          } else {
            outputString += tokenContent
          }
        }
 
        tryAddingInlineComment = noop
      }
    }
 
    function addLiteral () {
      addDelayedSpaceOrLineBreak()
      var tokenValue = token.value
      if (stripObjectKeys && scopeType === '{' && !isValue &&
          isIdentifierName(tokenValue)) {
        outputString += tokenValue
      } else if (typeof tokenValue === 'string') {
        if (enforceDoubleQuotes && tokenContent[0] !== '"') {
          outputString += JSON.stringify(tokenValue)
        } else if (enforceSingleQuotes && tokenContent[0] !== '\'') {
          outputString += '\'' + tokenValue.replace(/'/g, '\\\'') + '\''
        } else {
          outputString += tokenContent
        }
      } else {
        outputString += tokenContent
      }
      tryAddingInlineComment()
    }
 
    function openScope () {
      addDelayedSpaceOrLineBreak()
      scopes.push(scopeType)
      scopeType = tokenContent
      isValue = scopeType === '['
      outputString += tokenContent
      tryAddingInlineComment()
      ++indentLevel
      needsLineBreak = true
    }
 
    function closeScope () {
      scopeType = scopes.pop()
      addLineBreak()
      --indentLevel
      addIndent()
      needsSpace = needsLineBreak = false
      outputString += tokenContent
      tryAddingInlineComment()
    }
 
    function addComma () {
      if (trimTrailingCommas) {
        var nextToken = peekAtNextToken()
        if (nextToken && nextToken.type === 'symbol') {
          return tryAddingInlineComment()
        }
      }
      addDelayedSpaceOrLineBreak()
      outputString += ','
      tryAddingInlineComment()
      addLineBreak()
      addIndent()
      addedLineBreak = true
      needsLineBreak = false
      isValue = scopeType === '['
    }
 
    function addColon () {
      addDelayedSpaceOrLineBreak()
      outputString += ':'
      needsSpace = true
      tryAddingInlineComment()
      isValue = true
    }
 
    for (tokenIndex = 0; tokenIndex < tokenCount; ++tokenIndex) {
      token = tokens[tokenIndex]
      tokenType = token.type
      tokenContent = token.raw
      switch (tokenType) {
        case 'literal':
          addLiteral()
          break
        case 'comment':
          addStandaloneComment()
          break
        case 'symbol':
          switch (tokenContent) {
            case '{':
            case '[':
              openScope()
              break
            case '}':
            case ']':
              closeScope()
              break
            case ',':
              addComma()
              break
            case ':':
              addColon()
          }
          break
        default: // whitespace
          foundLineBreak = tokenContent.indexOf('\n') >= 0
      }
    }
 
    return outputString
  }
 
  exports.print = print
 
  Object.defineProperty(exports, '__esModule', { value: true })
}))