@@ -59,6 +59,12 @@ const md = require('../markdown');
59
59
* }} Metainfo
60
60
*/
61
61
62
+ /**
63
+ * @typedef {{
64
+ * csharpOptionOverloadsShortNotation?: boolean,
65
+ * }} LanguageOptions
66
+ */
67
+
62
68
class Documentation {
63
69
/**
64
70
* @param {!Array<!Documentation.Class> } classesArray
@@ -104,13 +110,14 @@ class Documentation {
104
110
105
111
/**
106
112
* @param {string } lang
113
+ * @param {LanguageOptions= } options
107
114
*/
108
- filterForLanguage ( lang ) {
115
+ filterForLanguage ( lang , options = { } ) {
109
116
const classesArray = [ ] ;
110
117
for ( const clazz of this . classesArray ) {
111
118
if ( clazz . langs . only && ! clazz . langs . only . includes ( lang ) )
112
119
continue ;
113
- clazz . filterForLanguage ( lang ) ;
120
+ clazz . filterForLanguage ( lang , options ) ;
114
121
classesArray . push ( clazz ) ;
115
122
}
116
123
this . classesArray = classesArray ;
@@ -259,13 +266,14 @@ Documentation.Class = class {
259
266
260
267
/**
261
268
* @param {string } lang
269
+ * @param {LanguageOptions= } options
262
270
*/
263
- filterForLanguage ( lang ) {
271
+ filterForLanguage ( lang , options = { } ) {
264
272
const membersArray = [ ] ;
265
273
for ( const member of this . membersArray ) {
266
274
if ( member . langs . only && ! member . langs . only . includes ( lang ) )
267
275
continue ;
268
- member . filterForLanguage ( lang ) ;
276
+ member . filterForLanguage ( lang , options ) ;
269
277
membersArray . push ( member ) ;
270
278
}
271
279
this . membersArray = membersArray ;
@@ -406,31 +414,41 @@ Documentation.Member = class {
406
414
}
407
415
}
408
416
409
- /**
417
+ /**
410
418
* @param {string } lang
419
+ * @param {LanguageOptions= } options
411
420
*/
412
- filterForLanguage ( lang ) {
421
+ filterForLanguage ( lang , options = { } ) {
413
422
if ( ! this . type )
414
423
return ;
415
424
if ( this . langs . aliases && this . langs . aliases [ lang ] )
416
425
this . alias = this . langs . aliases [ lang ] ;
417
426
if ( this . langs . types && this . langs . types [ lang ] )
418
427
this . type = this . langs . types [ lang ] ;
419
- this . type . filterForLanguage ( lang ) ;
428
+ this . type . filterForLanguage ( lang , options ) ;
420
429
const argsArray = [ ] ;
421
430
for ( const arg of this . argsArray ) {
422
431
if ( arg . langs . only && ! arg . langs . only . includes ( lang ) )
423
432
continue ;
424
433
const overriddenArg = ( arg . langs . overrides && arg . langs . overrides [ lang ] ) || arg ;
425
- overriddenArg . filterForLanguage ( lang ) ;
434
+ overriddenArg . filterForLanguage ( lang , options ) ;
426
435
// @ts -ignore
427
436
if ( overriddenArg . name === 'options' && ! overriddenArg . type . properties . length )
428
437
continue ;
429
438
// @ts -ignore
430
- overriddenArg . type . filterForLanguage ( lang ) ;
439
+ overriddenArg . type . filterForLanguage ( lang , options ) ;
431
440
argsArray . push ( overriddenArg ) ;
432
441
}
433
442
this . argsArray = argsArray ;
443
+
444
+ const optionsArg = this . argsArray . find ( arg => arg . name === 'options' ) ;
445
+ if ( lang === 'csharp' && optionsArg ) {
446
+ try {
447
+ patchCSharpOptionOverloads ( optionsArg , options ) ;
448
+ } catch ( e ) {
449
+ throw new Error ( `Error processing csharp options in ${ this . clazz ?. name } .${ this . name } : ` + e . message ) ;
450
+ }
451
+ }
434
452
}
435
453
436
454
filterOutExperimental ( ) {
@@ -642,15 +660,16 @@ Documentation.Type = class {
642
660
643
661
/**
644
662
* @param {string } lang
663
+ * @param {LanguageOptions= } options
645
664
*/
646
- filterForLanguage ( lang ) {
665
+ filterForLanguage ( lang , options = { } ) {
647
666
if ( ! this . properties )
648
667
return ;
649
668
const properties = [ ] ;
650
669
for ( const prop of this . properties ) {
651
670
if ( prop . langs . only && ! prop . langs . only . includes ( lang ) )
652
671
continue ;
653
- prop . filterForLanguage ( lang ) ;
672
+ prop . filterForLanguage ( lang , options ) ;
654
673
properties . push ( prop ) ;
655
674
}
656
675
this . properties = properties ;
@@ -839,4 +858,73 @@ function generateSourceCodeComment(spec) {
839
858
return md . render ( comments , 120 ) ;
840
859
}
841
860
861
+ /**
862
+ * @param {Documentation.Member } optionsArg
863
+ * @param {LanguageOptions= } options
864
+ */
865
+ function patchCSharpOptionOverloads ( optionsArg , options = { } ) {
866
+ const props = optionsArg . type ?. properties ;
867
+ if ( ! props )
868
+ return ;
869
+ const propsToDelete = new Set ( ) ;
870
+ const propsToAdd = [ ] ;
871
+ for ( const prop of props ) {
872
+ const union = prop . type ?. union ;
873
+ if ( ! union )
874
+ continue ;
875
+ const isEnum = union [ 0 ] . name . startsWith ( '"' ) ;
876
+ const isNullable = union . length === 2 && union . some ( type => type . name === 'null' ) ;
877
+ if ( isEnum || isNullable )
878
+ continue ;
879
+
880
+ const shortNotation = [ ] ;
881
+ propsToDelete . add ( prop ) ;
882
+ for ( const type of union ) {
883
+ const suffix = csharpOptionOverloadSuffix ( prop . name , type . name ) ;
884
+ if ( options . csharpOptionOverloadsShortNotation ) {
885
+ if ( type . name === 'string' )
886
+ shortNotation . push ( prop . alias ) ;
887
+ else
888
+ shortNotation . push ( prop . alias + suffix ) ;
889
+ continue ;
890
+ }
891
+
892
+ const newProp = prop . clone ( ) ;
893
+ newProp . name = prop . name + suffix ;
894
+ newProp . alias = prop . alias + suffix ;
895
+ newProp . type = type ;
896
+ propsToAdd . push ( newProp ) ;
897
+
898
+ if ( type . name === 'string' ) {
899
+ const stringProp = prop . clone ( ) ;
900
+ stringProp . type = type ;
901
+ propsToAdd . push ( stringProp ) ;
902
+ }
903
+ }
904
+ if ( options . csharpOptionOverloadsShortNotation ) {
905
+ const newProp = prop . clone ( ) ;
906
+ newProp . alias = newProp . name = shortNotation . join ( '|' ) ;
907
+ propsToAdd . push ( newProp ) ;
908
+ }
909
+ }
910
+ for ( const prop of propsToDelete )
911
+ props . splice ( props . indexOf ( prop ) , 1 ) ;
912
+ props . push ( ...propsToAdd ) ;
913
+ }
914
+
915
+ /**
916
+ * @param {string } option
917
+ * @param {string } type
918
+ */
919
+ function csharpOptionOverloadSuffix ( option , type ) {
920
+ switch ( type ) {
921
+ case 'string' : return 'String' ;
922
+ case 'RegExp' : return 'Regex' ;
923
+ case 'function' : return 'Func' ;
924
+ case 'Buffer' : return 'Byte' ;
925
+ case 'Serializable' : return 'Object' ;
926
+ }
927
+ throw new Error ( `CSharp option "${ option } " has unsupported type overload "${ type } "` ) ;
928
+ }
929
+
842
930
module . exports = Documentation ;
0 commit comments