-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.ts
241 lines (232 loc) · 7.86 KB
/
json.ts
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
/**
* Code generator for i18n json/json5 resource
*/
import { parseJSON, traverseNodes } from 'jsonc-eslint-parser'
import { isString } from '@intlify/shared'
import {
createCodeGenerator,
generateMessageFunction,
mapLinesColumns
} from './codegen'
import { RawSourceMap } from 'source-map'
import type { JSONProgram, JSONNode } from 'jsonc-eslint-parser/lib/parser/ast'
import type { CodeGenOptions, CodeGenerator, CodeGenResult } from './codegen'
/**
* @internal
*/
export function generate(
targetSource: string | Buffer,
{
type = 'plain',
filename = 'vue-i18n-loader.json',
inSourceMap = undefined,
locale = '',
isGlobal = false,
sourceMap = false,
env = 'development',
forceStringify = false
}: CodeGenOptions
): CodeGenResult<JSONProgram> {
const target = Buffer.isBuffer(targetSource)
? targetSource.toString()
: targetSource
// const value = JSON.stringify(JSON.parse(target))
// .replace(/\u2028/g, '\\u2028') // line separator
// .replace(/\u2029/g, '\\u2029') // paragraph separator
const value = target
const options = {
type,
source: value,
sourceMap,
locale,
isGlobal,
inSourceMap,
env,
filename,
forceStringify
} as CodeGenOptions
const generator = createCodeGenerator(options)
const ast = parseJSON(value, { filePath: filename })
const codeMaps = generateNode(generator, ast, options)
const { code, map } = generator.context()
// if (map) {
// const s = new SourceMapConsumer((map as any).toJSON())
// s.eachMapping(m => {
// console.log('sourcemap json', m)
// })
// }
// prettier-ignore
const newMap = map
? mapLinesColumns((map as any).toJSON(), codeMaps, inSourceMap) || null // eslint-disable-line @typescript-eslint/no-explicit-any
: null
return {
ast,
code,
map: newMap != null ? newMap : undefined
}
}
function generateNode(
generator: CodeGenerator,
node: JSONProgram,
options: CodeGenOptions
): Map<string, RawSourceMap> {
const propsCountStack = [] as number[]
const itemsCountStack = [] as number[]
const { forceStringify } = generator.context()
const codeMaps = new Map<string, RawSourceMap>()
const { type, sourceMap, isGlobal, locale } = options
traverseNodes(node, {
enterNode(node: JSONNode, parent: JSONNode) {
switch (node.type) {
case 'Program':
if (type === 'plain') {
generator.push(`export default `)
} else if (type === 'sfc') {
// for 'sfc'
const variableName =
type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : ''
const localeName =
type === 'sfc' ? (locale != null ? locale : `""`) : ''
generator.push(`export default function (Component) {`)
generator.indent()
generator.pushline(
`Component.${variableName} = Component.${variableName} || []`
)
generator.push(`Component.${variableName}.push({`)
generator.indent()
generator.pushline(`"locale": ${JSON.stringify(localeName)},`)
generator.push(`"resource": `)
}
break
case 'JSONObjectExpression':
generator.push(`{`)
generator.indent()
propsCountStack.push(node.properties.length)
if (parent.type === 'JSONArrayExpression') {
const lastIndex = itemsCountStack.length - 1
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex]
}
break
case 'JSONProperty':
if (
node.value.type === 'JSONLiteral' &&
(node.key.type === 'JSONLiteral' ||
node.key.type === 'JSONIdentifier')
) {
const name =
node.key.type === 'JSONLiteral' ? node.key.value : node.key.name
const value = node.value.value
if (isString(value)) {
generator.push(`${JSON.stringify(name)}: `)
const { code, map } = generateMessageFunction(value, options)
sourceMap && map != null && codeMaps.set(value, map)
generator.push(`${code}`, node.value, value)
} else {
if (forceStringify) {
const strValue = JSON.stringify(value)
generator.push(`${JSON.stringify(name)}: `)
const { code, map } = generateMessageFunction(strValue, options)
sourceMap && map != null && codeMaps.set(strValue, map)
generator.push(`${code}`, node.value, strValue)
} else {
generator.push(
`${JSON.stringify(name)}: ${JSON.stringify(value)}`
)
}
}
} else if (
(node.value.type === 'JSONObjectExpression' ||
node.value.type === 'JSONArrayExpression') &&
(node.key.type === 'JSONLiteral' ||
node.key.type === 'JSONIdentifier')
) {
const name =
node.key.type === 'JSONLiteral' ? node.key.value : node.key.name
generator.push(`${JSON.stringify(name)}: `)
}
const lastIndex = propsCountStack.length - 1
propsCountStack[lastIndex] = --propsCountStack[lastIndex]
break
case 'JSONArrayExpression':
generator.push(`[`)
generator.indent()
itemsCountStack.push(node.elements.length)
break
case 'JSONLiteral':
if (parent.type === 'JSONArrayExpression') {
if (node.type === 'JSONLiteral') {
const value = node.value
if (isString(value)) {
const { code, map } = generateMessageFunction(value, options)
sourceMap && map != null && codeMaps.set(value, map)
generator.push(`${code}`, node, value)
} else {
if (forceStringify) {
const strValue = JSON.stringify(value)
const { code, map } = generateMessageFunction(
strValue,
options
)
sourceMap && map != null && codeMaps.set(strValue, map)
generator.push(`${code}`, node, strValue)
} else {
generator.push(`${JSON.stringify(value)}`)
}
}
}
const lastIndex = itemsCountStack.length - 1
itemsCountStack[lastIndex] = --itemsCountStack[lastIndex]
}
break
default:
break
}
},
leaveNode(node: JSONNode, parent: JSONNode) {
switch (node.type) {
case 'Program':
if (type === 'sfc') {
generator.deindent()
generator.push(`})`)
generator.deindent()
generator.pushline(`}`)
}
break
case 'JSONObjectExpression':
if (propsCountStack[propsCountStack.length - 1] === 0) {
propsCountStack.pop()
}
generator.deindent()
generator.push(`}`)
if (parent.type === 'JSONArrayExpression') {
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
generator.pushline(`,`)
}
}
break
case 'JSONProperty':
if (propsCountStack[propsCountStack.length - 1] !== 0) {
generator.pushline(`,`)
}
break
case 'JSONArrayExpression':
if (itemsCountStack[itemsCountStack.length - 1] === 0) {
itemsCountStack.pop()
}
generator.deindent()
generator.push(`]`)
break
case 'JSONLiteral':
if (parent.type === 'JSONArrayExpression') {
if (itemsCountStack[itemsCountStack.length - 1] !== 0) {
generator.pushline(`,`)
}
}
break
default:
break
}
}
})
return codeMaps
}