Skip to content

Commit d9391fc

Browse files
committed
rewrite SimulateOldTyping again for clearer structure
1 parent 24a8153 commit d9391fc

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package zzzank.probejs.docs;
22

33
import lombok.val;
4+
import org.jetbrains.annotations.NotNull;
45
import zzzank.probejs.ProbeConfig;
5-
import zzzank.probejs.lang.java.ClassRegistry;
6+
import zzzank.probejs.lang.java.clazz.ClassPath;
67
import zzzank.probejs.lang.typescript.Declaration;
78
import zzzank.probejs.lang.typescript.ScriptDump;
8-
import zzzank.probejs.lang.typescript.code.Code;
99
import zzzank.probejs.lang.typescript.code.member.TypeDecl;
1010
import zzzank.probejs.lang.typescript.code.ts.Wrapped;
1111
import zzzank.probejs.lang.typescript.code.type.BaseType;
1212
import zzzank.probejs.lang.typescript.code.type.Types;
13-
import zzzank.probejs.lang.typescript.refer.ImportInfo;
14-
import zzzank.probejs.lang.typescript.refer.ImportInfos;
1513
import zzzank.probejs.plugin.ProbeJSPlugin;
1614
import zzzank.probejs.utils.CollectUtils;
1715

16+
import java.util.Collections;
1817
import java.util.List;
1918

2019
/**
@@ -24,61 +23,62 @@ public class SimulateOldTyping implements ProbeJSPlugin {
2423

2524
@Override
2625
public void addGlobals(ScriptDump scriptDump) {
27-
if (ProbeConfig.simulateOldTyping.get()) {
28-
scriptDump.addGlobal("simulated_internal", new DocImpl(scriptDump));
26+
if (!ProbeConfig.simulateOldTyping.get()) {
27+
return;
2928
}
30-
}
3129

32-
public static class DocImpl extends Code {
33-
private final ScriptDump scriptDump;
30+
val transpiler = scriptDump.transpiler;
31+
val namespace = new Wrapped.Namespace("Internal");
3432

35-
public DocImpl(ScriptDump scriptDump) {
36-
this.scriptDump = scriptDump;
37-
}
33+
for (val clazz : scriptDump.recordedClasses) {
34+
val path = clazz.classPath;
35+
if (!path.getName().startsWith("$") || transpiler.isRejected(clazz)) {
36+
continue;
37+
}
3838

39-
@Override
40-
public ImportInfos getImportInfos() {
41-
val transpiler = scriptDump.transpiler;
42-
return ImportInfos.of(
43-
scriptDump.recordedClasses.stream()
44-
.filter(c -> !transpiler.isRejected(c))
45-
.map(c -> c.classPath)
46-
.map(ImportInfo::ofDefault));
39+
if (clazz.variableTypes.isEmpty()) {
40+
namespace.addCode(new NameInferredTypeDecl(path).setTypeFormat(BaseType.FormatType.RETURN));
41+
namespace.addCode(new NameInferredTypeDecl(path));
42+
} else {
43+
val variables = CollectUtils.mapToList(
44+
clazz.variableTypes,
45+
transpiler.typeConverter::convertType
46+
);
47+
namespace.addCode(new NameInferredTypeDecl(variables, path).setTypeFormat(BaseType.FormatType.RETURN));
48+
namespace.addCode(new NameInferredTypeDecl(variables, path));
49+
}
4750
}
4851

49-
@Override
50-
public List<String> format(Declaration declaration) {
51-
val transpiler = scriptDump.transpiler;
52-
val namespace = new Wrapped.Namespace("Internal");
52+
scriptDump.addGlobal("simulated_internal", namespace);
53+
}
5354

54-
for (val reference : declaration.references.values()) {
55-
val name = reference.deduped;
56-
if (!name.startsWith("$")) {
57-
continue;
58-
}
55+
public static class NameInferredTypeDecl extends TypeDecl {
56+
private final ClassPath path;
5957

60-
val path = reference.info.path;
61-
val clazz = path.toClazz(ClassRegistry.REGISTRY);
62-
if (clazz == null || transpiler.isRejected(clazz)) {
63-
continue;
64-
}
58+
public NameInferredTypeDecl(@NotNull ClassPath path) {
59+
super("", Types.type(path));
60+
this.path = path;
61+
}
62+
63+
public NameInferredTypeDecl(@NotNull List<? extends BaseType> symbolVariables, @NotNull ClassPath path) {
64+
super("", symbolVariables, Types.type(path).withParams(symbolVariables));
65+
this.path = path;
66+
}
6567

66-
val typeName = name.substring(1);
67-
if (clazz.variableTypes.isEmpty()) {
68-
val type = Types.type(path);
69-
namespace.addCode(new TypeDecl(typeName, type).setTypeFormat(BaseType.FormatType.RETURN));
70-
namespace.addCode(new TypeDecl(typeName + "_", type));
71-
} else {
72-
val variables = CollectUtils.mapToList(
73-
clazz.variableTypes,
74-
transpiler.typeConverter::convertType
75-
);
76-
val type = Types.type(path).withParams(variables);
77-
namespace.addCode(new TypeDecl(typeName, variables, type).setTypeFormat(BaseType.FormatType.RETURN));
78-
namespace.addCode(new TypeDecl(typeName + "_", variables, type));
79-
}
68+
@Override
69+
public List<String> formatRaw(Declaration declaration) {
70+
val reference = declaration.references.get(path);
71+
var name = reference.deduped;
72+
if (!name.startsWith("$")) {
73+
return Collections.emptyList();
74+
}
75+
name = name.substring(1);
76+
if (this.typeFormat == BaseType.FormatType.INPUT) {
77+
name = name + '_';
8078
}
81-
return namespace.format(declaration);
79+
this.name = name;
80+
81+
return super.formatRaw(declaration);
8282
}
8383
}
8484
}

0 commit comments

Comments
 (0)