Skip to content

Commit a27d69b

Browse files
committed
tmp
Signed-off-by: flakey5 <[email protected]>
1 parent 858d77f commit a27d69b

File tree

8 files changed

+262
-198
lines changed

8 files changed

+262
-198
lines changed

src/generators/legacy-json-all/index.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export default {
3838
'methods',
3939
];
4040

41-
for (const section of input) {
41+
input.forEach(section => {
4242
// Copy the relevant properties from each section into our output
43-
for (const property of propertiesToCopy) {
43+
propertiesToCopy.forEach(property => {
4444
if (section[property]) {
4545
generatedValue[property].push(...section[property]);
4646
}
47-
}
48-
}
47+
});
48+
});
4949

5050
await writeFile(
5151
join(output, 'all.json'),
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
import { MiscSection, Section, SignatureSection } from '../legacy-json/types';
1+
import {
2+
MiscSection,
3+
Section,
4+
SignatureSection,
5+
ModuleSection,
6+
} from '../legacy-json/types';
27

38
export interface Output {
4-
miscs: MiscSection[];
5-
modules: Section[];
6-
classes: SignatureSection[];
7-
globals: (ModuleSection | { type: 'global' })[];
8-
methods: SignatureSection[];
9+
miscs: Array<MiscSection>;
10+
modules: Array<Section>;
11+
classes: Array<SignatureSection>;
12+
globals: Array<ModuleSection | { type: 'global' }>;
13+
methods: Array<SignatureSection>;
914
}

src/generators/legacy-json/types.d.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import { ListItem } from 'mdast';
22

33
export interface HierarchizedEntry extends ApiDocMetadataEntry {
4-
hierarchyChildren: ApiDocMetadataEntry[];
4+
hierarchyChildren: Array<ApiDocMetadataEntry>;
55
}
66

7-
export type Section =
8-
| SignatureSection
9-
| PropertySection
10-
| EventSection
11-
| MiscSection;
12-
137
export interface Meta {
14-
changes: ApiDocMetadataChange[];
15-
added?: string[];
16-
napiVersion?: string[];
17-
deprecated?: string[];
18-
removed?: string[];
8+
changes: Array<ApiDocMetadataChange>;
9+
added?: Array<string>;
10+
napiVersion?: Array<string>;
11+
deprecated?: Array<string>;
12+
removed?: Array<string>;
1913
}
2014

2115
export interface SectionBase {
@@ -33,28 +27,34 @@ export interface SectionBase {
3327
export interface ModuleSection extends SectionBase {
3428
type: 'module';
3529
source: string;
36-
miscs?: MiscSection[];
37-
modules?: ModuleSection[];
38-
classes?: SignatureSection[];
39-
methods?: MethodSignature[];
40-
properties?: PropertySection[];
30+
miscs?: Array<MiscSection>;
31+
modules?: Array<ModuleSection>;
32+
classes?: Array<SignatureSection>;
33+
methods?: Array<MethodSignature>;
34+
properties?: Array<PropertySection>;
4135
globals?: ModuleSection | { type: 'global' };
42-
signatures?: SignatureSection[];
36+
signatures?: Array<SignatureSection>;
4337
}
4438

4539
export interface SignatureSection extends SectionBase {
4640
type: 'class' | 'ctor' | 'classMethod' | 'method';
47-
signatures: MethodSignature[];
41+
signatures: Array<MethodSignature>;
4842
}
4943

44+
export type Section =
45+
| SignatureSection
46+
| PropertySection
47+
| EventSection
48+
| MiscSection;
49+
5050
export interface Parameter {
5151
name: string;
5252
optional?: boolean;
5353
default?: string;
5454
}
5555

5656
export interface MethodSignature {
57-
params: Parameter[];
57+
params: Array<Parameter>;
5858
return?: string;
5959
}
6060

@@ -65,7 +65,7 @@ export interface PropertySection extends SectionBase {
6565

6666
export interface EventSection extends SectionBase {
6767
type: 'event';
68-
params: ListItem[];
68+
params: Array<ListItem>;
6969
}
7070

7171
export interface MiscSection extends SectionBase {

src/generators/legacy-json/utils/buildSection.mjs

Lines changed: 13 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
TYPE_EXPRESSION,
1010
} from '../constants.mjs';
1111
import { buildHierarchy } from './buildHierarchy.mjs';
12+
import parseSignature from './parseSignature.mjs';
1213

1314
const sectionTypePlurals = {
1415
module: 'modules',
@@ -33,21 +34,17 @@ function createMeta(entry) {
3334
const makeArrayIfNotAlready = val => (Array.isArray(val) ? val : [val]);
3435

3536
const { added_in, n_api_version, deprecated_in, removed_in, changes } = entry;
36-
if (added_in || n_api_version || deprecated_in || removed_in) {
37-
return {
38-
changes,
39-
added: added_in ? makeArrayIfNotAlready(added_in) : undefined,
40-
napiVersion: n_api_version
41-
? makeArrayIfNotAlready(n_api_version)
42-
: undefined,
43-
deprecated: deprecated_in
44-
? makeArrayIfNotAlready(deprecated_in)
45-
: undefined,
46-
removed: removed_in ? makeArrayIfNotAlready(removed_in) : undefined,
47-
};
48-
}
49-
50-
return undefined;
37+
return {
38+
changes,
39+
added: added_in ? makeArrayIfNotAlready(added_in) : undefined,
40+
napiVersion: n_api_version
41+
? makeArrayIfNotAlready(n_api_version)
42+
: undefined,
43+
deprecated: deprecated_in
44+
? makeArrayIfNotAlready(deprecated_in)
45+
: undefined,
46+
removed: removed_in ? makeArrayIfNotAlready(removed_in) : undefined,
47+
};
5148
}
5249

5350
/**
@@ -68,154 +65,6 @@ function createSection(entry, head) {
6865
};
6966
}
7067

71-
/**
72-
* @param {string} textRaw Something like `new buffer.Blob([sources[, options]])`
73-
* @param {Array<import('../types.d.ts').List} values
74-
* @returns {import('../types.d.ts').MethodSignature}
75-
*/
76-
function parseSignature(textRaw, values) {
77-
/**
78-
* @type {import('../types.d.ts').MethodSignature}
79-
*/
80-
const signature = {
81-
params: [],
82-
};
83-
84-
const rawParameters = values.filter(value => {
85-
if (value.name === 'return') {
86-
signature.return = value;
87-
return false;
88-
}
89-
90-
return true;
91-
});
92-
93-
/**
94-
* Extract a list of the signatures from the method's declaration
95-
* @example `[sources[, options]]`
96-
*/
97-
let [, declaredParameters] = `\`${textRaw}\``.match(PARAM_EXPRESSION) || [];
98-
99-
if (!declaredParameters) {
100-
signature.params = rawParameters;
101-
102-
return signature;
103-
}
104-
105-
/**
106-
* @type {string[]}
107-
* @example ['sources[,', 'options]]']
108-
*/
109-
declaredParameters = declaredParameters.split(',');
110-
111-
let optionalDepth = 0;
112-
const optionalCharDict = { '[': 1, ' ': 0, ']': -1 };
113-
114-
declaredParameters.forEach((declaredParameter, i) => {
115-
/**
116-
* @example 'length]]'
117-
* @example 'arrayBuffer['
118-
* @example '[sources['
119-
* @example 'end'
120-
*/
121-
declaredParameter = declaredParameter.trim();
122-
123-
if (!declaredParameter) {
124-
throw new Error(`Empty parameter slot: ${textRaw}`);
125-
}
126-
127-
// We need to find out if this parameter is optional or not. We can tell this
128-
// if we're wrapped in brackets, so let's look for them.
129-
130-
let pos;
131-
for (pos = 0; pos < declaredParameter.length; pos++) {
132-
const levelChange = optionalCharDict[declaredParameter[pos]];
133-
134-
if (levelChange === undefined) {
135-
break;
136-
}
137-
138-
optionalDepth += levelChange;
139-
}
140-
141-
// Cut off any trailing brackets
142-
declaredParameter = declaredParameter.substring(pos);
143-
144-
const isParameterOptional = optionalDepth > 0;
145-
146-
for (pos = declaredParameter.length - 1; pos >= 0; pos--) {
147-
const levelChange = optionalCharDict[declaredParameter[pos]];
148-
149-
if (levelChange === undefined) {
150-
break;
151-
}
152-
153-
optionalDepth += levelChange;
154-
}
155-
156-
// Cut off any leading brackets
157-
declaredParameter = declaredParameter.substring(0, pos + 1);
158-
159-
// Default value of this parameter in the method's declaration
160-
let defaultValue;
161-
162-
const equalSignPos = declaredParameter.indexOf('=');
163-
if (equalSignPos !== -1) {
164-
// We have a default value, save it and then cut it off of the signature
165-
defaultValue = declaredParameter.substring(equalSignPos, 1).trim();
166-
declaredParameter = declaredParameter.substring(0, equalSignPos);
167-
}
168-
169-
let parameter = rawParameters[i];
170-
if (!parameter || declaredParameter !== parameter.name) {
171-
// If we're here then the method likely has shared signatures
172-
// Something like, `new Console(stdout[, stderr][, ignoreErrors])` and
173-
// `new Console(options)`
174-
parameter = undefined;
175-
176-
// Try finding a parameter this is being shared with
177-
for (const otherParam of rawParameters) {
178-
if (declaredParameter === otherParam.name) {
179-
// Found a matching one
180-
parameter = otherParam;
181-
break;
182-
} else if (otherParam.options) {
183-
// Found a matching one in the parameter's options
184-
for (const option of otherParam.options) {
185-
if (declaredParameter === option.name) {
186-
parameter = Object.assign({}, option);
187-
break;
188-
}
189-
}
190-
}
191-
}
192-
193-
if (!parameter) {
194-
// Couldn't find the shared one
195-
if (declaredParameter.startsWith('...')) {
196-
parameter = { name: declaredParameter };
197-
} else {
198-
throw new Error(
199-
`Invalid param "${declaredParameter}"\n` + ` > ${textRaw}`
200-
);
201-
}
202-
}
203-
}
204-
205-
if (isParameterOptional) {
206-
parameter.optional = true;
207-
}
208-
209-
if (defaultValue) {
210-
parameter.default = defaultValue;
211-
}
212-
213-
signature.params.push(parameter);
214-
});
215-
216-
return signature;
217-
}
218-
21968
/**
22069
* @param {Array<import('mdast').PhrasingContent>} nodes
22170
*/
@@ -534,7 +383,7 @@ export default (head, entries) => {
534383
*/
535384
const rootModule = {
536385
type: 'module',
537-
source: `doc/api/${head.api_doc_source}`,
386+
source: head.api_doc_source,
538387
};
539388

540389
buildHierarchy(entries).forEach(entry => handleEntry(entry, rootModule));

0 commit comments

Comments
 (0)