1
1
import * as path from "path" ;
2
- import types , { NodePath } from "ast-types" ;
2
+ import type { NodePath } from "ast-types/lib/node-path" ;
3
+ import { namedTypes as n , builders as b , PathVisitor } from "ast-types" ;
3
4
import { Context } from "ast-types/lib/path-visitor" ;
4
- import {
5
- CallExpression ,
6
- Literal ,
7
- ArrayExpression ,
8
- ObjectExpression ,
9
- Identifier ,
10
- Node ,
11
- MemberExpression
12
- } from "ast-types/gen/nodes" ;
5
+ import type { namedTypes } from "ast-types" ;
13
6
import Translation from "../translation" ;
14
7
import TranslateLoaderContext from "../translate-loader-context" ;
15
8
16
- const { namedTypes : n , builders : b } = types ;
17
9
const TRANSLATE_SERVICE_NAME = "$translate" ;
18
10
19
11
export default function createTranslateVisitor (
20
12
loader : TranslateLoaderContext ,
21
- parserOptions : acorn . Options = { }
13
+ parserOptions : acorn . Options = { ecmaVersion : "latest" }
22
14
) {
23
15
let context : Context = null ;
24
16
const comments : acorn . Comment [ ] = [ ] ;
@@ -34,7 +26,7 @@ export default function createTranslateVisitor(
34
26
* Handles a $translate(translateId, interpolateParams, interpolationId, defaultText) call.
35
27
* @param path the path to the call expression
36
28
*/
37
- function visitTranslate ( path : NodePath < CallExpression > ) : void {
29
+ function visitTranslate ( path : NodePath < namedTypes . CallExpression > ) : void {
38
30
const call = path . node ;
39
31
const args = call . arguments ;
40
32
@@ -55,19 +47,19 @@ export default function createTranslateVisitor(
55
47
}
56
48
57
49
function getTranslationIdFromTranslateCall (
58
- path : NodePath < CallExpression >
50
+ path : NodePath < namedTypes . CallExpression >
59
51
) : any [ ] {
60
52
const args = path . node . arguments ;
61
53
62
54
if ( n . Literal . check ( args [ 0 ] ) ) {
63
- return [ ( < Literal > args [ 0 ] ) . value ] ;
55
+ return [ args [ 0 ] . value ] ;
64
56
}
65
57
66
58
if ( n . ArrayExpression . check ( args [ 0 ] ) ) {
67
- const arrayExpression = < ArrayExpression > args [ 0 ] ;
59
+ const arrayExpression = args [ 0 ] ;
68
60
return arrayExpression . elements . map ( element => {
69
61
if ( n . Literal . check ( element ) ) {
70
- return ( < Literal > element ) . value ;
62
+ return element . value ;
71
63
}
72
64
throwSuppressableError (
73
65
"The array with the translation ids should only contain literals" ,
@@ -83,13 +75,13 @@ export default function createTranslateVisitor(
83
75
}
84
76
85
77
function getDefaultTextFromTranslateCall (
86
- path : NodePath < CallExpression >
78
+ path : NodePath < namedTypes . CallExpression >
87
79
) : any {
88
80
const args = path . node . arguments ;
89
81
90
82
if ( args . length > 3 ) {
91
83
if ( n . Literal . check ( args [ 3 ] ) ) {
92
- return ( < Literal > args [ 3 ] ) . value ;
84
+ return args [ 3 ] . value ;
93
85
}
94
86
95
87
throwSuppressableError (
@@ -107,7 +99,7 @@ export default function createTranslateVisitor(
107
99
* translation id.
108
100
* @param path of the call expression.
109
101
*/
110
- function visitRegisterTranslation ( path : NodePath < CallExpression > ) : void {
102
+ function visitRegisterTranslation ( path : NodePath < namedTypes . CallExpression > ) : void {
111
103
const call = path . node ,
112
104
args = call . arguments ;
113
105
@@ -118,12 +110,12 @@ export default function createTranslateVisitor(
118
110
) ;
119
111
}
120
112
121
- const translationId = ( < Literal > args [ 0 ] ) . value ;
113
+ const translationId = args [ 0 ] . value ;
122
114
let defaultText : any ;
123
115
124
116
if ( args . length > 1 ) {
125
117
if ( n . Literal . check ( args [ 1 ] ) ) {
126
- defaultText = < string > ( < Literal > args [ 1 ] ) . value ;
118
+ defaultText = < string > args [ 1 ] . value ;
127
119
} else {
128
120
throwError (
129
121
"Illegal argument for call to i18n.registerTranslation: the default text has to be a literal" ,
@@ -158,9 +150,7 @@ export default function createTranslateVisitor(
158
150
) ;
159
151
}
160
152
161
- const translations : Translation [ ] = ( < ObjectExpression > (
162
- translationsArgument
163
- ) ) . properties . map ( property => {
153
+ const translations : Translation [ ] = ( translationsArgument ) . properties . map ( property => {
164
154
let translationId : any ;
165
155
let defaultText : any ;
166
156
@@ -177,9 +167,9 @@ export default function createTranslateVisitor(
177
167
}
178
168
179
169
if ( n . Identifier . check ( property . key ) ) {
180
- translationId = ( < Identifier > property . key ) . name ;
170
+ translationId = property . key . name ;
181
171
} else if ( n . Literal . check ( property . key ) ) {
182
- translationId = ( < Literal > property . key ) . value ;
172
+ translationId = property . key . value ;
183
173
} else {
184
174
throwError (
185
175
"Illegal argument for call to i18n.registerTranslations: The key needs to be a literal or an identifier." ,
@@ -188,7 +178,7 @@ export default function createTranslateVisitor(
188
178
}
189
179
190
180
if ( n . Literal . check ( property . value ) ) {
191
- defaultText = ( < Literal > property . value ) . value ;
181
+ defaultText = property . value . value ;
192
182
} else {
193
183
throwError (
194
184
`Illegal argument for call to i18n.registerTranslations: The value for the key '${ translationId } ' needs to be a literal` ,
@@ -212,7 +202,7 @@ export default function createTranslateVisitor(
212
202
function createTranslation (
213
203
translationId : any ,
214
204
defaultText : any ,
215
- node : Node
205
+ node : namedTypes . Node
216
206
) : Translation {
217
207
const idAsString = valueToString ( translationId , "" ) ;
218
208
const defaultTextAsString = valueToString ( defaultText , undefined ) ;
@@ -227,14 +217,14 @@ export default function createTranslateVisitor(
227
217
* @param call the call expression
228
218
* @returns {string } the name of the function
229
219
*/
230
- function getFunctionName ( call : CallExpression ) : string | undefined {
220
+ function getFunctionName ( call : namedTypes . CallExpression ) : string | undefined {
231
221
var callee = call . callee ;
232
222
if ( n . Identifier . check ( callee ) ) {
233
- return ( < Identifier > callee ) . name ;
223
+ return callee . name ;
234
224
} else if ( n . MemberExpression . check ( callee ) ) {
235
- const property = ( < MemberExpression > callee ) . property ;
225
+ const property = callee . property ;
236
226
if ( n . Identifier . check ( property ) ) {
237
- return ( < Identifier > property ) . name ;
227
+ return property . name ;
238
228
}
239
229
return "[expression]" ;
240
230
} else if ( n . FunctionExpression . check ( callee ) ) {
@@ -249,18 +239,18 @@ export default function createTranslateVisitor(
249
239
* @param call the call expression
250
240
* @returns {string } the name of the callee or null if the name could not be determined
251
241
*/
252
- function getCalleeName ( call : CallExpression ) : string | null {
242
+ function getCalleeName ( call : namedTypes . CallExpression ) : string | null {
253
243
// this.method() or object.method()
254
244
if ( call . callee . type === "MemberExpression" ) {
255
- const member = < MemberExpression > call . callee ;
245
+ const member = call . callee ;
256
246
if ( member . object . type === "Identifier" ) {
257
- return ( < Identifier > member . object ) . name ;
247
+ return member . object . name ;
258
248
} else if ( member . object . type === "ThisExpression" ) {
259
249
return "this" ;
260
250
} else if ( member . object . type == "MemberExpression" ) {
261
- const parent = < MemberExpression > member . object ;
251
+ const parent = member . object ;
262
252
if ( parent . property . type === "Identifier" ) {
263
- return ( < Identifier > parent . property ) . name ;
253
+ return parent . property . name ;
264
254
}
265
255
}
266
256
}
@@ -274,7 +264,7 @@ export default function createTranslateVisitor(
274
264
* @param message the message to emit
275
265
* @param node the node for which a message is emitted
276
266
*/
277
- function throwError ( message : string , node : Node ) : never {
267
+ function throwError ( message : string , node : namedTypes . Node ) : never {
278
268
const relativePath = path . relative ( loader . context , loader . resourcePath ) ;
279
269
const start = node . loc ! . start ,
280
270
completeMessage = `${ message } (${ relativePath } :${ start . line } :${
@@ -325,8 +315,8 @@ export default function createTranslateVisitor(
325
315
return "" + value ;
326
316
}
327
317
328
- const visitor = types . PathVisitor . fromMethodsObject ( {
329
- visitCallExpression ( path : NodePath < CallExpression > ) : boolean {
318
+ const visitor = PathVisitor . fromMethodsObject ( {
319
+ visitCallExpression ( path : NodePath < namedTypes . CallExpression > ) : boolean {
330
320
context = this ;
331
321
const call = path . node ,
332
322
functionName = getFunctionName ( call ) ,
0 commit comments