Skip to content

Commit d31b26d

Browse files
committed
Refactoring of export procedures
1 parent 28e81de commit d31b26d

File tree

7 files changed

+106
-91
lines changed

7 files changed

+106
-91
lines changed

src/scripts/export.ts

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Class, Interface, Method, Namespace, Property, Type } from "./types"
1+
import { Class, Interface, Namespace } from "./types"
2+
import { createNamespace } from "./export/namespace";
23
import * as fs from 'fs';
34
import * as path from 'path';
45

@@ -51,93 +52,3 @@ const assignToNamespace = (s: Class | Interface, ns: Namespace, nsParts: string[
5152
}
5253
}
5354
}
54-
55-
const filterDupliciteStaticProperties = (c: Class, ns: Namespace) => c.properties.filter(p => {
56-
const sameNamespace = ns.namespaces.find(cns => cns.name === c.name);
57-
58-
return !sameNamespace || (!sameNamespace.classes.find(cc => cc.name === p.name) && !sameNamespace.namespaces.find(cns => cns.name === p.name));
59-
})
60-
61-
const propertyAccessMap = { 'normal': '', 'static': 'static ', 'constant': 'static readonly ' };
62-
63-
const createNamespace = (ns: Namespace) => (ns.parent ? (ns.parent.parent ? '' : 'declare ') + 'namespace ' + ns.name + ' {\n' : '')
64-
+ createStructures(ns.classes, ns)
65-
+ (ns.interfaces.length > 0 ? '\n\n' + createStructures(ns.interfaces, ns) : '')
66-
+ (ns.classes.length !== 0 && ns.namespaces.length !== 0 ? '\n\n' : '')
67-
+ ns.namespaces.map(createNamespace).join('\n\n')
68-
+ (ns.parent ? '}' : '' + '\n');
69-
70-
const createStructures = (ss: (Class | Interface)[], ns: Namespace) => ss.map((s) => createStructure(s, ns)).join('\n\n');
71-
72-
const createStructure = (s: Class | Interface, ns: Namespace) => createComment(s)
73-
+ (ns.parent ? '' : 'declare ')
74-
+ s.type + ' ' + s.name + ' ' + (s.type === 'class' ? createClass(s, ns) : createInterface(s, ns));
75-
76-
const createClass = (c: Class, ns: Namespace) => (c.extends ? ' extends ' + c.extends : '')
77-
+ (c?.implements.length > 0 ? ' implements ' + c.implements.join(', ') : '')
78-
+ ' {\n' + createProperties(c, ns) + createMethods(c) + '}';
79-
80-
const createInterface = (i: Interface, ns: Namespace) => (i.extends.length > 0 ? ' extends ' + i.extends.join(', ') : '')
81-
+ ' {\n' + createMethods(i) + '}';
82-
83-
const createProperties = (c: Class, ns: Namespace) => c.properties.length === 0 ? '' : (
84-
filterDupliciteStaticProperties(c, ns)
85-
.map(p => createComment(p) + propertyAccessMap[p.access] + p.name + ';\n')
86-
.join('\n') + '\n'
87-
);
88-
89-
const createMethods = (s: Class | Interface) => s.methods.map(m => createComment(m)
90-
+ (m.static ? 'static ' : '')
91-
+ m.name + '(' + createArguments(m) + ')'
92-
+ (m.type ? ': ' + createType(m.type) : '') + ';\n').join('\n');
93-
94-
const createComment = (o: Method | Class | Interface | Property) => {
95-
const commentSections = [];
96-
97-
if (o.comment) {
98-
commentSections.push(o.comment);
99-
}
100-
if ('events' in o) {
101-
o.events.forEach(e => {
102-
commentSections.push('@fires ' + e.name + (e.comment ? ' ' + e.comment : ''));
103-
})
104-
}
105-
if (o.url) {
106-
commentSections.push('@see ' + o.url);
107-
}
108-
if ('arguments' in o) {
109-
o.arguments.forEach(a => {
110-
const createName = () => {
111-
if (a.optional) {
112-
if (a.defaultValue) {
113-
if (a.type === 'string') {
114-
return '[' + a.name + '="' + a.defaultValue + '"]';
115-
} else {
116-
return '[' + a.name + '=' + a.defaultValue + ']';
117-
}
118-
} else {
119-
return '[' + a.name + ']';
120-
}
121-
} else {
122-
return a.name;
123-
}
124-
}
125-
126-
commentSections.push('@param ' + (a.type ? '{' + createType(a.type) + '} ' : '') + createName() + (a.comment ? ' ' + a.comment : ''));
127-
})
128-
129-
if (o.type && o.type !== 'void') {
130-
commentSections.push('@returns ' + ('{' + createType(o.type) + '}') + (o.returnComment ? ' ' + o.returnComment : ''));
131-
}
132-
}
133-
134-
if (commentSections.length === 0) {
135-
return '';
136-
}
137-
138-
return '/**\n * ' + commentSections.join('\n * ') + '\n */\n';
139-
}
140-
141-
const createArguments = (m: Method) => m.arguments.map(a => a.name + (a.optional ? '?' : '') + (a.type ? ': ' + createType(a.type) : '')).join(', ');
142-
143-
const createType = (type: Type | Type[]) => typeof type === 'string' ? type : type.join(' | ');

