forked from generaltranslation/gt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
288 lines (262 loc) · 9.86 KB
/
index.ts
File metadata and controls
288 lines (262 loc) · 9.86 KB
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
import {
colorizeFilepath,
colorizeComponent,
colorizeIdString,
colorizeContent,
colorizeLine,
colorizeFunctionName,
} from './colors.js';
import { formatCodeClamp } from './formatting.js';
const withWillErrorInNextVersion = (message: string): string =>
`${message} (This will become an error in the next major version of the CLI.)`;
// Static function related errors
const withStaticError = (message: string): string =>
`<Derive> rules violation: ${message}`;
const withDeclareStaticError = (message: string): string =>
`derive() rules violation: ${message}`;
// Synchronous wrappers for backward compatibility
export const warnApiKeyInConfigSync = (optionsFilepath: string): string =>
`${colorizeFilepath(
optionsFilepath
)}: Your API key is exposed! Please remove it from the file and include it as an environment variable.`;
export const warnVariablePropSync = (
file: string,
attrName: string,
value: string,
location?: string
): string =>
withLocation(
file,
`${colorizeComponent('<T>')} component has dynamic attribute ${colorizeIdString(attrName)} with value: ${colorizeContent(
value
)}. Change ${colorizeIdString(attrName)} to ensure this content is translated.`,
location
);
export const warnInvalidReturnSync = (
file: string,
functionName: string,
expression: string,
location?: string
): string =>
withLocation(
file,
withStaticError(
`Function ${colorizeFunctionName(functionName)} does not return a static expression. ${colorizeFunctionName(functionName)} must return either (1) a static string literal, (2) another static function invocation, (3) static JSX content, or (4) a ternary expression. Instead got:\n${colorizeContent(expression)}`
),
location
);
// TODO: this is temporary until we handle implicit returns
export const warnMissingReturnSync = (
file: string,
functionName: string,
location?: string
): string =>
withLocation(
file,
`Function ${colorizeFunctionName(functionName)} is wrapped in ${colorizeComponent('<Derive>')} (or ${colorizeComponent('<Static>')}) tags but does not have an explicit return statement. Static functions must have an explicit return statement.`,
location
);
export const warnHasUnwrappedExpressionSync = (
file: string,
unwrappedExpressions: string[],
id?: string,
location?: string
): string =>
withLocation(
file,
`${colorizeComponent('<T>')} component${
id ? ` with id ${colorizeIdString(id)}` : ''
} has children that could change at runtime. Use a variable component like ${colorizeComponent(
'<Var>'
)} to ensure this content is translated.\n${colorizeContent(
unwrappedExpressions.join('\n')
)}`,
location
);
export const warnFailedToConstructJsxTreeSync = (
file: string,
code: string,
location?: string
): string =>
withLocation(
file,
`Failed to construct JsxTree! Call expression is not a valid createElement call: ${colorizeContent(code)}`,
location
);
export const warnNestedTComponent = (file: string, location?: string): string =>
withLocation(
file,
`Found nested <T> component. <T> components cannot be directly nested.`,
location
);
export const warnNonStaticExpressionSync = (
file: string,
attrName: string,
value: string,
location?: string
): string =>
withLocation(
file,
`Found non-static expression for attribute ${colorizeIdString(
attrName
)}: ${colorizeContent(value)}. Change "${colorizeIdString(attrName)}" to ensure this content is translated.`,
location
);
export const warnInvalidMaxCharsSync = (
file: string,
value: string,
location?: string
): string =>
withLocation(
file,
`Found invalid maxChars value: ${colorizeContent(value)}. Change the value to a valid number to ensure this content is translated.`,
location
);
export const warnInvalidIcuSync = (
file: string,
value: string,
error: string,
location?: string
): string =>
withWillErrorInNextVersion(
withLocation(
file,
`Found invalid ICU string: ${colorizeContent(value)}. Change the value to a valid ICU to ensure this content is translated. Error message: ${error}.`,
location
)
);
export const warnTemplateLiteralSync = (
file: string,
value: string,
location?: string
): string =>
withLocation(
file,
`Found template literal with quasis (${colorizeContent(value)}). Change the template literal to a string to ensure this content is translated.`,
location
);
export const warnNonStringSync = (
file: string,
value: string,
location?: string
): string =>
withLocation(
file,
`Found non-string literal (${colorizeContent(value)}). Change the value to a string literal to ensure this content is translated.`,
location
);
export const warnAsyncUseGT = (file: string, location?: string): string =>
withLocation(
file,
`Found useGT() in an async function. Use getGT() instead, or make the function synchronous.`,
location
);
export const warnSyncGetGT = (file: string, location?: string): string =>
withLocation(
file,
`Found getGT() in a synchronous function. Use useGT() instead, or make the function async.`,
location
);
export const warnTernarySync = (file: string, location?: string): string =>
withLocation(
file,
'Found ternary expression. A Branch component may be more appropriate here.',
location
);
export const withLocation = (
file: string,
message: string,
location?: string
): string =>
`${colorizeFilepath(file)}${location ? ` (${colorizeLine(location)})` : ''}: ${message}`;
export const warnFunctionNotFoundSync = (
file: string,
functionName: string,
location?: string
): string =>
withLocation(
file,
`Function ${colorizeFunctionName(functionName)} definition could not be resolved. This might affect translation resolution for this ${colorizeComponent('<T>')} component.`,
location
);
export const warnInvalidDeclareVarNameSync = (
file: string,
value: string,
location?: string
): string =>
withLocation(
file,
`Found invalid declareVar() $name tag. Must be a static expression. Received: ${colorizeContent(value)}.`,
location
);
export const warnDuplicateFunctionDefinitionSync = (
file: string,
functionName: string,
location?: string
): string =>
withLocation(
file,
`Function ${colorizeFunctionName(functionName)} is defined multiple times. Only the first definition will be used.`,
location
);
export const warnInvalidStaticInitSync = (
file: string,
functionName: string,
location?: string
): string =>
withLocation(
file,
withStaticError(
`The definition for ${colorizeFunctionName(functionName)} could not be resolved. When using arrow syntax to define a static function, the right hand side or the assignment MUST only contain the arrow function itself and no other expressions.
Example: ${colorizeContent(`const ${colorizeFunctionName(functionName)} = () => { ... }`)}
Invalid: ${colorizeContent(`const ${colorizeFunctionName(functionName)} = [() => { ... }][0]`)}`
),
location
);
export const warnRecursiveFunctionCallSync = (
file: string,
functionName: string,
location?: string
): string =>
withLocation(
file,
withStaticError(
`Recursive function call detected: ${colorizeFunctionName(functionName)}. A static function cannot use recursive calls to construct its result.`
),
location
);
export const warnDeclareStaticNotWrappedSync = (
file: string,
functionName: string,
location?: string
): string =>
withLocation(
file,
withDeclareStaticError(
`Could not resolve ${colorizeFunctionName(formatCodeClamp(functionName))}. This call is not wrapped in derive() (or declareStatic()). Ensure the function is properly wrapped with derive() and does not have circular import dependencies.`
),
location
);
export const warnDeclareStaticNoResultsSync = (
file: string,
functionName: string,
location?: string
): string =>
withLocation(
file,
withDeclareStaticError(
`Could not resolve ${colorizeFunctionName(formatCodeClamp(functionName))}. derive() can only receive function invocations and cannot use undefined values or looped calls to construct its result.`
),
location
);
// Re-export error messages
export const noLocalesError = `No locales found! Please provide a list of locales for translation, or specify them in your gt.config.json file.`;
export const noDefaultLocaleError = `No default locale found! Please provide a default locale, or specify it in your gt.config.json file.`;
export const noFilesError = `Incorrect or missing files configuration! Please make sure your files are configured correctly in your gt.config.json file.`;
export const noSourceFileError = `No source file found! Please double check your translations directory and default locale.`;
export const noSupportedFormatError = `Unsupported data format! Please make sure your translationsDir parameter ends with a supported file extension.`;
export const noApiKeyError = `No API key found! Please provide an API key using the --api-key flag or set the GT_API_KEY environment variable.`;
export const devApiKeyError = `You are using a development API key. Please use a production API key to use the General Translation API.\nYou can generate a production API key with the command: npx gtx-cli auth -t production`;
export const noProjectIdError = `No project ID found! Please provide a project ID using the --project-id flag, specify it in your gt.config.json file, or set the GT_PROJECT_ID environment variable.`;
export const noVersionIdError = `No version ID found! Please provide a version ID using the --version-id flag or specify it in your gt.config.json file as the _versionId property.`;
export const invalidConfigurationError = `Invalid files configuration! Please either provide a valid configuration to download local translations or set the --publish flag to true to upload translations to the CDN.`;