Skip to content

Commit e1f9442

Browse files
committed
fix(ts-interface-generator): enable running with TypeScript < 4.8
TypeScript 4.8 changed the compiler API incompatibly. This change checks the version and uses the older method signatures if needed. Works at least in TS 4.5 and higher; older versions have not been tested. Tested in all current higher versions up to TS 5.0 and 5.1.1-rc. fix #396
1 parent 1218db8 commit e1f9442

File tree

3 files changed

+194
-84
lines changed

3 files changed

+194
-84
lines changed

packages/ts-interface-generator/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ This is the list of available commandline arguments, including the ones already
5252
- `--version`: Display the version of the generator
5353
- `-c`, `--config`: Path to the configuration file to use
5454
- `-w`, `--watch`: Run in watch mode
55-
- `--loglevel`: Set the console logging verbosity; options are: "error", "warn", "info", "debug", "trace"; default level is "info"
56-
- `--jsdoc`: Set the amount of JSDoc which should be generated; options are: "none", "minimal", "verbose"; default is "minimal": by default, the JSDoc documentation written in the control metadata for the properties, aggregations etc. is also used for the generated methods. By setting `--jsdoc none` or `--jsdoc verbose` you can decide to omit all JSDoc or to also have all the `@param` and `@returns` tags as well as some additional generic documentation generated.
55+
- `--loglevel`: Set the console logging verbosity; options are: `error`, `warn`, `info`, `debug`, `trace`; default level is `info`
56+
- `--jsdoc`: Set the amount of JSDoc which should be generated; options are: `none`, `minimal`, `verbose`; default is `verbose`: by default, the JSDoc documentation written in the control metadata for the properties, aggregations etc. is added to the generated methods, plus generic documentation for the `@param` and `@returns` tags as well as some additional generic documentation like for default values (if any). By setting `--jsdoc none` or `--jsdoc minimal` you can decide to omit all JSDoc or to only add the JSDoc written in the control.
5757

5858
## Limitations
5959

packages/ts-interface-generator/src/astGenerationHelper.ts

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ import Preferences from "./preferences";
99

1010
const factory = ts.factory;
1111

