18
18
* limitations under the License.
19
19
*/
20
20
21
- /* eslint-disable node/no-sync, no-console */
21
+ /* eslint-disable node/no-sync, no-console, max-statements */
22
22
23
23
'use strict' ;
24
24
@@ -44,6 +44,54 @@ var RE_TS_EXPORT = /export = ([^;]+);/;
44
44
var RE_EXAMPLE = / @ e x a m p l e [ \s \S ] * ?(? = @ e x a m p l e | \* \/ ) / g;
45
45
var RE_NAMESPACE = / @ n a m e s p a c e ( [ a - z . 0 - 9 ] + ) / i;
46
46
var RE_COPYRIGHT_YEAR = / C o p y r i g h t \( c \) ( \d { 4 } ) T h e S t d l i b A u t h o r s \. / ;
47
+ var RESERVED_WORDS = [
48
+ 'break' ,
49
+ 'case' ,
50
+ 'catch' ,
51
+ 'class' ,
52
+ 'const' ,
53
+ 'continue' ,
54
+ 'debugger' ,
55
+ 'default' ,
56
+ 'delete' ,
57
+ 'do' ,
58
+ 'else' ,
59
+ 'export' ,
60
+ 'extends' ,
61
+ 'false' ,
62
+ 'finally' ,
63
+ 'for' ,
64
+ 'function' ,
65
+ 'if' ,
66
+ 'import' ,
67
+ 'in' ,
68
+ 'instanceof' ,
69
+ 'new' ,
70
+ 'null' ,
71
+ 'return' ,
72
+ 'super' ,
73
+ 'switch' ,
74
+ 'this' ,
75
+ 'throw' ,
76
+ 'true' ,
77
+ 'try' ,
78
+ 'typeof' ,
79
+ 'var' ,
80
+ 'void' ,
81
+ 'while' ,
82
+ 'with' ,
83
+ 'let' ,
84
+ 'static' ,
85
+ 'yield' ,
86
+ 'await' ,
87
+ 'enum' ,
88
+ 'implements' ,
89
+ 'interface' ,
90
+ 'package' ,
91
+ 'private' ,
92
+ 'protected' ,
93
+ 'public'
94
+ ] ;
47
95
48
96
49
97
// FUNCTIONS //
@@ -175,6 +223,7 @@ function create( fullPath ) {
175
223
var tsDefPath ;
176
224
var testFile ;
177
225
var typesDir ;
226
+ var safeName ;
178
227
var defFile ;
179
228
var pkgPath ;
180
229
var match ;
@@ -237,11 +286,16 @@ function create( fullPath ) {
237
286
}
238
287
while ( match !== null ) {
239
288
name = match [ 1 ] ;
289
+ if ( contains ( RESERVED_WORDS , name ) ) {
290
+ safeName = '_' + name ;
291
+ } else {
292
+ safeName = name ;
293
+ }
240
294
pkgPath = match [ 2 ] ;
241
295
242
296
tsDefPath = replace ( require . resolve ( pkgPath ) , 'lib/index.js' , 'docs/types/index.d.ts' ) ;
243
297
if ( fs . existsSync ( tsDefPath ) ) {
244
- stmt = replace ( IMPORT_STATEMENT , '<name>' , name ) ;
298
+ stmt = replace ( IMPORT_STATEMENT , '<name>' , safeName ) ;
245
299
stmt = replace ( stmt , '<path>' , pkgPath ) ;
246
300
importStmts . push ( stmt ) ;
247
301
@@ -253,7 +307,7 @@ function create( fullPath ) {
253
307
if ( ! tsDoc ) {
254
308
tsDoc = tsDef . match ( / ( \/ \* \* \n [ ^ / ] + ?\* \/ ) \n d e c l a r e c l a s s / ) ;
255
309
}
256
- prop = '\t' + name + ': typeof ' + name + ';' ;
310
+ prop = '\t' + name + ': typeof ' + safeName + ';' ;
257
311
if ( tsDoc && tsDoc [ 1 ] ) {
258
312
str = tsDoc [ 1 ] ;
259
313
str = replace ( str , RE_EXAMPLE , onReplace ) ;
0 commit comments