Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
Signed-off-by: flakey5 <[email protected]>
  • Loading branch information
flakey5 committed Oct 31, 2024
1 parent 858d77f commit a27d69b
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 198 deletions.
8 changes: 4 additions & 4 deletions src/generators/legacy-json-all/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export default {
'methods',
];

for (const section of input) {
input.forEach(section => {
// Copy the relevant properties from each section into our output
for (const property of propertiesToCopy) {
propertiesToCopy.forEach(property => {
if (section[property]) {
generatedValue[property].push(...section[property]);
}
}
}
});
});

await writeFile(
join(output, 'all.json'),
Expand Down
17 changes: 11 additions & 6 deletions src/generators/legacy-json-all/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { MiscSection, Section, SignatureSection } from '../legacy-json/types';
import {
MiscSection,
Section,
SignatureSection,
ModuleSection,
} from '../legacy-json/types';

export interface Output {
miscs: MiscSection[];
modules: Section[];
classes: SignatureSection[];
globals: (ModuleSection | { type: 'global' })[];
methods: SignatureSection[];
miscs: Array<MiscSection>;
modules: Array<Section>;
classes: Array<SignatureSection>;
globals: Array<ModuleSection | { type: 'global' }>;
methods: Array<SignatureSection>;
}
42 changes: 21 additions & 21 deletions src/generators/legacy-json/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { ListItem } from 'mdast';

export interface HierarchizedEntry extends ApiDocMetadataEntry {
hierarchyChildren: ApiDocMetadataEntry[];
hierarchyChildren: Array<ApiDocMetadataEntry>;
}

export type Section =
| SignatureSection
| PropertySection
| EventSection
| MiscSection;

export interface Meta {
changes: ApiDocMetadataChange[];
added?: string[];
napiVersion?: string[];
deprecated?: string[];
removed?: string[];
changes: Array<ApiDocMetadataChange>;
added?: Array<string>;
napiVersion?: Array<string>;
deprecated?: Array<string>;
removed?: Array<string>;
}

export interface SectionBase {
Expand All @@ -33,28 +27,34 @@ export interface SectionBase {
export interface ModuleSection extends SectionBase {
type: 'module';
source: string;
miscs?: MiscSection[];
modules?: ModuleSection[];
classes?: SignatureSection[];
methods?: MethodSignature[];
properties?: PropertySection[];
miscs?: Array<MiscSection>;
modules?: Array<ModuleSection>;
classes?: Array<SignatureSection>;
methods?: Array<MethodSignature>;
properties?: Array<PropertySection>;
globals?: ModuleSection | { type: 'global' };
signatures?: SignatureSection[];
signatures?: Array<SignatureSection>;
}

export interface SignatureSection extends SectionBase {
type: 'class' | 'ctor' | 'classMethod' | 'method';
signatures: MethodSignature[];
signatures: Array<MethodSignature>;
}

export type Section =
| SignatureSection
| PropertySection
| EventSection
| MiscSection;

export interface Parameter {
name: string;
optional?: boolean;
default?: string;
}

export interface MethodSignature {
params: Parameter[];
params: Array<Parameter>;
return?: string;
}

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

export interface EventSection extends SectionBase {
type: 'event';
params: ListItem[];
params: Array<ListItem>;
}

export interface MiscSection extends SectionBase {
Expand Down
177 changes: 13 additions & 164 deletions src/generators/legacy-json/utils/buildSection.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TYPE_EXPRESSION,
} from '../constants.mjs';
import { buildHierarchy } from './buildHierarchy.mjs';
import parseSignature from './parseSignature.mjs';

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

const { added_in, n_api_version, deprecated_in, removed_in, changes } = entry;
if (added_in || n_api_version || deprecated_in || removed_in) {
return {
changes,
added: added_in ? makeArrayIfNotAlready(added_in) : undefined,
napiVersion: n_api_version
? makeArrayIfNotAlready(n_api_version)
: undefined,
deprecated: deprecated_in
? makeArrayIfNotAlready(deprecated_in)
: undefined,
removed: removed_in ? makeArrayIfNotAlready(removed_in) : undefined,
};
}

return undefined;
return {
changes,
added: added_in ? makeArrayIfNotAlready(added_in) : undefined,
napiVersion: n_api_version
? makeArrayIfNotAlready(n_api_version)
: undefined,
deprecated: deprecated_in
? makeArrayIfNotAlready(deprecated_in)
: undefined,
removed: removed_in ? makeArrayIfNotAlready(removed_in) : undefined,
};
}

/**
Expand All @@ -68,154 +65,6 @@ function createSection(entry, head) {
};
}

/**
* @param {string} textRaw Something like `new buffer.Blob([sources[, options]])`
* @param {Array<import('../types.d.ts').List} values
* @returns {import('../types.d.ts').MethodSignature}
*/
function parseSignature(textRaw, values) {
/**
* @type {import('../types.d.ts').MethodSignature}
*/
const signature = {
params: [],
};

const rawParameters = values.filter(value => {
if (value.name === 'return') {
signature.return = value;
return false;
}

return true;
});

/**
* Extract a list of the signatures from the method's declaration
* @example `[sources[, options]]`
*/
let [, declaredParameters] = `\`${textRaw}\``.match(PARAM_EXPRESSION) || [];

if (!declaredParameters) {
signature.params = rawParameters;

return signature;
}

/**
* @type {string[]}
* @example ['sources[,', 'options]]']
*/
declaredParameters = declaredParameters.split(',');

let optionalDepth = 0;
const optionalCharDict = { '[': 1, ' ': 0, ']': -1 };

declaredParameters.forEach((declaredParameter, i) => {
/**
* @example 'length]]'
* @example 'arrayBuffer['
* @example '[sources['
* @example 'end'
*/
declaredParameter = declaredParameter.trim();

if (!declaredParameter) {
throw new Error(`Empty parameter slot: ${textRaw}`);
}

// We need to find out if this parameter is optional or not. We can tell this
// if we're wrapped in brackets, so let's look for them.

let pos;
for (pos = 0; pos < declaredParameter.length; pos++) {
const levelChange = optionalCharDict[declaredParameter[pos]];

if (levelChange === undefined) {
break;
}

optionalDepth += levelChange;
}

// Cut off any trailing brackets
declaredParameter = declaredParameter.substring(pos);

const isParameterOptional = optionalDepth > 0;

for (pos = declaredParameter.length - 1; pos >= 0; pos--) {
const levelChange = optionalCharDict[declaredParameter[pos]];

if (levelChange === undefined) {
break;
}

optionalDepth += levelChange;
}

// Cut off any leading brackets
declaredParameter = declaredParameter.substring(0, pos + 1);

// Default value of this parameter in the method's declaration
let defaultValue;

const equalSignPos = declaredParameter.indexOf('=');
if (equalSignPos !== -1) {
// We have a default value, save it and then cut it off of the signature
defaultValue = declaredParameter.substring(equalSignPos, 1).trim();
declaredParameter = declaredParameter.substring(0, equalSignPos);
}

let parameter = rawParameters[i];
if (!parameter || declaredParameter !== parameter.name) {
// If we're here then the method likely has shared signatures
// Something like, `new Console(stdout[, stderr][, ignoreErrors])` and
// `new Console(options)`
parameter = undefined;

// Try finding a parameter this is being shared with
for (const otherParam of rawParameters) {
if (declaredParameter === otherParam.name) {
// Found a matching one
parameter = otherParam;
break;
} else if (otherParam.options) {
// Found a matching one in the parameter's options
for (const option of otherParam.options) {
if (declaredParameter === option.name) {
parameter = Object.assign({}, option);
break;
}
}
}
}

if (!parameter) {
// Couldn't find the shared one
if (declaredParameter.startsWith('...')) {
parameter = { name: declaredParameter };
} else {
throw new Error(
`Invalid param "${declaredParameter}"\n` + ` > ${textRaw}`
);
}
}
}

if (isParameterOptional) {
parameter.optional = true;
}

if (defaultValue) {
parameter.default = defaultValue;
}

signature.params.push(parameter);
});

return signature;
}

/**
* @param {Array<import('mdast').PhrasingContent>} nodes
*/
Expand Down Expand Up @@ -534,7 +383,7 @@ export default (head, entries) => {
*/
const rootModule = {
type: 'module',
source: `doc/api/${head.api_doc_source}`,
source: head.api_doc_source,
};

buildHierarchy(entries).forEach(entry => handleEntry(entry, rootModule));
Expand Down
Loading

0 comments on commit a27d69b

Please sign in to comment.