1818* limitations under the License.
1919*/
2020
21- /* eslint-disable node/no-sync, no-console */
21+ /* eslint-disable node/no-sync, no-console, max-statements */
2222
2323'use strict' ;
2424
@@ -44,6 +44,54 @@ var RE_TS_EXPORT = /export = ([^;]+);/;
4444var RE_EXAMPLE = / @ e x a m p l e [ \s \S ] * ?(? = @ e x a m p l e | \* \/ ) / g;
4545var RE_NAMESPACE = / @ n a m e s p a c e ( [ a - z . 0 - 9 ] + ) / i;
4646var 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+ ] ;
4795
4896
4997// FUNCTIONS //
@@ -175,6 +223,7 @@ function create( fullPath ) {
175223 var tsDefPath ;
176224 var testFile ;
177225 var typesDir ;
226+ var safeName ;
178227 var defFile ;
179228 var pkgPath ;
180229 var match ;
@@ -237,11 +286,16 @@ function create( fullPath ) {
237286 }
238287 while ( match !== null ) {
239288 name = match [ 1 ] ;
289+ if ( contains ( RESERVED_WORDS , name ) ) {
290+ safeName = '_' + name ;
291+ } else {
292+ safeName = name ;
293+ }
240294 pkgPath = match [ 2 ] ;
241295
242296 tsDefPath = replace ( require . resolve ( pkgPath ) , 'lib/index.js' , 'docs/types/index.d.ts' ) ;
243297 if ( fs . existsSync ( tsDefPath ) ) {
244- stmt = replace ( IMPORT_STATEMENT , '<name>' , name ) ;
298+ stmt = replace ( IMPORT_STATEMENT , '<name>' , safeName ) ;
245299 stmt = replace ( stmt , '<path>' , pkgPath ) ;
246300 importStmts . push ( stmt ) ;
247301
@@ -253,7 +307,7 @@ function create( fullPath ) {
253307 if ( ! tsDoc ) {
254308 tsDoc = tsDef . match ( / ( \/ \* \* \n [ ^ / ] + ?\* \/ ) \n d e c l a r e c l a s s / ) ;
255309 }
256- prop = '\t' + name + ': typeof ' + name + ';' ;
310+ prop = '\t' + name + ': typeof ' + safeName + ';' ;
257311 if ( tsDoc && tsDoc [ 1 ] ) {
258312 str = tsDoc [ 1 ] ;
259313 str = replace ( str , RE_EXAMPLE , onReplace ) ;
0 commit comments