12+
const fixedCreateParameterDeclaration =
13+
parseFloat(ts.version) >= 4.8
14+
? factory.createParameterDeclaration.bind(this) // not used, but required by linting
15+
: function (
16+
...args: [
17+
readonly ts.ModifierLike[],
18+
ts.DotDotDotToken,
19+
string | ts.BindingName,
20+
ts.QuestionToken?,
21+
ts.TypeNode?,
22+
ts.Expression?
23+
]
24+
) {
25+
// @ts-ignore old signature before 4.8 is used here
26+
return factory.createParameterDeclaration(undefined, ...args);
27+
};
28+
1229
function generateSettingsInterface(
1330
classInfo: ClassInfo,
1431
classFileName: string,
@@ -195,7 +212,7 @@ function generateSettingsInterface(
195212
factory.createFunctionTypeNode(
196213
[],
197214
[
198-
factory.createParameterDeclaration(
215+
fixedCreateParameterDeclaration(
199216
undefined,
200217
undefined,
201218
"event",
@@ -252,13 +269,26 @@ function generateSettingsInterface(
252269
settingsSuperclassAsExpression,
253270
]),
254271
];
255-
const myInterface = factory.createInterfaceDeclaration(
256-
undefined,
257-
ownSettingsTypeName,
258-
undefined,
259-
heritageClauses,
260-
interfaceProperties
261-
);
272+
let myInterface;
273+
if (parseFloat(ts.version) >= 4.8) {
274+
myInterface = factory.createInterfaceDeclaration(
275+
undefined,
276+
ownSettingsTypeName,
277+
undefined,
278+
heritageClauses,
279+
interfaceProperties
280+
);
281+
} else {
282+
myInterface = factory.createInterfaceDeclaration(
283+
undefined,
284+
undefined,
285+
ownSettingsTypeName,
286+
undefined,
287+
heritageClauses,
288+
// @ts-ignore: below TS 4.8 there were more params
289+
interfaceProperties
290+
);
291+
}
262292

263293
addLineBreakBefore(myInterface, 2);
264294
ts.addSyntheticLeadingComment(
@@ -359,7 +389,7 @@ function generateMethods(
359389
undefined,
360390
[],
361391
[
362-
factory.createParameterDeclaration(
392+
fixedCreateParameterDeclaration(
363393
undefined,
364394
undefined,
365395
n,
@@ -385,7 +415,7 @@ function generateMethods(
385415
undefined,
386416
[],
387417
[
388-
factory.createParameterDeclaration(
418+
fixedCreateParameterDeclaration(
389419
undefined,
390420
undefined,
391421
"bindingInfo",
@@ -467,7 +497,7 @@ function generateMethods(
467497
undefined,
468498
[],
469499
[
470-
factory.createParameterDeclaration(
500+
fixedCreateParameterDeclaration(
471501
undefined,
472502
undefined,
473503
n,
@@ -492,7 +522,7 @@ function generateMethods(
492522
undefined,
493523
[],
494524
[
495-
factory.createParameterDeclaration(
525+
fixedCreateParameterDeclaration(
496526
undefined,
497527
undefined,
498528
n,
@@ -504,7 +534,7 @@ function generateMethods(
504534
currentClassName
505535
)
506536
),
507-
factory.createParameterDeclaration(
537+
fixedCreateParameterDeclaration(
508538
undefined,
509539
undefined,
510540
"index",
@@ -527,7 +557,7 @@ function generateMethods(
527557
undefined,
528558
[],
529559
[
530-
factory.createParameterDeclaration(
560+
fixedCreateParameterDeclaration(
531561
undefined,
532562
undefined,
533563
n,
@@ -581,7 +611,7 @@ function generateMethods(
581611
undefined,
582612
[],
583613
[
584-
factory.createParameterDeclaration(
614+
fixedCreateParameterDeclaration(
585615
undefined,
586616
undefined,
587617
n,
@@ -612,7 +642,7 @@ function generateMethods(
612642
undefined,
613643
[],
614644
[
615-
factory.createParameterDeclaration(
645+
fixedCreateParameterDeclaration(
616646
undefined,
617647
undefined,
618648
n,
@@ -654,7 +684,7 @@ function generateMethods(
654684
undefined,
655685
[],
656686
[
657-
factory.createParameterDeclaration(
687+
fixedCreateParameterDeclaration(
658688
undefined,
659689
undefined,
660690
"bindingInfo",
@@ -727,7 +757,7 @@ function generateMethods(
727757
undefined,
728758
[],
729759
[
730-
factory.createParameterDeclaration(
760+
fixedCreateParameterDeclaration(
731761
undefined,
732762
undefined,
733763
n,
@@ -760,7 +790,7 @@ function generateMethods(
760790
undefined,
761791
[],
762792
[
763-
factory.createParameterDeclaration(
793+
fixedCreateParameterDeclaration(
764794
undefined,
765795
undefined,
766796
n,
@@ -788,7 +818,7 @@ function generateMethods(
788818
undefined,
789819
[],
790820
[
791-
factory.createParameterDeclaration(
821+
fixedCreateParameterDeclaration(
792822
undefined,
793823
undefined,
794824
n,
@@ -843,7 +873,7 @@ function generateMethods(
843873
const callback = factory.createFunctionTypeNode(
844874
[],
845875
[
846-
factory.createParameterDeclaration(
876+
fixedCreateParameterDeclaration(
847877
undefined,
848878
undefined,
849879
"event",
@@ -864,14 +894,14 @@ function generateMethods(
864894
undefined,
865895
[],
866896
[
867-
factory.createParameterDeclaration(
897+
fixedCreateParameterDeclaration(
868898
undefined,
869899
undefined,
870900
"fn",
871901
undefined,
872902
callback
873903
),
874-
factory.createParameterDeclaration(
904+
fixedCreateParameterDeclaration(
875905
undefined,
876906
undefined,
877907
"listener",
@@ -894,7 +924,7 @@ function generateMethods(
894924
const callbackWithData = factory.createFunctionTypeNode(
895925
[],
896926
[
897-
factory.createParameterDeclaration(
927+
fixedCreateParameterDeclaration(
898928
undefined,
899929
undefined,
900930
"event",
@@ -906,7 +936,7 @@ function generateMethods(
906936
currentClassName
907937
)
908938
),
909-
factory.createParameterDeclaration(
939+
fixedCreateParameterDeclaration(
910940
undefined,
911941
undefined,
912942
"data",
@@ -921,28 +951,34 @@ function generateMethods(
921951
event.methods.attach,
922952
undefined,
923953
[
924-
factory.createTypeParameterDeclaration(
925-
undefined,
926-
"CustomDataType",
927-
factory.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword)
928-
),
954+
parseFloat(ts.version) >= 4.8
955+
? factory.createTypeParameterDeclaration(
956+
undefined,
957+
"CustomDataType",
958+
factory.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword)
959+
)
960+
: factory.createTypeParameterDeclaration(
961+
// @ts-ignore this is the old method signature before TS 4.8
962+
"CustomDataType",
963+
factory.createKeywordTypeNode(ts.SyntaxKind.ObjectKeyword)
964+
),
929965
],
930966
[
931-
factory.createParameterDeclaration(
967+
fixedCreateParameterDeclaration(
932968
undefined,
933969
undefined,
934970
"data",
935971
undefined,
936972
factory.createTypeReferenceNode("CustomDataType")
937973
),
938-
factory.createParameterDeclaration(
974+
fixedCreateParameterDeclaration(
939975
undefined,
940976
undefined,
941977
"fn",
942978
undefined,
943979
callbackWithData
944980
),
945-
factory.createParameterDeclaration(
981+
fixedCreateParameterDeclaration(
946982
undefined,
947983
undefined,
948984
"listener",
@@ -965,14 +1001,14 @@ function generateMethods(
9651001
undefined,
9661002
[],
9671003
[
968-
factory.createParameterDeclaration(
1004+
fixedCreateParameterDeclaration(
9691005
undefined,
9701006
undefined,
9711007
"fn",
9721008
undefined,
9731009
callback
9741010
),
975-
factory.createParameterDeclaration(
1011+
fixedCreateParameterDeclaration(
9761012
undefined,
9771013
undefined,
9781014
"listener",
@@ -997,7 +1033,7 @@ function generateMethods(
9971033
[],
9981034
[
9991035
// TODO: describe parameter object with all details
1000-
factory.createParameterDeclaration(
1036+
fixedCreateParameterDeclaration(
10011037
undefined,
10021038
undefined,
10031039
"parameters",
@@ -1191,7 +1227,7 @@ function createConstructorBlock(settingsTypeName: string) {
11911227
factory.createConstructorDeclaration(
11921228
undefined,
11931229
[
1194-
factory.createParameterDeclaration(
1230+
fixedCreateParameterDeclaration(
11951231
undefined,
11961232
undefined,
11971233
"idOrSettings",
@@ -1212,14 +1248,14 @@ function createConstructorBlock(settingsTypeName: string) {
12121248
factory.createConstructorDeclaration(
12131249
undefined,
12141250
[
1215-
factory.createParameterDeclaration(
1251+
fixedCreateParameterDeclaration(
12161252
undefined,
12171253
undefined,
12181254
"id",
12191255
factory.createToken(ts.SyntaxKind.QuestionToken),
12201256
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
12211257
),
1222-
factory.createParameterDeclaration(
1258+
fixedCreateParameterDeclaration(
12231259
undefined,
12241260
undefined,
12251261
"settings",
@@ -1239,14 +1275,14 @@ function createConstructorBlock(settingsTypeName: string) {
12391275
factory.createConstructorDeclaration(
12401276
undefined,
12411277
[
1242-
factory.createParameterDeclaration(
1278+
fixedCreateParameterDeclaration(
12431279
undefined,
12441280
undefined,
12451281
"id",
12461282
factory.createToken(ts.SyntaxKind.QuestionToken),
12471283
factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
12481284
),
1249-
factory.createParameterDeclaration(
1285+
fixedCreateParameterDeclaration(
12501286
undefined,
12511287
undefined,
12521288
"settings",

0 commit comments

Comments
 (0)