src/scripts/export/comment.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Class, Interface, Method, Property } from "../types";
2+
import { createType } from "./variable";
3+
4+
export const createComment = (o: Method | Class | Interface | Property) => {
5+
const commentSections = [];
6+
7+
if (o.comment) {
8+
commentSections.push(o.comment);
9+
}
10+
if ('events' in o) {
11+
o.events.forEach(e => {
12+
commentSections.push('@fires ' + e.name + (e.comment ? ' ' + e.comment : ''));
13+
})
14+
}
15+
if (o.url) {
16+
commentSections.push('@see ' + o.url);
17+
}
18+
if ('arguments' in o) {
19+
o.arguments.forEach(a => {
20+
const createName = () => {
21+
if (a.optional) {
22+
if (a.defaultValue) {
23+
if (a.type === 'string') {
24+
return '[' + a.name + '="' + a.defaultValue + '"]';
25+
} else {
26+
return '[' + a.name + '=' + a.defaultValue + ']';
27+
}
28+
} else {
29+
return '[' + a.name + ']';
30+
}
31+
} else {
32+
return a.name;
33+
}
34+
}
35+
36+
commentSections.push('@param ' + (a.type ? '{' + createType(a.type) + '} ' : '') + createName() + (a.comment ? ' ' + a.comment : ''));
37+
})
38+
39+
if (o.type && o.type !== 'void') {
40+
commentSections.push('@returns ' + ('{' + createType(o.type) + '}') + (o.returnComment ? ' ' + o.returnComment : ''));
41+
}
42+
}
43+
44+
if (commentSections.length === 0) {
45+
return '';
46+
}
47+
48+
return '/**\n * ' + commentSections.join('\n * ') + '\n */\n';
49+
}

src/scripts/export/method.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Class, Interface, Method } from "../types";
2+
import { createComment } from "./comment";
3+
import { createType } from "./variable";
4+
5+
export const createMethods = (s: Class | Interface) => s.methods.map(m => createComment(m)
6+
+ (m.static ? 'static ' : '')
7+
+ m.name + '(' + createArguments(m) + ')'
8+
+ (m.type ? ': ' + createType(m.type) : '') + ';\n').join('\n');
9+
10+
const createArguments = (m: Method) => m.arguments.map(a => a.name + (a.optional ? '?' : '') + (a.type ? ': ' + createType(a.type) : '')).join(', ');

src/scripts/export/namespace.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Namespace } from "../types";
2+
import { createStructures } from "./structure";
3+
4+
export const createNamespace = (ns: Namespace) => (ns.parent ? (ns.parent.parent ? '' : 'declare ') + 'namespace ' + ns.name + ' {\n' : '')
5+
+ createStructures(ns.classes, ns)
6+
+ (ns.interfaces.length > 0 ? '\n\n' + createStructures(ns.interfaces, ns) : '')
7+
+ (ns.classes.length !== 0 && ns.namespaces.length !== 0 ? '\n\n' : '')
8+
+ ns.namespaces.map(createNamespace).join('\n\n')
9+
+ (ns.parent ? '}' : '' + '\n');

src/scripts/export/property.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { createComment } from "./comment";
2+
import { Class, Namespace } from "../types";
3+
4+
export const createProperties = (c: Class, ns: Namespace) => c.properties.length === 0 ? '' : (
5+
filterDupliciteStaticProperties(c, ns)
6+
.map(p => createComment(p) + propertyAccessMap[p.access] + p.name + ';\n')
7+
.join('\n') + '\n'
8+
);
9+
10+
const propertyAccessMap = { 'normal': '', 'static': 'static ', 'constant': 'static readonly ' };
11+
12+
const filterDupliciteStaticProperties = (c: Class, ns: Namespace) => c.properties.filter(p => {
13+
const sameNamespace = ns.namespaces.find(cns => cns.name === c.name);
14+
15+
return !sameNamespace || (!sameNamespace.classes.find(cc => cc.name === p.name) && !sameNamespace.namespaces.find(cns => cns.name === p.name));
16+
})

src/scripts/export/structure.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Class, Interface, Namespace } from "../types";
2+
import { createComment } from "./comment";
3+
import { createMethods } from "./method";
4+
import { createProperties } from "./property";
5+
6+
export const createStructures = (ss: (Class | Interface)[], ns: Namespace) => ss.map((s) => createStructure(s, ns)).join('\n\n');
7+
8+
export const createStructure = (s: Class | Interface, ns: Namespace) => createComment(s)
9+
+ (ns.parent ? '' : 'declare ')
10+
+ s.type + ' ' + s.name + ' ' + (s.type === 'class' ? createClass(s, ns) : createInterface(s, ns));
11+
12+
const createClass = (c: Class, ns: Namespace) => (c.extends ? ' extends ' + c.extends : '')
13+
+ (c?.implements.length > 0 ? ' implements ' + c.implements.join(', ') : '')
14+
+ ' {\n' + createProperties(c, ns) + createMethods(c) + '}';
15+
16+
const createInterface = (i: Interface, ns: Namespace) => (i.extends.length > 0 ? ' extends ' + i.extends.join(', ') : '')
17+
+ ' {\n' + createMethods(i) + '}';

src/scripts/export/variable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Type } from "../types";
2+
3+
export const createType = (type: Type | Type[]) => typeof type === 'string' ? type : type.join(' | ');

0 commit comments

Comments
 (0)